Botan  2.11.0
Crypto and TLS for C++11
sodium_auth.cpp
Go to the documentation of this file.
1 /*
2 * (C) 2019 Jack Lloyd
3 *
4 * Botan is released under the Simplified BSD License (see license.txt)
5 */
6 
7 #include <botan/sodium.h>
8 #include <botan/mac.h>
9 #include <botan/hash.h>
10 
11 namespace Botan {
12 
13 int Sodium::crypto_hash_sha512(uint8_t out[64], const uint8_t in[], size_t in_len)
14  {
15  auto sha512 = HashFunction::create_or_throw("SHA-512");
16  sha512->update(in, in_len);
17  sha512->final(out);
18  return 0;
19  }
20 
21 int Sodium::crypto_hash_sha256(uint8_t out[], const uint8_t in[], size_t in_len)
22  {
23  auto sha256 = HashFunction::create_or_throw("SHA-256");
24  sha256->update(in, in_len);
25  sha256->final(out);
26  return 0;
27  }
28 
29 int Sodium::crypto_shorthash_siphash24(uint8_t out[8], const uint8_t in[],
30  size_t in_len, const uint8_t key[16])
31  {
32  auto mac = MessageAuthenticationCode::create_or_throw("SipHash(2,4)");
33  mac->set_key(key, crypto_shorthash_siphash24_KEYBYTES);
34  mac->update(in, in_len);
35  mac->final(out);
36  return 0;
37  }
38 
40  const uint8_t in[],
41  size_t in_len,
42  const uint8_t key[])
43  {
44  auto mac = MessageAuthenticationCode::create_or_throw("Poly1305");
45  mac->set_key(key, crypto_onetimeauth_poly1305_KEYBYTES);
46  mac->update(in, in_len);
47  mac->final(out);
48  return 0;
49  }
50 
52  const uint8_t in[],
53  size_t in_len,
54  const uint8_t key[])
55  {
57  crypto_onetimeauth_poly1305(computed.data(), in, in_len, key);
58  return crypto_verify_16(computed.data(), mac) ? 0 : -1;
59  }
60 
62  const uint8_t in[],
63  size_t in_len,
64  const uint8_t key[])
65  {
66  auto mac = MessageAuthenticationCode::create_or_throw("HMAC(SHA-512)");
67  mac->set_key(key, crypto_auth_hmacsha512_KEYBYTES);
68  mac->update(in, in_len);
69  mac->final(out);
70  return 0;
71  }
72 
73 int Sodium::crypto_auth_hmacsha512_verify(const uint8_t mac[],
74  const uint8_t in[],
75  size_t in_len,
76  const uint8_t key[])
77  {
79  crypto_auth_hmacsha512(computed.data(), in, in_len, key);
80  return crypto_verify_64(computed.data(), mac) ? 0 : -1;
81  }
82 
84  const uint8_t in[],
85  size_t in_len,
86  const uint8_t key[])
87  {
88  auto mac = MessageAuthenticationCode::create_or_throw("HMAC(SHA-512)");
89  mac->set_key(key, crypto_auth_hmacsha512256_KEYBYTES);
90  mac->update(in, in_len);
91 
92  secure_vector<uint8_t> buf(64);
93  mac->final(buf);
94 
95  copy_mem(out, buf.data(), crypto_auth_hmacsha512256_BYTES);
96  return 0;
97  }
98 
100  const uint8_t in[],
101  size_t in_len,
102  const uint8_t key[])
103  {
105  crypto_auth_hmacsha512256(computed.data(), in, in_len, key);
106  return crypto_verify_32(computed.data(), mac) ? 0 : -1;
107  }
108 
110  const uint8_t in[],
111  size_t in_len,
112  const uint8_t key[])
113  {
114  auto mac = MessageAuthenticationCode::create_or_throw("HMAC(SHA-256)");
115  mac->set_key(key, crypto_auth_hmacsha256_KEYBYTES);
116  mac->update(in, in_len);
117  mac->final(out);
118  return 0;
119  }
120 
122  const uint8_t in[],
123  size_t in_len,
124  const uint8_t key[])
125  {
127  crypto_auth_hmacsha256(computed.data(), in, in_len, key);
128  return crypto_verify_32(computed.data(), mac) ? 0 : -1;
129  }
130 
131 }
int crypto_onetimeauth_poly1305(uint8_t out[], const uint8_t in[], size_t in_len, const uint8_t key[])
Definition: sodium_auth.cpp:39
int crypto_hash_sha256(uint8_t out[], const uint8_t in[], size_t in_len)
Definition: sodium_auth.cpp:21
static std::unique_ptr< HashFunction > create_or_throw(const std::string &algo_spec, const std::string &provider="")
Definition: hash.cpp:359
int crypto_hash_sha512(uint8_t out[64], const uint8_t in[], size_t in_len)
Definition: sodium_auth.cpp:13
int crypto_verify_16(const uint8_t x[16], const uint8_t y[16])
int crypto_auth_hmacsha512256(uint8_t out[], const uint8_t in[], size_t in_len, const uint8_t key[])
Definition: sodium_auth.cpp:83
int crypto_auth_hmacsha256_verify(const uint8_t h[], const uint8_t in[], size_t in_len, const uint8_t key[])
int crypto_onetimeauth_poly1305_verify(const uint8_t h[], const uint8_t in[], size_t in_len, const uint8_t key[])
Definition: sodium_auth.cpp:51
int crypto_auth_hmacsha512(uint8_t out[], const uint8_t in[], size_t in_len, const uint8_t key[])
Definition: sodium_auth.cpp:61
int crypto_verify_64(const uint8_t x[64], const uint8_t y[64])
void copy_mem(T *out, const T *in, size_t n)
Definition: mem_ops.h:122
Definition: alg_id.cpp:13
int crypto_auth_hmacsha512_verify(const uint8_t h[], const uint8_t in[], size_t in_len, const uint8_t key[])
Definition: sodium_auth.cpp:73
static std::unique_ptr< MessageAuthenticationCode > create_or_throw(const std::string &algo_spec, const std::string &provider="")
Definition: mac.cpp:141
std::vector< T, secure_allocator< T > > secure_vector
Definition: secmem.h:65
int crypto_verify_32(const uint8_t x[32], const uint8_t y[32])
int crypto_auth_hmacsha256(uint8_t out[], const uint8_t in[], size_t in_len, const uint8_t key[])
int crypto_auth_hmacsha512256_verify(const uint8_t h[], const uint8_t in[], size_t in_len, const uint8_t key[])
Definition: sodium_auth.cpp:99
int crypto_shorthash_siphash24(uint8_t out[8], const uint8_t in[], size_t in_len, const uint8_t key[16])
Definition: sodium_auth.cpp:29