This is the first time I've been working with structure pointers, and I can't figure out what is going on here. My test uses the main property of xor, which says x ^ y ^ y = x, but not in C?
The code below is in my main program and accurately recovers all the letters of the "test" (which I go to print on the screen, but I emptied a lot of garbage so that this question is short (er)). The structure "aes" refers to this definition:
typedef uint32_t word; struct aes { word iv[4]; word key[8]; word state[4]; word schedule[56]; };
As the context may suggest, an encapsulating project is an implementation of AES (I'm trying to speed up my current one by trying new methods).
In my testing, make_string and make_state work reliably, even in the functions in question, but for the sake of reference:
void make_string (word in[], char out[]) { for (int i = 0; i < 4; i++) { out[(i * 4) + 0] = (char) (in[i] >> 24); out[(i * 4) + 1] = (char) (in[i] >> 16); out[(i * 4) + 2] = (char) (in[i] >> 8); out[(i * 4) + 3] = (char) (in[i] ); } } void make_state(word out[], char in[]) { for (int i = 0; i < 4; i++) { out[i] = (word) (in[(i * 4) + 0] << 24) ^ (word) (in[(i * 4) + 1] << 16) ^ (word) (in[(i * 4) + 2] << 8) ^ (word) (in[(i * 4) + 3] ); } }
Anyway, here is a block that works. This is the kind of functionality that I am trying to do in a modular way by clipping it in a function:
char test[16] = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p' }; aes cipher; struct aes * work; work = &cipher; make_state(work->state, test); work->state[0] ^= 0xbc6378cd; work->state[0] ^= 0xbc6378cd; make_string(work->state, test);
And although this code works, doing the same thing by passing it to a function does not:
void encipher_block (struct aes * work, char in[]) { make_state(work->state, in); work->state[0] ^= 0xff00cd00; make_string(work->state, in); } void decipher_block (struct aes * work, char in[]) { make_state(work->state, in); work->state[0] ^= 0xff00cd00; make_string(work->state, in); }
However, by removing make_state and make_string calls for both encryption and decryption, it works as expected!
make_state(work->state, test); encipher_block(&cipher, test); decipher_block(&cipher, test); make_string(work->state, test);
So, to clarify, I have no problem! I just want to understand this behavior.