xref: /openbsd-src/regress/sbin/isakmpd/prf/prftest.c (revision 49a6e16f2c2c8e509184b1f777366d1a6f337e1c)
1*49a6e16fSderaadt /*	$OpenBSD: prftest.c,v 1.3 2021/12/13 16:56:49 deraadt Exp $	*/
271ec8d3aScloder /*	$EOM: prftest.c,v 1.2 1998/10/07 16:40:50 niklas Exp $	*/
371ec8d3aScloder 
471ec8d3aScloder /*
571ec8d3aScloder  * Copyright (c) 1998 Niels Provos.  All rights reserved.
671ec8d3aScloder  *
771ec8d3aScloder  * Redistribution and use in source and binary forms, with or without
871ec8d3aScloder  * modification, are permitted provided that the following conditions
971ec8d3aScloder  * are met:
1071ec8d3aScloder  * 1. Redistributions of source code must retain the above copyright
1171ec8d3aScloder  *    notice, this list of conditions and the following disclaimer.
1271ec8d3aScloder  * 2. Redistributions in binary form must reproduce the above copyright
1371ec8d3aScloder  *    notice, this list of conditions and the following disclaimer in the
1471ec8d3aScloder  *    documentation and/or other materials provided with the distribution.
1571ec8d3aScloder  *
1671ec8d3aScloder  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
1771ec8d3aScloder  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
1871ec8d3aScloder  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
1971ec8d3aScloder  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
2071ec8d3aScloder  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
2171ec8d3aScloder  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2271ec8d3aScloder  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2371ec8d3aScloder  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2471ec8d3aScloder  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
2571ec8d3aScloder  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2671ec8d3aScloder  */
2771ec8d3aScloder 
2871ec8d3aScloder /*
2971ec8d3aScloder  * This code was written under funding by Ericsson Radio Systems.
3071ec8d3aScloder  */
3171ec8d3aScloder 
32*49a6e16fSderaadt #include <sys/types.h>
3371ec8d3aScloder #include <stdlib.h>
3471ec8d3aScloder #include <stdio.h>
3571ec8d3aScloder #include <string.h>
3671ec8d3aScloder 
3771ec8d3aScloder #include "hash.h"
3871ec8d3aScloder #include "prf.h"
3971ec8d3aScloder 
4071ec8d3aScloder int test_prf (char *, enum hashes, char *, int, char *, int, char *);
4171ec8d3aScloder 
4271ec8d3aScloder #define nibble2c(x) ((x) >= 10 ? ('a'-10+(x)) : ('0' + (x)))
4371ec8d3aScloder 
4471ec8d3aScloder /*
4571ec8d3aScloder  * Basically the same as the HMAC regress, but to keep with modularity
4671ec8d3aScloder  * prf seems to be useful. So here we just check the HMAC test cases,
4771ec8d3aScloder  * until there are more PRFs.
4871ec8d3aScloder  */
4971ec8d3aScloder 
5071ec8d3aScloder int
main(void)5171ec8d3aScloder main (void)
5271ec8d3aScloder {
5371ec8d3aScloder   char key[100];
5471ec8d3aScloder 
5571ec8d3aScloder   memset (key, 11, 20);
5671ec8d3aScloder   test_prf ("PRF MD5 Test Case 1", HASH_MD5,
5771ec8d3aScloder 	    key, 16, "Hi There", 8, "9294727a3638bb1c13f48ef8158bfc9d");
5871ec8d3aScloder   test_prf ("PRF MD5 Test Case 2", HASH_MD5,
5971ec8d3aScloder 	    "Jefe", 4,
6071ec8d3aScloder 	    "what do ya want for nothing?", 28,
6171ec8d3aScloder 	    "750c783e6ab0b503eaa86e310a5db738");
6271ec8d3aScloder   test_prf ("PRF SHA1 Test Case 1", HASH_SHA1,
6371ec8d3aScloder 	    key, 20, "Hi There", 8,
6471ec8d3aScloder 	    "b617318655057264e28bc0b6fb378c8ef146be00");
6571ec8d3aScloder   test_prf ("PRF SHA1 Test Case 2",  HASH_SHA1,
6671ec8d3aScloder 	    "Jefe", 4, "what do ya want for nothing?", 28,
6771ec8d3aScloder 	    "effcdf6ae5eb2fa2d27416d5f184df9c259a7c79");
6871ec8d3aScloder   test_prf ("PRF SHA1 Test Case 3",  HASH_SHA1,
6971ec8d3aScloder 	    "Bloody long key, this one, eben longer than the blocksize "
7071ec8d3aScloder 	    "of ordinary keyed HMAC functions", 90,
7171ec8d3aScloder 	    "what do ya want for nothing?", 28,
7271ec8d3aScloder 	    "52ca5fbcd7d4821bc6bf8b6e95e131109dff901b");
7371ec8d3aScloder 
7471ec8d3aScloder   return 0;
7571ec8d3aScloder }
7671ec8d3aScloder 
7771ec8d3aScloder int
test_prf(char * test,enum hashes hash,char * key,int klen,char * data,int dlen,char * cmp)7871ec8d3aScloder test_prf (char *test, enum hashes hash, char *key, int klen,
7971ec8d3aScloder 	  char *data, int dlen, char *cmp)
8071ec8d3aScloder {
8171ec8d3aScloder   char output[2*HASH_MAX+1];
8271ec8d3aScloder   char digest[HASH_MAX];
8371ec8d3aScloder   struct prf *prf;
8471ec8d3aScloder   int i;
8571ec8d3aScloder 
8671ec8d3aScloder   printf ("Testing %s: ", test);
8771ec8d3aScloder 
8871ec8d3aScloder   prf = prf_alloc (PRF_HMAC, hash, key, klen);
8971ec8d3aScloder   if (!prf)
9071ec8d3aScloder     {
9171ec8d3aScloder       printf("prf_alloc () failed\n");
9271ec8d3aScloder       return 0;
9371ec8d3aScloder     }
9471ec8d3aScloder 
9571ec8d3aScloder   prf->Init (prf->prfctx);
9671ec8d3aScloder   prf->Update (prf->prfctx, data, dlen);
9771ec8d3aScloder   prf->Final (digest, prf->prfctx);
9871ec8d3aScloder 
9971ec8d3aScloder   for (i = 0; i < prf->blocksize; i++)
10071ec8d3aScloder     {
10171ec8d3aScloder       output[2 * i] = nibble2c ((digest[i] >> 4) & 0xf);
10271ec8d3aScloder       output[2 * i + 1] = nibble2c (digest[i] & 0xf);
10371ec8d3aScloder     }
10471ec8d3aScloder   output[2 * i] = 0;
10571ec8d3aScloder 
10698ae1486Smikeb   prf_free (prf);
10798ae1486Smikeb 
10871ec8d3aScloder   if (strcmp (output, cmp) == 0)
10971ec8d3aScloder     {
11071ec8d3aScloder       printf ("OKAY\n");
11171ec8d3aScloder       return 1;
11271ec8d3aScloder     }
11371ec8d3aScloder 
11471ec8d3aScloder   printf ("%s <-> %s\n", output, cmp);
11571ec8d3aScloder   return 0;
11671ec8d3aScloder }
117