Botan 3.14.1
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/pss_params.h>
9
10#include <botan/assert.h>
11#include <botan/ber_dec.h>
12#include <botan/der_enc.h>
13#include <botan/internal/algorithm_spec.h>
14#include <botan/internal/fmt.h>
15
16namespace Botan {
17
18//static
19PSS_Params PSS_Params::from_padding_name(std::string_view padding_name) {
20 const AlgorithmSpec spec(padding_name);
21
22 const auto m = spec.match("PSS|PSS_Raw({hash},MGF1,{salt_len:int})");
23 if(!m) {
24 throw Invalid_Argument(fmt("PSS_Params::from_padding_name unexpected param '{}'", padding_name));
25 }
26
27 return PSS_Params(m->str("hash"), m->integer("salt_len"));
28}
29
30PSS_Params::PSS_Params(std::string_view hash_fn, size_t salt_len) :
31 m_hash(hash_fn, AlgorithmIdentifier::USE_NULL_PARAM),
32 m_mgf("MGF1", m_hash.BER_encode()),
33 m_mgf_hash(m_hash),
34 m_salt_len(salt_len),
35 m_trailer_field(1) {}
36
37PSS_Params::PSS_Params(std::span<const uint8_t> der) : m_salt_len(0), m_trailer_field(1) {
39 this->decode_from(decoder);
40 decoder.verify_end();
41}
42
43std::vector<uint8_t> PSS_Params::serialize() const {
44 std::vector<uint8_t> output;
45 DER_Encoder(output).encode(*this);
46 return output;
47}
48
52 .encode(m_hash)
53 .end_cons()
55 .encode(m_mgf)
56 .end_cons()
58 .encode(m_salt_len)
59 .end_cons()
60 .end_cons();
61}
62
63void PSS_Params::decode_from(BER_Decoder& from) {
65 const AlgorithmIdentifier default_mgf("MGF1", default_hash.BER_encode());
66 const size_t default_salt_len = 20;
67 const size_t default_trailer = 1;
68
69 from.start_sequence()
72 .decode_optional(m_salt_len, ASN1_Type(2), ASN1_Class::ExplicitContextSpecific, default_salt_len)
73 .decode_optional(m_trailer_field, ASN1_Type(3), ASN1_Class::ExplicitContextSpecific, default_trailer)
74 .end_cons();
75
76 BER_Decoder(m_mgf.parameters(), from.limits()).decode(m_mgf_hash).verify_end();
77
78 if(!m_hash.parameters_are_null_or_empty() || !m_mgf_hash.parameters_are_null_or_empty()) {
79 throw Decoding_Error("Unexpected parameters for PSS hash algorithm identifier");
80 }
81}
82
83} // namespace Botan
std::vector< uint8_t > BER_encode() const
Definition asn1_obj.cpp:21
bool parameters_are_null_or_empty() const
Definition asn1_obj.h:720
const std::vector< uint8_t > & parameters() const
Definition asn1_obj.h:693
std::optional< AlgorithmMatch > match(const AlgorithmPattern &pattern) const &
static Limits DER()
Definition ber_dec.h:44
BER_Decoder & decode(bool &out)
Definition ber_dec.h:361
Limits limits() const
Definition ber_dec.h:201
BER_Decoder & verify_end()
Definition ber_dec.cpp:471
BER_Decoder & end_cons()
Definition ber_dec.cpp:635
BER_Decoder start_sequence()
Definition ber_dec.h:279
BER_Decoder & decode_optional(T &out, ASN1_Type type_tag, ASN1_Class class_tag, const T &default_value=T())
Definition ber_dec.h:550
DER_Encoder & start_context_specific(uint32_t tag)
Definition der_enc.h:113
DER_Encoder & start_sequence()
Definition der_enc.h:86
DER_Encoder & end_cons()
Definition der_enc.cpp:208
DER_Encoder & encode(bool b)
Definition der_enc.cpp:313
static PSS_Params from_padding_name(std::string_view padding_name)
void encode_into(DER_Encoder &to) const override
std::vector< uint8_t > serialize() const
PSS_Params(std::string_view hash_fn, size_t salt_len)
std::string fmt(std::string_view format, const T &... args)
Definition fmt.h:53
ASN1_Type
Definition asn1_obj.h:47