diff --git a/meson.build b/meson.build index 0f9c4c1..9724447 100644 --- a/meson.build +++ b/meson.build @@ -1,6 +1,7 @@ project('saltytools', 'c', version: '0.1.0') libpng_dep = dependency('libpng', fallback: ['libpng', 'libpng_dep']) +openssl_dep = dependency('openssl', fallback: ['openssl', 'openssl_dep']) inc = include_directories('.') diff --git a/subprojects/openssl.wrap b/subprojects/openssl.wrap new file mode 100644 index 0000000..b69462f --- /dev/null +++ b/subprojects/openssl.wrap @@ -0,0 +1,15 @@ +[wrap-file] +directory = openssl-3.0.8 +source_url = https://www.openssl.org/source/openssl-3.0.8.tar.gz +source_filename = openssl-3.0.8.tar.gz +source_hash = 6c13d2bf38fdf31eac3ce2a347073673f5d63263398f1f69d0df4a41253e4b3e +patch_filename = openssl_3.0.8-2_patch.zip +patch_url = https://wrapdb.mesonbuild.com/v2/openssl_3.0.8-2/get_patch +patch_hash = e84b5fe469e681e3318184157a0c7c43d4cbacd078bb88f506e31569f8f75072 +source_fallback_url = https://github.com/mesonbuild/wrapdb/releases/download/openssl_3.0.8-2/openssl-3.0.8.tar.gz +wrapdb_version = 3.0.8-2 + +[provide] +libcrypto = libcrypto_dep +libssl = libssl_dep +openssl = openssl_dep diff --git a/util/crypto.c b/util/crypto.c new file mode 100644 index 0000000..d79c30e --- /dev/null +++ b/util/crypto.c @@ -0,0 +1,63 @@ +#include +#include +#include + +#include + +#include "util/crypto.h" +#include "util/log.h" + +int md5_compute(struct md5_hash *dest, const void *bytes, size_t nbytes) { + EVP_MD_CTX *ctx; + const EVP_MD *md5; + unsigned int len; + int libr; + int r; + + assert(dest != NULL); + assert(bytes != NULL); + + ctx = EVP_MD_CTX_new(); + + if (ctx == NULL) { + r = -ENOMEM; + + goto end; + } + + md5 = EVP_md5(); + libr = EVP_DigestInit_ex(ctx, md5, NULL); + + if (libr != 1) { + r = -ENOSYS; + log_error(r); + + goto end; + } + + libr = EVP_DigestUpdate(ctx, bytes, nbytes); + + if (libr != 1) { + r = -ENOSYS; + log_error(r); + + goto end; + } + + len = sizeof(dest->b); + libr = EVP_DigestFinal_ex(ctx, dest->b, &len); + + if (libr != 1 || len != sizeof(dest->b)) { + r = -ENOSYS; + log_error(r); + + goto end; + } + + r = 0; + +end: + EVP_MD_CTX_free(ctx); + + return r; +} diff --git a/util/crypto.h b/util/crypto.h new file mode 100644 index 0000000..c6316e7 --- /dev/null +++ b/util/crypto.h @@ -0,0 +1,10 @@ +#pragma once + +#include +#include + +struct md5_hash { + uint8_t b[16]; +}; + +int md5_compute(struct md5_hash *dest, const void *bytes, size_t nbytes); diff --git a/util/meson.build b/util/meson.build index b119a33..d5cfcd7 100644 --- a/util/meson.build +++ b/util/meson.build @@ -1,8 +1,11 @@ util_lib = static_library( 'util', + dependencies: [openssl_dep], include_directories: [inc], c_pch: '../precompiled.h', sources: [ + 'crypto.c', + 'crypto.h', 'fs.c', 'fs.h', 'iobuf.c',