cipher.h 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803
  1. /**
  2. * \file cipher.h
  3. *
  4. * \brief This file contains an abstraction interface for use with the cipher
  5. * primitives provided by the library. It provides a common interface to all of
  6. * the available cipher operations.
  7. *
  8. * \author Adriaan de Jong <[email protected]>
  9. */
  10. /*
  11. * Copyright (C) 2006-2018, Arm Limited (or its affiliates), All Rights Reserved
  12. * SPDX-License-Identifier: Apache-2.0
  13. *
  14. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  15. * not use this file except in compliance with the License.
  16. * You may obtain a copy of the License at
  17. *
  18. * http://www.apache.org/licenses/LICENSE-2.0
  19. *
  20. * Unless required by applicable law or agreed to in writing, software
  21. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  22. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  23. * See the License for the specific language governing permissions and
  24. * limitations under the License.
  25. *
  26. * This file is part of Mbed TLS (https://tls.mbed.org)
  27. */
  28. #ifndef MBEDTLS_CIPHER_H
  29. #define MBEDTLS_CIPHER_H
  30. #if !defined(MBEDTLS_CONFIG_FILE)
  31. #include "config.h"
  32. #else
  33. #include MBEDTLS_CONFIG_FILE
  34. #endif
  35. #include <stddef.h>
  36. #if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
  37. #define MBEDTLS_CIPHER_MODE_AEAD
  38. #endif
  39. #if defined(MBEDTLS_CIPHER_MODE_CBC)
  40. #define MBEDTLS_CIPHER_MODE_WITH_PADDING
  41. #endif
  42. #if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
  43. #define MBEDTLS_CIPHER_MODE_STREAM
  44. #endif
  45. #if ( defined(__ARMCC_VERSION) || defined(_MSC_VER) ) && \
  46. !defined(inline) && !defined(__cplusplus)
  47. #define inline __inline
  48. #endif
  49. #define MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE -0x6080 /**< The selected feature is not available. */
  50. #define MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA -0x6100 /**< Bad input parameters. */
  51. #define MBEDTLS_ERR_CIPHER_ALLOC_FAILED -0x6180 /**< Failed to allocate memory. */
  52. #define MBEDTLS_ERR_CIPHER_INVALID_PADDING -0x6200 /**< Input data contains invalid padding and is rejected. */
  53. #define MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED -0x6280 /**< Decryption of block requires a full block. */
  54. #define MBEDTLS_ERR_CIPHER_AUTH_FAILED -0x6300 /**< Authentication failed (for AEAD modes). */
  55. #define MBEDTLS_ERR_CIPHER_INVALID_CONTEXT -0x6380 /**< The context is invalid. For example, because it was freed. */
  56. #define MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED -0x6400 /**< Cipher hardware accelerator failed. */
  57. #define MBEDTLS_CIPHER_VARIABLE_IV_LEN 0x01 /**< Cipher accepts IVs of variable length. */
  58. #define MBEDTLS_CIPHER_VARIABLE_KEY_LEN 0x02 /**< Cipher accepts keys of variable length. */
  59. #ifdef __cplusplus
  60. extern "C" {
  61. #endif
  62. /**
  63. * \brief Supported cipher types.
  64. *
  65. * \warning RC4 and DES are considered weak ciphers and their use
  66. * constitutes a security risk. Arm recommends considering stronger
  67. * ciphers instead.
  68. */
  69. typedef enum {
  70. MBEDTLS_CIPHER_ID_NONE = 0, /**< Placeholder to mark the end of cipher ID lists. */
  71. MBEDTLS_CIPHER_ID_NULL, /**< The identity cipher, treated as a stream cipher. */
  72. MBEDTLS_CIPHER_ID_AES, /**< The AES cipher. */
  73. MBEDTLS_CIPHER_ID_DES, /**< The DES cipher. */
  74. MBEDTLS_CIPHER_ID_3DES, /**< The Triple DES cipher. */
  75. MBEDTLS_CIPHER_ID_CAMELLIA, /**< The Camellia cipher. */
  76. MBEDTLS_CIPHER_ID_BLOWFISH, /**< The Blowfish cipher. */
  77. MBEDTLS_CIPHER_ID_ARC4, /**< The RC4 cipher. */
  78. MBEDTLS_CIPHER_ID_ARIA, /**< The Aria cipher. */
  79. MBEDTLS_CIPHER_ID_CHACHA20, /**< The ChaCha20 cipher. */
  80. } mbedtls_cipher_id_t;
  81. /**
  82. * \brief Supported {cipher type, cipher mode} pairs.
  83. *
  84. * \warning RC4 and DES are considered weak ciphers and their use
  85. * constitutes a security risk. Arm recommends considering stronger
  86. * ciphers instead.
  87. */
  88. typedef enum {
  89. MBEDTLS_CIPHER_NONE = 0, /**< Placeholder to mark the end of cipher-pair lists. */
  90. MBEDTLS_CIPHER_NULL, /**< The identity stream cipher. */
  91. MBEDTLS_CIPHER_AES_128_ECB, /**< AES cipher with 128-bit ECB mode. */
  92. MBEDTLS_CIPHER_AES_192_ECB, /**< AES cipher with 192-bit ECB mode. */
  93. MBEDTLS_CIPHER_AES_256_ECB, /**< AES cipher with 256-bit ECB mode. */
  94. MBEDTLS_CIPHER_AES_128_CBC, /**< AES cipher with 128-bit CBC mode. */
  95. MBEDTLS_CIPHER_AES_192_CBC, /**< AES cipher with 192-bit CBC mode. */
  96. MBEDTLS_CIPHER_AES_256_CBC, /**< AES cipher with 256-bit CBC mode. */
  97. MBEDTLS_CIPHER_AES_128_CFB128, /**< AES cipher with 128-bit CFB128 mode. */
  98. MBEDTLS_CIPHER_AES_192_CFB128, /**< AES cipher with 192-bit CFB128 mode. */
  99. MBEDTLS_CIPHER_AES_256_CFB128, /**< AES cipher with 256-bit CFB128 mode. */
  100. MBEDTLS_CIPHER_AES_128_CTR, /**< AES cipher with 128-bit CTR mode. */
  101. MBEDTLS_CIPHER_AES_192_CTR, /**< AES cipher with 192-bit CTR mode. */
  102. MBEDTLS_CIPHER_AES_256_CTR, /**< AES cipher with 256-bit CTR mode. */
  103. MBEDTLS_CIPHER_AES_128_GCM, /**< AES cipher with 128-bit GCM mode. */
  104. MBEDTLS_CIPHER_AES_192_GCM, /**< AES cipher with 192-bit GCM mode. */
  105. MBEDTLS_CIPHER_AES_256_GCM, /**< AES cipher with 256-bit GCM mode. */
  106. MBEDTLS_CIPHER_CAMELLIA_128_ECB, /**< Camellia cipher with 128-bit ECB mode. */
  107. MBEDTLS_CIPHER_CAMELLIA_192_ECB, /**< Camellia cipher with 192-bit ECB mode. */
  108. MBEDTLS_CIPHER_CAMELLIA_256_ECB, /**< Camellia cipher with 256-bit ECB mode. */
  109. MBEDTLS_CIPHER_CAMELLIA_128_CBC, /**< Camellia cipher with 128-bit CBC mode. */
  110. MBEDTLS_CIPHER_CAMELLIA_192_CBC, /**< Camellia cipher with 192-bit CBC mode. */
  111. MBEDTLS_CIPHER_CAMELLIA_256_CBC, /**< Camellia cipher with 256-bit CBC mode. */
  112. MBEDTLS_CIPHER_CAMELLIA_128_CFB128, /**< Camellia cipher with 128-bit CFB128 mode. */
  113. MBEDTLS_CIPHER_CAMELLIA_192_CFB128, /**< Camellia cipher with 192-bit CFB128 mode. */
  114. MBEDTLS_CIPHER_CAMELLIA_256_CFB128, /**< Camellia cipher with 256-bit CFB128 mode. */
  115. MBEDTLS_CIPHER_CAMELLIA_128_CTR, /**< Camellia cipher with 128-bit CTR mode. */
  116. MBEDTLS_CIPHER_CAMELLIA_192_CTR, /**< Camellia cipher with 192-bit CTR mode. */
  117. MBEDTLS_CIPHER_CAMELLIA_256_CTR, /**< Camellia cipher with 256-bit CTR mode. */
  118. MBEDTLS_CIPHER_CAMELLIA_128_GCM, /**< Camellia cipher with 128-bit GCM mode. */
  119. MBEDTLS_CIPHER_CAMELLIA_192_GCM, /**< Camellia cipher with 192-bit GCM mode. */
  120. MBEDTLS_CIPHER_CAMELLIA_256_GCM, /**< Camellia cipher with 256-bit GCM mode. */
  121. MBEDTLS_CIPHER_DES_ECB, /**< DES cipher with ECB mode. */
  122. MBEDTLS_CIPHER_DES_CBC, /**< DES cipher with CBC mode. */
  123. MBEDTLS_CIPHER_DES_EDE_ECB, /**< DES cipher with EDE ECB mode. */
  124. MBEDTLS_CIPHER_DES_EDE_CBC, /**< DES cipher with EDE CBC mode. */
  125. MBEDTLS_CIPHER_DES_EDE3_ECB, /**< DES cipher with EDE3 ECB mode. */
  126. MBEDTLS_CIPHER_DES_EDE3_CBC, /**< DES cipher with EDE3 CBC mode. */
  127. MBEDTLS_CIPHER_BLOWFISH_ECB, /**< Blowfish cipher with ECB mode. */
  128. MBEDTLS_CIPHER_BLOWFISH_CBC, /**< Blowfish cipher with CBC mode. */
  129. MBEDTLS_CIPHER_BLOWFISH_CFB64, /**< Blowfish cipher with CFB64 mode. */
  130. MBEDTLS_CIPHER_BLOWFISH_CTR, /**< Blowfish cipher with CTR mode. */
  131. MBEDTLS_CIPHER_ARC4_128, /**< RC4 cipher with 128-bit mode. */
  132. MBEDTLS_CIPHER_AES_128_CCM, /**< AES cipher with 128-bit CCM mode. */
  133. MBEDTLS_CIPHER_AES_192_CCM, /**< AES cipher with 192-bit CCM mode. */
  134. MBEDTLS_CIPHER_AES_256_CCM, /**< AES cipher with 256-bit CCM mode. */
  135. MBEDTLS_CIPHER_CAMELLIA_128_CCM, /**< Camellia cipher with 128-bit CCM mode. */
  136. MBEDTLS_CIPHER_CAMELLIA_192_CCM, /**< Camellia cipher with 192-bit CCM mode. */
  137. MBEDTLS_CIPHER_CAMELLIA_256_CCM, /**< Camellia cipher with 256-bit CCM mode. */
  138. MBEDTLS_CIPHER_ARIA_128_ECB, /**< Aria cipher with 128-bit key and ECB mode. */
  139. MBEDTLS_CIPHER_ARIA_192_ECB, /**< Aria cipher with 192-bit key and ECB mode. */
  140. MBEDTLS_CIPHER_ARIA_256_ECB, /**< Aria cipher with 256-bit key and ECB mode. */
  141. MBEDTLS_CIPHER_ARIA_128_CBC, /**< Aria cipher with 128-bit key and CBC mode. */
  142. MBEDTLS_CIPHER_ARIA_192_CBC, /**< Aria cipher with 192-bit key and CBC mode. */
  143. MBEDTLS_CIPHER_ARIA_256_CBC, /**< Aria cipher with 256-bit key and CBC mode. */
  144. MBEDTLS_CIPHER_ARIA_128_CFB128, /**< Aria cipher with 128-bit key and CFB-128 mode. */
  145. MBEDTLS_CIPHER_ARIA_192_CFB128, /**< Aria cipher with 192-bit key and CFB-128 mode. */
  146. MBEDTLS_CIPHER_ARIA_256_CFB128, /**< Aria cipher with 256-bit key and CFB-128 mode. */
  147. MBEDTLS_CIPHER_ARIA_128_CTR, /**< Aria cipher with 128-bit key and CTR mode. */
  148. MBEDTLS_CIPHER_ARIA_192_CTR, /**< Aria cipher with 192-bit key and CTR mode. */
  149. MBEDTLS_CIPHER_ARIA_256_CTR, /**< Aria cipher with 256-bit key and CTR mode. */
  150. MBEDTLS_CIPHER_ARIA_128_GCM, /**< Aria cipher with 128-bit key and GCM mode. */
  151. MBEDTLS_CIPHER_ARIA_192_GCM, /**< Aria cipher with 192-bit key and GCM mode. */
  152. MBEDTLS_CIPHER_ARIA_256_GCM, /**< Aria cipher with 256-bit key and GCM mode. */
  153. MBEDTLS_CIPHER_ARIA_128_CCM, /**< Aria cipher with 128-bit key and CCM mode. */
  154. MBEDTLS_CIPHER_ARIA_192_CCM, /**< Aria cipher with 192-bit key and CCM mode. */
  155. MBEDTLS_CIPHER_ARIA_256_CCM, /**< Aria cipher with 256-bit key and CCM mode. */
  156. MBEDTLS_CIPHER_AES_128_OFB, /**< AES 128-bit cipher in OFB mode. */
  157. MBEDTLS_CIPHER_AES_192_OFB, /**< AES 192-bit cipher in OFB mode. */
  158. MBEDTLS_CIPHER_AES_256_OFB, /**< AES 256-bit cipher in OFB mode. */
  159. MBEDTLS_CIPHER_AES_128_XTS, /**< AES 128-bit cipher in XTS block mode. */
  160. MBEDTLS_CIPHER_AES_256_XTS, /**< AES 256-bit cipher in XTS block mode. */
  161. MBEDTLS_CIPHER_CHACHA20, /**< ChaCha20 stream cipher. */
  162. MBEDTLS_CIPHER_CHACHA20_POLY1305, /**< ChaCha20-Poly1305 AEAD cipher. */
  163. } mbedtls_cipher_type_t;
  164. /** Supported cipher modes. */
  165. typedef enum {
  166. MBEDTLS_MODE_NONE = 0, /**< None. */
  167. MBEDTLS_MODE_ECB, /**< The ECB cipher mode. */
  168. MBEDTLS_MODE_CBC, /**< The CBC cipher mode. */
  169. MBEDTLS_MODE_CFB, /**< The CFB cipher mode. */
  170. MBEDTLS_MODE_OFB, /**< The OFB cipher mode. */
  171. MBEDTLS_MODE_CTR, /**< The CTR cipher mode. */
  172. MBEDTLS_MODE_GCM, /**< The GCM cipher mode. */
  173. MBEDTLS_MODE_STREAM, /**< The stream cipher mode. */
  174. MBEDTLS_MODE_CCM, /**< The CCM cipher mode. */
  175. MBEDTLS_MODE_XTS, /**< The XTS cipher mode. */
  176. MBEDTLS_MODE_CHACHAPOLY, /**< The ChaCha-Poly cipher mode. */
  177. } mbedtls_cipher_mode_t;
  178. /** Supported cipher padding types. */
  179. typedef enum {
  180. MBEDTLS_PADDING_PKCS7 = 0, /**< PKCS7 padding (default). */
  181. MBEDTLS_PADDING_ONE_AND_ZEROS, /**< ISO/IEC 7816-4 padding. */
  182. MBEDTLS_PADDING_ZEROS_AND_LEN, /**< ANSI X.923 padding. */
  183. MBEDTLS_PADDING_ZEROS, /**< Zero padding (not reversible). */
  184. MBEDTLS_PADDING_NONE, /**< Never pad (full blocks only). */
  185. } mbedtls_cipher_padding_t;
  186. /** Type of operation. */
  187. typedef enum {
  188. MBEDTLS_OPERATION_NONE = -1,
  189. MBEDTLS_DECRYPT = 0,
  190. MBEDTLS_ENCRYPT,
  191. } mbedtls_operation_t;
  192. enum {
  193. /** Undefined key length. */
  194. MBEDTLS_KEY_LENGTH_NONE = 0,
  195. /** Key length, in bits (including parity), for DES keys. */
  196. MBEDTLS_KEY_LENGTH_DES = 64,
  197. /** Key length in bits, including parity, for DES in two-key EDE. */
  198. MBEDTLS_KEY_LENGTH_DES_EDE = 128,
  199. /** Key length in bits, including parity, for DES in three-key EDE. */
  200. MBEDTLS_KEY_LENGTH_DES_EDE3 = 192,
  201. };
  202. /** Maximum length of any IV, in Bytes. */
  203. #define MBEDTLS_MAX_IV_LENGTH 16
  204. /** Maximum block size of any cipher, in Bytes. */
  205. #define MBEDTLS_MAX_BLOCK_LENGTH 16
  206. /**
  207. * Base cipher information (opaque struct).
  208. */
  209. typedef struct mbedtls_cipher_base_t mbedtls_cipher_base_t;
  210. /**
  211. * CMAC context (opaque struct).
  212. */
  213. typedef struct mbedtls_cmac_context_t mbedtls_cmac_context_t;
  214. /**
  215. * Cipher information. Allows calling cipher functions
  216. * in a generic way.
  217. */
  218. typedef struct {
  219. /** Full cipher identifier. For example,
  220. * MBEDTLS_CIPHER_AES_256_CBC.
  221. */
  222. mbedtls_cipher_type_t type;
  223. /** The cipher mode. For example, MBEDTLS_MODE_CBC. */
  224. mbedtls_cipher_mode_t mode;
  225. /** The cipher key length, in bits. This is the
  226. * default length for variable sized ciphers.
  227. * Includes parity bits for ciphers like DES.
  228. */
  229. unsigned int key_bitlen;
  230. /** Name of the cipher. */
  231. const char * name;
  232. /** IV or nonce size, in Bytes.
  233. * For ciphers that accept variable IV sizes,
  234. * this is the recommended size.
  235. */
  236. unsigned int iv_size;
  237. /** Bitflag comprised of MBEDTLS_CIPHER_VARIABLE_IV_LEN and
  238. * MBEDTLS_CIPHER_VARIABLE_KEY_LEN indicating whether the
  239. * cipher supports variable IV or variable key sizes, respectively.
  240. */
  241. int flags;
  242. /** The block size, in Bytes. */
  243. unsigned int block_size;
  244. /** Struct for base cipher information and functions. */
  245. const mbedtls_cipher_base_t *base;
  246. } mbedtls_cipher_info_t;
  247. /**
  248. * Generic cipher context.
  249. */
  250. typedef struct {
  251. /** Information about the associated cipher. */
  252. const mbedtls_cipher_info_t *cipher_info;
  253. /** Key length to use. */
  254. int key_bitlen;
  255. /** Operation that the key of the context has been
  256. * initialized for.
  257. */
  258. mbedtls_operation_t operation;
  259. #if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
  260. /** Padding functions to use, if relevant for
  261. * the specific cipher mode.
  262. */
  263. void (*add_padding)( unsigned char *output, size_t olen, size_t data_len );
  264. int (*get_padding)( unsigned char *input, size_t ilen, size_t *data_len );
  265. #endif
  266. /** Buffer for input that has not been processed yet. */
  267. unsigned char unprocessed_data[MBEDTLS_MAX_BLOCK_LENGTH];
  268. /** Number of Bytes that have not been processed yet. */
  269. size_t unprocessed_len;
  270. /** Current IV or NONCE_COUNTER for CTR-mode, data unit (or sector) number
  271. * for XTS-mode. */
  272. unsigned char iv[MBEDTLS_MAX_IV_LENGTH];
  273. /** IV size in Bytes, for ciphers with variable-length IVs. */
  274. size_t iv_size;
  275. /** The cipher-specific context. */
  276. void *cipher_ctx;
  277. #if defined(MBEDTLS_CMAC_C)
  278. /** CMAC-specific context. */
  279. mbedtls_cmac_context_t *cmac_ctx;
  280. #endif
  281. } mbedtls_cipher_context_t;
  282. /**
  283. * \brief This function retrieves the list of ciphers supported by the generic
  284. * cipher module.
  285. *
  286. * \return A statically-allocated array of ciphers. The last entry
  287. * is zero.
  288. */
  289. const int *mbedtls_cipher_list( void );
  290. /**
  291. * \brief This function retrieves the cipher-information
  292. * structure associated with the given cipher name.
  293. *
  294. * \param cipher_name Name of the cipher to search for.
  295. *
  296. * \return The cipher information structure associated with the
  297. * given \p cipher_name.
  298. * \return NULL if the associated cipher information is not found.
  299. */
  300. const mbedtls_cipher_info_t *mbedtls_cipher_info_from_string( const char *cipher_name );
  301. /**
  302. * \brief This function retrieves the cipher-information
  303. * structure associated with the given cipher type.
  304. *
  305. * \param cipher_type Type of the cipher to search for.
  306. *
  307. * \return The cipher information structure associated with the
  308. * given \p cipher_type.
  309. * \return NULL if the associated cipher information is not found.
  310. */
  311. const mbedtls_cipher_info_t *mbedtls_cipher_info_from_type( const mbedtls_cipher_type_t cipher_type );
  312. /**
  313. * \brief This function retrieves the cipher-information
  314. * structure associated with the given cipher ID,
  315. * key size and mode.
  316. *
  317. * \param cipher_id The ID of the cipher to search for. For example,
  318. * #MBEDTLS_CIPHER_ID_AES.
  319. * \param key_bitlen The length of the key in bits.
  320. * \param mode The cipher mode. For example, #MBEDTLS_MODE_CBC.
  321. *
  322. * \return The cipher information structure associated with the
  323. * given \p cipher_id.
  324. * \return NULL if the associated cipher information is not found.
  325. */
  326. const mbedtls_cipher_info_t *mbedtls_cipher_info_from_values( const mbedtls_cipher_id_t cipher_id,
  327. int key_bitlen,
  328. const mbedtls_cipher_mode_t mode );
  329. /**
  330. * \brief This function initializes a \p cipher_context as NONE.
  331. */
  332. void mbedtls_cipher_init( mbedtls_cipher_context_t *ctx );
  333. /**
  334. * \brief This function frees and clears the cipher-specific
  335. * context of \p ctx. Freeing \p ctx itself remains the
  336. * responsibility of the caller.
  337. */
  338. void mbedtls_cipher_free( mbedtls_cipher_context_t *ctx );
  339. /**
  340. * \brief This function initializes and fills the cipher-context
  341. * structure with the appropriate values. It also clears
  342. * the structure.
  343. *
  344. * \param ctx The context to initialize. May not be NULL.
  345. * \param cipher_info The cipher to use.
  346. *
  347. * \return \c 0 on success.
  348. * \return #MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA on
  349. * parameter-verification failure.
  350. * \return #MBEDTLS_ERR_CIPHER_ALLOC_FAILED if allocation of the
  351. * cipher-specific context fails.
  352. *
  353. * \internal Currently, the function also clears the structure.
  354. * In future versions, the caller will be required to call
  355. * mbedtls_cipher_init() on the structure first.
  356. */
  357. int mbedtls_cipher_setup( mbedtls_cipher_context_t *ctx, const mbedtls_cipher_info_t *cipher_info );
  358. /**
  359. * \brief This function returns the block size of the given cipher.
  360. *
  361. * \param ctx The context of the cipher. Must be initialized.
  362. *
  363. * \return The size of the blocks of the cipher.
  364. * \return 0 if \p ctx has not been initialized.
  365. */
  366. static inline unsigned int mbedtls_cipher_get_block_size( const mbedtls_cipher_context_t *ctx )
  367. {
  368. if( NULL == ctx || NULL == ctx->cipher_info )
  369. return 0;
  370. return ctx->cipher_info->block_size;
  371. }
  372. /**
  373. * \brief This function returns the mode of operation for
  374. * the cipher. For example, MBEDTLS_MODE_CBC.
  375. *
  376. * \param ctx The context of the cipher. Must be initialized.
  377. *
  378. * \return The mode of operation.
  379. * \return #MBEDTLS_MODE_NONE if \p ctx has not been initialized.
  380. */
  381. static inline mbedtls_cipher_mode_t mbedtls_cipher_get_cipher_mode( const mbedtls_cipher_context_t *ctx )
  382. {
  383. if( NULL == ctx || NULL == ctx->cipher_info )
  384. return MBEDTLS_MODE_NONE;
  385. return ctx->cipher_info->mode;
  386. }
  387. /**
  388. * \brief This function returns the size of the IV or nonce
  389. * of the cipher, in Bytes.
  390. *
  391. * \param ctx The context of the cipher. Must be initialized.
  392. *
  393. * \return The recommended IV size if no IV has been set.
  394. * \return \c 0 for ciphers not using an IV or a nonce.
  395. * \return The actual size if an IV has been set.
  396. */
  397. static inline int mbedtls_cipher_get_iv_size( const mbedtls_cipher_context_t *ctx )
  398. {
  399. if( NULL == ctx || NULL == ctx->cipher_info )
  400. return 0;
  401. if( ctx->iv_size != 0 )
  402. return (int) ctx->iv_size;
  403. return (int) ctx->cipher_info->iv_size;
  404. }
  405. /**
  406. * \brief This function returns the type of the given cipher.
  407. *
  408. * \param ctx The context of the cipher. Must be initialized.
  409. *
  410. * \return The type of the cipher.
  411. * \return #MBEDTLS_CIPHER_NONE if \p ctx has not been initialized.
  412. */
  413. static inline mbedtls_cipher_type_t mbedtls_cipher_get_type( const mbedtls_cipher_context_t *ctx )
  414. {
  415. if( NULL == ctx || NULL == ctx->cipher_info )
  416. return MBEDTLS_CIPHER_NONE;
  417. return ctx->cipher_info->type;
  418. }
  419. /**
  420. * \brief This function returns the name of the given cipher
  421. * as a string.
  422. *
  423. * \param ctx The context of the cipher. Must be initialized.
  424. *
  425. * \return The name of the cipher.
  426. * \return NULL if \p ctx has not been not initialized.
  427. */
  428. static inline const char *mbedtls_cipher_get_name( const mbedtls_cipher_context_t *ctx )
  429. {
  430. if( NULL == ctx || NULL == ctx->cipher_info )
  431. return 0;
  432. return ctx->cipher_info->name;
  433. }
  434. /**
  435. * \brief This function returns the key length of the cipher.
  436. *
  437. * \param ctx The context of the cipher. Must be initialized.
  438. *
  439. * \return The key length of the cipher in bits.
  440. * \return #MBEDTLS_KEY_LENGTH_NONE if ctx \p has not been
  441. * initialized.
  442. */
  443. static inline int mbedtls_cipher_get_key_bitlen( const mbedtls_cipher_context_t *ctx )
  444. {
  445. if( NULL == ctx || NULL == ctx->cipher_info )
  446. return MBEDTLS_KEY_LENGTH_NONE;
  447. return (int) ctx->cipher_info->key_bitlen;
  448. }
  449. /**
  450. * \brief This function returns the operation of the given cipher.
  451. *
  452. * \param ctx The context of the cipher. Must be initialized.
  453. *
  454. * \return The type of operation: #MBEDTLS_ENCRYPT or #MBEDTLS_DECRYPT.
  455. * \return #MBEDTLS_OPERATION_NONE if \p ctx has not been initialized.
  456. */
  457. static inline mbedtls_operation_t mbedtls_cipher_get_operation( const mbedtls_cipher_context_t *ctx )
  458. {
  459. if( NULL == ctx || NULL == ctx->cipher_info )
  460. return MBEDTLS_OPERATION_NONE;
  461. return ctx->operation;
  462. }
  463. /**
  464. * \brief This function sets the key to use with the given context.
  465. *
  466. * \param ctx The generic cipher context. May not be NULL. Must have
  467. * been initialized using mbedtls_cipher_info_from_type()
  468. * or mbedtls_cipher_info_from_string().
  469. * \param key The key to use.
  470. * \param key_bitlen The key length to use, in bits.
  471. * \param operation The operation that the key will be used for:
  472. * #MBEDTLS_ENCRYPT or #MBEDTLS_DECRYPT.
  473. *
  474. * \return \c 0 on success.
  475. * \return #MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA on
  476. * parameter-verification failure.
  477. * \return A cipher-specific error code on failure.
  478. */
  479. int mbedtls_cipher_setkey( mbedtls_cipher_context_t *ctx, const unsigned char *key,
  480. int key_bitlen, const mbedtls_operation_t operation );
  481. #if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
  482. /**
  483. * \brief This function sets the padding mode, for cipher modes
  484. * that use padding.
  485. *
  486. * The default passing mode is PKCS7 padding.
  487. *
  488. * \param ctx The generic cipher context.
  489. * \param mode The padding mode.
  490. *
  491. * \return \c 0 on success.
  492. * \return #MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE
  493. * if the selected padding mode is not supported.
  494. * \return #MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA if the cipher mode
  495. * does not support padding.
  496. */
  497. int mbedtls_cipher_set_padding_mode( mbedtls_cipher_context_t *ctx, mbedtls_cipher_padding_t mode );
  498. #endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
  499. /**
  500. * \brief This function sets the initialization vector (IV)
  501. * or nonce.
  502. *
  503. * \note Some ciphers do not use IVs nor nonce. For these
  504. * ciphers, this function has no effect.
  505. *
  506. * \param ctx The generic cipher context.
  507. * \param iv The IV to use, or NONCE_COUNTER for CTR-mode ciphers.
  508. * \param iv_len The IV length for ciphers with variable-size IV.
  509. * This parameter is discarded by ciphers with fixed-size IV.
  510. *
  511. * \return \c 0 on success.
  512. * \return #MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA on
  513. * parameter-verification failure.
  514. */
  515. int mbedtls_cipher_set_iv( mbedtls_cipher_context_t *ctx,
  516. const unsigned char *iv, size_t iv_len );
  517. /**
  518. * \brief This function resets the cipher state.
  519. *
  520. * \param ctx The generic cipher context.
  521. *
  522. * \return \c 0 on success.
  523. * \return #MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA on
  524. * parameter-verification failure.
  525. */
  526. int mbedtls_cipher_reset( mbedtls_cipher_context_t *ctx );
  527. #if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
  528. /**
  529. * \brief This function adds additional data for AEAD ciphers.
  530. * Currently supported with GCM and ChaCha20+Poly1305.
  531. * Must be called exactly once, after mbedtls_cipher_reset().
  532. *
  533. * \param ctx The generic cipher context.
  534. * \param ad The additional data to use.
  535. * \param ad_len the Length of \p ad.
  536. *
  537. * \return \c 0 on success.
  538. * \return A specific error code on failure.
  539. */
  540. int mbedtls_cipher_update_ad( mbedtls_cipher_context_t *ctx,
  541. const unsigned char *ad, size_t ad_len );
  542. #endif /* MBEDTLS_GCM_C || MBEDTLS_CHACHAPOLY_C */
  543. /**
  544. * \brief The generic cipher update function. It encrypts or
  545. * decrypts using the given cipher context. Writes as
  546. * many block-sized blocks of data as possible to output.
  547. * Any data that cannot be written immediately is either
  548. * added to the next block, or flushed when
  549. * mbedtls_cipher_finish() is called.
  550. * Exception: For MBEDTLS_MODE_ECB, expects a single block
  551. * in size. For example, 16 Bytes for AES.
  552. *
  553. * \note If the underlying cipher is used in GCM mode, all calls
  554. * to this function, except for the last one before
  555. * mbedtls_cipher_finish(), must have \p ilen as a
  556. * multiple of the block size of the cipher.
  557. *
  558. * \param ctx The generic cipher context.
  559. * \param input The buffer holding the input data.
  560. * \param ilen The length of the input data.
  561. * \param output The buffer for the output data. Must be able to hold at
  562. * least \p ilen + block_size. Must not be the same buffer
  563. * as input.
  564. * \param olen The length of the output data, to be updated with the
  565. * actual number of Bytes written.
  566. *
  567. * \return \c 0 on success.
  568. * \return #MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA on
  569. * parameter-verification failure.
  570. * \return #MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE on an
  571. * unsupported mode for a cipher.
  572. * \return A cipher-specific error code on failure.
  573. */
  574. int mbedtls_cipher_update( mbedtls_cipher_context_t *ctx, const unsigned char *input,
  575. size_t ilen, unsigned char *output, size_t *olen );
  576. /**
  577. * \brief The generic cipher finalization function. If data still
  578. * needs to be flushed from an incomplete block, the data
  579. * contained in it is padded to the size of
  580. * the last block, and written to the \p output buffer.
  581. *
  582. * \param ctx The generic cipher context.
  583. * \param output The buffer to write data to. Needs block_size available.
  584. * \param olen The length of the data written to the \p output buffer.
  585. *
  586. * \return \c 0 on success.
  587. * \return #MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA on
  588. * parameter-verification failure.
  589. * \return #MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED on decryption
  590. * expecting a full block but not receiving one.
  591. * \return #MBEDTLS_ERR_CIPHER_INVALID_PADDING on invalid padding
  592. * while decrypting.
  593. * \return A cipher-specific error code on failure.
  594. */
  595. int mbedtls_cipher_finish( mbedtls_cipher_context_t *ctx,
  596. unsigned char *output, size_t *olen );
  597. #if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
  598. /**
  599. * \brief This function writes a tag for AEAD ciphers.
  600. * Currently supported with GCM and ChaCha20+Poly1305.
  601. * Must be called after mbedtls_cipher_finish().
  602. *
  603. * \param ctx The generic cipher context.
  604. * \param tag The buffer to write the tag to.
  605. * \param tag_len The length of the tag to write.
  606. *
  607. * \return \c 0 on success.
  608. * \return A specific error code on failure.
  609. */
  610. int mbedtls_cipher_write_tag( mbedtls_cipher_context_t *ctx,
  611. unsigned char *tag, size_t tag_len );
  612. /**
  613. * \brief This function checks the tag for AEAD ciphers.
  614. * Currently supported with GCM and ChaCha20+Poly1305.
  615. * Must be called after mbedtls_cipher_finish().
  616. *
  617. * \param ctx The generic cipher context.
  618. * \param tag The buffer holding the tag.
  619. * \param tag_len The length of the tag to check.
  620. *
  621. * \return \c 0 on success.
  622. * \return A specific error code on failure.
  623. */
  624. int mbedtls_cipher_check_tag( mbedtls_cipher_context_t *ctx,
  625. const unsigned char *tag, size_t tag_len );
  626. #endif /* MBEDTLS_GCM_C || MBEDTLS_CHACHAPOLY_C */
  627. /**
  628. * \brief The generic all-in-one encryption/decryption function,
  629. * for all ciphers except AEAD constructs.
  630. *
  631. * \param ctx The generic cipher context.
  632. * \param iv The IV to use, or NONCE_COUNTER for CTR-mode ciphers.
  633. * \param iv_len The IV length for ciphers with variable-size IV.
  634. * This parameter is discarded by ciphers with fixed-size
  635. * IV.
  636. * \param input The buffer holding the input data.
  637. * \param ilen The length of the input data.
  638. * \param output The buffer for the output data. Must be able to hold at
  639. * least \p ilen + block_size. Must not be the same buffer
  640. * as input.
  641. * \param olen The length of the output data, to be updated with the
  642. * actual number of Bytes written.
  643. *
  644. * \note Some ciphers do not use IVs nor nonce. For these
  645. * ciphers, use \p iv = NULL and \p iv_len = 0.
  646. *
  647. * \return \c 0 on success.
  648. * \return #MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA on
  649. * parameter-verification failure.
  650. * \return #MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED on decryption
  651. * expecting a full block but not receiving one.
  652. * \return #MBEDTLS_ERR_CIPHER_INVALID_PADDING on invalid padding
  653. * while decrypting.
  654. * \return A cipher-specific error code on failure.
  655. */
  656. int mbedtls_cipher_crypt( mbedtls_cipher_context_t *ctx,
  657. const unsigned char *iv, size_t iv_len,
  658. const unsigned char *input, size_t ilen,
  659. unsigned char *output, size_t *olen );
  660. #if defined(MBEDTLS_CIPHER_MODE_AEAD)
  661. /**
  662. * \brief The generic autenticated encryption (AEAD) function.
  663. *
  664. * \param ctx The generic cipher context.
  665. * \param iv The IV to use, or NONCE_COUNTER for CTR-mode ciphers.
  666. * \param iv_len The IV length for ciphers with variable-size IV.
  667. * This parameter is discarded by ciphers with fixed-size IV.
  668. * \param ad The additional data to authenticate.
  669. * \param ad_len The length of \p ad.
  670. * \param input The buffer holding the input data.
  671. * \param ilen The length of the input data.
  672. * \param output The buffer for the output data.
  673. * Must be able to hold at least \p ilen.
  674. * \param olen The length of the output data, to be updated with the
  675. * actual number of Bytes written.
  676. * \param tag The buffer for the authentication tag.
  677. * \param tag_len The desired length of the authentication tag.
  678. *
  679. * \return \c 0 on success.
  680. * \return #MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA on
  681. * parameter-verification failure.
  682. * \return A cipher-specific error code on failure.
  683. */
  684. int mbedtls_cipher_auth_encrypt( mbedtls_cipher_context_t *ctx,
  685. const unsigned char *iv, size_t iv_len,
  686. const unsigned char *ad, size_t ad_len,
  687. const unsigned char *input, size_t ilen,
  688. unsigned char *output, size_t *olen,
  689. unsigned char *tag, size_t tag_len );
  690. /**
  691. * \brief The generic autenticated decryption (AEAD) function.
  692. *
  693. * \note If the data is not authentic, then the output buffer
  694. * is zeroed out to prevent the unauthentic plaintext being
  695. * used, making this interface safer.
  696. *
  697. * \param ctx The generic cipher context.
  698. * \param iv The IV to use, or NONCE_COUNTER for CTR-mode ciphers.
  699. * \param iv_len The IV length for ciphers with variable-size IV.
  700. * This parameter is discarded by ciphers with fixed-size IV.
  701. * \param ad The additional data to be authenticated.
  702. * \param ad_len The length of \p ad.
  703. * \param input The buffer holding the input data.
  704. * \param ilen The length of the input data.
  705. * \param output The buffer for the output data.
  706. * Must be able to hold at least \p ilen.
  707. * \param olen The length of the output data, to be updated with the
  708. * actual number of Bytes written.
  709. * \param tag The buffer holding the authentication tag.
  710. * \param tag_len The length of the authentication tag.
  711. *
  712. * \return \c 0 on success.
  713. * \return #MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA on
  714. * parameter-verification failure.
  715. * \return #MBEDTLS_ERR_CIPHER_AUTH_FAILED if data is not authentic.
  716. * \return A cipher-specific error code on failure.
  717. */
  718. int mbedtls_cipher_auth_decrypt( mbedtls_cipher_context_t *ctx,
  719. const unsigned char *iv, size_t iv_len,
  720. const unsigned char *ad, size_t ad_len,
  721. const unsigned char *input, size_t ilen,
  722. unsigned char *output, size_t *olen,
  723. const unsigned char *tag, size_t tag_len );
  724. #endif /* MBEDTLS_CIPHER_MODE_AEAD */
  725. #ifdef __cplusplus
  726. }
  727. #endif
  728. #endif /* MBEDTLS_CIPHER_H */