camellia.c 35 KB

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