-
Notifications
You must be signed in to change notification settings - Fork 0
/
cbc_key_recovery_test.clj
48 lines (35 loc) · 1.37 KB
/
cbc_key_recovery_test.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
(ns set4.cbc-key-recovery-test
(:require [set4.cbc-key-recovery :as sut]
[clojure.test :refer :all]
[util.tools :as u]
[util.random :as rand]
[util.aes :as aes]
[clojure.string :as str]))
(def random-cipher-key (rand/byte-lst 16))
(def random-iv random-cipher-key)
(def prefix (map int "comment1=cooking%20MCs;userdata="))
(def suffix (map int ";comment2=%20like%20a%20pound%20of%20bacon"))
(defn encrypt-cookie
[userdata]
(when (some #(or (= (int \;) %) (= (int \=) %)) userdata)
(throw (Exception. "Invalid userdata. Should not contain ; or =")))
(aes/encrypt (concat prefix
userdata
suffix)
random-cipher-key
:cbc random-iv))
(defn decrypt-cookie
([ciphertext cipher-key]
(let [plaintext (aes/decrypt ciphertext cipher-key :cbc random-iv)]
(if (first (filter #(> % 127) plaintext))
(u/raise (u/bytes->str plaintext))
plaintext)))
([ciphertext] (decrypt-cookie ciphertext random-cipher-key)))
(defn parse-cookie
[cookie]
(reduce #(conj %1 (str/split %2 #"=")) {}
(filter #(not= % "") (str/split (u/bytes->str cookie) #";"))))
(deftest cbc-key-recovery-test
(testing "Failed to extract CBC key"
(is (= random-cipher-key
(sut/cbc-key-recovery encrypt-cookie decrypt-cookie)))))