119e95205Sopenharmony_ci/* 219e95205Sopenharmony_ci * Copyright (C) 2021 Huawei Device Co., Ltd. 319e95205Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 419e95205Sopenharmony_ci * you may not use this file except in compliance with the License. 519e95205Sopenharmony_ci * You may obtain a copy of the License at 619e95205Sopenharmony_ci * 719e95205Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 819e95205Sopenharmony_ci * 919e95205Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 1019e95205Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 1119e95205Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 1219e95205Sopenharmony_ci * See the License for the specific language governing permissions and 1319e95205Sopenharmony_ci * limitations under the License. 1419e95205Sopenharmony_ci */ 1519e95205Sopenharmony_ci 1619e95205Sopenharmony_ci#include "smp.h" 1719e95205Sopenharmony_ci 1819e95205Sopenharmony_ci#include <stdlib.h> 1919e95205Sopenharmony_ci#include <string.h> 2019e95205Sopenharmony_ci 2119e95205Sopenharmony_ci#include "log.h" 2219e95205Sopenharmony_ci 2319e95205Sopenharmony_ci#include "btstack.h" 2419e95205Sopenharmony_ci#include "l2cap_le_if.h" 2519e95205Sopenharmony_ci#include "packet.h" 2619e95205Sopenharmony_ci 2719e95205Sopenharmony_ci#include "platform/include/alarm.h" 2819e95205Sopenharmony_ci#include "platform/include/allocator.h" 2919e95205Sopenharmony_ci#include "platform/include/event.h" 3019e95205Sopenharmony_ci#include "platform/include/list.h" 3119e95205Sopenharmony_ci#include "platform/include/module.h" 3219e95205Sopenharmony_ci#include "platform/include/mutex.h" 3319e95205Sopenharmony_ci#include "platform/include/random.h" 3419e95205Sopenharmony_ci 3519e95205Sopenharmony_ci#include "btm/btm_thread.h" 3619e95205Sopenharmony_ci#include "hci/hci.h" 3719e95205Sopenharmony_ci 3819e95205Sopenharmony_ci#include "smp_aes_encryption.h" 3919e95205Sopenharmony_ci#include "smp_cmd.h" 4019e95205Sopenharmony_ci#include "smp_common.h" 4119e95205Sopenharmony_ci#include "smp_def.h" 4219e95205Sopenharmony_ci#include "smp_hci_event.h" 4319e95205Sopenharmony_ci#include "smp_legacy.h" 4419e95205Sopenharmony_ci#include "smp_oob.h" 4519e95205Sopenharmony_ci#include "smp_privacy.h" 4619e95205Sopenharmony_ci#include "smp_receive.h" 4719e95205Sopenharmony_ci#include "smp_sc_accepter.h" 4819e95205Sopenharmony_ci#include "smp_sc_initiator.h" 4919e95205Sopenharmony_ci#include "smp_send.h" 5019e95205Sopenharmony_ci#include "smp_tool.h" 5119e95205Sopenharmony_ci 5219e95205Sopenharmony_cistatic const uint8_t SALT[SMP_ENCRYPT_KEY_LEN] = { 5319e95205Sopenharmony_ci 0x6C, 0x88, 0x83, 0x91, 0xAA, 0xF5, 0xA5, 0x38, 0x60, 0x37, 0x0B, 0xDB, 0x5A, 0x60, 0x83, 0xBE 5419e95205Sopenharmony_ci}; // The key of an encryption algorithm. 5519e95205Sopenharmony_ci 5619e95205Sopenharmony_cistatic SMP_PairMng g_smpPairMng = {0x00}; 5719e95205Sopenharmony_cistatic SMP_Callback_t g_smpCallBack = {0x00}; 5819e95205Sopenharmony_cistatic bool g_smpSecureConnOnlyMode = false; 5919e95205Sopenharmony_ci 6019e95205Sopenharmony_cistatic int SMP_AuthReqReplyProcessMaster( 6119e95205Sopenharmony_ci bool accept, uint8_t rejectReason, uint8_t pairMethod, const uint8_t *entryValue); 6219e95205Sopenharmony_cistatic int SMP_AuthReqReplyJudgeExceptionMaster(bool accept, uint8_t rejectReason); 6319e95205Sopenharmony_cistatic int SMP_AuthReqReplyStepTransMaster(uint8_t pairMethod); 6419e95205Sopenharmony_cistatic int SMP_AuthReqReplyProcessSlave( 6519e95205Sopenharmony_ci bool accept, uint8_t rejectReason, uint8_t pairMethod, const uint8_t *entryValue); 6619e95205Sopenharmony_cistatic int SMP_AuthReqReplyJudgeExceptionSlave(bool accept, uint8_t rejectReason); 6719e95205Sopenharmony_cistatic int SMP_AuthReqReplyStepTransSlave(uint8_t pairMethod); 6819e95205Sopenharmony_cistatic int SMP_AuthReqReplyNumericSlave(); 6919e95205Sopenharmony_cistatic int SMP_AuthReqReplyPasskeyEntrySlave(); 7019e95205Sopenharmony_cistatic int SMP_AuthReqReplyOobSlave(); 7119e95205Sopenharmony_cistatic void SMP_AuthReqReplyObtainEntryValue(uint8_t pairMethod, const uint8_t *entryValue); 7219e95205Sopenharmony_cistatic int SMP_PairReqReplyJudgeException( 7319e95205Sopenharmony_ci uint16_t handle, bool accept, uint8_t rejectReason, const SMP_PairParam *param); 7419e95205Sopenharmony_cistatic int SMP_PairReqReplyLegacyPair(); 7519e95205Sopenharmony_cistatic int SMP_PairReqReplyScPair(); 7619e95205Sopenharmony_cistatic int SMP_PairReqReplyScPairOob(); 7719e95205Sopenharmony_cistatic int SMP_PairRspReplyJudgeException(uint16_t handle, bool accept, uint8_t rejectReason); 7819e95205Sopenharmony_cistatic int SMP_PairRspReplyLegacyPair(); 7919e95205Sopenharmony_cistatic int SMP_PairRspReplyScPair(); 8019e95205Sopenharmony_cistatic int SMP_PairRspReplyScPairOob(); 8119e95205Sopenharmony_cistatic int SMP_LtkReqReplyNormal(uint16_t handle, bool accept, const uint8_t *key); 8219e95205Sopenharmony_cistatic int SMP_LtkReqReplyException(uint16_t handle); 8319e95205Sopenharmony_cistatic void SMP_GenPairRetNormal(uint16_t handle, uint8_t status); 8419e95205Sopenharmony_cistatic void SMP_GenPairRetException(uint16_t handle, uint8_t status, uint8_t failedReason, Alarm *cancelTimer); 8519e95205Sopenharmony_cistatic void SMP_L2cifLeRegisterFixChannelCallback(uint16_t cid, int result); 8619e95205Sopenharmony_cistatic void SMP_L2cifLeDeregisterFixChannelCallback(uint16_t cid, int result); 8719e95205Sopenharmony_ci 8819e95205Sopenharmony_cistatic void SMP_StartupTask(void *context); 8919e95205Sopenharmony_cistatic void SMP_ShutdownTask(void *context); 9019e95205Sopenharmony_cistatic void SMP_SetIrkTask(void *context); 9119e95205Sopenharmony_cistatic void SMP_SetIdentAddrTask(void *context); 9219e95205Sopenharmony_cistatic void SMP_AsyncResoRpaTask(void *context); 9319e95205Sopenharmony_cistatic void SMP_GenRpaTask(void *context); 9419e95205Sopenharmony_cistatic void SMP_SetSecConnOnlyModeTask(void *context); 9519e95205Sopenharmony_cistatic void SMP_SendSecReqToRemoteTask(void *context); 9619e95205Sopenharmony_cistatic void SMP_GenSignTask(void *context); 9719e95205Sopenharmony_cistatic void SMP_StartEncTask(void *context); 9819e95205Sopenharmony_cistatic void SMP_StartPairTask(void *context); 9919e95205Sopenharmony_cistatic void SMP_AuthReqReplyTask(void *context); 10019e95205Sopenharmony_cistatic void SMP_RemotePairReqReplyTask(void *context); 10119e95205Sopenharmony_cistatic void SMP_RemotePairRspReplyTask(void *context); 10219e95205Sopenharmony_cistatic void SMP_RemoteSecReqReplyTask(void *context); 10319e95205Sopenharmony_cistatic void SMP_LongTermKeyReqReplyTask(void *context); 10419e95205Sopenharmony_cistatic void SMP_CancelPairTask(void *context); 10519e95205Sopenharmony_cistatic void SMP_RegCbTask(void *context); 10619e95205Sopenharmony_cistatic void SMP_UnregCbTask(void *context); 10719e95205Sopenharmony_cistatic void SMP_GenScOobDataTask(void *context); 10819e95205Sopenharmony_cistatic void SMP_PairTimeoutTask(void *context); 10919e95205Sopenharmony_ci 11019e95205Sopenharmony_cistatic HciEventCallbacks g_smpHciEventCallbacks = { 11119e95205Sopenharmony_ci .leRandComplete = SMP_OnLERandComplete, 11219e95205Sopenharmony_ci .leEncryptComplete = SMP_OnLEEncryptComplete, 11319e95205Sopenharmony_ci .encryptionChange = SMP_OnLEEncryptionChange, 11419e95205Sopenharmony_ci .encryptionKeyRefreshComplete = SMP_OnLEEncryptionKeyRefreshComplete, 11519e95205Sopenharmony_ci .leLongTermKeyRequest = SMP_OnLELongTermKeyRequest, 11619e95205Sopenharmony_ci .leLongTermKeyRequestReplyComplete = SMP_OnLELongTermKeyRequestReplyComplete, 11719e95205Sopenharmony_ci .leReadLocalP256PublicKeyComplete = SMP_OnLEReadLocalP256PublicKeyComplete, 11819e95205Sopenharmony_ci .leGenerateDHKeyComplete = SMP_OnLEGenerateDHKeyComplete, 11919e95205Sopenharmony_ci}; 12019e95205Sopenharmony_ci 12119e95205Sopenharmony_cistatic void SMP_Initialize(int traceLevel) 12219e95205Sopenharmony_ci{ 12319e95205Sopenharmony_ci LOG_INFO("%{public}s", __FUNCTION__); 12419e95205Sopenharmony_ci} 12519e95205Sopenharmony_ci 12619e95205Sopenharmony_cistatic void SMP_Finalize(void) 12719e95205Sopenharmony_ci{ 12819e95205Sopenharmony_ci LOG_INFO("%{public}s", __FUNCTION__); 12919e95205Sopenharmony_ci} 13019e95205Sopenharmony_ci 13119e95205Sopenharmony_cistatic void SMP_Startup() 13219e95205Sopenharmony_ci{ 13319e95205Sopenharmony_ci int ret; 13419e95205Sopenharmony_ci LOG_INFO("%{public}s", __FUNCTION__); 13519e95205Sopenharmony_ci Event *ctx = EventCreate(true); 13619e95205Sopenharmony_ci ret = BTM_CreateProcessingQueue(PROCESSING_QUEUE_ID_SMP, BTM_PROCESSING_QUEUE_SIZE_DEFAULT); 13719e95205Sopenharmony_ci if (ret != SMP_SUCCESS) { 13819e95205Sopenharmony_ci EventDelete(ctx); 13919e95205Sopenharmony_ci return; 14019e95205Sopenharmony_ci } 14119e95205Sopenharmony_ci ret = BTM_RunTaskInProcessingQueue(PROCESSING_QUEUE_ID_SMP, SMP_StartupTask, ctx); 14219e95205Sopenharmony_ci if (ret != SMP_SUCCESS) { 14319e95205Sopenharmony_ci EventDelete(ctx); 14419e95205Sopenharmony_ci return; 14519e95205Sopenharmony_ci } 14619e95205Sopenharmony_ci EventWait(ctx, SMP_INIT_WAIT_TIME); 14719e95205Sopenharmony_ci EventDelete(ctx); 14819e95205Sopenharmony_ci} 14919e95205Sopenharmony_ci 15019e95205Sopenharmony_cistatic void SMP_StartupTask(void *context) 15119e95205Sopenharmony_ci{ 15219e95205Sopenharmony_ci Event *ctx = context; 15319e95205Sopenharmony_ci LOG_DEBUG("%{public}s", __FUNCTION__); 15419e95205Sopenharmony_ci L2capLeFixChannel channel; 15519e95205Sopenharmony_ci g_smpSecureConnOnlyMode = false; 15619e95205Sopenharmony_ci SMP_ClearScOobData(true); 15719e95205Sopenharmony_ci SMP_ClearPairState(&g_smpPairMng); 15819e95205Sopenharmony_ci SMP_SetEncCmdList(SMP_ListCreate(SMP_FreeEncCmd)); 15919e95205Sopenharmony_ci channel.cid = L2CAP_LE_SMP_CHANNEL; 16019e95205Sopenharmony_ci channel.leConnected = SMP_Connected; 16119e95205Sopenharmony_ci channel.leDisconnected = SMP_Disconnected; 16219e95205Sopenharmony_ci channel.recvLeData = SMP_ReceiveData; 16319e95205Sopenharmony_ci int ret = L2CIF_LeRegisterFixChannel(L2CAP_LE_SMP_CHANNEL, &channel, SMP_L2cifLeRegisterFixChannelCallback); 16419e95205Sopenharmony_ci if (ret != SMP_SUCCESS) { 16519e95205Sopenharmony_ci LOG_ERROR("RegisterFixChannel error %{public}d", ret); 16619e95205Sopenharmony_ci } 16719e95205Sopenharmony_ci HCI_RegisterEventCallbacks(&g_smpHciEventCallbacks); 16819e95205Sopenharmony_ci if (ctx != NULL) { 16919e95205Sopenharmony_ci EventSet(ctx); 17019e95205Sopenharmony_ci } 17119e95205Sopenharmony_ci} 17219e95205Sopenharmony_ci 17319e95205Sopenharmony_cistatic void SMP_Shutdown() 17419e95205Sopenharmony_ci{ 17519e95205Sopenharmony_ci int ret; 17619e95205Sopenharmony_ci LOG_INFO("%{public}s", __FUNCTION__); 17719e95205Sopenharmony_ci Event *ctx = EventCreate(true); 17819e95205Sopenharmony_ci ret = BTM_RunTaskInProcessingQueue(PROCESSING_QUEUE_ID_SMP, SMP_ShutdownTask, ctx); 17919e95205Sopenharmony_ci if (ret != SMP_SUCCESS) { 18019e95205Sopenharmony_ci EventDelete(ctx); 18119e95205Sopenharmony_ci return; 18219e95205Sopenharmony_ci } 18319e95205Sopenharmony_ci EventWait(ctx, SMP_INIT_WAIT_TIME); 18419e95205Sopenharmony_ci EventDelete(ctx); 18519e95205Sopenharmony_ci} 18619e95205Sopenharmony_ci 18719e95205Sopenharmony_cistatic void SMP_ShutdownTask(void *context) 18819e95205Sopenharmony_ci{ 18919e95205Sopenharmony_ci Event *ctx = context; 19019e95205Sopenharmony_ci LOG_DEBUG("%{public}s", __FUNCTION__); 19119e95205Sopenharmony_ci L2CIF_LeDeregisterFixChannel(L2CAP_LE_SMP_CHANNEL, SMP_L2cifLeDeregisterFixChannelCallback); 19219e95205Sopenharmony_ci HCI_DeregisterEventCallbacks(&g_smpHciEventCallbacks); 19319e95205Sopenharmony_ci g_smpSecureConnOnlyMode = false; 19419e95205Sopenharmony_ci SMP_ClearScOobData(true); 19519e95205Sopenharmony_ci SMP_ClearPairState(&g_smpPairMng); 19619e95205Sopenharmony_ci SMP_ListDelete(SMP_GetEncCmdList()); 19719e95205Sopenharmony_ci SMP_SetEncCmdList(NULL); 19819e95205Sopenharmony_ci BTM_DeleteProcessingQueue(PROCESSING_QUEUE_ID_SMP); 19919e95205Sopenharmony_ci if (ctx != NULL) { 20019e95205Sopenharmony_ci EventSet(ctx); 20119e95205Sopenharmony_ci } 20219e95205Sopenharmony_ci} 20319e95205Sopenharmony_ci 20419e95205Sopenharmony_ciint SMP_RegisterCallback(const SMP_Callback_t *cb) 20519e95205Sopenharmony_ci{ 20619e95205Sopenharmony_ci LOG_INFO("%{public}s", __FUNCTION__); 20719e95205Sopenharmony_ci SMP_RegCbTask_t *ctx = MEM_MALLOC.alloc(sizeof(SMP_RegCbTask_t)); 20819e95205Sopenharmony_ci if (ctx == NULL) { 20919e95205Sopenharmony_ci LOG_ERROR("%{public}s: Alloc error.", __FUNCTION__); 21019e95205Sopenharmony_ci return SMP_ERR_OUT_OF_RES; 21119e95205Sopenharmony_ci } 21219e95205Sopenharmony_ci (void)memset_s(ctx, sizeof(SMP_RegCbTask_t), 0x00, sizeof(SMP_RegCbTask_t)); 21319e95205Sopenharmony_ci (void)memcpy_s(&ctx->cb, sizeof(SMP_Callback_t), cb, sizeof(ctx->cb)); 21419e95205Sopenharmony_ci int ret = BTM_RunTaskInProcessingQueue(PROCESSING_QUEUE_ID_SMP, SMP_RegCbTask, (void *)ctx); 21519e95205Sopenharmony_ci if (ret != SMP_SUCCESS) { 21619e95205Sopenharmony_ci MEM_MALLOC.free(ctx); 21719e95205Sopenharmony_ci return ret; 21819e95205Sopenharmony_ci } 21919e95205Sopenharmony_ci return ret; 22019e95205Sopenharmony_ci} 22119e95205Sopenharmony_ci 22219e95205Sopenharmony_cistatic void SMP_RegCbTask(void *context) 22319e95205Sopenharmony_ci{ 22419e95205Sopenharmony_ci SMP_RegCbTask_t *param = (SMP_RegCbTask_t *)context; 22519e95205Sopenharmony_ci if (param->cb.SMP_CallbackAuthenticationRequest != NULL) { 22619e95205Sopenharmony_ci g_smpCallBack.SMP_CallbackAuthenticationRequest = param->cb.SMP_CallbackAuthenticationRequest; 22719e95205Sopenharmony_ci } 22819e95205Sopenharmony_ci if (param->cb.SMP_CallbackEncryptionComplete != NULL) { 22919e95205Sopenharmony_ci g_smpCallBack.SMP_CallbackEncryptionComplete = param->cb.SMP_CallbackEncryptionComplete; 23019e95205Sopenharmony_ci } 23119e95205Sopenharmony_ci if (param->cb.SMP_CallbackLongTermKeyRequest != NULL) { 23219e95205Sopenharmony_ci g_smpCallBack.SMP_CallbackLongTermKeyRequest = param->cb.SMP_CallbackLongTermKeyRequest; 23319e95205Sopenharmony_ci } 23419e95205Sopenharmony_ci if (param->cb.SMP_CallbackPairResult != NULL) { 23519e95205Sopenharmony_ci g_smpCallBack.SMP_CallbackPairResult = param->cb.SMP_CallbackPairResult; 23619e95205Sopenharmony_ci } 23719e95205Sopenharmony_ci if (param->cb.SMP_CallbackRemotePairRequest != NULL) { 23819e95205Sopenharmony_ci g_smpCallBack.SMP_CallbackRemotePairRequest = param->cb.SMP_CallbackRemotePairRequest; 23919e95205Sopenharmony_ci } 24019e95205Sopenharmony_ci if (param->cb.SMP_CallbackRemotePairResponse != NULL) { 24119e95205Sopenharmony_ci g_smpCallBack.SMP_CallbackRemotePairResponse = param->cb.SMP_CallbackRemotePairResponse; 24219e95205Sopenharmony_ci } 24319e95205Sopenharmony_ci if (param->cb.SMP_CallbackRemoteSecurityRequest != NULL) { 24419e95205Sopenharmony_ci g_smpCallBack.SMP_CallbackRemoteSecurityRequest = param->cb.SMP_CallbackRemoteSecurityRequest; 24519e95205Sopenharmony_ci } 24619e95205Sopenharmony_ci if (param->cb.SMP_CallbackGenerateSignatureResult != NULL) { 24719e95205Sopenharmony_ci g_smpCallBack.SMP_CallbackGenerateSignatureResult = param->cb.SMP_CallbackGenerateSignatureResult; 24819e95205Sopenharmony_ci } 24919e95205Sopenharmony_ci if (param->cb.SMP_CallbackGenerateRPAResult != NULL) { 25019e95205Sopenharmony_ci g_smpCallBack.SMP_CallbackGenerateRPAResult = param->cb.SMP_CallbackGenerateRPAResult; 25119e95205Sopenharmony_ci } 25219e95205Sopenharmony_ci if (param->cb.SMP_CallbackResolveRPAResult != NULL) { 25319e95205Sopenharmony_ci g_smpCallBack.SMP_CallbackResolveRPAResult = param->cb.SMP_CallbackResolveRPAResult; 25419e95205Sopenharmony_ci } 25519e95205Sopenharmony_ci if (param->cb.SMP_CallbackGenerateScOobDataResult != NULL) { 25619e95205Sopenharmony_ci g_smpCallBack.SMP_CallbackGenerateScOobDataResult = param->cb.SMP_CallbackGenerateScOobDataResult; 25719e95205Sopenharmony_ci } 25819e95205Sopenharmony_ci MEM_MALLOC.free(param); 25919e95205Sopenharmony_ci} 26019e95205Sopenharmony_ci 26119e95205Sopenharmony_ciint SMP_UnregisterCallback() 26219e95205Sopenharmony_ci{ 26319e95205Sopenharmony_ci LOG_INFO("%{public}s", __FUNCTION__); 26419e95205Sopenharmony_ci int ret = BTM_RunTaskInProcessingQueue(PROCESSING_QUEUE_ID_SMP, SMP_UnregCbTask, NULL); 26519e95205Sopenharmony_ci if (ret != SMP_SUCCESS) { 26619e95205Sopenharmony_ci return ret; 26719e95205Sopenharmony_ci } 26819e95205Sopenharmony_ci return ret; 26919e95205Sopenharmony_ci} 27019e95205Sopenharmony_ci 27119e95205Sopenharmony_cistatic void SMP_UnregCbTask(void *context) 27219e95205Sopenharmony_ci{ 27319e95205Sopenharmony_ci g_smpCallBack.SMP_CallbackAuthenticationRequest = NULL; 27419e95205Sopenharmony_ci g_smpCallBack.SMP_CallbackEncryptionComplete = NULL; 27519e95205Sopenharmony_ci g_smpCallBack.SMP_CallbackLongTermKeyRequest = NULL; 27619e95205Sopenharmony_ci g_smpCallBack.SMP_CallbackPairResult = NULL; 27719e95205Sopenharmony_ci g_smpCallBack.SMP_CallbackRemotePairRequest = NULL; 27819e95205Sopenharmony_ci g_smpCallBack.SMP_CallbackRemotePairResponse = NULL; 27919e95205Sopenharmony_ci g_smpCallBack.SMP_CallbackRemoteSecurityRequest = NULL; 28019e95205Sopenharmony_ci g_smpCallBack.SMP_CallbackGenerateSignatureResult = NULL; 28119e95205Sopenharmony_ci g_smpCallBack.SMP_CallbackGenerateRPAResult = NULL; 28219e95205Sopenharmony_ci g_smpCallBack.SMP_CallbackResolveRPAResult = NULL; 28319e95205Sopenharmony_ci g_smpCallBack.SMP_CallbackGenerateScOobDataResult = NULL; 28419e95205Sopenharmony_ci (void)context; 28519e95205Sopenharmony_ci} 28619e95205Sopenharmony_ci 28719e95205Sopenharmony_ciint SMP_StartPair(uint16_t handle, const BtAddr *localAddr, const BtAddr *peerAddr, const SMP_PairParam *param) 28819e95205Sopenharmony_ci{ 28919e95205Sopenharmony_ci LOG_INFO("%{public}s", __FUNCTION__); 29019e95205Sopenharmony_ci SMP_StartPairTask_t *ctx = MEM_MALLOC.alloc(sizeof(SMP_StartPairTask_t)); 29119e95205Sopenharmony_ci if (ctx == NULL) { 29219e95205Sopenharmony_ci LOG_ERROR("%{public}s: Alloc error.", __FUNCTION__); 29319e95205Sopenharmony_ci return SMP_ERR_OUT_OF_RES; 29419e95205Sopenharmony_ci } 29519e95205Sopenharmony_ci (void)memset_s(ctx, sizeof(SMP_StartPairTask_t), 0x00, sizeof(SMP_StartPairTask_t)); 29619e95205Sopenharmony_ci ctx->handle = handle; 29719e95205Sopenharmony_ci (void)memcpy_s(&ctx->localAddr, sizeof(BtAddr), localAddr, sizeof(ctx->localAddr)); 29819e95205Sopenharmony_ci (void)memcpy_s(&ctx->peerAddr, sizeof(BtAddr), peerAddr, sizeof(ctx->peerAddr)); 29919e95205Sopenharmony_ci (void)memcpy_s(&ctx->param, sizeof(SMP_PairParam), param, sizeof(ctx->param)); 30019e95205Sopenharmony_ci int ret = BTM_RunTaskInProcessingQueue(PROCESSING_QUEUE_ID_SMP, SMP_StartPairTask, (void *)ctx); 30119e95205Sopenharmony_ci if (ret != SMP_SUCCESS) { 30219e95205Sopenharmony_ci MEM_MALLOC.free(ctx); 30319e95205Sopenharmony_ci return ret; 30419e95205Sopenharmony_ci } 30519e95205Sopenharmony_ci return ret; 30619e95205Sopenharmony_ci} 30719e95205Sopenharmony_ci 30819e95205Sopenharmony_cistatic void SMP_StartPairTask(void *context) 30919e95205Sopenharmony_ci{ 31019e95205Sopenharmony_ci int ret = SMP_SUCCESS; 31119e95205Sopenharmony_ci SMP_StartPairTask_t *param = (SMP_StartPairTask_t *)context; 31219e95205Sopenharmony_ci LOG_DEBUG("%{public}s", __FUNCTION__); 31319e95205Sopenharmony_ci if (g_smpPairMng.state == SMP_STATE_PAIRING) { 31419e95205Sopenharmony_ci LOG_ERROR("It's already pairing state."); 31519e95205Sopenharmony_ci } else if (SMP_GetScOobMng()->state == SMP_STATE_SC_OOB_DATA_GENERATING) { 31619e95205Sopenharmony_ci LOG_ERROR("It's already SC OOB data generating state."); 31719e95205Sopenharmony_ci ret = SMP_ERR_INVAL_STATE; 31819e95205Sopenharmony_ci } else if (SMP_GetSecureConnOnlyMode() && (!(param->param.authReq & SMP_AUTH_REQ_BIT_SC))) { 31919e95205Sopenharmony_ci LOG_ERROR("SC bit is not set in authReq."); 32019e95205Sopenharmony_ci ret = SMP_ERR_INVAL_PARAM; 32119e95205Sopenharmony_ci } else { 32219e95205Sopenharmony_ci (void)memset_s(&g_smpPairMng, sizeof(g_smpPairMng), 0x00, sizeof(g_smpPairMng)); 32319e95205Sopenharmony_ci g_smpPairMng.state = SMP_STATE_PAIRING; 32419e95205Sopenharmony_ci g_smpPairMng.handle = param->handle; 32519e95205Sopenharmony_ci g_smpPairMng.role = SMP_ROLE_MASTER; 32619e95205Sopenharmony_ci g_smpPairMng.alarm = AlarmCreate("", false); 32719e95205Sopenharmony_ci g_smpPairMng.local.addr.type = param->localAddr.type; 32819e95205Sopenharmony_ci g_smpPairMng.peer.addr.type = param->peerAddr.type; 32919e95205Sopenharmony_ci SMP_MemoryReverseCopy( 33019e95205Sopenharmony_ci g_smpPairMng.local.addr.addr, param->localAddr.addr, sizeof(g_smpPairMng.local.addr.addr)); 33119e95205Sopenharmony_ci SMP_MemoryReverseCopy(g_smpPairMng.peer.addr.addr, param->peerAddr.addr, sizeof(g_smpPairMng.peer.addr.addr)); 33219e95205Sopenharmony_ci (void)memcpy_s(&g_smpPairMng.local.pairParam, sizeof(SMP_PairParam), ¶m->param, sizeof(SMP_PairParam)); 33319e95205Sopenharmony_ci g_smpPairMng.step = SMP_LEGACY_PAIR_MASTER_STEP_1; 33419e95205Sopenharmony_ci LOG_DEBUG("SMP_LEGACY_PAIR_MASTER_STEP_1 started."); 33519e95205Sopenharmony_ci AlarmSet(g_smpPairMng.alarm, SMP_PAIR_WAIT_TIME, SMP_PairTimeout, NULL); 33619e95205Sopenharmony_ci ret = SMP_SendPairingRequest(param->handle, ¶m->param, SMP_SendDataCallback); 33719e95205Sopenharmony_ci if (ret != SMP_SUCCESS) { 33819e95205Sopenharmony_ci LOG_ERROR("Send pairing request failed."); 33919e95205Sopenharmony_ci AlarmCancel(g_smpPairMng.alarm); 34019e95205Sopenharmony_ci } 34119e95205Sopenharmony_ci } 34219e95205Sopenharmony_ci if (ret != SMP_SUCCESS) { 34319e95205Sopenharmony_ci SMP_GeneratePairResult(param->handle, SMP_PAIR_STATUS_FAILED, 0x00, NULL); 34419e95205Sopenharmony_ci } 34519e95205Sopenharmony_ci MEM_MALLOC.free(param); 34619e95205Sopenharmony_ci} 34719e95205Sopenharmony_ci 34819e95205Sopenharmony_ciint SMP_CancelPair(uint16_t handle) 34919e95205Sopenharmony_ci{ 35019e95205Sopenharmony_ci LOG_INFO("%{public}s", __FUNCTION__); 35119e95205Sopenharmony_ci SMP_CancelPairTask_t *ctx = MEM_MALLOC.alloc(sizeof(SMP_CancelPairTask_t)); 35219e95205Sopenharmony_ci if (ctx == NULL) { 35319e95205Sopenharmony_ci LOG_ERROR("%{public}s: Alloc error.", __FUNCTION__); 35419e95205Sopenharmony_ci return SMP_ERR_OUT_OF_RES; 35519e95205Sopenharmony_ci } 35619e95205Sopenharmony_ci (void)memset_s(ctx, sizeof(SMP_CancelPairTask_t), 0x00, sizeof(SMP_CancelPairTask_t)); 35719e95205Sopenharmony_ci ctx->handle = handle; 35819e95205Sopenharmony_ci int ret = BTM_RunTaskInProcessingQueue(PROCESSING_QUEUE_ID_SMP, SMP_CancelPairTask, (void *)ctx); 35919e95205Sopenharmony_ci if (ret != SMP_SUCCESS) { 36019e95205Sopenharmony_ci MEM_MALLOC.free(ctx); 36119e95205Sopenharmony_ci return ret; 36219e95205Sopenharmony_ci } 36319e95205Sopenharmony_ci return ret; 36419e95205Sopenharmony_ci} 36519e95205Sopenharmony_ci 36619e95205Sopenharmony_cistatic void SMP_CancelPairTask(void *context) 36719e95205Sopenharmony_ci{ 36819e95205Sopenharmony_ci SMP_CancelPairTask_t *param = (SMP_CancelPairTask_t *)context; 36919e95205Sopenharmony_ci 37019e95205Sopenharmony_ci LOG_DEBUG("%{public}s", __FUNCTION__); 37119e95205Sopenharmony_ci if (g_smpPairMng.state != SMP_STATE_PAIRING) { 37219e95205Sopenharmony_ci LOG_ERROR("It's not pairing state. "); 37319e95205Sopenharmony_ci MEM_MALLOC.free(param); 37419e95205Sopenharmony_ci return; 37519e95205Sopenharmony_ci } 37619e95205Sopenharmony_ci if (param->handle != g_smpPairMng.handle) { 37719e95205Sopenharmony_ci LOG_ERROR("Connection handle error. "); 37819e95205Sopenharmony_ci MEM_MALLOC.free(param); 37919e95205Sopenharmony_ci return; 38019e95205Sopenharmony_ci } 38119e95205Sopenharmony_ci SMP_GeneratePairResult( 38219e95205Sopenharmony_ci param->handle, SMP_PAIR_STATUS_FAILED, SMP_PAIR_FAILED_UNSPECIFIED_REASION, g_smpPairMng.alarm); 38319e95205Sopenharmony_ci MEM_MALLOC.free(param); 38419e95205Sopenharmony_ci} 38519e95205Sopenharmony_ci 38619e95205Sopenharmony_ciint SMP_AuthenticationRequestReply( 38719e95205Sopenharmony_ci uint16_t handle, bool accept, uint8_t rejectReason, uint8_t pairMethod, const uint8_t *entryValue) 38819e95205Sopenharmony_ci{ 38919e95205Sopenharmony_ci LOG_INFO("%{public}s", __FUNCTION__); 39019e95205Sopenharmony_ci SMP_AuthReqReplyTask_t *ctx = MEM_MALLOC.alloc(sizeof(SMP_AuthReqReplyTask_t)); 39119e95205Sopenharmony_ci if (ctx == NULL) { 39219e95205Sopenharmony_ci LOG_ERROR("%{public}s: Alloc error.", __FUNCTION__); 39319e95205Sopenharmony_ci return SMP_ERR_OUT_OF_RES; 39419e95205Sopenharmony_ci } 39519e95205Sopenharmony_ci (void)memset_s(ctx, sizeof(SMP_AuthReqReplyTask_t), 0x00, sizeof(SMP_AuthReqReplyTask_t)); 39619e95205Sopenharmony_ci ctx->handle = handle; 39719e95205Sopenharmony_ci ctx->accept = accept; 39819e95205Sopenharmony_ci ctx->rejectReason = rejectReason; 39919e95205Sopenharmony_ci ctx->pairMethod = pairMethod; 40019e95205Sopenharmony_ci if (accept && SMP_IsPasskeyEntryPair(pairMethod)) { 40119e95205Sopenharmony_ci (void)memcpy_s(ctx->entryValue, SMP_ENTRY_VALUE_LEN, entryValue, SMP_PASSKEY_LEN); 40219e95205Sopenharmony_ci } else if (accept && (pairMethod == SMP_PAIR_METHOD_OOB_LEGACY)) { 40319e95205Sopenharmony_ci (void)memcpy_s(ctx->entryValue, SMP_ENTRY_VALUE_LEN, entryValue, SMP_LEGACY_OOB_LEN); 40419e95205Sopenharmony_ci } else if ((accept && (pairMethod == SMP_PAIR_METHOD_OOB_SC_BOTH_SIDE_SEND_RECV)) || 40519e95205Sopenharmony_ci (accept && (pairMethod == SMP_PAIR_METHOD_OOB_SC_LOCAL_RECV_PEER_SEND))) { 40619e95205Sopenharmony_ci (void)memcpy_s(ctx->entryValue, SMP_ENTRY_VALUE_LEN, entryValue, SMP_SC_OOB_LEN); 40719e95205Sopenharmony_ci } 40819e95205Sopenharmony_ci int ret = BTM_RunTaskInProcessingQueue(PROCESSING_QUEUE_ID_SMP, SMP_AuthReqReplyTask, (void *)ctx); 40919e95205Sopenharmony_ci if (ret != SMP_SUCCESS) { 41019e95205Sopenharmony_ci MEM_MALLOC.free(ctx); 41119e95205Sopenharmony_ci return ret; 41219e95205Sopenharmony_ci } 41319e95205Sopenharmony_ci return ret; 41419e95205Sopenharmony_ci} 41519e95205Sopenharmony_ci 41619e95205Sopenharmony_cistatic void SMP_AuthReqReplyTask(void *context) 41719e95205Sopenharmony_ci{ 41819e95205Sopenharmony_ci SMP_AuthReqReplyTask_t *param = (SMP_AuthReqReplyTask_t *)context; 41919e95205Sopenharmony_ci 42019e95205Sopenharmony_ci LOG_DEBUG("%{public}s", __FUNCTION__); 42119e95205Sopenharmony_ci if (g_smpPairMng.state != SMP_STATE_PAIRING) { 42219e95205Sopenharmony_ci LOG_ERROR("It's not pairing state"); 42319e95205Sopenharmony_ci MEM_MALLOC.free(param); 42419e95205Sopenharmony_ci return; 42519e95205Sopenharmony_ci } 42619e95205Sopenharmony_ci if (g_smpPairMng.handle != param->handle) { 42719e95205Sopenharmony_ci LOG_ERROR("Connection handle error"); 42819e95205Sopenharmony_ci MEM_MALLOC.free(param); 42919e95205Sopenharmony_ci return; 43019e95205Sopenharmony_ci } 43119e95205Sopenharmony_ci if (g_smpPairMng.role == SMP_ROLE_MASTER) { 43219e95205Sopenharmony_ci LOG_INFO("It's initiator role."); 43319e95205Sopenharmony_ci SMP_AuthReqReplyProcessMaster(param->accept, param->rejectReason, param->pairMethod, param->entryValue); 43419e95205Sopenharmony_ci } else { 43519e95205Sopenharmony_ci LOG_INFO("It's accepter role."); 43619e95205Sopenharmony_ci SMP_AuthReqReplyProcessSlave(param->accept, param->rejectReason, param->pairMethod, param->entryValue); 43719e95205Sopenharmony_ci } 43819e95205Sopenharmony_ci MEM_MALLOC.free(param); 43919e95205Sopenharmony_ci} 44019e95205Sopenharmony_ci 44119e95205Sopenharmony_ciint SMP_RemotePairRequestReply( 44219e95205Sopenharmony_ci uint16_t handle, uint8_t rejectReason, const BtAddr *localAddr, const BtAddr *peerAddr, const SMP_PairParam *param) 44319e95205Sopenharmony_ci{ 44419e95205Sopenharmony_ci LOG_INFO("%{public}s", __FUNCTION__); 44519e95205Sopenharmony_ci SMP_RemotePairReqReplyTask_t *ctx = MEM_MALLOC.alloc(sizeof(SMP_RemotePairReqReplyTask_t)); 44619e95205Sopenharmony_ci if (ctx == NULL) { 44719e95205Sopenharmony_ci LOG_ERROR("%{public}s: Alloc error.", __FUNCTION__); 44819e95205Sopenharmony_ci return SMP_ERR_OUT_OF_RES; 44919e95205Sopenharmony_ci } 45019e95205Sopenharmony_ci (void)memset_s(ctx, sizeof(SMP_RemotePairReqReplyTask_t), 0x00, sizeof(SMP_RemotePairReqReplyTask_t)); 45119e95205Sopenharmony_ci ctx->handle = handle; 45219e95205Sopenharmony_ci ctx->rejectReason = rejectReason; 45319e95205Sopenharmony_ci if (rejectReason == SMP_PAIR_FAILED_NO_FAILED) { 45419e95205Sopenharmony_ci ctx->accept = true; 45519e95205Sopenharmony_ci } else { 45619e95205Sopenharmony_ci ctx->accept = false; 45719e95205Sopenharmony_ci } 45819e95205Sopenharmony_ci if (ctx->accept) { 45919e95205Sopenharmony_ci (void)memcpy_s(&ctx->localAddr, sizeof(BtAddr), localAddr, sizeof(ctx->localAddr)); 46019e95205Sopenharmony_ci (void)memcpy_s(&ctx->peerAddr, sizeof(BtAddr), peerAddr, sizeof(ctx->peerAddr)); 46119e95205Sopenharmony_ci (void)memcpy_s(&ctx->param, sizeof(BtAddr), param, sizeof(ctx->param)); 46219e95205Sopenharmony_ci } 46319e95205Sopenharmony_ci int ret = BTM_RunTaskInProcessingQueue(PROCESSING_QUEUE_ID_SMP, SMP_RemotePairReqReplyTask, (void *)ctx); 46419e95205Sopenharmony_ci if (ret != SMP_SUCCESS) { 46519e95205Sopenharmony_ci MEM_MALLOC.free(ctx); 46619e95205Sopenharmony_ci return ret; 46719e95205Sopenharmony_ci } 46819e95205Sopenharmony_ci return ret; 46919e95205Sopenharmony_ci} 47019e95205Sopenharmony_ci 47119e95205Sopenharmony_cistatic void SMP_RemotePairReqReplyTask(void *context) 47219e95205Sopenharmony_ci{ 47319e95205Sopenharmony_ci SMP_RemotePairReqReplyTask_t *param = (SMP_RemotePairReqReplyTask_t *)context; 47419e95205Sopenharmony_ci 47519e95205Sopenharmony_ci LOG_DEBUG("%{public}s", __FUNCTION__); 47619e95205Sopenharmony_ci int ret = SMP_PairReqReplyJudgeException(param->handle, param->accept, param->rejectReason, ¶m->param); 47719e95205Sopenharmony_ci if (!ret) { 47819e95205Sopenharmony_ci g_smpPairMng.local.addr.type = param->localAddr.type; 47919e95205Sopenharmony_ci g_smpPairMng.peer.addr.type = param->peerAddr.type; 48019e95205Sopenharmony_ci SMP_MemoryReverseCopy( 48119e95205Sopenharmony_ci g_smpPairMng.local.addr.addr, param->localAddr.addr, sizeof(g_smpPairMng.local.addr.addr)); 48219e95205Sopenharmony_ci SMP_MemoryReverseCopy(g_smpPairMng.peer.addr.addr, param->peerAddr.addr, sizeof(g_smpPairMng.peer.addr.addr)); 48319e95205Sopenharmony_ci (void)memcpy_s(&g_smpPairMng.local.pairParam, sizeof(SMP_PairParam), ¶m->param, sizeof(SMP_PairParam)); 48419e95205Sopenharmony_ci SMP_CalculatePairType(&g_smpPairMng); 48519e95205Sopenharmony_ci SMP_CalculateEncKeySize(&g_smpPairMng); 48619e95205Sopenharmony_ci SMP_CalculatePairMethod(&g_smpPairMng); 48719e95205Sopenharmony_ci SMP_CalculateKeyDistribution(&g_smpPairMng); 48819e95205Sopenharmony_ci ret = SMP_SendPairingResponse(param->handle, &g_smpPairMng.local.pairParam, SMP_SendPairingResponseCallback); 48919e95205Sopenharmony_ci if (ret != SMP_SUCCESS) { 49019e95205Sopenharmony_ci LOG_ERROR("Send pairing response failed."); 49119e95205Sopenharmony_ci SMP_GeneratePairResult(param->handle, SMP_PAIR_STATUS_FAILED, SMP_PAIR_FAILED_UNSPECIFIED_REASION, NULL); 49219e95205Sopenharmony_ci } else { 49319e95205Sopenharmony_ci if (g_smpPairMng.pairType == SMP_PAIR_TYPE_LEGACY) { 49419e95205Sopenharmony_ci LOG_INFO("It's legacy pair."); 49519e95205Sopenharmony_ci SMP_PairReqReplyLegacyPair(); 49619e95205Sopenharmony_ci } else { 49719e95205Sopenharmony_ci LOG_INFO("It's SC pair."); 49819e95205Sopenharmony_ci SMP_PairReqReplyScPair(); 49919e95205Sopenharmony_ci } 50019e95205Sopenharmony_ci } 50119e95205Sopenharmony_ci } 50219e95205Sopenharmony_ci MEM_MALLOC.free(param); 50319e95205Sopenharmony_ci} 50419e95205Sopenharmony_ci 50519e95205Sopenharmony_ciint SMP_RemotePairResponseReply(uint16_t handle, bool accept, uint8_t rejectReason) 50619e95205Sopenharmony_ci{ 50719e95205Sopenharmony_ci LOG_INFO("%{public}s", __FUNCTION__); 50819e95205Sopenharmony_ci SMP_RemotePairRspReplyTask_t *ctx = MEM_MALLOC.alloc(sizeof(SMP_RemotePairRspReplyTask_t)); 50919e95205Sopenharmony_ci if (ctx == NULL) { 51019e95205Sopenharmony_ci LOG_ERROR("%{public}s: Alloc error.", __FUNCTION__); 51119e95205Sopenharmony_ci return SMP_ERR_OUT_OF_RES; 51219e95205Sopenharmony_ci } 51319e95205Sopenharmony_ci (void)memset_s(ctx, sizeof(SMP_RemotePairRspReplyTask_t), 0x00, sizeof(SMP_RemotePairRspReplyTask_t)); 51419e95205Sopenharmony_ci ctx->handle = handle; 51519e95205Sopenharmony_ci ctx->accept = accept; 51619e95205Sopenharmony_ci ctx->rejectReason = rejectReason; 51719e95205Sopenharmony_ci int ret = BTM_RunTaskInProcessingQueue(PROCESSING_QUEUE_ID_SMP, SMP_RemotePairRspReplyTask, (void *)ctx); 51819e95205Sopenharmony_ci if (ret != SMP_SUCCESS) { 51919e95205Sopenharmony_ci MEM_MALLOC.free(ctx); 52019e95205Sopenharmony_ci return ret; 52119e95205Sopenharmony_ci } 52219e95205Sopenharmony_ci return ret; 52319e95205Sopenharmony_ci} 52419e95205Sopenharmony_ci 52519e95205Sopenharmony_cistatic void SMP_RemotePairRspReplyTask(void *context) 52619e95205Sopenharmony_ci{ 52719e95205Sopenharmony_ci SMP_RemotePairRspReplyTask_t *param = (SMP_RemotePairRspReplyTask_t *)context; 52819e95205Sopenharmony_ci 52919e95205Sopenharmony_ci LOG_DEBUG("%{public}s", __FUNCTION__); 53019e95205Sopenharmony_ci int ret = SMP_PairRspReplyJudgeException(param->handle, param->accept, param->rejectReason); 53119e95205Sopenharmony_ci if (!ret) { 53219e95205Sopenharmony_ci SMP_CalculatePairType(&g_smpPairMng); 53319e95205Sopenharmony_ci SMP_CalculateEncKeySize(&g_smpPairMng); 53419e95205Sopenharmony_ci SMP_CalculatePairMethod(&g_smpPairMng); 53519e95205Sopenharmony_ci SMP_CalculateKeyDistribution(&g_smpPairMng); 53619e95205Sopenharmony_ci 53719e95205Sopenharmony_ci if (g_smpPairMng.pairType == SMP_PAIR_TYPE_LEGACY) { 53819e95205Sopenharmony_ci LOG_INFO("It's legacy pair."); 53919e95205Sopenharmony_ci SMP_PairRspReplyLegacyPair(); 54019e95205Sopenharmony_ci } else { 54119e95205Sopenharmony_ci LOG_INFO("It's SC pair."); 54219e95205Sopenharmony_ci SMP_PairRspReplyScPair(); 54319e95205Sopenharmony_ci } 54419e95205Sopenharmony_ci } 54519e95205Sopenharmony_ci MEM_MALLOC.free(param); 54619e95205Sopenharmony_ci} 54719e95205Sopenharmony_ci 54819e95205Sopenharmony_ciint SMP_RemoteSecurityRequestReply(uint16_t handle, bool accept, uint8_t rejectReason) 54919e95205Sopenharmony_ci{ 55019e95205Sopenharmony_ci LOG_INFO("%{public}s", __FUNCTION__); 55119e95205Sopenharmony_ci SMP_RemoteSecReqReplyTask_t *ctx = MEM_MALLOC.alloc(sizeof(SMP_RemoteSecReqReplyTask_t)); 55219e95205Sopenharmony_ci if (ctx == NULL) { 55319e95205Sopenharmony_ci LOG_ERROR("%{public}s: Alloc error.", __FUNCTION__); 55419e95205Sopenharmony_ci return SMP_ERR_OUT_OF_RES; 55519e95205Sopenharmony_ci } 55619e95205Sopenharmony_ci (void)memset_s(ctx, sizeof(SMP_RemoteSecReqReplyTask_t), 0x00, sizeof(SMP_RemoteSecReqReplyTask_t)); 55719e95205Sopenharmony_ci ctx->handle = handle; 55819e95205Sopenharmony_ci ctx->accept = accept; 55919e95205Sopenharmony_ci ctx->rejectReason = rejectReason; 56019e95205Sopenharmony_ci int ret = BTM_RunTaskInProcessingQueue(PROCESSING_QUEUE_ID_SMP, SMP_RemoteSecReqReplyTask, (void *)ctx); 56119e95205Sopenharmony_ci if (ret != SMP_SUCCESS) { 56219e95205Sopenharmony_ci MEM_MALLOC.free(ctx); 56319e95205Sopenharmony_ci return ret; 56419e95205Sopenharmony_ci } 56519e95205Sopenharmony_ci return ret; 56619e95205Sopenharmony_ci} 56719e95205Sopenharmony_ci 56819e95205Sopenharmony_cistatic void SMP_RemoteSecReqReplyTask(void *context) 56919e95205Sopenharmony_ci{ 57019e95205Sopenharmony_ci SMP_RemoteSecReqReplyTask_t *param = (SMP_RemoteSecReqReplyTask_t *)context; 57119e95205Sopenharmony_ci LOG_DEBUG("%{public}s", __FUNCTION__); 57219e95205Sopenharmony_ci if (!param->accept) { 57319e95205Sopenharmony_ci LOG_ERROR("User rejected."); 57419e95205Sopenharmony_ci SMP_SendPairingFailed(param->handle, param->rejectReason, SMP_SendPairingFailedCallback); 57519e95205Sopenharmony_ci } 57619e95205Sopenharmony_ci MEM_MALLOC.free(param); 57719e95205Sopenharmony_ci} 57819e95205Sopenharmony_ci 57919e95205Sopenharmony_ciint SMP_LongTermKeyRequestReply(uint16_t handle, bool accept, const uint8_t *key) 58019e95205Sopenharmony_ci{ 58119e95205Sopenharmony_ci LOG_INFO("%{public}s", __FUNCTION__); 58219e95205Sopenharmony_ci SMP_LongTermKeyReqReplyTask_t *ctx = MEM_MALLOC.alloc(sizeof(SMP_LongTermKeyReqReplyTask_t)); 58319e95205Sopenharmony_ci if (ctx == NULL) { 58419e95205Sopenharmony_ci LOG_ERROR("%{public}s: Alloc error.", __FUNCTION__); 58519e95205Sopenharmony_ci return SMP_ERR_OUT_OF_RES; 58619e95205Sopenharmony_ci } 58719e95205Sopenharmony_ci (void)memset_s(ctx, sizeof(SMP_LongTermKeyReqReplyTask_t), 0x00, sizeof(SMP_LongTermKeyReqReplyTask_t)); 58819e95205Sopenharmony_ci ctx->handle = handle; 58919e95205Sopenharmony_ci ctx->accept = accept; 59019e95205Sopenharmony_ci if (key != NULL) { 59119e95205Sopenharmony_ci (void)memcpy_s(ctx->key, SMP_LTK_LEN, key, SMP_LTK_LEN); 59219e95205Sopenharmony_ci } 59319e95205Sopenharmony_ci int ret = BTM_RunTaskInProcessingQueue(PROCESSING_QUEUE_ID_SMP, SMP_LongTermKeyReqReplyTask, (void *)ctx); 59419e95205Sopenharmony_ci if (ret != SMP_SUCCESS) { 59519e95205Sopenharmony_ci MEM_MALLOC.free(ctx); 59619e95205Sopenharmony_ci return ret; 59719e95205Sopenharmony_ci } 59819e95205Sopenharmony_ci return ret; 59919e95205Sopenharmony_ci} 60019e95205Sopenharmony_ci 60119e95205Sopenharmony_cistatic void SMP_LongTermKeyReqReplyTask(void *context) 60219e95205Sopenharmony_ci{ 60319e95205Sopenharmony_ci SMP_LongTermKeyReqReplyTask_t *param = (SMP_LongTermKeyReqReplyTask_t *)context; 60419e95205Sopenharmony_ci 60519e95205Sopenharmony_ci LOG_DEBUG("%{public}s", __FUNCTION__); 60619e95205Sopenharmony_ci if ((g_smpPairMng.state == SMP_STATE_PAIRING) && (g_smpPairMng.handle == param->handle)) { 60719e95205Sopenharmony_ci SMP_LtkReqReplyException(param->handle); 60819e95205Sopenharmony_ci } else { 60919e95205Sopenharmony_ci SMP_LtkReqReplyNormal(param->handle, param->accept, param->key); 61019e95205Sopenharmony_ci } 61119e95205Sopenharmony_ci MEM_MALLOC.free(param); 61219e95205Sopenharmony_ci} 61319e95205Sopenharmony_ci 61419e95205Sopenharmony_cistatic int SMP_LtkReqReplyNormal(uint16_t handle, bool accept, const uint8_t *key) 61519e95205Sopenharmony_ci{ 61619e95205Sopenharmony_ci int ret = SMP_SUCCESS; 61719e95205Sopenharmony_ci HciLeLongTermKeyRequestReplyParam replycmdParam; 61819e95205Sopenharmony_ci HciLeLongTermKeyRequestNegativeReplyParam negativeReplyParam; 61919e95205Sopenharmony_ci 62019e95205Sopenharmony_ci if (!accept) { 62119e95205Sopenharmony_ci LOG_ERROR("User rejected."); 62219e95205Sopenharmony_ci negativeReplyParam.connectionHandle = handle; 62319e95205Sopenharmony_ci HCI_LeLongTermKeyRequestNegativeReply(&negativeReplyParam); 62419e95205Sopenharmony_ci SMP_NotifyCbEncComp(handle, SMP_ENCRYPT_STATUS_FAILED); 62519e95205Sopenharmony_ci } else { 62619e95205Sopenharmony_ci replycmdParam.connectionHandle = handle; 62719e95205Sopenharmony_ci (void)memcpy_s(replycmdParam.longTermKey, SMP_LTK_LEN, key, SMP_LTK_LEN); 62819e95205Sopenharmony_ci ret = HCI_LeLongTermKeyRequestReply(&replycmdParam); 62919e95205Sopenharmony_ci if (ret != SMP_SUCCESS) { 63019e95205Sopenharmony_ci SMP_NotifyCbEncComp(handle, SMP_ENCRYPT_STATUS_FAILED); 63119e95205Sopenharmony_ci } 63219e95205Sopenharmony_ci } 63319e95205Sopenharmony_ci 63419e95205Sopenharmony_ci return ret; 63519e95205Sopenharmony_ci} 63619e95205Sopenharmony_ci 63719e95205Sopenharmony_cistatic int SMP_LtkReqReplyException(uint16_t handle) 63819e95205Sopenharmony_ci{ 63919e95205Sopenharmony_ci int ret = SMP_SUCCESS; 64019e95205Sopenharmony_ci HciLeLongTermKeyRequestNegativeReplyParam negativeReplyParam; 64119e95205Sopenharmony_ci 64219e95205Sopenharmony_ci LOG_ERROR("Invalid state."); 64319e95205Sopenharmony_ci negativeReplyParam.connectionHandle = handle; 64419e95205Sopenharmony_ci HCI_LeLongTermKeyRequestNegativeReply(&negativeReplyParam); 64519e95205Sopenharmony_ci SMP_NotifyCbEncComp(handle, SMP_ENCRYPT_STATUS_FAILED); 64619e95205Sopenharmony_ci 64719e95205Sopenharmony_ci return ret; 64819e95205Sopenharmony_ci} 64919e95205Sopenharmony_ci 65019e95205Sopenharmony_ciint SMP_StartEncryption(uint16_t handle, const uint8_t *random, uint16_t ediv, const uint8_t *key) 65119e95205Sopenharmony_ci{ 65219e95205Sopenharmony_ci LOG_INFO("%{public}s", __FUNCTION__); 65319e95205Sopenharmony_ci SMP_StartEncTask_t *ctx = MEM_MALLOC.alloc(sizeof(SMP_StartEncTask_t)); 65419e95205Sopenharmony_ci if (ctx == NULL) { 65519e95205Sopenharmony_ci LOG_ERROR("%{public}s: Alloc error.", __FUNCTION__); 65619e95205Sopenharmony_ci return SMP_ERR_OUT_OF_RES; 65719e95205Sopenharmony_ci } 65819e95205Sopenharmony_ci (void)memset_s(ctx, sizeof(SMP_StartEncTask_t), 0x00, sizeof(SMP_StartEncTask_t)); 65919e95205Sopenharmony_ci ctx->handle = handle; 66019e95205Sopenharmony_ci (void)memcpy_s(ctx->random, SMP_MASTER_RAND_LEN, random, SMP_MASTER_RAND_LEN); 66119e95205Sopenharmony_ci ctx->ediv = ediv; 66219e95205Sopenharmony_ci (void)memcpy_s(ctx->key, SMP_LTK_LEN, key, SMP_LTK_LEN); 66319e95205Sopenharmony_ci int ret = BTM_RunTaskInProcessingQueue(PROCESSING_QUEUE_ID_SMP, SMP_StartEncTask, (void *)ctx); 66419e95205Sopenharmony_ci if (ret != SMP_SUCCESS) { 66519e95205Sopenharmony_ci MEM_MALLOC.free(ctx); 66619e95205Sopenharmony_ci return ret; 66719e95205Sopenharmony_ci } 66819e95205Sopenharmony_ci return ret; 66919e95205Sopenharmony_ci} 67019e95205Sopenharmony_ci 67119e95205Sopenharmony_cistatic void SMP_StartEncTask(void *context) 67219e95205Sopenharmony_ci{ 67319e95205Sopenharmony_ci int ret; 67419e95205Sopenharmony_ci HciLeStartEncryptionParam startEncParam; 67519e95205Sopenharmony_ci SMP_StartEncTask_t *param = (SMP_StartEncTask_t *)context; 67619e95205Sopenharmony_ci 67719e95205Sopenharmony_ci LOG_DEBUG("%{public}s", __FUNCTION__); 67819e95205Sopenharmony_ci if ((g_smpPairMng.state == SMP_STATE_PAIRING) && (g_smpPairMng.handle == param->handle)) { 67919e95205Sopenharmony_ci LOG_ERROR("Invalid state."); 68019e95205Sopenharmony_ci ret = SMP_ERR_INVAL_STATE; 68119e95205Sopenharmony_ci } else { 68219e95205Sopenharmony_ci uint8_t edivTemp[sizeof(param->ediv)] = {0x00}; 68319e95205Sopenharmony_ci startEncParam.connectionHandle = param->handle; 68419e95205Sopenharmony_ci edivTemp[0x00] = ((uint8_t)((param->ediv) & 0xFF)); 68519e95205Sopenharmony_ci edivTemp[0x01] = ((uint8_t)(((param->ediv) >> 0x08) & 0xFF)); 68619e95205Sopenharmony_ci (void)memcpy_s(&startEncParam.encryptDiversifier, sizeof(edivTemp), edivTemp, sizeof(edivTemp)); 68719e95205Sopenharmony_ci SMP_MemoryReverseCopy(startEncParam.randomNumber, param->random, SMP_MASTER_RAND_LEN); 68819e95205Sopenharmony_ci (void)memcpy_s(startEncParam.longTermKey, SMP_LTK_LEN, param->key, SMP_LTK_LEN); 68919e95205Sopenharmony_ci ret = HCI_LeStartEncryption(&startEncParam); 69019e95205Sopenharmony_ci if (ret != SMP_SUCCESS) { 69119e95205Sopenharmony_ci LOG_ERROR("HCI_LeStartEncryption failed."); 69219e95205Sopenharmony_ci } 69319e95205Sopenharmony_ci } 69419e95205Sopenharmony_ci if (ret != SMP_SUCCESS) { 69519e95205Sopenharmony_ci SMP_NotifyCbEncComp(param->handle, SMP_ENCRYPT_STATUS_FAILED); 69619e95205Sopenharmony_ci } 69719e95205Sopenharmony_ci MEM_MALLOC.free(param); 69819e95205Sopenharmony_ci} 69919e95205Sopenharmony_ci 70019e95205Sopenharmony_civoid SMP_PairTimeout(void *parameter) 70119e95205Sopenharmony_ci{ 70219e95205Sopenharmony_ci LOG_INFO("%{public}s", __FUNCTION__); 70319e95205Sopenharmony_ci int ret = BTM_RunTaskInProcessingQueue(PROCESSING_QUEUE_ID_SMP, SMP_PairTimeoutTask, NULL); 70419e95205Sopenharmony_ci if (ret != SMP_SUCCESS) { 70519e95205Sopenharmony_ci return; 70619e95205Sopenharmony_ci } 70719e95205Sopenharmony_ci (void)parameter; 70819e95205Sopenharmony_ci} 70919e95205Sopenharmony_ci 71019e95205Sopenharmony_cistatic void SMP_PairTimeoutTask(void *context) 71119e95205Sopenharmony_ci{ 71219e95205Sopenharmony_ci LOG_DEBUG("%{public}s", __FUNCTION__); 71319e95205Sopenharmony_ci if (g_smpPairMng.state != SMP_STATE_PAIRING) { 71419e95205Sopenharmony_ci LOG_ERROR("It's not pairing state. "); 71519e95205Sopenharmony_ci return; 71619e95205Sopenharmony_ci } 71719e95205Sopenharmony_ci if (g_smpPairMng.role == SMP_ROLE_MASTER) { 71819e95205Sopenharmony_ci LOG_ERROR("Master, step:%hu. ", g_smpPairMng.step); 71919e95205Sopenharmony_ci } else { 72019e95205Sopenharmony_ci LOG_ERROR("Slave, step:%hu. ", g_smpPairMng.step); 72119e95205Sopenharmony_ci } 72219e95205Sopenharmony_ci SMP_GeneratePairResult(g_smpPairMng.handle, SMP_PAIR_STATUS_FAILED, 0x00, NULL); 72319e95205Sopenharmony_ci (void)context; 72419e95205Sopenharmony_ci} 72519e95205Sopenharmony_ci 72619e95205Sopenharmony_ciint SMP_SendSecurityRequestToRemote(uint16_t handle, uint8_t authReq) 72719e95205Sopenharmony_ci{ 72819e95205Sopenharmony_ci LOG_INFO("%{public}s", __FUNCTION__); 72919e95205Sopenharmony_ci SMP_SendSecReqToRemoteTask_t *ctx = MEM_MALLOC.alloc(sizeof(SMP_SendSecReqToRemoteTask_t)); 73019e95205Sopenharmony_ci if (ctx == NULL) { 73119e95205Sopenharmony_ci LOG_ERROR("%{public}s: Alloc error.", __FUNCTION__); 73219e95205Sopenharmony_ci return SMP_ERR_OUT_OF_RES; 73319e95205Sopenharmony_ci } 73419e95205Sopenharmony_ci (void)memset_s(ctx, sizeof(SMP_SendSecReqToRemoteTask_t), 0x00, sizeof(SMP_SendSecReqToRemoteTask_t)); 73519e95205Sopenharmony_ci ctx->handle = handle; 73619e95205Sopenharmony_ci ctx->authReq = authReq; 73719e95205Sopenharmony_ci int ret = BTM_RunTaskInProcessingQueue(PROCESSING_QUEUE_ID_SMP, SMP_SendSecReqToRemoteTask, (void *)ctx); 73819e95205Sopenharmony_ci if (ret != SMP_SUCCESS) { 73919e95205Sopenharmony_ci MEM_MALLOC.free(ctx); 74019e95205Sopenharmony_ci return ret; 74119e95205Sopenharmony_ci } 74219e95205Sopenharmony_ci return ret; 74319e95205Sopenharmony_ci} 74419e95205Sopenharmony_ci 74519e95205Sopenharmony_cistatic void SMP_SendSecReqToRemoteTask(void *context) 74619e95205Sopenharmony_ci{ 74719e95205Sopenharmony_ci SMP_SendSecReqToRemoteTask_t *param = (SMP_SendSecReqToRemoteTask_t *)context; 74819e95205Sopenharmony_ci LOG_DEBUG("%{public}s", __FUNCTION__); 74919e95205Sopenharmony_ci SMP_SendSecurityRequest(param->handle, param->authReq, SMP_SendSecurityRequestCallback); 75019e95205Sopenharmony_ci MEM_MALLOC.free(param); 75119e95205Sopenharmony_ci} 75219e95205Sopenharmony_ci 75319e95205Sopenharmony_ciint SMP_GenerateSignature(const uint8_t *csrk, uint32_t counter, const uint8_t *data, uint16_t dataLen) 75419e95205Sopenharmony_ci{ 75519e95205Sopenharmony_ci int ret; 75619e95205Sopenharmony_ci LOG_INFO("%{public}s", __FUNCTION__); 75719e95205Sopenharmony_ci SMP_GenSignTask_t *ctx = MEM_MALLOC.alloc(sizeof(SMP_GenSignTask_t)); 75819e95205Sopenharmony_ci if (ctx == NULL) { 75919e95205Sopenharmony_ci LOG_ERROR("%{public}s: Alloc error.", __FUNCTION__); 76019e95205Sopenharmony_ci return SMP_ERR_OUT_OF_RES; 76119e95205Sopenharmony_ci } 76219e95205Sopenharmony_ci (void)memset_s(ctx, sizeof(SMP_GenSignTask_t), 0x00, sizeof(SMP_GenSignTask_t)); 76319e95205Sopenharmony_ci (void)memcpy_s(ctx->csrk, SMP_CSRK_LEN, csrk, SMP_CSRK_LEN); 76419e95205Sopenharmony_ci ctx->counter = counter; 76519e95205Sopenharmony_ci ctx->dataLen = dataLen; 76619e95205Sopenharmony_ci ctx->data = MEM_MALLOC.alloc(dataLen); 76719e95205Sopenharmony_ci (void)memcpy_s(ctx->data, dataLen, data, dataLen); 76819e95205Sopenharmony_ci ret = BTM_RunTaskInProcessingQueue(PROCESSING_QUEUE_ID_SMP, SMP_GenSignTask, (void *)ctx); 76919e95205Sopenharmony_ci if (ret != SMP_SUCCESS) { 77019e95205Sopenharmony_ci MEM_MALLOC.free(ctx); 77119e95205Sopenharmony_ci return ret; 77219e95205Sopenharmony_ci } 77319e95205Sopenharmony_ci return ret; 77419e95205Sopenharmony_ci} 77519e95205Sopenharmony_ci 77619e95205Sopenharmony_cistatic void SMP_GenSignTask(void *context) 77719e95205Sopenharmony_ci{ 77819e95205Sopenharmony_ci uint8_t cryptAesCmacZ[CRYPT_AESCMAC_Z_LEN] = {0x00}; 77919e95205Sopenharmony_ci SMP_EncCmd *encCmd = SMP_AllocEncCmd(); 78019e95205Sopenharmony_ci if (encCmd == NULL) { 78119e95205Sopenharmony_ci LOG_ERROR("%{public}s: Alloc error.", __FUNCTION__); 78219e95205Sopenharmony_ci return; 78319e95205Sopenharmony_ci } 78419e95205Sopenharmony_ci HciLeEncryptParam encryptParam; 78519e95205Sopenharmony_ci SMP_GenSignTask_t *param = (SMP_GenSignTask_t *)context; 78619e95205Sopenharmony_ci 78719e95205Sopenharmony_ci LOG_DEBUG("%{public}s", __FUNCTION__); 78819e95205Sopenharmony_ci encCmd->length = param->dataLen + SMP_SIGNCOUNTER_LEN; 78919e95205Sopenharmony_ci encCmd->M = MEM_MALLOC.alloc(encCmd->length); 79019e95205Sopenharmony_ci (void)memcpy_s(encCmd->M, param->dataLen, param->data, param->dataLen); 79119e95205Sopenharmony_ci (void)memcpy_s(encCmd->M + param->dataLen, SMP_SIGNCOUNTER_LEN, (uint8_t *)¶m->counter, SMP_SIGNCOUNTER_LEN); 79219e95205Sopenharmony_ci encCmd->signDataLen = param->dataLen + SMP_SIGNCOUNTER_LEN; 79319e95205Sopenharmony_ci SMP_ReverseMemoryOrder(encCmd->M, encCmd->signDataLen); 79419e95205Sopenharmony_ci (void)memcpy_s(encCmd->key, sizeof(encCmd->key), param->csrk, sizeof(encCmd->key)); 79519e95205Sopenharmony_ci encCmd->signCounter = param->counter; 79619e95205Sopenharmony_ci (void)memcpy_s(encryptParam.key, SMP_CSRK_LEN, param->csrk, SMP_CSRK_LEN); 79719e95205Sopenharmony_ci (void)memcpy_s(encryptParam.plaintextData, sizeof(encryptParam.plaintextData), cryptAesCmacZ, CRYPT_AESCMAC_Z_LEN); 79819e95205Sopenharmony_ci LOG_DEBUG("SMP_GENERATE_SIGNATURE_STEP_1 started."); 79919e95205Sopenharmony_ci int ret = SMP_SendLeEncryptCmd(&encryptParam, SMP_GENERATE_SIGNATURE_STEP_1, encCmd, SMP_USING_HW_AES128_SIGN); 80019e95205Sopenharmony_ci (void)memset_s(encryptParam.key, SMP_ENCRYPT_KEY_LEN, 0x00, SMP_ENCRYPT_KEY_LEN); 80119e95205Sopenharmony_ci if (ret != SMP_SUCCESS) { 80219e95205Sopenharmony_ci SMP_NotifyCbGenSign(SMP_GENERATE_SIGN_STATUS_FAILED, NULL); 80319e95205Sopenharmony_ci } 80419e95205Sopenharmony_ci SMP_FreeEncCmd(encCmd); 80519e95205Sopenharmony_ci MEM_MALLOC.free(param->data); 80619e95205Sopenharmony_ci MEM_MALLOC.free(param); 80719e95205Sopenharmony_ci} 80819e95205Sopenharmony_ci 80919e95205Sopenharmony_ciint SMP_ResolveRPA(const uint8_t *addr, const uint8_t *irk) 81019e95205Sopenharmony_ci{ 81119e95205Sopenharmony_ci LOG_INFO("%{public}s", __FUNCTION__); 81219e95205Sopenharmony_ci int ret; 81319e95205Sopenharmony_ci uint8_t message[SMP_ENCRYPT_PLAINTEXTDATA_LEN] = {0x00}; 81419e95205Sopenharmony_ci (void)memcpy_s(message, sizeof(message), addr + SMP_RPA_HIGH_BIT_LEN, SMP_RPA_HIGH_BIT_LEN); 81519e95205Sopenharmony_ci uint8_t encryptedData[SMP_ENCRYPT_PLAINTEXTDATA_LEN] = {0x00}; 81619e95205Sopenharmony_ci ret = SMP_Aes128(irk, SMP_IRK_LEN, message, sizeof(message), encryptedData); 81719e95205Sopenharmony_ci if (ret != SMP_SUCCESS) { 81819e95205Sopenharmony_ci LOG_ERROR("status = %{public}d.", ret); 81919e95205Sopenharmony_ci ret = SMP_ERR_INVAL_STATE; 82019e95205Sopenharmony_ci return ret; 82119e95205Sopenharmony_ci } 82219e95205Sopenharmony_ci if (memcmp(encryptedData, addr, SMP_RPA_HIGH_BIT_LEN) != 0x00) { 82319e95205Sopenharmony_ci LOG_INFO("Resolve RPA failed"); 82419e95205Sopenharmony_ci ret = SMP_RESOLVE_RPA_RESULT_NO; 82519e95205Sopenharmony_ci } else { 82619e95205Sopenharmony_ci LOG_INFO("Resolve RPA success"); 82719e95205Sopenharmony_ci ret = SMP_RESOLVE_RPA_RESULT_YES; 82819e95205Sopenharmony_ci } 82919e95205Sopenharmony_ci return ret; 83019e95205Sopenharmony_ci} 83119e95205Sopenharmony_ci 83219e95205Sopenharmony_ciint SMP_AsyncResolveRPA(const uint8_t *addr, const uint8_t *irk) 83319e95205Sopenharmony_ci{ 83419e95205Sopenharmony_ci LOG_INFO("%{public}s", __FUNCTION__); 83519e95205Sopenharmony_ci SMP_AsyncResoRpaTask_t *ctx = MEM_MALLOC.alloc(sizeof(SMP_AsyncResoRpaTask_t)); 83619e95205Sopenharmony_ci if (ctx == NULL) { 83719e95205Sopenharmony_ci LOG_ERROR("%{public}s: Alloc error.", __FUNCTION__); 83819e95205Sopenharmony_ci return SMP_ERR_OUT_OF_RES; 83919e95205Sopenharmony_ci } 84019e95205Sopenharmony_ci (void)memset_s(ctx, sizeof(SMP_AsyncResoRpaTask_t), 0x00, sizeof(SMP_AsyncResoRpaTask_t)); 84119e95205Sopenharmony_ci (void)memcpy_s(ctx->addr, sizeof(ctx->addr), addr, sizeof(ctx->addr)); 84219e95205Sopenharmony_ci (void)memcpy_s(ctx->irk, sizeof(ctx->irk), irk, SMP_IRK_LEN); 84319e95205Sopenharmony_ci int ret = BTM_RunTaskInProcessingQueue(PROCESSING_QUEUE_ID_SMP, SMP_AsyncResoRpaTask, (void *)ctx); 84419e95205Sopenharmony_ci if (ret != SMP_SUCCESS) { 84519e95205Sopenharmony_ci MEM_MALLOC.free(ctx); 84619e95205Sopenharmony_ci return ret; 84719e95205Sopenharmony_ci } 84819e95205Sopenharmony_ci return ret; 84919e95205Sopenharmony_ci} 85019e95205Sopenharmony_ci 85119e95205Sopenharmony_cistatic void SMP_AsyncResoRpaTask(void *context) 85219e95205Sopenharmony_ci{ 85319e95205Sopenharmony_ci uint8_t message[SMP_ENCRYPT_PLAINTEXTDATA_LEN] = {0x00}; 85419e95205Sopenharmony_ci SMP_EncCmd *encCmd = SMP_AllocEncCmd(); 85519e95205Sopenharmony_ci if (encCmd == NULL) { 85619e95205Sopenharmony_ci LOG_ERROR("%{public}s: Alloc error.", __FUNCTION__); 85719e95205Sopenharmony_ci return; 85819e95205Sopenharmony_ci } 85919e95205Sopenharmony_ci HciLeEncryptParam encryptParam; 86019e95205Sopenharmony_ci SMP_AsyncResoRpaTask_t *param = (SMP_AsyncResoRpaTask_t *)context; 86119e95205Sopenharmony_ci 86219e95205Sopenharmony_ci LOG_DEBUG("%{public}s", __FUNCTION__); 86319e95205Sopenharmony_ci (void)memcpy_s(message, sizeof(message), param->addr + SMP_RPA_HIGH_BIT_LEN, SMP_RPA_HIGH_BIT_LEN); 86419e95205Sopenharmony_ci (void)memcpy_s(encCmd->address, sizeof(encCmd->address), param->addr, sizeof(encCmd->address)); 86519e95205Sopenharmony_ci (void)memcpy_s(encCmd->key, sizeof(encCmd->key), param->irk, SMP_IRK_LEN); 86619e95205Sopenharmony_ci (void)memcpy_s(encryptParam.key, sizeof(encryptParam.key), param->irk, sizeof(encryptParam.key)); 86719e95205Sopenharmony_ci (void)memcpy_s(encryptParam.plaintextData, sizeof(encryptParam.plaintextData), message, sizeof(message)); 86819e95205Sopenharmony_ci LOG_DEBUG("SMP_RESOLVE_RPA_STEP_1 started."); 86919e95205Sopenharmony_ci int ret = SMP_SendLeEncryptCmd(&encryptParam, SMP_RESOLVE_RPA_STEP_1, encCmd, SMP_USING_HW_AES128_RPA); 87019e95205Sopenharmony_ci (void)memset_s(encryptParam.key, SMP_ENCRYPT_KEY_LEN, 0x00, SMP_ENCRYPT_KEY_LEN); 87119e95205Sopenharmony_ci if (ret != SMP_SUCCESS) { 87219e95205Sopenharmony_ci SMP_NotifyCbResoRpa(SMP_RESOLVE_RPA_STATUS_FAILED, false, param->addr, param->irk); 87319e95205Sopenharmony_ci } 87419e95205Sopenharmony_ci SMP_FreeEncCmd(encCmd); 87519e95205Sopenharmony_ci MEM_MALLOC.free(param); 87619e95205Sopenharmony_ci} 87719e95205Sopenharmony_ci 87819e95205Sopenharmony_ciint SMP_GenerateRPA(const uint8_t *irk) 87919e95205Sopenharmony_ci{ 88019e95205Sopenharmony_ci LOG_INFO("%{public}s", __FUNCTION__); 88119e95205Sopenharmony_ci SMP_GenRpaTask_t *ctx = MEM_MALLOC.alloc(sizeof(SMP_GenRpaTask_t)); 88219e95205Sopenharmony_ci if (ctx == NULL) { 88319e95205Sopenharmony_ci LOG_ERROR("%{public}s: Alloc error.", __FUNCTION__); 88419e95205Sopenharmony_ci return SMP_ERR_OUT_OF_RES; 88519e95205Sopenharmony_ci } 88619e95205Sopenharmony_ci (void)memset_s(ctx, sizeof(SMP_GenRpaTask_t), 0x00, sizeof(SMP_GenRpaTask_t)); 88719e95205Sopenharmony_ci (void)memcpy_s(ctx->irk, sizeof(ctx->irk), irk, SMP_IRK_LEN); 88819e95205Sopenharmony_ci int ret = BTM_RunTaskInProcessingQueue(PROCESSING_QUEUE_ID_SMP, SMP_GenRpaTask, (void *)ctx); 88919e95205Sopenharmony_ci if (ret != SMP_SUCCESS) { 89019e95205Sopenharmony_ci MEM_MALLOC.free(ctx); 89119e95205Sopenharmony_ci return ret; 89219e95205Sopenharmony_ci } 89319e95205Sopenharmony_ci return ret; 89419e95205Sopenharmony_ci} 89519e95205Sopenharmony_ci 89619e95205Sopenharmony_cistatic void SMP_GenRpaTask(void *context) 89719e95205Sopenharmony_ci{ 89819e95205Sopenharmony_ci uint32_t number = RandomGenerate(); 89919e95205Sopenharmony_ci uint8_t address[BT_ADDRESS_SIZE] = {0x00}; 90019e95205Sopenharmony_ci uint8_t message[SMP_ENCRYPT_PLAINTEXTDATA_LEN] = {0x00}; 90119e95205Sopenharmony_ci SMP_EncCmd *encCmd = SMP_AllocEncCmd(); 90219e95205Sopenharmony_ci if (encCmd == NULL) { 90319e95205Sopenharmony_ci LOG_ERROR("%{public}s: Alloc error.", __FUNCTION__); 90419e95205Sopenharmony_ci return; 90519e95205Sopenharmony_ci } 90619e95205Sopenharmony_ci HciLeEncryptParam encryptParam; 90719e95205Sopenharmony_ci SMP_GenRpaTask_t *param = (SMP_GenRpaTask_t *)context; 90819e95205Sopenharmony_ci 90919e95205Sopenharmony_ci LOG_DEBUG("%{public}s", __FUNCTION__); 91019e95205Sopenharmony_ci (void)memcpy_s(address, sizeof(address), (uint8_t *)&number, SMP_RPA_HIGH_BIT_LEN); 91119e95205Sopenharmony_ci address[0x00] &= 0x7Fu; 91219e95205Sopenharmony_ci address[0x00] |= 0x40u; 91319e95205Sopenharmony_ci for (int i = 0; i < (BT_ADDRESS_SIZE - SMP_RPA_HIGH_BIT_LEN); i++) { 91419e95205Sopenharmony_ci message[sizeof(message) - SMP_RPA_HIGH_BIT_LEN + i] = address[i]; 91519e95205Sopenharmony_ci } 91619e95205Sopenharmony_ci (void)memcpy_s(encCmd->address, sizeof(encCmd->address), address, sizeof(encCmd->address)); 91719e95205Sopenharmony_ci (void)memcpy_s(encryptParam.key, sizeof(encryptParam.key), param->irk, sizeof(encryptParam.key)); 91819e95205Sopenharmony_ci SMP_MemoryReverseCopy(encryptParam.plaintextData, message, sizeof(message)); 91919e95205Sopenharmony_ci LOG_DEBUG("SMP_GENERATE_RPA_STEP_1 started."); 92019e95205Sopenharmony_ci int ret = SMP_SendLeEncryptCmd(&encryptParam, SMP_GENERATE_RPA_STEP_1, encCmd, SMP_USING_HW_AES128_RPA); 92119e95205Sopenharmony_ci (void)memset_s(encryptParam.key, SMP_ENCRYPT_KEY_LEN, 0x00, SMP_ENCRYPT_KEY_LEN); 92219e95205Sopenharmony_ci if (ret != SMP_SUCCESS) { 92319e95205Sopenharmony_ci SMP_NotifyCbGenRpa(SMP_GENERATE_RPA_STATUS_FAILED, address); 92419e95205Sopenharmony_ci } 92519e95205Sopenharmony_ci SMP_FreeEncCmd(encCmd); 92619e95205Sopenharmony_ci MEM_MALLOC.free(param); 92719e95205Sopenharmony_ci} 92819e95205Sopenharmony_ci 92919e95205Sopenharmony_ciint SMP_SetSecureConnOnlyMode(bool mode) 93019e95205Sopenharmony_ci{ 93119e95205Sopenharmony_ci LOG_INFO("%{public}s", __FUNCTION__); 93219e95205Sopenharmony_ci SMP_SetSecConnOnlyModeTask_t *ctx = MEM_MALLOC.alloc(sizeof(SMP_SetSecConnOnlyModeTask_t)); 93319e95205Sopenharmony_ci if (ctx == NULL) { 93419e95205Sopenharmony_ci LOG_ERROR("%{public}s: Alloc error.", __FUNCTION__); 93519e95205Sopenharmony_ci return SMP_ERR_OUT_OF_RES; 93619e95205Sopenharmony_ci } 93719e95205Sopenharmony_ci (void)memset_s(ctx, sizeof(SMP_SetSecConnOnlyModeTask_t), 0x00, sizeof(SMP_SetSecConnOnlyModeTask_t)); 93819e95205Sopenharmony_ci ctx->mode = mode; 93919e95205Sopenharmony_ci int ret = BTM_RunTaskInProcessingQueue(PROCESSING_QUEUE_ID_SMP, SMP_SetSecConnOnlyModeTask, (void *)ctx); 94019e95205Sopenharmony_ci if (ret != SMP_SUCCESS) { 94119e95205Sopenharmony_ci MEM_MALLOC.free(ctx); 94219e95205Sopenharmony_ci return ret; 94319e95205Sopenharmony_ci } 94419e95205Sopenharmony_ci return ret; 94519e95205Sopenharmony_ci} 94619e95205Sopenharmony_ci 94719e95205Sopenharmony_cistatic void SMP_SetSecConnOnlyModeTask(void *context) 94819e95205Sopenharmony_ci{ 94919e95205Sopenharmony_ci SMP_SetSecConnOnlyModeTask_t *param = (SMP_SetSecConnOnlyModeTask_t *)context; 95019e95205Sopenharmony_ci g_smpSecureConnOnlyMode = param->mode; 95119e95205Sopenharmony_ci MEM_MALLOC.free(param); 95219e95205Sopenharmony_ci} 95319e95205Sopenharmony_ci 95419e95205Sopenharmony_cibool SMP_GetSecureConnOnlyMode() 95519e95205Sopenharmony_ci{ 95619e95205Sopenharmony_ci bool mode = false; 95719e95205Sopenharmony_ci 95819e95205Sopenharmony_ci mode = g_smpSecureConnOnlyMode; 95919e95205Sopenharmony_ci 96019e95205Sopenharmony_ci return mode; 96119e95205Sopenharmony_ci} 96219e95205Sopenharmony_ci 96319e95205Sopenharmony_ciint SMP_SetIRK(const uint8_t *irk) 96419e95205Sopenharmony_ci{ 96519e95205Sopenharmony_ci LOG_INFO("%{public}s", __FUNCTION__); 96619e95205Sopenharmony_ci SMP_SetIrkTask_t *ctx = MEM_MALLOC.alloc(sizeof(SMP_SetIrkTask_t)); 96719e95205Sopenharmony_ci if (ctx == NULL) { 96819e95205Sopenharmony_ci LOG_ERROR("%{public}s: Alloc error.", __FUNCTION__); 96919e95205Sopenharmony_ci return SMP_ERR_OUT_OF_RES; 97019e95205Sopenharmony_ci } 97119e95205Sopenharmony_ci (void)memset_s(ctx, sizeof(SMP_SetIrkTask_t), 0x00, sizeof(SMP_SetIrkTask_t)); 97219e95205Sopenharmony_ci (void)memcpy_s(ctx->irk, sizeof(ctx->irk), irk, SMP_IRK_LEN); 97319e95205Sopenharmony_ci int ret = BTM_RunTaskInProcessingQueue(PROCESSING_QUEUE_ID_SMP, SMP_SetIrkTask, (void *)ctx); 97419e95205Sopenharmony_ci if (ret != SMP_SUCCESS) { 97519e95205Sopenharmony_ci MEM_MALLOC.free(ctx); 97619e95205Sopenharmony_ci return ret; 97719e95205Sopenharmony_ci } 97819e95205Sopenharmony_ci return ret; 97919e95205Sopenharmony_ci} 98019e95205Sopenharmony_ci 98119e95205Sopenharmony_cistatic void SMP_SetIrkTask(void *context) 98219e95205Sopenharmony_ci{ 98319e95205Sopenharmony_ci SMP_SetIrkTask_t *param = (SMP_SetIrkTask_t *)context; 98419e95205Sopenharmony_ci SMP_SetLocalIrk(param->irk, SMP_IRK_LEN); 98519e95205Sopenharmony_ci MEM_MALLOC.free(param); 98619e95205Sopenharmony_ci} 98719e95205Sopenharmony_ci 98819e95205Sopenharmony_ciint SMP_SetIdentAddr(const BtAddr *addr) 98919e95205Sopenharmony_ci{ 99019e95205Sopenharmony_ci LOG_INFO("%{public}s", __FUNCTION__); 99119e95205Sopenharmony_ci SMP_SetIdentAddrTask_t *ctx = MEM_MALLOC.alloc(sizeof(SMP_SetIdentAddrTask_t)); 99219e95205Sopenharmony_ci if (ctx == NULL) { 99319e95205Sopenharmony_ci LOG_ERROR("%{public}s: Alloc error.", __FUNCTION__); 99419e95205Sopenharmony_ci return SMP_ERR_OUT_OF_RES; 99519e95205Sopenharmony_ci } 99619e95205Sopenharmony_ci (void)memset_s(ctx, sizeof(SMP_SetIdentAddrTask_t), 0x00, sizeof(SMP_SetIdentAddrTask_t)); 99719e95205Sopenharmony_ci (void)memcpy_s(&ctx->addr, sizeof(ctx->addr), addr, sizeof(BtAddr)); 99819e95205Sopenharmony_ci int ret = BTM_RunTaskInProcessingQueue(PROCESSING_QUEUE_ID_SMP, SMP_SetIdentAddrTask, (void *)ctx); 99919e95205Sopenharmony_ci if (ret != SMP_SUCCESS) { 100019e95205Sopenharmony_ci MEM_MALLOC.free(ctx); 100119e95205Sopenharmony_ci return ret; 100219e95205Sopenharmony_ci } 100319e95205Sopenharmony_ci return ret; 100419e95205Sopenharmony_ci} 100519e95205Sopenharmony_ci 100619e95205Sopenharmony_cistatic void SMP_SetIdentAddrTask(void *context) 100719e95205Sopenharmony_ci{ 100819e95205Sopenharmony_ci SMP_SetIdentAddrTask_t *param = (SMP_SetIdentAddrTask_t *)context; 100919e95205Sopenharmony_ci SMP_SetLocalIdentAddr(¶m->addr); 101019e95205Sopenharmony_ci MEM_MALLOC.free(param); 101119e95205Sopenharmony_ci} 101219e95205Sopenharmony_ci 101319e95205Sopenharmony_civoid SMP_GeneratePairResult(uint16_t handle, uint8_t status, uint8_t failedReason, Alarm *cancelTimer) 101419e95205Sopenharmony_ci{ 101519e95205Sopenharmony_ci if (g_smpPairMng.state != SMP_STATE_PAIRING) { 101619e95205Sopenharmony_ci return; 101719e95205Sopenharmony_ci } 101819e95205Sopenharmony_ci 101919e95205Sopenharmony_ci if (status) { 102019e95205Sopenharmony_ci SMP_GenPairRetException(handle, status, failedReason, cancelTimer); 102119e95205Sopenharmony_ci } else { 102219e95205Sopenharmony_ci SMP_GenPairRetNormal(handle, status); 102319e95205Sopenharmony_ci } 102419e95205Sopenharmony_ci} 102519e95205Sopenharmony_ci 102619e95205Sopenharmony_ciint SMP_EncryptCompleteJudgeException(uint8_t status, uint8_t role) 102719e95205Sopenharmony_ci{ 102819e95205Sopenharmony_ci int ret = SMP_SUCCESS; 102919e95205Sopenharmony_ci 103019e95205Sopenharmony_ci if (g_smpPairMng.state != SMP_STATE_PAIRING) { 103119e95205Sopenharmony_ci LOG_ERROR("It's not pairing state."); 103219e95205Sopenharmony_ci ret = SMP_ERR_INVAL_STATE; 103319e95205Sopenharmony_ci return ret; 103419e95205Sopenharmony_ci } 103519e95205Sopenharmony_ci 103619e95205Sopenharmony_ci if (g_smpPairMng.role != role) { 103719e95205Sopenharmony_ci LOG_ERROR("Invalid role = %hhu.", g_smpPairMng.role); 103819e95205Sopenharmony_ci ret = SMP_ERR_INVAL_STATE; 103919e95205Sopenharmony_ci return ret; 104019e95205Sopenharmony_ci } 104119e95205Sopenharmony_ci 104219e95205Sopenharmony_ci if (status) { 104319e95205Sopenharmony_ci LOG_ERROR("returnParam->status = %hhu.", status); 104419e95205Sopenharmony_ci SMP_GeneratePairResult(g_smpPairMng.handle, SMP_PAIR_STATUS_FAILED, SMP_PAIR_FAILED_UNSPECIFIED_REASION, NULL); 104519e95205Sopenharmony_ci ret = SMP_ERR_INVAL_PARAM; 104619e95205Sopenharmony_ci return ret; 104719e95205Sopenharmony_ci } 104819e95205Sopenharmony_ci 104919e95205Sopenharmony_ci return ret; 105019e95205Sopenharmony_ci} 105119e95205Sopenharmony_ci 105219e95205Sopenharmony_cistatic int SMP_AuthReqReplyProcessMaster( 105319e95205Sopenharmony_ci bool accept, uint8_t rejectReason, uint8_t pairMethod, const uint8_t *entryValue) 105419e95205Sopenharmony_ci{ 105519e95205Sopenharmony_ci LOG_INFO("%{public}s", __FUNCTION__); 105619e95205Sopenharmony_ci int ret = SMP_AuthReqReplyJudgeExceptionMaster(accept, rejectReason); 105719e95205Sopenharmony_ci if (ret != SMP_SUCCESS) { 105819e95205Sopenharmony_ci return ret; 105919e95205Sopenharmony_ci } 106019e95205Sopenharmony_ci SMP_AuthReqReplyObtainEntryValue(pairMethod, entryValue); 106119e95205Sopenharmony_ci ret = SMP_AuthReqReplyStepTransMaster(pairMethod); 106219e95205Sopenharmony_ci 106319e95205Sopenharmony_ci return ret; 106419e95205Sopenharmony_ci} 106519e95205Sopenharmony_ci 106619e95205Sopenharmony_cistatic int SMP_AuthReqReplyJudgeExceptionMaster(bool accept, uint8_t rejectReason) 106719e95205Sopenharmony_ci{ 106819e95205Sopenharmony_ci int ret = SMP_SUCCESS; 106919e95205Sopenharmony_ci 107019e95205Sopenharmony_ci if ((g_smpPairMng.step != SMP_LEGACY_PAIR_MASTER_STEP_2) && 107119e95205Sopenharmony_ci (g_smpPairMng.step != SMP_SC_PAIR_PASSKEYENTRY_MASTER_STEP_5) && 107219e95205Sopenharmony_ci (g_smpPairMng.step != SMP_SC_PAIR_JUSTWORKORNUMERIC_MASTER_STEP_14) && 107319e95205Sopenharmony_ci (g_smpPairMng.step != SMP_SC_PAIR_OOB_MASTER_STEP_7)) { 107419e95205Sopenharmony_ci ret = SMP_ERR_INVAL_STATE; 107519e95205Sopenharmony_ci LOG_ERROR("It's invalid step:%hu. ", g_smpPairMng.step); 107619e95205Sopenharmony_ci return ret; 107719e95205Sopenharmony_ci } 107819e95205Sopenharmony_ci AlarmCancel(g_smpPairMng.alarm); 107919e95205Sopenharmony_ci if (!accept) { 108019e95205Sopenharmony_ci LOG_ERROR("User rejected. "); 108119e95205Sopenharmony_ci ret = SMP_ERR_INVAL_STATE; 108219e95205Sopenharmony_ci SMP_GeneratePairResult(g_smpPairMng.handle, SMP_PAIR_STATUS_FAILED, rejectReason, NULL); 108319e95205Sopenharmony_ci return ret; 108419e95205Sopenharmony_ci } 108519e95205Sopenharmony_ci 108619e95205Sopenharmony_ci return ret; 108719e95205Sopenharmony_ci} 108819e95205Sopenharmony_ci 108919e95205Sopenharmony_cistatic int SMP_AuthReqReplyStepTransMaster(uint8_t pairMethod) 109019e95205Sopenharmony_ci{ 109119e95205Sopenharmony_ci int ret = SMP_SUCCESS; 109219e95205Sopenharmony_ci uint8_t cryptAesCmacZ[CRYPT_AESCMAC_Z_LEN] = {0x00}; 109319e95205Sopenharmony_ci HciLeEncryptParam encryptParam; 109419e95205Sopenharmony_ci if (g_smpPairMng.pairType == SMP_PAIR_TYPE_LEGACY) { 109519e95205Sopenharmony_ci ret = SMP_SendHciLeRandCmd(0x00); 109619e95205Sopenharmony_ci } else { 109719e95205Sopenharmony_ci if (SMP_IsPasskeyEntryPair(pairMethod)) { 109819e95205Sopenharmony_ci LOG_DEBUG("SMP_SC_PAIR_PASSKEYENTRY_MASTER_STEP_6 started."); 109919e95205Sopenharmony_ci ret = SMP_SendHciLeRandCmd(SMP_SC_PAIR_PASSKEYENTRY_MASTER_STEP_6); 110019e95205Sopenharmony_ci } else if (pairMethod == SMP_PAIR_METHOD_NUMERIC_COMPARISON) { 110119e95205Sopenharmony_ci SMP_MemoryReverseCopy(encryptParam.key, SALT, sizeof(encryptParam.key)); 110219e95205Sopenharmony_ci (void)memcpy_s( 110319e95205Sopenharmony_ci encryptParam.plaintextData, sizeof(encryptParam.plaintextData), cryptAesCmacZ, CRYPT_AESCMAC_Z_LEN); 110419e95205Sopenharmony_ci LOG_DEBUG("SMP_SC_PAIR_COMMON_MASTER_STEP_1 started."); 110519e95205Sopenharmony_ci ret = SMP_SendLeEncryptCmd(&encryptParam, SMP_SC_PAIR_COMMON_MASTER_STEP_1, NULL, SMP_USING_HW_AES128_PAIR); 110619e95205Sopenharmony_ci } else if (SMP_IsScOobPair(pairMethod)) { 110719e95205Sopenharmony_ci LOG_DEBUG("SMP_SC_PAIR_OOB_MASTER_STEP_8 started."); 110819e95205Sopenharmony_ci g_smpPairMng.step = SMP_SC_PAIR_OOB_MASTER_STEP_8; 110919e95205Sopenharmony_ci AlarmSet(g_smpPairMng.alarm, SMP_PAIR_WAIT_TIME, SMP_PairTimeout, NULL); 111019e95205Sopenharmony_ci ret = SMP_SendPairingPublicKey(g_smpPairMng.handle, g_smpPairMng.local.publicKey, SMP_SendDataCallback); 111119e95205Sopenharmony_ci if (ret != SMP_SUCCESS) { 111219e95205Sopenharmony_ci LOG_ERROR("Send pairing public key failed."); 111319e95205Sopenharmony_ci AlarmCancel(g_smpPairMng.alarm); 111419e95205Sopenharmony_ci } 111519e95205Sopenharmony_ci } 111619e95205Sopenharmony_ci } 111719e95205Sopenharmony_ci 111819e95205Sopenharmony_ci if (ret != SMP_SUCCESS) { 111919e95205Sopenharmony_ci SMP_GeneratePairResult(g_smpPairMng.handle, SMP_PAIR_STATUS_FAILED, SMP_PAIR_FAILED_UNSPECIFIED_REASION, NULL); 112019e95205Sopenharmony_ci } 112119e95205Sopenharmony_ci (void)memset_s(encryptParam.key, SMP_ENCRYPT_KEY_LEN, 0x00, SMP_ENCRYPT_KEY_LEN); 112219e95205Sopenharmony_ci return ret; 112319e95205Sopenharmony_ci} 112419e95205Sopenharmony_ci 112519e95205Sopenharmony_cistatic int SMP_AuthReqReplyProcessSlave( 112619e95205Sopenharmony_ci bool accept, uint8_t rejectReason, uint8_t pairMethod, const uint8_t *entryValue) 112719e95205Sopenharmony_ci{ 112819e95205Sopenharmony_ci LOG_INFO("%{public}s", __FUNCTION__); 112919e95205Sopenharmony_ci int ret = SMP_AuthReqReplyJudgeExceptionSlave(accept, rejectReason); 113019e95205Sopenharmony_ci if (ret != SMP_SUCCESS) { 113119e95205Sopenharmony_ci return ret; 113219e95205Sopenharmony_ci } 113319e95205Sopenharmony_ci SMP_AuthReqReplyObtainEntryValue(pairMethod, entryValue); 113419e95205Sopenharmony_ci ret = SMP_AuthReqReplyStepTransSlave(pairMethod); 113519e95205Sopenharmony_ci 113619e95205Sopenharmony_ci return ret; 113719e95205Sopenharmony_ci} 113819e95205Sopenharmony_ci 113919e95205Sopenharmony_cistatic int SMP_AuthReqReplyJudgeExceptionSlave(bool accept, uint8_t rejectReason) 114019e95205Sopenharmony_ci{ 114119e95205Sopenharmony_ci int ret = SMP_SUCCESS; 114219e95205Sopenharmony_ci LOG_DEBUG("%{public}s", __FUNCTION__); 114319e95205Sopenharmony_ci if ((g_smpPairMng.step != SMP_LEGACY_PAIR_SLAVE_STEP_2) && 114419e95205Sopenharmony_ci (g_smpPairMng.step != SMP_SC_PAIR_PASSKEYENTRY_SLAVE_STEP_5) && 114519e95205Sopenharmony_ci (g_smpPairMng.step != SMP_SC_PAIR_JUSTWORKORNUMERIC_SLAVE_STEP_13) && 114619e95205Sopenharmony_ci (g_smpPairMng.step != SMP_SC_PAIR_OOB_SLAVE_STEP_7)) { 114719e95205Sopenharmony_ci ret = SMP_ERR_INVAL_STATE; 114819e95205Sopenharmony_ci LOG_ERROR("It's invalid step:%hu", g_smpPairMng.step); 114919e95205Sopenharmony_ci return ret; 115019e95205Sopenharmony_ci } 115119e95205Sopenharmony_ci AlarmCancel(g_smpPairMng.alarm); 115219e95205Sopenharmony_ci if (!accept) { 115319e95205Sopenharmony_ci ret = SMP_ERR_INVAL_STATE; 115419e95205Sopenharmony_ci SMP_GeneratePairResult(g_smpPairMng.handle, SMP_PAIR_STATUS_FAILED, rejectReason, NULL); 115519e95205Sopenharmony_ci LOG_ERROR("User rejected"); 115619e95205Sopenharmony_ci return ret; 115719e95205Sopenharmony_ci } 115819e95205Sopenharmony_ci 115919e95205Sopenharmony_ci return ret; 116019e95205Sopenharmony_ci} 116119e95205Sopenharmony_ci 116219e95205Sopenharmony_cistatic int SMP_AuthReqReplyStepTransSlave(uint8_t pairMethod) 116319e95205Sopenharmony_ci{ 116419e95205Sopenharmony_ci int ret = SMP_SUCCESS; 116519e95205Sopenharmony_ci LOG_DEBUG("%{public}s", __FUNCTION__); 116619e95205Sopenharmony_ci if (g_smpPairMng.pairType == SMP_PAIR_TYPE_LEGACY) { 116719e95205Sopenharmony_ci if (g_smpPairMng.slaveConfirmRecvFlag) { 116819e95205Sopenharmony_ci LOG_DEBUG("SMP_LEGACY_PAIR_SLAVE_STEP_4 started."); 116919e95205Sopenharmony_ci ret = SMP_SendHciLeRandCmd(SMP_LEGACY_PAIR_SLAVE_STEP_4); 117019e95205Sopenharmony_ci if (ret != SMP_SUCCESS) { 117119e95205Sopenharmony_ci SMP_GeneratePairResult( 117219e95205Sopenharmony_ci g_smpPairMng.handle, SMP_PAIR_STATUS_FAILED, SMP_PAIR_FAILED_UNSPECIFIED_REASION, NULL); 117319e95205Sopenharmony_ci } 117419e95205Sopenharmony_ci } else { 117519e95205Sopenharmony_ci LOG_DEBUG("SMP_LEGACY_PAIR_SLAVE_STEP_3 started."); 117619e95205Sopenharmony_ci g_smpPairMng.step = SMP_LEGACY_PAIR_SLAVE_STEP_3; 117719e95205Sopenharmony_ci AlarmSet(g_smpPairMng.alarm, SMP_PAIR_WAIT_TIME, SMP_PairTimeout, NULL); 117819e95205Sopenharmony_ci } 117919e95205Sopenharmony_ci } else { 118019e95205Sopenharmony_ci if (SMP_IsPasskeyEntryPair(pairMethod)) { 118119e95205Sopenharmony_ci ret = SMP_AuthReqReplyPasskeyEntrySlave(); 118219e95205Sopenharmony_ci } else if (pairMethod == SMP_PAIR_METHOD_NUMERIC_COMPARISON) { 118319e95205Sopenharmony_ci ret = SMP_AuthReqReplyNumericSlave(); 118419e95205Sopenharmony_ci } else if (SMP_IsScOobPair(pairMethod)) { 118519e95205Sopenharmony_ci ret = SMP_AuthReqReplyOobSlave(); 118619e95205Sopenharmony_ci } 118719e95205Sopenharmony_ci } 118819e95205Sopenharmony_ci 118919e95205Sopenharmony_ci return ret; 119019e95205Sopenharmony_ci} 119119e95205Sopenharmony_ci 119219e95205Sopenharmony_cistatic int SMP_AuthReqReplyNumericSlave() 119319e95205Sopenharmony_ci{ 119419e95205Sopenharmony_ci int ret = SMP_SUCCESS; 119519e95205Sopenharmony_ci HciLeEncryptParam encryptParam; 119619e95205Sopenharmony_ci LOG_DEBUG("%{public}s", __FUNCTION__); 119719e95205Sopenharmony_ci if (g_smpPairMng.slaveDHKeyCheckRecvFlag) { 119819e95205Sopenharmony_ci uint8_t cryptAesCmacZ[CRYPT_AESCMAC_Z_LEN] = {0x00}; 119919e95205Sopenharmony_ci SMP_MemoryReverseCopy(encryptParam.key, SALT, sizeof(encryptParam.key)); 120019e95205Sopenharmony_ci (void)memcpy_s( 120119e95205Sopenharmony_ci encryptParam.plaintextData, sizeof(encryptParam.plaintextData), cryptAesCmacZ, CRYPT_AESCMAC_Z_LEN); 120219e95205Sopenharmony_ci LOG_DEBUG("SMP_SC_PAIR_COMMON_SLAVE_STEP_2 started."); 120319e95205Sopenharmony_ci ret = SMP_SendLeEncryptCmd(&encryptParam, SMP_SC_PAIR_COMMON_SLAVE_STEP_2, NULL, SMP_USING_HW_AES128_PAIR); 120419e95205Sopenharmony_ci if (ret != SMP_SUCCESS) { 120519e95205Sopenharmony_ci SMP_GeneratePairResult( 120619e95205Sopenharmony_ci g_smpPairMng.handle, SMP_PAIR_STATUS_FAILED, SMP_PAIR_FAILED_UNSPECIFIED_REASION, NULL); 120719e95205Sopenharmony_ci } 120819e95205Sopenharmony_ci } else { 120919e95205Sopenharmony_ci LOG_DEBUG("SMP_SC_PAIR_COMMON_SLAVE_STEP_1 started."); 121019e95205Sopenharmony_ci g_smpPairMng.step = SMP_SC_PAIR_COMMON_SLAVE_STEP_1; 121119e95205Sopenharmony_ci AlarmSet(g_smpPairMng.alarm, SMP_PAIR_WAIT_TIME, SMP_PairTimeout, NULL); 121219e95205Sopenharmony_ci } 121319e95205Sopenharmony_ci (void)memset_s(encryptParam.key, SMP_ENCRYPT_KEY_LEN, 0x00, SMP_ENCRYPT_KEY_LEN); 121419e95205Sopenharmony_ci return ret; 121519e95205Sopenharmony_ci} 121619e95205Sopenharmony_ci 121719e95205Sopenharmony_cistatic int SMP_AuthReqReplyPasskeyEntrySlave() 121819e95205Sopenharmony_ci{ 121919e95205Sopenharmony_ci int ret = SMP_SUCCESS; 122019e95205Sopenharmony_ci LOG_DEBUG("%{public}s", __FUNCTION__); 122119e95205Sopenharmony_ci if (g_smpPairMng.slaveConfirmRecvFlag) { 122219e95205Sopenharmony_ci LOG_DEBUG("SMP_SC_PAIR_PASSKEYENTRY_SLAVE_STEP_7 started."); 122319e95205Sopenharmony_ci ret = SMP_SendHciLeRandCmd(SMP_SC_PAIR_PASSKEYENTRY_SLAVE_STEP_7); 122419e95205Sopenharmony_ci if (ret != SMP_SUCCESS) { 122519e95205Sopenharmony_ci SMP_GeneratePairResult( 122619e95205Sopenharmony_ci g_smpPairMng.handle, SMP_PAIR_STATUS_FAILED, SMP_PAIR_FAILED_UNSPECIFIED_REASION, NULL); 122719e95205Sopenharmony_ci } 122819e95205Sopenharmony_ci } else { 122919e95205Sopenharmony_ci LOG_DEBUG("SMP_SC_PAIR_PASSKEYENTRY_SLAVE_STEP_6 started."); 123019e95205Sopenharmony_ci g_smpPairMng.step = SMP_SC_PAIR_PASSKEYENTRY_SLAVE_STEP_6; 123119e95205Sopenharmony_ci AlarmSet(g_smpPairMng.alarm, SMP_PAIR_WAIT_TIME, SMP_PairTimeout, NULL); 123219e95205Sopenharmony_ci } 123319e95205Sopenharmony_ci 123419e95205Sopenharmony_ci return ret; 123519e95205Sopenharmony_ci} 123619e95205Sopenharmony_ci 123719e95205Sopenharmony_cistatic int SMP_AuthReqReplyOobSlave() 123819e95205Sopenharmony_ci{ 123919e95205Sopenharmony_ci int ret = SMP_SUCCESS; 124019e95205Sopenharmony_ci HciLeGenerateDHKeyParam DHKeyParam; 124119e95205Sopenharmony_ci 124219e95205Sopenharmony_ci if (g_smpPairMng.slavePubKeyRecvFlag) { 124319e95205Sopenharmony_ci g_smpPairMng.step = SMP_SC_PAIR_OOB_SLAVE_STEP_9; 124419e95205Sopenharmony_ci (void)memcpy_s( 124519e95205Sopenharmony_ci DHKeyParam.remoteP256PublicKey, SMP_PUBLICKEY_LEN, g_smpPairMng.peer.publicKey, SMP_PUBLICKEY_LEN); 124619e95205Sopenharmony_ci LOG_DEBUG("SMP_SC_PAIR_OOB_SLAVE_STEP_9 started. "); 124719e95205Sopenharmony_ci AlarmSet(g_smpPairMng.alarm, SMP_PAIR_WAIT_TIME, SMP_PairTimeout, NULL); 124819e95205Sopenharmony_ci ret = HCI_LeGenerateDHKey(&DHKeyParam); 124919e95205Sopenharmony_ci if (ret != SMP_SUCCESS) { 125019e95205Sopenharmony_ci LOG_ERROR("HCI_LeGenerateDHKey failed. "); 125119e95205Sopenharmony_ci SMP_GeneratePairResult( 125219e95205Sopenharmony_ci g_smpPairMng.handle, SMP_PAIR_STATUS_FAILED, SMP_PAIR_FAILED_UNSPECIFIED_REASION, g_smpPairMng.alarm); 125319e95205Sopenharmony_ci } 125419e95205Sopenharmony_ci } else { 125519e95205Sopenharmony_ci LOG_DEBUG("SMP_SC_PAIR_OOB_SLAVE_STEP_8 started. "); 125619e95205Sopenharmony_ci g_smpPairMng.step = SMP_SC_PAIR_OOB_SLAVE_STEP_8; 125719e95205Sopenharmony_ci AlarmSet(g_smpPairMng.alarm, SMP_PAIR_WAIT_TIME, SMP_PairTimeout, NULL); 125819e95205Sopenharmony_ci } 125919e95205Sopenharmony_ci 126019e95205Sopenharmony_ci return ret; 126119e95205Sopenharmony_ci} 126219e95205Sopenharmony_ci 126319e95205Sopenharmony_cistatic void SMP_AuthReqReplyObtainEntryValue(uint8_t pairMethod, const uint8_t *entryValue) 126419e95205Sopenharmony_ci{ 126519e95205Sopenharmony_ci if (SMP_IsPasskeyEntryPair(pairMethod)) { 126619e95205Sopenharmony_ci LOG_INFO("SMP_PAIR_METHOD_PASSKEY_ENTRY."); 126719e95205Sopenharmony_ci LOG_INFO("passkey is %u.", *((uint32_t *)entryValue)); 126819e95205Sopenharmony_ci (void)memset_s(g_smpPairMng.TK, SMP_TK_LEN, 0x00, sizeof(g_smpPairMng.TK)); 126919e95205Sopenharmony_ci for (int i = 0; i < (int)sizeof(*((uint32_t *)entryValue)); i++) { 127019e95205Sopenharmony_ci g_smpPairMng.TK[SMP_TK_LEN - 1 - i] = entryValue[i]; 127119e95205Sopenharmony_ci } 127219e95205Sopenharmony_ci } else if (pairMethod == SMP_PAIR_METHOD_OOB_LEGACY) { 127319e95205Sopenharmony_ci LOG_INFO("SMP_PAIR_METHOD_OOB_LEGACY."); 127419e95205Sopenharmony_ci (void)memcpy_s(g_smpPairMng.TK, SMP_TK_LEN, entryValue, SMP_TK_LEN); 127519e95205Sopenharmony_ci } else if ((pairMethod == SMP_PAIR_METHOD_OOB_SC_BOTH_SIDE_SEND_RECV) || 127619e95205Sopenharmony_ci (pairMethod == SMP_PAIR_METHOD_OOB_SC_LOCAL_RECV_PEER_SEND)) { 127719e95205Sopenharmony_ci LOG_INFO("SMP_PAIR_METHOD_OOB_SC_BOTH_SIDE_SEND_RECV or "); 127819e95205Sopenharmony_ci LOG_INFO("SMP_PAIR_METHOD_OOB_SC_LOCAL_RECV_PEER_SEND"); 127919e95205Sopenharmony_ci SMP_MemoryReverseCopy(g_smpPairMng.peer.addr.addr, entryValue, sizeof(g_smpPairMng.peer.addr.addr)); 128019e95205Sopenharmony_ci (void)memcpy_s(g_smpPairMng.peer.oobRand, 128119e95205Sopenharmony_ci SMP_RANDOM_DATA_LEN, 128219e95205Sopenharmony_ci entryValue + sizeof(g_smpPairMng.peer.addr.addr), 128319e95205Sopenharmony_ci SMP_RANDOM_DATA_LEN); 128419e95205Sopenharmony_ci SMP_MemoryReverseCopy(g_smpPairMng.peer.confirm, 128519e95205Sopenharmony_ci entryValue + sizeof(g_smpPairMng.peer.addr.addr) + SMP_RANDOM_DATA_LEN, 128619e95205Sopenharmony_ci SMP_CONFIRM_DATA_LEN); 128719e95205Sopenharmony_ci } 128819e95205Sopenharmony_ci} 128919e95205Sopenharmony_ci 129019e95205Sopenharmony_cistatic int SMP_PairReqReplyJudgeException( 129119e95205Sopenharmony_ci uint16_t handle, bool accept, uint8_t rejectReason, const SMP_PairParam *param) 129219e95205Sopenharmony_ci{ 129319e95205Sopenharmony_ci int ret = SMP_SUCCESS; 129419e95205Sopenharmony_ci 129519e95205Sopenharmony_ci if (g_smpPairMng.state != SMP_STATE_PAIRING) { 129619e95205Sopenharmony_ci LOG_ERROR("It's not pairing state. "); 129719e95205Sopenharmony_ci ret = SMP_ERR_INVAL_STATE; 129819e95205Sopenharmony_ci return ret; 129919e95205Sopenharmony_ci } 130019e95205Sopenharmony_ci if (g_smpPairMng.handle != handle) { 130119e95205Sopenharmony_ci LOG_ERROR("Connection handle error. "); 130219e95205Sopenharmony_ci ret = SMP_ERR_INVAL_PARAM; 130319e95205Sopenharmony_ci return ret; 130419e95205Sopenharmony_ci } 130519e95205Sopenharmony_ci if (g_smpPairMng.role != SMP_ROLE_SLAVE) { 130619e95205Sopenharmony_ci LOG_ERROR("It's not accepter role."); 130719e95205Sopenharmony_ci ret = SMP_ERR_INVAL_STATE; 130819e95205Sopenharmony_ci return ret; 130919e95205Sopenharmony_ci } 131019e95205Sopenharmony_ci if (g_smpPairMng.step != SMP_LEGACY_PAIR_SLAVE_STEP_1) { 131119e95205Sopenharmony_ci ret = SMP_ERR_INVAL_STATE; 131219e95205Sopenharmony_ci LOG_ERROR("It's not SMP_LEGACY_PAIR_SLAVE_STEP_1. "); 131319e95205Sopenharmony_ci return ret; 131419e95205Sopenharmony_ci } 131519e95205Sopenharmony_ci AlarmCancel(g_smpPairMng.alarm); 131619e95205Sopenharmony_ci 131719e95205Sopenharmony_ci if (!accept) { 131819e95205Sopenharmony_ci LOG_ERROR("User rejected. "); 131919e95205Sopenharmony_ci SMP_GeneratePairResult(handle, SMP_PAIR_STATUS_FAILED, rejectReason, NULL); 132019e95205Sopenharmony_ci ret = SMP_ERR_INVAL_STATE; 132119e95205Sopenharmony_ci return ret; 132219e95205Sopenharmony_ci } 132319e95205Sopenharmony_ci if (SMP_GetSecureConnOnlyMode() && (!(param->authReq & SMP_AUTH_REQ_BIT_SC))) { 132419e95205Sopenharmony_ci LOG_ERROR("SC bit is not set in authReq."); 132519e95205Sopenharmony_ci SMP_GeneratePairResult(handle, SMP_PAIR_STATUS_FAILED, SMP_PAIR_FAILED_UNSPECIFIED_REASION, NULL); 132619e95205Sopenharmony_ci ret = SMP_ERR_INVAL_PARAM; 132719e95205Sopenharmony_ci return ret; 132819e95205Sopenharmony_ci } 132919e95205Sopenharmony_ci 133019e95205Sopenharmony_ci return ret; 133119e95205Sopenharmony_ci} 133219e95205Sopenharmony_ci 133319e95205Sopenharmony_cistatic int SMP_PairReqReplyLegacyPair() 133419e95205Sopenharmony_ci{ 133519e95205Sopenharmony_ci int ret = SMP_SUCCESS; 133619e95205Sopenharmony_ci uint16_t handle = g_smpPairMng.handle; 133719e95205Sopenharmony_ci uint8_t pairMethod = g_smpPairMng.local.pairMethod; 133819e95205Sopenharmony_ci 133919e95205Sopenharmony_ci if (g_smpPairMng.local.pairMethod == SMP_PAIR_METHOD_JUST_WORK) { 134019e95205Sopenharmony_ci LOG_INFO("SMP_PAIR_METHOD_JUST_WORK."); 134119e95205Sopenharmony_ci (void)memset_s(g_smpPairMng.TK, SMP_TK_LEN, 0x00, sizeof(g_smpPairMng.TK)); 134219e95205Sopenharmony_ci LOG_DEBUG("SMP_LEGACY_PAIR_SLAVE_STEP_3 started."); 134319e95205Sopenharmony_ci g_smpPairMng.step = SMP_LEGACY_PAIR_SLAVE_STEP_3; 134419e95205Sopenharmony_ci AlarmSet(g_smpPairMng.alarm, SMP_PAIR_WAIT_TIME, SMP_PairTimeout, NULL); 134519e95205Sopenharmony_ci } else if (g_smpPairMng.local.pairMethod == SMP_PAIR_METHOD_PASSKEY_DISPLAY) { 134619e95205Sopenharmony_ci LOG_INFO("SMP_PAIR_METHOD_PASSKEY_DISPLAY."); 134719e95205Sopenharmony_ci g_smpPairMng.slaveConfirmRecvFlag = SMP_SLAVE_CONFIRM_RECV_FLAG_NO; 134819e95205Sopenharmony_ci ret = SMP_SendHciLeRandCmd(0x00); 134919e95205Sopenharmony_ci if (ret != SMP_SUCCESS) { 135019e95205Sopenharmony_ci SMP_GeneratePairResult( 135119e95205Sopenharmony_ci g_smpPairMng.handle, SMP_PAIR_STATUS_FAILED, SMP_PAIR_FAILED_UNSPECIFIED_REASION, NULL); 135219e95205Sopenharmony_ci } 135319e95205Sopenharmony_ci } else { 135419e95205Sopenharmony_ci LOG_DEBUG("SMP_LEGACY_PAIR_SLAVE_STEP_2 started."); 135519e95205Sopenharmony_ci g_smpPairMng.slaveConfirmRecvFlag = SMP_SLAVE_CONFIRM_RECV_FLAG_NO; 135619e95205Sopenharmony_ci g_smpPairMng.step = SMP_LEGACY_PAIR_SLAVE_STEP_2; 135719e95205Sopenharmony_ci AlarmSet(g_smpPairMng.alarm, SMP_PAIR_WAIT_TIME, SMP_PairTimeout, NULL); 135819e95205Sopenharmony_ci SMP_NotifyCbAuthReq(handle, pairMethod, NULL); 135919e95205Sopenharmony_ci } 136019e95205Sopenharmony_ci return ret; 136119e95205Sopenharmony_ci} 136219e95205Sopenharmony_ci 136319e95205Sopenharmony_cistatic int SMP_PairReqReplyScPair() 136419e95205Sopenharmony_ci{ 136519e95205Sopenharmony_ci int ret = SMP_SUCCESS; 136619e95205Sopenharmony_ci 136719e95205Sopenharmony_ci if ((SMP_IsPasskeyEntryPair(g_smpPairMng.local.pairMethod)) || 136819e95205Sopenharmony_ci (SMP_IsJustworkOrNumericPair(g_smpPairMng.local.pairMethod))) { 136919e95205Sopenharmony_ci LOG_INFO("It's not OOB"); 137019e95205Sopenharmony_ci if (SMP_IsJustworkOrNumericPair(g_smpPairMng.local.pairMethod)) { 137119e95205Sopenharmony_ci LOG_DEBUG("SMP_SC_PAIR_JUSTWORKORNUMERIC_SLAVE_STEP_1 started."); 137219e95205Sopenharmony_ci g_smpPairMng.step = SMP_SC_PAIR_JUSTWORKORNUMERIC_SLAVE_STEP_1; 137319e95205Sopenharmony_ci AlarmSet(g_smpPairMng.alarm, SMP_PAIR_WAIT_TIME, SMP_PairTimeout, NULL); 137419e95205Sopenharmony_ci } else { 137519e95205Sopenharmony_ci g_smpPairMng.scConfirmCheckCounter = 0x00; 137619e95205Sopenharmony_ci g_smpPairMng.slaveConfirmRecvFlag = SMP_SLAVE_CONFIRM_RECV_FLAG_NO; 137719e95205Sopenharmony_ci LOG_DEBUG("SMP_SC_PAIR_PASSKEYENTRY_SLAVE_STEP_1 started."); 137819e95205Sopenharmony_ci g_smpPairMng.step = SMP_SC_PAIR_PASSKEYENTRY_SLAVE_STEP_1; 137919e95205Sopenharmony_ci AlarmSet(g_smpPairMng.alarm, SMP_PAIR_WAIT_TIME, SMP_PairTimeout, NULL); 138019e95205Sopenharmony_ci } 138119e95205Sopenharmony_ci } else { 138219e95205Sopenharmony_ci LOG_INFO("It's OOB."); 138319e95205Sopenharmony_ci ret = SMP_PairReqReplyScPairOob(); 138419e95205Sopenharmony_ci } 138519e95205Sopenharmony_ci 138619e95205Sopenharmony_ci return ret; 138719e95205Sopenharmony_ci} 138819e95205Sopenharmony_ci 138919e95205Sopenharmony_cistatic int SMP_PairReqReplyScPairOob() 139019e95205Sopenharmony_ci{ 139119e95205Sopenharmony_ci int ret = SMP_SUCCESS; 139219e95205Sopenharmony_ci 139319e95205Sopenharmony_ci if (g_smpPairMng.local.pairMethod == SMP_PAIR_METHOD_OOB_SC_LOCAL_SEND_PEER_RECV) { 139419e95205Sopenharmony_ci (void)memset_s(g_smpPairMng.peer.oobRand, SMP_RANDOM_DATA_LEN, 0x00, sizeof(g_smpPairMng.peer.oobRand)); 139519e95205Sopenharmony_ci } else if (g_smpPairMng.local.pairMethod == SMP_PAIR_METHOD_OOB_SC_LOCAL_RECV_PEER_SEND) { 139619e95205Sopenharmony_ci (void)memset_s(g_smpPairMng.local.oobRand, SMP_RANDOM_DATA_LEN, 0x00, sizeof(g_smpPairMng.local.oobRand)); 139719e95205Sopenharmony_ci LOG_DEBUG("SMP_SC_PAIR_OOB_SLAVE_STEP_1 started"); 139819e95205Sopenharmony_ci g_smpPairMng.step = SMP_SC_PAIR_OOB_SLAVE_STEP_1; 139919e95205Sopenharmony_ci g_smpPairMng.slavePubKeyRecvFlag = SMP_SLAVE_PUBLICKEY_RECV_FLAG_NO; 140019e95205Sopenharmony_ci AlarmSet(g_smpPairMng.alarm, SMP_PAIR_WAIT_TIME, SMP_PairTimeout, NULL); 140119e95205Sopenharmony_ci ret = HCI_LeReadLocalP256PublicKey(); 140219e95205Sopenharmony_ci if (ret != SMP_SUCCESS) { 140319e95205Sopenharmony_ci SMP_GeneratePairResult( 140419e95205Sopenharmony_ci g_smpPairMng.handle, SMP_PAIR_STATUS_FAILED, SMP_PAIR_FAILED_UNSPECIFIED_REASION, g_smpPairMng.alarm); 140519e95205Sopenharmony_ci LOG_ERROR("HCI_LeReadLocalP256PublicKey failed"); 140619e95205Sopenharmony_ci } 140719e95205Sopenharmony_ci } 140819e95205Sopenharmony_ci 140919e95205Sopenharmony_ci if (g_smpPairMng.local.pairMethod != SMP_PAIR_METHOD_OOB_SC_LOCAL_RECV_PEER_SEND) { 141019e95205Sopenharmony_ci uint8_t scOobData[SMP_SC_OOB_LEN] = {0x00}; 141119e95205Sopenharmony_ci SMP_MemoryReverseCopy(g_smpPairMng.local.oobRand, SMP_GetScOobMng()->random, SMP_RANDOM_DATA_LEN); 141219e95205Sopenharmony_ci SMP_MemoryReverseCopy(scOobData, g_smpPairMng.local.addr.addr, sizeof(g_smpPairMng.local.addr.addr)); 141319e95205Sopenharmony_ci SMP_MemoryReverseCopy( 141419e95205Sopenharmony_ci scOobData + sizeof(g_smpPairMng.local.addr.addr), SMP_GetScOobMng()->random, SMP_RANDOM_DATA_LEN); 141519e95205Sopenharmony_ci SMP_MemoryReverseCopy(scOobData + sizeof(g_smpPairMng.local.addr.addr) + SMP_RANDOM_DATA_LEN, 141619e95205Sopenharmony_ci SMP_GetScOobMng()->confirm, 141719e95205Sopenharmony_ci SMP_CONFIRM_DATA_LEN); 141819e95205Sopenharmony_ci (void)memcpy_s( 141919e95205Sopenharmony_ci g_smpPairMng.local.publicKey, SMP_PUBLICKEY_LEN, SMP_GetScOobMng()->publicKey, SMP_PUBLICKEY_LEN); 142019e95205Sopenharmony_ci LOG_DEBUG("SMP_SC_PAIR_OOB_SLAVE_STEP_7 started."); 142119e95205Sopenharmony_ci g_smpPairMng.step = SMP_SC_PAIR_OOB_SLAVE_STEP_7; 142219e95205Sopenharmony_ci AlarmSet(g_smpPairMng.alarm, SMP_PAIR_WAIT_TIME, SMP_PairTimeout, NULL); 142319e95205Sopenharmony_ci SMP_NotifyCbAuthReq(g_smpPairMng.handle, g_smpPairMng.local.pairMethod, scOobData); 142419e95205Sopenharmony_ci } 142519e95205Sopenharmony_ci 142619e95205Sopenharmony_ci return ret; 142719e95205Sopenharmony_ci} 142819e95205Sopenharmony_ci 142919e95205Sopenharmony_cistatic int SMP_PairRspReplyJudgeException(uint16_t handle, bool accept, uint8_t rejectReason) 143019e95205Sopenharmony_ci{ 143119e95205Sopenharmony_ci int ret = SMP_SUCCESS; 143219e95205Sopenharmony_ci 143319e95205Sopenharmony_ci if (g_smpPairMng.state != SMP_STATE_PAIRING) { 143419e95205Sopenharmony_ci LOG_ERROR("It's not pairing state"); 143519e95205Sopenharmony_ci ret = SMP_ERR_INVAL_STATE; 143619e95205Sopenharmony_ci return ret; 143719e95205Sopenharmony_ci } 143819e95205Sopenharmony_ci if (g_smpPairMng.handle != handle) { 143919e95205Sopenharmony_ci LOG_ERROR("Connection handle error"); 144019e95205Sopenharmony_ci ret = SMP_ERR_INVAL_PARAM; 144119e95205Sopenharmony_ci return ret; 144219e95205Sopenharmony_ci } 144319e95205Sopenharmony_ci if (g_smpPairMng.role != SMP_ROLE_MASTER) { 144419e95205Sopenharmony_ci LOG_ERROR("It's not initiator role."); 144519e95205Sopenharmony_ci ret = SMP_ERR_INVAL_STATE; 144619e95205Sopenharmony_ci return ret; 144719e95205Sopenharmony_ci } 144819e95205Sopenharmony_ci if (g_smpPairMng.step != SMP_LEGACY_PAIR_MASTER_STEP_1) { 144919e95205Sopenharmony_ci ret = SMP_ERR_INVAL_STATE; 145019e95205Sopenharmony_ci LOG_ERROR("It's not SMP_LEGACY_PAIR_MASTER_STEP_1"); 145119e95205Sopenharmony_ci return ret; 145219e95205Sopenharmony_ci } 145319e95205Sopenharmony_ci AlarmCancel(g_smpPairMng.alarm); 145419e95205Sopenharmony_ci 145519e95205Sopenharmony_ci if (!accept) { 145619e95205Sopenharmony_ci LOG_ERROR("User rejected"); 145719e95205Sopenharmony_ci SMP_GeneratePairResult(handle, SMP_PAIR_STATUS_FAILED, rejectReason, NULL); 145819e95205Sopenharmony_ci ret = SMP_ERR_INVAL_STATE; 145919e95205Sopenharmony_ci return ret; 146019e95205Sopenharmony_ci } 146119e95205Sopenharmony_ci 146219e95205Sopenharmony_ci return ret; 146319e95205Sopenharmony_ci} 146419e95205Sopenharmony_ci 146519e95205Sopenharmony_cistatic int SMP_PairRspReplyLegacyPair() 146619e95205Sopenharmony_ci{ 146719e95205Sopenharmony_ci int ret = SMP_SUCCESS; 146819e95205Sopenharmony_ci uint16_t handle = g_smpPairMng.handle; 146919e95205Sopenharmony_ci uint8_t pairMethod = g_smpPairMng.local.pairMethod; 147019e95205Sopenharmony_ci 147119e95205Sopenharmony_ci if (g_smpPairMng.local.pairMethod == SMP_PAIR_METHOD_JUST_WORK) { 147219e95205Sopenharmony_ci LOG_INFO("SMP_PAIR_METHOD_JUST_WORK."); 147319e95205Sopenharmony_ci LOG_DEBUG("SMP_LEGACY_PAIR_MASTER_STEP_2 started."); 147419e95205Sopenharmony_ci (void)memset_s(g_smpPairMng.TK, SMP_TK_LEN, 0x00, sizeof(g_smpPairMng.TK)); 147519e95205Sopenharmony_ci ret = SMP_SendHciLeRandCmd(SMP_LEGACY_PAIR_MASTER_STEP_2); 147619e95205Sopenharmony_ci if (ret != SMP_SUCCESS) { 147719e95205Sopenharmony_ci SMP_GeneratePairResult( 147819e95205Sopenharmony_ci g_smpPairMng.handle, SMP_PAIR_STATUS_FAILED, SMP_PAIR_FAILED_UNSPECIFIED_REASION, NULL); 147919e95205Sopenharmony_ci } 148019e95205Sopenharmony_ci } else if (g_smpPairMng.local.pairMethod == SMP_PAIR_METHOD_PASSKEY_DISPLAY) { 148119e95205Sopenharmony_ci LOG_INFO("SMP_PAIR_METHOD_PASSKEY_DISPLAY."); 148219e95205Sopenharmony_ci ret = SMP_SendHciLeRandCmd(0x00); 148319e95205Sopenharmony_ci if (ret != SMP_SUCCESS) { 148419e95205Sopenharmony_ci SMP_GeneratePairResult( 148519e95205Sopenharmony_ci g_smpPairMng.handle, SMP_PAIR_STATUS_FAILED, SMP_PAIR_FAILED_UNSPECIFIED_REASION, NULL); 148619e95205Sopenharmony_ci } 148719e95205Sopenharmony_ci } else { 148819e95205Sopenharmony_ci LOG_INFO("Other pair method."); 148919e95205Sopenharmony_ci LOG_DEBUG("SMP_LEGACY_PAIR_MASTER_STEP_2 started."); 149019e95205Sopenharmony_ci g_smpPairMng.step = SMP_LEGACY_PAIR_MASTER_STEP_2; 149119e95205Sopenharmony_ci AlarmSet(g_smpPairMng.alarm, SMP_PAIR_WAIT_TIME, SMP_PairTimeout, NULL); 149219e95205Sopenharmony_ci SMP_NotifyCbAuthReq(handle, pairMethod, NULL); 149319e95205Sopenharmony_ci } 149419e95205Sopenharmony_ci 149519e95205Sopenharmony_ci return ret; 149619e95205Sopenharmony_ci} 149719e95205Sopenharmony_ci 149819e95205Sopenharmony_cistatic int SMP_PairRspReplyScPair() 149919e95205Sopenharmony_ci{ 150019e95205Sopenharmony_ci int ret; 150119e95205Sopenharmony_ci 150219e95205Sopenharmony_ci if ((SMP_IsJustworkOrNumericPair(g_smpPairMng.local.pairMethod)) || 150319e95205Sopenharmony_ci (SMP_IsPasskeyEntryPair(g_smpPairMng.local.pairMethod))) { 150419e95205Sopenharmony_ci LOG_INFO("It's not OOB. "); 150519e95205Sopenharmony_ci if (SMP_IsJustworkOrNumericPair(g_smpPairMng.local.pairMethod)) { 150619e95205Sopenharmony_ci LOG_DEBUG("SMP_SC_PAIR_JUSTWORKORNUMERIC_MASTER_STEP_1 started."); 150719e95205Sopenharmony_ci g_smpPairMng.step = SMP_SC_PAIR_JUSTWORKORNUMERIC_MASTER_STEP_1; 150819e95205Sopenharmony_ci } else { 150919e95205Sopenharmony_ci LOG_DEBUG("SMP_SC_PAIR_PASSKEYENTRY_MASTER_STEP_1 started."); 151019e95205Sopenharmony_ci g_smpPairMng.step = SMP_SC_PAIR_PASSKEYENTRY_MASTER_STEP_1; 151119e95205Sopenharmony_ci } 151219e95205Sopenharmony_ci AlarmSet(g_smpPairMng.alarm, SMP_PAIR_WAIT_TIME, SMP_PairTimeout, NULL); 151319e95205Sopenharmony_ci ret = HCI_LeReadLocalP256PublicKey(); 151419e95205Sopenharmony_ci if (ret != SMP_SUCCESS) { 151519e95205Sopenharmony_ci LOG_ERROR("HCI_LeReadLocalP256PublicKey failed."); 151619e95205Sopenharmony_ci SMP_GeneratePairResult( 151719e95205Sopenharmony_ci g_smpPairMng.handle, SMP_PAIR_STATUS_FAILED, SMP_PAIR_FAILED_UNSPECIFIED_REASION, g_smpPairMng.alarm); 151819e95205Sopenharmony_ci } 151919e95205Sopenharmony_ci } else { 152019e95205Sopenharmony_ci LOG_INFO("It's OOB."); 152119e95205Sopenharmony_ci ret = SMP_PairRspReplyScPairOob(); 152219e95205Sopenharmony_ci } 152319e95205Sopenharmony_ci 152419e95205Sopenharmony_ci return ret; 152519e95205Sopenharmony_ci} 152619e95205Sopenharmony_ci 152719e95205Sopenharmony_cistatic int SMP_PairRspReplyScPairOob() 152819e95205Sopenharmony_ci{ 152919e95205Sopenharmony_ci int ret = SMP_SUCCESS; 153019e95205Sopenharmony_ci 153119e95205Sopenharmony_ci if (g_smpPairMng.local.pairMethod == SMP_PAIR_METHOD_OOB_SC_LOCAL_SEND_PEER_RECV) { 153219e95205Sopenharmony_ci (void)memset_s(g_smpPairMng.peer.oobRand, SMP_RANDOM_DATA_LEN, 0x00, SMP_RANDOM_DATA_LEN); 153319e95205Sopenharmony_ci } else if (g_smpPairMng.local.pairMethod == SMP_PAIR_METHOD_OOB_SC_LOCAL_RECV_PEER_SEND) { 153419e95205Sopenharmony_ci (void)memset_s(g_smpPairMng.local.oobRand, SMP_RANDOM_DATA_LEN, 0x00, SMP_RANDOM_DATA_LEN); 153519e95205Sopenharmony_ci LOG_DEBUG("SMP_SC_PAIR_OOB_MASTER_STEP_1 started."); 153619e95205Sopenharmony_ci g_smpPairMng.step = SMP_SC_PAIR_OOB_MASTER_STEP_1; 153719e95205Sopenharmony_ci AlarmSet(g_smpPairMng.alarm, SMP_PAIR_WAIT_TIME, SMP_PairTimeout, NULL); 153819e95205Sopenharmony_ci ret = HCI_LeReadLocalP256PublicKey(); 153919e95205Sopenharmony_ci if (ret != SMP_SUCCESS) { 154019e95205Sopenharmony_ci LOG_ERROR("HCI_LeReadLocalP256PublicKey failed."); 154119e95205Sopenharmony_ci SMP_GeneratePairResult( 154219e95205Sopenharmony_ci g_smpPairMng.handle, SMP_PAIR_STATUS_FAILED, SMP_PAIR_FAILED_UNSPECIFIED_REASION, g_smpPairMng.alarm); 154319e95205Sopenharmony_ci } 154419e95205Sopenharmony_ci } 154519e95205Sopenharmony_ci 154619e95205Sopenharmony_ci if (g_smpPairMng.local.pairMethod != SMP_PAIR_METHOD_OOB_SC_LOCAL_RECV_PEER_SEND) { 154719e95205Sopenharmony_ci uint8_t scOOBData[SMP_SC_OOB_LEN] = {0x00}; 154819e95205Sopenharmony_ci SMP_MemoryReverseCopy(g_smpPairMng.local.oobRand, SMP_GetScOobMng()->random, SMP_RANDOM_DATA_LEN); 154919e95205Sopenharmony_ci SMP_MemoryReverseCopy(scOOBData, g_smpPairMng.local.addr.addr, sizeof(g_smpPairMng.local.addr.addr)); 155019e95205Sopenharmony_ci SMP_MemoryReverseCopy( 155119e95205Sopenharmony_ci scOOBData + sizeof(g_smpPairMng.local.addr.addr), SMP_GetScOobMng()->random, SMP_RANDOM_DATA_LEN); 155219e95205Sopenharmony_ci SMP_MemoryReverseCopy(scOOBData + sizeof(g_smpPairMng.local.addr.addr) + SMP_RANDOM_DATA_LEN, 155319e95205Sopenharmony_ci SMP_GetScOobMng()->confirm, 155419e95205Sopenharmony_ci SMP_CONFIRM_DATA_LEN); 155519e95205Sopenharmony_ci (void)memcpy_s( 155619e95205Sopenharmony_ci g_smpPairMng.local.publicKey, SMP_PUBLICKEY_LEN, SMP_GetScOobMng()->publicKey, SMP_PUBLICKEY_LEN); 155719e95205Sopenharmony_ci LOG_DEBUG("SMP_SC_PAIR_OOB_MASTER_STEP_7 started."); 155819e95205Sopenharmony_ci g_smpPairMng.step = SMP_SC_PAIR_OOB_MASTER_STEP_7; 155919e95205Sopenharmony_ci AlarmSet(g_smpPairMng.alarm, SMP_PAIR_WAIT_TIME, SMP_PairTimeout, NULL); 156019e95205Sopenharmony_ci SMP_NotifyCbAuthReq(g_smpPairMng.handle, g_smpPairMng.local.pairMethod, scOOBData); 156119e95205Sopenharmony_ci } 156219e95205Sopenharmony_ci 156319e95205Sopenharmony_ci return ret; 156419e95205Sopenharmony_ci} 156519e95205Sopenharmony_ci 156619e95205Sopenharmony_cistatic void SMP_GenPairRetNormal(uint16_t handle, uint8_t status) 156719e95205Sopenharmony_ci{ 156819e95205Sopenharmony_ci SMP_PairResult pairResult; 156919e95205Sopenharmony_ci 157019e95205Sopenharmony_ci (void)memset_s(&pairResult, sizeof(SMP_PairResult), 0x00, sizeof(pairResult)); 157119e95205Sopenharmony_ci pairResult.pairType = g_smpPairMng.pairType; 157219e95205Sopenharmony_ci 157319e95205Sopenharmony_ci if ((g_smpPairMng.local.pairParam.authReq & SMP_AUTH_REQ_BONDING) && 157419e95205Sopenharmony_ci (g_smpPairMng.peer.pairParam.authReq & SMP_AUTH_REQ_BONDING)) { 157519e95205Sopenharmony_ci pairResult.bondedFlag = SMP_BONDED_FLAG_YES; 157619e95205Sopenharmony_ci if (g_smpPairMng.local.pairMethod != SMP_PAIR_METHOD_JUST_WORK) { 157719e95205Sopenharmony_ci pairResult.authFlag = SMP_AUTH_FLAG_YES; 157819e95205Sopenharmony_ci } else { 157919e95205Sopenharmony_ci pairResult.authFlag = SMP_AUTH_FLAG_NO; 158019e95205Sopenharmony_ci } 158119e95205Sopenharmony_ci pairResult.localKeyDist = g_smpPairMng.local.keyDist; 158219e95205Sopenharmony_ci pairResult.peerKeyDist = g_smpPairMng.peer.keyDist; 158319e95205Sopenharmony_ci (void)memcpy_s(pairResult.localLTK, SMP_LTK_LEN, g_smpPairMng.local.LTK, SMP_LTK_LEN); 158419e95205Sopenharmony_ci pairResult.localEdiv = g_smpPairMng.local.masterIdEdiv; 158519e95205Sopenharmony_ci (void)memcpy_s( 158619e95205Sopenharmony_ci pairResult.localRandom, SMP_MASTER_RAND_LEN, g_smpPairMng.local.masterIdRand, SMP_MASTER_RAND_LEN); 158719e95205Sopenharmony_ci (void)memcpy_s(pairResult.peerLTK, SMP_LTK_LEN, g_smpPairMng.peer.LTK, SMP_LTK_LEN); 158819e95205Sopenharmony_ci pairResult.peerEdiv = g_smpPairMng.peer.masterIdEdiv; 158919e95205Sopenharmony_ci (void)memcpy_s(pairResult.peerRandom, SMP_MASTER_RAND_LEN, g_smpPairMng.peer.masterIdRand, SMP_MASTER_RAND_LEN); 159019e95205Sopenharmony_ci (void)memcpy_s(pairResult.localIRK, SMP_IRK_LEN, g_smpPairMng.local.IRK, SMP_IRK_LEN); 159119e95205Sopenharmony_ci (void)memcpy_s(&pairResult.localIdentAddr, sizeof(BtAddr), &g_smpPairMng.local.identityAddr, sizeof(BtAddr)); 159219e95205Sopenharmony_ci (void)memcpy_s(pairResult.peerIRK, SMP_IRK_LEN, g_smpPairMng.peer.IRK, SMP_IRK_LEN); 159319e95205Sopenharmony_ci (void)memcpy_s(&pairResult.peerIdentAddr, sizeof(BtAddr), &g_smpPairMng.peer.identityAddr, sizeof(BtAddr)); 159419e95205Sopenharmony_ci (void)memcpy_s(pairResult.localCSRK, SMP_CSRK_LEN, g_smpPairMng.local.CSRK, SMP_CSRK_LEN); 159519e95205Sopenharmony_ci (void)memcpy_s(pairResult.peerCSRK, SMP_CSRK_LEN, g_smpPairMng.peer.CSRK, SMP_CSRK_LEN); 159619e95205Sopenharmony_ci pairResult.encKeySize = g_smpPairMng.encKeySize; 159719e95205Sopenharmony_ci } else { 159819e95205Sopenharmony_ci pairResult.bondedFlag = SMP_BONDED_FLAG_NO; 159919e95205Sopenharmony_ci } 160019e95205Sopenharmony_ci 160119e95205Sopenharmony_ci AlarmDelete(g_smpPairMng.alarm); 160219e95205Sopenharmony_ci (void)memset_s(&g_smpPairMng, sizeof(g_smpPairMng), 0x00, sizeof(g_smpPairMng)); 160319e95205Sopenharmony_ci SMP_NotifyCbPairRet(handle, status, &pairResult); 160419e95205Sopenharmony_ci} 160519e95205Sopenharmony_ci 160619e95205Sopenharmony_cistatic void SMP_GenPairRetException(uint16_t handle, uint8_t status, uint8_t failedReason, Alarm *cancelTimer) 160719e95205Sopenharmony_ci{ 160819e95205Sopenharmony_ci SMP_PairResult pairResult; 160919e95205Sopenharmony_ci (void)memset_s(&pairResult, sizeof(SMP_PairResult), 0x00, sizeof(pairResult)); 161019e95205Sopenharmony_ci if (cancelTimer != NULL) { 161119e95205Sopenharmony_ci AlarmCancel(cancelTimer); 161219e95205Sopenharmony_ci } 161319e95205Sopenharmony_ci if (failedReason) { 161419e95205Sopenharmony_ci SMP_SendPairingFailed(handle, failedReason, SMP_SendPairingFailedCallback); 161519e95205Sopenharmony_ci } 161619e95205Sopenharmony_ci AlarmDelete(g_smpPairMng.alarm); 161719e95205Sopenharmony_ci (void)memset_s(&g_smpPairMng, sizeof(g_smpPairMng), 0x00, sizeof(g_smpPairMng)); 161819e95205Sopenharmony_ci SMP_NotifyCbPairRet(handle, status, &pairResult); 161919e95205Sopenharmony_ci} 162019e95205Sopenharmony_ci 162119e95205Sopenharmony_cistatic void SMP_L2cifLeRegisterFixChannelCallback(uint16_t cid, int result) 162219e95205Sopenharmony_ci{ 162319e95205Sopenharmony_ci LOG_INFO("%{public}s", __FUNCTION__); 162419e95205Sopenharmony_ci if (result) { 162519e95205Sopenharmony_ci LOG_ERROR("L2CIF_LeRegisterFixChannel failed result = %{public}d", result); 162619e95205Sopenharmony_ci } 162719e95205Sopenharmony_ci (void)cid; 162819e95205Sopenharmony_ci (void)result; 162919e95205Sopenharmony_ci} 163019e95205Sopenharmony_ci 163119e95205Sopenharmony_cistatic void SMP_L2cifLeDeregisterFixChannelCallback(uint16_t cid, int result) 163219e95205Sopenharmony_ci{ 163319e95205Sopenharmony_ci LOG_INFO("%{public}s", __FUNCTION__); 163419e95205Sopenharmony_ci if (result) { 163519e95205Sopenharmony_ci LOG_ERROR("L2CIF_LeDeregisterFixChannel failed result = %{public}d", result); 163619e95205Sopenharmony_ci } 163719e95205Sopenharmony_ci (void)cid; 163819e95205Sopenharmony_ci (void)result; 163919e95205Sopenharmony_ci} 164019e95205Sopenharmony_ci 164119e95205Sopenharmony_civoid SMP_GenerateSignatureStep1(const SMP_StepParam *param) 164219e95205Sopenharmony_ci{ 164319e95205Sopenharmony_ci if (SMP_ParamIsNULL(param) != SMP_SUCCESS) { 164419e95205Sopenharmony_ci return; 164519e95205Sopenharmony_ci } 164619e95205Sopenharmony_ci SMP_EncData *encData = (SMP_EncData *)param->data; 164719e95205Sopenharmony_ci SMP_EncCmd *pEncCmdData = encData->encCmd; 164819e95205Sopenharmony_ci const HciLeEncryptReturnParam *returnParam = encData->encRetParam; 164919e95205Sopenharmony_ci SMP_CryptAesCmacStep2Param cryptAesCmacstep2Param; 165019e95205Sopenharmony_ci SMP_CryptAesCmacStep3Param cryptAesCmacstep3Param; 165119e95205Sopenharmony_ci SMP_CryptAesCmacStep1Param cryptAesCmacStep1parm; 165219e95205Sopenharmony_ci 165319e95205Sopenharmony_ci LOG_INFO("%{public}s ", __FUNCTION__); 165419e95205Sopenharmony_ci if (returnParam->status) { 165519e95205Sopenharmony_ci LOG_ERROR("returnParam->status = %hhu", returnParam->status); 165619e95205Sopenharmony_ci SMP_NotifyCbGenSign(SMP_GENERATE_SIGN_STATUS_FAILED, NULL); 165719e95205Sopenharmony_ci return; 165819e95205Sopenharmony_ci } 165919e95205Sopenharmony_ci SMP_MemoryReverseCopy(cryptAesCmacStep1parm.input, returnParam->encryptedData, sizeof(cryptAesCmacStep1parm.input)); 166019e95205Sopenharmony_ci SMP_CryptographicAesCmacStep1(&cryptAesCmacStep1parm); 166119e95205Sopenharmony_ci (void)memcpy_s( 166219e95205Sopenharmony_ci cryptAesCmacstep2Param.input1, CRYPT_AESCMAC_IN_LEN, cryptAesCmacStep1parm.output1, CRYPT_AESCMAC_IN_LEN); 166319e95205Sopenharmony_ci (void)memcpy_s( 166419e95205Sopenharmony_ci cryptAesCmacstep2Param.input2, CRYPT_AESCMAC_IN_LEN, cryptAesCmacStep1parm.output2, CRYPT_AESCMAC_IN_LEN); 166519e95205Sopenharmony_ci cryptAesCmacstep2Param.input3 = pEncCmdData->M; 166619e95205Sopenharmony_ci cryptAesCmacstep2Param.length = pEncCmdData->signDataLen; 166719e95205Sopenharmony_ci SMP_CryptographicAesCmacStep2(&cryptAesCmacstep2Param); 166819e95205Sopenharmony_ci cryptAesCmacstep3Param.stepA = SMP_GENERATE_SIGNATURE_STEP_2; 166919e95205Sopenharmony_ci cryptAesCmacstep3Param.stepB = SMP_GENERATE_SIGNATURE_STEP_3; 167019e95205Sopenharmony_ci cryptAesCmacstep3Param.message = pEncCmdData->M; 167119e95205Sopenharmony_ci cryptAesCmacstep3Param.messageSize = pEncCmdData->signDataLen; 167219e95205Sopenharmony_ci (void)memcpy_s(cryptAesCmacstep3Param.aesCmacOutput, 167319e95205Sopenharmony_ci CRYPT_AESCMAC_OUT_LEN, 167419e95205Sopenharmony_ci cryptAesCmacstep2Param.output, 167519e95205Sopenharmony_ci CRYPT_AESCMAC_OUT_LEN); 167619e95205Sopenharmony_ci (void)memcpy_s(cryptAesCmacstep3Param.key, CRYPT_AESCMAC_KEY_LEN, pEncCmdData->key, CRYPT_AESCMAC_KEY_LEN); 167719e95205Sopenharmony_ci cryptAesCmacstep3Param.n = cryptAesCmacstep2Param.n; 167819e95205Sopenharmony_ci cryptAesCmacstep3Param.signCounter = pEncCmdData->signCounter; 167919e95205Sopenharmony_ci cryptAesCmacstep3Param.isUsingHwAes128 = SMP_USING_HW_AES128_SIGN; 168019e95205Sopenharmony_ci int ret = SMP_CryptographicAesCmacStep3(&cryptAesCmacstep3Param); 168119e95205Sopenharmony_ci if (ret != SMP_SUCCESS) { 168219e95205Sopenharmony_ci SMP_NotifyCbGenSign(SMP_GENERATE_SIGN_STATUS_FAILED, NULL); 168319e95205Sopenharmony_ci } 168419e95205Sopenharmony_ci (void)memset_s(cryptAesCmacstep3Param.key, CRYPT_AESCMAC_KEY_LEN, 0x00, CRYPT_AESCMAC_KEY_LEN); 168519e95205Sopenharmony_ci} 168619e95205Sopenharmony_ci 168719e95205Sopenharmony_civoid SMP_GenerateSignatureStep2(const SMP_StepParam *param) 168819e95205Sopenharmony_ci{ 168919e95205Sopenharmony_ci if (SMP_ParamIsNULL(param) != SMP_SUCCESS) { 169019e95205Sopenharmony_ci return; 169119e95205Sopenharmony_ci } 169219e95205Sopenharmony_ci SMP_EncData *encData = (SMP_EncData *)param->data; 169319e95205Sopenharmony_ci SMP_CryptAesCmacStep4Param cryptAesCmacStep4Param; 169419e95205Sopenharmony_ci const HciLeEncryptReturnParam *returnParam = encData->encRetParam; 169519e95205Sopenharmony_ci 169619e95205Sopenharmony_ci LOG_INFO("%{public}s.", __FUNCTION__); 169719e95205Sopenharmony_ci 169819e95205Sopenharmony_ci if (returnParam->status) { 169919e95205Sopenharmony_ci LOG_ERROR("returnParam->status = %hhu ", returnParam->status); 170019e95205Sopenharmony_ci SMP_NotifyCbGenSign(SMP_GENERATE_SIGN_STATUS_FAILED, NULL); 170119e95205Sopenharmony_ci return; 170219e95205Sopenharmony_ci } 170319e95205Sopenharmony_ci 170419e95205Sopenharmony_ci cryptAesCmacStep4Param.stepA = SMP_GENERATE_SIGNATURE_STEP_2; 170519e95205Sopenharmony_ci cryptAesCmacStep4Param.stepB = SMP_GENERATE_SIGNATURE_STEP_3; 170619e95205Sopenharmony_ci cryptAesCmacStep4Param.pEncCmdData = encData->encCmd; 170719e95205Sopenharmony_ci (void)memcpy_s(cryptAesCmacStep4Param.X, 170819e95205Sopenharmony_ci sizeof(cryptAesCmacStep4Param.X), 170919e95205Sopenharmony_ci returnParam->encryptedData, 171019e95205Sopenharmony_ci sizeof(cryptAesCmacStep4Param.X)); 171119e95205Sopenharmony_ci cryptAesCmacStep4Param.isUsingHwAes128 = SMP_USING_HW_AES128_SIGN; 171219e95205Sopenharmony_ci int ret = SMP_CryptographicAesCmacStep4(&cryptAesCmacStep4Param); 171319e95205Sopenharmony_ci if (ret != SMP_SUCCESS) { 171419e95205Sopenharmony_ci SMP_NotifyCbGenSign(SMP_GENERATE_SIGN_STATUS_FAILED, NULL); 171519e95205Sopenharmony_ci } 171619e95205Sopenharmony_ci} 171719e95205Sopenharmony_ci 171819e95205Sopenharmony_civoid SMP_GenerateSignatureStep3(const SMP_StepParam *param) 171919e95205Sopenharmony_ci{ 172019e95205Sopenharmony_ci if (SMP_ParamIsNULL(param) != SMP_SUCCESS) { 172119e95205Sopenharmony_ci return; 172219e95205Sopenharmony_ci } 172319e95205Sopenharmony_ci SMP_EncData *encData = (SMP_EncData *)param->data; 172419e95205Sopenharmony_ci SMP_EncCmd *pEncCmdData = encData->encCmd; 172519e95205Sopenharmony_ci const HciLeEncryptReturnParam *returnParam = encData->encRetParam; 172619e95205Sopenharmony_ci uint8_t signature[SMP_SIGNATURE_LEN] = {0}; 172719e95205Sopenharmony_ci 172819e95205Sopenharmony_ci LOG_INFO("%{public}s", __FUNCTION__); 172919e95205Sopenharmony_ci 173019e95205Sopenharmony_ci if (returnParam->status) { 173119e95205Sopenharmony_ci LOG_ERROR("returnParam->status = %hhu. ", returnParam->status); 173219e95205Sopenharmony_ci SMP_NotifyCbGenSign(SMP_GENERATE_SIGN_STATUS_FAILED, NULL); 173319e95205Sopenharmony_ci return; 173419e95205Sopenharmony_ci } 173519e95205Sopenharmony_ci 173619e95205Sopenharmony_ci (void)memcpy_s(signature, sizeof(signature), (uint8_t *)&pEncCmdData->signCounter, SMP_SIGNCOUNTER_LEN); 173719e95205Sopenharmony_ci (void)memcpy_s(signature + SMP_SIGNCOUNTER_LEN, 173819e95205Sopenharmony_ci (SMP_SIGNATURE_LEN - SMP_SIGNCOUNTER_LEN), 173919e95205Sopenharmony_ci &returnParam->encryptedData[SMP_SIGNATURE_LEN - SMP_SIGNCOUNTER_LEN], 174019e95205Sopenharmony_ci (SMP_SIGNATURE_LEN - SMP_SIGNCOUNTER_LEN)); 174119e95205Sopenharmony_ci SMP_NotifyCbGenSign(SMP_GENERATE_SIGN_STATUS_SUCCESS, signature); 174219e95205Sopenharmony_ci} 174319e95205Sopenharmony_ci 174419e95205Sopenharmony_civoid SMP_NotifyCbAuthReq(uint16_t handle, uint8_t pairMethod, const uint8_t *displayValue) 174519e95205Sopenharmony_ci{ 174619e95205Sopenharmony_ci if (g_smpCallBack.SMP_CallbackAuthenticationRequest != NULL) { 174719e95205Sopenharmony_ci g_smpCallBack.SMP_CallbackAuthenticationRequest(handle, pairMethod, displayValue); 174819e95205Sopenharmony_ci } 174919e95205Sopenharmony_ci} 175019e95205Sopenharmony_ci 175119e95205Sopenharmony_civoid SMP_NotifyCbPairRet(uint16_t handle, uint8_t status, const SMP_PairResult *result) 175219e95205Sopenharmony_ci{ 175319e95205Sopenharmony_ci if (g_smpCallBack.SMP_CallbackPairResult != NULL) { 175419e95205Sopenharmony_ci g_smpCallBack.SMP_CallbackPairResult(handle, status, result); 175519e95205Sopenharmony_ci } 175619e95205Sopenharmony_ci} 175719e95205Sopenharmony_ci 175819e95205Sopenharmony_civoid SMP_NotifyCbPairReq(uint16_t handle, const SMP_PairParam *param) 175919e95205Sopenharmony_ci{ 176019e95205Sopenharmony_ci if (g_smpCallBack.SMP_CallbackRemotePairRequest != NULL) { 176119e95205Sopenharmony_ci g_smpCallBack.SMP_CallbackRemotePairRequest(handle, param); 176219e95205Sopenharmony_ci } 176319e95205Sopenharmony_ci} 176419e95205Sopenharmony_ci 176519e95205Sopenharmony_civoid SMP_NotifyCbPairRsp(uint16_t handle, const SMP_PairParam *param) 176619e95205Sopenharmony_ci{ 176719e95205Sopenharmony_ci if (g_smpCallBack.SMP_CallbackRemotePairResponse != NULL) { 176819e95205Sopenharmony_ci g_smpCallBack.SMP_CallbackRemotePairResponse(handle, param); 176919e95205Sopenharmony_ci } 177019e95205Sopenharmony_ci} 177119e95205Sopenharmony_ci 177219e95205Sopenharmony_civoid SMP_NotifyCbSecReq(uint16_t handle, uint8_t authReq) 177319e95205Sopenharmony_ci{ 177419e95205Sopenharmony_ci if (g_smpCallBack.SMP_CallbackRemoteSecurityRequest != NULL) { 177519e95205Sopenharmony_ci g_smpCallBack.SMP_CallbackRemoteSecurityRequest(handle, authReq); 177619e95205Sopenharmony_ci } 177719e95205Sopenharmony_ci} 177819e95205Sopenharmony_ci 177919e95205Sopenharmony_civoid SMP_NotifyCbLtkReq(uint16_t handle, const uint8_t *random, uint16_t ediv) 178019e95205Sopenharmony_ci{ 178119e95205Sopenharmony_ci if (g_smpCallBack.SMP_CallbackLongTermKeyRequest != NULL) { 178219e95205Sopenharmony_ci g_smpCallBack.SMP_CallbackLongTermKeyRequest(handle, random, ediv); 178319e95205Sopenharmony_ci } 178419e95205Sopenharmony_ci} 178519e95205Sopenharmony_ci 178619e95205Sopenharmony_civoid SMP_NotifyCbEncComp(uint16_t handle, uint8_t status) 178719e95205Sopenharmony_ci{ 178819e95205Sopenharmony_ci if (g_smpCallBack.SMP_CallbackEncryptionComplete != NULL) { 178919e95205Sopenharmony_ci g_smpCallBack.SMP_CallbackEncryptionComplete(handle, status); 179019e95205Sopenharmony_ci } 179119e95205Sopenharmony_ci} 179219e95205Sopenharmony_ci 179319e95205Sopenharmony_civoid SMP_NotifyCbGenSign(uint8_t status, const uint8_t *sign) 179419e95205Sopenharmony_ci{ 179519e95205Sopenharmony_ci if (g_smpCallBack.SMP_CallbackGenerateSignatureResult != NULL) { 179619e95205Sopenharmony_ci g_smpCallBack.SMP_CallbackGenerateSignatureResult(status, sign); 179719e95205Sopenharmony_ci } 179819e95205Sopenharmony_ci} 179919e95205Sopenharmony_ci 180019e95205Sopenharmony_civoid SMP_NotifyCbGenRpa(uint8_t status, const uint8_t *addr) 180119e95205Sopenharmony_ci{ 180219e95205Sopenharmony_ci if (g_smpCallBack.SMP_CallbackGenerateRPAResult != NULL) { 180319e95205Sopenharmony_ci g_smpCallBack.SMP_CallbackGenerateRPAResult(status, addr); 180419e95205Sopenharmony_ci } 180519e95205Sopenharmony_ci} 180619e95205Sopenharmony_ci 180719e95205Sopenharmony_civoid SMP_NotifyCbResoRpa(uint8_t status, bool result, const uint8_t *addr, const uint8_t *irk) 180819e95205Sopenharmony_ci{ 180919e95205Sopenharmony_ci if (g_smpCallBack.SMP_CallbackResolveRPAResult != NULL) { 181019e95205Sopenharmony_ci g_smpCallBack.SMP_CallbackResolveRPAResult(status, result, addr, irk); 181119e95205Sopenharmony_ci } 181219e95205Sopenharmony_ci} 181319e95205Sopenharmony_ci 181419e95205Sopenharmony_civoid SMP_NotifyCbGenScOobData(uint8_t status, const uint8_t *random, const uint8_t *confirm) 181519e95205Sopenharmony_ci{ 181619e95205Sopenharmony_ci if (g_smpCallBack.SMP_CallbackGenerateScOobDataResult != NULL) { 181719e95205Sopenharmony_ci g_smpCallBack.SMP_CallbackGenerateScOobDataResult(status, random, confirm); 181819e95205Sopenharmony_ci } 181919e95205Sopenharmony_ci} 182019e95205Sopenharmony_ci 182119e95205Sopenharmony_ciint SMP_ParamIsNULL(const SMP_StepParam *param) 182219e95205Sopenharmony_ci{ 182319e95205Sopenharmony_ci if (param == NULL) { 182419e95205Sopenharmony_ci return SMP_ERR_INVAL_PARAM; 182519e95205Sopenharmony_ci } 182619e95205Sopenharmony_ci if (param->data == NULL) { 182719e95205Sopenharmony_ci return SMP_ERR_INVAL_PARAM; 182819e95205Sopenharmony_ci } 182919e95205Sopenharmony_ci return SMP_SUCCESS; 183019e95205Sopenharmony_ci} 183119e95205Sopenharmony_ci 183219e95205Sopenharmony_ciint SMP_GenerateScOobData() 183319e95205Sopenharmony_ci{ 183419e95205Sopenharmony_ci LOG_INFO("%{public}s", __FUNCTION__); 183519e95205Sopenharmony_ci int ret = BTM_RunTaskInProcessingQueue(PROCESSING_QUEUE_ID_SMP, SMP_GenScOobDataTask, NULL); 183619e95205Sopenharmony_ci if (ret != SMP_SUCCESS) { 183719e95205Sopenharmony_ci return ret; 183819e95205Sopenharmony_ci } 183919e95205Sopenharmony_ci return ret; 184019e95205Sopenharmony_ci} 184119e95205Sopenharmony_ci 184219e95205Sopenharmony_cistatic void SMP_GenScOobDataTask(void *context) 184319e95205Sopenharmony_ci{ 184419e95205Sopenharmony_ci int ret = SMP_SUCCESS; 184519e95205Sopenharmony_ci 184619e95205Sopenharmony_ci LOG_INFO("%{public}s", __FUNCTION__); 184719e95205Sopenharmony_ci if (SMP_GetScOobMng()->state == SMP_STATE_SC_OOB_DATA_GENERATING) { 184819e95205Sopenharmony_ci LOG_ERROR("It's already SC OOB data generating state."); 184919e95205Sopenharmony_ci } else if (g_smpPairMng.state == SMP_STATE_PAIRING) { 185019e95205Sopenharmony_ci LOG_ERROR("It's already pairing state."); 185119e95205Sopenharmony_ci ret = SMP_ERR_INVAL_STATE; 185219e95205Sopenharmony_ci } else { 185319e95205Sopenharmony_ci SMP_ClearScOobData(false); 185419e95205Sopenharmony_ci SMP_GetScOobMng()->state = SMP_STATE_SC_OOB_DATA_GENERATING; 185519e95205Sopenharmony_ci SMP_GetScOobMng()->alarm = AlarmCreate("", false); 185619e95205Sopenharmony_ci LOG_DEBUG("SMP_GENERATE_SC_OOB_DATA_STEP_1 started."); 185719e95205Sopenharmony_ci SMP_GetScOobMng()->step = SMP_GENERATE_SC_OOB_DATA_STEP_1; 185819e95205Sopenharmony_ci AlarmSet(SMP_GetScOobMng()->alarm, SMP_GEN_SC_OOB_DATA_WAIT_TIME, SMP_GenerateScOobDataTimeout, NULL); 185919e95205Sopenharmony_ci ret = HCI_LeReadLocalP256PublicKey(); 186019e95205Sopenharmony_ci if (ret != SMP_SUCCESS) { 186119e95205Sopenharmony_ci LOG_ERROR("HCI_LeReadLocalP256PublicKey failed."); 186219e95205Sopenharmony_ci SMP_ClearScOobData(true); 186319e95205Sopenharmony_ci } 186419e95205Sopenharmony_ci } 186519e95205Sopenharmony_ci if (ret != SMP_SUCCESS) { 186619e95205Sopenharmony_ci SMP_NotifyCbGenScOobData(SMP_GENERATE_SC_OOB_DATA_FAILED, NULL, NULL); 186719e95205Sopenharmony_ci } 186819e95205Sopenharmony_ci (void)context; 186919e95205Sopenharmony_ci} 187019e95205Sopenharmony_ci 187119e95205Sopenharmony_ciSMP_PairMng *SMP_GetPairMng() 187219e95205Sopenharmony_ci{ 187319e95205Sopenharmony_ci return &g_smpPairMng; 187419e95205Sopenharmony_ci} 187519e95205Sopenharmony_ci 187619e95205Sopenharmony_cistatic Module g_smp = { 187719e95205Sopenharmony_ci .name = MODULE_NAME_SMP, 187819e95205Sopenharmony_ci .init = SMP_Initialize, 187919e95205Sopenharmony_ci .startup = SMP_Startup, 188019e95205Sopenharmony_ci .shutdown = SMP_Shutdown, 188119e95205Sopenharmony_ci .cleanup = SMP_Finalize, 188219e95205Sopenharmony_ci .dependencies = {MODULE_NAME_L2CAP}, 188319e95205Sopenharmony_ci}; 188419e95205Sopenharmony_ci 188519e95205Sopenharmony_ciMODULE_DECL(g_smp) 1886