encoding

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jan 12, 2022 License: BSD-3-Clause Imports: 7 Imported by: 0

Documentation

Overview

Package encoding defines interfaces to be implemented by bijective, invertible functions. Implementing a common interface over the building blocks of a construction or cryptanalysis gives a simple way to compose, concatenate, and invert them.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func EquivalentBytes

func EquivalentBytes(a, b Byte) bool

EquivalentBytes returns true if two Byte encodings are identical and false if not.

func ProbablyEquivalentBlocks

func ProbablyEquivalentBlocks(a, b Block) bool

ProbablyEquivalentBlocks returns true if two Block encodings are probably equivalent and false if they're definitely not.

func ProbablyEquivalentDoubles

func ProbablyEquivalentDoubles(a, b Double) bool

ProbablyEquivalentDoubles returns true if two Double encodings are probably equivalent and false if they're definitely not.

func ProbablyEquivalentWords

func ProbablyEquivalentWords(a, b Word) bool

ProbablyEquivalentWords returns true if two Word encodings are probably equivalent and false if they're definitely not.

func SerializeByte

func SerializeByte(e Byte) []byte

SerializeByte serializes a Byte encoding to a byte slice.

func XOR

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

XOR xors the bytes in a and b. The destination is assumed to have enough space. Returns the number of bytes xor'd.

Types

type Block

type Block interface {
	Encode(i [16]byte) [16]byte
	Decode(i [16]byte) [16]byte
}

type BlockAdditive

type BlockAdditive [16]byte

BlockAdditive implements the Block interface over XORing with a fixed value.

func (BlockAdditive) Decode

func (ba BlockAdditive) Decode(in [16]byte) [16]byte

func (BlockAdditive) Encode

func (ba BlockAdditive) Encode(in [16]byte) [16]byte

type BlockAffine

type BlockAffine struct {
	BlockLinear
	BlockAdditive
}

BlockAffine implements the Block interface over an affine transformation (a linear transformation composed with an additive one).

func DecomposeBlockAffine

func DecomposeBlockAffine(in Block) (BlockAffine, bool)

DecomposeBlockAffine decomposes an opaque Block encoding into a BlockAffine encoding.

func NewBlockAffine

func NewBlockAffine(forwards matrix.Matrix, constant [16]byte) BlockAffine

NewBlockAffine constructs a new BlockAffine encoding from a matrix and a constant.

func (BlockAffine) Decode

func (ba BlockAffine) Decode(in [16]byte) [16]byte

func (BlockAffine) Encode

func (ba BlockAffine) Encode(in [16]byte) [16]byte

type BlockLinear

type BlockLinear struct {
	// Forwards is the matrix to multiply by in the forwards (encoding) direction.
	Forwards matrix.Matrix
	// Backwards is the matrix to multiply by in the backwards (decoding) direction. It should be the inverse of Forwards.
	Backwards matrix.Matrix
}

BlockLinear implements the Block interface over a 128x128 linear transformation.

func DecomposeBlockLinear

func DecomposeBlockLinear(in Block) (BlockLinear, bool)

DecomposeBlockLinear decomposes an opaque Block encoding into a BlockLinear encoding.

func NewBlockLinear

func NewBlockLinear(forwards matrix.Matrix) BlockLinear

NewBlockLinear constructs a new BlockLinear encoding from a given matrix.

func (BlockLinear) Decode

func (bl BlockLinear) Decode(in [16]byte) (out [16]byte)

func (BlockLinear) Encode

func (bl BlockLinear) Encode(in [16]byte) (out [16]byte)

type BlockTable

type BlockTable struct {
	In     Byte
	Out    Block
	Hidden table.Block
}

BlockTable implements the table.Block interface over another Block table with a Byte encoding on its input and a Block encoding on its output.

func (BlockTable) Get

func (bt BlockTable) Get(i byte) [16]byte

type Byte

type Byte interface {
	Encode(i byte) byte
	Decode(i byte) byte
}

func ParseByte

func ParseByte(serialized []byte) Byte

ParseByte parses a serialized Byte encoding.

type ByteAdditive

type ByteAdditive byte

