Debian Codegraph @

guile/examples/use-private-key.scm

raw
1 ;;; GnuTLS --- Guile bindings for GnuTLS.
2 ;;; Copyright (C) 2023-2025 Free Software Foundation, Inc.
3 ;;;
4 ;;; This file is part of Guile-GnuTLS.
5 ;;;
6 ;;; This library is free software; you can redistribute it and/or
7 ;;; modify it under the terms of the GNU Lesser General Public
8 ;;; License as published by the Free Software Foundation; either
9 ;;; version 2.1 of the License, or (at your option) any later version.
10 ;;;
11 ;;; This library 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 GNU
14 ;;; Lesser General Public License for more details.
15 ;;;
16 ;;; You should have received a copy of the GNU Lesser General Public
17 ;;; License along with this library; if not, see <https://www.gnu.org/licenses/>.
18
19 (use-modules (ice-9 rdelim)
20 (ice-9 binary-ports)
21 (ice-9 match)
22 (rnrs bytevectors)
23 (gnutls))
24
25 (define (read-curve)
26 (format #t "curve:\n")
27 (string->ecc-curve (read-line)))
28
29 (define (read-parameter name)
30 (format #t "~a:\n" name)
31 (base64-decode (read-line)))
32
33 (define (read-parameters)
34 (let* ((curve (read-curve))
35 (x (read-parameter 'x))
36 (y (read-parameter 'y))
37 (k (read-parameter 'k)))
38 (values curve x y k)))
39
40 (define private-key
41 (receive (curve x y k) (read-parameters)
42 (let ((key (import-raw-ecc-private-key curve x y k)))
43 key)))
44
45 (define message (string->utf8 "Hello, world!"))
46
47 (define signature
48 (private-key-sign-data private-key
49 sign-algorithm/ecdsa-secp521r1-sha512
50 message
51 '()))
52
53 (define public-key
54 (let ((key (private-key->public-key private-key
55 (list key-usage/digital-signature))))
56 key))
57
58 (public-key-verify-data public-key sign-algorithm/ecdsa-secp521r1-sha512
59 message signature)
60
61 (format #t "I could sign a message with that key.\n")