Documentation
¶
Overview ¶
Package cryptopals provides solutions for set 1 of cryptopals.
This package requires Go 1.18, as it makes use of generics, even though the challenges can be solved entirely without them.
Index ¶
- Variables
- func ComputeHamming(x, y []byte) (count int)
- func DecryptAdmin(src []byte, bl cipher.Block) bool
- func DecryptAppendOracle(oracle func([]byte) []byte) []byte
- func DecryptCBC(bl cipher.Block, src []byte, iv []byte) []byte
- func DecryptECB(src []byte, block cipher.Block) []byte
- func DecryptPaddingOracle(src []byte, blockSize int, padCheck func([]byte) bool) string
- func DecryptPrependOracle(oracle func([]byte) []byte) []byte
- func DecryptStream()
- func DetectECB(src []byte, blockSize int) bool
- func EncryptCBC(bl cipher.Block, src []byte, iv []byte) []byte
- func EncryptCTR(bl cipher.Block, src []byte, nonce []byte) []byte
- func EncryptECB(src []byte, bl cipher.Block) []byte
- func FindTransposedXorKey(src []byte, size int) []byte
- func FindXorKeySize(src []byte) (result int)
- func GenRandKey(size int) []byte
- func GenerateAdmin(oracle func(email string) []byte, size int) []byte
- func GenerateProfile(email string) string
- func GuessKey(src []byte) (key byte, high float64)
- func HexToBase64(hs string) string
- func NewAppendECBOracle(secret string) func([]byte) []byte
- func NewCBCBitflipOracle() (oracle func([]byte) []byte, checkAdmin func([]byte) bool)
- func NewCBCECBOracle() func([]byte) []byte
- func NewCBCPaddingOracle(strs []string) (res []byte, plaintext string, verify func([]byte) bool)
- func NewPrependECBOracle(secret string) func([]byte) []byte
- func NewProfileOracle() (oracle func(email string) []byte, block cipher.Block)
- func PadPKCS7(src []byte, size uint8) []byte
- func ScoreString(s string) (count float64)
- func SingleXor[T constraints.Integer](x []T, key T) []T
- func SolveCBCBitflipOracle(oracle func([]byte) []byte) []byte
- func TransposeBytes(src []byte, size int) [][]byte
- func UnpadPKCS7(src []byte, size uint8) []byte
- func XorSlice[T constraints.Integer](x, y []T) []T
Constants ¶
This section is empty.
Variables ¶
var DecryptCTR = EncryptCTR
Functions ¶
func ComputeHamming ¶
ComputeHammering counts the differing bits in the strings by xoring them together and counting the remaining bits.
func DecryptAdmin ¶
DecryptAdmin decrypts the ciphertext and evaluates if it grants admin access.
func DecryptAppendOracle ¶
DecryptAppendOracle attempts to recover appended plaintext given by NewAppendECBOracle.
func DecryptCBC ¶
DecryptCBC decrypts a block by decrypting the ciphertext block and xoring it with the previous ciphetext to obtain the plaintext.
func DecryptECB ¶
DecryptECB decrypts a byte slice encrypted in ECB mode.
func DecryptPaddingOracle ¶
func DecryptPrependOracle ¶
DecryptPrependOracle is very similar in spirit to DecryptAppendOracle; however, there is a random but consistent number of random bytes prepended to our plaintext. By consistently padding these bytes out we can slice/discard the ciphertext up to our controlled bytes and use DecryptAppendOracle to solve.
func DecryptStream ¶
func DecryptStream()
func DetectECB ¶
DetectECB detects if a string is encrypted in ECB mode by checking for the existence of identical blocks with a map.
func EncryptCBC ¶
EncryptCBC encrypts a block by xoring the plaintext block by the previous ciphertext block and encrypting with the block cipher.
func EncryptCTR ¶
EncryptCTR encrypts plaintext using ECB mode.
func EncryptECB ¶
EncryptECB encrypts plaintext using ECB mode.
func FindTransposedXorKey ¶
FindTransposedXorKey guesses the key by taking transposing the byte slice into chunks of length size and attempting to solve each chunk via GuessKey.
func FindXorKeySize ¶
FindXorKeySize is a probabilistic search for the key length in a range of 2..40. This is done by exploiting the fact that English has a lower hamming distance than random bytes; utilizing this property allows us to perform a search for the length of the key.
If our two chunks (x, y) are indeed the length of the key, we can expect the hamming distance between them to be exactly the same as their actual hamming distance. We can test this by writing a simple function.
x, y := []byte{1, 3, 3, 7}, []byte{10, 14, 14, 15} key := decodeHex("f00d") a, b := ComputeHamming(x, y), ComputeHamming(XorSlice(x, key), XorSlice(y, key)) if a != b { panic("oh no this should not happen???") }
See https://crypto.stackexchange.com/a/8118 for more info.
func GenRandKey ¶
GenRandKey generates a cryptographically random key of length size.
func GenerateAdmin ¶
GenerateAdmin takes advantage of our ability to manipulate blocks to craft a block with the role key and the admin value in separate blocks.
For instance, the plaintext blocks for email foo@bar.com when blocksize is 16 looks something like
email=foo@bar.co m&role=user&uid=1 0
We can isolate the role key and value to obtain the ability to effectively the key to an arbitrary value; it will look like the below.
email=AAAA&role= user&uid=10
Afterwards, we can just make a large email that will take up the whole block and then some; we can combine these two blocks together to yield
email=AAAA&role= admin&role=user& uid=10
func GenerateProfile ¶
GenerateProfile takes an email and encodes it into "URL encoded" form.
func GuessKey ¶
GuessKey automates iterating over 0..255 SingleXors, returning the single byte key with the highest scoring string along with the score.
func HexToBase64 ¶
HexToBase64 takes a hex-encoded string, decoding it as a byte slice and re-encoding it in base64 format.
func NewAppendECBOracle ¶
NewAppendECBOracle creates a new ECB oracle that appends the specified secret, padding appropriately and encrypting the input in ECB mode.
func NewCBCBitflipOracle ¶
NewCBCBitflipOracle returns an oracle that takes an input, prepends "comment1=cooking%20MCs;userdata=" and appends ";comment2=%20like%20a%20pound%20of%20bacon", padding with PKCS#7, as well as another function that decrypts the input and checks for the existence of ";admin=true;"
func NewCBCECBOracle ¶
NewCBCECBOracle takes a plaintext and encrypts it with AES, choosing ECB mode or CBC mode randomly. The key (and IV for CBC mode) are cryptographically secure, using the crypto/rand package.
func NewCBCPaddingOracle ¶
NewCBCPaddingOracle crafts a new CBC padding oracle and appends the IV to the ciphertext, automatically removing it at decryption time.
func NewPrependECBOracle ¶
NewPrependECBOracle creates a new ECB oracle that prepends the specified secret, padding appropriately and encrypting the input in ECB mode.
func NewProfileOracle ¶
NewProfileOracle returns an oracle that takes an email, generates a profile from it, and encrypts it under ECB mode.
func ScoreString ¶
ScoreString scores how the string is in-line with the most frequent English letters.
func SingleXor ¶
func SingleXor[T constraints.Integer](x []T, key T) []T
SingleXor XORs an integer slice by an integer.
func SolveCBCBitflipOracle ¶
SolveCBCBitflipOracle solves challenge 14 by flipping two bits to get ;admin=true; in the plaintext. This works by taking advantage of how CBC decryption works: a plaintext block is the result of decrypting the ciphertext and xoring it with the previous block.
If we can control the plaintext, we know that the raw result of the AES decryption pass is
D[i] ^ C[i-1]
By editing the same byte in the previous block we can craft a byte that is
C[i] ^= NEXT_BYTE_SAME_POSITION ^ TARGET_BYTE
I've elected to choose the 'X' byte to make this obvious, but it could feasibly be any byte.
func TransposeBytes ¶
TransposeBytes takes an integer slice and creates a slice of length size, then transposes those bytes appropriately.
func UnpadPKCS7 ¶
UnpadPKCS7 unpads src by reading the last byte value and deleting the specified number of bytes.
func XorSlice ¶
func XorSlice[T constraints.Integer](x, y []T) []T
XorSlice takes two integer slices and XORs the first slice by the second one. Callers should be careful to ensure that the length of y is greater than zero to avoid divide by 0.
Types ¶
This section is empty.