| @@ -1,10 +1,80 @@ |
| 1 | 1 | // std/crypto (SHA-256, HMAC-SHA-256, PBKDF2-HMAC-SHA-256 — issue #145's |
| 2 | | −// SCRAM) and std/base64 (RFC 4648), with their published vectors. |
| 2 | +// SCRAM — and Ed25519, ADR 0067) and std/base64 (RFC 4648), with their |
| 3 | +// published vectors. |
| 3 | 4 | |
| 4 | 5 | import std/base64 |
| 5 | 6 | import std/bytes |
| 6 | 7 | import std/crypto |
| 8 | +import std/int |
| 7 | 9 | import std/io |
| 10 | +import std/list |
| 11 | +import std/string |
| 12 | + |
| 13 | +/// The bytes a lowercase hexadecimal text spells, two digits a byte. |
| 14 | +fn unhex :: text: String -> Bytes |
| 15 | +{ |
| 16 | + bytes.from_list (byte_values (list.map (string.chars text) digit)) |
| 17 | +} |
| 18 | + |
| 19 | +fn byte_values :: digits: List<Int> -> List<Int> |
| 20 | +{ |
| 21 | + match digits |
| 22 | + { [high, low, ..rest] -> [high * 16 + low, ..byte_values rest], _ -> [] } |
| 23 | +} |
| 24 | + |
| 25 | +fn digit :: c: String -> Int |
| 26 | +{ |
| 27 | + match string.split "0123456789abcdef" c |
| 28 | + { [before, ..] -> string.length before, [] -> 0 } |
| 29 | +} |
| 30 | + |
| 31 | +/// `b` with the lowest bit of its byte `at` flipped. |
| 32 | +fn flipped :: b: Bytes at: Int -> Bytes |
| 33 | +{ |
| 34 | + bytes.from_list |
| 35 | + ( list.zip_with |
| 36 | + (bytes.to_list b) |
| 37 | + (list.range 0 (bytes.length b)) |
| 38 | + fn byte i |
| 39 | + { |
| 40 | + if i == at |
| 41 | + { |
| 42 | + int.xor byte 1 |
| 43 | + } |
| 44 | + else |
| 45 | + { |
| 46 | + byte |
| 47 | + } |
| 48 | + } |
| 49 | + ) |
| 50 | +} |
| 51 | + |
| 52 | +/// An RFC 8032 §7.1 vector: the key derived from the seed, the signature |
| 53 | +/// made, and the verdicts on it and on its flipped bits. |
| 54 | +fn ed25519_vector :: |
| 55 | + name: String |
| 56 | + seed_hex: String |
| 57 | + public_hex: String |
| 58 | + message_hex: String |
| 59 | + signature_hex: String |
| 60 | + -> () / <io> |
| 61 | +{ |
| 62 | + let seed = unhex seed_hex |
| 63 | + let public = unhex public_hex |
| 64 | + let message = unhex message_hex |
| 65 | + let signature = unhex signature_hex |
| 66 | + let derived = crypto.ed25519_public_key seed == public |
| 67 | + let signed = crypto.ed25519_sign seed message == signature |
| 68 | + let verified = crypto.ed25519_verify public message signature |
| 69 | + let forged = |
| 70 | + [ crypto.ed25519_verify public message (flipped signature 0) |
| 71 | + , crypto.ed25519_verify public message (flipped signature 63) |
| 72 | + , crypto.ed25519_verify (flipped public 0) message signature |
| 73 | + , crypto.ed25519_verify public (bytes.concat message (unhex "00")) signature |
| 74 | + ] |
| 75 | + io.println |
| 76 | + "{name}: public key {derived}, signature {signed}, verified {verified}, forged {forged:?}" |
| 77 | +} |
| 8 | 78 | |
| 9 | 79 | pub fn main :: -> () / <io> |
| 10 | 80 | { |
| @@ -35,6 +105,42 @@ pub fn main :: -> () / <io> |
| 35 | 105 | io.println "{base64.decode "Zg==":?}" |
| 36 | 106 | io.println "{base64.decode "Zg=":?}" |
| 37 | 107 | io.println "{base64.decode "Zm$v":?}" |
| 108 | + // RFC 8032 §7.1, TESTS 1 to 3. |
| 109 | + ed25519_vector |
| 110 | + "TEST 1" |
| 111 | + "9d61b19deffd5a60ba844af492ec2cc44449c5697b326919703bac031cae7f60" |
| 112 | + "d75a980182b10ab7d54bfed3c964073a0ee172f3daa62325af021a68f707511a" |
| 113 | + "" |
| 114 | + "e5564300c360ac729086e2cc806e828a84877f1eb8e5d974d873e065224901555fb8821590a33bacc61e39701cf9b46bd25bf5f0595bbe24655141438e7a100b" |
| 115 | + ed25519_vector |
| 116 | + "TEST 2" |
| 117 | + "4ccd089b28ff96da9db6c346ec114e0f5b8a319f35aba624da8cf6ed4fb8a6fb" |
| 118 | + "3d4017c3e843895a92b70aa74d1b7ebc9c982ccf2ec4968cc0cd55f12af4660c" |
| 119 | + "72" |
| 120 | + "92a009a9f0d4cab8720e820b5f642540a2b27b5416503f8fb3762223ebdb69da085ac1e43e15996e458f3613d0f11d8c387b2eaeb4302aeeb00d291612bb0c00" |
| 121 | + ed25519_vector |
| 122 | + "TEST 3" |
| 123 | + "c5aa8df43f9f837bedb7442f31dcb7b166d38535076f094b85ce3a2e0b4458f7" |
| 124 | + "fc51cd8e6218a1a38da47ed00230f0580816ed13ba3303ac5deb911548908025" |
| 125 | + "af82" |
| 126 | + "6291d657deec24024827e69c3abe01a30ce548a284743a445e3680d7db5ac3ac18ff9b538d16f290ae67f760984dc6594a7c15e9716ed28dc027beceea1ec40a" |
| 127 | + // A key that is not 32 bytes, or a signature that is not 64, is no |
| 128 | + // signature (TEST 3's, cut or grown by a byte). |
| 129 | + let public = |
| 130 | + unhex "fc51cd8e6218a1a38da47ed00230f0580816ed13ba3303ac5deb911548908025" |
| 131 | + let message = unhex "af82" |
| 132 | + let signature = |
| 133 | + unhex |
| 134 | + "6291d657deec24024827e69c3abe01a30ce548a284743a445e3680d7db5ac3ac18ff9b538d16f290ae67f760984dc6594a7c15e9716ed28dc027beceea1ec40a" |
| 135 | + let zero = unhex "00" |
| 136 | + let wrong_lengths = |
| 137 | + [ crypto.ed25519_verify (bytes.slice public 0 31) message signature |
| 138 | + , crypto.ed25519_verify (bytes.concat public zero) message signature |
| 139 | + , crypto.ed25519_verify public message (bytes.slice signature 0 63) |
| 140 | + , crypto.ed25519_verify public message (bytes.concat signature zero) |
| 141 | + , crypto.ed25519_verify (bytes.empty ()) message (bytes.empty ()) |
| 142 | + ] |
| 143 | + io.println "{wrong_lengths:?}" |
| 38 | 144 | } |
| 39 | 145 | //> ungWv48Bz+pBQUDeXa4iI7ADYaOWF3qctBD/YfIAFa0= |
| 40 | 146 | //> W9zBRr9gdU5qBCQmCJV1x1oAPwidJzmDnexYuWTsOEM= |
| @@ -45,3 +151,7 @@ pub fn main :: -> () / <io> |
| 45 | 151 | //> Ok(b"f") |
| 46 | 152 | //> Error("base64: the length is not a multiple of 4") |
| 47 | 153 | //> Error("base64: not a base64 character") |
| 154 | +//> TEST 1: public key True, signature True, verified True, forged [False, False, False, False] |
| 155 | +//> TEST 2: public key True, signature True, verified True, forged [False, False, False, False] |
| 156 | +//> TEST 3: public key True, signature True, verified True, forged [False, False, False, False] |
| 157 | +//> [False, False, False, False, False] |