cipher.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528
  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 _MSC_VER && !defined strcasecmp
  35. #define strcasecmp _stricmp
  36. #endif
  37. static const int supported_ciphers[] = {
  38. #if defined(POLARSSL_AES_C)
  39. POLARSSL_CIPHER_AES_128_CBC,
  40. POLARSSL_CIPHER_AES_192_CBC,
  41. POLARSSL_CIPHER_AES_256_CBC,
  42. #if defined(POLARSSL_CIPHER_MODE_CFB)
  43. POLARSSL_CIPHER_AES_128_CFB128,
  44. POLARSSL_CIPHER_AES_192_CFB128,
  45. POLARSSL_CIPHER_AES_256_CFB128,
  46. #endif /* defined(POLARSSL_CIPHER_MODE_CFB) */
  47. #if defined(POLARSSL_CIPHER_MODE_CTR)
  48. POLARSSL_CIPHER_AES_128_CTR,
  49. POLARSSL_CIPHER_AES_192_CTR,
  50. POLARSSL_CIPHER_AES_256_CTR,
  51. #endif /* defined(POLARSSL_CIPHER_MODE_CTR) */
  52. #endif /* defined(POLARSSL_AES_C) */
  53. #if defined(POLARSSL_CAMELLIA_C)
  54. POLARSSL_CIPHER_CAMELLIA_128_CBC,
  55. POLARSSL_CIPHER_CAMELLIA_192_CBC,
  56. POLARSSL_CIPHER_CAMELLIA_256_CBC,
  57. #if defined(POLARSSL_CIPHER_MODE_CFB)
  58. POLARSSL_CIPHER_CAMELLIA_128_CFB128,
  59. POLARSSL_CIPHER_CAMELLIA_192_CFB128,
  60. POLARSSL_CIPHER_CAMELLIA_256_CFB128,
  61. #endif /* defined(POLARSSL_CIPHER_MODE_CFB) */
  62. #if defined(POLARSSL_CIPHER_MODE_CTR)
  63. POLARSSL_CIPHER_CAMELLIA_128_CTR,
  64. POLARSSL_CIPHER_CAMELLIA_192_CTR,
  65. POLARSSL_CIPHER_CAMELLIA_256_CTR,
  66. #endif /* defined(POLARSSL_CIPHER_MODE_CTR) */
  67. #endif /* defined(POLARSSL_CAMELLIA_C) */
  68. #if defined(POLARSSL_DES_C)
  69. POLARSSL_CIPHER_DES_CBC,
  70. POLARSSL_CIPHER_DES_EDE_CBC,
  71. POLARSSL_CIPHER_DES_EDE3_CBC,
  72. #endif /* defined(POLARSSL_DES_C) */
  73. 0
  74. };
  75. const int *cipher_list( void )
  76. {
  77. return supported_ciphers;
  78. }
  79. const cipher_info_t *cipher_info_from_type( cipher_type_t cipher_type )
  80. {
  81. /* Find static cipher information */
  82. switch ( cipher_type )
  83. {
  84. #if defined(POLARSSL_AES_C)
  85. case POLARSSL_CIPHER_AES_128_CBC:
  86. return &aes_128_cbc_info;
  87. case POLARSSL_CIPHER_AES_192_CBC:
  88. return &aes_192_cbc_info;
  89. case POLARSSL_CIPHER_AES_256_CBC:
  90. return &aes_256_cbc_info;
  91. #if defined(POLARSSL_CIPHER_MODE_CFB)
  92. case POLARSSL_CIPHER_AES_128_CFB128:
  93. return &aes_128_cfb128_info;
  94. case POLARSSL_CIPHER_AES_192_CFB128:
  95. return &aes_192_cfb128_info;
  96. case POLARSSL_CIPHER_AES_256_CFB128:
  97. return &aes_256_cfb128_info;
  98. #endif /* defined(POLARSSL_CIPHER_MODE_CFB) */
  99. #if defined(POLARSSL_CIPHER_MODE_CTR)
  100. case POLARSSL_CIPHER_AES_128_CTR:
  101. return &aes_128_ctr_info;
  102. case POLARSSL_CIPHER_AES_192_CTR:
  103. return &aes_192_ctr_info;
  104. case POLARSSL_CIPHER_AES_256_CTR:
  105. return &aes_256_ctr_info;
  106. #endif /* defined(POLARSSL_CIPHER_MODE_CTR) */
  107. #endif
  108. #if defined(POLARSSL_CAMELLIA_C)
  109. case POLARSSL_CIPHER_CAMELLIA_128_CBC:
  110. return &camellia_128_cbc_info;
  111. case POLARSSL_CIPHER_CAMELLIA_192_CBC:
  112. return &camellia_192_cbc_info;
  113. case POLARSSL_CIPHER_CAMELLIA_256_CBC:
  114. return &camellia_256_cbc_info;
  115. #if defined(POLARSSL_CIPHER_MODE_CFB)
  116. case POLARSSL_CIPHER_CAMELLIA_128_CFB128:
  117. return &camellia_128_cfb128_info;
  118. case POLARSSL_CIPHER_CAMELLIA_192_CFB128:
  119. return &camellia_192_cfb128_info;
  120. case POLARSSL_CIPHER_CAMELLIA_256_CFB128:
  121. return &camellia_256_cfb128_info;
  122. #endif /* defined(POLARSSL_CIPHER_MODE_CFB) */
  123. #if defined(POLARSSL_CIPHER_MODE_CTR)
  124. case POLARSSL_CIPHER_CAMELLIA_128_CTR:
  125. return &camellia_128_ctr_info;
  126. case POLARSSL_CIPHER_CAMELLIA_192_CTR:
  127. return &camellia_192_ctr_info;
  128. case POLARSSL_CIPHER_CAMELLIA_256_CTR:
  129. return &camellia_256_ctr_info;
  130. #endif /* defined(POLARSSL_CIPHER_MODE_CTR) */
  131. #endif
  132. #if defined(POLARSSL_DES_C)
  133. case POLARSSL_CIPHER_DES_CBC:
  134. return &des_cbc_info;
  135. case POLARSSL_CIPHER_DES_EDE_CBC:
  136. return &des_ede_cbc_info;
  137. case POLARSSL_CIPHER_DES_EDE3_CBC:
  138. return &des_ede3_cbc_info;
  139. #endif
  140. default:
  141. return NULL;
  142. }
  143. }
  144. const cipher_info_t *cipher_info_from_string( const char *cipher_name )
  145. {
  146. if( NULL == cipher_name )
  147. return NULL;
  148. /* Get the appropriate cipher information */
  149. #if defined(POLARSSL_CAMELLIA_C)
  150. if( !strcasecmp( "CAMELLIA-128-CBC", cipher_name ) )
  151. return cipher_info_from_type( POLARSSL_CIPHER_CAMELLIA_128_CBC );
  152. if( !strcasecmp( "CAMELLIA-192-CBC", cipher_name ) )
  153. return cipher_info_from_type( POLARSSL_CIPHER_CAMELLIA_192_CBC );
  154. if( !strcasecmp( "CAMELLIA-256-CBC", cipher_name ) )
  155. return cipher_info_from_type( POLARSSL_CIPHER_CAMELLIA_256_CBC );
  156. #if defined(POLARSSL_CIPHER_MODE_CFB)
  157. if( !strcasecmp( "CAMELLIA-128-CFB128", cipher_name ) )
  158. return cipher_info_from_type( POLARSSL_CIPHER_CAMELLIA_128_CFB128 );
  159. if( !strcasecmp( "CAMELLIA-192-CFB128", cipher_name ) )
  160. return cipher_info_from_type( POLARSSL_CIPHER_CAMELLIA_192_CFB128 );
  161. if( !strcasecmp( "CAMELLIA-256-CFB128", cipher_name ) )
  162. return cipher_info_from_type( POLARSSL_CIPHER_CAMELLIA_256_CFB128 );
  163. #endif /* defined(POLARSSL_CIPHER_MODE_CFB) */
  164. #if defined(POLARSSL_CIPHER_MODE_CTR)
  165. if( !strcasecmp( "CAMELLIA-128-CTR", cipher_name ) )
  166. return cipher_info_from_type( POLARSSL_CIPHER_CAMELLIA_128_CTR );
  167. if( !strcasecmp( "CAMELLIA-192-CTR", cipher_name ) )
  168. return cipher_info_from_type( POLARSSL_CIPHER_CAMELLIA_192_CTR );
  169. if( !strcasecmp( "CAMELLIA-256-CTR", cipher_name ) )
  170. return cipher_info_from_type( POLARSSL_CIPHER_CAMELLIA_256_CTR );
  171. #endif /* defined(POLARSSL_CIPHER_MODE_CTR) */
  172. #endif
  173. #if defined(POLARSSL_AES_C)
  174. if( !strcasecmp( "AES-128-CBC", cipher_name ) )
  175. return cipher_info_from_type( POLARSSL_CIPHER_AES_128_CBC );
  176. if( !strcasecmp( "AES-192-CBC", cipher_name ) )
  177. return cipher_info_from_type( POLARSSL_CIPHER_AES_192_CBC );
  178. if( !strcasecmp( "AES-256-CBC", cipher_name ) )
  179. return cipher_info_from_type( POLARSSL_CIPHER_AES_256_CBC );
  180. #if defined(POLARSSL_CIPHER_MODE_CFB)
  181. if( !strcasecmp( "AES-128-CFB128", cipher_name ) )
  182. return cipher_info_from_type( POLARSSL_CIPHER_AES_128_CFB128 );
  183. if( !strcasecmp( "AES-192-CFB128", cipher_name ) )
  184. return cipher_info_from_type( POLARSSL_CIPHER_AES_192_CFB128 );
  185. if( !strcasecmp( "AES-256-CFB128", cipher_name ) )
  186. return cipher_info_from_type( POLARSSL_CIPHER_AES_256_CFB128 );
  187. #endif /* defined(POLARSSL_CIPHER_MODE_CFB) */
  188. #if defined(POLARSSL_CIPHER_MODE_CTR)
  189. if( !strcasecmp( "AES-128-CTR", cipher_name ) )
  190. return cipher_info_from_type( POLARSSL_CIPHER_AES_128_CTR );
  191. if( !strcasecmp( "AES-192-CTR", cipher_name ) )
  192. return cipher_info_from_type( POLARSSL_CIPHER_AES_192_CTR );
  193. if( !strcasecmp( "AES-256-CTR", cipher_name ) )
  194. return cipher_info_from_type( POLARSSL_CIPHER_AES_256_CTR );
  195. #endif /* defined(POLARSSL_CIPHER_MODE_CTR) */
  196. #endif
  197. #if defined(POLARSSL_DES_C)
  198. if( !strcasecmp( "DES-CBC", cipher_name ) )
  199. return cipher_info_from_type( POLARSSL_CIPHER_DES_CBC );
  200. if( !strcasecmp( "DES-EDE-CBC", cipher_name ) )
  201. return cipher_info_from_type( POLARSSL_CIPHER_DES_EDE_CBC );
  202. if( !strcasecmp( "DES-EDE3-CBC", cipher_name ) )
  203. return cipher_info_from_type( POLARSSL_CIPHER_DES_EDE3_CBC );
  204. #endif
  205. return NULL;
  206. }
  207. int cipher_init_ctx( cipher_context_t *ctx, const cipher_info_t *cipher_info )
  208. {
  209. if( NULL == cipher_info || NULL == ctx )
  210. return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
  211. memset( ctx, 0, sizeof( ctx ) );
  212. if( NULL == ( ctx->cipher_ctx = cipher_info->base->ctx_alloc_func() ) )
  213. return POLARSSL_ERR_CIPHER_ALLOC_FAILED;
  214. ctx->cipher_info = cipher_info;
  215. return 0;
  216. }
  217. int cipher_free_ctx( cipher_context_t *ctx )
  218. {
  219. if( ctx == NULL || ctx->cipher_info == NULL )
  220. return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
  221. ctx->cipher_info->base->ctx_free_func( ctx->cipher_ctx );
  222. return 0;
  223. }
  224. int cipher_setkey( cipher_context_t *ctx, const unsigned char *key,
  225. int key_length, const operation_t operation )
  226. {
  227. if( NULL == ctx || NULL == ctx->cipher_info )
  228. return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
  229. ctx->key_length = key_length;
  230. ctx->operation = operation;
  231. /*
  232. * For CFB128 and CTR mode always use the encryption key schedule
  233. */
  234. if( POLARSSL_ENCRYPT == operation ||
  235. POLARSSL_MODE_CFB128 == ctx->cipher_info->mode ||
  236. POLARSSL_MODE_CTR == ctx->cipher_info->mode )
  237. {
  238. return ctx->cipher_info->base->setkey_enc_func( ctx->cipher_ctx, key,
  239. ctx->key_length );
  240. }
  241. if( POLARSSL_DECRYPT == operation )
  242. return ctx->cipher_info->base->setkey_dec_func( ctx->cipher_ctx, key,
  243. ctx->key_length );
  244. return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
  245. }
  246. int cipher_reset( cipher_context_t *ctx, const unsigned char *iv )
  247. {
  248. if( NULL == ctx || NULL == ctx->cipher_info || NULL == iv )
  249. return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
  250. ctx->unprocessed_len = 0;
  251. memcpy( ctx->iv, iv, cipher_get_iv_size( ctx ) );
  252. return 0;
  253. }
  254. int cipher_update( cipher_context_t *ctx, const unsigned char *input, size_t ilen,
  255. unsigned char *output, size_t *olen )
  256. {
  257. int ret;
  258. size_t copy_len = 0;
  259. if( NULL == ctx || NULL == ctx->cipher_info || NULL == olen ||
  260. input == output )
  261. {
  262. return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
  263. }
  264. *olen = 0;
  265. if( ctx->cipher_info->mode == POLARSSL_MODE_CBC )
  266. {
  267. /*
  268. * If there is not enough data for a full block, cache it.
  269. */
  270. if( ( ctx->operation == POLARSSL_DECRYPT &&
  271. ilen + ctx->unprocessed_len <= cipher_get_block_size( ctx ) ) ||
  272. ( ctx->operation == POLARSSL_ENCRYPT &&
  273. ilen + ctx->unprocessed_len < cipher_get_block_size( ctx ) ) )
  274. {
  275. memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ), input,
  276. ilen );
  277. ctx->unprocessed_len += ilen;
  278. return 0;
  279. }
  280. /*
  281. * Process cached data first
  282. */
  283. if( ctx->unprocessed_len != 0 )
  284. {
  285. copy_len = cipher_get_block_size( ctx ) - ctx->unprocessed_len;
  286. memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ), input,
  287. copy_len );
  288. if( 0 != ( ret = ctx->cipher_info->base->cbc_func( ctx->cipher_ctx,
  289. ctx->operation, cipher_get_block_size( ctx ), ctx->iv,
  290. ctx->unprocessed_data, output ) ) )
  291. {
  292. return ret;
  293. }
  294. *olen += cipher_get_block_size( ctx );
  295. output += cipher_get_block_size( ctx );
  296. ctx->unprocessed_len = 0;
  297. input += copy_len;
  298. ilen -= copy_len;
  299. }
  300. /*
  301. * Cache final, incomplete block
  302. */
  303. if( 0 != ilen )
  304. {
  305. copy_len = ilen % cipher_get_block_size( ctx );
  306. if( copy_len == 0 && ctx->operation == POLARSSL_DECRYPT )
  307. copy_len = cipher_get_block_size(ctx);
  308. memcpy( ctx->unprocessed_data, &( input[ilen - copy_len] ),
  309. copy_len );
  310. ctx->unprocessed_len += copy_len;
  311. ilen -= copy_len;
  312. }
  313. /*
  314. * Process remaining full blocks
  315. */
  316. if( ilen )
  317. {
  318. if( 0 != ( ret = ctx->cipher_info->base->cbc_func( ctx->cipher_ctx,
  319. ctx->operation, ilen, ctx->iv, input, output ) ) )
  320. {
  321. return ret;
  322. }
  323. *olen += ilen;
  324. }
  325. return 0;
  326. }
  327. if( ctx->cipher_info->mode == POLARSSL_MODE_CFB128 )
  328. {
  329. if( 0 != ( ret = ctx->cipher_info->base->cfb128_func( ctx->cipher_ctx,
  330. ctx->operation, ilen, &ctx->unprocessed_len, ctx->iv,
  331. input, output ) ) )
  332. {
  333. return ret;
  334. }
  335. *olen = ilen;
  336. return 0;
  337. }
  338. if( ctx->cipher_info->mode == POLARSSL_MODE_CTR )
  339. {
  340. if( 0 != ( ret = ctx->cipher_info->base->ctr_func( ctx->cipher_ctx,
  341. ilen, &ctx->unprocessed_len, ctx->iv,
  342. ctx->unprocessed_data, input, output ) ) )
  343. {
  344. return ret;
  345. }
  346. *olen = ilen;
  347. return 0;
  348. }
  349. return POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE;
  350. }
  351. static void add_pkcs_padding( unsigned char *output, size_t output_len,
  352. size_t data_len )
  353. {
  354. size_t padding_len = output_len - data_len;
  355. unsigned char i = 0;
  356. for( i = 0; i < padding_len; i++ )
  357. output[data_len + i] = (unsigned char) padding_len;
  358. }
  359. static int get_pkcs_padding( unsigned char *input, unsigned char input_len,
  360. size_t *data_len)
  361. {
  362. int i = 0;
  363. unsigned char padding_len = 0;
  364. if( NULL == input || NULL == data_len )
  365. return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
  366. padding_len = input[input_len - 1];
  367. if( padding_len > input_len )
  368. return POLARSSL_ERR_CIPHER_INVALID_PADDING;
  369. for( i = input_len - padding_len; i < input_len; i++ )
  370. if( input[i] != padding_len )
  371. return POLARSSL_ERR_CIPHER_INVALID_PADDING;
  372. *data_len = input_len - padding_len;
  373. return 0;
  374. }
  375. int cipher_finish( cipher_context_t *ctx, unsigned char *output, size_t *olen)
  376. {
  377. int ret = 0;
  378. if( NULL == ctx || NULL == ctx->cipher_info || NULL == olen )
  379. return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
  380. *olen = 0;
  381. if( POLARSSL_MODE_CFB128 == ctx->cipher_info->mode ||
  382. POLARSSL_MODE_CTR == ctx->cipher_info->mode )
  383. {
  384. return 0;
  385. }
  386. if( POLARSSL_MODE_CBC == ctx->cipher_info->mode )
  387. {
  388. if( POLARSSL_ENCRYPT == ctx->operation )
  389. {
  390. add_pkcs_padding( ctx->unprocessed_data, cipher_get_iv_size( ctx ),
  391. ctx->unprocessed_len );
  392. }
  393. else if ( cipher_get_block_size( ctx ) != ctx->unprocessed_len )
  394. {
  395. /* For decrypt operations, expect a full block */
  396. return POLARSSL_ERR_CIPHER_FULL_BLOCK_EXPECTED;
  397. }
  398. /* cipher block */
  399. if( 0 != ( ret = ctx->cipher_info->base->cbc_func( ctx->cipher_ctx,
  400. ctx->operation, cipher_get_block_size( ctx ), ctx->iv,
  401. ctx->unprocessed_data, output ) ) )
  402. {
  403. return ret;
  404. }
  405. /* Set output size for decryption */
  406. if( POLARSSL_DECRYPT == ctx->operation )
  407. return get_pkcs_padding( output, cipher_get_block_size( ctx ), olen );
  408. /* Set output size for encryption */
  409. *olen = cipher_get_block_size( ctx );
  410. return 0;
  411. }
  412. return POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE;
  413. }
  414. #if defined(POLARSSL_SELF_TEST)
  415. #include <stdio.h>
  416. #define ASSERT(x) if (!(x)) { \
  417. printf( "failed with %i at %s\n", value, (#x) ); \
  418. return( 1 ); \
  419. }
  420. /*
  421. * Checkup routine
  422. */
  423. int cipher_self_test( int verbose )
  424. {
  425. ((void) verbose);
  426. return( 0 );
  427. }
  428. #endif
  429. #endif