/* * internal.hpp * SMB client library internals: byte order, crypto, NTLM, SPNEGO * Copyright (c) 2026 Daniel Hammer */ #pragma once #include #include namespace smb::detail { // ---- Little-endian accessors (the SMB wire format) ---- inline void put16(uint8_t* p, uint16_t v) { p[0] = (uint8_t)v; p[1] = (uint8_t)(v >> 8); } inline void put32(uint8_t* p, uint32_t v) { p[0] = (uint8_t)v; p[1] = (uint8_t)(v >> 8); p[2] = (uint8_t)(v >> 16); p[3] = (uint8_t)(v >> 24); } inline void put64(uint8_t* p, uint64_t v) { put32(p, (uint32_t)v); put32(p + 4, (uint32_t)(v >> 32)); } inline uint16_t get16(const uint8_t* p) { return (uint16_t)(p[0] | (p[1] << 8)); } inline uint32_t get32(const uint8_t* p) { return (uint32_t)p[0] | ((uint32_t)p[1] << 8) | ((uint32_t)p[2] << 16) | ((uint32_t)p[3] << 24); } inline uint64_t get64(const uint8_t* p) { return (uint64_t)get32(p) | ((uint64_t)get32(p + 4) << 32); } // ---- Time ---- // Windows FILETIME: 100 ns ticks since 1601-01-01 UTC. static constexpr uint64_t FILETIME_UNIX_EPOCH = 116444736000000000ULL; inline int64_t filetime_to_unix(uint64_t ft) { if (ft == 0) return 0; if (ft < FILETIME_UNIX_EPOCH) return 0; return (int64_t)((ft - FILETIME_UNIX_EPOCH) / 10000000ULL); } inline uint64_t unix_to_filetime(int64_t t) { if (t <= 0) return 0; return (uint64_t)t * 10000000ULL + FILETIME_UNIX_EPOCH; } // ---- Text ---- // UTF-8 -> UTF-16LE. Returns the number of bytes written (no terminator), // or -1 if `cap` is too small. With `path` set, '/' becomes '\' and // leading / trailing / doubled separators are dropped. int utf8_to_utf16(const char* in, uint8_t* out, uint32_t cap, bool path = false); // UTF-16LE (`len` bytes) -> NUL-terminated UTF-8, truncated to fit `cap`. void utf16_to_utf8(const uint8_t* in, uint32_t len, char* out, uint32_t cap); // ---- Crypto (crypto.cpp) ---- void md4(const uint8_t* data, size_t len, uint8_t out[16]); // HMAC over the concatenation of two segments (either may be empty). 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]); 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]); void aes_cmac(const uint8_t key[16], const uint8_t* data, size_t len, uint8_t out[16]); // SMB 3.0 key derivation (SP800-108 counter mode, HMAC-SHA256, 128 bits). // label and context include their terminating NUL. void smb3_kdf(const uint8_t key[16], const char* label, size_t labelLen, const char* context, size_t contextLen, uint8_t out[16]); // ---- NTLMSSP + SPNEGO (ntlm.cpp) ---- static constexpr size_t NTLM_NEGOTIATE_LEN = 32; void ntlm_negotiate(uint8_t out[NTLM_NEGOTIATE_LEN]); struct NtlmAuthParams { const char* user; // "" with anonymous set const char* password; const char* domain; bool anonymous; uint8_t clientChallenge[8]; uint64_t fallbackTime; // FILETIME used if the server sent no timestamp }; // Build the AUTHENTICATE message for a server CHALLENGE. Returns its // length or a negative Status. sessionKey receives the 16-byte session // base key (all zero for an anonymous logon). int ntlm_authenticate(const uint8_t* challenge, size_t challengeLen, const NtlmAuthParams& p, uint8_t* out, size_t outCap, uint8_t sessionKey[16]); // Wrap an NTLM token in a SPNEGO NegTokenInit (first leg) or NegTokenResp // (later legs). Returns the blob length or -1 if it does not fit. int spnego_wrap(const uint8_t* token, size_t tokenLen, bool first, uint8_t* out, size_t outCap); // Find the NTLM token inside a server security blob (SPNEGO or raw). bool spnego_unwrap(const uint8_t* blob, size_t len, const uint8_t** token, size_t* tokenLen); }