cryptopals

package module
v0.0.0-...-03fc782 Latest Latest
Warning

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

Go to latest
Published: Jul 24, 2024 License: MIT Imports: 13 Imported by: 0

README

cryptopals

Cryptopals solutions in Go.

Documentation

Overview

Package cryptopals contains solutions to the Cryptopals challenges.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Englishness

func Englishness(b []byte) float64

Englishness scores b on how much it resembles English.

Scores are length-normalized and between 0 and 1 inclusive. Higher is better.

If len(b) == 0, Englishness returns 0.

func FindBlockSize

func FindBlockSize(oracle func([]byte) []byte) int

FindBlockSize returns the block size used by an encryption oracle.

func FindSingleByteXORCiphertext

func FindSingleByteXORCiphertext(cts [][]byte) int

FindSingleByteXORCiphertext returns the index of the ciphertext most likely to be single-byte XOR encrypted.

FindSingleByteXORCiphertext returns -1 if no ciphertext was found.

func Hamming

func Hamming(a, b []byte) int

Hamming returns the Hamming distance between a and b.

It panics if a and b have different lengths.

func HexToBase64

func HexToBase64(s string) (string, error)

HexToBase64 converts a hex-encoded string to a Base64-encoded string.

func IsECBCiphertext

func IsECBCiphertext(b []byte, blockSize int) bool

IsECBCiphertext returns true if b is likely to be ECB encrypted.

func IsECBOracle

func IsECBOracle(oracle func([]byte) []byte) bool

IsECBOracle returns true if an encryption oracle uses ECB mode.

func NewAdminProfile

func NewAdminProfile(m *ProfileManager) []byte

NewAdminProfile performs a cut-and-paste ECB attack to create an admin profile from multiple user profiles.

TODO: Is this possible to do without using an invalid TLD (.admin)?

func NewCBCDecrypter

func NewCBCDecrypter(b cipher.Block, iv []byte) cipher.BlockMode

NewCBCDecrypter returns a cipher.BlockMode which decrypts in cipher block chaining mode.

func NewECBDecrypter

func NewECBDecrypter(b cipher.Block) cipher.BlockMode

NewECBDecrypter returns a cipher.BlockMode which decrypts in electronic codebook mode.

func NewECBEncrypter

func NewECBEncrypter(b cipher.Block) cipher.BlockMode

NewECBEncrypter returns a cipher.BlockMode which encrypts in electronic codebook mode.

func NewECBOrCBCPrefixSuffixOracle

func NewECBOrCBCPrefixSuffixOracle() func([]byte) []byte

NewECBOrCBCPrefixSuffixOracle returns a new oracle that encrypts inputs as described in challenge 11.

The oracle returns encrypt(pad(prefix || input || suffix)) under either AES-128-ECB or AES-128-CBC.

func NewECBPrefixSuffixOracle

func NewECBPrefixSuffixOracle(secret []byte) func([]byte) []byte

NewECBPrefixSuffixOracle returns an encryption oracle that behaves as described in challenge 14.

It returns AES-128-ECB(key, prefix || input || secret). The key and prefix are random and fixed.

func NewECBSuffixOracle

func NewECBSuffixOracle(secret []byte) func([]byte) []byte

NewECBSuffixOracle returns an oracle that encrypts inputs as described in challenge 12.

The oracle returns encrypt(pad(input || secret)).

func NewRepeatingKeyXORCipher

func NewRepeatingKeyXORCipher(key []byte) cipher.Stream

NewRepeatingKeyXORCipher returns a new repeating-key XOR cipher.

func NewSingleByteXORCipher

func NewSingleByteXORCipher(key byte) cipher.Stream

NewSingleByteXORCipher returns a new single-byte XOR cipher.

func PadPKCS7

func PadPKCS7(b []byte, n int) []byte

PadPKCS7 returns a new slice that concatenates b with PKCS #7 padding to guarantee block size n.

func RecoverECBSuffixOracleSecret

func RecoverECBSuffixOracleSecret(oracle func([]byte) []byte) []byte

RecoverECBSuffixOracleSecret takes an encryption oracle that behaves as described in challenge 12 and recovers the secret used.

func RecoverRepeatingKeyXORKey

func RecoverRepeatingKeyXORKey(ct []byte) []byte

RecoverRepeatingKeyXORKey returns the most likely key for a repeating-key XOR ciphertext.

It assumes the plaintext is English. It also assumes that the key size is between 2 and 40 bytes.

func RecoverRepeatingKeyXORKeySize

func RecoverRepeatingKeyXORKeySize(ct []byte, lo, hi int) int

RecoverRepeatingKeyXORKeySize returns the most likely key size for a repeating-key XOR ciphertext, within lo to hi inclusive.

It assumes that the plaintext is English.

TODO: Avoid panicking when a or b is large compared to len(ct).

func RecoverSingleByteXORKey

func RecoverSingleByteXORKey(ct []byte) byte

RecoverSingleByteXORKey returns the most likely key for a single-byte XOR ciphertext.

It assumes the plaintext is English.

func UnpadPKCS7

func UnpadPKCS7(b []byte) []byte

UnpadPKCS7 returns a subslice of b with PKCS #7 padding removed.

func XOR

func XOR(a, b []byte) []byte

XOR returns a xor b.

It panics if a and b have different lengths.

Types

type ProfileManager

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

ProfileManager manages profiles as described in challenge 13.

func NewProfileManager

func NewProfileManager() *ProfileManager

NewProfileManager returns a new profile manager.

func (ProfileManager) IsAdmin

func (p ProfileManager) IsAdmin(profile []byte) bool

IsAdmin returns true if the profile has admin permissions.

func (ProfileManager) NewUserProfile

func (p ProfileManager) NewUserProfile(email string) []byte

NewUserProfile returns a new profile with user permissions.

Jump to

Keyboard shortcuts

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