Debian Codegraph @

guile/tests/hmac.scm

raw
1 ;;; GnuTLS --- Guile bindings for GnuTLS
2 ;;; Copyright (C) 2011-2025 Free Software Foundation, Inc.
3 ;;;
4 ;;; This file is part of Guile-GnuTLS.
5 ;;;
6 ;;; This program 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 ;;;
11 ;;; This program is distributed in the hope that it will be useful,
12 ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
13 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 ;;; GNU General Public License for more details.
15 ;;;
16 ;;; You should have received a copy of the GNU General Public License
17 ;;; along with this program. If not, see <https://www.gnu.org/licenses/>.
18
19 ;; Check the HMAC algorithms.
20
21 (use-modules (gnutls)
22 (gnutls build tests)
23 (rnrs bytevectors)
24 (ice-9 match)
25 (srfi srfi-1)
26 (srfi srfi-26))
27
28 (define (hex->bytevector hex)
29 (let* ((digits (string->list hex))
30 (digits-as-numbers
31 (map
32 (cute string-index "0123456789abcdef" <>)
33 digits))
34 (digit-pairs
35 (let get ((digits digits-as-numbers))
36 (match digits
37 (() '())
38 ((big little other-digits ...)
39 `(,(+ (* big 16) little)
40 ,@(get other-digits)))))))
41 (u8-list->bytevector digit-pairs)))
42
43 (run-test
44 (lambda ()
45 ;; This checks that make-hmac and hmac-direct work, and that hmac-copy does
46 ;; not alter the source state.
47 (let ((secret (string->utf8 "secret!"))
48 (payload (string->utf8 "Example data to hash."))
49 (sha256-output
50 (hex->bytevector
51 "9d40ad76c2b12c5057e9f31bb35a7a382ade69d870e94510eb93dc35b14d262e")))
52 (unless (equal? sha256-output (hmac-direct mac/sha256 secret payload))
53 (error "The sha256 direct method failed."))
54 (let ((state (make-hmac mac/sha256 secret)))
55 (hmac! state payload)
56 (when (defined? 'hmac-copy)
57 (let ((copy (hmac-copy state)))
58 (hmac! copy (string->utf8 " More data."))
59 (unless
60 (equal?
61 (hmac-output copy)
62 (hex->bytevector
63 "288a7d4b6fddac45a99fbefbfed287ca1c58c6ab011a579f47c6f3313422b39b"))
64 (error "The hmac state copy failed."))))
65 (unless (equal? (hmac-output state) sha256-output)
66 (error "The indirect method failed."))))
67 (when (defined? 'hmac-key-size)
68 (unless (equal? (hmac-key-size mac/sha256) 32)
69 (error "hmac-key-size does not work")))
70 ;; This is the examples in the manual.
71 (let ((manual-example-1
72 (lambda ()
73 (load-from-path "hmac-example-1.scm")))
74 (manual-example-2
75 (lambda ()
76 (load-from-path "hmac-example-2.scm")))
77 (manual-example-3
78 (lambda ()
79 (load-from-path "hmac-example-3.scm")))
80 ;; This is how we run the examples for the purpose of testing the code.
81 (manual-example-1-environment
82 (lambda (f)
83 (let* ((input (call-with-output-string
84 (lambda (port)
85 ;; What is the secret?
86 (format port "secret!\n")
87 ;; What message do you want to hash?
88 (format port "Example data to hash.\n"))))
89 (expected-output
90 (call-with-output-string
91 (lambda (port)
92 (format port "What is the secret?\n")
93 ;; secret is input
94 (format port "What message do you want to hash?\n")
95 ;; message is input
96 (format port "The digest is: ~s\n"
97 (hex->bytevector
98 "9d40ad76c2b12c5057e9f31bb35a7a382ade69d870e94510eb93dc35b14d262e")))))
99 (true-output
100 (call-with-output-string
101 (lambda (output-port)
102 (with-input-from-port
103 (open-input-string input)
104 (lambda ()
105 (with-output-to-port output-port
106 f)))))))
107 (unless (equal? expected-output true-output)
108 (error "Manual example 1 failed.")))))
109 (manual-example-2-environment
110 (lambda (f)
111 (call-with-output-file "manual-example-2-data"
112 (lambda (port)
113 (format port "Example data to hash."))
114 #:encoding "UTF-8")
115 (let* ((input (call-with-output-string
116 (lambda (port)
117 ;; What is the secret?
118 (format port "secret!\n")
119 ;; Which file do you want to hash?
120 (format port "./manual-example-2-data\n"))))
121 (expected-output
122 (call-with-output-string
123 (lambda (port)
124 (format port "What is the secret?\n")
125 ;; secret is input
126 (format port "Which file do you want to hash?\n")
127 ;; message is input
128 (format port "The digest is: ~s\n"
129 (hex->bytevector
130 "9d40ad76c2b12c5057e9f31bb35a7a382ade69d870e94510eb93dc35b14d262e")))))
131 (true-output
132 (call-with-output-string
133 (lambda (output-port)
134 (with-input-from-port
135 (open-input-string input)
136 (lambda ()
137 (with-output-to-port output-port
138 f)))))))
139 (delete-file "manual-example-2-data")
140 (unless (equal? expected-output true-output)
141 (error "Manual example 2 failed."))))))
142 (manual-example-1-environment manual-example-1)
143 (manual-example-2-environment manual-example-2)
144 (manual-example-3))))