Skip to content

Commit

Permalink
Add bindings for libsodium v1.0.17
Browse files Browse the repository at this point in the history
  • Loading branch information
Geod24 committed Jan 22, 2019
1 parent 6fbf2fe commit 0bc5fe1
Show file tree
Hide file tree
Showing 65 changed files with 3,912 additions and 2 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
docs.json
__dummy.html
docs/
*-test-library

# Code coverage
*.lst
18 changes: 18 additions & 0 deletions LICENSE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/*
* ISC License
*
* Copyright (c) 2013-2019
* Frank Denis <j at pureftpd dot org>
*
* 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.
*/
63 changes: 61 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -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`
9 changes: 9 additions & 0 deletions dub.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"name": "libsodiumd",
"description": "D binding for libsodium",
"license": "ISC",
"libs": [ "sodium" ],
"authors": [
"Mathias Lang"
]
}
22 changes: 22 additions & 0 deletions source/libsodium/core.d
Original file line number Diff line number Diff line change
@@ -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 ();
160 changes: 160 additions & 0 deletions source/libsodium/crypto_aead_aes256gcm.d
Original file line number Diff line number Diff line change
@@ -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);
Loading

0 comments on commit 0bc5fe1

Please sign in to comment.