summaryrefslogtreecommitdiff
path: root/src/eris.lisp
diff options
context:
space:
mode:
Diffstat (limited to 'src/eris.lisp')
-rw-r--r--src/eris.lisp64
1 files changed, 30 insertions, 34 deletions
diff --git a/src/eris.lisp b/src/eris.lisp
index 0509ecb..a585e46 100644
--- a/src/eris.lisp
+++ b/src/eris.lisp
@@ -21,13 +21,11 @@
(defconstant 32kib 32768)
(defconstant 1kib 1024)
-(eval-when
- (:execute :load-toplevel :compile-toplevel)
- (defclass reference-pair ()
- ((reference :initarg :reference :accessor reference :type (simple-array (unsigned-byte 8) 32))
- (key :initarg :key :accessor key :type (simple-array (unsigned-byte 8) 32)))))
+(defclass reference-pair ()
+ ((reference :initarg :reference :accessor reference :type (octet-vector 32))
+ (key :initarg :key :accessor key :type (octet-vector 32))))
-(define-constant null-secret (make-array 32 :element-type '(unsigned-byte 8) :initial-element 0)
+(define-constant null-secret (make-array 32 :element-type 'octet :initial-element 0)
:test #'equalp
:documentation
"32-byte null vector.")
@@ -48,20 +46,20 @@ object, using the bytes from the OCTETS vector from at START."
(defun compute-reference (block)
- (declare (type (simple-array (unsigned-byte 8)) block))
- (let ((reference (make-array 32 :element-type '(unsigned-byte 8))))
+ (declare (type octet-vector block))
+ (let ((reference (make-array 32 :element-type 'octet)))
(ironclad:digest-sequence :blake2/256 block :digest reference)
reference))
(defun make-nonce (level)
- (let ((nonce (make-array 12 :element-type '(unsigned-byte 8) :initial-element 0)))
+ (let ((nonce (make-array 12 :element-type 'octet :initial-element 0)))
(setf (aref nonce 0) level)
nonce))
(defun encrypt-block (input secret reference)
- (declare (type (simple-array (unsigned-byte 8)) input secret reference))
+ (declare (type octet-vector input secret reference))
(let ((mac (ironclad:make-mac :blake2-mac secret :digest-length 32))
- (key (make-array 32 :element-type '(unsigned-byte 8))))
+ (key (make-array 32 :element-type 'octet)))
(ironclad:update-mac mac input)
(ironclad:produce-mac mac :digest key)
;; NOT the IETF chacha. Need to fix this by patching ironclad. Maybe.
@@ -72,8 +70,8 @@ object, using the bytes from the OCTETS vector from at START."
(make-instance 'reference-pair :key key :reference reference)))
(defun encrypt-internal-block (input reference nonce)
- (declare (type (simple-array (unsigned-byte 8)) input secret reference))
- (let ((key (make-array 32 :element-type '(unsigned-byte 8))))
+ (declare (type octet-vector input secret reference))
+ (let ((key (make-array 32 :element-type 'octet)))
(ironclad:digest-sequence :blake2/256 input :digest key)
;; NOT the IETF chacha. Need to fix this by patching ironclad. Maybe.
(ironclad:encrypt-in-place (ironclad:make-cipher :chacha :mode :stream :key key
@@ -94,33 +92,27 @@ object, using the bytes from the OCTETS vector from at START."
:accessor block-size
:type block-size
:documentation "A value of either 1024 or 1kb blocks.")
- (level :initarg :level :accessor level :type (unsigned-byte 8))
+ (level :initarg :level :accessor level :type octet)
(root-reference-pair :initarg :reference-pair :accessor reference-pair))
(:documentation "Class representing the concept of an ERIS read capability."))
-(declaim
- (ftype (function (read-capability) (values (simple-array (unsigned-byte 8) (66)) &optional)) read-capability-to-octets)
- (ftype (function ((simple-array (unsigned-byte 8) (66))) read-capability) octets-to-read-capability)
- (ftype (function (read-capability) string) read-capability-to-urn)
- (ftype (function (string) read-capability) urn-to-read-capability)
- (ftype (function ((simple-array (unsigned-byte 8) (32))) string) reference-to-block-urn)
- (ftype (function (string) (values (simple-array (unsigned-byte 8) (32)) &optional)) block-urn-to-reference))
-
+(-> read-capability-to-octets (read-capability) (octet-vector 66))
(defun read-capability-to-octets (read-capability)
"Convert a read-capability object to its standard binary representation. Returns
a (simple-array (unsigned-byte 8)) object."
(declare (type read-capability read-capability))
- (let ((cap (make-array 66 :element-type '(unsigned-byte 8))))
+ (let ((cap (make-array 66 :element-type 'octet)))
(case (block-size read-capability) ;; This depends on the version of the standard
(1024 (setf (aref cap 0) #x0a))
(32768 (setf (aref cap 0) #x0f)))
(setf (aref cap 1) (level read-capability))
(reference-pair-to-octets (reference-pair read-capability) cap 2)))
+(-> octets-to-read-capability ((octet-vector 66)) read-capability)
(defun octets-to-read-capability (octets)
"Convert the standard binary representation for ERIS read capabilities into a
read-capability object. Returns the read-capability."
- (declare (type (simple-array (unsigned-byte 8) (66)) octets))
+ (declare (type (octet-vector 66) octets))
(let ((capability (make-instance 'read-capability)))
(setf (block-size capability)
(case (aref octets 0)
@@ -133,6 +125,7 @@ read-capability object. Returns the read-capability."
(octets-to-reference-pair octets 2))
capability))
+(-> read-capability-to-urn (read-capability) string)
(defun read-capability-to-urn (capability)
"Convert a read-capability object into a URN string."
(declare (type read-capability capability))
@@ -140,23 +133,26 @@ read-capability object. Returns the read-capability."
"urn:eris:"
(bytes-to-base32-unpadded (read-capability-to-octets capability))))
+(-> urn-to-read-capability (string) read-capability)
(defun urn-to-read-capability (urn)
"Convert a urn:eris URN string into a read-capability object."
(declare (type string urn))
(octets-to-read-capability (base32-to-bytes-unpadded (subseq urn (1+ (position #\: urn :from-end t))))))
+(-> reference-to-block-urn ((octet-vector 32)) string)
(defun reference-to-block-urn (reference)
"Convert a 32-byte block reference into a URN string."
- (declare (type (simple-array (unsigned-byte 8) (32)) reference))
+ (declare (type (octet-vector 32) reference))
(concatenate 'string "urn:blake2b:" (bytes-to-base32-unpadded reference)))
+(-> block-urn-to-reference (string) (octet-vector 32))
(defun block-urn-to-reference (urn)
"Convert a urn:blake2b URN string into a 32-byte block reference vector."
(declare (type string urn))
(base32-to-bytes-unpadded (subseq urn (1+ (position #\: urn :from-end t)))))
(defun pad (input block-size)
- (declare (type (simple-array (unsigned-byte 8)) input)
+ (declare (type octet-vector input)
(type integer block-size))
(let* ((pad-size (- block-size (mod (length input) block-size)))
(padded-input (adjust-array input (+ pad-size (length input)) :initial-element 0)))
@@ -205,16 +201,16 @@ SECRET keyword argument.
The HASH-OUTPUT keyword argument controls whether a hash-table is used to
guarantee that a reference is only output once."))
-(defmethod eris-encode ((input simple-array) block-size output-function &key (secret null-secret) (hash-output t))
+(defmethod eris-encode ((input vector) block-size output-function &key (secret null-secret) (hash-output t))
(declare (type block-size block-size)
(type function output-function)
- (type (simple-array (unsigned-byte 8) (32)) secret))
+ (type (octet-vector 32) secret))
(setf input (pad input block-size))
(let ((reference-vector (make-array 16 :adjustable t :fill-pointer 0))
(*output-hashmap* (if hash-output (make-hash-table :test #'equalp) nil)))
- (loop for block = (make-array block-size :element-type '(unsigned-byte 8) :initial-element 0)
+ (loop for block = (make-array block-size :element-type 'octet :initial-element 0)
for i = 0 then (incf i)
until (= (length input) (* i block-size))
do (progn
@@ -225,10 +221,10 @@ guarantee that a reference is only output once."))
(defmethod eris-encode ((input stream) block-size output-function &key (secret null-secret) hash-output)
(declare (type block-size block-size)
(type function output-function)
- (type (simple-array (unsigned-byte 8) (32)) secret))
+ (type (octet-vector 32) secret))
(let ((reference-vector (make-array 16 :adjustable t :fill-pointer 0))
(*output-hashmap* (if hash-output (make-hash-table :test #'equalp) nil)))
- (loop for block = (make-array block-size :element-type '(unsigned-byte 8) :initial-element 0)
+ (loop for block = (make-array block-size :element-type 'octet :initial-element 0)
for bytes-read = (read-sequence block input)
for i = 0 then (incf i)
if (< bytes-read block-size)
@@ -240,7 +236,7 @@ guarantee that a reference is only output once."))
(defun eris-create-tree (reference-vector block-size output-function &key hash-output)
(declare (type block-size block-size)
(type function output-function)
- (type (simple-array (unsigned-byte 8) (32)) secret))
+ (type (octet-vector 32) secret))
(loop with block-keys = (/ block-size 64)
with level = 0
with reference-vector-l = (make-array 16 :adjustable t :fill-pointer 0)
@@ -253,12 +249,12 @@ guarantee that a reference is only output once."))
do (progn
(incf level)
;; loop across the key-reference vector and build the tree
- (loop with block = (make-array block-size :element-type '(unsigned-byte 8) :initial-element 0)
+ (loop with block = (make-array block-size :element-type 'octet :initial-element 0)
for rk across reference-vector
with i = 0
when (eql i block-keys)
do (output-internal-block reference-vector-l nonce
- (setf block (make-array block-size :element-type '(unsigned-byte 8) :initial-element 0)
+ (setf block (make-array block-size :element-type 'octet :initial-element 0)
i 0))
do (progn (reference-pair-to-octets rk block (* 64 i))
(incf i))