cipher.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918
  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-2014, 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. #if !defined(POLARSSL_CONFIG_FILE)
  30. #include "polarssl/config.h"
  31. #else
  32. #include POLARSSL_CONFIG_FILE
  33. #endif
  34. #if defined(POLARSSL_CIPHER_C)
  35. #include "polarssl/cipher.h"
  36. #include "polarssl/cipher_wrap.h"
  37. #if defined(POLARSSL_GCM_C)
  38. #include "polarssl/gcm.h"
  39. #endif
  40. #if defined(POLARSSL_CCM_C)
  41. #include "polarssl/ccm.h"
  42. #endif
  43. #include <stdlib.h>
  44. #if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER)
  45. #define POLARSSL_CIPHER_MODE_STREAM
  46. #endif
  47. #if defined(_MSC_VER) && !defined strcasecmp && !defined(EFIX64) && \
  48. !defined(EFI32)
  49. #define strcasecmp _stricmp
  50. #endif
  51. /* Implementation that should never be optimized out by the compiler */
  52. static void polarssl_zeroize( void *v, size_t n ) {
  53. volatile unsigned char *p = v; while( n-- ) *p++ = 0;
  54. }
  55. static int supported_init = 0;
  56. const int *cipher_list( void )
  57. {
  58. const cipher_definition_t *def;
  59. int *type;
  60. if( ! supported_init )
  61. {
  62. def = cipher_definitions;
  63. type = supported_ciphers;
  64. while( def->type != 0 )
  65. *type++ = (*def++).type;
  66. *type = 0;
  67. supported_init = 1;
  68. }
  69. return( supported_ciphers );
  70. }
  71. const cipher_info_t *cipher_info_from_type( const cipher_type_t cipher_type )
  72. {
  73. const cipher_definition_t *def;
  74. for( def = cipher_definitions; def->info != NULL; def++ )
  75. if( def->type == cipher_type )
  76. return( def->info );
  77. return( NULL );
  78. }
  79. const cipher_info_t *cipher_info_from_string( const char *cipher_name )
  80. {
  81. const cipher_definition_t *def;
  82. if( NULL == cipher_name )
  83. return( NULL );
  84. for( def = cipher_definitions; def->info != NULL; def++ )
  85. if( ! strcasecmp( def->info->name, cipher_name ) )
  86. return( def->info );
  87. return( NULL );
  88. }
  89. const cipher_info_t *cipher_info_from_values( const cipher_id_t cipher_id,
  90. int key_length,
  91. const cipher_mode_t mode )
  92. {
  93. const cipher_definition_t *def;
  94. for( def = cipher_definitions; def->info != NULL; def++ )
  95. if( def->info->base->cipher == cipher_id &&
  96. def->info->key_length == (unsigned) key_length &&
  97. def->info->mode == mode )
  98. return( def->info );
  99. return( NULL );
  100. }
  101. void cipher_init( cipher_context_t *ctx )
  102. {
  103. memset( ctx, 0, sizeof( cipher_context_t ) );
  104. }
  105. void cipher_free( cipher_context_t *ctx )
  106. {
  107. if( ctx == NULL )
  108. return;
  109. if( ctx->cipher_ctx )
  110. ctx->cipher_info->base->ctx_free_func( ctx->cipher_ctx );
  111. polarssl_zeroize( ctx, sizeof(cipher_context_t) );
  112. }
  113. int cipher_init_ctx( cipher_context_t *ctx, const cipher_info_t *cipher_info )
  114. {
  115. if( NULL == cipher_info || NULL == ctx )
  116. return( POLARSSL_ERR_CIPHER_BAD_INPUT_DATA );
  117. memset( ctx, 0, sizeof( cipher_context_t ) );
  118. if( NULL == ( ctx->cipher_ctx = cipher_info->base->ctx_alloc_func() ) )
  119. return( POLARSSL_ERR_CIPHER_ALLOC_FAILED );
  120. ctx->cipher_info = cipher_info;
  121. #if defined(POLARSSL_CIPHER_MODE_WITH_PADDING)
  122. /*
  123. * Ignore possible errors caused by a cipher mode that doesn't use padding
  124. */
  125. #if defined(POLARSSL_CIPHER_PADDING_PKCS7)
  126. (void) cipher_set_padding_mode( ctx, POLARSSL_PADDING_PKCS7 );
  127. #else
  128. (void) cipher_set_padding_mode( ctx, POLARSSL_PADDING_NONE );
  129. #endif
  130. #endif /* POLARSSL_CIPHER_MODE_WITH_PADDING */
  131. return( 0 );
  132. }
  133. /* Deprecated, redirects to cipher_free() */
  134. int cipher_free_ctx( cipher_context_t *ctx )
  135. {
  136. cipher_free( ctx );
  137. return( 0 );
  138. }
  139. int cipher_setkey( cipher_context_t *ctx, const unsigned char *key,
  140. int key_length, const operation_t operation )
  141. {
  142. if( NULL == ctx || NULL == ctx->cipher_info )
  143. return( POLARSSL_ERR_CIPHER_BAD_INPUT_DATA );
  144. if( ( ctx->cipher_info->flags & POLARSSL_CIPHER_VARIABLE_KEY_LEN ) == 0 &&
  145. (int) ctx->cipher_info->key_length != key_length )
  146. {
  147. return( POLARSSL_ERR_CIPHER_BAD_INPUT_DATA );
  148. }
  149. ctx->key_length = key_length;
  150. ctx->operation = operation;
  151. /*
  152. * For CFB and CTR mode always use the encryption key schedule
  153. */
  154. if( POLARSSL_ENCRYPT == operation ||
  155. POLARSSL_MODE_CFB == ctx->cipher_info->mode ||
  156. POLARSSL_MODE_CTR == ctx->cipher_info->mode )
  157. {
  158. return ctx->cipher_info->base->setkey_enc_func( ctx->cipher_ctx, key,
  159. ctx->key_length );
  160. }
  161. if( POLARSSL_DECRYPT == operation )
  162. return ctx->cipher_info->base->setkey_dec_func( ctx->cipher_ctx, key,
  163. ctx->key_length );
  164. return( POLARSSL_ERR_CIPHER_BAD_INPUT_DATA );
  165. }
  166. int cipher_set_iv( cipher_context_t *ctx,
  167. const unsigned char *iv, size_t iv_len )
  168. {
  169. size_t actual_iv_size;
  170. if( NULL == ctx || NULL == ctx->cipher_info || NULL == iv )
  171. return( POLARSSL_ERR_CIPHER_BAD_INPUT_DATA );
  172. /* avoid buffer overflow in ctx->iv */
  173. if( iv_len > POLARSSL_MAX_IV_LENGTH )
  174. return( POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE );
  175. if( ( ctx->cipher_info->flags & POLARSSL_CIPHER_VARIABLE_IV_LEN ) != 0 )
  176. actual_iv_size = iv_len;
  177. else
  178. {
  179. actual_iv_size = ctx->cipher_info->iv_size;
  180. /* avoid reading past the end of input buffer */
  181. if( actual_iv_size > iv_len )
  182. return( POLARSSL_ERR_CIPHER_BAD_INPUT_DATA );
  183. }
  184. memcpy( ctx->iv, iv, actual_iv_size );
  185. ctx->iv_size = actual_iv_size;
  186. return( 0 );
  187. }
  188. int cipher_reset( cipher_context_t *ctx )
  189. {
  190. if( NULL == ctx || NULL == ctx->cipher_info )
  191. return( POLARSSL_ERR_CIPHER_BAD_INPUT_DATA );
  192. ctx->unprocessed_len = 0;
  193. return( 0 );
  194. }
  195. #if defined(POLARSSL_GCM_C)
  196. int cipher_update_ad( cipher_context_t *ctx,
  197. const unsigned char *ad, size_t ad_len )
  198. {
  199. if( NULL == ctx || NULL == ctx->cipher_info )
  200. return( POLARSSL_ERR_CIPHER_BAD_INPUT_DATA );
  201. if( POLARSSL_MODE_GCM == ctx->cipher_info->mode )
  202. {
  203. return gcm_starts( (gcm_context *) ctx->cipher_ctx, ctx->operation,
  204. ctx->iv, ctx->iv_size, ad, ad_len );
  205. }
  206. return( 0 );
  207. }
  208. #endif /* POLARSSL_GCM_C */
  209. int cipher_update( cipher_context_t *ctx, const unsigned char *input,
  210. size_t ilen, unsigned char *output, size_t *olen )
  211. {
  212. int ret;
  213. if( NULL == ctx || NULL == ctx->cipher_info || NULL == olen )
  214. {
  215. return( POLARSSL_ERR_CIPHER_BAD_INPUT_DATA );
  216. }
  217. *olen = 0;
  218. if( ctx->cipher_info->mode == POLARSSL_MODE_ECB )
  219. {
  220. if( ilen != cipher_get_block_size( ctx ) )
  221. return( POLARSSL_ERR_CIPHER_FULL_BLOCK_EXPECTED );
  222. *olen = ilen;
  223. if( 0 != ( ret = ctx->cipher_info->base->ecb_func( ctx->cipher_ctx,
  224. ctx->operation, input, output ) ) )
  225. {
  226. return( ret );
  227. }
  228. return( 0 );
  229. }
  230. #if defined(POLARSSL_GCM_C)
  231. if( ctx->cipher_info->mode == POLARSSL_MODE_GCM )
  232. {
  233. *olen = ilen;
  234. return gcm_update( (gcm_context *) ctx->cipher_ctx, ilen, input,
  235. output );
  236. }
  237. #endif
  238. if( input == output &&
  239. ( ctx->unprocessed_len != 0 || ilen % cipher_get_block_size( ctx ) ) )
  240. {
  241. return( POLARSSL_ERR_CIPHER_BAD_INPUT_DATA );
  242. }
  243. #if defined(POLARSSL_CIPHER_MODE_CBC)
  244. if( ctx->cipher_info->mode == POLARSSL_MODE_CBC )
  245. {
  246. size_t copy_len = 0;
  247. /*
  248. * If there is not enough data for a full block, cache it.
  249. */
  250. if( ( ctx->operation == POLARSSL_DECRYPT &&
  251. ilen + ctx->unprocessed_len <= cipher_get_block_size( ctx ) ) ||
  252. ( ctx->operation == POLARSSL_ENCRYPT &&
  253. ilen + ctx->unprocessed_len < cipher_get_block_size( ctx ) ) )
  254. {
  255. memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ), input,
  256. ilen );
  257. ctx->unprocessed_len += ilen;
  258. return( 0 );
  259. }
  260. /*
  261. * Process cached data first
  262. */
  263. if( ctx->unprocessed_len != 0 )
  264. {
  265. copy_len = cipher_get_block_size( ctx ) - ctx->unprocessed_len;
  266. memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ), input,
  267. copy_len );
  268. if( 0 != ( ret = ctx->cipher_info->base->cbc_func( ctx->cipher_ctx,
  269. ctx->operation, cipher_get_block_size( ctx ), ctx->iv,
  270. ctx->unprocessed_data, output ) ) )
  271. {
  272. return( ret );
  273. }
  274. *olen += cipher_get_block_size( ctx );
  275. output += cipher_get_block_size( ctx );
  276. ctx->unprocessed_len = 0;
  277. input += copy_len;
  278. ilen -= copy_len;
  279. }
  280. /*
  281. * Cache final, incomplete block
  282. */
  283. if( 0 != ilen )
  284. {
  285. copy_len = ilen % cipher_get_block_size( ctx );
  286. if( copy_len == 0 && ctx->operation == POLARSSL_DECRYPT )
  287. copy_len = cipher_get_block_size( ctx );
  288. memcpy( ctx->unprocessed_data, &( input[ilen - copy_len] ),
  289. copy_len );
  290. ctx->unprocessed_len += copy_len;
  291. ilen -= copy_len;
  292. }
  293. /*
  294. * Process remaining full blocks
  295. */
  296. if( ilen )
  297. {
  298. if( 0 != ( ret = ctx->cipher_info->base->cbc_func( ctx->cipher_ctx,
  299. ctx->operation, ilen, ctx->iv, input, output ) ) )
  300. {
  301. return( ret );
  302. }
  303. *olen += ilen;
  304. }
  305. return( 0 );
  306. }
  307. #endif /* POLARSSL_CIPHER_MODE_CBC */
  308. #if defined(POLARSSL_CIPHER_MODE_CFB)
  309. if( ctx->cipher_info->mode == POLARSSL_MODE_CFB )
  310. {
  311. if( 0 != ( ret = ctx->cipher_info->base->cfb_func( ctx->cipher_ctx,
  312. ctx->operation, ilen, &ctx->unprocessed_len, ctx->iv,
  313. input, output ) ) )
  314. {
  315. return( ret );
  316. }
  317. *olen = ilen;
  318. return( 0 );
  319. }
  320. #endif /* POLARSSL_CIPHER_MODE_CFB */
  321. #if defined(POLARSSL_CIPHER_MODE_CTR)
  322. if( ctx->cipher_info->mode == POLARSSL_MODE_CTR )
  323. {
  324. if( 0 != ( ret = ctx->cipher_info->base->ctr_func( ctx->cipher_ctx,
  325. ilen, &ctx->unprocessed_len, ctx->iv,
  326. ctx->unprocessed_data, input, output ) ) )
  327. {
  328. return( ret );
  329. }
  330. *olen = ilen;
  331. return( 0 );
  332. }
  333. #endif /* POLARSSL_CIPHER_MODE_CTR */
  334. #if defined(POLARSSL_CIPHER_MODE_STREAM)
  335. if( ctx->cipher_info->mode == POLARSSL_MODE_STREAM )
  336. {
  337. if( 0 != ( ret = ctx->cipher_info->base->stream_func( ctx->cipher_ctx,
  338. ilen, input, output ) ) )
  339. {
  340. return( ret );
  341. }
  342. *olen = ilen;
  343. return( 0 );
  344. }
  345. #endif /* POLARSSL_CIPHER_MODE_STREAM */
  346. return( POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE );
  347. }
  348. #if defined(POLARSSL_CIPHER_MODE_WITH_PADDING)
  349. #if defined(POLARSSL_CIPHER_PADDING_PKCS7)
  350. /*
  351. * PKCS7 (and PKCS5) padding: fill with ll bytes, with ll = padding_len
  352. */
  353. static void add_pkcs_padding( unsigned char *output, size_t output_len,
  354. size_t data_len )
  355. {
  356. size_t padding_len = output_len - data_len;
  357. unsigned char i;
  358. for( i = 0; i < padding_len; i++ )
  359. output[data_len + i] = (unsigned char) padding_len;
  360. }
  361. static int get_pkcs_padding( unsigned char *input, size_t input_len,
  362. size_t *data_len )
  363. {
  364. size_t i, pad_idx;
  365. unsigned char padding_len, bad = 0;
  366. if( NULL == input || NULL == data_len )
  367. return( POLARSSL_ERR_CIPHER_BAD_INPUT_DATA );
  368. padding_len = input[input_len - 1];
  369. *data_len = input_len - padding_len;
  370. /* Avoid logical || since it results in a branch */
  371. bad |= padding_len > input_len;
  372. bad |= padding_len == 0;
  373. /* The number of bytes checked must be independent of padding_len,
  374. * so pick input_len, which is usually 8 or 16 (one block) */
  375. pad_idx = input_len - padding_len;
  376. for( i = 0; i < input_len; i++ )
  377. bad |= ( input[i] ^ padding_len ) * ( i >= pad_idx );
  378. return( POLARSSL_ERR_CIPHER_INVALID_PADDING * ( bad != 0 ) );
  379. }
  380. #endif /* POLARSSL_CIPHER_PADDING_PKCS7 */
  381. #if defined(POLARSSL_CIPHER_PADDING_ONE_AND_ZEROS)
  382. /*
  383. * One and zeros padding: fill with 80 00 ... 00
  384. */
  385. static void add_one_and_zeros_padding( unsigned char *output,
  386. size_t output_len, size_t data_len )
  387. {
  388. size_t padding_len = output_len - data_len;
  389. unsigned char i = 0;
  390. output[data_len] = 0x80;
  391. for( i = 1; i < padding_len; i++ )
  392. output[data_len + i] = 0x00;
  393. }
  394. static int get_one_and_zeros_padding( unsigned char *input, size_t input_len,
  395. size_t *data_len )
  396. {
  397. size_t i;
  398. unsigned char done = 0, prev_done, bad;
  399. if( NULL == input || NULL == data_len )
  400. return( POLARSSL_ERR_CIPHER_BAD_INPUT_DATA );
  401. bad = 0xFF;
  402. *data_len = 0;
  403. for( i = input_len; i > 0; i-- )
  404. {
  405. prev_done = done;
  406. done |= ( input[i-1] != 0 );
  407. *data_len |= ( i - 1 ) * ( done != prev_done );
  408. bad &= ( input[i-1] ^ 0x80 ) | ( done == prev_done );
  409. }
  410. return( POLARSSL_ERR_CIPHER_INVALID_PADDING * ( bad != 0 ) );
  411. }
  412. #endif /* POLARSSL_CIPHER_PADDING_ONE_AND_ZEROS */
  413. #if defined(POLARSSL_CIPHER_PADDING_ZEROS_AND_LEN)
  414. /*
  415. * Zeros and len padding: fill with 00 ... 00 ll, where ll is padding length
  416. */
  417. static void add_zeros_and_len_padding( unsigned char *output,
  418. size_t output_len, size_t data_len )
  419. {
  420. size_t padding_len = output_len - data_len;
  421. unsigned char i = 0;
  422. for( i = 1; i < padding_len; i++ )
  423. output[data_len + i - 1] = 0x00;
  424. output[output_len - 1] = (unsigned char) padding_len;
  425. }
  426. static int get_zeros_and_len_padding( unsigned char *input, size_t input_len,
  427. size_t *data_len )
  428. {
  429. size_t i, pad_idx;
  430. unsigned char padding_len, bad = 0;
  431. if( NULL == input || NULL == data_len )
  432. return( POLARSSL_ERR_CIPHER_BAD_INPUT_DATA );
  433. padding_len = input[input_len - 1];
  434. *data_len = input_len - padding_len;
  435. /* Avoid logical || since it results in a branch */
  436. bad |= padding_len > input_len;
  437. bad |= padding_len == 0;
  438. /* The number of bytes checked must be independent of padding_len */
  439. pad_idx = input_len - padding_len;
  440. for( i = 0; i < input_len - 1; i++ )
  441. bad |= input[i] * ( i >= pad_idx );
  442. return( POLARSSL_ERR_CIPHER_INVALID_PADDING * ( bad != 0 ) );
  443. }
  444. #endif /* POLARSSL_CIPHER_PADDING_ZEROS_AND_LEN */
  445. #if defined(POLARSSL_CIPHER_PADDING_ZEROS)
  446. /*
  447. * Zero padding: fill with 00 ... 00
  448. */
  449. static void add_zeros_padding( unsigned char *output,
  450. size_t output_len, size_t data_len )
  451. {
  452. size_t i;
  453. for( i = data_len; i < output_len; i++ )
  454. output[i] = 0x00;
  455. }
  456. static int get_zeros_padding( unsigned char *input, size_t input_len,
  457. size_t *data_len )
  458. {
  459. size_t i;
  460. unsigned char done = 0, prev_done;
  461. if( NULL == input || NULL == data_len )
  462. return( POLARSSL_ERR_CIPHER_BAD_INPUT_DATA );
  463. *data_len = 0;
  464. for( i = input_len; i > 0; i-- )
  465. {
  466. prev_done = done;
  467. done |= ( input[i-1] != 0 );
  468. *data_len |= i * ( done != prev_done );
  469. }
  470. return( 0 );
  471. }
  472. #endif /* POLARSSL_CIPHER_PADDING_ZEROS */
  473. /*
  474. * No padding: don't pad :)
  475. *
  476. * There is no add_padding function (check for NULL in cipher_finish)
  477. * but a trivial get_padding function
  478. */
  479. static int get_no_padding( unsigned char *input, size_t input_len,
  480. size_t *data_len )
  481. {
  482. if( NULL == input || NULL == data_len )
  483. return( POLARSSL_ERR_CIPHER_BAD_INPUT_DATA );
  484. *data_len = input_len;
  485. return( 0 );
  486. }
  487. #endif /* POLARSSL_CIPHER_MODE_WITH_PADDING */
  488. int cipher_finish( cipher_context_t *ctx,
  489. unsigned char *output, size_t *olen )
  490. {
  491. if( NULL == ctx || NULL == ctx->cipher_info || NULL == olen )
  492. return( POLARSSL_ERR_CIPHER_BAD_INPUT_DATA );
  493. *olen = 0;
  494. if( POLARSSL_MODE_CFB == ctx->cipher_info->mode ||
  495. POLARSSL_MODE_CTR == ctx->cipher_info->mode ||
  496. POLARSSL_MODE_GCM == ctx->cipher_info->mode ||
  497. POLARSSL_MODE_STREAM == ctx->cipher_info->mode )
  498. {
  499. return( 0 );
  500. }
  501. if( POLARSSL_MODE_ECB == ctx->cipher_info->mode )
  502. {
  503. if( ctx->unprocessed_len != 0 )
  504. return( POLARSSL_ERR_CIPHER_FULL_BLOCK_EXPECTED );
  505. return( 0 );
  506. }
  507. #if defined(POLARSSL_CIPHER_MODE_CBC)
  508. if( POLARSSL_MODE_CBC == ctx->cipher_info->mode )
  509. {
  510. int ret = 0;
  511. if( POLARSSL_ENCRYPT == ctx->operation )
  512. {
  513. /* check for 'no padding' mode */
  514. if( NULL == ctx->add_padding )
  515. {
  516. if( 0 != ctx->unprocessed_len )
  517. return( POLARSSL_ERR_CIPHER_FULL_BLOCK_EXPECTED );
  518. return( 0 );
  519. }
  520. ctx->add_padding( ctx->unprocessed_data, cipher_get_iv_size( ctx ),
  521. ctx->unprocessed_len );
  522. }
  523. else if( cipher_get_block_size( ctx ) != ctx->unprocessed_len )
  524. {
  525. /*
  526. * For decrypt operations, expect a full block,
  527. * or an empty block if no padding
  528. */
  529. if( NULL == ctx->add_padding && 0 == ctx->unprocessed_len )
  530. return( 0 );
  531. return( POLARSSL_ERR_CIPHER_FULL_BLOCK_EXPECTED );
  532. }
  533. /* cipher block */
  534. if( 0 != ( ret = ctx->cipher_info->base->cbc_func( ctx->cipher_ctx,
  535. ctx->operation, cipher_get_block_size( ctx ), ctx->iv,
  536. ctx->unprocessed_data, output ) ) )
  537. {
  538. return( ret );
  539. }
  540. /* Set output size for decryption */
  541. if( POLARSSL_DECRYPT == ctx->operation )
  542. return ctx->get_padding( output, cipher_get_block_size( ctx ),
  543. olen );
  544. /* Set output size for encryption */
  545. *olen = cipher_get_block_size( ctx );
  546. return( 0 );
  547. }
  548. #else
  549. ((void) output);
  550. #endif /* POLARSSL_CIPHER_MODE_CBC */
  551. return( POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE );
  552. }
  553. #if defined(POLARSSL_CIPHER_MODE_WITH_PADDING)
  554. int cipher_set_padding_mode( cipher_context_t *ctx, cipher_padding_t mode )
  555. {
  556. if( NULL == ctx ||
  557. POLARSSL_MODE_CBC != ctx->cipher_info->mode )
  558. {
  559. return( POLARSSL_ERR_CIPHER_BAD_INPUT_DATA );
  560. }
  561. switch( mode )
  562. {
  563. #if defined(POLARSSL_CIPHER_PADDING_PKCS7)
  564. case POLARSSL_PADDING_PKCS7:
  565. ctx->add_padding = add_pkcs_padding;
  566. ctx->get_padding = get_pkcs_padding;
  567. break;
  568. #endif
  569. #if defined(POLARSSL_CIPHER_PADDING_ONE_AND_ZEROS)
  570. case POLARSSL_PADDING_ONE_AND_ZEROS:
  571. ctx->add_padding = add_one_and_zeros_padding;
  572. ctx->get_padding = get_one_and_zeros_padding;
  573. break;
  574. #endif
  575. #if defined(POLARSSL_CIPHER_PADDING_ZEROS_AND_LEN)
  576. case POLARSSL_PADDING_ZEROS_AND_LEN:
  577. ctx->add_padding = add_zeros_and_len_padding;
  578. ctx->get_padding = get_zeros_and_len_padding;
  579. break;
  580. #endif
  581. #if defined(POLARSSL_CIPHER_PADDING_ZEROS)
  582. case POLARSSL_PADDING_ZEROS:
  583. ctx->add_padding = add_zeros_padding;
  584. ctx->get_padding = get_zeros_padding;
  585. break;
  586. #endif
  587. case POLARSSL_PADDING_NONE:
  588. ctx->add_padding = NULL;
  589. ctx->get_padding = get_no_padding;
  590. break;
  591. default:
  592. return( POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE );
  593. }
  594. return( 0 );
  595. }
  596. #endif /* POLARSSL_CIPHER_MODE_WITH_PADDING */
  597. #if defined(POLARSSL_GCM_C)
  598. int cipher_write_tag( cipher_context_t *ctx,
  599. unsigned char *tag, size_t tag_len )
  600. {
  601. if( NULL == ctx || NULL == ctx->cipher_info || NULL == tag )
  602. return( POLARSSL_ERR_CIPHER_BAD_INPUT_DATA );
  603. if( POLARSSL_ENCRYPT != ctx->operation )
  604. return( POLARSSL_ERR_CIPHER_BAD_INPUT_DATA );
  605. if( POLARSSL_MODE_GCM == ctx->cipher_info->mode )
  606. return gcm_finish( (gcm_context *) ctx->cipher_ctx, tag, tag_len );
  607. return( 0 );
  608. }
  609. int cipher_check_tag( cipher_context_t *ctx,
  610. const unsigned char *tag, size_t tag_len )
  611. {
  612. int ret;
  613. if( NULL == ctx || NULL == ctx->cipher_info ||
  614. POLARSSL_DECRYPT != ctx->operation )
  615. {
  616. return( POLARSSL_ERR_CIPHER_BAD_INPUT_DATA );
  617. }
  618. if( POLARSSL_MODE_GCM == ctx->cipher_info->mode )
  619. {
  620. unsigned char check_tag[16];
  621. size_t i;
  622. int diff;
  623. if( tag_len > sizeof( check_tag ) )
  624. return( POLARSSL_ERR_CIPHER_BAD_INPUT_DATA );
  625. if( 0 != ( ret = gcm_finish( (gcm_context *) ctx->cipher_ctx,
  626. check_tag, tag_len ) ) )
  627. {
  628. return( ret );
  629. }
  630. /* Check the tag in "constant-time" */
  631. for( diff = 0, i = 0; i < tag_len; i++ )
  632. diff |= tag[i] ^ check_tag[i];
  633. if( diff != 0 )
  634. return( POLARSSL_ERR_CIPHER_AUTH_FAILED );
  635. return( 0 );
  636. }
  637. return( 0 );
  638. }
  639. #endif /* POLARSSL_GCM_C */
  640. /*
  641. * Packet-oriented wrapper for non-AEAD modes
  642. */
  643. int cipher_crypt( cipher_context_t *ctx,
  644. const unsigned char *iv, size_t iv_len,
  645. const unsigned char *input, size_t ilen,
  646. unsigned char *output, size_t *olen )
  647. {
  648. int ret;
  649. size_t finish_olen;
  650. if( ( ret = cipher_set_iv( ctx, iv, iv_len ) ) != 0 )
  651. return( ret );
  652. if( ( ret = cipher_reset( ctx ) ) != 0 )
  653. return( ret );
  654. if( ( ret = cipher_update( ctx, input, ilen, output, olen ) ) != 0 )
  655. return( ret );
  656. if( ( ret = cipher_finish( ctx, output + *olen, &finish_olen ) ) != 0 )
  657. return( ret );
  658. *olen += finish_olen;
  659. return( 0 );
  660. }
  661. #if defined(POLARSSL_CIPHER_MODE_AEAD)
  662. /*
  663. * Packet-oriented encryption for AEAD modes
  664. */
  665. int cipher_auth_encrypt( cipher_context_t *ctx,
  666. const unsigned char *iv, size_t iv_len,
  667. const unsigned char *ad, size_t ad_len,
  668. const unsigned char *input, size_t ilen,
  669. unsigned char *output, size_t *olen,
  670. unsigned char *tag, size_t tag_len )
  671. {
  672. #if defined(POLARSSL_GCM_C)
  673. if( POLARSSL_MODE_GCM == ctx->cipher_info->mode )
  674. {
  675. *olen = ilen;
  676. return( gcm_crypt_and_tag( ctx->cipher_ctx, GCM_ENCRYPT, ilen,
  677. iv, iv_len, ad, ad_len, input, output,
  678. tag_len, tag ) );
  679. }
  680. #endif /* POLARSSL_GCM_C */
  681. #if defined(POLARSSL_CCM_C)
  682. if( POLARSSL_MODE_CCM == ctx->cipher_info->mode )
  683. {
  684. *olen = ilen;
  685. return( ccm_encrypt_and_tag( ctx->cipher_ctx, ilen,
  686. iv, iv_len, ad, ad_len, input, output,
  687. tag, tag_len ) );
  688. }
  689. #endif /* POLARSSL_CCM_C */
  690. return( POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE );
  691. }
  692. /*
  693. * Packet-oriented decryption for AEAD modes
  694. */
  695. int cipher_auth_decrypt( cipher_context_t *ctx,
  696. const unsigned char *iv, size_t iv_len,
  697. const unsigned char *ad, size_t ad_len,
  698. const unsigned char *input, size_t ilen,
  699. unsigned char *output, size_t *olen,
  700. const unsigned char *tag, size_t tag_len )
  701. {
  702. #if defined(POLARSSL_GCM_C)
  703. if( POLARSSL_MODE_GCM == ctx->cipher_info->mode )
  704. {
  705. int ret;
  706. *olen = ilen;
  707. ret = gcm_auth_decrypt( ctx->cipher_ctx, ilen,
  708. iv, iv_len, ad, ad_len,
  709. tag, tag_len, input, output );
  710. if( ret == POLARSSL_ERR_GCM_AUTH_FAILED )
  711. ret = POLARSSL_ERR_CIPHER_AUTH_FAILED;
  712. return( ret );
  713. }
  714. #endif /* POLARSSL_GCM_C */
  715. #if defined(POLARSSL_CCM_C)
  716. if( POLARSSL_MODE_CCM == ctx->cipher_info->mode )
  717. {
  718. int ret;
  719. *olen = ilen;
  720. ret = ccm_auth_decrypt( ctx->cipher_ctx, ilen,
  721. iv, iv_len, ad, ad_len,
  722. input, output, tag, tag_len );
  723. if( ret == POLARSSL_ERR_CCM_AUTH_FAILED )
  724. ret = POLARSSL_ERR_CIPHER_AUTH_FAILED;
  725. return( ret );
  726. }
  727. #endif /* POLARSSL_CCM_C */
  728. return( POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE );
  729. }
  730. #endif /* POLARSSL_CIPHER_MODE_AEAD */
  731. #if defined(POLARSSL_SELF_TEST)
  732. /*
  733. * Checkup routine
  734. */
  735. int cipher_self_test( int verbose )
  736. {
  737. ((void) verbose);
  738. return( 0 );
  739. }
  740. #endif /* POLARSSL_SELF_TEST */
  741. #endif /* POLARSSL_CIPHER_C */