forked from IntelRealSense/RealSenseID
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrsid_signature_example.cc
196 lines (169 loc) · 7.07 KB
/
rsid_signature_example.cc
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
// License: Apache 2.0. See LICENSE file in root directory.
// Copyright(c) 2020-2021 Intel Corporation. All Rights Reserved.
#include "rsid_signature_example.h"
#define PRI_KEY_SIZE 32
#define PUB_X_Y_SIZE 32
#define PUB_KEY_SIZE 64
// Host's private key
static const unsigned char HOST_PRI_KEY[PRI_KEY_SIZE] = {
0xb9, 0xad, 0xfe, 0x0e, 0x6d, 0xd4, 0xfb, 0x6f, 0x76, 0xdf, 0x53, 0x92, 0x87, 0x4e, 0x58, 0x39,
0xdd, 0x51, 0xd1, 0xaa, 0x79, 0x94, 0x5e, 0xa8, 0x36, 0x8f, 0xb5, 0xdf, 0xa8, 0x28, 0x26, 0x53};
// Host's public key
static const unsigned char HOST_PUB_KEY[PUB_KEY_SIZE] = {
0xa9, 0x19, 0xcd, 0x93, 0x0f, 0xfb, 0x3e, 0x95, 0x5e, 0xf2, 0x94, 0xa5, 0x90, 0xca, 0x0e, 0x82,
0x19, 0x08, 0x72, 0x23, 0x8d, 0xec, 0x49, 0x97, 0xb4, 0x7d, 0x1c, 0x81, 0x6f, 0x18, 0x4e, 0xe7,
0x86, 0xf5, 0x69, 0x7a, 0xde, 0x6a, 0x69, 0xac, 0x64, 0xa2, 0xcd, 0xdf, 0x8c, 0xe1, 0x7a, 0xea,
0x4d, 0xf7, 0xc6, 0xd6, 0x10, 0xa2, 0xc5, 0x33, 0xe6, 0x0c, 0x2f, 0xce, 0x55, 0x6e, 0x1c, 0xf8};
// Device's public key
static const unsigned char DEVICE_PUB_KEY[PUB_KEY_SIZE] = {
0x22, 0x52, 0x97, 0x90, 0xdf, 0x5e, 0x11, 0xe7, 0xba, 0xe8, 0xa5, 0x62, 0xec, 0x06, 0xd9, 0x73,
0x20, 0x4b, 0x9b, 0x5a, 0x14, 0x55, 0x3c, 0x64, 0xd9, 0xa6, 0x12, 0xb3, 0xb0, 0xa2, 0xfb, 0x3f,
0x91, 0xeb, 0x49, 0xc0, 0x2c, 0x6b, 0x3e, 0x5e, 0xdc, 0x3c, 0x90, 0x7a, 0x5d, 0x80, 0x63, 0x73,
0xff, 0x6f, 0x77, 0xcf, 0x04, 0xa8, 0x8b, 0x30, 0xd2, 0x44, 0x2b, 0x91, 0xda, 0x3f, 0x62, 0xc3};
namespace RealSenseID
{
namespace Examples
{
SignClbk::SignClbk()
{
mbedtls_entropy_init(&_entropy);
mbedtls_ctr_drbg_init(&_ctr_drbg);
mbedtls_ecdsa_init(&_ecdsa_host_context);
mbedtls_ecdsa_init(&_ecdsa_device_context);
if (mbedtls_ctr_drbg_seed(&_ctr_drbg, mbedtls_entropy_func, &_entropy, NULL, 0))
return;
if (mbedtls_ecdsa_genkey(&_ecdsa_host_context, MBEDTLS_ECP_DP_SECP256R1, mbedtls_ctr_drbg_random, &_ctr_drbg))
return;
if (mbedtls_mpi_read_binary(&_ecdsa_host_context.d, HOST_PRI_KEY, PRI_KEY_SIZE))
return;
if (mbedtls_mpi_read_binary(&_ecdsa_host_context.Q.X, HOST_PUB_KEY, PUB_X_Y_SIZE))
return;
if (mbedtls_mpi_read_binary(&_ecdsa_host_context.Q.Y, HOST_PUB_KEY + PUB_X_Y_SIZE, PUB_X_Y_SIZE))
return;
if (mbedtls_ecdsa_genkey(&_ecdsa_device_context, MBEDTLS_ECP_DP_SECP256R1, mbedtls_ctr_drbg_random, &_ctr_drbg))
return;
if (mbedtls_mpi_read_binary(&_ecdsa_device_context.d, HOST_PRI_KEY, PRI_KEY_SIZE))
return;
if (mbedtls_mpi_read_binary(&_ecdsa_device_context.Q.X, DEVICE_PUB_KEY, PUB_X_Y_SIZE))
return;
if (mbedtls_mpi_read_binary(&_ecdsa_device_context.Q.Y, DEVICE_PUB_KEY + PUB_X_Y_SIZE, PUB_X_Y_SIZE))
return;
if (mbedtls_ctr_drbg_seed(&_ctr_drbg, mbedtls_entropy_func, &_entropy, NULL, 0))
return;
_initialized = true;
}
SignClbk::~SignClbk()
{
mbedtls_entropy_free(&_entropy);
mbedtls_ctr_drbg_free(&_ctr_drbg);
mbedtls_ecdsa_free(&_ecdsa_host_context);
mbedtls_ecdsa_free(&_ecdsa_device_context);
}
bool SignClbk::Sign(const unsigned char* buffer, const unsigned int buffer_len, unsigned char* out_sig)
{
if (!_initialized)
return false;
unsigned char signature[MBEDTLS_ECDSA_MAX_LEN];
size_t sign_len, lenTag;
int ret = mbedtls_ecdsa_write_signature(&_ecdsa_host_context, MBEDTLS_MD_SHA256, buffer, buffer_len, signature,
&sign_len, mbedtls_ctr_drbg_random, &_ctr_drbg);
if (ret != 0)
{
printf("[ERROR] mbedtls_ecdsa_write_signature failed with status %d", ret);
return false;
}
unsigned char* ptrSig = signature;
mbedtls_mpi r, s;
mbedtls_mpi_init(&r);
mbedtls_mpi_init(&s);
ret =
mbedtls_asn1_get_tag(&ptrSig, signature + sign_len, &lenTag, MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE);
if (!ret)
{
ret = mbedtls_asn1_get_mpi(&ptrSig, signature + sign_len, &r);
if (!ret)
{
ret = mbedtls_asn1_get_mpi(&ptrSig, signature + sign_len, &s);
if (!ret)
{
ret = mbedtls_mpi_write_binary(&r, out_sig, PUB_X_Y_SIZE);
if (!ret)
{
ret = mbedtls_mpi_write_binary(&s, out_sig + PUB_X_Y_SIZE, PUB_X_Y_SIZE);
}
}
}
}
mbedtls_mpi_free(&r);
mbedtls_mpi_free(&s);
return ret == 0; // return true on success
}
bool SignClbk::Verify(const unsigned char* buffer, const unsigned int buffer_len, const unsigned char* sig,
const unsigned int sign_len)
{
if (!_initialized)
return false;
(void)sign_len; // unused
unsigned char buf[MBEDTLS_ECDSA_MAX_LEN];
unsigned char* p = buf + sizeof(buf);
mbedtls_mpi r, s;
mbedtls_mpi_init(&r);
mbedtls_mpi_init(&s);
int ret = mbedtls_mpi_read_binary(&r, sig, PUB_X_Y_SIZE);
if (!ret)
{
ret = mbedtls_mpi_read_binary(&s, sig + PUB_X_Y_SIZE, PUB_X_Y_SIZE);
if (!ret)
{
size_t len = 0;
MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_mpi(&p, buf, &s));
MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_mpi(&p, buf, &r));
MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&p, buf, len));
MBEDTLS_ASN1_CHK_ADD(len,
mbedtls_asn1_write_tag(&p, buf, MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE));
ret = mbedtls_ecdsa_read_signature(&_ecdsa_device_context, buffer, buffer_len, p, len);
}
}
mbedtls_mpi_free(&r);
mbedtls_mpi_free(&s);
return ret == 0; // return true on success
};
} // namespace Examples
} // namespace RealSenseID
/*
* c example implementation of signature callbacks.
* 1. create SignClbk instance.
* 2. store it in the *ctx field of the rsid_signature_clbk struct.
* 3. when the c callback are called, SignClbk* is extracted from the *ctx field,
* and used to perform the actual sign/verify callbacks.
*/
rsid_signature_clbk* rsid_create_example_sig_clbk()
{
auto* clbk = new (rsid_signature_clbk);
clbk->ctx = static_cast<void*>(new RealSenseID::Examples::SignClbk());
clbk->sign_clbk = rsid_sign_example;
clbk->verify_clbk = rsid_verify_example;
return clbk;
}
/* destroy security context */
void rsid_destroy_example_sig_clbk(rsid_signature_clbk* clbk)
{
if (clbk == nullptr)
return;
if (clbk->ctx != nullptr)
{
delete (reinterpret_cast<RealSenseID::Examples::SignClbk*>(clbk->ctx));
}
delete (clbk);
}
int rsid_sign_example(const unsigned char* buffer, const unsigned int buffer_len, unsigned char* out_sig, void* ctx)
{
auto* sig_clbk = static_cast<RealSenseID::Examples::SignClbk*>(ctx);
return static_cast<int>(sig_clbk->Sign(buffer, buffer_len, out_sig));
}
int rsid_verify_example(const unsigned char* buffer, const unsigned int buffer_len, const unsigned char* sig,
const unsigned int siglen, void* ctx)
{
auto* sig_clbk = static_cast<RealSenseID::Examples::SignClbk*>(ctx);
return static_cast<int>(sig_clbk->Verify(buffer, buffer_len, sig, siglen));
}