modus
Menu
On this page

The module cryptography

The cryptography of the system as one module of code: each export is a primitive of Odin's core:crypto that the system took (src/library/modus/cryptography, Cryptography), in the forms of the system.

The code of each export but argon2id is in the private part cryptography_primitives, under the same name and with the same signature: the QUIC peer of the tool and the host tests of QUIC call that code on the host, where no module is loaded. The export reads each argument once and passes it on.

  • The caller supplies the memory: each Text is bytes of the caller, each result is a buffer of the caller, so the module allocates nothing.
  • Each byte of the caller is read once, and each input that the code of the origin reads more than once is first copied into memory of the module, with a limit.
  • The result buffer must be private memory of the caller. The module says which arguments must be private.
  • A failure is an error record and a negative count, never a boolean that a caller can forget.
  • The copies of secrets that the module makes are erased, and so is the stack below each export after the work (erase_stack_below_the_caller).

A process says which cryptography it runs by the hash of this module: the hash names exactly the code that runs. The names of the algorithms stay as their standards write them.

cryptography module with no entry

Hash
b8cf5a11cd44879b8206a0fe93d67306358fd91ed566e304651d00a76e7e1568
Size
309369 bytes
Exports
15
Imports
0
Imported by
5
License
MIT

The cryptography of the system as one module of code: each export is a primitive of Odin's core:crypto that the system took (src/library/modus/cryptography), in the forms of the system

FactValue
KindModules with no entry
Sourcesrc/modules/libraries/cryptography
Boot imageyes: in the boot image, 309369 bytes in this build
Build settingsnone: the defaults of the source
Importsnone
Imported by

Neighbours

What it imports, at its left, and what imports it, at its right: the part of the module graph that touches it.

Exports

ExportWhat it does
seal_with_chacha20_poly1305(key: Text, nonce: Text, additional: Text, text: Text) -> Textencrypts a text and appends its tag of 16 bytes (RFC 8439)
open_with_chacha20_poly1305(key: Text, nonce: Text, additional: Text, sealed: Text) -> Textchecks the tag of a sealed text and decrypts it (RFC 8439)
seal_with_aes_128_gcm(key: Text, nonce: Text, additional: Text, text: Text) -> Textencrypts a text and appends its tag of 16 bytes (NIST SP 800-38D)
open_with_aes_128_gcm(key: Text, nonce: Text, additional: Text, sealed: Text) -> Textchecks the tag of a sealed text and decrypts it
sha256(text: Text) -> Textgives the SHA-256 hash of a text: 32 bytes (FIPS 180-4)
hmac_sha256(key: Text, text: Text) -> Textgives the HMAC of a text with a key, with SHA-256: 32 bytes (RFC 2104)
hkdf_sha256_extract(salt: Text, input: Text) -> Textgives the pseudorandom key of HKDF from a salt and input keying material, with SHA-256: 32 bytes (RFC 5869, section 2.2)
hkdf_sha256_expand(key: Text, info: Text, length: Integer) -> Textgives length bytes of output keying material from a pseudorandom key and an info text, with SHA-256 (RFC 5869, section 2.3)
header_protection_mask_with_aes_128(key: Text, sample: Text) -> Textgives the 5 bytes of the QUIC header protection mask of a sample of 16 bytes with a key of 16 bytes: AES-128 of the sample (RFC 9001, section 5.4.3)
header_protection_mask_with_chacha20(key: Text, sample: Text) -> Textgives the 5 bytes of the QUIC header protection mask of a sample of 16 bytes with a key of 32 bytes: the ChaCha20 keystream at the counter and the nonce that the sample gives (RFC 9001, section 5.4.4)
x25519(scalar: Text, point: Text) -> Textgives the shared secret of a scalar of 32 bytes and a point of 32 bytes (RFC 7748); with the point 9 it is the public key of the scalar
ed25519_public_key(secret: Text) -> Textgives the public key of a private key of 32 bytes (RFC 8032, section 5.1.5)
ed25519_sign(secret: Text, message: Text) -> Textgives the signature of a message of 4096 bytes at most, 64 bytes, with a private key of 32 bytes (RFC 8032, section 5.1.6)
ed25519_verify(public: Text, message: Text, signature: Text) -> Integerchecks the signature of a message of 4096 bytes at most with a public key of 32 bytes (RFC 8032, section 5.1.7)
argon2id(password: Text, salt: Text, cost: Argon2id_Cost, work: Bytes(Change)) -> Textderives a key from a password and a salt with Argon2id (RFC 9106), at the cost of the record: the KiB of the blocks, the passes, the lanes and the bytes of the key

seal_with_chacha20_poly1305

seal_with_chacha20_poly1305(key: , nonce: , additional: , text: ) -> 

seal_with_chacha20_poly1305 encrypts a text and appends its tag of 16 bytes (RFC 8439).

  • key: 32 bytes.
  • nonce: 12 bytes. A nonce must never come two times with one key: that is the duty of the caller.
  • additional: the additional data, which the tag covers and which is not encrypted: 2048 bytes at most.
  • text: the text to encrypt.
  • result: room for the bytes of the text and 16 more, in private memory of the caller.

