/* from crypto.c */
+void memxor(Byte *dest, const Byte *src, int l);
+
+/* from hash.c */
+
typedef struct {
int blocksize, hashsize;
} HashAlgInfo;
+/* from blockciph.c */
+
+typedef struct {
+ void (*make_schedule)(void *schedule, const Byte *key, int keylen);
+ void (*crypt)(const void *schedule, const void *in, void *out);
+ /* in and out may be the same, but if they aren't they may not overlap */
+ /* in and out for crypt will have been through block_byteswap */
+} BlockCipherDirectionInfo;
+
typedef struct {
- int blocksize;
+ const char *name;
+ int blocksize, schedule_size, key_min, key_max;
+ void (*byteswap)(Byte *block);
+ BlockCipherDirectionInfo encrypt, decrypt;
} BlockCipherAlgInfo;
+extern const BlockCipherAlgInfo blockcipheralginfos[];
+
+/* from bcmode.c */
+
typedef struct {
- int dummy;
+ const char *name;
+ int iv_blocks, buf_blocks;
+ const char *(*encrypt)(Byte *data, int blocks,
+ const Byte *iv, Byte *buf,
+ const BlockCipherAlgInfo *alg, int encr,
+ int blocksize, const void *sch);
+ const char *(*decrypt)(Byte *data, int blocks,
+ const Byte *iv, Byte *buf,
+ const BlockCipherAlgInfo *alg, int encr,
+ int blocksize, const void *sch);
+ /* in each case, *iv is provided, but may be modified */
} BlockCipherModeInfo;
+extern const BlockCipherModeInfo blockciphermodeinfos[];
+
/* useful macros */
#define OBJ_HBYTES(o) ((HBytes_Value*)&(o)->internalRep.twoPtrValue)
v hbv
length int
=> hb
-# pkcs5
-# meth enum(PadMethod, "hbytes pad subcommand")
-# obj ...
+ pkcs5
+ meth enum(PadMethod, "hbytes pad subcommand")
+ obj ...
# blockcipher
# encrypt charfrom("de","encrypt/decrypt")
# v hbv
# maclen int
# => hb
-#Table padmethod PadMethod
-# pa 1, 0
+Table padmethod PadMethod
+ pa 1
+ v hbv
+ alg enum(BlockCipherAlgInfo, "pad alg")
+ ua 0
+ v hbv
+ alg enum(BlockCipherAlgInfo, "pad alg")
+ pn 1
+ v hbv
+ block int
+ un 0
+ v hbv
+ block int
-#EntryExtra PadMethod
-# int pad, algname;
+EntryExtra PadMethod
+ int pad;
enum.o \
chop.o \
hook.o \
+ bcmode.o \
+ blockciph.o \
+ serpent.o \
+ crypto.o \
parse.o
HDRS= hbytes.h \
--- /dev/null
+/*
+ */
+
+#include "hbytes.h"
+
+const char *mode_cbc_encrypt(Byte *data, int blocks,
+ const Byte *iv, Byte *chain,
+ const BlockCipherAlgInfo *alg, int encr,
+ int blocksize, const void *sch) {
+ memcpy(chain,iv,blocksize);
+ alg->byteswap(chain);
+
+ while (blocks > 0) {
+ alg->byteswap(data);
+
+ memxor(data, chain, blocksize);
+ alg->encrypt.crypt(sch, data, data);
+ memcpy(chain, data, blocksize);
+
+ alg->byteswap(data);
+ blocks--; data += blocksize;
+ }
+ return 0;
+}
+
+const char *mode_cbc_decrypt(Byte *data, int blocks,
+ const Byte *iv, Byte *chain,
+ const BlockCipherAlgInfo *alg, int encr,
+ int blocksize, const void *sch) {
+ int cchain= 0;
+
+ memcpy(chain,iv,blocksize);
+ alg->byteswap(chain);
+
+ while (blocks > 0) {
+ alg->byteswap(data);
+
+ memcpy(chain + (cchain^blocksize), data, blocksize);
+ alg->decrypt.crypt(sch, data, data);
+ memxor(data, chain + cchain, blocksize);
+ cchain ^= blocksize;
+
+ alg->byteswap(data);
+ blocks--; data += blocksize;
+ }
+ return 0;
+}
+
+const BlockCipherModeInfo blockciphermodeinfos[]= {
+ { "cbc", 1, 2, mode_cbc_encrypt, mode_cbc_decrypt },
+ { 0 }
+};
--- /dev/null
+/*
+ */
+
+#include <endian.h>
+
+#include "hbytes.h"
+#include "tables.h"
+#include "serpent.h"
+
+void memxor(Byte *dest, const Byte *src, int l) {
+ while (l--) *dest++ ^= *src++;
+}
+
+int do_hbytes_pkcs5(ClientData cd, Tcl_Interp *ip,
+ const PadMethod *meth, int objc, Tcl_Obj *const *objv) {
+ return meth->func((void*)meth, ip, objc, objv);
+}
#include "hbytes.h"
#include "tables.h"
-#include "hbintern.h"
#define COMPLEX(hb) ((HBytes_ComplexValue*)hb->begin_complex)
#define SIMPLE_LEN(hb) ((Byte*)(hb)->end_0 - (Byte*)(hb)->begin_complex)
/* from crypto.c */
+void memxor(Byte *dest, const Byte *src, int l);
+
+/* from hash.c */
+
typedef struct {
int blocksize, hashsize;
} HashAlgInfo;
+/* from blockciph.c */
+
+typedef struct {
+ void (*make_schedule)(void *schedule, const Byte *key, int keylen);
+ void (*crypt)(const void *schedule, const void *in, void *out);
+ /* in and out may be the same, but if they aren't they may not overlap */
+ /* in and out for crypt will have been through block_byteswap */
+} BlockCipherDirectionInfo;
+
typedef struct {
- int blocksize;
+ const char *name;
+ int blocksize, schedule_size, key_min, key_max;
+ void (*byteswap)(Byte *block);
+ BlockCipherDirectionInfo encrypt, decrypt;
} BlockCipherAlgInfo;
+extern const BlockCipherAlgInfo blockcipheralginfos[];
+
+/* from bcmode.c */
+
typedef struct {
- int dummy;
+ const char *name;
+ int iv_blocks, buf_blocks;
+ const char *(*encrypt)(Byte *data, int blocks,
+ const Byte *iv, Byte *buf,
+ const BlockCipherAlgInfo *alg, int encr,
+ int blocksize, const void *sch);
+ const char *(*decrypt)(Byte *data, int blocks,
+ const Byte *iv, Byte *buf,
+ const BlockCipherAlgInfo *alg, int encr,
+ int blocksize, const void *sch);
+ /* in each case, *iv is provided, but may be modified */
} BlockCipherModeInfo;
+extern const BlockCipherModeInfo blockciphermodeinfos[];
+
/* useful macros */
#define OBJ_HBYTES(o) ((HBytes_Value*)&(o)->internalRep.twoPtrValue)