• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *  Copyright 2014-2022 The GmSSL Project. All Rights Reserved.
3  *
4  *  Licensed under the Apache License, Version 2.0 (the License); you may
5  *  not use this file except in compliance with the License.
6  *
7  *  http://www.apache.org/licenses/LICENSE-2.0
8  */
9 
10 
11 
12 #include <stdio.h>
13 #include <string.h>
14 #include <stdlib.h>
15 #include <gmssl/hex.h>
16 #include <gmssl/pbkdf2.h>
17 #include <gmssl/error.h>
18 
19 
20 struct {
21 	char *pass;
22 	char *salt;
23 	int iter;
24 	int dklen;
25 	char *dk;
26 } pbkdf2_hmac_sha1_tests[] = {
27 
28 	// rfc 6070 test vectors for pbkdf2-hmac-sha1
29 	{
30 		"password",
31 		"salt",
32 		1,
33 		20,
34 		"0c60c80f961f0e71f3a9b524af6012062fe037a6",
35 	},
36 	{
37 		"password",
38 		"salt",
39 		2,
40 		20,
41 		"ea6c014dc72d6f8ccd1ed92ace1d41f0d8de8957",
42 	},
43 	{
44 		"password",
45 		"salt",
46 		4096,
47 		20,
48 		"4b007901b765489abead49d926f721d065a429c1",
49 	},
50 	/*
51 	{
52 		"password",
53 		"salt",
54 		16777216, // very slow
55 		20,
56 		"eefe3d61cd4da4e4e9945b3d6ba2158c2634e984",
57 	},
58 	*/
59 	{
60 		"passwordPASSWORDpassword",
61 		"saltSALTsaltSALTsaltSALTsaltSALTsalt",
62 		4096,
63 		25,
64 		"3d2eec4fe41c849b80c8d83662c0e44a8b291a964cf2f07038",
65 	},
66 };
67 
68 /*
69 void test(void)
70 {
71 	HMAC_CTX ctx;
72 	uint8_t iter[4] = {0, 0, 0, 1};
73 	uint8_t mac[20];
74 	size_t len;
75 	int i;
76 
77 	hmac_init(&ctx, DIGEST_sha1(), (uint8_t *)"password", 8);
78 	hmac_update(&ctx, (uint8_t *)"salt", 4);
79 	hmac_update(&ctx, iter, 4);
80 	hmac_finish(&ctx, mac, &len);
81 
82 	for (i = 1; i < 4096; i++) {
83 		uint8_t buf[20];
84 		memset(&ctx, 0, sizeof(HMAC_CTX));
85 		hmac_init(&ctx, DIGEST_sha1(), (uint8_t *)"password", 8);
86 		hmac_update(&ctx, mac, len);
87 		hmac_finish(&ctx, buf, &len);
88 		int j;
89 		for (j = 0; j < len; j++) {
90 			mac[j] ^= buf[j];
91 		}
92 	}
93 
94 
95 	for (i = 0; i < len; i++) {
96 		printf("%02x", mac[i]);
97 	}
98 	printf("\n");
99 }
100 */
101 
test_pbkdf2_genkey(void)102 static int test_pbkdf2_genkey(void)
103 {
104 	int i;
105 	uint8_t key[64];
106 	uint8_t buf[64];
107 	size_t len;
108 
109 	for (i = 0; i < sizeof(pbkdf2_hmac_sha1_tests)/sizeof(pbkdf2_hmac_sha1_tests[0]); i++) {
110 		hex_to_bytes(pbkdf2_hmac_sha1_tests[i].dk, strlen(pbkdf2_hmac_sha1_tests[i].dk), buf, &len);
111 
112 		if (pbkdf2_genkey(DIGEST_sha1(),
113 			pbkdf2_hmac_sha1_tests[i].pass, strlen(pbkdf2_hmac_sha1_tests[i].pass),
114 			(uint8_t *)pbkdf2_hmac_sha1_tests[i].salt, strlen(pbkdf2_hmac_sha1_tests[i].salt),
115 			pbkdf2_hmac_sha1_tests[i].iter, pbkdf2_hmac_sha1_tests[i].dklen, key) != 1) {
116 			error_print();
117 			return -1;
118 		}
119 		if (memcmp(key, buf, pbkdf2_hmac_sha1_tests[i].dklen) != 0) {
120 			fprintf(stderr, "test_pbkdf2_genkey test %d failed\n", i);
121 			return -1;
122 		} else {
123 			fprintf(stderr, "test_pbkdf2_genkey test %d ok\n", i);
124 		}
125 	}
126 
127 	printf("%s() ok\n", __FUNCTION__);
128 	return 0;
129 }
130 
main(int argc,char ** argv)131 int main(int argc, char **argv)
132 {
133 	int err = 0;
134 	err += test_pbkdf2_genkey();
135 	return err;
136 }
137