1 /* $OpenBSD: hmactest.c,v 1.2 2021/12/13 16:56:49 deraadt Exp $ */ 2 /* $EOM: hmactest.c,v 1.3 1998/08/09 19:16:24 niklas Exp $ */ 3 4 /* 5 * Copyright (c) 1998 Niels Provos. All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 */ 27 28 /* 29 * This code was written under funding by Ericsson Radio Systems. 30 */ 31 32 #include <sys/types.h> 33 #include <stdio.h> 34 #include <stdlib.h> 35 #include <string.h> 36 37 #include "hash.h" 38 39 int test_hmac(char *, struct hash *, char *, int, char *, int, char *); 40 41 #define nibble2c(x) ((x) >= 10 ? ('a'-10+(x)) : ('0' + (x))) 42 43 int 44 main (void) 45 { 46 char key[100]; 47 48 memset(key, 11, 20); 49 test_hmac ("HMAC-MD5 Test Case 1", hash_get (HASH_MD5), 50 key, 16, "Hi There", 8, "9294727a3638bb1c13f48ef8158bfc9d"); 51 test_hmac ("HMAC-MD5 Test Case 2", hash_get (HASH_MD5), 52 "Jefe", 4, 53 "what do ya want for nothing?", 28, 54 "750c783e6ab0b503eaa86e310a5db738"); 55 test_hmac ("HMAC-SHA1 Test Case 1", hash_get (HASH_SHA1), 56 key, 20, "Hi There", 8, 57 "b617318655057264e28bc0b6fb378c8ef146be00"); 58 test_hmac ("HMAC-SHA1 Test Case 2", hash_get (HASH_SHA1), 59 "Jefe", 4, "what do ya want for nothing?", 28, 60 "effcdf6ae5eb2fa2d27416d5f184df9c259a7c79"); 61 62 return 0; 63 } 64 65 int 66 test_hmac(char *test, struct hash *hash, char *key, int klen, 67 char *data, int dlen, char *cmp) 68 { 69 char output[2*HASH_MAX+1]; 70 int i; 71 72 printf("Testing %s: ", test); 73 74 hash->HMACInit(hash, key, klen); 75 hash->Update(hash->ctx, data, dlen); 76 hash->HMACFinal(hash->digest, hash); 77 78 for (i=0; i<hash->hashsize; i++) 79 { 80 output[2*i] = nibble2c((hash->digest[i] >> 4) & 0xf); 81 output[2*i+1] = nibble2c(hash->digest[i] & 0xf); 82 } 83 output[2*i] = 0; 84 85 if (!strcmp(output, cmp)) 86 { 87 printf("OKAY\n"); 88 return 1; 89 } 90 91 printf("%s <-> %s\n", output, cmp); 92 return 0; 93 } 94