blob: 8830107df370181617bda4fc4a37b05f54647614 [file] [log] [blame]
Paul Bakker8123e9d2011-01-06 15:37:30 +00001/**
2 * \file cipher.h
Paul Bakker9af723c2014-05-01 13:03:14 +02003 *
Rose Zadik9ba6b622018-01-24 12:59:19 +00004 * \brief The generic cipher wrapper.
Paul Bakker8123e9d2011-01-06 15:37:30 +00005 *
6 * \author Adriaan de Jong <dejong@fox-it.com>
Darryl Greena40a1012018-01-05 15:33:17 +00007 */
8/*
Rose Zadik9ba6b622018-01-24 12:59:19 +00009 * Copyright (C) 2006-2018, Arm Limited (or its affiliates), All Rights Reserved
Bence Szépkúti4e9f7122020-06-05 13:02:18 +020010 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
11 *
12 * This file is provided under the Apache License 2.0, or the
13 * GNU General Public License v2.0 or later.
14 *
15 * **********
16 * Apache License 2.0:
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +020017 *
18 * Licensed under the Apache License, Version 2.0 (the "License"); you may
19 * not use this file except in compliance with the License.
20 * You may obtain a copy of the License at
21 *
22 * http://www.apache.org/licenses/LICENSE-2.0
23 *
24 * Unless required by applicable law or agreed to in writing, software
25 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
26 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
27 * See the License for the specific language governing permissions and
28 * limitations under the License.
Paul Bakker8123e9d2011-01-06 15:37:30 +000029 *
Bence Szépkúti4e9f7122020-06-05 13:02:18 +020030 * **********
31 *
32 * **********
33 * GNU General Public License v2.0 or later:
34 *
35 * This program is free software; you can redistribute it and/or modify
36 * it under the terms of the GNU General Public License as published by
37 * the Free Software Foundation; either version 2 of the License, or
38 * (at your option) any later version.
39 *
40 * This program is distributed in the hope that it will be useful,
41 * but WITHOUT ANY WARRANTY; without even the implied warranty of
42 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
43 * GNU General Public License for more details.
44 *
45 * You should have received a copy of the GNU General Public License along
46 * with this program; if not, write to the Free Software Foundation, Inc.,
47 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
48 *
49 * **********
50 *
Rose Zadik9ba6b622018-01-24 12:59:19 +000051 * This file is part of Mbed TLS (https://tls.mbed.org)
Paul Bakker8123e9d2011-01-06 15:37:30 +000052 */
53
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020054#ifndef MBEDTLS_CIPHER_H
55#define MBEDTLS_CIPHER_H
Paul Bakker8123e9d2011-01-06 15:37:30 +000056
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020057#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +020058#include "config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020059#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020060#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020061#endif
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +020062
Rich Evans00ab4702015-02-06 13:43:58 +000063#include <stddef.h>
64
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020065#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CCM_C)
66#define MBEDTLS_CIPHER_MODE_AEAD
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +020067#endif
68
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020069#if defined(MBEDTLS_CIPHER_MODE_CBC)
70#define MBEDTLS_CIPHER_MODE_WITH_PADDING
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +020071#endif
72
Ron Eldor0557b8f2018-06-27 18:33:13 +030073#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020074#define MBEDTLS_CIPHER_MODE_STREAM
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +010075#endif
76
Manuel Pégourié-Gonnard0223ab92015-10-05 11:40:01 +010077#if ( defined(__ARMCC_VERSION) || defined(_MSC_VER) ) && \
78 !defined(inline) && !defined(__cplusplus)
Paul Bakker569df2c2011-06-21 07:48:07 +000079#define inline __inline
Manuel Pégourié-Gonnard20af64d2015-07-07 18:33:39 +020080#endif
Paul Bakkeraf5c85f2011-04-18 03:47:52 +000081
Rose Zadik9ba6b622018-01-24 12:59:19 +000082#define MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE -0x6080 /**< The selected feature is not available. */
83#define MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA -0x6100 /**< Bad input parameters. */
84#define MBEDTLS_ERR_CIPHER_ALLOC_FAILED -0x6180 /**< Failed to allocate memory. */
85#define MBEDTLS_ERR_CIPHER_INVALID_PADDING -0x6200 /**< Input data contains invalid padding and is rejected. */
86#define MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED -0x6280 /**< Decryption of block requires a full block. */
87#define MBEDTLS_ERR_CIPHER_AUTH_FAILED -0x6300 /**< Authentication failed (for AEAD modes). */
88#define MBEDTLS_ERR_CIPHER_INVALID_CONTEXT -0x6380 /**< The context is invalid. For example, because it was freed. */
89#define MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED -0x6400 /**< Cipher hardware accelerator failed. */
Paul Bakker343a8702011-06-09 14:27:58 +000090
Rose Zadik9ba6b622018-01-24 12:59:19 +000091#define MBEDTLS_CIPHER_VARIABLE_IV_LEN 0x01 /**< Cipher accepts IVs of variable length. */
92#define MBEDTLS_CIPHER_VARIABLE_KEY_LEN 0x02 /**< Cipher accepts keys of variable length. */
Manuel Pégourié-Gonnard81754a02014-06-23 11:33:18 +020093
Paul Bakker407a0da2013-06-27 14:29:21 +020094#ifdef __cplusplus
95extern "C" {
96#endif
97
Hanno Beckerbbca8c52017-09-25 14:53:51 +010098/**
99 * \brief An enumeration of supported ciphers.
100 *
101 * \warning ARC4 and DES are considered weak ciphers and their use
102 * constitutes a security risk. We recommend considering stronger
103 * ciphers instead.
104 */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000105typedef enum {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200106 MBEDTLS_CIPHER_ID_NONE = 0,
107 MBEDTLS_CIPHER_ID_NULL,
108 MBEDTLS_CIPHER_ID_AES,
109 MBEDTLS_CIPHER_ID_DES,
110 MBEDTLS_CIPHER_ID_3DES,
111 MBEDTLS_CIPHER_ID_CAMELLIA,
112 MBEDTLS_CIPHER_ID_BLOWFISH,
113 MBEDTLS_CIPHER_ID_ARC4,
114} mbedtls_cipher_id_t;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000115
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100116/**
117 * \brief An enumeration of supported (cipher, mode) pairs.
118 *
119 * \warning ARC4 and DES are considered weak ciphers and their use
120 * constitutes a security risk. We recommend considering stronger
121 * ciphers instead.
122 */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000123typedef enum {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200124 MBEDTLS_CIPHER_NONE = 0,
125 MBEDTLS_CIPHER_NULL,
126 MBEDTLS_CIPHER_AES_128_ECB,
127 MBEDTLS_CIPHER_AES_192_ECB,
128 MBEDTLS_CIPHER_AES_256_ECB,
129 MBEDTLS_CIPHER_AES_128_CBC,
130 MBEDTLS_CIPHER_AES_192_CBC,
131 MBEDTLS_CIPHER_AES_256_CBC,
132 MBEDTLS_CIPHER_AES_128_CFB128,
133 MBEDTLS_CIPHER_AES_192_CFB128,
134 MBEDTLS_CIPHER_AES_256_CFB128,
135 MBEDTLS_CIPHER_AES_128_CTR,
136 MBEDTLS_CIPHER_AES_192_CTR,
137 MBEDTLS_CIPHER_AES_256_CTR,
138 MBEDTLS_CIPHER_AES_128_GCM,
139 MBEDTLS_CIPHER_AES_192_GCM,
140 MBEDTLS_CIPHER_AES_256_GCM,
141 MBEDTLS_CIPHER_CAMELLIA_128_ECB,
142 MBEDTLS_CIPHER_CAMELLIA_192_ECB,
143 MBEDTLS_CIPHER_CAMELLIA_256_ECB,
144 MBEDTLS_CIPHER_CAMELLIA_128_CBC,
145 MBEDTLS_CIPHER_CAMELLIA_192_CBC,
146 MBEDTLS_CIPHER_CAMELLIA_256_CBC,
147 MBEDTLS_CIPHER_CAMELLIA_128_CFB128,
148 MBEDTLS_CIPHER_CAMELLIA_192_CFB128,
149 MBEDTLS_CIPHER_CAMELLIA_256_CFB128,
150 MBEDTLS_CIPHER_CAMELLIA_128_CTR,
151 MBEDTLS_CIPHER_CAMELLIA_192_CTR,
152 MBEDTLS_CIPHER_CAMELLIA_256_CTR,
153 MBEDTLS_CIPHER_CAMELLIA_128_GCM,
154 MBEDTLS_CIPHER_CAMELLIA_192_GCM,
155 MBEDTLS_CIPHER_CAMELLIA_256_GCM,
156 MBEDTLS_CIPHER_DES_ECB,
157 MBEDTLS_CIPHER_DES_CBC,
158 MBEDTLS_CIPHER_DES_EDE_ECB,
159 MBEDTLS_CIPHER_DES_EDE_CBC,
160 MBEDTLS_CIPHER_DES_EDE3_ECB,
161 MBEDTLS_CIPHER_DES_EDE3_CBC,
162 MBEDTLS_CIPHER_BLOWFISH_ECB,
163 MBEDTLS_CIPHER_BLOWFISH_CBC,
164 MBEDTLS_CIPHER_BLOWFISH_CFB64,
165 MBEDTLS_CIPHER_BLOWFISH_CTR,
166 MBEDTLS_CIPHER_ARC4_128,
167 MBEDTLS_CIPHER_AES_128_CCM,
168 MBEDTLS_CIPHER_AES_192_CCM,
169 MBEDTLS_CIPHER_AES_256_CCM,
170 MBEDTLS_CIPHER_CAMELLIA_128_CCM,
171 MBEDTLS_CIPHER_CAMELLIA_192_CCM,
172 MBEDTLS_CIPHER_CAMELLIA_256_CCM,
173} mbedtls_cipher_type_t;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000174
Rose Zadik9ba6b622018-01-24 12:59:19 +0000175/** Supported cipher modes. */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000176typedef enum {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200177 MBEDTLS_MODE_NONE = 0,
178 MBEDTLS_MODE_ECB,
179 MBEDTLS_MODE_CBC,
180 MBEDTLS_MODE_CFB,
181 MBEDTLS_MODE_OFB, /* Unused! */
182 MBEDTLS_MODE_CTR,
183 MBEDTLS_MODE_GCM,
184 MBEDTLS_MODE_STREAM,
185 MBEDTLS_MODE_CCM,
186} mbedtls_cipher_mode_t;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000187
Rose Zadik9ba6b622018-01-24 12:59:19 +0000188/** Supported cipher padding types. */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000189typedef enum {
Rose Zadik9ba6b622018-01-24 12:59:19 +0000190 MBEDTLS_PADDING_PKCS7 = 0, /**< PKCS7 padding (default). */
191 MBEDTLS_PADDING_ONE_AND_ZEROS, /**< ISO/IEC 7816-4 padding. */
192 MBEDTLS_PADDING_ZEROS_AND_LEN, /**< ANSI X.923 padding. */
193 MBEDTLS_PADDING_ZEROS, /**< zero padding (not reversible). */
194 MBEDTLS_PADDING_NONE, /**< never pad (full blocks only). */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200195} mbedtls_cipher_padding_t;
Manuel Pégourié-Gonnardd5fdcaf2013-07-24 18:05:00 +0200196
Rose Zadik9ba6b622018-01-24 12:59:19 +0000197/** Type of operation. */
Manuel Pégourié-Gonnardd5fdcaf2013-07-24 18:05:00 +0200198typedef enum {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200199 MBEDTLS_OPERATION_NONE = -1,
200 MBEDTLS_DECRYPT = 0,
201 MBEDTLS_ENCRYPT,
202} mbedtls_operation_t;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000203
204enum {
Rose Zadik9ba6b622018-01-24 12:59:19 +0000205 /** Undefined key length. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200206 MBEDTLS_KEY_LENGTH_NONE = 0,
Rose Zadik9ba6b622018-01-24 12:59:19 +0000207 /** Key length, in bits (including parity), for DES keys. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200208 MBEDTLS_KEY_LENGTH_DES = 64,
Rose Zadik9ba6b622018-01-24 12:59:19 +0000209 /** Key length in bits, including parity, for DES in two-key EDE. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200210 MBEDTLS_KEY_LENGTH_DES_EDE = 128,
Rose Zadik9ba6b622018-01-24 12:59:19 +0000211 /** Key length in bits, including parity, for DES in three-key EDE. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200212 MBEDTLS_KEY_LENGTH_DES_EDE3 = 192,
Paul Bakker8123e9d2011-01-06 15:37:30 +0000213};
214
Rose Zadik9ba6b622018-01-24 12:59:19 +0000215/** Maximum length of any IV, in Bytes. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200216#define MBEDTLS_MAX_IV_LENGTH 16
Rose Zadik9ba6b622018-01-24 12:59:19 +0000217/** Maximum block size of any cipher, in Bytes. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200218#define MBEDTLS_MAX_BLOCK_LENGTH 16
Manuel Pégourié-Gonnard0b58c152013-10-24 17:17:54 +0200219
Paul Bakker8123e9d2011-01-06 15:37:30 +0000220/**
Manuel Pégourié-Gonnard5a74e8b2015-05-06 17:10:55 +0100221 * Base cipher information (opaque struct).
Paul Bakker343a8702011-06-09 14:27:58 +0000222 */
Manuel Pégourié-Gonnard5a74e8b2015-05-06 17:10:55 +0100223typedef struct mbedtls_cipher_base_t mbedtls_cipher_base_t;
Paul Bakker343a8702011-06-09 14:27:58 +0000224
225/**
Simon Butcher327398a2016-10-05 14:09:11 +0100226 * CMAC context (opaque struct).
227 */
228typedef struct mbedtls_cmac_context_t mbedtls_cmac_context_t;
229
230/**
Rose Zadik9ba6b622018-01-24 12:59:19 +0000231 * Cipher information. Allows calling cipher functions
232 * in a generic way.
Paul Bakker8123e9d2011-01-06 15:37:30 +0000233 */
234typedef struct {
Rose Zadik9ba6b622018-01-24 12:59:19 +0000235 /** Full cipher identifier. For example,
236 * MBEDTLS_CIPHER_AES_256_CBC.
237 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200238 mbedtls_cipher_type_t type;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000239
Rose Zadik9ba6b622018-01-24 12:59:19 +0000240 /** The cipher mode. For example, MBEDTLS_MODE_CBC. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200241 mbedtls_cipher_mode_t mode;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000242
Rose Zadik9ba6b622018-01-24 12:59:19 +0000243 /** The cipher key length, in bits. This is the
244 * default length for variable sized ciphers.
245 * Includes parity bits for ciphers like DES.
246 */
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200247 unsigned int key_bitlen;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000248
Rose Zadik9ba6b622018-01-24 12:59:19 +0000249 /** Name of the cipher. */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000250 const char * name;
251
Rose Zadik9ba6b622018-01-24 12:59:19 +0000252 /** IV or nonce size, in Bytes.
253 * For ciphers that accept variable IV sizes,
254 * this is the recommended size.
255 */
Paul Bakker23986e52011-04-24 08:57:21 +0000256 unsigned int iv_size;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000257
Rose Zadik9ba6b622018-01-24 12:59:19 +0000258 /** Flags to set. For example, if the cipher supports variable IV sizes or variable key sizes. */
Manuel Pégourié-Gonnard81754a02014-06-23 11:33:18 +0200259 int flags;
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200260
Rose Zadik9ba6b622018-01-24 12:59:19 +0000261 /** The block size, in Bytes. */
Paul Bakker23986e52011-04-24 08:57:21 +0000262 unsigned int block_size;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000263
Rose Zadik9ba6b622018-01-24 12:59:19 +0000264 /** Struct for base cipher information and functions. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200265 const mbedtls_cipher_base_t *base;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000266
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200267} mbedtls_cipher_info_t;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000268
269/**
Paul Bakker20281562011-11-11 10:34:04 +0000270 * Generic cipher context.
Paul Bakker8123e9d2011-01-06 15:37:30 +0000271 */
272typedef struct {
Rose Zadik9ba6b622018-01-24 12:59:19 +0000273 /** Information about the associated cipher. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200274 const mbedtls_cipher_info_t *cipher_info;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000275
Rose Zadik9ba6b622018-01-24 12:59:19 +0000276 /** Key length to use. */
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200277 int key_bitlen;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000278
Rose Zadik9ba6b622018-01-24 12:59:19 +0000279 /** Operation that the key of the context has been
280 * initialized for.
281 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200282 mbedtls_operation_t operation;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000283
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200284#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Rose Zadik9ba6b622018-01-24 12:59:19 +0000285 /** Padding functions to use, if relevant for
286 * the specific cipher mode.
287 */
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200288 void (*add_padding)( unsigned char *output, size_t olen, size_t data_len );
289 int (*get_padding)( unsigned char *input, size_t ilen, size_t *data_len );
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +0100290#endif
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200291
Rose Zadik9ba6b622018-01-24 12:59:19 +0000292 /** Buffer for input that has not been processed yet. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200293 unsigned char unprocessed_data[MBEDTLS_MAX_BLOCK_LENGTH];
Paul Bakker8123e9d2011-01-06 15:37:30 +0000294
Rose Zadik9ba6b622018-01-24 12:59:19 +0000295 /** Number of Bytes that have not been processed yet. */
Paul Bakker23986e52011-04-24 08:57:21 +0000296 size_t unprocessed_len;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000297
Rose Zadik9ba6b622018-01-24 12:59:19 +0000298 /** Current IV or NONCE_COUNTER for CTR-mode. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200299 unsigned char iv[MBEDTLS_MAX_IV_LENGTH];
Paul Bakker8123e9d2011-01-06 15:37:30 +0000300
Rose Zadik9ba6b622018-01-24 12:59:19 +0000301 /** IV size in Bytes, for ciphers with variable-length IVs. */
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200302 size_t iv_size;
303
Rose Zadik9ba6b622018-01-24 12:59:19 +0000304 /** The cipher-specific context. */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000305 void *cipher_ctx;
Simon Butcher327398a2016-10-05 14:09:11 +0100306
307#if defined(MBEDTLS_CMAC_C)
Rose Zadik9ba6b622018-01-24 12:59:19 +0000308 /** CMAC-specific context. */
Simon Butcher327398a2016-10-05 14:09:11 +0100309 mbedtls_cmac_context_t *cmac_ctx;
310#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200311} mbedtls_cipher_context_t;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000312
Paul Bakker8123e9d2011-01-06 15:37:30 +0000313/**
Rose Zadik9ba6b622018-01-24 12:59:19 +0000314 * \brief This function retrieves the list of ciphers supported by the generic
315 * cipher module.
Paul Bakker72f62662011-01-16 21:27:44 +0000316 *
Rose Zadik9ba6b622018-01-24 12:59:19 +0000317 * \return A statically-allocated array of ciphers. The last entry
318 * is zero.
Paul Bakker72f62662011-01-16 21:27:44 +0000319 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200320const int *mbedtls_cipher_list( void );
Paul Bakker72f62662011-01-16 21:27:44 +0000321
322/**
Rose Zadik9ba6b622018-01-24 12:59:19 +0000323 * \brief This function retrieves the cipher-information
324 * structure associated with the given cipher name.
Paul Bakker8123e9d2011-01-06 15:37:30 +0000325 *
Paul Bakker23986e52011-04-24 08:57:21 +0000326 * \param cipher_name Name of the cipher to search for.
Paul Bakker8123e9d2011-01-06 15:37:30 +0000327 *
Rose Zadik9ba6b622018-01-24 12:59:19 +0000328 * \return The cipher information structure associated with the
329 * given \p cipher_name, or NULL if not found.
Paul Bakker8123e9d2011-01-06 15:37:30 +0000330 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200331const mbedtls_cipher_info_t *mbedtls_cipher_info_from_string( const char *cipher_name );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000332
333/**
Rose Zadik9ba6b622018-01-24 12:59:19 +0000334 * \brief This function retrieves the cipher-information
335 * structure associated with the given cipher type.
Paul Bakker8123e9d2011-01-06 15:37:30 +0000336 *
337 * \param cipher_type Type of the cipher to search for.
338 *
Rose Zadik9ba6b622018-01-24 12:59:19 +0000339 * \return The cipher information structure associated with the
340 * given \p cipher_type, or NULL if not found.
Paul Bakker8123e9d2011-01-06 15:37:30 +0000341 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200342const mbedtls_cipher_info_t *mbedtls_cipher_info_from_type( const mbedtls_cipher_type_t cipher_type );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000343
344/**
Rose Zadik9ba6b622018-01-24 12:59:19 +0000345 * \brief This function retrieves the cipher-information
346 * structure associated with the given cipher ID,
347 * key size and mode.
Paul Bakkerf46b6952013-09-09 00:08:26 +0200348 *
Rose Zadik9ba6b622018-01-24 12:59:19 +0000349 * \param cipher_id The ID of the cipher to search for. For example,
350 * #MBEDTLS_CIPHER_ID_AES.
351 * \param key_bitlen The length of the key in bits.
352 * \param mode The cipher mode. For example, #MBEDTLS_MODE_CBC.
Paul Bakkerf46b6952013-09-09 00:08:26 +0200353 *
Rose Zadik9ba6b622018-01-24 12:59:19 +0000354 * \return The cipher information structure associated with the
355 * given \p cipher_id, or NULL if not found.
Paul Bakkerf46b6952013-09-09 00:08:26 +0200356 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200357const 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 +0200358 int key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200359 const mbedtls_cipher_mode_t mode );
Paul Bakkerf46b6952013-09-09 00:08:26 +0200360
361/**
Rose Zadik9ba6b622018-01-24 12:59:19 +0000362 * \brief This function initializes a \p cipher_context as NONE.
Paul Bakker84bbeb52014-07-01 14:53:22 +0200363 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200364void mbedtls_cipher_init( mbedtls_cipher_context_t *ctx );
Paul Bakker84bbeb52014-07-01 14:53:22 +0200365
366/**
Rose Zadik9ba6b622018-01-24 12:59:19 +0000367 * \brief This function frees and clears the cipher-specific
368 * context of \p ctx. Freeing \p ctx itself remains the
369 * responsibility of the caller.
Paul Bakker84bbeb52014-07-01 14:53:22 +0200370 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200371void mbedtls_cipher_free( mbedtls_cipher_context_t *ctx );
Paul Bakker84bbeb52014-07-01 14:53:22 +0200372
Rose Zadik9ba6b622018-01-24 12:59:19 +0000373
Paul Bakker84bbeb52014-07-01 14:53:22 +0200374/**
Rose Zadik9ba6b622018-01-24 12:59:19 +0000375 * \brief This function initializes and fills the cipher-context
376 * structure with the appropriate values. It also clears
377 * the structure.
Paul Bakker8123e9d2011-01-06 15:37:30 +0000378 *
Rose Zadik9ba6b622018-01-24 12:59:19 +0000379 * \param ctx The context to initialize. May not be NULL.
380 * \param cipher_info The cipher to use.
Paul Bakker84bbeb52014-07-01 14:53:22 +0200381 *
Rose Zadik9ba6b622018-01-24 12:59:19 +0000382 * \return \c 0 on success,
383 * #MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA on parameter failure,
384 * #MBEDTLS_ERR_CIPHER_ALLOC_FAILED if allocation of the
Paul Bakkerff61a782011-06-09 15:42:02 +0000385 * cipher-specific context failed.
Rose Zadik9ba6b622018-01-24 12:59:19 +0000386 *
387 * \internal Currently, the function also clears the structure.
388 * In future versions, the caller will be required to call
389 * mbedtls_cipher_init() on the structure first.
Paul Bakker8123e9d2011-01-06 15:37:30 +0000390 */
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +0200391int mbedtls_cipher_setup( mbedtls_cipher_context_t *ctx, const mbedtls_cipher_info_t *cipher_info );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000392
393/**
Rose Zadik9ba6b622018-01-24 12:59:19 +0000394 * \brief This function returns the block size of the given cipher.
Paul Bakker8123e9d2011-01-06 15:37:30 +0000395 *
Rose Zadik9ba6b622018-01-24 12:59:19 +0000396 * \param ctx The context of the cipher. Must be initialized.
Paul Bakker8123e9d2011-01-06 15:37:30 +0000397 *
Rose Zadik9ba6b622018-01-24 12:59:19 +0000398 * \return The size of the blocks of the cipher, or zero if \p ctx
399 * has not been initialized.
Paul Bakker8123e9d2011-01-06 15:37:30 +0000400 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200401static inline unsigned int mbedtls_cipher_get_block_size( const mbedtls_cipher_context_t *ctx )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000402{
403 if( NULL == ctx || NULL == ctx->cipher_info )
404 return 0;
405
406 return ctx->cipher_info->block_size;
407}
408
409/**
Rose Zadik9ba6b622018-01-24 12:59:19 +0000410 * \brief This function returns the mode of operation for
411 * the cipher. For example, MBEDTLS_MODE_CBC.
Paul Bakkerf7e5bb52011-11-11 10:53:37 +0000412 *
Rose Zadik9ba6b622018-01-24 12:59:19 +0000413 * \param ctx The context of the cipher. Must be initialized.
Paul Bakkerf7e5bb52011-11-11 10:53:37 +0000414 *
Rose Zadik9ba6b622018-01-24 12:59:19 +0000415 * \return The mode of operation, or #MBEDTLS_MODE_NONE if
416 * \p ctx has not been initialized.
Paul Bakkerf7e5bb52011-11-11 10:53:37 +0000417 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200418static inline mbedtls_cipher_mode_t mbedtls_cipher_get_cipher_mode( const mbedtls_cipher_context_t *ctx )
Paul Bakkerf7e5bb52011-11-11 10:53:37 +0000419{
420 if( NULL == ctx || NULL == ctx->cipher_info )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200421 return MBEDTLS_MODE_NONE;
Paul Bakkerf7e5bb52011-11-11 10:53:37 +0000422
423 return ctx->cipher_info->mode;
424}
425
426/**
Rose Zadik9ba6b622018-01-24 12:59:19 +0000427 * \brief This function returns the size of the IV or nonce
428 * of the cipher, in Bytes.
Paul Bakker8123e9d2011-01-06 15:37:30 +0000429 *
Rose Zadik9ba6b622018-01-24 12:59:19 +0000430 * \param ctx The context of the cipher. Must be initialized.
Paul Bakker8123e9d2011-01-06 15:37:30 +0000431 *
Rose Zadik9ba6b622018-01-24 12:59:19 +0000432 * \return <ul><li>If no IV has been set: the recommended IV size.
433 * 0 for ciphers not using IV or nonce.</li>
434 * <li>If IV has already been set: the actual size.</li></ul>
Paul Bakker8123e9d2011-01-06 15:37:30 +0000435 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200436static inline int mbedtls_cipher_get_iv_size( const mbedtls_cipher_context_t *ctx )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000437{
438 if( NULL == ctx || NULL == ctx->cipher_info )
439 return 0;
440
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200441 if( ctx->iv_size != 0 )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200442 return (int) ctx->iv_size;
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200443
Manuel Pégourié-Gonnard46c4fa12015-08-12 09:27:55 +0200444 return (int) ctx->cipher_info->iv_size;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000445}
446
447/**
Rose Zadik9ba6b622018-01-24 12:59:19 +0000448 * \brief This function returns the type of the given cipher.
Paul Bakker8123e9d2011-01-06 15:37:30 +0000449 *
Rose Zadik9ba6b622018-01-24 12:59:19 +0000450 * \param ctx The context of the cipher. Must be initialized.
Paul Bakker8123e9d2011-01-06 15:37:30 +0000451 *
Rose Zadik9ba6b622018-01-24 12:59:19 +0000452 * \return The type of the cipher, or #MBEDTLS_CIPHER_NONE if
453 * \p ctx has not been initialized.
Paul Bakker8123e9d2011-01-06 15:37:30 +0000454 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200455static inline mbedtls_cipher_type_t mbedtls_cipher_get_type( const mbedtls_cipher_context_t *ctx )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000456{
457 if( NULL == ctx || NULL == ctx->cipher_info )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200458 return MBEDTLS_CIPHER_NONE;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000459
460 return ctx->cipher_info->type;
461}
462
463/**
Rose Zadik9ba6b622018-01-24 12:59:19 +0000464 * \brief This function returns the name of the given cipher
465 * as a string.
Paul Bakker8123e9d2011-01-06 15:37:30 +0000466 *
Rose Zadik9ba6b622018-01-24 12:59:19 +0000467 * \param ctx The context of the cipher. Must be initialized.
Paul Bakker8123e9d2011-01-06 15:37:30 +0000468 *
Rose Zadik9ba6b622018-01-24 12:59:19 +0000469 * \return The name of the cipher, or NULL if \p ctx has not
470 * been not initialized.
Paul Bakker8123e9d2011-01-06 15:37:30 +0000471 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200472static inline const char *mbedtls_cipher_get_name( const mbedtls_cipher_context_t *ctx )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000473{
474 if( NULL == ctx || NULL == ctx->cipher_info )
475 return 0;
476
477 return ctx->cipher_info->name;
478}
479
480/**
Rose Zadik9ba6b622018-01-24 12:59:19 +0000481 * \brief This function returns the key length of the cipher.
Paul Bakker8123e9d2011-01-06 15:37:30 +0000482 *
Rose Zadik9ba6b622018-01-24 12:59:19 +0000483 * \param ctx The context of the cipher. Must be initialized.
Paul Bakker8123e9d2011-01-06 15:37:30 +0000484 *
Rose Zadik9ba6b622018-01-24 12:59:19 +0000485 * \return The key length of the cipher in bits, or
486 * #MBEDTLS_KEY_LENGTH_NONE if ctx \p has not been
487 * initialized.
Paul Bakker8123e9d2011-01-06 15:37:30 +0000488 */
Manuel Pégourié-Gonnard097c7bb2015-06-18 16:43:38 +0200489static inline int mbedtls_cipher_get_key_bitlen( const mbedtls_cipher_context_t *ctx )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000490{
Paul Bakker68884e32013-01-07 18:20:04 +0100491 if( NULL == ctx || NULL == ctx->cipher_info )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200492 return MBEDTLS_KEY_LENGTH_NONE;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000493
Manuel Pégourié-Gonnard46c4fa12015-08-12 09:27:55 +0200494 return (int) ctx->cipher_info->key_bitlen;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000495}
496
497/**
Rose Zadik9ba6b622018-01-24 12:59:19 +0000498 * \brief This function returns the operation of the given cipher.
Paul Bakkerf7e5bb52011-11-11 10:53:37 +0000499 *
Rose Zadik9ba6b622018-01-24 12:59:19 +0000500 * \param ctx The context of the cipher. Must be initialized.
Paul Bakkerf7e5bb52011-11-11 10:53:37 +0000501 *
Rose Zadik9ba6b622018-01-24 12:59:19 +0000502 * \return The type of operation: #MBEDTLS_ENCRYPT or
503 * #MBEDTLS_DECRYPT, or #MBEDTLS_OPERATION_NONE if \p ctx
504 * has not been initialized.
Paul Bakkerf7e5bb52011-11-11 10:53:37 +0000505 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200506static inline mbedtls_operation_t mbedtls_cipher_get_operation( const mbedtls_cipher_context_t *ctx )
Paul Bakkerf7e5bb52011-11-11 10:53:37 +0000507{
508 if( NULL == ctx || NULL == ctx->cipher_info )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200509 return MBEDTLS_OPERATION_NONE;
Paul Bakkerf7e5bb52011-11-11 10:53:37 +0000510
511 return ctx->operation;
512}
513
514/**
Rose Zadik9ba6b622018-01-24 12:59:19 +0000515 * \brief This function sets the key to use with the given context.
Paul Bakker8123e9d2011-01-06 15:37:30 +0000516 *
Rose Zadik9ba6b622018-01-24 12:59:19 +0000517 * \param ctx The generic cipher context. May not be NULL. Must have
518 * been initialized using mbedtls_cipher_info_from_type()
519 * or mbedtls_cipher_info_from_string().
Paul Bakker8123e9d2011-01-06 15:37:30 +0000520 * \param key The key to use.
Rose Zadik9ba6b622018-01-24 12:59:19 +0000521 * \param key_bitlen The key length to use, in bits.
522 * \param operation The operation that the key will be used for:
523 * #MBEDTLS_ENCRYPT or #MBEDTLS_DECRYPT.
Paul Bakker8123e9d2011-01-06 15:37:30 +0000524 *
Rose Zadik9ba6b622018-01-24 12:59:19 +0000525 * \returns \c 0 on success, #MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA if
526 * parameter verification fails, or a cipher-specific
Paul Bakkerff61a782011-06-09 15:42:02 +0000527 * error code.
Paul Bakker8123e9d2011-01-06 15:37:30 +0000528 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200529int mbedtls_cipher_setkey( mbedtls_cipher_context_t *ctx, const unsigned char *key,
Manuel Pégourié-Gonnardb8186a52015-06-18 14:58:58 +0200530 int key_bitlen, const mbedtls_operation_t operation );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000531
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200532#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Paul Bakker8123e9d2011-01-06 15:37:30 +0000533/**
Rose Zadik9ba6b622018-01-24 12:59:19 +0000534 * \brief This function sets the padding mode, for cipher modes
535 * that use padding.
Manuel Pégourié-Gonnardd5fdcaf2013-07-24 18:05:00 +0200536 *
Rose Zadik9ba6b622018-01-24 12:59:19 +0000537 * The default passing mode is PKCS7 padding.
Manuel Pégourié-Gonnardd5fdcaf2013-07-24 18:05:00 +0200538 *
Rose Zadik9ba6b622018-01-24 12:59:19 +0000539 * \param ctx The generic cipher context.
540 * \param mode The padding mode.
541 *
542 * \returns \c 0 on success, #MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE
543 * if the selected padding mode is not supported, or
544 * #MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA if the cipher mode
Paul Bakker1a45d912013-08-14 12:04:26 +0200545 * does not support padding.
Manuel Pégourié-Gonnardd5fdcaf2013-07-24 18:05:00 +0200546 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200547int mbedtls_cipher_set_padding_mode( mbedtls_cipher_context_t *ctx, mbedtls_cipher_padding_t mode );
548#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
Manuel Pégourié-Gonnardd5fdcaf2013-07-24 18:05:00 +0200549
550/**
Rose Zadik9ba6b622018-01-24 12:59:19 +0000551 * \brief This function sets the initialization vector (IV)
552 * or nonce.
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200553 *
Rose Zadik9ba6b622018-01-24 12:59:19 +0000554 * \param ctx The generic cipher context.
555 * \param iv The IV to use, or NONCE_COUNTER for CTR-mode ciphers.
556 * \param iv_len The IV length for ciphers with variable-size IV.
557 * This parameter is discarded by ciphers with fixed-size IV.
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200558 *
Rose Zadik9ba6b622018-01-24 12:59:19 +0000559 * \returns \c 0 on success, or #MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200560 *
Rose Zadik9ba6b622018-01-24 12:59:19 +0000561 * \note Some ciphers do not use IVs nor nonce. For these
562 * ciphers, this function has no effect.
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200563 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200564int mbedtls_cipher_set_iv( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200565 const unsigned char *iv, size_t iv_len );
566
567/**
Rose Zadik9ba6b622018-01-24 12:59:19 +0000568 * \brief This function resets the cipher state.
Paul Bakker8123e9d2011-01-06 15:37:30 +0000569 *
Rose Zadik9ba6b622018-01-24 12:59:19 +0000570 * \param ctx The generic cipher context.
Paul Bakker8123e9d2011-01-06 15:37:30 +0000571 *
Rose Zadik9ba6b622018-01-24 12:59:19 +0000572 * \returns \c 0 on success, #MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA
573 * if parameter verification fails.
Paul Bakker8123e9d2011-01-06 15:37:30 +0000574 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200575int mbedtls_cipher_reset( mbedtls_cipher_context_t *ctx );
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200576
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200577#if defined(MBEDTLS_GCM_C)
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200578/**
Rose Zadik9ba6b622018-01-24 12:59:19 +0000579 * \brief This function adds additional data for AEAD ciphers.
580 * Only supported with GCM. Must be called
581 * exactly once, after mbedtls_cipher_reset().
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200582 *
Rose Zadik9ba6b622018-01-24 12:59:19 +0000583 * \param ctx The generic cipher context.
584 * \param ad The additional data to use.
585 * \param ad_len the Length of \p ad.
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200586 *
Rose Zadik9ba6b622018-01-24 12:59:19 +0000587 * \return \c 0 on success, or a specific error code on failure.
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200588 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200589int mbedtls_cipher_update_ad( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200590 const unsigned char *ad, size_t ad_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200591#endif /* MBEDTLS_GCM_C */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000592
593/**
Rose Zadik9ba6b622018-01-24 12:59:19 +0000594 * \brief The generic cipher update function. It encrypts or
595 * decrypts using the given cipher context. Writes as
596 * many block-sized blocks of data as possible to output.
597 * Any data that cannot be written immediately is either
598 * added to the next block, or flushed when
599 * mbedtls_cipher_finish() is called.
600 * Exception: For MBEDTLS_MODE_ECB, expects a single block
601 * in size. For example, 16 Bytes for AES.
Paul Bakker8123e9d2011-01-06 15:37:30 +0000602 *
Rose Zadik9ba6b622018-01-24 12:59:19 +0000603 * \param ctx The generic cipher context.
604 * \param input The buffer holding the input data.
605 * \param ilen The length of the input data.
606 * \param output The buffer for the output data. Must be able to hold at
607 * least \p ilen + block_size. Must not be the same buffer
608 * as input.
609 * \param olen The length of the output data, to be updated with the
610 * actual number of Bytes written.
Paul Bakker8123e9d2011-01-06 15:37:30 +0000611 *
Rose Zadik9ba6b622018-01-24 12:59:19 +0000612 * \returns \c 0 on success, #MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA if
Paul Bakkerff61a782011-06-09 15:42:02 +0000613 * parameter verification fails,
Rose Zadik9ba6b622018-01-24 12:59:19 +0000614 * #MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE on an
615 * unsupported mode for a cipher, or a cipher-specific
Paul Bakkerff61a782011-06-09 15:42:02 +0000616 * error code.
Manuel Pégourié-Gonnardb8bd5932013-09-05 13:38:15 +0200617 *
618 * \note If the underlying cipher is GCM, all calls to this
Rose Zadik9ba6b622018-01-24 12:59:19 +0000619 * function, except the last one before
620 * mbedtls_cipher_finish(). Must have \p ilen as a
621 * multiple of the block_size.
Paul Bakker8123e9d2011-01-06 15:37:30 +0000622 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200623int mbedtls_cipher_update( mbedtls_cipher_context_t *ctx, const unsigned char *input,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200624 size_t ilen, unsigned char *output, size_t *olen );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000625
626/**
Rose Zadik9ba6b622018-01-24 12:59:19 +0000627 * \brief The generic cipher finalization function. If data still
628 * needs to be flushed from an incomplete block, the data
629 * contained in it is padded to the size of
630 * the last block, and written to the \p output buffer.
Paul Bakker8123e9d2011-01-06 15:37:30 +0000631 *
Rose Zadik9ba6b622018-01-24 12:59:19 +0000632 * \param ctx The generic cipher context.
633 * \param output The buffer to write data to. Needs block_size available.
634 * \param olen The length of the data written to the \p output buffer.
Paul Bakker8123e9d2011-01-06 15:37:30 +0000635 *
Rose Zadik9ba6b622018-01-24 12:59:19 +0000636 * \returns \c 0 on success, #MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA if
Paul Bakkerff61a782011-06-09 15:42:02 +0000637 * parameter verification fails,
Rose Zadik9ba6b622018-01-24 12:59:19 +0000638 * #MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED if decryption
Paul Bakkerff61a782011-06-09 15:42:02 +0000639 * expected a full block but was not provided one,
Rose Zadik9ba6b622018-01-24 12:59:19 +0000640 * #MBEDTLS_ERR_CIPHER_INVALID_PADDING on invalid padding
641 * while decrypting, or a cipher-specific error code
642 * on failure for any other reason.
Paul Bakker8123e9d2011-01-06 15:37:30 +0000643 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200644int mbedtls_cipher_finish( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200645 unsigned char *output, size_t *olen );
646
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200647#if defined(MBEDTLS_GCM_C)
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200648/**
Rose Zadik9ba6b622018-01-24 12:59:19 +0000649 * \brief This function writes a tag for AEAD ciphers.
650 * Only supported with GCM.
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200651 * Must be called after mbedtls_cipher_finish().
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200652 *
Rose Zadik9ba6b622018-01-24 12:59:19 +0000653 * \param ctx The generic cipher context.
654 * \param tag The buffer to write the tag to.
655 * \param tag_len The length of the tag to write.
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200656 *
Rose Zadik9ba6b622018-01-24 12:59:19 +0000657 * \return \c 0 on success, or a specific error code on failure.
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200658 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200659int mbedtls_cipher_write_tag( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200660 unsigned char *tag, size_t tag_len );
661
662/**
Rose Zadik9ba6b622018-01-24 12:59:19 +0000663 * \brief This function checks the tag for AEAD ciphers.
664 * Only supported with GCM.
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200665 * Must be called after mbedtls_cipher_finish().
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200666 *
Rose Zadik9ba6b622018-01-24 12:59:19 +0000667 * \param ctx The generic cipher context.
668 * \param tag The buffer holding the tag.
669 * \param tag_len The length of the tag to check.
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200670 *
Rose Zadik9ba6b622018-01-24 12:59:19 +0000671 * \return \c 0 on success, or a specific error code on failure.
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200672 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200673int mbedtls_cipher_check_tag( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200674 const unsigned char *tag, size_t tag_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200675#endif /* MBEDTLS_GCM_C */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000676
Paul Bakker8123e9d2011-01-06 15:37:30 +0000677/**
Rose Zadik9ba6b622018-01-24 12:59:19 +0000678 * \brief The generic all-in-one encryption/decryption function,
679 * for all ciphers except AEAD constructs.
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +0200680 *
Rose Zadik9ba6b622018-01-24 12:59:19 +0000681 * \param ctx The generic cipher context.
682 * \param iv The IV to use, or NONCE_COUNTER for CTR-mode ciphers.
683 * \param iv_len The IV length for ciphers with variable-size IV.
684 * This parameter is discarded by ciphers with fixed-size
685 * IV.
686 * \param input The buffer holding the input data.
687 * \param ilen The length of the input data.
688 * \param output The buffer for the output data. Must be able to hold at
689 * least \p ilen + block_size. Must not be the same buffer
690 * as input.
691 * \param olen The length of the output data, to be updated with the
692 * actual number of Bytes written.
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +0200693 *
Rose Zadik9ba6b622018-01-24 12:59:19 +0000694 * \note Some ciphers do not use IVs nor nonce. For these
695 * ciphers, use \p iv = NULL and \p iv_len = 0.
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +0200696 *
Rose Zadik9ba6b622018-01-24 12:59:19 +0000697 * \returns \c 0 on success, or
698 * #MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA, or
699 * #MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED if decryption
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +0200700 * expected a full block but was not provided one, or
Rose Zadik9ba6b622018-01-24 12:59:19 +0000701 * #MBEDTLS_ERR_CIPHER_INVALID_PADDING on invalid padding
702 * while decrypting, or a cipher-specific error code on
703 * failure for any other reason.
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +0200704 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200705int mbedtls_cipher_crypt( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +0200706 const unsigned char *iv, size_t iv_len,
707 const unsigned char *input, size_t ilen,
708 unsigned char *output, size_t *olen );
709
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200710#if defined(MBEDTLS_CIPHER_MODE_AEAD)
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200711/**
Rose Zadik9ba6b622018-01-24 12:59:19 +0000712 * \brief The generic autenticated encryption (AEAD) function.
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200713 *
Rose Zadik9ba6b622018-01-24 12:59:19 +0000714 * \param ctx The generic cipher context.
715 * \param iv The IV to use, or NONCE_COUNTER for CTR-mode ciphers.
716 * \param iv_len The IV length for ciphers with variable-size IV.
717 * This parameter is discarded by ciphers with fixed-size IV.
718 * \param ad The additional data to authenticate.
719 * \param ad_len The length of \p ad.
720 * \param input The buffer holding the input data.
721 * \param ilen The length of the input data.
722 * \param output The buffer for the output data.
723 * Must be able to hold at least \p ilen.
724 * \param olen The length of the output data, to be updated with the
725 * actual number of Bytes written.
726 * \param tag The buffer for the authentication tag.
727 * \param tag_len The desired length of the authentication tag.
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200728 *
Rose Zadik9ba6b622018-01-24 12:59:19 +0000729 * \returns \c 0 on success, or
730 * #MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA, or
731 * a cipher-specific error code.
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200732 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200733int mbedtls_cipher_auth_encrypt( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200734 const unsigned char *iv, size_t iv_len,
735 const unsigned char *ad, size_t ad_len,
736 const unsigned char *input, size_t ilen,
737 unsigned char *output, size_t *olen,
738 unsigned char *tag, size_t tag_len );
739
740/**
Rose Zadik9ba6b622018-01-24 12:59:19 +0000741 * \brief The generic autenticated decryption (AEAD) function.
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200742 *
Rose Zadik9ba6b622018-01-24 12:59:19 +0000743 * \param ctx The generic cipher context.
744 * \param iv The IV to use, or NONCE_COUNTER for CTR-mode ciphers.
745 * \param iv_len The IV length for ciphers with variable-size IV.
746 * This parameter is discarded by ciphers with fixed-size IV.
747 * \param ad The additional data to be authenticated.
748 * \param ad_len The length of \p ad.
749 * \param input The buffer holding the input data.
750 * \param ilen The length of the input data.
751 * \param output The buffer for the output data.
752 * Must be able to hold at least \p ilen.
753 * \param olen The length of the output data, to be updated with the
754 * actual number of Bytes written.
755 * \param tag The buffer holding the authentication tag.
756 * \param tag_len The length of the authentication tag.
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200757 *
Rose Zadik9ba6b622018-01-24 12:59:19 +0000758 * \returns \c 0 on success, or
759 * #MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA, or
760 * #MBEDTLS_ERR_CIPHER_AUTH_FAILED if data is not authentic,
761 * or a cipher-specific error code on failure for any other reason.
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200762 *
763 * \note If the data is not authentic, then the output buffer
Rose Zadik9ba6b622018-01-24 12:59:19 +0000764 * is zeroed out to prevent the unauthentic plaintext being
765 * used, making this interface safer.
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200766 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200767int mbedtls_cipher_auth_decrypt( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200768 const unsigned char *iv, size_t iv_len,
769 const unsigned char *ad, size_t ad_len,
770 const unsigned char *input, size_t ilen,
771 unsigned char *output, size_t *olen,
772 const unsigned char *tag, size_t tag_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200773#endif /* MBEDTLS_CIPHER_MODE_AEAD */
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200774
Paul Bakker8123e9d2011-01-06 15:37:30 +0000775#ifdef __cplusplus
776}
777#endif
778
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200779#endif /* MBEDTLS_CIPHER_H */