cromo La guida di Cromo
Scarica PDF EN

std.crypto

The building blocks of cryptography: hashes and HMAC, random bytes for keys, hex and base64, ciphers with authentication (AES-GCM and ChaCha20-Poly1305), signatures with Ed25519 and the exchange of keys with X25519. The bytes are lists of u8; a key or a nonce of the wrong size is an exception of type Error. The algorithms are those of mbedTLS and Monocypher, inside the runtime: the executables need no other library.

At the end are the parts that a protocol puts together by itself, as SSH does: chacha20, poly1305 and aesCtr, which alone protect nothing from who changes the data, and the signatures of RSA and of ECDSA with the keys given as numbers. A program that only wants to encrypt uses encrypt and decrypt.

cromo
list<u8> data = "hello".bytes()
println("{}", crypto.hex(crypto.sha256(data)))  // 2cf24dba5fb0a30e...

list<u8> key = crypto.random(32)
list<u8> nonce = crypto.random(12)
list<u8> sealed = crypto.encrypt(crypto.Cipher.ChaCha20Poly1305, key, nonce, data, list<u8>())
list<u8> opened = crypto.decrypt(crypto.Cipher.ChaCha20Poly1305, key, nonce, sealed, list<u8>())

The guide: Networking and HTTP.

Generata dalla documentazione nei sorgenti della libreria, che è in inglese.

#Tipi

#Hash

cromo
enum Hash

An algorithm of hash, for hmac and Hasher. MD5 and SHA-1 are broken: they are here for the formats that still use them.

  • Md5: MD5, of 16 bytes
  • Sha1: SHA-1, of 20 bytes
  • Sha256: SHA-256, of 32 bytes
  • Sha512: SHA-512, of 64 bytes
  • Sha384: SHA-384, of 48 bytes

#Hasher

cromo
struct Hasher

A hash computed in parts, for data that arrive in pieces, as a large file read a block at a time: the result is the same as that of all the data at once. It can be moved, not copied.

cromo
crypto.Hasher hasher = crypto.Hasher(crypto.Hash.Sha256)
hasher.add("hello ".bytes())
hasher.add("world".bytes())
string digest = crypto.hex(hasher.finish())  // as crypto.sha256 of "hello world"
cromo
Hasher(Hash algorithm)

A hasher of the algorithm, with no data yet.

#Hasher.add

cromo
void add(list<u8> data)

Adds the data to the hash; they are not kept.

#Hasher.finish

cromo
list<u8> finish()

The hash of what was added, of the size of the algorithm; then the hasher starts again, empty.

#Cipher

cromo
enum Cipher

A cipher with authentication, for encrypt and decrypt. Both take a nonce of 12 bytes, which must never be used twice with the same key.

  • AesGcm: AES in GCM mode, with a key of 16, 24 or 32 bytes
  • ChaCha20Poly1305: ChaCha20 with Poly1305, with a key of 32 bytes

#RsaKey

cromo
struct RsaKey

A key of RSA as its numbers, each big-endian: a public key has n and e, a private one also d, and p and q when they are known.

  • list<u8> n: the modulus
  • list<u8> e: the public exponent, as 65537
  • list<u8> d: the private exponent
  • list<u8> p: the first prime factor of n, or empty
  • list<u8> q: the second, or empty

#Curve

cromo
enum Curve

A curve of NIST, for ECDSA. The cases are in the order of the runtime.

  • P256: P-256, with numbers of 32 bytes
  • P384: P-384, with numbers of 48 bytes
  • P521: P-521, with numbers of 66 bytes

#Funzioni

#md5

cromo
list<u8> md5(list<u8> data)

The MD5 of the data, 16 bytes. It is broken: only for the formats that still use it.

#sha1

cromo
list<u8> sha1(list<u8> data)

The SHA-1 of the data, 20 bytes. It is broken: only for the formats that still use it, as WebSocket.

#sha256

cromo
list<u8> sha256(list<u8> data)

The SHA-256 of the data, 32 bytes: crypto.hex(crypto.sha256("hello".bytes())) gives "2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824".

#sha384

cromo
list<u8> sha384(list<u8> data)

