diff options
Diffstat (limited to 'extra/yassl/taocrypt/include')
-rw-r--r-- | extra/yassl/taocrypt/include/aes.hpp | 5 | ||||
-rw-r--r-- | extra/yassl/taocrypt/include/algebra.hpp | 126 | ||||
-rw-r--r-- | extra/yassl/taocrypt/include/block.hpp | 20 | ||||
-rw-r--r-- | extra/yassl/taocrypt/include/des.hpp | 5 | ||||
-rw-r--r-- | extra/yassl/taocrypt/include/error.hpp | 3 | ||||
-rw-r--r-- | extra/yassl/taocrypt/include/hash.hpp | 2 | ||||
-rw-r--r-- | extra/yassl/taocrypt/include/integer.hpp | 10 | ||||
-rw-r--r-- | extra/yassl/taocrypt/include/misc.hpp | 11 | ||||
-rw-r--r-- | extra/yassl/taocrypt/include/modarith.hpp | 13 | ||||
-rw-r--r-- | extra/yassl/taocrypt/include/modes.hpp | 55 |
10 files changed, 72 insertions, 178 deletions
diff --git a/extra/yassl/taocrypt/include/aes.hpp b/extra/yassl/taocrypt/include/aes.hpp index b2c93eff9fe..b8436d35c5f 100644 --- a/extra/yassl/taocrypt/include/aes.hpp +++ b/extra/yassl/taocrypt/include/aes.hpp @@ -37,11 +37,12 @@ enum { AES_BLOCK_SIZE = 16 }; // AES encryption and decryption, see FIPS-197 -class AES : public Mode_BASE<AES_BLOCK_SIZE> { +class AES : public Mode_BASE { public: enum { BLOCK_SIZE = AES_BLOCK_SIZE }; - AES(CipherDir DIR, Mode MODE) : dir_(DIR), mode_(MODE) {} + AES(CipherDir DIR, Mode MODE) + : Mode_BASE(BLOCK_SIZE), dir_(DIR), mode_(MODE) {} void Process(byte*, const byte*, word32); void SetKey(const byte* iv, word32 sz, CipherDir fake = ENCRYPTION); diff --git a/extra/yassl/taocrypt/include/algebra.hpp b/extra/yassl/taocrypt/include/algebra.hpp index 74f244507f6..92cac607d97 100644 --- a/extra/yassl/taocrypt/include/algebra.hpp +++ b/extra/yassl/taocrypt/include/algebra.hpp @@ -24,11 +24,10 @@ #ifndef TAO_CRYPT_ALGEBRA_HPP #define TAO_CRYPT_ALGEBRA_HPP -#include "misc.hpp" +#include "integer.hpp" namespace TaoCrypt { -class Integer; // "const Element&" returned by member functions are references // to internal data members. Since each object may have only @@ -38,11 +37,11 @@ class Integer; // But this should be fine: // abcd = group.Add(a, group.Add(b, group.Add(c,d)); -//! Abstract Group -template <class T> class TAOCRYPT_NO_VTABLE AbstractGroup +// Abstract Group +class TAOCRYPT_NO_VTABLE AbstractGroup { public: - typedef T Element; + typedef Integer Element; virtual ~AbstractGroup() {} @@ -65,15 +64,14 @@ public: const Integer *exponents, unsigned int exponentsCount) const; }; -//! Abstract Ring -template <class T> class TAOCRYPT_NO_VTABLE AbstractRing - : public AbstractGroup<T> +// Abstract Ring +class TAOCRYPT_NO_VTABLE AbstractRing : public AbstractGroup { public: - typedef T Element; + typedef Integer Element; AbstractRing() {m_mg.m_pRing = this;} - AbstractRing(const AbstractRing &source) {m_mg.m_pRing = this;} + AbstractRing(const AbstractRing &source) : AbstractGroup() {m_mg.m_pRing = this;} AbstractRing& operator=(const AbstractRing &source) {return *this;} virtual bool IsUnit(const Element &a) const =0; @@ -91,14 +89,14 @@ public: virtual void SimultaneousExponentiate(Element *results, const Element&, const Integer *exponents, unsigned int exponentsCount) const; - virtual const AbstractGroup<T>& MultiplicativeGroup() const + virtual const AbstractGroup& MultiplicativeGroup() const {return m_mg;} private: - class MultiplicativeGroupT : public AbstractGroup<T> + class MultiplicativeGroupT : public AbstractGroup { public: - const AbstractRing<T>& GetRing() const + const AbstractRing& GetRing() const {return *m_pRing;} bool Equal(const Element &a, const Element &b) const @@ -137,44 +135,19 @@ private: {GetRing().SimultaneousExponentiate(results, base, exponents, exponentsCount);} - const AbstractRing<T> *m_pRing; + const AbstractRing* m_pRing; }; MultiplicativeGroupT m_mg; }; -// ******************************************************** -//! Base and Exponent -template <class T, class E = Integer> -struct BaseAndExponent +// Abstract Euclidean Domain +class TAOCRYPT_NO_VTABLE AbstractEuclideanDomain + : public AbstractRing { public: - BaseAndExponent() {} - BaseAndExponent(const T &base, const E &exponent) : base(base), - exponent(exponent) {} - bool operator<(const BaseAndExponent<T, E> &rhs) const - {return exponent < rhs.exponent;} - T base; - E exponent; -}; - -// VC60 workaround: incomplete member template support -template <class Element, class Iterator> - Element GeneralCascadeMultiplication(const AbstractGroup<Element> &group, - Iterator begin, Iterator end); -template <class Element, class Iterator> - Element GeneralCascadeExponentiation(const AbstractRing<Element> &ring, - Iterator begin, Iterator end); - -// ******************************************************** - -//! Abstract Euclidean Domain -template <class T> class TAOCRYPT_NO_VTABLE AbstractEuclideanDomain - : public AbstractRing<T> -{ -public: - typedef T Element; + typedef Integer Element; virtual void DivisionAlgorithm(Element &r, Element &q, const Element &a, const Element &d) const =0; @@ -186,13 +159,12 @@ protected: mutable Element result; }; -// ******************************************************** -//! EuclideanDomainOf -template <class T> class EuclideanDomainOf : public AbstractEuclideanDomain<T> +// EuclideanDomainOf +class EuclideanDomainOf : public AbstractEuclideanDomain { public: - typedef T Element; + typedef Integer Element; EuclideanDomainOf() {} @@ -249,68 +221,8 @@ private: mutable Element result; }; -//! Quotient Ring -template<class T> class QuotientRing : public AbstractRing<typename T::Element> -{ -public: - typedef T EuclideanDomain; - typedef typename T::Element Element; - - QuotientRing(const EuclideanDomain &domain, const Element &modulus) - : m_domain(domain), m_modulus(modulus) {} - - const EuclideanDomain & GetDomain() const - {return m_domain;} - - const Element& GetModulus() const - {return m_modulus;} - - bool Equal(const Element &a, const Element &b) const - {return m_domain.Equal(m_domain.Mod(m_domain.Subtract(a, b), - m_modulus), m_domain.Identity());} - - const Element& Identity() const - {return m_domain.Identity();} - - const Element& Add(const Element &a, const Element &b) const - {return m_domain.Add(a, b);} - - Element& Accumulate(Element &a, const Element &b) const - {return m_domain.Accumulate(a, b);} - - const Element& Inverse(const Element &a) const - {return m_domain.Inverse(a);} - - const Element& Subtract(const Element &a, const Element &b) const - {return m_domain.Subtract(a, b);} - - Element& Reduce(Element &a, const Element &b) const - {return m_domain.Reduce(a, b);} - - const Element& Double(const Element &a) const - {return m_domain.Double(a);} - - bool IsUnit(const Element &a) const - {return m_domain.IsUnit(m_domain.Gcd(a, m_modulus));} - - const Element& MultiplicativeIdentity() const - {return m_domain.MultiplicativeIdentity();} - - const Element& Multiply(const Element &a, const Element &b) const - {return m_domain.Mod(m_domain.Multiply(a, b), m_modulus);} - - const Element& Square(const Element &a) const - {return m_domain.Mod(m_domain.Square(a), m_modulus);} - - const Element& MultiplicativeInverse(const Element &a) const; - -protected: - EuclideanDomain m_domain; - Element m_modulus; -}; } // namespace - #endif // TAO_CRYPT_ALGEBRA_HPP diff --git a/extra/yassl/taocrypt/include/block.hpp b/extra/yassl/taocrypt/include/block.hpp index f490fb0b6e7..f3c4415682d 100644 --- a/extra/yassl/taocrypt/include/block.hpp +++ b/extra/yassl/taocrypt/include/block.hpp @@ -34,10 +34,6 @@ #include <stddef.h> // ptrdiff_t -#if defined(_MSC_VER) && defined(_CRTAPI1) -#define TAOCRYPT_MSVCRT6 -#endif - namespace TaoCrypt { @@ -47,13 +43,13 @@ template<class T> class AllocatorBase { public: - typedef T value_type; - typedef size_t size_type; - typedef ptrdiff_t difference_type; - typedef T* pointer; - typedef const T* const_pointer; - typedef T& reference; - typedef const T& const_reference; + typedef T value_type; + typedef size_t size_type; + typedef ptrdiff_t difference_type; + typedef T* pointer; + typedef const T* const_pointer; + typedef T& reference; + typedef const T& const_reference; pointer address(reference r) const {return (&r);} const_pointer address(const_reference r) const {return (&r); } @@ -104,7 +100,7 @@ public: CheckSize(n); if (n == 0) return 0; - return new (tc) T[n]; + return new T[n]; } void deallocate(void* p, size_type n) diff --git a/extra/yassl/taocrypt/include/des.hpp b/extra/yassl/taocrypt/include/des.hpp index e8100b4e198..127b8ddc6d5 100644 --- a/extra/yassl/taocrypt/include/des.hpp +++ b/extra/yassl/taocrypt/include/des.hpp @@ -36,12 +36,13 @@ namespace TaoCrypt { enum { DES_BLOCK_SIZE = 8 }; // Base for all DES types -class DES_BASE : public Mode_BASE<DES_BLOCK_SIZE> { +class DES_BASE : public Mode_BASE { public: enum { BLOCK_SIZE = DES_BLOCK_SIZE, KEY_SIZE = 32, BOXES = 8, BOX_SIZE = 64 }; - DES_BASE(CipherDir DIR, Mode MODE) : dir_(DIR), mode_(MODE) {} + DES_BASE(CipherDir DIR, Mode MODE) + : Mode_BASE(BLOCK_SIZE), dir_(DIR), mode_(MODE) {} void Process(byte*, const byte*, word32); protected: diff --git a/extra/yassl/taocrypt/include/error.hpp b/extra/yassl/taocrypt/include/error.hpp index 6170d0349b5..55ab39313f5 100644 --- a/extra/yassl/taocrypt/include/error.hpp +++ b/extra/yassl/taocrypt/include/error.hpp @@ -65,7 +65,8 @@ UNKOWN_HASH_E = 1034, // "unknown hash OID" DSA_SZ_E = 1035, // "bad DSA r or s size" BEFORE_DATE_E = 1036, // "before date in the future" AFTER_DATE_E = 1037, // "after date in the past" -SIG_CONFIRM_E = 1038 // "bad signature confirmation" +SIG_CONFIRM_E = 1038, // "bad self signature confirmation" +SIG_OTHER_E = 1039 // "bad other signature confirmation" }; diff --git a/extra/yassl/taocrypt/include/hash.hpp b/extra/yassl/taocrypt/include/hash.hpp index 1703de23713..f01f343c2d1 100644 --- a/extra/yassl/taocrypt/include/hash.hpp +++ b/extra/yassl/taocrypt/include/hash.hpp @@ -50,7 +50,7 @@ public: class HASHwithTransform : public HASH { public: HASHwithTransform(word32 digSz, word32 buffSz) - : digest_(new (tc) word32[digSz]), buffer_(new (tc) byte[buffSz]) {} + : digest_(new word32[digSz]), buffer_(new byte[buffSz]) {} virtual ~HASHwithTransform() { delete[] buffer_; delete[] digest_; } virtual ByteOrder getByteOrder() const = 0; diff --git a/extra/yassl/taocrypt/include/integer.hpp b/extra/yassl/taocrypt/include/integer.hpp index 3713d09d9f9..6b1984e46ed 100644 --- a/extra/yassl/taocrypt/include/integer.hpp +++ b/extra/yassl/taocrypt/include/integer.hpp @@ -29,8 +29,8 @@ #include "block.hpp" #include "random.hpp" #include "file.hpp" -#include <string.h> #include "algorithm.hpp" // mySTL::swap +#include <string.h> #ifdef TAOCRYPT_X86ASM_AVAILABLE @@ -128,9 +128,6 @@ public: Integer(signed long value); Integer(Sign s, word highWord, word lowWord); - explicit Integer(const char* str); - explicit Integer(const wchar_t* str); - // BER Decode Source explicit Integer(Source&); @@ -254,15 +251,13 @@ public: private: friend class ModularArithmetic; friend class MontgomeryRepresentation; - friend class HalfMontgomeryRepresentation; - Integer(word value, unsigned int length); static const Integer zero; static const Integer one; static const Integer two; - int PositiveCompare(const Integer& t) const; + friend void PositiveAdd(Integer& sum, const Integer& a, const Integer& b); friend void PositiveSubtract(Integer& diff, const Integer& a, const Integer& b); @@ -308,6 +303,7 @@ inline void swap(Integer &a, Integer &b) Integer CRT(const Integer& xp, const Integer& p, const Integer& xq, const Integer& q, const Integer& u); + inline Integer ModularExponentiation(const Integer& a, const Integer& e, const Integer& m) { diff --git a/extra/yassl/taocrypt/include/misc.hpp b/extra/yassl/taocrypt/include/misc.hpp index b5b0a4575fc..de8cbb30fcb 100644 --- a/extra/yassl/taocrypt/include/misc.hpp +++ b/extra/yassl/taocrypt/include/misc.hpp @@ -28,17 +28,6 @@ #include <assert.h> #include <string.h> -namespace TaoCrypt { - -// library allocation -struct new_t {}; // TaoCrypt New type -extern new_t tc; // pass in parameter - -} // namespace TaoCrypt - -void* operator new (size_t, TaoCrypt::new_t); -void* operator new[](size_t, TaoCrypt::new_t); - namespace TaoCrypt { diff --git a/extra/yassl/taocrypt/include/modarith.hpp b/extra/yassl/taocrypt/include/modarith.hpp index 88a2cc95c7c..66a841b05c3 100644 --- a/extra/yassl/taocrypt/include/modarith.hpp +++ b/extra/yassl/taocrypt/include/modarith.hpp @@ -27,14 +27,13 @@ #define TAO_CRYPT_MODARITH_HPP #include "misc.hpp" -#include "integer.hpp" #include "algebra.hpp" namespace TaoCrypt { -//! ModularArithmetic -class ModularArithmetic : public AbstractRing<Integer> +// ModularArithmetic +class ModularArithmetic : public AbstractRing { public: @@ -45,7 +44,7 @@ public: : modulus(modulus), result((word)0, modulus.reg_.size()) {} ModularArithmetic(const ModularArithmetic &ma) - : AbstractRing<Integer>(), + : AbstractRing(), modulus(ma.modulus), result((word)0, modulus.reg_.size()) {} const Integer& GetModulus() const {return modulus;} @@ -149,12 +148,12 @@ public: Integer CascadeExponentiate(const Integer &x, const Integer &e1, const Integer &y, const Integer &e2) const - {return AbstractRing<Integer>::CascadeExponentiate(x, e1, y, e2);} + {return AbstractRing::CascadeExponentiate(x, e1, y, e2);} void SimultaneousExponentiate(Element *results, const Element &base, const Integer *exponents, unsigned int exponentsCount) const - {AbstractRing<Integer>::SimultaneousExponentiate(results, base, - exponents, exponentsCount);} + {AbstractRing::SimultaneousExponentiate(results, base, + exponents, exponentsCount);} private: Integer u; diff --git a/extra/yassl/taocrypt/include/modes.hpp b/extra/yassl/taocrypt/include/modes.hpp index 2a21ad46b76..3f9878a9e62 100644 --- a/extra/yassl/taocrypt/include/modes.hpp +++ b/extra/yassl/taocrypt/include/modes.hpp @@ -56,10 +56,11 @@ private: // Mode Base for block ciphers, static size -template<int BLOCK_SIZE> class Mode_BASE { public: - Mode_BASE() {} + enum { MaxBlockSz = 16 }; + + explicit Mode_BASE(int sz) : blockSz_(sz) { assert(sz <= MaxBlockSz); } virtual ~Mode_BASE() {} virtual void ProcessAndXorBlock(const byte*, const byte*, byte*) const = 0; @@ -68,10 +69,11 @@ public: void CBC_Encrypt(byte*, const byte*, word32); void CBC_Decrypt(byte*, const byte*, word32); - void SetIV(const byte* iv) { memcpy(reg_, iv, BLOCK_SIZE); } + void SetIV(const byte* iv) { memcpy(reg_, iv, blockSz_); } private: - byte reg_[BLOCK_SIZE]; - byte tmp_[BLOCK_SIZE]; + byte reg_[MaxBlockSz]; + byte tmp_[MaxBlockSz]; + int blockSz_; Mode_BASE(const Mode_BASE&); // hide copy Mode_BASE& operator=(const Mode_BASE&); // and assign @@ -79,51 +81,48 @@ private: // ECB Process blocks -template<int BLOCK_SIZE> -void Mode_BASE<BLOCK_SIZE>::ECB_Process(byte* out, const byte* in, word32 sz) +inline void Mode_BASE::ECB_Process(byte* out, const byte* in, word32 sz) { - word32 blocks = sz / BLOCK_SIZE; + word32 blocks = sz / blockSz_; while (blocks--) { ProcessAndXorBlock(in, 0, out); - out += BLOCK_SIZE; - in += BLOCK_SIZE; + out += blockSz_; + in += blockSz_; } } // CBC Encrypt -template<int BLOCK_SIZE> -void Mode_BASE<BLOCK_SIZE>::CBC_Encrypt(byte* out, const byte* in, word32 sz) +inline void Mode_BASE::CBC_Encrypt(byte* out, const byte* in, word32 sz) { - word32 blocks = sz / BLOCK_SIZE; + word32 blocks = sz / blockSz_; while (blocks--) { - xorbuf(reg_, in, BLOCK_SIZE); + xorbuf(reg_, in, blockSz_); ProcessAndXorBlock(reg_, 0, reg_); - memcpy(out, reg_, BLOCK_SIZE); - out += BLOCK_SIZE; - in += BLOCK_SIZE; + memcpy(out, reg_, blockSz_); + out += blockSz_; + in += blockSz_; } } // CBC Decrypt -template<int BLOCK_SIZE> -void Mode_BASE<BLOCK_SIZE>::CBC_Decrypt(byte* out, const byte* in, word32 sz) +inline void Mode_BASE::CBC_Decrypt(byte* out, const byte* in, word32 sz) { - word32 blocks = sz / BLOCK_SIZE; - byte hold[BLOCK_SIZE]; + word32 blocks = sz / blockSz_; + byte hold[MaxBlockSz]; while (blocks--) { - memcpy(tmp_, in, BLOCK_SIZE); + memcpy(tmp_, in, blockSz_); ProcessAndXorBlock(tmp_, 0, out); - xorbuf(out, reg_, BLOCK_SIZE); - memcpy(hold, reg_, BLOCK_SIZE); // swap reg_ and tmp_ - memcpy(reg_, tmp_, BLOCK_SIZE); - memcpy(tmp_, hold, BLOCK_SIZE); - out += BLOCK_SIZE; - in += BLOCK_SIZE; + xorbuf(out, reg_, blockSz_); + memcpy(hold, reg_, blockSz_); // swap reg_ and tmp_ + memcpy(reg_, tmp_, blockSz_); + memcpy(tmp_, hold, blockSz_); + out += blockSz_; + in += blockSz_; } } |