gnunet-svn
[Top][All Lists]
Advanced

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

[gnunet-scheme] 32/49: crypto: Implement a wrapper for hashing with byte


From: gnunet
Subject: [gnunet-scheme] 32/49: crypto: Implement a wrapper for hashing with bytevector slices.
Date: Sat, 25 Dec 2021 23:00:09 +0100

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 56eaffaf6a93ae58d9dfa63530d77d4294bbc3b5
Author: Maxime Devos <maximedevos@telenet.be>
AuthorDate: Sat Oct 2 19:20:39 2021 +0200

    crypto: Implement a wrapper for hashing with bytevector slices.
    
    * Makefile.am (modules): Add the new module.  Sort
      gnu/gnunet/crypto/struct.scm
    * tests/crypto.scm: New tests.
    * Makefile.am (SCM_TESTS): Add the test.
    * doc/scheme-gnunet.tm (Cryptography): New section, documenting
      hash/sha512!.
---
 Makefile.am          |  5 ++-
 doc/scheme-gnunet.tm | 13 ++++++++
 tests/crypto.scm     | 89 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 106 insertions(+), 1 deletion(-)

diff --git a/Makefile.am b/Makefile.am
index 9b4b051..adf964e 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -65,6 +65,9 @@ modules = \
   gnu/gnunet/config/db.scm \
   gnu/gnunet/config/fs.scm \
   \
+  gnu/gnunet/crypto.scm \
+  gnu/gnunet/crypto/struct.scm \
+  \
   gnu/gnunet/dht/client.scm \
   gnu/gnunet/dht/struct.scm \
   \
@@ -73,7 +76,6 @@ modules = \
   gnu/gnunet/icmp/struct.scm \
   \
   gnu/gnunet/util/struct.scm \
-  gnu/gnunet/crypto/struct.scm \
   gnu/gnunet/hashcode/struct.scm \
   \
   gnu/gnunet/nse/client.scm \
@@ -175,6 +177,7 @@ SCM_TESTS = \
   tests/config-expand.scm \
   tests/config-db.scm \
   tests/config-fs.scm \
+  tests/crypto.scm \
   tests/form.scm \
   tests/netstruct.scm \
   tests/time.scm \
diff --git a/doc/scheme-gnunet.tm b/doc/scheme-gnunet.tm
index 33f2e96..25f4216 100644
--- a/doc/scheme-gnunet.tm
+++ b/doc/scheme-gnunet.tm
@@ -1049,6 +1049,19 @@
   <scm|estimate:standard-deviation> can be used to put probablistic error
   bounds on the number of peers on the network. <todo|example>
 
+  <section|Cryptography>
+
+  The module <scm|(gnu gnunet crypto)> has a few small wrappers around
+  procedures from Guile-Gcrypt for performing cryptography on bytevector
+  slices.
+
+  <\explain>
+    <scm|(hash/sha512! <var|slice> <var|to>)>
+  </explain|Compute the SHA-512 hash of <var|slice>, a readable bytevector
+  slice of arbitrary length, and write it to <var|to>, a writable bytevector
+  slice of length 512 bits / 64 bytes.<space|1em>The result is unspecified if
+  <var|slice> and <var|to> overlap.>
+
   <chapter|Implementation details>
 
   TODO<appendix|GNU Free Documentation License>
diff --git a/tests/crypto.scm b/tests/crypto.scm
new file mode 100644
index 0000000..1920bf0
--- /dev/null
+++ b/tests/crypto.scm
@@ -0,0 +1,89 @@
+;; This file is part of scheme-GNUnet.
+;; Copyright (C) 2021 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
+;; by the Free Software Foundation, either version 3 of the License,
+;; or (at your option) any later version.
+;;
+;; scheme-GNUnet is distributed in the hope that it will be useful, but
+;; WITHOUT ANY WARRANTY; without even the implied warranty of
+;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+;; Affero General Public License for more details.
+;;
+;; You should have received a copy of the GNU Affero General Public License
+;; along with this program.  If not, see <http://www.gnu.org/licenses/>.
+;;
+;; SPDX-License-Identifier: AGPL3.0-or-later
+
+(import (gnu gnunet utils bv-slice)
+       (gnu gnunet crypto)
+       (gnu gnunet hashcode struct)
+       (only (gnu gnunet netstruct syntactic)
+             sizeof)
+       (only (gcrypt base16)
+             base16-string->bytevector)
+       (only (rnrs bytevectors)
+             make-bytevector string->utf8)
+       (srfi srfi-64)
+       (only (srfi srfi-43)
+             vector-every)
+       (only (ice-9 match)
+             match))
+
+;; Two test vectors from
+;; 
https://www.cosic.esat.kuleuven.be/nessie/testvectors/hash/sha/Sha-2-512.unverified.test-vectors
+(define test-vectors/sha512
+  #(#(""
+      "CF83E1357EEFB8BDF1542850D66D8007"
+      "D620E4050B5715DC83F4A921D36CE9CE"
+      "47D0D13C5D85F2B0FF8318D2877EEC2F"
+      "63B931BD47417A81A538327AF927DA3E")
+    #("abc"
+      "DDAF35A193617ABACC417349AE204131"
+      "12E6FA4E89A97EA20A9EEEE64B55D39A"
+      "2192992A274FC1A836BA3C23A3FEEBBD"
+      "454D4423643CE80E2A9AC94FA54CA49F")))
+
+(define (test-vector bits hash-slice! test-vector)
+  (match test-vector
+    (#(string hash-part/0 hash-part/1 hash-part/2 hash-part/3)
+     (define hash/base16 (string-append hash-part/0 hash-part/1 hash-part/2
+                                       hash-part/3))
+     (define hash/expected
+       (base16-string->bytevector (string-downcase hash/base16)))
+     ;; #xde: bogus filler, should be overwritten
+     (define hash/received (make-bytevector (/ bits 8) #xde))
+     (hash-slice! (slice/read-only
+                  (bv-slice/read-write
+                   (string->utf8 (string-append "don't" string "useme")))
+                  ;; The string length is also the bytevector
+                  ;; length, because the strings are restricted to ASCII.
+                  (string-length "don't")
+                  (string-length string))
+                 (slice/write-only
+                  (bv-slice/read-write hash/received)))
+     (when (not (equal? hash/expected hash/received))
+       (pk 'oops hash/expected hash/received))
+     (pk 'ok)
+     (equal? hash/expected hash/received))))
+
+(define (test-vectors bits hash-slice! vectors)
+  (vector-every (lambda (vector)
+                 (test-vector bits hash-slice! vector))
+               vectors))
+
+(test-assert "hash/sha512!, test vectors"
+  (test-vectors 512 hash/sha512! test-vectors/sha512))
+
+(test-error "hash/sha512!, requires readability"
+  (hash/sha512! (slice/write-only (make-slice/read-write 400))
+               (make-slice/read-write (/ 512 8))))
+
+(test-error "hash/sha512!, requires writability"
+  (hash/sha512! (make-slice/read-write 400)
+               (slice/read-only (make-slice/read-write (/ 512 8)))))
+
+(test-equal "size of /hashcode:512"
+  512
+  (* 8 (sizeof /hashcode:512 '())))

-- 
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]