diff --git a/.gitignore b/.gitignore index 74b926f..e6f42e5 100644 --- a/.gitignore +++ b/.gitignore @@ -19,6 +19,7 @@ docs.json __dummy.html docs/ +*-test-library # Code coverage *.lst diff --git a/LICENSE.txt b/LICENSE.txt new file mode 100644 index 0000000..62510f3 --- /dev/null +++ b/LICENSE.txt @@ -0,0 +1,18 @@ +/* + * ISC License + * + * Copyright (c) 2013-2019 + * Frank Denis + * + * Permission to use, copy, modify, and/or distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ diff --git a/README.md b/README.md index c5f5925..2a34be0 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,61 @@ -# libsodiumd -Simple D bindings for libsodium +## Libsodiumd: D bindings for libsodium + +Currently supported version: v1.0.17 (released 2019-01-07) + +Those bindings are simple translation from C to D. +They are simple, stupid, unnanotated - minimal modification has been applied +to make sure any new release does not lead to excessive work. + +## Usage / Documentation + +This wrapper provide a `package` file, just like `#include "sodium.h"`. +Just `import libsodium;` to get all available symbols. + +Some unittests are available in said package file. +For a more comprehensive documentation, refer directly to [libsodium's doc](https://libsodium.gitbook.io/doc/). + +## Binding generation + +The bindings were generated with the following procedure: +- Install [dstep](https://github.com/jacob-carlborg/dstep). +- Clone the [official repository](https://github.com/jedisct1/libsodium/). +- Checkout the required version +- Translate C headers to D modules: +```sh +find src/libsodium/include/sodium -depth 1 -name "*.h" | xargs -I F $DSTEP -Isrc/libsodium/include/sodium/ F -o $LIBSODIUMD/source/libsodium/$(basename F | cut -d'.' -f1).d +``` + With `$DSTEP` and `$LIBSODIUMD` being the dstep binary and path to this git repository, respectively. + +Then, a few manual adjustment were made: +- `mv source/libsodium/export.d source/libsodium/export_.d` as it conflicts with a D keyword +- Module documentation, module name, and standard import were added: +```sh +for file in $(find source/libsodium -name "*.d") +do +echo "\ +/******************************************************************************* + + D language bindings for libsodium's $(basename $file | cut -d'.' -f1).h + + License: ISC (see LICENSE.txt) + +*******************************************************************************/ + +module libsodium.$(basename $file | cut -d'.' -f1); + +@nogc nothrow: + +import libsodium.export_; +" | cat - $file > $file.tmp && mv $file.tmp $file +done +``` +- Turn `ULL` constants into `UL`: +```sh +for file in $(find source/libsodium -name "*.d") +do +sed -i '' -e 's/([[:digit:]])ULL/\1UL/g' $file +done +``` + +- Try to compile, add missing imports and fix `dstep` mishaps (e.g. some extra `_` are added) +- Generate `source/libsodium/package_.d` diff --git a/dub.json b/dub.json new file mode 100644 index 0000000..f56213c --- /dev/null +++ b/dub.json @@ -0,0 +1,9 @@ +{ + "name": "libsodiumd", + "description": "D binding for libsodium", + "license": "ISC", + "libs": [ "sodium" ], + "authors": [ + "Mathias Lang" + ] +} diff --git a/source/libsodium/core.d b/source/libsodium/core.d new file mode 100644 index 0000000..890ff69 --- /dev/null +++ b/source/libsodium/core.d @@ -0,0 +1,22 @@ +/******************************************************************************* + + D language bindings for libsodium's core.h + + License: ISC (see LICENSE.txt) + +*******************************************************************************/ + +module libsodium.core; + +@nogc nothrow: + +import libsodium.export_; +extern (C): + +int sodium_init (); + +/* ---- */ + +int sodium_set_misuse_handler (void function () handler); + +void sodium_misuse (); diff --git a/source/libsodium/crypto_aead_aes256gcm.d b/source/libsodium/crypto_aead_aes256gcm.d new file mode 100644 index 0000000..2e22cfa --- /dev/null +++ b/source/libsodium/crypto_aead_aes256gcm.d @@ -0,0 +1,160 @@ +/******************************************************************************* + + D language bindings for libsodium's crypto_aead_aes256gcm.h + + License: ISC (see LICENSE.txt) + +*******************************************************************************/ + +module libsodium.crypto_aead_aes256gcm; + +@nogc nothrow: + +import libsodium.export_; + +extern (C): + +/* + * WARNING: Despite being the most popular AEAD construction due to its + * use in TLS, safely using AES-GCM in a different context is tricky. + * + * No more than ~ 350 GB of input data should be encrypted with a given key. + * This is for ~ 16 KB messages -- Actual figures vary according to + * message sizes. + * + * In addition, nonces are short and repeated nonces would totally destroy + * the security of this scheme. + * + * Nonces should thus come from atomic counters, which can be difficult to + * set up in a distributed environment. + * + * Unless you absolutely need AES-GCM, use crypto_aead_xchacha20poly1305_ietf_*() + * instead. It doesn't have any of these limitations. + * Or, if you don't need to authenticate additional data, just stick to + * crypto_secretbox(). + */ + +int crypto_aead_aes256gcm_is_available (); + +enum crypto_aead_aes256gcm_KEYBYTES = 32U; +size_t crypto_aead_aes256gcm_keybytes (); + +enum crypto_aead_aes256gcm_NSECBYTES = 0U; +size_t crypto_aead_aes256gcm_nsecbytes (); + +enum crypto_aead_aes256gcm_NPUBBYTES = 12U; +size_t crypto_aead_aes256gcm_npubbytes (); + +enum crypto_aead_aes256gcm_ABYTES = 16U; +size_t crypto_aead_aes256gcm_abytes (); + +enum crypto_aead_aes256gcm_MESSAGEBYTES_MAX = + SODIUM_MIN(SODIUM_SIZE_MAX - crypto_aead_aes256gcm_ABYTES, 16UL * ((1UL << 32) - 2UL)); +size_t crypto_aead_aes256gcm_messagebytes_max (); + +struct crypto_aead_aes256gcm_state_ +{ + ubyte[512] opaque; +} + +alias crypto_aead_aes256gcm_state = crypto_aead_aes256gcm_state_; + +size_t crypto_aead_aes256gcm_statebytes (); + +int crypto_aead_aes256gcm_encrypt ( + ubyte* c, + ulong* clen_p, + const(ubyte)* m, + ulong mlen, + const(ubyte)* ad, + ulong adlen, + const(ubyte)* nsec, + const(ubyte)* npub, + const(ubyte)* k); + +int crypto_aead_aes256gcm_decrypt ( + ubyte* m, + ulong* mlen_p, + ubyte* nsec, + const(ubyte)* c, + ulong clen, + const(ubyte)* ad, + ulong adlen, + const(ubyte)* npub, + const(ubyte)* k); + +int crypto_aead_aes256gcm_encrypt_detached ( + ubyte* c, + ubyte* mac, + ulong* maclen_p, + const(ubyte)* m, + ulong mlen, + const(ubyte)* ad, + ulong adlen, + const(ubyte)* nsec, + const(ubyte)* npub, + const(ubyte)* k); + +int crypto_aead_aes256gcm_decrypt_detached ( + ubyte* m, + ubyte* nsec, + const(ubyte)* c, + ulong clen, + const(ubyte)* mac, + const(ubyte)* ad, + ulong adlen, + const(ubyte)* npub, + const(ubyte)* k); + +/* -- Precomputation interface -- */ + +int crypto_aead_aes256gcm_beforenm ( + crypto_aead_aes256gcm_state* ctx_, + const(ubyte)* k); + +int crypto_aead_aes256gcm_encrypt_afternm ( + ubyte* c, + ulong* clen_p, + const(ubyte)* m, + ulong mlen, + const(ubyte)* ad, + ulong adlen, + const(ubyte)* nsec, + const(ubyte)* npub, + const(crypto_aead_aes256gcm_state)* ctx_); + +int crypto_aead_aes256gcm_decrypt_afternm ( + ubyte* m, + ulong* mlen_p, + ubyte* nsec, + const(ubyte)* c, + ulong clen, + const(ubyte)* ad, + ulong adlen, + const(ubyte)* npub, + const(crypto_aead_aes256gcm_state)* ctx_); + +int crypto_aead_aes256gcm_encrypt_detached_afternm ( + ubyte* c, + ubyte* mac, + ulong* maclen_p, + const(ubyte)* m, + ulong mlen, + const(ubyte)* ad, + ulong adlen, + const(ubyte)* nsec, + const(ubyte)* npub, + const(crypto_aead_aes256gcm_state)* ctx_); + +int crypto_aead_aes256gcm_decrypt_detached_afternm ( + ubyte* m, + ubyte* nsec, + const(ubyte)* c, + ulong clen, + const(ubyte)* mac, + const(ubyte)* ad, + ulong adlen, + const(ubyte)* npub, + const(crypto_aead_aes256gcm_state)* ctx_); + +void crypto_aead_aes256gcm_keygen (ref ubyte[crypto_aead_aes256gcm_KEYBYTES] k); diff --git a/source/libsodium/crypto_aead_chacha20poly1305.d b/source/libsodium/crypto_aead_chacha20poly1305.d new file mode 100644 index 0000000..3f29566 --- /dev/null +++ b/source/libsodium/crypto_aead_chacha20poly1305.d @@ -0,0 +1,154 @@ +/******************************************************************************* + + D language bindings for libsodium's crypto_aead_chacha20poly1305.h + + License: ISC (see LICENSE.txt) + +*******************************************************************************/ + +module libsodium.crypto_aead_chacha20poly1305; + +@nogc nothrow: + +import libsodium.export_; + +extern (C): + +/* -- IETF ChaCha20-Poly1305 construction with a 96-bit nonce and a 32-bit internal counter -- */ + +enum crypto_aead_chacha20poly1305_ietf_KEYBYTES = 32U; +size_t crypto_aead_chacha20poly1305_ietf_keybytes (); + +enum crypto_aead_chacha20poly1305_ietf_NSECBYTES = 0U; +size_t crypto_aead_chacha20poly1305_ietf_nsecbytes (); + +enum crypto_aead_chacha20poly1305_ietf_NPUBBYTES = 12U; + +size_t crypto_aead_chacha20poly1305_ietf_npubbytes (); + +enum crypto_aead_chacha20poly1305_ietf_ABYTES = 16U; +size_t crypto_aead_chacha20poly1305_ietf_abytes (); + +enum crypto_aead_chacha20poly1305_ietf_MESSAGEBYTES_MAX = SODIUM_MIN(SODIUM_SIZE_MAX - crypto_aead_chacha20poly1305_ietf_ABYTES, 64UL * ((1UL << 32) - 1UL)); +size_t crypto_aead_chacha20poly1305_ietf_messagebytes_max (); + +int crypto_aead_chacha20poly1305_ietf_encrypt ( + ubyte* c, + ulong* clen_p, + const(ubyte)* m, + ulong mlen, + const(ubyte)* ad, + ulong adlen, + const(ubyte)* nsec, + const(ubyte)* npub, + const(ubyte)* k); + +int crypto_aead_chacha20poly1305_ietf_decrypt ( + ubyte* m, + ulong* mlen_p, + ubyte* nsec, + const(ubyte)* c, + ulong clen, + const(ubyte)* ad, + ulong adlen, + const(ubyte)* npub, + const(ubyte)* k); + +int crypto_aead_chacha20poly1305_ietf_encrypt_detached ( + ubyte* c, + ubyte* mac, + ulong* maclen_p, + const(ubyte)* m, + ulong mlen, + const(ubyte)* ad, + ulong adlen, + const(ubyte)* nsec, + const(ubyte)* npub, + const(ubyte)* k); + +int crypto_aead_chacha20poly1305_ietf_decrypt_detached ( + ubyte* m, + ubyte* nsec, + const(ubyte)* c, + ulong clen, + const(ubyte)* mac, + const(ubyte)* ad, + ulong adlen, + const(ubyte)* npub, + const(ubyte)* k); + +void crypto_aead_chacha20poly1305_ietf_keygen ( + ref ubyte[crypto_aead_chacha20poly1305_ietf_KEYBYTES] k); + +/* -- Original ChaCha20-Poly1305 construction with a 64-bit nonce and a 64-bit internal counter -- */ + +enum crypto_aead_chacha20poly1305_KEYBYTES = 32U; +size_t crypto_aead_chacha20poly1305_keybytes (); + +enum crypto_aead_chacha20poly1305_NSECBYTES = 0U; +size_t crypto_aead_chacha20poly1305_nsecbytes (); + +enum crypto_aead_chacha20poly1305_NPUBBYTES = 8U; +size_t crypto_aead_chacha20poly1305_npubbytes (); + +enum crypto_aead_chacha20poly1305_ABYTES = 16U; +size_t crypto_aead_chacha20poly1305_abytes (); + +enum crypto_aead_chacha20poly1305_MESSAGEBYTES_MAX = SODIUM_SIZE_MAX - crypto_aead_chacha20poly1305_ABYTES; +size_t crypto_aead_chacha20poly1305_messagebytes_max (); + +int crypto_aead_chacha20poly1305_encrypt ( + ubyte* c, + ulong* clen_p, + const(ubyte)* m, + ulong mlen, + const(ubyte)* ad, + ulong adlen, + const(ubyte)* nsec, + const(ubyte)* npub, + const(ubyte)* k); + +int crypto_aead_chacha20poly1305_decrypt ( + ubyte* m, + ulong* mlen_p, + ubyte* nsec, + const(ubyte)* c, + ulong clen, + const(ubyte)* ad, + ulong adlen, + const(ubyte)* npub, + const(ubyte)* k); + +int crypto_aead_chacha20poly1305_encrypt_detached ( + ubyte* c, + ubyte* mac, + ulong* maclen_p, + const(ubyte)* m, + ulong mlen, + const(ubyte)* ad, + ulong adlen, + const(ubyte)* nsec, + const(ubyte)* npub, + const(ubyte)* k); + +int crypto_aead_chacha20poly1305_decrypt_detached ( + ubyte* m, + ubyte* nsec, + const(ubyte)* c, + ulong clen, + const(ubyte)* mac, + const(ubyte)* ad, + ulong adlen, + const(ubyte)* npub, + const(ubyte)* k); + +void crypto_aead_chacha20poly1305_keygen ( + ref ubyte[crypto_aead_chacha20poly1305_KEYBYTES] k); + +/* Aliases */ + +enum crypto_aead_chacha20poly1305_IETF_KEYBYTES = crypto_aead_chacha20poly1305_ietf_KEYBYTES; +enum crypto_aead_chacha20poly1305_IETF_NSECBYTES = crypto_aead_chacha20poly1305_ietf_NSECBYTES; +enum crypto_aead_chacha20poly1305_IETF_NPUBBYTES = crypto_aead_chacha20poly1305_ietf_NPUBBYTES; +enum crypto_aead_chacha20poly1305_IETF_ABYTES = crypto_aead_chacha20poly1305_ietf_ABYTES; +enum crypto_aead_chacha20poly1305_IETF_MESSAGEBYTES_MAX = crypto_aead_chacha20poly1305_ietf_MESSAGEBYTES_MAX; diff --git a/source/libsodium/crypto_aead_xchacha20poly1305.d b/source/libsodium/crypto_aead_xchacha20poly1305.d new file mode 100644 index 0000000..d5c7d15 --- /dev/null +++ b/source/libsodium/crypto_aead_xchacha20poly1305.d @@ -0,0 +1,86 @@ +/******************************************************************************* + + D language bindings for libsodium's crypto_aead_xchacha20poly1305.h + + License: ISC (see LICENSE.txt) + +*******************************************************************************/ + +module libsodium.crypto_aead_xchacha20poly1305; + +@nogc nothrow: + +import libsodium.export_; + +extern (C): + +enum crypto_aead_xchacha20poly1305_ietf_KEYBYTES = 32U; +size_t crypto_aead_xchacha20poly1305_ietf_keybytes (); + +enum crypto_aead_xchacha20poly1305_ietf_NSECBYTES = 0U; +size_t crypto_aead_xchacha20poly1305_ietf_nsecbytes (); + +enum crypto_aead_xchacha20poly1305_ietf_NPUBBYTES = 24U; +size_t crypto_aead_xchacha20poly1305_ietf_npubbytes (); + +enum crypto_aead_xchacha20poly1305_ietf_ABYTES = 16U; +size_t crypto_aead_xchacha20poly1305_ietf_abytes (); + +enum crypto_aead_xchacha20poly1305_ietf_MESSAGEBYTES_MAX = SODIUM_SIZE_MAX - crypto_aead_xchacha20poly1305_ietf_ABYTES; +size_t crypto_aead_xchacha20poly1305_ietf_messagebytes_max (); + +int crypto_aead_xchacha20poly1305_ietf_encrypt ( + ubyte* c, + ulong* clen_p, + const(ubyte)* m, + ulong mlen, + const(ubyte)* ad, + ulong adlen, + const(ubyte)* nsec, + const(ubyte)* npub, + const(ubyte)* k); + +int crypto_aead_xchacha20poly1305_ietf_decrypt ( + ubyte* m, + ulong* mlen_p, + ubyte* nsec, + const(ubyte)* c, + ulong clen, + const(ubyte)* ad, + ulong adlen, + const(ubyte)* npub, + const(ubyte)* k); + +int crypto_aead_xchacha20poly1305_ietf_encrypt_detached ( + ubyte* c, + ubyte* mac, + ulong* maclen_p, + const(ubyte)* m, + ulong mlen, + const(ubyte)* ad, + ulong adlen, + const(ubyte)* nsec, + const(ubyte)* npub, + const(ubyte)* k); + +int crypto_aead_xchacha20poly1305_ietf_decrypt_detached ( + ubyte* m, + ubyte* nsec, + const(ubyte)* c, + ulong clen, + const(ubyte)* mac, + const(ubyte)* ad, + ulong adlen, + const(ubyte)* npub, + const(ubyte)* k); + +void crypto_aead_xchacha20poly1305_ietf_keygen ( + ref ubyte[crypto_aead_xchacha20poly1305_ietf_KEYBYTES] k); + +/* Aliases */ + +enum crypto_aead_xchacha20poly1305_IETF_KEYBYTES = crypto_aead_xchacha20poly1305_ietf_KEYBYTES; +enum crypto_aead_xchacha20poly1305_IETF_NSECBYTES = crypto_aead_xchacha20poly1305_ietf_NSECBYTES; +enum crypto_aead_xchacha20poly1305_IETF_NPUBBYTES = crypto_aead_xchacha20poly1305_ietf_NPUBBYTES; +enum crypto_aead_xchacha20poly1305_IETF_ABYTES = crypto_aead_xchacha20poly1305_ietf_ABYTES; +enum crypto_aead_xchacha20poly1305_IETF_MESSAGEBYTES_MAX = crypto_aead_xchacha20poly1305_ietf_MESSAGEBYTES_MAX; diff --git a/source/libsodium/crypto_auth.d b/source/libsodium/crypto_auth.d new file mode 100644 index 0000000..5be16e8 --- /dev/null +++ b/source/libsodium/crypto_auth.d @@ -0,0 +1,35 @@ +/******************************************************************************* + + D language bindings for libsodium's crypto_auth.h + + License: ISC (see LICENSE.txt) + +*******************************************************************************/ + +module libsodium.crypto_auth; + +@nogc nothrow: + +import libsodium.export_; +import libsodium.crypto_auth_hmacsha512256; + +extern (C): + +enum crypto_auth_BYTES = crypto_auth_hmacsha512256_BYTES; +size_t crypto_auth_bytes (); + +enum crypto_auth_KEYBYTES = crypto_auth_hmacsha512256_KEYBYTES; +size_t crypto_auth_keybytes (); + +enum crypto_auth_PRIMITIVE = "hmacsha512256"; +const(char)* crypto_auth_primitive (); + +int crypto_auth (ubyte* out_, const(ubyte)* in_, ulong inlen, const(ubyte)* k); + +int crypto_auth_verify ( + const(ubyte)* h, + const(ubyte)* in_, + ulong inlen, + const(ubyte)* k); + +void crypto_auth_keygen (ref ubyte[crypto_auth_KEYBYTES] k); diff --git a/source/libsodium/crypto_auth_hmacsha256.d b/source/libsodium/crypto_auth_hmacsha256.d new file mode 100644 index 0000000..72acba9 --- /dev/null +++ b/source/libsodium/crypto_auth_hmacsha256.d @@ -0,0 +1,61 @@ +/******************************************************************************* + + D language bindings for libsodium's crypto_auth_hmacsha256.h + + License: ISC (see LICENSE.txt) + +*******************************************************************************/ + +module libsodium.crypto_auth_hmacsha256; + +@nogc nothrow: + +import libsodium.export_; +import libsodium.crypto_hash_sha256; + +extern (C): + +enum crypto_auth_hmacsha256_BYTES = 32U; +size_t crypto_auth_hmacsha256_bytes (); + +enum crypto_auth_hmacsha256_KEYBYTES = 32U; +size_t crypto_auth_hmacsha256_keybytes (); + +int crypto_auth_hmacsha256 ( + ubyte* out_, + const(ubyte)* in_, + ulong inlen, + const(ubyte)* k); + +int crypto_auth_hmacsha256_verify ( + const(ubyte)* h, + const(ubyte)* in_, + ulong inlen, + const(ubyte)* k); + +/* ------------------------------------------------------------------------- */ + +struct crypto_auth_hmacsha256_state +{ + crypto_hash_sha256_state ictx; + crypto_hash_sha256_state octx; +} + +size_t crypto_auth_hmacsha256_statebytes (); + +int crypto_auth_hmacsha256_init ( + crypto_auth_hmacsha256_state* state, + const(ubyte)* key, + size_t keylen); + +int crypto_auth_hmacsha256_update ( + crypto_auth_hmacsha256_state* state, + const(ubyte)* in_, + ulong inlen); + +int crypto_auth_hmacsha256_final ( + crypto_auth_hmacsha256_state* state, + ubyte* out_); + +void crypto_auth_hmacsha256_keygen ( + ref ubyte[crypto_auth_hmacsha256_KEYBYTES] k); diff --git a/source/libsodium/crypto_auth_hmacsha512.d b/source/libsodium/crypto_auth_hmacsha512.d new file mode 100644 index 0000000..ca646e3 --- /dev/null +++ b/source/libsodium/crypto_auth_hmacsha512.d @@ -0,0 +1,61 @@ +/******************************************************************************* + + D language bindings for libsodium's crypto_auth_hmacsha512.h + + License: ISC (see LICENSE.txt) + +*******************************************************************************/ + +module libsodium.crypto_auth_hmacsha512; + +@nogc nothrow: + +import libsodium.export_; +import libsodium.crypto_hash_sha512; + +extern (C): + +enum crypto_auth_hmacsha512_BYTES = 64U; +size_t crypto_auth_hmacsha512_bytes (); + +enum crypto_auth_hmacsha512_KEYBYTES = 32U; +size_t crypto_auth_hmacsha512_keybytes (); + +int crypto_auth_hmacsha512 ( + ubyte* out_, + const(ubyte)* in_, + ulong inlen, + const(ubyte)* k); + +int crypto_auth_hmacsha512_verify ( + const(ubyte)* h, + const(ubyte)* in_, + ulong inlen, + const(ubyte)* k); + +/* ------------------------------------------------------------------------- */ + +struct crypto_auth_hmacsha512_state +{ + crypto_hash_sha512_state ictx; + crypto_hash_sha512_state octx; +} + +size_t crypto_auth_hmacsha512_statebytes (); + +int crypto_auth_hmacsha512_init ( + crypto_auth_hmacsha512_state* state, + const(ubyte)* key, + size_t keylen); + +int crypto_auth_hmacsha512_update ( + crypto_auth_hmacsha512_state* state, + const(ubyte)* in_, + ulong inlen); + +int crypto_auth_hmacsha512_final ( + crypto_auth_hmacsha512_state* state, + ubyte* out_); + +void crypto_auth_hmacsha512_keygen ( + ref ubyte[crypto_auth_hmacsha512_KEYBYTES] k); diff --git a/source/libsodium/crypto_auth_hmacsha512256.d b/source/libsodium/crypto_auth_hmacsha512256.d new file mode 100644 index 0000000..e89c917 --- /dev/null +++ b/source/libsodium/crypto_auth_hmacsha512256.d @@ -0,0 +1,57 @@ +/******************************************************************************* + + D language bindings for libsodium's crypto_auth_hmacsha512256.h + + License: ISC (see LICENSE.txt) + +*******************************************************************************/ + +module libsodium.crypto_auth_hmacsha512256; + +@nogc nothrow: + +import libsodium.export_; +import libsodium.crypto_auth_hmacsha512; + +extern (C): + +enum crypto_auth_hmacsha512256_BYTES = 32U; +size_t crypto_auth_hmacsha512256_bytes (); + +enum crypto_auth_hmacsha512256_KEYBYTES = 32U; +size_t crypto_auth_hmacsha512256_keybytes (); + +int crypto_auth_hmacsha512256 ( + ubyte* out_, + const(ubyte)* in_, + ulong inlen, + const(ubyte)* k); + +int crypto_auth_hmacsha512256_verify ( + const(ubyte)* h, + const(ubyte)* in_, + ulong inlen, + const(ubyte)* k); + +/* ------------------------------------------------------------------------- */ + +alias crypto_auth_hmacsha512256_state = crypto_auth_hmacsha512_state; + +size_t crypto_auth_hmacsha512256_statebytes (); + +int crypto_auth_hmacsha512256_init ( + crypto_auth_hmacsha512256_state* state, + const(ubyte)* key, + size_t keylen); + +int crypto_auth_hmacsha512256_update ( + crypto_auth_hmacsha512256_state* state, + const(ubyte)* in_, + ulong inlen); + +int crypto_auth_hmacsha512256_final ( + crypto_auth_hmacsha512256_state* state, + ubyte* out_); + +void crypto_auth_hmacsha512256_keygen ( + ref ubyte[crypto_auth_hmacsha512256_KEYBYTES] k); diff --git a/source/libsodium/crypto_box.d b/source/libsodium/crypto_box.d new file mode 100644 index 0000000..efc6976 --- /dev/null +++ b/source/libsodium/crypto_box.d @@ -0,0 +1,171 @@ +/******************************************************************************* + + D language bindings for libsodium's crypto_box.h + + License: ISC (see LICENSE.txt) + +*******************************************************************************/ + +module libsodium.crypto_box; + +@nogc nothrow: + +import libsodium.export_; +import libsodium.crypto_box_curve25519xsalsa20poly1305; + +extern (C): + +/* + * THREAD SAFETY: crypto_box_keypair() is thread-safe, + * provided that sodium_init() was called before. + * + * Other functions are always thread-safe. + */ + +enum crypto_box_SEEDBYTES = crypto_box_curve25519xsalsa20poly1305_SEEDBYTES; +size_t crypto_box_seedbytes (); + +enum crypto_box_PUBLICKEYBYTES = crypto_box_curve25519xsalsa20poly1305_PUBLICKEYBYTES; +size_t crypto_box_publickeybytes (); + +enum crypto_box_SECRETKEYBYTES = crypto_box_curve25519xsalsa20poly1305_SECRETKEYBYTES; +size_t crypto_box_secretkeybytes (); + +enum crypto_box_NONCEBYTES = crypto_box_curve25519xsalsa20poly1305_NONCEBYTES; +size_t crypto_box_noncebytes (); + +enum crypto_box_MACBYTES = crypto_box_curve25519xsalsa20poly1305_MACBYTES; +size_t crypto_box_macbytes (); + +enum crypto_box_MESSAGEBYTES_MAX = crypto_box_curve25519xsalsa20poly1305_MESSAGEBYTES_MAX; +size_t crypto_box_messagebytes_max (); + +enum crypto_box_PRIMITIVE = "curve25519xsalsa20poly1305"; +const(char)* crypto_box_primitive (); + +int crypto_box_seed_keypair (ubyte* pk, ubyte* sk, const(ubyte)* seed); + +int crypto_box_keypair (ubyte* pk, ubyte* sk); + +int crypto_box_easy ( + ubyte* c, + const(ubyte)* m, + ulong mlen, + const(ubyte)* n, + const(ubyte)* pk, + const(ubyte)* sk); + +int crypto_box_open_easy ( + ubyte* m, + const(ubyte)* c, + ulong clen, + const(ubyte)* n, + const(ubyte)* pk, + const(ubyte)* sk); + +int crypto_box_detached ( + ubyte* c, + ubyte* mac, + const(ubyte)* m, + ulong mlen, + const(ubyte)* n, + const(ubyte)* pk, + const(ubyte)* sk); + +int crypto_box_open_detached ( + ubyte* m, + const(ubyte)* c, + const(ubyte)* mac, + ulong clen, + const(ubyte)* n, + const(ubyte)* pk, + const(ubyte)* sk); + +/* -- Precomputation interface -- */ + +enum crypto_box_BEFORENMBYTES = crypto_box_curve25519xsalsa20poly1305_BEFORENMBYTES; +size_t crypto_box_beforenmbytes (); + +int crypto_box_beforenm (ubyte* k, const(ubyte)* pk, const(ubyte)* sk); + +int crypto_box_easy_afternm ( + ubyte* c, + const(ubyte)* m, + ulong mlen, + const(ubyte)* n, + const(ubyte)* k); + +int crypto_box_open_easy_afternm ( + ubyte* m, + const(ubyte)* c, + ulong clen, + const(ubyte)* n, + const(ubyte)* k); + +int crypto_box_detached_afternm ( + ubyte* c, + ubyte* mac, + const(ubyte)* m, + ulong mlen, + const(ubyte)* n, + const(ubyte)* k); + +int crypto_box_open_detached_afternm ( + ubyte* m, + const(ubyte)* c, + const(ubyte)* mac, + ulong clen, + const(ubyte)* n, + const(ubyte)* k); + +/* -- Ephemeral SK interface -- */ + +enum crypto_box_SEALBYTES = crypto_box_PUBLICKEYBYTES + crypto_box_MACBYTES; +size_t crypto_box_sealbytes (); + +int crypto_box_seal (ubyte* c, const(ubyte)* m, ulong mlen, const(ubyte)* pk); + +int crypto_box_seal_open ( + ubyte* m, + const(ubyte)* c, + ulong clen, + const(ubyte)* pk, + const(ubyte)* sk); + +/* -- NaCl compatibility interface ; Requires padding -- */ + +enum crypto_box_ZEROBYTES = crypto_box_curve25519xsalsa20poly1305_ZEROBYTES; +size_t crypto_box_zerobytes (); + +enum crypto_box_BOXZEROBYTES = crypto_box_curve25519xsalsa20poly1305_BOXZEROBYTES; +size_t crypto_box_boxzerobytes (); + +int crypto_box ( + ubyte* c, + const(ubyte)* m, + ulong mlen, + const(ubyte)* n, + const(ubyte)* pk, + const(ubyte)* sk); + +int crypto_box_open ( + ubyte* m, + const(ubyte)* c, + ulong clen, + const(ubyte)* n, + const(ubyte)* pk, + const(ubyte)* sk); + +int crypto_box_afternm ( + ubyte* c, + const(ubyte)* m, + ulong mlen, + const(ubyte)* n, + const(ubyte)* k); + +int crypto_box_open_afternm ( + ubyte* m, + const(ubyte)* c, + ulong clen, + const(ubyte)* n, + const(ubyte)* k); diff --git a/source/libsodium/crypto_box_curve25519xchacha20poly1305.d b/source/libsodium/crypto_box_curve25519xchacha20poly1305.d new file mode 100644 index 0000000..627b8ac --- /dev/null +++ b/source/libsodium/crypto_box_curve25519xchacha20poly1305.d @@ -0,0 +1,134 @@ +/******************************************************************************* + + D language bindings for libsodium's crypto_box_curve25519xchacha20poly1305.h + + License: ISC (see LICENSE.txt) + +*******************************************************************************/ + +module libsodium.crypto_box_curve25519xchacha20poly1305; + +@nogc nothrow: + +import libsodium.export_; +import libsodium.crypto_stream_xchacha20; + +extern (C): + +enum crypto_box_curve25519xchacha20poly1305_SEEDBYTES = 32U; +size_t crypto_box_curve25519xchacha20poly1305_seedbytes (); + +enum crypto_box_curve25519xchacha20poly1305_PUBLICKEYBYTES = 32U; +size_t crypto_box_curve25519xchacha20poly1305_publickeybytes (); + +enum crypto_box_curve25519xchacha20poly1305_SECRETKEYBYTES = 32U; +size_t crypto_box_curve25519xchacha20poly1305_secretkeybytes (); + +enum crypto_box_curve25519xchacha20poly1305_BEFORENMBYTES = 32U; +size_t crypto_box_curve25519xchacha20poly1305_beforenmbytes (); + +enum crypto_box_curve25519xchacha20poly1305_NONCEBYTES = 24U; +size_t crypto_box_curve25519xchacha20poly1305_noncebytes (); + +enum crypto_box_curve25519xchacha20poly1305_MACBYTES = 16U; +size_t crypto_box_curve25519xchacha20poly1305_macbytes (); + +enum crypto_box_curve25519xchacha20poly1305_MESSAGEBYTES_MAX = crypto_stream_xchacha20_MESSAGEBYTES_MAX - crypto_box_curve25519xchacha20poly1305_MACBYTES; +size_t crypto_box_curve25519xchacha20poly1305_messagebytes_max (); + +int crypto_box_curve25519xchacha20poly1305_seed_keypair ( + ubyte* pk, + ubyte* sk, + const(ubyte)* seed); + +int crypto_box_curve25519xchacha20poly1305_keypair (ubyte* pk, ubyte* sk); + +int crypto_box_curve25519xchacha20poly1305_easy ( + ubyte* c, + const(ubyte)* m, + ulong mlen, + const(ubyte)* n, + const(ubyte)* pk, + const(ubyte)* sk); + +int crypto_box_curve25519xchacha20poly1305_open_easy ( + ubyte* m, + const(ubyte)* c, + ulong clen, + const(ubyte)* n, + const(ubyte)* pk, + const(ubyte)* sk); + +int crypto_box_curve25519xchacha20poly1305_detached ( + ubyte* c, + ubyte* mac, + const(ubyte)* m, + ulong mlen, + const(ubyte)* n, + const(ubyte)* pk, + const(ubyte)* sk); + +int crypto_box_curve25519xchacha20poly1305_open_detached ( + ubyte* m, + const(ubyte)* c, + const(ubyte)* mac, + ulong clen, + const(ubyte)* n, + const(ubyte)* pk, + const(ubyte)* sk); + +/* -- Precomputation interface -- */ + +int crypto_box_curve25519xchacha20poly1305_beforenm ( + ubyte* k, + const(ubyte)* pk, + const(ubyte)* sk); + +int crypto_box_curve25519xchacha20poly1305_easy_afternm ( + ubyte* c, + const(ubyte)* m, + ulong mlen, + const(ubyte)* n, + const(ubyte)* k); + +int crypto_box_curve25519xchacha20poly1305_open_easy_afternm ( + ubyte* m, + const(ubyte)* c, + ulong clen, + const(ubyte)* n, + const(ubyte)* k); + +int crypto_box_curve25519xchacha20poly1305_detached_afternm ( + ubyte* c, + ubyte* mac, + const(ubyte)* m, + ulong mlen, + const(ubyte)* n, + const(ubyte)* k); + +int crypto_box_curve25519xchacha20poly1305_open_detached_afternm ( + ubyte* m, + const(ubyte)* c, + const(ubyte)* mac, + ulong clen, + const(ubyte)* n, + const(ubyte)* k); + +/* -- Ephemeral SK interface -- */ + +enum crypto_box_curve25519xchacha20poly1305_SEALBYTES = crypto_box_curve25519xchacha20poly1305_PUBLICKEYBYTES + crypto_box_curve25519xchacha20poly1305_MACBYTES; + +size_t crypto_box_curve25519xchacha20poly1305_sealbytes (); + +int crypto_box_curve25519xchacha20poly1305_seal ( + ubyte* c, + const(ubyte)* m, + ulong mlen, + const(ubyte)* pk); + +int crypto_box_curve25519xchacha20poly1305_seal_open ( + ubyte* m, + const(ubyte)* c, + ulong clen, + const(ubyte)* pk, + const(ubyte)* sk); diff --git a/source/libsodium/crypto_box_curve25519xsalsa20poly1305.d b/source/libsodium/crypto_box_curve25519xsalsa20poly1305.d new file mode 100644 index 0000000..e651fdb --- /dev/null +++ b/source/libsodium/crypto_box_curve25519xsalsa20poly1305.d @@ -0,0 +1,88 @@ +/******************************************************************************* + + D language bindings for libsodium's crypto_box_curve25519xsalsa20poly1305.h + + License: ISC (see LICENSE.txt) + +*******************************************************************************/ + +module libsodium.crypto_box_curve25519xsalsa20poly1305; + +@nogc nothrow: + +import libsodium.export_; +import libsodium.crypto_stream_xsalsa20; + +extern (C): + +enum crypto_box_curve25519xsalsa20poly1305_SEEDBYTES = 32U; +size_t crypto_box_curve25519xsalsa20poly1305_seedbytes (); + +enum crypto_box_curve25519xsalsa20poly1305_PUBLICKEYBYTES = 32U; +size_t crypto_box_curve25519xsalsa20poly1305_publickeybytes (); + +enum crypto_box_curve25519xsalsa20poly1305_SECRETKEYBYTES = 32U; +size_t crypto_box_curve25519xsalsa20poly1305_secretkeybytes (); + +enum crypto_box_curve25519xsalsa20poly1305_BEFORENMBYTES = 32U; +size_t crypto_box_curve25519xsalsa20poly1305_beforenmbytes (); + +enum crypto_box_curve25519xsalsa20poly1305_NONCEBYTES = 24U; +size_t crypto_box_curve25519xsalsa20poly1305_noncebytes (); + +enum crypto_box_curve25519xsalsa20poly1305_MACBYTES = 16U; +size_t crypto_box_curve25519xsalsa20poly1305_macbytes (); + +/* Only for the libsodium API - The NaCl compatibility API would require BOXZEROBYTES extra bytes */ +enum crypto_box_curve25519xsalsa20poly1305_MESSAGEBYTES_MAX = crypto_stream_xsalsa20_MESSAGEBYTES_MAX - crypto_box_curve25519xsalsa20poly1305_MACBYTES; +size_t crypto_box_curve25519xsalsa20poly1305_messagebytes_max (); + +int crypto_box_curve25519xsalsa20poly1305_seed_keypair ( + ubyte* pk, + ubyte* sk, + const(ubyte)* seed); + +int crypto_box_curve25519xsalsa20poly1305_keypair (ubyte* pk, ubyte* sk); + +int crypto_box_curve25519xsalsa20poly1305_beforenm ( + ubyte* k, + const(ubyte)* pk, + const(ubyte)* sk); + +/* -- NaCl compatibility interface ; Requires padding -- */ + +enum crypto_box_curve25519xsalsa20poly1305_BOXZEROBYTES = 16U; +size_t crypto_box_curve25519xsalsa20poly1305_boxzerobytes (); + +enum crypto_box_curve25519xsalsa20poly1305_ZEROBYTES = crypto_box_curve25519xsalsa20poly1305_BOXZEROBYTES + crypto_box_curve25519xsalsa20poly1305_MACBYTES; +size_t crypto_box_curve25519xsalsa20poly1305_zerobytes (); + +int crypto_box_curve25519xsalsa20poly1305 ( + ubyte* c, + const(ubyte)* m, + ulong mlen, + const(ubyte)* n, + const(ubyte)* pk, + const(ubyte)* sk); + +int crypto_box_curve25519xsalsa20poly1305_open ( + ubyte* m, + const(ubyte)* c, + ulong clen, + const(ubyte)* n, + const(ubyte)* pk, + const(ubyte)* sk); + +int crypto_box_curve25519xsalsa20poly1305_afternm ( + ubyte* c, + const(ubyte)* m, + ulong mlen, + const(ubyte)* n, + const(ubyte)* k); + +int crypto_box_curve25519xsalsa20poly1305_open_afternm ( + ubyte* m, + const(ubyte)* c, + ulong clen, + const(ubyte)* n, + const(ubyte)* k); diff --git a/source/libsodium/crypto_core_ed25519.d b/source/libsodium/crypto_core_ed25519.d new file mode 100644 index 0000000..5072c4e --- /dev/null +++ b/source/libsodium/crypto_core_ed25519.d @@ -0,0 +1,59 @@ +/******************************************************************************* + + D language bindings for libsodium's crypto_core_ed25519.h + + License: ISC (see LICENSE.txt) + +*******************************************************************************/ + +module libsodium.crypto_core_ed25519; + +@nogc nothrow: + +import libsodium.export_; + +extern (C): + +enum crypto_core_ed25519_BYTES = 32; +size_t crypto_core_ed25519_bytes (); + +enum crypto_core_ed25519_UNIFORMBYTES = 32; +size_t crypto_core_ed25519_uniformbytes (); + +enum crypto_core_ed25519_SCALARBYTES = 32; +size_t crypto_core_ed25519_scalarbytes (); + +enum crypto_core_ed25519_NONREDUCEDSCALARBYTES = 64; +size_t crypto_core_ed25519_nonreducedscalarbytes (); + +int crypto_core_ed25519_is_valid_point (const(ubyte)* p); + +int crypto_core_ed25519_add (ubyte* r, const(ubyte)* p, const(ubyte)* q); + +int crypto_core_ed25519_sub (ubyte* r, const(ubyte)* p, const(ubyte)* q); + +int crypto_core_ed25519_from_uniform (ubyte* p, const(ubyte)* r); + +void crypto_core_ed25519_scalar_random (ubyte* r); + +int crypto_core_ed25519_scalar_invert (ubyte* recip, const(ubyte)* s); + +void crypto_core_ed25519_scalar_negate (ubyte* neg, const(ubyte)* s); + +void crypto_core_ed25519_scalar_complement (ubyte* comp, const(ubyte)* s); + +void crypto_core_ed25519_scalar_add ( + ubyte* z, + const(ubyte)* x, + const(ubyte)* y); + +void crypto_core_ed25519_scalar_sub ( + ubyte* z, + const(ubyte)* x, + const(ubyte)* y); + +/* + * The interval `s` is sampled from should be at least 317 bits to ensure almost + * uniformity of `r` over `L`. + */ +void crypto_core_ed25519_scalar_reduce (ubyte* r, const(ubyte)* s); diff --git a/source/libsodium/crypto_core_hchacha20.d b/source/libsodium/crypto_core_hchacha20.d new file mode 100644 index 0000000..6077145 --- /dev/null +++ b/source/libsodium/crypto_core_hchacha20.d @@ -0,0 +1,33 @@ +/******************************************************************************* + + D language bindings for libsodium's crypto_core_hchacha20.h + + License: ISC (see LICENSE.txt) + +*******************************************************************************/ + +module libsodium.crypto_core_hchacha20; + +@nogc nothrow: + +import libsodium.export_; + +extern (C): + +enum crypto_core_hchacha20_OUTPUTBYTES = 32U; +size_t crypto_core_hchacha20_outputbytes (); + +enum crypto_core_hchacha20_INPUTBYTES = 16U; +size_t crypto_core_hchacha20_inputbytes (); + +enum crypto_core_hchacha20_KEYBYTES = 32U; +size_t crypto_core_hchacha20_keybytes (); + +enum crypto_core_hchacha20_CONSTBYTES = 16U; +size_t crypto_core_hchacha20_constbytes (); + +int crypto_core_hchacha20 ( + ubyte* out_, + const(ubyte)* in_, + const(ubyte)* k, + const(ubyte)* c); diff --git a/source/libsodium/crypto_core_hsalsa20.d b/source/libsodium/crypto_core_hsalsa20.d new file mode 100644 index 0000000..16c7d81 --- /dev/null +++ b/source/libsodium/crypto_core_hsalsa20.d @@ -0,0 +1,33 @@ +/******************************************************************************* + + D language bindings for libsodium's crypto_core_hsalsa20.h + + License: ISC (see LICENSE.txt) + +*******************************************************************************/ + +module libsodium.crypto_core_hsalsa20; + +@nogc nothrow: + +import libsodium.export_; + +extern (C): + +enum crypto_core_hsalsa20_OUTPUTBYTES = 32U; +size_t crypto_core_hsalsa20_outputbytes (); + +enum crypto_core_hsalsa20_INPUTBYTES = 16U; +size_t crypto_core_hsalsa20_inputbytes (); + +enum crypto_core_hsalsa20_KEYBYTES = 32U; +size_t crypto_core_hsalsa20_keybytes (); + +enum crypto_core_hsalsa20_CONSTBYTES = 16U; +size_t crypto_core_hsalsa20_constbytes (); + +int crypto_core_hsalsa20 ( + ubyte* out_, + const(ubyte)* in_, + const(ubyte)* k, + const(ubyte)* c); diff --git a/source/libsodium/crypto_core_salsa20.d b/source/libsodium/crypto_core_salsa20.d new file mode 100644 index 0000000..f39462c --- /dev/null +++ b/source/libsodium/crypto_core_salsa20.d @@ -0,0 +1,33 @@ +/******************************************************************************* + + D language bindings for libsodium's crypto_core_salsa20.h + + License: ISC (see LICENSE.txt) + +*******************************************************************************/ + +module libsodium.crypto_core_salsa20; + +@nogc nothrow: + +import libsodium.export_; + +extern (C): + +enum crypto_core_salsa20_OUTPUTBYTES = 64U; +size_t crypto_core_salsa20_outputbytes (); + +enum crypto_core_salsa20_INPUTBYTES = 16U; +size_t crypto_core_salsa20_inputbytes (); + +enum crypto_core_salsa20_KEYBYTES = 32U; +size_t crypto_core_salsa20_keybytes (); + +enum crypto_core_salsa20_CONSTBYTES = 16U; +size_t crypto_core_salsa20_constbytes (); + +int crypto_core_salsa20 ( + ubyte* out_, + const(ubyte)* in_, + const(ubyte)* k, + const(ubyte)* c); diff --git a/source/libsodium/crypto_core_salsa2012.d b/source/libsodium/crypto_core_salsa2012.d new file mode 100644 index 0000000..ce6d07c --- /dev/null +++ b/source/libsodium/crypto_core_salsa2012.d @@ -0,0 +1,33 @@ +/******************************************************************************* + + D language bindings for libsodium's crypto_core_salsa2012.h + + License: ISC (see LICENSE.txt) + +*******************************************************************************/ + +module libsodium.crypto_core_salsa2012; + +@nogc nothrow: + +import libsodium.export_; + +extern (C): + +enum crypto_core_salsa2012_OUTPUTBYTES = 64U; +size_t crypto_core_salsa2012_outputbytes (); + +enum crypto_core_salsa2012_INPUTBYTES = 16U; +size_t crypto_core_salsa2012_inputbytes (); + +enum crypto_core_salsa2012_KEYBYTES = 32U; +size_t crypto_core_salsa2012_keybytes (); + +enum crypto_core_salsa2012_CONSTBYTES = 16U; +size_t crypto_core_salsa2012_constbytes (); + +int crypto_core_salsa2012 ( + ubyte* out_, + const(ubyte)* in_, + const(ubyte)* k, + const(ubyte)* c); diff --git a/source/libsodium/crypto_core_salsa208.d b/source/libsodium/crypto_core_salsa208.d new file mode 100644 index 0000000..2ce9c4b --- /dev/null +++ b/source/libsodium/crypto_core_salsa208.d @@ -0,0 +1,33 @@ +/******************************************************************************* + + D language bindings for libsodium's crypto_core_salsa208.h + + License: ISC (see LICENSE.txt) + +*******************************************************************************/ + +module libsodium.crypto_core_salsa208; + +@nogc nothrow: + +import libsodium.export_; + +extern (C): + +enum crypto_core_salsa208_OUTPUTBYTES = 64U; +size_t crypto_core_salsa208_outputbytes (); + +enum crypto_core_salsa208_INPUTBYTES = 16U; +size_t crypto_core_salsa208_inputbytes (); + +enum crypto_core_salsa208_KEYBYTES = 32U; +size_t crypto_core_salsa208_keybytes (); + +enum crypto_core_salsa208_CONSTBYTES = 16U; +size_t crypto_core_salsa208_constbytes (); + +int crypto_core_salsa208 ( + ubyte* out_, + const(ubyte)* in_, + const(ubyte)* k, + const(ubyte)* c); diff --git a/source/libsodium/crypto_generichash.d b/source/libsodium/crypto_generichash.d new file mode 100644 index 0000000..efe001e --- /dev/null +++ b/source/libsodium/crypto_generichash.d @@ -0,0 +1,71 @@ +/******************************************************************************* + + D language bindings for libsodium's crypto_generichash.h + + License: ISC (see LICENSE.txt) + +*******************************************************************************/ + +module libsodium.crypto_generichash; + +@nogc nothrow: + +import libsodium.export_; +import libsodium.crypto_generichash_blake2b; + +extern (C): + +enum crypto_generichash_BYTES_MIN = crypto_generichash_blake2b_BYTES_MIN; +size_t crypto_generichash_bytes_min (); + +enum crypto_generichash_BYTES_MAX = crypto_generichash_blake2b_BYTES_MAX; +size_t crypto_generichash_bytes_max (); + +enum crypto_generichash_BYTES = crypto_generichash_blake2b_BYTES; +size_t crypto_generichash_bytes (); + +enum crypto_generichash_KEYBYTES_MIN = crypto_generichash_blake2b_KEYBYTES_MIN; +size_t crypto_generichash_keybytes_min (); + +enum crypto_generichash_KEYBYTES_MAX = crypto_generichash_blake2b_KEYBYTES_MAX; +size_t crypto_generichash_keybytes_max (); + +enum crypto_generichash_KEYBYTES = crypto_generichash_blake2b_KEYBYTES; +size_t crypto_generichash_keybytes (); + +enum crypto_generichash_PRIMITIVE = "blake2b"; +const(char)* crypto_generichash_primitive (); + +/* + * Important when writing bindings for other programming languages: + * the state address should be 64-bytes aligned. + */ +alias crypto_generichash_state = crypto_generichash_blake2b_state; + +size_t crypto_generichash_statebytes (); + +int crypto_generichash ( + ubyte* out_, + size_t outlen, + const(ubyte)* in_, + ulong inlen, + const(ubyte)* key, + size_t keylen); + +int crypto_generichash_init ( + crypto_generichash_state* state, + const(ubyte)* key, + const size_t keylen, + const size_t outlen); + +int crypto_generichash_update ( + crypto_generichash_state* state, + const(ubyte)* in_, + ulong inlen); + +int crypto_generichash_final ( + crypto_generichash_state* state, + ubyte* out_, + const size_t outlen); + +void crypto_generichash_keygen (ref ubyte[crypto_generichash_KEYBYTES] k); diff --git a/source/libsodium/crypto_generichash_blake2b.d b/source/libsodium/crypto_generichash_blake2b.d new file mode 100644 index 0000000..e7a9c3c --- /dev/null +++ b/source/libsodium/crypto_generichash_blake2b.d @@ -0,0 +1,91 @@ +/******************************************************************************* + + D language bindings for libsodium's crypto_generichash_blake2b.h + + License: ISC (see LICENSE.txt) + +*******************************************************************************/ + +module libsodium.crypto_generichash_blake2b; + +@nogc nothrow: + +import libsodium.export_; + +extern (C): + +struct crypto_generichash_blake2b_state +{ + ubyte[384] opaque; +} + +enum crypto_generichash_blake2b_BYTES_MIN = 16U; +size_t crypto_generichash_blake2b_bytes_min (); + +enum crypto_generichash_blake2b_BYTES_MAX = 64U; +size_t crypto_generichash_blake2b_bytes_max (); + +enum crypto_generichash_blake2b_BYTES = 32U; +size_t crypto_generichash_blake2b_bytes (); + +enum crypto_generichash_blake2b_KEYBYTES_MIN = 16U; +size_t crypto_generichash_blake2b_keybytes_min (); + +enum crypto_generichash_blake2b_KEYBYTES_MAX = 64U; +size_t crypto_generichash_blake2b_keybytes_max (); + +enum crypto_generichash_blake2b_KEYBYTES = 32U; +size_t crypto_generichash_blake2b_keybytes (); + +enum crypto_generichash_blake2b_SALTBYTES = 16U; +size_t crypto_generichash_blake2b_saltbytes (); + +enum crypto_generichash_blake2b_PERSONALBYTES = 16U; +size_t crypto_generichash_blake2b_personalbytes (); + +size_t crypto_generichash_blake2b_statebytes (); + +int crypto_generichash_blake2b ( + ubyte* out_, + size_t outlen, + const(ubyte)* in_, + ulong inlen, + const(ubyte)* key, + size_t keylen); + +int crypto_generichash_blake2b_salt_personal ( + ubyte* out_, + size_t outlen, + const(ubyte)* in_, + ulong inlen, + const(ubyte)* key, + size_t keylen, + const(ubyte)* salt, + const(ubyte)* personal); + +int crypto_generichash_blake2b_init ( + crypto_generichash_blake2b_state* state, + const(ubyte)* key, + const size_t keylen, + const size_t outlen); + +int crypto_generichash_blake2b_init_salt_personal ( + crypto_generichash_blake2b_state* state, + const(ubyte)* key, + const size_t keylen, + const size_t outlen, + const(ubyte)* salt, + const(ubyte)* personal); + +int crypto_generichash_blake2b_update ( + crypto_generichash_blake2b_state* state, + const(ubyte)* in_, + ulong inlen); + +int crypto_generichash_blake2b_final ( + crypto_generichash_blake2b_state* state, + ubyte* out_, + const size_t outlen); + +void crypto_generichash_blake2b_keygen ( + ref ubyte[crypto_generichash_blake2b_KEYBYTES] k); diff --git a/source/libsodium/crypto_hash.d b/source/libsodium/crypto_hash.d new file mode 100644 index 0000000..cf2b75d --- /dev/null +++ b/source/libsodium/crypto_hash.d @@ -0,0 +1,31 @@ +/******************************************************************************* + + D language bindings for libsodium's crypto_hash.h + + License: ISC (see LICENSE.txt) + +*******************************************************************************/ + +module libsodium.crypto_hash; + +@nogc nothrow: + +import libsodium.export_; +import libsodium.crypto_hash_sha512; + +extern (C): + +/* + * WARNING: Unless you absolutely need to use SHA512 for interoperatibility, + * purposes, you might want to consider crypto_generichash() instead. + * Unlike SHA512, crypto_generichash() is not vulnerable to length + * extension attacks. + */ + +enum crypto_hash_BYTES = crypto_hash_sha512_BYTES; +size_t crypto_hash_bytes (); + +int crypto_hash (ubyte* out_, const(ubyte)* in_, ulong inlen); + +enum crypto_hash_PRIMITIVE = "sha512"; +const(char)* crypto_hash_primitive (); diff --git a/source/libsodium/crypto_hash_sha256.d b/source/libsodium/crypto_hash_sha256.d new file mode 100644 index 0000000..5a41942 --- /dev/null +++ b/source/libsodium/crypto_hash_sha256.d @@ -0,0 +1,45 @@ +/******************************************************************************* + + D language bindings for libsodium's crypto_hash_sha256.h + + License: ISC (see LICENSE.txt) + +*******************************************************************************/ + +module libsodium.crypto_hash_sha256; + +@nogc nothrow: + +import libsodium.export_; + +extern (C): + +/* + * WARNING: Unless you absolutely need to use SHA256 for interoperatibility, + * purposes, you might want to consider crypto_generichash() instead. + * Unlike SHA256, crypto_generichash() is not vulnerable to length + * extension attacks. + */ + +struct crypto_hash_sha256_state +{ + uint[8] state; + ulong count; + ubyte[64] buf; +} + +size_t crypto_hash_sha256_statebytes (); + +enum crypto_hash_sha256_BYTES = 32U; +size_t crypto_hash_sha256_bytes (); + +int crypto_hash_sha256 (ubyte* out_, const(ubyte)* in_, ulong inlen); + +int crypto_hash_sha256_init (crypto_hash_sha256_state* state); + +int crypto_hash_sha256_update ( + crypto_hash_sha256_state* state, + const(ubyte)* in_, + ulong inlen); + +int crypto_hash_sha256_final (crypto_hash_sha256_state* state, ubyte* out_); diff --git a/source/libsodium/crypto_hash_sha512.d b/source/libsodium/crypto_hash_sha512.d new file mode 100644 index 0000000..4f5a47d --- /dev/null +++ b/source/libsodium/crypto_hash_sha512.d @@ -0,0 +1,45 @@ +/******************************************************************************* + + D language bindings for libsodium's crypto_hash_sha512.h + + License: ISC (see LICENSE.txt) + +*******************************************************************************/ + +module libsodium.crypto_hash_sha512; + +@nogc nothrow: + +import libsodium.export_; + +extern (C): + +/* + * WARNING: Unless you absolutely need to use SHA512 for interoperatibility, + * purposes, you might want to consider crypto_generichash() instead. + * Unlike SHA512, crypto_generichash() is not vulnerable to length + * extension attacks. + */ + +struct crypto_hash_sha512_state +{ + ulong[8] state; + ulong[2] count; + ubyte[128] buf; +} + +size_t crypto_hash_sha512_statebytes (); + +enum crypto_hash_sha512_BYTES = 64U; +size_t crypto_hash_sha512_bytes (); + +int crypto_hash_sha512 (ubyte* out_, const(ubyte)* in_, ulong inlen); + +int crypto_hash_sha512_init (crypto_hash_sha512_state* state); + +int crypto_hash_sha512_update ( + crypto_hash_sha512_state* state, + const(ubyte)* in_, + ulong inlen); + +int crypto_hash_sha512_final (crypto_hash_sha512_state* state, ubyte* out_); diff --git a/source/libsodium/crypto_kdf.d b/source/libsodium/crypto_kdf.d new file mode 100644 index 0000000..112d6bf --- /dev/null +++ b/source/libsodium/crypto_kdf.d @@ -0,0 +1,40 @@ +/******************************************************************************* + + D language bindings for libsodium's crypto_kdf.h + + License: ISC (see LICENSE.txt) + +*******************************************************************************/ + +module libsodium.crypto_kdf; + +@nogc nothrow: + +import libsodium.export_; +import libsodium.crypto_kdf_blake2b; + +extern (C): + +enum crypto_kdf_BYTES_MIN = crypto_kdf_blake2b_BYTES_MIN; +size_t crypto_kdf_bytes_min (); + +enum crypto_kdf_BYTES_MAX = crypto_kdf_blake2b_BYTES_MAX; +size_t crypto_kdf_bytes_max (); + +enum crypto_kdf_CONTEXTBYTES = crypto_kdf_blake2b_CONTEXTBYTES; +size_t crypto_kdf_contextbytes (); + +enum crypto_kdf_KEYBYTES = crypto_kdf_blake2b_KEYBYTES; +size_t crypto_kdf_keybytes (); + +enum crypto_kdf_PRIMITIVE = "blake2b"; +const(char)* crypto_kdf_primitive (); + +int crypto_kdf_derive_from_key ( + ubyte* subkey, + size_t subkey_len, + ulong subkey_id, + ref const(char)[crypto_kdf_CONTEXTBYTES] ctx, + ref const(ubyte)[crypto_kdf_KEYBYTES] key); + +void crypto_kdf_keygen (ref ubyte[crypto_kdf_KEYBYTES] k); diff --git a/source/libsodium/crypto_kdf_blake2b.d b/source/libsodium/crypto_kdf_blake2b.d new file mode 100644 index 0000000..cd8ae10 --- /dev/null +++ b/source/libsodium/crypto_kdf_blake2b.d @@ -0,0 +1,34 @@ +/******************************************************************************* + + D language bindings for libsodium's crypto_kdf_blake2b.h + + License: ISC (see LICENSE.txt) + +*******************************************************************************/ + +module libsodium.crypto_kdf_blake2b; + +@nogc nothrow: + +import libsodium.export_; + +extern (C): + +enum crypto_kdf_blake2b_BYTES_MIN = 16; +size_t crypto_kdf_blake2b_bytes_min (); + +enum crypto_kdf_blake2b_BYTES_MAX = 64; +size_t crypto_kdf_blake2b_bytes_max (); + +enum crypto_kdf_blake2b_CONTEXTBYTES = 8; +size_t crypto_kdf_blake2b_contextbytes (); + +enum crypto_kdf_blake2b_KEYBYTES = 32; +size_t crypto_kdf_blake2b_keybytes (); + +int crypto_kdf_blake2b_derive_from_key ( + ubyte* subkey, + size_t subkey_len, + ulong subkey_id, + ref const(char)[crypto_kdf_blake2b_CONTEXTBYTES] ctx, + ref const(ubyte)[crypto_kdf_blake2b_KEYBYTES] key); diff --git a/source/libsodium/crypto_kx.d b/source/libsodium/crypto_kx.d new file mode 100644 index 0000000..b57cb94 --- /dev/null +++ b/source/libsodium/crypto_kx.d @@ -0,0 +1,53 @@ +/******************************************************************************* + + D language bindings for libsodium's crypto_kx.h + + License: ISC (see LICENSE.txt) + +*******************************************************************************/ + +module libsodium.crypto_kx; + +@nogc nothrow: + +import libsodium.export_; + +extern (C): + +enum crypto_kx_PUBLICKEYBYTES = 32; +size_t crypto_kx_publickeybytes (); + +enum crypto_kx_SECRETKEYBYTES = 32; +size_t crypto_kx_secretkeybytes (); + +enum crypto_kx_SEEDBYTES = 32; +size_t crypto_kx_seedbytes (); + +enum crypto_kx_SESSIONKEYBYTES = 32; +size_t crypto_kx_sessionkeybytes (); + +enum crypto_kx_PRIMITIVE = "x25519blake2b"; +const(char)* crypto_kx_primitive (); + +int crypto_kx_seed_keypair ( + ref ubyte[crypto_kx_PUBLICKEYBYTES] pk, + ref ubyte[crypto_kx_SECRETKEYBYTES] sk, + ref const(ubyte)[crypto_kx_SEEDBYTES] seed); + +int crypto_kx_keypair ( + ref ubyte[crypto_kx_PUBLICKEYBYTES] pk, + ref ubyte[crypto_kx_SECRETKEYBYTES] sk); + +int crypto_kx_client_session_keys ( + ref ubyte[crypto_kx_SESSIONKEYBYTES] rx, + ref ubyte[crypto_kx_SESSIONKEYBYTES] tx, + ref const(ubyte)[crypto_kx_PUBLICKEYBYTES] client_pk, + ref const(ubyte)[crypto_kx_SECRETKEYBYTES] client_sk, + ref const(ubyte)[crypto_kx_PUBLICKEYBYTES] server_pk); + +int crypto_kx_server_session_keys ( + ref ubyte[crypto_kx_SESSIONKEYBYTES] rx, + ref ubyte[crypto_kx_SESSIONKEYBYTES] tx, + ref const(ubyte)[crypto_kx_PUBLICKEYBYTES] server_pk, + ref const(ubyte)[crypto_kx_SECRETKEYBYTES] server_sk, + ref const(ubyte)[crypto_kx_PUBLICKEYBYTES] client_pk); diff --git a/source/libsodium/crypto_onetimeauth.d b/source/libsodium/crypto_onetimeauth.d new file mode 100644 index 0000000..7a068dc --- /dev/null +++ b/source/libsodium/crypto_onetimeauth.d @@ -0,0 +1,54 @@ +/******************************************************************************* + + D language bindings for libsodium's crypto_onetimeauth.h + + License: ISC (see LICENSE.txt) + +*******************************************************************************/ + +module libsodium.crypto_onetimeauth; + +@nogc nothrow: + +import libsodium.export_; +import libsodium.crypto_onetimeauth_poly1305; + +extern (C): + +alias crypto_onetimeauth_state = crypto_onetimeauth_poly1305_state; + +size_t crypto_onetimeauth_statebytes (); + +enum crypto_onetimeauth_BYTES = crypto_onetimeauth_poly1305_BYTES; +size_t crypto_onetimeauth_bytes (); + +enum crypto_onetimeauth_KEYBYTES = crypto_onetimeauth_poly1305_KEYBYTES; +size_t crypto_onetimeauth_keybytes (); + +enum crypto_onetimeauth_PRIMITIVE = "poly1305"; +const(char)* crypto_onetimeauth_primitive (); + +int crypto_onetimeauth ( + ubyte* out_, + const(ubyte)* in_, + ulong inlen, + const(ubyte)* k); + +int crypto_onetimeauth_verify ( + const(ubyte)* h, + const(ubyte)* in_, + ulong inlen, + const(ubyte)* k); + +int crypto_onetimeauth_init ( + crypto_onetimeauth_state* state, + const(ubyte)* key); + +int crypto_onetimeauth_update ( + crypto_onetimeauth_state* state, + const(ubyte)* in_, + ulong inlen); + +int crypto_onetimeauth_final (crypto_onetimeauth_state* state, ubyte* out_); + +void crypto_onetimeauth_keygen (ref ubyte[crypto_onetimeauth_KEYBYTES] k); diff --git a/source/libsodium/crypto_onetimeauth_poly1305.d b/source/libsodium/crypto_onetimeauth_poly1305.d new file mode 100644 index 0000000..e51d21d --- /dev/null +++ b/source/libsodium/crypto_onetimeauth_poly1305.d @@ -0,0 +1,56 @@ +/******************************************************************************* + + D language bindings for libsodium's crypto_onetimeauth_poly1305.h + + License: ISC (see LICENSE.txt) + +*******************************************************************************/ + +module libsodium.crypto_onetimeauth_poly1305; + +@nogc nothrow: + +import libsodium.export_; + +extern (C): + +struct crypto_onetimeauth_poly1305_state +{ + ubyte[256] opaque; +} + +size_t crypto_onetimeauth_poly1305_statebytes (); + +enum crypto_onetimeauth_poly1305_BYTES = 16U; +size_t crypto_onetimeauth_poly1305_bytes (); + +enum crypto_onetimeauth_poly1305_KEYBYTES = 32U; +size_t crypto_onetimeauth_poly1305_keybytes (); + +int crypto_onetimeauth_poly1305 ( + ubyte* out_, + const(ubyte)* in_, + ulong inlen, + const(ubyte)* k); + +int crypto_onetimeauth_poly1305_verify ( + const(ubyte)* h, + const(ubyte)* in_, + ulong inlen, + const(ubyte)* k); + +int crypto_onetimeauth_poly1305_init ( + crypto_onetimeauth_poly1305_state* state, + const(ubyte)* key); + +int crypto_onetimeauth_poly1305_update ( + crypto_onetimeauth_poly1305_state* state, + const(ubyte)* in_, + ulong inlen); + +int crypto_onetimeauth_poly1305_final ( + crypto_onetimeauth_poly1305_state* state, + ubyte* out_); + +void crypto_onetimeauth_poly1305_keygen ( + ref ubyte[crypto_onetimeauth_poly1305_KEYBYTES] k); diff --git a/source/libsodium/crypto_pwhash.d b/source/libsodium/crypto_pwhash.d new file mode 100644 index 0000000..37e5d6e --- /dev/null +++ b/source/libsodium/crypto_pwhash.d @@ -0,0 +1,126 @@ +/******************************************************************************* + + D language bindings for libsodium's crypto_pwhash.h + + License: ISC (see LICENSE.txt) + +*******************************************************************************/ + +module libsodium.crypto_pwhash; + +@nogc nothrow: + +import libsodium.export_; +import libsodium.crypto_pwhash_argon2i; +import libsodium.crypto_pwhash_argon2id; + +extern (C): + +enum crypto_pwhash_ALG_ARGON2I13 = crypto_pwhash_argon2i_ALG_ARGON2I13; +int crypto_pwhash_alg_argon2i13 (); + +enum crypto_pwhash_ALG_ARGON2ID13 = crypto_pwhash_argon2id_ALG_ARGON2ID13; +int crypto_pwhash_alg_argon2id13 (); + +enum crypto_pwhash_ALG_DEFAULT = crypto_pwhash_ALG_ARGON2ID13; +int crypto_pwhash_alg_default (); + +enum crypto_pwhash_BYTES_MIN = crypto_pwhash_argon2id_BYTES_MIN; +size_t crypto_pwhash_bytes_min (); + +enum crypto_pwhash_BYTES_MAX = crypto_pwhash_argon2id_BYTES_MAX; +size_t crypto_pwhash_bytes_max (); + +enum crypto_pwhash_PASSWD_MIN = crypto_pwhash_argon2id_PASSWD_MIN; +size_t crypto_pwhash_passwd_min (); + +enum crypto_pwhash_PASSWD_MAX = crypto_pwhash_argon2id_PASSWD_MAX; +size_t crypto_pwhash_passwd_max (); + +enum crypto_pwhash_SALTBYTES = crypto_pwhash_argon2id_SALTBYTES; +size_t crypto_pwhash_saltbytes (); + +enum crypto_pwhash_STRBYTES = crypto_pwhash_argon2id_STRBYTES; +size_t crypto_pwhash_strbytes (); + +enum crypto_pwhash_STRPREFIX = crypto_pwhash_argon2id_STRPREFIX; +const(char)* crypto_pwhash_strprefix (); + +enum crypto_pwhash_OPSLIMIT_MIN = crypto_pwhash_argon2id_OPSLIMIT_MIN; +size_t crypto_pwhash_opslimit_min (); + +enum crypto_pwhash_OPSLIMIT_MAX = crypto_pwhash_argon2id_OPSLIMIT_MAX; +size_t crypto_pwhash_opslimit_max (); + +enum crypto_pwhash_MEMLIMIT_MIN = crypto_pwhash_argon2id_MEMLIMIT_MIN; +size_t crypto_pwhash_memlimit_min (); + +enum crypto_pwhash_MEMLIMIT_MAX = crypto_pwhash_argon2id_MEMLIMIT_MAX; +size_t crypto_pwhash_memlimit_max (); + +enum crypto_pwhash_OPSLIMIT_INTERACTIVE = crypto_pwhash_argon2id_OPSLIMIT_INTERACTIVE; +size_t crypto_pwhash_opslimit_interactive (); + +enum crypto_pwhash_MEMLIMIT_INTERACTIVE = crypto_pwhash_argon2id_MEMLIMIT_INTERACTIVE; +size_t crypto_pwhash_memlimit_interactive (); + +enum crypto_pwhash_OPSLIMIT_MODERATE = crypto_pwhash_argon2id_OPSLIMIT_MODERATE; +size_t crypto_pwhash_opslimit_moderate (); + +enum crypto_pwhash_MEMLIMIT_MODERATE = crypto_pwhash_argon2id_MEMLIMIT_MODERATE; +size_t crypto_pwhash_memlimit_moderate (); + +enum crypto_pwhash_OPSLIMIT_SENSITIVE = crypto_pwhash_argon2id_OPSLIMIT_SENSITIVE; +size_t crypto_pwhash_opslimit_sensitive (); + +enum crypto_pwhash_MEMLIMIT_SENSITIVE = crypto_pwhash_argon2id_MEMLIMIT_SENSITIVE; +size_t crypto_pwhash_memlimit_sensitive (); + +/* + * With this function, do not forget to store all parameters, including the + * algorithm identifier in order to produce deterministic output. + * The crypto_pwhash_* definitions, including crypto_pwhash_ALG_DEFAULT, + * may change. + */ +int crypto_pwhash ( + ubyte* out_, + ulong outlen, + const char* passwd, + ulong passwdlen, + const ubyte* salt, + ulong opslimit, + size_t memlimit, + int alg); + +/* + * The output string already includes all the required parameters, including + * the algorithm identifier. The string is all that has to be stored in + * order to verify a password. + */ +int crypto_pwhash_str ( + ref char[crypto_pwhash_STRBYTES] out_, + const char* passwd, + ulong passwdlen, + ulong opslimit, + size_t memlimit); + +int crypto_pwhash_str_alg ( + ref char[crypto_pwhash_STRBYTES] out_, + const char* passwd, + ulong passwdlen, + ulong opslimit, + size_t memlimit, + int alg); + +int crypto_pwhash_str_verify ( + ref const(char)[crypto_pwhash_STRBYTES] str, + const char* passwd, + ulong passwdlen); + +int crypto_pwhash_str_needs_rehash ( + ref const(char)[crypto_pwhash_STRBYTES] str, + ulong opslimit, + size_t memlimit); + +enum crypto_pwhash_PRIMITIVE = "argon2i"; +const(char)* crypto_pwhash_primitive (); diff --git a/source/libsodium/crypto_pwhash_argon2i.d b/source/libsodium/crypto_pwhash_argon2i.d new file mode 100644 index 0000000..efaed5e --- /dev/null +++ b/source/libsodium/crypto_pwhash_argon2i.d @@ -0,0 +1,97 @@ +/******************************************************************************* + + D language bindings for libsodium's crypto_pwhash_argon2i.h + + License: ISC (see LICENSE.txt) + +*******************************************************************************/ + +module libsodium.crypto_pwhash_argon2i; + +@nogc nothrow: + +import libsodium.export_; +import core.stdc.stdint; + +extern (C): + +enum crypto_pwhash_argon2i_ALG_ARGON2I13 = 1; +int crypto_pwhash_argon2i_alg_argon2i13 (); + +enum crypto_pwhash_argon2i_BYTES_MIN = 16U; +size_t crypto_pwhash_argon2i_bytes_min (); + +enum crypto_pwhash_argon2i_BYTES_MAX = SODIUM_MIN(SODIUM_SIZE_MAX, 4294967295U); +size_t crypto_pwhash_argon2i_bytes_max (); + +enum crypto_pwhash_argon2i_PASSWD_MIN = 0U; +size_t crypto_pwhash_argon2i_passwd_min (); + +enum crypto_pwhash_argon2i_PASSWD_MAX = 4294967295U; +size_t crypto_pwhash_argon2i_passwd_max (); + +enum crypto_pwhash_argon2i_SALTBYTES = 16U; +size_t crypto_pwhash_argon2i_saltbytes (); + +enum crypto_pwhash_argon2i_STRBYTES = 128U; +size_t crypto_pwhash_argon2i_strbytes (); + +enum crypto_pwhash_argon2i_STRPREFIX = "$argon2i$"; +const(char)* crypto_pwhash_argon2i_strprefix (); + +enum crypto_pwhash_argon2i_OPSLIMIT_MIN = 3U; +size_t crypto_pwhash_argon2i_opslimit_min (); + +enum crypto_pwhash_argon2i_OPSLIMIT_MAX = 4294967295U; +size_t crypto_pwhash_argon2i_opslimit_max (); + +enum crypto_pwhash_argon2i_MEMLIMIT_MIN = 8192U; +size_t crypto_pwhash_argon2i_memlimit_min (); + +enum crypto_pwhash_argon2i_MEMLIMIT_MAX = (SIZE_MAX >= 4398046510080U) ? 4398046510080U : (SIZE_MAX >= 2147483648U) ? 2147483648U : 32768U; +size_t crypto_pwhash_argon2i_memlimit_max (); + +enum crypto_pwhash_argon2i_OPSLIMIT_INTERACTIVE = 4U; +size_t crypto_pwhash_argon2i_opslimit_interactive (); + +enum crypto_pwhash_argon2i_MEMLIMIT_INTERACTIVE = 33554432U; +size_t crypto_pwhash_argon2i_memlimit_interactive (); + +enum crypto_pwhash_argon2i_OPSLIMIT_MODERATE = 6U; +size_t crypto_pwhash_argon2i_opslimit_moderate (); + +enum crypto_pwhash_argon2i_MEMLIMIT_MODERATE = 134217728U; +size_t crypto_pwhash_argon2i_memlimit_moderate (); + +enum crypto_pwhash_argon2i_OPSLIMIT_SENSITIVE = 8U; +size_t crypto_pwhash_argon2i_opslimit_sensitive (); + +enum crypto_pwhash_argon2i_MEMLIMIT_SENSITIVE = 536870912U; +size_t crypto_pwhash_argon2i_memlimit_sensitive (); + +int crypto_pwhash_argon2i ( + ubyte* out_, + ulong outlen, + const char* passwd, + ulong passwdlen, + const ubyte* salt, + ulong opslimit, + size_t memlimit, + int alg); + +int crypto_pwhash_argon2i_str ( + ref char[crypto_pwhash_argon2i_STRBYTES] out_, + const char* passwd, + ulong passwdlen, + ulong opslimit, + size_t memlimit); + +int crypto_pwhash_argon2i_str_verify ( + ref const(char)[crypto_pwhash_argon2i_STRBYTES] str, + const char* passwd, + ulong passwdlen); + +int crypto_pwhash_argon2i_str_needs_rehash ( + ref const(char)[crypto_pwhash_argon2i_STRBYTES] str, + ulong opslimit, + size_t memlimit); diff --git a/source/libsodium/crypto_pwhash_argon2id.d b/source/libsodium/crypto_pwhash_argon2id.d new file mode 100644 index 0000000..1a27c45 --- /dev/null +++ b/source/libsodium/crypto_pwhash_argon2id.d @@ -0,0 +1,97 @@ +/******************************************************************************* + + D language bindings for libsodium's crypto_pwhash_argon2id.h + + License: ISC (see LICENSE.txt) + +*******************************************************************************/ + +module libsodium.crypto_pwhash_argon2id; + +@nogc nothrow: + +import libsodium.export_; +import core.stdc.stdint; + +extern (C): + +enum crypto_pwhash_argon2id_ALG_ARGON2ID13 = 2; +int crypto_pwhash_argon2id_alg_argon2id13 (); + +enum crypto_pwhash_argon2id_BYTES_MIN = 16U; +size_t crypto_pwhash_argon2id_bytes_min (); + +enum crypto_pwhash_argon2id_BYTES_MAX = SODIUM_MIN(SODIUM_SIZE_MAX, 4294967295U); +size_t crypto_pwhash_argon2id_bytes_max (); + +enum crypto_pwhash_argon2id_PASSWD_MIN = 0U; +size_t crypto_pwhash_argon2id_passwd_min (); + +enum crypto_pwhash_argon2id_PASSWD_MAX = 4294967295U; +size_t crypto_pwhash_argon2id_passwd_max (); + +enum crypto_pwhash_argon2id_SALTBYTES = 16U; +size_t crypto_pwhash_argon2id_saltbytes (); + +enum crypto_pwhash_argon2id_STRBYTES = 128U; +size_t crypto_pwhash_argon2id_strbytes (); + +enum crypto_pwhash_argon2id_STRPREFIX = "$argon2id$"; +const(char)* crypto_pwhash_argon2id_strprefix (); + +enum crypto_pwhash_argon2id_OPSLIMIT_MIN = 1U; +size_t crypto_pwhash_argon2id_opslimit_min (); + +enum crypto_pwhash_argon2id_OPSLIMIT_MAX = 4294967295U; +size_t crypto_pwhash_argon2id_opslimit_max (); + +enum crypto_pwhash_argon2id_MEMLIMIT_MIN = 8192U; +size_t crypto_pwhash_argon2id_memlimit_min (); + +enum crypto_pwhash_argon2id_MEMLIMIT_MAX = (SIZE_MAX >= 4398046510080U) ? 4398046510080U : (SIZE_MAX >= 2147483648U) ? 2147483648U : 32768U; +size_t crypto_pwhash_argon2id_memlimit_max (); + +enum crypto_pwhash_argon2id_OPSLIMIT_INTERACTIVE = 2U; +size_t crypto_pwhash_argon2id_opslimit_interactive (); + +enum crypto_pwhash_argon2id_MEMLIMIT_INTERACTIVE = 67108864U; +size_t crypto_pwhash_argon2id_memlimit_interactive (); + +enum crypto_pwhash_argon2id_OPSLIMIT_MODERATE = 3U; +size_t crypto_pwhash_argon2id_opslimit_moderate (); + +enum crypto_pwhash_argon2id_MEMLIMIT_MODERATE = 268435456U; +size_t crypto_pwhash_argon2id_memlimit_moderate (); + +enum crypto_pwhash_argon2id_OPSLIMIT_SENSITIVE = 4U; +size_t crypto_pwhash_argon2id_opslimit_sensitive (); + +enum crypto_pwhash_argon2id_MEMLIMIT_SENSITIVE = 1073741824U; +size_t crypto_pwhash_argon2id_memlimit_sensitive (); + +int crypto_pwhash_argon2id ( + ubyte* out_, + ulong outlen, + const char* passwd, + ulong passwdlen, + const ubyte* salt, + ulong opslimit, + size_t memlimit, + int alg); + +int crypto_pwhash_argon2id_str ( + ref char[crypto_pwhash_argon2id_STRBYTES] out_, + const char* passwd, + ulong passwdlen, + ulong opslimit, + size_t memlimit); + +int crypto_pwhash_argon2id_str_verify ( + ref const(char)[crypto_pwhash_argon2id_STRBYTES] str, + const char* passwd, + ulong passwdlen); + +int crypto_pwhash_argon2id_str_needs_rehash ( + ref const(char)[crypto_pwhash_argon2id_STRBYTES] str, + ulong opslimit, + size_t memlimit); diff --git a/source/libsodium/crypto_pwhash_scryptsalsa208sha256.d b/source/libsodium/crypto_pwhash_scryptsalsa208sha256.d new file mode 100644 index 0000000..8cbd6ea --- /dev/null +++ b/source/libsodium/crypto_pwhash_scryptsalsa208sha256.d @@ -0,0 +1,98 @@ +/******************************************************************************* + + D language bindings for libsodium's crypto_pwhash_scryptsalsa208sha256.h + + License: ISC (see LICENSE.txt) + +*******************************************************************************/ + +module libsodium.crypto_pwhash_scryptsalsa208sha256; + +@nogc nothrow: + +import libsodium.export_; +import core.stdc.stdint; + +extern (C): + +enum crypto_pwhash_scryptsalsa208sha256_BYTES_MIN = 16U; +size_t crypto_pwhash_scryptsalsa208sha256_bytes_min (); + +enum crypto_pwhash_scryptsalsa208sha256_BYTES_MAX = SODIUM_MIN(SODIUM_SIZE_MAX, 0x1fffffffe0UL); +size_t crypto_pwhash_scryptsalsa208sha256_bytes_max (); + +enum crypto_pwhash_scryptsalsa208sha256_PASSWD_MIN = 0U; +size_t crypto_pwhash_scryptsalsa208sha256_passwd_min (); + +enum crypto_pwhash_scryptsalsa208sha256_PASSWD_MAX = SODIUM_SIZE_MAX; +size_t crypto_pwhash_scryptsalsa208sha256_passwd_max (); + +enum crypto_pwhash_scryptsalsa208sha256_SALTBYTES = 32U; +size_t crypto_pwhash_scryptsalsa208sha256_saltbytes (); + +enum crypto_pwhash_scryptsalsa208sha256_STRBYTES = 102U; +size_t crypto_pwhash_scryptsalsa208sha256_strbytes (); + +enum crypto_pwhash_scryptsalsa208sha256_STRPREFIX = "$7$"; +const(char)* crypto_pwhash_scryptsalsa208sha256_strprefix (); + +enum crypto_pwhash_scryptsalsa208sha256_OPSLIMIT_MIN = 32768U; +size_t crypto_pwhash_scryptsalsa208sha256_opslimit_min (); + +enum crypto_pwhash_scryptsalsa208sha256_OPSLIMIT_MAX = 4294967295U; +size_t crypto_pwhash_scryptsalsa208sha256_opslimit_max (); + +enum crypto_pwhash_scryptsalsa208sha256_MEMLIMIT_MIN = 16777216U; +size_t crypto_pwhash_scryptsalsa208sha256_memlimit_min (); + +enum crypto_pwhash_scryptsalsa208sha256_MEMLIMIT_MAX = SODIUM_MIN(SIZE_MAX, 68719476736UL); +size_t crypto_pwhash_scryptsalsa208sha256_memlimit_max (); + +enum crypto_pwhash_scryptsalsa208sha256_OPSLIMIT_INTERACTIVE = 524288U; +size_t crypto_pwhash_scryptsalsa208sha256_opslimit_interactive (); + +enum crypto_pwhash_scryptsalsa208sha256_MEMLIMIT_INTERACTIVE = 16777216U; +size_t crypto_pwhash_scryptsalsa208sha256_memlimit_interactive (); + +enum crypto_pwhash_scryptsalsa208sha256_OPSLIMIT_SENSITIVE = 33554432U; +size_t crypto_pwhash_scryptsalsa208sha256_opslimit_sensitive (); + +enum crypto_pwhash_scryptsalsa208sha256_MEMLIMIT_SENSITIVE = 1073741824U; +size_t crypto_pwhash_scryptsalsa208sha256_memlimit_sensitive (); + +int crypto_pwhash_scryptsalsa208sha256 ( + ubyte* out_, + ulong outlen, + const char* passwd, + ulong passwdlen, + const ubyte* salt, + ulong opslimit, + size_t memlimit); + +int crypto_pwhash_scryptsalsa208sha256_str ( + ref char[crypto_pwhash_scryptsalsa208sha256_STRBYTES] out_, + const char* passwd, + ulong passwdlen, + ulong opslimit, + size_t memlimit); + +int crypto_pwhash_scryptsalsa208sha256_str_verify ( + ref const(char)[crypto_pwhash_scryptsalsa208sha256_STRBYTES] str, + const char* passwd, + ulong passwdlen); + +int crypto_pwhash_scryptsalsa208sha256_ll ( + const(ubyte)* passwd, + size_t passwdlen, + const(ubyte)* salt, + size_t saltlen, + ulong N, + uint r, + uint p, + ubyte* buf, + size_t buflen); + +int crypto_pwhash_scryptsalsa208sha256_str_needs_rehash ( + ref const(char)[crypto_pwhash_scryptsalsa208sha256_STRBYTES] str, + ulong opslimit, + size_t memlimit); diff --git a/source/libsodium/crypto_scalarmult.d b/source/libsodium/crypto_scalarmult.d new file mode 100644 index 0000000..950b41f --- /dev/null +++ b/source/libsodium/crypto_scalarmult.d @@ -0,0 +1,37 @@ +/******************************************************************************* + + D language bindings for libsodium's crypto_scalarmult.h + + License: ISC (see LICENSE.txt) + +*******************************************************************************/ + +module libsodium.crypto_scalarmult; + +@nogc nothrow: + +import libsodium.export_; +import libsodium.crypto_scalarmult_curve25519; + +extern (C): + +enum crypto_scalarmult_BYTES = crypto_scalarmult_curve25519_BYTES; +size_t crypto_scalarmult_bytes (); + +enum crypto_scalarmult_SCALARBYTES = crypto_scalarmult_curve25519_SCALARBYTES; +size_t crypto_scalarmult_scalarbytes (); + +enum crypto_scalarmult_PRIMITIVE = "curve25519"; +const(char)* crypto_scalarmult_primitive (); + +int crypto_scalarmult_base (ubyte* q, const(ubyte)* n); + +/* + * NOTE: Do not use the result of this function directly. + * + * Hash the result with the public keys in order to compute a shared + * secret key: H(q || client_pk || server_pk) + * + * Or unless this is not an option, use the crypto_kx() API instead. + */ +int crypto_scalarmult (ubyte* q, const(ubyte)* n, const(ubyte)* p); diff --git a/source/libsodium/crypto_scalarmult_curve25519.d b/source/libsodium/crypto_scalarmult_curve25519.d new file mode 100644 index 0000000..0787b4e --- /dev/null +++ b/source/libsodium/crypto_scalarmult_curve25519.d @@ -0,0 +1,33 @@ +/******************************************************************************* + + D language bindings for libsodium's crypto_scalarmult_curve25519.h + + License: ISC (see LICENSE.txt) + +*******************************************************************************/ + +module libsodium.crypto_scalarmult_curve25519; + +@nogc nothrow: + +import libsodium.export_; + +extern (C): + +enum crypto_scalarmult_curve25519_BYTES = 32U; +size_t crypto_scalarmult_curve25519_bytes (); + +enum crypto_scalarmult_curve25519_SCALARBYTES = 32U; +size_t crypto_scalarmult_curve25519_scalarbytes (); + +/* + * NOTE: Do not use the result of this function directly. + * + * Hash the result with the public keys in order to compute a shared + * secret key: H(q || client_pk || server_pk) + * + * Or unless this is not an option, use the crypto_kx() API instead. + */ +int crypto_scalarmult_curve25519 (ubyte* q, const(ubyte)* n, const(ubyte)* p); + +int crypto_scalarmult_curve25519_base (ubyte* q, const(ubyte)* n); diff --git a/source/libsodium/crypto_scalarmult_ed25519.d b/source/libsodium/crypto_scalarmult_ed25519.d new file mode 100644 index 0000000..5626b6c --- /dev/null +++ b/source/libsodium/crypto_scalarmult_ed25519.d @@ -0,0 +1,40 @@ +/******************************************************************************* + + D language bindings for libsodium's crypto_scalarmult_ed25519.h + + License: ISC (see LICENSE.txt) + +*******************************************************************************/ + +module libsodium.crypto_scalarmult_ed25519; + +@nogc nothrow: + +import libsodium.export_; + +extern (C): + +enum crypto_scalarmult_ed25519_BYTES = 32U; +size_t crypto_scalarmult_ed25519_bytes (); + +enum crypto_scalarmult_ed25519_SCALARBYTES = 32U; +size_t crypto_scalarmult_ed25519_scalarbytes (); + +/* + * NOTE: Do not use the result of this function directly. + * + * Hash the result with the public keys in order to compute a shared + * secret key: H(q || client_pk || server_pk) + * + * Or unless this is not an option, use the crypto_kx() API instead. + */ +int crypto_scalarmult_ed25519 (ubyte* q, const(ubyte)* n, const(ubyte)* p); + +int crypto_scalarmult_ed25519_noclamp ( + ubyte* q, + const(ubyte)* n, + const(ubyte)* p); + +int crypto_scalarmult_ed25519_base (ubyte* q, const(ubyte)* n); + +int crypto_scalarmult_ed25519_base_noclamp (ubyte* q, const(ubyte)* n); diff --git a/source/libsodium/crypto_secretbox.d b/source/libsodium/crypto_secretbox.d new file mode 100644 index 0000000..a9a1f43 --- /dev/null +++ b/source/libsodium/crypto_secretbox.d @@ -0,0 +1,85 @@ +/******************************************************************************* + + D language bindings for libsodium's crypto_secretbox.h + + License: ISC (see LICENSE.txt) + +*******************************************************************************/ + +module libsodium.crypto_secretbox; + +@nogc nothrow: + +import libsodium.export_; +import libsodium.crypto_secretbox_xsalsa20poly1305; + +extern (C): + +enum crypto_secretbox_KEYBYTES = crypto_secretbox_xsalsa20poly1305_KEYBYTES; +size_t crypto_secretbox_keybytes (); + +enum crypto_secretbox_NONCEBYTES = crypto_secretbox_xsalsa20poly1305_NONCEBYTES; +size_t crypto_secretbox_noncebytes (); + +enum crypto_secretbox_MACBYTES = crypto_secretbox_xsalsa20poly1305_MACBYTES; +size_t crypto_secretbox_macbytes (); + +enum crypto_secretbox_PRIMITIVE = "xsalsa20poly1305"; +const(char)* crypto_secretbox_primitive (); + +enum crypto_secretbox_MESSAGEBYTES_MAX = crypto_secretbox_xsalsa20poly1305_MESSAGEBYTES_MAX; +size_t crypto_secretbox_messagebytes_max (); + +int crypto_secretbox_easy ( + ubyte* c, + const(ubyte)* m, + ulong mlen, + const(ubyte)* n, + const(ubyte)* k); + +int crypto_secretbox_open_easy ( + ubyte* m, + const(ubyte)* c, + ulong clen, + const(ubyte)* n, + const(ubyte)* k); + +int crypto_secretbox_detached ( + ubyte* c, + ubyte* mac, + const(ubyte)* m, + ulong mlen, + const(ubyte)* n, + const(ubyte)* k); + +int crypto_secretbox_open_detached ( + ubyte* m, + const(ubyte)* c, + const(ubyte)* mac, + ulong clen, + const(ubyte)* n, + const(ubyte)* k); + +void crypto_secretbox_keygen (ref ubyte[crypto_secretbox_KEYBYTES] k); + +/* -- NaCl compatibility interface ; Requires padding -- */ + +enum crypto_secretbox_ZEROBYTES = crypto_secretbox_xsalsa20poly1305_ZEROBYTES; +size_t crypto_secretbox_zerobytes (); + +enum crypto_secretbox_BOXZEROBYTES = crypto_secretbox_xsalsa20poly1305_BOXZEROBYTES; +size_t crypto_secretbox_boxzerobytes (); + +int crypto_secretbox ( + ubyte* c, + const(ubyte)* m, + ulong mlen, + const(ubyte)* n, + const(ubyte)* k); + +int crypto_secretbox_open ( + ubyte* m, + const(ubyte)* c, + ulong clen, + const(ubyte)* n, + const(ubyte)* k); diff --git a/source/libsodium/crypto_secretbox_xchacha20poly1305.d b/source/libsodium/crypto_secretbox_xchacha20poly1305.d new file mode 100644 index 0000000..63f77f3 --- /dev/null +++ b/source/libsodium/crypto_secretbox_xchacha20poly1305.d @@ -0,0 +1,58 @@ +/******************************************************************************* + + D language bindings for libsodium's crypto_secretbox_xchacha20poly1305.h + + License: ISC (see LICENSE.txt) + +*******************************************************************************/ + +module libsodium.crypto_secretbox_xchacha20poly1305; + +@nogc nothrow: + +import libsodium.export_; +import libsodium.crypto_stream_xchacha20; + +extern (C): + +enum crypto_secretbox_xchacha20poly1305_KEYBYTES = 32U; +size_t crypto_secretbox_xchacha20poly1305_keybytes (); + +enum crypto_secretbox_xchacha20poly1305_NONCEBYTES = 24U; +size_t crypto_secretbox_xchacha20poly1305_noncebytes (); + +enum crypto_secretbox_xchacha20poly1305_MACBYTES = 16U; +size_t crypto_secretbox_xchacha20poly1305_macbytes (); + +enum crypto_secretbox_xchacha20poly1305_MESSAGEBYTES_MAX = crypto_stream_xchacha20_MESSAGEBYTES_MAX - crypto_secretbox_xchacha20poly1305_MACBYTES; +size_t crypto_secretbox_xchacha20poly1305_messagebytes_max (); + +int crypto_secretbox_xchacha20poly1305_easy ( + ubyte* c, + const(ubyte)* m, + ulong mlen, + const(ubyte)* n, + const(ubyte)* k); + +int crypto_secretbox_xchacha20poly1305_open_easy ( + ubyte* m, + const(ubyte)* c, + ulong clen, + const(ubyte)* n, + const(ubyte)* k); + +int crypto_secretbox_xchacha20poly1305_detached ( + ubyte* c, + ubyte* mac, + const(ubyte)* m, + ulong mlen, + const(ubyte)* n, + const(ubyte)* k); + +int crypto_secretbox_xchacha20poly1305_open_detached ( + ubyte* m, + const(ubyte)* c, + const(ubyte)* mac, + ulong clen, + const(ubyte)* n, + const(ubyte)* k); diff --git a/source/libsodium/crypto_secretbox_xsalsa20poly1305.d b/source/libsodium/crypto_secretbox_xsalsa20poly1305.d new file mode 100644 index 0000000..f413975 --- /dev/null +++ b/source/libsodium/crypto_secretbox_xsalsa20poly1305.d @@ -0,0 +1,54 @@ +/******************************************************************************* + + D language bindings for libsodium's crypto_secretbox_xsalsa20poly1305.h + + License: ISC (see LICENSE.txt) + +*******************************************************************************/ + +module libsodium.crypto_secretbox_xsalsa20poly1305; + +@nogc nothrow: + +import libsodium.export_; +import libsodium.crypto_stream_xsalsa20; + +extern (C): + +enum crypto_secretbox_xsalsa20poly1305_KEYBYTES = 32U; +size_t crypto_secretbox_xsalsa20poly1305_keybytes (); + +enum crypto_secretbox_xsalsa20poly1305_NONCEBYTES = 24U; +size_t crypto_secretbox_xsalsa20poly1305_noncebytes (); + +enum crypto_secretbox_xsalsa20poly1305_MACBYTES = 16U; +size_t crypto_secretbox_xsalsa20poly1305_macbytes (); + +/* Only for the libsodium API - The NaCl compatibility API would require BOXZEROBYTES extra bytes */ +enum crypto_secretbox_xsalsa20poly1305_MESSAGEBYTES_MAX = crypto_stream_xsalsa20_MESSAGEBYTES_MAX - crypto_secretbox_xsalsa20poly1305_MACBYTES; +size_t crypto_secretbox_xsalsa20poly1305_messagebytes_max (); + +int crypto_secretbox_xsalsa20poly1305 ( + ubyte* c, + const(ubyte)* m, + ulong mlen, + const(ubyte)* n, + const(ubyte)* k); + +int crypto_secretbox_xsalsa20poly1305_open ( + ubyte* m, + const(ubyte)* c, + ulong clen, + const(ubyte)* n, + const(ubyte)* k); + +void crypto_secretbox_xsalsa20poly1305_keygen ( + ref ubyte[crypto_secretbox_xsalsa20poly1305_KEYBYTES] k); + +/* -- NaCl compatibility interface ; Requires padding -- */ + +enum crypto_secretbox_xsalsa20poly1305_BOXZEROBYTES = 16U; +size_t crypto_secretbox_xsalsa20poly1305_boxzerobytes (); + +enum crypto_secretbox_xsalsa20poly1305_ZEROBYTES = crypto_secretbox_xsalsa20poly1305_BOXZEROBYTES + crypto_secretbox_xsalsa20poly1305_MACBYTES; +size_t crypto_secretbox_xsalsa20poly1305_zerobytes (); diff --git a/source/libsodium/crypto_secretstream_xchacha20poly1305.d b/source/libsodium/crypto_secretstream_xchacha20poly1305.d new file mode 100644 index 0000000..f9c14c9 --- /dev/null +++ b/source/libsodium/crypto_secretstream_xchacha20poly1305.d @@ -0,0 +1,86 @@ +/******************************************************************************* + + D language bindings for libsodium's crypto_secretstream_xchacha20poly1305.h + + License: ISC (see LICENSE.txt) + +*******************************************************************************/ + +module libsodium.crypto_secretstream_xchacha20poly1305; + +@nogc nothrow: + +import libsodium.export_; +import libsodium.crypto_aead_xchacha20poly1305; +import libsodium.crypto_stream_chacha20; + +extern (C): + +enum crypto_secretstream_xchacha20poly1305_ABYTES = 1U + crypto_aead_xchacha20poly1305_ietf_ABYTES; +size_t crypto_secretstream_xchacha20poly1305_abytes (); + +enum crypto_secretstream_xchacha20poly1305_HEADERBYTES = crypto_aead_xchacha20poly1305_ietf_NPUBBYTES; +size_t crypto_secretstream_xchacha20poly1305_headerbytes (); + +enum crypto_secretstream_xchacha20poly1305_KEYBYTES = crypto_aead_xchacha20poly1305_ietf_KEYBYTES; +size_t crypto_secretstream_xchacha20poly1305_keybytes (); + +enum crypto_secretstream_xchacha20poly1305_MESSAGEBYTES_MAX = SODIUM_MIN(SODIUM_SIZE_MAX - crypto_secretstream_xchacha20poly1305_ABYTES, 64UL * ((1UL << 32) - 2UL)); +size_t crypto_secretstream_xchacha20poly1305_messagebytes_max (); + +enum crypto_secretstream_xchacha20poly1305_TAG_MESSAGE = 0x00; +ubyte crypto_secretstream_xchacha20poly1305_tag_message (); + +enum crypto_secretstream_xchacha20poly1305_TAG_PUSH = 0x01; +ubyte crypto_secretstream_xchacha20poly1305_tag_push (); + +enum crypto_secretstream_xchacha20poly1305_TAG_REKEY = 0x02; +ubyte crypto_secretstream_xchacha20poly1305_tag_rekey (); + +enum crypto_secretstream_xchacha20poly1305_TAG_FINAL = crypto_secretstream_xchacha20poly1305_TAG_PUSH | crypto_secretstream_xchacha20poly1305_TAG_REKEY; +ubyte crypto_secretstream_xchacha20poly1305_tag_final (); + +struct crypto_secretstream_xchacha20poly1305_state +{ + ubyte[crypto_stream_chacha20_ietf_KEYBYTES] k; + ubyte[crypto_stream_chacha20_ietf_NONCEBYTES] nonce; + ubyte[8] _pad; +} + +size_t crypto_secretstream_xchacha20poly1305_statebytes (); + +void crypto_secretstream_xchacha20poly1305_keygen ( + ref ubyte[crypto_secretstream_xchacha20poly1305_KEYBYTES] k); + +int crypto_secretstream_xchacha20poly1305_init_push ( + crypto_secretstream_xchacha20poly1305_state* state, + ref ubyte[crypto_secretstream_xchacha20poly1305_HEADERBYTES] header, + ref const(ubyte)[crypto_secretstream_xchacha20poly1305_KEYBYTES] k); + +int crypto_secretstream_xchacha20poly1305_push ( + crypto_secretstream_xchacha20poly1305_state* state, + ubyte* c, + ulong* clen_p, + const(ubyte)* m, + ulong mlen, + const(ubyte)* ad, + ulong adlen, + ubyte tag); + +int crypto_secretstream_xchacha20poly1305_init_pull ( + crypto_secretstream_xchacha20poly1305_state* state, + ref const(ubyte)[crypto_secretstream_xchacha20poly1305_HEADERBYTES] header, + ref const(ubyte)[crypto_secretstream_xchacha20poly1305_KEYBYTES] k); + +int crypto_secretstream_xchacha20poly1305_pull ( + crypto_secretstream_xchacha20poly1305_state* state, + ubyte* m, + ulong* mlen_p, + ubyte* tag_p, + const(ubyte)* c, + ulong clen, + const(ubyte)* ad, + ulong adlen); + +void crypto_secretstream_xchacha20poly1305_rekey ( + crypto_secretstream_xchacha20poly1305_state* state); diff --git a/source/libsodium/crypto_shorthash.d b/source/libsodium/crypto_shorthash.d new file mode 100644 index 0000000..c139c61 --- /dev/null +++ b/source/libsodium/crypto_shorthash.d @@ -0,0 +1,33 @@ +/******************************************************************************* + + D language bindings for libsodium's crypto_shorthash.h + + License: ISC (see LICENSE.txt) + +*******************************************************************************/ + +module libsodium.crypto_shorthash; + +@nogc nothrow: + +import libsodium.export_; +import libsodium.crypto_shorthash_siphash24; + +extern (C): + +enum crypto_shorthash_BYTES = crypto_shorthash_siphash24_BYTES; +size_t crypto_shorthash_bytes (); + +enum crypto_shorthash_KEYBYTES = crypto_shorthash_siphash24_KEYBYTES; +size_t crypto_shorthash_keybytes (); + +enum crypto_shorthash_PRIMITIVE = "siphash24"; +const(char)* crypto_shorthash_primitive (); + +int crypto_shorthash ( + ubyte* out_, + const(ubyte)* in_, + ulong inlen, + const(ubyte)* k); + +void crypto_shorthash_keygen (ref ubyte[crypto_shorthash_KEYBYTES] k); diff --git a/source/libsodium/crypto_shorthash_siphash24.d b/source/libsodium/crypto_shorthash_siphash24.d new file mode 100644 index 0000000..bdb6e89 --- /dev/null +++ b/source/libsodium/crypto_shorthash_siphash24.d @@ -0,0 +1,43 @@ +/******************************************************************************* + + D language bindings for libsodium's crypto_shorthash_siphash24.h + + License: ISC (see LICENSE.txt) + +*******************************************************************************/ + +module libsodium.crypto_shorthash_siphash24; + +@nogc nothrow: + +import libsodium.export_; + +extern (C): + +/* -- 64-bit output -- */ + +enum crypto_shorthash_siphash24_BYTES = 8U; +size_t crypto_shorthash_siphash24_bytes (); + +enum crypto_shorthash_siphash24_KEYBYTES = 16U; +size_t crypto_shorthash_siphash24_keybytes (); + +int crypto_shorthash_siphash24 ( + ubyte* out_, + const(ubyte)* in_, + ulong inlen, + const(ubyte)* k); + +/* -- 128-bit output -- */ + +enum crypto_shorthash_siphashx24_BYTES = 16U; +size_t crypto_shorthash_siphashx24_bytes (); + +enum crypto_shorthash_siphashx24_KEYBYTES = 16U; +size_t crypto_shorthash_siphashx24_keybytes (); + +int crypto_shorthash_siphashx24 ( + ubyte* out_, + const(ubyte)* in_, + ulong inlen, + const(ubyte)* k); diff --git a/source/libsodium/crypto_sign.d b/source/libsodium/crypto_sign.d new file mode 100644 index 0000000..f863ef9 --- /dev/null +++ b/source/libsodium/crypto_sign.d @@ -0,0 +1,91 @@ +/******************************************************************************* + + D language bindings for libsodium's crypto_sign.h + + License: ISC (see LICENSE.txt) + +*******************************************************************************/ + +module libsodium.crypto_sign; + +@nogc nothrow: + +import libsodium.export_; +import libsodium.crypto_sign_ed25519; + +extern (C): + +/* + * THREAD SAFETY: crypto_sign_keypair() is thread-safe, + * provided that sodium_init() was called before. + * + * Other functions, including crypto_sign_seed_keypair() are always thread-safe. + */ + +alias crypto_sign_state = crypto_sign_ed25519ph_state; + +size_t crypto_sign_statebytes (); + +enum crypto_sign_BYTES = crypto_sign_ed25519_BYTES; +size_t crypto_sign_bytes (); + +enum crypto_sign_SEEDBYTES = crypto_sign_ed25519_SEEDBYTES; +size_t crypto_sign_seedbytes (); + +enum crypto_sign_PUBLICKEYBYTES = crypto_sign_ed25519_PUBLICKEYBYTES; +size_t crypto_sign_publickeybytes (); + +enum crypto_sign_SECRETKEYBYTES = crypto_sign_ed25519_SECRETKEYBYTES; +size_t crypto_sign_secretkeybytes (); + +enum crypto_sign_MESSAGEBYTES_MAX = crypto_sign_ed25519_MESSAGEBYTES_MAX; +size_t crypto_sign_messagebytes_max (); + +enum crypto_sign_PRIMITIVE = "ed25519"; +const(char)* crypto_sign_primitive (); + +int crypto_sign_seed_keypair (ubyte* pk, ubyte* sk, const(ubyte)* seed); + +int crypto_sign_keypair (ubyte* pk, ubyte* sk); + +int crypto_sign ( + ubyte* sm, + ulong* smlen_p, + const(ubyte)* m, + ulong mlen, + const(ubyte)* sk); + +int crypto_sign_open ( + ubyte* m, + ulong* mlen_p, + const(ubyte)* sm, + ulong smlen, + const(ubyte)* pk); + +int crypto_sign_detached ( + ubyte* sig, + ulong* siglen_p, + const(ubyte)* m, + ulong mlen, + const(ubyte)* sk); + +int crypto_sign_verify_detached ( + const(ubyte)* sig, + const(ubyte)* m, + ulong mlen, + const(ubyte)* pk); + +int crypto_sign_init (crypto_sign_state* state); + +int crypto_sign_update (crypto_sign_state* state, const(ubyte)* m, ulong mlen); + +int crypto_sign_final_create ( + crypto_sign_state* state, + ubyte* sig, + ulong* siglen_p, + const(ubyte)* sk); + +int crypto_sign_final_verify ( + crypto_sign_state* state, + const(ubyte)* sig, + const(ubyte)* pk); diff --git a/source/libsodium/crypto_sign_ed25519.d b/source/libsodium/crypto_sign_ed25519.d new file mode 100644 index 0000000..28b8a58 --- /dev/null +++ b/source/libsodium/crypto_sign_ed25519.d @@ -0,0 +1,99 @@ +/******************************************************************************* + + D language bindings for libsodium's crypto_sign_ed25519.h + + License: ISC (see LICENSE.txt) + +*******************************************************************************/ + +module libsodium.crypto_sign_ed25519; + +@nogc nothrow: + +import libsodium.export_; +import libsodium.crypto_hash_sha512; + +extern (C): + +struct crypto_sign_ed25519ph_state +{ + crypto_hash_sha512_state hs; +} + +size_t crypto_sign_ed25519ph_statebytes (); + +enum crypto_sign_ed25519_BYTES = 64U; +size_t crypto_sign_ed25519_bytes (); + +enum crypto_sign_ed25519_SEEDBYTES = 32U; +size_t crypto_sign_ed25519_seedbytes (); + +enum crypto_sign_ed25519_PUBLICKEYBYTES = 32U; +size_t crypto_sign_ed25519_publickeybytes (); + +enum crypto_sign_ed25519_SECRETKEYBYTES = 32U + 32U; +size_t crypto_sign_ed25519_secretkeybytes (); + +enum crypto_sign_ed25519_MESSAGEBYTES_MAX = SODIUM_SIZE_MAX - crypto_sign_ed25519_BYTES; +size_t crypto_sign_ed25519_messagebytes_max (); + +int crypto_sign_ed25519 ( + ubyte* sm, + ulong* smlen_p, + const(ubyte)* m, + ulong mlen, + const(ubyte)* sk); + +int crypto_sign_ed25519_open ( + ubyte* m, + ulong* mlen_p, + const(ubyte)* sm, + ulong smlen, + const(ubyte)* pk); + +int crypto_sign_ed25519_detached ( + ubyte* sig, + ulong* siglen_p, + const(ubyte)* m, + ulong mlen, + const(ubyte)* sk); + +int crypto_sign_ed25519_verify_detached ( + const(ubyte)* sig, + const(ubyte)* m, + ulong mlen, + const(ubyte)* pk); + +int crypto_sign_ed25519_keypair (ubyte* pk, ubyte* sk); + +int crypto_sign_ed25519_seed_keypair (ubyte* pk, ubyte* sk, const(ubyte)* seed); + +int crypto_sign_ed25519_pk_to_curve25519 ( + ubyte* curve25519_pk, + const(ubyte)* ed25519_pk); + +int crypto_sign_ed25519_sk_to_curve25519 ( + ubyte* curve25519_sk, + const(ubyte)* ed25519_sk); + +int crypto_sign_ed25519_sk_to_seed (ubyte* seed, const(ubyte)* sk); + +int crypto_sign_ed25519_sk_to_pk (ubyte* pk, const(ubyte)* sk); + +int crypto_sign_ed25519ph_init (crypto_sign_ed25519ph_state* state); + +int crypto_sign_ed25519ph_update ( + crypto_sign_ed25519ph_state* state, + const(ubyte)* m, + ulong mlen); + +int crypto_sign_ed25519ph_final_create ( + crypto_sign_ed25519ph_state* state, + ubyte* sig, + ulong* siglen_p, + const(ubyte)* sk); + +int crypto_sign_ed25519ph_final_verify ( + crypto_sign_ed25519ph_state* state, + const(ubyte)* sig, + const(ubyte)* pk); diff --git a/source/libsodium/crypto_sign_edwards25519sha512batch.d b/source/libsodium/crypto_sign_edwards25519sha512batch.d new file mode 100644 index 0000000..0f192be --- /dev/null +++ b/source/libsodium/crypto_sign_edwards25519sha512batch.d @@ -0,0 +1,47 @@ +/******************************************************************************* + + D language bindings for libsodium's crypto_sign_edwards25519sha512batch.h + + License: ISC (see LICENSE.txt) + +*******************************************************************************/ + +module libsodium.crypto_sign_edwards25519sha512batch; + +@nogc nothrow: + +import libsodium.export_; + +extern (C): + +/* + * WARNING: This construction was a prototype, which should not be used + * any more in new projects. + * + * crypto_sign_edwards25519sha512batch is provided for applications + * initially built with NaCl, but as recommended by the author of this + * construction, new applications should use ed25519 instead. + * + * In Sodium, you should use the high-level crypto_sign_*() functions instead. + */ + +enum crypto_sign_edwards25519sha512batch_BYTES = 64U; +enum crypto_sign_edwards25519sha512batch_PUBLICKEYBYTES = 32U; +enum crypto_sign_edwards25519sha512batch_SECRETKEYBYTES = 32U + 32U; +enum crypto_sign_edwards25519sha512batch_MESSAGEBYTES_MAX = SODIUM_SIZE_MAX - crypto_sign_edwards25519sha512batch_BYTES; + +int crypto_sign_edwards25519sha512batch ( + ubyte* sm, + ulong* smlen_p, + const(ubyte)* m, + ulong mlen, + const(ubyte)* sk); + +int crypto_sign_edwards25519sha512batch_open ( + ubyte* m, + ulong* mlen_p, + const(ubyte)* sm, + ulong smlen, + const(ubyte)* pk); + +int crypto_sign_edwards25519sha512batch_keypair (ubyte* pk, ubyte* sk); diff --git a/source/libsodium/crypto_stream.d b/source/libsodium/crypto_stream.d new file mode 100644 index 0000000..52f5f41 --- /dev/null +++ b/source/libsodium/crypto_stream.d @@ -0,0 +1,47 @@ +/******************************************************************************* + + D language bindings for libsodium's crypto_stream.h + + License: ISC (see LICENSE.txt) + +*******************************************************************************/ + +module libsodium.crypto_stream; + +@nogc nothrow: + +import libsodium.export_; +import libsodium.crypto_stream_xsalsa20; + +extern (C): + +/* + * WARNING: This is just a stream cipher. It is NOT authenticated encryption. + * While it provides some protection against eavesdropping, it does NOT + * provide any security against active attacks. + * Unless you know what you're doing, what you are looking for is probably + * the crypto_box functions. + */ + +enum crypto_stream_KEYBYTES = crypto_stream_xsalsa20_KEYBYTES; +size_t crypto_stream_keybytes (); + +enum crypto_stream_NONCEBYTES = crypto_stream_xsalsa20_NONCEBYTES; +size_t crypto_stream_noncebytes (); + +enum crypto_stream_MESSAGEBYTES_MAX = crypto_stream_xsalsa20_MESSAGEBYTES_MAX; +size_t crypto_stream_messagebytes_max (); + +enum crypto_stream_PRIMITIVE = "xsalsa20"; +const(char)* crypto_stream_primitive (); + +int crypto_stream (ubyte* c, ulong clen, const(ubyte)* n, const(ubyte)* k); + +int crypto_stream_xor ( + ubyte* c, + const(ubyte)* m, + ulong mlen, + const(ubyte)* n, + const(ubyte)* k); + +void crypto_stream_keygen (ref ubyte[crypto_stream_KEYBYTES] k); diff --git a/source/libsodium/crypto_stream_chacha20.d b/source/libsodium/crypto_stream_chacha20.d new file mode 100644 index 0000000..fedf9a3 --- /dev/null +++ b/source/libsodium/crypto_stream_chacha20.d @@ -0,0 +1,99 @@ +/******************************************************************************* + + D language bindings for libsodium's crypto_stream_chacha20.h + + License: ISC (see LICENSE.txt) + +*******************************************************************************/ + +module libsodium.crypto_stream_chacha20; + +@nogc nothrow: + +import libsodium.export_; + +extern (C): + +/* + * WARNING: This is just a stream cipher. It is NOT authenticated encryption. + * While it provides some protection against eavesdropping, it does NOT + * provide any security against active attacks. + * Unless you know what you're doing, what you are looking for is probably + * the crypto_box functions. + */ + +enum crypto_stream_chacha20_KEYBYTES = 32U; +size_t crypto_stream_chacha20_keybytes (); + +enum crypto_stream_chacha20_NONCEBYTES = 8U; +size_t crypto_stream_chacha20_noncebytes (); + +enum crypto_stream_chacha20_MESSAGEBYTES_MAX = SODIUM_SIZE_MAX; +size_t crypto_stream_chacha20_messagebytes_max (); + +/* ChaCha20 with a 64-bit nonce and a 64-bit counter, as originally designed */ + +int crypto_stream_chacha20 ( + ubyte* c, + ulong clen, + const(ubyte)* n, + const(ubyte)* k); + +int crypto_stream_chacha20_xor ( + ubyte* c, + const(ubyte)* m, + ulong mlen, + const(ubyte)* n, + const(ubyte)* k); + +int crypto_stream_chacha20_xor_ic ( + ubyte* c, + const(ubyte)* m, + ulong mlen, + const(ubyte)* n, + ulong ic, + const(ubyte)* k); + +void crypto_stream_chacha20_keygen ( + ref ubyte[crypto_stream_chacha20_KEYBYTES] k); + +/* ChaCha20 with a 96-bit nonce and a 32-bit counter (IETF) */ + +enum crypto_stream_chacha20_ietf_KEYBYTES = 32U; +size_t crypto_stream_chacha20_ietf_keybytes (); + +enum crypto_stream_chacha20_ietf_NONCEBYTES = 12U; +size_t crypto_stream_chacha20_ietf_noncebytes (); + +enum crypto_stream_chacha20_ietf_MESSAGEBYTES_MAX = SODIUM_MIN(SODIUM_SIZE_MAX, 64UL * (1UL << 32)); +size_t crypto_stream_chacha20_ietf_messagebytes_max (); + +int crypto_stream_chacha20_ietf ( + ubyte* c, + ulong clen, + const(ubyte)* n, + const(ubyte)* k); + +int crypto_stream_chacha20_ietf_xor ( + ubyte* c, + const(ubyte)* m, + ulong mlen, + const(ubyte)* n, + const(ubyte)* k); + +int crypto_stream_chacha20_ietf_xor_ic ( + ubyte* c, + const(ubyte)* m, + ulong mlen, + const(ubyte)* n, + uint ic, + const(ubyte)* k); + +void crypto_stream_chacha20_ietf_keygen ( + ref ubyte[crypto_stream_chacha20_ietf_KEYBYTES] k); + +/* Aliases */ + +enum crypto_stream_chacha20_IETF_KEYBYTES = crypto_stream_chacha20_ietf_KEYBYTES; +enum crypto_stream_chacha20_IETF_NONCEBYTES = crypto_stream_chacha20_ietf_NONCEBYTES; +enum crypto_stream_chacha20_IETF_MESSAGEBYTES_MAX = crypto_stream_chacha20_ietf_MESSAGEBYTES_MAX; diff --git a/source/libsodium/crypto_stream_salsa20.d b/source/libsodium/crypto_stream_salsa20.d new file mode 100644 index 0000000..307eceb --- /dev/null +++ b/source/libsodium/crypto_stream_salsa20.d @@ -0,0 +1,55 @@ +/******************************************************************************* + + D language bindings for libsodium's crypto_stream_salsa20.h + + License: ISC (see LICENSE.txt) + +*******************************************************************************/ + +module libsodium.crypto_stream_salsa20; + +@nogc nothrow: + +import libsodium.export_; + +extern (C): + +/* + * WARNING: This is just a stream cipher. It is NOT authenticated encryption. + * While it provides some protection against eavesdropping, it does NOT + * provide any security against active attacks. + * Unless you know what you're doing, what you are looking for is probably + * the crypto_box functions. + */ + +enum crypto_stream_salsa20_KEYBYTES = 32U; +size_t crypto_stream_salsa20_keybytes (); + +enum crypto_stream_salsa20_NONCEBYTES = 8U; +size_t crypto_stream_salsa20_noncebytes (); + +enum crypto_stream_salsa20_MESSAGEBYTES_MAX = SODIUM_SIZE_MAX; +size_t crypto_stream_salsa20_messagebytes_max (); + +int crypto_stream_salsa20 ( + ubyte* c, + ulong clen, + const(ubyte)* n, + const(ubyte)* k); + +int crypto_stream_salsa20_xor ( + ubyte* c, + const(ubyte)* m, + ulong mlen, + const(ubyte)* n, + const(ubyte)* k); + +int crypto_stream_salsa20_xor_ic ( + ubyte* c, + const(ubyte)* m, + ulong mlen, + const(ubyte)* n, + ulong ic, + const(ubyte)* k); + +void crypto_stream_salsa20_keygen (ref ubyte[crypto_stream_salsa20_KEYBYTES] k); diff --git a/source/libsodium/crypto_stream_salsa2012.d b/source/libsodium/crypto_stream_salsa2012.d new file mode 100644 index 0000000..2005f11 --- /dev/null +++ b/source/libsodium/crypto_stream_salsa2012.d @@ -0,0 +1,48 @@ +/******************************************************************************* + + D language bindings for libsodium's crypto_stream_salsa2012.h + + License: ISC (see LICENSE.txt) + +*******************************************************************************/ + +module libsodium.crypto_stream_salsa2012; + +@nogc nothrow: + +import libsodium.export_; + +extern (C): + +/* + * WARNING: This is just a stream cipher. It is NOT authenticated encryption. + * While it provides some protection against eavesdropping, it does NOT + * provide any security against active attacks. + * Unless you know what you're doing, what you are looking for is probably + * the crypto_box functions. + */ + +enum crypto_stream_salsa2012_KEYBYTES = 32U; +size_t crypto_stream_salsa2012_keybytes (); + +enum crypto_stream_salsa2012_NONCEBYTES = 8U; +size_t crypto_stream_salsa2012_noncebytes (); + +enum crypto_stream_salsa2012_MESSAGEBYTES_MAX = SODIUM_SIZE_MAX; +size_t crypto_stream_salsa2012_messagebytes_max (); + +int crypto_stream_salsa2012 ( + ubyte* c, + ulong clen, + const(ubyte)* n, + const(ubyte)* k); + +int crypto_stream_salsa2012_xor ( + ubyte* c, + const(ubyte)* m, + ulong mlen, + const(ubyte)* n, + const(ubyte)* k); + +void crypto_stream_salsa2012_keygen ( + ref ubyte[crypto_stream_salsa2012_KEYBYTES] k); diff --git a/source/libsodium/crypto_stream_salsa208.d b/source/libsodium/crypto_stream_salsa208.d new file mode 100644 index 0000000..e9c020d --- /dev/null +++ b/source/libsodium/crypto_stream_salsa208.d @@ -0,0 +1,48 @@ +/******************************************************************************* + + D language bindings for libsodium's crypto_stream_salsa208.h + + License: ISC (see LICENSE.txt) + +*******************************************************************************/ + +module libsodium.crypto_stream_salsa208; + +@nogc nothrow: + +import libsodium.export_; + +extern (C): + +/* + * WARNING: This is just a stream cipher. It is NOT authenticated encryption. + * While it provides some protection against eavesdropping, it does NOT + * provide any security against active attacks. + * Unless you know what you're doing, what you are looking for is probably + * the crypto_box functions. + */ + +enum crypto_stream_salsa208_KEYBYTES = 32U; +size_t crypto_stream_salsa208_keybytes (); + +enum crypto_stream_salsa208_NONCEBYTES = 8U; +size_t crypto_stream_salsa208_noncebytes (); + +enum crypto_stream_salsa208_MESSAGEBYTES_MAX = SODIUM_SIZE_MAX; +size_t crypto_stream_salsa208_messagebytes_max (); + +int crypto_stream_salsa208 ( + ubyte* c, + ulong clen, + const(ubyte)* n, + const(ubyte)* k); + +int crypto_stream_salsa208_xor ( + ubyte* c, + const(ubyte)* m, + ulong mlen, + const(ubyte)* n, + const(ubyte)* k); + +void crypto_stream_salsa208_keygen ( + ref ubyte[crypto_stream_salsa208_KEYBYTES] k); diff --git a/source/libsodium/crypto_stream_xchacha20.d b/source/libsodium/crypto_stream_xchacha20.d new file mode 100644 index 0000000..031062c --- /dev/null +++ b/source/libsodium/crypto_stream_xchacha20.d @@ -0,0 +1,56 @@ +/******************************************************************************* + + D language bindings for libsodium's crypto_stream_xchacha20.h + + License: ISC (see LICENSE.txt) + +*******************************************************************************/ + +module libsodium.crypto_stream_xchacha20; + +@nogc nothrow: + +import libsodium.export_; + +extern (C): + +/* + * WARNING: This is just a stream cipher. It is NOT authenticated encryption. + * While it provides some protection against eavesdropping, it does NOT + * provide any security against active attacks. + * Unless you know what you're doing, what you are looking for is probably + * the crypto_box functions. + */ + +enum crypto_stream_xchacha20_KEYBYTES = 32U; +size_t crypto_stream_xchacha20_keybytes (); + +enum crypto_stream_xchacha20_NONCEBYTES = 24U; +size_t crypto_stream_xchacha20_noncebytes (); + +enum crypto_stream_xchacha20_MESSAGEBYTES_MAX = SODIUM_SIZE_MAX; +size_t crypto_stream_xchacha20_messagebytes_max (); + +int crypto_stream_xchacha20 ( + ubyte* c, + ulong clen, + const(ubyte)* n, + const(ubyte)* k); + +int crypto_stream_xchacha20_xor ( + ubyte* c, + const(ubyte)* m, + ulong mlen, + const(ubyte)* n, + const(ubyte)* k); + +int crypto_stream_xchacha20_xor_ic ( + ubyte* c, + const(ubyte)* m, + ulong mlen, + const(ubyte)* n, + ulong ic, + const(ubyte)* k); + +void crypto_stream_xchacha20_keygen ( + ref ubyte[crypto_stream_xchacha20_KEYBYTES] k); diff --git a/source/libsodium/crypto_stream_xsalsa20.d b/source/libsodium/crypto_stream_xsalsa20.d new file mode 100644 index 0000000..919bce8 --- /dev/null +++ b/source/libsodium/crypto_stream_xsalsa20.d @@ -0,0 +1,56 @@ +/******************************************************************************* + + D language bindings for libsodium's crypto_stream_xsalsa20.h + + License: ISC (see LICENSE.txt) + +*******************************************************************************/ + +module libsodium.crypto_stream_xsalsa20; + +@nogc nothrow: + +import libsodium.export_; + +extern (C): + +/* + * WARNING: This is just a stream cipher. It is NOT authenticated encryption. + * While it provides some protection against eavesdropping, it does NOT + * provide any security against active attacks. + * Unless you know what you're doing, what you are looking for is probably + * the crypto_box functions. + */ + +enum crypto_stream_xsalsa20_KEYBYTES = 32U; +size_t crypto_stream_xsalsa20_keybytes (); + +enum crypto_stream_xsalsa20_NONCEBYTES = 24U; +size_t crypto_stream_xsalsa20_noncebytes (); + +enum crypto_stream_xsalsa20_MESSAGEBYTES_MAX = SODIUM_SIZE_MAX; +size_t crypto_stream_xsalsa20_messagebytes_max (); + +int crypto_stream_xsalsa20 ( + ubyte* c, + ulong clen, + const(ubyte)* n, + const(ubyte)* k); + +int crypto_stream_xsalsa20_xor ( + ubyte* c, + const(ubyte)* m, + ulong mlen, + const(ubyte)* n, + const(ubyte)* k); + +int crypto_stream_xsalsa20_xor_ic ( + ubyte* c, + const(ubyte)* m, + ulong mlen, + const(ubyte)* n, + ulong ic, + const(ubyte)* k); + +void crypto_stream_xsalsa20_keygen ( + ref ubyte[crypto_stream_xsalsa20_KEYBYTES] k); diff --git a/source/libsodium/crypto_verify_16.d b/source/libsodium/crypto_verify_16.d new file mode 100644 index 0000000..a210846 --- /dev/null +++ b/source/libsodium/crypto_verify_16.d @@ -0,0 +1,20 @@ +/******************************************************************************* + + D language bindings for libsodium's crypto_verify_16.h + + License: ISC (see LICENSE.txt) + +*******************************************************************************/ + +module libsodium.crypto_verify_16; + +@nogc nothrow: + +import libsodium.export_; + +extern (C): + +enum crypto_verify_16_BYTES = 16U; +size_t crypto_verify_16_bytes (); + +int crypto_verify_16 (const(ubyte)* x, const(ubyte)* y); diff --git a/source/libsodium/crypto_verify_32.d b/source/libsodium/crypto_verify_32.d new file mode 100644 index 0000000..7f9f26c --- /dev/null +++ b/source/libsodium/crypto_verify_32.d @@ -0,0 +1,20 @@ +/******************************************************************************* + + D language bindings for libsodium's crypto_verify_32.h + + License: ISC (see LICENSE.txt) + +*******************************************************************************/ + +module libsodium.crypto_verify_32; + +@nogc nothrow: + +import libsodium.export_; + +extern (C): + +enum crypto_verify_32_BYTES = 32U; +size_t crypto_verify_32_bytes (); + +int crypto_verify_32 (const(ubyte)* x, const(ubyte)* y); diff --git a/source/libsodium/crypto_verify_64.d b/source/libsodium/crypto_verify_64.d new file mode 100644 index 0000000..b15ccf5 --- /dev/null +++ b/source/libsodium/crypto_verify_64.d @@ -0,0 +1,20 @@ +/******************************************************************************* + + D language bindings for libsodium's crypto_verify_64.h + + License: ISC (see LICENSE.txt) + +*******************************************************************************/ + +module libsodium.crypto_verify_64; + +@nogc nothrow: + +import libsodium.export_; + +extern (C): + +enum crypto_verify_64_BYTES = 64U; +size_t crypto_verify_64_bytes (); + +int crypto_verify_64 (const(ubyte)* x, const(ubyte)* y); diff --git a/source/libsodium/export_.d b/source/libsodium/export_.d new file mode 100644 index 0000000..4bd20e9 --- /dev/null +++ b/source/libsodium/export_.d @@ -0,0 +1,21 @@ +/******************************************************************************* + + D language bindings for libsodium's export_.h + + License: ISC (see LICENSE.txt) + +*******************************************************************************/ + +module libsodium.export_; + +import libsodium.export_; +import core.stdc.stdint; + +extern (C): + +extern (D) auto SODIUM_MIN(T0, T1)(auto ref T0 A, auto ref T1 B) +{ + return A < B ? A : B; +} + +enum SODIUM_SIZE_MAX = SODIUM_MIN(UINT64_MAX, SIZE_MAX); diff --git a/source/libsodium/package.d b/source/libsodium/package.d new file mode 100644 index 0000000..9c00289 --- /dev/null +++ b/source/libsodium/package.d @@ -0,0 +1,129 @@ +/******************************************************************************* + + D language bindings for libsodium's sodium.h + + License: ISC (see LICENSE.txt) + +*******************************************************************************/ + +module libsodium; + +public import libsodium.core; +public import libsodium.crypto_aead_aes256gcm; +public import libsodium.crypto_aead_chacha20poly1305; +public import libsodium.crypto_aead_xchacha20poly1305; +public import libsodium.crypto_auth; +public import libsodium.crypto_auth_hmacsha256; +public import libsodium.crypto_auth_hmacsha512256; +public import libsodium.crypto_auth_hmacsha512; +public import libsodium.crypto_box; +public import libsodium.crypto_box_curve25519xchacha20poly1305; +public import libsodium.crypto_box_curve25519xsalsa20poly1305; +public import libsodium.crypto_core_ed25519; +public import libsodium.crypto_core_hchacha20; +public import libsodium.crypto_core_hsalsa20; +public import libsodium.crypto_core_salsa2012; +public import libsodium.crypto_core_salsa208; +public import libsodium.crypto_core_salsa20; +public import libsodium.crypto_generichash; +public import libsodium.crypto_generichash_blake2b; +public import libsodium.crypto_hash; +public import libsodium.crypto_hash_sha256; +public import libsodium.crypto_hash_sha512; +public import libsodium.crypto_kdf; +public import libsodium.crypto_kdf_blake2b; +public import libsodium.crypto_kx; +public import libsodium.crypto_onetimeauth; +public import libsodium.crypto_onetimeauth_poly1305; +public import libsodium.crypto_pwhash; +public import libsodium.crypto_pwhash_argon2i; +public import libsodium.crypto_pwhash_argon2id; +public import libsodium.crypto_pwhash_scryptsalsa208sha256; +public import libsodium.crypto_scalarmult; +public import libsodium.crypto_scalarmult_curve25519; +public import libsodium.crypto_scalarmult_ed25519; +public import libsodium.crypto_secretbox; +public import libsodium.crypto_secretbox_xchacha20poly1305; +public import libsodium.crypto_secretbox_xsalsa20poly1305; +public import libsodium.crypto_secretstream_xchacha20poly1305; +public import libsodium.crypto_shorthash; +public import libsodium.crypto_shorthash_siphash24; +public import libsodium.crypto_sign; +public import libsodium.crypto_sign_ed25519; +public import libsodium.crypto_sign_edwards25519sha512batch; +public import libsodium.crypto_stream; +public import libsodium.crypto_stream_chacha20; +public import libsodium.crypto_stream_salsa2012; +public import libsodium.crypto_stream_salsa208; +public import libsodium.crypto_stream_salsa20; +public import libsodium.crypto_stream_xchacha20; +public import libsodium.crypto_stream_xsalsa20; +public import libsodium.crypto_verify_16; +public import libsodium.crypto_verify_32; +public import libsodium.crypto_verify_64; +public import libsodium.export_; +public import libsodium.randombytes; +public import libsodium.randombytes_nativeclient; +public import libsodium.randombytes_salsa20_random; +public import libsodium.randombytes_sysrandom; +public import libsodium.runtime; +public import libsodium.utils; + +version (unittest) +shared static this () +{ + auto res = sodium_init(); + assert(res >= 0); +} + +/// https://libsodium.gitbook.io/doc/secret-key_cryptography/authenticated_encryption +unittest +{ + static immutable message = "Hello world"; + + ubyte[crypto_secretbox_KEYBYTES] key; + ubyte[crypto_secretbox_NONCEBYTES] nonce; + ubyte[crypto_secretbox_MACBYTES + message.length] cipher; + + crypto_secretbox_keygen(key); + randombytes_buf(nonce.ptr, nonce.length); + crypto_secretbox_easy(cipher.ptr, cast(const(ubyte*))message.ptr, message.length, + nonce.ptr, key.ptr); + + ubyte[message.length] decrypted; + if (crypto_secretbox_open_easy(decrypted.ptr, cipher.ptr, cipher.length, + nonce.ptr, key.ptr) != 0) { + assert(0); + } +} + +/// https://libsodium.gitbook.io/doc/public-key_cryptography/authenticated_encryption +unittest +{ + static immutable message = "authenticated encryption"; + + ubyte[crypto_box_PUBLICKEYBYTES] alice_publickey; + ubyte[crypto_box_SECRETKEYBYTES] alice_secretkey; + crypto_box_keypair(alice_publickey.ptr, alice_secretkey.ptr); + + ubyte[crypto_box_PUBLICKEYBYTES] bob_publickey; + ubyte[crypto_box_SECRETKEYBYTES] bob_secretkey; + crypto_box_keypair(bob_publickey.ptr, bob_secretkey.ptr); + + ubyte[crypto_box_NONCEBYTES] nonce; + ubyte[crypto_box_MACBYTES + message.length] ciphertext; + randombytes_buf(nonce.ptr, nonce.length); + + if (crypto_box_easy(ciphertext.ptr, cast(const(ubyte*))message.ptr, + message.length, nonce.ptr, + bob_publickey.ptr, alice_secretkey.ptr) != 0) { + assert(0); + } + + ubyte[message.length] decrypted; + if (crypto_box_open_easy( + decrypted.ptr, ciphertext.ptr, ciphertext.length, nonce.ptr, + alice_publickey.ptr, bob_secretkey.ptr) != 0) { + assert(0); + } +} diff --git a/source/libsodium/randombytes.d b/source/libsodium/randombytes.d new file mode 100644 index 0000000..6382570 --- /dev/null +++ b/source/libsodium/randombytes.d @@ -0,0 +1,53 @@ +/******************************************************************************* + + D language bindings for libsodium's randombytes.h + + License: ISC (see LICENSE.txt) + +*******************************************************************************/ + +module libsodium.randombytes; + +@nogc nothrow: + +import libsodium.export_; + +extern (C): + +struct randombytes_implementation +{ + const(char)* function () implementation_name; /* required */ + uint function () random; /* required */ + void function () stir; /* optional */ + uint function (const uint upper_bound) uniform; /* optional, a default implementation will be used if NULL */ + void function (void* buf, const size_t size) buf; /* required */ + int function () close; /* optional */ +} + +enum randombytes_BYTES_MAX = SODIUM_MIN(SODIUM_SIZE_MAX, 0xffffffffUL); + +enum randombytes_SEEDBYTES = 32U; +size_t randombytes_seedbytes (); + +void randombytes_buf (void* buf, const size_t size); + +void randombytes_buf_deterministic ( + void* buf, + const size_t size, + ref const(ubyte)[randombytes_SEEDBYTES] seed); + +uint randombytes_random (); + +uint randombytes_uniform (const uint upper_bound); + +void randombytes_stir (); + +int randombytes_close (); + +int randombytes_set_implementation (randombytes_implementation* impl); + +const(char)* randombytes_implementation_name (); + +/* -- NaCl compatibility interface -- */ + +void randombytes (ubyte* buf, const ulong buf_len); diff --git a/source/libsodium/randombytes_nativeclient.d b/source/libsodium/randombytes_nativeclient.d new file mode 100644 index 0000000..ca6d231 --- /dev/null +++ b/source/libsodium/randombytes_nativeclient.d @@ -0,0 +1,15 @@ +/******************************************************************************* + + D language bindings for libsodium's randombytes_nativeclient.h + + License: ISC (see LICENSE.txt) + +*******************************************************************************/ + +module libsodium.randombytes_nativeclient; + +@nogc nothrow: + +import libsodium.export_; + +extern (C): diff --git a/source/libsodium/randombytes_salsa20_random.d b/source/libsodium/randombytes_salsa20_random.d new file mode 100644 index 0000000..c28d783 --- /dev/null +++ b/source/libsodium/randombytes_salsa20_random.d @@ -0,0 +1,18 @@ +/******************************************************************************* + + D language bindings for libsodium's randombytes_salsa20_random.h + + License: ISC (see LICENSE.txt) + +*******************************************************************************/ + +module libsodium.randombytes_salsa20_random; + +@nogc nothrow: + +import libsodium.export_; +import libsodium.randombytes; + +extern (C): + +extern __gshared randombytes_implementation randombytes_salsa20_implementation; diff --git a/source/libsodium/randombytes_sysrandom.d b/source/libsodium/randombytes_sysrandom.d new file mode 100644 index 0000000..5a2c68d --- /dev/null +++ b/source/libsodium/randombytes_sysrandom.d @@ -0,0 +1,18 @@ +/******************************************************************************* + + D language bindings for libsodium's randombytes_sysrandom.h + + License: ISC (see LICENSE.txt) + +*******************************************************************************/ + +module libsodium.randombytes_sysrandom; + +@nogc nothrow: + +import libsodium.export_; +import libsodium.randombytes; + +extern (C): + +extern __gshared randombytes_implementation randombytes_sysrandom_implementation; diff --git a/source/libsodium/runtime.d b/source/libsodium/runtime.d new file mode 100644 index 0000000..c76a687 --- /dev/null +++ b/source/libsodium/runtime.d @@ -0,0 +1,41 @@ +/******************************************************************************* + + D language bindings for libsodium's runtime.h + + License: ISC (see LICENSE.txt) + +*******************************************************************************/ + +module libsodium.runtime; + +@nogc nothrow: + +import libsodium.export_; + +extern (C): + +int sodium_runtime_has_neon (); + +int sodium_runtime_has_sse2 (); + +int sodium_runtime_has_sse3 (); + +int sodium_runtime_has_ssse3 (); + +int sodium_runtime_has_sse41 (); + +int sodium_runtime_has_avx (); + +int sodium_runtime_has_avx2 (); + +int sodium_runtime_has_avx512f (); + +int sodium_runtime_has_pclmul (); + +int sodium_runtime_has_aesni (); + +int sodium_runtime_has_rdrand (); + +/* ------------------------------------------------------------------------- */ + +int _sodium_runtime_get_cpu_features (); diff --git a/source/libsodium/utils.d b/source/libsodium/utils.d new file mode 100644 index 0000000..fa490c9 --- /dev/null +++ b/source/libsodium/utils.d @@ -0,0 +1,162 @@ +/******************************************************************************* + + D language bindings for libsodium's utils.h + + License: ISC (see LICENSE.txt) + +*******************************************************************************/ + +module libsodium.utils; + +@nogc nothrow: + +import libsodium.export_; + +extern (C): + +extern (D) auto SODIUM_C99(T)(auto ref T X) +{ + return X; +} + +void sodium_memzero (void* pnt, const size_t len); + +void sodium_stackzero (const size_t len); + +/* + * WARNING: sodium_memcmp() must be used to verify if two secret keys + * are equal, in constant time. + * It returns 0 if the keys are equal, and -1 if they differ. + * This function is not designed for lexicographical comparisons. + */ +int sodium_memcmp (const void* b1_, const void* b2_, size_t len); + +/* + * sodium_compare() returns -1 if b1_ < b2_, 1 if b1_ > b2_ and 0 if b1_ == b2_ + * It is suitable for lexicographical comparisons, or to compare nonces + * and counters stored in little-endian format. + * However, it is slower than sodium_memcmp(). + */ +int sodium_compare (const(ubyte)* b1_, const(ubyte)* b2_, size_t len); + +int sodium_is_zero (const(ubyte)* n, const size_t nlen); + +void sodium_increment (ubyte* n, const size_t nlen); + +void sodium_add (ubyte* a, const(ubyte)* b, const size_t len); + +void sodium_sub (ubyte* a, const(ubyte)* b, const size_t len); + +char* sodium_bin2hex ( + char* hex, + const size_t hex_maxlen, + const ubyte* bin, + const size_t bin_len); + +int sodium_hex2bin ( + ubyte* bin, + const size_t bin_maxlen, + const char* hex, + const size_t hex_len, + const char* ignore, + size_t* bin_len, + const char** hex_end); + +enum sodium_base64_VARIANT_ORIGINAL = 1; +enum sodium_base64_VARIANT_ORIGINAL_NO_PADDING = 3; +enum sodium_base64_VARIANT_URLSAFE = 5; +enum sodium_base64_VARIANT_URLSAFE_NO_PADDING = 7; + +/* + * Computes the required length to encode BIN_LEN bytes as a base64 string + * using the given variant. The computed length includes a trailing \0. + */ +extern (D) auto sodium_base64_ENCODED_LEN(T0, T1)(auto ref T0 BIN_LEN, auto ref T1 VARIANT) +{ + return (BIN_LEN / 3U) * 4U + (((BIN_LEN - (BIN_LEN / 3U) * 3U) | ((BIN_LEN - (BIN_LEN / 3U) * 3U) >> 1)) & 1U) * (4U - (~(((VARIANT & 2U) >> 1) - 1U) & (3U - (BIN_LEN - (BIN_LEN / 3U) * 3U)))) + 1U; +} + +size_t sodium_base64_encoded_len (const size_t bin_len, const int variant); + +char* sodium_bin2base64 ( + char* b64, + const size_t b64_maxlen, + const ubyte* bin, + const size_t bin_len, + const int variant); + +int sodium_base642bin ( + ubyte* bin, + const size_t bin_maxlen, + const char* b64, + const size_t b64_len, + const char* ignore, + size_t* bin_len, + const char** b64_end, + const int variant); + +int sodium_mlock (void* addr, const size_t len); + +int sodium_munlock (void* addr, const size_t len); + +/* WARNING: sodium_malloc() and sodium_allocarray() are not general-purpose + * allocation functions. + * + * They return a pointer to a region filled with 0xd0 bytes, immediately + * followed by a guard page. + * As a result, accessing a single byte after the requested allocation size + * will intentionally trigger a segmentation fault. + * + * A canary and an additional guard page placed before the beginning of the + * region may also kill the process if a buffer underflow is detected. + * + * The memory layout is: + * [unprotected region size (read only)][guard page (no access)][unprotected pages (read/write)][guard page (no access)] + * With the layout of the unprotected pages being: + * [optional padding][16-bytes canary][user region] + * + * However: + * - These functions are significantly slower than standard functions + * - Each allocation requires 3 or 4 additional pages + * - The returned address will not be aligned if the allocation size is not + * a multiple of the required alignment. For this reason, these functions + * are designed to store data, such as secret keys and messages. + * + * sodium_malloc() can be used to allocate any libsodium data structure. + * + * The crypto_generichash_state structure is packed and its length is + * either 357 or 361 bytes. For this reason, when using sodium_malloc() to + * allocate a crypto_generichash_state structure, padding must be added in + * order to ensure proper alignment. crypto_generichash_statebytes() + * returns the rounded up structure size, and should be prefered to sizeof(): + * state = sodium_malloc(crypto_generichash_statebytes()); + */ + +void* sodium_malloc (const size_t size); + +void* sodium_allocarray (size_t count, size_t size); + +void sodium_free (void* ptr); + +int sodium_mprotect_noaccess (void* ptr); + +int sodium_mprotect_readonly (void* ptr); + +int sodium_mprotect_readwrite (void* ptr); + +int sodium_pad ( + size_t* padded_buflen_p, + ubyte* buf, + size_t unpadded_buflen, + size_t blocksize, + size_t max_buflen); + +int sodium_unpad ( + size_t* unpadded_buflen_p, + const(ubyte)* buf, + size_t padded_buflen, + size_t blocksize); + +/* -------- */ + +int _sodium_alloc_init ();