authorgravatar for 60517552+jakubDoka@users.noreply.github.comJakub Dóka <60517552+jakubDoka@users.noreply.github.com> 2024-08-07 10:06:15+02:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2024-08-07 01:06:15-07:00
loga6486492bed6ec840203cc88e0b30149dc72ad62
tree1fa230421f07d3264a9a88789c5686a5aa3f52c6
parentebd0c6ffd0eb91f7a2838d6a17bf5344d539d3c6
signaturebadge-check Signed by PGP key B5690EEEBB952194

std.crypto.ecdsa: use separate function for null seed (#20953)

Due to the `std.crypto.ecdsa.KeyPair.create` taking and optional of seed, even if the seed is generated, cross-compiling to the environments without standard random source (eg. wasm) (`std.crypto.random.bytes`) will fail to compile. This commit changes the API of the problematic function and moves the random seed generation to a new utility function.

1 files changed, 13 insertions(+), 13 deletions(-)

lib/std/crypto/ecdsa.zig+13-13
...@@ -289,18 +289,18 @@ pub fn Ecdsa(comptime Curve: type, comptime Hash: type) type {...@@ -289,18 +289,18 @@ pub fn Ecdsa(comptime Curve: type, comptime Hash: type) type {
289 /// Secret scalar.289 /// Secret scalar.
290 secret_key: SecretKey,290 secret_key: SecretKey,
291291
292 /// Create a new random key pair. `crypto.random.bytes` must be supported for the target.
293 pub fn generate() IdentityElementError!KeyPair {
294 var random_seed: [seed_length]u8 = undefined;
295 crypto.random.bytes(&random_seed);
296 return create(random_seed);
297 }
298
292 /// Create a new key pair. The seed must be secret and indistinguishable from random.299 /// Create a new key pair. The seed must be secret and indistinguishable from random.
293 /// The seed can also be left to null in order to generate a random key pair.300 pub fn create(seed: [seed_length]u8) IdentityElementError!KeyPair {
294 pub fn create(seed: ?[seed_length]u8) IdentityElementError!KeyPair {
295 var seed_ = seed;
296 if (seed_ == null) {
297 var random_seed: [seed_length]u8 = undefined;
298 crypto.random.bytes(&random_seed);
299 seed_ = random_seed;
300 }
301 const h = [_]u8{0x00} ** Hash.digest_length;301 const h = [_]u8{0x00} ** Hash.digest_length;
302 const k0 = [_]u8{0x01} ** SecretKey.encoded_length;302 const k0 = [_]u8{0x01} ** SecretKey.encoded_length;
303 const secret_key = deterministicScalar(h, k0, seed_).toBytes(.big);303 const secret_key = deterministicScalar(h, k0, seed).toBytes(.big);
304 return fromSecretKey(SecretKey{ .bytes = secret_key });304 return fromSecretKey(SecretKey{ .bytes = secret_key });
305 }305 }
306306
...@@ -380,7 +380,7 @@ test "Basic operations over EcdsaP384Sha384" {...@@ -380,7 +380,7 @@ test "Basic operations over EcdsaP384Sha384" {
380 if (builtin.zig_backend == .stage2_c) return error.SkipZigTest;380 if (builtin.zig_backend == .stage2_c) return error.SkipZigTest;
381381
382 const Scheme = EcdsaP384Sha384;382 const Scheme = EcdsaP384Sha384;
383 const kp = try Scheme.KeyPair.create(null);383 const kp = try Scheme.KeyPair.generate();
384 const msg = "test";384 const msg = "test";
385385
386 var noise: [Scheme.noise_length]u8 = undefined;386 var noise: [Scheme.noise_length]u8 = undefined;
...@@ -396,7 +396,7 @@ test "Basic operations over Secp256k1" {...@@ -396,7 +396,7 @@ test "Basic operations over Secp256k1" {
396 if (builtin.zig_backend == .stage2_c) return error.SkipZigTest;396 if (builtin.zig_backend == .stage2_c) return error.SkipZigTest;
397397
398 const Scheme = EcdsaSecp256k1Sha256oSha256;398 const Scheme = EcdsaSecp256k1Sha256oSha256;
399 const kp = try Scheme.KeyPair.create(null);399 const kp = try Scheme.KeyPair.generate();
400 const msg = "test";400 const msg = "test";
401401
402 var noise: [Scheme.noise_length]u8 = undefined;402 var noise: [Scheme.noise_length]u8 = undefined;
...@@ -412,7 +412,7 @@ test "Basic operations over EcdsaP384Sha256" {...@@ -412,7 +412,7 @@ test "Basic operations over EcdsaP384Sha256" {
412 if (builtin.zig_backend == .stage2_c) return error.SkipZigTest;412 if (builtin.zig_backend == .stage2_c) return error.SkipZigTest;
413413
414 const Scheme = Ecdsa(crypto.ecc.P384, crypto.hash.sha2.Sha256);414 const Scheme = Ecdsa(crypto.ecc.P384, crypto.hash.sha2.Sha256);
415 const kp = try Scheme.KeyPair.create(null);415 const kp = try Scheme.KeyPair.generate();
416 const msg = "test";416 const msg = "test";
417417
418 var noise: [Scheme.noise_length]u8 = undefined;418 var noise: [Scheme.noise_length]u8 = undefined;
...@@ -886,7 +886,7 @@ test "Sec1 encoding/decoding" {...@@ -886,7 +886,7 @@ test "Sec1 encoding/decoding" {
886 if (builtin.zig_backend == .stage2_c) return error.SkipZigTest;886 if (builtin.zig_backend == .stage2_c) return error.SkipZigTest;
887887
888 const Scheme = EcdsaP384Sha384;888 const Scheme = EcdsaP384Sha384;
889 const kp = try Scheme.KeyPair.create(null);889 const kp = try Scheme.KeyPair.generate();
890 const pk = kp.public_key;890 const pk = kp.public_key;
891 const pk_compressed_sec1 = pk.toCompressedSec1();891 const pk_compressed_sec1 = pk.toCompressedSec1();
892 const pk_recovered1 = try Scheme.PublicKey.fromSec1(&pk_compressed_sec1);892 const pk_recovered1 = try Scheme.PublicKey.fromSec1(&pk_compressed_sec1);