Line | Count | Source (jump to first uncovered line) |
1 | | // pkcspad.h - originally written and placed in the public domain by Wei Dai |
2 | | |
3 | | /// \file pkcspad.h |
4 | | /// \brief Classes for PKCS padding schemes |
5 | | /// \details PKCS #1 v1.5, v2.0 and P1363a allow MD2, MD5, SHA1, SHA224, SHA256, SHA384, |
6 | | /// SHA512, Tiger and RipeMd-160 to be instantiated. |
7 | | |
8 | | #ifndef CRYPTOPP_PKCSPAD_H |
9 | | #define CRYPTOPP_PKCSPAD_H |
10 | | |
11 | | #include "cryptlib.h" |
12 | | #include "pubkey.h" |
13 | | #include "hashfwd.h" |
14 | | |
15 | | #ifdef CRYPTOPP_IS_DLL |
16 | | #include "sha.h" |
17 | | #endif |
18 | | |
19 | | NAMESPACE_BEGIN(CryptoPP) |
20 | | |
21 | | /// \brief PKCS #1 v1.5 Encryption Padding Scheme |
22 | | /// \sa <a href="http://www.weidai.com/scan-mirror/ca.html#cem_PKCS1-1.5">EME-PKCS1-v1_5</a> |
23 | | class PKCS_EncryptionPaddingScheme : public PK_EncryptionMessageEncodingMethod |
24 | | { |
25 | | public: |
26 | 0 | CRYPTOPP_STATIC_CONSTEXPR const char* StaticAlgorithmName() {return "EME-PKCS1-v1_5";} |
27 | | |
28 | | size_t MaxUnpaddedLength(size_t paddedLength) const; |
29 | | void Pad(RandomNumberGenerator &rng, const byte *raw, size_t inputLength, byte *padded, size_t paddedLength, const NameValuePairs ¶meters) const; |
30 | | DecodingResult Unpad(const byte *padded, size_t paddedLength, byte *raw, const NameValuePairs ¶meters) const; |
31 | | }; |
32 | | |
33 | | /// \brief PKCS #1 decoration data structure |
34 | | template <class H> class PKCS_DigestDecoration |
35 | | { |
36 | | public: |
37 | | static const byte decoration[]; |
38 | | static const unsigned int length; |
39 | | }; |
40 | | |
41 | | // PKCS_DigestDecoration can be instantiated with the following |
42 | | // classes as specified in PKCS #1 v2.0 and P1363a |
43 | | // SHA1, SHA224, SHA256, SHA384, SHA512, Tiger, RIPEMD160, MD2, MD5 |
44 | | |
45 | | #if defined(CRYPTOPP_IS_DLL) |
46 | | CRYPTOPP_DLL_TEMPLATE_CLASS PKCS_DigestDecoration<SHA1>; |
47 | | CRYPTOPP_DLL_TEMPLATE_CLASS PKCS_DigestDecoration<SHA224>; |
48 | | CRYPTOPP_DLL_TEMPLATE_CLASS PKCS_DigestDecoration<SHA256>; |
49 | | CRYPTOPP_DLL_TEMPLATE_CLASS PKCS_DigestDecoration<SHA384>; |
50 | | CRYPTOPP_DLL_TEMPLATE_CLASS PKCS_DigestDecoration<SHA512>; |
51 | | // http://github.com/weidai11/cryptopp/issues/517 |
52 | | CRYPTOPP_DLL_TEMPLATE_CLASS PKCS_DigestDecoration<SHA3_256>; |
53 | | CRYPTOPP_DLL_TEMPLATE_CLASS PKCS_DigestDecoration<SHA3_384>; |
54 | | CRYPTOPP_DLL_TEMPLATE_CLASS PKCS_DigestDecoration<SHA3_512>; |
55 | | #endif |
56 | | |
57 | | // https://github.com/weidai11/cryptopp/issues/300 and |
58 | | // https://github.com/weidai11/cryptopp/issues/533 |
59 | | #if defined(__clang__) |
60 | | template<> const byte PKCS_DigestDecoration<SHA1>::decoration[]; |
61 | | template<> const unsigned int PKCS_DigestDecoration<SHA1>::length; |
62 | | template<> const byte PKCS_DigestDecoration<SHA224>::decoration[]; |
63 | | template<> const unsigned int PKCS_DigestDecoration<SHA224>::length; |
64 | | template<> const byte PKCS_DigestDecoration<SHA256>::decoration[]; |
65 | | template<> const unsigned int PKCS_DigestDecoration<SHA256>::length; |
66 | | template<> const byte PKCS_DigestDecoration<SHA384>::decoration[]; |
67 | | template<> const unsigned int PKCS_DigestDecoration<SHA384>::length; |
68 | | template<> const byte PKCS_DigestDecoration<SHA512>::decoration[]; |
69 | | template<> const unsigned int PKCS_DigestDecoration<SHA512>::length; |
70 | | |
71 | | // http://github.com/weidai11/cryptopp/issues/517 |
72 | | template<> const byte PKCS_DigestDecoration<SHA3_256>::decoration[]; |
73 | | template<> const unsigned int PKCS_DigestDecoration<SHA3_256>::length; |
74 | | template<> const byte PKCS_DigestDecoration<SHA3_384>::decoration[]; |
75 | | template<> const unsigned int PKCS_DigestDecoration<SHA3_384>::length; |
76 | | template<> const byte PKCS_DigestDecoration<SHA3_512>::decoration[]; |
77 | | template<> const unsigned int PKCS_DigestDecoration<SHA3_512>::length; |
78 | | |
79 | | template<> const byte PKCS_DigestDecoration<Weak1::MD2>::decoration[]; |
80 | | template<> const unsigned int PKCS_DigestDecoration<Weak1::MD2>::length; |
81 | | template<> const byte PKCS_DigestDecoration<Weak1::MD5>::decoration[]; |
82 | | template<> const unsigned int PKCS_DigestDecoration<Weak1::MD5>::length; |
83 | | #endif |
84 | | |
85 | | /// \brief PKCS #1 v1.5 Signature Encoding Scheme |
86 | | /// \sa <a href="http://www.weidai.com/scan-mirror/sig.html#sem_PKCS1-1.5">EMSA-PKCS1-v1_5</a> |
87 | | class CRYPTOPP_DLL PKCS1v15_SignatureMessageEncodingMethod : public PK_DeterministicSignatureMessageEncodingMethod |
88 | | { |
89 | | public: |
90 | 0 | CRYPTOPP_STATIC_CONSTEXPR const char* CRYPTOPP_API StaticAlgorithmName() {return "EMSA-PKCS1-v1_5";} |
91 | | |
92 | | size_t MinRepresentativeBitLength(size_t hashIdentifierSize, size_t digestSize) const |
93 | 0 | {return 8 * (digestSize + hashIdentifierSize + 10);} |
94 | | |
95 | | void ComputeMessageRepresentative(RandomNumberGenerator &rng, |
96 | | const byte *recoverableMessage, size_t recoverableMessageLength, |
97 | | HashTransformation &hash, HashIdentifier hashIdentifier, bool messageEmpty, |
98 | | byte *representative, size_t representativeBitLength) const; |
99 | | |
100 | | struct HashIdentifierLookup |
101 | | { |
102 | | template <class H> struct HashIdentifierLookup2 |
103 | | { |
104 | | static HashIdentifier Lookup() |
105 | | { |
106 | | return HashIdentifier(PKCS_DigestDecoration<H>::decoration, PKCS_DigestDecoration<H>::length); |
107 | | } |
108 | | }; |
109 | | }; |
110 | | }; |
111 | | |
112 | | /// \brief PKCS #1 version 1.5, for use with RSAES and RSASS |
113 | | /// \dontinclude pkcspad.h |
114 | | |
115 | | struct PKCS1v15 : public SignatureStandard, public EncryptionStandard |
116 | | { |
117 | | typedef PKCS_EncryptionPaddingScheme EncryptionMessageEncodingMethod; |
118 | | typedef PKCS1v15_SignatureMessageEncodingMethod SignatureMessageEncodingMethod; |
119 | | }; |
120 | | |
121 | | NAMESPACE_END |
122 | | |
123 | | #endif |