cipher.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894
  1. /*
  2. * Copyright (C) 2015-2018 Alibaba Group Holding Limited
  3. */
  4. #if !defined(MBEDTLS_CONFIG_FILE)
  5. #include "mbedtls/config.h"
  6. #else
  7. #include MBEDTLS_CONFIG_FILE
  8. #endif
  9. #if defined(MBEDTLS_CIPHER_C)
  10. #include "mbedtls/cipher.h"
  11. #include "mbedtls/cipher_internal.h"
  12. #include <stdlib.h>
  13. #include <string.h>
  14. #if defined(MBEDTLS_GCM_C)
  15. #include "mbedtls/gcm.h"
  16. #endif
  17. #if defined(MBEDTLS_CCM_C)
  18. #include "mbedtls/ccm.h"
  19. #endif
  20. #if defined(MBEDTLS_CMAC_C)
  21. #include "mbedtls/cmac.h"
  22. #endif
  23. #if defined(MBEDTLS_PLATFORM_C)
  24. #include "mbedtls/platform.h"
  25. #else
  26. #define mbedtls_calloc calloc
  27. #define mbedtls_free free
  28. #endif
  29. #if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
  30. #define MBEDTLS_CIPHER_MODE_STREAM
  31. #endif
  32. /* Implementation that should never be optimized out by the compiler */
  33. static void mbedtls_zeroize( void *v, size_t n ) {
  34. volatile unsigned char *p = (unsigned char*)v; while( n-- ) *p++ = 0;
  35. }
  36. static int supported_init = 0;
  37. const int *mbedtls_cipher_list( void )
  38. {
  39. const mbedtls_cipher_definition_t *def;
  40. int *type;
  41. if( ! supported_init )
  42. {
  43. def = mbedtls_cipher_definitions;
  44. type = mbedtls_cipher_supported;
  45. while( def->type != 0 )
  46. *type++ = (*def++).type;
  47. *type = 0;
  48. supported_init = 1;
  49. }
  50. return( mbedtls_cipher_supported );
  51. }
  52. const mbedtls_cipher_info_t *mbedtls_cipher_info_from_type( const mbedtls_cipher_type_t cipher_type )
  53. {
  54. const mbedtls_cipher_definition_t *def;
  55. for( def = mbedtls_cipher_definitions; def->info != NULL; def++ )
  56. if( def->type == cipher_type )
  57. return( def->info );
  58. return( NULL );
  59. }
  60. const mbedtls_cipher_info_t *mbedtls_cipher_info_from_string( const char *cipher_name )
  61. {
  62. const mbedtls_cipher_definition_t *def;
  63. if( NULL == cipher_name )
  64. return( NULL );
  65. for( def = mbedtls_cipher_definitions; def->info != NULL; def++ )
  66. if( ! strcmp( def->info->name, cipher_name ) )
  67. return( def->info );
  68. return( NULL );
  69. }
  70. const mbedtls_cipher_info_t *mbedtls_cipher_info_from_values( const mbedtls_cipher_id_t cipher_id,
  71. int key_bitlen,
  72. const mbedtls_cipher_mode_t mode )
  73. {
  74. const mbedtls_cipher_definition_t *def;
  75. for( def = mbedtls_cipher_definitions; def->info != NULL; def++ )
  76. if( def->info->base->cipher == cipher_id &&
  77. def->info->key_bitlen == (unsigned) key_bitlen &&
  78. def->info->mode == mode )
  79. return( def->info );
  80. return( NULL );
  81. }
  82. void mbedtls_cipher_init( mbedtls_cipher_context_t *ctx )
  83. {
  84. memset( ctx, 0, sizeof( mbedtls_cipher_context_t ) );
  85. }
  86. void mbedtls_cipher_free( mbedtls_cipher_context_t *ctx )
  87. {
  88. if( ctx == NULL )
  89. return;
  90. #if defined(MBEDTLS_CMAC_C)
  91. if( ctx->cmac_ctx )
  92. {
  93. mbedtls_zeroize( ctx->cmac_ctx, sizeof( mbedtls_cmac_context_t ) );
  94. mbedtls_free( ctx->cmac_ctx );
  95. }
  96. #endif
  97. if( ctx->cipher_ctx )
  98. ctx->cipher_info->base->ctx_free_func( ctx->cipher_ctx );
  99. mbedtls_zeroize( ctx, sizeof(mbedtls_cipher_context_t) );
  100. }
  101. int mbedtls_cipher_setup( mbedtls_cipher_context_t *ctx, const mbedtls_cipher_info_t *cipher_info )
  102. {
  103. if( NULL == cipher_info || NULL == ctx )
  104. return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
  105. memset( ctx, 0, sizeof( mbedtls_cipher_context_t ) );
  106. if( NULL == ( ctx->cipher_ctx = cipher_info->base->ctx_alloc_func() ) )
  107. return( MBEDTLS_ERR_CIPHER_ALLOC_FAILED );
  108. ctx->cipher_info = cipher_info;
  109. #if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
  110. /*
  111. * Ignore possible errors caused by a cipher mode that doesn't use padding
  112. */
  113. #if defined(MBEDTLS_CIPHER_PADDING_PKCS7)
  114. (void) mbedtls_cipher_set_padding_mode( ctx, MBEDTLS_PADDING_PKCS7 );
  115. #else
  116. (void) mbedtls_cipher_set_padding_mode( ctx, MBEDTLS_PADDING_NONE );
  117. #endif
  118. #endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
  119. return( 0 );
  120. }
  121. int mbedtls_cipher_setkey( mbedtls_cipher_context_t *ctx, const unsigned char *key,
  122. int key_bitlen, const mbedtls_operation_t operation )
  123. {
  124. if( NULL == ctx || NULL == ctx->cipher_info )
  125. return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
  126. if( ( ctx->cipher_info->flags & MBEDTLS_CIPHER_VARIABLE_KEY_LEN ) == 0 &&
  127. (int) ctx->cipher_info->key_bitlen != key_bitlen )
  128. {
  129. return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
  130. }
  131. ctx->key_bitlen = key_bitlen;
  132. ctx->operation = operation;
  133. /*
  134. * For CFB and CTR mode always use the encryption key schedule
  135. */
  136. if( MBEDTLS_ENCRYPT == operation ||
  137. MBEDTLS_MODE_CFB == ctx->cipher_info->mode ||
  138. MBEDTLS_MODE_CTR == ctx->cipher_info->mode )
  139. {
  140. return ctx->cipher_info->base->setkey_enc_func( ctx->cipher_ctx, key,
  141. ctx->key_bitlen );
  142. }
  143. if( MBEDTLS_DECRYPT == operation )
  144. return ctx->cipher_info->base->setkey_dec_func( ctx->cipher_ctx, key,
  145. ctx->key_bitlen );
  146. return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
  147. }
  148. int mbedtls_cipher_set_iv( mbedtls_cipher_context_t *ctx,
  149. const unsigned char *iv, size_t iv_len )
  150. {
  151. size_t actual_iv_size;
  152. if( NULL == ctx || NULL == ctx->cipher_info || NULL == iv )
  153. return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
  154. /* avoid buffer overflow in ctx->iv */
  155. if( iv_len > MBEDTLS_MAX_IV_LENGTH )
  156. return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
  157. if( ( ctx->cipher_info->flags & MBEDTLS_CIPHER_VARIABLE_IV_LEN ) != 0 )
  158. actual_iv_size = iv_len;
  159. else
  160. {
  161. actual_iv_size = ctx->cipher_info->iv_size;
  162. /* avoid reading past the end of input buffer */
  163. if( actual_iv_size > iv_len )
  164. return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
  165. }
  166. memcpy( ctx->iv, iv, actual_iv_size );
  167. ctx->iv_size = actual_iv_size;
  168. return( 0 );
  169. }
  170. int mbedtls_cipher_reset( mbedtls_cipher_context_t *ctx )
  171. {
  172. if( NULL == ctx || NULL == ctx->cipher_info )
  173. return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
  174. ctx->unprocessed_len = 0;
  175. return( 0 );
  176. }
  177. #if defined(MBEDTLS_GCM_C)
  178. int mbedtls_cipher_update_ad( mbedtls_cipher_context_t *ctx,
  179. const unsigned char *ad, size_t ad_len )
  180. {
  181. if( NULL == ctx || NULL == ctx->cipher_info )
  182. return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
  183. if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
  184. {
  185. return mbedtls_gcm_starts( (mbedtls_gcm_context *) ctx->cipher_ctx, ctx->operation,
  186. ctx->iv, ctx->iv_size, ad, ad_len );
  187. }
  188. return( 0 );
  189. }
  190. #endif /* MBEDTLS_GCM_C */
  191. int mbedtls_cipher_update( mbedtls_cipher_context_t *ctx, const unsigned char *input,
  192. size_t ilen, unsigned char *output, size_t *olen )
  193. {
  194. int ret;
  195. size_t block_size = 0;
  196. if( NULL == ctx || NULL == ctx->cipher_info || NULL == olen )
  197. {
  198. return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
  199. }
  200. *olen = 0;
  201. block_size = mbedtls_cipher_get_block_size( ctx );
  202. if( ctx->cipher_info->mode == MBEDTLS_MODE_ECB )
  203. {
  204. if( ilen != block_size )
  205. return( MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED );
  206. *olen = ilen;
  207. if( 0 != ( ret = ctx->cipher_info->base->ecb_func( ctx->cipher_ctx,
  208. ctx->operation, input, output ) ) )
  209. {
  210. return( ret );
  211. }
  212. return( 0 );
  213. }
  214. #if defined(MBEDTLS_GCM_C)
  215. if( ctx->cipher_info->mode == MBEDTLS_MODE_GCM )
  216. {
  217. *olen = ilen;
  218. return mbedtls_gcm_update( (mbedtls_gcm_context *) ctx->cipher_ctx, ilen, input,
  219. output );
  220. }
  221. #endif
  222. if ( 0 == block_size )
  223. {
  224. return MBEDTLS_ERR_CIPHER_INVALID_CONTEXT;
  225. }
  226. if( input == output &&
  227. ( ctx->unprocessed_len != 0 || ilen % block_size ) )
  228. {
  229. return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
  230. }
  231. #if defined(MBEDTLS_CIPHER_MODE_CBC)
  232. if( ctx->cipher_info->mode == MBEDTLS_MODE_CBC )
  233. {
  234. size_t copy_len = 0;
  235. /*
  236. * If there is not enough data for a full block, cache it.
  237. */
  238. if( ( ctx->operation == MBEDTLS_DECRYPT &&
  239. ilen <= block_size - ctx->unprocessed_len ) ||
  240. ( ctx->operation == MBEDTLS_ENCRYPT &&
  241. ilen < block_size - ctx->unprocessed_len ) )
  242. {
  243. memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ), input,
  244. ilen );
  245. ctx->unprocessed_len += ilen;
  246. return( 0 );
  247. }
  248. /*
  249. * Process cached data first
  250. */
  251. if( 0 != ctx->unprocessed_len )
  252. {
  253. copy_len = block_size - ctx->unprocessed_len;
  254. memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ), input,
  255. copy_len );
  256. if( 0 != ( ret = ctx->cipher_info->base->cbc_func( ctx->cipher_ctx,
  257. ctx->operation, block_size, ctx->iv,
  258. ctx->unprocessed_data, output ) ) )
  259. {
  260. return( ret );
  261. }
  262. *olen += block_size;
  263. output += block_size;
  264. ctx->unprocessed_len = 0;
  265. input += copy_len;
  266. ilen -= copy_len;
  267. }
  268. /*
  269. * Cache final, incomplete block
  270. */
  271. if( 0 != ilen )
  272. {
  273. copy_len = ilen % block_size;
  274. if( copy_len == 0 && ctx->operation == MBEDTLS_DECRYPT )
  275. copy_len = block_size;
  276. memcpy( ctx->unprocessed_data, &( input[ilen - copy_len] ),
  277. copy_len );
  278. ctx->unprocessed_len += copy_len;
  279. ilen -= copy_len;
  280. }
  281. /*
  282. * Process remaining full blocks
  283. */
  284. if( ilen )
  285. {
  286. if( 0 != ( ret = ctx->cipher_info->base->cbc_func( ctx->cipher_ctx,
  287. ctx->operation, ilen, ctx->iv, input, output ) ) )
  288. {
  289. return( ret );
  290. }
  291. *olen += ilen;
  292. }
  293. return( 0 );
  294. }
  295. #endif /* MBEDTLS_CIPHER_MODE_CBC */
  296. #if defined(MBEDTLS_CIPHER_MODE_CFB)
  297. if( ctx->cipher_info->mode == MBEDTLS_MODE_CFB )
  298. {
  299. if( 0 != ( ret = ctx->cipher_info->base->cfb_func( ctx->cipher_ctx,
  300. ctx->operation, ilen, &ctx->unprocessed_len, ctx->iv,
  301. input, output ) ) )
  302. {
  303. return( ret );
  304. }
  305. *olen = ilen;
  306. return( 0 );
  307. }
  308. #endif /* MBEDTLS_CIPHER_MODE_CFB */
  309. #if defined(MBEDTLS_CIPHER_MODE_CTR)
  310. if( ctx->cipher_info->mode == MBEDTLS_MODE_CTR )
  311. {
  312. if( 0 != ( ret = ctx->cipher_info->base->ctr_func( ctx->cipher_ctx,
  313. ilen, &ctx->unprocessed_len, ctx->iv,
  314. ctx->unprocessed_data, input, output ) ) )
  315. {
  316. return( ret );
  317. }
  318. *olen = ilen;
  319. return( 0 );
  320. }
  321. #endif /* MBEDTLS_CIPHER_MODE_CTR */
  322. #if defined(MBEDTLS_CIPHER_MODE_STREAM)
  323. if( ctx->cipher_info->mode == MBEDTLS_MODE_STREAM )
  324. {
  325. if( 0 != ( ret = ctx->cipher_info->base->stream_func( ctx->cipher_ctx,
  326. ilen, input, output ) ) )
  327. {
  328. return( ret );
  329. }
  330. *olen = ilen;
  331. return( 0 );
  332. }
  333. #endif /* MBEDTLS_CIPHER_MODE_STREAM */
  334. return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
  335. }
  336. #if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
  337. #if defined(MBEDTLS_CIPHER_PADDING_PKCS7)
  338. /*
  339. * PKCS7 (and PKCS5) padding: fill with ll bytes, with ll = padding_len
  340. */
  341. static void add_pkcs_padding( unsigned char *output, size_t output_len,
  342. size_t data_len )
  343. {
  344. size_t padding_len = output_len - data_len;
  345. unsigned char i;
  346. for( i = 0; i < padding_len; i++ )
  347. output[data_len + i] = (unsigned char) padding_len;
  348. }
  349. static int get_pkcs_padding( unsigned char *input, size_t input_len,
  350. size_t *data_len )
  351. {
  352. size_t i, pad_idx;
  353. unsigned char padding_len, bad = 0;
  354. if( NULL == input || NULL == data_len )
  355. return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
  356. padding_len = input[input_len - 1];
  357. *data_len = input_len - padding_len;
  358. /* Avoid logical || since it results in a branch */
  359. bad |= padding_len > input_len;
  360. bad |= padding_len == 0;
  361. /* The number of bytes checked must be independent of padding_len,
  362. * so pick input_len, which is usually 8 or 16 (one block) */
  363. pad_idx = input_len - padding_len;
  364. for( i = 0; i < input_len; i++ )
  365. bad |= ( input[i] ^ padding_len ) * ( i >= pad_idx );
  366. return( MBEDTLS_ERR_CIPHER_INVALID_PADDING * ( bad != 0 ) );
  367. }
  368. #endif /* MBEDTLS_CIPHER_PADDING_PKCS7 */
  369. #if defined(MBEDTLS_CIPHER_PADDING_ONE_AND_ZEROS)
  370. /*
  371. * One and zeros padding: fill with 80 00 ... 00
  372. */
  373. static void add_one_and_zeros_padding( unsigned char *output,
  374. size_t output_len, size_t data_len )
  375. {
  376. size_t padding_len = output_len - data_len;
  377. unsigned char i = 0;
  378. output[data_len] = 0x80;
  379. for( i = 1; i < padding_len; i++ )
  380. output[data_len + i] = 0x00;
  381. }
  382. static int get_one_and_zeros_padding( unsigned char *input, size_t input_len,
  383. size_t *data_len )
  384. {
  385. size_t i;
  386. unsigned char done = 0, prev_done, bad;
  387. if( NULL == input || NULL == data_len )
  388. return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
  389. bad = 0xFF;
  390. *data_len = 0;
  391. for( i = input_len; i > 0; i-- )
  392. {
  393. prev_done = done;
  394. done |= ( input[i-1] != 0 );
  395. *data_len |= ( i - 1 ) * ( done != prev_done );
  396. bad &= ( input[i-1] ^ 0x80 ) | ( done == prev_done );
  397. }
  398. return( MBEDTLS_ERR_CIPHER_INVALID_PADDING * ( bad != 0 ) );
  399. }
  400. #endif /* MBEDTLS_CIPHER_PADDING_ONE_AND_ZEROS */
  401. #if defined(MBEDTLS_CIPHER_PADDING_ZEROS_AND_LEN)
  402. /*
  403. * Zeros and len padding: fill with 00 ... 00 ll, where ll is padding length
  404. */
  405. static void add_zeros_and_len_padding( unsigned char *output,
  406. size_t output_len, size_t data_len )
  407. {
  408. size_t padding_len = output_len - data_len;
  409. unsigned char i = 0;
  410. for( i = 1; i < padding_len; i++ )
  411. output[data_len + i - 1] = 0x00;
  412. output[output_len - 1] = (unsigned char) padding_len;
  413. }
  414. static int get_zeros_and_len_padding( unsigned char *input, size_t input_len,
  415. size_t *data_len )
  416. {
  417. size_t i, pad_idx;
  418. unsigned char padding_len, bad = 0;
  419. if( NULL == input || NULL == data_len )
  420. return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
  421. padding_len = input[input_len - 1];
  422. *data_len = input_len - padding_len;
  423. /* Avoid logical || since it results in a branch */
  424. bad |= padding_len > input_len;
  425. bad |= padding_len == 0;
  426. /* The number of bytes checked must be independent of padding_len */
  427. pad_idx = input_len - padding_len;
  428. for( i = 0; i < input_len - 1; i++ )
  429. bad |= input[i] * ( i >= pad_idx );
  430. return( MBEDTLS_ERR_CIPHER_INVALID_PADDING * ( bad != 0 ) );
  431. }
  432. #endif /* MBEDTLS_CIPHER_PADDING_ZEROS_AND_LEN */
  433. #if defined(MBEDTLS_CIPHER_PADDING_ZEROS)
  434. /*
  435. * Zero padding: fill with 00 ... 00
  436. */
  437. static void add_zeros_padding( unsigned char *output,
  438. size_t output_len, size_t data_len )
  439. {
  440. size_t i;
  441. for( i = data_len; i < output_len; i++ )
  442. output[i] = 0x00;
  443. }
  444. static int get_zeros_padding( unsigned char *input, size_t input_len,
  445. size_t *data_len )
  446. {
  447. size_t i;
  448. unsigned char done = 0, prev_done;
  449. if( NULL == input || NULL == data_len )
  450. return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
  451. *data_len = 0;
  452. for( i = input_len; i > 0; i-- )
  453. {
  454. prev_done = done;
  455. done |= ( input[i-1] != 0 );
  456. *data_len |= i * ( done != prev_done );
  457. }
  458. return( 0 );
  459. }
  460. #endif /* MBEDTLS_CIPHER_PADDING_ZEROS */
  461. /*
  462. * No padding: don't pad :)
  463. *
  464. * There is no add_padding function (check for NULL in mbedtls_cipher_finish)
  465. * but a trivial get_padding function
  466. */
  467. static int get_no_padding( unsigned char *input, size_t input_len,
  468. size_t *data_len )
  469. {
  470. if( NULL == input || NULL == data_len )
  471. return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
  472. *data_len = input_len;
  473. return( 0 );
  474. }
  475. #endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
  476. int mbedtls_cipher_finish( mbedtls_cipher_context_t *ctx,
  477. unsigned char *output, size_t *olen )
  478. {
  479. if( NULL == ctx || NULL == ctx->cipher_info || NULL == olen )
  480. return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
  481. *olen = 0;
  482. if( MBEDTLS_MODE_CFB == ctx->cipher_info->mode ||
  483. MBEDTLS_MODE_CTR == ctx->cipher_info->mode ||
  484. MBEDTLS_MODE_GCM == ctx->cipher_info->mode ||
  485. MBEDTLS_MODE_STREAM == ctx->cipher_info->mode )
  486. {
  487. return( 0 );
  488. }
  489. if( MBEDTLS_MODE_ECB == ctx->cipher_info->mode )
  490. {
  491. if( ctx->unprocessed_len != 0 )
  492. return( MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED );
  493. return( 0 );
  494. }
  495. #if defined(MBEDTLS_CIPHER_MODE_CBC)
  496. if( MBEDTLS_MODE_CBC == ctx->cipher_info->mode )
  497. {
  498. int ret = 0;
  499. if( MBEDTLS_ENCRYPT == ctx->operation )
  500. {
  501. /* check for 'no padding' mode */
  502. if( NULL == ctx->add_padding )
  503. {
  504. if( 0 != ctx->unprocessed_len )
  505. return( MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED );
  506. return( 0 );
  507. }
  508. ctx->add_padding( ctx->unprocessed_data, mbedtls_cipher_get_iv_size( ctx ),
  509. ctx->unprocessed_len );
  510. }
  511. else if( mbedtls_cipher_get_block_size( ctx ) != ctx->unprocessed_len )
  512. {
  513. /*
  514. * For decrypt operations, expect a full block,
  515. * or an empty block if no padding
  516. */
  517. if( NULL == ctx->add_padding && 0 == ctx->unprocessed_len )
  518. return( 0 );
  519. return( MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED );
  520. }
  521. /* cipher block */
  522. if( 0 != ( ret = ctx->cipher_info->base->cbc_func( ctx->cipher_ctx,
  523. ctx->operation, mbedtls_cipher_get_block_size( ctx ), ctx->iv,
  524. ctx->unprocessed_data, output ) ) )
  525. {
  526. return( ret );
  527. }
  528. /* Set output size for decryption */
  529. if( MBEDTLS_DECRYPT == ctx->operation )
  530. return ctx->get_padding( output, mbedtls_cipher_get_block_size( ctx ),
  531. olen );
  532. /* Set output size for encryption */
  533. *olen = mbedtls_cipher_get_block_size( ctx );
  534. return( 0 );
  535. }
  536. #else
  537. ((void) output);
  538. #endif /* MBEDTLS_CIPHER_MODE_CBC */
  539. return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
  540. }
  541. #if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
  542. int mbedtls_cipher_set_padding_mode( mbedtls_cipher_context_t *ctx, mbedtls_cipher_padding_t mode )
  543. {
  544. if( NULL == ctx ||
  545. MBEDTLS_MODE_CBC != ctx->cipher_info->mode )
  546. {
  547. return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
  548. }
  549. switch( mode )
  550. {
  551. #if defined(MBEDTLS_CIPHER_PADDING_PKCS7)
  552. case MBEDTLS_PADDING_PKCS7:
  553. ctx->add_padding = add_pkcs_padding;
  554. ctx->get_padding = get_pkcs_padding;
  555. break;
  556. #endif
  557. #if defined(MBEDTLS_CIPHER_PADDING_ONE_AND_ZEROS)
  558. case MBEDTLS_PADDING_ONE_AND_ZEROS:
  559. ctx->add_padding = add_one_and_zeros_padding;
  560. ctx->get_padding = get_one_and_zeros_padding;
  561. break;
  562. #endif
  563. #if defined(MBEDTLS_CIPHER_PADDING_ZEROS_AND_LEN)
  564. case MBEDTLS_PADDING_ZEROS_AND_LEN:
  565. ctx->add_padding = add_zeros_and_len_padding;
  566. ctx->get_padding = get_zeros_and_len_padding;
  567. break;
  568. #endif
  569. #if defined(MBEDTLS_CIPHER_PADDING_ZEROS)
  570. case MBEDTLS_PADDING_ZEROS:
  571. ctx->add_padding = add_zeros_padding;
  572. ctx->get_padding = get_zeros_padding;
  573. break;
  574. #endif
  575. case MBEDTLS_PADDING_NONE:
  576. ctx->add_padding = NULL;
  577. ctx->get_padding = get_no_padding;
  578. break;
  579. default:
  580. return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
  581. }
  582. return( 0 );
  583. }
  584. #endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
  585. #if defined(MBEDTLS_GCM_C)
  586. int mbedtls_cipher_write_tag( mbedtls_cipher_context_t *ctx,
  587. unsigned char *tag, size_t tag_len )
  588. {
  589. if( NULL == ctx || NULL == ctx->cipher_info || NULL == tag )
  590. return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
  591. if( MBEDTLS_ENCRYPT != ctx->operation )
  592. return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
  593. if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
  594. return mbedtls_gcm_finish( (mbedtls_gcm_context *) ctx->cipher_ctx, tag, tag_len );
  595. return( 0 );
  596. }
  597. int mbedtls_cipher_check_tag( mbedtls_cipher_context_t *ctx,
  598. const unsigned char *tag, size_t tag_len )
  599. {
  600. int ret;
  601. if( NULL == ctx || NULL == ctx->cipher_info ||
  602. MBEDTLS_DECRYPT != ctx->operation )
  603. {
  604. return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
  605. }
  606. if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
  607. {
  608. unsigned char check_tag[16];
  609. size_t i;
  610. int diff;
  611. if( tag_len > sizeof( check_tag ) )
  612. return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
  613. if( 0 != ( ret = mbedtls_gcm_finish( (mbedtls_gcm_context *) ctx->cipher_ctx,
  614. check_tag, tag_len ) ) )
  615. {
  616. return( ret );
  617. }
  618. /* Check the tag in "constant-time" */
  619. for( diff = 0, i = 0; i < tag_len; i++ )
  620. diff |= tag[i] ^ check_tag[i];
  621. if( diff != 0 )
  622. return( MBEDTLS_ERR_CIPHER_AUTH_FAILED );
  623. return( 0 );
  624. }
  625. return( 0 );
  626. }
  627. #endif /* MBEDTLS_GCM_C */
  628. /*
  629. * Packet-oriented wrapper for non-AEAD modes
  630. */
  631. int mbedtls_cipher_crypt( mbedtls_cipher_context_t *ctx,
  632. const unsigned char *iv, size_t iv_len,
  633. const unsigned char *input, size_t ilen,
  634. unsigned char *output, size_t *olen )
  635. {
  636. int ret;
  637. size_t finish_olen;
  638. if( ( ret = mbedtls_cipher_set_iv( ctx, iv, iv_len ) ) != 0 )
  639. return( ret );
  640. if( ( ret = mbedtls_cipher_reset( ctx ) ) != 0 )
  641. return( ret );
  642. if( ( ret = mbedtls_cipher_update( ctx, input, ilen, output, olen ) ) != 0 )
  643. return( ret );
  644. if( ( ret = mbedtls_cipher_finish( ctx, output + *olen, &finish_olen ) ) != 0 )
  645. return( ret );
  646. *olen += finish_olen;
  647. return( 0 );
  648. }
  649. #if defined(MBEDTLS_CIPHER_MODE_AEAD)
  650. /*
  651. * Packet-oriented encryption for AEAD modes
  652. */
  653. int mbedtls_cipher_auth_encrypt( mbedtls_cipher_context_t *ctx,
  654. const unsigned char *iv, size_t iv_len,
  655. const unsigned char *ad, size_t ad_len,
  656. const unsigned char *input, size_t ilen,
  657. unsigned char *output, size_t *olen,
  658. unsigned char *tag, size_t tag_len )
  659. {
  660. #if defined(MBEDTLS_GCM_C)
  661. if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
  662. {
  663. *olen = ilen;
  664. return( mbedtls_gcm_crypt_and_tag( ctx->cipher_ctx, MBEDTLS_GCM_ENCRYPT, ilen,
  665. iv, iv_len, ad, ad_len, input, output,
  666. tag_len, tag ) );
  667. }
  668. #endif /* MBEDTLS_GCM_C */
  669. #if defined(MBEDTLS_CCM_C)
  670. if( MBEDTLS_MODE_CCM == ctx->cipher_info->mode )
  671. {
  672. *olen = ilen;
  673. return( mbedtls_ccm_encrypt_and_tag( ctx->cipher_ctx, ilen,
  674. iv, iv_len, ad, ad_len, input, output,
  675. tag, tag_len ) );
  676. }
  677. #endif /* MBEDTLS_CCM_C */
  678. return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
  679. }
  680. /*
  681. * Packet-oriented decryption for AEAD modes
  682. */
  683. int mbedtls_cipher_auth_decrypt( mbedtls_cipher_context_t *ctx,
  684. const unsigned char *iv, size_t iv_len,
  685. const unsigned char *ad, size_t ad_len,
  686. const unsigned char *input, size_t ilen,
  687. unsigned char *output, size_t *olen,
  688. const unsigned char *tag, size_t tag_len )
  689. {
  690. #if defined(MBEDTLS_GCM_C)
  691. if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
  692. {
  693. int ret;
  694. *olen = ilen;
  695. ret = mbedtls_gcm_auth_decrypt( ctx->cipher_ctx, ilen,
  696. iv, iv_len, ad, ad_len,
  697. tag, tag_len, input, output );
  698. if( ret == MBEDTLS_ERR_GCM_AUTH_FAILED )
  699. ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
  700. return( ret );
  701. }
  702. #endif /* MBEDTLS_GCM_C */
  703. #if defined(MBEDTLS_CCM_C)
  704. if( MBEDTLS_MODE_CCM == ctx->cipher_info->mode )
  705. {
  706. int ret;
  707. *olen = ilen;
  708. ret = mbedtls_ccm_auth_decrypt( ctx->cipher_ctx, ilen,
  709. iv, iv_len, ad, ad_len,
  710. input, output, tag, tag_len );
  711. if( ret == MBEDTLS_ERR_CCM_AUTH_FAILED )
  712. ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
  713. return( ret );
  714. }
  715. #endif /* MBEDTLS_CCM_C */
  716. return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
  717. }
  718. #endif /* MBEDTLS_CIPHER_MODE_AEAD */
  719. #endif /* MBEDTLS_CIPHER_C */