The SHA-384 of the data, 48 bytes.

#sha512

cromo
list<u8> sha512(list<u8> data)

The SHA-512 of the data, 64 bytes.

#hmac

cromo
list<u8> hmac(Hash kind, list<u8> key, list<u8> data)

The HMAC of the data with the key (RFC 2104), of the size of the hash: it proves that who made it has the key. The key can have any size; one as long as the hash, from random, is the usual choice. To compare an HMAC received with the one computed, compare all the bytes, not only up to the first that differs.

#random

cromo
list<u8> random(i64 count)

count bytes from the generator of the system, good for keys and nonces: 32 for a key of ChaCha20-Poly1305 or Ed25519, 12 for a nonce. An exception if the system gives none; a negative count is a panic.

#sameBytes

cromo
bool sameBytes(list<u8> a, list<u8> b)

Whether two lists of bytes are the same, in a time that depends only on their length and not on where they differ: for what is secret, as a MAC, a signature, a token or the hash of a password. a == b stops at the first byte that is different, and who measures how long it takes can find a secret a byte at a time. Lists of different lengths are not the same, and that is known at once.

cromo
if (!crypto.sameBytes(crypto.hmac(crypto.Hash.Sha256, key, message), received)) {
    throw Error("the message was changed")
}

#hex

cromo
string hex(list<u8> bytes)

The bytes as text, with two lowercase hex digits for each byte: "00ff10".

#fromHex

cromo
list<u8> fromHex(string text)

The bytes of a text of hex digits, two for each byte, uppercase or lowercase, without spaces or "0x"; an empty text gives no bytes. An exception for an odd number of digits, or a character that is not a digit.

#base64

cromo
string base64(list<u8> bytes)

The bytes in base64 (RFC 4648), with the padding =: "aGVsbG8=" for "hello".

#fromBase64

cromo
list<u8> fromBase64(string text)

The bytes of a text in base64, with its padding =, and without spaces or new lines. An exception for a length or a character that is not valid.

#base64Url

cromo
string base64Url(list<u8> bytes)

The bytes in base64 for URLs and names of files (RFC 4648 §5): - and _ instead of + and /, and no padding.

#fromBase64Url

cromo
list<u8> fromBase64Url(string text)

The bytes of a text in base64 for URLs, with or without the padding. An exception for a length or a character that is not valid.

#base32

cromo
string base32(list<u8> bytes)

The bytes in base32 (RFC 4648), with the letters from A to Z, the digits from 2 to 7 and the padding =: "NBSWY3DP" for "hello". It is how the secrets of the one-time codes are written, since a person can read and type it.

#fromBase32

cromo
list<u8> fromBase32(string text)

The bytes of a text in base32, in upper or lower case, with or without the padding =; the spaces and the - that group the letters for a person are passed over. An exception for a character that is not of base32.

#encrypt

cromo
list<u8> encrypt(Cipher cipher, list<u8> key, list<u8> nonce, list<u8> data, list<u8> extra)

The data encrypted with the key and the nonce, followed by a tag of 16 bytes that authenticates them and the extra data. The extra data are not encrypted and not in the result, but decrypt needs the same ones, as a header sent in clear; often they are empty, list<u8>(). The nonce, of 12 bytes, must never be used twice with the same key: a new random(12) for each message is the simple way, and it goes with the message, in clear. An exception for a key or a nonce of the wrong size.

#decrypt

cromo
list<u8> decrypt(Cipher cipher, list<u8> key, list<u8> nonce, list<u8> data, list<u8> extra)

The data that encrypt was given, from its result with the tag, when they are authentic: the same cipher, key, nonce and extra data, and nothing changed. Otherwise an exception, which does not say what was wrong. An exception also for data shorter than the tag, and for a key or a nonce of the wrong size.

#ed25519PublicKey

cromo
list<u8> ed25519PublicKey(list<u8> privateKey)

The public key, of 32 bytes, of a private key of Ed25519, which is 32 random bytes, as random(32) gives (RFC 8032). An exception if the private key does not have 32 bytes.

