Fabcoin Core  0.16.2
P2P Digital Currency
oaep.cpp
Go to the documentation of this file.
1 // oaep.cpp - written and placed in the public domain by Wei Dai
2 
3 #include "pch.h"
4 
5 #ifndef CRYPTOPP_IMPORTS
6 
7 #include "oaep.h"
8 #include "stdcpp.h"
9 #include "smartptr.h"
10 
12 
13 // ********************************************************
14 
15 size_t OAEP_Base::MaxUnpaddedLength(size_t paddedLength) const
16 {
17  return SaturatingSubtract(paddedLength/8, 1+2*DigestSize());
18 }
19 
20 void OAEP_Base::Pad(RandomNumberGenerator &rng, const byte *input, size_t inputLength, byte *oaepBlock, size_t oaepBlockLen, const NameValuePairs &parameters) const
21 {
22  CRYPTOPP_ASSERT (inputLength <= MaxUnpaddedLength(oaepBlockLen));
23 
24  // convert from bit length to byte length
25  if (oaepBlockLen % 8 != 0)
26  {
27  oaepBlock[0] = 0;
28  oaepBlock++;
29  }
30  oaepBlockLen /= 8;
31 
33  const size_t hLen = pHash->DigestSize();
34  const size_t seedLen = hLen, dbLen = oaepBlockLen-seedLen;
35  byte *const maskedSeed = oaepBlock;
36  byte *const maskedDB = oaepBlock+seedLen;
37 
38  ConstByteArrayParameter encodingParameters;
39  parameters.GetValue(Name::EncodingParameters(), encodingParameters);
40 
41  // DB = pHash || 00 ... || 01 || M
42  pHash->CalculateDigest(maskedDB, encodingParameters.begin(), encodingParameters.size());
43  memset(maskedDB+hLen, 0, dbLen-hLen-inputLength-1);
44  maskedDB[dbLen-inputLength-1] = 0x01;
45  memcpy(maskedDB+dbLen-inputLength, input, inputLength);
46 
47  rng.GenerateBlock(maskedSeed, seedLen);
49  pMGF->GenerateAndMask(*pHash, maskedDB, dbLen, maskedSeed, seedLen);
50  pMGF->GenerateAndMask(*pHash, maskedSeed, seedLen, maskedDB, dbLen);
51 }
52 
53 DecodingResult OAEP_Base::Unpad(const byte *oaepBlock, size_t oaepBlockLen, byte *output, const NameValuePairs &parameters) const
54 {
55  bool invalid = false;
56 
57  // convert from bit length to byte length
58  if (oaepBlockLen % 8 != 0)
59  {
60  invalid = (oaepBlock[0] != 0) || invalid;
61  oaepBlock++;
62  }
63  oaepBlockLen /= 8;
64 
66  const size_t hLen = pHash->DigestSize();
67  const size_t seedLen = hLen, dbLen = oaepBlockLen-seedLen;
68 
69  invalid = (oaepBlockLen < 2*hLen+1) || invalid;
70 
71  SecByteBlock t(oaepBlock, oaepBlockLen);
72  byte *const maskedSeed = t;
73  byte *const maskedDB = t+seedLen;
74 
76  pMGF->GenerateAndMask(*pHash, maskedSeed, seedLen, maskedDB, dbLen);
77  pMGF->GenerateAndMask(*pHash, maskedDB, dbLen, maskedSeed, seedLen);
78 
79  ConstByteArrayParameter encodingParameters;
80  parameters.GetValue(Name::EncodingParameters(), encodingParameters);
81 
82  // DB = pHash' || 00 ... || 01 || M
83  byte *M = std::find(maskedDB+hLen, maskedDB+dbLen, 0x01);
84  invalid = (M == maskedDB+dbLen) || invalid;
85  invalid = (std::find_if(maskedDB+hLen, M, std::bind2nd(std::not_equal_to<byte>(), byte(0))) != M) || invalid;
86  invalid = !pHash->VerifyDigest(maskedDB, encodingParameters.begin(), encodingParameters.size()) || invalid;
87 
88  if (invalid)
89  return DecodingResult();
90 
91  M++;
92  memcpy(output, M, maskedDB+dbLen-M);
93  return DecodingResult(maskedDB+dbLen-M);
94 }
95 
97 
98 #endif
Used to pass byte array input as part of a NameValuePairs object.
Definition: algparam.h:29
OAEP padding base class.
Definition: oaep.h:11
uint8_t byte
Definition: Common.h:57
void Pad(RandomNumberGenerator &rng, const byte *raw, size_t inputLength, byte *padded, size_t paddedLength, const NameValuePairs &parameters) const
Definition: oaep.cpp:20
virtual void GenerateBlock(byte *output, size_t size)
Generate random array of bytes.
Definition: cryptlib.cpp:326
#define NAMESPACE_BEGIN(x)
Definition: config.h:200
Classes for automatic resource management.
size_t size() const
Length of the memory block.
Definition: algparam.h:93
Interface for random number generators.
Definition: cryptlib.h:1188
DecodingResult Unpad(const byte *padded, size_t paddedLength, byte *raw, const NameValuePairs &parameters) const
Definition: oaep.cpp:53
const byte * begin() const
Pointer to the first byte in the memory block.
Definition: algparam.h:89
Pointer that overloads operator ->
Definition: smartptr.h:39
Returns a decoding results.
Definition: cryptlib.h:238
bool GetValue(const char *name, T &value) const
Get a named value.
Definition: cryptlib.h:337
virtual void GenerateAndMask(HashTransformation &hash, byte *output, size_t outputLength, const byte *input, size_t inputLength, bool mask=true) const =0
Generate and apply mask.
size_t MaxUnpaddedLength(size_t paddedLength) const
max size of unpadded message in bytes, given max size of padded message in bits (1 less than size of ...
Definition: oaep.cpp:15
T1 SaturatingSubtract(const T1 &a, const T2 &b)
Performs a saturating subtract clamped at 0.
Definition: misc.h:847
#define CRYPTOPP_ASSERT(exp)
Definition: trap.h:92
virtual void CalculateDigest(byte *digest, const byte *input, size_t length)
Updates the hash with additional input and computes the hash of the current message.
Definition: cryptlib.h:1003
virtual unsigned int DigestSize() const =0
Provides the digest size of the hash.
virtual HashTransformation * NewHash() const =0
void * memcpy(void *a, const void *b, size_t c)
uint8_t byte
Definition: Common.h:10
#define NAMESPACE_END
Definition: config.h:201
std::vector< char * > parameters
Definition: boostTest.cpp:46
virtual MaskGeneratingFunction * NewMGF() const =0
virtual bool VerifyDigest(const byte *digest, const byte *input, size_t length)
Updates the hash with additional input and verifies the hash of the current message.
Definition: cryptlib.h:1031
Interface for retrieving values given their names.
Definition: cryptlib.h:279