Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 1 | /** |
| 2 | * \file cipher.h |
Paul Bakker | 9af723c | 2014-05-01 13:03:14 +0200 | [diff] [blame] | 3 | * |
Rose Zadik | 9ba6b62 | 2018-01-24 12:59:19 +0000 | [diff] [blame] | 4 | * \brief The generic cipher wrapper. |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 5 | * |
| 6 | * \author Adriaan de Jong <dejong@fox-it.com> |
Darryl Green | a40a101 | 2018-01-05 15:33:17 +0000 | [diff] [blame] | 7 | */ |
| 8 | /* |
Bence Szépkúti | 44bfbe3 | 2020-08-19 16:54:51 +0200 | [diff] [blame] | 9 | * Copyright The Mbed TLS Contributors |
Bence Szépkúti | 4e9f712 | 2020-06-05 13:02:18 +0200 | [diff] [blame] | 10 | * 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é-Gonnard | 37ff140 | 2015-09-04 14:21:07 +0200 | [diff] [blame] | 17 | * |
| 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 Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 29 | * |
Bence Szépkúti | 4e9f712 | 2020-06-05 13:02:18 +0200 | [diff] [blame] | 30 | * ********** |
| 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 | * ********** |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 50 | */ |
| 51 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 52 | #ifndef MBEDTLS_CIPHER_H |
| 53 | #define MBEDTLS_CIPHER_H |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 54 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 55 | #if !defined(MBEDTLS_CONFIG_FILE) |
Manuel Pégourié-Gonnard | 43a4780 | 2013-09-03 16:35:53 +0200 | [diff] [blame] | 56 | #include "config.h" |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 57 | #else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 58 | #include MBEDTLS_CONFIG_FILE |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 59 | #endif |
Manuel Pégourié-Gonnard | 43a4780 | 2013-09-03 16:35:53 +0200 | [diff] [blame] | 60 | |
Rich Evans | 00ab470 | 2015-02-06 13:43:58 +0000 | [diff] [blame] | 61 | #include <stddef.h> |
| 62 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 63 | #if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CCM_C) |
| 64 | #define MBEDTLS_CIPHER_MODE_AEAD |
Manuel Pégourié-Gonnard | 43a4780 | 2013-09-03 16:35:53 +0200 | [diff] [blame] | 65 | #endif |
| 66 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 67 | #if defined(MBEDTLS_CIPHER_MODE_CBC) |
| 68 | #define MBEDTLS_CIPHER_MODE_WITH_PADDING |
Manuel Pégourié-Gonnard | 989ed38 | 2013-09-13 14:41:45 +0200 | [diff] [blame] | 69 | #endif |
| 70 | |
Ron Eldor | 0557b8f | 2018-06-27 18:33:13 +0300 | [diff] [blame] | 71 | #if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 72 | #define MBEDTLS_CIPHER_MODE_STREAM |
Manuel Pégourié-Gonnard | b8ca723 | 2014-12-02 10:09:10 +0100 | [diff] [blame] | 73 | #endif |
| 74 | |
Manuel Pégourié-Gonnard | 0223ab9 | 2015-10-05 11:40:01 +0100 | [diff] [blame] | 75 | #if ( defined(__ARMCC_VERSION) || defined(_MSC_VER) ) && \ |
| 76 | !defined(inline) && !defined(__cplusplus) |
Paul Bakker | 569df2c | 2011-06-21 07:48:07 +0000 | [diff] [blame] | 77 | #define inline __inline |
Manuel Pégourié-Gonnard | 20af64d | 2015-07-07 18:33:39 +0200 | [diff] [blame] | 78 | #endif |
Paul Bakker | af5c85f | 2011-04-18 03:47:52 +0000 | [diff] [blame] | 79 | |
Rose Zadik | 9ba6b62 | 2018-01-24 12:59:19 +0000 | [diff] [blame] | 80 | #define MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE -0x6080 /**< The selected feature is not available. */ |
| 81 | #define MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA -0x6100 /**< Bad input parameters. */ |
| 82 | #define MBEDTLS_ERR_CIPHER_ALLOC_FAILED -0x6180 /**< Failed to allocate memory. */ |
| 83 | #define MBEDTLS_ERR_CIPHER_INVALID_PADDING -0x6200 /**< Input data contains invalid padding and is rejected. */ |
| 84 | #define MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED -0x6280 /**< Decryption of block requires a full block. */ |
| 85 | #define MBEDTLS_ERR_CIPHER_AUTH_FAILED -0x6300 /**< Authentication failed (for AEAD modes). */ |
| 86 | #define MBEDTLS_ERR_CIPHER_INVALID_CONTEXT -0x6380 /**< The context is invalid. For example, because it was freed. */ |
| 87 | #define MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED -0x6400 /**< Cipher hardware accelerator failed. */ |
Paul Bakker | 343a870 | 2011-06-09 14:27:58 +0000 | [diff] [blame] | 88 | |
Rose Zadik | 9ba6b62 | 2018-01-24 12:59:19 +0000 | [diff] [blame] | 89 | #define MBEDTLS_CIPHER_VARIABLE_IV_LEN 0x01 /**< Cipher accepts IVs of variable length. */ |
| 90 | #define MBEDTLS_CIPHER_VARIABLE_KEY_LEN 0x02 /**< Cipher accepts keys of variable length. */ |
Manuel Pégourié-Gonnard | 81754a0 | 2014-06-23 11:33:18 +0200 | [diff] [blame] | 91 | |
Paul Bakker | 407a0da | 2013-06-27 14:29:21 +0200 | [diff] [blame] | 92 | #ifdef __cplusplus |
| 93 | extern "C" { |
| 94 | #endif |
| 95 | |
Hanno Becker | bbca8c5 | 2017-09-25 14:53:51 +0100 | [diff] [blame] | 96 | /** |
| 97 | * \brief An enumeration of supported ciphers. |
| 98 | * |
| 99 | * \warning ARC4 and DES are considered weak ciphers and their use |
| 100 | * constitutes a security risk. We recommend considering stronger |
| 101 | * ciphers instead. |
| 102 | */ |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 103 | typedef enum { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 104 | MBEDTLS_CIPHER_ID_NONE = 0, |
| 105 | MBEDTLS_CIPHER_ID_NULL, |
| 106 | MBEDTLS_CIPHER_ID_AES, |
| 107 | MBEDTLS_CIPHER_ID_DES, |
| 108 | MBEDTLS_CIPHER_ID_3DES, |
| 109 | MBEDTLS_CIPHER_ID_CAMELLIA, |
| 110 | MBEDTLS_CIPHER_ID_BLOWFISH, |
| 111 | MBEDTLS_CIPHER_ID_ARC4, |
| 112 | } mbedtls_cipher_id_t; |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 113 | |
Hanno Becker | bbca8c5 | 2017-09-25 14:53:51 +0100 | [diff] [blame] | 114 | /** |
| 115 | * \brief An enumeration of supported (cipher, mode) pairs. |
| 116 | * |
| 117 | * \warning ARC4 and DES are considered weak ciphers and their use |
| 118 | * constitutes a security risk. We recommend considering stronger |
| 119 | * ciphers instead. |
| 120 | */ |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 121 | typedef enum { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 122 | MBEDTLS_CIPHER_NONE = 0, |
| 123 | MBEDTLS_CIPHER_NULL, |
| 124 | MBEDTLS_CIPHER_AES_128_ECB, |
| 125 | MBEDTLS_CIPHER_AES_192_ECB, |
| 126 | MBEDTLS_CIPHER_AES_256_ECB, |
| 127 | MBEDTLS_CIPHER_AES_128_CBC, |
| 128 | MBEDTLS_CIPHER_AES_192_CBC, |
| 129 | MBEDTLS_CIPHER_AES_256_CBC, |
| 130 | MBEDTLS_CIPHER_AES_128_CFB128, |
| 131 | MBEDTLS_CIPHER_AES_192_CFB128, |
| 132 | MBEDTLS_CIPHER_AES_256_CFB128, |
| 133 | MBEDTLS_CIPHER_AES_128_CTR, |
| 134 | MBEDTLS_CIPHER_AES_192_CTR, |
| 135 | MBEDTLS_CIPHER_AES_256_CTR, |
| 136 | MBEDTLS_CIPHER_AES_128_GCM, |
| 137 | MBEDTLS_CIPHER_AES_192_GCM, |
| 138 | MBEDTLS_CIPHER_AES_256_GCM, |
| 139 | MBEDTLS_CIPHER_CAMELLIA_128_ECB, |
| 140 | MBEDTLS_CIPHER_CAMELLIA_192_ECB, |
| 141 | MBEDTLS_CIPHER_CAMELLIA_256_ECB, |
| 142 | MBEDTLS_CIPHER_CAMELLIA_128_CBC, |
| 143 | MBEDTLS_CIPHER_CAMELLIA_192_CBC, |
| 144 | MBEDTLS_CIPHER_CAMELLIA_256_CBC, |
| 145 | MBEDTLS_CIPHER_CAMELLIA_128_CFB128, |
| 146 | MBEDTLS_CIPHER_CAMELLIA_192_CFB128, |
| 147 | MBEDTLS_CIPHER_CAMELLIA_256_CFB128, |
| 148 | MBEDTLS_CIPHER_CAMELLIA_128_CTR, |
| 149 | MBEDTLS_CIPHER_CAMELLIA_192_CTR, |
| 150 | MBEDTLS_CIPHER_CAMELLIA_256_CTR, |
| 151 | MBEDTLS_CIPHER_CAMELLIA_128_GCM, |
| 152 | MBEDTLS_CIPHER_CAMELLIA_192_GCM, |
| 153 | MBEDTLS_CIPHER_CAMELLIA_256_GCM, |
| 154 | MBEDTLS_CIPHER_DES_ECB, |
| 155 | MBEDTLS_CIPHER_DES_CBC, |
| 156 | MBEDTLS_CIPHER_DES_EDE_ECB, |
| 157 | MBEDTLS_CIPHER_DES_EDE_CBC, |
| 158 | MBEDTLS_CIPHER_DES_EDE3_ECB, |
| 159 | MBEDTLS_CIPHER_DES_EDE3_CBC, |
| 160 | MBEDTLS_CIPHER_BLOWFISH_ECB, |
| 161 | MBEDTLS_CIPHER_BLOWFISH_CBC, |
| 162 | MBEDTLS_CIPHER_BLOWFISH_CFB64, |
| 163 | MBEDTLS_CIPHER_BLOWFISH_CTR, |
| 164 | MBEDTLS_CIPHER_ARC4_128, |
| 165 | MBEDTLS_CIPHER_AES_128_CCM, |
| 166 | MBEDTLS_CIPHER_AES_192_CCM, |
| 167 | MBEDTLS_CIPHER_AES_256_CCM, |
| 168 | MBEDTLS_CIPHER_CAMELLIA_128_CCM, |
| 169 | MBEDTLS_CIPHER_CAMELLIA_192_CCM, |
| 170 | MBEDTLS_CIPHER_CAMELLIA_256_CCM, |
| 171 | } mbedtls_cipher_type_t; |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 172 | |
Rose Zadik | 9ba6b62 | 2018-01-24 12:59:19 +0000 | [diff] [blame] | 173 | /** Supported cipher modes. */ |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 174 | typedef enum { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 175 | MBEDTLS_MODE_NONE = 0, |
| 176 | MBEDTLS_MODE_ECB, |
| 177 | MBEDTLS_MODE_CBC, |
| 178 | MBEDTLS_MODE_CFB, |
| 179 | MBEDTLS_MODE_OFB, /* Unused! */ |
| 180 | MBEDTLS_MODE_CTR, |
| 181 | MBEDTLS_MODE_GCM, |
| 182 | MBEDTLS_MODE_STREAM, |
| 183 | MBEDTLS_MODE_CCM, |
| 184 | } mbedtls_cipher_mode_t; |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 185 | |
Rose Zadik | 9ba6b62 | 2018-01-24 12:59:19 +0000 | [diff] [blame] | 186 | /** Supported cipher padding types. */ |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 187 | typedef enum { |
Rose Zadik | 9ba6b62 | 2018-01-24 12:59:19 +0000 | [diff] [blame] | 188 | MBEDTLS_PADDING_PKCS7 = 0, /**< PKCS7 padding (default). */ |
| 189 | MBEDTLS_PADDING_ONE_AND_ZEROS, /**< ISO/IEC 7816-4 padding. */ |
| 190 | MBEDTLS_PADDING_ZEROS_AND_LEN, /**< ANSI X.923 padding. */ |
| 191 | MBEDTLS_PADDING_ZEROS, /**< zero padding (not reversible). */ |
| 192 | MBEDTLS_PADDING_NONE, /**< never pad (full blocks only). */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 193 | } mbedtls_cipher_padding_t; |
Manuel Pégourié-Gonnard | d5fdcaf | 2013-07-24 18:05:00 +0200 | [diff] [blame] | 194 | |
Rose Zadik | 9ba6b62 | 2018-01-24 12:59:19 +0000 | [diff] [blame] | 195 | /** Type of operation. */ |
Manuel Pégourié-Gonnard | d5fdcaf | 2013-07-24 18:05:00 +0200 | [diff] [blame] | 196 | typedef enum { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 197 | MBEDTLS_OPERATION_NONE = -1, |
| 198 | MBEDTLS_DECRYPT = 0, |
| 199 | MBEDTLS_ENCRYPT, |
| 200 | } mbedtls_operation_t; |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 201 | |
| 202 | enum { |
Rose Zadik | 9ba6b62 | 2018-01-24 12:59:19 +0000 | [diff] [blame] | 203 | /** Undefined key length. */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 204 | MBEDTLS_KEY_LENGTH_NONE = 0, |
Rose Zadik | 9ba6b62 | 2018-01-24 12:59:19 +0000 | [diff] [blame] | 205 | /** Key length, in bits (including parity), for DES keys. */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 206 | MBEDTLS_KEY_LENGTH_DES = 64, |
Rose Zadik | 9ba6b62 | 2018-01-24 12:59:19 +0000 | [diff] [blame] | 207 | /** Key length in bits, including parity, for DES in two-key EDE. */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 208 | MBEDTLS_KEY_LENGTH_DES_EDE = 128, |
Rose Zadik | 9ba6b62 | 2018-01-24 12:59:19 +0000 | [diff] [blame] | 209 | /** Key length in bits, including parity, for DES in three-key EDE. */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 210 | MBEDTLS_KEY_LENGTH_DES_EDE3 = 192, |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 211 | }; |
| 212 | |
Rose Zadik | 9ba6b62 | 2018-01-24 12:59:19 +0000 | [diff] [blame] | 213 | /** Maximum length of any IV, in Bytes. */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 214 | #define MBEDTLS_MAX_IV_LENGTH 16 |
Rose Zadik | 9ba6b62 | 2018-01-24 12:59:19 +0000 | [diff] [blame] | 215 | /** Maximum block size of any cipher, in Bytes. */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 216 | #define MBEDTLS_MAX_BLOCK_LENGTH 16 |
Manuel Pégourié-Gonnard | 0b58c15 | 2013-10-24 17:17:54 +0200 | [diff] [blame] | 217 | |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 218 | /** |
Manuel Pégourié-Gonnard | 5a74e8b | 2015-05-06 17:10:55 +0100 | [diff] [blame] | 219 | * Base cipher information (opaque struct). |
Paul Bakker | 343a870 | 2011-06-09 14:27:58 +0000 | [diff] [blame] | 220 | */ |
Manuel Pégourié-Gonnard | 5a74e8b | 2015-05-06 17:10:55 +0100 | [diff] [blame] | 221 | typedef struct mbedtls_cipher_base_t mbedtls_cipher_base_t; |
Paul Bakker | 343a870 | 2011-06-09 14:27:58 +0000 | [diff] [blame] | 222 | |
| 223 | /** |
Simon Butcher | 327398a | 2016-10-05 14:09:11 +0100 | [diff] [blame] | 224 | * CMAC context (opaque struct). |
| 225 | */ |
| 226 | typedef struct mbedtls_cmac_context_t mbedtls_cmac_context_t; |
| 227 | |
| 228 | /** |
Rose Zadik | 9ba6b62 | 2018-01-24 12:59:19 +0000 | [diff] [blame] | 229 | * Cipher information. Allows calling cipher functions |
| 230 | * in a generic way. |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 231 | */ |
| 232 | typedef struct { |
Rose Zadik | 9ba6b62 | 2018-01-24 12:59:19 +0000 | [diff] [blame] | 233 | /** Full cipher identifier. For example, |
| 234 | * MBEDTLS_CIPHER_AES_256_CBC. |
| 235 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 236 | mbedtls_cipher_type_t type; |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 237 | |
Rose Zadik | 9ba6b62 | 2018-01-24 12:59:19 +0000 | [diff] [blame] | 238 | /** The cipher mode. For example, MBEDTLS_MODE_CBC. */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 239 | mbedtls_cipher_mode_t mode; |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 240 | |
Rose Zadik | 9ba6b62 | 2018-01-24 12:59:19 +0000 | [diff] [blame] | 241 | /** The cipher key length, in bits. This is the |
| 242 | * default length for variable sized ciphers. |
| 243 | * Includes parity bits for ciphers like DES. |
| 244 | */ |
Manuel Pégourié-Gonnard | 898e0aa | 2015-06-18 15:28:12 +0200 | [diff] [blame] | 245 | unsigned int key_bitlen; |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 246 | |
Rose Zadik | 9ba6b62 | 2018-01-24 12:59:19 +0000 | [diff] [blame] | 247 | /** Name of the cipher. */ |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 248 | const char * name; |
| 249 | |
Rose Zadik | 9ba6b62 | 2018-01-24 12:59:19 +0000 | [diff] [blame] | 250 | /** IV or nonce size, in Bytes. |
| 251 | * For ciphers that accept variable IV sizes, |
| 252 | * this is the recommended size. |
| 253 | */ |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 254 | unsigned int iv_size; |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 255 | |
Rose Zadik | 9ba6b62 | 2018-01-24 12:59:19 +0000 | [diff] [blame] | 256 | /** Flags to set. For example, if the cipher supports variable IV sizes or variable key sizes. */ |
Manuel Pégourié-Gonnard | 81754a0 | 2014-06-23 11:33:18 +0200 | [diff] [blame] | 257 | int flags; |
Manuel Pégourié-Gonnard | a235b5b | 2013-09-03 13:25:52 +0200 | [diff] [blame] | 258 | |
Rose Zadik | 9ba6b62 | 2018-01-24 12:59:19 +0000 | [diff] [blame] | 259 | /** The block size, in Bytes. */ |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 260 | unsigned int block_size; |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 261 | |
Rose Zadik | 9ba6b62 | 2018-01-24 12:59:19 +0000 | [diff] [blame] | 262 | /** Struct for base cipher information and functions. */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 263 | const mbedtls_cipher_base_t *base; |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 264 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 265 | } mbedtls_cipher_info_t; |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 266 | |
| 267 | /** |
Paul Bakker | 2028156 | 2011-11-11 10:34:04 +0000 | [diff] [blame] | 268 | * Generic cipher context. |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 269 | */ |
| 270 | typedef struct { |
Rose Zadik | 9ba6b62 | 2018-01-24 12:59:19 +0000 | [diff] [blame] | 271 | /** Information about the associated cipher. */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 272 | const mbedtls_cipher_info_t *cipher_info; |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 273 | |
Rose Zadik | 9ba6b62 | 2018-01-24 12:59:19 +0000 | [diff] [blame] | 274 | /** Key length to use. */ |
Manuel Pégourié-Gonnard | 898e0aa | 2015-06-18 15:28:12 +0200 | [diff] [blame] | 275 | int key_bitlen; |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 276 | |
Rose Zadik | 9ba6b62 | 2018-01-24 12:59:19 +0000 | [diff] [blame] | 277 | /** Operation that the key of the context has been |
| 278 | * initialized for. |
| 279 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 280 | mbedtls_operation_t operation; |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 281 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 282 | #if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING) |
Rose Zadik | 9ba6b62 | 2018-01-24 12:59:19 +0000 | [diff] [blame] | 283 | /** Padding functions to use, if relevant for |
| 284 | * the specific cipher mode. |
| 285 | */ |
Manuel Pégourié-Gonnard | ac56a1a | 2013-07-25 12:31:10 +0200 | [diff] [blame] | 286 | void (*add_padding)( unsigned char *output, size_t olen, size_t data_len ); |
| 287 | int (*get_padding)( unsigned char *input, size_t ilen, size_t *data_len ); |
Manuel Pégourié-Gonnard | b8ca723 | 2014-12-02 10:09:10 +0100 | [diff] [blame] | 288 | #endif |
Manuel Pégourié-Gonnard | ac56a1a | 2013-07-25 12:31:10 +0200 | [diff] [blame] | 289 | |
Rose Zadik | 9ba6b62 | 2018-01-24 12:59:19 +0000 | [diff] [blame] | 290 | /** Buffer for input that has not been processed yet. */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 291 | unsigned char unprocessed_data[MBEDTLS_MAX_BLOCK_LENGTH]; |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 292 | |
Rose Zadik | 9ba6b62 | 2018-01-24 12:59:19 +0000 | [diff] [blame] | 293 | /** Number of Bytes that have not been processed yet. */ |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 294 | size_t unprocessed_len; |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 295 | |
Rose Zadik | 9ba6b62 | 2018-01-24 12:59:19 +0000 | [diff] [blame] | 296 | /** Current IV or NONCE_COUNTER for CTR-mode. */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 297 | unsigned char iv[MBEDTLS_MAX_IV_LENGTH]; |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 298 | |
Rose Zadik | 9ba6b62 | 2018-01-24 12:59:19 +0000 | [diff] [blame] | 299 | /** IV size in Bytes, for ciphers with variable-length IVs. */ |
Manuel Pégourié-Gonnard | 9c853b9 | 2013-09-03 13:04:44 +0200 | [diff] [blame] | 300 | size_t iv_size; |
| 301 | |
Rose Zadik | 9ba6b62 | 2018-01-24 12:59:19 +0000 | [diff] [blame] | 302 | /** The cipher-specific context. */ |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 303 | void *cipher_ctx; |
Simon Butcher | 327398a | 2016-10-05 14:09:11 +0100 | [diff] [blame] | 304 | |
| 305 | #if defined(MBEDTLS_CMAC_C) |
Rose Zadik | 9ba6b62 | 2018-01-24 12:59:19 +0000 | [diff] [blame] | 306 | /** CMAC-specific context. */ |
Simon Butcher | 327398a | 2016-10-05 14:09:11 +0100 | [diff] [blame] | 307 | mbedtls_cmac_context_t *cmac_ctx; |
| 308 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 309 | } mbedtls_cipher_context_t; |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 310 | |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 311 | /** |
Rose Zadik | 9ba6b62 | 2018-01-24 12:59:19 +0000 | [diff] [blame] | 312 | * \brief This function retrieves the list of ciphers supported by the generic |
| 313 | * cipher module. |
Paul Bakker | 72f6266 | 2011-01-16 21:27:44 +0000 | [diff] [blame] | 314 | * |
Rose Zadik | 9ba6b62 | 2018-01-24 12:59:19 +0000 | [diff] [blame] | 315 | * \return A statically-allocated array of ciphers. The last entry |
| 316 | * is zero. |
Paul Bakker | 72f6266 | 2011-01-16 21:27:44 +0000 | [diff] [blame] | 317 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 318 | const int *mbedtls_cipher_list( void ); |
Paul Bakker | 72f6266 | 2011-01-16 21:27:44 +0000 | [diff] [blame] | 319 | |
| 320 | /** |
Rose Zadik | 9ba6b62 | 2018-01-24 12:59:19 +0000 | [diff] [blame] | 321 | * \brief This function retrieves the cipher-information |
| 322 | * structure associated with the given cipher name. |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 323 | * |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 324 | * \param cipher_name Name of the cipher to search for. |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 325 | * |
Rose Zadik | 9ba6b62 | 2018-01-24 12:59:19 +0000 | [diff] [blame] | 326 | * \return The cipher information structure associated with the |
| 327 | * given \p cipher_name, or NULL if not found. |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 328 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 329 | const mbedtls_cipher_info_t *mbedtls_cipher_info_from_string( const char *cipher_name ); |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 330 | |
| 331 | /** |
Rose Zadik | 9ba6b62 | 2018-01-24 12:59:19 +0000 | [diff] [blame] | 332 | * \brief This function retrieves the cipher-information |
| 333 | * structure associated with the given cipher type. |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 334 | * |
| 335 | * \param cipher_type Type of the cipher to search for. |
| 336 | * |
Rose Zadik | 9ba6b62 | 2018-01-24 12:59:19 +0000 | [diff] [blame] | 337 | * \return The cipher information structure associated with the |
| 338 | * given \p cipher_type, or NULL if not found. |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 339 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 340 | const mbedtls_cipher_info_t *mbedtls_cipher_info_from_type( const mbedtls_cipher_type_t cipher_type ); |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 341 | |
| 342 | /** |
Rose Zadik | 9ba6b62 | 2018-01-24 12:59:19 +0000 | [diff] [blame] | 343 | * \brief This function retrieves the cipher-information |
| 344 | * structure associated with the given cipher ID, |
| 345 | * key size and mode. |
Paul Bakker | f46b695 | 2013-09-09 00:08:26 +0200 | [diff] [blame] | 346 | * |
Rose Zadik | 9ba6b62 | 2018-01-24 12:59:19 +0000 | [diff] [blame] | 347 | * \param cipher_id The ID of the cipher to search for. For example, |
| 348 | * #MBEDTLS_CIPHER_ID_AES. |
| 349 | * \param key_bitlen The length of the key in bits. |
| 350 | * \param mode The cipher mode. For example, #MBEDTLS_MODE_CBC. |
Paul Bakker | f46b695 | 2013-09-09 00:08:26 +0200 | [diff] [blame] | 351 | * |
Rose Zadik | 9ba6b62 | 2018-01-24 12:59:19 +0000 | [diff] [blame] | 352 | * \return The cipher information structure associated with the |
| 353 | * given \p cipher_id, or NULL if not found. |
Paul Bakker | f46b695 | 2013-09-09 00:08:26 +0200 | [diff] [blame] | 354 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 355 | const mbedtls_cipher_info_t *mbedtls_cipher_info_from_values( const mbedtls_cipher_id_t cipher_id, |
Manuel Pégourié-Gonnard | b8186a5 | 2015-06-18 14:58:58 +0200 | [diff] [blame] | 356 | int key_bitlen, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 357 | const mbedtls_cipher_mode_t mode ); |
Paul Bakker | f46b695 | 2013-09-09 00:08:26 +0200 | [diff] [blame] | 358 | |
| 359 | /** |
Rose Zadik | 9ba6b62 | 2018-01-24 12:59:19 +0000 | [diff] [blame] | 360 | * \brief This function initializes a \p cipher_context as NONE. |
Paul Bakker | 84bbeb5 | 2014-07-01 14:53:22 +0200 | [diff] [blame] | 361 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 362 | void mbedtls_cipher_init( mbedtls_cipher_context_t *ctx ); |
Paul Bakker | 84bbeb5 | 2014-07-01 14:53:22 +0200 | [diff] [blame] | 363 | |
| 364 | /** |
Rose Zadik | 9ba6b62 | 2018-01-24 12:59:19 +0000 | [diff] [blame] | 365 | * \brief This function frees and clears the cipher-specific |
| 366 | * context of \p ctx. Freeing \p ctx itself remains the |
| 367 | * responsibility of the caller. |
Paul Bakker | 84bbeb5 | 2014-07-01 14:53:22 +0200 | [diff] [blame] | 368 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 369 | void mbedtls_cipher_free( mbedtls_cipher_context_t *ctx ); |
Paul Bakker | 84bbeb5 | 2014-07-01 14:53:22 +0200 | [diff] [blame] | 370 | |
Rose Zadik | 9ba6b62 | 2018-01-24 12:59:19 +0000 | [diff] [blame] | 371 | |
Paul Bakker | 84bbeb5 | 2014-07-01 14:53:22 +0200 | [diff] [blame] | 372 | /** |
Rose Zadik | 9ba6b62 | 2018-01-24 12:59:19 +0000 | [diff] [blame] | 373 | * \brief This function initializes and fills the cipher-context |
| 374 | * structure with the appropriate values. It also clears |
| 375 | * the structure. |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 376 | * |
Rose Zadik | 9ba6b62 | 2018-01-24 12:59:19 +0000 | [diff] [blame] | 377 | * \param ctx The context to initialize. May not be NULL. |
| 378 | * \param cipher_info The cipher to use. |
Paul Bakker | 84bbeb5 | 2014-07-01 14:53:22 +0200 | [diff] [blame] | 379 | * |
Rose Zadik | 9ba6b62 | 2018-01-24 12:59:19 +0000 | [diff] [blame] | 380 | * \return \c 0 on success, |
| 381 | * #MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA on parameter failure, |
| 382 | * #MBEDTLS_ERR_CIPHER_ALLOC_FAILED if allocation of the |
Paul Bakker | ff61a78 | 2011-06-09 15:42:02 +0000 | [diff] [blame] | 383 | * cipher-specific context failed. |
Rose Zadik | 9ba6b62 | 2018-01-24 12:59:19 +0000 | [diff] [blame] | 384 | * |
| 385 | * \internal Currently, the function also clears the structure. |
| 386 | * In future versions, the caller will be required to call |
| 387 | * mbedtls_cipher_init() on the structure first. |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 388 | */ |
Manuel Pégourié-Gonnard | 8473f87 | 2015-05-14 13:51:45 +0200 | [diff] [blame] | 389 | int mbedtls_cipher_setup( mbedtls_cipher_context_t *ctx, const mbedtls_cipher_info_t *cipher_info ); |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 390 | |
| 391 | /** |
Rose Zadik | 9ba6b62 | 2018-01-24 12:59:19 +0000 | [diff] [blame] | 392 | * \brief This function returns the block size of the given cipher. |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 393 | * |
Rose Zadik | 9ba6b62 | 2018-01-24 12:59:19 +0000 | [diff] [blame] | 394 | * \param ctx The context of the cipher. Must be initialized. |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 395 | * |
Rose Zadik | 9ba6b62 | 2018-01-24 12:59:19 +0000 | [diff] [blame] | 396 | * \return The size of the blocks of the cipher, or zero if \p ctx |
| 397 | * has not been initialized. |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 398 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 399 | static inline unsigned int mbedtls_cipher_get_block_size( const mbedtls_cipher_context_t *ctx ) |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 400 | { |
| 401 | if( NULL == ctx || NULL == ctx->cipher_info ) |
| 402 | return 0; |
| 403 | |
| 404 | return ctx->cipher_info->block_size; |
| 405 | } |
| 406 | |
| 407 | /** |
Rose Zadik | 9ba6b62 | 2018-01-24 12:59:19 +0000 | [diff] [blame] | 408 | * \brief This function returns the mode of operation for |
| 409 | * the cipher. For example, MBEDTLS_MODE_CBC. |
Paul Bakker | f7e5bb5 | 2011-11-11 10:53:37 +0000 | [diff] [blame] | 410 | * |
Rose Zadik | 9ba6b62 | 2018-01-24 12:59:19 +0000 | [diff] [blame] | 411 | * \param ctx The context of the cipher. Must be initialized. |
Paul Bakker | f7e5bb5 | 2011-11-11 10:53:37 +0000 | [diff] [blame] | 412 | * |
Rose Zadik | 9ba6b62 | 2018-01-24 12:59:19 +0000 | [diff] [blame] | 413 | * \return The mode of operation, or #MBEDTLS_MODE_NONE if |
| 414 | * \p ctx has not been initialized. |
Paul Bakker | f7e5bb5 | 2011-11-11 10:53:37 +0000 | [diff] [blame] | 415 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 416 | static inline mbedtls_cipher_mode_t mbedtls_cipher_get_cipher_mode( const mbedtls_cipher_context_t *ctx ) |
Paul Bakker | f7e5bb5 | 2011-11-11 10:53:37 +0000 | [diff] [blame] | 417 | { |
| 418 | if( NULL == ctx || NULL == ctx->cipher_info ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 419 | return MBEDTLS_MODE_NONE; |
Paul Bakker | f7e5bb5 | 2011-11-11 10:53:37 +0000 | [diff] [blame] | 420 | |
| 421 | return ctx->cipher_info->mode; |
| 422 | } |
| 423 | |
| 424 | /** |
Rose Zadik | 9ba6b62 | 2018-01-24 12:59:19 +0000 | [diff] [blame] | 425 | * \brief This function returns the size of the IV or nonce |
| 426 | * of the cipher, in Bytes. |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 427 | * |
Rose Zadik | 9ba6b62 | 2018-01-24 12:59:19 +0000 | [diff] [blame] | 428 | * \param ctx The context of the cipher. Must be initialized. |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 429 | * |
Rose Zadik | 9ba6b62 | 2018-01-24 12:59:19 +0000 | [diff] [blame] | 430 | * \return <ul><li>If no IV has been set: the recommended IV size. |
| 431 | * 0 for ciphers not using IV or nonce.</li> |
| 432 | * <li>If IV has already been set: the actual size.</li></ul> |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 433 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 434 | static inline int mbedtls_cipher_get_iv_size( const mbedtls_cipher_context_t *ctx ) |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 435 | { |
| 436 | if( NULL == ctx || NULL == ctx->cipher_info ) |
| 437 | return 0; |
| 438 | |
Manuel Pégourié-Gonnard | 9c853b9 | 2013-09-03 13:04:44 +0200 | [diff] [blame] | 439 | if( ctx->iv_size != 0 ) |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 440 | return (int) ctx->iv_size; |
Manuel Pégourié-Gonnard | 9c853b9 | 2013-09-03 13:04:44 +0200 | [diff] [blame] | 441 | |
Manuel Pégourié-Gonnard | 46c4fa1 | 2015-08-12 09:27:55 +0200 | [diff] [blame] | 442 | return (int) ctx->cipher_info->iv_size; |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 443 | } |
| 444 | |
| 445 | /** |
Rose Zadik | 9ba6b62 | 2018-01-24 12:59:19 +0000 | [diff] [blame] | 446 | * \brief This function returns the type of the given cipher. |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 447 | * |
Rose Zadik | 9ba6b62 | 2018-01-24 12:59:19 +0000 | [diff] [blame] | 448 | * \param ctx The context of the cipher. Must be initialized. |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 449 | * |
Rose Zadik | 9ba6b62 | 2018-01-24 12:59:19 +0000 | [diff] [blame] | 450 | * \return The type of the cipher, or #MBEDTLS_CIPHER_NONE if |
| 451 | * \p ctx has not been initialized. |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 452 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 453 | static inline mbedtls_cipher_type_t mbedtls_cipher_get_type( const mbedtls_cipher_context_t *ctx ) |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 454 | { |
| 455 | if( NULL == ctx || NULL == ctx->cipher_info ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 456 | return MBEDTLS_CIPHER_NONE; |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 457 | |
| 458 | return ctx->cipher_info->type; |
| 459 | } |
| 460 | |
| 461 | /** |
Rose Zadik | 9ba6b62 | 2018-01-24 12:59:19 +0000 | [diff] [blame] | 462 | * \brief This function returns the name of the given cipher |
| 463 | * as a string. |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 464 | * |
Rose Zadik | 9ba6b62 | 2018-01-24 12:59:19 +0000 | [diff] [blame] | 465 | * \param ctx The context of the cipher. Must be initialized. |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 466 | * |
Rose Zadik | 9ba6b62 | 2018-01-24 12:59:19 +0000 | [diff] [blame] | 467 | * \return The name of the cipher, or NULL if \p ctx has not |
| 468 | * been not initialized. |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 469 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 470 | static inline const char *mbedtls_cipher_get_name( const mbedtls_cipher_context_t *ctx ) |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 471 | { |
| 472 | if( NULL == ctx || NULL == ctx->cipher_info ) |
| 473 | return 0; |
| 474 | |
| 475 | return ctx->cipher_info->name; |
| 476 | } |
| 477 | |
| 478 | /** |
Rose Zadik | 9ba6b62 | 2018-01-24 12:59:19 +0000 | [diff] [blame] | 479 | * \brief This function returns the key length of the cipher. |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 480 | * |
Rose Zadik | 9ba6b62 | 2018-01-24 12:59:19 +0000 | [diff] [blame] | 481 | * \param ctx The context of the cipher. Must be initialized. |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 482 | * |
Rose Zadik | 9ba6b62 | 2018-01-24 12:59:19 +0000 | [diff] [blame] | 483 | * \return The key length of the cipher in bits, or |
| 484 | * #MBEDTLS_KEY_LENGTH_NONE if ctx \p has not been |
| 485 | * initialized. |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 486 | */ |
Manuel Pégourié-Gonnard | 097c7bb | 2015-06-18 16:43:38 +0200 | [diff] [blame] | 487 | static inline int mbedtls_cipher_get_key_bitlen( const mbedtls_cipher_context_t *ctx ) |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 488 | { |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 489 | if( NULL == ctx || NULL == ctx->cipher_info ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 490 | return MBEDTLS_KEY_LENGTH_NONE; |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 491 | |
Manuel Pégourié-Gonnard | 46c4fa1 | 2015-08-12 09:27:55 +0200 | [diff] [blame] | 492 | return (int) ctx->cipher_info->key_bitlen; |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 493 | } |
| 494 | |
| 495 | /** |
Rose Zadik | 9ba6b62 | 2018-01-24 12:59:19 +0000 | [diff] [blame] | 496 | * \brief This function returns the operation of the given cipher. |
Paul Bakker | f7e5bb5 | 2011-11-11 10:53:37 +0000 | [diff] [blame] | 497 | * |
Rose Zadik | 9ba6b62 | 2018-01-24 12:59:19 +0000 | [diff] [blame] | 498 | * \param ctx The context of the cipher. Must be initialized. |
Paul Bakker | f7e5bb5 | 2011-11-11 10:53:37 +0000 | [diff] [blame] | 499 | * |
Rose Zadik | 9ba6b62 | 2018-01-24 12:59:19 +0000 | [diff] [blame] | 500 | * \return The type of operation: #MBEDTLS_ENCRYPT or |
| 501 | * #MBEDTLS_DECRYPT, or #MBEDTLS_OPERATION_NONE if \p ctx |
| 502 | * has not been initialized. |
Paul Bakker | f7e5bb5 | 2011-11-11 10:53:37 +0000 | [diff] [blame] | 503 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 504 | static inline mbedtls_operation_t mbedtls_cipher_get_operation( const mbedtls_cipher_context_t *ctx ) |
Paul Bakker | f7e5bb5 | 2011-11-11 10:53:37 +0000 | [diff] [blame] | 505 | { |
| 506 | if( NULL == ctx || NULL == ctx->cipher_info ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 507 | return MBEDTLS_OPERATION_NONE; |
Paul Bakker | f7e5bb5 | 2011-11-11 10:53:37 +0000 | [diff] [blame] | 508 | |
| 509 | return ctx->operation; |
| 510 | } |
| 511 | |
| 512 | /** |
Rose Zadik | 9ba6b62 | 2018-01-24 12:59:19 +0000 | [diff] [blame] | 513 | * \brief This function sets the key to use with the given context. |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 514 | * |
Rose Zadik | 9ba6b62 | 2018-01-24 12:59:19 +0000 | [diff] [blame] | 515 | * \param ctx The generic cipher context. May not be NULL. Must have |
| 516 | * been initialized using mbedtls_cipher_info_from_type() |
| 517 | * or mbedtls_cipher_info_from_string(). |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 518 | * \param key The key to use. |
Rose Zadik | 9ba6b62 | 2018-01-24 12:59:19 +0000 | [diff] [blame] | 519 | * \param key_bitlen The key length to use, in bits. |
| 520 | * \param operation The operation that the key will be used for: |
| 521 | * #MBEDTLS_ENCRYPT or #MBEDTLS_DECRYPT. |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 522 | * |
Rose Zadik | 9ba6b62 | 2018-01-24 12:59:19 +0000 | [diff] [blame] | 523 | * \returns \c 0 on success, #MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA if |
| 524 | * parameter verification fails, or a cipher-specific |
Paul Bakker | ff61a78 | 2011-06-09 15:42:02 +0000 | [diff] [blame] | 525 | * error code. |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 526 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 527 | int mbedtls_cipher_setkey( mbedtls_cipher_context_t *ctx, const unsigned char *key, |
Manuel Pégourié-Gonnard | b8186a5 | 2015-06-18 14:58:58 +0200 | [diff] [blame] | 528 | int key_bitlen, const mbedtls_operation_t operation ); |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 529 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 530 | #if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING) |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 531 | /** |
Rose Zadik | 9ba6b62 | 2018-01-24 12:59:19 +0000 | [diff] [blame] | 532 | * \brief This function sets the padding mode, for cipher modes |
| 533 | * that use padding. |
Manuel Pégourié-Gonnard | d5fdcaf | 2013-07-24 18:05:00 +0200 | [diff] [blame] | 534 | * |
Rose Zadik | 9ba6b62 | 2018-01-24 12:59:19 +0000 | [diff] [blame] | 535 | * The default passing mode is PKCS7 padding. |
Manuel Pégourié-Gonnard | d5fdcaf | 2013-07-24 18:05:00 +0200 | [diff] [blame] | 536 | * |
Rose Zadik | 9ba6b62 | 2018-01-24 12:59:19 +0000 | [diff] [blame] | 537 | * \param ctx The generic cipher context. |
| 538 | * \param mode The padding mode. |
| 539 | * |
| 540 | * \returns \c 0 on success, #MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE |
| 541 | * if the selected padding mode is not supported, or |
| 542 | * #MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA if the cipher mode |
Paul Bakker | 1a45d91 | 2013-08-14 12:04:26 +0200 | [diff] [blame] | 543 | * does not support padding. |
Manuel Pégourié-Gonnard | d5fdcaf | 2013-07-24 18:05:00 +0200 | [diff] [blame] | 544 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 545 | int mbedtls_cipher_set_padding_mode( mbedtls_cipher_context_t *ctx, mbedtls_cipher_padding_t mode ); |
| 546 | #endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */ |
Manuel Pégourié-Gonnard | d5fdcaf | 2013-07-24 18:05:00 +0200 | [diff] [blame] | 547 | |
| 548 | /** |
Rose Zadik | 9ba6b62 | 2018-01-24 12:59:19 +0000 | [diff] [blame] | 549 | * \brief This function sets the initialization vector (IV) |
| 550 | * or nonce. |
Manuel Pégourié-Gonnard | 9c853b9 | 2013-09-03 13:04:44 +0200 | [diff] [blame] | 551 | * |
Rose Zadik | 9ba6b62 | 2018-01-24 12:59:19 +0000 | [diff] [blame] | 552 | * \param ctx The generic cipher context. |
| 553 | * \param iv The IV to use, or NONCE_COUNTER for CTR-mode ciphers. |
| 554 | * \param iv_len The IV length for ciphers with variable-size IV. |
| 555 | * This parameter is discarded by ciphers with fixed-size IV. |
Manuel Pégourié-Gonnard | 9c853b9 | 2013-09-03 13:04:44 +0200 | [diff] [blame] | 556 | * |
Rose Zadik | 9ba6b62 | 2018-01-24 12:59:19 +0000 | [diff] [blame] | 557 | * \returns \c 0 on success, or #MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA |
Manuel Pégourié-Gonnard | 9c853b9 | 2013-09-03 13:04:44 +0200 | [diff] [blame] | 558 | * |
Rose Zadik | 9ba6b62 | 2018-01-24 12:59:19 +0000 | [diff] [blame] | 559 | * \note Some ciphers do not use IVs nor nonce. For these |
| 560 | * ciphers, this function has no effect. |
Manuel Pégourié-Gonnard | 9c853b9 | 2013-09-03 13:04:44 +0200 | [diff] [blame] | 561 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 562 | int mbedtls_cipher_set_iv( mbedtls_cipher_context_t *ctx, |
Manuel Pégourié-Gonnard | 9c853b9 | 2013-09-03 13:04:44 +0200 | [diff] [blame] | 563 | const unsigned char *iv, size_t iv_len ); |
| 564 | |
| 565 | /** |
Rose Zadik | 9ba6b62 | 2018-01-24 12:59:19 +0000 | [diff] [blame] | 566 | * \brief This function resets the cipher state. |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 567 | * |
Rose Zadik | 9ba6b62 | 2018-01-24 12:59:19 +0000 | [diff] [blame] | 568 | * \param ctx The generic cipher context. |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 569 | * |
Rose Zadik | 9ba6b62 | 2018-01-24 12:59:19 +0000 | [diff] [blame] | 570 | * \returns \c 0 on success, #MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA |
| 571 | * if parameter verification fails. |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 572 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 573 | int mbedtls_cipher_reset( mbedtls_cipher_context_t *ctx ); |
Manuel Pégourié-Gonnard | 2adc40c | 2013-09-03 13:54:12 +0200 | [diff] [blame] | 574 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 575 | #if defined(MBEDTLS_GCM_C) |
Manuel Pégourié-Gonnard | 2adc40c | 2013-09-03 13:54:12 +0200 | [diff] [blame] | 576 | /** |
Rose Zadik | 9ba6b62 | 2018-01-24 12:59:19 +0000 | [diff] [blame] | 577 | * \brief This function adds additional data for AEAD ciphers. |
| 578 | * Only supported with GCM. Must be called |
| 579 | * exactly once, after mbedtls_cipher_reset(). |
Manuel Pégourié-Gonnard | 2adc40c | 2013-09-03 13:54:12 +0200 | [diff] [blame] | 580 | * |
Rose Zadik | 9ba6b62 | 2018-01-24 12:59:19 +0000 | [diff] [blame] | 581 | * \param ctx The generic cipher context. |
| 582 | * \param ad The additional data to use. |
| 583 | * \param ad_len the Length of \p ad. |
Manuel Pégourié-Gonnard | 2adc40c | 2013-09-03 13:54:12 +0200 | [diff] [blame] | 584 | * |
Rose Zadik | 9ba6b62 | 2018-01-24 12:59:19 +0000 | [diff] [blame] | 585 | * \return \c 0 on success, or a specific error code on failure. |
Manuel Pégourié-Gonnard | 2adc40c | 2013-09-03 13:54:12 +0200 | [diff] [blame] | 586 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 587 | int mbedtls_cipher_update_ad( mbedtls_cipher_context_t *ctx, |
Manuel Pégourié-Gonnard | 2adc40c | 2013-09-03 13:54:12 +0200 | [diff] [blame] | 588 | const unsigned char *ad, size_t ad_len ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 589 | #endif /* MBEDTLS_GCM_C */ |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 590 | |
| 591 | /** |
Rose Zadik | 9ba6b62 | 2018-01-24 12:59:19 +0000 | [diff] [blame] | 592 | * \brief The generic cipher update function. It encrypts or |
| 593 | * decrypts using the given cipher context. Writes as |
| 594 | * many block-sized blocks of data as possible to output. |
| 595 | * Any data that cannot be written immediately is either |
| 596 | * added to the next block, or flushed when |
| 597 | * mbedtls_cipher_finish() is called. |
| 598 | * Exception: For MBEDTLS_MODE_ECB, expects a single block |
| 599 | * in size. For example, 16 Bytes for AES. |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 600 | * |
Rose Zadik | 9ba6b62 | 2018-01-24 12:59:19 +0000 | [diff] [blame] | 601 | * \param ctx The generic cipher context. |
| 602 | * \param input The buffer holding the input data. |
| 603 | * \param ilen The length of the input data. |
| 604 | * \param output The buffer for the output data. Must be able to hold at |
| 605 | * least \p ilen + block_size. Must not be the same buffer |
| 606 | * as input. |
| 607 | * \param olen The length of the output data, to be updated with the |
| 608 | * actual number of Bytes written. |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 609 | * |
Rose Zadik | 9ba6b62 | 2018-01-24 12:59:19 +0000 | [diff] [blame] | 610 | * \returns \c 0 on success, #MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA if |
Paul Bakker | ff61a78 | 2011-06-09 15:42:02 +0000 | [diff] [blame] | 611 | * parameter verification fails, |
Rose Zadik | 9ba6b62 | 2018-01-24 12:59:19 +0000 | [diff] [blame] | 612 | * #MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE on an |
| 613 | * unsupported mode for a cipher, or a cipher-specific |
Paul Bakker | ff61a78 | 2011-06-09 15:42:02 +0000 | [diff] [blame] | 614 | * error code. |
Manuel Pégourié-Gonnard | b8bd593 | 2013-09-05 13:38:15 +0200 | [diff] [blame] | 615 | * |
| 616 | * \note If the underlying cipher is GCM, all calls to this |
Rose Zadik | 9ba6b62 | 2018-01-24 12:59:19 +0000 | [diff] [blame] | 617 | * function, except the last one before |
| 618 | * mbedtls_cipher_finish(). Must have \p ilen as a |
| 619 | * multiple of the block_size. |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 620 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 621 | int mbedtls_cipher_update( mbedtls_cipher_context_t *ctx, const unsigned char *input, |
Paul Bakker | b9e4e2c | 2014-05-01 14:18:25 +0200 | [diff] [blame] | 622 | size_t ilen, unsigned char *output, size_t *olen ); |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 623 | |
| 624 | /** |
Rose Zadik | 9ba6b62 | 2018-01-24 12:59:19 +0000 | [diff] [blame] | 625 | * \brief The generic cipher finalization function. If data still |
| 626 | * needs to be flushed from an incomplete block, the data |
| 627 | * contained in it is padded to the size of |
| 628 | * the last block, and written to the \p output buffer. |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 629 | * |
Rose Zadik | 9ba6b62 | 2018-01-24 12:59:19 +0000 | [diff] [blame] | 630 | * \param ctx The generic cipher context. |
| 631 | * \param output The buffer to write data to. Needs block_size available. |
| 632 | * \param olen The length of the data written to the \p output buffer. |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 633 | * |
Rose Zadik | 9ba6b62 | 2018-01-24 12:59:19 +0000 | [diff] [blame] | 634 | * \returns \c 0 on success, #MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA if |
Paul Bakker | ff61a78 | 2011-06-09 15:42:02 +0000 | [diff] [blame] | 635 | * parameter verification fails, |
Rose Zadik | 9ba6b62 | 2018-01-24 12:59:19 +0000 | [diff] [blame] | 636 | * #MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED if decryption |
Paul Bakker | ff61a78 | 2011-06-09 15:42:02 +0000 | [diff] [blame] | 637 | * expected a full block but was not provided one, |
Rose Zadik | 9ba6b62 | 2018-01-24 12:59:19 +0000 | [diff] [blame] | 638 | * #MBEDTLS_ERR_CIPHER_INVALID_PADDING on invalid padding |
| 639 | * while decrypting, or a cipher-specific error code |
| 640 | * on failure for any other reason. |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 641 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 642 | int mbedtls_cipher_finish( mbedtls_cipher_context_t *ctx, |
Manuel Pégourié-Gonnard | aa9ffc5 | 2013-09-03 16:19:22 +0200 | [diff] [blame] | 643 | unsigned char *output, size_t *olen ); |
| 644 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 645 | #if defined(MBEDTLS_GCM_C) |
Manuel Pégourié-Gonnard | aa9ffc5 | 2013-09-03 16:19:22 +0200 | [diff] [blame] | 646 | /** |
Rose Zadik | 9ba6b62 | 2018-01-24 12:59:19 +0000 | [diff] [blame] | 647 | * \brief This function writes a tag for AEAD ciphers. |
| 648 | * Only supported with GCM. |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 649 | * Must be called after mbedtls_cipher_finish(). |
Manuel Pégourié-Gonnard | aa9ffc5 | 2013-09-03 16:19:22 +0200 | [diff] [blame] | 650 | * |
Rose Zadik | 9ba6b62 | 2018-01-24 12:59:19 +0000 | [diff] [blame] | 651 | * \param ctx The generic cipher context. |
| 652 | * \param tag The buffer to write the tag to. |
| 653 | * \param tag_len The length of the tag to write. |
Manuel Pégourié-Gonnard | aa9ffc5 | 2013-09-03 16:19:22 +0200 | [diff] [blame] | 654 | * |
Rose Zadik | 9ba6b62 | 2018-01-24 12:59:19 +0000 | [diff] [blame] | 655 | * \return \c 0 on success, or a specific error code on failure. |
Manuel Pégourié-Gonnard | aa9ffc5 | 2013-09-03 16:19:22 +0200 | [diff] [blame] | 656 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 657 | int mbedtls_cipher_write_tag( mbedtls_cipher_context_t *ctx, |
Manuel Pégourié-Gonnard | aa9ffc5 | 2013-09-03 16:19:22 +0200 | [diff] [blame] | 658 | unsigned char *tag, size_t tag_len ); |
| 659 | |
| 660 | /** |
Rose Zadik | 9ba6b62 | 2018-01-24 12:59:19 +0000 | [diff] [blame] | 661 | * \brief This function checks the tag for AEAD ciphers. |
| 662 | * Only supported with GCM. |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 663 | * Must be called after mbedtls_cipher_finish(). |
Manuel Pégourié-Gonnard | aa9ffc5 | 2013-09-03 16:19:22 +0200 | [diff] [blame] | 664 | * |
Rose Zadik | 9ba6b62 | 2018-01-24 12:59:19 +0000 | [diff] [blame] | 665 | * \param ctx The generic cipher context. |
| 666 | * \param tag The buffer holding the tag. |
| 667 | * \param tag_len The length of the tag to check. |
Manuel Pégourié-Gonnard | aa9ffc5 | 2013-09-03 16:19:22 +0200 | [diff] [blame] | 668 | * |
Rose Zadik | 9ba6b62 | 2018-01-24 12:59:19 +0000 | [diff] [blame] | 669 | * \return \c 0 on success, or a specific error code on failure. |
Manuel Pégourié-Gonnard | aa9ffc5 | 2013-09-03 16:19:22 +0200 | [diff] [blame] | 670 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 671 | int mbedtls_cipher_check_tag( mbedtls_cipher_context_t *ctx, |
Manuel Pégourié-Gonnard | aa9ffc5 | 2013-09-03 16:19:22 +0200 | [diff] [blame] | 672 | const unsigned char *tag, size_t tag_len ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 673 | #endif /* MBEDTLS_GCM_C */ |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 674 | |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 675 | /** |
Rose Zadik | 9ba6b62 | 2018-01-24 12:59:19 +0000 | [diff] [blame] | 676 | * \brief The generic all-in-one encryption/decryption function, |
| 677 | * for all ciphers except AEAD constructs. |
Manuel Pégourié-Gonnard | 3c1d150 | 2014-05-12 13:46:08 +0200 | [diff] [blame] | 678 | * |
Rose Zadik | 9ba6b62 | 2018-01-24 12:59:19 +0000 | [diff] [blame] | 679 | * \param ctx The generic cipher context. |
| 680 | * \param iv The IV to use, or NONCE_COUNTER for CTR-mode ciphers. |
| 681 | * \param iv_len The IV length for ciphers with variable-size IV. |
| 682 | * This parameter is discarded by ciphers with fixed-size |
| 683 | * IV. |
| 684 | * \param input The buffer holding the input data. |
| 685 | * \param ilen The length of the input data. |
| 686 | * \param output The buffer for the output data. Must be able to hold at |
| 687 | * least \p ilen + block_size. Must not be the same buffer |
| 688 | * as input. |
| 689 | * \param olen The length of the output data, to be updated with the |
| 690 | * actual number of Bytes written. |
Manuel Pégourié-Gonnard | 3c1d150 | 2014-05-12 13:46:08 +0200 | [diff] [blame] | 691 | * |
Rose Zadik | 9ba6b62 | 2018-01-24 12:59:19 +0000 | [diff] [blame] | 692 | * \note Some ciphers do not use IVs nor nonce. For these |
| 693 | * ciphers, use \p iv = NULL and \p iv_len = 0. |
Manuel Pégourié-Gonnard | 3c1d150 | 2014-05-12 13:46:08 +0200 | [diff] [blame] | 694 | * |
Rose Zadik | 9ba6b62 | 2018-01-24 12:59:19 +0000 | [diff] [blame] | 695 | * \returns \c 0 on success, or |
| 696 | * #MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA, or |
| 697 | * #MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED if decryption |
Manuel Pégourié-Gonnard | 3c1d150 | 2014-05-12 13:46:08 +0200 | [diff] [blame] | 698 | * expected a full block but was not provided one, or |
Rose Zadik | 9ba6b62 | 2018-01-24 12:59:19 +0000 | [diff] [blame] | 699 | * #MBEDTLS_ERR_CIPHER_INVALID_PADDING on invalid padding |
| 700 | * while decrypting, or a cipher-specific error code on |
| 701 | * failure for any other reason. |
Manuel Pégourié-Gonnard | 3c1d150 | 2014-05-12 13:46:08 +0200 | [diff] [blame] | 702 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 703 | int mbedtls_cipher_crypt( mbedtls_cipher_context_t *ctx, |
Manuel Pégourié-Gonnard | 3c1d150 | 2014-05-12 13:46:08 +0200 | [diff] [blame] | 704 | const unsigned char *iv, size_t iv_len, |
| 705 | const unsigned char *input, size_t ilen, |
| 706 | unsigned char *output, size_t *olen ); |
| 707 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 708 | #if defined(MBEDTLS_CIPHER_MODE_AEAD) |
Manuel Pégourié-Gonnard | 4562ffe | 2014-05-13 12:19:29 +0200 | [diff] [blame] | 709 | /** |
Rose Zadik | 9ba6b62 | 2018-01-24 12:59:19 +0000 | [diff] [blame] | 710 | * \brief The generic autenticated encryption (AEAD) function. |
Manuel Pégourié-Gonnard | 4562ffe | 2014-05-13 12:19:29 +0200 | [diff] [blame] | 711 | * |
Rose Zadik | 9ba6b62 | 2018-01-24 12:59:19 +0000 | [diff] [blame] | 712 | * \param ctx The generic cipher context. |
| 713 | * \param iv The IV to use, or NONCE_COUNTER for CTR-mode ciphers. |
| 714 | * \param iv_len The IV length for ciphers with variable-size IV. |
| 715 | * This parameter is discarded by ciphers with fixed-size IV. |
| 716 | * \param ad The additional data to authenticate. |
| 717 | * \param ad_len The length of \p ad. |
| 718 | * \param input The buffer holding the input data. |
| 719 | * \param ilen The length of the input data. |
| 720 | * \param output The buffer for the output data. |
| 721 | * Must be able to hold at least \p ilen. |
| 722 | * \param olen The length of the output data, to be updated with the |
| 723 | * actual number of Bytes written. |
| 724 | * \param tag The buffer for the authentication tag. |
| 725 | * \param tag_len The desired length of the authentication tag. |
Manuel Pégourié-Gonnard | 4562ffe | 2014-05-13 12:19:29 +0200 | [diff] [blame] | 726 | * |
Rose Zadik | 9ba6b62 | 2018-01-24 12:59:19 +0000 | [diff] [blame] | 727 | * \returns \c 0 on success, or |
| 728 | * #MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA, or |
| 729 | * a cipher-specific error code. |
Manuel Pégourié-Gonnard | 4562ffe | 2014-05-13 12:19:29 +0200 | [diff] [blame] | 730 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 731 | int mbedtls_cipher_auth_encrypt( mbedtls_cipher_context_t *ctx, |
Manuel Pégourié-Gonnard | 4562ffe | 2014-05-13 12:19:29 +0200 | [diff] [blame] | 732 | const unsigned char *iv, size_t iv_len, |
| 733 | const unsigned char *ad, size_t ad_len, |
| 734 | const unsigned char *input, size_t ilen, |
| 735 | unsigned char *output, size_t *olen, |
| 736 | unsigned char *tag, size_t tag_len ); |
| 737 | |
| 738 | /** |
Rose Zadik | 9ba6b62 | 2018-01-24 12:59:19 +0000 | [diff] [blame] | 739 | * \brief The generic autenticated decryption (AEAD) function. |
Manuel Pégourié-Gonnard | 4562ffe | 2014-05-13 12:19:29 +0200 | [diff] [blame] | 740 | * |
Rose Zadik | 9ba6b62 | 2018-01-24 12:59:19 +0000 | [diff] [blame] | 741 | * \param ctx The generic cipher context. |
| 742 | * \param iv The IV to use, or NONCE_COUNTER for CTR-mode ciphers. |
| 743 | * \param iv_len The IV length for ciphers with variable-size IV. |
| 744 | * This parameter is discarded by ciphers with fixed-size IV. |
| 745 | * \param ad The additional data to be authenticated. |
| 746 | * \param ad_len The length of \p ad. |
| 747 | * \param input The buffer holding the input data. |
| 748 | * \param ilen The length of the input data. |
| 749 | * \param output The buffer for the output data. |
| 750 | * Must be able to hold at least \p ilen. |
| 751 | * \param olen The length of the output data, to be updated with the |
| 752 | * actual number of Bytes written. |
| 753 | * \param tag The buffer holding the authentication tag. |
| 754 | * \param tag_len The length of the authentication tag. |
Manuel Pégourié-Gonnard | 4562ffe | 2014-05-13 12:19:29 +0200 | [diff] [blame] | 755 | * |
Rose Zadik | 9ba6b62 | 2018-01-24 12:59:19 +0000 | [diff] [blame] | 756 | * \returns \c 0 on success, or |
| 757 | * #MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA, or |
| 758 | * #MBEDTLS_ERR_CIPHER_AUTH_FAILED if data is not authentic, |
| 759 | * or a cipher-specific error code on failure for any other reason. |
Manuel Pégourié-Gonnard | 4562ffe | 2014-05-13 12:19:29 +0200 | [diff] [blame] | 760 | * |
| 761 | * \note If the data is not authentic, then the output buffer |
Rose Zadik | 9ba6b62 | 2018-01-24 12:59:19 +0000 | [diff] [blame] | 762 | * is zeroed out to prevent the unauthentic plaintext being |
| 763 | * used, making this interface safer. |
Manuel Pégourié-Gonnard | 4562ffe | 2014-05-13 12:19:29 +0200 | [diff] [blame] | 764 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 765 | int mbedtls_cipher_auth_decrypt( mbedtls_cipher_context_t *ctx, |
Manuel Pégourié-Gonnard | 4562ffe | 2014-05-13 12:19:29 +0200 | [diff] [blame] | 766 | const unsigned char *iv, size_t iv_len, |
| 767 | const unsigned char *ad, size_t ad_len, |
| 768 | const unsigned char *input, size_t ilen, |
| 769 | unsigned char *output, size_t *olen, |
| 770 | const unsigned char *tag, size_t tag_len ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 771 | #endif /* MBEDTLS_CIPHER_MODE_AEAD */ |
Manuel Pégourié-Gonnard | 4562ffe | 2014-05-13 12:19:29 +0200 | [diff] [blame] | 772 | |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 773 | #ifdef __cplusplus |
| 774 | } |
| 775 | #endif |
| 776 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 777 | #endif /* MBEDTLS_CIPHER_H */ |