test-hmac.c (1037B)
1 #include "hmac-sha2.h" 2 3 #include <stdio.h> 4 #include <string.h> 5 6 int 7 main(void) 8 { 9 char msg[] = "Hi There"; 10 11 unsigned char key[] = { 12 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 13 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 14 }; 15 16 struct sha256_ctx sha256_ctx; 17 uint8_t dgst[SHA256_DIGEST_SIZE]; 18 hmac(&hmac_sha256, &sha256_ctx, key, sizeof key, msg, strlen(msg), dgst); 19 20 uint8_t expected[SHA256_DIGEST_SIZE] = { 21 0x49, 0x2c, 0xe0, 0x20, 0xfe, 0x25, 0x34, 0xa5, 22 0x78, 0x9d, 0xc3, 0x84, 0x88, 0x06, 0xc7, 0x8f, 23 0x4f, 0x67, 0x11, 0x39, 0x7f, 0x08, 0xe7, 0xe7, 24 0xa1, 0x2c, 0xa5, 0xa4, 0x48, 0x3c, 0x8a, 0xa6, 25 }; 26 27 printf("msg: \"%.*s\" (%lu bytes)\n", sizeof msg, msg, strlen(msg)); 28 29 printf("dgst: "); 30 for (size_t i = 0; i < sizeof dgst; i++) { 31 printf("%.2x", dgst[i]); 32 } 33 printf("\n"); 34 35 printf("exp.: "); 36 for (size_t i = 0; i < sizeof expected; i++) { 37 printf("%.2x", expected[i]); 38 } 39 printf("\n"); 40 41 return memcmp(expected, dgst, SHA224_DIGEST_SIZE); 42 } 43 44 #include "hmac.c" 45 #include "hmac-sha2.c" 46 #include "sha.c"