Coverage report: /home/ellis/comp/ext/ironclad/src/ciphers/xor.lisp
Kind | Covered | All | % |
expression | 0 | 88 | 0.0 |
branch | 0 | 10 | 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
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
6
(cl:in-package :crypto)
8
(eval-when (:compile-toplevel :load-toplevel :execute)
9
(defconstant +xor-block-length+ 8))
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)))
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))
23
(let ((byte (aref key 0)))
24
;; Ignore the case where we just crypt in place.
25
(unless (and (zerop byte)
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))))))))
32
(dotimes (i +xor-block-length+)
33
(setf (aref out (+ out-start i))
34
(logxor (aref key key-index) (aref in (+ in-start i))))
36
(when (>= key-index key-length)
38
(setf (xor-key-index context) key-index)))))
40
(define-block-encryptor xor #.+xor-block-length+
41
(xor-crypt-block context plaintext plaintext-start ciphertext ciphertext-start))
43
(define-block-decryptor xor #.+xor-block-length+
44
(xor-crypt-block context ciphertext ciphertext-start plaintext plaintext-start))
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))
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)))