LCOV - code coverage report
Current view: top level - src/bls - bls_ies.h (source / functions) Hit Total Coverage
Test: total_coverage.info Lines: 38 44 86.4 %
Date: 2026-06-25 07:23:43 Functions: 42 42 100.0 %

          Line data    Source code
       1             : // Copyright (c) 2018-2024 The Dash Core developers
       2             : // Distributed under the MIT software license, see the accompanying
       3             : // file COPYING or http://www.opensource.org/licenses/mit-license.php.
       4             : 
       5             : #ifndef DASH_CRYPTO_BLS_IES_H
       6             : #define DASH_CRYPTO_BLS_IES_H
       7             : 
       8             : #include <bls/bls.h>
       9             : #include <streams.h>
      10             : 
      11             : /**
      12             :  * All objects in this module working from assumption that basic scheme is
      13             :  * available on all masternodes. Serialization of public key for Encrypt and
      14             :  * Decrypt by bls_ies.h done using Basic Scheme.
      15             :  */
      16         344 : class CBLSIESEncryptedBlob
      17             : {
      18             : public:
      19             :     CBLSPublicKey ephemeralPubKey;
      20             :     uint256 ivSeed;
      21             :     std::vector<unsigned char> data;
      22             : 
      23             :     uint256 GetIV(size_t idx) const;
      24             : 
      25        1032 :     SERIALIZE_METHODS(CBLSIESEncryptedBlob, obj)
      26             :     {
      27         344 :         READWRITE(obj.ephemeralPubKey, obj.ivSeed, obj.data);
      28         344 :     }
      29             : 
      30             :     bool Decrypt(size_t idx, const CBLSSecretKey& secretKey, CDataStream& decryptedDataRet) const;
      31             :     bool IsValid() const;
      32             : };
      33             : 
      34             : template <typename Object>
      35             : class CBLSIESEncryptedObject : public CBLSIESEncryptedBlob
      36             : {
      37             : public:
      38         254 :     CBLSIESEncryptedObject() = default;
      39             : 
      40         434 :     CBLSIESEncryptedObject(const CBLSPublicKey& ephemeralPubKeyIn, const uint256& ivSeedIn, const std::vector<unsigned char>& dataIn)
      41         217 :     {
      42         217 :         ephemeralPubKey = ephemeralPubKeyIn;
      43         217 :         ivSeed = ivSeedIn;
      44         217 :         data = dataIn;
      45         434 :     }
      46             : 
      47         127 :     bool Decrypt(size_t idx, const CBLSSecretKey& secretKey, Object& objRet, int nVersion) const
      48             :     {
      49         127 :         CDataStream ds(SER_NETWORK, nVersion);
      50         127 :         if (!CBLSIESEncryptedBlob::Decrypt(idx, secretKey, ds)) {
      51           0 :             return false;
      52             :         }
      53             :         try {
      54         127 :             ds >> objRet;
      55         127 :         } catch (const std::exception&) {
      56           0 :             return false;
      57           0 :         }
      58         127 :         return true;
      59         127 :     }
      60             : };
      61             : 
      62       20732 : class CBLSIESMultiRecipientBlobs
      63             : {
      64             : public:
      65             :     using Blob = std::vector<unsigned char>;
      66             :     using BlobVector = std::vector<Blob>;
      67             : 
      68             :     CBLSPublicKey ephemeralPubKey;
      69             :     uint256 ivSeed;
      70             :     BlobVector blobs;
      71             : 
      72             :     // Used while encrypting. Temporary and only in-memory
      73             :     CBLSSecretKey ephemeralSecretKey;
      74             :     std::vector<uint256> ivVector;
      75             : 
      76             :     void InitEncrypt(size_t count);
      77             :     bool Encrypt(size_t idx, const CBLSPublicKey& recipient, const Blob& blob);
      78             :     bool Decrypt(size_t idx, const CBLSSecretKey& sk, Blob& blobRet) const;
      79             : 
      80      142328 :     SERIALIZE_METHODS(CBLSIESMultiRecipientBlobs, obj)
      81             :     {
      82       47442 :         READWRITE(obj.ephemeralPubKey, obj.ivSeed, obj.blobs);
      83       47442 :     }
      84             : };
      85             : 
      86             : template <typename Object>
      87             : class CBLSIESMultiRecipientObjects : public CBLSIESMultiRecipientBlobs
      88             : {
      89             : public:
      90        9780 :     bool Encrypt(size_t idx, const CBLSPublicKey& recipient, const Object& obj, int nVersion)
      91             :     {
      92        9780 :         CDataStream ds(SER_NETWORK, nVersion);
      93        9780 :         ds << obj;
      94        9780 :         Blob blob(UCharCast(ds.data()), UCharCast(ds.data() + ds.size()));
      95        9780 :         return CBLSIESMultiRecipientBlobs::Encrypt(idx, recipient, blob);
      96        9780 :     }
      97             : 
      98        8948 :     bool Decrypt(size_t idx, const CBLSSecretKey& sk, Object& objectRet, int nVersion) const
      99             :     {
     100        8948 :         Blob blob;
     101        8948 :         if (!CBLSIESMultiRecipientBlobs::Decrypt(idx, sk, blob)) {
     102           0 :             return false;
     103             :         }
     104             : 
     105             :         try {
     106        8948 :             CDataStream ds(blob, SER_NETWORK, nVersion);
     107        8948 :             ds >> objectRet;
     108        8948 :             return true;
     109        8948 :         } catch (const std::exception&) {
     110           0 :             return false;
     111           0 :         }
     112        8948 :     }
     113             : 
     114         217 :     CBLSIESEncryptedObject<Object> Get(const size_t idx)
     115             :     {
     116         217 :         return {ephemeralPubKey, ivSeed, blobs[idx]};
     117             :     }
     118             : };
     119             : 
     120             : #endif // DASH_CRYPTO_BLS_IES_H

Generated by: LCOV version 1.16