1 // Copyright 2015-2016 Brian Smith.
2 //
3 // Permission to use, copy, modify, and/or distribute this software for any
4 // purpose with or without fee is hereby granted, provided that the above
5 // copyright notice and this permission notice appear in all copies.
6 //
7 // THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHORS DISCLAIM ALL WARRANTIES
8 // WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
9 // MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
10 // SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
11 // WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
12 // OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
13 // CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
14
15 use ring::{digest, error, hmac, test, test_file};
16
17 #[cfg(target_arch = "wasm32")]
18 use wasm_bindgen_test::{wasm_bindgen_test, wasm_bindgen_test_configure};
19
20 #[cfg(target_arch = "wasm32")]
21 wasm_bindgen_test_configure!(run_in_browser);
22
23 #[test]
24 #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
hmac_tests()25 fn hmac_tests() {
26 test::run(test_file!("hmac_tests.txt"), |section, test_case| {
27 assert_eq!(section, "");
28 let digest_alg = test_case.consume_digest_alg("HMAC");
29 let key_value = test_case.consume_bytes("Key");
30 let mut input = test_case.consume_bytes("Input");
31 let output = test_case.consume_bytes("Output");
32
33 let algorithm = {
34 let digest_alg = match digest_alg {
35 Some(digest_alg) => digest_alg,
36 None => {
37 return Ok(());
38 } // Unsupported digest algorithm
39 };
40 if digest_alg == &digest::SHA1_FOR_LEGACY_USE_ONLY {
41 hmac::HMAC_SHA1_FOR_LEGACY_USE_ONLY
42 } else if digest_alg == &digest::SHA256 {
43 hmac::HMAC_SHA256
44 } else if digest_alg == &digest::SHA384 {
45 hmac::HMAC_SHA384
46 } else if digest_alg == &digest::SHA512 {
47 hmac::HMAC_SHA512
48 } else {
49 unreachable!()
50 }
51 };
52
53 hmac_test_case_inner(algorithm, &key_value[..], &input[..], &output[..], true)?;
54
55 // Tamper with the input and check that verification fails.
56 if input.is_empty() {
57 input.push(0);
58 } else {
59 input[0] ^= 1;
60 }
61
62 hmac_test_case_inner(algorithm, &key_value[..], &input[..], &output[..], false)
63 });
64 }
65
hmac_test_case_inner( algorithm: hmac::Algorithm, key_value: &[u8], input: &[u8], output: &[u8], is_ok: bool, ) -> Result<(), error::Unspecified>66 fn hmac_test_case_inner(
67 algorithm: hmac::Algorithm,
68 key_value: &[u8],
69 input: &[u8],
70 output: &[u8],
71 is_ok: bool,
72 ) -> Result<(), error::Unspecified> {
73 let key = hmac::Key::new(algorithm, key_value);
74
75 // One-shot API.
76 {
77 let signature = hmac::sign(&key, input);
78 assert_eq!(is_ok, signature.as_ref() == output);
79
80 #[cfg(any(not(target_arch = "wasm32"), feature = "wasm32_c"))]
81 assert_eq!(is_ok, hmac::verify(&key, input, output).is_ok());
82 }
83
84 // Multi-part API, one single part.
85 {
86 let mut s_ctx = hmac::Context::with_key(&key);
87 s_ctx.update(input);
88 let signature = s_ctx.sign();
89 assert_eq!(is_ok, signature.as_ref() == output);
90 }
91
92 // Multi-part API, byte by byte.
93 {
94 let mut ctx = hmac::Context::with_key(&key);
95 for b in input {
96 ctx.update(&[*b]);
97 }
98 let signature = ctx.sign();
99 assert_eq!(is_ok, signature.as_ref() == output);
100 }
101
102 Ok(())
103 }
104
105 #[test]
106 #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
hmac_debug()107 fn hmac_debug() {
108 let key = hmac::Key::new(hmac::HMAC_SHA256, &[0; 32]);
109 assert_eq!("Key { algorithm: SHA256 }", format!("{:?}", &key));
110
111 let ctx = hmac::Context::with_key(&key);
112 assert_eq!("Context { algorithm: SHA256 }", format!("{:?}", &ctx));
113 }
114