sha512.patch 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. --- USM.pm.default 2025-01-24 17:51:21.777264690 +0300
  2. +++ USM.pm 2025-01-24 18:03:43.338158040 +0300
  3. @@ -25,8 +25,8 @@
  4. use Crypt::DES();
  5. use Digest::MD5();
  6. -use Digest::SHA();
  7. -use Digest::HMAC();
  8. +use Digest::SHA qw( hmac_sha1 hmac_sha224 hmac_sha256 hmac_sha384 hmac_sha512 );
  9. +use Digest::HMAC_MD5 qw ( hmac_md5 );
  10. ## Version of the Net::SNMP::Security::USM module
  11. @@ -40,7 +40,9 @@
  12. our %EXPORT_TAGS = (
  13. authprotos => [
  14. - qw( AUTH_PROTOCOL_NONE AUTH_PROTOCOL_HMACMD5 AUTH_PROTOCOL_HMACSHA )
  15. + qw( AUTH_PROTOCOL_NONE AUTH_PROTOCOL_HMACMD5 AUTH_PROTOCOL_HMACSHA
  16. + AUTH_PROTOCOL_HMACSHA224 AUTH_PROTOCOL_HMACSHA256.
  17. + AUTH_PROTOCOL_HMACSHA384 AUTH_PROTOCOL_HMACSHA512 )
  18. ],
  19. levels => [
  20. qw( SECURITY_LEVEL_NOAUTHNOPRIV SECURITY_LEVEL_AUTHNOPRIV
  21. @@ -63,9 +65,13 @@
  22. ## RCC 3414 - Authentication protocols
  23. -sub AUTH_PROTOCOL_NONE { '1.3.6.1.6.3.10.1.1.1' } # usmNoAuthProtocol
  24. -sub AUTH_PROTOCOL_HMACMD5 { '1.3.6.1.6.3.10.1.1.2' } # usmHMACMD5AuthProtocol
  25. -sub AUTH_PROTOCOL_HMACSHA { '1.3.6.1.6.3.10.1.1.3' } # usmHMACSHAAuthProtocol
  26. +sub AUTH_PROTOCOL_NONE { '1.3.6.1.6.3.10.1.1.1' } # usmNoAuthProtocol
  27. +sub AUTH_PROTOCOL_HMACMD5 { '1.3.6.1.6.3.10.1.1.2' } # usmHMACMD5AuthProtocol
  28. +sub AUTH_PROTOCOL_HMACSHA { '1.3.6.1.6.3.10.1.1.3' } # usmHMACSHAAuthProtocol
  29. +sub AUTH_PROTOCOL_HMACSHA224 { '1.3.6.1.6.3.10.1.1.4' } # usmHMAC128SHA224AuthProtocol
  30. +sub AUTH_PROTOCOL_HMACSHA256 { '1.3.6.1.6.3.10.1.1.5' } # usmHMAC192SHA256AuthProtocol
  31. +sub AUTH_PROTOCOL_HMACSHA384 { '1.3.6.1.6.3.10.1.1.6' } # usmHMAC256SHA384AuthProtocol
  32. +sub AUTH_PROTOCOL_HMACSHA512 { '1.3.6.1.6.3.10.1.1.7' } # usmHMAC384SHA512AuthProtocol
  33. ## RFC 3414 - Privacy protocols
  34. @@ -124,6 +130,7 @@
  35. '_time_epoc' => time(), # snmpEngineBoots epoc
  36. '_user_name' => q{}, # securityName
  37. '_auth_data' => undef, # Authentication data
  38. + '_auth_maclen' => undef, # MAC length
  39. '_auth_key' => undef, # authKey
  40. '_auth_password' => undef, # Authentication password
  41. '_auth_protocol' => AUTH_PROTOCOL_HMACMD5, # authProtocol
  42. @@ -280,10 +287,10 @@
  43. if ($pdu->security_level() > SECURITY_LEVEL_NOAUTHNOPRIV) {
  44. # Save the location to fill in msgAuthenticationParameters later
  45. - $auth_location = $msg->length() + 12 + length $pdu_buffer;
  46. + $auth_location = $msg->length() + $this->{_auth_maclen} + length $pdu_buffer;
  47. # Set the msgAuthenticationParameters to all zeros
  48. - $auth_params = pack 'x12';
  49. + $auth_params = pack "x$this->{_auth_maclen}";
  50. }
  51. if (!defined $msg->prepare(OCTET_STRING, $auth_params)) {
  52. @@ -418,12 +425,12 @@
  53. # to compute the HMAC properly.
  54. if (my $len = length $auth_params) {
  55. - if ($len != 12) {
  56. + if ($len != $this->{_auth_maclen}) {
  57. return $this->_error(
  58. 'The msgAuthenticationParameters length of %d is invalid', $len
  59. );
  60. }
  61. - substr ${$msg->reference}, ($msg->index() - 12), 12, pack 'x12';
  62. + substr ${$msg->reference}, ($msg->index() - $this->{_auth_maclen}), $this->{_auth_maclen}, pack "x$this->{_auth_maclen}";
  63. }
  64. # msgPrivacyParameters::=OCTET STRING
  65. @@ -747,6 +754,18 @@
  66. quotemeta AUTH_PROTOCOL_HMACMD5, AUTH_PROTOCOL_HMACMD5,
  67. '(?:hmac-)?sha(?:-?1|-96)?', AUTH_PROTOCOL_HMACSHA,
  68. quotemeta AUTH_PROTOCOL_HMACSHA, AUTH_PROTOCOL_HMACSHA,
  69. + '(?:hmac-)?sha(?:-?224)', AUTH_PROTOCOL_HMACSHA224,
  70. + 'usmHMAC128SHA224AuthProtocol', AUTH_PROTOCOL_HMACSHA224,
  71. + quotemeta AUTH_PROTOCOL_HMACSHA224,AUTH_PROTOCOL_HMACSHA224,
  72. + '(?:hmac-)?sha(?:-?256)', AUTH_PROTOCOL_HMACSHA256,
  73. + 'usmHMAC192SHA256AuthProtocol', AUTH_PROTOCOL_HMACSHA256,
  74. + quotemeta AUTH_PROTOCOL_HMACSHA256,AUTH_PROTOCOL_HMACSHA256,
  75. + '(?:hmac-)?sha(?:-?384)', AUTH_PROTOCOL_HMACSHA384,
  76. + 'usmHMAC256SHA384AuthProtocol', AUTH_PROTOCOL_HMACSHA384,
  77. + quotemeta AUTH_PROTOCOL_HMACSHA384,AUTH_PROTOCOL_HMACSHA384,
  78. + '(?:hmac-)?sha(?:-?512)', AUTH_PROTOCOL_HMACSHA512,
  79. + 'usmHMAC384SHA512AuthProtocol', AUTH_PROTOCOL_HMACSHA512,
  80. + quotemeta AUTH_PROTOCOL_HMACSHA512,AUTH_PROTOCOL_HMACSHA512,
  81. };
  82. sub _auth_protocol
  83. @@ -1099,8 +1118,7 @@
  84. }
  85. # Set the msgAuthenticationParameters
  86. - substr ${$msg->reference}, -$auth_location, 12, $this->_auth_hmac($msg);
  87. -
  88. + substr ${$msg->reference}, -$auth_location, $this->{_auth_maclen}, $this->_auth_hmac($msg);
  89. return TRUE;
  90. }
  91. @@ -1125,7 +1143,7 @@
  92. return q{} if (!defined($this->{_auth_data}) || !defined $msg);
  93. return substr
  94. - $this->{_auth_data}->reset()->add(${$msg->reference()})->digest(), 0, 12;
  95. + $this->{_auth_data}(${$msg->reference()}, $this->{_auth_key}), 0, $this->{_auth_maclen};
  96. }
  97. sub _auth_data_init
  98. @@ -1140,13 +1158,33 @@
  99. if ($this->{_auth_protocol} eq AUTH_PROTOCOL_HMACMD5) {
  100. - $this->{_auth_data} =
  101. - Digest::HMAC->new($this->{_auth_key}, 'Digest::MD5');
  102. + $this->{_auth_data} = \&hmac_md5;
  103. + $this->{_auth_maclen} = 12;
  104. } elsif ($this->{_auth_protocol} eq AUTH_PROTOCOL_HMACSHA) {
  105. - $this->{_auth_data} =
  106. - Digest::HMAC->new($this->{_auth_key}, 'Digest::SHA');
  107. + $this->{_auth_data} = \&hmac_sha1;
  108. + $this->{_auth_maclen} = 12;
  109. +
  110. + } elsif ($this->{_auth_protocol} eq AUTH_PROTOCOL_HMACSHA224) {
  111. +
  112. + $this->{_auth_data} = \&hmac_sha224;
  113. + $this->{_auth_maclen} = 16;
  114. +
  115. + } elsif ($this->{_auth_protocol} eq AUTH_PROTOCOL_HMACSHA256) {
  116. +
  117. + $this->{_auth_data} = \&hmac_sha256;
  118. + $this->{_auth_maclen} = 24;
  119. +
  120. + } elsif ($this->{_auth_protocol} eq AUTH_PROTOCOL_HMACSHA384) {
  121. +
  122. + $this->{_auth_data} = \&hmac_sha384;
  123. + $this->{_auth_maclen} = 32;
  124. +
  125. + } elsif ($this->{_auth_protocol} eq AUTH_PROTOCOL_HMACSHA512) {
  126. +
  127. + $this->{_auth_data} = \&hmac_sha512;
  128. + $this->{_auth_maclen} = 48;
  129. } else {
  130. @@ -1627,6 +1665,10 @@
  131. {
  132. AUTH_PROTOCOL_HMACMD5, [ 16, 'HMAC-MD5' ],
  133. AUTH_PROTOCOL_HMACSHA, [ 20, 'HMAC-SHA' ],
  134. + AUTH_PROTOCOL_HMACSHA224, [ 28, 'HMAC-SHA224' ],
  135. + AUTH_PROTOCOL_HMACSHA256, [ 32, 'HMAC-SHA256' ],
  136. + AUTH_PROTOCOL_HMACSHA384, [ 48, 'HMAC-SHA384' ],
  137. + AUTH_PROTOCOL_HMACSHA512, [ 64, 'HMAC-SHA512' ],
  138. };
  139. if (!exists $key_len->{$this->{_auth_protocol}}) {
  140. @@ -1782,9 +1824,13 @@
  141. my $digests =
  142. {
  143. - AUTH_PROTOCOL_HMACMD5, 'Digest::MD5',
  144. - AUTH_PROTOCOL_HMACSHA, 'Digest::SHA',
  145. - };
  146. + AUTH_PROTOCOL_HMACMD5, ['Digest::MD5', ],
  147. + AUTH_PROTOCOL_HMACSHA, ['Digest::SHA', 1],
  148. + AUTH_PROTOCOL_HMACSHA224, ['Digest::SHA', 224],
  149. + AUTH_PROTOCOL_HMACSHA256, ['Digest::SHA', 256],
  150. + AUTH_PROTOCOL_HMACSHA384, ['Digest::SHA', 384],
  151. + AUTH_PROTOCOL_HMACSHA512, ['Digest::SHA', 512],
  152. + };
  153. if (!exists $digests->{$this->{_auth_protocol}}) {
  154. return $this->_error(
  155. @@ -1792,7 +1838,12 @@
  156. );
  157. }
  158. - my $digest = $digests->{$this->{_auth_protocol}}->new;
  159. + my $digest;
  160. + if (!defined($digests->{$this->{_auth_protocol}}[1])) {
  161. + $digest = $digests->{$this->{_auth_protocol}}[0]->new;
  162. + } else {
  163. + $digest = $digests->{$this->{_auth_protocol}}[0]->new($digests->{$this->{_auth_protocol}}[1]);
  164. + }
  165. # Create the initial digest using the password