1 | // pkcspad.h - 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, SHA512, Tiger and RipeMd-160 to be instantiated. |
---|
6 | |
---|
7 | #ifndef CRYPTOPP_PKCSPAD_H |
---|
8 | #define CRYPTOPP_PKCSPAD_H |
---|
9 | |
---|
10 | #include "cryptlib.h" |
---|
11 | #include "pubkey.h" |
---|
12 | |
---|
13 | #ifdef CRYPTOPP_IS_DLL |
---|
14 | #include "sha.h" |
---|
15 | #endif |
---|
16 | |
---|
17 | NAMESPACE_BEGIN(CryptoPP) |
---|
18 | |
---|
19 | //! \class PKCS_EncryptionPaddingScheme |
---|
20 | //! \brief PKCS#1 v1.5 Encryption Padding Scheme |
---|
21 | //! \sa <a href="http://www.weidai.com/scan-mirror/ca.html#cem_PKCS1-1.5">EME-PKCS1-v1_5</a> |
---|
22 | class PKCS_EncryptionPaddingScheme : public PK_EncryptionMessageEncodingMethod |
---|
23 | { |
---|
24 | public: |
---|
25 | CRYPTOPP_CONSTEXPR static const char *StaticAlgorithmName() {return "EME-PKCS1-v1_5";} |
---|
26 | |
---|
27 | size_t MaxUnpaddedLength(size_t paddedLength) const; |
---|
28 | void Pad(RandomNumberGenerator &rng, const byte *raw, size_t inputLength, byte *padded, size_t paddedLength, const NameValuePairs ¶meters) const; |
---|
29 | DecodingResult Unpad(const byte *padded, size_t paddedLength, byte *raw, const NameValuePairs ¶meters) const; |
---|
30 | }; |
---|
31 | |
---|
32 | //! \class PKCS_DigestDecoration |
---|
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 | class SHA1; |
---|
44 | class SHA224; |
---|
45 | class SHA256; |
---|
46 | class SHA384; |
---|
47 | class SHA512; |
---|
48 | class Tiger; |
---|
49 | class RIPEMD160; |
---|
50 | namespace Weak1 { |
---|
51 | class MD2; |
---|
52 | class MD5; |
---|
53 | } |
---|
54 | // end of list |
---|
55 | |
---|
56 | #ifdef CRYPTOPP_IS_DLL |
---|
57 | CRYPTOPP_DLL_TEMPLATE_CLASS PKCS_DigestDecoration<SHA1>; |
---|
58 | CRYPTOPP_DLL_TEMPLATE_CLASS PKCS_DigestDecoration<SHA224>; |
---|
59 | CRYPTOPP_DLL_TEMPLATE_CLASS PKCS_DigestDecoration<SHA256>; |
---|
60 | CRYPTOPP_DLL_TEMPLATE_CLASS PKCS_DigestDecoration<SHA384>; |
---|
61 | CRYPTOPP_DLL_TEMPLATE_CLASS PKCS_DigestDecoration<SHA512>; |
---|
62 | #endif |
---|
63 | |
---|
64 | //! \class PKCS1v15_SignatureMessageEncodingMethod |
---|
65 | //! \brief PKCS#1 v1.5 Signature Encoding Scheme |
---|
66 | //! \sa <a href="http://www.weidai.com/scan-mirror/sig.html#sem_PKCS1-1.5">EMSA-PKCS1-v1_5</a> |
---|
67 | class CRYPTOPP_DLL PKCS1v15_SignatureMessageEncodingMethod : public PK_DeterministicSignatureMessageEncodingMethod |
---|
68 | { |
---|
69 | public: |
---|
70 | CRYPTOPP_CONSTEXPR static const char * CRYPTOPP_API StaticAlgorithmName() {return "EMSA-PKCS1-v1_5";} |
---|
71 | |
---|
72 | size_t MinRepresentativeBitLength(size_t hashIdentifierSize, size_t digestSize) const |
---|
73 | {return 8 * (digestSize + hashIdentifierSize + 10);} |
---|
74 | |
---|
75 | void ComputeMessageRepresentative(RandomNumberGenerator &rng, |
---|
76 | const byte *recoverableMessage, size_t recoverableMessageLength, |
---|
77 | HashTransformation &hash, HashIdentifier hashIdentifier, bool messageEmpty, |
---|
78 | byte *representative, size_t representativeBitLength) const; |
---|
79 | |
---|
80 | struct HashIdentifierLookup |
---|
81 | { |
---|
82 | template <class H> struct HashIdentifierLookup2 |
---|
83 | { |
---|
84 | static HashIdentifier Lookup() |
---|
85 | { |
---|
86 | return HashIdentifier(PKCS_DigestDecoration<H>::decoration, PKCS_DigestDecoration<H>::length); |
---|
87 | } |
---|
88 | }; |
---|
89 | }; |
---|
90 | }; |
---|
91 | |
---|
92 | //! PKCS #1 version 1.5, for use with RSAES and RSASS |
---|
93 | /*! Only the following hash functions are supported by this signature standard: |
---|
94 | \dontinclude pkcspad.h |
---|
95 | \skip can be instantiated |
---|
96 | \until end of list |
---|
97 | */ |
---|
98 | struct PKCS1v15 : public SignatureStandard, public EncryptionStandard |
---|
99 | { |
---|
100 | typedef PKCS_EncryptionPaddingScheme EncryptionMessageEncodingMethod; |
---|
101 | typedef PKCS1v15_SignatureMessageEncodingMethod SignatureMessageEncodingMethod; |
---|
102 | }; |
---|
103 | |
---|
104 | NAMESPACE_END |
---|
105 | |
---|
106 | #endif |
---|