+ +
2da5fe7 OpenAVX v0.2.0 - High-performance open-standard IP audio and video transport suite
saifmukhtar · 2026-11-24
main
| 1 | /* |
| 2 | * SPDX-License-Identifier: AGPL-3.0-or-later WITH Classpath-exception-2.0 |
| 3 | * Copyright (c) 2026 Saif Mukhtar |
| 4 | * |
| 5 | * OpenAVX - Open Audio-Video eXchange |
| 6 | * src/avx_crypto.c - Native End-to-End Encryption (ChaCha20-Poly1305 / XChaCha20) |
| 7 | */ |
| 8 | |
| 9 | #define _GNU_SOURCE |
| 10 | #include <stdio.h> |
| 11 | #include <stdlib.h> |
| 12 | #include <string.h> |
| 13 | |
| 14 | #include "avx_crypto.h" |
| 15 | #include "monocypher.h" |
| 16 | #include "avx_platform.h" |
| 17 | |
| 18 | #if defined(AVX_OS_POSIX) |
| 19 | #include <fcntl.h> |
| 20 | #include <unistd.h> |
| 21 | #endif |
| 22 | |
| 23 | /* |
| 24 | * Constructs 24-byte XChaCha20 nonce packing 8-byte session salt, frame sequence, |
| 25 | * fragment index, fragment count, and fixed "OAVXE2EE" domain separation tag. |
| 26 | */ |
| 27 | static void build_nonce(uint8_t nonce[AVX_CRYPTO_NONCE_SIZE], |
| 28 | const uint8_t session_salt[AVX_CRYPTO_SALT_SIZE], |
| 29 | uint32_t frame_seq, uint16_t frag_idx, uint16_t frag_count) |
| 30 | { |
| 31 | memcpy(nonce, session_salt, 8); |
| 32 | memcpy(nonce + 8, &frame_seq, 4); |
| 33 | memcpy(nonce + 12, &frag_idx, 2); |
| 34 | memcpy(nonce + 14, &frag_count, 2); |
| 35 | memcpy(nonce + 16, "OAVXE2EE", 8); |
| 36 | } |
| 37 | |
| 38 | void avx_crypto_generate_salt(avx_crypto_ctx_t *ctx) |
| 39 | { |
| 40 | if (!ctx) return; |
| 41 | |
| 42 | bool random_ok = false; |
| 43 | |
| 44 | #if defined(AVX_OS_POSIX) |
| 45 | int fd = open("/dev/urandom", O_RDONLY); |
| 46 | if (fd >= 0) { |
| 47 | ssize_t rd = read(fd, ctx->session_salt, sizeof(ctx->session_salt)); |
| 48 | close(fd); |
| 49 | if (rd == (ssize_t)sizeof(ctx->session_salt)) { |
| 50 | random_ok = true; |
| 51 | } |
| 52 | } |
| 53 | #endif |
| 54 | |
| 55 | if (!random_ok) { |
| 56 | /* High-entropy fallback mix using monotonic clock, PID, and BLAKE2b */ |
| 57 | int64_t t = avx_time_ns(); |
| 58 | uintptr_t ptr = (uintptr_t)ctx; |
| 59 | uint8_t entropy[sizeof(t) + sizeof(ptr) + 16]; |
| 60 | memcpy(entropy, &t, sizeof(t)); |
| 61 | memcpy(entropy + sizeof(t), &ptr, sizeof(ptr)); |
| 62 | |
| 63 | static uint64_t counter = 0x9e3779b97f4a7c15ULL; |
| 64 | counter += 0x517cc1b727220a95ULL; |
| 65 | memcpy(entropy + sizeof(t) + sizeof(ptr), &counter, sizeof(counter)); |
| 66 | |
| 67 | uint8_t full_hash[32]; |
| 68 | crypto_blake2b(full_hash, sizeof(full_hash), entropy, sizeof(entropy)); |
| 69 | memcpy(ctx->session_salt, full_hash, sizeof(ctx->session_salt)); |
| 70 | crypto_wipe(full_hash, sizeof(full_hash)); |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | /* |
| 75 | * Derives 256-bit symmetric key via BLAKE2b from passphrase and stream name, |
| 76 | * then generates an initial random session salt from /dev/urandom or entropy fallback. |
| 77 | */ |
| 78 | int avx_crypto_init(avx_crypto_ctx_t *ctx, const char *passphrase, const char *stream_name) |
| 79 | { |
| 80 | if (!ctx) return -1; |
| 81 | memset(ctx, 0, sizeof(*ctx)); |
| 82 | |
| 83 | if (!passphrase || passphrase[0] == '\0') { |
| 84 | ctx->enabled = false; |
| 85 | return 0; |
| 86 | } |
| 87 | |
| 88 | crypto_blake2b_ctx bctx; |
| 89 | crypto_blake2b_init(&bctx, AVX_CRYPTO_KEY_SIZE); |
| 90 | |
| 91 | const char prefix[] = "OpenAVX_v1_E2EE_BLAKE2b_KeyDerivation"; |
| 92 | crypto_blake2b_update(&bctx, (const uint8_t*)prefix, strlen(prefix)); |
| 93 | crypto_blake2b_update(&bctx, (const uint8_t*)passphrase, strlen(passphrase)); |
| 94 | |
| 95 | if (stream_name && stream_name[0] != '\0') { |
| 96 | crypto_blake2b_update(&bctx, (const uint8_t*)stream_name, strlen(stream_name)); |
| 97 | } |
| 98 | |
| 99 | crypto_blake2b_final(&bctx, ctx->key); |
| 100 | crypto_wipe(&bctx, sizeof(bctx)); |
| 101 | |
| 102 | avx_crypto_generate_salt(ctx); |
| 103 | ctx->enabled = true; |
| 104 | return 0; |
| 105 | } |
| 106 | |
| 107 | void avx_crypto_wipe(avx_crypto_ctx_t *ctx) |
| 108 | { |
| 109 | if (!ctx) return; |
| 110 | crypto_wipe(ctx, sizeof(*ctx)); |
| 111 | ctx->enabled = false; |
| 112 | } |
| 113 | |
| 114 | /* |
| 115 | * Encrypts payload with XChaCha20-Poly1305 AEAD, authenticating the unencrypted |
| 116 | * network wire header as Additional Data (AD) and prefixing session salt to ciphertext. |
| 117 | */ |
| 118 | int avx_crypto_encrypt_payload(const avx_crypto_ctx_t *ctx, |
| 119 | uint32_t frame_seq, uint16_t frag_idx, uint16_t frag_count, |
| 120 | const void *header_ad, size_t ad_len, |
| 121 | const void *plain_payload, size_t plain_len, |
| 122 | void *out_buf, size_t *out_len) |
| 123 | { |
| 124 | if (!ctx || !ctx->enabled || !out_buf || !out_len) return -1; |
| 125 | if (plain_len > 0 && !plain_payload) return -1; |
| 126 | |
| 127 | size_t required = plain_len + AVX_CRYPTO_OVERHEAD; |
| 128 | if (*out_len < required) { |
| 129 | *out_len = required; |
| 130 | return -1; |
| 131 | } |
| 132 | |
| 133 | uint8_t *dest = (uint8_t*)out_buf; |
| 134 | |
| 135 | /* 1. Write 8-byte session salt */ |
| 136 | memcpy(dest, ctx->session_salt, AVX_CRYPTO_SALT_SIZE); |
| 137 | |
| 138 | /* 2. Construct 24-byte XChaCha20 nonce */ |
| 139 | uint8_t nonce[AVX_CRYPTO_NONCE_SIZE]; |
| 140 | build_nonce(nonce, ctx->session_salt, frame_seq, frag_idx, frag_count); |
| 141 | |
| 142 | /* 3. Encrypt payload and append 16-byte Poly1305 MAC */ |
| 143 | uint8_t *cipher_text = dest + AVX_CRYPTO_SALT_SIZE; |
| 144 | uint8_t *mac = cipher_text + plain_len; |
| 145 | |
| 146 | crypto_aead_lock(cipher_text, mac, ctx->key, nonce, |
| 147 | (const uint8_t*)header_ad, ad_len, |
| 148 | (const uint8_t*)plain_payload, plain_len); |
| 149 | |
| 150 | *out_len = required; |
| 151 | crypto_wipe(nonce, sizeof(nonce)); |
| 152 | return 0; |
| 153 | } |
| 154 | |
| 155 | /* |
| 156 | * Authenticates wire header AD and decrypts payload in-place using Poly1305 MAC. |
| 157 | * Returns -1 if packet was forged, truncated, or encrypted with a different key. |
| 158 | */ |
| 159 | int avx_crypto_decrypt_payload(const avx_crypto_ctx_t *ctx, |
| 160 | uint32_t frame_seq, uint16_t frag_idx, uint16_t frag_count, |
| 161 | const void *header_ad, size_t ad_len, |
| 162 | const void *in_buf, size_t in_len, |
| 163 | void *plain_out, size_t *out_plain_len) |
| 164 | { |
| 165 | if (!ctx || !ctx->enabled || !in_buf || !plain_out || !out_plain_len) return -1; |
| 166 | if (in_len < AVX_CRYPTO_OVERHEAD) return -1; |
| 167 | |
| 168 | const uint8_t *src = (const uint8_t*)in_buf; |
| 169 | const uint8_t *packet_salt = src; |
| 170 | size_t cipher_len = in_len - AVX_CRYPTO_OVERHEAD; |
| 171 | const uint8_t *cipher_text = src + AVX_CRYPTO_SALT_SIZE; |
| 172 | const uint8_t *mac = cipher_text + cipher_len; |
| 173 | |
| 174 | /* Reconstruct 24-byte XChaCha20 nonce from packet salt */ |
| 175 | uint8_t nonce[AVX_CRYPTO_NONCE_SIZE]; |
| 176 | build_nonce(nonce, packet_salt, frame_seq, frag_idx, frag_count); |
| 177 | |
| 178 | int ret = crypto_aead_unlock((uint8_t*)plain_out, mac, ctx->key, nonce, |
| 179 | (const uint8_t*)header_ad, ad_len, |
| 180 | cipher_text, cipher_len); |
| 181 | |
| 182 | crypto_wipe(nonce, sizeof(nonce)); |
| 183 | |
| 184 | if (ret != 0) { |
| 185 | return -1; /* Authentication failure / tampered packet / wrong key */ |
| 186 | } |
| 187 | |
| 188 | *out_plain_len = cipher_len; |
| 189 | return 0; |
| 190 | } |