Main Page | Namespace List | Class Hierarchy | Alphabetical List | Compound List | File List | Namespace Members | Compound 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<SHA256>::decoration[] = {0x30,0x31,0x30,0x0d,0x06,0x09,0x60,0x86,0x48,0x01,0x65,0x03,0x04,0x02,0x01,0x05,0x00,0x04,0x20};
00020 template<> const unsigned int PKCS_DigestDecoration<SHA256>::length = sizeof(PKCS_DigestDecoration<SHA256>::decoration);
00021 
00022 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};
00023 template<> const unsigned int PKCS_DigestDecoration<SHA384>::length = sizeof(PKCS_DigestDecoration<SHA384>::decoration);
00024 
00025 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};
00026 template<> const unsigned int PKCS_DigestDecoration<SHA512>::length = sizeof(PKCS_DigestDecoration<SHA512>::decoration);
00027 
00028 unsigned int PKCS_EncryptionPaddingScheme::MaxUnpaddedLength(unsigned int paddedLength) const
00029 {
00030         return paddedLength/8 > 10 ? paddedLength/8-10 : 0;
00031 }
00032 
00033 void PKCS_EncryptionPaddingScheme::Pad(RandomNumberGenerator &rng, const byte *input, unsigned int inputLen, byte *pkcsBlock, unsigned int pkcsBlockLen) const
00034 {
00035         assert (inputLen <= MaxUnpaddedLength(pkcsBlockLen));   // this should be checked by caller
00036 
00037         // convert from bit length to byte length
00038         if (pkcsBlockLen % 8 != 0)
00039         {
00040                 pkcsBlock[0] = 0;
00041                 pkcsBlock++;
00042         }
00043         pkcsBlockLen /= 8;
00044 
00045         pkcsBlock[0] = 2;  // block type 2
00046 
00047         // pad with non-zero random bytes
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;     // separator
00052         memcpy(pkcsBlock+pkcsBlockLen-inputLen, input, inputLen);
00053 }
00054 
00055 DecodingResult PKCS_EncryptionPaddingScheme::Unpad(const byte *pkcsBlock, unsigned int pkcsBlockLen, byte *output) const
00056 {
00057         bool invalid = false;
00058         unsigned int maxOutputLen = MaxUnpaddedLength(pkcsBlockLen);
00059 
00060         // convert from bit length to byte length
00061         if (pkcsBlockLen % 8 != 0)
00062         {
00063                 invalid = (pkcsBlock[0] != 0) || invalid;
00064                 pkcsBlock++;
00065         }
00066         pkcsBlockLen /= 8;
00067 
00068         // Require block type 2.
00069         invalid = (pkcsBlock[0] != 2) || invalid;
00070 
00071         // skip past the padding until we find the seperator
00072         unsigned i=1;
00073         while (i<pkcsBlockLen && pkcsBlock[i++]) { // null body
00074                 }
00075         assert(i==pkcsBlockLen || pkcsBlock[i-1]==0);
00076 
00077         unsigned int outputLen = pkcsBlockLen - i;
00078         invalid = (outputLen > maxOutputLen) || invalid;
00079 
00080         if (invalid)
00081                 return DecodingResult();
00082 
00083         memcpy (output, pkcsBlock+i, outputLen);
00084         return DecodingResult(outputLen);
00085 }
00086 
00087 // ********************************************************
00088 
00089 #ifndef CRYPTOPP_IMPORTS
00090 
00091 unsigned int PKCS_SignaturePaddingScheme::MaxUnpaddedLength(unsigned int paddedLength) const
00092 {
00093         return paddedLength/8 > 10 ? paddedLength/8-10 : 0;
00094 }
00095 
00096 void PKCS_SignaturePaddingScheme::Pad(RandomNumberGenerator &, const byte *input, unsigned int inputLen, byte *pkcsBlock, unsigned int pkcsBlockLen) const
00097 {
00098         assert (inputLen <= MaxUnpaddedLength(pkcsBlockLen));   // this should be checked by caller
00099 
00100         // convert from bit length to byte length
00101         if (pkcsBlockLen % 8 != 0)
00102         {
00103                 pkcsBlock[0] = 0;
00104                 pkcsBlock++;
00105         }
00106         pkcsBlockLen /= 8;
00107 
00108         pkcsBlock[0] = 1;   // block type 1
00109 
00110         // padd with 0xff
00111         memset(pkcsBlock+1, 0xff, pkcsBlockLen-inputLen-2);
00112 
00113         pkcsBlock[pkcsBlockLen-inputLen-1] = 0;               // separator
00114         memcpy(pkcsBlock+pkcsBlockLen-inputLen, input, inputLen);
00115 }
00116 
00117 DecodingResult PKCS_SignaturePaddingScheme::Unpad(const byte *pkcsBlock, unsigned int pkcsBlockLen, byte *output) const
00118 {
00119         unsigned int maxOutputLen = MaxUnpaddedLength(pkcsBlockLen);
00120 
00121         // convert from bit length to byte length
00122         if (pkcsBlockLen % 8 != 0)
00123         {
00124                 if (pkcsBlock[0] != 0)
00125                         return DecodingResult();
00126                 pkcsBlock++;
00127         }
00128         pkcsBlockLen /= 8;
00129 
00130         // Require block type 1.
00131         if (pkcsBlock[0] != 1)
00132                 return DecodingResult();
00133 
00134         // skip past the padding until we find the seperator
00135         unsigned i=1;
00136         while (i<pkcsBlockLen && pkcsBlock[i++])
00137                 if (pkcsBlock[i-1] != 0xff)     // not valid padding
00138                         return DecodingResult();
00139         assert(i==pkcsBlockLen || pkcsBlock[i-1]==0);
00140 
00141         unsigned int outputLen = pkcsBlockLen - i;
00142         if (outputLen > maxOutputLen)
00143                 return DecodingResult();
00144 
00145         memcpy (output, pkcsBlock+i, outputLen);
00146         return DecodingResult(outputLen);
00147 }
00148 
00149 #endif
00150 
00151 NAMESPACE_END

Generated on Tue Jul 8 23:34:21 2003 for Crypto++ by doxygen 1.3.2