Botan 2.19.5 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#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
39 int Sodium::crypto_onetimeauth_poly1305(uint8_t out[],
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
51 int Sodium::crypto_onetimeauth_poly1305_verify(const uint8_t mac[],
52 const uint8_t in[],
53 size_t in_len,
54 const uint8_t key[])
55 {
56 secure_vector<uint8_t> computed(crypto_onetimeauth_poly1305_BYTES);
57 crypto_onetimeauth_poly1305(computed.data(), in, in_len, key);
58 return crypto_verify_16(computed.data(), mac) ? 0 : -1;
59 }
60
61 int Sodium::crypto_auth_hmacsha512(uint8_t out[],
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 {
78 secure_vector<uint8_t> computed(crypto_auth_hmacsha512_BYTES);
79 crypto_auth_hmacsha512(computed.data(), in, in_len, key);
80 return crypto_verify_64(computed.data(), mac) ? 0 : -1;
81 }
82
83 int Sodium::crypto_auth_hmacsha512256(uint8_t out[],
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
99 int Sodium::crypto_auth_hmacsha512256_verify(const uint8_t mac[],
100 const uint8_t in[],
101 size_t in_len,
102 const uint8_t key[])
103 {
104 secure_vector<uint8_t> computed(crypto_auth_hmacsha512256_BYTES);
105 crypto_auth_hmacsha512256(computed.data(), in, in_len, key);
106 return crypto_verify_32(computed.data(), mac) ? 0 : -1;
107 }
108
109 int Sodium::crypto_auth_hmacsha256(uint8_t out[],
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
121 int Sodium::crypto_auth_hmacsha256_verify(const uint8_t mac[],
122 const uint8_t in[],
123 size_t in_len,
124 const uint8_t key[])
125 {
126 secure_vector<uint8_t> computed(crypto_auth_hmacsha256_BYTES);
127 crypto_auth_hmacsha256(computed.data(), in, in_len, key);
128 return crypto_verify_32(computed.data(), mac) ? 0 : -1;
129 }
130
131}
Botan::HashFunction::create_or_throw
static std::unique_ptr< HashFunction > create_or_throw(const std::string &algo_spec, const std::string &provider="")
Definition: hash.cpp:329
Botan::MessageAuthenticationCode::create_or_throw
static std::unique_ptr< MessageAuthenticationCode > create_or_throw(const std::string &algo_spec, const std::string &provider="")
Definition: mac.cpp:139
Botan::Sodium::crypto_hash_sha256
int crypto_hash_sha256(uint8_t out[], const uint8_t in[], size_t in_len)
Definition: sodium_auth.cpp:21
Botan::Sodium::crypto_verify_32
int crypto_verify_32(const uint8_t x[32], const uint8_t y[32])
Definition: sodium_utils.cpp:51
Botan::Sodium::crypto_auth_hmacsha512
int crypto_auth_hmacsha512(uint8_t out[], const uint8_t in[], size_t in_len, const uint8_t key[])
Definition: sodium_auth.cpp:61
Botan::Sodium::crypto_verify_16
int crypto_verify_16(const uint8_t x[16], const uint8_t y[16])
Definition: sodium_utils.cpp:46
Botan::Sodium::crypto_shorthash_siphash24
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
Botan::Sodium::crypto_auth_hmacsha256_KEYBYTES
@ crypto_auth_hmacsha256_KEYBYTES
Definition: sodium.h:43
Botan::Sodium::crypto_auth_hmacsha256_BYTES
@ crypto_auth_hmacsha256_BYTES
Definition: sodium.h:42
Botan::Sodium::crypto_shorthash_siphash24_KEYBYTES
@ crypto_shorthash_siphash24_KEYBYTES
Definition: sodium.h:111
Botan::Sodium::crypto_onetimeauth_poly1305_KEYBYTES
@ crypto_onetimeauth_poly1305_KEYBYTES
Definition: sodium.h:87
Botan::Sodium::crypto_auth_hmacsha512_BYTES
@ crypto_auth_hmacsha512_BYTES
Definition: sodium.h:46
Botan::Sodium::crypto_onetimeauth_poly1305_BYTES
@ crypto_onetimeauth_poly1305_BYTES
Definition: sodium.h:86
Botan::Sodium::crypto_auth_hmacsha512256_KEYBYTES
@ crypto_auth_hmacsha512256_KEYBYTES
Definition: sodium.h:45
Botan::Sodium::crypto_auth_hmacsha512_KEYBYTES
@ crypto_auth_hmacsha512_KEYBYTES
Definition: sodium.h:47
Botan::Sodium::crypto_auth_hmacsha512256_BYTES
@ crypto_auth_hmacsha512256_BYTES
Definition: sodium.h:44
Botan::Sodium::crypto_hash_sha512
int crypto_hash_sha512(uint8_t out[64], const uint8_t in[], size_t in_len)
Definition: sodium_auth.cpp:13
Botan::Sodium::crypto_onetimeauth_poly1305_verify
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
Botan::Sodium::crypto_verify_64
int crypto_verify_64(const uint8_t x[64], const uint8_t y[64])
Definition: sodium_utils.cpp:56
Botan::Sodium::crypto_auth_hmacsha512256_verify
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
Botan::Sodium::crypto_auth_hmacsha256_verify
int crypto_auth_hmacsha256_verify(const uint8_t h[], const uint8_t in[], size_t in_len, const uint8_t key[])
Definition: sodium_auth.cpp:121
Botan::Sodium::crypto_onetimeauth_poly1305
int crypto_onetimeauth_poly1305(uint8_t out[], const uint8_t in[], size_t in_len, const uint8_t key[])
Definition: sodium_auth.cpp:39
Botan::Sodium::crypto_auth_hmacsha512256
int crypto_auth_hmacsha512256(uint8_t out[], const uint8_t in[], size_t in_len, const uint8_t key[])
Definition: sodium_auth.cpp:83
Botan::Sodium::crypto_auth_hmacsha256
int crypto_auth_hmacsha256(uint8_t out[], const uint8_t in[], size_t in_len, const uint8_t key[])
Definition: sodium_auth.cpp:109
Botan::Sodium::crypto_auth_hmacsha512_verify
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
Botan
Definition: alg_id.cpp:13
Botan::copy_mem
void copy_mem(T *out, const T *in, size_t n)
Definition: mem_ops.h:133
Botan::secure_vector
std::vector< T, secure_allocator< T > > secure_vector
Definition: secmem.h:65