static int pbkdf2_sha1(const char *pw, size_t pw_len, const uint8_t *salt, size_t salt_len, unsigned rounds, uint8_t *derived_key, size_t derived_key_len) { pbkdf2_hmac_sha1((unsigned)pw_len, (const uint8_t *)pw, rounds, salt_len, salt, derived_key_len, derived_key); return 0; }
void SecKeyFromPassphraseDataHMACSHA1(CFDataRef password, CFDataRef salt, uint32_t interationCount, CFMutableDataRef derivedKey) { pbkdf2_hmac_sha1(CFDataGetBytePtr(password), CFDataGetLength(password), CFDataGetBytePtr(salt), CFDataGetLength(salt), interationCount, CFDataGetMutableBytePtr(derivedKey), CFDataGetLength(derivedKey)); }
static int dcrypt_gnutls_pbkdf2(const unsigned char *password, size_t password_len, const unsigned char *salt, size_t salt_len, const char *algorithm, unsigned int rounds, buffer_t *result, unsigned int result_len, const char **error_r) { unsigned char buf[result_len]; /* only sha1 or sha256 is supported */ if (strncasecmp(algorithm, "sha1", 4) == 0) { pbkdf2_hmac_sha1(password_len, password, rounds, salt_len, salt, result_len, buf); } else if (strncasecmp(algorithm, "sha256", 6) == 0) { pbkdf2_hmac_sha256(password_len, password, rounds, salt_len, salt, result_len, buf); } else if (strncasecmp(algorithm, "sha512", 6) == 0) { struct hmac_sha512_ctx ctx; hmac_sha512_set_key(&ctx, password_len, password); PBKDF2(&ctx, hmac_sha512_update, hmac_sha512_digest, 64, rounds, salt_len, salt, result_len, buf); i_zero(&ctx); } else { *error_r = "Unsupported algorithm"; return -1; } buffer_append(result, buf, result_len); memset(buf, 0, sizeof(buf)); return 0; }
static void tests(void) { { const char *password = "******"; const char *salt = "salt"; const int iterations = 1; const uint8_t expected[20] = { 0x0c, 0x60, 0xc8, 0x0f, 0x96, 0x1f, 0x0e, 0x71, 0xf3, 0xa9, 0xb5, 0x24, 0xaf, 0x60, 0x12, 0x06, 0x2f, 0xe0, 0x37, 0xa6 }; const char resultSize = sizeof(expected); uint8_t actual[resultSize]; pbkdf2_hmac_sha1((const uint8_t*) password, strlen(password), (const uint8_t*) salt, strlen(salt), iterations, actual, resultSize); ok(memcmp(expected, actual, resultSize) == 0, "pbkdf-sha-1: P-'password' S-'Salt' I-1"); } { const char *password = "******"; const char *salt = "salt"; const int iterations = 2; const uint8_t expected[20] = { 0xea, 0x6c, 0x01, 0x4d, 0xc7, 0x2d, 0x6f, 0x8c, 0xcd, 0x1e, 0xd9, 0x2a, 0xce, 0x1d, 0x41, 0xf0, 0xd8, 0xde, 0x89, 0x57 }; const char resultSize = sizeof(expected); uint8_t actual[resultSize]; pbkdf2_hmac_sha1((const uint8_t*) password, strlen(password), (const uint8_t*) salt, strlen(salt), iterations, actual, resultSize); ok(memcmp(expected, actual, resultSize) == 0, "pbkdf-sha-1: P-'password' S-'Salt' I-2"); } { const char *password = "******"; const char *salt = "salt"; const int iterations = 4096; const uint8_t expected[20] = { 0x4b, 0x00, 0x79, 0x01, 0xb7, 0x65, 0x48, 0x9a, 0xbe, 0xad, 0x49, 0xd9, 0x26, 0xf7, 0x21, 0xd0, 0x65, 0xa4, 0x29, 0xc1 }; const char resultSize = sizeof(expected); uint8_t actual[resultSize]; pbkdf2_hmac_sha1((const uint8_t*) password, strlen(password), (const uint8_t*) salt, strlen(salt), iterations, actual, resultSize); ok(memcmp(expected, actual, resultSize) == 0, "pbkdf-sha-1: P-'password' S-'Salt' I-4096"); } SKIP: { skip("16777216 iterations is too slow", 1, 0); const char *password = "******"; const char *salt = "salt"; const int iterations = 16777216; const uint8_t expected[20] = { 0xee, 0xfe, 0x3d, 0x61, 0xcd, 0x4d, 0xa4, 0xe4, 0xe9, 0x94, 0x5b, 0x3d, 0x6b, 0xa2, 0x15, 0x8c, 0x26, 0x34, 0xe9, 0x84 }; const char resultSize = sizeof(expected); uint8_t actual[resultSize]; pbkdf2_hmac_sha1((const uint8_t*) password, strlen(password), (const uint8_t*) salt, strlen(salt), iterations, actual, resultSize); ok(memcmp(expected, actual, resultSize) == 0, "pbkdf-sha-1: P-'password' S-'Salt' I-16777216"); } { CFStringRef password = CFStringCreateWithCString(NULL, "password", kCFStringEncodingUTF8); CFStringRef salt = CFStringCreateWithCString(NULL, "salt", kCFStringEncodingUTF8); CFDataRef passwordData = CFStringCreateExternalRepresentation(NULL, password, kCFStringEncodingUTF8, 0); CFDataRef saltData = CFStringCreateExternalRepresentation(NULL, salt, kCFStringEncodingUTF8, 0); const int iterations = 1; const uint8_t expected[20] = { 0x0c, 0x60, 0xc8, 0x0f, 0x96, 0x1f, 0x0e, 0x71, 0xf3, 0xa9, 0xb5, 0x24, 0xaf, 0x60, 0x12, 0x06, 0x2f, 0xe0, 0x37, 0xa6 }; const char resultSize = sizeof(expected); CFMutableDataRef resultData = CFDataCreateMutable(NULL, resultSize); CFDataIncreaseLength(resultData, resultSize); SecKeyFromPassphraseDataHMACSHA1(passwordData, saltData, iterations, resultData); ok(memcmp(expected, CFDataGetBytePtr(resultData), resultSize) == 0, "pbkdf-sha-1: P-'password' S-'Salt' I-1"); CFReleaseSafe(password); CFReleaseSafe(salt); CFReleaseSafe(passwordData); CFReleaseSafe(saltData); CFReleaseSafe(resultData); } { CFStringRef password = CFStringCreateWithCString(NULL, "password", kCFStringEncodingUTF8); CFStringRef salt = CFStringCreateWithCString(NULL, "salt", kCFStringEncodingUTF8); CFDataRef passwordData = CFStringCreateExternalRepresentation(NULL, password, kCFStringEncodingUTF8, 0); CFDataRef saltData = CFStringCreateExternalRepresentation(NULL, salt, kCFStringEncodingUTF8, 0); const int iterations = 2; const uint8_t expected[20] = { 0xea, 0x6c, 0x01, 0x4d, 0xc7, 0x2d, 0x6f, 0x8c, 0xcd, 0x1e, 0xd9, 0x2a, 0xce, 0x1d, 0x41, 0xf0, 0xd8, 0xde, 0x89, 0x57 }; const char resultSize = sizeof(expected); CFMutableDataRef resultData = CFDataCreateMutable(NULL, resultSize); CFDataIncreaseLength(resultData, resultSize); SecKeyFromPassphraseDataHMACSHA1(passwordData, saltData, iterations, resultData); ok(memcmp(expected, CFDataGetBytePtr(resultData), resultSize) == 0, "pbkdf-sha-1: P-'password' S-'Salt' I-1"); CFReleaseSafe(password); CFReleaseSafe(salt); CFReleaseSafe(passwordData); CFReleaseSafe(saltData); CFReleaseSafe(resultData); } { CFStringRef password = CFStringCreateWithCString(NULL, "password", kCFStringEncodingUTF8); CFStringRef salt = CFStringCreateWithCString(NULL, "salt", kCFStringEncodingUTF8); CFDataRef passwordData = CFStringCreateExternalRepresentation(NULL, password, kCFStringEncodingUTF8, 0); CFDataRef saltData = CFStringCreateExternalRepresentation(NULL, salt, kCFStringEncodingUTF8, 0); const int iterations = 4096; const uint8_t expected[20] = { 0x4b, 0x00, 0x79, 0x01, 0xb7, 0x65, 0x48, 0x9a, 0xbe, 0xad, 0x49, 0xd9, 0x26, 0xf7, 0x21, 0xd0, 0x65, 0xa4, 0x29, 0xc1 }; const char resultSize = sizeof(expected); CFMutableDataRef resultData = CFDataCreateMutable(NULL, resultSize); CFDataIncreaseLength(resultData, resultSize); SecKeyFromPassphraseDataHMACSHA1(passwordData, saltData, iterations, resultData); ok(memcmp(expected, CFDataGetBytePtr(resultData), resultSize) == 0, "pbkdf-sha-1: P-'password' S-'Salt' I-1"); CFReleaseSafe(password); CFReleaseSafe(salt); CFReleaseSafe(passwordData); CFReleaseSafe(saltData); CFReleaseSafe(resultData); } SKIP: { skip("16777216 iterations is too slow", 1, 0); CFStringRef password = CFStringCreateWithCString(NULL, "password", kCFStringEncodingUTF8); CFStringRef salt = CFStringCreateWithCString(NULL, "salt", kCFStringEncodingUTF8); CFDataRef passwordData = CFStringCreateExternalRepresentation(NULL, password, kCFStringEncodingUTF8, 0); CFDataRef saltData = CFStringCreateExternalRepresentation(NULL, salt, kCFStringEncodingUTF8, 0); const int iterations = 16777216; const uint8_t expected[20] = { 0xee, 0xfe, 0x3d, 0x61, 0xcd, 0x4d, 0xa4, 0xe4, 0xe9, 0x94, 0x5b, 0x3d, 0x6b, 0xa2, 0x15, 0x8c, 0x26, 0x34, 0xe9, 0x84 }; const char resultSize = sizeof(expected); CFMutableDataRef resultData = CFDataCreateMutable(NULL, resultSize); CFDataIncreaseLength(resultData, resultSize); SecKeyFromPassphraseDataHMACSHA1(passwordData, saltData, iterations, resultData); ok(memcmp(expected, CFDataGetBytePtr(resultData), resultSize) == 0, "pbkdf-sha-1: P-'password' S-'Salt' I-1"); CFReleaseSafe(password); CFReleaseSafe(salt); CFReleaseSafe(passwordData); CFReleaseSafe(saltData); CFReleaseSafe(resultData); } }