ByteAdditive implements the Byte interface over XORing with a fixed value.

func (ByteAdditive) Decode

func (ba ByteAdditive) Decode(in byte) byte

func (ByteAdditive) Encode

func (ba ByteAdditive) Encode(in byte) byte

type ByteAffine

type ByteAffine struct {
	ByteLinear
	ByteAdditive
}

ByteAffine implements the Byte interface over an affine transformation (a linear transformation composed with an additive one).

func DecomposeByteAffine

func DecomposeByteAffine(in Byte) (ByteAffine, bool)

DecomposeByteAffine decomposes an opaque Byte encoding into a ByteAffine encoding.

func NewByteAffine

func NewByteAffine(forwards matrix.Matrix, constant byte) ByteAffine

NewByteAffine constructs a new ByteAffine encoding from a matrix and a constant.

func (ByteAffine) Decode

func (ba ByteAffine) Decode(in byte) byte

func (ByteAffine) Encode

func (ba ByteAffine) Encode(in byte) byte

type ByteLinear

type ByteLinear struct {
	// Forwards is the matrix to multiply by in the forwards (encoding) direction.
	Forwards matrix.Matrix
	// Backwards is the matrix to multiply by in the backwards (decoding) direction. It should be the inverse of Forwards.
	Backwards matrix.Matrix
}

ByteLinear implements the Byte interface over an 8x8 linear transformation.

func DecomposeByteLinear

func DecomposeByteLinear(in Byte) (ByteLinear, bool)

DecomposeByteLinear decomposes an opaque Byte encoding into a ByteLinear encoding.

func NewByteLinear

func NewByteLinear(forwards matrix.Matrix) ByteLinear

NewByteLinear constructs a new ByteLinear encoding from a given matrix.

func (ByteLinear) Decode

func (bl ByteLinear) Decode(in byte) byte

func (ByteLinear) Encode

func (bl ByteLinear) Encode(in byte) byte

type ByteMultiplication

type ByteMultiplication struct {
	// Forwards is the number to multiply by in the forwards (encoding) direction.
	Forwards number.ByteFieldElem
	// Backwards is the number to multiply by in the backwards (decoding) direction. It should be the inverse of Forwards.
	Backwards number.ByteFieldElem
}

ByteMultiplication implements the Byte interface over multiplication by an element of GF(2^8).

func NewByteMultiplication

func NewByteMultiplication(forwards number.ByteFieldElem) ByteMultiplication

NewByteMultiplication constructs a new ByteMultiplication encoding from a field element.

func (ByteMultiplication) Decode

func (bm ByteMultiplication) Decode(i byte) byte

func (ByteMultiplication) Encode

func (bm ByteMultiplication) Encode(i byte) byte

type ByteTable

type ByteTable struct {
	In     Byte
	Out    Byte
	Hidden table.Byte
}

ByteTable implements the table.Byte interface over another Byte table with Byte encodings on its input and output.

func (ByteTable) Get

func (bt ByteTable) Get(i byte) byte

type ComposedBlocks

type ComposedBlocks []Block

ComposedBlocks converts an array of Block encodings into one by chaining them. See ComposedBytes.

func (ComposedBlocks) Decode

func (cb ComposedBlocks) Decode(i [16]byte) (out [16]byte)

func (ComposedBlocks) Encode

func (cb ComposedBlocks) Encode(i [16]byte) (out [16]byte)

type ComposedBytes

type ComposedBytes []Byte

ComposedBytes converts an array of Byte encodings into one by chaining them. Functions are chained in REVERSE order than they would be in function composition notation.

Example:

f := ComposedBytes([]Byte{A, B, C}) // f(x) = C(B(A(x)))

func (ComposedBytes) Decode

func (cb ComposedBytes) Decode(i byte) byte

func (ComposedBytes) Encode

func (cb ComposedBytes) Encode(i byte) byte

type ComposedDoubles

type ComposedDoubles []Double

ComposedDoubles converts an array of Double encodings into one by chaining them. See ComposedBytes.

func (ComposedDoubles) Decode

func (cd ComposedDoubles) Decode(i [2]byte) (out [2]byte)

func (ComposedDoubles) Encode

