cipher.h 57 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250
  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 <dejong@fox-it.com>
  9. */
  10. /*
  11. * Copyright The Mbed TLS Contributors
  12. * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
  13. */
  14. #ifndef MBEDTLS_CIPHER_H
  15. #define MBEDTLS_CIPHER_H
  16. #include "mbedtls/private_access.h"
  17. #include "mbedtls/build_info.h"
  18. #include <stddef.h>
  19. #include "mbedtls/platform_util.h"
  20. #if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
  21. #define MBEDTLS_CIPHER_MODE_AEAD
  22. #endif
  23. #if defined(MBEDTLS_CIPHER_MODE_CBC)
  24. #define MBEDTLS_CIPHER_MODE_WITH_PADDING
  25. #endif
  26. #if defined(MBEDTLS_CIPHER_NULL_CIPHER) || \
  27. defined(MBEDTLS_CHACHA20_C)
  28. #define MBEDTLS_CIPHER_MODE_STREAM
  29. #endif
  30. /** The selected feature is not available. */
  31. #define MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE -0x6080
  32. /** Bad input parameters. */
  33. #define MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA -0x6100
  34. /** Failed to allocate memory. */
  35. #define MBEDTLS_ERR_CIPHER_ALLOC_FAILED -0x6180
  36. /** Input data contains invalid padding and is rejected. */
  37. #define MBEDTLS_ERR_CIPHER_INVALID_PADDING -0x6200
  38. /** Decryption of block requires a full block. */
  39. #define MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED -0x6280
  40. /** Authentication failed (for AEAD modes). */
  41. #define MBEDTLS_ERR_CIPHER_AUTH_FAILED -0x6300
  42. /** The context is invalid. For example, because it was freed. */
  43. #define MBEDTLS_ERR_CIPHER_INVALID_CONTEXT -0x6380
  44. #define MBEDTLS_CIPHER_VARIABLE_IV_LEN 0x01 /**< Cipher accepts IVs of variable length. */
  45. #define MBEDTLS_CIPHER_VARIABLE_KEY_LEN 0x02 /**< Cipher accepts keys of variable length. */
  46. #ifdef __cplusplus
  47. extern "C" {
  48. #endif
  49. /**
  50. * \brief Supported cipher types.
  51. *
  52. * \warning DES/3DES are considered weak ciphers and their use
  53. * constitutes a security risk. We recommend considering stronger
  54. * ciphers instead.
  55. */
  56. typedef enum {
  57. MBEDTLS_CIPHER_ID_NONE = 0, /**< Placeholder to mark the end of cipher ID lists. */
  58. MBEDTLS_CIPHER_ID_NULL, /**< The identity cipher, treated as a stream cipher. */
  59. MBEDTLS_CIPHER_ID_AES, /**< The AES cipher. */
  60. MBEDTLS_CIPHER_ID_DES, /**< The DES cipher. \warning DES is considered weak. */
  61. MBEDTLS_CIPHER_ID_3DES, /**< The Triple DES cipher. \warning 3DES is considered weak. */
  62. MBEDTLS_CIPHER_ID_CAMELLIA, /**< The Camellia cipher. */
  63. MBEDTLS_CIPHER_ID_ARIA, /**< The Aria cipher. */
  64. MBEDTLS_CIPHER_ID_CHACHA20, /**< The ChaCha20 cipher. */
  65. } mbedtls_cipher_id_t;
  66. /**
  67. * \brief Supported {cipher type, cipher mode} pairs.
  68. *
  69. * \warning DES/3DES are considered weak ciphers and their use
  70. * constitutes a security risk. We recommend considering stronger
  71. * ciphers instead.
  72. */
  73. typedef enum {
  74. MBEDTLS_CIPHER_NONE = 0, /**< Placeholder to mark the end of cipher-pair lists. */
  75. MBEDTLS_CIPHER_NULL, /**< The identity stream cipher. */
  76. MBEDTLS_CIPHER_AES_128_ECB, /**< AES cipher with 128-bit ECB mode. */
  77. MBEDTLS_CIPHER_AES_192_ECB, /**< AES cipher with 192-bit ECB mode. */
  78. MBEDTLS_CIPHER_AES_256_ECB, /**< AES cipher with 256-bit ECB mode. */
  79. MBEDTLS_CIPHER_AES_128_CBC, /**< AES cipher with 128-bit CBC mode. */
  80. MBEDTLS_CIPHER_AES_192_CBC, /**< AES cipher with 192-bit CBC mode. */
  81. MBEDTLS_CIPHER_AES_256_CBC, /**< AES cipher with 256-bit CBC mode. */
  82. MBEDTLS_CIPHER_AES_128_CFB128, /**< AES cipher with 128-bit CFB128 mode. */
  83. MBEDTLS_CIPHER_AES_192_CFB128, /**< AES cipher with 192-bit CFB128 mode. */
  84. MBEDTLS_CIPHER_AES_256_CFB128, /**< AES cipher with 256-bit CFB128 mode. */
  85. MBEDTLS_CIPHER_AES_128_CTR, /**< AES cipher with 128-bit CTR mode. */
  86. MBEDTLS_CIPHER_AES_192_CTR, /**< AES cipher with 192-bit CTR mode. */
  87. MBEDTLS_CIPHER_AES_256_CTR, /**< AES cipher with 256-bit CTR mode. */
  88. MBEDTLS_CIPHER_AES_128_GCM, /**< AES cipher with 128-bit GCM mode. */
  89. MBEDTLS_CIPHER_AES_192_GCM, /**< AES cipher with 192-bit GCM mode. */
  90. MBEDTLS_CIPHER_AES_256_GCM, /**< AES cipher with 256-bit GCM mode. */
  91. MBEDTLS_CIPHER_CAMELLIA_128_ECB, /**< Camellia cipher with 128-bit ECB mode. */
  92. MBEDTLS_CIPHER_CAMELLIA_192_ECB, /**< Camellia cipher with 192-bit ECB mode. */
  93. MBEDTLS_CIPHER_CAMELLIA_256_ECB, /**< Camellia cipher with 256-bit ECB mode. */
  94. MBEDTLS_CIPHER_CAMELLIA_128_CBC, /**< Camellia cipher with 128-bit CBC mode. */
  95. MBEDTLS_CIPHER_CAMELLIA_192_CBC, /**< Camellia cipher with 192-bit CBC mode. */
  96. MBEDTLS_CIPHER_CAMELLIA_256_CBC, /**< Camellia cipher with 256-bit CBC mode. */
  97. MBEDTLS_CIPHER_CAMELLIA_128_CFB128, /**< Camellia cipher with 128-bit CFB128 mode. */
  98. MBEDTLS_CIPHER_CAMELLIA_192_CFB128, /**< Camellia cipher with 192-bit CFB128 mode. */
  99. MBEDTLS_CIPHER_CAMELLIA_256_CFB128, /**< Camellia cipher with 256-bit CFB128 mode. */
  100. MBEDTLS_CIPHER_CAMELLIA_128_CTR, /**< Camellia cipher with 128-bit CTR mode. */
  101. MBEDTLS_CIPHER_CAMELLIA_192_CTR, /**< Camellia cipher with 192-bit CTR mode. */
  102. MBEDTLS_CIPHER_CAMELLIA_256_CTR, /**< Camellia cipher with 256-bit CTR mode. */
  103. MBEDTLS_CIPHER_CAMELLIA_128_GCM, /**< Camellia cipher with 128-bit GCM mode. */
  104. MBEDTLS_CIPHER_CAMELLIA_192_GCM, /**< Camellia cipher with 192-bit GCM mode. */
  105. MBEDTLS_CIPHER_CAMELLIA_256_GCM, /**< Camellia cipher with 256-bit GCM mode. */
  106. MBEDTLS_CIPHER_DES_ECB, /**< DES cipher with ECB mode. \warning DES is considered weak. */
  107. MBEDTLS_CIPHER_DES_CBC, /**< DES cipher with CBC mode. \warning DES is considered weak. */
  108. MBEDTLS_CIPHER_DES_EDE_ECB, /**< DES cipher with EDE ECB mode. \warning 3DES is considered weak. */
  109. MBEDTLS_CIPHER_DES_EDE_CBC, /**< DES cipher with EDE CBC mode. \warning 3DES is considered weak. */
  110. MBEDTLS_CIPHER_DES_EDE3_ECB, /**< DES cipher with EDE3 ECB mode. \warning 3DES is considered weak. */
  111. MBEDTLS_CIPHER_DES_EDE3_CBC, /**< DES cipher with EDE3 CBC mode. \warning 3DES is considered weak. */
  112. MBEDTLS_CIPHER_AES_128_CCM, /**< AES cipher with 128-bit CCM mode. */
  113. MBEDTLS_CIPHER_AES_192_CCM, /**< AES cipher with 192-bit CCM mode. */
  114. MBEDTLS_CIPHER_AES_256_CCM, /**< AES cipher with 256-bit CCM mode. */
  115. MBEDTLS_CIPHER_AES_128_CCM_STAR_NO_TAG, /**< AES cipher with 128-bit CCM_STAR_NO_TAG mode. */
  116. MBEDTLS_CIPHER_AES_192_CCM_STAR_NO_TAG, /**< AES cipher with 192-bit CCM_STAR_NO_TAG mode. */
  117. MBEDTLS_CIPHER_AES_256_CCM_STAR_NO_TAG, /**< AES cipher with 256-bit CCM_STAR_NO_TAG mode. */
  118. MBEDTLS_CIPHER_CAMELLIA_128_CCM, /**< Camellia cipher with 128-bit CCM mode. */
  119. MBEDTLS_CIPHER_CAMELLIA_192_CCM, /**< Camellia cipher with 192-bit CCM mode. */
  120. MBEDTLS_CIPHER_CAMELLIA_256_CCM, /**< Camellia cipher with 256-bit CCM mode. */
  121. MBEDTLS_CIPHER_CAMELLIA_128_CCM_STAR_NO_TAG, /**< Camellia cipher with 128-bit CCM_STAR_NO_TAG mode. */
  122. MBEDTLS_CIPHER_CAMELLIA_192_CCM_STAR_NO_TAG, /**< Camellia cipher with 192-bit CCM_STAR_NO_TAG mode. */
  123. MBEDTLS_CIPHER_CAMELLIA_256_CCM_STAR_NO_TAG, /**< Camellia cipher with 256-bit CCM_STAR_NO_TAG mode. */
  124. MBEDTLS_CIPHER_ARIA_128_ECB, /**< Aria cipher with 128-bit key and ECB mode. */
  125. MBEDTLS_CIPHER_ARIA_192_ECB, /**< Aria cipher with 192-bit key and ECB mode. */
  126. MBEDTLS_CIPHER_ARIA_256_ECB, /**< Aria cipher with 256-bit key and ECB mode. */
  127. MBEDTLS_CIPHER_ARIA_128_CBC, /**< Aria cipher with 128-bit key and CBC mode. */
  128. MBEDTLS_CIPHER_ARIA_192_CBC, /**< Aria cipher with 192-bit key and CBC mode. */
  129. MBEDTLS_CIPHER_ARIA_256_CBC, /**< Aria cipher with 256-bit key and CBC mode. */
  130. MBEDTLS_CIPHER_ARIA_128_CFB128, /**< Aria cipher with 128-bit key and CFB-128 mode. */
  131. MBEDTLS_CIPHER_ARIA_192_CFB128, /**< Aria cipher with 192-bit key and CFB-128 mode. */
  132. MBEDTLS_CIPHER_ARIA_256_CFB128, /**< Aria cipher with 256-bit key and CFB-128 mode. */
  133. MBEDTLS_CIPHER_ARIA_128_CTR, /**< Aria cipher with 128-bit key and CTR mode. */
  134. MBEDTLS_CIPHER_ARIA_192_CTR, /**< Aria cipher with 192-bit key and CTR mode. */
  135. MBEDTLS_CIPHER_ARIA_256_CTR, /**< Aria cipher with 256-bit key and CTR mode. */
  136. MBEDTLS_CIPHER_ARIA_128_GCM, /**< Aria cipher with 128-bit key and GCM mode. */
  137. MBEDTLS_CIPHER_ARIA_192_GCM, /**< Aria cipher with 192-bit key and GCM mode. */
  138. MBEDTLS_CIPHER_ARIA_256_GCM, /**< Aria cipher with 256-bit key and GCM mode. */
  139. MBEDTLS_CIPHER_ARIA_128_CCM, /**< Aria cipher with 128-bit key and CCM mode. */
  140. MBEDTLS_CIPHER_ARIA_192_CCM, /**< Aria cipher with 192-bit key and CCM mode. */
  141. MBEDTLS_CIPHER_ARIA_256_CCM, /**< Aria cipher with 256-bit key and CCM mode. */
  142. MBEDTLS_CIPHER_ARIA_128_CCM_STAR_NO_TAG, /**< Aria cipher with 128-bit key and CCM_STAR_NO_TAG mode. */
  143. MBEDTLS_CIPHER_ARIA_192_CCM_STAR_NO_TAG, /**< Aria cipher with 192-bit key and CCM_STAR_NO_TAG mode. */
  144. MBEDTLS_CIPHER_ARIA_256_CCM_STAR_NO_TAG, /**< Aria cipher with 256-bit key and CCM_STAR_NO_TAG mode. */
  145. MBEDTLS_CIPHER_AES_128_OFB, /**< AES 128-bit cipher in OFB mode. */
  146. MBEDTLS_CIPHER_AES_192_OFB, /**< AES 192-bit cipher in OFB mode. */
  147. MBEDTLS_CIPHER_AES_256_OFB, /**< AES 256-bit cipher in OFB mode. */
  148. MBEDTLS_CIPHER_AES_128_XTS, /**< AES 128-bit cipher in XTS block mode. */
  149. MBEDTLS_CIPHER_AES_256_XTS, /**< AES 256-bit cipher in XTS block mode. */
  150. MBEDTLS_CIPHER_CHACHA20, /**< ChaCha20 stream cipher. */
  151. MBEDTLS_CIPHER_CHACHA20_POLY1305, /**< ChaCha20-Poly1305 AEAD cipher. */
  152. MBEDTLS_CIPHER_AES_128_KW, /**< AES cipher with 128-bit NIST KW mode. */
  153. MBEDTLS_CIPHER_AES_192_KW, /**< AES cipher with 192-bit NIST KW mode. */
  154. MBEDTLS_CIPHER_AES_256_KW, /**< AES cipher with 256-bit NIST KW mode. */
  155. MBEDTLS_CIPHER_AES_128_KWP, /**< AES cipher with 128-bit NIST KWP mode. */
  156. MBEDTLS_CIPHER_AES_192_KWP, /**< AES cipher with 192-bit NIST KWP mode. */
  157. MBEDTLS_CIPHER_AES_256_KWP, /**< AES cipher with 256-bit NIST KWP mode. */
  158. } mbedtls_cipher_type_t;
  159. /** Supported cipher modes. */
  160. typedef enum {
  161. MBEDTLS_MODE_NONE = 0, /**< None. */
  162. MBEDTLS_MODE_ECB, /**< The ECB cipher mode. */
  163. MBEDTLS_MODE_CBC, /**< The CBC cipher mode. */
  164. MBEDTLS_MODE_CFB, /**< The CFB cipher mode. */
  165. MBEDTLS_MODE_OFB, /**< The OFB cipher mode. */
  166. MBEDTLS_MODE_CTR, /**< The CTR cipher mode. */
  167. MBEDTLS_MODE_GCM, /**< The GCM cipher mode. */
  168. MBEDTLS_MODE_STREAM, /**< The stream cipher mode. */
  169. MBEDTLS_MODE_CCM, /**< The CCM cipher mode. */
  170. MBEDTLS_MODE_CCM_STAR_NO_TAG, /**< The CCM*-no-tag cipher mode. */
  171. MBEDTLS_MODE_XTS, /**< The XTS cipher mode. */
  172. MBEDTLS_MODE_CHACHAPOLY, /**< The ChaCha-Poly cipher mode. */
  173. MBEDTLS_MODE_KW, /**< The SP800-38F KW mode */
  174. MBEDTLS_MODE_KWP, /**< The SP800-38F KWP mode */
  175. } mbedtls_cipher_mode_t;
  176. /** Supported cipher padding types. */
  177. typedef enum {
  178. MBEDTLS_PADDING_PKCS7 = 0, /**< PKCS7 padding (default). */
  179. MBEDTLS_PADDING_ONE_AND_ZEROS, /**< ISO/IEC 7816-4 padding. */
  180. MBEDTLS_PADDING_ZEROS_AND_LEN, /**< ANSI X.923 padding. */
  181. MBEDTLS_PADDING_ZEROS, /**< Zero padding (not reversible). */
  182. MBEDTLS_PADDING_NONE, /**< Never pad (full blocks only). */
  183. } mbedtls_cipher_padding_t;
  184. /** Type of operation. */
  185. typedef enum {
  186. MBEDTLS_OPERATION_NONE = -1,
  187. MBEDTLS_DECRYPT = 0,
  188. MBEDTLS_ENCRYPT,
  189. } mbedtls_operation_t;
  190. enum {
  191. /** Undefined key length. */
  192. MBEDTLS_KEY_LENGTH_NONE = 0,
  193. /** Key length, in bits (including parity), for DES keys. \warning DES is considered weak. */
  194. MBEDTLS_KEY_LENGTH_DES = 64,
  195. /** Key length in bits, including parity, for DES in two-key EDE. \warning 3DES is considered weak. */
  196. MBEDTLS_KEY_LENGTH_DES_EDE = 128,
  197. /** Key length in bits, including parity, for DES in three-key EDE. \warning 3DES is considered weak. */
  198. MBEDTLS_KEY_LENGTH_DES_EDE3 = 192,
  199. };
  200. /** Maximum length of any IV, in Bytes. */
  201. /* This should ideally be derived automatically from list of ciphers.
  202. * This should be kept in sync with MBEDTLS_SSL_MAX_IV_LENGTH defined
  203. * in library/ssl_misc.h. */
  204. #define MBEDTLS_MAX_IV_LENGTH 16
  205. /** Maximum block size of any cipher, in Bytes. */
  206. /* This should ideally be derived automatically from list of ciphers.
  207. * This should be kept in sync with MBEDTLS_SSL_MAX_BLOCK_LENGTH defined
  208. * in library/ssl_misc.h. */
  209. #define MBEDTLS_MAX_BLOCK_LENGTH 16
  210. /** Maximum key length, in Bytes. */
  211. /* This should ideally be derived automatically from list of ciphers.
  212. * For now, only check whether XTS is enabled which uses 64 Byte keys,
  213. * and use 32 Bytes as an upper bound for the maximum key length otherwise.
  214. * This should be kept in sync with MBEDTLS_SSL_MAX_BLOCK_LENGTH defined
  215. * in library/ssl_misc.h, which however deliberately ignores the case of XTS
  216. * since the latter isn't used in SSL/TLS. */
  217. #if defined(MBEDTLS_CIPHER_MODE_XTS)
  218. #define MBEDTLS_MAX_KEY_LENGTH 64
  219. #else
  220. #define MBEDTLS_MAX_KEY_LENGTH 32
  221. #endif /* MBEDTLS_CIPHER_MODE_XTS */
  222. /**
  223. * Base cipher information (opaque struct).
  224. */
  225. typedef struct mbedtls_cipher_base_t mbedtls_cipher_base_t;
  226. /**
  227. * CMAC context (opaque struct).
  228. */
  229. typedef struct mbedtls_cmac_context_t mbedtls_cmac_context_t;
  230. /**
  231. * Cipher information. Allows calling cipher functions
  232. * in a generic way.
  233. *
  234. * \note The library does not support custom cipher info structures,
  235. * only built-in structures returned by the functions
  236. * mbedtls_cipher_info_from_string(),
  237. * mbedtls_cipher_info_from_type(),
  238. * mbedtls_cipher_info_from_values(),
  239. * mbedtls_cipher_info_from_psa().
  240. *
  241. * \note Some fields store a value that has been right-shifted to save
  242. * code-size, so should not be used directly. The accessor
  243. * functions adjust for this and return the "natural" value.
  244. */
  245. typedef struct mbedtls_cipher_info_t {
  246. /** Name of the cipher. */
  247. const char *MBEDTLS_PRIVATE(name);
  248. /** The block size, in bytes. */
  249. unsigned int MBEDTLS_PRIVATE(block_size) : 5;
  250. /** IV or nonce size, in bytes (right shifted by #MBEDTLS_IV_SIZE_SHIFT).
  251. * For ciphers that accept variable IV sizes,
  252. * this is the recommended size.
  253. */
  254. unsigned int MBEDTLS_PRIVATE(iv_size) : 3;
  255. /** The cipher key length, in bits (right shifted by #MBEDTLS_KEY_BITLEN_SHIFT).
  256. * This is the default length for variable sized ciphers.
  257. * Includes parity bits for ciphers like DES.
  258. */
  259. unsigned int MBEDTLS_PRIVATE(key_bitlen) : 4;
  260. /** The cipher mode (as per mbedtls_cipher_mode_t).
  261. * For example, MBEDTLS_MODE_CBC.
  262. */
  263. unsigned int MBEDTLS_PRIVATE(mode) : 4;
  264. /** Full cipher identifier (as per mbedtls_cipher_type_t).
  265. * For example, MBEDTLS_CIPHER_AES_256_CBC.
  266. *
  267. * This could be 7 bits, but 8 bits retains byte alignment for the
  268. * next field, which reduces code size to access that field.
  269. */
  270. unsigned int MBEDTLS_PRIVATE(type) : 8;
  271. /** Bitflag comprised of MBEDTLS_CIPHER_VARIABLE_IV_LEN and
  272. * MBEDTLS_CIPHER_VARIABLE_KEY_LEN indicating whether the
  273. * cipher supports variable IV or variable key sizes, respectively.
  274. */
  275. unsigned int MBEDTLS_PRIVATE(flags) : 2;
  276. /** Index to LUT for base cipher information and functions. */
  277. unsigned int MBEDTLS_PRIVATE(base_idx) : 5;
  278. } mbedtls_cipher_info_t;
  279. /* For internal use only.
  280. * These are used to more compactly represent the fields above. */
  281. #define MBEDTLS_KEY_BITLEN_SHIFT 6
  282. #define MBEDTLS_IV_SIZE_SHIFT 2
  283. /**
  284. * Generic cipher context.
  285. */
  286. typedef struct mbedtls_cipher_context_t {
  287. /** Information about the associated cipher. */
  288. const mbedtls_cipher_info_t *MBEDTLS_PRIVATE(cipher_info);
  289. /** Key length to use. */
  290. int MBEDTLS_PRIVATE(key_bitlen);
  291. /** Operation that the key of the context has been
  292. * initialized for.
  293. */
  294. mbedtls_operation_t MBEDTLS_PRIVATE(operation);
  295. #if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
  296. /** Padding functions to use, if relevant for
  297. * the specific cipher mode.
  298. */
  299. void(*MBEDTLS_PRIVATE(add_padding))(unsigned char *output, size_t olen,
  300. size_t data_len);
  301. /* Report invalid-padding condition through the output parameter
  302. * invalid_padding. To minimize changes in Mbed TLS 3.6, where this
  303. * declaration is in a public header, use the public type size_t
  304. * rather than the internal type mbedtls_ct_condition_t. */
  305. int(*MBEDTLS_PRIVATE(get_padding))(unsigned char *input, size_t ilen,
  306. size_t *data_len,
  307. size_t *invalid_padding);
  308. #endif
  309. /** Buffer for input that has not been processed yet. */
  310. unsigned char MBEDTLS_PRIVATE(unprocessed_data)[MBEDTLS_MAX_BLOCK_LENGTH];
  311. /** Number of Bytes that have not been processed yet. */
  312. size_t MBEDTLS_PRIVATE(unprocessed_len);
  313. /** Current IV or NONCE_COUNTER for CTR-mode, data unit (or sector) number
  314. * for XTS-mode. */
  315. unsigned char MBEDTLS_PRIVATE(iv)[MBEDTLS_MAX_IV_LENGTH];
  316. /** IV size in Bytes, for ciphers with variable-length IVs. */
  317. size_t MBEDTLS_PRIVATE(iv_size);
  318. /** The cipher-specific context. */
  319. void *MBEDTLS_PRIVATE(cipher_ctx);
  320. #if defined(MBEDTLS_CMAC_C)
  321. /** CMAC-specific context. */
  322. mbedtls_cmac_context_t *MBEDTLS_PRIVATE(cmac_ctx);
  323. #endif
  324. #if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED)
  325. /** Indicates whether the cipher operations should be performed
  326. * by Mbed TLS' own crypto library or an external implementation
  327. * of the PSA Crypto API.
  328. * This is unset if the cipher context was established through
  329. * mbedtls_cipher_setup(), and set if it was established through
  330. * mbedtls_cipher_setup_psa().
  331. */
  332. unsigned char MBEDTLS_PRIVATE(psa_enabled);
  333. #endif /* MBEDTLS_USE_PSA_CRYPTO && !MBEDTLS_DEPRECATED_REMOVED */
  334. } mbedtls_cipher_context_t;
  335. /**
  336. * \brief This function retrieves the list of ciphers supported
  337. * by the generic cipher module.
  338. *
  339. * For any cipher identifier in the returned list, you can
  340. * obtain the corresponding generic cipher information structure
  341. * via mbedtls_cipher_info_from_type(), which can then be used
  342. * to prepare a cipher context via mbedtls_cipher_setup().
  343. *
  344. *
  345. * \return A statically-allocated array of cipher identifiers
  346. * of type cipher_type_t. The last entry is zero.
  347. */
  348. const int *mbedtls_cipher_list(void);
  349. /**
  350. * \brief This function retrieves the cipher-information
  351. * structure associated with the given cipher name.
  352. *
  353. * \param cipher_name Name of the cipher to search for. This must not be
  354. * \c NULL.
  355. *
  356. * \return The cipher information structure associated with the
  357. * given \p cipher_name.
  358. * \return \c NULL if the associated cipher information is not found.
  359. */
  360. const mbedtls_cipher_info_t *mbedtls_cipher_info_from_string(const char *cipher_name);
  361. /**
  362. * \brief This function retrieves the cipher-information
  363. * structure associated with the given cipher type.
  364. *
  365. * \param cipher_type Type of the cipher to search for.
  366. *
  367. * \return The cipher information structure associated with the
  368. * given \p cipher_type.
  369. * \return \c NULL if the associated cipher information is not found.
  370. */
  371. const mbedtls_cipher_info_t *mbedtls_cipher_info_from_type(const mbedtls_cipher_type_t cipher_type);
  372. /**
  373. * \brief This function retrieves the cipher-information
  374. * structure associated with the given cipher ID,
  375. * key size and mode.
  376. *
  377. * \param cipher_id The ID of the cipher to search for. For example,
  378. * #MBEDTLS_CIPHER_ID_AES.
  379. * \param key_bitlen The length of the key in bits.
  380. * \param mode The cipher mode. For example, #MBEDTLS_MODE_CBC.
  381. *
  382. * \return The cipher information structure associated with the
  383. * given \p cipher_id.
  384. * \return \c NULL if the associated cipher information is not found.
  385. */
  386. const mbedtls_cipher_info_t *mbedtls_cipher_info_from_values(const mbedtls_cipher_id_t cipher_id,
  387. int key_bitlen,
  388. const mbedtls_cipher_mode_t mode);
  389. /**
  390. * \brief Retrieve the identifier for a cipher info structure.
  391. *
  392. * \param[in] info The cipher info structure to query.
  393. * This may be \c NULL.
  394. *
  395. * \return The full cipher identifier (\c MBEDTLS_CIPHER_xxx).
  396. * \return #MBEDTLS_CIPHER_NONE if \p info is \c NULL.
  397. */
  398. static inline mbedtls_cipher_type_t mbedtls_cipher_info_get_type(
  399. const mbedtls_cipher_info_t *info)
  400. {
  401. if (info == NULL) {
  402. return MBEDTLS_CIPHER_NONE;
  403. } else {
  404. return (mbedtls_cipher_type_t) info->MBEDTLS_PRIVATE(type);
  405. }
  406. }
  407. /**
  408. * \brief Retrieve the operation mode for a cipher info structure.
  409. *
  410. * \param[in] info The cipher info structure to query.
  411. * This may be \c NULL.
  412. *
  413. * \return The cipher mode (\c MBEDTLS_MODE_xxx).
  414. * \return #MBEDTLS_MODE_NONE if \p info is \c NULL.
  415. */
  416. static inline mbedtls_cipher_mode_t mbedtls_cipher_info_get_mode(
  417. const mbedtls_cipher_info_t *info)
  418. {
  419. if (info == NULL) {
  420. return MBEDTLS_MODE_NONE;
  421. } else {
  422. return (mbedtls_cipher_mode_t) info->MBEDTLS_PRIVATE(mode);
  423. }
  424. }
  425. /**
  426. * \brief Retrieve the key size for a cipher info structure.
  427. *
  428. * \param[in] info The cipher info structure to query.
  429. * This may be \c NULL.
  430. *
  431. * \return The key length in bits.
  432. * For variable-sized ciphers, this is the default length.
  433. * For DES, this includes the parity bits.
  434. * \return \c 0 if \p info is \c NULL.
  435. */
  436. static inline size_t mbedtls_cipher_info_get_key_bitlen(
  437. const mbedtls_cipher_info_t *info)
  438. {
  439. if (info == NULL) {
  440. return 0;
  441. } else {
  442. return ((size_t) info->MBEDTLS_PRIVATE(key_bitlen)) << MBEDTLS_KEY_BITLEN_SHIFT;
  443. }
  444. }
  445. /**
  446. * \brief Retrieve the human-readable name for a
  447. * cipher info structure.
  448. *
  449. * \param[in] info The cipher info structure to query.
  450. * This may be \c NULL.
  451. *
  452. * \return The cipher name, which is a human readable string,
  453. * with static storage duration.
  454. * \return \c NULL if \p info is \c NULL.
  455. */
  456. static inline const char *mbedtls_cipher_info_get_name(
  457. const mbedtls_cipher_info_t *info)
  458. {
  459. if (info == NULL) {
  460. return NULL;
  461. } else {
  462. return info->MBEDTLS_PRIVATE(name);
  463. }
  464. }
  465. /**
  466. * \brief This function returns the size of the IV or nonce
  467. * for the cipher info structure, in bytes.
  468. *
  469. * \param info The cipher info structure. This may be \c NULL.
  470. *
  471. * \return The recommended IV size.
  472. * \return \c 0 for ciphers not using an IV or a nonce.
  473. * \return \c 0 if \p info is \c NULL.
  474. */
  475. static inline size_t mbedtls_cipher_info_get_iv_size(
  476. const mbedtls_cipher_info_t *info)
  477. {
  478. if (info == NULL) {
  479. return 0;
  480. }
  481. return ((size_t) info->MBEDTLS_PRIVATE(iv_size)) << MBEDTLS_IV_SIZE_SHIFT;
  482. }
  483. /**
  484. * \brief This function returns the block size of the given
  485. * cipher info structure in bytes.
  486. *
  487. * \param info The cipher info structure. This may be \c NULL.
  488. *
  489. * \return The block size of the cipher.
  490. * \return \c 1 if the cipher is a stream cipher.
  491. * \return \c 0 if \p info is \c NULL.
  492. */
  493. static inline size_t mbedtls_cipher_info_get_block_size(
  494. const mbedtls_cipher_info_t *info)
  495. {
  496. if (info == NULL) {
  497. return 0;
  498. }
  499. return (size_t) (info->MBEDTLS_PRIVATE(block_size));
  500. }
  501. /**
  502. * \brief This function returns a non-zero value if the key length for
  503. * the given cipher is variable.
  504. *
  505. * \param info The cipher info structure. This may be \c NULL.
  506. *
  507. * \return Non-zero if the key length is variable, \c 0 otherwise.
  508. * \return \c 0 if the given pointer is \c NULL.
  509. */
  510. static inline int mbedtls_cipher_info_has_variable_key_bitlen(
  511. const mbedtls_cipher_info_t *info)
  512. {
  513. if (info == NULL) {
  514. return 0;
  515. }
  516. return info->MBEDTLS_PRIVATE(flags) & MBEDTLS_CIPHER_VARIABLE_KEY_LEN;
  517. }
  518. /**
  519. * \brief This function returns a non-zero value if the IV size for
  520. * the given cipher is variable.
  521. *
  522. * \param info The cipher info structure. This may be \c NULL.
  523. *
  524. * \return Non-zero if the IV size is variable, \c 0 otherwise.
  525. * \return \c 0 if the given pointer is \c NULL.
  526. */
  527. static inline int mbedtls_cipher_info_has_variable_iv_size(
  528. const mbedtls_cipher_info_t *info)
  529. {
  530. if (info == NULL) {
  531. return 0;
  532. }
  533. return info->MBEDTLS_PRIVATE(flags) & MBEDTLS_CIPHER_VARIABLE_IV_LEN;
  534. }
  535. /**
  536. * \brief This function initializes a \p ctx as NONE.
  537. *
  538. * \param ctx The context to be initialized. This must not be \c NULL.
  539. */
  540. void mbedtls_cipher_init(mbedtls_cipher_context_t *ctx);
  541. /**
  542. * \brief This function frees and clears the cipher-specific
  543. * context of \p ctx. Freeing \p ctx itself remains the
  544. * responsibility of the caller.
  545. *
  546. * \param ctx The context to be freed. If this is \c NULL, the
  547. * function has no effect, otherwise this must point to an
  548. * initialized context.
  549. */
  550. void mbedtls_cipher_free(mbedtls_cipher_context_t *ctx);
  551. /**
  552. * \brief This function prepares a cipher context for
  553. * use with the given cipher primitive.
  554. *
  555. * \note After calling this function, you should call
  556. * mbedtls_cipher_setkey() and, if the mode uses padding,
  557. * mbedtls_cipher_set_padding_mode(), then for each
  558. * message to encrypt or decrypt with this key, either:
  559. * - mbedtls_cipher_crypt() for one-shot processing with
  560. * non-AEAD modes;
  561. * - mbedtls_cipher_auth_encrypt_ext() or
  562. * mbedtls_cipher_auth_decrypt_ext() for one-shot
  563. * processing with AEAD modes or NIST_KW;
  564. * - for multi-part processing, see the documentation of
  565. * mbedtls_cipher_reset().
  566. *
  567. * \param ctx The context to prepare. This must be initialized by
  568. * a call to mbedtls_cipher_init() first.
  569. * \param cipher_info The cipher to use.
  570. *
  571. * \return \c 0 on success.
  572. * \return #MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA on
  573. * parameter-verification failure.
  574. * \return #MBEDTLS_ERR_CIPHER_ALLOC_FAILED if allocation of the
  575. * cipher-specific context fails.
  576. */
  577. int mbedtls_cipher_setup(mbedtls_cipher_context_t *ctx,
  578. const mbedtls_cipher_info_t *cipher_info);
  579. #if defined(MBEDTLS_USE_PSA_CRYPTO)
  580. #if !defined(MBEDTLS_DEPRECATED_REMOVED)
  581. /**
  582. * \brief This function initializes a cipher context for
  583. * PSA-based use with the given cipher primitive.
  584. *
  585. * \deprecated This function is deprecated and will be removed in a
  586. * future version of the library.
  587. * Please use psa_aead_xxx() / psa_cipher_xxx() directly
  588. * instead.
  589. *
  590. * \note See #MBEDTLS_USE_PSA_CRYPTO for information on PSA.
  591. *
  592. * \param ctx The context to initialize. May not be \c NULL.
  593. * \param cipher_info The cipher to use.
  594. * \param taglen For AEAD ciphers, the length in bytes of the
  595. * authentication tag to use. Subsequent uses of
  596. * mbedtls_cipher_auth_encrypt_ext() or
  597. * mbedtls_cipher_auth_decrypt_ext() must provide
  598. * the same tag length.
  599. * For non-AEAD ciphers, the value must be \c 0.
  600. *
  601. * \return \c 0 on success.
  602. * \return #MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA on
  603. * parameter-verification failure.
  604. * \return #MBEDTLS_ERR_CIPHER_ALLOC_FAILED if allocation of the
  605. * cipher-specific context fails.
  606. */
  607. int MBEDTLS_DEPRECATED mbedtls_cipher_setup_psa(mbedtls_cipher_context_t *ctx,
  608. const mbedtls_cipher_info_t *cipher_info,
  609. size_t taglen);
  610. #endif /* MBEDTLS_DEPRECATED_REMOVED */
  611. #endif /* MBEDTLS_USE_PSA_CRYPTO */
  612. /**
  613. * \brief This function returns the block size of the given cipher
  614. * in bytes.
  615. *
  616. * \param ctx The context of the cipher.
  617. *
  618. * \return The block size of the underlying cipher.
  619. * \return \c 1 if the cipher is a stream cipher.
  620. * \return \c 0 if \p ctx has not been initialized.
  621. */
  622. static inline unsigned int mbedtls_cipher_get_block_size(
  623. const mbedtls_cipher_context_t *ctx)
  624. {
  625. if (ctx->MBEDTLS_PRIVATE(cipher_info) == NULL) {
  626. return 0;
  627. }
  628. return (unsigned int) ctx->MBEDTLS_PRIVATE(cipher_info)->MBEDTLS_PRIVATE(block_size);
  629. }
  630. /**
  631. * \brief This function returns the mode of operation for
  632. * the cipher. For example, MBEDTLS_MODE_CBC.
  633. *
  634. * \param ctx The context of the cipher. This must be initialized.
  635. *
  636. * \return The mode of operation.
  637. * \return #MBEDTLS_MODE_NONE if \p ctx has not been initialized.
  638. */
  639. static inline mbedtls_cipher_mode_t mbedtls_cipher_get_cipher_mode(
  640. const mbedtls_cipher_context_t *ctx)
  641. {
  642. if (ctx->MBEDTLS_PRIVATE(cipher_info) == NULL) {
  643. return MBEDTLS_MODE_NONE;
  644. }
  645. return (mbedtls_cipher_mode_t) ctx->MBEDTLS_PRIVATE(cipher_info)->MBEDTLS_PRIVATE(mode);
  646. }
  647. /**
  648. * \brief This function returns the size of the IV or nonce
  649. * of the cipher, in Bytes.
  650. *
  651. * \param ctx The context of the cipher. This must be initialized.
  652. *
  653. * \return The recommended IV size if no IV has been set.
  654. * \return \c 0 for ciphers not using an IV or a nonce.
  655. * \return The actual size if an IV has been set.
  656. */
  657. static inline int mbedtls_cipher_get_iv_size(
  658. const mbedtls_cipher_context_t *ctx)
  659. {
  660. if (ctx->MBEDTLS_PRIVATE(cipher_info) == NULL) {
  661. return 0;
  662. }
  663. if (ctx->MBEDTLS_PRIVATE(iv_size) != 0) {
  664. return (int) ctx->MBEDTLS_PRIVATE(iv_size);
  665. }
  666. return (int) (((int) ctx->MBEDTLS_PRIVATE(cipher_info)->MBEDTLS_PRIVATE(iv_size)) <<
  667. MBEDTLS_IV_SIZE_SHIFT);
  668. }
  669. /**
  670. * \brief This function returns the type of the given cipher.
  671. *
  672. * \param ctx The context of the cipher. This must be initialized.
  673. *
  674. * \return The type of the cipher.
  675. * \return #MBEDTLS_CIPHER_NONE if \p ctx has not been initialized.
  676. */
  677. static inline mbedtls_cipher_type_t mbedtls_cipher_get_type(
  678. const mbedtls_cipher_context_t *ctx)
  679. {
  680. if (ctx->MBEDTLS_PRIVATE(cipher_info) == NULL) {
  681. return MBEDTLS_CIPHER_NONE;
  682. }
  683. return (mbedtls_cipher_type_t) ctx->MBEDTLS_PRIVATE(cipher_info)->MBEDTLS_PRIVATE(type);
  684. }
  685. /**
  686. * \brief This function returns the name of the given cipher
  687. * as a string.
  688. *
  689. * \param ctx The context of the cipher. This must be initialized.
  690. *
  691. * \return The name of the cipher.
  692. * \return NULL if \p ctx has not been not initialized.
  693. */
  694. static inline const char *mbedtls_cipher_get_name(
  695. const mbedtls_cipher_context_t *ctx)
  696. {
  697. if (ctx->MBEDTLS_PRIVATE(cipher_info) == NULL) {
  698. return 0;
  699. }
  700. return ctx->MBEDTLS_PRIVATE(cipher_info)->MBEDTLS_PRIVATE(name);
  701. }
  702. /**
  703. * \brief This function returns the key length of the cipher.
  704. *
  705. * \param ctx The context of the cipher. This must be initialized.
  706. *
  707. * \return The key length of the cipher in bits.
  708. * \return #MBEDTLS_KEY_LENGTH_NONE if \p ctx has not been
  709. * initialized.
  710. */
  711. static inline int mbedtls_cipher_get_key_bitlen(
  712. const mbedtls_cipher_context_t *ctx)
  713. {
  714. if (ctx->MBEDTLS_PRIVATE(cipher_info) == NULL) {
  715. return MBEDTLS_KEY_LENGTH_NONE;
  716. }
  717. return (int) ctx->MBEDTLS_PRIVATE(cipher_info)->MBEDTLS_PRIVATE(key_bitlen) <<
  718. MBEDTLS_KEY_BITLEN_SHIFT;
  719. }
  720. /**
  721. * \brief This function returns the operation of the given cipher.
  722. *
  723. * \param ctx The context of the cipher. This must be initialized.
  724. *
  725. * \return The type of operation: #MBEDTLS_ENCRYPT or #MBEDTLS_DECRYPT.
  726. * \return #MBEDTLS_OPERATION_NONE if \p ctx has not been initialized.
  727. */
  728. static inline mbedtls_operation_t mbedtls_cipher_get_operation(
  729. const mbedtls_cipher_context_t *ctx)
  730. {
  731. if (ctx->MBEDTLS_PRIVATE(cipher_info) == NULL) {
  732. return MBEDTLS_OPERATION_NONE;
  733. }
  734. return ctx->MBEDTLS_PRIVATE(operation);
  735. }
  736. /**
  737. * \brief This function sets the key to use with the given context.
  738. *
  739. * \param ctx The generic cipher context. This must be initialized and
  740. * bound to a cipher information structure.
  741. * \param key The key to use. This must be a readable buffer of at
  742. * least \p key_bitlen Bits.
  743. * \param key_bitlen The key length to use, in Bits.
  744. * \param operation The operation that the key will be used for:
  745. * #MBEDTLS_ENCRYPT or #MBEDTLS_DECRYPT.
  746. *
  747. * \return \c 0 on success.
  748. * \return #MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA on
  749. * parameter-verification failure.
  750. * \return A cipher-specific error code on failure.
  751. */
  752. int mbedtls_cipher_setkey(mbedtls_cipher_context_t *ctx,
  753. const unsigned char *key,
  754. int key_bitlen,
  755. const mbedtls_operation_t operation);
  756. #if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
  757. /**
  758. * \brief This function sets the padding mode, for cipher modes
  759. * that use padding.
  760. *
  761. *
  762. * \param ctx The generic cipher context. This must be initialized and
  763. * bound to a cipher information structure.
  764. * \param mode The padding mode.
  765. *
  766. * \return \c 0 on success.
  767. * \return #MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE
  768. * if the selected padding mode is not supported.
  769. * \return #MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA if the cipher mode
  770. * does not support padding.
  771. */
  772. int mbedtls_cipher_set_padding_mode(mbedtls_cipher_context_t *ctx,
  773. mbedtls_cipher_padding_t mode);
  774. #endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
  775. /**
  776. * \brief This function sets the initialization vector (IV)
  777. * or nonce.
  778. *
  779. * \note Some ciphers do not use IVs nor nonce. For these
  780. * ciphers, this function has no effect.
  781. *
  782. * \note For #MBEDTLS_CIPHER_CHACHA20, the nonce length must
  783. * be 12, and the initial counter value is 0.
  784. *
  785. * \note For #MBEDTLS_CIPHER_CHACHA20_POLY1305, the nonce length
  786. * must be 12.
  787. *
  788. * \param ctx The generic cipher context. This must be initialized and
  789. * bound to a cipher information structure.
  790. * \param iv The IV to use, or NONCE_COUNTER for CTR-mode ciphers. This
  791. * must be a readable buffer of at least \p iv_len Bytes.
  792. * \param iv_len The IV length for ciphers with variable-size IV.
  793. * This parameter is discarded by ciphers with fixed-size IV.
  794. *
  795. * \return \c 0 on success.
  796. * \return #MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA on
  797. * parameter-verification failure.
  798. */
  799. int mbedtls_cipher_set_iv(mbedtls_cipher_context_t *ctx,
  800. const unsigned char *iv,
  801. size_t iv_len);
  802. /**
  803. * \brief This function resets the cipher state.
  804. *
  805. * \note With non-AEAD ciphers, the order of calls for each message
  806. * is as follows:
  807. * 1. mbedtls_cipher_set_iv() if the mode uses an IV/nonce;
  808. * 2. mbedtls_cipher_reset();
  809. * 3. mbedtls_cipher_update() zero, one or more times;
  810. * 4. mbedtls_cipher_finish_padded() (recommended for decryption
  811. * if the mode uses padding) or mbedtls_cipher_finish().
  812. * .
  813. * This sequence can be repeated to encrypt or decrypt multiple
  814. * messages with the same key.
  815. *
  816. * \note With AEAD ciphers, the order of calls for each message
  817. * is as follows:
  818. * 1. mbedtls_cipher_set_iv() if the mode uses an IV/nonce;
  819. * 2. mbedtls_cipher_reset();
  820. * 3. mbedtls_cipher_update_ad();
  821. * 4. mbedtls_cipher_update() zero, one or more times;
  822. * 5. mbedtls_cipher_finish() (or mbedtls_cipher_finish_padded());
  823. * 6. mbedtls_cipher_check_tag() (for decryption) or
  824. * mbedtls_cipher_write_tag() (for encryption).
  825. * .
  826. * This sequence can be repeated to encrypt or decrypt multiple
  827. * messages with the same key.
  828. *
  829. * \param ctx The generic cipher context. This must be bound to a key.
  830. *
  831. * \return \c 0 on success.
  832. * \return #MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA on
  833. * parameter-verification failure.
  834. */
  835. int mbedtls_cipher_reset(mbedtls_cipher_context_t *ctx);
  836. #if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
  837. /**
  838. * \brief This function adds additional data for AEAD ciphers.
  839. * Currently supported with GCM and ChaCha20+Poly1305.
  840. *
  841. * \param ctx The generic cipher context. This must be initialized.
  842. * \param ad The additional data to use. This must be a readable
  843. * buffer of at least \p ad_len Bytes.
  844. * \param ad_len The length of \p ad in Bytes.
  845. *
  846. * \return \c 0 on success.
  847. * \return A specific error code on failure.
  848. */
  849. int mbedtls_cipher_update_ad(mbedtls_cipher_context_t *ctx,
  850. const unsigned char *ad, size_t ad_len);
  851. #endif /* MBEDTLS_GCM_C || MBEDTLS_CHACHAPOLY_C */
  852. /**
  853. * \brief The generic cipher update function. It encrypts or
  854. * decrypts using the given cipher context. Writes as
  855. * many block-sized blocks of data as possible to output.
  856. * Any data that cannot be written immediately is either
  857. * added to the next block, or flushed when
  858. * mbedtls_cipher_finish() or mbedtls_cipher_finish_padded()
  859. * is called.
  860. * Exception: For MBEDTLS_MODE_ECB, expects a single block
  861. * in size. For example, 16 Bytes for AES.
  862. *
  863. * \param ctx The generic cipher context. This must be initialized and
  864. * bound to a key.
  865. * \param input The buffer holding the input data. This must be a
  866. * readable buffer of at least \p ilen Bytes.
  867. * \param ilen The length of the input data.
  868. * \param output The buffer for the output data. This must be able to
  869. * hold at least `ilen + block_size`. This must not be the
  870. * same buffer as \p input.
  871. * \param olen The length of the output data, to be updated with the
  872. * actual number of Bytes written. This must not be
  873. * \c NULL.
  874. *
  875. * \return \c 0 on success.
  876. * \return #MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA on
  877. * parameter-verification failure.
  878. * \return #MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE on an
  879. * unsupported mode for a cipher.
  880. * \return A cipher-specific error code on failure.
  881. */
  882. int mbedtls_cipher_update(mbedtls_cipher_context_t *ctx,
  883. const unsigned char *input,
  884. size_t ilen, unsigned char *output,
  885. size_t *olen);
  886. /**
  887. * \brief The generic cipher finalization function. If data still
  888. * needs to be flushed from an incomplete block, the data
  889. * contained in it is padded to the size of
  890. * the last block, and written to the \p output buffer.
  891. *
  892. * \warning This function reports invalid padding through an error
  893. * code. Adversaries may be able to decrypt encrypted
  894. * data if they can submit chosen ciphertexts and
  895. * detect whether it has valid padding or not,
  896. * either through direct observation or through a side
  897. * channel such as timing. This is known as a
  898. * padding oracle attack.
  899. * Therefore applications that call this function for
  900. * decryption with a cipher that involves padding
  901. * should take care around error handling. Preferably,
  902. * such applications should use
  903. * mbedtls_cipher_finish_padded() instead of this function.
  904. *
  905. * \param ctx The generic cipher context. This must be initialized and
  906. * bound to a key.
  907. * \param output The buffer to write data to. This needs to be a writable
  908. * buffer of at least block_size Bytes.
  909. * \param olen The length of the data written to the \p output buffer.
  910. * This may not be \c NULL.
  911. * Note that when decrypting in a mode with padding,
  912. * the actual output length is sensitive and may be
  913. * used to mount a padding oracle attack (see warning
  914. * above), although less efficiently than through
  915. * the invalid-padding condition.
  916. *
  917. * \return \c 0 on success.
  918. * \return #MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA on
  919. * parameter-verification failure.
  920. * \return #MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED on decryption
  921. * expecting a full block but not receiving one.
  922. * \return #MBEDTLS_ERR_CIPHER_INVALID_PADDING on invalid padding
  923. * while decrypting. Note that invalid-padding errors
  924. * should be handled carefully; see the warning above.
  925. * \return A cipher-specific error code on failure.
  926. */
  927. int mbedtls_cipher_finish(mbedtls_cipher_context_t *ctx,
  928. unsigned char *output, size_t *olen);
  929. /**
  930. * \brief The generic cipher finalization function. If data still
  931. * needs to be flushed from an incomplete block, the data
  932. * contained in it is padded to the size of
  933. * the last block, and written to the \p output buffer.
  934. *
  935. * \note This function is similar to mbedtls_cipher_finish().
  936. * The only difference is that it reports invalid padding
  937. * decryption differently, through the \p invalid_padding
  938. * parameter rather than an error code.
  939. * For encryption, and in modes without padding (including
  940. * all authenticated modes), this function is identical
  941. * to mbedtls_cipher_finish().
  942. *
  943. * \param[in,out] ctx The generic cipher context. This must be initialized and
  944. * bound to a key.
  945. * \param[out] output The buffer to write data to. This needs to be a writable
  946. * buffer of at least block_size Bytes.
  947. * \param[out] olen The length of the data written to the \p output buffer.
  948. * This may not be \c NULL.
  949. * Note that when decrypting in a mode with padding,
  950. * the actual output length is sensitive and may be
  951. * used to mount a padding oracle attack (see warning
  952. * on mbedtls_cipher_finish()).
  953. * \param[out] invalid_padding
  954. * If this function returns \c 0 on decryption,
  955. * \p *invalid_padding is \c 0 if the ciphertext was
  956. * valid, and all-bits-one if the ciphertext had invalid
  957. * padding.
  958. * On encryption, or in a mode without padding (including
  959. * all authenticated modes), \p *invalid_padding is \c 0
  960. * on success.
  961. * The value in \p *invalid_padding is unspecified if
  962. * this function returns a nonzero status.
  963. *
  964. * \return \c 0 on success.
  965. * Also \c 0 for decryption with invalid padding.
  966. * \return #MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA on
  967. * parameter-verification failure.
  968. * \return #MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED on decryption
  969. * expecting a full block but not receiving one.
  970. * \return A cipher-specific error code on failure.
  971. */
  972. int mbedtls_cipher_finish_padded(mbedtls_cipher_context_t *ctx,
  973. unsigned char *output, size_t *olen,
  974. size_t *invalid_padding);
  975. #if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
  976. /**
  977. * \brief This function writes a tag for AEAD ciphers.
  978. * Currently supported with GCM and ChaCha20+Poly1305.
  979. * This must be called after mbedtls_cipher_finish()
  980. * or mbedtls_cipher_finish_padded().
  981. *
  982. * \param ctx The generic cipher context. This must be initialized,
  983. * bound to a key, and have just completed a cipher
  984. * operation through mbedtls_cipher_finish() the tag for
  985. * which should be written.
  986. * \param tag The buffer to write the tag to. This must be a writable
  987. * buffer of at least \p tag_len Bytes.
  988. * \param tag_len The length of the tag to write.
  989. *
  990. * \return \c 0 on success.
  991. * \return A specific error code on failure.
  992. */
  993. int mbedtls_cipher_write_tag(mbedtls_cipher_context_t *ctx,
  994. unsigned char *tag, size_t tag_len);
  995. /**
  996. * \brief This function checks the tag for AEAD ciphers.
  997. * Currently supported with GCM and ChaCha20+Poly1305.
  998. * This must be called after mbedtls_cipher_finish()
  999. * or mbedtls_cipher_finish_padded().
  1000. *
  1001. * \param ctx The generic cipher context. This must be initialized.
  1002. * \param tag The buffer holding the tag. This must be a readable
  1003. * buffer of at least \p tag_len Bytes.
  1004. * \param tag_len The length of the tag to check.
  1005. *
  1006. * \return \c 0 on success.
  1007. * \return A specific error code on failure.
  1008. */
  1009. int mbedtls_cipher_check_tag(mbedtls_cipher_context_t *ctx,
  1010. const unsigned char *tag, size_t tag_len);
  1011. #endif /* MBEDTLS_GCM_C || MBEDTLS_CHACHAPOLY_C */
  1012. /**
  1013. * \brief The generic all-in-one encryption/decryption function,
  1014. * for all ciphers except AEAD constructs.
  1015. *
  1016. * \param ctx The generic cipher context. This must be initialized.
  1017. * \param iv The IV to use, or NONCE_COUNTER for CTR-mode ciphers.
  1018. * This must be a readable buffer of at least \p iv_len
  1019. * Bytes.
  1020. * \param iv_len The IV length for ciphers with variable-size IV.
  1021. * This parameter is discarded by ciphers with fixed-size
  1022. * IV.
  1023. * \param input The buffer holding the input data. This must be a
  1024. * readable buffer of at least \p ilen Bytes.
  1025. * \param ilen The length of the input data in Bytes.
  1026. * \param output The buffer for the output data. This must be able to
  1027. * hold at least `ilen + block_size`. This must not be the
  1028. * same buffer as \p input.
  1029. * \param olen The length of the output data, to be updated with the
  1030. * actual number of Bytes written. This must not be
  1031. * \c NULL.
  1032. *
  1033. * \note Some ciphers do not use IVs nor nonce. For these
  1034. * ciphers, use \p iv = NULL and \p iv_len = 0.
  1035. *
  1036. * \return \c 0 on success.
  1037. * \return #MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA on
  1038. * parameter-verification failure.
  1039. * \return #MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED on decryption
  1040. * expecting a full block but not receiving one.
  1041. * \return #MBEDTLS_ERR_CIPHER_INVALID_PADDING on invalid padding
  1042. * while decrypting.
  1043. * \return A cipher-specific error code on failure.
  1044. */
  1045. int mbedtls_cipher_crypt(mbedtls_cipher_context_t *ctx,
  1046. const unsigned char *iv, size_t iv_len,
  1047. const unsigned char *input, size_t ilen,
  1048. unsigned char *output, size_t *olen);
  1049. #if defined(MBEDTLS_CIPHER_MODE_AEAD) || defined(MBEDTLS_NIST_KW_C)
  1050. /**
  1051. * \brief The authenticated encryption (AEAD/NIST_KW) function.
  1052. *
  1053. * \note For AEAD modes, the tag will be appended to the
  1054. * ciphertext, as recommended by RFC 5116.
  1055. * (NIST_KW doesn't have a separate tag.)
  1056. *
  1057. * \param ctx The generic cipher context. This must be initialized and
  1058. * bound to a key, with an AEAD algorithm or NIST_KW.
  1059. * \param iv The nonce to use. This must be a readable buffer of
  1060. * at least \p iv_len Bytes and may be \c NULL if \p
  1061. * iv_len is \c 0.
  1062. * \param iv_len The length of the nonce. For AEAD ciphers, this must
  1063. * satisfy the constraints imposed by the cipher used.
  1064. * For NIST_KW, this must be \c 0.
  1065. * \param ad The additional data to authenticate. This must be a
  1066. * readable buffer of at least \p ad_len Bytes, and may
  1067. * be \c NULL is \p ad_len is \c 0.
  1068. * \param ad_len The length of \p ad. For NIST_KW, this must be \c 0.
  1069. * \param input The buffer holding the input data. This must be a
  1070. * readable buffer of at least \p ilen Bytes, and may be
  1071. * \c NULL if \p ilen is \c 0.
  1072. * \param ilen The length of the input data.
  1073. * \param output The buffer for the output data. This must be a
  1074. * writable buffer of at least \p output_len Bytes, and
  1075. * must not be \c NULL.
  1076. * \param output_len The length of the \p output buffer in Bytes. For AEAD
  1077. * ciphers, this must be at least \p ilen + \p tag_len.
  1078. * For NIST_KW, this must be at least \p ilen + 8
  1079. * (rounded up to a multiple of 8 if KWP is used);
  1080. * \p ilen + 15 is always a safe value.
  1081. * \param olen This will be filled with the actual number of Bytes
  1082. * written to the \p output buffer. This must point to a
  1083. * writable object of type \c size_t.
  1084. * \param tag_len The desired length of the authentication tag. For AEAD
  1085. * ciphers, this must match the constraints imposed by
  1086. * the cipher used, and in particular must not be \c 0.
  1087. * For NIST_KW, this must be \c 0.
  1088. *
  1089. * \return \c 0 on success.
  1090. * \return #MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA on
  1091. * parameter-verification failure.
  1092. * \return A cipher-specific error code on failure.
  1093. */
  1094. int mbedtls_cipher_auth_encrypt_ext(mbedtls_cipher_context_t *ctx,
  1095. const unsigned char *iv, size_t iv_len,
  1096. const unsigned char *ad, size_t ad_len,
  1097. const unsigned char *input, size_t ilen,
  1098. unsigned char *output, size_t output_len,
  1099. size_t *olen, size_t tag_len);
  1100. /**
  1101. * \brief The authenticated encryption (AEAD/NIST_KW) function.
  1102. *
  1103. * \note If the data is not authentic, then the output buffer
  1104. * is zeroed out to prevent the unauthentic plaintext being
  1105. * used, making this interface safer.
  1106. *
  1107. * \note For AEAD modes, the tag must be appended to the
  1108. * ciphertext, as recommended by RFC 5116.
  1109. * (NIST_KW doesn't have a separate tag.)
  1110. *
  1111. * \param ctx The generic cipher context. This must be initialized and
  1112. * bound to a key, with an AEAD algorithm or NIST_KW.
  1113. * \param iv The nonce to use. This must be a readable buffer of
  1114. * at least \p iv_len Bytes and may be \c NULL if \p
  1115. * iv_len is \c 0.
  1116. * \param iv_len The length of the nonce. For AEAD ciphers, this must
  1117. * satisfy the constraints imposed by the cipher used.
  1118. * For NIST_KW, this must be \c 0.
  1119. * \param ad The additional data to authenticate. This must be a
  1120. * readable buffer of at least \p ad_len Bytes, and may
  1121. * be \c NULL is \p ad_len is \c 0.
  1122. * \param ad_len The length of \p ad. For NIST_KW, this must be \c 0.
  1123. * \param input The buffer holding the input data. This must be a
  1124. * readable buffer of at least \p ilen Bytes, and may be
  1125. * \c NULL if \p ilen is \c 0.
  1126. * \param ilen The length of the input data. For AEAD ciphers this
  1127. * must be at least \p tag_len. For NIST_KW this must be
  1128. * at least \c 8.
  1129. * \param output The buffer for the output data. This must be a
  1130. * writable buffer of at least \p output_len Bytes, and
  1131. * may be \c NULL if \p output_len is \c 0.
  1132. * \param output_len The length of the \p output buffer in Bytes. For AEAD
  1133. * ciphers, this must be at least \p ilen - \p tag_len.
  1134. * For NIST_KW, this must be at least \p ilen - 8.
  1135. * \param olen This will be filled with the actual number of Bytes
  1136. * written to the \p output buffer. This must point to a
  1137. * writable object of type \c size_t.
  1138. * \param tag_len The actual length of the authentication tag. For AEAD
  1139. * ciphers, this must match the constraints imposed by
  1140. * the cipher used, and in particular must not be \c 0.
  1141. * For NIST_KW, this must be \c 0.
  1142. *
  1143. * \return \c 0 on success.
  1144. * \return #MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA on
  1145. * parameter-verification failure.
  1146. * \return #MBEDTLS_ERR_CIPHER_AUTH_FAILED if data is not authentic.
  1147. * \return A cipher-specific error code on failure.
  1148. */
  1149. int mbedtls_cipher_auth_decrypt_ext(mbedtls_cipher_context_t *ctx,
  1150. const unsigned char *iv, size_t iv_len,
  1151. const unsigned char *ad, size_t ad_len,
  1152. const unsigned char *input, size_t ilen,
  1153. unsigned char *output, size_t output_len,
  1154. size_t *olen, size_t tag_len);
  1155. #endif /* MBEDTLS_CIPHER_MODE_AEAD || MBEDTLS_NIST_KW_C */
  1156. #ifdef __cplusplus
  1157. }
  1158. #endif
  1159. #endif /* MBEDTLS_CIPHER_H */