Advertisement
Anupznk

expandRoundKeys new

Jul 2nd, 2023
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.30 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. """
  9. N as the length of the key in 32-bit words:
  10. 4 words for AES-128, 6 words for AES-192,
  11. and 8 words for AES-256
  12.  
  13. R as the number of rounds:
  14. 10 rounds for AES-128, 12 rounds for AES-192,
  15. 14 rounds for AES-256
  16. """
  17.  
  18. key_size = len(key)
  19. # print('key_size', key_size)
  20. num_keys = util.getNumOfRounds() + 1 # this is R
  21.  
  22. round_keys = []
  23.  
  24. N = key_size // 4
  25. for i in range(4 * num_keys):
  26.  
  27. if i < N:
  28. # Initialize round keys with the original key
  29. currWord = []
  30. for j in range(4):
  31. currWord.append(key[i * 4 + j])
  32. round_keys.append(currWord)
  33.  
  34. print('key', key)
  35. print('round keys', round_keys)
  36.  
  37. elif i >= N and i % N == 0:
  38. prevWord = round_keys[i - 1].copy()
  39. prevWord = rot_word(prevWord)
  40. prevWord = sub_word(prevWord)
  41. prevWord[0] ^= roundConstsHex[i // N]
  42. # round constants are only added to the first element of a word
  43. prevWord = [round_keys[i - N][j] ^ prevWord[j] for j in range(4)]
  44. round_keys.append(prevWord)
  45.  
  46. elif i >= N and N > 6 and i % N == 4:
  47. prevWord = round_keys[i - 1].copy()
  48. prevWord = sub_word(prevWord) # SubWord(W[i-1])
  49. prevWord = [round_keys[i - N][j] ^ prevWord[j] for j in range(4)]
  50. round_keys.append(prevWord)
  51.  
  52. else:
  53. prevWord = round_keys[i - 1].copy()
  54. prevWord = [round_keys[i - N][j] ^ prevWord[j] for j in range(4)]
  55. round_keys.append(prevWord)
  56.  
  57. # print('round keys', round_keys)
  58.  
  59. allRoundKeys = []
  60. for i in range(0, len(round_keys), 4): # range(start, stop, step) 4 words per round
  61. currRoundKey = []
  62. for j in range(4):
  63. currRoundKey.append(round_keys[i + j])
  64. allRoundKeys.append(np.transpose(currRoundKey))
  65.  
  66. flattened_list = [element for sublist in allRoundKeys for element in sublist]
  67. # print(flattened_list[2])
  68. global expandedRoundKeys
  69. expandedRoundKeys = flattened_list
  70.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement