camellia.c 36 KB

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