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-encryptor l) l) |
---|
33 | (define (dummy-decryptor l) l) |
---|
34 | |
---|
35 | (define (test-cbc name string) |
---|
36 | (let* |
---|
37 | ((encryptor dummy-encryptor) |
---|
38 | (decryptor dummy-decryptor) |
---|
39 | (cbc-encryptor (make-cbc-encryptor encryptor 16)) |
---|
40 | (cbc-decryptor (make-cbc-decryptor decryptor 16)) |
---|
41 | (test-input (string->blob string)) |
---|
42 | (encrypted (cbc-encryptor test-input (hexstring->blob "00010203050607080A0B0C0D0F101112"))) |
---|
43 | (decrypted (cbc-decryptor encrypted (hexstring->blob "00010203050607080A0B0C0D0F101112")))) |
---|
44 | |
---|
45 | (printf "Test vector ~Aa: ~A\n" name (blob->hexstring/uppercase encrypted)) |
---|
46 | (printf "Test vector ~Ab: <~A>\n" name (blob->string decrypted)) |
---|
47 | (assert (blob=? test-input decrypted)))) |
---|
48 | |
---|
49 | (test-cbc "5" "") ; Zero bytes |
---|
50 | (test-cbc "6" "a") ; One byte |
---|
51 | (test-cbc "7" "1234567890123456") ; 1 block exactly |
---|
52 | (test-cbc "8" "12345678901234561234567890123456") ; 2 blocks exactly |
---|
53 | (test-cbc "9" "1234567890123456X") ; 1 block + 1 |
---|
54 | (test-cbc "10" "12345678901234561234567890123456X") ; 2 blocks + 1 |
---|
55 | (test-cbc "11" "123456789012345") ; 1 block - 1 |
---|
56 | (test-cbc "12" "1234567890123456123456789012345") ; 2 blocks - 1 |
---|