1 /* LibTomCrypt, modular cryptographic library -- Tom St Denis */
2 /* SPDX-License-Identifier: Unlicense */
3
4 /**
5 @file ocb_test.c
6 OCB implementation, self-test by Tom St Denis
7 */
8 #include "tomcrypt_private.h"
9
10 #ifdef LTC_OCB_MODE
11
12 /**
13 Test the OCB protocol
14 @return CRYPT_OK if successful
15 */
ocb_test(void)16 int ocb_test(void)
17 {
18 #ifndef LTC_TEST
19 return CRYPT_NOP;
20 #else
21 static const struct {
22 int ptlen;
23 unsigned char key[16], nonce[16], pt[34], ct[34], tag[16];
24 } tests[] = {
25
26 /* OCB-AES-128-0B */
27 {
28 0,
29 /* key */
30 { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
31 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f },
32 /* nonce */
33 { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
34 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01 },
35 /* pt */
36 { 0 },
37 /* ct */
38 { 0 },
39 /* tag */
40 { 0x15, 0xd3, 0x7d, 0xd7, 0xc8, 0x90, 0xd5, 0xd6,
41 0xac, 0xab, 0x92, 0x7b, 0xc0, 0xdc, 0x60, 0xee },
42 },
43
44
45 /* OCB-AES-128-3B */
46 {
47 3,
48 /* key */
49 { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
50 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f },
51 /* nonce */
52 { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
53 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01 },
54 /* pt */
55 { 0x00, 0x01, 0x02 },
56 /* ct */
57 { 0xfc, 0xd3, 0x7d },
58 /* tag */
59 { 0x02, 0x25, 0x47, 0x39, 0xa5, 0xe3, 0x56, 0x5a,
60 0xe2, 0xdc, 0xd6, 0x2c, 0x65, 0x97, 0x46, 0xba },
61 },
62
63 /* OCB-AES-128-16B */
64 {
65 16,
66 /* key */
67 { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
68 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f },
69 /* nonce */
70 { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
71 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01 },
72 /* pt */
73 { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
74 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f },
75 /* ct */
76 { 0x37, 0xdf, 0x8c, 0xe1, 0x5b, 0x48, 0x9b, 0xf3,
77 0x1d, 0x0f, 0xc4, 0x4d, 0xa1, 0xfa, 0xf6, 0xd6 },
78 /* tag */
79 { 0xdf, 0xb7, 0x63, 0xeb, 0xdb, 0x5f, 0x0e, 0x71,
80 0x9c, 0x7b, 0x41, 0x61, 0x80, 0x80, 0x04, 0xdf },
81 },
82
83 /* OCB-AES-128-20B */
84 {
85 20,
86 /* key */
87 { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
88 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f },
89 /* nonce */
90 { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
91 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01 },
92 /* pt */
93 { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
94 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
95 0x10, 0x11, 0x12, 0x13 },
96 /* ct */
97 { 0x01, 0xa0, 0x75, 0xf0, 0xd8, 0x15, 0xb1, 0xa4,
98 0xe9, 0xc8, 0x81, 0xa1, 0xbc, 0xff, 0xc3, 0xeb,
99 0x70, 0x03, 0xeb, 0x55},
100 /* tag */
101 { 0x75, 0x30, 0x84, 0x14, 0x4e, 0xb6, 0x3b, 0x77,
102 0x0b, 0x06, 0x3c, 0x2e, 0x23, 0xcd, 0xa0, 0xbb },
103 },
104
105 /* OCB-AES-128-32B */
106 {
107 32,
108 /* key */
109 { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
110 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f },
111 /* nonce */
112 { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
113 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01 },
114 /* pt */
115 { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
116 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
117 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
118 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f },
119 /* ct */
120 { 0x01, 0xa0, 0x75, 0xf0, 0xd8, 0x15, 0xb1, 0xa4,
121 0xe9, 0xc8, 0x81, 0xa1, 0xbc, 0xff, 0xc3, 0xeb,
122 0x4a, 0xfc, 0xbb, 0x7f, 0xed, 0xc0, 0x8c, 0xa8,
123 0x65, 0x4c, 0x6d, 0x30, 0x4d, 0x16, 0x12, 0xfa },
124
125 /* tag */
126 { 0xc1, 0x4c, 0xbf, 0x2c, 0x1a, 0x1f, 0x1c, 0x3c,
127 0x13, 0x7e, 0xad, 0xea, 0x1f, 0x2f, 0x2f, 0xcf },
128 },
129
130 /* OCB-AES-128-34B */
131 {
132 34,
133 /* key */
134 { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
135 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f },
136 /* nonce */
137 { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
138 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01 },
139 /* pt */
140 { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
141 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
142 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
143 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
144 0x20, 0x21 },
145 /* ct */
146 { 0x01, 0xa0, 0x75, 0xf0, 0xd8, 0x15, 0xb1, 0xa4,
147 0xe9, 0xc8, 0x81, 0xa1, 0xbc, 0xff, 0xc3, 0xeb,
148 0xd4, 0x90, 0x3d, 0xd0, 0x02, 0x5b, 0xa4, 0xaa,
149 0x83, 0x7c, 0x74, 0xf1, 0x21, 0xb0, 0x26, 0x0f,
150 0xa9, 0x5d },
151
152 /* tag */
153 { 0xcf, 0x83, 0x41, 0xbb, 0x10, 0x82, 0x0c, 0xcf,
154 0x14, 0xbd, 0xec, 0x56, 0xb8, 0xd7, 0xd6, 0xab },
155 },
156
157 };
158
159 int err, x, idx, res;
160 unsigned long len;
161 unsigned char outct[MAXBLOCKSIZE], outtag[MAXBLOCKSIZE];
162
163 /* AES can be under rijndael or aes... try to find it */
164 if ((idx = find_cipher("aes")) == -1) {
165 if ((idx = find_cipher("rijndael")) == -1) {
166 return CRYPT_NOP;
167 }
168 }
169
170 for (x = 0; x < (int)(sizeof(tests)/sizeof(tests[0])); x++) {
171 len = sizeof(outtag);
172 if ((err = ocb_encrypt_authenticate_memory(idx, tests[x].key, 16,
173 tests[x].nonce, tests[x].pt, tests[x].ptlen, outct, outtag, &len)) != CRYPT_OK) {
174 return err;
175 }
176
177 if (compare_testvector(outtag, len, tests[x].tag, sizeof(tests[x].tag), "OCB Tag", x) ||
178 compare_testvector(outct, tests[x].ptlen, tests[x].ct, tests[x].ptlen, "OCB CT", x)) {
179 return CRYPT_FAIL_TESTVECTOR;
180 }
181
182 if ((err = ocb_decrypt_verify_memory(idx, tests[x].key, 16, tests[x].nonce, outct, tests[x].ptlen,
183 outct, tests[x].tag, len, &res)) != CRYPT_OK) {
184 return err;
185 }
186 if ((res != 1) || compare_testvector(outct, tests[x].ptlen, tests[x].pt, tests[x].ptlen, "OCB", x)) {
187 #ifdef LTC_TEST_DBG
188 printf("\n\nOCB: Failure-decrypt - res = %d\n", res);
189 #endif
190 return CRYPT_FAIL_TESTVECTOR;
191 }
192 }
193 return CRYPT_OK;
194 #endif /* LTC_TEST */
195 }
196
197 #endif /* LTC_OCB_MODE */
198
199
200 /* some comments
201
202 -- it's hard to seek
203 -- hard to stream [you can't emit ciphertext until full block]
204 -- The setup is somewhat complicated...
205 */
206