source: project/release/5/crypto-tools/trunk/tests/run.scm @ 38658

Last change on this file since 38658 was 38658, checked in by Alaric Snell-Pym, 5 months ago

Applied patch from dsp (against original c4 version)

File size: 5.4 KB
Line 
1(import crypto-tools)
2(import (chicken format))
3(import (chicken blob))
4
5(define hexstring1 (blob->hexstring (hexstring->blob "0123456789ABCDEF")))
6(format #t "Hex1: ~A\n" hexstring1)
7(assert (string=? hexstring1 "0123456789abcdef"))
8
9(define hexstring2 (blob->hexstring/uppercase (hexstring->blob "0123456789ABCDEF")))
10(format #t "Hex2: ~A\n" hexstring2)
11(assert (string=? hexstring2 "0123456789ABCDEF"))
12
13(define hexstring3 (blob->hexstring (hexstring->blob "")))
14(format #t "Hex3: ~A\n" hexstring3)
15(assert (string=? hexstring3 ""))
16
17(define (test-padding name input)
18   (let*
19      ((input-blob (string->blob input))
20       (padded (blob-pad input-blob 16)))
21       
22      (format #t "Test vector ~Aa: ~A\n" name (blob->hexstring padded))
23      (let ((unpadded (blob-unpad padded)))
24
25         (format #t "Test vector ~Ab: <~A>\n" name (blob->string unpadded))
26     
27         (assert (blob=? input-blob unpadded)))))
28
29(test-padding "1" "")
30(test-padding "2" "a")
31(test-padding "3" "ab")
32(test-padding "4" "abcdefghijklmno")
33
34(define dummy-key (hexstring->blob "01010101010101010101010101010101"))
35(define (dummy-encryptor l) (blob-xor l dummy-key))
36(define dummy-decryptor dummy-encryptor)
37
38(define (test-cbc name string)
39   (let*
40      ((encryptor dummy-encryptor)
41       (decryptor dummy-decryptor)
42       (cbc-encryptor (make-cbc-encryptor encryptor blob-pad 16))
43       (cbc-decryptor (make-cbc-decryptor decryptor blob-unpad 16))
44       (test-input (string->blob string))
45       (encrypted (cbc-encryptor test-input (hexstring->blob "00010203050607080A0B0C0D0F101112")))
46       (decrypted (cbc-decryptor encrypted (hexstring->blob "00010203050607080A0B0C0D0F101112"))))
47   
48      (format #t "Test vector ~Aa: ~A\n" name (blob->hexstring/uppercase encrypted))
49      (format #t "Test vector ~Ab: <~A>\n" name (blob->string decrypted))
50      (assert (blob=? test-input decrypted))))
51
52(test-cbc "5" "") ; Zero bytes
53(test-cbc "6" "a") ; One byte
54(test-cbc "7" "1234567890123456") ; 1 block exactly
55(test-cbc "8" "12345678901234561234567890123456") ; 2 blocks exactly
56(test-cbc "9" "1234567890123456X") ; 1 block + 1
57(test-cbc "10" "12345678901234561234567890123456X") ; 2 blocks + 1
58(test-cbc "11" "123456789012345") ; 1 block - 1
59(test-cbc "12" "1234567890123456123456789012345") ; 2 blocks - 1
60
61(define (test-cbc* name string)
62   (let*
63      ((encryptor dummy-encryptor)
64       (decryptor dummy-decryptor)
65       (cbc-encryptor (make-cbc*-encryptor encryptor blob-pad 16))
66       (cbc-decryptor (make-cbc*-decryptor decryptor blob-unpad 16))
67       (test-input (string->blob string))
68       (encrypted (cbc-encryptor test-input (hexstring->blob "00010203050607080A0B0C0D0F101112")))
69       (decrypted (cbc-decryptor encrypted)))
70   
71      (format #t "Test vector ~Aa: ~A\n" name (blob->hexstring/uppercase encrypted))
72      (format #t "Test vector ~Ab: <~A>\n" name (blob->string decrypted))
73      (assert (blob=? test-input decrypted))))
74
75(test-cbc* "13" "") ; Zero bytes
76(test-cbc* "14" "a") ; One byte
77(test-cbc* "15" "1234567890123456") ; 1 block exactly
78(test-cbc* "16" "12345678901234561234567890123456") ; 2 blocks exactly
79(test-cbc* "17" "1234567890123456X") ; 1 block + 1
80(test-cbc* "18" "12345678901234561234567890123456X") ; 2 blocks + 1
81(test-cbc* "19" "123456789012345") ; 1 block - 1
82(test-cbc* "20" "1234567890123456123456789012345") ; 2 blocks - 1
83
84(define (test-ctr name string)
85   (let*
86      ((encryptor dummy-encryptor)
87       (decryptor dummy-decryptor)
88       (ctr-encryptor (make-ctr-encryptor encryptor 16))
89       (ctr-decryptor (make-ctr-decryptor decryptor 16))
90       (test-input (string->blob string))
91       (iv (hexstring->blob "00010203050607080A0B0C0D0F101112")) 
92       (encrypted (ctr-encryptor test-input iv))
93       (decrypted (ctr-decryptor encrypted iv)))
94   
95      (format #t "Test vector ~Aa: ~A\n" name (blob->hexstring/uppercase encrypted))
96      (format #t "Test vector ~Ab: <~A>\n" name (blob->string decrypted))
97      (assert (blob=? test-input decrypted))))
98
99(test-ctr "21" "") ; Zero bytes
100(test-ctr "22" "a") ; One byte
101(test-ctr "23" "1234567890123456") ; 1 block exactly
102(test-ctr "24" "12345678901234561234567890123456") ; 2 blocks exactly
103(test-ctr "25" "1234567890123456X") ; 1 block + 1
104(test-ctr "26" "12345678901234561234567890123456X") ; 2 blocks + 1
105(test-ctr "27" "123456789012345") ; 1 block - 1
106(test-ctr "28" "1234567890123456123456789012345") ; 2 blocks - 1
107
108
109(define (test-ctr* name string)
110   (let*
111      ((encryptor dummy-encryptor)
112       (decryptor dummy-decryptor)
113       (ctr-encryptor (make-ctr*-encryptor encryptor 16))
114       (ctr-decryptor (make-ctr*-decryptor decryptor 16))
115       (test-input (string->blob string))
116       (encrypted (ctr-encryptor test-input (hexstring->blob "00010203050607080A0B0C0D0F101112")))
117       (decrypted (ctr-decryptor encrypted)))
118   
119      (format #t "Test vector ~Aa: ~A\n" name (blob->hexstring/uppercase encrypted))
120      (format #t "Test vector ~Ab: <~A>\n" name (blob->string decrypted))
121      (assert (blob=? test-input decrypted))))
122
123(test-ctr* "29" "") ; Zero bytes
124(test-ctr* "30" "a") ; One byte
125(test-ctr* "31" "1234567890123456") ; 1 block exactly
126(test-ctr* "32" "12345678901234561234567890123456") ; 2 blocks exactly
127(test-ctr* "33" "1234567890123456X") ; 1 block + 1
128(test-ctr* "34" "12345678901234561234567890123456X") ; 2 blocks + 1
129(test-ctr* "35" "123456789012345") ; 1 block - 1
130(test-ctr* "36" "1234567890123456123456789012345") ; 2 blocks - 1
131
132
Note: See TracBrowser for help on using the repository browser.