move control socket handling to separate file
[mizunara.git] / libidav / crypto.h
1 /*
2  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3  *
4  * Copyright 2018 Olaf Wintermann. All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  *
9  *   1. Redistributions of source code must retain the above copyright
10  *      notice, this list of conditions and the following disclaimer.
11  *
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.
15  *
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.
27  */
28
29 #ifndef DAV_CRYPTO_H
30 #define DAV_CRYPTO_H
31
32 #include "webdav.h"
33 #include <ucx/string.h>
34
35 #ifdef __APPLE__
36 /* macos */
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 /* unix/linux */
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 UcxBuffer* aes_encrypt_buffer(UcxBuffer *buf, DavKey *key);
159 UcxBuffer* aes_decrypt_buffer(UcxBuffer *buf, DavKey *key);
160
161 #ifdef  __cplusplus
162 }
163 #endif
164
165 #endif  /* DAV_CRYPTO_H */
166