summaryrefslogtreecommitdiffstats
path: root/comm/third_party/botan/src/lib/compat/sodium/sodium_auth.cpp
blob: 747b8af33ec174bcea9114cf3a869d8db271becd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/*
* (C) 2019 Jack Lloyd
*
* Botan is released under the Simplified BSD License (see license.txt)
*/

#include <botan/sodium.h>
#include <botan/mac.h>
#include <botan/hash.h>

namespace Botan {

int Sodium::crypto_hash_sha512(uint8_t out[64], const uint8_t in[], size_t in_len)
   {
   auto sha512 = HashFunction::create_or_throw("SHA-512");
   sha512->update(in, in_len);
   sha512->final(out);
   return 0;
   }

int Sodium::crypto_hash_sha256(uint8_t out[], const uint8_t in[], size_t in_len)
   {
   auto sha256 = HashFunction::create_or_throw("SHA-256");
   sha256->update(in, in_len);
   sha256->final(out);
   return 0;
   }

int Sodium::crypto_shorthash_siphash24(uint8_t out[8], const uint8_t in[],
                                       size_t in_len, const uint8_t key[16])
   {
   auto mac = MessageAuthenticationCode::create_or_throw("SipHash(2,4)");
   mac->set_key(key, crypto_shorthash_siphash24_KEYBYTES);
   mac->update(in, in_len);
   mac->final(out);
   return 0;
   }

int Sodium::crypto_onetimeauth_poly1305(uint8_t out[],
                                        const uint8_t in[],
                                        size_t in_len,
                                        const uint8_t key[])
   {
   auto mac = MessageAuthenticationCode::create_or_throw("Poly1305");
   mac->set_key(key, crypto_onetimeauth_poly1305_KEYBYTES);
   mac->update(in, in_len);
   mac->final(out);
   return 0;
   }

int Sodium::crypto_onetimeauth_poly1305_verify(const uint8_t mac[],
                                               const uint8_t in[],
                                               size_t in_len,
                                               const uint8_t key[])
   {
   secure_vector<uint8_t> computed(crypto_onetimeauth_poly1305_BYTES);
   crypto_onetimeauth_poly1305(computed.data(), in, in_len, key);
   return crypto_verify_16(computed.data(), mac) ? 0 : -1;
   }

int Sodium::crypto_auth_hmacsha512(uint8_t out[],
                                   const uint8_t in[],
                                   size_t in_len,
                                   const uint8_t key[])
   {
   auto mac = MessageAuthenticationCode::create_or_throw("HMAC(SHA-512)");
   mac->set_key(key, crypto_auth_hmacsha512_KEYBYTES);
   mac->update(in, in_len);
   mac->final(out);
   return 0;
   }

int Sodium::crypto_auth_hmacsha512_verify(const uint8_t mac[],
                                          const uint8_t in[],
                                          size_t in_len,
                                          const uint8_t key[])
   {
   secure_vector<uint8_t> computed(crypto_auth_hmacsha512_BYTES);
   crypto_auth_hmacsha512(computed.data(), in, in_len, key);
   return crypto_verify_64(computed.data(), mac) ? 0 : -1;
   }

int Sodium::crypto_auth_hmacsha512256(uint8_t out[],
                                      const uint8_t in[],
                                      size_t in_len,
                                      const uint8_t key[])
   {
   auto mac = MessageAuthenticationCode::create_or_throw("HMAC(SHA-512)");
   mac->set_key(key, crypto_auth_hmacsha512256_KEYBYTES);
   mac->update(in, in_len);

   secure_vector<uint8_t> buf(64);
   mac->final(buf);

   copy_mem(out, buf.data(), crypto_auth_hmacsha512256_BYTES);
   return 0;
   }

int Sodium::crypto_auth_hmacsha512256_verify(const uint8_t mac[],
                                             const uint8_t in[],
                                             size_t in_len,
                                             const uint8_t key[])
   {
   secure_vector<uint8_t> computed(crypto_auth_hmacsha512256_BYTES);
   crypto_auth_hmacsha512256(computed.data(), in, in_len, key);
   return crypto_verify_32(computed.data(), mac) ? 0 : -1;
   }

int Sodium::crypto_auth_hmacsha256(uint8_t out[],
                                   const uint8_t in[],
                                   size_t in_len,
                                   const uint8_t key[])
   {
   auto mac = MessageAuthenticationCode::create_or_throw("HMAC(SHA-256)");
   mac->set_key(key, crypto_auth_hmacsha256_KEYBYTES);
   mac->update(in, in_len);
   mac->final(out);
   return 0;
   }

int Sodium::crypto_auth_hmacsha256_verify(const uint8_t mac[],
                                          const uint8_t in[],
                                          size_t in_len,
                                          const uint8_t key[])
   {
   secure_vector<uint8_t> computed(crypto_auth_hmacsha256_BYTES);
   crypto_auth_hmacsha256(computed.data(), in, in_len, key);
   return crypto_verify_32(computed.data(), mac) ? 0 : -1;
   }

}