cipher.c 25 KB

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