cipher.h 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709
  1. /**
  2. * \file cipher.h
  3. *
  4. * \brief Generic cipher wrapper.
  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. #ifndef MBEDTLS_CIPHER_H
  26. #define MBEDTLS_CIPHER_H
  27. #if !defined(MBEDTLS_CONFIG_FILE)
  28. #include "config.h"
  29. #else
  30. #include MBEDTLS_CONFIG_FILE
  31. #endif
  32. #include <stddef.h>
  33. #if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CCM_C)
  34. #define MBEDTLS_CIPHER_MODE_AEAD
  35. #endif
  36. #if defined(MBEDTLS_CIPHER_MODE_CBC)
  37. #define MBEDTLS_CIPHER_MODE_WITH_PADDING
  38. #endif
  39. #if defined(MBEDTLS_ARC4_C)
  40. #define MBEDTLS_CIPHER_MODE_STREAM
  41. #endif
  42. #if ( defined(__ARMCC_VERSION) || defined(_MSC_VER) ) && \
  43. !defined(inline) && !defined(__cplusplus)
  44. #define inline __inline
  45. #endif
  46. #define MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE -0x6080 /**< The selected feature is not available. */
  47. #define MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA -0x6100 /**< Bad input parameters to function. */
  48. #define MBEDTLS_ERR_CIPHER_ALLOC_FAILED -0x6180 /**< Failed to allocate memory. */
  49. #define MBEDTLS_ERR_CIPHER_INVALID_PADDING -0x6200 /**< Input data contains invalid padding and is rejected. */
  50. #define MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED -0x6280 /**< Decryption of block requires a full block. */
  51. #define MBEDTLS_ERR_CIPHER_AUTH_FAILED -0x6300 /**< Authentication failed (for AEAD modes). */
  52. #define MBEDTLS_ERR_CIPHER_INVALID_CONTEXT -0x6380 /**< The context is invalid, eg because it was free()ed. */
  53. #define MBEDTLS_CIPHER_VARIABLE_IV_LEN 0x01 /**< Cipher accepts IVs of variable length */
  54. #define MBEDTLS_CIPHER_VARIABLE_KEY_LEN 0x02 /**< Cipher accepts keys of variable length */
  55. #ifdef __cplusplus
  56. extern "C" {
  57. #endif
  58. typedef enum {
  59. MBEDTLS_CIPHER_ID_NONE = 0,
  60. MBEDTLS_CIPHER_ID_NULL,
  61. MBEDTLS_CIPHER_ID_AES,
  62. MBEDTLS_CIPHER_ID_DES,
  63. MBEDTLS_CIPHER_ID_3DES,
  64. MBEDTLS_CIPHER_ID_CAMELLIA,
  65. MBEDTLS_CIPHER_ID_BLOWFISH,
  66. MBEDTLS_CIPHER_ID_ARC4,
  67. } mbedtls_cipher_id_t;
  68. typedef enum {
  69. MBEDTLS_CIPHER_NONE = 0,
  70. MBEDTLS_CIPHER_NULL,
  71. MBEDTLS_CIPHER_AES_128_ECB,
  72. MBEDTLS_CIPHER_AES_192_ECB,
  73. MBEDTLS_CIPHER_AES_256_ECB,
  74. MBEDTLS_CIPHER_AES_128_CBC,
  75. MBEDTLS_CIPHER_AES_192_CBC,
  76. MBEDTLS_CIPHER_AES_256_CBC,
  77. MBEDTLS_CIPHER_AES_128_CFB128,
  78. MBEDTLS_CIPHER_AES_192_CFB128,
  79. MBEDTLS_CIPHER_AES_256_CFB128,
  80. MBEDTLS_CIPHER_AES_128_CTR,
  81. MBEDTLS_CIPHER_AES_192_CTR,
  82. MBEDTLS_CIPHER_AES_256_CTR,
  83. MBEDTLS_CIPHER_AES_128_GCM,
  84. MBEDTLS_CIPHER_AES_192_GCM,
  85. MBEDTLS_CIPHER_AES_256_GCM,
  86. MBEDTLS_CIPHER_CAMELLIA_128_ECB,
  87. MBEDTLS_CIPHER_CAMELLIA_192_ECB,
  88. MBEDTLS_CIPHER_CAMELLIA_256_ECB,
  89. MBEDTLS_CIPHER_CAMELLIA_128_CBC,
  90. MBEDTLS_CIPHER_CAMELLIA_192_CBC,
  91. MBEDTLS_CIPHER_CAMELLIA_256_CBC,
  92. MBEDTLS_CIPHER_CAMELLIA_128_CFB128,
  93. MBEDTLS_CIPHER_CAMELLIA_192_CFB128,
  94. MBEDTLS_CIPHER_CAMELLIA_256_CFB128,
  95. MBEDTLS_CIPHER_CAMELLIA_128_CTR,
  96. MBEDTLS_CIPHER_CAMELLIA_192_CTR,
  97. MBEDTLS_CIPHER_CAMELLIA_256_CTR,
  98. MBEDTLS_CIPHER_CAMELLIA_128_GCM,
  99. MBEDTLS_CIPHER_CAMELLIA_192_GCM,
  100. MBEDTLS_CIPHER_CAMELLIA_256_GCM,
  101. MBEDTLS_CIPHER_DES_ECB,
  102. MBEDTLS_CIPHER_DES_CBC,
  103. MBEDTLS_CIPHER_DES_EDE_ECB,
  104. MBEDTLS_CIPHER_DES_EDE_CBC,
  105. MBEDTLS_CIPHER_DES_EDE3_ECB,
  106. MBEDTLS_CIPHER_DES_EDE3_CBC,
  107. MBEDTLS_CIPHER_BLOWFISH_ECB,
  108. MBEDTLS_CIPHER_BLOWFISH_CBC,
  109. MBEDTLS_CIPHER_BLOWFISH_CFB64,
  110. MBEDTLS_CIPHER_BLOWFISH_CTR,
  111. MBEDTLS_CIPHER_ARC4_128,
  112. MBEDTLS_CIPHER_AES_128_CCM,
  113. MBEDTLS_CIPHER_AES_192_CCM,
  114. MBEDTLS_CIPHER_AES_256_CCM,
  115. MBEDTLS_CIPHER_CAMELLIA_128_CCM,
  116. MBEDTLS_CIPHER_CAMELLIA_192_CCM,
  117. MBEDTLS_CIPHER_CAMELLIA_256_CCM,
  118. } mbedtls_cipher_type_t;
  119. typedef enum {
  120. MBEDTLS_MODE_NONE = 0,
  121. MBEDTLS_MODE_ECB,
  122. MBEDTLS_MODE_CBC,
  123. MBEDTLS_MODE_CFB,
  124. MBEDTLS_MODE_OFB, /* Unused! */
  125. MBEDTLS_MODE_CTR,
  126. MBEDTLS_MODE_GCM,
  127. MBEDTLS_MODE_STREAM,
  128. MBEDTLS_MODE_CCM,
  129. } mbedtls_cipher_mode_t;
  130. typedef enum {
  131. MBEDTLS_PADDING_PKCS7 = 0, /**< PKCS7 padding (default) */
  132. MBEDTLS_PADDING_ONE_AND_ZEROS, /**< ISO/IEC 7816-4 padding */
  133. MBEDTLS_PADDING_ZEROS_AND_LEN, /**< ANSI X.923 padding */
  134. MBEDTLS_PADDING_ZEROS, /**< zero padding (not reversible!) */
  135. MBEDTLS_PADDING_NONE, /**< never pad (full blocks only) */
  136. } mbedtls_cipher_padding_t;
  137. typedef enum {
  138. MBEDTLS_OPERATION_NONE = -1,
  139. MBEDTLS_DECRYPT = 0,
  140. MBEDTLS_ENCRYPT,
  141. } mbedtls_operation_t;
  142. enum {
  143. /** Undefined key length */
  144. MBEDTLS_KEY_LENGTH_NONE = 0,
  145. /** Key length, in bits (including parity), for DES keys */
  146. MBEDTLS_KEY_LENGTH_DES = 64,
  147. /** Key length, in bits (including parity), for DES in two key EDE */
  148. MBEDTLS_KEY_LENGTH_DES_EDE = 128,
  149. /** Key length, in bits (including parity), for DES in three-key EDE */
  150. MBEDTLS_KEY_LENGTH_DES_EDE3 = 192,
  151. };
  152. /** Maximum length of any IV, in bytes */
  153. #define MBEDTLS_MAX_IV_LENGTH 16
  154. /** Maximum block size of any cipher, in bytes */
  155. #define MBEDTLS_MAX_BLOCK_LENGTH 16
  156. /**
  157. * Base cipher information (opaque struct).
  158. */
  159. typedef struct mbedtls_cipher_base_t mbedtls_cipher_base_t;
  160. /**
  161. * CMAC context (opaque struct).
  162. */
  163. typedef struct mbedtls_cmac_context_t mbedtls_cmac_context_t;
  164. /**
  165. * Cipher information. Allows cipher functions to be called in a generic way.
  166. */
  167. typedef struct {
  168. /** Full cipher identifier (e.g. MBEDTLS_CIPHER_AES_256_CBC) */
  169. mbedtls_cipher_type_t type;
  170. /** Cipher mode (e.g. MBEDTLS_MODE_CBC) */
  171. mbedtls_cipher_mode_t mode;
  172. /** Cipher key length, in bits (default length for variable sized ciphers)
  173. * (Includes parity bits for ciphers like DES) */
  174. unsigned int key_bitlen;
  175. /** Name of the cipher */
  176. const char * name;
  177. /** IV/NONCE size, in bytes.
  178. * For cipher that accept many sizes: recommended size */
  179. unsigned int iv_size;
  180. /** Flags for variable IV size, variable key size, etc. */
  181. int flags;
  182. /** block size, in bytes */
  183. unsigned int block_size;
  184. /** Base cipher information and functions */
  185. const mbedtls_cipher_base_t *base;
  186. } mbedtls_cipher_info_t;
  187. /**
  188. * Generic cipher context.
  189. */
  190. typedef struct {
  191. /** Information about the associated cipher */
  192. const mbedtls_cipher_info_t *cipher_info;
  193. /** Key length to use */
  194. int key_bitlen;
  195. /** Operation that the context's key has been initialised for */
  196. mbedtls_operation_t operation;
  197. #if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
  198. /** Padding functions to use, if relevant for cipher mode */
  199. void (*add_padding)( unsigned char *output, size_t olen, size_t data_len );
  200. int (*get_padding)( unsigned char *input, size_t ilen, size_t *data_len );
  201. #endif
  202. /** Buffer for data that hasn't been encrypted yet */
  203. unsigned char unprocessed_data[MBEDTLS_MAX_BLOCK_LENGTH];
  204. /** Number of bytes that still need processing */
  205. size_t unprocessed_len;
  206. /** Current IV or NONCE_COUNTER for CTR-mode */
  207. unsigned char iv[MBEDTLS_MAX_IV_LENGTH];
  208. /** IV size in bytes (for ciphers with variable-length IVs) */
  209. size_t iv_size;
  210. /** Cipher-specific context */
  211. void *cipher_ctx;
  212. #if defined(MBEDTLS_CMAC_C)
  213. /** CMAC Specific context */
  214. mbedtls_cmac_context_t *cmac_ctx;
  215. #endif
  216. } mbedtls_cipher_context_t;
  217. /**
  218. * \brief Returns the list of ciphers supported by the generic cipher module.
  219. *
  220. * \return a statically allocated array of ciphers, the last entry
  221. * is 0.
  222. */
  223. const int *mbedtls_cipher_list( void );
  224. /**
  225. * \brief Returns the cipher information structure associated
  226. * with the given cipher name.
  227. *
  228. * \param cipher_name Name of the cipher to search for.
  229. *
  230. * \return the cipher information structure associated with the
  231. * given cipher_name, or NULL if not found.
  232. */
  233. const mbedtls_cipher_info_t *mbedtls_cipher_info_from_string( const char *cipher_name );
  234. /**
  235. * \brief Returns the cipher information structure associated
  236. * with the given cipher type.
  237. *
  238. * \param cipher_type Type of the cipher to search for.
  239. *
  240. * \return the cipher information structure associated with the
  241. * given cipher_type, or NULL if not found.
  242. */
  243. const mbedtls_cipher_info_t *mbedtls_cipher_info_from_type( const mbedtls_cipher_type_t cipher_type );
  244. /**
  245. * \brief Returns the cipher information structure associated
  246. * with the given cipher id, key size and mode.
  247. *
  248. * \param cipher_id Id of the cipher to search for
  249. * (e.g. MBEDTLS_CIPHER_ID_AES)
  250. * \param key_bitlen Length of the key in bits
  251. * \param mode Cipher mode (e.g. MBEDTLS_MODE_CBC)
  252. *
  253. * \return the cipher information structure associated with the
  254. * given cipher_type, or NULL if not found.
  255. */
  256. const mbedtls_cipher_info_t *mbedtls_cipher_info_from_values( const mbedtls_cipher_id_t cipher_id,
  257. int key_bitlen,
  258. const mbedtls_cipher_mode_t mode );
  259. /**
  260. * \brief Initialize a cipher_context (as NONE)
  261. */
  262. void mbedtls_cipher_init( mbedtls_cipher_context_t *ctx );
  263. /**
  264. * \brief Free and clear the cipher-specific context of ctx.
  265. * Freeing ctx itself remains the responsibility of the
  266. * caller.
  267. */
  268. void mbedtls_cipher_free( mbedtls_cipher_context_t *ctx );
  269. /**
  270. * \brief Initialises and fills the cipher context structure with
  271. * the appropriate values.
  272. *
  273. * \note Currently also clears structure. In future versions you
  274. * will be required to call mbedtls_cipher_init() on the structure
  275. * first.
  276. *
  277. * \param ctx context to initialise. May not be NULL.
  278. * \param cipher_info cipher to use.
  279. *
  280. * \return 0 on success,
  281. * MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA on parameter failure,
  282. * MBEDTLS_ERR_CIPHER_ALLOC_FAILED if allocation of the
  283. * cipher-specific context failed.
  284. */
  285. int mbedtls_cipher_setup( mbedtls_cipher_context_t *ctx, const mbedtls_cipher_info_t *cipher_info );
  286. /**
  287. * \brief Returns the block size of the given cipher.
  288. *
  289. * \param ctx cipher's context. Must have been initialised.
  290. *
  291. * \return size of the cipher's blocks, or 0 if ctx has not been
  292. * initialised.
  293. */
  294. static inline unsigned int mbedtls_cipher_get_block_size( const mbedtls_cipher_context_t *ctx )
  295. {
  296. if( NULL == ctx || NULL == ctx->cipher_info )
  297. return 0;
  298. return ctx->cipher_info->block_size;
  299. }
  300. /**
  301. * \brief Returns the mode of operation for the cipher.
  302. * (e.g. MBEDTLS_MODE_CBC)
  303. *
  304. * \param ctx cipher's context. Must have been initialised.
  305. *
  306. * \return mode of operation, or MBEDTLS_MODE_NONE if ctx
  307. * has not been initialised.
  308. */
  309. static inline mbedtls_cipher_mode_t mbedtls_cipher_get_cipher_mode( const mbedtls_cipher_context_t *ctx )
  310. {
  311. if( NULL == ctx || NULL == ctx->cipher_info )
  312. return MBEDTLS_MODE_NONE;
  313. return ctx->cipher_info->mode;
  314. }
  315. /**
  316. * \brief Returns the size of the cipher's IV/NONCE in bytes.
  317. *
  318. * \param ctx cipher's context. Must have been initialised.
  319. *
  320. * \return If IV has not been set yet: (recommended) IV size
  321. * (0 for ciphers not using IV/NONCE).
  322. * If IV has already been set: actual size.
  323. */
  324. static inline int mbedtls_cipher_get_iv_size( const mbedtls_cipher_context_t *ctx )
  325. {
  326. if( NULL == ctx || NULL == ctx->cipher_info )
  327. return 0;
  328. if( ctx->iv_size != 0 )
  329. return (int) ctx->iv_size;
  330. return (int) ctx->cipher_info->iv_size;
  331. }
  332. /**
  333. * \brief Returns the type of the given cipher.
  334. *
  335. * \param ctx cipher's context. Must have been initialised.
  336. *
  337. * \return type of the cipher, or MBEDTLS_CIPHER_NONE if ctx has
  338. * not been initialised.
  339. */
  340. static inline mbedtls_cipher_type_t mbedtls_cipher_get_type( const mbedtls_cipher_context_t *ctx )
  341. {
  342. if( NULL == ctx || NULL == ctx->cipher_info )
  343. return MBEDTLS_CIPHER_NONE;
  344. return ctx->cipher_info->type;
  345. }
  346. /**
  347. * \brief Returns the name of the given cipher, as a string.
  348. *
  349. * \param ctx cipher's context. Must have been initialised.
  350. *
  351. * \return name of the cipher, or NULL if ctx was not initialised.
  352. */
  353. static inline const char *mbedtls_cipher_get_name( const mbedtls_cipher_context_t *ctx )
  354. {
  355. if( NULL == ctx || NULL == ctx->cipher_info )
  356. return 0;
  357. return ctx->cipher_info->name;
  358. }
  359. /**
  360. * \brief Returns the key length of the cipher.
  361. *
  362. * \param ctx cipher's context. Must have been initialised.
  363. *
  364. * \return cipher's key length, in bits, or
  365. * MBEDTLS_KEY_LENGTH_NONE if ctx has not been
  366. * initialised.
  367. */
  368. static inline int mbedtls_cipher_get_key_bitlen( const mbedtls_cipher_context_t *ctx )
  369. {
  370. if( NULL == ctx || NULL == ctx->cipher_info )
  371. return MBEDTLS_KEY_LENGTH_NONE;
  372. return (int) ctx->cipher_info->key_bitlen;
  373. }
  374. /**
  375. * \brief Returns the operation of the given cipher.
  376. *
  377. * \param ctx cipher's context. Must have been initialised.
  378. *
  379. * \return operation (MBEDTLS_ENCRYPT or MBEDTLS_DECRYPT),
  380. * or MBEDTLS_OPERATION_NONE if ctx has not been
  381. * initialised.
  382. */
  383. static inline mbedtls_operation_t mbedtls_cipher_get_operation( const mbedtls_cipher_context_t *ctx )
  384. {
  385. if( NULL == ctx || NULL == ctx->cipher_info )
  386. return MBEDTLS_OPERATION_NONE;
  387. return ctx->operation;
  388. }
  389. /**
  390. * \brief Set the key to use with the given context.
  391. *
  392. * \param ctx generic cipher context. May not be NULL. Must have been
  393. * initialised using cipher_context_from_type or
  394. * cipher_context_from_string.
  395. * \param key The key to use.
  396. * \param key_bitlen key length to use, in bits.
  397. * \param operation Operation that the key will be used for, either
  398. * MBEDTLS_ENCRYPT or MBEDTLS_DECRYPT.
  399. *
  400. * \returns 0 on success, MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA if
  401. * parameter verification fails or a cipher specific
  402. * error code.
  403. */
  404. int mbedtls_cipher_setkey( mbedtls_cipher_context_t *ctx, const unsigned char *key,
  405. int key_bitlen, const mbedtls_operation_t operation );
  406. #if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
  407. /**
  408. * \brief Set padding mode, for cipher modes that use padding.
  409. * (Default: PKCS7 padding.)
  410. *
  411. * \param ctx generic cipher context
  412. * \param mode padding mode
  413. *
  414. * \returns 0 on success, MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE
  415. * if selected padding mode is not supported, or
  416. * MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA if the cipher mode
  417. * does not support padding.
  418. */
  419. int mbedtls_cipher_set_padding_mode( mbedtls_cipher_context_t *ctx, mbedtls_cipher_padding_t mode );
  420. #endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
  421. /**
  422. * \brief Set the initialization vector (IV) or nonce
  423. *
  424. * \param ctx generic cipher context
  425. * \param iv IV to use (or NONCE_COUNTER for CTR-mode ciphers)
  426. * \param iv_len IV length for ciphers with variable-size IV;
  427. * discarded by ciphers with fixed-size IV.
  428. *
  429. * \returns 0 on success, or MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA
  430. *
  431. * \note Some ciphers don't use IVs nor NONCE. For these
  432. * ciphers, this function has no effect.
  433. */
  434. int mbedtls_cipher_set_iv( mbedtls_cipher_context_t *ctx,
  435. const unsigned char *iv, size_t iv_len );
  436. /**
  437. * \brief Finish preparation of the given context
  438. *
  439. * \param ctx generic cipher context
  440. *
  441. * \returns 0 on success, MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA
  442. * if parameter verification fails.
  443. */
  444. int mbedtls_cipher_reset( mbedtls_cipher_context_t *ctx );
  445. #if defined(MBEDTLS_GCM_C)
  446. /**
  447. * \brief Add additional data (for AEAD ciphers).
  448. * Currently only supported with GCM.
  449. * Must be called exactly once, after mbedtls_cipher_reset().
  450. *
  451. * \param ctx generic cipher context
  452. * \param ad Additional data to use.
  453. * \param ad_len Length of ad.
  454. *
  455. * \return 0 on success, or a specific error code.
  456. */
  457. int mbedtls_cipher_update_ad( mbedtls_cipher_context_t *ctx,
  458. const unsigned char *ad, size_t ad_len );
  459. #endif /* MBEDTLS_GCM_C */
  460. /**
  461. * \brief Generic cipher update function. Encrypts/decrypts
  462. * using the given cipher context. Writes as many block
  463. * size'd blocks of data as possible to output. Any data
  464. * that cannot be written immediately will either be added
  465. * to the next block, or flushed when cipher_final is
  466. * called.
  467. * Exception: for MBEDTLS_MODE_ECB, expects single block
  468. * in size (e.g. 16 bytes for AES)
  469. *
  470. * \param ctx generic cipher context
  471. * \param input buffer holding the input data
  472. * \param ilen length of the input data
  473. * \param output buffer for the output data. Should be able to hold at
  474. * least ilen + block_size. Cannot be the same buffer as
  475. * input!
  476. * \param olen length of the output data, will be filled with the
  477. * actual number of bytes written.
  478. *
  479. * \returns 0 on success, MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA if
  480. * parameter verification fails,
  481. * MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE on an
  482. * unsupported mode for a cipher or a cipher specific
  483. * error code.
  484. *
  485. * \note If the underlying cipher is GCM, all calls to this
  486. * function, except the last one before mbedtls_cipher_finish(),
  487. * must have ilen a multiple of the block size.
  488. */
  489. int mbedtls_cipher_update( mbedtls_cipher_context_t *ctx, const unsigned char *input,
  490. size_t ilen, unsigned char *output, size_t *olen );
  491. /**
  492. * \brief Generic cipher finalisation function. If data still
  493. * needs to be flushed from an incomplete block, data
  494. * contained within it will be padded with the size of
  495. * the last block, and written to the output buffer.
  496. *
  497. * \param ctx Generic cipher context
  498. * \param output buffer to write data to. Needs block_size available.
  499. * \param olen length of the data written to the output buffer.
  500. *
  501. * \returns 0 on success, MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA if
  502. * parameter verification fails,
  503. * MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED if decryption
  504. * expected a full block but was not provided one,
  505. * MBEDTLS_ERR_CIPHER_INVALID_PADDING on invalid padding
  506. * while decrypting or a cipher specific error code.
  507. */
  508. int mbedtls_cipher_finish( mbedtls_cipher_context_t *ctx,
  509. unsigned char *output, size_t *olen );
  510. #if defined(MBEDTLS_GCM_C)
  511. /**
  512. * \brief Write tag for AEAD ciphers.
  513. * Currently only supported with GCM.
  514. * Must be called after mbedtls_cipher_finish().
  515. *
  516. * \param ctx Generic cipher context
  517. * \param tag buffer to write the tag
  518. * \param tag_len Length of the tag to write
  519. *
  520. * \return 0 on success, or a specific error code.
  521. */
  522. int mbedtls_cipher_write_tag( mbedtls_cipher_context_t *ctx,
  523. unsigned char *tag, size_t tag_len );
  524. /**
  525. * \brief Check tag for AEAD ciphers.
  526. * Currently only supported with GCM.
  527. * Must be called after mbedtls_cipher_finish().
  528. *
  529. * \param ctx Generic cipher context
  530. * \param tag Buffer holding the tag
  531. * \param tag_len Length of the tag to check
  532. *
  533. * \return 0 on success, or a specific error code.
  534. */
  535. int mbedtls_cipher_check_tag( mbedtls_cipher_context_t *ctx,
  536. const unsigned char *tag, size_t tag_len );
  537. #endif /* MBEDTLS_GCM_C */
  538. /**
  539. * \brief Generic all-in-one encryption/decryption
  540. * (for all ciphers except AEAD constructs).
  541. *
  542. * \param ctx generic cipher context
  543. * \param iv IV to use (or NONCE_COUNTER for CTR-mode ciphers)
  544. * \param iv_len IV length for ciphers with variable-size IV;
  545. * discarded by ciphers with fixed-size IV.
  546. * \param input buffer holding the input data
  547. * \param ilen length of the input data
  548. * \param output buffer for the output data. Should be able to hold at
  549. * least ilen + block_size. Cannot be the same buffer as
  550. * input!
  551. * \param olen length of the output data, will be filled with the
  552. * actual number of bytes written.
  553. *
  554. * \note Some ciphers don't use IVs nor NONCE. For these
  555. * ciphers, use iv = NULL and iv_len = 0.
  556. *
  557. * \returns 0 on success, or
  558. * MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA, or
  559. * MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED if decryption
  560. * expected a full block but was not provided one, or
  561. * MBEDTLS_ERR_CIPHER_INVALID_PADDING on invalid padding
  562. * while decrypting, or
  563. * a cipher specific error code.
  564. */
  565. int mbedtls_cipher_crypt( mbedtls_cipher_context_t *ctx,
  566. const unsigned char *iv, size_t iv_len,
  567. const unsigned char *input, size_t ilen,
  568. unsigned char *output, size_t *olen );
  569. #if defined(MBEDTLS_CIPHER_MODE_AEAD)
  570. /**
  571. * \brief Generic autenticated encryption (AEAD ciphers).
  572. *
  573. * \param ctx generic cipher context
  574. * \param iv IV to use (or NONCE_COUNTER for CTR-mode ciphers)
  575. * \param iv_len IV length for ciphers with variable-size IV;
  576. * discarded by ciphers with fixed-size IV.
  577. * \param ad Additional data to authenticate.
  578. * \param ad_len Length of ad.
  579. * \param input buffer holding the input data
  580. * \param ilen length of the input data
  581. * \param output buffer for the output data.
  582. * Should be able to hold at least ilen.
  583. * \param olen length of the output data, will be filled with the
  584. * actual number of bytes written.
  585. * \param tag buffer for the authentication tag
  586. * \param tag_len desired tag length
  587. *
  588. * \returns 0 on success, or
  589. * MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA, or
  590. * a cipher specific error code.
  591. */
  592. int mbedtls_cipher_auth_encrypt( mbedtls_cipher_context_t *ctx,
  593. const unsigned char *iv, size_t iv_len,
  594. const unsigned char *ad, size_t ad_len,
  595. const unsigned char *input, size_t ilen,
  596. unsigned char *output, size_t *olen,
  597. unsigned char *tag, size_t tag_len );
  598. /**
  599. * \brief Generic autenticated decryption (AEAD ciphers).
  600. *
  601. * \param ctx generic cipher context
  602. * \param iv IV to use (or NONCE_COUNTER for CTR-mode ciphers)
  603. * \param iv_len IV length for ciphers with variable-size IV;
  604. * discarded by ciphers with fixed-size IV.
  605. * \param ad Additional data to be authenticated.
  606. * \param ad_len Length of ad.
  607. * \param input buffer holding the input data
  608. * \param ilen length of the input data
  609. * \param output buffer for the output data.
  610. * Should be able to hold at least ilen.
  611. * \param olen length of the output data, will be filled with the
  612. * actual number of bytes written.
  613. * \param tag buffer holding the authentication tag
  614. * \param tag_len length of the authentication tag
  615. *
  616. * \returns 0 on success, or
  617. * MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA, or
  618. * MBEDTLS_ERR_CIPHER_AUTH_FAILED if data isn't authentic,
  619. * or a cipher specific error code.
  620. *
  621. * \note If the data is not authentic, then the output buffer
  622. * is zeroed out to prevent the unauthentic plaintext to
  623. * be used by mistake, making this interface safer.
  624. */
  625. int mbedtls_cipher_auth_decrypt( mbedtls_cipher_context_t *ctx,
  626. const unsigned char *iv, size_t iv_len,
  627. const unsigned char *ad, size_t ad_len,
  628. const unsigned char *input, size_t ilen,
  629. unsigned char *output, size_t *olen,
  630. const unsigned char *tag, size_t tag_len );
  631. #endif /* MBEDTLS_CIPHER_MODE_AEAD */
  632. #ifdef __cplusplus
  633. }
  634. #endif
  635. #endif /* MBEDTLS_CIPHER_H */