From 675389aeabb148508d2deb069d551466611b64f9 Mon Sep 17 00:00:00 2001 From: Yawning Angel Date: Sat, 16 May 2026 17:02:51 +0900 Subject: [PATCH] core/crypto/ecdsa: Minor additions and cleanups --- core/crypto/ecdsa/ecdsa.odin | 27 ++++++++++++++++++++++++--- core/crypto/ecdsa/ecdsa_sign.odin | 8 ++++---- core/crypto/ecdsa/ecdsa_verify.odin | 8 ++++---- tests/core/crypto/wycheproof/ecc.odin | 2 +- 4 files changed, 33 insertions(+), 12 deletions(-) diff --git a/core/crypto/ecdsa/ecdsa.odin b/core/crypto/ecdsa/ecdsa.odin index 8bb1748cf..e63539fb1 100644 --- a/core/crypto/ecdsa/ecdsa.odin +++ b/core/crypto/ecdsa/ecdsa.odin @@ -2,7 +2,6 @@ package ecdsa import "core:crypto" import secec "core:crypto/_weierstrass" -import "core:mem" import "core:reflect" // Curve the curve identifier associated with a given Private_Key @@ -196,6 +195,12 @@ private_key_bytes :: proc(priv_key: ^Private_Key, dst: []byte) { } } +// private_key_public_bytes sets dst to the byte-encoding of the public +// key corresponding to priv_key. +private_key_public_bytes :: proc(priv_key: ^Private_Key, dst: []byte) { + public_key_bytes(&priv_key._pub_key, dst) +} + // private_key_set sets priv_key to src. private_key_set :: proc(priv_key, src: ^Private_Key) { if src == nil || src._curve == .Invalid { @@ -244,7 +249,7 @@ private_key_equal :: proc(p, q: ^Private_Key) -> bool { // private_key_clear clears priv_key to the uninitialized state. private_key_clear :: proc "contextless" (priv_key: ^Private_Key) { - mem.zero_explicit(priv_key, size_of(Private_Key)) + crypto.zero_explicit(priv_key, size_of(Private_Key)) } // public_key_set_bytes decodes a byte-encoded public key, and returns @@ -358,5 +363,21 @@ public_key_equal :: proc(p, q: ^Public_Key) -> bool { // public_key_clear clears pub_key to the uninitialized state. public_key_clear :: proc "contextless" (pub_key: ^Public_Key) { - mem.zero_explicit(pub_key, size_of(Public_Key)) + crypto.zero_explicit(pub_key, size_of(Public_Key)) +} + +// curve returns the Curve used by a Private_Key or Public_Key instance. +@(require_results) +curve :: proc(k: ^$T) -> Curve where (T == Private_Key || T == Public_Key) { + return k._curve +} + +// key_size returns the key size of a Private_Key or Public_Key in bytes. +@(require_results) +key_size :: proc(k: ^$T) -> int where (T == Private_Key || T == Public_Key) { + when T == Private_Key { + return PRIVATE_KEY_SIZES[k._curve] + } else { + return PUBLIC_KEY_SIZES[k._curve] + } } diff --git a/core/crypto/ecdsa/ecdsa_sign.odin b/core/crypto/ecdsa/ecdsa_sign.odin index c6fec56dc..a594bc601 100644 --- a/core/crypto/ecdsa/ecdsa_sign.odin +++ b/core/crypto/ecdsa/ecdsa_sign.odin @@ -13,8 +13,8 @@ import secec "core:crypto/_weierstrass" // The signature format is ASN1. `SEQUECE `{ r INTEGER, s INTEGER }`. @(require_results) sign_asn1 :: proc(priv_key: ^Private_Key, hash_algo: hash.Algorithm, msg: []byte, allocator: runtime.Allocator, deterministic := !crypto.HAS_RAND_BYTES) -> ([]byte, bool) { - ensure(hash_algo != .Invalid, "crypto/edsa: invalid hash algorithm") - ensure(priv_key._curve != .Invalid, "crypto/edsa: invalid curve") + ensure(hash_algo != .Invalid, "crypto/ecdsa: invalid hash algorithm") + ensure(priv_key._curve != .Invalid, "crypto/ecdsa: invalid curve") if !deterministic && !crypto.HAS_RAND_BYTES { return nil, false @@ -49,8 +49,8 @@ sign_asn1 :: proc(priv_key: ^Private_Key, hash_algo: hash.Algorithm, msg: []byte // The signature format is `r | s`. @(require_results) sign_raw :: proc(priv_key: ^Private_Key, hash_algo: hash.Algorithm, msg, sig: []byte, deterministic := !crypto.HAS_RAND_BYTES) -> bool { - ensure(hash_algo != .Invalid, "crypto/edsa: invalid hash algorithm") - ensure(priv_key._curve != .Invalid, "crypto/edsa: invalid curve") + ensure(hash_algo != .Invalid, "crypto/ecdsa: invalid hash algorithm") + ensure(priv_key._curve != .Invalid, "crypto/ecdsa: invalid curve") ensure(len(sig) == RAW_SIGNATURE_SIZES[priv_key._curve], "crypto/ecdsa: invalid destination size") if !deterministic && !crypto.HAS_RAND_BYTES { diff --git a/core/crypto/ecdsa/ecdsa_verify.odin b/core/crypto/ecdsa/ecdsa_verify.odin index bd973a8df..ddc1df9e6 100644 --- a/core/crypto/ecdsa/ecdsa_verify.odin +++ b/core/crypto/ecdsa/ecdsa_verify.odin @@ -10,8 +10,8 @@ import secec "core:crypto/_weierstrass" // The signature format is `r | s`. @(require_results) verify_raw :: proc(pub_key: ^Public_Key, hash_algo: hash.Algorithm, msg, sig: []byte) -> bool { - ensure(hash_algo != .Invalid, "crypto/edsa: invalid hash algorithm") - ensure(pub_key._curve != .Invalid, "crypto/edsa: invalid curve") + ensure(hash_algo != .Invalid, "crypto/ecdsa: invalid hash algorithm") + ensure(pub_key._curve != .Invalid, "crypto/ecdsa: invalid curve") if len(sig) != RAW_SIGNATURE_SIZES[pub_key._curve] { return false @@ -40,8 +40,8 @@ verify_raw :: proc(pub_key: ^Public_Key, hash_algo: hash.Algorithm, msg, sig: [] // The signature format is ASN.1 `SEQUENCE { r INTEGER, s INTEGER }`. @(require_results) verify_asn1 :: proc(pub_key: ^Public_Key, hash_algo: hash.Algorithm, msg, sig: []byte) -> bool { - ensure(hash_algo != .Invalid, "crypto/edsa: invalid hash algorithm") - ensure(pub_key._curve != .Invalid, "crypto/edsa: invalid curve") + ensure(hash_algo != .Invalid, "crypto/ecdsa: invalid hash algorithm") + ensure(pub_key._curve != .Invalid, "crypto/ecdsa: invalid curve") r_bytes, s_bytes, ok := parse_asn1_sig(sig) if !ok { diff --git a/tests/core/crypto/wycheproof/ecc.odin b/tests/core/crypto/wycheproof/ecc.odin index 53b63cc08..839f152bb 100644 --- a/tests/core/crypto/wycheproof/ecc.odin +++ b/tests/core/crypto/wycheproof/ecc.odin @@ -163,7 +163,7 @@ test_ecdsa_impl :: proc(t: ^testing.T, test_vectors: ^Test_Vectors(Ecdsa_Test_Gr if comment := test_vector.comment; comment != "" { log.debugf( - "ecda/%s/%s/%d: %s: %+v", + "ecdsa/%s/%s/%d: %s: %+v", curve_str, hash_str, test_vector.tc_id,