Coverage report: /home/ellis/comp/ext/ironclad/src/public-key/public-key.lisp

KindCoveredAll%
expression1687 18.4
branch00nil
Key
Not instrumented
Conditionalized out
Executed
Not executed
 
Both branches taken
One branch taken
Neither branch taken
1
 ;;;; public-key.lisp -- implementation of common public key components
2
 (in-package :crypto)
3
 
4
 (defun list-all-key-pair-kinds ()
5
   (copy-list '(:curve25519 :curve448 :dsa :ed25519 :ed448 :elgamal
6
                :rsa :secp256k1 :secp256r1 :secp384r1 :secp521r1)))
7
 
8
 ;;; class definitions
9
 (defclass discrete-logarithm-group ()
10
   ((p :initarg :p :reader group-pval)
11
    (q :initarg :q :reader group-qval)
12
    (g :initarg :g :reader group-gval)))
13
 
14
 
15
 ;;; Special variable to force the signature nonce during tests instead of
16
 ;;; generating a random one.
17
 (defparameter *signature-nonce-for-test* nil)
18
 
19
 ;;; converting from integers to octet vectors
20
 (defun octets-to-integer (octet-vec &key (start 0) end (big-endian t) n-bits)
21
   (declare (type (simple-array (unsigned-byte 8) (*)) octet-vec)
22
            (optimize (speed 3) (space 0) (safety 1) (debug 0)))
23
   (let ((end (or end (length octet-vec))))
24
     (multiple-value-bind (n-bits n-bytes)
25
         (let ((size (- end start)))
26
           (if n-bits
27
               (values n-bits (min (ceiling n-bits 8) size))
28
               (values (* 8 size) size)))
29
       (let ((sum (if big-endian
30
                      (loop with sum = 0
31
                            for i from (- end n-bytes) below end
32
                            do (setf sum (+ (ash sum 8) (aref octet-vec i)))
33
                            finally (return sum))
34
                      (loop for i from start below (+ start n-bytes)
35
                            for j from 0 by 8
36
                            sum (ash (aref octet-vec i) j)))))
37
         (ldb (byte n-bits 0) sum)))))
38
 
39
 (defun integer-to-octets (bignum &key n-bits (big-endian t))
40
   (declare (optimize (speed 3) (space 0) (safety 1) (debug 0)))
41
   (let* ((n-bits (or n-bits (integer-length bignum)))
42
          (bignum (ldb (byte n-bits 0) bignum))
43
          (n-bytes (ceiling n-bits 8))
44
          (octet-vec (make-array n-bytes :element-type '(unsigned-byte 8))))
45
     (declare (type (simple-array (unsigned-byte 8) (*)) octet-vec))
46
     (if big-endian
47
         (loop for i from (1- n-bytes) downto 0
48
               for index from 0
49
               do (setf (aref octet-vec index) (ldb (byte 8 (* i 8)) bignum))
50
               finally (return octet-vec))
51
         (loop for i from 0 below n-bytes
52
               for byte from 0 by 8
53
               do (setf (aref octet-vec i) (ldb (byte 8 byte) bignum))
54
               finally (return octet-vec)))))
55
 
56
 (defun maybe-integerize (thing)
57
   (etypecase thing
58
     (integer thing)
59
     ((simple-array (unsigned-byte 8) (*)) (octets-to-integer thing))))