1

I need some help, I need to pass a rijndael round key into a function as a parameter. The round key is a 3D array which looks like this:

unsigned char rk[11][4][4]; 

Now I need to input this into a function and make some binary operations to it (so the parameter should be editable). How do I do it the right way? The way I did it was to put it inside a struct and pass the struct to the function. But it is ugly. So now my code looks like this:

typedef struct{ unsigned char rk[11][4][4]; }roundKey; void addKey(roundKey* rk, ....) { some_unsigned_char ^= rk->rk[foo][bar][i]; } 

It is ugly and I would like to do it the right way. How should I do it?

2
  • Off topic: The typedef struct { ... } roundKey; trick isn't needed in C++. Plain old struct roundKey { ... }; does the deed for you. Commented Jun 7, 2017 at 18:16
  • Thank you @user4581301 ! Yes, I coded much more C and C++ is rather new for me. Commented Jun 7, 2017 at 18:51

1 Answer 1

2

Au contraire, I think you are doing it in one of the best possible ways, I'd just rearrange to make that a full-fledged class. That way we can easily pass it around, add some nice accessor methods to simplify the code, and otherwise do our best to abstract away the 3D nature of the array from most of the code.

class RoundKey { public: uint8_t rk[11][4][4]; uint8_t& operator()(int x, int y, int z){ return rk[x][y][z]; } }; void addKey(RoundKey &rk, ....){ some_unsigned_char ^= rk(foo,bar,i); } 

(Rearrange indices as appropriate.)

Given the simplicity of the class, the compiler's likely to compile it to something that is very similar, if not identical, to what passing the full 3D array around would have given.

Sign up to request clarification or add additional context in comments.

2 Comments

Well thank you ! That is nice to hear. I think I will go with my way though, as I only need to pass my "ugly" struct only to a couple of functions.
@JussiHietanen: I'm sure things will work out. That said, evil always starts with only a "couple of functions". Defensive coding practices would say that we should use designs that are resilient to unknown future needs and scope creep.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.