Advertisement
Anupznk

expandRoundKeys

Jul 2nd, 2023
660
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.79 KB | None | 0 0
  1. def expandRoundKeys(key):
  2.     """
  3.    https://en.m.wikipedia.org/wiki/AES_key_schedule?fbclid=IwAR2MKdPC8cO1BeqKlkaheZYKKlUf_QgO7-t1SDPD5etsE8_RvhrLnQnWYAA
  4.    """
  5.  
  6.     genRoundConsts()
  7.  
  8.     key_size = len(key)
  9.     # print('key_size', key_size)
  10.     num_keys = util.getNumOfRounds() + 1  # this is R
  11.  
  12.     # Initialize round keys with the original key
  13.     round_keys = [key[i:i + 4] for i in range(0, key_size, 4)]
  14.  
  15.     """
  16.    N as the length of the key in 32-bit words:
  17.    4 words for AES-128, 6 words for AES-192,
  18.    and 8 words for AES-256
  19.  
  20.    R as the number of rounds:
  21.    10 rounds for AES-128, 12 rounds for AES-192,
  22.    14 rounds for AES-256
  23.    """
  24.     # print('round const hex', aes_extension.roundConstsHex)
  25.  
  26.     N = key_size // 4
  27.     for i in range(N, 4 * num_keys):
  28.         # i >= N
  29.         prevWord = round_keys[i - 1].copy()
  30.         if i % N == 0:
  31.             prevWord = rot_word(prevWord)
  32.             prevWord = sub_word(prevWord)
  33.             prevWord[0] ^= roundConstsHex[i // N]
  34.  
  35.         elif i >= N and N > 6 and i % N == 4:  # todo check
  36.             prevWord = sub_word(prevWord)   # SubWord(W[i-1])
  37.  
  38.         # common for all cases
  39.         # todo
  40.         prevWord = [round_keys[i - N][j] ^ prevWord[j] for j in range(4)]
  41.         round_keys.append(prevWord)
  42.         # print('round keys', round_keys)
  43.  
  44.     allRoundKeys = []
  45.     for i in range(0, len(round_keys), 4):  # range(start, stop, step) 4 words per round
  46.         currRoundKey = []
  47.         for j in range(4):
  48.             currRoundKey.append(round_keys[i + j])
  49.         allRoundKeys.append(np.transpose(currRoundKey))
  50.  
  51.     flattened_list = [element for sublist in allRoundKeys for element in sublist]
  52.     # print(flattened_list[2])
  53.     global expandedRoundKeys
  54.     expandedRoundKeys = flattened_list
  55.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement