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/polyval.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 / polyval.c` 100 lines ยท 3063 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 /* POLYVAL per RFC 8452 Section 3, port of crates/aienos-crypto/src/polyval.rs. 18 * 19 * Field GF(2^128) modulo x^128 + x^127 + x^126 + x^121 + 1, little-endian 20 * bit order. Constant-time: dot() always runs 128 steps; each bit of a 21 * selects b through a mask (behind an optimization barrier), never a branch 22 * or a secret-dependent address. 23 */ 24 #include "aienos_crypto.h" 25 #include "crypto_internal.h" 26 27 static inline uint64_t load_le64(const uint8_t *p) 28 { 29 uint64_t v = 0; 30 for (int i = 7; i >= 0; i--) 31 v = (v << 8) | p[i]; 32 return v; 33 } 34 35 static inline void store_le64(uint8_t *p, uint64_t v) 36 { 37 for (int i = 0; i < 8; i++) 38 p[i] = (uint8_t)(v >> (8 * i)); 39 } 40 41 void aienos_polyval_dot(uint8_t out[16], const uint8_t a[16], const uint8_t b[16]) 42 { 43 uint64_t a0 = load_le64(a), a1 = load_le64(a + 8); 44 uint64_t b0 = load_le64(b), b1 = load_le64(b + 8); 45 uint64_t v0 = 0, v1 = 0; 46 47 for (int i = 0; i < 128; i++) { 48 uint64_t bit = ((i < 64 ? a0 >> i : a1 >> (i - 64))) & 1u; 49 AIENOS_CT_BARRIER(bit); 50 uint64_t m = 0u - bit; 51 v0 ^= b0 & m; 52 v1 ^= b1 & m; 53 54 /* v = v * x^-1: shift right one bit, fold x^-1 = 0xe1 << 120 if the 55 * bit shifted out was set. */ 56 uint64_t lsb = v0 & 1u; 57 AIENOS_CT_BARRIER(lsb); 58 v0 = (v0 >> 1) | (v1 << 63); 59 v1 = (v1 >> 1) ^ ((UINT64_C(0xe1) << 56) & (0u - lsb)); 60 } 61 store_le64(out, v0); 62 store_le64(out + 8, v1); 63 /* a0..b1 are secret register copies; nothing to wipe in memory. */ 64 } 65 66 void aienos_polyval_init(aienos_polyval *p, const uint8_t h[16]) 67 { 68 for (int i = 0; i < 16; i++) { 69 p->h[i] = h[i]; 70 p->s[i] = 0; 71 } 72 } 73 74 void aienos_polyval_update_block(aienos_polyval *p, const uint8_t x[16]) 75 { 76 for (int i = 0; i < 16; i++) 77 p->s[i] ^= x[i]; 78 aienos_polyval_dot(p->s, p->s, p->h); 79 } 80 81 int aienos_polyval_update(aienos_polyval *p, const uint8_t *data, size_t len) 82 { 83 if (len % 16 != 0 || (data == NULL && len != 0)) 84 return AIENOS_CRYPTO_ERR_LENGTH; 85 for (size_t off = 0; off < len; off += 16) 86 aienos_polyval_update_block(p, data + off); 87 return AIENOS_CRYPTO_OK; 88 } 89 90 void aienos_polyval_final(aienos_polyval *p, uint8_t out[16]) 91 { 92 for (int i = 0; i < 16; i++) 93 out[i] = p->s[i]; 94 aienos_polyval_wipe(p); 95 } 96 97 void aienos_polyval_wipe(aienos_polyval *p) 98 { 99 aienos_wipe(p, sizeof *p); 100 }