tls

tls.git
git clone git://git.lenczewski.org/tls.git
Log | Files | Refs

commit 1a5b5acc814fbd49f9b090809615063960f4db96
parent 0bd0fc1f03b21c8ee7abe98c231f4561b673d941
Author: MikoĊ‚aj Lenczewski <mikolaj@lenczewski.org>
Date:   Wed, 16 Sep 2026 00:03:29 +0100

Add sha256 implementation

Diffstat:
M.gitignore | 3+++
MNOTES.txt | 12++++++++++++
Mbuild.sh | 13+++++++++++--
Mclean.sh | 2+-
Asha.c | 317+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Asha.h | 165+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Asha.pdf | 0
Atest-sha256.c | 36++++++++++++++++++++++++++++++++++++
Rtest-client.c -> tls_client.c | 0
Rtest-server.c -> tls_server.c | 0
10 files changed, 545 insertions(+), 3 deletions(-)

diff --git a/.gitignore b/.gitignore @@ -1,3 +1,6 @@ +bin/ +test/ + **/.*.swp imgui.ini tags diff --git a/NOTES.txt b/NOTES.txt @@ -1,6 +1,18 @@ +https://jvns.ca/blog/2022/03/23/a-toy-version-of-tls/ https://datatracker.ietf.org/doc/html/rfc8446 https://tls13.xargs.org/ + https://datatracker.ietf.org/doc/html/rfc2246 https://tls12.xargs.org/ + https://datatracker.ietf.org/doc/html/rfc7748 https://x25519.xargs.org/ + +# hmac and hkdf +https://datatracker.ietf.org/doc/html/rfc2104 +https://datatracker.ietf.org/doc/html/rfc5869 + +# aes-{128,256}-gcm, chacha20, and sha256/sha384 +https://datatracker.ietf.org/doc/html/rfc5116 +https://datatracker.ietf.org/doc/html/rfc8439 +https://nvlpubs.nist.gov/nistpubs/FIPS/NIST.FIPS.180-4.pdf diff --git a/build.sh b/build.sh @@ -2,5 +2,14 @@ set -ex -cc -o test-client test-client.c -std=c11 -O0 -g3 -Wall -Wextra -cc -o test-server test-server.c -std=c11 -O0 -g3 -Wall -Wextra +CC="${CC:-clang}" +FLAGS="-std=c11 -O0 -g3 -Wall -Wextra -Wno-unused-parameter -Wno-format" + +mkdir -p bin test + +for src in test-*.c; do + cc -o test/$(basename -s .c $src) $src $FLAGS +done + +cc -o bin/tls_client tls_client.c $FLAGS +cc -o bin/tls_server tls_server.c $FLAGS diff --git a/clean.sh b/clean.sh @@ -2,4 +2,4 @@ set -ex -rm -f test-client test-server +rm -rf bin/ diff --git a/sha.c b/sha.c @@ -0,0 +1,317 @@ +#include "sha.h" + +// sha constants +// =========================================================================== + +static sha256_word_t const _sha256_constants[] = { + 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, + 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5, + 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, + 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174, + 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, + 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da, + 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, + 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967, + 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, + 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85, + 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, + 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070, + 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, + 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3, + 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, + 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2, +}; + +static sha512_word_t const _sha512_constants[] = { + 0x428a2f98d728ae22, 0x7137449123ef65cd, 0xb5c0fbcfec4d3b2f, 0xe9b5dba58189dbbc, + 0x3956c25bf348b538, 0x59f111f1b605d019, 0x923f82a4af194f9b, 0xab1c5ed5da6d8118, + 0xd807aa98a3030242, 0x12835b0145706fbe, 0x243185be4ee4b28c, 0x550c7dc3d5ffb4e2, + 0x72be5d74f27b896f, 0x80deb1fe3b1696b1, 0x9bdc06a725c71235, 0xc19bf174cf692694, + 0xe49b69c19ef14ad2, 0xefbe4786384f25e3, 0x0fc19dc68b8cd5b5, 0x240ca1cc77ac9c65, + 0x2de92c6f592b0275, 0x4a7484aa6ea6e483, 0x5cb0a9dcbd41fbd4, 0x76f988da831153b5, + 0x983e5152ee66dfab, 0xa831c66d2db43210, 0xb00327c898fb213f, 0xbf597fc7beef0ee4, + 0xc6e00bf33da88fc2, 0xd5a79147930aa725, 0x06ca6351e003826f, 0x142929670a0e6e70, + 0x27b70a8546d22ffc, 0x2e1b21385c26c926, 0x4d2c6dfc5ac42aed, 0x53380d139d95b3df, + 0x650a73548baf63de, 0x766a0abb3c77b2a8, 0x81c2c92e47edaee6, 0x92722c851482353b, + 0xa2bfe8a14cf10364, 0xa81a664bbc423001, 0xc24b8b70d0f89791, 0xc76c51a30654be30, + 0xd192e819d6ef5218, 0xd69906245565a910, 0xf40e35855771202a, 0x106aa07032bbd1b8, + 0x19a4c116b8d2d0c8, 0x1e376c085141ab53, 0x2748774cdf8eeb99, 0x34b0bcb5e19b48a8, + 0x391c0cb3c5c95a63, 0x4ed8aa4ae3418acb, 0x5b9cca4f7763e373, 0x682e6ff3d6b2b8a3, + 0x748f82ee5defb2fc, 0x78a5636f43172f60, 0x84c87814a1f0ab72, 0x8cc702081a6439ec, + 0x90befffa23631e28, 0xa4506cebde82bde9, 0xbef9a3f7b2c67915, 0xc67178f2e372532b, + 0xca273eceea26619c, 0xd186b8c721c0c207, 0xeada7dd6cde0eb1e, 0xf57d4f7fee6ed178, + 0x06f067aa72176fba, 0x0a637dc5a2c898a6, 0x113f9804bef90dae, 0x1b710b35131c471b, + 0x28db77f523047d84, 0x32caab7b40c72493, 0x3c9ebe0a15c9bebc, 0x431d67c49c100d4c, + 0x4cc5d4becb3e42b6, 0x597f299cfc657e2a, 0x5fcb6fab3ad6faec, 0x6c44198c4a475817, +}; + +// sha256 +// =========================================================================== + +void +sha256_init(struct sha256_ctx *ctx) +{ + memset(ctx, 0, sizeof *ctx); + + ctx->digest[0] = 0x6a09e667; + ctx->digest[1] = 0xbb67ae85; + ctx->digest[2] = 0x3c6ef372; + ctx->digest[3] = 0xa54ff53a; + ctx->digest[4] = 0x510e527f; + ctx->digest[5] = 0x9b05688c; + ctx->digest[6] = 0x1f83d9ab; + ctx->digest[7] = 0x5be0cd19; +} + +static inline void +_sha256_block(struct sha256_ctx *ctx) +{ + sha256_word_t a, b, c, d, e, f, g, h, t, t1, t2; + + // load block words + for (t = 0; t < SHA256_BLOCK_WORDS; t++) { + ctx->schedule[t] = htobe32(ctx->schedule[t]); + } + + // expand out block words into full schedule + for (; t < SHA256_SCHEDULE_WORDS; t++) { + ctx->schedule[t] = SHA256_s1(ctx->schedule[t - 2]) + + ctx->schedule[t - 7] + + SHA256_s0(ctx->schedule[t - 15]) + + ctx->schedule[t - 16]; + } + + // initialise working variables + a = ctx->digest[0]; + b = ctx->digest[1]; + c = ctx->digest[2]; + d = ctx->digest[3]; + e = ctx->digest[4]; + f = ctx->digest[5]; + g = ctx->digest[6]; + h = ctx->digest[7]; + + // perform rounds of sha256 compression (as many as words in the schedule) + for (t = 0; t < SHA256_SCHEDULE_WORDS; t++) { + t1 = h + + SHA256_S1(e) + + SHA256_Ch(e, f, g) + + _sha256_constants[t] + + ctx->schedule[t]; + + t2 = SHA256_S0(a) + + SHA256_Maj(a, b, c); + + h = g; + g = f; + f = e; + e = d + t1; + d = c; + c = b; + b = a; + a = t1 + t2; + } + + // save intermediate hash + ctx->digest[0] += a; + ctx->digest[1] += b; + ctx->digest[2] += c; + ctx->digest[3] += d; + ctx->digest[4] += e; + ctx->digest[5] += f; + ctx->digest[6] += g; + ctx->digest[7] += h; +} + +void +sha256_push(struct sha256_ctx *ctx, void const *buf, size_t len) +{ + unsigned char const *ptr = buf, *end = ptr + len; + unsigned char *block = ((unsigned char *) ctx->schedule); + + while (ptr < end) { + size_t idx = ctx->ctr++ & SHA256_BLOCK_MASK; + block[idx] = *ptr++; + + if (idx == SHA256_BLOCK_MASK) { // filled a block; hash it + _sha256_block(ctx); + } + } +} + +static inline void +_sha256_digest_bswap(struct sha256_ctx *ctx, size_t words) +{ + for (size_t i = 0; i < words; i++) + ctx->digest[i] = htobe32(ctx->digest[i]); +} + +void +sha256_dgst(struct sha256_ctx *ctx) +{ + unsigned char *block = ((unsigned char *) ctx->schedule); + unsigned char *cur = block + (ctx->ctr & SHA256_BLOCK_MASK); + unsigned char *end = block + SHA256_BLOCK_SIZE; + unsigned char *len = end - (64 / CHAR_BIT); + + // TODO: handle 64-bit overflow here + assert(ctx->ctr < (UINT64_MAX / CHAR_BIT)); + uint64_t bits = ctx->ctr * CHAR_BIT; + + // append the 1 bit + *cur++ = 0x80; + + // can we fit our message bit length? if not, add an extra padding block + if (len < cur) { + memset(cur, 0, end - cur); + _sha256_block(ctx); + + cur = block; + } + + // pad out final block + memset(cur, 0, len - cur); + + // write out message bit length + ((uint64_t *) len)[0] = htobe64(bits); + + // hash final block + _sha256_block(ctx); + + // write out internally host-endian hash as big-endian hash + _sha256_digest_bswap(ctx, SHA256_DIGEST_WORDS); +} + +void +sha256(void const *buf, size_t len, unsigned char digest[static SHA256_DIGEST_SIZE]) +{ + struct sha256_ctx ctx; + sha256_init(&ctx); + sha256_push(&ctx, buf, len); + sha256_dgst(&ctx); + + memcpy(digest, ctx.digest, SHA256_DIGEST_SIZE); +} + +// sha 512 +// =========================================================================== + +void +sha512_init(struct sha512_ctx *ctx) +{ + ctx->digest[0] = 0x6a09e667f3bcc908; + ctx->digest[1] = 0xbb67ae8584caa73b; + ctx->digest[2] = 0x3c6ef372fe94f82b; + ctx->digest[3] = 0xa54ff53a5f1d36f1; + ctx->digest[4] = 0x510e527fade682d1; + ctx->digest[5] = 0x9b05688c2b3e6c1f; + ctx->digest[6] = 0x1f83d9abfb41bd6b; + ctx->digest[7] = 0x5be0cd19137e2179; +} + +static inline void +_sha512_block() +{ +} + +void +sha512_push(struct sha512_ctx *ctx, void const *buf, size_t len) +{ +} + +static inline void +_sha512_digest_bswap(struct sha512_ctx *ctx, size_t len) +{ + for (size_t i = 0; i < len; i++) + ctx->digest[i] = htobe64(ctx->digest[i]); +} + +void +sha512_dgst(struct sha512_ctx *ctx) +{ +} + +void +sha512(void const *buf, size_t len, unsigned char digest[static SHA512_DIGEST_SIZE]) +{ + struct sha512_ctx ctx; + sha512_init(&ctx); + sha512_push(&ctx, buf, len); + sha512_dgst(&ctx); + + memcpy(digest, ctx.digest, SHA512_DIGEST_SIZE); +} + +// sha 384 +// =========================================================================== + +void +sha384_init(struct sha512_ctx *ctx) +{ + ctx->digest[0] = 0xcbbb9d5dc1059ed8; + ctx->digest[1] = 0x629a292a367cd507; + ctx->digest[2] = 0x9159015a3070dd17; + ctx->digest[3] = 0x152fecd8f70e5939; + ctx->digest[4] = 0x67332667ffc00b31; + ctx->digest[5] = 0x8eb44a8768581511; + ctx->digest[6] = 0xdb0c2e0d64f98fa7; + ctx->digest[7] = 0x47b5481dbefa4fa4; +} + +void +sha384_push(struct sha512_ctx *ctx, void const *buf, size_t len) +{ +} + +void +sha384_dgst(struct sha512_ctx *ctx) +{ +} + +void +sha384(void const *buf, size_t len, unsigned char digest[static SHA384_DIGEST_SIZE]) +{ + struct sha512_ctx ctx; + sha384_init(&ctx); + sha384_push(&ctx, buf, len); + sha384_dgst(&ctx); + + memcpy(digest, ctx.digest, SHA384_DIGEST_SIZE); +} + +// sha512/256 +// =========================================================================== + +void +sha512_256_init(struct sha512_ctx *ctx) +{ + ctx->digest[0] = 0x22312194fc2bf72c; + ctx->digest[1] = 0x9f555fa3c84c64c2; + ctx->digest[2] = 0x2393b86b6f53b151; + ctx->digest[3] = 0x963877195940eabd; + ctx->digest[4] = 0x96283ee2A88effe3; + ctx->digest[5] = 0xbe5e1e2553863992; + ctx->digest[6] = 0x2b0199fc2c85b8aa; + ctx->digest[7] = 0x0eb72ddc81c52ca2; +} + +void +sha512_256_push(struct sha512_ctx *ctx, void const *buf, size_t len) +{ +} + +void +sha512_256_dgst(struct sha512_ctx *ctx) +{ +} + +void +sha512_256(void const *buf, size_t len, unsigned char digest[static SHA512_256_DIGEST_SIZE]) +{ + struct sha512_ctx ctx; + sha512_256_init(&ctx); + sha512_256_push(&ctx, buf, len); + sha512_256_dgst(&ctx); + + memcpy(digest, ctx.digest, SHA512_256_DIGEST_SIZE); +} diff --git a/sha.h b/sha.h @@ -0,0 +1,165 @@ +// https://nvlpubs.nist.gov/nistpubs/FIPS/NIST.FIPS.180-4.pdf + +#ifndef SHA_H +#define SHA_H + +#ifndef _GNU_SOURCE +# define _GNU_SOURCE 1 +#endif + +#include <assert.h> +#include <endian.h> +#include <limits.h> +#include <stdalign.h> +#include <stdint.h> +#include <string.h> + +// sha definitions and constants +// =========================================================================== + +#define SHA_ALIGN 64 // align structures to a 64-byte/512-bit boundary + +typedef uint32_t sha256_word_t; + +#define SHA256_WORD_BITS 32 +#define SHA256_BLOCK_BITS 512 +#define SHA256_DIGEST_BITS 256 +#define SHA256_SCHEDULE_BITS 2048 + +#define SHA256_BLOCK_WORDS (SHA256_BLOCK_BITS / SHA256_WORD_BITS) +#define SHA256_BLOCK_SIZE (SHA256_BLOCK_BITS / CHAR_BIT) +#define SHA256_BLOCK_MASK (SHA256_BLOCK_SIZE - 1) + +#define SHA256_DIGEST_WORDS (SHA256_DIGEST_BITS / SHA256_WORD_BITS) +#define SHA256_DIGEST_SIZE (SHA256_DIGEST_BITS / CHAR_BIT) + +#define SHA256_SCHEDULE_WORDS (SHA256_SCHEDULE_BITS / SHA256_WORD_BITS) +#define SHA256_SCHEDULE_SIZE (SHA256_SCHEDULE_BITS / CHAR_BIT) + +struct sha256_ctx { + alignas(SHA_ALIGN) + sha256_word_t schedule[SHA256_SCHEDULE_WORDS]; + sha256_word_t digest[SHA256_DIGEST_WORDS]; + size_t ctr; +}; + +typedef uint64_t sha512_word_t; + +#define SHA512_WORD_BITS 64 +#define SHA512_BLOCK_BITS 1024 +#define SHA512_DIGEST_BITS 512 +#define SHA512_SCHEDULE_BITS 5120 + +#define SHA512_BLOCK_WORDS (SHA512_BLOCK_BITS / SHA512_WORD_BITS) +#define SHA512_BLOCK_SIZE (SHA512_BLOCK_BITS / CHAR_BIT) +#define SHA512_BLOCK_MASK (SHA512_BLOCK_SIZE - 1) + +#define SHA512_DIGEST_WORDS (SHA512_DIGEST_BITS / SHA512_WORD_BITS) +#define SHA512_DIGEST_SIZE (SHA512_DIGEST_BITS / CHAR_BIT) + +#define SHA512_SCHEDULE_WORDS (SHA512_SCHEDULE_BITS / SHA512_WORD_BITS) +#define SHA512_SCHEDULE_SIZE (SHA512_SCHEDULE_BITS / CHAR_BIT) + +struct sha512_ctx { + alignas(SHA_ALIGN) + sha512_word_t schedule[SHA512_SCHEDULE_WORDS]; + sha512_word_t block[SHA512_BLOCK_WORDS]; + sha512_word_t digest[SHA512_DIGEST_WORDS]; + size_t ctr; +}; + +// sha384 re-uses the sha512 definitions +#define SHA384_DIGEST_BITS 384 +#define SHA384_DIGEST_WORDS (SHA384_DIGEST_BITS / SHA512_WORD_BITS) +#define SHA384_DIGEST_SIZE (SHA384_DIGEST_BITS / CHAR_BIT) + +// sha512/256 re-uses the sha512 definitions +#define SHA512_256_DIGEST_BITS 256 +#define SHA512_256_DIGEST_WORDS (SHA512_256_DIGEST_BITS / SHA512_WORD_BITS) +#define SHA512_256_DIGEST_SIZE (SHA512_256_DIGEST_BITS / CHAR_BIT) + +// sha operations +// =========================================================================== + +#define SHA_ROTR32(x, n) (((x) >> (n)) | ((x) << (32 - (n)))) +#define SHA_ROTR64(x, n) (((x) >> (n)) | ((x) << (64 - (n)))) +#define SHA_SHR(x, n) ((x) >> (n)) + +// sha functions +// =========================================================================== + +#define SHA256_Ch(x, y, z) (((x) & (y)) ^ ((~(x)) & (z))) +#define SHA256_Maj(x, y, z) (((x) & (y)) ^ ((x) & (z)) ^ ((y) & (z))) +#define SHA256_S0(x) (SHA_ROTR32((x), 2) ^ SHA_ROTR32((x), 13) ^ SHA_ROTR32((x), 22)) +#define SHA256_S1(x) (SHA_ROTR32((x), 6) ^ SHA_ROTR32((x), 11) ^ SHA_ROTR32((x), 25)) +#define SHA256_s0(x) (SHA_ROTR32((x), 7) ^ SHA_ROTR32((x), 18) ^ SHA_SHR((x), 3)) +#define SHA256_s1(x) (SHA_ROTR32((x), 17) ^ SHA_ROTR32((x), 19) ^ SHA_SHR((x), 10)) + +#define SHA512_Ch(x, y, z) (((x) & (y)) ^ ((~(x)) & (z))) +#define SHA512_Maj(x, y, z) (((x) ^ (y)) ^ ((x) & (z)) ^ ((y) & (z))) +#define SHA512_S0(x) (SHA_ROTR64((x), 28) ^ SHA_ROTR64((x), 34) ^ SHA_ROTR64((x), 39)) +#define SHA512_S1(x) (SHA_ROTR64((x), 14) ^ SHA_ROTR64((x), 18) ^ SHA_ROTR64((x), 41)) +#define SHA512_s0(x) (SHA_ROTR64((x), 1) ^ SHA_ROTR64((x), 8) ^ SHA_SHR((x), 7)) +#define SHA512_s1(x) (SHA_ROTR64((x), 19) ^ SHA_ROTR64((x), 61) ^ SHA_SHR((x), 6)) + +// sha256 +// =========================================================================== + +void +sha256_init(struct sha256_ctx *ctx); + +void +sha256_push(struct sha256_ctx *ctx, void const *buf, size_t len); + +void +sha256_dgst(struct sha256_ctx *ctx); + +void +sha256(void const *buf, size_t len, unsigned char digest[static SHA256_DIGEST_SIZE]); + +// sha 512 +// =========================================================================== + +void +sha512_init(struct sha512_ctx *ctx); + +void +sha512_push(struct sha512_ctx *ctx, void const *buf, size_t len); + +void +sha512_dgst(struct sha512_ctx *ctx); + +void +sha512(void const *buf, size_t len, unsigned char digest[static SHA512_DIGEST_SIZE]); + +// sha 384 +// =========================================================================== + +void +sha384_init(struct sha512_ctx *ctx); + +void +sha384_push(struct sha512_ctx *ctx, void const *buf, size_t len); + +void +sha384_dgst(struct sha512_ctx *ctx); + +void +sha384(void const *buf, size_t len, unsigned char digest[static SHA384_DIGEST_SIZE]); + +// sha512/256 +// =========================================================================== + +void +sha512_256_init(struct sha512_ctx *ctx); + +void +sha512_256_push(struct sha512_ctx *ctx, void const *buf, size_t len); + +void +sha512_256_dgst(struct sha512_ctx *ctx); + +void +sha512_256(void const *buf, size_t len, unsigned char digest[static SHA512_256_DIGEST_SIZE]); + +#endif /* SHA_H */ diff --git a/sha.pdf b/sha.pdf Binary files differ. diff --git a/test-sha256.c b/test-sha256.c @@ -0,0 +1,36 @@ +#include "sha.h" +#include <stdio.h> + +int +main(void) +{ + char msg[] = "abc"; + + uint8_t dgst[SHA256_DIGEST_SIZE]; + sha256(msg, strlen(msg), dgst); + + uint8_t expected[SHA256_DIGEST_SIZE] = { + 0xba, 0x78, 0x16, 0xbf, 0x8f, 0x01, 0xcf, 0xea, + 0x41, 0x41, 0x40, 0xde, 0x5d, 0xae, 0x22, 0x23, + 0xb0, 0x03, 0x61, 0xa3, 0x96, 0x17, 0x7a, 0x9c, + 0xb4, 0x10, 0xff, 0x61, 0xf2, 0x00, 0x15, 0xad, + }; + + printf("msg: \"%.*s\" (%lu bytes)\n", sizeof msg, msg, strlen(msg)); + + printf("dgst: "); + for (size_t i = 0; i < sizeof dgst; i++) { + printf("%.2x", dgst[i]); + } + printf("\n"); + + printf("exp.: "); + for (size_t i = 0; i < sizeof expected; i++) { + printf("%.2x", expected[i]); + } + printf("\n"); + + return memcmp(expected, dgst, SHA256_DIGEST_SIZE); +} + +#include "sha.c" diff --git a/test-client.c b/tls_client.c diff --git a/test-server.c b/tls_server.c