2 Star 4 Fork 3

Kuroba_kaito/面向车载异构网络的密钥分配与消息认证方法

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
gateway.c 2.05 KB
一键复制 编辑 原始数据 按行查看 历史
Kuroba_kaito 提交于 6个月前 . 1
//
// Created by 猎人猪 on 2023/2/26.
//
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#include "common.h"
#include "gateway.h"
#include "sponge.h"
GW_store gw_store;
GW_temp_store gw_temp_store;
int ecu_count=0;
// 注册阶段-分配密钥
void gw_reg(unsigned char ID[2],unsigned char key[KEY_LENGTH_BYTES])
{
for(int i=0;i<2;++i)
{
gw_store.ecu_material[ecu_count].ID[i]=ID[i];
}
for(int i=0;i<KEY_LENGTH_BYTES;++i)
{
gw_store.ecu_material[ecu_count].kk[0][i]=key[i];
}
}
// 认证阶段-发送请求
AUTH_request gw_auth_req()
{
srand((unsigned) time(NULL));
AUTH_request request;
request.r1=rand();
gw_temp_store.r1=request.r1;
return request;
}
// 认证阶段-验证/更新密钥
AUTH_verify gw_auth_verify(AUTH_response response)
{
unsigned char hash_plain[SIZE_INT*2+KEY_LENGTH_BYTES];
unsigned char hash_result[SHA3_RESULT_LENGTH_BYTES];
unsigned char *p_temp;
memcpy(hash_plain,&gw_temp_store.r1,SIZE_INT);
memcpy(hash_plain+SIZE_INT,&response.r2,SIZE_INT);
memcpy(hash_plain+SIZE_INT*2, &gw_store.ecu_material[ecu_count].kk[response.f], KEY_LENGTH_BYTES);
p_temp= sponge(hash_plain, sizeof(hash_plain));
memcpy(hash_result,p_temp,SHA3_RESULT_LENGTH_BYTES);
if(memcmp(hash_result+KEY_LENGTH_BYTES,response.c2,KEY_LENGTH_BYTES))
{
cclPrintf("gateway: authentication failed!");
exit(-1);
}
else
{
cclPrintf("gateway: authentication passed!");
}
unsigned char new_f=(response.f+1)%2;
memcpy(gw_store.ecu_material[ecu_count].kk[new_f], hash_result, KEY_LENGTH_BYTES);
unsigned char sk[KEY_LENGTH_BYTES];
calculate_sk(hash_result+KEY_LENGTH_BYTES*2,hash_result+KEY_LENGTH_BYTES*3,sk);
cclPrintf("sk on gateway side is:");
char print_buf[KEY_LENGTH_BYTES * 2 + 1];
arrayToStr(sk, KEY_LENGTH_BYTES, print_buf);
print_buf[KEY_LENGTH_BYTES * 2] = '\0';
cclPrintf("%s", print_buf);
AUTH_verify verify;
memcpy(verify.c3,hash_result+KEY_LENGTH_BYTES*2,KEY_LENGTH_BYTES);
return verify;
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/kuroba-kaito/Key-Distribution-and-Message-Authentication-Method-for-Vehicular-Heterogeneous-Networks.git
git@gitee.com:kuroba-kaito/Key-Distribution-and-Message-Authentication-Method-for-Vehicular-Heterogeneous-Networks.git
kuroba-kaito
Key-Distribution-and-Message-Authentication-Method-for-Vehicular-Heterogeneous-Networks
面向车载异构网络的密钥分配与消息认证方法
master

搜索帮助