cryptolib

package
v0.0.0-...-ec3246f Latest Latest
Warning

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

Go to latest
Published: Mar 6, 2020 License: MIT Imports: 5 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ASCIIAbnormalControlCharacters map[byte]bool = map[byte]bool{

	1: true,
	2: true,
	3: true,
	4: true,
	5: true,
	6: true,
	7: true,
	8: true,

	11: true,
	12: true,

	14:  true,
	15:  true,
	16:  true,
	17:  true,
	18:  true,
	19:  true,
	20:  true,
	21:  true,
	22:  true,
	23:  true,
	24:  true,
	25:  true,
	26:  true,
	27:  true,
	28:  true,
	29:  true,
	30:  true,
	31:  true,
	127: true,
}

A set of ASCII codepoints that should trigger rejection of a string

View Source
var EnglishAlphabetFrequencies map[byte]float64 = map[byte]float64{

	' ': 0.16000,
	'e': 0.12702,
	't': 0.09056,
	'a': 0.08167,
	'o': 0.07507,
	'i': 0.06966,
	'n': 0.06749,
	's': 0.06327,
	'h': 0.06094,
	'r': 0.05987,
	'd': 0.04253,
	'l': 0.04025,
	'c': 0.02782,
	'u': 0.02758,
	'm': 0.02406,
	'w': 0.02360,
	'f': 0.02228,
	'g': 0.02015,
	'y': 0.01974,
	'p': 0.01929,
	'b': 0.01492,
	'v': 0.00978,
	'k': 0.00772,
	'j': 0.00153,
	'x': 0.00150,
	'q': 0.00095,
	'z': 0.00074,
}

Frequencies of each letter in the English alphabet. There's a neat little citation nest here but I pulled these from Wikipedia https://en.wikipedia.org/wiki/Letter_frequency

Functions

func AverageBlockHammingDist

func AverageBlockHammingDist(text []byte, blockSize, blockCount uint) float64

Normalized hamming distance over a number of equally sized blocks

func CrackSingleByteXor

func CrackSingleByteXor(cyphertext []byte,
	scoreFunc func([]byte) (float64, error)) (key byte, err error)

* Find the single byte key a string has been XORed against.

Given a scoring function to determine how likely a key is to be correct and a cyphertext that is assumed to have been XORed against a single byte, finds the most likely key.

Inputs:

cyphertext []byte: The cyphertext to decrypt.
scoreFunc func([]byte) (float64, error): A scoring function where lower
    numers are considered better scores. First return value should be the
    score, second should be an optional error which, if present, indicates
    the score should be rejected.

Outputs:

key byte: The key the string has probably been XORed against
err error: Returns an error if no possible keys were found

func DecodeBase64

func DecodeBase64(b64Str string) []byte

func DecodeHex

func DecodeHex(hexStr string) []byte
Bytes to Pretty Prints Manipulation Functions

These functions handle conversions between raw byte arrays and printed outputs. Program internals should always handle data as raw bytes, and use other formats only for accepting and returning values. in_ functions convert from a pretty printed format into a byte array. out_ functions convert byte arrays into other formats.

These will likely be early candidates for putting in their own library. Or just removing entirely, since they pretty much just wrap the Go encoding library.

func DecryptAesCbc

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

* Decrypt text using AES in CBC mode.

Inputs:

ciphertext []byte: The text to encrypt.
key []byte: The key for AES. Must be 16, 24, or 32 bytes
iv []byte: The initialization vector for CBC mode. Must be 16 bytes.

Outputs:

[]byte: The ciphertext produced by encrypting through AES-CBC
error: Error if key or text has an invalid size

func DecryptAesEcb

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

* Decrypt text using AES in ECB mode.

This function assumes properly padded inputs and will not handle padding.

Again, IF YOU ARE USING THIS TO HANDLE REAL THINGS THOSE THINGS ARE BROKEN.

Inputs:

ciphertext []byte: The text to decrypt. Must be a multiple of 16 bytes.
key []byte: The key for AES. Must be 16, 24, or 32 bytes

Outputs:

[]byte: The plaintext produced by decrypting through AES-ECB
error: Error if key or text has an invalid size

func DecryptXor

func DecryptXor(cyphertext []byte, key []byte) []byte

* DecryptXor applies a simple bitwise XOR to a cyphertext.

Note that despite the name, this could just as easily be used to encrypt XOR. Reapplying it on the same key will produce the original input, or, in code:

DecryptXor(DecryptXor(input, key), key) == input

