Support Functions

securefile.aes_algorithm.AES

securefile.aes_algorithm.AES.Cipher(self, expandedKey, data)

At the start of the Cipher, the input is copied to the State Matrix. After an initial Round Key addition, the State Matrix is transformed by implementing a round function 10, 12, or 14 times (depending on the key length), with the final round differing slightly from the first Nr -1 rounds. The final State Matrix is then copied as the output.

Parameters:
  • expandedKey (list) – The expanded key schedule
  • data (str) – Hex string to encrypt
Returns:

Encrypted data as a Hex string

securefile.aes_algorithm.AES.InvCipher(self, expandedKey, data)
securefile.aes_algorithm.AES.ExpandKey(self, key)

Takes the Cipher Key and performs a Key Expansion routine to generate a key schedule thus generating a total of Nb (Nr + 1) words.

Parameters:key (str) – 128, 192, 256 bit Cipher Key
Returns:Expanded Cipher Keys
securefile.aes_algorithm.AES.MixColumns(self, state, isInv)

Operates on the State column-by-column, treating each column as a four-term polynomial. The columns are considered as polynomials over GF(2^8) and multiplied modulo x^4 + 1 with a fixed polynomial a(x).

Parameters:
  • state – State Matrix input
  • isInv – Encrypt or decrypt mode
Returns:

securefile.aes_algorithm.AES.AddRoundKey(state, key)

Round Key is added to the State using an XOR operation.

Parameters:
  • state (list) – State Matrix
  • key (list) – Round Key
Returns:

Hex values of XOR operation

securefile.aes_algorithm.AES.RevertStateMatrix(state)

Reverts State Matrix format as str

Parameters:state (list) – Final State Matrix
Returns:Reverted State Matrix
securefile.aes_algorithm.AES.RotWord(word)

Takes a word [a0, a1, a2, a3] as input and perform a cyclic permutation that returns the word [a1, a2, a3, a0].

Parameters:word (str) – Row within State Matrix
Returns:Circular byte left shift
securefile.aes_algorithm.AES.ShiftRows(self, state, isInv)

Changes the State by cyclically shifting the last three rows of the State by different offsets.

Parameters:
  • state (list) – State Matrix
  • isInv – Encrypt or Decrypt
Returns:

Shifted state by offsets [0, 1, 2, 3]

securefile.aes_algorithm.AES.StateMatrix(state)

Formats a State Matrix str to a properly formatted list.

Parameters:state (str) – String State Matrix
Returns:Formatted State Matrix
securefile.aes_algorithm.AES.SubBytes(self, state, isInv)

Transforms the State Matrix using a nonlinear byte S-box that operates on each of the State bytes independently.

Parameters:
  • state – State matrix input
  • isInv – Encrypt or decrypt mode
Returns:

Byte substitution from the state matrix

securefile.aes_algorithm.AES.SubWord(self, byte)

Key Expansion routine that takes a four-byte input word and applies an S-box substitution.

Parameters:byte (int) – Output from the circular byte left shift
Returns:Substituted bytes through sbox
securefile.aes_algorithm.AES.cbc(self, data, expanded_key, isInv)

CBC mode: In CBC mode, each block of plaintext is XORed with the previous ciphertext block before being encrypted.

Denoted as:
Encryption: Ci = Ek(Pi xor C(i-1)) and C0 = IV Decryption: Pi = Dk(Ci) xor C(i-1) and C0 = IV
Parameters:
  • data – Data to be encrypted (type defined by input type)
  • expanded_key – The AES expanded key set
  • isInv
Returns:

Data as string or binary data (defined by output type)

securefile.aes_algorithm.AES.ecb(self, data, expanded_key, isInv)

ECB mode: The simplest of the encrypt modes is the Electronic Codebook (ECB) mode. The message is divided into blocks, and each block is encrypted separately.

Parameters:
  • isInv
  • data – Data to be encrypted (type defined by input type)
  • expanded_key – The AES expanded key set
Returns:

Data as string or binary data (defined by output type)

securefile.aes_algorithm.AES.galois(a, b)

Galois multiplication of 8 bit characters a and b

Parameters:
  • a – State Matrix col or row
  • b – Fixed number
Returns:

Galois field GF(2^8)

securefile.aes_algorithm.AES.key_handler(self, key, isInv)

Gets the key length and sets Nb, Nk, Nr accordingly.

Parameters:
  • key (str) – 128, 192, 256 bit Cipher Key
  • isInv – Encrypt or decrypt mode
Returns:

Expanded Cipher Keys

securefile.aes_algorithm.AES.pad(data, block=16)

Padding method for data

Parameters:
  • data – Data to pad
  • block (int) – Block size
Returns:

Padded data

securefile.aes_algorithm.AES.unblock(data, size=16)

Unblock binary data

Parameters:
  • data (bytes) – Binary data to split into blocks
  • size (int) – Block size
Returns:

Blocked binary data

securefile.aes_algorithm.AES.unpad(data)

Un-Padding for data

Parameters:data – Data to be un-padded
Returns:Data with removed padding
securefile.aes_algorithm.AES.xor(first, last)

Xor method for CBC usage

Parameters:
  • first – first encrypted block
  • last – last encrypted block
Returns:

Xor output of two blocks