xref: /freebsd/crypto/krb5/src/lib/crypto/crypto_tests/camellia-test.c (revision f1c4c3daccbaf3820f0e2224de53df12fc952fcc)
1 /* lib/crypto/crypto_tests/camellia-test.c */
2 /*
3  * Copyright (c) 2009
4  * NTT (Nippon Telegraph and Telephone Corporation) . All rights reserved.
5  *
6  * Export of this software from the United States of America may
7  *   require a specific license from the United States Government.
8  *   It is the responsibility of any person or organization contemplating
9  *   export to obtain such a license before exporting.
10  *
11  * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
12  * distribute this software and its documentation for any purpose and
13  * without fee is hereby granted, provided that the above copyright
14  * notice appear in all copies and that both that copyright notice and
15  * this permission notice appear in supporting documentation, and that
16  * the name of M.I.T. not be used in advertising or publicity pertaining
17  * to distribution of the software without specific, written prior
18  * permission.  Furthermore if you modify this software you must label
19  * your software as modified software and not distribute it in such a
20  * fashion that it might be confused with the original M.I.T. software.
21  * M.I.T. makes no representations about the suitability of
22  * this software for any purpose.  It is provided "as is" without express
23  * or implied warranty.
24  *
25  *
26  * Subset of NIST tests for AES as applied to Camellia; specifically, the
27  * variable-key and variable-text tests for 128- and 256-bit keys.
28  */
29 
30 #include <stdio.h>
31 #include "crypto_int.h"
32 
33 static char key[32];
34 static char plain[16], cipher[16], zero[16];
35 
36 static krb5_keyblock enc_key;
37 static krb5_data ivec;
init(void)38 static void init(void)
39 {
40     enc_key.contents = (unsigned char *)key;
41     enc_key.length = 16;
42     ivec.data = zero;
43     ivec.length = 16;
44 }
enc(void)45 static void enc(void)
46 {
47     krb5_key k;
48     krb5_crypto_iov iov;
49 
50     memcpy(cipher, plain, 16);
51     iov.flags = KRB5_CRYPTO_TYPE_DATA;
52     iov.data = make_data(cipher, 16);
53     krb5_k_create_key(NULL, &enc_key, &k);
54     krb5int_camellia_encrypt(k, &ivec, &iov, 1);
55     krb5_k_free_key(NULL, k);
56 }
57 
hexdump(const char * label,const char * cp,int len)58 static void hexdump(const char *label, const char *cp, int len)
59 {
60     printf("%s=", label);
61     while (len--) printf("%02X", 0xff & *cp++);
62     printf("\n");
63 }
64 
set_bit(char * ptr,int bitnum)65 static void set_bit(char *ptr, int bitnum)
66 {
67     int bytenum;
68     bytenum = bitnum / 8;
69     bitnum %= 8;
70     /* First bit is the high bit! */
71     ptr[bytenum] = 1 << (7 - bitnum);
72 }
73 
74 /* Variable-Key tests */
vk_test_1(int len)75 static void vk_test_1(int len)
76 {
77     int i;
78 
79     enc_key.enctype = ENCTYPE_CAMELLIA128_CTS_CMAC;
80     enc_key.length = len;
81     printf("\nKEYSIZE=%d\n\n", len * 8);
82     memset(plain, 0, sizeof(plain));
83     hexdump("PT", plain, 16);
84     for (i = 0; i < len * 8; i++) {
85 	memset(key, 0, len);
86 	set_bit(key, i);
87 	printf("\nI=%d\n", i+1);
88 	hexdump("KEY", key, len);
89 	enc();
90 	hexdump("CT", cipher, 16);
91     }
92     printf("\n==========\n");
93 }
vk_test(void)94 static void vk_test(void)
95 {
96     vk_test_1(16);
97     vk_test_1(32);
98 }
99 
100 /* Variable-Text tests */
vt_test_1(int len,krb5_enctype etype)101 static void vt_test_1(int len, krb5_enctype etype)
102 {
103     int i;
104 
105     enc_key.enctype = etype;
106     enc_key.length = len;
107     printf("\nKEYSIZE=%d\n\n", len * 8);
108     memset(key, 0, len);
109     hexdump("KEY", key, len);
110     for (i = 0; i < 16 * 8; i++) {
111 	memset(plain, 0, sizeof(plain));
112 	set_bit(plain, i);
113 	printf("\nI=%d\n", i+1);
114 	hexdump("PT", plain, 16);
115 	enc();
116 	hexdump("CT", cipher, 16);
117     }
118     printf("\n==========\n");
119 }
vt_test(void)120 static void vt_test(void)
121 {
122     vt_test_1(16, ENCTYPE_CAMELLIA128_CTS_CMAC);
123     vt_test_1(32, ENCTYPE_CAMELLIA256_CTS_CMAC);
124 }
125 
main(int argc,char * argv[])126 int main (int argc, char *argv[])
127 {
128     if (argc > 2 || (argc == 2 && strcmp(argv[1], "-k"))) {
129 	fprintf(stderr,
130 		"usage:\t%s -k\tfor variable-key tests\n"
131 		"   or:\t%s   \tfor variable-plaintext tests\n",
132 		argv[0], argv[0]);
133 	return 1;
134     }
135     init();
136     if (argc == 2)
137 	vk_test();
138     else
139 	vt_test();
140     return 0;
141 }
142