xref: /minix3/crypto/external/bsd/heimdal/dist/lib/otp/otptest.c (revision ebfedea0ce5bbe81e252ddf32d732e40fb633fae)
1*ebfedea0SLionel Sambuc /*	$NetBSD: otptest.c,v 1.1.1.1 2011/04/13 18:15:40 elric Exp $	*/
2*ebfedea0SLionel Sambuc 
3*ebfedea0SLionel Sambuc /*
4*ebfedea0SLionel Sambuc  * Copyright (c) 1995, 1996, 1997 Kungliga Tekniska Högskolan
5*ebfedea0SLionel Sambuc  * (Royal Institute of Technology, Stockholm, Sweden).
6*ebfedea0SLionel Sambuc  * All rights reserved.
7*ebfedea0SLionel Sambuc  *
8*ebfedea0SLionel Sambuc  * Redistribution and use in source and binary forms, with or without
9*ebfedea0SLionel Sambuc  * modification, are permitted provided that the following conditions
10*ebfedea0SLionel Sambuc  * are met:
11*ebfedea0SLionel Sambuc  *
12*ebfedea0SLionel Sambuc  * 1. Redistributions of source code must retain the above copyright
13*ebfedea0SLionel Sambuc  *    notice, this list of conditions and the following disclaimer.
14*ebfedea0SLionel Sambuc  *
15*ebfedea0SLionel Sambuc  * 2. Redistributions in binary form must reproduce the above copyright
16*ebfedea0SLionel Sambuc  *    notice, this list of conditions and the following disclaimer in the
17*ebfedea0SLionel Sambuc  *    documentation and/or other materials provided with the distribution.
18*ebfedea0SLionel Sambuc  *
19*ebfedea0SLionel Sambuc  * 3. Neither the name of the Institute nor the names of its contributors
20*ebfedea0SLionel Sambuc  *    may be used to endorse or promote products derived from this software
21*ebfedea0SLionel Sambuc  *    without specific prior written permission.
22*ebfedea0SLionel Sambuc  *
23*ebfedea0SLionel Sambuc  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
24*ebfedea0SLionel Sambuc  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25*ebfedea0SLionel Sambuc  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26*ebfedea0SLionel Sambuc  * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
27*ebfedea0SLionel Sambuc  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28*ebfedea0SLionel Sambuc  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29*ebfedea0SLionel Sambuc  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30*ebfedea0SLionel Sambuc  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31*ebfedea0SLionel Sambuc  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32*ebfedea0SLionel Sambuc  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33*ebfedea0SLionel Sambuc  * SUCH DAMAGE.
34*ebfedea0SLionel Sambuc  */
35*ebfedea0SLionel Sambuc 
36*ebfedea0SLionel Sambuc #include "config.h"
37*ebfedea0SLionel Sambuc 
38*ebfedea0SLionel Sambuc #include <stdio.h>
39*ebfedea0SLionel Sambuc #include <string.h>
40*ebfedea0SLionel Sambuc #include <otp.h>
41*ebfedea0SLionel Sambuc 
42*ebfedea0SLionel Sambuc static int
test_one(OtpKey key1,char * name,char * val,void (* print)(OtpKey,char *,size_t),OtpAlgorithm * alg)43*ebfedea0SLionel Sambuc test_one(OtpKey key1, char *name, char *val,
44*ebfedea0SLionel Sambuc 	 void (*print)(OtpKey,char*, size_t),
45*ebfedea0SLionel Sambuc 	 OtpAlgorithm *alg)
46*ebfedea0SLionel Sambuc {
47*ebfedea0SLionel Sambuc   char buf[256];
48*ebfedea0SLionel Sambuc   OtpKey key2;
49*ebfedea0SLionel Sambuc 
50*ebfedea0SLionel Sambuc   (*print)(key1, buf, sizeof(buf));
51*ebfedea0SLionel Sambuc   printf ("%s: %s, ", name, buf);
52*ebfedea0SLionel Sambuc   if (strcmp (buf, val) != 0) {
53*ebfedea0SLionel Sambuc     printf ("failed(*%s* != *%s*)\n", buf, val);
54*ebfedea0SLionel Sambuc     return 1;
55*ebfedea0SLionel Sambuc   }
56*ebfedea0SLionel Sambuc   if (otp_parse (key2, buf, alg)) {
57*ebfedea0SLionel Sambuc     printf ("parse of %s failed\n", name);
58*ebfedea0SLionel Sambuc     return 1;
59*ebfedea0SLionel Sambuc   }
60*ebfedea0SLionel Sambuc   if (memcmp (key1, key2, OTPKEYSIZE) != 0) {
61*ebfedea0SLionel Sambuc     printf ("key1 != key2, ");
62*ebfedea0SLionel Sambuc   }
63*ebfedea0SLionel Sambuc   printf ("success\n");
64*ebfedea0SLionel Sambuc   return 0;
65*ebfedea0SLionel Sambuc }
66*ebfedea0SLionel Sambuc 
67*ebfedea0SLionel Sambuc static int
test(void)68*ebfedea0SLionel Sambuc test (void)
69*ebfedea0SLionel Sambuc {
70*ebfedea0SLionel Sambuc   struct test {
71*ebfedea0SLionel Sambuc     char *alg;
72*ebfedea0SLionel Sambuc     char *passphrase;
73*ebfedea0SLionel Sambuc     char *seed;
74*ebfedea0SLionel Sambuc     int count;
75*ebfedea0SLionel Sambuc     char *hex;
76*ebfedea0SLionel Sambuc     char *word;
77*ebfedea0SLionel Sambuc   } tests[] = {
78*ebfedea0SLionel Sambuc 
79*ebfedea0SLionel Sambuc     /* md4 */
80*ebfedea0SLionel Sambuc     {"md4", "This is a test.", "TeSt", 0, "d1854218ebbb0b51", "ROME MUG FRED SCAN LIVE LACE"},
81*ebfedea0SLionel Sambuc     {"md4", "This is a test.", "TeSt", 1, "63473ef01cd0b444", "CARD SAD MINI RYE COL KIN"},
82*ebfedea0SLionel Sambuc     {"md4", "This is a test.", "TeSt", 99, "c5e612776e6c237a", "NOTE OUT IBIS SINK NAVE MODE"},
83*ebfedea0SLionel Sambuc     {"md4", "AbCdEfGhIjK", "alpha1", 0, "50076f47eb1ade4e", "AWAY SEN ROOK SALT LICE MAP"},
84*ebfedea0SLionel Sambuc     {"md4", "AbCdEfGhIjK", "alpha1", 1, "65d20d1949b5f7ab", "CHEW GRIM WU HANG BUCK SAID"},
85*ebfedea0SLionel Sambuc     {"md4", "AbCdEfGhIjK", "alpha1", 99, "d150c82cce6f62d1", "ROIL FREE COG HUNK WAIT COCA"},
86*ebfedea0SLionel Sambuc     {"md4", "OTP's are good", "correct", 0, "849c79d4f6f55388", "FOOL STEM DONE TOOL BECK NILE"},
87*ebfedea0SLionel Sambuc     {"md4", "OTP's are good", "correct", 1, "8c0992fb250847b1", "GIST AMOS MOOT AIDS FOOD SEEM"},
88*ebfedea0SLionel Sambuc     {"md4", "OTP's are good", "correct",99, "3f3bf4b4145fd74b", "TAG SLOW NOV MIN WOOL KENO"},
89*ebfedea0SLionel Sambuc 
90*ebfedea0SLionel Sambuc 
91*ebfedea0SLionel Sambuc     /* md5 */
92*ebfedea0SLionel Sambuc     {"md5", "This is a test.", "TeSt", 0, "9e876134d90499dd", "INCH SEA ANNE LONG AHEM TOUR"},
93*ebfedea0SLionel Sambuc     {"md5", "This is a test.", "TeSt", 1, "7965e05436f5029f", "EASE OIL FUM CURE AWRY AVIS"},
94*ebfedea0SLionel Sambuc     {"md5", "This is a test.", "TeSt", 99, "50fe1962c4965880", "BAIL TUFT BITS GANG CHEF THY"},
95*ebfedea0SLionel Sambuc     {"md5", "AbCdEfGhIjK", "alpha1",  0, "87066dd9644bf206", "FULL PEW DOWN ONCE MORT ARC"},
96*ebfedea0SLionel Sambuc     {"md5", "AbCdEfGhIjK", "alpha1",   1, "7cd34c1040add14b", "FACT HOOF AT FIST SITE KENT"},
97*ebfedea0SLionel Sambuc     {"md5", "AbCdEfGhIjK", "alpha1",  99, "5aa37a81f212146c", "BODE HOP JAKE STOW JUT RAP"},
98*ebfedea0SLionel Sambuc     {"md5", "OTP's are good", "correct", 0,  "f205753943de4cf9", "ULAN NEW ARMY FUSE SUIT EYED"},
99*ebfedea0SLionel Sambuc     {"md5", "OTP's are good", "correct", 1, "ddcdac956f234937", "SKIM CULT LOB SLAM POE HOWL"},
100*ebfedea0SLionel Sambuc     {"md5", "OTP's are good", "correct",99, "b203e28fa525be47", "LONG IVY JULY AJAR BOND LEE"},
101*ebfedea0SLionel Sambuc 
102*ebfedea0SLionel Sambuc     /* sha */
103*ebfedea0SLionel Sambuc     {"sha", "This is a test.", "TeSt", 0, "bb9e6ae1979d8ff4", "MILT VARY MAST OK SEES WENT"},
104*ebfedea0SLionel Sambuc     {"sha", "This is a test.", "TeSt", 1, "63d936639734385b", "CART OTTO HIVE ODE VAT NUT"},
105*ebfedea0SLionel Sambuc     {"sha", "This is a test.", "TeSt", 99, "87fec7768b73ccf9", "GAFF WAIT SKID GIG SKY EYED"},
106*ebfedea0SLionel Sambuc     {"sha", "AbCdEfGhIjK", "alpha1", 0, "ad85f658ebe383c9", "LEST OR HEEL SCOT ROB SUIT"},
107*ebfedea0SLionel Sambuc     {"sha", "AbCdEfGhIjK", "alpha1", 1, "d07ce229b5cf119b", "RITE TAKE GELD COST TUNE RECK"},
108*ebfedea0SLionel Sambuc     {"sha", "AbCdEfGhIjK", "alpha1", 99, "27bc71035aaf3dc6", "MAY STAR TIN LYON VEDA STAN"},
109*ebfedea0SLionel Sambuc     {"sha", "OTP's are good", "correct", 0, "d51f3e99bf8e6f0b", "RUST WELT KICK FELL TAIL FRAU"},
110*ebfedea0SLionel Sambuc     {"sha", "OTP's are good", "correct", 1, "82aeb52d943774e4", "FLIT DOSE ALSO MEW DRUM DEFY"},
111*ebfedea0SLionel Sambuc     {"sha", "OTP's are good", "correct", 99, "4f296a74fe1567ec", "AURA ALOE HURL WING BERG WAIT"},
112*ebfedea0SLionel Sambuc     {NULL}
113*ebfedea0SLionel Sambuc   };
114*ebfedea0SLionel Sambuc 
115*ebfedea0SLionel Sambuc   struct test *t;
116*ebfedea0SLionel Sambuc   int sum = 0;
117*ebfedea0SLionel Sambuc 
118*ebfedea0SLionel Sambuc   for(t = tests; t->alg; ++t) {
119*ebfedea0SLionel Sambuc     int i;
120*ebfedea0SLionel Sambuc     OtpAlgorithm *alg = otp_find_alg (t->alg);
121*ebfedea0SLionel Sambuc     OtpKey key;
122*ebfedea0SLionel Sambuc 
123*ebfedea0SLionel Sambuc     if (alg == NULL) {
124*ebfedea0SLionel Sambuc       printf ("Could not find alg %s\n", t->alg);
125*ebfedea0SLionel Sambuc       return 1;
126*ebfedea0SLionel Sambuc     }
127*ebfedea0SLionel Sambuc     if(alg->init (key, t->passphrase, t->seed))
128*ebfedea0SLionel Sambuc       return 1;
129*ebfedea0SLionel Sambuc     for (i = 0; i < t->count; ++i) {
130*ebfedea0SLionel Sambuc       if (alg->next (key))
131*ebfedea0SLionel Sambuc 	return 1;
132*ebfedea0SLionel Sambuc     }
133*ebfedea0SLionel Sambuc     sum += test_one (key, "hexadecimal", t->hex, otp_print_hex,
134*ebfedea0SLionel Sambuc 		     alg) +
135*ebfedea0SLionel Sambuc       test_one (key, "standard_word", t->word, otp_print_stddict, alg);
136*ebfedea0SLionel Sambuc   }
137*ebfedea0SLionel Sambuc   return sum;
138*ebfedea0SLionel Sambuc }
139*ebfedea0SLionel Sambuc 
140*ebfedea0SLionel Sambuc int
main(void)141*ebfedea0SLionel Sambuc main (void)
142*ebfedea0SLionel Sambuc {
143*ebfedea0SLionel Sambuc   return test ();
144*ebfedea0SLionel Sambuc }
145