Result: the count of bytes (the size that the result needs, when it is too small), or a negative number with an error record:

Parameters
  • key:
  • nonce:
  • additional:
  • text:
Result
When it fails
a negative result, and the error record
Source
src/modules/libraries/cryptography/cryptography.odin:52

open_with_chacha20_poly1305

open_with_chacha20_poly1305(key: , nonce: , additional: , sealed: ) -> 

open_with_chacha20_poly1305 checks the tag of a sealed text and decrypts it (RFC 8439).

The key, the nonce and the additional data are those of the seal (seal). The result must be private memory of the caller, with room for the text: the sealed text minus 16 bytes.

Result: the count of bytes, or a negative number with an error record:

  • Not_Authentic: the tag does not verify; the result keeps no byte of the text;
  • Too_Small: a sealed text under 16 bytes, or a result with no room;
  • Bad_Value: a result that overlaps an argument.
Parameters
  • key:
  • nonce:
  • additional:
  • sealed:
Result
When it fails
a negative result, and the error record
Source
src/modules/libraries/cryptography/cryptography.odin:67

seal_with_aes_128_gcm

seal_with_aes_128_gcm(key: , nonce: , additional: , text: ) -> 

seal_with_aes_128_gcm encrypts a text and appends its tag of 16 bytes (NIST SP 800-38D).

The key has 16 bytes, the nonce 12. A nonce must never come two times with one key: with AES-GCM a repeated nonce gives away the key of the tag, and with it forgeries. The other rules of the arguments and of the result are those of the seal with ChaCha20-Poly1305.

Parameters
  • key:
  • nonce:
  • additional:
  • text:
Result
When it fails
a negative result, and the error record
Source
src/modules/libraries/cryptography/cryptography.odin:77

open_with_aes_128_gcm

open_with_aes_128_gcm(key: , nonce: , additional: , sealed: ) -> 

open_with_aes_128_gcm checks the tag of a sealed text and decrypts it.

The rules of the arguments, of the result and of the errors are those of the open with ChaCha20-Poly1305.

Parameters
  • key:
  • nonce:
  • additional:
  • sealed:
Result
When it fails
a negative result, and the error record
Source
src/modules/libraries/cryptography/cryptography.odin:86

sha256

sha256(text: ) -> 

sha256 gives the SHA-256 hash of a text: 32 bytes (FIPS 180-4).

  • text: the bytes to hash, of any length.
  • result: a buffer of 32 bytes at least.

Result: 32. A buffer under 32 bytes receives nothing, and the result 32 says how much room the hash needs. It has no error record: each text has a hash. The stack below the export is erased after the work, because a text can be a secret (a transcript, a key).

Example, after load("cryptography"): sha256("abc") gives the 32 bytes that start with ba 78 16 bf.

Parameters
  • text:
Result
Source
src/modules/libraries/cryptography/cryptography.odin:101

hmac_sha256

hmac_sha256(key: , text: ) -> 

hmac_sha256 gives the HMAC of a text with a key, with SHA-256: 32 bytes (RFC 2104).

  • key: the key, of any length; a key longer than 64 bytes is hashed first, as the standard says.
  • text: the bytes to authenticate.
  • result: a buffer of 32 bytes at least.

Result: 32. A buffer under 32 bytes receives nothing. The tag is computed in memory of the module, then copied: the buffer of the caller never holds a part of the work. The copy of the tag is erased.

Parameters
  • key:
  • text:
Result
Source
src/modules/libraries/cryptography/cryptography.odin:114

hkdf_sha256_extract

hkdf_sha256_extract(salt: , input: ) -> 

hkdf_sha256_extract gives the pseudorandom key of HKDF from a salt and input keying material, with SHA-256: 32 bytes (RFC 5869, section 2.2).

Parameters
  • salt:
  • input:
Result
Source
src/modules/libraries/cryptography/cryptography.odin:120

hkdf_sha256_expand

hkdf_sha256_expand(key: , info: , length: ) -> 

hkdf_sha256_expand gives length bytes of output keying material from a pseudorandom key and an info text, with SHA-256 (RFC 5869, section 2.3).

  • The key: 32 bytes at least, as the extract gives it.
  • The info: 1024 bytes at most.
  • The length: 1 to 8160 bytes, 255 times the size of a hash.

Result: the count of bytes, or a negative number with an error record.

Parameters
  • key:
  • info:
  • length:
Result
When it fails
a negative result, and the error record
Source
src/modules/libraries/cryptography/cryptography.odin:132

header_protection_mask_with_aes_128

header_protection_mask_with_aes_128(key: , sample: ) -> 

