代码拉取完成,页面将自动刷新
//
// 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;
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。