Sun, 10 Jun 2018 12:35:00 +0200
using new io abstraction in dav-sync
40
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1 | /* |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
2 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
3 | * |
404
5c08b8e14df8
updates copyright notice
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
367
diff
changeset
|
4 | * Copyright 2018 Olaf Wintermann. All rights reserved. |
40
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
5 | * |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
6 | * Redistribution and use in source and binary forms, with or without |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
7 | * modification, are permitted provided that the following conditions are met: |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
8 | * |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
9 | * 1. Redistributions of source code must retain the above copyright |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
10 | * notice, this list of conditions and the following disclaimer. |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
11 | * |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
12 | * 2. Redistributions in binary form must reproduce the above copyright |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
13 | * notice, this list of conditions and the following disclaimer in the |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
14 | * documentation and/or other materials provided with the distribution. |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
15 | * |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
17 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
18 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
19 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
20 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
21 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
22 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
23 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
24 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
a95ee94b9204
supports whitespace in paths
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 |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
26 | * POSSIBILITY OF SUCH DAMAGE. |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
27 | */ |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
28 | |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
29 | #include <stdio.h> |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
30 | #include <stdlib.h> |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
31 | #include <string.h> |
349
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
32 | #include <unistd.h> |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
33 | #include <fcntl.h> |
267
171498cb2137
some mingw fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
261
diff
changeset
|
34 | #include <libidav/utils.h> |
40
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
35 | |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
36 | #include "crypto.h" |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
37 | |
349
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
38 | /* -------------------- OpenSSL Crypto Functions -------------------- */ |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
39 | #ifdef DAV_USE_OPENSSL |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
40 | |
261
f60d742a62a0
fixes build with ancient openssl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
260
diff
changeset
|
41 | #if OPENSSL_VERSION_NUMBER < 10000000 |
f60d742a62a0
fixes build with ancient openssl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
260
diff
changeset
|
42 | |
f60d742a62a0
fixes build with ancient openssl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
260
diff
changeset
|
43 | static EVP_CIPHER_CTX* create_evp_cipher_ctx() { |
f60d742a62a0
fixes build with ancient openssl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
260
diff
changeset
|
44 | EVP_CIPHER_CTX *ctx = malloc(sizeof(EVP_CIPHER_CTX)); |
f60d742a62a0
fixes build with ancient openssl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
260
diff
changeset
|
45 | EVP_CIPHER_CTX_init(ctx); |
f60d742a62a0
fixes build with ancient openssl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
260
diff
changeset
|
46 | return ctx; |
f60d742a62a0
fixes build with ancient openssl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
260
diff
changeset
|
47 | } |
f60d742a62a0
fixes build with ancient openssl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
260
diff
changeset
|
48 | |
f60d742a62a0
fixes build with ancient openssl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
260
diff
changeset
|
49 | static void free_evp_cipher_ctx(EVP_CIPHER_CTX *ctx) { |
f60d742a62a0
fixes build with ancient openssl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
260
diff
changeset
|
50 | EVP_CIPHER_CTX_cleanup(ctx); |
f60d742a62a0
fixes build with ancient openssl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
260
diff
changeset
|
51 | free(ctx); |
f60d742a62a0
fixes build with ancient openssl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
260
diff
changeset
|
52 | } |
f60d742a62a0
fixes build with ancient openssl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
260
diff
changeset
|
53 | |
f60d742a62a0
fixes build with ancient openssl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
260
diff
changeset
|
54 | #define EVP_CIPHER_CTX_new() create_evp_cipher_ctx() |
f60d742a62a0
fixes build with ancient openssl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
260
diff
changeset
|
55 | #define EVP_CIPHER_CTX_free(ctx) free_evp_cipher_ctx(ctx) |
f60d742a62a0
fixes build with ancient openssl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
260
diff
changeset
|
56 | |
f60d742a62a0
fixes build with ancient openssl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
260
diff
changeset
|
57 | #endif |
f60d742a62a0
fixes build with ancient openssl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
260
diff
changeset
|
58 | |
40
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
59 | AESDecrypter* aes_decrypter_new(DavKey *key, void *stream, dav_write_func write_func) { |
207
de23f8881e9f
fixed hash verification and head requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
185
diff
changeset
|
60 | AESDecrypter *dec = calloc(1, sizeof(AESDecrypter)); |
150
37fb12574acd
added checksums for encrypted resources
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
75
diff
changeset
|
61 | SHA256_Init(&dec->sha256); |
40
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
62 | dec->stream = stream; |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
63 | dec->write = write_func; |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
64 | dec->key = key; |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
65 | dec->init = 0; |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
66 | dec->ivpos = 0; |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
67 | |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
68 | return dec; |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
69 | } |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
70 | |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
71 | void aes_decrypter_init(AESDecrypter *dec) { |
260
26f5f817429e
fixes build with openssl 1.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
237
diff
changeset
|
72 | //EVP_CIPHER_CTX_init(&dec->ctx); |
26f5f817429e
fixes build with openssl 1.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
237
diff
changeset
|
73 | dec->ctx = EVP_CIPHER_CTX_new(); |
40
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
74 | dec->init = 1; |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
75 | if(dec->key->type == DAV_KEY_AES128) { |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
76 | EVP_DecryptInit_ex( |
260
26f5f817429e
fixes build with openssl 1.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
237
diff
changeset
|
77 | dec->ctx, |
40
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
78 | EVP_aes_128_cbc(), |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
79 | NULL, |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
80 | dec->key->data, |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
81 | dec->ivtmp); |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
82 | } else if(dec->key->type == DAV_KEY_AES256) { |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
83 | EVP_DecryptInit_ex( |
260
26f5f817429e
fixes build with openssl 1.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
237
diff
changeset
|
84 | dec->ctx, |
40
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
85 | EVP_aes_256_cbc(), |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
86 | NULL, |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
87 | dec->key->data, |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
88 | dec->ivtmp); |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
89 | } else { |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
90 | fprintf(stderr, "unknown key type\n"); |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
91 | exit(-1); |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
92 | } |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
93 | } |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
94 | |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
95 | size_t aes_write(const void *buf, size_t s, size_t n, AESDecrypter *dec) { |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
96 | int len = s*n; |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
97 | if(!dec->init) { |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
98 | size_t n = 16 - dec->ivpos; |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
99 | size_t cp = n > len ? len : n; |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
100 | memcpy(dec->ivtmp + dec->ivpos, buf, cp); |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
101 | dec->ivpos += cp; |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
102 | if(dec->ivpos >= 16) { |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
103 | aes_decrypter_init(dec); |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
104 | } |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
105 | if(len == cp) { |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
106 | return len; |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
107 | } else { |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
108 | buf = (char*)buf + cp; |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
109 | len -= cp; |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
110 | } |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
111 | } |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
112 | |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
113 | int outlen = len + 16; |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
114 | unsigned char *out = malloc(outlen); |
260
26f5f817429e
fixes build with openssl 1.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
237
diff
changeset
|
115 | EVP_DecryptUpdate(dec->ctx, out, &len, buf, len); |
150
37fb12574acd
added checksums for encrypted resources
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
75
diff
changeset
|
116 | ssize_t wlen = dec->write(out, 1, len, dec->stream); |
37fb12574acd
added checksums for encrypted resources
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
75
diff
changeset
|
117 | SHA256_Update(&dec->sha256, out, wlen); |
40
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
118 | free(out); |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
119 | return (s*n) / s; |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
120 | } |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
121 | |
207
de23f8881e9f
fixed hash verification and head requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
185
diff
changeset
|
122 | void aes_decrypter_shutdown(AESDecrypter *dec) { |
237
fd9135bc7580
fixed crash when encrypted streams are empty
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
207
diff
changeset
|
123 | if(dec->init) { |
fd9135bc7580
fixed crash when encrypted streams are empty
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
207
diff
changeset
|
124 | void *out = malloc(128); |
fd9135bc7580
fixed crash when encrypted streams are empty
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
207
diff
changeset
|
125 | int len = 0; |
260
26f5f817429e
fixes build with openssl 1.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
237
diff
changeset
|
126 | EVP_DecryptFinal_ex(dec->ctx, out, &len); |
237
fd9135bc7580
fixed crash when encrypted streams are empty
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
207
diff
changeset
|
127 | dec->write(out, 1, len, dec->stream); |
fd9135bc7580
fixed crash when encrypted streams are empty
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
207
diff
changeset
|
128 | SHA256_Update(&dec->sha256, out, len); |
fd9135bc7580
fixed crash when encrypted streams are empty
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
207
diff
changeset
|
129 | free(out); |
260
26f5f817429e
fixes build with openssl 1.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
237
diff
changeset
|
130 | //EVP_CIPHER_CTX_cleanup(&dec->ctx); |
26f5f817429e
fixes build with openssl 1.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
237
diff
changeset
|
131 | EVP_CIPHER_CTX_free(dec->ctx); |
237
fd9135bc7580
fixed crash when encrypted streams are empty
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
207
diff
changeset
|
132 | } |
207
de23f8881e9f
fixed hash verification and head requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
185
diff
changeset
|
133 | } |
de23f8881e9f
fixed hash verification and head requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
185
diff
changeset
|
134 | |
de23f8881e9f
fixed hash verification and head requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
185
diff
changeset
|
135 | void aes_decrypter_close(AESDecrypter *dec) { |
40
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
136 | free(dec); |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
137 | } |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
138 | |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
139 | |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
140 | AESEncrypter* aes_encrypter_new(DavKey *key, void *stream, dav_read_func read_func) { |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
141 | unsigned char *iv = malloc(16); |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
142 | if(!RAND_bytes(iv, 16)) { |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
143 | free(iv); |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
144 | return NULL; |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
145 | } |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
146 | |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
147 | AESEncrypter *enc = malloc(sizeof(AESEncrypter)); |
150
37fb12574acd
added checksums for encrypted resources
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
75
diff
changeset
|
148 | SHA256_Init(&enc->sha256); |
40
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
149 | enc->stream = stream; |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
150 | enc->read = read_func; |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
151 | enc->tmp = NULL; |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
152 | enc->tmplen = 0; |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
153 | enc->tmpoff = 0; |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
154 | enc->end = 0; |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
155 | enc->iv = iv; |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
156 | enc->ivlen = 16; |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
157 | |
260
26f5f817429e
fixes build with openssl 1.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
237
diff
changeset
|
158 | //EVP_CIPHER_CTX_init(&enc->ctx); |
26f5f817429e
fixes build with openssl 1.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
237
diff
changeset
|
159 | enc->ctx = EVP_CIPHER_CTX_new(); |
40
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
160 | if(key->type == DAV_KEY_AES128) { |
260
26f5f817429e
fixes build with openssl 1.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
237
diff
changeset
|
161 | EVP_EncryptInit_ex(enc->ctx, EVP_aes_128_cbc(), NULL, key->data, enc->iv); |
40
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
162 | } else if(key->type == DAV_KEY_AES256) { |
260
26f5f817429e
fixes build with openssl 1.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
237
diff
changeset
|
163 | EVP_EncryptInit_ex(enc->ctx, EVP_aes_256_cbc(), NULL, key->data, enc->iv); |
40
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
164 | } else { |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
165 | fprintf(stderr, "unknown key type\n"); |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
166 | exit(-1); |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
167 | } |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
168 | return enc; |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
169 | } |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
170 | |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
171 | size_t aes_read(void *buf, size_t s, size_t n, AESEncrypter *enc) { |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
172 | size_t len = s*n; |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
173 | if(enc->tmp) { |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
174 | size_t tmp_diff = enc->tmplen - enc->tmpoff; |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
175 | size_t cp_len = tmp_diff > len ? len : tmp_diff; |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
176 | memcpy(buf, enc->tmp + enc->tmpoff, cp_len); |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
177 | enc->tmpoff += cp_len; |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
178 | if(enc->tmpoff >= enc->tmplen) { |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
179 | free(enc->tmp); |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
180 | enc->tmp = NULL; |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
181 | enc->tmplen = 0; |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
182 | enc->tmpoff = 0; |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
183 | } |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
184 | return cp_len / s; |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
185 | } |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
186 | |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
187 | if(enc->end) { |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
188 | return 0; |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
189 | } |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
190 | |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
191 | void *in = malloc(len); |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
192 | size_t in_len = enc->read(in, 1, len, enc->stream); |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
193 | |
150
37fb12574acd
added checksums for encrypted resources
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
75
diff
changeset
|
194 | SHA256_Update(&enc->sha256, in, in_len); |
37fb12574acd
added checksums for encrypted resources
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
75
diff
changeset
|
195 | |
40
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
196 | unsigned char *out = NULL; |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
197 | int outlen = 0; |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
198 | size_t ivl = enc->ivlen; |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
199 | if(in_len != 0) { |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
200 | outlen = len + 16; |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
201 | out = malloc(outlen + ivl); |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
202 | if(enc->iv) { |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
203 | memcpy(out, enc->iv, ivl); |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
204 | } |
260
26f5f817429e
fixes build with openssl 1.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
237
diff
changeset
|
205 | EVP_EncryptUpdate(enc->ctx, out + ivl, &outlen, in, in_len); |
40
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
206 | free(in); |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
207 | } else { |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
208 | out = malloc(16); |
260
26f5f817429e
fixes build with openssl 1.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
237
diff
changeset
|
209 | EVP_EncryptFinal_ex(enc->ctx, out, &outlen); |
40
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
210 | enc->end = 1; |
150
37fb12574acd
added checksums for encrypted resources
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
75
diff
changeset
|
211 | free(in); |
40
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
212 | } |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
213 | enc->tmp = (char*)out; |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
214 | enc->tmplen = outlen + ivl; |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
215 | enc->tmpoff = 0; |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
216 | |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
217 | if(enc->iv) { |
150
37fb12574acd
added checksums for encrypted resources
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
75
diff
changeset
|
218 | free(enc->iv); |
40
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
219 | enc->iv = NULL; |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
220 | enc->ivlen = 0; |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
221 | } |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
222 | |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
223 | return aes_read(buf, s, n, enc); |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
224 | } |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
225 | |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
226 | void aes_encrypter_close(AESEncrypter *enc) { |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
227 | if(enc->tmp) { |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
228 | free(enc->tmp); |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
229 | } |
150
37fb12574acd
added checksums for encrypted resources
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
75
diff
changeset
|
230 | if(enc->iv) { |
37fb12574acd
added checksums for encrypted resources
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
75
diff
changeset
|
231 | free(enc->iv); |
37fb12574acd
added checksums for encrypted resources
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
75
diff
changeset
|
232 | } |
260
26f5f817429e
fixes build with openssl 1.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
237
diff
changeset
|
233 | //EVP_CIPHER_CTX_cleanup(&enc->ctx); |
26f5f817429e
fixes build with openssl 1.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
237
diff
changeset
|
234 | EVP_CIPHER_CTX_free(enc->ctx); |
40
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
235 | free(enc); |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
236 | } |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
237 | |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
238 | |
150
37fb12574acd
added checksums for encrypted resources
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
75
diff
changeset
|
239 | char* aes_encrypt(char *in, size_t len, DavKey *key) { |
43
03076907b58a
added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
40
diff
changeset
|
240 | unsigned char iv[16]; |
40
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
241 | if(!RAND_bytes(iv, 16)) { |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
242 | return NULL; |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
243 | } |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
244 | |
260
26f5f817429e
fixes build with openssl 1.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
237
diff
changeset
|
245 | //EVP_CIPHER_CTX ctx; |
26f5f817429e
fixes build with openssl 1.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
237
diff
changeset
|
246 | //EVP_CIPHER_CTX_init(&ctx); |
26f5f817429e
fixes build with openssl 1.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
237
diff
changeset
|
247 | EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new(); |
40
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
248 | if(key->type == DAV_KEY_AES128) { |
43
03076907b58a
added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
40
diff
changeset
|
249 | EVP_EncryptInit_ex( |
260
26f5f817429e
fixes build with openssl 1.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
237
diff
changeset
|
250 | ctx, |
43
03076907b58a
added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
40
diff
changeset
|
251 | EVP_aes_128_cbc(), |
03076907b58a
added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
40
diff
changeset
|
252 | NULL, |
03076907b58a
added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
40
diff
changeset
|
253 | (unsigned char*)key->data, |
03076907b58a
added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
40
diff
changeset
|
254 | iv); |
40
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
255 | } else if(key->type == DAV_KEY_AES256) { |
43
03076907b58a
added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
40
diff
changeset
|
256 | EVP_EncryptInit_ex( |
260
26f5f817429e
fixes build with openssl 1.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
237
diff
changeset
|
257 | ctx, |
43
03076907b58a
added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
40
diff
changeset
|
258 | EVP_aes_256_cbc(), |
03076907b58a
added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
40
diff
changeset
|
259 | NULL, |
03076907b58a
added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
40
diff
changeset
|
260 | (unsigned char*)key->data, |
03076907b58a
added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
40
diff
changeset
|
261 | iv); |
40
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
262 | } else { |
260
26f5f817429e
fixes build with openssl 1.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
237
diff
changeset
|
263 | //EVP_CIPHER_CTX_cleanup(&ctx); |
26f5f817429e
fixes build with openssl 1.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
237
diff
changeset
|
264 | EVP_CIPHER_CTX_free(ctx); |
40
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
265 | return NULL; |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
266 | } |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
267 | |
150
37fb12574acd
added checksums for encrypted resources
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
75
diff
changeset
|
268 | //int len = strlen(in); |
40
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
269 | int buflen = len + 64; |
43
03076907b58a
added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
40
diff
changeset
|
270 | unsigned char *buf = calloc(1, buflen); |
40
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
271 | memcpy(buf, iv, 16); |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
272 | |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
273 | int l = buflen - 16; |
260
26f5f817429e
fixes build with openssl 1.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
237
diff
changeset
|
274 | EVP_EncryptUpdate(ctx, buf + 16, &l, (unsigned char*)in, len); |
40
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
275 | |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
276 | int f = 0; |
260
26f5f817429e
fixes build with openssl 1.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
237
diff
changeset
|
277 | EVP_EncryptFinal_ex(ctx, buf + 16 + l, &f); |
43
03076907b58a
added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
40
diff
changeset
|
278 | char *out = util_base64encode((char*)buf, 16 + l + f); |
40
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
279 | free(buf); |
260
26f5f817429e
fixes build with openssl 1.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
237
diff
changeset
|
280 | EVP_CIPHER_CTX_free(ctx); |
26f5f817429e
fixes build with openssl 1.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
237
diff
changeset
|
281 | //EVP_CIPHER_CTX_cleanup(&ctx); |
150
37fb12574acd
added checksums for encrypted resources
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
75
diff
changeset
|
282 | |
40
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
283 | return out; |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
284 | } |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
285 | |
150
37fb12574acd
added checksums for encrypted resources
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
75
diff
changeset
|
286 | char* aes_decrypt(char *in, size_t *length, DavKey *key) { |
40
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
287 | int len; |
43
03076907b58a
added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
40
diff
changeset
|
288 | unsigned char *buf = (unsigned char*)util_base64decode_len(in, &len); |
40
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
289 | |
260
26f5f817429e
fixes build with openssl 1.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
237
diff
changeset
|
290 | //EVP_CIPHER_CTX ctx; |
26f5f817429e
fixes build with openssl 1.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
237
diff
changeset
|
291 | //EVP_CIPHER_CTX_init(&ctx); |
26f5f817429e
fixes build with openssl 1.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
237
diff
changeset
|
292 | EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new(); |
40
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
293 | if(key->type == DAV_KEY_AES128) { |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
294 | EVP_DecryptInit_ex( |
260
26f5f817429e
fixes build with openssl 1.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
237
diff
changeset
|
295 | ctx, |
40
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
296 | EVP_aes_128_cbc(), |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
297 | NULL, |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
298 | key->data, |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
299 | buf); |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
300 | } else if(key->type == DAV_KEY_AES256) { |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
301 | EVP_DecryptInit_ex( |
260
26f5f817429e
fixes build with openssl 1.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
237
diff
changeset
|
302 | ctx, |
40
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
303 | EVP_aes_256_cbc(), |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
304 | NULL, |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
305 | key->data, |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
306 | buf); |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
307 | } else { |
260
26f5f817429e
fixes build with openssl 1.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
237
diff
changeset
|
308 | //EVP_CIPHER_CTX_cleanup(&ctx); |
26f5f817429e
fixes build with openssl 1.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
237
diff
changeset
|
309 | EVP_CIPHER_CTX_free(ctx); |
40
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
310 | return NULL; |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
311 | } |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
312 | |
43
03076907b58a
added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
40
diff
changeset
|
313 | unsigned char *out = malloc(len + 1); |
40
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
314 | int outlen = len; |
43
03076907b58a
added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
40
diff
changeset
|
315 | unsigned char *in_buf = buf + 16; |
40
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
316 | int inlen = len - 16; |
43
03076907b58a
added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
40
diff
changeset
|
317 | int f = 0; |
40
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
318 | |
260
26f5f817429e
fixes build with openssl 1.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
237
diff
changeset
|
319 | EVP_DecryptUpdate(ctx, out, &outlen, in_buf, inlen); |
26f5f817429e
fixes build with openssl 1.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
237
diff
changeset
|
320 | EVP_DecryptFinal_ex(ctx, out + outlen, &f); |
40
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
321 | out[outlen + f] = '\0'; |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
322 | free(buf); |
260
26f5f817429e
fixes build with openssl 1.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
237
diff
changeset
|
323 | //EVP_CIPHER_CTX_cleanup(&ctx); |
26f5f817429e
fixes build with openssl 1.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
237
diff
changeset
|
324 | EVP_CIPHER_CTX_free(ctx); |
150
37fb12574acd
added checksums for encrypted resources
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
75
diff
changeset
|
325 | |
37fb12574acd
added checksums for encrypted resources
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
75
diff
changeset
|
326 | *length = outlen + f; |
43
03076907b58a
added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
40
diff
changeset
|
327 | return (char*)out; |
40
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
328 | } |
150
37fb12574acd
added checksums for encrypted resources
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
75
diff
changeset
|
329 | |
37fb12574acd
added checksums for encrypted resources
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
75
diff
changeset
|
330 | |
354
067ea2315a8a
moves auth prompt functionality to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
349
diff
changeset
|
331 | void dav_get_hash(DAV_SHA_CTX *sha256, unsigned char *buf){ |
155
b85248a8aee2
fixed some cases of incompatible types in crypto functions
Mike Becker <universe@uap-core.de>
parents:
150
diff
changeset
|
332 | SHA256_Final((unsigned char*)buf, sha256); |
150
37fb12574acd
added checksums for encrypted resources
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
75
diff
changeset
|
333 | } |
349
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
334 | |
367
4a6a59f89f9f
adds dav-sync config for autodetecting tag changes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
354
diff
changeset
|
335 | char* dav_create_hash(const char *data, size_t len) { |
4a6a59f89f9f
adds dav-sync config for autodetecting tag changes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
354
diff
changeset
|
336 | unsigned char hash[DAV_SHA256_DIGEST_LENGTH]; |
4a6a59f89f9f
adds dav-sync config for autodetecting tag changes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
354
diff
changeset
|
337 | SHA256((const unsigned char*)data, len, hash); |
4a6a59f89f9f
adds dav-sync config for autodetecting tag changes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
354
diff
changeset
|
338 | return util_hexstr(hash, DAV_SHA256_DIGEST_LENGTH); |
4a6a59f89f9f
adds dav-sync config for autodetecting tag changes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
354
diff
changeset
|
339 | } |
4a6a59f89f9f
adds dav-sync config for autodetecting tag changes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
354
diff
changeset
|
340 | |
349
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
341 | #endif |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
342 | |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
343 | |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
344 | /* -------------------- Apple Crypto Functions -------------------- */ |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
345 | #ifdef __APPLE__ |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
346 | |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
347 | #define RANDOM_BUFFER_LENGTH 256 |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
348 | static char randbuf[RANDOM_BUFFER_LENGTH]; |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
349 | static int rbufpos = RANDOM_BUFFER_LENGTH; |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
350 | |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
351 | int dav_rand_bytes(unsigned char *buf, size_t len) { |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
352 | if(len + rbufpos > RANDOM_BUFFER_LENGTH) { |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
353 | int devr = open("/dev/urandom", O_RDONLY); |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
354 | if(devr == -1) { |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
355 | return 1; |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
356 | } |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
357 | |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
358 | if(read(devr, randbuf, RANDOM_BUFFER_LENGTH) < RANDOM_BUFFER_LENGTH) { |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
359 | close(devr); |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
360 | return 1; |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
361 | } |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
362 | |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
363 | rbufpos = 0; |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
364 | if(len > RANDOM_BUFFER_LENGTH) { |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
365 | int err = 0; |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
366 | if(read(devr, buf, len) < len) { |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
367 | err = 1; |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
368 | } |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
369 | close(devr); |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
370 | return err; |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
371 | } |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
372 | |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
373 | close(devr); |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
374 | } |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
375 | |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
376 | char *r = randbuf; |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
377 | memcpy(buf, r + rbufpos, len); |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
378 | rbufpos += len; |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
379 | |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
380 | return 0; |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
381 | } |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
382 | |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
383 | AESDecrypter* aes_decrypter_new(DavKey *key, void *stream, dav_write_func write_func) { |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
384 | AESDecrypter *dec = calloc(1, sizeof(AESDecrypter)); |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
385 | CC_SHA256_Init(&dec->sha256); |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
386 | dec->stream = stream; |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
387 | dec->write = write_func; |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
388 | dec->key = key; |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
389 | dec->init = 0; |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
390 | dec->ivpos = 0; |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
391 | |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
392 | return dec; |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
393 | } |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
394 | |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
395 | |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
396 | void aes_decrypter_init(AESDecrypter *dec) { |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
397 | //EVP_CIPHER_CTX_init(&dec->ctx); |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
398 | dec->init = 1; |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
399 | |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
400 | CCCryptorRef cryptor; |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
401 | CCCryptorStatus status; |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
402 | if(dec->key->type == DAV_KEY_AES128) { |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
403 | status = CCCryptorCreate(kCCDecrypt, kCCAlgorithmAES128, kCCOptionPKCS7Padding, dec->key->data, dec->key->length, dec->ivtmp, &cryptor); |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
404 | } else if(dec->key->type == DAV_KEY_AES256) { |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
405 | status = CCCryptorCreate(kCCDecrypt, kCCAlgorithmAES, kCCOptionPKCS7Padding, dec->key->data, dec->key->length, dec->ivtmp, &cryptor); |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
406 | } else { |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
407 | fprintf(stderr, "unknown key type\n"); |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
408 | exit(-1); |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
409 | } |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
410 | dec->ctx = cryptor; |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
411 | } |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
412 | |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
413 | size_t aes_write(const void *buf, size_t s, size_t n, AESDecrypter *dec) { |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
414 | int len = s*n; |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
415 | if(!dec->init) { |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
416 | size_t n = 16 - dec->ivpos; |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
417 | size_t cp = n > len ? len : n; |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
418 | memcpy(dec->ivtmp + dec->ivpos, buf, cp); |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
419 | dec->ivpos += cp; |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
420 | if(dec->ivpos >= 16) { |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
421 | aes_decrypter_init(dec); |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
422 | } |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
423 | if(len == cp) { |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
424 | return len; |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
425 | } else { |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
426 | buf = (char*)buf + cp; |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
427 | len -= cp; |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
428 | } |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
429 | } |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
430 | |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
431 | int outlen = len + 16; |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
432 | unsigned char *out = malloc(outlen); |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
433 | |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
434 | CCCryptorStatus status; |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
435 | size_t avail = outlen; |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
436 | size_t moved = 0; |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
437 | status = CCCryptorUpdate(dec->ctx, buf, len, out, avail, &moved); |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
438 | |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
439 | ssize_t wlen = dec->write(out, 1, moved, dec->stream); |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
440 | CC_SHA256_Update(&dec->sha256, out, wlen); |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
441 | free(out); |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
442 | return (s*n) / s; |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
443 | } |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
444 | |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
445 | void aes_decrypter_shutdown(AESDecrypter *dec) { |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
446 | if(dec->init) { |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
447 | void *out = malloc(128); |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
448 | size_t len = 0; |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
449 | //EVP_DecryptFinal_ex(dec->ctx, out, &len); |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
450 | CCCryptorFinal(dec->ctx, out, 128, &len); |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
451 | |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
452 | |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
453 | dec->write(out, 1, len, dec->stream); |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
454 | CC_SHA256_Update(&dec->sha256, out, len); |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
455 | free(out); |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
456 | //EVP_CIPHER_CTX_cleanup(&dec->ctx); |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
457 | //EVP_CIPHER_CTX_free(dec->ctx); |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
458 | } |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
459 | } |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
460 | |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
461 | void aes_decrypter_close(AESDecrypter *dec) { |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
462 | |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
463 | } |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
464 | |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
465 | AESEncrypter* aes_encrypter_new(DavKey *key, void *stream, dav_read_func read_func) { |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
466 | unsigned char *iv = malloc(16); |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
467 | if(dav_rand_bytes(iv, 16)) { |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
468 | return NULL; |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
469 | } |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
470 | |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
471 | CCCryptorRef cryptor; |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
472 | CCCryptorStatus status; |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
473 | if(key->type == DAV_KEY_AES128) { |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
474 | status = CCCryptorCreate(kCCEncrypt, kCCAlgorithmAES128, kCCOptionPKCS7Padding, key->data, key->length, iv, &cryptor); |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
475 | } else if(key->type == DAV_KEY_AES256) { |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
476 | status = CCCryptorCreate(kCCEncrypt, kCCAlgorithmAES, kCCOptionPKCS7Padding, key->data, key->length, iv, &cryptor); |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
477 | } else { |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
478 | free(iv); |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
479 | return NULL; |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
480 | } |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
481 | |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
482 | AESEncrypter *enc = malloc(sizeof(AESEncrypter)); |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
483 | enc->ctx = cryptor; |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
484 | CC_SHA256_Init(&enc->sha256); |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
485 | enc->stream = stream; |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
486 | enc->read = read_func; |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
487 | enc->tmp = NULL; |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
488 | enc->tmplen = 0; |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
489 | enc->tmpoff = 0; |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
490 | enc->end = 0; |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
491 | enc->iv = iv; |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
492 | enc->ivlen = 16; |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
493 | |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
494 | return enc; |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
495 | } |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
496 | |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
497 | size_t aes_read(void *buf, size_t s, size_t n, AESEncrypter *enc) { |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
498 | size_t len = s*n; |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
499 | if(enc->tmp) { |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
500 | size_t tmp_diff = enc->tmplen - enc->tmpoff; |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
501 | size_t cp_len = tmp_diff > len ? len : tmp_diff; |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
502 | memcpy(buf, enc->tmp + enc->tmpoff, cp_len); |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
503 | enc->tmpoff += cp_len; |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
504 | if(enc->tmpoff >= enc->tmplen) { |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
505 | free(enc->tmp); |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
506 | enc->tmp = NULL; |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
507 | enc->tmplen = 0; |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
508 | enc->tmpoff = 0; |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
509 | } |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
510 | return cp_len / s; |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
511 | } |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
512 | |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
513 | if(enc->end) { |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
514 | return 0; |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
515 | } |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
516 | |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
517 | void *in = malloc(len); |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
518 | size_t in_len = enc->read(in, 1, len, enc->stream); |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
519 | |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
520 | CC_SHA256_Update(&enc->sha256, in, in_len); |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
521 | |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
522 | unsigned char *out = NULL; |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
523 | size_t outlen = 0; |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
524 | size_t ivl = enc->ivlen; |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
525 | if(in_len != 0) { |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
526 | outlen = len + 16; |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
527 | out = malloc(outlen + ivl); |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
528 | if(enc->iv) { |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
529 | memcpy(out, enc->iv, ivl); |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
530 | } |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
531 | |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
532 | CCCryptorStatus status; |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
533 | size_t avail = outlen; |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
534 | status = CCCryptorUpdate(enc->ctx, in, in_len, out + ivl, avail, &outlen); |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
535 | |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
536 | free(in); |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
537 | } else { |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
538 | out = malloc(32); |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
539 | CCCryptorStatus status; |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
540 | size_t avail = outlen; |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
541 | status = CCCryptorFinal(enc->ctx, out, 32, &outlen); |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
542 | enc->end = 1; |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
543 | free(in); |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
544 | } |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
545 | enc->tmp = (char*)out; |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
546 | enc->tmplen = outlen + ivl; |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
547 | enc->tmpoff = 0; |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
548 | |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
549 | if(enc->iv) { |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
550 | free(enc->iv); |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
551 | enc->iv = NULL; |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
552 | enc->ivlen = 0; |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
553 | } |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
554 | |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
555 | return aes_read(buf, s, n, enc); |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
556 | } |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
557 | |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
558 | void aes_encrypter_close(AESEncrypter *enc) { |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
559 | if(enc->tmp) { |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
560 | free(enc->tmp); |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
561 | } |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
562 | if(enc->iv) { |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
563 | free(enc->iv); |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
564 | } |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
565 | // TODO: cleanup cryptor |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
566 | free(enc); |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
567 | } |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
568 | |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
569 | char* aes_encrypt(char *in, size_t len, DavKey *key) { |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
570 | unsigned char iv[16]; |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
571 | if(dav_rand_bytes(iv, 16)) { |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
572 | return NULL; |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
573 | } |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
574 | |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
575 | CCCryptorRef cryptor; |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
576 | CCCryptorStatus status; |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
577 | if(key->type == DAV_KEY_AES128) { |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
578 | status = CCCryptorCreate(kCCEncrypt, kCCAlgorithmAES128, kCCOptionPKCS7Padding, key->data, key->length, iv, &cryptor); |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
579 | } else if(key->type == DAV_KEY_AES256) { |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
580 | status = CCCryptorCreate(kCCEncrypt, kCCAlgorithmAES, kCCOptionPKCS7Padding, key->data, key->length, iv, &cryptor); |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
581 | } else { |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
582 | return NULL; |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
583 | } |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
584 | |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
585 | if(status != kCCSuccess) { |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
586 | return NULL; |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
587 | } |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
588 | |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
589 | int buflen = len + 64; |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
590 | char *buf = calloc(1, buflen); |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
591 | memcpy(buf, iv, 16); |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
592 | |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
593 | int pos = 16; |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
594 | size_t avail = buflen - 16; |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
595 | size_t moved; |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
596 | char *out = buf + 16; |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
597 | |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
598 | status = CCCryptorUpdate(cryptor, in, |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
599 | len, out, avail, |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
600 | &moved); |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
601 | if(status != kCCSuccess) { |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
602 | free(buf); |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
603 | return NULL; |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
604 | } |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
605 | |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
606 | pos += moved; |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
607 | avail -= moved; |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
608 | out += moved; |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
609 | |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
610 | status = CCCryptorFinal(cryptor, out, avail, &moved); |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
611 | if(status != kCCSuccess) { |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
612 | free(buf); |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
613 | return NULL; |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
614 | } |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
615 | |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
616 | pos += moved; |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
617 | |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
618 | char *b64enc = util_base64encode(buf, pos); |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
619 | free(buf); |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
620 | |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
621 | return b64enc; |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
622 | } |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
623 | |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
624 | char* aes_decrypt(char *in, size_t *len, DavKey *key) { |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
625 | int inlen; |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
626 | unsigned char *buf = (unsigned char*)util_base64decode_len(in, &inlen); |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
627 | |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
628 | CCCryptorRef cryptor; |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
629 | CCCryptorStatus status; |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
630 | if(key->type == DAV_KEY_AES128) { |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
631 | status = CCCryptorCreate(kCCDecrypt, kCCAlgorithmAES128, kCCOptionPKCS7Padding, key->data, key->length, buf, &cryptor); |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
632 | } else if(key->type == DAV_KEY_AES256) { |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
633 | status = CCCryptorCreate(kCCDecrypt, kCCAlgorithmAES, kCCOptionPKCS7Padding, key->data, key->length, buf, &cryptor); |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
634 | } else { |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
635 | free(buf); |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
636 | return NULL; |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
637 | } |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
638 | |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
639 | if(status != kCCSuccess) { |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
640 | free(buf); |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
641 | return NULL; |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
642 | } |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
643 | |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
644 | char *out = malloc(inlen + 1); |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
645 | size_t outavail = inlen; |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
646 | size_t outlen = 0; |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
647 | |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
648 | unsigned char *inbuf = buf + 16; |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
649 | inlen -= 16; |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
650 | |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
651 | size_t moved = 0; |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
652 | status = CCCryptorUpdate(cryptor, inbuf, inlen, out, outavail, &moved); |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
653 | if(status != kCCSuccess) { |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
654 | free(buf); |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
655 | free(out); |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
656 | // TODO cryptor |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
657 | return NULL; |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
658 | } |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
659 | |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
660 | outlen += moved; |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
661 | outavail -= moved; |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
662 | |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
663 | status = CCCryptorFinal(cryptor, out + outlen, outavail, &moved); |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
664 | if(status != kCCSuccess) { |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
665 | free(buf); |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
666 | free(out); |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
667 | // TODO cryptor |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
668 | return NULL; |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
669 | } |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
670 | |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
671 | outlen += moved; |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
672 | out[outlen] = 0; |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
673 | |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
674 | *len = outlen; |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
675 | return out; |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
676 | } |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
677 | |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
678 | void dav_get_hash(DAV_SHA_CTX *sha256, unsigned char *buf) { |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
679 | CC_SHA256_Final(buf, sha256); |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
680 | } |
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
681 | |
367
4a6a59f89f9f
adds dav-sync config for autodetecting tag changes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
354
diff
changeset
|
682 | char* dav_create_hash(const char *data, size_t len) { |
4a6a59f89f9f
adds dav-sync config for autodetecting tag changes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
354
diff
changeset
|
683 | unsigned char hash[DAV_SHA256_DIGEST_LENGTH]; |
4a6a59f89f9f
adds dav-sync config for autodetecting tag changes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
354
diff
changeset
|
684 | CC_SHA256((const unsigned char*)data, len, hash); |
4a6a59f89f9f
adds dav-sync config for autodetecting tag changes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
354
diff
changeset
|
685 | return util_hexstr(hash, DAV_SHA256_DIGEST_LENGTH); |
4a6a59f89f9f
adds dav-sync config for autodetecting tag changes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
354
diff
changeset
|
686 | } |
4a6a59f89f9f
adds dav-sync config for autodetecting tag changes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
354
diff
changeset
|
687 | |
349
0b4ecadaf3f9
ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
267
diff
changeset
|
688 | #endif |