blob: dc1bc56470e73bc97cab22ebc9d4b8340fcea828 [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
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +020010 * SPDX-License-Identifier: Apache-2.0
11 *
12 * Licensed under the Apache License, Version 2.0 (the "License"); you may
13 * not use this file except in compliance with the License.
14 * You may obtain a copy of the License at
15 *
16 * http://www.apache.org/licenses/LICENSE-2.0
17 *
18 * Unless required by applicable law or agreed to in writing, software
19 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
20 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21 * See the License for the specific language governing permissions and
22 * limitations under the License.
Paul Bakker8123e9d2011-01-06 15:37:30 +000023 *
Rose Zadik9ba6b622018-01-24 12:59:19 +000024 * This file is part of Mbed TLS (https://tls.mbed.org)
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)
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +020031#include "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>
37
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020038#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CCM_C)
39#define MBEDTLS_CIPHER_MODE_AEAD
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +020040#endif
41
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020042#if defined(MBEDTLS_CIPHER_MODE_CBC)
43#define MBEDTLS_CIPHER_MODE_WITH_PADDING
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +020044#endif
45
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020046#if defined(MBEDTLS_ARC4_C)
47#define MBEDTLS_CIPHER_MODE_STREAM
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +010048#endif
49
Manuel Pégourié-Gonnard0223ab92015-10-05 11:40:01 +010050#if ( defined(__ARMCC_VERSION) || defined(_MSC_VER) ) && \
51 !defined(inline) && !defined(__cplusplus)
Paul Bakker569df2c2011-06-21 07:48:07 +000052#define inline __inline
Manuel Pégourié-Gonnard20af64d2015-07-07 18:33:39 +020053#endif
Paul Bakkeraf5c85f2011-04-18 03:47:52 +000054
Rose Zadik9ba6b622018-01-24 12:59:19 +000055#define MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE -0x6080 /**< The selected feature is not available. */
56#define MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA -0x6100 /**< Bad input parameters. */
57#define MBEDTLS_ERR_CIPHER_ALLOC_FAILED -0x6180 /**< Failed to allocate memory. */
58#define MBEDTLS_ERR_CIPHER_INVALID_PADDING -0x6200 /**< Input data contains invalid padding and is rejected. */
59#define MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED -0x6280 /**< Decryption of block requires a full block. */
60#define MBEDTLS_ERR_CIPHER_AUTH_FAILED -0x6300 /**< Authentication failed (for AEAD modes). */
61#define MBEDTLS_ERR_CIPHER_INVALID_CONTEXT -0x6380 /**< The context is invalid. For example, because it was freed. */
62#define MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED -0x6400 /**< Cipher hardware accelerator failed. */
Paul Bakker343a8702011-06-09 14:27:58 +000063
Rose Zadik9ba6b622018-01-24 12:59:19 +000064#define MBEDTLS_CIPHER_VARIABLE_IV_LEN 0x01 /**< Cipher accepts IVs of variable length. */
65#define MBEDTLS_CIPHER_VARIABLE_KEY_LEN 0x02 /**< Cipher accepts keys of variable length. */
Manuel Pégourié-Gonnard81754a02014-06-23 11:33:18 +020066
Paul Bakker407a0da2013-06-27 14:29:21 +020067#ifdef __cplusplus
68extern "C" {
69#endif
70
Rose Zadik9ba6b622018-01-24 12:59:19 +000071/** Supported cipher IDs. */
Paul Bakker8123e9d2011-01-06 15:37:30 +000072typedef enum {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020073 MBEDTLS_CIPHER_ID_NONE = 0,
74 MBEDTLS_CIPHER_ID_NULL,
75 MBEDTLS_CIPHER_ID_AES,
76 MBEDTLS_CIPHER_ID_DES,
77 MBEDTLS_CIPHER_ID_3DES,
78 MBEDTLS_CIPHER_ID_CAMELLIA,
79 MBEDTLS_CIPHER_ID_BLOWFISH,
80 MBEDTLS_CIPHER_ID_ARC4,
81} mbedtls_cipher_id_t;
Paul Bakker8123e9d2011-01-06 15:37:30 +000082
Rose Zadik9ba6b622018-01-24 12:59:19 +000083/** Supported cipher types. */
Paul Bakker8123e9d2011-01-06 15:37:30 +000084typedef enum {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020085 MBEDTLS_CIPHER_NONE = 0,
86 MBEDTLS_CIPHER_NULL,
87 MBEDTLS_CIPHER_AES_128_ECB,
88 MBEDTLS_CIPHER_AES_192_ECB,
89 MBEDTLS_CIPHER_AES_256_ECB,
90 MBEDTLS_CIPHER_AES_128_CBC,
91 MBEDTLS_CIPHER_AES_192_CBC,
92 MBEDTLS_CIPHER_AES_256_CBC,
93 MBEDTLS_CIPHER_AES_128_CFB128,
94 MBEDTLS_CIPHER_AES_192_CFB128,
95 MBEDTLS_CIPHER_AES_256_CFB128,
96 MBEDTLS_CIPHER_AES_128_CTR,
97 MBEDTLS_CIPHER_AES_192_CTR,
98 MBEDTLS_CIPHER_AES_256_CTR,
99 MBEDTLS_CIPHER_AES_128_GCM,
100 MBEDTLS_CIPHER_AES_192_GCM,
101 MBEDTLS_CIPHER_AES_256_GCM,
102 MBEDTLS_CIPHER_CAMELLIA_128_ECB,
103 MBEDTLS_CIPHER_CAMELLIA_192_ECB,
104 MBEDTLS_CIPHER_CAMELLIA_256_ECB,
105 MBEDTLS_CIPHER_CAMELLIA_128_CBC,
106 MBEDTLS_CIPHER_CAMELLIA_192_CBC,
107 MBEDTLS_CIPHER_CAMELLIA_256_CBC,
108 MBEDTLS_CIPHER_CAMELLIA_128_CFB128,
109 MBEDTLS_CIPHER_CAMELLIA_192_CFB128,
110 MBEDTLS_CIPHER_CAMELLIA_256_CFB128,
111 MBEDTLS_CIPHER_CAMELLIA_128_CTR,
112 MBEDTLS_CIPHER_CAMELLIA_192_CTR,
113 MBEDTLS_CIPHER_CAMELLIA_256_CTR,
114 MBEDTLS_CIPHER_CAMELLIA_128_GCM,
115 MBEDTLS_CIPHER_CAMELLIA_192_GCM,
116 MBEDTLS_CIPHER_CAMELLIA_256_GCM,
117 MBEDTLS_CIPHER_DES_ECB,
118 MBEDTLS_CIPHER_DES_CBC,
119 MBEDTLS_CIPHER_DES_EDE_ECB,
120 MBEDTLS_CIPHER_DES_EDE_CBC,
121 MBEDTLS_CIPHER_DES_EDE3_ECB,
122 MBEDTLS_CIPHER_DES_EDE3_CBC,
123 MBEDTLS_CIPHER_BLOWFISH_ECB,
124 MBEDTLS_CIPHER_BLOWFISH_CBC,
125 MBEDTLS_CIPHER_BLOWFISH_CFB64,
126 MBEDTLS_CIPHER_BLOWFISH_CTR,
127 MBEDTLS_CIPHER_ARC4_128,
128 MBEDTLS_CIPHER_AES_128_CCM,
129 MBEDTLS_CIPHER_AES_192_CCM,
130 MBEDTLS_CIPHER_AES_256_CCM,
131 MBEDTLS_CIPHER_CAMELLIA_128_CCM,
132 MBEDTLS_CIPHER_CAMELLIA_192_CCM,
133 MBEDTLS_CIPHER_CAMELLIA_256_CCM,
134} mbedtls_cipher_type_t;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000135
Rose Zadik9ba6b622018-01-24 12:59:19 +0000136/** Supported cipher modes. */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000137typedef enum {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200138 MBEDTLS_MODE_NONE = 0,
139 MBEDTLS_MODE_ECB,
140 MBEDTLS_MODE_CBC,
141 MBEDTLS_MODE_CFB,
142 MBEDTLS_MODE_OFB, /* Unused! */
143 MBEDTLS_MODE_CTR,
144 MBEDTLS_MODE_GCM,
145 MBEDTLS_MODE_STREAM,
146 MBEDTLS_MODE_CCM,
147} mbedtls_cipher_mode_t;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000148
Rose Zadik9ba6b622018-01-24 12:59:19 +0000149/** Supported cipher padding types. */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000150typedef enum {
Rose Zadik9ba6b622018-01-24 12:59:19 +0000151 MBEDTLS_PADDING_PKCS7 = 0, /**< PKCS7 padding (default). */
152 MBEDTLS_PADDING_ONE_AND_ZEROS, /**< ISO/IEC 7816-4 padding. */
153 MBEDTLS_PADDING_ZEROS_AND_LEN, /**< ANSI X.923 padding. */
154 MBEDTLS_PADDING_ZEROS, /**< zero padding (not reversible). */
155 MBEDTLS_PADDING_NONE, /**< never pad (full blocks only). */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200156} mbedtls_cipher_padding_t;
Manuel Pégourié-Gonnardd5fdcaf2013-07-24 18:05:00 +0200157
Rose Zadik9ba6b622018-01-24 12:59:19 +0000158/** Type of operation. */
Manuel Pégourié-Gonnardd5fdcaf2013-07-24 18:05:00 +0200159typedef enum {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200160 MBEDTLS_OPERATION_NONE = -1,
161 MBEDTLS_DECRYPT = 0,
162 MBEDTLS_ENCRYPT,
163} mbedtls_operation_t;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000164
165enum {
Rose Zadik9ba6b622018-01-24 12:59:19 +0000166 /** Undefined key length. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200167 MBEDTLS_KEY_LENGTH_NONE = 0,
Rose Zadik9ba6b622018-01-24 12:59:19 +0000168 /** Key length, in bits (including parity), for DES keys. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200169 MBEDTLS_KEY_LENGTH_DES = 64,
Rose Zadik9ba6b622018-01-24 12:59:19 +0000170 /** Key length in bits, including parity, for DES in two-key EDE. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200171 MBEDTLS_KEY_LENGTH_DES_EDE = 128,
Rose Zadik9ba6b622018-01-24 12:59:19 +0000172 /** Key length in bits, including parity, for DES in three-key EDE. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200173 MBEDTLS_KEY_LENGTH_DES_EDE3 = 192,
Paul Bakker8123e9d2011-01-06 15:37:30 +0000174};
175
Rose Zadik9ba6b622018-01-24 12:59:19 +0000176/** Maximum length of any IV, in Bytes. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200177#define MBEDTLS_MAX_IV_LENGTH 16
Rose Zadik9ba6b622018-01-24 12:59:19 +0000178/** Maximum block size of any cipher, in Bytes. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200179#define MBEDTLS_MAX_BLOCK_LENGTH 16
Manuel Pégourié-Gonnard0b58c152013-10-24 17:17:54 +0200180
Paul Bakker8123e9d2011-01-06 15:37:30 +0000181/**
Manuel Pégourié-Gonnard5a74e8b2015-05-06 17:10:55 +0100182 * Base cipher information (opaque struct).
Paul Bakker343a8702011-06-09 14:27:58 +0000183 */
Manuel Pégourié-Gonnard5a74e8b2015-05-06 17:10:55 +0100184typedef struct mbedtls_cipher_base_t mbedtls_cipher_base_t;
Paul Bakker343a8702011-06-09 14:27:58 +0000185
186/**
Simon Butcher327398a2016-10-05 14:09:11 +0100187 * CMAC context (opaque struct).
188 */
189typedef struct mbedtls_cmac_context_t mbedtls_cmac_context_t;
190
191/**
Rose Zadik9ba6b622018-01-24 12:59:19 +0000192 * Cipher information. Allows calling cipher functions
193 * in a generic way.
Paul Bakker8123e9d2011-01-06 15:37:30 +0000194 */
195typedef struct {
Rose Zadik9ba6b622018-01-24 12:59:19 +0000196 /** Full cipher identifier. For example,
197 * MBEDTLS_CIPHER_AES_256_CBC.
198 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200199 mbedtls_cipher_type_t type;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000200
Rose Zadik9ba6b622018-01-24 12:59:19 +0000201 /** The cipher mode. For example, MBEDTLS_MODE_CBC. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200202 mbedtls_cipher_mode_t mode;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000203
Rose Zadik9ba6b622018-01-24 12:59:19 +0000204 /** The cipher key length, in bits. This is the
205 * default length for variable sized ciphers.
206 * Includes parity bits for ciphers like DES.
207 */
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200208 unsigned int key_bitlen;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000209
Rose Zadik9ba6b622018-01-24 12:59:19 +0000210 /** Name of the cipher. */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000211 const char * name;
212
Rose Zadik9ba6b622018-01-24 12:59:19 +0000213 /** IV or nonce size, in Bytes.
214 * For ciphers that accept variable IV sizes,
215 * this is the recommended size.
216 */
Paul Bakker23986e52011-04-24 08:57:21 +0000217 unsigned int iv_size;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000218
Rose Zadik9ba6b622018-01-24 12:59:19 +0000219 /** 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 +0200220 int flags;
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200221
Rose Zadik9ba6b622018-01-24 12:59:19 +0000222 /** The block size, in Bytes. */
Paul Bakker23986e52011-04-24 08:57:21 +0000223 unsigned int block_size;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000224
Rose Zadik9ba6b622018-01-24 12:59:19 +0000225 /** Struct for base cipher information and functions. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200226 const mbedtls_cipher_base_t *base;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000227
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200228} mbedtls_cipher_info_t;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000229
230/**
Paul Bakker20281562011-11-11 10:34:04 +0000231 * Generic cipher context.
Paul Bakker8123e9d2011-01-06 15:37:30 +0000232 */
233typedef struct {
Rose Zadik9ba6b622018-01-24 12:59:19 +0000234 /** Information about the associated cipher. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200235 const mbedtls_cipher_info_t *cipher_info;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000236
Rose Zadik9ba6b622018-01-24 12:59:19 +0000237 /** Key length to use. */
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200238 int key_bitlen;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000239
Rose Zadik9ba6b622018-01-24 12:59:19 +0000240 /** Operation that the key of the context has been
241 * initialized for.
242 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200243 mbedtls_operation_t operation;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000244
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200245#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Rose Zadik9ba6b622018-01-24 12:59:19 +0000246 /** Padding functions to use, if relevant for
247 * the specific cipher mode.
248 */
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200249 void (*add_padding)( unsigned char *output, size_t olen, size_t data_len );
250 int (*get_padding)( unsigned char *input, size_t ilen, size_t *data_len );
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +0100251#endif
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200252
Rose Zadik9ba6b622018-01-24 12:59:19 +0000253 /** Buffer for input that has not been processed yet. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200254 unsigned char unprocessed_data[MBEDTLS_MAX_BLOCK_LENGTH];
Paul Bakker8123e9d2011-01-06 15:37:30 +0000255
Rose Zadik9ba6b622018-01-24 12:59:19 +0000256 /** Number of Bytes that have not been processed yet. */
Paul Bakker23986e52011-04-24 08:57:21 +0000257 size_t unprocessed_len;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000258
Rose Zadik9ba6b622018-01-24 12:59:19 +0000259 /** Current IV or NONCE_COUNTER for CTR-mode. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200260 unsigned char iv[MBEDTLS_MAX_IV_LENGTH];
Paul Bakker8123e9d2011-01-06 15:37:30 +0000261
Rose Zadik9ba6b622018-01-24 12:59:19 +0000262 /** IV size in Bytes, for ciphers with variable-length IVs. */
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200263 size_t iv_size;
264
Rose Zadik9ba6b622018-01-24 12:59:19 +0000265 /** The cipher-specific context. */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000266 void *cipher_ctx;
Simon Butcher327398a2016-10-05 14:09:11 +0100267
268#if defined(MBEDTLS_CMAC_C)
Rose Zadik9ba6b622018-01-24 12:59:19 +0000269 /** CMAC-specific context. */
Simon Butcher327398a2016-10-05 14:09:11 +0100270 mbedtls_cmac_context_t *cmac_ctx;
271#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200272} mbedtls_cipher_context_t;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000273
Paul Bakker8123e9d2011-01-06 15:37:30 +0000274/**
Rose Zadik9ba6b622018-01-24 12:59:19 +0000275 * \brief This function retrieves the list of ciphers supported by the generic
276 * cipher module.
Paul Bakker72f62662011-01-16 21:27:44 +0000277 *
Rose Zadik9ba6b622018-01-24 12:59:19 +0000278 * \return A statically-allocated array of ciphers. The last entry
279 * is zero.
Paul Bakker72f62662011-01-16 21:27:44 +0000280 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200281const int *mbedtls_cipher_list( void );
Paul Bakker72f62662011-01-16 21:27:44 +0000282
283/**
Rose Zadik9ba6b622018-01-24 12:59:19 +0000284 * \brief This function retrieves the cipher-information
285 * structure associated with the given cipher name.
Paul Bakker8123e9d2011-01-06 15:37:30 +0000286 *
Paul Bakker23986e52011-04-24 08:57:21 +0000287 * \param cipher_name Name of the cipher to search for.
Paul Bakker8123e9d2011-01-06 15:37:30 +0000288 *
Rose Zadik9ba6b622018-01-24 12:59:19 +0000289 * \return The cipher information structure associated with the
290 * given \p cipher_name, or NULL if not found.
Paul Bakker8123e9d2011-01-06 15:37:30 +0000291 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200292const mbedtls_cipher_info_t *mbedtls_cipher_info_from_string( const char *cipher_name );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000293
294/**
Rose Zadik9ba6b622018-01-24 12:59:19 +0000295 * \brief This function retrieves the cipher-information
296 * structure associated with the given cipher type.
Paul Bakker8123e9d2011-01-06 15:37:30 +0000297 *
298 * \param cipher_type Type of the cipher to search for.
299 *
Rose Zadik9ba6b622018-01-24 12:59:19 +0000300 * \return The cipher information structure associated with the
301 * given \p cipher_type, or NULL if not found.
Paul Bakker8123e9d2011-01-06 15:37:30 +0000302 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200303const mbedtls_cipher_info_t *mbedtls_cipher_info_from_type( const mbedtls_cipher_type_t cipher_type );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000304
305/**
Rose Zadik9ba6b622018-01-24 12:59:19 +0000306 * \brief This function retrieves the cipher-information
307 * structure associated with the given cipher ID,
308 * key size and mode.
Paul Bakkerf46b6952013-09-09 00:08:26 +0200309 *
Rose Zadik9ba6b622018-01-24 12:59:19 +0000310 * \param cipher_id The ID of the cipher to search for. For example,
311 * #MBEDTLS_CIPHER_ID_AES.
312 * \param key_bitlen The length of the key in bits.
313 * \param mode The cipher mode. For example, #MBEDTLS_MODE_CBC.
Paul Bakkerf46b6952013-09-09 00:08:26 +0200314 *
Rose Zadik9ba6b622018-01-24 12:59:19 +0000315 * \return The cipher information structure associated with the
316 * given \p cipher_id, or NULL if not found.
Paul Bakkerf46b6952013-09-09 00:08:26 +0200317 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200318const 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 +0200319 int key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200320 const mbedtls_cipher_mode_t mode );
Paul Bakkerf46b6952013-09-09 00:08:26 +0200321
322/**
Rose Zadik9ba6b622018-01-24 12:59:19 +0000323 * \brief This function initializes a \p cipher_context as NONE.
Paul Bakker84bbeb52014-07-01 14:53:22 +0200324 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200325void mbedtls_cipher_init( mbedtls_cipher_context_t *ctx );
Paul Bakker84bbeb52014-07-01 14:53:22 +0200326
327/**
Rose Zadik9ba6b622018-01-24 12:59:19 +0000328 * \brief This function frees and clears the cipher-specific
329 * context of \p ctx. Freeing \p ctx itself remains the
330 * responsibility of the caller.
Paul Bakker84bbeb52014-07-01 14:53:22 +0200331 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200332void mbedtls_cipher_free( mbedtls_cipher_context_t *ctx );
Paul Bakker84bbeb52014-07-01 14:53:22 +0200333
Rose Zadik9ba6b622018-01-24 12:59:19 +0000334
Paul Bakker84bbeb52014-07-01 14:53:22 +0200335/**
Rose Zadik9ba6b622018-01-24 12:59:19 +0000336 * \brief This function initializes and fills the cipher-context
337 * structure with the appropriate values. It also clears
338 * the structure.
Paul Bakker8123e9d2011-01-06 15:37:30 +0000339 *
Rose Zadik9ba6b622018-01-24 12:59:19 +0000340 * \param ctx The context to initialize. May not be NULL.
341 * \param cipher_info The cipher to use.
Paul Bakker84bbeb52014-07-01 14:53:22 +0200342 *
Rose Zadik9ba6b622018-01-24 12:59:19 +0000343 * \return \c 0 on success,
344 * #MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA on parameter failure,
345 * #MBEDTLS_ERR_CIPHER_ALLOC_FAILED if allocation of the
Paul Bakkerff61a782011-06-09 15:42:02 +0000346 * cipher-specific context failed.
Rose Zadik9ba6b622018-01-24 12:59:19 +0000347 *
348 * \internal Currently, the function also clears the structure.
349 * In future versions, the caller will be required to call
350 * mbedtls_cipher_init() on the structure first.
Paul Bakker8123e9d2011-01-06 15:37:30 +0000351 */
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +0200352int mbedtls_cipher_setup( mbedtls_cipher_context_t *ctx, const mbedtls_cipher_info_t *cipher_info );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000353
354/**
Rose Zadik9ba6b622018-01-24 12:59:19 +0000355 * \brief This function returns the block size of the given cipher.
Paul Bakker8123e9d2011-01-06 15:37:30 +0000356 *
Rose Zadik9ba6b622018-01-24 12:59:19 +0000357 * \param ctx The context of the cipher. Must be initialized.
Paul Bakker8123e9d2011-01-06 15:37:30 +0000358 *
Rose Zadik9ba6b622018-01-24 12:59:19 +0000359 * \return The size of the blocks of the cipher, or zero if \p ctx
360 * has not been initialized.
Paul Bakker8123e9d2011-01-06 15:37:30 +0000361 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200362static inline unsigned int mbedtls_cipher_get_block_size( const mbedtls_cipher_context_t *ctx )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000363{
364 if( NULL == ctx || NULL == ctx->cipher_info )
365 return 0;
366
367 return ctx->cipher_info->block_size;
368}
369
370/**
Rose Zadik9ba6b622018-01-24 12:59:19 +0000371 * \brief This function returns the mode of operation for
372 * the cipher. For example, MBEDTLS_MODE_CBC.
Paul Bakkerf7e5bb52011-11-11 10:53:37 +0000373 *
Rose Zadik9ba6b622018-01-24 12:59:19 +0000374 * \param ctx The context of the cipher. Must be initialized.
Paul Bakkerf7e5bb52011-11-11 10:53:37 +0000375 *
Rose Zadik9ba6b622018-01-24 12:59:19 +0000376 * \return The mode of operation, or #MBEDTLS_MODE_NONE if
377 * \p ctx has not been initialized.
Paul Bakkerf7e5bb52011-11-11 10:53:37 +0000378 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200379static inline mbedtls_cipher_mode_t mbedtls_cipher_get_cipher_mode( const mbedtls_cipher_context_t *ctx )
Paul Bakkerf7e5bb52011-11-11 10:53:37 +0000380{
381 if( NULL == ctx || NULL == ctx->cipher_info )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200382 return MBEDTLS_MODE_NONE;
Paul Bakkerf7e5bb52011-11-11 10:53:37 +0000383
384 return ctx->cipher_info->mode;
385}
386
387/**
Rose Zadik9ba6b622018-01-24 12:59:19 +0000388 * \brief This function returns the size of the IV or nonce
389 * of the cipher, in Bytes.
Paul Bakker8123e9d2011-01-06 15:37:30 +0000390 *
Rose Zadik9ba6b622018-01-24 12:59:19 +0000391 * \param ctx The context of the cipher. Must be initialized.
Paul Bakker8123e9d2011-01-06 15:37:30 +0000392 *
Rose Zadik9ba6b622018-01-24 12:59:19 +0000393 * \return <ul><li>If no IV has been set: the recommended IV size.
394 * 0 for ciphers not using IV or nonce.</li>
395 * <li>If IV has already been set: the actual size.</li></ul>
Paul Bakker8123e9d2011-01-06 15:37:30 +0000396 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200397static inline int mbedtls_cipher_get_iv_size( const mbedtls_cipher_context_t *ctx )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000398{
399 if( NULL == ctx || NULL == ctx->cipher_info )
400 return 0;
401
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200402 if( ctx->iv_size != 0 )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200403 return (int) ctx->iv_size;
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200404
Manuel Pégourié-Gonnard46c4fa12015-08-12 09:27:55 +0200405 return (int) ctx->cipher_info->iv_size;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000406}
407
408/**
Rose Zadik9ba6b622018-01-24 12:59:19 +0000409 * \brief This function returns the type of the given cipher.
Paul Bakker8123e9d2011-01-06 15:37:30 +0000410 *
Rose Zadik9ba6b622018-01-24 12:59:19 +0000411 * \param ctx The context of the cipher. Must be initialized.
Paul Bakker8123e9d2011-01-06 15:37:30 +0000412 *
Rose Zadik9ba6b622018-01-24 12:59:19 +0000413 * \return The type of the cipher, or #MBEDTLS_CIPHER_NONE if
414 * \p ctx has not been initialized.
Paul Bakker8123e9d2011-01-06 15:37:30 +0000415 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200416static inline mbedtls_cipher_type_t mbedtls_cipher_get_type( const mbedtls_cipher_context_t *ctx )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000417{
418 if( NULL == ctx || NULL == ctx->cipher_info )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200419 return MBEDTLS_CIPHER_NONE;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000420
421 return ctx->cipher_info->type;
422}
423
424/**
Rose Zadik9ba6b622018-01-24 12:59:19 +0000425 * \brief This function returns the name of the given cipher
426 * as a string.
Paul Bakker8123e9d2011-01-06 15:37:30 +0000427 *
Rose Zadik9ba6b622018-01-24 12:59:19 +0000428 * \param ctx The context of the cipher. Must be initialized.
Paul Bakker8123e9d2011-01-06 15:37:30 +0000429 *
Rose Zadik9ba6b622018-01-24 12:59:19 +0000430 * \return The name of the cipher, or NULL if \p ctx has not
431 * been not initialized.
Paul Bakker8123e9d2011-01-06 15:37:30 +0000432 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200433static inline const char *mbedtls_cipher_get_name( const mbedtls_cipher_context_t *ctx )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000434{
435 if( NULL == ctx || NULL == ctx->cipher_info )
436 return 0;
437
438 return ctx->cipher_info->name;
439}
440
441/**
Rose Zadik9ba6b622018-01-24 12:59:19 +0000442 * \brief This function returns the key length of the cipher.
Paul Bakker8123e9d2011-01-06 15:37:30 +0000443 *
Rose Zadik9ba6b622018-01-24 12:59:19 +0000444 * \param ctx The context of the cipher. Must be initialized.
Paul Bakker8123e9d2011-01-06 15:37:30 +0000445 *
Rose Zadik9ba6b622018-01-24 12:59:19 +0000446 * \return The key length of the cipher in bits, or
447 * #MBEDTLS_KEY_LENGTH_NONE if ctx \p has not been
448 * initialized.
Paul Bakker8123e9d2011-01-06 15:37:30 +0000449 */
Manuel Pégourié-Gonnard097c7bb2015-06-18 16:43:38 +0200450static inline int mbedtls_cipher_get_key_bitlen( const mbedtls_cipher_context_t *ctx )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000451{
Paul Bakker68884e32013-01-07 18:20:04 +0100452 if( NULL == ctx || NULL == ctx->cipher_info )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200453 return MBEDTLS_KEY_LENGTH_NONE;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000454
Manuel Pégourié-Gonnard46c4fa12015-08-12 09:27:55 +0200455 return (int) ctx->cipher_info->key_bitlen;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000456}
457
458/**
Rose Zadik9ba6b622018-01-24 12:59:19 +0000459 * \brief This function returns the operation of the given cipher.
Paul Bakkerf7e5bb52011-11-11 10:53:37 +0000460 *
Rose Zadik9ba6b622018-01-24 12:59:19 +0000461 * \param ctx The context of the cipher. Must be initialized.
Paul Bakkerf7e5bb52011-11-11 10:53:37 +0000462 *
Rose Zadik9ba6b622018-01-24 12:59:19 +0000463 * \return The type of operation: #MBEDTLS_ENCRYPT or
464 * #MBEDTLS_DECRYPT, or #MBEDTLS_OPERATION_NONE if \p ctx
465 * has not been initialized.
Paul Bakkerf7e5bb52011-11-11 10:53:37 +0000466 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200467static inline mbedtls_operation_t mbedtls_cipher_get_operation( const mbedtls_cipher_context_t *ctx )
Paul Bakkerf7e5bb52011-11-11 10:53:37 +0000468{
469 if( NULL == ctx || NULL == ctx->cipher_info )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200470 return MBEDTLS_OPERATION_NONE;
Paul Bakkerf7e5bb52011-11-11 10:53:37 +0000471
472 return ctx->operation;
473}
474
475/**
Rose Zadik9ba6b622018-01-24 12:59:19 +0000476 * \brief This function sets the key to use with the given context.
Paul Bakker8123e9d2011-01-06 15:37:30 +0000477 *
Rose Zadik9ba6b622018-01-24 12:59:19 +0000478 * \param ctx The generic cipher context. May not be NULL. Must have
479 * been initialized using mbedtls_cipher_info_from_type()
480 * or mbedtls_cipher_info_from_string().
Paul Bakker8123e9d2011-01-06 15:37:30 +0000481 * \param key The key to use.
Rose Zadik9ba6b622018-01-24 12:59:19 +0000482 * \param key_bitlen The key length to use, in bits.
483 * \param operation The operation that the key will be used for:
484 * #MBEDTLS_ENCRYPT or #MBEDTLS_DECRYPT.
Paul Bakker8123e9d2011-01-06 15:37:30 +0000485 *
Rose Zadik9ba6b622018-01-24 12:59:19 +0000486 * \returns \c 0 on success, #MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA if
487 * parameter verification fails, or a cipher-specific
Paul Bakkerff61a782011-06-09 15:42:02 +0000488 * error code.
Paul Bakker8123e9d2011-01-06 15:37:30 +0000489 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200490int mbedtls_cipher_setkey( mbedtls_cipher_context_t *ctx, const unsigned char *key,
Manuel Pégourié-Gonnardb8186a52015-06-18 14:58:58 +0200491 int key_bitlen, const mbedtls_operation_t operation );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000492
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200493#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Paul Bakker8123e9d2011-01-06 15:37:30 +0000494/**
Rose Zadik9ba6b622018-01-24 12:59:19 +0000495 * \brief This function sets the padding mode, for cipher modes
496 * that use padding.
Manuel Pégourié-Gonnardd5fdcaf2013-07-24 18:05:00 +0200497 *
Rose Zadik9ba6b622018-01-24 12:59:19 +0000498 * The default passing mode is PKCS7 padding.
Manuel Pégourié-Gonnardd5fdcaf2013-07-24 18:05:00 +0200499 *
Rose Zadik9ba6b622018-01-24 12:59:19 +0000500 * \param ctx The generic cipher context.
501 * \param mode The padding mode.
502 *
503 * \returns \c 0 on success, #MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE
504 * if the selected padding mode is not supported, or
505 * #MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA if the cipher mode
Paul Bakker1a45d912013-08-14 12:04:26 +0200506 * does not support padding.
Manuel Pégourié-Gonnardd5fdcaf2013-07-24 18:05:00 +0200507 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200508int mbedtls_cipher_set_padding_mode( mbedtls_cipher_context_t *ctx, mbedtls_cipher_padding_t mode );
509#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
Manuel Pégourié-Gonnardd5fdcaf2013-07-24 18:05:00 +0200510
511/**
Rose Zadik9ba6b622018-01-24 12:59:19 +0000512 * \brief This function sets the initialization vector (IV)
513 * or nonce.
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200514 *
Rose Zadik9ba6b622018-01-24 12:59:19 +0000515 * \param ctx The generic cipher context.
516 * \param iv The IV to use, or NONCE_COUNTER for CTR-mode ciphers.
517 * \param iv_len The IV length for ciphers with variable-size IV.
518 * This parameter is discarded by ciphers with fixed-size IV.
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200519 *
Rose Zadik9ba6b622018-01-24 12:59:19 +0000520 * \returns \c 0 on success, or #MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200521 *
Rose Zadik9ba6b622018-01-24 12:59:19 +0000522 * \note Some ciphers do not use IVs nor nonce. For these
523 * ciphers, this function has no effect.
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200524 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200525int mbedtls_cipher_set_iv( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200526 const unsigned char *iv, size_t iv_len );
527
528/**
Rose Zadik9ba6b622018-01-24 12:59:19 +0000529 * \brief This function resets the cipher state.
Paul Bakker8123e9d2011-01-06 15:37:30 +0000530 *
Rose Zadik9ba6b622018-01-24 12:59:19 +0000531 * \param ctx The generic cipher context.
Paul Bakker8123e9d2011-01-06 15:37:30 +0000532 *
Rose Zadik9ba6b622018-01-24 12:59:19 +0000533 * \returns \c 0 on success, #MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA
534 * if parameter verification fails.
Paul Bakker8123e9d2011-01-06 15:37:30 +0000535 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200536int mbedtls_cipher_reset( mbedtls_cipher_context_t *ctx );
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200537
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200538#if defined(MBEDTLS_GCM_C)
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200539/**
Rose Zadik9ba6b622018-01-24 12:59:19 +0000540 * \brief This function adds additional data for AEAD ciphers.
541 * Only supported with GCM. Must be called
542 * exactly once, after mbedtls_cipher_reset().
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200543 *
Rose Zadik9ba6b622018-01-24 12:59:19 +0000544 * \param ctx The generic cipher context.
545 * \param ad The additional data to use.
546 * \param ad_len the Length of \p ad.
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200547 *
Rose Zadik9ba6b622018-01-24 12:59:19 +0000548 * \return \c 0 on success, or a specific error code on failure.
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200549 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200550int mbedtls_cipher_update_ad( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200551 const unsigned char *ad, size_t ad_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200552#endif /* MBEDTLS_GCM_C */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000553
554/**
Rose Zadik9ba6b622018-01-24 12:59:19 +0000555 * \brief The generic cipher update function. It encrypts or
556 * decrypts using the given cipher context. Writes as
557 * many block-sized blocks of data as possible to output.
558 * Any data that cannot be written immediately is either
559 * added to the next block, or flushed when
560 * mbedtls_cipher_finish() is called.
561 * Exception: For MBEDTLS_MODE_ECB, expects a single block
562 * in size. For example, 16 Bytes for AES.
Paul Bakker8123e9d2011-01-06 15:37:30 +0000563 *
Rose Zadik9ba6b622018-01-24 12:59:19 +0000564 * \param ctx The generic cipher context.
565 * \param input The buffer holding the input data.
566 * \param ilen The length of the input data.
567 * \param output The buffer for the output data. Must be able to hold at
568 * least \p ilen + block_size. Must not be the same buffer
569 * as input.
570 * \param olen The length of the output data, to be updated with the
571 * actual number of Bytes written.
Paul Bakker8123e9d2011-01-06 15:37:30 +0000572 *
Rose Zadik9ba6b622018-01-24 12:59:19 +0000573 * \returns \c 0 on success, #MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA if
Paul Bakkerff61a782011-06-09 15:42:02 +0000574 * parameter verification fails,
Rose Zadik9ba6b622018-01-24 12:59:19 +0000575 * #MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE on an
576 * unsupported mode for a cipher, or a cipher-specific
Paul Bakkerff61a782011-06-09 15:42:02 +0000577 * error code.
Manuel Pégourié-Gonnardb8bd5932013-09-05 13:38:15 +0200578 *
579 * \note If the underlying cipher is GCM, all calls to this
Rose Zadik9ba6b622018-01-24 12:59:19 +0000580 * function, except the last one before
581 * mbedtls_cipher_finish(). Must have \p ilen as a
582 * multiple of the block_size.
Paul Bakker8123e9d2011-01-06 15:37:30 +0000583 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200584int mbedtls_cipher_update( mbedtls_cipher_context_t *ctx, const unsigned char *input,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200585 size_t ilen, unsigned char *output, size_t *olen );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000586
587/**
Rose Zadik9ba6b622018-01-24 12:59:19 +0000588 * \brief The generic cipher finalization function. If data still
589 * needs to be flushed from an incomplete block, the data
590 * contained in it is padded to the size of
591 * the last block, and written to the \p output buffer.
Paul Bakker8123e9d2011-01-06 15:37:30 +0000592 *
Rose Zadik9ba6b622018-01-24 12:59:19 +0000593 * \param ctx The generic cipher context.
594 * \param output The buffer to write data to. Needs block_size available.
595 * \param olen The length of the data written to the \p output buffer.
Paul Bakker8123e9d2011-01-06 15:37:30 +0000596 *
Rose Zadik9ba6b622018-01-24 12:59:19 +0000597 * \returns \c 0 on success, #MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA if
Paul Bakkerff61a782011-06-09 15:42:02 +0000598 * parameter verification fails,
Rose Zadik9ba6b622018-01-24 12:59:19 +0000599 * #MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED if decryption
Paul Bakkerff61a782011-06-09 15:42:02 +0000600 * expected a full block but was not provided one,
Rose Zadik9ba6b622018-01-24 12:59:19 +0000601 * #MBEDTLS_ERR_CIPHER_INVALID_PADDING on invalid padding
602 * while decrypting, or a cipher-specific error code
603 * on failure for any other reason.
Paul Bakker8123e9d2011-01-06 15:37:30 +0000604 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200605int mbedtls_cipher_finish( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200606 unsigned char *output, size_t *olen );
607
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200608#if defined(MBEDTLS_GCM_C)
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200609/**
Rose Zadik9ba6b622018-01-24 12:59:19 +0000610 * \brief This function writes a tag for AEAD ciphers.
611 * Only supported with GCM.
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200612 * Must be called after mbedtls_cipher_finish().
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200613 *
Rose Zadik9ba6b622018-01-24 12:59:19 +0000614 * \param ctx The generic cipher context.
615 * \param tag The buffer to write the tag to.
616 * \param tag_len The length of the tag to write.
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200617 *
Rose Zadik9ba6b622018-01-24 12:59:19 +0000618 * \return \c 0 on success, or a specific error code on failure.
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200619 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200620int mbedtls_cipher_write_tag( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200621 unsigned char *tag, size_t tag_len );
622
623/**
Rose Zadik9ba6b622018-01-24 12:59:19 +0000624 * \brief This function checks the tag for AEAD ciphers.
625 * Only supported with GCM.
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200626 * Must be called after mbedtls_cipher_finish().
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200627 *
Rose Zadik9ba6b622018-01-24 12:59:19 +0000628 * \param ctx The generic cipher context.
629 * \param tag The buffer holding the tag.
630 * \param tag_len The length of the tag to check.
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200631 *
Rose Zadik9ba6b622018-01-24 12:59:19 +0000632 * \return \c 0 on success, or a specific error code on failure.
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200633 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200634int mbedtls_cipher_check_tag( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200635 const unsigned char *tag, size_t tag_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200636#endif /* MBEDTLS_GCM_C */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000637
Paul Bakker8123e9d2011-01-06 15:37:30 +0000638/**
Rose Zadik9ba6b622018-01-24 12:59:19 +0000639 * \brief The generic all-in-one encryption/decryption function,
640 * for all ciphers except AEAD constructs.
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +0200641 *
Rose Zadik9ba6b622018-01-24 12:59:19 +0000642 * \param ctx The generic cipher context.
643 * \param iv The IV to use, or NONCE_COUNTER for CTR-mode ciphers.
644 * \param iv_len The IV length for ciphers with variable-size IV.
645 * This parameter is discarded by ciphers with fixed-size
646 * IV.
647 * \param input The buffer holding the input data.
648 * \param ilen The length of the input data.
649 * \param output The buffer for the output data. Must be able to hold at
650 * least \p ilen + block_size. Must not be the same buffer
651 * as input.
652 * \param olen The length of the output data, to be updated with the
653 * actual number of Bytes written.
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +0200654 *
Rose Zadik9ba6b622018-01-24 12:59:19 +0000655 * \note Some ciphers do not use IVs nor nonce. For these
656 * ciphers, use \p iv = NULL and \p iv_len = 0.
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +0200657 *
Rose Zadik9ba6b622018-01-24 12:59:19 +0000658 * \returns \c 0 on success, or
659 * #MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA, or
660 * #MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED if decryption
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +0200661 * expected a full block but was not provided one, or
Rose Zadik9ba6b622018-01-24 12:59:19 +0000662 * #MBEDTLS_ERR_CIPHER_INVALID_PADDING on invalid padding
663 * while decrypting, or a cipher-specific error code on
664 * failure for any other reason.
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +0200665 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200666int mbedtls_cipher_crypt( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +0200667 const unsigned char *iv, size_t iv_len,
668 const unsigned char *input, size_t ilen,
669 unsigned char *output, size_t *olen );
670
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200671#if defined(MBEDTLS_CIPHER_MODE_AEAD)
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200672/**
Rose Zadik9ba6b622018-01-24 12:59:19 +0000673 * \brief The generic autenticated encryption (AEAD) function.
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200674 *
Rose Zadik9ba6b622018-01-24 12:59:19 +0000675 * \param ctx The generic cipher context.
676 * \param iv The IV to use, or NONCE_COUNTER for CTR-mode ciphers.
677 * \param iv_len The IV length for ciphers with variable-size IV.
678 * This parameter is discarded by ciphers with fixed-size IV.
679 * \param ad The additional data to authenticate.
680 * \param ad_len The length of \p ad.
681 * \param input The buffer holding the input data.
682 * \param ilen The length of the input data.
683 * \param output The buffer for the output data.
684 * Must be able to hold at least \p ilen.
685 * \param olen The length of the output data, to be updated with the
686 * actual number of Bytes written.
687 * \param tag The buffer for the authentication tag.
688 * \param tag_len The desired length of the authentication tag.
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200689 *
Rose Zadik9ba6b622018-01-24 12:59:19 +0000690 * \returns \c 0 on success, or
691 * #MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA, or
692 * a cipher-specific error code.
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200693 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200694int mbedtls_cipher_auth_encrypt( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200695 const unsigned char *iv, size_t iv_len,
696 const unsigned char *ad, size_t ad_len,
697 const unsigned char *input, size_t ilen,
698 unsigned char *output, size_t *olen,
699 unsigned char *tag, size_t tag_len );
700
701/**
Rose Zadik9ba6b622018-01-24 12:59:19 +0000702 * \brief The generic autenticated decryption (AEAD) function.
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200703 *
Rose Zadik9ba6b622018-01-24 12:59:19 +0000704 * \param ctx The generic cipher context.
705 * \param iv The IV to use, or NONCE_COUNTER for CTR-mode ciphers.
706 * \param iv_len The IV length for ciphers with variable-size IV.
707 * This parameter is discarded by ciphers with fixed-size IV.
708 * \param ad The additional data to be authenticated.
709 * \param ad_len The length of \p ad.
710 * \param input The buffer holding the input data.
711 * \param ilen The length of the input data.
712 * \param output The buffer for the output data.
713 * Must be able to hold at least \p ilen.
714 * \param olen The length of the output data, to be updated with the
715 * actual number of Bytes written.
716 * \param tag The buffer holding the authentication tag.
717 * \param tag_len The length of the authentication tag.
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200718 *
Rose Zadik9ba6b622018-01-24 12:59:19 +0000719 * \returns \c 0 on success, or
720 * #MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA, or
721 * #MBEDTLS_ERR_CIPHER_AUTH_FAILED if data is not authentic,
722 * or a cipher-specific error code on failure for any other reason.
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200723 *
724 * \note If the data is not authentic, then the output buffer
Rose Zadik9ba6b622018-01-24 12:59:19 +0000725 * is zeroed out to prevent the unauthentic plaintext being
726 * used, making this interface safer.
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200727 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200728int mbedtls_cipher_auth_decrypt( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200729 const unsigned char *iv, size_t iv_len,
730 const unsigned char *ad, size_t ad_len,
731 const unsigned char *input, size_t ilen,
732 unsigned char *output, size_t *olen,
733 const unsigned char *tag, size_t tag_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200734#endif /* MBEDTLS_CIPHER_MODE_AEAD */
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200735
Paul Bakker8123e9d2011-01-06 15:37:30 +0000736#ifdef __cplusplus
737}
738#endif
739
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200740#endif /* MBEDTLS_CIPHER_H */