ikmachine

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: May 1, 2025 License: Apache-2.0 Imports: 8 Imported by: 1

README

ikmachine

Package ikmachine implements an pseudo-random generator using rotors and permutation tables. This generator has a very long period of 8,748,598,929,835,975,127,667,306,878,568,975 and is used to implement an infinite key (one-time pad) encryption system. I know that a period of 8.748598929975X1033 is not infinite, but as long as the key length is equal to length of the plain text to be encrypted it meets the definition of a one time pad.

This pseudo-random generator is based on the article in Dr. Dobbs Journal Volume 9, Number 94, 1984 titled An Infinite Key Encryption System by John A. Thomas and Joan Thersites.

The generator consists of 6 predefined rotors and 2 predefined permutation tables that are used to create the working rotors and permutation tables based on a user supplied passphrase. Once the working rotors and permutation tables are created, they are used to encrypt the plain text 32 bytes at a time. While each message or file can be encrypted using a different passphrase, that would be wasteful of the available key length. By keeping track of the number of 32 byte blocks that were encrypted, the generator can be initialized to start where if left off while encrypting the last message for file so that the passphrase can be reused for multiple encryption sessions. Please reference the ikmachine user documentation for details on using ikmachine.

Documentation

Overview

Package ikmachine implements an encryption system based using rotors and transposition to emulate infinite key (one-time pad) encryption.

This is an encryption system based on the article in Dr. Dobbs Journal Volume 9, Number 94, 1984 titled An Infinite Key Encryption System by John A. Thomas and Joan Thersites.

Index

Constants

View Source
const (
	BitsPerByte      int  = 8
	CipherBlockSize  int  = 256
	CipherBlockBytes int  = CipherBlockSize / BitsPerByte
	Encryption       rune = 'E'
	Decryption       rune = 'D'
)

Define constants needed for ikmachine

Variables

View Source
var (
	BigZero = big.NewInt(0)
	BigOne  = big.NewInt(1)
)

Functions

func N

func N[Int intType](n Int, rnd *Rand) Int

N returns a pseudo-random number in the half-open interval [0,n) from the given random number generator. The type parameter Int can be any integer type. It panics if n <= 0.

func Shuffle

func Shuffle[S ~[]E, E any](s S, rnd *Rand) S

Shuffle pseudo-randomizes the order of elements of the slice s using the given random number generator.

Types

type CipherBlock

type CipherBlock []byte

CipherBlock is the data processed by the rotors and permutators. It consists of the length in bytes to process and the (32 bytes of) data to process.

func (CipherBlock) String

func (cblk CipherBlock) String() string

String formats a string representing the CipherBlock.

type IkMachine

type IkMachine struct {
	Input  chan CipherBlock
	Output chan CipherBlock
	// contains filtered or unexported fields
}

IkMachine defines the machine used to encrypt or decrypt the plaintext. It consists of rotors and permutators that are connected using channels to pass the data (32 bytes at a time) being encrypted/decrypted between the rotors and permutators.

func (*IkMachine) ApplyKey

func (ikm *IkMachine) ApplyKey(machineType rune, key []byte) *IkMachine

ApplyKey will update the IkMachine's rotors, permutators, and counter file key based on the given secret key. The rotors and permutators are updated in place 32 bytes at a time causing the partially update rotor and permutators to be used during the update process. This should increase the difficulty to discover the working rotors and permutators of the encryption machine.

func (*IkMachine) Index

func (ikm *IkMachine) Index(idx ...*big.Int) (newIdx *big.Int)

setIndex will set the index of the encryption/decryption machine (the point where the keystream will begin) by setting the index of the rotors, permutators, and counter of the ikmachine.

func (*IkMachine) InitializeProformaEngine

func (ikm *IkMachine) InitializeProformaEngine() *IkMachine

Initialize the proforma encryption engine. Note that the rotors and permutators of the ikmachine are copies of the proforma rotors and permutators so that they are not changed during the process of initialization and encryption/decryption. This is to allow the test to work when running the package or file tests.

func (*IkMachine) MaximalStates

func (ikm *IkMachine) MaximalStates() *big.Int

MaximalStates returns the maximum number of states that the encryption/ decription machine can be in before repeating the same random sequence.

func (*IkMachine) StopIkMachine

func (ikm *IkMachine) StopIkMachine()

StopIkMachine shuts down the IkMachine freeing up the resouces used.

func (*IkMachine) String

func (ikm *IkMachine) String() string

String will create a string representation of the IkMachine.

type Rand

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

func (*Rand) Int15n

func (rnd *Rand) Int15n(max int16) int16

Int15n returns, as an int16, a non-negative pseudo-random number in the half-open interval [0,n). It panics if n <= 0.

func (*Rand) Int31n

func (rnd *Rand) Int31n(max int32) int32

Int31n returns, as an int32, a non-negative pseudo-random number in the half-open interval [0,n). It panics if n <= 0.

func (*Rand) Int63n

func (rnd *Rand) Int63n(max int64) int64

Int63n returns, as an int64, a non-negative pseudo-random number in the half-open interval [0,n). It panics if n <= 0.

func (*Rand) Intn

func (rnd *Rand) Intn(max int) int

Intn returns, as an int, a non-negative pseudo-random number in the half-open interval [0,n)]. It panics if n <= 0.

func (*Rand) New

func (rnd *Rand) New(src *IkMachine) *Rand

New returns a Rand object.

func (*Rand) Perm

func (rnd *Rand) Perm(n int) []int

Perm returns, as a slice of n ints, a pseudo-random permutation of the integers in the half-open interval [0,n).

func (*Rand) Read

func (rnd *Rand) Read(p []byte) (n int, err error)

Read generates len(p) random bytes and writes them into p. It returns number of bytes readd and a possible error.

func (*Rand) StopRand

func (rnd *Rand) StopRand()

StopRand will stop the random number generator. It is and error to call any function of the Rand object after calling StopRand.

func (*Rand) Uint64

func (rnd *Rand) Uint64() uint64

Uint64 returns, as an uint64, a pseudo-random 64 bit number.

func (*Rand) Uint64n

func (rnd *Rand) Uint64n(max uint64) uint64

Uint63n returns, as an uint64, a unsigned 64 bit pseudo-random number in the half-open interval [0, max).

Jump to

Keyboard shortcuts

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