test-blinding.cpp 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. #include <cassert>
  2. #include <memory>
  3. #include <string.h>
  4. #include "Blinding.h"
  5. #include "Identity.h"
  6. #include "Timestamp.h"
  7. using namespace i2p::data;
  8. using namespace i2p::util;
  9. using namespace i2p::crypto;
  10. void BlindTest (SigningKeyType sigType)
  11. {
  12. auto keys = PrivateKeys::CreateRandomKeys (sigType);
  13. BlindedPublicKey blindedKey (keys.GetPublic ());
  14. auto timestamp = GetSecondsSinceEpoch ();
  15. char date[9];
  16. GetDateString (timestamp, date);
  17. uint8_t blindedPriv[64], blindedPub[128];
  18. auto publicKeyLen = blindedKey.BlindPrivateKey (keys.GetSigningPrivateKey (), date, blindedPriv, blindedPub);
  19. uint8_t blindedPub1[128];
  20. blindedKey.GetBlindedKey (date, blindedPub1);
  21. // check if public key produced from private blinded key matches blided public key
  22. assert (!memcmp (blindedPub, blindedPub1, publicKeyLen));
  23. // try to sign and verify
  24. std::unique_ptr<Signer> blindedSigner (PrivateKeys::CreateSigner (sigType, blindedPriv));
  25. uint8_t buf[100], signature[128];
  26. memset (buf, 1, 100);
  27. blindedSigner->Sign (buf, 100, signature);
  28. std::unique_ptr<Verifier> blindedVerifier (IdentityEx::CreateVerifier (sigType));
  29. blindedVerifier->SetPublicKey (blindedPub1);
  30. assert (blindedVerifier->Verify (buf, 100, signature));
  31. }
  32. int main ()
  33. {
  34. // RedDSA test
  35. BlindTest (SIGNING_KEY_TYPE_REDDSA_SHA512_ED25519);
  36. // P256 test
  37. BlindTest (SIGNING_KEY_TYPE_ECDSA_SHA256_P256);
  38. // P384 test
  39. BlindTest (SIGNING_KEY_TYPE_ECDSA_SHA384_P384);
  40. }