]> git.ipfire.org Git - thirdparty/openssl.git/blame - test/hmactest.c
Fix possible leaks on sk_X509_EXTENSION_push() failure ...
[thirdparty/openssl.git] / test / hmactest.c
CommitLineData
440e5d80 1/*
c2500f65 2 * Copyright 1995-2017 The OpenSSL Project Authors. All Rights Reserved.
58964a49 3 *
440e5d80
RS
4 * Licensed under the OpenSSL license (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
58964a49
RE
8 */
9
10#include <stdio.h>
11#include <string.h>
12#include <stdlib.h>
f5d7a031 13
176db6dc 14#include "internal/nelem.h"
55f78baf 15
0f113f3e 16# include <openssl/hmac.h>
b1413d9b 17# include <openssl/sha.h>
0f113f3e
MC
18# ifndef OPENSSL_NO_MD5
19# include <openssl/md5.h>
20# endif
58964a49 21
0f113f3e
MC
22# ifdef CHARSET_EBCDIC
23# include <openssl/ebcdic.h>
24# endif
a53955d8 25
623d1056
RS
26#include "testutil.h"
27
0f113f3e
MC
28# ifndef OPENSSL_NO_MD5
29static struct test_st {
30 unsigned char key[16];
31 int key_len;
32 unsigned char data[64];
33 int data_len;
34 unsigned char *digest;
2cfbdd71 35} test[8] = {
0f113f3e
MC
36 {
37 "", 0, "More text test vectors to stuff up EBCDIC machines :-)", 54,
38 (unsigned char *)"e9139d1e6ee064ef8cf514fc7dc83e86",
39 },
40 {
41 {
42 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
43 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
44 }, 16, "Hi There", 8,
45 (unsigned char *)"9294727a3638bb1c13f48ef8158bfc9d",
46 },
47 {
48 "Jefe", 4, "what do ya want for nothing?", 28,
49 (unsigned char *)"750c783e6ab0b503eaa86e310a5db738",
50 },
51 {
52 {
53 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
54 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
55 }, 16, {
56 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd,
57 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd,
58 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd,
59 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd,
60 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd
61 }, 50, (unsigned char *)"56be34521d144c88dbb8c733f0e8b3f6",
62 },
2cfbdd71
MC
63 {
64 "", 0, "My test data", 12,
65 (unsigned char *)"61afdecb95429ef494d61fdee15990cabf0826fc"
66 },
67 {
68 "", 0, "My test data", 12,
69 (unsigned char *)"2274b195d90ce8e03406f4b526a47e0787a88a65479938f1a5baa3ce0f079776"
70 },
71 {
72 "123456", 6, "My test data", 12,
73 (unsigned char *)"bab53058ae861a7f191abe2d0145cbb123776a6369ee3f9d79ce455667e411dd"
74 },
75 {
8a3c000c
KC
76 "12345", 5, "My test data again", 18,
77 (unsigned char *)"a12396ceddd2a85f4c656bc1e0aa50c78cffde3e"
2cfbdd71 78 }
0f113f3e
MC
79};
80# endif
58964a49 81
2cfbdd71
MC
82static char *pt(unsigned char *md, unsigned int len);
83
623d1056 84
0f113f3e 85# ifndef OPENSSL_NO_MD5
623d1056
RS
86static int test_hmac_md5(int idx)
87{
0f113f3e 88 char *p;
0f113f3e
MC
89# ifdef CHARSET_EBCDIC
90 ebcdic2ascii(test[0].data, test[0].data, test[0].data_len);
91 ebcdic2ascii(test[1].data, test[1].data, test[1].data_len);
92 ebcdic2ascii(test[2].key, test[2].key, test[2].key_len);
93 ebcdic2ascii(test[2].data, test[2].data, test[2].data_len);
94# endif
a53955d8 95
623d1056
RS
96 p = pt(HMAC(EVP_md5(),
97 test[idx].key, test[idx].key_len,
98 test[idx].data, test[idx].data_len, NULL, NULL),
99 MD5_DIGEST_LENGTH);
100
101 if (!TEST_str_eq(p, (char *)test[idx].digest))
102 return 0;
103
104 return 1;
105}
106# endif
107
31a80694 108static int test_hmac_bad(void)
623d1056
RS
109{
110 HMAC_CTX *ctx = NULL;
111 int ret = 0;
2cfbdd71 112
bf7c6817 113 ctx = HMAC_CTX_new();
623d1056
RS
114 if (!TEST_ptr(ctx)
115 || !TEST_ptr_null(HMAC_CTX_get_md(ctx))
116 || !TEST_false(HMAC_Init_ex(ctx, NULL, 0, NULL, NULL))
117 || !TEST_false(HMAC_Update(ctx, test[4].data, test[4].data_len))
118 || !TEST_false(HMAC_Init_ex(ctx, NULL, 0, EVP_sha1(), NULL))
119 || !TEST_false(HMAC_Update(ctx, test[4].data, test[4].data_len)))
120 goto err;
121
122 ret = 1;
123err:
124 HMAC_CTX_free(ctx);
125 return ret;
126}
127
31a80694 128static int test_hmac_run(void)
623d1056
RS
129{
130 char *p;
131 HMAC_CTX *ctx = NULL;
132 unsigned char buf[EVP_MAX_MD_SIZE];
133 unsigned int len;
134 int ret = 0;
b1413d9b 135
623d1056 136 ctx = HMAC_CTX_new();
a87a0a6e 137 HMAC_CTX_reset(ctx);
623d1056
RS
138
139 if (!TEST_ptr(ctx)
140 || !TEST_ptr_null(HMAC_CTX_get_md(ctx))
141 || !TEST_false(HMAC_Init_ex(ctx, NULL, 0, NULL, NULL))
142 || !TEST_false(HMAC_Update(ctx, test[4].data, test[4].data_len))
143 || !TEST_false(HMAC_Init_ex(ctx, test[4].key, -1, EVP_sha1(), NULL)))
144 goto err;
145
146 if (!TEST_true(HMAC_Init_ex(ctx, test[4].key, test[4].key_len, EVP_sha1(), NULL))
147 || !TEST_true(HMAC_Update(ctx, test[4].data, test[4].data_len))
148 || !TEST_true(HMAC_Final(ctx, buf, &len)))
149 goto err;
150
2cfbdd71 151 p = pt(buf, len);
623d1056
RS
152 if (!TEST_str_eq(p, (char *)test[4].digest))
153 goto err;
154
155 if (!TEST_false(HMAC_Init_ex(ctx, NULL, 0, EVP_sha256(), NULL)))
156 goto err;
157
158 if (!TEST_true(HMAC_Init_ex(ctx, test[5].key, test[5].key_len, EVP_sha256(), NULL))
159 || !TEST_ptr_eq(HMAC_CTX_get_md(ctx), EVP_sha256())
160 || !TEST_true(HMAC_Update(ctx, test[5].data, test[5].data_len))
161 || !TEST_true(HMAC_Final(ctx, buf, &len)))
162 goto err;
163
2cfbdd71 164 p = pt(buf, len);
623d1056
RS
165 if (!TEST_str_eq(p, (char *)test[5].digest))
166 goto err;
167
168 if (!TEST_true(HMAC_Init_ex(ctx, test[6].key, test[6].key_len, NULL, NULL))
169 || !TEST_true(HMAC_Update(ctx, test[6].data, test[6].data_len))
170 || !TEST_true(HMAC_Final(ctx, buf, &len)))
171 goto err;
2cfbdd71 172 p = pt(buf, len);
623d1056
RS
173 if (!TEST_str_eq(p, (char *)test[6].digest))
174 goto err;
175
176 ret = 1;
177err:
178 HMAC_CTX_free(ctx);
179 return ret;
180}
181
182
31a80694 183static int test_hmac_single_shot(void)
623d1056
RS
184{
185 char *p;
186
187 /* Test single-shot with an empty key. */
188 p = pt(HMAC(EVP_sha1(), NULL, 0, test[4].data, test[4].data_len,
189 NULL, NULL), SHA_DIGEST_LENGTH);
190 if (!TEST_str_eq(p, (char *)test[4].digest))
191 return 0;
192
193 return 1;
194}
195
196
31a80694 197static int test_hmac_copy(void)
623d1056
RS
198{
199 char *p;
200 HMAC_CTX *ctx = NULL, *ctx2 = NULL;
201 unsigned char buf[EVP_MAX_MD_SIZE];
202 unsigned int len;
203 int ret = 0;
204
205 ctx = HMAC_CTX_new();
bf7c6817 206 ctx2 = HMAC_CTX_new();
623d1056
RS
207 if (!TEST_ptr(ctx) || !TEST_ptr(ctx2))
208 goto err;
209
210 if (!TEST_true(HMAC_Init_ex(ctx, test[7].key, test[7].key_len, EVP_sha1(), NULL))
211 || !TEST_true(HMAC_Update(ctx, test[7].data, test[7].data_len))
212 || !TEST_true(HMAC_CTX_copy(ctx2, ctx))
213 || !TEST_true(HMAC_Final(ctx2, buf, &len)))
214 goto err;
215
2cfbdd71 216 p = pt(buf, len);
623d1056
RS
217 if (!TEST_str_eq(p, (char *)test[7].digest))
218 goto err;
219
220 ret = 1;
221err:
bf7c6817
RL
222 HMAC_CTX_free(ctx2);
223 HMAC_CTX_free(ctx);
623d1056 224 return ret;
0f113f3e 225}
58964a49 226
0f113f3e 227# ifndef OPENSSL_NO_MD5
2cfbdd71 228static char *pt(unsigned char *md, unsigned int len)
0f113f3e 229{
2cfbdd71 230 unsigned int i;
0f113f3e 231 static char buf[80];
58964a49 232
2cfbdd71 233 for (i = 0; i < len; i++)
0f113f3e 234 sprintf(&(buf[i * 2]), "%02x", md[i]);
c2500f65 235 return buf;
0f113f3e
MC
236}
237# endif
623d1056 238
ad887416 239int setup_tests(void)
623d1056
RS
240{
241 ADD_ALL_TESTS(test_hmac_md5, 4);
242 ADD_TEST(test_hmac_single_shot);
243 ADD_TEST(test_hmac_bad);
244 ADD_TEST(test_hmac_run);
245 ADD_TEST(test_hmac_copy);
ad887416 246 return 1;
623d1056
RS
247}
248