//Test for Set 1 challenge 6 func TestBreakRepeatingKeyXor(t *testing.T) { file, err := os.Open("../files/6.txt") if err != nil { t.Errorf("RepeatKeyXorKeysize: Error opening file.") } defer file.Close() scanner := bufio.NewScanner(file) var bytes []byte for scanner.Scan() { line := scanner.Text() bytes = append(bytes, encoding.Base64ToBytes(line)...) } if FindRepeatKeyXorKeysize(bytes) != 29 { t.Errorf("BreakRepeatingKeyXor: Actual key size did not match expected key size.") } _, key := BreakRepeatingKeyXor(bytes) //t.Log(string(key)) if string(key) != "Terminator X: Bring the noise" { t.Errorf("BreakRepeatingKeyXor: Actual result did not match exptected result.") } }
//Test for set 3 challenge 20 func TestRepeatNonceCTRMode(t *testing.T) { file, err := os.Open("../files/20.txt") if err != nil { t.Errorf("DetectECBMode: Error opening file.") } defer file.Close() scanner := bufio.NewScanner(file) smallest_len := 99999999 var lines [][]byte for scanner.Scan() { line := encoding.Base64ToBytes(scanner.Text()) if len(line) < smallest_len { smallest_len = len(line) } lines = append(lines, line) } var ciphertext_blob []byte for _, l := range lines { ciphertext_blob = append(ciphertext_blob, l[:smallest_len]...) } pt, _ := BreakRepeatingKeyXor(ciphertext_blob) expected_result := "I'm rated \"R\"...this is a warning, ya better void" if string(pt[:len(expected_result)]) != expected_result { t.Errorf("RepeatNonceCTRMode: Expected result did not match actual result!") } }
//Oracle for set 2 challenge 12 func ECBChosenPrefix(input []byte, key []byte) []byte { secret := encoding.Base64ToBytes(`Um9sbGluJyBpbiBteSA1LjAKV2l0aCBteSByYWctdG9wIGRvd24gc28gbXkgaGFpciBjYW4gYmxvdwpUaGUgZ2lybGllcyBvbiBzdGFuZGJ5IHdhdmluZyBqdXN0IHRvIHNheSBoaQpEaWQgeW91IHN0b3A/IE5vLCBJIGp1c3QgZHJvdmUgYnkK`) data := append(input, secret...) data = aes.Pad(data, 16) return aes.ECBEncrypt(key, data) }
//Test for set 3 challenge 18 func TestCTRDecrypt(t *testing.T) { ct := encoding.Base64ToBytes("L77na/nrFsKvynd6HzOoG7GHTLXsTVu9qvY/2syLXzhPweyyMTJULu/6/kXX0KSvoOLSFQ==") key := []byte("YELLOW SUBMARINE") result := string(CTRDecrypt(key, uint64(0), ct)) if result != "Yo, VIP Let's kick it Ice, Ice, baby Ice, Ice, baby " { t.Errorf("CTRDecrypt: Actual result did not match expected result!") t.Errorf("%v", []byte(result)) t.Errorf("%v", []byte("Yo, VIP Let's kick it Ice, Ice, baby Ice, Ice, baby")) } }
//Test for Set 2 challenge 10 func TestCBCMode(t *testing.T) { key := []byte("YELLOW SUBMARINE") iv := []byte("\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00") file, err := os.Open("../files/10.txt") if err != nil { fmt.Println("[ERR] CBCMode test failed to open file.") os.Exit(1) } defer file.Close() scanner := bufio.NewScanner(file) var ciphertext []byte for scanner.Scan() { line := encoding.Base64ToBytes(scanner.Text()) ciphertext = append(ciphertext, []byte(line)...) } if string(CBCEncrypt(key, iv, (CBCDecrypt(key, iv, ciphertext)))) != string(ciphertext) { t.Errorf("CBCMode: Expected result did not match actual result") } }
//Test for Set 1 challenge 7 func TestAESDecrypt(t *testing.T) { key := []byte("YELLOW SUBMARINE") file, err := os.Open("../files/7.txt") if err != nil { fmt.Println("[ERR] AESDecrypt failed to open file.") os.Exit(1) } defer file.Close() scanner := bufio.NewScanner(file) var ciphertext []byte for scanner.Scan() { line := encoding.Base64ToBytes(scanner.Text()) ciphertext = append(ciphertext, []byte(line)...) } if actual := ECBEncrypt(key, ECBDecrypt(key, ciphertext)); string(actual) != string(ciphertext) { t.Errorf("AESDecrypt: Decrypting and then encrypting failed!") t.Errorf("Actual: %v\n", actual) t.Errorf("Expected: %v\n", ciphertext) } }
//One of the oracle functions for the CBC padding oracle. func CBCCookieCreate() (key, iv, ct []byte) { key = RandBytes(16) iv = RandBytes(16) file, _ := os.Open("../files/17.txt") defer file.Close() scanner := bufio.NewScanner(file) r := rand.NewSource(time.Now().UnixNano()) rnd := rand.New(r) line := rnd.Intn(10) var pt []byte var i int for scanner.Scan() { if i == line { pt = encoding.Base64ToBytes(scanner.Text()) break } i++ scanner.Text() } pt = Pad(pt, 16) fmt.Println("PLAINTEXT:\n", "(", len(pt), ")", string(pt), "\n", pt) ct = CBCEncrypt(key, iv, pt) return }