# eric5.Utilities.crypto.py3AES

Module implementing classes for encryption according Advanced Encryption Standard.

### Global Attributes

None

### Classes

AES | Class implementing the Advanced Encryption Standard algorithm.
AESModeOfOperation | Class implementing the different AES mode of operations.

### Functions

append_PKCS7_padding | Function to pad the given data to a multiple of 16-bytes by PKCS7 padding.
decryptData | Module function to decrypt the given data with the given key.
encryptData | Module function to encrypt the given data with the given key.
strip_PKCS7_padding | Function to strip off PKCS7 padding.

## AES

Class implementing the Advanced Encryption Standard algorithm.

### Derived from

object

### Class Attributes

KeySize
Rcon
rsbox
sbox

### Class Methods

None

### Methods

__addRoundKey | Private method to add (XORs) the round key to the state.
__aes_invMain | Private method to do the inverse AES encryption for one round.
__aes_invRound | Private method to apply the 4 operations of the inverse round in sequence.
__aes_main | Private method to do the AES encryption for one round.
__aes_round | Private method to apply the 4 operations of the forward round in sequence.
__core | Private method performing the key schedule core operation.
__createRoundKey | Private method to create a round key.
__expandKey | Private method performing Rijndael's key expansion.
__galois_multiplication | Private method to perform a Galois multiplication of 8 bit characters a and b.
__getRconValue | Private method to retrieve a given Rcon value.
__getSBoxInvert | Private method to retrieve a given Inverted S-Box value.
__getSBoxValue | Private method to retrieve a given S-Box value.
__mixColumn | Private method to perform a galois multiplication of 1 column the 4x4 matrix.
__mixColumns | Private method to perform a galois multiplication of the 4x4 matrix.
__rotate | Private method performing Rijndael's key schedule rotate operation.
__shiftRow | Private method to shift the bytes of a row to the left.
__shiftRows | Private method to iterate over the 4 rows and call __shiftRow() with that row.
__subBytes | Private method to substitute all the values from the state with the value in the SBox using the state value as index for the SBox.
decrypt | Public method to decrypt a 128 bit input block against the given key of size specified.
encrypt | Public method to encrypt a 128 bit input block against the given key of size specified.

### Static Methods

None

#### AES.__addRoundKey

__addRoundKey(state, roundKey)

Private method to add (XORs) the round key to the state.

state
  state to be changed (bytearray)
roundKey
  key to be used for the modification (bytearray)

Returns:
  modified state (bytearray)

#### AES.__aes_invMain

__aes_invMain(state, expandedKey, nbrRounds)

Private method to do the inverse AES encryption for one round.

Perform the initial operations, the standard round, and the final operations of the inverse AES, creating a round key for each round.

state
  state to be worked on (bytearray)
expandedKey
  expanded key to be used (bytearray)
nbrRounds
  number of rounds to be done (integer)

Returns:
  modified state (bytearray)

#### AES.__aes_invRound

__aes_invRound(state, roundKey)

Private method to apply the 4 operations of the inverse round in sequence.

state
  state to be worked on (bytearray)
roundKey
  round key to be used (bytearray)

Returns:
  modified state (bytearray)

#### AES.__aes_main

__aes_main(state, expandedKey, nbrRounds)

Private method to do the AES encryption for one round.

Perform the initial operations, the standard round, and the final operations of the forward AES, creating a round key for each round.

state
  state to be worked on (bytearray)
expandedKey
  expanded key to be used (bytearray)
nbrRounds
  number of rounds to be done (integer)

Returns:
  modified state (bytearray)

#### AES.__aes_round

__aes_round(state, roundKey)

Private method to apply the 4 operations of the forward round in sequence.

state
  state to be worked on (bytearray)
roundKey
  round key to be used (bytearray)

Returns:
  modified state (bytearray)

#### AES.__core

__core(data, iteration)

Private method performing the key schedule core operation.

data
  data to operate on (bytearray)
iteration
  iteration counter (integer)

Returns:
  modified data (bytearray)

#### AES.__createRoundKey

__createRoundKey(expandedKey, roundKeyPointer)

Private method to create a round key.

expandedKey
  expanded key to be used (bytearray)
roundKeyPointer
  position within the expanded key (integer)

Returns:
  round key (bytearray)

#### AES.__expandKey

__expandKey(key, size, expandedKeySize)

Private method performing Rijndael's key expansion.

Expands a 128, 192 or 256 bit key into a 176, 208 or 240 bit key.

key
  key to be expanded (bytes or bytearray)
size
  size of the key in bytes (16, 24 or 32)
expandedKeySize
  size of the expanded key (integer)

Returns:
  expanded key (bytearray)

#### AES.__galois_multiplication

__galois_multiplication(a, b)

Private method to perform a Galois multiplication of 8 bit characters a and b.

a
  first factor (byte)
b
  second factor (byte)

Returns:
  result (byte)

#### AES.__getRconValue

__getRconValue(num)

Private method to retrieve a given Rcon value.

num
  position of the value (integer)

Returns:
  Rcon value (integer)

#### AES.__getSBoxInvert

__getSBoxInvert(num)

Private method to retrieve a given Inverted S-Box value.

num
  position of the value (integer)

Returns:
  value of the Inverted S-Box (integer)

#### AES.__getSBoxValue

__getSBoxValue(num)

Private method to retrieve a given S-Box value.

