camellia.c 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040
  1. /*
  2. * Camellia implementation
  3. *
  4. * Copyright (C) 2006-2010, Brainspark B.V.
  5. *
  6. * This file is part of PolarSSL (http://www.polarssl.org)
  7. * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
  8. *
  9. * All rights reserved.
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 2 of the License, or
  14. * (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License along
  22. * with this program; if not, write to the Free Software Foundation, Inc.,
  23. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  24. */
  25. /*
  26. * The Camellia block cipher was designed by NTT and Mitsubishi Electric
  27. * Corporation.
  28. *
  29. * http://info.isl.ntt.co.jp/crypt/eng/camellia/dl/01espec.pdf
  30. */
  31. #include "config.h"
  32. #if defined(POLARSSL_CAMELLIA_C)
  33. #include "polarssl/camellia.h"
  34. /*
  35. * 32-bit integer manipulation macros (big endian)
  36. */
  37. #ifndef GET_ULONG_BE
  38. #define GET_ULONG_BE(n,b,i) \
  39. { \
  40. (n) = ( (unsigned long) (b)[(i) ] << 24 ) \
  41. | ( (unsigned long) (b)[(i) + 1] << 16 ) \
  42. | ( (unsigned long) (b)[(i) + 2] << 8 ) \
  43. | ( (unsigned long) (b)[(i) + 3] ); \
  44. }
  45. #endif
  46. #ifndef PUT_ULONG_BE
  47. #define PUT_ULONG_BE(n,b,i) \
  48. { \
  49. (b)[(i) ] = (unsigned char) ( (n) >> 24 ); \
  50. (b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \
  51. (b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); \
  52. (b)[(i) + 3] = (unsigned char) ( (n) ); \
  53. }
  54. #endif
  55. static const unsigned char SIGMA_CHARS[6][8] =
  56. {
  57. { 0xa0, 0x9e, 0x66, 0x7f, 0x3b, 0xcc, 0x90, 0x8b },
  58. { 0xb6, 0x7a, 0xe8, 0x58, 0x4c, 0xaa, 0x73, 0xb2 },
  59. { 0xc6, 0xef, 0x37, 0x2f, 0xe9, 0x4f, 0x82, 0xbe },
  60. { 0x54, 0xff, 0x53, 0xa5, 0xf1, 0xd3, 0x6f, 0x1c },
  61. { 0x10, 0xe5, 0x27, 0xfa, 0xde, 0x68, 0x2d, 0x1d },
  62. { 0xb0, 0x56, 0x88, 0xc2, 0xb3, 0xe6, 0xc1, 0xfd }
  63. };
  64. #ifdef POLARSSL_CAMELLIA_SMALL_MEMORY
  65. static const unsigned char FSb[256] =
  66. {
  67. 112,130, 44,236,179, 39,192,229,228,133, 87, 53,234, 12,174, 65,
  68. 35,239,107,147, 69, 25,165, 33,237, 14, 79, 78, 29,101,146,189,
  69. 134,184,175,143,124,235, 31,206, 62, 48,220, 95, 94,197, 11, 26,
  70. 166,225, 57,202,213, 71, 93, 61,217, 1, 90,214, 81, 86,108, 77,
  71. 139, 13,154,102,251,204,176, 45,116, 18, 43, 32,240,177,132,153,
  72. 223, 76,203,194, 52,126,118, 5,109,183,169, 49,209, 23, 4,215,
  73. 20, 88, 58, 97,222, 27, 17, 28, 50, 15,156, 22, 83, 24,242, 34,
  74. 254, 68,207,178,195,181,122,145, 36, 8,232,168, 96,252,105, 80,
  75. 170,208,160,125,161,137, 98,151, 84, 91, 30,149,224,255,100,210,
  76. 16,196, 0, 72,163,247,117,219,138, 3,230,218, 9, 63,221,148,
  77. 135, 92,131, 2,205, 74,144, 51,115,103,246,243,157,127,191,226,
  78. 82,155,216, 38,200, 55,198, 59,129,150,111, 75, 19,190, 99, 46,
  79. 233,121,167,140,159,110,188,142, 41,245,249,182, 47,253,180, 89,
  80. 120,152, 6,106,231, 70,113,186,212, 37,171, 66,136,162,141,250,
  81. 114, 7,185, 85,248,238,172, 10, 54, 73, 42,104, 60, 56,241,164,
  82. 64, 40,211,123,187,201, 67,193, 21,227,173,244,119,199,128,158
  83. };
  84. #define SBOX1(n) FSb[(n)]
  85. #define SBOX2(n) (unsigned char)((FSb[(n)] >> 7 ^ FSb[(n)] << 1) & 0xff)
  86. #define SBOX3(n) (unsigned char)((FSb[(n)] >> 1 ^ FSb[(n)] << 7) & 0xff)
  87. #define SBOX4(n) FSb[((n) << 1 ^ (n) >> 7) &0xff]
  88. #else
  89. static const unsigned char FSb[256] =
  90. {
  91. 112, 130, 44, 236, 179, 39, 192, 229, 228, 133, 87, 53, 234, 12, 174, 65,
  92. 35, 239, 107, 147, 69, 25, 165, 33, 237, 14, 79, 78, 29, 101, 146, 189,
  93. 134, 184, 175, 143, 124, 235, 31, 206, 62, 48, 220, 95, 94, 197, 11, 26,
  94. 166, 225, 57, 202, 213, 71, 93, 61, 217, 1, 90, 214, 81, 86, 108, 77,
  95. 139, 13, 154, 102, 251, 204, 176, 45, 116, 18, 43, 32, 240, 177, 132, 153,
  96. 223, 76, 203, 194, 52, 126, 118, 5, 109, 183, 169, 49, 209, 23, 4, 215,
  97. 20, 88, 58, 97, 222, 27, 17, 28, 50, 15, 156, 22, 83, 24, 242, 34,
  98. 254, 68, 207, 178, 195, 181, 122, 145, 36, 8, 232, 168, 96, 252, 105, 80,
  99. 170, 208, 160, 125, 161, 137, 98, 151, 84, 91, 30, 149, 224, 255, 100, 210,
  100. 16, 196, 0, 72, 163, 247, 117, 219, 138, 3, 230, 218, 9, 63, 221, 148,
  101. 135, 92, 131, 2, 205, 74, 144, 51, 115, 103, 246, 243, 157, 127, 191, 226,
  102. 82, 155, 216, 38, 200, 55, 198, 59, 129, 150, 111, 75, 19, 190, 99, 46,
  103. 233, 121, 167, 140, 159, 110, 188, 142, 41, 245, 249, 182, 47, 253, 180, 89,
  104. 120, 152, 6, 106, 231, 70, 113, 186, 212, 37, 171, 66, 136, 162, 141, 250,
  105. 114, 7, 185, 85, 248, 238, 172, 10, 54, 73, 42, 104, 60, 56, 241, 164,
  106. 64, 40, 211, 123, 187, 201, 67, 193, 21, 227, 173, 244, 119, 199, 128, 158
  107. };
  108. static const unsigned char FSb2[256] =
  109. {
  110. 224, 5, 88, 217, 103, 78, 129, 203, 201, 11, 174, 106, 213, 24, 93, 130,
  111. 70, 223, 214, 39, 138, 50, 75, 66, 219, 28, 158, 156, 58, 202, 37, 123,
  112. 13, 113, 95, 31, 248, 215, 62, 157, 124, 96, 185, 190, 188, 139, 22, 52,
  113. 77, 195, 114, 149, 171, 142, 186, 122, 179, 2, 180, 173, 162, 172, 216, 154,
  114. 23, 26, 53, 204, 247, 153, 97, 90, 232, 36, 86, 64, 225, 99, 9, 51,
  115. 191, 152, 151, 133, 104, 252, 236, 10, 218, 111, 83, 98, 163, 46, 8, 175,
  116. 40, 176, 116, 194, 189, 54, 34, 56, 100, 30, 57, 44, 166, 48, 229, 68,
  117. 253, 136, 159, 101, 135, 107, 244, 35, 72, 16, 209, 81, 192, 249, 210, 160,
  118. 85, 161, 65, 250, 67, 19, 196, 47, 168, 182, 60, 43, 193, 255, 200, 165,
  119. 32, 137, 0, 144, 71, 239, 234, 183, 21, 6, 205, 181, 18, 126, 187, 41,
  120. 15, 184, 7, 4, 155, 148, 33, 102, 230, 206, 237, 231, 59, 254, 127, 197,
  121. 164, 55, 177, 76, 145, 110, 141, 118, 3, 45, 222, 150, 38, 125, 198, 92,
  122. 211, 242, 79, 25, 63, 220, 121, 29, 82, 235, 243, 109, 94, 251, 105, 178,
  123. 240, 49, 12, 212, 207, 140, 226, 117, 169, 74, 87, 132, 17, 69, 27, 245,
  124. 228, 14, 115, 170, 241, 221, 89, 20, 108, 146, 84, 208, 120, 112, 227, 73,
  125. 128, 80, 167, 246, 119, 147, 134, 131, 42, 199, 91, 233, 238, 143, 1, 61
  126. };
  127. static const unsigned char FSb3[256] =
  128. {
  129. 56, 65, 22, 118, 217, 147, 96, 242, 114, 194, 171, 154, 117, 6, 87, 160,
  130. 145, 247, 181, 201, 162, 140, 210, 144, 246, 7, 167, 39, 142, 178, 73, 222,
  131. 67, 92, 215, 199, 62, 245, 143, 103, 31, 24, 110, 175, 47, 226, 133, 13,
  132. 83, 240, 156, 101, 234, 163, 174, 158, 236, 128, 45, 107, 168, 43, 54, 166,
  133. 197, 134, 77, 51, 253, 102, 88, 150, 58, 9, 149, 16, 120, 216, 66, 204,
  134. 239, 38, 229, 97, 26, 63, 59, 130, 182, 219, 212, 152, 232, 139, 2, 235,
  135. 10, 44, 29, 176, 111, 141, 136, 14, 25, 135, 78, 11, 169, 12, 121, 17,
  136. 127, 34, 231, 89, 225, 218, 61, 200, 18, 4, 116, 84, 48, 126, 180, 40,
  137. 85, 104, 80, 190, 208, 196, 49, 203, 42, 173, 15, 202, 112, 255, 50, 105,
  138. 8, 98, 0, 36, 209, 251, 186, 237, 69, 129, 115, 109, 132, 159, 238, 74,
  139. 195, 46, 193, 1, 230, 37, 72, 153, 185, 179, 123, 249, 206, 191, 223, 113,
  140. 41, 205, 108, 19, 100, 155, 99, 157, 192, 75, 183, 165, 137, 95, 177, 23,
  141. 244, 188, 211, 70, 207, 55, 94, 71, 148, 250, 252, 91, 151, 254, 90, 172,
  142. 60, 76, 3, 53, 243, 35, 184, 93, 106, 146, 213, 33, 68, 81, 198, 125,
  143. 57, 131, 220, 170, 124, 119, 86, 5, 27, 164, 21, 52, 30, 28, 248, 82,
  144. 32, 20, 233, 189, 221, 228, 161, 224, 138, 241, 214, 122, 187, 227, 64, 79
  145. };
  146. static const unsigned char FSb4[256] =
  147. {
  148. 112, 44, 179, 192, 228, 87, 234, 174, 35, 107, 69, 165, 237, 79, 29, 146,
  149. 134, 175, 124, 31, 62, 220, 94, 11, 166, 57, 213, 93, 217, 90, 81, 108,
  150. 139, 154, 251, 176, 116, 43, 240, 132, 223, 203, 52, 118, 109, 169, 209, 4,
  151. 20, 58, 222, 17, 50, 156, 83, 242, 254, 207, 195, 122, 36, 232, 96, 105,
  152. 170, 160, 161, 98, 84, 30, 224, 100, 16, 0, 163, 117, 138, 230, 9, 221,
  153. 135, 131, 205, 144, 115, 246, 157, 191, 82, 216, 200, 198, 129, 111, 19, 99,
  154. 233, 167, 159, 188, 41, 249, 47, 180, 120, 6, 231, 113, 212, 171, 136, 141,
  155. 114, 185, 248, 172, 54, 42, 60, 241, 64, 211, 187, 67, 21, 173, 119, 128,
  156. 130, 236, 39, 229, 133, 53, 12, 65, 239, 147, 25, 33, 14, 78, 101, 189,
  157. 184, 143, 235, 206, 48, 95, 197, 26, 225, 202, 71, 61, 1, 214, 86, 77,
  158. 13, 102, 204, 45, 18, 32, 177, 153, 76, 194, 126, 5, 183, 49, 23, 215,
  159. 88, 97, 27, 28, 15, 22, 24, 34, 68, 178, 181, 145, 8, 168, 252, 80,
  160. 208, 125, 137, 151, 91, 149, 255, 210, 196, 72, 247, 219, 3, 218, 63, 148,
  161. 92, 2, 74, 51, 103, 243, 127, 226, 155, 38, 55, 59, 150, 75, 190, 46,
  162. 121, 140, 110, 142, 245, 182, 253, 89, 152, 106, 70, 186, 37, 66, 162, 250,
  163. 7, 85, 238, 10, 73, 104, 56, 164, 40, 123, 201, 193, 227, 244, 199, 158
  164. };
  165. #define SBOX1(n) FSb[(n)]
  166. #define SBOX2(n) FSb2[(n)]
  167. #define SBOX3(n) FSb3[(n)]
  168. #define SBOX4(n) FSb4[(n)]
  169. #endif
  170. static const unsigned char shifts[2][4][4] =
  171. {
  172. {
  173. { 1, 1, 1, 1 }, /* KL */
  174. { 0, 0, 0, 0 }, /* KR */
  175. { 1, 1, 1, 1 }, /* KA */
  176. { 0, 0, 0, 0 } /* KB */
  177. },
  178. {
  179. { 1, 0, 1, 1 }, /* KL */
  180. { 1, 1, 0, 1 }, /* KR */
  181. { 1, 1, 1, 0 }, /* KA */
  182. { 1, 1, 0, 1 } /* KB */
  183. }
  184. };
  185. static const signed char indexes[2][4][20] =
  186. {
  187. {
  188. { 0, 1, 2, 3, 8, 9, 10, 11, 38, 39,
  189. 36, 37, 23, 20, 21, 22, 27, -1, -1, 26 }, /* KL -> RK */
  190. { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  191. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 }, /* KR -> RK */
  192. { 4, 5, 6, 7, 12, 13, 14, 15, 16, 17,
  193. 18, 19, -1, 24, 25, -1, 31, 28, 29, 30 }, /* KA -> RK */
  194. { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  195. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 } /* KB -> RK */
  196. },
  197. {
  198. { 0, 1, 2, 3, 61, 62, 63, 60, -1, -1,
  199. -1, -1, 27, 24, 25, 26, 35, 32, 33, 34 }, /* KL -> RK */
  200. { -1, -1, -1, -1, 8, 9, 10, 11, 16, 17,
  201. 18, 19, -1, -1, -1, -1, 39, 36, 37, 38 }, /* KR -> RK */
  202. { -1, -1, -1, -1, 12, 13, 14, 15, 58, 59,
  203. 56, 57, 31, 28, 29, 30, -1, -1, -1, -1 }, /* KA -> RK */
  204. { 4, 5, 6, 7, 65, 66, 67, 64, 20, 21,
  205. 22, 23, -1, -1, -1, -1, 43, 40, 41, 42 } /* KB -> RK */
  206. }
  207. };
  208. static const signed char transposes[2][20] =
  209. {
  210. {
  211. 21, 22, 23, 20,
  212. -1, -1, -1, -1,
  213. 18, 19, 16, 17,
  214. 11, 8, 9, 10,
  215. 15, 12, 13, 14
  216. },
  217. {
  218. 25, 26, 27, 24,
  219. 29, 30, 31, 28,
  220. 18, 19, 16, 17,
  221. -1, -1, -1, -1,
  222. -1, -1, -1, -1
  223. }
  224. };
  225. /* Shift macro for 128 bit strings with rotation smaller than 32 bits (!) */
  226. #define ROTL(DEST, SRC, SHIFT) \
  227. { \
  228. (DEST)[0] = (SRC)[0] << (SHIFT) ^ (SRC)[1] >> (32 - (SHIFT)); \
  229. (DEST)[1] = (SRC)[1] << (SHIFT) ^ (SRC)[2] >> (32 - (SHIFT)); \
  230. (DEST)[2] = (SRC)[2] << (SHIFT) ^ (SRC)[3] >> (32 - (SHIFT)); \
  231. (DEST)[3] = (SRC)[3] << (SHIFT) ^ (SRC)[0] >> (32 - (SHIFT)); \
  232. }
  233. #define FL(XL, XR, KL, KR) \
  234. { \
  235. (XR) = ((((XL) & (KL)) << 1) | (((XL) & (KL)) >> 31)) ^ (XR); \
  236. (XL) = ((XR) | (KR)) ^ (XL); \
  237. }
  238. #define FLInv(YL, YR, KL, KR) \
  239. { \
  240. (YL) = ((YR) | (KR)) ^ (YL); \
  241. (YR) = ((((YL) & (KL)) << 1) | (((YL) & (KL)) >> 31)) ^ (YR); \
  242. }
  243. #define SHIFT_AND_PLACE(INDEX, OFFSET) \
  244. { \
  245. TK[0] = KC[(OFFSET) * 4 + 0]; \
  246. TK[1] = KC[(OFFSET) * 4 + 1]; \
  247. TK[2] = KC[(OFFSET) * 4 + 2]; \
  248. TK[3] = KC[(OFFSET) * 4 + 3]; \
  249. \
  250. for ( i = 1; i <= 4; i++ ) \
  251. if (shifts[(INDEX)][(OFFSET)][i -1]) \
  252. ROTL(TK + i * 4, TK, (15 * i) % 32); \
  253. \
  254. for ( i = 0; i < 20; i++ ) \
  255. if (indexes[(INDEX)][(OFFSET)][i] != -1) { \
  256. RK[indexes[(INDEX)][(OFFSET)][i]] = TK[ i ]; \
  257. } \
  258. }
  259. static void camellia_feistel(const uint32_t x[2], const uint32_t k[2], uint32_t z[2])
  260. {
  261. uint32_t I0, I1;
  262. I0 = x[0] ^ k[0];
  263. I1 = x[1] ^ k[1];
  264. I0 = (SBOX1((I0 >> 24) & 0xFF) << 24) |
  265. (SBOX2((I0 >> 16) & 0xFF) << 16) |
  266. (SBOX3((I0 >> 8) & 0xFF) << 8) |
  267. (SBOX4((I0 ) & 0xFF) );
  268. I1 = (SBOX2((I1 >> 24) & 0xFF) << 24) |
  269. (SBOX3((I1 >> 16) & 0xFF) << 16) |
  270. (SBOX4((I1 >> 8) & 0xFF) << 8) |
  271. (SBOX1((I1 ) & 0xFF) );
  272. I0 ^= (I1 << 8) | (I1 >> 24);
  273. I1 ^= (I0 << 16) | (I0 >> 16);
  274. I0 ^= (I1 >> 8) | (I1 << 24);
  275. I1 ^= (I0 >> 8) | (I0 << 24);
  276. z[0] ^= I1;
  277. z[1] ^= I0;
  278. }
  279. /*
  280. * Camellia key schedule (encryption)
  281. */
  282. int camellia_setkey_enc( camellia_context *ctx, const unsigned char *key, unsigned int keysize )
  283. {
  284. int idx;
  285. size_t i;
  286. uint32_t *RK;
  287. unsigned char t[64];
  288. uint32_t SIGMA[6][2];
  289. uint32_t KC[16];
  290. uint32_t TK[20];
  291. RK = ctx->rk;
  292. memset(t, 0, 64);
  293. memset(RK, 0, sizeof(ctx->rk));
  294. switch( keysize )
  295. {
  296. case 128: ctx->nr = 3; idx = 0; break;
  297. case 192:
  298. case 256: ctx->nr = 4; idx = 1; break;
  299. default : return( POLARSSL_ERR_CAMELLIA_INVALID_KEY_LENGTH );
  300. }
  301. for( i = 0; i < keysize / 8; ++i)
  302. t[i] = key[i];
  303. if (keysize == 192) {
  304. for (i = 0; i < 8; i++)
  305. t[24 + i] = ~t[16 + i];
  306. }
  307. /*
  308. * Prepare SIGMA values
  309. */
  310. for (i = 0; i < 6; i++) {
  311. GET_ULONG_BE(SIGMA[i][0], SIGMA_CHARS[i], 0);
  312. GET_ULONG_BE(SIGMA[i][1], SIGMA_CHARS[i], 4);
  313. }
  314. /*
  315. * Key storage in KC
  316. * Order: KL, KR, KA, KB
  317. */
  318. memset(KC, 0, sizeof(KC));
  319. /* Store KL, KR */
  320. for (i = 0; i < 8; i++)
  321. GET_ULONG_BE(KC[i], t, i * 4);
  322. /* Generate KA */
  323. for( i = 0; i < 4; ++i)
  324. KC[8 + i] = KC[i] ^ KC[4 + i];
  325. camellia_feistel(KC + 8, SIGMA[0], KC + 10);
  326. camellia_feistel(KC + 10, SIGMA[1], KC + 8);
  327. for( i = 0; i < 4; ++i)
  328. KC[8 + i] ^= KC[i];
  329. camellia_feistel(KC + 8, SIGMA[2], KC + 10);
  330. camellia_feistel(KC + 10, SIGMA[3], KC + 8);
  331. if (keysize > 128) {
  332. /* Generate KB */
  333. for( i = 0; i < 4; ++i)
  334. KC[12 + i] = KC[4 + i] ^ KC[8 + i];
  335. camellia_feistel(KC + 12, SIGMA[4], KC + 14);
  336. camellia_feistel(KC + 14, SIGMA[5], KC + 12);
  337. }
  338. /*
  339. * Generating subkeys
  340. */
  341. /* Manipulating KL */
  342. SHIFT_AND_PLACE(idx, 0);
  343. /* Manipulating KR */
  344. if (keysize > 128) {
  345. SHIFT_AND_PLACE(idx, 1);
  346. }
  347. /* Manipulating KA */
  348. SHIFT_AND_PLACE(idx, 2);
  349. /* Manipulating KB */
  350. if (keysize > 128) {
  351. SHIFT_AND_PLACE(idx, 3);
  352. }
  353. /* Do transpositions */
  354. for ( i = 0; i < 20; i++ ) {
  355. if (transposes[idx][i] != -1) {
  356. RK[32 + 12 * idx + i] = RK[transposes[idx][i]];
  357. }
  358. }
  359. return( 0 );
  360. }
  361. /*
  362. * Camellia key schedule (decryption)
  363. */
  364. int camellia_setkey_dec( camellia_context *ctx, const unsigned char *key, unsigned int keysize )
  365. {
  366. int idx;
  367. size_t i;
  368. camellia_context cty;
  369. uint32_t *RK;
  370. uint32_t *SK;
  371. int ret;
  372. switch( keysize )
  373. {
  374. case 128: ctx->nr = 3; idx = 0; break;
  375. case 192:
  376. case 256: ctx->nr = 4; idx = 1; break;
  377. default : return( POLARSSL_ERR_CAMELLIA_INVALID_KEY_LENGTH );
  378. }
  379. RK = ctx->rk;
  380. ret = camellia_setkey_enc(&cty, key, keysize);
  381. if( ret != 0 )
  382. return( ret );
  383. SK = cty.rk + 24 * 2 + 8 * idx * 2;
  384. *RK++ = *SK++;
  385. *RK++ = *SK++;
  386. *RK++ = *SK++;
  387. *RK++ = *SK++;
  388. for (i = 22 + 8 * idx, SK -= 6; i > 0; i--, SK -= 4)
  389. {
  390. *RK++ = *SK++;
  391. *RK++ = *SK++;
  392. }
  393. SK -= 2;
  394. *RK++ = *SK++;
  395. *RK++ = *SK++;
  396. *RK++ = *SK++;
  397. *RK++ = *SK++;
  398. memset( &cty, 0, sizeof( camellia_context ) );
  399. return( 0 );
  400. }
  401. /*
  402. * Camellia-ECB block encryption/decryption
  403. */
  404. int camellia_crypt_ecb( camellia_context *ctx,
  405. int mode,
  406. const unsigned char input[16],
  407. unsigned char output[16] )
  408. {
  409. int NR;
  410. uint32_t *RK, X[4];
  411. ( (void) mode );
  412. NR = ctx->nr;
  413. RK = ctx->rk;
  414. GET_ULONG_BE( X[0], input, 0 );
  415. GET_ULONG_BE( X[1], input, 4 );
  416. GET_ULONG_BE( X[2], input, 8 );
  417. GET_ULONG_BE( X[3], input, 12 );
  418. X[0] ^= *RK++;
  419. X[1] ^= *RK++;
  420. X[2] ^= *RK++;
  421. X[3] ^= *RK++;
  422. while (NR) {
  423. --NR;
  424. camellia_feistel(X, RK, X + 2);
  425. RK += 2;
  426. camellia_feistel(X + 2, RK, X);
  427. RK += 2;
  428. camellia_feistel(X, RK, X + 2);
  429. RK += 2;
  430. camellia_feistel(X + 2, RK, X);
  431. RK += 2;
  432. camellia_feistel(X, RK, X + 2);
  433. RK += 2;
  434. camellia_feistel(X + 2, RK, X);
  435. RK += 2;
  436. if (NR) {
  437. FL(X[0], X[1], RK[0], RK[1]);
  438. RK += 2;
  439. FLInv(X[2], X[3], RK[0], RK[1]);
  440. RK += 2;
  441. }
  442. }
  443. X[2] ^= *RK++;
  444. X[3] ^= *RK++;
  445. X[0] ^= *RK++;
  446. X[1] ^= *RK++;
  447. PUT_ULONG_BE( X[2], output, 0 );
  448. PUT_ULONG_BE( X[3], output, 4 );
  449. PUT_ULONG_BE( X[0], output, 8 );
  450. PUT_ULONG_BE( X[1], output, 12 );
  451. return( 0 );
  452. }
  453. /*
  454. * Camellia-CBC buffer encryption/decryption
  455. */
  456. int camellia_crypt_cbc( camellia_context *ctx,
  457. int mode,
  458. size_t length,
  459. unsigned char iv[16],
  460. const unsigned char *input,
  461. unsigned char *output )
  462. {
  463. int i;
  464. unsigned char temp[16];
  465. if( length % 16 )
  466. return( POLARSSL_ERR_CAMELLIA_INVALID_INPUT_LENGTH );
  467. if( mode == CAMELLIA_DECRYPT )
  468. {
  469. while( length > 0 )
  470. {
  471. memcpy( temp, input, 16 );
  472. camellia_crypt_ecb( ctx, mode, input, output );
  473. for( i = 0; i < 16; i++ )
  474. output[i] = (unsigned char)( output[i] ^ iv[i] );
  475. memcpy( iv, temp, 16 );
  476. input += 16;
  477. output += 16;
  478. length -= 16;
  479. }
  480. }
  481. else
  482. {
  483. while( length > 0 )
  484. {
  485. for( i = 0; i < 16; i++ )
  486. output[i] = (unsigned char)( input[i] ^ iv[i] );
  487. camellia_crypt_ecb( ctx, mode, output, output );
  488. memcpy( iv, output, 16 );
  489. input += 16;
  490. output += 16;
  491. length -= 16;
  492. }
  493. }
  494. return( 0 );
  495. }
  496. #if defined(POLARSSL_CIPHER_MODE_CFB)
  497. /*
  498. * Camellia-CFB128 buffer encryption/decryption
  499. */
  500. int camellia_crypt_cfb128( camellia_context *ctx,
  501. int mode,
  502. size_t length,
  503. size_t *iv_off,
  504. unsigned char iv[16],
  505. const unsigned char *input,
  506. unsigned char *output )
  507. {
  508. int c;
  509. size_t n = *iv_off;
  510. if( mode == CAMELLIA_DECRYPT )
  511. {
  512. while( length-- )
  513. {
  514. if( n == 0 )
  515. camellia_crypt_ecb( ctx, CAMELLIA_ENCRYPT, iv, iv );
  516. c = *input++;
  517. *output++ = (unsigned char)( c ^ iv[n] );
  518. iv[n] = (unsigned char) c;
  519. n = (n + 1) & 0x0F;
  520. }
  521. }
  522. else
  523. {
  524. while( length-- )
  525. {
  526. if( n == 0 )
  527. camellia_crypt_ecb( ctx, CAMELLIA_ENCRYPT, iv, iv );
  528. iv[n] = *output++ = (unsigned char)( iv[n] ^ *input++ );
  529. n = (n + 1) & 0x0F;
  530. }
  531. }
  532. *iv_off = n;
  533. return( 0 );
  534. }
  535. #endif /* POLARSSL_CIPHER_MODE_CFB */
  536. #if defined(POLARSSL_CIPHER_MODE_CTR)
  537. /*
  538. * Camellia-CTR buffer encryption/decryption
  539. */
  540. int camellia_crypt_ctr( camellia_context *ctx,
  541. size_t length,
  542. size_t *nc_off,
  543. unsigned char nonce_counter[16],
  544. unsigned char stream_block[16],
  545. const unsigned char *input,
  546. unsigned char *output )
  547. {
  548. int c, i, cb;
  549. size_t n = *nc_off;
  550. while( length-- )
  551. {
  552. if( n == 0 ) {
  553. camellia_crypt_ecb( ctx, CAMELLIA_ENCRYPT, nonce_counter, stream_block );
  554. i = 15;
  555. do {
  556. nonce_counter[i]++;
  557. cb = nonce_counter[i] == 0;
  558. } while( i-- && cb );
  559. }
  560. c = *input++;
  561. *output++ = (unsigned char)( c ^ stream_block[n] );
  562. n = (n + 1) & 0x0F;
  563. }
  564. *nc_off = n;
  565. return( 0 );
  566. }
  567. #endif /* POLARSSL_CIPHER_MODE_CTR */
  568. #if defined(POLARSSL_SELF_TEST)
  569. #ifdef PRINTF_STDLIB
  570. #include <stdio.h>
  571. #endif
  572. #ifdef PRINTF_CUSTOM
  573. #include "tinystdio.h"
  574. #endif
  575. /*
  576. * Camellia test vectors from:
  577. *
  578. * http://info.isl.ntt.co.jp/crypt/eng/camellia/technology.html:
  579. * http://info.isl.ntt.co.jp/crypt/eng/camellia/dl/cryptrec/intermediate.txt
  580. * http://info.isl.ntt.co.jp/crypt/eng/camellia/dl/cryptrec/t_camellia.txt
  581. * (For each bitlength: Key 0, Nr 39)
  582. */
  583. #define CAMELLIA_TESTS_ECB 2
  584. static const unsigned char camellia_test_ecb_key[3][CAMELLIA_TESTS_ECB][32] =
  585. {
  586. {
  587. { 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  588. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10 },
  589. { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  590. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }
  591. },
  592. {
  593. { 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  594. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
  595. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77 },
  596. { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  597. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  598. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }
  599. },
  600. {
  601. { 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  602. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
  603. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  604. 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff },
  605. { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  606. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  607. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  608. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }
  609. },
  610. };
  611. static const unsigned char camellia_test_ecb_plain[CAMELLIA_TESTS_ECB][16] =
  612. {
  613. { 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  614. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10 },
  615. { 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00,
  616. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }
  617. };
  618. static const unsigned char camellia_test_ecb_cipher[3][CAMELLIA_TESTS_ECB][16] =
  619. {
  620. {
  621. { 0x67, 0x67, 0x31, 0x38, 0x54, 0x96, 0x69, 0x73,
  622. 0x08, 0x57, 0x06, 0x56, 0x48, 0xea, 0xbe, 0x43 },
  623. { 0x38, 0x3C, 0x6C, 0x2A, 0xAB, 0xEF, 0x7F, 0xDE,
  624. 0x25, 0xCD, 0x47, 0x0B, 0xF7, 0x74, 0xA3, 0x31 }
  625. },
  626. {
  627. { 0xb4, 0x99, 0x34, 0x01, 0xb3, 0xe9, 0x96, 0xf8,
  628. 0x4e, 0xe5, 0xce, 0xe7, 0xd7, 0x9b, 0x09, 0xb9 },
  629. { 0xD1, 0x76, 0x3F, 0xC0, 0x19, 0xD7, 0x7C, 0xC9,
  630. 0x30, 0xBF, 0xF2, 0xA5, 0x6F, 0x7C, 0x93, 0x64 }
  631. },
  632. {
  633. { 0x9a, 0xcc, 0x23, 0x7d, 0xff, 0x16, 0xd7, 0x6c,
  634. 0x20, 0xef, 0x7c, 0x91, 0x9e, 0x3a, 0x75, 0x09 },
  635. { 0x05, 0x03, 0xFB, 0x10, 0xAB, 0x24, 0x1E, 0x7C,
  636. 0xF4, 0x5D, 0x8C, 0xDE, 0xEE, 0x47, 0x43, 0x35 }
  637. }
  638. };
  639. #define CAMELLIA_TESTS_CBC 3
  640. static const unsigned char camellia_test_cbc_key[3][32] =
  641. {
  642. { 0x2B, 0x7E, 0x15, 0x16, 0x28, 0xAE, 0xD2, 0xA6,
  643. 0xAB, 0xF7, 0x15, 0x88, 0x09, 0xCF, 0x4F, 0x3C }
  644. ,
  645. { 0x8E, 0x73, 0xB0, 0xF7, 0xDA, 0x0E, 0x64, 0x52,
  646. 0xC8, 0x10, 0xF3, 0x2B, 0x80, 0x90, 0x79, 0xE5,
  647. 0x62, 0xF8, 0xEA, 0xD2, 0x52, 0x2C, 0x6B, 0x7B }
  648. ,
  649. { 0x60, 0x3D, 0xEB, 0x10, 0x15, 0xCA, 0x71, 0xBE,
  650. 0x2B, 0x73, 0xAE, 0xF0, 0x85, 0x7D, 0x77, 0x81,
  651. 0x1F, 0x35, 0x2C, 0x07, 0x3B, 0x61, 0x08, 0xD7,
  652. 0x2D, 0x98, 0x10, 0xA3, 0x09, 0x14, 0xDF, 0xF4 }
  653. };
  654. static const unsigned char camellia_test_cbc_iv[16] =
  655. { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  656. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F }
  657. ;
  658. static const unsigned char camellia_test_cbc_plain[CAMELLIA_TESTS_CBC][16] =
  659. {
  660. { 0x6B, 0xC1, 0xBE, 0xE2, 0x2E, 0x40, 0x9F, 0x96,
  661. 0xE9, 0x3D, 0x7E, 0x11, 0x73, 0x93, 0x17, 0x2A },
  662. { 0xAE, 0x2D, 0x8A, 0x57, 0x1E, 0x03, 0xAC, 0x9C,
  663. 0x9E, 0xB7, 0x6F, 0xAC, 0x45, 0xAF, 0x8E, 0x51 },
  664. { 0x30, 0xC8, 0x1C, 0x46, 0xA3, 0x5C, 0xE4, 0x11,
  665. 0xE5, 0xFB, 0xC1, 0x19, 0x1A, 0x0A, 0x52, 0xEF }
  666. };
  667. static const unsigned char camellia_test_cbc_cipher[3][CAMELLIA_TESTS_CBC][16] =
  668. {
  669. {
  670. { 0x16, 0x07, 0xCF, 0x49, 0x4B, 0x36, 0xBB, 0xF0,
  671. 0x0D, 0xAE, 0xB0, 0xB5, 0x03, 0xC8, 0x31, 0xAB },
  672. { 0xA2, 0xF2, 0xCF, 0x67, 0x16, 0x29, 0xEF, 0x78,
  673. 0x40, 0xC5, 0xA5, 0xDF, 0xB5, 0x07, 0x48, 0x87 },
  674. { 0x0F, 0x06, 0x16, 0x50, 0x08, 0xCF, 0x8B, 0x8B,
  675. 0x5A, 0x63, 0x58, 0x63, 0x62, 0x54, 0x3E, 0x54 }
  676. },
  677. {
  678. { 0x2A, 0x48, 0x30, 0xAB, 0x5A, 0xC4, 0xA1, 0xA2,
  679. 0x40, 0x59, 0x55, 0xFD, 0x21, 0x95, 0xCF, 0x93 },
  680. { 0x5D, 0x5A, 0x86, 0x9B, 0xD1, 0x4C, 0xE5, 0x42,
  681. 0x64, 0xF8, 0x92, 0xA6, 0xDD, 0x2E, 0xC3, 0xD5 },
  682. { 0x37, 0xD3, 0x59, 0xC3, 0x34, 0x98, 0x36, 0xD8,
  683. 0x84, 0xE3, 0x10, 0xAD, 0xDF, 0x68, 0xC4, 0x49 }
  684. },
  685. {
  686. { 0xE6, 0xCF, 0xA3, 0x5F, 0xC0, 0x2B, 0x13, 0x4A,
  687. 0x4D, 0x2C, 0x0B, 0x67, 0x37, 0xAC, 0x3E, 0xDA },
  688. { 0x36, 0xCB, 0xEB, 0x73, 0xBD, 0x50, 0x4B, 0x40,
  689. 0x70, 0xB1, 0xB7, 0xDE, 0x2B, 0x21, 0xEB, 0x50 },
  690. { 0xE3, 0x1A, 0x60, 0x55, 0x29, 0x7D, 0x96, 0xCA,
  691. 0x33, 0x30, 0xCD, 0xF1, 0xB1, 0x86, 0x0A, 0x83 }
  692. }
  693. };
  694. #if defined(POLARSSL_CIPHER_MODE_CTR)
  695. /*
  696. * Camellia-CTR test vectors from:
  697. *
  698. * http://www.faqs.org/rfcs/rfc5528.html
  699. */
  700. static const unsigned char camellia_test_ctr_key[3][16] =
  701. {
  702. { 0xAE, 0x68, 0x52, 0xF8, 0x12, 0x10, 0x67, 0xCC,
  703. 0x4B, 0xF7, 0xA5, 0x76, 0x55, 0x77, 0xF3, 0x9E },
  704. { 0x7E, 0x24, 0x06, 0x78, 0x17, 0xFA, 0xE0, 0xD7,
  705. 0x43, 0xD6, 0xCE, 0x1F, 0x32, 0x53, 0x91, 0x63 },
  706. { 0x76, 0x91, 0xBE, 0x03, 0x5E, 0x50, 0x20, 0xA8,
  707. 0xAC, 0x6E, 0x61, 0x85, 0x29, 0xF9, 0xA0, 0xDC }
  708. };
  709. static const unsigned char camellia_test_ctr_nonce_counter[3][16] =
  710. {
  711. { 0x00, 0x00, 0x00, 0x30, 0x00, 0x00, 0x00, 0x00,
  712. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01 },
  713. { 0x00, 0x6C, 0xB6, 0xDB, 0xC0, 0x54, 0x3B, 0x59,
  714. 0xDA, 0x48, 0xD9, 0x0B, 0x00, 0x00, 0x00, 0x01 },
  715. { 0x00, 0xE0, 0x01, 0x7B, 0x27, 0x77, 0x7F, 0x3F,
  716. 0x4A, 0x17, 0x86, 0xF0, 0x00, 0x00, 0x00, 0x01 }
  717. };
  718. static const unsigned char camellia_test_ctr_pt[3][48] =
  719. {
  720. { 0x53, 0x69, 0x6E, 0x67, 0x6C, 0x65, 0x20, 0x62,
  721. 0x6C, 0x6F, 0x63, 0x6B, 0x20, 0x6D, 0x73, 0x67 },
  722. { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  723. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
  724. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  725. 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F },
  726. { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  727. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
  728. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  729. 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F,
  730. 0x20, 0x21, 0x22, 0x23 }
  731. };
  732. static const unsigned char camellia_test_ctr_ct[3][48] =
  733. {
  734. { 0xD0, 0x9D, 0xC2, 0x9A, 0x82, 0x14, 0x61, 0x9A,
  735. 0x20, 0x87, 0x7C, 0x76, 0xDB, 0x1F, 0x0B, 0x3F },
  736. { 0xDB, 0xF3, 0xC7, 0x8D, 0xC0, 0x83, 0x96, 0xD4,
  737. 0xDA, 0x7C, 0x90, 0x77, 0x65, 0xBB, 0xCB, 0x44,
  738. 0x2B, 0x8E, 0x8E, 0x0F, 0x31, 0xF0, 0xDC, 0xA7,
  739. 0x2C, 0x74, 0x17, 0xE3, 0x53, 0x60, 0xE0, 0x48 },
  740. { 0xB1, 0x9D, 0x1F, 0xCD, 0xCB, 0x75, 0xEB, 0x88,
  741. 0x2F, 0x84, 0x9C, 0xE2, 0x4D, 0x85, 0xCF, 0x73,
  742. 0x9C, 0xE6, 0x4B, 0x2B, 0x5C, 0x9D, 0x73, 0xF1,
  743. 0x4F, 0x2D, 0x5D, 0x9D, 0xCE, 0x98, 0x89, 0xCD,
  744. 0xDF, 0x50, 0x86, 0x96 }
  745. };
  746. static const int camellia_test_ctr_len[3] =
  747. { 16, 32, 36 };
  748. #endif /* POLARSSL_CIPHER_MODE_CTR */
  749. /*
  750. * Checkup routine
  751. */
  752. int camellia_self_test( int verbose )
  753. {
  754. int i, j, u, v;
  755. unsigned char key[32];
  756. unsigned char buf[64];
  757. unsigned char src[16];
  758. unsigned char dst[16];
  759. unsigned char iv[16];
  760. #if defined(POLARSSL_CIPHER_MODE_CTR)
  761. size_t offset, len;
  762. unsigned char nonce_counter[16];
  763. unsigned char stream_block[16];
  764. #endif
  765. camellia_context ctx;
  766. memset( key, 0, 32 );
  767. for (j = 0; j < 6; j++) {
  768. u = j >> 1;
  769. v = j & 1;
  770. if( verbose != 0 )
  771. printf( " CAMELLIA-ECB-%3d (%s): ", 128 + u * 64,
  772. (v == CAMELLIA_DECRYPT) ? "dec" : "enc");
  773. for (i = 0; i < CAMELLIA_TESTS_ECB; i++ ) {
  774. memcpy( key, camellia_test_ecb_key[u][i], 16 + 8 * u);
  775. if (v == CAMELLIA_DECRYPT) {
  776. camellia_setkey_dec(&ctx, key, 128 + u * 64);
  777. memcpy(src, camellia_test_ecb_cipher[u][i], 16);
  778. memcpy(dst, camellia_test_ecb_plain[i], 16);
  779. } else { /* CAMELLIA_ENCRYPT */
  780. camellia_setkey_enc(&ctx, key, 128 + u * 64);
  781. memcpy(src, camellia_test_ecb_plain[i], 16);
  782. memcpy(dst, camellia_test_ecb_cipher[u][i], 16);
  783. }
  784. camellia_crypt_ecb(&ctx, v, src, buf);
  785. if( memcmp( buf, dst, 16 ) != 0 )
  786. {
  787. if( verbose != 0 )
  788. printf( "failed\n" );
  789. return( 1 );
  790. }
  791. }
  792. if( verbose != 0 )
  793. printf( "passed\n" );
  794. }
  795. if( verbose != 0 )
  796. printf( "\n" );
  797. /*
  798. * CBC mode
  799. */
  800. for( j = 0; j < 6; j++ )
  801. {
  802. u = j >> 1;
  803. v = j & 1;
  804. if( verbose != 0 )
  805. printf( " CAMELLIA-CBC-%3d (%s): ", 128 + u * 64,
  806. ( v == CAMELLIA_DECRYPT ) ? "dec" : "enc" );
  807. memcpy( src, camellia_test_cbc_iv, 16);
  808. memcpy( dst, camellia_test_cbc_iv, 16);
  809. memcpy( key, camellia_test_cbc_key[u], 16 + 8 * u);
  810. if (v == CAMELLIA_DECRYPT) {
  811. camellia_setkey_dec(&ctx, key, 128 + u * 64);
  812. } else {
  813. camellia_setkey_enc(&ctx, key, 128 + u * 64);
  814. }
  815. for (i = 0; i < CAMELLIA_TESTS_CBC; i++ ) {
  816. if (v == CAMELLIA_DECRYPT) {
  817. memcpy( iv , src, 16 );
  818. memcpy(src, camellia_test_cbc_cipher[u][i], 16);
  819. memcpy(dst, camellia_test_cbc_plain[i], 16);
  820. } else { /* CAMELLIA_ENCRYPT */
  821. memcpy( iv , dst, 16 );
  822. memcpy(src, camellia_test_cbc_plain[i], 16);
  823. memcpy(dst, camellia_test_cbc_cipher[u][i], 16);
  824. }
  825. camellia_crypt_cbc(&ctx, v, 16, iv, src, buf);
  826. if( memcmp( buf, dst, 16 ) != 0 )
  827. {
  828. if( verbose != 0 )
  829. printf( "failed\n" );
  830. return( 1 );
  831. }
  832. }
  833. if( verbose != 0 )
  834. printf( "passed\n" );
  835. }
  836. if( verbose != 0 )
  837. printf( "\n" );
  838. #if defined(POLARSSL_CIPHER_MODE_CTR)
  839. /*
  840. * CTR mode
  841. */
  842. for( i = 0; i < 6; i++ )
  843. {
  844. u = i >> 1;
  845. v = i & 1;
  846. if( verbose != 0 )
  847. printf( " CAMELLIA-CTR-128 (%s): ",
  848. ( v == CAMELLIA_DECRYPT ) ? "dec" : "enc" );
  849. memcpy( nonce_counter, camellia_test_ctr_nonce_counter[u], 16 );
  850. memcpy( key, camellia_test_ctr_key[u], 16 );
  851. offset = 0;
  852. camellia_setkey_enc( &ctx, key, 128 );
  853. if( v == CAMELLIA_DECRYPT )
  854. {
  855. len = camellia_test_ctr_len[u];
  856. memcpy( buf, camellia_test_ctr_ct[u], len );
  857. camellia_crypt_ctr( &ctx, len, &offset, nonce_counter, stream_block, buf, buf );
  858. if( memcmp( buf, camellia_test_ctr_pt[u], len ) != 0 )
  859. {
  860. if( verbose != 0 )
  861. printf( "failed\n" );
  862. return( 1 );
  863. }
  864. }
  865. else
  866. {
  867. len = camellia_test_ctr_len[u];
  868. memcpy( buf, camellia_test_ctr_pt[u], len );
  869. camellia_crypt_ctr( &ctx, len, &offset, nonce_counter, stream_block, buf, buf );
  870. if( memcmp( buf, camellia_test_ctr_ct[u], len ) != 0 )
  871. {
  872. if( verbose != 0 )
  873. printf( "failed\n" );
  874. return( 1 );
  875. }
  876. }
  877. if( verbose != 0 )
  878. printf( "passed\n" );
  879. }
  880. if( verbose != 0 )
  881. printf( "\n" );
  882. #endif /* POLARSSL_CIPHER_MODE_CTR */
  883. return ( 0 );
  884. }
  885. #endif
  886. #endif