Botan 3.4.0
Crypto and TLS for C&
pss_params.cpp
Go to the documentation of this file.
1/*
2* (C) 2017 Daniel Neus
3* 2023 Jack Lloyd
4*
5* Botan is released under the Simplified BSD License (see license.txt)
6*/
7
8#include <botan/internal/pss_params.h>
9
10#include <botan/ber_dec.h>
11#include <botan/der_enc.h>
12#include <botan/internal/fmt.h>
13#include <botan/internal/scan_name.h>
14
15namespace Botan {
16
17//static
18PSS_Params PSS_Params::from_emsa_name(std::string_view emsa_name) {
19 SCAN_Name scanner(emsa_name);
20
21 if((scanner.algo_name() != "EMSA4" && scanner.algo_name() != "PSSR") || scanner.arg_count() != 3) {
22 throw Invalid_Argument(fmt("PSS_Params::from_emsa_name unexpected param '{}'", emsa_name));
23 }
24
25 const std::string hash_fn = scanner.arg(0);
26 BOTAN_ASSERT_NOMSG(scanner.arg(1) == "MGF1");
27 const size_t salt_len = scanner.arg_as_integer(2);
28 return PSS_Params(hash_fn, salt_len);
29}
30
31PSS_Params::PSS_Params(std::string_view hash_fn, size_t salt_len) :
32 m_hash(hash_fn, AlgorithmIdentifier::USE_NULL_PARAM),
33 m_mgf("MGF1", m_hash.BER_encode()),
34 m_mgf_hash(m_hash),
35 m_salt_len(salt_len) {}
36
37PSS_Params::PSS_Params(const uint8_t der[], size_t der_len) {
38 BER_Decoder decoder(der, der_len);
39 this->decode_from(decoder);
40}
41
42std::vector<uint8_t> PSS_Params::serialize() const {
43 std::vector<uint8_t> output;
44 DER_Encoder(output).encode(*this);
45 return output;
46}
47
49 const size_t trailer_field = 1;
50
53 .encode(m_hash)
54 .end_cons()
56 .encode(m_mgf)
57 .end_cons()
59 .encode(m_salt_len)
60 .end_cons()
63 .end_cons()
64 .end_cons();
65}
66
69 const AlgorithmIdentifier default_mgf("MGF1", default_hash.BER_encode());
70 const size_t default_salt_len = 20;
71 const size_t default_trailer = 1;
72
73 from.start_sequence()
76 .decode_optional(m_salt_len, ASN1_Type(2), ASN1_Class::ExplicitContextSpecific, default_salt_len)
77 .decode_optional(m_trailer_field, ASN1_Type(3), ASN1_Class::ExplicitContextSpecific, default_trailer)
78 .end_cons();
79
80 BER_Decoder(m_mgf.parameters()).decode(m_mgf_hash);
81}
82
83} // namespace Botan
#define BOTAN_ASSERT_NOMSG(expr)
Definition assert.h:59
std::vector< uint8_t > BER_encode() const
Definition asn1_obj.cpp:19
const std::vector< uint8_t > & parameters() const
Definition asn1_obj.h:457
BER_Decoder & decode(bool &out)
Definition ber_dec.h:176
BER_Decoder & end_cons()
Definition ber_dec.cpp:295
BER_Decoder start_sequence()
Definition ber_dec.h:113
BER_Decoder & decode_optional(T &out, ASN1_Type type_tag, ASN1_Class class_tag, const T &default_value=T())
Definition ber_dec.h:317
DER_Encoder & start_context_specific(uint32_t tag)
Definition der_enc.h:69
DER_Encoder & start_sequence()
Definition der_enc.h:65
DER_Encoder & end_cons()
Definition der_enc.cpp:171
DER_Encoder & encode(bool b)
Definition der_enc.cpp:250
void encode_into(DER_Encoder &to) const override
size_t trailer_field() const
Definition pss_params.h:35
std::vector< uint8_t > serialize() const
PSS_Params(std::string_view hash_fn, size_t salt_len)
static PSS_Params from_emsa_name(std::string_view emsa_name)
void decode_from(BER_Decoder &from) override
std::string arg(size_t i) const
size_t arg_count() const
Definition scan_name.h:49
const std::string & algo_name() const
Definition scan_name.h:44
size_t arg_as_integer(size_t i, size_t def_value) const
std::string fmt(std::string_view format, const T &... args)
Definition fmt.h:53
ASN1_Type
Definition asn1_obj.h:43