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 | // ChaCha20-Poly1305 (RFC 8439 §2.8.2) and HKDF-SHA-256 (RFC 5869, test |
| 2 | // cases 1 and 3) on both backends (ADR 0071). |
| 3 | |
| 4 | import std/bytes |
| 5 | import std/crypto |
| 6 | import std/io |
| 7 | import std/list |
| 8 | import std/string |
| 9 | |
| 10 | /// The bytes a lowercase hexadecimal text spells, two digits a byte. |
| 11 | fn unhex :: text: String -> Bytes |
| 12 | { |
| 13 | bytes.from_list (byte_values (list.map (string.chars text) digit)) |
| 14 | } |
| 15 | |
| 16 | fn byte_values :: digits: List<Int> -> List<Int> |
| 17 | { |
| 18 | match digits |
| 19 | { [high, low, ..rest] -> [high * 16 + low, ..byte_values rest], _ -> [] } |
| 20 | } |
| 21 | |
| 22 | fn digit :: c: String -> Int |
| 23 | { |
| 24 | match string.find "0123456789abcdef" c { Some(at) -> at, None -> 0 } |
| 25 | } |
| 26 | |
| 27 | pub fn main :: -> () / <io> |
| 28 | { |
| 29 | let key = bytes.from_list (list.range 128 159) |
| 30 | let nonce = unhex "070000004041424344454647" |
| 31 | let aad = unhex "50515253c0c1c2c3c4c5c6c7" |
| 32 | let plaintext = |
| 33 | bytes.from_string |
| 34 | "Ladies and Gentlemen of the class of '99: If I could offer you only one tip for the future, sunscreen would be it." |
| 35 | let expected = |
| 36 | unhex |
| 37 | "d31a8d34648e60db7b86afbc53ef7ec2a4aded51296e08fea9e2b5a736ee62d63dbea45e8ca9671282fafb69da92728b1a71de0a9e060b2905d6a5b67ecd3b3692ddbd7f2d778b8c9803aee328091b58fab324e4fad675945585808b4831d7bc3ff4def08e4b7a9de576d26586cec64b61161ae10b594f09e26a7e902ecbd0600691" |
| 38 | let sealed = crypto.chacha20_poly1305_seal key nonce plaintext aad |
| 39 | io.println "sealed {sealed == expected}" |
| 40 | io.println |
| 41 | "opened {crypto.chacha20_poly1305_open key nonce sealed aad == Ok(plaintext)}" |
| 42 | let other = bytes.from_string "other" |
| 43 | let refusals = |
| 44 | [ crypto.chacha20_poly1305_open key nonce sealed other |
| 45 | , crypto.chacha20_poly1305_open |
| 46 | (bytes.concat (bytes.slice key 1 32) (unhex "00")) |
| 47 | nonce |
| 48 | sealed |
| 49 | aad |
| 50 | , crypto.chacha20_poly1305_open |
| 51 | key |
| 52 | (unhex "000000000000000000000000") |
| 53 | sealed |
| 54 | aad |
| 55 | , crypto.chacha20_poly1305_open key nonce (bytes.slice sealed 1 129) aad |
| 56 | , crypto.chacha20_poly1305_open key nonce (bytes.empty ()) aad |
| 57 | ] |
| 58 | io.println |
| 59 | "{list.map refusals fn r { r == Error("crypto: the sealed message does not authenticate under this key, nonce and associated data") }:?}" |
| 60 | let empty = |
| 61 | crypto.chacha20_poly1305_seal key nonce (bytes.empty ()) (bytes.empty ()) |
| 62 | io.println |
| 63 | "{bytes.length empty} {crypto.chacha20_poly1305_open key nonce empty (bytes.empty ()) == Ok(bytes.empty ())}" |
| 64 | let secret = unhex "0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b" |
| 65 | let one = |
| 66 | crypto.hkdf_sha256 |
| 67 | secret |
| 68 | (unhex "000102030405060708090a0b0c") |
| 69 | (unhex "f0f1f2f3f4f5f6f7f8f9") |
| 70 | 42 |
| 71 | io.println |
| 72 | "hkdf 1 {one == unhex "3cb25f25faacd57a90434f64d0362f2a2d2d0a90cf1a5a4c5db02d56ecc4c5bf34007208d5b887185865"}" |
| 73 | let three = crypto.hkdf_sha256 secret (bytes.empty ()) (bytes.empty ()) 42 |
| 74 | io.println |
| 75 | "hkdf 3 {three == unhex "8da4e775a563c18f715f802a063c5a31b8a11f5c5ee1879ec3454e5f3c738d2d9d201395faa4b61a96c8"}" |
| 76 | io.println |
| 77 | "{bytes.length (crypto.hkdf_sha256 secret (bytes.empty ()) (bytes.empty ()) 8160)}" |
| 78 | } |
| 79 | //> sealed True |
| 80 | //> opened True |
| 81 | //> [True, True, True, True, True] |
| 82 | //> 16 True |
| 83 | //> hkdf 1 True |
| 84 | //> hkdf 3 True |
| 85 | //> 8160 |