chore: remove base64.c & test_base64.c

This commit is contained in:
d0p1 🏳️‍⚧️ 2024-06-18 11:11:33 +02:00
parent 21d335ed95
commit de26533871
11 changed files with 149 additions and 137 deletions

View file

@ -1,7 +1,6 @@
noinst_LIBRARIES = libstpdfs.a
libstpdfs_a_SOURCES = block.c \
superblock.c \
codec/base64.c \
compression/lzp.c \
crypto/hchacha.c \
crypto/xchacha.c

View file

View file

@ -1,44 +0,0 @@
#include <stddef.h>
#include <stdint.h>
static const char ALPHABET[64] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
size_t
base64_encode(char *dest, const char *src, size_t srclen)
{
size_t encodedlen;
size_t i, j;
uint8_t a, b, c;
uint32_t triple;
encodedlen = 4 * ((srclen + 2) / 3);
if (dest == NULL)
{
return (encodedlen);
}
for (i = 0, j = 0; i < srclen;)
{
a = (i < srclen ? src[i++] : 0);
b = (i < srclen ? src[i++] : 0);
c = (i < srclen ? src[i++] : 0);
triple = (a << 0x10) | (b << 0x08) | c;
dest[j++] = ALPHABET[(triple >> 3 * 6) & 0x3F];
dest[j++] = ALPHABET[(triple >> 2 * 6) & 0x3F];
dest[j++] = ALPHABET[(triple >> 1 * 6) & 0x3F];
dest[j++] = ALPHABET[(triple >> 0 * 6) & 0x3F];
}
if (srclen % 3 != 0)
{
dest[encodedlen - 1] = '=';
if (srclen % 3 == 1)
{
dest[encodedlen - 2] = '=';
}
}
dest[encodedlen] = 0;
return (encodedlen);
}

10
lib/compression/lzp.h Normal file
View file

@ -0,0 +1,10 @@
#ifndef STPDFS_COMPRESSION_LZP_H
# define STPDFS_COMPRESSION_LZP_H 1
# include <stddef.h>
# include <stdint.h>
void lzp_compress(uint8_t *out, size_t *outsz, const uint8_t *in, size_t insz);
void lzp_decompress(uint8_t *out, size_t *outsz, const uint8_t *in, size_t insz);
#endif /* STPDF_COMPRESSION_LZP_H */

View file

@ -18,7 +18,8 @@
# define HCHACHA_OUT_BYTES 32
# define CHACHA_KEY_BYTES 32
# define CHACHA_NONCE_BYTES 16
# define CHACHA_NONCE_BYTES 12
# define HCHACHA_NONCE_BYTES 16
# define CHACHA_BLOCK_BYTES 64
# define TO_LE_32(x) ((x)[0]) | ((x)[1] << 8) | ((x)[2] << 16) | ((x)[3] << 24)
@ -31,6 +32,9 @@ struct chacha_ctx {
void
hchacha(uint8_t out[HCHACHA_OUT_BYTES],
const uint8_t key[CHACHA_KEY_BYTES],
const uint8_t nonce[CHACHA_NONCE_BYTES], int round);
const uint8_t nonce[HCHACHA_NONCE_BYTES], int round);
void
xchacha12(uint8_t *out, uint8_t key[CHACHA_KEY_BYTES], uint8_t nonce[24],
uint8_t *in, size_t inlen);
#endif /* STPDFS_CRYPTO_CHACHA_H */

View file

