camellia-glue.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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. /* I put all the libgcrypt-specific stuff in this file to keep the
  22. camellia.c/camellia.h files exactly as provided by NTT. If they
  23. update their code, this should make it easier to bring the changes
  24. in. - dshaw
  25. There is one small change which needs to be done: Include the
  26. following code at the top of camellia.h: */
  27. #if 0
  28. /* To use Camellia with libraries it is often useful to keep the name
  29. * space of the library clean. The following macro is thus useful:
  30. *
  31. * #define CAMELLIA_EXT_SYM_PREFIX foo_
  32. *
  33. * This prefixes all external symbols with "foo_".
  34. */
  35. #ifdef HAVE_CONFIG_H
  36. #include <config.h>
  37. #endif
  38. #ifdef CAMELLIA_EXT_SYM_PREFIX
  39. #define CAMELLIA_PREFIX1(x,y) x ## y
  40. #define CAMELLIA_PREFIX2(x,y) CAMELLIA_PREFIX1(x,y)
  41. #define CAMELLIA_PREFIX(x) CAMELLIA_PREFIX2(CAMELLIA_EXT_SYM_PREFIX,x)
  42. #define Camellia_Ekeygen CAMELLIA_PREFIX(Camellia_Ekeygen)
  43. #define Camellia_EncryptBlock CAMELLIA_PREFIX(Camellia_EncryptBlock)
  44. #define Camellia_DecryptBlock CAMELLIA_PREFIX(Camellia_DecryptBlock)
  45. #define camellia_decrypt128 CAMELLIA_PREFIX(camellia_decrypt128)
  46. #define camellia_decrypt256 CAMELLIA_PREFIX(camellia_decrypt256)
  47. #define camellia_encrypt128 CAMELLIA_PREFIX(camellia_encrypt128)
  48. #define camellia_encrypt256 CAMELLIA_PREFIX(camellia_encrypt256)
  49. #define camellia_setup128 CAMELLIA_PREFIX(camellia_setup128)
  50. #define camellia_setup192 CAMELLIA_PREFIX(camellia_setup192)
  51. #define camellia_setup256 CAMELLIA_PREFIX(camellia_setup256)
  52. #endif /*CAMELLIA_EXT_SYM_PREFIX*/
  53. #endif /* Code sample. */
  54. #include <config.h>
  55. #include "types.h"
  56. #include "g10lib.h"
  57. #include "cipher.h"
  58. #include "camellia.h"
  59. typedef struct
  60. {
  61. int keybitlength;
  62. KEY_TABLE_TYPE keytable;
  63. } CAMELLIA_context;
  64. static const char *selftest(void);
  65. static gcry_err_code_t
  66. camellia_setkey(void *c, const byte *key, unsigned keylen)
  67. {
  68. CAMELLIA_context *ctx=c;
  69. static int initialized=0;
  70. static const char *selftest_failed=NULL;
  71. if(keylen!=16 && keylen!=24 && keylen!=32)
  72. return GPG_ERR_INV_KEYLEN;
  73. if(!initialized)
  74. {
  75. initialized=1;
  76. selftest_failed=selftest();
  77. if(selftest_failed)
  78. log_error("%s\n",selftest_failed);
  79. }
  80. if(selftest_failed)
  81. return GPG_ERR_SELFTEST_FAILED;
  82. ctx->keybitlength=keylen*8;
  83. Camellia_Ekeygen(ctx->keybitlength,key,ctx->keytable);
  84. _gcry_burn_stack
  85. ((19+34+34)*sizeof(u32)+2*sizeof(void*) /* camellia_setup256 */
  86. +(4+32)*sizeof(u32)+2*sizeof(void*) /* camellia_setup192 */
  87. +0+sizeof(int)+2*sizeof(void*) /* Camellia_Ekeygen */
  88. +3*2*sizeof(void*) /* Function calls. */
  89. );
  90. return 0;
  91. }
  92. static void
  93. camellia_encrypt(void *c, byte *outbuf, const byte *inbuf)
  94. {
  95. CAMELLIA_context *ctx=c;
  96. Camellia_EncryptBlock(ctx->keybitlength,inbuf,ctx->keytable,outbuf);
  97. _gcry_burn_stack
  98. (sizeof(int)+2*sizeof(unsigned char *)+sizeof(KEY_TABLE_TYPE)
  99. +4*sizeof(u32)
  100. +2*sizeof(u32*)+4*sizeof(u32)
  101. +2*2*sizeof(void*) /* Function calls. */
  102. );
  103. }
  104. static void
  105. camellia_decrypt(void *c, byte *outbuf, const byte *inbuf)
  106. {
  107. CAMELLIA_context *ctx=c;
  108. Camellia_DecryptBlock(ctx->keybitlength,inbuf,ctx->keytable,outbuf);
  109. _gcry_burn_stack
  110. (sizeof(int)+2*sizeof(unsigned char *)+sizeof(KEY_TABLE_TYPE)
  111. +4*sizeof(u32)
  112. +2*sizeof(u32*)+4*sizeof(u32)
  113. +2*2*sizeof(void*) /* Function calls. */
  114. );
  115. }
  116. static const char *
  117. selftest(void)
  118. {
  119. CAMELLIA_context ctx;
  120. byte scratch[16];
  121. /* These test vectors are from RFC-3713 */
  122. const byte plaintext[]=
  123. {
  124. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
  125. 0xfe,0xdc,0xba,0x98,0x76,0x54,0x32,0x10
  126. };
  127. const byte key_128[]=
  128. {
  129. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
  130. 0xfe,0xdc,0xba,0x98,0x76,0x54,0x32,0x10
  131. };
  132. const byte ciphertext_128[]=
  133. {
  134. 0x67,0x67,0x31,0x38,0x54,0x96,0x69,0x73,
  135. 0x08,0x57,0x06,0x56,0x48,0xea,0xbe,0x43
  136. };
  137. const byte key_192[]=
  138. {
  139. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,0xfe,0xdc,0xba,0x98,
  140. 0x76,0x54,0x32,0x10,0x00,0x11,0x22,0x33,0x44,0x55,0x66,0x77
  141. };
  142. const byte ciphertext_192[]=
  143. {
  144. 0xb4,0x99,0x34,0x01,0xb3,0xe9,0x96,0xf8,
  145. 0x4e,0xe5,0xce,0xe7,0xd7,0x9b,0x09,0xb9
  146. };
  147. const byte key_256[]=
  148. {
  149. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,0xfe,0xdc,0xba,
  150. 0x98,0x76,0x54,0x32,0x10,0x00,0x11,0x22,0x33,0x44,0x55,
  151. 0x66,0x77,0x88,0x99,0xaa,0xbb,0xcc,0xdd,0xee,0xff
  152. };
  153. const byte ciphertext_256[]=
  154. {
  155. 0x9a,0xcc,0x23,0x7d,0xff,0x16,0xd7,0x6c,
  156. 0x20,0xef,0x7c,0x91,0x9e,0x3a,0x75,0x09
  157. };
  158. camellia_setkey(&ctx,key_128,sizeof(key_128));
  159. camellia_encrypt(&ctx,scratch,plaintext);
  160. if(memcmp(scratch,ciphertext_128,sizeof(ciphertext_128))!=0)
  161. return "CAMELLIA-128 test encryption failed.";
  162. camellia_decrypt(&ctx,scratch,scratch);
  163. if(memcmp(scratch,plaintext,sizeof(plaintext))!=0)
  164. return "CAMELLIA-128 test decryption failed.";
  165. camellia_setkey(&ctx,key_192,sizeof(key_192));
  166. camellia_encrypt(&ctx,scratch,plaintext);
  167. if(memcmp(scratch,ciphertext_192,sizeof(ciphertext_192))!=0)
  168. return "CAMELLIA-192 test encryption failed.";
  169. camellia_decrypt(&ctx,scratch,scratch);
  170. if(memcmp(scratch,plaintext,sizeof(plaintext))!=0)
  171. return "CAMELLIA-192 test decryption failed.";
  172. camellia_setkey(&ctx,key_256,sizeof(key_256));
  173. camellia_encrypt(&ctx,scratch,plaintext);
  174. if(memcmp(scratch,ciphertext_256,sizeof(ciphertext_256))!=0)
  175. return "CAMELLIA-256 test encryption failed.";
  176. camellia_decrypt(&ctx,scratch,scratch);
  177. if(memcmp(scratch,plaintext,sizeof(plaintext))!=0)
  178. return "CAMELLIA-256 test decryption failed.";
  179. return NULL;
  180. }
  181. /* These oids are from
  182. <http://info.isl.ntt.co.jp/crypt/eng/camellia/specifications_oid.html>,
  183. retrieved May 1, 2007. */
  184. static gcry_cipher_oid_spec_t camellia128_oids[] =
  185. {
  186. {"1.2.392.200011.61.1.1.1.2", GCRY_CIPHER_MODE_CBC},
  187. {"0.3.4401.5.3.1.9.1", GCRY_CIPHER_MODE_ECB},
  188. {"0.3.4401.5.3.1.9.3", GCRY_CIPHER_MODE_OFB},
  189. {"0.3.4401.5.3.1.9.4", GCRY_CIPHER_MODE_CFB},
  190. { NULL }
  191. };
  192. static gcry_cipher_oid_spec_t camellia192_oids[] =
  193. {
  194. {"1.2.392.200011.61.1.1.1.3", GCRY_CIPHER_MODE_CBC},
  195. {"0.3.4401.5.3.1.9.21", GCRY_CIPHER_MODE_ECB},
  196. {"0.3.4401.5.3.1.9.23", GCRY_CIPHER_MODE_OFB},
  197. {"0.3.4401.5.3.1.9.24", GCRY_CIPHER_MODE_CFB},
  198. { NULL }
  199. };
  200. static gcry_cipher_oid_spec_t camellia256_oids[] =
  201. {
  202. {"1.2.392.200011.61.1.1.1.4", GCRY_CIPHER_MODE_CBC},
  203. {"0.3.4401.5.3.1.9.41", GCRY_CIPHER_MODE_ECB},
  204. {"0.3.4401.5.3.1.9.43", GCRY_CIPHER_MODE_OFB},
  205. {"0.3.4401.5.3.1.9.44", GCRY_CIPHER_MODE_CFB},
  206. { NULL }
  207. };
  208. gcry_cipher_spec_t _gcry_cipher_spec_camellia128 =
  209. {
  210. "CAMELLIA128",NULL,camellia128_oids,CAMELLIA_BLOCK_SIZE,128,
  211. sizeof(CAMELLIA_context),camellia_setkey,camellia_encrypt,camellia_decrypt
  212. };
  213. gcry_cipher_spec_t _gcry_cipher_spec_camellia192 =
  214. {
  215. "CAMELLIA192",NULL,camellia192_oids,CAMELLIA_BLOCK_SIZE,192,
  216. sizeof(CAMELLIA_context),camellia_setkey,camellia_encrypt,camellia_decrypt
  217. };
  218. gcry_cipher_spec_t _gcry_cipher_spec_camellia256 =
  219. {
  220. "CAMELLIA256",NULL,camellia256_oids,CAMELLIA_BLOCK_SIZE,256,
  221. sizeof(CAMELLIA_context),camellia_setkey,camellia_encrypt,camellia_decrypt
  222. };