blob: db73c1b537ee9ae6b9afcd349c3aada6609e84aa [file] [log] [blame]
Paul Bakker8123e9d2011-01-06 15:37:30 +00001/**
2 * \file cipher.h
Paul Bakker9af723c2014-05-01 13:03:14 +02003 *
Rose Zadik826f2642018-04-19 14:01:29 +01004 * \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.
Paul Bakker8123e9d2011-01-06 15:37:30 +00007 *
8 * \author Adriaan de Jong <dejong@fox-it.com>
Darryl Greena40a1012018-01-05 15:33:17 +00009 */
10/*
Bence Szépkúti1e148272020-08-07 13:07:28 +020011 * Copyright The Mbed TLS Contributors
Dave Rodgman7ff79652023-11-03 12:04:52 +000012 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Paul Bakker8123e9d2011-01-06 15:37:30 +000013 */
14
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020015#ifndef MBEDTLS_CIPHER_H
16#define MBEDTLS_CIPHER_H
Paul Bakker8123e9d2011-01-06 15:37:30 +000017
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020018#if !defined(MBEDTLS_CONFIG_FILE)
Jaeden Ameroc49fbbf2019-07-04 20:01:14 +010019#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020020#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020021#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020022#endif
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +020023
Rich Evans00ab4702015-02-06 13:43:58 +000024#include <stddef.h>
Jaeden Ameroc49fbbf2019-07-04 20:01:14 +010025#include "mbedtls/platform_util.h"
Rich Evans00ab4702015-02-06 13:43:58 +000026
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +020027#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020028#define MBEDTLS_CIPHER_MODE_AEAD
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +020029#endif
30
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020031#if defined(MBEDTLS_CIPHER_MODE_CBC)
32#define MBEDTLS_CIPHER_MODE_WITH_PADDING
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +020033#endif
34
Simon Butcherc7965732018-07-27 17:13:39 +010035#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER) || \
36 defined(MBEDTLS_CHACHA20_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020037#define MBEDTLS_CIPHER_MODE_STREAM
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +010038#endif
39
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010040#if (defined(__ARMCC_VERSION) || defined(_MSC_VER)) && \
Manuel Pégourié-Gonnard0223ab92015-10-05 11:40:01 +010041 !defined(inline) && !defined(__cplusplus)
Paul Bakker569df2c2011-06-21 07:48:07 +000042#define inline __inline
Manuel Pégourié-Gonnard20af64d2015-07-07 18:33:39 +020043#endif
Paul Bakkeraf5c85f2011-04-18 03:47:52 +000044
Gilles Peskinea3974432021-07-26 18:48:10 +020045/** The selected feature is not available. */
46#define MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE -0x6080
47/** Bad input parameters. */
48#define MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA -0x6100
49/** Failed to allocate memory. */
50#define MBEDTLS_ERR_CIPHER_ALLOC_FAILED -0x6180
51/** Input data contains invalid padding and is rejected. */
52#define MBEDTLS_ERR_CIPHER_INVALID_PADDING -0x6200
53/** Decryption of block requires a full block. */
54#define MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED -0x6280
55/** Authentication failed (for AEAD modes). */
56#define MBEDTLS_ERR_CIPHER_AUTH_FAILED -0x6300
57/** The context is invalid. For example, because it was freed. */
58#define MBEDTLS_ERR_CIPHER_INVALID_CONTEXT -0x6380
Ron Eldor9924bdc2018-10-04 10:59:13 +030059
60/* MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED is deprecated and should not be used. */
Gilles Peskinea3974432021-07-26 18:48:10 +020061/** Cipher hardware accelerator failed. */
62#define MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED -0x6400
Paul Bakker343a8702011-06-09 14:27:58 +000063
Rose Zadik9ba6b622018-01-24 12:59:19 +000064#define MBEDTLS_CIPHER_VARIABLE_IV_LEN 0x01 /**< Cipher accepts IVs of variable length. */
65#define MBEDTLS_CIPHER_VARIABLE_KEY_LEN 0x02 /**< Cipher accepts keys of variable length. */
Manuel Pégourié-Gonnard81754a02014-06-23 11:33:18 +020066
Paul Bakker407a0da2013-06-27 14:29:21 +020067#ifdef __cplusplus
68extern "C" {
69#endif
70
Hanno Beckerbbca8c52017-09-25 14:53:51 +010071/**
Rose Zadik02f73a62018-03-26 18:02:32 +010072 * \brief Supported cipher types.
Hanno Beckerbbca8c52017-09-25 14:53:51 +010073 *
Dave Rodgman4ff02312023-02-02 13:17:34 +000074 * \warning RC4 and DES/3DES are considered weak ciphers and their use
75 * constitutes a security risk. We recommend considering stronger
Rose Zadikb5607bf2018-04-16 10:34:51 +010076 * ciphers instead.
Hanno Beckerbbca8c52017-09-25 14:53:51 +010077 */
Paul Bakker8123e9d2011-01-06 15:37:30 +000078typedef enum {
Rose Zadik4c368e82018-04-19 14:24:11 +010079 MBEDTLS_CIPHER_ID_NONE = 0, /**< Placeholder to mark the end of cipher ID lists. */
80 MBEDTLS_CIPHER_ID_NULL, /**< The identity cipher, treated as a stream cipher. */
Rose Zadik02f73a62018-03-26 18:02:32 +010081 MBEDTLS_CIPHER_ID_AES, /**< The AES cipher. */
Dave Rodgman4ff02312023-02-02 13:17:34 +000082 MBEDTLS_CIPHER_ID_DES, /**< The DES cipher. \warning DES is considered weak. */
83 MBEDTLS_CIPHER_ID_3DES, /**< The Triple DES cipher. \warning 3DES is considered weak. */
Rose Zadik02f73a62018-03-26 18:02:32 +010084 MBEDTLS_CIPHER_ID_CAMELLIA, /**< The Camellia cipher. */
85 MBEDTLS_CIPHER_ID_BLOWFISH, /**< The Blowfish cipher. */
Rose Zadik826f2642018-04-19 14:01:29 +010086 MBEDTLS_CIPHER_ID_ARC4, /**< The RC4 cipher. */
Manuel Pégourié-Gonnarda3712be2018-05-22 15:58:50 +020087 MBEDTLS_CIPHER_ID_ARIA, /**< The Aria cipher. */
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +020088 MBEDTLS_CIPHER_ID_CHACHA20, /**< The ChaCha20 cipher. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020089} mbedtls_cipher_id_t;
Paul Bakker8123e9d2011-01-06 15:37:30 +000090
Hanno Beckerbbca8c52017-09-25 14:53:51 +010091/**
Rose Zadik02f73a62018-03-26 18:02:32 +010092 * \brief Supported {cipher type, cipher mode} pairs.
Hanno Beckerbbca8c52017-09-25 14:53:51 +010093 *
Dave Rodgmanb6c5d042023-02-02 13:42:38 +000094 * \warning RC4 and DES/3DES are considered weak ciphers and their use
95 * constitutes a security risk. We recommend considering stronger
Hanno Beckerbbca8c52017-09-25 14:53:51 +010096 * ciphers instead.
97 */
Paul Bakker8123e9d2011-01-06 15:37:30 +000098typedef enum {
Rose Zadik4c368e82018-04-19 14:24:11 +010099 MBEDTLS_CIPHER_NONE = 0, /**< Placeholder to mark the end of cipher-pair lists. */
Rose Zadikc441f742018-04-19 14:38:20 +0100100 MBEDTLS_CIPHER_NULL, /**< The identity stream cipher. */
Rose Zadik02f73a62018-03-26 18:02:32 +0100101 MBEDTLS_CIPHER_AES_128_ECB, /**< AES cipher with 128-bit ECB mode. */
102 MBEDTLS_CIPHER_AES_192_ECB, /**< AES cipher with 192-bit ECB mode. */
103 MBEDTLS_CIPHER_AES_256_ECB, /**< AES cipher with 256-bit ECB mode. */
104 MBEDTLS_CIPHER_AES_128_CBC, /**< AES cipher with 128-bit CBC mode. */
105 MBEDTLS_CIPHER_AES_192_CBC, /**< AES cipher with 192-bit CBC mode. */
106 MBEDTLS_CIPHER_AES_256_CBC, /**< AES cipher with 256-bit CBC mode. */
107 MBEDTLS_CIPHER_AES_128_CFB128, /**< AES cipher with 128-bit CFB128 mode. */
108 MBEDTLS_CIPHER_AES_192_CFB128, /**< AES cipher with 192-bit CFB128 mode. */
109 MBEDTLS_CIPHER_AES_256_CFB128, /**< AES cipher with 256-bit CFB128 mode. */
110 MBEDTLS_CIPHER_AES_128_CTR, /**< AES cipher with 128-bit CTR mode. */
111 MBEDTLS_CIPHER_AES_192_CTR, /**< AES cipher with 192-bit CTR mode. */
112 MBEDTLS_CIPHER_AES_256_CTR, /**< AES cipher with 256-bit CTR mode. */
113 MBEDTLS_CIPHER_AES_128_GCM, /**< AES cipher with 128-bit GCM mode. */
114 MBEDTLS_CIPHER_AES_192_GCM, /**< AES cipher with 192-bit GCM mode. */
115 MBEDTLS_CIPHER_AES_256_GCM, /**< AES cipher with 256-bit GCM mode. */
116 MBEDTLS_CIPHER_CAMELLIA_128_ECB, /**< Camellia cipher with 128-bit ECB mode. */
117 MBEDTLS_CIPHER_CAMELLIA_192_ECB, /**< Camellia cipher with 192-bit ECB mode. */
118 MBEDTLS_CIPHER_CAMELLIA_256_ECB, /**< Camellia cipher with 256-bit ECB mode. */
119 MBEDTLS_CIPHER_CAMELLIA_128_CBC, /**< Camellia cipher with 128-bit CBC mode. */
120 MBEDTLS_CIPHER_CAMELLIA_192_CBC, /**< Camellia cipher with 192-bit CBC mode. */
121 MBEDTLS_CIPHER_CAMELLIA_256_CBC, /**< Camellia cipher with 256-bit CBC mode. */
122 MBEDTLS_CIPHER_CAMELLIA_128_CFB128, /**< Camellia cipher with 128-bit CFB128 mode. */
123 MBEDTLS_CIPHER_CAMELLIA_192_CFB128, /**< Camellia cipher with 192-bit CFB128 mode. */
124 MBEDTLS_CIPHER_CAMELLIA_256_CFB128, /**< Camellia cipher with 256-bit CFB128 mode. */
125 MBEDTLS_CIPHER_CAMELLIA_128_CTR, /**< Camellia cipher with 128-bit CTR mode. */
126 MBEDTLS_CIPHER_CAMELLIA_192_CTR, /**< Camellia cipher with 192-bit CTR mode. */
127 MBEDTLS_CIPHER_CAMELLIA_256_CTR, /**< Camellia cipher with 256-bit CTR mode. */
128 MBEDTLS_CIPHER_CAMELLIA_128_GCM, /**< Camellia cipher with 128-bit GCM mode. */
129 MBEDTLS_CIPHER_CAMELLIA_192_GCM, /**< Camellia cipher with 192-bit GCM mode. */
130 MBEDTLS_CIPHER_CAMELLIA_256_GCM, /**< Camellia cipher with 256-bit GCM mode. */
Dave Rodgmanb6c5d042023-02-02 13:42:38 +0000131 MBEDTLS_CIPHER_DES_ECB, /**< DES cipher with ECB mode. \warning DES is considered weak. */
132 MBEDTLS_CIPHER_DES_CBC, /**< DES cipher with CBC mode. \warning DES is considered weak. */
133 MBEDTLS_CIPHER_DES_EDE_ECB, /**< DES cipher with EDE ECB mode. \warning 3DES is considered weak. */
134 MBEDTLS_CIPHER_DES_EDE_CBC, /**< DES cipher with EDE CBC mode. \warning 3DES is considered weak. */
135 MBEDTLS_CIPHER_DES_EDE3_ECB, /**< DES cipher with EDE3 ECB mode. \warning 3DES is considered weak. */
136 MBEDTLS_CIPHER_DES_EDE3_CBC, /**< DES cipher with EDE3 CBC mode. \warning 3DES is considered weak. */
Rose Zadik02f73a62018-03-26 18:02:32 +0100137 MBEDTLS_CIPHER_BLOWFISH_ECB, /**< Blowfish cipher with ECB mode. */
138 MBEDTLS_CIPHER_BLOWFISH_CBC, /**< Blowfish cipher with CBC mode. */
139 MBEDTLS_CIPHER_BLOWFISH_CFB64, /**< Blowfish cipher with CFB64 mode. */
140 MBEDTLS_CIPHER_BLOWFISH_CTR, /**< Blowfish cipher with CTR mode. */
Rose Zadik826f2642018-04-19 14:01:29 +0100141 MBEDTLS_CIPHER_ARC4_128, /**< RC4 cipher with 128-bit mode. */
Rose Zadik02f73a62018-03-26 18:02:32 +0100142 MBEDTLS_CIPHER_AES_128_CCM, /**< AES cipher with 128-bit CCM mode. */
143 MBEDTLS_CIPHER_AES_192_CCM, /**< AES cipher with 192-bit CCM mode. */
144 MBEDTLS_CIPHER_AES_256_CCM, /**< AES cipher with 256-bit CCM mode. */
145 MBEDTLS_CIPHER_CAMELLIA_128_CCM, /**< Camellia cipher with 128-bit CCM mode. */
146 MBEDTLS_CIPHER_CAMELLIA_192_CCM, /**< Camellia cipher with 192-bit CCM mode. */
147 MBEDTLS_CIPHER_CAMELLIA_256_CCM, /**< Camellia cipher with 256-bit CCM mode. */
Manuel Pégourié-Gonnarda3712be2018-05-22 15:58:50 +0200148 MBEDTLS_CIPHER_ARIA_128_ECB, /**< Aria cipher with 128-bit key and ECB mode. */
149 MBEDTLS_CIPHER_ARIA_192_ECB, /**< Aria cipher with 192-bit key and ECB mode. */
150 MBEDTLS_CIPHER_ARIA_256_ECB, /**< Aria cipher with 256-bit key and ECB mode. */
151 MBEDTLS_CIPHER_ARIA_128_CBC, /**< Aria cipher with 128-bit key and CBC mode. */
152 MBEDTLS_CIPHER_ARIA_192_CBC, /**< Aria cipher with 192-bit key and CBC mode. */
153 MBEDTLS_CIPHER_ARIA_256_CBC, /**< Aria cipher with 256-bit key and CBC mode. */
154 MBEDTLS_CIPHER_ARIA_128_CFB128, /**< Aria cipher with 128-bit key and CFB-128 mode. */
155 MBEDTLS_CIPHER_ARIA_192_CFB128, /**< Aria cipher with 192-bit key and CFB-128 mode. */
156 MBEDTLS_CIPHER_ARIA_256_CFB128, /**< Aria cipher with 256-bit key and CFB-128 mode. */
157 MBEDTLS_CIPHER_ARIA_128_CTR, /**< Aria cipher with 128-bit key and CTR mode. */
158 MBEDTLS_CIPHER_ARIA_192_CTR, /**< Aria cipher with 192-bit key and CTR mode. */
159 MBEDTLS_CIPHER_ARIA_256_CTR, /**< Aria cipher with 256-bit key and CTR mode. */
160 MBEDTLS_CIPHER_ARIA_128_GCM, /**< Aria cipher with 128-bit key and GCM mode. */
161 MBEDTLS_CIPHER_ARIA_192_GCM, /**< Aria cipher with 192-bit key and GCM mode. */
162 MBEDTLS_CIPHER_ARIA_256_GCM, /**< Aria cipher with 256-bit key and GCM mode. */
163 MBEDTLS_CIPHER_ARIA_128_CCM, /**< Aria cipher with 128-bit key and CCM mode. */
164 MBEDTLS_CIPHER_ARIA_192_CCM, /**< Aria cipher with 192-bit key and CCM mode. */
165 MBEDTLS_CIPHER_ARIA_256_CCM, /**< Aria cipher with 256-bit key and CCM mode. */
Simon Butcher6873c842018-04-29 13:03:20 +0100166 MBEDTLS_CIPHER_AES_128_OFB, /**< AES 128-bit cipher in OFB mode. */
167 MBEDTLS_CIPHER_AES_192_OFB, /**< AES 192-bit cipher in OFB mode. */
168 MBEDTLS_CIPHER_AES_256_OFB, /**< AES 256-bit cipher in OFB mode. */
Jaeden Ameroc6539902018-04-30 17:17:41 +0100169 MBEDTLS_CIPHER_AES_128_XTS, /**< AES 128-bit cipher in XTS block mode. */
170 MBEDTLS_CIPHER_AES_256_XTS, /**< AES 256-bit cipher in XTS block mode. */
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200171 MBEDTLS_CIPHER_CHACHA20, /**< ChaCha20 stream cipher. */
172 MBEDTLS_CIPHER_CHACHA20_POLY1305, /**< ChaCha20-Poly1305 AEAD cipher. */
Jack Lloydffdf2882019-03-07 17:00:32 -0500173 MBEDTLS_CIPHER_AES_128_KW, /**< AES cipher with 128-bit NIST KW mode. */
174 MBEDTLS_CIPHER_AES_192_KW, /**< AES cipher with 192-bit NIST KW mode. */
175 MBEDTLS_CIPHER_AES_256_KW, /**< AES cipher with 256-bit NIST KW mode. */
176 MBEDTLS_CIPHER_AES_128_KWP, /**< AES cipher with 128-bit NIST KWP mode. */
177 MBEDTLS_CIPHER_AES_192_KWP, /**< AES cipher with 192-bit NIST KWP mode. */
178 MBEDTLS_CIPHER_AES_256_KWP, /**< AES cipher with 256-bit NIST KWP mode. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200179} mbedtls_cipher_type_t;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000180
Rose Zadik9ba6b622018-01-24 12:59:19 +0000181/** Supported cipher modes. */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000182typedef enum {
Hanno Beckere5a04502018-11-08 16:57:42 +0000183 MBEDTLS_MODE_NONE = 0, /**< None. */
184 MBEDTLS_MODE_ECB, /**< The ECB cipher mode. */
185 MBEDTLS_MODE_CBC, /**< The CBC cipher mode. */
186 MBEDTLS_MODE_CFB, /**< The CFB cipher mode. */
187 MBEDTLS_MODE_OFB, /**< The OFB cipher mode. */
188 MBEDTLS_MODE_CTR, /**< The CTR cipher mode. */
189 MBEDTLS_MODE_GCM, /**< The GCM cipher mode. */
190 MBEDTLS_MODE_STREAM, /**< The stream cipher mode. */
191 MBEDTLS_MODE_CCM, /**< The CCM cipher mode. */
192 MBEDTLS_MODE_XTS, /**< The XTS cipher mode. */
Manuel Pégourié-Gonnardf57bf8b2018-06-18 11:14:09 +0200193 MBEDTLS_MODE_CHACHAPOLY, /**< The ChaCha-Poly cipher mode. */
Jack Lloydffdf2882019-03-07 17:00:32 -0500194 MBEDTLS_MODE_KW, /**< The SP800-38F KW mode */
195 MBEDTLS_MODE_KWP, /**< The SP800-38F KWP mode */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200196} mbedtls_cipher_mode_t;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000197
Rose Zadik9ba6b622018-01-24 12:59:19 +0000198/** Supported cipher padding types. */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000199typedef enum {
Rose Zadik9ba6b622018-01-24 12:59:19 +0000200 MBEDTLS_PADDING_PKCS7 = 0, /**< PKCS7 padding (default). */
201 MBEDTLS_PADDING_ONE_AND_ZEROS, /**< ISO/IEC 7816-4 padding. */
202 MBEDTLS_PADDING_ZEROS_AND_LEN, /**< ANSI X.923 padding. */
Rose Zadik02f73a62018-03-26 18:02:32 +0100203 MBEDTLS_PADDING_ZEROS, /**< Zero padding (not reversible). */
204 MBEDTLS_PADDING_NONE, /**< Never pad (full blocks only). */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200205} mbedtls_cipher_padding_t;
Manuel Pégourié-Gonnardd5fdcaf2013-07-24 18:05:00 +0200206
Rose Zadik9ba6b622018-01-24 12:59:19 +0000207/** Type of operation. */
Manuel Pégourié-Gonnardd5fdcaf2013-07-24 18:05:00 +0200208typedef enum {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200209 MBEDTLS_OPERATION_NONE = -1,
210 MBEDTLS_DECRYPT = 0,
211 MBEDTLS_ENCRYPT,
212} mbedtls_operation_t;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000213
214enum {
Rose Zadik9ba6b622018-01-24 12:59:19 +0000215 /** Undefined key length. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200216 MBEDTLS_KEY_LENGTH_NONE = 0,
Dave Rodgmanb6c5d042023-02-02 13:42:38 +0000217 /** Key length, in bits (including parity), for DES keys. \warning DES is considered weak. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200218 MBEDTLS_KEY_LENGTH_DES = 64,
Dave Rodgmanb6c5d042023-02-02 13:42:38 +0000219 /** Key length in bits, including parity, for DES in two-key EDE. \warning 3DES is considered weak. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200220 MBEDTLS_KEY_LENGTH_DES_EDE = 128,
Dave Rodgmanb6c5d042023-02-02 13:42:38 +0000221 /** Key length in bits, including parity, for DES in three-key EDE. \warning 3DES is considered weak. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200222 MBEDTLS_KEY_LENGTH_DES_EDE3 = 192,
Paul Bakker8123e9d2011-01-06 15:37:30 +0000223};
224
Rose Zadik9ba6b622018-01-24 12:59:19 +0000225/** Maximum length of any IV, in Bytes. */
Hanno Becker15889832020-09-08 11:29:11 +0100226/* This should ideally be derived automatically from list of ciphers.
227 * This should be kept in sync with MBEDTLS_SSL_MAX_IV_LENGTH defined
228 * in ssl_internal.h. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200229#define MBEDTLS_MAX_IV_LENGTH 16
Hanno Becker27a26882020-08-07 11:30:05 +0100230
Rose Zadik9ba6b622018-01-24 12:59:19 +0000231/** Maximum block size of any cipher, in Bytes. */
Hanno Becker15889832020-09-08 11:29:11 +0100232/* This should ideally be derived automatically from list of ciphers.
233 * This should be kept in sync with MBEDTLS_SSL_MAX_BLOCK_LENGTH defined
234 * in ssl_internal.h. */
Manuel Pégourié-Gonnard32902e62018-05-10 12:30:19 +0200235#define MBEDTLS_MAX_BLOCK_LENGTH 16
Manuel Pégourié-Gonnard0b58c152013-10-24 17:17:54 +0200236
Hanno Becker27a26882020-08-07 11:30:05 +0100237/** Maximum key length, in Bytes. */
238/* This should ideally be derived automatically from list of ciphers.
239 * For now, only check whether XTS is enabled which uses 64 Byte keys,
Hanno Becker15889832020-09-08 11:29:11 +0100240 * and use 32 Bytes as an upper bound for the maximum key length otherwise.
241 * This should be kept in sync with MBEDTLS_SSL_MAX_BLOCK_LENGTH defined
242 * in ssl_internal.h, which however deliberately ignores the case of XTS
243 * since the latter isn't used in SSL/TLS. */
Hanno Becker27a26882020-08-07 11:30:05 +0100244#if defined(MBEDTLS_CIPHER_MODE_XTS)
245#define MBEDTLS_MAX_KEY_LENGTH 64
246#else
247#define MBEDTLS_MAX_KEY_LENGTH 32
248#endif /* MBEDTLS_CIPHER_MODE_XTS */
249
Paul Bakker8123e9d2011-01-06 15:37:30 +0000250/**
Manuel Pégourié-Gonnard5a74e8b2015-05-06 17:10:55 +0100251 * Base cipher information (opaque struct).
Paul Bakker343a8702011-06-09 14:27:58 +0000252 */
Manuel Pégourié-Gonnard5a74e8b2015-05-06 17:10:55 +0100253typedef struct mbedtls_cipher_base_t mbedtls_cipher_base_t;
Paul Bakker343a8702011-06-09 14:27:58 +0000254
255/**
Simon Butcher327398a2016-10-05 14:09:11 +0100256 * CMAC context (opaque struct).
257 */
258typedef struct mbedtls_cmac_context_t mbedtls_cmac_context_t;
259
260/**
Rose Zadik9ba6b622018-01-24 12:59:19 +0000261 * Cipher information. Allows calling cipher functions
262 * in a generic way.
Paul Bakker8123e9d2011-01-06 15:37:30 +0000263 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100264typedef struct mbedtls_cipher_info_t {
Rose Zadik9ba6b622018-01-24 12:59:19 +0000265 /** Full cipher identifier. For example,
266 * MBEDTLS_CIPHER_AES_256_CBC.
267 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200268 mbedtls_cipher_type_t type;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000269
Rose Zadik9ba6b622018-01-24 12:59:19 +0000270 /** The cipher mode. For example, MBEDTLS_MODE_CBC. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200271 mbedtls_cipher_mode_t mode;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000272
Rose Zadik9ba6b622018-01-24 12:59:19 +0000273 /** The cipher key length, in bits. This is the
274 * default length for variable sized ciphers.
275 * Includes parity bits for ciphers like DES.
276 */
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200277 unsigned int key_bitlen;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000278
Rose Zadik9ba6b622018-01-24 12:59:19 +0000279 /** Name of the cipher. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100280 const char *name;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000281
Rose Zadik9ba6b622018-01-24 12:59:19 +0000282 /** IV or nonce size, in Bytes.
283 * For ciphers that accept variable IV sizes,
284 * this is the recommended size.
285 */
Paul Bakker23986e52011-04-24 08:57:21 +0000286 unsigned int iv_size;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000287
Rose Zadikb5607bf2018-04-16 10:34:51 +0100288 /** Bitflag comprised of MBEDTLS_CIPHER_VARIABLE_IV_LEN and
289 * MBEDTLS_CIPHER_VARIABLE_KEY_LEN indicating whether the
290 * cipher supports variable IV or variable key sizes, respectively.
291 */
Manuel Pégourié-Gonnard81754a02014-06-23 11:33:18 +0200292 int flags;
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200293
Rose Zadik9ba6b622018-01-24 12:59:19 +0000294 /** The block size, in Bytes. */
Paul Bakker23986e52011-04-24 08:57:21 +0000295 unsigned int block_size;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000296
Rose Zadik9ba6b622018-01-24 12:59:19 +0000297 /** Struct for base cipher information and functions. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200298 const mbedtls_cipher_base_t *base;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000299
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200300} mbedtls_cipher_info_t;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000301
302/**
Paul Bakker20281562011-11-11 10:34:04 +0000303 * Generic cipher context.
Paul Bakker8123e9d2011-01-06 15:37:30 +0000304 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100305typedef struct mbedtls_cipher_context_t {
Rose Zadik9ba6b622018-01-24 12:59:19 +0000306 /** Information about the associated cipher. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200307 const mbedtls_cipher_info_t *cipher_info;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000308
Rose Zadik9ba6b622018-01-24 12:59:19 +0000309 /** Key length to use. */
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200310 int key_bitlen;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000311
Rose Zadik9ba6b622018-01-24 12:59:19 +0000312 /** Operation that the key of the context has been
313 * initialized for.
314 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200315 mbedtls_operation_t operation;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000316
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200317#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Rose Zadik9ba6b622018-01-24 12:59:19 +0000318 /** Padding functions to use, if relevant for
319 * the specific cipher mode.
320 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100321 void (*add_padding)(unsigned char *output, size_t olen, size_t data_len);
322 int (*get_padding)(unsigned char *input, size_t ilen, size_t *data_len);
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +0100323#endif
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200324
Rose Zadik9ba6b622018-01-24 12:59:19 +0000325 /** Buffer for input that has not been processed yet. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200326 unsigned char unprocessed_data[MBEDTLS_MAX_BLOCK_LENGTH];
Paul Bakker8123e9d2011-01-06 15:37:30 +0000327
Rose Zadik9ba6b622018-01-24 12:59:19 +0000328 /** Number of Bytes that have not been processed yet. */
Paul Bakker23986e52011-04-24 08:57:21 +0000329 size_t unprocessed_len;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000330
Jaeden Ameroc6539902018-04-30 17:17:41 +0100331 /** Current IV or NONCE_COUNTER for CTR-mode, data unit (or sector) number
332 * for XTS-mode. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200333 unsigned char iv[MBEDTLS_MAX_IV_LENGTH];
Paul Bakker8123e9d2011-01-06 15:37:30 +0000334
Rose Zadik9ba6b622018-01-24 12:59:19 +0000335 /** IV size in Bytes, for ciphers with variable-length IVs. */
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200336 size_t iv_size;
337
Rose Zadik9ba6b622018-01-24 12:59:19 +0000338 /** The cipher-specific context. */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000339 void *cipher_ctx;
Simon Butcher327398a2016-10-05 14:09:11 +0100340
341#if defined(MBEDTLS_CMAC_C)
Rose Zadik9ba6b622018-01-24 12:59:19 +0000342 /** CMAC-specific context. */
Simon Butcher327398a2016-10-05 14:09:11 +0100343 mbedtls_cmac_context_t *cmac_ctx;
344#endif
Hanno Becker1cb36532018-11-09 16:20:29 +0000345
346#if defined(MBEDTLS_USE_PSA_CRYPTO)
347 /** Indicates whether the cipher operations should be performed
348 * by Mbed TLS' own crypto library or an external implementation
349 * of the PSA Crypto API.
Hanno Becker7909c4c2018-11-20 11:34:34 +0000350 * This is unset if the cipher context was established through
351 * mbedtls_cipher_setup(), and set if it was established through
Hanno Becker1cb36532018-11-09 16:20:29 +0000352 * mbedtls_cipher_setup_psa().
353 */
354 unsigned char psa_enabled;
355#endif /* MBEDTLS_USE_PSA_CRYPTO */
356
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200357} mbedtls_cipher_context_t;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000358
Paul Bakker8123e9d2011-01-06 15:37:30 +0000359/**
Hanno Beckerd7503a72018-11-08 15:55:24 +0000360 * \brief This function retrieves the list of ciphers supported
361 * by the generic cipher module.
Paul Bakker72f62662011-01-16 21:27:44 +0000362 *
Hanno Beckerd7503a72018-11-08 15:55:24 +0000363 * For any cipher identifier in the returned list, you can
364 * obtain the corresponding generic cipher information structure
365 * via mbedtls_cipher_info_from_type(), which can then be used
366 * to prepare a cipher context via mbedtls_cipher_setup().
367 *
368 *
369 * \return A statically-allocated array of cipher identifiers
370 * of type cipher_type_t. The last entry is zero.
Paul Bakker72f62662011-01-16 21:27:44 +0000371 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100372const int *mbedtls_cipher_list(void);
Paul Bakker72f62662011-01-16 21:27:44 +0000373
374/**
Rose Zadik9ba6b622018-01-24 12:59:19 +0000375 * \brief This function retrieves the cipher-information
376 * structure associated with the given cipher name.
Paul Bakker8123e9d2011-01-06 15:37:30 +0000377 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500378 * \param cipher_name Name of the cipher to search for. This must not be
379 * \c NULL.
Paul Bakker8123e9d2011-01-06 15:37:30 +0000380 *
Rose Zadik9ba6b622018-01-24 12:59:19 +0000381 * \return The cipher information structure associated with the
Rose Zadik02f73a62018-03-26 18:02:32 +0100382 * given \p cipher_name.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500383 * \return \c NULL if the associated cipher information is not found.
Paul Bakker8123e9d2011-01-06 15:37:30 +0000384 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100385const mbedtls_cipher_info_t *mbedtls_cipher_info_from_string(const char *cipher_name);
Paul Bakker8123e9d2011-01-06 15:37:30 +0000386
387/**
Rose Zadik9ba6b622018-01-24 12:59:19 +0000388 * \brief This function retrieves the cipher-information
389 * structure associated with the given cipher type.
Paul Bakker8123e9d2011-01-06 15:37:30 +0000390 *
391 * \param cipher_type Type of the cipher to search for.
392 *
Rose Zadik9ba6b622018-01-24 12:59:19 +0000393 * \return The cipher information structure associated with the
Rose Zadikb5607bf2018-04-16 10:34:51 +0100394 * given \p cipher_type.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500395 * \return \c NULL if the associated cipher information is not found.
Paul Bakker8123e9d2011-01-06 15:37:30 +0000396 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100397const mbedtls_cipher_info_t *mbedtls_cipher_info_from_type(const mbedtls_cipher_type_t cipher_type);
Paul Bakker8123e9d2011-01-06 15:37:30 +0000398
399/**
Rose Zadik9ba6b622018-01-24 12:59:19 +0000400 * \brief This function retrieves the cipher-information
401 * structure associated with the given cipher ID,
402 * key size and mode.
Paul Bakkerf46b6952013-09-09 00:08:26 +0200403 *
Rose Zadik9ba6b622018-01-24 12:59:19 +0000404 * \param cipher_id The ID of the cipher to search for. For example,
405 * #MBEDTLS_CIPHER_ID_AES.
406 * \param key_bitlen The length of the key in bits.
407 * \param mode The cipher mode. For example, #MBEDTLS_MODE_CBC.
Paul Bakkerf46b6952013-09-09 00:08:26 +0200408 *
Rose Zadik9ba6b622018-01-24 12:59:19 +0000409 * \return The cipher information structure associated with the
Rose Zadik02f73a62018-03-26 18:02:32 +0100410 * given \p cipher_id.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500411 * \return \c NULL if the associated cipher information is not found.
Paul Bakkerf46b6952013-09-09 00:08:26 +0200412 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100413const mbedtls_cipher_info_t *mbedtls_cipher_info_from_values(const mbedtls_cipher_id_t cipher_id,
414 int key_bitlen,
415 const mbedtls_cipher_mode_t mode);
Paul Bakkerf46b6952013-09-09 00:08:26 +0200416
417/**
Andrzej Kurek96ce1b02023-07-14 05:22:42 -0400418 * \brief This function initializes a \p ctx as NONE.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500419 *
420 * \param ctx The context to be initialized. This must not be \c NULL.
Paul Bakker84bbeb52014-07-01 14:53:22 +0200421 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100422void mbedtls_cipher_init(mbedtls_cipher_context_t *ctx);
Paul Bakker84bbeb52014-07-01 14:53:22 +0200423
424/**
Rose Zadik9ba6b622018-01-24 12:59:19 +0000425 * \brief This function frees and clears the cipher-specific
426 * context of \p ctx. Freeing \p ctx itself remains the
427 * responsibility of the caller.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500428 *
429 * \param ctx The context to be freed. If this is \c NULL, the
430 * function has no effect, otherwise this must point to an
431 * initialized context.
Paul Bakker84bbeb52014-07-01 14:53:22 +0200432 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100433void mbedtls_cipher_free(mbedtls_cipher_context_t *ctx);
Paul Bakker84bbeb52014-07-01 14:53:22 +0200434
Rose Zadik9ba6b622018-01-24 12:59:19 +0000435
Paul Bakker84bbeb52014-07-01 14:53:22 +0200436/**
Manuel Pégourié-Gonnard36979542021-05-27 12:59:11 +0200437 * \brief This function prepares a cipher context for
Hanno Beckerb1f08722018-11-09 16:09:19 +0000438 * use with the given cipher primitive.
Paul Bakker8123e9d2011-01-06 15:37:30 +0000439 *
Waleed Elmelegy8ce42eb2023-09-25 14:21:49 +0100440 * \warning In CBC mode, if mbedtls_cipher_set_padding_mode() is not called:
441 * - If MBEDTLS_CIPHER_PADDING_PKCS7 is enabled, the
442 * context will use PKCS7 padding.
443 * - Otherwise the context uses no padding and the input
444 * must be a whole number of blocks.
445 *
Manuel Pégourié-Gonnard8013e682021-05-31 11:13:35 +0200446 * \note After calling this function, you should call
447 * mbedtls_cipher_setkey() and, if the mode uses padding,
448 * mbedtls_cipher_set_padding_mode(), then for each
449 * message to encrypt or decrypt with this key, either:
450 * - mbedtls_cipher_crypt() for one-shot processing with
451 * non-AEAD modes;
452 * - mbedtls_cipher_auth_encrypt_ext() or
453 * mbedtls_cipher_auth_decrypt_ext() for one-shot
454 * processing with AEAD modes or NIST_KW;
455 * - for multi-part processing, see the documentation of
456 * mbedtls_cipher_reset().
457 *
Manuel Pégourié-Gonnard36979542021-05-27 12:59:11 +0200458 * \param ctx The context to prepare. This must be initialized by
459 * a call to mbedtls_cipher_init() first.
Rose Zadik9ba6b622018-01-24 12:59:19 +0000460 * \param cipher_info The cipher to use.
Paul Bakker84bbeb52014-07-01 14:53:22 +0200461 *
Rose Zadik02f73a62018-03-26 18:02:32 +0100462 * \return \c 0 on success.
Rose Zadik4c368e82018-04-19 14:24:11 +0100463 * \return #MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA on
464 * parameter-verification failure.
Rose Zadik02f73a62018-03-26 18:02:32 +0100465 * \return #MBEDTLS_ERR_CIPHER_ALLOC_FAILED if allocation of the
Rose Zadik4c368e82018-04-19 14:24:11 +0100466 * cipher-specific context fails.
Waleed Elmelegy916ed7b2023-09-25 15:18:48 +0100467 *
468 * \internal Currently, the function also clears the structure.
469 * In future versions, the caller will be required to call
470 * mbedtls_cipher_init() on the structure first.
Paul Bakker8123e9d2011-01-06 15:37:30 +0000471 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100472int mbedtls_cipher_setup(mbedtls_cipher_context_t *ctx,
473 const mbedtls_cipher_info_t *cipher_info);
Paul Bakker8123e9d2011-01-06 15:37:30 +0000474
Hanno Becker098c9de2018-11-09 16:10:57 +0000475#if defined(MBEDTLS_USE_PSA_CRYPTO)
476/**
477 * \brief This function initializes a cipher context for
478 * PSA-based use with the given cipher primitive.
479 *
480 * \note See #MBEDTLS_USE_PSA_CRYPTO for information on PSA.
481 *
482 * \param ctx The context to initialize. May not be \c NULL.
483 * \param cipher_info The cipher to use.
Hanno Beckerf1336402018-11-12 16:26:27 +0000484 * \param taglen For AEAD ciphers, the length in bytes of the
485 * authentication tag to use. Subsequent uses of
486 * mbedtls_cipher_auth_encrypt() or
487 * mbedtls_cipher_auth_decrypt() must provide
488 * the same tag length.
489 * For non-AEAD ciphers, the value must be \c 0.
Hanno Becker098c9de2018-11-09 16:10:57 +0000490 *
491 * \return \c 0 on success.
492 * \return #MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA on
493 * parameter-verification failure.
494 * \return #MBEDTLS_ERR_CIPHER_ALLOC_FAILED if allocation of the
495 * cipher-specific context fails.
496 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100497int mbedtls_cipher_setup_psa(mbedtls_cipher_context_t *ctx,
498 const mbedtls_cipher_info_t *cipher_info,
499 size_t taglen);
Hanno Becker098c9de2018-11-09 16:10:57 +0000500#endif /* MBEDTLS_USE_PSA_CRYPTO */
501
Paul Bakker8123e9d2011-01-06 15:37:30 +0000502/**
Rose Zadik9ba6b622018-01-24 12:59:19 +0000503 * \brief This function returns the block size of the given cipher.
Paul Bakker8123e9d2011-01-06 15:37:30 +0000504 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500505 * \param ctx The context of the cipher. This must be initialized.
Paul Bakker8123e9d2011-01-06 15:37:30 +0000506 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500507 * \return The block size of the underlying cipher.
508 * \return \c 0 if \p ctx has not been initialized.
Paul Bakker8123e9d2011-01-06 15:37:30 +0000509 */
Hanno Beckerf4fb8762018-11-08 15:57:42 +0000510static inline unsigned int mbedtls_cipher_get_block_size(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100511 const mbedtls_cipher_context_t *ctx)
Paul Bakker8123e9d2011-01-06 15:37:30 +0000512{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100513 MBEDTLS_INTERNAL_VALIDATE_RET(ctx != NULL, 0);
514 if (ctx->cipher_info == NULL) {
Paul Bakker8123e9d2011-01-06 15:37:30 +0000515 return 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100516 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000517
518 return ctx->cipher_info->block_size;
519}
520
521/**
Rose Zadik9ba6b622018-01-24 12:59:19 +0000522 * \brief This function returns the mode of operation for
523 * the cipher. For example, MBEDTLS_MODE_CBC.
Paul Bakkerf7e5bb52011-11-11 10:53:37 +0000524 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500525 * \param ctx The context of the cipher. This must be initialized.
Paul Bakkerf7e5bb52011-11-11 10:53:37 +0000526 *
Rose Zadik02f73a62018-03-26 18:02:32 +0100527 * \return The mode of operation.
528 * \return #MBEDTLS_MODE_NONE if \p ctx has not been initialized.
Paul Bakkerf7e5bb52011-11-11 10:53:37 +0000529 */
Hanno Beckerf4fb8762018-11-08 15:57:42 +0000530static inline mbedtls_cipher_mode_t mbedtls_cipher_get_cipher_mode(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100531 const mbedtls_cipher_context_t *ctx)
Paul Bakkerf7e5bb52011-11-11 10:53:37 +0000532{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100533 MBEDTLS_INTERNAL_VALIDATE_RET(ctx != NULL, MBEDTLS_MODE_NONE);
534 if (ctx->cipher_info == NULL) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200535 return MBEDTLS_MODE_NONE;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100536 }
Paul Bakkerf7e5bb52011-11-11 10:53:37 +0000537
538 return ctx->cipher_info->mode;
539}
540
541/**
Rose Zadik9ba6b622018-01-24 12:59:19 +0000542 * \brief This function returns the size of the IV or nonce
543 * of the cipher, in Bytes.
Paul Bakker8123e9d2011-01-06 15:37:30 +0000544 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500545 * \param ctx The context of the cipher. This must be initialized.
Paul Bakker8123e9d2011-01-06 15:37:30 +0000546 *
Rose Zadikb5607bf2018-04-16 10:34:51 +0100547 * \return The recommended IV size if no IV has been set.
Rose Zadik4c368e82018-04-19 14:24:11 +0100548 * \return \c 0 for ciphers not using an IV or a nonce.
Rose Zadikb5607bf2018-04-16 10:34:51 +0100549 * \return The actual size if an IV has been set.
Paul Bakker8123e9d2011-01-06 15:37:30 +0000550 */
Hanno Beckerf4fb8762018-11-08 15:57:42 +0000551static inline int mbedtls_cipher_get_iv_size(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100552 const mbedtls_cipher_context_t *ctx)
Paul Bakker8123e9d2011-01-06 15:37:30 +0000553{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100554 MBEDTLS_INTERNAL_VALIDATE_RET(ctx != NULL, 0);
555 if (ctx->cipher_info == NULL) {
Paul Bakker8123e9d2011-01-06 15:37:30 +0000556 return 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100557 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000558
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100559 if (ctx->iv_size != 0) {
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200560 return (int) ctx->iv_size;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100561 }
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200562
Manuel Pégourié-Gonnard46c4fa12015-08-12 09:27:55 +0200563 return (int) ctx->cipher_info->iv_size;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000564}
565
566/**
Rose Zadik9ba6b622018-01-24 12:59:19 +0000567 * \brief This function returns the type of the given cipher.
Paul Bakker8123e9d2011-01-06 15:37:30 +0000568 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500569 * \param ctx The context of the cipher. This must be initialized.
Paul Bakker8123e9d2011-01-06 15:37:30 +0000570 *
Rose Zadik02f73a62018-03-26 18:02:32 +0100571 * \return The type of the cipher.
572 * \return #MBEDTLS_CIPHER_NONE if \p ctx has not been initialized.
Paul Bakker8123e9d2011-01-06 15:37:30 +0000573 */
Hanno Beckerf4fb8762018-11-08 15:57:42 +0000574static inline mbedtls_cipher_type_t mbedtls_cipher_get_type(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100575 const mbedtls_cipher_context_t *ctx)
Paul Bakker8123e9d2011-01-06 15:37:30 +0000576{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500577 MBEDTLS_INTERNAL_VALIDATE_RET(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100578 ctx != NULL, MBEDTLS_CIPHER_NONE);
579 if (ctx->cipher_info == NULL) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200580 return MBEDTLS_CIPHER_NONE;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100581 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000582
583 return ctx->cipher_info->type;
584}
585
586/**
Rose Zadik9ba6b622018-01-24 12:59:19 +0000587 * \brief This function returns the name of the given cipher
588 * as a string.
Paul Bakker8123e9d2011-01-06 15:37:30 +0000589 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500590 * \param ctx The context of the cipher. This must be initialized.
Paul Bakker8123e9d2011-01-06 15:37:30 +0000591 *
Rose Zadik02f73a62018-03-26 18:02:32 +0100592 * \return The name of the cipher.
593 * \return NULL if \p ctx has not been not initialized.
Paul Bakker8123e9d2011-01-06 15:37:30 +0000594 */
Hanno Beckerf4fb8762018-11-08 15:57:42 +0000595static inline const char *mbedtls_cipher_get_name(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100596 const mbedtls_cipher_context_t *ctx)
Paul Bakker8123e9d2011-01-06 15:37:30 +0000597{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100598 MBEDTLS_INTERNAL_VALIDATE_RET(ctx != NULL, 0);
599 if (ctx->cipher_info == NULL) {
Paul Bakker8123e9d2011-01-06 15:37:30 +0000600 return 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100601 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000602
603 return ctx->cipher_info->name;
604}
605
606/**
Rose Zadik9ba6b622018-01-24 12:59:19 +0000607 * \brief This function returns the key length of the cipher.
Paul Bakker8123e9d2011-01-06 15:37:30 +0000608 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500609 * \param ctx The context of the cipher. This must be initialized.
Paul Bakker8123e9d2011-01-06 15:37:30 +0000610 *
Rose Zadik02f73a62018-03-26 18:02:32 +0100611 * \return The key length of the cipher in bits.
Andrzej Kurek96ce1b02023-07-14 05:22:42 -0400612 * \return #MBEDTLS_KEY_LENGTH_NONE if \p ctx has not been
Rose Zadik9ba6b622018-01-24 12:59:19 +0000613 * initialized.
Paul Bakker8123e9d2011-01-06 15:37:30 +0000614 */
Hanno Beckerf4fb8762018-11-08 15:57:42 +0000615static inline int mbedtls_cipher_get_key_bitlen(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100616 const mbedtls_cipher_context_t *ctx)
Paul Bakker8123e9d2011-01-06 15:37:30 +0000617{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500618 MBEDTLS_INTERNAL_VALIDATE_RET(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100619 ctx != NULL, MBEDTLS_KEY_LENGTH_NONE);
620 if (ctx->cipher_info == NULL) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200621 return MBEDTLS_KEY_LENGTH_NONE;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100622 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000623
Manuel Pégourié-Gonnard46c4fa12015-08-12 09:27:55 +0200624 return (int) ctx->cipher_info->key_bitlen;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000625}
626
627/**
Rose Zadik9ba6b622018-01-24 12:59:19 +0000628 * \brief This function returns the operation of the given cipher.
Paul Bakkerf7e5bb52011-11-11 10:53:37 +0000629 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500630 * \param ctx The context of the cipher. This must be initialized.
Paul Bakkerf7e5bb52011-11-11 10:53:37 +0000631 *
Rose Zadikb5607bf2018-04-16 10:34:51 +0100632 * \return The type of operation: #MBEDTLS_ENCRYPT or #MBEDTLS_DECRYPT.
633 * \return #MBEDTLS_OPERATION_NONE if \p ctx has not been initialized.
Paul Bakkerf7e5bb52011-11-11 10:53:37 +0000634 */
Hanno Beckerf4fb8762018-11-08 15:57:42 +0000635static inline mbedtls_operation_t mbedtls_cipher_get_operation(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100636 const mbedtls_cipher_context_t *ctx)
Paul Bakkerf7e5bb52011-11-11 10:53:37 +0000637{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500638 MBEDTLS_INTERNAL_VALIDATE_RET(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100639 ctx != NULL, MBEDTLS_OPERATION_NONE);
640 if (ctx->cipher_info == NULL) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200641 return MBEDTLS_OPERATION_NONE;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100642 }
Paul Bakkerf7e5bb52011-11-11 10:53:37 +0000643
644 return ctx->operation;
645}
646
647/**
Rose Zadik9ba6b622018-01-24 12:59:19 +0000648 * \brief This function sets the key to use with the given context.
Paul Bakker8123e9d2011-01-06 15:37:30 +0000649 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500650 * \param ctx The generic cipher context. This must be initialized and
651 * bound to a cipher information structure.
652 * \param key The key to use. This must be a readable buffer of at
653 * least \p key_bitlen Bits.
654 * \param key_bitlen The key length to use, in Bits.
Rose Zadik9ba6b622018-01-24 12:59:19 +0000655 * \param operation The operation that the key will be used for:
656 * #MBEDTLS_ENCRYPT or #MBEDTLS_DECRYPT.
Paul Bakker8123e9d2011-01-06 15:37:30 +0000657 *
Rose Zadikb5607bf2018-04-16 10:34:51 +0100658 * \return \c 0 on success.
Rose Zadik4c368e82018-04-19 14:24:11 +0100659 * \return #MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA on
660 * parameter-verification failure.
Rose Zadikb5607bf2018-04-16 10:34:51 +0100661 * \return A cipher-specific error code on failure.
Paul Bakker8123e9d2011-01-06 15:37:30 +0000662 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100663int mbedtls_cipher_setkey(mbedtls_cipher_context_t *ctx,
664 const unsigned char *key,
665 int key_bitlen,
666 const mbedtls_operation_t operation);
Paul Bakker8123e9d2011-01-06 15:37:30 +0000667
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200668#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Paul Bakker8123e9d2011-01-06 15:37:30 +0000669/**
Rose Zadik9ba6b622018-01-24 12:59:19 +0000670 * \brief This function sets the padding mode, for cipher modes
671 * that use padding.
Manuel Pégourié-Gonnardd5fdcaf2013-07-24 18:05:00 +0200672 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500673 * \param ctx The generic cipher context. This must be initialized and
674 * bound to a cipher information structure.
Rose Zadik9ba6b622018-01-24 12:59:19 +0000675 * \param mode The padding mode.
676 *
Rose Zadikb5607bf2018-04-16 10:34:51 +0100677 * \return \c 0 on success.
Rose Zadik02f73a62018-03-26 18:02:32 +0100678 * \return #MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE
679 * if the selected padding mode is not supported.
680 * \return #MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA if the cipher mode
Paul Bakker1a45d912013-08-14 12:04:26 +0200681 * does not support padding.
Manuel Pégourié-Gonnardd5fdcaf2013-07-24 18:05:00 +0200682 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100683int mbedtls_cipher_set_padding_mode(mbedtls_cipher_context_t *ctx,
684 mbedtls_cipher_padding_t mode);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200685#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
Manuel Pégourié-Gonnardd5fdcaf2013-07-24 18:05:00 +0200686
687/**
Rose Zadik9ba6b622018-01-24 12:59:19 +0000688 * \brief This function sets the initialization vector (IV)
689 * or nonce.
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200690 *
Rose Zadik02f73a62018-03-26 18:02:32 +0100691 * \note Some ciphers do not use IVs nor nonce. For these
692 * ciphers, this function has no effect.
693 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500694 * \param ctx The generic cipher context. This must be initialized and
695 * bound to a cipher information structure.
696 * \param iv The IV to use, or NONCE_COUNTER for CTR-mode ciphers. This
697 * must be a readable buffer of at least \p iv_len Bytes.
Rose Zadik9ba6b622018-01-24 12:59:19 +0000698 * \param iv_len The IV length for ciphers with variable-size IV.
699 * This parameter is discarded by ciphers with fixed-size IV.
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200700 *
Rose Zadikb5607bf2018-04-16 10:34:51 +0100701 * \return \c 0 on success.
Rose Zadik4c368e82018-04-19 14:24:11 +0100702 * \return #MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA on
703 * parameter-verification failure.
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200704 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100705int mbedtls_cipher_set_iv(mbedtls_cipher_context_t *ctx,
706 const unsigned char *iv,
707 size_t iv_len);
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200708
709/**
Rose Zadik9ba6b622018-01-24 12:59:19 +0000710 * \brief This function resets the cipher state.
Paul Bakker8123e9d2011-01-06 15:37:30 +0000711 *
Manuel Pégourié-Gonnard8013e682021-05-31 11:13:35 +0200712 * \note With non-AEAD ciphers, the order of calls for each message
713 * is as follows:
714 * 1. mbedtls_cipher_set_iv() if the mode uses an IV/nonce.
715 * 2. mbedtls_cipher_reset()
716 * 3. mbedtls_cipher_update() one or more times
717 * 4. mbedtls_cipher_finish()
718 * .
Manuel Pégourié-Gonnarde4138e32021-05-31 12:14:02 +0200719 * This sequence can be repeated to encrypt or decrypt multiple
Manuel Pégourié-Gonnard8013e682021-05-31 11:13:35 +0200720 * messages with the same key.
721 *
722 * \note With AEAD ciphers, the order of calls for each message
723 * is as follows:
724 * 1. mbedtls_cipher_set_iv() if the mode uses an IV/nonce.
725 * 2. mbedtls_cipher_reset()
726 * 3. mbedtls_cipher_update_ad()
727 * 4. mbedtls_cipher_update() one or more times
Waleed Elmelegy8ce42eb2023-09-25 14:21:49 +0100728 * 5. mbedtls_cipher_check_tag() (for decryption) or
Manuel Pégourié-Gonnard8013e682021-05-31 11:13:35 +0200729 * mbedtls_cipher_write_tag() (for encryption).
730 * .
Manuel Pégourié-Gonnarde4138e32021-05-31 12:14:02 +0200731 * This sequence can be repeated to encrypt or decrypt multiple
Manuel Pégourié-Gonnard8013e682021-05-31 11:13:35 +0200732 * messages with the same key.
733 *
734 * \param ctx The generic cipher context. This must be bound to a key.
Paul Bakker8123e9d2011-01-06 15:37:30 +0000735 *
Rose Zadikb5607bf2018-04-16 10:34:51 +0100736 * \return \c 0 on success.
Rose Zadik4c368e82018-04-19 14:24:11 +0100737 * \return #MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA on
738 * parameter-verification failure.
Paul Bakker8123e9d2011-01-06 15:37:30 +0000739 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100740int mbedtls_cipher_reset(mbedtls_cipher_context_t *ctx);
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200741
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200742#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200743/**
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500744 * \brief This function adds additional data for AEAD ciphers.
745 * Currently supported with GCM and ChaCha20+Poly1305.
746 * This must be called exactly once, after
747 * mbedtls_cipher_reset().
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200748 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500749 * \param ctx The generic cipher context. This must be initialized.
750 * \param ad The additional data to use. This must be a readable
751 * buffer of at least \p ad_len Bytes.
Andrzej Kurek02f39ac2019-02-08 06:50:55 -0500752 * \param ad_len The length of \p ad in Bytes.
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200753 *
Andrzej Kurek57f04e52019-02-08 06:44:43 -0500754 * \return \c 0 on success.
755 * \return A specific error code on failure.
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200756 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100757int mbedtls_cipher_update_ad(mbedtls_cipher_context_t *ctx,
758 const unsigned char *ad, size_t ad_len);
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200759#endif /* MBEDTLS_GCM_C || MBEDTLS_CHACHAPOLY_C */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000760
761/**
Rose Zadik9ba6b622018-01-24 12:59:19 +0000762 * \brief The generic cipher update function. It encrypts or
763 * decrypts using the given cipher context. Writes as
764 * many block-sized blocks of data as possible to output.
765 * Any data that cannot be written immediately is either
766 * added to the next block, or flushed when
767 * mbedtls_cipher_finish() is called.
768 * Exception: For MBEDTLS_MODE_ECB, expects a single block
769 * in size. For example, 16 Bytes for AES.
Paul Bakker8123e9d2011-01-06 15:37:30 +0000770 *
Rose Zadik4c368e82018-04-19 14:24:11 +0100771 * \note If the underlying cipher is used in GCM mode, all calls
772 * to this function, except for the last one before
773 * mbedtls_cipher_finish(), must have \p ilen as a
774 * multiple of the block size of the cipher.
Rose Zadik02f73a62018-03-26 18:02:32 +0100775 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500776 * \param ctx The generic cipher context. This must be initialized and
777 * bound to a key.
778 * \param input The buffer holding the input data. This must be a
779 * readable buffer of at least \p ilen Bytes.
Rose Zadik9ba6b622018-01-24 12:59:19 +0000780 * \param ilen The length of the input data.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500781 * \param output The buffer for the output data. This must be able to
782 * hold at least `ilen + block_size`. This must not be the
783 * same buffer as \p input.
Rose Zadik9ba6b622018-01-24 12:59:19 +0000784 * \param olen The length of the output data, to be updated with the
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500785 * actual number of Bytes written. This must not be
786 * \c NULL.
Paul Bakker8123e9d2011-01-06 15:37:30 +0000787 *
Rose Zadikb5607bf2018-04-16 10:34:51 +0100788 * \return \c 0 on success.
Rose Zadik4c368e82018-04-19 14:24:11 +0100789 * \return #MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA on
790 * parameter-verification failure.
Rose Zadik02f73a62018-03-26 18:02:32 +0100791 * \return #MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE on an
792 * unsupported mode for a cipher.
Rose Zadikb5607bf2018-04-16 10:34:51 +0100793 * \return A cipher-specific error code on failure.
Paul Bakker8123e9d2011-01-06 15:37:30 +0000794 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100795int mbedtls_cipher_update(mbedtls_cipher_context_t *ctx,
796 const unsigned char *input,
797 size_t ilen, unsigned char *output,
798 size_t *olen);
Paul Bakker8123e9d2011-01-06 15:37:30 +0000799
800/**
Rose Zadik9ba6b622018-01-24 12:59:19 +0000801 * \brief The generic cipher finalization function. If data still
802 * needs to be flushed from an incomplete block, the data
803 * contained in it is padded to the size of
804 * the last block, and written to the \p output buffer.
Paul Bakker8123e9d2011-01-06 15:37:30 +0000805 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500806 * \param ctx The generic cipher context. This must be initialized and
807 * bound to a key.
808 * \param output The buffer to write data to. This needs to be a writable
Andrzej Kurek532ec202023-05-06 09:57:40 -0400809 * buffer of at least block_size Bytes.
Rose Zadik9ba6b622018-01-24 12:59:19 +0000810 * \param olen The length of the data written to the \p output buffer.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500811 * This may not be \c NULL.
Paul Bakker8123e9d2011-01-06 15:37:30 +0000812 *
Rose Zadikb5607bf2018-04-16 10:34:51 +0100813 * \return \c 0 on success.
Rose Zadik4c368e82018-04-19 14:24:11 +0100814 * \return #MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA on
815 * parameter-verification failure.
816 * \return #MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED on decryption
817 * expecting a full block but not receiving one.
Rose Zadik02f73a62018-03-26 18:02:32 +0100818 * \return #MBEDTLS_ERR_CIPHER_INVALID_PADDING on invalid padding
819 * while decrypting.
Rose Zadikb5607bf2018-04-16 10:34:51 +0100820 * \return A cipher-specific error code on failure.
Paul Bakker8123e9d2011-01-06 15:37:30 +0000821 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100822int mbedtls_cipher_finish(mbedtls_cipher_context_t *ctx,
823 unsigned char *output, size_t *olen);
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200824
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200825#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200826/**
Rose Zadik9ba6b622018-01-24 12:59:19 +0000827 * \brief This function writes a tag for AEAD ciphers.
Daniel King8fe47012016-05-17 20:33:28 -0300828 * Currently supported with GCM and ChaCha20+Poly1305.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500829 * This must be called after mbedtls_cipher_finish().
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200830 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500831 * \param ctx The generic cipher context. This must be initialized,
832 * bound to a key, and have just completed a cipher
833 * operation through mbedtls_cipher_finish() the tag for
834 * which should be written.
835 * \param tag The buffer to write the tag to. This must be a writable
836 * buffer of at least \p tag_len Bytes.
Rose Zadik9ba6b622018-01-24 12:59:19 +0000837 * \param tag_len The length of the tag to write.
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200838 *
Rose Zadik02f73a62018-03-26 18:02:32 +0100839 * \return \c 0 on success.
840 * \return A specific error code on failure.
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200841 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100842int mbedtls_cipher_write_tag(mbedtls_cipher_context_t *ctx,
843 unsigned char *tag, size_t tag_len);
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200844
845/**
Rose Zadik9ba6b622018-01-24 12:59:19 +0000846 * \brief This function checks the tag for AEAD ciphers.
Daniel King8fe47012016-05-17 20:33:28 -0300847 * Currently supported with GCM and ChaCha20+Poly1305.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500848 * This must be called after mbedtls_cipher_finish().
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200849 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500850 * \param ctx The generic cipher context. This must be initialized.
851 * \param tag The buffer holding the tag. This must be a readable
852 * buffer of at least \p tag_len Bytes.
Rose Zadik9ba6b622018-01-24 12:59:19 +0000853 * \param tag_len The length of the tag to check.
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200854 *
Rose Zadik02f73a62018-03-26 18:02:32 +0100855 * \return \c 0 on success.
856 * \return A specific error code on failure.
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200857 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100858int mbedtls_cipher_check_tag(mbedtls_cipher_context_t *ctx,
859 const unsigned char *tag, size_t tag_len);
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200860#endif /* MBEDTLS_GCM_C || MBEDTLS_CHACHAPOLY_C */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000861
Paul Bakker8123e9d2011-01-06 15:37:30 +0000862/**
Rose Zadik9ba6b622018-01-24 12:59:19 +0000863 * \brief The generic all-in-one encryption/decryption function,
864 * for all ciphers except AEAD constructs.
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +0200865 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500866 * \param ctx The generic cipher context. This must be initialized.
Rose Zadik9ba6b622018-01-24 12:59:19 +0000867 * \param iv The IV to use, or NONCE_COUNTER for CTR-mode ciphers.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500868 * This must be a readable buffer of at least \p iv_len
869 * Bytes.
Rose Zadik9ba6b622018-01-24 12:59:19 +0000870 * \param iv_len The IV length for ciphers with variable-size IV.
871 * This parameter is discarded by ciphers with fixed-size
872 * IV.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500873 * \param input The buffer holding the input data. This must be a
874 * readable buffer of at least \p ilen Bytes.
875 * \param ilen The length of the input data in Bytes.
876 * \param output The buffer for the output data. This must be able to
877 * hold at least `ilen + block_size`. This must not be the
878 * same buffer as \p input.
Rose Zadik9ba6b622018-01-24 12:59:19 +0000879 * \param olen The length of the output data, to be updated with the
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500880 * actual number of Bytes written. This must not be
881 * \c NULL.
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +0200882 *
Rose Zadik9ba6b622018-01-24 12:59:19 +0000883 * \note Some ciphers do not use IVs nor nonce. For these
884 * ciphers, use \p iv = NULL and \p iv_len = 0.
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +0200885 *
Rose Zadikb5607bf2018-04-16 10:34:51 +0100886 * \return \c 0 on success.
Rose Zadik4c368e82018-04-19 14:24:11 +0100887 * \return #MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA on
888 * parameter-verification failure.
889 * \return #MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED on decryption
890 * expecting a full block but not receiving one.
Rose Zadik02f73a62018-03-26 18:02:32 +0100891 * \return #MBEDTLS_ERR_CIPHER_INVALID_PADDING on invalid padding
892 * while decrypting.
Rose Zadikb5607bf2018-04-16 10:34:51 +0100893 * \return A cipher-specific error code on failure.
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +0200894 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100895int mbedtls_cipher_crypt(mbedtls_cipher_context_t *ctx,
896 const unsigned char *iv, size_t iv_len,
897 const unsigned char *input, size_t ilen,
898 unsigned char *output, size_t *olen);
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +0200899
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200900#if defined(MBEDTLS_CIPHER_MODE_AEAD)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100901#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard513c2432020-12-01 10:34:57 +0100902#if defined(MBEDTLS_DEPRECATED_WARNING)
903#define MBEDTLS_DEPRECATED __attribute__((deprecated))
904#else
905#define MBEDTLS_DEPRECATED
906#endif /* MBEDTLS_DEPRECATED_WARNING */
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200907/**
Manuel Pégourié-Gonnardf2ffbc42020-12-01 09:57:55 +0100908 * \brief The generic authenticated encryption (AEAD) function.
909 *
Manuel Pégourié-Gonnard513c2432020-12-01 10:34:57 +0100910 * \deprecated Superseded by mbedtls_cipher_auth_encrypt_ext().
911 *
Manuel Pégourié-Gonnardf2ffbc42020-12-01 09:57:55 +0100912 * \note This function only supports AEAD algorithms, not key
913 * wrapping algorithms such as NIST_KW; for this, see
914 * mbedtls_cipher_auth_encrypt_ext().
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200915 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500916 * \param ctx The generic cipher context. This must be initialized and
Manuel Pégourié-Gonnardf2ffbc42020-12-01 09:57:55 +0100917 * bound to a key associated with an AEAD algorithm.
Manuel Pégourié-Gonnard3ba84d52020-11-20 10:17:20 +0100918 * \param iv The nonce to use. This must be a readable buffer of
919 * at least \p iv_len Bytes and must not be \c NULL.
920 * \param iv_len The length of the nonce. This must satisfy the
921 * constraints imposed by the AEAD cipher used.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500922 * \param ad The additional data to authenticate. This must be a
Manuel Pégourié-Gonnard3ba84d52020-11-20 10:17:20 +0100923 * readable buffer of at least \p ad_len Bytes, and may
924 * be \c NULL is \p ad_len is \c 0.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500925 * \param ad_len The length of \p ad.
926 * \param input The buffer holding the input data. This must be a
Manuel Pégourié-Gonnard3ba84d52020-11-20 10:17:20 +0100927 * readable buffer of at least \p ilen Bytes, and may be
928 * \c NULL if \p ilen is \c 0.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500929 * \param ilen The length of the input data.
Manuel Pégourié-Gonnard3ba84d52020-11-20 10:17:20 +0100930 * \param output The buffer for the output data. This must be a
931 * writable buffer of at least \p ilen Bytes, and must
932 * not be \c NULL.
933 * \param olen This will be filled with the actual number of Bytes
934 * written to the \p output buffer. This must point to a
935 * writable object of type \c size_t.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500936 * \param tag The buffer for the authentication tag. This must be a
Manuel Pégourié-Gonnard3ba84d52020-11-20 10:17:20 +0100937 * writable buffer of at least \p tag_len Bytes. See note
938 * below regarding restrictions with PSA-based contexts.
939 * \param tag_len The desired length of the authentication tag. This
940 * must match the constraints imposed by the AEAD cipher
Manuel Pégourié-Gonnardf2ffbc42020-12-01 09:57:55 +0100941 * used, and in particular must not be \c 0.
Manuel Pégourié-Gonnard3ba84d52020-11-20 10:17:20 +0100942 *
943 * \note If the context is based on PSA (that is, it was set up
944 * with mbedtls_cipher_setup_psa()), then it is required
945 * that \c tag == output + ilen. That is, the tag must be
946 * appended to the ciphertext as recommended by RFC 5116.
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200947 *
Andrzej Kurek246cc862019-02-05 04:40:53 -0500948 * \return \c 0 on success.
949 * \return #MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA on
950 * parameter-verification failure.
951 * \return A cipher-specific error code on failure.
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200952 */
David Horstmann9c021222022-12-13 17:56:12 +0000953int MBEDTLS_DEPRECATED mbedtls_cipher_auth_encrypt(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100954 mbedtls_cipher_context_t *ctx,
955 const unsigned char *iv, size_t iv_len,
956 const unsigned char *ad, size_t ad_len,
957 const unsigned char *input, size_t ilen,
958 unsigned char *output, size_t *olen,
959 unsigned char *tag, size_t tag_len);
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200960
961/**
Manuel Pégourié-Gonnardf2ffbc42020-12-01 09:57:55 +0100962 * \brief The generic authenticated decryption (AEAD) function.
963 *
Manuel Pégourié-Gonnard513c2432020-12-01 10:34:57 +0100964 * \deprecated Superseded by mbedtls_cipher_auth_decrypt_ext().
965 *
Manuel Pégourié-Gonnardf2ffbc42020-12-01 09:57:55 +0100966 * \note This function only supports AEAD algorithms, not key
967 * wrapping algorithms such as NIST_KW; for this, see
Manuel Pégourié-Gonnard513c2432020-12-01 10:34:57 +0100968 * mbedtls_cipher_auth_decrypt_ext().
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200969 *
Andrzej Kurek246cc862019-02-05 04:40:53 -0500970 * \note If the data is not authentic, then the output buffer
971 * is zeroed out to prevent the unauthentic plaintext being
972 * used, making this interface safer.
Rose Zadik02f73a62018-03-26 18:02:32 +0100973 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500974 * \param ctx The generic cipher context. This must be initialized and
Manuel Pégourié-Gonnardf2ffbc42020-12-01 09:57:55 +0100975 * bound to a key associated with an AEAD algorithm.
Manuel Pégourié-Gonnard3ba84d52020-11-20 10:17:20 +0100976 * \param iv The nonce to use. This must be a readable buffer of
977 * at least \p iv_len Bytes and must not be \c NULL.
978 * \param iv_len The length of the nonce. This must satisfy the
979 * constraints imposed by the AEAD cipher used.
980 * \param ad The additional data to authenticate. This must be a
981 * readable buffer of at least \p ad_len Bytes, and may
982 * be \c NULL is \p ad_len is \c 0.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500983 * \param ad_len The length of \p ad.
984 * \param input The buffer holding the input data. This must be a
Manuel Pégourié-Gonnard3ba84d52020-11-20 10:17:20 +0100985 * readable buffer of at least \p ilen Bytes, and may be
986 * \c NULL if \p ilen is \c 0.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500987 * \param ilen The length of the input data.
Manuel Pégourié-Gonnard3ba84d52020-11-20 10:17:20 +0100988 * \param output The buffer for the output data. This must be a
989 * writable buffer of at least \p ilen Bytes, and must
990 * not be \c NULL.
991 * \param olen This will be filled with the actual number of Bytes
992 * written to the \p output buffer. This must point to a
993 * writable object of type \c size_t.
994 * \param tag The buffer for the authentication tag. This must be a
995 * readable buffer of at least \p tag_len Bytes. See note
996 * below regarding restrictions with PSA-based contexts.
997 * \param tag_len The length of the authentication tag. This must match
998 * the constraints imposed by the AEAD cipher used, and in
999 * particular must not be \c 0.
1000 *
1001 * \note If the context is based on PSA (that is, it was set up
1002 * with mbedtls_cipher_setup_psa()), then it is required
1003 * that \c tag == input + len. That is, the tag must be
1004 * appended to the ciphertext as recommended by RFC 5116.
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001005 *
Andrzej Kurek246cc862019-02-05 04:40:53 -05001006 * \return \c 0 on success.
1007 * \return #MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA on
1008 * parameter-verification failure.
1009 * \return #MBEDTLS_ERR_CIPHER_AUTH_FAILED if data is not authentic.
1010 * \return A cipher-specific error code on failure.
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001011 */
David Horstmann9c021222022-12-13 17:56:12 +00001012int MBEDTLS_DEPRECATED mbedtls_cipher_auth_decrypt(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001013 mbedtls_cipher_context_t *ctx,
1014 const unsigned char *iv, size_t iv_len,
1015 const unsigned char *ad, size_t ad_len,
1016 const unsigned char *input, size_t ilen,
1017 unsigned char *output, size_t *olen,
1018 const unsigned char *tag, size_t tag_len);
Manuel Pégourié-Gonnard513c2432020-12-01 10:34:57 +01001019#undef MBEDTLS_DEPRECATED
1020#endif /* MBEDTLS_DEPRECATED_REMOVED */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001021#endif /* MBEDTLS_CIPHER_MODE_AEAD */
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001022
Manuel Pégourié-Gonnard9cc079d2020-11-25 12:57:47 +01001023#if defined(MBEDTLS_CIPHER_MODE_AEAD) || defined(MBEDTLS_NIST_KW_C)
1024/**
Manuel Pégourié-Gonnardf2ffbc42020-12-01 09:57:55 +01001025 * \brief The authenticated encryption (AEAD/NIST_KW) function.
Manuel Pégourié-Gonnard9cc079d2020-11-25 12:57:47 +01001026 *
1027 * \note For AEAD modes, the tag will be appended to the
1028 * ciphertext, as recommended by RFC 5116.
1029 * (NIST_KW doesn't have a separate tag.)
1030 *
1031 * \param ctx The generic cipher context. This must be initialized and
Manuel Pégourié-Gonnardf2ffbc42020-12-01 09:57:55 +01001032 * bound to a key, with an AEAD algorithm or NIST_KW.
Manuel Pégourié-Gonnard9cc079d2020-11-25 12:57:47 +01001033 * \param iv The nonce to use. This must be a readable buffer of
1034 * at least \p iv_len Bytes and may be \c NULL if \p
1035 * iv_len is \c 0.
Manuel Pégourié-Gonnardb23e31d2020-12-07 09:57:35 +01001036 * \param iv_len The length of the nonce. For AEAD ciphers, this must
1037 * satisfy the constraints imposed by the cipher used.
1038 * For NIST_KW, this must be \c 0.
Manuel Pégourié-Gonnard9cc079d2020-11-25 12:57:47 +01001039 * \param ad The additional data to authenticate. This must be a
1040 * readable buffer of at least \p ad_len Bytes, and may
1041 * be \c NULL is \p ad_len is \c 0.
1042 * \param ad_len The length of \p ad. For NIST_KW, this must be \c 0.
1043 * \param input The buffer holding the input data. This must be a
1044 * readable buffer of at least \p ilen Bytes, and may be
1045 * \c NULL if \p ilen is \c 0.
1046 * \param ilen The length of the input data.
1047 * \param output The buffer for the output data. This must be a
1048 * writable buffer of at least \p output_len Bytes, and
1049 * must not be \c NULL.
1050 * \param output_len The length of the \p output buffer in Bytes. For AEAD
1051 * ciphers, this must be at least \p ilen + \p tag_len.
1052 * For NIST_KW, this must be at least \p ilen + 8
1053 * (rounded up to a multiple of 8 if KWP is used);
1054 * \p ilen + 15 is always a safe value.
1055 * \param olen This will be filled with the actual number of Bytes
1056 * written to the \p output buffer. This must point to a
1057 * writable object of type \c size_t.
1058 * \param tag_len The desired length of the authentication tag. For AEAD
1059 * ciphers, this must match the constraints imposed by
Manuel Pégourié-Gonnardf2ffbc42020-12-01 09:57:55 +01001060 * the cipher used, and in particular must not be \c 0.
Manuel Pégourié-Gonnard9cc079d2020-11-25 12:57:47 +01001061 * For NIST_KW, this must be \c 0.
1062 *
1063 * \return \c 0 on success.
1064 * \return #MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA on
1065 * parameter-verification failure.
1066 * \return A cipher-specific error code on failure.
1067 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001068int mbedtls_cipher_auth_encrypt_ext(mbedtls_cipher_context_t *ctx,
1069 const unsigned char *iv, size_t iv_len,
1070 const unsigned char *ad, size_t ad_len,
1071 const unsigned char *input, size_t ilen,
1072 unsigned char *output, size_t output_len,
1073 size_t *olen, size_t tag_len);
Manuel Pégourié-Gonnard9cc079d2020-11-25 12:57:47 +01001074
1075/**
Manuel Pégourié-Gonnardf2ffbc42020-12-01 09:57:55 +01001076 * \brief The authenticated encryption (AEAD/NIST_KW) function.
Manuel Pégourié-Gonnard9cc079d2020-11-25 12:57:47 +01001077 *
1078 * \note If the data is not authentic, then the output buffer
1079 * is zeroed out to prevent the unauthentic plaintext being
1080 * used, making this interface safer.
1081 *
1082 * \note For AEAD modes, the tag must be appended to the
1083 * ciphertext, as recommended by RFC 5116.
1084 * (NIST_KW doesn't have a separate tag.)
1085 *
1086 * \param ctx The generic cipher context. This must be initialized and
Manuel Pégourié-Gonnardf2ffbc42020-12-01 09:57:55 +01001087 * bound to a key, with an AEAD algorithm or NIST_KW.
Manuel Pégourié-Gonnard9cc079d2020-11-25 12:57:47 +01001088 * \param iv The nonce to use. This must be a readable buffer of
1089 * at least \p iv_len Bytes and may be \c NULL if \p
1090 * iv_len is \c 0.
Manuel Pégourié-Gonnardb23e31d2020-12-07 09:57:35 +01001091 * \param iv_len The length of the nonce. For AEAD ciphers, this must
1092 * satisfy the constraints imposed by the cipher used.
1093 * For NIST_KW, this must be \c 0.
Manuel Pégourié-Gonnard9cc079d2020-11-25 12:57:47 +01001094 * \param ad The additional data to authenticate. This must be a
1095 * readable buffer of at least \p ad_len Bytes, and may
1096 * be \c NULL is \p ad_len is \c 0.
1097 * \param ad_len The length of \p ad. For NIST_KW, this must be \c 0.
1098 * \param input The buffer holding the input data. This must be a
1099 * readable buffer of at least \p ilen Bytes, and may be
1100 * \c NULL if \p ilen is \c 0.
1101 * \param ilen The length of the input data. For AEAD ciphers this
1102 * must be at least \p tag_len. For NIST_KW this must be
1103 * at least \c 8.
1104 * \param output The buffer for the output data. This must be a
1105 * writable buffer of at least \p output_len Bytes, and
1106 * may be \c NULL if \p output_len is \c 0.
1107 * \param output_len The length of the \p output buffer in Bytes. For AEAD
1108 * ciphers, this must be at least \p ilen - \p tag_len.
1109 * For NIST_KW, this must be at least \p ilen - 8.
1110 * \param olen This will be filled with the actual number of Bytes
1111 * written to the \p output buffer. This must point to a
1112 * writable object of type \c size_t.
1113 * \param tag_len The actual length of the authentication tag. For AEAD
1114 * ciphers, this must match the constraints imposed by
Manuel Pégourié-Gonnardf2ffbc42020-12-01 09:57:55 +01001115 * the cipher used, and in particular must not be \c 0.
Manuel Pégourié-Gonnard9cc079d2020-11-25 12:57:47 +01001116 * For NIST_KW, this must be \c 0.
1117 *
1118 * \return \c 0 on success.
1119 * \return #MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA on
1120 * parameter-verification failure.
1121 * \return #MBEDTLS_ERR_CIPHER_AUTH_FAILED if data is not authentic.
1122 * \return A cipher-specific error code on failure.
1123 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001124int mbedtls_cipher_auth_decrypt_ext(mbedtls_cipher_context_t *ctx,
1125 const unsigned char *iv, size_t iv_len,
1126 const unsigned char *ad, size_t ad_len,
1127 const unsigned char *input, size_t ilen,
1128 unsigned char *output, size_t output_len,
1129 size_t *olen, size_t tag_len);
Manuel Pégourié-Gonnard9cc079d2020-11-25 12:57:47 +01001130#endif /* MBEDTLS_CIPHER_MODE_AEAD || MBEDTLS_NIST_KW_C */
Paul Bakker8123e9d2011-01-06 15:37:30 +00001131#ifdef __cplusplus
1132}
1133#endif
1134
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001135#endif /* MBEDTLS_CIPHER_H */