func (cd ComposedDoubles) Encode(i [2]byte) (out [2]byte)

type ComposedWords

type ComposedWords []Word

ComposedWords converts an array of Word encodings into one by chaining them. See ComposedBytes.

func (ComposedWords) Decode

func (cw ComposedWords) Decode(i [4]byte) (out [4]byte)

func (ComposedWords) Encode

func (cw ComposedWords) Encode(i [4]byte) (out [4]byte)

type ConcatenatedBlock

type ConcatenatedBlock [16]Byte

ConcatenatedBlock builds a Block encoding by concatenating sixteen Byte encodings. The Byte encoding in position i is the one applied to position i of the input.

func DecomposeConcatenatedBlock

func DecomposeConcatenatedBlock(in Block) (out ConcatenatedBlock)

DecomposeConcatenatedBlock decomposes an opaque concatenated Block encoding into an explicit one.

func (ConcatenatedBlock) Decode

func (cb ConcatenatedBlock) Decode(i [16]byte) (out [16]byte)

func (ConcatenatedBlock) Encode

func (cb ConcatenatedBlock) Encode(i [16]byte) (out [16]byte)

type ConcatenatedByte

type ConcatenatedByte [2]Nibble

ConcatenatedByte builds a Byte encoding by concatenating two Nibble encodings. The Nibble encoding in position 0 is applied to the upper half of the byte and the one in position 1 is applied to the lower half.

func (ConcatenatedByte) Decode

func (cb ConcatenatedByte) Decode(i byte) byte

func (ConcatenatedByte) Encode

func (cb ConcatenatedByte) Encode(i byte) byte

type ConcatenatedDouble

type ConcatenatedDouble [2]Byte

ConcatenatedDouble builds a Double encoding by Concatenating two Byte encodings. The Byte encoding in position i is the one applied to position i of the input.

func (ConcatenatedDouble) Decode

func (cd ConcatenatedDouble) Decode(i [2]byte) [2]byte

func (ConcatenatedDouble) Encode

func (cd ConcatenatedDouble) Encode(i [2]byte) [2]byte

type ConcatenatedWord

type ConcatenatedWord [4]Byte

ConcatenatedWord builds a Word encoding by concatenating four Byte encodings. The Byte encoding in position i is the one applied to position i of the input.

func (ConcatenatedWord) Decode

func (cw ConcatenatedWord) Decode(i [4]byte) [4]byte

func (ConcatenatedWord) Encode

func (cw ConcatenatedWord) Encode(i [4]byte) [4]byte

type Double

type Double interface {
	Encode(i [2]byte) [2]byte
	Decode(i [2]byte) [2]byte
}

type DoubleAdditive

type DoubleAdditive [2]byte

DoubleAdditive implements the Double interface over XORing with a fixed value.

func (DoubleAdditive) Decode

func (da DoubleAdditive) Decode(in [2]byte) [2]byte

func (DoubleAdditive) Encode

func (da DoubleAdditive) Encode(in [2]byte) [2]byte

type DoubleAffine

type DoubleAffine struct {
	DoubleLinear
	DoubleAdditive
}

DoubleAffine implements the Double interface over an affine transformation (a linear transformation composed with an additive one).

func DecomposeDoubleAffine

func DecomposeDoubleAffine(in Double) (DoubleAffine, bool)

DecomposeDoubleAffine decomposes an opaque Double encoding into a DoubleAffine encoding.

func NewDoubleAffine

func NewDoubleAffine(forwards matrix.Matrix, constant [2]byte) DoubleAffine

NewDoubleAffine constructs a new DoubleAffine encoding from a matrix and a constant.

func (DoubleAffine) Decode

func (da DoubleAffine) Decode(in [2]byte) [2]byte

func (DoubleAffine) Encode

func (da DoubleAffine) Encode(in [2]byte) [2]byte

type DoubleLinear

type DoubleLinear struct {
	// Forwards is the matrix to multiply by in the forwards (encoding) direction.
	Forwards matrix.Matrix
	// Backwards is the matrix to multiply by in the backwards (decoding) direction. It should be the inverse of Forwards.
	Backwards matrix.Matrix
}

