Botan 3.8.0
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#include <botan/mem_ops.h>
12
13namespace Botan {
14
15int Sodium::crypto_hash_sha512(uint8_t out[64], const uint8_t in[], size_t in_len) {
16 auto sha512 = HashFunction::create_or_throw("SHA-512");
17 sha512->update(in, in_len);
18 sha512->final(out);
19 return 0;
20}
21
22int Sodium::crypto_hash_sha256(uint8_t out[], const uint8_t in[], size_t in_len) {
23 auto sha256 = HashFunction::create_or_throw("SHA-256");
24 sha256->update(in, in_len);
25 sha256->final(out);
26 return 0;
27}
28
29int Sodium::crypto_shorthash_siphash24(uint8_t out[8], const uint8_t in[], size_t in_len, const uint8_t key[16]) {
30 auto mac = MessageAuthenticationCode::create_or_throw("SipHash(2,4)");
31 mac->set_key(key, crypto_shorthash_siphash24_KEYBYTES);
32 mac->update(in, in_len);
33 mac->final(out);
34 return 0;
35}
36
37int Sodium::crypto_onetimeauth_poly1305(uint8_t out[], const uint8_t in[], size_t in_len, const uint8_t key[]) {
38 auto mac = MessageAuthenticationCode::create_or_throw("Poly1305");
39 mac->set_key(key, crypto_onetimeauth_poly1305_KEYBYTES);
40 mac->update(in, in_len);
41 mac->final(out);
42 return 0;
43}
44
46 const uint8_t in[],
47 size_t in_len,
48 const uint8_t key[]) {
50 crypto_onetimeauth_poly1305(computed.data(), in, in_len, key);
51 return crypto_verify_16(computed.data(), mac) ? 0 : -1;
52}
53
54int Sodium::crypto_auth_hmacsha512(uint8_t out[], const uint8_t in[], size_t in_len, const uint8_t key[]) {
55 auto mac = MessageAuthenticationCode::create_or_throw("HMAC(SHA-512)");
56 mac->set_key(key, crypto_auth_hmacsha512_KEYBYTES);
57 mac->update(in, in_len);
58 mac->final(out);
59 return 0;
60}
61
62int Sodium::crypto_auth_hmacsha512_verify(const uint8_t mac[], const uint8_t in[], size_t in_len, const uint8_t key[]) {
64 crypto_auth_hmacsha512(computed.data(), in, in_len, key);
65 return crypto_verify_64(computed.data(), mac) ? 0 : -1;
66}
67
68int Sodium::crypto_auth_hmacsha512256(uint8_t out[], const uint8_t in[], size_t in_len, const uint8_t key[]) {
69 auto mac = MessageAuthenticationCode::create_or_throw("HMAC(SHA-512)");
70 mac->set_key(key, crypto_auth_hmacsha512256_KEYBYTES);
71 mac->update(in, in_len);
72
74 mac->final(buf);
75
77 return 0;
78}
79
81 const uint8_t in[],
82 size_t in_len,
83 const uint8_t key[]) {
85 crypto_auth_hmacsha512256(computed.data(), in, in_len, key);
86 return crypto_verify_32(computed.data(), mac) ? 0 : -1;
87}
88
89int Sodium::crypto_auth_hmacsha256(uint8_t out[], const uint8_t in[], size_t in_len, const uint8_t key[]) {
90 auto mac = MessageAuthenticationCode::create_or_throw("HMAC(SHA-256)");
91 mac->set_key(key, crypto_auth_hmacsha256_KEYBYTES);
92 mac->update(in, in_len);
93 mac->final(out);
94 return 0;
95}
96
97int Sodium::crypto_auth_hmacsha256_verify(const uint8_t mac[], const uint8_t in[], size_t in_len, const uint8_t key[]) {
99 crypto_auth_hmacsha256(computed.data(), in, in_len, key);
100 return crypto_verify_32(computed.data(), mac) ? 0 : -1;
101}
102
103} // 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:64
constexpr void copy_mem(T *out, const T *in, size_t n)
Definition mem_ops.h:149