SFE.BOAL
Class MyUtil

java.lang.Object
  extended by SFE.BOAL.MyUtil

public class MyUtil
extends java.lang.Object

This class initializes general services required by other classes, namely a hash-function and an RNG.

Author:
Dahlia Malkhi and Yaron Sella

Field Summary
static java.math.BigInteger dp
           
static java.math.BigInteger dq
           
static int key_size
           
static java.security.MessageDigest md
           
static java.math.BigInteger modulus
           
static java.math.BigInteger pinverseq
           
static java.security.SecureRandom random
           
 
Constructor Summary
MyUtil()
           
 
Method Summary
static void BigInt2FixedBytes(java.math.BigInteger num, byte[] res)
          BigInt2FixedBytes: converts a BigInteger (smaller than the El-Gamal prime) into a fixed size byte array.
static void BigInts2FixedBytes(java.math.BigInteger[] nums, byte[] res)
          BigInts2FixedBytes: converts an array of BigIntegers (smaller than the El-Gamal prime) into a fixed size byte array.
static byte[] decArrays(byte[] cipher, byte[] key)
          decArrays - Decrypt cipher-text array with key array by XOR
static java.math.BigInteger decrypt(java.math.BigInteger c)
           
static long deltaHeap()
          deltaHeap - tool for measuring size of heap, returns the heap size delta (in bytes) from previous call to this routine.
static long deltaTime(boolean reset_time)
          deltaTime - profiling tool for measuring time spent in different program sections, returns the delta time (in milli-secs) from previous call to this routine.
static byte[] EG_decrypt(java.math.BigInteger gr, java.math.BigInteger yrm, java.math.BigInteger x)
          EG_decrypt: Perform El-Gamal decryption
static java.math.BigInteger EG_deduce(java.math.BigInteger pub_key0)
          EG_deduce: Deduce one El-Gamal public key from another
static java.math.BigInteger EG_encrypt(java.math.BigInteger y, java.math.BigInteger r, byte[] packed_code)
          EG_encrypt: Perform El-Gamal encryption
static java.math.BigInteger EG_g()
          EG_g: return the EG generator g
static java.math.BigInteger EG_genPublic(int selector, java.math.BigInteger x)
          EG_genPublic: generate a public El-Gamal encryption key
static java.math.BigInteger EG_pow(java.math.BigInteger pow)
          EG_pow: Perform modular exponentiation g^pow mod p
static java.math.BigInteger EG_randExp()
          EG_randExp: generate an EG random exponent
static byte[] encArrays(byte[] plain, byte[] key)
          encArrays - Encrypt plain-text array with key array by XOR
static java.math.BigInteger encrypt(java.math.BigInteger m)
           
static java.math.BigInteger FixedBytes2BigInt(byte[] ba)
          FixedBytes2BigInt: converts a fixed size byte array to a BigInteger (smaller than the El-Gamal prime)
static java.math.BigInteger[] FixedBytes2BigInts(byte[] ba, int pos, int how_many)
          FixedBytes2BigInts: converts a fixed size byte array to an array of BigIntegers (smaller than the El-Gamal prime)
static byte[] hash(byte[] inp, int no_out_bytes)
          hash
static void init(java.lang.String seed)
          Initialize
static void newRSA(int N)
          newRSA - receive the sixe of the new modulus creates new RSA key, prints it and EXITS!!!
static java.math.BigInteger pad(java.math.BigInteger bi)
           
static java.lang.String pathFile(java.lang.String fname)
          pathFile - concatenate $rundir to a filename
static java.math.BigInteger rand(int size)
          rand: generate an random number of max size
static byte randomByte()
          Generate one random byte
static void randomBytes(byte[] result)
          Generate random bytes
static void receiveBytes(java.io.ObjectInputStream ois, byte[] in, int len)
          receiveBytes - receive an array of bytes via ObjectInputStream
static int receiveInt(java.io.ObjectInputStream ois)
          receiveInt - receive an int via ObjectInputStream
static SFE.BOAL.OTMESS receiveOTMESS(java.io.ObjectInputStream ois)
          receiveOTMESS - receive an OTMESS object via ObjectInputStream
static java.util.Vector receiveVector(java.io.ObjectInputStream ois)
          receiveVector - receive a Vector object via ObjectInputStream
static void sendBytes(java.io.ObjectOutputStream oos, byte[] a, boolean flush)
          sendBytes - send an array of bytes via ObjectOutputStream
static void sendInt(java.io.ObjectOutputStream oos, int k, boolean flush)
          sendInt - send an int via ObjectOutputStream