num
  position of the value (integer)

Returns:
  value of the S-Box (integer)

#### AES.__mixColumn

__mixColumn(column, isInv)

Private method to perform a galois multiplication of 1 column the 4x4 matrix.

column
  column to be worked on (bytearray)
isInv
  flag indicating an inverse operation (boolean)

Returns:
  modified column (bytearray)

#### AES.__mixColumns

__mixColumns(state, isInv)

Private method to perform a galois multiplication of the 4x4 matrix.

state
  state to be worked on (bytearray)
isInv
  flag indicating an inverse operation (boolean)

Returns:
  modified state (bytearray)

#### AES.__rotate

__rotate(data)

Private method performing Rijndael's key schedule rotate operation.

Rotate the data word eight bits to the left: eg, rotate(1d2c3a4f) == 2c3a4f1d.

data
  data of size 4 (bytearray)

Returns:
  rotated data (bytearray)

#### AES.__shiftRow

__shiftRow(state, statePointer, nbr, isInv)

Private method to shift the bytes of a row to the left.

state
  state to be worked on (bytearray)
statePointer
  index into the state (integer)
nbr
  number of positions to shift (integer)
isInv
  flag indicating an inverse operation (boolean)

Returns:
  modified state (bytearray)

#### AES.__shiftRows

__shiftRows(state, isInv)

Private method to iterate over the 4 rows and call __shiftRow() with that row.

state
  state to be worked on (bytearray)
isInv
  flag indicating an inverse operation (boolean)

Returns:
  modified state (bytearray)

#### AES.__subBytes

__subBytes(state, isInv)

Private method to substitute all the values from the state with the value in the SBox using the state value as index for the SBox.

state
  state to be worked on (bytearray)
isInv
  flag indicating an inverse operation (boolean)

Returns:
  modified state (bytearray)

#### AES.decrypt

decrypt(iput, key, size)

Public method to decrypt a 128 bit input block against the given key of size specified.

iput
  input data (bytearray)
key
  key to be used (bytes or bytearray)
size
  key size (16, 24 or 32)

Returns:
  decrypted data (bytes)

Raises ValueError:
  key size is invalid

#### AES.encrypt

encrypt(iput, key, size)

Public method to encrypt a 128 bit input block against the given key of size specified.

iput
  input data (bytearray)
key
  key to be used (bytes or bytearray)
size
  key size (16, 24 or 32)

Returns:
  encrypted data (bytes)

Raises ValueError:
  key size is invalid

Up

## AESModeOfOperation

Class implementing the different AES mode of operations.

### Derived from

object

### Class Attributes

ModeOfOperation
aes

### Class Methods

None

### Methods

__extractBytes | Private method to extract a range of bytes from the input.
decrypt | Public method to perform the decryption operation.
encrypt | Public method to perform the encryption operation.

### Static Methods

None

#### AESModeOfOperation.__extractBytes

__extractBytes(input, start, end, mode)

Private method to extract a range of bytes from the input.

input
  input data (bytes)
start
  start index (integer)
end
  end index (integer)
mode
  mode of operation (0, 1, 2)

Returns:
  extracted bytes (bytearray)

#### AESModeOfOperation.decrypt

decrypt(cipherIn, originalsize, mode, key, size, IV)

Public method to perform the decryption operation.

cipherIn
  data to be decrypted (bytes)
originalsize
  unencrypted string length (required for CBC) (integer)
mode
  mode of operation (0, 1 or 2)
key
  key to be used (bytes)
size
  length of the key (16, 24 or 32)
IV
  initialisation vector (bytearray)

Returns:
  decrypted data (bytes)

Raises ValueError:
  key size is invalid or decrypted data is invalid

#### AESModeOfOperation.encrypt

encrypt(input, mode, key, size, IV)

Public method to perform the encryption operation.

input
  data to be encrypted (bytes)
mode
  mode of operation (0, 1 or 2)
key
  key to be used (bytes)
size
  length of the key (16, 24 or 32)
IV
  initialisation vector (bytearray)

Returns:
  tuple with mode of operation, length of the input and the encrypted data (integer, integer, bytes)

Raises ValueError:
  key size is invalid or decrypted data is invalid

Up

## append_PKCS7_padding

append_PKCS7_padding(b)

Function to pad the given data to a multiple of 16-bytes by PKCS7 padding.

b
  data to be padded (bytes)

Returns:
  padded data (bytes)

Up

## decryptData

decryptData(key, data, mode=AESModeOfOperation.ModeOfOperation["CBC"])

Module function to decrypt the given data with the given key.

key
  key to be used for decryption (bytes)
data
  data to be decrypted (with initialization vector prepended) (bytes)
mode
  mode of operations (0, 1 or 2)

Returns:
  decrypted data (bytes)

Up

## encryptData

encryptData(key, data, mode=AESModeOfOperation.ModeOfOperation["CBC"])

Module function to encrypt the given data with the given key.

key
  key to be used for encryption (bytes)
data
  data to be encrypted (bytes)
mode
  mode of operations (0, 1 or 2)

Returns:
  encrypted data prepended with the initialization vector (bytes)

Up

## strip_PKCS7_padding

strip_PKCS7_padding(b)

Function to strip off PKCS7 padding.

b
  data to be stripped (bytes)

Returns:
  stripped data (bytes)

Raises ValueError:
  data padding is invalid

Up