/** * Key Derivation Function 2 (KDF2) from the IEEE P1363a draft standard. It generates a secret key * of length oLen bits from shared secret Z and key derivation parameter P. */ public static int[] KDF2(int[] Z, int oLen, int[] P) { // Note: // oLen cannot be > hbits * (2^32-1) bits, because the size of an // int is 32 bits int[] K = new int[0]; int[] CB = new int[1]; int cThreshold = (oLen / 160); if (oLen % 160 != 0) cThreshold++; try { MessageDigest sha = MessageDigest.getInstance("SHA"); // sha.update(data); // byte[] hash = sha.digest(data); for (byte i = 1; i <= cThreshold; i++) { CB[0] = i; sha.update(Utils.toByteArray(Utils.concatenate(Z, CB, P))); K = Utils.concatenate(K, Utils.revIntArray(Utils.toIntArray(sha.digest()))); // K = Utils.concatenate(K, Utils.toIntArray(sha.digest())); sha.reset(); // not needed after diget() } } catch (NoSuchAlgorithmException e) { } // return Utils.resize (Utils.revIntArray(K), oLen); return Utils.resize(K, oLen); }
/** * MAC1 as described in the IEEE P1363 standard. It computes a HMAC message authentication code * tag from secret key KB and message M. */ public static int[] MAC1(int[] K, int[] M) { int[] HH = new int[0]; try { int i; int[] KK; MessageDigest sha = MessageDigest.getInstance("SHA"); // SHA1 Blocksize B = 512 if (K.length > (8 * 512)) { sha.update(Utils.toByteArray(K)); // kkLen = 20 octets, 160 bits KK = Utils.revIntArray(Utils.toIntArray(sha.digest())); } else KK = K; int[] P = new int[512 - KK.length]; for (i = 0; i < P.length; i++) { P[i] = 0; } int[] K0 = Utils.concatenate(KK, P); int[] iPad = new int[512]; for (i = 0; i < iPad.length; i++) { iPad[i] = 0x36; } int[] oPad = new int[512]; for (i = 0; i < oPad.length; i++) { oPad[i] = 0x54; } sha.reset(); sha.update(Utils.toByteArray(Utils.concatenate(Utils.xor(K0, iPad), M))); int[] H = Utils.revIntArray(Utils.toIntArray(sha.digest())); sha.reset(); sha.update(Utils.toByteArray(Utils.concatenate(Utils.xor(K0, oPad), H))); HH = Utils.revIntArray(Utils.toIntArray(sha.digest())); } catch (NoSuchAlgorithmException e) { } return HH; }