cipher.c 32 KB

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