xref: /openbsd-src/regress/lib/libskey/skeytest.c (revision 4a387c110822725636f3d5612ac90790f37f24dd)
1*4a387c11Slteo /*	$OpenBSD: skeytest.c,v 1.4 2014/03/25 04:29:49 lteo Exp $	*/
238fc1b39Smickey /*	$NetBSD: skeytest.c,v 1.3 2002/02/21 07:38:18 itojun Exp $	*/
338fc1b39Smickey 
438fc1b39Smickey /*-
538fc1b39Smickey  * Copyright (c) 2000 The NetBSD Foundation, Inc.
638fc1b39Smickey  * All rights reserved.
738fc1b39Smickey  *
838fc1b39Smickey  * Redistribution and use in source and binary forms, with or without
938fc1b39Smickey  * modification, are permitted provided that the following conditions
1038fc1b39Smickey  * are met:
1138fc1b39Smickey  * 1. Redistributions of source code must retain the above copyright
1238fc1b39Smickey  *    notice, this list of conditions and the following disclaimer.
1338fc1b39Smickey  * 2. Redistributions in binary form must reproduce the above copyright
1438fc1b39Smickey  *    notice, this list of conditions and the following disclaimer in the
1538fc1b39Smickey  *    documentation and/or other materials provided with the distribution.
1638fc1b39Smickey  *
1738fc1b39Smickey  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
1838fc1b39Smickey  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
1938fc1b39Smickey  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
2038fc1b39Smickey  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
2138fc1b39Smickey  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2238fc1b39Smickey  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2338fc1b39Smickey  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2438fc1b39Smickey  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
2538fc1b39Smickey  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
2638fc1b39Smickey  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
2738fc1b39Smickey  * POSSIBILITY OF SUCH DAMAGE.
2838fc1b39Smickey  */
2938fc1b39Smickey 
30*4a387c11Slteo /*
31*4a387c11Slteo  * This is a regression test for the S/Key implementation against the data set
32*4a387c11Slteo  * from Appendix C of RFC2289 without the MD4 set (MD4 support was removed from
33*4a387c11Slteo  * OpenBSD base in March 2014) and with the addition of an RIPEMD-160 set.
34*4a387c11Slteo  */
3538fc1b39Smickey 
3638fc1b39Smickey #include <stdio.h>
3738fc1b39Smickey #include <string.h>
3838fc1b39Smickey #include "skey.h"
3938fc1b39Smickey 
4038fc1b39Smickey struct regRes {
4138fc1b39Smickey 	char *algo, *zero, *one, *nine;
4238fc1b39Smickey 	};
4338fc1b39Smickey 
4438fc1b39Smickey struct regPass {
4538fc1b39Smickey 	char *passphrase, *seed;
4638fc1b39Smickey 	struct regRes res[4];
4738fc1b39Smickey 	} regPass[] = {
4838fc1b39Smickey 		{ "This is a test.", "TeSt", {
4938fc1b39Smickey 			{ "md5", "9E876134D90499DD", "7965E05436F5029F", "50FE1962C4965880" },
50*4a387c11Slteo 			{ "rmd160","3A1BFB10A64B4CCD", "39D56BF655E65DE7", "42F84BA862941033" },
5138fc1b39Smickey 			{ "sha1","BB9E6AE1979D8FF4", "63D936639734385B", "87FEC7768B73CCF9" },
5238fc1b39Smickey 			{ NULL } } },
5338fc1b39Smickey 		{ "AbCdEfGhIjK", "alpha1", {
5438fc1b39Smickey 			{ "md5", "87066DD9644BF206", "7CD34C1040ADD14B", "5AA37A81F212146C" },
55*4a387c11Slteo 			{ "rmd160","726EDD1BB5DB3642", "46A231C501A1D2CE", "848664EF3A300CC9" },
5638fc1b39Smickey 			{ "sha1","AD85F658EBE383C9", "D07CE229B5CF119B", "27BC71035AAF3DC6" },
5738fc1b39Smickey 			{ NULL } } },
5838fc1b39Smickey 		{ "OTP's are good", "correct", {
5938fc1b39Smickey 			{ "md5", "F205753943DE4CF9", "DDCDAC956F234937", "B203E28FA525BE47" },
60*4a387c11Slteo 			{ "rmd160","F90D03CC969208C8", "B6F5D25A08A90009", "C890C1F05018BA5F" },
6138fc1b39Smickey 			{ "sha1","D51F3E99BF8E6F0B", "82AEB52D943774E4", "4F296A74FE1567EC" },
6238fc1b39Smickey 			{ NULL } } },
6338fc1b39Smickey 		{ NULL }
6438fc1b39Smickey 	};
6538fc1b39Smickey 
66db3296cfSderaadt int
main(int argc,char * argv[])67db3296cfSderaadt main(int argc, char *argv[])
6838fc1b39Smickey {
6938fc1b39Smickey 	char data[16], prn[64];
7038fc1b39Smickey 	struct regPass *rp;
7138fc1b39Smickey 	int i = 0;
7238fc1b39Smickey 	int errors = 0;
7338fc1b39Smickey 	int j;
7438fc1b39Smickey 
75*4a387c11Slteo 	if (strcmp(skey_get_algorithm(), "md5") != 0) {
76*4a387c11Slteo 		errors++;
77*4a387c11Slteo 		printf("default algorithm is not md5\n");
78*4a387c11Slteo 	}
79*4a387c11Slteo 
80*4a387c11Slteo 	if (skey_set_algorithm("md4") != NULL) {
81*4a387c11Slteo 		errors++;
82*4a387c11Slteo 		printf("accepted unsupported algorithm md4\n");
83*4a387c11Slteo 	}
84*4a387c11Slteo 
85db3296cfSderaadt 	for(rp = regPass; rp->passphrase; rp++) {
8638fc1b39Smickey 		struct regRes *rr;
8738fc1b39Smickey 
8838fc1b39Smickey 		i++;
89db3296cfSderaadt 		for(rr = rp->res; rr->algo; rr++) {
90*4a387c11Slteo 			if (skey_set_algorithm(rr->algo) == NULL) {
91*4a387c11Slteo 				errors++;
92*4a387c11Slteo 				printf("Set %d: %s algorithm is not supported\n",
93*4a387c11Slteo 				    i, rr->algo);
94*4a387c11Slteo 				continue;
95*4a387c11Slteo 			}
96*4a387c11Slteo 
97*4a387c11Slteo 			if (strcmp(skey_get_algorithm(), rr->algo) != 0) {
98*4a387c11Slteo 				errors++;
99*4a387c11Slteo 				printf("Set %d: unable to set algorithm to %s\n",
100*4a387c11Slteo 				    i, rr->algo);
101*4a387c11Slteo 				continue;
102*4a387c11Slteo 			}
10338fc1b39Smickey 
10438fc1b39Smickey 			keycrunch(data, rp->seed, rp->passphrase);
10538fc1b39Smickey 			btoa8(prn, data);
10638fc1b39Smickey 
107db3296cfSderaadt 			if(strcasecmp(prn, rr->zero)) {
10838fc1b39Smickey 				errors++;
109db3296cfSderaadt 				printf("Set %d, round 0, %s: Expected %s and got %s\n",
110db3296cfSderaadt 				    i, rr->algo, rr->zero, prn);
11138fc1b39Smickey 			}
11238fc1b39Smickey 
11338fc1b39Smickey 			f(data);
11438fc1b39Smickey 			btoa8(prn, data);
11538fc1b39Smickey 
116db3296cfSderaadt 			if(strcasecmp(prn, rr->one)) {
11738fc1b39Smickey 				errors++;
118db3296cfSderaadt 				printf("Set %d, round 1, %s: Expected %s and got %s\n",
119db3296cfSderaadt 				    i, rr->algo, rr->one, prn);
12038fc1b39Smickey 			}
12138fc1b39Smickey 
12238fc1b39Smickey 			for(j=1; j<99; j++)
12338fc1b39Smickey 				f(data);
12438fc1b39Smickey 			btoa8(prn, data);
12538fc1b39Smickey 
126db3296cfSderaadt 			if(strcasecmp(prn, rr->nine)) {
12738fc1b39Smickey 				errors++;
128db3296cfSderaadt 				printf("Set %d, round 99, %s: Expected %s and got %s\n",
129db3296cfSderaadt 				    i, rr->algo, rr->nine, prn);
13038fc1b39Smickey 			}
13138fc1b39Smickey 		}
13238fc1b39Smickey 	}
13338fc1b39Smickey 
13438fc1b39Smickey 	printf("%d errors\n", errors);
13538fc1b39Smickey 	return(errors ? 1 : 0);
13638fc1b39Smickey }
137