1 // Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #include <stdlib.h> // standard library
16 #include <string.h>
17
18 #include "esp_sec_api.h"
19
20
21 extern void srand (unsigned int seed);
22 extern int random (void);
23
24 /// Application Security Environment Structure
25 tAPP_SEC_ENV app_sec_env;
26
27
28 /*******************************************************************************
29 **
30 ** Function app_ble_sec_gen_tk
31 **
32 ** Description This function is called to generate the ble tk
33 **
34 ** Returns the generate tk value
35 **
36 *******************************************************************************/
app_ble_sec_gen_tk(void)37 UINT32 app_ble_sec_gen_tk(void)
38 {
39 // Generate a PIN Code (Between 100000 and 999999)
40 return (100000 + (random() % 900000));
41 }
42
43 /*******************************************************************************
44 **
45 ** Function app_ble_sec_gen_ltk
46 **
47 ** Description This function is called to generate the ble ltk
48 **
49 ** Returns NULL
50 **
51 *******************************************************************************/
app_ble_sec_gen_ltk(UINT8 key_size)52 void app_ble_sec_gen_ltk(UINT8 key_size)
53 {
54 // Counter
55 UINT8 i;
56 app_sec_env.key_size = key_size;
57
58 // Randomly generate the LTK and the Random Number
59 for (i = 0; i < RAND_NB_LEN; i++) {
60 app_sec_env.rand_nb.nb[i] = random() % 256;
61 }
62
63 // Randomly generate the end of the LTK
64 for (i = 0; i < SEC_KEY_LEN; i++) {
65 app_sec_env.ltk.key[i] = (((key_size) < (16 - i)) ? 0 : random() % 256);
66 }
67
68 // Randomly generate the EDIV
69 app_sec_env.ediv = random() % 65536;
70 }
71
72
73 /*******************************************************************************
74 **
75 ** Function app_ble_sec_init
76 **
77 ** Description This function is init the security env and function
78 **
79 ** Returns NULL
80 **
81 *******************************************************************************/
app_ble_sec_init(void)82 void app_ble_sec_init(void)
83 {
84 // Reset Security Environment
85 memset(&app_sec_env, 0, sizeof(app_sec_env));
86 }
87
88
89 /*******************************************************************************
90 **
91 ** Function app_ble_security_start
92 **
93 ** Description This function is called by the slave when the seurity start
94 **
95 ** Returns NULL
96 **
97 *******************************************************************************/
app_ble_security_start(void)98 void app_ble_security_start(void)
99 {
100
101 }
102