xref: /freebsd/crypto/krb5/src/lib/crypto/crypto_tests/t_nfold.c (revision f1c4c3daccbaf3820f0e2224de53df12fc952fcc)
1 /* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2 /* lib/crypto/crypto_tests/t_nfold.c - Test nfold implementation correctness */
3 /*
4  * Copyright 1988, 1990 by the Massachusetts Institute of Technology.
5  * All Rights Reserved.
6  *
7  * Export of this software from the United States of America may
8  *   require a specific license from the United States Government.
9  *   It is the responsibility of any person or organization contemplating
10  *   export to obtain such a license before exporting.
11  *
12  * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
13  * distribute this software and its documentation for any purpose and
14  * without fee is hereby granted, provided that the above copyright
15  * notice appear in all copies and that both that copyright notice and
16  * this permission notice appear in supporting documentation, and that
17  * the name of M.I.T. not be used in advertising or publicity pertaining
18  * to distribution of the software without specific, written prior
19  * permission.  Furthermore if you modify this software you must label
20  * your software as modified software and not distribute it in such a
21  * fashion that it might be confused with the original M.I.T. software.
22  * M.I.T. makes no representations about the suitability of
23  * this software for any purpose.  It is provided "as is" without express
24  * or implied warranty.
25  */
26 
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <assert.h>
31 
32 #include "crypto_int.h"
33 
34 #define ASIZE(ARRAY) (sizeof(ARRAY)/sizeof(ARRAY[0]))
35 
36 static void
printhex(size_t len,const unsigned char * p)37 printhex(size_t len, const unsigned char *p)
38 {
39     while (len--)
40         printf ("%02x", 0xff & *p++);
41 }
42 
43 static void
printstringhex(const unsigned char * p)44 printstringhex(const unsigned char *p) {
45     printhex (strlen ((const char *) p), p);
46 }
47 
48 static void
rfc_tests(void)49 rfc_tests(void)
50 {
51     unsigned i;
52     struct {
53         char *input;
54         unsigned int n;
55         unsigned char exp[192/8];
56     } tests[] = {
57         { "012345", 64,
58           { 0xbe,0x07,0x26,0x31,0x27,0x6b,0x19,0x55, }
59         },
60         { "password", 56,
61           { 0x78,0xa0,0x7b,0x6c,0xaf,0x85,0xfa, }
62         },
63         { "Rough Consensus, and Running Code", 64,
64           { 0xbb,0x6e,0xd3,0x08,0x70,0xb7,0xf0,0xe0, }
65         },
66         { "password", 168,
67           { 0x59,0xe4,0xa8,0xca,0x7c,0x03,0x85,0xc3,
68             0xc3,0x7b,0x3f,0x6d,0x20,0x00,0x24,0x7c,
69             0xb6,0xe6,0xbd,0x5b,0x3e, }
70         },
71         { "MASSACHVSETTS INSTITVTE OF TECHNOLOGY", 192,
72           { 0xdb,0x3b,0x0d,0x8f,0x0b,0x06,0x1e,0x60,
73             0x32,0x82,0xb3,0x08,0xa5,0x08,0x41,0x22,
74             0x9a,0xd7,0x98,0xfa,0xb9,0x54,0x0c,0x1b, }
75         },
76     };
77     unsigned char outbuf[192/8];
78 
79     printf ("RFC tests:\n");
80     for (i = 0; i < ASIZE (tests); i++) {
81         unsigned char *p = (unsigned char *) tests[i].input;
82         assert (tests[i].n / 8 <= sizeof (outbuf));
83         krb5int_nfold (8 * strlen ((char *) p), p, tests[i].n, outbuf);
84         printf ("%d-fold(\"%s\") =\n", tests[i].n, p);
85         printf ("%d-fold(", tests[i].n);
86         printstringhex (p);
87         printf (") =\n\t");
88         printhex (tests[i].n / 8, outbuf);
89         printf ("\n\n");
90         if (memcmp (outbuf, tests[i].exp, tests[i].n/8) != 0) {
91             printf ("wrong value! expected:\n\t");
92             printhex (tests[i].n / 8, tests[i].exp);
93             exit (1);
94         }
95     }
96 }
97 
98 static void
fold_kerberos(unsigned int nbytes)99 fold_kerberos(unsigned int nbytes)
100 {
101     unsigned char cipher_text[300];
102     unsigned int j;
103 
104     if (nbytes > 300)
105         abort();
106 
107     printf("%d-fold(\"kerberos\") =\n\t", nbytes*8);
108     krb5int_nfold(64, (unsigned char *) "kerberos", 8*nbytes, cipher_text);
109     for (j=0; j<nbytes; j++)
110         printf("%s%02x", (j&3) ? "" : " ", cipher_text[j]);
111     printf("\n");
112 }
113 
114 unsigned char *nfold_in[] = {
115     (unsigned char *) "basch",
116     (unsigned char *) "eichin",
117     (unsigned char *) "sommerfeld",
118     (unsigned char *) "MASSACHVSETTS INSTITVTE OF TECHNOLOGY" };
119 
120 unsigned char nfold_192[4][24] = {
121     { 0x1a, 0xab, 0x6b, 0x42, 0x96, 0x4b, 0x98, 0xb2, 0x1f, 0x8c, 0xde, 0x2d,
122       0x24, 0x48, 0xba, 0x34, 0x55, 0xd7, 0x86, 0x2c, 0x97, 0x31, 0x64, 0x3f },
123     { 0x65, 0x69, 0x63, 0x68, 0x69, 0x6e, 0x4b, 0x73, 0x2b, 0x4b, 0x1b, 0x43,
124       0xda, 0x1a, 0x5b, 0x99, 0x5a, 0x58, 0xd2, 0xc6, 0xd0, 0xd2, 0xdc, 0xca },
125     { 0x2f, 0x7a, 0x98, 0x55, 0x7c, 0x6e, 0xe4, 0xab, 0xad, 0xf4, 0xe7, 0x11,
126       0x92, 0xdd, 0x44, 0x2b, 0xd4, 0xff, 0x53, 0x25, 0xa5, 0xde, 0xf7, 0x5c },
127     { 0xdb, 0x3b, 0x0d, 0x8f, 0x0b, 0x06, 0x1e, 0x60, 0x32, 0x82, 0xb3, 0x08,
128       0xa5, 0x08, 0x41, 0x22, 0x9a, 0xd7, 0x98, 0xfa, 0xb9, 0x54, 0x0c, 0x1b }
129 };
130 
131 int
main(int argc,char * argv[])132 main(int argc, char *argv[])
133 {
134     unsigned char cipher_text[64];
135     unsigned int i, j;
136 
137     printf("N-fold\n");
138     for (i=0; i<sizeof(nfold_in)/sizeof(char *); i++) {
139         printf("\tInput:\t\"%.*s\"\n", (int) strlen((char *) nfold_in[i]),
140                nfold_in[i]);
141         printf("\t192-Fold:\t");
142         krb5int_nfold(strlen((char *) nfold_in[i])*8, nfold_in[i], 24*8,
143                       cipher_text);
144         for (j=0; j<24; j++)
145             printf("%s%02x", (j&3) ? "" : " ", cipher_text[j]);
146         printf("\n");
147         if (memcmp(cipher_text, nfold_192[i], 24)) {
148             printf("verify: error in n-fold\n");
149             exit(-1);
150         };
151     }
152     rfc_tests ();
153 
154     printf("verify: N-fold is correct\n\n");
155 
156     fold_kerberos(8);
157     fold_kerberos(16);
158     fold_kerberos(21);
159     fold_kerberos(32);
160 
161     exit(0);
162 }
163