# eric7.EricUtilities.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

None

### 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 (bytearray)
  state to be changed
roundKey (bytearray)
  key to be used for the modification

Return:
  modified state

Return Type:
  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 (bytearray)
  state to be worked on
expandedKey (bytearray)
  expanded key to be used
nbrRounds (int)
  number of rounds to be done

Return:
  modified state

Return Type:
  bytearray

#### AES.__aes_invRound

__aes_invRound(state, roundKey)

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

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

Return:
  modified state

Return Type:
  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 (bytearray)
  state to be worked on
expandedKey (bytearray)
  expanded key to be used
nbrRounds (int)
  number of rounds to be done

Return:
  modified state

Return Type:
  bytearray

#### AES.__aes_round

__aes_round(state, roundKey)

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

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

Return:
  modified state

Return Type:
  bytearray

#### AES.__core

__core(data, iteration)

Private method performing the key schedule core operation.

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

Return:
  modified data

Return Type:
  bytearray

#### AES.__createRoundKey

__createRoundKey(expandedKey, roundKeyPointer)

Private method to create a round key.

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

Return:
  round key

Return Type:
  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 (bytes or bytearray)
  key to be expanded
size (int)
  size of the key in bytes (16, 24 or 32)
expandedKeySize (int)
  size of the expanded key

Return:
  expanded key

Return Type:
  bytearray

#### AES.__galois_multiplication

__galois_multiplication(a, b)

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

a (bytes)
  first factor
b (bytes)
  second factor

Return:
  result

Return Type:
  bytes

#### AES.__getRconValue

__getRconValue(num)

Private method to retrieve a given Rcon value.

num (int)
  position of the value

Return:
  Rcon value

Return Type:
  int

#### AES.__getSBoxInvert

__getSBoxInvert(num)

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

num (int)
  position of the value

Return:
  value of the Inverted S-Box

Return Type:
  int

#### AES.__getSBoxValue

__getSBoxValue(num)

Private method to retrieve a given S-Box value.

num (int)
  position of the value

Return:
  value of the S-Box

Return Type:
  int

#### AES.__mixColumn

__mixColumn(column, isInv)

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

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

Return:
  modified column

Return Type:
  bytearray

#### AES.__mixColumns

__mixColumns(state, isInv)

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

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

Return:
  modified state

Return Type:
  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 (bytearray)
  data of size 4

Return:
  rotated data

Return Type:
  bytearray

#### AES.__shiftRow

__shiftRow(state, statePointer, nbr, isInv)

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

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

Return:
  modified state

Return Type:
  bytearray

#### AES.__shiftRows

__shiftRows(state, isInv)

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

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

Return:
  modified state

Return Type:
  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 (bytearray)
  state to be worked on
isInv (bool)
  flag indicating an inverse operation

Return:
  modified state

Return Type:
  bytearray

#### AES.decrypt

decrypt(iput, key, size)

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

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

Return:
  decrypted data

Return Type:
  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 (bytearray)
  input data
key (bytes or bytearray)
  key to be used
size (int)
  key size (16, 24 or 32)

Return:
  encrypted data

Return Type:
  bytes

Raises ValueError:
  key size is invalid

Up

## AESModeOfOperation

Class implementing the different AES mode of operations.

### Derived from

None

### 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(inputData, start, end, mode)

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

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

Return:
  extracted bytes

Return Type:
  bytearray

#### AESModeOfOperation.decrypt

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

Public method to perform the decryption operation.

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

Return:
  decrypted data

Return Type:
  bytes

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

#### AESModeOfOperation.encrypt

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

Public method to perform the encryption operation.

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

Return:
  tuple with mode of operation, length of the input data and the encrypted data

Return Type:
  tuple of (int, int, 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 (bytes)
  data to be padded

Return:
  padded data

Return Type:
  bytes

Up

## decryptData

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

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

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

Return:
  decrypted data

Return Type:
  bytes

Raises ValueError:
  raised to indicate an invalid key size

Up

## encryptData

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

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

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

Return:
  encrypted data prepended with the initialization vector

Return Type:
  bytes

Raises ValueError:
  raised to indicate an invalid key size

Up

## strip_PKCS7_padding

strip_PKCS7_padding(b)

Function to strip off PKCS7 padding.

b (bytes)
  data to be stripped

Return:
  stripped data

Return Type:
  bytes

Raises ValueError:
  data padding is invalid

Up