X-Git-Url: https://git.distorted.org.uk/u/mdw/catacomb/blobdiff_plain/b3f050848e5d285c2a1d7f8a4bea98984c201805..b817bfc642225b8c3c0b6a7e42d1fb949b61a606:/blkc.h diff --git a/blkc.h b/blkc.h index fcd31dd..dfbd4f7 100644 --- a/blkc.h +++ b/blkc.h @@ -1,6 +1,6 @@ /* -*-c-*- * - * $Id: blkc.h,v 1.2 1999/12/10 23:29:48 mdw Exp $ + * $Id: blkc.h,v 1.7 2004/04/08 01:36:15 mdw Exp $ * * Common definitions for block ciphers * @@ -27,17 +27,6 @@ * MA 02111-1307, USA. */ -/*----- Revision history --------------------------------------------------* - * - * $Log: blkc.h,v $ - * Revision 1.2 1999/12/10 23:29:48 mdw - * Change header file guard names. - * - * Revision 1.1 1999/09/03 08:41:11 mdw - * Initial import. - * - */ - #ifndef CATACOMB_BLKC_H #define CATACOMB_BLKC_H @@ -47,6 +36,8 @@ /*----- Header files ------------------------------------------------------*/ +#include + #include /*----- Theory of operation -----------------------------------------------* @@ -106,6 +97,20 @@ BLKC_GLUE(BLKC_XMOVE_, BLKC_TYPE(PRE)) \ (PRE, w, wx, BLKC_BITS(PRE)) +#define BLKC_STEP(PRE, w) \ + BLKC_GLUE(BLKC_STEP_X_, BLKC_ENDIAN(PRE)) \ + (PRE, w) + +#define BLKC_SET(PRE, w, x) \ + BLKC_GLUE(BLKC_SET_X_, BLKC_ENDIAN(PRE)) \ + (PRE, w, x) + +#define BLKC_SHOW(PRE, tag, w) do { \ + fputs(tag ": ", stdout); \ + BLKC_SKEL_X(PRE, BLKC_W(w);, printf("%08x ", *_w++);); \ + fputc('\n', stdout); \ +} while (0) + /* --- General implementation skeleton --- */ #define BLKC_SKEL(PRE, decl, guts) do { \ @@ -115,12 +120,12 @@ #define BLKC_P(p) register octet *_p = (octet *)(p) #define BLKC_W(w) register uint32 *_w = (w) -#define BLKC_WX(wx) register uint32 *_wx = (wx); +#define BLKC_WX(wx) register uint32 *_wx = (wx) /* --- Implementation for unusual block sizes --- */ #define BLKC_SKEL_X(PRE, decl, guts) \ - BLKC_SKEL(PRE, int _i; decl, \ + BLKC_SKEL(PRE, unsigned _i; decl, \ for (_i = 0; _i < PRE##_BLKSZ / 4; _i++) { \ guts \ }) @@ -149,14 +154,54 @@ BLKC_SKEL_X(PRE, BLKC_W(w); const BLKC_WX(wx);, \ *_w ^= *_wx; _w++; _wx++; ) \ +#define BLKC_STEP_X_B(PRE, w) do { \ + unsigned _i = PRE##_BLKSZ / 4; BLKC_W(w); uint32 _x = 0; \ + while (_i && !_x) { _i--; _w[_i] = _x = U32(_w[_i] + 1); } \ +} while (0) + +#define BLKC_STEP_X_L(PRE, w) do { \ + unsigned _i = 0; BLKC_W(w); uint32 _x = 0; \ + while (_i < PRE##_BLKSZ / 4 && !_x) \ + { _w[_i] = _x = U32(_w[_i] + 1); _i++; } \ +} while (0) + +#define BLKC_SET_X_B(PRE, w, x) do { \ + unsigned _i; BLKC_W(w); unsigned long _x = x; \ + for (_i = 0; _i < PRE##_BLKSZ / 4; _i++) { \ + *_w++ = U32(_x); \ + _x = ((_x & ~MASK32) >> 16) >> 16; \ + } \ +} while (0) + +#define BLKC_SET_X_L(PRE, w, x) do { \ + unsigned _i; BLKC_W(w); unsigned long _x = x; _w += PRE##_BLKSZ / 4; \ + for (_i = 0; _i < PRE##_BLKSZ / 4; _i++) { \ + *--_w = U32(_x); \ + _x = ((_x & ~MASK32) >> 16) >> 16; \ + } \ +} while (0) + /* --- Implementation for known block sizes --- */ #define BLKC_SKEL_64(PRE, decl, op, guts) \ BLKC_SKEL(PRE, decl, guts(op, 0); guts(op, 1);) +#define BLKC_SKEL_96(PRE, decl, op, guts) \ + BLKC_SKEL(PRE, decl, guts(op, 0); guts(op, 1); guts(op, 2);) + #define BLKC_SKEL_128(PRE, decl, op, guts) \ BLKC_SKEL(PRE, decl, guts(op, 0); guts(op, 1); guts(op, 2); guts(op, 3);) +#define BLKC_SKEL_192(PRE, decl, op, guts) \ + BLKC_SKEL(PRE, decl, \ + guts(op, 0); guts(op, 1); guts(op, 2); guts(op, 3); \ + guts(op, 4); guts(op, 5);) + +#define BLKC_SKEL_256(PRE, decl, op, guts) \ + BLKC_SKEL(PRE, decl, \ + guts(op, 0); guts(op, 1); guts(op, 2); guts(op, 3); \ + guts(op, 4); guts(op, 5); guts(op, 6); guts(op, 7);) + #define BLKC_STORE_GUTS(op, i) op(_p + 4 * i, _w[i]) #define BLKC_XSTORE_GUTS(op, i) op(_p + 4 * i, _w[i] ^ _wx[i]) #define BLKC_LOAD_GUTS(op, i) _w[i] = op(_p + 4 * i) @@ -203,9 +248,9 @@ #include #include -#define BLKC_TEST(PRE, pre) \ +#define BLKC_VERIFY(PRE, pre) \ \ -static int verify(dstr *v) \ +static int pre##_verify(dstr *v) \ { \ pre##_ctx k; \ uint32 p[PRE##_BLKSZ / 4]; \ @@ -257,11 +302,14 @@ static int verify(dstr *v) \ /* --- Return --- */ \ \ return (ok); \ -} \ +} + +#define BLKC_TEST(PRE, pre) \ + \ +BLKC_VERIFY(PRE, pre) \ \ -static test_chunk defs[] = { \ - { #pre, verify, { &type_hex, &type_hex, &type_hex, 0 } }, \ - { #pre "-sched", verify, { &type_hex, &type_hex, &type_hex, 0 } }, \ +static const test_chunk defs[] = { \ + { #pre, pre##_verify, { &type_hex, &type_hex, &type_hex, 0 } }, \ { 0, 0, { 0 } } \ }; \ \ @@ -272,6 +320,7 @@ int main(int argc, char *argv[]) \ } #else +# define BLKC_VERIFY(PRE, pre) # define BLKC_TEST(PRE, pre) #endif