summaryrefslogtreecommitdiffstats
path: root/crypto_buffer.c
diff options
context:
space:
mode:
authorJérémy Zurcher <jeremy@asynk.ch>2010-07-09 12:32:17 +0200
committerJérémy Zurcher <jeremy@asynk.ch>2010-07-09 12:32:17 +0200
commit6c027b4de25a529908be895e7ff19236f4002a57 (patch)
treef70d3a400bbb9ba8a83e9d4ffcaa4b6e367fc8bb /crypto_buffer.c
downloadcrypto-6c027b4de25a529908be895e7ff19236f4002a57.zip
crypto-6c027b4de25a529908be895e7ff19236f4002a57.tar.gz
initial commit, resurrect one of my realy old projects
Diffstat (limited to 'crypto_buffer.c')
-rw-r--r--crypto_buffer.c82
1 files changed, 82 insertions, 0 deletions
diff --git a/crypto_buffer.c b/crypto_buffer.c
new file mode 100644
index 0000000..d17e805
--- /dev/null
+++ b/crypto_buffer.c
@@ -0,0 +1,82 @@
+#include <stdlib.h>
+#include <string.h>
+
+#include "crypto_buffer.h"
+
+
+#include <stdio.h>
+int crypto_buffer_init(crypto_buffer *s, int fd, operation op, u32 blocks, int type, u8 *key, u32 key_len)
+{
+ int ret;
+ int size;
+
+ if(blocks<3) return -1; /* magic + info + data */
+
+ s->fd = fd;
+ s->op = op;
+
+ if(type & BLOWFISH){
+ size = BF_BLOCK_SIZE;
+ ret = blowfish_setkey((struct blowfish_ctx*)&s->ctx, key, key_len, 0);
+ if(type & DECRYPT){ s->cipher = blowfish_decrypt; }
+ else { s->cipher = blowfish_encrypt; }
+ }
+ else if(type & TWOFISH){
+ size = TF_BLOCK_SIZE;
+ ret = twofish_setkey((struct twofish_ctx*)&s->ctx, key, key_len, 0);
+ if(type & DECRYPT) { s->cipher = twofish_decrypt; }
+ else {s->cipher = twofish_encrypt; }
+ }
+ else if(type & AES){
+ size = AES_BLOCK_SIZE;
+ ret = aes_setkey((struct aes_ctx*)&s->ctx, key, key_len, 0);
+ if(type & DECRYPT) { s->cipher = aes_decrypt; }
+ else {s->cipher = aes_encrypt; }
+ }
+ else if(type & DES){
+ size = DES_BLOCK_SIZE;
+ ret = des_setkey((struct des_ctx*)&s->ctx, key, key_len, 0);
+ if(type & DECRYPT) { s->cipher = des_decrypt; }
+ else {s->cipher = des_encrypt; }
+ }
+ else if(type & DES3_EDE){
+ size = DES3_EDE_BLOCK_SIZE;
+ ret = des3_ede_setkey((struct des3_ede_ctx*)&s->ctx, key, key_len, 0);
+ if(type & DECRYPT) { s->cipher = des3_ede_decrypt; }
+ else {s->cipher = des3_ede_encrypt; }
+ }
+ else return -1;
+ if(ret!=0) return -1;
+
+ if((U32_MAX / size) < blocks) return -1;
+
+ s->block_size = size;
+ s->e_len = size * blocks; /* TODO check overflow */
+ s->data_st.bytes = s->data_st.strlen = 0;
+ /* clear buffer */
+ s->c_buffer = malloc(size);
+ if(s->c_buffer==NULL) return -1;
+ s->c_ptr = s->c_buffer;
+ s->c_end = s->c_buffer + size;
+ /* encrypte buffer */
+ s->e_buffer = malloc(s->e_len);
+ if(s->e_buffer==NULL) return -1;
+ memset(s->e_buffer,0x5a,size); /* for write operation */
+ s->e_data = (struct info*)(s->e_buffer + size);
+ s->e_end = s->e_buffer + s->e_len;
+ if(type & DECRYPT){
+ s->e_ptr = s->e_buffer;
+ }
+ else{
+ s->e_ptr = s->e_buffer + (size << 1);
+ s->e_len -= (size << 1);
+ }
+/*
+ fprintf(stderr,"e_buffer : %x\n",s->e_buffer);
+ fprintf(stderr,"e_data : %x\n",s->e_data);
+ fprintf(stderr,"e_ptr : %x\n",s->e_ptr);
+ fprintf(stderr,"e_end : %x\n",s->e_end);
+ fprintf(stderr,"length : %d\n",s->e_len);
+*/
+ return ret;
+}