//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 case for Set 2 Challenge 11
func TestDetectionOracle(t *testing.T) {
	//Simulating data i would send myself
	testcase := []byte("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA")

	//Big ugly oracle function.
	oracle := func(plaintext []byte) (result []byte, mode string) {
		key := make([]byte, 16)
		_, err := rand.Read(key)
		if err != nil {
			t.Errorf("DetectionOracle: Reading random bytes failed.")
		}

		//"Coin flip"
		var number byte
		binary.Read(rand.Reader, binary.LittleEndian, &number)
		cbc := false
		if number%2 == 0 {
			cbc = true
		}

		//Random prefix and postfix
		var pre uint32
		var post uint32
		binary.Read(rand.Reader, binary.LittleEndian, &pre)
		binary.Read(rand.Reader, binary.LittleEndian, &post)
		predata := make([]byte, pre%10)
		postdata := make([]byte, post%10)
		rand.Read(predata)
		rand.Read(postdata)
		data := append(predata, plaintext...)
		data = append(data, postdata...)
		data = aes.Pad(data, 16)

		if cbc {
			iv := make([]byte, 16)
			_, err := rand.Read(iv)
			if err != nil {
				t.Errorf("DetectionOracle: Reading random bytes failed.")
			}
			result = aes.CBCEncrypt(key, iv, data)
			mode = "CBC"
			return
		} else {
			result = aes.ECBEncrypt(key, data)
			mode = "ECB"
			return
		}
	}

	//Iterate oracle
	for i := 0; i < 40; i++ {
		data, mode := oracle(testcase)
		if output := AESModeDetectionOracle(data); output != mode {
			t.Errorf("DetectionOracle: Detection oracle failed to guess correctly.")
			t.Errorf("\tExpected: %v", mode)
			t.Errorf("\tGuessed:  %v", output)
		}
	}
}
Beispiel #3
0
//For simplicity sake, return key and iv too.
func EncryptComment(comment string) (key, iv, ciphertext []byte) {
	pre := []byte("comment1=cooking%20MCs;userdata=")
	//Eat illegal characters
	comment = strings.Split(comment, ";")[0]
	comment = strings.Split(comment, "=")[0]
	combytes := []byte(comment)
	post := []byte(";comment2=%20like%20a%20pound%20of%20bacon")
	plaintext := append(pre, combytes...)
	plaintext = append(plaintext, post...)
	plaintext = aes.Pad(plaintext, 16)
	key = aes.RandBytes(16)
	iv = aes.RandBytes(16)
	ciphertext = aes.CBCEncrypt(key, iv, plaintext)
	return
}
Beispiel #4
0
func ProfileFor(key []byte, profile string) []byte {
	u := User{role: "user", uid: "17", email: profile}
	ue := []byte(u.Encode())
	return aes.ECBEncrypt(key, aes.Pad(ue, 16))
}