-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbreak_ctr_test.clj
executable file
·36 lines (25 loc) · 1 KB
/
break_ctr_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
(ns set3.break-ctr-test
(:require [set3.break-ctr :as sut]
[clojure.test :refer :all]
[util.tools :as u]
[util.random :as rand]
[util.aes :as aes]
[clojure.string :as str]
[clojure.java.io :as io]))
(def plaintexts (map u/base64-to-byte' (str/split-lines (slurp (io/resource "set3/more_ctr_plaintexts.txt")))))
(def cipher-key (rand/byte-lst 16))
(def nonce (repeat 8 0))
(defn encrypt-text
[plaintext]
(aes/encrypt plaintext cipher-key :ctr nonce))
(def ciphertexts (map encrypt-text plaintexts))
(defn check-broken-plaintext-success
[pts]
(->> plaintexts
(map #(count (filter (fn [[f t]] (not= f t)) (map vector %1 %2))) pts)
(reduce + 0)
;; If >75% of text matched, then breaking worked quite well
(> (/ (reduce #(+ %1 (count %2)) 0 plaintexts) 25))))
(deftest ^:parallel break-ctr-test
(testing "Failed to break CTR ciphertext"
(is (check-broken-plaintext-success (sut/break-ctr ciphertexts)))))