dav/crypto.c

changeset 5
88625853ae74
child 9
6aec77cfa95b
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/dav/crypto.c	Mon Aug 12 14:40:19 2013 +0200
@@ -0,0 +1,142 @@
+/*
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
+ *
+ * Copyright 2013 Olaf Wintermann. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ *   1. Redistributions of source code must retain the above copyright
+ *      notice, this list of conditions and the following disclaimer.
+ *
+ *   2. Redistributions in binary form must reproduce the above copyright
+ *      notice, this list of conditions and the following disclaimer in the
+ *      documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include "crypto.h"
+
+AESDecrypter* aes_decrypter_new(Key *key, void *stream, dav_write_func write_func) {
+    AESDecrypter *dec = malloc(sizeof(AESDecrypter));
+    dec->stream = stream;
+    dec->write = write_func;
+    
+    EVP_CIPHER_CTX_init(&dec->ctx);
+    if(key->type == KEY_AES128) {
+        EVP_DecryptInit_ex(&dec->ctx, EVP_aes_128_cbc(), NULL, key->data, NULL);
+    } else if(key->type == KEY_AES256) {
+        EVP_DecryptInit_ex(&dec->ctx, EVP_aes_256_cbc(), NULL, key->data, NULL);
+    } else {
+        fprintf(stderr, "unknown key type\n");
+        exit(-1);
+    }
+    return dec;
+}
+
+size_t aes_write(const void *buf, size_t s, size_t n, AESDecrypter *dec) {
+    int len = s*n;
+    int outlen = len + AES_BLOCK_SIZE;
+    unsigned char *out = malloc(outlen);
+    EVP_DecryptUpdate(&dec->ctx, out, &len, buf, len);
+    dec->write(out, 1, len, dec->stream);
+    free(out);
+    return (s*n) / s;
+}
+
+void aes_decrypter_close(AESDecrypter *dec) {
+    void *out = malloc(128);
+    int len = 0;
+    EVP_DecryptFinal_ex(&dec->ctx, out, &len);
+    dec->write(out, 1, len, dec->stream);
+    free(out);
+    EVP_CIPHER_CTX_cleanup(&dec->ctx);
+    free(dec);
+}
+
+
+AESEncrypter* aes_encrypter_new(Key *key, void *stream, dav_read_func read_func) {
+    AESEncrypter *enc = malloc(sizeof(AESEncrypter));
+    enc->stream = stream;
+    enc->read = read_func;
+    enc->tmp = NULL;
+    enc->tmplen = 0;
+    enc->tmpoff = 0;
+    enc->end = 0;
+    
+    EVP_CIPHER_CTX_init(&enc->ctx);
+    if(key->type == KEY_AES128) {
+        EVP_EncryptInit_ex(&enc->ctx, EVP_aes_128_cbc(), NULL, key->data, NULL);
+    } else if(key->type == KEY_AES256) {
+        EVP_EncryptInit_ex(&enc->ctx, EVP_aes_256_cbc(), NULL, key->data, NULL);
+    } else {
+        fprintf(stderr, "unknown key type\n");
+        exit(-1);
+    }
+    return enc;
+}
+
+size_t aes_read(void *buf, size_t s, size_t n, AESEncrypter *enc) {
+    size_t len = s*n;
+    if(enc->tmp) {
+        size_t tmp_diff = enc->tmplen - enc->tmpoff;
+        size_t cp_len = tmp_diff > len ? len : tmp_diff;
+        memcpy(buf, enc->tmp + enc->tmpoff, cp_len);
+        enc->tmpoff += cp_len;
+        if(enc->tmpoff >= enc->tmplen) {
+            free(enc->tmp);
+            enc->tmp = NULL;
+            enc->tmplen = 0;
+            enc->tmpoff = 0;
+        }
+        return cp_len / s;
+    }
+    
+    if(enc->end) {
+        return 0;
+    }
+    
+    void *in = malloc(len);
+    size_t in_len = enc->read(in, 1, len, enc->stream);
+    
+    void *out = NULL;
+    int outlen = 0;
+    if(in_len != 0) {
+        outlen = len + AES_BLOCK_SIZE;
+        out = malloc(outlen);
+        EVP_EncryptUpdate(&enc->ctx, out, &outlen, in, in_len);
+        //out = (char*)aes_encrypt(enc->ctx, (unsigned char*)in, (int*)&in_len);
+    } else {
+        out = malloc(AES_BLOCK_SIZE);
+        EVP_EncryptFinal_ex(&enc->ctx, out, &outlen);
+        enc->end = 1;
+    }
+    enc->tmp = out;
+    enc->tmplen = outlen;
+    enc->tmpoff = 0;
+    
+    return aes_read(buf, s, n, enc);
+}
+
+void aes_encrypter_close(AESEncrypter *enc) {
+    if(enc->tmp) {
+        free(enc->tmp);
+    }
+    EVP_CIPHER_CTX_cleanup(&enc->ctx);
+    free(enc);
+}

mercurial