cryptopals

package module
v0.0.0-...-5a9ae2b Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 23, 2018 License: MIT Imports: 16 Imported by: 0

README

cryptopals

Documentation

Index

Examples

Constants

View Source
const EnglishLetterFrequenciesTop13 = "ETAOIN SHRDLU"
View Source
const MaxPrefix = 1024 // only check for up to this size of fixed prefix in unknown ECB encryption function.

Variables

View Source
var (
	// ErrMismatchedLength is returned when parameters violate a length-matching invariant.
	ErrMismatchedLength = errors.New("mismatched lengths")
	// ErrEmpty is returned when a necessary value has not been provided.
	ErrEmpty = errors.New("empty")
	// ErrNotFound is returned when a searched-for value cannot be determined.
	ErrNotFound = errors.New("not found")
	// ErrInvalidPadding  is returned when a value is improperly padded.
	ErrInvalidPadding = errors.New("invalid padding")
)

Functions

func DecryptAESCBC

func DecryptAESCBC(ciphertext, key, iv []byte) ([]byte, error)

DecryptAESCBC decrypts the given ciphertext with the given key in CBC mode.

Example
encoded, err := ioutil.ReadFile("testdata/set2/10.txt")
if err != nil {
	panic(err)
}
contents := make([]byte, base64.StdEncoding.DecodedLen(len(encoded)))
if _, err := base64.StdEncoding.Decode(contents, encoded); err != nil {
	panic(err)
}
d, err := DecryptAESCBC(contents, []byte("YELLOW SUBMARINE"), bytes.Repeat([]byte{byte(0x0)}, aes.BlockSize))
fmt.Printf("%v %q\n", err, d[:aes.BlockSize*3])
Output:

<nil> "I'm back and I'm ringin' the bell \nA rockin' on "

func DecryptAESCBCUnknownButConsistentKey

func DecryptAESCBCUnknownButConsistentKey(ciphertext []byte) ([]byte, error)

DecryptAESCBCUnknownButConsistentKey

func DecryptAESECB

func DecryptAESECB(ciphertext, key []byte) ([]byte, error)

DecryptAESECB decrypts the given ciphertext with the given key in ECB mode.

func DecryptAESECBSuffix

func DecryptAESECBSuffix(encryptionFn EncryptionFunc) ([]byte, error)

func DecryptAESECBUnknownButConsistentKey

func DecryptAESECBUnknownButConsistentKey(ciphertext []byte) ([]byte, error)

DecryptAESECBUnknownButConsistentKey

func DetermineBlockSize

func DetermineBlockSize(fn EncryptionFunc) (int, error)

DetermineBlockSize returns the block size of the given encryption function in bytes.

func DeterminePrefixSize

func DeterminePrefixSize(fn EncryptionFunc) (n, blocks int, err error)

DeterminePrefixSize returns the size necessary to include in a plaintext prefix to have the next bytes at the start of a new ECB block.

func EncryptAESCBC

func EncryptAESCBC(plaintext, key, iv []byte) ([]byte, error)

EncryptAESCBC encrypts the given plaintext with the given key in CBC mode.

func EncryptAESCBCUnknownButConsistentKey

func EncryptAESCBCUnknownButConsistentKey(plaintext []byte) ([]byte, error)

EncryptAESCBCUnknownButConsistentKey

func EncryptAESECB

func EncryptAESECB(plaintext, key []byte) ([]byte, error)

EncryptAESECB decrypts the given ciphertext with the given key in ECB mode.

func EncryptAESECBUnknownButConsistentKey

func EncryptAESECBUnknownButConsistentKey(plaintext []byte) ([]byte, error)

EncryptAESECBUnknownButConsistentKey

func EncryptAESECBUnknownButConsistentKeyWithPrefixAndSuffix

func EncryptAESECBUnknownButConsistentKeyWithPrefixAndSuffix(plaintext []byte) ([]byte, error)

EncryptAESECBUnknownButConsistentKeyWithPrefixAndSuffix

func EncryptAESECBUnknownButConsistentKeyWithSuffix

func EncryptAESECBUnknownButConsistentKeyWithSuffix(plaintext []byte) ([]byte, error)

EncryptAESECBUnknownButConsistentKeyWithSuffix

func EncryptAESWithRandomKey

func EncryptAESWithRandomKey(plaintext []byte) ([]byte, error)

EncryptAESWithRandomKey

