gnunet-svn
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[gnunet-scheme] 03/03: crypto: Define procedures for decoding Crockford


From: gnunet
Subject: [gnunet-scheme] 03/03: crypto: Define procedures for decoding Crockford Base32-encoded public keys.
Date: Mon, 15 Aug 2022 15:57:41 +0200

This is an automated email from the git hooks/post-receive script.

maxime-devos pushed a commit to branch master
in repository gnunet-scheme.

commit 11cda5c1282a01efd7ab5c8e0c4a70267083dfa4
Author: Maxime Devos <maximedevos@telenet.be>
AuthorDate: Mon Aug 15 15:12:06 2022 +0200

    crypto: Define procedures for decoding Crockford Base32-encoded public keys.
    
    * gnu/gnunet/crypto.scm: Update comment.
    (&invalid-public-key-encoding): New exception type.
    (raise-invalid-public-key-encoding,string->something-public-key)
    (string->eddsa-public-key,string->ecdsa-public-key): New procedures.
    * tests/crypto.scm ("string->eddsa-public-key test case")
    ("string->eddsa-public-key, overly short (invalid)")
    ("string->eddsa-public-key, bogus character (invalid)"): New tests.
    * doc/scheme-gnunet.tm (Miscellaneous): Document the new procedures.
---
 doc/scheme-gnunet.tm  |  9 ++++++++
 gnu/gnunet/crypto.scm | 63 ++++++++++++++++++++++++++++++++++++++++++++++-----
 tests/crypto.scm      | 21 ++++++++++++++++-
 3 files changed, 86 insertions(+), 7 deletions(-)

diff --git a/doc/scheme-gnunet.tm b/doc/scheme-gnunet.tm
index e0e0491..2359858 100644
--- a/doc/scheme-gnunet.tm
+++ b/doc/scheme-gnunet.tm
@@ -260,6 +260,15 @@
     <var|to> and return it.
   </explain>
 
+  It also has a procedure 
<scm|string-\<gtr\>eddsa-public-key><index|string-\<gtr\>eddsa-public-key>
+  and 
<scm|string-\<gtr\>ecdsa-public-key><index|string-\<gtr\>ecdsa-public-key>.
+  They respectively accept a Crockford Base32-encoded EdDSA and ECDSA public
+  key as a string and return the decoded key as a fresh bytevector. In case
+  the string has an incorrect length or characters, a
+  <scm|&invalid-public-key-encoding><index|&invalid-public-key-encoding>
+  condition is raised, which can be tested for with the predicate
+  <scm|invalid-public-key-encoding?><index|invalid-public-key-encoding?>.
+
   The module <scm|(gnu gnunet data-string)><index|(gnu gnunet data-string)>
   has two procedures <scm|data-\<gtr\>string><index|data-\<gtr\>string> and
   <scm|string-\<gtr\>data><index|string-\<gtr\>data>, for representing binary
diff --git a/gnu/gnunet/crypto.scm b/gnu/gnunet/crypto.scm
index 824516e..df6af3b 100644
--- a/gnu/gnunet/crypto.scm
+++ b/gnu/gnunet/crypto.scm
@@ -1,5 +1,5 @@
 ;; This file is part of GNUnet
-;; Copyright © 2021 GNUnet e.V.
+;; Copyright © 2021, 2022 GNUnet e.V.
 ;;
 ;; GNUnet is free software: you can redistribute it and/or modify it
 ;; under the terms of the GNU Affero General Public License as published
@@ -16,20 +16,36 @@
 ;;
 ;; SPDX-License-Identifier: AGPL-3.0-or-later
 
