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 #![forbid(
16 anonymous_parameters,
17 box_pointers,
18 missing_copy_implementations,
19 missing_debug_implementations,
20 missing_docs,
21 trivial_casts,
22 trivial_numeric_casts,
23 unsafe_code,
24 unstable_features,
25 unused_extern_crates,
26 unused_import_braces,
27 unused_qualifications,
28 unused_results,
29 variant_size_differences,
30 warnings
31 )]
32
33 use ring::{digest, error, hmac, test, test_file};
34
35 #[cfg(target_arch = "wasm32")]
36 use wasm_bindgen_test::{wasm_bindgen_test, wasm_bindgen_test_configure};
37
38 #[cfg(target_arch = "wasm32")]
39 wasm_bindgen_test_configure!(run_in_browser);
40
41 #[test]
42 #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
hmac_tests()43 fn hmac_tests() {
44 test::run(test_file!("hmac_tests.txt"), |section, test_case| {
45 assert_eq!(section, "");
46 let digest_alg = test_case.consume_digest_alg("HMAC");
47 let key_value = test_case.consume_bytes("Key");
48 let mut input = test_case.consume_bytes("Input");
49 let output = test_case.consume_bytes("Output");
50
51 let algorithm = {
52 let digest_alg = match digest_alg {
53 Some(digest_alg) => digest_alg,
54 None => {
55 return Ok(());
56 } // Unsupported digest algorithm
57 };
58 if digest_alg == &digest::SHA1_FOR_LEGACY_USE_ONLY {
59 hmac::HMAC_SHA1_FOR_LEGACY_USE_ONLY
60 } else if digest_alg == &digest::SHA256 {
61 hmac::HMAC_SHA256
62 } else if digest_alg == &digest::SHA384 {
63 hmac::HMAC_SHA384
64 } else if digest_alg == &digest::SHA512 {
65 hmac::HMAC_SHA512
66 } else {
67 unreachable!()
68 }
69 };
70
71 hmac_test_case_inner(algorithm, &key_value[..], &input[..], &output[..], true)?;
72
73 // Tamper with the input and check that verification fails.
74 if input.is_empty() {
75 input.push(0);
76 } else {
77 input[0] ^= 1;
78 }
79
80 hmac_test_case_inner(algorithm, &key_value[..], &input[..], &output[..], false)
81 });
82 }
83
hmac_test_case_inner( algorithm: hmac::Algorithm, key_value: &[u8], input: &[u8], output: &[u8], is_ok: bool, ) -> Result<(), error::Unspecified>84 fn hmac_test_case_inner(
85 algorithm: hmac::Algorithm,
86 key_value: &[u8],
87 input: &[u8],
88 output: &[u8],
89 is_ok: bool,
90 ) -> Result<(), error::Unspecified> {
91 let key = hmac::Key::new(algorithm, key_value);
92
93 // One-shot API.
94 {
95 let signature = hmac::sign(&key, input);
96 assert_eq!(is_ok, signature.as_ref() == output);
97
98 #[cfg(any(not(target_arch = "wasm32"), feature = "wasm32_c"))]
99 assert_eq!(is_ok, hmac::verify(&key, input, output).is_ok());
100 }
101
102 // Multi-part API, one single part.
103 {
104 let mut s_ctx = hmac::Context::with_key(&key);
105 s_ctx.update(input);
106 let signature = s_ctx.sign();
107 assert_eq!(is_ok, signature.as_ref() == output);
108 }
109
110 // Multi-part API, byte by byte.
111 {
112 let mut ctx = hmac::Context::with_key(&key);
113 for b in input {
114 ctx.update(&[*b]);
115 }
116 let signature = ctx.sign();
117 assert_eq!(is_ok, signature.as_ref() == output);
118 }
119
120 Ok(())
121 }
122
123 #[test]
124 #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
hmac_debug()125 fn hmac_debug() {
126 let key = hmac::Key::new(hmac::HMAC_SHA256, &[0; 32]);
127 assert_eq!("Key { algorithm: SHA256 }", format!("{:?}", &key));
128
129 let ctx = hmac::Context::with_key(&key);
130 assert_eq!("Context { algorithm: SHA256 }", format!("{:?}", &ctx));
131 }
132