DoubleLinear implements the Double interface over a 16x16 linear transformation.

func DecomposeDoubleLinear

func DecomposeDoubleLinear(in Double) (DoubleLinear, bool)

DecomposeDoubleLinear decomposes an opaque Double encoding into a DoubleLinear encoding.

func NewDoubleLinear

func NewDoubleLinear(forwards matrix.Matrix) DoubleLinear

NewDoubleLinear constructs a new DoubleLinear encoding from a given matrix.

func (DoubleLinear) Decode

func (dl DoubleLinear) Decode(in [2]byte) (out [2]byte)

func (DoubleLinear) Encode

func (dl DoubleLinear) Encode(in [2]byte) (out [2]byte)

type DoubleToByteTable

type DoubleToByteTable struct {
	In     Double
	Out    Byte
	Hidden table.DoubleToByte
}

DoubleToByteTable implements the table.DoubleToByte interface over another DoubleToByte table with a Double encoding on its input and a Byte encoding on its output.

func (DoubleToByteTable) Get

func (dtbt DoubleToByteTable) Get(i [2]byte) byte

type DoubleToWordTable

type DoubleToWordTable struct {
	In     Double
	Out    Word
	Hidden table.DoubleToWord
}

DoubleToWordTable implements the table.DoubleToWord interface over another DoubleToWord table with a Double encoding on its input and a Word encoding on its output.

func (DoubleToWordTable) Get

func (dtwt DoubleToWordTable) Get(i [2]byte) [4]byte

type IdentityBlock

type IdentityBlock struct{}

IdentityBlock is the identity operation on blocks.

func (IdentityBlock) Decode

func (ib IdentityBlock) Decode(i [16]byte) (out [16]byte)

func (IdentityBlock) Encode

func (ib IdentityBlock) Encode(i [16]byte) (out [16]byte)

type IdentityByte

type IdentityByte struct{}

IdentityByte is the identity operation on bytes. It is used in place of an IdentityNibble encoding.

func (IdentityByte) Decode

func (ib IdentityByte) Decode(i byte) byte

func (IdentityByte) Encode

func (ib IdentityByte) Encode(i byte) byte

type IdentityDouble

type IdentityDouble struct{}

IdentityDouble is the identity operation on doubles.

func (IdentityDouble) Decode

func (id IdentityDouble) Decode(i [2]byte) [2]byte

func (IdentityDouble) Encode

func (id IdentityDouble) Encode(i [2]byte) [2]byte

type IdentityWord

type IdentityWord struct{}

IdentityWord is the identity operation on words.

func (IdentityWord) Decode

func (iw IdentityWord) Decode(i [4]byte) (out [4]byte)

func (IdentityWord) Encode

func (iw IdentityWord) Encode(i [4]byte) (out [4]byte)

type InverseBlock

type InverseBlock struct{ Block }

InverseBlock swaps the Encode and Decode methods of a Block encoding.

func (InverseBlock) Decode

func (ib InverseBlock) Decode(i [16]byte) [16]byte

func (InverseBlock) Encode

func (ib InverseBlock) Encode(i [16]byte) [16]byte

type InverseByte

type InverseByte struct{ Byte }

InverseByte swaps the Encode and Decode methods of a Byte encoding.

func (InverseByte) Decode

func (ib InverseByte) Decode(i byte) byte

func (InverseByte) Encode

func (ib InverseByte) Encode(i byte) byte

type InverseDouble

type InverseDouble struct{ Double }

InverseDouble swaps the Encode and Decode methods of a Double encoding.

func (InverseDouble) Decode

func (id InverseDouble) Decode(i [2]byte) [2]byte

func (InverseDouble) Encode

func (id InverseDouble) Encode(i [2]byte) [2]byte

type InverseWord

type InverseWord struct{ Word }

InverseWord swaps the Encode and Decode methods of a Word encoding.

func (InverseWord) Decode

func (iw InverseWord) Decode(i [4]byte) [4]byte

func (InverseWord) Encode

func (iw InverseWord) Encode(i [4]byte) [4]byte

type Nibble

type Nibble interface {
	Encode(i byte) byte // Encode(i nibble) nibble
	Decode(i byte) byte // Decode(i nibble) nibble
}

