Why does this code break when -O2 or higher is enabled?
It was a bug in the compiler.
I posted the question in the manufacturer's forum. Other people have indeed reproduced the issue, which happens when compiling for certain parts. Other parts are unaffected.
As a workaround, I changed the macros into real functions, and split the operation in two lines:
uint32_t ROL(uint32_t x, uint8_t r) {
uint32_t intermedio;
intermedio = x << r;
intermedio |= x >> (32 - r);
return intermedio;
}
This gives the correct result.
Posting compilable test code as a reference.
#include <stdint.h>
#include <string.h>
//#include "speck.h"
#define ROR(x, r) ((x >> r) | (x << (32 - r)))
#define ROL(x, r) ((x << r) | (x >> (32 - r)))
#define R(x, y, k) (x = ROR(x, 8), x += y, x ^= k, y = ROL(y, 3), y ^= x)
#define ROUNDS 27
void encrypt_block(uint32_t ct[2], uint32_t const pt[2], uint32_t const K[4]) {
uint32_t x = pt[0], y = pt[1];
uint32_t a = K[0], b = K[1], c = K[2], d = K[3];
R(y, x, a);
// for (int i = 0; i < ROUNDS - 3; i += 3) {
for (uint32_t i = 0; i < ROUNDS - 3; i += 3) {
R(b, a, i);
R(y, x, a);
R(c, a, i + 1);
R(y, x, a);
R(d, a, i + 2);
R(y, x, a);
}
R(b, a, ROUNDS - 3);
R(y, x, a);
R(c, a, ROUNDS - 2);
R(y, x, a);
ct[0] = x;
ct[1] = y;
}
int main(void) {
uint32_t out[2];
uint32_t in[] = {0x7475432d, 0x3b726574};
uint32_t key[] = {0x03020100, 0x0b0a0908, 0x13121110, 0x1b1a1918};
encrypt_block(out, in, key);
printf("%8lx %8lx\n", (unsigned long) out[0], 0x454e028bLU);
printf("%8lx %8lx\n", (unsigned long) out[1], 0x8c6fa548LU);
}
Output
454e028b 454e028b
8c6fa548 8c6fa548
Unexpected output
0x8FA3FED7
0x53D8CEA8