Commit 3b844f76 authored by jan.koester's avatar jan.koester
Browse files

tes

parent 329abe21
Loading
Loading
Loading
Loading
+2 −2
Changes for src/CMakeLists.txt: 2 added lines, 2 removed lines.
Original line number Diff line number Diff line
@@ -116,8 +116,8 @@ if(${CMAKE_HOST_SYSTEM_NAME} MATCHES "Windows")
   target_link_libraries(netplus ws2_32)
   target_link_libraries(netplus-static  ws2_32)
else()
   target_link_libraries(netplus pthread crypto) #PUBLIC MbedTLS::mbedcrypto MbedTLS::mbedtls MbedTLS::mbedx509)
   target_link_libraries(netplus-static pthread crypto)
   target_link_libraries(netplus pthread) #PUBLIC MbedTLS::mbedcrypto MbedTLS::mbedtls MbedTLS::mbedx509)
   target_link_libraries(netplus-static pthread)
endif()

set_property(TARGET netplus PROPERTY VERSION ${Upstream_VERSION})
+98 −4
Changes for src/crypto/sha.cpp: 98 added lines, 4 removed lines.
Original line number Diff line number Diff line
#include "sha.h"
#include <cstring>

std::vector<uint8_t> netplus::sha1_hash(const std::vector<uint8_t>& input) {

@@ -139,11 +140,104 @@ std::vector<uint8_t> netplus::sha256_hash(const std::vector<uint8_t>& input) {
    return hash;
}

// SHA-384: Use OpenSSL's proven implementation for correctness
// SHA-384: Native implementation (SHA-512 with different IVs, truncated to 48 bytes)
std::vector<uint8_t> netplus::sha384_hash(const std::vector<uint8_t>& input) {
    unsigned char digest[SHA384_DIGEST_LENGTH];
    SHA384(input.data(), input.size(), digest);
    return std::vector<uint8_t>(digest, digest + SHA384_DIGEST_LENGTH);
    #define SHA512_ROR(value, bits) (((value) >> (bits)) | ((value) << (64 - (bits))))

    #define SHA512_SIG0(x) (SHA512_ROR(x, 1)  ^ SHA512_ROR(x, 8)  ^ ((x) >> 7))
    #define SHA512_SIG1(x) (SHA512_ROR(x, 19) ^ SHA512_ROR(x, 61) ^ ((x) >> 6))
    #define SHA512_EPS0(x) (SHA512_ROR(x, 28) ^ SHA512_ROR(x, 34) ^ SHA512_ROR(x, 39))
    #define SHA512_EPS1(x) (SHA512_ROR(x, 14) ^ SHA512_ROR(x, 18) ^ SHA512_ROR(x, 41))

    // SHA-384 initial hash values (from the fractional parts of the square roots of the 9th-16th primes)
    uint64_t h[8] = {
        0xcbbb9d5dc1059ed8ULL, 0x629a292a367cd507ULL,
        0x9159015a3070dd17ULL, 0x152fecd8f70e5939ULL,
        0x67332667ffc00b31ULL, 0x8eb44a8768581511ULL,
        0xdb0c2e0d64f98fa7ULL, 0x47b5481dbefa4fa4ULL
    };

    // SHA-512 round constants
    static const uint64_t k[80] = {
        0x428a2f98d728ae22ULL, 0x7137449123ef65cdULL, 0xb5c0fbcfec4d3b2fULL, 0xe9b5dba58189dbbcULL,
        0x3956c25bf348b538ULL, 0x59f111f1b605d019ULL, 0x923f82a4af194f9bULL, 0xab1c5ed5da6d8118ULL,
        0xd807aa98a3030242ULL, 0x12835b0145706fbeULL, 0x243185be4ee4b28cULL, 0x550c7dc3d5ffb4e2ULL,
        0x72be5d74f27b896fULL, 0x80deb1fe3b1696b1ULL, 0x9bdc06a725c71235ULL, 0xc19bf174cf692694ULL,
        0xe49b69c19ef14ad2ULL, 0xefbe4786384f25e3ULL, 0x0fc19dc68b8cd5b5ULL, 0x240ca1cc77ac9c65ULL,
        0x2de92c6f592b0275ULL, 0x4a7484aa6ea6e483ULL, 0x5cb0a9dcbd41fbd4ULL, 0x76f988da831153b5ULL,
        0x983e5152ee66dfabULL, 0xa831c66d2db43210ULL, 0xb00327c898fb213fULL, 0xbf597fc7beef0ee4ULL,
        0xc6e00bf33da88fc2ULL, 0xd5a79147930aa725ULL, 0x06ca6351e003826fULL, 0x142929670a0e6e70ULL,
        0x27b70a8546d22ffcULL, 0x2e1b21385c26c926ULL, 0x4d2c6dfc5ac42aedULL, 0x53380d139d95b3dfULL,
        0x650a73548baf63deULL, 0x766a0abb3c77b2a8ULL, 0x81c2c92e47edaee6ULL, 0x92722c851482353bULL,
        0xa2bfe8a14cf10364ULL, 0xa81a664bbc423001ULL, 0xc24b8b70d0f89791ULL, 0xc76c51a30654be30ULL,
        0xd192e819d6ef5218ULL, 0xd69906245565a910ULL, 0xf40e35855771202aULL, 0x106aa07032bbd1b8ULL,
        0x19a4c116b8d2d0c8ULL, 0x1e376c085141ab53ULL, 0x2748774cdf8eeb99ULL, 0x34b0bcb5e19b48a8ULL,
        0x391c0cb3c5c95a63ULL, 0x4ed8aa4ae3418acbULL, 0x5b9cca4f7763e373ULL, 0x682e6ff3d6b2b8a3ULL,
        0x748f82ee5defb2fcULL, 0x78a5636f43172f60ULL, 0x84c87814a1f0ab72ULL, 0x8cc702081a6439ecULL,
        0x90befffa23631e28ULL, 0xa4506cebde82bde9ULL, 0xbef9a3f7b2c67915ULL, 0xc67178f2e372532bULL,
        0xca273eceea26619cULL, 0xd186b8c721c0c207ULL, 0xeada7dd6cde0eb1eULL, 0xf57d4f7fee6ed178ULL,
        0x06f067aa72176fbaULL, 0x0a637dc5a2c898a6ULL, 0x113f9804bef90daeULL, 0x1b710b35131c471bULL,
        0x28db77f523047d84ULL, 0x32caab7b40c72493ULL, 0x3c9ebe0a15c9bebcULL, 0x431d67c49c100d4cULL,
        0x4cc5d4becb3e42b6ULL, 0x597f299cfc657e2aULL, 0x5fcb6fab3ad6faecULL, 0x6c44198c4a475817ULL
    };

    // Pre-processing (Padding)
    std::vector<uint8_t> msg = input;
    __uint128_t bitLen = (__uint128_t)input.size() * 8;
    msg.push_back(0x80);
    while ((msg.size() + 16) % 128 != 0) msg.push_back(0x00);

    // Append length as 128-bit big-endian
    for (int i = 15; i >= 0; --i) msg.push_back((uint8_t)((bitLen >> (i * 8)) & 0xFF));

    // Process in 1024-bit blocks
    for (size_t offset = 0; offset < msg.size(); offset += 128) {
        uint64_t w[80];
        for (int i = 0; i < 16; ++i) {
            w[i] = ((uint64_t)msg[offset + i * 8    ] << 56) | ((uint64_t)msg[offset + i * 8 + 1] << 48) |
                   ((uint64_t)msg[offset + i * 8 + 2] << 40) | ((uint64_t)msg[offset + i * 8 + 3] << 32) |
                   ((uint64_t)msg[offset + i * 8 + 4] << 24) | ((uint64_t)msg[offset + i * 8 + 5] << 16) |
                   ((uint64_t)msg[offset + i * 8 + 6] <<  8) | ((uint64_t)msg[offset + i * 8 + 7]);
        }
        for (int i = 16; i < 80; ++i) {
            w[i] = SHA512_SIG1(w[i - 2]) + w[i - 7] + SHA512_SIG0(w[i - 15]) + w[i - 16];
        }

        uint64_t a = h[0], b = h[1], c = h[2], d = h[3],
                 e = h[4], f = h[5], g = h[6], l = h[7];

        for (int i = 0; i < 80; ++i) {
            uint64_t t1 = l + SHA512_EPS1(e) + ((e & f) ^ (~e & g)) + k[i] + w[i];
            uint64_t t2 = SHA512_EPS0(a) + ((a & b) ^ (a & c) ^ (b & c));
            l = g; g = f; f = e;
            e = d + t1;
            d = c; c = b; b = a;
            a = t1 + t2;
        }

        h[0] += a; h[1] += b; h[2] += c; h[3] += d;
        h[4] += e; h[5] += f; h[6] += g; h[7] += l;
    }

    // Produce final hash - SHA-384 uses first 6 words of SHA-512 state (48 bytes)
    std::vector<uint8_t> hash(48);
    for (int i = 0; i < 6; ++i) {
        hash[i * 8]     = (h[i] >> 56) & 0xFF;
        hash[i * 8 + 1] = (h[i] >> 48) & 0xFF;
        hash[i * 8 + 2] = (h[i] >> 40) & 0xFF;
        hash[i * 8 + 3] = (h[i] >> 32) & 0xFF;
        hash[i * 8 + 4] = (h[i] >> 24) & 0xFF;
        hash[i * 8 + 5] = (h[i] >> 16) & 0xFF;
        hash[i * 8 + 6] = (h[i] >>  8) & 0xFF;
        hash[i * 8 + 7] = h[i] & 0xFF;
    }
    return hash;

    #undef SHA512_ROR
    #undef SHA512_SIG0
    #undef SHA512_SIG1
    #undef SHA512_EPS0
    #undef SHA512_EPS1
}

// HMAC-SHA256 implementation