From 3a382e473f86e98220250c5e3a02f115ed5f249d Mon Sep 17 00:00:00 2001 From: Ricardo Dias Date: Wed, 9 Jan 2019 13:59:41 +0000 Subject: [PATCH] types.h,rgw: merge sha*_digest_t definitions Signed-off-by: Ricardo Dias --- src/include/buffer.h | 18 ++++++++++-------- src/include/types.h | 34 ++++++++++++++++++---------------- src/rgw/rgw_auth_s3.cc | 28 +++++++++++++--------------- src/rgw/rgw_auth_s3.h | 3 +-- src/rgw/rgw_common.cc | 6 +++--- src/rgw/rgw_common.h | 39 ++++++++++++++++++++++----------------- 6 files changed, 67 insertions(+), 61 deletions(-) diff --git a/src/include/buffer.h b/src/include/buffer.h index b13ef615f14..e2f86db8e5b 100644 --- a/src/include/buffer.h +++ b/src/include/buffer.h @@ -1,4 +1,4 @@ -// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*- +// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*- // vim: ts=8 sw=2 smarttab /* * Ceph - scalable distributed file system @@ -7,9 +7,9 @@ * * This is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public - * License version 2.1, as published by the Free Software + * License version 2.1, as published by the Free Software * Foundation. See file COPYING. - * + * */ #ifndef CEPH_BUFFER_H #define CEPH_BUFFER_H @@ -81,7 +81,9 @@ class packet; } #endif // HAVE_SEASTAR class deleter; -struct sha1_digest_t; +template +struct sha_digest_t; +using sha1_digest_t = sha_digest_t<20>; namespace ceph { @@ -141,7 +143,7 @@ namespace buffer CEPH_BUFFER_API { class xio_msg_buffer; /* - * named constructors + * named constructors */ raw* copy(const char *c, unsigned len); raw* create(unsigned len); @@ -691,7 +693,7 @@ namespace buffer CEPH_BUFFER_API { /// get current iterator offset in buffer::list unsigned get_off() const { return off; } - + /// get number of bytes remaining from iterator position to the end of the buffer::list unsigned get_remaining() const { return bl->length() - off; } @@ -1174,7 +1176,7 @@ namespace buffer CEPH_BUFFER_API { contiguous_filler append_hole(unsigned len); void append_zero(unsigned len); void prepend_zero(unsigned len); - + /* * get a char */ @@ -1215,7 +1217,7 @@ namespace buffer CEPH_BUFFER_API { } uint32_t crc32c(uint32_t crc) const; void invalidate_crc(); - sha1_digest_t sha1(); + sha1_digest_t sha1(); // These functions return a bufferlist with a pointer to a single // static buffer. They /must/ not outlive the memory they diff --git a/src/include/types.h b/src/include/types.h index 76447caa0b2..92a30f56248 100644 --- a/src/include/types.h +++ b/src/include/types.h @@ -1,4 +1,4 @@ -// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*- +// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*- // vim: ts=8 sw=2 smarttab /* * Ceph - scalable distributed file system @@ -7,9 +7,9 @@ * * This is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public - * License version 2.1, as published by the Free Software + * License version 2.1, as published by the Free Software * Foundation. See file COPYING. - * + * */ #ifndef CEPH_TYPES_H #define CEPH_TYPES_H @@ -359,7 +359,7 @@ struct client_t { // cppcheck-suppress noExplicitConstructor client_t(int64_t _v = -2) : v(_v) {} - + void encode(bufferlist& bl) const { using ceph::encode; encode(v, bl); @@ -558,28 +558,30 @@ WRITE_CLASS_ENCODER(errorcode32_t) WRITE_EQ_OPERATORS_1(errorcode32_t, code) WRITE_CMP_OPERATORS_1(errorcode32_t, code) -struct sha1_digest_t { -#define SHA1_DIGEST_SIZE 20 - unsigned char v[SHA1_DIGEST_SIZE] = {0}; +template +struct sha_digest_t { + constexpr static uint32_t SIZE = S; + unsigned char v[S] = {0}; string to_str() const { - char str[SHA1_DIGEST_SIZE*2+1] = {0}; + char str[S * 2 + 1] = {0}; str[0] = '\0'; - for (size_t i = 0; i < 20; i++) { - ::sprintf(&str[i*2], "%02x", static_cast(v[i])); + for (size_t i = 0; i < S; i++) { + ::sprintf(&str[i * 2], "%02x", static_cast(v[i])); } return string(str); } - sha1_digest_t(unsigned char *_v) { - memcpy(v, _v, 20); - }; - sha1_digest_t() {} + sha_digest_t(const unsigned char *_v) { memcpy(v, _v, S); }; + sha_digest_t() {} }; -inline ostream& operator<<(ostream& out, const sha1_digest_t& b) -{ +template +inline ostream &operator<<(ostream &out, const sha_digest_t &b) { string str = b.to_str(); return out << str; } +using sha1_digest_t = sha_digest_t<20>; +using sha256_digest_t = sha_digest_t<32>; + #endif diff --git a/src/rgw/rgw_auth_s3.cc b/src/rgw/rgw_auth_s3.cc index 36a96e7b7e1..58639bfdc81 100644 --- a/src/rgw/rgw_auth_s3.cc +++ b/src/rgw/rgw_auth_s3.cc @@ -643,7 +643,7 @@ get_v4_canon_req_hash(CephContext* cct, using sanitize = rgw::crypt_sanitize::log_content; ldout(cct, 10) << "canonical request = " << sanitize{canonical_req} << dendl; ldout(cct, 10) << "canonical request hash = " - << buf_to_hex(canonical_req_hash).data() << dendl; + << canonical_req_hash << dendl; return canonical_req_hash; } @@ -660,9 +660,8 @@ get_v4_string_to_sign(CephContext* const cct, const boost::string_view& credential_scope, const sha256_digest_t& canonreq_hash) { - const auto hexed_cr_hash = buf_to_hex(canonreq_hash); - const boost::string_view hexed_cr_hash_str(hexed_cr_hash.data(), - hexed_cr_hash.size() - 1); + const auto hexed_cr_hash = canonreq_hash.to_str(); + const boost::string_view hexed_cr_hash_str(hexed_cr_hash); const auto string_to_sign = string_join_reserve("\n", algorithm, @@ -742,10 +741,10 @@ get_v4_signing_key(CephContext* const cct, const auto signing_key = calc_hmac_sha256(service_k, boost::string_view("aws4_request")); - ldout(cct, 10) << "date_k = " << buf_to_hex(date_k).data() << dendl; - ldout(cct, 10) << "region_k = " << buf_to_hex(region_k).data() << dendl; - ldout(cct, 10) << "service_k = " << buf_to_hex(service_k).data() << dendl; - ldout(cct, 10) << "signing_k = " << buf_to_hex(signing_key).data() << dendl; + ldout(cct, 10) << "date_k = " << date_k << dendl; + ldout(cct, 10) << "region_k = " << region_k << dendl; + ldout(cct, 10) << "service_k = " << service_k << dendl; + ldout(cct, 10) << "signing_k = " << signing_key << dendl; return signing_key; } @@ -774,8 +773,8 @@ get_v4_signature(const boost::string_view& credential_scope, * the non-const data() variant like C++17's std::string. */ using srv_signature_t = AWSEngine::VersionAbstractor::server_signature_t; srv_signature_t signature(srv_signature_t::initialized_later(), - digest.size() * 2); - buf_to_hex(digest.data(), digest.size(), signature.begin()); + digest.SIZE * 2); + buf_to_hex(digest.v, digest.SIZE, signature.begin()); ldout(cct, 10) << "generated signature = " << signature << dendl; @@ -797,8 +796,8 @@ get_v2_signature(CephContext* const cct, char buf[64]; const int ret = ceph_armor(std::begin(buf), std::begin(buf) + 64, - std::begin(digest), - std::begin(digest) + digest.size()); + reinterpret_cast(digest.v), + reinterpret_cast(digest.v + digest.SIZE)); if (ret < 0) { ldout(cct, 10) << "ceph_armor failed" << dendl; throw ret; @@ -905,10 +904,9 @@ AWSv4ComplMulti::calc_chunk_signature(const std::string& payload_hash) const << dendl; /* new chunk signature */ - const auto sighex = buf_to_hex(calc_hmac_sha256(signing_key, - string_to_sign)); + const auto sig = calc_hmac_sha256(signing_key, string_to_sign); /* FIXME(rzarzynski): std::string here is really unnecessary. */ - return std::string(sighex.data(), sighex.size() - 1); + return sig.to_str(); } diff --git a/src/rgw/rgw_auth_s3.h b/src/rgw/rgw_auth_s3.h index d848d917136..0b4ab994bb0 100644 --- a/src/rgw/rgw_auth_s3.h +++ b/src/rgw/rgw_auth_s3.h @@ -256,8 +256,7 @@ class AWSv4ComplMulti : public rgw::auth::Completer, public rgw::io::DecoratedRestfulClient, public std::enable_shared_from_this { using io_base_t = rgw::io::DecoratedRestfulClient; - using signing_key_t = std::array; + using signing_key_t = sha256_digest_t; CephContext* const cct; diff --git a/src/rgw/rgw_common.cc b/src/rgw/rgw_common.cc index a23eb418edd..b6eaa95f324 100644 --- a/src/rgw/rgw_common.cc +++ b/src/rgw/rgw_common.cc @@ -719,11 +719,11 @@ using ceph::crypto::SHA256; */ sha256_digest_t calc_hash_sha256(const boost::string_view& msg) { - std::array hash; + sha256_digest_t hash; SHA256 hasher; - hasher.Update(reinterpret_cast(msg.data()), msg.size()); - hasher.Final(hash.data()); + hasher.Update(reinterpret_cast(hash.v), msg.size()); + hasher.Final(hash.v); return hash; } diff --git a/src/rgw/rgw_common.h b/src/rgw/rgw_common.h index 890e746ce32..5d3b072daff 100644 --- a/src/rgw/rgw_common.h +++ b/src/rgw/rgw_common.h @@ -2537,13 +2537,11 @@ extern std::string url_encode(const std::string& src, bool encode_slash = true); extern void calc_hmac_sha1(const char *key, int key_len, const char *msg, int msg_len, char *dest); -using sha1_digest_array_t = \ - std::array; - -static inline sha1_digest_array_t +static inline sha1_digest_t calc_hmac_sha1(const boost::string_view& key, const boost::string_view& msg) { - sha1_digest_array_t dest; - calc_hmac_sha1(key.data(), key.size(), msg.data(), msg.size(), dest.data()); + sha1_digest_t dest; + calc_hmac_sha1(key.data(), key.size(), msg.data(), msg.size(), + reinterpret_cast(dest.v)); return dest; } @@ -2552,34 +2550,41 @@ extern void calc_hmac_sha256(const char *key, int key_len, const char *msg, int msg_len, char *dest); -using sha256_digest_t = \ - std::array; - static inline sha256_digest_t calc_hmac_sha256(const char *key, const int key_len, const char *msg, const int msg_len) { - std::array dest; + sha256_digest_t dest; calc_hmac_sha256(key, key_len, msg, msg_len, - reinterpret_cast(dest.data())); + reinterpret_cast(dest.v)); return dest; } static inline sha256_digest_t calc_hmac_sha256(const boost::string_view& key, const boost::string_view& msg) { - std::array dest; + sha256_digest_t dest; calc_hmac_sha256(key.data(), key.size(), msg.data(), msg.size(), - reinterpret_cast(dest.data())); + reinterpret_cast(dest.v)); + return dest; +} + +static inline sha256_digest_t +calc_hmac_sha256(const sha256_digest_t &key, + const boost::string_view& msg) { + sha256_digest_t dest; + calc_hmac_sha256(reinterpret_cast(key.v), sha256_digest_t::SIZE, + msg.data(), msg.size(), + reinterpret_cast(dest.v)); return dest; } static inline sha256_digest_t calc_hmac_sha256(const std::vector& key, const boost::string_view& msg) { - std::array dest; + sha256_digest_t dest; calc_hmac_sha256(reinterpret_cast(key.data()), key.size(), msg.data(), msg.size(), - reinterpret_cast(dest.data())); + reinterpret_cast(dest.v)); return dest; } @@ -2587,10 +2592,10 @@ template static inline sha256_digest_t calc_hmac_sha256(const std::array& key, const boost::string_view& msg) { - std::array dest; + sha256_digest_t dest; calc_hmac_sha256(reinterpret_cast(key.data()), key.size(), msg.data(), msg.size(), - reinterpret_cast(dest.data())); + reinterpret_cast(dest.v)); return dest; } -- 2.39.5