Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/aienux/blob/main/native/crypto/aes.c # aienux Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately. Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `aienux / native / crypto / aes.c` 193 lines ยท 6218 bytes Raw Blame History 1 /* SPDX-License-Identifier: Apache-2.0 2 * Copyright 2026 Drake Stapleton (AIEN / aien-dev) 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 /* AES-256 block encryption per FIPS 197, port of crates/aienos-crypto/src/aes.rs. 18 * 19 * Constant-time design: the S-box is computed, not looked up. Each byte is 20 * inverted in GF(2^8) as x^254 (0 maps to 0) and passed through the FIPS 197 21 * affine transform. All arithmetic is shifts, masks and XORs with a fixed 22 * number of steps, 8 bytes packed per uint64_t ("SIMD within a register"), 23 * so no memory address and no branch depends on key or data bytes. 24 * Slower than table AES; ARMv8 AESE/AESMC are a possible future speedup. 25 */ 26 #include "aienos_crypto.h" 27 #include "crypto_internal.h" 28 29 static const uint32_t RCON[10] = { 30 0x01000000u, 0x02000000u, 0x04000000u, 0x08000000u, 0x10000000u, 31 0x20000000u, 0x40000000u, 0x80000000u, 0x1b000000u, 0x36000000u, 32 }; 33 34 #define LANES(m) ((uint64_t)(m) * UINT64_C(0x0101010101010101)) 35 #define LSB LANES(0x01) 36 #define LOW7 LANES(0x7f) 37 38 /* 0/1 per lane -> 0x00/0xff per lane. (b << 8) - b == b * 0xff lane by lane: 39 * no carry or borrow crosses lanes, and it holds mod 2^64 for the top lane. */ 40 static inline uint64_t lane_mask(uint64_t bits) 41 { 42 return (bits << 8) - bits; 43 } 44 45 /* Multiply every lane by x in GF(2^8), branch-free. */ 46 static inline uint64_t xtime_lanes(uint64_t a) 47 { 48 uint64_t hi = (a >> 7) & LSB; 49 uint64_t reduce = hi ^ (hi << 1) ^ (hi << 3) ^ (hi << 4); /* hi * 0x1b */ 50 return ((a & LOW7) << 1) ^ reduce; 51 } 52 53 /* Lane-wise GF(2^8) multiply modulo x^8 + x^4 + x^3 + x + 1; always 8 steps. */ 54 static inline uint64_t gf_mul_lanes(uint64_t a, uint64_t b) 55 { 56 uint64_t acc = 0; 57 for (int i = 0; i < 8; i++) { 58 acc ^= a & lane_mask((b >> i) & LSB); 59 a = xtime_lanes(a); 60 } 61 return acc; 62 } 63 64 /* Rotate each byte lane left by k (1..7). */ 65 static inline uint64_t rotl_lanes(uint64_t x, unsigned k) 66 { 67 uint64_t hi = LANES((uint8_t)(0xffu << k)); 68 uint64_t lo = LANES(0xffu >> (8 - k)); 69 return ((x << k) & hi) | ((x >> (8 - k)) & lo); 70 } 71 72 /* S(b) = Affine(b^254) on 8 lanes; fixed addition chain of 11 multiplies. */ 73 static uint64_t sub_lanes(uint64_t x) 74 { 75 uint64_t x2 = gf_mul_lanes(x, x); 76 uint64_t x3 = gf_mul_lanes(x2, x); 77 uint64_t x6 = gf_mul_lanes(x3, x3); 78 uint64_t x12 = gf_mul_lanes(x6, x6); 79 uint64_t x15 = gf_mul_lanes(x12, x3); 80 uint64_t x30 = gf_mul_lanes(x15, x15); 81 uint64_t x60 = gf_mul_lanes(x30, x30); 82 uint64_t x120 = gf_mul_lanes(x60, x60); 83 uint64_t x240 = gf_mul_lanes(x120, x120); 84 uint64_t x252 = gf_mul_lanes(x240, x12); 85 uint64_t inv = gf_mul_lanes(x252, x2); 86 return inv ^ rotl_lanes(inv, 1) ^ rotl_lanes(inv, 2) ^ rotl_lanes(inv, 3) ^ 87 rotl_lanes(inv, 4) ^ LANES(0x63); 88 } 89 90 static inline uint64_t load_le64(const uint8_t *p) 91 { 92 uint64_t v = 0; 93 for (int i = 7; i >= 0; i--) 94 v = (v << 8) | p[i]; 95 return v; 96 } 97 98 static inline void store_le64(uint8_t *p, uint64_t v) 99 { 100 for (int i = 0; i < 8; i++) 101 p[i] = (uint8_t)(v >> (8 * i)); 102 } 103 104 void aienos_aes_sub_bytes16(uint8_t s[16]) 105 { 106 store_le64(s, sub_lanes(load_le64(s))); 107 store_le64(s + 8, sub_lanes(load_le64(s + 8))); 108 } 109 110 static uint32_t sub_word(uint32_t w) 111 { 112 return (uint32_t)sub_lanes(w); 113 } 114 115 static inline uint32_t rot_word(uint32_t w) 116 { 117 return (w << 8) | (w >> 24); 118 } 119 120 static inline uint8_t xtime(uint8_t x) 121 { 122 return (uint8_t)((x << 1) ^ (0x1bu & (0u - (unsigned)(x >> 7)))); 123 } 124 125 void aienos_aes256_init(aienos_aes256_key *k, const uint8_t key[32]) 126 { 127 uint32_t *rk = k->rk; 128 for (int i = 0; i < 8; i++) 129 rk[i] = ((uint32_t)key[4 * i] << 24) | ((uint32_t)key[4 * i + 1] << 16) | 130 ((uint32_t)key[4 * i + 2] << 8) | (uint32_t)key[4 * i + 3]; 131 /* i % 8 depends only on the public loop index. */ 132 for (int i = 8; i < 60; i++) { 133 uint32_t t = rk[i - 1]; 134 if (i % 8 == 0) 135 t = sub_word(rot_word(t)) ^ RCON[i / 8 - 1]; 136 else if (i % 8 == 4) 137 t = sub_word(t); 138 rk[i] = rk[i - 8] ^ t; 139 } 140 } 141 142 void aienos_aes256_wipe(aienos_aes256_key *k) 143 { 144 aienos_wipe(k->rk, sizeof k->rk); 145 } 146 147 static inline void add_round_key(uint8_t s[16], const uint32_t *rk) 148 { 149 for (int c = 0; c < 4; c++) { 150 s[4 * c] ^= (uint8_t)(rk[c] >> 24); 151 s[4 * c + 1] ^= (uint8_t)(rk[c] >> 16); 152 s[4 * c + 2] ^= (uint8_t)(rk[c] >> 8); 153 s[4 * c + 3] ^= (uint8_t)rk[c]; 154 } 155 } 156 157 /* State is column-major: s[r + 4c]. Row r rotates left by r. */ 158 static inline void shift_rows(uint8_t s[16]) 159 { 160 uint8_t t; 161 t = s[1]; s[1] = s[5]; s[5] = s[9]; s[9] = s[13]; s[13] = t; 162 t = s[2]; s[2] = s[10]; s[10] = t; 163 t = s[6]; s[6] = s[14]; s[14] = t; 164 t = s[15]; s[15] = s[11]; s[11] = s[7]; s[7] = s[3]; s[3] = t; 165 } 166 167 static inline void mix_columns(uint8_t s[16]) 168 { 169 for (int c = 0; c < 4; c++) { 170 uint8_t *p = s + 4 * c; 171 uint8_t s0 = p[0], s1 = p[1], s2 = p[2], s3 = p[3]; 172 uint8_t h0 = xtime(s0), h1 = xtime(s1), h2 = xtime(s2), h3 = xtime(s3); 173 p[0] = h0 ^ s1 ^ h1 ^ s2 ^ s3; 174 p[1] = s0 ^ h1 ^ s2 ^ h2 ^ s3; 175 p[2] = s0 ^ s1 ^ h2 ^ s3 ^ h3; 176 p[3] = s0 ^ h0 ^ s1 ^ s2 ^ h3; 177 } 178 } 179 180 /* Works directly on the caller's block: no state copy left on the stack. */ 181 void aienos_aes256_encrypt_block(const aienos_aes256_key *k, uint8_t block[16]) 182 { 183 add_round_key(block, &k->rk[0]); 184 for (int round = 1; round < 14; round++) { 185 aienos_aes_sub_bytes16(block); 186 shift_rows(block); 187 mix_columns(block); 188 add_round_key(block, &k->rk[4 * round]); 189 } 190 aienos_aes_sub_bytes16(block); 191 shift_rows(block); 192 add_round_key(block, &k->rk[56]); 193 }