Nibble is the same interface as Byte. A function implementing Nibble shouldn't accept inputs or give outputs over 16.

type NibbleTable

type NibbleTable struct {
	In     Byte
	Out    Nibble
	Hidden table.Nibble
}

NibbleTable implements the table.Nibble interface over another Nibble table with a Byte encoding on its input and a Nibble encoding on its output.

func (NibbleTable) Get

func (nt NibbleTable) Get(i byte) byte

type SBox

type SBox struct {
	EncKey, DecKey [256]byte
}

SBox implements a random 8-bit bijection.

func GenerateSBox

func GenerateSBox(reader io.Reader) (s SBox)

GenerateSBox generates a random 8-bit bijection using the random source random (for example, crypto/rand.Reader).

func (SBox) Decode

func (s SBox) Decode(i byte) byte

func (SBox) Encode

func (s SBox) Encode(i byte) byte

func (SBox) Permutation

func (s SBox) Permutation() (out []int)

Permutation returns the SBox's permutation.

type Shuffle

type Shuffle struct {
	EncKey, DecKey [16]byte
}

Shuffle implements a random 4-bit bijection.

func GenerateShuffle

func GenerateShuffle(reader io.Reader) (s Shuffle)

GenerateShuffle generates a random 4-bit bijection using the random source random (for example, crypto/rand.Reader).

func (Shuffle) Decode

func (s Shuffle) Decode(i byte) byte

func (Shuffle) Encode

func (s Shuffle) Encode(i byte) byte

func (Shuffle) Permutation

func (s Shuffle) Permutation() (out []int)

Permutation returns the shuffle's permutation.

type Word

type Word interface {
	Encode(i [4]byte) [4]byte
	Decode(i [4]byte) [4]byte
}

type WordAdditive

type WordAdditive [4]byte

WordAdditive implements the Word interface over XORing with a fixed value.

func (WordAdditive) Decode

func (wa WordAdditive) Decode(in [4]byte) [4]byte

func (WordAdditive) Encode

func (wa WordAdditive) Encode(in [4]byte) [4]byte

type WordAffine

type WordAffine struct {
	WordLinear
	WordAdditive
}

WordAffine implements the Word interface over an affine transformation (a linear transformation composed with an additive one).

func DecomposeWordAffine

func DecomposeWordAffine(in Word) (WordAffine, bool)

DecomposeWordAffine decomposes an opaque Word encoding into a WordAffine encoding.

func NewWordAffine

func NewWordAffine(forwards matrix.Matrix, constant [4]byte) WordAffine

NewWordAffine constructs a new WordAffine encoding from a matrix and a constant.

func (WordAffine) Decode

func (wa WordAffine) Decode(in [4]byte) [4]byte

func (WordAffine) Encode

func (wa WordAffine) Encode(in [4]byte) [4]byte

type WordLinear

type WordLinear struct {
	// Forwards is the matrix to multiply by in the forwards (encoding) direction.
	Forwards matrix.Matrix
	// Backwards is the matrix to multiply by in the backwards (decoding) direction. It should be the inverse of Forwards.
	Backwards matrix.Matrix
}

WordLinear implements the Word interface over a 32x32 linear transformation.

func DecomposeWordLinear

func DecomposeWordLinear(in Word) (WordLinear, bool)

DecomposeWordLinear decomposes an opaque Word encoding into a WordLinear encoding.

func NewWordLinear

func NewWordLinear(forwards matrix.Matrix) WordLinear

NewWordLinear constructs a new WordLinear encoding from a given matrix.

func (WordLinear) Decode

func (wl WordLinear) Decode(in [4]byte) (out [4]byte)

func (WordLinear) Encode

func (wl WordLinear) Encode(in [4]byte) (out [4]byte)

type WordTable

type WordTable struct {
	In     Byte
	Out    Word
	Hidden table.Word
}

WordTable implements the table.Word interface over another Word table with a Byte encoding on its input and a Word encoding on its output.

func (WordTable) Get

func (wt WordTable) Get(i byte) [4]byte

Jump to

Keyboard shortcuts

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