1 /*
2  * Copyright (C) 2021 Huawei Device Co., Ltd.
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 
16 #include "smp_privacy.h"
17 
18 #include <string.h>
19 
20 #include "log.h"
21 #include "smp.h"
22 #include "smp_cmd.h"
23 #include "smp_common.h"
24 #include "smp_def.h"
25 #include "smp_tool.h"
26 
27 static uint8_t g_smpLocalIRK[SMP_IRK_LEN] = {
28     0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01
29 };
30 static BtAddr g_smpLocalIdentAddr = {
31     .type = 0x01,
32     .addr = {0xFF, 0x01, 0x02, 0x03, 0x04, 0x05},
33 };
34 
35 static void SMP_ResoRpaStep1Exception(const SMP_EncCmd *pEncCmdData);
36 static void SMP_ResoRpaStep1Failed(const SMP_EncCmd *pEncCmdData);
37 static void SMP_ResoRpaStep1Success(const SMP_EncCmd *pEncCmdData);
38 
SMP_GenerateRpaStep1(const SMP_StepParam * param)39 void SMP_GenerateRpaStep1(const SMP_StepParam *param)
40 {
41     if (SMP_ParamIsNULL(param) != SMP_SUCCESS) {
42         return;
43     }
44     SMP_EncData *encData = (SMP_EncData *)param->data;
45     SMP_EncCmd *pEncCmdData = encData->encCmd;
46     uint8_t address[BT_ADDRESS_SIZE] = {0x00};
47     const HciLeEncryptReturnParam *returnParam = encData->encRetParam;
48 
49     LOG_DEBUG("%{public}s.", __FUNCTION__);
50 
51     if (returnParam->status) {
52         LOG_ERROR("returnParam->status = %hhu", returnParam->status);
53         SMP_NotifyCbGenRpa(SMP_GENERATE_RPA_STATUS_FAILED, address);
54         return;
55     }
56 
57     SMP_MemoryReverseCopy(
58         pEncCmdData->address + SMP_RPA_HIGH_BIT_LEN, returnParam->encryptedData, SMP_RPA_HIGH_BIT_LEN);
59     SMP_MemoryReverseCopy(address, pEncCmdData->address, sizeof(address));
60     SMP_NotifyCbGenRpa(SMP_GENERATE_RPA_STATUS_SUCCESS, address);
61 }
62 
SMP_ResolveRpaStep1(const SMP_StepParam * param)63 void SMP_ResolveRpaStep1(const SMP_StepParam *param)
64 {
65     if (SMP_ParamIsNULL(param) != SMP_SUCCESS) {
66         return;
67     }
68     SMP_EncData *encData = (SMP_EncData *)param->data;
69     SMP_EncCmd *pEncCmdData = encData->encCmd;
70     const HciLeEncryptReturnParam *returnParam = encData->encRetParam;
71 
72     LOG_DEBUG("%{public}s", __FUNCTION__);
73 
74     if (returnParam->status) {
75         LOG_ERROR("returnParam->status = %hhu.", returnParam->status);
76         SMP_ResoRpaStep1Exception(pEncCmdData);
77         return;
78     }
79 
80     if (memcmp(returnParam->encryptedData, pEncCmdData->address, BT_ADDRESS_SIZE - SMP_RPA_HIGH_BIT_LEN) != 0x00) {
81         SMP_ResoRpaStep1Failed(pEncCmdData);
82     } else {
83         SMP_ResoRpaStep1Success(pEncCmdData);
84     }
85 }
86 
SMP_ResoRpaStep1Exception(const SMP_EncCmd * pEncCmdData)87 static void SMP_ResoRpaStep1Exception(const SMP_EncCmd *pEncCmdData)
88 {
89     SMP_NotifyCbResoRpa(SMP_RESOLVE_RPA_STATUS_FAILED, false, pEncCmdData->address, pEncCmdData->key);
90 }
91 
SMP_ResoRpaStep1Failed(const SMP_EncCmd * pEncCmdData)92 static void SMP_ResoRpaStep1Failed(const SMP_EncCmd *pEncCmdData)
93 {
94     LOG_INFO("Resolve RPA failed.");
95     SMP_NotifyCbResoRpa(SMP_RESOLVE_RPA_STATUS_SUCCESS, false, pEncCmdData->address, pEncCmdData->key);
96 }
97 
SMP_ResoRpaStep1Success(const SMP_EncCmd * pEncCmdData)98 static void SMP_ResoRpaStep1Success(const SMP_EncCmd *pEncCmdData)
99 {
100     LOG_INFO("Resolve RPA successfully.");
101     SMP_NotifyCbResoRpa(SMP_RESOLVE_RPA_STATUS_SUCCESS, true, pEncCmdData->address, pEncCmdData->key);
102 }
103 
SMP_SetLocalIrk(const uint8_t * irk,uint8_t size)104 void SMP_SetLocalIrk(const uint8_t *irk, uint8_t size)
105 {
106     (void)memcpy_s(g_smpLocalIRK, SMP_IRK_LEN, irk, size);
107 }
108 
SMP_SetLocalIdentAddr(const BtAddr * addr)109 void SMP_SetLocalIdentAddr(const BtAddr *addr)
110 {
111     g_smpLocalIdentAddr.type = addr->type;
112     SMP_MemoryReverseCopy(g_smpLocalIdentAddr.addr, addr->addr, sizeof(g_smpLocalIdentAddr.addr));
113 }
114 
SMP_GetLocalIrk(uint8_t * irk,uint8_t size)115 void SMP_GetLocalIrk(uint8_t *irk, uint8_t size)
116 {
117     (void)memcpy_s(irk, SMP_IRK_LEN, g_smpLocalIRK, size);
118 }
119 
SMP_GetLocalIdentAddr(BtAddr * addr)120 void SMP_GetLocalIdentAddr(BtAddr *addr)
121 {
122     (void)memcpy_s(addr, SMP_IRK_LEN, &g_smpLocalIdentAddr, sizeof(BtAddr));
123 }