static void sendOTMESS(java.io.ObjectOutputStream oos, SFE.BOAL.OTMESS otmess)
          sendOTMESS - send an OTMESS object via ObjectOutputStream
static void sendVector(java.io.ObjectOutputStream oos, java.util.Vector v)
          sendVector - send a Vector object via ObjectOutputStream
static byte[] toByteArray(int in)
          Convert int to a byte array.
static java.lang.String toHexStr(byte b)
          toHexStr - turn a byte to a hex String (for debugging)
static java.lang.String toHexString(byte[] arr)
          toHexString - turn byte array to a hex String (for debugging purposes)
static byte[] xorArrays(byte[] inp1, byte[] inp2)
          xorArrays - XOR one byte-array with another
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

md

public static java.security.MessageDigest md

random

public static java.security.SecureRandom random

key_size

public static int key_size

modulus

public static java.math.BigInteger modulus

dp

public static java.math.BigInteger dp

dq

public static java.math.BigInteger dq

pinverseq

public static java.math.BigInteger pinverseq
Constructor Detail

MyUtil

public MyUtil()
Method Detail

init

public static void init(java.lang.String seed)
Initialize


hash

public static byte[] hash(byte[] inp,
                          int no_out_bytes)
hash


randomByte

public static byte randomByte()
Generate one random byte

Returns:
- a single random byte.

randomBytes

public static void randomBytes(byte[] result)
Generate random bytes

Parameters:
result - - a byte array for generated random bytes.

encArrays

public static byte[] encArrays(byte[] plain,
                               byte[] key)
encArrays - Encrypt plain-text array with key array by XOR

Parameters:
plain - - plain text byte array.
key - - key byte array.
Returns:
a byte array containing XOR of plain & key.

decArrays

public static byte[] decArrays(byte[] cipher,
                               byte[] key)
decArrays - Decrypt cipher-text array with key array by XOR

Parameters:
cipher - - cipher text byte array.
key - - key byte array.
Returns:
a byte array containing XOR of cipher & key.

xorArrays

public static byte[] xorArrays(byte[] inp1,
                               byte[] inp2)
xorArrays - XOR one byte-array with another

Parameters:
inp1 - - 1st byte array to be XORed.
inp2 - - 2nd byte array to be XORed.
Returns:
XOR of inp1 & inp2 (byte byte) in a byte array.

toHexStr

public static java.lang.String toHexStr(byte b)
toHexStr - turn a byte to a hex String (for debugging)

Parameters:
b - - a byte.
Returns:
String containing the byte in hex

toHexString

public static java.lang.String toHexString(byte[] arr)
toHexString - turn byte array to a hex String (for debugging purposes)

Parameters:
arr - - byte array.
Returns:
String containing the byte array in hex

toByteArray

public static byte[] toByteArray(int in)
Convert int to a byte array.

Parameters:
in - An int to be converted to a byte array.
Returns:
a byte array containing the int.

pathFile

public static java.lang.String pathFile(java.lang.String fname)
pathFile - concatenate $rundir to a filename

Parameters:
fname - - file name to concatenate to.
Returns:
String containing $rundir + fname

deltaTime

public static long deltaTime(boolean reset_time)
deltaTime - profiling tool for measuring time spent in different program sections, returns the delta time (in milli-secs) from previous call to this routine.

Parameters:
reset_time - - reset timing measurements.
Returns:
long - delta time (in milli-secs) from previous call.

deltaHeap

public static long deltaHeap()
deltaHeap - tool for measuring size of heap, returns the heap size delta (in bytes) from previous call to this routine.

Returns:
long - delta heap size (in bytes) from previous call.

EG_genPublic

public static java.math.BigInteger EG_genPublic(int selector,
                                                java.math.BigInteger x)
EG_genPublic: generate a public El-Gamal encryption key

Parameters:
selector - a 0/1 value selecting which of the two public keys is real and which is 'fake'
x - The El-Gamal private key
Returns:
A public El-Gamal encryption key

EG_deduce

public static java.math.BigInteger EG_deduce(java.math.BigInteger pub_key0)
EG_deduce: Deduce one El-Gamal public key from another

Parameters:
pub_key0 - An EG public key
Returns:
A deduced EG public key

EG_encrypt

public static java.math.BigInteger EG_encrypt(java.math.BigInteger y,
                                              java.math.BigInteger r,
                                              byte[] packed_code)
EG_encrypt: Perform El-Gamal encryption

Parameters:
y - El-Gamal public key
r - El-Gamal random secret exponent
packed_code - data to encrypt
Returns:
BigInteger containing the ciphertext

EG_decrypt

