hare

[hare] The Hare programming language
git clone https://git.torresjrjr.com/hare.git
Log | Files | Refs | README | LICENSE

validate+test.ha (1622B)


      1 // SPDX-License-Identifier: MPL-2.0
      2 // (c) Hare authors <https://harelang.org>
      3 
      4 use bytes;
      5 use crypto::ec;
      6 use crypto::sha256;
      7 use hash;
      8 use memio;
      9 
     10 const randbuf: [_]u8 = [
     11 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
     12 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
     13 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff,
     14 	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
     15 	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
     16 	0xff, 0xff, 0xff, 0xff, 0x9f, 0x4e, 0x6c, 0xf9, 0x0f, 0xbc, 0xd7, 0xfa,
     17 	0x68, 0x33, 0x0d, 0x62, 0x04, 0xdd, 0x61, 0x1c, 0x00, 0xd9, 0x69, 0xfb,
     18 	0xa5, 0xcd, 0xb7, 0xa9, 0x9d, 0xca, 0x94, 0xfb, 0x50, 0x20, 0x5a, 0x6b,
     19 ];
     20 
     21 @test fn validate() void = {
     22 	let rnd = memio::fixed(randbuf);
     23 	let k = p256priv();
     24 	newkey(&k, &rnd)!;
     25 
     26 	assert(bytes::equal(randbuf[ec::P256_SCALARSZ * 2..], privkey_buf(&k)));
     27 	privkey_validate(&k)!;
     28 
     29 	let p = p256pub();
     30 	pubkey_derive(&p, &k);
     31 
     32 	pubkey_validate_format(&p)!;
     33 	pubkey_validate(&p)!;
     34 
     35 	let hashfn = sha256::sha256();
     36 	let hashbuf: [sha256::SZ * 2 + sha256::BLOCKSZ]u8 = [0...];
     37 
     38 	let msghash: [sha256::SZ]u8 = [0...];
     39 	hash::write(&hashfn, [0, 1, 2, 3]);
     40 	hash::sum(&hashfn, msghash);
     41 
     42 	let sig: [P256_SIGSZ]u8 = [0...];
     43 
     44 	assert(sign(&k, msghash, &hashfn, hashbuf, sig)! == len(sig));
     45 	verify(&p, msghash, sig)!;
     46 
     47 	const save = sig[4];
     48 	sig[4] = 0xff;
     49 	assert(verify(&p, msghash, sig) is invalidsig);
     50 	sig[4] = save;
     51 
     52 	pubkey_buf(&p)[1] = 0xff;
     53 	assert(verify(&p, msghash, sig) is invalidsig);
     54 	assert(pubkey_validate(&p) is invalidkey);
     55 };