| 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 aead implementation. |
| 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 confidential-data |
| 29 |
"Confidential data.") |
| 30 |
|
| 31 |
(run-test |
| 32 |
(lambda () |
| 33 |
;; This is the example in the manual. |
| 34 |
(let ((encrypt-example |
| 35 |
(lambda () |
| 36 |
(load-from-path "aead-encrypt-example.scm"))) |
| 37 |
(decrypt-example |
| 38 |
(lambda () |
| 39 |
(load-from-path "aead-decrypt-example.scm")))) |
| 40 |
(call-with-output-file "aead-manual-example-data" |
| 41 |
(lambda (port) |
| 42 |
(format port "~a" confidential-data)) |
| 43 |
#:encoding "UTF-8") |
| 44 |
(let ((encrypt-input |
| 45 |
(call-with-output-string |
| 46 |
(lambda (port) |
| 47 |
;; What is the secret? |
| 48 |
(format port "the secret key is 32 bytes long.\n") |
| 49 |
;; Which file do you want to encrypt? |
| 50 |
(format port "./aead-manual-example-data")))) |
| 51 |
(expected-encrypt-output |
| 52 |
(call-with-output-string |
| 53 |
(lambda (port) |
| 54 |
(format port "What is the secret?\n") |
| 55 |
;; the secret key is 32 bytes long. |
| 56 |
(format port "Which file do you want to encrypt?\n") |
| 57 |
;; ./aead-manual-example-data |
| 58 |
)))) |
| 59 |
(let ((true-encrypt-output |
| 60 |
(call-with-output-string |
| 61 |
(lambda (output-port) |
| 62 |
(with-input-from-port (open-input-string encrypt-input) |
| 63 |
(lambda () |
| 64 |
(with-output-to-port output-port |
| 65 |
encrypt-example))))))) |
| 66 |
(unless (equal? expected-encrypt-output true-encrypt-output) |
| 67 |
(error "Encryption failed.")))) |
| 68 |
(delete-file "./aead-manual-example-data") |
| 69 |
(let ((decrypt-input |
| 70 |
(call-with-output-string |
| 71 |
(lambda (port) |
| 72 |
;; What is the secret? |
| 73 |
(format port "the secret key is 32 bytes long.\n") |
| 74 |
;; Which file do you want to decrypt? |
| 75 |
(format port "./aead-manual-example-data.encrypted")))) |
| 76 |
(expected-decrypt-output |
| 77 |
(call-with-output-string |
| 78 |
(lambda (port) |
| 79 |
(format port "What is the secret?\n") |
| 80 |
;; the secret key is 32 bytes long. |
| 81 |
(format port "Which file do you want to decrypt?\n") |
| 82 |
;; ./aead-manual-example-data.encrypted |
| 83 |
)))) |
| 84 |
(let ((true-decrypt-output |
| 85 |
(call-with-output-string |
| 86 |
(lambda (output-port) |
| 87 |
(with-input-from-port (open-input-string decrypt-input) |
| 88 |
(lambda () |
| 89 |
(with-output-to-port output-port |
| 90 |
decrypt-example))))))) |
| 91 |
(unless (equal? expected-decrypt-output true-decrypt-output) |
| 92 |
(error "Decryption failed.")))) |
| 93 |
(delete-file "./aead-manual-example-data.encrypted") |
| 94 |
(call-with-input-file "./aead-manual-example-data.encrypted.decrypted" |
| 95 |
(lambda (decrypted-file) |
| 96 |
(let ((content (read-line decrypted-file))) |
| 97 |
(unless (equal? content confidential-data) |
| 98 |
(error "Inconsistent encryption and decryption.")))) |
| 99 |
#:encoding "UTF-8") |
| 100 |
(delete-file "./aead-manual-example-data.encrypted.decrypted")))) |