Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | File Members

pkcspad.cpp

00001 // pkcspad.cpp - written and placed in the public domain by Wei Dai 00002 00003 #include "pch.h" 00004 00005 #include "pkcspad.h" 00006 #include <assert.h> 00007 00008 NAMESPACE_BEGIN(CryptoPP) 00009 00010 template<> const byte PKCS_DigestDecoration<MD2>::decoration[] = {0x30,0x20,0x30,0x0c,0x06,0x08,0x2a,0x86,0x48,0x86,0xf7,0x0d,0x02,0x02,0x05,0x00,0x04,0x10}; 00011 template<> const unsigned int PKCS_DigestDecoration<MD2>::length = sizeof(PKCS_DigestDecoration<MD2>::decoration); 00012 00013 template<> const byte PKCS_DigestDecoration<MD5>::decoration[] = {0x30,0x20,0x30,0x0c,0x06,0x08,0x2a,0x86,0x48,0x86,0xf7,0x0d,0x02,0x05,0x05,0x00,0x04,0x10}; 00014 template<> const unsigned int PKCS_DigestDecoration<MD5>::length = sizeof(PKCS_DigestDecoration<MD5>::decoration); 00015 00016 template<> const byte PKCS_DigestDecoration<RIPEMD160>::decoration[] = {0x30,0x21,0x30,0x09,0x06,0x05,0x2b,0x24,0x03,0x02,0x01,0x05,0x00,0x04,0x14}; 00017 template<> const unsigned int PKCS_DigestDecoration<RIPEMD160>::length = sizeof(PKCS_DigestDecoration<RIPEMD160>::decoration); 00018 00019 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}; 00020 template<> const unsigned int PKCS_DigestDecoration<Tiger>::length = sizeof(PKCS_DigestDecoration<Tiger>::decoration); 00021 00022 template<> const byte PKCS_DigestDecoration<SHA256>::decoration[] = {0x30,0x31,0x30,0x0d,0x06,0x09,0x60,0x86,0x48,0x01,0x65,0x03,0x04,0x02,0x01,0x05,0x00,0x04,0x20}; 00023 template<> const unsigned int PKCS_DigestDecoration<SHA256>::length = sizeof(PKCS_DigestDecoration<SHA256>::decoration); 00024 00025 template<> const byte PKCS_DigestDecoration<SHA384>::decoration[] = {0x30,0x41,0x30,0x0d,0x06,0x09,0x60,0x86,0x48,0x01,0x65,0x03,0x04,0x02,0x02,0x05,0x00,0x04,0x30}; 00026 template<> const unsigned int PKCS_DigestDecoration<SHA384>::length = sizeof(PKCS_DigestDecoration<SHA384>::decoration); 00027 00028 template<> const byte PKCS_DigestDecoration<SHA512>::decoration[] = {0x30,0x51,0x30,0x0d,0x06,0x09,0x60,0x86,0x48,0x01,0x65,0x03,0x04,0x02,0x03,0x05,0x00,0x04,0x40}; 00029 template<> const unsigned int PKCS_DigestDecoration<SHA512>::length = sizeof(PKCS_DigestDecoration<SHA512>::decoration); 00030 00031 unsigned int PKCS_EncryptionPaddingScheme::MaxUnpaddedLength(unsigned int paddedLength) const 00032 { 00033 return SaturatingSubtract(paddedLength/8, 10U); 00034 } 00035 00036 void PKCS_EncryptionPaddingScheme::Pad(RandomNumberGenerator &rng, const byte *input, unsigned int inputLen, byte *pkcsBlock, unsigned int pkcsBlockLen, const NameValuePairs &parameters) const 00037 { 00038 assert (inputLen <= MaxUnpaddedLength(pkcsBlockLen)); // this should be checked by caller 00039 00040 // convert from bit length to byte length 00041 if (pkcsBlockLen % 8 != 0) 00042 { 00043 pkcsBlock[0] = 0; 00044 pkcsBlock++; 00045 } 00046 pkcsBlockLen /= 8; 00047 00048 pkcsBlock[0] = 2; // block type 2 00049 00050 // pad with non-zero random bytes 00051 for (unsigned i = 1; i < pkcsBlockLen-inputLen-1; i++) 00052 pkcsBlock[i] = (byte)rng.GenerateWord32(1, 0xff); 00053 00054 pkcsBlock[pkcsBlockLen-inputLen-1] = 0; // separator 00055 memcpy(pkcsBlock+pkcsBlockLen-inputLen, input, inputLen); 00056 } 00057 00058 DecodingResult PKCS_EncryptionPaddingScheme::Unpad(const byte *pkcsBlock, unsigned int pkcsBlockLen, byte *output, const NameValuePairs &parameters) const 00059 { 00060 bool invalid = false; 00061 unsigned int maxOutputLen = MaxUnpaddedLength(pkcsBlockLen); 00062 00063 // convert from bit length to byte length 00064 if (pkcsBlockLen % 8 != 0) 00065 { 00066 invalid = (pkcsBlock[0] != 0) || invalid; 00067 pkcsBlock++; 00068 } 00069 pkcsBlockLen /= 8; 00070 00071 // Require block type 2. 00072 invalid = (pkcsBlock[0] != 2) || invalid; 00073 00074 // skip past the padding until we find the separator 00075 unsigned i=1; 00076 while (i<pkcsBlockLen && pkcsBlock[i++]) { // null body 00077 } 00078 assert(i==pkcsBlockLen || pkcsBlock[i-1]==0); 00079 00080 unsigned int outputLen = pkcsBlockLen - i; 00081 invalid = (outputLen > maxOutputLen) || invalid; 00082 00083 if (invalid) 00084 return DecodingResult(); 00085 00086 memcpy (output, pkcsBlock+i, outputLen); 00087 return DecodingResult(outputLen); 00088 } 00089 00090 // ******************************************************** 00091 00092 #ifndef CRYPTOPP_IMPORTS 00093 00094 void PKCS1v15_SignatureMessageEncodingMethod::ComputeMessageRepresentative(RandomNumberGenerator &rng, 00095 const byte *recoverableMessage, unsigned int recoverableMessageLength, 00096 HashTransformation &hash, HashIdentifier hashIdentifier, bool messageEmpty, 00097 byte *representative, unsigned int representativeBitLength) const 00098 { 00099 unsigned int digestSize = hash.DigestSize(); 00100 if (digestSize + hashIdentifier.second + 10 > representativeBitLength/8) 00101 throw PK_Signer::KeyTooShort(); 00102 00103 unsigned int pkcsBlockLen = representativeBitLength; 00104 // convert from bit length to byte length 00105 if (pkcsBlockLen % 8 != 0) 00106 { 00107 representative[0] = 0; 00108 representative++; 00109 } 00110 pkcsBlockLen /= 8; 00111 00112 representative[0] = 1; // block type 1 00113 00114 byte *pPadding = representative + 1; 00115 byte *pDigest = representative + pkcsBlockLen - digestSize; 00116 byte *pHashId = pDigest - hashIdentifier.second; 00117 byte *pSeparator = pHashId - 1; 00118 00119 // pad with 0xff 00120 memset(pPadding, 0xff, pSeparator-pPadding); 00121 *pSeparator = 0; 00122 memcpy(pHashId, hashIdentifier.first, hashIdentifier.second); 00123 hash.Final(pDigest); 00124 } 00125 00126 #endif 00127 00128 NAMESPACE_END

Generated on Fri Aug 27 16:09:27 2004 for Crypto++ by doxygen 1.3.8