-// -*- 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
*
* 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
}
#endif // HAVE_SEASTAR
class deleter;
-struct sha1_digest_t;
+template<uint8_t S>
+struct sha_digest_t;
+using sha1_digest_t = sha_digest_t<20>;
namespace ceph {
class xio_msg_buffer;
/*
- * named constructors
+ * named constructors
*/
raw* copy(const char *c, unsigned len);
raw* create(unsigned len);
/// 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; }
contiguous_filler append_hole(unsigned len);
void append_zero(unsigned len);
void prepend_zero(unsigned len);
-
+
/*
* get a char
*/
}
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
-// -*- 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
*
* 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
// cppcheck-suppress noExplicitConstructor
client_t(int64_t _v = -2) : v(_v) {}
-
+
void encode(bufferlist& bl) const {
using ceph::encode;
encode(v, bl);
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 <uint8_t S>
+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<int>(v[i]));
+ for (size_t i = 0; i < S; i++) {
+ ::sprintf(&str[i * 2], "%02x", static_cast<int>(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 <uint8_t S>
+inline ostream &operator<<(ostream &out, const sha_digest_t<S> &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
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;
}
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,
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;
}
* 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;
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<const char *>(digest.v),
+ reinterpret_cast<const char *>(digest.v + digest.SIZE));
if (ret < 0) {
ldout(cct, 10) << "ceph_armor failed" << dendl;
throw ret;
<< 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();
}
public rgw::io::DecoratedRestfulClient<rgw::io::RestfulClient*>,
public std::enable_shared_from_this<AWSv4ComplMulti> {
using io_base_t = rgw::io::DecoratedRestfulClient<rgw::io::RestfulClient*>;
- using signing_key_t = std::array<unsigned char,
- CEPH_CRYPTO_HMACSHA256_DIGESTSIZE>;
+ using signing_key_t = sha256_digest_t;
CephContext* const cct;
*/
sha256_digest_t calc_hash_sha256(const boost::string_view& msg)
{
- std::array<unsigned char, CEPH_CRYPTO_HMACSHA256_DIGESTSIZE> hash;
+ sha256_digest_t hash;
SHA256 hasher;
- hasher.Update(reinterpret_cast<const unsigned char*>(msg.data()), msg.size());
- hasher.Final(hash.data());
+ hasher.Update(reinterpret_cast<const unsigned char*>(hash.v), msg.size());
+ hasher.Final(hash.v);
return hash;
}
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<char, CEPH_CRYPTO_HMACSHA1_DIGESTSIZE>;
-
-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<char*>(dest.v));
return dest;
}
const char *msg, int msg_len,
char *dest);
-using sha256_digest_t = \
- std::array<unsigned char, CEPH_CRYPTO_HMACSHA256_DIGESTSIZE>;
-
static inline sha256_digest_t
calc_hmac_sha256(const char *key, const int key_len,
const char *msg, const int msg_len) {
- std::array<unsigned char, CEPH_CRYPTO_HMACSHA256_DIGESTSIZE> dest;
+ sha256_digest_t dest;
calc_hmac_sha256(key, key_len, msg, msg_len,
- reinterpret_cast<char*>(dest.data()));
+ reinterpret_cast<char*>(dest.v));
return dest;
}
static inline sha256_digest_t
calc_hmac_sha256(const boost::string_view& key, const boost::string_view& msg) {
- std::array<unsigned char, CEPH_CRYPTO_HMACSHA256_DIGESTSIZE> dest;
+ sha256_digest_t dest;
calc_hmac_sha256(key.data(), key.size(),
msg.data(), msg.size(),
- reinterpret_cast<char*>(dest.data()));
+ reinterpret_cast<char*>(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<const char*>(key.v), sha256_digest_t::SIZE,
+ msg.data(), msg.size(),
+ reinterpret_cast<char*>(dest.v));
return dest;
}
static inline sha256_digest_t
calc_hmac_sha256(const std::vector<unsigned char>& key,
const boost::string_view& msg) {
- std::array<unsigned char, CEPH_CRYPTO_HMACSHA256_DIGESTSIZE> dest;
+ sha256_digest_t dest;
calc_hmac_sha256(reinterpret_cast<const char*>(key.data()), key.size(),
msg.data(), msg.size(),
- reinterpret_cast<char*>(dest.data()));
+ reinterpret_cast<char*>(dest.v));
return dest;
}
static inline sha256_digest_t
calc_hmac_sha256(const std::array<unsigned char, KeyLenN>& key,
const boost::string_view& msg) {
- std::array<unsigned char, CEPH_CRYPTO_HMACSHA256_DIGESTSIZE> dest;
+ sha256_digest_t dest;
calc_hmac_sha256(reinterpret_cast<const char*>(key.data()), key.size(),
msg.data(), msg.size(),
- reinterpret_cast<char*>(dest.data()));
+ reinterpret_cast<char*>(dest.v));
return dest;
}