pkcs5_pbkdf2_test.c (3681B)
1 #include "config.h" 2 3 #include <glib.h> 4 5 #include <common/pkcs5_pbkdf2.h> 6 7 struct td { 8 gchar *input; 9 size_t input_length; 10 gchar *salt; 11 size_t salt_length; 12 size_t length; 13 guint rounds; 14 gint expected_return; 15 gchar *expected_output; 16 }; 17 18 /* Test vectors from RFC 6070 */ 19 struct td td_rfc6070_1 = { "password", 8, 20 "salt", 4, 21 20, 1, 0, 22 "\x0c\x60\xc8\x0f\x96\x1f\x0e\x71\xf3\xa9\xb5\x24\xaf\x60\x12\x06\x2f\xe0\x37\xa6" }; 23 struct td td_rfc6070_2 = { "password", 8, 24 "salt", 4, 25 20, 2, 0, 26 "\xea\x6c\x01\x4d\xc7\x2d\x6f\x8c\xcd\x1e\xd9\x2a\xce\x1d\x41\xf0\xd8\xde\x89\x57" }; 27 struct td td_rfc6070_3 = { "password", 8, 28 "salt", 4, 29 20, 4096, 0, 30 "\x4b\x00\x79\x01\xb7\x65\x48\x9a\xbe\xad\x49\xd9\x26\xf7\x21\xd0\x65\xa4\x29\xc1" }; 31 struct td td_rfc6070_4 = { "password", 8, 32 "salt", 4, 33 20, 16777216, 0, 34 "\xee\xfe\x3d\x61\xcd\x4d\xa4\xe4\xe9\x94\x5b\x3d\x6b\xa2\x15\x8c\x26\x34\xe9\x84" }; 35 struct td td_rfc6070_5 = { "passwordPASSWORDpassword", 24, 36 "saltSALTsaltSALTsaltSALTsaltSALTsalt", 36, 37 25, 4096, 0, 38 "\x3d\x2e\xec\x4f\xe4\x1c\x84\x9b\x80\xc8\xd8\x36\x62\xc0\xe4\x4a\x8b\x29\x1a\x96\x4c\xf2\xf0\x70\x38" }; 39 struct td td_rfc6070_6 = { "pass\0word", 9, 40 "sa\0lt", 5, 41 16, 4096, 0, 42 "\x56\xfa\x6a\xa7\x55\x48\x09\x9d\xcc\x37\xd7\xf0\x34\x25\xe0\xc3" }; 43 44 struct td td_zero_rounds = { "abc", 3, "abc", 3, 3, 0, -1, "" }; 45 struct td td_zero_output_length = { "abc", 3, "abc", 3, 0, 1, -1, NULL }; 46 struct td td_null_input = { NULL, 10, "", 10, 10, 100, -1, "" }; 47 struct td td_null_salt = { "", 10, NULL, 10, 10, 100, -1, "" }; 48 49 static void 50 test_pkcs5_pbkdf2(gconstpointer user_data) 51 { 52 struct td *data = (struct td *)user_data; 53 guchar *kd = g_malloc0(data->length); 54 gint ret; 55 56 if (data->rounds > 10000 && !g_test_slow()) { 57 const gchar *msg = "Time-intensive test, rerun in slow mode to run it."; 58 g_test_skip(msg); 59 g_test_message("%s", msg); 60 return; 61 } 62 63 if (g_test_verbose()) { 64 g_printerr("input '%s' (%ld)\nsalt '%s' (%ld)\nlength %ld\nrounds %d\nexpected_return %d\n", 65 data->input, data->input_length, 66 data->salt, data->salt_length, 67 data->length, 68 data->rounds, 69 data->expected_return); 70 } 71 72 ret = pkcs5_pbkdf2( 73 data->input, 74 data->input_length, 75 data->salt, 76 data->salt_length, 77 kd, 78 data->length, 79 data->rounds); 80 81 g_assert_cmpint(ret, ==, data->expected_return); 82 g_assert_cmpstr(kd, ==, data->expected_output); 83 } 84 85 static void 86 test_pkcs5_pbkdf2_null_output_buffer(void) 87 { 88 gint ret = pkcs5_pbkdf2("abc", 3, "abc", 3, NULL, 10, 1); 89 90 g_assert_cmpint(ret, ==, -1); 91 } 92 93 int 94 main(int argc, char *argv[]) 95 { 96 g_test_init(&argc, &argv, NULL); 97 98 g_test_add_data_func("/common/pkcs5_pbkdf2/zero_rounds", 99 &td_zero_rounds, test_pkcs5_pbkdf2); 100 g_test_add_data_func("/common/pkcs5_pbkdf2/zero_output_length", 101 &td_zero_output_length, test_pkcs5_pbkdf2); 102 g_test_add_data_func("/common/pkcs5_pbkdf2/null_salt", 103 &td_null_salt, test_pkcs5_pbkdf2); 104 g_test_add_data_func("/common/pkcs5_pbkdf2/null_input", 105 &td_null_input, test_pkcs5_pbkdf2); 106 g_test_add_func("/common/pkcs5_pbkdf2/null_output_buffer", 107 test_pkcs5_pbkdf2_null_output_buffer); 108 109 g_test_add_data_func("/common/pkcs5_pbkdf2/rfc6070_1", 110 &td_rfc6070_1, test_pkcs5_pbkdf2); 111 g_test_add_data_func("/common/pkcs5_pbkdf2/rfc6070_2", 112 &td_rfc6070_2, test_pkcs5_pbkdf2); 113 g_test_add_data_func("/common/pkcs5_pbkdf2/rfc6070_3", 114 &td_rfc6070_3, test_pkcs5_pbkdf2); 115 g_test_add_data_func("/common/pkcs5_pbkdf2/rfc6070_4", 116 &td_rfc6070_4, test_pkcs5_pbkdf2); 117 g_test_add_data_func("/common/pkcs5_pbkdf2/rfc6070_5", 118 &td_rfc6070_5, test_pkcs5_pbkdf2); 119 g_test_add_data_func("/common/pkcs5_pbkdf2/rfc6070_6", 120 &td_rfc6070_6, test_pkcs5_pbkdf2); 121 122 return g_test_run(); 123 }