diff options
author | Jack Lloyd <[email protected]> | 2017-08-15 17:15:22 -0400 |
---|---|---|
committer | Jack Lloyd <[email protected]> | 2017-08-15 17:15:22 -0400 |
commit | defe02db7b384ce2454122bf61949379e4a8181b (patch) | |
tree | 896e395560a7180b381e459fc4b7bf7be5ded77a /src/lib | |
parent | ba75558b723ae0c8b1e3b8718d972ef2fa567dfd (diff) |
Allow signature using `Raw(hashname)`
This confirms the message is exactly the size of the expected hash,
and also causes RFC 6979 nonces to be generated using the specified
hash. See also https://github.com/riboseinc/rnp/issues/367
Diffstat (limited to 'src/lib')
-rw-r--r-- | src/lib/pk_pad/emsa.cpp | 13 | ||||
-rw-r--r-- | src/lib/pk_pad/emsa_raw/emsa_raw.cpp | 22 | ||||
-rw-r--r-- | src/lib/pk_pad/emsa_raw/emsa_raw.h | 10 |
3 files changed, 38 insertions, 7 deletions
diff --git a/src/lib/pk_pad/emsa.cpp b/src/lib/pk_pad/emsa.cpp index 074af273a..0fd35ef43 100644 --- a/src/lib/pk_pad/emsa.cpp +++ b/src/lib/pk_pad/emsa.cpp @@ -134,9 +134,18 @@ EMSA* get_emsa(const std::string& algo_spec) #endif #if defined(BOTAN_HAS_EMSA_RAW) - if(req.algo_name() == "Raw" && req.arg_count() == 0) + if(req.algo_name() == "Raw") { - return new EMSA_Raw; + if(req.arg_count() == 0) + { + return new EMSA_Raw; + } + else + { + auto hash = HashFunction::create(req.arg(0)); + if(hash) + return new EMSA_Raw(hash->output_length()); + } } #endif diff --git a/src/lib/pk_pad/emsa_raw/emsa_raw.cpp b/src/lib/pk_pad/emsa_raw/emsa_raw.cpp index d15012a0d..bae7b2d04 100644 --- a/src/lib/pk_pad/emsa_raw/emsa_raw.cpp +++ b/src/lib/pk_pad/emsa_raw/emsa_raw.cpp @@ -22,6 +22,12 @@ void EMSA_Raw::update(const uint8_t input[], size_t length) */ secure_vector<uint8_t> EMSA_Raw::raw_data() { + if(m_expected_size && m_message.size() != m_expected_size) + throw Invalid_Argument("EMSA_Raw was configured to use a " + + std::to_string(m_expected_size) + + " byte hash but instead was used for a " + + std::to_string(m_message.size()) + " hash"); + secure_vector<uint8_t> output; std::swap(m_message, output); return output; @@ -30,10 +36,17 @@ secure_vector<uint8_t> EMSA_Raw::raw_data() /* * EMSA-Raw Encode Operation */ -secure_vector<uint8_t> EMSA_Raw::encoding_of(const secure_vector<uint8_t>& msg, - size_t, - RandomNumberGenerator&) +secure_vector<uint8_t> +EMSA_Raw::encoding_of(const secure_vector<uint8_t>& msg, + size_t, + RandomNumberGenerator&) { + if(m_expected_size && msg.size() != m_expected_size) + throw Invalid_Argument("EMSA_Raw was configured to use a " + + std::to_string(m_expected_size) + + " byte hash but instead was used for a " + + std::to_string(msg.size()) + " hash"); + return msg; } @@ -44,6 +57,9 @@ bool EMSA_Raw::verify(const secure_vector<uint8_t>& coded, const secure_vector<uint8_t>& raw, size_t) { + if(m_expected_size && raw.size() != m_expected_size) + return false; + if(coded.size() == raw.size()) return (coded == raw); diff --git a/src/lib/pk_pad/emsa_raw/emsa_raw.h b/src/lib/pk_pad/emsa_raw/emsa_raw.h index 288969257..598b9b97c 100644 --- a/src/lib/pk_pad/emsa_raw/emsa_raw.h +++ b/src/lib/pk_pad/emsa_raw/emsa_raw.h @@ -21,15 +21,21 @@ class BOTAN_DLL EMSA_Raw final : public EMSA public: EMSA* clone() override { return new EMSA_Raw(); } + explicit EMSA_Raw(size_t expected_hash_size = 0) : + m_expected_size(expected_hash_size) {} + private: void update(const uint8_t[], size_t) override; secure_vector<uint8_t> raw_data() override; secure_vector<uint8_t> encoding_of(const secure_vector<uint8_t>&, size_t, - RandomNumberGenerator&) override; - bool verify(const secure_vector<uint8_t>&, const secure_vector<uint8_t>&, + RandomNumberGenerator&) override; + + bool verify(const secure_vector<uint8_t>&, + const secure_vector<uint8_t>&, size_t) override; + const size_t m_expected_size; secure_vector<uint8_t> m_message; }; |