aes

package module
v0.0.0-...-1120a76 Latest Latest
Warning

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

Go to latest
Published: May 8, 2023 License: MIT Imports: 3 Imported by: 0

README

AES

Go Reference

This repo contains an implementation of the Rijndael encryption algorithm as described in the NIST FIPS 197 AES paper.

Although the public API of this package adheres to common Go patterns, the internals strive to closely implement the details of the FIPS paper, so you should be able to easily use this package and the paper alongside one another.

This package aims to be clear and easy to read, rather than efficient, and may contain bugs. Do not use this package for real cryptography.

Documentation

Overview

Example
package main

import (
	"log"

	"github.com/ny0m/aes"
)

func main() {
	// Generate a key from a collection of bytes.
	// For AES, key are either 16, 24, or 32 bytes long.
	// Hopefully it's easy to remember.
	key := aes.NewKey([]byte("ABSENTMINDEDNESS"))

	// Create a cipher with the key.
	// This can be used to encrypt messages.
	c := aes.NewCipher(key)

	// Create a 128-bit block from a message that we'd like to send.
	block := aes.NewBlock([]byte("a secret message"))

	// Finally, use the cipher to encrypt the block.
	out := c.Encrypt(block)

	// Et voila!
	log.Println(out)
}

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func DotProduct

func DotProduct(a, b matrix.Vector) byte

func Exp2

func Exp2(i int) int

func Mod

func Mod(dividend, divisor int) int

Mod returns the remainder of the given arguments using division for the field GF(2⁸), as defined in the AES128 paper, FIPS-197 Section 4.2.

func Multiply

func Multiply(a, b byte) byte

func Xtime

func Xtime(a byte) byte

Types

type Block

type Block [16]byte

Block is just a byte array. AES is a 128-bit symmetric block cipher, which means that it takes 128 bits as input, and returns 128 bits output, irrespective of key size.

func NewBlock

func NewBlock(bytes []byte) Block

NewBlock returns a block that contains the given bytes, padded if len(bytes) < 16.

func (Block) String

func (b Block) String() string

String returns a hexadecimal representation of each byte in the block.

type Cipher

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

Cipher consists of a parsed key and its derived schedule. Depending on key size, will perform a different number of rounds during encryption and decryption.

func NewCipher

func NewCipher(key Key) Cipher

func (Cipher) Decrypt

func (c Cipher) Decrypt(block Block) Block

Decrypt is an implementation of the InvCipher function. It's effectively the inverse of the Encrypt function; the steps are applied in reverse order. See FIPS-197 Section 5.3.

func (Cipher) Encrypt

func (c Cipher) Encrypt(block Block) Block

Encrypt implements the AES flavour of the Rijndael algo. See FIPS-197 Section 5.1.

type Key

type Key []Word

Key is a group of 32-bit words that is used to generate a key schedule, which is in turn used to encrypt the state during successive rounds.

type Word

type Word = uint32

Word is an array of 4 bytes represented as a single uint32.

func NewKey

func NewKey(bytes []byte) []Word

NewKey returns

func NewWord

func NewWord(bytes []byte) Word

NewWord converts a byte slice of length 4 to a 32-bit Word.

func Rcon

func Rcon(round int) Word

Rcon returns the round constant, which is a 4-bit polynomial represented as a power of two raised by the round number, mod poly.

The result is shifted three bytes to the left, since these constants are always of the form x³.

func RotateWord

func RotateWord(w Word) Word

RotateWord moves the most significant 8 bits of a word to the least significant.

func SubstituteWord

func SubstituteWord(w Word) Word

SubstituteWord applies the substitution algorithm from FIPS-197 Section 5.2.

func Words

func Words(bytes []byte) []Word

Words returns a slice of 32-bit words from a given byte slice. Panics if the byte slice is not a multiple of 4.

Directories

Path Synopsis
cmd
aes command
Package matrix contains data structures to make reasoning through the AES paper a bit simpler on a high level, at the cost of some efficiency.
Package matrix contains data structures to make reasoning through the AES paper a bit simpler on a high level, at the cost of some efficiency.

Jump to

Keyboard shortcuts

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