diff --git a/include/codecrypt.h b/include/codecrypt.h index 7b73816..6d7f2a7 100644 --- a/include/codecrypt.h +++ b/include/codecrypt.h @@ -2,12 +2,15 @@ #ifndef _CODECRYPT_H_ #define _CODECRYPT_H_ +#include +#include + #ifdef __cplusplus extern "C" { #endif /* codecrypt matrix/vector/whatever type */ - typedef char* ccr_mtx; + typedef uint8_t* ccr_mtx; /* permutation as a list of transpositions */ typedef int* ccr_perm; @@ -67,12 +70,15 @@ extern "C" { /* actual functions */ int ccr_mce_gen (struct ccr_mce_pubkey*, struct ccr_mce_privkey*); - int ccr_mce_encrypt (struct ccr_mce_pubkey*, const char*, char*); - int ccr_mce_decrypt (struct ccr_mce_privkey*, const char*, char*); + int ccr_mce_encrypt (struct ccr_mce_pubkey*, const uint8_t*, uint8_t*); + int ccr_mce_decrypt (struct ccr_mce_privkey*, const uint8_t*, uint8_t*); int ccr_nd_gen (struct ccr_nd_pubkey*, struct ccr_nd_privkey*); - int ccr_nd_encrypt (struct ccr_nd_privkey*, const char*, char*); - int ccr_nd_decrypt (struct ccr_nd_pubkey*, const char*, char*); + int ccr_nd_encrypt (struct ccr_nd_privkey*, const uint8_t*, uint8_t*); + int ccr_nd_decrypt (struct ccr_nd_pubkey*, const uint8_t*, uint8_t*); + + void ccr_set_log_func (void (*) (const char*) ); + void ccr_set_internal_allocator (void* (*) (size_t), void (*) (void*) ); #ifdef __cplusplus } diff --git a/lib/log.c b/lib/log.c index b243cca..01f7ac4 100644 --- a/lib/log.c +++ b/lib/log.c @@ -2,15 +2,15 @@ #include "codecrypt.h" #include "log.h" -typedef void(*)(const char*) logfunc; +static void (*global_log) (const char*) = NULL; -static logfunc global_log=NULL; - -//TODO export -void ccr_set_log_func(logfunc x) { - global_log=x; +void ccr_set_log_func (void (*x) (const char*) ) +{ + global_log = x; } -void ccr_log(const char*, ...) { - +void ccr_log (const char* fmt, ...) +{ + if (!global_log) return; + //TODO } diff --git a/lib/log.h b/lib/log.h index c2e7269..7bfb193 100644 --- a/lib/log.h +++ b/lib/log.h @@ -2,7 +2,7 @@ #ifndef _CCR_LOG_H_ #define _CCR_LOG_H_ -void ccr_log(const char*, ...); +void ccr_log (const char*, ...); #endif diff --git a/lib/math.c b/lib/math.c index e69de29..9cc67ff 100644 --- a/lib/math.c +++ b/lib/math.c @@ -0,0 +1,99 @@ + +#include "math.h" + +#include + +void ccr_vec_xor (int bits, ccr_mtx a, ccr_mtx b, ccr_mtx r) +{ + /* possible speedup for wideword architectures + while(bits>=32) { + *(uint32_t*)r = *(uint32_t*)a ^ *(uint32_t*)b; + a+=4;b+=4;r+=4;bits-=32; + } */ + while (bits > 0) { + * (uint8_t*) r = * (uint8_t*) a ^ * (uint8_t*) b; + a += 1; + b += 1; + r += 1; + bits -= 8; + } + /* we can safely ignore padding bytes at the end of the vector */ +} + +void ccr_vec_and (int bits, ccr_mtx a, ccr_mtx b, ccr_mtx r) +{ + while (bits > 0) { + * (uint8_t*) r = * (uint8_t*) a & * (uint8_t*) b; + a += 1; + b += 1; + r += 1; + bits -= 8; + } +} + +int ccr_vec_parity (int bits, ccr_mtx a) +{ + /* first, xor everything to one byte */ + uint8_t b = 0; + while (bits >= 8) { + b ^= * (uint8_t*) a; + a += 1; + bits -= 8; + } + if (bits > 0) /* overflow padding bits away */ + b ^= * (uint8_t*) a << (8 - bits); + + /* squash the result in a single bit */ + b ^= b >> 4; + b ^= b >> 2; + b ^= b >> 1; + return b & 1; +} + +void ccr_vec_bit_set (ccr_mtx a, int offset, int bit) +{ + if (bit) + ( (uint8_t*) a) [offset/8] |= (uint8_t) (1 << (offset % 8) ); + else + ( (uint8_t*) a) [offset/8] &= ~ (uint8_t) (1 << (offset % 8) ); +} + +uint8_t ccr_vec_bit_get (ccr_mtx a, int offset) +{ + return 1 & ( ( (uint8_t*) a) [offset/8] >> (offset % 8) ); +} + +void ccr_mtx_add (int cols, int rows, + ccr_mtx a, ccr_mtx b, ccr_mtx r) +{ + int i, t; + for (i = 0; i < cols; ++i) { + t = ccr_mtx_vec_offset (rows, i); + ccr_vec_xor (rows, a + t, b + t, r + t); + } +} + +int ccr_mtx_dotproduct (ccr_mtx a, ccr_mtx b, + int aoff, int aheight, int boff, int len) +{ + uint8_t r = 0; + int i; + for (i = 0; i < len; ++i) + r ^= ccr_vec_bit_get (a + ccr_mtx_vec_offset (aheight, i), aoff) + & ccr_vec_bit_get (b + ccr_mtx_vec_offset (len, boff), i); + return r; +} + +void ccr_mtx_multiply (int rows, int veclen, int cols, + ccr_mtx a, ccr_mtx b, ccr_mtx r) +{ + /* TODO use faster algorithm */ + + int i, j; + for (i = 0; i < cols; ++i) + for (j = 0; j < rows; ++j) + ccr_vec_bit_set (r + ccr_mtx_vec_offset (rows, i), j, + ccr_mtx_dotproduct (a, b, + j, rows, i, + veclen) ); +} diff --git a/lib/math.h b/lib/math.h index 8dc17c0..96a8cb0 100644 --- a/lib/math.h +++ b/lib/math.h @@ -4,8 +4,8 @@ #include "codecrypt.h" -void ccr_mtx_add(int, int, const ccr_mtx, const ccr_mtx, ccr_mtx); -void ccr_mtx_multiply(int, int, int, const ccr_mtx, const ccr_mtx, ccr_mtx); +void ccr_mtx_add (int, int, ccr_mtx, ccr_mtx, ccr_mtx); +void ccr_mtx_multiply (int, int, int, ccr_mtx, ccr_mtx, ccr_mtx); #endif diff --git a/lib/prng.h b/lib/prng.h index 96667ac..4dbee67 100644 --- a/lib/prng.h +++ b/lib/prng.h @@ -2,8 +2,10 @@ #ifndef _CCR_PRNG_H_ #define _CCR_PRNG_H_ +#include "codecrypt.h" +int ccr_prng_seed (const char*); int ccr_prng_bit(); -int ccr_prng_int(int); +int ccr_prng_int (int); #endif diff --git a/lib/tools.c b/lib/tools.c index e69de29..f2b069f 100644 --- a/lib/tools.c +++ b/lib/tools.c @@ -0,0 +1,25 @@ + +#include "tools.h" + +#include + +static void* (*malloc_func) (size_t) = NULL; +static void (*free_func) (void*) = NULL; + +void* ccr_malloc (size_t s) +{ + if (malloc_func) return malloc_func (s); + else return malloc (s); +} + +void ccr_free (void*p) +{ + if (free_func) return free_func (p); + else return free (p); +} + +void ccr_set_internal_allocator (void* (*new_malloc) (size_t), void (*new_free) (void*) ) +{ + malloc_func = new_malloc; + free_func = new_free; +} diff --git a/lib/tools.h b/lib/tools.h index e69de29..33451df 100644 --- a/lib/tools.h +++ b/lib/tools.h @@ -0,0 +1,10 @@ + +#ifndef _CCR_TOOLS_H_ +#define _CCR_TOOLS_H_ + +#include "codecrypt.h" + +void* ccr_malloc (size_t); +void ccr_free (void*); + +#endif