- /* --- If I can deal with the block from my buffer, do that --- */ \
- \
- if (sz < PRE ## _BLKSZ - off) \
- goto small; \
- \
- /* --- Finish off what's left in my buffer --- */ \
- \
- while (off < PRE ## _BLKSZ) { \
- register octet x = *s++; \
- *d++ = ctx->iv[off] ^ x; \
- ctx->iv[off++] = x; \
- sz--; \
- } \
- \
- /* --- Main encryption loop --- */ \
- \
- { \
- uint32 iv[PRE ## _BLKSZ / 4]; \
- BLKC_LOAD(PRE, iv, ctx->iv); \
- \
- for (;;) { \
- uint32 x[PRE ## _BLKSZ / 4]; \
- pre ## _eblk(&ctx->ctx, iv, iv); \
- if (sz < PRE ## _BLKSZ) \
- break; \
- BLKC_LOAD(PRE, x, s); \
- BLKC_XSTORE(PRE, d, iv, x); \
- BLKC_MOVE(PRE, iv, x); \
- s += PRE ## _BLKSZ; \
- d += PRE ## _BLKSZ; \
- sz -= PRE ## _BLKSZ; \
- } \
- off = 0; \
- BLKC_STORE(PRE, ctx->iv, iv); \
- } \
- \
- /* --- Tidying up the tail end --- */ \
- \
- if (sz) { \
- small: \
- do { \
- register octet x = *s++; \
- *d++ = ctx->iv[off] ^ x; \
- ctx->iv[off++] = x; \
- sz--; \
- } while (sz); \
- } \
- \
- /* --- Done --- */ \
- \
- ctx->off = off; \
- return; \
-} \
- \
-CFB_TEST(PRE, pre)
-
-/*----- Test rig ----------------------------------------------------------*/
-
-#ifdef TEST_RIG
-
-#include <stdio.h>
-
-#include "daftstory.h"
-
-/* --- @CFB_TEST@ --- *
- *
- * Arguments: @PRE@, @pre@ = prefixes for block cipher definitions
- *
- * Use: Standard test rig for CFB functions.
- */
-
-#define CFB_TEST(PRE, pre) \
- \
-/* --- Initial plaintext for the test --- */ \
- \
-static const octet text[] = TEXT; \
- \
-/* --- Key and IV to use --- */ \
- \
-static const octet key[] = KEY; \
-static const octet iv[] = IV; \
- \
-/* --- Buffers for encryption and decryption output --- */ \
- \
-static octet ct[sizeof(text)]; \
-static octet pt[sizeof(text)]; \
- \
-static void hexdump(const octet *p, size_t sz) \
-{ \
- const octet *q = p + sz; \
- for (sz = 0; p < q; p++, sz++) { \
- printf("%02x", *p); \
- if ((sz + 1) % PRE ## _BLKSZ == 0) \
- putchar(':'); \
- } \
-} \
- \
-int main(void) \
-{ \
- size_t sz = 0, rest; \
- pre ## _cfbctx ctx; \
- int status = 0; \
- int done = 0; \
- pre ## _ctx k; \
- \
- size_t keysz = PRE ## _KEYSZ ? \
- PRE ## _KEYSZ : strlen((const char *)key); \
- \
- fputs(#pre "-cfb: ", stdout); \
- \
- pre ## _init(&k, key, keysz); \
- pre ## _cfbsetkey(&ctx, &k); \
- \
- while (sz <= sizeof(text)) { \
- rest = sizeof(text) - sz; \
- memcpy(ct, text, sizeof(text)); \
- pre ## _cfbsetiv(&ctx, iv); \
- pre ## _cfbencrypt(&ctx, ct, ct, sz); \
- pre ## _cfbencrypt(&ctx, ct + sz, ct + sz, rest); \
- memcpy(pt, ct, sizeof(text)); \
- pre ## _cfbsetiv(&ctx, iv); \
- pre ## _cfbdecrypt(&ctx, pt, pt, rest); \
- pre ## _cfbdecrypt(&ctx, pt + rest, pt + rest, sz); \
- if (memcmp(pt, text, sizeof(text)) == 0) { \
- done++; \
- if (sizeof(text) < 40 || done % 8 == 0) \
- fputc('.', stdout); \
- if (done % 480 == 0) \
- fputs("\n\t", stdout); \
- fflush(stdout); \
- } else { \
- printf("\nError (sz = %lu)\n", (unsigned long)sz); \
- status = 1; \
- printf("\tplaintext = "); hexdump(text, sz); \
- printf(", "); hexdump(text + sz, rest); \
- fputc('\n', stdout); \
- printf("\tciphertext = "); hexdump(ct, sz); \
- printf(", "); hexdump(ct + sz, rest); \
- fputc('\n', stdout); \
- printf("\trecovered text = "); hexdump(pt, sz); \
- printf(", "); hexdump(pt + sz, rest); \
- fputc('\n', stdout); \
- fputc('\n', stdout); \
- } \
- if (sz < 63) \
- sz++; \
- else \
- sz += 9; \
- } \
- \
- fputs(status ? " failed\n" : " ok\n", stdout); \
- return (status); \
-}
-
-#else
-# define CFB_TEST(PRE, pre)
-#endif