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