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