blob: 014786ad5193f29ca29c796d19ae8119c7bd9d1b [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
Paul Bakker8123e9d2011-01-06 15:37:30 +000029
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020030#if !defined(MBEDTLS_CONFIG_FILE)
Jaeden Ameroc49fbbf2019-07-04 20:01:14 +010031#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020032#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020033#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020034#endif
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +020035
Rich Evans00ab4702015-02-06 13:43:58 +000036#include <stddef.h>
Jaeden Ameroc49fbbf2019-07-04 20:01:14 +010037#include "mbedtls/platform_util.h"
Rich Evans00ab4702015-02-06 13:43:58 +000038
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +020039#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020040#define MBEDTLS_CIPHER_MODE_AEAD
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +020041#endif
42
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020043#if defined(MBEDTLS_CIPHER_MODE_CBC)
44#define MBEDTLS_CIPHER_MODE_WITH_PADDING
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +020045#endif
46
Simon Butcherc7965732018-07-27 17:13:39 +010047#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER) || \
48 defined(MBEDTLS_CHACHA20_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020049#define MBEDTLS_CIPHER_MODE_STREAM
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +010050#endif
51
Manuel Pégourié-Gonnard0223ab92015-10-05 11:40:01 +010052#if ( defined(__ARMCC_VERSION) || defined(_MSC_VER) ) && \
53 !defined(inline) && !defined(__cplusplus)
Paul Bakker569df2c2011-06-21 07:48:07 +000054#define inline __inline
Manuel Pégourié-Gonnard20af64d2015-07-07 18:33:39 +020055#endif
Paul Bakkeraf5c85f2011-04-18 03:47:52 +000056
Rose Zadik9ba6b622018-01-24 12:59:19 +000057#define MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE -0x6080 /**< The selected feature is not available. */
58#define MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA -0x6100 /**< Bad input parameters. */
59#define MBEDTLS_ERR_CIPHER_ALLOC_FAILED -0x6180 /**< Failed to allocate memory. */
60#define MBEDTLS_ERR_CIPHER_INVALID_PADDING -0x6200 /**< Input data contains invalid padding and is rejected. */
61#define MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED -0x6280 /**< Decryption of block requires a full block. */
62#define MBEDTLS_ERR_CIPHER_AUTH_FAILED -0x6300 /**< Authentication failed (for AEAD modes). */
63#define MBEDTLS_ERR_CIPHER_INVALID_CONTEXT -0x6380 /**< The context is invalid. For example, because it was freed. */
Ron Eldor9924bdc2018-10-04 10:59:13 +030064
65/* MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED is deprecated and should not be used. */
Rose Zadik9ba6b622018-01-24 12:59:19 +000066#define MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED -0x6400 /**< Cipher hardware accelerator failed. */
Paul Bakker343a8702011-06-09 14:27:58 +000067
Rose Zadik9ba6b622018-01-24 12:59:19 +000068#define MBEDTLS_CIPHER_VARIABLE_IV_LEN 0x01 /**< Cipher accepts IVs of variable length. */
69#define MBEDTLS_CIPHER_VARIABLE_KEY_LEN 0x02 /**< Cipher accepts keys of variable length. */
Manuel Pégourié-Gonnard81754a02014-06-23 11:33:18 +020070
Paul Bakker407a0da2013-06-27 14:29:21 +020071#ifdef __cplusplus
72extern "C" {
73#endif
74
Hanno Beckerbbca8c52017-09-25 14:53:51 +010075/**
Rose Zadik02f73a62018-03-26 18:02:32 +010076 * \brief Supported cipher types.
Hanno Beckerbbca8c52017-09-25 14:53:51 +010077 *
Rose Zadik826f2642018-04-19 14:01:29 +010078 * \warning RC4 and DES are considered weak ciphers and their use
Rose Zadikb5607bf2018-04-16 10:34:51 +010079 * constitutes a security risk. Arm recommends considering stronger
80 * ciphers instead.
Hanno Beckerbbca8c52017-09-25 14:53:51 +010081 */
Paul Bakker8123e9d2011-01-06 15:37:30 +000082typedef enum {
Rose Zadik4c368e82018-04-19 14:24:11 +010083 MBEDTLS_CIPHER_ID_NONE = 0, /**< Placeholder to mark the end of cipher ID lists. */
84 MBEDTLS_CIPHER_ID_NULL, /**< The identity cipher, treated as a stream cipher. */
Rose Zadik02f73a62018-03-26 18:02:32 +010085 MBEDTLS_CIPHER_ID_AES, /**< The AES cipher. */
86 MBEDTLS_CIPHER_ID_DES, /**< The DES cipher. */
Rose Zadik93f99192018-04-19 14:41:33 +010087 MBEDTLS_CIPHER_ID_3DES, /**< The Triple DES cipher. */
Rose Zadik02f73a62018-03-26 18:02:32 +010088 MBEDTLS_CIPHER_ID_CAMELLIA, /**< The Camellia cipher. */
89 MBEDTLS_CIPHER_ID_BLOWFISH, /**< The Blowfish cipher. */
Rose Zadik826f2642018-04-19 14:01:29 +010090 MBEDTLS_CIPHER_ID_ARC4, /**< The RC4 cipher. */
Manuel Pégourié-Gonnarda3712be2018-05-22 15:58:50 +020091 MBEDTLS_CIPHER_ID_ARIA, /**< The Aria cipher. */
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +020092 MBEDTLS_CIPHER_ID_CHACHA20, /**< The ChaCha20 cipher. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020093} mbedtls_cipher_id_t;
Paul Bakker8123e9d2011-01-06 15:37:30 +000094
Hanno Beckerbbca8c52017-09-25 14:53:51 +010095/**
Rose Zadik02f73a62018-03-26 18:02:32 +010096 * \brief Supported {cipher type, cipher mode} pairs.
Hanno Beckerbbca8c52017-09-25 14:53:51 +010097 *
Rose Zadik826f2642018-04-19 14:01:29 +010098 * \warning RC4 and DES are considered weak ciphers and their use
Rose Zadikb5607bf2018-04-16 10:34:51 +010099 * constitutes a security risk. Arm recommends considering stronger
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100100 * ciphers instead.
101 */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000102typedef enum {
Rose Zadik4c368e82018-04-19 14:24:11 +0100103 MBEDTLS_CIPHER_NONE = 0, /**< Placeholder to mark the end of cipher-pair lists. */
Rose Zadikc441f742018-04-19 14:38:20 +0100104 MBEDTLS_CIPHER_NULL, /**< The identity stream cipher. */
Rose Zadik02f73a62018-03-26 18:02:32 +0100105 MBEDTLS_CIPHER_AES_128_ECB, /**< AES cipher with 128-bit ECB mode. */
106 MBEDTLS_CIPHER_AES_192_ECB, /**< AES cipher with 192-bit ECB mode. */
107 MBEDTLS_CIPHER_AES_256_ECB, /**< AES cipher with 256-bit ECB mode. */
108 MBEDTLS_CIPHER_AES_128_CBC, /**< AES cipher with 128-bit CBC mode. */
109 MBEDTLS_CIPHER_AES_192_CBC, /**< AES cipher with 192-bit CBC mode. */
110 MBEDTLS_CIPHER_AES_256_CBC, /**< AES cipher with 256-bit CBC mode. */
111 MBEDTLS_CIPHER_AES_128_CFB128, /**< AES cipher with 128-bit CFB128 mode. */
112 MBEDTLS_CIPHER_AES_192_CFB128, /**< AES cipher with 192-bit CFB128 mode. */
113 MBEDTLS_CIPHER_AES_256_CFB128, /**< AES cipher with 256-bit CFB128 mode. */
114 MBEDTLS_CIPHER_AES_128_CTR, /**< AES cipher with 128-bit CTR mode. */
115 MBEDTLS_CIPHER_AES_192_CTR, /**< AES cipher with 192-bit CTR mode. */
116 MBEDTLS_CIPHER_AES_256_CTR, /**< AES cipher with 256-bit CTR mode. */
117 MBEDTLS_CIPHER_AES_128_GCM, /**< AES cipher with 128-bit GCM mode. */
118 MBEDTLS_CIPHER_AES_192_GCM, /**< AES cipher with 192-bit GCM mode. */
119 MBEDTLS_CIPHER_AES_256_GCM, /**< AES cipher with 256-bit GCM mode. */
120 MBEDTLS_CIPHER_CAMELLIA_128_ECB, /**< Camellia cipher with 128-bit ECB mode. */
121 MBEDTLS_CIPHER_CAMELLIA_192_ECB, /**< Camellia cipher with 192-bit ECB mode. */
122 MBEDTLS_CIPHER_CAMELLIA_256_ECB, /**< Camellia cipher with 256-bit ECB mode. */
123 MBEDTLS_CIPHER_CAMELLIA_128_CBC, /**< Camellia cipher with 128-bit CBC mode. */
124 MBEDTLS_CIPHER_CAMELLIA_192_CBC, /**< Camellia cipher with 192-bit CBC mode. */
125 MBEDTLS_CIPHER_CAMELLIA_256_CBC, /**< Camellia cipher with 256-bit CBC mode. */
126 MBEDTLS_CIPHER_CAMELLIA_128_CFB128, /**< Camellia cipher with 128-bit CFB128 mode. */
127 MBEDTLS_CIPHER_CAMELLIA_192_CFB128, /**< Camellia cipher with 192-bit CFB128 mode. */
128 MBEDTLS_CIPHER_CAMELLIA_256_CFB128, /**< Camellia cipher with 256-bit CFB128 mode. */
129 MBEDTLS_CIPHER_CAMELLIA_128_CTR, /**< Camellia cipher with 128-bit CTR mode. */
130 MBEDTLS_CIPHER_CAMELLIA_192_CTR, /**< Camellia cipher with 192-bit CTR mode. */
131 MBEDTLS_CIPHER_CAMELLIA_256_CTR, /**< Camellia cipher with 256-bit CTR mode. */
132 MBEDTLS_CIPHER_CAMELLIA_128_GCM, /**< Camellia cipher with 128-bit GCM mode. */
133 MBEDTLS_CIPHER_CAMELLIA_192_GCM, /**< Camellia cipher with 192-bit GCM mode. */
134 MBEDTLS_CIPHER_CAMELLIA_256_GCM, /**< Camellia cipher with 256-bit GCM mode. */
135 MBEDTLS_CIPHER_DES_ECB, /**< DES cipher with ECB mode. */
136 MBEDTLS_CIPHER_DES_CBC, /**< DES cipher with CBC mode. */
137 MBEDTLS_CIPHER_DES_EDE_ECB, /**< DES cipher with EDE ECB mode. */
138 MBEDTLS_CIPHER_DES_EDE_CBC, /**< DES cipher with EDE CBC mode. */
139 MBEDTLS_CIPHER_DES_EDE3_ECB, /**< DES cipher with EDE3 ECB mode. */
140 MBEDTLS_CIPHER_DES_EDE3_CBC, /**< DES cipher with EDE3 CBC mode. */
141 MBEDTLS_CIPHER_BLOWFISH_ECB, /**< Blowfish cipher with ECB mode. */
142 MBEDTLS_CIPHER_BLOWFISH_CBC, /**< Blowfish cipher with CBC mode. */
143 MBEDTLS_CIPHER_BLOWFISH_CFB64, /**< Blowfish cipher with CFB64 mode. */
144 MBEDTLS_CIPHER_BLOWFISH_CTR, /**< Blowfish cipher with CTR mode. */
Rose Zadik826f2642018-04-19 14:01:29 +0100145 MBEDTLS_CIPHER_ARC4_128, /**< RC4 cipher with 128-bit mode. */
Rose Zadik02f73a62018-03-26 18:02:32 +0100146 MBEDTLS_CIPHER_AES_128_CCM, /**< AES cipher with 128-bit CCM mode. */
147 MBEDTLS_CIPHER_AES_192_CCM, /**< AES cipher with 192-bit CCM mode. */
148 MBEDTLS_CIPHER_AES_256_CCM, /**< AES cipher with 256-bit CCM mode. */
149 MBEDTLS_CIPHER_CAMELLIA_128_CCM, /**< Camellia cipher with 128-bit CCM mode. */
150 MBEDTLS_CIPHER_CAMELLIA_192_CCM, /**< Camellia cipher with 192-bit CCM mode. */
151 MBEDTLS_CIPHER_CAMELLIA_256_CCM, /**< Camellia cipher with 256-bit CCM mode. */
Manuel Pégourié-Gonnarda3712be2018-05-22 15:58:50 +0200152 MBEDTLS_CIPHER_ARIA_128_ECB, /**< Aria cipher with 128-bit key and ECB mode. */
153 MBEDTLS_CIPHER_ARIA_192_ECB, /**< Aria cipher with 192-bit key and ECB mode. */
154 MBEDTLS_CIPHER_ARIA_256_ECB, /**< Aria cipher with 256-bit key and ECB mode. */
155 MBEDTLS_CIPHER_ARIA_128_CBC, /**< Aria cipher with 128-bit key and CBC mode. */
156 MBEDTLS_CIPHER_ARIA_192_CBC, /**< Aria cipher with 192-bit key and CBC mode. */
157 MBEDTLS_CIPHER_ARIA_256_CBC, /**< Aria cipher with 256-bit key and CBC mode. */
158 MBEDTLS_CIPHER_ARIA_128_CFB128, /**< Aria cipher with 128-bit key and CFB-128 mode. */
159 MBEDTLS_CIPHER_ARIA_192_CFB128, /**< Aria cipher with 192-bit key and CFB-128 mode. */
160 MBEDTLS_CIPHER_ARIA_256_CFB128, /**< Aria cipher with 256-bit key and CFB-128 mode. */
161 MBEDTLS_CIPHER_ARIA_128_CTR, /**< Aria cipher with 128-bit key and CTR mode. */
162 MBEDTLS_CIPHER_ARIA_192_CTR, /**< Aria cipher with 192-bit key and CTR mode. */
163 MBEDTLS_CIPHER_ARIA_256_CTR, /**< Aria cipher with 256-bit key and CTR mode. */
164 MBEDTLS_CIPHER_ARIA_128_GCM, /**< Aria cipher with 128-bit key and GCM mode. */
165 MBEDTLS_CIPHER_ARIA_192_GCM, /**< Aria cipher with 192-bit key and GCM mode. */
166 MBEDTLS_CIPHER_ARIA_256_GCM, /**< Aria cipher with 256-bit key and GCM mode. */
167 MBEDTLS_CIPHER_ARIA_128_CCM, /**< Aria cipher with 128-bit key and CCM mode. */
168 MBEDTLS_CIPHER_ARIA_192_CCM, /**< Aria cipher with 192-bit key and CCM mode. */
169 MBEDTLS_CIPHER_ARIA_256_CCM, /**< Aria cipher with 256-bit key and CCM mode. */
Simon Butcher6873c842018-04-29 13:03:20 +0100170 MBEDTLS_CIPHER_AES_128_OFB, /**< AES 128-bit cipher in OFB mode. */
171 MBEDTLS_CIPHER_AES_192_OFB, /**< AES 192-bit cipher in OFB mode. */
172 MBEDTLS_CIPHER_AES_256_OFB, /**< AES 256-bit cipher in OFB mode. */
Jaeden Ameroc6539902018-04-30 17:17:41 +0100173 MBEDTLS_CIPHER_AES_128_XTS, /**< AES 128-bit cipher in XTS block mode. */
174 MBEDTLS_CIPHER_AES_256_XTS, /**< AES 256-bit cipher in XTS block mode. */
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200175 MBEDTLS_CIPHER_CHACHA20, /**< ChaCha20 stream cipher. */
176 MBEDTLS_CIPHER_CHACHA20_POLY1305, /**< ChaCha20-Poly1305 AEAD cipher. */
Jack Lloydffdf2882019-03-07 17:00:32 -0500177 MBEDTLS_CIPHER_AES_128_KW, /**< AES cipher with 128-bit NIST KW mode. */
178 MBEDTLS_CIPHER_AES_192_KW, /**< AES cipher with 192-bit NIST KW mode. */
179 MBEDTLS_CIPHER_AES_256_KW, /**< AES cipher with 256-bit NIST KW mode. */
180 MBEDTLS_CIPHER_AES_128_KWP, /**< AES cipher with 128-bit NIST KWP mode. */
181 MBEDTLS_CIPHER_AES_192_KWP, /**< AES cipher with 192-bit NIST KWP mode. */
182 MBEDTLS_CIPHER_AES_256_KWP, /**< AES cipher with 256-bit NIST KWP mode. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200183} mbedtls_cipher_type_t;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000184
Rose Zadik9ba6b622018-01-24 12:59:19 +0000185/** Supported cipher modes. */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000186typedef enum {
Hanno Beckere5a04502018-11-08 16:57:42 +0000187 MBEDTLS_MODE_NONE = 0, /**< None. */
188 MBEDTLS_MODE_ECB, /**< The ECB cipher mode. */
189 MBEDTLS_MODE_CBC, /**< The CBC cipher mode. */
190 MBEDTLS_MODE_CFB, /**< The CFB cipher mode. */
191 MBEDTLS_MODE_OFB, /**< The OFB cipher mode. */
192 MBEDTLS_MODE_CTR, /**< The CTR cipher mode. */
193 MBEDTLS_MODE_GCM, /**< The GCM cipher mode. */
194 MBEDTLS_MODE_STREAM, /**< The stream cipher mode. */
195 MBEDTLS_MODE_CCM, /**< The CCM cipher mode. */
196 MBEDTLS_MODE_XTS, /**< The XTS cipher mode. */
Manuel Pégourié-Gonnardf57bf8b2018-06-18 11:14:09 +0200197 MBEDTLS_MODE_CHACHAPOLY, /**< The ChaCha-Poly cipher mode. */
Jack Lloydffdf2882019-03-07 17:00:32 -0500198 MBEDTLS_MODE_KW, /**< The SP800-38F KW mode */
199 MBEDTLS_MODE_KWP, /**< The SP800-38F KWP mode */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200200} mbedtls_cipher_mode_t;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000201
Rose Zadik9ba6b622018-01-24 12:59:19 +0000202/** Supported cipher padding types. */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000203typedef enum {
Rose Zadik9ba6b622018-01-24 12:59:19 +0000204 MBEDTLS_PADDING_PKCS7 = 0, /**< PKCS7 padding (default). */
205 MBEDTLS_PADDING_ONE_AND_ZEROS, /**< ISO/IEC 7816-4 padding. */
206 MBEDTLS_PADDING_ZEROS_AND_LEN, /**< ANSI X.923 padding. */
Rose Zadik02f73a62018-03-26 18:02:32 +0100207 MBEDTLS_PADDING_ZEROS, /**< Zero padding (not reversible). */
208 MBEDTLS_PADDING_NONE, /**< Never pad (full blocks only). */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200209} mbedtls_cipher_padding_t;
Manuel Pégourié-Gonnardd5fdcaf2013-07-24 18:05:00 +0200210
Rose Zadik9ba6b622018-01-24 12:59:19 +0000211/** Type of operation. */
Manuel Pégourié-Gonnardd5fdcaf2013-07-24 18:05:00 +0200212typedef enum {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200213 MBEDTLS_OPERATION_NONE = -1,
214 MBEDTLS_DECRYPT = 0,
215 MBEDTLS_ENCRYPT,
216} mbedtls_operation_t;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000217
218enum {
Rose Zadik9ba6b622018-01-24 12:59:19 +0000219 /** Undefined key length. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200220 MBEDTLS_KEY_LENGTH_NONE = 0,
Rose Zadik9ba6b622018-01-24 12:59:19 +0000221 /** Key length, in bits (including parity), for DES keys. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200222 MBEDTLS_KEY_LENGTH_DES = 64,
Rose Zadik9ba6b622018-01-24 12:59:19 +0000223 /** Key length in bits, including parity, for DES in two-key EDE. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200224 MBEDTLS_KEY_LENGTH_DES_EDE = 128,
Rose Zadik9ba6b622018-01-24 12:59:19 +0000225 /** Key length in bits, including parity, for DES in three-key EDE. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200226 MBEDTLS_KEY_LENGTH_DES_EDE3 = 192,
Paul Bakker8123e9d2011-01-06 15:37:30 +0000227};
228
Rose Zadik9ba6b622018-01-24 12:59:19 +0000229/** Maximum length of any IV, in Bytes. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200230#define MBEDTLS_MAX_IV_LENGTH 16
Rose Zadik9ba6b622018-01-24 12:59:19 +0000231/** Maximum block size of any cipher, in Bytes. */
Manuel Pégourié-Gonnard32902e62018-05-10 12:30:19 +0200232#define MBEDTLS_MAX_BLOCK_LENGTH 16
Manuel Pégourié-Gonnard0b58c152013-10-24 17:17:54 +0200233
Paul Bakker8123e9d2011-01-06 15:37:30 +0000234/**
Manuel Pégourié-Gonnard5a74e8b2015-05-06 17:10:55 +0100235 * Base cipher information (opaque struct).
Paul Bakker343a8702011-06-09 14:27:58 +0000236 */
Manuel Pégourié-Gonnard5a74e8b2015-05-06 17:10:55 +0100237typedef struct mbedtls_cipher_base_t mbedtls_cipher_base_t;
Paul Bakker343a8702011-06-09 14:27:58 +0000238
239/**
Simon Butcher327398a2016-10-05 14:09:11 +0100240 * CMAC context (opaque struct).
241 */
242typedef struct mbedtls_cmac_context_t mbedtls_cmac_context_t;
243
244/**
Rose Zadik9ba6b622018-01-24 12:59:19 +0000245 * Cipher information. Allows calling cipher functions
246 * in a generic way.
Paul Bakker8123e9d2011-01-06 15:37:30 +0000247 */
Dawid Drozd428cc522018-07-24 10:02:47 +0200248typedef struct mbedtls_cipher_info_t
249{
Rose Zadik9ba6b622018-01-24 12:59:19 +0000250 /** Full cipher identifier. For example,
251 * MBEDTLS_CIPHER_AES_256_CBC.
252 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200253 mbedtls_cipher_type_t type;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000254
Rose Zadik9ba6b622018-01-24 12:59:19 +0000255 /** The cipher mode. For example, MBEDTLS_MODE_CBC. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200256 mbedtls_cipher_mode_t mode;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000257
Rose Zadik9ba6b622018-01-24 12:59:19 +0000258 /** The cipher key length, in bits. This is the
259 * default length for variable sized ciphers.
260 * Includes parity bits for ciphers like DES.
261 */
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200262 unsigned int key_bitlen;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000263
Rose Zadik9ba6b622018-01-24 12:59:19 +0000264 /** Name of the cipher. */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000265 const char * name;
266
Rose Zadik9ba6b622018-01-24 12:59:19 +0000267 /** IV or nonce size, in Bytes.
268 * For ciphers that accept variable IV sizes,
269 * this is the recommended size.
270 */
Paul Bakker23986e52011-04-24 08:57:21 +0000271 unsigned int iv_size;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000272
Rose Zadikb5607bf2018-04-16 10:34:51 +0100273 /** Bitflag comprised of MBEDTLS_CIPHER_VARIABLE_IV_LEN and
274 * MBEDTLS_CIPHER_VARIABLE_KEY_LEN indicating whether the
275 * cipher supports variable IV or variable key sizes, respectively.
276 */
Manuel Pégourié-Gonnard81754a02014-06-23 11:33:18 +0200277 int flags;
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200278
Rose Zadik9ba6b622018-01-24 12:59:19 +0000279 /** The block size, in Bytes. */
Paul Bakker23986e52011-04-24 08:57:21 +0000280 unsigned int block_size;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000281
Rose Zadik9ba6b622018-01-24 12:59:19 +0000282 /** Struct for base cipher information and functions. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200283 const mbedtls_cipher_base_t *base;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000284
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200285} mbedtls_cipher_info_t;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000286
287/**
Paul Bakker20281562011-11-11 10:34:04 +0000288 * Generic cipher context.
Paul Bakker8123e9d2011-01-06 15:37:30 +0000289 */
Dawid Drozd428cc522018-07-24 10:02:47 +0200290typedef struct mbedtls_cipher_context_t
291{
Rose Zadik9ba6b622018-01-24 12:59:19 +0000292 /** Information about the associated cipher. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200293 const mbedtls_cipher_info_t *cipher_info;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000294
Rose Zadik9ba6b622018-01-24 12:59:19 +0000295 /** Key length to use. */
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200296 int key_bitlen;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000297
Rose Zadik9ba6b622018-01-24 12:59:19 +0000298 /** Operation that the key of the context has been
299 * initialized for.
300 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200301 mbedtls_operation_t operation;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000302
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200303#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Rose Zadik9ba6b622018-01-24 12:59:19 +0000304 /** Padding functions to use, if relevant for
305 * the specific cipher mode.
306 */
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200307 void (*add_padding)( unsigned char *output, size_t olen, size_t data_len );
308 int (*get_padding)( unsigned char *input, size_t ilen, size_t *data_len );
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +0100309#endif
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200310
Rose Zadik9ba6b622018-01-24 12:59:19 +0000311 /** Buffer for input that has not been processed yet. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200312 unsigned char unprocessed_data[MBEDTLS_MAX_BLOCK_LENGTH];
Paul Bakker8123e9d2011-01-06 15:37:30 +0000313
Rose Zadik9ba6b622018-01-24 12:59:19 +0000314 /** Number of Bytes that have not been processed yet. */
Paul Bakker23986e52011-04-24 08:57:21 +0000315 size_t unprocessed_len;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000316
Jaeden Ameroc6539902018-04-30 17:17:41 +0100317 /** Current IV or NONCE_COUNTER for CTR-mode, data unit (or sector) number
318 * for XTS-mode. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200319 unsigned char iv[MBEDTLS_MAX_IV_LENGTH];
Paul Bakker8123e9d2011-01-06 15:37:30 +0000320
Rose Zadik9ba6b622018-01-24 12:59:19 +0000321 /** IV size in Bytes, for ciphers with variable-length IVs. */
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200322 size_t iv_size;
323
Rose Zadik9ba6b622018-01-24 12:59:19 +0000324 /** The cipher-specific context. */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000325 void *cipher_ctx;
Simon Butcher327398a2016-10-05 14:09:11 +0100326
327#if defined(MBEDTLS_CMAC_C)
Rose Zadik9ba6b622018-01-24 12:59:19 +0000328 /** CMAC-specific context. */
Simon Butcher327398a2016-10-05 14:09:11 +0100329 mbedtls_cmac_context_t *cmac_ctx;
330#endif
Hanno Becker1cb36532018-11-09 16:20:29 +0000331
332#if defined(MBEDTLS_USE_PSA_CRYPTO)
333 /** Indicates whether the cipher operations should be performed
334 * by Mbed TLS' own crypto library or an external implementation
335 * of the PSA Crypto API.
Hanno Becker7909c4c2018-11-20 11:34:34 +0000336 * This is unset if the cipher context was established through
337 * mbedtls_cipher_setup(), and set if it was established through
Hanno Becker1cb36532018-11-09 16:20:29 +0000338 * mbedtls_cipher_setup_psa().
339 */
340 unsigned char psa_enabled;
341#endif /* MBEDTLS_USE_PSA_CRYPTO */
342
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200343} mbedtls_cipher_context_t;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000344
Paul Bakker8123e9d2011-01-06 15:37:30 +0000345/**
Hanno Beckerd7503a72018-11-08 15:55:24 +0000346 * \brief This function retrieves the list of ciphers supported
347 * by the generic cipher module.
Paul Bakker72f62662011-01-16 21:27:44 +0000348 *
Hanno Beckerd7503a72018-11-08 15:55:24 +0000349 * For any cipher identifier in the returned list, you can
350 * obtain the corresponding generic cipher information structure
351 * via mbedtls_cipher_info_from_type(), which can then be used
352 * to prepare a cipher context via mbedtls_cipher_setup().
353 *
354 *
355 * \return A statically-allocated array of cipher identifiers
356 * of type cipher_type_t. The last entry is zero.
Paul Bakker72f62662011-01-16 21:27:44 +0000357 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200358const int *mbedtls_cipher_list( void );
Paul Bakker72f62662011-01-16 21:27:44 +0000359
360/**
Rose Zadik9ba6b622018-01-24 12:59:19 +0000361 * \brief This function retrieves the cipher-information
362 * structure associated with the given cipher name.
Paul Bakker8123e9d2011-01-06 15:37:30 +0000363 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500364 * \param cipher_name Name of the cipher to search for. This must not be
365 * \c NULL.
Paul Bakker8123e9d2011-01-06 15:37:30 +0000366 *
Rose Zadik9ba6b622018-01-24 12:59:19 +0000367 * \return The cipher information structure associated with the
Rose Zadik02f73a62018-03-26 18:02:32 +0100368 * given \p cipher_name.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500369 * \return \c NULL if the associated cipher information is not found.
Paul Bakker8123e9d2011-01-06 15:37:30 +0000370 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200371const mbedtls_cipher_info_t *mbedtls_cipher_info_from_string( const char *cipher_name );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000372
373/**
Rose Zadik9ba6b622018-01-24 12:59:19 +0000374 * \brief This function retrieves the cipher-information
375 * structure associated with the given cipher type.
Paul Bakker8123e9d2011-01-06 15:37:30 +0000376 *
377 * \param cipher_type Type of the cipher to search for.
378 *
Rose Zadik9ba6b622018-01-24 12:59:19 +0000379 * \return The cipher information structure associated with the
Rose Zadikb5607bf2018-04-16 10:34:51 +0100380 * given \p cipher_type.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500381 * \return \c NULL if the associated cipher information is not found.
Paul Bakker8123e9d2011-01-06 15:37:30 +0000382 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200383const mbedtls_cipher_info_t *mbedtls_cipher_info_from_type( const mbedtls_cipher_type_t cipher_type );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000384
385/**
Rose Zadik9ba6b622018-01-24 12:59:19 +0000386 * \brief This function retrieves the cipher-information
387 * structure associated with the given cipher ID,
388 * key size and mode.
Paul Bakkerf46b6952013-09-09 00:08:26 +0200389 *
Rose Zadik9ba6b622018-01-24 12:59:19 +0000390 * \param cipher_id The ID of the cipher to search for. For example,
391 * #MBEDTLS_CIPHER_ID_AES.
392 * \param key_bitlen The length of the key in bits.
393 * \param mode The cipher mode. For example, #MBEDTLS_MODE_CBC.
Paul Bakkerf46b6952013-09-09 00:08:26 +0200394 *
Rose Zadik9ba6b622018-01-24 12:59:19 +0000395 * \return The cipher information structure associated with the
Rose Zadik02f73a62018-03-26 18:02:32 +0100396 * given \p cipher_id.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500397 * \return \c NULL if the associated cipher information is not found.
Paul Bakkerf46b6952013-09-09 00:08:26 +0200398 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200399const 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 +0200400 int key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200401 const mbedtls_cipher_mode_t mode );
Paul Bakkerf46b6952013-09-09 00:08:26 +0200402
403/**
Rose Zadik9ba6b622018-01-24 12:59:19 +0000404 * \brief This function initializes a \p cipher_context as NONE.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500405 *
406 * \param ctx The context to be initialized. This must not be \c NULL.
Paul Bakker84bbeb52014-07-01 14:53:22 +0200407 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200408void mbedtls_cipher_init( mbedtls_cipher_context_t *ctx );
Paul Bakker84bbeb52014-07-01 14:53:22 +0200409
410/**
Rose Zadik9ba6b622018-01-24 12:59:19 +0000411 * \brief This function frees and clears the cipher-specific
412 * context of \p ctx. Freeing \p ctx itself remains the
413 * responsibility of the caller.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500414 *
415 * \param ctx The context to be freed. If this is \c NULL, the
416 * function has no effect, otherwise this must point to an
417 * initialized context.
Paul Bakker84bbeb52014-07-01 14:53:22 +0200418 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200419void mbedtls_cipher_free( mbedtls_cipher_context_t *ctx );
Paul Bakker84bbeb52014-07-01 14:53:22 +0200420
Rose Zadik9ba6b622018-01-24 12:59:19 +0000421
Paul Bakker84bbeb52014-07-01 14:53:22 +0200422/**
Hanno Beckerb1f08722018-11-09 16:09:19 +0000423 * \brief This function initializes a cipher context for
424 * use with the given cipher primitive.
Paul Bakker8123e9d2011-01-06 15:37:30 +0000425 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500426 * \param ctx The context to initialize. This must be initialized.
Rose Zadik9ba6b622018-01-24 12:59:19 +0000427 * \param cipher_info The cipher to use.
Paul Bakker84bbeb52014-07-01 14:53:22 +0200428 *
Rose Zadik02f73a62018-03-26 18:02:32 +0100429 * \return \c 0 on success.
Rose Zadik4c368e82018-04-19 14:24:11 +0100430 * \return #MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA on
431 * parameter-verification failure.
Rose Zadik02f73a62018-03-26 18:02:32 +0100432 * \return #MBEDTLS_ERR_CIPHER_ALLOC_FAILED if allocation of the
Rose Zadik4c368e82018-04-19 14:24:11 +0100433 * cipher-specific context fails.
Rose Zadik9ba6b622018-01-24 12:59:19 +0000434 *
435 * \internal Currently, the function also clears the structure.
436 * In future versions, the caller will be required to call
437 * mbedtls_cipher_init() on the structure first.
Paul Bakker8123e9d2011-01-06 15:37:30 +0000438 */
Hanno Beckerf4fb8762018-11-08 15:57:42 +0000439int mbedtls_cipher_setup( mbedtls_cipher_context_t *ctx,
440 const mbedtls_cipher_info_t *cipher_info );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000441
Hanno Becker098c9de2018-11-09 16:10:57 +0000442#if defined(MBEDTLS_USE_PSA_CRYPTO)
443/**
444 * \brief This function initializes a cipher context for
445 * PSA-based use with the given cipher primitive.
446 *
447 * \note See #MBEDTLS_USE_PSA_CRYPTO for information on PSA.
448 *
449 * \param ctx The context to initialize. May not be \c NULL.
450 * \param cipher_info The cipher to use.
Hanno Beckerf1336402018-11-12 16:26:27 +0000451 * \param taglen For AEAD ciphers, the length in bytes of the
452 * authentication tag to use. Subsequent uses of
453 * mbedtls_cipher_auth_encrypt() or
454 * mbedtls_cipher_auth_decrypt() must provide
455 * the same tag length.
456 * For non-AEAD ciphers, the value must be \c 0.
Hanno Becker098c9de2018-11-09 16:10:57 +0000457 *
458 * \return \c 0 on success.
459 * \return #MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA on
460 * parameter-verification failure.
461 * \return #MBEDTLS_ERR_CIPHER_ALLOC_FAILED if allocation of the
462 * cipher-specific context fails.
463 */
464int mbedtls_cipher_setup_psa( mbedtls_cipher_context_t *ctx,
Hanno Beckerf1336402018-11-12 16:26:27 +0000465 const mbedtls_cipher_info_t *cipher_info,
466 size_t taglen );
Hanno Becker098c9de2018-11-09 16:10:57 +0000467#endif /* MBEDTLS_USE_PSA_CRYPTO */
468
Paul Bakker8123e9d2011-01-06 15:37:30 +0000469/**
Rose Zadik9ba6b622018-01-24 12:59:19 +0000470 * \brief This function returns the block size of the given cipher.
Paul Bakker8123e9d2011-01-06 15:37:30 +0000471 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500472 * \param ctx The context of the cipher. This must be initialized.
Paul Bakker8123e9d2011-01-06 15:37:30 +0000473 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500474 * \return The block size of the underlying cipher.
475 * \return \c 0 if \p ctx has not been initialized.
Paul Bakker8123e9d2011-01-06 15:37:30 +0000476 */
Hanno Beckerf4fb8762018-11-08 15:57:42 +0000477static inline unsigned int mbedtls_cipher_get_block_size(
478 const mbedtls_cipher_context_t *ctx )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000479{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500480 MBEDTLS_INTERNAL_VALIDATE_RET( ctx != NULL, 0 );
481 if( ctx->cipher_info == NULL )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000482 return 0;
483
484 return ctx->cipher_info->block_size;
485}
486
487/**
Rose Zadik9ba6b622018-01-24 12:59:19 +0000488 * \brief This function returns the mode of operation for
489 * the cipher. For example, MBEDTLS_MODE_CBC.
Paul Bakkerf7e5bb52011-11-11 10:53:37 +0000490 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500491 * \param ctx The context of the cipher. This must be initialized.
Paul Bakkerf7e5bb52011-11-11 10:53:37 +0000492 *
Rose Zadik02f73a62018-03-26 18:02:32 +0100493 * \return The mode of operation.
494 * \return #MBEDTLS_MODE_NONE if \p ctx has not been initialized.
Paul Bakkerf7e5bb52011-11-11 10:53:37 +0000495 */
Hanno Beckerf4fb8762018-11-08 15:57:42 +0000496static inline mbedtls_cipher_mode_t mbedtls_cipher_get_cipher_mode(
497 const mbedtls_cipher_context_t *ctx )
Paul Bakkerf7e5bb52011-11-11 10:53:37 +0000498{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500499 MBEDTLS_INTERNAL_VALIDATE_RET( ctx != NULL, MBEDTLS_MODE_NONE );
500 if( ctx->cipher_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200501 return MBEDTLS_MODE_NONE;
Paul Bakkerf7e5bb52011-11-11 10:53:37 +0000502
503 return ctx->cipher_info->mode;
504}
505
506/**
Rose Zadik9ba6b622018-01-24 12:59:19 +0000507 * \brief This function returns the size of the IV or nonce
508 * of the cipher, in Bytes.
Paul Bakker8123e9d2011-01-06 15:37:30 +0000509 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500510 * \param ctx The context of the cipher. This must be initialized.
Paul Bakker8123e9d2011-01-06 15:37:30 +0000511 *
Rose Zadikb5607bf2018-04-16 10:34:51 +0100512 * \return The recommended IV size if no IV has been set.
Rose Zadik4c368e82018-04-19 14:24:11 +0100513 * \return \c 0 for ciphers not using an IV or a nonce.
Rose Zadikb5607bf2018-04-16 10:34:51 +0100514 * \return The actual size if an IV has been set.
Paul Bakker8123e9d2011-01-06 15:37:30 +0000515 */
Hanno Beckerf4fb8762018-11-08 15:57:42 +0000516static inline int mbedtls_cipher_get_iv_size(
517 const mbedtls_cipher_context_t *ctx )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000518{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500519 MBEDTLS_INTERNAL_VALIDATE_RET( ctx != NULL, 0 );
520 if( ctx->cipher_info == NULL )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000521 return 0;
522
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200523 if( ctx->iv_size != 0 )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200524 return (int) ctx->iv_size;
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200525
Manuel Pégourié-Gonnard46c4fa12015-08-12 09:27:55 +0200526 return (int) ctx->cipher_info->iv_size;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000527}
528
529/**
Rose Zadik9ba6b622018-01-24 12:59:19 +0000530 * \brief This function returns the type of the given cipher.
Paul Bakker8123e9d2011-01-06 15:37:30 +0000531 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500532 * \param ctx The context of the cipher. This must be initialized.
Paul Bakker8123e9d2011-01-06 15:37:30 +0000533 *
Rose Zadik02f73a62018-03-26 18:02:32 +0100534 * \return The type of the cipher.
535 * \return #MBEDTLS_CIPHER_NONE if \p ctx has not been initialized.
Paul Bakker8123e9d2011-01-06 15:37:30 +0000536 */
Hanno Beckerf4fb8762018-11-08 15:57:42 +0000537static inline mbedtls_cipher_type_t mbedtls_cipher_get_type(
538 const mbedtls_cipher_context_t *ctx )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000539{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500540 MBEDTLS_INTERNAL_VALIDATE_RET(
541 ctx != NULL, MBEDTLS_CIPHER_NONE );
542 if( ctx->cipher_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200543 return MBEDTLS_CIPHER_NONE;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000544
545 return ctx->cipher_info->type;
546}
547
548/**
Rose Zadik9ba6b622018-01-24 12:59:19 +0000549 * \brief This function returns the name of the given cipher
550 * as a string.
Paul Bakker8123e9d2011-01-06 15:37:30 +0000551 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500552 * \param ctx The context of the cipher. This must be initialized.
Paul Bakker8123e9d2011-01-06 15:37:30 +0000553 *
Rose Zadik02f73a62018-03-26 18:02:32 +0100554 * \return The name of the cipher.
555 * \return NULL if \p ctx has not been not initialized.
Paul Bakker8123e9d2011-01-06 15:37:30 +0000556 */
Hanno Beckerf4fb8762018-11-08 15:57:42 +0000557static inline const char *mbedtls_cipher_get_name(
558 const mbedtls_cipher_context_t *ctx )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000559{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500560 MBEDTLS_INTERNAL_VALIDATE_RET( ctx != NULL, 0 );
561 if( ctx->cipher_info == NULL )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000562 return 0;
563
564 return ctx->cipher_info->name;
565}
566
567/**
Rose Zadik9ba6b622018-01-24 12:59:19 +0000568 * \brief This function returns the key length of the cipher.
Paul Bakker8123e9d2011-01-06 15:37:30 +0000569 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500570 * \param ctx The context of the cipher. This must be initialized.
Paul Bakker8123e9d2011-01-06 15:37:30 +0000571 *
Rose Zadik02f73a62018-03-26 18:02:32 +0100572 * \return The key length of the cipher in bits.
573 * \return #MBEDTLS_KEY_LENGTH_NONE if ctx \p has not been
Rose Zadik9ba6b622018-01-24 12:59:19 +0000574 * initialized.
Paul Bakker8123e9d2011-01-06 15:37:30 +0000575 */
Hanno Beckerf4fb8762018-11-08 15:57:42 +0000576static inline int mbedtls_cipher_get_key_bitlen(
577 const mbedtls_cipher_context_t *ctx )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000578{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500579 MBEDTLS_INTERNAL_VALIDATE_RET(
580 ctx != NULL, MBEDTLS_KEY_LENGTH_NONE );
581 if( ctx->cipher_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200582 return MBEDTLS_KEY_LENGTH_NONE;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000583
Manuel Pégourié-Gonnard46c4fa12015-08-12 09:27:55 +0200584 return (int) ctx->cipher_info->key_bitlen;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000585}
586
587/**
Rose Zadik9ba6b622018-01-24 12:59:19 +0000588 * \brief This function returns the operation of the given cipher.
Paul Bakkerf7e5bb52011-11-11 10:53:37 +0000589 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500590 * \param ctx The context of the cipher. This must be initialized.
Paul Bakkerf7e5bb52011-11-11 10:53:37 +0000591 *
Rose Zadikb5607bf2018-04-16 10:34:51 +0100592 * \return The type of operation: #MBEDTLS_ENCRYPT or #MBEDTLS_DECRYPT.
593 * \return #MBEDTLS_OPERATION_NONE if \p ctx has not been initialized.
Paul Bakkerf7e5bb52011-11-11 10:53:37 +0000594 */
Hanno Beckerf4fb8762018-11-08 15:57:42 +0000595static inline mbedtls_operation_t mbedtls_cipher_get_operation(
596 const mbedtls_cipher_context_t *ctx )
Paul Bakkerf7e5bb52011-11-11 10:53:37 +0000597{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500598 MBEDTLS_INTERNAL_VALIDATE_RET(
599 ctx != NULL, MBEDTLS_OPERATION_NONE );
600 if( ctx->cipher_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200601 return MBEDTLS_OPERATION_NONE;
Paul Bakkerf7e5bb52011-11-11 10:53:37 +0000602
603 return ctx->operation;
604}
605
606/**
Rose Zadik9ba6b622018-01-24 12:59:19 +0000607 * \brief This function sets the key to use with the given context.
Paul Bakker8123e9d2011-01-06 15:37:30 +0000608 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500609 * \param ctx The generic cipher context. This must be initialized and
610 * bound to a cipher information structure.
611 * \param key The key to use. This must be a readable buffer of at
612 * least \p key_bitlen Bits.
613 * \param key_bitlen The key length to use, in Bits.
Rose Zadik9ba6b622018-01-24 12:59:19 +0000614 * \param operation The operation that the key will be used for:
615 * #MBEDTLS_ENCRYPT or #MBEDTLS_DECRYPT.
Paul Bakker8123e9d2011-01-06 15:37:30 +0000616 *
Rose Zadikb5607bf2018-04-16 10:34:51 +0100617 * \return \c 0 on success.
Rose Zadik4c368e82018-04-19 14:24:11 +0100618 * \return #MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA on
619 * parameter-verification failure.
Rose Zadikb5607bf2018-04-16 10:34:51 +0100620 * \return A cipher-specific error code on failure.
Paul Bakker8123e9d2011-01-06 15:37:30 +0000621 */
Hanno Beckerf4fb8762018-11-08 15:57:42 +0000622int mbedtls_cipher_setkey( mbedtls_cipher_context_t *ctx,
623 const unsigned char *key,
624 int key_bitlen,
625 const mbedtls_operation_t operation );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000626
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200627#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Paul Bakker8123e9d2011-01-06 15:37:30 +0000628/**
Rose Zadik9ba6b622018-01-24 12:59:19 +0000629 * \brief This function sets the padding mode, for cipher modes
630 * that use padding.
Manuel Pégourié-Gonnardd5fdcaf2013-07-24 18:05:00 +0200631 *
Rose Zadik9ba6b622018-01-24 12:59:19 +0000632 * The default passing mode is PKCS7 padding.
Manuel Pégourié-Gonnardd5fdcaf2013-07-24 18:05:00 +0200633 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500634 * \param ctx The generic cipher context. This must be initialized and
635 * bound to a cipher information structure.
Rose Zadik9ba6b622018-01-24 12:59:19 +0000636 * \param mode The padding mode.
637 *
Rose Zadikb5607bf2018-04-16 10:34:51 +0100638 * \return \c 0 on success.
Rose Zadik02f73a62018-03-26 18:02:32 +0100639 * \return #MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE
640 * if the selected padding mode is not supported.
641 * \return #MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA if the cipher mode
Paul Bakker1a45d912013-08-14 12:04:26 +0200642 * does not support padding.
Manuel Pégourié-Gonnardd5fdcaf2013-07-24 18:05:00 +0200643 */
Hanno Beckerf4fb8762018-11-08 15:57:42 +0000644int mbedtls_cipher_set_padding_mode( mbedtls_cipher_context_t *ctx,
645 mbedtls_cipher_padding_t mode );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200646#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
Manuel Pégourié-Gonnardd5fdcaf2013-07-24 18:05:00 +0200647
648/**
Rose Zadik9ba6b622018-01-24 12:59:19 +0000649 * \brief This function sets the initialization vector (IV)
650 * or nonce.
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200651 *
Rose Zadik02f73a62018-03-26 18:02:32 +0100652 * \note Some ciphers do not use IVs nor nonce. For these
653 * ciphers, this function has no effect.
654 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500655 * \param ctx The generic cipher context. This must be initialized and
656 * bound to a cipher information structure.
657 * \param iv The IV to use, or NONCE_COUNTER for CTR-mode ciphers. This
658 * must be a readable buffer of at least \p iv_len Bytes.
Rose Zadik9ba6b622018-01-24 12:59:19 +0000659 * \param iv_len The IV length for ciphers with variable-size IV.
660 * This parameter is discarded by ciphers with fixed-size IV.
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200661 *
Rose Zadikb5607bf2018-04-16 10:34:51 +0100662 * \return \c 0 on success.
Rose Zadik4c368e82018-04-19 14:24:11 +0100663 * \return #MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA on
664 * parameter-verification failure.
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200665 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200666int mbedtls_cipher_set_iv( mbedtls_cipher_context_t *ctx,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500667 const unsigned char *iv,
668 size_t iv_len );
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200669
670/**
Rose Zadik9ba6b622018-01-24 12:59:19 +0000671 * \brief This function resets the cipher state.
Paul Bakker8123e9d2011-01-06 15:37:30 +0000672 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500673 * \param ctx The generic cipher context. This must be initialized.
Paul Bakker8123e9d2011-01-06 15:37:30 +0000674 *
Rose Zadikb5607bf2018-04-16 10:34:51 +0100675 * \return \c 0 on success.
Rose Zadik4c368e82018-04-19 14:24:11 +0100676 * \return #MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA on
677 * parameter-verification failure.
Paul Bakker8123e9d2011-01-06 15:37:30 +0000678 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200679int mbedtls_cipher_reset( mbedtls_cipher_context_t *ctx );
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200680
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200681#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200682/**
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500683 * \brief This function adds additional data for AEAD ciphers.
684 * Currently supported with GCM and ChaCha20+Poly1305.
685 * This must be called exactly once, after
686 * mbedtls_cipher_reset().
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200687 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500688 * \param ctx The generic cipher context. This must be initialized.
689 * \param ad The additional data to use. This must be a readable
690 * buffer of at least \p ad_len Bytes.
Andrzej Kurek02f39ac2019-02-08 06:50:55 -0500691 * \param ad_len The length of \p ad in Bytes.
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200692 *
Andrzej Kurek57f04e52019-02-08 06:44:43 -0500693 * \return \c 0 on success.
694 * \return A specific error code on failure.
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200695 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200696int mbedtls_cipher_update_ad( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200697 const unsigned char *ad, size_t ad_len );
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200698#endif /* MBEDTLS_GCM_C || MBEDTLS_CHACHAPOLY_C */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000699
700/**
Rose Zadik9ba6b622018-01-24 12:59:19 +0000701 * \brief The generic cipher update function. It encrypts or
702 * decrypts using the given cipher context. Writes as
703 * many block-sized blocks of data as possible to output.
704 * Any data that cannot be written immediately is either
705 * added to the next block, or flushed when
706 * mbedtls_cipher_finish() is called.
707 * Exception: For MBEDTLS_MODE_ECB, expects a single block
708 * in size. For example, 16 Bytes for AES.
Paul Bakker8123e9d2011-01-06 15:37:30 +0000709 *
Rose Zadik4c368e82018-04-19 14:24:11 +0100710 * \note If the underlying cipher is used in GCM mode, all calls
711 * to this function, except for the last one before
712 * mbedtls_cipher_finish(), must have \p ilen as a
713 * multiple of the block size of the cipher.
Rose Zadik02f73a62018-03-26 18:02:32 +0100714 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500715 * \param ctx The generic cipher context. This must be initialized and
716 * bound to a key.
717 * \param input The buffer holding the input data. This must be a
718 * readable buffer of at least \p ilen Bytes.
Rose Zadik9ba6b622018-01-24 12:59:19 +0000719 * \param ilen The length of the input data.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500720 * \param output The buffer for the output data. This must be able to
721 * hold at least `ilen + block_size`. This must not be the
722 * same buffer as \p input.
Rose Zadik9ba6b622018-01-24 12:59:19 +0000723 * \param olen The length of the output data, to be updated with the
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500724 * actual number of Bytes written. This must not be
725 * \c NULL.
Paul Bakker8123e9d2011-01-06 15:37:30 +0000726 *
Rose Zadikb5607bf2018-04-16 10:34:51 +0100727 * \return \c 0 on success.
Rose Zadik4c368e82018-04-19 14:24:11 +0100728 * \return #MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA on
729 * parameter-verification failure.
Rose Zadik02f73a62018-03-26 18:02:32 +0100730 * \return #MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE on an
731 * unsupported mode for a cipher.
Rose Zadikb5607bf2018-04-16 10:34:51 +0100732 * \return A cipher-specific error code on failure.
Paul Bakker8123e9d2011-01-06 15:37:30 +0000733 */
Hanno Beckerf4fb8762018-11-08 15:57:42 +0000734int mbedtls_cipher_update( mbedtls_cipher_context_t *ctx,
735 const unsigned char *input,
736 size_t ilen, unsigned char *output,
737 size_t *olen );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000738
739/**
Rose Zadik9ba6b622018-01-24 12:59:19 +0000740 * \brief The generic cipher finalization function. If data still
741 * needs to be flushed from an incomplete block, the data
742 * contained in it is padded to the size of
743 * the last block, and written to the \p output buffer.
Paul Bakker8123e9d2011-01-06 15:37:30 +0000744 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500745 * \param ctx The generic cipher context. This must be initialized and
746 * bound to a key.
747 * \param output The buffer to write data to. This needs to be a writable
748 * buffer of at least \p block_size Bytes.
Rose Zadik9ba6b622018-01-24 12:59:19 +0000749 * \param olen The length of the data written to the \p output buffer.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500750 * This may not be \c NULL.
Paul Bakker8123e9d2011-01-06 15:37:30 +0000751 *
Rose Zadikb5607bf2018-04-16 10:34:51 +0100752 * \return \c 0 on success.
Rose Zadik4c368e82018-04-19 14:24:11 +0100753 * \return #MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA on
754 * parameter-verification failure.
755 * \return #MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED on decryption
756 * expecting a full block but not receiving one.
Rose Zadik02f73a62018-03-26 18:02:32 +0100757 * \return #MBEDTLS_ERR_CIPHER_INVALID_PADDING on invalid padding
758 * while decrypting.
Rose Zadikb5607bf2018-04-16 10:34:51 +0100759 * \return A cipher-specific error code on failure.
Paul Bakker8123e9d2011-01-06 15:37:30 +0000760 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200761int mbedtls_cipher_finish( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200762 unsigned char *output, size_t *olen );
763
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200764#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200765/**
Rose Zadik9ba6b622018-01-24 12:59:19 +0000766 * \brief This function writes a tag for AEAD ciphers.
Daniel King8fe47012016-05-17 20:33:28 -0300767 * Currently supported with GCM and ChaCha20+Poly1305.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500768 * This must be called after mbedtls_cipher_finish().
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200769 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500770 * \param ctx The generic cipher context. This must be initialized,
771 * bound to a key, and have just completed a cipher
772 * operation through mbedtls_cipher_finish() the tag for
773 * which should be written.
774 * \param tag The buffer to write the tag to. This must be a writable
775 * buffer of at least \p tag_len Bytes.
Rose Zadik9ba6b622018-01-24 12:59:19 +0000776 * \param tag_len The length of the tag to write.
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200777 *
Rose Zadik02f73a62018-03-26 18:02:32 +0100778 * \return \c 0 on success.
779 * \return A specific error code on failure.
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200780 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200781int mbedtls_cipher_write_tag( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200782 unsigned char *tag, size_t tag_len );
783
784/**
Rose Zadik9ba6b622018-01-24 12:59:19 +0000785 * \brief This function checks the tag for AEAD ciphers.
Daniel King8fe47012016-05-17 20:33:28 -0300786 * Currently supported with GCM and ChaCha20+Poly1305.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500787 * This must be called after mbedtls_cipher_finish().
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200788 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500789 * \param ctx The generic cipher context. This must be initialized.
790 * \param tag The buffer holding the tag. This must be a readable
791 * buffer of at least \p tag_len Bytes.
Rose Zadik9ba6b622018-01-24 12:59:19 +0000792 * \param tag_len The length of the tag to check.
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200793 *
Rose Zadik02f73a62018-03-26 18:02:32 +0100794 * \return \c 0 on success.
795 * \return A specific error code on failure.
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200796 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200797int mbedtls_cipher_check_tag( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200798 const unsigned char *tag, size_t tag_len );
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200799#endif /* MBEDTLS_GCM_C || MBEDTLS_CHACHAPOLY_C */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000800
Paul Bakker8123e9d2011-01-06 15:37:30 +0000801/**
Rose Zadik9ba6b622018-01-24 12:59:19 +0000802 * \brief The generic all-in-one encryption/decryption function,
803 * for all ciphers except AEAD constructs.
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +0200804 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500805 * \param ctx The generic cipher context. This must be initialized.
Rose Zadik9ba6b622018-01-24 12:59:19 +0000806 * \param iv The IV to use, or NONCE_COUNTER for CTR-mode ciphers.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500807 * This must be a readable buffer of at least \p iv_len
808 * Bytes.
Rose Zadik9ba6b622018-01-24 12:59:19 +0000809 * \param iv_len The IV length for ciphers with variable-size IV.
810 * This parameter is discarded by ciphers with fixed-size
811 * IV.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500812 * \param input The buffer holding the input data. This must be a
813 * readable buffer of at least \p ilen Bytes.
814 * \param ilen The length of the input data in Bytes.
815 * \param output The buffer for the output data. This must be able to
816 * hold at least `ilen + block_size`. This must not be the
817 * same buffer as \p input.
Rose Zadik9ba6b622018-01-24 12:59:19 +0000818 * \param olen The length of the output data, to be updated with the
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500819 * actual number of Bytes written. This must not be
820 * \c NULL.
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +0200821 *
Rose Zadik9ba6b622018-01-24 12:59:19 +0000822 * \note Some ciphers do not use IVs nor nonce. For these
823 * ciphers, use \p iv = NULL and \p iv_len = 0.
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +0200824 *
Rose Zadikb5607bf2018-04-16 10:34:51 +0100825 * \return \c 0 on success.
Rose Zadik4c368e82018-04-19 14:24:11 +0100826 * \return #MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA on
827 * parameter-verification failure.
828 * \return #MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED on decryption
829 * expecting a full block but not receiving one.
Rose Zadik02f73a62018-03-26 18:02:32 +0100830 * \return #MBEDTLS_ERR_CIPHER_INVALID_PADDING on invalid padding
831 * while decrypting.
Rose Zadikb5607bf2018-04-16 10:34:51 +0100832 * \return A cipher-specific error code on failure.
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +0200833 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200834int mbedtls_cipher_crypt( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +0200835 const unsigned char *iv, size_t iv_len,
836 const unsigned char *input, size_t ilen,
837 unsigned char *output, size_t *olen );
838
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200839#if defined(MBEDTLS_CIPHER_MODE_AEAD)
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200840/**
Andrzej Kurek246cc862019-02-05 04:40:53 -0500841 * \brief The generic autenticated encryption (AEAD) function.
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200842 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500843 * \param ctx The generic cipher context. This must be initialized and
844 * bound to a key.
845 * \param iv The IV to use, or NONCE_COUNTER for CTR-mode ciphers.
846 * This must be a readable buffer of at least \p iv_len
847 * Bytes.
848 * \param iv_len The IV length for ciphers with variable-size IV.
849 * This parameter is discarded by ciphers with fixed-size IV.
850 * \param ad The additional data to authenticate. This must be a
851 * readable buffer of at least \p ad_len Bytes.
852 * \param ad_len The length of \p ad.
853 * \param input The buffer holding the input data. This must be a
854 * readable buffer of at least \p ilen Bytes.
855 * \param ilen The length of the input data.
856 * \param output The buffer for the output data. This must be able to
857 * hold at least \p ilen Bytes.
858 * \param olen The length of the output data, to be updated with the
859 * actual number of Bytes written. This must not be
860 * \c NULL.
861 * \param tag The buffer for the authentication tag. This must be a
862 * writable buffer of at least \p tag_len Bytes.
863 * \param tag_len The desired length of the authentication tag.
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200864 *
Andrzej Kurek246cc862019-02-05 04:40:53 -0500865 * \return \c 0 on success.
866 * \return #MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA on
867 * parameter-verification failure.
868 * \return A cipher-specific error code on failure.
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200869 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200870int mbedtls_cipher_auth_encrypt( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200871 const unsigned char *iv, size_t iv_len,
872 const unsigned char *ad, size_t ad_len,
873 const unsigned char *input, size_t ilen,
874 unsigned char *output, size_t *olen,
875 unsigned char *tag, size_t tag_len );
876
877/**
Andrzej Kurek246cc862019-02-05 04:40:53 -0500878 * \brief The generic autenticated decryption (AEAD) function.
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200879 *
Andrzej Kurek246cc862019-02-05 04:40:53 -0500880 * \note If the data is not authentic, then the output buffer
881 * is zeroed out to prevent the unauthentic plaintext being
882 * used, making this interface safer.
Rose Zadik02f73a62018-03-26 18:02:32 +0100883 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500884 * \param ctx The generic cipher context. This must be initialized and
885 * and bound to a key.
886 * \param iv The IV to use, or NONCE_COUNTER for CTR-mode ciphers.
887 * This must be a readable buffer of at least \p iv_len
888 * Bytes.
889 * \param iv_len The IV length for ciphers with variable-size IV.
890 * This parameter is discarded by ciphers with fixed-size IV.
891 * \param ad The additional data to be authenticated. This must be a
892 * readable buffer of at least \p ad_len Bytes.
893 * \param ad_len The length of \p ad.
894 * \param input The buffer holding the input data. This must be a
895 * readable buffer of at least \p ilen Bytes.
896 * \param ilen The length of the input data.
897 * \param output The buffer for the output data.
898 * This must be able to hold at least \p ilen Bytes.
899 * \param olen The length of the output data, to be updated with the
900 * actual number of Bytes written. This must not be
901 * \c NULL.
902 * \param tag The buffer holding the authentication tag. This must be
903 * a readable buffer of at least \p tag_len Bytes.
904 * \param tag_len The length of the authentication tag.
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200905 *
Andrzej Kurek246cc862019-02-05 04:40:53 -0500906 * \return \c 0 on success.
907 * \return #MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA on
908 * parameter-verification failure.
909 * \return #MBEDTLS_ERR_CIPHER_AUTH_FAILED if data is not authentic.
910 * \return A cipher-specific error code on failure.
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200911 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200912int mbedtls_cipher_auth_decrypt( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200913 const unsigned char *iv, size_t iv_len,
914 const unsigned char *ad, size_t ad_len,
915 const unsigned char *input, size_t ilen,
916 unsigned char *output, size_t *olen,
917 const unsigned char *tag, size_t tag_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200918#endif /* MBEDTLS_CIPHER_MODE_AEAD */
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200919
Paul Bakker8123e9d2011-01-06 15:37:30 +0000920#ifdef __cplusplus
921}
922#endif
923
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200924#endif /* MBEDTLS_CIPHER_H */