(use crypto-tools) (define hexstring1 (blob->hexstring (hexstring->blob "0123456789ABCDEF"))) (printf "Hex1: ~A\n" hexstring1) (assert (string=? hexstring1 "0123456789abcdef")) (define hexstring2 (blob->hexstring/uppercase (hexstring->blob "0123456789ABCDEF"))) (printf "Hex2: ~A\n" hexstring2) (assert (string=? hexstring2 "0123456789ABCDEF")) (define hexstring3 (blob->hexstring (hexstring->blob ""))) (printf "Hex3: ~A\n" hexstring3) (assert (string=? hexstring3 "")) (define (test-padding name input) (let* ((input-blob (string->blob input)) (padded (blob-pad input-blob 16))) (printf "Test vector ~Aa: ~A\n" name (blob->hexstring padded)) (let ((unpadded (blob-unpad padded))) (printf "Test vector ~Ab: <~A>\n" name (blob->string unpadded)) (assert (blob=? input-blob unpadded))))) (test-padding "1" "") (test-padding "2" "a") (test-padding "3" "ab") (test-padding "4" "abcdefghijklmno") (define dummy-key (hexstring->blob "01010101010101010101010101010101")) (define (dummy-encryptor l) (blob-xor l dummy-key)) (define dummy-decryptor dummy-encryptor) (define (test-cbc name string) (let* ((encryptor dummy-encryptor) (decryptor dummy-decryptor) (cbc-encryptor (make-cbc-encryptor encryptor 16)) (cbc-decryptor (make-cbc-decryptor decryptor 16)) (test-input (string->blob string)) (encrypted (cbc-encryptor test-input (hexstring->blob "00010203050607080A0B0C0D0F101112"))) (decrypted (cbc-decryptor encrypted (hexstring->blob "00010203050607080A0B0C0D0F101112")))) (printf "Test vector ~Aa: ~A\n" name (blob->hexstring/uppercase encrypted)) (printf "Test vector ~Ab: <~A>\n" name (blob->string decrypted)) (assert (blob=? test-input decrypted)))) (test-cbc "5" "") ; Zero bytes (test-cbc "6" "a") ; One byte (test-cbc "7" "1234567890123456") ; 1 block exactly (test-cbc "8" "12345678901234561234567890123456") ; 2 blocks exactly (test-cbc "9" "1234567890123456X") ; 1 block + 1 (test-cbc "10" "12345678901234561234567890123456X") ; 2 blocks + 1 (test-cbc "11" "123456789012345") ; 1 block - 1 (test-cbc "12" "1234567890123456123456789012345") ; 2 blocks - 1 (define (test-cbc* name string) (let* ((encryptor dummy-encryptor) (decryptor dummy-decryptor) (cbc-encryptor (make-cbc*-encryptor encryptor 16)) (cbc-decryptor (make-cbc*-decryptor decryptor 16)) (test-input (string->blob string)) (encrypted (cbc-encryptor test-input (hexstring->blob "00010203050607080A0B0C0D0F101112"))) (decrypted (cbc-decryptor encrypted))) (printf "Test vector ~Aa: ~A\n" name (blob->hexstring/uppercase encrypted)) (printf "Test vector ~Ab: <~A>\n" name (blob->string decrypted)) (assert (blob=? test-input decrypted)))) (test-cbc* "13" "") ; Zero bytes (test-cbc* "14" "a") ; One byte (test-cbc* "15" "1234567890123456") ; 1 block exactly (test-cbc* "16" "12345678901234561234567890123456") ; 2 blocks exactly (test-cbc* "17" "1234567890123456X") ; 1 block + 1 (test-cbc* "18" "12345678901234561234567890123456X") ; 2 blocks + 1 (test-cbc* "19" "123456789012345") ; 1 block - 1 (test-cbc* "20" "1234567890123456123456789012345") ; 2 blocks - 1