camellia.c 34 KB

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