1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29 #ifndef DAV_CRYPTO_H
30 #define DAV_CRYPTO_H
31
32 #include "webdav.h"
33 #include <cx/string.h>
34
35 #ifdef __APPLE__
36
37
38 #define DAV_CRYPTO_COMMON_CRYPTO
39
40 #define DAV_AES_CTX CCCryptorRef
41 #define DAV_SHA_CTX CC_SHA256_CTX
42 #define DAV_SHA256_DIGEST_LENGTH 32
43
44 #include <CommonCrypto/CommonCrypto.h>
45 #include <CommonCrypto/CommonDigest.h>
46
47 #elif defined(
_WIN32)
48
49 #define DAV_CRYPTO_CNG
50
51 #include <windows.h>
52 #include <bcrypt.h>
53
54 typedef struct WinBCryptCTX {
55 BCRYPT_ALG_HANDLE hAlg;
56 BCRYPT_KEY_HANDLE hKey;
57 void *pbKeyObject;
58 unsigned char pbIV[
16];
59
60 unsigned char buf[
16];
61 ULONG buflen;
62 } WinBCryptCTX;
63
64 typedef struct WinBCryptSHACTX {
65 BCRYPT_ALG_HANDLE hAlg;
66 BCRYPT_HASH_HANDLE hHash;
67 void *pbHashObject;
68 } WinBCryptSHACTX;
69
70 #define DAV_AES_CTX WinBCryptCTX
71 #define DAV_SHA_CTX WinBCryptSHACTX
72 #define DAV_SHA256_DIGEST_LENGTH 32
73
74 #else
75
76
77 #define DAV_USE_OPENSSL
78
79 #define DAV_AES_CTX EVP_CIPHER_CTX*
80 #define DAV_SHA_CTX SHA256_CTX
81 #define DAV_SHA256_DIGEST_LENGTH 32
82
83 #include <openssl/evp.h>
84 #include <openssl/rand.h>
85
86 #if defined(__sun) && defined(__SunOS_5_10)
87 #include <sha2.h>
88 #define SHA256_Init SHA256Init
89 #define SHA256_Update SHA256Update
90 #define SHA256_Final SHA256Final
91 #else
92 #include <openssl/sha.h>
93 #endif
94
95 #endif
96
97 #ifdef __cplusplus
98 extern "C" {
99 #endif
100
101 #define DAV_PWFUNC_PBKDF2_SHA256 0
102 #define DAV_PWFUNC_PBKDF2_SHA512 1
103
104 #define DAV_CRYPTO_ITERATION_COUNT 4000
105
106 typedef struct {
107 DAV_AES_CTX ctx;
108 DAV_SHA_CTX sha256;
109 void *stream;
110 dav_write_func write;
111 DavKey *key;
112 int init;
113 unsigned char ivtmp[
16];
114 size_t ivpos;
115 } AESDecrypter;
116
117 typedef struct {
118 DAV_AES_CTX ctx;
119 DAV_SHA_CTX sha256;
120 void *iv;
121 size_t ivlen;
122 void *stream;
123 dav_read_func read;
124 dav_seek_func seek;
125 char *tmp;
126 size_t tmplen;
127 size_t tmpoff;
128 int end;
129 } AESEncrypter;
130
131 typedef struct DavHashContext DavHashContext;
132
133 int dav_rand_bytes(
unsigned char *buf,
size_t len);
134
135 AESDecrypter* aes_decrypter_new(DavKey *key,
void *stream, dav_write_func write_func);
136 size_t aes_write(
const void *buf,
size_t s,
size_t n, AESDecrypter *dec);
137 void aes_decrypter_shutdown(AESDecrypter *dec);
138 void aes_decrypter_close(AESDecrypter *dec);
139
140 AESEncrypter* aes_encrypter_new(DavKey *key,
void *stream, dav_read_func read_func, dav_seek_func seek_func);
141 size_t aes_read(
void *buf,
size_t s,
size_t n, AESEncrypter *enc);
142 void aes_encrypter_close(AESEncrypter *enc);
143 int aes_encrypter_reset(AESEncrypter *enc,
curl_off_t offset,
int origin);
144
145 char* aes_encrypt(
const char *in,
size_t len, DavKey *key);
146 char* aes_decrypt(
const char *in,
size_t *len, DavKey *key);
147
148 void dav_get_hash(
DAV_SHA_CTX *sha256,
unsigned char *buf);
149
150 char* dav_create_hash(
const char *data,
size_t len);
151
152 DAV_SHA_CTX* dav_hash_init(
void);
153 void dav_hash_update(
DAV_SHA_CTX *ctx,
const char *data,
size_t len);
154 void dav_hash_final(
DAV_SHA_CTX *ctx,
unsigned char *buf);
155
156 DavKey* dav_pw2key(
const char *password,
const unsigned char *salt,
int saltlen,
int pwfunc,
int enc);
157
158 CxBuffer* aes_encrypt_buffer(CxBuffer *in, DavKey *key);
159 CxBuffer* aes_decrypt_buffer(CxBuffer *in, DavKey *key);
160
161 #ifdef __cplusplus
162 }
163 #endif
164
165 #endif
166
167