Kenji Arai / mbed-os_TYBLE16

Dependents:   TYBLE16_simple_data_logger TYBLE16_MP3_Air

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers hmac_sha1.c Source File

hmac_sha1.c

00001 /*
00002  * Copyright (c) 2016-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 #include "nsconfig.h"
00019 #include <string.h>
00020 #include "ns_types.h"
00021 #include "ns_list.h"
00022 #include "ns_trace.h"
00023 #include "mbedtls/md.h"
00024 #include "Service_Libs/hmac/hmac_sha1.h"
00025 
00026 #define TRACE_GROUP "hmac"
00027 
00028 int8_t hmac_sha1_calc(const uint8_t *key, uint16_t key_len, const uint8_t *data, uint16_t data_len, uint8_t *result, uint8_t result_len)
00029 {
00030 #ifdef EXTRA_DEBUG_INFO
00031     // Extensive debug for now, to be disabled later
00032     tr_debug("hmac_sha_1 key %s\n", trace_array(key, key_len));
00033 
00034     const uint8_t *print_data = data;
00035     uint16_t print_data_len = data_len;
00036     while (true) {
00037         tr_debug("hmac_sha_1 data %s\n", trace_array(print_data, print_data_len > 32 ? 32 : print_data_len));
00038         if (print_data_len > 32) {
00039             print_data_len -= 32;
00040             print_data += 32;
00041         } else {
00042             break;
00043         }
00044     }
00045 #endif
00046 
00047     const mbedtls_md_type_t md_type = MBEDTLS_MD_SHA1;
00048     const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type(md_type);
00049     if (md_info == NULL) {
00050         return -1;
00051     }
00052 
00053     mbedtls_md_context_t ctx;
00054 
00055     mbedtls_md_init(&ctx);
00056     if (mbedtls_md_setup(&ctx, md_info, 1) != 0) {
00057         return -1;
00058     }
00059     if (mbedtls_md_hmac_starts(&ctx, (const unsigned char *) key, key_len) != 0) {
00060         goto error;
00061     }
00062     if (mbedtls_md_hmac_update(&ctx, (const unsigned char *) data, data_len) != 0) {
00063         goto error;
00064     }
00065 
00066     uint8_t result_value[20];
00067     if (mbedtls_md_hmac_finish(&ctx, result_value) != 0) {
00068         goto error;
00069     }
00070     mbedtls_md_free(&ctx);
00071 
00072     if (result_len > 20) {
00073         result_len = 20;
00074     }
00075 
00076     memcpy(result, result_value, result_len);
00077 
00078 #ifdef EXTRA_DEBUG_INFO
00079     tr_debug("hmac_sha_1 result %s\n", trace_array(result_value, 20));
00080 #endif
00081     return 0;
00082 
00083 error:
00084     mbedtls_md_free(&ctx);
00085     return -1;
00086 }