cipher.c 26 KB

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