xref: /openbsd/regress/sys/crypto/hmac/hmac_test.c (revision 17df1aa7)
1 /*-
2  * Copyright (c) 2008 Damien Bergamini <damien.bergamini@free.fr>
3  *
4  * Permission to use, copy, modify, and distribute this software for any
5  * purpose with or without fee is hereby granted, provided that the above
6  * copyright notice and this permission notice appear in all copies.
7  *
8  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15  */
16 
17 #include <stdio.h>
18 #include <crypto/md5.h>
19 #include <crypto/sha1.h>
20 #include <crypto/sha2.h>
21 #include <crypto/hmac.h>
22 #include <string.h>
23 
24 static void
25 print_hex(unsigned char *buf, int len)
26 {
27 	int i;
28 
29 	printf("digest = 0x");
30 	for (i = 0; i < len; i++)
31 		printf("%02x", buf[i]);
32 	printf("\n");
33 }
34 
35 int
36 main(void)
37 {
38 	HMAC_MD5_CTX md5;
39 	HMAC_SHA1_CTX sha1;
40 	HMAC_SHA256_CTX sha256;
41 	u_int8_t data[50], output[32];
42 
43 	HMAC_MD5_Init(&md5, "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b", 16);
44 	HMAC_MD5_Update(&md5, "Hi There", 8);
45 	HMAC_MD5_Final(output, &md5);
46 	print_hex(output, MD5_DIGEST_LENGTH);
47 
48 	HMAC_MD5_Init(&md5, "Jefe", 4);
49 	HMAC_MD5_Update(&md5, "what do ya want for nothing?", 28);
50 	HMAC_MD5_Final(output, &md5);
51 	print_hex(output, MD5_DIGEST_LENGTH);
52 
53 	HMAC_MD5_Init(&md5, "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA", 16);
54 	memset(data, 0xDD, sizeof data);
55 	HMAC_MD5_Update(&md5, data, sizeof data);
56 	HMAC_MD5_Final(output, &md5);
57 	print_hex(output, MD5_DIGEST_LENGTH);
58 
59 	HMAC_SHA1_Init(&sha1, "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b", 16);
60 	HMAC_SHA1_Update(&sha1, "Hi There", 8);
61 	HMAC_SHA1_Final(output, &sha1);
62 	print_hex(output, SHA1_DIGEST_LENGTH);
63 
64 	HMAC_SHA1_Init(&sha1, "Jefe", 4);
65 	HMAC_SHA1_Update(&sha1, "what do ya want for nothing?", 28);
66 	HMAC_SHA1_Final(output, &sha1);
67 	print_hex(output, SHA1_DIGEST_LENGTH);
68 
69 	HMAC_SHA1_Init(&sha1, "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA", 16);
70 	memset(data, 0xDD, sizeof data);
71 	HMAC_SHA1_Update(&sha1, data, sizeof data);
72 	HMAC_SHA1_Final(output, &sha1);
73 	print_hex(output, SHA1_DIGEST_LENGTH);
74 
75 	HMAC_SHA256_Init(&sha256, "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b", 16);
76 	HMAC_SHA256_Update(&sha256, "Hi There", 8);
77 	HMAC_SHA256_Final(output, &sha256);
78 	print_hex(output, SHA256_DIGEST_LENGTH);
79 
80 	HMAC_SHA256_Init(&sha256, "Jefe", 4);
81 	HMAC_SHA256_Update(&sha256, "what do ya want for nothing?", 28);
82 	HMAC_SHA256_Final(output, &sha256);
83 	print_hex(output, SHA256_DIGEST_LENGTH);
84 
85 	HMAC_SHA256_Init(&sha256, "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA", 16);
86 	memset(data, 0xDD, sizeof data);
87 	HMAC_SHA256_Update(&sha256, data, sizeof data);
88 	HMAC_SHA256_Final(output, &sha256);
89 	print_hex(output, SHA256_DIGEST_LENGTH);
90 
91 	return 0;
92 }
93