28 |
28 |
29 #ifndef DAV_CRYPTO_H |
29 #ifndef DAV_CRYPTO_H |
30 #define DAV_CRYPTO_H |
30 #define DAV_CRYPTO_H |
31 |
31 |
32 #include "webdav.h" |
32 #include "webdav.h" |
|
33 #include <ucx/string.h> |
|
34 |
|
35 #ifdef __APPLE__ |
|
36 /* macos */ |
|
37 #define DAV_AES_CTX CCCryptorRef |
|
38 #define DAV_SHA_CTX CC_SHA256_CTX |
|
39 #define DAV_SHA256_DIGEST_LENGTH 32 |
|
40 |
|
41 #include <CommonCrypto/CommonCrypto.h> |
|
42 #include <CommonCrypto/CommonDigest.h> |
|
43 |
|
44 #else |
|
45 /* unix/linux and still windows */ |
|
46 #define DAV_AES_CTX EVP_CIPHER_CTX* |
|
47 #define DAV_SHA_CTX SHA256_CTX |
|
48 #define DAV_SHA256_DIGEST_LENGTH 32 |
|
49 |
|
50 #define DAV_USE_OPENSSL |
|
51 |
33 #include <openssl/evp.h> |
52 #include <openssl/evp.h> |
34 #include <ucx/string.h> |
53 #include <openssl/rand.h> |
35 |
54 |
36 #if defined(__sun) && defined(__SunOS_5_10) |
55 #if defined(__sun) && defined(__SunOS_5_10) |
37 #include <sha2.h> |
56 #include <sha2.h> |
38 #define SHA256_Init SHA256Init |
57 #define SHA256_Init SHA256Init |
39 #define SHA256_Update SHA256Update |
58 #define SHA256_Update SHA256Update |
40 #define SHA256_Final SHA256Final |
59 #define SHA256_Final SHA256Final |
41 #else |
60 #else |
42 #include <openssl/sha.h> |
61 #include <openssl/sha.h> |
43 #endif |
62 #endif |
44 |
63 |
|
64 #endif |
|
65 |
45 #ifdef __cplusplus |
66 #ifdef __cplusplus |
46 extern "C" { |
67 extern "C" { |
47 #endif |
68 #endif |
48 |
69 |
49 typedef struct { |
70 typedef struct { |
50 EVP_CIPHER_CTX *ctx; |
71 DAV_AES_CTX ctx; |
51 SHA256_CTX sha256; |
72 DAV_SHA_CTX sha256; |
52 void *stream; |
73 void *stream; |
53 dav_write_func write; |
74 dav_write_func write; |
54 DavKey *key; |
75 DavKey *key; |
55 int init; |
76 int init; |
56 unsigned char ivtmp[16]; |
77 unsigned char ivtmp[16]; |
57 size_t ivpos; |
78 size_t ivpos; |
58 } AESDecrypter; |
79 } AESDecrypter; |
59 |
80 |
60 typedef struct { |
81 typedef struct { |
61 EVP_CIPHER_CTX *ctx; |
82 DAV_AES_CTX ctx; |
62 SHA256_CTX sha256; |
83 DAV_SHA_CTX sha256; |
63 void *iv; |
84 void *iv; |
64 size_t ivlen; |
85 size_t ivlen; |
65 void *stream; |
86 void *stream; |
66 dav_read_func read; |
87 dav_read_func read; |
67 char *tmp; |
88 char *tmp; |
68 size_t tmplen; |
89 size_t tmplen; |
69 size_t tmpoff; |
90 size_t tmpoff; |
70 int end; |
91 int end; |
71 } AESEncrypter; |
92 } AESEncrypter; |
|
93 |
|
94 int dav_rand_bytes(unsigned char *buf, size_t len); |
72 |
95 |
73 AESDecrypter* aes_decrypter_new(DavKey *key, void *stream, dav_write_func write_func); |
96 AESDecrypter* aes_decrypter_new(DavKey *key, void *stream, dav_write_func write_func); |
74 size_t aes_write(const void *buf, size_t s, size_t n, AESDecrypter *dec); |
97 size_t aes_write(const void *buf, size_t s, size_t n, AESDecrypter *dec); |
75 void aes_decrypter_shutdown(AESDecrypter *dec); |
98 void aes_decrypter_shutdown(AESDecrypter *dec); |
76 void aes_decrypter_close(AESDecrypter *dec); |
99 void aes_decrypter_close(AESDecrypter *dec); |
80 void aes_encrypter_close(AESEncrypter *enc); |
103 void aes_encrypter_close(AESEncrypter *enc); |
81 |
104 |
82 char* aes_encrypt(char *in, size_t len, DavKey *key); |
105 char* aes_encrypt(char *in, size_t len, DavKey *key); |
83 char* aes_decrypt(char *in, size_t *len, DavKey *key); |
106 char* aes_decrypt(char *in, size_t *len, DavKey *key); |
84 |
107 |
85 void dav_get_hash(SHA256_CTX *sha256, unsigned char *buf); |
108 void dav_get_hash(DAV_SHA_CTX *sha256, unsigned char *buf); |
86 |
109 |
87 #ifdef __cplusplus |
110 #ifdef __cplusplus |
88 } |
111 } |
89 #endif |
112 #endif |
90 |
113 |