00001 #ifndef CRYPTOPP_PKCSPAD_H
00002 #define CRYPTOPP_PKCSPAD_H
00003
00004 #include "cryptlib.h"
00005 #include "pubkey.h"
00006
00007 #ifdef CRYPTOPP_IS_DLL
00008 #include "sha.h"
00009 #endif
00010
00011 NAMESPACE_BEGIN(CryptoPP)
00012
00013
00014 class PKCS_EncryptionPaddingScheme : public PK_PaddingAlgorithm
00015 {
00016 public:
00017 static const char * StaticAlgorithmName() {return "EME-PKCS1-v1_5";}
00018
00019 unsigned int MaxUnpaddedLength(unsigned int paddedLength) const;
00020 void Pad(RandomNumberGenerator &rng, const byte *raw, unsigned int inputLength, byte *padded, unsigned int paddedLength) const;
00021 DecodingResult Unpad(const byte *padded, unsigned int paddedLength, byte *raw) const;
00022 };
00023
00024
00025 class CRYPTOPP_DLL PKCS_SignaturePaddingScheme : public PK_PaddingAlgorithm
00026 {
00027 public:
00028 static const char * StaticAlgorithmName() {return "EMSA-PKCS1-v1_5";}
00029
00030 unsigned int MaxUnpaddedLength(unsigned int paddedLength) const;
00031 void Pad(RandomNumberGenerator &rng, const byte *raw, unsigned int inputLength, byte *padded, unsigned int paddedLength) const;
00032 DecodingResult Unpad(const byte *padded, unsigned int paddedLength, byte *raw) const;
00033 };
00034
00035
00036 template <class H>
00037 class PKCS_DecoratedHashModule : public HashTransformationWithDefaultTruncation
00038 {
00039 public:
00040 static std::string StaticAlgorithmName() {return std::string("EMSA-PKCS1-v1_5(") + H::StaticAlgorithmName() + ")";}
00041
00042 void Update(const byte *input, unsigned int length)
00043 {h.Update(input, length);}
00044 unsigned int DigestSize() const;
00045 void Final(byte *digest);
00046 void Restart() {h.Restart();}
00047
00048 private:
00049 H h;
00050 };
00051
00052 #ifdef CRYPTOPP_IS_DLL
00053 CRYPTOPP_DLL_TEMPLATE_CLASS PKCS_DecoratedHashModule<SHA>;
00054 #endif
00055
00056
00057
00058 struct PKCS1v15 : public SignatureStandard, public EncryptionStandard
00059 {
00060 typedef PKCS_EncryptionPaddingScheme EncryptionPaddingAlgorithm;
00061
00062 template <class H> struct SignaturePaddingAlgorithm {typedef PKCS_SignaturePaddingScheme type;};
00063 template <class H> struct DecoratedHashingAlgorithm {typedef PKCS_DecoratedHashModule<H> type;};
00064 };
00065
00066 template<> struct CryptoStandardTraits<PKCS1v15> : public PKCS1v15 {};
00067
00068 template <class H> class PKCS_DigestDecoration
00069 {
00070 public:
00071 static const byte decoration[];
00072 static const unsigned int length;
00073 };
00074
00075 #ifdef CRYPTOPP_IS_DLL
00076 CRYPTOPP_DLL_TEMPLATE_CLASS PKCS_DigestDecoration<SHA>;
00077 #endif
00078
00079
00080
00081 class SHA;
00082 class MD2;
00083 class MD5;
00084 class RIPEMD160;
00085 class SHA256;
00086 class SHA384;
00087 class SHA512;
00088
00089 template <class H>
00090 void PKCS_DecoratedHashModule<H>::Final(byte *digest)
00091 {
00092 const unsigned int decorationLen = PKCS_DigestDecoration<H>::length;
00093 memcpy(digest, PKCS_DigestDecoration<H>::decoration, decorationLen);
00094 h.Final(digest+decorationLen);
00095 }
00096
00097 template <class H>
00098 unsigned int PKCS_DecoratedHashModule<H>::DigestSize() const
00099 {
00100 return h.DigestSize() + PKCS_DigestDecoration<H>::length;
00101 }
00102
00103 NAMESPACE_END
00104
00105 #endif