From d515a8e99a1e0b0af0e31078b6612c5f12369c9a Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Tue, 13 Aug 2019 17:20:07 +0200 Subject: [PATCH] lib/gcrypt/MD5: add wrapper in lib/crypto/ Prepare for using other crypto libraries, e.g. FFmpeg's libavutil. --- meson.build | 2 + src/input/plugins/QobuzClient.cxx | 2 +- src/input/plugins/QobuzInputPlugin.cxx | 4 +- src/input/plugins/meson.build | 4 +- src/lib/crypto/MD5.cxx | 52 ++++++++++++++++++++++++++ src/lib/crypto/MD5.hxx | 51 +++++++++++++++++++++++++ src/lib/crypto/meson.build | 18 +++++++++ src/lib/gcrypt/MD5.cxx | 12 ++---- src/lib/gcrypt/MD5.hxx | 8 ++-- 9 files changed, 136 insertions(+), 17 deletions(-) create mode 100644 src/lib/crypto/MD5.cxx create mode 100644 src/lib/crypto/MD5.hxx create mode 100644 src/lib/crypto/meson.build diff --git a/meson.build b/meson.build index c2a9a1136..3bc69e1be 100644 --- a/meson.build +++ b/meson.build @@ -335,6 +335,8 @@ subdir('src/lib/systemd') subdir('src/lib/upnp') subdir('src/lib/yajl') +subdir('src/lib/crypto') + subdir('src/fs') subdir('src/config') subdir('src/net') diff --git a/src/input/plugins/QobuzClient.cxx b/src/input/plugins/QobuzClient.cxx index be5b40f19..153c80a63 100644 --- a/src/input/plugins/QobuzClient.cxx +++ b/src/input/plugins/QobuzClient.cxx @@ -18,7 +18,7 @@ */ #include "QobuzClient.hxx" -#include "lib/gcrypt/MD5.hxx" +#include "lib/crypto/MD5.hxx" #include "util/ConstBuffer.hxx" #include diff --git a/src/input/plugins/QobuzInputPlugin.cxx b/src/input/plugins/QobuzInputPlugin.cxx index cf53ec15c..099e95ce8 100644 --- a/src/input/plugins/QobuzInputPlugin.cxx +++ b/src/input/plugins/QobuzInputPlugin.cxx @@ -27,7 +27,7 @@ #include "input/FailingInputStream.hxx" #include "input/InputPlugin.hxx" #include "config/Block.hxx" -#include "lib/gcrypt/Init.hxx" +#include "lib/crypto/MD5.hxx" #include "thread/Mutex.hxx" #include "util/StringCompare.hxx" @@ -123,7 +123,7 @@ QobuzInputStream::OnQobuzTrackError(std::exception_ptr e) noexcept static void InitQobuzInput(EventLoop &event_loop, const ConfigBlock &block) { - Gcrypt::Init(); + GlobalInitMD5(); const char *base_url = block.GetBlockValue("base_url", "http://www.qobuz.com/api.json/0.2/"); diff --git a/src/input/plugins/meson.build b/src/input/plugins/meson.build index cf866a0ee..77db192e3 100644 --- a/src/input/plugins/meson.build +++ b/src/input/plugins/meson.build @@ -42,7 +42,7 @@ qobuz_feature = get_option('qobuz') if qobuz_feature.disabled() enable_qobuz = false else - enable_qobuz = curl_dep.found() and yajl_dep.found() and gcrypt_dep.found() + enable_qobuz = curl_dep.found() and yajl_dep.found() and crypto_md5_dep.found() if not enable_qobuz and qobuz_feature.enabled() error('Qobuz requires CURL, libyajl and libgcrypt') endif @@ -93,7 +93,7 @@ input_plugins = static_library( nfs_dep, smbclient_dep, yajl_dep, - gcrypt_dep, + crypto_md5_dep, ], ) diff --git a/src/lib/crypto/MD5.cxx b/src/lib/crypto/MD5.cxx new file mode 100644 index 000000000..3e2cba445 --- /dev/null +++ b/src/lib/crypto/MD5.cxx @@ -0,0 +1,52 @@ +/* + * Copyright 2018-2019 Max Kellermann + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * - Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * - Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the + * distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, + * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED + * OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "MD5.hxx" +#include "lib/gcrypt/MD5.hxx" +#include "lib/gcrypt/Init.hxx" +#include "util/HexFormat.hxx" + +void +GlobalInitMD5() noexcept +{ + Gcrypt::Init(); +} + +std::array +MD5(ConstBuffer input) noexcept +{ + return Gcrypt::MD5(input); +} + +StringBuffer<33> +MD5Hex(ConstBuffer input) noexcept +{ + const auto raw = MD5(input); + return HexFormatBuffer(&raw.front()); +} diff --git a/src/lib/crypto/MD5.hxx b/src/lib/crypto/MD5.hxx new file mode 100644 index 000000000..08cd31b81 --- /dev/null +++ b/src/lib/crypto/MD5.hxx @@ -0,0 +1,51 @@ +/* + * Copyright 2018-2019 Max Kellermann + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * - Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * - Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the + * distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, + * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED + * OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef MD5_HXX +#define MD5_HXX + +#include "util/StringBuffer.hxx" +#include "util/Compiler.h" + +#include + +template struct ConstBuffer; + +void +GlobalInitMD5() noexcept; + +gcc_pure +std::array +MD5(ConstBuffer input) noexcept; + +gcc_pure +StringBuffer<33> +MD5Hex(ConstBuffer input) noexcept; + +#endif diff --git a/src/lib/crypto/meson.build b/src/lib/crypto/meson.build new file mode 100644 index 000000000..5ea661baf --- /dev/null +++ b/src/lib/crypto/meson.build @@ -0,0 +1,18 @@ +if gcrypt_dep.found() + crypto_md5 = static_library( + 'crypto_md5', + 'MD5.cxx', + include_directories: inc, + dependencies: [ + gcrypt_dep, + ], + ) + + crypto_md5_dep = declare_dependency( + link_with: crypto_md5, + ) +else + crypto_md5_dep = dependency('', required: false) +endif + +conf.set('HAVE_MD5', crypto_md5_dep.found()) diff --git a/src/lib/gcrypt/MD5.cxx b/src/lib/gcrypt/MD5.cxx index 09508a060..db0cf38ad 100644 --- a/src/lib/gcrypt/MD5.cxx +++ b/src/lib/gcrypt/MD5.cxx @@ -1,5 +1,5 @@ /* - * Copyright (C) 2018 Max Kellermann + * Copyright 2018-2019 Max Kellermann * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions @@ -29,7 +29,8 @@ #include "MD5.hxx" #include "Hash.hxx" -#include "util/HexFormat.hxx" + +namespace Gcrypt { std::array MD5(ConstBuffer input) noexcept @@ -37,9 +38,4 @@ MD5(ConstBuffer input) noexcept return Gcrypt::Hash(input); } -StringBuffer<33> -MD5Hex(ConstBuffer input) noexcept -{ - const auto raw = MD5(input); - return HexFormatBuffer(&raw.front()); -} +} // namespace Gcrypt diff --git a/src/lib/gcrypt/MD5.hxx b/src/lib/gcrypt/MD5.hxx index 03bfb0347..21a4f8d43 100644 --- a/src/lib/gcrypt/MD5.hxx +++ b/src/lib/gcrypt/MD5.hxx @@ -1,5 +1,5 @@ /* - * Copyright (C) 2018 Max Kellermann + * Copyright 2018-2019 Max Kellermann * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions @@ -37,12 +37,12 @@ template struct ConstBuffer; +namespace Gcrypt { + gcc_pure std::array MD5(ConstBuffer input) noexcept; -gcc_pure -StringBuffer<33> -MD5Hex(ConstBuffer input) noexcept; +} // namespace Gcrypt #endif