MCPcopy Create free account
hub / github.com/PurpleI2P/i2pd / ECIESDecrypt

Function ECIESDecrypt

libi2pd/Crypto.cpp:486–535  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

484 }
485
486 bool ECIESDecrypt (const EC_GROUP * curve, const BIGNUM * key, const uint8_t * encrypted, uint8_t * data)
487 {
488 bool ret = true;
489 BN_CTX * ctx = BN_CTX_new ();
490 BN_CTX_start (ctx);
491 BIGNUM * q = BN_CTX_get (ctx);
492 EC_GROUP_get_order(curve, q, ctx);
493 int len = BN_num_bytes (q);
494 // point for shared secret
495 BIGNUM * x = BN_CTX_get (ctx), * y = BN_CTX_get (ctx);
496 BN_bin2bn (encrypted + 1, len, x);
497 BN_bin2bn (encrypted + 1 + len, len, y);
498 auto p = EC_POINT_new (curve);
499 if (EC_POINT_set_affine_coordinates (curve, p, x, y, nullptr))
500 {
501 auto s = EC_POINT_new (curve);
502 EC_POINT_mul (curve, s, nullptr, p, key, ctx);
503 EC_POINT_get_affine_coordinates (curve, s, x, y, nullptr);
504 EC_POINT_free (s);
505 uint8_t keyBuf[64], iv[64], shared[32];
506 bn2buf (x, keyBuf, len);
507 bn2buf (y, iv, len);
508 SHA256 (keyBuf, len, shared);
509 // decrypt
510 uint8_t m[256];
511 CBCDecryption decryption;
512 decryption.SetKey (shared);
513 decryption.Decrypt (encrypted + 258, 256, iv, m);
514 // verify and copy
515 uint8_t hash[32];
516 SHA256 (m + 33, 222, hash);
517 if (!memcmp (m + 1, hash, 32))
518 memcpy (data, m + 33, 222);
519 else
520 {
521 LogPrint (eLogError, "ECIES decrypt hash doesn't match");
522 ret = false;
523 }
524 }
525 else
526 {
527 LogPrint (eLogError, "ECIES decrypt point is invalid");
528 ret = false;
529 }
530
531 EC_POINT_free (p);
532 BN_CTX_end (ctx);
533 BN_CTX_free (ctx);
534 return ret;
535 }
536
537 void GenerateECIESKeyPair (const EC_GROUP * curve, BIGNUM *& priv, EC_POINT *& pub)
538 {

Callers 1

DecryptMethod · 0.85

Calls 4

bn2bufFunction · 0.85
LogPrintFunction · 0.85
SetKeyMethod · 0.45
DecryptMethod · 0.45

Tested by

no test coverage detected