hmac_tests.rs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. use ring::{digest, error, hmac, test, test_file};
  15. #[cfg(target_arch = "wasm32")]
  16. use wasm_bindgen_test::{wasm_bindgen_test, wasm_bindgen_test_configure};
  17. #[cfg(target_arch = "wasm32")]
  18. wasm_bindgen_test_configure!(run_in_browser);
  19. #[test]
  20. #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
  21. fn hmac_tests() {
  22. test::run(test_file!("hmac_tests.txt"), |section, test_case| {
  23. assert_eq!(section, "");
  24. let digest_alg = test_case.consume_digest_alg("HMAC");
  25. let key_value = test_case.consume_bytes("Key");
  26. let mut input = test_case.consume_bytes("Input");
  27. let output = test_case.consume_bytes("Output");
  28. let algorithm = {
  29. let digest_alg = match digest_alg {
  30. Some(digest_alg) => digest_alg,
  31. None => {
  32. return Ok(());
  33. } // Unsupported digest algorithm
  34. };
  35. if digest_alg == &digest::SHA1_FOR_LEGACY_USE_ONLY {
  36. hmac::HMAC_SHA1_FOR_LEGACY_USE_ONLY
  37. } else if digest_alg == &digest::SHA256 {
  38. hmac::HMAC_SHA256
  39. } else if digest_alg == &digest::SHA384 {
  40. hmac::HMAC_SHA384
  41. } else if digest_alg == &digest::SHA512 {
  42. hmac::HMAC_SHA512
  43. } else {
  44. unreachable!()
  45. }
  46. };
  47. hmac_test_case_inner(algorithm, &key_value[..], &input[..], &output[..], true)?;
  48. // Tamper with the input and check that verification fails.
  49. if input.is_empty() {
  50. input.push(0);
  51. } else {
  52. input[0] ^= 1;
  53. }
  54. hmac_test_case_inner(algorithm, &key_value[..], &input[..], &output[..], false)
  55. });
  56. }
  57. fn hmac_test_case_inner(
  58. algorithm: hmac::Algorithm,
  59. key_value: &[u8],
  60. input: &[u8],
  61. output: &[u8],
  62. is_ok: bool,
  63. ) -> Result<(), error::Unspecified> {
  64. let key = hmac::Key::new(algorithm, key_value);
  65. // One-shot API.
  66. {
  67. let signature = hmac::sign(&key, input);
  68. assert_eq!(is_ok, signature.as_ref() == output);
  69. #[cfg(any(not(target_arch = "wasm32"), feature = "wasm32_c"))]
  70. assert_eq!(is_ok, hmac::verify(&key, input, output).is_ok());
  71. }
  72. // Multi-part API, one single part.
  73. {
  74. let mut s_ctx = hmac::Context::with_key(&key);
  75. s_ctx.update(input);
  76. let signature = s_ctx.sign();
  77. assert_eq!(is_ok, signature.as_ref() == output);
  78. }
  79. // Multi-part API, byte by byte.
  80. {
  81. let mut ctx = hmac::Context::with_key(&key);
  82. for b in input {
  83. ctx.update(&[*b]);
  84. }
  85. let signature = ctx.sign();
  86. assert_eq!(is_ok, signature.as_ref() == output);
  87. }
  88. Ok(())
  89. }
  90. #[test]
  91. #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
  92. fn hmac_debug() {
  93. let key = hmac::Key::new(hmac::HMAC_SHA256, &[0; 32]);
  94. assert_eq!("Key { algorithm: SHA256 }", format!("{:?}", &key));
  95. let ctx = hmac::Context::with_key(&key);
  96. assert_eq!("Context { algorithm: SHA256 }", format!("{:?}", &ctx));
  97. }