From 50d541407507edc2f29ad6a46f3f17724e52f7f7 Mon Sep 17 00:00:00 2001 From: Zach Hilman Date: Sun, 26 May 2019 13:01:42 -0400 Subject: key_manager: Convert Ticket union to std::variant --- src/core/crypto/key_manager.h | 50 ++++++++++++++++++++++++++----------------- 1 file changed, 30 insertions(+), 20 deletions(-) (limited to 'src/core/crypto/key_manager.h') diff --git a/src/core/crypto/key_manager.h b/src/core/crypto/key_manager.h index d4e89d35c..7265c4171 100644 --- a/src/core/crypto/key_manager.h +++ b/src/core/crypto/key_manager.h @@ -9,6 +9,7 @@ #include #include +#include #include #include #include "common/common_funcs.h" @@ -73,33 +74,36 @@ struct TicketData { }; static_assert(sizeof(TicketData) == 0x2C0, "TicketData has incorrect size."); -struct Ticket { +struct RSA4096Ticket { SignatureType sig_type; - union { - struct { - std::array sig_data; - INSERT_PADDING_BYTES(0x3C); - TicketData data; - } rsa_4096; + std::array sig_data; + INSERT_PADDING_BYTES(0x3C); + TicketData data; +}; - struct { - std::array sig_data; - INSERT_PADDING_BYTES(0x3C); - TicketData data; - } rsa_2048; +struct RSA2048Ticket { + SignatureType sig_type; + std::array sig_data; + INSERT_PADDING_BYTES(0x3C); + TicketData data; +}; - struct { - std::array sig_data; - INSERT_PADDING_BYTES(0x40); - TicketData data; - } ecdsa; - }; +struct ECDSATicket { + SignatureType sig_type; + std::array sig_data; + INSERT_PADDING_BYTES(0x40); + TicketData data; +}; + +struct Ticket { + std::variant data; + SignatureType GetSignatureType() const; TicketData& GetData(); const TicketData& GetData() const; u64 GetSize() const; - static Ticket SynthesizeCommon(Key128 title_key, std::array rights_id); + static Ticket SynthesizeCommon(Key128 title_key, const std::array& rights_id); }; static_assert(sizeof(Key128) == 16, "Key128 must be 128 bytes big."); @@ -120,6 +124,12 @@ bool operator==(const RSAKeyPair& lhs, std::tie(rhs.encryption_key, rhs.decryption_key, rhs.modulus, rhs.exponent); } +template +bool operator!=(const RSAKeyPair& lhs, + const RSAKeyPair& rhs) { + return !(lhs == rhs); +} + enum class KeyCategory : u8 { Standard, Title, @@ -268,7 +278,7 @@ private: void DeriveGeneralPurposeKeys(std::size_t crypto_revision); - RSAKeyPair<2048> GetETicketRSAKey(); + RSAKeyPair<2048> GetETicketRSAKey() const; void SetKeyWrapped(S128KeyType id, Key128 key, u64 field1 = 0, u64 field2 = 0); void SetKeyWrapped(S256KeyType id, Key256 key, u64 field1 = 0, u64 field2 = 0); -- cgit v1.2.3