react-native-quick-crypto 1.0.9 → 1.0.10
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/QuickCrypto.podspec +9 -2
- package/README.md +13 -9
- package/android/CMakeLists.txt +4 -0
- package/cpp/cipher/HybridCipherFactory.hpp +15 -1
- package/cpp/cipher/OCBCipher.cpp +4 -4
- package/cpp/cipher/XChaCha20Poly1305Cipher.cpp +161 -0
- package/cpp/cipher/XChaCha20Poly1305Cipher.hpp +43 -0
- package/cpp/cipher/XSalsa20Poly1305Cipher.cpp +145 -0
- package/cpp/cipher/XSalsa20Poly1305Cipher.hpp +42 -0
- package/cpp/dh/HybridDiffieHellman.cpp +10 -0
- package/cpp/dh/HybridDiffieHellman.hpp +1 -0
- package/cpp/ec/HybridEcKeyPair.cpp +21 -0
- package/cpp/ec/HybridEcKeyPair.hpp +1 -0
- package/cpp/hash/HybridHash.cpp +1 -1
- package/cpp/hash/HybridHash.hpp +1 -1
- package/cpp/hmac/HybridHmac.cpp +1 -1
- package/cpp/hmac/HybridHmac.hpp +1 -1
- package/cpp/keys/HybridKeyObjectHandle.cpp +112 -1
- package/cpp/keys/HybridKeyObjectHandle.hpp +5 -1
- package/deps/ncrypto/.bazelrc +0 -1
- package/deps/ncrypto/.bazelversion +1 -1
- package/deps/ncrypto/.github/workflows/commitlint.yml +16 -0
- package/deps/ncrypto/.github/workflows/linter.yml +2 -2
- package/deps/ncrypto/.github/workflows/release-please.yml +16 -0
- package/deps/ncrypto/.github/workflows/ubuntu.yml +82 -0
- package/deps/ncrypto/.release-please-manifest.json +3 -0
- package/deps/ncrypto/BUILD.bazel +9 -1
- package/deps/ncrypto/CHANGELOG.md +37 -0
- package/deps/ncrypto/CMakeLists.txt +35 -11
- package/deps/ncrypto/MODULE.bazel +16 -1
- package/deps/ncrypto/MODULE.bazel.lock +299 -118
- package/deps/ncrypto/cmake/ncrypto-flags.cmake +1 -0
- package/deps/ncrypto/include/ncrypto/aead.h +137 -0
- package/deps/ncrypto/include/ncrypto/version.h +14 -0
- package/deps/ncrypto/include/ncrypto.h +85 -230
- package/deps/ncrypto/ncrypto.pc.in +10 -0
- package/deps/ncrypto/release-please-config.json +11 -0
- package/deps/ncrypto/src/CMakeLists.txt +31 -6
- package/deps/ncrypto/src/aead.cpp +302 -0
- package/deps/ncrypto/src/ncrypto.cpp +274 -556
- package/deps/ncrypto/tests/BUILD.bazel +2 -0
- package/deps/ncrypto/tests/basic.cpp +772 -2
- package/deps/ncrypto/tools/run-clang-format.sh +5 -5
- package/lib/commonjs/diffie-hellman.js +4 -1
- package/lib/commonjs/diffie-hellman.js.map +1 -1
- package/lib/commonjs/ec.js +20 -25
- package/lib/commonjs/ec.js.map +1 -1
- package/lib/commonjs/ed.js +1 -2
- package/lib/commonjs/ed.js.map +1 -1
- package/lib/commonjs/hash.js +7 -0
- package/lib/commonjs/hash.js.map +1 -1
- package/lib/commonjs/index.js +11 -1
- package/lib/commonjs/index.js.map +1 -1
- package/lib/commonjs/keys/classes.js +9 -5
- package/lib/commonjs/keys/classes.js.map +1 -1
- package/lib/commonjs/subtle.js +82 -31
- package/lib/commonjs/subtle.js.map +1 -1
- package/lib/commonjs/utils/types.js.map +1 -1
- package/lib/module/diffie-hellman.js +4 -0
- package/lib/module/diffie-hellman.js.map +1 -1
- package/lib/module/ec.js +19 -25
- package/lib/module/ec.js.map +1 -1
- package/lib/module/ed.js +1 -2
- package/lib/module/ed.js.map +1 -1
- package/lib/module/hash.js +6 -0
- package/lib/module/hash.js.map +1 -1
- package/lib/module/index.js +3 -0
- package/lib/module/index.js.map +1 -1
- package/lib/module/keys/classes.js +9 -5
- package/lib/module/keys/classes.js.map +1 -1
- package/lib/module/subtle.js +83 -32
- package/lib/module/subtle.js.map +1 -1
- package/lib/module/utils/types.js.map +1 -1
- package/lib/tsconfig.tsbuildinfo +1 -1
- package/lib/typescript/diffie-hellman.d.ts +2 -0
- package/lib/typescript/diffie-hellman.d.ts.map +1 -1
- package/lib/typescript/ec.d.ts +1 -0
- package/lib/typescript/ec.d.ts.map +1 -1
- package/lib/typescript/ed.d.ts.map +1 -1
- package/lib/typescript/hash.d.ts +2 -0
- package/lib/typescript/hash.d.ts.map +1 -1
- package/lib/typescript/index.d.ts +5 -0
- package/lib/typescript/index.d.ts.map +1 -1
- package/lib/typescript/keys/classes.d.ts +2 -0
- package/lib/typescript/keys/classes.d.ts.map +1 -1
- package/lib/typescript/specs/diffie-hellman.nitro.d.ts +1 -0
- package/lib/typescript/specs/diffie-hellman.nitro.d.ts.map +1 -1
- package/lib/typescript/specs/ecKeyPair.nitro.d.ts +1 -0
- package/lib/typescript/specs/ecKeyPair.nitro.d.ts.map +1 -1
- package/lib/typescript/specs/keyObjectHandle.nitro.d.ts +2 -0
- package/lib/typescript/specs/keyObjectHandle.nitro.d.ts.map +1 -1
- package/lib/typescript/subtle.d.ts.map +1 -1
- package/lib/typescript/utils/types.d.ts +12 -5
- package/lib/typescript/utils/types.d.ts.map +1 -1
- package/nitrogen/generated/android/QuickCrypto+autolinking.cmake +8 -5
- package/nitrogen/generated/android/QuickCrypto+autolinking.gradle +1 -1
- package/nitrogen/generated/android/QuickCryptoOnLoad.cpp +54 -54
- package/nitrogen/generated/android/QuickCryptoOnLoad.hpp +1 -1
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/crypto/QuickCryptoOnLoad.kt +1 -1
- package/nitrogen/generated/ios/QuickCrypto+autolinking.rb +2 -2
- package/nitrogen/generated/ios/QuickCrypto-Swift-Cxx-Bridge.cpp +1 -1
- package/nitrogen/generated/ios/QuickCrypto-Swift-Cxx-Bridge.hpp +1 -1
- package/nitrogen/generated/ios/QuickCrypto-Swift-Cxx-Umbrella.hpp +1 -1
- package/nitrogen/generated/ios/QuickCryptoAutolinking.mm +54 -54
- package/nitrogen/generated/ios/QuickCryptoAutolinking.swift +5 -1
- package/nitrogen/generated/shared/c++/AsymmetricKeyType.hpp +1 -1
- package/nitrogen/generated/shared/c++/CipherArgs.hpp +34 -19
- package/nitrogen/generated/shared/c++/HybridBlake3Spec.cpp +1 -1
- package/nitrogen/generated/shared/c++/HybridBlake3Spec.hpp +1 -3
- package/nitrogen/generated/shared/c++/HybridCipherFactorySpec.cpp +1 -1
- package/nitrogen/generated/shared/c++/HybridCipherFactorySpec.hpp +1 -1
- package/nitrogen/generated/shared/c++/HybridCipherSpec.cpp +1 -1
- package/nitrogen/generated/shared/c++/HybridCipherSpec.hpp +1 -3
- package/nitrogen/generated/shared/c++/HybridDiffieHellmanSpec.cpp +2 -1
- package/nitrogen/generated/shared/c++/HybridDiffieHellmanSpec.hpp +3 -3
- package/nitrogen/generated/shared/c++/HybridECDHSpec.cpp +1 -1
- package/nitrogen/generated/shared/c++/HybridECDHSpec.hpp +2 -3
- package/nitrogen/generated/shared/c++/HybridEcKeyPairSpec.cpp +2 -1
- package/nitrogen/generated/shared/c++/HybridEcKeyPairSpec.hpp +2 -3
- package/nitrogen/generated/shared/c++/HybridEdKeyPairSpec.cpp +1 -1
- package/nitrogen/generated/shared/c++/HybridEdKeyPairSpec.hpp +2 -3
- package/nitrogen/generated/shared/c++/HybridHashSpec.cpp +1 -1
- package/nitrogen/generated/shared/c++/HybridHashSpec.hpp +2 -4
- package/nitrogen/generated/shared/c++/HybridHkdfSpec.cpp +1 -1
- package/nitrogen/generated/shared/c++/HybridHkdfSpec.hpp +2 -3
- package/nitrogen/generated/shared/c++/HybridHmacSpec.cpp +1 -1
- package/nitrogen/generated/shared/c++/HybridHmacSpec.hpp +3 -4
- package/nitrogen/generated/shared/c++/HybridKeyObjectHandleSpec.cpp +3 -1
- package/nitrogen/generated/shared/c++/HybridKeyObjectHandleSpec.hpp +8 -4
- package/nitrogen/generated/shared/c++/HybridMlDsaKeyPairSpec.cpp +1 -1
- package/nitrogen/generated/shared/c++/HybridMlDsaKeyPairSpec.hpp +2 -3
- package/nitrogen/generated/shared/c++/HybridPbkdf2Spec.cpp +1 -1
- package/nitrogen/generated/shared/c++/HybridPbkdf2Spec.hpp +2 -3
- package/nitrogen/generated/shared/c++/HybridRandomSpec.cpp +1 -1
- package/nitrogen/generated/shared/c++/HybridRandomSpec.hpp +2 -3
- package/nitrogen/generated/shared/c++/HybridRsaCipherSpec.cpp +1 -1
- package/nitrogen/generated/shared/c++/HybridRsaCipherSpec.hpp +1 -3
- package/nitrogen/generated/shared/c++/HybridRsaKeyPairSpec.cpp +1 -1
- package/nitrogen/generated/shared/c++/HybridRsaKeyPairSpec.hpp +1 -3
- package/nitrogen/generated/shared/c++/HybridScryptSpec.cpp +1 -1
- package/nitrogen/generated/shared/c++/HybridScryptSpec.hpp +2 -3
- package/nitrogen/generated/shared/c++/HybridSignHandleSpec.cpp +1 -1
- package/nitrogen/generated/shared/c++/HybridSignHandleSpec.hpp +1 -3
- package/nitrogen/generated/shared/c++/HybridUtilsSpec.cpp +1 -1
- package/nitrogen/generated/shared/c++/HybridUtilsSpec.hpp +2 -3
- package/nitrogen/generated/shared/c++/HybridVerifyHandleSpec.cpp +1 -1
- package/nitrogen/generated/shared/c++/HybridVerifyHandleSpec.hpp +1 -3
- package/nitrogen/generated/shared/c++/JWK.hpp +84 -68
- package/nitrogen/generated/shared/c++/JWKkty.hpp +5 -1
- package/nitrogen/generated/shared/c++/JWKuse.hpp +1 -1
- package/nitrogen/generated/shared/c++/KFormatType.hpp +1 -1
- package/nitrogen/generated/shared/c++/KeyDetail.hpp +39 -23
- package/nitrogen/generated/shared/c++/KeyEncoding.hpp +1 -1
- package/nitrogen/generated/shared/c++/KeyObject.hpp +21 -5
- package/nitrogen/generated/shared/c++/KeyType.hpp +1 -1
- package/nitrogen/generated/shared/c++/KeyUsage.hpp +1 -1
- package/nitrogen/generated/shared/c++/NamedCurve.hpp +1 -1
- package/package.json +1 -1
- package/src/diffie-hellman.ts +6 -0
- package/src/ec.ts +23 -19
- package/src/ed.ts +1 -2
- package/src/hash.ts +11 -0
- package/src/index.ts +3 -0
- package/src/keys/classes.ts +10 -3
- package/src/specs/diffie-hellman.nitro.ts +1 -0
- package/src/specs/ecKeyPair.nitro.ts +2 -0
- package/src/specs/keyObjectHandle.nitro.ts +2 -0
- package/src/subtle.ts +131 -32
- package/src/utils/types.ts +18 -3
- package/deps/ncrypto/WORKSPACE +0 -15
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
#pragma once
|
|
2
|
+
|
|
3
|
+
#include "ncrypto.h"
|
|
4
|
+
|
|
5
|
+
#ifdef OPENSSL_IS_BORINGSSL
|
|
6
|
+
|
|
7
|
+
namespace ncrypto {
|
|
8
|
+
|
|
9
|
+
class AeadCtxPointer;
|
|
10
|
+
|
|
11
|
+
class Aead final {
|
|
12
|
+
private:
|
|
13
|
+
// BoringSSL does not keep a list of AEADs, so we need to maintain our own.
|
|
14
|
+
struct AeadInfo {
|
|
15
|
+
std::string name;
|
|
16
|
+
int mode;
|
|
17
|
+
int nid = 0; // Note: BoringSSL only defines NIDs for some AEADs
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
public:
|
|
21
|
+
Aead() = default;
|
|
22
|
+
Aead(const AeadInfo* info, const EVP_AEAD* aead) : aead_(aead), info_(info) {}
|
|
23
|
+
Aead(const Aead&) = default;
|
|
24
|
+
Aead& operator=(const Aead&) = default;
|
|
25
|
+
NCRYPTO_DISALLOW_MOVE(Aead)
|
|
26
|
+
|
|
27
|
+
inline const EVP_AEAD* get() const { return aead_; }
|
|
28
|
+
std::string_view getModeLabel() const;
|
|
29
|
+
inline operator const EVP_AEAD*() const { return aead_; }
|
|
30
|
+
inline operator bool() const { return aead_ != nullptr; }
|
|
31
|
+
|
|
32
|
+
int getMode() const;
|
|
33
|
+
int getNonceLength() const;
|
|
34
|
+
int getKeyLength() const;
|
|
35
|
+
int getBlockSize() const;
|
|
36
|
+
int getMaxOverhead() const;
|
|
37
|
+
int getMaxTagLength() const;
|
|
38
|
+
std::string_view getName() const;
|
|
39
|
+
|
|
40
|
+
static const Aead FromName(std::string_view name);
|
|
41
|
+
|
|
42
|
+
// TODO(npaun): BoringSSL does not define NIDs for all AEADs.
|
|
43
|
+
// This method is included only for implementing getCipherInfo and can't be
|
|
44
|
+
// used to construct an Aead instance.
|
|
45
|
+
int getNid() const;
|
|
46
|
+
// static const AEAD FromNid(int nid);
|
|
47
|
+
|
|
48
|
+
static const Aead FromCtx(std::string_view name, const AeadCtxPointer& ctx);
|
|
49
|
+
|
|
50
|
+
using AeadNameCallback = std::function<void(std::string_view name)>;
|
|
51
|
+
|
|
52
|
+
// Iterates the known ciphers if the underlying implementation
|
|
53
|
+
// is able to do so.
|
|
54
|
+
static void ForEach(AeadNameCallback callback);
|
|
55
|
+
|
|
56
|
+
// Utilities to get various AEADs by type.
|
|
57
|
+
|
|
58
|
+
static const Aead EMPTY;
|
|
59
|
+
static const Aead AES_128_GCM;
|
|
60
|
+
static const Aead AES_192_GCM;
|
|
61
|
+
static const Aead AES_256_GCM;
|
|
62
|
+
static const Aead CHACHA20_POLY1305;
|
|
63
|
+
static const Aead XCHACHA20_POLY1305;
|
|
64
|
+
static const Aead AES_128_CTR_HMAC_SHA256;
|
|
65
|
+
static const Aead AES_256_CTR_HMAC_SHA256;
|
|
66
|
+
static const Aead AES_128_GCM_SIV;
|
|
67
|
+
static const Aead AES_256_GCM_SIV;
|
|
68
|
+
static const Aead AES_128_GCM_RANDNONCE;
|
|
69
|
+
static const Aead AES_256_GCM_RANDNONCE;
|
|
70
|
+
static const Aead AES_128_CCM_BLUETOOTH;
|
|
71
|
+
static const Aead AES_128_CCM_BLUETOOTH_8;
|
|
72
|
+
static const Aead AES_128_CCM_MATTER;
|
|
73
|
+
static const Aead AES_128_EAX;
|
|
74
|
+
static const Aead AES_256_EAX;
|
|
75
|
+
|
|
76
|
+
private:
|
|
77
|
+
const EVP_AEAD* aead_ = nullptr;
|
|
78
|
+
const AeadInfo* info_ = nullptr;
|
|
79
|
+
|
|
80
|
+
using AeadConstructor = const EVP_AEAD* (*)();
|
|
81
|
+
static const std::unordered_map<AeadConstructor, AeadInfo> aeadIndex;
|
|
82
|
+
static const Aead FromConstructor(AeadConstructor construct);
|
|
83
|
+
};
|
|
84
|
+
|
|
85
|
+
class AeadCtxPointer final {
|
|
86
|
+
public:
|
|
87
|
+
static AeadCtxPointer New(
|
|
88
|
+
const Aead& aead,
|
|
89
|
+
bool encrypt,
|
|
90
|
+
const unsigned char* key = nullptr,
|
|
91
|
+
size_t keyLen = 0,
|
|
92
|
+
size_t tagLen = EVP_AEAD_DEFAULT_TAG_LENGTH /* = 0 */);
|
|
93
|
+
|
|
94
|
+
AeadCtxPointer() = default;
|
|
95
|
+
explicit AeadCtxPointer(EVP_AEAD_CTX* ctx);
|
|
96
|
+
AeadCtxPointer(AeadCtxPointer&& other) noexcept;
|
|
97
|
+
AeadCtxPointer& operator=(AeadCtxPointer&& other) noexcept;
|
|
98
|
+
NCRYPTO_DISALLOW_COPY(AeadCtxPointer)
|
|
99
|
+
~AeadCtxPointer();
|
|
100
|
+
|
|
101
|
+
inline bool operator==(std::nullptr_t) const noexcept {
|
|
102
|
+
return ctx_ == nullptr;
|
|
103
|
+
}
|
|
104
|
+
inline operator bool() const { return ctx_ != nullptr; }
|
|
105
|
+
inline EVP_AEAD_CTX* get() const { return ctx_.get(); }
|
|
106
|
+
inline operator EVP_AEAD_CTX*() const { return ctx_.get(); }
|
|
107
|
+
void reset(EVP_AEAD_CTX* ctx = nullptr);
|
|
108
|
+
EVP_AEAD_CTX* release();
|
|
109
|
+
|
|
110
|
+
bool init(const Aead& aead,
|
|
111
|
+
bool encrypt,
|
|
112
|
+
const unsigned char* key = nullptr,
|
|
113
|
+
size_t keyLen = 0,
|
|
114
|
+
size_t tagLen = EVP_AEAD_DEFAULT_TAG_LENGTH /* = 0 */);
|
|
115
|
+
|
|
116
|
+
// TODO(npaun): BoringSSL does not define NIDs for all AEADs.
|
|
117
|
+
// Decide if we will even implement this method.
|
|
118
|
+
// int getNid() const;
|
|
119
|
+
|
|
120
|
+
bool encrypt(const Buffer<const unsigned char>& in,
|
|
121
|
+
Buffer<unsigned char>& out,
|
|
122
|
+
Buffer<unsigned char>& tag,
|
|
123
|
+
const Buffer<const unsigned char>& nonce,
|
|
124
|
+
const Buffer<const unsigned char>& aad);
|
|
125
|
+
|
|
126
|
+
bool decrypt(const Buffer<const unsigned char>& in,
|
|
127
|
+
Buffer<unsigned char>& out,
|
|
128
|
+
const Buffer<const unsigned char>& tag,
|
|
129
|
+
const Buffer<const unsigned char>& nonce,
|
|
130
|
+
const Buffer<const unsigned char>& aad);
|
|
131
|
+
|
|
132
|
+
private:
|
|
133
|
+
DeleteFnPtr<EVP_AEAD_CTX, EVP_AEAD_CTX_free> ctx_;
|
|
134
|
+
};
|
|
135
|
+
} // namespace ncrypto
|
|
136
|
+
|
|
137
|
+
#endif // OPENSSL_IS_BORINGSSL
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
// ============================================================================
|
|
2
|
+
// Version metadata
|
|
3
|
+
#ifndef NCRYPTO_VERSION_H_
|
|
4
|
+
#define NCRYPTO_VERSION_H_
|
|
5
|
+
|
|
6
|
+
#define NCRYPTO_VERSION "1.1.3" // x-release-please-version
|
|
7
|
+
|
|
8
|
+
enum {
|
|
9
|
+
NCRYPTO_VERSION_MAJOR = 1, // x-release-please-major
|
|
10
|
+
NCRYPTO_VERSION_MINOR = 1, // x-release-please-minor
|
|
11
|
+
NCRYPTO_VERSION_REVISION = 3, // x-release-please-patch
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
#endif // NCRYPTO_VERSION_H_
|
|
@@ -8,6 +8,7 @@
|
|
|
8
8
|
#include <openssl/err.h>
|
|
9
9
|
#include <openssl/evp.h>
|
|
10
10
|
#include <openssl/hmac.h>
|
|
11
|
+
#include <openssl/kdf.h>
|
|
11
12
|
#include <openssl/rsa.h>
|
|
12
13
|
#include <openssl/ssl.h>
|
|
13
14
|
#include <openssl/x509.h>
|
|
@@ -16,17 +17,13 @@
|
|
|
16
17
|
#include <openssl/aead.h>
|
|
17
18
|
#endif
|
|
18
19
|
|
|
19
|
-
#include <stdint.h>
|
|
20
20
|
#include <cstddef>
|
|
21
|
-
#include <cstdio>
|
|
22
21
|
#include <functional>
|
|
23
22
|
#include <list>
|
|
24
23
|
#include <memory>
|
|
25
24
|
#include <optional>
|
|
26
25
|
#include <string>
|
|
27
26
|
#include <string_view>
|
|
28
|
-
#include <unordered_map>
|
|
29
|
-
#include <utility>
|
|
30
27
|
|
|
31
28
|
#if NCRYPTO_DEVELOPMENT_CHECKS
|
|
32
29
|
#include <iostream>
|
|
@@ -81,11 +78,21 @@ namespace ncrypto {
|
|
|
81
78
|
// ============================================================================
|
|
82
79
|
// Utility macros
|
|
83
80
|
|
|
81
|
+
inline bool EqualNoCase(const std::string_view a, const std::string_view b) {
|
|
82
|
+
if (a.size() != b.size()) return false;
|
|
83
|
+
return std::equal(a.begin(), a.end(), b.begin(), b.end(), [](char a, char b) {
|
|
84
|
+
return std::tolower(a) == std::tolower(b);
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
|
|
84
88
|
#if NCRYPTO_DEVELOPMENT_CHECKS
|
|
85
89
|
#define NCRYPTO_STR(x) #x
|
|
86
90
|
#define NCRYPTO_REQUIRE(EXPR) \
|
|
87
91
|
{ \
|
|
88
|
-
if (!(EXPR) {
|
|
92
|
+
if (!(EXPR)) { \
|
|
93
|
+
abort(); \
|
|
94
|
+
} \
|
|
95
|
+
}
|
|
89
96
|
|
|
90
97
|
#define NCRYPTO_FAIL(MESSAGE) \
|
|
91
98
|
do { \
|
|
@@ -262,8 +269,6 @@ class ECKeyPointer;
|
|
|
262
269
|
class Dsa;
|
|
263
270
|
class Rsa;
|
|
264
271
|
class Ec;
|
|
265
|
-
class Aead;
|
|
266
|
-
class AeadCtxPointer;
|
|
267
272
|
|
|
268
273
|
struct StackOfXASN1Deleter {
|
|
269
274
|
void operator()(STACK_OF(ASN1_OBJECT) * p) const {
|
|
@@ -318,25 +323,7 @@ DataPointer xofHashDigest(const Buffer<const unsigned char>& data,
|
|
|
318
323
|
const EVP_MD* md,
|
|
319
324
|
size_t length);
|
|
320
325
|
|
|
321
|
-
|
|
322
|
-
class ModeMixin {
|
|
323
|
-
public:
|
|
324
|
-
std::string_view getModeLabel() const;
|
|
325
|
-
|
|
326
|
-
bool isGcmMode() const { return self().getMode() == EVP_CIPH_GCM_MODE; }
|
|
327
|
-
bool isWrapMode() const { return self().getMode() == EVP_CIPH_WRAP_MODE; }
|
|
328
|
-
bool isCtrMode() const { return self().getMode() == EVP_CIPH_CTR_MODE; }
|
|
329
|
-
bool isCcmMode() const { return self().getMode() == EVP_CIPH_CCM_MODE; }
|
|
330
|
-
bool isOcbMode() const { return self().getMode() == EVP_CIPH_OCB_MODE; }
|
|
331
|
-
bool isStreamMode() const {
|
|
332
|
-
return self().getMode() == EVP_CIPH_STREAM_CIPHER;
|
|
333
|
-
}
|
|
334
|
-
|
|
335
|
-
private:
|
|
336
|
-
const T& self() const { return static_cast<const T&>(*this); }
|
|
337
|
-
};
|
|
338
|
-
|
|
339
|
-
class Cipher final : public ModeMixin<Cipher> {
|
|
326
|
+
class Cipher final {
|
|
340
327
|
public:
|
|
341
328
|
static constexpr size_t MAX_KEY_LENGTH = EVP_MAX_KEY_LENGTH;
|
|
342
329
|
static constexpr size_t MAX_IV_LENGTH = EVP_MAX_IV_LENGTH;
|
|
@@ -345,10 +332,12 @@ class Cipher final : public ModeMixin<Cipher> {
|
|
|
345
332
|
#else
|
|
346
333
|
static constexpr size_t MAX_AUTH_TAG_LENGTH = 16;
|
|
347
334
|
#endif
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
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
|
+
);
|
|
352
341
|
|
|
353
342
|
Cipher() = default;
|
|
354
343
|
Cipher(const EVP_CIPHER* cipher) : cipher_(cipher) {}
|
|
@@ -369,9 +358,15 @@ class Cipher final : public ModeMixin<Cipher> {
|
|
|
369
358
|
int getIvLength() const;
|
|
370
359
|
int getKeyLength() const;
|
|
371
360
|
int getBlockSize() const;
|
|
372
|
-
|
|
361
|
+
std::string_view getModeLabel() const;
|
|
373
362
|
const char* getName() const;
|
|
374
363
|
|
|
364
|
+
bool isGcmMode() const;
|
|
365
|
+
bool isWrapMode() const;
|
|
366
|
+
bool isCtrMode() const;
|
|
367
|
+
bool isCcmMode() const;
|
|
368
|
+
bool isOcbMode() const;
|
|
369
|
+
bool isStreamMode() const;
|
|
375
370
|
bool isChaCha20Poly1305() const;
|
|
376
371
|
|
|
377
372
|
bool isSupportedAuthenticatedMode() const;
|
|
@@ -464,6 +459,61 @@ class Dsa final {
|
|
|
464
459
|
OSSL3_CONST DSA* dsa_;
|
|
465
460
|
};
|
|
466
461
|
|
|
462
|
+
// ============================================================================
|
|
463
|
+
// RSA
|
|
464
|
+
|
|
465
|
+
class Rsa final {
|
|
466
|
+
public:
|
|
467
|
+
Rsa();
|
|
468
|
+
Rsa(OSSL3_CONST RSA* rsa);
|
|
469
|
+
NCRYPTO_DISALLOW_COPY_AND_MOVE(Rsa)
|
|
470
|
+
|
|
471
|
+
inline operator bool() const { return rsa_ != nullptr; }
|
|
472
|
+
inline operator OSSL3_CONST RSA*() const { return rsa_; }
|
|
473
|
+
|
|
474
|
+
struct PublicKey {
|
|
475
|
+
const BIGNUM* n;
|
|
476
|
+
const BIGNUM* e;
|
|
477
|
+
const BIGNUM* d;
|
|
478
|
+
};
|
|
479
|
+
struct PrivateKey {
|
|
480
|
+
const BIGNUM* p;
|
|
481
|
+
const BIGNUM* q;
|
|
482
|
+
const BIGNUM* dp;
|
|
483
|
+
const BIGNUM* dq;
|
|
484
|
+
const BIGNUM* qi;
|
|
485
|
+
};
|
|
486
|
+
struct PssParams {
|
|
487
|
+
std::string_view digest = "sha1";
|
|
488
|
+
std::optional<std::string_view> mgf1_digest = "sha1";
|
|
489
|
+
int64_t salt_length = 20;
|
|
490
|
+
};
|
|
491
|
+
|
|
492
|
+
const PublicKey getPublicKey() const;
|
|
493
|
+
const PrivateKey getPrivateKey() const;
|
|
494
|
+
const std::optional<PssParams> getPssParams() const;
|
|
495
|
+
|
|
496
|
+
bool setPublicKey(BignumPointer&& n, BignumPointer&& e);
|
|
497
|
+
bool setPrivateKey(BignumPointer&& d,
|
|
498
|
+
BignumPointer&& q,
|
|
499
|
+
BignumPointer&& p,
|
|
500
|
+
BignumPointer&& dp,
|
|
501
|
+
BignumPointer&& dq,
|
|
502
|
+
BignumPointer&& qi);
|
|
503
|
+
|
|
504
|
+
using CipherParams = Cipher::CipherParams;
|
|
505
|
+
|
|
506
|
+
static DataPointer encrypt(const EVPKeyPointer& key,
|
|
507
|
+
const CipherParams& params,
|
|
508
|
+
const Buffer<const void> in);
|
|
509
|
+
static DataPointer decrypt(const EVPKeyPointer& key,
|
|
510
|
+
const CipherParams& params,
|
|
511
|
+
const Buffer<const void> in);
|
|
512
|
+
|
|
513
|
+
private:
|
|
514
|
+
OSSL3_CONST RSA* rsa_;
|
|
515
|
+
};
|
|
516
|
+
|
|
467
517
|
class BignumPointer final {
|
|
468
518
|
public:
|
|
469
519
|
BignumPointer() = default;
|
|
@@ -537,58 +587,6 @@ class BignumPointer final {
|
|
|
537
587
|
static bool defaultPrimeCheckCallback(int, int) { return 1; }
|
|
538
588
|
};
|
|
539
589
|
|
|
540
|
-
class Rsa final {
|
|
541
|
-
public:
|
|
542
|
-
Rsa();
|
|
543
|
-
Rsa(OSSL3_CONST RSA* rsa);
|
|
544
|
-
NCRYPTO_DISALLOW_COPY_AND_MOVE(Rsa)
|
|
545
|
-
|
|
546
|
-
inline operator bool() const { return rsa_ != nullptr; }
|
|
547
|
-
inline operator OSSL3_CONST RSA*() const { return rsa_; }
|
|
548
|
-
|
|
549
|
-
struct PublicKey {
|
|
550
|
-
const BIGNUM* n;
|
|
551
|
-
const BIGNUM* e;
|
|
552
|
-
const BIGNUM* d;
|
|
553
|
-
};
|
|
554
|
-
struct PrivateKey {
|
|
555
|
-
const BIGNUM* p;
|
|
556
|
-
const BIGNUM* q;
|
|
557
|
-
const BIGNUM* dp;
|
|
558
|
-
const BIGNUM* dq;
|
|
559
|
-
const BIGNUM* qi;
|
|
560
|
-
};
|
|
561
|
-
struct PssParams {
|
|
562
|
-
std::string_view digest = "sha1";
|
|
563
|
-
std::optional<std::string_view> mgf1_digest = "sha1";
|
|
564
|
-
int64_t salt_length = 20;
|
|
565
|
-
};
|
|
566
|
-
|
|
567
|
-
const PublicKey getPublicKey() const;
|
|
568
|
-
const PrivateKey getPrivateKey() const;
|
|
569
|
-
const std::optional<PssParams> getPssParams() const;
|
|
570
|
-
|
|
571
|
-
bool setPublicKey(BignumPointer&& n, BignumPointer&& e);
|
|
572
|
-
bool setPrivateKey(BignumPointer&& d,
|
|
573
|
-
BignumPointer&& q,
|
|
574
|
-
BignumPointer&& p,
|
|
575
|
-
BignumPointer&& dp,
|
|
576
|
-
BignumPointer&& dq,
|
|
577
|
-
BignumPointer&& qi);
|
|
578
|
-
|
|
579
|
-
using CipherParams = Cipher::CipherParams;
|
|
580
|
-
|
|
581
|
-
static DataPointer encrypt(const EVPKeyPointer& key,
|
|
582
|
-
const CipherParams& params,
|
|
583
|
-
const Buffer<const void> in);
|
|
584
|
-
static DataPointer decrypt(const EVPKeyPointer& key,
|
|
585
|
-
const CipherParams& params,
|
|
586
|
-
const Buffer<const void> in);
|
|
587
|
-
|
|
588
|
-
private:
|
|
589
|
-
OSSL3_CONST RSA* rsa_;
|
|
590
|
-
};
|
|
591
|
-
|
|
592
590
|
class Ec final {
|
|
593
591
|
public:
|
|
594
592
|
Ec();
|
|
@@ -1628,15 +1626,11 @@ bool SafeX509InfoAccessPrint(const BIOPointer& out, X509_EXTENSION* ext);
|
|
|
1628
1626
|
// ============================================================================
|
|
1629
1627
|
// SPKAC
|
|
1630
1628
|
|
|
1631
|
-
|
|
1632
|
-
|
|
1633
|
-
|
|
1634
|
-
[[deprecated("Use the version that takes a Buffer")]] BIOPointer
|
|
1635
|
-
ExportPublicKey(const char* input, size_t length);
|
|
1629
|
+
bool VerifySpkac(const char* input, size_t length);
|
|
1630
|
+
BIOPointer ExportPublicKey(const char* input, size_t length);
|
|
1636
1631
|
|
|
1637
1632
|
// The caller takes ownership of the returned Buffer<char>
|
|
1638
|
-
|
|
1639
|
-
ExportChallenge(const char* input, size_t length);
|
|
1633
|
+
Buffer<char> ExportChallenge(const char* input, size_t length);
|
|
1640
1634
|
|
|
1641
1635
|
bool VerifySpkac(const Buffer<const char>& buf);
|
|
1642
1636
|
BIOPointer ExportPublicKey(const Buffer<const char>& buf);
|
|
@@ -1753,145 +1747,6 @@ class KEM final {
|
|
|
1753
1747
|
|
|
1754
1748
|
#endif // OPENSSL_VERSION_MAJOR >= 3
|
|
1755
1749
|
|
|
1756
|
-
|
|
1757
|
-
// AEAD (Authenticated Encryption with Associated Data)
|
|
1758
|
-
// Note that the underlying EVP_AEAD interface is specific to BoringSSL. AEAD
|
|
1759
|
-
// primitives are accessed through the Cipher class instead, if using OpenSSL.
|
|
1760
|
-
|
|
1761
|
-
#ifdef OPENSSL_IS_BORINGSSL
|
|
1762
|
-
class Aead final : public ModeMixin<Aead> {
|
|
1763
|
-
private:
|
|
1764
|
-
// BoringSSL does not keep a list of AEADs, so we need to maintain our own.
|
|
1765
|
-
struct AeadInfo {
|
|
1766
|
-
std::string name;
|
|
1767
|
-
int mode;
|
|
1768
|
-
int nid = 0; // Note: BoringSSL only defines NIDs for some AEADs
|
|
1769
|
-
};
|
|
1770
|
-
|
|
1771
|
-
public:
|
|
1772
|
-
Aead() = default;
|
|
1773
|
-
Aead(const AeadInfo* info, const EVP_AEAD* aead) : info_(info), aead_(aead) {}
|
|
1774
|
-
Aead(const Aead&) = default;
|
|
1775
|
-
Aead& operator=(const Aead&) = default;
|
|
1776
|
-
NCRYPTO_DISALLOW_MOVE(Aead)
|
|
1777
|
-
|
|
1778
|
-
inline const EVP_AEAD* get() const { return aead_; }
|
|
1779
|
-
inline operator const EVP_AEAD*() const { return aead_; }
|
|
1780
|
-
inline operator bool() const { return aead_ != nullptr; }
|
|
1781
|
-
|
|
1782
|
-
int getMode() const;
|
|
1783
|
-
int getNonceLength() const;
|
|
1784
|
-
int getKeyLength() const;
|
|
1785
|
-
int getBlockSize() const;
|
|
1786
|
-
int getMaxOverhead() const;
|
|
1787
|
-
int getMaxTagLength() const;
|
|
1788
|
-
std::string_view getName() const;
|
|
1789
|
-
|
|
1790
|
-
static const Aead FromName(std::string_view name);
|
|
1791
|
-
|
|
1792
|
-
// TODO(npaun): BoringSSL does not define NIDs for all AEADs.
|
|
1793
|
-
// This method is included only for implementing getCipherInfo and can't be
|
|
1794
|
-
// used to construct an Aead instance.
|
|
1795
|
-
int getNid() const;
|
|
1796
|
-
// static const AEAD FromNid(int nid);
|
|
1797
|
-
|
|
1798
|
-
static const Aead FromCtx(std::string_view name, const AeadCtxPointer& ctx);
|
|
1799
|
-
|
|
1800
|
-
using AeadNameCallback = std::function<void(std::string_view name)>;
|
|
1801
|
-
|
|
1802
|
-
// Iterates the known ciphers if the underlying implementation
|
|
1803
|
-
// is able to do so.
|
|
1804
|
-
static void ForEach(AeadNameCallback callback);
|
|
1805
|
-
|
|
1806
|
-
// Utilities to get various AEADs by type.
|
|
1807
|
-
|
|
1808
|
-
static const Aead EMPTY;
|
|
1809
|
-
static const Aead AES_128_GCM;
|
|
1810
|
-
static const Aead AES_192_GCM;
|
|
1811
|
-
static const Aead AES_256_GCM;
|
|
1812
|
-
static const Aead CHACHA20_POLY1305;
|
|
1813
|
-
static const Aead XCHACHA20_POLY1305;
|
|
1814
|
-
static const Aead AES_128_CTR_HMAC_SHA256;
|
|
1815
|
-
static const Aead AES_256_CTR_HMAC_SHA256;
|
|
1816
|
-
static const Aead AES_128_GCM_SIV;
|
|
1817
|
-
static const Aead AES_256_GCM_SIV;
|
|
1818
|
-
static const Aead AES_128_GCM_RANDNONCE;
|
|
1819
|
-
static const Aead AES_256_GCM_RANDNONCE;
|
|
1820
|
-
static const Aead AES_128_CCM_BLUETOOTH;
|
|
1821
|
-
static const Aead AES_128_CCM_BLUETOOTH_8;
|
|
1822
|
-
static const Aead AES_128_CCM_MATTER;
|
|
1823
|
-
static const Aead AES_128_EAX;
|
|
1824
|
-
static const Aead AES_256_EAX;
|
|
1825
|
-
|
|
1826
|
-
private:
|
|
1827
|
-
const EVP_AEAD* aead_ = nullptr;
|
|
1828
|
-
const AeadInfo* info_ = nullptr;
|
|
1829
|
-
|
|
1830
|
-
using AeadConstructor = const EVP_AEAD* (*)();
|
|
1831
|
-
static const std::unordered_map<AeadConstructor, AeadInfo> aeadIndex;
|
|
1832
|
-
static const Aead FromConstructor(AeadConstructor construct);
|
|
1833
|
-
};
|
|
1834
|
-
|
|
1835
|
-
class AeadCtxPointer final {
|
|
1836
|
-
public:
|
|
1837
|
-
static AeadCtxPointer New(
|
|
1838
|
-
const Aead& aead,
|
|
1839
|
-
bool encrypt,
|
|
1840
|
-
const unsigned char* key = nullptr,
|
|
1841
|
-
size_t keyLen = 0,
|
|
1842
|
-
size_t tagLen = EVP_AEAD_DEFAULT_TAG_LENGTH /* = 0 */);
|
|
1843
|
-
|
|
1844
|
-
AeadCtxPointer() = default;
|
|
1845
|
-
explicit AeadCtxPointer(EVP_AEAD_CTX* ctx);
|
|
1846
|
-
AeadCtxPointer(AeadCtxPointer&& other) noexcept;
|
|
1847
|
-
AeadCtxPointer& operator=(AeadCtxPointer&& other) noexcept;
|
|
1848
|
-
NCRYPTO_DISALLOW_COPY(AeadCtxPointer)
|
|
1849
|
-
~AeadCtxPointer();
|
|
1850
|
-
|
|
1851
|
-
inline bool operator==(std::nullptr_t) const noexcept {
|
|
1852
|
-
return ctx_ == nullptr;
|
|
1853
|
-
}
|
|
1854
|
-
inline operator bool() const { return ctx_ != nullptr; }
|
|
1855
|
-
inline EVP_AEAD_CTX* get() const { return ctx_.get(); }
|
|
1856
|
-
inline operator EVP_AEAD_CTX*() const { return ctx_.get(); }
|
|
1857
|
-
void reset(EVP_AEAD_CTX* ctx = nullptr);
|
|
1858
|
-
EVP_AEAD_CTX* release();
|
|
1859
|
-
|
|
1860
|
-
bool init(const Aead& aead,
|
|
1861
|
-
bool encrypt,
|
|
1862
|
-
const unsigned char* key = nullptr,
|
|
1863
|
-
size_t keyLen = 0,
|
|
1864
|
-
size_t tagLen = EVP_AEAD_DEFAULT_TAG_LENGTH /* = 0 */);
|
|
1865
|
-
|
|
1866
|
-
// TODO(npaun): BoringSSL does not define NIDs for all AEADs.
|
|
1867
|
-
// Decide if we will even implement this method.
|
|
1868
|
-
// int getNid() const;
|
|
1869
|
-
|
|
1870
|
-
bool encrypt(const Buffer<const unsigned char>& in,
|
|
1871
|
-
Buffer<unsigned char>& out,
|
|
1872
|
-
Buffer<unsigned char>& tag,
|
|
1873
|
-
const Buffer<const unsigned char>& nonce,
|
|
1874
|
-
const Buffer<const unsigned char>& aad);
|
|
1875
|
-
|
|
1876
|
-
bool decrypt(const Buffer<const unsigned char>& in,
|
|
1877
|
-
Buffer<unsigned char>& out,
|
|
1878
|
-
const Buffer<const unsigned char>& tag,
|
|
1879
|
-
const Buffer<const unsigned char>& nonce,
|
|
1880
|
-
const Buffer<const unsigned char>& aad);
|
|
1881
|
-
|
|
1882
|
-
private:
|
|
1883
|
-
DeleteFnPtr<EVP_AEAD_CTX, EVP_AEAD_CTX_free> ctx_;
|
|
1884
|
-
};
|
|
1885
|
-
#endif
|
|
1886
|
-
|
|
1887
|
-
// ============================================================================
|
|
1888
|
-
// Version metadata
|
|
1889
|
-
#define NCRYPTO_VERSION "0.0.1"
|
|
1890
|
-
|
|
1891
|
-
enum {
|
|
1892
|
-
NCRYPTO_VERSION_MAJOR = 0,
|
|
1893
|
-
NCRYPTO_VERSION_MINOR = 0,
|
|
1894
|
-
NCRYPTO_VERSION_REVISION = 1,
|
|
1895
|
-
};
|
|
1750
|
+
#include "ncrypto/version.h"
|
|
1896
1751
|
|
|
1897
1752
|
} // namespace ncrypto
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
prefix=@CMAKE_INSTALL_PREFIX@
|
|
2
|
+
exec_prefix=${prefix}
|
|
3
|
+
libdir=@CMAKE_INSTALL_FULL_LIBDIR@
|
|
4
|
+
includedir=@CMAKE_INSTALL_FULL_INCLUDEDIR@
|
|
5
|
+
|
|
6
|
+
Name: ncrypto
|
|
7
|
+
Description: crypto functions for node:crypto
|
|
8
|
+
Version: @PROJECT_VERSION@
|
|
9
|
+
Libs: -L${libdir} -lncrypto
|
|
10
|
+
Cflags: -I${includedir}
|
|
@@ -1,12 +1,37 @@
|
|
|
1
|
-
add_library(ncrypto ncrypto.cpp engine.cpp)
|
|
2
|
-
target_link_libraries(ncrypto PUBLIC ssl crypto)
|
|
1
|
+
add_library(ncrypto ncrypto.cpp engine.cpp aead.cpp)
|
|
3
2
|
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
3
|
+
# Enable strict warning flags for ncrypto sources only
|
|
4
|
+
if(CMAKE_CXX_COMPILER_ID MATCHES "GNU|Clang|AppleClang")
|
|
5
|
+
target_compile_options(ncrypto PRIVATE
|
|
6
|
+
-Werror
|
|
7
|
+
-Wextra
|
|
8
|
+
-Wno-unused-parameter
|
|
9
|
+
-Wimplicit-fallthrough
|
|
10
|
+
-Wno-deprecated-declarations # OpenSSL 3.0 deprecates many APIs we intentionally use
|
|
11
|
+
)
|
|
12
|
+
elseif(MSVC)
|
|
13
|
+
target_compile_options(ncrypto PRIVATE
|
|
14
|
+
/WX # Treat warnings as errors
|
|
15
|
+
/W3 # Warning level 3 (production quality)
|
|
16
|
+
/wd4100 # Unreferenced formal parameter (like -Wno-unused-parameter)
|
|
17
|
+
/wd4267 # Conversion from 'size_t' to smaller type
|
|
18
|
+
/wd4244 # Conversion, possible loss of data
|
|
19
|
+
/wd4305 # Truncation from 'int' to 'bool'
|
|
20
|
+
/wd4127 # Conditional expression is constant
|
|
21
|
+
)
|
|
8
22
|
endif()
|
|
9
23
|
|
|
24
|
+
if (NCRYPTO_SHARED_LIBS)
|
|
25
|
+
target_link_libraries(ncrypto PUBLIC OpenSSL::SSL OpenSSL::Crypto)
|
|
26
|
+
else()
|
|
27
|
+
target_link_libraries(ncrypto PUBLIC ssl crypto)
|
|
28
|
+
|
|
29
|
+
if (NCRYPTO_BSSL_LIBDECREPIT_MISSING)
|
|
30
|
+
target_compile_definitions(ncrypto PUBLIC NCRYPTO_BSSL_LIBDECREPIT_MISSING=1)
|
|
31
|
+
else()
|
|
32
|
+
target_link_libraries(ncrypto PUBLIC decrepit)
|
|
33
|
+
endif()
|
|
34
|
+
endif()
|
|
10
35
|
target_include_directories(ncrypto
|
|
11
36
|
PUBLIC
|
|
12
37
|
$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include>
|