@ -1,11 +1,20 @@
#ifdef _WIN32
# include <sys/param.h>
# if BYTE_ORDER == LITTLE_ENDIAN
# define htole32(x) (x)
# else
# define htole32(x) __builtin_bswap32(x)
# endif
#else
# include <endian.h>
#endif /* _WIN32 */
#include <stdint.h>
#include "chacha.h"
void
hchacha(uint8_t out[HCHACHA_OUT_BYTES],
const uint8_t key[CHACHA_KEY_BYTES],
const uint8_t nonce[CHACHA_NONCE_BYTES],
const uint8_t nonce[HCHACHA_NONCE_BYTES],
int round)
{
int idx;

View file

@ -1,5 +1,13 @@
#ifdef _WIN32
# include <sys/param.h>
# if BYTE_ORDER == LITTLE_ENDIAN
# define htole32(x) (x)
# else
# define htole32(x) __builtin_bswap32(x)
# endif
#else
# include <endian.h>
#endif /* _WIN32 */
#include <stddef.h>
#include <stdint.h>
#include <string.h>

View file

@ -1,13 +1,12 @@
AM_CPPFLAGS = -I$(top_srcdir)/lib
TESTS = test_lzp test_base64 test_hchacha test_xchacha
check_PROGRAMS = test_lzp test_base64 test_hchacha test_xchacha
TESTS = test_lzp test_hchacha test_xchacha
check_PROGRAMS = test_lzp test_hchacha test_xchacha
LDADD = -lcmocka ../lib/libstpdfs.a
test_lzp_SOURCES = test_lzp.c
test_base64_SOURCES = test_base64.c
test_hchacha_SOURCES = test_hchacha.c

View file

@ -1,57 +0,0 @@
#include <stdarg.h>
#include <stddef.h>
#include <stdint.h>
#include <setjmp.h>
#include <cmocka.h>
#include <string.h>
const char *simple_enc = "SGkh";
const char *simple_dec = "Hi!";
const char *with_pad_enc = "bGlnaHQgdw==";
const char *with_pad_dec = "light w";
size_t base64_encode(char *dest, const char *src, size_t srclen);
static void
test_encode(void **state)
{
size_t len;
char enc[14];
len = base64_encode(enc, simple_dec, strlen(simple_dec));
assert_int_equal(strlen(simple_enc), len);
assert_string_equal(simple_enc, enc);
}
static void
test_encode_with_padding(void **state)
{
size_t len;
char enc[14];
len = base64_encode(enc, with_pad_dec, strlen(with_pad_dec));
assert_int_equal(strlen(with_pad_enc), len);
assert_string_equal(with_pad_enc, enc);
}
static void
test_decode(void **state)
{
(void)state;
}
int
main(void)
{
const struct CMUnitTest tests[] = {
cmocka_unit_test(test_encode),
cmocka_unit_test(test_encode_with_padding),
cmocka_unit_test(test_decode),
};
return cmocka_run_group_tests(tests, NULL, NULL);
}

View file

@ -11,7 +11,7 @@
void
hchacha(uint8_t out[HCHACHA_OUT_BYTES],
const uint8_t key[CHACHA_KEY_BYTES],
const uint8_t nonce[CHACHA_NONCE_BYTES],
const uint8_t nonce[HCHACHA_NONCE_BYTES],
int round);
uint8_t key[CHACHA_KEY_BYTES] = {
@ -20,7 +20,7 @@ uint8_t key[CHACHA_KEY_BYTES] = {
0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f
};
uint8_t nonce[CHACHA_NONCE_BYTES] = {
uint8_t nonce[HCHACHA_NONCE_BYTES] = {
0x00, 0x00, 0x00, 0x09, 0x00, 0x00, 0x00, 0x4a, 0x00, 0x00, 0x00, 0x00, 0x31, 0x41, 0x59, 0x27
};

View file

@ -3,6 +3,7 @@
#include <stdint.h>
#include <setjmp.h>
#include <cmocka.h>
#include <string.h>
#include <crypto/chacha.h>
@ -11,6 +12,8 @@ void xchacha(uint8_t *out, uint8_t key[CHACHA_KEY_BYTES], uint8_t nonce[24],
void chacha_init(struct chacha_ctx *ctx, const uint8_t key[CHACHA_KEY_BYTES],
const uint32_t ctr, const uint8_t nonce[CHACHA_NONCE_BYTES]);
void chacha_block(struct chacha_ctx *ctx, uint8_t *out, int round);
void xchacha12(uint8_t *out, uint8_t key[CHACHA_KEY_BYTES], uint8_t nonce[24],
uint8_t *in, size_t inlen);
uint8_t key[CHACHA_KEY_BYTES] = {
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a,
@ -19,19 +22,20 @@ uint8_t key[CHACHA_KEY_BYTES] = {
};
uint8_t nonce[CHACHA_NONCE_BYTES] = {
0x00, 0x00, 0x00, 0x09, 0x00, 0x00, 0x00, 0x4a, 0x00, 0x00, 0x00, 0x00, 0x31, 0x41, 0x59, 0x27
0x00, 0x00, 0x00, 0x09, 0x00, 0x00, 0x00, 0x4a, 0x00, 0x00, 0x00, 0x00,
};
uint8_t nonce2[24] = {
0xc0, 0x47, 0x54, 0x82, 0x66, 0xb7, 0xc3, 0x70,
0xd3, 0x35, 0x66, 0xa2, 0x42, 0x5c, 0xbf, 0x30,
0xd8, 0x2d, 0x1e, 0xaf, 0x52, 0x94, 0x10, 0x9e
0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47,
0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f,
0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x58
};
uint8_t key2[CHACHA_KEY_BYTES] = {
0xc0, 0x47, 0x54, 0x82, 0x66, 0xb7, 0xc3, 0x70,
0xd3, 0x35, 0x66, 0xa2, 0x42, 0x5c, 0xbf, 0x30,
0xd8, 0x2d, 0x1e, 0xaf, 0x52, 0x94, 0x10, 0x9e
0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f,
0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97,
0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f
};
static void
@ -68,28 +72,107 @@ test_chacha20_block(void **state)
assert_memory_equal(expected, block, CHACHA_BLOCK_BYTES);
}
static void
test_xchacha12_enc_dec(void **state)
{
const char *expected = "Trans Rights are Human Rights";
uint8_t enc[30];
uint8_t dec[30];
xchacha12(enc, key2, nonce2, (uint8_t *)expected, 30);
xchacha12(dec, key2, nonce2, enc, 30);
assert_string_equal(expected, dec);
}
static void
test_xchacha20(void **state)
{
uint8_t expected[] = {
0xa2, 0x12, 0x09, 0x09, 0x65, 0x94, 0xde, 0x8c,
0x56, 0x67, 0xb1, 0xd1, 0x3a, 0xd9, 0x3f, 0x74,
0x41, 0x06, 0xd0, 0x54, 0xdf, 0x21, 0x0e, 0x47,
0x82, 0xcd, 0x39, 0x6f, 0xec, 0x69, 0x2d, 0x35,
0x15, 0xa2, 0x0b, 0xf3, 0x51, 0xee, 0xc0, 0x11,
0xa9, 0x2c, 0x36, 0x78, 0x88, 0xbc, 0x46, 0x4c,
0x32, 0xf0, 0x80, 0x7a, 0xcd, 0x6c, 0x20, 0x3a,
0x24, 0x7e, 0x0d, 0xb8, 0x54, 0x14, 0x84, 0x68,
0xe9, 0xf9, 0x6b, 0xee, 0x4c, 0xf7, 0x18, 0xd6,
0x8d, 0x5f, 0x63, 0x7c, 0xbd, 0x5a, 0x37, 0x64,
0x57, 0x78, 0x8e, 0x6f, 0xae, 0x90, 0xfc, 0x31, 0x09, 0x7c, 0xfc
uint8_t in[304] = {
0x54, 0x68, 0x65, 0x20, 0x64, 0x68, 0x6f, 0x6c,
0x65, 0x20, 0x28, 0x70, 0x72, 0x6f, 0x6e, 0x6f,
0x75, 0x6e, 0x63, 0x65, 0x64, 0x20, 0x22, 0x64,
0x6f, 0x6c, 0x65, 0x22, 0x29, 0x20, 0x69, 0x73,
0x20, 0x61, 0x6c, 0x73, 0x6f, 0x20, 0x6b, 0x6e,
0x6f, 0x77, 0x6e, 0x20, 0x61, 0x73, 0x20, 0x74,
0x68, 0x65, 0x20, 0x41, 0x73, 0x69, 0x61, 0x74,
0x69, 0x63, 0x20, 0x77, 0x69, 0x6c, 0x64, 0x20,
0x64, 0x6f, 0x67, 0x2c, 0x20, 0x72, 0x65, 0x64,
0x20, 0x64, 0x6f, 0x67, 0x2c, 0x20, 0x61, 0x6e,
0x64, 0x20, 0x77, 0x68, 0x69, 0x73, 0x74, 0x6c,
0x69, 0x6e, 0x67, 0x20, 0x64, 0x6f, 0x67, 0x2e,
0x20, 0x49, 0x74, 0x20, 0x69, 0x73, 0x20, 0x61,
0x62, 0x6f, 0x75, 0x74, 0x20, 0x74, 0x68, 0x65,
0x20, 0x73, 0x69, 0x7a, 0x65, 0x20, 0x6f, 0x66,
0x20, 0x61, 0x20, 0x47, 0x65, 0x72, 0x6d, 0x61,
0x6e, 0x20, 0x73, 0x68, 0x65, 0x70, 0x68, 0x65,
0x72, 0x64, 0x20, 0x62, 0x75, 0x74, 0x20, 0x6c,
0x6f, 0x6f, 0x6b, 0x73, 0x20, 0x6d, 0x6f, 0x72,
0x65, 0x20, 0x6c, 0x69, 0x6b, 0x65, 0x20, 0x61,
0x20, 0x6c, 0x6f, 0x6e, 0x67, 0x2d, 0x6c, 0x65,
0x67, 0x67, 0x65, 0x64, 0x20, 0x66, 0x6f, 0x78,
0x2e, 0x20, 0x54, 0x68, 0x69, 0x73, 0x20, 0x68,
0x69, 0x67, 0x68, 0x6c, 0x79, 0x20, 0x65, 0x6c,
0x75, 0x73, 0x69, 0x76, 0x65, 0x20, 0x61, 0x6e,
0x64, 0x20, 0x73, 0x6b, 0x69, 0x6c, 0x6c, 0x65,
0x64, 0x20, 0x6a, 0x75, 0x6d, 0x70, 0x65, 0x72,
0x20, 0x69, 0x73, 0x20, 0x63, 0x6c, 0x61, 0x73,
0x73, 0x69, 0x66, 0x69, 0x65, 0x64, 0x20, 0x77,
0x69, 0x74, 0x68, 0x20, 0x77, 0x6f, 0x6c, 0x76,
0x65, 0x73, 0x2c, 0x20, 0x63, 0x6f, 0x79, 0x6f,
0x74, 0x65, 0x73, 0x2c, 0x20, 0x6a, 0x61, 0x63,
0x6b, 0x61, 0x6c, 0x73, 0x2c, 0x20, 0x61, 0x6e,
0x64, 0x20, 0x66, 0x6f, 0x78, 0x65, 0x73, 0x20,
0x69, 0x6e, 0x20, 0x74, 0x68, 0x65, 0x20, 0x74,
0x61, 0x78, 0x6f, 0x6e, 0x6f, 0x6d, 0x69, 0x63,
0x20, 0x66, 0x61, 0x6d, 0x69, 0x6c, 0x79, 0x20,
0x43, 0x61, 0x6e, 0x69, 0x64, 0x61, 0x65, 0x2e
};
uint8_t out[91];
uint8_t in[91] = { 0 };
uint8_t expected[304] = {
0x7d, 0x0a, 0x2e, 0x6b, 0x7f, 0x7c, 0x65, 0xa2,
0x36, 0x54, 0x26, 0x30, 0x29, 0x4e, 0x06, 0x3b,
0x7a, 0xb9, 0xb5, 0x55, 0xa5, 0xd5, 0x14, 0x9a,
0xa2, 0x1e, 0x4a, 0xe1, 0xe4, 0xfb, 0xce, 0x87,
0xec, 0xc8, 0xe0, 0x8a, 0x8b, 0x5e, 0x35, 0x0a,
0xbe, 0x62, 0x2b, 0x2f, 0xfa, 0x61, 0x7b, 0x20,
0x2c, 0xfa, 0xd7, 0x20, 0x32, 0xa3, 0x03, 0x7e,
0x76, 0xff, 0xdc, 0xdc, 0x43, 0x76, 0xee, 0x05,
0x3a, 0x19, 0x0d, 0x7e, 0x46, 0xca, 0x1d, 0xe0,
0x41, 0x44, 0x85, 0x03, 0x81, 0xb9, 0xcb, 0x29,
0xf0, 0x51, 0x91, 0x53, 0x86, 0xb8, 0xa7, 0x10,
0xb8, 0xac, 0x4d, 0x02, 0x7b, 0x8b, 0x05, 0x0f,
0x7c, 0xba, 0x58, 0x54, 0xe0, 0x28, 0xd5, 0x64,
0xe4, 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71, 0xf7, 0x0b, 0x5b, 0x59, 0x07,
0xa6, 0x6f, 0x7e, 0xa4, 0x9a, 0xad, 0xc4, 0x09
};
uint8_t out[304];
xchacha(out, key2, nonce2, in, 91, 20);
xchacha(out, key2, nonce2, in, 304, 20);
assert_memory_equal(expected, out, 91);
assert_memory_equal(expected, out, 304);
}
int
@ -98,6 +181,7 @@ main(void)
const struct CMUnitTest tests[] = {
cmocka_unit_test(test_chacha_key_init),
cmocka_unit_test(test_chacha20_block),
cmocka_unit_test(test_xchacha12_enc_dec),
cmocka_unit_test(test_xchacha20),
};