public void setKey(byte[] key, int keyOff, int keyLen) { hmac.init(new KeyParameter(key, keyOff, keyLen)); }
static HMac createHmacSha512Digest(byte[] key) { SHA512Digest digest = new SHA512Digest(); HMac hMac = new HMac(digest); hMac.init(new KeyParameter(key)); return hMac; }
static HMac createHmacSha512Digest(byte[] key) { SHA512Digest digest = new SHA512Digest(); HMac hMac = new HMac(digest); hMac.init(new KeyParameter(key)); return hMac; }
static HMac createHmacSha512Digest(byte[] key) { SHA512Digest digest = new SHA512Digest(); HMac hMac = new HMac(digest); hMac.init(new KeyParameter(key)); return hMac; }
static HMac createHmacSha512Digest(byte[] key) { SHA512Digest digest = new SHA512Digest(); HMac hMac = new HMac(digest); hMac.init(new KeyParameter(key)); return hMac; }
static HMac createHmacSha512Digest(byte[] key) { SHA512Digest digest = new SHA512Digest(); HMac hMac = new HMac(digest); hMac.init(new KeyParameter(key)); return hMac; }
/** * 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 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 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); } }
private static void hmac_hash(Digest digest, byte[] secret, byte[] seed, byte[] out) { HMac mac = new HMac(digest); KeyParameter param = 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.init(param); mac.update(a, 0, a.length); mac.doFinal(buf, 0); a = buf; mac.init(param); 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))); } }
public void engineStore(OutputStream stream, char[] password) throws IOException { DataOutputStream dOut = new DataOutputStream(stream); byte[] salt = new byte[STORE_SALT_SIZE]; int iterationCount = MIN_ITERATIONS + (random.nextInt() & 0x3ff); random.nextBytes(salt); dOut.writeInt(STORE_VERSION); dOut.writeInt(salt.length); dOut.write(salt); dOut.writeInt(iterationCount); HMac hMac = new HMac(new SHA1Digest()); MacOutputStream mOut = new MacOutputStream(hMac); PBEParametersGenerator pbeGen = new PKCS12ParametersGenerator(new SHA1Digest()); byte[] passKey = PBEParametersGenerator.PKCS12PasswordToBytes(password); pbeGen.init(passKey, salt, iterationCount); hMac.init(pbeGen.generateDerivedMacParameters(hMac.getMacSize() * 8)); for (int i = 0; i != passKey.length; i++) { passKey[i] = 0; } saveStore(new TeeOutputStream(dOut, mOut)); byte[] mac = new byte[hMac.getMacSize()]; hMac.doFinal(mac, 0); dOut.write(mac); dOut.close(); }
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; } }
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))); } }
/** * Create a {@link SimulatedTlsSRPIdentityManager} that implements the algorithm from RFC 5054 2.5.1.3 * * @param group the {@link SRP6GroupParameters} defining the group that SRP is operating in * @param seedKey the secret "seed key" referred to in RFC 5054 2.5.1.3 * @return an instance of {@link SimulatedTlsSRPIdentityManager} */ public static SimulatedTlsSRPIdentityManager getRFC5054Default(SRP6GroupParameters group, byte[] seedKey) { SRP6VerifierGenerator verifierGenerator = new SRP6VerifierGenerator(); verifierGenerator.init(group, TlsUtils.createHash(HashAlgorithm.sha1)); HMac mac = new HMac(TlsUtils.createHash(HashAlgorithm.sha1)); mac.init(new KeyParameter(seedKey)); return new SimulatedTlsSRPIdentityManager(group, verifierGenerator, mac); }
static MacCalculator createMacCalculator(final ASN1ObjectIdentifier digestAlgorithm, ExtendedDigest digest, final PKCS12PBEParams pbeParams, final char[] password) { PKCS12ParametersGenerator pGen = new PKCS12ParametersGenerator(digest); pGen.init(PKCS12ParametersGenerator.PKCS12PasswordToBytes(password), pbeParams.getIV(), pbeParams.getIterations().intValue()); final KeyParameter keyParam = (KeyParameter)pGen.generateDerivedMacParameters(digest.getDigestSize() * 8); final HMac hMac = new HMac(digest); hMac.init(keyParam); return new MacCalculator() { public AlgorithmIdentifier getAlgorithmIdentifier() { return new AlgorithmIdentifier(digestAlgorithm, pbeParams); } public OutputStream getOutputStream() { return new MacOutputStream(hMac); } public byte[] getMac() { byte[] res = new byte[hMac.getMacSize()]; hMac.doFinal(res, 0); return res; } public GenericKey getKey() { return new GenericKey(getAlgorithmIdentifier(), PKCS12ParametersGenerator.PKCS12PasswordToBytes(password)); } }; }
static MacCalculator createMacCalculator(final ASN1ObjectIdentifier digestAlgorithm, ExtendedDigest digest, final PKCS12PBEParams pbeParams, final char[] password) { PKCS12ParametersGenerator pGen = new PKCS12ParametersGenerator(digest); pGen.init(PKCS12ParametersGenerator.PKCS12PasswordToBytes(password), pbeParams.getIV(), pbeParams.getIterations().intValue()); final KeyParameter keyParam = (KeyParameter)pGen.generateDerivedMacParameters(digest.getDigestSize() * 8); final HMac hMac = new HMac(digest); hMac.init(keyParam); return new MacCalculator() { public AlgorithmIdentifier getAlgorithmIdentifier() { return new AlgorithmIdentifier(digestAlgorithm, pbeParams); } public OutputStream getOutputStream() { return new MacOutputStream(hMac); } public byte[] getMac() { byte[] res = new byte[hMac.getMacSize()]; hMac.doFinal(res, 0); return res; } public GenericKey getKey() { return new GenericKey(getAlgorithmIdentifier(), PKCS12ParametersGenerator.PKCS12PasswordToBytes(password)); } }; }
private HMac createMac(SecurityAlgorithm algorithm, KeyParameter param) throws ServiceResultException { HMac hmac = null; if (algorithm.equals(SecurityAlgorithm.HmacSha1)) { hmac = new HMac(new SHA1Digest()); } else if (algorithm.equals(SecurityAlgorithm.HmacSha256)) { hmac = new HMac(new SHA256Digest()); } else { throw new ServiceResultException( StatusCodes.Bad_SecurityPolicyRejected, "Unsupported symmetric signature algorithm: " + algorithm); } hmac.init(param); return hmac; }
mac.init(new KeyParameter(macKey));