public static byte[] EG_decrypt(java.math.BigInteger gr,
                                java.math.BigInteger yrm,
                                java.math.BigInteger x)
EG_decrypt: Perform El-Gamal decryption

Parameters:
gr - g^r part of El-Gamal ciphertext
yrm - (y^r)*M part of El-Gamal ciphertext
Returns:
Byte-array with the clear text

EG_pow

public static java.math.BigInteger EG_pow(java.math.BigInteger pow)
EG_pow: Perform modular exponentiation g^pow mod p

Parameters:
pow - to exponentiate to
Returns:
BigInteger containing the result

EG_randExp

public static java.math.BigInteger EG_randExp()
EG_randExp: generate an EG random exponent

Returns:
BigInteger containing the result

rand

public static java.math.BigInteger rand(int size)
rand: generate an random number of max size

Returns:
BigInteger containing the result

EG_g

public static java.math.BigInteger EG_g()
EG_g: return the EG generator g

Returns:
BigInteger containing g

BigInt2FixedBytes

public static void BigInt2FixedBytes(java.math.BigInteger num,
                                     byte[] res)
BigInt2FixedBytes: converts a BigInteger (smaller than the El-Gamal prime) into a fixed size byte array.


FixedBytes2BigInt

public static java.math.BigInteger FixedBytes2BigInt(byte[] ba)
FixedBytes2BigInt: converts a fixed size byte array to a BigInteger (smaller than the El-Gamal prime)


BigInts2FixedBytes

public static void BigInts2FixedBytes(java.math.BigInteger[] nums,
                                      byte[] res)
BigInts2FixedBytes: converts an array of BigIntegers (smaller than the El-Gamal prime) into a fixed size byte array.


FixedBytes2BigInts

public static java.math.BigInteger[] FixedBytes2BigInts(byte[] ba,
                                                        int pos,
                                                        int how_many)
FixedBytes2BigInts: converts a fixed size byte array to an array of BigIntegers (smaller than the El-Gamal prime)


sendOTMESS

public static void sendOTMESS(java.io.ObjectOutputStream oos,
                              SFE.BOAL.OTMESS otmess)
sendOTMESS - send an OTMESS object via ObjectOutputStream

Parameters:
oos - - ObjectOutputStream to send the object on.
otmess - - object to send.

receiveOTMESS

public static SFE.BOAL.OTMESS receiveOTMESS(java.io.ObjectInputStream ois)
receiveOTMESS - receive an OTMESS object via ObjectInputStream

Parameters:
ois - - ObjectInputStream to receive the object on.
Returns:
OTMESS that was received.

sendInt

public static void sendInt(java.io.ObjectOutputStream oos,
                           int k,
                           boolean flush)
sendInt - send an int via ObjectOutputStream

Parameters:
oos - - ObjectOutputStream to send the object on.
k - - int to send.
flush - - whether to flus or not.

receiveInt

public static int receiveInt(java.io.ObjectInputStream ois)
receiveInt - receive an int via ObjectInputStream

Parameters:
ois - - ObjectInputStream to receive the object on.
Returns:
int that was received.

sendBytes

public static void sendBytes(java.io.ObjectOutputStream oos,
                             byte[] a,
                             boolean flush)
sendBytes - send an array of bytes via ObjectOutputStream

Parameters:
oos - - ObjectOutputStream to send the object on.
a - - array of bytes to send.
flush - - whether to flus or not.

receiveBytes

public static void receiveBytes(java.io.ObjectInputStream ois,
                                byte[] in,
                                int len)
receiveBytes - receive an array of bytes via ObjectInputStream

Parameters:
ois - - ObjectInputStream to receive the object on.

sendVector

public static void sendVector(java.io.ObjectOutputStream oos,
                              java.util.Vector v)
sendVector - send a Vector object via ObjectOutputStream

Parameters:
oos - - ObjectOutputStream to send the object on.
v - - object to send.

receiveVector

public static java.util.Vector receiveVector(java.io.ObjectInputStream ois)
receiveVector - receive a Vector object via ObjectInputStream

Parameters:
ois - - ObjectInputStream to receive the object on.
Returns:
Vector that was received.

newRSA

public static void newRSA(int N)
newRSA - receive the sixe of the new modulus creates new RSA key, prints it and EXITS!!!

Parameters:
N - - the size in bits of the new modulus

decrypt

public static java.math.BigInteger decrypt(java.math.BigInteger c)

encrypt

public static java.math.BigInteger encrypt(java.math.BigInteger m)

pad

public static java.math.BigInteger pad(java.math.BigInteger bi)