Fix Serpent-AVX2 and Camellia-AVX2 counter modes
[libgcrypt.git] / cipher / camellia-glue.c
1 /* camellia-glue.c - Glue for the Camellia cipher
2  * Copyright (C) 2007 Free Software Foundation, Inc.
3  *
4  * This file is part of Libgcrypt.
5  *
6  * Libgcrypt is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU Lesser General Public License as
8  * published by the Free Software Foundation; either version 2.1 of
9  * the License, or (at your option) any later version.
10  *
11  * Libgcrypt is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19  * 02110-1301, USA.
20  */
21
22 /* I put all the libgcrypt-specific stuff in this file to keep the
23    camellia.c/camellia.h files exactly as provided by NTT.  If they
24    update their code, this should make it easier to bring the changes
25    in. - dshaw
26
27    There is one small change which needs to be done: Include the
28    following code at the top of camellia.h: */
29 #if 0
30
31 /* To use Camellia with libraries it is often useful to keep the name
32  * space of the library clean.  The following macro is thus useful:
33  *
34  *     #define CAMELLIA_EXT_SYM_PREFIX foo_
35  *
36  * This prefixes all external symbols with "foo_".
37  */
38 #ifdef HAVE_CONFIG_H
39 #include <config.h>
40 #endif
41 #ifdef CAMELLIA_EXT_SYM_PREFIX
42 #define CAMELLIA_PREFIX1(x,y) x ## y
43 #define CAMELLIA_PREFIX2(x,y) CAMELLIA_PREFIX1(x,y)
44 #define CAMELLIA_PREFIX(x)    CAMELLIA_PREFIX2(CAMELLIA_EXT_SYM_PREFIX,x)
45 #define Camellia_Ekeygen      CAMELLIA_PREFIX(Camellia_Ekeygen)
46 #define Camellia_EncryptBlock CAMELLIA_PREFIX(Camellia_EncryptBlock)
47 #define Camellia_DecryptBlock CAMELLIA_PREFIX(Camellia_DecryptBlock)
48 #define camellia_decrypt128   CAMELLIA_PREFIX(camellia_decrypt128)
49 #define camellia_decrypt256   CAMELLIA_PREFIX(camellia_decrypt256)
50 #define camellia_encrypt128   CAMELLIA_PREFIX(camellia_encrypt128)
51 #define camellia_encrypt256   CAMELLIA_PREFIX(camellia_encrypt256)
52 #define camellia_setup128     CAMELLIA_PREFIX(camellia_setup128)
53 #define camellia_setup192     CAMELLIA_PREFIX(camellia_setup192)
54 #define camellia_setup256     CAMELLIA_PREFIX(camellia_setup256)
55 #endif /*CAMELLIA_EXT_SYM_PREFIX*/
56
57 #endif /* Code sample. */
58
59
60 #include <config.h>
61 #include "types.h"
62 #include "g10lib.h"
63 #include "cipher.h"
64 #include "camellia.h"
65 #include "bufhelp.h"
66 #include "cipher-selftest.h"
67
68 /* Helper macro to force alignment to 16 bytes.  */
69 #ifdef HAVE_GCC_ATTRIBUTE_ALIGNED
70 # define ATTR_ALIGNED_16  __attribute__ ((aligned (16)))
71 #else
72 # define ATTR_ALIGNED_16
73 #endif
74
75 /* USE_AESNI inidicates whether to compile with Intel AES-NI/AVX code. */
76 #undef USE_AESNI_AVX
77 #if defined(ENABLE_AESNI_SUPPORT) && defined(ENABLE_AVX_SUPPORT)
78 # if defined(__x86_64__) && defined(HAVE_COMPATIBLE_GCC_AMD64_PLATFORM_AS)
79 #  define USE_AESNI_AVX 1
80 # endif
81 #endif
82
83 /* USE_AESNI_AVX2 inidicates whether to compile with Intel AES-NI/AVX2 code. */
84 #undef USE_AESNI_AVX2
85 #if defined(ENABLE_AESNI_SUPPORT) && defined(ENABLE_AVX2_SUPPORT)
86 # if defined(__x86_64__) && defined(HAVE_COMPATIBLE_GCC_AMD64_PLATFORM_AS)
87 #  define USE_AESNI_AVX2 1
88 # endif
89 #endif
90
91 typedef struct
92 {
93   int keybitlength;
94   KEY_TABLE_TYPE keytable;
95 #ifdef USE_AESNI_AVX
96   int use_aesni_avx;            /* AES-NI/AVX implementation shall be used.  */
97 #endif /*USE_AESNI_AVX*/
98 #ifdef USE_AESNI_AVX2
99   int use_aesni_avx2;           /* AES-NI/AVX2 implementation shall be used.  */
100 #endif /*USE_AESNI_AVX2*/
101 } CAMELLIA_context;
102
103 #ifdef USE_AESNI_AVX
104 /* Assembler implementations of Camellia using AES-NI and AVX.  Process data
105    in 16 block same time.
106  */
107 extern void _gcry_camellia_aesni_avx_ctr_enc(CAMELLIA_context *ctx,
108                                              unsigned char *out,
109                                              const unsigned char *in,
110                                              unsigned char *ctr);
111
112 extern void _gcry_camellia_aesni_avx_cbc_dec(CAMELLIA_context *ctx,
113                                              unsigned char *out,
114                                              const unsigned char *in,
115                                              unsigned char *iv);
116
117 extern void _gcry_camellia_aesni_avx_cfb_dec(CAMELLIA_context *ctx,
118                                              unsigned char *out,
119                                              const unsigned char *in,
120                                              unsigned char *iv);
121 #endif
122
123 #ifdef USE_AESNI_AVX2
124 /* Assembler implementations of Camellia using AES-NI and AVX2.  Process data
125    in 32 block same time.
126  */
127 extern void _gcry_camellia_aesni_avx2_ctr_enc(CAMELLIA_context *ctx,
128                                               unsigned char *out,
129                                               const unsigned char *in,
130                                               unsigned char *ctr);
131
132 extern void _gcry_camellia_aesni_avx2_cbc_dec(CAMELLIA_context *ctx,
133                                               unsigned char *out,
134                                               const unsigned char *in,
135                                               unsigned char *iv);
136
137 extern void _gcry_camellia_aesni_avx2_cfb_dec(CAMELLIA_context *ctx,
138                                               unsigned char *out,
139                                               const unsigned char *in,
140                                               unsigned char *iv);
141 #endif
142
143 static const char *selftest(void);
144
145 static gcry_err_code_t
146 camellia_setkey(void *c, const byte *key, unsigned keylen)
147 {
148   CAMELLIA_context *ctx=c;
149   static int initialized=0;
150   static const char *selftest_failed=NULL;
151
152   if(keylen!=16 && keylen!=24 && keylen!=32)
153     return GPG_ERR_INV_KEYLEN;
154
155   if(!initialized)
156     {
157       initialized=1;
158       selftest_failed=selftest();
159       if(selftest_failed)
160         log_error("%s\n",selftest_failed);
161     }
162
163   if(selftest_failed)
164     return GPG_ERR_SELFTEST_FAILED;
165
166   ctx->keybitlength=keylen*8;
167   Camellia_Ekeygen(ctx->keybitlength,key,ctx->keytable);
168   _gcry_burn_stack
169     ((19+34+34)*sizeof(u32)+2*sizeof(void*) /* camellia_setup256 */
170      +(4+32)*sizeof(u32)+2*sizeof(void*)    /* camellia_setup192 */
171      +0+sizeof(int)+2*sizeof(void*)         /* Camellia_Ekeygen */
172      +3*2*sizeof(void*)                     /* Function calls.  */
173      );
174
175 #ifdef USE_AESNI_AVX
176   ctx->use_aesni_avx = 0;
177   if ((_gcry_get_hw_features () & HWF_INTEL_AESNI) &&
178       (_gcry_get_hw_features () & HWF_INTEL_AVX))
179     {
180       ctx->use_aesni_avx = 1;
181     }
182 #endif
183
184 #ifdef USE_AESNI_AVX2
185   ctx->use_aesni_avx2 = 0;
186   if ((_gcry_get_hw_features () & HWF_INTEL_AESNI) &&
187       (_gcry_get_hw_features () & HWF_INTEL_AVX2))
188     {
189       ctx->use_aesni_avx2 = 1;
190     }
191 #endif
192
193   return 0;
194 }
195
196 #ifdef USE_ARM_ASM
197
198 /* Assembly implementations of CAST5. */
199 extern void _gcry_camellia_arm_encrypt_block(const KEY_TABLE_TYPE keyTable,
200                                                byte *outbuf, const byte *inbuf,
201                                                const int keybits);
202
203 extern void _gcry_camellia_arm_decrypt_block(const KEY_TABLE_TYPE keyTable,
204                                                byte *outbuf, const byte *inbuf,
205                                                const int keybits);
206
207 static void Camellia_EncryptBlock(const int keyBitLength,
208                                   const unsigned char *plaintext,
209                                   const KEY_TABLE_TYPE keyTable,
210                                   unsigned char *cipherText)
211 {
212   _gcry_camellia_arm_encrypt_block(keyTable, cipherText, plaintext,
213                                      keyBitLength);
214 }
215
216 static void Camellia_DecryptBlock(const int keyBitLength,
217                                   const unsigned char *cipherText,
218                                   const KEY_TABLE_TYPE keyTable,
219                                   unsigned char *plaintext)
220 {
221   _gcry_camellia_arm_decrypt_block(keyTable, plaintext, cipherText,
222                                      keyBitLength);
223 }
224
225 static unsigned int
226 camellia_encrypt(void *c, byte *outbuf, const byte *inbuf)
227 {
228   CAMELLIA_context *ctx = c;
229   Camellia_EncryptBlock(ctx->keybitlength,inbuf,ctx->keytable,outbuf);
230 #define CAMELLIA_encrypt_stack_burn_size (15*4)
231   return /*burn_stack*/ (CAMELLIA_encrypt_stack_burn_size);
232 }
233
234 static unsigned int
235 camellia_decrypt(void *c, byte *outbuf, const byte *inbuf)
236 {
237   CAMELLIA_context *ctx=c;
238   Camellia_DecryptBlock(ctx->keybitlength,inbuf,ctx->keytable,outbuf);
239 #define CAMELLIA_decrypt_stack_burn_size (15*4)
240   return /*burn_stack*/ (CAMELLIA_decrypt_stack_burn_size);
241 }
242
243 #else /*USE_ARM_ASM*/
244
245 static unsigned int
246 camellia_encrypt(void *c, byte *outbuf, const byte *inbuf)
247 {
248   CAMELLIA_context *ctx=c;
249
250   Camellia_EncryptBlock(ctx->keybitlength,inbuf,ctx->keytable,outbuf);
251
252 #define CAMELLIA_encrypt_stack_burn_size \
253   (sizeof(int)+2*sizeof(unsigned char *)+sizeof(void*/*KEY_TABLE_TYPE*/) \
254      +4*sizeof(u32)+4*sizeof(u32) \
255      +2*sizeof(u32*)+4*sizeof(u32) \
256      +2*2*sizeof(void*) /* Function calls.  */ \
257     )
258
259   return /*burn_stack*/ (CAMELLIA_encrypt_stack_burn_size);
260 }
261
262 static unsigned int
263 camellia_decrypt(void *c, byte *outbuf, const byte *inbuf)
264 {
265   CAMELLIA_context *ctx=c;
266
267   Camellia_DecryptBlock(ctx->keybitlength,inbuf,ctx->keytable,outbuf);
268
269 #define CAMELLIA_decrypt_stack_burn_size \
270     (sizeof(int)+2*sizeof(unsigned char *)+sizeof(void*/*KEY_TABLE_TYPE*/) \
271      +4*sizeof(u32)+4*sizeof(u32) \
272      +2*sizeof(u32*)+4*sizeof(u32) \
273      +2*2*sizeof(void*) /* Function calls.  */ \
274     )
275
276   return /*burn_stack*/ (CAMELLIA_decrypt_stack_burn_size);
277 }
278
279 #endif /*!USE_ARM_ASM*/
280
281 /* Bulk encryption of complete blocks in CTR mode.  This function is only
282    intended for the bulk encryption feature of cipher.c.  CTR is expected to be
283    of size CAMELLIA_BLOCK_SIZE. */
284 void
285 _gcry_camellia_ctr_enc(void *context, unsigned char *ctr,
286                        void *outbuf_arg, const void *inbuf_arg,
287                        unsigned int nblocks)
288 {
289   CAMELLIA_context *ctx = context;
290   unsigned char *outbuf = outbuf_arg;
291   const unsigned char *inbuf = inbuf_arg;
292   unsigned char tmpbuf[CAMELLIA_BLOCK_SIZE];
293   int burn_stack_depth = CAMELLIA_encrypt_stack_burn_size;
294   int i;
295
296 #ifdef USE_AESNI_AVX2
297   if (ctx->use_aesni_avx2)
298     {
299       int did_use_aesni_avx2 = 0;
300
301       /* Process data in 32 block chunks. */
302       while (nblocks >= 32)
303         {
304           _gcry_camellia_aesni_avx2_ctr_enc(ctx, outbuf, inbuf, ctr);
305
306           nblocks -= 32;
307           outbuf += 32 * CAMELLIA_BLOCK_SIZE;
308           inbuf  += 32 * CAMELLIA_BLOCK_SIZE;
309           did_use_aesni_avx2 = 1;
310         }
311
312       if (did_use_aesni_avx2)
313         {
314           int avx2_burn_stack_depth = 32 * CAMELLIA_BLOCK_SIZE + 16 +
315                                         2 * sizeof(void *);
316
317           if (burn_stack_depth < avx2_burn_stack_depth)
318             burn_stack_depth = avx2_burn_stack_depth;
319         }
320
321       /* Use generic code to handle smaller chunks... */
322       /* TODO: use caching instead? */
323     }
324 #endif
325
326 #ifdef USE_AESNI_AVX
327   if (ctx->use_aesni_avx)
328     {
329       int did_use_aesni_avx = 0;
330
331       /* Process data in 16 block chunks. */
332       while (nblocks >= 16)
333         {
334           _gcry_camellia_aesni_avx_ctr_enc(ctx, outbuf, inbuf, ctr);
335
336           nblocks -= 16;
337           outbuf += 16 * CAMELLIA_BLOCK_SIZE;
338           inbuf  += 16 * CAMELLIA_BLOCK_SIZE;
339           did_use_aesni_avx = 1;
340         }
341
342       if (did_use_aesni_avx)
343         {
344           if (burn_stack_depth < 16 * CAMELLIA_BLOCK_SIZE + 2 * sizeof(void *))
345             burn_stack_depth = 16 * CAMELLIA_BLOCK_SIZE + 2 * sizeof(void *);
346         }
347
348       /* Use generic code to handle smaller chunks... */
349       /* TODO: use caching instead? */
350     }
351 #endif
352
353   for ( ;nblocks; nblocks-- )
354     {
355       /* Encrypt the counter. */
356       Camellia_EncryptBlock(ctx->keybitlength, ctr, ctx->keytable, tmpbuf);
357       /* XOR the input with the encrypted counter and store in output.  */
358       buf_xor(outbuf, tmpbuf, inbuf, CAMELLIA_BLOCK_SIZE);
359       outbuf += CAMELLIA_BLOCK_SIZE;
360       inbuf  += CAMELLIA_BLOCK_SIZE;
361       /* Increment the counter.  */
362       for (i = CAMELLIA_BLOCK_SIZE; i > 0; i--)
363         {
364           ctr[i-1]++;
365           if (ctr[i-1])
366             break;
367         }
368     }
369
370   wipememory(tmpbuf, sizeof(tmpbuf));
371   _gcry_burn_stack(burn_stack_depth);
372 }
373
374 /* Bulk decryption of complete blocks in CBC mode.  This function is only
375    intended for the bulk encryption feature of cipher.c. */
376 void
377 _gcry_camellia_cbc_dec(void *context, unsigned char *iv,
378                        void *outbuf_arg, const void *inbuf_arg,
379                        unsigned int nblocks)
380 {
381   CAMELLIA_context *ctx = context;
382   unsigned char *outbuf = outbuf_arg;
383   const unsigned char *inbuf = inbuf_arg;
384   unsigned char savebuf[CAMELLIA_BLOCK_SIZE];
385   int burn_stack_depth = CAMELLIA_decrypt_stack_burn_size;
386
387 #ifdef USE_AESNI_AVX2
388   if (ctx->use_aesni_avx2)
389     {
390       int did_use_aesni_avx2 = 0;
391
392       /* Process data in 32 block chunks. */
393       while (nblocks >= 32)
394         {
395           _gcry_camellia_aesni_avx2_cbc_dec(ctx, outbuf, inbuf, iv);
396
397           nblocks -= 32;
398           outbuf += 32 * CAMELLIA_BLOCK_SIZE;
399           inbuf  += 32 * CAMELLIA_BLOCK_SIZE;
400           did_use_aesni_avx2 = 1;
401         }
402
403       if (did_use_aesni_avx2)
404         {
405           int avx2_burn_stack_depth = 32 * CAMELLIA_BLOCK_SIZE + 16 +
406                                         2 * sizeof(void *);
407
408           if (burn_stack_depth < avx2_burn_stack_depth)
409             burn_stack_depth = avx2_burn_stack_depth;
410         }
411
412       /* Use generic code to handle smaller chunks... */
413     }
414 #endif
415
416 #ifdef USE_AESNI_AVX
417   if (ctx->use_aesni_avx)
418     {
419       int did_use_aesni_avx = 0;
420
421       /* Process data in 16 block chunks. */
422       while (nblocks >= 16)
423         {
424           _gcry_camellia_aesni_avx_cbc_dec(ctx, outbuf, inbuf, iv);
425
426           nblocks -= 16;
427           outbuf += 16 * CAMELLIA_BLOCK_SIZE;
428           inbuf  += 16 * CAMELLIA_BLOCK_SIZE;
429           did_use_aesni_avx = 1;
430         }
431
432       if (did_use_aesni_avx)
433         {
434           if (burn_stack_depth < 16 * CAMELLIA_BLOCK_SIZE + 2 * sizeof(void *))
435             burn_stack_depth = 16 * CAMELLIA_BLOCK_SIZE + 2 * sizeof(void *);
436         }
437
438       /* Use generic code to handle smaller chunks... */
439     }
440 #endif
441
442   for ( ;nblocks; nblocks-- )
443     {
444       /* INBUF is needed later and it may be identical to OUTBUF, so store
445          the intermediate result to SAVEBUF.  */
446       Camellia_DecryptBlock(ctx->keybitlength, inbuf, ctx->keytable, savebuf);
447
448       buf_xor_n_copy_2(outbuf, savebuf, iv, inbuf, CAMELLIA_BLOCK_SIZE);
449       inbuf += CAMELLIA_BLOCK_SIZE;
450       outbuf += CAMELLIA_BLOCK_SIZE;
451     }
452
453   wipememory(savebuf, sizeof(savebuf));
454   _gcry_burn_stack(burn_stack_depth);
455 }
456
457 /* Bulk decryption of complete blocks in CFB mode.  This function is only
458    intended for the bulk encryption feature of cipher.c. */
459 void
460 _gcry_camellia_cfb_dec(void *context, unsigned char *iv,
461                        void *outbuf_arg, const void *inbuf_arg,
462                        unsigned int nblocks)
463 {
464   CAMELLIA_context *ctx = context;
465   unsigned char *outbuf = outbuf_arg;
466   const unsigned char *inbuf = inbuf_arg;
467   int burn_stack_depth = CAMELLIA_decrypt_stack_burn_size;
468
469 #ifdef USE_AESNI_AVX2
470   if (ctx->use_aesni_avx2)
471     {
472       int did_use_aesni_avx2 = 0;
473
474       /* Process data in 32 block chunks. */
475       while (nblocks >= 32)
476         {
477           _gcry_camellia_aesni_avx2_cfb_dec(ctx, outbuf, inbuf, iv);
478
479           nblocks -= 32;
480           outbuf += 32 * CAMELLIA_BLOCK_SIZE;
481           inbuf  += 32 * CAMELLIA_BLOCK_SIZE;
482           did_use_aesni_avx2 = 1;
483         }
484
485       if (did_use_aesni_avx2)
486         {
487           int avx2_burn_stack_depth = 32 * CAMELLIA_BLOCK_SIZE + 16 +
488                                         2 * sizeof(void *);
489
490           if (burn_stack_depth < avx2_burn_stack_depth)
491             burn_stack_depth = avx2_burn_stack_depth;
492         }
493
494       /* Use generic code to handle smaller chunks... */
495     }
496 #endif
497
498 #ifdef USE_AESNI_AVX
499   if (ctx->use_aesni_avx)
500     {
501       int did_use_aesni_avx = 0;
502
503       /* Process data in 16 block chunks. */
504       while (nblocks >= 16)
505         {
506           _gcry_camellia_aesni_avx_cfb_dec(ctx, outbuf, inbuf, iv);
507
508           nblocks -= 16;
509           outbuf += 16 * CAMELLIA_BLOCK_SIZE;
510           inbuf  += 16 * CAMELLIA_BLOCK_SIZE;
511           did_use_aesni_avx = 1;
512         }
513
514       if (did_use_aesni_avx)
515         {
516           if (burn_stack_depth < 16 * CAMELLIA_BLOCK_SIZE + 2 * sizeof(void *))
517             burn_stack_depth = 16 * CAMELLIA_BLOCK_SIZE + 2 * sizeof(void *);
518         }
519
520       /* Use generic code to handle smaller chunks... */
521     }
522 #endif
523
524   for ( ;nblocks; nblocks-- )
525     {
526       Camellia_EncryptBlock(ctx->keybitlength, iv, ctx->keytable, iv);
527       buf_xor_n_copy(outbuf, iv, inbuf, CAMELLIA_BLOCK_SIZE);
528       outbuf += CAMELLIA_BLOCK_SIZE;
529       inbuf  += CAMELLIA_BLOCK_SIZE;
530     }
531
532   _gcry_burn_stack(burn_stack_depth);
533 }
534
535 /* Run the self-tests for CAMELLIA-CTR-128, tests IV increment of bulk CTR
536    encryption.  Returns NULL on success. */
537 static const char*
538 selftest_ctr_128 (void)
539 {
540   const int nblocks = 32+16+1;
541   const int blocksize = CAMELLIA_BLOCK_SIZE;
542   const int context_size = sizeof(CAMELLIA_context);
543
544   return _gcry_selftest_helper_ctr("CAMELLIA", &camellia_setkey,
545            &camellia_encrypt, &_gcry_camellia_ctr_enc, nblocks, blocksize,
546            context_size);
547 }
548
549 /* Run the self-tests for CAMELLIA-CBC-128, tests bulk CBC decryption.
550    Returns NULL on success. */
551 static const char*
552 selftest_cbc_128 (void)
553 {
554   const int nblocks = 32+16+2;
555   const int blocksize = CAMELLIA_BLOCK_SIZE;
556   const int context_size = sizeof(CAMELLIA_context);
557
558   return _gcry_selftest_helper_cbc("CAMELLIA", &camellia_setkey,
559            &camellia_encrypt, &_gcry_camellia_cbc_dec, nblocks, blocksize,
560            context_size);
561 }
562
563 /* Run the self-tests for CAMELLIA-CFB-128, tests bulk CFB decryption.
564    Returns NULL on success. */
565 static const char*
566 selftest_cfb_128 (void)
567 {
568   const int nblocks = 32+16+2;
569   const int blocksize = CAMELLIA_BLOCK_SIZE;
570   const int context_size = sizeof(CAMELLIA_context);
571
572   return _gcry_selftest_helper_cfb("CAMELLIA", &camellia_setkey,
573            &camellia_encrypt, &_gcry_camellia_cfb_dec, nblocks, blocksize,
574            context_size);
575 }
576
577 static const char *
578 selftest(void)
579 {
580   CAMELLIA_context ctx;
581   byte scratch[16];
582   const char *r;
583
584   /* These test vectors are from RFC-3713 */
585   static const byte plaintext[]=
586     {
587       0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
588       0xfe,0xdc,0xba,0x98,0x76,0x54,0x32,0x10
589     };
590   static const byte key_128[]=
591     {
592       0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
593       0xfe,0xdc,0xba,0x98,0x76,0x54,0x32,0x10
594     };
595   static const byte ciphertext_128[]=
596     {
597       0x67,0x67,0x31,0x38,0x54,0x96,0x69,0x73,
598       0x08,0x57,0x06,0x56,0x48,0xea,0xbe,0x43
599     };
600   static const byte key_192[]=
601     {
602       0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,0xfe,0xdc,0xba,0x98,
603       0x76,0x54,0x32,0x10,0x00,0x11,0x22,0x33,0x44,0x55,0x66,0x77
604     };
605   static const byte ciphertext_192[]=
606     {
607       0xb4,0x99,0x34,0x01,0xb3,0xe9,0x96,0xf8,
608       0x4e,0xe5,0xce,0xe7,0xd7,0x9b,0x09,0xb9
609     };
610   static const byte key_256[]=
611     {
612       0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,0xfe,0xdc,0xba,
613       0x98,0x76,0x54,0x32,0x10,0x00,0x11,0x22,0x33,0x44,0x55,
614       0x66,0x77,0x88,0x99,0xaa,0xbb,0xcc,0xdd,0xee,0xff
615     };
616   static const byte ciphertext_256[]=
617     {
618       0x9a,0xcc,0x23,0x7d,0xff,0x16,0xd7,0x6c,
619       0x20,0xef,0x7c,0x91,0x9e,0x3a,0x75,0x09
620     };
621
622   camellia_setkey(&ctx,key_128,sizeof(key_128));
623   camellia_encrypt(&ctx,scratch,plaintext);
624   if(memcmp(scratch,ciphertext_128,sizeof(ciphertext_128))!=0)
625     return "CAMELLIA-128 test encryption failed.";
626   camellia_decrypt(&ctx,scratch,scratch);
627   if(memcmp(scratch,plaintext,sizeof(plaintext))!=0)
628     return "CAMELLIA-128 test decryption failed.";
629
630   camellia_setkey(&ctx,key_192,sizeof(key_192));
631   camellia_encrypt(&ctx,scratch,plaintext);
632   if(memcmp(scratch,ciphertext_192,sizeof(ciphertext_192))!=0)
633     return "CAMELLIA-192 test encryption failed.";
634   camellia_decrypt(&ctx,scratch,scratch);
635   if(memcmp(scratch,plaintext,sizeof(plaintext))!=0)
636     return "CAMELLIA-192 test decryption failed.";
637
638   camellia_setkey(&ctx,key_256,sizeof(key_256));
639   camellia_encrypt(&ctx,scratch,plaintext);
640   if(memcmp(scratch,ciphertext_256,sizeof(ciphertext_256))!=0)
641     return "CAMELLIA-256 test encryption failed.";
642   camellia_decrypt(&ctx,scratch,scratch);
643   if(memcmp(scratch,plaintext,sizeof(plaintext))!=0)
644     return "CAMELLIA-256 test decryption failed.";
645
646   if ( (r = selftest_ctr_128 ()) )
647     return r;
648
649   if ( (r = selftest_cbc_128 ()) )
650     return r;
651
652   if ( (r = selftest_cfb_128 ()) )
653     return r;
654
655   return NULL;
656 }
657
658 /* These oids are from
659    <http://info.isl.ntt.co.jp/crypt/eng/camellia/specifications_oid.html>,
660    retrieved May 1, 2007. */
661
662 static gcry_cipher_oid_spec_t camellia128_oids[] =
663   {
664     {"1.2.392.200011.61.1.1.1.2", GCRY_CIPHER_MODE_CBC},
665     {"0.3.4401.5.3.1.9.1", GCRY_CIPHER_MODE_ECB},
666     {"0.3.4401.5.3.1.9.3", GCRY_CIPHER_MODE_OFB},
667     {"0.3.4401.5.3.1.9.4", GCRY_CIPHER_MODE_CFB},
668     { NULL }
669   };
670
671 static gcry_cipher_oid_spec_t camellia192_oids[] =
672   {
673     {"1.2.392.200011.61.1.1.1.3", GCRY_CIPHER_MODE_CBC},
674     {"0.3.4401.5.3.1.9.21", GCRY_CIPHER_MODE_ECB},
675     {"0.3.4401.5.3.1.9.23", GCRY_CIPHER_MODE_OFB},
676     {"0.3.4401.5.3.1.9.24", GCRY_CIPHER_MODE_CFB},
677     { NULL }
678   };
679
680 static gcry_cipher_oid_spec_t camellia256_oids[] =
681   {
682     {"1.2.392.200011.61.1.1.1.4", GCRY_CIPHER_MODE_CBC},
683     {"0.3.4401.5.3.1.9.41", GCRY_CIPHER_MODE_ECB},
684     {"0.3.4401.5.3.1.9.43", GCRY_CIPHER_MODE_OFB},
685     {"0.3.4401.5.3.1.9.44", GCRY_CIPHER_MODE_CFB},
686     { NULL }
687   };
688
689 gcry_cipher_spec_t _gcry_cipher_spec_camellia128 =
690   {
691     GCRY_CIPHER_CAMELLIA128, {0, 0},
692     "CAMELLIA128",NULL,camellia128_oids,CAMELLIA_BLOCK_SIZE,128,
693     sizeof(CAMELLIA_context),camellia_setkey,camellia_encrypt,camellia_decrypt
694   };
695
696 gcry_cipher_spec_t _gcry_cipher_spec_camellia192 =
697   {
698     GCRY_CIPHER_CAMELLIA192, {0, 0},
699     "CAMELLIA192",NULL,camellia192_oids,CAMELLIA_BLOCK_SIZE,192,
700     sizeof(CAMELLIA_context),camellia_setkey,camellia_encrypt,camellia_decrypt
701   };
702
703 gcry_cipher_spec_t _gcry_cipher_spec_camellia256 =
704   {
705     GCRY_CIPHER_CAMELLIA256, {0, 0},
706     "CAMELLIA256",NULL,camellia256_oids,CAMELLIA_BLOCK_SIZE,256,
707     sizeof(CAMELLIA_context),camellia_setkey,camellia_encrypt,camellia_decrypt
708   };