diff --git a/include/codecrypt.h b/include/codecrypt.h index ce16978..d395005 100644 --- a/include/codecrypt.h +++ b/include/codecrypt.h @@ -2,87 +2,65 @@ #ifndef _CODECRYPT_H_ #define _CODECRYPT_H_ -#include -#include +#include -#ifdef __cplusplus -extern "C" { -#endif +namespace ccr { - /* codecrypt matrix/vector/whatever type */ - typedef uint8_t* ccr_mtx; - /* permutation as a list of transpositions */ - typedef int* ccr_perm; + typedef std::vector bvector; + //for broken/old/weird STL uncomment this: + //typedef std::bit_vector bvector; + //TODO ifdef + + class matrix : public std::vector { - /* macros for faster allocation/accessing */ -#define ccr_mtx_alloc_size(veclen,nvec) ((1+(((veclen)-1)/8))*(nvec)) -#define ccr_mtx_vec_offset ccr_mtx_alloc_size - - struct ccr_mce_pubkey { - /* params */ - int n, k, t; - - /* n*k G' pubkey matrix */ - ccr_mtx g; }; - struct ccr_mce_privkey { - /* params, n and t are input params */ - int n, k, t; + class permutation : public vector { - /* goppa polynomial of degree t */ - ccr_mtx poly; - - /* inverse of S matrix */ - ccr_mtx sinv; - - /* inverse of P permutation */ - ccr_perm pinv; - - /* systematic form permutation (inv.) */ - ccr_perm psys; - - /* parity check matrix */ - ccr_mtx h; }; - struct ccr_nd_pubkey { - /* params */ - int n, k, t; + class polynomial : public bvector { - /* pubkey matrix */ - ccr_mtx h; }; - struct ccr_nd_privkey { - /* params */ - int n, k, t; + namespace mce { + class privkey { + public: + matrix - /* goppa polynomial of degree t */ - ccr_mtx poly; + int decrypt(const bvector&, bvector&); + }; - /* inverse of S matrix */ - ccr_mtx sinv; + class pubkey { + public: + matrix G; + int t; + int encrypt(const bvector&, bvector&); + }; - /* inverse of P permutation */ - ccr_perm pinv; - }; + int generate(pubkey&,privkey&); + } - /* actual functions */ - int ccr_mce_gen (struct ccr_mce_pubkey*, struct ccr_mce_privkey*); - 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*); + namespace nd { + class privkey { - int ccr_nd_gen (struct ccr_nd_pubkey*, struct ccr_nd_privkey*); - 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*); + int decrypt(const bvector&, bvector&); + }; - void ccr_set_log_func (void (*) (const char*) ); - void ccr_set_internal_allocator (void* (*) (size_t), void (*) (void*) ); + class pubkey { + public: + matrix H; + int t; -#ifdef __cplusplus -} -#endif + int encrypt(const bvector&, bvector&); + }; -#endif /* _CODECRYPT_H_ */ + int generate(pubkey&,privkey&); + } + + //TODO entropy sources + +} //namespace CCR + +#endif // _CODECRYPT_H_ diff --git a/lib/log.c b/lib/log.c deleted file mode 100644 index 01f7ac4..0000000 --- a/lib/log.c +++ /dev/null @@ -1,16 +0,0 @@ - -#include "codecrypt.h" -#include "log.h" - -static void (*global_log) (const char*) = NULL; - -void ccr_set_log_func (void (*x) (const char*) ) -{ - global_log = x; -} - -void ccr_log (const char* fmt, ...) -{ - if (!global_log) return; - //TODO -} diff --git a/lib/log.h b/lib/log.h deleted file mode 100644 index 7bfb193..0000000 --- a/lib/log.h +++ /dev/null @@ -1,8 +0,0 @@ - -#ifndef _CCR_LOG_H_ -#define _CCR_LOG_H_ - -void ccr_log (const char*, ...); - -#endif - diff --git a/lib/math.c b/lib/math.c deleted file mode 100644 index 9cc67ff..0000000 --- a/lib/math.c +++ /dev/null @@ -1,99 +0,0 @@ - -#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/nd.c b/lib/math.cpp similarity index 100% rename from lib/nd.c rename to lib/math.cpp diff --git a/lib/math.h b/lib/math.h deleted file mode 100644 index 1026fa1..0000000 --- a/lib/math.h +++ /dev/null @@ -1,16 +0,0 @@ - -#ifndef _CCR_MATH_H_ -#define _CCR_MATH_H_ - -#include "codecrypt.h" - -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); - -int ccr_log2 (int, int*); -int ccr_gen_irred_poly (ccr_mtx, int); - -int ccr_goppa_check_mtx (ccr_mtx, int, int, ccr_mtx*, int*, int*); - -#endif - diff --git a/lib/mce.c b/lib/mce.c deleted file mode 100644 index c2af0bc..0000000 --- a/lib/mce.c +++ /dev/null @@ -1,55 +0,0 @@ - -#include "codecrypt.h" -#include "math.h" -#include "tools.h" - -int ccr_mce_gen (struct ccr_mce_pubkey* Pub, struct ccr_mce_privkey* Priv) -{ - /* params are taken from privkey matrix */ - - int ret; - int m; - ccr_mtx h; - int h_cols, h_rows; - - /* param n must be power of 2 */ - if (ccr_log2 (Priv->n, &m) ) { - ret = 1; - goto fail; - } - - /* check sanity of t param, k<=n-mt */ - if (Priv->n >= m * Priv->t) { - ret = 2; - goto fail; - } - - /* allocate space for goppa polynomial */ - Priv->poly = ccr_malloc (ccr_mtx_alloc_size (t + 1, 1) ); - if (!Priv->poly) { - ret = 3; - goto fail; - } - - /* generate the polynomial */ - if (ccr_gen_irred_poly (Priv->poly, Priv->t) ) { - ret = 4; - goto fail_free_poly; - } - - /* create canonical check matrix */ - if (ccr_goppa_check_mtx (Priv->poly, m, Priv->t, &h, &h_cols, &h_rows) ) { - ret = 5; - goto fail_free_poly; - } - - if(ccr_goppa_systematic_form(h,h_cols,h_rows, - - return 0; - -fail_free_poly: - ccr_free (Priv->poly); -fail: - return ret; -} - diff --git a/lib/prng.c b/lib/prng.c deleted file mode 100644 index e69de29..0000000 diff --git a/lib/prng.h b/lib/prng.h deleted file mode 100644 index 4dbee67..0000000 --- a/lib/prng.h +++ /dev/null @@ -1,11 +0,0 @@ - -#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); - -#endif diff --git a/lib/tools.c b/lib/tools.c deleted file mode 100644 index f2b069f..0000000 --- a/lib/tools.c +++ /dev/null @@ -1,25 +0,0 @@ - -#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 deleted file mode 100644 index 33451df..0000000 --- a/lib/tools.h +++ /dev/null @@ -1,10 +0,0 @@ - -#ifndef _CCR_TOOLS_H_ -#define _CCR_TOOLS_H_ - -#include "codecrypt.h" - -void* ccr_malloc (size_t); -void ccr_free (void*); - -#endif diff --git a/src/main.c b/src/main.cpp similarity index 100% rename from src/main.c rename to src/main.cpp