cipher.h 39 KB

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