Botan 3.6.1
Crypto and TLS for C&
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
9#include <botan/hash.h>
10#include <botan/mac.h>
11
12namespace Botan {
13
14int Sodium::crypto_hash_sha512(uint8_t out[64], const uint8_t in[], size_t in_len) {
15 auto sha512 = HashFunction::create_or_throw("SHA-512");
16 sha512->update(in, in_len);
17 sha512->final(out);
18 return 0;
19}
20
21int Sodium::crypto_hash_sha256(uint8_t out[], const uint8_t in[], size_t in_len) {
22 auto sha256 = HashFunction::create_or_throw("SHA-256");
23 sha256->update(in, in_len);
24 sha256->final(out);
25 return 0;
26}
27
28int Sodium::crypto_shorthash_siphash24(uint8_t out[8], const uint8_t in[], size_t in_len, const uint8_t key[16]) {
29 auto mac = MessageAuthenticationCode::create_or_throw("SipHash(2,4)");
30 mac->set_key(key, crypto_shorthash_siphash24_KEYBYTES);
31 mac->update(in, in_len);
32 mac->final(out);
33 return 0;
34}
35
36int Sodium::crypto_onetimeauth_poly1305(uint8_t out[], const uint8_t in[], size_t in_len, const uint8_t key[]) {
37 auto mac = MessageAuthenticationCode::create_or_throw("Poly1305");
38 mac->set_key(key, crypto_onetimeauth_poly1305_KEYBYTES);
39 mac->update(in, in_len);
40 mac->final(out);
41 return 0;
42}
43
45 const uint8_t in[],
46 size_t in_len,
47 const uint8_t key[]) {
49 crypto_onetimeauth_poly1305(computed.data(), in, in_len, key);
50 return crypto_verify_16(computed.data(), mac) ? 0 : -1;
51}
52
53int Sodium::crypto_auth_hmacsha512(uint8_t out[], const uint8_t in[], size_t in_len, const uint8_t key[]) {
54 auto mac = MessageAuthenticationCode::create_or_throw("HMAC(SHA-512)");
55 mac->set_key(key, crypto_auth_hmacsha512_KEYBYTES);
56 mac->update(in, in_len);
57 mac->final(out);
58 return 0;
59}
60
61int Sodium::crypto_auth_hmacsha512_verify(const uint8_t mac[], const uint8_t in[], size_t in_len, const uint8_t key[]) {
63 crypto_auth_hmacsha512(computed.data(), in, in_len, key);
64 return crypto_verify_64(computed.data(), mac) ? 0 : -1;
65}
66
67int Sodium::crypto_auth_hmacsha512256(uint8_t out[], const uint8_t in[], size_t in_len, const uint8_t key[]) {
68 auto mac = MessageAuthenticationCode::create_or_throw("HMAC(SHA-512)");
69 mac->set_key(key, crypto_auth_hmacsha512256_KEYBYTES);
70 mac->update(in, in_len);
71
73 mac->final(buf);
74
76 return 0;
77}
78
80 const uint8_t in[],
81 size_t in_len,
82 const uint8_t key[]) {
84 crypto_auth_hmacsha512256(computed.data(), in, in_len, key);
85 return crypto_verify_32(computed.data(), mac) ? 0 : -1;
86}
87
88int Sodium::crypto_auth_hmacsha256(uint8_t out[], const uint8_t in[], size_t in_len, const uint8_t key[]) {
89 auto mac = MessageAuthenticationCode::create_or_throw("HMAC(SHA-256)");
90 mac->set_key(key, crypto_auth_hmacsha256_KEYBYTES);
91 mac->update(in, in_len);
92 mac->final(out);
93 return 0;
94}
95
96int Sodium::crypto_auth_hmacsha256_verify(const uint8_t mac[], const uint8_t in[], size_t in_len, const uint8_t key[]) {
98 crypto_auth_hmacsha256(computed.data(), in, in_len, key);
99 return crypto_verify_32(computed.data(), mac) ? 0 : -1;
100}
101
102} // namespace Botan
static std::unique_ptr< HashFunction > create_or_throw(std::string_view algo_spec, std::string_view provider="")
Definition hash.cpp:298
static std::unique_ptr< MessageAuthenticationCode > create_or_throw(std::string_view algo_spec, std::string_view provider="")
Definition mac.cpp:148
int crypto_hash_sha256(uint8_t out[], const uint8_t in[], size_t in_len)
int crypto_verify_32(const uint8_t x[32], const uint8_t y[32])
int crypto_auth_hmacsha512(uint8_t out[], const uint8_t in[], size_t in_len, const uint8_t key[])
int crypto_verify_16(const uint8_t x[16], const uint8_t y[16])
int crypto_shorthash_siphash24(uint8_t out[8], const uint8_t in[], size_t in_len, const uint8_t key[16])
@ crypto_auth_hmacsha256_KEYBYTES
Definition sodium.h:41
@ crypto_auth_hmacsha256_BYTES
Definition sodium.h:40
@ crypto_shorthash_siphash24_KEYBYTES
Definition sodium.h:111
@ crypto_onetimeauth_poly1305_KEYBYTES
Definition sodium.h:86
@ crypto_auth_hmacsha512_BYTES
Definition sodium.h:44
@ crypto_onetimeauth_poly1305_BYTES
Definition sodium.h:85
@ crypto_auth_hmacsha512256_KEYBYTES
Definition sodium.h:43
@ crypto_auth_hmacsha512_KEYBYTES
Definition sodium.h:45
@ crypto_auth_hmacsha512256_BYTES
Definition sodium.h:42
int crypto_hash_sha512(uint8_t out[64], const uint8_t in[], size_t in_len)
int crypto_onetimeauth_poly1305_verify(const uint8_t h[], const uint8_t in[], size_t in_len, const uint8_t key[])
int crypto_verify_64(const uint8_t x[64], const uint8_t y[64])
int crypto_auth_hmacsha512256_verify(const uint8_t h[], const uint8_t in[], size_t in_len, const uint8_t key[])
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(uint8_t out[], const uint8_t in[], size_t in_len, const uint8_t key[])
int crypto_auth_hmacsha512256(uint8_t out[], const uint8_t in[], size_t in_len, const uint8_t key[])
int crypto_auth_hmacsha256(uint8_t out[], const uint8_t in[], size_t in_len, const uint8_t key[])
int crypto_auth_hmacsha512_verify(const uint8_t h[], const uint8_t in[], size_t in_len, const uint8_t key[])
std::vector< T, secure_allocator< T > > secure_vector
Definition secmem.h:61
constexpr void copy_mem(T *out, const T *in, size_t n)
Definition mem_ops.h:146