Yawning Angel 9cc5cd9d40 core/crypto: Update the documentation (NFC) 1 year ago
..
_blake2 aa821991b8 core/crypto/blake2: API cleanup and bug fixes 1 year ago
_fiat 1279ebe948 core/crypto/poly1305: Cleanups 1 year ago
_sha3 70ba4b5321 core/crypto: Add more assertions to the low level API 1 year ago
blake2b aa821991b8 core/crypto/blake2: API cleanup and bug fixes 1 year ago
blake2s aa821991b8 core/crypto/blake2: API cleanup and bug fixes 1 year ago
chacha20 fa1cb28c8f core/crypto/chacha20: Cleanups 1 year ago
chacha20poly1305 aa5a95a4d1 core/crypto/chacha20poly1305: Cleanups 1 year ago
legacy 59950bcad6 core/crypto: Exile keccak, md5 and sha1 to legacy 1 year ago
poly1305 1279ebe948 core/crypto/poly1305: Cleanups 1 year ago
sha2 92aad90c6b core/crypto/sha2: API cleanup 1 year ago
sha3 b8f9deb3d8 core/crypto/sha3: API cleanup 1 year ago
shake 7640fb0483 core/crypto/shake: API cleanup 1 year ago
siphash e86bb3a795 core/crypto: Change hash asserts to panics 1 year ago
sm3 4587a55486 core/crypto/sm3: API cleanup 1 year ago
x25519 7fc2081543 core/crypto: Add private attributes for internals 2 years ago
README.md 9cc5cd9d40 core/crypto: Update the documentation (NFC) 1 year ago
crypto.odin b8c2b0105b core/crypto: Disable optimization for the ct byte compare 2 years ago
rand_generic.odin 11a2b2a942 Add system_random and random_bytes for js target 1 year ago
rand_js.odin 49da19e013 Replace Math.random with crypto.getRandomValues for _system_number 1 year ago
rand_linux.odin 4d65b1ab9c Implement new sys/unix package 1 year ago
rand_openbsd.odin 5676c9e7eb initial OpenBSD support 3 years ago
rand_windows.odin 3b4199a669 Added rand_bytes for Windows in core:crypto 3 years ago

README.md

crypto

A cryptography library for the Odin language

Supported

This library offers various algorithms implemented in Odin. Please see the chart below for some of the options.

Hashing algorithms

Algorithm
BLAKE2B ✔️
BLAKE2S ✔️
SHA-2 ✔️
SHA-3 ✔️
SHAKE ✔️
SM3 ✔️
legacy/Keccak ✔️
legacy/MD5 ✔️
legacy/SHA-1 ✔️

High level API

Each hash algorithm contains a procedure group named hash, or if the algorithm provides more than one digest size hash_<size>*. Included in these groups are six procedures.

  • hash_string - Hash a given string and return the computed hash. Just calls hash_bytes internally
  • hash_bytes - Hash a given byte slice and return the computed hash
  • hash_string_to_buffer - Hash a given string and put the computed hash in the second proc parameter. Just calls hash_bytes_to_buffer internally
  • hash_bytes_to_buffer - Hash a given string and put the computed hash in the second proc parameter. The destination buffer has to be at least as big as the digest size of the hash
  • hash_stream - Takes a stream from io.Stream and returns the computed hash from it
  • hash_file - Takes a file handle and returns the computed hash from it. A second optional boolean parameter controls if the file is streamed (this is the default) or read at once (set to true)

* On some algorithms there is another part to the name, since they might offer control about additional parameters. For instance, SHA-2 offers different sizes. Computing a 512-bit hash is therefore achieved by calling sha2.hash_512(...).

Low level API

The above mentioned procedures internally call three procedures: init, update and final. You may also directly call them, if you wish.

Example

package crypto_example

// Import the desired package
import "core:crypto/blake2b"

main :: proc() {
    input := "foo"

    // Compute the hash, using the high level API
    computed_hash := blake2b.hash(input)

    // Variant that takes a destination buffer, instead of returning the computed hash
    hash := make([]byte, sha2.DIGEST_SIZE) // @note: Destination buffer has to be at least as big as the digest size of the hash
    blake2b.hash(input, hash[:])

    // Compute the hash, using the low level API
    ctx: blake2b.Context
    computed_hash_low: [blake2b.DIGEST_SIZE]byte
    blake2b.init(&ctx)
    blake2b.update(&ctx, transmute([]byte)input)
    blake2b.final(&ctx, computed_hash_low[:])
}

For example uses of all available algorithms, please see the tests within tests/core/crypto.

Implementation considerations

  • The crypto packages are not thread-safe.
  • Best-effort is make to mitigate timing side-channels on reasonable architectures. Architectures that are known to be unreasonable include but are not limited to i386, i486, and WebAssembly.
  • Some but not all of the packages attempt to santize sensitive data, however this is not done consistently through the library at the moment. As Thomas Pornin puts it "In general, such memory cleansing is a fool's quest."
  • All of these packages have not received independent third party review.

License

This library is made available under the BSD-3 license.