react-native-quick-crypto 1.0.0 → 1.0.1
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 +14 -5
- package/android/CMakeLists.txt +4 -2
- package/android/build.gradle +1 -1
- package/cpp/cipher/HybridCipher.cpp +3 -2
- package/cpp/cipher/HybridRsaCipher.cpp +20 -1
- package/cpp/keys/HybridKeyObjectHandle.cpp +8 -0
- package/cpp/keys/KeyObjectData.hpp +1 -1
- package/cpp/mldsa/HybridMlDsaKeyPair.cpp +264 -0
- package/cpp/mldsa/HybridMlDsaKeyPair.hpp +47 -0
- package/cpp/sign/HybridSignHandle.cpp +97 -22
- package/cpp/sign/HybridVerifyHandle.cpp +90 -21
- package/deps/ncrypto/.bazelignore +4 -0
- package/deps/ncrypto/.bazelrc +2 -0
- package/deps/ncrypto/.bazelversion +1 -0
- package/deps/ncrypto/.clang-format +111 -0
- package/deps/ncrypto/.github/workflows/bazel.yml +58 -0
- package/deps/ncrypto/.github/workflows/linter.yml +38 -0
- package/deps/ncrypto/.github/workflows/macos.yml +43 -0
- package/deps/ncrypto/.github/workflows/ubuntu.yml +46 -0
- package/deps/ncrypto/.github/workflows/visual-studio.yml +49 -0
- package/deps/ncrypto/.python-version +1 -0
- package/deps/ncrypto/BUILD.bazel +36 -0
- package/deps/ncrypto/CMakeLists.txt +55 -0
- package/deps/ncrypto/LICENSE +21 -0
- package/deps/ncrypto/MODULE.bazel +1 -0
- package/deps/ncrypto/MODULE.bazel.lock +280 -0
- package/deps/ncrypto/README.md +18 -0
- package/deps/ncrypto/WORKSPACE +15 -0
- package/deps/ncrypto/cmake/CPM.cmake +1225 -0
- package/deps/ncrypto/cmake/ncrypto-flags.cmake +16 -0
- package/deps/ncrypto/include/dh-primes.h +67 -0
- package/deps/ncrypto/{ncrypto.h → include/ncrypto.h} +361 -89
- package/deps/ncrypto/patches/0001-Expose-libdecrepit-so-NodeJS-can-use-it-for-ncrypto.patch +28 -0
- package/deps/ncrypto/pyproject.toml +38 -0
- package/deps/ncrypto/src/CMakeLists.txt +15 -0
- package/deps/ncrypto/src/engine.cpp +93 -0
- package/deps/ncrypto/{ncrypto.cc → src/ncrypto.cpp} +1168 -234
- package/deps/ncrypto/tests/BUILD.bazel +9 -0
- package/deps/ncrypto/tests/CMakeLists.txt +7 -0
- package/deps/ncrypto/tests/basic.cpp +86 -0
- package/deps/ncrypto/tools/run-clang-format.sh +42 -0
- package/lib/commonjs/keys/classes.js +6 -0
- package/lib/commonjs/keys/classes.js.map +1 -1
- package/lib/commonjs/mldsa.js +69 -0
- package/lib/commonjs/mldsa.js.map +1 -0
- package/lib/commonjs/specs/mlDsaKeyPair.nitro.js +6 -0
- package/lib/commonjs/specs/mlDsaKeyPair.nitro.js.map +1 -0
- package/lib/commonjs/subtle.js +111 -6
- package/lib/commonjs/subtle.js.map +1 -1
- package/lib/commonjs/utils/types.js.map +1 -1
- package/lib/module/keys/classes.js +6 -0
- package/lib/module/keys/classes.js.map +1 -1
- package/lib/module/mldsa.js +63 -0
- package/lib/module/mldsa.js.map +1 -0
- package/lib/module/specs/mlDsaKeyPair.nitro.js +4 -0
- package/lib/module/specs/mlDsaKeyPair.nitro.js.map +1 -0
- package/lib/module/subtle.js +111 -6
- 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/keys/classes.d.ts +2 -0
- package/lib/typescript/keys/classes.d.ts.map +1 -1
- package/lib/typescript/mldsa.d.ts +18 -0
- package/lib/typescript/mldsa.d.ts.map +1 -0
- package/lib/typescript/specs/mlDsaKeyPair.nitro.d.ts +16 -0
- package/lib/typescript/specs/mlDsaKeyPair.nitro.d.ts.map +1 -0
- package/lib/typescript/subtle.d.ts.map +1 -1
- package/lib/typescript/utils/types.d.ts +5 -3
- package/lib/typescript/utils/types.d.ts.map +1 -1
- package/nitrogen/generated/android/QuickCrypto+autolinking.cmake +1 -0
- package/nitrogen/generated/android/QuickCryptoOnLoad.cpp +10 -0
- package/nitrogen/generated/ios/QuickCryptoAutolinking.mm +10 -0
- package/nitrogen/generated/shared/c++/AsymmetricKeyType.hpp +12 -0
- package/nitrogen/generated/shared/c++/HybridMlDsaKeyPairSpec.cpp +29 -0
- package/nitrogen/generated/shared/c++/HybridMlDsaKeyPairSpec.hpp +73 -0
- package/package.json +7 -3
- package/src/keys/classes.ts +9 -0
- package/src/mldsa.ts +125 -0
- package/src/specs/mlDsaKeyPair.nitro.ts +29 -0
- package/src/subtle.ts +148 -8
- package/src/utils/types.ts +11 -3
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
option(NCRYPTO_DEVELOPMENT_CHECKS "development checks (useful for debugging)" OFF)
|
|
2
|
+
option(NCRYPTO_TESTING "Build tests" ON)
|
|
3
|
+
option(NCRYPTO_BSSL_LIBDECREPIT_MISSING "enable if boringssl is built without libdecrepit" OFF)
|
|
4
|
+
|
|
5
|
+
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
|
|
6
|
+
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
|
|
7
|
+
set(CMAKE_CXX_STANDARD 20)
|
|
8
|
+
set(CMAKE_CXX_STANDARD_REQUIRED ON)
|
|
9
|
+
set(CMAKE_CXX_EXTENSIONS OFF)
|
|
10
|
+
|
|
11
|
+
find_program(CCACHE_FOUND ccache)
|
|
12
|
+
if(CCACHE_FOUND)
|
|
13
|
+
message(STATUS "Ccache found using it as compiler launcher.")
|
|
14
|
+
set(CMAKE_C_COMPILER_LAUNCHER ccache)
|
|
15
|
+
set(CMAKE_CXX_COMPILER_LAUNCHER ccache)
|
|
16
|
+
endif(CCACHE_FOUND)
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
/* ====================================================================
|
|
2
|
+
* Copyright (c) 2011 The OpenSSL Project. All rights reserved.
|
|
3
|
+
*
|
|
4
|
+
* Redistribution and use in source and binary forms, with or without
|
|
5
|
+
* modification, are permitted provided that the following conditions
|
|
6
|
+
* are met:
|
|
7
|
+
*
|
|
8
|
+
* 1. Redistributions of source code must retain the above copyright
|
|
9
|
+
* notice, this list of conditions and the following disclaimer.
|
|
10
|
+
*
|
|
11
|
+
* 2. Redistributions in binary form must reproduce the above copyright
|
|
12
|
+
* notice, this list of conditions and the following disclaimer in
|
|
13
|
+
* the documentation and/or other materials provided with the
|
|
14
|
+
* distribution.
|
|
15
|
+
*
|
|
16
|
+
* 3. All advertising materials mentioning features or use of this
|
|
17
|
+
* software must display the following acknowledgment:
|
|
18
|
+
* "This product includes software developed by the OpenSSL Project
|
|
19
|
+
* for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
|
|
20
|
+
*
|
|
21
|
+
* 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
|
|
22
|
+
* endorse or promote products derived from this software without
|
|
23
|
+
* prior written permission. For written permission, please contact
|
|
24
|
+
* licensing@OpenSSL.org.
|
|
25
|
+
*
|
|
26
|
+
* 5. Products derived from this software may not be called "OpenSSL"
|
|
27
|
+
* nor may "OpenSSL" appear in their names without prior written
|
|
28
|
+
* permission of the OpenSSL Project.
|
|
29
|
+
*
|
|
30
|
+
* 6. Redistributions of any form whatsoever must retain the following
|
|
31
|
+
* acknowledgment:
|
|
32
|
+
* "This product includes software developed by the OpenSSL Project
|
|
33
|
+
* for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
|
|
34
|
+
*
|
|
35
|
+
* THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
|
|
36
|
+
* EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
37
|
+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
|
38
|
+
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
|
|
39
|
+
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
40
|
+
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|
41
|
+
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
|
42
|
+
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
43
|
+
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
|
|
44
|
+
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
45
|
+
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
|
|
46
|
+
* OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
47
|
+
* ====================================================================
|
|
48
|
+
*
|
|
49
|
+
* This product includes cryptographic software written by Eric Young
|
|
50
|
+
* (eay@cryptsoft.com). This product includes software written by Tim
|
|
51
|
+
* Hudson (tjh@cryptsoft.com). */
|
|
52
|
+
|
|
53
|
+
#ifndef DEPS_NCRYPTO_DH_PRIMES_H_
|
|
54
|
+
#define DEPS_NCRYPTO_DH_PRIMES_H_
|
|
55
|
+
|
|
56
|
+
#include <openssl/bn.h>
|
|
57
|
+
|
|
58
|
+
// Backporting primes that may not be supported in earlier boringssl versions.
|
|
59
|
+
// Intentionally keeping the existing C-style formatting.
|
|
60
|
+
|
|
61
|
+
BIGNUM* BN_get_rfc3526_prime_2048(BIGNUM* ret);
|
|
62
|
+
BIGNUM* BN_get_rfc3526_prime_3072(BIGNUM* ret);
|
|
63
|
+
BIGNUM* BN_get_rfc3526_prime_4096(BIGNUM* ret);
|
|
64
|
+
BIGNUM* BN_get_rfc3526_prime_6144(BIGNUM* ret);
|
|
65
|
+
BIGNUM* BN_get_rfc3526_prime_8192(BIGNUM* ret);
|
|
66
|
+
|
|
67
|
+
#endif // DEPS_NCRYPTO_DH_PRIMES_H_
|
|
@@ -8,17 +8,30 @@
|
|
|
8
8
|
#include <openssl/err.h>
|
|
9
9
|
#include <openssl/evp.h>
|
|
10
10
|
#include <openssl/hmac.h>
|
|
11
|
-
#include <openssl/kdf.h>
|
|
12
11
|
#include <openssl/rsa.h>
|
|
13
12
|
#include <openssl/ssl.h>
|
|
14
13
|
#include <openssl/x509.h>
|
|
14
|
+
|
|
15
|
+
#ifdef OPENSSL_IS_BORINGSSL
|
|
16
|
+
#include <openssl/aead.h>
|
|
17
|
+
#endif
|
|
18
|
+
|
|
19
|
+
#include <stdint.h>
|
|
15
20
|
#include <cstddef>
|
|
21
|
+
#include <cstdio>
|
|
16
22
|
#include <functional>
|
|
17
23
|
#include <list>
|
|
18
24
|
#include <memory>
|
|
19
25
|
#include <optional>
|
|
20
26
|
#include <string>
|
|
21
27
|
#include <string_view>
|
|
28
|
+
#include <unordered_map>
|
|
29
|
+
#include <utility>
|
|
30
|
+
|
|
31
|
+
#if NCRYPTO_DEVELOPMENT_CHECKS
|
|
32
|
+
#include <iostream>
|
|
33
|
+
#endif
|
|
34
|
+
|
|
22
35
|
#ifndef OPENSSL_NO_ENGINE
|
|
23
36
|
#include <openssl/engine.h>
|
|
24
37
|
#endif // !OPENSSL_NO_ENGINE
|
|
@@ -57,6 +70,12 @@ using OPENSSL_SIZE_T = size_t;
|
|
|
57
70
|
using OPENSSL_SIZE_T = int;
|
|
58
71
|
#endif
|
|
59
72
|
|
|
73
|
+
#ifdef OPENSSL_IS_BORINGSSL
|
|
74
|
+
#ifdef NCRYPTO_BSSL_NEEDS_DH_PRIMES
|
|
75
|
+
#include "dh-primes.h"
|
|
76
|
+
#endif // NCRYPTO_BSSL_NEEDS_DH_PRIMES
|
|
77
|
+
#endif // OPENSSL_IS_BORINGSSL
|
|
78
|
+
|
|
60
79
|
namespace ncrypto {
|
|
61
80
|
|
|
62
81
|
// ============================================================================
|
|
@@ -229,6 +248,8 @@ class DataPointer;
|
|
|
229
248
|
class DHPointer;
|
|
230
249
|
class ECKeyPointer;
|
|
231
250
|
class EVPKeyPointer;
|
|
251
|
+
class EVPMacCtxPointer;
|
|
252
|
+
class EVPMacPointer;
|
|
232
253
|
class EVPMDCtxPointer;
|
|
233
254
|
class SSLCtxPointer;
|
|
234
255
|
class SSLPointer;
|
|
@@ -241,6 +262,8 @@ class ECKeyPointer;
|
|
|
241
262
|
class Dsa;
|
|
242
263
|
class Rsa;
|
|
243
264
|
class Ec;
|
|
265
|
+
class Aead;
|
|
266
|
+
class AeadCtxPointer;
|
|
244
267
|
|
|
245
268
|
struct StackOfXASN1Deleter {
|
|
246
269
|
void operator()(STACK_OF(ASN1_OBJECT) * p) const {
|
|
@@ -295,7 +318,25 @@ DataPointer xofHashDigest(const Buffer<const unsigned char>& data,
|
|
|
295
318
|
const EVP_MD* md,
|
|
296
319
|
size_t length);
|
|
297
320
|
|
|
298
|
-
|
|
321
|
+
template <typename T>
|
|
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> {
|
|
299
340
|
public:
|
|
300
341
|
static constexpr size_t MAX_KEY_LENGTH = EVP_MAX_KEY_LENGTH;
|
|
301
342
|
static constexpr size_t MAX_IV_LENGTH = EVP_MAX_IV_LENGTH;
|
|
@@ -304,9 +345,10 @@ class Cipher final {
|
|
|
304
345
|
#else
|
|
305
346
|
static constexpr size_t MAX_AUTH_TAG_LENGTH = 16;
|
|
306
347
|
#endif
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
348
|
+
// FIXME: These constants are not available in all OpenSSL/BoringSSL versions
|
|
349
|
+
// static_assert(EVP_GCM_TLS_TAG_LEN <= MAX_AUTH_TAG_LENGTH &&
|
|
350
|
+
// EVP_CCM_TLS_TAG_LEN <= MAX_AUTH_TAG_LENGTH &&
|
|
351
|
+
// EVP_CHACHAPOLY_TLS_TAG_LEN <= MAX_AUTH_TAG_LENGTH);
|
|
310
352
|
|
|
311
353
|
Cipher() = default;
|
|
312
354
|
Cipher(const EVP_CIPHER* cipher) : cipher_(cipher) {}
|
|
@@ -327,15 +369,9 @@ class Cipher final {
|
|
|
327
369
|
int getIvLength() const;
|
|
328
370
|
int getKeyLength() const;
|
|
329
371
|
int getBlockSize() const;
|
|
330
|
-
|
|
372
|
+
|
|
331
373
|
const char* getName() const;
|
|
332
374
|
|
|
333
|
-
bool isGcmMode() const;
|
|
334
|
-
bool isWrapMode() const;
|
|
335
|
-
bool isCtrMode() const;
|
|
336
|
-
bool isCcmMode() const;
|
|
337
|
-
bool isOcbMode() const;
|
|
338
|
-
bool isStreamMode() const;
|
|
339
375
|
bool isChaCha20Poly1305() const;
|
|
340
376
|
|
|
341
377
|
bool isSupportedAuthenticatedMode() const;
|
|
@@ -428,8 +464,78 @@ class Dsa final {
|
|
|
428
464
|
OSSL3_CONST DSA* dsa_;
|
|
429
465
|
};
|
|
430
466
|
|
|
431
|
-
|
|
432
|
-
|
|
467
|
+
class BignumPointer final {
|
|
468
|
+
public:
|
|
469
|
+
BignumPointer() = default;
|
|
470
|
+
explicit BignumPointer(BIGNUM* bignum);
|
|
471
|
+
explicit BignumPointer(const unsigned char* data, size_t len);
|
|
472
|
+
BignumPointer(BignumPointer&& other) noexcept;
|
|
473
|
+
BignumPointer& operator=(BignumPointer&& other) noexcept;
|
|
474
|
+
NCRYPTO_DISALLOW_COPY(BignumPointer)
|
|
475
|
+
~BignumPointer();
|
|
476
|
+
|
|
477
|
+
int operator<=>(const BignumPointer& other) const noexcept;
|
|
478
|
+
int operator<=>(const BIGNUM* other) const noexcept;
|
|
479
|
+
inline operator bool() const { return bn_ != nullptr; }
|
|
480
|
+
inline BIGNUM* get() const noexcept { return bn_.get(); }
|
|
481
|
+
void reset(BIGNUM* bn = nullptr);
|
|
482
|
+
void reset(const unsigned char* data, size_t len);
|
|
483
|
+
BIGNUM* release();
|
|
484
|
+
|
|
485
|
+
bool isZero() const;
|
|
486
|
+
bool isOne() const;
|
|
487
|
+
|
|
488
|
+
bool setWord(unsigned long w); // NOLINT(runtime/int)
|
|
489
|
+
unsigned long getWord() const; // NOLINT(runtime/int)
|
|
490
|
+
|
|
491
|
+
size_t byteLength() const;
|
|
492
|
+
size_t bitLength() const;
|
|
493
|
+
|
|
494
|
+
DataPointer toHex() const;
|
|
495
|
+
DataPointer encode() const;
|
|
496
|
+
DataPointer encodePadded(size_t size) const;
|
|
497
|
+
size_t encodeInto(unsigned char* out) const;
|
|
498
|
+
size_t encodePaddedInto(unsigned char* out, size_t size) const;
|
|
499
|
+
|
|
500
|
+
using PrimeCheckCallback = std::function<bool(int, int)>;
|
|
501
|
+
int isPrime(int checks,
|
|
502
|
+
PrimeCheckCallback cb = defaultPrimeCheckCallback) const;
|
|
503
|
+
struct PrimeConfig {
|
|
504
|
+
int bits;
|
|
505
|
+
bool safe = false;
|
|
506
|
+
const BignumPointer& add;
|
|
507
|
+
const BignumPointer& rem;
|
|
508
|
+
};
|
|
509
|
+
|
|
510
|
+
static BignumPointer NewPrime(
|
|
511
|
+
const PrimeConfig& params,
|
|
512
|
+
PrimeCheckCallback cb = defaultPrimeCheckCallback);
|
|
513
|
+
|
|
514
|
+
bool generate(const PrimeConfig& params,
|
|
515
|
+
PrimeCheckCallback cb = defaultPrimeCheckCallback) const;
|
|
516
|
+
|
|
517
|
+
static BignumPointer New();
|
|
518
|
+
static BignumPointer NewSecure();
|
|
519
|
+
static BignumPointer NewSub(const BignumPointer& a, const BignumPointer& b);
|
|
520
|
+
static BignumPointer NewLShift(size_t length);
|
|
521
|
+
|
|
522
|
+
static DataPointer Encode(const BIGNUM* bn);
|
|
523
|
+
static DataPointer EncodePadded(const BIGNUM* bn, size_t size);
|
|
524
|
+
static size_t EncodePaddedInto(const BIGNUM* bn,
|
|
525
|
+
unsigned char* out,
|
|
526
|
+
size_t size);
|
|
527
|
+
static int GetBitCount(const BIGNUM* bn);
|
|
528
|
+
static int GetByteCount(const BIGNUM* bn);
|
|
529
|
+
static unsigned long GetWord(const BIGNUM* bn); // NOLINT(runtime/int)
|
|
530
|
+
static const BIGNUM* One();
|
|
531
|
+
|
|
532
|
+
BignumPointer clone();
|
|
533
|
+
|
|
534
|
+
private:
|
|
535
|
+
DeleteFnPtr<BIGNUM, BN_clear_free> bn_;
|
|
536
|
+
|
|
537
|
+
static bool defaultPrimeCheckCallback(int, int) { return 1; }
|
|
538
|
+
};
|
|
433
539
|
|
|
434
540
|
class Rsa final {
|
|
435
541
|
public:
|
|
@@ -491,6 +597,10 @@ class Ec final {
|
|
|
491
597
|
|
|
492
598
|
const EC_GROUP* getGroup() const;
|
|
493
599
|
int getCurve() const;
|
|
600
|
+
uint32_t getDegree() const;
|
|
601
|
+
std::string getCurveName() const;
|
|
602
|
+
const EC_POINT* getPublicKey() const;
|
|
603
|
+
const BIGNUM* getPrivateKey() const;
|
|
494
604
|
|
|
495
605
|
inline operator bool() const { return ec_ != nullptr; }
|
|
496
606
|
inline operator OSSL3_CONST EC_KEY*() const { return ec_; }
|
|
@@ -500,8 +610,16 @@ class Ec final {
|
|
|
500
610
|
using GetCurveCallback = std::function<bool(const char*)>;
|
|
501
611
|
static bool GetCurves(GetCurveCallback callback);
|
|
502
612
|
|
|
613
|
+
inline const BignumPointer& getX() const { return x_; }
|
|
614
|
+
inline const BignumPointer& getY() const { return y_; }
|
|
615
|
+
inline const BignumPointer& getD() const { return d_; }
|
|
616
|
+
|
|
503
617
|
private:
|
|
504
618
|
OSSL3_CONST EC_KEY* ec_ = nullptr;
|
|
619
|
+
// Affine coordinates for the EC_KEY.
|
|
620
|
+
BignumPointer x_;
|
|
621
|
+
BignumPointer y_;
|
|
622
|
+
BignumPointer d_;
|
|
505
623
|
};
|
|
506
624
|
|
|
507
625
|
// A managed pointer to a buffer of data. When destroyed the underlying
|
|
@@ -632,78 +750,6 @@ class BIOPointer final {
|
|
|
632
750
|
mutable DeleteFnPtr<BIO, BIO_free_all> bio_;
|
|
633
751
|
};
|
|
634
752
|
|
|
635
|
-
class BignumPointer final {
|
|
636
|
-
public:
|
|
637
|
-
BignumPointer() = default;
|
|
638
|
-
explicit BignumPointer(BIGNUM* bignum);
|
|
639
|
-
explicit BignumPointer(const unsigned char* data, size_t len);
|
|
640
|
-
BignumPointer(BignumPointer&& other) noexcept;
|
|
641
|
-
BignumPointer& operator=(BignumPointer&& other) noexcept;
|
|
642
|
-
NCRYPTO_DISALLOW_COPY(BignumPointer)
|
|
643
|
-
~BignumPointer();
|
|
644
|
-
|
|
645
|
-
int operator<=>(const BignumPointer& other) const noexcept;
|
|
646
|
-
int operator<=>(const BIGNUM* other) const noexcept;
|
|
647
|
-
inline operator bool() const { return bn_ != nullptr; }
|
|
648
|
-
inline BIGNUM* get() const noexcept { return bn_.get(); }
|
|
649
|
-
void reset(BIGNUM* bn = nullptr);
|
|
650
|
-
void reset(const unsigned char* data, size_t len);
|
|
651
|
-
BIGNUM* release();
|
|
652
|
-
|
|
653
|
-
bool isZero() const;
|
|
654
|
-
bool isOne() const;
|
|
655
|
-
|
|
656
|
-
bool setWord(unsigned long w); // NOLINT(runtime/int)
|
|
657
|
-
unsigned long getWord() const; // NOLINT(runtime/int)
|
|
658
|
-
|
|
659
|
-
size_t byteLength() const;
|
|
660
|
-
|
|
661
|
-
DataPointer toHex() const;
|
|
662
|
-
DataPointer encode() const;
|
|
663
|
-
DataPointer encodePadded(size_t size) const;
|
|
664
|
-
size_t encodeInto(unsigned char* out) const;
|
|
665
|
-
size_t encodePaddedInto(unsigned char* out, size_t size) const;
|
|
666
|
-
|
|
667
|
-
using PrimeCheckCallback = std::function<bool(int, int)>;
|
|
668
|
-
int isPrime(int checks,
|
|
669
|
-
PrimeCheckCallback cb = defaultPrimeCheckCallback) const;
|
|
670
|
-
struct PrimeConfig {
|
|
671
|
-
int bits;
|
|
672
|
-
bool safe = false;
|
|
673
|
-
const BignumPointer& add;
|
|
674
|
-
const BignumPointer& rem;
|
|
675
|
-
};
|
|
676
|
-
|
|
677
|
-
static BignumPointer NewPrime(
|
|
678
|
-
const PrimeConfig& params,
|
|
679
|
-
PrimeCheckCallback cb = defaultPrimeCheckCallback);
|
|
680
|
-
|
|
681
|
-
bool generate(const PrimeConfig& params,
|
|
682
|
-
PrimeCheckCallback cb = defaultPrimeCheckCallback) const;
|
|
683
|
-
|
|
684
|
-
static BignumPointer New();
|
|
685
|
-
static BignumPointer NewSecure();
|
|
686
|
-
static BignumPointer NewSub(const BignumPointer& a, const BignumPointer& b);
|
|
687
|
-
static BignumPointer NewLShift(size_t length);
|
|
688
|
-
|
|
689
|
-
static DataPointer Encode(const BIGNUM* bn);
|
|
690
|
-
static DataPointer EncodePadded(const BIGNUM* bn, size_t size);
|
|
691
|
-
static size_t EncodePaddedInto(const BIGNUM* bn,
|
|
692
|
-
unsigned char* out,
|
|
693
|
-
size_t size);
|
|
694
|
-
static int GetBitCount(const BIGNUM* bn);
|
|
695
|
-
static int GetByteCount(const BIGNUM* bn);
|
|
696
|
-
static unsigned long GetWord(const BIGNUM* bn); // NOLINT(runtime/int)
|
|
697
|
-
static const BIGNUM* One();
|
|
698
|
-
|
|
699
|
-
BignumPointer clone();
|
|
700
|
-
|
|
701
|
-
private:
|
|
702
|
-
DeleteFnPtr<BIGNUM, BN_clear_free> bn_;
|
|
703
|
-
|
|
704
|
-
static bool defaultPrimeCheckCallback(int, int) { return 1; }
|
|
705
|
-
};
|
|
706
|
-
|
|
707
753
|
class CipherCtxPointer final {
|
|
708
754
|
public:
|
|
709
755
|
static CipherCtxPointer New();
|
|
@@ -945,12 +991,15 @@ class EVPKeyPointer final {
|
|
|
945
991
|
int getDefaultSignPadding() const;
|
|
946
992
|
operator Rsa() const;
|
|
947
993
|
operator Dsa() const;
|
|
994
|
+
operator Ec() const;
|
|
948
995
|
|
|
949
996
|
bool isRsaVariant() const;
|
|
950
997
|
bool isOneShotVariant() const;
|
|
951
998
|
bool isSigVariant() const;
|
|
952
999
|
bool validateDsaParameters() const;
|
|
953
1000
|
|
|
1001
|
+
EVPKeyPointer clone() const;
|
|
1002
|
+
|
|
954
1003
|
private:
|
|
955
1004
|
DeleteFnPtr<EVP_PKEY, EVP_PKEY_free> pkey_;
|
|
956
1005
|
};
|
|
@@ -1189,6 +1238,8 @@ class X509View final {
|
|
|
1189
1238
|
BIOPointer getInfoAccess() const;
|
|
1190
1239
|
BIOPointer getValidFrom() const;
|
|
1191
1240
|
BIOPointer getValidTo() const;
|
|
1241
|
+
std::optional<std::string_view> getSignatureAlgorithm() const;
|
|
1242
|
+
std::optional<std::string> getSignatureAlgorithmOID() const;
|
|
1192
1243
|
int64_t getValidFromTime() const;
|
|
1193
1244
|
int64_t getValidToTime() const;
|
|
1194
1245
|
DataPointer getSerialNumber() const;
|
|
@@ -1407,6 +1458,15 @@ class EVPMDCtxPointer final {
|
|
|
1407
1458
|
std::optional<EVP_PKEY_CTX*> verifyInit(const EVPKeyPointer& key,
|
|
1408
1459
|
const Digest& digest);
|
|
1409
1460
|
|
|
1461
|
+
std::optional<EVP_PKEY_CTX*> signInitWithContext(
|
|
1462
|
+
const EVPKeyPointer& key,
|
|
1463
|
+
const Digest& digest,
|
|
1464
|
+
const Buffer<const unsigned char>& context_string);
|
|
1465
|
+
std::optional<EVP_PKEY_CTX*> verifyInitWithContext(
|
|
1466
|
+
const EVPKeyPointer& key,
|
|
1467
|
+
const Digest& digest,
|
|
1468
|
+
const Buffer<const unsigned char>& context_string);
|
|
1469
|
+
|
|
1410
1470
|
DataPointer signOneShot(const Buffer<const unsigned char>& buf) const;
|
|
1411
1471
|
DataPointer sign(const Buffer<const unsigned char>& buf) const;
|
|
1412
1472
|
bool verify(const Buffer<const unsigned char>& buf,
|
|
@@ -1451,6 +1511,56 @@ class HMACCtxPointer final {
|
|
|
1451
1511
|
DeleteFnPtr<HMAC_CTX, HMAC_CTX_free> ctx_;
|
|
1452
1512
|
};
|
|
1453
1513
|
|
|
1514
|
+
#if OPENSSL_VERSION_MAJOR >= 3
|
|
1515
|
+
class EVPMacPointer final {
|
|
1516
|
+
public:
|
|
1517
|
+
EVPMacPointer() = default;
|
|
1518
|
+
explicit EVPMacPointer(EVP_MAC* mac);
|
|
1519
|
+
EVPMacPointer(EVPMacPointer&& other) noexcept;
|
|
1520
|
+
EVPMacPointer& operator=(EVPMacPointer&& other) noexcept;
|
|
1521
|
+
NCRYPTO_DISALLOW_COPY(EVPMacPointer)
|
|
1522
|
+
~EVPMacPointer();
|
|
1523
|
+
|
|
1524
|
+
inline bool operator==(std::nullptr_t) noexcept { return mac_ == nullptr; }
|
|
1525
|
+
inline operator bool() const { return mac_ != nullptr; }
|
|
1526
|
+
inline EVP_MAC* get() const { return mac_.get(); }
|
|
1527
|
+
inline operator EVP_MAC*() const { return mac_.get(); }
|
|
1528
|
+
void reset(EVP_MAC* mac = nullptr);
|
|
1529
|
+
EVP_MAC* release();
|
|
1530
|
+
|
|
1531
|
+
static EVPMacPointer Fetch(const char* algorithm);
|
|
1532
|
+
|
|
1533
|
+
private:
|
|
1534
|
+
DeleteFnPtr<EVP_MAC, EVP_MAC_free> mac_;
|
|
1535
|
+
};
|
|
1536
|
+
|
|
1537
|
+
class EVPMacCtxPointer final {
|
|
1538
|
+
public:
|
|
1539
|
+
EVPMacCtxPointer() = default;
|
|
1540
|
+
explicit EVPMacCtxPointer(EVP_MAC_CTX* ctx);
|
|
1541
|
+
EVPMacCtxPointer(EVPMacCtxPointer&& other) noexcept;
|
|
1542
|
+
EVPMacCtxPointer& operator=(EVPMacCtxPointer&& other) noexcept;
|
|
1543
|
+
NCRYPTO_DISALLOW_COPY(EVPMacCtxPointer)
|
|
1544
|
+
~EVPMacCtxPointer();
|
|
1545
|
+
|
|
1546
|
+
inline bool operator==(std::nullptr_t) noexcept { return ctx_ == nullptr; }
|
|
1547
|
+
inline operator bool() const { return ctx_ != nullptr; }
|
|
1548
|
+
inline EVP_MAC_CTX* get() const { return ctx_.get(); }
|
|
1549
|
+
inline operator EVP_MAC_CTX*() const { return ctx_.get(); }
|
|
1550
|
+
void reset(EVP_MAC_CTX* ctx = nullptr);
|
|
1551
|
+
EVP_MAC_CTX* release();
|
|
1552
|
+
|
|
1553
|
+
bool init(const Buffer<const void>& key, const OSSL_PARAM* params = nullptr);
|
|
1554
|
+
bool update(const Buffer<const void>& data);
|
|
1555
|
+
DataPointer final(size_t length);
|
|
1556
|
+
|
|
1557
|
+
static EVPMacCtxPointer New(EVP_MAC* mac);
|
|
1558
|
+
|
|
1559
|
+
private:
|
|
1560
|
+
DeleteFnPtr<EVP_MAC_CTX, EVP_MAC_CTX_free> ctx_;
|
|
1561
|
+
};
|
|
1562
|
+
#endif // OPENSSL_VERSION_MAJOR >= 3
|
|
1563
|
+
|
|
1454
1564
|
#ifndef OPENSSL_NO_ENGINE
|
|
1455
1565
|
class EnginePointer final {
|
|
1456
1566
|
public:
|
|
@@ -1518,11 +1628,19 @@ bool SafeX509InfoAccessPrint(const BIOPointer& out, X509_EXTENSION* ext);
|
|
|
1518
1628
|
// ============================================================================
|
|
1519
1629
|
// SPKAC
|
|
1520
1630
|
|
|
1521
|
-
|
|
1522
|
-
|
|
1631
|
+
[[deprecated("Use the version that takes a Buffer")]] bool VerifySpkac(
|
|
1632
|
+
const char* input, size_t length);
|
|
1633
|
+
|
|
1634
|
+
[[deprecated("Use the version that takes a Buffer")]] BIOPointer
|
|
1635
|
+
ExportPublicKey(const char* input, size_t length);
|
|
1523
1636
|
|
|
1524
1637
|
// The caller takes ownership of the returned Buffer<char>
|
|
1525
|
-
|
|
1638
|
+
[[deprecated("Use the version that takes a Buffer")]] Buffer<char>
|
|
1639
|
+
ExportChallenge(const char* input, size_t length);
|
|
1640
|
+
|
|
1641
|
+
bool VerifySpkac(const Buffer<const char>& buf);
|
|
1642
|
+
BIOPointer ExportPublicKey(const Buffer<const char>& buf);
|
|
1643
|
+
DataPointer ExportChallenge(const Buffer<const char>& buf);
|
|
1526
1644
|
|
|
1527
1645
|
// ============================================================================
|
|
1528
1646
|
// KDF
|
|
@@ -1539,6 +1657,13 @@ bool extractP1363(const Buffer<const unsigned char>& buf,
|
|
|
1539
1657
|
unsigned char* dest,
|
|
1540
1658
|
size_t n);
|
|
1541
1659
|
|
|
1660
|
+
bool hkdfInfo(const Digest& md,
|
|
1661
|
+
const Buffer<const unsigned char>& key,
|
|
1662
|
+
const Buffer<const unsigned char>& info,
|
|
1663
|
+
const Buffer<const unsigned char>& salt,
|
|
1664
|
+
size_t length,
|
|
1665
|
+
Buffer<unsigned char>* out);
|
|
1666
|
+
|
|
1542
1667
|
DataPointer hkdf(const Digest& md,
|
|
1543
1668
|
const Buffer<const unsigned char>& key,
|
|
1544
1669
|
const Buffer<const unsigned char>& info,
|
|
@@ -1547,6 +1672,15 @@ DataPointer hkdf(const Digest& md,
|
|
|
1547
1672
|
|
|
1548
1673
|
bool checkScryptParams(uint64_t N, uint64_t r, uint64_t p, uint64_t maxmem);
|
|
1549
1674
|
|
|
1675
|
+
bool scryptInto(const Buffer<const char>& pass,
|
|
1676
|
+
const Buffer<const unsigned char>& salt,
|
|
1677
|
+
uint64_t N,
|
|
1678
|
+
uint64_t r,
|
|
1679
|
+
uint64_t p,
|
|
1680
|
+
uint64_t maxmem,
|
|
1681
|
+
size_t length,
|
|
1682
|
+
Buffer<unsigned char>* out);
|
|
1683
|
+
|
|
1550
1684
|
DataPointer scrypt(const Buffer<const char>& pass,
|
|
1551
1685
|
const Buffer<const unsigned char>& salt,
|
|
1552
1686
|
uint64_t N,
|
|
@@ -1555,6 +1689,13 @@ DataPointer scrypt(const Buffer<const char>& pass,
|
|
|
1555
1689
|
uint64_t maxmem,
|
|
1556
1690
|
size_t length);
|
|
1557
1691
|
|
|
1692
|
+
bool pbkdf2Into(const Digest& md,
|
|
1693
|
+
const Buffer<const char>& pass,
|
|
1694
|
+
const Buffer<const unsigned char>& salt,
|
|
1695
|
+
uint32_t iterations,
|
|
1696
|
+
size_t length,
|
|
1697
|
+
Buffer<unsigned char>* out);
|
|
1698
|
+
|
|
1558
1699
|
DataPointer pbkdf2(const Digest& md,
|
|
1559
1700
|
const Buffer<const char>& pass,
|
|
1560
1701
|
const Buffer<const unsigned char>& salt,
|
|
@@ -1612,6 +1753,137 @@ class KEM final {
|
|
|
1612
1753
|
|
|
1613
1754
|
#endif // OPENSSL_VERSION_MAJOR >= 3
|
|
1614
1755
|
|
|
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
|
+
|
|
1615
1887
|
// ============================================================================
|
|
1616
1888
|
// Version metadata
|
|
1617
1889
|
#define NCRYPTO_VERSION "0.0.1"
|