Crypto++
emsa2.cpp
1 // emsa2.cpp - written and placed in the public domain by Wei Dai
2 
3 #include "pch.h"
4 #include "emsa2.h"
5 
6 #ifndef CRYPTOPP_IMPORTS
7 
8 NAMESPACE_BEGIN(CryptoPP)
9 
10 void EMSA2Pad::ComputeMessageRepresentative(RandomNumberGenerator &rng,
11  const byte *recoverableMessage, size_t recoverableMessageLength,
12  HashTransformation &hash, HashIdentifier hashIdentifier, bool messageEmpty,
13  byte *representative, size_t representativeBitLength) const
14 {
15  assert(representativeBitLength >= MinRepresentativeBitLength(hashIdentifier.second, hash.DigestSize()));
16 
17  if (representativeBitLength % 8 != 7)
18  throw PK_SignatureScheme::InvalidKeyLength("EMSA2: EMSA2 requires a key length that is a multiple of 8");
19 
20  size_t digestSize = hash.DigestSize();
21  size_t representativeByteLength = BitsToBytes(representativeBitLength);
22 
23  representative[0] = messageEmpty ? 0x4b : 0x6b;
24  memset(representative+1, 0xbb, representativeByteLength-digestSize-4); // pad with 0xbb
25  byte *afterP2 = representative+representativeByteLength-digestSize-3;
26  afterP2[0] = 0xba;
27  hash.Final(afterP2+1);
28  representative[representativeByteLength-2] = *hashIdentifier.first;
29  representative[representativeByteLength-1] = 0xcc;
30 }
31 
32 NAMESPACE_END
33 
34 #endif