4#ifndef PROTOCORE_AES_BLOCK_H
5#define PROTOCORE_AES_BLOCK_H
8#include "memoria_operor/memoria_operor.h"
42 return (uint8_t)((a << 1) ^ ((a >> 7) ? 0x1bu : 0x00u));
48 return ((uint32_t)PROTOCORE_AES_SBOX[w >> 24] << 24) | ((uint32_t)PROTOCORE_AES_SBOX[(w >> 16) & 0xff] << 16) |
49 ((uint32_t)PROTOCORE_AES_SBOX[(w >> 8) & 0xff] << 8) | (uint32_t)PROTOCORE_AES_SBOX[w & 0xff];
55 return (w << 8) | (w >> 24);
65 static const uint8_t RCON[11] = {0x00, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x1b, 0x36};
66 for (
int i = 0; i < nk; i++)
68 rk[i] = ((uint32_t)key[4 * i] << 24) | ((uint32_t)key[4 * i + 1] << 16) | ((uint32_t)key[4 * i + 2] << 8) |
69 (uint32_t)key[4 * i + 3];
72 int total = 4 * (nk + 7);
73 for (
int i = nk; i < total; i++)
75 uint32_t t = rk[i - 1];
80 else if (nk > 6 && i % nk == 4)
84 rk[i] = rk[i - nk] ^ t;
95 for (
int i = 0; i < 16; i++)
97 s[i] = in[i] ^ (uint8_t)(rk[i / 4] >> (24 - (i % 4) * 8));
100 for (
int r = 1; r < nr; r++)
102 for (
int i = 0; i < 16; i++)
104 s[i] = PROTOCORE_AES_SBOX[s[i]];
125 for (
int c = 0; c < 4; c++)
127 uint8_t a = s[c * 4];
128 uint8_t b = s[c * 4 + 1];
129 uint8_t cc = s[c * 4 + 2];
130 uint8_t d = s[c * 4 + 3];
131 uint8_t e = a ^ b ^ cc ^ d;
138 for (
int i = 0; i < 16; i++)
140 s[i] ^= (uint8_t)(rk[r * 4 + i / 4] >> (24 - (i % 4) * 8));
144 for (
int i = 0; i < 16; i++)
146 s[i] = PROTOCORE_AES_SBOX[s[i]];
167 for (
int i = 0; i < 16; i++)
169 s[i] ^= (uint8_t)(rk[nr * 4 + i / 4] >> (24 - (i % 4) * 8));
172 EMBED_CALL(memor.cpy, MemoriaCfg, .dst = out, .src = s, .bytes = 16);
178 proto_bool (*key_expand)(uint8_t *,
const uint8_t *, int, uint32_t *);
179 proto_bool (*encrypt_block)(uint8_t *,
const uint32_t *, int,
const uint8_t *, uint8_t *);
PROTOCORE_INLINE uint32_t protocore_aes_rot_word(uint32_t w)
AES RotWord (FIPS 197 sec 5.2): cyclically rotate a 32-bit word one byte left.
PROTOCORE_NS AesBlockNs AesBlock PROTOCORE_UNUSED
Module namespace.
PROTOCORE_INLINE void protocore_aes_encrypt_block(const uint32_t *rk, int nr, const uint8_t in[16], uint8_t out[16])
AES single-block encrypt (FIPS 197 sec 5.1), nr rounds (10=AES-128, 14=AES-256). State is column-majo...
PROTOCORE_INLINE uint8_t protocore_aes_xtime(uint8_t a)
GF(2^8) multiply-by-2 (xtime) for the AES MixColumns step.
proto_bool protocore_aes_block_key_expand(uint8_t *work, const uint8_t *key, int nk, uint32_t *rk)
Expand a key into 4 * (nk + 7) round-key words.
PROTOCORE_INLINE uint32_t protocore_aes_sub_word(uint32_t w)
AES SubWord (FIPS 197 sec 5.2): apply the S-box to each of the four bytes of a 32-bit word.
proto_bool protocore_aes_block_encrypt_block(uint8_t *work, const uint32_t *rk, int nr, const uint8_t *in, uint8_t *out)
Encrypt one 16-byte block under that schedule.
PROTOCORE_INLINE void protocore_aes_key_expand(const uint8_t *key, int nk, uint32_t *rk)
AES key expansion (FIPS 197 sec 5.2). nk key words (4=AES-128, 8=AES-256); rk receives 4*(nk + 7) rou...
The AES forward S-box (FIPS 197 Figure 7) - one shared copy.
#define PROTOCORE_INLINE
Linkage for a leaf primitive whose body is cheaper than the call that reaches it.
#define PROTOCORE_NS_LAYOUT(T,...)
Pin every dispatch slot of a table that is nothing but function pointers.
#define PROTOCORE_NS
Storage for a dispatch table. The const is load bearing.
Dispatch table. Addressed by offset, so the layout is asserted below.
proto_bool(* key_expand)(uint8_t *, const uint8_t *, int, uint32_t *)
#define PROTOCORE_BEGIN_DECLS
Give a header's declarations C linkage, so their symbol names carry no parameter types.
_Bool proto_bool
The truth value.
#define PROTOCORE_END_DECLS