chiark / gitweb /
Compiles but does not link. Before undo silly pad indirection.
[chiark-tcl.git] / crypto / bcmode.c
diff --git a/crypto/bcmode.c b/crypto/bcmode.c
new file mode 100644 (file)
index 0000000..7d5a5d4
--- /dev/null
@@ -0,0 +1,52 @@
+/*
+ */
+  
+#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 }
+};