22 #ifndef TAO_CRYPT_RSA_HPP
23 #define TAO_CRYPT_RSA_HPP
25 #include "integer.hpp"
40 word32 PaddedBlockBitLength()
const {
return image_.BitCount() - 1;}
41 word32 PaddedBlockByteLength()
const
42 {
return BitsToBytes(PaddedBlockBitLength());}
44 word32 FixedCiphertextLength()
const {
return image_.ByteCount();}
45 word32 FixedMaxPlaintextLength()
const
46 {
return SaturatingSubtract(PaddedBlockBitLength() / 8, 10
U); }
64 const Integer& GetModulus()
const {
return n_;}
65 const Integer& GetPublicExponent()
const {
return e_;}
67 void SetModulus(
const Integer& n) {n_ =
n;}
68 void SetPublicExponent(
const Integer& e) {e_ = e;}
70 word32 FixedCiphertextLength()
106 {n_ =
n; e_ = e; d_ = d; p_ = p; q_ = q; dp_ = dp; dq_ = dq; u_ = u;}
111 const Integer& GetPrime1()
const {
return p_;}
112 const Integer& GetPrime2()
const {
return q_;}
113 const Integer& GetPrivateExponent()
const {
return d_;}
114 const Integer& GetModPrime1PrivateExponent()
const {
return dp_;}
115 const Integer& GetModPrime2PrivateExponent()
const {
return dq_;}
116 const Integer& GetMultiplicativeInverseOfPrime2ModPrime1()
const
119 void SetPrime1(
const Integer& p) {p_ = p;}
120 void SetPrime2(
const Integer& q) {q_ = q;}
121 void SetPrivateExponent(
const Integer& d) {d_ = d;}
122 void SetModPrime1PrivateExponent(
const Integer& dp) {dp_ = dp;}
123 void SetModPrime2PrivateExponent(
const Integer& dq) {dq_ = dq;}
124 void SetMultiplicativeInverseOfPrime2ModPrime1(
const Integer& u) {u_ = u;}
134 void Pad(
const byte*, word32, byte*, word32,
136 word32 UnPad(
const byte*, word32, byte*)
const;
143 void Pad(
const byte*, word32, byte*, word32,
145 word32 UnPad(
const byte*, word32, byte*)
const;
150 template<
class Pad = RSA_BlockType2>
158 bool SSL_Verify(
const byte*
msg, word32 sz,
const byte* sig);
163 template<
class Pad = RSA_BlockType2>
181 if (sz > lengths.FixedMaxPlaintextLength())
184 ByteBlock paddedBlock(lengths.PaddedBlockByteLength());
185 padding_.Pad(plain, sz, paddedBlock.get_buffer(),
186 lengths.PaddedBlockBitLength(), rng);
188 key_.ApplyFunction(
Integer(paddedBlock.get_buffer(), paddedBlock.size())).
189 Encode(cipher, lengths.FixedCiphertextLength());
195 word32 RSA_Decryptor<Pad>::Decrypt(
const byte* cipher, word32 sz, byte* plain,
196 RandomNumberGenerator& rng)
198 PK_Lengths lengths(key_.GetModulus());
200 if (sz != lengths.FixedCiphertextLength())
203 ByteBlock paddedBlock(lengths.PaddedBlockByteLength());
204 Integer x = key_.CalculateInverse(rng, Integer(cipher,
205 lengths.FixedCiphertextLength()).Ref());
206 if (x.ByteCount() > paddedBlock.size())
208 x.Encode(paddedBlock.get_buffer(), paddedBlock.size());
209 return padding_.UnPad(paddedBlock.get_buffer(),
210 lengths.PaddedBlockBitLength(), plain);
216 void RSA_Decryptor<Pad>::SSL_Sign(
const byte*
message, word32 sz, byte* sig,
217 RandomNumberGenerator& rng)
219 RSA_PublicKey inverse;
220 inverse.Initialize(key_.GetModulus(), key_.GetPrivateExponent());
221 RSA_Encryptor<RSA_BlockType1> enc(inverse);
222 enc.Encrypt(message, sz, sig, rng);
226 word32 SSL_Decrypt(
const RSA_PublicKey& key,
const byte* sig, byte* plain);
231 bool RSA_Encryptor<Pad>::SSL_Verify(
const byte* message, word32 sz,
234 ByteBlock plain(PK_Lengths(key_.GetModulus()).FixedMaxPlaintextLength());
235 if (SSL_Decrypt(key_, sig, plain.get_buffer()) != sz)
238 if ( (memcmp(plain.get_buffer(), message, sz)) == 0)
244 typedef RSA_Encryptor<> RSAES_Encryptor;
245 typedef RSA_Decryptor<> RSAES_Decryptor;
250 #endif // TAO_CRYPT_RSA_HPP