Inputs:

cyphertext []byte: The cyphertext to decrypt.
key []byte: The key to XOR the cyphertext against. May be shorter than the
    cyphertext, in which case, repeating key XOR will be assumed.

Outputs:

out []byte: The result of XORing the cyphertext against the key.

func DetectEcbMode

func DetectEcbMode(ciphertext []byte, blocksize int) bool

* Detect if a given ciphertext was encrypted in ECB mode.

Looks for duplicate blocks to detect the presence of ECB mode. This is easily generalizable to _any_ ECB block cipher by just allowing a variable block size.

func DetermineEcbBlocksize

func DetermineEcbBlocksize(cipherFunc func([]byte) []byte, maxGuess int) (int, error)

* Determine the blocksize of a cipher function presumed to be using an ECB cipher

func EncodeBase64

func EncodeBase64(raw []byte) string

func EncodeHex

func EncodeHex(raw []byte) string

func EncryptAesCbc

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

* Encrypt text using AES in CBC mode.

Text will be padded with PKCS#7 padding before being encrypted.

CBC (Cipher Block Chaining) is a block cipher mode where each block of plaintext is XORed against the preceding ciphertext block before being encrypted. The first block is XORed against an initialization vector.

Inputs:

plaintext []byte: The text to encrypt.
key []byte: The key for AES. Must be 16, 24, or 32 bytes
iv []byte: The initialization vector for CBC mode. Must be 16 bytes.

Outputs:

[]byte: The ciphertext produced by encrypting through AES-CBC
error: Error if key or text has an invalid size

func EncryptAesEcb

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

* Encrypt text using AES in ECB mode.

Text will be padded with PKCS#7 padding before being encrypted

ECB (Electronic Codebook) is an INSECURE method of encrypting a block cipher. Because it just repeats the same key without modification, it's easy to detect, and it leaks information about the encrypted data. If you're using this method, it should be for some security research or similar project (like cryptopals!). There is a reason the go dev team decided not to include ECB mode in the libs.

IF YOU ARE USING THIS TO ENCRYPT REAL THINGS THOSE THINGS ARE BROKEN.

Inputs:

plaintext []byte: The text to encrypt.
key []byte: The key for AES. Must be 16, 24, or 32 bytes

Outputs:

[]byte: The ciphertext produced by encrypting through AES-ECB
error: Error if key or text has an invalid size

func FrequenciesDifferenceEnglishASCIIScore

func FrequenciesDifferenceEnglishASCIIScore(text []byte) (float64, error)

* Score candidate text based on how close its frequencies map to expectations.

Basically, build a map of all the letters we find, compute how frequent those letters were in the candidate text, compare it against the cannonical frequency map. We'll include characters that aren't in the map in the denominator but not the numerator unless those characters are spaces; this is a hack, and I should really build a frequency map that includes spaces and punctuation.

Since this is a difference function, lower values are better; 0.0 indicates a perfect match with expected frequencies (highly unlikely). This is hacky and annoying on two levels: the other scoring function is higher-better, and -1 is still a special case for a rejected candidate text, since negative outputs aren't possible.

The naïve function is so clearly worse than this that I might just delete it, though.

func HammingDist

func HammingDist(a []byte, b []byte) int

func NaiveEnglishASCIIScore

func NaiveEnglishASCIIScore(text []byte) (float64, error)

* Score a candidate text based only on each individual character.

This is a pretty naive way to do scoring; we assume the data is in ASCII, add points based on letter frequency, and remove a large number of points for characters that should never appear (like control characters).

There are numerous flaws in this method; for instance, the string "EEEEEE" would score higher than "Friend", since "E" is a more frequent letter and therefore worth more points outright.

Returns a float score which is the negative sum of the frequencies of the encountered characters. Lower scores indicate a more probable plaintext candidate.

func NormalizedHammingDist

func NormalizedHammingDist(a []byte, b []byte) float64

Hamming distance divided by bytesize

func PKCS7PadMessage

func PKCS7PadMessage(message []byte, keysize uint) []byte

* Pad a message to be block encrypted with the method defined in PKCS#7

RFC describing algorithm: https://tools.ietf.org/html/rfc2315#section-10.3

To summarize, where a message is expected to be a multiple of some bytes, find the number of additional bytes of padding that are needed, and pad the message with that many bytes where each byte of padding contains the number of bytes of padding being inserted and all bytes are inserted at the end.

Types

This section is empty.

Jump to

Keyboard shortcuts

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