cipher.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158
  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 "mbedtls/platform_util.h"
  34. #include <stdlib.h>
  35. #include <string.h>
  36. #if defined(MBEDTLS_CHACHAPOLY_C)
  37. #include "mbedtls/chachapoly.h"
  38. #endif
  39. #if defined(MBEDTLS_GCM_C)
  40. #include "mbedtls/gcm.h"
  41. #endif
  42. #if defined(MBEDTLS_CCM_C)
  43. #include "mbedtls/ccm.h"
  44. #endif
  45. #if defined(MBEDTLS_CHACHA20_C)
  46. #include "mbedtls/chacha20.h"
  47. #endif
  48. #if defined(MBEDTLS_CMAC_C)
  49. #include "mbedtls/cmac.h"
  50. #endif
  51. #if defined(MBEDTLS_PLATFORM_C)
  52. #include "mbedtls/platform.h"
  53. #else
  54. #define mbedtls_calloc calloc
  55. #define mbedtls_free free
  56. #endif
  57. #define CIPHER_VALIDATE_RET( cond ) \
  58. MBEDTLS_INTERNAL_VALIDATE_RET( cond, MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA )
  59. #define CIPHER_VALIDATE( cond ) \
  60. MBEDTLS_INTERNAL_VALIDATE( cond )
  61. #if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
  62. /* Compare the contents of two buffers in constant time.
  63. * Returns 0 if the contents are bitwise identical, otherwise returns
  64. * a non-zero value.
  65. * This is currently only used by GCM and ChaCha20+Poly1305.
  66. */
  67. static int mbedtls_constant_time_memcmp( const void *v1, const void *v2, size_t len )
  68. {
  69. const unsigned char *p1 = (const unsigned char*) v1;
  70. const unsigned char *p2 = (const unsigned char*) v2;
  71. size_t i;
  72. unsigned char diff;
  73. for( diff = 0, i = 0; i < len; i++ )
  74. diff |= p1[i] ^ p2[i];
  75. return( (int)diff );
  76. }
  77. #endif /* MBEDTLS_GCM_C || MBEDTLS_CHACHAPOLY_C */
  78. static int supported_init = 0;
  79. const int *mbedtls_cipher_list( void )
  80. {
  81. const mbedtls_cipher_definition_t *def;
  82. int *type;
  83. if( ! supported_init )
  84. {
  85. def = mbedtls_cipher_definitions;
  86. type = mbedtls_cipher_supported;
  87. while( def->type != 0 )
  88. *type++ = (*def++).type;
  89. *type = 0;
  90. supported_init = 1;
  91. }
  92. return( mbedtls_cipher_supported );
  93. }
  94. const mbedtls_cipher_info_t *mbedtls_cipher_info_from_type( const mbedtls_cipher_type_t cipher_type )
  95. {
  96. const mbedtls_cipher_definition_t *def;
  97. for( def = mbedtls_cipher_definitions; def->info != NULL; def++ )
  98. if( def->type == cipher_type )
  99. return( def->info );
  100. return( NULL );
  101. }
  102. const mbedtls_cipher_info_t *mbedtls_cipher_info_from_string( const char *cipher_name )
  103. {
  104. const mbedtls_cipher_definition_t *def;
  105. if( NULL == cipher_name )
  106. return( NULL );
  107. for( def = mbedtls_cipher_definitions; def->info != NULL; def++ )
  108. if( ! strcmp( def->info->name, cipher_name ) )
  109. return( def->info );
  110. return( NULL );
  111. }
  112. const mbedtls_cipher_info_t *mbedtls_cipher_info_from_values( const mbedtls_cipher_id_t cipher_id,
  113. int key_bitlen,
  114. const mbedtls_cipher_mode_t mode )
  115. {
  116. const mbedtls_cipher_definition_t *def;
  117. for( def = mbedtls_cipher_definitions; def->info != NULL; def++ )
  118. if( def->info->base->cipher == cipher_id &&
  119. def->info->key_bitlen == (unsigned) key_bitlen &&
  120. def->info->mode == mode )
  121. return( def->info );
  122. return( NULL );
  123. }
  124. void mbedtls_cipher_init( mbedtls_cipher_context_t *ctx )
  125. {
  126. CIPHER_VALIDATE( ctx != NULL );
  127. memset( ctx, 0, sizeof( mbedtls_cipher_context_t ) );
  128. }
  129. void mbedtls_cipher_free( mbedtls_cipher_context_t *ctx )
  130. {
  131. if( ctx == NULL )
  132. return;
  133. #if defined(MBEDTLS_CMAC_C)
  134. if( ctx->cmac_ctx )
  135. {
  136. mbedtls_platform_zeroize( ctx->cmac_ctx,
  137. sizeof( mbedtls_cmac_context_t ) );
  138. mbedtls_free( ctx->cmac_ctx );
  139. }
  140. #endif
  141. if( ctx->cipher_ctx )
  142. ctx->cipher_info->base->ctx_free_func( ctx->cipher_ctx );
  143. mbedtls_platform_zeroize( ctx, sizeof(mbedtls_cipher_context_t) );
  144. }
  145. int mbedtls_cipher_setup( mbedtls_cipher_context_t *ctx, const mbedtls_cipher_info_t *cipher_info )
  146. {
  147. CIPHER_VALIDATE_RET( ctx != NULL );
  148. if( cipher_info == NULL )
  149. return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
  150. memset( ctx, 0, sizeof( mbedtls_cipher_context_t ) );
  151. if( NULL == ( ctx->cipher_ctx = cipher_info->base->ctx_alloc_func() ) )
  152. return( MBEDTLS_ERR_CIPHER_ALLOC_FAILED );
  153. ctx->cipher_info = cipher_info;
  154. #if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
  155. /*
  156. * Ignore possible errors caused by a cipher mode that doesn't use padding
  157. */
  158. #if defined(MBEDTLS_CIPHER_PADDING_PKCS7)
  159. (void) mbedtls_cipher_set_padding_mode( ctx, MBEDTLS_PADDING_PKCS7 );
  160. #else
  161. (void) mbedtls_cipher_set_padding_mode( ctx, MBEDTLS_PADDING_NONE );
  162. #endif
  163. #endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
  164. return( 0 );
  165. }
  166. int mbedtls_cipher_setkey( mbedtls_cipher_context_t *ctx,
  167. const unsigned char *key,
  168. int key_bitlen,
  169. const mbedtls_operation_t operation )
  170. {
  171. CIPHER_VALIDATE_RET( ctx != NULL );
  172. CIPHER_VALIDATE_RET( key != NULL );
  173. CIPHER_VALIDATE_RET( operation == MBEDTLS_ENCRYPT ||
  174. operation == MBEDTLS_DECRYPT );
  175. if( ctx->cipher_info == NULL )
  176. return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
  177. if( ( ctx->cipher_info->flags & MBEDTLS_CIPHER_VARIABLE_KEY_LEN ) == 0 &&
  178. (int) ctx->cipher_info->key_bitlen != key_bitlen )
  179. {
  180. return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
  181. }
  182. ctx->key_bitlen = key_bitlen;
  183. ctx->operation = operation;
  184. /*
  185. * For OFB, CFB and CTR mode always use the encryption key schedule
  186. */
  187. if( MBEDTLS_ENCRYPT == operation ||
  188. MBEDTLS_MODE_CFB == ctx->cipher_info->mode ||
  189. MBEDTLS_MODE_OFB == ctx->cipher_info->mode ||
  190. MBEDTLS_MODE_CTR == ctx->cipher_info->mode )
  191. {
  192. return( ctx->cipher_info->base->setkey_enc_func( ctx->cipher_ctx, key,
  193. ctx->key_bitlen ) );
  194. }
  195. if( MBEDTLS_DECRYPT == operation )
  196. return( ctx->cipher_info->base->setkey_dec_func( ctx->cipher_ctx, key,
  197. ctx->key_bitlen ) );
  198. return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
  199. }
  200. int mbedtls_cipher_set_iv( mbedtls_cipher_context_t *ctx,
  201. const unsigned char *iv,
  202. size_t iv_len )
  203. {
  204. size_t actual_iv_size;
  205. CIPHER_VALIDATE_RET( ctx != NULL );
  206. CIPHER_VALIDATE_RET( iv_len == 0 || iv != NULL );
  207. if( ctx->cipher_info == NULL )
  208. return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
  209. /* avoid buffer overflow in ctx->iv */
  210. if( iv_len > MBEDTLS_MAX_IV_LENGTH )
  211. return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
  212. if( ( ctx->cipher_info->flags & MBEDTLS_CIPHER_VARIABLE_IV_LEN ) != 0 )
  213. actual_iv_size = iv_len;
  214. else
  215. {
  216. actual_iv_size = ctx->cipher_info->iv_size;
  217. /* avoid reading past the end of input buffer */
  218. if( actual_iv_size > iv_len )
  219. return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
  220. }
  221. #if defined(MBEDTLS_CHACHA20_C)
  222. if ( ctx->cipher_info->type == MBEDTLS_CIPHER_CHACHA20 )
  223. {
  224. if ( 0 != mbedtls_chacha20_starts( (mbedtls_chacha20_context*)ctx->cipher_ctx,
  225. iv,
  226. 0U ) ) /* Initial counter value */
  227. {
  228. return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
  229. }
  230. }
  231. #endif
  232. if ( actual_iv_size != 0 )
  233. {
  234. memcpy( ctx->iv, iv, actual_iv_size );
  235. ctx->iv_size = actual_iv_size;
  236. }
  237. return( 0 );
  238. }
  239. int mbedtls_cipher_reset( mbedtls_cipher_context_t *ctx )
  240. {
  241. CIPHER_VALIDATE_RET( ctx != NULL );
  242. if( ctx->cipher_info == NULL )
  243. return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
  244. ctx->unprocessed_len = 0;
  245. return( 0 );
  246. }
  247. #if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
  248. int mbedtls_cipher_update_ad( mbedtls_cipher_context_t *ctx,
  249. const unsigned char *ad, size_t ad_len )
  250. {
  251. CIPHER_VALIDATE_RET( ctx != NULL );
  252. CIPHER_VALIDATE_RET( ad_len == 0 || ad != NULL );
  253. if( ctx->cipher_info == NULL )
  254. return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
  255. #if defined(MBEDTLS_GCM_C)
  256. if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
  257. {
  258. return( mbedtls_gcm_starts( (mbedtls_gcm_context *) ctx->cipher_ctx, ctx->operation,
  259. ctx->iv, ctx->iv_size, ad, ad_len ) );
  260. }
  261. #endif
  262. #if defined(MBEDTLS_CHACHAPOLY_C)
  263. if (MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type )
  264. {
  265. int result;
  266. mbedtls_chachapoly_mode_t mode;
  267. mode = ( ctx->operation == MBEDTLS_ENCRYPT )
  268. ? MBEDTLS_CHACHAPOLY_ENCRYPT
  269. : MBEDTLS_CHACHAPOLY_DECRYPT;
  270. result = mbedtls_chachapoly_starts( (mbedtls_chachapoly_context*) ctx->cipher_ctx,
  271. ctx->iv,
  272. mode );
  273. if ( result != 0 )
  274. return( result );
  275. return( mbedtls_chachapoly_update_aad( (mbedtls_chachapoly_context*) ctx->cipher_ctx,
  276. ad, ad_len ) );
  277. }
  278. #endif
  279. return( 0 );
  280. }
  281. #endif /* MBEDTLS_GCM_C || MBEDTLS_CHACHAPOLY_C */
  282. int mbedtls_cipher_update( mbedtls_cipher_context_t *ctx, const unsigned char *input,
  283. size_t ilen, unsigned char *output, size_t *olen )
  284. {
  285. int ret;
  286. size_t block_size;
  287. CIPHER_VALIDATE_RET( ctx != NULL );
  288. CIPHER_VALIDATE_RET( ilen == 0 || input != NULL );
  289. CIPHER_VALIDATE_RET( output != NULL );
  290. CIPHER_VALIDATE_RET( olen != NULL );
  291. if( ctx->cipher_info == NULL )
  292. return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
  293. *olen = 0;
  294. block_size = mbedtls_cipher_get_block_size( ctx );
  295. if ( 0 == block_size )
  296. {
  297. return( MBEDTLS_ERR_CIPHER_INVALID_CONTEXT );
  298. }
  299. if( ctx->cipher_info->mode == MBEDTLS_MODE_ECB )
  300. {
  301. if( ilen != block_size )
  302. return( MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED );
  303. *olen = ilen;
  304. if( 0 != ( ret = ctx->cipher_info->base->ecb_func( ctx->cipher_ctx,
  305. ctx->operation, input, output ) ) )
  306. {
  307. return( ret );
  308. }
  309. return( 0 );
  310. }
  311. #if defined(MBEDTLS_GCM_C)
  312. if( ctx->cipher_info->mode == MBEDTLS_MODE_GCM )
  313. {
  314. *olen = ilen;
  315. return( mbedtls_gcm_update( (mbedtls_gcm_context *) ctx->cipher_ctx, ilen, input,
  316. output ) );
  317. }
  318. #endif
  319. #if defined(MBEDTLS_CHACHAPOLY_C)
  320. if ( ctx->cipher_info->type == MBEDTLS_CIPHER_CHACHA20_POLY1305 )
  321. {
  322. *olen = ilen;
  323. return( mbedtls_chachapoly_update( (mbedtls_chachapoly_context*) ctx->cipher_ctx,
  324. ilen, input, output ) );
  325. }
  326. #endif
  327. if( input == output &&
  328. ( ctx->unprocessed_len != 0 || ilen % block_size ) )
  329. {
  330. return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
  331. }
  332. #if defined(MBEDTLS_CIPHER_MODE_CBC)
  333. if( ctx->cipher_info->mode == MBEDTLS_MODE_CBC )
  334. {
  335. size_t copy_len = 0;
  336. /*
  337. * If there is not enough data for a full block, cache it.
  338. */
  339. if( ( ctx->operation == MBEDTLS_DECRYPT && NULL != ctx->add_padding &&
  340. ilen <= block_size - ctx->unprocessed_len ) ||
  341. ( ctx->operation == MBEDTLS_DECRYPT && NULL == ctx->add_padding &&
  342. ilen < block_size - ctx->unprocessed_len ) ||
  343. ( ctx->operation == MBEDTLS_ENCRYPT &&
  344. ilen < block_size - ctx->unprocessed_len ) )
  345. {
  346. memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ), input,
  347. ilen );
  348. ctx->unprocessed_len += ilen;
  349. return( 0 );
  350. }
  351. /*
  352. * Process cached data first
  353. */
  354. if( 0 != ctx->unprocessed_len )
  355. {
  356. copy_len = block_size - ctx->unprocessed_len;
  357. memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ), input,
  358. copy_len );
  359. if( 0 != ( ret = ctx->cipher_info->base->cbc_func( ctx->cipher_ctx,
  360. ctx->operation, block_size, ctx->iv,
  361. ctx->unprocessed_data, output ) ) )
  362. {
  363. return( ret );
  364. }
  365. *olen += block_size;
  366. output += block_size;
  367. ctx->unprocessed_len = 0;
  368. input += copy_len;
  369. ilen -= copy_len;
  370. }
  371. /*
  372. * Cache final, incomplete block
  373. */
  374. if( 0 != ilen )
  375. {
  376. /* Encryption: only cache partial blocks
  377. * Decryption w/ padding: always keep at least one whole block
  378. * Decryption w/o padding: only cache partial blocks
  379. */
  380. copy_len = ilen % block_size;
  381. if( copy_len == 0 &&
  382. ctx->operation == MBEDTLS_DECRYPT &&
  383. NULL != ctx->add_padding)
  384. {
  385. copy_len = block_size;
  386. }
  387. memcpy( ctx->unprocessed_data, &( input[ilen - copy_len] ),
  388. copy_len );
  389. ctx->unprocessed_len += copy_len;
  390. ilen -= copy_len;
  391. }
  392. /*
  393. * Process remaining full blocks
  394. */
  395. if( ilen )
  396. {
  397. if( 0 != ( ret = ctx->cipher_info->base->cbc_func( ctx->cipher_ctx,
  398. ctx->operation, ilen, ctx->iv, input, output ) ) )
  399. {
  400. return( ret );
  401. }
  402. *olen += ilen;
  403. }
  404. return( 0 );
  405. }
  406. #endif /* MBEDTLS_CIPHER_MODE_CBC */
  407. #if defined(MBEDTLS_CIPHER_MODE_CFB)
  408. if( ctx->cipher_info->mode == MBEDTLS_MODE_CFB )
  409. {
  410. if( 0 != ( ret = ctx->cipher_info->base->cfb_func( ctx->cipher_ctx,
  411. ctx->operation, ilen, &ctx->unprocessed_len, ctx->iv,
  412. input, output ) ) )
  413. {
  414. return( ret );
  415. }
  416. *olen = ilen;
  417. return( 0 );
  418. }
  419. #endif /* MBEDTLS_CIPHER_MODE_CFB */
  420. #if defined(MBEDTLS_CIPHER_MODE_OFB)
  421. if( ctx->cipher_info->mode == MBEDTLS_MODE_OFB )
  422. {
  423. if( 0 != ( ret = ctx->cipher_info->base->ofb_func( ctx->cipher_ctx,
  424. ilen, &ctx->unprocessed_len, ctx->iv, input, output ) ) )
  425. {
  426. return( ret );
  427. }
  428. *olen = ilen;
  429. return( 0 );
  430. }
  431. #endif /* MBEDTLS_CIPHER_MODE_OFB */
  432. #if defined(MBEDTLS_CIPHER_MODE_CTR)
  433. if( ctx->cipher_info->mode == MBEDTLS_MODE_CTR )
  434. {
  435. if( 0 != ( ret = ctx->cipher_info->base->ctr_func( ctx->cipher_ctx,
  436. ilen, &ctx->unprocessed_len, ctx->iv,
  437. ctx->unprocessed_data, input, output ) ) )
  438. {
  439. return( ret );
  440. }
  441. *olen = ilen;
  442. return( 0 );
  443. }
  444. #endif /* MBEDTLS_CIPHER_MODE_CTR */
  445. #if defined(MBEDTLS_CIPHER_MODE_XTS)
  446. if( ctx->cipher_info->mode == MBEDTLS_MODE_XTS )
  447. {
  448. if( ctx->unprocessed_len > 0 ) {
  449. /* We can only process an entire data unit at a time. */
  450. return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
  451. }
  452. ret = ctx->cipher_info->base->xts_func( ctx->cipher_ctx,
  453. ctx->operation, ilen, ctx->iv, input, output );
  454. if( ret != 0 )
  455. {
  456. return( ret );
  457. }
  458. *olen = ilen;
  459. return( 0 );
  460. }
  461. #endif /* MBEDTLS_CIPHER_MODE_XTS */
  462. #if defined(MBEDTLS_CIPHER_MODE_STREAM)
  463. if( ctx->cipher_info->mode == MBEDTLS_MODE_STREAM )
  464. {
  465. if( 0 != ( ret = ctx->cipher_info->base->stream_func( ctx->cipher_ctx,
  466. ilen, input, output ) ) )
  467. {
  468. return( ret );
  469. }
  470. *olen = ilen;
  471. return( 0 );
  472. }
  473. #endif /* MBEDTLS_CIPHER_MODE_STREAM */
  474. return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
  475. }
  476. #if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
  477. #if defined(MBEDTLS_CIPHER_PADDING_PKCS7)
  478. /*
  479. * PKCS7 (and PKCS5) padding: fill with ll bytes, with ll = padding_len
  480. */
  481. static void add_pkcs_padding( unsigned char *output, size_t output_len,
  482. size_t data_len )
  483. {
  484. size_t padding_len = output_len - data_len;
  485. unsigned char i;
  486. for( i = 0; i < padding_len; i++ )
  487. output[data_len + i] = (unsigned char) padding_len;
  488. }
  489. static int get_pkcs_padding( unsigned char *input, size_t input_len,
  490. size_t *data_len )
  491. {
  492. size_t i, pad_idx;
  493. unsigned char padding_len, bad = 0;
  494. if( NULL == input || NULL == data_len )
  495. return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
  496. padding_len = input[input_len - 1];
  497. *data_len = input_len - padding_len;
  498. /* Avoid logical || since it results in a branch */
  499. bad |= padding_len > input_len;
  500. bad |= padding_len == 0;
  501. /* The number of bytes checked must be independent of padding_len,
  502. * so pick input_len, which is usually 8 or 16 (one block) */
  503. pad_idx = input_len - padding_len;
  504. for( i = 0; i < input_len; i++ )
  505. bad |= ( input[i] ^ padding_len ) * ( i >= pad_idx );
  506. return( MBEDTLS_ERR_CIPHER_INVALID_PADDING * ( bad != 0 ) );
  507. }
  508. #endif /* MBEDTLS_CIPHER_PADDING_PKCS7 */
  509. #if defined(MBEDTLS_CIPHER_PADDING_ONE_AND_ZEROS)
  510. /*
  511. * One and zeros padding: fill with 80 00 ... 00
  512. */
  513. static void add_one_and_zeros_padding( unsigned char *output,
  514. size_t output_len, size_t data_len )
  515. {
  516. size_t padding_len = output_len - data_len;
  517. unsigned char i = 0;
  518. output[data_len] = 0x80;
  519. for( i = 1; i < padding_len; i++ )
  520. output[data_len + i] = 0x00;
  521. }
  522. static int get_one_and_zeros_padding( unsigned char *input, size_t input_len,
  523. size_t *data_len )
  524. {
  525. size_t i;
  526. unsigned char done = 0, prev_done, bad;
  527. if( NULL == input || NULL == data_len )
  528. return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
  529. bad = 0x80;
  530. *data_len = 0;
  531. for( i = input_len; i > 0; i-- )
  532. {
  533. prev_done = done;
  534. done |= ( input[i - 1] != 0 );
  535. *data_len |= ( i - 1 ) * ( done != prev_done );
  536. bad ^= input[i - 1] * ( done != prev_done );
  537. }
  538. return( MBEDTLS_ERR_CIPHER_INVALID_PADDING * ( bad != 0 ) );
  539. }
  540. #endif /* MBEDTLS_CIPHER_PADDING_ONE_AND_ZEROS */
  541. #if defined(MBEDTLS_CIPHER_PADDING_ZEROS_AND_LEN)
  542. /*
  543. * Zeros and len padding: fill with 00 ... 00 ll, where ll is padding length
  544. */
  545. static void add_zeros_and_len_padding( unsigned char *output,
  546. size_t output_len, size_t data_len )
  547. {
  548. size_t padding_len = output_len - data_len;
  549. unsigned char i = 0;
  550. for( i = 1; i < padding_len; i++ )
  551. output[data_len + i - 1] = 0x00;
  552. output[output_len - 1] = (unsigned char) padding_len;
  553. }
  554. static int get_zeros_and_len_padding( unsigned char *input, size_t input_len,
  555. size_t *data_len )
  556. {
  557. size_t i, pad_idx;
  558. unsigned char padding_len, bad = 0;
  559. if( NULL == input || NULL == data_len )
  560. return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
  561. padding_len = input[input_len - 1];
  562. *data_len = input_len - padding_len;
  563. /* Avoid logical || since it results in a branch */
  564. bad |= padding_len > input_len;
  565. bad |= padding_len == 0;
  566. /* The number of bytes checked must be independent of padding_len */
  567. pad_idx = input_len - padding_len;
  568. for( i = 0; i < input_len - 1; i++ )
  569. bad |= input[i] * ( i >= pad_idx );
  570. return( MBEDTLS_ERR_CIPHER_INVALID_PADDING * ( bad != 0 ) );
  571. }
  572. #endif /* MBEDTLS_CIPHER_PADDING_ZEROS_AND_LEN */
  573. #if defined(MBEDTLS_CIPHER_PADDING_ZEROS)
  574. /*
  575. * Zero padding: fill with 00 ... 00
  576. */
  577. static void add_zeros_padding( unsigned char *output,
  578. size_t output_len, size_t data_len )
  579. {
  580. size_t i;
  581. for( i = data_len; i < output_len; i++ )
  582. output[i] = 0x00;
  583. }
  584. static int get_zeros_padding( unsigned char *input, size_t input_len,
  585. size_t *data_len )
  586. {
  587. size_t i;
  588. unsigned char done = 0, prev_done;
  589. if( NULL == input || NULL == data_len )
  590. return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
  591. *data_len = 0;
  592. for( i = input_len; i > 0; i-- )
  593. {
  594. prev_done = done;
  595. done |= ( input[i-1] != 0 );
  596. *data_len |= i * ( done != prev_done );
  597. }
  598. return( 0 );
  599. }
  600. #endif /* MBEDTLS_CIPHER_PADDING_ZEROS */
  601. /*
  602. * No padding: don't pad :)
  603. *
  604. * There is no add_padding function (check for NULL in mbedtls_cipher_finish)
  605. * but a trivial get_padding function
  606. */
  607. static int get_no_padding( unsigned char *input, size_t input_len,
  608. size_t *data_len )
  609. {
  610. if( NULL == input || NULL == data_len )
  611. return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
  612. *data_len = input_len;
  613. return( 0 );
  614. }
  615. #endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
  616. int mbedtls_cipher_finish( mbedtls_cipher_context_t *ctx,
  617. unsigned char *output, size_t *olen )
  618. {
  619. CIPHER_VALIDATE_RET( ctx != NULL );
  620. CIPHER_VALIDATE_RET( output != NULL );
  621. CIPHER_VALIDATE_RET( olen != NULL );
  622. if( ctx->cipher_info == NULL )
  623. return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
  624. *olen = 0;
  625. if( MBEDTLS_MODE_CFB == ctx->cipher_info->mode ||
  626. MBEDTLS_MODE_OFB == ctx->cipher_info->mode ||
  627. MBEDTLS_MODE_CTR == ctx->cipher_info->mode ||
  628. MBEDTLS_MODE_GCM == ctx->cipher_info->mode ||
  629. MBEDTLS_MODE_XTS == ctx->cipher_info->mode ||
  630. MBEDTLS_MODE_STREAM == ctx->cipher_info->mode )
  631. {
  632. return( 0 );
  633. }
  634. if ( ( MBEDTLS_CIPHER_CHACHA20 == ctx->cipher_info->type ) ||
  635. ( MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type ) )
  636. {
  637. return( 0 );
  638. }
  639. if( MBEDTLS_MODE_ECB == ctx->cipher_info->mode )
  640. {
  641. if( ctx->unprocessed_len != 0 )
  642. return( MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED );
  643. return( 0 );
  644. }
  645. #if defined(MBEDTLS_CIPHER_MODE_CBC)
  646. if( MBEDTLS_MODE_CBC == ctx->cipher_info->mode )
  647. {
  648. int ret = 0;
  649. if( MBEDTLS_ENCRYPT == ctx->operation )
  650. {
  651. /* check for 'no padding' mode */
  652. if( NULL == ctx->add_padding )
  653. {
  654. if( 0 != ctx->unprocessed_len )
  655. return( MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED );
  656. return( 0 );
  657. }
  658. ctx->add_padding( ctx->unprocessed_data, mbedtls_cipher_get_iv_size( ctx ),
  659. ctx->unprocessed_len );
  660. }
  661. else if( mbedtls_cipher_get_block_size( ctx ) != ctx->unprocessed_len )
  662. {
  663. /*
  664. * For decrypt operations, expect a full block,
  665. * or an empty block if no padding
  666. */
  667. if( NULL == ctx->add_padding && 0 == ctx->unprocessed_len )
  668. return( 0 );
  669. return( MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED );
  670. }
  671. /* cipher block */
  672. if( 0 != ( ret = ctx->cipher_info->base->cbc_func( ctx->cipher_ctx,
  673. ctx->operation, mbedtls_cipher_get_block_size( ctx ), ctx->iv,
  674. ctx->unprocessed_data, output ) ) )
  675. {
  676. return( ret );
  677. }
  678. /* Set output size for decryption */
  679. if( MBEDTLS_DECRYPT == ctx->operation )
  680. return( ctx->get_padding( output, mbedtls_cipher_get_block_size( ctx ),
  681. olen ) );
  682. /* Set output size for encryption */
  683. *olen = mbedtls_cipher_get_block_size( ctx );
  684. return( 0 );
  685. }
  686. #else
  687. ((void) output);
  688. #endif /* MBEDTLS_CIPHER_MODE_CBC */
  689. return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
  690. }
  691. #if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
  692. int mbedtls_cipher_set_padding_mode( mbedtls_cipher_context_t *ctx,
  693. mbedtls_cipher_padding_t mode )
  694. {
  695. CIPHER_VALIDATE_RET( ctx != NULL );
  696. if( NULL == ctx->cipher_info || MBEDTLS_MODE_CBC != ctx->cipher_info->mode )
  697. {
  698. return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
  699. }
  700. switch( mode )
  701. {
  702. #if defined(MBEDTLS_CIPHER_PADDING_PKCS7)
  703. case MBEDTLS_PADDING_PKCS7:
  704. ctx->add_padding = add_pkcs_padding;
  705. ctx->get_padding = get_pkcs_padding;
  706. break;
  707. #endif
  708. #if defined(MBEDTLS_CIPHER_PADDING_ONE_AND_ZEROS)
  709. case MBEDTLS_PADDING_ONE_AND_ZEROS:
  710. ctx->add_padding = add_one_and_zeros_padding;
  711. ctx->get_padding = get_one_and_zeros_padding;
  712. break;
  713. #endif
  714. #if defined(MBEDTLS_CIPHER_PADDING_ZEROS_AND_LEN)
  715. case MBEDTLS_PADDING_ZEROS_AND_LEN:
  716. ctx->add_padding = add_zeros_and_len_padding;
  717. ctx->get_padding = get_zeros_and_len_padding;
  718. break;
  719. #endif
  720. #if defined(MBEDTLS_CIPHER_PADDING_ZEROS)
  721. case MBEDTLS_PADDING_ZEROS:
  722. ctx->add_padding = add_zeros_padding;
  723. ctx->get_padding = get_zeros_padding;
  724. break;
  725. #endif
  726. case MBEDTLS_PADDING_NONE:
  727. ctx->add_padding = NULL;
  728. ctx->get_padding = get_no_padding;
  729. break;
  730. default:
  731. return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
  732. }
  733. return( 0 );
  734. }
  735. #endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
  736. #if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
  737. int mbedtls_cipher_write_tag( mbedtls_cipher_context_t *ctx,
  738. unsigned char *tag, size_t tag_len )
  739. {
  740. CIPHER_VALIDATE_RET( ctx != NULL );
  741. CIPHER_VALIDATE_RET( tag_len == 0 || tag != NULL );
  742. if( ctx->cipher_info == NULL )
  743. return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
  744. if( MBEDTLS_ENCRYPT != ctx->operation )
  745. return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
  746. #if defined(MBEDTLS_GCM_C)
  747. if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
  748. return( mbedtls_gcm_finish( (mbedtls_gcm_context *) ctx->cipher_ctx,
  749. tag, tag_len ) );
  750. #endif
  751. #if defined(MBEDTLS_CHACHAPOLY_C)
  752. if ( MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type )
  753. {
  754. /* Don't allow truncated MAC for Poly1305 */
  755. if ( tag_len != 16U )
  756. return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
  757. return( mbedtls_chachapoly_finish( (mbedtls_chachapoly_context*) ctx->cipher_ctx,
  758. tag ) );
  759. }
  760. #endif
  761. return( 0 );
  762. }
  763. int mbedtls_cipher_check_tag( mbedtls_cipher_context_t *ctx,
  764. const unsigned char *tag, size_t tag_len )
  765. {
  766. unsigned char check_tag[16];
  767. int ret;
  768. CIPHER_VALIDATE_RET( ctx != NULL );
  769. CIPHER_VALIDATE_RET( tag_len == 0 || tag != NULL );
  770. if( ctx->cipher_info == NULL )
  771. return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
  772. if( MBEDTLS_DECRYPT != ctx->operation )
  773. {
  774. return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
  775. }
  776. #if defined(MBEDTLS_GCM_C)
  777. if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
  778. {
  779. if( tag_len > sizeof( check_tag ) )
  780. return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
  781. if( 0 != ( ret = mbedtls_gcm_finish( (mbedtls_gcm_context *) ctx->cipher_ctx,
  782. check_tag, tag_len ) ) )
  783. {
  784. return( ret );
  785. }
  786. /* Check the tag in "constant-time" */
  787. if( mbedtls_constant_time_memcmp( tag, check_tag, tag_len ) != 0 )
  788. return( MBEDTLS_ERR_CIPHER_AUTH_FAILED );
  789. return( 0 );
  790. }
  791. #endif /* MBEDTLS_GCM_C */
  792. #if defined(MBEDTLS_CHACHAPOLY_C)
  793. if ( MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type )
  794. {
  795. /* Don't allow truncated MAC for Poly1305 */
  796. if ( tag_len != sizeof( check_tag ) )
  797. return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
  798. ret = mbedtls_chachapoly_finish( (mbedtls_chachapoly_context*) ctx->cipher_ctx,
  799. check_tag );
  800. if ( ret != 0 )
  801. {
  802. return( ret );
  803. }
  804. /* Check the tag in "constant-time" */
  805. if( mbedtls_constant_time_memcmp( tag, check_tag, tag_len ) != 0 )
  806. return( MBEDTLS_ERR_CIPHER_AUTH_FAILED );
  807. return( 0 );
  808. }
  809. #endif /* MBEDTLS_CHACHAPOLY_C */
  810. return( 0 );
  811. }
  812. #endif /* MBEDTLS_GCM_C || MBEDTLS_CHACHAPOLY_C */
  813. /*
  814. * Packet-oriented wrapper for non-AEAD modes
  815. */
  816. int mbedtls_cipher_crypt( mbedtls_cipher_context_t *ctx,
  817. const unsigned char *iv, size_t iv_len,
  818. const unsigned char *input, size_t ilen,
  819. unsigned char *output, size_t *olen )
  820. {
  821. int ret;
  822. size_t finish_olen;
  823. CIPHER_VALIDATE_RET( ctx != NULL );
  824. CIPHER_VALIDATE_RET( iv_len == 0 || iv != NULL );
  825. CIPHER_VALIDATE_RET( ilen == 0 || input != NULL );
  826. CIPHER_VALIDATE_RET( output != NULL );
  827. CIPHER_VALIDATE_RET( olen != NULL );
  828. if( ( ret = mbedtls_cipher_set_iv( ctx, iv, iv_len ) ) != 0 )
  829. return( ret );
  830. if( ( ret = mbedtls_cipher_reset( ctx ) ) != 0 )
  831. return( ret );
  832. if( ( ret = mbedtls_cipher_update( ctx, input, ilen, output, olen ) ) != 0 )
  833. return( ret );
  834. if( ( ret = mbedtls_cipher_finish( ctx, output + *olen, &finish_olen ) ) != 0 )
  835. return( ret );
  836. *olen += finish_olen;
  837. return( 0 );
  838. }
  839. #if defined(MBEDTLS_CIPHER_MODE_AEAD)
  840. /*
  841. * Packet-oriented encryption for AEAD modes
  842. */
  843. int mbedtls_cipher_auth_encrypt( mbedtls_cipher_context_t *ctx,
  844. const unsigned char *iv, size_t iv_len,
  845. const unsigned char *ad, size_t ad_len,
  846. const unsigned char *input, size_t ilen,
  847. unsigned char *output, size_t *olen,
  848. unsigned char *tag, size_t tag_len )
  849. {
  850. CIPHER_VALIDATE_RET( ctx != NULL );
  851. CIPHER_VALIDATE_RET( iv != NULL );
  852. CIPHER_VALIDATE_RET( ad_len == 0 || ad != NULL );
  853. CIPHER_VALIDATE_RET( ilen == 0 || input != NULL );
  854. CIPHER_VALIDATE_RET( output != NULL );
  855. CIPHER_VALIDATE_RET( olen != NULL );
  856. CIPHER_VALIDATE_RET( tag_len == 0 || tag != NULL );
  857. #if defined(MBEDTLS_GCM_C)
  858. if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
  859. {
  860. *olen = ilen;
  861. return( mbedtls_gcm_crypt_and_tag( ctx->cipher_ctx, MBEDTLS_GCM_ENCRYPT, ilen,
  862. iv, iv_len, ad, ad_len, input, output,
  863. tag_len, tag ) );
  864. }
  865. #endif /* MBEDTLS_GCM_C */
  866. #if defined(MBEDTLS_CCM_C)
  867. if( MBEDTLS_MODE_CCM == ctx->cipher_info->mode )
  868. {
  869. *olen = ilen;
  870. return( mbedtls_ccm_encrypt_and_tag( ctx->cipher_ctx, ilen,
  871. iv, iv_len, ad, ad_len, input, output,
  872. tag, tag_len ) );
  873. }
  874. #endif /* MBEDTLS_CCM_C */
  875. #if defined(MBEDTLS_CHACHAPOLY_C)
  876. if ( MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type )
  877. {
  878. /* ChachaPoly has fixed length nonce and MAC (tag) */
  879. if ( ( iv_len != ctx->cipher_info->iv_size ) ||
  880. ( tag_len != 16U ) )
  881. {
  882. return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
  883. }
  884. *olen = ilen;
  885. return( mbedtls_chachapoly_encrypt_and_tag( ctx->cipher_ctx,
  886. ilen, iv, ad, ad_len, input, output, tag ) );
  887. }
  888. #endif /* MBEDTLS_CHACHAPOLY_C */
  889. return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
  890. }
  891. /*
  892. * Packet-oriented decryption for AEAD modes
  893. */
  894. int mbedtls_cipher_auth_decrypt( mbedtls_cipher_context_t *ctx,
  895. const unsigned char *iv, size_t iv_len,
  896. const unsigned char *ad, size_t ad_len,
  897. const unsigned char *input, size_t ilen,
  898. unsigned char *output, size_t *olen,
  899. const unsigned char *tag, size_t tag_len )
  900. {
  901. CIPHER_VALIDATE_RET( ctx != NULL );
  902. CIPHER_VALIDATE_RET( iv != NULL );
  903. CIPHER_VALIDATE_RET( ad_len == 0 || ad != NULL );
  904. CIPHER_VALIDATE_RET( ilen == 0 || input != NULL );
  905. CIPHER_VALIDATE_RET( output != NULL );
  906. CIPHER_VALIDATE_RET( olen != NULL );
  907. CIPHER_VALIDATE_RET( tag_len == 0 || tag != NULL );
  908. #if defined(MBEDTLS_GCM_C)
  909. if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
  910. {
  911. int ret;
  912. *olen = ilen;
  913. ret = mbedtls_gcm_auth_decrypt( ctx->cipher_ctx, ilen,
  914. iv, iv_len, ad, ad_len,
  915. tag, tag_len, input, output );
  916. if( ret == MBEDTLS_ERR_GCM_AUTH_FAILED )
  917. ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
  918. return( ret );
  919. }
  920. #endif /* MBEDTLS_GCM_C */
  921. #if defined(MBEDTLS_CCM_C)
  922. if( MBEDTLS_MODE_CCM == ctx->cipher_info->mode )
  923. {
  924. int ret;
  925. *olen = ilen;
  926. ret = mbedtls_ccm_auth_decrypt( ctx->cipher_ctx, ilen,
  927. iv, iv_len, ad, ad_len,
  928. input, output, tag, tag_len );
  929. if( ret == MBEDTLS_ERR_CCM_AUTH_FAILED )
  930. ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
  931. return( ret );
  932. }
  933. #endif /* MBEDTLS_CCM_C */
  934. #if defined(MBEDTLS_CHACHAPOLY_C)
  935. if ( MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type )
  936. {
  937. int ret;
  938. /* ChachaPoly has fixed length nonce and MAC (tag) */
  939. if ( ( iv_len != ctx->cipher_info->iv_size ) ||
  940. ( tag_len != 16U ) )
  941. {
  942. return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
  943. }
  944. *olen = ilen;
  945. ret = mbedtls_chachapoly_auth_decrypt( ctx->cipher_ctx, ilen,
  946. iv, ad, ad_len, tag, input, output );
  947. if( ret == MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED )
  948. ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
  949. return( ret );
  950. }
  951. #endif /* MBEDTLS_CHACHAPOLY_C */
  952. return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
  953. }
  954. #endif /* MBEDTLS_CIPHER_MODE_AEAD */
  955. #endif /* MBEDTLS_CIPHER_C */