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

KindCoveredAll%
expression0160 0.0
branch024 0.0
Key
Not instrumented
Conditionalized out
Executed
Not executed
 
Both branches taken
One branch taken
Neither branch taken
1
 ;;;; padding.lisp -- implementation of various padding algorithms
2
 (in-package :crypto)
3
 
4
 (defclass padding () ())
5
 
6
 ;;; PKCS7 padding
7
 (defclass pkcs7-padding (padding) ())
8
 
9
 (defmethod add-padding-bytes ((padding pkcs7-padding) text start block-offset block-size)
10
   (declare (type simple-octet-vector text))
11
   (declare (type index start block-offset))
12
   (let ((n-padding-bytes (- block-size block-offset)))
13
     (declare (type (unsigned-byte 8) n-padding-bytes))
14
     (when (plusp n-padding-bytes)
15
       (fill text n-padding-bytes :start (+ start block-offset) :end (+ start block-size)))
16
     (values)))
17
 
18
 (defmethod count-padding-bytes ((padding pkcs7-padding) text start block-size)
19
   (declare (type simple-octet-vector text))
20
   (declare (type index start))
21
   (let* ((end (+ start block-size))
22
          (n-padding-bytes (aref text (1- end)))
23
          (offset (- end n-padding-bytes)))
24
     (declare (type index end offset))
25
     (declare (type (unsigned-byte 8) n-padding-bytes))
26
     (when (or (> n-padding-bytes block-size)
27
               (not (loop for i from offset below end
28
                          always (= (aref text i) n-padding-bytes))))
29
       (error 'invalid-padding :name 'pkcs7 :block text))
30
     n-padding-bytes))
31
 
32
 ;;; ANSI X.923 padding
33
 (defclass ansi-x923-padding (padding) ())
34
 
35
 (defmethod add-padding-bytes ((padding ansi-x923-padding) text start block-offset block-size)
36
   (declare (type simple-octet-vector text))
37
   (declare (type index start block-offset))
38
   (let ((end (+ start block-size))
39
         (n-padding-bytes (- block-size block-offset)))
40
     (declare (type index end))
41
     (declare (type (unsigned-byte 8) n-padding-bytes))
42
     (when (plusp n-padding-bytes)
43
       (fill text 0 :start (+ start block-offset) :end end)
44
       (setf (aref text (1- end)) n-padding-bytes))
45
     (values)))
46
 
47
 (defmethod count-padding-bytes ((padding ansi-x923-padding) text start block-size)
48
   (declare (type simple-octet-vector text))
49
   (declare (type index start))
50
   (let* ((end (+ start block-size))
51
          (n-padding-bytes (aref text (1- end)))
52
          (offset (- end n-padding-bytes)))
53
     (declare (type index end))
54
     (declare (type (unsigned-byte 8) n-padding-bytes))
55
     (when (or (> n-padding-bytes block-size)
56
               (not (loop for i from offset below (1- end)
57
                          always (zerop (aref text i)))))
58
       (error 'invalid-padding :name 'ansi-x923 :block text))
59
     n-padding-bytes))
60
 
61
 
62
 ;;; ISO 7816-4 padding
63
 (defclass iso-7816-4-padding (padding) ())
64
 
65
 (defmethod add-padding-bytes ((padding iso-7816-4-padding) text start block-offset block-size)
66
   (declare (type simple-octet-vector text))
67
   (declare (type index start block-offset))
68
   (let ((end (+ start block-size))
69
         (offset (+ start block-offset)))
70
     (declare (type index end offset))
71
     (when (< block-offset block-size)
72
       (setf (aref text offset) #x80)
73
       (fill text 0 :start (1+ offset) :end end))
74
     (values)))
75
 
76
 (defmethod count-padding-bytes ((padding iso-7816-4-padding) text start block-size)
77
   (declare (type simple-octet-vector text))
78
   (declare (type index start))
79
   (let* ((end (+ start block-size))
80
          (offset (position #x80 text :start start :end end :from-end t)))
81
     (declare (type index end))
82
     (when (or (null offset)
83
               (not (loop for i from (1+ offset) below end
84
                          always (zerop (aref text i)))))
85
       (error 'invalid-padding :name 'iso-7816-4 :block text))
86
     (- end offset)))