Tue, 13 Aug 2013 11:19:22 +0200
added usage info
5
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1 | /* |
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
2 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
3 | * |
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
4 | * Copyright 2013 Olaf Wintermann. All rights reserved. |
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
5 | * |
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
6 | * Redistribution and use in source and binary forms, with or without |
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
7 | * modification, are permitted provided that the following conditions are met: |
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
8 | * |
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
9 | * 1. Redistributions of source code must retain the above copyright |
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
10 | * notice, this list of conditions and the following disclaimer. |
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
11 | * |
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
12 | * 2. Redistributions in binary form must reproduce the above copyright |
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
13 | * notice, this list of conditions and the following disclaimer in the |
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
14 | * documentation and/or other materials provided with the distribution. |
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
15 | * |
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
17 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
18 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
19 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
20 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
21 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
22 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
23 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
24 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
25 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
26 | * POSSIBILITY OF SUCH DAMAGE. |
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
27 | */ |
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
28 | |
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
29 | #include <stdio.h> |
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
30 | #include <stdlib.h> |
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
31 | #include <string.h> |
10
e6d0fbe0ebd9
added aes initialization vector
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
9
diff
changeset
|
32 | #include <openssl/rand.h> |
5
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
33 | |
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
34 | #include "crypto.h" |
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
35 | |
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
36 | AESDecrypter* aes_decrypter_new(Key *key, void *stream, dav_write_func write_func) { |
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
37 | AESDecrypter *dec = malloc(sizeof(AESDecrypter)); |
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
38 | dec->stream = stream; |
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
39 | dec->write = write_func; |
10
e6d0fbe0ebd9
added aes initialization vector
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
9
diff
changeset
|
40 | dec->key = key; |
e6d0fbe0ebd9
added aes initialization vector
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
9
diff
changeset
|
41 | dec->init = 0; |
e6d0fbe0ebd9
added aes initialization vector
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
9
diff
changeset
|
42 | dec->ivpos = 0; |
5
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
43 | |
10
e6d0fbe0ebd9
added aes initialization vector
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
9
diff
changeset
|
44 | return dec; |
e6d0fbe0ebd9
added aes initialization vector
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
9
diff
changeset
|
45 | } |
e6d0fbe0ebd9
added aes initialization vector
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
9
diff
changeset
|
46 | |
e6d0fbe0ebd9
added aes initialization vector
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
9
diff
changeset
|
47 | void aes_decrypter_init(AESDecrypter *dec) { |
5
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
48 | EVP_CIPHER_CTX_init(&dec->ctx); |
10
e6d0fbe0ebd9
added aes initialization vector
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
9
diff
changeset
|
49 | dec->init = 1; |
e6d0fbe0ebd9
added aes initialization vector
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
9
diff
changeset
|
50 | if(dec->key->type == KEY_AES128) { |
e6d0fbe0ebd9
added aes initialization vector
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
9
diff
changeset
|
51 | EVP_DecryptInit_ex( |
e6d0fbe0ebd9
added aes initialization vector
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
9
diff
changeset
|
52 | &dec->ctx, |
e6d0fbe0ebd9
added aes initialization vector
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
9
diff
changeset
|
53 | EVP_aes_128_cbc(), |
e6d0fbe0ebd9
added aes initialization vector
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
9
diff
changeset
|
54 | NULL, |
e6d0fbe0ebd9
added aes initialization vector
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
9
diff
changeset
|
55 | dec->key->data, |
e6d0fbe0ebd9
added aes initialization vector
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
9
diff
changeset
|
56 | dec->ivtmp); |
e6d0fbe0ebd9
added aes initialization vector
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
9
diff
changeset
|
57 | } else if(dec->key->type == KEY_AES256) { |
e6d0fbe0ebd9
added aes initialization vector
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
9
diff
changeset
|
58 | EVP_DecryptInit_ex( |
e6d0fbe0ebd9
added aes initialization vector
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
9
diff
changeset
|
59 | &dec->ctx, |
e6d0fbe0ebd9
added aes initialization vector
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
9
diff
changeset
|
60 | EVP_aes_256_cbc(), |
e6d0fbe0ebd9
added aes initialization vector
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
9
diff
changeset
|
61 | NULL, |
e6d0fbe0ebd9
added aes initialization vector
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
9
diff
changeset
|
62 | dec->key->data, |
e6d0fbe0ebd9
added aes initialization vector
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
9
diff
changeset
|
63 | dec->ivtmp); |
5
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
64 | } else { |
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
65 | fprintf(stderr, "unknown key type\n"); |
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
66 | exit(-1); |
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
67 | } |
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
68 | } |
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
69 | |
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
70 | size_t aes_write(const void *buf, size_t s, size_t n, AESDecrypter *dec) { |
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
71 | int len = s*n; |
10
e6d0fbe0ebd9
added aes initialization vector
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
9
diff
changeset
|
72 | if(!dec->init) { |
e6d0fbe0ebd9
added aes initialization vector
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
9
diff
changeset
|
73 | size_t n = 16 - dec->ivpos; |
e6d0fbe0ebd9
added aes initialization vector
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
9
diff
changeset
|
74 | size_t cp = n > len ? len : n; |
e6d0fbe0ebd9
added aes initialization vector
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
9
diff
changeset
|
75 | memcpy(dec->ivtmp + dec->ivpos, buf, cp); |
e6d0fbe0ebd9
added aes initialization vector
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
9
diff
changeset
|
76 | dec->ivpos += cp; |
e6d0fbe0ebd9
added aes initialization vector
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
9
diff
changeset
|
77 | if(dec->ivpos >= 16) { |
e6d0fbe0ebd9
added aes initialization vector
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
9
diff
changeset
|
78 | aes_decrypter_init(dec); |
e6d0fbe0ebd9
added aes initialization vector
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
9
diff
changeset
|
79 | } |
e6d0fbe0ebd9
added aes initialization vector
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
9
diff
changeset
|
80 | if(len == cp) { |
e6d0fbe0ebd9
added aes initialization vector
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
9
diff
changeset
|
81 | return len; |
e6d0fbe0ebd9
added aes initialization vector
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
9
diff
changeset
|
82 | } else { |
e6d0fbe0ebd9
added aes initialization vector
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
9
diff
changeset
|
83 | buf = (char*)buf + cp; |
e6d0fbe0ebd9
added aes initialization vector
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
9
diff
changeset
|
84 | len -= cp; |
e6d0fbe0ebd9
added aes initialization vector
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
9
diff
changeset
|
85 | } |
e6d0fbe0ebd9
added aes initialization vector
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
9
diff
changeset
|
86 | } |
e6d0fbe0ebd9
added aes initialization vector
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
9
diff
changeset
|
87 | |
9 | 88 | int outlen = len + 16; |
5
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
89 | unsigned char *out = malloc(outlen); |
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
90 | EVP_DecryptUpdate(&dec->ctx, out, &len, buf, len); |
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
91 | dec->write(out, 1, len, dec->stream); |
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
92 | free(out); |
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
93 | return (s*n) / s; |
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
94 | } |
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
95 | |
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
96 | void aes_decrypter_close(AESDecrypter *dec) { |
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
97 | void *out = malloc(128); |
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
98 | int len = 0; |
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
99 | EVP_DecryptFinal_ex(&dec->ctx, out, &len); |
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
100 | dec->write(out, 1, len, dec->stream); |
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
101 | free(out); |
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
102 | EVP_CIPHER_CTX_cleanup(&dec->ctx); |
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
103 | free(dec); |
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
104 | } |
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
105 | |
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
106 | |
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
107 | AESEncrypter* aes_encrypter_new(Key *key, void *stream, dav_read_func read_func) { |
10
e6d0fbe0ebd9
added aes initialization vector
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
9
diff
changeset
|
108 | unsigned char *iv = malloc(16); |
e6d0fbe0ebd9
added aes initialization vector
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
9
diff
changeset
|
109 | if(!RAND_bytes(iv, 16)) { |
e6d0fbe0ebd9
added aes initialization vector
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
9
diff
changeset
|
110 | free(iv); |
e6d0fbe0ebd9
added aes initialization vector
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
9
diff
changeset
|
111 | return NULL; |
e6d0fbe0ebd9
added aes initialization vector
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
9
diff
changeset
|
112 | } |
e6d0fbe0ebd9
added aes initialization vector
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
9
diff
changeset
|
113 | |
5
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
114 | AESEncrypter *enc = malloc(sizeof(AESEncrypter)); |
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
115 | enc->stream = stream; |
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
116 | enc->read = read_func; |
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
117 | enc->tmp = NULL; |
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
118 | enc->tmplen = 0; |
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
119 | enc->tmpoff = 0; |
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
120 | enc->end = 0; |
10
e6d0fbe0ebd9
added aes initialization vector
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
9
diff
changeset
|
121 | //enc->iv = iv; |
e6d0fbe0ebd9
added aes initialization vector
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
9
diff
changeset
|
122 | enc->iv = iv; |
e6d0fbe0ebd9
added aes initialization vector
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
9
diff
changeset
|
123 | enc->ivlen = 16; |
5
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
124 | |
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
125 | EVP_CIPHER_CTX_init(&enc->ctx); |
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
126 | if(key->type == KEY_AES128) { |
10
e6d0fbe0ebd9
added aes initialization vector
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
9
diff
changeset
|
127 | EVP_EncryptInit_ex(&enc->ctx, EVP_aes_128_cbc(), NULL, key->data, enc->iv); |
5
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
128 | } else if(key->type == KEY_AES256) { |
10
e6d0fbe0ebd9
added aes initialization vector
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
9
diff
changeset
|
129 | EVP_EncryptInit_ex(&enc->ctx, EVP_aes_256_cbc(), NULL, key->data, enc->iv); |
5
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
130 | } else { |
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
131 | fprintf(stderr, "unknown key type\n"); |
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
132 | exit(-1); |
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
133 | } |
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
134 | return enc; |
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
135 | } |
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
136 | |
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
137 | size_t aes_read(void *buf, size_t s, size_t n, AESEncrypter *enc) { |
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
138 | size_t len = s*n; |
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
139 | if(enc->tmp) { |
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
140 | size_t tmp_diff = enc->tmplen - enc->tmpoff; |
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
141 | size_t cp_len = tmp_diff > len ? len : tmp_diff; |
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
142 | memcpy(buf, enc->tmp + enc->tmpoff, cp_len); |
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
143 | enc->tmpoff += cp_len; |
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
144 | if(enc->tmpoff >= enc->tmplen) { |
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
145 | free(enc->tmp); |
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
146 | enc->tmp = NULL; |
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
147 | enc->tmplen = 0; |
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
148 | enc->tmpoff = 0; |
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
149 | } |
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
150 | return cp_len / s; |
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
151 | } |
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
152 | |
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
153 | if(enc->end) { |
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
154 | return 0; |
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
155 | } |
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
156 | |
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
157 | void *in = malloc(len); |
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
158 | size_t in_len = enc->read(in, 1, len, enc->stream); |
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
159 | |
10
e6d0fbe0ebd9
added aes initialization vector
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
9
diff
changeset
|
160 | unsigned char *out = NULL; |
5
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
161 | int outlen = 0; |
10
e6d0fbe0ebd9
added aes initialization vector
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
9
diff
changeset
|
162 | size_t ivl = enc->ivlen; |
5
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
163 | if(in_len != 0) { |
9 | 164 | outlen = len + 16; |
10
e6d0fbe0ebd9
added aes initialization vector
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
9
diff
changeset
|
165 | out = malloc(outlen + ivl); |
e6d0fbe0ebd9
added aes initialization vector
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
9
diff
changeset
|
166 | if(enc->iv) { |
e6d0fbe0ebd9
added aes initialization vector
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
9
diff
changeset
|
167 | memcpy(out, enc->iv, ivl); |
e6d0fbe0ebd9
added aes initialization vector
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
9
diff
changeset
|
168 | } |
e6d0fbe0ebd9
added aes initialization vector
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
9
diff
changeset
|
169 | EVP_EncryptUpdate(&enc->ctx, out + ivl, &outlen, in, in_len); |
11
5db6178d8b58
added usage info
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
10
diff
changeset
|
170 | free(in); |
5
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
171 | } else { |
9 | 172 | out = malloc(16); |
5
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
173 | EVP_EncryptFinal_ex(&enc->ctx, out, &outlen); |
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
174 | enc->end = 1; |
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
175 | } |
10
e6d0fbe0ebd9
added aes initialization vector
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
9
diff
changeset
|
176 | enc->tmp = (char*)out; |
e6d0fbe0ebd9
added aes initialization vector
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
9
diff
changeset
|
177 | enc->tmplen = outlen + ivl; |
5
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
178 | enc->tmpoff = 0; |
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
179 | |
10
e6d0fbe0ebd9
added aes initialization vector
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
9
diff
changeset
|
180 | if(enc->iv) { |
e6d0fbe0ebd9
added aes initialization vector
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
9
diff
changeset
|
181 | enc->iv = NULL; |
e6d0fbe0ebd9
added aes initialization vector
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
9
diff
changeset
|
182 | enc->ivlen = 0; |
e6d0fbe0ebd9
added aes initialization vector
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
9
diff
changeset
|
183 | } |
e6d0fbe0ebd9
added aes initialization vector
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
9
diff
changeset
|
184 | |
5
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
185 | return aes_read(buf, s, n, enc); |
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
186 | } |
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
187 | |
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
188 | void aes_encrypter_close(AESEncrypter *enc) { |
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
189 | if(enc->tmp) { |
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
190 | free(enc->tmp); |
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
191 | } |
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
192 | EVP_CIPHER_CTX_cleanup(&enc->ctx); |
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
193 | free(enc); |
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
194 | } |