private static EthereumIESEngine makeIESEngine(boolean isEncrypt, ECPoint pub, BigInteger prv, byte[] IV) { AESEngine aesFastEngine = new AESEngine(); EthereumIESEngine iesEngine = new EthereumIESEngine( new ECDHBasicAgreement(), new ConcatKDFBytesGenerator(new SHA256Digest()), new HMac(new SHA256Digest()), new SHA256Digest(), new BufferedBlockCipher(new SICBlockCipher(aesFastEngine))); byte[] d = new byte[] {}; byte[] e = new byte[] {}; IESParameters p = new IESWithCipherParameters(d, e, KEY_SIZE, KEY_SIZE); ParametersWithIV parametersWithIV = new ParametersWithIV(p, IV); iesEngine.init(isEncrypt, new ECPrivateKeyParameters(prv, CURVE), new ECPublicKeyParameters(pub, CURVE), parametersWithIV); return iesEngine; }
/** * Base constructor. * * @param digest digest to build the HMAC on. */ public HMacDSAKCalculator(Digest digest) { this.hMac = new HMac(digest); this.V = new byte[hMac.getMacSize()]; this.K = new byte[hMac.getMacSize()]; }
public byte[] getMac() { byte[] res = new byte[hMac.getMacSize()]; hMac.doFinal(res, 0); return res; }
protected void hmacHash(Digest digest, byte[] secret, int secretOff, int secretLen, byte[] seed, byte[] output) { HMac mac = new HMac(digest); mac.init(new KeyParameter(secret, secretOff, secretLen)); byte[] a = seed; int macSize = mac.getMacSize(); byte[] b1 = new byte[macSize]; byte[] b2 = new byte[macSize]; int pos = 0; while (pos < output.length) { mac.update(a, 0, a.length); mac.doFinal(b1, 0); a = b1; mac.update(a, 0, a.length); mac.update(seed, 0, seed.length); mac.doFinal(b2, 0); System.arraycopy(b2, 0, output, pos, Math.min(macSize, output.length - pos)); pos += macSize; } }
public BigInteger nextK() { byte[] t = new byte[((n.bitLength() + 7) / 8)]; for (;;) { int tOff = 0; while (tOff < t.length) { hMac.update(V, 0, V.length); hMac.doFinal(V, 0); int len = Math.min(t.length - tOff, V.length); System.arraycopy(V, 0, t, tOff, len); tOff += len; } BigInteger k = bitsToInt(t); if (k.compareTo(ZERO) > 0 && k.compareTo(n) < 0) { return k; } hMac.update(V, 0, V.length); hMac.update((byte)0x00); hMac.doFinal(K, 0); hMac.init(new KeyParameter(K)); hMac.update(V, 0, V.length); hMac.doFinal(V, 0); } }
byte[] computedSignature = new byte[hmac.getMacSize()]; hmac.update(dataToVerify, inputOffset, verifyLen); hmac.doFinal(computedSignature, 0);
/** * Performs the expand part of the key derivation function, using currentT * as input and output buffer. * * @throws DataLengthException if the total number of bytes generated is larger than the one * specified by RFC 5869 (255 * HashLen) */ private void expandNext() throws DataLengthException { int n = generatedBytes / hashLen + 1; if (n >= 256) { throw new DataLengthException( "HKDF cannot generate more than 255 blocks of HashLen size"); } // special case for T(0): T(0) is empty, so no update if (generatedBytes != 0) { hMacHash.update(currentT, 0, hashLen); } hMacHash.update(info, 0, info.length); hMacHash.update((byte)n); hMacHash.doFinal(currentT, 0); }
public void setKey(byte[] key, int keyOff, int keyLen) { hmac.init(new KeyParameter(key, keyOff, keyLen)); }
public void update(byte[] input, int inOff, int length) { hmac.update(input, inOff, length); }
public int getMacLength() { return hmac.getMacSize(); }
public void reset() { hmac.reset(); } }
static void hmac_hash(Digest digest, byte[] secret, byte[] seed, byte[] out) { HMac mac = new HMac(digest); mac.init(new KeyParameter(secret)); byte[] a = seed; int size = digest.getDigestSize(); int iterations = (out.length + size - 1) / size; byte[] buf = new byte[mac.getMacSize()]; byte[] buf2 = new byte[mac.getMacSize()]; for (int i = 0; i < iterations; i++) { mac.update(a, 0, a.length); mac.doFinal(buf, 0); a = buf; mac.update(a, 0, a.length); mac.update(seed, 0, seed.length); mac.doFinal(buf2, 0); System.arraycopy(buf2, 0, out, (size * i), Math.min(size, out.length - (size * i))); } }
/** * Performs the extract part of the key derivation function. * * @param salt the salt to use * @param ikm the input keying material * @return the PRK as KeyParameter */ private KeyParameter extract(byte[] salt, byte[] ikm) { if (salt == null) { // TODO check if hashLen is indeed same as HMAC size hMacHash.init(new KeyParameter(new byte[hashLen])); } else { hMacHash.init(new KeyParameter(salt)); } hMacHash.update(ikm, 0, ikm.length); byte[] prk = new byte[hashLen]; hMacHash.doFinal(prk, 0); return new KeyParameter(prk); }
public byte[] calculateMAC() { byte[] rv = new byte[hmac.getMacSize()]; hmac.doFinal(rv, 0); return rv; }
/** {@inheritDoc} */ @Override public void signSymm(SecurityPolicy policy, byte[] key, byte[] input, int inputOffset, int verifyLen, byte[] output, int outputOffset) throws ServiceResultException { HMac hmac = createMac(policy.getSymmetricSignatureAlgorithm(), new KeyParameter(key)); hmac.update(input, inputOffset, verifyLen); hmac.doFinal(output, outputOffset); }
public void init(DerivationParameters param) { if (!(param instanceof HKDFParameters)) { throw new IllegalArgumentException( "HKDF parameters required for HKDFBytesGenerator"); } HKDFParameters params = (HKDFParameters)param; if (params.skipExtract()) { // use IKM directly as PRK hMacHash.init(new KeyParameter(params.getIKM())); } else { hMacHash.init(extract(params.getSalt(), params.getIKM())); } info = params.getInfo(); generatedBytes = 0; currentT = new byte[hashLen]; }
public int doFinal( byte[] out, int outOff) { byte[] tmp = new byte[digestSize]; digest.doFinal(tmp, 0); digest.update(outputPad, 0, outputPad.length); digest.update(tmp, 0, tmp.length); int len = digest.doFinal(out, outOff); reset(); return len; }