1 /* 2 * UFTP - UDP based FTP with multicast 3 * 4 * Copyright (C) 2001-2020 Dennis A. Bush, Jr. bush@tcnj.edu 5 * 6 * This program is free software: you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License as published by 8 * the Free Software Foundation, either version 3 of the License, or 9 * (at your option) any later version. 10 * 11 * This program is distributed in the hope that it will be useful, 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 * GNU General Public License for more details. 15 * 16 * You should have received a copy of the GNU General Public License 17 * along with this program. If not, see <http://www.gnu.org/licenses/>. 18 * 19 * Additional permission under GNU GPL version 3 section 7 20 * 21 * If you modify this program, or any covered work, by linking or 22 * combining it with the OpenSSL project's OpenSSL library (or a 23 * modified version of that library), containing parts covered by the 24 * terms of the OpenSSL or SSLeay licenses, the copyright holder 25 * grants you additional permission to convey the resulting work. 26 * Corresponding Source for a non-source form of such a combination 27 * shall include the source code for the parts of OpenSSL used as well 28 * as that of the covered work. 29 */ 30 31 #ifndef _ENCRYPTION_H 32 #define _ENCRYPTION_H 33 34 // This section includes items normally listed in uftp_common.h 35 // that are required in encrypt_cng.c. See encrypt_cng.c for more details. 36 #ifdef NO_UFTP_COMMON_H 37 38 #include <stdio.h> 39 extern int showtime; 40 extern FILE *applog; 41 extern int log_level; 42 43 void logfunc(uint32_t group_id, uint16_t file_id, int level, int _showtime, 44 int newline, int err, int sockerr, const char *str, ...); 45 46 #define clog0(group_id, file_id, ...) \ 47 logfunc(group_id, file_id, 0, showtime, 0, 0, 0, __VA_ARGS__) 48 #define log0(group_id, file_id, ...) \ 49 logfunc(group_id, file_id, 0, showtime, 1, 0, 0, __VA_ARGS__) 50 #define sclog2(...) \ 51 logfunc(0, 0, 2, 0, 0, 0, 0, __VA_ARGS__) 52 #define syserror(group_id, file_id, ...) \ 53 logfunc(group_id, file_id, 0, showtime, 1, errno, 0, __VA_ARGS__) 54 55 int is_auth_enc(int keytype); 56 int is_gcm_mode(int keytype); 57 int is_ccm_mode(int keytype); 58 59 void *safe_malloc(size_t size); 60 void *safe_calloc(size_t num, size_t size); 61 62 #endif 63 64 #ifdef NO_ENCRYPTION 65 66 typedef void *RSA_key_t; 67 typedef void *EC_key_t; 68 69 #elif defined WINDOWS && !defined OPENSSL &&\ 70 (_WIN32_WINNT >= _WIN32_WINNT_LONGHORN) 71 72 #include <windows.h> 73 #include <bcrypt.h> 74 #include <ncrypt.h> 75 typedef NCRYPT_KEY_HANDLE RSA_key_t; 76 typedef NCRYPT_KEY_HANDLE EC_key_t; 77 78 #elif defined WINDOWS && !defined OPENSSL 79 80 #include <windows.h> 81 #include <wincrypt.h> 82 typedef HCRYPTKEY RSA_key_t; 83 typedef void *EC_key_t; 84 85 #else 86 87 #include <openssl/rsa.h> 88 typedef RSA *RSA_key_t; 89 90 #include <openssl/ec.h> 91 #include <openssl/ecdh.h> 92 #include <openssl/ecdsa.h> 93 typedef EC_KEY *EC_key_t; 94 95 #endif 96 97 union key_t { 98 uint64_t key; 99 RSA_key_t rsa; 100 EC_key_t ec; 101 }; 102 103 void crypto_init(int set_sys_key); 104 105 void crypto_cleanup(void); 106 107 int cipher_supported(int keytype); 108 109 int hash_supported(int hashtype); 110 111 void get_key_info(int keytype, int *keylen, int *ivlen); 112 113 int get_hash_len(int hashtype); 114 115 int get_random_bytes(unsigned char *buf, int num); 116 117 int encrypt_block(int keytype, const unsigned char *IV, 118 const unsigned char *key, 119 const unsigned char *aad, unsigned int aadlen, 120 const unsigned char *src, unsigned int srclen, 121 unsigned char *dest, unsigned int *destlen); 122 123 int decrypt_block(int keytype, const unsigned char *IV, 124 const unsigned char *key, 125 const unsigned char *aad, unsigned int aadlen, 126 unsigned char *src, unsigned int srclen, 127 unsigned char *dest, unsigned int *destlen); 128 129 int create_hmac(int hashtype, const unsigned char *key, unsigned int keylen, 130 const unsigned char *src, unsigned int srclen, 131 unsigned char *dest, unsigned int *destlen); 132 133 int hash(int hashtype, const unsigned char *src, unsigned int srclen, 134 unsigned char *dest, unsigned int *destlen); 135 136 int RSA_keylen(const RSA_key_t rsa); 137 138 int EC_keylen(const EC_key_t ec); 139 140 int ECDSA_siglen(const EC_key_t ec); 141 142 int RSA_encrypt(RSA_key_t rsa, const unsigned char *from, unsigned int fromlen, 143 unsigned char *to, unsigned int *tolen); 144 145 int RSA_decrypt(RSA_key_t rsa, const unsigned char *from, unsigned int fromlen, 146 unsigned char *to, unsigned int *tolen); 147 148 int create_RSA_sig(RSA_key_t rsa, int hashtype, 149 const unsigned char *mes, unsigned int meslen, 150 unsigned char *sig, unsigned int *siglen); 151 152 int verify_RSA_sig(RSA_key_t rsa, int hashtype, 153 const unsigned char *mes, unsigned int meslen, 154 const unsigned char *sig, unsigned int siglen); 155 156 int create_ECDSA_sig(EC_key_t ec, int hashtype, 157 const unsigned char *mes, unsigned int meslen, 158 unsigned char *sig, unsigned int *siglen); 159 160 int verify_ECDSA_sig(EC_key_t ec, int hashtype, 161 const unsigned char *mes, unsigned int meslen, 162 const unsigned char *sig, unsigned int siglen); 163 164 int get_ECDH_key(EC_key_t pubkey, EC_key_t privkey, unsigned char *key, 165 unsigned int *keylen, int kdf_hash); 166 167 int import_RSA_key(RSA_key_t *rsa, const unsigned char *keyblob, 168 uint16_t bloblen); 169 170 int export_RSA_key(const RSA_key_t rsa, unsigned char *keyblob, 171 uint16_t *bloblen); 172 173 int import_EC_key(EC_key_t *ec, const unsigned char *keyblob, uint16_t bloblen, 174 int isdh); 175 176 int export_EC_key(const EC_key_t ec, unsigned char *keyblob, uint16_t *bloblen); 177 178 RSA_key_t gen_RSA_key(int bits, int exponent, const char *filename); 179 180 RSA_key_t read_RSA_key(const char *filename); 181 182 EC_key_t gen_EC_key(uint8_t curve, int isdh, const char *filename); 183 184 EC_key_t read_EC_key(const char *filename); 185 186 union key_t read_private_key(const char *filename, int *keytype); 187 188 uint8_t get_EC_curve(const EC_key_t ec); 189 190 void free_RSA_key(RSA_key_t rsa); 191 192 void free_EC_key(EC_key_t ec); 193 194 const char *get_next_container(void); 195 196 void delete_container(const char *name); 197 198 void set_sys_keys(int set); 199 200 201 #endif // _ENCRYPTION_H 202 203