feat: add SMB networking support

This commit is contained in:
daniel committed 2026-11-22 08:43:18 +01:00
1 parent 6665750528
commit 2af2e90606
55 files changed
+7456 -158

No files matched your search

+174
View File
@@ -0,0 +1,174 @@
/*
* crypto.cpp
* Hashes and MACs used by SMB signing and NTLMv2
* Copyright (c) 2026 Daniel Hammer
*/
#include "internal.hpp"
extern "C" {
#include <bearssl_hash.h>
#include <bearssl_hmac.h>
#include <bearssl_block.h>
}
namespace smb::detail {
// ==========================================================================
// MD4 (RFC 1320). BearSSL does not ship it; NTLM's password hash needs it.
// ==========================================================================
static inline uint32_t rol(uint32_t v, int s) { return (v << s) | (v >> (32 - s)); }
static void md4_block(uint32_t st[4], const uint8_t* blk) {
uint32_t x[16];
for (int i = 0; i < 16; i++) x[i] = get32(blk + i * 4);
uint32_t a = st[0], b = st[1], c = st[2], d = st[3];
#define MD4_F(x, y, z) (((x) & (y)) | (~(x) & (z)))
#define MD4_G(x, y, z) (((x) & (y)) | ((x) & (z)) | ((y) & (z)))
#define MD4_H(x, y, z) ((x) ^ (y) ^ (z))
#define R1(a, b, c, d, k, s) a = rol(a + MD4_F(b, c, d) + x[k], s)
#define R2(a, b, c, d, k, s) a = rol(a + MD4_G(b, c, d) + x[k] + 0x5A827999u, s)
#define R3(a, b, c, d, k, s) a = rol(a + MD4_H(b, c, d) + x[k] + 0x6ED9EBA1u, s)
for (int i = 0; i < 16; i += 4) {
R1(a, b, c, d, i, 3); R1(d, a, b, c, i + 1, 7);
R1(c, d, a, b, i + 2, 11); R1(b, c, d, a, i + 3, 19);
}
for (int i = 0; i < 4; i++) {
R2(a, b, c, d, i, 3); R2(d, a, b, c, i + 4, 5);
R2(c, d, a, b, i + 8, 9); R2(b, c, d, a, i + 12, 13);
}
static const int order[4] = {0, 2, 1, 3};
for (int j = 0; j < 4; j++) {
int i = order[j];
R3(a, b, c, d, i, 3); R3(d, a, b, c, i + 8, 9);
R3(c, d, a, b, i + 4, 11); R3(b, c, d, a, i + 12, 15);
}
#undef MD4_F
#undef MD4_G
#undef MD4_H
#undef R1
#undef R2
#undef R3
st[0] += a; st[1] += b; st[2] += c; st[3] += d;
}
void md4(const uint8_t* data, size_t len, uint8_t out[16]) {
uint32_t st[4] = {0x67452301u, 0xEFCDAB89u, 0x98BADCFEu, 0x10325476u};
size_t off = 0;
for (; off + 64 <= len; off += 64) md4_block(st, data + off);
uint8_t tail[128];
size_t rem = len - off;
memset(tail, 0, sizeof(tail));
if (rem > 0) memcpy(tail, data + off, rem);
tail[rem] = 0x80;
size_t total = (rem < 56) ? 64 : 128;
put64(tail + total - 8, (uint64_t)len * 8);
md4_block(st, tail);
if (total == 128) md4_block(st, tail + 64);
for (int i = 0; i < 4; i++) put32(out + i * 4, st[i]);
}
// ==========================================================================
// HMAC
// ==========================================================================
static void hmac(const br_hash_class* cls, const uint8_t* key, size_t keyLen,
const uint8_t* a, size_t aLen, const uint8_t* b, size_t bLen, uint8_t* out) {
br_hmac_key_context kc;
br_hmac_context ctx;
br_hmac_key_init(&kc, cls, key, keyLen);
br_hmac_init(&ctx, &kc, 0);
if (aLen > 0) br_hmac_update(&ctx, a, aLen);
if (bLen > 0) br_hmac_update(&ctx, b, bLen);
br_hmac_out(&ctx, out);
}
void hmac_md5(const uint8_t* key, size_t keyLen, const uint8_t* a, size_t aLen,
const uint8_t* b, size_t bLen, uint8_t out[16]) {
hmac(&br_md5_vtable, key, keyLen, a, aLen, b, bLen, out);
}
void hmac_sha256(const uint8_t* key, size_t keyLen, const uint8_t* a, size_t aLen,
const uint8_t* b, size_t bLen, uint8_t out[32]) {
hmac(&br_sha256_vtable, key, keyLen, a, aLen, b, bLen, out);
}
// ==========================================================================
// AES-128-CMAC (RFC 4493), the SMB 3.0 signing algorithm
// ==========================================================================
// Double a 128-bit value in GF(2^128) (the CMAC subkey step).
static void cmac_double(const uint8_t in[16], uint8_t out[16]) {
uint8_t carry = 0;
for (int i = 15; i >= 0; i--) {
uint8_t next = (uint8_t)(in[i] >> 7);
out[i] = (uint8_t)((in[i] << 1) | carry);
carry = next;
}
if (carry) out[15] ^= 0x87;
}
void aes_cmac(const uint8_t key[16], const uint8_t* data, size_t len, uint8_t out[16]) {
br_aes_ct_cbcenc_keys keys;
br_aes_ct_cbcenc_init(&keys, key, 16);
// CBC-MAC is CBC encryption that keeps only the chaining value, which
// br_aes_ct_cbcenc_run leaves in `iv`.
uint8_t iv[16];
uint8_t block[16];
uint8_t l[16], k1[16], k2[16];
memset(iv, 0, 16);
memset(l, 0, 16);
br_aes_ct_cbcenc_run(&keys, iv, l, 16);
cmac_double(l, k1);
cmac_double(k1, k2);
size_t blocks = (len + 15) / 16;
bool lastComplete = len > 0 && (len % 16) == 0;
if (blocks == 0) blocks = 1;
memset(iv, 0, 16);
for (size_t i = 0; i + 1 < blocks; i++) {
memcpy(block, data + i * 16, 16);
br_aes_ct_cbcenc_run(&keys, iv, block, 16);
}
const uint8_t* last = data + (blocks - 1) * 16;
if (lastComplete) {
for (int i = 0; i < 16; i++) block[i] = last[i] ^ k1[i];
} else {
size_t rem = len - (blocks - 1) * 16;
memset(block, 0, 16);
if (rem > 0) memcpy(block, last, rem);
block[rem] = 0x80;
for (int i = 0; i < 16; i++) block[i] ^= k2[i];
}
br_aes_ct_cbcenc_run(&keys, iv, block, 16);
memcpy(out, block, 16);
}
void smb3_kdf(const uint8_t key[16], const char* label, size_t labelLen,
const char* context, size_t contextLen, uint8_t out[16]) {
// K(1) = HMAC-SHA256(key, [i=1]_32be | label | 0x00 | context | [L=128]_32be)
uint8_t msg[160];
size_t n = 0;
if (labelLen + contextLen + 9 > sizeof(msg)) { memset(out, 0, 16); return; }
msg[n++] = 0; msg[n++] = 0; msg[n++] = 0; msg[n++] = 1;
memcpy(msg + n, label, labelLen); n += labelLen;
msg[n++] = 0;
memcpy(msg + n, context, contextLen); n += contextLen;
msg[n++] = 0; msg[n++] = 0; msg[n++] = 0; msg[n++] = 128;
uint8_t full[32];
hmac_sha256(key, 16, msg, n, nullptr, 0, full);
memcpy(out, full, 16);
}
}