louise
The Louise language: compiler, runtime, std, tooling
Clone avec HTTPS, un jeton personnel en mot de passe, ou avec SSH, une clé ajoutée dans vos paramètres.
Dernier runRéussi#125 · main · 16 min 13 s
0Étoiles
0Forks
40Issues
| 1 | // std/crypto (SHA-256, HMAC-SHA-256, PBKDF2-HMAC-SHA-256 — issue #145's |
| 2 | // SCRAM — and Ed25519, ADR 0067) and std/base64 (RFC 4648), with their |
| 3 | // published vectors. |
| 4 | |
| 5 | import std/base64 |
| 6 | import std/bytes |
| 7 | import std/crypto |
| 8 | import std/int |
| 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 | } |
| 78 | |
| 79 | pub fn main :: -> () / <io> |
| 80 | { |
| 81 | // FIPS 180-4: SHA-256("abc"). |
| 82 | io.println (base64.encode (crypto.sha256 (bytes.from_string "abc"))) |
| 83 | // RFC 2202 case 2's key and text, HMAC-SHA-256. |
| 84 | io.println |
| 85 | ( base64.encode |
| 86 | ( crypto.hmac_sha256 |
| 87 | (bytes.from_string "Jefe") |
| 88 | (bytes.from_string "what do ya want for nothing?") |
| 89 | ) |
| 90 | ) |
| 91 | // PBKDF2-HMAC-SHA-256("password", "salt", 4096, 32), the well-known vector. |
| 92 | io.println |
| 93 | ( base64.encode |
| 94 | ( crypto.pbkdf2_hmac_sha256 |
| 95 | (bytes.from_string "password") |
| 96 | (bytes.from_string "salt") |
| 97 | 4096 |
| 98 | 32 |
| 99 | ) |
| 100 | ) |
| 101 | // RFC 4648's vectors, both ways. |
| 102 | io.println (base64.encode (bytes.from_string "foobar")) |
| 103 | io.println "{base64.decode "Zm9vYmFy":?}" |
| 104 | io.println "{base64.decode "Zm9vYmE=":?}" |
| 105 | io.println "{base64.decode "Zg==":?}" |
| 106 | io.println "{base64.decode "Zg=":?}" |
| 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:?}" |
| 144 | } |
| 145 | //> ungWv48Bz+pBQUDeXa4iI7ADYaOWF3qctBD/YfIAFa0= |
| 146 | //> W9zBRr9gdU5qBCQmCJV1x1oAPwidJzmDnexYuWTsOEM= |
| 147 | //> xeR41ZKIyEGqUw22hFxMjZYok6ABzk4RpJY4c6qYE0o= |
| 148 | //> Zm9vYmFy |
| 149 | //> Ok(b"foobar") |
| 150 | //> Ok(b"fooba") |
| 151 | //> Ok(b"f") |
| 152 | //> Error("base64: the length is not a multiple of 4") |
| 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] |