1 /* 2 * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved. 3 * 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 8 */ 9 10 #include <stdio.h> 11 #include <string.h> 12 #include <stdlib.h> 13 14 #include "../e_os.h" 15 16 # include <openssl/hmac.h> 17 # include <openssl/sha.h> 18 # ifndef OPENSSL_NO_MD5 19 # include <openssl/md5.h> 20 # endif 21 22 # ifdef CHARSET_EBCDIC 23 # include <openssl/ebcdic.h> 24 # endif 25 26 #define UC(a) ((const unsigned char *)(a)) 27 28 # ifndef OPENSSL_NO_MD5 29 static struct test_st { 30 const char key[16]; 31 int key_len; 32 const char data[64]; 33 int data_len; 34 const char *digest; 35 } test[8] = { 36 { 37 "", 0, "More text test vectors to stuff up EBCDIC machines :-)", 54, 38 "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 "9294727a3638bb1c13f48ef8158bfc9d", 46 }, 47 { 48 "Jefe", 4, "what do ya want for nothing?", 28, 49 "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, "56be34521d144c88dbb8c733f0e8b3f6", 62 }, 63 { 64 "", 0, "My test data", 12, 65 "61afdecb95429ef494d61fdee15990cabf0826fc" 66 }, 67 { 68 "", 0, "My test data", 12, 69 "2274b195d90ce8e03406f4b526a47e0787a88a65479938f1a5baa3ce0f079776" 70 }, 71 { 72 "123456", 6, "My test data", 12, 73 "bab53058ae861a7f191abe2d0145cbb123776a6369ee3f9d79ce455667e411dd" 74 }, 75 { 76 "12345", 5, "My test data again", 18, 77 "a12396ceddd2a85f4c656bc1e0aa50c78cffde3e" 78 } 79 }; 80 # endif 81 82 static char *pt(unsigned char *md, unsigned int len); 83 84 int main(int argc, char *argv[]) 85 { 86 # ifndef OPENSSL_NO_MD5 87 int i; 88 char *p; 89 # endif 90 int err = 0; 91 HMAC_CTX *ctx = NULL, *ctx2 = NULL; 92 unsigned char buf[EVP_MAX_MD_SIZE]; 93 unsigned int len; 94 95 # ifdef OPENSSL_NO_MD5 96 printf("test skipped: MD5 disabled\n"); 97 # else 98 99 # ifdef CHARSET_EBCDIC 100 ebcdic2ascii(test[0].data, test[0].data, test[0].data_len); 101 ebcdic2ascii(test[1].data, test[1].data, test[1].data_len); 102 ebcdic2ascii(test[2].key, test[2].key, test[2].key_len); 103 ebcdic2ascii(test[2].data, test[2].data, test[2].data_len); 104 # endif 105 106 for (i = 0; i < 4; i++) { 107 p = pt(HMAC(EVP_md5(), 108 test[i].key, test[i].key_len, 109 UC(test[i].data), test[i].data_len, NULL, NULL), 110 MD5_DIGEST_LENGTH); 111 112 if (strcmp(p, (const char *)test[i].digest) != 0) { 113 printf("Error calculating HMAC on %d entry'\n", i); 114 printf("got %s instead of %s\n", p, test[i].digest); 115 err++; 116 } else 117 printf("test %d ok\n", i); 118 } 119 # endif /* OPENSSL_NO_MD5 */ 120 121 /* test4 */ 122 ctx = HMAC_CTX_new(); 123 if (ctx == NULL) { 124 printf("HMAC malloc failure (test 4)\n"); 125 err++; 126 goto end; 127 } 128 if (HMAC_CTX_get_md(ctx) != NULL) { 129 printf("Message digest not NULL for HMAC (test 4)\n"); 130 err++; 131 goto test5; 132 } 133 if (HMAC_Init_ex(ctx, NULL, 0, NULL, NULL)) { 134 printf("Should fail to initialise HMAC with empty MD and key (test 4)\n"); 135 err++; 136 goto test5; 137 } 138 if (HMAC_Update(ctx, UC(test[4].data), test[4].data_len)) { 139 printf("Should fail HMAC_Update with ctx not set up (test 4)\n"); 140 err++; 141 goto test5; 142 } 143 if (HMAC_Init_ex(ctx, NULL, 0, EVP_sha1(), NULL)) { 144 printf("Should fail to initialise HMAC with empty key (test 4)\n"); 145 err++; 146 goto test5; 147 } 148 if (HMAC_Update(ctx, UC(test[4].data), test[4].data_len)) { 149 printf("Should fail HMAC_Update with ctx not set up (test 4)\n"); 150 err++; 151 goto test5; 152 } 153 printf("test 4 ok\n"); 154 test5: 155 /* Test 5 has empty key; test that single-shot accepts a NULL key. */ 156 p = pt(HMAC(EVP_sha1(), NULL, 0, UC(test[4].data), test[4].data_len, 157 NULL, NULL), SHA_DIGEST_LENGTH); 158 if (strcmp(p, (const char *)test[4].digest) != 0) { 159 printf("Error calculating HMAC on %d entry'\n", i); 160 printf("got %s instead of %s\n", p, test[4].digest); 161 err++; 162 } 163 164 HMAC_CTX_reset(ctx); 165 if (HMAC_CTX_get_md(ctx) != NULL) { 166 printf("Message digest not NULL for HMAC (test 5)\n"); 167 err++; 168 goto test6; 169 } 170 if (HMAC_Init_ex(ctx, test[4].key, test[4].key_len, NULL, NULL)) { 171 printf("Should fail to initialise HMAC with empty MD (test 5)\n"); 172 err++; 173 goto test6; 174 } 175 if (HMAC_Update(ctx, UC(test[4].data), test[4].data_len)) { 176 printf("Should fail HMAC_Update with ctx not set up (test 5)\n"); 177 err++; 178 goto test6; 179 } 180 if (HMAC_Init_ex(ctx, test[4].key, -1, EVP_sha1(), NULL)) { 181 printf("Should fail to initialise HMAC with invalid key len(test 5)\n"); 182 err++; 183 goto test6; 184 } 185 if (!HMAC_Init_ex(ctx, test[4].key, test[4].key_len, EVP_sha1(), NULL)) { 186 printf("Failed to initialise HMAC (test 5)\n"); 187 err++; 188 goto test6; 189 } 190 if (!HMAC_Update(ctx, UC(test[4].data), test[4].data_len)) { 191 printf("Error updating HMAC with data (test 5)\n"); 192 err++; 193 goto test6; 194 } 195 if (!HMAC_Final(ctx, buf, &len)) { 196 printf("Error finalising data (test 5)\n"); 197 err++; 198 goto test6; 199 } 200 p = pt(buf, len); 201 if (strcmp(p, (const char *)test[4].digest) != 0) { 202 printf("Error calculating interim HMAC on test 5\n"); 203 printf("got %s instead of %s\n", p, test[4].digest); 204 err++; 205 goto test6; 206 } 207 if (HMAC_Init_ex(ctx, NULL, 0, EVP_sha256(), NULL)) { 208 printf("Should disallow changing MD without a new key (test 5)\n"); 209 err++; 210 goto test6; 211 } 212 if (!HMAC_Init_ex(ctx, test[5].key, test[5].key_len, EVP_sha256(), NULL)) { 213 printf("Failed to reinitialise HMAC (test 5)\n"); 214 err++; 215 goto test6; 216 } 217 if (HMAC_CTX_get_md(ctx) != EVP_sha256()) { 218 printf("Unexpected message digest for HMAC (test 5)\n"); 219 err++; 220 goto test6; 221 } 222 if (!HMAC_Update(ctx, UC(test[5].data), test[5].data_len)) { 223 printf("Error updating HMAC with data (sha256) (test 5)\n"); 224 err++; 225 goto test6; 226 } 227 if (!HMAC_Final(ctx, buf, &len)) { 228 printf("Error finalising data (sha256) (test 5)\n"); 229 err++; 230 goto test6; 231 } 232 p = pt(buf, len); 233 if (strcmp(p, (const char *)test[5].digest) != 0) { 234 printf("Error calculating 2nd interim HMAC on test 5\n"); 235 printf("got %s instead of %s\n", p, test[5].digest); 236 err++; 237 goto test6; 238 } 239 if (!HMAC_Init_ex(ctx, test[6].key, test[6].key_len, NULL, NULL)) { 240 printf("Failed to reinitialise HMAC with key (test 5)\n"); 241 err++; 242 goto test6; 243 } 244 if (!HMAC_Update(ctx, UC(test[6].data), test[6].data_len)) { 245 printf("Error updating HMAC with data (new key) (test 5)\n"); 246 err++; 247 goto test6; 248 } 249 if (!HMAC_Final(ctx, buf, &len)) { 250 printf("Error finalising data (new key) (test 5)\n"); 251 err++; 252 goto test6; 253 } 254 p = pt(buf, len); 255 if (strcmp(p, (const char *)test[6].digest) != 0) { 256 printf("error calculating HMAC on test 5\n"); 257 printf("got %s instead of %s\n", p, test[6].digest); 258 err++; 259 } else { 260 printf("test 5 ok\n"); 261 } 262 test6: 263 HMAC_CTX_reset(ctx); 264 ctx2 = HMAC_CTX_new(); 265 if (ctx2 == NULL) { 266 printf("HMAC malloc failure (test 6)\n"); 267 err++; 268 goto end; 269 } 270 if (!HMAC_Init_ex(ctx, test[7].key, test[7].key_len, EVP_sha1(), NULL)) { 271 printf("Failed to initialise HMAC (test 6)\n"); 272 err++; 273 goto end; 274 } 275 if (!HMAC_Update(ctx, UC(test[7].data), test[7].data_len)) { 276 printf("Error updating HMAC with data (test 6)\n"); 277 err++; 278 goto end; 279 } 280 if (!HMAC_CTX_copy(ctx2, ctx)) { 281 printf("Failed to copy HMAC_CTX (test 6)\n"); 282 err++; 283 goto end; 284 } 285 if (!HMAC_Final(ctx2, buf, &len)) { 286 printf("Error finalising data (test 6)\n"); 287 err++; 288 goto end; 289 } 290 p = pt(buf, len); 291 if (strcmp(p, (const char *)test[7].digest) != 0) { 292 printf("Error calculating HMAC on test 6\n"); 293 printf("got %s instead of %s\n", p, test[7].digest); 294 err++; 295 } else { 296 printf("test 6 ok\n"); 297 } 298 end: 299 HMAC_CTX_free(ctx2); 300 HMAC_CTX_free(ctx); 301 EXIT(err); 302 } 303 304 # ifndef OPENSSL_NO_MD5 305 static char *pt(unsigned char *md, unsigned int len) 306 { 307 unsigned int i; 308 static char buf[80]; 309 310 for (i = 0; i < len; i++) 311 sprintf(&(buf[i * 2]), "%02x", md[i]); 312 return (buf); 313 } 314 # endif 315