mac.scm 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. ;;; guile-gcrypt --- crypto tooling for guile
  2. ;;; Copyright © 2016 Christopher Allan Webber <cwebber@dustycloud.org>
  3. ;;; Copyright © 2019 Ludovic Courtès <ludo@gnu.org>
  4. ;;;
  5. ;;; This file is part of guile-gcrypt.
  6. ;;;
  7. ;;; guile-gcrypt is free software; you can redistribute it and/or modify it
  8. ;;; under the terms of the GNU General Public License as published by
  9. ;;; the Free Software Foundation; either version 3 of the License, or
  10. ;;; (at your option) any later version.
  11. ;;;
  12. ;;; guile-gcrypt is distributed in the hope that it will be useful, but
  13. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  14. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. ;;; General Public License for more details.
  16. ;;;
  17. ;;; You should have received a copy of the GNU General Public License
  18. ;;; along with guile-gcrypt. If not, see <http://www.gnu.org/licenses/>.
  19. (define-module (test-mac)
  20. #:use-module (rnrs bytevectors)
  21. #:use-module (srfi srfi-64)
  22. #:use-module (gcrypt mac))
  23. (test-begin "mac")
  24. (test-equal "lookup-mac-algorithm"
  25. (mac-algorithm hmac-sha3-256)
  26. (lookup-mac-algorithm 'hmac-sha3-256))
  27. (test-equal "mac-size"
  28. (list 32 28 64 64)
  29. (map mac-size
  30. (list (mac-algorithm hmac-sha256)
  31. (mac-algorithm hmac-sha224)
  32. (mac-algorithm hmac-sha512)
  33. (mac-algorithm hmac-sha3-512))))
  34. (define test-key (generate-signing-key))
  35. (let ((sig (sign-data test-key "monkey party"
  36. #:algorithm (mac-algorithm hmac-sha256))))
  37. ;; Should be a bytevector
  38. (test-assert (bytevector? sig))
  39. ;; Correct sig succeeds
  40. (test-assert (valid-signature? test-key "monkey party" sig
  41. #:algorithm (mac-algorithm hmac-sha256)))
  42. ;; Incorrect data fails
  43. (test-assert (not (valid-signature? test-key "something else" sig
  44. #:algorithm
  45. (mac-algorithm hmac-sha256))))
  46. ;; Fake signature fails
  47. (test-assert (not (valid-signature? test-key "monkey party"
  48. (string->utf8 "fake sig")
  49. #:algorithm
  50. (mac-algorithm hmac-sha256))))
  51. ;; Wrong algorithm fails
  52. (test-assert (not (valid-signature? test-key "monkey party" sig
  53. #:algorithm
  54. (mac-algorithm hmac-sha512))))
  55. ;; Should equal a re-run of itself
  56. (test-equal sig (sign-data test-key "monkey party"
  57. #:algorithm (mac-algorithm hmac-sha256)))
  58. ;; Shouldn't equal something different
  59. (test-assert (not (equal? sig (sign-data test-key "cookie party"
  60. #:algorithm
  61. (mac-algorithm hmac-sha256))))))
  62. ;; Now with a CMAC.
  63. (let* ((key (generate-signing-key 16))
  64. (sig (sign-data key "monkey party"
  65. #:algorithm (mac-algorithm cmac-aes))))
  66. ;; Should be a bytevector
  67. (test-assert (bytevector? sig))
  68. ;; Correct sig succeeds
  69. (test-assert (valid-signature? key "monkey party" sig
  70. #:algorithm (mac-algorithm cmac-aes)))
  71. ;; Fake signature fails
  72. (test-assert (not (valid-signature? key "monkey party"
  73. (string->utf8 "fake sig")
  74. #:algorithm (mac-algorithm cmac-aes)))))
  75. ;; Now with base64 encoding
  76. (let ((sig (sign-data-base64 test-key "monkey party")))
  77. ;; Should be a string
  78. (test-assert (string? sig))
  79. ;; Correct sig succeeds
  80. (test-assert (valid-base64-signature? test-key "monkey party" sig))
  81. ;; Incorrect data fails
  82. (test-assert (not (valid-base64-signature? test-key "something else" sig)))
  83. ;; Fake signature fails
  84. (test-assert (not (valid-base64-signature? test-key "monkey party"
  85. "f41c3516")))
  86. ;; Should equal a re-run of itself
  87. (test-equal sig (sign-data-base64 test-key "monkey party"))
  88. ;; Shouldn't equal something different
  89. (test-assert (not (equal? sig (sign-data-base64 test-key "cookie party")))))
  90. (test-end "mac")