Example
// 41 characters which will guarantee two repeated blocks.
plaintext := "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
for i := 0; i < 10; i++ {
	b, err := EncryptAESWithRandomKey([]byte(plaintext))
	if err != nil {
		fmt.Println(err)
	}
	h := hex.EncodeToString(b)
	fmt.Printf("%#v %q\n", DetectECBorCBC(b), h)
}
Output:

func HammingDistance

func HammingDistance(a, b []byte) (int, error)

HammingDistance computes the number of bits that differ between a and b.

func HammingDistances

func HammingDistances(in []byte, blocksize int) ([]int, error)

HammingDistances returns a list of hamming distances between the blocks in the provided byte slice.

func HammingDistancesMap

func HammingDistancesMap(in []byte, blocksize int) (map[int][]int, error)

HammingDistancesMap returns a map of hamming distances between the blocks in the provided byte slice.

func Hex2Base64

func Hex2Base64(in []byte) ([]byte, error)

Hex2Base64 takes a byte slice of hex-encoded data and produces base64-encoded output.

func LetterFrequenciesFromString

func LetterFrequenciesFromString(s string, n int) ([]rune, error)

func MinHammingDistance

func MinHammingDistance(in []byte, blocksize int) (int, error)

MinHammingDistance returns the minimum hamming distance between two blocks.

func NLetters

func NLetters(s string) int

func NTopEnglish

func NTopEnglish(s string) int

NTopEnglish returns the number of ocurrencies of common english letters in a string.

func NumMatchingBlocks

func NumMatchingBlocks(in []byte, blocksize int) (int, error)

NumMatchingBlocks computes the number of blocks that match for the given block size.

func NumRepeatingBlocks

func NumRepeatingBlocks(in []byte, blocksize int) (int, int, error)

NumRepeatingBlocks computes the longest run of repeated blocks and the position of the beginning of the run in the given input.

func PKCS7Padding

func PKCS7Padding(in []byte, blockSize int) []byte

PKCS7Padding returns the provided input padded to the given block size.

Example
fmt.Printf("%q", PKCS7Padding([]byte("YELLOW SUBMARINE"), 20))
Output:

"YELLOW SUBMARINE\x04\x04\x04\x04"

func PercentCommonEnglish

func PercentCommonEnglish(s string) float64

PercentCommonEnglish returns the percentage of characters that are appear to be common english letters.

func PercentLetters

func PercentLetters(s string) float64

PercentLetters

func RandomAESKey

func RandomAESKey() []byte

RandomAESKey generates a random aes key.

func RandomNBytes

func RandomNBytes(n int) []byte

RandomBytes generates a random set of bytes.

func RepeatingXOR

func RepeatingXOR(plaintext, key []byte) ([]byte, error)

RepeatingXOR returns the hex-encoded result of repeated XOR application of key to plaintext.

func SolveRepeatingXOR

func SolveRepeatingXOR(ciphertext []byte, keysize int) ([]byte, error)

SolveRepeatingXOR attempts to recover plaintext from a given repeated-xor encrypted ciphertext and given key size.

func SolveSingleByteXOR

func SolveSingleByteXOR(in []byte) (key byte, plaintext string, err error)

SolveSingleByteXOR attempts to recover plaintext from a single-byte xor encrypted ciphertext.

func StripPKCS7Padding

func StripPKCS7Padding(in []byte, blockSize int) ([]byte, error)

StripPKCS7Padding returns an unpadded version of the input or a non-nil error if the PKCS7 padding is invalid.

func XORHexSlices

func XORHexSlices(a, b []byte) ([]byte, error)

XORHexSlices takes a two byte slices of hex-encoded data and produces the hex-encoded XOR result.

Types

type BlockMode

type BlockMode int
const (
	UnknownBlockMode BlockMode = iota
	ECBBlockMode
	CBCBlockMode
)

func DetectECBorCBC

func DetectECBorCBC(in []byte) BlockMode

DetectECBorCBC is our oracle for detecting ECB vs CBC.

func (BlockMode) String

func (i BlockMode) String() string

type EncryptionFunc

type EncryptionFunc func([]byte) ([]byte, error)

type LetterFrequencies

type LetterFrequencies map[rune]int

LetterFrequencies allows computation of rune frequencies.

func (LetterFrequencies) TopN

func (lf LetterFrequencies) TopN(n int) ([]rune, error)

TopN returns the top n printable runes by frequency.

func (LetterFrequencies) Write

func (lf LetterFrequencies) Write(p []byte) (n int, err error)

Write satisfies the io.Writer interface.

type RuneFreq

type RuneFreq struct {
	// contains filtered or unexported fields
}

RuneFreq is a rune,occurance pair.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL