Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:55:06

0001 // pkcspad.h - originally written and placed in the public domain by Wei Dai

0002 
0003 /// \file pkcspad.h

0004 /// \brief Classes for PKCS padding schemes

0005 /// \details PKCS #1 v1.5, v2.0 and P1363a allow MD2, MD5, SHA1, SHA224, SHA256, SHA384,

0006 ///   SHA512, Tiger and RipeMd-160 to be instantiated.

0007 
0008 #ifndef CRYPTOPP_PKCSPAD_H
0009 #define CRYPTOPP_PKCSPAD_H
0010 
0011 #include "cryptlib.h"
0012 #include "pubkey.h"
0013 #include "hashfwd.h"
0014 
0015 #ifdef CRYPTOPP_IS_DLL
0016 #include "sha.h"
0017 #endif
0018 
0019 NAMESPACE_BEGIN(CryptoPP)
0020 
0021 /// \brief PKCS #1 v1.5 Encryption Padding Scheme

0022 /// \sa <a href="http://www.weidai.com/scan-mirror/ca.html#cem_PKCS1-1.5">EME-PKCS1-v1_5</a>

0023 class PKCS_EncryptionPaddingScheme : public PK_EncryptionMessageEncodingMethod
0024 {
0025 public:
0026     CRYPTOPP_STATIC_CONSTEXPR const char* StaticAlgorithmName() {return "EME-PKCS1-v1_5";}
0027 
0028     size_t MaxUnpaddedLength(size_t paddedLength) const;
0029     void Pad(RandomNumberGenerator &rng, const byte *raw, size_t inputLength, byte *padded, size_t paddedLength, const NameValuePairs &parameters) const;
0030     DecodingResult Unpad(const byte *padded, size_t paddedLength, byte *raw, const NameValuePairs &parameters) const;
0031 };
0032 
0033 /// \brief PKCS #1 decoration data structure

0034 template <class H> class PKCS_DigestDecoration
0035 {
0036 public:
0037     static const byte decoration[];
0038     static const unsigned int length;
0039 };
0040 
0041 // PKCS_DigestDecoration can be instantiated with the following

0042 // classes as specified in PKCS #1 v2.0 and P1363a

0043 // SHA1, SHA224, SHA256, SHA384, SHA512, Tiger, RIPEMD160, MD2, MD5

0044 
0045 #if defined(CRYPTOPP_IS_DLL)
0046 CRYPTOPP_DLL_TEMPLATE_CLASS PKCS_DigestDecoration<SHA1>;
0047 CRYPTOPP_DLL_TEMPLATE_CLASS PKCS_DigestDecoration<SHA224>;
0048 CRYPTOPP_DLL_TEMPLATE_CLASS PKCS_DigestDecoration<SHA256>;
0049 CRYPTOPP_DLL_TEMPLATE_CLASS PKCS_DigestDecoration<SHA384>;
0050 CRYPTOPP_DLL_TEMPLATE_CLASS PKCS_DigestDecoration<SHA512>;
0051 // http://github.com/weidai11/cryptopp/issues/517

0052 CRYPTOPP_DLL_TEMPLATE_CLASS PKCS_DigestDecoration<SHA3_256>;
0053 CRYPTOPP_DLL_TEMPLATE_CLASS PKCS_DigestDecoration<SHA3_384>;
0054 CRYPTOPP_DLL_TEMPLATE_CLASS PKCS_DigestDecoration<SHA3_512>;
0055 #endif
0056 
0057 // https://github.com/weidai11/cryptopp/issues/300 and

0058 // https://github.com/weidai11/cryptopp/issues/533

0059 #if defined(__clang__)
0060 template<> const byte PKCS_DigestDecoration<SHA1>::decoration[];
0061 template<> const unsigned int PKCS_DigestDecoration<SHA1>::length;
0062 template<> const byte PKCS_DigestDecoration<SHA224>::decoration[];
0063 template<> const unsigned int PKCS_DigestDecoration<SHA224>::length;
0064 template<> const byte PKCS_DigestDecoration<SHA256>::decoration[];
0065 template<> const unsigned int PKCS_DigestDecoration<SHA256>::length;
0066 template<> const byte PKCS_DigestDecoration<SHA384>::decoration[];
0067 template<> const unsigned int PKCS_DigestDecoration<SHA384>::length;
0068 template<> const byte PKCS_DigestDecoration<SHA512>::decoration[];
0069 template<> const unsigned int PKCS_DigestDecoration<SHA512>::length;
0070 
0071 // http://github.com/weidai11/cryptopp/issues/517

0072 template<> const byte PKCS_DigestDecoration<SHA3_256>::decoration[];
0073 template<> const unsigned int PKCS_DigestDecoration<SHA3_256>::length;
0074 template<> const byte PKCS_DigestDecoration<SHA3_384>::decoration[];
0075 template<> const unsigned int PKCS_DigestDecoration<SHA3_384>::length;
0076 template<> const byte PKCS_DigestDecoration<SHA3_512>::decoration[];
0077 template<> const unsigned int PKCS_DigestDecoration<SHA3_512>::length;
0078 
0079 template<> const byte PKCS_DigestDecoration<Weak1::MD2>::decoration[];
0080 template<> const unsigned int PKCS_DigestDecoration<Weak1::MD2>::length;
0081 template<> const byte PKCS_DigestDecoration<Weak1::MD5>::decoration[];
0082 template<> const unsigned int PKCS_DigestDecoration<Weak1::MD5>::length;
0083 #endif
0084 
0085 /// \brief PKCS #1 v1.5 Signature Encoding Scheme

0086 /// \sa <a href="http://www.weidai.com/scan-mirror/sig.html#sem_PKCS1-1.5">EMSA-PKCS1-v1_5</a>

0087 class CRYPTOPP_DLL PKCS1v15_SignatureMessageEncodingMethod : public PK_DeterministicSignatureMessageEncodingMethod
0088 {
0089 public:
0090     CRYPTOPP_STATIC_CONSTEXPR const char* CRYPTOPP_API StaticAlgorithmName() {return "EMSA-PKCS1-v1_5";}
0091 
0092     size_t MinRepresentativeBitLength(size_t hashIdentifierSize, size_t digestSize) const
0093         {return 8 * (digestSize + hashIdentifierSize + 10);}
0094 
0095     void ComputeMessageRepresentative(RandomNumberGenerator &rng,
0096         const byte *recoverableMessage, size_t recoverableMessageLength,
0097         HashTransformation &hash, HashIdentifier hashIdentifier, bool messageEmpty,
0098         byte *representative, size_t representativeBitLength) const;
0099 
0100     struct HashIdentifierLookup
0101     {
0102         template <class H> struct HashIdentifierLookup2
0103         {
0104             static HashIdentifier Lookup()
0105             {
0106                 return HashIdentifier(PKCS_DigestDecoration<H>::decoration, PKCS_DigestDecoration<H>::length);
0107             }
0108         };
0109     };
0110 };
0111 
0112 /// \brief PKCS #1 version 1.5, for use with RSAES and RSASS

0113 /// \dontinclude pkcspad.h

0114 
0115 struct PKCS1v15 : public SignatureStandard, public EncryptionStandard
0116 {
0117     typedef PKCS_EncryptionPaddingScheme EncryptionMessageEncodingMethod;
0118     typedef PKCS1v15_SignatureMessageEncodingMethod SignatureMessageEncodingMethod;
0119 };
0120 
0121 NAMESPACE_END
0122 
0123 #endif