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

Generated on Tue Aug 16 08:38:42 2005 for Crypto++ by  doxygen 1.3.9.1