1 // Written in the D programming language.
2 
3 module wrapper.sodium.crypto_auth_hmacsha512256;
4 
5 import wrapper.sodium.core; // assure sodium got initialized
6 
7 public
8 import  deimos.sodium.crypto_auth_hmacsha512256 : crypto_auth_hmacsha512256_BYTES,
9                                                   crypto_auth_hmacsha512256_bytes,
10                                                   crypto_auth_hmacsha512256_KEYBYTES,
11                                                   crypto_auth_hmacsha512256_keybytes,
12 /*                                                crypto_auth_hmacsha512256,
13                                                   crypto_auth_hmacsha512256_verify,  */
14                                                   crypto_auth_hmacsha512256_state,
15                                                   crypto_auth_hmacsha512256_statebytes,
16 /*                                                crypto_auth_hmacsha512256_init,
17                                                   crypto_auth_hmacsha512256_update,
18                                                   crypto_auth_hmacsha512256_final,   */
19                                                   crypto_auth_hmacsha512256_keygen;
20 
21 
22 // overloading some functions between module deimos.sodium.crypto_auth_hmacsha512256 and this module
23 
24 alias  crypto_auth_hmacsha512256        = deimos.sodium.crypto_auth_hmacsha512256.crypto_auth_hmacsha512256;
25 alias  crypto_auth_hmacsha512256_verify = deimos.sodium.crypto_auth_hmacsha512256.crypto_auth_hmacsha512256_verify;
26 alias  crypto_auth_hmacsha512256_init   = deimos.sodium.crypto_auth_hmacsha512256.crypto_auth_hmacsha512256_init;
27 alias  crypto_auth_hmacsha512256_update = deimos.sodium.crypto_auth_hmacsha512256.crypto_auth_hmacsha512256_update;
28 alias  crypto_auth_hmacsha512256_final  = deimos.sodium.crypto_auth_hmacsha512256.crypto_auth_hmacsha512256_final;
29 
30 
31 pragma(inline, true)  @nogc pure @trusted
32 {
33 
34 /**
35  * The crypto_auth_hmacsha512256() function authenticates a message `message` using the secret key skey,
36  * and puts the authenticator into mac.
37  * Returns 0? on success.
38  */
39 bool crypto_auth_hmacsha512256(out ubyte[crypto_auth_hmacsha512256_BYTES] mac,
40                                scope const ubyte[] message,
41                                scope const ubyte[crypto_auth_hmacsha512256_KEYBYTES] skey)
42 {
43   return  crypto_auth_hmacsha512256(mac.ptr, message.ptr, message.length, skey.ptr) == 0;
44 }
45 
46 /**
47  * The   crypto_auth_hmacsha512256_verify()   function verifies in constant time that   h   is a correct
48 authenticator for the message   in  whose length is   inlen   under a secret key   k .
49 It returns   -1  if the verification fails, and   0  on success.
50 
51  */
52 bool crypto_auth_hmacsha512256_verify(const ubyte[crypto_auth_hmacsha512256_BYTES] mac,
53                                       scope const ubyte[] message,
54                                       const ubyte[crypto_auth_hmacsha512256_KEYBYTES] skey) nothrow
55 {
56   return  crypto_auth_hmacsha512256_verify(mac.ptr, message.ptr, message.length, skey.ptr) == 0;
57 }
58 
59 /**
60  * This alternative API supports a key of arbitrary length
61  */
62 bool crypto_auth_hmacsha512256_init(out crypto_auth_hmacsha512256_state state,
63                                     scope const ubyte[] skey)
64 {
65   return  crypto_auth_hmacsha512256_init(&state, skey.ptr, skey.length) == 0;
66 }
67 
68 bool crypto_auth_hmacsha512256_update(ref crypto_auth_hmacsha512256_state state,
69                                       scope const ubyte[] in_)
70 {
71   return  crypto_auth_hmacsha512256_update(&state, in_.ptr, in_.length) == 0;
72 }
73 
74 bool crypto_auth_hmacsha512256_final(ref crypto_auth_hmacsha512256_state state,
75                                      out ubyte[crypto_auth_hmacsha512256_BYTES] out_)
76 {
77   return  crypto_auth_hmacsha512256_final(&state, out_.ptr) == 0;
78 }
79 } //pragma(inline, true)  pure @nogc @trusted
80 
81 
82 @safe
83 unittest
84 {
85   import std.stdio : writeln;
86   import std.string : representation;
87   import wrapper.sodium.randombytes : randombytes;
88 
89   debug writeln("unittest block 1 from sodium.crypto_auth_hmacsha512256.d");
90 
91   assert(crypto_auth_hmacsha512256_bytes()      == crypto_auth_hmacsha512256_BYTES);
92   assert(crypto_auth_hmacsha512256_keybytes()   == crypto_auth_hmacsha512256_KEYBYTES);
93   assert(crypto_auth_hmacsha512256_statebytes() == crypto_auth_hmacsha512256_state.sizeof);
94 //  writeln("crypto_auth_hmacsha512256_statebytes(): ", crypto_auth_hmacsha512256_statebytes()); // 416
95 //  writeln("crypto_auth_hmacsha512256_state.sizeof: ", crypto_auth_hmacsha512256_state.sizeof); // 416 = 2*crypto_hash_sha512256_state.sizeof
96 
97   auto                                      message  = representation("test some more text");
98   auto                                      message2 = representation(" some more text");
99   ubyte[crypto_auth_hmacsha512256_KEYBYTES] skey;
100   ubyte[crypto_auth_hmacsha512256_BYTES]    mac;
101 
102   randombytes(skey);
103   assert(crypto_auth_hmacsha512256(mac, message, skey));
104   assert(crypto_auth_hmacsha512256_verify(mac, message, skey));
105   ubyte[crypto_auth_hmacsha512256_BYTES]    mac_saved = mac;
106 
107   message  = representation("test");
108   crypto_auth_hmacsha512256_state state;
109   assert(crypto_auth_hmacsha512256_init  (state, skey));
110   assert(crypto_auth_hmacsha512256_update(state, message));
111   assert(crypto_auth_hmacsha512256_update(state, message2));
112   assert(crypto_auth_hmacsha512256_final (state, mac));
113   message  = representation("test some more text");
114   assert(crypto_auth_hmacsha512256_verify(mac, message, skey));
115   assert(mac == mac_saved);
116   ubyte[crypto_auth_hmacsha512256_KEYBYTES] k;
117   crypto_auth_hmacsha512256_keygen(k);
118 }