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