Crypto++ 8.2 Free C++ class library of cryptographic schemes

emsa2.cpp
1 // emsa2.cpp - originally 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 // Inclusion based on DLL due to Clang, http://github.com/weidai11/cryptopp/issues/300
11 #ifndef CRYPTOPP_IS_DLL
12 template<> const byte EMSA2HashId<SHA1>::id = 0x33;
13 template<> const byte EMSA2HashId<SHA224>::id = 0x38;
14 template<> const byte EMSA2HashId<SHA256>::id = 0x34;
15 template<> const byte EMSA2HashId<SHA384>::id = 0x36;
16 template<> const byte EMSA2HashId<SHA512>::id = 0x35;
17 #endif
18
19 void EMSA2Pad::ComputeMessageRepresentative(RandomNumberGenerator& /*rng*/,
20 const byte* recoverableMessage, size_t recoverableMessageLength,
21 HashTransformation &hash, HashIdentifier hashIdentifier, bool messageEmpty,
22 byte *representative, size_t representativeBitLength) const
23 {
24 CRYPTOPP_UNUSED(recoverableMessage), CRYPTOPP_UNUSED(recoverableMessageLength), CRYPTOPP_UNUSED(representativeBitLength);
25 CRYPTOPP_ASSERT(representativeBitLength >= MinRepresentativeBitLength(hashIdentifier.second, hash.DigestSize()));
26
27 if (representativeBitLength % 8 != 7)
28 throw PK_SignatureScheme::InvalidKeyLength("EMSA2: EMSA2 requires a key length that is a multiple of 8");
29
30 size_t digestSize = hash.DigestSize();
31 size_t representativeByteLength = BitsToBytes(representativeBitLength);
32
33 representative[0] = messageEmpty ? 0x4b : 0x6b;
34 memset(representative+1, 0xbb, representativeByteLength-digestSize-4); // pad with 0xbb
35 byte *afterP2 = representative+representativeByteLength-digestSize-3;
36 afterP2[0] = 0xba;
37 hash.Final(afterP2+1);
38 representative[representativeByteLength-2] = *hashIdentifier.first;
39 representative[representativeByteLength-1] = 0xcc;
40 }
41
42 NAMESPACE_END
43
44 #endif
emsa2.h
Classes and functions for various padding schemes used in public key algorithms.
BitsToBytes
size_t BitsToBytes(size_t bitCount)
Returns the number of 8-bit bytes or octets required for the specified number of bits.
Definition: misc.h:918
RandomNumberGenerator
Interface for random number generators.
Definition: cryptlib.h:1413
EMSA2HashId
EMSA2 hash identifier.
Definition: emsa2.h:23
pch.h
Precompiled header file.
PK_SignatureScheme::InvalidKeyLength
Exception throw when the private or public key has a length that can&#39;t be used.
Definition: cryptlib.h:2769
CRYPTOPP_ASSERT
#define CRYPTOPP_ASSERT(exp)
Debugging and diagnostic assertion.
Definition: trap.h:68
HashTransformation::DigestSize
virtual unsigned int DigestSize() const =0
Provides the digest size of the hash.
HashTransformation
Interface for hash functions and data processing part of MACs.
Definition: cryptlib.h:1112
CryptoPP
Crypto++ library namespace.
HashTransformation::Final
virtual void Final(byte *digest)
Computes the hash of the current message.
Definition: cryptlib.h:1142