1919#include " Signing.hh"
2020#include " Error.hh"
2121#include " SecureRandomize.hh"
22+ #include " mbedUtils.hh"
2223#include " monocypher.h"
2324#include " monocypher-ed25519.h"
2425
26+ #pragma clang diagnostic push
27+ #pragma clang diagnostic ignored "-Wdocumentation-deprecated-sync"
28+ #include " mbedtls/pk.h"
29+ #pragma clang diagnostic pop
30+
2531namespace litecore ::crypto {
2632 using namespace std ;
2733
2834
35+ std::unique_ptr<SigningKey> SigningKey::generate (const char *algorithm) {
36+ if (0 == strcmp (algorithm, kRSAAlgorithmName )) {
37+ return make_unique<RSASigningKey>(PrivateKey::generateTemporaryRSA (2048 ));
38+ } else if (0 == strcmp (algorithm, kEd25519AlgorithmName )) {
39+ return make_unique<Ed25519SigningKey>();
40+ } else {
41+ error::_throw (error::CryptoError, " Unknown signature algorithm '%s'" , algorithm);
42+ }
43+ }
44+
45+
2946 unique_ptr<VerifyingKey> VerifyingKey::instantiate (slice data, const char *algorithm) {
3047 if (0 == strcmp (algorithm, kRSAAlgorithmName )) {
3148 return make_unique<RSAVerifyingKey>(data);
@@ -37,9 +54,54 @@ namespace litecore::crypto {
3754 }
3855
3956
57+ #pragma mark - RSA:
58+
59+
60+ static int rngFunction (void *ctx, unsigned char *dst, size_t size) {
61+ SecureRandomize ({dst, size});
62+ return 0 ;
63+ }
64+
65+
66+ alloc_slice RSASigningKey::sign (slice data) const {
67+ SHA256 inputDigest (data);
68+ alloc_slice signature (MBEDTLS_PK_SIGNATURE_MAX_SIZE);
69+ size_t sigLen = 0 ;
70+ TRY (mbedtls_pk_sign (_key->context (),
71+ MBEDTLS_MD_SHA256, // declares that input is a SHA256 digest.
72+ (const uint8_t *)inputDigest.asSlice ().buf , inputDigest.asSlice ().size ,
73+ (uint8_t *)signature.buf , &sigLen,
74+ rngFunction, nullptr ));
75+ signature.shorten (sigLen);
76+ return signature;
77+ }
78+
79+
80+ unique_ptr<VerifyingKey> RSASigningKey::verifyingKey () const {
81+ return make_unique<RSAVerifyingKey>(_key->publicKey ());
82+ }
83+
84+
85+ bool RSAVerifyingKey::verifySignature (slice data, slice signature) const {
86+ SHA256 inputDigest (data);
87+ int result = mbedtls_pk_verify (_key->context (),
88+ MBEDTLS_MD_SHA256, // declares that input is a SHA256 digest.
89+ (const uint8_t *)inputDigest.asSlice ().buf ,
90+ inputDigest.asSlice ().size ,
91+ (const uint8_t *)signature.buf , signature.size );
92+ if (result == MBEDTLS_ERR_RSA_VERIFY_FAILED)
93+ return false ;
94+ TRY (result); // other error codes throw exceptions
95+ return true ;
96+ }
97+
98+
99+ #pragma mark - Ed25519:
100+
101+
40102 Ed25519Base::Ed25519Base (slice bytes) {
41103 if (bytes.size != sizeof (_bytes))
42- error::_throw (error::CryptoError, " Invalid data for Ed25519 key" );
104+ error::_throw (error::CryptoError, " Invalid data size for Ed25519 key" );
43105 bytes.copyTo (_bytes.data ());
44106 }
45107
@@ -56,13 +118,20 @@ namespace litecore::crypto {
56118 }
57119
58120
121+ unique_ptr<VerifyingKey> Ed25519SigningKey::verifyingKey () const {
122+ unique_ptr<Ed25519VerifyingKey> pub (new Ed25519VerifyingKey ());
123+ crypto_ed25519_public_key (pub->_bytes .data (), _bytes.data ());
124+ return pub;
125+ }
126+
127+
59128 alloc_slice Ed25519SigningKey::verifyingKeyData () const {
60129 return publicKey ().data ();
61130 }
62131
63132
64133 alloc_slice Ed25519SigningKey::sign (slice data) const {
65- alloc_slice signature (64 );
134+ alloc_slice signature (kSignatureSize );
66135 crypto_ed25519_sign ((uint8_t *)signature.buf ,
67136 (const uint8_t *)_bytes.data (), nullptr ,
68137 (const uint8_t *)data.buf , data.size );
@@ -76,7 +145,8 @@ namespace litecore::crypto {
76145 }
77146
78147 bool Ed25519VerifyingKey::verifySignature (slice inputData, slice signature) const {
79- return 0 == crypto_ed25519_check ((const uint8_t *)signature.buf ,
148+ return signature.size == kSignatureSize
149+ && 0 == crypto_ed25519_check ((const uint8_t *)signature.buf ,
80150 (const uint8_t *)_bytes.data (),
81151 (const uint8_t *)inputData.buf ,
82152 inputData.size );
0 commit comments