3232#include < openssl/dh.h>
3333#include < openssl/digest.h>
3434#include < openssl/ec.h>
35+ #include < openssl/evp.h>
3536#include < openssl/ec_key.h>
3637#include < openssl/ecdh.h>
3738#include < openssl/ecdsa.h>
@@ -1567,16 +1568,27 @@ static bool ECDSASigGen(const Span<const uint8_t> args[], ReplyCallback write_re
15671568 bssl::UniquePtr<EC_KEY> key = ECKeyFromName (args[0 ]);
15681569 bssl::UniquePtr<BIGNUM> d = BytesToBIGNUM (args[1 ]);
15691570 const EVP_MD *hash = HashFromName (args[2 ]);
1570- uint8_t digest[EVP_MAX_MD_SIZE];
1571- unsigned digest_len;
1572- if (!key || !hash ||
1573- !EVP_Digest (args[3 ].data (), args[3 ].size (), digest, &digest_len, hash,
1574- /* impl=*/ nullptr ) ||
1575- !EC_KEY_set_private_key (key.get (), d.get ())) {
1571+ auto msg = args[3 ];
1572+ if (!key || !hash || !EC_KEY_set_private_key (key.get (), d.get ())) {
15761573 return false ;
15771574 }
1578-
1579- bssl::UniquePtr<ECDSA_SIG> sig (ECDSA_do_sign (digest, digest_len, key.get ()));
1575+ bssl::ScopedEVP_MD_CTX ctx;
1576+ EVP_PKEY_CTX *pctx;
1577+ bssl::UniquePtr<EVP_PKEY> evp_pkey (EVP_PKEY_new ());
1578+ if (!evp_pkey || !EVP_PKEY_set1_EC_KEY (evp_pkey.get (), key.get ())) {
1579+ return false ;
1580+ }
1581+ std::vector<uint8_t > sig_der;
1582+ size_t len;
1583+ if (!EVP_DigestSignInit (ctx.get (), &pctx, hash, nullptr , evp_pkey.get ()) ||
1584+ !EVP_DigestSign (ctx.get (), nullptr , &len, msg.data (), msg.size ())) {
1585+ return false ;
1586+ }
1587+ sig_der.resize (len);
1588+ if (!EVP_DigestSign (ctx.get (), sig_der.data (), &len, msg.data (), msg.size ())) {
1589+ return false ;
1590+ }
1591+ bssl::UniquePtr<ECDSA_SIG> sig (ECDSA_SIG_from_bytes (sig_der.data (), len));
15801592 if (!sig) {
15811593 return false ;
15821594 }
@@ -1599,27 +1611,35 @@ static bool ECDSASigVer(const Span<const uint8_t> args[], ReplyCallback write_re
15991611 ECDSA_SIG sig;
16001612 sig.r = r.get ();
16011613 sig.s = s.get ();
1602-
1603- uint8_t digest[EVP_MAX_MD_SIZE];
1604- unsigned digest_len;
1605- if (!key || !hash ||
1606- !EVP_Digest (msg.data (), msg.size (), digest, &digest_len, hash,
1607- /* impl=*/ nullptr )) {
1614+ uint8_t *der;
1615+ size_t der_len;
1616+ if (!key || !hash || !ECDSA_SIG_to_bytes (&der, &der_len, &sig)) {
16081617 return false ;
16091618 }
1610-
1619+ // Let |delete_der| manage the release of |der|.
1620+ bssl::UniquePtr<uint8_t > delete_der (der);
16111621 bssl::UniquePtr<EC_POINT> point (EC_POINT_new (EC_KEY_get0_group (key.get ())));
1612- uint8_t reply[1 ];
16131622 if (!EC_POINT_set_affine_coordinates_GFp (EC_KEY_get0_group (key.get ()),
16141623 point.get (), x.get (), y.get (),
16151624 /* ctx=*/ nullptr ) ||
16161625 !EC_KEY_set_public_key (key.get (), point.get ()) ||
1617- !EC_KEY_check_fips (key.get ()) ||
1618- !ECDSA_do_verify (digest, digest_len, &sig, key.get ())) {
1626+ !EC_KEY_check_fips (key.get ())) {
1627+ return false ;
1628+ }
1629+ bssl::ScopedEVP_MD_CTX ctx;
1630+ EVP_PKEY_CTX *pctx;
1631+ bssl::UniquePtr<EVP_PKEY> evp_pkey (EVP_PKEY_new ());
1632+ if (!evp_pkey || !EVP_PKEY_set1_EC_KEY (evp_pkey.get (), key.get ())) {
1633+ return false ;
1634+ }
1635+ uint8_t reply[1 ];
1636+ if (!EVP_DigestVerifyInit (ctx.get (), &pctx, hash, nullptr , evp_pkey.get ()) ||
1637+ !EVP_DigestVerify (ctx.get (), der, der_len, msg.data (), msg.size ())) {
16191638 reply[0 ] = 0 ;
16201639 } else {
16211640 reply[0 ] = 1 ;
16221641 }
1642+ ERR_clear_error ();
16231643
16241644 return write_reply ({Span<const uint8_t >(reply)});
16251645}
@@ -1657,26 +1677,34 @@ static bool CMAC_AESVerify(const Span<const uint8_t> args[], ReplyCallback write
16571677 return write_reply ({Span<const uint8_t >(&ok, sizeof (ok))});
16581678}
16591679
1660- static std::map<unsigned , bssl::UniquePtr<RSA >>& CachedRSAKeys () {
1661- static std::map<unsigned , bssl::UniquePtr<RSA >> keys;
1680+ static std::map<unsigned , bssl::UniquePtr<EVP_PKEY >>& CachedRSAEVPKeys () {
1681+ static std::map<unsigned , bssl::UniquePtr<EVP_PKEY >> keys;
16621682 return keys;
16631683}
16641684
1665- static RSA* GetRSAKey (unsigned bits) {
1666- auto it = CachedRSAKeys ().find (bits);
1667- if (it != CachedRSAKeys ().end ()) {
1685+ static EVP_PKEY* AddRSAKeyToCache (bssl::UniquePtr<RSA>& rsa, unsigned bits) {
1686+ bssl::UniquePtr<EVP_PKEY> evp_pkey (EVP_PKEY_new ());
1687+ if (!evp_pkey || !EVP_PKEY_set1_RSA (evp_pkey.get (), rsa.get ())) {
1688+ return nullptr ;
1689+ }
1690+
1691+ EVP_PKEY *const ret = evp_pkey.get ();
1692+ CachedRSAEVPKeys ().emplace (static_cast <unsigned >(bits), std::move (evp_pkey));
1693+ return ret;
1694+ }
1695+
1696+ static EVP_PKEY* GetRSAKey (unsigned bits) {
1697+ auto it = CachedRSAEVPKeys ().find (bits);
1698+ if (it != CachedRSAEVPKeys ().end ()) {
16681699 return it->second .get ();
16691700 }
16701701
1671- bssl::UniquePtr<RSA> key (RSA_new ());
1672- if (!RSA_generate_key_fips (key .get (), bits, nullptr )) {
1702+ bssl::UniquePtr<RSA> rsa (RSA_new ());
1703+ if (!RSA_generate_key_fips (rsa .get (), bits, nullptr )) {
16731704 abort ();
16741705 }
16751706
1676- RSA *const ret = key.get ();
1677- CachedRSAKeys ().emplace (static_cast <unsigned >(bits), std::move (key));
1678-
1679- return ret;
1707+ return AddRSAKeyToCache (rsa, bits);
16801708}
16811709
16821710static bool RSAKeyGen (const Span<const uint8_t > args[], ReplyCallback write_reply) {
@@ -1686,22 +1714,24 @@ static bool RSAKeyGen(const Span<const uint8_t> args[], ReplyCallback write_repl
16861714 }
16871715 memcpy (&bits, args[0 ].data (), sizeof (bits));
16881716
1689- bssl::UniquePtr<RSA> key (RSA_new ());
1690- if (!RSA_generate_key_fips (key .get (), bits, nullptr )) {
1717+ bssl::UniquePtr<RSA> rsa (RSA_new ());
1718+ if (!RSA_generate_key_fips (rsa .get (), bits, nullptr )) {
16911719 LOG_ERROR (" RSA_generate_key_fips failed for modulus length %u.\n " , bits);
16921720 return false ;
16931721 }
16941722
16951723 const BIGNUM *n, *e, *d, *p, *q;
1696- RSA_get0_key (key .get (), &n, &e, &d);
1697- RSA_get0_factors (key .get (), &p, &q);
1724+ RSA_get0_key (rsa .get (), &n, &e, &d);
1725+ RSA_get0_factors (rsa .get (), &p, &q);
16981726
16991727 if (!write_reply ({BIGNUMBytes (e), BIGNUMBytes (p), BIGNUMBytes (q),
17001728 BIGNUMBytes (n), BIGNUMBytes (d)})) {
17011729 return false ;
17021730 }
17031731
1704- CachedRSAKeys ().emplace (static_cast <unsigned >(bits), std::move (key));
1732+ if (AddRSAKeyToCache (rsa, bits) == nullptr ) {
1733+ return false ;
1734+ }
17051735 return true ;
17061736}
17071737
@@ -1713,35 +1743,32 @@ static bool RSASigGen(const Span<const uint8_t> args[], ReplyCallback write_repl
17131743 }
17141744 memcpy (&bits, args[0 ].data (), sizeof (bits));
17151745 const Span<const uint8_t > msg = args[1 ];
1716-
1717- RSA *const key = GetRSAKey (bits);
1718- const EVP_MD *const md = MDFunc ();
1719- uint8_t digest_buf[EVP_MAX_MD_SIZE];
1720- unsigned digest_len;
1721- if (!EVP_Digest (msg.data (), msg.size (), digest_buf, &digest_len, md, NULL )) {
1746+ EVP_PKEY *const evp_pkey = GetRSAKey (bits);
1747+ if (evp_pkey == nullptr ) {
17221748 return false ;
17231749 }
1724-
1725- std::vector<uint8_t > sig (RSA_size (key));
1750+ RSA *const rsa = EVP_PKEY_get0_RSA (evp_pkey);
1751+ if (rsa == nullptr ) {
1752+ return false ;
1753+ }
1754+ const EVP_MD *const md = MDFunc ();
1755+ std::vector<uint8_t > sig;
17261756 size_t sig_len;
1727- if (UsePSS) {
1728- if (!RSA_sign_pss_mgf1 (key, &sig_len, sig.data (), sig.size (), digest_buf,
1729- digest_len, md, md, -1 )) {
1730- return false ;
1731- }
1732- } else {
1733- unsigned sig_len_u;
1734- if (!RSA_sign (EVP_MD_type (md), digest_buf, digest_len, sig.data (),
1735- &sig_len_u, key)) {
1736- return false ;
1737- }
1738- sig_len = sig_len_u;
1757+ bssl::ScopedEVP_MD_CTX ctx;
1758+ EVP_PKEY_CTX *pctx;
1759+ int padding = UsePSS ? RSA_PKCS1_PSS_PADDING : RSA_PKCS1_PADDING;
1760+ if (!EVP_DigestSignInit (ctx.get (), &pctx, md, nullptr , evp_pkey) ||
1761+ !EVP_PKEY_CTX_set_rsa_padding (pctx, padding) ||
1762+ !EVP_DigestSign (ctx.get (), nullptr , &sig_len, msg.data (), msg.size ())) {
1763+ return false ;
17391764 }
1740-
17411765 sig.resize (sig_len);
1766+ if (!EVP_DigestSign (ctx.get (), sig.data (), &sig_len, msg.data (), msg.size ())) {
1767+ return false ;
1768+ }
17421769
17431770 return write_reply (
1744- {BIGNUMBytes (RSA_get0_n (key )), BIGNUMBytes (RSA_get0_e (key )), sig});
1771+ {BIGNUMBytes (RSA_get0_n (rsa )), BIGNUMBytes (RSA_get0_e (rsa )), sig});
17451772}
17461773
17471774template <const EVP_MD *(MDFunc)(), bool UsePSS>
@@ -1761,19 +1788,20 @@ static bool RSASigVer(const Span<const uint8_t> args[], ReplyCallback write_repl
17611788 }
17621789
17631790 const EVP_MD *const md = MDFunc ();
1764- uint8_t digest_buf[EVP_MAX_MD_SIZE];
1765- unsigned digest_len;
1766- if (!EVP_Digest (msg.data (), msg.size (), digest_buf, &digest_len, md, NULL )) {
1791+ bssl::ScopedEVP_MD_CTX ctx;
1792+ EVP_PKEY_CTX *pctx;
1793+ bssl::UniquePtr<EVP_PKEY> evp_pkey (EVP_PKEY_new ());
1794+ if (!evp_pkey || !EVP_PKEY_set1_RSA (evp_pkey.get (), key.get ())) {
17671795 return false ;
17681796 }
1769-
17701797 uint8_t ok;
1771- if (UsePSS) {
1772- ok = RSA_verify_pss_mgf1 (key.get (), digest_buf, digest_len, md, md, -1 ,
1773- sig.data (), sig.size ());
1798+ int padding = UsePSS ? RSA_PKCS1_PSS_PADDING : RSA_PKCS1_PADDING;
1799+ if (!EVP_DigestVerifyInit (ctx.get (), &pctx, md, nullptr , evp_pkey.get ()) ||
1800+ !EVP_PKEY_CTX_set_rsa_padding (pctx, padding) ||
1801+ !EVP_DigestVerify (ctx.get (), sig.data (), sig.size (), msg.data (), msg.size ())) {
1802+ ok = 0 ;
17741803 } else {
1775- ok = RSA_verify (EVP_MD_type (md), digest_buf, digest_len, sig.data (),
1776- sig.size (), key.get ());
1804+ ok = 1 ;
17771805 }
17781806 ERR_clear_error ();
17791807
0 commit comments