forked from IntelRealSense/RealSenseID
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MbedtlsWrapper.cc
245 lines (208 loc) · 7.22 KB
/
MbedtlsWrapper.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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
// License: Apache 2.0. See LICENSE file in root directory.
// Copyright(c) 2020-2021 Intel Corporation. All Rights Reserved.
#include "MbedtlsWrapper.h"
#include "Logger.h"
#include <string.h>
static const char* LOG_TAG = "MbedtlsWrapper";
static const char* SALT_AES = "aes";
static const char* SALT_HMAC = "hmac";
namespace RealSenseID
{
namespace PacketManager
{
MbedtlsWrapper::MbedtlsWrapper() :
_ecdh_generate_key {false}, _shared_secret {}, _aes_key {}, _hmac_key {}, _ecdh_signed_pubkey {}
{
mbedtls_entropy_init(&_entropy_ctx);
mbedtls_ctr_drbg_init(&_ctr_drbg_ctx);
mbedtls_ecdh_init(&_edch_ctx);
mbedtls_aes_init(&_aes_ctx);
_md = mbedtls_md_info_from_type(MBEDTLS_MD_SHA256);
}
MbedtlsWrapper::~MbedtlsWrapper()
{
mbedtls_entropy_free(&_entropy_ctx);
mbedtls_ctr_drbg_free(&_ctr_drbg_ctx);
mbedtls_ecdh_free(&_edch_ctx);
mbedtls_aes_free(&_aes_ctx);
}
void MbedtlsWrapper::Reset()
{
::memset(_shared_secret, 0, sizeof(_shared_secret));
::memset(_ecdh_signed_pubkey, 0, sizeof(_ecdh_signed_pubkey));
}
bool MbedtlsWrapper::IsMaEnabled(bool& isMaEnabled)
{
#ifdef RSID_SECURE
isMaEnabled = true;
#else
isMaEnabled = false;
#endif // RSID_SECURE
return true;
}
size_t MbedtlsWrapper::GetSignedEcdhPubkeySize()
{
return SIGNED_PUBKEY_SIZE;
}
unsigned char* MbedtlsWrapper::GetSignedEcdhPubkey(SignCallback sign_clbk)
{
Reset();
if (!GenerateEcdhKey())
{
LOG_ERROR(LOG_TAG, "Failed to generate ecdh key");
return nullptr;
}
int ret = mbedtls_mpi_write_binary(&_edch_ctx.Q.X, _ecdh_signed_pubkey, ECC_P256_KEY_X_Y_Z_SIZE_BYTES);
if (ret != 0)
{
LOG_ERROR(LOG_TAG, "Failed! mbedtls_mpi_write_binary returned %d", ret);
return nullptr;
}
ret = mbedtls_mpi_write_binary(&_edch_ctx.Q.Y, _ecdh_signed_pubkey + ECC_P256_KEY_X_Y_Z_SIZE_BYTES,
ECC_P256_KEY_X_Y_Z_SIZE_BYTES);
if (ret != 0)
{
LOG_ERROR(LOG_TAG, "Failed! mbedtls_mpi_write_binary returned %d", ret);
return nullptr;
}
bool res = sign_clbk(_ecdh_signed_pubkey, ECC_P256_KEY_SIZE_BYTES, _ecdh_signed_pubkey + ECC_P256_KEY_SIZE_BYTES);
if (!res)
{
LOG_ERROR(LOG_TAG, "Failed to sign key");
return nullptr;
}
return _ecdh_signed_pubkey;
}
bool MbedtlsWrapper::VerifyEcdhSignedKey(const unsigned char* ecdh_signed_pubkey, VerifyCallback verify_clbk)
{
int ret = mbedtls_mpi_lset(&_edch_ctx.Qp.Z, 1);
if (ret != 0)
{
LOG_ERROR(LOG_TAG, "Failed! mbedtls_mpi_lset returned %d", ret);
return false;
}
ret = mbedtls_mpi_read_binary(&_edch_ctx.Qp.X, ecdh_signed_pubkey, ECC_P256_KEY_X_Y_Z_SIZE_BYTES);
if (ret != 0)
{
LOG_ERROR(LOG_TAG, "Failed! mbedtls_mpi_read_binary ecdh_signed_pubkey X returned %d", ret);
return false;
}
ret = mbedtls_mpi_read_binary(&_edch_ctx.Qp.Y, ecdh_signed_pubkey + ECC_P256_KEY_X_Y_Z_SIZE_BYTES,
ECC_P256_KEY_X_Y_Z_SIZE_BYTES);
if (ret != 0)
{
LOG_ERROR(LOG_TAG, "Failed! mbedtls_mpi_read_binary ecdh_signed_pubkey Y returned %d", ret);
return false;
}
bool res = verify_clbk(ecdh_signed_pubkey, ECC_P256_KEY_SIZE_BYTES, ecdh_signed_pubkey + ECC_P256_KEY_SIZE_BYTES,
ECC_P256_SIG_SIZE_BYTES);
if (!res)
{
LOG_WARNING(LOG_TAG, "Failed to verify key - ignoring");
return false;
}
if (!GenerateEcdhKey())
{
LOG_ERROR(LOG_TAG, "Failed to generate ecdh key");
return false;
}
ret = mbedtls_ecdh_compute_shared(&_edch_ctx.grp, &_edch_ctx.z, &_edch_ctx.Qp, &_edch_ctx.d,
mbedtls_ctr_drbg_random, &_ctr_drbg_ctx);
if (ret != 0)
{
LOG_ERROR(LOG_TAG, "Failed! mbedtls_ecdh_compute_shared returned %d", ret);
return false;
}
ret = mbedtls_mpi_write_binary(&_edch_ctx.z, _shared_secret, ECC_P256_KEY_X_Y_Z_SIZE_BYTES);
if (ret != 0)
{
LOG_ERROR(LOG_TAG, "Failed! mbedtls_mpi_write_binary returned %d", ret);
return false;
}
ret = mbedtls_hkdf(_md, (unsigned char*)SALT_AES, strlen(SALT_AES), _shared_secret, ECC_P256_KEY_X_Y_Z_SIZE_BYTES,
0, 0, _aes_key, ECC_P256_KEY_X_Y_Z_SIZE_BYTES);
if (ret != 0)
{
LOG_ERROR(LOG_TAG, "Failed! mbedtls_hkdf 0 returned %d", ret);
return false;
}
ret = mbedtls_aes_setkey_enc(&_aes_ctx, _aes_key, AES_CTR_256_BIT_KEY_SIZE_BYTES * 8);
if (ret != 0)
{
LOG_ERROR(LOG_TAG, "Failed! mbedtls_aes_setkey_enc returned %d", ret);
return false;
}
ret = mbedtls_hkdf(_md, (unsigned char*)SALT_HMAC, strlen(SALT_HMAC), _shared_secret, ECC_P256_KEY_X_Y_Z_SIZE_BYTES,
0, 0, _hmac_key, ECC_P256_KEY_X_Y_Z_SIZE_BYTES);
if (ret != 0)
{
LOG_ERROR(LOG_TAG, "Failed! mbedtls_hkdf 1 returned %d", ret);
return false;
}
return true;
}
bool MbedtlsWrapper::Encrypt(const unsigned char* iv, const unsigned char* input, unsigned char* output,
const unsigned int length)
{
bool res = AesCtr256(iv, input, output, length);
return res;
}
bool MbedtlsWrapper::Decrypt(const unsigned char* iv, const unsigned char* input, unsigned char* output,
const unsigned int length)
{
bool res = AesCtr256(iv, input, output, length);
return res;
}
bool MbedtlsWrapper::CalcHmac(const unsigned char* input, const unsigned int length, unsigned char* hmac)
{
int ret = mbedtls_md_hmac(_md, _hmac_key, ECC_P256_KEY_X_Y_Z_SIZE_BYTES, input, length, hmac);
if (ret != 0)
{
LOG_ERROR(LOG_TAG, "Failed! mbedtls_hkdf returned %d", ret);
return false;
}
return true;
}
bool MbedtlsWrapper::GenerateEcdhKey()
{
if (_ecdh_generate_key)
return true;
int ret = mbedtls_ctr_drbg_seed(&_ctr_drbg_ctx, mbedtls_entropy_func, &_entropy_ctx, NULL, 0);
if (ret != 0)
{
LOG_ERROR(LOG_TAG, "Failed! mbedtls_ctr_drbg_seed returned %d", ret);
return false;
}
ret = mbedtls_ecp_group_load(&_edch_ctx.grp, MBEDTLS_ECP_DP_SECP256R1);
if (ret != 0)
{
LOG_ERROR(LOG_TAG, "Failed! mbedtls_ecp_group_load returned %d", ret);
return false;
}
ret = mbedtls_ecdh_gen_public(&_edch_ctx.grp, &_edch_ctx.d, &_edch_ctx.Q, mbedtls_ctr_drbg_random, &_ctr_drbg_ctx);
if (ret != 0)
{
LOG_ERROR(LOG_TAG, "Failed! mbedtls_ecdh_gen_public returned %d", ret);
return false;
}
_ecdh_generate_key = true;
return true;
}
bool MbedtlsWrapper::AesCtr256(const unsigned char* iv, const unsigned char* input, unsigned char* output,
const unsigned int length)
{
size_t nc_off = 0;
unsigned char ivBuf[AES_CTR_IV_SIZE_BYTES];
::memcpy(ivBuf, iv, AES_CTR_IV_SIZE_BYTES);
unsigned char stream_block[AES_CTR_IV_SIZE_BYTES];
::memset(stream_block, 0, AES_CTR_IV_SIZE_BYTES);
auto ret = mbedtls_aes_crypt_ctr(&_aes_ctx, length, &nc_off, ivBuf, stream_block, input, output);
if (ret != 0)
{
LOG_ERROR(LOG_TAG, "Failed! mbedtls_aes_crypt_ctr returned %d", ret);
return false;
}
return true;
}
} // namespace PacketManager
} // namespace RealSenseID