// std/crypto (SHA-256, HMAC-SHA-256, PBKDF2-HMAC-SHA-256 — issue #145's // SCRAM — and Ed25519, ADR 0067) and std/base64 (RFC 4648), with their // published vectors. import std/base64 import std/bytes import std/crypto import std/int import std/io import std/list import std/string /// The bytes a lowercase hexadecimal text spells, two digits a byte. fn unhex :: text: String -> Bytes { bytes.from_list (byte_values (list.map (string.chars text) digit)) } fn byte_values :: digits: List -> List { match digits { [high, low, ..rest] -> [high * 16 + low, ..byte_values rest], _ -> [] } } fn digit :: c: String -> Int { match string.split "0123456789abcdef" c { [before, ..] -> string.length before, [] -> 0 } } /// `b` with the lowest bit of its byte `at` flipped. fn flipped :: b: Bytes at: Int -> Bytes { bytes.from_list ( list.zip_with (bytes.to_list b) (list.range 0 (bytes.length b)) fn byte i { if i == at { int.xor byte 1 } else { byte } } ) } /// An RFC 8032 §7.1 vector: the key derived from the seed, the signature /// made, and the verdicts on it and on its flipped bits. fn ed25519_vector :: name: String seed_hex: String public_hex: String message_hex: String signature_hex: String -> () / { let seed = unhex seed_hex let public = unhex public_hex let message = unhex message_hex let signature = unhex signature_hex let derived = crypto.ed25519_public_key seed == public let signed = crypto.ed25519_sign seed message == signature let verified = crypto.ed25519_verify public message signature let forged = [ crypto.ed25519_verify public message (flipped signature 0) , crypto.ed25519_verify public message (flipped signature 63) , crypto.ed25519_verify (flipped public 0) message signature , crypto.ed25519_verify public (bytes.concat message (unhex "00")) signature ] io.println "{name}: public key {derived}, signature {signed}, verified {verified}, forged {forged:?}" } pub fn main :: -> () / { // FIPS 180-4: SHA-256("abc"). io.println (base64.encode (crypto.sha256 (bytes.from_string "abc"))) // RFC 2202 case 2's key and text, HMAC-SHA-256. io.println ( base64.encode ( crypto.hmac_sha256 (bytes.from_string "Jefe") (bytes.from_string "what do ya want for nothing?") ) ) // PBKDF2-HMAC-SHA-256("password", "salt", 4096, 32), the well-known vector. io.println ( base64.encode ( crypto.pbkdf2_hmac_sha256 (bytes.from_string "password") (bytes.from_string "salt") 4096 32 ) ) // RFC 4648's vectors, both ways. io.println (base64.encode (bytes.from_string "foobar")) io.println "{base64.decode "Zm9vYmFy":?}" io.println "{base64.decode "Zm9vYmE=":?}" io.println "{base64.decode "Zg==":?}" io.println "{base64.decode "Zg=":?}" io.println "{base64.decode "Zm$v":?}" // RFC 8032 §7.1, TESTS 1 to 3. ed25519_vector "TEST 1" "9d61b19deffd5a60ba844af492ec2cc44449c5697b326919703bac031cae7f60" "d75a980182b10ab7d54bfed3c964073a0ee172f3daa62325af021a68f707511a" "" "e5564300c360ac729086e2cc806e828a84877f1eb8e5d974d873e065224901555fb8821590a33bacc61e39701cf9b46bd25bf5f0595bbe24655141438e7a100b" ed25519_vector "TEST 2" "4ccd089b28ff96da9db6c346ec114e0f5b8a319f35aba624da8cf6ed4fb8a6fb" "3d4017c3e843895a92b70aa74d1b7ebc9c982ccf2ec4968cc0cd55f12af4660c" "72" "92a009a9f0d4cab8720e820b5f642540a2b27b5416503f8fb3762223ebdb69da085ac1e43e15996e458f3613d0f11d8c387b2eaeb4302aeeb00d291612bb0c00" ed25519_vector "TEST 3" "c5aa8df43f9f837bedb7442f31dcb7b166d38535076f094b85ce3a2e0b4458f7" "fc51cd8e6218a1a38da47ed00230f0580816ed13ba3303ac5deb911548908025" "af82" "6291d657deec24024827e69c3abe01a30ce548a284743a445e3680d7db5ac3ac18ff9b538d16f290ae67f760984dc6594a7c15e9716ed28dc027beceea1ec40a" // A key that is not 32 bytes, or a signature that is not 64, is no // signature (TEST 3's, cut or grown by a byte). let public = unhex "fc51cd8e6218a1a38da47ed00230f0580816ed13ba3303ac5deb911548908025" let message = unhex "af82" let signature = unhex "6291d657deec24024827e69c3abe01a30ce548a284743a445e3680d7db5ac3ac18ff9b538d16f290ae67f760984dc6594a7c15e9716ed28dc027beceea1ec40a" let zero = unhex "00" let wrong_lengths = [ crypto.ed25519_verify (bytes.slice public 0 31) message signature , crypto.ed25519_verify (bytes.concat public zero) message signature , crypto.ed25519_verify public message (bytes.slice signature 0 63) , crypto.ed25519_verify public message (bytes.concat signature zero) , crypto.ed25519_verify (bytes.empty ()) message (bytes.empty ()) ] io.println "{wrong_lengths:?}" } //> ungWv48Bz+pBQUDeXa4iI7ADYaOWF3qctBD/YfIAFa0= //> W9zBRr9gdU5qBCQmCJV1x1oAPwidJzmDnexYuWTsOEM= //> xeR41ZKIyEGqUw22hFxMjZYok6ABzk4RpJY4c6qYE0o= //> Zm9vYmFy //> Ok(b"foobar") //> Ok(b"fooba") //> Ok(b"f") //> Error("base64: the length is not a multiple of 4") //> Error("base64: not a base64 character") //> TEST 1: public key True, signature True, verified True, forged [False, False, False, False] //> TEST 2: public key True, signature True, verified True, forged [False, False, False, False] //> TEST 3: public key True, signature True, verified True, forged [False, False, False, False] //> [False, False, False, False, False]