00001
00002
00003 #include "pch.h"
00004
00005 #ifndef CRYPTOPP_PKCSPAD_CPP // SunCC workaround: compiler could cause this file to be included twice
00006 #define CRYPTOPP_PKCSPAD_CPP
00007
00008 #include "pkcspad.h"
00009 #include "misc.h"
00010 #include <assert.h>
00011
00012 NAMESPACE_BEGIN(CryptoPP)
00013
00014
00015 template<> const byte PKCS_DigestDecoration<Weak1::MD2>::decoration[] = {0x30,0x20,0x30,0x0c,0x06,0x08,0x2a,0x86,0x48,0x86,0xf7,0x0d,0x02,0x02,0x05,0x00,0x04,0x10};
00016 template<> const unsigned int PKCS_DigestDecoration<Weak1::MD2>::length = sizeof(PKCS_DigestDecoration<Weak1::MD2>::decoration);
00017
00018 template<> const byte PKCS_DigestDecoration<Weak1::MD5>::decoration[] = {0x30,0x20,0x30,0x0c,0x06,0x08,0x2a,0x86,0x48,0x86,0xf7,0x0d,0x02,0x05,0x05,0x00,0x04,0x10};
00019 template<> const unsigned int PKCS_DigestDecoration<Weak1::MD5>::length = sizeof(PKCS_DigestDecoration<Weak1::MD5>::decoration);
00020
00021 template<> const byte PKCS_DigestDecoration<RIPEMD160>::decoration[] = {0x30,0x21,0x30,0x09,0x06,0x05,0x2b,0x24,0x03,0x02,0x01,0x05,0x00,0x04,0x14};
00022 template<> const unsigned int PKCS_DigestDecoration<RIPEMD160>::length = sizeof(PKCS_DigestDecoration<RIPEMD160>::decoration);
00023
00024 template<> const byte PKCS_DigestDecoration<Tiger>::decoration[] = {0x30,0x29,0x30,0x0D,0x06,0x09,0x2B,0x06,0x01,0x04,0x01,0xDA,0x47,0x0C,0x02,0x05,0x00,0x04,0x18};
00025 template<> const unsigned int PKCS_DigestDecoration<Tiger>::length = sizeof(PKCS_DigestDecoration<Tiger>::decoration);
00026
00027 size_t PKCS_EncryptionPaddingScheme::MaxUnpaddedLength(size_t paddedLength) const
00028 {
00029 return SaturatingSubtract(paddedLength/8, 10U);
00030 }
00031
00032 void PKCS_EncryptionPaddingScheme::Pad(RandomNumberGenerator& rng, const byte *input, size_t inputLen, byte *pkcsBlock, size_t pkcsBlockLen, const NameValuePairs& parameters) const
00033 {
00034 CRYPTOPP_UNUSED(parameters);
00035 assert (inputLen <= MaxUnpaddedLength(pkcsBlockLen));
00036
00037
00038 if (pkcsBlockLen % 8 != 0)
00039 {
00040 pkcsBlock[0] = 0;
00041 pkcsBlock++;
00042 }
00043 pkcsBlockLen /= 8;
00044
00045 pkcsBlock[0] = 2;
00046
00047
00048 for (unsigned i = 1; i < pkcsBlockLen-inputLen-1; i++)
00049 pkcsBlock[i] = (byte)rng.GenerateWord32(1, 0xff);
00050
00051 pkcsBlock[pkcsBlockLen-inputLen-1] = 0;
00052 memcpy(pkcsBlock+pkcsBlockLen-inputLen, input, inputLen);
00053 }
00054
00055 DecodingResult PKCS_EncryptionPaddingScheme::Unpad(const byte *pkcsBlock, size_t pkcsBlockLen, byte *output, const NameValuePairs& parameters) const
00056 {
00057 CRYPTOPP_UNUSED(parameters);
00058 bool invalid = false;
00059 size_t maxOutputLen = MaxUnpaddedLength(pkcsBlockLen);
00060
00061
00062 if (pkcsBlockLen % 8 != 0)
00063 {
00064 invalid = (pkcsBlock[0] != 0) || invalid;
00065 pkcsBlock++;
00066 }
00067 pkcsBlockLen /= 8;
00068
00069
00070 invalid = (pkcsBlock[0] != 2) || invalid;
00071
00072
00073 size_t i=1;
00074 while (i<pkcsBlockLen && pkcsBlock[i++]) {
00075 }
00076 assert(i==pkcsBlockLen || pkcsBlock[i-1]==0);
00077
00078 size_t outputLen = pkcsBlockLen - i;
00079 invalid = (outputLen > maxOutputLen) || invalid;
00080
00081 if (invalid)
00082 return DecodingResult();
00083
00084 memcpy (output, pkcsBlock+i, outputLen);
00085 return DecodingResult(outputLen);
00086 }
00087
00088
00089
00090 #ifndef CRYPTOPP_IMPORTS
00091
00092 void PKCS1v15_SignatureMessageEncodingMethod::ComputeMessageRepresentative(RandomNumberGenerator &rng,
00093 const byte *recoverableMessage, size_t recoverableMessageLength,
00094 HashTransformation &hash, HashIdentifier hashIdentifier, bool messageEmpty,
00095 byte *representative, size_t representativeBitLength) const
00096 {
00097 CRYPTOPP_UNUSED(rng), CRYPTOPP_UNUSED(recoverableMessage), CRYPTOPP_UNUSED(recoverableMessageLength);
00098 CRYPTOPP_UNUSED(messageEmpty), CRYPTOPP_UNUSED(hashIdentifier);
00099 assert(representativeBitLength >= MinRepresentativeBitLength(hashIdentifier.second, hash.DigestSize()));
00100
00101 size_t pkcsBlockLen = representativeBitLength;
00102
00103 if (pkcsBlockLen % 8 != 0)
00104 {
00105 representative[0] = 0;
00106 representative++;
00107 }
00108 pkcsBlockLen /= 8;
00109
00110 representative[0] = 1;
00111
00112 unsigned int digestSize = hash.DigestSize();
00113 byte *pPadding = representative + 1;
00114 byte *pDigest = representative + pkcsBlockLen - digestSize;
00115 byte *pHashId = pDigest - hashIdentifier.second;
00116 byte *pSeparator = pHashId - 1;
00117
00118
00119 memset(pPadding, 0xff, pSeparator-pPadding);
00120 *pSeparator = 0;
00121 memcpy(pHashId, hashIdentifier.first, hashIdentifier.second);
00122 hash.Final(pDigest);
00123 }
00124
00125 #endif
00126
00127 NAMESPACE_END
00128
00129 #endif