rfc2104.el 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. ;;; rfc2104.el --- RFC2104 Hashed Message Authentication Codes
  2. ;; Copyright (C) 1998-2012 Free Software Foundation, Inc.
  3. ;; Author: Simon Josefsson <jas@pdc.kth.se>
  4. ;; Keywords: mail
  5. ;; This file is part of GNU Emacs.
  6. ;; GNU Emacs is free software: you can redistribute it and/or modify
  7. ;; it under the terms of the GNU General Public License as published by
  8. ;; the Free Software Foundation, either version 3 of the License, or
  9. ;; (at your option) any later version.
  10. ;; GNU Emacs is distributed in the hope that it will be useful,
  11. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ;; GNU General Public License for more details.
  14. ;; You should have received a copy of the GNU General Public License
  15. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  16. ;;; Commentary:
  17. ;; This is a high performance implementation of RFC2104.
  18. ;;
  19. ;; Example:
  20. ;;
  21. ;; (require 'md5)
  22. ;; (rfc2104-hash 'md5 64 16 "Jefe" "what do ya want for nothing?")
  23. ;; "750c783e6ab0b503eaa86e310a5db738"
  24. ;;
  25. ;; (require 'sha1)
  26. ;; (rfc2104-hash 'sha1 64 20 "Jefe" "what do ya want for nothing?")
  27. ;; "effcdf6ae5eb2fa2d27416d5f184df9c259a7c79"
  28. ;;
  29. ;; 64 is block length of hash function (64 for MD5 and SHA), 16 is
  30. ;; resulting hash length (16 for MD5, 20 for SHA).
  31. ;;
  32. ;; Tested with Emacs 20.2 and XEmacs 20.3.
  33. ;;
  34. ;; Test case reference: RFC 2202.
  35. ;;; History:
  36. ;; 1998-08-16 initial release posted to gnu.emacs.sources
  37. ;; 1998-08-17 use append instead of char-list-to-string
  38. ;; 1998-08-26 don't require hexl
  39. ;; 1998-09-25 renamed from hmac.el to rfc2104.el, also renamed functions
  40. ;; 1999-10-23 included in pgnus
  41. ;; 2000-08-15 `rfc2104-hexstring-to-bitstring'
  42. ;; 2000-05-12 added sha-1 example, added test case reference
  43. ;; 2003-11-13 change rfc2104-hexstring-to-bitstring to ...-byte-list
  44. ;; 2008-04-25 rewrite rfc2104-hash for speed
  45. ;;; Code:
  46. (eval-when-compile (require 'cl))
  47. ;; Magic character for inner HMAC round. 0x36 == 54 == '6'
  48. (defconst rfc2104-ipad ?\x36)
  49. ;; Magic character for outer HMAC round. 0x5C == 92 == '\'
  50. (defconst rfc2104-opad ?\x5C)
  51. (defconst rfc2104-nybbles
  52. (let ((v (make-vector
  53. ;; Find upper bound to save some space.
  54. (1+ (max ?0 ?9 ?a ?f ?A ?F))
  55. ;; Use non-numeric default to catch bogus hex strings.
  56. nil))
  57. (ls '((?0 . 0) (?a . 10) (?A . 10)
  58. (?1 . 1) (?b . 11) (?B . 11)
  59. (?2 . 2) (?c . 12) (?C . 12)
  60. (?3 . 3) (?d . 13) (?D . 13)
  61. (?4 . 4) (?e . 14) (?E . 14)
  62. (?5 . 5) (?f . 15) (?F . 15)
  63. (?6 . 6)
  64. (?7 . 7)
  65. (?8 . 8)
  66. (?9 . 9))))
  67. (while ls
  68. (aset v (caar ls) (cdar ls))
  69. (setq ls (cdr ls)))
  70. v))
  71. (eval-when-compile
  72. (defmacro rfc2104-string-make-unibyte (string)
  73. "Return the unibyte equivalent of STRING.
  74. In XEmacs return just STRING."
  75. (if (featurep 'xemacs)
  76. string
  77. `(string-make-unibyte ,string))))
  78. (defun rfc2104-hash (hash block-length hash-length key text)
  79. (let* (;; if key is longer than B, reset it to HASH(key)
  80. (key (if (> (length key) block-length)
  81. (funcall hash key) key))
  82. (len (length key))
  83. (ipad (make-string block-length rfc2104-ipad))
  84. (opad (make-string (+ block-length hash-length) rfc2104-opad))
  85. c partial)
  86. ;; Prefix *pad with key, appropriately XORed.
  87. (do ((i 0 (1+ i)))
  88. ((= len i))
  89. (setq c (aref key i))
  90. (aset ipad i (logxor rfc2104-ipad c))
  91. (aset opad i (logxor rfc2104-opad c)))
  92. ;; Perform inner hash.
  93. (setq partial (rfc2104-string-make-unibyte
  94. (funcall hash (concat ipad text))))
  95. ;; Pack latter part of opad.
  96. (do ((r 0 (+ 2 r))
  97. (w block-length (1+ w)))
  98. ((= (* 2 hash-length) r))
  99. (aset opad w
  100. (+ (* 16 (aref rfc2104-nybbles (aref partial r)))
  101. ( aref rfc2104-nybbles (aref partial (1+ r))))))
  102. ;; Perform outer hash.
  103. (rfc2104-string-make-unibyte (funcall hash opad))))
  104. (provide 'rfc2104)
  105. ;;; rfc2104.el ends here