-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClientApplication.c
More file actions
120 lines (95 loc) · 3.77 KB
/
Copy pathClientApplication.c
File metadata and controls
120 lines (95 loc) · 3.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
*/
#include <string.h> /* memset */
#include <stdio.h>
#include <err.h>
#include <tee_client_api.h>
static const TEEC_UUID uuid = {
0x12345678, 0x8765, 0x4321, { 'M', 'A', 'S', 'K', '0', '0', '0', '2'}
};
/* The TAFs ID implemented in this TA */
#define CMD_GEN_RANDOMS 0
#define CMD_DO_SIGN 1
int main()
{
TEEC_Result res;
TEEC_Context ctx;
TEEC_Session sess;
TEEC_Operation op;
uint32_t err_origin;
/* Initialize a context connecting us to the TEE */
res = TEEC_InitializeContext(NULL, &ctx);
if (res != TEEC_SUCCESS)
errx(1, "TEEC_InitializeContext failed with code 0x%x", res);
/*
* Open a session to the TA
*/
res = TEEC_OpenSession(&ctx, &sess, &uuid,
TEEC_LOGIN_PUBLIC, NULL, NULL, &err_origin);
if (res != TEEC_SUCCESS)
errx(1, "TEEC_Opensession failed with code 0x%x origin 0x%x",
res, err_origin);
memset(&op, 0, sizeof(op));
op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INOUT, TEEC_NONE,TEEC_NONE, TEEC_NONE);
int i, j;
unsigned char gened_mask[32];
char *privkey = "prkey.pem";
unsigned char *data;
unsigned int dataLen;
unsigned char sign[256];
char dt_to_sign[18]="";
char *sm_id;
char *sm_seq;
int consumption;
/* there should be at least 1 arg (consumption) for correct execution */
if ( argc < 2 )
{
/* We print argv[0] assuming it is the program name */
printf( "Usage: %s consumption id seq\n\
consumption: consumption to be masked, range 0-40000, required\n\
id: smart meter ID, 10 numerical digits, optional\n\
seq: mask sequence number, 3 numerical digits, optional\n", argv[0] );
}
else
{
consumption=atoi(argv[1]);
if ( argc > 2 )
sm_id = argv[2];
else
sm_id = "1520160001";
if ( argc > 3 )
sm_seq = argv[3];
else
sm_seq = "211";
do {
res = TEEC_InvokeCommand(&sess, CMD_GEN_RANDOMS, &op,
&err_origin);
if (res != TEEC_SUCCESS)
errx(1, "TEEC_InvokeCommand failed with code 0x%x origin 0x%x",
res, err_origin);
{
for(j = 0; (i < 40960 || i > 65535) && j<32 ;j=j+2)
{
i = (int)gened_mask[j] << 8 | (int)gened_mask[j+1];
i = i + consumption;
}
}
else
printf("Mask generation failed\n");
}while (i < 40960 || i > 65535);
char buf[5]="";
sprintf(buf, "%d", i);
strcpy(dt_to_sign, sm_id); // smart meter ID
strncat(dt_to_sign, buf, 5); // masked reading
strncat(dt_to_sign, sm_seq, 3); // seq No
printf("data to sign: %s\n", dt_to_sign);
data = (unsigned char *)dt_to_sign;
dataLen = strlen(dt_to_sign);
res = TEEC_InvokeCommand(&sess, CMD_DO_SIGN, &op,
&err_origin);
if (res != TEEC_SUCCESS)
errx(1, "TEEC_InvokeCommand failed with code 0x%x origin 0x%x",
res, err_origin);
TEEC_CloseSession(&sess);
TEEC_FinalizeContext(&ctx);
return 0;
}