mirror of
https://github.com/odin-lang/Odin.git
synced 2026-05-28 06:35:11 +00:00
core/crypto/ecdsa: Minor additions and cleanups
This commit is contained in:
@@ -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]
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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,
|
||||
|
||||
Reference in New Issue
Block a user