Skip to content
This repository was archived by the owner on Apr 8, 2025. It is now read-only.

Commit bef3af8

Browse files
Jinming-Huvinjiang
authored andcommitted
Add support for CPK-V
1 parent 22ba811 commit bef3af8

16 files changed

+367
-55
lines changed

Microsoft.WindowsAzure.Storage/includes/was/blob.h

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1287,6 +1287,7 @@ namespace azure { namespace storage {
12871287
m_content_md5 = std::move(other.m_content_md5);
12881288
m_content_type = std::move(other.m_content_type);
12891289
m_etag = std::move(other.m_etag);
1290+
m_encryption_key_sha256 = std::move(other.m_encryption_key_sha256);
12901291
m_last_modified = std::move(other.m_last_modified);
12911292
m_type = std::move(other.m_type);
12921293
m_lease_status = std::move(other.m_lease_status);
@@ -1432,6 +1433,15 @@ namespace azure { namespace storage {
14321433
return m_etag;
14331434
}
14341435

1436+
/// <summary>
1437+
/// Gets the SHA-256 of the customer-provided key used to encrypt this blob.
1438+
/// </summary>
1439+
/// <returns>The base64-encoded SHA-256 value.</returns>
1440+
const utility::string_t& encryption_key_sha256() const
1441+
{
1442+
return m_encryption_key_sha256;
1443+
}
1444+
14351445
/// <summary>
14361446
/// Gets the last-modified time for the blob, expressed as a UTC value.
14371447
/// </summary>
@@ -1583,6 +1593,7 @@ namespace azure { namespace storage {
15831593
utility::string_t m_content_md5;
15841594
utility::string_t m_content_type;
15851595
utility::string_t m_etag;
1596+
utility::string_t m_encryption_key_sha256;
15861597
utility::datetime m_last_modified;
15871598
utility::datetime m_access_tier_change_time;
15881599
blob_type m_type;
@@ -1819,6 +1830,7 @@ namespace azure { namespace storage {
18191830
m_stream_write_size = std::move(other.m_stream_write_size);
18201831
m_stream_read_size = std::move(other.m_stream_read_size);
18211832
m_absorb_conditional_errors_on_retry = std::move(other.m_absorb_conditional_errors_on_retry);
1833+
m_encryption_key = std::move(other.m_encryption_key);
18221834
}
18231835
return *this;
18241836
}
@@ -1859,6 +1871,8 @@ namespace azure { namespace storage {
18591871
m_stream_write_size.merge(other.m_stream_write_size);
18601872
m_stream_read_size.merge(other.m_stream_read_size);
18611873
m_absorb_conditional_errors_on_retry.merge(other.m_absorb_conditional_errors_on_retry);
1874+
if (m_encryption_key.empty() && !other.m_encryption_key.empty())
1875+
m_encryption_key = other.m_encryption_key;
18621876
}
18631877

18641878
/// <summary>
@@ -2055,6 +2069,24 @@ namespace azure { namespace storage {
20552069
m_absorb_conditional_errors_on_retry = value;
20562070
}
20572071

2072+
/// <summary>
2073+
/// Gets the customer provided encryption key.
2074+
/// </summary>
2075+
/// <returns>The customer provided encryption key.</returns>
2076+
const std::vector<uint8_t>& encryption_key() const
2077+
{
2078+
return m_encryption_key;
2079+
}
2080+
2081+
/// <summary>
2082+
/// Sets the customer provided encryption key.
2083+
/// </summary>
2084+
/// <param name="encryption_key">The customer provided encryption key.</returns>
2085+
void set_encryption_key(std::vector<uint8_t> encryption_key)
2086+
{
2087+
m_encryption_key = std::move(encryption_key);
2088+
}
2089+
20582090
private:
20592091

20602092
option_with_default<bool> m_use_transactional_md5;
@@ -2067,6 +2099,7 @@ namespace azure { namespace storage {
20672099
option_with_default<size_t> m_stream_write_size;
20682100
option_with_default<size_t> m_stream_read_size;
20692101
option_with_default<bool> m_absorb_conditional_errors_on_retry;
2102+
std::vector<uint8_t> m_encryption_key;
20702103
};
20712104

20722105
/// <summary>

Microsoft.WindowsAzure.Storage/includes/was/core.h

Lines changed: 38 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -482,17 +482,20 @@ namespace azure { namespace storage {
482482
{
483483
none,
484484
md5,
485+
sha256,
485486
crc64,
486487
hmac_sha256,
487488
};
488489

489490
using checksum_none_t = std::integral_constant<checksum_type, checksum_type::none>;
490491
using checksum_md5_t = std::integral_constant<checksum_type, checksum_type::md5>;
492+
using checksum_sha256_t = std::integral_constant<checksum_type, checksum_type::sha256>;
491493
using checksum_crc64_t = std::integral_constant<checksum_type, checksum_type::crc64>;
492494
using checksum_hmac_sha256_t = std::integral_constant<checksum_type, checksum_type::hmac_sha256>;
493495

494496
constexpr auto checksum_none = checksum_none_t();
495497
constexpr auto checksum_md5 = checksum_md5_t();
498+
constexpr auto checksum_sha256 = checksum_sha256_t();
496499
constexpr auto checksum_crc64 = checksum_crc64_t();
497500
constexpr auto checksum_hmac_sha256 = checksum_hmac_sha256_t();
498501

@@ -516,9 +519,9 @@ namespace azure { namespace storage {
516519
/// <remarks>
517520
/// If the provided string is empty, this class is initialized as if checksum method isn't specified.
518521
/// </remarks>
519-
checksum(utility::string_t md5) : m_type(checksum_type::md5), m_md5(std::move(md5))
522+
checksum(utility::string_t md5) : m_type(checksum_type::md5), m_str_hash(std::move(md5))
520523
{
521-
if (m_md5.empty())
524+
if (m_str_hash.empty())
522525
{
523526
m_type = checksum_type::none;
524527
}
@@ -555,7 +558,16 @@ namespace azure { namespace storage {
555558
/// </summary>
556559
/// <param name="type">Explicitly specified checksum type, must be <see cref="azure::storage::checksum_md5" />.</param>
557560
/// <param name="val">A string containing base64-encoded MD5.</param>
558-
checksum(checksum_md5_t type, utility::string_t val) : m_type(type.value), m_md5(std::move(val))
561+
checksum(checksum_md5_t type, utility::string_t val) : m_type(type.value), m_str_hash(std::move(val))
562+
{
563+
}
564+
565+
/// <summary>
566+
/// Initializes a new instance of the <see cref="azure::storage::checksum" /> class with SHA-256 hash value.
567+
/// </summary>
568+
/// <param name="type">Explicitly specified checksum type, must be <see cref="azure::storage::checksum_sha256" />.</param>
569+
/// <param name="val">A string containing base64-encoded SHA-256.</param>
570+
checksum(checksum_sha256_t type, utility::string_t val) : m_type(type.value), m_str_hash(std::move(val))
559571
{
560572
}
561573

@@ -573,7 +585,7 @@ namespace azure { namespace storage {
573585
/// </summary>
574586
/// <param name="type">Explicitly specified checksum type, must be <see cref="azure::storage::checksum_hmac_sha256" />.</param>
575587
/// <param name="val">A string containing base64-encoded HMAC-SHA256 authentication code.</param>
576-
checksum(checksum_hmac_sha256_t type, utility::string_t val) : m_type(type.value), m_hmac_sha256(std::move(val))
588+
checksum(checksum_hmac_sha256_t type, utility::string_t val) : m_type(type.value), m_str_hash(std::move(val))
577589
{
578590
}
579591

@@ -600,8 +612,7 @@ namespace azure { namespace storage {
600612
if (this != &other)
601613
{
602614
m_type = std::move(other.m_type);
603-
m_md5 = std::move(other.m_md5);
604-
m_hmac_sha256 = std::move(other.hmac_sha256);
615+
m_str_hash = std::move(other.m_str_hash);
605616
m_crc64 = std::move(other.m_crc64);
606617
}
607618
return *this;
@@ -617,6 +628,15 @@ namespace azure { namespace storage {
617628
return m_type == checksum_type::md5;
618629
}
619630

631+
/// <summary>
632+
/// Indicates whether this is an SHA-256 checksum.
633+
/// </summary>
634+
/// <returns><c>true</c> if this is an SHA-256 checksum; otherwise, <c>false</c>.</returns>
635+
bool is_sha256() const
636+
{
637+
return m_type == checksum_type::sha256;
638+
}
639+
620640
/// <summary>
621641
/// Indicates whether this is an HMAC-SHA256 authentication code.
622642
/// </summary>
@@ -650,7 +670,16 @@ namespace azure { namespace storage {
650670
/// <returns>A string containing base64-encoded MD5.</returns>
651671
const utility::string_t& md5() const
652672
{
653-
return m_md5;
673+
return m_str_hash;
674+
}
675+
676+
/// <summary>
677+
/// Gets the SHA-256 checksum.
678+
/// </summary>
679+
/// <returns>A string containing base64-encoded SHA-256.</returns>
680+
const utility::string_t& sha256() const
681+
{
682+
return m_str_hash;
654683
}
655684

656685
/// <summary>
@@ -659,7 +688,7 @@ namespace azure { namespace storage {
659688
/// <returns>A string containing base64-encoded HMAC-256 authentiction code.</returns>
660689
const utility::string_t& hmac_sha256() const
661690
{
662-
return m_hmac_sha256;
691+
return m_str_hash;
663692
}
664693

665694
/// <summary>
@@ -675,8 +704,7 @@ namespace azure { namespace storage {
675704

676705
private:
677706
checksum_type m_type;
678-
utility::string_t m_md5;
679-
utility::string_t m_hmac_sha256;
707+
utility::string_t m_str_hash;
680708
uint64_t m_crc64;
681709
};
682710

Microsoft.WindowsAzure.Storage/includes/wascore/constants.dat

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,9 @@ DAT(ms_header_file_change_time, _XPLATSTR("x-ms-file-change-time"))
210210
DAT(ms_header_file_id, _XPLATSTR("x-ms-file-id"))
211211
DAT(ms_header_file_parent_id, _XPLATSTR("x-ms-file-parent-id"))
212212
DAT(ms_header_previous_snapshot_url, _XPLATSTR("x-ms-previous-snapshot-url"))
213+
DAT(ms_header_encryption_key, _XPLATSTR("x-ms-encryption-key"))
214+
DAT(ms_header_encryption_key_sha256, _XPLATSTR("x-ms-encryption-key-sha256"))
215+
DAT(ms_header_encryption_algorithm, _XPLATSTR("x-ms-encryption-algorithm"))
213216

214217
// header values
215218
DAT(header_value_storage_version, _XPLATSTR("2019-07-07"))
@@ -285,6 +288,7 @@ DAT(header_value_file_attribute_offline, _XPLATSTR("Offline"))
285288
DAT(header_value_file_attribute_notcontentindexed, _XPLATSTR("NotContentIndexed"))
286289
DAT(header_value_file_attribute_noscrubdata, _XPLATSTR("NoScrubData"))
287290
DAT(header_value_file_attribute_delimiter, _XPLATSTR(" | "))
291+
DAT(header_value_encryption_algorithm_aes256, _XPLATSTR("AES256"))
288292

289293
// xml strings
290294
DAT(xml_last_modified, _XPLATSTR("Last-Modified"))

Microsoft.WindowsAzure.Storage/includes/wascore/hashing.h

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,33 @@ namespace azure { namespace storage { namespace core {
151151
#endif
152152
};
153153

154+
class sha256_hash_provider_impl : public cryptography_hash_provider_impl
155+
{
156+
public:
157+
sha256_hash_provider_impl();
158+
~sha256_hash_provider_impl() override;
159+
160+
bool is_enabled() const override
161+
{
162+
return true;
163+
}
164+
165+
void write(const uint8_t* data, size_t count) override;
166+
void close() override;
167+
168+
checksum hash() const override
169+
{
170+
return checksum(checksum_sha256, utility::conversions::to_base64(m_hash));
171+
}
172+
173+
private:
174+
#ifdef _WIN32
175+
static BCRYPT_ALG_HANDLE algorithm_handle();
176+
#else // Linux
177+
SHA256_CTX* m_hash_context = nullptr;
178+
#endif
179+
};
180+
154181
class crc64_hash_provider_impl : public hash_provider_impl
155182
{
156183
public:
@@ -215,6 +242,11 @@ namespace azure { namespace storage { namespace core {
215242
return hash_provider(std::make_shared<md5_hash_provider_impl>());
216243
}
217244

245+
static hash_provider create_sha256_hash_provider()
246+
{
247+
return hash_provider(std::make_shared<sha256_hash_provider_impl>());
248+
}
249+
218250
static hash_provider create_crc64_hash_provider()
219251
{
220252
return hash_provider(std::make_shared<crc64_hash_provider_impl>());

0 commit comments

Comments
 (0)