diff --git a/pclsync/pcompiler.h b/pclsync/pcompiler.h index 140a7e5..ad3d356 100644 --- a/pclsync/pcompiler.h +++ b/pclsync/pcompiler.h @@ -32,132 +32,20 @@ #ifndef _PSYNC_COMPILER_H #define _PSYNC_COMPILER_H -#if defined(_MSC_VER) -#include -#endif - -#if !defined(__has_attribute) -#if defined(__GNUC__) -#define __has_attribute(x) 1 -#else -#define __has_attribute(x) 0 -#endif -#else -#if defined(__GNUC__) && !__has_attribute(malloc) -#undef __has_attribute -#define __has_attribute(x) 1 -#endif -#endif - -#ifndef __has_builtin -#if defined(__GNUC__) -#define __has_builtin(x) 1 -#else -#define __has_builtin(x) 0 -#endif -#endif - -#if __has_builtin(__builtin_expect) #define likely(expr) __builtin_expect(!!(expr), 1) #define unlikely(expr) __builtin_expect(!!(expr), 0) -#else -#define likely(expr) (expr) -#define unlikely(expr) (expr) -#endif - -#if __has_builtin(__builtin_prefetch) #define psync_prefetch(expr) __builtin_prefetch(expr) -#elif defined(_MSC_VER) -#define psync_prefetch(expr) _mm_prefetch((char *)(expr), _MM_HINT_T0) -#else -#define psync_prefetch(expr) ((void)0) -#endif - -#if defined(_MSC_VER) -#define PSYNC_THREAD __declspec(thread) -#define PSYNC_NOINLINE __declspec(noinline) -#else -#if __has_attribute(noinline) #define PSYNC_NOINLINE __attribute__((noinline)) -#else -#define PSYNC_NOINLINE -#endif #define PSYNC_THREAD __thread -#endif -#if __has_attribute(malloc) #define PSYNC_MALLOC __attribute__((malloc)) -#else -#define PSYNC_MALLOC -#endif - -#if __has_attribute(sentinel) #define PSYNC_SENTINEL __attribute__((sentinel)) -#else -#define PSYNC_SENTINEL -#endif - -#if __has_attribute(pure) #define PSYNC_PURE __attribute__((pure)) -#else -#define PSYNC_PURE -#endif - -#if __has_attribute(const) #define PSYNC_CONST __attribute__((const)) -#else -#define PSYNC_CONST -#endif - -#if __has_attribute(cold) #define PSYNC_COLD __attribute__((cold)) -#else -#define PSYNC_COLD -#endif - -#if __has_attribute(format) #define PSYNC_FORMAT(a, b, c) __attribute__((format(a, b, c))) -#else -#define PSYNC_FORMAT(a, b, c) -#endif - -#if __has_attribute(nonnull) #define PSYNC_NONNULL(...) __attribute__((nonnull(__VA_ARGS__))) -#else -#define PSYNC_NONNULL(...) -#endif - -#if __has_attribute(packed) #define PSYNC_PACKED_STRUCT struct __attribute__((packed)) -#elif defined(_MSC_VER) -#define PSYNC_PACKED_STRUCT __declspec(align(1)) struct -#else -#define PSYNC_PACKED_STRUCT struct -#endif - -#if _MSC_VER >= 1500 && _MSC_VER < 1600 -#define inline __inline -#define restrict __restrict -#elif __GNUC__ >= 3 -#define inline __inline -#define restrict __restrict -#elif __STDC_VERSION__ != 199901L -#define inline -#define restrict -#endif - -#if defined(__clang__) || defined(_MSC_VER) -#define psync_alignof __alignof -#elif defined(__GNUC__) #define psync_alignof __alignof__ -#else -#define psync_alignof(t) \ - offsetof( \ - struct { \ - char a; \ - t b; \ - }, \ - b) -#endif #endif diff --git a/pclsync/pssl.c b/pclsync/pssl.c index 5db24c7..2fb265b 100644 --- a/pclsync/pssl.c +++ b/pclsync/pssl.c @@ -174,41 +174,6 @@ static void psync_ssl_free_psync_encrypted_data_t(psync_encrypted_data_t e) { pmemlock_free(e); } -static inline void psync_aes256_encode_block(psync_aes256_encoder enc, const unsigned char *src, unsigned char *dst) { - mbedtls_aes_crypt_ecb(enc, MBEDTLS_AES_ENCRYPT, src, dst); -} - -static inline void psync_aes256_decode_block(psync_aes256_decoder enc, const unsigned char *src, unsigned char *dst) { - mbedtls_aes_crypt_ecb(enc, MBEDTLS_AES_DECRYPT, src, dst); -} - -static inline void psync_aes256_encode_2blocks_consec(psync_aes256_decoder enc, const unsigned char *src, unsigned char *dst) { - mbedtls_aes_crypt_ecb(enc, MBEDTLS_AES_ENCRYPT, src, dst); - mbedtls_aes_crypt_ecb(enc, MBEDTLS_AES_ENCRYPT, src + PSYNC_AES256_BLOCK_SIZE, - dst + PSYNC_AES256_BLOCK_SIZE); -} - -static inline void psync_aes256_decode_2blocks_consec(psync_aes256_decoder enc, const unsigned char *src, unsigned char *dst) { - mbedtls_aes_crypt_ecb(enc, MBEDTLS_AES_DECRYPT, src, dst); - mbedtls_aes_crypt_ecb(enc, MBEDTLS_AES_DECRYPT, src + PSYNC_AES256_BLOCK_SIZE, - dst + PSYNC_AES256_BLOCK_SIZE); -} - -static inline void psync_aes256_decode_4blocks_consec_xor(psync_aes256_decoder enc, const unsigned char *src, unsigned char *dst, unsigned char *bxor) { - unsigned long i; - mbedtls_aes_crypt_ecb(enc, MBEDTLS_AES_DECRYPT, src, dst); - mbedtls_aes_crypt_ecb(enc, MBEDTLS_AES_DECRYPT, src + PSYNC_AES256_BLOCK_SIZE, - dst + PSYNC_AES256_BLOCK_SIZE); - mbedtls_aes_crypt_ecb(enc, MBEDTLS_AES_DECRYPT, - src + PSYNC_AES256_BLOCK_SIZE * 2, - dst + PSYNC_AES256_BLOCK_SIZE * 2); - mbedtls_aes_crypt_ecb(enc, MBEDTLS_AES_DECRYPT, - src + PSYNC_AES256_BLOCK_SIZE * 3, - dst + PSYNC_AES256_BLOCK_SIZE * 3); - for (i = 0; i < PSYNC_AES256_BLOCK_SIZE * 4 / sizeof(unsigned long); i++) - ((unsigned long *)dst)[i] ^= ((unsigned long *)bxor)[i]; -} - static ssl_connection_t *psync_ssl_alloc_conn(const char *hostname) { ssl_connection_t *conn; size_t len; @@ -749,27 +714,28 @@ psync_rsa_publickey_t psync_ssl_rsa_load_public(const unsigned char *keydata, si } psync_rsa_privatekey_t psync_ssl_rsa_load_private(const unsigned char *keydata, size_t keylen) { - mbedtls_pk_context ctx; - mbedtls_rsa_context *rsa; + mbedtls_pk_context pkctx; + mbedtls_rsa_context *rsactx; int ret; - mbedtls_pk_init(&ctx); - if (unlikely(ret = mbedtls_pk_parse_key(&ctx, keydata, keylen, NULL, 0, mbedtls_ctr_drbg_random, &psync_mbed_rng.rnd))) { + mbedtls_pk_init(&pkctx); + ret = mbedtls_pk_parse_key(&pkctx, keydata, keylen, NULL, 0, mbedtls_ctr_drbg_random, &psync_mbed_rng.rnd); + if (unlikely(ret)) { debug(D_WARNING, "mbedtls_pk_parse_key failed with code %d", ret); return PSYNC_INVALID_RSA; } - rsa = psync_new(mbedtls_rsa_context); - mbedtls_rsa_init(rsa); - mbedtls_rsa_set_padding(rsa, MBEDTLS_RSA_PKCS_V21, MBEDTLS_MD_SHA1); - ret = mbedtls_rsa_copy(rsa, mbedtls_pk_rsa(ctx)); - mbedtls_pk_free(&ctx); + rsactx = psync_new(mbedtls_rsa_context); + mbedtls_rsa_init(rsactx); + mbedtls_rsa_set_padding(rsactx, MBEDTLS_RSA_PKCS_V21, MBEDTLS_MD_SHA1); + ret = mbedtls_rsa_copy(rsactx, mbedtls_pk_rsa(pkctx)); + mbedtls_pk_free(&pkctx); if (unlikely(ret)) { debug(D_WARNING, "rsa_copy failed with code %d", ret); - mbedtls_rsa_free(rsa); - psync_free(rsa); + mbedtls_rsa_free(rsactx); + psync_free(rsactx); return PSYNC_INVALID_RSA; } else { - mbedtls_rsa_set_padding(rsa, MBEDTLS_RSA_PKCS_V21, MBEDTLS_MD_SHA1); - return rsa; + mbedtls_rsa_set_padding(rsactx, MBEDTLS_RSA_PKCS_V21, MBEDTLS_MD_SHA1); + return rsactx; } } @@ -935,3 +901,38 @@ void psync_aes256_decode_4blocks_consec_xor_sw(psync_aes256_decoder enc, const u for (i = 0; i < PSYNC_AES256_BLOCK_SIZE * 4 / sizeof(unsigned long); i++) ((unsigned long *)dst)[i] ^= ((unsigned long *)bxor)[i]; } + +void psync_aes256_encode_block(psync_aes256_encoder enc, const unsigned char *src, unsigned char *dst) { + mbedtls_aes_crypt_ecb(enc, MBEDTLS_AES_ENCRYPT, src, dst); +} + +void psync_aes256_decode_block(psync_aes256_decoder enc, const unsigned char *src, unsigned char *dst) { + mbedtls_aes_crypt_ecb(enc, MBEDTLS_AES_DECRYPT, src, dst); +} + +void psync_aes256_encode_2blocks_consec(psync_aes256_decoder enc, const unsigned char *src, unsigned char *dst) { + mbedtls_aes_crypt_ecb(enc, MBEDTLS_AES_ENCRYPT, src, dst); + mbedtls_aes_crypt_ecb(enc, MBEDTLS_AES_ENCRYPT, src + PSYNC_AES256_BLOCK_SIZE, + dst + PSYNC_AES256_BLOCK_SIZE); +} + +void psync_aes256_decode_2blocks_consec(psync_aes256_decoder enc, const unsigned char *src, unsigned char *dst) { + mbedtls_aes_crypt_ecb(enc, MBEDTLS_AES_DECRYPT, src, dst); + mbedtls_aes_crypt_ecb(enc, MBEDTLS_AES_DECRYPT, src + PSYNC_AES256_BLOCK_SIZE, + dst + PSYNC_AES256_BLOCK_SIZE); +} + +void psync_aes256_decode_4blocks_consec_xor(psync_aes256_decoder enc, const unsigned char *src, unsigned char *dst, unsigned char *bxor) { + unsigned long i; + mbedtls_aes_crypt_ecb(enc, MBEDTLS_AES_DECRYPT, src, dst); + mbedtls_aes_crypt_ecb(enc, MBEDTLS_AES_DECRYPT, src + PSYNC_AES256_BLOCK_SIZE, + dst + PSYNC_AES256_BLOCK_SIZE); + mbedtls_aes_crypt_ecb(enc, MBEDTLS_AES_DECRYPT, + src + PSYNC_AES256_BLOCK_SIZE * 2, + dst + PSYNC_AES256_BLOCK_SIZE * 2); + mbedtls_aes_crypt_ecb(enc, MBEDTLS_AES_DECRYPT, + src + PSYNC_AES256_BLOCK_SIZE * 3, + dst + PSYNC_AES256_BLOCK_SIZE * 3); + for (i = 0; i < PSYNC_AES256_BLOCK_SIZE * 4 / sizeof(unsigned long); i++) + ((unsigned long *)dst)[i] ^= ((unsigned long *)bxor)[i]; +} \ No newline at end of file diff --git a/pclsync/pssl.h b/pclsync/pssl.h index b9f263f..e1a6137 100644 --- a/pclsync/pssl.h +++ b/pclsync/pssl.h @@ -160,5 +160,10 @@ psync_aes256_encoder psync_ssl_aes256_create_decoder(psync_symmetric_key_t key); void psync_ssl_aes256_free_decoder(psync_aes256_encoder aes); psync_rsa_signature_t psync_ssl_rsa_sign_sha256_hash(psync_rsa_privatekey_t rsa, const unsigned char *data); psync_symmetric_key_t psync_ssl_rsa_decrypt_symm_key_lock(psync_rsa_privatekey_t *rsa, const psync_encrypted_symmetric_key_t *enckey); +void psync_aes256_encode_block(psync_aes256_encoder enc, const unsigned char *src, unsigned char *dst); +void psync_aes256_decode_block(psync_aes256_decoder enc, const unsigned char *src, unsigned char *dst); +void psync_aes256_encode_2blocks_consec(psync_aes256_decoder enc, const unsigned char *src, unsigned char *dst); +void psync_aes256_decode_2blocks_consec(psync_aes256_decoder enc, const unsigned char *src, unsigned char *dst); +void psync_aes256_decode_4blocks_consec_xor(psync_aes256_decoder enc, const unsigned char *src, unsigned char *dst, unsigned char *bxor); #endif