-;; Small wrapper around guile-gcrypt
+;; Small wrapper around guile-gcrypt and other related procedures.
 (define-library (gnu gnunet crypto)
-  (export hash/sha512 hash/sha512!)
+  (export hash/sha512 hash/sha512!
+         &invalid-public-key-encoding
+         invalid-public-key-encoding?
+         string->eddsa-public-key
+         string->ecdsa-public-key)
   (import (only (gcrypt hash)
                hash-algorithm open-hash-port sha512)
          (gnu gnunet utils bv-slice)
+         (only (gnu gnunet crypto struct)
+               /eddsa-public-key /ecdsa-public-key)
+         (only (gnu gnunet data-string)
+               string->data)
+         (only (gnu gnunet netstruct syntactic)
+               sizeof)
+         (only (gnu gnunet utils hat-let)
+               let^)
          (only (srfi srfi-8)
                receive)
          (only (guile)
-               %make-void-port close-port)
+               %make-void-port close-port ceiling-quotient)
          (only (ice-9 binary-ports)
                put-bytevector)
          (only (rnrs base)
-               begin lambda define))
+               begin lambda define quote * not = string-length or)
+         (only (rnrs conditions)
+               define-condition-type &violation)
+         (only (rnrs exceptions)
+               raise))
   (begin
     ;; TODO: Extend bytevector-hash with offset + length.
     (define (hash-slice/bytevector algorithm slice)
@@ -66,4 +82,39 @@ fresh readable bytevector slice with the hash."
 
     ;; (hash/sha512! data-to-hash-slice destination-slice) --> (nothing)
     (define hash/sha512! (hasher! (hash-algorithm sha512)))
-    (define hash/sha512 (hasher (hash-algorithm sha512)))))
+    (define hash/sha512 (hasher (hash-algorithm sha512)))
+
+    
+    ;; TODO: &who, how to decide between supertypes, maybe some
+    ;; more specific subtypes and some fields
+    (define-condition-type &invalid-public-key-encoding &violation
+      make-invalid-public-key-encoding invalid-public-key-encoding?)
+
+    (define (raise-invalid-public-key-encoding)
+      (raise (make-invalid-public-key-encoding)))
+
+    ;; See next two procedures.
+    (define (string->something-public-key key-length/bytes string)
+      (let^ ((! key-length/bits (* 8 key-length/bytes))
+            (! key-length/characters (ceiling-quotient key-length/bits 5))
+            (? (not (= key-length/characters (string-length string)))
+               (raise-invalid-public-key-encoding)))
+           (or (string->data string key-length/bytes)
+               (raise-invalid-public-key-encoding))))
+
+    ;; TODO: find some test cases
+    (define (string->eddsa-public-key string)
+      "Decode the Crockford Base32-encoded EdDSA public key @var{string} from 
a string
+into a fresh bytevector.  In case of a decoding error (unexpected characters
+or wrong length), an @code{&invalid-public-key-encoding} is raised.
+
+This corresponds to @code{GNUNET_CRYPTO_ecdsa_public_key_from_string} in the C 
API."
+      (string->something-public-key (sizeof /eddsa-public-key '()) string))
+
+    (define (string->ecdsa-public-key string)
+      "Decode the Crockford Base32-encoded ECDSA public key @var{string} from 
a string
+into a fresh bytevector.  In case of a decoding error (unexpected characters
+or wrong length), an @code{&invalid-public-key-encoding} is raised.
+
+This corresponds to @code{GNUNET_CRYPTO_ecdsa_public_key_from_string} in the C 
API."
+      (string->something-public-key (sizeof /ecdsa-public-key '()) string))))
diff --git a/tests/crypto.scm b/tests/crypto.scm
index bcc6723..26d6b5b 100644
--- a/tests/crypto.scm
+++ b/tests/crypto.scm
@@ -1,5 +1,5 @@
 ;; This file is part of scheme-GNUnet.
-;; Copyright (C) 2021 GNUnet e.V.
+;; Copyright © 2021, 2022 GNUnet e.V.
 ;;
 ;; scheme-GNUnet is free software: you can redistribute it and/or modify it
 ;; under the terms of the GNU Affero General Public License as published
@@ -26,6 +26,7 @@
        (only (rnrs bytevectors)
              make-bytevector string->utf8)
        (srfi srfi-64)
+       (rnrs exceptions)
        (only (srfi srfi-43)
              vector-every)
        (only (ice-9 match)
@@ -87,3 +88,21 @@
 (test-equal "size of /hashcode:512"
   512
   (* 8 (sizeof /hashcode:512 '())))
+
+;; TODO: the #vu8(...) was generated by running string->eddsa-public-key,
+;; it is not independently tested (TODO: test with examples/web.scm and
+;; local GNUnet peer).
+(test-equal "string->eddsa-public-key test case"
+           #vu8(57 243 205 186 142 192 173 103 132 59 211 78 51 218 234 21 213 
205 245 73 49 220 179 121 235 142 11 241 72 234 145 220)
+           (string->eddsa-public-key 
"77SWVEMER2PPF11VTD737PQA2QAWVXA967EB6YFBHR5Z2J7AJ7E0"))
+
+(test-assert "string->eddsa-public-key, overly short (invalid)"
+            (guard (c ((invalid-public-key-encoding? c) #true))
+                   (string->eddsa-public-key "")
+                   #false))
+
+(test-expect-fail 1) ; TODO
+(test-assert "string->eddsa-public-key, bogus character (invalid)"
+            (guard (c ((invalid-public-key-encoding? c) #true))
+                   (string->eddsa-public-key 
"@7SWVEMER2PPF11VTD737PQA2QAWVXA967EB6YFBHR5Z2J7AJ7E0")
+                   #false))

-- 
To stop receiving notification emails like this one, please contact
gnunet@gnunet.org.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]