mbed TLS Build
include/mbedtls/cipher.h@0:cdf462088d13, 2017-01-05 (annotated)
- Committer:
- markrad
- Date:
- Thu Jan 05 00:18:44 2017 +0000
- Revision:
- 0:cdf462088d13
Initial commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
markrad | 0:cdf462088d13 | 1 | /** |
markrad | 0:cdf462088d13 | 2 | * \file cipher.h |
markrad | 0:cdf462088d13 | 3 | * |
markrad | 0:cdf462088d13 | 4 | * \brief Generic cipher wrapper. |
markrad | 0:cdf462088d13 | 5 | * |
markrad | 0:cdf462088d13 | 6 | * \author Adriaan de Jong <dejong@fox-it.com> |
markrad | 0:cdf462088d13 | 7 | * |
markrad | 0:cdf462088d13 | 8 | * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved |
markrad | 0:cdf462088d13 | 9 | * SPDX-License-Identifier: Apache-2.0 |
markrad | 0:cdf462088d13 | 10 | * |
markrad | 0:cdf462088d13 | 11 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
markrad | 0:cdf462088d13 | 12 | * not use this file except in compliance with the License. |
markrad | 0:cdf462088d13 | 13 | * You may obtain a copy of the License at |
markrad | 0:cdf462088d13 | 14 | * |
markrad | 0:cdf462088d13 | 15 | * http://www.apache.org/licenses/LICENSE-2.0 |
markrad | 0:cdf462088d13 | 16 | * |
markrad | 0:cdf462088d13 | 17 | * Unless required by applicable law or agreed to in writing, software |
markrad | 0:cdf462088d13 | 18 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
markrad | 0:cdf462088d13 | 19 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
markrad | 0:cdf462088d13 | 20 | * See the License for the specific language governing permissions and |
markrad | 0:cdf462088d13 | 21 | * limitations under the License. |
markrad | 0:cdf462088d13 | 22 | * |
markrad | 0:cdf462088d13 | 23 | * This file is part of mbed TLS (https://tls.mbed.org) |
markrad | 0:cdf462088d13 | 24 | */ |
markrad | 0:cdf462088d13 | 25 | |
markrad | 0:cdf462088d13 | 26 | #ifndef MBEDTLS_CIPHER_H |
markrad | 0:cdf462088d13 | 27 | #define MBEDTLS_CIPHER_H |
markrad | 0:cdf462088d13 | 28 | |
markrad | 0:cdf462088d13 | 29 | #if !defined(MBEDTLS_CONFIG_FILE) |
markrad | 0:cdf462088d13 | 30 | #include "config.h" |
markrad | 0:cdf462088d13 | 31 | #else |
markrad | 0:cdf462088d13 | 32 | #include MBEDTLS_CONFIG_FILE |
markrad | 0:cdf462088d13 | 33 | #endif |
markrad | 0:cdf462088d13 | 34 | |
markrad | 0:cdf462088d13 | 35 | #include <stddef.h> |
markrad | 0:cdf462088d13 | 36 | |
markrad | 0:cdf462088d13 | 37 | #if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CCM_C) |
markrad | 0:cdf462088d13 | 38 | #define MBEDTLS_CIPHER_MODE_AEAD |
markrad | 0:cdf462088d13 | 39 | #endif |
markrad | 0:cdf462088d13 | 40 | |
markrad | 0:cdf462088d13 | 41 | #if defined(MBEDTLS_CIPHER_MODE_CBC) |
markrad | 0:cdf462088d13 | 42 | #define MBEDTLS_CIPHER_MODE_WITH_PADDING |
markrad | 0:cdf462088d13 | 43 | #endif |
markrad | 0:cdf462088d13 | 44 | |
markrad | 0:cdf462088d13 | 45 | #if defined(MBEDTLS_ARC4_C) |
markrad | 0:cdf462088d13 | 46 | #define MBEDTLS_CIPHER_MODE_STREAM |
markrad | 0:cdf462088d13 | 47 | #endif |
markrad | 0:cdf462088d13 | 48 | |
markrad | 0:cdf462088d13 | 49 | #if ( defined(__ARMCC_VERSION) || defined(_MSC_VER) ) && \ |
markrad | 0:cdf462088d13 | 50 | !defined(inline) && !defined(__cplusplus) |
markrad | 0:cdf462088d13 | 51 | #define inline __inline |
markrad | 0:cdf462088d13 | 52 | #endif |
markrad | 0:cdf462088d13 | 53 | |
markrad | 0:cdf462088d13 | 54 | #define MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE -0x6080 /**< The selected feature is not available. */ |
markrad | 0:cdf462088d13 | 55 | #define MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA -0x6100 /**< Bad input parameters to function. */ |
markrad | 0:cdf462088d13 | 56 | #define MBEDTLS_ERR_CIPHER_ALLOC_FAILED -0x6180 /**< Failed to allocate memory. */ |
markrad | 0:cdf462088d13 | 57 | #define MBEDTLS_ERR_CIPHER_INVALID_PADDING -0x6200 /**< Input data contains invalid padding and is rejected. */ |
markrad | 0:cdf462088d13 | 58 | #define MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED -0x6280 /**< Decryption of block requires a full block. */ |
markrad | 0:cdf462088d13 | 59 | #define MBEDTLS_ERR_CIPHER_AUTH_FAILED -0x6300 /**< Authentication failed (for AEAD modes). */ |
markrad | 0:cdf462088d13 | 60 | #define MBEDTLS_ERR_CIPHER_INVALID_CONTEXT -0x6380 /**< The context is invalid, eg because it was free()ed. */ |
markrad | 0:cdf462088d13 | 61 | |
markrad | 0:cdf462088d13 | 62 | #define MBEDTLS_CIPHER_VARIABLE_IV_LEN 0x01 /**< Cipher accepts IVs of variable length */ |
markrad | 0:cdf462088d13 | 63 | #define MBEDTLS_CIPHER_VARIABLE_KEY_LEN 0x02 /**< Cipher accepts keys of variable length */ |
markrad | 0:cdf462088d13 | 64 | |
markrad | 0:cdf462088d13 | 65 | #ifdef __cplusplus |
markrad | 0:cdf462088d13 | 66 | extern "C" { |
markrad | 0:cdf462088d13 | 67 | #endif |
markrad | 0:cdf462088d13 | 68 | |
markrad | 0:cdf462088d13 | 69 | typedef enum { |
markrad | 0:cdf462088d13 | 70 | MBEDTLS_CIPHER_ID_NONE = 0, |
markrad | 0:cdf462088d13 | 71 | MBEDTLS_CIPHER_ID_NULL, |
markrad | 0:cdf462088d13 | 72 | MBEDTLS_CIPHER_ID_AES, |
markrad | 0:cdf462088d13 | 73 | MBEDTLS_CIPHER_ID_DES, |
markrad | 0:cdf462088d13 | 74 | MBEDTLS_CIPHER_ID_3DES, |
markrad | 0:cdf462088d13 | 75 | MBEDTLS_CIPHER_ID_CAMELLIA, |
markrad | 0:cdf462088d13 | 76 | MBEDTLS_CIPHER_ID_BLOWFISH, |
markrad | 0:cdf462088d13 | 77 | MBEDTLS_CIPHER_ID_ARC4, |
markrad | 0:cdf462088d13 | 78 | } mbedtls_cipher_id_t; |
markrad | 0:cdf462088d13 | 79 | |
markrad | 0:cdf462088d13 | 80 | typedef enum { |
markrad | 0:cdf462088d13 | 81 | MBEDTLS_CIPHER_NONE = 0, |
markrad | 0:cdf462088d13 | 82 | MBEDTLS_CIPHER_NULL, |
markrad | 0:cdf462088d13 | 83 | MBEDTLS_CIPHER_AES_128_ECB, |
markrad | 0:cdf462088d13 | 84 | MBEDTLS_CIPHER_AES_192_ECB, |
markrad | 0:cdf462088d13 | 85 | MBEDTLS_CIPHER_AES_256_ECB, |
markrad | 0:cdf462088d13 | 86 | MBEDTLS_CIPHER_AES_128_CBC, |
markrad | 0:cdf462088d13 | 87 | MBEDTLS_CIPHER_AES_192_CBC, |
markrad | 0:cdf462088d13 | 88 | MBEDTLS_CIPHER_AES_256_CBC, |
markrad | 0:cdf462088d13 | 89 | MBEDTLS_CIPHER_AES_128_CFB128, |
markrad | 0:cdf462088d13 | 90 | MBEDTLS_CIPHER_AES_192_CFB128, |
markrad | 0:cdf462088d13 | 91 | MBEDTLS_CIPHER_AES_256_CFB128, |
markrad | 0:cdf462088d13 | 92 | MBEDTLS_CIPHER_AES_128_CTR, |
markrad | 0:cdf462088d13 | 93 | MBEDTLS_CIPHER_AES_192_CTR, |
markrad | 0:cdf462088d13 | 94 | MBEDTLS_CIPHER_AES_256_CTR, |
markrad | 0:cdf462088d13 | 95 | MBEDTLS_CIPHER_AES_128_GCM, |
markrad | 0:cdf462088d13 | 96 | MBEDTLS_CIPHER_AES_192_GCM, |
markrad | 0:cdf462088d13 | 97 | MBEDTLS_CIPHER_AES_256_GCM, |
markrad | 0:cdf462088d13 | 98 | MBEDTLS_CIPHER_CAMELLIA_128_ECB, |
markrad | 0:cdf462088d13 | 99 | MBEDTLS_CIPHER_CAMELLIA_192_ECB, |
markrad | 0:cdf462088d13 | 100 | MBEDTLS_CIPHER_CAMELLIA_256_ECB, |
markrad | 0:cdf462088d13 | 101 | MBEDTLS_CIPHER_CAMELLIA_128_CBC, |
markrad | 0:cdf462088d13 | 102 | MBEDTLS_CIPHER_CAMELLIA_192_CBC, |
markrad | 0:cdf462088d13 | 103 | MBEDTLS_CIPHER_CAMELLIA_256_CBC, |
markrad | 0:cdf462088d13 | 104 | MBEDTLS_CIPHER_CAMELLIA_128_CFB128, |
markrad | 0:cdf462088d13 | 105 | MBEDTLS_CIPHER_CAMELLIA_192_CFB128, |
markrad | 0:cdf462088d13 | 106 | MBEDTLS_CIPHER_CAMELLIA_256_CFB128, |
markrad | 0:cdf462088d13 | 107 | MBEDTLS_CIPHER_CAMELLIA_128_CTR, |
markrad | 0:cdf462088d13 | 108 | MBEDTLS_CIPHER_CAMELLIA_192_CTR, |
markrad | 0:cdf462088d13 | 109 | MBEDTLS_CIPHER_CAMELLIA_256_CTR, |
markrad | 0:cdf462088d13 | 110 | MBEDTLS_CIPHER_CAMELLIA_128_GCM, |
markrad | 0:cdf462088d13 | 111 | MBEDTLS_CIPHER_CAMELLIA_192_GCM, |
markrad | 0:cdf462088d13 | 112 | MBEDTLS_CIPHER_CAMELLIA_256_GCM, |
markrad | 0:cdf462088d13 | 113 | MBEDTLS_CIPHER_DES_ECB, |
markrad | 0:cdf462088d13 | 114 | MBEDTLS_CIPHER_DES_CBC, |
markrad | 0:cdf462088d13 | 115 | MBEDTLS_CIPHER_DES_EDE_ECB, |
markrad | 0:cdf462088d13 | 116 | MBEDTLS_CIPHER_DES_EDE_CBC, |
markrad | 0:cdf462088d13 | 117 | MBEDTLS_CIPHER_DES_EDE3_ECB, |
markrad | 0:cdf462088d13 | 118 | MBEDTLS_CIPHER_DES_EDE3_CBC, |
markrad | 0:cdf462088d13 | 119 | MBEDTLS_CIPHER_BLOWFISH_ECB, |
markrad | 0:cdf462088d13 | 120 | MBEDTLS_CIPHER_BLOWFISH_CBC, |
markrad | 0:cdf462088d13 | 121 | MBEDTLS_CIPHER_BLOWFISH_CFB64, |
markrad | 0:cdf462088d13 | 122 | MBEDTLS_CIPHER_BLOWFISH_CTR, |
markrad | 0:cdf462088d13 | 123 | MBEDTLS_CIPHER_ARC4_128, |
markrad | 0:cdf462088d13 | 124 | MBEDTLS_CIPHER_AES_128_CCM, |
markrad | 0:cdf462088d13 | 125 | MBEDTLS_CIPHER_AES_192_CCM, |
markrad | 0:cdf462088d13 | 126 | MBEDTLS_CIPHER_AES_256_CCM, |
markrad | 0:cdf462088d13 | 127 | MBEDTLS_CIPHER_CAMELLIA_128_CCM, |
markrad | 0:cdf462088d13 | 128 | MBEDTLS_CIPHER_CAMELLIA_192_CCM, |
markrad | 0:cdf462088d13 | 129 | MBEDTLS_CIPHER_CAMELLIA_256_CCM, |
markrad | 0:cdf462088d13 | 130 | } mbedtls_cipher_type_t; |
markrad | 0:cdf462088d13 | 131 | |
markrad | 0:cdf462088d13 | 132 | typedef enum { |
markrad | 0:cdf462088d13 | 133 | MBEDTLS_MODE_NONE = 0, |
markrad | 0:cdf462088d13 | 134 | MBEDTLS_MODE_ECB, |
markrad | 0:cdf462088d13 | 135 | MBEDTLS_MODE_CBC, |
markrad | 0:cdf462088d13 | 136 | MBEDTLS_MODE_CFB, |
markrad | 0:cdf462088d13 | 137 | MBEDTLS_MODE_OFB, /* Unused! */ |
markrad | 0:cdf462088d13 | 138 | MBEDTLS_MODE_CTR, |
markrad | 0:cdf462088d13 | 139 | MBEDTLS_MODE_GCM, |
markrad | 0:cdf462088d13 | 140 | MBEDTLS_MODE_STREAM, |
markrad | 0:cdf462088d13 | 141 | MBEDTLS_MODE_CCM, |
markrad | 0:cdf462088d13 | 142 | } mbedtls_cipher_mode_t; |
markrad | 0:cdf462088d13 | 143 | |
markrad | 0:cdf462088d13 | 144 | typedef enum { |
markrad | 0:cdf462088d13 | 145 | MBEDTLS_PADDING_PKCS7 = 0, /**< PKCS7 padding (default) */ |
markrad | 0:cdf462088d13 | 146 | MBEDTLS_PADDING_ONE_AND_ZEROS, /**< ISO/IEC 7816-4 padding */ |
markrad | 0:cdf462088d13 | 147 | MBEDTLS_PADDING_ZEROS_AND_LEN, /**< ANSI X.923 padding */ |
markrad | 0:cdf462088d13 | 148 | MBEDTLS_PADDING_ZEROS, /**< zero padding (not reversible!) */ |
markrad | 0:cdf462088d13 | 149 | MBEDTLS_PADDING_NONE, /**< never pad (full blocks only) */ |
markrad | 0:cdf462088d13 | 150 | } mbedtls_cipher_padding_t; |
markrad | 0:cdf462088d13 | 151 | |
markrad | 0:cdf462088d13 | 152 | typedef enum { |
markrad | 0:cdf462088d13 | 153 | MBEDTLS_OPERATION_NONE = -1, |
markrad | 0:cdf462088d13 | 154 | MBEDTLS_DECRYPT = 0, |
markrad | 0:cdf462088d13 | 155 | MBEDTLS_ENCRYPT, |
markrad | 0:cdf462088d13 | 156 | } mbedtls_operation_t; |
markrad | 0:cdf462088d13 | 157 | |
markrad | 0:cdf462088d13 | 158 | enum { |
markrad | 0:cdf462088d13 | 159 | /** Undefined key length */ |
markrad | 0:cdf462088d13 | 160 | MBEDTLS_KEY_LENGTH_NONE = 0, |
markrad | 0:cdf462088d13 | 161 | /** Key length, in bits (including parity), for DES keys */ |
markrad | 0:cdf462088d13 | 162 | MBEDTLS_KEY_LENGTH_DES = 64, |
markrad | 0:cdf462088d13 | 163 | /** Key length, in bits (including parity), for DES in two key EDE */ |
markrad | 0:cdf462088d13 | 164 | MBEDTLS_KEY_LENGTH_DES_EDE = 128, |
markrad | 0:cdf462088d13 | 165 | /** Key length, in bits (including parity), for DES in three-key EDE */ |
markrad | 0:cdf462088d13 | 166 | MBEDTLS_KEY_LENGTH_DES_EDE3 = 192, |
markrad | 0:cdf462088d13 | 167 | }; |
markrad | 0:cdf462088d13 | 168 | |
markrad | 0:cdf462088d13 | 169 | /** Maximum length of any IV, in bytes */ |
markrad | 0:cdf462088d13 | 170 | #define MBEDTLS_MAX_IV_LENGTH 16 |
markrad | 0:cdf462088d13 | 171 | /** Maximum block size of any cipher, in bytes */ |
markrad | 0:cdf462088d13 | 172 | #define MBEDTLS_MAX_BLOCK_LENGTH 16 |
markrad | 0:cdf462088d13 | 173 | |
markrad | 0:cdf462088d13 | 174 | /** |
markrad | 0:cdf462088d13 | 175 | * Base cipher information (opaque struct). |
markrad | 0:cdf462088d13 | 176 | */ |
markrad | 0:cdf462088d13 | 177 | typedef struct mbedtls_cipher_base_t mbedtls_cipher_base_t; |
markrad | 0:cdf462088d13 | 178 | |
markrad | 0:cdf462088d13 | 179 | /** |
markrad | 0:cdf462088d13 | 180 | * CMAC context (opaque struct). |
markrad | 0:cdf462088d13 | 181 | */ |
markrad | 0:cdf462088d13 | 182 | typedef struct mbedtls_cmac_context_t mbedtls_cmac_context_t; |
markrad | 0:cdf462088d13 | 183 | |
markrad | 0:cdf462088d13 | 184 | /** |
markrad | 0:cdf462088d13 | 185 | * Cipher information. Allows cipher functions to be called in a generic way. |
markrad | 0:cdf462088d13 | 186 | */ |
markrad | 0:cdf462088d13 | 187 | typedef struct { |
markrad | 0:cdf462088d13 | 188 | /** Full cipher identifier (e.g. MBEDTLS_CIPHER_AES_256_CBC) */ |
markrad | 0:cdf462088d13 | 189 | mbedtls_cipher_type_t type; |
markrad | 0:cdf462088d13 | 190 | |
markrad | 0:cdf462088d13 | 191 | /** Cipher mode (e.g. MBEDTLS_MODE_CBC) */ |
markrad | 0:cdf462088d13 | 192 | mbedtls_cipher_mode_t mode; |
markrad | 0:cdf462088d13 | 193 | |
markrad | 0:cdf462088d13 | 194 | /** Cipher key length, in bits (default length for variable sized ciphers) |
markrad | 0:cdf462088d13 | 195 | * (Includes parity bits for ciphers like DES) */ |
markrad | 0:cdf462088d13 | 196 | unsigned int key_bitlen; |
markrad | 0:cdf462088d13 | 197 | |
markrad | 0:cdf462088d13 | 198 | /** Name of the cipher */ |
markrad | 0:cdf462088d13 | 199 | const char * name; |
markrad | 0:cdf462088d13 | 200 | |
markrad | 0:cdf462088d13 | 201 | /** IV/NONCE size, in bytes. |
markrad | 0:cdf462088d13 | 202 | * For cipher that accept many sizes: recommended size */ |
markrad | 0:cdf462088d13 | 203 | unsigned int iv_size; |
markrad | 0:cdf462088d13 | 204 | |
markrad | 0:cdf462088d13 | 205 | /** Flags for variable IV size, variable key size, etc. */ |
markrad | 0:cdf462088d13 | 206 | int flags; |
markrad | 0:cdf462088d13 | 207 | |
markrad | 0:cdf462088d13 | 208 | /** block size, in bytes */ |
markrad | 0:cdf462088d13 | 209 | unsigned int block_size; |
markrad | 0:cdf462088d13 | 210 | |
markrad | 0:cdf462088d13 | 211 | /** Base cipher information and functions */ |
markrad | 0:cdf462088d13 | 212 | const mbedtls_cipher_base_t *base; |
markrad | 0:cdf462088d13 | 213 | |
markrad | 0:cdf462088d13 | 214 | } mbedtls_cipher_info_t; |
markrad | 0:cdf462088d13 | 215 | |
markrad | 0:cdf462088d13 | 216 | /** |
markrad | 0:cdf462088d13 | 217 | * Generic cipher context. |
markrad | 0:cdf462088d13 | 218 | */ |
markrad | 0:cdf462088d13 | 219 | typedef struct { |
markrad | 0:cdf462088d13 | 220 | /** Information about the associated cipher */ |
markrad | 0:cdf462088d13 | 221 | const mbedtls_cipher_info_t *cipher_info; |
markrad | 0:cdf462088d13 | 222 | |
markrad | 0:cdf462088d13 | 223 | /** Key length to use */ |
markrad | 0:cdf462088d13 | 224 | int key_bitlen; |
markrad | 0:cdf462088d13 | 225 | |
markrad | 0:cdf462088d13 | 226 | /** Operation that the context's key has been initialised for */ |
markrad | 0:cdf462088d13 | 227 | mbedtls_operation_t operation; |
markrad | 0:cdf462088d13 | 228 | |
markrad | 0:cdf462088d13 | 229 | #if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING) |
markrad | 0:cdf462088d13 | 230 | /** Padding functions to use, if relevant for cipher mode */ |
markrad | 0:cdf462088d13 | 231 | void (*add_padding)( unsigned char *output, size_t olen, size_t data_len ); |
markrad | 0:cdf462088d13 | 232 | int (*get_padding)( unsigned char *input, size_t ilen, size_t *data_len ); |
markrad | 0:cdf462088d13 | 233 | #endif |
markrad | 0:cdf462088d13 | 234 | |
markrad | 0:cdf462088d13 | 235 | /** Buffer for data that hasn't been encrypted yet */ |
markrad | 0:cdf462088d13 | 236 | unsigned char unprocessed_data[MBEDTLS_MAX_BLOCK_LENGTH]; |
markrad | 0:cdf462088d13 | 237 | |
markrad | 0:cdf462088d13 | 238 | /** Number of bytes that still need processing */ |
markrad | 0:cdf462088d13 | 239 | size_t unprocessed_len; |
markrad | 0:cdf462088d13 | 240 | |
markrad | 0:cdf462088d13 | 241 | /** Current IV or NONCE_COUNTER for CTR-mode */ |
markrad | 0:cdf462088d13 | 242 | unsigned char iv[MBEDTLS_MAX_IV_LENGTH]; |
markrad | 0:cdf462088d13 | 243 | |
markrad | 0:cdf462088d13 | 244 | /** IV size in bytes (for ciphers with variable-length IVs) */ |
markrad | 0:cdf462088d13 | 245 | size_t iv_size; |
markrad | 0:cdf462088d13 | 246 | |
markrad | 0:cdf462088d13 | 247 | /** Cipher-specific context */ |
markrad | 0:cdf462088d13 | 248 | void *cipher_ctx; |
markrad | 0:cdf462088d13 | 249 | |
markrad | 0:cdf462088d13 | 250 | #if defined(MBEDTLS_CMAC_C) |
markrad | 0:cdf462088d13 | 251 | /** CMAC Specific context */ |
markrad | 0:cdf462088d13 | 252 | mbedtls_cmac_context_t *cmac_ctx; |
markrad | 0:cdf462088d13 | 253 | #endif |
markrad | 0:cdf462088d13 | 254 | } mbedtls_cipher_context_t; |
markrad | 0:cdf462088d13 | 255 | |
markrad | 0:cdf462088d13 | 256 | /** |
markrad | 0:cdf462088d13 | 257 | * \brief Returns the list of ciphers supported by the generic cipher module. |
markrad | 0:cdf462088d13 | 258 | * |
markrad | 0:cdf462088d13 | 259 | * \return a statically allocated array of ciphers, the last entry |
markrad | 0:cdf462088d13 | 260 | * is 0. |
markrad | 0:cdf462088d13 | 261 | */ |
markrad | 0:cdf462088d13 | 262 | const int *mbedtls_cipher_list( void ); |
markrad | 0:cdf462088d13 | 263 | |
markrad | 0:cdf462088d13 | 264 | /** |
markrad | 0:cdf462088d13 | 265 | * \brief Returns the cipher information structure associated |
markrad | 0:cdf462088d13 | 266 | * with the given cipher name. |
markrad | 0:cdf462088d13 | 267 | * |
markrad | 0:cdf462088d13 | 268 | * \param cipher_name Name of the cipher to search for. |
markrad | 0:cdf462088d13 | 269 | * |
markrad | 0:cdf462088d13 | 270 | * \return the cipher information structure associated with the |
markrad | 0:cdf462088d13 | 271 | * given cipher_name, or NULL if not found. |
markrad | 0:cdf462088d13 | 272 | */ |
markrad | 0:cdf462088d13 | 273 | const mbedtls_cipher_info_t *mbedtls_cipher_info_from_string( const char *cipher_name ); |
markrad | 0:cdf462088d13 | 274 | |
markrad | 0:cdf462088d13 | 275 | /** |
markrad | 0:cdf462088d13 | 276 | * \brief Returns the cipher information structure associated |
markrad | 0:cdf462088d13 | 277 | * with the given cipher type. |
markrad | 0:cdf462088d13 | 278 | * |
markrad | 0:cdf462088d13 | 279 | * \param cipher_type Type of the cipher to search for. |
markrad | 0:cdf462088d13 | 280 | * |
markrad | 0:cdf462088d13 | 281 | * \return the cipher information structure associated with the |
markrad | 0:cdf462088d13 | 282 | * given cipher_type, or NULL if not found. |
markrad | 0:cdf462088d13 | 283 | */ |
markrad | 0:cdf462088d13 | 284 | const mbedtls_cipher_info_t *mbedtls_cipher_info_from_type( const mbedtls_cipher_type_t cipher_type ); |
markrad | 0:cdf462088d13 | 285 | |
markrad | 0:cdf462088d13 | 286 | /** |
markrad | 0:cdf462088d13 | 287 | * \brief Returns the cipher information structure associated |
markrad | 0:cdf462088d13 | 288 | * with the given cipher id, key size and mode. |
markrad | 0:cdf462088d13 | 289 | * |
markrad | 0:cdf462088d13 | 290 | * \param cipher_id Id of the cipher to search for |
markrad | 0:cdf462088d13 | 291 | * (e.g. MBEDTLS_CIPHER_ID_AES) |
markrad | 0:cdf462088d13 | 292 | * \param key_bitlen Length of the key in bits |
markrad | 0:cdf462088d13 | 293 | * \param mode Cipher mode (e.g. MBEDTLS_MODE_CBC) |
markrad | 0:cdf462088d13 | 294 | * |
markrad | 0:cdf462088d13 | 295 | * \return the cipher information structure associated with the |
markrad | 0:cdf462088d13 | 296 | * given cipher_type, or NULL if not found. |
markrad | 0:cdf462088d13 | 297 | */ |
markrad | 0:cdf462088d13 | 298 | const mbedtls_cipher_info_t *mbedtls_cipher_info_from_values( const mbedtls_cipher_id_t cipher_id, |
markrad | 0:cdf462088d13 | 299 | int key_bitlen, |
markrad | 0:cdf462088d13 | 300 | const mbedtls_cipher_mode_t mode ); |
markrad | 0:cdf462088d13 | 301 | |
markrad | 0:cdf462088d13 | 302 | /** |
markrad | 0:cdf462088d13 | 303 | * \brief Initialize a cipher_context (as NONE) |
markrad | 0:cdf462088d13 | 304 | */ |
markrad | 0:cdf462088d13 | 305 | void mbedtls_cipher_init( mbedtls_cipher_context_t *ctx ); |
markrad | 0:cdf462088d13 | 306 | |
markrad | 0:cdf462088d13 | 307 | /** |
markrad | 0:cdf462088d13 | 308 | * \brief Free and clear the cipher-specific context of ctx. |
markrad | 0:cdf462088d13 | 309 | * Freeing ctx itself remains the responsibility of the |
markrad | 0:cdf462088d13 | 310 | * caller. |
markrad | 0:cdf462088d13 | 311 | */ |
markrad | 0:cdf462088d13 | 312 | void mbedtls_cipher_free( mbedtls_cipher_context_t *ctx ); |
markrad | 0:cdf462088d13 | 313 | |
markrad | 0:cdf462088d13 | 314 | /** |
markrad | 0:cdf462088d13 | 315 | * \brief Initialises and fills the cipher context structure with |
markrad | 0:cdf462088d13 | 316 | * the appropriate values. |
markrad | 0:cdf462088d13 | 317 | * |
markrad | 0:cdf462088d13 | 318 | * \note Currently also clears structure. In future versions you |
markrad | 0:cdf462088d13 | 319 | * will be required to call mbedtls_cipher_init() on the structure |
markrad | 0:cdf462088d13 | 320 | * first. |
markrad | 0:cdf462088d13 | 321 | * |
markrad | 0:cdf462088d13 | 322 | * \param ctx context to initialise. May not be NULL. |
markrad | 0:cdf462088d13 | 323 | * \param cipher_info cipher to use. |
markrad | 0:cdf462088d13 | 324 | * |
markrad | 0:cdf462088d13 | 325 | * \return 0 on success, |
markrad | 0:cdf462088d13 | 326 | * MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA on parameter failure, |
markrad | 0:cdf462088d13 | 327 | * MBEDTLS_ERR_CIPHER_ALLOC_FAILED if allocation of the |
markrad | 0:cdf462088d13 | 328 | * cipher-specific context failed. |
markrad | 0:cdf462088d13 | 329 | */ |
markrad | 0:cdf462088d13 | 330 | int mbedtls_cipher_setup( mbedtls_cipher_context_t *ctx, const mbedtls_cipher_info_t *cipher_info ); |
markrad | 0:cdf462088d13 | 331 | |
markrad | 0:cdf462088d13 | 332 | /** |
markrad | 0:cdf462088d13 | 333 | * \brief Returns the block size of the given cipher. |
markrad | 0:cdf462088d13 | 334 | * |
markrad | 0:cdf462088d13 | 335 | * \param ctx cipher's context. Must have been initialised. |
markrad | 0:cdf462088d13 | 336 | * |
markrad | 0:cdf462088d13 | 337 | * \return size of the cipher's blocks, or 0 if ctx has not been |
markrad | 0:cdf462088d13 | 338 | * initialised. |
markrad | 0:cdf462088d13 | 339 | */ |
markrad | 0:cdf462088d13 | 340 | static inline unsigned int mbedtls_cipher_get_block_size( const mbedtls_cipher_context_t *ctx ) |
markrad | 0:cdf462088d13 | 341 | { |
markrad | 0:cdf462088d13 | 342 | if( NULL == ctx || NULL == ctx->cipher_info ) |
markrad | 0:cdf462088d13 | 343 | return 0; |
markrad | 0:cdf462088d13 | 344 | |
markrad | 0:cdf462088d13 | 345 | return ctx->cipher_info->block_size; |
markrad | 0:cdf462088d13 | 346 | } |
markrad | 0:cdf462088d13 | 347 | |
markrad | 0:cdf462088d13 | 348 | /** |
markrad | 0:cdf462088d13 | 349 | * \brief Returns the mode of operation for the cipher. |
markrad | 0:cdf462088d13 | 350 | * (e.g. MBEDTLS_MODE_CBC) |
markrad | 0:cdf462088d13 | 351 | * |
markrad | 0:cdf462088d13 | 352 | * \param ctx cipher's context. Must have been initialised. |
markrad | 0:cdf462088d13 | 353 | * |
markrad | 0:cdf462088d13 | 354 | * \return mode of operation, or MBEDTLS_MODE_NONE if ctx |
markrad | 0:cdf462088d13 | 355 | * has not been initialised. |
markrad | 0:cdf462088d13 | 356 | */ |
markrad | 0:cdf462088d13 | 357 | static inline mbedtls_cipher_mode_t mbedtls_cipher_get_cipher_mode( const mbedtls_cipher_context_t *ctx ) |
markrad | 0:cdf462088d13 | 358 | { |
markrad | 0:cdf462088d13 | 359 | if( NULL == ctx || NULL == ctx->cipher_info ) |
markrad | 0:cdf462088d13 | 360 | return MBEDTLS_MODE_NONE; |
markrad | 0:cdf462088d13 | 361 | |
markrad | 0:cdf462088d13 | 362 | return ctx->cipher_info->mode; |
markrad | 0:cdf462088d13 | 363 | } |
markrad | 0:cdf462088d13 | 364 | |
markrad | 0:cdf462088d13 | 365 | /** |
markrad | 0:cdf462088d13 | 366 | * \brief Returns the size of the cipher's IV/NONCE in bytes. |
markrad | 0:cdf462088d13 | 367 | * |
markrad | 0:cdf462088d13 | 368 | * \param ctx cipher's context. Must have been initialised. |
markrad | 0:cdf462088d13 | 369 | * |
markrad | 0:cdf462088d13 | 370 | * \return If IV has not been set yet: (recommended) IV size |
markrad | 0:cdf462088d13 | 371 | * (0 for ciphers not using IV/NONCE). |
markrad | 0:cdf462088d13 | 372 | * If IV has already been set: actual size. |
markrad | 0:cdf462088d13 | 373 | */ |
markrad | 0:cdf462088d13 | 374 | static inline int mbedtls_cipher_get_iv_size( const mbedtls_cipher_context_t *ctx ) |
markrad | 0:cdf462088d13 | 375 | { |
markrad | 0:cdf462088d13 | 376 | if( NULL == ctx || NULL == ctx->cipher_info ) |
markrad | 0:cdf462088d13 | 377 | return 0; |
markrad | 0:cdf462088d13 | 378 | |
markrad | 0:cdf462088d13 | 379 | if( ctx->iv_size != 0 ) |
markrad | 0:cdf462088d13 | 380 | return (int) ctx->iv_size; |
markrad | 0:cdf462088d13 | 381 | |
markrad | 0:cdf462088d13 | 382 | return (int) ctx->cipher_info->iv_size; |
markrad | 0:cdf462088d13 | 383 | } |
markrad | 0:cdf462088d13 | 384 | |
markrad | 0:cdf462088d13 | 385 | /** |
markrad | 0:cdf462088d13 | 386 | * \brief Returns the type of the given cipher. |
markrad | 0:cdf462088d13 | 387 | * |
markrad | 0:cdf462088d13 | 388 | * \param ctx cipher's context. Must have been initialised. |
markrad | 0:cdf462088d13 | 389 | * |
markrad | 0:cdf462088d13 | 390 | * \return type of the cipher, or MBEDTLS_CIPHER_NONE if ctx has |
markrad | 0:cdf462088d13 | 391 | * not been initialised. |
markrad | 0:cdf462088d13 | 392 | */ |
markrad | 0:cdf462088d13 | 393 | static inline mbedtls_cipher_type_t mbedtls_cipher_get_type( const mbedtls_cipher_context_t *ctx ) |
markrad | 0:cdf462088d13 | 394 | { |
markrad | 0:cdf462088d13 | 395 | if( NULL == ctx || NULL == ctx->cipher_info ) |
markrad | 0:cdf462088d13 | 396 | return MBEDTLS_CIPHER_NONE; |
markrad | 0:cdf462088d13 | 397 | |
markrad | 0:cdf462088d13 | 398 | return ctx->cipher_info->type; |
markrad | 0:cdf462088d13 | 399 | } |
markrad | 0:cdf462088d13 | 400 | |
markrad | 0:cdf462088d13 | 401 | /** |
markrad | 0:cdf462088d13 | 402 | * \brief Returns the name of the given cipher, as a string. |
markrad | 0:cdf462088d13 | 403 | * |
markrad | 0:cdf462088d13 | 404 | * \param ctx cipher's context. Must have been initialised. |
markrad | 0:cdf462088d13 | 405 | * |
markrad | 0:cdf462088d13 | 406 | * \return name of the cipher, or NULL if ctx was not initialised. |
markrad | 0:cdf462088d13 | 407 | */ |
markrad | 0:cdf462088d13 | 408 | static inline const char *mbedtls_cipher_get_name( const mbedtls_cipher_context_t *ctx ) |
markrad | 0:cdf462088d13 | 409 | { |
markrad | 0:cdf462088d13 | 410 | if( NULL == ctx || NULL == ctx->cipher_info ) |
markrad | 0:cdf462088d13 | 411 | return 0; |
markrad | 0:cdf462088d13 | 412 | |
markrad | 0:cdf462088d13 | 413 | return ctx->cipher_info->name; |
markrad | 0:cdf462088d13 | 414 | } |
markrad | 0:cdf462088d13 | 415 | |
markrad | 0:cdf462088d13 | 416 | /** |
markrad | 0:cdf462088d13 | 417 | * \brief Returns the key length of the cipher. |
markrad | 0:cdf462088d13 | 418 | * |
markrad | 0:cdf462088d13 | 419 | * \param ctx cipher's context. Must have been initialised. |
markrad | 0:cdf462088d13 | 420 | * |
markrad | 0:cdf462088d13 | 421 | * \return cipher's key length, in bits, or |
markrad | 0:cdf462088d13 | 422 | * MBEDTLS_KEY_LENGTH_NONE if ctx has not been |
markrad | 0:cdf462088d13 | 423 | * initialised. |
markrad | 0:cdf462088d13 | 424 | */ |
markrad | 0:cdf462088d13 | 425 | static inline int mbedtls_cipher_get_key_bitlen( const mbedtls_cipher_context_t *ctx ) |
markrad | 0:cdf462088d13 | 426 | { |
markrad | 0:cdf462088d13 | 427 | if( NULL == ctx || NULL == ctx->cipher_info ) |
markrad | 0:cdf462088d13 | 428 | return MBEDTLS_KEY_LENGTH_NONE; |
markrad | 0:cdf462088d13 | 429 | |
markrad | 0:cdf462088d13 | 430 | return (int) ctx->cipher_info->key_bitlen; |
markrad | 0:cdf462088d13 | 431 | } |
markrad | 0:cdf462088d13 | 432 | |
markrad | 0:cdf462088d13 | 433 | /** |
markrad | 0:cdf462088d13 | 434 | * \brief Returns the operation of the given cipher. |
markrad | 0:cdf462088d13 | 435 | * |
markrad | 0:cdf462088d13 | 436 | * \param ctx cipher's context. Must have been initialised. |
markrad | 0:cdf462088d13 | 437 | * |
markrad | 0:cdf462088d13 | 438 | * \return operation (MBEDTLS_ENCRYPT or MBEDTLS_DECRYPT), |
markrad | 0:cdf462088d13 | 439 | * or MBEDTLS_OPERATION_NONE if ctx has not been |
markrad | 0:cdf462088d13 | 440 | * initialised. |
markrad | 0:cdf462088d13 | 441 | */ |
markrad | 0:cdf462088d13 | 442 | static inline mbedtls_operation_t mbedtls_cipher_get_operation( const mbedtls_cipher_context_t *ctx ) |
markrad | 0:cdf462088d13 | 443 | { |
markrad | 0:cdf462088d13 | 444 | if( NULL == ctx || NULL == ctx->cipher_info ) |
markrad | 0:cdf462088d13 | 445 | return MBEDTLS_OPERATION_NONE; |
markrad | 0:cdf462088d13 | 446 | |
markrad | 0:cdf462088d13 | 447 | return ctx->operation; |
markrad | 0:cdf462088d13 | 448 | } |
markrad | 0:cdf462088d13 | 449 | |
markrad | 0:cdf462088d13 | 450 | /** |
markrad | 0:cdf462088d13 | 451 | * \brief Set the key to use with the given context. |
markrad | 0:cdf462088d13 | 452 | * |
markrad | 0:cdf462088d13 | 453 | * \param ctx generic cipher context. May not be NULL. Must have been |
markrad | 0:cdf462088d13 | 454 | * initialised using cipher_context_from_type or |
markrad | 0:cdf462088d13 | 455 | * cipher_context_from_string. |
markrad | 0:cdf462088d13 | 456 | * \param key The key to use. |
markrad | 0:cdf462088d13 | 457 | * \param key_bitlen key length to use, in bits. |
markrad | 0:cdf462088d13 | 458 | * \param operation Operation that the key will be used for, either |
markrad | 0:cdf462088d13 | 459 | * MBEDTLS_ENCRYPT or MBEDTLS_DECRYPT. |
markrad | 0:cdf462088d13 | 460 | * |
markrad | 0:cdf462088d13 | 461 | * \returns 0 on success, MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA if |
markrad | 0:cdf462088d13 | 462 | * parameter verification fails or a cipher specific |
markrad | 0:cdf462088d13 | 463 | * error code. |
markrad | 0:cdf462088d13 | 464 | */ |
markrad | 0:cdf462088d13 | 465 | int mbedtls_cipher_setkey( mbedtls_cipher_context_t *ctx, const unsigned char *key, |
markrad | 0:cdf462088d13 | 466 | int key_bitlen, const mbedtls_operation_t operation ); |
markrad | 0:cdf462088d13 | 467 | |
markrad | 0:cdf462088d13 | 468 | #if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING) |
markrad | 0:cdf462088d13 | 469 | /** |
markrad | 0:cdf462088d13 | 470 | * \brief Set padding mode, for cipher modes that use padding. |
markrad | 0:cdf462088d13 | 471 | * (Default: PKCS7 padding.) |
markrad | 0:cdf462088d13 | 472 | * |
markrad | 0:cdf462088d13 | 473 | * \param ctx generic cipher context |
markrad | 0:cdf462088d13 | 474 | * \param mode padding mode |
markrad | 0:cdf462088d13 | 475 | * |
markrad | 0:cdf462088d13 | 476 | * \returns 0 on success, MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE |
markrad | 0:cdf462088d13 | 477 | * if selected padding mode is not supported, or |
markrad | 0:cdf462088d13 | 478 | * MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA if the cipher mode |
markrad | 0:cdf462088d13 | 479 | * does not support padding. |
markrad | 0:cdf462088d13 | 480 | */ |
markrad | 0:cdf462088d13 | 481 | int mbedtls_cipher_set_padding_mode( mbedtls_cipher_context_t *ctx, mbedtls_cipher_padding_t mode ); |
markrad | 0:cdf462088d13 | 482 | #endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */ |
markrad | 0:cdf462088d13 | 483 | |
markrad | 0:cdf462088d13 | 484 | /** |
markrad | 0:cdf462088d13 | 485 | * \brief Set the initialization vector (IV) or nonce |
markrad | 0:cdf462088d13 | 486 | * |
markrad | 0:cdf462088d13 | 487 | * \param ctx generic cipher context |
markrad | 0:cdf462088d13 | 488 | * \param iv IV to use (or NONCE_COUNTER for CTR-mode ciphers) |
markrad | 0:cdf462088d13 | 489 | * \param iv_len IV length for ciphers with variable-size IV; |
markrad | 0:cdf462088d13 | 490 | * discarded by ciphers with fixed-size IV. |
markrad | 0:cdf462088d13 | 491 | * |
markrad | 0:cdf462088d13 | 492 | * \returns 0 on success, or MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA |
markrad | 0:cdf462088d13 | 493 | * |
markrad | 0:cdf462088d13 | 494 | * \note Some ciphers don't use IVs nor NONCE. For these |
markrad | 0:cdf462088d13 | 495 | * ciphers, this function has no effect. |
markrad | 0:cdf462088d13 | 496 | */ |
markrad | 0:cdf462088d13 | 497 | int mbedtls_cipher_set_iv( mbedtls_cipher_context_t *ctx, |
markrad | 0:cdf462088d13 | 498 | const unsigned char *iv, size_t iv_len ); |
markrad | 0:cdf462088d13 | 499 | |
markrad | 0:cdf462088d13 | 500 | /** |
markrad | 0:cdf462088d13 | 501 | * \brief Finish preparation of the given context |
markrad | 0:cdf462088d13 | 502 | * |
markrad | 0:cdf462088d13 | 503 | * \param ctx generic cipher context |
markrad | 0:cdf462088d13 | 504 | * |
markrad | 0:cdf462088d13 | 505 | * \returns 0 on success, MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA |
markrad | 0:cdf462088d13 | 506 | * if parameter verification fails. |
markrad | 0:cdf462088d13 | 507 | */ |
markrad | 0:cdf462088d13 | 508 | int mbedtls_cipher_reset( mbedtls_cipher_context_t *ctx ); |
markrad | 0:cdf462088d13 | 509 | |
markrad | 0:cdf462088d13 | 510 | #if defined(MBEDTLS_GCM_C) |
markrad | 0:cdf462088d13 | 511 | /** |
markrad | 0:cdf462088d13 | 512 | * \brief Add additional data (for AEAD ciphers). |
markrad | 0:cdf462088d13 | 513 | * Currently only supported with GCM. |
markrad | 0:cdf462088d13 | 514 | * Must be called exactly once, after mbedtls_cipher_reset(). |
markrad | 0:cdf462088d13 | 515 | * |
markrad | 0:cdf462088d13 | 516 | * \param ctx generic cipher context |
markrad | 0:cdf462088d13 | 517 | * \param ad Additional data to use. |
markrad | 0:cdf462088d13 | 518 | * \param ad_len Length of ad. |
markrad | 0:cdf462088d13 | 519 | * |
markrad | 0:cdf462088d13 | 520 | * \return 0 on success, or a specific error code. |
markrad | 0:cdf462088d13 | 521 | */ |
markrad | 0:cdf462088d13 | 522 | int mbedtls_cipher_update_ad( mbedtls_cipher_context_t *ctx, |
markrad | 0:cdf462088d13 | 523 | const unsigned char *ad, size_t ad_len ); |
markrad | 0:cdf462088d13 | 524 | #endif /* MBEDTLS_GCM_C */ |
markrad | 0:cdf462088d13 | 525 | |
markrad | 0:cdf462088d13 | 526 | /** |
markrad | 0:cdf462088d13 | 527 | * \brief Generic cipher update function. Encrypts/decrypts |
markrad | 0:cdf462088d13 | 528 | * using the given cipher context. Writes as many block |
markrad | 0:cdf462088d13 | 529 | * size'd blocks of data as possible to output. Any data |
markrad | 0:cdf462088d13 | 530 | * that cannot be written immediately will either be added |
markrad | 0:cdf462088d13 | 531 | * to the next block, or flushed when cipher_final is |
markrad | 0:cdf462088d13 | 532 | * called. |
markrad | 0:cdf462088d13 | 533 | * Exception: for MBEDTLS_MODE_ECB, expects single block |
markrad | 0:cdf462088d13 | 534 | * in size (e.g. 16 bytes for AES) |
markrad | 0:cdf462088d13 | 535 | * |
markrad | 0:cdf462088d13 | 536 | * \param ctx generic cipher context |
markrad | 0:cdf462088d13 | 537 | * \param input buffer holding the input data |
markrad | 0:cdf462088d13 | 538 | * \param ilen length of the input data |
markrad | 0:cdf462088d13 | 539 | * \param output buffer for the output data. Should be able to hold at |
markrad | 0:cdf462088d13 | 540 | * least ilen + block_size. Cannot be the same buffer as |
markrad | 0:cdf462088d13 | 541 | * input! |
markrad | 0:cdf462088d13 | 542 | * \param olen length of the output data, will be filled with the |
markrad | 0:cdf462088d13 | 543 | * actual number of bytes written. |
markrad | 0:cdf462088d13 | 544 | * |
markrad | 0:cdf462088d13 | 545 | * \returns 0 on success, MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA if |
markrad | 0:cdf462088d13 | 546 | * parameter verification fails, |
markrad | 0:cdf462088d13 | 547 | * MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE on an |
markrad | 0:cdf462088d13 | 548 | * unsupported mode for a cipher or a cipher specific |
markrad | 0:cdf462088d13 | 549 | * error code. |
markrad | 0:cdf462088d13 | 550 | * |
markrad | 0:cdf462088d13 | 551 | * \note If the underlying cipher is GCM, all calls to this |
markrad | 0:cdf462088d13 | 552 | * function, except the last one before mbedtls_cipher_finish(), |
markrad | 0:cdf462088d13 | 553 | * must have ilen a multiple of the block size. |
markrad | 0:cdf462088d13 | 554 | */ |
markrad | 0:cdf462088d13 | 555 | int mbedtls_cipher_update( mbedtls_cipher_context_t *ctx, const unsigned char *input, |
markrad | 0:cdf462088d13 | 556 | size_t ilen, unsigned char *output, size_t *olen ); |
markrad | 0:cdf462088d13 | 557 | |
markrad | 0:cdf462088d13 | 558 | /** |
markrad | 0:cdf462088d13 | 559 | * \brief Generic cipher finalisation function. If data still |
markrad | 0:cdf462088d13 | 560 | * needs to be flushed from an incomplete block, data |
markrad | 0:cdf462088d13 | 561 | * contained within it will be padded with the size of |
markrad | 0:cdf462088d13 | 562 | * the last block, and written to the output buffer. |
markrad | 0:cdf462088d13 | 563 | * |
markrad | 0:cdf462088d13 | 564 | * \param ctx Generic cipher context |
markrad | 0:cdf462088d13 | 565 | * \param output buffer to write data to. Needs block_size available. |
markrad | 0:cdf462088d13 | 566 | * \param olen length of the data written to the output buffer. |
markrad | 0:cdf462088d13 | 567 | * |
markrad | 0:cdf462088d13 | 568 | * \returns 0 on success, MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA if |
markrad | 0:cdf462088d13 | 569 | * parameter verification fails, |
markrad | 0:cdf462088d13 | 570 | * MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED if decryption |
markrad | 0:cdf462088d13 | 571 | * expected a full block but was not provided one, |
markrad | 0:cdf462088d13 | 572 | * MBEDTLS_ERR_CIPHER_INVALID_PADDING on invalid padding |
markrad | 0:cdf462088d13 | 573 | * while decrypting or a cipher specific error code. |
markrad | 0:cdf462088d13 | 574 | */ |
markrad | 0:cdf462088d13 | 575 | int mbedtls_cipher_finish( mbedtls_cipher_context_t *ctx, |
markrad | 0:cdf462088d13 | 576 | unsigned char *output, size_t *olen ); |
markrad | 0:cdf462088d13 | 577 | |
markrad | 0:cdf462088d13 | 578 | #if defined(MBEDTLS_GCM_C) |
markrad | 0:cdf462088d13 | 579 | /** |
markrad | 0:cdf462088d13 | 580 | * \brief Write tag for AEAD ciphers. |
markrad | 0:cdf462088d13 | 581 | * Currently only supported with GCM. |
markrad | 0:cdf462088d13 | 582 | * Must be called after mbedtls_cipher_finish(). |
markrad | 0:cdf462088d13 | 583 | * |
markrad | 0:cdf462088d13 | 584 | * \param ctx Generic cipher context |
markrad | 0:cdf462088d13 | 585 | * \param tag buffer to write the tag |
markrad | 0:cdf462088d13 | 586 | * \param tag_len Length of the tag to write |
markrad | 0:cdf462088d13 | 587 | * |
markrad | 0:cdf462088d13 | 588 | * \return 0 on success, or a specific error code. |
markrad | 0:cdf462088d13 | 589 | */ |
markrad | 0:cdf462088d13 | 590 | int mbedtls_cipher_write_tag( mbedtls_cipher_context_t *ctx, |
markrad | 0:cdf462088d13 | 591 | unsigned char *tag, size_t tag_len ); |
markrad | 0:cdf462088d13 | 592 | |
markrad | 0:cdf462088d13 | 593 | /** |
markrad | 0:cdf462088d13 | 594 | * \brief Check tag for AEAD ciphers. |
markrad | 0:cdf462088d13 | 595 | * Currently only supported with GCM. |
markrad | 0:cdf462088d13 | 596 | * Must be called after mbedtls_cipher_finish(). |
markrad | 0:cdf462088d13 | 597 | * |
markrad | 0:cdf462088d13 | 598 | * \param ctx Generic cipher context |
markrad | 0:cdf462088d13 | 599 | * \param tag Buffer holding the tag |
markrad | 0:cdf462088d13 | 600 | * \param tag_len Length of the tag to check |
markrad | 0:cdf462088d13 | 601 | * |
markrad | 0:cdf462088d13 | 602 | * \return 0 on success, or a specific error code. |
markrad | 0:cdf462088d13 | 603 | */ |
markrad | 0:cdf462088d13 | 604 | int mbedtls_cipher_check_tag( mbedtls_cipher_context_t *ctx, |
markrad | 0:cdf462088d13 | 605 | const unsigned char *tag, size_t tag_len ); |
markrad | 0:cdf462088d13 | 606 | #endif /* MBEDTLS_GCM_C */ |
markrad | 0:cdf462088d13 | 607 | |
markrad | 0:cdf462088d13 | 608 | /** |
markrad | 0:cdf462088d13 | 609 | * \brief Generic all-in-one encryption/decryption |
markrad | 0:cdf462088d13 | 610 | * (for all ciphers except AEAD constructs). |
markrad | 0:cdf462088d13 | 611 | * |
markrad | 0:cdf462088d13 | 612 | * \param ctx generic cipher context |
markrad | 0:cdf462088d13 | 613 | * \param iv IV to use (or NONCE_COUNTER for CTR-mode ciphers) |
markrad | 0:cdf462088d13 | 614 | * \param iv_len IV length for ciphers with variable-size IV; |
markrad | 0:cdf462088d13 | 615 | * discarded by ciphers with fixed-size IV. |
markrad | 0:cdf462088d13 | 616 | * \param input buffer holding the input data |
markrad | 0:cdf462088d13 | 617 | * \param ilen length of the input data |
markrad | 0:cdf462088d13 | 618 | * \param output buffer for the output data. Should be able to hold at |
markrad | 0:cdf462088d13 | 619 | * least ilen + block_size. Cannot be the same buffer as |
markrad | 0:cdf462088d13 | 620 | * input! |
markrad | 0:cdf462088d13 | 621 | * \param olen length of the output data, will be filled with the |
markrad | 0:cdf462088d13 | 622 | * actual number of bytes written. |
markrad | 0:cdf462088d13 | 623 | * |
markrad | 0:cdf462088d13 | 624 | * \note Some ciphers don't use IVs nor NONCE. For these |
markrad | 0:cdf462088d13 | 625 | * ciphers, use iv = NULL and iv_len = 0. |
markrad | 0:cdf462088d13 | 626 | * |
markrad | 0:cdf462088d13 | 627 | * \returns 0 on success, or |
markrad | 0:cdf462088d13 | 628 | * MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA, or |
markrad | 0:cdf462088d13 | 629 | * MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED if decryption |
markrad | 0:cdf462088d13 | 630 | * expected a full block but was not provided one, or |
markrad | 0:cdf462088d13 | 631 | * MBEDTLS_ERR_CIPHER_INVALID_PADDING on invalid padding |
markrad | 0:cdf462088d13 | 632 | * while decrypting, or |
markrad | 0:cdf462088d13 | 633 | * a cipher specific error code. |
markrad | 0:cdf462088d13 | 634 | */ |
markrad | 0:cdf462088d13 | 635 | int mbedtls_cipher_crypt( mbedtls_cipher_context_t *ctx, |
markrad | 0:cdf462088d13 | 636 | const unsigned char *iv, size_t iv_len, |
markrad | 0:cdf462088d13 | 637 | const unsigned char *input, size_t ilen, |
markrad | 0:cdf462088d13 | 638 | unsigned char *output, size_t *olen ); |
markrad | 0:cdf462088d13 | 639 | |
markrad | 0:cdf462088d13 | 640 | #if defined(MBEDTLS_CIPHER_MODE_AEAD) |
markrad | 0:cdf462088d13 | 641 | /** |
markrad | 0:cdf462088d13 | 642 | * \brief Generic autenticated encryption (AEAD ciphers). |
markrad | 0:cdf462088d13 | 643 | * |
markrad | 0:cdf462088d13 | 644 | * \param ctx generic cipher context |
markrad | 0:cdf462088d13 | 645 | * \param iv IV to use (or NONCE_COUNTER for CTR-mode ciphers) |
markrad | 0:cdf462088d13 | 646 | * \param iv_len IV length for ciphers with variable-size IV; |
markrad | 0:cdf462088d13 | 647 | * discarded by ciphers with fixed-size IV. |
markrad | 0:cdf462088d13 | 648 | * \param ad Additional data to authenticate. |
markrad | 0:cdf462088d13 | 649 | * \param ad_len Length of ad. |
markrad | 0:cdf462088d13 | 650 | * \param input buffer holding the input data |
markrad | 0:cdf462088d13 | 651 | * \param ilen length of the input data |
markrad | 0:cdf462088d13 | 652 | * \param output buffer for the output data. |
markrad | 0:cdf462088d13 | 653 | * Should be able to hold at least ilen. |
markrad | 0:cdf462088d13 | 654 | * \param olen length of the output data, will be filled with the |
markrad | 0:cdf462088d13 | 655 | * actual number of bytes written. |
markrad | 0:cdf462088d13 | 656 | * \param tag buffer for the authentication tag |
markrad | 0:cdf462088d13 | 657 | * \param tag_len desired tag length |
markrad | 0:cdf462088d13 | 658 | * |
markrad | 0:cdf462088d13 | 659 | * \returns 0 on success, or |
markrad | 0:cdf462088d13 | 660 | * MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA, or |
markrad | 0:cdf462088d13 | 661 | * a cipher specific error code. |
markrad | 0:cdf462088d13 | 662 | */ |
markrad | 0:cdf462088d13 | 663 | int mbedtls_cipher_auth_encrypt( mbedtls_cipher_context_t *ctx, |
markrad | 0:cdf462088d13 | 664 | const unsigned char *iv, size_t iv_len, |
markrad | 0:cdf462088d13 | 665 | const unsigned char *ad, size_t ad_len, |
markrad | 0:cdf462088d13 | 666 | const unsigned char *input, size_t ilen, |
markrad | 0:cdf462088d13 | 667 | unsigned char *output, size_t *olen, |
markrad | 0:cdf462088d13 | 668 | unsigned char *tag, size_t tag_len ); |
markrad | 0:cdf462088d13 | 669 | |
markrad | 0:cdf462088d13 | 670 | /** |
markrad | 0:cdf462088d13 | 671 | * \brief Generic autenticated decryption (AEAD ciphers). |
markrad | 0:cdf462088d13 | 672 | * |
markrad | 0:cdf462088d13 | 673 | * \param ctx generic cipher context |
markrad | 0:cdf462088d13 | 674 | * \param iv IV to use (or NONCE_COUNTER for CTR-mode ciphers) |
markrad | 0:cdf462088d13 | 675 | * \param iv_len IV length for ciphers with variable-size IV; |
markrad | 0:cdf462088d13 | 676 | * discarded by ciphers with fixed-size IV. |
markrad | 0:cdf462088d13 | 677 | * \param ad Additional data to be authenticated. |
markrad | 0:cdf462088d13 | 678 | * \param ad_len Length of ad. |
markrad | 0:cdf462088d13 | 679 | * \param input buffer holding the input data |
markrad | 0:cdf462088d13 | 680 | * \param ilen length of the input data |
markrad | 0:cdf462088d13 | 681 | * \param output buffer for the output data. |
markrad | 0:cdf462088d13 | 682 | * Should be able to hold at least ilen. |
markrad | 0:cdf462088d13 | 683 | * \param olen length of the output data, will be filled with the |
markrad | 0:cdf462088d13 | 684 | * actual number of bytes written. |
markrad | 0:cdf462088d13 | 685 | * \param tag buffer holding the authentication tag |
markrad | 0:cdf462088d13 | 686 | * \param tag_len length of the authentication tag |
markrad | 0:cdf462088d13 | 687 | * |
markrad | 0:cdf462088d13 | 688 | * \returns 0 on success, or |
markrad | 0:cdf462088d13 | 689 | * MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA, or |
markrad | 0:cdf462088d13 | 690 | * MBEDTLS_ERR_CIPHER_AUTH_FAILED if data isn't authentic, |
markrad | 0:cdf462088d13 | 691 | * or a cipher specific error code. |
markrad | 0:cdf462088d13 | 692 | * |
markrad | 0:cdf462088d13 | 693 | * \note If the data is not authentic, then the output buffer |
markrad | 0:cdf462088d13 | 694 | * is zeroed out to prevent the unauthentic plaintext to |
markrad | 0:cdf462088d13 | 695 | * be used by mistake, making this interface safer. |
markrad | 0:cdf462088d13 | 696 | */ |
markrad | 0:cdf462088d13 | 697 | int mbedtls_cipher_auth_decrypt( mbedtls_cipher_context_t *ctx, |
markrad | 0:cdf462088d13 | 698 | const unsigned char *iv, size_t iv_len, |
markrad | 0:cdf462088d13 | 699 | const unsigned char *ad, size_t ad_len, |
markrad | 0:cdf462088d13 | 700 | const unsigned char *input, size_t ilen, |
markrad | 0:cdf462088d13 | 701 | unsigned char *output, size_t *olen, |
markrad | 0:cdf462088d13 | 702 | const unsigned char *tag, size_t tag_len ); |
markrad | 0:cdf462088d13 | 703 | #endif /* MBEDTLS_CIPHER_MODE_AEAD */ |
markrad | 0:cdf462088d13 | 704 | |
markrad | 0:cdf462088d13 | 705 | #ifdef __cplusplus |
markrad | 0:cdf462088d13 | 706 | } |
markrad | 0:cdf462088d13 | 707 | #endif |
markrad | 0:cdf462088d13 | 708 | |
markrad | 0:cdf462088d13 | 709 | #endif /* MBEDTLS_CIPHER_H */ |