Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions src/PrivateKey.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -256,10 +256,9 @@ PublicKey PrivateKey::getPublicKey(TWPublicKeyType type) const {
}

int ecdsa_sign_digest_checked(const ecdsa_curve* curve, const uint8_t* priv_key, const uint8_t* digest, size_t digest_size, uint8_t* sig, uint8_t* pby, int (*is_canonical)(uint8_t by, uint8_t sig[64])) {
if (digest_size < 32) {
if (digest_size != 32) {
return -1;
}
assert(digest_size >= 32);
return ecdsa_sign_digest(curve, priv_key, digest, sig, pby, is_canonical);
}

Expand Down
24 changes: 23 additions & 1 deletion src/PublicKey.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

namespace TW {

bool validateSignatureLength(TWPublicKeyType type, const Data& signature) {
static bool validateSignatureLength(TWPublicKeyType type, const Data& signature) {
switch (type) {
case TWPublicKeyTypeSECP256k1:
case TWPublicKeyTypeSECP256k1Extended:
Expand All @@ -35,6 +35,25 @@ bool validateSignatureLength(TWPublicKeyType type, const Data& signature) {
}
}

static bool validateMessageLength(TWPublicKeyType type, const Data& message) {
switch (type) {
case TWPublicKeyTypeED25519:
case TWPublicKeyTypeCURVE25519:
case TWPublicKeyTypeED25519Blake2b:
case TWPublicKeyTypeED25519Cardano:
// Technically, we should allow any message size for ed25519.
return true;
case TWPublicKeyTypeSECP256k1:
case TWPublicKeyTypeNIST256p1:
case TWPublicKeyTypeSECP256k1Extended:
case TWPublicKeyTypeNIST256p1Extended:
case TWPublicKeyTypeStarkex:
return message.size() == PublicKey::ecdsaMessageSize;
default:
return false;
}
}

/// Determines if a collection of bytes makes a valid public key of the
/// given type.
bool PublicKey::isValid(const Data& data, enum TWPublicKeyType type) {
Expand Down Expand Up @@ -165,6 +184,9 @@ bool PublicKey::verify(const Data& signature, const Data& message) const {
if (!validateSignatureLength(type, signature)) {
return false;
}
if (!validateMessageLength(type, message)) {
return false;
}

switch (type) {
case TWPublicKeyTypeSECP256k1:
Expand Down
5 changes: 4 additions & 1 deletion src/PublicKey.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,12 @@ class PublicKey {
/// The number of bytes in a secp256k1 signature.
static const size_t secp256k1SignatureSize = 65;

/// Magic number used in V compnent encoding
/// Magic number used in V component encoding
static const byte SignatureVOffset = 27;

/// The exact number of bytes in a message that can be signed or verified.
static const size_t ecdsaMessageSize = 32;

/// The public key bytes.
Data bytes;

Expand Down
Loading