1 /**
2 * \brief AES block cipher, ESP hardware accelerated version, common
3 * Based on mbedTLS FIPS-197 compliant version.
4 *
5 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
6 * Additions Copyright (C) 2016-2017, Espressif Systems (Shanghai) PTE Ltd
7 * SPDX-License-Identifier: Apache-2.0
8 *
9 * Licensed under the Apache License, Version 2.0 (the "License"); you may
10 * not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
12 *
13 * http://www.apache.org/licenses/LICENSE-2.0
14 *
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
17 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
20 *
21 */
22 /*
23 * The AES block cipher was designed by Vincent Rijmen and Joan Daemen.
24 *
25 * http://csrc.nist.gov/encryption/aes/rijndael/Rijndael.pdf
26 * http://csrc.nist.gov/publications/fips/fips197/fips-197.pdf
27 */
28
29 #include "aes/esp_aes_internal.h"
30 #include "mbedtls/aes.h"
31 #include "hal/aes_hal.h"
32 #include "hal/aes_types.h"
33 #include "soc/soc_caps.h"
34
35 #include <string.h>
36 #include "mbedtls/platform.h"
37
38 #if SOC_AES_GDMA
39 #include "esp_aes_dma_priv.h"
40 #endif
41
valid_key_length(const esp_aes_context * ctx)42 bool valid_key_length(const esp_aes_context *ctx)
43 {
44 bool valid_len = (ctx->key_bytes == AES_128_KEY_BYTES) || (ctx->key_bytes == AES_256_KEY_BYTES);
45
46 #if SOC_AES_SUPPORT_AES_192
47 valid_len |= ctx->key_bytes == AES_192_KEY_BYTES;
48 #endif
49
50 return valid_len;
51 }
52
53
esp_aes_init(esp_aes_context * ctx)54 void esp_aes_init( esp_aes_context *ctx )
55 {
56 bzero( ctx, sizeof( esp_aes_context ) );
57 }
58
esp_aes_free(esp_aes_context * ctx)59 void esp_aes_free( esp_aes_context *ctx )
60 {
61 if ( ctx == NULL ) {
62 return;
63 }
64
65 bzero( ctx, sizeof( esp_aes_context ) );
66 }
67
68 /*
69 * AES key schedule (same for encryption or decryption, as hardware handles schedule)
70 *
71 */
esp_aes_setkey(esp_aes_context * ctx,const unsigned char * key,unsigned int keybits)72 int esp_aes_setkey( esp_aes_context *ctx, const unsigned char *key,
73 unsigned int keybits )
74 {
75 #if !SOC_AES_SUPPORT_AES_192
76 if (keybits == 192) {
77 return MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED;
78 }
79 #endif
80 if (keybits != 128 && keybits != 192 && keybits != 256) {
81 return MBEDTLS_ERR_AES_INVALID_KEY_LENGTH;
82 }
83 ctx->key_bytes = keybits / 8;
84 memcpy(ctx->key, key, ctx->key_bytes);
85 ctx->key_in_hardware = 0;
86 return 0;
87 }
88