summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorPiotr Szarmanski2022-10-24 10:50:11 +0200
committerPiotr Szarmanski2022-10-24 10:50:11 +0200
commitc4b44fb12108c0764d1fa4ef1c37f11a54379343 (patch)
treeb5bb4d586e7973537608b73a7def903667c33f42 /src
parent087b4ea280caa05e7213eb2b051782086c96efba (diff)
Add hash-backend and update README.
Diffstat (limited to 'src')
-rw-r--r--src/eris-decode.lisp29
-rw-r--r--src/eris.lisp1
-rw-r--r--src/file-backend.lisp4
-rw-r--r--src/hash-backend.lisp51
-rw-r--r--src/package.lisp10
5 files changed, 83 insertions, 12 deletions
diff --git a/src/eris-decode.lisp b/src/eris-decode.lisp
index ba20e9d..3d93312 100644
--- a/src/eris-decode.lisp
+++ b/src/eris-decode.lisp
@@ -218,19 +218,28 @@ hash-table is used, a (copy-seq) needs to be done on the return value of
gethash.
The keyword argument CACHE-CAPACITY indicates the amount of blocks stored in the
-cache."
+cache. It may be NIL to turn off caching entirely."
(declare (type read-capability read-capability)
(type function fetch-function)
- (type integer cache-capacity))
+ (type (or integer null) cache-capacity))
(with-slots (level block-size root-reference-pair) read-capability
- (let* ((get-block (cached-lambda (:cache-class 'lru-cache
- :capacity cache-capacity
- :table (make-hash-table :size (1+ cache-capacity) :test #'equalp))
- (reference key &optional nonce)
- (let* ((block (execute-fetch-function fetch-function reference)))
- (unless block (error 'missing-block :reference reference))
- (hash-check block reference)
- (decrypt-block block key nonce))))
+ (let* ((get-block (if cache-capacity ;; handle caching
+ (cached-lambda (:cache-class 'lru-cache
+ :capacity cache-capacity
+ :table (make-hash-table :size (1+ cache-capacity) :test #'equalp))
+ (reference key nonce)
+ (declare (type octet-vector reference key))
+ (let ((block (execute-fetch-function fetch-function reference)))
+ (declare (type octet-vector block))
+ (hash-check block reference)
+ (decrypt-block block key nonce)))
+ (lambda (reference key nonce)
+ (declare (type octet-vector reference key)
+ (optimize (debug 3)))
+ (let ((block (execute-fetch-function fetch-function reference)))
+ (declare (type octet-vector block))
+ (hash-check block reference)
+ (decrypt-block block key nonce)))))
(root (funcall get-block (reference root-reference-pair) (key root-reference-pair) (make-nonce level))))
;; "Implementations MUST verify the key appearing in the read capability
;; if level of encoded content is larger than 0."
diff --git a/src/eris.lisp b/src/eris.lisp
index 4693104..ed4a05e 100644
--- a/src/eris.lisp
+++ b/src/eris.lisp
@@ -81,6 +81,7 @@ object, using the bytes from the OCTETS vector from at START."
(make-instance 'reference-pair :key key :reference reference)))
(defun decrypt-block (input key &optional (nonce null-secret))
+ (declare (type octet-vector input key nonce))
(ironclad:decrypt-in-place
(ironclad:make-cipher :chacha :mode :stream :key key :initialization-vector nonce)
input)
diff --git a/src/file-backend.lisp b/src/file-backend.lisp
index 2bb8693..03e3ef7 100644
--- a/src/file-backend.lisp
+++ b/src/file-backend.lisp
@@ -43,7 +43,9 @@
(lambda (reference)
(let* ((base32 (bytes-to-base32-unpadded reference))
(file (merge-pathnames directory base32)))
- (alexandria:read-file-into-byte-vector file)))
+ (if (probe-file file)
+ (alexandria:read-file-into-byte-vector file)
+ (error 'missing-block :reference reference))))
output-function
(lambda (block reference)
(let* ((base32 (bytes-to-base32-unpadded reference))
diff --git a/src/hash-backend.lisp b/src/hash-backend.lisp
new file mode 100644
index 0000000..91dc673
--- /dev/null
+++ b/src/hash-backend.lisp
@@ -0,0 +1,51 @@
+;; This file is part of eris-cl.
+;; Copyright (C) 2022 Piotr SzarmaƄski
+
+;; eris-cl is free software: you can redistribute it and/or modify it under the
+;; terms of the GNU Lesser General Public License as published by the Free
+;; Software Foundation, either version 3 of the License, or (at your option) any
+;; later version.
+
+;; eris-cl 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 General Public License for more details.
+
+;; You should have received a copy of the GNU General Public License along with
+;; eris-cl. If not, see <https://www.gnu.org/licenses/>.
+
+(in-package :eris)
+
+(defclass hash-backend (encoding-backend decoding-backend)
+ ((table :type hash-table)))
+
+(defmethod shared-initialize :after ((hash-backend hash-backend) slot-names
+ &rest initargs
+ &key &allow-other-keys)
+
+ (declare (ignore initargs slot-names))
+ (let ((hash-table (make-hash-table :test #'equalp)))
+ (with-slots (table output-function fetch-function) hash-backend
+ (setf table hash-table
+ fetch-function (lambda (reference)
+ (declare (type octet-vector reference))
+ (copy-seq (gethash reference hash-table)))
+ output-function (lambda (block reference)
+ (declare (type octet-vector block reference))
+ (setf (gethash reference hash-table)
+ block))))))
+
+(defmethod fetch-read-capability (read-capability (backend hash-backend) &key &allow-other-keys)
+ (declare (type read-capability read-capability))
+ (with-slots (fetch-function) backend
+ (eris-decode read-capability fetch-function :cache-capacity nil)))
+
+(defmethod store-data (input (backend hash-backend) &key (secret null-secret) (block-size 1kib) &allow-other-keys)
+ (declare (type octet-vector secret))
+ (with-slots (output-function) backend
+ (eris-encode input
+ block-size
+ output-function
+ :hash-output nil
+ :secret secret)))
+
+
diff --git a/src/package.lisp b/src/package.lisp
index 873a453..0265f74 100644
--- a/src/package.lisp
+++ b/src/package.lisp
@@ -43,4 +43,12 @@
#:padding-error
#:version-mismatch
#:hash-mismatch
- #:invalid-internal-block))
+ #:invalid-internal-block
+ #:missing-block
+
+ #:store-data
+ #:fetch-read-capability
+ #:encoding-backend
+ #:decoding-backend
+ #:file-backend
+ #:hash-backend))