cromo
list<u8> privateKey = crypto.random(32)
list<u8> publicKey = crypto.ed25519PublicKey(privateKey)
list<u8> signature = crypto.ed25519Sign(privateKey, message)
bool valid = crypto.ed25519Verify(publicKey, message, signature)  // true

#ed25519Sign

cromo
list<u8> ed25519Sign(list<u8> privateKey, list<u8> message)

The signature of the message with a private key of Ed25519, of 64 bytes; the same message and key always give the same signature. An exception if the private key does not have 32 bytes.

#ed25519Verify

cromo
bool ed25519Verify(list<u8> publicKey, list<u8> message, list<u8> signature)

Whether the signature is that of the message, made with the private key of the public key; false also when a size is wrong, never an exception.

#x25519PublicKey

cromo
list<u8> x25519PublicKey(list<u8> privateKey)

The public key, of 32 bytes, of a private key of X25519, which is 32 random bytes, as random(32) gives (RFC 7748): it is sent to the other side. An exception if the private key does not have 32 bytes.

#x25519

cromo
list<u8> x25519(list<u8> privateKey, list<u8> publicKey)

The secret of 32 bytes shared with the other side, from our private key and its public key: the other side computes the same from its private key and our public key. A key for a cipher is better made from it with a hash, as sha256. An exception if a key does not have 32 bytes, or the public key is not valid.

#chacha20

cromo
list<u8> chacha20(list<u8> key, list<u8> nonce, u32 counter, list<u8> data)

The data encrypted, or decrypted, with the stream of ChaCha20 (RFC 8439): a key of 32 bytes, a nonce of 12, and the number of the first block of 64 bytes of the stream. The same call that encrypts decrypts. Alone it does not say whether the data were changed: encrypt with Cipher.ChaCha20Poly1305 does, and is what a program wants unless a protocol asks for the cipher alone. An exception for a key or a nonce of the wrong size.

#poly1305

cromo
list<u8> poly1305(list<u8> key, list<u8> data)

The tag of Poly1305 of the data, 16 bytes, with a key of 32 bytes (RFC 8439): it proves that who made it has the key, which must be used for one message only. An exception if the key does not have 32 bytes.

#aesCtr

cromo
list<u8> aesCtr(list<u8> key, list<u8> counter, list<u8> data)

The data encrypted, or decrypted, with AES in CTR mode: a key of 16, 24 or 32 bytes, and a counter of 16 bytes, which is the first block and grows by one for each 16 bytes of data, as a big-endian number. The same call that encrypts decrypts; a counter must never be used twice with the same key. Alone it does not say whether the data were changed, as chacha20. An exception for a key or a counter of the wrong size.

#rsaVerify

cromo
bool rsaVerify(Hash hash, RsaKey key, list<u8> message, list<u8> signature)

Whether the signature is that of the message, made with the private key of n and e, with the padding of PKCS#1 v1.5 (RFC 8017) and the hash; false also when the key or a size is wrong, never an exception.

#rsaSign

cromo
list<u8> rsaSign(Hash hash, RsaKey key, list<u8> message)

The signature of the message with a private key of RSA, as long as its n, with the padding of PKCS#1 v1.5 and the hash. An exception when the numbers are not a private key.

#ecdsaPublicKey

cromo
list<u8> ecdsaPublicKey(Curve curve, list<u8> privateKey)

The public key of a private key of ECDSA: the point of the curve as SEC 1 writes it, a byte 4 and then x and y, 65 bytes for P-256. The private key is a number, big-endian, as long as the numbers of the curve. An exception when it is not a private key of the curve.

#ecdsaSign

cromo
list<u8> ecdsaSign(Curve curve, Hash hash, list<u8> privateKey, list<u8> message)

The signature of the message with a private key of ECDSA and the hash: its two numbers r and s side by side, each as long as the numbers of the curve, 64 bytes for P-256. Two signatures of the same message differ. An exception when the number is not a private key of the curve.

#ecdsaVerify

cromo
bool ecdsaVerify(Curve curve, Hash hash, list<u8> publicKey, list<u8> message, list<u8> signature)

Whether the signature, r and s side by side, is that of the message, made with the private key of the public key; false also when the key or a size is wrong, never an exception.