Coverage report: /home/ellis/comp/ext/ironclad/src/ciphers/xchacha.lisp

KindCoveredAll%
expression0116 0.0
branch02 0.0
Key
Not instrumented
Conditionalized out
Executed
Not executed
 
Both branches taken
One branch taken
Neither branch taken
1
 ;;;; xchacha.lisp -- implementation of the XChacha stream cipher
2
 (in-package :crypto)
3
 
4
 (defclass xchacha (chacha)
5
   ())
6
 
7
 (defclass xchacha/12 (xchacha)
8
   ()
9
   (:default-initargs :core-function #'chacha/12-core))
10
 
11
 (defclass xchacha/8 (xchacha)
12
   ()
13
   (:default-initargs :core-function #'chacha/8-core))
14
 
15
 (defmethod shared-initialize :after ((cipher xchacha) slot-names
16
                                      &rest initargs
17
                                      &key (key nil key-p)
18
                                      (initialization-vector nil iv-p)
19
                                      &allow-other-keys)
20
   (declare (ignore initargs key key-p iv-p))
21
   (let ((state (chacha-state cipher))
22
         (buffer (make-array 64 :element-type '(unsigned-byte 8))))
23
     (declare (type chacha-state state)
24
              (type chacha-keystream-buffer buffer))
25
     (when initialization-vector
26
       (when (< (length initialization-vector) 24)
27
         (error 'invalid-initialization-vector
28
                :cipher (class-name (class-of cipher))
29
                :block-length 24))
30
       (setf (aref state 12) (ub32ref/le initialization-vector 0)
31
             (aref state 13) (ub32ref/le initialization-vector 4)
32
             (aref state 14) (ub32ref/le initialization-vector 8)
33
             (aref state 15) (ub32ref/le initialization-vector 12)))
34
     (funcall (chacha-core-function cipher) buffer state)
35
     (setf (aref state 4) (mod32- (ub32ref/le buffer 0) (aref state 0))
36
           (aref state 5) (mod32- (ub32ref/le buffer 4) (aref state 1))
37
           (aref state 6) (mod32- (ub32ref/le buffer 8) (aref state 2))
38
           (aref state 7) (mod32- (ub32ref/le buffer 12) (aref state 3))
39
           (aref state 8) (mod32- (ub32ref/le buffer 48) (aref state 12))
40
           (aref state 9) (mod32- (ub32ref/le buffer 52) (aref state 13))
41
           (aref state 10) (mod32- (ub32ref/le buffer 56) (aref state 14))
42
           (aref state 11) (mod32- (ub32ref/le buffer 60) (aref state 15))
43
           (aref state 12) 0
44
           (aref state 13) 0)
45
     (if initialization-vector
46
         (setf (aref state 14) (ub32ref/le initialization-vector 16)
47
               (aref state 15) (ub32ref/le initialization-vector 20))
48
         (setf (aref state 14) 0
49
               (aref state 15) 0)))
50
   cipher)
51
 
52
 (defcipher xchacha
53
   (:mode :stream)
54
   (:crypt-function chacha-crypt)
55
   (:key-length (:fixed 16 32)))
56
 
57
 (defcipher xchacha/12
58
   (:mode :stream)
59
   (:crypt-function chacha-crypt)
60
   (:key-length (:fixed 16 32)))
61
 
62
 (defcipher xchacha/8
63
   (:mode :stream)
64
   (:crypt-function chacha-crypt)
65
   (:key-length (:fixed 16 32)))