00001
00002
00003
#include "pch.h"
00004
#include "pkcspad.h"
00005
#include <assert.h>
00006
00007 NAMESPACE_BEGIN(CryptoPP)
00008
00009 template<> const byte PKCS_DigestDecoration<
SHA>::decoration[] = {0x30,0x21,0x30,0x09,0x06,0x05,0x2B,0x0E,0x03,0x02,0x1A,0x05,0x00,0x04,0x14};
00010
template<>
const unsigned int PKCS_DigestDecoration<SHA>::length =
sizeof(PKCS_DigestDecoration<SHA>::decoration);
00011
00012
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};
00013
template<>
const unsigned int PKCS_DigestDecoration<MD2>::length =
sizeof(PKCS_DigestDecoration<MD2>::decoration);
00014
00015
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};
00016
template<>
const unsigned int PKCS_DigestDecoration<MD5>::length =
sizeof(PKCS_DigestDecoration<MD5>::decoration);
00017
00018
template<>
const byte PKCS_DigestDecoration<RIPEMD160>::decoration[] = {0x30,0x21,0x30,0x09,0x06,0x05,0x2b,0x24,0x03,0x02,0x01,0x05,0x00,0x04,0x14};
00019
template<>
const unsigned int PKCS_DigestDecoration<RIPEMD160>::length =
sizeof(PKCS_DigestDecoration<RIPEMD160>::decoration);
00020
00021
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};
00022
template<>
const unsigned int PKCS_DigestDecoration<Tiger>::length =
sizeof(PKCS_DigestDecoration<Tiger>::decoration);
00023
00024
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};
00025
template<>
const unsigned int PKCS_DigestDecoration<SHA256>::length =
sizeof(PKCS_DigestDecoration<SHA256>::decoration);
00026
00027
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};
00028
template<>
const unsigned int PKCS_DigestDecoration<SHA384>::length =
sizeof(PKCS_DigestDecoration<SHA384>::decoration);
00029
00030
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};
00031
template<>
const unsigned int PKCS_DigestDecoration<SHA512>::length =
sizeof(PKCS_DigestDecoration<SHA512>::decoration);
00032
00033 unsigned int PKCS_EncryptionPaddingScheme::MaxUnpaddedLength(
unsigned int paddedLength)
const
00034
{
00035
return SaturatingSubtract(paddedLength/8, 10U);
00036 }
00037
00038
void PKCS_EncryptionPaddingScheme::Pad(
RandomNumberGenerator &rng,
const byte *input,
unsigned int inputLen, byte *pkcsBlock,
unsigned int pkcsBlockLen)
const
00039
{
00040 assert (inputLen <= MaxUnpaddedLength(pkcsBlockLen));
00041
00042
00043
if (pkcsBlockLen % 8 != 0)
00044 {
00045 pkcsBlock[0] = 0;
00046 pkcsBlock++;
00047 }
00048 pkcsBlockLen /= 8;
00049
00050 pkcsBlock[0] = 2;
00051
00052
00053
for (
unsigned i = 1; i < pkcsBlockLen-inputLen-1; i++)
00054 pkcsBlock[i] = (byte)rng.
GenerateWord32(1, 0xff);
00055
00056 pkcsBlock[pkcsBlockLen-inputLen-1] = 0;
00057 memcpy(pkcsBlock+pkcsBlockLen-inputLen, input, inputLen);
00058 }
00059
00060
DecodingResult PKCS_EncryptionPaddingScheme::Unpad(
const byte *pkcsBlock,
unsigned int pkcsBlockLen, byte *output)
const
00061
{
00062
bool invalid =
false;
00063
unsigned int maxOutputLen =
MaxUnpaddedLength(pkcsBlockLen);
00064
00065
00066
if (pkcsBlockLen % 8 != 0)
00067 {
00068 invalid = (pkcsBlock[0] != 0) || invalid;
00069 pkcsBlock++;
00070 }
00071 pkcsBlockLen /= 8;
00072
00073
00074 invalid = (pkcsBlock[0] != 2) || invalid;
00075
00076
00077
unsigned i=1;
00078
while (i<pkcsBlockLen && pkcsBlock[i++]) {
00079 }
00080 assert(i==pkcsBlockLen || pkcsBlock[i-1]==0);
00081
00082
unsigned int outputLen = pkcsBlockLen - i;
00083 invalid = (outputLen > maxOutputLen) || invalid;
00084
00085
if (invalid)
00086
return DecodingResult();
00087
00088 memcpy (output, pkcsBlock+i, outputLen);
00089
return DecodingResult(outputLen);
00090 }
00091
00092
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
00105
if (pkcsBlockLen % 8 != 0)
00106 {
00107 representative[0] = 0;
00108 representative++;
00109 }
00110 pkcsBlockLen /= 8;
00111
00112 representative[0] = 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
00120 memset(pPadding, 0xff, pSeparator-pPadding);
00121 *pSeparator = 0;
00122 memcpy(pHashId, hashIdentifier.first, hashIdentifier.second);
00123 hash.
Final(pDigest);
00124 }
00125
00126 NAMESPACE_END