/usr/include/x86_64-linux-gnu/qcc/CryptoECC.h is in liballjoyn-common-dev-1509 15.09a-5.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 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 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 | #ifndef _CRYPTOECC_H
#define _CRYPTOECC_H
/**
* @file
*
* This file provide wrappers around ECC cryptographic algorithms.
*/
/******************************************************************************
* Copyright AllSeen Alliance. All rights reserved.
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
******************************************************************************/
#include <alljoyn/Status.h>
#include <assert.h>
#include <qcc/platform.h>
#include <qcc/String.h>
namespace qcc {
/**
* The ECC coordinate size
*/
static const size_t ECC_COORDINATE_SZ = 8 * sizeof(uint32_t);
/**
* The ECC private key
*
* At the moment, because the code only supports one curve, private keys
* are not innately tied to a particular curve. In the future, if the code
* supports more than one curve, a private key should store its curve also.
*/
class ECCPrivateKey {
/**
* The ECCPrivateKey data
*/
uint8_t d[ECC_COORDINATE_SZ];
public:
/**
* ECCPrivateKey constructor
*/
ECCPrivateKey() {
memset(d, 0, ECC_COORDINATE_SZ);
}
/**
* ECCPrivateKey destructor
*/
~ECCPrivateKey();
/**
* the assign operator for the ECCPrivateKey
*
* @param[in] other the ECCPrivate key to assign
*/
ECCPrivateKey& operator=(const ECCPrivateKey& other)
{
if (this != &other) {
memcpy(d, other.d, ECC_COORDINATE_SZ);
}
return *this;
}
/**
* Get the size of the private key value
*
* @return Size of the private key in bytes
*/
const size_t GetSize() const
{
return ECC_COORDINATE_SZ;
}
/**
* Return the ECCPrivateKey as a string
* @return the ECCPrivateKey as a string
*/
const String ToString() const;
/**
* Import the key from a byte array.
* @param[in] data the array to store the data in
* @param[in] size the size of the passed buffer
*
* @return ER_OK on success others on failure
*/
QStatus Import(const uint8_t* data, const size_t size)
{
if (ECC_COORDINATE_SZ != size) {
return ER_BAD_ARG_2;
}
memcpy(d, data, size);
return ER_OK;
}
/**
* Export the key to a byte array.
* @param[in] data the array to store the data in
* @param[in,out] size provides the size of the passed buffer as input. On a successful return it
* will contain the actual amount of data stored, which is the same value as returned by GetSize().
* On ER_BUFFER_TOO_SMALL, contains the amount of storage required, which is also the value returned
* by GetSize().
*
* @return ER_OK on success others on failure
*/
QStatus Export(uint8_t* data, size_t* size) const;
/**
* Equals operator for the ECCPrivateKey.
*
* @param[in] k the ECCPrivateKey to compare
*
* @return true if the ECCPrivateKeys are equal
*/
bool operator==(const ECCPrivateKey& k) const
{
return memcmp(d, k.d, ECC_COORDINATE_SZ) == 0;
}
/** Support methods for CryptoECCMath **/
/*
* Return a buffer containing just the private key value
*
* @return Buffer containing the private key value
*/
const uint8_t* GetD() const
{
return this->d;
}
const size_t GetDSize() const
{
return this->GetSize();
}
};
/**
* The ECC public key
*
* At the moment, because the code only supports one curve, private keys
* are not innately tied to a particular curve. In the future, if the code
* supports more than one curve, a private key should store its curve also.
*/
class ECCPublicKey {
/**
* The x coordinate of the elliptic curve
*/
uint8_t x[ECC_COORDINATE_SZ];
/**
* The y coordinate of the elliptic curve
*/
uint8_t y[ECC_COORDINATE_SZ];
public:
/**
* Clear the key to make it empty.
*/
void Clear()
{
memset(x, 0, GetCoordinateSize());
memset(y, 0, GetCoordinateSize());
}
ECCPublicKey()
{
Clear();
}
/**
* Check to see if the ECCPublicKey is empty
*
* @return true if the ECCPublicKey is empty
*/
bool empty() const;
/**
* Equals operator
*
* @param[in] k the ECCPublic key to compare
*
* @return true if the compared ECCPublicKeys are equal to each other
*/
bool operator==(const ECCPublicKey& k) const
{
int n = memcmp(x, k.x, GetCoordinateSize());
return (n == 0) && (0 == memcmp(y, k.y, GetCoordinateSize()));
}
/**
* Not equals operator
*
* @param[in] k the ECCPublicKey to compare
*
* @return true if the compared ECCPublicKeys are not equal to each other
*/
bool operator!=(const ECCPublicKey& k) const
{
return !(*this == k);
}
/**
* The less than operator for the ECCPublicKey
*
* The x coordinate are compared first. If the x coordinates match then
* the y coordinate is compared.
*
* @param[in] k the ECCPublicKey to compare
*
* @return True if the left ECCPublicKey is less than the right ECCPublicKey
* false otherwise.
*/
bool operator<(const ECCPublicKey& k) const
{
int n = memcmp(x, k.x, GetCoordinateSize());
if (n == 0) {
n = memcmp(y, k.y, GetCoordinateSize());
}
if (n < 0) {
return true;
} else {
return false;
}
}
/**
* Copy constructor for ECCPublicKey
*
* @param[in] other the ECCPublicKey to copy
*/
ECCPublicKey(const ECCPublicKey& other)
{
memcpy(x, other.x, ECC_COORDINATE_SZ);
memcpy(y, other.y, ECC_COORDINATE_SZ);
}
/**
* Assign operator for ECCPublicKey
*
* @param[in] other the ECCPublic key to assign
*/
ECCPublicKey& operator=(const ECCPublicKey& other)
{
if (this != &other) {
memcpy(x, other.x, GetCoordinateSize());
memcpy(y, other.y, GetCoordinateSize());
}
return *this;
}
/**
* Export the key to a byte array. The X and Y coordinates are concatenated in that order, and each
* occupy exactly half of the returned array. The X coordinate is in the first half, and the Y coordinate
* in the second. Use the returned size divided by two as the length of an individual coordinate.
* @param[in] data the array to store the data in
* @param[in,out] size provides the size of the passed buffer as input. On a successful return it
* will contain the actual amount of data stored
*
* @return ER_OK on success others on failure
*/
QStatus Export(uint8_t* data, size_t* size) const;
/**
* Import the key from a byte array
* @param[in] data the array to store the data in
* @param[in] size the size of the passed buffer
*
* @return ER_OK on success others on failure
*/
QStatus Import(const uint8_t* data, const size_t size);
/**
* Import the key from two byte arrays, one containing each coordinate
* @param[in] xData array containing the bytes of the X coordinate
* @param[in] xSize length of xData
* @param[in] yData array containing the bytes of the Y coordinate
* @param[in] ySize length of yData
*
* @return ER_OK on success others on failure
*/
QStatus Import(const uint8_t* xData, const size_t xSize, const uint8_t* yData, const size_t ySize);
/**
* Return the ECCPublicKey to a string
* @return the ECCPublicKey as a string.
*/
const String ToString() const;
/**
* Return the size of the public key in exported form
*
* @return Size of the exported public key
*/
inline const size_t GetSize() const
{
return 2 * GetCoordinateSize();
}
/** Support methods for CryptoECCMath **/
/*
* Get a buffer containing just the X coordinate of this public key
*
* @return Buffer containing the X value
*/
const uint8_t* GetX() const
{
return this->x;
}
/**
* Get a buffer containing just the Y coordinate of this public key
*
* @return Buffer containing the Y value
*/
const uint8_t* GetY() const
{
return this->y;
}
/*
* Get the size of a single coordinate
*
* @return The size of a single coordinate
*/
inline const size_t GetCoordinateSize() const
{
assert(sizeof(this->x) == sizeof(this->y));
return sizeof(this->x);
}
};
/**
* The ECC secret
*/
class ECCSecret {
public:
/**
* Opaque type for the internal state.
*/
struct ECCSecretState;
/**
* Default Constructor;
*/
ECCSecret();
/**
* Set the opaque secret state for this object
* @param pEccSecretState the internal secret state to set.
* @return
* ER_OK if the secret is successfully set.
* ER_FAIL otherwise.
* Other error status.
*/
QStatus SetSecretState(const ECCSecretState* pEccSecretState);
/**
* Derives the PreMasterSecret.
* Current implementaiton uses SHA256 HASH KDF.
* @param pbPreMasterSecret buffer to receive premaster secret.
* @param cbPreMasterSecret count of buffer to receive premaster secret.
* @return
* ER_OK if the pre-master secret is successfully computed and put in pbPreMasterSecret.
* ER_FAIL otherwise.
* Other error status.
*/
QStatus DerivePreMasterSecret(uint8_t* pbPreMasterSecret, size_t cbPreMasterSecret);
/**
* Default Destructor
*/
~ECCSecret();
private:
/* private copy constructor to prevent double delete of eccSecretState */
ECCSecret(const ECCSecret&);
/* private assignment operator to prevent double delete of eccSecretState */
ECCSecret& operator=(const ECCSecret&);
/**
* Private internal state
*/
ECCSecretState* eccSecretState;
};
/**
* The ECC signature big endian byte array
*/
struct ECCSignature {
/**
* The r value for the Elliptic Curve Digital Signature (r,s) signature pair
*/
uint8_t r[ECC_COORDINATE_SZ];
/**
* The s value for the Elliptic Curve Digital Signature (r,s) signature pair
*/
uint8_t s[ECC_COORDINATE_SZ];
/**
* ECCSignature constructor
*
* The Elliptic Curve Digital Signature (r,s) signature initialized to zero.
*/
ECCSignature() {
memset(r, 0, ECC_COORDINATE_SZ);
memset(s, 0, ECC_COORDINATE_SZ);
}
/**
* The ECCSignature copy operator
*
* @param[in] other the ECCSignature to copy
*/
ECCSignature(const ECCSignature& other)
{
memcpy(r, other.r, ECC_COORDINATE_SZ);
memcpy(s, other.s, ECC_COORDINATE_SZ);
}
/**
* The ECCSignature assign operator
* @param[in] other the ECC signature to assign
*/
ECCSignature& operator=(const ECCSignature& other)
{
if (this != &other) {
memcpy(r, other.r, ECC_COORDINATE_SZ);
memcpy(s, other.s, ECC_COORDINATE_SZ);
}
return *this;
}
};
/**
* Elliptic Curve Cryptography
*/
class Crypto_ECC {
public:
/**
* The NIST recommended elliptic curve P-256
*/
static const uint8_t ECC_NIST_P256 = 0;
/**
* Default constructor.
*/
Crypto_ECC();
/**
* Generates the Ephemeral Diffie-Hellman key pair.
*
* @return
* ER_OK if the key pair is successfully generated.
* ER_FAIL otherwise
* Other error status.
*/
QStatus GenerateDHKeyPair();
/**
* Generates the Diffie-Hellman shared secret.
* @param peerPublicKey the peer's public key
* @param secret the output shared secret
* @return
* ER_OK if the shared secret is successfully generated.
* ER_FAIL otherwise
* Other error status.
*/
QStatus GenerateSharedSecret(const ECCPublicKey* peerPublicKey, ECCSecret* secret);
/**
* Retrieve the DH public key
* @return the DH public key. It's a pointer to an internal buffer. Its lifetime is the same as the object's lifetime.
*/
const ECCPublicKey* GetDHPublicKey() const;
/**
* Assign the DH public key
* @param pubKey the public key to copy
*/
void SetDHPublicKey(const ECCPublicKey* pubKey);
/**
* Retrieve the DH private key
* @return the DH private key. Same lifetime as the object.
*/
const ECCPrivateKey* GetDHPrivateKey() const;
/**
* Assign the DH private key
* @param privateKey the private key to copy
*/
void SetDHPrivateKey(const ECCPrivateKey* privateKey);
/**
* Retrieve the DSA public key
* @return the DSA public key. Same lifetime as the object.
*/
const ECCPublicKey* GetDSAPublicKey() const;
/**
* Assign the DSA public key
* @param pubKey the public key to copy
*/
void SetDSAPublicKey(const ECCPublicKey* pubKey);
/**
* Retrieve the DSA private key
* @return the DSA private key. Same lifetime as the object.
*/
const ECCPrivateKey* GetDSAPrivateKey() const;
/**
* Assign the DSA private key
* @param privateKey the private key to copy
*/
void SetDSAPrivateKey(const ECCPrivateKey* privateKey);
/**
* Generates the DSA key pair.
*
* @return
* ER_OK if the key pair is successfully generated.
* ER_FAIL otherwise
* Other error status.
*/
QStatus GenerateDSAKeyPair();
/**
* Sign a digest using the DSA key
* @param digest The digest to sign
* @param len The digest len
* @param sig The output signature
* @return
* ER_OK if the signing process succeeds
* ER_FAIL otherwise
* Other error status.
*/
QStatus DSASignDigest(const uint8_t* digest, uint16_t len, ECCSignature* sig);
/**
* Sign a buffer using the DSA key
* @param buf The buffer to sign
* @param len The buffer len
* @param sig The output signature
* @return
* ER_OK if the signing process succeeds
* ER_FAIL otherwise
* Other error status.
*/
QStatus DSASign(const uint8_t* buf, uint16_t len, ECCSignature* sig);
/**
* Verify DSA signature of a digest
* @param digest The digest to sign
* @param len The digest len
* @param sig The signature
* @return - ER_OK if the signature verification succeeds
* - ER_FAIL otherwise
* - Other error status.
*/
QStatus DSAVerifyDigest(const uint8_t* digest, uint16_t len, const ECCSignature* sig);
/**
* Verify DSA signature of a buffer
* @param buf The buffer to sign
* @param len The buffer len
* @param sig The signature
* @return
* ER_OK if the signature verification succeeds
* ER_FAIL otherwise
* Other error status.
*/
QStatus DSAVerify(const uint8_t* buf, uint16_t len, const ECCSignature* sig);
/**
* Retrieve the ECC curve type.
* @return the ECC curve type
*/
const uint8_t GetCurveType()
{
return ECC_NIST_P256;
}
~Crypto_ECC();
private:
/* private copy constructor to prevent double freeing of eccState */
Crypto_ECC(const Crypto_ECC&);
/* private assignment operator to prevent double freeing of eccState */
Crypto_ECC& operator=(const Crypto_ECC&);
/**
* Opaque type for the internal state.
*/
struct ECCState;
/**
* Private internal state
*/
ECCState* eccState;
};
} /* namespace qcc */
#endif
|