2

I'm recently studying cryptography, and I have a task for collecting the s-boxes for AES, and then implementing that s-boxes for encrypting & decrypting, but most of the S-boxes I found are not including the inverse of it.

I know that most of it gives the calculation on how to construct the s-box and the inverse, but I don't think I can make it in time if I do that.

So, I wonder is any method of calculating the inverse of random AES S-BOXes?

kelalaka
  • 48,443
  • 11
  • 116
  • 196
joni
  • 23
  • 2

1 Answers1

2

The SageMath has a great S-Box package that is heavily used by S-Box designers. If you are going to learn/play with S-Boxes you may benefit from this package.

With Sbox Package (Try online)

from sage.crypto.sbox import SBox

#must be power of two otherwise error p = Permutations(range(16)).random_element()

S = SBox(p); print("The permutation ", S) print("The inverse permutation", S.inverse())

SageMath currently using Python3, so one can write their version very easily either in Python or SageMath (Try online);

P = Permutations(range(16)).random_element()

print("The permutation ", P)

InverseP=[P.index(i) for i in range(16)] print("The inverse permutation", InverseP)

As we can see the S-Box package is much better, for example, the construction checks that the size is equal to a power of 2 or not.


Note: I've used random permutations to guarantee that the inverse exists.


As we can see from the Sbox package source

m = self.input_size()
L = [self(i) for i in range(1<<m)]
return SBox([L.index(i) for i in range(1<<m)], big_endian=self._big_endian)

SageMath converts S-Box into a List, takes the inverse on the list then constructs a S-Box on the list. As we can see the inverse - if exist - is not magic.

kelalaka
  • 48,443
  • 11
  • 116
  • 196