Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: TYBLE16_simple_data_logger TYBLE16_MP3_Air
aes_mbedtls_adapter.c
00001 /* 00002 * Copyright (c) 2015-2019, Arm Limited and affiliates. 00003 * SPDX-License-Identifier: Apache-2.0 00004 * 00005 * Licensed under the Apache License, Version 2.0 (the "License"); 00006 * you may not use this file except in compliance with the License. 00007 * You may obtain a copy of the License at 00008 * 00009 * http://www.apache.org/licenses/LICENSE-2.0 00010 * 00011 * Unless required by applicable law or agreed to in writing, software 00012 * distributed under the License is distributed on an "AS IS" BASIS, 00013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00014 * See the License for the specific language governing permissions and 00015 * limitations under the License. 00016 */ 00017 /* 00018 * Implementation of platform/arm_hal_aes.h using mbed TLS. 00019 * 00020 * Possible scenarios: 00021 * 00022 * 1) Platform with no hardware AES assist, mbed TLS not in use: 00023 * Use this source file, and let it pull in the minimal mbed TLS code 00024 * contained in aes_mbedtls.c to implement software AES. 00025 * 00026 * 2) Platform with hardware AES assist, mbed TLS not in use: 00027 * Do not use this source file - implement arm_hal_aes.h yourself using 00028 * your AES hardware. Note that you must be able to provide 00029 * ARM_AES_MBEDTLS_CONTEXT_MIN contexts. This may or may not be 1, depending 00030 * on Nanostack config. 00031 * 00032 * 3) Platform without hardware assist, already using (or wanting to use) mbed TLS: 00033 * Use this source file, and define NS_USE_EXTERNAL_MBED_TLS so that 00034 * it uses the external mbed TLS library. That library must be built with 00035 * MBEDTLS_AES_C enabled, and it must be on the include path. 00036 * 00037 * 4) Platform with context-capable hardware assist, already using mbed TLS: 00038 * Use this source file, and define NS_USE_EXTERNAL_MBED_TLS so that 00039 * it uses the external mbed TLS library. That library must be built with 00040 * MBEDTLS_AES_C enabled. Attach your hardware-accelerated AES to mbed TLS 00041 * by defining MBEDTLS_AES_ALT; it will then be used both by users 00042 * of arm_hal_aes.h, and other users of mbed TLS. 00043 */ 00044 00045 /* Get the API we are implementing from libService */ 00046 #include "platform/arm_hal_aes.h" 00047 #include "platform/arm_hal_interrupt.h" 00048 00049 /* Either pull in the external mbed TLS header for its AES functions, or 00050 * pull in our own local cut-down copy of the mbed TLS code. 00051 */ 00052 #ifdef NS_USE_EXTERNAL_MBED_TLS 00053 #include "mbedtls/aes.h" 00054 #else 00055 #include "aes_mbedtls.c" 00056 #endif /* NS_USE_EXTERNAL_MBED_TLS */ 00057 00058 struct arm_aes_context { 00059 mbedtls_aes_context ctx; 00060 bool reserved; 00061 }; 00062 00063 static arm_aes_context_t context_list[ARM_AES_MBEDTLS_CONTEXT_MIN]; 00064 00065 static arm_aes_context_t *mbed_tls_context_get(void) 00066 { 00067 platform_enter_critical(); 00068 for (int i = 0; i < ARM_AES_MBEDTLS_CONTEXT_MIN; i++) { 00069 if (!context_list[i].reserved) { 00070 //Reserve context 00071 context_list[i].reserved = true; 00072 platform_exit_critical(); 00073 return &context_list[i]; 00074 } 00075 } 00076 00077 platform_exit_critical(); 00078 return NULL; 00079 } 00080 00081 arm_aes_context_t *arm_aes_start(const uint8_t key[static 16]) 00082 { 00083 arm_aes_context_t *context = mbed_tls_context_get(); 00084 if (context) { 00085 mbedtls_aes_init(&context->ctx); 00086 if (0 != mbedtls_aes_setkey_enc(&context->ctx, key, 128)) { 00087 return NULL; 00088 } 00089 } 00090 return context; 00091 } 00092 00093 void arm_aes_encrypt(arm_aes_context_t *aes_context, const uint8_t src[static 16], uint8_t dst[static 16]) 00094 { 00095 mbedtls_aes_crypt_ecb(&aes_context->ctx, MBEDTLS_AES_ENCRYPT, src, dst); 00096 } 00097 00098 void arm_aes_finish(arm_aes_context_t *aes_context) 00099 { 00100 mbedtls_aes_free(&aes_context->ctx); 00101 platform_enter_critical(); 00102 aes_context->reserved = false; 00103 platform_exit_critical(); 00104 }
Generated on Tue Jul 12 2022 13:54:00 by
