tls

tls.git
git clone git://git.lenczewski.org/tls.git
Log | Files | Refs

test-hkdf.c (1664B)


      1 #include "hkdf.h"
      2 #include "hmac-sha2.h"
      3 
      4 #include <stdio.h>
      5 #include <string.h>
      6 
      7 #define HKDF_ROUNDS 2
      8 #define HKDF_DIGEST_SIZE (SHA256_DIGEST_SIZE * HKDF_ROUNDS)
      9 
     10 int
     11 main(void)
     12 {
     13 	char key[] = "test";
     14 	char salt[] = "salt";
     15 	char info[] = "info";
     16 
     17 	struct sha256_ctx sha256_ctx;
     18 	uint8_t dgst[HKDF_DIGEST_SIZE];
     19 	hkdf(&hmac_sha256, &sha256_ctx,
     20 	     key, strlen(key),
     21 	     salt, strlen(salt),
     22 	     info, strlen(info),
     23 	     dgst, HKDF_ROUNDS);
     24 
     25 
     26 	// generated with:
     27 	// --
     28 	//  openssl kdf
     29 	//    -keylen 64
     30 	//    -kdfopt digest:sha2-256
     31 	//    -kdfopt key:test
     32 	//    -kdfopt salt:salt
     33 	//    -kdfopt info:info
     34 	//    hkdf
     35 	//
     36 	uint8_t expected[HKDF_DIGEST_SIZE] = {
     37 		0x8a, 0x43, 0x59, 0x8a, 0xc9, 0x49, 0x78, 0x8d,
     38 		0xab, 0x1a, 0xc0, 0xae, 0x0e, 0xd4, 0x2f, 0x10,
     39 		0x0e, 0xcf, 0x6a, 0x98, 0x29, 0x2d, 0xd8, 0x89,
     40 		0x51, 0xf8, 0x32, 0xd5, 0xdd, 0xf7, 0x63, 0x49,
     41 
     42 		0xb0, 0x27, 0x5a, 0xfc, 0x2d, 0x0d, 0x8f, 0xe7,
     43 		0x0c, 0x34, 0x46, 0x66, 0xcb, 0x97, 0xb1, 0x3e,
     44 		0x85, 0x76, 0xa1, 0xc0, 0x13, 0x6c, 0x49, 0x84,
     45 		0xe6, 0xd3, 0xf6, 0xee, 0x62, 0x55, 0x4d, 0x37,
     46 	};
     47 
     48 	printf("key: \"%.*s\" (%lu bytes)\n",
     49 			sizeof key, key, strlen(key));
     50 	printf("salt: \"%.*s\" (%lu bytes)\n",
     51 			sizeof salt, salt, strlen(salt));
     52 	printf("info: \"%.*s\" (%lu bytes)\n",
     53 			sizeof info, info, strlen(info));
     54 
     55 	printf("dgst: ");
     56 	for (size_t i = 0; i < sizeof dgst; i++) {
     57 		printf("%.2x", dgst[i]);
     58 	}
     59 	printf("\n");
     60 
     61 	printf("exp.: ");
     62 	for (size_t i = 0; i < sizeof expected; i++) {
     63 		printf("%.2x", expected[i]);
     64 	}
     65 	printf("\n");
     66 
     67 	return memcmp(expected, dgst, HKDF_DIGEST_SIZE);
     68 }
     69 
     70 #include "hkdf.c"
     71 #include "hmac.c"
     72 #include "hmac-sha2.c"
     73 #include "sha.c"