1#include <glib.h>
2#include <string.h>
3#include <stdlib.h>
4
5/* HMAC-MD5 test vectors as per RFC 2202 */
6
7/* Test 1 */
8guint8 key_md5_test1[] = {
9 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
10 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b };
11guint8 result_md5_test1[] = {
12 0x92, 0x94, 0x72, 0x7a, 0x36, 0x38, 0xbb, 0x1c, 0x13, 0xf4,
13 0x8e, 0xf8, 0x15, 0x8b, 0xfc, 0x9d };
14
15/* Test 2 */
16guint8 result_md5_test2[] = {
17 0x75, 0x0c, 0x78, 0x3e, 0x6a, 0xb0, 0xb5, 0x03, 0xea, 0xa8,
18 0x6e, 0x31, 0x0a, 0x5d, 0xb7, 0x38 };
19
20/* Test 3 */
21guint8 key_md5_test3[] = {
22 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
23 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa };
24guint8 data_md5_test3[] = {
25 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd,
26 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd,
27 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd,
28 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd,
29 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd };
30guint8 result_md5_test3[] = {
31 0x56, 0xbe, 0x34, 0x52, 0x1d, 0x14, 0x4c, 0x88, 0xdb, 0xb8,
32 0xc7, 0x33, 0xf0, 0xe8, 0xb3, 0xf6 };
33
34/* Test 4 */
35guint8 key_md5_test4[] = {
36 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a,
37 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14,
38 0x15, 0x16, 0x17, 0x18, 0x19 };
39guint8 data_md5_test4[] = {
40 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd,
41 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd,
42 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd,
43 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd,
44 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd };
45guint8 result_md5_test4[] = {
46 0x69, 0x7e, 0xaf, 0x0a, 0xca, 0x3a, 0x3a, 0xea, 0x3a, 0x75,
47 0x16, 0x47, 0x46, 0xff, 0xaa, 0x79 };
48
49/* Test 5 */
50guint8 key_md5_test5[] = {
51 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c,
52 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c};
53guint8 result_md5_test5[] = {
54 0x56, 0x46, 0x1e, 0xf2, 0x34, 0x2e, 0xdc, 0x00, 0xf9, 0xba,
55 0xb9, 0x95, 0x69, 0x0e, 0xfd, 0x4c };
56
57/* Test 6 */
58guint8 key_md5_test6[] = {
59 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
60 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
61 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
62 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
63 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
64 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
65 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
66 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa };
67guint8 result_md5_test6[] = {
68 0x6b, 0x1a, 0xb7, 0xfe, 0x4b, 0xd7, 0xbf, 0x8f, 0x0b, 0x62,
69 0xe6, 0xce, 0x61, 0xb9, 0xd0, 0xcd };
70
71/* Test 6 */
72guint8 key_md5_test7[] = {
73 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
74 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
75 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
76 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
77 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
78 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
79 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
80 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa };
81guint8 result_md5_test7[] = {
82 0x6f, 0x63, 0x0f, 0xad, 0x67, 0xcd, 0xa0, 0xee, 0x1f, 0xb1,
83 0xf5, 0x62, 0xdb, 0x3a, 0xa5, 0x3e };
84
85/* HMAC-SHA1, HMAC-SHA256, HMAC-SHA384 and HMAC-SHA512 test vectors
86 * as per RFCs 2202 and 4868.
87 *
88 * See: https://tools.ietf.org/html/rfc4868#section-2.7.1 */
89
90/* Test 1 */
91guint8 key_sha_test1[] = {
92 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
93 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b };
94guint8 result_sha1_test1[] = {
95 0xb6, 0x17, 0x31, 0x86, 0x55, 0x05, 0x72, 0x64, 0xe2, 0x8b,
96 0xc0, 0xb6, 0xfb, 0x37, 0x8c, 0x8e, 0xf1, 0x46, 0xbe, 0x00 };
97guint8 result_sha256_test1[] = {
98 0xb0, 0x34, 0x4c, 0x61, 0xd8, 0xdb, 0x38, 0x53, 0x5c, 0xa8,
99 0xaf, 0xce, 0xaf, 0x0b, 0xf1, 0x2b, 0x88, 0x1d, 0xc2, 0x00,
100 0xc9, 0x83, 0x3d, 0xa7, 0x26, 0xe9, 0x37, 0x6c, 0x2e, 0x32,
101 0xcf, 0xf7 };
102guint8 result_sha384_test1[] = {
103 0xaf, 0xd0, 0x39, 0x44, 0xd8, 0x48, 0x95, 0x62, 0x6b, 0x08,
104 0x25, 0xf4, 0xab, 0x46, 0x90, 0x7f, 0x15, 0xf9, 0xda, 0xdb,
105 0xe4, 0x10, 0x1e, 0xc6, 0x82, 0xaa, 0x03, 0x4c, 0x7c, 0xeb,
106 0xc5, 0x9c, 0xfa, 0xea, 0x9e, 0xa9, 0x07, 0x6e, 0xde, 0x7f,
107 0x4a, 0xf1, 0x52, 0xe8, 0xb2, 0xfa, 0x9c, 0xb6 };
108guint8 result_sha512_test1[] = {
109 0x87, 0xaa, 0x7c, 0xde, 0xa5, 0xef, 0x61, 0x9d, 0x4f, 0xf0,
110 0xb4, 0x24, 0x1a, 0x1d, 0x6c, 0xb0, 0x23, 0x79, 0xf4, 0xe2,
111 0xce, 0x4e, 0xc2, 0x78, 0x7a, 0xd0, 0xb3, 0x05, 0x45, 0xe1,
112 0x7c, 0xde, 0xda, 0xa8, 0x33, 0xb7, 0xd6, 0xb8, 0xa7, 0x02,
113 0x03, 0x8b, 0x27, 0x4e, 0xae, 0xa3, 0xf4, 0xe4, 0xbe, 0x9d,
114 0x91, 0x4e, 0xeb, 0x61, 0xf1, 0x70, 0x2e, 0x69, 0x6c, 0x20,
115 0x3a, 0x12, 0x68, 0x54 };
116
117/* Test 2 */
118guint8 result_sha1_test2[] = {
119 0xef, 0xfc, 0xdf, 0x6a, 0xe5, 0xeb, 0x2f, 0xa2, 0xd2, 0x74,
120 0x16, 0xd5, 0xf1, 0x84, 0xdf, 0x9c, 0x25, 0x9a, 0x7c, 0x79 };
121guint8 result_sha256_test2[] = {
122 0x5b, 0xdc, 0xc1, 0x46, 0xbf, 0x60, 0x75, 0x4e, 0x6a, 0x04,
123 0x24, 0x26, 0x08, 0x95, 0x75, 0xc7, 0x5a, 0x00, 0x3f, 0x08,
124 0x9d, 0x27, 0x39, 0x83, 0x9d, 0xec, 0x58, 0xb9, 0x64, 0xec,
125 0x38, 0x43 };
126guint8 result_sha384_test2[] = {
127 0xaf, 0x45, 0xd2, 0xe3, 0x76, 0x48, 0x40, 0x31, 0x61, 0x7f,
128 0x78, 0xd2, 0xb5, 0x8a, 0x6b, 0x1b, 0x9c, 0x7e, 0xf4, 0x64,
129 0xf5, 0xa0, 0x1b, 0x47, 0xe4, 0x2e, 0xc3, 0x73, 0x63, 0x22,
130 0x44, 0x5e, 0x8e, 0x22, 0x40, 0xca, 0x5e, 0x69, 0xe2, 0xc7,
131 0x8b, 0x32, 0x39, 0xec, 0xfa, 0xb2, 0x16, 0x49 };
132guint8 result_sha512_test2[] = {
133 0x16, 0x4b, 0x7a, 0x7b, 0xfc, 0xf8, 0x19, 0xe2, 0xe3, 0x95,
134 0xfb, 0xe7, 0x3b, 0x56, 0xe0, 0xa3, 0x87, 0xbd, 0x64, 0x22,
135 0x2e, 0x83, 0x1f, 0xd6, 0x10, 0x27, 0x0c, 0xd7, 0xea, 0x25,
136 0x05, 0x54, 0x97, 0x58, 0xbf, 0x75, 0xc0, 0x5a, 0x99, 0x4a,
137 0x6d, 0x03, 0x4f, 0x65, 0xf8, 0xf0, 0xe6, 0xfd, 0xca, 0xea,
138 0xb1, 0xa3, 0x4d, 0x4a, 0x6b, 0x4b, 0x63, 0x6e, 0x07, 0x0a,
139 0x38, 0xbc, 0xe7, 0x37 };
140
141/* Test 3 */
142guint8 key_sha_test3[] = {
143 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
144 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa };
145guint8 data_sha_test3[] = {
146 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd,
147 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd,
148 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd,
149 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd,
150 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd };
151guint8 result_sha1_test3[] = {
152 0x12, 0x5d, 0x73, 0x42, 0xb9, 0xac, 0x11, 0xcd, 0x91, 0xa3,
153 0x9a, 0xf4, 0x8a, 0xa1, 0x7b, 0x4f, 0x63, 0xf1, 0x75, 0xd3 };
154guint8 result_sha256_test3[] = {
155 0x77, 0x3e, 0xa9, 0x1e, 0x36, 0x80, 0x0e, 0x46, 0x85, 0x4d,
156 0xb8, 0xeb, 0xd0, 0x91, 0x81, 0xa7, 0x29, 0x59, 0x09, 0x8b,
157 0x3e, 0xf8, 0xc1, 0x22, 0xd9, 0x63, 0x55, 0x14, 0xce, 0xd5,
158 0x65, 0xfe };
159guint8 result_sha384_test3[] = {
160 0x88, 0x06, 0x26, 0x08, 0xd3, 0xe6, 0xad, 0x8a, 0x0a, 0xa2,
161 0xac, 0xe0, 0x14, 0xc8, 0xa8, 0x6f, 0x0a, 0xa6, 0x35, 0xd9,
162 0x47, 0xac, 0x9f, 0xeb, 0xe8, 0x3e, 0xf4, 0xe5, 0x59, 0x66,
163 0x14, 0x4b, 0x2a, 0x5a, 0xb3, 0x9d, 0xc1, 0x38, 0x14, 0xb9,
164 0x4e, 0x3a, 0xb6, 0xe1, 0x01, 0xa3, 0x4f, 0x27 };
165guint8 result_sha512_test3[] = {
166 0xfa, 0x73, 0xb0, 0x08, 0x9d, 0x56, 0xa2, 0x84, 0xef, 0xb0,
167 0xf0, 0x75, 0x6c, 0x89, 0x0b, 0xe9, 0xb1, 0xb5, 0xdb, 0xdd,
168 0x8e, 0xe8, 0x1a, 0x36, 0x55, 0xf8, 0x3e, 0x33, 0xb2, 0x27,
169 0x9d, 0x39, 0xbf, 0x3e, 0x84, 0x82, 0x79, 0xa7, 0x22, 0xc8,
170 0x06, 0xb4, 0x85, 0xa4, 0x7e, 0x67, 0xc8, 0x07, 0xb9, 0x46,
171 0xa3, 0x37, 0xbe, 0xe8, 0x94, 0x26, 0x74, 0x27, 0x88, 0x59,
172 0xe1, 0x32, 0x92, 0xfb };
173
174/* Test 4 */
175guint8 key_sha_test4[] = {
176 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a,
177 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14,
178 0x15, 0x16, 0x17, 0x18, 0x19 };
179guint8 data_sha_test4[] = {
180 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd,
181 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd,
182 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd,
183 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd,
184 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd };
185guint8 result_sha1_test4[] = {
186 0x4c, 0x90, 0x07, 0xf4, 0x02, 0x62, 0x50, 0xc6, 0xbc, 0x84,
187 0x14, 0xf9, 0xbf, 0x50, 0xc8, 0x6c, 0x2d, 0x72, 0x35, 0xda };
188guint8 result_sha256_test4[] = {
189 0x82, 0x55, 0x8a, 0x38, 0x9a, 0x44, 0x3c, 0x0e, 0xa4, 0xcc,
190 0x81, 0x98, 0x99, 0xf2, 0x08, 0x3a, 0x85, 0xf0, 0xfa, 0xa3,
191 0xe5, 0x78, 0xf8, 0x07, 0x7a, 0x2e, 0x3f, 0xf4, 0x67, 0x29,
192 0x66, 0x5b };
193guint8 result_sha384_test4[] = {
194 0x3e, 0x8a, 0x69, 0xb7, 0x78, 0x3c, 0x25, 0x85, 0x19, 0x33,
195 0xab, 0x62, 0x90, 0xaf, 0x6c, 0xa7, 0x7a, 0x99, 0x81, 0x48,
196 0x08, 0x50, 0x00, 0x9c, 0xc5, 0x57, 0x7c, 0x6e, 0x1f, 0x57,
197 0x3b, 0x4e, 0x68, 0x01, 0xdd, 0x23, 0xc4, 0xa7, 0xd6, 0x79,
198 0xcc, 0xf8, 0xa3, 0x86, 0xc6, 0x74, 0xcf, 0xfb };
199guint8 result_sha512_test4[] = {
200 0xb0, 0xba, 0x46, 0x56, 0x37, 0x45, 0x8c, 0x69, 0x90, 0xe5,
201 0xa8, 0xc5, 0xf6, 0x1d, 0x4a, 0xf7, 0xe5, 0x76, 0xd9, 0x7f,
202 0xf9, 0x4b, 0x87, 0x2d, 0xe7, 0x6f, 0x80, 0x50, 0x36, 0x1e,
203 0xe3, 0xdb, 0xa9, 0x1c, 0xa5, 0xc1, 0x1a, 0xa2, 0x5e, 0xb4,
204 0xd6, 0x79, 0x27, 0x5c, 0xc5, 0x78, 0x80, 0x63, 0xa5, 0xf1,
205 0x97, 0x41, 0x12, 0x0c, 0x4f, 0x2d, 0xe2, 0xad, 0xeb, 0xeb,
206 0x10, 0xa2, 0x98, 0xdd };
207
208/* Test 5 (note: different for SHA-256/SHA-512) */
209guint8 key_sha1_test5[] = {
210 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c,
211 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c };
212guint8 result_sha1_test5[] = {
213 0x4c, 0x1a, 0x03, 0x42, 0x4b, 0x55, 0xe0, 0x7f, 0xe7, 0xf2,
214 0x7b, 0xe1, 0xd5, 0x8b, 0xb9, 0x32, 0x4a, 0x9a, 0x5a, 0x04 };
215
216/* Test 6 & 7 (note: different for SHA-1 and SHA-256/SHA-512) */
217guint8 key_sha1_test6_7[] = {
218 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
219 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
220 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
221 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
222 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
223 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
224 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
225 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa };
226
227guint8 result_sha1_test6[] = {
228 0xaa, 0x4a, 0xe5, 0xe1, 0x52, 0x72, 0xd0, 0x0e, 0x95, 0x70,
229 0x56, 0x37, 0xce, 0x8a, 0x3b, 0x55, 0xed, 0x40, 0x21, 0x12 };
230
231guint8 result_sha1_test7[] = {
232 0xe8, 0xe9, 0x9d, 0xf, 0x45, 0x23, 0x7d, 0x78, 0x6d, 0x6b,
233 0xba, 0xa7, 0x96, 0x5c, 0x78, 0x8, 0xbb, 0xff, 0x1a, 0x91 };
234
235/* Test 5 & 6 for SHA-256 and SHA-512. */
236guint8 key_sha256_test5_6[] = {
237 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
238 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
239 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
240 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
241 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
242 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
243 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
244 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
245 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
246 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
247 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
248 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
249 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
250 0xaa };
251
252guint8 result_sha256_test5[] = {
253 0x60, 0xe4, 0x31, 0x59, 0x1e, 0xe0, 0xb6, 0x7f, 0x0d, 0x8a,
254 0x26, 0xaa, 0xcb, 0xf5, 0xb7, 0x7f, 0x8e, 0x0b, 0xc6, 0x21,
255 0x37, 0x28, 0xc5, 0x14, 0x05, 0x46, 0x04, 0x0f, 0x0e, 0xe3,
256 0x7f, 0x54 };
257guint8 result_sha384_test5[] = {
258 0x4e, 0xce, 0x08, 0x44, 0x85, 0x81, 0x3e, 0x90, 0x88, 0xd2,
259 0xc6, 0x3a, 0x04, 0x1b, 0xc5, 0xb4, 0x4f, 0x9e, 0xf1, 0x01,
260 0x2a, 0x2b, 0x58, 0x8f, 0x3c, 0xd1, 0x1f, 0x05, 0x03, 0x3a,
261 0xc4, 0xc6, 0x0c, 0x2e, 0xf6, 0xab, 0x40, 0x30, 0xfe, 0x82,
262 0x96, 0x24, 0x8d, 0xf1, 0x63, 0xf4, 0x49, 0x52 };
263guint8 result_sha512_test5[] = {
264 0x80, 0xb2, 0x42, 0x63, 0xc7, 0xc1, 0xa3, 0xeb, 0xb7, 0x14,
265 0x93, 0xc1, 0xdd, 0x7b, 0xe8, 0xb4, 0x9b, 0x46, 0xd1, 0xf4,
266 0x1b, 0x4a, 0xee, 0xc1, 0x12, 0x1b, 0x01, 0x37, 0x83, 0xf8,
267 0xf3, 0x52, 0x6b, 0x56, 0xd0, 0x37, 0xe0, 0x5f, 0x25, 0x98,
268 0xbd, 0x0f, 0xd2, 0x21, 0x5d, 0x6a, 0x1e, 0x52, 0x95, 0xe6,
269 0x4f, 0x73, 0xf6, 0x3f, 0x0a, 0xec, 0x8b, 0x91, 0x5a, 0x98,
270 0x5d, 0x78, 0x65, 0x98 };
271
272guint8 result_sha256_test6[] = {
273 0x9b, 0x09, 0xff, 0xa7, 0x1b, 0x94, 0x2f, 0xcb, 0x27, 0x63,
274 0x5f, 0xbc, 0xd5, 0xb0, 0xe9, 0x44, 0xbf, 0xdc, 0x63, 0x64,
275 0x4f, 0x07, 0x13, 0x93, 0x8a, 0x7f, 0x51, 0x53, 0x5c, 0x3a,
276 0x35, 0xe2 };
277guint8 result_sha384_test6[] = {
278 0x66, 0x17, 0x17, 0x8e, 0x94, 0x1f, 0x02, 0x0d, 0x35, 0x1e,
279 0x2f, 0x25, 0x4e, 0x8f, 0xd3, 0x2c, 0x60, 0x24, 0x20, 0xfe,
280 0xb0, 0xb8, 0xfb, 0x9a, 0xdc, 0xce, 0xbb, 0x82, 0x46, 0x1e,
281 0x99, 0xc5, 0xa6, 0x78, 0xcc, 0x31, 0xe7, 0x99, 0x17, 0x6d,
282 0x38, 0x60, 0xe6, 0x11, 0x0c, 0x46, 0x52, 0x3e };
283guint8 result_sha512_test6[] = {
284 0xe3, 0x7b, 0x6a, 0x77, 0x5d, 0xc8, 0x7d, 0xba, 0xa4, 0xdf,
285 0xa9, 0xf9, 0x6e, 0x5e, 0x3f, 0xfd, 0xde, 0xbd, 0x71, 0xf8,
286 0x86, 0x72, 0x89, 0x86, 0x5d, 0xf5, 0xa3, 0x2d, 0x20, 0xcd,
287 0xc9, 0x44, 0xb6, 0x02, 0x2c, 0xac, 0x3c, 0x49, 0x82, 0xb1,
288 0x0d, 0x5e, 0xeb, 0x55, 0xc3, 0xe4, 0xde, 0x15, 0x13, 0x46,
289 0x76, 0xfb, 0x6d, 0xe0, 0x44, 0x60, 0x65, 0xc9, 0x74, 0x40,
290 0xfa, 0x8c, 0x6a, 0x58 };
291
292
293typedef struct {
294 GChecksumType digest_type;
295 gconstpointer key;
296 gsize key_len;
297 gconstpointer data;
298 gsize data_len;
299 gconstpointer result;
300} HmacCase;
301
302HmacCase hmac_md5_tests[] = {
303 { G_CHECKSUM_MD5, key_md5_test1, 16, "Hi There", 8, result_md5_test1 },
304 { G_CHECKSUM_MD5, "Jefe", 4, "what do ya want for nothing?", 28,
305 result_md5_test2 },
306 { G_CHECKSUM_MD5, key_md5_test3, 16, data_md5_test3, 50,
307 result_md5_test3 },
308 { G_CHECKSUM_MD5, key_md5_test4, 25, data_md5_test4, 50,
309 result_md5_test4 },
310 { G_CHECKSUM_MD5, key_md5_test5, 16, "Test With Truncation", 20,
311 result_md5_test5 },
312 { G_CHECKSUM_MD5, key_md5_test6, 80,
313 "Test Using Larger Than Block-Size Key - Hash Key First", 54,
314 result_md5_test6 },
315 { G_CHECKSUM_MD5, key_md5_test7, 80,
316 "Test Using Larger Than Block-Size Key and Larger Than One Block-Size Data",
317 73, result_md5_test7 },
318 { -1, NULL, 0, NULL, 0, NULL },
319};
320
321HmacCase hmac_sha1_tests[] = {
322 { G_CHECKSUM_SHA1, key_sha_test1, 20, "Hi There", 8, result_sha1_test1 },
323 { G_CHECKSUM_SHA1, "Jefe", 4, "what do ya want for nothing?", 28,
324 result_sha1_test2 },
325 { G_CHECKSUM_SHA1, key_sha_test3, 20, data_sha_test3, 50,
326 result_sha1_test3 },
327 { G_CHECKSUM_SHA1, key_sha_test4, 25, data_sha_test4, 50,
328 result_sha1_test4 },
329 { G_CHECKSUM_SHA1, key_sha1_test5, 20, "Test With Truncation", 20,
330 result_sha1_test5 },
331 { G_CHECKSUM_SHA1, key_sha1_test6_7, 80,
332 "Test Using Larger Than Block-Size Key - Hash Key First", 54,
333 result_sha1_test6 },
334 { G_CHECKSUM_SHA1, key_sha1_test6_7, 80,
335 "Test Using Larger Than Block-Size Key and Larger" \
336 " Than One Block-Size Data", 73, result_sha1_test7, },
337 { -1, NULL, 0, NULL, 0, NULL },
338};
339
340HmacCase hmac_sha256_tests[] = {
341 { G_CHECKSUM_SHA256, key_sha_test1, 20, "Hi There", 8, result_sha256_test1 },
342 { G_CHECKSUM_SHA256, "Jefe", 4, "what do ya want for nothing?", 28,
343 result_sha256_test2 },
344 { G_CHECKSUM_SHA256, key_sha_test3, 20, data_sha_test3, 50,
345 result_sha256_test3 },
346 { G_CHECKSUM_SHA256, key_sha_test4, 25, data_sha_test4, 50,
347 result_sha256_test4 },
348 { G_CHECKSUM_SHA256, key_sha256_test5_6, 131,
349 "Test Using Larger Than Block-Size Key - Hash Key First", 54,
350 result_sha256_test5 },
351 { G_CHECKSUM_SHA256, key_sha256_test5_6, 131,
352 "This is a test using a larger than block-size key and a larger than "
353 "block-size data. The key needs to be hashed before being used by the "
354 "HMAC algorithm.", 152, result_sha256_test6, },
355 { -1, NULL, 0, NULL, 0, NULL },
356};
357
358HmacCase hmac_sha384_tests[] = {
359 { G_CHECKSUM_SHA384, key_sha_test1, 20, "Hi There", 8, result_sha384_test1 },
360 { G_CHECKSUM_SHA384, "Jefe", 4, "what do ya want for nothing?", 28,
361 result_sha384_test2 },
362 { G_CHECKSUM_SHA384, key_sha_test3, 20, data_sha_test3, 50,
363 result_sha384_test3 },
364 { G_CHECKSUM_SHA384, key_sha_test4, 25, data_sha_test4, 50,
365 result_sha384_test4 },
366 { G_CHECKSUM_SHA384, key_sha256_test5_6, 131,
367 "Test Using Larger Than Block-Size Key - Hash Key First", 54,
368 result_sha384_test5 },
369 { G_CHECKSUM_SHA384, key_sha256_test5_6, 131,
370 "This is a test using a larger than block-size key and a larger than "
371 "block-size data. The key needs to be hashed before being used by the "
372 "HMAC algorithm.", 152, result_sha384_test6, },
373 { -1, NULL, 0, NULL, 0, NULL },
374};
375
376HmacCase hmac_sha512_tests[] = {
377 { G_CHECKSUM_SHA512, key_sha_test1, 20, "Hi There", 8, result_sha512_test1 },
378 { G_CHECKSUM_SHA512, "Jefe", 4, "what do ya want for nothing?", 28,
379 result_sha512_test2 },
380 { G_CHECKSUM_SHA512, key_sha_test3, 20, data_sha_test3, 50,
381 result_sha512_test3 },
382 { G_CHECKSUM_SHA512, key_sha_test4, 25, data_sha_test4, 50,
383 result_sha512_test4 },
384 { G_CHECKSUM_SHA512, key_sha256_test5_6, 131,
385 "Test Using Larger Than Block-Size Key - Hash Key First", 54,
386 result_sha512_test5 },
387 { G_CHECKSUM_SHA512, key_sha256_test5_6, 131,
388 "This is a test using a larger than block-size key and a larger than "
389 "block-size data. The key needs to be hashed before being used by the "
390 "HMAC algorithm.", 152, result_sha512_test6, },
391 { -1, NULL, 0, NULL, 0, NULL },
392};
393
394
395static void
396test_hmac (HmacCase *t)
397{
398 GHmac *hmac;
399 gsize digest_len, hmac_len;
400 gpointer digest;
401
402 hmac_len = digest_len = g_checksum_type_get_length (checksum_type: t->digest_type);
403 digest = g_malloc (n_bytes: hmac_len);
404
405 hmac = g_hmac_new (digest_type: t->digest_type, key: t->key, key_len: t->key_len);
406 g_hmac_update (hmac, data: t->data, length: t->data_len);
407 g_hmac_get_digest (hmac, buffer: digest, digest_len: &digest_len);
408
409 g_assert_cmpmem (digest, hmac_len, t->result, digest_len);
410
411 g_free (mem: digest);
412 g_hmac_unref (hmac);
413}
414
415static void
416test_hmac_ref_unref (void)
417{
418 GHmac *hmac, *check;
419
420 hmac = g_hmac_new (digest_type: G_CHECKSUM_SHA1, key: (guchar*)"aaa", key_len: 3);
421 check = g_hmac_ref (hmac);
422 g_assert (check == hmac);
423 g_hmac_unref (hmac: check);
424 g_hmac_unref (hmac);
425}
426
427static void
428test_hmac_copy (void)
429{
430 GHmac *hmac, *check;
431
432 hmac = g_hmac_new (digest_type: G_CHECKSUM_SHA256, key: (guchar*)"aaa", key_len: 3);
433 check = g_hmac_copy (hmac);
434 g_assert (check != hmac);
435 g_assert_cmpstr (g_hmac_get_string (hmac), ==, g_hmac_get_string (check));
436 g_hmac_unref (hmac: check);
437 g_hmac_unref (hmac);
438}
439
440static void
441test_hmac_for_data (void)
442{
443 gchar *string;
444 GHmac *hmac;
445
446 string = g_compute_hmac_for_data (digest_type: G_CHECKSUM_SHA1,
447 key: (guchar*)"aaa", key_len: 3,
448 data: (guchar*)"bcdef", length: 5);
449
450 hmac = g_hmac_new (digest_type: G_CHECKSUM_SHA1, key: (guchar*)"aaa", key_len: 3);
451 g_hmac_update (hmac, data: (guchar*)"bcdef", length: 5);
452 g_assert_cmpstr (string, ==, g_hmac_get_string (hmac));
453 g_hmac_unref (hmac);
454 g_free (mem: string);
455}
456
457static void
458test_hmac_for_string (void)
459{
460 gchar *string;
461 GHmac *hmac;
462
463 string = g_compute_hmac_for_string (digest_type: G_CHECKSUM_SHA1,
464 key: (guchar*)"aaa", key_len: 3,
465 str: "bcdef", length: -1);
466
467 hmac = g_hmac_new (digest_type: G_CHECKSUM_SHA1, key: (guchar*)"aaa", key_len: 3);
468 g_hmac_update (hmac, data: (guchar*)"bcdef", length: 5);
469 g_assert_cmpstr (string, ==, g_hmac_get_string (hmac));
470 g_hmac_unref (hmac);
471 g_free (mem: string);
472}
473
474static void
475test_hmac_for_bytes (void)
476{
477 gchar *string;
478 GHmac *hmac;
479 GBytes *key, *data;
480
481 key = g_bytes_new (data: "aaa", size: 3);
482 data = g_bytes_new (data: "bcdef", size: 5);
483
484 string = g_compute_hmac_for_bytes (digest_type: G_CHECKSUM_SHA1, key, data);
485
486 hmac = g_hmac_new (digest_type: G_CHECKSUM_SHA1, key: (guchar*)"aaa", key_len: 3);
487 g_hmac_update (hmac, data: (guchar*)"bcdef", length: 5);
488 g_assert_cmpstr (string, ==, g_hmac_get_string (hmac));
489 g_hmac_unref (hmac);
490 g_free (mem: string);
491
492 g_bytes_unref (bytes: key);
493 g_bytes_unref (bytes: data);
494}
495
496int
497main (int argc,
498 char **argv)
499{
500 int i;
501 g_test_init (argc: &argc, argv: &argv, NULL);
502
503 for (i = 0 ; hmac_sha1_tests[i].key_len > 0 ; i++)
504 {
505 gchar *name = g_strdup_printf (format: "/hmac/sha1-%d", i + 1);
506 g_test_add_data_func (testpath: name, test_data: hmac_sha1_tests + i,
507 test_func: (void (*)(const void *)) test_hmac);
508 g_free (mem: name);
509 }
510
511 for (i = 0 ; hmac_sha256_tests[i].key_len > 0 ; i++)
512 {
513 gchar *name = g_strdup_printf (format: "/hmac/sha256-%d", i + 1);
514 g_test_add_data_func (testpath: name, test_data: hmac_sha256_tests + i,
515 test_func: (void (*)(const void *)) test_hmac);
516 g_free (mem: name);
517 }
518
519 for (i = 0 ; hmac_sha384_tests[i].key_len > 0 ; i++)
520 {
521 gchar *name = g_strdup_printf (format: "/hmac/sha384-%d", i + 1);
522 g_test_add_data_func (testpath: name, test_data: hmac_sha384_tests + i,
523 test_func: (void (*)(const void *)) test_hmac);
524 g_free (mem: name);
525 }
526
527 for (i = 0 ; hmac_sha512_tests[i].key_len > 0 ; i++)
528 {
529 gchar *name = g_strdup_printf (format: "/hmac/sha512-%d", i + 1);
530 g_test_add_data_func (testpath: name, test_data: hmac_sha512_tests + i,
531 test_func: (void (*)(const void *)) test_hmac);
532 g_free (mem: name);
533 }
534
535 for (i = 0 ; hmac_md5_tests[i].key_len > 0 ; i++)
536 {
537 gchar *name = g_strdup_printf (format: "/hmac/md5-%d", i + 1);
538 g_test_add_data_func (testpath: name, test_data: hmac_md5_tests + i,
539 test_func: (void (*)(const void *)) test_hmac);
540 g_free (mem: name);
541 }
542
543 g_test_add_func (testpath: "/hmac/ref-unref", test_func: test_hmac_ref_unref);
544 g_test_add_func (testpath: "/hmac/copy", test_func: test_hmac_copy);
545 g_test_add_func (testpath: "/hmac/for-data", test_func: test_hmac_for_data);
546 g_test_add_func (testpath: "/hmac/for-string", test_func: test_hmac_for_string);
547 g_test_add_func (testpath: "/hmac/for-bytes", test_func: test_hmac_for_bytes);
548
549 return g_test_run ();
550}
551

source code of gtk/subprojects/glib/glib/tests/hmac.c