blob: 7afdc6ac977447169f27a2ebf7d712972e621017 [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
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +020012 * SPDX-License-Identifier: Apache-2.0
13 *
14 * Licensed under the Apache License, Version 2.0 (the "License"); you may
15 * not use this file except in compliance with the License.
16 * You may obtain a copy of the License at
17 *
18 * http://www.apache.org/licenses/LICENSE-2.0
19 *
20 * Unless required by applicable law or agreed to in writing, software
21 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
22 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
23 * See the License for the specific language governing permissions and
24 * limitations under the License.
Paul Bakker8123e9d2011-01-06 15:37:30 +000025 */
26
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020027#ifndef MBEDTLS_CIPHER_H
28#define MBEDTLS_CIPHER_H
Mateusz Starzyk846f0212021-05-19 19:44:07 +020029#include "mbedtls/private_access.h"
Paul Bakker8123e9d2011-01-06 15:37:30 +000030
Bence Szépkútic662b362021-05-27 11:25:03 +020031#include "mbedtls/build_info.h"
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +020032
Rich Evans00ab4702015-02-06 13:43:58 +000033#include <stddef.h>
Jaeden Ameroc49fbbf2019-07-04 20:01:14 +010034#include "mbedtls/platform_util.h"
Rich Evans00ab4702015-02-06 13:43:58 +000035
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +020036#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020037#define MBEDTLS_CIPHER_MODE_AEAD
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +020038#endif
39
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020040#if defined(MBEDTLS_CIPHER_MODE_CBC)
41#define MBEDTLS_CIPHER_MODE_WITH_PADDING
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +020042#endif
43
TRodziewicz10e8cf52021-05-31 17:58:57 +020044#if defined(MBEDTLS_CIPHER_NULL_CIPHER) || \
Simon Butcherc7965732018-07-27 17:13:39 +010045 defined(MBEDTLS_CHACHA20_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020046#define MBEDTLS_CIPHER_MODE_STREAM
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +010047#endif
48
Manuel Pégourié-Gonnard0223ab92015-10-05 11:40:01 +010049#if ( defined(__ARMCC_VERSION) || defined(_MSC_VER) ) && \
50 !defined(inline) && !defined(__cplusplus)
Paul Bakker569df2c2011-06-21 07:48:07 +000051#define inline __inline
Manuel Pégourié-Gonnard20af64d2015-07-07 18:33:39 +020052#endif
Paul Bakkeraf5c85f2011-04-18 03:47:52 +000053
Rose Zadik9ba6b622018-01-24 12:59:19 +000054#define MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE -0x6080 /**< The selected feature is not available. */
55#define MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA -0x6100 /**< Bad input parameters. */
56#define MBEDTLS_ERR_CIPHER_ALLOC_FAILED -0x6180 /**< Failed to allocate memory. */
57#define MBEDTLS_ERR_CIPHER_INVALID_PADDING -0x6200 /**< Input data contains invalid padding and is rejected. */
58#define MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED -0x6280 /**< Decryption of block requires a full block. */
59#define MBEDTLS_ERR_CIPHER_AUTH_FAILED -0x6300 /**< Authentication failed (for AEAD modes). */
60#define MBEDTLS_ERR_CIPHER_INVALID_CONTEXT -0x6380 /**< The context is invalid. For example, because it was freed. */
Ron Eldor9924bdc2018-10-04 10:59:13 +030061
Rose Zadik9ba6b622018-01-24 12:59:19 +000062#define MBEDTLS_CIPHER_VARIABLE_IV_LEN 0x01 /**< Cipher accepts IVs of variable length. */
63#define MBEDTLS_CIPHER_VARIABLE_KEY_LEN 0x02 /**< Cipher accepts keys of variable length. */
Manuel Pégourié-Gonnard81754a02014-06-23 11:33:18 +020064
Paul Bakker407a0da2013-06-27 14:29:21 +020065#ifdef __cplusplus
66extern "C" {
67#endif
68
Hanno Beckerbbca8c52017-09-25 14:53:51 +010069/**
Rose Zadik02f73a62018-03-26 18:02:32 +010070 * \brief Supported cipher types.
Hanno Beckerbbca8c52017-09-25 14:53:51 +010071 *
TRodziewicz10e8cf52021-05-31 17:58:57 +020072 * \warning DES is considered weak cipher and its use
Rose Zadikb5607bf2018-04-16 10:34:51 +010073 * constitutes a security risk. Arm recommends considering stronger
74 * ciphers instead.
Hanno Beckerbbca8c52017-09-25 14:53:51 +010075 */
Paul Bakker8123e9d2011-01-06 15:37:30 +000076typedef enum {
Rose Zadik4c368e82018-04-19 14:24:11 +010077 MBEDTLS_CIPHER_ID_NONE = 0, /**< Placeholder to mark the end of cipher ID lists. */
78 MBEDTLS_CIPHER_ID_NULL, /**< The identity cipher, treated as a stream cipher. */
Rose Zadik02f73a62018-03-26 18:02:32 +010079 MBEDTLS_CIPHER_ID_AES, /**< The AES cipher. */
80 MBEDTLS_CIPHER_ID_DES, /**< The DES cipher. */
Rose Zadik93f99192018-04-19 14:41:33 +010081 MBEDTLS_CIPHER_ID_3DES, /**< The Triple DES cipher. */
Rose Zadik02f73a62018-03-26 18:02:32 +010082 MBEDTLS_CIPHER_ID_CAMELLIA, /**< The Camellia cipher. */
Manuel Pégourié-Gonnarda3712be2018-05-22 15:58:50 +020083 MBEDTLS_CIPHER_ID_ARIA, /**< The Aria cipher. */
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +020084 MBEDTLS_CIPHER_ID_CHACHA20, /**< The ChaCha20 cipher. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020085} mbedtls_cipher_id_t;
Paul Bakker8123e9d2011-01-06 15:37:30 +000086
Hanno Beckerbbca8c52017-09-25 14:53:51 +010087/**
Rose Zadik02f73a62018-03-26 18:02:32 +010088 * \brief Supported {cipher type, cipher mode} pairs.
Hanno Beckerbbca8c52017-09-25 14:53:51 +010089 *
TRodziewicz10e8cf52021-05-31 17:58:57 +020090 * \warning DES is considered weak cipher and its use
Rose Zadikb5607bf2018-04-16 10:34:51 +010091 * constitutes a security risk. Arm recommends considering stronger
Hanno Beckerbbca8c52017-09-25 14:53:51 +010092 * ciphers instead.
93 */
Paul Bakker8123e9d2011-01-06 15:37:30 +000094typedef enum {
Rose Zadik4c368e82018-04-19 14:24:11 +010095 MBEDTLS_CIPHER_NONE = 0, /**< Placeholder to mark the end of cipher-pair lists. */
Rose Zadikc441f742018-04-19 14:38:20 +010096 MBEDTLS_CIPHER_NULL, /**< The identity stream cipher. */
Rose Zadik02f73a62018-03-26 18:02:32 +010097 MBEDTLS_CIPHER_AES_128_ECB, /**< AES cipher with 128-bit ECB mode. */
98 MBEDTLS_CIPHER_AES_192_ECB, /**< AES cipher with 192-bit ECB mode. */
99 MBEDTLS_CIPHER_AES_256_ECB, /**< AES cipher with 256-bit ECB mode. */
100 MBEDTLS_CIPHER_AES_128_CBC, /**< AES cipher with 128-bit CBC mode. */
101 MBEDTLS_CIPHER_AES_192_CBC, /**< AES cipher with 192-bit CBC mode. */
102 MBEDTLS_CIPHER_AES_256_CBC, /**< AES cipher with 256-bit CBC mode. */
103 MBEDTLS_CIPHER_AES_128_CFB128, /**< AES cipher with 128-bit CFB128 mode. */
104 MBEDTLS_CIPHER_AES_192_CFB128, /**< AES cipher with 192-bit CFB128 mode. */
105 MBEDTLS_CIPHER_AES_256_CFB128, /**< AES cipher with 256-bit CFB128 mode. */
106 MBEDTLS_CIPHER_AES_128_CTR, /**< AES cipher with 128-bit CTR mode. */
107 MBEDTLS_CIPHER_AES_192_CTR, /**< AES cipher with 192-bit CTR mode. */
108 MBEDTLS_CIPHER_AES_256_CTR, /**< AES cipher with 256-bit CTR mode. */
109 MBEDTLS_CIPHER_AES_128_GCM, /**< AES cipher with 128-bit GCM mode. */
110 MBEDTLS_CIPHER_AES_192_GCM, /**< AES cipher with 192-bit GCM mode. */
111 MBEDTLS_CIPHER_AES_256_GCM, /**< AES cipher with 256-bit GCM mode. */
112 MBEDTLS_CIPHER_CAMELLIA_128_ECB, /**< Camellia cipher with 128-bit ECB mode. */
113 MBEDTLS_CIPHER_CAMELLIA_192_ECB, /**< Camellia cipher with 192-bit ECB mode. */
114 MBEDTLS_CIPHER_CAMELLIA_256_ECB, /**< Camellia cipher with 256-bit ECB mode. */
115 MBEDTLS_CIPHER_CAMELLIA_128_CBC, /**< Camellia cipher with 128-bit CBC mode. */
116 MBEDTLS_CIPHER_CAMELLIA_192_CBC, /**< Camellia cipher with 192-bit CBC mode. */
117 MBEDTLS_CIPHER_CAMELLIA_256_CBC, /**< Camellia cipher with 256-bit CBC mode. */
118 MBEDTLS_CIPHER_CAMELLIA_128_CFB128, /**< Camellia cipher with 128-bit CFB128 mode. */
119 MBEDTLS_CIPHER_CAMELLIA_192_CFB128, /**< Camellia cipher with 192-bit CFB128 mode. */
120 MBEDTLS_CIPHER_CAMELLIA_256_CFB128, /**< Camellia cipher with 256-bit CFB128 mode. */
121 MBEDTLS_CIPHER_CAMELLIA_128_CTR, /**< Camellia cipher with 128-bit CTR mode. */
122 MBEDTLS_CIPHER_CAMELLIA_192_CTR, /**< Camellia cipher with 192-bit CTR mode. */
123 MBEDTLS_CIPHER_CAMELLIA_256_CTR, /**< Camellia cipher with 256-bit CTR mode. */
124 MBEDTLS_CIPHER_CAMELLIA_128_GCM, /**< Camellia cipher with 128-bit GCM mode. */
125 MBEDTLS_CIPHER_CAMELLIA_192_GCM, /**< Camellia cipher with 192-bit GCM mode. */
126 MBEDTLS_CIPHER_CAMELLIA_256_GCM, /**< Camellia cipher with 256-bit GCM mode. */
127 MBEDTLS_CIPHER_DES_ECB, /**< DES cipher with ECB mode. */
128 MBEDTLS_CIPHER_DES_CBC, /**< DES cipher with CBC mode. */
129 MBEDTLS_CIPHER_DES_EDE_ECB, /**< DES cipher with EDE ECB mode. */
130 MBEDTLS_CIPHER_DES_EDE_CBC, /**< DES cipher with EDE CBC mode. */
131 MBEDTLS_CIPHER_DES_EDE3_ECB, /**< DES cipher with EDE3 ECB mode. */
132 MBEDTLS_CIPHER_DES_EDE3_CBC, /**< DES cipher with EDE3 CBC mode. */
Rose Zadik02f73a62018-03-26 18:02:32 +0100133 MBEDTLS_CIPHER_AES_128_CCM, /**< AES cipher with 128-bit CCM mode. */
134 MBEDTLS_CIPHER_AES_192_CCM, /**< AES cipher with 192-bit CCM mode. */
135 MBEDTLS_CIPHER_AES_256_CCM, /**< AES cipher with 256-bit CCM mode. */
136 MBEDTLS_CIPHER_CAMELLIA_128_CCM, /**< Camellia cipher with 128-bit CCM mode. */
137 MBEDTLS_CIPHER_CAMELLIA_192_CCM, /**< Camellia cipher with 192-bit CCM mode. */
138 MBEDTLS_CIPHER_CAMELLIA_256_CCM, /**< Camellia cipher with 256-bit CCM mode. */
Manuel Pégourié-Gonnarda3712be2018-05-22 15:58:50 +0200139 MBEDTLS_CIPHER_ARIA_128_ECB, /**< Aria cipher with 128-bit key and ECB mode. */
140 MBEDTLS_CIPHER_ARIA_192_ECB, /**< Aria cipher with 192-bit key and ECB mode. */
141 MBEDTLS_CIPHER_ARIA_256_ECB, /**< Aria cipher with 256-bit key and ECB mode. */
142 MBEDTLS_CIPHER_ARIA_128_CBC, /**< Aria cipher with 128-bit key and CBC mode. */
143 MBEDTLS_CIPHER_ARIA_192_CBC, /**< Aria cipher with 192-bit key and CBC mode. */
144 MBEDTLS_CIPHER_ARIA_256_CBC, /**< Aria cipher with 256-bit key and CBC mode. */
145 MBEDTLS_CIPHER_ARIA_128_CFB128, /**< Aria cipher with 128-bit key and CFB-128 mode. */
146 MBEDTLS_CIPHER_ARIA_192_CFB128, /**< Aria cipher with 192-bit key and CFB-128 mode. */
147 MBEDTLS_CIPHER_ARIA_256_CFB128, /**< Aria cipher with 256-bit key and CFB-128 mode. */
148 MBEDTLS_CIPHER_ARIA_128_CTR, /**< Aria cipher with 128-bit key and CTR mode. */
149 MBEDTLS_CIPHER_ARIA_192_CTR, /**< Aria cipher with 192-bit key and CTR mode. */
150 MBEDTLS_CIPHER_ARIA_256_CTR, /**< Aria cipher with 256-bit key and CTR mode. */
151 MBEDTLS_CIPHER_ARIA_128_GCM, /**< Aria cipher with 128-bit key and GCM mode. */
152 MBEDTLS_CIPHER_ARIA_192_GCM, /**< Aria cipher with 192-bit key and GCM mode. */
153 MBEDTLS_CIPHER_ARIA_256_GCM, /**< Aria cipher with 256-bit key and GCM mode. */
154 MBEDTLS_CIPHER_ARIA_128_CCM, /**< Aria cipher with 128-bit key and CCM mode. */
155 MBEDTLS_CIPHER_ARIA_192_CCM, /**< Aria cipher with 192-bit key and CCM mode. */
156 MBEDTLS_CIPHER_ARIA_256_CCM, /**< Aria cipher with 256-bit key and CCM mode. */
Simon Butcher6873c842018-04-29 13:03:20 +0100157 MBEDTLS_CIPHER_AES_128_OFB, /**< AES 128-bit cipher in OFB mode. */
158 MBEDTLS_CIPHER_AES_192_OFB, /**< AES 192-bit cipher in OFB mode. */
159 MBEDTLS_CIPHER_AES_256_OFB, /**< AES 256-bit cipher in OFB mode. */
Jaeden Ameroc6539902018-04-30 17:17:41 +0100160 MBEDTLS_CIPHER_AES_128_XTS, /**< AES 128-bit cipher in XTS block mode. */
161 MBEDTLS_CIPHER_AES_256_XTS, /**< AES 256-bit cipher in XTS block mode. */
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200162 MBEDTLS_CIPHER_CHACHA20, /**< ChaCha20 stream cipher. */
163 MBEDTLS_CIPHER_CHACHA20_POLY1305, /**< ChaCha20-Poly1305 AEAD cipher. */
Jack Lloydffdf2882019-03-07 17:00:32 -0500164 MBEDTLS_CIPHER_AES_128_KW, /**< AES cipher with 128-bit NIST KW mode. */
165 MBEDTLS_CIPHER_AES_192_KW, /**< AES cipher with 192-bit NIST KW mode. */
166 MBEDTLS_CIPHER_AES_256_KW, /**< AES cipher with 256-bit NIST KW mode. */
167 MBEDTLS_CIPHER_AES_128_KWP, /**< AES cipher with 128-bit NIST KWP mode. */
168 MBEDTLS_CIPHER_AES_192_KWP, /**< AES cipher with 192-bit NIST KWP mode. */
169 MBEDTLS_CIPHER_AES_256_KWP, /**< AES cipher with 256-bit NIST KWP mode. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200170} mbedtls_cipher_type_t;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000171
Rose Zadik9ba6b622018-01-24 12:59:19 +0000172/** Supported cipher modes. */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000173typedef enum {
Hanno Beckere5a04502018-11-08 16:57:42 +0000174 MBEDTLS_MODE_NONE = 0, /**< None. */
175 MBEDTLS_MODE_ECB, /**< The ECB cipher mode. */
176 MBEDTLS_MODE_CBC, /**< The CBC cipher mode. */
177 MBEDTLS_MODE_CFB, /**< The CFB cipher mode. */
178 MBEDTLS_MODE_OFB, /**< The OFB cipher mode. */
179 MBEDTLS_MODE_CTR, /**< The CTR cipher mode. */
180 MBEDTLS_MODE_GCM, /**< The GCM cipher mode. */
181 MBEDTLS_MODE_STREAM, /**< The stream cipher mode. */
182 MBEDTLS_MODE_CCM, /**< The CCM cipher mode. */
183 MBEDTLS_MODE_XTS, /**< The XTS cipher mode. */
Manuel Pégourié-Gonnardf57bf8b2018-06-18 11:14:09 +0200184 MBEDTLS_MODE_CHACHAPOLY, /**< The ChaCha-Poly cipher mode. */
Jack Lloydffdf2882019-03-07 17:00:32 -0500185 MBEDTLS_MODE_KW, /**< The SP800-38F KW mode */
186 MBEDTLS_MODE_KWP, /**< The SP800-38F KWP mode */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200187} mbedtls_cipher_mode_t;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000188
Rose Zadik9ba6b622018-01-24 12:59:19 +0000189/** Supported cipher padding types. */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000190typedef enum {
Rose Zadik9ba6b622018-01-24 12:59:19 +0000191 MBEDTLS_PADDING_PKCS7 = 0, /**< PKCS7 padding (default). */
192 MBEDTLS_PADDING_ONE_AND_ZEROS, /**< ISO/IEC 7816-4 padding. */
193 MBEDTLS_PADDING_ZEROS_AND_LEN, /**< ANSI X.923 padding. */
Rose Zadik02f73a62018-03-26 18:02:32 +0100194 MBEDTLS_PADDING_ZEROS, /**< Zero padding (not reversible). */
195 MBEDTLS_PADDING_NONE, /**< Never pad (full blocks only). */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200196} mbedtls_cipher_padding_t;
Manuel Pégourié-Gonnardd5fdcaf2013-07-24 18:05:00 +0200197
Rose Zadik9ba6b622018-01-24 12:59:19 +0000198/** Type of operation. */
Manuel Pégourié-Gonnardd5fdcaf2013-07-24 18:05:00 +0200199typedef enum {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200200 MBEDTLS_OPERATION_NONE = -1,
201 MBEDTLS_DECRYPT = 0,
202 MBEDTLS_ENCRYPT,
203} mbedtls_operation_t;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000204
205enum {
Rose Zadik9ba6b622018-01-24 12:59:19 +0000206 /** Undefined key length. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200207 MBEDTLS_KEY_LENGTH_NONE = 0,
Rose Zadik9ba6b622018-01-24 12:59:19 +0000208 /** Key length, in bits (including parity), for DES keys. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200209 MBEDTLS_KEY_LENGTH_DES = 64,
Rose Zadik9ba6b622018-01-24 12:59:19 +0000210 /** Key length in bits, including parity, for DES in two-key EDE. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200211 MBEDTLS_KEY_LENGTH_DES_EDE = 128,
Rose Zadik9ba6b622018-01-24 12:59:19 +0000212 /** Key length in bits, including parity, for DES in three-key EDE. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200213 MBEDTLS_KEY_LENGTH_DES_EDE3 = 192,
Paul Bakker8123e9d2011-01-06 15:37:30 +0000214};
215
Rose Zadik9ba6b622018-01-24 12:59:19 +0000216/** Maximum length of any IV, in Bytes. */
Hanno Becker15889832020-09-08 11:29:11 +0100217/* This should ideally be derived automatically from list of ciphers.
218 * This should be kept in sync with MBEDTLS_SSL_MAX_IV_LENGTH defined
Chris Jones3848e312021-03-11 16:17:59 +0000219 * in library/ssl_misc.h. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200220#define MBEDTLS_MAX_IV_LENGTH 16
Hanno Becker27a26882020-08-07 11:30:05 +0100221
Rose Zadik9ba6b622018-01-24 12:59:19 +0000222/** Maximum block size of any cipher, in Bytes. */
Hanno Becker15889832020-09-08 11:29:11 +0100223/* This should ideally be derived automatically from list of ciphers.
224 * This should be kept in sync with MBEDTLS_SSL_MAX_BLOCK_LENGTH defined
Chris Jones3848e312021-03-11 16:17:59 +0000225 * in library/ssl_misc.h. */
Manuel Pégourié-Gonnard32902e62018-05-10 12:30:19 +0200226#define MBEDTLS_MAX_BLOCK_LENGTH 16
Manuel Pégourié-Gonnard0b58c152013-10-24 17:17:54 +0200227
Hanno Becker27a26882020-08-07 11:30:05 +0100228/** Maximum key length, in Bytes. */
229/* This should ideally be derived automatically from list of ciphers.
230 * For now, only check whether XTS is enabled which uses 64 Byte keys,
Hanno Becker15889832020-09-08 11:29:11 +0100231 * and use 32 Bytes as an upper bound for the maximum key length otherwise.
232 * This should be kept in sync with MBEDTLS_SSL_MAX_BLOCK_LENGTH defined
Chris Jones3848e312021-03-11 16:17:59 +0000233 * in library/ssl_misc.h, which however deliberately ignores the case of XTS
Hanno Becker15889832020-09-08 11:29:11 +0100234 * since the latter isn't used in SSL/TLS. */
Hanno Becker27a26882020-08-07 11:30:05 +0100235#if defined(MBEDTLS_CIPHER_MODE_XTS)
236#define MBEDTLS_MAX_KEY_LENGTH 64
237#else
238#define MBEDTLS_MAX_KEY_LENGTH 32
239#endif /* MBEDTLS_CIPHER_MODE_XTS */
240
Paul Bakker8123e9d2011-01-06 15:37:30 +0000241/**
Manuel Pégourié-Gonnard5a74e8b2015-05-06 17:10:55 +0100242 * Base cipher information (opaque struct).
Paul Bakker343a8702011-06-09 14:27:58 +0000243 */
Manuel Pégourié-Gonnard5a74e8b2015-05-06 17:10:55 +0100244typedef struct mbedtls_cipher_base_t mbedtls_cipher_base_t;
Paul Bakker343a8702011-06-09 14:27:58 +0000245
246/**
Simon Butcher327398a2016-10-05 14:09:11 +0100247 * CMAC context (opaque struct).
248 */
249typedef struct mbedtls_cmac_context_t mbedtls_cmac_context_t;
250
251/**
Rose Zadik9ba6b622018-01-24 12:59:19 +0000252 * Cipher information. Allows calling cipher functions
253 * in a generic way.
Paul Bakker8123e9d2011-01-06 15:37:30 +0000254 */
Dawid Drozd428cc522018-07-24 10:02:47 +0200255typedef struct mbedtls_cipher_info_t
256{
Rose Zadik9ba6b622018-01-24 12:59:19 +0000257 /** Full cipher identifier. For example,
258 * MBEDTLS_CIPHER_AES_256_CBC.
259 */
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200260 mbedtls_cipher_type_t MBEDTLS_PRIVATE(type);
Paul Bakker8123e9d2011-01-06 15:37:30 +0000261
Rose Zadik9ba6b622018-01-24 12:59:19 +0000262 /** The cipher mode. For example, MBEDTLS_MODE_CBC. */
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200263 mbedtls_cipher_mode_t MBEDTLS_PRIVATE(mode);
Paul Bakker8123e9d2011-01-06 15:37:30 +0000264
Rose Zadik9ba6b622018-01-24 12:59:19 +0000265 /** The cipher key length, in bits. This is the
266 * default length for variable sized ciphers.
267 * Includes parity bits for ciphers like DES.
268 */
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200269 unsigned int MBEDTLS_PRIVATE(key_bitlen);
Paul Bakker8123e9d2011-01-06 15:37:30 +0000270
Rose Zadik9ba6b622018-01-24 12:59:19 +0000271 /** Name of the cipher. */
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200272 const char * MBEDTLS_PRIVATE(name);
Paul Bakker8123e9d2011-01-06 15:37:30 +0000273
Rose Zadik9ba6b622018-01-24 12:59:19 +0000274 /** IV or nonce size, in Bytes.
275 * For ciphers that accept variable IV sizes,
276 * this is the recommended size.
277 */
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200278 unsigned int MBEDTLS_PRIVATE(iv_size);
Paul Bakker8123e9d2011-01-06 15:37:30 +0000279
Rose Zadikb5607bf2018-04-16 10:34:51 +0100280 /** Bitflag comprised of MBEDTLS_CIPHER_VARIABLE_IV_LEN and
281 * MBEDTLS_CIPHER_VARIABLE_KEY_LEN indicating whether the
282 * cipher supports variable IV or variable key sizes, respectively.
283 */
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200284 int MBEDTLS_PRIVATE(flags);
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200285
Rose Zadik9ba6b622018-01-24 12:59:19 +0000286 /** The block size, in Bytes. */
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200287 unsigned int MBEDTLS_PRIVATE(block_size);
Paul Bakker8123e9d2011-01-06 15:37:30 +0000288
Rose Zadik9ba6b622018-01-24 12:59:19 +0000289 /** Struct for base cipher information and functions. */
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200290 const mbedtls_cipher_base_t *MBEDTLS_PRIVATE(base);
Paul Bakker8123e9d2011-01-06 15:37:30 +0000291
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200292} mbedtls_cipher_info_t;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000293
294/**
Paul Bakker20281562011-11-11 10:34:04 +0000295 * Generic cipher context.
Paul Bakker8123e9d2011-01-06 15:37:30 +0000296 */
Dawid Drozd428cc522018-07-24 10:02:47 +0200297typedef struct mbedtls_cipher_context_t
298{
Rose Zadik9ba6b622018-01-24 12:59:19 +0000299 /** Information about the associated cipher. */
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200300 const mbedtls_cipher_info_t *MBEDTLS_PRIVATE(cipher_info);
Paul Bakker8123e9d2011-01-06 15:37:30 +0000301
Rose Zadik9ba6b622018-01-24 12:59:19 +0000302 /** Key length to use. */
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200303 int MBEDTLS_PRIVATE(key_bitlen);
Paul Bakker8123e9d2011-01-06 15:37:30 +0000304
Rose Zadik9ba6b622018-01-24 12:59:19 +0000305 /** Operation that the key of the context has been
306 * initialized for.
307 */
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200308 mbedtls_operation_t MBEDTLS_PRIVATE(operation);
Paul Bakker8123e9d2011-01-06 15:37:30 +0000309
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200310#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Rose Zadik9ba6b622018-01-24 12:59:19 +0000311 /** Padding functions to use, if relevant for
312 * the specific cipher mode.
313 */
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200314 void (*MBEDTLS_PRIVATE(add_padding))( unsigned char *output, size_t olen, size_t data_len );
315 int (*MBEDTLS_PRIVATE(get_padding))( unsigned char *input, size_t ilen, size_t *data_len );
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +0100316#endif
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200317
Rose Zadik9ba6b622018-01-24 12:59:19 +0000318 /** Buffer for input that has not been processed yet. */
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200319 unsigned char MBEDTLS_PRIVATE(unprocessed_data)[MBEDTLS_MAX_BLOCK_LENGTH];
Paul Bakker8123e9d2011-01-06 15:37:30 +0000320
Rose Zadik9ba6b622018-01-24 12:59:19 +0000321 /** Number of Bytes that have not been processed yet. */
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200322 size_t MBEDTLS_PRIVATE(unprocessed_len);
Paul Bakker8123e9d2011-01-06 15:37:30 +0000323
Jaeden Ameroc6539902018-04-30 17:17:41 +0100324 /** Current IV or NONCE_COUNTER for CTR-mode, data unit (or sector) number
325 * for XTS-mode. */
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200326 unsigned char MBEDTLS_PRIVATE(iv)[MBEDTLS_MAX_IV_LENGTH];
Paul Bakker8123e9d2011-01-06 15:37:30 +0000327
Rose Zadik9ba6b622018-01-24 12:59:19 +0000328 /** IV size in Bytes, for ciphers with variable-length IVs. */
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200329 size_t MBEDTLS_PRIVATE(iv_size);
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200330
Rose Zadik9ba6b622018-01-24 12:59:19 +0000331 /** The cipher-specific context. */
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200332 void *MBEDTLS_PRIVATE(cipher_ctx);
Simon Butcher327398a2016-10-05 14:09:11 +0100333
334#if defined(MBEDTLS_CMAC_C)
Rose Zadik9ba6b622018-01-24 12:59:19 +0000335 /** CMAC-specific context. */
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200336 mbedtls_cmac_context_t *MBEDTLS_PRIVATE(cmac_ctx);
Simon Butcher327398a2016-10-05 14:09:11 +0100337#endif
Hanno Becker1cb36532018-11-09 16:20:29 +0000338
339#if defined(MBEDTLS_USE_PSA_CRYPTO)
340 /** Indicates whether the cipher operations should be performed
341 * by Mbed TLS' own crypto library or an external implementation
342 * of the PSA Crypto API.
Hanno Becker7909c4c2018-11-20 11:34:34 +0000343 * This is unset if the cipher context was established through
344 * mbedtls_cipher_setup(), and set if it was established through
Hanno Becker1cb36532018-11-09 16:20:29 +0000345 * mbedtls_cipher_setup_psa().
346 */
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200347 unsigned char MBEDTLS_PRIVATE(psa_enabled);
Hanno Becker1cb36532018-11-09 16:20:29 +0000348#endif /* MBEDTLS_USE_PSA_CRYPTO */
349
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200350} mbedtls_cipher_context_t;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000351
Paul Bakker8123e9d2011-01-06 15:37:30 +0000352/**
Hanno Beckerd7503a72018-11-08 15:55:24 +0000353 * \brief This function retrieves the list of ciphers supported
354 * by the generic cipher module.
Paul Bakker72f62662011-01-16 21:27:44 +0000355 *
Hanno Beckerd7503a72018-11-08 15:55:24 +0000356 * For any cipher identifier in the returned list, you can
357 * obtain the corresponding generic cipher information structure
358 * via mbedtls_cipher_info_from_type(), which can then be used
359 * to prepare a cipher context via mbedtls_cipher_setup().
360 *
361 *
362 * \return A statically-allocated array of cipher identifiers
363 * of type cipher_type_t. The last entry is zero.
Paul Bakker72f62662011-01-16 21:27:44 +0000364 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200365const int *mbedtls_cipher_list( void );
Paul Bakker72f62662011-01-16 21:27:44 +0000366
367/**
Rose Zadik9ba6b622018-01-24 12:59:19 +0000368 * \brief This function retrieves the cipher-information
369 * structure associated with the given cipher name.
Paul Bakker8123e9d2011-01-06 15:37:30 +0000370 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500371 * \param cipher_name Name of the cipher to search for. This must not be
372 * \c NULL.
Paul Bakker8123e9d2011-01-06 15:37:30 +0000373 *
Rose Zadik9ba6b622018-01-24 12:59:19 +0000374 * \return The cipher information structure associated with the
Rose Zadik02f73a62018-03-26 18:02:32 +0100375 * given \p cipher_name.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500376 * \return \c NULL if the associated cipher information is not found.
Paul Bakker8123e9d2011-01-06 15:37:30 +0000377 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200378const mbedtls_cipher_info_t *mbedtls_cipher_info_from_string( const char *cipher_name );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000379
380/**
Rose Zadik9ba6b622018-01-24 12:59:19 +0000381 * \brief This function retrieves the cipher-information
382 * structure associated with the given cipher type.
Paul Bakker8123e9d2011-01-06 15:37:30 +0000383 *
384 * \param cipher_type Type of the cipher to search for.
385 *
Rose Zadik9ba6b622018-01-24 12:59:19 +0000386 * \return The cipher information structure associated with the
Rose Zadikb5607bf2018-04-16 10:34:51 +0100387 * given \p cipher_type.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500388 * \return \c NULL if the associated cipher information is not found.
Paul Bakker8123e9d2011-01-06 15:37:30 +0000389 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200390const mbedtls_cipher_info_t *mbedtls_cipher_info_from_type( const mbedtls_cipher_type_t cipher_type );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000391
392/**
Rose Zadik9ba6b622018-01-24 12:59:19 +0000393 * \brief This function retrieves the cipher-information
394 * structure associated with the given cipher ID,
395 * key size and mode.
Paul Bakkerf46b6952013-09-09 00:08:26 +0200396 *
Rose Zadik9ba6b622018-01-24 12:59:19 +0000397 * \param cipher_id The ID of the cipher to search for. For example,
398 * #MBEDTLS_CIPHER_ID_AES.
399 * \param key_bitlen The length of the key in bits.
400 * \param mode The cipher mode. For example, #MBEDTLS_MODE_CBC.
Paul Bakkerf46b6952013-09-09 00:08:26 +0200401 *
Rose Zadik9ba6b622018-01-24 12:59:19 +0000402 * \return The cipher information structure associated with the
Rose Zadik02f73a62018-03-26 18:02:32 +0100403 * given \p cipher_id.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500404 * \return \c NULL if the associated cipher information is not found.
Paul Bakkerf46b6952013-09-09 00:08:26 +0200405 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200406const mbedtls_cipher_info_t *mbedtls_cipher_info_from_values( const mbedtls_cipher_id_t cipher_id,
Manuel Pégourié-Gonnardb8186a52015-06-18 14:58:58 +0200407 int key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200408 const mbedtls_cipher_mode_t mode );
Paul Bakkerf46b6952013-09-09 00:08:26 +0200409
410/**
Rose Zadik9ba6b622018-01-24 12:59:19 +0000411 * \brief This function initializes a \p cipher_context as NONE.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500412 *
413 * \param ctx The context to be initialized. This must not be \c NULL.
Paul Bakker84bbeb52014-07-01 14:53:22 +0200414 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200415void mbedtls_cipher_init( mbedtls_cipher_context_t *ctx );
Paul Bakker84bbeb52014-07-01 14:53:22 +0200416
417/**
Rose Zadik9ba6b622018-01-24 12:59:19 +0000418 * \brief This function frees and clears the cipher-specific
419 * context of \p ctx. Freeing \p ctx itself remains the
420 * responsibility of the caller.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500421 *
422 * \param ctx The context to be freed. If this is \c NULL, the
423 * function has no effect, otherwise this must point to an
424 * initialized context.
Paul Bakker84bbeb52014-07-01 14:53:22 +0200425 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200426void mbedtls_cipher_free( mbedtls_cipher_context_t *ctx );
Paul Bakker84bbeb52014-07-01 14:53:22 +0200427
Rose Zadik9ba6b622018-01-24 12:59:19 +0000428
Paul Bakker84bbeb52014-07-01 14:53:22 +0200429/**
Manuel Pégourié-Gonnarde9cac0e2021-05-27 12:59:11 +0200430 * \brief This function prepares a cipher context for
Hanno Beckerb1f08722018-11-09 16:09:19 +0000431 * use with the given cipher primitive.
Paul Bakker8123e9d2011-01-06 15:37:30 +0000432 *
Manuel Pégourié-Gonnardc42a0be2021-05-31 11:13:35 +0200433 * \note After calling this function, you should call
434 * mbedtls_cipher_setkey() and, if the mode uses padding,
435 * mbedtls_cipher_set_padding_mode(), then for each
436 * message to encrypt or decrypt with this key, either:
437 * - mbedtls_cipher_crypt() for one-shot processing with
438 * non-AEAD modes;
439 * - mbedtls_cipher_auth_encrypt_ext() or
440 * mbedtls_cipher_auth_decrypt_ext() for one-shot
441 * processing with AEAD modes or NIST_KW;
442 * - for multi-part processing, see the documentation of
443 * mbedtls_cipher_reset().
444 *
Manuel Pégourié-Gonnarde9cac0e2021-05-27 12:59:11 +0200445 * \param ctx The context to prepare. This must be initialized by
446 * a call to mbedtls_cipher_init() first.
Rose Zadik9ba6b622018-01-24 12:59:19 +0000447 * \param cipher_info The cipher to use.
Paul Bakker84bbeb52014-07-01 14:53:22 +0200448 *
Rose Zadik02f73a62018-03-26 18:02:32 +0100449 * \return \c 0 on success.
Rose Zadik4c368e82018-04-19 14:24:11 +0100450 * \return #MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA on
451 * parameter-verification failure.
Rose Zadik02f73a62018-03-26 18:02:32 +0100452 * \return #MBEDTLS_ERR_CIPHER_ALLOC_FAILED if allocation of the
Rose Zadik4c368e82018-04-19 14:24:11 +0100453 * cipher-specific context fails.
Paul Bakker8123e9d2011-01-06 15:37:30 +0000454 */
Hanno Beckerf4fb8762018-11-08 15:57:42 +0000455int mbedtls_cipher_setup( mbedtls_cipher_context_t *ctx,
456 const mbedtls_cipher_info_t *cipher_info );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000457
Hanno Becker098c9de2018-11-09 16:10:57 +0000458#if defined(MBEDTLS_USE_PSA_CRYPTO)
459/**
460 * \brief This function initializes a cipher context for
461 * PSA-based use with the given cipher primitive.
462 *
463 * \note See #MBEDTLS_USE_PSA_CRYPTO for information on PSA.
464 *
465 * \param ctx The context to initialize. May not be \c NULL.
466 * \param cipher_info The cipher to use.
Hanno Beckerf1336402018-11-12 16:26:27 +0000467 * \param taglen For AEAD ciphers, the length in bytes of the
468 * authentication tag to use. Subsequent uses of
TRodziewicz18efb732021-04-29 23:12:19 +0200469 * mbedtls_cipher_auth_encrypt_ext() or
470 * mbedtls_cipher_auth_decrypt_ext() must provide
Hanno Beckerf1336402018-11-12 16:26:27 +0000471 * the same tag length.
472 * For non-AEAD ciphers, the value must be \c 0.
Hanno Becker098c9de2018-11-09 16:10:57 +0000473 *
474 * \return \c 0 on success.
475 * \return #MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA on
476 * parameter-verification failure.
477 * \return #MBEDTLS_ERR_CIPHER_ALLOC_FAILED if allocation of the
478 * cipher-specific context fails.
479 */
480int mbedtls_cipher_setup_psa( mbedtls_cipher_context_t *ctx,
Hanno Beckerf1336402018-11-12 16:26:27 +0000481 const mbedtls_cipher_info_t *cipher_info,
482 size_t taglen );
Hanno Becker098c9de2018-11-09 16:10:57 +0000483#endif /* MBEDTLS_USE_PSA_CRYPTO */
484
Paul Bakker8123e9d2011-01-06 15:37:30 +0000485/**
Rose Zadik9ba6b622018-01-24 12:59:19 +0000486 * \brief This function returns the block size of the given cipher.
Paul Bakker8123e9d2011-01-06 15:37:30 +0000487 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500488 * \param ctx The context of the cipher. This must be initialized.
Paul Bakker8123e9d2011-01-06 15:37:30 +0000489 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500490 * \return The block size of the underlying cipher.
491 * \return \c 0 if \p ctx has not been initialized.
Paul Bakker8123e9d2011-01-06 15:37:30 +0000492 */
Hanno Beckerf4fb8762018-11-08 15:57:42 +0000493static inline unsigned int mbedtls_cipher_get_block_size(
494 const mbedtls_cipher_context_t *ctx )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000495{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500496 MBEDTLS_INTERNAL_VALIDATE_RET( ctx != NULL, 0 );
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200497 if( ctx->MBEDTLS_PRIVATE(cipher_info) == NULL )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000498 return 0;
499
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200500 return ctx->MBEDTLS_PRIVATE(cipher_info)->MBEDTLS_PRIVATE(block_size);
Paul Bakker8123e9d2011-01-06 15:37:30 +0000501}
502
503/**
Rose Zadik9ba6b622018-01-24 12:59:19 +0000504 * \brief This function returns the mode of operation for
505 * the cipher. For example, MBEDTLS_MODE_CBC.
Paul Bakkerf7e5bb52011-11-11 10:53:37 +0000506 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500507 * \param ctx The context of the cipher. This must be initialized.
Paul Bakkerf7e5bb52011-11-11 10:53:37 +0000508 *
Rose Zadik02f73a62018-03-26 18:02:32 +0100509 * \return The mode of operation.
510 * \return #MBEDTLS_MODE_NONE if \p ctx has not been initialized.
Paul Bakkerf7e5bb52011-11-11 10:53:37 +0000511 */
Hanno Beckerf4fb8762018-11-08 15:57:42 +0000512static inline mbedtls_cipher_mode_t mbedtls_cipher_get_cipher_mode(
513 const mbedtls_cipher_context_t *ctx )
Paul Bakkerf7e5bb52011-11-11 10:53:37 +0000514{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500515 MBEDTLS_INTERNAL_VALIDATE_RET( ctx != NULL, MBEDTLS_MODE_NONE );
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200516 if( ctx->MBEDTLS_PRIVATE(cipher_info) == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200517 return MBEDTLS_MODE_NONE;
Paul Bakkerf7e5bb52011-11-11 10:53:37 +0000518
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200519 return ctx->MBEDTLS_PRIVATE(cipher_info)->MBEDTLS_PRIVATE(mode);
Paul Bakkerf7e5bb52011-11-11 10:53:37 +0000520}
521
522/**
Rose Zadik9ba6b622018-01-24 12:59:19 +0000523 * \brief This function returns the size of the IV or nonce
524 * of the cipher, in Bytes.
Paul Bakker8123e9d2011-01-06 15:37:30 +0000525 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500526 * \param ctx The context of the cipher. This must be initialized.
Paul Bakker8123e9d2011-01-06 15:37:30 +0000527 *
Rose Zadikb5607bf2018-04-16 10:34:51 +0100528 * \return The recommended IV size if no IV has been set.
Rose Zadik4c368e82018-04-19 14:24:11 +0100529 * \return \c 0 for ciphers not using an IV or a nonce.
Rose Zadikb5607bf2018-04-16 10:34:51 +0100530 * \return The actual size if an IV has been set.
Paul Bakker8123e9d2011-01-06 15:37:30 +0000531 */
Hanno Beckerf4fb8762018-11-08 15:57:42 +0000532static inline int mbedtls_cipher_get_iv_size(
533 const mbedtls_cipher_context_t *ctx )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000534{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500535 MBEDTLS_INTERNAL_VALIDATE_RET( ctx != NULL, 0 );
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200536 if( ctx->MBEDTLS_PRIVATE(cipher_info) == NULL )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000537 return 0;
538
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200539 if( ctx->MBEDTLS_PRIVATE(iv_size) != 0 )
540 return (int) ctx->MBEDTLS_PRIVATE(iv_size);
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200541
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200542 return (int) ctx->MBEDTLS_PRIVATE(cipher_info)->MBEDTLS_PRIVATE(iv_size);
Paul Bakker8123e9d2011-01-06 15:37:30 +0000543}
544
545/**
Rose Zadik9ba6b622018-01-24 12:59:19 +0000546 * \brief This function returns the type of the given cipher.
Paul Bakker8123e9d2011-01-06 15:37:30 +0000547 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500548 * \param ctx The context of the cipher. This must be initialized.
Paul Bakker8123e9d2011-01-06 15:37:30 +0000549 *
Rose Zadik02f73a62018-03-26 18:02:32 +0100550 * \return The type of the cipher.
551 * \return #MBEDTLS_CIPHER_NONE if \p ctx has not been initialized.
Paul Bakker8123e9d2011-01-06 15:37:30 +0000552 */
Hanno Beckerf4fb8762018-11-08 15:57:42 +0000553static inline mbedtls_cipher_type_t mbedtls_cipher_get_type(
554 const mbedtls_cipher_context_t *ctx )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000555{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500556 MBEDTLS_INTERNAL_VALIDATE_RET(
557 ctx != NULL, MBEDTLS_CIPHER_NONE );
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200558 if( ctx->MBEDTLS_PRIVATE(cipher_info) == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200559 return MBEDTLS_CIPHER_NONE;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000560
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200561 return ctx->MBEDTLS_PRIVATE(cipher_info)->MBEDTLS_PRIVATE(type);
Paul Bakker8123e9d2011-01-06 15:37:30 +0000562}
563
564/**
Rose Zadik9ba6b622018-01-24 12:59:19 +0000565 * \brief This function returns the name of the given cipher
566 * as a string.
Paul Bakker8123e9d2011-01-06 15:37:30 +0000567 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500568 * \param ctx The context of the cipher. This must be initialized.
Paul Bakker8123e9d2011-01-06 15:37:30 +0000569 *
Rose Zadik02f73a62018-03-26 18:02:32 +0100570 * \return The name of the cipher.
571 * \return NULL if \p ctx has not been not initialized.
Paul Bakker8123e9d2011-01-06 15:37:30 +0000572 */
Hanno Beckerf4fb8762018-11-08 15:57:42 +0000573static inline const char *mbedtls_cipher_get_name(
574 const mbedtls_cipher_context_t *ctx )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000575{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500576 MBEDTLS_INTERNAL_VALIDATE_RET( ctx != NULL, 0 );
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200577 if( ctx->MBEDTLS_PRIVATE(cipher_info) == NULL )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000578 return 0;
579
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200580 return ctx->MBEDTLS_PRIVATE(cipher_info)->MBEDTLS_PRIVATE(name);
Paul Bakker8123e9d2011-01-06 15:37:30 +0000581}
582
583/**
Rose Zadik9ba6b622018-01-24 12:59:19 +0000584 * \brief This function returns the key length of the cipher.
Paul Bakker8123e9d2011-01-06 15:37:30 +0000585 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500586 * \param ctx The context of the cipher. This must be initialized.
Paul Bakker8123e9d2011-01-06 15:37:30 +0000587 *
Rose Zadik02f73a62018-03-26 18:02:32 +0100588 * \return The key length of the cipher in bits.
589 * \return #MBEDTLS_KEY_LENGTH_NONE if ctx \p has not been
Rose Zadik9ba6b622018-01-24 12:59:19 +0000590 * initialized.
Paul Bakker8123e9d2011-01-06 15:37:30 +0000591 */
Hanno Beckerf4fb8762018-11-08 15:57:42 +0000592static inline int mbedtls_cipher_get_key_bitlen(
593 const mbedtls_cipher_context_t *ctx )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000594{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500595 MBEDTLS_INTERNAL_VALIDATE_RET(
596 ctx != NULL, MBEDTLS_KEY_LENGTH_NONE );
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200597 if( ctx->MBEDTLS_PRIVATE(cipher_info) == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200598 return MBEDTLS_KEY_LENGTH_NONE;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000599
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200600 return (int) ctx->MBEDTLS_PRIVATE(cipher_info)->MBEDTLS_PRIVATE(key_bitlen);
Paul Bakker8123e9d2011-01-06 15:37:30 +0000601}
602
603/**
Rose Zadik9ba6b622018-01-24 12:59:19 +0000604 * \brief This function returns the operation of the given cipher.
Paul Bakkerf7e5bb52011-11-11 10:53:37 +0000605 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500606 * \param ctx The context of the cipher. This must be initialized.
Paul Bakkerf7e5bb52011-11-11 10:53:37 +0000607 *
Rose Zadikb5607bf2018-04-16 10:34:51 +0100608 * \return The type of operation: #MBEDTLS_ENCRYPT or #MBEDTLS_DECRYPT.
609 * \return #MBEDTLS_OPERATION_NONE if \p ctx has not been initialized.
Paul Bakkerf7e5bb52011-11-11 10:53:37 +0000610 */
Hanno Beckerf4fb8762018-11-08 15:57:42 +0000611static inline mbedtls_operation_t mbedtls_cipher_get_operation(
612 const mbedtls_cipher_context_t *ctx )
Paul Bakkerf7e5bb52011-11-11 10:53:37 +0000613{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500614 MBEDTLS_INTERNAL_VALIDATE_RET(
615 ctx != NULL, MBEDTLS_OPERATION_NONE );
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200616 if( ctx->MBEDTLS_PRIVATE(cipher_info) == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200617 return MBEDTLS_OPERATION_NONE;
Paul Bakkerf7e5bb52011-11-11 10:53:37 +0000618
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200619 return ctx->MBEDTLS_PRIVATE(operation);
Paul Bakkerf7e5bb52011-11-11 10:53:37 +0000620}
621
622/**
Rose Zadik9ba6b622018-01-24 12:59:19 +0000623 * \brief This function sets the key to use with the given context.
Paul Bakker8123e9d2011-01-06 15:37:30 +0000624 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500625 * \param ctx The generic cipher context. This must be initialized and
626 * bound to a cipher information structure.
627 * \param key The key to use. This must be a readable buffer of at
628 * least \p key_bitlen Bits.
629 * \param key_bitlen The key length to use, in Bits.
Rose Zadik9ba6b622018-01-24 12:59:19 +0000630 * \param operation The operation that the key will be used for:
631 * #MBEDTLS_ENCRYPT or #MBEDTLS_DECRYPT.
Paul Bakker8123e9d2011-01-06 15:37:30 +0000632 *
Rose Zadikb5607bf2018-04-16 10:34:51 +0100633 * \return \c 0 on success.
Rose Zadik4c368e82018-04-19 14:24:11 +0100634 * \return #MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA on
635 * parameter-verification failure.
Rose Zadikb5607bf2018-04-16 10:34:51 +0100636 * \return A cipher-specific error code on failure.
Paul Bakker8123e9d2011-01-06 15:37:30 +0000637 */
Hanno Beckerf4fb8762018-11-08 15:57:42 +0000638int mbedtls_cipher_setkey( mbedtls_cipher_context_t *ctx,
639 const unsigned char *key,
640 int key_bitlen,
641 const mbedtls_operation_t operation );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000642
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200643#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Paul Bakker8123e9d2011-01-06 15:37:30 +0000644/**
Rose Zadik9ba6b622018-01-24 12:59:19 +0000645 * \brief This function sets the padding mode, for cipher modes
646 * that use padding.
Manuel Pégourié-Gonnardd5fdcaf2013-07-24 18:05:00 +0200647 *
Rose Zadik9ba6b622018-01-24 12:59:19 +0000648 * The default passing mode is PKCS7 padding.
Manuel Pégourié-Gonnardd5fdcaf2013-07-24 18:05:00 +0200649 *
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.
Rose Zadik9ba6b622018-01-24 12:59:19 +0000652 * \param mode The padding mode.
653 *
Rose Zadikb5607bf2018-04-16 10:34:51 +0100654 * \return \c 0 on success.
Rose Zadik02f73a62018-03-26 18:02:32 +0100655 * \return #MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE
656 * if the selected padding mode is not supported.
657 * \return #MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA if the cipher mode
Paul Bakker1a45d912013-08-14 12:04:26 +0200658 * does not support padding.
Manuel Pégourié-Gonnardd5fdcaf2013-07-24 18:05:00 +0200659 */
Hanno Beckerf4fb8762018-11-08 15:57:42 +0000660int mbedtls_cipher_set_padding_mode( mbedtls_cipher_context_t *ctx,
661 mbedtls_cipher_padding_t mode );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200662#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
Manuel Pégourié-Gonnardd5fdcaf2013-07-24 18:05:00 +0200663
664/**
Rose Zadik9ba6b622018-01-24 12:59:19 +0000665 * \brief This function sets the initialization vector (IV)
666 * or nonce.
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200667 *
Rose Zadik02f73a62018-03-26 18:02:32 +0100668 * \note Some ciphers do not use IVs nor nonce. For these
669 * ciphers, this function has no effect.
670 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500671 * \param ctx The generic cipher context. This must be initialized and
672 * bound to a cipher information structure.
673 * \param iv The IV to use, or NONCE_COUNTER for CTR-mode ciphers. This
674 * must be a readable buffer of at least \p iv_len Bytes.
Rose Zadik9ba6b622018-01-24 12:59:19 +0000675 * \param iv_len The IV length for ciphers with variable-size IV.
676 * This parameter is discarded by ciphers with fixed-size IV.
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200677 *
Rose Zadikb5607bf2018-04-16 10:34:51 +0100678 * \return \c 0 on success.
Rose Zadik4c368e82018-04-19 14:24:11 +0100679 * \return #MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA on
680 * parameter-verification failure.
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200681 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200682int mbedtls_cipher_set_iv( mbedtls_cipher_context_t *ctx,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500683 const unsigned char *iv,
684 size_t iv_len );
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200685
686/**
Rose Zadik9ba6b622018-01-24 12:59:19 +0000687 * \brief This function resets the cipher state.
Paul Bakker8123e9d2011-01-06 15:37:30 +0000688 *
Manuel Pégourié-Gonnardc42a0be2021-05-31 11:13:35 +0200689 * \note With non-AEAD ciphers, the order of calls for each message
690 * is as follows:
691 * 1. mbedtls_cipher_set_iv() if the mode uses an IV/nonce.
692 * 2. mbedtls_cipher_reset()
693 * 3. mbedtls_cipher_update() one or more times
694 * 4. mbedtls_cipher_finish()
695 * .
Manuel Pégourié-Gonnard42eba1a2021-05-31 12:14:02 +0200696 * This sequence can be repeated to encrypt or decrypt multiple
Manuel Pégourié-Gonnardc42a0be2021-05-31 11:13:35 +0200697 * messages with the same key.
698 *
699 * \note With AEAD ciphers, the order of calls for each message
700 * is as follows:
701 * 1. mbedtls_cipher_set_iv() if the mode uses an IV/nonce.
702 * 2. mbedtls_cipher_reset()
703 * 3. mbedtls_cipher_update_ad()
704 * 4. mbedtls_cipher_update() one or more times
705 * 5. mbedtls_cipher_finish()
706 * 6. mbedtls_cipher_check_tag() (for decryption) or
707 * mbedtls_cipher_write_tag() (for encryption).
708 * .
Manuel Pégourié-Gonnard42eba1a2021-05-31 12:14:02 +0200709 * This sequence can be repeated to encrypt or decrypt multiple
Manuel Pégourié-Gonnardc42a0be2021-05-31 11:13:35 +0200710 * messages with the same key.
711 *
712 * \param ctx The generic cipher context. This must be bound to a key.
Paul Bakker8123e9d2011-01-06 15:37:30 +0000713 *
Rose Zadikb5607bf2018-04-16 10:34:51 +0100714 * \return \c 0 on success.
Rose Zadik4c368e82018-04-19 14:24:11 +0100715 * \return #MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA on
716 * parameter-verification failure.
Paul Bakker8123e9d2011-01-06 15:37:30 +0000717 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200718int mbedtls_cipher_reset( mbedtls_cipher_context_t *ctx );
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200719
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200720#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200721/**
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500722 * \brief This function adds additional data for AEAD ciphers.
723 * Currently supported with GCM and ChaCha20+Poly1305.
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200724 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500725 * \param ctx The generic cipher context. This must be initialized.
726 * \param ad The additional data to use. This must be a readable
727 * buffer of at least \p ad_len Bytes.
Andrzej Kurek02f39ac2019-02-08 06:50:55 -0500728 * \param ad_len The length of \p ad in Bytes.
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200729 *
Andrzej Kurek57f04e52019-02-08 06:44:43 -0500730 * \return \c 0 on success.
731 * \return A specific error code on failure.
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200732 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200733int mbedtls_cipher_update_ad( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200734 const unsigned char *ad, size_t ad_len );
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200735#endif /* MBEDTLS_GCM_C || MBEDTLS_CHACHAPOLY_C */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000736
737/**
Rose Zadik9ba6b622018-01-24 12:59:19 +0000738 * \brief The generic cipher update function. It encrypts or
739 * decrypts using the given cipher context. Writes as
740 * many block-sized blocks of data as possible to output.
741 * Any data that cannot be written immediately is either
742 * added to the next block, or flushed when
743 * mbedtls_cipher_finish() is called.
744 * Exception: For MBEDTLS_MODE_ECB, expects a single block
745 * in size. For example, 16 Bytes for AES.
Paul Bakker8123e9d2011-01-06 15:37:30 +0000746 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500747 * \param ctx The generic cipher context. This must be initialized and
748 * bound to a key.
749 * \param input The buffer holding the input data. This must be a
750 * readable buffer of at least \p ilen Bytes.
Rose Zadik9ba6b622018-01-24 12:59:19 +0000751 * \param ilen The length of the input data.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500752 * \param output The buffer for the output data. This must be able to
753 * hold at least `ilen + block_size`. This must not be the
754 * same buffer as \p input.
Rose Zadik9ba6b622018-01-24 12:59:19 +0000755 * \param olen The length of the output data, to be updated with the
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500756 * actual number of Bytes written. This must not be
757 * \c NULL.
Paul Bakker8123e9d2011-01-06 15:37:30 +0000758 *
Rose Zadikb5607bf2018-04-16 10:34:51 +0100759 * \return \c 0 on success.
Rose Zadik4c368e82018-04-19 14:24:11 +0100760 * \return #MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA on
761 * parameter-verification failure.
Rose Zadik02f73a62018-03-26 18:02:32 +0100762 * \return #MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE on an
763 * unsupported mode for a cipher.
Rose Zadikb5607bf2018-04-16 10:34:51 +0100764 * \return A cipher-specific error code on failure.
Paul Bakker8123e9d2011-01-06 15:37:30 +0000765 */
Hanno Beckerf4fb8762018-11-08 15:57:42 +0000766int mbedtls_cipher_update( mbedtls_cipher_context_t *ctx,
767 const unsigned char *input,
768 size_t ilen, unsigned char *output,
769 size_t *olen );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000770
771/**
Rose Zadik9ba6b622018-01-24 12:59:19 +0000772 * \brief The generic cipher finalization function. If data still
773 * needs to be flushed from an incomplete block, the data
774 * contained in it is padded to the size of
775 * the last block, and written to the \p output buffer.
Paul Bakker8123e9d2011-01-06 15:37:30 +0000776 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500777 * \param ctx The generic cipher context. This must be initialized and
778 * bound to a key.
779 * \param output The buffer to write data to. This needs to be a writable
780 * buffer of at least \p block_size Bytes.
Rose Zadik9ba6b622018-01-24 12:59:19 +0000781 * \param olen The length of the data written to the \p output buffer.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500782 * This may not be \c NULL.
Paul Bakker8123e9d2011-01-06 15:37:30 +0000783 *
Rose Zadikb5607bf2018-04-16 10:34:51 +0100784 * \return \c 0 on success.
Rose Zadik4c368e82018-04-19 14:24:11 +0100785 * \return #MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA on
786 * parameter-verification failure.
787 * \return #MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED on decryption
788 * expecting a full block but not receiving one.
Rose Zadik02f73a62018-03-26 18:02:32 +0100789 * \return #MBEDTLS_ERR_CIPHER_INVALID_PADDING on invalid padding
790 * while decrypting.
Rose Zadikb5607bf2018-04-16 10:34:51 +0100791 * \return A cipher-specific error code on failure.
Paul Bakker8123e9d2011-01-06 15:37:30 +0000792 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200793int mbedtls_cipher_finish( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200794 unsigned char *output, size_t *olen );
795
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200796#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200797/**
Rose Zadik9ba6b622018-01-24 12:59:19 +0000798 * \brief This function writes a tag for AEAD ciphers.
Daniel King8fe47012016-05-17 20:33:28 -0300799 * Currently supported with GCM and ChaCha20+Poly1305.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500800 * This must be called after mbedtls_cipher_finish().
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200801 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500802 * \param ctx The generic cipher context. This must be initialized,
803 * bound to a key, and have just completed a cipher
804 * operation through mbedtls_cipher_finish() the tag for
805 * which should be written.
806 * \param tag The buffer to write the tag to. This must be a writable
807 * buffer of at least \p tag_len Bytes.
Rose Zadik9ba6b622018-01-24 12:59:19 +0000808 * \param tag_len The length of the tag to write.
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200809 *
Rose Zadik02f73a62018-03-26 18:02:32 +0100810 * \return \c 0 on success.
811 * \return A specific error code on failure.
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200812 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200813int mbedtls_cipher_write_tag( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200814 unsigned char *tag, size_t tag_len );
815
816/**
Rose Zadik9ba6b622018-01-24 12:59:19 +0000817 * \brief This function checks the tag for AEAD ciphers.
Daniel King8fe47012016-05-17 20:33:28 -0300818 * Currently supported with GCM and ChaCha20+Poly1305.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500819 * This must be called after mbedtls_cipher_finish().
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200820 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500821 * \param ctx The generic cipher context. This must be initialized.
822 * \param tag The buffer holding the tag. This must be a readable
823 * buffer of at least \p tag_len Bytes.
Rose Zadik9ba6b622018-01-24 12:59:19 +0000824 * \param tag_len The length of the tag to check.
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200825 *
Rose Zadik02f73a62018-03-26 18:02:32 +0100826 * \return \c 0 on success.
827 * \return A specific error code on failure.
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200828 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200829int mbedtls_cipher_check_tag( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200830 const unsigned char *tag, size_t tag_len );
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200831#endif /* MBEDTLS_GCM_C || MBEDTLS_CHACHAPOLY_C */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000832
Paul Bakker8123e9d2011-01-06 15:37:30 +0000833/**
Rose Zadik9ba6b622018-01-24 12:59:19 +0000834 * \brief The generic all-in-one encryption/decryption function,
835 * for all ciphers except AEAD constructs.
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +0200836 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500837 * \param ctx The generic cipher context. This must be initialized.
Rose Zadik9ba6b622018-01-24 12:59:19 +0000838 * \param iv The IV to use, or NONCE_COUNTER for CTR-mode ciphers.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500839 * This must be a readable buffer of at least \p iv_len
840 * Bytes.
Rose Zadik9ba6b622018-01-24 12:59:19 +0000841 * \param iv_len The IV length for ciphers with variable-size IV.
842 * This parameter is discarded by ciphers with fixed-size
843 * IV.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500844 * \param input The buffer holding the input data. This must be a
845 * readable buffer of at least \p ilen Bytes.
846 * \param ilen The length of the input data in Bytes.
847 * \param output The buffer for the output data. This must be able to
848 * hold at least `ilen + block_size`. This must not be the
849 * same buffer as \p input.
Rose Zadik9ba6b622018-01-24 12:59:19 +0000850 * \param olen The length of the output data, to be updated with the
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500851 * actual number of Bytes written. This must not be
852 * \c NULL.
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +0200853 *
Rose Zadik9ba6b622018-01-24 12:59:19 +0000854 * \note Some ciphers do not use IVs nor nonce. For these
855 * ciphers, use \p iv = NULL and \p iv_len = 0.
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +0200856 *
Rose Zadikb5607bf2018-04-16 10:34:51 +0100857 * \return \c 0 on success.
Rose Zadik4c368e82018-04-19 14:24:11 +0100858 * \return #MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA on
859 * parameter-verification failure.
860 * \return #MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED on decryption
861 * expecting a full block but not receiving one.
Rose Zadik02f73a62018-03-26 18:02:32 +0100862 * \return #MBEDTLS_ERR_CIPHER_INVALID_PADDING on invalid padding
863 * while decrypting.
Rose Zadikb5607bf2018-04-16 10:34:51 +0100864 * \return A cipher-specific error code on failure.
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +0200865 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200866int mbedtls_cipher_crypt( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +0200867 const unsigned char *iv, size_t iv_len,
868 const unsigned char *input, size_t ilen,
869 unsigned char *output, size_t *olen );
870
Manuel Pégourié-Gonnard9cc079d2020-11-25 12:57:47 +0100871#if defined(MBEDTLS_CIPHER_MODE_AEAD) || defined(MBEDTLS_NIST_KW_C)
872/**
Manuel Pégourié-Gonnardf2ffbc42020-12-01 09:57:55 +0100873 * \brief The authenticated encryption (AEAD/NIST_KW) function.
Manuel Pégourié-Gonnard9cc079d2020-11-25 12:57:47 +0100874 *
875 * \note For AEAD modes, the tag will be appended to the
876 * ciphertext, as recommended by RFC 5116.
877 * (NIST_KW doesn't have a separate tag.)
878 *
879 * \param ctx The generic cipher context. This must be initialized and
Manuel Pégourié-Gonnardf2ffbc42020-12-01 09:57:55 +0100880 * bound to a key, with an AEAD algorithm or NIST_KW.
Manuel Pégourié-Gonnard9cc079d2020-11-25 12:57:47 +0100881 * \param iv The nonce to use. This must be a readable buffer of
882 * at least \p iv_len Bytes and may be \c NULL if \p
883 * iv_len is \c 0.
Manuel Pégourié-Gonnardb23e31d2020-12-07 09:57:35 +0100884 * \param iv_len The length of the nonce. For AEAD ciphers, this must
885 * satisfy the constraints imposed by the cipher used.
886 * For NIST_KW, this must be \c 0.
Manuel Pégourié-Gonnard9cc079d2020-11-25 12:57:47 +0100887 * \param ad The additional data to authenticate. This must be a
888 * readable buffer of at least \p ad_len Bytes, and may
889 * be \c NULL is \p ad_len is \c 0.
890 * \param ad_len The length of \p ad. For NIST_KW, this must be \c 0.
891 * \param input The buffer holding the input data. This must be a
892 * readable buffer of at least \p ilen Bytes, and may be
893 * \c NULL if \p ilen is \c 0.
894 * \param ilen The length of the input data.
895 * \param output The buffer for the output data. This must be a
896 * writable buffer of at least \p output_len Bytes, and
897 * must not be \c NULL.
898 * \param output_len The length of the \p output buffer in Bytes. For AEAD
899 * ciphers, this must be at least \p ilen + \p tag_len.
900 * For NIST_KW, this must be at least \p ilen + 8
901 * (rounded up to a multiple of 8 if KWP is used);
902 * \p ilen + 15 is always a safe value.
903 * \param olen This will be filled with the actual number of Bytes
904 * written to the \p output buffer. This must point to a
905 * writable object of type \c size_t.
906 * \param tag_len The desired length of the authentication tag. For AEAD
907 * ciphers, this must match the constraints imposed by
Manuel Pégourié-Gonnardf2ffbc42020-12-01 09:57:55 +0100908 * the cipher used, and in particular must not be \c 0.
Manuel Pégourié-Gonnard9cc079d2020-11-25 12:57:47 +0100909 * For NIST_KW, this must be \c 0.
910 *
911 * \return \c 0 on success.
912 * \return #MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA on
913 * parameter-verification failure.
914 * \return A cipher-specific error code on failure.
915 */
916int mbedtls_cipher_auth_encrypt_ext( mbedtls_cipher_context_t *ctx,
917 const unsigned char *iv, size_t iv_len,
918 const unsigned char *ad, size_t ad_len,
919 const unsigned char *input, size_t ilen,
920 unsigned char *output, size_t output_len,
921 size_t *olen, size_t tag_len );
922
923/**
Manuel Pégourié-Gonnardf2ffbc42020-12-01 09:57:55 +0100924 * \brief The authenticated encryption (AEAD/NIST_KW) function.
Manuel Pégourié-Gonnard9cc079d2020-11-25 12:57:47 +0100925 *
926 * \note If the data is not authentic, then the output buffer
927 * is zeroed out to prevent the unauthentic plaintext being
928 * used, making this interface safer.
929 *
930 * \note For AEAD modes, the tag must be appended to the
931 * ciphertext, as recommended by RFC 5116.
932 * (NIST_KW doesn't have a separate tag.)
933 *
934 * \param ctx The generic cipher context. This must be initialized and
Manuel Pégourié-Gonnardf2ffbc42020-12-01 09:57:55 +0100935 * bound to a key, with an AEAD algorithm or NIST_KW.
Manuel Pégourié-Gonnard9cc079d2020-11-25 12:57:47 +0100936 * \param iv The nonce to use. This must be a readable buffer of
937 * at least \p iv_len Bytes and may be \c NULL if \p
938 * iv_len is \c 0.
Manuel Pégourié-Gonnardb23e31d2020-12-07 09:57:35 +0100939 * \param iv_len The length of the nonce. For AEAD ciphers, this must
940 * satisfy the constraints imposed by the cipher used.
941 * For NIST_KW, this must be \c 0.
Manuel Pégourié-Gonnard9cc079d2020-11-25 12:57:47 +0100942 * \param ad The additional data to authenticate. This must be a
943 * readable buffer of at least \p ad_len Bytes, and may
944 * be \c NULL is \p ad_len is \c 0.
945 * \param ad_len The length of \p ad. For NIST_KW, this must be \c 0.
946 * \param input The buffer holding the input data. This must be a
947 * readable buffer of at least \p ilen Bytes, and may be
948 * \c NULL if \p ilen is \c 0.
949 * \param ilen The length of the input data. For AEAD ciphers this
950 * must be at least \p tag_len. For NIST_KW this must be
951 * at least \c 8.
952 * \param output The buffer for the output data. This must be a
953 * writable buffer of at least \p output_len Bytes, and
954 * may be \c NULL if \p output_len is \c 0.
955 * \param output_len The length of the \p output buffer in Bytes. For AEAD
956 * ciphers, this must be at least \p ilen - \p tag_len.
957 * For NIST_KW, this must be at least \p ilen - 8.
958 * \param olen This will be filled with the actual number of Bytes
959 * written to the \p output buffer. This must point to a
960 * writable object of type \c size_t.
961 * \param tag_len The actual length of the authentication tag. For AEAD
962 * ciphers, this must match the constraints imposed by
Manuel Pégourié-Gonnardf2ffbc42020-12-01 09:57:55 +0100963 * the cipher used, and in particular must not be \c 0.
Manuel Pégourié-Gonnard9cc079d2020-11-25 12:57:47 +0100964 * For NIST_KW, this must be \c 0.
965 *
966 * \return \c 0 on success.
967 * \return #MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA on
968 * parameter-verification failure.
969 * \return #MBEDTLS_ERR_CIPHER_AUTH_FAILED if data is not authentic.
970 * \return A cipher-specific error code on failure.
971 */
972int mbedtls_cipher_auth_decrypt_ext( mbedtls_cipher_context_t *ctx,
973 const unsigned char *iv, size_t iv_len,
974 const unsigned char *ad, size_t ad_len,
975 const unsigned char *input, size_t ilen,
976 unsigned char *output, size_t output_len,
977 size_t *olen, size_t tag_len );
978#endif /* MBEDTLS_CIPHER_MODE_AEAD || MBEDTLS_NIST_KW_C */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000979#ifdef __cplusplus
980}
981#endif
982
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200983#endif /* MBEDTLS_CIPHER_H */