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
//! Secret Key Message Authentication
//!
//! This operation computes an authentication tag for a message and a secret
//! key, and provides a way to verify that a given tag is valid for a given
//! message and a key.
//!
//! The function computing the tag deterministic: the same (message, key) tuple
//! will always produce the same output.
//!
//! However, even if the message is public, knowing the key is required in order
//! to be able to compute a valid tag. Therefore, the key should remain
//! confidential. The tag, however, can be public.
//!
//! A typical use case is:
//!
//! - A prepares a message, add an authentication tag, sends it to B
//! - A doesn't store the message
//! - Later on, B sends the message and the authentication tag to A
//! - A uses the authentication tag to verify that it created this message.
//! This operation does not encrypt the message. It only computes and verifies
//! an authentication tag.
use ;
use ;
use secmem;
/// 32 bytes.
pub const BYTES: usize = 32;
/// 32 bytes.
pub const KEYBYTES: usize = 32;
extern "C"
/// The *auth()* function computes a tag for the message and a key. The key
/// should be KEYBYTES bytes. The function return the tag byte sequence.
///
/// # Examples
///
/// ```
/// use sodium_sys::crypto::utils::init;
/// use sodium_sys::crypto::symmetrickey::{auth,key};
///
/// // Initialize sodium_sys
/// init::init();
///
/// // Create the key and activate for use.
/// let key = key::Key::new(auth::KEYBYTES);
/// key.activate();
///
/// // Generate the MAC and protect it as readonly.
/// let mac = auth::auth(b"test", key.bytes()).unwrap();
///
/// println!("{:?}", mac);
/// ```
/// The *auth_verify()* function verifies that the mac is a valid mac for the
/// given message and the key k.
///
/// # Examples
///
/// ```
/// use sodium_sys::crypto::utils::init;
/// use sodium_sys::crypto::symmetrickey::{auth,key};
///
/// // Initialize sodium_sys
/// init::init();
///
/// // Create the key and activate for use.
/// let key = key::Key::new(auth::KEYBYTES);
/// key.activate();
///
/// // Generate the MAC and protect it as readonly.
/// let mac = auth::auth(b"test", key.bytes()).unwrap();
///
/// // Verify the MAC and message.
/// let res = auth::auth_verify(b"test", mac, key.bytes()).unwrap();
/// assert!(res == 0);
/// ```