Newer
Older
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
/*!
* \File lib_its_security.h
* \brief Declaration file for Security external functions.
* \author FSCOM
* \copyright FSCOM Copyright Notification
* No part may be reproduced except as authorized by written permission.
* The copyright and the foregoing restriction extend to reproduction in all media.
* All rights reserved.
* \version 0.1
*/
#pragma once
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <stdint.h>
#include <string.h>
#include <ctype.h>
#include <memory.h>
#include <errno.h>
#include <openssl/err.h>
#include <openssl/ec.h>
#include <openssl/pem.h>
#include <openssl/bn.h>
#include <openssl/hmac.h>
/*!
* \enum Supported Elliptic curves
*/
typedef enum ecc_elliptic_curves_ {
nist_p_256, /*!< NIST P-256, P-256, primve256v1 */
brainpool_p_256_r1, /*!< Brainpool P256r1 */
brainpool_p_384_r1 /*!< Brainpool P384r1 */
} ecc_elliptic_curves_t;
/*!
* \enum Public key coordinates compression mode
*/
typedef enum ecc_compressed_mode_ {
compressed_y_0, /*!< The last significant bit of Y-coodinate ended with 0 */
compressed_y_1 /*!< The last significant bit of Y-coodinate ended with 1 */
} ecc_compressed_mode_t;
/*!
* \enum Supported encryption algorithem
*/
typedef enum ecc_encryption_algorithm_ {
aes_128_ccm,
aes_256_ccm,
aes_128_gcm,
aes_256_gcm
} encryption_algorithm_t;
/*!
* \struct ITS security context to be used in functions
* \brief This data structure contains all the infomation required to achieve security computation such signature, ciphering...
*/
typedef struct lib_its_security_context_ {
ecc_elliptic_curves_t elliptic_curve; /*! The ellicptic curve to be used */
EC_KEY* ec_key; /*!< EC_KEY reference */
EC_GROUP* ec_group; /*!< EC_GROUP reference */
BN_CTX* bn_ctx; /*!< Pre-alocated memory used to increase OpenSSL processing */
size_t key_length; /*!< private/public keys length */
uint8_t* private_key; /*!< Private key */
uint8_t* public_key_x; /*!< Public key Y-coodinate */
uint8_t* public_key_y; /*!< Public key Y-coodinate */
uint8_t* public_key_c; /*!< Compressed Public key */
ecc_compressed_mode_t compressed_mode; /*!< Compression mode */
encryption_algorithm_t encryption_algorithm; /* Encryption algorithm currently used */
uint8_t* secret_key;
uint8_t* sym_key;
uint8_t* enc_sym_key;
uint8_t* tag;
uint8_t* nonce;
size_t secret_key_length;
size_t sym_key_length;
size_t nonce_length;
size_t tag_length;
} lib_its_security_context_t;
/**
* \fn int32_t initialize(const ecc_elliptic_curves_t p_elliptic_curve, lib_its_security_context_t** p_lib_its_security_context);
* \brief Initialize the ITS security context according to the specified elliptic curve. This function shall be called before any othe lib_its_security function.
* \param[in] p_elliptic_curve The elliptic curve to be used
* \param[out] p_lib_its_security_context The internl context (To be released using uninitialize function)
* \return 0 on success,-1 otherwise
*/
int32_t initialize(const ecc_elliptic_curves_t p_elliptic_curve, lib_its_security_context_t** p_lib_its_security_context);
/**
* \fn int32_t uninitialize(lib_its_security_context_t** p_lib_its_security_context);
* \brief Release resources allocated by initialize fiunction
* \param[in/out] p_lib_its_security_context The internl context
* \return 0 on success,-1 otherwise
*/
int32_t uninitialize(lib_its_security_context_t** p_lib_its_security_context);
/**
* \fn int32_t hash_with_sha256(const uint8_t* p_to_be_hashed_data,const size_t p_to_be_hashed_data_length,uint8_t** p_hashed_data);
* \brief Produces a 256-bit (32-bytes) hash value
* \param[in] p_to_be_hashed_data The data to be used to calculate the hash value
* \param[in] p_to_be_hashed_data_length The length of the data to be hashed
* \param[in/out] p_hashed_data The data to be used to calculate the hash value
* \return 0 on success,-1 otherwise
*/
int32_t hash_with_sha256(const uint8_t* p_to_be_hashed_data, const size_t p_to_be_hashed_data_length, uint8_t** p_hashed_data);
/**
* \fn int32_t hash_with_sha384(const uint8_t* p_to_be_hashed_data,const size_t p_to_be_hashed_data_length,uint8_t** p_hashed_data);
* \brief Produces a 384-bit (48-bytes) hash value
* \param[in] p_to_be_hashed_data Data to be used to calculate the hash value
* \param[in] p_to_be_hashed_data_length The length of the data to be hashed
* \param[in] p_hashed_data The data to be used to calculate the hash value
* \return 0 on success,-1 otherwise
*/
int32_t hash_with_sha384(const uint8_t* p_to_be_hashed_data, const size_t p_to_be_hashed_data_length, uint8_t** p_hashed_data);
/**
* \fn int32_t hmac_sha256(const uint8_t* p_secret_key,const size p_secret_key_length,const OCTETSTRING& p_message,const size p_message_length, uint8_t** p_hmac);
* \brief Generate a HMAC-SHA256 value based on the provided secret key
* \param[in] p_secret_key The secret key used for the HMAC calculation
* \param[in] p_message The message
* \param[out] p_hmac The HMAC with SHA256 of the message resized to 16-bytes (To be released after use)
* \return 0 on success,-1 otherwise
*/
int32_t hmac_sha256(const uint8_t* p_secret_key, const size_t p_secret_key_length, const uint8_t* p_message, const size_t p_message_length, uint8_t** p_hmac);
/**
* \fn int32_t sign_with_ecdsa_nistp256_with_sha256(lib_its_security_context_t* p_lib_its_security_context, const uint8_t* p_to_be_signed_secured_message,const size_t p_to_be_signed_secured_message_length,const uint8_t* p_certificate_issuer,const uint8_t* p_private_key,uint8_t** p_signature);
* \brief Produces a Elliptic Curve Digital Signature Algorithm (ECDSA) signature based on standard IEEE 1609.2
* \param[in/out] p_hashed_data The data to be used to calculate the hash value
* \param[in] p_to_be_signed_secured_message The data to be signed
* \param[in] p_certificate_issuer The whole-hash issuer certificate or int2oct(0,32) in case of self signed certificate
* \param[in] p_private_key The private key
* \param[out] p_signature The signature of the data to be signed (To be released after use)
* \return The signature value
*/
int32_t sign_with_ecdsa_nistp256_with_sha256(
lib_its_security_context_t* p_lib_its_security_context,
const uint8_t* p_to_be_signed_secured_message,
const size_t p_to_be_signed_secured_message_length,
const uint8_t* p_certificate_issuer,
const uint8_t* p_private_key,
uint8_t** p_signature
);
/**
* \fn int32_t sign_with_ecdsa_brainpoolp256r1_with_sha256(lib_its_security_context_t* p_lib_its_security_context, const uint8_t* p_to_be_signed_secured_message,const size_t p_to_be_signed_secured_message_length,const uint8_t* p_certificate_issuer,const uint8_t* p_private_key,uint8_t** p_signature);
* \brief Produces a Elliptic Curve Digital Signature Algorithm (ECDSA) signature based on standard IEEE 1609.2
* \param[in/out] p_hashed_data The data to be used to calculate the hash value
* \param[in] p_to_be_signed_secured_message The data to be signed
* \param[in] p_certificate_issuer The whole-hash issuer certificate or int2oct(0,32) in case of self signed certificate
* \param[in] p_private_key The private key
* \param[out] p_signature The signature of the data to be signed (To be released after use)
* \return The signature value
*/
int32_t sign_with_ecdsa_brainpoolp256r1_with_sha256(
lib_its_security_context_t* p_lib_its_security_context,
const uint8_t* p_to_be_signed_secured_message,
const size_t p_to_be_signed_secured_message_length,
const uint8_t* p_certificate_issuer,
const uint8_t* p_private_key,
uint8_t** p_signature
);
/**
* \fn int32_t sign_with_ecdsa_brainpoolp384r1_with_sha384(lib_its_security_context_t* p_lib_its_security_context, const uint8_t* p_to_be_signed_secured_message,const size_t p_to_be_signed_secured_message_length,const uint8_t* p_certificate_issuer,const uint8_t* p_private_key,uint8_t** p_signature);
* \brief Produces a Elliptic Curve Digital Signature Algorithm (ECDSA) signature based on standard IEEE 1609.2
* \param[in/out] p_hashed_data The data to be used to calculate the hash value
* \param[in] p_to_be_signed_secured_message The data to be signed
* \param[in] p_certificate_issuer The whole-hash issuer certificate or int2oct(0,32) in case of self signed certificate
* \param[in] p_private_key The private key
* \param[out] p_signature The signature of the data to be signed (To be released after use)
* \return 0 on success,-1 otherwise
*/
int32_t sign_with_ecdsa_brainpoolp384r1_with_sha384(
lib_its_security_context_t* p_lib_its_security_context,
const uint8_t* p_to_be_signed_secured_message,
const size_t p_to_be_signed_secured_message_length,
const uint8_t* p_certificate_issuer,
const uint8_t* p_private_key,
uint8_t** p_signature
);
/**
* \fn int32_t verify_with_ecdsa_nistp256_with_sha256(lib_its_security_context_t* p_lib_its_security_context, const uint8_t* p_to_be_verified_data,const size_t p_to_be_verified_data_length,const uint8_t* p_certificate_issuer,const uint8_t* p_signature,const uint8_t* p_ecdsa_nistp256_publicKey_compressed, const ecc_compressed_mode_t p_compressed_mode);
* \brief Verify the signature of the specified data based on standard IEEE 1609.2
* \param[in/out] p_hashed_data The data to be used to calculate the hash value
* \param[in] p_to_be_verified_data The data to be verified
* \param[in] p_certificate_issuer The whole-hash issuer certificate or int2oct(0,32) in case of self signed certificate
* \param[in] p_signature The signature
* \param[in] p_ecdsa_nistp256_publicKey_compressed The compressed public key (x coordinate only)
* \return 0 on success,-1 otherwise
*/
int32_t verify_with_ecdsa_nistp256_with_sha256(
lib_its_security_context_t* p_lib_its_security_context,
const uint8_t* p_to_be_verified_data,
const size_t p_to_be_verified_data_length,
const uint8_t* p_certificate_issuer,
const uint8_t* p_signature,
const uint8_t* p_ecdsa_nistp256_publicKey_compressed,
const ecc_compressed_mode_t p_compressed_mode
);
/**
* \fn int32_t verify_with_ecdsa_nistp256_with_sha256_raw(lib_its_security_context_t* p_lib_its_security_context, const uint8_t* p_to_be_verified_data,const size_t p_to_be_verified_data_length,const uint8_t* p_certificate_issuer,const uint8_t* p_signature,const uint8_t* p_ecdsa_nistp256_publicKey_compressed, const ecc_compressed_mode_t p_compressed_mode));
* \brief Verify the signature of the specified data based on raw data
* \param[in/out] p_hashed_data The data to be used to calculate the hash value
* \param[in] p_to_be_verified_data The data to be verified
* \param[in] p_signature The signature
* \param[in] p_ecdsa_nistp256_publicKey_compressed The compressed public key (x coordinate only)
* \return 0 on success,-1 otherwise
*/
int32_t verify_with_ecdsa_nistp256_with_sha256_raw(
lib_its_security_context_t* p_lib_its_security_context,
const uint8_t* p_to_be_verified_data,
const size_t p_to_be_verified_data_length,
const uint8_t* p_signature,
const uint8_t* p_ecdsa_nistp256_publicKey_compressed,
const ecc_compressed_mode_t p_compressed_mode
);
/**
* \fn int32_t verify_with_ecdsa_brainpoolp256r1_with_sha256(lib_its_security_context_t* p_lib_its_security_context, const uint8_t* p_to_be_verified_data,const size_t p_to_be_verified_data_length,const uint8_t* p_certificate_issuer,const uint8_t* p_signature,const uint8_t* p_ecdsa_nistp256_publicKey_compressed, const ecc_compressed_mode_t p_compressed_mode);
* \brief Verify the signature of the specified data based on standard IEEE 1609.2
* \param[in/out] p_hashed_data The data to be used to calculate the hash value
* \param[in] p_to_be_verified_data The data to be verified
* \param[in] p_certificate_issuer The whole-hash issuer certificate or int2oct(0,32) in case of self signed certificate
* \param[in] p_signature The signature
* \param[in] p_ecdsaBrainpoolp256PublicKeyCompressed The compressed public key (x coordinate only)
* \return 0 on success,-1 otherwise
*/
int32_t verify_with_ecdsa_brainpoolp256r1_with_sha256(
lib_its_security_context_t* p_lib_its_security_context,
const uint8_t* p_to_be_verified_data,
const size_t p_to_be_verified_data_length,
const uint8_t* p_certificate_issuer,
const uint8_t* p_signature,
const uint8_t* p_ecdsaBrainpoolp256PublicKeyCompressed,
const ecc_compressed_mode_t p_compressed_mode
);
/**
* \fn int32_t verify_with_ecdsa_brainpoolp384r1_with_sha384(lib_its_security_context_t* p_lib_its_security_context, const uint8_t* p_to_be_verified_data,const size_t p_to_be_verified_data_length,const uint8_t* p_certificate_issuer,const uint8_t* p_signature,const uint8_t* p_ecdsa_nistp256_publicKey_compressed, const ecc_compressed_mode_t p_compressed_mode);
* \brief Verify the signature of the specified data based on standard IEEE 1609.2
* \param[in/out] p_hashed_data The data to be used to calculate the hash value
* \param[in] p_to_be_verified_data The data to be verified
* \param[in] p_certificate_issuer The whole-hash issuer certificate or int2oct(0,32) in case of self signed certificate
* \param[in] p_signature The signature
* \param[in] p_ecdsaBrainpoolp384PublicKeyCompressed The compressed public key (x coordinate only)
* \return 0 on success,-1 otherwise
*/
int32_t verify_with_ecdsa_brainpoolp384r1_with_sha384(
lib_its_security_context_t* p_lib_its_security_context,
const uint8_t* p_to_be_verified_data,
const size_t p_to_be_verified_data_length,
const uint8_t* p_certificate_issuer,
const uint8_t* p_signature,
const uint8_t* p_ecdsaBrainpoolp384PublicKeyCompressed,
const ecc_compressed_mode_t p_compressed_mode
);
/**
* \brief Encrypt the message using ECIES algorithm to encrypt AES 128 CCM symmetric key,as defined in IEEE Std 1609.2-2017
* \param[in/out] p_hashed_data The data to be used to calculate the hash value
* \param[in] p_to_be_encrypted_secured_message The message to be encrypted
* \param[in] p_recipients_public_key_compressed The Recipient's compressed public key
* \param[in] p_compressed_mode The compressed mode,0 if the latest bit of Y-coordinate is 0,1 otherwise
* \param[out] p_public_ephemeral_key_compressed The public ephemeral compressed key (To be released after use)
* \param[out] p_ephemeral_compressed_mode The compressed mode,0 if the latest bit of Y-coordinate is 0,1 otherwise
* \param[out] p_encrypted_sym_key The encrypted AES 128 symmetric key (To be released after use)
* \param[out] p_authentication_vector The tag of the encrypted AES 128 symmetric key (To be released after use)
* \param[out] p_nonce The nonce vector (To be released after use)
uint8_t** p_encrypted_secured_message,
size_t* p_encrypted_secured_message_length,
* \return 0 on success,-1 otherwise
* \see IEEE Std 1609.2-2017 Clause 5.3.5 Public key encryption algorithms: ECIES
* \see https://www.nominet.uk/researchblog/how-elliptic-curve-cryptography-encryption-works/
* \see http://digital.csic.es/bitstream/10261/32671/1/V2-I2-P7-13.pdf
*/
int32_t encrypt_with_ecies_nistp256_with_sha256(
lib_its_security_context_t* p_lib_its_security_context,
const uint8_t* p_to_be_encrypted_secured_message,
const size_t p_to_be_encrypted_secured_message_length,
const uint8_t* p_recipients_public_key_compressed,
const ecc_compressed_mode_t p_compressed_mode,
const uint8_t* p_salt,
const size_t p_salt_length,
uint8_t** p_public_ephemeral_key_compressed,
ecc_compressed_mode_t* p_ephemeral_compressed_mode,
uint8_t** p_aes_sym_key,
uint8_t** p_encrypted_sym_key,
uint8_t** p_authentication_vector,
uint8_t** p_nonce,
uint8_t** p_encrypted_secured_message,
size_t* p_encrypted_secured_message_length
);
/**
* \brief Decrypt the message using ECIES algorithm to decrypt AES 128 CCM symmetric key,as defined in IEEE Std 1609.2-2017
* \param[in/out] p_hashed_data The data to be used to calculate the hash value
* \param[in] p_encrypted_secured_message The encrypted message
* \param[in] p_private_enc_key The private encryption key
* \param[in] p_public_ephemeral_key_compressed The public ephemeral compressed key
* \param[in] p_ephemeral_compressed_mode The compressed mode,0 if the latest bit of Y-coordinate is 0,1 otherwise
* \param[in] p_encrypted_sym_key The encrypted AES 128 symmetric key
* \param[in] p_authentication_vector The tag of the encrypted AES 128 symmetric key
* \param[in] p_nonce The nonce vector
* \return 0 on success,-1 otherwise
* \see IEEE Std 1609.2-2017 Clause 5.3.5 Public key encryption algorithms: ECIES
* \see https://www.nominet.uk/researchblog/how-elliptic-curve-cryptography-encryption-works/
* \see http://digital.csic.es/bitstream/10261/32671/1/V2-I2-P7-13.pdf
*/
int32_t decrypt_with_ecies_nistp256_with_sha256(
lib_its_security_context_t* p_lib_its_security_context,
const uint8_t* p_encrypted_secured_message,
const size_t p_encrypted_secured_message_length,
const uint8_t* p_private_enc_key,
const uint8_t* p_public_ephemeral_key_compressed,
const ecc_compressed_mode_t p_ephemeral_compressed_mode,
const uint8_t* p_encrypted_sym_key,
const uint8_t* p_authentication_vector,
const uint8_t* p_nonce,
const uint8_t* p_salt,
const size_t p_salt_length,
uint8_t** p_aes_sym_enc_key,
uint8_t** p_plain_text_message,
size_t* p_plain_text_message_length
);
int32_t encrypt_with_ecies_brainpoolp256r1_with_sha256(
lib_its_security_context_t* p_lib_its_security_context,
const uint8_t* p_to_be_encrypted_secured_message,
const size_t p_to_be_encrypted_secured_message_length,
const uint8_t* p_recipients_public_key_compressed,
const ecc_compressed_mode_t p_compressed_mode,
const uint8_t* p_salt,
const size_t p_salt_length,
uint8_t** p_public_ephemeral_key_compressed,
ecc_compressed_mode_t* p_ephemeral_compressed_mode,
uint8_t** p_aes_sym_key,
uint8_t** p_encrypted_sym_key,
uint8_t** p_authentication_vector,
uint8_t** p_nonce,
uint8_t** p_encrypted_secured_message,
size_t* p_encrypted_secured_message_length
);
int32_t decrypt_with_ecies_brainpoolp256r1_with_sha256(
lib_its_security_context_t* p_lib_its_security_context,
const uint8_t* p_encrypted_secured_message,
const size_t p_encrypted_secured_message_length,
const uint8_t* p_private_enc_key,
const uint8_t* p_public_ephemeral_key_compressed,
const ecc_compressed_mode_t p_ephemeral_compressed_mode,
const uint8_t* p_encrypted_sym_key,
const uint8_t* p_authentication_vector,
const uint8_t* p_nonce,
const uint8_t* p_salt,
const size_t p_salt_length,
uint8_t** p_aes_sym_enc_key,
uint8_t** p_plain_text_message,
size_t* p_plain_text_message_length
);
/**
* \fn int32_t generate_key_pair(lib_its_security_context_t* p_lib_its_security_context, uint8_t** p_private_key,uint8_t** p_public_key_x,uint8_t** p_public_key_y,uint8_t** p_public_key_compressed, ecc_compressed_mode_t* p_compressed_mode);
* \brief Produce a new public/private key pair based on Elliptic Curve Digital Signature Algorithm (ECDSA) algorithm.
* \param[in/out] p_hashed_data The data to be used to calculate the hash value
* \param[out] p_private_key The new private key value (To be released after use)
* \param[out] p_public_key_x The new public key value (x coordinate) (To be released after use)
* \param[out] p_public_key_x The new public key value (y coordinate) (To be released after use)
* \return 0 on success,-1 otherwise
*/
int32_t generate_key_pair(
lib_its_security_context_t* p_lib_its_security_context,
uint8_t** p_private_key,
uint8_t** p_public_key_x,
uint8_t** p_public_key_y,
uint8_t** p_public_key_compressed,
ecc_compressed_mode_t* p_compressed_mode
);