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