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

« Back to documentation index

Show/hide line numbers arm_uc_crypto_hmac_mbedtls.c Source File

arm_uc_crypto_hmac_mbedtls.c

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 #include "update-client-common/arm_uc_types.h"
00020 #include "update-client-common/arm_uc_error.h"
00021 #include <string.h>
00022 
00023 #include "arm_uc_config.h"
00024 #if defined(ARM_UC_FEATURE_CRYPTO_MBEDTLS) && (ARM_UC_FEATURE_CRYPTO_MBEDTLS == 1)
00025 #include "mbedtls/md_internal.h"
00026 
00027 arm_uc_error_t ARM_UC_cryptoHMACSHA256(arm_uc_buffer_t *key,
00028                                        arm_uc_buffer_t *input,
00029                                        arm_uc_buffer_t *output)
00030 {
00031     arm_uc_error_t result = (arm_uc_error_t) { ARM_UC_CU_ERR_INVALID_PARAMETER };
00032 
00033     const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type(MBEDTLS_MD_SHA256);
00034     if (md_info != NULL) {
00035         int8_t rv = mbedtls_md_hmac(md_info,
00036                                     key->ptr, key->size,
00037                                     input->ptr, input->size,
00038                                     output->ptr);
00039         if (rv == 0) {
00040             output->size = ARM_UC_SHA256_SIZE;
00041             result = (arm_uc_error_t) { ERR_NONE };
00042         }
00043     }
00044 
00045     return result;
00046 }
00047 
00048 #endif