Skip to content

Commit 47c21db

Browse files
authored
feat: sync source code with nodejs/node (#17)
1 parent d714e74 commit 47c21db

File tree

3 files changed

+322
-503
lines changed

3 files changed

+322
-503
lines changed

include/ncrypto.h

Lines changed: 96 additions & 152 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include <openssl/err.h>
99
#include <openssl/evp.h>
1010
#include <openssl/hmac.h>
11+
#include <openssl/kdf.h>
1112
#include <openssl/rsa.h>
1213
#include <openssl/ssl.h>
1314
#include <openssl/x509.h>
@@ -16,17 +17,13 @@
1617
#include <openssl/aead.h>
1718
#endif
1819

19-
#include <stdint.h>
2020
#include <cstddef>
21-
#include <cstdio>
2221
#include <functional>
2322
#include <list>
2423
#include <memory>
2524
#include <optional>
2625
#include <string>
2726
#include <string_view>
28-
#include <unordered_map>
29-
#include <utility>
3027

3128
#if NCRYPTO_DEVELOPMENT_CHECKS
3229
#include <iostream>
@@ -92,7 +89,10 @@ inline bool EqualNoCase(const std::string_view a, const std::string_view b) {
9289
#define NCRYPTO_STR(x) #x
9390
#define NCRYPTO_REQUIRE(EXPR) \
9491
{ \
95-
if (!(EXPR) { abort(); }) }
92+
if (!(EXPR)) { \
93+
abort(); \
94+
} \
95+
}
9696

9797
#define NCRYPTO_FAIL(MESSAGE) \
9898
do { \
@@ -269,8 +269,6 @@ class ECKeyPointer;
269269
class Dsa;
270270
class Rsa;
271271
class Ec;
272-
class Aead;
273-
class AeadCtxPointer;
274272

275273
struct StackOfXASN1Deleter {
276274
void operator()(STACK_OF(ASN1_OBJECT) * p) const {
@@ -325,25 +323,7 @@ DataPointer xofHashDigest(const Buffer<const unsigned char>& data,
325323
const EVP_MD* md,
326324
size_t length);
327325

328-
template <typename T>
329-
class ModeMixin {
330-
public:
331-
std::string_view getModeLabel() const;
332-
333-
bool isGcmMode() const { return self().getMode() == EVP_CIPH_GCM_MODE; }
334-
bool isWrapMode() const { return self().getMode() == EVP_CIPH_WRAP_MODE; }
335-
bool isCtrMode() const { return self().getMode() == EVP_CIPH_CTR_MODE; }
336-
bool isCcmMode() const { return self().getMode() == EVP_CIPH_CCM_MODE; }
337-
bool isOcbMode() const { return self().getMode() == EVP_CIPH_OCB_MODE; }
338-
bool isStreamMode() const {
339-
return self().getMode() == EVP_CIPH_STREAM_CIPHER;
340-
}
341-
342-
private:
343-
const T& self() const { return static_cast<const T&>(*this); }
344-
};
345-
346-
class Cipher final : public ModeMixin<Cipher> {
326+
class Cipher final {
347327
public:
348328
static constexpr size_t MAX_KEY_LENGTH = EVP_MAX_KEY_LENGTH;
349329
static constexpr size_t MAX_IV_LENGTH = EVP_MAX_IV_LENGTH;
@@ -352,10 +332,12 @@ class Cipher final : public ModeMixin<Cipher> {
352332
#else
353333
static constexpr size_t MAX_AUTH_TAG_LENGTH = 16;
354334
#endif
355-
// FIXME: These constants are not available in all OpenSSL/BoringSSL versions
356-
// static_assert(EVP_GCM_TLS_TAG_LEN <= MAX_AUTH_TAG_LENGTH &&
357-
// EVP_CCM_TLS_TAG_LEN <= MAX_AUTH_TAG_LENGTH &&
358-
// EVP_CHACHAPOLY_TLS_TAG_LEN <= MAX_AUTH_TAG_LENGTH);
335+
static_assert(EVP_GCM_TLS_TAG_LEN <= MAX_AUTH_TAG_LENGTH
336+
#ifndef OPENSSL_IS_BORINGSSL
337+
&& EVP_CCM_TLS_TAG_LEN <= MAX_AUTH_TAG_LENGTH &&
338+
EVP_CHACHAPOLY_TLS_TAG_LEN <= MAX_AUTH_TAG_LENGTH
339+
#endif
340+
);
359341

360342
Cipher() = default;
361343
Cipher(const EVP_CIPHER* cipher) : cipher_(cipher) {}
@@ -376,9 +358,15 @@ class Cipher final : public ModeMixin<Cipher> {
376358
int getIvLength() const;
377359
int getKeyLength() const;
378360
int getBlockSize() const;
379-
361+
std::string_view getModeLabel() const;
380362
const char* getName() const;
381363

364+
bool isGcmMode() const;
365+
bool isWrapMode() const;
366+
bool isCtrMode() const;
367+
bool isCcmMode() const;
368+
bool isOcbMode() const;
369+
bool isStreamMode() const;
382370
bool isChaCha20Poly1305() const;
383371

384372
bool isSupportedAuthenticatedMode() const;
@@ -471,78 +459,8 @@ class Dsa final {
471459
OSSL3_CONST DSA* dsa_;
472460
};
473461

474-
class BignumPointer final {
475-
public:
476-
BignumPointer() = default;
477-
explicit BignumPointer(BIGNUM* bignum);
478-
explicit BignumPointer(const unsigned char* data, size_t len);
479-
BignumPointer(BignumPointer&& other) noexcept;
480-
BignumPointer& operator=(BignumPointer&& other) noexcept;
481-
NCRYPTO_DISALLOW_COPY(BignumPointer)
482-
~BignumPointer();
483-
484-
int operator<=>(const BignumPointer& other) const noexcept;
485-
int operator<=>(const BIGNUM* other) const noexcept;
486-
inline operator bool() const { return bn_ != nullptr; }
487-
inline BIGNUM* get() const noexcept { return bn_.get(); }
488-
void reset(BIGNUM* bn = nullptr);
489-
void reset(const unsigned char* data, size_t len);
490-
BIGNUM* release();
491-
492-
bool isZero() const;
493-
bool isOne() const;
494-
495-
bool setWord(unsigned long w); // NOLINT(runtime/int)
496-
unsigned long getWord() const; // NOLINT(runtime/int)
497-
498-
size_t byteLength() const;
499-
size_t bitLength() const;
500-
501-
DataPointer toHex() const;
502-
DataPointer encode() const;
503-
DataPointer encodePadded(size_t size) const;
504-
size_t encodeInto(unsigned char* out) const;
505-
size_t encodePaddedInto(unsigned char* out, size_t size) const;
506-
507-
using PrimeCheckCallback = std::function<bool(int, int)>;
508-
int isPrime(int checks,
509-
PrimeCheckCallback cb = defaultPrimeCheckCallback) const;
510-
struct PrimeConfig {
511-
int bits;
512-
bool safe = false;
513-
const BignumPointer& add;
514-
const BignumPointer& rem;
515-
};
516-
517-
static BignumPointer NewPrime(
518-
const PrimeConfig& params,
519-
PrimeCheckCallback cb = defaultPrimeCheckCallback);
520-
521-
bool generate(const PrimeConfig& params,
522-
PrimeCheckCallback cb = defaultPrimeCheckCallback) const;
523-
524-
static BignumPointer New();
525-
static BignumPointer NewSecure();
526-
static BignumPointer NewSub(const BignumPointer& a, const BignumPointer& b);
527-
static BignumPointer NewLShift(size_t length);
528-
529-
static DataPointer Encode(const BIGNUM* bn);
530-
static DataPointer EncodePadded(const BIGNUM* bn, size_t size);
531-
static size_t EncodePaddedInto(const BIGNUM* bn,
532-
unsigned char* out,
533-
size_t size);
534-
static int GetBitCount(const BIGNUM* bn);
535-
static int GetByteCount(const BIGNUM* bn);
536-
static unsigned long GetWord(const BIGNUM* bn); // NOLINT(runtime/int)
537-
static const BIGNUM* One();
538-
539-
BignumPointer clone();
540-
541-
private:
542-
DeleteFnPtr<BIGNUM, BN_clear_free> bn_;
543-
544-
static bool defaultPrimeCheckCallback(int, int) { return 1; }
545-
};
462+
// ============================================================================
463+
// RSA
546464

547465
class Rsa final {
548466
public:
@@ -604,10 +522,6 @@ class Ec final {
604522

605523
const EC_GROUP* getGroup() const;
606524
int getCurve() const;
607-
uint32_t getDegree() const;
608-
std::string getCurveName() const;
609-
const EC_POINT* getPublicKey() const;
610-
const BIGNUM* getPrivateKey() const;
611525

612526
inline operator bool() const { return ec_ != nullptr; }
613527
inline operator OSSL3_CONST EC_KEY*() const { return ec_; }
@@ -617,16 +531,8 @@ class Ec final {
617531
using GetCurveCallback = std::function<bool(const char*)>;
618532
static bool GetCurves(GetCurveCallback callback);
619533

620-
inline const BignumPointer& getX() const { return x_; }
621-
inline const BignumPointer& getY() const { return y_; }
622-
inline const BignumPointer& getD() const { return d_; }
623-
624534
private:
625535
OSSL3_CONST EC_KEY* ec_ = nullptr;
626-
// Affine coordinates for the EC_KEY.
627-
BignumPointer x_;
628-
BignumPointer y_;
629-
BignumPointer d_;
630536
};
631537

632538
// A managed pointer to a buffer of data. When destroyed the underlying
@@ -757,6 +663,78 @@ class BIOPointer final {
757663
mutable DeleteFnPtr<BIO, BIO_free_all> bio_;
758664
};
759665

666+
class BignumPointer final {
667+
public:
668+
BignumPointer() = default;
669+
explicit BignumPointer(BIGNUM* bignum);
670+
explicit BignumPointer(const unsigned char* data, size_t len);
671+
BignumPointer(BignumPointer&& other) noexcept;
672+
BignumPointer& operator=(BignumPointer&& other) noexcept;
673+
NCRYPTO_DISALLOW_COPY(BignumPointer)
674+
~BignumPointer();
675+
676+
int operator<=>(const BignumPointer& other) const noexcept;
677+
int operator<=>(const BIGNUM* other) const noexcept;
678+
inline operator bool() const { return bn_ != nullptr; }
679+
inline BIGNUM* get() const noexcept { return bn_.get(); }
680+
void reset(BIGNUM* bn = nullptr);
681+
void reset(const unsigned char* data, size_t len);
682+
BIGNUM* release();
683+
684+
bool isZero() const;
685+
bool isOne() const;
686+
687+
bool setWord(unsigned long w); // NOLINT(runtime/int)
688+
unsigned long getWord() const; // NOLINT(runtime/int)
689+
690+
size_t byteLength() const;
691+
692+
DataPointer toHex() const;
693+
DataPointer encode() const;
694+
DataPointer encodePadded(size_t size) const;
695+
size_t encodeInto(unsigned char* out) const;
696+
size_t encodePaddedInto(unsigned char* out, size_t size) const;
697+
698+
using PrimeCheckCallback = std::function<bool(int, int)>;
699+
int isPrime(int checks,
700+
PrimeCheckCallback cb = defaultPrimeCheckCallback) const;
701+
struct PrimeConfig {
702+
int bits;
703+
bool safe = false;
704+
const BignumPointer& add;
705+
const BignumPointer& rem;
706+
};
707+
708+
static BignumPointer NewPrime(
709+
const PrimeConfig& params,
710+
PrimeCheckCallback cb = defaultPrimeCheckCallback);
711+
712+
bool generate(const PrimeConfig& params,
713+
PrimeCheckCallback cb = defaultPrimeCheckCallback) const;
714+
715+
static BignumPointer New();
716+
static BignumPointer NewSecure();
717+
static BignumPointer NewSub(const BignumPointer& a, const BignumPointer& b);
718+
static BignumPointer NewLShift(size_t length);
719+
720+
static DataPointer Encode(const BIGNUM* bn);
721+
static DataPointer EncodePadded(const BIGNUM* bn, size_t size);
722+
static size_t EncodePaddedInto(const BIGNUM* bn,
723+
unsigned char* out,
724+
size_t size);
725+
static int GetBitCount(const BIGNUM* bn);
726+
static int GetByteCount(const BIGNUM* bn);
727+
static unsigned long GetWord(const BIGNUM* bn); // NOLINT(runtime/int)
728+
static const BIGNUM* One();
729+
730+
BignumPointer clone();
731+
732+
private:
733+
DeleteFnPtr<BIGNUM, BN_clear_free> bn_;
734+
735+
static bool defaultPrimeCheckCallback(int, int) { return 1; }
736+
};
737+
760738
class CipherCtxPointer final {
761739
public:
762740
static CipherCtxPointer New();
@@ -998,15 +976,12 @@ class EVPKeyPointer final {
998976
int getDefaultSignPadding() const;
999977
operator Rsa() const;
1000978
operator Dsa() const;
1001-
operator Ec() const;
1002979

1003980
bool isRsaVariant() const;
1004981
bool isOneShotVariant() const;
1005982
bool isSigVariant() const;
1006983
bool validateDsaParameters() const;
1007984

1008-
EVPKeyPointer clone() const;
1009-
1010985
private:
1011986
DeleteFnPtr<EVP_PKEY, EVP_PKEY_free> pkey_;
1012987
};
@@ -1635,19 +1610,11 @@ bool SafeX509InfoAccessPrint(const BIOPointer& out, X509_EXTENSION* ext);
16351610
// ============================================================================
16361611
// SPKAC
16371612

1638-
[[deprecated("Use the version that takes a Buffer")]] bool VerifySpkac(
1639-
const char* input, size_t length);
1640-
1641-
[[deprecated("Use the version that takes a Buffer")]] BIOPointer
1642-
ExportPublicKey(const char* input, size_t length);
1613+
bool VerifySpkac(const char* input, size_t length);
1614+
BIOPointer ExportPublicKey(const char* input, size_t length);
16431615

16441616
// The caller takes ownership of the returned Buffer<char>
1645-
[[deprecated("Use the version that takes a Buffer")]] Buffer<char>
1646-
ExportChallenge(const char* input, size_t length);
1647-
1648-
bool VerifySpkac(const Buffer<const char>& buf);
1649-
BIOPointer ExportPublicKey(const Buffer<const char>& buf);
1650-
DataPointer ExportChallenge(const Buffer<const char>& buf);
1617+
Buffer<char> ExportChallenge(const char* input, size_t length);
16511618

16521619
// ============================================================================
16531620
// KDF
@@ -1664,13 +1631,6 @@ bool extractP1363(const Buffer<const unsigned char>& buf,
16641631
unsigned char* dest,
16651632
size_t n);
16661633

1667-
bool hkdfInfo(const Digest& md,
1668-
const Buffer<const unsigned char>& key,
1669-
const Buffer<const unsigned char>& info,
1670-
const Buffer<const unsigned char>& salt,
1671-
size_t length,
1672-
Buffer<unsigned char>* out);
1673-
16741634
DataPointer hkdf(const Digest& md,
16751635
const Buffer<const unsigned char>& key,
16761636
const Buffer<const unsigned char>& info,
@@ -1679,15 +1639,6 @@ DataPointer hkdf(const Digest& md,
16791639

16801640
bool checkScryptParams(uint64_t N, uint64_t r, uint64_t p, uint64_t maxmem);
16811641

1682-
bool scryptInto(const Buffer<const char>& pass,
1683-
const Buffer<const unsigned char>& salt,
1684-
uint64_t N,
1685-
uint64_t r,
1686-
uint64_t p,
1687-
uint64_t maxmem,
1688-
size_t length,
1689-
Buffer<unsigned char>* out);
1690-
16911642
DataPointer scrypt(const Buffer<const char>& pass,
16921643
const Buffer<const unsigned char>& salt,
16931644
uint64_t N,
@@ -1696,13 +1647,6 @@ DataPointer scrypt(const Buffer<const char>& pass,
16961647
uint64_t maxmem,
16971648
size_t length);
16981649

1699-
bool pbkdf2Into(const Digest& md,
1700-
const Buffer<const char>& pass,
1701-
const Buffer<const unsigned char>& salt,
1702-
uint32_t iterations,
1703-
size_t length,
1704-
Buffer<unsigned char>* out);
1705-
17061650
DataPointer pbkdf2(const Digest& md,
17071651
const Buffer<const char>& pass,
17081652
const Buffer<const unsigned char>& salt,

0 commit comments

Comments
 (0)