Kenji Arai / mbed-os_TYBLE16

Dependents:   TYBLE16_simple_data_logger TYBLE16_MP3_Air

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers ieee_802_11.c Source File

ieee_802_11.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/ieee_802_11/ieee_802_11.h"
00025 #include "Service_Libs/hmac/hmac_sha1.h"
00026 
00027 #define TRACE_GROUP "iprf"
00028 
00029 uint16_t ieee_802_11_prf_setup(ieee_802_11_prf_t *prf, uint16_t bits, uint16_t a_len, uint16_t b_len)
00030 {
00031     prf->bits = bits;
00032     prf->a_len = a_len;
00033     prf->b_len = b_len;
00034     return a_len + 1 + b_len + 1; // A string + Y + B string + X (index)
00035 }
00036 
00037 uint8_t *ieee_802_11_prf_get_a_string(ieee_802_11_prf_t *prf, uint8_t *string)
00038 {
00039     (void) prf;
00040     return string;
00041 }
00042 
00043 uint8_t *ieee_802_11_prf_get_b_string(ieee_802_11_prf_t *prf, uint8_t *string)
00044 {
00045     return string + prf->a_len + 1;
00046 }
00047 
00048 uint16_t ieee_802_11_prf_starts(ieee_802_11_prf_t *prf, const uint8_t *key, uint16_t key_len)
00049 {
00050     prf->key = key;
00051     prf->key_len = key_len;
00052 
00053     uint8_t iterations = (prf->bits + 159) / 160;
00054     uint16_t result_len = 160 / 8 * iterations;
00055     return result_len;
00056 }
00057 
00058 void ieee_802_11_prf_update(ieee_802_11_prf_t *prf, uint8_t *string)
00059 {
00060     prf->string = string;
00061     prf->string[prf->a_len] = 0x00; /* Y (0) */
00062 }
00063 
00064 int8_t ieee_802_11_prf_finish(ieee_802_11_prf_t *prf, uint8_t *result)
00065 {
00066     uint16_t string_len = prf->a_len + 1 + prf->b_len + 1;
00067 
00068     for (uint8_t i = 0; i < (prf->bits + 159) / 160; i++) {
00069         prf->string[prf->a_len + 1 + prf->b_len] = i; /* X (index) */
00070         if (hmac_sha1_calc(prf->key, prf->key_len, prf->string, string_len, result, 20) < 0) {
00071             return -1;
00072         }
00073         result += 160 / 8;
00074     }
00075 
00076     return 0;
00077 }