Skip to content

Commit ebae1d5

Browse files
author
Mika Leppänen
committed
Added HMAC-SHA1, IEEE 802.11 PRF and NIST AES KW libraries
1 parent 2d50887 commit ebae1d5

File tree

22 files changed

+1383
-0
lines changed

22 files changed

+1383
-0
lines changed

source/Service_Libs/hmac/hmac_sha1.c

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/*
2+
* Copyright (c) 2016-2018, Arm Limited and affiliates.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
#include "nsconfig.h"
19+
#include <string.h>
20+
#include "ns_types.h"
21+
#include "ns_list.h"
22+
#include "ns_trace.h"
23+
#include "mbedtls/md.h"
24+
#include "Service_Libs/hmac/hmac_sha1.h"
25+
26+
#define TRACE_GROUP "hmac"
27+
28+
int8_t hmac_sha1_calc(const uint8_t *key, uint16_t key_len, const uint8_t *data, uint16_t data_len, uint8_t *result)
29+
{
30+
// Extensive debug for now, to be disabled later
31+
tr_debug("hmac_sha_1 key %s\n", trace_array(key, key_len));
32+
33+
const uint8_t *print_data = data;
34+
uint16_t print_data_len = data_len;
35+
while (true) {
36+
tr_debug("hmac_sha_1 data %s\n", trace_array(print_data, print_data_len > 32 ? 32 : print_data_len));
37+
if (print_data_len > 32) {
38+
print_data_len -= 32;
39+
print_data += 32;
40+
} else {
41+
break;
42+
}
43+
}
44+
45+
const mbedtls_md_type_t md_type = MBEDTLS_MD_SHA1;
46+
const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type(md_type);
47+
if (md_info == NULL) {
48+
return -1;
49+
}
50+
51+
mbedtls_md_context_t ctx;
52+
53+
mbedtls_md_init(&ctx);
54+
if (mbedtls_md_setup(&ctx, md_info, 1) != 0) {
55+
return -1;
56+
}
57+
if (mbedtls_md_hmac_starts(&ctx, (const unsigned char *) key, key_len) != 0) {
58+
goto error;
59+
}
60+
if (mbedtls_md_hmac_update(&ctx, (const unsigned char *) data, data_len) != 0) {
61+
goto error;
62+
}
63+
if (mbedtls_md_hmac_finish(&ctx, result) != 0) {
64+
goto error;
65+
}
66+
mbedtls_md_free(&ctx);
67+
68+
tr_debug("hmac_sha_1 result %s\n", trace_array(result, 20));
69+
return 0;
70+
71+
error:
72+
mbedtls_md_free(&ctx);
73+
return -1;
74+
}

source/Service_Libs/hmac/hmac_sha1.h

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* Copyright (c) 2016-2018, Arm Limited and affiliates.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
#ifndef HMAC_SHA1_
19+
#define HMAC_SHA1_
20+
21+
/**
22+
* \brief Calculate HMAC-SHA1-160
23+
*
24+
* Calculate HMAC-SHA1-160
25+
*
26+
* \param key pointer to key
27+
* \param key_len key length
28+
* \param data pointer to data
29+
* \param data_len data length
30+
* \param result pointer to result, must be at least 160 bytes
31+
*
32+
* \return < 0 failure
33+
* \return >= 0 success
34+
*
35+
*/
36+
int8_t hmac_sha1_calc(const uint8_t *key, uint16_t key_len, const uint8_t *data, uint16_t data_len, uint8_t *result);
37+
38+
#endif /* HMAC_SHA1_ */
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/*
2+
* Copyright (c) 2016-2018, Arm Limited and affiliates.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
#include "nsconfig.h"
19+
#include <string.h>
20+
#include "ns_types.h"
21+
#include "ns_list.h"
22+
#include "ns_trace.h"
23+
#include "mbedtls/md.h"
24+
#include "Service_Libs/ieee_802_11/ieee_802_11.h"
25+
#include "Service_Libs/hmac/hmac_sha1.h"
26+
27+
#define TRACE_GROUP "ieee_prf"
28+
29+
uint16_t ieee_802_11_prf_setup(ieee_802_11_prf_t *prf, uint16_t bits, uint16_t a_len, uint16_t b_len)
30+
{
31+
prf->bits = bits;
32+
prf->a_len = a_len;
33+
prf->b_len = b_len;
34+
return a_len + 1 + b_len + 1; // A string + Y + B string + X (index)
35+
}
36+
37+
uint8_t *ieee_802_11_prf_get_a_string(ieee_802_11_prf_t *prf, uint8_t *string)
38+
{
39+
(void) prf;
40+
return string;
41+
}
42+
43+
uint8_t *ieee_802_11_prf_get_b_string(ieee_802_11_prf_t *prf, uint8_t *string)
44+
{
45+
return string + prf->a_len + 1;
46+
}
47+
48+
uint16_t ieee_802_11_prf_starts(ieee_802_11_prf_t *prf, const uint8_t *key, uint16_t key_len)
49+
{
50+
prf->key = key;
51+
prf->key_len = key_len;
52+
53+
uint8_t iterations = (prf->bits + 159) / 160;
54+
uint16_t result_len = 160 / 8 * iterations;
55+
return result_len;
56+
}
57+
58+
void ieee_802_11_prf_update(ieee_802_11_prf_t *prf, uint8_t *string)
59+
{
60+
prf->string = string;
61+
prf->string[prf->a_len] = 0x00; /* Y (0) */
62+
}
63+
64+
int8_t ieee_802_11_prf_finish(ieee_802_11_prf_t *prf, uint8_t *result)
65+
{
66+
uint16_t string_len = prf->a_len + 1 + prf->b_len + 1;
67+
68+
for (uint8_t i = 0; i < (prf->bits + 159) / 160; i++) {
69+
prf->string[prf->a_len + 1 + prf->b_len] = i; /* X (index) */
70+
if (hmac_sha1_calc(prf->key, prf->key_len, prf->string, string_len, result) < 0) {
71+
return -1;
72+
}
73+
result += 160 / 8;
74+
}
75+
76+
return 0;
77+
}
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
/*
2+
* Copyright (c) 2016-2018, Arm Limited and affiliates.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
#ifndef IEEE_802_11_
19+
#define IEEE_802_11_
20+
21+
typedef struct {
22+
const uint8_t *key; /**< Key string */
23+
uint8_t *string; /**< Data string (A string + Y + B string + X) */
24+
uint16_t bits; /**< Number of PRF bits */
25+
uint16_t key_len; /**< Key string length */
26+
uint16_t a_len; /**< A string length */
27+
uint16_t b_len; /**< B string length */
28+
} ieee_802_11_prf_t;
29+
30+
/*
31+
From IEEE 802.11 chapter 11.6.1.2 PRF:
32+
33+
In the following, K is a key; A is a unique label for each different purpose
34+
of the PRF; B is a variable-length string; Y is a single octet containing 0;
35+
X is a single octet containing the loop parameter i; and || denotes concatenation:
36+
37+
H-SHA-1(K, A, B, X) ← HMAC-SHA-1(K, A || Y || B || X)
38+
39+
PRF(K, A, B, Len)
40+
for i ← 0 to (Len+159)/160 do
41+
R ← R || H-SHA-1(K, A, B, i)
42+
return L(R, 0, Len)
43+
44+
PRF-128(K, A, B) = PRF(K, A, B, 128)
45+
PRF-192(K, A, B) = PRF(K, A, B, 192)
46+
PRF-256(K, A, B) = PRF(K, A, B, 256)
47+
PRF-384(K, A, B) = PRF(K, A, B, 384)
48+
PRF-512(K, A, B) = PRF(K, A, B, 512)
49+
*/
50+
51+
/**
52+
* \brief Setup IEEE 802.11 PRF
53+
*
54+
* Update is made based on failed and successful message sending
55+
* attempts for a message.
56+
*
57+
* \param prf pointer to PRF data
58+
* \param bits number of bits
59+
* \param a_len A string length
60+
* \param b_len B string length
61+
*
62+
* \return length of the combined string that is input parameter for further functions
63+
* (this is length of the A string + Y + B string + X)
64+
*/
65+
uint16_t ieee_802_11_prf_setup(ieee_802_11_prf_t *prf, uint16_t bits, uint16_t a_len, uint16_t b_len);
66+
67+
/**
68+
* \brief Get A string pointer
69+
*
70+
* Get a pointer to A string start that is used to write A string contents
71+
*
72+
* \param prf pointer to PRF data
73+
* \param string pointer to input string
74+
*
75+
* \return pointer to A string part of the input string
76+
*/
77+
uint8_t *ieee_802_11_prf_get_a_string(ieee_802_11_prf_t *prf, uint8_t *string);
78+
79+
/**
80+
* \brief Get B string pointer
81+
*
82+
* Get a pointer to B string start that is used to write B string contents
83+
*
84+
* \param prf pointer to PRF data
85+
* \param string pointer to input string
86+
*
87+
* \return pointer to B string part of the input string
88+
*/
89+
uint8_t *ieee_802_11_prf_get_b_string(ieee_802_11_prf_t *prf, uint8_t *string);
90+
91+
/**
92+
* \brief Start PRF process
93+
*
94+
* Start PRF process
95+
*
96+
* \param prf pointer to PRF data
97+
* \param key key
98+
* \param key_len key length
99+
*
100+
* \return length of the return string
101+
*/
102+
uint16_t ieee_802_11_prf_starts(ieee_802_11_prf_t *prf, const uint8_t *key, uint16_t key_len);
103+
104+
/**
105+
* \brief Update PRF process
106+
*
107+
* Update PRF process
108+
*
109+
* \param prf pointer to PRF data
110+
* \param string pointer to input string
111+
*
112+
*/
113+
void ieee_802_11_prf_update(ieee_802_11_prf_t *prf, uint8_t *string);
114+
115+
/**
116+
* \brief Finish PRF process
117+
*
118+
* Finish PRF process
119+
*
120+
* \param prf pointer to PRF data
121+
* \param result pointer to result string
122+
*
123+
* \return < 0 failure
124+
* \return >= 0 success
125+
*
126+
*/
127+
int8_t ieee_802_11_prf_finish(ieee_802_11_prf_t *prf, uint8_t *result);
128+
129+
#endif /* IEEE_802_11_*/

0 commit comments

Comments
 (0)