2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
4 * Copyright 2018 Olaf Wintermann. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
33 #include <ucx/string.h>
38 #define DAV_CRYPTO_COMMON_CRYPTO
40 #define DAV_AES_CTX CCCryptorRef
41 #define DAV_SHA_CTX CC_SHA256_CTX
42 #define DAV_SHA256_DIGEST_LENGTH 32
44 #include <CommonCrypto/CommonCrypto.h>
45 #include <CommonCrypto/CommonDigest.h>
49 #define DAV_CRYPTO_CNG
54 typedef struct WinBCryptCTX {
55 BCRYPT_ALG_HANDLE hAlg;
56 BCRYPT_KEY_HANDLE hKey;
58 unsigned char pbIV[16];
60 unsigned char buf[16];
64 typedef struct WinBCryptSHACTX {
65 BCRYPT_ALG_HANDLE hAlg;
66 BCRYPT_HASH_HANDLE hHash;
70 #define DAV_AES_CTX WinBCryptCTX
71 #define DAV_SHA_CTX WinBCryptSHACTX
72 #define DAV_SHA256_DIGEST_LENGTH 32
77 #define DAV_USE_OPENSSL
79 #define DAV_AES_CTX EVP_CIPHER_CTX*
80 #define DAV_SHA_CTX SHA256_CTX
81 #define DAV_SHA256_DIGEST_LENGTH 32
83 #include <openssl/evp.h>
84 #include <openssl/rand.h>
86 #if defined(__sun) && defined(__SunOS_5_10)
88 #define SHA256_Init SHA256Init
89 #define SHA256_Update SHA256Update
90 #define SHA256_Final SHA256Final
92 #include <openssl/sha.h>
101 #define DAV_PWFUNC_PBKDF2_SHA256 0
102 #define DAV_PWFUNC_PBKDF2_SHA512 1
104 #define DAV_CRYPTO_ITERATION_COUNT 4000
110 dav_write_func write;
113 unsigned char ivtmp[16];
131 typedef struct DavHashContext DavHashContext;
133 int dav_rand_bytes(unsigned char *buf, size_t len);
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);
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);
145 char* aes_encrypt(const char *in, size_t len, DavKey *key);
146 char* aes_decrypt(const char *in, size_t *len, DavKey *key);
148 void dav_get_hash(DAV_SHA_CTX *sha256, unsigned char *buf);
150 char* dav_create_hash(const char *data, size_t len);
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);
156 DavKey* dav_pw2key(const char *password, const unsigned char *salt, int saltlen, int pwfunc, int enc);
158 UcxBuffer* aes_encrypt_buffer(UcxBuffer *buf, DavKey *key);
159 UcxBuffer* aes_decrypt_buffer(UcxBuffer *buf, DavKey *key);
165 #endif /* DAV_CRYPTO_H */