Check if assembler is compatible with AMD64 assembly implementations
[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 static void
197 camellia_encrypt(void *c, byte *outbuf, const byte *inbuf)
198 {
199   CAMELLIA_context *ctx=c;
200
201   Camellia_EncryptBlock(ctx->keybitlength,inbuf,ctx->keytable,outbuf);
202
203 #define CAMELLIA_encrypt_stack_burn_size \
204   (sizeof(int)+2*sizeof(unsigned char *)+sizeof(void*/*KEY_TABLE_TYPE*/) \
205      +4*sizeof(u32)+4*sizeof(u32) \
206      +2*sizeof(u32*)+4*sizeof(u32) \
207      +2*2*sizeof(void*) /* Function calls.  */ \
208     )
209
210   _gcry_burn_stack(CAMELLIA_encrypt_stack_burn_size);
211 }
212
213 static void
214 camellia_decrypt(void *c, byte *outbuf, const byte *inbuf)
215 {
216   CAMELLIA_context *ctx=c;
217
218   Camellia_DecryptBlock(ctx->keybitlength,inbuf,ctx->keytable,outbuf);
219
220 #define CAMELLIA_decrypt_stack_burn_size \
221     (sizeof(int)+2*sizeof(unsigned char *)+sizeof(void*/*KEY_TABLE_TYPE*/) \
222      +4*sizeof(u32)+4*sizeof(u32) \
223      +2*sizeof(u32*)+4*sizeof(u32) \
224      +2*2*sizeof(void*) /* Function calls.  */ \
225     )
226
227   _gcry_burn_stack(CAMELLIA_decrypt_stack_burn_size);
228 }
229
230 /* Bulk encryption of complete blocks in CTR mode.  This function is only
231    intended for the bulk encryption feature of cipher.c.  CTR is expected to be
232    of size CAMELLIA_BLOCK_SIZE. */
233 void
234 _gcry_camellia_ctr_enc(void *context, unsigned char *ctr,
235                        void *outbuf_arg, const void *inbuf_arg,
236                        unsigned int nblocks)
237 {
238   CAMELLIA_context *ctx = context;
239   unsigned char *outbuf = outbuf_arg;
240   const unsigned char *inbuf = inbuf_arg;
241   unsigned char tmpbuf[CAMELLIA_BLOCK_SIZE];
242   int burn_stack_depth = CAMELLIA_encrypt_stack_burn_size;
243   int i;
244
245 #ifdef USE_AESNI_AVX2
246   if (ctx->use_aesni_avx2)
247     {
248       int did_use_aesni_avx2 = 0;
249
250       /* Process data in 32 block chunks. */
251       while (nblocks >= 32)
252         {
253           _gcry_camellia_aesni_avx2_ctr_enc(ctx, outbuf, inbuf, ctr);
254
255           nblocks -= 32;
256           outbuf += 32 * CAMELLIA_BLOCK_SIZE;
257           inbuf  += 32 * CAMELLIA_BLOCK_SIZE;
258           did_use_aesni_avx2 = 1;
259         }
260
261       if (did_use_aesni_avx2)
262         {
263           int avx2_burn_stack_depth = 32 * CAMELLIA_BLOCK_SIZE + 16 +
264                                         2 * sizeof(void *);
265
266           /* clear AVX registers */
267           asm volatile ("vzeroall;\n":::);
268
269           if (burn_stack_depth < avx2_burn_stack_depth)
270             burn_stack_depth = avx2_burn_stack_depth;
271         }
272
273       /* Use generic code to handle smaller chunks... */
274       /* TODO: use caching instead? */
275     }
276 #endif
277
278 #ifdef USE_AESNI_AVX
279   if (ctx->use_aesni_avx)
280     {
281       int did_use_aesni_avx = 0;
282
283       /* Process data in 16 block chunks. */
284       while (nblocks >= 16)
285         {
286           _gcry_camellia_aesni_avx_ctr_enc(ctx, outbuf, inbuf, ctr);
287
288           nblocks -= 16;
289           outbuf += 16 * CAMELLIA_BLOCK_SIZE;
290           inbuf  += 16 * CAMELLIA_BLOCK_SIZE;
291           did_use_aesni_avx = 1;
292         }
293
294       if (did_use_aesni_avx)
295         {
296           /* clear AVX registers */
297           asm volatile ("vzeroall;\n":::);
298
299           if (burn_stack_depth < 16 * CAMELLIA_BLOCK_SIZE + 2 * sizeof(void *))
300             burn_stack_depth = 16 * CAMELLIA_BLOCK_SIZE + 2 * sizeof(void *);
301         }
302
303       /* Use generic code to handle smaller chunks... */
304       /* TODO: use caching instead? */
305     }
306 #endif
307
308   for ( ;nblocks; nblocks-- )
309     {
310       /* Encrypt the counter. */
311       Camellia_EncryptBlock(ctx->keybitlength, ctr, ctx->keytable, tmpbuf);
312       /* XOR the input with the encrypted counter and store in output.  */
313       buf_xor(outbuf, tmpbuf, inbuf, CAMELLIA_BLOCK_SIZE);
314       outbuf += CAMELLIA_BLOCK_SIZE;
315       inbuf  += CAMELLIA_BLOCK_SIZE;
316       /* Increment the counter.  */
317       for (i = CAMELLIA_BLOCK_SIZE; i > 0; i--)
318         {
319           ctr[i-1]++;
320           if (ctr[i-1])
321             break;
322         }
323     }
324
325   wipememory(tmpbuf, sizeof(tmpbuf));
326   _gcry_burn_stack(burn_stack_depth);
327 }
328
329 /* Bulk decryption of complete blocks in CBC mode.  This function is only
330    intended for the bulk encryption feature of cipher.c. */
331 void
332 _gcry_camellia_cbc_dec(void *context, unsigned char *iv,
333                        void *outbuf_arg, const void *inbuf_arg,
334                        unsigned int nblocks)
335 {
336   CAMELLIA_context *ctx = context;
337   unsigned char *outbuf = outbuf_arg;
338   const unsigned char *inbuf = inbuf_arg;
339   unsigned char savebuf[CAMELLIA_BLOCK_SIZE];
340   int burn_stack_depth = CAMELLIA_decrypt_stack_burn_size;
341
342 #ifdef USE_AESNI_AVX2
343   if (ctx->use_aesni_avx2)
344     {
345       int did_use_aesni_avx2 = 0;
346
347       /* Process data in 32 block chunks. */
348       while (nblocks >= 32)
349         {
350           _gcry_camellia_aesni_avx2_cbc_dec(ctx, outbuf, inbuf, iv);
351
352           nblocks -= 32;
353           outbuf += 32 * CAMELLIA_BLOCK_SIZE;
354           inbuf  += 32 * CAMELLIA_BLOCK_SIZE;
355           did_use_aesni_avx2 = 1;
356         }
357
358       if (did_use_aesni_avx2)
359         {
360           int avx2_burn_stack_depth = 32 * CAMELLIA_BLOCK_SIZE + 16 +
361                                         2 * sizeof(void *);
362
363           /* clear AVX registers */
364           asm volatile ("vzeroall;\n":::);
365
366           if (burn_stack_depth < avx2_burn_stack_depth)
367             burn_stack_depth = avx2_burn_stack_depth;
368         }
369
370       /* Use generic code to handle smaller chunks... */
371     }
372 #endif
373
374 #ifdef USE_AESNI_AVX
375   if (ctx->use_aesni_avx)
376     {
377       int did_use_aesni_avx = 0;
378
379       /* Process data in 16 block chunks. */
380       while (nblocks >= 16)
381         {
382           _gcry_camellia_aesni_avx_cbc_dec(ctx, outbuf, inbuf, iv);
383
384           nblocks -= 16;
385           outbuf += 16 * CAMELLIA_BLOCK_SIZE;
386           inbuf  += 16 * CAMELLIA_BLOCK_SIZE;
387           did_use_aesni_avx = 1;
388         }
389
390       if (did_use_aesni_avx)
391         {
392           /* clear AVX registers */
393           asm volatile ("vzeroall;\n":::);
394
395           if (burn_stack_depth < 16 * CAMELLIA_BLOCK_SIZE + 2 * sizeof(void *))
396             burn_stack_depth = 16 * CAMELLIA_BLOCK_SIZE + 2 * sizeof(void *);
397         }
398
399       /* Use generic code to handle smaller chunks... */
400     }
401 #endif
402
403   for ( ;nblocks; nblocks-- )
404     {
405       /* We need to save INBUF away because it may be identical to
406          OUTBUF.  */
407       memcpy(savebuf, inbuf, CAMELLIA_BLOCK_SIZE);
408
409       Camellia_DecryptBlock(ctx->keybitlength, inbuf, ctx->keytable, outbuf);
410
411       buf_xor(outbuf, outbuf, iv, CAMELLIA_BLOCK_SIZE);
412       memcpy(iv, savebuf, CAMELLIA_BLOCK_SIZE);
413       inbuf += CAMELLIA_BLOCK_SIZE;
414       outbuf += CAMELLIA_BLOCK_SIZE;
415     }
416
417   wipememory(savebuf, sizeof(savebuf));
418   _gcry_burn_stack(burn_stack_depth);
419 }
420
421 /* Bulk decryption of complete blocks in CFB mode.  This function is only
422    intended for the bulk encryption feature of cipher.c. */
423 void
424 _gcry_camellia_cfb_dec(void *context, unsigned char *iv,
425                        void *outbuf_arg, const void *inbuf_arg,
426                        unsigned int nblocks)
427 {
428   CAMELLIA_context *ctx = context;
429   unsigned char *outbuf = outbuf_arg;
430   const unsigned char *inbuf = inbuf_arg;
431   int burn_stack_depth = CAMELLIA_decrypt_stack_burn_size;
432
433 #ifdef USE_AESNI_AVX2
434   if (ctx->use_aesni_avx2)
435     {
436       int did_use_aesni_avx2 = 0;
437
438       /* Process data in 32 block chunks. */
439       while (nblocks >= 32)
440         {
441           _gcry_camellia_aesni_avx2_cfb_dec(ctx, outbuf, inbuf, iv);
442
443           nblocks -= 32;
444           outbuf += 32 * CAMELLIA_BLOCK_SIZE;
445           inbuf  += 32 * CAMELLIA_BLOCK_SIZE;
446           did_use_aesni_avx2 = 1;
447         }
448
449       if (did_use_aesni_avx2)
450         {
451           int avx2_burn_stack_depth = 32 * CAMELLIA_BLOCK_SIZE + 16 +
452                                         2 * sizeof(void *);
453
454           /* clear AVX registers */
455           asm volatile ("vzeroall;\n":::);
456
457           if (burn_stack_depth < avx2_burn_stack_depth)
458             burn_stack_depth = avx2_burn_stack_depth;
459         }
460
461       /* Use generic code to handle smaller chunks... */
462     }
463 #endif
464
465 #ifdef USE_AESNI_AVX
466   if (ctx->use_aesni_avx)
467     {
468       int did_use_aesni_avx = 0;
469
470       /* Process data in 16 block chunks. */
471       while (nblocks >= 16)
472         {
473           _gcry_camellia_aesni_avx_cfb_dec(ctx, outbuf, inbuf, iv);
474
475           nblocks -= 16;
476           outbuf += 16 * CAMELLIA_BLOCK_SIZE;
477           inbuf  += 16 * CAMELLIA_BLOCK_SIZE;
478           did_use_aesni_avx = 1;
479         }
480
481       if (did_use_aesni_avx)
482         {
483           /* clear AVX registers */
484           asm volatile ("vzeroall;\n":::);
485
486           if (burn_stack_depth < 16 * CAMELLIA_BLOCK_SIZE + 2 * sizeof(void *))
487             burn_stack_depth = 16 * CAMELLIA_BLOCK_SIZE + 2 * sizeof(void *);
488         }
489
490       /* Use generic code to handle smaller chunks... */
491     }
492 #endif
493
494   for ( ;nblocks; nblocks-- )
495     {
496       Camellia_EncryptBlock(ctx->keybitlength, iv, ctx->keytable, iv);
497       buf_xor_n_copy(outbuf, iv, inbuf, CAMELLIA_BLOCK_SIZE);
498       outbuf += CAMELLIA_BLOCK_SIZE;
499       inbuf  += CAMELLIA_BLOCK_SIZE;
500     }
501
502   _gcry_burn_stack(burn_stack_depth);
503 }
504
505 /* Run the self-tests for CAMELLIA-CTR-128, tests IV increment of bulk CTR
506    encryption.  Returns NULL on success. */
507 static const char*
508 selftest_ctr_128 (void)
509 {
510   const int nblocks = 32+1;
511   const int blocksize = CAMELLIA_BLOCK_SIZE;
512   const int context_size = sizeof(CAMELLIA_context);
513
514   return _gcry_selftest_helper_ctr("CAMELLIA", &camellia_setkey,
515            &camellia_encrypt, &_gcry_camellia_ctr_enc, nblocks, blocksize,
516            context_size);
517 }
518
519 /* Run the self-tests for CAMELLIA-CBC-128, tests bulk CBC decryption.
520    Returns NULL on success. */
521 static const char*
522 selftest_cbc_128 (void)
523 {
524   const int nblocks = 32+2;
525   const int blocksize = CAMELLIA_BLOCK_SIZE;
526   const int context_size = sizeof(CAMELLIA_context);
527
528   return _gcry_selftest_helper_cbc("CAMELLIA", &camellia_setkey,
529            &camellia_encrypt, &_gcry_camellia_cbc_dec, nblocks, blocksize,
530            context_size);
531 }
532
533 /* Run the self-tests for CAMELLIA-CFB-128, tests bulk CFB decryption.
534    Returns NULL on success. */
535 static const char*
536 selftest_cfb_128 (void)
537 {
538   const int nblocks = 32+2;
539   const int blocksize = CAMELLIA_BLOCK_SIZE;
540   const int context_size = sizeof(CAMELLIA_context);
541
542   return _gcry_selftest_helper_cfb("CAMELLIA", &camellia_setkey,
543            &camellia_encrypt, &_gcry_camellia_cfb_dec, nblocks, blocksize,
544            context_size);
545 }
546
547 static const char *
548 selftest(void)
549 {
550   CAMELLIA_context ctx;
551   byte scratch[16];
552   const char *r;
553
554   /* These test vectors are from RFC-3713 */
555   const byte plaintext[]=
556     {
557       0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
558       0xfe,0xdc,0xba,0x98,0x76,0x54,0x32,0x10
559     };
560   const byte key_128[]=
561     {
562       0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
563       0xfe,0xdc,0xba,0x98,0x76,0x54,0x32,0x10
564     };
565   const byte ciphertext_128[]=
566     {
567       0x67,0x67,0x31,0x38,0x54,0x96,0x69,0x73,
568       0x08,0x57,0x06,0x56,0x48,0xea,0xbe,0x43
569     };
570   const byte key_192[]=
571     {
572       0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,0xfe,0xdc,0xba,0x98,
573       0x76,0x54,0x32,0x10,0x00,0x11,0x22,0x33,0x44,0x55,0x66,0x77
574     };
575   const byte ciphertext_192[]=
576     {
577       0xb4,0x99,0x34,0x01,0xb3,0xe9,0x96,0xf8,
578       0x4e,0xe5,0xce,0xe7,0xd7,0x9b,0x09,0xb9
579     };
580   const byte key_256[]=
581     {
582       0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,0xfe,0xdc,0xba,
583       0x98,0x76,0x54,0x32,0x10,0x00,0x11,0x22,0x33,0x44,0x55,
584       0x66,0x77,0x88,0x99,0xaa,0xbb,0xcc,0xdd,0xee,0xff
585     };
586   const byte ciphertext_256[]=
587     {
588       0x9a,0xcc,0x23,0x7d,0xff,0x16,0xd7,0x6c,
589       0x20,0xef,0x7c,0x91,0x9e,0x3a,0x75,0x09
590     };
591
592   camellia_setkey(&ctx,key_128,sizeof(key_128));
593   camellia_encrypt(&ctx,scratch,plaintext);
594   if(memcmp(scratch,ciphertext_128,sizeof(ciphertext_128))!=0)
595     return "CAMELLIA-128 test encryption failed.";
596   camellia_decrypt(&ctx,scratch,scratch);
597   if(memcmp(scratch,plaintext,sizeof(plaintext))!=0)
598     return "CAMELLIA-128 test decryption failed.";
599
600   camellia_setkey(&ctx,key_192,sizeof(key_192));
601   camellia_encrypt(&ctx,scratch,plaintext);
602   if(memcmp(scratch,ciphertext_192,sizeof(ciphertext_192))!=0)
603     return "CAMELLIA-192 test encryption failed.";
604   camellia_decrypt(&ctx,scratch,scratch);
605   if(memcmp(scratch,plaintext,sizeof(plaintext))!=0)
606     return "CAMELLIA-192 test decryption failed.";
607
608   camellia_setkey(&ctx,key_256,sizeof(key_256));
609   camellia_encrypt(&ctx,scratch,plaintext);
610   if(memcmp(scratch,ciphertext_256,sizeof(ciphertext_256))!=0)
611     return "CAMELLIA-256 test encryption failed.";
612   camellia_decrypt(&ctx,scratch,scratch);
613   if(memcmp(scratch,plaintext,sizeof(plaintext))!=0)
614     return "CAMELLIA-256 test decryption failed.";
615
616   if ( (r = selftest_ctr_128 ()) )
617     return r;
618
619   if ( (r = selftest_cbc_128 ()) )
620     return r;
621
622   if ( (r = selftest_cfb_128 ()) )
623     return r;
624
625   return NULL;
626 }
627
628 /* These oids are from
629    <http://info.isl.ntt.co.jp/crypt/eng/camellia/specifications_oid.html>,
630    retrieved May 1, 2007. */
631
632 static gcry_cipher_oid_spec_t camellia128_oids[] =
633   {
634     {"1.2.392.200011.61.1.1.1.2", GCRY_CIPHER_MODE_CBC},
635     {"0.3.4401.5.3.1.9.1", GCRY_CIPHER_MODE_ECB},
636     {"0.3.4401.5.3.1.9.3", GCRY_CIPHER_MODE_OFB},
637     {"0.3.4401.5.3.1.9.4", GCRY_CIPHER_MODE_CFB},
638     { NULL }
639   };
640
641 static gcry_cipher_oid_spec_t camellia192_oids[] =
642   {
643     {"1.2.392.200011.61.1.1.1.3", GCRY_CIPHER_MODE_CBC},
644     {"0.3.4401.5.3.1.9.21", GCRY_CIPHER_MODE_ECB},
645     {"0.3.4401.5.3.1.9.23", GCRY_CIPHER_MODE_OFB},
646     {"0.3.4401.5.3.1.9.24", GCRY_CIPHER_MODE_CFB},
647     { NULL }
648   };
649
650 static gcry_cipher_oid_spec_t camellia256_oids[] =
651   {
652     {"1.2.392.200011.61.1.1.1.4", GCRY_CIPHER_MODE_CBC},
653     {"0.3.4401.5.3.1.9.41", GCRY_CIPHER_MODE_ECB},
654     {"0.3.4401.5.3.1.9.43", GCRY_CIPHER_MODE_OFB},
655     {"0.3.4401.5.3.1.9.44", GCRY_CIPHER_MODE_CFB},
656     { NULL }
657   };
658
659 gcry_cipher_spec_t _gcry_cipher_spec_camellia128 =
660   {
661     "CAMELLIA128",NULL,camellia128_oids,CAMELLIA_BLOCK_SIZE,128,
662     sizeof(CAMELLIA_context),camellia_setkey,camellia_encrypt,camellia_decrypt
663   };
664
665 gcry_cipher_spec_t _gcry_cipher_spec_camellia192 =
666   {
667     "CAMELLIA192",NULL,camellia192_oids,CAMELLIA_BLOCK_SIZE,192,
668     sizeof(CAMELLIA_context),camellia_setkey,camellia_encrypt,camellia_decrypt
669   };
670
671 gcry_cipher_spec_t _gcry_cipher_spec_camellia256 =
672   {
673     "CAMELLIA256",NULL,camellia256_oids,CAMELLIA_BLOCK_SIZE,256,
674     sizeof(CAMELLIA_context),camellia_setkey,camellia_encrypt,camellia_decrypt
675   };