bool TestHKDF(KeyDerivationFunction &kdf, const HKDF_TestTuple *testSet, unsigned int testSetSize) { bool pass = true; for (unsigned int i=0; i<testSetSize; i++) { const HKDF_TestTuple &tuple = testSet[i]; string secret, context, salt, derivedKey; StringSource(tuple.hexSecret, true, new HexDecoder(new StringSink(secret))); StringSource(tuple.hexSalt ? tuple.hexSalt : "", true, new HexDecoder(new StringSink(salt))); StringSource(tuple.hexContext ? tuple.hexContext : "", true, new HexDecoder(new StringSink(context))); StringSource(tuple.hexDerivedKey, true, new HexDecoder(new StringSink(derivedKey))); SecByteBlock derived(derivedKey.size()); unsigned int ret = kdf.DeriveKey(derived, derived.size(), reinterpret_cast<const unsigned char*>(secret.data()), secret.size(), reinterpret_cast<const unsigned char*>(salt.data()), salt.size(), reinterpret_cast<const unsigned char*>(context.data()), context.size()); pass = pass && (ret == tuple.len); bool fail = !VerifyBufsEqual(derived, reinterpret_cast<const unsigned char*>(derivedKey.data()), derived.size()); pass = pass && !fail; HexEncoder enc(new FileSink(cout)); cout << (fail ? "FAILED " : "passed "); cout << " " << tuple.hexSecret << " " << (tuple.hexSalt ? tuple.hexSalt : "<NO SALT>"); cout << " " << (tuple.hexContext ? tuple.hexContext : "<NO CTX>") << " "; enc.Put(derived, derived.size()); cout << endl; } return pass; }
bool TestHKDF(KeyDerivationFunction &kdf, const HKDF_TestTuple *testSet, unsigned int testSetSize) { bool pass = true; for (unsigned int i=0; i<testSetSize; i++) { const HKDF_TestTuple &tuple = testSet[i]; std::string secret, salt, info, expected; StringSource(tuple.hexSecret, true, new HexDecoder(new StringSink(secret))); StringSource(tuple.hexSalt ? tuple.hexSalt : "", true, new HexDecoder(new StringSink(salt))); StringSource(tuple.hexInfo ? tuple.hexInfo : "", true, new HexDecoder(new StringSink(info))); StringSource(tuple.hexExpected, true, new HexDecoder(new StringSink(expected))); SecByteBlock derived(expected.size()); unsigned int ret = kdf.DeriveKey(derived, derived.size(), reinterpret_cast<const unsigned char*>(secret.data()), secret.size(), (tuple.hexSalt ? reinterpret_cast<const unsigned char*>(salt.data()) : NULL), salt.size(), (tuple.hexInfo ? reinterpret_cast<const unsigned char*>(info.data()) : NULL), info.size()); bool fail = !VerifyBufsEqual(derived, reinterpret_cast<const unsigned char*>(expected.data()), derived.size()); pass = pass && (ret == tuple.len) && !fail; HexEncoder enc(new FileSink(std::cout)); std::cout << (fail ? "FAILED " : "passed "); std::cout << " " << tuple.hexSecret << " "; std::cout << (tuple.hexSalt ? (strlen(tuple.hexSalt) ? tuple.hexSalt : "<0-LEN SALT>") : "<NO SALT>"); std::cout << " "; std::cout << (tuple.hexInfo ? (strlen(tuple.hexInfo) ? tuple.hexInfo : "<0-LEN INFO>") : "<NO INFO>"); std::cout << " "; enc.Put(derived, derived.size()); std::cout << std::endl; } return pass; }