xref: /freebsd/crypto/krb5/src/lib/crypto/crypto_tests/t_cts.c (revision f1c4c3daccbaf3820f0e2224de53df12fc952fcc)
1 /* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2 /* lib/crypto/crypto_tests/t_cts.c */
3 /*
4  * Copyright 2001, 2007 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 /*
28  *
29  * Test vectors for crypto code, matching data submitted for inclusion
30  * with RFC1510bis.
31  *
32  * N.B.: Doesn't compile -- this file uses some routines internal to our
33  * crypto library which are declared "static" and thus aren't accessible
34  * without modifying the other sources.
35  */
36 
37 #include <assert.h>
38 #include <stdio.h>
39 #include <string.h>
40 #include <ctype.h>
41 #include "crypto_int.h"
42 
43 #define ASIZE(ARRAY) (sizeof(ARRAY)/sizeof(ARRAY[0]))
44 
45 const char *whoami;
46 
47 #define JURISIC "Juri\305\241i\304\207" /* hi Miro */
48 #define ESZETT "\303\237"
49 #define GCLEF  "\360\235\204\236" /* outside BMP, woo hoo!  */
50 
printd(const char * descr,krb5_data * d)51 static void printd (const char *descr, krb5_data *d) {
52     unsigned int i, j;
53     const int r = 16;
54 
55     printf("%s:", descr);
56 
57     for (i = 0; i < d->length; i += r) {
58         printf("\n  %04x: ", i);
59         for (j = i; j < i + r && j < d->length; j++)
60             printf(" %02x", 0xff & d->data[j]);
61 #ifdef SHOW_TEXT
62         for (; j < i + r; j++)
63             printf("   ");
64         printf("   ");
65         for (j = i; j < i + r && j < d->length; j++) {
66             int c = 0xff & d->data[j];
67             printf("%c", isprint(c) ? c : '.');
68         }
69 #endif
70     }
71     printf("\n");
72 }
printk(const char * descr,krb5_keyblock * k)73 static void printk(const char *descr, krb5_keyblock *k) {
74     krb5_data d;
75     d.data = (char *) k->contents;
76     d.length = k->length;
77     printd(descr, &d);
78 }
79 
test_cts(void)80 static void test_cts(void)
81 {
82     static const char input[4*16] =
83         "I would like the General Gau's Chicken, please, and wonton soup.";
84     static const unsigned char aeskey[16] = "chicken teriyaki";
85     static const int lengths[] = { 17, 31, 32, 47, 48, 64 };
86 
87     unsigned int i;
88     char outbuf[64], encivbuf[16], decivbuf[16];
89     krb5_crypto_iov iov;
90     krb5_data in, enciv, deciv;
91     krb5_keyblock keyblock;
92     krb5_key key;
93     krb5_error_code err;
94 
95     iov.flags = KRB5_CRYPTO_TYPE_DATA;
96     iov.data.data = outbuf;
97     in.data = (char *)input;
98     enciv.length = deciv.length = 16;
99     enciv.data = encivbuf;
100     deciv.data = decivbuf;
101     keyblock.contents = (krb5_octet *)aeskey;
102     keyblock.length = 16;
103     keyblock.enctype = ENCTYPE_AES128_CTS_HMAC_SHA1_96;
104 
105     err = krb5_k_create_key(NULL, &keyblock, &key);
106     if (err) {
107         printf("error %ld from krb5_k_create_key\n", (long)err);
108         exit(1);
109     }
110 
111     memset(enciv.data, 0, 16);
112     printk("AES 128-bit key", &keyblock);
113     for (i = 0; i < sizeof(lengths)/sizeof(lengths[0]); i++) {
114         memset(enciv.data, 0, 16);
115         memset(deciv.data, 0, 16);
116 
117         printf("\n");
118         iov.data.length = in.length = lengths[i];
119         memcpy(outbuf, input, lengths[i]);
120         printd("IV", &enciv);
121         err = krb5int_aes_encrypt(key, &enciv, &iov, 1);
122         if (err) {
123             printf("error %ld from krb5int_aes_encrypt\n", (long)err);
124             exit(1);
125         }
126         printd("Input", &in);
127         printd("Output", &iov.data);
128         printd("Next IV", &enciv);
129         err = krb5int_aes_decrypt(key, &deciv, &iov, 1);
130         if (err) {
131             printf("error %ld from krb5int_aes_decrypt\n", (long)err);
132             exit(1);
133         }
134         if (memcmp(outbuf, input, lengths[i]) != 0) {
135             printd("Decryption result DOESN'T MATCH", &iov.data);
136             exit(1);
137         }
138         if (memcmp(enciv.data, deciv.data, 16)) {
139             printd("Decryption IV result DOESN'T MATCH", &deciv);
140             exit(1);
141         }
142     }
143     krb5_k_free_key(NULL, key);
144 }
145 
main(int argc,char ** argv)146 int main (int argc, char **argv)
147 {
148     whoami = argv[0];
149     test_cts();
150     return 0;
151 }
152