header_protection_mask_with_aes_128 gives the 5 bytes of the QUIC header protection mask of a sample of 16 bytes with a key of 16 bytes: AES-128 of the sample (RFC 9001, section 5.4.3). The result is 5, or a negative number with an error record for a key or a sample of another size.

  • key: the header protection key of 16 bytes (argument 1).
  • sample: the 16 bytes of the protected packet that the standard names (argument 2).
  • result: a buffer of 5 bytes at least; a shorter buffer receives nothing, and the result is 5.

The errors: Empty, Too_Small or Too_Long for a key or a sample of another size, with the number of the argument. The copy of the key is erased. The QUIC library of the system calls it for each packet of a suite with AES.

Parameters
  • key:
  • sample:
Result
When it fails
a negative result, and the error record
Source
src/modules/libraries/cryptography/cryptography.odin:146

header_protection_mask_with_chacha20

header_protection_mask_with_chacha20(key: , sample: ) -> 

header_protection_mask_with_chacha20 gives the 5 bytes of the QUIC header protection mask of a sample of 16 bytes with a key of 32 bytes: the ChaCha20 keystream at the counter and the nonce that the sample gives (RFC 9001, section 5.4.4). The result is 5, or a negative number with an error record for a key or a sample of another size.

  • key: the header protection key of 32 bytes (argument 1).
  • sample: 16 bytes of the protected packet: the first 4 are the counter, the other 12 the nonce (argument 2).
  • result: a buffer of 5 bytes at least; a shorter buffer receives nothing, and the result is 5.

The errors are those of the mask with AES-128.

Parameters
  • key:
  • sample:
Result
When it fails
a negative result, and the error record
Source
src/modules/libraries/cryptography/cryptography.odin:158

x25519

x25519(scalar: , point: ) -> 

x25519 gives the shared secret of a scalar of 32 bytes and a point of 32 bytes (RFC 7748); with the point 9 it is the public key of the scalar.

A point of small order, whose shared secret is 32 zero bytes, is refused with Bad_Value (RFC 7748, section 6.1): such a secret is known to anyone.

Parameters
  • scalar:
  • point:
Result
When it fails
a negative result, and the error record
Source
src/modules/libraries/cryptography/cryptography.odin:167

ed25519_public_key

ed25519_public_key(secret: ) -> 

ed25519_public_key gives the public key of a private key of 32 bytes (RFC 8032, section 5.1.5).

Parameters
  • secret:
Result
When it fails
a negative result, and the error record
Source
src/modules/libraries/cryptography/cryptography.odin:173

ed25519_sign

ed25519_sign(secret: , message: ) -> 

ed25519_sign gives the signature of a message of 4096 bytes at most, 64 bytes, with a private key of 32 bytes (RFC 8032, section 5.1.6).

Parameters
  • secret:
  • message:
Result
When it fails
a negative result, and the error record
Source
src/modules/libraries/cryptography/cryptography.odin:179

ed25519_verify

ed25519_verify(public: , message: , signature: ) -> 

ed25519_verify checks the signature of a message of 4096 bytes at most with a public key of 32 bytes (RFC 8032, section 5.1.7).

The check is strict: a signature with a non-canonical S or R, or a public key of small order, is refused.

Result: 0, or a negative number with an error record:

Parameters
  • public:
  • message:
  • signature:
Result
When it fails
a negative result, and the error record
Source
src/modules/libraries/cryptography/cryptography.odin:192

argon2id

argon2id(password: , salt: , cost: , work: (Change)) -> 

argon2id derives a key from a password and a salt with Argon2id (RFC 9106), at the cost of the record: the KiB of the blocks, the passes, the lanes and the bytes of the key.

  1. 1. The password: 1024 bytes at most.
  2. 2. The salt: 8 to 64 bytes.
  3. 3. The cost: an Argon2id_Cost.
  4. 4. The work: the blocks, which the caller supplies: memory_kibibytes * 1024 bytes of its own memory, aligned on 8 bytes, which the module erases at the end.

Result: the count of the bytes of the key, or a negative number with an error record:

Parameters
  • password:
  • salt:
  • cost:
  • work: (Change)
Result
When it fails
a negative result, and the error record
Source
src/modules/libraries/cryptography/cryptography.odin:292

Read from the source of the module, as the build reads it to describe the exports (tools/modustool/build/describe.odin).

Text wire form

A wire form of the module contract: one word of a call. The address of a slice record {address, count} of UTF-8 bytes.

Error_Record record of a module

A record of the module cryptography: 56 bytes, aligned on 8 bytes, 0 bytes of padding.

  • kindi6408 bytes
  • uniti6488 bytes
  • operationi64168 bytes
  • argumenti64248 bytes
  • valuei64328 bytes
  • limiti64408 bytes
  • foundi64488 bytes

Integer wire form

A wire form of the module contract: one word of a call. A signed integer of 64 bits, in a register.

Argon2id_Cost record of a module

A record of the module cryptography: 32 bytes, aligned on 8 bytes, 0 bytes of padding.

  • memory_kibibytesi6408 bytes
  • passesi6488 bytes
  • lanesi64168 bytes
  • tag_bytesi64248 bytes

Bytes wire form

A wire form of the module contract: one word of a call. Bytes with no description.