1 | (use crypto-tools) |
---|
2 | |
---|
3 | (define hexstring1 (blob->hexstring (hexstring->blob "0123456789ABCDEF"))) |
---|
4 | (printf "Hex1: ~A\n" hexstring1) |
---|
5 | (assert (string=? hexstring1 "0123456789abcdef")) |
---|
6 | |
---|
7 | (define hexstring2 (blob->hexstring/uppercase (hexstring->blob "0123456789ABCDEF"))) |
---|
8 | (printf "Hex2: ~A\n" hexstring2) |
---|
9 | (assert (string=? hexstring2 "0123456789ABCDEF")) |
---|
10 | |
---|
11 | (define hexstring3 (blob->hexstring (hexstring->blob ""))) |
---|
12 | (printf "Hex3: ~A\n" hexstring3) |
---|
13 | (assert (string=? hexstring3 "")) |
---|
14 | |
---|
15 | (define (test-padding name input) |
---|
16 | (let* |
---|
17 | ((input-blob (string->blob input)) |
---|
18 | (padded (blob-pad input-blob 16))) |
---|
19 | |
---|
20 | (printf "Test vector ~Aa: ~A\n" name (blob->hexstring padded)) |
---|
21 | (let ((unpadded (blob-unpad padded))) |
---|
22 | |
---|
23 | (printf "Test vector ~Ab: <~A>\n" name (blob->string unpadded)) |
---|
24 | |
---|
25 | (assert (blob=? input-blob unpadded))))) |
---|
26 | |
---|
27 | (test-padding "1" "") |
---|
28 | (test-padding "2" "a") |
---|
29 | (test-padding "3" "ab") |
---|
30 | (test-padding "4" "abcdefghijklmno") |
---|
31 | |
---|
32 | (define dummy-key (hexstring->blob "01010101010101010101010101010101")) |
---|
33 | (define (dummy-encryptor l) (blob-xor l dummy-key)) |
---|
34 | (define dummy-decryptor dummy-encryptor) |
---|
35 | |
---|
36 | (define (test-cbc name string) |
---|
37 | (let* |
---|
38 | ((encryptor dummy-encryptor) |
---|
39 | (decryptor dummy-decryptor) |
---|
40 | (cbc-encryptor (make-cbc-encryptor encryptor 16)) |
---|
41 | (cbc-decryptor (make-cbc-decryptor decryptor 16)) |
---|
42 | (test-input (string->blob string)) |
---|
43 | (encrypted (cbc-encryptor test-input (hexstring->blob "00010203050607080A0B0C0D0F101112"))) |
---|
44 | (decrypted (cbc-decryptor encrypted (hexstring->blob "00010203050607080A0B0C0D0F101112")))) |
---|
45 | |
---|
46 | (printf "Test vector ~Aa: ~A\n" name (blob->hexstring/uppercase encrypted)) |
---|
47 | (printf "Test vector ~Ab: <~A>\n" name (blob->string decrypted)) |
---|
48 | (assert (blob=? test-input decrypted)))) |
---|
49 | |
---|
50 | (test-cbc "5" "") ; Zero bytes |
---|
51 | (test-cbc "6" "a") ; One byte |
---|
52 | (test-cbc "7" "1234567890123456") ; 1 block exactly |
---|
53 | (test-cbc "8" "12345678901234561234567890123456") ; 2 blocks exactly |
---|
54 | (test-cbc "9" "1234567890123456X") ; 1 block + 1 |
---|
55 | (test-cbc "10" "12345678901234561234567890123456X") ; 2 blocks + 1 |
---|
56 | (test-cbc "11" "123456789012345") ; 1 block - 1 |
---|
57 | (test-cbc "12" "1234567890123456123456789012345") ; 2 blocks - 1 |
---|
58 | |
---|
59 | (define (test-cbc* name string) |
---|
60 | (let* |
---|
61 | ((encryptor dummy-encryptor) |
---|
62 | (decryptor dummy-decryptor) |
---|
63 | (cbc-encryptor (make-cbc*-encryptor encryptor 16)) |
---|
64 | (cbc-decryptor (make-cbc*-decryptor decryptor 16)) |
---|
65 | (test-input (string->blob string)) |
---|
66 | (encrypted (cbc-encryptor test-input (hexstring->blob "00010203050607080A0B0C0D0F101112"))) |
---|
67 | (decrypted (cbc-decryptor encrypted))) |
---|
68 | |
---|
69 | (printf "Test vector ~Aa: ~A\n" name (blob->hexstring/uppercase encrypted)) |
---|
70 | (printf "Test vector ~Ab: <~A>\n" name (blob->string decrypted)) |
---|
71 | (assert (blob=? test-input decrypted)))) |
---|
72 | |
---|
73 | (test-cbc* "13" "") ; Zero bytes |
---|
74 | (test-cbc* "14" "a") ; One byte |
---|
75 | (test-cbc* "15" "1234567890123456") ; 1 block exactly |
---|
76 | (test-cbc* "16" "12345678901234561234567890123456") ; 2 blocks exactly |
---|
77 | (test-cbc* "17" "1234567890123456X") ; 1 block + 1 |
---|
78 | (test-cbc* "18" "12345678901234561234567890123456X") ; 2 blocks + 1 |
---|
79 | (test-cbc* "19" "123456789012345") ; 1 block - 1 |
---|
80 | (test-cbc* "20" "1234567890123456123456789012345") ; 2 blocks - 1 |
---|
81 | |
---|