/src/Botan-3.4.0/src/lib/asn1/pss_params.cpp
Line | Count | Source (jump to first uncovered line) |
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 | | |
15 | | namespace Botan { |
16 | | |
17 | | //static |
18 | 0 | PSS_Params PSS_Params::from_emsa_name(std::string_view emsa_name) { |
19 | 0 | SCAN_Name scanner(emsa_name); |
20 | |
|
21 | 0 | if((scanner.algo_name() != "EMSA4" && scanner.algo_name() != "PSSR") || scanner.arg_count() != 3) { |
22 | 0 | throw Invalid_Argument(fmt("PSS_Params::from_emsa_name unexpected param '{}'", emsa_name)); |
23 | 0 | } |
24 | | |
25 | 0 | const std::string hash_fn = scanner.arg(0); |
26 | 0 | BOTAN_ASSERT_NOMSG(scanner.arg(1) == "MGF1"); |
27 | 0 | const size_t salt_len = scanner.arg_as_integer(2); |
28 | 0 | return PSS_Params(hash_fn, salt_len); |
29 | 0 | } |
30 | | |
31 | | PSS_Params::PSS_Params(std::string_view hash_fn, size_t salt_len) : |
32 | 0 | m_hash(hash_fn, AlgorithmIdentifier::USE_NULL_PARAM), |
33 | 0 | m_mgf("MGF1", m_hash.BER_encode()), |
34 | 0 | m_mgf_hash(m_hash), |
35 | 0 | m_salt_len(salt_len) {} |
36 | | |
37 | 0 | PSS_Params::PSS_Params(const uint8_t der[], size_t der_len) { |
38 | 0 | BER_Decoder decoder(der, der_len); |
39 | 0 | this->decode_from(decoder); |
40 | 0 | } |
41 | | |
42 | 0 | std::vector<uint8_t> PSS_Params::serialize() const { |
43 | 0 | std::vector<uint8_t> output; |
44 | 0 | DER_Encoder(output).encode(*this); |
45 | 0 | return output; |
46 | 0 | } |
47 | | |
48 | 0 | void PSS_Params::encode_into(DER_Encoder& to) const { |
49 | 0 | const size_t trailer_field = 1; |
50 | |
|
51 | 0 | to.start_sequence() |
52 | 0 | .start_context_specific(0) |
53 | 0 | .encode(m_hash) |
54 | 0 | .end_cons() |
55 | 0 | .start_context_specific(1) |
56 | 0 | .encode(m_mgf) |
57 | 0 | .end_cons() |
58 | 0 | .start_context_specific(2) |
59 | 0 | .encode(m_salt_len) |
60 | 0 | .end_cons() |
61 | 0 | .start_context_specific(3) |
62 | 0 | .encode(trailer_field) |
63 | 0 | .end_cons() |
64 | 0 | .end_cons(); |
65 | 0 | } |
66 | | |
67 | 0 | void PSS_Params::decode_from(BER_Decoder& from) { |
68 | 0 | const AlgorithmIdentifier default_hash("SHA-1", AlgorithmIdentifier::USE_NULL_PARAM); |
69 | 0 | const AlgorithmIdentifier default_mgf("MGF1", default_hash.BER_encode()); |
70 | 0 | const size_t default_salt_len = 20; |
71 | 0 | const size_t default_trailer = 1; |
72 | |
|
73 | 0 | from.start_sequence() |
74 | 0 | .decode_optional(m_hash, ASN1_Type(0), ASN1_Class::ExplicitContextSpecific, default_hash) |
75 | 0 | .decode_optional(m_mgf, ASN1_Type(1), ASN1_Class::ExplicitContextSpecific, default_mgf) |
76 | 0 | .decode_optional(m_salt_len, ASN1_Type(2), ASN1_Class::ExplicitContextSpecific, default_salt_len) |
77 | 0 | .decode_optional(m_trailer_field, ASN1_Type(3), ASN1_Class::ExplicitContextSpecific, default_trailer) |
78 | 0 | .end_cons(); |
79 | |
|
80 | 0 | BER_Decoder(m_mgf.parameters()).decode(m_mgf_hash); |
81 | 0 | } |
82 | | |
83 | | } // namespace Botan |