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

KindCoveredAll%
expression088 0.0
branch010 0.0
Key
Not instrumented
Conditionalized out
Executed
Not executed
 
Both branches taken
One branch taken
Neither branch taken
1
 ;;;; xor.lisp -- the do-nothing cipher
2
 
3
 ;;; It's not very secure, but it does come in handy to serve as a dummy
4
 ;;; cipher in security protocols before ciphers and keys have been
5
 ;;; established.
6
 (cl:in-package :crypto)
7
 
8
 (eval-when (:compile-toplevel :load-toplevel :execute)
9
   (defconstant +xor-block-length+ 8))
10
 
11
 (defclass xor (cipher 8-byte-block-mixin)
12
   ((key :accessor xor-key :type simple-octet-vector)
13
    (key-index :accessor xor-key-index :initform 0 :type fixnum)))
14
 
15
 (defun xor-crypt-block (context in in-start out out-start)
16
   (declare (type simple-octet-vector in out))
17
   (let* ((key (xor-key context))
18
          (key-index (xor-key-index context))
19
          (key-length (length key)))
20
     (declare (type simple-octet-vector key))
21
     (cond
22
       ((= key-length 1)
23
        (let ((byte (aref key 0)))
24
          ;; Ignore the case where we just crypt in place.
25
          (unless (and (zerop byte)
26
                       (eq in out)
27
                       (= in-start out-start))
28
            (dotimes (i +xor-block-length+)
29
              (setf (aref out (+ out-start i))
30
                    (logxor byte (aref in (+ in-start i))))))))
31
       (t
32
        (dotimes (i +xor-block-length+)
33
          (setf (aref out (+ out-start i))
34
                (logxor (aref key key-index) (aref in (+ in-start i))))
35
          (incf key-index)
36
          (when (>= key-index key-length)
37
            (setf key-index 0)))
38
        (setf (xor-key-index context) key-index)))))
39
 
40
 (define-block-encryptor xor #.+xor-block-length+
41
   (xor-crypt-block context plaintext plaintext-start ciphertext ciphertext-start))
42
 
43
 (define-block-decryptor xor #.+xor-block-length+
44
   (xor-crypt-block context ciphertext ciphertext-start plaintext plaintext-start))
45
 
46
 (defmethod schedule-key ((cipher xor) key)
47
   ;; Optimize the probable common case of a key with bytes all the same.
48
   (let ((short-key (remove-duplicates key)))
49
     (if (= (length short-key) 1)
50
         (setf (xor-key cipher) short-key)
51
         (setf (xor-key cipher) key))
52
     cipher))
53
 
54
 (defcipher xor
55
   (:encrypt-function xor-encrypt-block)
56
   (:decrypt-function xor-decrypt-block)
57
   (:block-length #.+xor-block-length+)
58
   (:key-length (:variable 1 256 1)))