1 /* $OpenBSD: prftest.c,v 1.3 2021/12/13 16:56:49 deraadt Exp $ */ 2 /* $EOM: prftest.c,v 1.2 1998/10/07 16:40:50 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 <stdlib.h> 34 #include <stdio.h> 35 #include <string.h> 36 37 #include "hash.h" 38 #include "prf.h" 39 40 int test_prf (char *, enum hashes, char *, int, char *, int, char *); 41 42 #define nibble2c(x) ((x) >= 10 ? ('a'-10+(x)) : ('0' + (x))) 43 44 /* 45 * Basically the same as the HMAC regress, but to keep with modularity 46 * prf seems to be useful. So here we just check the HMAC test cases, 47 * until there are more PRFs. 48 */ 49 50 int 51 main (void) 52 { 53 char key[100]; 54 55 memset (key, 11, 20); 56 test_prf ("PRF MD5 Test Case 1", HASH_MD5, 57 key, 16, "Hi There", 8, "9294727a3638bb1c13f48ef8158bfc9d"); 58 test_prf ("PRF MD5 Test Case 2", HASH_MD5, 59 "Jefe", 4, 60 "what do ya want for nothing?", 28, 61 "750c783e6ab0b503eaa86e310a5db738"); 62 test_prf ("PRF SHA1 Test Case 1", HASH_SHA1, 63 key, 20, "Hi There", 8, 64 "b617318655057264e28bc0b6fb378c8ef146be00"); 65 test_prf ("PRF SHA1 Test Case 2", HASH_SHA1, 66 "Jefe", 4, "what do ya want for nothing?", 28, 67 "effcdf6ae5eb2fa2d27416d5f184df9c259a7c79"); 68 test_prf ("PRF SHA1 Test Case 3", HASH_SHA1, 69 "Bloody long key, this one, eben longer than the blocksize " 70 "of ordinary keyed HMAC functions", 90, 71 "what do ya want for nothing?", 28, 72 "52ca5fbcd7d4821bc6bf8b6e95e131109dff901b"); 73 74 return 0; 75 } 76 77 int 78 test_prf (char *test, enum hashes hash, char *key, int klen, 79 char *data, int dlen, char *cmp) 80 { 81 char output[2*HASH_MAX+1]; 82 char digest[HASH_MAX]; 83 struct prf *prf; 84 int i; 85 86 printf ("Testing %s: ", test); 87 88 prf = prf_alloc (PRF_HMAC, hash, key, klen); 89 if (!prf) 90 { 91 printf("prf_alloc () failed\n"); 92 return 0; 93 } 94 95 prf->Init (prf->prfctx); 96 prf->Update (prf->prfctx, data, dlen); 97 prf->Final (digest, prf->prfctx); 98 99 for (i = 0; i < prf->blocksize; i++) 100 { 101 output[2 * i] = nibble2c ((digest[i] >> 4) & 0xf); 102 output[2 * i + 1] = nibble2c (digest[i] & 0xf); 103 } 104 output[2 * i] = 0; 105 106 prf_free (prf); 107 108 if (strcmp (output, cmp) == 0) 109 { 110 printf ("OKAY\n"); 111 return 1; 112 } 113 114 printf ("%s <-> %s\n", output, cmp); 115 return 0; 116 } 117