hmac-def.el 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. ;;; hmac-def.el --- A macro for defining HMAC functions.
  2. ;; Copyright (C) 1999, 2001, 2007-2017 Free Software Foundation, Inc.
  3. ;; Author: Shuhei KOBAYASHI <shuhei@aqua.ocn.ne.jp>
  4. ;; Keywords: HMAC, RFC2104
  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 program is implemented from RFC2104,
  18. ;; "HMAC: Keyed-Hashing for Message Authentication".
  19. ;;; Code:
  20. (defmacro define-hmac-function (name H B L &optional bit)
  21. "Define a function NAME(TEXT KEY) which computes HMAC with function H.
  22. HMAC function is H(KEY XOR opad, H(KEY XOR ipad, TEXT)):
  23. H is a cryptographic hash function, such as SHA1 and MD5, which takes
  24. a string and return a digest of it (in binary form).
  25. B is a byte-length of a block size of H. (B=64 for both SHA1 and MD5.)
  26. L is a byte-length of hash outputs. (L=16 for MD5, L=20 for SHA1.)
  27. If BIT is non-nil, truncate output to specified bits."
  28. `(defun ,name (text key)
  29. ,(concat "Compute "
  30. (upcase (symbol-name name))
  31. " over TEXT with KEY.")
  32. (let ((key-xor-ipad (make-string ,B ?\x36))
  33. (key-xor-opad (make-string ,B ?\x5C))
  34. (len (length key))
  35. (pos 0))
  36. (unwind-protect
  37. (progn
  38. ;; if `key' is longer than the block size, apply hash function
  39. ;; to `key' and use the result as a real `key'.
  40. (if (> len ,B)
  41. (setq key (,H key)
  42. len ,L))
  43. (while (< pos len)
  44. (aset key-xor-ipad pos (logxor (aref key pos) ?\x36))
  45. (aset key-xor-opad pos (logxor (aref key pos) ?\x5C))
  46. (setq pos (1+ pos)))
  47. (setq key-xor-ipad (unwind-protect
  48. (concat key-xor-ipad text)
  49. (fillarray key-xor-ipad 0))
  50. key-xor-ipad (unwind-protect
  51. (,H key-xor-ipad)
  52. (fillarray key-xor-ipad 0))
  53. key-xor-opad (unwind-protect
  54. (concat key-xor-opad key-xor-ipad)
  55. (fillarray key-xor-opad 0))
  56. key-xor-opad (unwind-protect
  57. (,H key-xor-opad)
  58. (fillarray key-xor-opad 0)))
  59. ;; now `key-xor-opad' contains
  60. ;; H(KEY XOR opad, H(KEY XOR ipad, TEXT)).
  61. ,(if (and bit (< (/ bit 8) L))
  62. `(substring key-xor-opad 0 ,(/ bit 8))
  63. ;; return a copy of `key-xor-opad'.
  64. `(concat key-xor-opad)))
  65. ;; cleanup.
  66. (fillarray key-xor-ipad 0)
  67. (fillarray key-xor-opad 0)))))
  68. (provide 'hmac-def)
  69. ;;; hmac-def.el ends here