rust-hmac-sha1-update-dependencies.patch 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. From 0ec5e893bc16eb051f5fbfd453eee2e344369a91 Mon Sep 17 00:00:00 2001
  2. From: Philip Woolford <woolford.philip@gmail.com>
  3. Date: Tue, 1 Mar 2022 19:48:56 +1030
  4. Subject: [PATCH] Update dependencies and complete rfc2202 test coverage.
  5. Update sha1 to 0.10, change rustc-serialize to hex 0.4.3
  6. This also adds additional RFC test vectors.
  7. Upstream patch: <https://github.com/pantsman0/rust-hmac-sha1/pull/2>.
  8. As the patch hasn't been accepted yet, the change in version number
  9. has been dropped.
  10. ---
  11. Cargo.toml | 8 ++++---
  12. LICENSE | 2 +-
  13. src/lib.rs | 68 +++++++++++++++++++++++++++++++++++++++---------------
  14. 3 files changed, 55 insertions(+), 23 deletions(-)
  15. diff --git a/Cargo.toml b/Cargo.toml
  16. index cbd1959..ee26f0e 100644
  17. --- a/Cargo.toml
  18. +++ b/Cargo.toml
  19. @@ -1,9 +1,11 @@
  20. [package]
  21. +edition = "2018"
  22. name = "hmac-sha1"
  23. version = "0.1.3"
  24. authors = ["Philip Woolford <woolford.philip@gmail.com>"]
  25. keywords = ["hmac", "sha1"]
  26. description = "Minimal implementation of HMAC-SHA1 in Rust."
  27. +license = "BSD 3-CLAUSE"
  28. license-file = "LICENSE"
  29. repository = "https://github.com/pantsman0/rust-hmac-sha1"
  30. @@ -18,7 +20,7 @@ name = "hmacsha1"
  31. path = "src/lib.rs"
  32. [dependencies]
  33. -sha1 = "^0.2.0"
  34. +sha1 = "^0.10.0"
  35. [dev-dependencies]
  36. -rustc-serialize = "0.3"
  37. +hex = "0.4.3"
  38. diff --git a/LICENSE b/LICENSE
  39. index 5ea1dd4..1e359f1 100644
  40. --- a/LICENSE
  41. +++ b/LICENSE
  42. @@ -1,4 +1,4 @@
  43. -Copyright © 2016 Philip Woolford
  44. +Copyright © 2022 Philip Woolford
  45. Some rights reserved.
  46. diff --git a/src/lib.rs b/src/lib.rs
  47. index 934404e..1123e17 100644
  48. --- a/src/lib.rs
  49. +++ b/src/lib.rs
  50. @@ -1,4 +1,4 @@
  51. -extern crate sha1;
  52. +use sha1::{Sha1, Digest};
  53. // define hash constants
  54. pub const SHA1_DIGEST_BYTES: usize = 20;
  55. @@ -11,19 +11,16 @@ pub fn hmac_sha1(key: &[u8], message: &[u8]) -> [u8; SHA1_DIGEST_BYTES] {
  56. let key_pad_byte: u8 = 0x00;
  57. // instantiate internal structures
  58. - let mut sha1_ctx = sha1::Sha1::new();
  59. - let mut auth_key: &mut [u8; SHA1_KEY_BYTES] = &mut [key_pad_byte; SHA1_KEY_BYTES];
  60. + let mut sha1_ctx = Sha1::new();
  61. + let auth_key: &mut [u8; SHA1_KEY_BYTES] = &mut [key_pad_byte; SHA1_KEY_BYTES];
  62. // if the key is longer than the hasher's block length, it should be truncated using the hasher
  63. - if { key.len() > SHA1_KEY_BYTES } {
  64. + if key.len() > SHA1_KEY_BYTES {
  65. // derive new authentication from provided key
  66. sha1_ctx.update(key);
  67. // assign derived authentication key
  68. - auth_key[..SHA1_DIGEST_BYTES].copy_from_slice(&(sha1_ctx.digest().bytes()));
  69. -
  70. - // reset hash for reuse
  71. - sha1_ctx.reset();
  72. + auth_key[..SHA1_DIGEST_BYTES].copy_from_slice(&(sha1_ctx.finalize_reset()[..]));
  73. } else {
  74. auth_key[..key.len()].copy_from_slice(key);
  75. }
  76. @@ -40,23 +37,23 @@ pub fn hmac_sha1(key: &[u8], message: &[u8]) -> [u8; SHA1_DIGEST_BYTES] {
  77. // perform inner hash
  78. sha1_ctx.update(&inner_padding);
  79. sha1_ctx.update(message);
  80. - let inner_hash = sha1_ctx.digest().bytes();
  81. - sha1_ctx.reset();
  82. + let inner_hash = sha1_ctx.finalize_reset();
  83. // perform outer hash
  84. + let mut return_buffer = [0u8;20];
  85. sha1_ctx.update(&outer_padding);
  86. sha1_ctx.update(&inner_hash);
  87. - sha1_ctx.digest().bytes()
  88. + return_buffer[..SHA1_DIGEST_BYTES].copy_from_slice(&(sha1_ctx.finalize()[..]));
  89. + return_buffer
  90. }
  91. -#[cfg(test)]extern crate rustc_serialize;
  92. +
  93. #[cfg(test)]
  94. mod tests {
  95. + use crate::*;
  96. - use hmac_sha1;
  97. -
  98. - use rustc_serialize::hex::ToHex;
  99. + use hex::ToHex;
  100. #[test]
  101. @@ -67,7 +64,7 @@ mod tests {
  102. let expected = "b617318655057264e28bc0b6fb378c8ef146be00".to_string();
  103. let hash = hmac_sha1(key, data);
  104. - assert_eq!(hash.to_hex(),expected);
  105. + assert_eq!(hash.encode_hex::<String>(),expected);
  106. }
  107. #[test]
  108. @@ -78,7 +75,7 @@ mod tests {
  109. let expected = "effcdf6ae5eb2fa2d27416d5f184df9c259a7c79".to_string();
  110. let hash = hmac_sha1(key, data);
  111. - assert_eq!(hash.to_hex(),expected);
  112. + assert_eq!(hash.encode_hex::<String>(),expected);
  113. }
  114. #[test]
  115. @@ -89,7 +86,7 @@ mod tests {
  116. let expected = "125d7342b9ac11cd91a39af48aa17b4f63f175d3".to_string();
  117. let hash = hmac_sha1(key, data);
  118. - assert_eq!(hash.to_hex(),expected);
  119. + assert_eq!(hash.encode_hex::<String>(),expected);
  120. }
  121. #[test]
  122. @@ -100,6 +97,39 @@ mod tests {
  123. let expected = "4c9007f4026250c6bc8414f9bf50c86c2d7235da".to_string();
  124. let hash = hmac_sha1(key, data);
  125. - assert_eq!(hash.to_hex(),expected);
  126. + assert_eq!(hash.encode_hex::<String>(),expected);
  127. + }
  128. +
  129. + #[test]
  130. + fn test_vector5() {
  131. + // tuples of (data, key, expected hex string)
  132. + let data = "Test With Truncation".as_bytes();
  133. + let key = &[0x0c;20];
  134. + let expected = "4c1a03424b55e07fe7f27be1d58bb9324a9a5a04".to_string();
  135. +
  136. + let hash = hmac_sha1(key, data);
  137. + assert_eq!(hash.encode_hex::<String>(),expected);
  138. + }
  139. +
  140. + #[test]
  141. + fn test_vector6() {
  142. + // tuples of (data, key, expected hex string)
  143. + let data = "Test Using Larger Than Block-Size Key - Hash Key First".as_bytes();
  144. + let key = &[0xaa;80];
  145. + let expected = "aa4ae5e15272d00e95705637ce8a3b55ed402112".to_string();
  146. +
  147. + let hash = hmac_sha1(key, data);
  148. + assert_eq!(hash.encode_hex::<String>(),expected);
  149. + }
  150. +
  151. + #[test]
  152. + fn test_vector7() {
  153. + // tuples of (data, key, expected hex string)
  154. + let data = "Test Using Larger Than Block-Size Key and Larger Than One Block-Size Data".as_bytes();
  155. + let key = &[0xaa;80];
  156. + let expected = "e8e99d0f45237d786d6bbaa7965c7808bbff1a91".to_string();
  157. +
  158. + let hash = hmac_sha1(key, data);
  159. + assert_eq!(hash.encode_hex::<String>(),expected);
  160. }
  161. }