1 /* 2 * This file is part of the openHiTLS project. 3 * 4 * openHiTLS is licensed under the Mulan PSL v2. 5 * You can use this software according to the terms and conditions of the Mulan PSL v2. 6 * You may obtain a copy of Mulan PSL v2 at: 7 * 8 * http://license.coscl.org.cn/MulanPSL2 9 * 10 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 11 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 12 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 13 * See the Mulan PSL v2 for more details. 14 */ 15 16 #ifndef CRYPT_AES_H 17 #define CRYPT_AES_H 18 19 #include "hitls_build.h" 20 #ifdef HITLS_CRYPTO_AES 21 22 #include <stdint.h> 23 24 #ifdef __cplusplus 25 extern "C" { 26 #endif // __cplusplus 27 28 #define CRYPT_AES_128 128 29 #define CRYPT_AES_192 192 30 #define CRYPT_AES_256 256 31 32 #define CRYPT_AES_MAX_ROUNDS 14 33 #define CRYPT_AES_MAX_KEYLEN (4 * (CRYPT_AES_MAX_ROUNDS + 1)) 34 35 /** 36 * @ingroup CRYPT_AES_Key 37 * 38 * aes key structure 39 */ 40 typedef struct { 41 uint32_t key[CRYPT_AES_MAX_KEYLEN]; 42 uint32_t rounds; 43 } CRYPT_AES_Key; 44 45 /** 46 * @ingroup aes 47 * @brief Set the AES encryption key. 48 * 49 * @param ctx [IN] AES handle 50 * @param key [IN] Encryption key 51 * @param len [IN] Key length. The value must be 16 bytes. 52 */ 53 int32_t CRYPT_AES_SetEncryptKey128(CRYPT_AES_Key *ctx, const uint8_t *key, uint32_t len); 54 55 /** 56 * @ingroup aes 57 * @brief Set the AES encryption key. 58 * 59 * @param ctx [IN] AES handle 60 * @param key [IN] Encryption key 61 * @param len [IN] Key length. The value must be 24 bytes. 62 */ 63 int32_t CRYPT_AES_SetEncryptKey192(CRYPT_AES_Key *ctx, const uint8_t *key, uint32_t len); 64 65 /** 66 * @ingroup aes 67 * @brief Set the AES encryption key. 68 * 69 * @param ctx [IN] AES handle 70 * @param key [IN] Encryption key 71 * @param len [IN] Key length. The value must be 32 bytes. 72 */ 73 int32_t CRYPT_AES_SetEncryptKey256(CRYPT_AES_Key *ctx, const uint8_t *key, uint32_t len); 74 75 /** 76 * @ingroup aes 77 * @brief Set the AES decryption key. 78 * 79 * @param ctx [IN] AES handle 80 * @param key [IN] Decryption key 81 * @param len [IN] Key length. The value must be 16 bytes. 82 */ 83 int32_t CRYPT_AES_SetDecryptKey128(CRYPT_AES_Key *ctx, const uint8_t *key, uint32_t len); 84 85 /** 86 * @ingroup aes 87 * @brief Set the AES decryption key. 88 * 89 * @param ctx [IN] AES handle 90 * @param key [IN] Decryption key 91 * @param len [IN] Key length. The value must be 24 bytes. 92 */ 93 int32_t CRYPT_AES_SetDecryptKey192(CRYPT_AES_Key *ctx, const uint8_t *key, uint32_t len); 94 95 /** 96 * @ingroup aes 97 * @brief Set the AES decryption key. 98 * 99 * @param ctx [IN] AES handle 100 * @param key [IN] Decryption key 101 * @param len [IN] Key length. The value must be 32 bytes. 102 */ 103 int32_t CRYPT_AES_SetDecryptKey256(CRYPT_AES_Key *ctx, const uint8_t *key, uint32_t len); 104 105 /** 106 * @ingroup aes 107 * @brief AES encryption 108 * 109 * @param ctx [IN] AES handle, storing keys 110 * @param in [IN] Input plaintext data. The value must be 16 bytes. 111 * @param out [OUT] Output ciphertext data. The length is 16 bytes. 112 * @param len [IN] Block length. 113 */ 114 int32_t CRYPT_AES_Encrypt(const CRYPT_AES_Key *ctx, const uint8_t *in, uint8_t *out, uint32_t len); 115 116 /** 117 * @ingroup aes 118 * @brief AES decryption 119 * 120 * @param ctx [IN] AES handle, storing keys 121 * @param in [IN] Input ciphertext data. The value must be 16 bytes. 122 * @param out [OUT] Output plaintext data. The length is 16 bytes. 123 * @param len [IN] Block length. The length is 16. 124 */ 125 int32_t CRYPT_AES_Decrypt(const CRYPT_AES_Key *ctx, const uint8_t *in, uint8_t *out, uint32_t len); 126 127 #ifdef HITLS_CRYPTO_CBC 128 /** 129 * @ingroup aes 130 * @brief AES cbc encryption 131 * 132 * @param ctx [IN] AES handle, storing keys 133 * @param in [IN] Input plaintext data, 16 bytes. 134 * @param out [OUT] Output ciphertext data. The length is 16 bytes. 135 * @param len [IN] Block length. 136 * @param iv [IN] Initialization vector. 137 */ 138 int32_t CRYPT_AES_CBC_Encrypt(const CRYPT_AES_Key *ctx, const uint8_t *in, uint8_t *out, uint32_t len, uint8_t *iv); 139 140 /** 141 * @ingroup aes 142 * @brief AES cbc decryption 143 * 144 * @param ctx [IN] AES handle, storing keys 145 * @param in [IN] Input ciphertext data. The value is 16 bytes. 146 * @param out [OUT] Output plaintext data. The length is 16 bytes. 147 * @param len [IN] Block length. 148 * @param iv [IN] Initialization vector. 149 */ 150 int32_t CRYPT_AES_CBC_Decrypt(const CRYPT_AES_Key *ctx, const uint8_t *in, uint8_t *out, uint32_t len, uint8_t *iv); 151 #endif /* HITLS_CRYPTO_CBC */ 152 153 #if defined(HITLS_CRYPTO_CTR) || defined(HITLS_CRYPTO_GCM) 154 /** 155 * @ingroup aes 156 * @brief AES ctr encryption 157 * 158 * @param ctx [IN] AES handle, storing keys 159 * @param in [IN] Input plaintext data, 16 bytes. 160 * @param out [OUT] Output ciphertext data. The length is 16 bytes. 161 * @param len [IN] Block length. 162 * @param iv [IN] Initialization vector. 163 */ 164 int32_t CRYPT_AES_CTR_Encrypt(const CRYPT_AES_Key *ctx, const uint8_t *in, uint8_t *out, uint32_t len, uint8_t *iv); 165 #endif 166 167 #ifdef HITLS_CRYPTO_ECB 168 /** 169 * @ingroup aes 170 * @brief AES ecb encryption 171 * 172 * @param ctx [IN] AES handle, storing keys 173 * @param in [IN] Input plaintext data. The length is a multiple of 16 bytes. 174 * @param out [OUT] Output ciphertext data. The length is a multiple of 16 bytes. 175 * @param len [IN] Block length. 176 */ 177 int32_t CRYPT_AES_ECB_Encrypt(const CRYPT_AES_Key *ctx, const uint8_t *in, uint8_t *out, uint32_t len); 178 179 /** 180 * @ingroup aes 181 * @brief AES ecb decryption 182 * 183 * @param ctx [IN] AES handle, storing keys 184 * @param in [IN] Input ciphertext data. The value is 16 bytes. 185 * @param out [OUT] Output plaintext data. The length is 16 bytes. 186 * @param len [IN] Block length. 187 */ 188 int32_t CRYPT_AES_ECB_Decrypt(const CRYPT_AES_Key *ctx, const uint8_t *in, uint8_t *out, uint32_t len); 189 #endif 190 191 #ifdef HITLS_CRYPTO_CFB 192 /** 193 * @brief Decryption in CFB mode 194 * 195 * @param ctx [IN] Mode handle 196 * @param in [IN] Data to be encrypted 197 * @param out [OUT] Encrypted data 198 * @param len [IN] Data length 199 * @param iv [IN] Initial vector 200 * @return Success response: CRYPT_SUCCESS 201 * Returned upon failure: Other error codes. 202 */ 203 int32_t CRYPT_AES_CFB_Decrypt(const CRYPT_AES_Key *ctx, const uint8_t *in, uint8_t *out, uint32_t len, uint8_t *iv); 204 #endif 205 206 #ifdef HITLS_CRYPTO_XTS 207 /** 208 * @ingroup aes 209 * @brief AES xts encryption 210 * 211 * @param ctx [IN] AES key 212 * @param in [IN] Input plaintext. 213 * @param out [OUT] Output ciphertext. 214 * @param len [IN] Input length. The length is guaraenteed to be greater than block-size. 215 * @param tweak [IN/OUT] XTS tweak. 216 */ 217 int32_t CRYPT_AES_XTS_Encrypt(const CRYPT_AES_Key *ctx, const uint8_t *in, 218 uint8_t *out, uint32_t len, const uint8_t *tweak); 219 220 /** 221 * @ingroup aes 222 * @brief AES xts decryption 223 * 224 * @param ctx [IN] AES handle, storing keys 225 * @param in [IN] Input ciphertext data. The value is 16 bytes. 226 * @param out [OUT] Output plaintext data. The length is 16 bytes. 227 * @param len [IN] Block length. 228 * @param t [IN/OUT] XTS tweak. 229 */ 230 int32_t CRYPT_AES_XTS_Decrypt(const CRYPT_AES_Key *ctx, const uint8_t *in, 231 uint8_t *out, uint32_t len, const uint8_t *t); 232 #endif 233 234 /** 235 * @ingroup aes 236 * @brief Delete the AES key information. 237 * 238 * @param ctx [IN] AES handle, storing keys 239 * @return void 240 */ 241 void CRYPT_AES_Clean(CRYPT_AES_Key *ctx); 242 243 #ifdef __cplusplus 244 } 245 #endif // __cplusplus 246 247 #endif // HITLS_CRYPTO_AES 248 249 #endif // CRYPT_AES_H 250