leo hendrickson / Mbed OS example-Ethernet-mbed-Cloud-connect
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers arm_uc_crypto.h Source File

arm_uc_crypto.h

00001 // ----------------------------------------------------------------------------
00002 // Copyright 2016-2017 ARM Ltd.
00003 //
00004 // SPDX-License-Identifier: Apache-2.0
00005 //
00006 // Licensed under the Apache License, Version 2.0 (the "License");
00007 // you may not use this file except in compliance with the License.
00008 // You may obtain a copy of the License at
00009 //
00010 //     http://www.apache.org/licenses/LICENSE-2.0
00011 //
00012 // Unless required by applicable law or agreed to in writing, software
00013 // distributed under the License is distributed on an "AS IS" BASIS,
00014 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00015 // See the License for the specific language governing permissions and
00016 // limitations under the License.
00017 // ----------------------------------------------------------------------------
00018 
00019 #ifndef ARM_UPDATE_CRYPTO_H
00020 #define ARM_UPDATE_CRYPTO_H
00021 
00022 #ifdef __cplusplus
00023 extern "C" {
00024 #endif
00025 
00026 #include "arm_uc_error.h"
00027 #include "arm_uc_types.h"
00028 #include "arm_uc_config.h"
00029 
00030 #if defined(ARM_UC_FEATURE_CRYPTO_PAL) && (ARM_UC_FEATURE_CRYPTO_PAL == 1)
00031 #include "pal.h"
00032 #ifndef palMDHandle_t
00033 #include "pal_Crypto.h"
00034 #endif
00035 
00036 typedef palMDHandle_t arm_uc_mdHandle_t;
00037 typedef palMDType_t arm_uc_mdType_t;
00038 typedef struct arm_uc_cipherHandle_t {
00039     palAesHandle_t aes_context;
00040     uint8_t *aes_iv;
00041 } arm_uc_cipherHandle_t;
00042 
00043 #define ARM_UC_CU_SHA256 PAL_SHA256
00044 
00045 #elif defined(ARM_UC_FEATURE_CRYPTO_MBEDTLS) && (ARM_UC_FEATURE_CRYPTO_MBEDTLS == 1) // ARM_UC_FEATURE_CRYPTO_PAL
00046 
00047 #include "mbedtls/md_internal.h"
00048 #include "mbedtls/sha256.h"
00049 #include "mbedtls/aes.h"
00050 #if ARM_UC_FEATURE_MANIFEST_PUBKEY
00051 #include "mbedtls/x509_crt.h"
00052 #endif
00053 #include "mbedtls/cipher.h"
00054 typedef mbedtls_md_context_t arm_uc_mdHandle_t;
00055 typedef mbedtls_md_type_t arm_uc_mdType_t;
00056 typedef struct arm_uc_cipherHandle_t {
00057     mbedtls_aes_context aes_context;
00058     uint8_t  aes_partial[MBEDTLS_MAX_BLOCK_LENGTH];
00059     uint8_t *aes_iv;
00060     size_t   aes_nc_off;
00061 } arm_uc_cipherHandle_t;
00062 
00063 #define ARM_UC_CU_SHA256 MBEDTLS_MD_SHA256
00064 
00065 #else // ARM_UC_FEATURE_CRYPTO_PAL
00066 
00067 #error Either ARM_UC_FEATURE_CRYPTO_PAL or ARM_UC_FEATURE_CRYPTO_MBEDTLS must be defined.
00068 
00069 #endif // ARM_UC_FEATURE_CRYPTO_PAL
00070 
00071 arm_uc_error_t ARM_UC_cryptoHashSetup(arm_uc_mdHandle_t *h, arm_uc_mdType_t mdType);
00072 arm_uc_error_t ARM_UC_cryptoHashUpdate(arm_uc_mdHandle_t *h, arm_uc_buffer_t *input);
00073 arm_uc_error_t ARM_UC_cryptoHashFinish(arm_uc_mdHandle_t *h, arm_uc_buffer_t *output);
00074 arm_uc_error_t ARM_UC_cryptoDecryptSetup(arm_uc_cipherHandle_t *h, arm_uc_buffer_t *key, arm_uc_buffer_t *iv,
00075                                          int32_t bits);
00076 arm_uc_error_t ARM_UC_cryptoDecryptUpdate(arm_uc_cipherHandle_t *h, const uint8_t *input_ptr, uint32_t input_size,
00077                                           arm_uc_buffer_t *output);
00078 arm_uc_error_t ARM_UC_cryptoDecryptFinish(arm_uc_cipherHandle_t *h, arm_uc_buffer_t *output);
00079 
00080 /**
00081  * @brief Calculate HAMC-SHA256
00082  *
00083  * @param key    buffer struct containing the hmac key
00084  * @param input  buffer struct containing the input data
00085  * @param output buffer struct to cotain output HMAC, it is safe to use the same buffer
00086  *               as input to save memory. The size member of the struct will be set on success.
00087  *
00088  * @return ERR_NONE on success, error code on failure.
00089  */
00090 arm_uc_error_t ARM_UC_cryptoHMACSHA256(arm_uc_buffer_t *key, arm_uc_buffer_t *input, arm_uc_buffer_t *output);
00091 
00092 #ifdef __cplusplus
00093 }
00094 #endif
00095 
00096 #endif // ARM_UPDATE_CRYPTO_H