// AesOracle prepends and appends 5-10 random bytes to a plaintext, // encrypts the plaintext under a predetermined BlockMode, then // returns the detected BlockMode func AesOracle(plaintext []byte, encrypter cipher.BlockMode) string { // Generate random bytes to prepend/append to plaintext prependBytes, _ := bytes.Random(r.Intn(5) + 5) appendBytes, _ := bytes.Random(r.Intn(5) + 5) plaintext = append(prependBytes, plaintext...) plaintext = append(plaintext, appendBytes...) plaintext, _ = blocks.Pkcs7(plaintext, aes.BlockSize) ciphertext := make([]byte, len(plaintext)) modifiedCiphertext := make([]byte, len(plaintext)) // Modify the first block of the plaintext modified := plaintext modified[0] = byte(255) encrypter.CryptBlocks(ciphertext, plaintext) encrypter.CryptBlocks(modifiedCiphertext, modified) // If the second block in the modified ciphertext is affected by a // change in the first block of the plaintext, return CBC mode if ciphertext[16] != modifiedCiphertext[16] { return "CBC" } return "ECB" }
// Implement PKCS#/ padding func c9() (actual, expected Result) { input := "YELLOW SUBMARINE" expected = "YELLOW SUBMARINE\x04\x04\x04\x04" padded, err := blocks.Pkcs7([]byte(input), 20) if err != nil { log.Fatal(err) } return string(padded), expected }
func EcbEncrypt(plaintext, key []byte) ([]byte, error) { plaintext, _ = blocks.Pkcs7(plaintext, aes.BlockSize) block, err := aes.NewCipher(key) if err != nil { return nil, err } ciphertext := make([]byte, len(plaintext)) encrypter := NewECBEncrypter(block) encrypter.CryptBlocks(ciphertext, plaintext) return ciphertext, nil }