1 /*******************************************************************************
2 
3     D language bindings for libsodium's sodium.h
4 
5     License: ISC (see LICENSE.txt)
6 
7 *******************************************************************************/
8 
9 module libsodium;
10 
11 public import libsodium.core;
12 public import libsodium.crypto_aead_aes256gcm;
13 public import libsodium.crypto_aead_chacha20poly1305;
14 public import libsodium.crypto_aead_xchacha20poly1305;
15 public import libsodium.crypto_auth;
16 public import libsodium.crypto_auth_hmacsha256;
17 public import libsodium.crypto_auth_hmacsha512256;
18 public import libsodium.crypto_auth_hmacsha512;
19 public import libsodium.crypto_box;
20 public import libsodium.crypto_box_curve25519xchacha20poly1305;
21 public import libsodium.crypto_box_curve25519xsalsa20poly1305;
22 public import libsodium.crypto_core_ed25519;
23 public import libsodium.crypto_core_hchacha20;
24 public import libsodium.crypto_core_hsalsa20;
25 public import libsodium.crypto_core_salsa2012;
26 public import libsodium.crypto_core_salsa208;
27 public import libsodium.crypto_core_salsa20;
28 public import libsodium.crypto_generichash;
29 public import libsodium.crypto_generichash_blake2b;
30 public import libsodium.crypto_hash;
31 public import libsodium.crypto_hash_sha256;
32 public import libsodium.crypto_hash_sha512;
33 public import libsodium.crypto_kdf;
34 public import libsodium.crypto_kdf_blake2b;
35 public import libsodium.crypto_kx;
36 public import libsodium.crypto_onetimeauth;
37 public import libsodium.crypto_onetimeauth_poly1305;
38 public import libsodium.crypto_pwhash;
39 public import libsodium.crypto_pwhash_argon2i;
40 public import libsodium.crypto_pwhash_argon2id;
41 public import libsodium.crypto_pwhash_scryptsalsa208sha256;
42 public import libsodium.crypto_scalarmult;
43 public import libsodium.crypto_scalarmult_curve25519;
44 public import libsodium.crypto_scalarmult_ed25519;
45 public import libsodium.crypto_secretbox;
46 public import libsodium.crypto_secretbox_xchacha20poly1305;
47 public import libsodium.crypto_secretbox_xsalsa20poly1305;
48 public import libsodium.crypto_secretstream_xchacha20poly1305;
49 public import libsodium.crypto_shorthash;
50 public import libsodium.crypto_shorthash_siphash24;
51 public import libsodium.crypto_sign;
52 public import libsodium.crypto_sign_ed25519;
53 public import libsodium.crypto_sign_edwards25519sha512batch;
54 public import libsodium.crypto_stream;
55 public import libsodium.crypto_stream_chacha20;
56 public import libsodium.crypto_stream_salsa2012;
57 public import libsodium.crypto_stream_salsa208;
58 public import libsodium.crypto_stream_salsa20;
59 public import libsodium.crypto_stream_xchacha20;
60 public import libsodium.crypto_stream_xsalsa20;
61 public import libsodium.crypto_verify_16;
62 public import libsodium.crypto_verify_32;
63 public import libsodium.crypto_verify_64;
64 public import libsodium.export_;
65 public import libsodium.randombytes;
66 public import libsodium.randombytes_nativeclient;
67 public import libsodium.randombytes_salsa20_random;
68 public import libsodium.randombytes_sysrandom;
69 public import libsodium.runtime;
70 public import libsodium.utils;
71 
72 version (unittest)
73 shared static this ()
74 {
75     auto res = sodium_init();
76     assert(res >= 0);
77 }
78 
79 /// https://libsodium.gitbook.io/doc/secret-key_cryptography/authenticated_encryption
80 unittest
81 {
82     static immutable message = "Hello world";
83 
84     ubyte[crypto_secretbox_KEYBYTES] key;
85     ubyte[crypto_secretbox_NONCEBYTES] nonce;
86     ubyte[crypto_secretbox_MACBYTES + message.length] cipher;
87 
88     crypto_secretbox_keygen(key);
89     randombytes_buf(nonce.ptr, nonce.length);
90     crypto_secretbox_easy(cipher.ptr, cast(const(ubyte*))message.ptr, message.length,
91                           nonce.ptr, key.ptr);
92 
93     ubyte[message.length] decrypted;
94     if (crypto_secretbox_open_easy(decrypted.ptr, cipher.ptr, cipher.length,
95                                    nonce.ptr, key.ptr) != 0) {
96         assert(0);
97     }
98 }
99 
100 /// https://libsodium.gitbook.io/doc/public-key_cryptography/authenticated_encryption
101 unittest
102 {
103     static immutable message = "authenticated encryption";
104 
105     ubyte[crypto_box_PUBLICKEYBYTES] alice_publickey;
106     ubyte[crypto_box_SECRETKEYBYTES] alice_secretkey;
107     crypto_box_keypair(alice_publickey.ptr, alice_secretkey.ptr);
108 
109     ubyte[crypto_box_PUBLICKEYBYTES] bob_publickey;
110     ubyte[crypto_box_SECRETKEYBYTES] bob_secretkey;
111     crypto_box_keypair(bob_publickey.ptr, bob_secretkey.ptr);
112 
113     ubyte[crypto_box_NONCEBYTES] nonce;
114     ubyte[crypto_box_MACBYTES + message.length] ciphertext;
115     randombytes_buf(nonce.ptr, nonce.length);
116 
117     if (crypto_box_easy(ciphertext.ptr, cast(const(ubyte*))message.ptr,
118                         message.length, nonce.ptr,
119                         bob_publickey.ptr, alice_secretkey.ptr) != 0) {
120         assert(0);
121     }
122 
123     ubyte[message.length] decrypted;
124     if (crypto_box_open_easy(
125             decrypted.ptr, ciphertext.ptr, ciphertext.length, nonce.ptr,
126             alice_publickey.ptr, bob_secretkey.ptr) != 0) {
127         assert(0);
128     }
129 }