Newer
Older
#include "LibItsSecurity_Functions.hh"
#include "sha256.hh"
#include "sha384.hh"
#include "ec_keys.hh"
#include <openssl/ec.h>
#include <openssl/ecdsa.h>
#define FIELD_SIZE_256 (256/8)
#define SIGNATURE_SIZE_256 (2+FIELD_SIZE_256*2)
#define FIELD_SIZE_384 (384/8)
#define SIGNATURE_SIZE_384 (2+FIELD_SIZE_284*2)
namespace LibItsSecurity__Functions
{
/**
* @desc Produces a 256-bit (32-byte) hash value
* @param p_toBeHashedData Data to be used to calculate the hash value
* @return The hash value
fx_hashWithSha256(in octetstring p_toBeHashedData) return Oct32;
*/
OCTETSTRING fx__hashWithSha256(
const OCTETSTRING& p__toBeHashedData
) {
sha256 hash;
std::vector<unsigned char> tbh(static_cast<const unsigned char *>(p__toBeHashedData), p__toBeHashedData.lengthof() + static_cast<const unsigned char *>(p__toBeHashedData));
return OCTETSTRING(hashData.size(), hashData.data());
} // End of function fx__hashWithSha256
/**
* @desc Produces a 384-bit (48-byte) hash value
* @param p_toBeHashedData Data to be used to calculate the hash value
* @return The hash value
fx_hashWithSha384(in octetstring p_toBeHashedData) return Oct48;
*/
OCTETSTRING fx__hashWithSha384(
const OCTETSTRING& p__toBeHashedData
) {
sha384 hash;
std::vector<unsigned char> tbh(static_cast<const unsigned char *>(p__toBeHashedData), p__toBeHashedData.lengthof() + static_cast<const unsigned char *>(p__toBeHashedData));
return OCTETSTRING(hashData.size(), hashData.data());
} // End of function fx__hashWithSha384
/**
* @desc Produces a Elliptic Curve Digital Signature Algorithm (ECDSA) signaturee
* @param p_toBeSignedSecuredMessage The data to be signed
* @param p_privateKey The private key
* @return The signature value
fx_signWithEcdsaNistp256WithSha256(in octetstring p_toBeSignedSecuredMessage, in octetstring<UInt64> p_privateKey) return octetstring;
*/
OCTETSTRING fx__signWithEcdsaNistp256WithSha256(
const OCTETSTRING& p__toBeSignedSecuredMessage,
const OCTETSTRING& p__privateKey
sha256 hash;
std::vector<unsigned char> hashData;
// TODO Create SHX interface and add generate method with std::vector
std::vector<unsigned char> tbs(static_cast<const unsigned char *>(p__toBeSignedSecuredMessage), p__toBeSignedSecuredMessage.lengthof() + static_cast<const unsigned char *>(p__toBeSignedSecuredMessage));
hash.generate(tbs, hashData);
// Calculate the signature
const unsigned char * p = static_cast<const unsigned char *>(p__privateKey);
std::vector<unsigned char> p_key(p, p + p__privateKey.lengthof());
ec_keys k(ec_elliptic_curves::nist_p_256, p_key);
std::vector<unsigned char> r_sig;
std::vector<unsigned char> s_sig;
if (k.sign(hashData, r_sig, s_sig) == 0) {
OCTETSTRING os(r_sig.size(), r_sig.data());
// loggers::get_instance().log_to_hexa("r_sig= ", os);
// loggers::get_instance().log_to_hexa("s_sig= ", OCTETSTRING(s_sig.size(), s_sig.data());
// loggers::get_instance().log_to_hexa("sig= ", os += OCTETSTRING(s_sig.size(), s_sig.data()));
os += OCTETSTRING(s_sig.size(), s_sig.data());
return os;
}
return OCTETSTRING();
}
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
/**
* @desc Produces a Elliptic Curve Digital Signature Algorithm (ECDSA) signaturee
* @param p_toBeSignedSecuredMessage The data to be signed
* @param p_privateKey The private key
* @return The signature value
fx_signWithEcdsaBrainpoolp256WithSha256(in octetstring p_toBeSignedSecuredMessage, in octetstring<UInt64> p_privateKey) return octetstring;
*/
OCTETSTRING fx__signWithEcdsaBrainpoolp256WithSha256(
const OCTETSTRING& p__toBeSignedSecuredMessage,
const OCTETSTRING& p__privateKey
) {
// Calculate the SHA256 of the data
sha256 hash;
std::vector<unsigned char> hashData;
// TODO Create SHX interface and add generate method with std::vector
std::vector<unsigned char> tbs(static_cast<const unsigned char *>(p__toBeSignedSecuredMessage), p__toBeSignedSecuredMessage.lengthof() + static_cast<const unsigned char *>(p__toBeSignedSecuredMessage));
hash.generate(tbs, hashData);
// Calculate the signature
const unsigned char * p = static_cast<const unsigned char *>(p__privateKey);
std::vector<unsigned char> p_key(p, p + p__privateKey.lengthof());
ec_keys k(ec_elliptic_curves::brainpool_p_256_r1, p_key);
std::vector<unsigned char> r_sig;
std::vector<unsigned char> s_sig;
if (k.sign(hashData, r_sig, s_sig) == 0) {
OCTETSTRING os(r_sig.size(), r_sig.data());
// loggers::get_instance().log_to_hexa("r_sig= ", os);
// loggers::get_instance().log_to_hexa("s_sig= ", OCTETSTRING(s_sig.size(), s_sig.data());
// loggers::get_instance().log_to_hexa("sig= ", os += OCTETSTRING(s_sig.size(), s_sig.data()));
os += OCTETSTRING(s_sig.size(), s_sig.data());
return os;
}
return OCTETSTRING();
}
/**
* @desc Produces a Elliptic Curve Digital Signature Algorithm (ECDSA) signaturee
* @param p_toBeSignedSecuredMessage The data to be signed
* @param p_privateKey The private key
* @return The signature value
fx_signWithEcdsaBrainpoolp384WithSha384(in octetstring p_toBeSignedSecuredMessage, in octetstring<UInt64> p_privateKey) return octetstring;
*/
OCTETSTRING fx__signWithEcdsaBrainpoolp384WithSha384(
const OCTETSTRING& p__toBeSignedSecuredMessage,
const OCTETSTRING& p__privateKey
) {
// Calculate the SHA384 of the data
sha384 hash;
std::vector<unsigned char> hashData;
// TODO Create SHX interface and add generate method with std::vector
std::vector<unsigned char> tbs(static_cast<const unsigned char *>(p__toBeSignedSecuredMessage), p__toBeSignedSecuredMessage.lengthof() + static_cast<const unsigned char *>(p__toBeSignedSecuredMessage));
hash.generate(tbs, hashData);
// Calculate the signature
const unsigned char * p = static_cast<const unsigned char *>(p__privateKey);
std::vector<unsigned char> p_key(p, p + p__privateKey.lengthof());
ec_keys k(ec_elliptic_curves::brainpool_p_384_r1, p_key);
std::vector<unsigned char> r_sig;
std::vector<unsigned char> s_sig;
if (k.sign(hashData, r_sig, s_sig) == 0) {
OCTETSTRING os(r_sig.size(), r_sig.data());
// loggers::get_instance().log_to_hexa("r_sig= ", os);
// loggers::get_instance().log_to_hexa("s_sig= ", OCTETSTRING(s_sig.size(), s_sig.data());
// loggers::get_instance().log_to_hexa("sig= ", os += OCTETSTRING(s_sig.size(), s_sig.data()));
os += OCTETSTRING(s_sig.size(), s_sig.data());
return os;
}
return OCTETSTRING();
}
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
/**
* @desc Verify the signature of the specified data
* @param p_toBeVerifiedData The data to be verified
* @param p_signature The signature
* @param p_ecdsaNistp256PublicKeyX The public key (x coordinate)
* @param p_ecdsaNistp256PublicKeyY The public key (y coordinate)
* @return true on success, false otherwise
fx_verifyWithEcdsaNistp256WithSha256(in octetstring p_toBeVerifiedData, in octetstring p_signature, in octetstring p_ecdsaNistp256PublicKeyX, in octetstring p_ecdsaNistp256PublicKeyY) return boolean;
*/
BOOLEAN fx__verifyWithEcdsaNistp256WithSha256(
const OCTETSTRING& p__toBeVerifiedData,
const OCTETSTRING& p__signature,
const OCTETSTRING& p__ecdsaNistp256PublicKeyX,
const OCTETSTRING& p__ecdsaNistp256PublicKeyY
) {
sha256 hash;
std::vector<unsigned char> hashData;
// TODO Create SHX interface and add generate method with std::vector
//std::vector<unsigned char> tbs(p__toBeSignedSecuredMessage.lengthof(), static_cast<const unsigned char *>(p__toBeSignedSecuredMessage));
// hash.generate(tbs, hashData);
hash.generate(static_cast<const unsigned char*>(p__toBeVerifiedData), p__toBeVerifiedData.lengthof(), hashData);
const unsigned char * p = static_cast<const unsigned char *>(p__signature);
std::vector<unsigned char> signature(p, p + p__signature.lengthof());
p = static_cast<const unsigned char *>(p__ecdsaNistp256PublicKeyX);
std::vector<unsigned char> pub_key_x(p, p + p__ecdsaNistp256PublicKeyX.lengthof());
p = static_cast<const unsigned char *>(p__ecdsaNistp256PublicKeyY);
std::vector<unsigned char> pub_key_y(p, p + p__ecdsaNistp256PublicKeyY.lengthof());
ec_keys k(ec_elliptic_curves::nist_p_256, pub_key_x, pub_key_y);
if (k.sign_verif(hashData, signature) == 0) {
return TRUE;
/**
* @desc Produce a new public/private key pair based on Elliptic Curve Digital Signature Algorithm (ECDSA) algorithm.
* This function should not be used by the ATS
* @param p_privateKey The new private key value
* @param p_publicKeyX The new public key value (x coordinate)
* @param p_publicKeyX The new public key value (y coordinate)
* @return true on success, false otherwise
fx_generateKeyPair_nistp256(out octetstring<UInt64> p_privateKey, out octetstring p_publicKeyX, out octetstring p_publicKeyY) return boolean;
BOOLEAN fx__generateKeyPair__nistp256(
OCTETSTRING& p__privateKey,
OCTETSTRING& p__publicKeyX,
OCTETSTRING& p__publicKeyY
) {
ec_keys k(ec_elliptic_curves::nist_p_256);
if (k.generate() != 0) {
p__privateKey = OCTETSTRING();
p__publicKeyX = OCTETSTRING();
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
p__publicKeyY = OCTETSTRING();
return FALSE;
}
p__privateKey = OCTETSTRING(k.private_key().size(), k.private_key().data());
p__publicKeyX = OCTETSTRING(k.public_key_x().size(), k.public_key_x().data());
p__publicKeyY = OCTETSTRING(k.public_key_y().size(), k.public_key_y().data());
return TRUE;
}
/**
* @desc Produce a new public/private key pair based on Elliptic Curve Digital Signature Algorithm (ECDSA) algorithm.
* This function should not be used by the ATS
* @param p_privateKey The new private key value
* @param p_publicKeyX The new public key value (x coordinate)
* @param p_publicKeyX The new public key value (y coordinate)
* @return true on success, false otherwise
fx_generateKeyPair_nistp256(out octetstring<UInt64> p_privateKey, out octetstring p_publicKeyX, out octetstring p_publicKeyY) return boolean;
*/
BOOLEAN fx__generateKeyPair__brainpoolp256(
OCTETSTRING& p__privateKey,
OCTETSTRING& p__publicKeyX,
OCTETSTRING& p__publicKeyY
) {
ec_keys k(ec_elliptic_curves::brainpool_p_256_r1);
if (k.generate() != 0) {
p__privateKey = OCTETSTRING();
p__publicKeyX = OCTETSTRING();
p__publicKeyY = OCTETSTRING();
return FALSE;
}
p__privateKey = OCTETSTRING(k.private_key().size(), k.private_key().data());
p__publicKeyX = OCTETSTRING(k.public_key_x().size(), k.public_key_x().data());
p__publicKeyY = OCTETSTRING(k.public_key_y().size(), k.public_key_y().data());
return TRUE;
}
/**
* @desc Produce a new public/private key pair based on Elliptic Curve Digital Signature Algorithm (ECDSA) algorithm.
* This function should not be used by the ATS
* @param p_privateKey The new private key value
* @param p_publicKeyX The new public key value (x coordinate)
* @param p_publicKeyX The new public key value (y coordinate)
* @return true on success, false otherwise
fx_generateKeyPair_nistp256(out octetstring<UInt64> p_privateKey, out octetstring p_publicKeyX, out octetstring p_publicKeyY) return boolean;
*/
BOOLEAN fx__generateKeyPair__brainpoolp384(
OCTETSTRING& p__privateKey,
OCTETSTRING& p__publicKeyX,
OCTETSTRING& p__publicKeyY
) {
ec_keys k(ec_elliptic_curves::brainpool_p_384_r1);
if (k.generate() != 0) {
p__privateKey = OCTETSTRING();
p__publicKeyX = OCTETSTRING();
p__publicKeyY = OCTETSTRING();
return FALSE;
}
p__privateKey = OCTETSTRING(k.private_key().size(), k.private_key().data());
p__publicKeyX = OCTETSTRING(k.public_key_x().size(), k.public_key_x().data());
p__publicKeyY = OCTETSTRING(k.public_key_y().size(), k.public_key_y().data());
return TRUE;
// group encryption
// group certificatesLoader
/* * @desc Load in memory cache the certificates available in the specified directory
* @param p_rootDirectory Root directory to access to the certificates identified by the certificate ID
* @param p_configId A configuration identifier
* @remark This method SHALL be call before any usage of certificates
* @return true on success, false otherwise
fx_loadCertificates(in charstring p_rootDirectory, in charstring p_configId) return boolean;
*/
BOOLEAN fx__loadCertificates(
const CHARSTRING& p__rootDirectory,
const CHARSTRING& p__configId
) {
return TRUE;
}
/* * @desc Unload from memory cache the certificates
* @return true on success, false otherwise
fx_unloadCertificates() return boolean;
*/
BOOLEAN fx__unloadCertificates(
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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
return TRUE;
}
/* * @desc Read the specified certificate
* @param p_certificateId the certificate identifier
* @param p_certificate the expected certificate
* @return true on success, false otherwise
fx_readCertificate(in charstring p_certificateId, out octetstring p_certificate) return boolean;
*/
BOOLEAN fx__readCertificate(
const CHARSTRING& p__certificateId,
OCTETSTRING& p__certificate
) {
return TRUE;
}
/* * @desc Read the specified certificate digest
* @param p_certificateId the certificate identifier
* @param p_digest the expected certificate
* @return true on success, false otherwise
fx_readCertificateDigest(in charstring p_certificateId, out HashedId8 p_digest) return boolean;
*/
BOOLEAN fx__readCertificateDigest(
const CHARSTRING& p__certificateId,
OCTETSTRING& p__digest
) {
return TRUE;
}
/* * @desc Read the private keys for the specified certificate
* @param p_keysId the keys identifier
* @param p_signingPrivateKey the signing private key
* @return true on success, false otherwise
fx_readSigningKey(in charstring p_keysId, out Oct32 p_signingPrivateKey) return boolean;
*/
BOOLEAN fx__readSigningKey(
const CHARSTRING& p__keysId,
OCTETSTRING& p__signingPrivateKey
) {
return TRUE;
}
/* * @desc Read the private keys for the specified certificate
* @param p_keysId the keys identifier
* @param p_encryptPrivateKey the encrypt private key
* @return true on success, false otherwise
fx_readEncryptingKey(in charstring p_keysId, out Oct32 p_encryptingPrivateKey) return boolean;
*/
BOOLEAN fx__readEncryptingKey(
const CHARSTRING& p__keysId,
OCTETSTRING& p__encryptingPrivateKey
) {
return TRUE;
}
// group geodesic
/* * @desc Check that given polygon doesn't have neither self-intersections nor holes.
* @param p_region Polygonal Region
* @return true on success, false otherwise
* @verdict Unchanged
fx_isValidPolygonalRegion(in PolygonalRegion p_region) return boolean;
*/
BOOLEAN fx__isValidPolygonalRegion(
const IEEE1609dot2BaseTypes::PolygonalRegion& p__region
) {
return TRUE;
}
/* * @desc Check if a polygonal region is inside another one
* @param p_parent The main polygonal region
* @param p_region The polygonal region to be included
* @return true on success, false otherwise
* @verdict Unchanged
fx_isPolygonalRegionInside(in PolygonalRegion p_parent, in PolygonalRegion p_region) return boolean;
*/
BOOLEAN fx__isPolygonalRegionInside(
const IEEE1609dot2BaseTypes::PolygonalRegion& p__parent,
const IEEE1609dot2BaseTypes::PolygonalRegion& p__region
) {
return TRUE;
}
/* * @desc Check that the location is inside a circular region
* @param p_region The circular region to consider
* @param p_location The device location
* @return true on success, false otherwise
* @verdict Unchanged
fx_isLocationInsideCircularRegion(in CircularRegion p_region, in ThreeDLocation p_location) return boolean;
*/
BOOLEAN fx__isLocationInsideCircularRegion(
const IEEE1609dot2BaseTypes::CircularRegion& p__region,
const IEEE1609dot2BaseTypes::ThreeDLocation& p__location
) {
return TRUE;
}
/* * @desc Check that the location is inside a rectangular region
* @param p_region The rectangular region to consider
* @param p_location The device location
* @return true on success, false otherwise
* @verdict Unchanged
fx_isLocationInsideRectangularRegion(in SequenceOfRectangularRegion p_region, in ThreeDLocation p_location) return boolean;
*/
BOOLEAN fx__isLocationInsideRectangularRegion(
const IEEE1609dot2BaseTypes::SequenceOfRectangularRegion& p__region,
const IEEE1609dot2BaseTypes::ThreeDLocation& p__location
) {
return TRUE;
}
/* * @desc Check that the location is inside a polygonal region
* @param p_region The polygonal region to consider
* @param p_location The device location
* @return true on success, false otherwise
* @verdict Unchanged
fx_isLocationInsidePolygonalRegion(in PolygonalRegion p_region, in ThreeDLocation p_location) return boolean;
*/
BOOLEAN fx__isLocationInsidePolygonalRegion(
const IEEE1609dot2BaseTypes::PolygonalRegion& p__region,
const IEEE1609dot2BaseTypes::ThreeDLocation& p__location
) {
return TRUE;
}
/* * @desc Check if the location is inside an identified region
* @param p_region The identified region to consider
* @param p_location The device location
* @return true on success, false otherwise
* @verdict Unchanged
fx_isLocationInsideIdentifiedRegion(in IdentifiedRegion p_region, in ThreeDLocation p_location) return boolean;
*/
BOOLEAN fx__isLocationInsideIdentifiedRegion(
const IEEE1609dot2BaseTypes::IdentifiedRegion& p__region,
const IEEE1609dot2BaseTypes::ThreeDLocation& p__location
) {
return TRUE;
}
/* * @desc Check if the location is inside an undefined region
* @param p_region The identified region to consider
* @param p_location The device location
* @return true on success, false otherwise
* @verdict Unchanged
fx_isLocationInsideOtherRegion(in octetstring p_region, in ThreeDLocation p_location) return boolean;
*/
BOOLEAN fx__isLocationInsideOtherRegion(
const OCTETSTRING& p_region,
const IEEE1609dot2BaseTypes::ThreeDLocation& p_location
) {
return TRUE;
}
/* * @desc Check that p_circular_region_1 circular region is included into p_circular_region_2 circular region
* @param p_circular_region_1 Circular region 1
* @param p_circular_region_2 Circular region 2
* @return true on success, false otherwise
fx_areCirclesInside(in CircularRegion p_circular_region_1, in CircularRegion p_circular_region_2) return boolean;
*/
BOOLEAN fx__areCirclesInside(
const IEEE1609dot2BaseTypes::CircularRegion& p_circular_region_1,
const IEEE1609dot2BaseTypes::CircularRegion& p_circular_region_2
) {
return TRUE;
}
/* * @desc Check that p_rectanglar_region_1 rectangular region is included into p_rectanglar_region_2 rectangular region
* @param p_rectanglar_region_1 Rectangular region 1
* @param p_rectanglar_region_2 Rectangular region 2
* @return true on success, false otherwise
fx_areRectanglesInside(in SequenceOfRectangularRegion p_rectanglar_region_1, in SequenceOfRectangularRegion p_rectanglar_region_2) return boolean;
*/
BOOLEAN fx__areRectanglesInside(
const IEEE1609dot2BaseTypes::SequenceOfRectangularRegion& p_rectanglar_region_1,
const IEEE1609dot2BaseTypes::SequenceOfRectangularRegion& p_rectanglar_region_2
) {
return TRUE;
}
/* * @desc Check that p_polygonal_region_1 polygonal region is included into p_polygonal_region_2 polygonal region
* @param p_polygonal_region_1 Polygonal region 1
* @param p_polygonal_region_2 Polygonal region 2
* @return true on success, false otherwise
fx_arePolygonsInside(in PolygonalRegion p_polygonal_region_1, in PolygonalRegion p_polygonal_region_2) return boolean;
*/
BOOLEAN fx__arePolygonsInside(
const IEEE1609dot2BaseTypes::PolygonalRegion& p_polygonal_region_1,
const IEEE1609dot2BaseTypes::PolygonalRegion& p_polygonal_region_2
) {
return TRUE;
}
/* * @desc Convert a spacial coordinate from DMS to Dms
* @param p_degrees The degrees (D)
* @param p_minutes The minutes (M)
* @param p_seconds The seconds (S)
* @param p_latlon The latitude/longitude: (N|S|E|W)
* @return The decimal coordinate on success, 0.0, otherwise
* @verdict Unchanged
fx_dms2dd(in Int p_degrees, in Int p_minutes, in float p_seconds, in Oct1 p_latlon) return float;
*/
FLOAT fx__dms2dd(
const INTEGER& p__degrees,
const INTEGER& p__minutes,
const FLOAT& p__seconds,
const OCTETSTRING& p__latlon
) {
return 0.0;
}