Openssl meson (#10909)

* meson: allow to build with openssl
* use MD4 from openssl
* meson: use MD5 from openssl too
* meson: use SHA* functions from ssl library
* libr/hash: fix makefile when using openssl
This commit is contained in:
Riccardo Schirone 2018-08-10 10:17:48 +02:00 committed by GitHub
parent 90cacfdcde
commit 54176f1bbc
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
12 changed files with 124 additions and 68 deletions

View file

@ -11,8 +11,16 @@ endif
endif
DEPS=r_util
OBJS=state.o md5.o sha1.o hash.o md4.o hamdist.o crca.o
OBJS+=entropy.o sha2.o calc.o adler32.o luhn.o
OBJS=state.o hash.o hamdist.o crca.o
OBJS+=entropy.o calc.o adler32.o luhn.o
ifeq ($(HAVE_LIB_SSL),1)
CFLAGS+=${SSL_CFLAGS}
LDFLAGS+=${SSL_LDFLAGS}
LINK+=${SSL_LDFLAGS}
else
OBJS+=md4.o md5.o sha1.o sha2.o
endif
ifeq ($(USE_LIB_XXHASH),1)
LDFLAGS+=${LIB_XXHASH}

View file

@ -138,7 +138,7 @@ static void copy4(ut8 *out, ut32 x) {
out[3] = (x >> 24) & 0xFF;
}
static void mdfour(ut8 *out, const ut8 *in, int n) {
void MD4(const ut8 *in, int n, ut8 *out) {
ut8 buf[128];
ut32 M[16];
ut32 b = n * 8;
@ -185,11 +185,3 @@ static void mdfour(ut8 *out, const ut8 *in, int n) {
A = B = C = D = 0;
}
R_API ut8 *r_hash_do_md4(RHash *ctx, const ut8 *input, int len) {
if (len >= 0) {
mdfour (ctx->digest, input, len);
return ctx->digest;
}
return NULL;
}

6
libr/hash/md4.h Normal file
View file

@ -0,0 +1,6 @@
#ifndef _R_MD4_H
#define _R_MD4_H
void MD4(const ut8 *in, int n, ut8 *out);
#endif

View file

@ -24,10 +24,6 @@
#include <r_hash.h>
static void MD5Init (MD5_CTX *);
static void MD5Update (MD5_CTX *, const ut8*, unsigned int);
static void MD5Final (ut8 [16], MD5_CTX *);
/* F, G, H and I are basic MD5 functions. */
#define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
#define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
@ -175,7 +171,7 @@ static const ut8 PADDING[64] = {
};
/* MD5 initialization. Begins an MD5 operation, writing a new context */
static void MD5Init(R_MD5_CTX *context) {
void MD5_Init(R_MD5_CTX *context) {
if (context) {
context->count[0] = context->count[1] = 0;
context->state[0] = 0x67452301;
@ -187,7 +183,7 @@ static void MD5Init(R_MD5_CTX *context) {
/* MD5 block update operation. Continues an MD5 message-digest operation,
* processing another message block, and updating the context */
static void MD5Update(R_MD5_CTX *context, const ut8 *input, ut32 inputLen) {
void MD5_Update(R_MD5_CTX *context, const ut8 *input, ut32 inputLen) {
ut32 i;
/* Compute number of bytes mod 64 */
@ -216,7 +212,7 @@ static void MD5Update(R_MD5_CTX *context, const ut8 *input, ut32 inputLen) {
memmove ((void*)&context->buffer[index], (void*)&input[i], inputLen - i);
}
static void MD5Final (ut8 digest[16], R_MD5_CTX *context) {
void MD5_Final(ut8 digest[16], R_MD5_CTX *context) {
ut8 bits[8];
/* Save number of bits */
@ -225,10 +221,10 @@ static void MD5Final (ut8 digest[16], R_MD5_CTX *context) {
/* Pad out to 56 mod 64. */
ut32 index = (ut32)((context->count[0] >> 3) & 0x3f);
ut32 padLen = (index < 56) ? (56 - index) : (120 - index);
MD5Update (context, PADDING, padLen);
MD5_Update (context, PADDING, padLen);
/* Append length (before padding) */
MD5Update (context, bits, 8);
MD5_Update (context, bits, 8);
/* Store state in digest */
Encode (digest, context->state, 16);
@ -236,26 +232,3 @@ static void MD5Final (ut8 digest[16], R_MD5_CTX *context) {
/* Zeroize sensitive information. */
r_mem_memzero ((void*)context, sizeof (*context));
}
R_API ut8 *r_hash_do_md5(RHash *ctx, const ut8 *input, int len) {
if (len < 0) {
if (len == -1) {
MD5Init (&ctx->md5);
} else if (len == -2) {
MD5Final (ctx->digest, &ctx->md5);
}
return NULL;
}
if (ctx->rst) {
MD5Init (&ctx->md5);
}
if (len > 0) {
MD5Update (&ctx->md5, input, len);
} else {
MD5Update (&ctx->md5, (const ut8 *) "", 0);
}
if (ctx->rst) {
MD5Final (ctx->digest, &ctx->md5);
}
return ctx->digest;
}

8
libr/hash/md5.h Normal file
View file

@ -0,0 +1,8 @@
#ifndef _R_MD5_H
#define _R_MD5_H
void MD5_Init(MD5_CTX *);
void MD5_Update(MD5_CTX *, const ut8*, unsigned int);
void MD5_Final(ut8 [16], MD5_CTX *);
#endif

View file

@ -6,10 +6,6 @@ files = [
'hamdist.c',
'hash.c',
'luhn.c',
'md4.c',
'md5.c',
'sha1.c',
'sha2.c',
'state.c'
]
@ -21,6 +17,12 @@ else
files += ['xxhash.c']
endif
if use_sys_openssl
dependencies += [sys_openssl]
else
files += ['md4.c', 'md5.c', 'sha1.c', 'sha2.c']
endif
r_hash = library('r_hash', files,
include_directories: [platform_inc],
dependencies: dependencies,

View file

@ -1,10 +1,17 @@
/* radare - LGPL - Copyright 2009-2017 pancake */
#include <r_hash.h>
#if HAVE_LIB_SSL
#include <openssl/md4.h>
#include <openssl/md5.h>
#include <openssl/sha.h>
#else
#include "md4.h"
#include "md5.h"
#include "sha1.h"
#include "sha2.h"
R_API void mdfour(ut8 *out, const ut8 *in, int n);
#endif
#define CHKFLAG(x) if (!flags || flags & x)
@ -94,3 +101,34 @@ R_API ut8 *r_hash_do_sha512(RHash *ctx, const ut8 *input, int len) {
}
return ctx->digest;
}
R_API ut8 *r_hash_do_md5(RHash *ctx, const ut8 *input, int len) {
if (len < 0) {
if (len == -1) {
MD5_Init (&ctx->md5);
} else if (len == -2) {
MD5_Final (ctx->digest, &ctx->md5);
}
return NULL;
}
if (ctx->rst) {
MD5_Init (&ctx->md5);
}
if (len > 0) {
MD5_Update (&ctx->md5, input, len);
} else {
MD5_Update (&ctx->md5, (const ut8 *) "", 0);
}
if (ctx->rst) {
MD5_Final (ctx->digest, &ctx->md5);
}
return ctx->digest;
}
R_API ut8 *r_hash_do_md4(RHash *ctx, const ut8 *input, int len) {
if (len >= 0) {
MD4 (input, len, ctx->digest);
return ctx->digest;
}
return NULL;
}

View file

@ -10,6 +10,15 @@ extern "C" {
R_LIB_VERSION_HEADER (r_hash);
#if HAVE_LIB_SSL
#include <openssl/sha.h>
#include <openssl/md5.h>
typedef MD5_CTX R_MD5_CTX;
typedef SHA_CTX R_SHA_CTX;
typedef SHA256_CTX R_SHA256_CTX;
typedef SHA512_CTX R_SHA384_CTX;
typedef SHA512_CTX R_SHA512_CTX;
#else
#define MD5_CTX R_MD5_CTX
/* hashing */
@ -26,6 +35,23 @@ typedef struct {
ut32 sizeHi, sizeLo;
} R_SHA_CTX;
#define SHA256_BLOCK_LENGTH 64
typedef struct _SHA256_CTX {
ut32 state[8];
ut64 bitcount;
ut8 buffer[SHA256_BLOCK_LENGTH];
} R_SHA256_CTX;
#define SHA384_BLOCK_LENGTH 128
#define SHA512_BLOCK_LENGTH 128
typedef struct _SHA512_CTX {
ut64 state[8];
ut64 bitcount[2];
ut8 buffer[SHA512_BLOCK_LENGTH];
} R_SHA512_CTX;
typedef R_SHA512_CTX R_SHA384_CTX;
#endif
/*
* Since we have not enought space in bitmask, you may do fine
@ -135,22 +161,6 @@ enum CRC_PRESETS {
CRC_PRESET_SIZE
};
#define SHA256_BLOCK_LENGTH 64
typedef struct _SHA256_CTX {
ut32 state[8];
ut64 bitcount;
ut8 buffer[SHA256_BLOCK_LENGTH];
} R_SHA256_CTX;
#define SHA384_BLOCK_LENGTH 128
#define SHA512_BLOCK_LENGTH 128
typedef struct _SHA512_CTX {
ut64 state[8];
ut64 bitcount[2];
ut8 buffer[SHA512_BLOCK_LENGTH];
} R_SHA512_CTX;
typedef R_SHA512_CTX R_SHA384_CTX;
/* Fix names conflict with ruby bindings */
#define RHash struct r_hash_t

View file

@ -9,9 +9,15 @@ files = [
'serial.c',
]
dependencies = [utl, r_util_dep, platform_deps]
if use_sys_openssl
dependencies += [sys_openssl]
endif
r_socket = library('r_socket', files,
include_directories: [platform_inc],
dependencies: [utl, r_util_dep, platform_deps],
dependencies: dependencies,
c_args: ['-DCORELIB=1'],
install: true,
implicit_include_directories: false,

View file

@ -4,7 +4,7 @@ files = [
'base85.c',
'base91.c',
'bdiff.c',
'big.c',
#'big.c',
'binheap.c',
'bitmap.c',
'btree.c',

View file

@ -197,6 +197,7 @@ config_h = configure_file(
configuration: conf_data
)
# handle magic library
sys_magic = cc.find_library('magic', required: false)
use_syslib_magic = false
@ -204,6 +205,7 @@ if sys_magic.found() and get_option('use_sys_magic')
use_syslib_magic = true
endif
# handle xxhash library
sys_xxhash = dependency('xxhash', required: false)
use_sys_xxhash = false
if not sys_xxhash.found()
@ -217,6 +219,16 @@ else
message('Using bundled xxhash library')
endif
# handle openssl library
sys_openssl = dependency('openssl', required: false)
use_sys_openssl = false
if sys_openssl.found() and get_option('use_sys_openssl')
message('Using system openssl library')
use_sys_openssl = true
else
message('Using bundled openssl code')
endif
userconf = configuration_data()
userconf.set('HAVE_LIB_MAGIC', sys_magic.found().to_int())
userconf.set('USE_LIB_MAGIC', use_syslib_magic.to_int())
@ -237,7 +249,7 @@ userconf.set('HUD', r2_hud)
userconf.set('PLUGINS', r2_plugins)
userconf.set('EXTRAS', r2_extras)
userconf.set('BINDINGS', r2_bindings)
userconf.set('HAVE_OPENSSL', 0)
userconf.set('HAVE_OPENSSL', use_sys_openssl.to_int())
userconf.set('HAVE_FORK', 1)
userconf.set('WITH_GPL', 1)
if host_machine.system() == 'windows'

View file

@ -18,3 +18,4 @@ option('use_sys_zip', type: 'boolean', value: false)
option('use_sys_zlib', type: 'boolean', value: false)
option('use_sys_lz4', type: 'boolean', value: false)
option('use_sys_xxhash', type: 'boolean', value: false)
option('use_sys_openssl', type: 'boolean', value: false)