Signature.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531
  1. #ifndef SIGNATURE_H__
  2. #define SIGNATURE_H__
  3. #include <inttypes.h>
  4. #include <string.h>
  5. #include <openssl/dsa.h>
  6. #include <openssl/ec.h>
  7. #include <openssl/ecdsa.h>
  8. #include <openssl/evp.h>
  9. #include "Crypto.h"
  10. #include "Ed25519.h"
  11. #include "Gost.h"
  12. namespace i2p
  13. {
  14. namespace crypto
  15. {
  16. class Verifier
  17. {
  18. public:
  19. virtual ~Verifier () {};
  20. virtual bool Verify (const uint8_t * buf, size_t len, const uint8_t * signature) const = 0;
  21. virtual size_t GetPublicKeyLen () const = 0;
  22. virtual size_t GetSignatureLen () const = 0;
  23. virtual size_t GetPrivateKeyLen () const { return GetSignatureLen ()/2; };
  24. virtual void SetPublicKey (const uint8_t * signingKey) = 0;
  25. };
  26. class Signer
  27. {
  28. public:
  29. virtual ~Signer () {};
  30. virtual void Sign (const uint8_t * buf, int len, uint8_t * signature) const = 0;
  31. };
  32. const size_t DSA_PUBLIC_KEY_LENGTH = 128;
  33. const size_t DSA_SIGNATURE_LENGTH = 40;
  34. const size_t DSA_PRIVATE_KEY_LENGTH = DSA_SIGNATURE_LENGTH/2;
  35. class DSAVerifier: public Verifier
  36. {
  37. public:
  38. DSAVerifier ()
  39. {
  40. m_PublicKey = CreateDSA ();
  41. }
  42. void SetPublicKey (const uint8_t * signingKey)
  43. {
  44. DSA_set0_key (m_PublicKey, BN_bin2bn (signingKey, DSA_PUBLIC_KEY_LENGTH, NULL), NULL);
  45. }
  46. ~DSAVerifier ()
  47. {
  48. DSA_free (m_PublicKey);
  49. }
  50. bool Verify (const uint8_t * buf, size_t len, const uint8_t * signature) const
  51. {
  52. // calculate SHA1 digest
  53. uint8_t digest[20];
  54. SHA1 (buf, len, digest);
  55. // signature
  56. DSA_SIG * sig = DSA_SIG_new();
  57. DSA_SIG_set0 (sig, BN_bin2bn (signature, DSA_SIGNATURE_LENGTH/2, NULL), BN_bin2bn (signature + DSA_SIGNATURE_LENGTH/2, DSA_SIGNATURE_LENGTH/2, NULL));
  58. // DSA verification
  59. int ret = DSA_do_verify (digest, 20, sig, m_PublicKey);
  60. DSA_SIG_free(sig);
  61. return ret;
  62. }
  63. size_t GetPublicKeyLen () const { return DSA_PUBLIC_KEY_LENGTH; };
  64. size_t GetSignatureLen () const { return DSA_SIGNATURE_LENGTH; };
  65. private:
  66. DSA * m_PublicKey;
  67. };
  68. class DSASigner: public Signer
  69. {
  70. public:
  71. DSASigner (const uint8_t * signingPrivateKey, const uint8_t * signingPublicKey)
  72. // openssl 1.1 always requires DSA public key even for signing
  73. {
  74. m_PrivateKey = CreateDSA ();
  75. DSA_set0_key (m_PrivateKey, BN_bin2bn (signingPublicKey, DSA_PUBLIC_KEY_LENGTH, NULL), BN_bin2bn (signingPrivateKey, DSA_PRIVATE_KEY_LENGTH, NULL));
  76. }
  77. ~DSASigner ()
  78. {
  79. DSA_free (m_PrivateKey);
  80. }
  81. void Sign (const uint8_t * buf, int len, uint8_t * signature) const
  82. {
  83. uint8_t digest[20];
  84. SHA1 (buf, len, digest);
  85. DSA_SIG * sig = DSA_do_sign (digest, 20, m_PrivateKey);
  86. const BIGNUM * r, * s;
  87. DSA_SIG_get0 (sig, &r, &s);
  88. bn2buf (r, signature, DSA_SIGNATURE_LENGTH/2);
  89. bn2buf (s, signature + DSA_SIGNATURE_LENGTH/2, DSA_SIGNATURE_LENGTH/2);
  90. DSA_SIG_free(sig);
  91. }
  92. private:
  93. DSA * m_PrivateKey;
  94. };
  95. inline void CreateDSARandomKeys (uint8_t * signingPrivateKey, uint8_t * signingPublicKey)
  96. {
  97. DSA * dsa = CreateDSA ();
  98. DSA_generate_key (dsa);
  99. const BIGNUM * pub_key, * priv_key;
  100. DSA_get0_key(dsa, &pub_key, &priv_key);
  101. bn2buf (priv_key, signingPrivateKey, DSA_PRIVATE_KEY_LENGTH);
  102. bn2buf (pub_key, signingPublicKey, DSA_PUBLIC_KEY_LENGTH);
  103. DSA_free (dsa);
  104. }
  105. struct SHA256Hash
  106. {
  107. static void CalculateHash (const uint8_t * buf, size_t len, uint8_t * digest)
  108. {
  109. SHA256 (buf, len, digest);
  110. }
  111. enum { hashLen = 32 };
  112. };
  113. struct SHA384Hash
  114. {
  115. static void CalculateHash (const uint8_t * buf, size_t len, uint8_t * digest)
  116. {
  117. SHA384 (buf, len, digest);
  118. }
  119. enum { hashLen = 48 };
  120. };
  121. struct SHA512Hash
  122. {
  123. static void CalculateHash (const uint8_t * buf, size_t len, uint8_t * digest)
  124. {
  125. SHA512 (buf, len, digest);
  126. }
  127. enum { hashLen = 64 };
  128. };
  129. // EcDSA
  130. template<typename Hash, int curve, size_t keyLen>
  131. class ECDSAVerifier: public Verifier
  132. {
  133. public:
  134. ECDSAVerifier ()
  135. {
  136. m_PublicKey = EC_KEY_new_by_curve_name (curve);
  137. }
  138. void SetPublicKey (const uint8_t * signingKey)
  139. {
  140. BIGNUM * x = BN_bin2bn (signingKey, keyLen/2, NULL);
  141. BIGNUM * y = BN_bin2bn (signingKey + keyLen/2, keyLen/2, NULL);
  142. EC_KEY_set_public_key_affine_coordinates (m_PublicKey, x, y);
  143. BN_free (x); BN_free (y);
  144. }
  145. ~ECDSAVerifier ()
  146. {
  147. EC_KEY_free (m_PublicKey);
  148. }
  149. bool Verify (const uint8_t * buf, size_t len, const uint8_t * signature) const
  150. {
  151. uint8_t digest[Hash::hashLen];
  152. Hash::CalculateHash (buf, len, digest);
  153. ECDSA_SIG * sig = ECDSA_SIG_new();
  154. auto r = BN_bin2bn (signature, GetSignatureLen ()/2, NULL);
  155. auto s = BN_bin2bn (signature + GetSignatureLen ()/2, GetSignatureLen ()/2, NULL);
  156. ECDSA_SIG_set0(sig, r, s);
  157. // ECDSA verification
  158. int ret = ECDSA_do_verify (digest, Hash::hashLen, sig, m_PublicKey);
  159. ECDSA_SIG_free(sig);
  160. return ret;
  161. }
  162. size_t GetPublicKeyLen () const { return keyLen; };
  163. size_t GetSignatureLen () const { return keyLen; }; // signature length = key length
  164. private:
  165. EC_KEY * m_PublicKey;
  166. };
  167. template<typename Hash, int curve, size_t keyLen>
  168. class ECDSASigner: public Signer
  169. {
  170. public:
  171. ECDSASigner (const uint8_t * signingPrivateKey)
  172. {
  173. m_PrivateKey = EC_KEY_new_by_curve_name (curve);
  174. EC_KEY_set_private_key (m_PrivateKey, BN_bin2bn (signingPrivateKey, keyLen/2, NULL));
  175. }
  176. ~ECDSASigner ()
  177. {
  178. EC_KEY_free (m_PrivateKey);
  179. }
  180. void Sign (const uint8_t * buf, int len, uint8_t * signature) const
  181. {
  182. uint8_t digest[Hash::hashLen];
  183. Hash::CalculateHash (buf, len, digest);
  184. ECDSA_SIG * sig = ECDSA_do_sign (digest, Hash::hashLen, m_PrivateKey);
  185. const BIGNUM * r, * s;
  186. ECDSA_SIG_get0 (sig, &r, &s);
  187. // signatureLen = keyLen
  188. bn2buf (r, signature, keyLen/2);
  189. bn2buf (s, signature + keyLen/2, keyLen/2);
  190. ECDSA_SIG_free(sig);
  191. }
  192. private:
  193. EC_KEY * m_PrivateKey;
  194. };
  195. inline void CreateECDSARandomKeys (int curve, size_t keyLen, uint8_t * signingPrivateKey, uint8_t * signingPublicKey)
  196. {
  197. EC_KEY * signingKey = EC_KEY_new_by_curve_name (curve);
  198. EC_KEY_generate_key (signingKey);
  199. bn2buf (EC_KEY_get0_private_key (signingKey), signingPrivateKey, keyLen/2);
  200. BIGNUM * x = BN_new(), * y = BN_new();
  201. EC_POINT_get_affine_coordinates_GFp (EC_KEY_get0_group(signingKey),
  202. EC_KEY_get0_public_key (signingKey), x, y, NULL);
  203. bn2buf (x, signingPublicKey, keyLen/2);
  204. bn2buf (y, signingPublicKey + keyLen/2, keyLen/2);
  205. BN_free (x); BN_free (y);
  206. EC_KEY_free (signingKey);
  207. }
  208. // ECDSA_SHA256_P256
  209. const size_t ECDSAP256_KEY_LENGTH = 64;
  210. typedef ECDSAVerifier<SHA256Hash, NID_X9_62_prime256v1, ECDSAP256_KEY_LENGTH> ECDSAP256Verifier;
  211. typedef ECDSASigner<SHA256Hash, NID_X9_62_prime256v1, ECDSAP256_KEY_LENGTH> ECDSAP256Signer;
  212. inline void CreateECDSAP256RandomKeys (uint8_t * signingPrivateKey, uint8_t * signingPublicKey)
  213. {
  214. CreateECDSARandomKeys (NID_X9_62_prime256v1, ECDSAP256_KEY_LENGTH, signingPrivateKey, signingPublicKey);
  215. }
  216. // ECDSA_SHA384_P384
  217. const size_t ECDSAP384_KEY_LENGTH = 96;
  218. typedef ECDSAVerifier<SHA384Hash, NID_secp384r1, ECDSAP384_KEY_LENGTH> ECDSAP384Verifier;
  219. typedef ECDSASigner<SHA384Hash, NID_secp384r1, ECDSAP384_KEY_LENGTH> ECDSAP384Signer;
  220. inline void CreateECDSAP384RandomKeys (uint8_t * signingPrivateKey, uint8_t * signingPublicKey)
  221. {
  222. CreateECDSARandomKeys (NID_secp384r1, ECDSAP384_KEY_LENGTH, signingPrivateKey, signingPublicKey);
  223. }
  224. // ECDSA_SHA512_P521
  225. const size_t ECDSAP521_KEY_LENGTH = 132;
  226. typedef ECDSAVerifier<SHA512Hash, NID_secp521r1, ECDSAP521_KEY_LENGTH> ECDSAP521Verifier;
  227. typedef ECDSASigner<SHA512Hash, NID_secp521r1, ECDSAP521_KEY_LENGTH> ECDSAP521Signer;
  228. inline void CreateECDSAP521RandomKeys (uint8_t * signingPrivateKey, uint8_t * signingPublicKey)
  229. {
  230. CreateECDSARandomKeys (NID_secp521r1, ECDSAP521_KEY_LENGTH, signingPrivateKey, signingPublicKey);
  231. }
  232. // EdDSA
  233. class EDDSA25519Verifier: public Verifier
  234. {
  235. public:
  236. EDDSA25519Verifier ();
  237. void SetPublicKey (const uint8_t * signingKey);
  238. ~EDDSA25519Verifier ();
  239. bool Verify (const uint8_t * buf, size_t len, const uint8_t * signature) const;
  240. size_t GetPublicKeyLen () const { return EDDSA25519_PUBLIC_KEY_LENGTH; };
  241. size_t GetSignatureLen () const { return EDDSA25519_SIGNATURE_LENGTH; };
  242. private:
  243. #if OPENSSL_EDDSA
  244. EVP_PKEY * m_Pkey;
  245. EVP_MD_CTX * m_MDCtx;
  246. #else
  247. EDDSAPoint m_PublicKey;
  248. uint8_t m_PublicKeyEncoded[EDDSA25519_PUBLIC_KEY_LENGTH];
  249. #endif
  250. };
  251. class EDDSA25519SignerCompat: public Signer
  252. {
  253. public:
  254. EDDSA25519SignerCompat (const uint8_t * signingPrivateKey, const uint8_t * signingPublicKey = nullptr);
  255. // we pass signingPublicKey to check if it matches private key
  256. ~EDDSA25519SignerCompat ();
  257. void Sign (const uint8_t * buf, int len, uint8_t * signature) const;
  258. const uint8_t * GetPublicKey () const { return m_PublicKeyEncoded; }; // for keys creation
  259. private:
  260. uint8_t m_ExpandedPrivateKey[64];
  261. uint8_t m_PublicKeyEncoded[EDDSA25519_PUBLIC_KEY_LENGTH];
  262. };
  263. #if OPENSSL_EDDSA
  264. class EDDSA25519Signer: public Signer
  265. {
  266. public:
  267. EDDSA25519Signer (const uint8_t * signingPrivateKey, const uint8_t * signingPublicKey = nullptr);
  268. // we pass signingPublicKey to check if it matches private key
  269. ~EDDSA25519Signer ();
  270. void Sign (const uint8_t * buf, int len, uint8_t * signature) const;
  271. private:
  272. EVP_PKEY * m_Pkey;
  273. EVP_MD_CTX * m_MDCtx;
  274. EDDSA25519SignerCompat * m_Fallback;
  275. };
  276. #else
  277. typedef EDDSA25519SignerCompat EDDSA25519Signer;
  278. #endif
  279. inline void CreateEDDSA25519RandomKeys (uint8_t * signingPrivateKey, uint8_t * signingPublicKey)
  280. {
  281. #if OPENSSL_EDDSA
  282. EVP_PKEY *pkey = NULL;
  283. EVP_PKEY_CTX *pctx = EVP_PKEY_CTX_new_id (EVP_PKEY_ED25519, NULL);
  284. EVP_PKEY_keygen_init (pctx);
  285. EVP_PKEY_keygen (pctx, &pkey);
  286. EVP_PKEY_CTX_free (pctx);
  287. size_t len = EDDSA25519_PUBLIC_KEY_LENGTH;
  288. EVP_PKEY_get_raw_public_key (pkey, signingPublicKey, &len);
  289. len = EDDSA25519_PRIVATE_KEY_LENGTH;
  290. EVP_PKEY_get_raw_private_key (pkey, signingPrivateKey, &len);
  291. EVP_PKEY_free (pkey);
  292. #else
  293. RAND_bytes (signingPrivateKey, EDDSA25519_PRIVATE_KEY_LENGTH);
  294. EDDSA25519Signer signer (signingPrivateKey);
  295. memcpy (signingPublicKey, signer.GetPublicKey (), EDDSA25519_PUBLIC_KEY_LENGTH);
  296. #endif
  297. }
  298. // ГОСТ Р 34.11
  299. struct GOSTR3411_256_Hash
  300. {
  301. static void CalculateHash (const uint8_t * buf, size_t len, uint8_t * digest)
  302. {
  303. GOSTR3411_2012_256 (buf, len, digest);
  304. }
  305. enum { hashLen = 32 };
  306. };
  307. struct GOSTR3411_512_Hash
  308. {
  309. static void CalculateHash (const uint8_t * buf, size_t len, uint8_t * digest)
  310. {
  311. GOSTR3411_2012_512 (buf, len, digest);
  312. }
  313. enum { hashLen = 64 };
  314. };
  315. // ГОСТ Р 34.10
  316. const size_t GOSTR3410_256_PUBLIC_KEY_LENGTH = 64;
  317. const size_t GOSTR3410_512_PUBLIC_KEY_LENGTH = 128;
  318. template<typename Hash>
  319. class GOSTR3410Verifier: public Verifier
  320. {
  321. public:
  322. enum { keyLen = Hash::hashLen };
  323. GOSTR3410Verifier (GOSTR3410ParamSet paramSet):
  324. m_ParamSet (paramSet), m_PublicKey (nullptr)
  325. {
  326. }
  327. void SetPublicKey (const uint8_t * signingKey)
  328. {
  329. BIGNUM * x = BN_bin2bn (signingKey, GetPublicKeyLen ()/2, NULL);
  330. BIGNUM * y = BN_bin2bn (signingKey + GetPublicKeyLen ()/2, GetPublicKeyLen ()/2, NULL);
  331. m_PublicKey = GetGOSTR3410Curve (m_ParamSet)->CreatePoint (x, y);
  332. BN_free (x); BN_free (y);
  333. }
  334. ~GOSTR3410Verifier ()
  335. {
  336. if (m_PublicKey) EC_POINT_free (m_PublicKey);
  337. }
  338. bool Verify (const uint8_t * buf, size_t len, const uint8_t * signature) const
  339. {
  340. uint8_t digest[Hash::hashLen];
  341. Hash::CalculateHash (buf, len, digest);
  342. BIGNUM * d = BN_bin2bn (digest, Hash::hashLen, nullptr);
  343. BIGNUM * r = BN_bin2bn (signature, GetSignatureLen ()/2, NULL);
  344. BIGNUM * s = BN_bin2bn (signature + GetSignatureLen ()/2, GetSignatureLen ()/2, NULL);
  345. bool ret = GetGOSTR3410Curve (m_ParamSet)->Verify (m_PublicKey, d, r, s);
  346. BN_free (d); BN_free (r); BN_free (s);
  347. return ret;
  348. }
  349. size_t GetPublicKeyLen () const { return keyLen*2; }
  350. size_t GetSignatureLen () const { return keyLen*2; }
  351. private:
  352. GOSTR3410ParamSet m_ParamSet;
  353. EC_POINT * m_PublicKey;
  354. };
  355. template<typename Hash>
  356. class GOSTR3410Signer: public Signer
  357. {
  358. public:
  359. enum { keyLen = Hash::hashLen };
  360. GOSTR3410Signer (GOSTR3410ParamSet paramSet, const uint8_t * signingPrivateKey):
  361. m_ParamSet (paramSet)
  362. {
  363. m_PrivateKey = BN_bin2bn (signingPrivateKey, keyLen, nullptr);
  364. }
  365. ~GOSTR3410Signer () { BN_free (m_PrivateKey); }
  366. void Sign (const uint8_t * buf, int len, uint8_t * signature) const
  367. {
  368. uint8_t digest[Hash::hashLen];
  369. Hash::CalculateHash (buf, len, digest);
  370. BIGNUM * d = BN_bin2bn (digest, Hash::hashLen, nullptr);
  371. BIGNUM * r = BN_new (), * s = BN_new ();
  372. GetGOSTR3410Curve (m_ParamSet)->Sign (m_PrivateKey, d, r, s);
  373. bn2buf (r, signature, keyLen);
  374. bn2buf (s, signature + keyLen, keyLen);
  375. BN_free (d); BN_free (r); BN_free (s);
  376. }
  377. private:
  378. GOSTR3410ParamSet m_ParamSet;
  379. BIGNUM * m_PrivateKey;
  380. };
  381. inline void CreateGOSTR3410RandomKeys (GOSTR3410ParamSet paramSet, uint8_t * signingPrivateKey, uint8_t * signingPublicKey)
  382. {
  383. const auto& curve = GetGOSTR3410Curve (paramSet);
  384. auto keyLen = curve->GetKeyLen ();
  385. RAND_bytes (signingPrivateKey, keyLen);
  386. BIGNUM * priv = BN_bin2bn (signingPrivateKey, keyLen, nullptr);
  387. auto pub = curve->MulP (priv);
  388. BN_free (priv);
  389. BIGNUM * x = BN_new (), * y = BN_new ();
  390. curve->GetXY (pub, x, y);
  391. EC_POINT_free (pub);
  392. bn2buf (x, signingPublicKey, keyLen);
  393. bn2buf (y, signingPublicKey + keyLen, keyLen);
  394. BN_free (x); BN_free (y);
  395. }
  396. typedef GOSTR3410Verifier<GOSTR3411_256_Hash> GOSTR3410_256_Verifier;
  397. typedef GOSTR3410Signer<GOSTR3411_256_Hash> GOSTR3410_256_Signer;
  398. typedef GOSTR3410Verifier<GOSTR3411_512_Hash> GOSTR3410_512_Verifier;
  399. typedef GOSTR3410Signer<GOSTR3411_512_Hash> GOSTR3410_512_Signer;
  400. // RedDSA
  401. typedef EDDSA25519Verifier RedDSA25519Verifier;
  402. class RedDSA25519Signer: public Signer
  403. {
  404. public:
  405. RedDSA25519Signer (const uint8_t * signingPrivateKey)
  406. {
  407. memcpy (m_PrivateKey, signingPrivateKey, EDDSA25519_PRIVATE_KEY_LENGTH);
  408. BN_CTX * ctx = BN_CTX_new ();
  409. auto publicKey = GetEd25519 ()->GeneratePublicKey (m_PrivateKey, ctx);
  410. GetEd25519 ()->EncodePublicKey (publicKey, m_PublicKeyEncoded, ctx);
  411. BN_CTX_free (ctx);
  412. }
  413. ~RedDSA25519Signer () {};
  414. void Sign (const uint8_t * buf, int len, uint8_t * signature) const
  415. {
  416. GetEd25519 ()->SignRedDSA (m_PrivateKey, m_PublicKeyEncoded, buf, len, signature);
  417. }
  418. const uint8_t * GetPublicKey () const { return m_PublicKeyEncoded; }; // for keys creation
  419. private:
  420. uint8_t m_PrivateKey[EDDSA25519_PRIVATE_KEY_LENGTH];
  421. uint8_t m_PublicKeyEncoded[EDDSA25519_PUBLIC_KEY_LENGTH];
  422. };
  423. inline void CreateRedDSA25519RandomKeys (uint8_t * signingPrivateKey, uint8_t * signingPublicKey)
  424. {
  425. GetEd25519 ()->CreateRedDSAPrivateKey (signingPrivateKey);
  426. RedDSA25519Signer signer (signingPrivateKey);
  427. memcpy (signingPublicKey, signer.GetPublicKey (), EDDSA25519_PUBLIC_KEY_LENGTH);
  428. }
  429. }
  430. }
  431. #endif