mac.scm 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. ;;; guile-gcrypt --- crypto tooling for guile
  2. ;;; Copyright © 2016 Christine Lemmer-Webber <cwebber@dustycloud.org>
  3. ;;; Copyright © 2019, 2020 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
  8. ;;; modify it under the terms of the GNU Lesser General Public License
  9. ;;; as published by the Free Software Foundation; either version 3 of
  10. ;;; the License, or (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. ;;; Lesser General Public License for more details.
  16. ;;;
  17. ;;; You should have received a copy of the GNU Lesser 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-eq "mac-algorithm-name"
  28. 'hmac-sha3-512
  29. (mac-algorithm-name (mac-algorithm hmac-sha3-512)))
  30. (test-equal "mac-size"
  31. (list 32 28 64 64)
  32. (map mac-size
  33. (list (mac-algorithm hmac-sha256)
  34. (mac-algorithm hmac-sha224)
  35. (mac-algorithm hmac-sha512)
  36. (mac-algorithm hmac-sha3-512))))
  37. (define test-key (generate-signing-key))
  38. (let ((sig (sign-data test-key "monkey party"
  39. #:algorithm (mac-algorithm hmac-sha256))))
  40. ;; Should be a bytevector
  41. (test-assert (bytevector? sig))
  42. ;; Correct sig succeeds
  43. (test-assert (valid-signature? test-key "monkey party" sig
  44. #:algorithm (mac-algorithm hmac-sha256)))
  45. ;; Incorrect data fails
  46. (test-assert (not (valid-signature? test-key "something else" sig
  47. #:algorithm
  48. (mac-algorithm hmac-sha256))))
  49. ;; Fake signature fails
  50. (test-assert (not (valid-signature? test-key "monkey party"
  51. (string->utf8 "fake sig")
  52. #:algorithm
  53. (mac-algorithm hmac-sha256))))
  54. ;; Wrong algorithm fails
  55. (test-assert (not (valid-signature? test-key "monkey party" sig
  56. #:algorithm
  57. (mac-algorithm hmac-sha512))))
  58. ;; Should equal a re-run of itself
  59. (test-equal sig (sign-data test-key "monkey party"
  60. #:algorithm (mac-algorithm hmac-sha256)))
  61. ;; Shouldn't equal something different
  62. (test-assert (not (equal? sig (sign-data test-key "cookie party"
  63. #:algorithm
  64. (mac-algorithm hmac-sha256))))))
  65. ;; Now with a CMAC.
  66. (let* ((key (generate-signing-key 16))
  67. (sig (sign-data key "monkey party"
  68. #:algorithm (mac-algorithm cmac-aes))))
  69. ;; Should be a bytevector
  70. (test-assert (bytevector? sig))
  71. ;; Correct sig succeeds
  72. (test-assert (valid-signature? key "monkey party" sig
  73. #:algorithm (mac-algorithm cmac-aes)))
  74. ;; Fake signature fails
  75. (test-assert (not (valid-signature? key "monkey party"
  76. (string->utf8 "fake sig")
  77. #:algorithm (mac-algorithm cmac-aes)))))
  78. ;; Now with base64 encoding
  79. (let ((sig (sign-data-base64 test-key "monkey party")))
  80. ;; Should be a string
  81. (test-assert (string? sig))
  82. ;; Correct sig succeeds
  83. (test-assert (valid-base64-signature? test-key "monkey party" sig))
  84. ;; Incorrect data fails
  85. (test-assert (not (valid-base64-signature? test-key "something else" sig)))
  86. ;; Fake signature fails
  87. (test-assert (not (valid-base64-signature? test-key "monkey party"
  88. "f41c3516")))
  89. ;; Should equal a re-run of itself
  90. (test-equal sig (sign-data-base64 test-key "monkey party"))
  91. ;; Shouldn't equal something different
  92. (test-assert (not (equal? sig (sign-data-base64 test-key "cookie party")))))
  93. (test-end "mac")