gingerBill eb261f5b28 Merge branch 'master' into new-sys-unix 1 year ago
..
_blake2 4c22982732 Rename files to not start with `_` 1 year ago
_fiat d72db2698b core/crypto/_fiat: Hedge against LLVM cleverness 2 years ago
_sha3 4c22982732 Rename files to not start with `_` 1 year ago
_tiger 4c22982732 Rename files to not start with `_` 1 year ago
blake 9ee4b76cd9 Just make the `io.Reader` etc aliases 2 years ago
blake2b 9ee4b76cd9 Just make the `io.Reader` etc aliases 2 years ago
blake2s 9ee4b76cd9 Just make the `io.Reader` etc aliases 2 years ago
chacha20 7fc2081543 core/crypto: Add private attributes for internals 2 years ago
chacha20poly1305 7fc2081543 core/crypto: Add private attributes for internals 2 years ago
gost 9ee4b76cd9 Just make the `io.Reader` etc aliases 2 years ago
groestl 9ee4b76cd9 Just make the `io.Reader` etc aliases 2 years ago
haval 9ee4b76cd9 Just make the `io.Reader` etc aliases 2 years ago
jh 9ee4b76cd9 Just make the `io.Reader` etc aliases 2 years ago
keccak 9ee4b76cd9 Just make the `io.Reader` etc aliases 2 years ago
md2 9ee4b76cd9 Just make the `io.Reader` etc aliases 2 years ago
md4 9ee4b76cd9 Just make the `io.Reader` etc aliases 2 years ago
md5 9ee4b76cd9 Just make the `io.Reader` etc aliases 2 years ago
poly1305 7fc2081543 core/crypto: Add private attributes for internals 2 years ago
ripemd 9ee4b76cd9 Just make the `io.Reader` etc aliases 2 years ago
sha1 9ee4b76cd9 Just make the `io.Reader` etc aliases 2 years ago
sha2 9ee4b76cd9 Just make the `io.Reader` etc aliases 2 years ago
sha3 9ee4b76cd9 Just make the `io.Reader` etc aliases 2 years ago
shake 9ee4b76cd9 Just make the `io.Reader` etc aliases 2 years ago
siphash dc8b7a0eb8 fix some typos 3 years ago
sm3 9ee4b76cd9 Just make the `io.Reader` etc aliases 2 years ago
streebog 9ee4b76cd9 Just make the `io.Reader` etc aliases 2 years ago
tiger 9ee4b76cd9 Just make the `io.Reader` etc aliases 2 years ago
tiger2 9ee4b76cd9 Just make the `io.Reader` etc aliases 2 years ago
util c59ad24856 Make tests scripts error if a test fails 2 years ago
whirlpool 9ee4b76cd9 Just make the `io.Reader` etc aliases 2 years ago
x25519 7fc2081543 core/crypto: Add private attributes for internals 2 years ago
README.md dc8b7a0eb8 fix some typos 3 years 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 crypto library for the Odin language

Supported

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

Hashing algorithms

Algorithm
BLAKE ✔️
BLAKE2B ✔️
BLAKE2S ✔️
GOST ✔️
Grøstl ✔️
HAVAL ✔️
JH ✔️
Keccak ✔️
MD2 ✔️
MD4 ✔️
MD5 ✔️
RIPEMD ✔️
SHA-1 ✔️
SHA-2 ✔️
SHA-3 ✔️
SHAKE ✔️
SM3 ✔️
Streebog ✔️
Tiger ✔️
Tiger2 ✔️
Whirlpool ✔️

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, HAVAL offers different sizes as well as three different round amounts.
Computing a 256-bit hash with 3 rounds is therefore achieved by calling haval.hash_256_3(...).

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/md4"

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

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

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

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

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

Thread safety

The crypto package is not thread-safe at the moment. This may change in the future.

Disclaimer

The algorithms were ported out of curiosity and due to interest in the field. We have not had any of the code verified by a third party or tested/fuzzed by any automatic means. Wherever we were able to find official test vectors, those were used to verify the implementation. We do not recommend using them in a production environment, without any additional testing and/or verification.

ToDo

  • Ciphers (Symmetric, Asymmetric)
  • MACs (Message Authentication Code)
  • CSPRNGs (Cryptographically Secure PseudoRandom Number Generator)
  • KDFs (Key Derivation Function)
  • KEAs (Key Exchange Algorithm)

License

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