blob: c6613bb8c5330bffb1fcd86f299c05c533207d43 [file] [log] [blame]
lh9ed821d2023-04-07 01:36:19 -07001/************************************************************************
2*¹¦ÄܽéÉÜ£ºÓû§Ä£Ê½ÇÐÑз¢Ä£Ê½¼øÈ¨
3*¸ºÔðÈË£º
4*±¸·ÝÈË£º
5*ÐÞ¸ÄÈÕ£º2019/9/20
6*ÐÞ¸ÄÄÚÈÝ£º
7*°æ±¾ºÅ£ºv1.0
8************************************************************************/
9#include <stdio.h>
10#include <unistd.h>
11#include <getopt.h>
12#include <sys/types.h>
13#include <sys/stat.h>
14#include <fcntl.h>
15#include <sys/ioctl.h>
16#include <strings.h>
17#include <sys/types.h>
18#include <assert.h>
19#include "os_type_def.h"
20#include "RONvParam_AMT.h"
21#include "libcpnv.h"
22
23int auth_device_key_init_value(void)
24{
25 /*³õʼֵΪ0´ò¿ªÑз¢Ä£Ê½ÐèÃÜÂëÇл»¹¦ÄÜ£¬Ä¬ÈÏΪ1²»´ò¿ª*/
26 //return 0;
27 return 1;
28}
29
30int verify_device_key(char *device_key,int key_len,char *seed,int seed_len)
31{
32 int verify_result = auth_device_key_init_value();
33 //char myInit[16] = "ZXIC-TSP";
34
35 //»ñÈ¡IMEIºÅ
36 unsigned int retCode = CPNV_ERROR;
37 unsigned char imei_bcd[8]= {0};
38 int abImeiindex = 0,imeistrindex = 0;
39 char imeistr[2*8] = {0};
40 int i = 0;
41
42 if((device_key == NULL)||(key_len != 6)||(seed == NULL)||(seed_len != 8))
43 {
44 printf("parameter error!\r\n");
45 return verify_result;
46 }
47 printf("parameter device key = %s,key_len=%d,seed = %s,seed_len=%d\r\n",device_key,key_len,seed,seed_len);
48 retCode = cpnv_NvItemRead(OS_FLASH_AMT_COMM_RO_IMEI_ADDRESS, (unsigned char *)imei_bcd, OS_FLASH_AMT_COMM_RO_IMEI_SIZE);
49 printf("retCode = %d\r\n",retCode );
50 if(CPNV_ERROR == retCode )
51 {
52 printf("read imei fail!\r\n");
53 return verify_result; //»ñÈ¡imeiºÅʧ°Ü
54 }
55 #if 0
56 for (i = 0; i < sizeof(imei_bcd); i++)
57 {
58 printf("imei_bcd[%d] = %02x\r\n",i,imei_bcd[i]);
59 }
60
61 for (abImeiindex=0; abImeiindex< 8-1; abImeiindex++,imeistrindex+=2)
62 {
63 imeistr[imeistrindex] = (imei_bcd[abImeiindex]&0x0F) + '0';
64 imeistr[imeistrindex+1] = ((imei_bcd[abImeiindex]&0xF0)>>4)+ '0';
65 }
66
67 imeistr[2*8-2]=(imei_bcd[8-1]&0x0F)+ '0';
68 imeistr[2*8-1] = '\0';
69
70 printf("imei = %s\r\n",imeistr);
71 #endif
72
73 imei_bcd[7] |= 0xF0;
74 for (i = 0; i < sizeof(imei_bcd); i++)
75 {
76 imei_bcd[i] += seed[i];
77 }
78
79 for (i = 0; i < sizeof(imei_bcd); i++)
80 {
81 printf("imei_bcd[%d] = %02x\r\n",i,imei_bcd[i]);
82 }
83
84 unsigned int crc = crc32(0, imei_bcd, sizeof(imei_bcd));
85 printf("crc = %02x\r\n",crc);
86
87 char buf[16] = { 0 };
88
89 for (i = 0; i < 8; i++)
90 {
91 buf[i] = ((crc >> (28 - i * 4)) & 0xF);
92 if (buf[i] > 9)
93 {
94 buf[i] = buf[i] + 'A' - 10;
95 }
96 else
97 {
98 buf[i] = buf[i] + '0';
99 }
100 }
101
102 //printf("buf = %s\r\n",buf);
103
104 printf("current device key = %s\r\n",buf+2);
105
106 if(strcmp(buf+2,device_key)== 0)
107 {
108 verify_result = 1;
109 printf("verify device key success!verify_result=%d\r\n",verify_result);
110 }
111 else
112 {
113 printf("verify device key fail!verify_result=%d\r\n",verify_result);
114 }
115
116 return verify_result;
117}
118
119
120