Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Скачиваний:
29
Добавлен:
16.04.2013
Размер:
3.67 Кб
Скачать
#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
  • #
    16.04.2013204 б27rsa400pb.dat
  • #
    16.04.2013892 б27rsa400pv.dat
  • #
    16.04.2013632 б27rsa512.dat
  • #
    16.04.20131.38 Кб27rsa512a.dat
  • #
    16.04.20135.24 Кб29rw.cpp
  • #
    16.04.20133.67 Кб29rw.h
  • #
    16.04.2013674 б27rw1024.dat
  • #
    16.04.20131.32 Кб27rw2048.dat
  • #
    16.04.2013348 б27rw512.dat
  • #
    16.04.20136.37 Кб29safer.cpp
  • #
    16.04.20133.9 Кб29safer.h