cipher.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536
  1. /**
  2. * \file cipher.c
  3. *
  4. * \brief Generic cipher wrapper for PolarSSL
  5. *
  6. * \author Adriaan de Jong <dejong@fox-it.com>
  7. *
  8. * Copyright (C) 2006-2010, Brainspark B.V.
  9. *
  10. * This file is part of PolarSSL (http://www.polarssl.org)
  11. * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
  12. *
  13. * All rights reserved.
  14. *
  15. * This program is free software; you can redistribute it and/or modify
  16. * it under the terms of the GNU General Public License as published by
  17. * the Free Software Foundation; either version 2 of the License, or
  18. * (at your option) any later version.
  19. *
  20. * This program is distributed in the hope that it will be useful,
  21. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. * GNU General Public License for more details.
  24. *
  25. * You should have received a copy of the GNU General Public License along
  26. * with this program; if not, write to the Free Software Foundation, Inc.,
  27. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  28. */
  29. #include "config.h"
  30. #if defined(POLARSSL_CIPHER_C)
  31. #include "polarssl/cipher.h"
  32. #include "polarssl/cipher_wrap.h"
  33. #include <stdlib.h>
  34. #if defined ( __GNUC__ )
  35. #include <my_strings.h>
  36. #endif
  37. #if defined _MSC_VER && !defined strcasecmp
  38. #define strcasecmp _stricmp
  39. #endif
  40. static const int supported_ciphers[] = {
  41. #if defined(POLARSSL_AES_C)
  42. POLARSSL_CIPHER_AES_128_CBC,
  43. POLARSSL_CIPHER_AES_192_CBC,
  44. POLARSSL_CIPHER_AES_256_CBC,
  45. #if defined(POLARSSL_CIPHER_MODE_CFB)
  46. POLARSSL_CIPHER_AES_128_CFB128,
  47. POLARSSL_CIPHER_AES_192_CFB128,
  48. POLARSSL_CIPHER_AES_256_CFB128,
  49. #endif /* defined(POLARSSL_CIPHER_MODE_CFB) */
  50. #if defined(POLARSSL_CIPHER_MODE_CTR)
  51. POLARSSL_CIPHER_AES_128_CTR,
  52. POLARSSL_CIPHER_AES_192_CTR,
  53. POLARSSL_CIPHER_AES_256_CTR,
  54. #endif /* defined(POLARSSL_CIPHER_MODE_CTR) */
  55. #endif /* defined(POLARSSL_AES_C) */
  56. #if defined(POLARSSL_CAMELLIA_C)
  57. POLARSSL_CIPHER_CAMELLIA_128_CBC,
  58. POLARSSL_CIPHER_CAMELLIA_192_CBC,
  59. POLARSSL_CIPHER_CAMELLIA_256_CBC,
  60. #if defined(POLARSSL_CIPHER_MODE_CFB)
  61. POLARSSL_CIPHER_CAMELLIA_128_CFB128,
  62. POLARSSL_CIPHER_CAMELLIA_192_CFB128,
  63. POLARSSL_CIPHER_CAMELLIA_256_CFB128,
  64. #endif /* defined(POLARSSL_CIPHER_MODE_CFB) */
  65. #if defined(POLARSSL_CIPHER_MODE_CTR)
  66. POLARSSL_CIPHER_CAMELLIA_128_CTR,
  67. POLARSSL_CIPHER_CAMELLIA_192_CTR,
  68. POLARSSL_CIPHER_CAMELLIA_256_CTR,
  69. #endif /* defined(POLARSSL_CIPHER_MODE_CTR) */
  70. #endif /* defined(POLARSSL_CAMELLIA_C) */
  71. #if defined(POLARSSL_DES_C)
  72. POLARSSL_CIPHER_DES_CBC,
  73. POLARSSL_CIPHER_DES_EDE_CBC,
  74. POLARSSL_CIPHER_DES_EDE3_CBC,
  75. #endif /* defined(POLARSSL_DES_C) */
  76. 0
  77. };
  78. const int *cipher_list( void )
  79. {
  80. return supported_ciphers;
  81. }
  82. const cipher_info_t *cipher_info_from_type( cipher_type_t cipher_type )
  83. {
  84. /* Find static cipher information */
  85. switch ( cipher_type )
  86. {
  87. #if defined(POLARSSL_AES_C)
  88. case POLARSSL_CIPHER_AES_128_CBC:
  89. return &aes_128_cbc_info;
  90. case POLARSSL_CIPHER_AES_192_CBC:
  91. return &aes_192_cbc_info;
  92. case POLARSSL_CIPHER_AES_256_CBC:
  93. return &aes_256_cbc_info;
  94. #if defined(POLARSSL_CIPHER_MODE_CFB)
  95. case POLARSSL_CIPHER_AES_128_CFB128:
  96. return &aes_128_cfb128_info;
  97. case POLARSSL_CIPHER_AES_192_CFB128:
  98. return &aes_192_cfb128_info;
  99. case POLARSSL_CIPHER_AES_256_CFB128:
  100. return &aes_256_cfb128_info;
  101. #endif /* defined(POLARSSL_CIPHER_MODE_CFB) */
  102. #if defined(POLARSSL_CIPHER_MODE_CTR)
  103. case POLARSSL_CIPHER_AES_128_CTR:
  104. return &aes_128_ctr_info;
  105. case POLARSSL_CIPHER_AES_192_CTR:
  106. return &aes_192_ctr_info;
  107. case POLARSSL_CIPHER_AES_256_CTR:
  108. return &aes_256_ctr_info;
  109. #endif /* defined(POLARSSL_CIPHER_MODE_CTR) */
  110. #endif
  111. #if defined(POLARSSL_CAMELLIA_C)
  112. case POLARSSL_CIPHER_CAMELLIA_128_CBC:
  113. return &camellia_128_cbc_info;
  114. case POLARSSL_CIPHER_CAMELLIA_192_CBC:
  115. return &camellia_192_cbc_info;
  116. case POLARSSL_CIPHER_CAMELLIA_256_CBC:
  117. return &camellia_256_cbc_info;
  118. #if defined(POLARSSL_CIPHER_MODE_CFB)
  119. case POLARSSL_CIPHER_CAMELLIA_128_CFB128:
  120. return &camellia_128_cfb128_info;
  121. case POLARSSL_CIPHER_CAMELLIA_192_CFB128:
  122. return &camellia_192_cfb128_info;
  123. case POLARSSL_CIPHER_CAMELLIA_256_CFB128:
  124. return &camellia_256_cfb128_info;
  125. #endif /* defined(POLARSSL_CIPHER_MODE_CFB) */
  126. #if defined(POLARSSL_CIPHER_MODE_CTR)
  127. case POLARSSL_CIPHER_CAMELLIA_128_CTR:
  128. return &camellia_128_ctr_info;
  129. case POLARSSL_CIPHER_CAMELLIA_192_CTR:
  130. return &camellia_192_ctr_info;
  131. case POLARSSL_CIPHER_CAMELLIA_256_CTR:
  132. return &camellia_256_ctr_info;
  133. #endif /* defined(POLARSSL_CIPHER_MODE_CTR) */
  134. #endif
  135. #if defined(POLARSSL_DES_C)
  136. case POLARSSL_CIPHER_DES_CBC:
  137. return &des_cbc_info;
  138. case POLARSSL_CIPHER_DES_EDE_CBC:
  139. return &des_ede_cbc_info;
  140. case POLARSSL_CIPHER_DES_EDE3_CBC:
  141. return &des_ede3_cbc_info;
  142. #endif
  143. default:
  144. return NULL;
  145. }
  146. }
  147. const cipher_info_t *cipher_info_from_string( const char *cipher_name )
  148. {
  149. if( NULL == cipher_name )
  150. return NULL;
  151. /* Get the appropriate cipher information */
  152. #if defined(POLARSSL_CAMELLIA_C)
  153. if( !strcasecmp( "CAMELLIA-128-CBC", cipher_name ) )
  154. return cipher_info_from_type( POLARSSL_CIPHER_CAMELLIA_128_CBC );
  155. if( !strcasecmp( "CAMELLIA-192-CBC", cipher_name ) )
  156. return cipher_info_from_type( POLARSSL_CIPHER_CAMELLIA_192_CBC );
  157. if( !strcasecmp( "CAMELLIA-256-CBC", cipher_name ) )
  158. return cipher_info_from_type( POLARSSL_CIPHER_CAMELLIA_256_CBC );
  159. #if defined(POLARSSL_CIPHER_MODE_CFB)
  160. if( !strcasecmp( "CAMELLIA-128-CFB128", cipher_name ) )
  161. return cipher_info_from_type( POLARSSL_CIPHER_CAMELLIA_128_CFB128 );
  162. if( !strcasecmp( "CAMELLIA-192-CFB128", cipher_name ) )
  163. return cipher_info_from_type( POLARSSL_CIPHER_CAMELLIA_192_CFB128 );
  164. if( !strcasecmp( "CAMELLIA-256-CFB128", cipher_name ) )
  165. return cipher_info_from_type( POLARSSL_CIPHER_CAMELLIA_256_CFB128 );
  166. #endif /* defined(POLARSSL_CIPHER_MODE_CFB) */
  167. #if defined(POLARSSL_CIPHER_MODE_CTR)
  168. if( !strcasecmp( "CAMELLIA-128-CTR", cipher_name ) )
  169. return cipher_info_from_type( POLARSSL_CIPHER_CAMELLIA_128_CTR );
  170. if( !strcasecmp( "CAMELLIA-192-CTR", cipher_name ) )
  171. return cipher_info_from_type( POLARSSL_CIPHER_CAMELLIA_192_CTR );
  172. if( !strcasecmp( "CAMELLIA-256-CTR", cipher_name ) )
  173. return cipher_info_from_type( POLARSSL_CIPHER_CAMELLIA_256_CTR );
  174. #endif /* defined(POLARSSL_CIPHER_MODE_CTR) */
  175. #endif
  176. #if defined(POLARSSL_AES_C)
  177. if( !strcasecmp( "AES-128-CBC", cipher_name ) )
  178. return cipher_info_from_type( POLARSSL_CIPHER_AES_128_CBC );
  179. if( !strcasecmp( "AES-192-CBC", cipher_name ) )
  180. return cipher_info_from_type( POLARSSL_CIPHER_AES_192_CBC );
  181. if( !strcasecmp( "AES-256-CBC", cipher_name ) )
  182. return cipher_info_from_type( POLARSSL_CIPHER_AES_256_CBC );
  183. #if defined(POLARSSL_CIPHER_MODE_CFB)
  184. if( !strcasecmp( "AES-128-CFB128", cipher_name ) )
  185. return cipher_info_from_type( POLARSSL_CIPHER_AES_128_CFB128 );
  186. if( !strcasecmp( "AES-192-CFB128", cipher_name ) )
  187. return cipher_info_from_type( POLARSSL_CIPHER_AES_192_CFB128 );
  188. if( !strcasecmp( "AES-256-CFB128", cipher_name ) )
  189. return cipher_info_from_type( POLARSSL_CIPHER_AES_256_CFB128 );
  190. #endif /* defined(POLARSSL_CIPHER_MODE_CFB) */
  191. #if defined(POLARSSL_CIPHER_MODE_CTR)
  192. if( !strcasecmp( "AES-128-CTR", cipher_name ) )
  193. return cipher_info_from_type( POLARSSL_CIPHER_AES_128_CTR );
  194. if( !strcasecmp( "AES-192-CTR", cipher_name ) )
  195. return cipher_info_from_type( POLARSSL_CIPHER_AES_192_CTR );
  196. if( !strcasecmp( "AES-256-CTR", cipher_name ) )
  197. return cipher_info_from_type( POLARSSL_CIPHER_AES_256_CTR );
  198. #endif /* defined(POLARSSL_CIPHER_MODE_CTR) */
  199. #endif
  200. #if defined(POLARSSL_DES_C)
  201. if( !strcasecmp( "DES-CBC", cipher_name ) )
  202. return cipher_info_from_type( POLARSSL_CIPHER_DES_CBC );
  203. if( !strcasecmp( "DES-EDE-CBC", cipher_name ) )
  204. return cipher_info_from_type( POLARSSL_CIPHER_DES_EDE_CBC );
  205. if( !strcasecmp( "DES-EDE3-CBC", cipher_name ) )
  206. return cipher_info_from_type( POLARSSL_CIPHER_DES_EDE3_CBC );
  207. #endif
  208. return NULL;
  209. }
  210. int cipher_init_ctx( cipher_context_t *ctx, const cipher_info_t *cipher_info )
  211. {
  212. if( NULL == cipher_info || NULL == ctx )
  213. return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
  214. memset( ctx, 0, sizeof( ctx ) );
  215. if( NULL == ( ctx->cipher_ctx = cipher_info->base->ctx_alloc_func() ) )
  216. return POLARSSL_ERR_CIPHER_ALLOC_FAILED;
  217. ctx->cipher_info = cipher_info;
  218. return 0;
  219. }
  220. int cipher_free_ctx( cipher_context_t *ctx )
  221. {
  222. if( ctx == NULL || ctx->cipher_info == NULL )
  223. return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
  224. ctx->cipher_info->base->ctx_free_func( ctx->cipher_ctx );
  225. return 0;
  226. }
  227. int cipher_setkey( cipher_context_t *ctx, const unsigned char *key,
  228. int key_length, const operation_t operation )
  229. {
  230. if( NULL == ctx || NULL == ctx->cipher_info )
  231. return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
  232. ctx->key_length = key_length;
  233. ctx->operation = operation;
  234. /*
  235. * For CFB128 and CTR mode always use the encryption key schedule
  236. */
  237. if( POLARSSL_ENCRYPT == operation ||
  238. POLARSSL_MODE_CFB128 == ctx->cipher_info->mode ||
  239. POLARSSL_MODE_CTR == ctx->cipher_info->mode )
  240. {
  241. return ctx->cipher_info->base->setkey_enc_func( ctx->cipher_ctx, key,
  242. ctx->key_length );
  243. }
  244. if( POLARSSL_DECRYPT == operation )
  245. return ctx->cipher_info->base->setkey_dec_func( ctx->cipher_ctx, key,
  246. ctx->key_length );
  247. return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
  248. }
  249. int cipher_reset( cipher_context_t *ctx, const unsigned char *iv )
  250. {
  251. if( NULL == ctx || NULL == ctx->cipher_info || NULL == iv )
  252. return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
  253. ctx->unprocessed_len = 0;
  254. memcpy( ctx->iv, iv, cipher_get_iv_size( ctx ) );
  255. return 0;
  256. }
  257. int cipher_update( cipher_context_t *ctx, const unsigned char *input, size_t ilen,
  258. unsigned char *output, size_t *olen )
  259. {
  260. int ret;
  261. size_t copy_len = 0;
  262. if( NULL == ctx || NULL == ctx->cipher_info || NULL == olen ||
  263. input == output )
  264. {
  265. return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
  266. }
  267. *olen = 0;
  268. if( ctx->cipher_info->mode == POLARSSL_MODE_CBC )
  269. {
  270. /*
  271. * If there is not enough data for a full block, cache it.
  272. */
  273. if( ( ctx->operation == POLARSSL_DECRYPT &&
  274. ilen + ctx->unprocessed_len <= cipher_get_block_size( ctx ) ) ||
  275. ( ctx->operation == POLARSSL_ENCRYPT &&
  276. ilen + ctx->unprocessed_len < cipher_get_block_size( ctx ) ) )
  277. {
  278. memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ), input,
  279. ilen );
  280. ctx->unprocessed_len += ilen;
  281. return 0;
  282. }
  283. /*
  284. * Process cached data first
  285. */
  286. if( ctx->unprocessed_len != 0 )
  287. {
  288. copy_len = cipher_get_block_size( ctx ) - ctx->unprocessed_len;
  289. memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ), input,
  290. copy_len );
  291. if( 0 != ( ret = ctx->cipher_info->base->cbc_func( ctx->cipher_ctx,
  292. ctx->operation, cipher_get_block_size( ctx ), ctx->iv,
  293. ctx->unprocessed_data, output ) ) )
  294. {
  295. return ret;
  296. }
  297. *olen += cipher_get_block_size( ctx );
  298. output += cipher_get_block_size( ctx );
  299. ctx->unprocessed_len = 0;
  300. input += copy_len;
  301. ilen -= copy_len;
  302. }
  303. /*
  304. * Cache final, incomplete block
  305. */
  306. if( 0 != ilen )
  307. {
  308. copy_len = ilen % cipher_get_block_size( ctx );
  309. if( copy_len == 0 && ctx->operation == POLARSSL_DECRYPT )
  310. copy_len = cipher_get_block_size(ctx);
  311. memcpy( ctx->unprocessed_data, &( input[ilen - copy_len] ),
  312. copy_len );
  313. ctx->unprocessed_len += copy_len;
  314. ilen -= copy_len;
  315. }
  316. /*
  317. * Process remaining full blocks
  318. */
  319. if( ilen )
  320. {
  321. if( 0 != ( ret = ctx->cipher_info->base->cbc_func( ctx->cipher_ctx,
  322. ctx->operation, ilen, ctx->iv, input, output ) ) )
  323. {
  324. return ret;
  325. }
  326. *olen += ilen;
  327. }
  328. return 0;
  329. }
  330. if( ctx->cipher_info->mode == POLARSSL_MODE_CFB128 )
  331. {
  332. if( 0 != ( ret = ctx->cipher_info->base->cfb128_func( ctx->cipher_ctx,
  333. ctx->operation, ilen, &ctx->unprocessed_len, ctx->iv,
  334. input, output ) ) )
  335. {
  336. return ret;
  337. }
  338. *olen = ilen;
  339. return 0;
  340. }
  341. if( ctx->cipher_info->mode == POLARSSL_MODE_CTR )
  342. {
  343. if( 0 != ( ret = ctx->cipher_info->base->ctr_func( ctx->cipher_ctx,
  344. ilen, &ctx->unprocessed_len, ctx->iv,
  345. ctx->unprocessed_data, input, output ) ) )
  346. {
  347. return ret;
  348. }
  349. *olen = ilen;
  350. return 0;
  351. }
  352. return POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE;
  353. }
  354. static void add_pkcs_padding( unsigned char *output, size_t output_len,
  355. size_t data_len )
  356. {
  357. size_t padding_len = output_len - data_len;
  358. unsigned char i = 0;
  359. for( i = 0; i < padding_len; i++ )
  360. output[data_len + i] = (unsigned char) padding_len;
  361. }
  362. static int get_pkcs_padding( unsigned char *input, unsigned char input_len,
  363. size_t *data_len)
  364. {
  365. int i = 0;
  366. unsigned char padding_len = 0;
  367. if( NULL == input || NULL == data_len )
  368. return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
  369. padding_len = input[input_len - 1];
  370. if( padding_len > input_len )
  371. return POLARSSL_ERR_CIPHER_INVALID_PADDING;
  372. for( i = input_len - padding_len; i < input_len; i++ )
  373. if( input[i] != padding_len )
  374. return POLARSSL_ERR_CIPHER_INVALID_PADDING;
  375. *data_len = input_len - padding_len;
  376. return 0;
  377. }
  378. int cipher_finish( cipher_context_t *ctx, unsigned char *output, size_t *olen)
  379. {
  380. int ret = 0;
  381. if( NULL == ctx || NULL == ctx->cipher_info || NULL == olen )
  382. return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
  383. *olen = 0;
  384. if( POLARSSL_MODE_CFB128 == ctx->cipher_info->mode ||
  385. POLARSSL_MODE_CTR == ctx->cipher_info->mode )
  386. {
  387. return 0;
  388. }
  389. if( POLARSSL_MODE_CBC == ctx->cipher_info->mode )
  390. {
  391. if( POLARSSL_ENCRYPT == ctx->operation )
  392. {
  393. add_pkcs_padding( ctx->unprocessed_data, cipher_get_iv_size( ctx ),
  394. ctx->unprocessed_len );
  395. }
  396. else if ( cipher_get_block_size( ctx ) != ctx->unprocessed_len )
  397. {
  398. /* For decrypt operations, expect a full block */
  399. return POLARSSL_ERR_CIPHER_FULL_BLOCK_EXPECTED;
  400. }
  401. /* cipher block */
  402. if( 0 != ( ret = ctx->cipher_info->base->cbc_func( ctx->cipher_ctx,
  403. ctx->operation, cipher_get_block_size( ctx ), ctx->iv,
  404. ctx->unprocessed_data, output ) ) )
  405. {
  406. return ret;
  407. }
  408. /* Set output size for decryption */
  409. if( POLARSSL_DECRYPT == ctx->operation )
  410. return get_pkcs_padding( output, cipher_get_block_size( ctx ), olen );
  411. /* Set output size for encryption */
  412. *olen = cipher_get_block_size( ctx );
  413. return 0;
  414. }
  415. return POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE;
  416. }
  417. #if defined(POLARSSL_SELF_TEST)
  418. #ifdef PRINTF_STDLIB
  419. #include <stdio.h>
  420. #endif
  421. #ifdef PRINTF_CUSTOM
  422. #include "tinystdio.h"
  423. #endif
  424. #define ASSERT(x) if (!(x)) { \
  425. printf( "failed with %i at %s\n", value, (#x) ); \
  426. return( 1 ); \
  427. }
  428. /*
  429. * Checkup routine
  430. */
  431. int cipher_self_test( int verbose )
  432. {
  433. ((void) verbose);
  434. return( 0 );
  435. }
  436. #endif
  437. #endif