Добавил:
korayakov
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз:
Предмет:
Файл:Документация по криптоалгоритмам / CRYPTO30 / rw
.h#ifndef CRYPTOPP_RW_H
#define CRYPTOPP_RW_H
#include "pubkey.h"
#include "integer.h"
NAMESPACE_BEGIN(CryptoPP)
const word IFSSR_R = 6;
const word IFSSA_R = 12;
class EMSA2Pad : public PaddingScheme
{
public:
unsigned int MaxUnpaddedLength(unsigned int paddedLength) const {return (paddedLength+1)/8-2;}
void Pad(RandomNumberGenerator &rng, const byte *raw, unsigned int inputLength, byte *padded, unsigned int paddedLength) const;
unsigned int Unpad(const byte *padded, unsigned int paddedLength, byte *raw) const;
};
template <class H>
class EMSA2DecoratedHashModule : public HashModule
{
public:
EMSA2DecoratedHashModule() : empty(true) {}
void Update(const byte *input, unsigned int length)
{h.Update(input, length); empty = empty && length==0;}
unsigned int DigestSize() const;
void Final(byte *digest);
private:
H h;
bool empty;
};
template <class H> struct EMSA2DigestDecoration
{
static const byte decoration;
};
// EMSA2DecoratedHashModule can be instantiated with the following two classes.
class SHA;
class RIPEMD160;
template <class H>
void EMSA2DecoratedHashModule<H>::Final(byte *digest)
{
digest[0] = empty ? 0x4b : 0x6b;
h.Final(digest+1);
digest[DigestSize()-1] = EMSA2DigestDecoration<H>::decoration;
}
template <class H>
unsigned int EMSA2DecoratedHashModule<H>::DigestSize() const
{
return h.DigestSize() + 2;
}
// *****************************************************************************
template <word r>
class RWFunction : virtual public TrapdoorFunction
{
public:
RWFunction(const Integer &n);
RWFunction(BufferedTransformation &bt);
void DEREncode(BufferedTransformation &bt) const;
~RWFunction();
Integer ApplyFunction(const Integer &x) const;
Integer MaxPreimage() const {return n-1;}
Integer MaxImage() const {return n>>1;}
protected:
RWFunction() {} // to be used only by InvertableRWFunction
Integer n;
};
template <word r>
class InvertableRWFunction : public RWFunction<r>, public InvertableTrapdoorFunction
{
public:
InvertableRWFunction(const Integer &n, const Integer &p, const Integer &q, const Integer &u);
// generate a random private key
InvertableRWFunction(RandomNumberGenerator &rng, unsigned int keybits);
InvertableRWFunction(BufferedTransformation &bt);
~InvertableRWFunction();
void DEREncode(BufferedTransformation &bt) const;
Integer CalculateInverse(const Integer &x) const;
protected:
Integer p, q, u;
};
template <class H>
class RWSigner : public SignerTemplate<DigestSignerTemplate<EMSA2Pad, InvertableRWFunction<IFSSA_R> >, EMSA2DecoratedHashModule<H> >
{
public:
RWSigner(const Integer &n, const Integer &p, const Integer &q, const Integer &u)
: PublicKeyBaseTemplate<InvertableRWFunction<IFSSA_R> >(
InvertableRWFunction<IFSSA_R>(n, p, q, u)) {}
RWSigner(RandomNumberGenerator &rng, unsigned int keybits)
: PublicKeyBaseTemplate<InvertableRWFunction<IFSSA_R> >(
InvertableRWFunction<IFSSA_R>(rng, keybits)) {}
RWSigner(BufferedTransformation &bt)
: PublicKeyBaseTemplate<InvertableRWFunction<IFSSA_R> >(bt) {}
};
template <class H>
class RWVerifier : public VerifierTemplate<DigestVerifierTemplate<EMSA2Pad, RWFunction<IFSSA_R> >, EMSA2DecoratedHashModule<H> >
{
public:
RWVerifier(const Integer &n)
: PublicKeyBaseTemplate<RWFunction<IFSSA_R> >(RWFunction<IFSSA_R>(n)) {}
RWVerifier(const RWSigner<H> &priv)
: PublicKeyBaseTemplate<RWFunction<IFSSA_R> >(priv.GetTrapdoorFunction()) {}
RWVerifier(BufferedTransformation &bt)
: PublicKeyBaseTemplate<RWFunction<IFSSA_R> >(bt) {}
};
NAMESPACE_END
#endif
Соседние файлы в папке CRYPTO30