- while (sz >= 2 * PRE ## _BLKSZ || sz == PRE ## _BLKSZ) { \
- uint32 b[PRE ## _BLKSZ / 4], niv[PRE ## _BLKSZ / 4]; \
- BLKC_LOAD(PRE, niv, s); \
- pre ## _dblk(&ctx->ctx, niv, b); \
- BLKC_XSTORE(PRE, d, b, ctx->iv); \
- BLKC_MOVE(PRE, ctx->iv, niv); \
- s += PRE ## _BLKSZ; \
- d += PRE ## _BLKSZ; \
- sz -= PRE ## _BLKSZ; \
- } \
- \
- /* --- Do the tail-end block and bit-left-over --- * \
- * \
- * This isn't very efficient. That shouldn't matter much. \
- */ \
- \
- if (sz) { \
- octet b[PRE ## _BLKSZ]; \
- uint32 bk[PRE ## _BLKSZ / 4], niv[PRE ## _BLKSZ / 4]; \
- unsigned i; \
- \
- /* --- Let @sz@ be the size of the partial block --- */ \
- \
- sz -= PRE ## _BLKSZ; \
- \
- /* --- First stage --- * \
- * \
- * Take the complete ciphertext block, and decrypt it. This block \
- * is carried over for the next encryption operation. \
- */ \
- \
- BLKC_LOAD(PRE, niv, s); \
- pre ## _dblk(&ctx->ctx, niv, bk); \
- \
- /* --- Second stage --- * \
- * \
- * XORing the first few bytes of this with the partial ciphertext \
- * block recovers the partial plaintext block. At the same time, \
- * write the partial ciphertext block's contents in ready for stage \
- * three. \
- */ \
- \
- BLKC_STORE(PRE, b, bk); \
- s += PRE ## _BLKSZ; \
- d += PRE ## _BLKSZ; \
- for (i = 0; i < sz; i++) { \
- register octet x = s[i]; \
- d[i] = b[i] ^ x; \
- b[i] = x; \
- } \
- \
- /* --- Third stage --- * \
- * \
- * Decrypt the block we've got left, and XOR with the initial IV to \
- * recover the complete plaintext block. \
- */ \
- \
- BLKC_LOAD(PRE, bk, b); \
- pre ## _dblk(&ctx->ctx, bk, bk); \
- BLKC_XSTORE(PRE, d - PRE ## _BLKSZ, bk, ctx->iv); \
- BLKC_MOVE(PRE, ctx->iv, niv); \
- } \
- \
- /* --- Done --- */ \
- \
- return; \
-} \
- \
-CBC_TEST(PRE, pre)
-
-/*----- Test rig ----------------------------------------------------------*/
-
-#ifdef TEST_RIG
-
-#include <stdio.h>
-
-#include "daftstory.h"
-
-/* --- @CBC_TEST@ --- *
- *
- * Arguments: @PRE@, @pre@ = prefixes for block cipher definitions
- *
- * Use: Standard test rig for CBC functions.
- */
-
-#define CBC_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 ## _cbcctx ctx; \
- pre ## _ctx k; \
- int status = 0; \
- int done = 0; \
- \
- size_t keysz = PRE ## _KEYSZ ? \
- PRE ## _KEYSZ : strlen((const char *)key); \
- \
- fputs(#pre "-cbc: ", stdout); \
- \
- pre ## _init(&k, key, keysz); \
- pre ## _cbcsetkey(&ctx, &k); \
- \
- while (sz <= sizeof(text)) { \
- rest = sizeof(text) - sz; \
- memcpy(ct, text, sizeof(text)); \
- pre ## _cbcsetiv(&ctx, iv); \
- pre ## _cbcencrypt(&ctx, ct, ct, sz); \
- pre ## _cbcencrypt(&ctx, ct + sz, ct + sz, rest); \
- memcpy(pt, ct, sizeof(text)); \
- pre ## _cbcsetiv(&ctx, iv); \
- pre ## _cbcdecrypt(&ctx, pt, pt, sz); \
- pre ## _cbcdecrypt(&ctx, pt + sz, pt + sz, rest); \
- 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 CBC_TEST(PRE, pre)
-#endif