ds.ov2.util
Class Misc

java.lang.Object
  extended by ds.ov2.util.Misc

public class Misc
extends Object

Collection of miscellaneous methods that do not fit anywhere else, most of them abstracting from differences between standard Java and Java Card.

Static class.

CPP Preprocessing
This class uses the following cpp defines: PACKAGE, PUBLIC, ASSERT, JAVACARD_APPLET, MESSAGE_DIGEST, RANDOM
Execution Environment:
host, card
Author:
Hendrik Tews
Version:
$Revision: 1.14 $
Last Commit:
$Date: 2009-06-17 21:16:39 $ by $Author: tews $

Constructor Summary
protected Misc()
          Static class, object creation disabled.
 
Method Summary
static byte[] allocate_transient_byte_array(short size)
          Portable RAM allocation.
static void array_copy(byte[] src, short src_off, byte[] dest, short dest_off, short len)
          Portable array copy.
static void begin_transaction()
          Portable transaction start call.
static void commit_transaction()
          Portable transaction end call.
static MessageDigest get_message_digest()
          Portable way of creating a portable message digest instance computing SHA 160 bit digests.
static RandomData get_new_rand()
          Portable way to get an instance of a random number generator.
static short length_of_serializable_array(APDU_Serializable[] a)
          Total size of an APDU_Serializable array.
static void myassert(boolean condition, short tag)
          Implementation of the ASSERT and ASSERT_TAG macros.
static void rand_data_int(RandomData rand, int[] ba, short start, int len)
          Fill an integer array with random data.
static void rand_data(RandomData rand, byte[] ba, short start, short len)
          Portable way to fill a byte array with random data.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Misc

protected Misc()
Static class, object creation disabled.

Method Detail

myassert

public static void myassert(boolean condition,
                            short tag)
Implementation of the ASSERT and ASSERT_TAG macros. Aborts the computation if condition is false. On the card (if JAVACARD_APPLET is defined) this is done by throwing an ISOException with status Response_status.OV_ASSERTION_00, where tag is or-ed into the lower 8 bits of the response status. On the host (if JAVACARD_APPLET is undefined) it does simply assert false.

A list with all currently used assert tag is contained in the overview page.

Parameters:
condition - abort computation if condition is false
tag - 1-byte tag providing additional information about the assertion
Throws:
ISOException - with Response_status.OV_ASSERTION_00 on the card, if the condition is false
AssertionError - on the host, if the condition is false

array_copy

public static final void array_copy(byte[] src,
                                    short src_off,
                                    byte[] dest,
                                    short dest_off,
                                    short len)
Portable array copy. Abstracts from the different names of the array copy routines on standard Java and Java Card. Does a non-atomic copy on the card.

Behaves either like arrayCopyNonAtomic or System.arraycopy depending on whether JAVACARD_APPLET is defined.

Parameters:
src - the source array
src_off - starting position in the source array
dest - the destination array
dest_off - starting position in the destination array
len - the number of elements to be copied

allocate_transient_byte_array

public static byte[] allocate_transient_byte_array(short size)
Portable RAM allocation. Allocates a byte array in transient memory (RAM) on the card (if JAVACARD_APPLET is defined). Does a normal allocation on the host (if JAVACARD_APPLET is undefined).

Parameters:
size - length of the byte array
Returns:
a freshly allocated byte array
See Also:
JCSystem.makeTransientByteArray

length_of_serializable_array

public static short length_of_serializable_array(APDU_Serializable[] a)
Total size of an APDU_Serializable array.

Parameters:
a - the array, might be a null reference
Returns:
the sum of the sizes of all elements in a
Throws:
NullPointerException - if one of the elements in a is null

get_new_rand

public static RandomData get_new_rand()
Portable way to get an instance of a random number generator. Does RandomData.getInstance(RandomData.ALG_SECURE_RANDOM) on the card (if JAVACARD_APPLET is defined) and new Random on the host (if JAVACARD_APPLET is undefined).

The return type is RANDOM, which expands to RandomData on the card and Random on the host.

Returns:
an instance of a random number generator

rand_data

public static void rand_data(RandomData rand,
                             byte[] ba,
                             short start,
                             short len)
Portable way to fill a byte array with random data. Does generateData on the card (if JAVACARD_APPLET is defined) and nextBytes on the host (if JAVACARD_APPLET is undefined).

Parameters:
rand - the random number instance of type RANDOM
ba - the byte array to fill with random data
start - starting index in ba
len - number of random bytes to store in ba starting at start

rand_data_int

public static void rand_data_int(RandomData rand,
                                 int[] ba,
                                 short start,
                                 int len)
Fill an integer array with random data. Same as rand_data but for an integer array.

Only available if JAVACARD_APPLET is undefined (there are no ints on Java Card anyway).

Parameters:
rand - the random number instance of type RANDOM
ba - the int array to fill with random data
start - starting index in ba
len - number of random ints to store in ba starting at start

get_message_digest

public static MessageDigest get_message_digest()
Portable way of creating a portable message digest instance computing SHA 160 bit digests. Does MessageDigest.getInstance(MessageDigest.ALG_SHA) on the card (if JAVACARD_APPLET is defined) and new Message_digest_wrapper on the host (if JAVACARD_APPLET is undefined).

Only available if MESSAGE_DIGEST is defined to avoid needing Message_digest_wrapper in all host side programs.

Returns:
a message digest instance of type MESSAGE_DIGEST, which expands to MessageDigest on the card and Message_digest_wrapper on the host.

begin_transaction

public static void begin_transaction()
Portable transaction start call. Does JCSystem.beginTransaction() on the card (if JAVACARD_APPLET is defined) and nothing on the host (if JAVACARD_APPLET is undefined). So on the host no transaction is started. Needed for the APPLET_TESTFRAME.


commit_transaction

public static void commit_transaction()
Portable transaction end call. Does JCSystem.commitTransaction() on the card (if JAVACARD_APPLET is defined) and nothing on the host (if JAVACARD_APPLET is undefined). So on the host no transaction is done. Needed for the APPLET_TESTFRAME.