blob: 1c453a1d32d0db585c6821975767bdf76d79a04f [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
Ron Eldor0557b8f2018-06-27 18:33:13 +030046#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020047#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
Hanno Beckerbbca8c52017-09-25 14:53:51 +010071/**
72 * \brief An enumeration of supported ciphers.
73 *
74 * \warning ARC4 and DES are considered weak ciphers and their use
75 * constitutes a security risk. We recommend considering stronger
76 * ciphers instead.
77 */
Paul Bakker8123e9d2011-01-06 15:37:30 +000078typedef enum {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020079 MBEDTLS_CIPHER_ID_NONE = 0,
80 MBEDTLS_CIPHER_ID_NULL,
81 MBEDTLS_CIPHER_ID_AES,
82 MBEDTLS_CIPHER_ID_DES,
83 MBEDTLS_CIPHER_ID_3DES,
84 MBEDTLS_CIPHER_ID_CAMELLIA,
85 MBEDTLS_CIPHER_ID_BLOWFISH,
86 MBEDTLS_CIPHER_ID_ARC4,
87} mbedtls_cipher_id_t;
Paul Bakker8123e9d2011-01-06 15:37:30 +000088
Hanno Beckerbbca8c52017-09-25 14:53:51 +010089/**
90 * \brief An enumeration of supported (cipher, mode) pairs.
91 *
92 * \warning ARC4 and DES are considered weak ciphers and their use
93 * constitutes a security risk. We recommend considering stronger
94 * ciphers instead.
95 */
Paul Bakker8123e9d2011-01-06 15:37:30 +000096typedef enum {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020097 MBEDTLS_CIPHER_NONE = 0,
98 MBEDTLS_CIPHER_NULL,
99 MBEDTLS_CIPHER_AES_128_ECB,
100 MBEDTLS_CIPHER_AES_192_ECB,
101 MBEDTLS_CIPHER_AES_256_ECB,
102 MBEDTLS_CIPHER_AES_128_CBC,
103 MBEDTLS_CIPHER_AES_192_CBC,
104 MBEDTLS_CIPHER_AES_256_CBC,
105 MBEDTLS_CIPHER_AES_128_CFB128,
106 MBEDTLS_CIPHER_AES_192_CFB128,
107 MBEDTLS_CIPHER_AES_256_CFB128,
108 MBEDTLS_CIPHER_AES_128_CTR,
109 MBEDTLS_CIPHER_AES_192_CTR,
110 MBEDTLS_CIPHER_AES_256_CTR,
111 MBEDTLS_CIPHER_AES_128_GCM,
112 MBEDTLS_CIPHER_AES_192_GCM,
113 MBEDTLS_CIPHER_AES_256_GCM,
114 MBEDTLS_CIPHER_CAMELLIA_128_ECB,
115 MBEDTLS_CIPHER_CAMELLIA_192_ECB,
116 MBEDTLS_CIPHER_CAMELLIA_256_ECB,
117 MBEDTLS_CIPHER_CAMELLIA_128_CBC,
118 MBEDTLS_CIPHER_CAMELLIA_192_CBC,
119 MBEDTLS_CIPHER_CAMELLIA_256_CBC,
120 MBEDTLS_CIPHER_CAMELLIA_128_CFB128,
121 MBEDTLS_CIPHER_CAMELLIA_192_CFB128,
122 MBEDTLS_CIPHER_CAMELLIA_256_CFB128,
123 MBEDTLS_CIPHER_CAMELLIA_128_CTR,
124 MBEDTLS_CIPHER_CAMELLIA_192_CTR,
125 MBEDTLS_CIPHER_CAMELLIA_256_CTR,
126 MBEDTLS_CIPHER_CAMELLIA_128_GCM,
127 MBEDTLS_CIPHER_CAMELLIA_192_GCM,
128 MBEDTLS_CIPHER_CAMELLIA_256_GCM,
129 MBEDTLS_CIPHER_DES_ECB,
130 MBEDTLS_CIPHER_DES_CBC,
131 MBEDTLS_CIPHER_DES_EDE_ECB,
132 MBEDTLS_CIPHER_DES_EDE_CBC,
133 MBEDTLS_CIPHER_DES_EDE3_ECB,
134 MBEDTLS_CIPHER_DES_EDE3_CBC,
135 MBEDTLS_CIPHER_BLOWFISH_ECB,
136 MBEDTLS_CIPHER_BLOWFISH_CBC,
137 MBEDTLS_CIPHER_BLOWFISH_CFB64,
138 MBEDTLS_CIPHER_BLOWFISH_CTR,
139 MBEDTLS_CIPHER_ARC4_128,
140 MBEDTLS_CIPHER_AES_128_CCM,
141 MBEDTLS_CIPHER_AES_192_CCM,
142 MBEDTLS_CIPHER_AES_256_CCM,
143 MBEDTLS_CIPHER_CAMELLIA_128_CCM,
144 MBEDTLS_CIPHER_CAMELLIA_192_CCM,
145 MBEDTLS_CIPHER_CAMELLIA_256_CCM,
146} mbedtls_cipher_type_t;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000147
Rose Zadik9ba6b622018-01-24 12:59:19 +0000148/** Supported cipher modes. */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000149typedef enum {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200150 MBEDTLS_MODE_NONE = 0,
151 MBEDTLS_MODE_ECB,
152 MBEDTLS_MODE_CBC,
153 MBEDTLS_MODE_CFB,
154 MBEDTLS_MODE_OFB, /* Unused! */
155 MBEDTLS_MODE_CTR,
156 MBEDTLS_MODE_GCM,
157 MBEDTLS_MODE_STREAM,
158 MBEDTLS_MODE_CCM,
159} mbedtls_cipher_mode_t;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000160
Rose Zadik9ba6b622018-01-24 12:59:19 +0000161/** Supported cipher padding types. */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000162typedef enum {
Rose Zadik9ba6b622018-01-24 12:59:19 +0000163 MBEDTLS_PADDING_PKCS7 = 0, /**< PKCS7 padding (default). */
164 MBEDTLS_PADDING_ONE_AND_ZEROS, /**< ISO/IEC 7816-4 padding. */
165 MBEDTLS_PADDING_ZEROS_AND_LEN, /**< ANSI X.923 padding. */
166 MBEDTLS_PADDING_ZEROS, /**< zero padding (not reversible). */
167 MBEDTLS_PADDING_NONE, /**< never pad (full blocks only). */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200168} mbedtls_cipher_padding_t;
Manuel Pégourié-Gonnardd5fdcaf2013-07-24 18:05:00 +0200169
Rose Zadik9ba6b622018-01-24 12:59:19 +0000170/** Type of operation. */
Manuel Pégourié-Gonnardd5fdcaf2013-07-24 18:05:00 +0200171typedef enum {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200172 MBEDTLS_OPERATION_NONE = -1,
173 MBEDTLS_DECRYPT = 0,
174 MBEDTLS_ENCRYPT,
175} mbedtls_operation_t;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000176
177enum {
Rose Zadik9ba6b622018-01-24 12:59:19 +0000178 /** Undefined key length. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200179 MBEDTLS_KEY_LENGTH_NONE = 0,
Rose Zadik9ba6b622018-01-24 12:59:19 +0000180 /** Key length, in bits (including parity), for DES keys. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200181 MBEDTLS_KEY_LENGTH_DES = 64,
Rose Zadik9ba6b622018-01-24 12:59:19 +0000182 /** Key length in bits, including parity, for DES in two-key EDE. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200183 MBEDTLS_KEY_LENGTH_DES_EDE = 128,
Rose Zadik9ba6b622018-01-24 12:59:19 +0000184 /** Key length in bits, including parity, for DES in three-key EDE. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200185 MBEDTLS_KEY_LENGTH_DES_EDE3 = 192,
Paul Bakker8123e9d2011-01-06 15:37:30 +0000186};
187
Rose Zadik9ba6b622018-01-24 12:59:19 +0000188/** Maximum length of any IV, in Bytes. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200189#define MBEDTLS_MAX_IV_LENGTH 16
Rose Zadik9ba6b622018-01-24 12:59:19 +0000190/** Maximum block size of any cipher, in Bytes. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200191#define MBEDTLS_MAX_BLOCK_LENGTH 16
Manuel Pégourié-Gonnard0b58c152013-10-24 17:17:54 +0200192
Paul Bakker8123e9d2011-01-06 15:37:30 +0000193/**
Manuel Pégourié-Gonnard5a74e8b2015-05-06 17:10:55 +0100194 * Base cipher information (opaque struct).
Paul Bakker343a8702011-06-09 14:27:58 +0000195 */
Manuel Pégourié-Gonnard5a74e8b2015-05-06 17:10:55 +0100196typedef struct mbedtls_cipher_base_t mbedtls_cipher_base_t;
Paul Bakker343a8702011-06-09 14:27:58 +0000197
198/**
Simon Butcher327398a2016-10-05 14:09:11 +0100199 * CMAC context (opaque struct).
200 */
201typedef struct mbedtls_cmac_context_t mbedtls_cmac_context_t;
202
203/**
Rose Zadik9ba6b622018-01-24 12:59:19 +0000204 * Cipher information. Allows calling cipher functions
205 * in a generic way.
Paul Bakker8123e9d2011-01-06 15:37:30 +0000206 */
207typedef struct {
Rose Zadik9ba6b622018-01-24 12:59:19 +0000208 /** Full cipher identifier. For example,
209 * MBEDTLS_CIPHER_AES_256_CBC.
210 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200211 mbedtls_cipher_type_t type;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000212
Rose Zadik9ba6b622018-01-24 12:59:19 +0000213 /** The cipher mode. For example, MBEDTLS_MODE_CBC. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200214 mbedtls_cipher_mode_t mode;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000215
Rose Zadik9ba6b622018-01-24 12:59:19 +0000216 /** The cipher key length, in bits. This is the
217 * default length for variable sized ciphers.
218 * Includes parity bits for ciphers like DES.
219 */
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200220 unsigned int key_bitlen;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000221
Rose Zadik9ba6b622018-01-24 12:59:19 +0000222 /** Name of the cipher. */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000223 const char * name;
224
Rose Zadik9ba6b622018-01-24 12:59:19 +0000225 /** IV or nonce size, in Bytes.
226 * For ciphers that accept variable IV sizes,
227 * this is the recommended size.
228 */
Paul Bakker23986e52011-04-24 08:57:21 +0000229 unsigned int iv_size;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000230
Rose Zadik9ba6b622018-01-24 12:59:19 +0000231 /** 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 +0200232 int flags;
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200233
Rose Zadik9ba6b622018-01-24 12:59:19 +0000234 /** The block size, in Bytes. */
Paul Bakker23986e52011-04-24 08:57:21 +0000235 unsigned int block_size;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000236
Rose Zadik9ba6b622018-01-24 12:59:19 +0000237 /** Struct for base cipher information and functions. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200238 const mbedtls_cipher_base_t *base;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000239
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200240} mbedtls_cipher_info_t;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000241
242/**
Paul Bakker20281562011-11-11 10:34:04 +0000243 * Generic cipher context.
Paul Bakker8123e9d2011-01-06 15:37:30 +0000244 */
245typedef struct {
Rose Zadik9ba6b622018-01-24 12:59:19 +0000246 /** Information about the associated cipher. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200247 const mbedtls_cipher_info_t *cipher_info;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000248
Rose Zadik9ba6b622018-01-24 12:59:19 +0000249 /** Key length to use. */
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200250 int key_bitlen;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000251
Rose Zadik9ba6b622018-01-24 12:59:19 +0000252 /** Operation that the key of the context has been
253 * initialized for.
254 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200255 mbedtls_operation_t operation;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000256
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200257#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Rose Zadik9ba6b622018-01-24 12:59:19 +0000258 /** Padding functions to use, if relevant for
259 * the specific cipher mode.
260 */
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200261 void (*add_padding)( unsigned char *output, size_t olen, size_t data_len );
262 int (*get_padding)( unsigned char *input, size_t ilen, size_t *data_len );
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +0100263#endif
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200264
Rose Zadik9ba6b622018-01-24 12:59:19 +0000265 /** Buffer for input that has not been processed yet. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200266 unsigned char unprocessed_data[MBEDTLS_MAX_BLOCK_LENGTH];
Paul Bakker8123e9d2011-01-06 15:37:30 +0000267
Rose Zadik9ba6b622018-01-24 12:59:19 +0000268 /** Number of Bytes that have not been processed yet. */
Paul Bakker23986e52011-04-24 08:57:21 +0000269 size_t unprocessed_len;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000270
Rose Zadik9ba6b622018-01-24 12:59:19 +0000271 /** Current IV or NONCE_COUNTER for CTR-mode. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200272 unsigned char iv[MBEDTLS_MAX_IV_LENGTH];
Paul Bakker8123e9d2011-01-06 15:37:30 +0000273
Rose Zadik9ba6b622018-01-24 12:59:19 +0000274 /** IV size in Bytes, for ciphers with variable-length IVs. */
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200275 size_t iv_size;
276
Rose Zadik9ba6b622018-01-24 12:59:19 +0000277 /** The cipher-specific context. */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000278 void *cipher_ctx;
Simon Butcher327398a2016-10-05 14:09:11 +0100279
280#if defined(MBEDTLS_CMAC_C)
Rose Zadik9ba6b622018-01-24 12:59:19 +0000281 /** CMAC-specific context. */
Simon Butcher327398a2016-10-05 14:09:11 +0100282 mbedtls_cmac_context_t *cmac_ctx;
283#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200284} mbedtls_cipher_context_t;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000285
Paul Bakker8123e9d2011-01-06 15:37:30 +0000286/**
Rose Zadik9ba6b622018-01-24 12:59:19 +0000287 * \brief This function retrieves the list of ciphers supported by the generic
288 * cipher module.
Paul Bakker72f62662011-01-16 21:27:44 +0000289 *
Rose Zadik9ba6b622018-01-24 12:59:19 +0000290 * \return A statically-allocated array of ciphers. The last entry
291 * is zero.
Paul Bakker72f62662011-01-16 21:27:44 +0000292 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200293const int *mbedtls_cipher_list( void );
Paul Bakker72f62662011-01-16 21:27:44 +0000294
295/**
Rose Zadik9ba6b622018-01-24 12:59:19 +0000296 * \brief This function retrieves the cipher-information
297 * structure associated with the given cipher name.
Paul Bakker8123e9d2011-01-06 15:37:30 +0000298 *
Paul Bakker23986e52011-04-24 08:57:21 +0000299 * \param cipher_name Name of the cipher to search for.
Paul Bakker8123e9d2011-01-06 15:37:30 +0000300 *
Rose Zadik9ba6b622018-01-24 12:59:19 +0000301 * \return The cipher information structure associated with the
302 * given \p cipher_name, or NULL if not found.
Paul Bakker8123e9d2011-01-06 15:37:30 +0000303 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200304const mbedtls_cipher_info_t *mbedtls_cipher_info_from_string( const char *cipher_name );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000305
306/**
Rose Zadik9ba6b622018-01-24 12:59:19 +0000307 * \brief This function retrieves the cipher-information
308 * structure associated with the given cipher type.
Paul Bakker8123e9d2011-01-06 15:37:30 +0000309 *
310 * \param cipher_type Type of the cipher to search for.
311 *
Rose Zadik9ba6b622018-01-24 12:59:19 +0000312 * \return The cipher information structure associated with the
313 * given \p cipher_type, or NULL if not found.
Paul Bakker8123e9d2011-01-06 15:37:30 +0000314 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200315const mbedtls_cipher_info_t *mbedtls_cipher_info_from_type( const mbedtls_cipher_type_t cipher_type );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000316
317/**
Rose Zadik9ba6b622018-01-24 12:59:19 +0000318 * \brief This function retrieves the cipher-information
319 * structure associated with the given cipher ID,
320 * key size and mode.
Paul Bakkerf46b6952013-09-09 00:08:26 +0200321 *
Rose Zadik9ba6b622018-01-24 12:59:19 +0000322 * \param cipher_id The ID of the cipher to search for. For example,
323 * #MBEDTLS_CIPHER_ID_AES.
324 * \param key_bitlen The length of the key in bits.
325 * \param mode The cipher mode. For example, #MBEDTLS_MODE_CBC.
Paul Bakkerf46b6952013-09-09 00:08:26 +0200326 *
Rose Zadik9ba6b622018-01-24 12:59:19 +0000327 * \return The cipher information structure associated with the
328 * given \p cipher_id, or NULL if not found.
Paul Bakkerf46b6952013-09-09 00:08:26 +0200329 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200330const 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 +0200331 int key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200332 const mbedtls_cipher_mode_t mode );
Paul Bakkerf46b6952013-09-09 00:08:26 +0200333
334/**
Rose Zadik9ba6b622018-01-24 12:59:19 +0000335 * \brief This function initializes a \p cipher_context as NONE.
Paul Bakker84bbeb52014-07-01 14:53:22 +0200336 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200337void mbedtls_cipher_init( mbedtls_cipher_context_t *ctx );
Paul Bakker84bbeb52014-07-01 14:53:22 +0200338
339/**
Rose Zadik9ba6b622018-01-24 12:59:19 +0000340 * \brief This function frees and clears the cipher-specific
341 * context of \p ctx. Freeing \p ctx itself remains the
342 * responsibility of the caller.
Paul Bakker84bbeb52014-07-01 14:53:22 +0200343 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200344void mbedtls_cipher_free( mbedtls_cipher_context_t *ctx );
Paul Bakker84bbeb52014-07-01 14:53:22 +0200345
Rose Zadik9ba6b622018-01-24 12:59:19 +0000346
Paul Bakker84bbeb52014-07-01 14:53:22 +0200347/**
Rose Zadik9ba6b622018-01-24 12:59:19 +0000348 * \brief This function initializes and fills the cipher-context
349 * structure with the appropriate values. It also clears
350 * the structure.
Paul Bakker8123e9d2011-01-06 15:37:30 +0000351 *
Rose Zadik9ba6b622018-01-24 12:59:19 +0000352 * \param ctx The context to initialize. May not be NULL.
353 * \param cipher_info The cipher to use.
Paul Bakker84bbeb52014-07-01 14:53:22 +0200354 *
Rose Zadik9ba6b622018-01-24 12:59:19 +0000355 * \return \c 0 on success,
356 * #MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA on parameter failure,
357 * #MBEDTLS_ERR_CIPHER_ALLOC_FAILED if allocation of the
Paul Bakkerff61a782011-06-09 15:42:02 +0000358 * cipher-specific context failed.
Rose Zadik9ba6b622018-01-24 12:59:19 +0000359 *
360 * \internal Currently, the function also clears the structure.
361 * In future versions, the caller will be required to call
362 * mbedtls_cipher_init() on the structure first.
Paul Bakker8123e9d2011-01-06 15:37:30 +0000363 */
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +0200364int mbedtls_cipher_setup( mbedtls_cipher_context_t *ctx, const mbedtls_cipher_info_t *cipher_info );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000365
366/**
Rose Zadik9ba6b622018-01-24 12:59:19 +0000367 * \brief This function returns the block size of the given cipher.
Paul Bakker8123e9d2011-01-06 15:37:30 +0000368 *
Rose Zadik9ba6b622018-01-24 12:59:19 +0000369 * \param ctx The context of the cipher. Must be initialized.
Paul Bakker8123e9d2011-01-06 15:37:30 +0000370 *
Rose Zadik9ba6b622018-01-24 12:59:19 +0000371 * \return The size of the blocks of the cipher, or zero if \p ctx
372 * has not been initialized.
Paul Bakker8123e9d2011-01-06 15:37:30 +0000373 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200374static inline unsigned int mbedtls_cipher_get_block_size( const mbedtls_cipher_context_t *ctx )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000375{
376 if( NULL == ctx || NULL == ctx->cipher_info )
377 return 0;
378
379 return ctx->cipher_info->block_size;
380}
381
382/**
Rose Zadik9ba6b622018-01-24 12:59:19 +0000383 * \brief This function returns the mode of operation for
384 * the cipher. For example, MBEDTLS_MODE_CBC.
Paul Bakkerf7e5bb52011-11-11 10:53:37 +0000385 *
Rose Zadik9ba6b622018-01-24 12:59:19 +0000386 * \param ctx The context of the cipher. Must be initialized.
Paul Bakkerf7e5bb52011-11-11 10:53:37 +0000387 *
Rose Zadik9ba6b622018-01-24 12:59:19 +0000388 * \return The mode of operation, or #MBEDTLS_MODE_NONE if
389 * \p ctx has not been initialized.
Paul Bakkerf7e5bb52011-11-11 10:53:37 +0000390 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200391static inline mbedtls_cipher_mode_t mbedtls_cipher_get_cipher_mode( const mbedtls_cipher_context_t *ctx )
Paul Bakkerf7e5bb52011-11-11 10:53:37 +0000392{
393 if( NULL == ctx || NULL == ctx->cipher_info )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200394 return MBEDTLS_MODE_NONE;
Paul Bakkerf7e5bb52011-11-11 10:53:37 +0000395
396 return ctx->cipher_info->mode;
397}
398
399/**
Rose Zadik9ba6b622018-01-24 12:59:19 +0000400 * \brief This function returns the size of the IV or nonce
401 * of the cipher, in Bytes.
Paul Bakker8123e9d2011-01-06 15:37:30 +0000402 *
Rose Zadik9ba6b622018-01-24 12:59:19 +0000403 * \param ctx The context of the cipher. Must be initialized.
Paul Bakker8123e9d2011-01-06 15:37:30 +0000404 *
Rose Zadik9ba6b622018-01-24 12:59:19 +0000405 * \return <ul><li>If no IV has been set: the recommended IV size.
406 * 0 for ciphers not using IV or nonce.</li>
407 * <li>If IV has already been set: the actual size.</li></ul>
Paul Bakker8123e9d2011-01-06 15:37:30 +0000408 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200409static inline int mbedtls_cipher_get_iv_size( const mbedtls_cipher_context_t *ctx )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000410{
411 if( NULL == ctx || NULL == ctx->cipher_info )
412 return 0;
413
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200414 if( ctx->iv_size != 0 )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200415 return (int) ctx->iv_size;
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200416
Manuel Pégourié-Gonnard46c4fa12015-08-12 09:27:55 +0200417 return (int) ctx->cipher_info->iv_size;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000418}
419
420/**
Rose Zadik9ba6b622018-01-24 12:59:19 +0000421 * \brief This function returns the type of the given cipher.
Paul Bakker8123e9d2011-01-06 15:37:30 +0000422 *
Rose Zadik9ba6b622018-01-24 12:59:19 +0000423 * \param ctx The context of the cipher. Must be initialized.
Paul Bakker8123e9d2011-01-06 15:37:30 +0000424 *
Rose Zadik9ba6b622018-01-24 12:59:19 +0000425 * \return The type of the cipher, or #MBEDTLS_CIPHER_NONE if
426 * \p ctx has not been initialized.
Paul Bakker8123e9d2011-01-06 15:37:30 +0000427 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200428static inline mbedtls_cipher_type_t mbedtls_cipher_get_type( const mbedtls_cipher_context_t *ctx )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000429{
430 if( NULL == ctx || NULL == ctx->cipher_info )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200431 return MBEDTLS_CIPHER_NONE;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000432
433 return ctx->cipher_info->type;
434}
435
436/**
Rose Zadik9ba6b622018-01-24 12:59:19 +0000437 * \brief This function returns the name of the given cipher
438 * as a string.
Paul Bakker8123e9d2011-01-06 15:37:30 +0000439 *
Rose Zadik9ba6b622018-01-24 12:59:19 +0000440 * \param ctx The context of the cipher. Must be initialized.
Paul Bakker8123e9d2011-01-06 15:37:30 +0000441 *
Rose Zadik9ba6b622018-01-24 12:59:19 +0000442 * \return The name of the cipher, or NULL if \p ctx has not
443 * been not initialized.
Paul Bakker8123e9d2011-01-06 15:37:30 +0000444 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200445static inline const char *mbedtls_cipher_get_name( const mbedtls_cipher_context_t *ctx )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000446{
447 if( NULL == ctx || NULL == ctx->cipher_info )
448 return 0;
449
450 return ctx->cipher_info->name;
451}
452
453/**
Rose Zadik9ba6b622018-01-24 12:59:19 +0000454 * \brief This function returns the key length of the cipher.
Paul Bakker8123e9d2011-01-06 15:37:30 +0000455 *
Rose Zadik9ba6b622018-01-24 12:59:19 +0000456 * \param ctx The context of the cipher. Must be initialized.
Paul Bakker8123e9d2011-01-06 15:37:30 +0000457 *
Rose Zadik9ba6b622018-01-24 12:59:19 +0000458 * \return The key length of the cipher in bits, or
459 * #MBEDTLS_KEY_LENGTH_NONE if ctx \p has not been
460 * initialized.
Paul Bakker8123e9d2011-01-06 15:37:30 +0000461 */
Manuel Pégourié-Gonnard097c7bb2015-06-18 16:43:38 +0200462static inline int mbedtls_cipher_get_key_bitlen( const mbedtls_cipher_context_t *ctx )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000463{
Paul Bakker68884e32013-01-07 18:20:04 +0100464 if( NULL == ctx || NULL == ctx->cipher_info )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200465 return MBEDTLS_KEY_LENGTH_NONE;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000466
Manuel Pégourié-Gonnard46c4fa12015-08-12 09:27:55 +0200467 return (int) ctx->cipher_info->key_bitlen;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000468}
469
470/**
Rose Zadik9ba6b622018-01-24 12:59:19 +0000471 * \brief This function returns the operation of the given cipher.
Paul Bakkerf7e5bb52011-11-11 10:53:37 +0000472 *
Rose Zadik9ba6b622018-01-24 12:59:19 +0000473 * \param ctx The context of the cipher. Must be initialized.
Paul Bakkerf7e5bb52011-11-11 10:53:37 +0000474 *
Rose Zadik9ba6b622018-01-24 12:59:19 +0000475 * \return The type of operation: #MBEDTLS_ENCRYPT or
476 * #MBEDTLS_DECRYPT, or #MBEDTLS_OPERATION_NONE if \p ctx
477 * has not been initialized.
Paul Bakkerf7e5bb52011-11-11 10:53:37 +0000478 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200479static inline mbedtls_operation_t mbedtls_cipher_get_operation( const mbedtls_cipher_context_t *ctx )
Paul Bakkerf7e5bb52011-11-11 10:53:37 +0000480{
481 if( NULL == ctx || NULL == ctx->cipher_info )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200482 return MBEDTLS_OPERATION_NONE;
Paul Bakkerf7e5bb52011-11-11 10:53:37 +0000483
484 return ctx->operation;
485}
486
487/**
Rose Zadik9ba6b622018-01-24 12:59:19 +0000488 * \brief This function sets the key to use with the given context.
Paul Bakker8123e9d2011-01-06 15:37:30 +0000489 *
Rose Zadik9ba6b622018-01-24 12:59:19 +0000490 * \param ctx The generic cipher context. May not be NULL. Must have
491 * been initialized using mbedtls_cipher_info_from_type()
492 * or mbedtls_cipher_info_from_string().
Paul Bakker8123e9d2011-01-06 15:37:30 +0000493 * \param key The key to use.
Rose Zadik9ba6b622018-01-24 12:59:19 +0000494 * \param key_bitlen The key length to use, in bits.
495 * \param operation The operation that the key will be used for:
496 * #MBEDTLS_ENCRYPT or #MBEDTLS_DECRYPT.
Paul Bakker8123e9d2011-01-06 15:37:30 +0000497 *
Rose Zadik9ba6b622018-01-24 12:59:19 +0000498 * \returns \c 0 on success, #MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA if
499 * parameter verification fails, or a cipher-specific
Paul Bakkerff61a782011-06-09 15:42:02 +0000500 * error code.
Paul Bakker8123e9d2011-01-06 15:37:30 +0000501 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200502int mbedtls_cipher_setkey( mbedtls_cipher_context_t *ctx, const unsigned char *key,
Manuel Pégourié-Gonnardb8186a52015-06-18 14:58:58 +0200503 int key_bitlen, const mbedtls_operation_t operation );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000504
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200505#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Paul Bakker8123e9d2011-01-06 15:37:30 +0000506/**
Rose Zadik9ba6b622018-01-24 12:59:19 +0000507 * \brief This function sets the padding mode, for cipher modes
508 * that use padding.
Manuel Pégourié-Gonnardd5fdcaf2013-07-24 18:05:00 +0200509 *
Rose Zadik9ba6b622018-01-24 12:59:19 +0000510 * The default passing mode is PKCS7 padding.
Manuel Pégourié-Gonnardd5fdcaf2013-07-24 18:05:00 +0200511 *
Rose Zadik9ba6b622018-01-24 12:59:19 +0000512 * \param ctx The generic cipher context.
513 * \param mode The padding mode.
514 *
515 * \returns \c 0 on success, #MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE
516 * if the selected padding mode is not supported, or
517 * #MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA if the cipher mode
Paul Bakker1a45d912013-08-14 12:04:26 +0200518 * does not support padding.
Manuel Pégourié-Gonnardd5fdcaf2013-07-24 18:05:00 +0200519 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200520int mbedtls_cipher_set_padding_mode( mbedtls_cipher_context_t *ctx, mbedtls_cipher_padding_t mode );
521#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
Manuel Pégourié-Gonnardd5fdcaf2013-07-24 18:05:00 +0200522
523/**
Rose Zadik9ba6b622018-01-24 12:59:19 +0000524 * \brief This function sets the initialization vector (IV)
525 * or nonce.
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200526 *
Rose Zadik9ba6b622018-01-24 12:59:19 +0000527 * \param ctx The generic cipher context.
528 * \param iv The IV to use, or NONCE_COUNTER for CTR-mode ciphers.
529 * \param iv_len The IV length for ciphers with variable-size IV.
530 * This parameter is discarded by ciphers with fixed-size IV.
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200531 *
Rose Zadik9ba6b622018-01-24 12:59:19 +0000532 * \returns \c 0 on success, or #MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200533 *
Rose Zadik9ba6b622018-01-24 12:59:19 +0000534 * \note Some ciphers do not use IVs nor nonce. For these
535 * ciphers, this function has no effect.
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200536 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200537int mbedtls_cipher_set_iv( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200538 const unsigned char *iv, size_t iv_len );
539
540/**
Rose Zadik9ba6b622018-01-24 12:59:19 +0000541 * \brief This function resets the cipher state.
Paul Bakker8123e9d2011-01-06 15:37:30 +0000542 *
Rose Zadik9ba6b622018-01-24 12:59:19 +0000543 * \param ctx The generic cipher context.
Paul Bakker8123e9d2011-01-06 15:37:30 +0000544 *
Rose Zadik9ba6b622018-01-24 12:59:19 +0000545 * \returns \c 0 on success, #MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA
546 * if parameter verification fails.
Paul Bakker8123e9d2011-01-06 15:37:30 +0000547 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200548int mbedtls_cipher_reset( mbedtls_cipher_context_t *ctx );
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200549
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200550#if defined(MBEDTLS_GCM_C)
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200551/**
Rose Zadik9ba6b622018-01-24 12:59:19 +0000552 * \brief This function adds additional data for AEAD ciphers.
553 * Only supported with GCM. Must be called
554 * exactly once, after mbedtls_cipher_reset().
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200555 *
Rose Zadik9ba6b622018-01-24 12:59:19 +0000556 * \param ctx The generic cipher context.
557 * \param ad The additional data to use.
558 * \param ad_len the Length of \p ad.
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200559 *
Rose Zadik9ba6b622018-01-24 12:59:19 +0000560 * \return \c 0 on success, or a specific error code on failure.
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200561 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200562int mbedtls_cipher_update_ad( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200563 const unsigned char *ad, size_t ad_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200564#endif /* MBEDTLS_GCM_C */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000565
566/**
Rose Zadik9ba6b622018-01-24 12:59:19 +0000567 * \brief The generic cipher update function. It encrypts or
568 * decrypts using the given cipher context. Writes as
569 * many block-sized blocks of data as possible to output.
570 * Any data that cannot be written immediately is either
571 * added to the next block, or flushed when
572 * mbedtls_cipher_finish() is called.
573 * Exception: For MBEDTLS_MODE_ECB, expects a single block
574 * in size. For example, 16 Bytes for AES.
Paul Bakker8123e9d2011-01-06 15:37:30 +0000575 *
Rose Zadik9ba6b622018-01-24 12:59:19 +0000576 * \param ctx The generic cipher context.
577 * \param input The buffer holding the input data.
578 * \param ilen The length of the input data.
579 * \param output The buffer for the output data. Must be able to hold at
580 * least \p ilen + block_size. Must not be the same buffer
581 * as input.
582 * \param olen The length of the output data, to be updated with the
583 * actual number of Bytes written.
Paul Bakker8123e9d2011-01-06 15:37:30 +0000584 *
Rose Zadik9ba6b622018-01-24 12:59:19 +0000585 * \returns \c 0 on success, #MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA if
Paul Bakkerff61a782011-06-09 15:42:02 +0000586 * parameter verification fails,
Rose Zadik9ba6b622018-01-24 12:59:19 +0000587 * #MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE on an
588 * unsupported mode for a cipher, or a cipher-specific
Paul Bakkerff61a782011-06-09 15:42:02 +0000589 * error code.
Manuel Pégourié-Gonnardb8bd5932013-09-05 13:38:15 +0200590 *
591 * \note If the underlying cipher is GCM, all calls to this
Rose Zadik9ba6b622018-01-24 12:59:19 +0000592 * function, except the last one before
593 * mbedtls_cipher_finish(). Must have \p ilen as a
594 * multiple of the block_size.
Paul Bakker8123e9d2011-01-06 15:37:30 +0000595 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200596int mbedtls_cipher_update( mbedtls_cipher_context_t *ctx, const unsigned char *input,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200597 size_t ilen, unsigned char *output, size_t *olen );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000598
599/**
Rose Zadik9ba6b622018-01-24 12:59:19 +0000600 * \brief The generic cipher finalization function. If data still
601 * needs to be flushed from an incomplete block, the data
602 * contained in it is padded to the size of
603 * the last block, and written to the \p output buffer.
Paul Bakker8123e9d2011-01-06 15:37:30 +0000604 *
Rose Zadik9ba6b622018-01-24 12:59:19 +0000605 * \param ctx The generic cipher context.
606 * \param output The buffer to write data to. Needs block_size available.
607 * \param olen The length of the data written to the \p output buffer.
Paul Bakker8123e9d2011-01-06 15:37:30 +0000608 *
Rose Zadik9ba6b622018-01-24 12:59:19 +0000609 * \returns \c 0 on success, #MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA if
Paul Bakkerff61a782011-06-09 15:42:02 +0000610 * parameter verification fails,
Rose Zadik9ba6b622018-01-24 12:59:19 +0000611 * #MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED if decryption
Paul Bakkerff61a782011-06-09 15:42:02 +0000612 * expected a full block but was not provided one,
Rose Zadik9ba6b622018-01-24 12:59:19 +0000613 * #MBEDTLS_ERR_CIPHER_INVALID_PADDING on invalid padding
614 * while decrypting, or a cipher-specific error code
615 * on failure for any other reason.
Paul Bakker8123e9d2011-01-06 15:37:30 +0000616 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200617int mbedtls_cipher_finish( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200618 unsigned char *output, size_t *olen );
619
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200620#if defined(MBEDTLS_GCM_C)
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200621/**
Rose Zadik9ba6b622018-01-24 12:59:19 +0000622 * \brief This function writes a tag for AEAD ciphers.
623 * Only supported with GCM.
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200624 * Must be called after mbedtls_cipher_finish().
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200625 *
Rose Zadik9ba6b622018-01-24 12:59:19 +0000626 * \param ctx The generic cipher context.
627 * \param tag The buffer to write the tag to.
628 * \param tag_len The length of the tag to write.
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200629 *
Rose Zadik9ba6b622018-01-24 12:59:19 +0000630 * \return \c 0 on success, or a specific error code on failure.
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200631 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200632int mbedtls_cipher_write_tag( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200633 unsigned char *tag, size_t tag_len );
634
635/**
Rose Zadik9ba6b622018-01-24 12:59:19 +0000636 * \brief This function checks the tag for AEAD ciphers.
637 * Only supported with GCM.
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200638 * Must be called after mbedtls_cipher_finish().
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200639 *
Rose Zadik9ba6b622018-01-24 12:59:19 +0000640 * \param ctx The generic cipher context.
641 * \param tag The buffer holding the tag.
642 * \param tag_len The length of the tag to check.
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200643 *
Rose Zadik9ba6b622018-01-24 12:59:19 +0000644 * \return \c 0 on success, or a specific error code on failure.
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200645 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200646int mbedtls_cipher_check_tag( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200647 const unsigned char *tag, size_t tag_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200648#endif /* MBEDTLS_GCM_C */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000649
Paul Bakker8123e9d2011-01-06 15:37:30 +0000650/**
Rose Zadik9ba6b622018-01-24 12:59:19 +0000651 * \brief The generic all-in-one encryption/decryption function,
652 * for all ciphers except AEAD constructs.
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +0200653 *
Rose Zadik9ba6b622018-01-24 12:59:19 +0000654 * \param ctx The generic cipher context.
655 * \param iv The IV to use, or NONCE_COUNTER for CTR-mode ciphers.
656 * \param iv_len The IV length for ciphers with variable-size IV.
657 * This parameter is discarded by ciphers with fixed-size
658 * IV.
659 * \param input The buffer holding the input data.
660 * \param ilen The length of the input data.
661 * \param output The buffer for the output data. Must be able to hold at
662 * least \p ilen + block_size. Must not be the same buffer
663 * as input.
664 * \param olen The length of the output data, to be updated with the
665 * actual number of Bytes written.
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +0200666 *
Rose Zadik9ba6b622018-01-24 12:59:19 +0000667 * \note Some ciphers do not use IVs nor nonce. For these
668 * ciphers, use \p iv = NULL and \p iv_len = 0.
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +0200669 *
Rose Zadik9ba6b622018-01-24 12:59:19 +0000670 * \returns \c 0 on success, or
671 * #MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA, or
672 * #MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED if decryption
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +0200673 * expected a full block but was not provided one, or
Rose Zadik9ba6b622018-01-24 12:59:19 +0000674 * #MBEDTLS_ERR_CIPHER_INVALID_PADDING on invalid padding
675 * while decrypting, or a cipher-specific error code on
676 * failure for any other reason.
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +0200677 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200678int mbedtls_cipher_crypt( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +0200679 const unsigned char *iv, size_t iv_len,
680 const unsigned char *input, size_t ilen,
681 unsigned char *output, size_t *olen );
682
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200683#if defined(MBEDTLS_CIPHER_MODE_AEAD)
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200684/**
Rose Zadik9ba6b622018-01-24 12:59:19 +0000685 * \brief The generic autenticated encryption (AEAD) function.
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200686 *
Rose Zadik9ba6b622018-01-24 12:59:19 +0000687 * \param ctx The generic cipher context.
688 * \param iv The IV to use, or NONCE_COUNTER for CTR-mode ciphers.
689 * \param iv_len The IV length for ciphers with variable-size IV.
690 * This parameter is discarded by ciphers with fixed-size IV.
691 * \param ad The additional data to authenticate.
692 * \param ad_len The length of \p ad.
693 * \param input The buffer holding the input data.
694 * \param ilen The length of the input data.
695 * \param output The buffer for the output data.
696 * Must be able to hold at least \p ilen.
697 * \param olen The length of the output data, to be updated with the
698 * actual number of Bytes written.
699 * \param tag The buffer for the authentication tag.
700 * \param tag_len The desired length of the authentication tag.
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200701 *
Rose Zadik9ba6b622018-01-24 12:59:19 +0000702 * \returns \c 0 on success, or
703 * #MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA, or
704 * a cipher-specific error code.
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200705 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200706int mbedtls_cipher_auth_encrypt( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200707 const unsigned char *iv, size_t iv_len,
708 const unsigned char *ad, size_t ad_len,
709 const unsigned char *input, size_t ilen,
710 unsigned char *output, size_t *olen,
711 unsigned char *tag, size_t tag_len );
712
713/**
Rose Zadik9ba6b622018-01-24 12:59:19 +0000714 * \brief The generic autenticated decryption (AEAD) function.
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200715 *
Rose Zadik9ba6b622018-01-24 12:59:19 +0000716 * \param ctx The generic cipher context.
717 * \param iv The IV to use, or NONCE_COUNTER for CTR-mode ciphers.
718 * \param iv_len The IV length for ciphers with variable-size IV.
719 * This parameter is discarded by ciphers with fixed-size IV.
720 * \param ad The additional data to be authenticated.
721 * \param ad_len The length of \p ad.
722 * \param input The buffer holding the input data.
723 * \param ilen The length of the input data.
724 * \param output The buffer for the output data.
725 * Must be able to hold at least \p ilen.
726 * \param olen The length of the output data, to be updated with the
727 * actual number of Bytes written.
728 * \param tag The buffer holding the authentication tag.
729 * \param tag_len The length of the authentication tag.
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200730 *
Rose Zadik9ba6b622018-01-24 12:59:19 +0000731 * \returns \c 0 on success, or
732 * #MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA, or
733 * #MBEDTLS_ERR_CIPHER_AUTH_FAILED if data is not authentic,
734 * or a cipher-specific error code on failure for any other reason.
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200735 *
736 * \note If the data is not authentic, then the output buffer
Rose Zadik9ba6b622018-01-24 12:59:19 +0000737 * is zeroed out to prevent the unauthentic plaintext being
738 * used, making this interface safer.
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200739 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200740int mbedtls_cipher_auth_decrypt( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200741 const unsigned char *iv, size_t iv_len,
742 const unsigned char *ad, size_t ad_len,
743 const unsigned char *input, size_t ilen,
744 unsigned char *output, size_t *olen,
745 const unsigned char *tag, size_t tag_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200746#endif /* MBEDTLS_CIPHER_MODE_AEAD */
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200747
Paul Bakker8123e9d2011-01-06 15:37:30 +0000748#ifdef __cplusplus
749}
750#endif
751
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200752#endif /* MBEDTLS_CIPHER_H */