added mysql source for win build
--HG-- branch : trunk
This commit is contained in:
+207
@@ -0,0 +1,207 @@
|
||||
/*
|
||||
Copyright (C) 2000-2007 MySQL AB
|
||||
|
||||
This program is free software; you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation; version 2 of the License.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program; see the file COPYING. If not, write to the
|
||||
Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
|
||||
MA 02110-1301 USA.
|
||||
*/
|
||||
|
||||
|
||||
/* yaSSL buffer header defines input and output buffers to simulate streaming
|
||||
* with SSL types and sockets
|
||||
*/
|
||||
|
||||
#ifndef yaSSL_BUFFER_HPP
|
||||
#define yaSSL_BUFFER_HPP
|
||||
|
||||
#include <assert.h> // assert
|
||||
#include "yassl_types.hpp" // ysDelete
|
||||
#include "memory.hpp" // mySTL::auto_ptr
|
||||
#include STL_ALGORITHM_FILE
|
||||
|
||||
|
||||
namespace STL = STL_NAMESPACE;
|
||||
|
||||
|
||||
#ifdef _MSC_VER
|
||||
// disable truncated debug symbols
|
||||
#pragma warning(disable:4786)
|
||||
#endif
|
||||
|
||||
|
||||
namespace yaSSL {
|
||||
|
||||
typedef unsigned char byte;
|
||||
typedef unsigned int uint;
|
||||
const uint AUTO = 0xFEEDBEEF;
|
||||
|
||||
|
||||
// Checking Policy should implement a check function that tests whether the
|
||||
// index is within the size limit of the array
|
||||
struct Check {
|
||||
void check(uint i, uint limit);
|
||||
};
|
||||
|
||||
|
||||
struct NoCheck {
|
||||
void check(uint, uint);
|
||||
};
|
||||
|
||||
/* input_buffer operates like a smart c style array with a checking option,
|
||||
* meant to be read from through [] with AUTO index or read().
|
||||
* Should only write to at/near construction with assign() or raw (e.g., recv)
|
||||
* followed by add_size with the number of elements added by raw write.
|
||||
*
|
||||
* Not using vector because need checked []access, offset, and the ability to
|
||||
* write to the buffer bulk wise and have the correct size
|
||||
*/
|
||||
|
||||
class input_buffer : public Check {
|
||||
uint size_; // number of elements in buffer
|
||||
uint current_; // current offset position in buffer
|
||||
byte* buffer_; // storage for buffer
|
||||
byte* end_; // end of storage marker
|
||||
public:
|
||||
input_buffer();
|
||||
|
||||
explicit input_buffer(uint s);
|
||||
|
||||
// with assign
|
||||
input_buffer(uint s, const byte* t, uint len);
|
||||
|
||||
~input_buffer();
|
||||
|
||||
// users can pass defualt zero length buffer and then allocate
|
||||
void allocate(uint s);
|
||||
|
||||
// for passing to raw writing functions at beginning, then use add_size
|
||||
byte* get_buffer() const;
|
||||
|
||||
// after a raw write user can set new size
|
||||
// if you know the size before the write use assign()
|
||||
void add_size(uint i);
|
||||
|
||||
uint get_capacity() const;
|
||||
|
||||
uint get_current() const;
|
||||
|
||||
uint get_size() const;
|
||||
|
||||
uint get_remaining() const;
|
||||
|
||||
void set_current(uint i);
|
||||
|
||||
// read only access through [], advance current
|
||||
// user passes in AUTO index for ease of use
|
||||
const byte& operator[](uint i);
|
||||
|
||||
// end of input test
|
||||
bool eof();
|
||||
|
||||
// peek ahead
|
||||
byte peek() const;
|
||||
|
||||
// write function, should use at/near construction
|
||||
void assign(const byte* t, uint s);
|
||||
|
||||
// use read to query input, adjusts current
|
||||
void read(byte* dst, uint length);
|
||||
|
||||
private:
|
||||
input_buffer(const input_buffer&); // hide copy
|
||||
input_buffer& operator=(const input_buffer&); // and assign
|
||||
};
|
||||
|
||||
|
||||
/* output_buffer operates like a smart c style array with a checking option.
|
||||
* Meant to be written to through [] with AUTO index or write().
|
||||
* Size (current) counter increases when written to. Can be constructed with
|
||||
* zero length buffer but be sure to allocate before first use.
|
||||
* Don't use add write for a couple bytes, use [] instead, way less overhead.
|
||||
*
|
||||
* Not using vector because need checked []access and the ability to
|
||||
* write to the buffer bulk wise and retain correct size
|
||||
*/
|
||||
class output_buffer : public Check {
|
||||
uint current_; // current offset and elements in buffer
|
||||
byte* buffer_; // storage for buffer
|
||||
byte* end_; // end of storage marker
|
||||
public:
|
||||
// default
|
||||
output_buffer();
|
||||
|
||||
// with allocate
|
||||
explicit output_buffer(uint s);
|
||||
|
||||
// with assign
|
||||
output_buffer(uint s, const byte* t, uint len);
|
||||
|
||||
~output_buffer();
|
||||
|
||||
uint get_size() const;
|
||||
|
||||
uint get_capacity() const;
|
||||
|
||||
void set_current(uint c);
|
||||
|
||||
// users can pass defualt zero length buffer and then allocate
|
||||
void allocate(uint s);
|
||||
|
||||
// for passing to reading functions when finished
|
||||
const byte* get_buffer() const;
|
||||
|
||||
// allow write access through [], update current
|
||||
// user passes in AUTO as index for ease of use
|
||||
byte& operator[](uint i);
|
||||
|
||||
// end of output test
|
||||
bool eof();
|
||||
|
||||
void write(const byte* t, uint s);
|
||||
|
||||
private:
|
||||
output_buffer(const output_buffer&); // hide copy
|
||||
output_buffer& operator=(const output_buffer&); // and assign
|
||||
};
|
||||
|
||||
|
||||
|
||||
|
||||
// turn delete an incomplete type into comipler error instead of warning
|
||||
template <typename T>
|
||||
inline void checked_delete(T* p)
|
||||
{
|
||||
typedef char complete_type[sizeof(T) ? 1 : -1];
|
||||
(void)sizeof(complete_type);
|
||||
ysDelete(p);
|
||||
}
|
||||
|
||||
|
||||
// checked delete functor increases effeciency, no indirection on function call
|
||||
// sets pointer to zero so safe for std conatiners
|
||||
struct del_ptr_zero
|
||||
{
|
||||
template <typename T>
|
||||
void operator()(T*& p) const
|
||||
{
|
||||
T* tmp = 0;
|
||||
STL::swap(tmp, p);
|
||||
checked_delete(tmp);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
|
||||
} // naemspace
|
||||
|
||||
#endif // yaSSL_BUUFER_HPP
|
||||
@@ -0,0 +1,132 @@
|
||||
/*
|
||||
Copyright (C) 2000-2007 MySQL AB
|
||||
|
||||
This program is free software; you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation; version 2 of the License.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program; see the file COPYING. If not, write to the
|
||||
Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
|
||||
MA 02110-1301 USA.
|
||||
*/
|
||||
|
||||
|
||||
/* The certificate wrapper header defines certificate management functions
|
||||
*
|
||||
*/
|
||||
|
||||
|
||||
#ifndef yaSSL_CERT_WRAPPER_HPP
|
||||
#define yaSSL_CERT_WRAPPER_HPP
|
||||
|
||||
#ifdef _MSC_VER
|
||||
// disable truncated debug symbols
|
||||
#pragma warning(disable:4786)
|
||||
#endif
|
||||
|
||||
|
||||
#include "yassl_types.hpp" // SignatureAlgorithm
|
||||
#include "buffer.hpp" // input_buffer
|
||||
#include "asn.hpp" // SignerList
|
||||
#include "openssl/ssl.h" // internal and external use
|
||||
#include STL_LIST_FILE
|
||||
#include STL_ALGORITHM_FILE
|
||||
|
||||
|
||||
namespace STL = STL_NAMESPACE;
|
||||
|
||||
|
||||
namespace yaSSL {
|
||||
|
||||
typedef unsigned char opaque;
|
||||
class X509; // forward openSSL type
|
||||
|
||||
using TaoCrypt::SignerList;
|
||||
|
||||
// an x509 version 3 certificate
|
||||
class x509 {
|
||||
uint length_;
|
||||
opaque* buffer_;
|
||||
public:
|
||||
explicit x509(uint sz);
|
||||
~x509();
|
||||
|
||||
uint get_length() const;
|
||||
const opaque* get_buffer() const;
|
||||
opaque* use_buffer();
|
||||
|
||||
x509(const x509&);
|
||||
x509& operator=(const x509&);
|
||||
private:
|
||||
void Swap(x509&);
|
||||
};
|
||||
|
||||
|
||||
// Certificate Manager keeps a list of the cert chain and public key
|
||||
class CertManager {
|
||||
typedef STL::list<x509*> CertList;
|
||||
|
||||
CertList list_; // self
|
||||
input_buffer privateKey_;
|
||||
|
||||
CertList peerList_; // peer
|
||||
input_buffer peerPublicKey_;
|
||||
X509* peerX509_; // peer's openSSL X509
|
||||
|
||||
SignatureAlgorithm keyType_; // self key type
|
||||
SignatureAlgorithm peerKeyType_; // peer's key type
|
||||
|
||||
SignerList signers_; // decoded CA keys and names
|
||||
// plus verified chained certs
|
||||
bool verifyPeer_;
|
||||
bool verifyNone_; // no error if verify fails
|
||||
bool failNoCert_;
|
||||
bool sendVerify_;
|
||||
VerifyCallback verifyCallback_; // user verify callback
|
||||
public:
|
||||
CertManager();
|
||||
~CertManager();
|
||||
|
||||
void AddPeerCert(x509* x); // take ownership
|
||||
void CopySelfCert(const x509* x);
|
||||
int CopyCaCert(const x509* x);
|
||||
int Validate();
|
||||
|
||||
int SetPrivateKey(const x509&);
|
||||
|
||||
const x509* get_cert() const;
|
||||
const opaque* get_peerKey() const;
|
||||
const opaque* get_privateKey() const;
|
||||
X509* get_peerX509() const;
|
||||
SignatureAlgorithm get_keyType() const;
|
||||
SignatureAlgorithm get_peerKeyType() const;
|
||||
|
||||
uint get_peerKeyLength() const;
|
||||
uint get_privateKeyLength() const;
|
||||
|
||||
bool verifyPeer() const;
|
||||
bool verifyNone() const;
|
||||
bool failNoCert() const;
|
||||
bool sendVerify() const;
|
||||
|
||||
void setVerifyPeer();
|
||||
void setVerifyNone();
|
||||
void setFailNoCert();
|
||||
void setSendVerify();
|
||||
void setPeerX509(X509*);
|
||||
void setVerifyCallback(VerifyCallback);
|
||||
private:
|
||||
CertManager(const CertManager&); // hide copy
|
||||
CertManager& operator=(const CertManager&); // and assign
|
||||
};
|
||||
|
||||
|
||||
} // naemspace
|
||||
|
||||
#endif // yaSSL_CERT_WRAPPER_HPP
|
||||
+295
@@ -0,0 +1,295 @@
|
||||
|
||||
/* Headers we may want to use. */
|
||||
/* #undef HAVE_ALLOCA_H */
|
||||
/* #undef HAVE_ARPA_INET_H */
|
||||
/* #undef HAVE_CRYPT_H */
|
||||
/* #undef HAVE_DIRENT_H */
|
||||
/* #undef HAVE_EXECINFO_H */
|
||||
#define HAVE_FCNTL_H 1
|
||||
/* #undef HAVE_FENV_H */
|
||||
#define HAVE_FLOAT_H 1
|
||||
/* #undef HAVE_FPU_CONTROL_H */
|
||||
/* #undef HAVE_GRP_H */
|
||||
/* #undef HAVE_IEEEFP_H */
|
||||
#define HAVE_LIMITS_H 1
|
||||
#define HAVE_MALLOC_H 1
|
||||
#define HAVE_MEMORY_H 1
|
||||
/* #undef HAVE_NETINET_IN_H */
|
||||
/* #undef HAVE_PATHS_H */
|
||||
/* #undef HAVE_PWD_H */
|
||||
/* #undef HAVE_SCHED_H */
|
||||
/* #undef HAVE_SELECT_H */
|
||||
#define HAVE_STDDEF_H 1
|
||||
#define HAVE_STDLIB_H 1
|
||||
/* #undef HAVE_STRINGS_H */
|
||||
#define HAVE_STRING_H 1
|
||||
/* #undef HAVE_SYNCH_H */
|
||||
/* #undef HAVE_SYSENT_H */
|
||||
/* #undef HAVE_SYS_FPU_H */
|
||||
/* #undef HAVE_SYS_IOCTL_H */
|
||||
/* #undef HAVE_SYS_IPC_H */
|
||||
/* #undef HAVE_SYS_MMAN_H */
|
||||
/* #undef HAVE_SYS_PRCTL_H */
|
||||
/* #undef HAVE_SYS_SELECT_H */
|
||||
/* #undef HAVE_SYS_SHM_H */
|
||||
/* #undef HAVE_SYS_SOCKET_H */
|
||||
#define HAVE_SYS_STAT_H 1
|
||||
/* #undef HAVE_SYS_STREAM_H */
|
||||
#define HAVE_SYS_TIMEB_H 1
|
||||
#define HAVE_SYS_TYPES_H 1
|
||||
/* #undef HAVE_SYS_UN_H */
|
||||
/* #undef HAVE_TERMIOS_H */
|
||||
/* #undef HAVE_TERMIO_H */
|
||||
/* #undef HAVE_UNISTD_H */
|
||||
/* #undef HAVE_UTIME_H */
|
||||
|
||||
/* Functions we may want to use. */
|
||||
#define HAVE_ACCESS 1
|
||||
/* #undef HAVE_AIOWAIT */
|
||||
/* #undef HAVE_ALARM */
|
||||
/* #undef HAVE_ALLOCA */
|
||||
/* #undef HAVE_BCMP */
|
||||
/* #undef HAVE_BFILL */
|
||||
/* #undef HAVE_BMOVE */
|
||||
/* #undef HAVE_BZERO */
|
||||
/* #undef HAVE_CLOCK_GETTIME */
|
||||
/* #undef HAVE_COMPRESS */
|
||||
/* #undef HAVE_CRYPT */
|
||||
/* #undef HAVE_DLERROR */
|
||||
/* #undef HAVE_DLOPEN */
|
||||
/* #undef HAVE_FCHMOD */
|
||||
/* #undef HAVE_FCNTL */
|
||||
/* #undef HAVE_FCONVERT */
|
||||
/* #undef HAVE_FDATASYNC */
|
||||
/* #undef HAVE_FESETROUND */
|
||||
/* #undef HAVE_FINITE */
|
||||
/* #undef HAVE_FP_EXCEPT */
|
||||
/* #undef HAVE_FSEEKO */
|
||||
/* #undef HAVE_FSYNC */
|
||||
/* #undef HAVE_GETADDRINFO */
|
||||
#define HAVE_GETCWD 1
|
||||
/* #undef HAVE_GETHOSTBYADDR_R */
|
||||
/* #undef HAVE_GETHOSTBYNAME_R */
|
||||
/* #undef HAVE_GETHRTIME */
|
||||
/* #undef HAVE_GETNAMEINFO */
|
||||
/* #undef HAVE_GETPAGESIZE */
|
||||
/* #undef HAVE_GETPASS */
|
||||
/* #undef HAVE_GETPASSPHRASE */
|
||||
/* #undef HAVE_GETPWNAM */
|
||||
/* #undef HAVE_GETPWUID */
|
||||
/* #undef HAVE_GETRLIMIT */
|
||||
/* #undef HAVE_GETRUSAGE */
|
||||
/* #undef HAVE_GETWD */
|
||||
/* #undef HAVE_GMTIME_R */
|
||||
/* #undef HAVE_INITGROUPS */
|
||||
/* #undef HAVE_ISNAN */
|
||||
#define HAVE_LDIV 1
|
||||
/* #undef HAVE_LOCALTIME_R */
|
||||
/* #undef HAVE_LOG2 */
|
||||
#define HAVE_LONGJMP 1
|
||||
/* #undef HAVE_LSTAT */
|
||||
/* #undef HAVE_MADVISE */
|
||||
/* #undef HAVE_DECL_MADVISE */
|
||||
/* #undef HAVE_MALLINFO */
|
||||
#define HAVE_MEMCPY 1
|
||||
#define HAVE_MEMCPY 1
|
||||
#define HAVE_MEMMOVE 1
|
||||
#define HAVE_MEMMOVE 1
|
||||
/* #undef HAVE_MKSTEMP */
|
||||
/* #undef HAVE_MLOCK */
|
||||
/* #undef HAVE_MLOCKALL */
|
||||
/* #undef HAVE_MMAP */
|
||||
/* #undef HAVE_MMAP64 */
|
||||
#define HAVE_PERROR 1
|
||||
/* #undef HAVE_POLL */
|
||||
/* #undef HAVE_PREAD */
|
||||
/* #undef HAVE_PTHREAD_ATTR_CREATE */
|
||||
/* #undef HAVE_PTHREAD_ATTR_GETSTACKSIZE */
|
||||
/* #undef HAVE_PTHREAD_ATTR_SETPRIO */
|
||||
/* #undef HAVE_PTHREAD_ATTR_SETSCHEDPARAM */
|
||||
/* #undef HAVE_PTHREAD_ATTR_SETSCOPE */
|
||||
/* #undef HAVE_PTHREAD_ATTR_SETSTACKSIZE */
|
||||
/* #undef HAVE_PTHREAD_CONDATTR_CREATE */
|
||||
/* #undef HAVE_PTHREAD_INIT */
|
||||
/* #undef HAVE_PTHREAD_KEY_DELETE */
|
||||
/* #undef HAVE_PTHREAD_KEY_DELETE */
|
||||
/* #undef HAVE_PTHREAD_KILL */
|
||||
/* #undef HAVE_PTHREAD_RWLOCK_RDLOCK */
|
||||
/* #undef HAVE_PTHREAD_SETPRIO_NP */
|
||||
/* #undef HAVE_PTHREAD_SETSCHEDPARAM */
|
||||
/* #undef HAVE_PTHREAD_SIGMASK */
|
||||
/* #undef HAVE_PTHREAD_THREADMASK */
|
||||
/* #undef HAVE_PTHREAD_YIELD_NP */
|
||||
/* #undef HAVE_READDIR_R */
|
||||
/* #undef HAVE_READLINK */
|
||||
/* #undef HAVE_REALPATH */
|
||||
#define HAVE_RENAME 1
|
||||
/* #undef HAVE_RINT */
|
||||
/* #undef HAVE_SCHED_YIELD */
|
||||
/* #undef HAVE_SELECT */
|
||||
/* #undef HAVE_SETFD */
|
||||
/* #undef HAVE_SETFILEPOINTER */
|
||||
/* #undef HAVE_SIGACTION */
|
||||
/* #undef HAVE_SIGTHREADMASK */
|
||||
/* #undef HAVE_SIGWAIT */
|
||||
/* #undef HAVE_SLEEP */
|
||||
/* #undef HAVE_SNPRINTF */
|
||||
/* #undef HAVE_STPCPY */
|
||||
#define HAVE_STRERROR 1
|
||||
/* #undef HAVE_STRLCPY */
|
||||
#define HAVE_STRNLEN 1
|
||||
#define HAVE_STRPBRK 1
|
||||
/* #undef HAVE_STRSEP */
|
||||
#define HAVE_STRSTR 1
|
||||
/* #undef HAVE_STRTOK_R */
|
||||
/* #undef HAVE_STRTOK_R */
|
||||
#define HAVE_STRTOL 1
|
||||
/* #undef HAVE_STRTOLL */
|
||||
#define HAVE_STRTOUL 1
|
||||
/* #undef HAVE_STRTOULL */
|
||||
#define HAVE_TELL 1
|
||||
/* #undef HAVE_THR_SETCONCURRENCY */
|
||||
/* #undef HAVE_THR_YIELD */
|
||||
/* #undef HAVE_VASPRINTF */
|
||||
#define HAVE_VSNPRINTF 1
|
||||
|
||||
/* Symbols we may use */
|
||||
/* #undef HAVE_SYS_ERRLIST */
|
||||
/* used by stacktrace functions */
|
||||
/* #undef HAVE_BSS_START */
|
||||
|
||||
/* Does "struct timespec" have a "sec" and "nsec" field? */
|
||||
/* #undef HAVE_TIMESPEC_TS_SEC */
|
||||
|
||||
/* Types we may use */
|
||||
#define SIZEOF_CHAR 1
|
||||
#if SIZEOF_CHAR
|
||||
# define HAVE_CHAR 1
|
||||
#endif
|
||||
|
||||
#define SIZEOF_CHARP 4
|
||||
#if SIZEOF_CHARP
|
||||
# define HAVE_CHARP 1
|
||||
#endif
|
||||
|
||||
#define SIZEOF_SHORT 2
|
||||
#if SIZEOF_SHORT
|
||||
# define HAVE_SHORT 1
|
||||
#endif
|
||||
|
||||
#define SIZEOF_INT 4
|
||||
#if SIZEOF_INT
|
||||
# define HAVE_INT 1
|
||||
#endif
|
||||
|
||||
#define SIZEOF_LONG 4
|
||||
#if SIZEOF_LONG
|
||||
# define HAVE_LONG 1
|
||||
#endif
|
||||
|
||||
#define SIZEOF_LONG_LONG 8
|
||||
#if SIZEOF_LONG_LONG
|
||||
# define HAVE_LONG_LONG 1
|
||||
#endif
|
||||
|
||||
#define SIZEOF_OFF_T 4
|
||||
#if SIZEOF_OFF_T
|
||||
# define HAVE_OFF_T 1
|
||||
#endif
|
||||
|
||||
/* #undef SIZEOF_SIGSET_T */
|
||||
#if SIZEOF_SIGSET_T
|
||||
# define HAVE_SIGSET_T 1
|
||||
#endif
|
||||
|
||||
#define SIZEOF_SIZE_T 4
|
||||
#if SIZEOF_SIZE_T
|
||||
# define HAVE_SIZE_T 1
|
||||
#endif
|
||||
|
||||
/* #undef SIZEOF_UCHAR */
|
||||
#if SIZEOF_UCHAR
|
||||
# define HAVE_UCHAR 1
|
||||
#endif
|
||||
|
||||
/* #undef SIZEOF_UINT */
|
||||
#if SIZEOF_UINT
|
||||
# define HAVE_UINT 1
|
||||
#endif
|
||||
|
||||
/* #undef SIZEOF_ULONG */
|
||||
#if SIZEOF_ULONG
|
||||
# define HAVE_ULONG 1
|
||||
#endif
|
||||
|
||||
/* #undef SIZEOF_INT8 */
|
||||
#if SIZEOF_INT8
|
||||
# define HAVE_INT8 1
|
||||
#endif
|
||||
/* #undef SIZEOF_UINT8 */
|
||||
#if SIZEOF_UINT8
|
||||
# define HAVE_UINT8 1
|
||||
#endif
|
||||
|
||||
/* #undef SIZEOF_INT16 */
|
||||
#if SIZEOF_INT16
|
||||
# define HAVE_INT16 1
|
||||
#endif
|
||||
/* #undef SIZEOF_UINT16 */
|
||||
#if SIZEOF_UINT16
|
||||
# define HAVE_UINT16 1
|
||||
#endif
|
||||
|
||||
/* #undef SIZEOF_INT32 */
|
||||
#if SIZEOF_INT32
|
||||
# define HAVE_INT32 1
|
||||
#endif
|
||||
/* #undef SIZEOF_UINT32 */
|
||||
#if SIZEOF_UINT32
|
||||
# define HAVE_UINT32 1
|
||||
#endif
|
||||
/* #undef SIZEOF_U_INT32_T */
|
||||
#if SIZEOF_U_INT32_T
|
||||
# define HAVE_U_INT32_T 1
|
||||
#endif
|
||||
|
||||
/* #undef SIZEOF_INT64 */
|
||||
#if SIZEOF_INT64
|
||||
# define HAVE_INT64 1
|
||||
#endif
|
||||
/* #undef SIZEOF_UINT64 */
|
||||
#if SIZEOF_UINT64
|
||||
# define HAVE_UINT64 1
|
||||
#endif
|
||||
|
||||
/* #undef SIZEOF_SOCKLEN_T */
|
||||
#if SIZEOF_SOCKLEN_T
|
||||
# define HAVE_SOCKLEN_T 1
|
||||
#endif
|
||||
|
||||
/* XXX mysql_client_test uses this -- rip it out, please! */
|
||||
#define MAX_INDEXES 64
|
||||
|
||||
#define QSORT_TYPE_IS_VOID 1
|
||||
#define RETQSORTTYPE void
|
||||
|
||||
#define SIGNAL_RETURN_TYPE_IS_VOID 1
|
||||
#define RETSIGTYPE void
|
||||
|
||||
/* #undef WORDS_BIGENDIAN */
|
||||
|
||||
/* Define to `__inline__' or `__inline' if that's what the C compiler calls
|
||||
it, or to nothing if 'inline' is not supported under any name. */
|
||||
#ifndef __cplusplus
|
||||
# define inline
|
||||
#endif
|
||||
|
||||
#define TIME_WITH_SYS_TIME 1
|
||||
|
||||
#define STACK_DIRECTION -1
|
||||
|
||||
#define SHAREDIR "share"
|
||||
#define THREAD 1
|
||||
#define THREAD_SAFE_CLIENT 1
|
||||
|
||||
#define DEFAULT_CHARSET_HOME "C:/mysql/"
|
||||
@@ -0,0 +1,427 @@
|
||||
/*
|
||||
Copyright (C) 2000-2007 MySQL AB
|
||||
|
||||
This program is free software; you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation; version 2 of the License.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program; see the file COPYING. If not, write to the
|
||||
Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
|
||||
MA 02110-1301 USA.
|
||||
*/
|
||||
|
||||
|
||||
/* The crypto wrapper header is used to define policies for the cipher
|
||||
* components used by SSL. There are 3 policies to consider:
|
||||
*
|
||||
* 1) MAC, the Message Authentication Code used for each Message
|
||||
* 2) Bulk Cipher, the Cipher used to encrypt/decrypt each Message
|
||||
* 3) Atuhentication, the Digitial Signing/Verifiaction scheme used
|
||||
*
|
||||
* This header doesn't rely on a specific crypto libraries internals,
|
||||
* only the implementation should.
|
||||
*/
|
||||
|
||||
|
||||
#ifndef yaSSL_CRYPTO_WRAPPER_HPP
|
||||
#define yaSSL_CRYPTO_WRAPPER_HPP
|
||||
|
||||
#include "yassl_types.hpp"
|
||||
#include <stdio.h> // FILE
|
||||
|
||||
|
||||
namespace yaSSL {
|
||||
|
||||
|
||||
// Digest policy should implement a get_digest, update, and get sizes for pad
|
||||
// and digest
|
||||
struct Digest : public virtual_base {
|
||||
virtual void get_digest(byte*) = 0;
|
||||
virtual void get_digest(byte*, const byte*, unsigned int) = 0;
|
||||
virtual void update(const byte*, unsigned int) = 0;
|
||||
virtual uint get_digestSize() const = 0;
|
||||
virtual uint get_padSize() const = 0;
|
||||
virtual ~Digest() {}
|
||||
};
|
||||
|
||||
|
||||
// For use with NULL Digests
|
||||
struct NO_MAC : public Digest {
|
||||
void get_digest(byte*);
|
||||
void get_digest(byte*, const byte*, unsigned int);
|
||||
void update(const byte*, unsigned int);
|
||||
uint get_digestSize() const;
|
||||
uint get_padSize() const;
|
||||
};
|
||||
|
||||
|
||||
// MD5 Digest
|
||||
class MD5 : public Digest {
|
||||
public:
|
||||
void get_digest(byte*);
|
||||
void get_digest(byte*, const byte*, unsigned int);
|
||||
void update(const byte*, unsigned int);
|
||||
uint get_digestSize() const;
|
||||
uint get_padSize() const;
|
||||
MD5();
|
||||
~MD5();
|
||||
MD5(const MD5&);
|
||||
MD5& operator=(const MD5&);
|
||||
private:
|
||||
struct MD5Impl;
|
||||
MD5Impl* pimpl_;
|
||||
};
|
||||
|
||||
|
||||
// SHA-1 Digest
|
||||
class SHA : public Digest {
|
||||
public:
|
||||
void get_digest(byte*);
|
||||
void get_digest(byte*, const byte*, unsigned int);
|
||||
void update(const byte*, unsigned int);
|
||||
uint get_digestSize() const;
|
||||
uint get_padSize() const;
|
||||
SHA();
|
||||
~SHA();
|
||||
SHA(const SHA&);
|
||||
SHA& operator=(const SHA&);
|
||||
private:
|
||||
struct SHAImpl;
|
||||
SHAImpl* pimpl_;
|
||||
|
||||
};
|
||||
|
||||
|
||||
// RIPEMD-160 Digest
|
||||
class RMD : public Digest {
|
||||
public:
|
||||
void get_digest(byte*);
|
||||
void get_digest(byte*, const byte*, unsigned int);
|
||||
void update(const byte*, unsigned int);
|
||||
uint get_digestSize() const;
|
||||
uint get_padSize() const;
|
||||
RMD();
|
||||
~RMD();
|
||||
RMD(const RMD&);
|
||||
RMD& operator=(const RMD&);
|
||||
private:
|
||||
struct RMDImpl;
|
||||
RMDImpl* pimpl_;
|
||||
|
||||
};
|
||||
|
||||
|
||||
// HMAC_MD5
|
||||
class HMAC_MD5 : public Digest {
|
||||
public:
|
||||
void get_digest(byte*);
|
||||
void get_digest(byte*, const byte*, unsigned int);
|
||||
void update(const byte*, unsigned int);
|
||||
uint get_digestSize() const;
|
||||
uint get_padSize() const;
|
||||
HMAC_MD5(const byte*, unsigned int);
|
||||
~HMAC_MD5();
|
||||
private:
|
||||
struct HMAC_MD5Impl;
|
||||
HMAC_MD5Impl* pimpl_;
|
||||
|
||||
HMAC_MD5(const HMAC_MD5&);
|
||||
HMAC_MD5& operator=(const HMAC_MD5&);
|
||||
};
|
||||
|
||||
|
||||
// HMAC_SHA-1
|
||||
class HMAC_SHA : public Digest {
|
||||
public:
|
||||
void get_digest(byte*);
|
||||
void get_digest(byte*, const byte*, unsigned int);
|
||||
void update(const byte*, unsigned int);
|
||||
uint get_digestSize() const;
|
||||
uint get_padSize() const;
|
||||
HMAC_SHA(const byte*, unsigned int);
|
||||
~HMAC_SHA();
|
||||
private:
|
||||
struct HMAC_SHAImpl;
|
||||
HMAC_SHAImpl* pimpl_;
|
||||
|
||||
HMAC_SHA(const HMAC_SHA&);
|
||||
HMAC_SHA& operator=(const HMAC_SHA&);
|
||||
};
|
||||
|
||||
|
||||
// HMAC_RMD
|
||||
class HMAC_RMD : public Digest {
|
||||
public:
|
||||
void get_digest(byte*);
|
||||
void get_digest(byte*, const byte*, unsigned int);
|
||||
void update(const byte*, unsigned int);
|
||||
uint get_digestSize() const;
|
||||
uint get_padSize() const;
|
||||
HMAC_RMD(const byte*, unsigned int);
|
||||
~HMAC_RMD();
|
||||
private:
|
||||
struct HMAC_RMDImpl;
|
||||
HMAC_RMDImpl* pimpl_;
|
||||
|
||||
HMAC_RMD(const HMAC_RMD&);
|
||||
HMAC_RMD& operator=(const HMAC_RMD&);
|
||||
};
|
||||
|
||||
|
||||
// BulkCipher policy should implement encrypt, decrypt, get block size,
|
||||
// and set keys for encrypt and decrypt
|
||||
struct BulkCipher : public virtual_base {
|
||||
virtual void encrypt(byte*, const byte*, unsigned int) = 0;
|
||||
virtual void decrypt(byte*, const byte*, unsigned int) = 0;
|
||||
virtual void set_encryptKey(const byte*, const byte* = 0) = 0;
|
||||
virtual void set_decryptKey(const byte*, const byte* = 0) = 0;
|
||||
virtual uint get_blockSize() const = 0;
|
||||
virtual int get_keySize() const = 0;
|
||||
virtual int get_ivSize() const = 0;
|
||||
virtual ~BulkCipher() {}
|
||||
};
|
||||
|
||||
|
||||
// For use with NULL Ciphers
|
||||
struct NO_Cipher : public BulkCipher {
|
||||
void encrypt(byte*, const byte*, unsigned int) {}
|
||||
void decrypt(byte*, const byte*, unsigned int) {}
|
||||
void set_encryptKey(const byte*, const byte*) {}
|
||||
void set_decryptKey(const byte*, const byte*) {}
|
||||
uint get_blockSize() const { return 0; }
|
||||
int get_keySize() const { return 0; }
|
||||
int get_ivSize() const { return 0; }
|
||||
};
|
||||
|
||||
|
||||
// SSLv3 and TLSv1 always use DES in CBC mode so IV is required
|
||||
class DES : public BulkCipher {
|
||||
public:
|
||||
void encrypt(byte*, const byte*, unsigned int);
|
||||
void decrypt(byte*, const byte*, unsigned int);
|
||||
void set_encryptKey(const byte*, const byte*);
|
||||
void set_decryptKey(const byte*, const byte*);
|
||||
uint get_blockSize() const { return DES_BLOCK; }
|
||||
int get_keySize() const { return DES_KEY_SZ; }
|
||||
int get_ivSize() const { return DES_IV_SZ; }
|
||||
DES();
|
||||
~DES();
|
||||
private:
|
||||
struct DESImpl;
|
||||
DESImpl* pimpl_;
|
||||
|
||||
DES(const DES&); // hide copy
|
||||
DES& operator=(const DES&); // & assign
|
||||
};
|
||||
|
||||
|
||||
// 3DES Encrypt-Decrypt-Encrypt in CBC mode
|
||||
class DES_EDE : public BulkCipher {
|
||||
public:
|
||||
void encrypt(byte*, const byte*, unsigned int);
|
||||
void decrypt(byte*, const byte*, unsigned int);
|
||||
void set_encryptKey(const byte*, const byte*);
|
||||
void set_decryptKey(const byte*, const byte*);
|
||||
uint get_blockSize() const { return DES_BLOCK; }
|
||||
int get_keySize() const { return DES_EDE_KEY_SZ; }
|
||||
int get_ivSize() const { return DES_IV_SZ; }
|
||||
DES_EDE();
|
||||
~DES_EDE();
|
||||
private:
|
||||
struct DES_EDEImpl;
|
||||
DES_EDEImpl* pimpl_;
|
||||
|
||||
DES_EDE(const DES_EDE&); // hide copy
|
||||
DES_EDE& operator=(const DES_EDE&); // & assign
|
||||
};
|
||||
|
||||
|
||||
// Alledged RC4
|
||||
class RC4 : public BulkCipher {
|
||||
public:
|
||||
void encrypt(byte*, const byte*, unsigned int);
|
||||
void decrypt(byte*, const byte*, unsigned int);
|
||||
void set_encryptKey(const byte*, const byte*);
|
||||
void set_decryptKey(const byte*, const byte*);
|
||||
uint get_blockSize() const { return 0; }
|
||||
int get_keySize() const { return RC4_KEY_SZ; }
|
||||
int get_ivSize() const { return 0; }
|
||||
RC4();
|
||||
~RC4();
|
||||
private:
|
||||
struct RC4Impl;
|
||||
RC4Impl* pimpl_;
|
||||
|
||||
RC4(const RC4&); // hide copy
|
||||
RC4& operator=(const RC4&); // & assign
|
||||
};
|
||||
|
||||
|
||||
// AES
|
||||
class AES : public BulkCipher {
|
||||
public:
|
||||
void encrypt(byte*, const byte*, unsigned int);
|
||||
void decrypt(byte*, const byte*, unsigned int);
|
||||
void set_encryptKey(const byte*, const byte*);
|
||||
void set_decryptKey(const byte*, const byte*);
|
||||
uint get_blockSize() const { return AES_BLOCK_SZ; }
|
||||
int get_keySize() const;
|
||||
int get_ivSize() const { return AES_IV_SZ; }
|
||||
explicit AES(unsigned int = AES_128_KEY_SZ);
|
||||
~AES();
|
||||
private:
|
||||
struct AESImpl;
|
||||
AESImpl* pimpl_;
|
||||
|
||||
AES(const AES&); // hide copy
|
||||
AES& operator=(const AES&); // & assign
|
||||
};
|
||||
|
||||
|
||||
// Random number generator
|
||||
class RandomPool {
|
||||
public:
|
||||
void Fill(opaque* dst, uint sz) const;
|
||||
RandomPool();
|
||||
~RandomPool();
|
||||
|
||||
int GetError() const;
|
||||
|
||||
friend class RSA;
|
||||
friend class DSS;
|
||||
friend class DiffieHellman;
|
||||
private:
|
||||
struct RandomImpl;
|
||||
RandomImpl* pimpl_;
|
||||
|
||||
RandomPool(const RandomPool&); // hide copy
|
||||
RandomPool& operator=(const RandomPool&); // & assign
|
||||
};
|
||||
|
||||
|
||||
// Authentication policy should implement sign, and verify
|
||||
struct Auth : public virtual_base {
|
||||
virtual void sign(byte*, const byte*, unsigned int, const RandomPool&) = 0;
|
||||
virtual bool verify(const byte*, unsigned int, const byte*,
|
||||
unsigned int) = 0;
|
||||
virtual uint get_signatureLength() const = 0;
|
||||
virtual ~Auth() {}
|
||||
};
|
||||
|
||||
|
||||
// For use with NULL Authentication schemes
|
||||
struct NO_Auth : public Auth {
|
||||
void sign(byte*, const byte*, unsigned int, const RandomPool&) {}
|
||||
bool verify(const byte*, unsigned int, const byte*, unsigned int)
|
||||
{ return true; }
|
||||
};
|
||||
|
||||
|
||||
// Digitial Signature Standard scheme
|
||||
class DSS : public Auth {
|
||||
public:
|
||||
void sign(byte*, const byte*, unsigned int, const RandomPool&);
|
||||
bool verify(const byte*, unsigned int, const byte*, unsigned int);
|
||||
uint get_signatureLength() const;
|
||||
DSS(const byte*, unsigned int, bool publicKey = true);
|
||||
~DSS();
|
||||
private:
|
||||
struct DSSImpl;
|
||||
DSSImpl* pimpl_;
|
||||
|
||||
DSS(const DSS&);
|
||||
DSS& operator=(const DSS&);
|
||||
};
|
||||
|
||||
|
||||
// RSA Authentication and exchange
|
||||
class RSA : public Auth {
|
||||
public:
|
||||
void sign(byte*, const byte*, unsigned int, const RandomPool&);
|
||||
bool verify(const byte*, unsigned int, const byte*, unsigned int);
|
||||
void encrypt(byte*, const byte*, unsigned int, const RandomPool&);
|
||||
void decrypt(byte*, const byte*, unsigned int, const RandomPool&);
|
||||
uint get_signatureLength() const;
|
||||
uint get_cipherLength() const;
|
||||
RSA(const byte*, unsigned int, bool publicKey = true);
|
||||
~RSA();
|
||||
private:
|
||||
struct RSAImpl;
|
||||
RSAImpl* pimpl_;
|
||||
|
||||
RSA(const RSA&); // hide copy
|
||||
RSA& operator=(const RSA&); // & assing
|
||||
};
|
||||
|
||||
|
||||
class Integer;
|
||||
|
||||
// Diffie-Hellman agreement
|
||||
// hide for now TODO: figure out a way to give access to C clients p and g args
|
||||
class DiffieHellman {
|
||||
public:
|
||||
DiffieHellman(const byte*, unsigned int, const byte*, unsigned int,
|
||||
const byte*, unsigned int, const RandomPool& random);
|
||||
//DiffieHellman(const char*, const RandomPool&);
|
||||
DiffieHellman(const Integer&, const Integer&, const RandomPool&);
|
||||
~DiffieHellman();
|
||||
|
||||
DiffieHellman(const DiffieHellman&);
|
||||
DiffieHellman& operator=(const DiffieHellman&);
|
||||
|
||||
uint get_agreedKeyLength() const;
|
||||
const byte* get_agreedKey() const;
|
||||
const byte* get_publicKey() const;
|
||||
void makeAgreement(const byte*, unsigned int);
|
||||
|
||||
void set_sizes(int&, int&, int&) const;
|
||||
void get_parms(byte*, byte*, byte*) const;
|
||||
private:
|
||||
struct DHImpl;
|
||||
DHImpl* pimpl_;
|
||||
};
|
||||
|
||||
|
||||
// Lagrge Integer
|
||||
class Integer {
|
||||
public:
|
||||
Integer();
|
||||
~Integer();
|
||||
|
||||
Integer(const Integer&);
|
||||
Integer& operator=(const Integer&);
|
||||
|
||||
void assign(const byte*, unsigned int);
|
||||
|
||||
friend class DiffieHellman;
|
||||
private:
|
||||
struct IntegerImpl;
|
||||
IntegerImpl* pimpl_;
|
||||
};
|
||||
|
||||
|
||||
class x509;
|
||||
|
||||
|
||||
struct EncryptedInfo {
|
||||
enum { IV_SZ = 32, NAME_SZ = 80 };
|
||||
char name[NAME_SZ]; // max one line
|
||||
byte iv[IV_SZ]; // in base16 rep
|
||||
uint ivSz;
|
||||
bool set;
|
||||
|
||||
EncryptedInfo() : ivSz(0), set(false) {}
|
||||
};
|
||||
|
||||
x509* PemToDer(FILE*, CertType, EncryptedInfo* info = 0);
|
||||
|
||||
|
||||
} // naemspace
|
||||
|
||||
#endif // yaSSL_CRYPTO_WRAPPER_HPP
|
||||
+101
@@ -0,0 +1,101 @@
|
||||
/*
|
||||
Copyright (C) 2000-2007 MySQL AB
|
||||
|
||||
This program is free software; you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation; version 2 of the License.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program; see the file COPYING. If not, write to the
|
||||
Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
|
||||
MA 02110-1301 USA.
|
||||
*/
|
||||
|
||||
/* The factory header defines an Object Factory, used by SSL message and
|
||||
* handshake types.
|
||||
*
|
||||
* See Desgin Pattern in GoF and Alexandrescu's chapter in Modern C++ Design,
|
||||
* page 208
|
||||
*/
|
||||
|
||||
|
||||
|
||||
#ifndef yaSSL_FACTORY_HPP
|
||||
#define yaSSL_FACTORY_HPP
|
||||
|
||||
#include STL_VECTOR_FILE
|
||||
#include STL_PAIR_FILE
|
||||
|
||||
|
||||
namespace STL = STL_NAMESPACE;
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
namespace yaSSL {
|
||||
|
||||
|
||||
// Factory uses its callback map to create objects by id,
|
||||
// returning an abstract base pointer
|
||||
template<class AbstractProduct,
|
||||
typename IdentifierType = int,
|
||||
typename ProductCreator = AbstractProduct* (*)()
|
||||
>
|
||||
class Factory {
|
||||
typedef STL::pair<IdentifierType, ProductCreator> CallBack;
|
||||
typedef STL::vector<CallBack> CallBackVector;
|
||||
|
||||
CallBackVector callbacks_;
|
||||
public:
|
||||
// pass function pointer to register all callbacks upon creation
|
||||
explicit Factory(void (*init)(Factory<AbstractProduct, IdentifierType,
|
||||
ProductCreator>&))
|
||||
{
|
||||
init(*this);
|
||||
}
|
||||
|
||||
// reserve place in vector before registering, used by init funcion
|
||||
void Reserve(size_t sz)
|
||||
{
|
||||
callbacks_.reserve(sz);
|
||||
}
|
||||
|
||||
// register callback
|
||||
void Register(const IdentifierType& id, ProductCreator pc)
|
||||
{
|
||||
callbacks_.push_back(STL::make_pair(id, pc));
|
||||
}
|
||||
|
||||
// THE Creator, returns a new object of the proper type or 0
|
||||
AbstractProduct* CreateObject(const IdentifierType& id) const
|
||||
{
|
||||
typedef typename STL::vector<CallBack>::const_iterator cIter;
|
||||
|
||||
cIter first = callbacks_.begin();
|
||||
cIter last = callbacks_.end();
|
||||
|
||||
while (first != last) {
|
||||
if (first->first == id)
|
||||
break;
|
||||
++first;
|
||||
}
|
||||
|
||||
if (first == callbacks_.end())
|
||||
return 0;
|
||||
return (first->second)();
|
||||
}
|
||||
private:
|
||||
Factory(const Factory&); // hide copy
|
||||
Factory& operator=(const Factory&); // and assign
|
||||
};
|
||||
|
||||
|
||||
} // naemspace
|
||||
|
||||
#endif // yaSSL_FACTORY_HPP
|
||||
@@ -0,0 +1,69 @@
|
||||
/*
|
||||
Copyright (C) 2000-2007 MySQL AB
|
||||
|
||||
This program is free software; you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation; version 2 of the License.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program; see the file COPYING. If not, write to the
|
||||
Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
|
||||
MA 02110-1301 USA.
|
||||
*/
|
||||
|
||||
/* The handshake header declares function prototypes for creating and reading
|
||||
* the various handshake messages.
|
||||
*/
|
||||
|
||||
|
||||
|
||||
#ifndef yaSSL_HANDSHAKE_HPP
|
||||
#define yaSSL_HANDSHAKE_HPP
|
||||
|
||||
#include "yassl_types.hpp"
|
||||
|
||||
|
||||
namespace yaSSL {
|
||||
|
||||
// forward decls
|
||||
class SSL;
|
||||
class Finished;
|
||||
class Data;
|
||||
class Alert;
|
||||
struct Hashes;
|
||||
|
||||
enum BufferOutput { buffered, unbuffered };
|
||||
|
||||
void sendClientHello(SSL&);
|
||||
void sendServerHello(SSL&, BufferOutput = buffered);
|
||||
void sendServerHelloDone(SSL&, BufferOutput = buffered);
|
||||
void sendClientKeyExchange(SSL&, BufferOutput = buffered);
|
||||
void sendServerKeyExchange(SSL&, BufferOutput = buffered);
|
||||
void sendChangeCipher(SSL&, BufferOutput = buffered);
|
||||
void sendFinished(SSL&, ConnectionEnd, BufferOutput = buffered);
|
||||
void sendCertificate(SSL&, BufferOutput = buffered);
|
||||
void sendCertificateRequest(SSL&, BufferOutput = buffered);
|
||||
void sendCertificateVerify(SSL&, BufferOutput = buffered);
|
||||
int sendData(SSL&, const void*, int);
|
||||
int sendAlert(SSL& ssl, const Alert& alert);
|
||||
|
||||
int receiveData(SSL&, Data&, bool peek = false);
|
||||
void processReply(SSL&);
|
||||
|
||||
void buildFinished(SSL&, Finished&, const opaque*);
|
||||
void build_certHashes(SSL&, Hashes&);
|
||||
|
||||
void hmac(SSL&, byte*, const byte*, uint, ContentType, bool verify = false);
|
||||
void TLS_hmac(SSL&, byte*, const byte*, uint, ContentType,
|
||||
bool verify = false);
|
||||
void PRF(byte* digest, uint digLen, const byte* secret, uint secLen,
|
||||
const byte* label, uint labLen, const byte* seed, uint seedLen);
|
||||
|
||||
} // naemspace
|
||||
|
||||
#endif // yaSSL_HANDSHAKE_HPP
|
||||
+87
@@ -0,0 +1,87 @@
|
||||
/*
|
||||
Copyright (C) 2000-2007 MySQL AB
|
||||
|
||||
This program is free software; you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation; version 2 of the License.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program; see the file COPYING. If not, write to the
|
||||
Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
|
||||
MA 02110-1301 USA.
|
||||
*/
|
||||
|
||||
/* lock.hpp provides an os specific Lock, locks mutex on entry and unlocks
|
||||
* automatically upon exit, no-ops provided for Single Threaded
|
||||
*/
|
||||
|
||||
#ifndef yaSSL_LOCK_HPP
|
||||
#define yaSSL_LOCK_HPP
|
||||
|
||||
|
||||
namespace yaSSL {
|
||||
|
||||
|
||||
#ifdef MULTI_THREADED
|
||||
#ifdef _WIN32
|
||||
#include <windows.h>
|
||||
|
||||
class Mutex {
|
||||
CRITICAL_SECTION cs_;
|
||||
public:
|
||||
Mutex();
|
||||
~Mutex();
|
||||
|
||||
class Lock;
|
||||
friend class Lock;
|
||||
|
||||
class Lock {
|
||||
Mutex& mutex_;
|
||||
public:
|
||||
explicit Lock(Mutex& lm);
|
||||
~Lock();
|
||||
};
|
||||
};
|
||||
#else // _WIN32
|
||||
#include <pthread.h>
|
||||
|
||||
class Mutex {
|
||||
pthread_mutex_t mutex_;
|
||||
public:
|
||||
|
||||
Mutex();
|
||||
~Mutex();
|
||||
|
||||
class Lock;
|
||||
friend class Lock;
|
||||
|
||||
class Lock {
|
||||
Mutex& mutex_;
|
||||
public:
|
||||
explicit Lock(Mutex& lm);
|
||||
~Lock();
|
||||
};
|
||||
};
|
||||
|
||||
#endif // _WIN32
|
||||
#else // MULTI_THREADED (WE'RE SINGLE)
|
||||
|
||||
class Mutex {
|
||||
public:
|
||||
class Lock {
|
||||
public:
|
||||
explicit Lock(Mutex&) {}
|
||||
};
|
||||
};
|
||||
|
||||
#endif // MULTI_THREADED
|
||||
|
||||
|
||||
|
||||
} // namespace
|
||||
#endif // yaSSL_LOCK_HPP
|
||||
+55
@@ -0,0 +1,55 @@
|
||||
/*
|
||||
Copyright (C) 2000-2007 MySQL AB
|
||||
|
||||
This program is free software; you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation; version 2 of the License.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program; see the file COPYING. If not, write to the
|
||||
Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
|
||||
MA 02110-1301 USA.
|
||||
*/
|
||||
|
||||
|
||||
/* yaSSL log interface
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef yaSSL_LOG_HPP
|
||||
#define yaSSL_LOG_HPP
|
||||
|
||||
#include "socket_wrapper.hpp"
|
||||
|
||||
#ifdef YASSL_LOG
|
||||
#include <stdio.h>
|
||||
#endif
|
||||
|
||||
namespace yaSSL {
|
||||
|
||||
typedef unsigned int uint;
|
||||
|
||||
|
||||
// Debug logger
|
||||
class Log {
|
||||
#ifdef YASSL_LOG
|
||||
FILE* log_;
|
||||
#endif
|
||||
public:
|
||||
explicit Log(const char* str = "yaSSL.log");
|
||||
~Log();
|
||||
|
||||
void Trace(const char*);
|
||||
void ShowTCP(socket_t, bool ended = false);
|
||||
void ShowData(uint, bool sent = false);
|
||||
};
|
||||
|
||||
|
||||
} // naemspace
|
||||
|
||||
#endif // yaSSL_LOG_HPP
|
||||
@@ -0,0 +1,18 @@
|
||||
/* crypto.h for openSSL */
|
||||
|
||||
#ifndef ysSSL_crypto_h__
|
||||
#define yaSSL_crypto_h__
|
||||
|
||||
#ifdef YASSL_PREFIX
|
||||
#include "prefix_crypto.h"
|
||||
#endif
|
||||
|
||||
const char* SSLeay_version(int type);
|
||||
|
||||
#define SSLEAY_NUMBER_DEFINED
|
||||
#define SSLEAY_VERSION 0x0900L
|
||||
#define SSLEAY_VERSION_NUMBER SSLEAY_VERSION
|
||||
|
||||
|
||||
#endif /* yaSSL_crypto_h__ */
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
/* des.h for openssl */
|
||||
@@ -0,0 +1 @@
|
||||
/* des_old.h for openvn */
|
||||
@@ -0,0 +1,5 @@
|
||||
/* engine.h for libcurl */
|
||||
|
||||
#undef HAVE_OPENSSL_ENGINE_H
|
||||
|
||||
|
||||
@@ -0,0 +1,8 @@
|
||||
/* err.h for openssl */
|
||||
|
||||
#ifndef yaSSL_err_h__
|
||||
#define yaSSL_err_h__
|
||||
|
||||
|
||||
|
||||
#endif /* yaSSL_err_h__ */
|
||||
@@ -0,0 +1,10 @@
|
||||
/* evp.h for openSSL */
|
||||
|
||||
#ifndef SSLEAY_NUMBER_DEFINED
|
||||
#define SSLEAY_NUMBER_DEFINED
|
||||
|
||||
/* for OpenVPN */
|
||||
#define SSLEAY_VERSION_NUMBER 0x0090700f
|
||||
|
||||
|
||||
#endif /* SSLEAY_NUMBER_DEFINED */
|
||||
@@ -0,0 +1,45 @@
|
||||
#!/usr/bin/perl
|
||||
#
|
||||
# This script generates defines for all functions
|
||||
# in yassl/include/openssl/ so they are renamed to
|
||||
# ya<old_function_name>. Hopefully that is unique enough.
|
||||
#
|
||||
# The script is to be run manually when we import
|
||||
# a new version of yaSSL
|
||||
#
|
||||
|
||||
|
||||
|
||||
# Find all functions in "input" and add macros
|
||||
# to prefix/rename them into "output
|
||||
sub generate_prefix($$)
|
||||
{
|
||||
my $input= shift;
|
||||
my $output= shift;
|
||||
open(IN, $input)
|
||||
or die("Can't open input file $input: $!");
|
||||
open(OUT, ">", $output)
|
||||
or mtr_error("Can't open output file $output: $!");
|
||||
|
||||
while (<IN>)
|
||||
{
|
||||
chomp;
|
||||
|
||||
if ( /typedef/ )
|
||||
{
|
||||
next;
|
||||
}
|
||||
|
||||
if ( /^\s*[a-zA-Z0-9*_ ]+\s+\*?([_a-zA-Z0-9]+)\s*\(/ )
|
||||
{
|
||||
print OUT "#define $1 ya$1\n";
|
||||
}
|
||||
}
|
||||
|
||||
close OUT;
|
||||
close IN;
|
||||
}
|
||||
|
||||
generate_prefix("ssl.h", "prefix_ssl.h");
|
||||
generate_prefix("crypto.h", "prefix_crypto.h");
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
/* hmac.h for openvpn */
|
||||
@@ -0,0 +1,2 @@
|
||||
/* lhash.h for openSSL */
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
/* md4.h for libcurl */
|
||||
@@ -0,0 +1,4 @@
|
||||
/* md5.h for openssl */
|
||||
|
||||
#include "ssl.h" /* in there for now */
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
/* objects.h for openvpn */
|
||||
@@ -0,0 +1,12 @@
|
||||
/* opensslv.h compatibility */
|
||||
|
||||
#ifndef yaSSL_opensslv_h__
|
||||
#define yaSSL_opensslv_h__
|
||||
|
||||
|
||||
/* api version compatibility */
|
||||
#define OPENSSL_VERSION_NUMBER 0x0090700f
|
||||
|
||||
|
||||
#endif /* yaSSLopensslv_h__ */
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
/* pem.h for libcurl */
|
||||
@@ -0,0 +1,5 @@
|
||||
/* pkcs12.h for libcurl */
|
||||
|
||||
|
||||
#undef HAVE_OPENSSL_PKCS12_H
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
#define SSLeay_version yaSSLeay_version
|
||||
@@ -0,0 +1,169 @@
|
||||
#define Copyright yaCopyright
|
||||
#define yaSSL_CleanUp yayaSSL_CleanUp
|
||||
#define BN_bin2bn yaBN_bin2bn
|
||||
#define DH_new yaDH_new
|
||||
#define DH_free yaDH_free
|
||||
#define RSA_free yaRSA_free
|
||||
#define RSA_generate_key yaRSA_generate_key
|
||||
#define X509_free yaX509_free
|
||||
#define X509_STORE_CTX_get_current_cert yaX509_STORE_CTX_get_current_cert
|
||||
#define X509_STORE_CTX_get_error yaX509_STORE_CTX_get_error
|
||||
#define X509_STORE_CTX_get_error_depth yaX509_STORE_CTX_get_error_depth
|
||||
#define X509_NAME_oneline yaX509_NAME_oneline
|
||||
#define X509_get_issuer_name yaX509_get_issuer_name
|
||||
#define X509_get_subject_name yaX509_get_subject_name
|
||||
#define X509_verify_cert_error_string yaX509_verify_cert_error_string
|
||||
#define X509_LOOKUP_add_dir yaX509_LOOKUP_add_dir
|
||||
#define X509_LOOKUP_load_file yaX509_LOOKUP_load_file
|
||||
#define X509_LOOKUP_hash_dir yaX509_LOOKUP_hash_dir
|
||||
#define X509_LOOKUP_file yaX509_LOOKUP_file
|
||||
#define X509_STORE_add_lookup yaX509_STORE_add_lookup
|
||||
#define X509_STORE_new yaX509_STORE_new
|
||||
#define X509_STORE_get_by_subject yaX509_STORE_get_by_subject
|
||||
#define ERR_get_error_line_data yaERR_get_error_line_data
|
||||
#define ERR_print_errors_fp yaERR_print_errors_fp
|
||||
#define ERR_error_string yaERR_error_string
|
||||
#define ERR_remove_state yaERR_remove_state
|
||||
#define ERR_get_error yaERR_get_error
|
||||
#define ERR_peek_error yaERR_peek_error
|
||||
#define ERR_GET_REASON yaERR_GET_REASON
|
||||
#define SSL_CTX_new yaSSL_CTX_new
|
||||
#define SSL_new yaSSL_new
|
||||
#define SSL_set_fd yaSSL_set_fd
|
||||
#define SSL_get_fd yaSSL_get_fd
|
||||
#define SSL_connect yaSSL_connect
|
||||
#define SSL_write yaSSL_write
|
||||
#define SSL_read yaSSL_read
|
||||
#define SSL_accept yaSSL_accept
|
||||
#define SSL_CTX_free yaSSL_CTX_free
|
||||
#define SSL_free yaSSL_free
|
||||
#define SSL_clear yaSSL_clear
|
||||
#define SSL_shutdown yaSSL_shutdown
|
||||
#define SSL_set_connect_state yaSSL_set_connect_state
|
||||
#define SSL_set_accept_state yaSSL_set_accept_state
|
||||
#define SSL_do_handshake yaSSL_do_handshake
|
||||
#define SSL_get_cipher yaSSL_get_cipher
|
||||
#define SSL_get_cipher_name yaSSL_get_cipher_name
|
||||
#define SSL_get_shared_ciphers yaSSL_get_shared_ciphers
|
||||
#define SSL_get_cipher_list yaSSL_get_cipher_list
|
||||
#define SSL_get_version yaSSL_get_version
|
||||
#define SSLeay_version yaSSLeay_version
|
||||
#define SSL_get_error yaSSL_get_error
|
||||
#define SSL_load_error_strings yaSSL_load_error_strings
|
||||
#define SSL_set_session yaSSL_set_session
|
||||
#define SSL_get_session yaSSL_get_session
|
||||
#define SSL_flush_sessions yaSSL_flush_sessions
|
||||
#define SSL_SESSION_set_timeout yaSSL_SESSION_set_timeout
|
||||
#define SSL_CTX_set_session_cache_mode yaSSL_CTX_set_session_cache_mode
|
||||
#define SSL_get_peer_certificate yaSSL_get_peer_certificate
|
||||
#define SSL_get_verify_result yaSSL_get_verify_result
|
||||
#define SSL_CTX_set_verify yaSSL_CTX_set_verify
|
||||
#define SSL_CTX_load_verify_locations yaSSL_CTX_load_verify_locations
|
||||
#define SSL_CTX_set_default_verify_paths yaSSL_CTX_set_default_verify_paths
|
||||
#define SSL_CTX_check_private_key yaSSL_CTX_check_private_key
|
||||
#define SSL_CTX_set_session_id_context yaSSL_CTX_set_session_id_context
|
||||
#define SSL_CTX_set_tmp_rsa_callback yaSSL_CTX_set_tmp_rsa_callback
|
||||
#define SSL_CTX_set_options yaSSL_CTX_set_options
|
||||
#define SSL_CTX_set_session_cache_mode yaSSL_CTX_set_session_cache_mode
|
||||
#define SSL_CTX_set_timeout yaSSL_CTX_set_timeout
|
||||
#define SSL_CTX_use_certificate_chain_file yaSSL_CTX_use_certificate_chain_file
|
||||
#define SSL_CTX_set_default_passwd_cb yaSSL_CTX_set_default_passwd_cb
|
||||
#define SSL_CTX_use_RSAPrivateKey_file yaSSL_CTX_use_RSAPrivateKey_file
|
||||
#define SSL_CTX_set_info_callback yaSSL_CTX_set_info_callback
|
||||
#define SSL_CTX_sess_accept yaSSL_CTX_sess_accept
|
||||
#define SSL_CTX_sess_connect yaSSL_CTX_sess_connect
|
||||
#define SSL_CTX_sess_accept_good yaSSL_CTX_sess_accept_good
|
||||
#define SSL_CTX_sess_connect_good yaSSL_CTX_sess_connect_good
|
||||
#define SSL_CTX_sess_accept_renegotiate yaSSL_CTX_sess_accept_renegotiate
|
||||
#define SSL_CTX_sess_connect_renegotiate yaSSL_CTX_sess_connect_renegotiate
|
||||
#define SSL_CTX_sess_hits yaSSL_CTX_sess_hits
|
||||
#define SSL_CTX_sess_cb_hits yaSSL_CTX_sess_cb_hits
|
||||
#define SSL_CTX_sess_cache_full yaSSL_CTX_sess_cache_full
|
||||
#define SSL_CTX_sess_misses yaSSL_CTX_sess_misses
|
||||
#define SSL_CTX_sess_timeouts yaSSL_CTX_sess_timeouts
|
||||
#define SSL_CTX_sess_number yaSSL_CTX_sess_number
|
||||
#define SSL_CTX_sess_get_cache_size yaSSL_CTX_sess_get_cache_size
|
||||
#define SSL_CTX_get_verify_mode yaSSL_CTX_get_verify_mode
|
||||
#define SSL_get_verify_mode yaSSL_get_verify_mode
|
||||
#define SSL_CTX_get_verify_depth yaSSL_CTX_get_verify_depth
|
||||
#define SSL_get_verify_depth yaSSL_get_verify_depth
|
||||
#define SSL_get_default_timeout yaSSL_get_default_timeout
|
||||
#define SSL_CTX_get_session_cache_mode yaSSL_CTX_get_session_cache_mode
|
||||
#define SSL_session_reused yaSSL_session_reused
|
||||
#define SSL_set_rfd yaSSL_set_rfd
|
||||
#define SSL_set_wfd yaSSL_set_wfd
|
||||
#define SSL_set_shutdown yaSSL_set_shutdown
|
||||
#define SSL_set_quiet_shutdown yaSSL_set_quiet_shutdown
|
||||
#define SSL_get_quiet_shutdown yaSSL_get_quiet_shutdown
|
||||
#define SSL_want_read yaSSL_want_read
|
||||
#define SSL_want_write yaSSL_want_write
|
||||
#define SSL_pending yaSSL_pending
|
||||
#define SSLv3_method yaSSLv3_method
|
||||
#define SSLv3_server_method yaSSLv3_server_method
|
||||
#define SSLv3_client_method yaSSLv3_client_method
|
||||
#define TLSv1_server_method yaTLSv1_server_method
|
||||
#define TLSv1_client_method yaTLSv1_client_method
|
||||
#define TLSv1_1_server_method yaTLSv1_1_server_method
|
||||
#define TLSv1_1_client_method yaTLSv1_1_client_method
|
||||
#define SSLv23_server_method yaSSLv23_server_method
|
||||
#define SSL_CTX_use_certificate_file yaSSL_CTX_use_certificate_file
|
||||
#define SSL_CTX_use_PrivateKey_file yaSSL_CTX_use_PrivateKey_file
|
||||
#define SSL_CTX_set_cipher_list yaSSL_CTX_set_cipher_list
|
||||
#define SSL_CTX_sess_set_cache_size yaSSL_CTX_sess_set_cache_size
|
||||
#define SSL_CTX_set_tmp_dh yaSSL_CTX_set_tmp_dh
|
||||
#define OpenSSL_add_all_algorithms yaOpenSSL_add_all_algorithms
|
||||
#define SSL_library_init yaSSL_library_init
|
||||
#define SSLeay_add_ssl_algorithms yaSSLeay_add_ssl_algorithms
|
||||
#define SSL_get_current_cipher yaSSL_get_current_cipher
|
||||
#define SSL_CIPHER_description yaSSL_CIPHER_description
|
||||
#define SSL_alert_type_string_long yaSSL_alert_type_string_long
|
||||
#define SSL_alert_desc_string_long yaSSL_alert_desc_string_long
|
||||
#define SSL_state_string_long yaSSL_state_string_long
|
||||
#define EVP_md5 yaEVP_md5
|
||||
#define EVP_des_ede3_cbc yaEVP_des_ede3_cbc
|
||||
#define EVP_BytesToKey yaEVP_BytesToKey
|
||||
#define DES_set_key_unchecked yaDES_set_key_unchecked
|
||||
#define DES_ede3_cbc_encrypt yaDES_ede3_cbc_encrypt
|
||||
#define RAND_screen yaRAND_screen
|
||||
#define RAND_file_name yaRAND_file_name
|
||||
#define RAND_write_file yaRAND_write_file
|
||||
#define RAND_load_file yaRAND_load_file
|
||||
#define RAND_status yaRAND_status
|
||||
#define RAND_bytes yaRAND_bytes
|
||||
#define DES_set_key yaDES_set_key
|
||||
#define DES_set_odd_parity yaDES_set_odd_parity
|
||||
#define DES_ecb_encrypt yaDES_ecb_encrypt
|
||||
#define SSL_CTX_set_default_passwd_cb_userdata yaSSL_CTX_set_default_passwd_cb_userdata
|
||||
#define SSL_SESSION_free yaSSL_SESSION_free
|
||||
#define SSL_peek yaSSL_peek
|
||||
#define SSL_get_certificate yaSSL_get_certificate
|
||||
#define SSL_get_privatekey yaSSL_get_privatekey
|
||||
#define X509_get_pubkey yaX509_get_pubkey
|
||||
#define EVP_PKEY_copy_parameters yaEVP_PKEY_copy_parameters
|
||||
#define EVP_PKEY_free yaEVP_PKEY_free
|
||||
#define ERR_error_string_n yaERR_error_string_n
|
||||
#define ERR_free_strings yaERR_free_strings
|
||||
#define EVP_cleanup yaEVP_cleanup
|
||||
#define X509_get_ext_d2i yaX509_get_ext_d2i
|
||||
#define GENERAL_NAMES_free yaGENERAL_NAMES_free
|
||||
#define sk_GENERAL_NAME_num yask_GENERAL_NAME_num
|
||||
#define sk_GENERAL_NAME_value yask_GENERAL_NAME_value
|
||||
#define ASN1_STRING_data yaASN1_STRING_data
|
||||
#define ASN1_STRING_length yaASN1_STRING_length
|
||||
#define ASN1_STRING_type yaASN1_STRING_type
|
||||
#define X509_NAME_get_index_by_NID yaX509_NAME_get_index_by_NID
|
||||
#define X509_NAME_ENTRY_get_data yaX509_NAME_ENTRY_get_data
|
||||
#define X509_NAME_get_entry yaX509_NAME_get_entry
|
||||
#define ASN1_STRING_to_UTF8 yaASN1_STRING_to_UTF8
|
||||
#define SSLv23_client_method yaSSLv23_client_method
|
||||
#define SSLv2_client_method yaSSLv2_client_method
|
||||
#define SSL_get1_session yaSSL_get1_session
|
||||
#define X509_get_notBefore yaX509_get_notBefore
|
||||
#define X509_get_notAfter yaX509_get_notAfter
|
||||
#define MD4_Init yaMD4_Init
|
||||
#define MD4_Update yaMD4_Update
|
||||
#define MD4_Final yaMD4_Final
|
||||
#define MD5_Init yaMD5_Init
|
||||
#define MD5_Update yaMD5_Update
|
||||
#define MD5_Final yaMD5_Final
|
||||
#define SSL_set_compression yaSSL_set_compression
|
||||
@@ -0,0 +1,2 @@
|
||||
/* rand.h for openSSL */
|
||||
|
||||
@@ -0,0 +1,10 @@
|
||||
/* rsa.h for openSSL */
|
||||
|
||||
|
||||
#ifndef yaSSL_rsa_h__
|
||||
#define yaSSL_rsa_h__
|
||||
|
||||
enum { RSA_F4 = 1 };
|
||||
|
||||
|
||||
#endif /* yaSSL_rsa_h__ */
|
||||
@@ -0,0 +1 @@
|
||||
/* sha.h for openvpn */
|
||||
+554
@@ -0,0 +1,554 @@
|
||||
/*
|
||||
Copyright (C) 2000-2007 MySQL AB
|
||||
|
||||
This program is free software; you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation; version 2 of the License.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program; see the file COPYING. If not, write to the
|
||||
Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
|
||||
MA 02110-1301 USA.
|
||||
*/
|
||||
|
||||
/* ssl.h defines openssl compatibility layer
|
||||
*
|
||||
*/
|
||||
|
||||
|
||||
|
||||
#ifndef yaSSL_openssl_h__
|
||||
#define yaSSL_openssl_h__
|
||||
|
||||
#ifdef YASSL_PREFIX
|
||||
#include "prefix_ssl.h"
|
||||
#endif
|
||||
|
||||
#include <stdio.h> /* ERR_print fp */
|
||||
#include "opensslv.h" /* for version number */
|
||||
#include "rsa.h"
|
||||
|
||||
|
||||
#define YASSL_VERSION "1.7.2"
|
||||
|
||||
|
||||
#if defined(__cplusplus)
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
void yaSSL_CleanUp(); /* call once at end of application use to
|
||||
free static singleton memory holders,
|
||||
not a leak per se, but helpful when
|
||||
looking for them */
|
||||
|
||||
#if defined(__cplusplus)
|
||||
} // extern
|
||||
#endif
|
||||
|
||||
#if defined(__cplusplus) && !defined(YASSL_MYSQL_COMPATIBLE)
|
||||
namespace yaSSL {
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#undef X509_NAME /* wincrypt.h clash */
|
||||
|
||||
#if defined(__cplusplus) && !defined(YASSL_MYSQL_COMPATIBLE)
|
||||
class SSL;
|
||||
class SSL_SESSION;
|
||||
class SSL_METHOD;
|
||||
class SSL_CTX;
|
||||
class SSL_CIPHER;
|
||||
|
||||
class RSA;
|
||||
|
||||
class X509;
|
||||
class X509_NAME;
|
||||
#else
|
||||
typedef struct SSL SSL;
|
||||
typedef struct SSL_SESSION SSL_SESSION;
|
||||
typedef struct SSL_METHOD SSL_METHOD;
|
||||
typedef struct SSL_CTX SSL_CTX;
|
||||
typedef struct SSL_CIPHER SSL_CIPHER;
|
||||
|
||||
typedef struct RSA RSA;
|
||||
|
||||
typedef struct X509 X509;
|
||||
typedef struct X509_NAME X509_NAME;
|
||||
#endif
|
||||
|
||||
|
||||
/* Big Number stuff, different file? */
|
||||
typedef struct BIGNUM BIGNUM;
|
||||
|
||||
BIGNUM *BN_bin2bn(const unsigned char*, int, BIGNUM*);
|
||||
|
||||
|
||||
/* Diffie-Hellman stuff, different file? */
|
||||
/* mySQL deferences to set group parameters */
|
||||
typedef struct DH {
|
||||
BIGNUM* p;
|
||||
BIGNUM* g;
|
||||
} DH;
|
||||
|
||||
DH* DH_new(void);
|
||||
void DH_free(DH*);
|
||||
|
||||
/* RSA stuff */
|
||||
|
||||
void RSA_free(RSA*);
|
||||
RSA* RSA_generate_key(int, unsigned long, void(*)(int, int, void*), void*);
|
||||
|
||||
|
||||
/* X509 stuff, different file? */
|
||||
|
||||
typedef struct X509_STORE X509_STORE;
|
||||
typedef struct X509_LOOKUP X509_LOOKUP;
|
||||
typedef struct X509_OBJECT { char c; } X509_OBJECT;
|
||||
typedef struct X509_CRL X509_CRL;
|
||||
typedef struct X509_REVOKED X509_REVOKED;
|
||||
typedef struct X509_LOOKUP_METHOD X509_LOOKUP_METHOD;
|
||||
|
||||
|
||||
void X509_free(X509*);
|
||||
|
||||
|
||||
/* bio stuff */
|
||||
typedef struct BIO BIO;
|
||||
|
||||
/* ASN stuff */
|
||||
|
||||
|
||||
|
||||
/* because mySQL dereferences to use error and current_cert, even after calling
|
||||
* get functions for local references */
|
||||
typedef struct X509_STORE_CTX {
|
||||
int error;
|
||||
int error_depth;
|
||||
X509* current_cert;
|
||||
} X509_STORE_CTX;
|
||||
|
||||
|
||||
|
||||
X509* X509_STORE_CTX_get_current_cert(X509_STORE_CTX*);
|
||||
int X509_STORE_CTX_get_error(X509_STORE_CTX*);
|
||||
int X509_STORE_CTX_get_error_depth(X509_STORE_CTX*);
|
||||
|
||||
char* X509_NAME_oneline(X509_NAME*, char*, int);
|
||||
X509_NAME* X509_get_issuer_name(X509*);
|
||||
X509_NAME* X509_get_subject_name(X509*);
|
||||
const char* X509_verify_cert_error_string(long);
|
||||
|
||||
int X509_LOOKUP_add_dir(X509_LOOKUP*, const char*, long);
|
||||
int X509_LOOKUP_load_file(X509_LOOKUP*, const char*, long);
|
||||
X509_LOOKUP_METHOD* X509_LOOKUP_hash_dir(void);
|
||||
X509_LOOKUP_METHOD* X509_LOOKUP_file(void);
|
||||
|
||||
X509_LOOKUP* X509_STORE_add_lookup(X509_STORE*, X509_LOOKUP_METHOD*);
|
||||
X509_STORE* X509_STORE_new(void);
|
||||
int X509_STORE_get_by_subject(X509_STORE_CTX*, int, X509_NAME*,
|
||||
X509_OBJECT*);
|
||||
|
||||
|
||||
|
||||
|
||||
enum { /* X509 Constants */
|
||||
X509_V_OK = 0,
|
||||
X509_V_ERR_CERT_CHAIN_TOO_LONG = 1,
|
||||
X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT = 2,
|
||||
X509_V_ERR_CERT_NOT_YET_VALID = 3,
|
||||
X509_V_ERR_ERROR_IN_CERT_NOT_BEFORE_FIELD = 4,
|
||||
X509_V_ERR_CERT_HAS_EXPIRED = 5,
|
||||
X509_V_ERR_ERROR_IN_CERT_NOT_AFTER_FIELD = 6,
|
||||
X509_FILETYPE_PEM = 7,
|
||||
X509_LU_X509 = 8,
|
||||
X509_LU_CRL = 9,
|
||||
X509_V_ERR_CRL_SIGNATURE_FAILURE = 10,
|
||||
X509_V_ERR_ERROR_IN_CRL_NEXT_UPDATE_FIELD = 11,
|
||||
X509_V_ERR_CRL_HAS_EXPIRED = 12,
|
||||
X509_V_ERR_CERT_REVOKED = 13,
|
||||
X509_V_FLAG_CRL_CHECK = 14,
|
||||
X509_V_FLAG_CRL_CHECK_ALL = 15
|
||||
};
|
||||
|
||||
|
||||
/* Error stuff, could move to yassl_error */
|
||||
unsigned long ERR_get_error_line_data(const char**, int*, const char**, int *);
|
||||
void ERR_print_errors_fp(FILE*);
|
||||
char* ERR_error_string(unsigned long,char*);
|
||||
void ERR_remove_state(unsigned long);
|
||||
unsigned long ERR_get_error(void);
|
||||
unsigned long ERR_peek_error(void);
|
||||
int ERR_GET_REASON(int);
|
||||
|
||||
|
||||
enum { /* ERR Constants */
|
||||
ERR_TXT_STRING = 1,
|
||||
EVP_R_BAD_DECRYPT = 2
|
||||
};
|
||||
|
||||
/*
|
||||
Allow type used by SSL_set_fd to be changed, default to int
|
||||
in order to be compatible with OpenSSL
|
||||
*/
|
||||
#ifndef YASSL_SOCKET_T_DEFINED
|
||||
typedef int YASSL_SOCKET_T;
|
||||
#endif
|
||||
|
||||
SSL_CTX* SSL_CTX_new(SSL_METHOD*);
|
||||
SSL* SSL_new(SSL_CTX*);
|
||||
int SSL_set_fd (SSL*, YASSL_SOCKET_T);
|
||||
YASSL_SOCKET_T SSL_get_fd(const SSL*);
|
||||
int SSL_connect(SSL*); /* if you get an error from connect
|
||||
see note at top of REAMDE */
|
||||
int SSL_write(SSL*, const void*, int);
|
||||
int SSL_read(SSL*, void*, int);
|
||||
int SSL_accept(SSL*);
|
||||
void SSL_CTX_free(SSL_CTX*);
|
||||
void SSL_free(SSL*);
|
||||
int SSL_clear(SSL*);
|
||||
int SSL_shutdown(SSL*);
|
||||
|
||||
void SSL_set_connect_state(SSL*);
|
||||
void SSL_set_accept_state(SSL*);
|
||||
int SSL_do_handshake(SSL*);
|
||||
|
||||
const char* SSL_get_cipher(SSL*);
|
||||
const char* SSL_get_cipher_name(SSL*); /* uses SSL_get_cipher */
|
||||
char* SSL_get_shared_ciphers(SSL*, char*, int);
|
||||
const char* SSL_get_cipher_list(SSL*, int);
|
||||
const char* SSL_get_version(SSL*);
|
||||
const char* SSLeay_version(int);
|
||||
|
||||
int SSL_get_error(SSL*, int);
|
||||
void SSL_load_error_strings(void);
|
||||
|
||||
int SSL_set_session(SSL *ssl, SSL_SESSION *session);
|
||||
SSL_SESSION* SSL_get_session(SSL* ssl);
|
||||
void SSL_flush_sessions(SSL_CTX *ctx, long tm);
|
||||
long SSL_SESSION_set_timeout(SSL_SESSION*, long);
|
||||
long SSL_CTX_set_session_cache_mode(SSL_CTX* ctx, long mode);
|
||||
X509* SSL_get_peer_certificate(SSL*);
|
||||
long SSL_get_verify_result(SSL*);
|
||||
|
||||
|
||||
typedef int (*VerifyCallback)(int, X509_STORE_CTX*);
|
||||
typedef int (*pem_password_cb)(char*, int, int, void*);
|
||||
|
||||
void SSL_CTX_set_verify(SSL_CTX*, int, VerifyCallback verify_callback);
|
||||
int SSL_CTX_load_verify_locations(SSL_CTX*, const char*, const char*);
|
||||
int SSL_CTX_set_default_verify_paths(SSL_CTX*);
|
||||
int SSL_CTX_check_private_key(SSL_CTX*);
|
||||
int SSL_CTX_set_session_id_context(SSL_CTX*, const unsigned char*,
|
||||
unsigned int);
|
||||
|
||||
void SSL_CTX_set_tmp_rsa_callback(SSL_CTX*, RSA*(*)(SSL*, int, int));
|
||||
long SSL_CTX_set_options(SSL_CTX*, long);
|
||||
long SSL_CTX_set_session_cache_mode(SSL_CTX*, long);
|
||||
long SSL_CTX_set_timeout(SSL_CTX*, long);
|
||||
int SSL_CTX_use_certificate_chain_file(SSL_CTX*, const char*);
|
||||
void SSL_CTX_set_default_passwd_cb(SSL_CTX*, pem_password_cb);
|
||||
int SSL_CTX_use_RSAPrivateKey_file(SSL_CTX*, const char*, int);
|
||||
void SSL_CTX_set_info_callback(SSL_CTX*, void (*)());
|
||||
|
||||
long SSL_CTX_sess_accept(SSL_CTX*);
|
||||
long SSL_CTX_sess_connect(SSL_CTX*);
|
||||
long SSL_CTX_sess_accept_good(SSL_CTX*);
|
||||
long SSL_CTX_sess_connect_good(SSL_CTX*);
|
||||
long SSL_CTX_sess_accept_renegotiate(SSL_CTX*);
|
||||
long SSL_CTX_sess_connect_renegotiate(SSL_CTX*);
|
||||
long SSL_CTX_sess_hits(SSL_CTX*);
|
||||
long SSL_CTX_sess_cb_hits(SSL_CTX*);
|
||||
long SSL_CTX_sess_cache_full(SSL_CTX*);
|
||||
long SSL_CTX_sess_misses(SSL_CTX*);
|
||||
long SSL_CTX_sess_timeouts(SSL_CTX*);
|
||||
long SSL_CTX_sess_number(SSL_CTX*);
|
||||
long SSL_CTX_sess_get_cache_size(SSL_CTX*);
|
||||
|
||||
int SSL_CTX_get_verify_mode(SSL_CTX*);
|
||||
int SSL_get_verify_mode(SSL*);
|
||||
int SSL_CTX_get_verify_depth(SSL_CTX*);
|
||||
int SSL_get_verify_depth(SSL*);
|
||||
|
||||
long SSL_get_default_timeout(SSL*);
|
||||
long SSL_CTX_get_session_cache_mode(SSL_CTX*);
|
||||
int SSL_session_reused(SSL*);
|
||||
|
||||
int SSL_set_rfd(SSL*, int);
|
||||
int SSL_set_wfd(SSL*, int);
|
||||
void SSL_set_shutdown(SSL*, int);
|
||||
void SSL_set_quiet_shutdown(SSL *ssl,int mode);
|
||||
int SSL_get_quiet_shutdown(SSL *ssl);
|
||||
|
||||
int SSL_want_read(SSL*);
|
||||
int SSL_want_write(SSL*);
|
||||
|
||||
int SSL_pending(SSL*);
|
||||
|
||||
|
||||
enum { /* ssl Constants */
|
||||
SSL_WOULD_BLOCK = -8,
|
||||
SSL_BAD_STAT = -7,
|
||||
SSL_BAD_PATH = -6,
|
||||
SSL_BAD_FILETYPE = -5,
|
||||
SSL_BAD_FILE = -4,
|
||||
SSL_NOT_IMPLEMENTED = -3,
|
||||
SSL_UNKNOWN = -2,
|
||||
SSL_FATAL_ERROR = -1,
|
||||
SSL_NORMAL_SHUTDOWN = 0,
|
||||
SSL_ERROR_NONE = 0, /* for most functions */
|
||||
SSL_FAILURE = 0, /* for some functions */
|
||||
SSL_SUCCESS = 1,
|
||||
|
||||
SSL_FILETYPE_ASN1 = 10,
|
||||
SSL_FILETYPE_PEM = 11,
|
||||
SSL_FILETYPE_DEFAULT = 10, /* ASN1 */
|
||||
|
||||
SSL_VERIFY_NONE = 0,
|
||||
SSL_VERIFY_PEER = 1,
|
||||
SSL_VERIFY_FAIL_IF_NO_PEER_CERT = 2,
|
||||
SSL_VERIFY_CLIENT_ONCE = 4,
|
||||
|
||||
SSL_SESS_CACHE_OFF = 30,
|
||||
SSL_SESS_CACHE_CLIENT = 31,
|
||||
SSL_SESS_CACHE_SERVER = 32,
|
||||
SSL_SESS_CACHE_BOTH = 33,
|
||||
SSL_SESS_CACHE_NO_AUTO_CLEAR = 34,
|
||||
SSL_SESS_CACHE_NO_INTERNAL_LOOKUP = 35,
|
||||
|
||||
SSL_OP_MICROSOFT_SESS_ID_BUG = 50,
|
||||
SSL_OP_NETSCAPE_CHALLENGE_BUG = 51,
|
||||
SSL_OP_NETSCAPE_REUSE_CIPHER_CHANGE_BUG = 52,
|
||||
SSL_OP_SSLREF2_REUSE_CERT_TYPE_BUG = 53,
|
||||
SSL_OP_MICROSOFT_BIG_SSLV3_BUFFER = 54,
|
||||
SSL_OP_MSIE_SSLV2_RSA_PADDING = 55,
|
||||
SSL_OP_SSLEAY_080_CLIENT_DH_BUG = 56,
|
||||
SSL_OP_TLS_D5_BUG = 57,
|
||||
SSL_OP_TLS_BLOCK_PADDING_BUG = 58,
|
||||
SSL_OP_TLS_ROLLBACK_BUG = 59,
|
||||
SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS = 60,
|
||||
SSL_OP_ALL = 61,
|
||||
SSL_OP_SINGLE_DH_USE = 62,
|
||||
SSL_OP_EPHEMERAL_RSA = 63,
|
||||
SSL_OP_NO_SSLv2 = 64,
|
||||
SSL_OP_NO_SSLv3 = 65,
|
||||
SSL_OP_NO_TLSv1 = 66,
|
||||
SSL_OP_PKCS1_CHECK_1 = 67,
|
||||
SSL_OP_PKCS1_CHECK_2 = 68,
|
||||
SSL_OP_NETSCAPE_CA_DN_BUG = 69,
|
||||
SSL_OP_NON_EXPORT_FIRST = 70,
|
||||
SSL_OP_NETSCAPE_DEMO_CIPHER_CHANGE_BUG = 71,
|
||||
|
||||
SSL_ERROR_WANT_READ = 80,
|
||||
SSL_ERROR_WANT_WRITE = 81,
|
||||
SSL_ERROR_SYSCALL = 82,
|
||||
SSL_ERROR_WANT_X509_LOOKUP = 83,
|
||||
SSL_ERROR_ZERO_RETURN = 84,
|
||||
SSL_ERROR_SSL = 85,
|
||||
|
||||
SSL_SENT_SHUTDOWN = 90,
|
||||
SSL_RECEIVED_SHUTDOWN = 91,
|
||||
SSL_CB_LOOP = 92,
|
||||
SSL_ST_CONNECT = 93,
|
||||
SSL_ST_ACCEPT = 94,
|
||||
SSL_CB_ALERT = 95,
|
||||
SSL_CB_READ = 96,
|
||||
SSL_CB_HANDSHAKE_DONE = 97
|
||||
|
||||
};
|
||||
|
||||
|
||||
SSL_METHOD *SSLv3_method(void);
|
||||
SSL_METHOD *SSLv3_server_method(void);
|
||||
SSL_METHOD *SSLv3_client_method(void);
|
||||
SSL_METHOD *TLSv1_server_method(void);
|
||||
SSL_METHOD *TLSv1_client_method(void);
|
||||
SSL_METHOD *TLSv1_1_server_method(void);
|
||||
SSL_METHOD *TLSv1_1_client_method(void);
|
||||
SSL_METHOD *SSLv23_server_method(void);
|
||||
|
||||
int SSL_CTX_use_certificate_file(SSL_CTX*, const char*, int);
|
||||
int SSL_CTX_use_PrivateKey_file(SSL_CTX*, const char*, int);
|
||||
int SSL_CTX_set_cipher_list(SSL_CTX*, const char*);
|
||||
|
||||
long SSL_CTX_sess_set_cache_size(SSL_CTX*, long);
|
||||
long SSL_CTX_set_tmp_dh(SSL_CTX*, DH*);
|
||||
|
||||
void OpenSSL_add_all_algorithms(void);
|
||||
int SSL_library_init();
|
||||
int SSLeay_add_ssl_algorithms(void);
|
||||
|
||||
|
||||
SSL_CIPHER* SSL_get_current_cipher(SSL*);
|
||||
char* SSL_CIPHER_description(SSL_CIPHER*, char*, int);
|
||||
|
||||
|
||||
char* SSL_alert_type_string_long(int);
|
||||
char* SSL_alert_desc_string_long(int);
|
||||
char* SSL_state_string_long(SSL*);
|
||||
|
||||
|
||||
/* EVP stuff, des and md5, different file? */
|
||||
typedef char EVP_MD;
|
||||
|
||||
typedef char EVP_CIPHER;
|
||||
|
||||
typedef struct EVP_PKEY EVP_PKEY;
|
||||
|
||||
typedef unsigned char DES_cblock[8];
|
||||
typedef const DES_cblock const_DES_cblock;
|
||||
typedef DES_cblock DES_key_schedule;
|
||||
|
||||
enum {
|
||||
DES_ENCRYPT = 1,
|
||||
DES_DECRYPT = 0
|
||||
};
|
||||
|
||||
const EVP_MD* EVP_md5(void);
|
||||
const EVP_CIPHER* EVP_des_ede3_cbc(void);
|
||||
|
||||
typedef unsigned char opaque;
|
||||
|
||||
int EVP_BytesToKey(const EVP_CIPHER*, const EVP_MD*, const opaque*,
|
||||
const opaque*, int, int, opaque*, opaque*);
|
||||
|
||||
void DES_set_key_unchecked(const_DES_cblock*, DES_key_schedule*);
|
||||
void DES_ede3_cbc_encrypt(const opaque*, opaque*, long, DES_key_schedule*,
|
||||
DES_key_schedule*, DES_key_schedule*, DES_cblock*, int);
|
||||
|
||||
|
||||
/* RAND stuff */
|
||||
void RAND_screen(void);
|
||||
const char* RAND_file_name(char*, size_t);
|
||||
int RAND_write_file(const char*);
|
||||
int RAND_load_file(const char*, long);
|
||||
|
||||
|
||||
/* for libcurl */
|
||||
int RAND_status(void);
|
||||
int RAND_bytes(unsigned char* buf, int num);
|
||||
|
||||
int DES_set_key(const_DES_cblock*, DES_key_schedule*);
|
||||
void DES_set_odd_parity(DES_cblock*);
|
||||
void DES_ecb_encrypt(DES_cblock*, DES_cblock*, DES_key_schedule*, int);
|
||||
|
||||
void SSL_CTX_set_default_passwd_cb_userdata(SSL_CTX*, void* userdata);
|
||||
void SSL_SESSION_free(SSL_SESSION* session);
|
||||
int SSL_peek(SSL* ssl, void* buf, int num);
|
||||
|
||||
X509* SSL_get_certificate(SSL* ssl);
|
||||
EVP_PKEY* SSL_get_privatekey(SSL* ssl);
|
||||
EVP_PKEY* X509_get_pubkey(X509* x);
|
||||
|
||||
int EVP_PKEY_copy_parameters(EVP_PKEY* to, const EVP_PKEY* from);
|
||||
void EVP_PKEY_free(EVP_PKEY* pkey);
|
||||
void ERR_error_string_n(unsigned long e, char *buf, size_t len);
|
||||
void ERR_free_strings(void);
|
||||
void EVP_cleanup(void);
|
||||
|
||||
void* X509_get_ext_d2i(X509* x, int nid, int* crit, int* idx);
|
||||
|
||||
#define GEN_IPADD 7
|
||||
#define NID_subject_alt_name 85
|
||||
#define STACK_OF(x) x
|
||||
|
||||
|
||||
/* defined here because libcurl dereferences */
|
||||
typedef struct ASN1_STRING {
|
||||
int type;
|
||||
int length;
|
||||
unsigned char* data;
|
||||
} ASN1_STRING;
|
||||
|
||||
|
||||
typedef struct GENERAL_NAME {
|
||||
int type;
|
||||
union {
|
||||
ASN1_STRING* ia5;
|
||||
} d;
|
||||
} GENERAL_NAME;
|
||||
|
||||
void GENERAL_NAMES_free(STACK_OF(GENERAL_NAME) *x);
|
||||
|
||||
int sk_GENERAL_NAME_num(STACK_OF(GENERAL_NAME) *x);
|
||||
GENERAL_NAME* sk_GENERAL_NAME_value(STACK_OF(GENERAL_NAME) *x, int i);
|
||||
|
||||
|
||||
unsigned char* ASN1_STRING_data(ASN1_STRING* x);
|
||||
int ASN1_STRING_length(ASN1_STRING* x);
|
||||
int ASN1_STRING_type(ASN1_STRING *x);
|
||||
|
||||
typedef ASN1_STRING X509_NAME_ENTRY;
|
||||
|
||||
int X509_NAME_get_index_by_NID(X509_NAME* name,int nid, int lastpos);
|
||||
|
||||
ASN1_STRING* X509_NAME_ENTRY_get_data(X509_NAME_ENTRY* ne);
|
||||
X509_NAME_ENTRY* X509_NAME_get_entry(X509_NAME* name, int loc);
|
||||
|
||||
#define OPENSSL_malloc(x) malloc(x)
|
||||
#define OPENSSL_free(x) free(x)
|
||||
|
||||
int ASN1_STRING_to_UTF8(unsigned char** out, ASN1_STRING* in);
|
||||
|
||||
SSL_METHOD* SSLv23_client_method(void); /* doesn't actually roll back */
|
||||
SSL_METHOD* SSLv2_client_method(void); /* will never work, no v 2 */
|
||||
|
||||
|
||||
SSL_SESSION* SSL_get1_session(SSL* ssl); /* what's ref count */
|
||||
|
||||
|
||||
#define CRYPTO_free(x) free(x)
|
||||
#define ASN1_TIME ASN1_STRING
|
||||
|
||||
ASN1_TIME* X509_get_notBefore(X509* x);
|
||||
ASN1_TIME* X509_get_notAfter(X509* x);
|
||||
|
||||
|
||||
#define ASN1_UTCTIME ASN1_STRING
|
||||
#define NID_commonName 13
|
||||
#define V_ASN1_UTF8STRING 12
|
||||
#define GEN_DNS 2
|
||||
|
||||
#define CERTFICATE_ERROR 0x14090086 /* SSLv3 error */
|
||||
|
||||
|
||||
typedef struct MD4_CTX {
|
||||
int buffer[32]; /* big enough to hold, check size in Init */
|
||||
} MD4_CTX;
|
||||
|
||||
void MD4_Init(MD4_CTX*);
|
||||
void MD4_Update(MD4_CTX*, const void*, unsigned long);
|
||||
void MD4_Final(unsigned char*, MD4_CTX*);
|
||||
|
||||
|
||||
typedef struct MD5_CTX {
|
||||
int buffer[32]; /* big enough to hold, check size in Init */
|
||||
} MD5_CTX;
|
||||
|
||||
void MD5_Init(MD5_CTX*);
|
||||
void MD5_Update(MD5_CTX*, const void*, unsigned long);
|
||||
void MD5_Final(unsigned char*, MD5_CTX*);
|
||||
|
||||
#define MD5_DIGEST_LENGTH 16
|
||||
|
||||
|
||||
#define SSL_DEFAULT_CIPHER_LIST "" /* default all */
|
||||
|
||||
|
||||
/* yaSSL adds */
|
||||
int SSL_set_compression(SSL*); /* turn on yaSSL zlib compression */
|
||||
|
||||
|
||||
|
||||
|
||||
#if defined(__cplusplus) && !defined(YASSL_MYSQL_COMPATIBLE)
|
||||
} /* namespace */
|
||||
} /* extern "C" */
|
||||
#endif
|
||||
|
||||
|
||||
#endif /* yaSSL_openssl_h__ */
|
||||
@@ -0,0 +1 @@
|
||||
/* x509.h for libcurl */
|
||||
@@ -0,0 +1 @@
|
||||
/* x509v3.h for libcurl */
|
||||
@@ -0,0 +1,96 @@
|
||||
/*
|
||||
Copyright (C) 2000-2007 MySQL AB
|
||||
|
||||
This program is free software; you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation; version 2 of the License.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program; see the file COPYING. If not, write to the
|
||||
Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
|
||||
MA 02110-1301 USA.
|
||||
*/
|
||||
|
||||
|
||||
/* The socket wrapper header defines a Socket class that hides the differences
|
||||
* between Berkely style sockets and Windows sockets, allowing transparent TCP
|
||||
* access.
|
||||
*/
|
||||
|
||||
|
||||
#ifndef yaSSL_SOCKET_WRAPPER_HPP
|
||||
#define yaSSL_SOCKET_WRAPPER_HPP
|
||||
|
||||
#include <assert.h>
|
||||
|
||||
#ifdef _WIN32
|
||||
#include <winsock2.h>
|
||||
#else
|
||||
#include <sys/time.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/socket.h>
|
||||
#include <unistd.h>
|
||||
#include <netinet/in.h>
|
||||
#include <arpa/inet.h>
|
||||
#endif
|
||||
|
||||
|
||||
namespace yaSSL {
|
||||
|
||||
typedef unsigned int uint;
|
||||
|
||||
#ifdef _WIN32
|
||||
typedef SOCKET socket_t;
|
||||
#else
|
||||
typedef int socket_t;
|
||||
const socket_t INVALID_SOCKET = -1;
|
||||
const int SD_RECEIVE = 0;
|
||||
const int SD_SEND = 1;
|
||||
const int SD_BOTH = 2;
|
||||
const int SOCKET_ERROR = -1;
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
typedef unsigned char byte;
|
||||
|
||||
|
||||
// Wraps Windows Sockets and BSD Sockets
|
||||
class Socket {
|
||||
socket_t socket_; // underlying socket descriptor
|
||||
bool wouldBlock_; // if non-blocking data, for last read
|
||||
bool nonBlocking_; // is option set
|
||||
public:
|
||||
explicit Socket(socket_t s = INVALID_SOCKET);
|
||||
~Socket();
|
||||
|
||||
void set_fd(socket_t s);
|
||||
uint get_ready() const;
|
||||
socket_t get_fd() const;
|
||||
|
||||
uint send(const byte* buf, unsigned int len, int flags = 0) const;
|
||||
uint receive(byte* buf, unsigned int len, int flags = 0);
|
||||
|
||||
bool wait();
|
||||
bool WouldBlock() const;
|
||||
bool IsNonBlocking() const;
|
||||
|
||||
void closeSocket();
|
||||
void shutDown(int how = SD_SEND);
|
||||
|
||||
static int get_lastError();
|
||||
static void set_lastError(int error);
|
||||
private:
|
||||
Socket(const Socket&); // hide copy
|
||||
Socket& operator= (const Socket&); // and assign
|
||||
};
|
||||
|
||||
|
||||
} // naemspace
|
||||
|
||||
#endif // yaSSL_SOCKET_WRAPPER_HPP
|
||||
+40
@@ -0,0 +1,40 @@
|
||||
/*
|
||||
Copyright (C) 2000-2007 MySQL AB
|
||||
|
||||
This program is free software; you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation; version 2 of the License.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program; see the file COPYING. If not, write to the
|
||||
Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
|
||||
MA 02110-1301 USA.
|
||||
*/
|
||||
|
||||
/* timer.hpp provides a high res and low res timers
|
||||
*
|
||||
*/
|
||||
|
||||
|
||||
#ifndef yaSSL_TIMER_HPP
|
||||
#define yaSSL_TIMER_HPP
|
||||
|
||||
namespace yaSSL {
|
||||
|
||||
typedef double timer_d;
|
||||
typedef unsigned int uint;
|
||||
|
||||
|
||||
|
||||
timer_d timer();
|
||||
uint lowResTimer();
|
||||
|
||||
|
||||
|
||||
} // namespace
|
||||
#endif // yaSSL_TIMER_HPP
|
||||
+85
@@ -0,0 +1,85 @@
|
||||
/*
|
||||
Copyright (C) 2000-2007 MySQL AB
|
||||
|
||||
This program is free software; you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation; version 2 of the License.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program; see the file COPYING. If not, write to the
|
||||
Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
|
||||
MA 02110-1301 USA.
|
||||
*/
|
||||
|
||||
|
||||
/* yaSSL externel header defines yaSSL API
|
||||
*/
|
||||
|
||||
|
||||
#ifndef yaSSL_EXT_HPP
|
||||
#define yaSSL_EXT_HPP
|
||||
|
||||
|
||||
namespace yaSSL {
|
||||
|
||||
|
||||
#ifdef _WIN32
|
||||
typedef unsigned int SOCKET_T;
|
||||
#else
|
||||
typedef int SOCKET_T;
|
||||
#endif
|
||||
|
||||
|
||||
class Client {
|
||||
public:
|
||||
Client();
|
||||
~Client();
|
||||
|
||||
// basics
|
||||
int Connect(SOCKET_T);
|
||||
int Write(const void*, int);
|
||||
int Read(void*, int);
|
||||
|
||||
// options
|
||||
void SetCA(const char*);
|
||||
void SetCert(const char*);
|
||||
void SetKey(const char*);
|
||||
private:
|
||||
struct ClientImpl;
|
||||
ClientImpl* pimpl_;
|
||||
|
||||
Client(const Client&); // hide copy
|
||||
Client& operator=(const Client&); // and assign
|
||||
};
|
||||
|
||||
|
||||
class Server {
|
||||
public:
|
||||
Server();
|
||||
~Server();
|
||||
|
||||
// basics
|
||||
int Accept(SOCKET_T);
|
||||
int Write(const void*, int);
|
||||
int Read(void*, int);
|
||||
|
||||
// options
|
||||
void SetCA(const char*);
|
||||
void SetCert(const char*);
|
||||
void SetKey(const char*);
|
||||
private:
|
||||
struct ServerImpl;
|
||||
ServerImpl* pimpl_;
|
||||
|
||||
Server(const Server&); // hide copy
|
||||
Server& operator=(const Server&); // and assign
|
||||
};
|
||||
|
||||
|
||||
} // namespace yaSSL
|
||||
#endif // yaSSL_EXT_HPP
|
||||
@@ -0,0 +1,86 @@
|
||||
/*
|
||||
Copyright (C) 2000-2007 MySQL AB
|
||||
|
||||
This program is free software; you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation; version 2 of the License.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program; see the file COPYING. If not, write to the
|
||||
Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
|
||||
MA 02110-1301 USA.
|
||||
*/
|
||||
|
||||
|
||||
/* yaSSL error header defines error codes and an exception class
|
||||
*/
|
||||
|
||||
#ifndef yaSSL_ERROR_HPP
|
||||
#define yaSSL_ERROR_HPP
|
||||
|
||||
|
||||
|
||||
namespace yaSSL {
|
||||
|
||||
|
||||
enum YasslError {
|
||||
no_error = 0,
|
||||
|
||||
// 10 - 47 from AlertDescription, 0 also close_notify
|
||||
|
||||
range_error = 101,
|
||||
realloc_error = 102,
|
||||
factory_error = 103,
|
||||
unknown_cipher = 104,
|
||||
prefix_error = 105,
|
||||
record_layer = 106,
|
||||
handshake_layer = 107,
|
||||
out_of_order = 108,
|
||||
bad_input = 109,
|
||||
match_error = 110,
|
||||
no_key_file = 111,
|
||||
verify_error = 112,
|
||||
send_error = 113,
|
||||
receive_error = 114,
|
||||
certificate_error = 115,
|
||||
privateKey_error = 116,
|
||||
badVersion_error = 117,
|
||||
compress_error = 118,
|
||||
decompress_error = 119,
|
||||
pms_version_error = 120
|
||||
|
||||
// !!!! add error message to .cpp !!!!
|
||||
|
||||
// 1000+ from TaoCrypt error.hpp
|
||||
|
||||
};
|
||||
|
||||
|
||||
enum Library { yaSSL_Lib = 0, CryptoLib, SocketLib };
|
||||
enum { MAX_ERROR_SZ = 80 };
|
||||
|
||||
void SetErrorString(YasslError, char*);
|
||||
|
||||
/* remove for now, if go back to exceptions use this wrapper
|
||||
// Base class for all yaSSL exceptions
|
||||
class Error : public mySTL::runtime_error {
|
||||
YasslError error_;
|
||||
Library lib_;
|
||||
public:
|
||||
explicit Error(const char* s = "", YasslError e = no_error,
|
||||
Library l = yaSSL_Lib);
|
||||
|
||||
YasslError get_number() const;
|
||||
Library get_lib() const;
|
||||
};
|
||||
*/
|
||||
|
||||
|
||||
} // naemspace
|
||||
|
||||
#endif // yaSSL_ERROR_HPP
|
||||
+748
@@ -0,0 +1,748 @@
|
||||
/*
|
||||
Copyright (C) 2000-2007 MySQL AB
|
||||
|
||||
This program is free software; you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation; version 2 of the License.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program; see the file COPYING. If not, write to the
|
||||
Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
|
||||
MA 02110-1301 USA.
|
||||
*/
|
||||
|
||||
/* yaSSL implementation header defines all strucutres from the SSL.v3
|
||||
* specification "draft-freier-ssl-version3-02.txt"
|
||||
* all page citations refer to this document unless otherwise noted.
|
||||
*/
|
||||
|
||||
|
||||
#ifndef yaSSL_IMP_HPP
|
||||
#define yaSSL_IMP_HPP
|
||||
|
||||
#ifdef _MSC_VER
|
||||
// disable truncated debug symbols
|
||||
#pragma warning(disable:4786)
|
||||
#endif
|
||||
|
||||
#include "yassl_types.hpp"
|
||||
#include "factory.hpp"
|
||||
#include STL_LIST_FILE
|
||||
|
||||
|
||||
namespace STL = STL_NAMESPACE;
|
||||
|
||||
|
||||
namespace yaSSL {
|
||||
|
||||
|
||||
class SSL; // forward decls
|
||||
class input_buffer;
|
||||
class output_buffer;
|
||||
|
||||
|
||||
struct ProtocolVersion {
|
||||
uint8 major_;
|
||||
uint8 minor_; // major and minor SSL/TLS version numbers
|
||||
|
||||
ProtocolVersion(uint8 maj = 3, uint8 min = 0);
|
||||
};
|
||||
|
||||
|
||||
// Record Layer Header for PlainText, Compressed, and CipherText
|
||||
struct RecordLayerHeader {
|
||||
ContentType type_;
|
||||
ProtocolVersion version_;
|
||||
uint16 length_; // should not exceed 2^14
|
||||
};
|
||||
|
||||
|
||||
// base for all messages
|
||||
struct Message : public virtual_base {
|
||||
virtual input_buffer& set(input_buffer&) =0;
|
||||
virtual output_buffer& get(output_buffer&) const =0;
|
||||
|
||||
virtual void Process(input_buffer&, SSL&) =0;
|
||||
virtual ContentType get_type() const =0;
|
||||
virtual uint16 get_length() const =0;
|
||||
|
||||
virtual ~Message() {}
|
||||
};
|
||||
|
||||
|
||||
class ChangeCipherSpec : public Message {
|
||||
CipherChoice type_;
|
||||
public:
|
||||
ChangeCipherSpec();
|
||||
|
||||
friend input_buffer& operator>>(input_buffer&, ChangeCipherSpec&);
|
||||
friend output_buffer& operator<<(output_buffer&, const ChangeCipherSpec&);
|
||||
|
||||
input_buffer& set(input_buffer& in);
|
||||
output_buffer& get(output_buffer& out) const;
|
||||
|
||||
ContentType get_type() const;
|
||||
uint16 get_length() const;
|
||||
void Process(input_buffer&, SSL&);
|
||||
private:
|
||||
ChangeCipherSpec(const ChangeCipherSpec&); // hide copy
|
||||
ChangeCipherSpec& operator=(const ChangeCipherSpec&); // and assign
|
||||
};
|
||||
|
||||
|
||||
|
||||
class Alert : public Message {
|
||||
AlertLevel level_;
|
||||
AlertDescription description_;
|
||||
public:
|
||||
Alert() {}
|
||||
Alert(AlertLevel al, AlertDescription ad);
|
||||
|
||||
ContentType get_type() const;
|
||||
uint16 get_length() const;
|
||||
void Process(input_buffer&, SSL&);
|
||||
|
||||
friend input_buffer& operator>>(input_buffer&, Alert&);
|
||||
friend output_buffer& operator<<(output_buffer&, const Alert&);
|
||||
|
||||
input_buffer& set(input_buffer& in);
|
||||
output_buffer& get(output_buffer& out) const;
|
||||
private:
|
||||
Alert(const Alert&); // hide copy
|
||||
Alert& operator=(const Alert&); // and assign
|
||||
};
|
||||
|
||||
|
||||
class Data : public Message {
|
||||
uint16 length_;
|
||||
opaque* buffer_; // read buffer used by fillData input
|
||||
const opaque* write_buffer_; // write buffer used by output operator
|
||||
public:
|
||||
Data();
|
||||
Data(uint16 len, opaque* b);
|
||||
|
||||
friend output_buffer& operator<<(output_buffer&, const Data&);
|
||||
|
||||
input_buffer& set(input_buffer& in);
|
||||
output_buffer& get(output_buffer& out) const;
|
||||
|
||||
ContentType get_type() const;
|
||||
uint16 get_length() const;
|
||||
void set_length(uint16 l);
|
||||
opaque* set_buffer();
|
||||
void SetData(uint16, const opaque*);
|
||||
void Process(input_buffer&, SSL&);
|
||||
private:
|
||||
Data(const Data&); // hide copy
|
||||
Data& operator=(const Data&); // and assign
|
||||
};
|
||||
|
||||
|
||||
uint32 c24to32(const uint24); // forward form internal header
|
||||
void c32to24(uint32, uint24&);
|
||||
|
||||
|
||||
// HandShake header, same for each message type from page 20/21
|
||||
class HandShakeHeader : public Message {
|
||||
HandShakeType type_;
|
||||
uint24 length_; // length of message
|
||||
public:
|
||||
HandShakeHeader() {}
|
||||
|
||||
ContentType get_type() const;
|
||||
uint16 get_length() const;
|
||||
HandShakeType get_handshakeType() const;
|
||||
void Process(input_buffer&, SSL&);
|
||||
|
||||
void set_type(HandShakeType hst);
|
||||
void set_length(uint32 u32);
|
||||
|
||||
friend input_buffer& operator>>(input_buffer&, HandShakeHeader&);
|
||||
friend output_buffer& operator<<(output_buffer&, const HandShakeHeader&);
|
||||
|
||||
input_buffer& set(input_buffer& in);
|
||||
output_buffer& get(output_buffer& out) const;
|
||||
private:
|
||||
HandShakeHeader(const HandShakeHeader&); // hide copy
|
||||
HandShakeHeader& operator=(const HandShakeHeader&); // and assign
|
||||
};
|
||||
|
||||
|
||||
// Base Class for all handshake messages
|
||||
class HandShakeBase : public virtual_base {
|
||||
int length_;
|
||||
public:
|
||||
int get_length() const;
|
||||
void set_length(int);
|
||||
|
||||
// for building buffer's type field
|
||||
virtual HandShakeType get_type() const =0;
|
||||
|
||||
// handles dispactch of proper >>
|
||||
virtual input_buffer& set(input_buffer& in) =0;
|
||||
virtual output_buffer& get(output_buffer& out) const =0;
|
||||
|
||||
virtual void Process(input_buffer&, SSL&) =0;
|
||||
|
||||
virtual ~HandShakeBase() {}
|
||||
};
|
||||
|
||||
|
||||
struct HelloRequest : public HandShakeBase {
|
||||
input_buffer& set(input_buffer& in);
|
||||
output_buffer& get(output_buffer& out) const;
|
||||
|
||||
void Process(input_buffer&, SSL&);
|
||||
|
||||
HandShakeType get_type() const;
|
||||
};
|
||||
|
||||
|
||||
// The Client's Hello Message from page 23
|
||||
class ClientHello : public HandShakeBase {
|
||||
ProtocolVersion client_version_;
|
||||
Random random_;
|
||||
uint8 id_len_; // session id length
|
||||
opaque session_id_[ID_LEN];
|
||||
uint16 suite_len_; // cipher suite length
|
||||
opaque cipher_suites_[MAX_SUITE_SZ];
|
||||
uint8 comp_len_; // compression length
|
||||
CompressionMethod compression_methods_;
|
||||
public:
|
||||
friend input_buffer& operator>>(input_buffer&, ClientHello&);
|
||||
friend output_buffer& operator<<(output_buffer&, const ClientHello&);
|
||||
|
||||
input_buffer& set(input_buffer& in);
|
||||
output_buffer& get(output_buffer& out) const;
|
||||
|
||||
HandShakeType get_type() const;
|
||||
void Process(input_buffer&, SSL&);
|
||||
|
||||
const opaque* get_random() const;
|
||||
friend void buildClientHello(SSL&, ClientHello&);
|
||||
friend void ProcessOldClientHello(input_buffer& input, SSL& ssl);
|
||||
|
||||
ClientHello();
|
||||
ClientHello(ProtocolVersion pv, bool useCompression);
|
||||
private:
|
||||
ClientHello(const ClientHello&); // hide copy
|
||||
ClientHello& operator=(const ClientHello&); // and assign
|
||||
};
|
||||
|
||||
|
||||
|
||||
// The Server's Hello Message from page 24
|
||||
class ServerHello : public HandShakeBase {
|
||||
ProtocolVersion server_version_;
|
||||
Random random_;
|
||||
uint8 id_len_; // session id length
|
||||
opaque session_id_[ID_LEN];
|
||||
opaque cipher_suite_[SUITE_LEN];
|
||||
CompressionMethod compression_method_;
|
||||
public:
|
||||
ServerHello(ProtocolVersion pv, bool useCompression);
|
||||
ServerHello();
|
||||
|
||||
friend input_buffer& operator>>(input_buffer&, ServerHello&);
|
||||
friend output_buffer& operator<<(output_buffer&, const ServerHello&);
|
||||
|
||||
input_buffer& set(input_buffer& in);
|
||||
output_buffer& get(output_buffer& out) const;
|
||||
|
||||
HandShakeType get_type() const;
|
||||
void Process(input_buffer&, SSL&);
|
||||
|
||||
const opaque* get_random() const;
|
||||
friend void buildServerHello(SSL&, ServerHello&);
|
||||
private:
|
||||
ServerHello(const ServerHello&); // hide copy
|
||||
ServerHello& operator=(const ServerHello&); // and assign
|
||||
};
|
||||
|
||||
|
||||
class x509;
|
||||
|
||||
// Certificate could be a chain
|
||||
class Certificate : public HandShakeBase {
|
||||
const x509* cert_;
|
||||
public:
|
||||
Certificate();
|
||||
explicit Certificate(const x509* cert);
|
||||
friend output_buffer& operator<<(output_buffer&, const Certificate&);
|
||||
|
||||
const opaque* get_buffer() const;
|
||||
|
||||
// Process handles input, needs SSL
|
||||
input_buffer& set(input_buffer& in);
|
||||
output_buffer& get(output_buffer& out) const;
|
||||
|
||||
HandShakeType get_type() const;
|
||||
void Process(input_buffer&, SSL&);
|
||||
private:
|
||||
Certificate(const Certificate&); // hide copy
|
||||
Certificate& operator=(const Certificate&); // and assign
|
||||
};
|
||||
|
||||
|
||||
|
||||
// RSA Public Key
|
||||
struct ServerRSAParams {
|
||||
opaque* rsa_modulus_;
|
||||
opaque* rsa_exponent_;
|
||||
};
|
||||
|
||||
|
||||
// Ephemeral Diffie-Hellman Parameters
|
||||
class ServerDHParams {
|
||||
int pSz_;
|
||||
int gSz_;
|
||||
int pubSz_;
|
||||
opaque* p_;
|
||||
opaque* g_;
|
||||
opaque* Ys_;
|
||||
public:
|
||||
ServerDHParams();
|
||||
~ServerDHParams();
|
||||
|
||||
int get_pSize() const;
|
||||
int get_gSize() const;
|
||||
int get_pubSize() const;
|
||||
|
||||
const opaque* get_p() const;
|
||||
const opaque* get_g() const;
|
||||
const opaque* get_pub() const;
|
||||
|
||||
opaque* alloc_p(int sz);
|
||||
opaque* alloc_g(int sz);
|
||||
opaque* alloc_pub(int sz);
|
||||
private:
|
||||
ServerDHParams(const ServerDHParams&); // hide copy
|
||||
ServerDHParams& operator=(const ServerDHParams&); // and assign
|
||||
};
|
||||
|
||||
|
||||
struct ServerKeyBase : public virtual_base {
|
||||
virtual ~ServerKeyBase() {}
|
||||
virtual void build(SSL&) {}
|
||||
virtual void read(SSL&, input_buffer&) {}
|
||||
virtual int get_length() const;
|
||||
virtual opaque* get_serverKey() const;
|
||||
};
|
||||
|
||||
|
||||
// Server random number for FORTEZZA KEA
|
||||
struct Fortezza_Server : public ServerKeyBase {
|
||||
opaque r_s_[FORTEZZA_MAX];
|
||||
};
|
||||
|
||||
|
||||
struct SignatureBase : public virtual_base {
|
||||
virtual ~SignatureBase() {}
|
||||
};
|
||||
|
||||
struct anonymous_sa : public SignatureBase {};
|
||||
|
||||
|
||||
struct Hashes {
|
||||
uint8 md5_[MD5_LEN];
|
||||
uint8 sha_[SHA_LEN];
|
||||
};
|
||||
|
||||
|
||||
struct rsa_sa : public SignatureBase {
|
||||
Hashes hashes_;
|
||||
};
|
||||
|
||||
|
||||
struct dsa_sa : public SignatureBase {
|
||||
uint8 sha_[SHA_LEN];
|
||||
};
|
||||
|
||||
|
||||
// Server's Diffie-Hellman exchange
|
||||
class DH_Server : public ServerKeyBase {
|
||||
ServerDHParams parms_;
|
||||
opaque* signature_;
|
||||
|
||||
int length_; // total length of message
|
||||
opaque* keyMessage_; // total exchange message
|
||||
public:
|
||||
DH_Server();
|
||||
~DH_Server();
|
||||
|
||||
void build(SSL&);
|
||||
void read(SSL&, input_buffer&);
|
||||
int get_length() const;
|
||||
opaque* get_serverKey() const;
|
||||
private:
|
||||
DH_Server(const DH_Server&); // hide copy
|
||||
DH_Server& operator=(const DH_Server&); // and assign
|
||||
};
|
||||
|
||||
|
||||
// Server's RSA exchange
|
||||
struct RSA_Server : public ServerKeyBase {
|
||||
ServerRSAParams params_;
|
||||
opaque* signature_; // signed rsa_sa hashes
|
||||
};
|
||||
|
||||
|
||||
class ServerKeyExchange : public HandShakeBase {
|
||||
ServerKeyBase* server_key_;
|
||||
public:
|
||||
explicit ServerKeyExchange(SSL&);
|
||||
ServerKeyExchange();
|
||||
~ServerKeyExchange();
|
||||
|
||||
void createKey(SSL&);
|
||||
void build(SSL& ssl);
|
||||
|
||||
const opaque* getKey() const;
|
||||
int getKeyLength() const;
|
||||
|
||||
input_buffer& set(input_buffer& in);
|
||||
output_buffer& get(output_buffer& out) const;
|
||||
|
||||
friend output_buffer& operator<<(output_buffer&, const ServerKeyExchange&);
|
||||
|
||||
void Process(input_buffer&, SSL&);
|
||||
HandShakeType get_type() const;
|
||||
private:
|
||||
ServerKeyExchange(const ServerKeyExchange&); // hide copy
|
||||
ServerKeyExchange& operator=(const ServerKeyExchange&); // and assign
|
||||
};
|
||||
|
||||
|
||||
|
||||
class CertificateRequest : public HandShakeBase {
|
||||
ClientCertificateType certificate_types_[CERT_TYPES];
|
||||
int typeTotal_;
|
||||
STL::list<DistinguishedName> certificate_authorities_;
|
||||
public:
|
||||
CertificateRequest();
|
||||
~CertificateRequest();
|
||||
|
||||
input_buffer& set(input_buffer& in);
|
||||
output_buffer& get(output_buffer& out) const;
|
||||
|
||||
friend input_buffer& operator>>(input_buffer&, CertificateRequest&);
|
||||
friend output_buffer& operator<<(output_buffer&,
|
||||
const CertificateRequest&);
|
||||
|
||||
void Process(input_buffer&, SSL&);
|
||||
HandShakeType get_type() const;
|
||||
|
||||
void Build();
|
||||
private:
|
||||
CertificateRequest(const CertificateRequest&); // hide copy
|
||||
CertificateRequest& operator=(const CertificateRequest&); // and assign
|
||||
};
|
||||
|
||||
|
||||
struct ServerHelloDone : public HandShakeBase {
|
||||
ServerHelloDone();
|
||||
input_buffer& set(input_buffer& in);
|
||||
output_buffer& get(output_buffer& out) const;
|
||||
|
||||
void Process(input_buffer& input, SSL& ssl);
|
||||
|
||||
HandShakeType get_type() const;
|
||||
};
|
||||
|
||||
|
||||
struct PreMasterSecret {
|
||||
opaque random_[SECRET_LEN]; // first two bytes Protocol Version
|
||||
};
|
||||
|
||||
|
||||
struct ClientKeyBase : public virtual_base {
|
||||
virtual ~ClientKeyBase() {}
|
||||
virtual void build(SSL&) {}
|
||||
virtual void read(SSL&, input_buffer&) {}
|
||||
virtual int get_length() const;
|
||||
virtual opaque* get_clientKey() const;
|
||||
};
|
||||
|
||||
|
||||
class EncryptedPreMasterSecret : public ClientKeyBase {
|
||||
opaque* secret_;
|
||||
int length_;
|
||||
public:
|
||||
EncryptedPreMasterSecret();
|
||||
~EncryptedPreMasterSecret();
|
||||
|
||||
void build(SSL&);
|
||||
void read(SSL&, input_buffer&);
|
||||
int get_length() const;
|
||||
opaque* get_clientKey() const;
|
||||
void alloc(int sz);
|
||||
private:
|
||||
// hide copy and assign
|
||||
EncryptedPreMasterSecret(const EncryptedPreMasterSecret&);
|
||||
EncryptedPreMasterSecret& operator=(const EncryptedPreMasterSecret&);
|
||||
};
|
||||
|
||||
|
||||
// Fortezza Key Parameters from page 29
|
||||
// hard code lengths cause only used here
|
||||
struct FortezzaKeys : public ClientKeyBase {
|
||||
opaque y_c_ [128]; // client's Yc, public value
|
||||
opaque r_c_ [128]; // client's Rc
|
||||
opaque y_signature_ [40]; // DSS signed public key
|
||||
opaque wrapped_client_write_key_ [12]; // wrapped by the TEK
|
||||
opaque wrapped_server_write_key_ [12]; // wrapped by the TEK
|
||||
opaque client_write_iv_ [24];
|
||||
opaque server_write_iv_ [24];
|
||||
opaque master_secret_iv_ [24]; // IV used to encrypt preMaster
|
||||
opaque encrypted_preMasterSecret_[48]; // random & crypted by the TEK
|
||||
};
|
||||
|
||||
|
||||
|
||||
// Diffie-Hellman public key from page 40/41
|
||||
class ClientDiffieHellmanPublic : public ClientKeyBase {
|
||||
PublicValueEncoding public_value_encoding_;
|
||||
int length_; // includes two byte length for message
|
||||
opaque* Yc_; // length + Yc_
|
||||
// dh_Yc only if explicit, otherwise sent in certificate
|
||||
enum { KEY_OFFSET = 2 };
|
||||
public:
|
||||
ClientDiffieHellmanPublic();
|
||||
~ClientDiffieHellmanPublic();
|
||||
|
||||
void build(SSL&);
|
||||
void read(SSL&, input_buffer&);
|
||||
int get_length() const;
|
||||
opaque* get_clientKey() const;
|
||||
void alloc(int sz, bool offset = false);
|
||||
private:
|
||||
// hide copy and assign
|
||||
ClientDiffieHellmanPublic(const ClientDiffieHellmanPublic&);
|
||||
ClientDiffieHellmanPublic& operator=(const ClientDiffieHellmanPublic&);
|
||||
};
|
||||
|
||||
|
||||
class ClientKeyExchange : public HandShakeBase {
|
||||
ClientKeyBase* client_key_;
|
||||
public:
|
||||
explicit ClientKeyExchange(SSL& ssl);
|
||||
ClientKeyExchange();
|
||||
~ClientKeyExchange();
|
||||
|
||||
void createKey(SSL&);
|
||||
void build(SSL& ssl);
|
||||
|
||||
const opaque* getKey() const;
|
||||
int getKeyLength() const;
|
||||
|
||||
friend output_buffer& operator<<(output_buffer&, const ClientKeyExchange&);
|
||||
|
||||
input_buffer& set(input_buffer& in);
|
||||
output_buffer& get(output_buffer& out) const;
|
||||
|
||||
HandShakeType get_type() const;
|
||||
void Process(input_buffer&, SSL&);
|
||||
private:
|
||||
ClientKeyExchange(const ClientKeyExchange&); // hide copy
|
||||
ClientKeyExchange& operator=(const ClientKeyExchange&); // and assign
|
||||
};
|
||||
|
||||
|
||||
class CertificateVerify : public HandShakeBase {
|
||||
Hashes hashes_;
|
||||
byte* signature_; // owns
|
||||
public:
|
||||
CertificateVerify();
|
||||
~CertificateVerify();
|
||||
|
||||
input_buffer& set(input_buffer& in);
|
||||
output_buffer& get(output_buffer& out) const;
|
||||
|
||||
friend input_buffer& operator>>(input_buffer&, CertificateVerify&);
|
||||
friend output_buffer& operator<<(output_buffer&, const CertificateVerify&);
|
||||
|
||||
void Process(input_buffer&, SSL&);
|
||||
HandShakeType get_type() const;
|
||||
|
||||
void Build(SSL&);
|
||||
private:
|
||||
CertificateVerify(const CertificateVerify&); // hide copy
|
||||
CertificateVerify& operator=(const CertificateVerify&); // and assign
|
||||
};
|
||||
|
||||
|
||||
class Finished : public HandShakeBase {
|
||||
Hashes hashes_;
|
||||
public:
|
||||
Finished();
|
||||
|
||||
uint8* set_md5();
|
||||
uint8* set_sha();
|
||||
|
||||
friend input_buffer& operator>>(input_buffer&, Finished&);
|
||||
friend output_buffer& operator<<(output_buffer&, const Finished&);
|
||||
|
||||
input_buffer& set(input_buffer& in);
|
||||
output_buffer& get(output_buffer& out) const;
|
||||
|
||||
void Process(input_buffer&, SSL&);
|
||||
|
||||
HandShakeType get_type() const;
|
||||
private:
|
||||
Finished(const Finished&); // hide copy
|
||||
Finished& operator=(const Finished&); // and assign
|
||||
};
|
||||
|
||||
|
||||
class RandomPool; // forward for connection
|
||||
|
||||
|
||||
// SSL Connection defined on page 11
|
||||
struct Connection {
|
||||
opaque *pre_master_secret_;
|
||||
opaque master_secret_[SECRET_LEN];
|
||||
opaque client_random_[RAN_LEN];
|
||||
opaque server_random_[RAN_LEN];
|
||||
opaque sessionID_[ID_LEN];
|
||||
opaque client_write_MAC_secret_[SHA_LEN]; // sha is max size
|
||||
opaque server_write_MAC_secret_[SHA_LEN];
|
||||
opaque client_write_key_[AES_256_KEY_SZ]; // aes 256bit is max sz
|
||||
opaque server_write_key_[AES_256_KEY_SZ];
|
||||
opaque client_write_IV_[AES_IV_SZ]; // aes is max size
|
||||
opaque server_write_IV_[AES_IV_SZ];
|
||||
uint32 sequence_number_;
|
||||
uint32 peer_sequence_number_;
|
||||
uint32 pre_secret_len_; // pre master length
|
||||
bool send_server_key_; // server key exchange?
|
||||
bool master_clean_; // master secret clean?
|
||||
bool TLS_; // TLSv1 or greater
|
||||
bool TLSv1_1_; // TLSv1.1 or greater
|
||||
bool sessionID_Set_; // do we have a session
|
||||
bool compression_; // zlib compression?
|
||||
ProtocolVersion version_; // negotiated version
|
||||
ProtocolVersion chVersion_; // client hello version
|
||||
RandomPool& random_;
|
||||
|
||||
Connection(ProtocolVersion v, RandomPool& ran);
|
||||
~Connection();
|
||||
|
||||
void AllocPreSecret(uint sz);
|
||||
void CleanPreMaster();
|
||||
void CleanMaster();
|
||||
void TurnOffTLS();
|
||||
void TurnOffTLS1_1();
|
||||
private:
|
||||
Connection(const Connection&); // hide copy
|
||||
Connection& operator=(const Connection&); // and assign
|
||||
};
|
||||
|
||||
|
||||
struct Ciphers; // forward
|
||||
|
||||
|
||||
// TLSv1 Security Spec, defined on page 56 of RFC 2246
|
||||
struct Parameters {
|
||||
ConnectionEnd entity_;
|
||||
BulkCipherAlgorithm bulk_cipher_algorithm_;
|
||||
CipherType cipher_type_;
|
||||
uint8 key_size_;
|
||||
uint8 iv_size_;
|
||||
IsExportable is_exportable_;
|
||||
MACAlgorithm mac_algorithm_;
|
||||
uint8 hash_size_;
|
||||
CompressionMethod compression_algorithm_;
|
||||
KeyExchangeAlgorithm kea_; // yassl additions
|
||||
SignatureAlgorithm sig_algo_; // signature auth type
|
||||
SignatureAlgorithm verify_algo_; // cert verify auth type
|
||||
bool pending_;
|
||||
bool resumable_; // new conns by session
|
||||
uint16 encrypt_size_; // current msg encrypt sz
|
||||
Cipher suite_[SUITE_LEN]; // choosen suite
|
||||
uint8 suites_size_;
|
||||
Cipher suites_[MAX_SUITE_SZ];
|
||||
char cipher_name_[MAX_SUITE_NAME];
|
||||
char cipher_list_[MAX_CIPHERS][MAX_SUITE_NAME];
|
||||
bool removeDH_; // for server's later use
|
||||
|
||||
Parameters(ConnectionEnd, const Ciphers&, ProtocolVersion, bool haveDH);
|
||||
|
||||
void SetSuites(ProtocolVersion pv, bool removeDH = false,
|
||||
bool removeRSA = false, bool removeDSA = false);
|
||||
void SetCipherNames();
|
||||
private:
|
||||
Parameters(const Parameters&); // hide copy
|
||||
Parameters& operator=(const Parameters&); // and assing
|
||||
};
|
||||
|
||||
|
||||
input_buffer& operator>>(input_buffer&, RecordLayerHeader&);
|
||||
output_buffer& operator<<(output_buffer&, const RecordLayerHeader&);
|
||||
|
||||
input_buffer& operator>>(input_buffer&, Message&);
|
||||
output_buffer& operator<<(output_buffer&, const Message&);
|
||||
|
||||
input_buffer& operator>>(input_buffer&, HandShakeBase&);
|
||||
output_buffer& operator<<(output_buffer&, const HandShakeBase&);
|
||||
|
||||
|
||||
// Message Factory definition
|
||||
// uses the ContentType enumeration for unique id
|
||||
typedef Factory<Message> MessageFactory;
|
||||
void InitMessageFactory(MessageFactory&); // registers derived classes
|
||||
|
||||
// HandShake Factory definition
|
||||
// uses the HandShakeType enumeration for unique id
|
||||
typedef Factory<HandShakeBase> HandShakeFactory;
|
||||
void InitHandShakeFactory(HandShakeFactory&); // registers derived classes
|
||||
|
||||
// ServerKey Factory definition
|
||||
// uses KeyExchangeAlgorithm enumeration for unique id
|
||||
typedef Factory<ServerKeyBase> ServerKeyFactory;
|
||||
void InitServerKeyFactory(ServerKeyFactory&);
|
||||
|
||||
// ClientKey Factory definition
|
||||
// uses KeyExchangeAlgorithm enumeration for unique id
|
||||
typedef Factory<ClientKeyBase> ClientKeyFactory;
|
||||
void InitClientKeyFactory(ClientKeyFactory&);
|
||||
|
||||
|
||||
// Message Creators
|
||||
Message* CreateHandShake();
|
||||
Message* CreateCipherSpec();
|
||||
Message* CreateAlert();
|
||||
Message* CreateData();
|
||||
|
||||
|
||||
// HandShake Creators
|
||||
HandShakeBase* CreateCertificate();
|
||||
HandShakeBase* CreateHelloRequest();
|
||||
HandShakeBase* CreateClientHello();
|
||||
HandShakeBase* CreateServerHello();
|
||||
HandShakeBase* CreateServerKeyExchange();
|
||||
HandShakeBase* CreateCertificateRequest();
|
||||
HandShakeBase* CreateServerHelloDone();
|
||||
HandShakeBase* CreateClientKeyExchange();
|
||||
HandShakeBase* CreateCertificateVerify();
|
||||
HandShakeBase* CreateFinished();
|
||||
|
||||
|
||||
// ServerKey Exchange Creators
|
||||
ServerKeyBase* CreateRSAServerKEA();
|
||||
ServerKeyBase* CreateDHServerKEA();
|
||||
ServerKeyBase* CreateFortezzaServerKEA();
|
||||
|
||||
// ClientKey Exchange Creators
|
||||
ClientKeyBase* CreateRSAClient();
|
||||
ClientKeyBase* CreateDHClient();
|
||||
ClientKeyBase* CreateFortezzaClient();
|
||||
|
||||
|
||||
|
||||
} // naemspace
|
||||
|
||||
#endif // yaSSL_IMP_HPP
|
||||
+693
@@ -0,0 +1,693 @@
|
||||
/*
|
||||
Copyright 2000-2008 MySQL AB, 2008 Sun Microsystems, Inc.
|
||||
|
||||
This program is free software; you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation; version 2 of the License.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program; see the file COPYING. If not, write to the
|
||||
Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
|
||||
MA 02110-1301 USA.
|
||||
*/
|
||||
|
||||
|
||||
/* yaSSL internal header defines SSL supporting types not specified in the
|
||||
* draft along with type conversion functions and openssl compatibility
|
||||
*/
|
||||
|
||||
|
||||
#ifndef yaSSL_INT_HPP
|
||||
#define yaSSL_INT_HPP
|
||||
|
||||
#include "yassl_imp.hpp"
|
||||
#include "yassl_error.hpp"
|
||||
#include "crypto_wrapper.hpp"
|
||||
#include "cert_wrapper.hpp"
|
||||
#include "log.hpp"
|
||||
#include "lock.hpp"
|
||||
#include "openssl/ssl.h" // ASN1_STRING and DH
|
||||
|
||||
// Check if _POSIX_THREADS should be forced
|
||||
#if !defined(_POSIX_THREADS) && (defined(__NETWARE__) || defined(__hpux))
|
||||
// HPUX does not define _POSIX_THREADS as it's not _fully_ implemented
|
||||
// Netware supports pthreads but does not announce it
|
||||
#define _POSIX_THREADS
|
||||
#endif
|
||||
|
||||
#ifdef _POSIX_THREADS
|
||||
#include <pthread.h>
|
||||
#endif
|
||||
|
||||
|
||||
namespace STL = STL_NAMESPACE;
|
||||
|
||||
|
||||
namespace yaSSL {
|
||||
|
||||
|
||||
// State Machine for Record Layer Protocol
|
||||
enum RecordLayerState {
|
||||
recordNotReady = 0, // fatal error, no more processing
|
||||
recordReady
|
||||
};
|
||||
|
||||
|
||||
// State Machine for HandShake Protocol
|
||||
enum HandShakeState {
|
||||
handShakeNotReady = 0, // fatal error, no more processing
|
||||
preHandshake, // initial state
|
||||
inHandshake, // handshake started
|
||||
handShakeReady // handshake done
|
||||
};
|
||||
|
||||
|
||||
// client input HandShake state, use if HandShakeState == inHandShake
|
||||
enum ClientState {
|
||||
serverNull = 0,
|
||||
serverHelloComplete,
|
||||
serverCertComplete,
|
||||
serverKeyExchangeComplete,
|
||||
serverHelloDoneComplete,
|
||||
serverFinishedComplete
|
||||
};
|
||||
|
||||
|
||||
// server input HandShake state, use if HandShakeState == inHandShake
|
||||
enum ServerState {
|
||||
clientNull = 0,
|
||||
clientHelloComplete,
|
||||
clientKeyExchangeComplete,
|
||||
clientFinishedComplete
|
||||
};
|
||||
|
||||
|
||||
// client connect state for nonblocking restart
|
||||
enum ConnectState {
|
||||
CONNECT_BEGIN = 0,
|
||||
CLIENT_HELLO_SENT,
|
||||
FIRST_REPLY_DONE,
|
||||
FINISHED_DONE,
|
||||
SECOND_REPLY_DONE
|
||||
};
|
||||
|
||||
|
||||
// server accpet state for nonblocking restart
|
||||
enum AcceptState {
|
||||
ACCEPT_BEGIN = 0,
|
||||
ACCEPT_FIRST_REPLY_DONE,
|
||||
SERVER_HELLO_DONE,
|
||||
ACCEPT_SECOND_REPLY_DONE,
|
||||
ACCEPT_FINISHED_DONE,
|
||||
ACCEPT_THIRD_REPLY_DONE
|
||||
};
|
||||
|
||||
|
||||
// combines all states
|
||||
class States {
|
||||
RecordLayerState recordLayer_;
|
||||
HandShakeState handshakeLayer_;
|
||||
ClientState clientState_;
|
||||
ServerState serverState_;
|
||||
ConnectState connectState_;
|
||||
AcceptState acceptState_;
|
||||
char errorString_[MAX_ERROR_SZ];
|
||||
YasslError what_;
|
||||
public:
|
||||
States();
|
||||
|
||||
const RecordLayerState& getRecord() const;
|
||||
const HandShakeState& getHandShake() const;
|
||||
const ClientState& getClient() const;
|
||||
const ServerState& getServer() const;
|
||||
const ConnectState& GetConnect() const;
|
||||
const AcceptState& GetAccept() const;
|
||||
const char* getString() const;
|
||||
YasslError What() const;
|
||||
|
||||
RecordLayerState& useRecord();
|
||||
HandShakeState& useHandShake();
|
||||
ClientState& useClient();
|
||||
ServerState& useServer();
|
||||
ConnectState& UseConnect();
|
||||
AcceptState& UseAccept();
|
||||
char* useString();
|
||||
void SetError(YasslError);
|
||||
private:
|
||||
States(const States&); // hide copy
|
||||
States& operator=(const States&); // and assign
|
||||
};
|
||||
|
||||
|
||||
// holds all factories
|
||||
class sslFactory {
|
||||
MessageFactory messageFactory_; // creates new messages by type
|
||||
HandShakeFactory handShakeFactory_; // creates new handshake types
|
||||
ServerKeyFactory serverKeyFactory_; // creates new server key types
|
||||
ClientKeyFactory clientKeyFactory_; // creates new client key types
|
||||
|
||||
sslFactory(); // only GetSSL_Factory creates
|
||||
public:
|
||||
const MessageFactory& getMessage() const;
|
||||
const HandShakeFactory& getHandShake() const;
|
||||
const ServerKeyFactory& getServerKey() const;
|
||||
const ClientKeyFactory& getClientKey() const;
|
||||
|
||||
friend sslFactory& GetSSL_Factory(); // singleton creator
|
||||
private:
|
||||
sslFactory(const sslFactory&); // hide copy
|
||||
sslFactory& operator=(const sslFactory&); // and assign
|
||||
};
|
||||
|
||||
|
||||
#undef X509_NAME // wincrypt.h clash
|
||||
|
||||
// openSSL X509 names
|
||||
class X509_NAME {
|
||||
char* name_;
|
||||
size_t sz_;
|
||||
ASN1_STRING entry_;
|
||||
public:
|
||||
X509_NAME(const char*, size_t sz);
|
||||
~X509_NAME();
|
||||
|
||||
const char* GetName() const;
|
||||
ASN1_STRING* GetEntry(int i);
|
||||
size_t GetLength() const;
|
||||
private:
|
||||
X509_NAME(const X509_NAME&); // hide copy
|
||||
X509_NAME& operator=(const X509_NAME&); // and assign
|
||||
};
|
||||
|
||||
|
||||
class StringHolder {
|
||||
ASN1_STRING asnString_;
|
||||
public:
|
||||
StringHolder(const char* str, int sz);
|
||||
~StringHolder();
|
||||
|
||||
ASN1_STRING* GetString();
|
||||
private:
|
||||
StringHolder(const StringHolder&); // hide copy
|
||||
StringHolder& operator=(const StringHolder&); // and assign
|
||||
};
|
||||
|
||||
|
||||
// openSSL X509
|
||||
class X509 {
|
||||
X509_NAME issuer_;
|
||||
X509_NAME subject_;
|
||||
StringHolder beforeDate_; // not valid before
|
||||
StringHolder afterDate_; // not valid after
|
||||
public:
|
||||
X509(const char* i, size_t, const char* s, size_t,
|
||||
const char* b, int, const char* a, int);
|
||||
~X509() {}
|
||||
|
||||
X509_NAME* GetIssuer();
|
||||
X509_NAME* GetSubject();
|
||||
|
||||
ASN1_STRING* GetBefore();
|
||||
ASN1_STRING* GetAfter();
|
||||
|
||||
private:
|
||||
X509(const X509&); // hide copy
|
||||
X509& operator=(const X509&); // and assign
|
||||
};
|
||||
|
||||
|
||||
// openSSL bignum
|
||||
struct BIGNUM {
|
||||
/*
|
||||
gcc 2.96 fix: because of two Integer classes (yaSSL::Integer and
|
||||
TaoCrypt::Integer), we need to explicitly state the namespace
|
||||
here to let gcc 2.96 deduce the correct type.
|
||||
*/
|
||||
yaSSL::Integer int_;
|
||||
void assign(const byte* b, uint s) { int_.assign(b,s); }
|
||||
};
|
||||
|
||||
|
||||
// openSSL session
|
||||
class SSL_SESSION {
|
||||
opaque sessionID_[ID_LEN];
|
||||
opaque master_secret_[SECRET_LEN];
|
||||
Cipher suite_[SUITE_LEN];
|
||||
uint bornOn_; // create time in seconds
|
||||
uint timeout_; // timeout in seconds
|
||||
RandomPool& random_; // will clean master secret
|
||||
X509* peerX509_;
|
||||
public:
|
||||
explicit SSL_SESSION(RandomPool&);
|
||||
SSL_SESSION(const SSL&, RandomPool&);
|
||||
~SSL_SESSION();
|
||||
|
||||
const opaque* GetID() const;
|
||||
const opaque* GetSecret() const;
|
||||
const Cipher* GetSuite() const;
|
||||
uint GetBornOn() const;
|
||||
uint GetTimeOut() const;
|
||||
X509* GetPeerX509() const;
|
||||
void SetTimeOut(uint);
|
||||
|
||||
SSL_SESSION& operator=(const SSL_SESSION&); // allow assign for resumption
|
||||
private:
|
||||
SSL_SESSION(const SSL_SESSION&); // hide copy
|
||||
|
||||
void CopyX509(X509*);
|
||||
};
|
||||
|
||||
|
||||
// holds all sessions
|
||||
class Sessions {
|
||||
STL::list<SSL_SESSION*> list_;
|
||||
RandomPool random_; // for session cleaning
|
||||
Mutex mutex_; // no-op for single threaded
|
||||
int count_; // flush counter
|
||||
|
||||
Sessions() : count_(0) {} // only GetSessions can create
|
||||
public:
|
||||
SSL_SESSION* lookup(const opaque*, SSL_SESSION* copy = 0);
|
||||
void add(const SSL&);
|
||||
void remove(const opaque*);
|
||||
void Flush();
|
||||
|
||||
~Sessions();
|
||||
|
||||
friend Sessions& GetSessions(); // singleton creator
|
||||
private:
|
||||
Sessions(const Sessions&); // hide copy
|
||||
Sessions& operator=(const Sessions&); // and assign
|
||||
};
|
||||
|
||||
|
||||
#ifdef _POSIX_THREADS
|
||||
typedef pthread_t THREAD_ID_T;
|
||||
#else
|
||||
typedef DWORD THREAD_ID_T;
|
||||
#endif
|
||||
|
||||
// thread error data
|
||||
struct ThreadError {
|
||||
THREAD_ID_T threadID_;
|
||||
int errorID_;
|
||||
};
|
||||
|
||||
|
||||
// holds all errors
|
||||
class Errors {
|
||||
STL::list<ThreadError> list_;
|
||||
Mutex mutex_;
|
||||
|
||||
Errors() {} // only GetErrors can create
|
||||
public:
|
||||
int Lookup(bool peek); // self lookup
|
||||
void Add(int);
|
||||
void Remove(); // remove self
|
||||
|
||||
~Errors() {}
|
||||
|
||||
friend Errors& GetErrors(); // singleton creator
|
||||
private:
|
||||
Errors(const Errors&); // hide copy
|
||||
Errors& operator=(const Errors); // and assign
|
||||
};
|
||||
|
||||
|
||||
Sessions& GetSessions(); // forward singletons
|
||||
sslFactory& GetSSL_Factory();
|
||||
Errors& GetErrors();
|
||||
|
||||
|
||||
// openSSL method and context types
|
||||
class SSL_METHOD {
|
||||
ProtocolVersion version_;
|
||||
ConnectionEnd side_;
|
||||
bool verifyPeer_; // request or send certificate
|
||||
bool verifyNone_; // whether to verify certificate
|
||||
bool failNoCert_;
|
||||
bool multipleProtocol_; // for SSLv23 compatibility
|
||||
public:
|
||||
SSL_METHOD(ConnectionEnd ce, ProtocolVersion pv,
|
||||
bool multipleProtocol = false);
|
||||
|
||||
ProtocolVersion getVersion() const;
|
||||
ConnectionEnd getSide() const;
|
||||
|
||||
void setVerifyPeer();
|
||||
void setVerifyNone();
|
||||
void setFailNoCert();
|
||||
|
||||
bool verifyPeer() const;
|
||||
bool verifyNone() const;
|
||||
bool failNoCert() const;
|
||||
bool multipleProtocol() const;
|
||||
private:
|
||||
SSL_METHOD(const SSL_METHOD&); // hide copy
|
||||
SSL_METHOD& operator=(const SSL_METHOD&); // and assign
|
||||
};
|
||||
|
||||
|
||||
struct Ciphers {
|
||||
bool setSuites_; // user set suites from default
|
||||
byte suites_[MAX_SUITE_SZ]; // new suites
|
||||
int suiteSz_; // suite length in bytes
|
||||
|
||||
Ciphers() : setSuites_(false), suiteSz_(0) {}
|
||||
};
|
||||
|
||||
|
||||
struct DH; // forward
|
||||
|
||||
|
||||
// save for SSL construction
|
||||
struct DH_Parms {
|
||||
Integer p_;
|
||||
Integer g_;
|
||||
bool set_; // if set by user
|
||||
|
||||
DH_Parms() : set_(false) {}
|
||||
};
|
||||
|
||||
|
||||
enum StatsField {
|
||||
Accept, Connect, AcceptGood, ConnectGood, AcceptRenegotiate,
|
||||
ConnectRenegotiate, Hits, CbHits, CacheFull, Misses, Timeouts, Number,
|
||||
GetCacheSize, VerifyMode, VerifyDepth
|
||||
};
|
||||
|
||||
|
||||
// SSL stats
|
||||
struct Stats {
|
||||
long accept_;
|
||||
long connect_;
|
||||
long acceptGood_;
|
||||
long connectGood_;
|
||||
long acceptRenegotiate_;
|
||||
long connectRenegotiate_;
|
||||
|
||||
long hits_;
|
||||
long cbHits_;
|
||||
long cacheFull_;
|
||||
long misses_;
|
||||
long timeouts_;
|
||||
long number_;
|
||||
long getCacheSize_;
|
||||
|
||||
int verifyMode_;
|
||||
int verifyDepth_;
|
||||
public:
|
||||
Stats() : accept_(0), connect_(0), acceptGood_(0), connectGood_(0),
|
||||
acceptRenegotiate_(0), connectRenegotiate_(0), hits_(0), cbHits_(0),
|
||||
cacheFull_(0), misses_(0), timeouts_(0), number_(0), getCacheSize_(0),
|
||||
verifyMode_(0), verifyDepth_(0)
|
||||
{}
|
||||
private:
|
||||
Stats(const Stats&); // hide copy
|
||||
Stats& operator=(const Stats&); // and assign
|
||||
};
|
||||
|
||||
|
||||
// the SSL context
|
||||
class SSL_CTX {
|
||||
public:
|
||||
typedef STL::list<x509*> CertList;
|
||||
private:
|
||||
SSL_METHOD* method_;
|
||||
x509* certificate_;
|
||||
x509* privateKey_;
|
||||
CertList caList_;
|
||||
Ciphers ciphers_;
|
||||
DH_Parms dhParms_;
|
||||
pem_password_cb passwordCb_;
|
||||
void* userData_;
|
||||
bool sessionCacheOff_;
|
||||
bool sessionCacheFlushOff_;
|
||||
Stats stats_;
|
||||
Mutex mutex_; // for Stats
|
||||
VerifyCallback verifyCallback_;
|
||||
public:
|
||||
explicit SSL_CTX(SSL_METHOD* meth);
|
||||
~SSL_CTX();
|
||||
|
||||
const x509* getCert() const;
|
||||
const x509* getKey() const;
|
||||
const SSL_METHOD* getMethod() const;
|
||||
const Ciphers& GetCiphers() const;
|
||||
const DH_Parms& GetDH_Parms() const;
|
||||
const Stats& GetStats() const;
|
||||
const VerifyCallback getVerifyCallback() const;
|
||||
pem_password_cb GetPasswordCb() const;
|
||||
void* GetUserData() const;
|
||||
bool GetSessionCacheOff() const;
|
||||
bool GetSessionCacheFlushOff() const;
|
||||
|
||||
void setVerifyPeer();
|
||||
void setVerifyNone();
|
||||
void setFailNoCert();
|
||||
void setVerifyCallback(VerifyCallback);
|
||||
bool SetCipherList(const char*);
|
||||
bool SetDH(const DH&);
|
||||
void SetPasswordCb(pem_password_cb cb);
|
||||
void SetUserData(void*);
|
||||
void SetSessionCacheOff();
|
||||
void SetSessionCacheFlushOff();
|
||||
|
||||
void IncrementStats(StatsField);
|
||||
void AddCA(x509* ca);
|
||||
const CertList& GetCA_List() const;
|
||||
|
||||
friend int read_file(SSL_CTX*, const char*, int, CertType);
|
||||
private:
|
||||
SSL_CTX(const SSL_CTX&); // hide copy
|
||||
SSL_CTX& operator=(const SSL_CTX&); // and assign
|
||||
};
|
||||
|
||||
|
||||
// holds all cryptographic types
|
||||
class Crypto {
|
||||
Digest* digest_; // agreed upon digest
|
||||
BulkCipher* cipher_; // agreed upon cipher
|
||||
DiffieHellman* dh_; // dh parms
|
||||
RandomPool random_; // random number generator
|
||||
CertManager cert_; // manages certificates
|
||||
public:
|
||||
explicit Crypto();
|
||||
~Crypto();
|
||||
|
||||
const Digest& get_digest() const;
|
||||
const BulkCipher& get_cipher() const;
|
||||
const DiffieHellman& get_dh() const;
|
||||
const RandomPool& get_random() const;
|
||||
const CertManager& get_certManager() const;
|
||||
|
||||
Digest& use_digest();
|
||||
BulkCipher& use_cipher();
|
||||
DiffieHellman& use_dh();
|
||||
RandomPool& use_random();
|
||||
CertManager& use_certManager();
|
||||
|
||||
void SetDH(DiffieHellman*);
|
||||
void SetDH(const DH_Parms&);
|
||||
void setDigest(Digest*);
|
||||
void setCipher(BulkCipher*);
|
||||
|
||||
bool DhSet();
|
||||
private:
|
||||
Crypto(const Crypto&); // hide copy
|
||||
Crypto& operator=(const Crypto&); // and assign
|
||||
};
|
||||
|
||||
|
||||
// holds all handshake and verify hashes
|
||||
class sslHashes {
|
||||
MD5 md5HandShake_; // md5 handshake hash
|
||||
SHA shaHandShake_; // sha handshake hash
|
||||
Finished verify_; // peer's verify hash
|
||||
Hashes certVerify_; // peer's cert verify hash
|
||||
public:
|
||||
sslHashes() {}
|
||||
|
||||
const MD5& get_MD5() const;
|
||||
const SHA& get_SHA() const;
|
||||
const Finished& get_verify() const;
|
||||
const Hashes& get_certVerify() const;
|
||||
|
||||
MD5& use_MD5();
|
||||
SHA& use_SHA();
|
||||
Finished& use_verify();
|
||||
Hashes& use_certVerify();
|
||||
private:
|
||||
sslHashes(const sslHashes&); // hide copy
|
||||
sslHashes& operator=(const sslHashes&); // and assign
|
||||
};
|
||||
|
||||
|
||||
// holds input and output buffers
|
||||
class Buffers {
|
||||
public:
|
||||
typedef STL::list<input_buffer*> inputList;
|
||||
typedef STL::list<output_buffer*> outputList;
|
||||
private:
|
||||
inputList dataList_; // list of users app data / handshake
|
||||
outputList handShakeList_; // buffered handshake msgs
|
||||
input_buffer* rawInput_; // buffered raw input yet to process
|
||||
public:
|
||||
Buffers();
|
||||
~Buffers();
|
||||
|
||||
const inputList& getData() const;
|
||||
const outputList& getHandShake() const;
|
||||
|
||||
inputList& useData();
|
||||
outputList& useHandShake();
|
||||
|
||||
void SetRawInput(input_buffer*); // takes ownership
|
||||
input_buffer* TakeRawInput(); // takes ownership
|
||||
private:
|
||||
Buffers(const Buffers&); // hide copy
|
||||
Buffers& operator=(const Buffers&); // and assign
|
||||
};
|
||||
|
||||
|
||||
// wraps security parameters
|
||||
class Security {
|
||||
Connection conn_; // connection information
|
||||
Parameters parms_; // may be pending
|
||||
SSL_SESSION resumeSession_; // if resuming
|
||||
SSL_CTX* ctx_; // context used to init
|
||||
bool resuming_; // trying to resume
|
||||
public:
|
||||
Security(ProtocolVersion, RandomPool&, ConnectionEnd, const Ciphers&,
|
||||
SSL_CTX*, bool);
|
||||
|
||||
const SSL_CTX* GetContext() const;
|
||||
const Connection& get_connection() const;
|
||||
const Parameters& get_parms() const;
|
||||
const SSL_SESSION& get_resume() const;
|
||||
bool get_resuming() const;
|
||||
|
||||
Connection& use_connection();
|
||||
Parameters& use_parms();
|
||||
SSL_SESSION& use_resume();
|
||||
|
||||
void set_resuming(bool b);
|
||||
private:
|
||||
Security(const Security&); // hide copy
|
||||
Security& operator=(const Security&); // and assign
|
||||
};
|
||||
|
||||
|
||||
// THE SSL type
|
||||
class SSL {
|
||||
Crypto crypto_; // agreed crypto agents
|
||||
Security secure_; // Connection and Session parms
|
||||
States states_; // Record and HandShake states
|
||||
sslHashes hashes_; // handshake, finished hashes
|
||||
Socket socket_; // socket wrapper
|
||||
Buffers buffers_; // buffered handshakes and data
|
||||
Log log_; // logger
|
||||
bool quietShutdown_;
|
||||
|
||||
// optimization variables
|
||||
bool has_data_; // buffered data ready?
|
||||
public:
|
||||
SSL(SSL_CTX* ctx);
|
||||
|
||||
// gets and uses
|
||||
const Crypto& getCrypto() const;
|
||||
const Security& getSecurity() const;
|
||||
const States& getStates() const;
|
||||
const sslHashes& getHashes() const;
|
||||
const sslFactory& getFactory() const;
|
||||
const Socket& getSocket() const;
|
||||
YasslError GetError() const;
|
||||
bool GetMultiProtocol() const;
|
||||
bool CompressionOn() const;
|
||||
|
||||
Crypto& useCrypto();
|
||||
Security& useSecurity();
|
||||
States& useStates();
|
||||
sslHashes& useHashes();
|
||||
Socket& useSocket();
|
||||
Log& useLog();
|
||||
Buffers& useBuffers();
|
||||
|
||||
bool HasData() const;
|
||||
bool GetQuietShutdown() const;
|
||||
|
||||
// sets
|
||||
void set_pending(Cipher suite);
|
||||
void set_random(const opaque*, ConnectionEnd);
|
||||
void set_sessionID(const opaque*);
|
||||
void set_session(SSL_SESSION*);
|
||||
void set_preMaster(const opaque*, uint);
|
||||
void set_masterSecret(const opaque*);
|
||||
void SetError(YasslError);
|
||||
int SetCompression();
|
||||
void UnSetCompression();
|
||||
void SetQuietShutdown(bool mode);
|
||||
|
||||
// helpers
|
||||
bool isTLS() const;
|
||||
bool isTLSv1_1() const;
|
||||
void order_error();
|
||||
void makeMasterSecret();
|
||||
void makeTLSMasterSecret();
|
||||
void addData(input_buffer* data);
|
||||
void fillData(Data&);
|
||||
void PeekData(Data&);
|
||||
void addBuffer(output_buffer* b);
|
||||
void flushBuffer();
|
||||
void verifyState(const RecordLayerHeader&);
|
||||
void verifyState(const HandShakeHeader&);
|
||||
void verifyState(ClientState);
|
||||
void verifyState(ServerState);
|
||||
void verfiyHandShakeComplete();
|
||||
void matchSuite(const opaque*, uint length);
|
||||
void deriveKeys();
|
||||
void deriveTLSKeys();
|
||||
void Send(const byte*, uint);
|
||||
|
||||
uint bufferedData();
|
||||
uint get_SEQIncrement(bool);
|
||||
|
||||
const byte* get_macSecret(bool);
|
||||
private:
|
||||
void storeKeys(const opaque*);
|
||||
void setKeys();
|
||||
void verifyClientState(HandShakeType);
|
||||
void verifyServerState(HandShakeType);
|
||||
|
||||
SSL(const SSL&); // hide copy
|
||||
const SSL& operator=(const SSL&); // and assign
|
||||
};
|
||||
|
||||
|
||||
// compression
|
||||
int Compress(const byte*, int, input_buffer&);
|
||||
int DeCompress(input_buffer&, int, input_buffer&);
|
||||
|
||||
|
||||
// conversion functions
|
||||
void c32to24(uint32, uint24&);
|
||||
void c24to32(const uint24, uint32&);
|
||||
|
||||
uint32 c24to32(const uint24);
|
||||
|
||||
void ato16(const opaque*, uint16&);
|
||||
void ato24(const opaque*, uint24&);
|
||||
|
||||
void c16toa(uint16, opaque*);
|
||||
void c24toa(const uint24, opaque*);
|
||||
void c32toa(uint32 u32, opaque*);
|
||||
|
||||
|
||||
} // naemspace
|
||||
|
||||
#endif // yaSSL_INT_HPP
|
||||
@@ -0,0 +1,528 @@
|
||||
/*
|
||||
Copyright 2000-2008 MySQL AB, 2008 Sun Microsystems, Inc.
|
||||
|
||||
This program is free software; you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation; version 2 of the License.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program; see the file COPYING. If not, write to the
|
||||
Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
|
||||
MA 02110-1301 USA.
|
||||
*/
|
||||
|
||||
/* yaSSL types header defines all constants, enums, and typedefs
|
||||
* from the SSL.v3 specification "draft-freier-ssl-version3-02.txt"
|
||||
*/
|
||||
|
||||
|
||||
#ifndef yaSSL_TYPES_HPP
|
||||
#define yaSSL_TYPES_HPP
|
||||
|
||||
#include <stddef.h>
|
||||
#include <assert.h>
|
||||
#include "type_traits.hpp"
|
||||
|
||||
|
||||
#ifdef _MSC_VER
|
||||
// disable conversion warning
|
||||
// 4996 warning to use MS extensions e.g., strcpy_s instead of strncpy
|
||||
#pragma warning(disable:4244 4996)
|
||||
#endif
|
||||
|
||||
|
||||
namespace yaSSL {
|
||||
|
||||
#define YASSL_LIB
|
||||
|
||||
|
||||
#ifdef YASSL_PURE_C
|
||||
|
||||
// library allocation
|
||||
struct new_t {}; // yaSSL New type
|
||||
extern new_t ys; // pass in parameter
|
||||
|
||||
} // namespace yaSSL
|
||||
|
||||
void* operator new (size_t, yaSSL::new_t);
|
||||
void* operator new[](size_t, yaSSL::new_t);
|
||||
|
||||
void operator delete (void*, yaSSL::new_t);
|
||||
void operator delete[](void*, yaSSL::new_t);
|
||||
|
||||
|
||||
namespace yaSSL {
|
||||
|
||||
|
||||
template<typename T>
|
||||
void ysDelete(T* ptr)
|
||||
{
|
||||
if (ptr) ptr->~T();
|
||||
::operator delete(ptr, yaSSL::ys);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
void ysArrayDelete(T* ptr)
|
||||
{
|
||||
// can't do array placement destruction since not tracking size in
|
||||
// allocation, only allow builtins to use array placement since they
|
||||
// don't need destructors called
|
||||
typedef char builtin[TaoCrypt::IsFundamentalType<T>::Yes ? 1 : -1];
|
||||
(void)sizeof(builtin);
|
||||
|
||||
::operator delete[](ptr, yaSSL::ys);
|
||||
}
|
||||
|
||||
#define NEW_YS new (yaSSL::ys)
|
||||
|
||||
// to resolve compiler generated operator delete on base classes with
|
||||
// virtual destructors (when on stack), make sure doesn't get called
|
||||
class virtual_base {
|
||||
public:
|
||||
static void operator delete(void*) { assert(0); }
|
||||
};
|
||||
|
||||
|
||||
#else // YASSL_PURE_C
|
||||
|
||||
|
||||
template<typename T>
|
||||
void ysDelete(T* ptr)
|
||||
{
|
||||
delete ptr;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
void ysArrayDelete(T* ptr)
|
||||
{
|
||||
delete[] ptr;
|
||||
}
|
||||
|
||||
#define NEW_YS new
|
||||
|
||||
class virtual_base {};
|
||||
|
||||
|
||||
|
||||
#endif // YASSL_PURE_C
|
||||
|
||||
|
||||
typedef unsigned char uint8;
|
||||
typedef unsigned short uint16;
|
||||
typedef unsigned int uint32;
|
||||
typedef uint8 uint24[3];
|
||||
typedef uint32 uint64[2];
|
||||
|
||||
typedef uint8 opaque;
|
||||
typedef opaque byte;
|
||||
|
||||
typedef unsigned int uint;
|
||||
|
||||
|
||||
#ifdef USE_SYS_STL
|
||||
// use system STL
|
||||
#define STL_VECTOR_FILE <vector>
|
||||
#define STL_LIST_FILE <list>
|
||||
#define STL_ALGORITHM_FILE <algorithm>
|
||||
#define STL_MEMORY_FILE <memory>
|
||||
#define STL_PAIR_FILE <utility>
|
||||
|
||||
#define STL_NAMESPACE std
|
||||
#else
|
||||
// use mySTL
|
||||
#define STL_VECTOR_FILE "vector.hpp"
|
||||
#define STL_LIST_FILE "list.hpp"
|
||||
#define STL_ALGORITHM_FILE "algorithm.hpp"
|
||||
#define STL_MEMORY_FILE "memory.hpp"
|
||||
#define STL_PAIR_FILE "pair.hpp"
|
||||
|
||||
#define STL_NAMESPACE mySTL
|
||||
#endif
|
||||
|
||||
|
||||
#ifndef min
|
||||
template <typename T>
|
||||
T min(T a, T b)
|
||||
{
|
||||
return a < b ? a : b;
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
// all length constants in bytes
|
||||
const int ID_LEN = 32; // session id length
|
||||
const int SUITE_LEN = 2; // cipher suite length
|
||||
const int SECRET_LEN = 48; // pre RSA and all master secret length
|
||||
const int MASTER_ROUNDS = 3; // master secret derivation rounds
|
||||
const int RAN_LEN = 32; // client and server random length
|
||||
const int MAC_BLOCK_SZ = 64; // MAC block size, & padding
|
||||
const int MD5_LEN = 16; // MD5 digest length
|
||||
const int SHA_LEN = 20; // SHA digest length
|
||||
const int RMD_LEN = 20; // RIPEMD-160 digest length
|
||||
const int PREFIX = 3; // up to 3 prefix letters for secret rounds
|
||||
const int KEY_PREFIX = 7; // up to 7 prefix letters for key rounds
|
||||
const int FORTEZZA_MAX = 128; // Maximum Fortezza Key length
|
||||
const int MAX_SUITE_SZ = 128; // 64 max suites * sizeof(suite)
|
||||
const int MAX_SUITE_NAME = 48; // max length of suite name
|
||||
const int MAX_CIPHERS = 32; // max supported ciphers for cipher list
|
||||
const int SIZEOF_ENUM = 1; // SSL considers an enum 1 byte, not 4
|
||||
const int SIZEOF_SENDER = 4; // Sender constant, for finished generation
|
||||
const int PAD_MD5 = 48; // pad length 1 and 2 for md5 finished
|
||||
const int PAD_SHA = 40; // should be 44, specd wrong by netscape
|
||||
const int PAD_RMD = 44; // pad length for RIPEMD-160, some use 40??
|
||||
const int CERT_HEADER = 3; // always use 3 bytes for certificate
|
||||
const int CERT_TYPES = 7; // certificate request types
|
||||
const int REQUEST_HEADER = 2; // request uses 2 bytes
|
||||
const int VERIFY_HEADER = 2; // verify length field
|
||||
const int MIN_CERT_TYPES = 1; // minimum certificate request types
|
||||
const int MIN_DIS_NAMES = 3; // minimum distinguished names
|
||||
const int MIN_DIS_SIZE = 1; // minimum distinguished name size
|
||||
const int RECORD_HEADER = 5; // type + version + length(2)
|
||||
const int HANDSHAKE_HEADER = 4; // type + length(3)
|
||||
const int FINISHED_SZ = MD5_LEN + SHA_LEN; // sizeof finished data
|
||||
const int TLS_FINISHED_SZ = 12; // TLS verify data size
|
||||
const int SEQ_SZ = 8; // 64 bit sequence number
|
||||
const int LENGTH_SZ = 2; // length field for HMAC, data only
|
||||
const int VERSION_SZ = SIZEOF_ENUM * 2; // SSL/TLS length of version
|
||||
const int DES_KEY_SZ = 8; // DES Key length
|
||||
const int DES_EDE_KEY_SZ = 24; // DES EDE Key length
|
||||
const int DES_BLOCK = 8; // DES is always fixed block size 8
|
||||
const int DES_IV_SZ = DES_BLOCK; // Init Vector length for DES
|
||||
const int RC4_KEY_SZ = 16; // RC4 Key length
|
||||
const int AES_128_KEY_SZ = 16; // AES 128bit Key length
|
||||
const int AES_192_KEY_SZ = 24; // AES 192bit Key length
|
||||
const int AES_256_KEY_SZ = 32; // AES 256bit Key length
|
||||
const int AES_BLOCK_SZ = 16; // AES 128bit block size, rfc 3268
|
||||
const int AES_IV_SZ = AES_BLOCK_SZ; // AES Init Vector length
|
||||
const int DSS_SIG_SZ = 40; // two 20 byte high byte first Integers
|
||||
const int DSS_ENCODED_EXTRA = 6; // seqID + len(1) + (intID + len(1)) * 2
|
||||
const int EVP_SALT_SZ = 8;
|
||||
const int MASTER_LABEL_SZ = 13; // TLS master secret label size
|
||||
const int KEY_LABEL_SZ = 13; // TLS key block expansion size
|
||||
const int FINISHED_LABEL_SZ = 15; // TLS finished lable length
|
||||
const int SEED_LEN = RAN_LEN * 2; // TLS seed, client + server random
|
||||
const int DEFAULT_TIMEOUT = 500; // Default Session timeout in seconds
|
||||
const int MAX_RECORD_SIZE = 16384; // 2^14, max size by standard
|
||||
const int COMPRESS_EXTRA = 1024; // extra compression possible addition
|
||||
const int SESSION_FLUSH_COUNT = 256; // when to flush session cache
|
||||
|
||||
|
||||
typedef uint8 Cipher; // first byte is always 0x00 for SSLv3 & TLS
|
||||
|
||||
typedef opaque Random[RAN_LEN];
|
||||
|
||||
typedef opaque* DistinguishedName;
|
||||
|
||||
typedef bool IsExportable;
|
||||
|
||||
|
||||
enum CompressionMethod { no_compression = 0, zlib = 221 };
|
||||
|
||||
enum CipherType { stream, block };
|
||||
|
||||
enum CipherChoice { change_cipher_spec_choice = 1 };
|
||||
|
||||
enum PublicValueEncoding { implicit_encoding, explicit_encoding };
|
||||
|
||||
enum ConnectionEnd { server_end, client_end };
|
||||
|
||||
enum AlertLevel { warning = 1, fatal = 2 };
|
||||
|
||||
|
||||
|
||||
// Record Layer Header identifier from page 12
|
||||
enum ContentType {
|
||||
no_type = 0,
|
||||
change_cipher_spec = 20,
|
||||
alert = 21,
|
||||
handshake = 22,
|
||||
application_data = 23
|
||||
};
|
||||
|
||||
|
||||
// HandShake Layer Header identifier from page 20
|
||||
enum HandShakeType {
|
||||
no_shake = -1,
|
||||
hello_request = 0,
|
||||
client_hello = 1,
|
||||
server_hello = 2,
|
||||
certificate = 11,
|
||||
server_key_exchange = 12,
|
||||
certificate_request = 13,
|
||||
server_hello_done = 14,
|
||||
certificate_verify = 15,
|
||||
client_key_exchange = 16,
|
||||
finished = 20
|
||||
};
|
||||
|
||||
|
||||
// Valid Alert types from page 16/17
|
||||
enum AlertDescription {
|
||||
close_notify = 0,
|
||||
unexpected_message = 10,
|
||||
bad_record_mac = 20,
|
||||
decompression_failure = 30,
|
||||
handshake_failure = 40,
|
||||
no_certificate = 41,
|
||||
bad_certificate = 42,
|
||||
unsupported_certificate = 43,
|
||||
certificate_revoked = 44,
|
||||
certificate_expired = 45,
|
||||
certificate_unknown = 46,
|
||||
illegal_parameter = 47
|
||||
};
|
||||
|
||||
|
||||
// Supported Key Exchange Protocols
|
||||
enum KeyExchangeAlgorithm {
|
||||
no_kea = 0,
|
||||
rsa_kea,
|
||||
diffie_hellman_kea,
|
||||
fortezza_kea
|
||||
};
|
||||
|
||||
|
||||
// Supported Authentication Schemes
|
||||
enum SignatureAlgorithm {
|
||||
anonymous_sa_algo = 0,
|
||||
rsa_sa_algo,
|
||||
dsa_sa_algo
|
||||
};
|
||||
|
||||
|
||||
// Valid client certificate request types from page 27
|
||||
enum ClientCertificateType {
|
||||
rsa_sign = 1,
|
||||
dss_sign = 2,
|
||||
rsa_fixed_dh = 3,
|
||||
dss_fixed_dh = 4,
|
||||
rsa_ephemeral_dh = 5,
|
||||
dss_ephemeral_dh = 6,
|
||||
fortezza_kea_cert = 20
|
||||
};
|
||||
|
||||
|
||||
// Supported Ciphers from page 43
|
||||
enum BulkCipherAlgorithm {
|
||||
cipher_null,
|
||||
rc4,
|
||||
rc2,
|
||||
des,
|
||||
triple_des, // leading 3 (3des) not valid identifier
|
||||
des40,
|
||||
idea,
|
||||
aes
|
||||
};
|
||||
|
||||
|
||||
// Supported Message Authentication Codes from page 43
|
||||
enum MACAlgorithm {
|
||||
no_mac,
|
||||
md5,
|
||||
sha,
|
||||
rmd
|
||||
};
|
||||
|
||||
|
||||
// Certificate file Type
|
||||
enum CertType { Cert = 0, PrivateKey, CA };
|
||||
|
||||
|
||||
// all Cipher Suites from pages 41/42
|
||||
const Cipher SSL_NULL_WITH_NULL_NULL = 0; // { 0x00, 0x00 }
|
||||
const Cipher SSL_RSA_WITH_NULL_MD5 = 1; // { 0x00, 0x01 }
|
||||
const Cipher SSL_RSA_WITH_NULL_SHA = 2; // { 0x00, 0x02 }
|
||||
const Cipher SSL_RSA_EXPORT_WITH_RC4_40_MD5 = 3; // { 0x00, 0x03 }
|
||||
const Cipher SSL_RSA_WITH_RC4_128_MD5 = 4; // { 0x00, 0x04 }
|
||||
const Cipher SSL_RSA_WITH_RC4_128_SHA = 5; // { 0x00, 0x05 }
|
||||
const Cipher SSL_RSA_EXPORT_WITH_RC2_CBC_40_MD5 = 6; // { 0x00, 0x06 }
|
||||
const Cipher SSL_RSA_WITH_IDEA_CBC_SHA = 7; // { 0x00, 0x07 }
|
||||
const Cipher SSL_RSA_EXPORT_WITH_DES40_CBC_SHA = 8; // { 0x00, 0x08 }
|
||||
const Cipher SSL_RSA_WITH_DES_CBC_SHA = 9; // { 0x00, 0x09 }
|
||||
const Cipher SSL_RSA_WITH_3DES_EDE_CBC_SHA = 10; // { 0x00, 0x0A }
|
||||
const Cipher SSL_DH_DSS_EXPORT_WITH_DES40_CBC_SHA = 11; // { 0x00, 0x0B }
|
||||
const Cipher SSL_DH_DSS_WITH_DES_CBC_SHA = 12; // { 0x00, 0x0C }
|
||||
const Cipher SSL_DH_DSS_WITH_3DES_EDE_CBC_SHA = 13; // { 0x00, 0x0D }
|
||||
const Cipher SSL_DH_RSA_EXPORT_WITH_DES40_CBC_SHA = 14; // { 0x00, 0x0E }
|
||||
const Cipher SSL_DH_RSA_WITH_DES_CBC_SHA = 15; // { 0x00, 0x0F }
|
||||
const Cipher SSL_DH_RSA_WITH_3DES_EDE_CBC_SHA = 16; // { 0x00, 0x10 }
|
||||
const Cipher SSL_DHE_DSS_EXPORT_WITH_DES40_CBC_SHA = 17; // { 0x00, 0x11 }
|
||||
const Cipher SSL_DHE_DSS_WITH_DES_CBC_SHA = 18; // { 0x00, 0x12 }
|
||||
const Cipher SSL_DHE_DSS_WITH_3DES_EDE_CBC_SHA = 19; // { 0x00, 0x13 }
|
||||
const Cipher SSL_DHE_RSA_EXPORT_WITH_DES40_CBC_SHA = 20; // { 0x00, 0x14 }
|
||||
const Cipher SSL_DHE_RSA_WITH_DES_CBC_SHA = 21; // { 0x00, 0x15 }
|
||||
const Cipher SSL_DHE_RSA_WITH_3DES_EDE_CBC_SHA = 22; // { 0x00, 0x16 }
|
||||
const Cipher SSL_DH_anon_EXPORT_WITH_RC4_40_MD5 = 23; // { 0x00, 0x17 }
|
||||
const Cipher SSL_DH_anon_WITH_RC4_128_MD5 = 24; // { 0x00, 0x18 }
|
||||
const Cipher SSL_DH_anon_EXPORT_WITH_DES40_CBC_SHA = 25; // { 0x00, 0x19 }
|
||||
const Cipher SSL_DH_anon_WITH_DES_CBC_SHA = 26; // { 0x00, 0x1A }
|
||||
const Cipher SSL_DH_anon_WITH_3DES_EDE_CBC_SHA = 27; // { 0x00, 0x1B }
|
||||
const Cipher SSL_FORTEZZA_KEA_WITH_NULL_SHA = 28; // { 0x00, 0x1C }
|
||||
const Cipher SSL_FORTEZZA_KEA_WITH_FORTEZZA_CBC_SHA = 29; // { 0x00, 0x1D }
|
||||
const Cipher SSL_FORTEZZA_KEA_WITH_RC4_128_SHA = 30; // { 0x00, 0x1E }
|
||||
|
||||
// .. to 0x2B uses Kerberos Authentication
|
||||
|
||||
|
||||
// TLS AES extensions
|
||||
const Cipher TLS_RSA_WITH_AES_128_CBC_SHA = 47; // { 0x00, 0x2F }
|
||||
const Cipher TLS_DH_DSS_WITH_AES_128_CBC_SHA = 48; // { 0x00, 0x30 }
|
||||
const Cipher TLS_DH_RSA_WITH_AES_128_CBC_SHA = 49; // { 0x00, 0x31 }
|
||||
const Cipher TLS_DHE_DSS_WITH_AES_128_CBC_SHA = 50; // { 0x00, 0x32 }
|
||||
const Cipher TLS_DHE_RSA_WITH_AES_128_CBC_SHA = 51; // { 0x00, 0x33 }
|
||||
const Cipher TLS_DH_anon_WITH_AES_128_CBC_SHA = 52; // { 0x00, 0x34 }
|
||||
|
||||
const Cipher TLS_RSA_WITH_AES_256_CBC_SHA = 53; // { 0x00, 0x35 }
|
||||
const Cipher TLS_DH_DSS_WITH_AES_256_CBC_SHA = 54; // { 0x00, 0x36 }
|
||||
const Cipher TLS_DH_RSA_WITH_AES_256_CBC_SHA = 55; // { 0x00, 0x37 }
|
||||
const Cipher TLS_DHE_DSS_WITH_AES_256_CBC_SHA = 56; // { 0x00, 0x38 }
|
||||
const Cipher TLS_DHE_RSA_WITH_AES_256_CBC_SHA = 57; // { 0x00, 0x39 }
|
||||
const Cipher TLS_DH_anon_WITH_AES_256_CBC_SHA = 58; // { 0x00, 0x3A }
|
||||
|
||||
|
||||
// OpenPGP extensions
|
||||
|
||||
const Cipher TLS_DHE_DSS_WITH_3DES_EDE_CBC_RMD160 = 114; // { 0x00, 0x72 };
|
||||
const Cipher TLS_DHE_DSS_WITH_AES_128_CBC_RMD160 = 115; // { 0x00, 0x73 };
|
||||
const Cipher TLS_DHE_DSS_WITH_AES_256_CBC_RMD160 = 116; // { 0x00, 0x74 };
|
||||
const Cipher TLS_DHE_RSA_WITH_3DES_EDE_CBC_RMD160 = 119; // { 0x00, 0x77 };
|
||||
const Cipher TLS_DHE_RSA_WITH_AES_128_CBC_RMD160 = 120; // { 0x00, 0x78 };
|
||||
const Cipher TLS_DHE_RSA_WITH_AES_256_CBC_RMD160 = 121; // { 0x00, 0x79 };
|
||||
const Cipher TLS_RSA_WITH_3DES_EDE_CBC_RMD160 = 124; // { 0x00, 0x7C };
|
||||
const Cipher TLS_RSA_WITH_AES_128_CBC_RMD160 = 125; // { 0x00, 0x7D };
|
||||
const Cipher TLS_RSA_WITH_AES_256_CBC_RMD160 = 126; // { 0x00, 0x7E };
|
||||
|
||||
|
||||
const char* const null_str = "";
|
||||
|
||||
const char* const cipher_names[128] =
|
||||
{
|
||||
null_str, // SSL_NULL_WITH_NULL_NULL = 0
|
||||
null_str, // SSL_RSA_WITH_NULL_MD5 = 1
|
||||
null_str, // SSL_RSA_WITH_NULL_SHA = 2
|
||||
null_str, // SSL_RSA_EXPORT_WITH_RC4_40_MD5 = 3
|
||||
"RC4-MD5", // SSL_RSA_WITH_RC4_128_MD5 = 4
|
||||
"RC4-SHA", // SSL_RSA_WITH_RC4_128_SHA = 5
|
||||
null_str, // SSL_RSA_EXPORT_WITH_RC2_CBC_40_MD5 = 6
|
||||
null_str, // SSL_RSA_WITH_IDEA_CBC_SHA = 7
|
||||
null_str, // SSL_RSA_EXPORT_WITH_DES40_CBC_SHA = 8
|
||||
"DES-CBC-SHA", // SSL_RSA_WITH_DES_CBC_SHA = 9
|
||||
"DES-CBC3-SHA", // SSL_RSA_WITH_3DES_EDE_CBC_SHA = 10
|
||||
|
||||
null_str, // SSL_DH_DSS_EXPORT_WITH_DES40_CBC_SHA = 11
|
||||
null_str, // SSL_DH_DSS_WITH_DES_CBC_SHA = 12
|
||||
null_str, // SSL_DH_DSS_WITH_3DES_EDE_CBC_SHA = 13
|
||||
null_str, // SSL_DH_RSA_EXPORT_WITH_DES40_CBC_SHA = 14
|
||||
null_str, // SSL_DH_RSA_WITH_DES_CBC_SHA = 15
|
||||
null_str, // SSL_DH_RSA_WITH_3DES_EDE_CBC_SHA = 16
|
||||
null_str, // SSL_DHE_DSS_EXPORT_WITH_DES40_CBC_SHA = 17
|
||||
"EDH-DSS-DES-CBC-SHA", // SSL_DHE_DSS_WITH_DES_CBC_SHA = 18
|
||||
"EDH-DSS-DES-CBC3-SHA", // SSL_DHE_DSS_WITH_3DES_EDE_CBC_SHA = 19
|
||||
null_str, // SSL_DHE_RSA_EXPORT_WITH_DES40_CBC_SHA = 20
|
||||
|
||||
"EDH-RSA-DES-CBC-SHA", // SSL_DHE_RSA_WITH_DES_CBC_SHA = 21
|
||||
"EDH-RSA-DES-CBC3-SHA", // SSL_DHE_RSA_WITH_3DES_EDE_CBC_SHA = 22
|
||||
null_str, // SSL_DH_anon_EXPORT_WITH_RC4_40_MD5 = 23
|
||||
null_str, // SSL_DH_anon_WITH_RC4_128_MD5 = 24
|
||||
null_str, // SSL_DH_anon_EXPORT_WITH_DES40_CBC_SHA = 25
|
||||
null_str, // SSL_DH_anon_WITH_DES_CBC_SHA = 26
|
||||
null_str, // SSL_DH_anon_WITH_3DES_EDE_CBC_SHA = 27
|
||||
null_str, // SSL_FORTEZZA_KEA_WITH_NULL_SHA = 28
|
||||
null_str, // SSL_FORTEZZA_KEA_WITH_FORTEZZA_CBC_SHA = 29
|
||||
null_str, // SSL_FORTEZZA_KEA_WITH_RC4_128_SHA = 30
|
||||
|
||||
null_str, null_str, null_str, null_str, null_str, // 31 - 35
|
||||
null_str, null_str, null_str, null_str, null_str, // 36 - 40
|
||||
null_str, null_str, null_str, null_str, null_str, // 41 - 45
|
||||
null_str, // 46
|
||||
|
||||
// TLS AES extensions
|
||||
"AES128-SHA", // TLS_RSA_WITH_AES_128_CBC_SHA = 47
|
||||
null_str, // TLS_DH_DSS_WITH_AES_128_CBC_SHA = 48
|
||||
null_str, // TLS_DH_RSA_WITH_AES_128_CBC_SHA = 49
|
||||
"DHE-DSS-AES128-SHA", // TLS_DHE_DSS_WITH_AES_128_CBC_SHA = 50
|
||||
"DHE-RSA-AES128-SHA", // TLS_DHE_RSA_WITH_AES_128_CBC_SHA = 51
|
||||
null_str, // TLS_DH_anon_WITH_AES_128_CBC_SHA = 52
|
||||
|
||||
"AES256-SHA", // TLS_RSA_WITH_AES_256_CBC_SHA = 53
|
||||
null_str, // TLS_DH_DSS_WITH_AES_256_CBC_SHA = 54
|
||||
null_str, // TLS_DH_RSA_WITH_AES_256_CBC_SHA = 55
|
||||
"DHE-DSS-AES256-SHA", // TLS_DHE_DSS_WITH_AES_256_CBC_SHA = 56
|
||||
"DHE-RSA-AES256-SHA", // TLS_DHE_RSA_WITH_AES_256_CBC_SHA = 57
|
||||
null_str, // TLS_DH_anon_WITH_AES_256_CBC_SHA = 58
|
||||
|
||||
null_str, // 59
|
||||
null_str, // 60
|
||||
null_str, null_str, null_str, null_str, null_str, // 61 - 65
|
||||
null_str, null_str, null_str, null_str, null_str, // 66 - 70
|
||||
null_str, null_str, null_str, null_str, null_str, // 71 - 75
|
||||
null_str, null_str, null_str, null_str, null_str, // 76 - 80
|
||||
null_str, null_str, null_str, null_str, null_str, // 81 - 85
|
||||
null_str, null_str, null_str, null_str, null_str, // 86 - 90
|
||||
null_str, null_str, null_str, null_str, null_str, // 91 - 95
|
||||
null_str, null_str, null_str, null_str, null_str, // 96 - 100
|
||||
null_str, null_str, null_str, null_str, null_str, // 101 - 105
|
||||
null_str, null_str, null_str, null_str, null_str, // 106 - 110
|
||||
null_str, null_str, null_str, // 111 - 113
|
||||
|
||||
"DHE-DSS-DES-CBC3-RMD", // TLS_DHE_DSS_WITH_3DES_EDE_CBC_RMD160 = 114
|
||||
"DHE-DSS-AES128-RMD", // TLS_DHE_DSS_WITH_AES_128_CBC_RMD160 = 115
|
||||
"DHE-DSS-AES256-RMD", // TLS_DHE_DSS_WITH_AES_256_CBC_RMD160 = 116
|
||||
null_str, // 117
|
||||
null_str, // 118
|
||||
"DHE-RSA-DES-CBC3-RMD", // TLS_DHE_RSA_WITH_3DES_EDE_CBC_RMD160 = 119
|
||||
"DHE-RSA-AES128-RMD", // TLS_DHE_RSA_WITH_AES_128_CBC_RMD160 = 120
|
||||
"DHE-RSA-AES256-RMD", // TLS_DHE_RSA_WITH_AES_256_CBC_RMD160 = 121
|
||||
null_str, // 122
|
||||
null_str, // 123
|
||||
"DES-CBC3-RMD", // TLS_RSA_WITH_3DES_EDE_CBC_RMD160 = 124
|
||||
"AES128-RMD", // TLS_RSA_WITH_AES_128_CBC_RMD160 = 125
|
||||
"AES256-RMD", // TLS_RSA_WITH_AES_256_CBC_RMD160 = 126
|
||||
null_str // 127
|
||||
};
|
||||
|
||||
// fill with MD5 pad size since biggest required
|
||||
const opaque PAD1[PAD_MD5] = { 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36,
|
||||
0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36,
|
||||
0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36,
|
||||
0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36,
|
||||
0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36,
|
||||
0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36
|
||||
};
|
||||
const opaque PAD2[PAD_MD5] = { 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c,
|
||||
0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c,
|
||||
0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c,
|
||||
0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c,
|
||||
0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c,
|
||||
0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c
|
||||
};
|
||||
|
||||
const opaque client[SIZEOF_SENDER] = { 0x43, 0x4C, 0x4E, 0x54 };
|
||||
const opaque server[SIZEOF_SENDER] = { 0x53, 0x52, 0x56, 0x52 };
|
||||
|
||||
const opaque tls_client[FINISHED_LABEL_SZ + 1] = "client finished";
|
||||
const opaque tls_server[FINISHED_LABEL_SZ + 1] = "server finished";
|
||||
|
||||
const opaque master_label[MASTER_LABEL_SZ + 1] = "master secret";
|
||||
const opaque key_label [KEY_LABEL_SZ + 1] = "key expansion";
|
||||
|
||||
|
||||
} // naemspace
|
||||
|
||||
#if __GNUC__ == 2 && __GNUC_MINOR__ <= 96
|
||||
/*
|
||||
gcc 2.96 bails out because of two declarations of byte: yaSSL::byte and
|
||||
TaoCrypt::byte. TODO: define global types.hpp and move the declaration of
|
||||
'byte' there.
|
||||
*/
|
||||
using yaSSL::byte;
|
||||
#endif
|
||||
|
||||
|
||||
#endif // yaSSL_TYPES_HPP
|
||||
+279
@@ -0,0 +1,279 @@
|
||||
/*
|
||||
Copyright (C) 2000-2007 MySQL AB
|
||||
|
||||
This program is free software; you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation; version 2 of the License.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program; see the file COPYING. If not, write to the
|
||||
Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
|
||||
MA 02110-1301 USA.
|
||||
*/
|
||||
|
||||
|
||||
/* yaSSL buffer header implements input/output buffers to simulate streaming
|
||||
* with SSL types and sockets
|
||||
*/
|
||||
|
||||
|
||||
#include <string.h> // memcpy
|
||||
#include "runtime.hpp"
|
||||
#include "buffer.hpp"
|
||||
#include "yassl_types.hpp"
|
||||
|
||||
namespace yaSSL {
|
||||
|
||||
|
||||
|
||||
// Checking Policy should implement a check function that tests whether the
|
||||
// index is within the size limit of the array
|
||||
|
||||
void Check::check(uint i, uint limit)
|
||||
{
|
||||
assert(i < limit);
|
||||
}
|
||||
|
||||
|
||||
void NoCheck::check(uint, uint)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
/* input_buffer operates like a smart c style array with a checking option,
|
||||
* meant to be read from through [] with AUTO index or read().
|
||||
* Should only write to at/near construction with assign() or raw (e.g., recv)
|
||||
* followed by add_size with the number of elements added by raw write.
|
||||
*
|
||||
* Not using vector because need checked []access, offset, and the ability to
|
||||
* write to the buffer bulk wise and have the correct size
|
||||
*/
|
||||
|
||||
|
||||
input_buffer::input_buffer()
|
||||
: size_(0), current_(0), buffer_(0), end_(0)
|
||||
{}
|
||||
|
||||
|
||||
input_buffer::input_buffer(uint s)
|
||||
: size_(0), current_(0), buffer_(NEW_YS byte[s]), end_(buffer_ + s)
|
||||
{}
|
||||
|
||||
|
||||
// with assign
|
||||
input_buffer::input_buffer(uint s, const byte* t, uint len)
|
||||
: size_(0), current_(0), buffer_(NEW_YS byte[s]), end_(buffer_ + s)
|
||||
{
|
||||
assign(t, len);
|
||||
}
|
||||
|
||||
|
||||
input_buffer::~input_buffer()
|
||||
{
|
||||
ysArrayDelete(buffer_);
|
||||
}
|
||||
|
||||
|
||||
// users can pass defualt zero length buffer and then allocate
|
||||
void input_buffer::allocate(uint s)
|
||||
{
|
||||
assert(!buffer_); // find realloc error
|
||||
buffer_ = NEW_YS byte[s];
|
||||
end_ = buffer_ + s;
|
||||
}
|
||||
|
||||
|
||||
// for passing to raw writing functions at beginning, then use add_size
|
||||
byte* input_buffer::get_buffer() const
|
||||
{
|
||||
return buffer_;
|
||||
}
|
||||
|
||||
|
||||
// after a raw write user can set NEW_YS size
|
||||
// if you know the size before the write use assign()
|
||||
void input_buffer::add_size(uint i)
|
||||
{
|
||||
check(size_ + i-1, get_capacity());
|
||||
size_ += i;
|
||||
}
|
||||
|
||||
|
||||
uint input_buffer::get_capacity() const
|
||||
{
|
||||
return end_ - buffer_;
|
||||
}
|
||||
|
||||
|
||||
uint input_buffer::get_current() const
|
||||
{
|
||||
return current_;
|
||||
}
|
||||
|
||||
|
||||
uint input_buffer::get_size() const
|
||||
{
|
||||
return size_;
|
||||
}
|
||||
|
||||
|
||||
uint input_buffer::get_remaining() const
|
||||
{
|
||||
return size_ - current_;
|
||||
}
|
||||
|
||||
|
||||
void input_buffer::set_current(uint i)
|
||||
{
|
||||
if (i)
|
||||
check(i - 1, size_);
|
||||
current_ = i;
|
||||
}
|
||||
|
||||
|
||||
// read only access through [], advance current
|
||||
// user passes in AUTO index for ease of use
|
||||
const byte& input_buffer::operator[](uint i)
|
||||
{
|
||||
assert (i == AUTO);
|
||||
check(current_, size_);
|
||||
return buffer_[current_++];
|
||||
}
|
||||
|
||||
|
||||
// end of input test
|
||||
bool input_buffer::eof()
|
||||
{
|
||||
return current_ >= size_;
|
||||
}
|
||||
|
||||
|
||||
// peek ahead
|
||||
byte input_buffer::peek() const
|
||||
{
|
||||
return buffer_[current_];
|
||||
}
|
||||
|
||||
|
||||
// write function, should use at/near construction
|
||||
void input_buffer::assign(const byte* t, uint s)
|
||||
{
|
||||
check(current_, get_capacity());
|
||||
add_size(s);
|
||||
memcpy(&buffer_[current_], t, s);
|
||||
}
|
||||
|
||||
|
||||
// use read to query input, adjusts current
|
||||
void input_buffer::read(byte* dst, uint length)
|
||||
{
|
||||
check(current_ + length - 1, size_);
|
||||
memcpy(dst, &buffer_[current_], length);
|
||||
current_ += length;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/* output_buffer operates like a smart c style array with a checking option.
|
||||
* Meant to be written to through [] with AUTO index or write().
|
||||
* Size (current) counter increases when written to. Can be constructed with
|
||||
* zero length buffer but be sure to allocate before first use.
|
||||
* Don't use add write for a couple bytes, use [] instead, way less overhead.
|
||||
*
|
||||
* Not using vector because need checked []access and the ability to
|
||||
* write to the buffer bulk wise and retain correct size
|
||||
*/
|
||||
|
||||
|
||||
output_buffer::output_buffer()
|
||||
: current_(0), buffer_(0), end_(0)
|
||||
{}
|
||||
|
||||
|
||||
// with allocate
|
||||
output_buffer::output_buffer(uint s)
|
||||
: current_(0), buffer_(NEW_YS byte[s]), end_(buffer_ + s)
|
||||
{}
|
||||
|
||||
|
||||
// with assign
|
||||
output_buffer::output_buffer(uint s, const byte* t, uint len)
|
||||
: current_(0), buffer_(NEW_YS byte[s]), end_(buffer_+ s)
|
||||
{
|
||||
write(t, len);
|
||||
}
|
||||
|
||||
|
||||
output_buffer::~output_buffer()
|
||||
{
|
||||
ysArrayDelete(buffer_);
|
||||
}
|
||||
|
||||
|
||||
uint output_buffer::get_size() const
|
||||
{
|
||||
return current_;
|
||||
}
|
||||
|
||||
|
||||
uint output_buffer::get_capacity() const
|
||||
{
|
||||
return end_ - buffer_;
|
||||
}
|
||||
|
||||
|
||||
void output_buffer::set_current(uint c)
|
||||
{
|
||||
check(c, get_capacity());
|
||||
current_ = c;
|
||||
}
|
||||
|
||||
|
||||
// users can pass defualt zero length buffer and then allocate
|
||||
void output_buffer::allocate(uint s)
|
||||
{
|
||||
assert(!buffer_); // find realloc error
|
||||
buffer_ = NEW_YS byte[s]; end_ = buffer_ + s;
|
||||
}
|
||||
|
||||
|
||||
// for passing to reading functions when finished
|
||||
const byte* output_buffer::get_buffer() const
|
||||
{
|
||||
return buffer_;
|
||||
}
|
||||
|
||||
|
||||
// allow write access through [], update current
|
||||
// user passes in AUTO as index for ease of use
|
||||
byte& output_buffer::operator[](uint i)
|
||||
{
|
||||
assert(i == AUTO);
|
||||
check(current_, get_capacity());
|
||||
return buffer_[current_++];
|
||||
}
|
||||
|
||||
|
||||
// end of output test
|
||||
bool output_buffer::eof()
|
||||
{
|
||||
return current_ >= get_capacity();
|
||||
}
|
||||
|
||||
|
||||
void output_buffer::write(const byte* t, uint s)
|
||||
{
|
||||
check(current_ + s - 1, get_capacity());
|
||||
memcpy(&buffer_[current_], t, s);
|
||||
current_ += s;
|
||||
}
|
||||
|
||||
|
||||
|
||||
} // naemspace
|
||||
|
||||
+370
@@ -0,0 +1,370 @@
|
||||
/*
|
||||
Copyright 2000-2008 MySQL AB, 2008 Sun Microsystems, Inc.
|
||||
|
||||
This program is free software; you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation; version 2 of the License.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program; see the file COPYING. If not, write to the
|
||||
Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
|
||||
MA 02110-1301 USA.
|
||||
*/
|
||||
|
||||
|
||||
/* The certificate wrapper source implements certificate management functions
|
||||
*
|
||||
*/
|
||||
|
||||
#include "runtime.hpp"
|
||||
#include "cert_wrapper.hpp"
|
||||
#include "yassl_int.hpp"
|
||||
#include "error.hpp"
|
||||
|
||||
#if defined(USE_CML_LIB)
|
||||
#include "cmapi_cpp.h"
|
||||
#else
|
||||
#include "asn.hpp"
|
||||
#include "file.hpp"
|
||||
#endif // USE_CML_LIB
|
||||
|
||||
|
||||
namespace yaSSL {
|
||||
|
||||
|
||||
x509::x509(uint sz) : length_(sz), buffer_(NEW_YS opaque[sz])
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
x509::~x509()
|
||||
{
|
||||
ysArrayDelete(buffer_);
|
||||
}
|
||||
|
||||
|
||||
x509::x509(const x509& that) : length_(that.length_),
|
||||
buffer_(NEW_YS opaque[length_])
|
||||
{
|
||||
memcpy(buffer_, that.buffer_, length_);
|
||||
}
|
||||
|
||||
|
||||
void x509::Swap(x509& that)
|
||||
{
|
||||
STL::swap(length_, that.length_);
|
||||
STL::swap(buffer_, that.buffer_);
|
||||
}
|
||||
|
||||
|
||||
x509& x509::operator=(const x509& that)
|
||||
{
|
||||
x509 temp(that);
|
||||
Swap(temp);
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
uint x509::get_length() const
|
||||
{
|
||||
return length_;
|
||||
}
|
||||
|
||||
|
||||
const opaque* x509::get_buffer() const
|
||||
{
|
||||
return buffer_;
|
||||
}
|
||||
|
||||
|
||||
opaque* x509::use_buffer()
|
||||
{
|
||||
return buffer_;
|
||||
}
|
||||
|
||||
|
||||
//CertManager
|
||||
CertManager::CertManager()
|
||||
: peerX509_(0), verifyPeer_(false), verifyNone_(false), failNoCert_(false),
|
||||
sendVerify_(false), verifyCallback_(0)
|
||||
{}
|
||||
|
||||
|
||||
CertManager::~CertManager()
|
||||
{
|
||||
ysDelete(peerX509_);
|
||||
|
||||
STL::for_each(signers_.begin(), signers_.end(), del_ptr_zero()) ;
|
||||
|
||||
STL::for_each(peerList_.begin(), peerList_.end(), del_ptr_zero()) ;
|
||||
|
||||
STL::for_each(list_.begin(), list_.end(), del_ptr_zero()) ;
|
||||
}
|
||||
|
||||
|
||||
bool CertManager::verifyPeer() const
|
||||
{
|
||||
return verifyPeer_;
|
||||
}
|
||||
|
||||
|
||||
bool CertManager::verifyNone() const
|
||||
{
|
||||
return verifyNone_;
|
||||
}
|
||||
|
||||
|
||||
bool CertManager::failNoCert() const
|
||||
{
|
||||
return failNoCert_;
|
||||
}
|
||||
|
||||
|
||||
bool CertManager::sendVerify() const
|
||||
{
|
||||
return sendVerify_;
|
||||
}
|
||||
|
||||
|
||||
void CertManager::setVerifyPeer()
|
||||
{
|
||||
verifyPeer_ = true;
|
||||
}
|
||||
|
||||
|
||||
void CertManager::setVerifyNone()
|
||||
{
|
||||
verifyNone_ = true;
|
||||
}
|
||||
|
||||
|
||||
void CertManager::setFailNoCert()
|
||||
{
|
||||
failNoCert_ = true;
|
||||
}
|
||||
|
||||
|
||||
void CertManager::setSendVerify()
|
||||
{
|
||||
sendVerify_ = true;
|
||||
}
|
||||
|
||||
|
||||
void CertManager::setVerifyCallback(VerifyCallback vc)
|
||||
{
|
||||
verifyCallback_ = vc;
|
||||
}
|
||||
|
||||
|
||||
void CertManager::AddPeerCert(x509* x)
|
||||
{
|
||||
peerList_.push_back(x); // take ownership
|
||||
}
|
||||
|
||||
|
||||
void CertManager::CopySelfCert(const x509* x)
|
||||
{
|
||||
if (x)
|
||||
list_.push_back(NEW_YS x509(*x));
|
||||
}
|
||||
|
||||
|
||||
// add to signers
|
||||
int CertManager::CopyCaCert(const x509* x)
|
||||
{
|
||||
TaoCrypt::Source source(x->get_buffer(), x->get_length());
|
||||
TaoCrypt::CertDecoder cert(source, true, &signers_, verifyNone_,
|
||||
TaoCrypt::CertDecoder::CA);
|
||||
|
||||
if (!cert.GetError().What()) {
|
||||
const TaoCrypt::PublicKey& key = cert.GetPublicKey();
|
||||
signers_.push_back(NEW_YS TaoCrypt::Signer(key.GetKey(), key.size(),
|
||||
cert.GetCommonName(), cert.GetHash()));
|
||||
}
|
||||
// just don't add, not an error return cert.GetError().What();
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
const x509* CertManager::get_cert() const
|
||||
{
|
||||
return list_.front();
|
||||
}
|
||||
|
||||
|
||||
const opaque* CertManager::get_peerKey() const
|
||||
{
|
||||
return peerPublicKey_.get_buffer();
|
||||
}
|
||||
|
||||
|
||||
X509* CertManager::get_peerX509() const
|
||||
{
|
||||
return peerX509_;
|
||||
}
|
||||
|
||||
|
||||
SignatureAlgorithm CertManager::get_peerKeyType() const
|
||||
{
|
||||
return peerKeyType_;
|
||||
}
|
||||
|
||||
|
||||
SignatureAlgorithm CertManager::get_keyType() const
|
||||
{
|
||||
return keyType_;
|
||||
}
|
||||
|
||||
|
||||
uint CertManager::get_peerKeyLength() const
|
||||
{
|
||||
return peerPublicKey_.get_size();
|
||||
}
|
||||
|
||||
|
||||
const opaque* CertManager::get_privateKey() const
|
||||
{
|
||||
return privateKey_.get_buffer();
|
||||
}
|
||||
|
||||
|
||||
uint CertManager::get_privateKeyLength() const
|
||||
{
|
||||
return privateKey_.get_size();
|
||||
}
|
||||
|
||||
|
||||
// Validate the peer's certificate list, from root to peer (last to first)
|
||||
int CertManager::Validate()
|
||||
{
|
||||
CertList::reverse_iterator last = peerList_.rbegin();
|
||||
size_t count = peerList_.size();
|
||||
|
||||
while ( count > 1 ) {
|
||||
TaoCrypt::Source source((*last)->get_buffer(), (*last)->get_length());
|
||||
TaoCrypt::CertDecoder cert(source, true, &signers_, verifyNone_);
|
||||
|
||||
if (int err = cert.GetError().What())
|
||||
return err;
|
||||
|
||||
const TaoCrypt::PublicKey& key = cert.GetPublicKey();
|
||||
signers_.push_back(NEW_YS TaoCrypt::Signer(key.GetKey(), key.size(),
|
||||
cert.GetCommonName(), cert.GetHash()));
|
||||
++last;
|
||||
--count;
|
||||
}
|
||||
|
||||
if (count) {
|
||||
// peer's is at the front
|
||||
TaoCrypt::Source source((*last)->get_buffer(), (*last)->get_length());
|
||||
TaoCrypt::CertDecoder cert(source, true, &signers_, verifyNone_);
|
||||
|
||||
int err = cert.GetError().What();
|
||||
if ( err )
|
||||
return err;
|
||||
|
||||
uint sz = cert.GetPublicKey().size();
|
||||
peerPublicKey_.allocate(sz);
|
||||
peerPublicKey_.assign(cert.GetPublicKey().GetKey(), sz);
|
||||
|
||||
if (cert.GetKeyType() == TaoCrypt::RSAk)
|
||||
peerKeyType_ = rsa_sa_algo;
|
||||
else
|
||||
peerKeyType_ = dsa_sa_algo;
|
||||
|
||||
size_t iSz = strlen(cert.GetIssuer()) + 1;
|
||||
size_t sSz = strlen(cert.GetCommonName()) + 1;
|
||||
int bSz = (int)strlen(cert.GetBeforeDate()) + 1;
|
||||
int aSz = (int)strlen(cert.GetAfterDate()) + 1;
|
||||
peerX509_ = NEW_YS X509(cert.GetIssuer(), iSz, cert.GetCommonName(),
|
||||
sSz, cert.GetBeforeDate(), bSz,
|
||||
cert.GetAfterDate(), aSz);
|
||||
|
||||
if (err == TaoCrypt::SIG_OTHER_E && verifyCallback_) {
|
||||
X509_STORE_CTX store;
|
||||
store.error = err;
|
||||
store.error_depth = static_cast<int>(count) - 1;
|
||||
store.current_cert = peerX509_;
|
||||
|
||||
int ok = verifyCallback_(0, &store);
|
||||
if (ok) return 0;
|
||||
}
|
||||
|
||||
if (err == TaoCrypt::SIG_OTHER_E) return err;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
// Set the private key
|
||||
int CertManager::SetPrivateKey(const x509& key)
|
||||
{
|
||||
privateKey_.allocate(key.get_length());
|
||||
privateKey_.assign(key.get_buffer(), key.get_length());
|
||||
|
||||
// set key type
|
||||
if (x509* cert = list_.front()) {
|
||||
TaoCrypt::Source source(cert->get_buffer(), cert->get_length());
|
||||
TaoCrypt::CertDecoder cd(source, false);
|
||||
cd.DecodeToKey();
|
||||
if (int err = cd.GetError().What())
|
||||
return err;
|
||||
if (cd.GetKeyType() == TaoCrypt::RSAk)
|
||||
keyType_ = rsa_sa_algo;
|
||||
else
|
||||
keyType_ = dsa_sa_algo;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
// Store OpenSSL type peer's cert
|
||||
void CertManager::setPeerX509(X509* x)
|
||||
{
|
||||
assert(peerX509_ == 0);
|
||||
if (x == 0) return;
|
||||
|
||||
X509_NAME* issuer = x->GetIssuer();
|
||||
X509_NAME* subject = x->GetSubject();
|
||||
ASN1_STRING* before = x->GetBefore();
|
||||
ASN1_STRING* after = x->GetAfter();
|
||||
|
||||
peerX509_ = NEW_YS X509(issuer->GetName(), issuer->GetLength(),
|
||||
subject->GetName(), subject->GetLength(), (const char*) before->data,
|
||||
before->length, (const char*) after->data, after->length);
|
||||
}
|
||||
|
||||
|
||||
#if defined(USE_CML_LIB)
|
||||
|
||||
// Get the peer's certificate, extract and save public key
|
||||
void CertManager::SetPeerKey()
|
||||
{
|
||||
// first cert is the peer's
|
||||
x509* main = peerList_.front();
|
||||
|
||||
Bytes_struct cert;
|
||||
cert.num = main->get_length();
|
||||
cert.data = main->set_buffer();
|
||||
|
||||
CML::Certificate cm(cert);
|
||||
const CML::ASN::Cert& raw = cm.base();
|
||||
CTIL::CSM_Buffer key = raw.pubKeyInfo.key;
|
||||
|
||||
uint sz;
|
||||
opaque* key_buffer = reinterpret_cast<opaque*>(key.Get(sz));
|
||||
peerPublicKey_.allocate(sz);
|
||||
peerPublicKey_.assign(key_buffer, sz);
|
||||
}
|
||||
|
||||
|
||||
#endif // USE_CML_LIB
|
||||
|
||||
|
||||
|
||||
} // namespace
|
||||
+1013
File diff suppressed because it is too large
Load Diff
+4
@@ -0,0 +1,4 @@
|
||||
/*
|
||||
To make libtool always use a C++ linker when compiling with yaSSL we need
|
||||
to add a dummy C++ file to the source list.
|
||||
*/
|
||||
+1117
File diff suppressed because it is too large
Load Diff
+87
@@ -0,0 +1,87 @@
|
||||
/*
|
||||
Copyright (C) 2000-2007 MySQL AB
|
||||
|
||||
This program is free software; you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation; version 2 of the License.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program; see the file COPYING. If not, write to the
|
||||
Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
|
||||
MA 02110-1301 USA.
|
||||
*/
|
||||
|
||||
/* Locking functions
|
||||
*/
|
||||
|
||||
#include "runtime.hpp"
|
||||
#include "lock.hpp"
|
||||
|
||||
|
||||
namespace yaSSL {
|
||||
|
||||
|
||||
#ifdef MULTI_THREADED
|
||||
#ifdef _WIN32
|
||||
|
||||
Mutex::Mutex()
|
||||
{
|
||||
InitializeCriticalSection(&cs_);
|
||||
}
|
||||
|
||||
|
||||
Mutex::~Mutex()
|
||||
{
|
||||
DeleteCriticalSection(&cs_);
|
||||
}
|
||||
|
||||
|
||||
Mutex::Lock::Lock(Mutex& lm) : mutex_(lm)
|
||||
{
|
||||
EnterCriticalSection(&mutex_.cs_);
|
||||
}
|
||||
|
||||
|
||||
Mutex::Lock::~Lock()
|
||||
{
|
||||
LeaveCriticalSection(&mutex_.cs_);
|
||||
}
|
||||
|
||||
#else // _WIN32
|
||||
|
||||
Mutex::Mutex()
|
||||
{
|
||||
pthread_mutex_init(&mutex_, 0);
|
||||
}
|
||||
|
||||
|
||||
Mutex::~Mutex()
|
||||
{
|
||||
pthread_mutex_destroy(&mutex_);
|
||||
}
|
||||
|
||||
|
||||
Mutex::Lock::Lock(Mutex& lm) : mutex_(lm)
|
||||
{
|
||||
pthread_mutex_lock(&mutex_.mutex_);
|
||||
}
|
||||
|
||||
|
||||
Mutex::Lock::~Lock()
|
||||
{
|
||||
pthread_mutex_unlock(&mutex_.mutex_);
|
||||
}
|
||||
|
||||
|
||||
#endif // _WIN32
|
||||
#endif // MULTI_THREADED
|
||||
|
||||
|
||||
|
||||
} // namespace yaSSL
|
||||
|
||||
+146
@@ -0,0 +1,146 @@
|
||||
/*
|
||||
Copyright (C) 2000-2007 MySQL AB
|
||||
|
||||
This program is free software; you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation; version 2 of the License.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program; see the file COPYING. If not, write to the
|
||||
Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
|
||||
MA 02110-1301 USA.
|
||||
*/
|
||||
|
||||
/* Debug logging functions
|
||||
*/
|
||||
|
||||
|
||||
#include "runtime.hpp"
|
||||
#include "log.hpp"
|
||||
|
||||
#ifdef YASSL_LOG
|
||||
#include <time.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
namespace yaSSL {
|
||||
|
||||
|
||||
#ifdef YASSL_LOG
|
||||
|
||||
enum { MAX_MSG = 81 };
|
||||
|
||||
Log::Log(const char* str)
|
||||
{
|
||||
log_ = fopen(str, "w");
|
||||
Trace("********** Logger Attached **********");
|
||||
}
|
||||
|
||||
|
||||
Log::~Log()
|
||||
{
|
||||
Trace("********** Logger Detached **********");
|
||||
fclose(log_);
|
||||
}
|
||||
|
||||
|
||||
// Trace a message
|
||||
void Log::Trace(const char* str)
|
||||
{
|
||||
if (!log_) return;
|
||||
|
||||
time_t clicks = time(0);
|
||||
char timeStr[32];
|
||||
|
||||
// get rid of newline
|
||||
strncpy(timeStr, ctime(&clicks), sizeof(timeStr));
|
||||
unsigned int len = strlen(timeStr);
|
||||
timeStr[len - 1] = 0;
|
||||
|
||||
char msg[MAX_MSG];
|
||||
|
||||
strncpy(msg, timeStr, sizeof(timeStr));
|
||||
strncat(msg, ":", 1);
|
||||
strncat(msg, str, MAX_MSG - sizeof(timeStr) - 2);
|
||||
strncat(msg, "\n", 1);
|
||||
msg[MAX_MSG - 1] = 0;
|
||||
|
||||
fputs(msg, log_);
|
||||
}
|
||||
|
||||
|
||||
#if defined(_WIN32) || defined(__MACH__) || defined(__hpux__)
|
||||
typedef int socklen_t;
|
||||
#endif
|
||||
|
||||
|
||||
// write tcp address
|
||||
void Log::ShowTCP(socket_t fd, bool ended)
|
||||
{
|
||||
sockaddr_in peeraddr;
|
||||
socklen_t len = sizeof(peeraddr);
|
||||
if (getpeername(fd, (sockaddr*)&peeraddr, &len) != 0)
|
||||
return;
|
||||
|
||||
const char* p = reinterpret_cast<const char*>(&peeraddr.sin_addr);
|
||||
char msg[MAX_MSG];
|
||||
char number[16];
|
||||
|
||||
if (ended)
|
||||
strncpy(msg, "yaSSL conn DONE w/ peer ", 26);
|
||||
else
|
||||
strncpy(msg, "yaSSL conn BEGUN w/ peer ", 26);
|
||||
for (int i = 0; i < 4; ++i) {
|
||||
sprintf(number, "%u", static_cast<unsigned short>(p[i]));
|
||||
strncat(msg, number, 8);
|
||||
if (i < 3)
|
||||
strncat(msg, ".", 1);
|
||||
}
|
||||
strncat(msg, " port ", 8);
|
||||
sprintf(number, "%d", htons(peeraddr.sin_port));
|
||||
strncat(msg, number, 8);
|
||||
|
||||
msg[MAX_MSG - 1] = 0;
|
||||
Trace(msg);
|
||||
}
|
||||
|
||||
|
||||
// log processed data
|
||||
void Log::ShowData(uint bytes, bool sent)
|
||||
{
|
||||
char msg[MAX_MSG];
|
||||
char number[16];
|
||||
|
||||
if (sent)
|
||||
strncpy(msg, "Sent ", 10);
|
||||
else
|
||||
strncpy(msg, "Received ", 10);
|
||||
sprintf(number, "%u", bytes);
|
||||
strncat(msg, number, 8);
|
||||
strncat(msg, " bytes of application data", 27);
|
||||
|
||||
msg[MAX_MSG - 1] = 0;
|
||||
Trace(msg);
|
||||
}
|
||||
|
||||
|
||||
#else // no YASSL_LOG
|
||||
|
||||
|
||||
Log::Log(const char*) {}
|
||||
Log::~Log() {}
|
||||
void Log::Trace(const char*) {}
|
||||
void Log::ShowTCP(socket_t, bool) {}
|
||||
void Log::ShowData(uint, bool) {}
|
||||
|
||||
|
||||
#endif // YASSL_LOG
|
||||
} // namespace
|
||||
+199
@@ -0,0 +1,199 @@
|
||||
/*
|
||||
Copyright (C) 2000-2007 MySQL AB
|
||||
|
||||
This program is free software; you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation; version 2 of the License.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program; see the file COPYING. If not, write to the
|
||||
Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
|
||||
MA 02110-1301 USA.
|
||||
*/
|
||||
|
||||
|
||||
/* The socket wrapper source implements a Socket class that hides the
|
||||
* differences between Berkely style sockets and Windows sockets, allowing
|
||||
* transparent TCP access.
|
||||
*/
|
||||
|
||||
|
||||
#include "runtime.hpp"
|
||||
#include "socket_wrapper.hpp"
|
||||
|
||||
#ifndef _WIN32
|
||||
#include <errno.h>
|
||||
#include <netdb.h>
|
||||
#include <unistd.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <netinet/in.h>
|
||||
#include <sys/ioctl.h>
|
||||
#include <string.h>
|
||||
#include <fcntl.h>
|
||||
#endif // _WIN32
|
||||
|
||||
#if defined(__sun) || defined(__SCO_VERSION__) || defined(__NETWARE__)
|
||||
#include <sys/filio.h>
|
||||
#endif
|
||||
|
||||
#ifdef _WIN32
|
||||
const int SOCKET_EINVAL = WSAEINVAL;
|
||||
const int SOCKET_EWOULDBLOCK = WSAEWOULDBLOCK;
|
||||
const int SOCKET_EAGAIN = WSAEWOULDBLOCK;
|
||||
#else
|
||||
const int SOCKET_EINVAL = EINVAL;
|
||||
const int SOCKET_EWOULDBLOCK = EWOULDBLOCK;
|
||||
const int SOCKET_EAGAIN = EAGAIN;
|
||||
#endif // _WIN32
|
||||
|
||||
|
||||
namespace yaSSL {
|
||||
|
||||
|
||||
Socket::Socket(socket_t s)
|
||||
: socket_(s), wouldBlock_(false), nonBlocking_(false)
|
||||
{}
|
||||
|
||||
|
||||
void Socket::set_fd(socket_t s)
|
||||
{
|
||||
socket_ = s;
|
||||
}
|
||||
|
||||
|
||||
socket_t Socket::get_fd() const
|
||||
{
|
||||
return socket_;
|
||||
}
|
||||
|
||||
|
||||
Socket::~Socket()
|
||||
{
|
||||
// don't close automatically now
|
||||
}
|
||||
|
||||
|
||||
void Socket::closeSocket()
|
||||
{
|
||||
if (socket_ != INVALID_SOCKET) {
|
||||
#ifdef _WIN32
|
||||
closesocket(socket_);
|
||||
#else
|
||||
close(socket_);
|
||||
#endif
|
||||
socket_ = INVALID_SOCKET;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
uint Socket::get_ready() const
|
||||
{
|
||||
#ifdef _WIN32
|
||||
unsigned long ready = 0;
|
||||
ioctlsocket(socket_, FIONREAD, &ready);
|
||||
#else
|
||||
/*
|
||||
64-bit Solaris requires the variable passed to
|
||||
FIONREAD be a 32-bit value.
|
||||
*/
|
||||
unsigned int ready = 0;
|
||||
ioctl(socket_, FIONREAD, &ready);
|
||||
#endif
|
||||
|
||||
return ready;
|
||||
}
|
||||
|
||||
|
||||
uint Socket::send(const byte* buf, unsigned int sz, int flags) const
|
||||
{
|
||||
const byte* pos = buf;
|
||||
const byte* end = pos + sz;
|
||||
|
||||
while (pos != end) {
|
||||
int sent = ::send(socket_, reinterpret_cast<const char *>(pos),
|
||||
static_cast<int>(end - pos), flags);
|
||||
|
||||
if (sent == -1)
|
||||
return 0;
|
||||
|
||||
pos += sent;
|
||||
}
|
||||
|
||||
return sz;
|
||||
}
|
||||
|
||||
|
||||
uint Socket::receive(byte* buf, unsigned int sz, int flags)
|
||||
{
|
||||
wouldBlock_ = false;
|
||||
|
||||
int recvd = ::recv(socket_, reinterpret_cast<char *>(buf), sz, flags);
|
||||
|
||||
// idea to seperate error from would block by [email protected]
|
||||
if (recvd == -1) {
|
||||
if (get_lastError() == SOCKET_EWOULDBLOCK ||
|
||||
get_lastError() == SOCKET_EAGAIN) {
|
||||
wouldBlock_ = true; // would have blocked this time only
|
||||
nonBlocking_ = true; // socket nonblocking, win32 only way to tell
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
else if (recvd == 0)
|
||||
return static_cast<uint>(-1);
|
||||
|
||||
return recvd;
|
||||
}
|
||||
|
||||
|
||||
// wait if blocking for input, return false for error
|
||||
bool Socket::wait()
|
||||
{
|
||||
byte b;
|
||||
return receive(&b, 1, MSG_PEEK) != static_cast<uint>(-1);
|
||||
}
|
||||
|
||||
|
||||
void Socket::shutDown(int how)
|
||||
{
|
||||
shutdown(socket_, how);
|
||||
}
|
||||
|
||||
|
||||
int Socket::get_lastError()
|
||||
{
|
||||
#ifdef _WIN32
|
||||
return WSAGetLastError();
|
||||
#else
|
||||
return errno;
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
bool Socket::WouldBlock() const
|
||||
{
|
||||
return wouldBlock_;
|
||||
}
|
||||
|
||||
|
||||
bool Socket::IsNonBlocking() const
|
||||
{
|
||||
return nonBlocking_;
|
||||
}
|
||||
|
||||
|
||||
void Socket::set_lastError(int errorCode)
|
||||
{
|
||||
#ifdef _WIN32
|
||||
WSASetLastError(errorCode);
|
||||
#else
|
||||
errno = errorCode;
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
} // namespace
|
||||
+1672
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,110 @@
|
||||
/*
|
||||
Copyright (C) 2000-2007 MySQL AB
|
||||
|
||||
This program is free software; you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation; version 2 of the License.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program; see the file COPYING. If not, write to the
|
||||
Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
|
||||
MA 02110-1301 USA.
|
||||
*/
|
||||
|
||||
|
||||
/* Explicit template instantiation requests
|
||||
*/
|
||||
|
||||
|
||||
#include "runtime.hpp"
|
||||
#include "handshake.hpp"
|
||||
#include "yassl_int.hpp"
|
||||
#include "crypto_wrapper.hpp"
|
||||
#include "hmac.hpp"
|
||||
#include "md5.hpp"
|
||||
#include "sha.hpp"
|
||||
#include "ripemd.hpp"
|
||||
#include "openssl/ssl.h"
|
||||
|
||||
#ifdef HAVE_EXPLICIT_TEMPLATE_INSTANTIATION
|
||||
|
||||
namespace mySTL {
|
||||
template class list<unsigned char*>;
|
||||
template yaSSL::del_ptr_zero for_each(mySTL::list<unsigned char*>::iterator, mySTL::list<unsigned char*>::iterator, yaSSL::del_ptr_zero);
|
||||
template pair<int, yaSSL::Message* (*)()>* uninit_copy<mySTL::pair<int, yaSSL::Message* (*)()>*, mySTL::pair<int, yaSSL::Message* (*)()>*>(mySTL::pair<int, yaSSL::Message* (*)()>*, mySTL::pair<int, yaSSL::Message* (*)()>*, mySTL::pair<int, yaSSL::Message* (*)()>*);
|
||||
template pair<int, yaSSL::HandShakeBase* (*)()>* uninit_copy<mySTL::pair<int, yaSSL::HandShakeBase* (*)()>*, mySTL::pair<int, yaSSL::HandShakeBase* (*)()>*>(mySTL::pair<int, yaSSL::HandShakeBase* (*)()>*, mySTL::pair<int, yaSSL::HandShakeBase* (*)()>*, mySTL::pair<int, yaSSL::HandShakeBase* (*)()>*);
|
||||
template void destroy<mySTL::pair<int, yaSSL::Message* (*)()>*>(mySTL::pair<int, yaSSL::Message* (*)()>*, mySTL::pair<int, yaSSL::Message* (*)()>*);
|
||||
template void destroy<mySTL::pair<int, yaSSL::HandShakeBase* (*)()>*>(mySTL::pair<int, yaSSL::HandShakeBase* (*)()>*, mySTL::pair<int, yaSSL::HandShakeBase* (*)()>*);
|
||||
template pair<int, yaSSL::ServerKeyBase* (*)()>* uninit_copy<mySTL::pair<int, yaSSL::ServerKeyBase* (*)()>*, mySTL::pair<int, yaSSL::ServerKeyBase* (*)()>*>(mySTL::pair<int, yaSSL::ServerKeyBase* (*)()>*, mySTL::pair<int, yaSSL::ServerKeyBase* (*)()>*, mySTL::pair<int, yaSSL::ServerKeyBase* (*)()>*);
|
||||
template void destroy<mySTL::pair<int, yaSSL::ServerKeyBase* (*)()>*>(mySTL::pair<int, yaSSL::ServerKeyBase* (*)()>*, mySTL::pair<int, yaSSL::ServerKeyBase* (*)()>*);
|
||||
template pair<int, yaSSL::ClientKeyBase* (*)()>* uninit_copy<mySTL::pair<int, yaSSL::ClientKeyBase* (*)()>*, mySTL::pair<int, yaSSL::ClientKeyBase* (*)()>*>(mySTL::pair<int, yaSSL::ClientKeyBase* (*)()>*, mySTL::pair<int, yaSSL::ClientKeyBase* (*)()>*, mySTL::pair<int, yaSSL::ClientKeyBase* (*)()>*);
|
||||
template class list<TaoCrypt::Signer*>;
|
||||
template class list<yaSSL::SSL_SESSION*>;
|
||||
template class list<yaSSL::input_buffer*>;
|
||||
template class list<yaSSL::output_buffer*>;
|
||||
template class list<yaSSL::x509*>;
|
||||
template class list<yaSSL::Digest*>;
|
||||
template class list<yaSSL::BulkCipher*>;
|
||||
template void destroy<mySTL::pair<int, yaSSL::ClientKeyBase* (*)()>*>(mySTL::pair<int, yaSSL::ClientKeyBase* (*)()>*, mySTL::pair<int, yaSSL::ClientKeyBase* (*)()>*);
|
||||
template yaSSL::del_ptr_zero for_each<mySTL::list<TaoCrypt::Signer*>::iterator, yaSSL::del_ptr_zero>(mySTL::list<TaoCrypt::Signer*>::iterator, mySTL::list<TaoCrypt::Signer*>::iterator, yaSSL::del_ptr_zero);
|
||||
template yaSSL::del_ptr_zero for_each<mySTL::list<yaSSL::SSL_SESSION*>::iterator, yaSSL::del_ptr_zero>(mySTL::list<yaSSL::SSL_SESSION*>::iterator, mySTL::list<yaSSL::SSL_SESSION*>::iterator, yaSSL::del_ptr_zero);
|
||||
template yaSSL::del_ptr_zero for_each<mySTL::list<yaSSL::input_buffer*>::iterator, yaSSL::del_ptr_zero>(mySTL::list<yaSSL::input_buffer*>::iterator, mySTL::list<yaSSL::input_buffer*>::iterator, yaSSL::del_ptr_zero);
|
||||
template yaSSL::del_ptr_zero for_each<mySTL::list<yaSSL::output_buffer*>::iterator, yaSSL::del_ptr_zero>(mySTL::list<yaSSL::output_buffer*>::iterator, mySTL::list<yaSSL::output_buffer*>::iterator, yaSSL::del_ptr_zero);
|
||||
template yaSSL::del_ptr_zero for_each<mySTL::list<yaSSL::x509*>::iterator, yaSSL::del_ptr_zero>(mySTL::list<yaSSL::x509*>::iterator, mySTL::list<yaSSL::x509*>::iterator, yaSSL::del_ptr_zero);
|
||||
template yaSSL::del_ptr_zero for_each<mySTL::list<yaSSL::Digest*>::iterator, yaSSL::del_ptr_zero>(mySTL::list<yaSSL::Digest*>::iterator, mySTL::list<yaSSL::Digest*>::iterator, yaSSL::del_ptr_zero);
|
||||
template yaSSL::del_ptr_zero for_each<mySTL::list<yaSSL::BulkCipher*>::iterator, yaSSL::del_ptr_zero>(mySTL::list<yaSSL::BulkCipher*>::iterator, mySTL::list<yaSSL::BulkCipher*>::iterator, yaSSL::del_ptr_zero);
|
||||
template bool list<yaSSL::ThreadError>::erase(list<yaSSL::ThreadError>::iterator);
|
||||
template void list<yaSSL::ThreadError>::push_back(yaSSL::ThreadError);
|
||||
template void list<yaSSL::ThreadError>::pop_front();
|
||||
template void list<yaSSL::ThreadError>::pop_back();
|
||||
template list<yaSSL::ThreadError>::~list();
|
||||
template pair<int, yaSSL::Message* (*)()>* GetArrayMemory<pair<int, yaSSL::Message* (*)()> >(size_t);
|
||||
template void FreeArrayMemory<pair<int, yaSSL::Message* (*)()> >(pair<int, yaSSL::Message* (*)()>*);
|
||||
template pair<int, yaSSL::HandShakeBase* (*)()>* GetArrayMemory<pair<int, yaSSL::HandShakeBase* (*)()> >(size_t);
|
||||
template void FreeArrayMemory<pair<int, yaSSL::HandShakeBase* (*)()> >(pair<int, yaSSL::HandShakeBase* (*)()>*);
|
||||
template pair<int, yaSSL::ServerKeyBase* (*)()>* GetArrayMemory<pair<int, yaSSL::ServerKeyBase* (*)()> >(size_t);
|
||||
template void FreeArrayMemory<pair<int, yaSSL::ServerKeyBase* (*)()> >(pair<int, yaSSL::ServerKeyBase* (*)()>*);
|
||||
template pair<int, yaSSL::ClientKeyBase* (*)()>* GetArrayMemory<pair<int, yaSSL::ClientKeyBase* (*)()> >(size_t);
|
||||
template void FreeArrayMemory<pair<int, yaSSL::ClientKeyBase* (*)()> >(pair<int, yaSSL::ClientKeyBase* (*)()>*);
|
||||
}
|
||||
|
||||
namespace yaSSL {
|
||||
template void ysDelete<SSL_CTX>(yaSSL::SSL_CTX*);
|
||||
template void ysDelete<SSL>(yaSSL::SSL*);
|
||||
template void ysDelete<BIGNUM>(yaSSL::BIGNUM*);
|
||||
template void ysDelete<unsigned char>(unsigned char*);
|
||||
template void ysDelete<DH>(yaSSL::DH*);
|
||||
template void ysDelete<TaoCrypt::Signer>(TaoCrypt::Signer*);
|
||||
template void ysDelete<SSL_SESSION>(yaSSL::SSL_SESSION*);
|
||||
template void ysDelete<input_buffer>(input_buffer*);
|
||||
template void ysDelete<output_buffer>(output_buffer*);
|
||||
template void ysDelete<x509>(x509*);
|
||||
template void ysDelete<Auth>(Auth*);
|
||||
template void ysDelete<HandShakeBase>(HandShakeBase*);
|
||||
template void ysDelete<ServerKeyBase>(ServerKeyBase*);
|
||||
template void ysDelete<ClientKeyBase>(ClientKeyBase*);
|
||||
template void ysDelete<SSL_METHOD>(SSL_METHOD*);
|
||||
template void ysDelete<DiffieHellman>(DiffieHellman*);
|
||||
template void ysDelete<BulkCipher>(BulkCipher*);
|
||||
template void ysDelete<Digest>(Digest*);
|
||||
template void ysDelete<X509>(X509*);
|
||||
template void ysDelete<Message>(Message*);
|
||||
template void ysDelete<sslFactory>(sslFactory*);
|
||||
template void ysDelete<Sessions>(Sessions*);
|
||||
template void ysDelete<Errors>(Errors*);
|
||||
template void ysArrayDelete<unsigned char>(unsigned char*);
|
||||
template void ysArrayDelete<char>(char*);
|
||||
|
||||
template int min<int>(int, int);
|
||||
template uint16 min<uint16>(uint16, uint16);
|
||||
template unsigned int min<unsigned int>(unsigned int, unsigned int);
|
||||
template unsigned long min<unsigned long>(unsigned long, unsigned long);
|
||||
}
|
||||
|
||||
#endif // HAVE_EXPLICIT_TEMPLATE_INSTANTIATION
|
||||
|
||||
+81
@@ -0,0 +1,81 @@
|
||||
/*
|
||||
Copyright (C) 2000-2007 MySQL AB
|
||||
|
||||
This program is free software; you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation; version 2 of the License.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program; see the file COPYING. If not, write to the
|
||||
Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
|
||||
MA 02110-1301 USA.
|
||||
*/
|
||||
|
||||
/* timer.cpp implements a high res and low res timer
|
||||
*
|
||||
*/
|
||||
|
||||
#include "runtime.hpp"
|
||||
#include "timer.hpp"
|
||||
|
||||
#ifdef _WIN32
|
||||
#define WIN32_LEAN_AND_MEAN
|
||||
#include <windows.h>
|
||||
#else
|
||||
#include <sys/time.h>
|
||||
#endif
|
||||
|
||||
namespace yaSSL {
|
||||
|
||||
#ifdef _WIN32
|
||||
|
||||
timer_d timer()
|
||||
{
|
||||
static bool init(false);
|
||||
static LARGE_INTEGER freq;
|
||||
|
||||
if (!init) {
|
||||
QueryPerformanceFrequency(&freq);
|
||||
init = true;
|
||||
}
|
||||
|
||||
LARGE_INTEGER count;
|
||||
QueryPerformanceCounter(&count);
|
||||
|
||||
return static_cast<double>(count.QuadPart) / freq.QuadPart;
|
||||
}
|
||||
|
||||
|
||||
uint lowResTimer()
|
||||
{
|
||||
return static_cast<uint>(timer());
|
||||
}
|
||||
|
||||
#else // _WIN32
|
||||
|
||||
timer_d timer()
|
||||
{
|
||||
struct timeval tv;
|
||||
gettimeofday(&tv, 0);
|
||||
|
||||
return static_cast<double>(tv.tv_sec)
|
||||
+ static_cast<double>(tv.tv_usec) / 1000000;
|
||||
}
|
||||
|
||||
|
||||
uint lowResTimer()
|
||||
{
|
||||
struct timeval tv;
|
||||
gettimeofday(&tv, 0);
|
||||
|
||||
return tv.tv_sec;
|
||||
}
|
||||
|
||||
|
||||
#endif // _WIN32
|
||||
} // namespace yaSSL
|
||||
+229
@@ -0,0 +1,229 @@
|
||||
/*
|
||||
Copyright (C) 2000-2007 MySQL AB
|
||||
|
||||
This program is free software; you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation; version 2 of the License.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program; see the file COPYING. If not, write to the
|
||||
Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
|
||||
MA 02110-1301 USA.
|
||||
*/
|
||||
|
||||
|
||||
/* yaSSL implements external API
|
||||
*/
|
||||
|
||||
#include "runtime.hpp"
|
||||
#include "yassl.hpp"
|
||||
#include "yassl_int.hpp"
|
||||
#include "handshake.hpp"
|
||||
#include <stdio.h>
|
||||
|
||||
#include "openssl/ssl.h" // get rid of this
|
||||
|
||||
|
||||
|
||||
namespace yaSSL {
|
||||
|
||||
|
||||
|
||||
struct Base {
|
||||
SSL_METHOD* method_;
|
||||
SSL_CTX* ctx_;
|
||||
SSL* ssl_;
|
||||
|
||||
char* ca_;
|
||||
char* cert_;
|
||||
char* key_;
|
||||
|
||||
DH* dh_;
|
||||
|
||||
Base() : method_(0), ctx_(0), ssl_(0), ca_(0), cert_(0), key_(0), dh_(0)
|
||||
{}
|
||||
|
||||
~Base()
|
||||
{
|
||||
if (dh_) DH_free(dh_);
|
||||
delete[] key_;
|
||||
delete[] cert_;
|
||||
delete[] ca_;
|
||||
SSL_CTX_free(ctx_); // frees method_ too
|
||||
SSL_free(ssl_);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
void SetDH(Base&);
|
||||
|
||||
void SetUpBase(Base& base, ConnectionEnd end, SOCKET_T s)
|
||||
{
|
||||
base.method_ = new SSL_METHOD(end, ProtocolVersion(3,1));
|
||||
base.ctx_ = new SSL_CTX(base.method_);
|
||||
|
||||
if (base.ca_)
|
||||
if (SSL_CTX_load_verify_locations(base.ctx_,
|
||||
base.ca_, 0) != SSL_SUCCESS) assert(0);
|
||||
if (base.cert_)
|
||||
if (SSL_CTX_use_certificate_file(base.ctx_,
|
||||
base.cert_, SSL_FILETYPE_PEM) != SSL_SUCCESS) assert(0);
|
||||
if (base.key_)
|
||||
if (SSL_CTX_use_PrivateKey_file(base.ctx_, base.key_,
|
||||
SSL_FILETYPE_PEM) != SSL_SUCCESS) assert(0);
|
||||
|
||||
if (end == server_end) SetDH(base);
|
||||
|
||||
base.ssl_ = new SSL(base.ctx_);
|
||||
base.ssl_->useSocket().set_fd(s);
|
||||
}
|
||||
|
||||
|
||||
void SetDH(Base& base)
|
||||
{
|
||||
static unsigned char dh512_p[] =
|
||||
{
|
||||
0xDA,0x58,0x3C,0x16,0xD9,0x85,0x22,0x89,0xD0,0xE4,0xAF,0x75,
|
||||
0x6F,0x4C,0xCA,0x92,0xDD,0x4B,0xE5,0x33,0xB8,0x04,0xFB,0x0F,
|
||||
0xED,0x94,0xEF,0x9C,0x8A,0x44,0x03,0xED,0x57,0x46,0x50,0xD3,
|
||||
0x69,0x99,0xDB,0x29,0xD7,0x76,0x27,0x6B,0xA2,0xD3,0xD4,0x12,
|
||||
0xE2,0x18,0xF4,0xDD,0x1E,0x08,0x4C,0xF6,0xD8,0x00,0x3E,0x7C,
|
||||
0x47,0x74,0xE8,0x33,
|
||||
};
|
||||
|
||||
static unsigned char dh512_g[] =
|
||||
{
|
||||
0x02,
|
||||
};
|
||||
|
||||
if ( (base.dh_ = DH_new()) ) {
|
||||
base.dh_->p = BN_bin2bn(dh512_p, sizeof(dh512_p), 0);
|
||||
base.dh_->g = BN_bin2bn(dh512_g, sizeof(dh512_g), 0);
|
||||
}
|
||||
if (!base.dh_->p || !base.dh_->g) {
|
||||
DH_free(base.dh_);
|
||||
base.dh_ = 0;
|
||||
}
|
||||
SSL_CTX_set_tmp_dh(base.ctx_, base.dh_);
|
||||
}
|
||||
|
||||
|
||||
void NewCopy(char*& dst, const char* src)
|
||||
{
|
||||
size_t len = strlen(src) + 1;
|
||||
dst = new char[len];
|
||||
|
||||
strncpy(dst, src, len);
|
||||
}
|
||||
|
||||
|
||||
// Client Implementation
|
||||
struct Client::ClientImpl {
|
||||
Base base_;
|
||||
};
|
||||
|
||||
|
||||
Client::Client() : pimpl_(new ClientImpl)
|
||||
{}
|
||||
|
||||
|
||||
Client::~Client() { delete pimpl_; }
|
||||
|
||||
|
||||
int Client::Connect(SOCKET_T s)
|
||||
{
|
||||
SetUpBase(pimpl_->base_, client_end, s);
|
||||
return SSL_connect(pimpl_->base_.ssl_);
|
||||
}
|
||||
|
||||
|
||||
int Client::Write(const void* buffer, int sz)
|
||||
{
|
||||
return sendData(*pimpl_->base_.ssl_, buffer, sz);
|
||||
}
|
||||
|
||||
|
||||
int Client::Read(void* buffer, int sz)
|
||||
{
|
||||
Data data(min(sz, MAX_RECORD_SIZE), static_cast<opaque*>(buffer));
|
||||
return receiveData(*pimpl_->base_.ssl_, data);
|
||||
}
|
||||
|
||||
|
||||
void Client::SetCA(const char* name)
|
||||
{
|
||||
NewCopy(pimpl_->base_.ca_, name);
|
||||
}
|
||||
|
||||
|
||||
void Client::SetCert(const char* name)
|
||||
{
|
||||
NewCopy(pimpl_->base_.cert_, name);
|
||||
}
|
||||
|
||||
|
||||
void Client::SetKey(const char* name)
|
||||
{
|
||||
NewCopy(pimpl_->base_.key_, name);
|
||||
}
|
||||
|
||||
|
||||
|
||||
// Server Implementation
|
||||
struct Server::ServerImpl {
|
||||
Base base_;
|
||||
};
|
||||
|
||||
|
||||
Server::Server() : pimpl_(new ServerImpl)
|
||||
{}
|
||||
|
||||
|
||||
Server::~Server() { delete pimpl_; }
|
||||
|
||||
|
||||
int Server::Accept(SOCKET_T s)
|
||||
{
|
||||
SetUpBase(pimpl_->base_, server_end, s);
|
||||
return SSL_accept(pimpl_->base_.ssl_);
|
||||
}
|
||||
|
||||
|
||||
int Server::Write(const void* buffer, int sz)
|
||||
{
|
||||
return sendData(*pimpl_->base_.ssl_, buffer, sz);
|
||||
}
|
||||
|
||||
|
||||
int Server::Read(void* buffer, int sz)
|
||||
{
|
||||
Data data(min(sz, MAX_RECORD_SIZE), static_cast<opaque*>(buffer));
|
||||
return receiveData(*pimpl_->base_.ssl_, data);
|
||||
}
|
||||
|
||||
|
||||
void Server::SetCA(const char* name)
|
||||
{
|
||||
NewCopy(pimpl_->base_.ca_, name);
|
||||
}
|
||||
|
||||
|
||||
void Server::SetCert(const char* name)
|
||||
{
|
||||
NewCopy(pimpl_->base_.cert_, name);
|
||||
}
|
||||
|
||||
|
||||
void Server::SetKey(const char* name)
|
||||
{
|
||||
NewCopy(pimpl_->base_.key_, name);
|
||||
}
|
||||
|
||||
|
||||
|
||||
} // namespace yaSSL
|
||||
+275
@@ -0,0 +1,275 @@
|
||||
/*
|
||||
Copyright 2000-2008 MySQL AB, 2008 Sun Microsystems, Inc.
|
||||
|
||||
This program is free software; you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation; version 2 of the License.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program; see the file COPYING. If not, write to the
|
||||
Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
|
||||
MA 02110-1301 USA.
|
||||
*/
|
||||
|
||||
|
||||
/* yaSSL error implements and an exception class
|
||||
*/
|
||||
|
||||
#include "runtime.hpp"
|
||||
#include "yassl_error.hpp"
|
||||
#include "error.hpp" // TaoCrypt error numbers
|
||||
#include "openssl/ssl.h" // SSL_ERROR_WANT_READ
|
||||
#include <string.h> // strncpy
|
||||
|
||||
#ifdef _MSC_VER
|
||||
// 4996 warning to use MS extensions e.g., strcpy_s instead of strncpy
|
||||
#pragma warning(disable: 4996)
|
||||
#endif
|
||||
|
||||
namespace yaSSL {
|
||||
|
||||
|
||||
/* may bring back in future
|
||||
Error::Error(const char* s, YasslError e, Library l)
|
||||
: mySTL::runtime_error(s), error_(e), lib_(l)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
YasslError Error::get_number() const
|
||||
{
|
||||
return error_;
|
||||
}
|
||||
|
||||
|
||||
Library Error::get_lib() const
|
||||
{
|
||||
|
||||
return lib_;
|
||||
}
|
||||
*/
|
||||
|
||||
|
||||
void SetErrorString(YasslError error, char* buffer)
|
||||
{
|
||||
using namespace TaoCrypt;
|
||||
const int max = MAX_ERROR_SZ; // shorthand
|
||||
|
||||
switch (error) {
|
||||
|
||||
// yaSSL proper errors
|
||||
case range_error :
|
||||
strncpy(buffer, "buffer index error, out of range", max);
|
||||
break;
|
||||
|
||||
case realloc_error :
|
||||
strncpy(buffer, "trying to realloc a fixed buffer", max);
|
||||
break;
|
||||
|
||||
case factory_error :
|
||||
strncpy(buffer, "unknown factory create request", max);
|
||||
break;
|
||||
|
||||
case unknown_cipher :
|
||||
strncpy(buffer, "trying to use an unknown cipher", max);
|
||||
break;
|
||||
|
||||
case prefix_error :
|
||||
strncpy(buffer, "bad master secret derivation, prefix too big", max);
|
||||
break;
|
||||
|
||||
case record_layer :
|
||||
strncpy(buffer, "record layer not ready yet", max);
|
||||
break;
|
||||
|
||||
case handshake_layer :
|
||||
strncpy(buffer, "handshake layer not ready yet", max);
|
||||
break;
|
||||
|
||||
case out_of_order :
|
||||
strncpy(buffer, "handshake message received in wrong order", max);
|
||||
break;
|
||||
|
||||
case bad_input :
|
||||
strncpy(buffer, "bad cipher suite input", max);
|
||||
break;
|
||||
|
||||
case match_error :
|
||||
strncpy(buffer, "unable to match a supported cipher suite", max);
|
||||
break;
|
||||
|
||||
case no_key_file :
|
||||
strncpy(buffer, "the server needs a private key file", max);
|
||||
break;
|
||||
|
||||
case verify_error :
|
||||
strncpy(buffer, "unable to verify peer checksum", max);
|
||||
break;
|
||||
|
||||
case send_error :
|
||||
strncpy(buffer, "socket layer send error", max);
|
||||
break;
|
||||
|
||||
case receive_error :
|
||||
strncpy(buffer, "socket layer receive error", max);
|
||||
break;
|
||||
|
||||
case certificate_error :
|
||||
strncpy(buffer, "unable to proccess cerificate", max);
|
||||
break;
|
||||
|
||||
case privateKey_error :
|
||||
strncpy(buffer, "unable to proccess private key, bad format", max);
|
||||
break;
|
||||
|
||||
case badVersion_error :
|
||||
strncpy(buffer, "protocl version mismatch", max);
|
||||
break;
|
||||
|
||||
case compress_error :
|
||||
strncpy(buffer, "compression error", max);
|
||||
break;
|
||||
|
||||
case decompress_error :
|
||||
strncpy(buffer, "decompression error", max);
|
||||
break;
|
||||
|
||||
case pms_version_error :
|
||||
strncpy(buffer, "bad PreMasterSecret version error", max);
|
||||
break;
|
||||
|
||||
// openssl errors
|
||||
case SSL_ERROR_WANT_READ :
|
||||
strncpy(buffer, "the read operation would block", max);
|
||||
break;
|
||||
|
||||
case CERTFICATE_ERROR :
|
||||
strncpy(buffer, "Unable to verify certificate", max);
|
||||
break;
|
||||
|
||||
// TaoCrypt errors
|
||||
case NO_ERROR_E :
|
||||
strncpy(buffer, "not in error state", max);
|
||||
break;
|
||||
|
||||
case WINCRYPT_E :
|
||||
strncpy(buffer, "bad wincrypt acquire", max);
|
||||
break;
|
||||
|
||||
case CRYPTGEN_E :
|
||||
strncpy(buffer, "CryptGenRandom error", max);
|
||||
break;
|
||||
|
||||
case OPEN_RAN_E :
|
||||
strncpy(buffer, "unable to use random device", max);
|
||||
break;
|
||||
|
||||
case READ_RAN_E :
|
||||
strncpy(buffer, "unable to use random device", max);
|
||||
break;
|
||||
|
||||
case INTEGER_E :
|
||||
strncpy(buffer, "ASN: bad DER Integer Header", max);
|
||||
break;
|
||||
|
||||
case SEQUENCE_E :
|
||||
strncpy(buffer, "ASN: bad Sequence Header", max);
|
||||
break;
|
||||
|
||||
case SET_E :
|
||||
strncpy(buffer, "ASN: bad Set Header", max);
|
||||
break;
|
||||
|
||||
case VERSION_E :
|
||||
strncpy(buffer, "ASN: version length not 1", max);
|
||||
break;
|
||||
|
||||
case SIG_OID_E :
|
||||
strncpy(buffer, "ASN: signature OID mismatch", max);
|
||||
break;
|
||||
|
||||
case BIT_STR_E :
|
||||
strncpy(buffer, "ASN: bad BitString Header", max);
|
||||
break;
|
||||
|
||||
case UNKNOWN_OID_E :
|
||||
strncpy(buffer, "ASN: unknown key OID type", max);
|
||||
break;
|
||||
|
||||
case OBJECT_ID_E :
|
||||
strncpy(buffer, "ASN: bad Ojbect ID Header", max);
|
||||
break;
|
||||
|
||||
case TAG_NULL_E :
|
||||
strncpy(buffer, "ASN: expected TAG NULL", max);
|
||||
break;
|
||||
|
||||
case EXPECT_0_E :
|
||||
strncpy(buffer, "ASN: expected 0", max);
|
||||
break;
|
||||
|
||||
case OCTET_STR_E :
|
||||
strncpy(buffer, "ASN: bad Octet String Header", max);
|
||||
break;
|
||||
|
||||
case TIME_E :
|
||||
strncpy(buffer, "ASN: bad TIME", max);
|
||||
break;
|
||||
|
||||
case DATE_SZ_E :
|
||||
strncpy(buffer, "ASN: bad Date Size", max);
|
||||
break;
|
||||
|
||||
case SIG_LEN_E :
|
||||
strncpy(buffer, "ASN: bad Signature Length", max);
|
||||
break;
|
||||
|
||||
case UNKOWN_SIG_E :
|
||||
strncpy(buffer, "ASN: unknown signature OID", max);
|
||||
break;
|
||||
|
||||
case UNKOWN_HASH_E :
|
||||
strncpy(buffer, "ASN: unknown hash OID", max);
|
||||
break;
|
||||
|
||||
case DSA_SZ_E :
|
||||
strncpy(buffer, "ASN: bad DSA r or s size", max);
|
||||
break;
|
||||
|
||||
case BEFORE_DATE_E :
|
||||
strncpy(buffer, "ASN: before date in the future", max);
|
||||
break;
|
||||
|
||||
case AFTER_DATE_E :
|
||||
strncpy(buffer, "ASN: after date in the past", max);
|
||||
break;
|
||||
|
||||
case SIG_CONFIRM_E :
|
||||
strncpy(buffer, "ASN: bad self signature confirmation", max);
|
||||
break;
|
||||
|
||||
case SIG_OTHER_E :
|
||||
strncpy(buffer, "ASN: bad other signature confirmation", max);
|
||||
break;
|
||||
|
||||
case CONTENT_E :
|
||||
strncpy(buffer, "bad content processing", max);
|
||||
break;
|
||||
|
||||
case PEM_E :
|
||||
strncpy(buffer, "bad PEM format processing", max);
|
||||
break;
|
||||
|
||||
default :
|
||||
strncpy(buffer, "unknown error number", max);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
} // namespace yaSSL
|
||||
+2287
File diff suppressed because it is too large
Load Diff
+2580
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,98 @@
|
||||
/*
|
||||
Copyright (C) 2000-2007 MySQL AB
|
||||
|
||||
This program is free software; you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation; version 2 of the License.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program; see the file COPYING. If not, write to the
|
||||
Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
|
||||
MA 02110-1301 USA.
|
||||
*/
|
||||
|
||||
/* aes.hpp defines AES
|
||||
*/
|
||||
|
||||
|
||||
#ifndef TAO_CRYPT_AES_HPP
|
||||
#define TAO_CRYPT_AES_HPP
|
||||
|
||||
#include "misc.hpp"
|
||||
#include "modes.hpp"
|
||||
|
||||
|
||||
#if defined(TAOCRYPT_X86ASM_AVAILABLE) && defined(TAO_ASM)
|
||||
#define DO_AES_ASM
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
namespace TaoCrypt {
|
||||
|
||||
|
||||
enum { AES_BLOCK_SIZE = 16 };
|
||||
|
||||
|
||||
// AES encryption and decryption, see FIPS-197
|
||||
class AES : public Mode_BASE {
|
||||
public:
|
||||
enum { BLOCK_SIZE = AES_BLOCK_SIZE };
|
||||
|
||||
AES(CipherDir DIR, Mode MODE)
|
||||
: Mode_BASE(BLOCK_SIZE, DIR, MODE) {}
|
||||
|
||||
#ifdef DO_AES_ASM
|
||||
void Process(byte*, const byte*, word32);
|
||||
#endif
|
||||
void SetKey(const byte* key, word32 sz, CipherDir fake = ENCRYPTION);
|
||||
void SetIV(const byte* iv) { memcpy(r_, iv, BLOCK_SIZE); }
|
||||
private:
|
||||
static const word32 rcon_[];
|
||||
|
||||
word32 rounds_;
|
||||
word32 key_[60]; // max size
|
||||
|
||||
static const word32 Te[5][256];
|
||||
static const word32 Td[5][256];
|
||||
|
||||
static const word32* Te0;
|
||||
static const word32* Te1;
|
||||
static const word32* Te2;
|
||||
static const word32* Te3;
|
||||
static const word32* Te4;
|
||||
|
||||
static const word32* Td0;
|
||||
static const word32* Td1;
|
||||
static const word32* Td2;
|
||||
static const word32* Td3;
|
||||
static const word32* Td4;
|
||||
|
||||
void encrypt(const byte*, const byte*, byte*) const;
|
||||
void AsmEncrypt(const byte*, byte*, void*) const;
|
||||
void decrypt(const byte*, const byte*, byte*) const;
|
||||
void AsmDecrypt(const byte*, byte*, void*) const;
|
||||
|
||||
void ProcessAndXorBlock(const byte*, const byte*, byte*) const;
|
||||
|
||||
AES(const AES&); // hide copy
|
||||
AES& operator=(const AES&); // and assign
|
||||
};
|
||||
|
||||
|
||||
typedef BlockCipher<ENCRYPTION, AES, ECB> AES_ECB_Encryption;
|
||||
typedef BlockCipher<DECRYPTION, AES, ECB> AES_ECB_Decryption;
|
||||
|
||||
typedef BlockCipher<ENCRYPTION, AES, CBC> AES_CBC_Encryption;
|
||||
typedef BlockCipher<DECRYPTION, AES, CBC> AES_CBC_Decryption;
|
||||
|
||||
|
||||
|
||||
} // naemspace
|
||||
|
||||
#endif // TAO_CRYPT_AES_HPP
|
||||
@@ -0,0 +1,226 @@
|
||||
/*
|
||||
Copyright (C) 2000-2007 MySQL AB
|
||||
|
||||
This program is free software; you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation; version 2 of the License.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program; see the file COPYING. If not, write to the
|
||||
Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
|
||||
MA 02110-1301 USA.
|
||||
*/
|
||||
|
||||
/* based on Wei Dai's algebra.h from CryptoPP */
|
||||
|
||||
#ifndef TAO_CRYPT_ALGEBRA_HPP
|
||||
#define TAO_CRYPT_ALGEBRA_HPP
|
||||
|
||||
#include "integer.hpp"
|
||||
|
||||
namespace TaoCrypt {
|
||||
|
||||
|
||||
// "const Element&" returned by member functions are references
|
||||
// to internal data members. Since each object may have only
|
||||
// one such data member for holding results, the following code
|
||||
// will produce incorrect results:
|
||||
// abcd = group.Add(group.Add(a,b), group.Add(c,d));
|
||||
// But this should be fine:
|
||||
// abcd = group.Add(a, group.Add(b, group.Add(c,d));
|
||||
|
||||
// Abstract Group
|
||||
class TAOCRYPT_NO_VTABLE AbstractGroup : public virtual_base
|
||||
{
|
||||
public:
|
||||
typedef Integer Element;
|
||||
|
||||
virtual ~AbstractGroup() {}
|
||||
|
||||
virtual bool Equal(const Element &a, const Element &b) const =0;
|
||||
virtual const Element& Identity() const =0;
|
||||
virtual const Element& Add(const Element &a, const Element &b) const =0;
|
||||
virtual const Element& Inverse(const Element &a) const =0;
|
||||
virtual bool InversionIsFast() const {return false;}
|
||||
|
||||
virtual const Element& Double(const Element &a) const;
|
||||
virtual const Element& Subtract(const Element &a, const Element &b) const;
|
||||
virtual Element& Accumulate(Element &a, const Element &b) const;
|
||||
virtual Element& Reduce(Element &a, const Element &b) const;
|
||||
|
||||
virtual Element ScalarMultiply(const Element &a, const Integer &e) const;
|
||||
virtual Element CascadeScalarMultiply(const Element &x, const Integer &e1,
|
||||
const Element &y, const Integer &e2) const;
|
||||
|
||||
virtual void SimultaneousMultiply(Element *results, const Element &base,
|
||||
const Integer *exponents, unsigned int exponentsCount) const;
|
||||
};
|
||||
|
||||
// Abstract Ring
|
||||
class TAOCRYPT_NO_VTABLE AbstractRing : public AbstractGroup
|
||||
{
|
||||
public:
|
||||
typedef Integer Element;
|
||||
|
||||
AbstractRing() : AbstractGroup() {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;
|
||||
virtual const Element& MultiplicativeIdentity() const =0;
|
||||
virtual const Element& Multiply(const Element&, const Element&) const =0;
|
||||
virtual const Element& MultiplicativeInverse(const Element &a) const =0;
|
||||
|
||||
virtual const Element& Square(const Element &a) const;
|
||||
virtual const Element& Divide(const Element &a, const Element &b) const;
|
||||
|
||||
virtual Element Exponentiate(const Element &a, const Integer &e) const;
|
||||
virtual Element CascadeExponentiate(const Element &x, const Integer &e1,
|
||||
const Element &y, const Integer &e2) const;
|
||||
|
||||
virtual void SimultaneousExponentiate(Element *results, const Element&,
|
||||
const Integer *exponents, unsigned int exponentsCount) const;
|
||||
|
||||
virtual const AbstractGroup& MultiplicativeGroup() const
|
||||
{return m_mg;}
|
||||
|
||||
private:
|
||||
class MultiplicativeGroupT : public AbstractGroup
|
||||
{
|
||||
public:
|
||||
const AbstractRing& GetRing() const
|
||||
{return *m_pRing;}
|
||||
|
||||
bool Equal(const Element &a, const Element &b) const
|
||||
{return GetRing().Equal(a, b);}
|
||||
|
||||
const Element& Identity() const
|
||||
{return GetRing().MultiplicativeIdentity();}
|
||||
|
||||
const Element& Add(const Element &a, const Element &b) const
|
||||
{return GetRing().Multiply(a, b);}
|
||||
|
||||
Element& Accumulate(Element &a, const Element &b) const
|
||||
{return a = GetRing().Multiply(a, b);}
|
||||
|
||||
const Element& Inverse(const Element &a) const
|
||||
{return GetRing().MultiplicativeInverse(a);}
|
||||
|
||||
const Element& Subtract(const Element &a, const Element &b) const
|
||||
{return GetRing().Divide(a, b);}
|
||||
|
||||
Element& Reduce(Element &a, const Element &b) const
|
||||
{return a = GetRing().Divide(a, b);}
|
||||
|
||||
const Element& Double(const Element &a) const
|
||||
{return GetRing().Square(a);}
|
||||
|
||||
Element ScalarMultiply(const Element &a, const Integer &e) const
|
||||
{return GetRing().Exponentiate(a, e);}
|
||||
|
||||
Element CascadeScalarMultiply(const Element &x, const Integer &e1,
|
||||
const Element &y, const Integer &e2) const
|
||||
{return GetRing().CascadeExponentiate(x, e1, y, e2);}
|
||||
|
||||
void SimultaneousMultiply(Element *results, const Element &base,
|
||||
const Integer *exponents, unsigned int exponentsCount) const
|
||||
{GetRing().SimultaneousExponentiate(results, base, exponents,
|
||||
exponentsCount);}
|
||||
|
||||
const AbstractRing* m_pRing;
|
||||
};
|
||||
|
||||
MultiplicativeGroupT m_mg;
|
||||
};
|
||||
|
||||
|
||||
// Abstract Euclidean Domain
|
||||
class TAOCRYPT_NO_VTABLE AbstractEuclideanDomain
|
||||
: public AbstractRing
|
||||
{
|
||||
public:
|
||||
typedef Integer Element;
|
||||
|
||||
virtual void DivisionAlgorithm(Element &r, Element &q, const Element &a,
|
||||
const Element &d) const =0;
|
||||
|
||||
virtual const Element& Mod(const Element &a, const Element &b) const =0;
|
||||
virtual const Element& Gcd(const Element &a, const Element &b) const;
|
||||
|
||||
protected:
|
||||
mutable Element result;
|
||||
};
|
||||
|
||||
|
||||
// EuclideanDomainOf
|
||||
class EuclideanDomainOf : public AbstractEuclideanDomain
|
||||
{
|
||||
public:
|
||||
typedef Integer Element;
|
||||
|
||||
EuclideanDomainOf() {}
|
||||
|
||||
bool Equal(const Element &a, const Element &b) const
|
||||
{return a==b;}
|
||||
|
||||
const Element& Identity() const
|
||||
{return Element::Zero();}
|
||||
|
||||
const Element& Add(const Element &a, const Element &b) const
|
||||
{return result = a+b;}
|
||||
|
||||
Element& Accumulate(Element &a, const Element &b) const
|
||||
{return a+=b;}
|
||||
|
||||
const Element& Inverse(const Element &a) const
|
||||
{return result = -a;}
|
||||
|
||||
const Element& Subtract(const Element &a, const Element &b) const
|
||||
{return result = a-b;}
|
||||
|
||||
Element& Reduce(Element &a, const Element &b) const
|
||||
{return a-=b;}
|
||||
|
||||
const Element& Double(const Element &a) const
|
||||
{return result = a.Doubled();}
|
||||
|
||||
const Element& MultiplicativeIdentity() const
|
||||
{return Element::One();}
|
||||
|
||||
const Element& Multiply(const Element &a, const Element &b) const
|
||||
{return result = a*b;}
|
||||
|
||||
const Element& Square(const Element &a) const
|
||||
{return result = a.Squared();}
|
||||
|
||||
bool IsUnit(const Element &a) const
|
||||
{return a.IsUnit();}
|
||||
|
||||
const Element& MultiplicativeInverse(const Element &a) const
|
||||
{return result = a.MultiplicativeInverse();}
|
||||
|
||||
const Element& Divide(const Element &a, const Element &b) const
|
||||
{return result = a/b;}
|
||||
|
||||
const Element& Mod(const Element &a, const Element &b) const
|
||||
{return result = a%b;}
|
||||
|
||||
void DivisionAlgorithm(Element &r, Element &q, const Element &a,
|
||||
const Element &d) const
|
||||
{Element::Divide(r, q, a, d);}
|
||||
|
||||
private:
|
||||
mutable Element result;
|
||||
};
|
||||
|
||||
|
||||
|
||||
} // namespace
|
||||
|
||||
#endif // TAO_CRYPT_ALGEBRA_HPP
|
||||
@@ -0,0 +1,58 @@
|
||||
/*
|
||||
Copyright (C) 2000-2007 MySQL AB
|
||||
|
||||
This program is free software; you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation; version 2 of the License.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program; see the file COPYING. If not, write to the
|
||||
Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
|
||||
MA 02110-1301 USA.
|
||||
*/
|
||||
|
||||
/* arc4.hpp defines ARC4
|
||||
*/
|
||||
|
||||
|
||||
#ifndef TAO_CRYPT_ARC4_HPP
|
||||
#define TAO_CRYPT_ARC4_HPP
|
||||
|
||||
#include "misc.hpp"
|
||||
|
||||
namespace TaoCrypt {
|
||||
|
||||
|
||||
// ARC4 encryption and decryption
|
||||
class ARC4 {
|
||||
public:
|
||||
enum { STATE_SIZE = 256 };
|
||||
|
||||
typedef ARC4 Encryption;
|
||||
typedef ARC4 Decryption;
|
||||
|
||||
ARC4() {}
|
||||
|
||||
void Process(byte*, const byte*, word32);
|
||||
void SetKey(const byte*, word32);
|
||||
private:
|
||||
byte x_;
|
||||
byte y_;
|
||||
byte state_[STATE_SIZE];
|
||||
|
||||
ARC4(const ARC4&); // hide copy
|
||||
const ARC4 operator=(const ARC4&); // and assign
|
||||
|
||||
void AsmProcess(byte*, const byte*, word32);
|
||||
};
|
||||
|
||||
} // namespace
|
||||
|
||||
|
||||
#endif // TAO_CRYPT_ARC4_HPP
|
||||
|
||||
@@ -0,0 +1,372 @@
|
||||
/*
|
||||
Copyright (C) 2000-2007 MySQL AB
|
||||
|
||||
This program is free software; you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation; version 2 of the License.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program; see the file COPYING. If not, write to the
|
||||
Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
|
||||
MA 02110-1301 USA.
|
||||
*/
|
||||
|
||||
/* asn.hpp provides ASN1 BER, PublicKey, and x509v3 decoding
|
||||
*/
|
||||
|
||||
|
||||
#ifndef TAO_CRYPT_ASN_HPP
|
||||
#define TAO_CRYPT_ASN_HPP
|
||||
|
||||
|
||||
#include "misc.hpp"
|
||||
#include "block.hpp"
|
||||
#include "error.hpp"
|
||||
#ifdef USE_SYS_STL
|
||||
#include <list>
|
||||
#else
|
||||
#include "list.hpp"
|
||||
#endif
|
||||
|
||||
|
||||
namespace STL = STL_NAMESPACE;
|
||||
|
||||
|
||||
namespace TaoCrypt {
|
||||
|
||||
// these tags and flags are not complete
|
||||
enum ASNTag
|
||||
{
|
||||
BOOLEAN = 0x01,
|
||||
INTEGER = 0x02,
|
||||
BIT_STRING = 0x03,
|
||||
OCTET_STRING = 0x04,
|
||||
TAG_NULL = 0x05,
|
||||
OBJECT_IDENTIFIER = 0x06,
|
||||
OBJECT_DESCRIPTOR = 0x07,
|
||||
EXTERNAL = 0x08,
|
||||
REAL = 0x09,
|
||||
ENUMERATED = 0x0a,
|
||||
UTF8_STRING = 0x0c,
|
||||
SEQUENCE = 0x10,
|
||||
SET = 0x11,
|
||||
NUMERIC_STRING = 0x12,
|
||||
PRINTABLE_STRING = 0x13,
|
||||
T61_STRING = 0x14,
|
||||
VIDEOTEXT_STRING = 0x15,
|
||||
IA5_STRING = 0x16,
|
||||
UTC_TIME = 0x17,
|
||||
GENERALIZED_TIME = 0x18,
|
||||
GRAPHIC_STRING = 0x19,
|
||||
VISIBLE_STRING = 0x1a,
|
||||
GENERAL_STRING = 0x1b,
|
||||
LONG_LENGTH = 0x80
|
||||
};
|
||||
|
||||
enum ASNIdFlag
|
||||
{
|
||||
UNIVERSAL = 0x00,
|
||||
DATA = 0x01,
|
||||
HEADER = 0x02,
|
||||
CONSTRUCTED = 0x20,
|
||||
APPLICATION = 0x40,
|
||||
CONTEXT_SPECIFIC = 0x80,
|
||||
PRIVATE = 0xc0
|
||||
};
|
||||
|
||||
|
||||
enum DNTags
|
||||
{
|
||||
COMMON_NAME = 0x03, // CN
|
||||
SUR_NAME = 0x04, // SN
|
||||
COUNTRY_NAME = 0x06, // C
|
||||
LOCALITY_NAME = 0x07, // L
|
||||
STATE_NAME = 0x08, // ST
|
||||
ORG_NAME = 0x0a, // O
|
||||
ORGUNIT_NAME = 0x0b // OU
|
||||
};
|
||||
|
||||
|
||||
enum PCKS12_Tags
|
||||
{
|
||||
/* DATA = 1, */ // from ASN1
|
||||
SIGNED_DATA = 2,
|
||||
ENVELOPED_DATA = 3,
|
||||
SIGNED_AND_ENVELOPED_DATA = 4,
|
||||
DIGESTED_DATA = 5,
|
||||
ENCRYPTED_DATA = 6
|
||||
};
|
||||
|
||||
|
||||
enum Constants
|
||||
{
|
||||
MIN_DATE_SZ = 13,
|
||||
MAX_DATE_SZ = 16,
|
||||
MAX_ALGO_SZ = 16,
|
||||
MAX_LENGTH_SZ = 5,
|
||||
MAX_SEQ_SZ = 5, // enum(seq|con) + length(4)
|
||||
MAX_ALGO_SIZE = 9,
|
||||
MAX_DIGEST_SZ = 25, // SHA + enum(Bit or Octet) + length(4)
|
||||
DSA_SIG_SZ = 40,
|
||||
ASN_NAME_MAX = 512 // max total of all included names
|
||||
};
|
||||
|
||||
|
||||
class Source;
|
||||
class RSA_PublicKey;
|
||||
class RSA_PrivateKey;
|
||||
class DSA_PublicKey;
|
||||
class DSA_PrivateKey;
|
||||
class Integer;
|
||||
class DH;
|
||||
|
||||
|
||||
// General BER decoding
|
||||
class BER_Decoder : public virtual_base {
|
||||
protected:
|
||||
Source& source_;
|
||||
public:
|
||||
explicit BER_Decoder(Source& s) : source_(s) {}
|
||||
virtual ~BER_Decoder() {}
|
||||
|
||||
Integer& GetInteger(Integer&);
|
||||
word32 GetSequence();
|
||||
word32 GetSet();
|
||||
word32 GetVersion();
|
||||
word32 GetExplicitVersion();
|
||||
|
||||
Error GetError();
|
||||
private:
|
||||
virtual void ReadHeader() = 0;
|
||||
|
||||
BER_Decoder(const BER_Decoder&); // hide copy
|
||||
BER_Decoder& operator=(const BER_Decoder&); // and assign
|
||||
};
|
||||
|
||||
|
||||
// RSA Private Key BER Decoder
|
||||
class RSA_Private_Decoder : public BER_Decoder {
|
||||
public:
|
||||
explicit RSA_Private_Decoder(Source& s) : BER_Decoder(s) {}
|
||||
void Decode(RSA_PrivateKey&);
|
||||
private:
|
||||
void ReadHeader();
|
||||
};
|
||||
|
||||
|
||||
// RSA Public Key BER Decoder
|
||||
class RSA_Public_Decoder : public BER_Decoder {
|
||||
public:
|
||||
explicit RSA_Public_Decoder(Source& s) : BER_Decoder(s) {}
|
||||
void Decode(RSA_PublicKey&);
|
||||
private:
|
||||
void ReadHeader();
|
||||
};
|
||||
|
||||
|
||||
// DSA Private Key BER Decoder
|
||||
class DSA_Private_Decoder : public BER_Decoder {
|
||||
public:
|
||||
explicit DSA_Private_Decoder(Source& s) : BER_Decoder(s) {}
|
||||
void Decode(DSA_PrivateKey&);
|
||||
private:
|
||||
void ReadHeader();
|
||||
};
|
||||
|
||||
|
||||
// DSA Public Key BER Decoder
|
||||
class DSA_Public_Decoder : public BER_Decoder {
|
||||
public:
|
||||
explicit DSA_Public_Decoder(Source& s) : BER_Decoder(s) {}
|
||||
void Decode(DSA_PublicKey&);
|
||||
private:
|
||||
void ReadHeader();
|
||||
};
|
||||
|
||||
|
||||
// DH Key BER Decoder
|
||||
class DH_Decoder : public BER_Decoder {
|
||||
public:
|
||||
explicit DH_Decoder(Source& s) : BER_Decoder(s) {}
|
||||
void Decode(DH&);
|
||||
private:
|
||||
void ReadHeader();
|
||||
};
|
||||
|
||||
|
||||
// PKCS12 BER Decoder
|
||||
class PKCS12_Decoder : public BER_Decoder {
|
||||
public:
|
||||
explicit PKCS12_Decoder(Source& s) : BER_Decoder(s) {}
|
||||
void Decode();
|
||||
private:
|
||||
void ReadHeader();
|
||||
};
|
||||
|
||||
|
||||
// General PublicKey
|
||||
class PublicKey {
|
||||
byte* key_;
|
||||
word32 sz_;
|
||||
public:
|
||||
explicit PublicKey(const byte* k = 0, word32 s = 0);
|
||||
~PublicKey() { tcArrayDelete(key_); }
|
||||
|
||||
const byte* GetKey() const { return key_; }
|
||||
word32 size() const { return sz_; }
|
||||
|
||||
void SetKey(const byte*);
|
||||
void SetSize(word32 s);
|
||||
|
||||
void AddToEnd(const byte*, word32);
|
||||
private:
|
||||
PublicKey(const PublicKey&); // hide copy
|
||||
PublicKey& operator=(const PublicKey&); // and assign
|
||||
};
|
||||
|
||||
|
||||
enum { SHA_SIZE = 20 };
|
||||
|
||||
|
||||
// A Signing Authority
|
||||
class Signer {
|
||||
PublicKey key_;
|
||||
char name_[ASN_NAME_MAX];
|
||||
byte hash_[SHA_SIZE];
|
||||
public:
|
||||
Signer(const byte* k, word32 kSz, const char* n, const byte* h);
|
||||
~Signer();
|
||||
|
||||
const PublicKey& GetPublicKey() const { return key_; }
|
||||
const char* GetName() const { return name_; }
|
||||
const byte* GetHash() const { return hash_; }
|
||||
|
||||
private:
|
||||
Signer(const Signer&); // hide copy
|
||||
Signer& operator=(const Signer&); // and assign
|
||||
};
|
||||
|
||||
|
||||
typedef STL::list<Signer*> SignerList;
|
||||
|
||||
|
||||
enum ContentType { HUH = 651 };
|
||||
enum SigType { SHAwDSA = 517, MD2wRSA = 646, MD5wRSA = 648, SHAwRSA =649};
|
||||
enum HashType { MD2h = 646, MD5h = 649, SHAh = 88 };
|
||||
enum KeyType { DSAk = 515, RSAk = 645 }; // sums of algo OID
|
||||
|
||||
|
||||
// an x509v Certificate BER Decoder
|
||||
class CertDecoder : public BER_Decoder {
|
||||
public:
|
||||
enum DateType { BEFORE, AFTER };
|
||||
enum NameType { ISSUER, SUBJECT };
|
||||
enum CertType { CA, USER };
|
||||
|
||||
explicit CertDecoder(Source&, bool decode = true, SignerList* sl = 0,
|
||||
bool noVerify = false, CertType ct = USER);
|
||||
~CertDecoder();
|
||||
|
||||
const PublicKey& GetPublicKey() const { return key_; }
|
||||
KeyType GetKeyType() const { return KeyType(keyOID_); }
|
||||
const char* GetIssuer() const { return issuer_; }
|
||||
const char* GetCommonName() const { return subject_; }
|
||||
const byte* GetHash() const { return subjectHash_; }
|
||||
const char* GetBeforeDate() const { return beforeDate_; }
|
||||
const char* GetAfterDate() const { return afterDate_; }
|
||||
|
||||
void DecodeToKey();
|
||||
private:
|
||||
PublicKey key_;
|
||||
word32 certBegin_; // offset to start of cert
|
||||
word32 sigIndex_; // offset to start of signature
|
||||
word32 sigLength_; // length of signature
|
||||
word32 signatureOID_; // sum of algorithm object id
|
||||
word32 keyOID_; // sum of key algo object id
|
||||
byte subjectHash_[SHA_SIZE]; // hash of all Names
|
||||
byte issuerHash_[SHA_SIZE]; // hash of all Names
|
||||
byte* signature_;
|
||||
char issuer_[ASN_NAME_MAX]; // Names
|
||||
char subject_[ASN_NAME_MAX]; // Names
|
||||
char beforeDate_[MAX_DATE_SZ]; // valid before date
|
||||
char afterDate_[MAX_DATE_SZ]; // valid after date
|
||||
bool verify_; // Default to yes, but could be off
|
||||
|
||||
void ReadHeader();
|
||||
void Decode(SignerList*, CertType);
|
||||
void StoreKey();
|
||||
void AddDSA();
|
||||
bool ValidateSelfSignature();
|
||||
bool ValidateSignature(SignerList*);
|
||||
bool ConfirmSignature(Source&);
|
||||
void GetKey();
|
||||
void GetName(NameType);
|
||||
void GetValidity();
|
||||
void GetDate(DateType);
|
||||
void GetCompareHash(const byte*, word32, byte*, word32);
|
||||
word32 GetAlgoId();
|
||||
word32 GetSignature();
|
||||
word32 GetDigest();
|
||||
};
|
||||
|
||||
|
||||
word32 GetLength(Source&);
|
||||
|
||||
word32 SetLength(word32, byte*);
|
||||
word32 SetSequence(word32, byte*);
|
||||
|
||||
word32 EncodeDSA_Signature(const byte* signature, byte* output);
|
||||
word32 EncodeDSA_Signature(const Integer& r, const Integer& s, byte* output);
|
||||
word32 DecodeDSA_Signature(byte* decoded, const byte* encoded, word32 sz);
|
||||
|
||||
|
||||
// General DER encoding
|
||||
class DER_Encoder : public virtual_base {
|
||||
public:
|
||||
DER_Encoder() {}
|
||||
virtual ~DER_Encoder() {}
|
||||
|
||||
word32 SetAlgoID(HashType, byte*);
|
||||
|
||||
Error GetError() const { return error_; }
|
||||
private:
|
||||
//virtual void WriteHeader() = 0;
|
||||
Error error_;
|
||||
|
||||
DER_Encoder(const DER_Encoder&); // hide copy
|
||||
DER_Encoder& operator=(const DER_Encoder&); // and assign
|
||||
};
|
||||
|
||||
|
||||
|
||||
class Signature_Encoder : public DER_Encoder {
|
||||
const byte* digest_;
|
||||
word32 digestSz_;
|
||||
SigType digestOID_;
|
||||
public:
|
||||
explicit Signature_Encoder(const byte*, word32, HashType, Source&);
|
||||
|
||||
private:
|
||||
void WriteHeader();
|
||||
word32 SetDigest(const byte*, word32, byte*);
|
||||
|
||||
Signature_Encoder(const Signature_Encoder&); // hide copy
|
||||
Signature_Encoder& operator=(const Signature_Encoder&); // and assign
|
||||
};
|
||||
|
||||
|
||||
// Get Cert in PEM format from BEGIN to END
|
||||
int GetCert(Source&);
|
||||
|
||||
// Get Cert in PEM format from pkcs12 file
|
||||
int GetPKCS_Cert(const char* password, Source&);
|
||||
|
||||
} // namespace
|
||||
|
||||
|
||||
#endif // TAO_CRYPT_ASN_HPP
|
||||
@@ -0,0 +1,206 @@
|
||||
/*
|
||||
Copyright (C) 2000-2007 MySQL AB
|
||||
|
||||
This program is free software; you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation; version 2 of the License.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program; see the file COPYING. If not, write to the
|
||||
Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
|
||||
MA 02110-1301 USA.
|
||||
*/
|
||||
|
||||
|
||||
/* block.hpp provides word and byte blocks with configurable allocators
|
||||
*/
|
||||
|
||||
|
||||
#ifndef TAO_CRYPT_BLOCK_HPP
|
||||
#define TAO_CRYPT_BLOCK_HPP
|
||||
|
||||
#include "misc.hpp"
|
||||
#include <string.h> // memcpy
|
||||
#include <stddef.h> // ptrdiff_t
|
||||
|
||||
#ifdef USE_SYS_STL
|
||||
#include <algorithm>
|
||||
#else
|
||||
#include "algorithm.hpp"
|
||||
#endif
|
||||
|
||||
|
||||
namespace STL = STL_NAMESPACE;
|
||||
|
||||
|
||||
namespace TaoCrypt {
|
||||
|
||||
|
||||
// a Base class for Allocators
|
||||
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;
|
||||
|
||||
pointer address(reference r) const {return (&r);}
|
||||
const_pointer address(const_reference r) const {return (&r); }
|
||||
void construct(pointer p, const T& val) {new (p) T(val);}
|
||||
void destroy(pointer p) {p->~T();}
|
||||
size_type max_size() const {return ~size_type(0)/sizeof(T);}
|
||||
protected:
|
||||
static void CheckSize(size_t n)
|
||||
{
|
||||
assert(n <= ~size_t(0) / sizeof(T));
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
// General purpose realloc
|
||||
template<typename T, class A>
|
||||
typename A::pointer StdReallocate(A& a, T* p, typename A::size_type oldSize,
|
||||
typename A::size_type newSize, bool preserve)
|
||||
{
|
||||
if (oldSize == newSize)
|
||||
return p;
|
||||
|
||||
if (preserve) {
|
||||
A b = A();
|
||||
typename A::pointer newPointer = b.allocate(newSize, 0);
|
||||
memcpy(newPointer, p, sizeof(T) * min(oldSize, newSize));
|
||||
a.deallocate(p, oldSize);
|
||||
STL::swap(a, b);
|
||||
return newPointer;
|
||||
}
|
||||
else {
|
||||
a.deallocate(p, oldSize);
|
||||
return a.allocate(newSize, 0);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// Allocator that zeros out memory on deletion
|
||||
template <class T>
|
||||
class AllocatorWithCleanup : public AllocatorBase<T>
|
||||
{
|
||||
public:
|
||||
typedef typename AllocatorBase<T>::pointer pointer;
|
||||
typedef typename AllocatorBase<T>::size_type size_type;
|
||||
|
||||
pointer allocate(size_type n, const void* = 0)
|
||||
{
|
||||
this->CheckSize(n);
|
||||
if (n == 0)
|
||||
return 0;
|
||||
return NEW_TC T[n];
|
||||
}
|
||||
|
||||
void deallocate(void* p, size_type n)
|
||||
{
|
||||
memset(p, 0, n * sizeof(T));
|
||||
tcArrayDelete((T*)p);
|
||||
}
|
||||
|
||||
pointer reallocate(T* p, size_type oldSize, size_type newSize,
|
||||
bool preserve)
|
||||
{
|
||||
return StdReallocate(*this, p, oldSize, newSize, preserve);
|
||||
}
|
||||
|
||||
// VS.NET STL enforces the policy of "All STL-compliant allocators have to
|
||||
// provide a template class member called rebind".
|
||||
template <class U> struct rebind { typedef AllocatorWithCleanup<U> other;};
|
||||
};
|
||||
|
||||
|
||||
// Block class template
|
||||
template<typename T, class A = AllocatorWithCleanup<T> >
|
||||
class Block {
|
||||
public:
|
||||
explicit Block(word32 s = 0) : sz_(s), buffer_(allocator_.allocate(sz_))
|
||||
{ CleanNew(sz_); }
|
||||
|
||||
Block(const T* buff, word32 s) : sz_(s), buffer_(allocator_.allocate(sz_))
|
||||
{ memcpy(buffer_, buff, sz_ * sizeof(T)); }
|
||||
|
||||
Block(const Block& that) : sz_(that.sz_), buffer_(allocator_.allocate(sz_))
|
||||
{ memcpy(buffer_, that.buffer_, sz_ * sizeof(T)); }
|
||||
|
||||
Block& operator=(const Block& that) {
|
||||
Block tmp(that);
|
||||
Swap(tmp);
|
||||
return *this;
|
||||
}
|
||||
|
||||
T& operator[] (word32 i) { assert(i < sz_); return buffer_[i]; }
|
||||
const T& operator[] (word32 i) const
|
||||
{ assert(i < sz_); return buffer_[i]; }
|
||||
|
||||
T* operator+ (word32 i) { return buffer_ + i; }
|
||||
const T* operator+ (word32 i) const { return buffer_ + i; }
|
||||
|
||||
word32 size() const { return sz_; }
|
||||
|
||||
T* get_buffer() const { return buffer_; }
|
||||
T* begin() const { return get_buffer(); }
|
||||
|
||||
void CleanGrow(word32 newSize)
|
||||
{
|
||||
if (newSize > sz_) {
|
||||
buffer_ = allocator_.reallocate(buffer_, sz_, newSize, true);
|
||||
memset(buffer_ + sz_, 0, (newSize - sz_) * sizeof(T));
|
||||
sz_ = newSize;
|
||||
}
|
||||
}
|
||||
|
||||
void CleanNew(word32 newSize)
|
||||
{
|
||||
New(newSize);
|
||||
memset(buffer_, 0, sz_ * sizeof(T));
|
||||
}
|
||||
|
||||
void New(word32 newSize)
|
||||
{
|
||||
buffer_ = allocator_.reallocate(buffer_, sz_, newSize, false);
|
||||
sz_ = newSize;
|
||||
}
|
||||
|
||||
void resize(word32 newSize)
|
||||
{
|
||||
buffer_ = allocator_.reallocate(buffer_, sz_, newSize, true);
|
||||
sz_ = newSize;
|
||||
}
|
||||
|
||||
void Swap(Block& other) {
|
||||
STL::swap(sz_, other.sz_);
|
||||
STL::swap(buffer_, other.buffer_);
|
||||
STL::swap(allocator_, other.allocator_);
|
||||
}
|
||||
|
||||
~Block() { allocator_.deallocate(buffer_, sz_); }
|
||||
private:
|
||||
word32 sz_; // size in Ts
|
||||
T* buffer_;
|
||||
A allocator_;
|
||||
};
|
||||
|
||||
|
||||
typedef Block<byte> ByteBlock;
|
||||
typedef Block<word> WordBlock;
|
||||
typedef Block<word32> Word32Block;
|
||||
|
||||
|
||||
} // namespace
|
||||
|
||||
#endif // TAO_CRYPT_BLOCK_HPP
|
||||
@@ -0,0 +1,88 @@
|
||||
/*
|
||||
Copyright (C) 2000-2007 MySQL AB
|
||||
|
||||
This program is free software; you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation; version 2 of the License.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program; see the file COPYING. If not, write to the
|
||||
Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
|
||||
MA 02110-1301 USA.
|
||||
*/
|
||||
|
||||
/* blowfish.hpp defines Blowfish
|
||||
*/
|
||||
|
||||
|
||||
#ifndef TAO_CRYPT_BLOWFISH_HPP
|
||||
#define TAO_CRYPT_BLOWFISH_HPP
|
||||
|
||||
#include "misc.hpp"
|
||||
#include "modes.hpp"
|
||||
#ifdef USE_SYS_STL
|
||||
#include <algorithm>
|
||||
#else
|
||||
#include "algorithm.hpp"
|
||||
#endif
|
||||
|
||||
|
||||
namespace STL = STL_NAMESPACE;
|
||||
|
||||
|
||||
#if defined(TAOCRYPT_X86ASM_AVAILABLE) && defined(TAO_ASM)
|
||||
#define DO_BLOWFISH_ASM
|
||||
#endif
|
||||
|
||||
|
||||
namespace TaoCrypt {
|
||||
|
||||
enum { BLOWFISH_BLOCK_SIZE = 8 };
|
||||
|
||||
|
||||
// Blowfish encryption and decryption, see
|
||||
class Blowfish : public Mode_BASE {
|
||||
public:
|
||||
enum { BLOCK_SIZE = BLOWFISH_BLOCK_SIZE, ROUNDS = 16 };
|
||||
|
||||
Blowfish(CipherDir DIR, Mode MODE)
|
||||
: Mode_BASE(BLOCK_SIZE, DIR, MODE) {}
|
||||
|
||||
#ifdef DO_BLOWFISH_ASM
|
||||
void Process(byte*, const byte*, word32);
|
||||
#endif
|
||||
void SetKey(const byte* key, word32 sz, CipherDir fake = ENCRYPTION);
|
||||
void SetIV(const byte* iv) { memcpy(r_, iv, BLOCK_SIZE); }
|
||||
private:
|
||||
static const word32 p_init_[ROUNDS + 2];
|
||||
static const word32 s_init_[4 * 256];
|
||||
|
||||
word32 pbox_[ROUNDS + 2];
|
||||
word32 sbox_[4 * 256];
|
||||
|
||||
void crypt_block(const word32 in[2], word32 out[2]) const;
|
||||
void AsmProcess(const byte* in, byte* out) const;
|
||||
void ProcessAndXorBlock(const byte*, const byte*, byte*) const;
|
||||
|
||||
Blowfish(const Blowfish&); // hide copy
|
||||
Blowfish& operator=(const Blowfish&); // and assign
|
||||
};
|
||||
|
||||
|
||||
typedef BlockCipher<ENCRYPTION, Blowfish, ECB> Blowfish_ECB_Encryption;
|
||||
typedef BlockCipher<DECRYPTION, Blowfish, ECB> Blowfish_ECB_Decryption;
|
||||
|
||||
typedef BlockCipher<ENCRYPTION, Blowfish, CBC> Blowfish_CBC_Encryption;
|
||||
typedef BlockCipher<DECRYPTION, Blowfish, CBC> Blowfish_CBC_Decryption;
|
||||
|
||||
|
||||
|
||||
} // namespace
|
||||
|
||||
#endif // TAO_CRYPT_BLOWFISH_HPP
|
||||
|
||||
@@ -0,0 +1,91 @@
|
||||
/*
|
||||
Copyright (C) 2000-2007 MySQL AB
|
||||
|
||||
This program is free software; you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation; version 2 of the License.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program; see the file COPYING. If not, write to the
|
||||
Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
|
||||
MA 02110-1301 USA.
|
||||
*/
|
||||
|
||||
/* coding.hpp defines hex and base64 encoding/decoing
|
||||
*/
|
||||
|
||||
#ifndef TAO_CRYPT_CODING_HPP
|
||||
#define TAO_CRYPT_CODING_HPP
|
||||
|
||||
#include "misc.hpp"
|
||||
#include "block.hpp"
|
||||
|
||||
namespace TaoCrypt {
|
||||
|
||||
class Source;
|
||||
|
||||
|
||||
// Hex Encoding, see RFC 3548
|
||||
class HexEncoder {
|
||||
ByteBlock encoded_;
|
||||
Source& plain_;
|
||||
public:
|
||||
explicit HexEncoder(Source& s) : plain_(s) { Encode(); }
|
||||
private:
|
||||
void Encode();
|
||||
|
||||
HexEncoder(const HexEncoder&); // hide copy
|
||||
HexEncoder& operator=(const HexEncoder&); // and assign
|
||||
};
|
||||
|
||||
|
||||
// Hex Decoding, see RFC 3548
|
||||
class HexDecoder {
|
||||
ByteBlock decoded_;
|
||||
Source& coded_;
|
||||
public:
|
||||
explicit HexDecoder(Source& s) : coded_(s) { Decode(); }
|
||||
private:
|
||||
void Decode();
|
||||
|
||||
HexDecoder(const HexDecoder&); // hide copy
|
||||
HexDecoder& operator=(const HexDecoder&); // and assign
|
||||
};
|
||||
|
||||
|
||||
// Base 64 encoding, see RFC 3548
|
||||
class Base64Encoder {
|
||||
ByteBlock encoded_;
|
||||
Source& plain_;
|
||||
public:
|
||||
explicit Base64Encoder(Source& s) : plain_(s) { Encode(); }
|
||||
private:
|
||||
void Encode();
|
||||
|
||||
Base64Encoder(const Base64Encoder&); // hide copy
|
||||
Base64Encoder& operator=(const Base64Encoder&); // and assign
|
||||
};
|
||||
|
||||
|
||||
// Base 64 decoding, see RFC 3548
|
||||
class Base64Decoder {
|
||||
ByteBlock decoded_;
|
||||
Source& coded_;
|
||||
public:
|
||||
explicit Base64Decoder(Source& s) : coded_(s) { Decode(); }
|
||||
private:
|
||||
void Decode();
|
||||
|
||||
Base64Decoder(const Base64Decoder&); // hide copy
|
||||
Base64Decoder& operator=(const Base64Decoder&); // and assign
|
||||
};
|
||||
|
||||
|
||||
} // namespace
|
||||
|
||||
#endif // TAO_CRYPT_CODING_HPP
|
||||
@@ -0,0 +1,130 @@
|
||||
/*
|
||||
Copyright (C) 2000-2007 MySQL AB
|
||||
|
||||
This program is free software; you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation; version 2 of the License.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program; see the file COPYING. If not, write to the
|
||||
Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
|
||||
MA 02110-1301 USA.
|
||||
*/
|
||||
|
||||
/* des.hpp defines DES, DES_EDE2, and DES_EDE3
|
||||
see FIPS 46-2 and FIPS 81
|
||||
*/
|
||||
|
||||
|
||||
#ifndef TAO_CRYPT_DES_HPP
|
||||
#define TAO_CRYPT_DES_HPP
|
||||
|
||||
#include "misc.hpp"
|
||||
#include "modes.hpp"
|
||||
|
||||
|
||||
#if defined(TAOCRYPT_X86ASM_AVAILABLE) && defined(TAO_ASM)
|
||||
#define DO_DES_ASM
|
||||
#endif
|
||||
|
||||
|
||||
namespace TaoCrypt {
|
||||
|
||||
|
||||
enum { DES_BLOCK_SIZE = 8, DES_KEY_SIZE = 32 };
|
||||
|
||||
|
||||
class BasicDES {
|
||||
public:
|
||||
void SetKey(const byte*, word32, CipherDir dir);
|
||||
void RawProcessBlock(word32&, word32&) const;
|
||||
protected:
|
||||
word32 k_[DES_KEY_SIZE];
|
||||
};
|
||||
|
||||
|
||||
// DES
|
||||
class DES : public Mode_BASE, public BasicDES {
|
||||
public:
|
||||
DES(CipherDir DIR, Mode MODE)
|
||||
: Mode_BASE(DES_BLOCK_SIZE, DIR, MODE) {}
|
||||
|
||||
private:
|
||||
void ProcessAndXorBlock(const byte*, const byte*, byte*) const;
|
||||
|
||||
DES(const DES&); // hide copy
|
||||
DES& operator=(const DES&); // and assign
|
||||
};
|
||||
|
||||
|
||||
// DES_EDE2
|
||||
class DES_EDE2 : public Mode_BASE {
|
||||
public:
|
||||
DES_EDE2(CipherDir DIR, Mode MODE)
|
||||
: Mode_BASE(DES_BLOCK_SIZE, DIR, MODE) {}
|
||||
|
||||
void SetKey(const byte*, word32, CipherDir dir);
|
||||
private:
|
||||
BasicDES des1_;
|
||||
BasicDES des2_;
|
||||
|
||||
void ProcessAndXorBlock(const byte*, const byte*, byte*) const;
|
||||
|
||||
DES_EDE2(const DES_EDE2&); // hide copy
|
||||
DES_EDE2& operator=(const DES_EDE2&); // and assign
|
||||
};
|
||||
|
||||
|
||||
|
||||
// DES_EDE3
|
||||
class DES_EDE3 : public Mode_BASE {
|
||||
public:
|
||||
DES_EDE3(CipherDir DIR, Mode MODE)
|
||||
: Mode_BASE(DES_BLOCK_SIZE, DIR, MODE) {}
|
||||
|
||||
void SetKey(const byte*, word32, CipherDir dir);
|
||||
void SetIV(const byte* iv) { memcpy(r_, iv, DES_BLOCK_SIZE); }
|
||||
#ifdef DO_DES_ASM
|
||||
void Process(byte*, const byte*, word32);
|
||||
#endif
|
||||
private:
|
||||
BasicDES des1_;
|
||||
BasicDES des2_;
|
||||
BasicDES des3_;
|
||||
|
||||
void AsmProcess(const byte* in, byte* out, void* box) const;
|
||||
void ProcessAndXorBlock(const byte*, const byte*, byte*) const;
|
||||
|
||||
DES_EDE3(const DES_EDE3&); // hide copy
|
||||
DES_EDE3& operator=(const DES_EDE3&); // and assign
|
||||
};
|
||||
|
||||
|
||||
typedef BlockCipher<ENCRYPTION, DES, ECB> DES_ECB_Encryption;
|
||||
typedef BlockCipher<DECRYPTION, DES, ECB> DES_ECB_Decryption;
|
||||
|
||||
typedef BlockCipher<ENCRYPTION, DES, CBC> DES_CBC_Encryption;
|
||||
typedef BlockCipher<DECRYPTION, DES, CBC> DES_CBC_Decryption;
|
||||
|
||||
typedef BlockCipher<ENCRYPTION, DES_EDE2, ECB> DES_EDE2_ECB_Encryption;
|
||||
typedef BlockCipher<DECRYPTION, DES_EDE2, ECB> DES_EDE2_ECB_Decryption;
|
||||
|
||||
typedef BlockCipher<ENCRYPTION, DES_EDE2, CBC> DES_EDE2_CBC_Encryption;
|
||||
typedef BlockCipher<DECRYPTION, DES_EDE2, CBC> DES_EDE2_CBC_Decryption;
|
||||
|
||||
typedef BlockCipher<ENCRYPTION, DES_EDE3, ECB> DES_EDE3_ECB_Encryption;
|
||||
typedef BlockCipher<DECRYPTION, DES_EDE3, ECB> DES_EDE3_ECB_Decryption;
|
||||
|
||||
typedef BlockCipher<ENCRYPTION, DES_EDE3, CBC> DES_EDE3_CBC_Encryption;
|
||||
typedef BlockCipher<DECRYPTION, DES_EDE3, CBC> DES_EDE3_CBC_Decryption;
|
||||
|
||||
|
||||
} // namespace
|
||||
|
||||
|
||||
#endif // TAO_CRYPT_DES_HPP
|
||||
@@ -0,0 +1,86 @@
|
||||
/*
|
||||
Copyright (C) 2000-2007 MySQL AB
|
||||
|
||||
This program is free software; you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation; version 2 of the License.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program; see the file COPYING. If not, write to the
|
||||
Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
|
||||
MA 02110-1301 USA.
|
||||
*/
|
||||
|
||||
/* dh.hpp provides Diffie-Hellman support
|
||||
*/
|
||||
|
||||
|
||||
#ifndef TAO_CRYPT_DH_HPP
|
||||
#define TAO_CRYPT_DH_HPP
|
||||
|
||||
#include "misc.hpp"
|
||||
#include "integer.hpp"
|
||||
|
||||
namespace TaoCrypt {
|
||||
|
||||
|
||||
class Source;
|
||||
|
||||
|
||||
// Diffie-Hellman
|
||||
class DH {
|
||||
public:
|
||||
DH() {}
|
||||
DH(Integer& p, Integer& g) : p_(p), g_(g) {}
|
||||
explicit DH(Source&);
|
||||
|
||||
DH(const DH& that) : p_(that.p_), g_(that.g_) {}
|
||||
DH& operator=(const DH& that)
|
||||
{
|
||||
DH tmp(that);
|
||||
Swap(tmp);
|
||||
return *this;
|
||||
}
|
||||
|
||||
void Swap(DH& other)
|
||||
{
|
||||
p_.Swap(other.p_);
|
||||
g_.Swap(other.g_);
|
||||
}
|
||||
|
||||
void Initialize(Source&);
|
||||
void Initialize(Integer& p, Integer& g)
|
||||
{
|
||||
SetP(p);
|
||||
SetG(g);
|
||||
}
|
||||
|
||||
void GenerateKeyPair(RandomNumberGenerator&, byte*, byte*);
|
||||
void Agree(byte*, const byte*, const byte*, word32 otherSz = 0);
|
||||
|
||||
void SetP(const Integer& p) { p_ = p; }
|
||||
void SetG(const Integer& g) { g_ = g; }
|
||||
|
||||
Integer& GetP() { return p_; }
|
||||
Integer& GetG() { return g_; }
|
||||
|
||||
// for p and agree
|
||||
word32 GetByteLength() const { return p_.ByteCount(); }
|
||||
private:
|
||||
// group parms
|
||||
Integer p_;
|
||||
Integer g_;
|
||||
|
||||
void GeneratePrivate(RandomNumberGenerator&, byte*);
|
||||
void GeneratePublic(const byte*, byte*);
|
||||
};
|
||||
|
||||
|
||||
} // namespace
|
||||
|
||||
#endif // TAO_CRYPT_DH_HPP
|
||||
@@ -0,0 +1,126 @@
|
||||
/*
|
||||
Copyright (C) 2000-2007 MySQL AB
|
||||
|
||||
This program is free software; you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation; version 2 of the License.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program; see the file COPYING. If not, write to the
|
||||
Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
|
||||
MA 02110-1301 USA.
|
||||
*/
|
||||
|
||||
/* dsa.hpp provides Digitial Signautre Algorithm see FIPS 186-2
|
||||
*/
|
||||
|
||||
#ifndef TAO_CRYPT_DSA_HPP
|
||||
#define TAO_CRYPT_DSA_HPP
|
||||
|
||||
#include "integer.hpp"
|
||||
|
||||
|
||||
namespace TaoCrypt {
|
||||
|
||||
class Source;
|
||||
|
||||
|
||||
class DSA_PublicKey {
|
||||
protected:
|
||||
Integer p_;
|
||||
Integer q_;
|
||||
Integer g_;
|
||||
Integer y_;
|
||||
public:
|
||||
DSA_PublicKey() {}
|
||||
explicit DSA_PublicKey(Source&);
|
||||
|
||||
void Initialize(Source&);
|
||||
void Initialize(const Integer& p, const Integer& q, const Integer& g,
|
||||
const Integer& y);
|
||||
|
||||
const Integer& GetModulus() const;
|
||||
const Integer& GetSubGroupOrder() const;
|
||||
const Integer& GetSubGroupGenerator() const;
|
||||
const Integer& GetPublicPart() const;
|
||||
|
||||
void SetModulus(const Integer&);
|
||||
void SetSubGroupOrder(const Integer&);
|
||||
void SetSubGroupGenerator(const Integer&);
|
||||
void SetPublicPart(const Integer&);
|
||||
|
||||
word32 SignatureLength() const;
|
||||
|
||||
DSA_PublicKey(const DSA_PublicKey&);
|
||||
DSA_PublicKey& operator=(const DSA_PublicKey&);
|
||||
|
||||
void Swap(DSA_PublicKey& other);
|
||||
};
|
||||
|
||||
|
||||
|
||||
class DSA_PrivateKey : public DSA_PublicKey {
|
||||
Integer x_;
|
||||
public:
|
||||
DSA_PrivateKey() {}
|
||||
explicit DSA_PrivateKey(Source&);
|
||||
|
||||
void Initialize(Source&);
|
||||
void Initialize(const Integer& p, const Integer& q, const Integer& g,
|
||||
const Integer& y, const Integer& x);
|
||||
|
||||
const Integer& GetPrivatePart() const;
|
||||
|
||||
void SetPrivatePart(const Integer&);
|
||||
private:
|
||||
DSA_PrivateKey(const DSA_PrivateKey&); // hide copy
|
||||
DSA_PrivateKey& operator=(const DSA_PrivateKey&); // and assign
|
||||
};
|
||||
|
||||
|
||||
|
||||
class DSA_Signer {
|
||||
const DSA_PrivateKey& key_;
|
||||
Integer r_;
|
||||
Integer s_;
|
||||
public:
|
||||
explicit DSA_Signer(const DSA_PrivateKey&);
|
||||
|
||||
word32 Sign(const byte* sha_digest, byte* sig, RandomNumberGenerator&);
|
||||
|
||||
const Integer& GetR() const;
|
||||
const Integer& GetS() const;
|
||||
private:
|
||||
DSA_Signer(const DSA_Signer&); // hide copy
|
||||
DSA_Signer& operator=(DSA_Signer&); // and assign
|
||||
};
|
||||
|
||||
|
||||
class DSA_Verifier {
|
||||
const DSA_PublicKey& key_;
|
||||
Integer r_;
|
||||
Integer s_;
|
||||
public:
|
||||
explicit DSA_Verifier(const DSA_PublicKey&);
|
||||
|
||||
bool Verify(const byte* sha_digest, const byte* sig);
|
||||
|
||||
const Integer& GetR() const;
|
||||
const Integer& GetS() const;
|
||||
private:
|
||||
DSA_Verifier(const DSA_Verifier&); // hide copy
|
||||
DSA_Verifier& operator=(const DSA_Verifier&); // and assign
|
||||
};
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
} // namespace
|
||||
|
||||
#endif // TAO_CRYPT_DSA_HPP
|
||||
@@ -0,0 +1,88 @@
|
||||
/*
|
||||
Copyright (C) 2000-2007 MySQL AB
|
||||
|
||||
This program is free software; you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation; version 2 of the License.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program; see the file COPYING. If not, write to the
|
||||
Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
|
||||
MA 02110-1301 USA.
|
||||
*/
|
||||
|
||||
/* error.hpp provides a taocrypt error numbers
|
||||
*
|
||||
*/
|
||||
|
||||
|
||||
#ifndef TAO_CRYPT_ERROR_HPP
|
||||
#define TAO_CRYPT_ERROR_HPP
|
||||
|
||||
|
||||
namespace TaoCrypt {
|
||||
|
||||
|
||||
enum ErrorNumber {
|
||||
|
||||
NO_ERROR_E = 0, // "not in error state"
|
||||
|
||||
// RandomNumberGenerator
|
||||
WINCRYPT_E = 1001, // "bad wincrypt acquire"
|
||||
CRYPTGEN_E = 1002, // "CryptGenRandom error"
|
||||
OPEN_RAN_E = 1003, // "open /dev/urandom error"
|
||||
READ_RAN_E = 1004, // "read /dev/urandom error"
|
||||
|
||||
// Integer
|
||||
INTEGER_E = 1010, // "bad DER Integer Header"
|
||||
|
||||
|
||||
// ASN.1
|
||||
SEQUENCE_E = 1020, // "bad Sequence Header"
|
||||
SET_E = 1021, // "bad Set Header"
|
||||
VERSION_E = 1022, // "version length not 1"
|
||||
SIG_OID_E = 1023, // "signature OID mismatch"
|
||||
BIT_STR_E = 1024, // "bad BitString Header"
|
||||
UNKNOWN_OID_E = 1025, // "unknown key OID type"
|
||||
OBJECT_ID_E = 1026, // "bad Ojbect ID Header"
|
||||
TAG_NULL_E = 1027, // "expected TAG NULL"
|
||||
EXPECT_0_E = 1028, // "expected 0"
|
||||
OCTET_STR_E = 1029, // "bad Octet String Header"
|
||||
TIME_E = 1030, // "bad TIME"
|
||||
|
||||
DATE_SZ_E = 1031, // "bad Date Size"
|
||||
SIG_LEN_E = 1032, // "bad Signature Length"
|
||||
UNKOWN_SIG_E = 1033, // "unknown signature OID"
|
||||
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 self signature confirmation"
|
||||
SIG_OTHER_E = 1039, // "bad other signature confirmation"
|
||||
|
||||
CONTENT_E = 1040, // "bad content processing"
|
||||
PEM_E = 1041 // "bad pem format error"
|
||||
|
||||
// add error string to yassl/src/yassl_error.cpp !!!
|
||||
};
|
||||
|
||||
|
||||
struct Error {
|
||||
ErrorNumber what_; // description number, 0 for no error
|
||||
|
||||
explicit Error(ErrorNumber w = NO_ERROR_E) : what_(w) {}
|
||||
|
||||
ErrorNumber What() const { return what_; }
|
||||
void SetError(ErrorNumber w) { what_ = w; }
|
||||
};
|
||||
|
||||
|
||||
|
||||
} // namespace TaoCrypt
|
||||
|
||||
#endif // TAO_CRYPT_ERROR_HPP
|
||||
@@ -0,0 +1,123 @@
|
||||
/*
|
||||
Copyright (C) 2000-2007 MySQL AB
|
||||
|
||||
This program is free software; you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation; version 2 of the License.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program; see the file COPYING. If not, write to the
|
||||
Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
|
||||
MA 02110-1301 USA.
|
||||
*/
|
||||
|
||||
/* file.hpp provies File Sources and Sinks
|
||||
*/
|
||||
|
||||
|
||||
#ifndef TAO_CRYPT_FILE_HPP
|
||||
#define TAO_CRYPT_FILE_HPP
|
||||
|
||||
#include "misc.hpp"
|
||||
#include "block.hpp"
|
||||
#include "error.hpp"
|
||||
#include <stdio.h>
|
||||
|
||||
namespace TaoCrypt {
|
||||
|
||||
|
||||
class Source {
|
||||
ByteBlock buffer_;
|
||||
word32 current_;
|
||||
Error error_;
|
||||
public:
|
||||
explicit Source(word32 sz = 0) : buffer_(sz), current_(0) {}
|
||||
Source(const byte* b, word32 sz) : buffer_(b, sz), current_(0) {}
|
||||
|
||||
word32 size() const { return buffer_.size(); }
|
||||
void grow(word32 sz) { buffer_.CleanGrow(sz); }
|
||||
|
||||
const byte* get_buffer() const { return buffer_.get_buffer(); }
|
||||
const byte* get_current() const { return &buffer_[current_]; }
|
||||
word32 get_index() const { return current_; }
|
||||
void set_index(word32 i) { current_ = i; }
|
||||
|
||||
byte operator[] (word32 i) { current_ = i; return next(); }
|
||||
byte next() { return buffer_[current_++]; }
|
||||
byte prev() { return buffer_[--current_]; }
|
||||
|
||||
void add(const byte* data, word32 len)
|
||||
{
|
||||
memcpy(buffer_.get_buffer() + current_, data, len);
|
||||
current_ += len;
|
||||
}
|
||||
|
||||
void advance(word32 i) { current_ += i; }
|
||||
void reset(ByteBlock&);
|
||||
|
||||
Error GetError() { return error_; }
|
||||
void SetError(ErrorNumber w) { error_.SetError(w); }
|
||||
|
||||
friend class FileSource; // for get()
|
||||
|
||||
Source(const Source& that)
|
||||
: buffer_(that.buffer_), current_(that.current_) {}
|
||||
|
||||
Source& operator=(const Source& that)
|
||||
{
|
||||
Source tmp(that);
|
||||
Swap(tmp);
|
||||
return *this;
|
||||
}
|
||||
|
||||
void Swap(Source& other)
|
||||
{
|
||||
buffer_.Swap(other.buffer_);
|
||||
STL::swap(current_, other.current_);
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
|
||||
// File Source
|
||||
class FileSource {
|
||||
FILE* file_;
|
||||
public:
|
||||
FileSource(const char* fname, Source& source);
|
||||
~FileSource();
|
||||
|
||||
word32 size(bool use_current = false);
|
||||
private:
|
||||
word32 get(Source&);
|
||||
word32 size_left();
|
||||
|
||||
FileSource(const FileSource&); // hide
|
||||
FileSource& operator=(const FileSource&); // hide
|
||||
};
|
||||
|
||||
|
||||
// File Sink
|
||||
class FileSink {
|
||||
FILE* file_;
|
||||
public:
|
||||
FileSink(const char* fname, Source& source);
|
||||
~FileSink();
|
||||
|
||||
word32 size(bool use_current = false);
|
||||
private:
|
||||
void put(Source&);
|
||||
|
||||
FileSink(const FileSink&); // hide
|
||||
FileSink& operator=(const FileSink&); // hide
|
||||
};
|
||||
|
||||
|
||||
|
||||
} // namespace
|
||||
|
||||
#endif // TAO_CRYPT_FILE_HPP
|
||||
@@ -0,0 +1,110 @@
|
||||
/*
|
||||
Copyright (C) 2000-2007 MySQL AB
|
||||
|
||||
This program is free software; you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation; version 2 of the License.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program; see the file COPYING. If not, write to the
|
||||
Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
|
||||
MA 02110-1301 USA.
|
||||
*/
|
||||
|
||||
/* hash.hpp provides a base for digest types
|
||||
*/
|
||||
|
||||
|
||||
#ifndef TAO_CRYPT_HASH_HPP
|
||||
#define TAO_CRYPT_HASH_HPP
|
||||
|
||||
#include "misc.hpp"
|
||||
|
||||
namespace TaoCrypt {
|
||||
|
||||
|
||||
// HASH
|
||||
class HASH : public virtual_base {
|
||||
public:
|
||||
virtual ~HASH() {}
|
||||
|
||||
virtual void Update(const byte*, word32) = 0;
|
||||
virtual void Final(byte*) = 0;
|
||||
|
||||
virtual void Init() = 0;
|
||||
|
||||
virtual word32 getBlockSize() const = 0;
|
||||
virtual word32 getDigestSize() const = 0;
|
||||
};
|
||||
|
||||
|
||||
// HASH with Transform
|
||||
class HASHwithTransform : public HASH {
|
||||
public:
|
||||
HASHwithTransform(word32 digSz, word32 buffSz);
|
||||
virtual ~HASHwithTransform() {}
|
||||
virtual ByteOrder getByteOrder() const = 0;
|
||||
virtual word32 getPadSize() const = 0;
|
||||
|
||||
virtual void Update(const byte*, word32);
|
||||
virtual void Final(byte*);
|
||||
|
||||
word32 GetBitCountLo() const { return loLen_ << 3; }
|
||||
word32 GetBitCountHi() const { return (loLen_ >> (8*sizeof(loLen_) - 3)) +
|
||||
(hiLen_ << 3); }
|
||||
enum { MaxDigestSz = 8, MaxBufferSz = 64 };
|
||||
protected:
|
||||
typedef word32 HashLengthType;
|
||||
word32 buffLen_; // in bytes
|
||||
HashLengthType loLen_; // length in bytes
|
||||
HashLengthType hiLen_; // length in bytes
|
||||
word32 digest_[MaxDigestSz];
|
||||
word32 buffer_[MaxBufferSz / sizeof(word32)];
|
||||
|
||||
virtual void Transform() = 0;
|
||||
|
||||
void AddLength(word32);
|
||||
};
|
||||
|
||||
|
||||
#ifdef WORD64_AVAILABLE
|
||||
|
||||
// 64-bit HASH with Transform
|
||||
class HASH64withTransform : public HASH {
|
||||
public:
|
||||
HASH64withTransform(word32 digSz, word32 buffSz);
|
||||
virtual ~HASH64withTransform() {}
|
||||
virtual ByteOrder getByteOrder() const = 0;
|
||||
virtual word32 getPadSize() const = 0;
|
||||
|
||||
virtual void Update(const byte*, word32);
|
||||
virtual void Final(byte*);
|
||||
|
||||
word32 GetBitCountLo() const { return loLen_ << 3; }
|
||||
word32 GetBitCountHi() const { return (loLen_ >> (8*sizeof(loLen_) - 3)) +
|
||||
(hiLen_ << 3); }
|
||||
enum { MaxDigestSz = 8, MaxBufferSz = 128 };
|
||||
protected:
|
||||
typedef word32 HashLengthType;
|
||||
word32 buffLen_; // in bytes
|
||||
HashLengthType loLen_; // length in bytes
|
||||
HashLengthType hiLen_; // length in bytes
|
||||
word64 digest_[MaxDigestSz];
|
||||
word64 buffer_[MaxBufferSz / sizeof(word64)];
|
||||
|
||||
virtual void Transform() = 0;
|
||||
|
||||
void AddLength(word32);
|
||||
};
|
||||
|
||||
#endif // WORD64_AVAILABLE
|
||||
|
||||
|
||||
} // namespace
|
||||
|
||||
#endif // TAO_CRYPT_HASH_HPP
|
||||
@@ -0,0 +1,138 @@
|
||||
/*
|
||||
Copyright (C) 2000-2007 MySQL AB
|
||||
|
||||
This program is free software; you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation; version 2 of the License.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program; see the file COPYING. If not, write to the
|
||||
Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
|
||||
MA 02110-1301 USA.
|
||||
*/
|
||||
|
||||
/* hamc.hpp implements HMAC, see RFC 2104
|
||||
*/
|
||||
|
||||
|
||||
#ifndef TAO_CRYPT_HMAC_HPP
|
||||
#define TAO_CRYPT_HMAC_HPP
|
||||
|
||||
#include "hash.hpp"
|
||||
|
||||
namespace TaoCrypt {
|
||||
|
||||
|
||||
// HMAC class template
|
||||
template <class T>
|
||||
class HMAC {
|
||||
public:
|
||||
enum { IPAD = 0x36, OPAD = 0x5C };
|
||||
|
||||
HMAC() : ipad_(reinterpret_cast<byte*>(&ip_)),
|
||||
opad_(reinterpret_cast<byte*>(&op_)),
|
||||
innerHash_(reinterpret_cast<byte*>(&innerH_))
|
||||
{
|
||||
Init();
|
||||
}
|
||||
void Update(const byte*, word32);
|
||||
void Final(byte*);
|
||||
void Init();
|
||||
|
||||
void SetKey(const byte*, word32);
|
||||
private:
|
||||
byte* ipad_;
|
||||
byte* opad_;
|
||||
byte* innerHash_;
|
||||
bool innerHashKeyed_;
|
||||
T mac_;
|
||||
|
||||
// MSVC 6 HACK, gives compiler error if calculated in array
|
||||
enum { HMAC_BSIZE = T::BLOCK_SIZE / sizeof(word32),
|
||||
HMAC_DSIZE = T::DIGEST_SIZE / sizeof(word32) };
|
||||
|
||||
word32 ip_[HMAC_BSIZE]; // align ipad_ on word32
|
||||
word32 op_[HMAC_BSIZE]; // align opad_ on word32
|
||||
word32 innerH_[HMAC_DSIZE]; // align innerHash_ on word32
|
||||
|
||||
void KeyInnerHash();
|
||||
|
||||
HMAC(const HMAC&);
|
||||
HMAC& operator= (const HMAC&);
|
||||
};
|
||||
|
||||
|
||||
// Setup
|
||||
template <class T>
|
||||
void HMAC<T>::Init()
|
||||
{
|
||||
mac_.Init();
|
||||
innerHashKeyed_ = false;
|
||||
}
|
||||
|
||||
|
||||
// Key generation
|
||||
template <class T>
|
||||
void HMAC<T>::SetKey(const byte* key, word32 length)
|
||||
{
|
||||
Init();
|
||||
|
||||
if (length <= T::BLOCK_SIZE)
|
||||
memcpy(ipad_, key, length);
|
||||
else {
|
||||
mac_.Update(key, length);
|
||||
mac_.Final(ipad_);
|
||||
length = T::DIGEST_SIZE;
|
||||
}
|
||||
memset(ipad_ + length, 0, T::BLOCK_SIZE - length);
|
||||
|
||||
for (word32 i = 0; i < T::BLOCK_SIZE; i++) {
|
||||
opad_[i] = ipad_[i] ^ OPAD;
|
||||
ipad_[i] ^= IPAD;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// Inner Key Hash
|
||||
template <class T>
|
||||
void HMAC<T>::KeyInnerHash()
|
||||
{
|
||||
mac_.Update(ipad_, T::BLOCK_SIZE);
|
||||
innerHashKeyed_ = true;
|
||||
}
|
||||
|
||||
|
||||
// Update
|
||||
template <class T>
|
||||
void HMAC<T>::Update(const byte* msg, word32 length)
|
||||
{
|
||||
if (!innerHashKeyed_)
|
||||
KeyInnerHash();
|
||||
mac_.Update(msg, length);
|
||||
}
|
||||
|
||||
|
||||
// Final
|
||||
template <class T>
|
||||
void HMAC<T>::Final(byte* hash)
|
||||
{
|
||||
if (!innerHashKeyed_)
|
||||
KeyInnerHash();
|
||||
mac_.Final(innerHash_);
|
||||
|
||||
mac_.Update(opad_, T::BLOCK_SIZE);
|
||||
mac_.Update(innerHash_, T::DIGEST_SIZE);
|
||||
mac_.Final(hash);
|
||||
|
||||
innerHashKeyed_ = false;
|
||||
}
|
||||
|
||||
|
||||
} // namespace
|
||||
|
||||
#endif // TAO_CRYPT_HMAC_HPP
|
||||
@@ -0,0 +1,328 @@
|
||||
/*
|
||||
Copyright (C) 2000-2007 MySQL AB
|
||||
|
||||
This program is free software; you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation; version 2 of the License.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program; see the file COPYING. If not, write to the
|
||||
Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
|
||||
MA 02110-1301 USA.
|
||||
*/
|
||||
|
||||
/* based on Wei Dai's integer.h from CryptoPP */
|
||||
|
||||
|
||||
#ifndef TAO_CRYPT_INTEGER_HPP
|
||||
#define TAO_CRYPT_INTEGER_HPP
|
||||
|
||||
|
||||
#ifdef _MSC_VER
|
||||
// 4250: dominance
|
||||
// 4660: explicitly instantiating a class already implicitly instantiated
|
||||
// 4661: no suitable definition provided for explicit template request
|
||||
// 4786: identifer was truncated in debug information
|
||||
// 4355: 'this' : used in base member initializer list
|
||||
# pragma warning(disable: 4250 4660 4661 4786 4355)
|
||||
#endif
|
||||
|
||||
|
||||
#include "misc.hpp"
|
||||
#include "block.hpp"
|
||||
#include "random.hpp"
|
||||
#include "file.hpp"
|
||||
#include <string.h>
|
||||
#ifdef USE_SYS_STL
|
||||
#include <algorithm>
|
||||
#else
|
||||
#include "algorithm.hpp"
|
||||
#endif
|
||||
|
||||
|
||||
#ifdef TAOCRYPT_X86ASM_AVAILABLE
|
||||
|
||||
#ifdef _M_IX86
|
||||
#if (defined(__INTEL_COMPILER) && (__INTEL_COMPILER >= 500)) || \
|
||||
(defined(__ICL) && (__ICL >= 500))
|
||||
#define SSE2_INTRINSICS_AVAILABLE
|
||||
#define TAOCRYPT_MM_MALLOC_AVAILABLE
|
||||
#elif defined(_MSC_VER)
|
||||
// _mm_free seems to be the only way to tell if the Processor Pack is
|
||||
//installed or not
|
||||
#include <malloc.h>
|
||||
#if defined(_mm_free)
|
||||
#define SSE2_INTRINSICS_AVAILABLE
|
||||
#define TAOCRYPT_MM_MALLOC_AVAILABLE
|
||||
#endif
|
||||
#endif
|
||||
#endif
|
||||
|
||||
// SSE2 intrinsics work in GCC 3.3 or later
|
||||
#if defined(__SSE2__) && (__GNUC__ == 4 || __GNUC_MAJOR__ > 3 || \
|
||||
__GNUC_MINOR__ > 2)
|
||||
#define SSE2_INTRINSICS_AVAILABLE
|
||||
#endif
|
||||
|
||||
#endif // X86ASM
|
||||
|
||||
|
||||
|
||||
|
||||
namespace TaoCrypt {
|
||||
|
||||
#if defined(SSE2_INTRINSICS_AVAILABLE)
|
||||
|
||||
// Allocator handling proper alignment
|
||||
template <class T>
|
||||
class AlignedAllocator : public AllocatorBase<T>
|
||||
{
|
||||
public:
|
||||
typedef typename AllocatorBase<T>::pointer pointer;
|
||||
typedef typename AllocatorBase<T>::size_type size_type;
|
||||
|
||||
pointer allocate(size_type n, const void* = 0);
|
||||
void deallocate(void* p, size_type n);
|
||||
pointer reallocate(T* p, size_type oldSize, size_type newSize,
|
||||
bool preserve)
|
||||
{
|
||||
return StdReallocate(*this, p, oldSize, newSize, preserve);
|
||||
}
|
||||
|
||||
#if !(defined(TAOCRYPT_MALLOC_ALIGNMENT_IS_16) || \
|
||||
defined(TAOCRYPT_MEMALIGN_AVAILABLE) || \
|
||||
defined(TAOCRYPT_MM_MALLOC_AVAILABLE))
|
||||
#define TAOCRYPT_NO_ALIGNED_ALLOC
|
||||
AlignedAllocator() : m_pBlock(0) {}
|
||||
protected:
|
||||
void *m_pBlock;
|
||||
#endif
|
||||
};
|
||||
|
||||
typedef Block<word, AlignedAllocator<word> > AlignedWordBlock;
|
||||
#else
|
||||
typedef WordBlock AlignedWordBlock;
|
||||
#endif
|
||||
|
||||
|
||||
// general MIN
|
||||
template<typename T> inline
|
||||
const T& min(const T& a, const T& b)
|
||||
{
|
||||
return a < b ? a : b;
|
||||
}
|
||||
|
||||
|
||||
// general MAX
|
||||
template<typename T> inline
|
||||
const T& max(const T& a, const T& b)
|
||||
{
|
||||
return a > b ? a : b;
|
||||
}
|
||||
|
||||
|
||||
// Large Integer class
|
||||
class Integer {
|
||||
public:
|
||||
enum Sign {POSITIVE = 0, NEGATIVE = 1 };
|
||||
enum Signedness { UNSIGNED, SIGNED };
|
||||
enum RandomNumberType { ANY, PRIME };
|
||||
|
||||
class DivideByZero {};
|
||||
|
||||
Integer();
|
||||
Integer(const Integer& t);
|
||||
Integer(signed long value);
|
||||
Integer(Sign s, word highWord, word lowWord);
|
||||
|
||||
// BER Decode Source
|
||||
explicit Integer(Source&);
|
||||
|
||||
Integer(const byte* encodedInteger, unsigned int byteCount,
|
||||
Signedness s = UNSIGNED);
|
||||
|
||||
~Integer() {}
|
||||
|
||||
static const Integer& Zero();
|
||||
static const Integer& One();
|
||||
|
||||
Integer& Ref() { return *this; }
|
||||
|
||||
Integer(RandomNumberGenerator& rng, const Integer& min,
|
||||
const Integer& max);
|
||||
|
||||
static Integer Power2(unsigned int e);
|
||||
|
||||
unsigned int MinEncodedSize(Signedness = UNSIGNED) const;
|
||||
unsigned int Encode(byte* output, unsigned int outputLen,
|
||||
Signedness = UNSIGNED) const;
|
||||
|
||||
void Decode(const byte* input, unsigned int inputLen,
|
||||
Signedness = UNSIGNED);
|
||||
void Decode(Source&);
|
||||
|
||||
bool IsConvertableToLong() const;
|
||||
signed long ConvertToLong() const;
|
||||
|
||||
unsigned int BitCount() const;
|
||||
unsigned int ByteCount() const;
|
||||
unsigned int WordCount() const;
|
||||
|
||||
bool GetBit(unsigned int i) const;
|
||||
byte GetByte(unsigned int i) const;
|
||||
unsigned long GetBits(unsigned int i, unsigned int n) const;
|
||||
|
||||
bool IsZero() const { return !*this; }
|
||||
bool NotZero() const { return !IsZero(); }
|
||||
bool IsNegative() const { return sign_ == NEGATIVE; }
|
||||
bool NotNegative() const { return !IsNegative(); }
|
||||
bool IsPositive() const { return NotNegative() && NotZero(); }
|
||||
bool NotPositive() const { return !IsPositive(); }
|
||||
bool IsEven() const { return GetBit(0) == 0; }
|
||||
bool IsOdd() const { return GetBit(0) == 1; }
|
||||
|
||||
Integer& operator=(const Integer& t);
|
||||
Integer& operator+=(const Integer& t);
|
||||
Integer& operator-=(const Integer& t);
|
||||
Integer& operator*=(const Integer& t) { return *this = Times(t); }
|
||||
Integer& operator/=(const Integer& t)
|
||||
{ return *this = DividedBy(t);}
|
||||
Integer& operator%=(const Integer& t) { return *this = Modulo(t); }
|
||||
Integer& operator/=(word t) { return *this = DividedBy(t); }
|
||||
Integer& operator%=(word t) { return *this = Modulo(t); }
|
||||
Integer& operator<<=(unsigned int);
|
||||
Integer& operator>>=(unsigned int);
|
||||
|
||||
|
||||
void Randomize(RandomNumberGenerator &rng, unsigned int bitcount);
|
||||
void Randomize(RandomNumberGenerator &rng, const Integer &min,
|
||||
const Integer &max);
|
||||
|
||||
void SetBit(unsigned int n, bool value = 1);
|
||||
void SetByte(unsigned int n, byte value);
|
||||
|
||||
void Negate();
|
||||
void SetPositive() { sign_ = POSITIVE; }
|
||||
void SetNegative() { if (!!(*this)) sign_ = NEGATIVE; }
|
||||
void Swap(Integer& a);
|
||||
|
||||
bool operator!() const;
|
||||
Integer operator+() const {return *this;}
|
||||
Integer operator-() const;
|
||||
Integer& operator++();
|
||||
Integer& operator--();
|
||||
Integer operator++(int)
|
||||
{ Integer temp = *this; ++*this; return temp; }
|
||||
Integer operator--(int)
|
||||
{ Integer temp = *this; --*this; return temp; }
|
||||
|
||||
int Compare(const Integer& a) const;
|
||||
|
||||
Integer Plus(const Integer &b) const;
|
||||
Integer Minus(const Integer &b) const;
|
||||
Integer Times(const Integer &b) const;
|
||||
Integer DividedBy(const Integer &b) const;
|
||||
Integer Modulo(const Integer &b) const;
|
||||
Integer DividedBy(word b) const;
|
||||
word Modulo(word b) const;
|
||||
|
||||
Integer operator>>(unsigned int n) const { return Integer(*this)>>=n; }
|
||||
Integer operator<<(unsigned int n) const { return Integer(*this)<<=n; }
|
||||
|
||||
Integer AbsoluteValue() const;
|
||||
Integer Doubled() const { return Plus(*this); }
|
||||
Integer Squared() const { return Times(*this); }
|
||||
Integer SquareRoot() const;
|
||||
|
||||
bool IsSquare() const;
|
||||
bool IsUnit() const;
|
||||
|
||||
Integer MultiplicativeInverse() const;
|
||||
|
||||
friend Integer a_times_b_mod_c(const Integer& x, const Integer& y,
|
||||
const Integer& m);
|
||||
friend Integer a_exp_b_mod_c(const Integer& x, const Integer& e,
|
||||
const Integer& m);
|
||||
|
||||
static void Divide(Integer& r, Integer& q, const Integer& a,
|
||||
const Integer& d);
|
||||
static void Divide(word& r, Integer& q, const Integer& a, word d);
|
||||
static void DivideByPowerOf2(Integer& r, Integer& q, const Integer& a,
|
||||
unsigned int n);
|
||||
static Integer Gcd(const Integer& a, const Integer& n);
|
||||
|
||||
Integer InverseMod(const Integer& n) const;
|
||||
word InverseMod(word n) const;
|
||||
|
||||
private:
|
||||
friend class ModularArithmetic;
|
||||
friend class MontgomeryRepresentation;
|
||||
|
||||
Integer(word value, unsigned int length);
|
||||
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);
|
||||
friend void PositiveMultiply(Integer& product, const Integer& a,
|
||||
const Integer& b);
|
||||
friend void PositiveDivide(Integer& remainder, Integer& quotient, const
|
||||
Integer& dividend, const Integer& divisor);
|
||||
AlignedWordBlock reg_;
|
||||
Sign sign_;
|
||||
};
|
||||
|
||||
inline bool operator==(const Integer& a, const Integer& b)
|
||||
{return a.Compare(b)==0;}
|
||||
inline bool operator!=(const Integer& a, const Integer& b)
|
||||
{return a.Compare(b)!=0;}
|
||||
inline bool operator> (const Integer& a, const Integer& b)
|
||||
{return a.Compare(b)> 0;}
|
||||
inline bool operator>=(const Integer& a, const Integer& b)
|
||||
{return a.Compare(b)>=0;}
|
||||
inline bool operator< (const Integer& a, const Integer& b)
|
||||
{return a.Compare(b)< 0;}
|
||||
inline bool operator<=(const Integer& a, const Integer& b)
|
||||
{return a.Compare(b)<=0;}
|
||||
|
||||
inline Integer operator+(const Integer &a, const Integer &b)
|
||||
{return a.Plus(b);}
|
||||
inline Integer operator-(const Integer &a, const Integer &b)
|
||||
{return a.Minus(b);}
|
||||
inline Integer operator*(const Integer &a, const Integer &b)
|
||||
{return a.Times(b);}
|
||||
inline Integer operator/(const Integer &a, const Integer &b)
|
||||
{return a.DividedBy(b);}
|
||||
inline Integer operator%(const Integer &a, const Integer &b)
|
||||
{return a.Modulo(b);}
|
||||
inline Integer operator/(const Integer &a, word b) {return a.DividedBy(b);}
|
||||
inline word operator%(const Integer &a, word b) {return a.Modulo(b);}
|
||||
|
||||
inline void swap(Integer &a, Integer &b)
|
||||
{
|
||||
a.Swap(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)
|
||||
{
|
||||
return a_exp_b_mod_c(a, e, m);
|
||||
}
|
||||
|
||||
Integer ModularRoot(const Integer& a, const Integer& dp, const Integer& dq,
|
||||
const Integer& p, const Integer& q, const Integer& u);
|
||||
|
||||
|
||||
|
||||
} // namespace
|
||||
|
||||
#endif // TAO_CRYPT_INTEGER_HPP
|
||||
@@ -0,0 +1,46 @@
|
||||
/*
|
||||
Copyright (C) 2000-2007 MySQL AB
|
||||
|
||||
This program is free software; you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation; version 2 of the License.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program; see the file COPYING. If not, write to the
|
||||
Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
|
||||
MA 02110-1301 USA.
|
||||
*/
|
||||
|
||||
/* kernelc.hpp provides support for C std lib when compiled in kernel mode
|
||||
*/
|
||||
|
||||
#ifndef TAOCRYPT_KERNELC_HPP
|
||||
#define TAOCRYPT_KERNELC_HPP
|
||||
|
||||
#include <linux/types.h> // get right size_t
|
||||
|
||||
// system functions that c++ doesn't like headers for
|
||||
|
||||
extern "C" void* memcpy(void*, const void*, size_t);
|
||||
extern "C" void* memset(void*, int, size_t);
|
||||
extern "C" void printk(char *fmt, ...);
|
||||
|
||||
#define KERN_ERR "<3>" /* error conditions */
|
||||
|
||||
#if defined(NDEBUG)
|
||||
#define assert(p) ((void)0)
|
||||
#else
|
||||
#define assert(expr) \
|
||||
if (!(expr)) { \
|
||||
printk(KERN_ERR "Assertion failed! %s,%s,%s,line=%d\n", \
|
||||
#expr,__FILE__,__FUNCTION__,__LINE__); }
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
#endif // TAOCRYPT_KERNELC_HPP
|
||||
@@ -0,0 +1,64 @@
|
||||
/*
|
||||
Copyright (C) 2000-2007 MySQL AB
|
||||
|
||||
This program is free software; you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation; version 2 of the License.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program; see the file COPYING. If not, write to the
|
||||
Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
|
||||
MA 02110-1301 USA.
|
||||
*/
|
||||
|
||||
/* md2.hpp provides MD2 digest support, see RFC 1319
|
||||
*/
|
||||
|
||||
#ifndef TAO_CRYPT_MD2_HPP
|
||||
#define TAO_CRYPT_MD2_HPP
|
||||
|
||||
|
||||
#include "hash.hpp"
|
||||
#include "block.hpp"
|
||||
|
||||
|
||||
namespace TaoCrypt {
|
||||
|
||||
|
||||
// MD2 digest
|
||||
class MD2 : public HASH {
|
||||
public:
|
||||
enum { BLOCK_SIZE = 16, DIGEST_SIZE = 16, PAD_SIZE = 16, X_SIZE = 48 };
|
||||
MD2();
|
||||
|
||||
word32 getBlockSize() const { return BLOCK_SIZE; }
|
||||
word32 getDigestSize() const { return DIGEST_SIZE; }
|
||||
|
||||
void Update(const byte*, word32);
|
||||
void Final(byte*);
|
||||
|
||||
void Init();
|
||||
void Swap(MD2&);
|
||||
private:
|
||||
ByteBlock X_, C_, buffer_;
|
||||
word32 count_; // bytes % PAD_SIZE
|
||||
|
||||
MD2(const MD2&);
|
||||
MD2& operator=(const MD2&);
|
||||
};
|
||||
|
||||
inline void swap(MD2& a, MD2& b)
|
||||
{
|
||||
a.Swap(b);
|
||||
}
|
||||
|
||||
|
||||
} // namespace
|
||||
|
||||
#endif // TAO_CRYPT_MD2_HPP
|
||||
|
||||
@@ -0,0 +1,62 @@
|
||||
/*
|
||||
Copyright (C) 2000-2007 MySQL AB
|
||||
|
||||
This program is free software; you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation; version 2 of the License.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program; see the file COPYING. If not, write to the
|
||||
Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
|
||||
MA 02110-1301 USA.
|
||||
*/
|
||||
|
||||
/* md4.hpp provides MD4 digest support
|
||||
* WANRING: MD4 is considered insecure, only use if you have to, e.g., yaSSL
|
||||
* libcurl supports needs this for NTLM authentication
|
||||
*/
|
||||
|
||||
#ifndef TAO_CRYPT_MD4_HPP
|
||||
#define TAO_CRYPT_MD4_HPP
|
||||
|
||||
#include "hash.hpp"
|
||||
|
||||
namespace TaoCrypt {
|
||||
|
||||
|
||||
// MD4 digest
|
||||
class MD4 : public HASHwithTransform {
|
||||
public:
|
||||
enum { BLOCK_SIZE = 64, DIGEST_SIZE = 16, PAD_SIZE = 56,
|
||||
TAO_BYTE_ORDER = LittleEndianOrder }; // in Bytes
|
||||
MD4() : HASHwithTransform(DIGEST_SIZE / sizeof(word32), BLOCK_SIZE)
|
||||
{ Init(); }
|
||||
ByteOrder getByteOrder() const { return ByteOrder(TAO_BYTE_ORDER); }
|
||||
word32 getBlockSize() const { return BLOCK_SIZE; }
|
||||
word32 getDigestSize() const { return DIGEST_SIZE; }
|
||||
word32 getPadSize() const { return PAD_SIZE; }
|
||||
|
||||
MD4(const MD4&);
|
||||
MD4& operator= (const MD4&);
|
||||
|
||||
void Init();
|
||||
void Swap(MD4&);
|
||||
private:
|
||||
void Transform();
|
||||
};
|
||||
|
||||
inline void swap(MD4& a, MD4& b)
|
||||
{
|
||||
a.Swap(b);
|
||||
}
|
||||
|
||||
|
||||
} // namespace
|
||||
|
||||
#endif // TAO_CRYPT_MD4_HPP
|
||||
|
||||
@@ -0,0 +1,70 @@
|
||||
/*
|
||||
Copyright (C) 2000-2007 MySQL AB
|
||||
|
||||
This program is free software; you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation; version 2 of the License.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program; see the file COPYING. If not, write to the
|
||||
Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
|
||||
MA 02110-1301 USA.
|
||||
*/
|
||||
|
||||
/* md5.hpp provides MD5 digest support, see RFC 1321
|
||||
*/
|
||||
|
||||
#ifndef TAO_CRYPT_MD5_HPP
|
||||
#define TAO_CRYPT_MD5_HPP
|
||||
|
||||
#include "hash.hpp"
|
||||
|
||||
|
||||
#if defined(TAOCRYPT_X86ASM_AVAILABLE) && defined(TAO_ASM)
|
||||
#define DO_MD5_ASM
|
||||
#endif
|
||||
|
||||
namespace TaoCrypt {
|
||||
|
||||
|
||||
// MD5 digest
|
||||
class MD5 : public HASHwithTransform {
|
||||
public:
|
||||
enum { BLOCK_SIZE = 64, DIGEST_SIZE = 16, PAD_SIZE = 56,
|
||||
TAO_BYTE_ORDER = LittleEndianOrder }; // in Bytes
|
||||
MD5() : HASHwithTransform(DIGEST_SIZE / sizeof(word32), BLOCK_SIZE)
|
||||
{ Init(); }
|
||||
ByteOrder getByteOrder() const { return ByteOrder(TAO_BYTE_ORDER); }
|
||||
word32 getBlockSize() const { return BLOCK_SIZE; }
|
||||
word32 getDigestSize() const { return DIGEST_SIZE; }
|
||||
word32 getPadSize() const { return PAD_SIZE; }
|
||||
|
||||
MD5(const MD5&);
|
||||
MD5& operator= (const MD5&);
|
||||
|
||||
#ifdef DO_MD5_ASM
|
||||
void Update(const byte*, word32);
|
||||
#endif
|
||||
|
||||
void Init();
|
||||
void Swap(MD5&);
|
||||
private:
|
||||
void Transform();
|
||||
void AsmTransform(const byte* data, word32 times);
|
||||
};
|
||||
|
||||
inline void swap(MD5& a, MD5& b)
|
||||
{
|
||||
a.Swap(b);
|
||||
}
|
||||
|
||||
|
||||
} // namespace
|
||||
|
||||
#endif // TAO_CRYPT_MD5_HPP
|
||||
|
||||
@@ -0,0 +1,886 @@
|
||||
/*
|
||||
Copyright (C) 2000-2007 MySQL AB
|
||||
|
||||
This program is free software; you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation; version 2 of the License.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program; see the file COPYING. If not, write to the
|
||||
Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
|
||||
MA 02110-1301 USA.
|
||||
*/
|
||||
|
||||
/* based on Wei Dai's misc.h from CryptoPP */
|
||||
|
||||
#ifndef TAO_CRYPT_MISC_HPP
|
||||
#define TAO_CRYPT_MISC_HPP
|
||||
|
||||
|
||||
#if !defined(DO_TAOCRYPT_KERNEL_MODE)
|
||||
#include <stdlib.h>
|
||||
#include <assert.h>
|
||||
#include <string.h>
|
||||
#else
|
||||
#include "kernelc.hpp"
|
||||
#endif
|
||||
|
||||
#include "types.hpp"
|
||||
#include "type_traits.hpp"
|
||||
|
||||
|
||||
|
||||
namespace TaoCrypt {
|
||||
|
||||
|
||||
// Delete static singleton holders
|
||||
void CleanUp();
|
||||
|
||||
|
||||
#ifdef YASSL_PURE_C
|
||||
|
||||
// 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);
|
||||
|
||||
void operator delete (void*, TaoCrypt::new_t);
|
||||
void operator delete[](void*, TaoCrypt::new_t);
|
||||
|
||||
|
||||
namespace TaoCrypt {
|
||||
|
||||
template<typename T>
|
||||
void tcDelete(T* ptr)
|
||||
{
|
||||
if (ptr) ptr->~T();
|
||||
::operator delete(ptr, TaoCrypt::tc);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
void tcArrayDelete(T* ptr)
|
||||
{
|
||||
// can't do array placement destruction since not tracking size in
|
||||
// allocation, only allow builtins to use array placement since they
|
||||
// don't need destructors called
|
||||
typedef char builtin[IsFundamentalType<T>::Yes ? 1 : -1];
|
||||
(void)sizeof(builtin);
|
||||
|
||||
::operator delete[](ptr, TaoCrypt::tc);
|
||||
}
|
||||
|
||||
#define NEW_TC new (TaoCrypt::tc)
|
||||
|
||||
|
||||
// to resolve compiler generated operator delete on base classes with
|
||||
// virtual destructors (when on stack), make sure doesn't get called
|
||||
class virtual_base {
|
||||
public:
|
||||
static void operator delete(void*) { assert(0); }
|
||||
};
|
||||
|
||||
#else // YASSL_PURE_C
|
||||
|
||||
|
||||
template<typename T>
|
||||
void tcDelete(T* ptr)
|
||||
{
|
||||
delete ptr;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
void tcArrayDelete(T* ptr)
|
||||
{
|
||||
delete[] ptr;
|
||||
}
|
||||
|
||||
#define NEW_TC new
|
||||
|
||||
class virtual_base {};
|
||||
|
||||
|
||||
#endif // YASSL_PURE_C
|
||||
|
||||
|
||||
#if defined(_MSC_VER) || defined(__BCPLUSPLUS__)
|
||||
#define INTEL_INTRINSICS
|
||||
#define FAST_ROTATE
|
||||
#elif defined(__MWERKS__) && TARGET_CPU_PPC
|
||||
#define PPC_INTRINSICS
|
||||
#define FAST_ROTATE
|
||||
#elif defined(__GNUC__) && defined(__i386__)
|
||||
// GCC does peephole optimizations which should result in using rotate
|
||||
// instructions
|
||||
#define FAST_ROTATE
|
||||
#endif
|
||||
|
||||
|
||||
// no gas on these systems ?, disable for now
|
||||
#if defined(__sun__) || defined (__QNX__) || defined (__APPLE__)
|
||||
#define TAOCRYPT_DISABLE_X86ASM
|
||||
#endif
|
||||
|
||||
// icc problem with -03 and integer, disable for now
|
||||
#if defined(__INTEL_COMPILER)
|
||||
#define TAOCRYPT_DISABLE_X86ASM
|
||||
#endif
|
||||
|
||||
|
||||
// Turn on ia32 ASM for Big Integer
|
||||
// CodeWarrior defines _MSC_VER
|
||||
#if !defined(TAOCRYPT_DISABLE_X86ASM) && ((defined(_MSC_VER) && \
|
||||
!defined(__MWERKS__) && defined(_M_IX86)) || \
|
||||
(defined(__GNUC__) && defined(__i386__)))
|
||||
#define TAOCRYPT_X86ASM_AVAILABLE
|
||||
#endif
|
||||
|
||||
|
||||
#ifdef TAOCRYPT_X86ASM_AVAILABLE
|
||||
bool HaveCpuId();
|
||||
bool IsPentium();
|
||||
void CpuId(word32 input, word32 *output);
|
||||
|
||||
extern bool isMMX;
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
|
||||
// Turn on ia32 ASM for Ciphers and Message Digests
|
||||
// Seperate define since these are more complex, use member offsets
|
||||
// and user may want to turn off while leaving Big Integer optos on
|
||||
#if defined(TAOCRYPT_X86ASM_AVAILABLE) && !defined(DISABLE_TAO_ASM)
|
||||
#define TAO_ASM
|
||||
#endif
|
||||
|
||||
|
||||
// Extra word in older vtable implementations, for ASM member offset
|
||||
#if defined(__GNUC__) && __GNUC__ < 3
|
||||
#define OLD_GCC_OFFSET
|
||||
#endif
|
||||
|
||||
|
||||
#if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__)
|
||||
# define TAOCRYPT_MALLOC_ALIGNMENT_IS_16
|
||||
#endif
|
||||
|
||||
#if defined(__linux__) || defined(__sun__) || defined(__CYGWIN__)
|
||||
# define TAOCRYPT_MEMALIGN_AVAILABLE
|
||||
#endif
|
||||
|
||||
|
||||
#if defined(_WIN32) || defined(__CYGWIN__)
|
||||
#define TAOCRYPT_WIN32_AVAILABLE
|
||||
#endif
|
||||
|
||||
#if defined(__unix__) || defined(__MACH__)
|
||||
#define TAOCRYPT_UNIX_AVAILABLE
|
||||
#endif
|
||||
|
||||
|
||||
// VC60 workaround: it doesn't allow typename in some places
|
||||
#if defined(_MSC_VER) && (_MSC_VER < 1300)
|
||||
#define CPP_TYPENAME
|
||||
#else
|
||||
#define CPP_TYPENAME typename
|
||||
#endif
|
||||
|
||||
|
||||
#ifdef _MSC_VER
|
||||
#define TAOCRYPT_NO_VTABLE __declspec(novtable)
|
||||
#else
|
||||
#define TAOCRYPT_NO_VTABLE
|
||||
#endif
|
||||
|
||||
|
||||
#ifdef USE_SYS_STL
|
||||
// use system STL
|
||||
#define STL_NAMESPACE std
|
||||
#else
|
||||
// use mySTL
|
||||
#define STL_NAMESPACE mySTL
|
||||
#endif
|
||||
|
||||
|
||||
// ***************** DLL related ********************
|
||||
|
||||
#ifdef TAOCRYPT_WIN32_AVAILABLE
|
||||
|
||||
#ifdef TAOCRYPT_EXPORTS
|
||||
#define TAOCRYPT_IS_DLL
|
||||
#define TAOCRYPT_DLL __declspec(dllexport)
|
||||
#elif defined(TAOCRYPT_IMPORTS)
|
||||
#define TAOCRYPT_IS_DLL
|
||||
#define TAOCRYPT_DLL __declspec(dllimport)
|
||||
#else
|
||||
#define TAOCRYPT_DLL
|
||||
#endif // EXPORTS
|
||||
|
||||
#define TAOCRYPT_API __stdcall
|
||||
#define TAOCRYPT_CDECL __cdecl
|
||||
|
||||
#else // TAOCRYPT_WIN32_AVAILABLE
|
||||
|
||||
#define TAOCRYPT_DLL
|
||||
#define TAOCRYPT_API
|
||||
#define TAOCRYPT_CDECL
|
||||
|
||||
#endif // TAOCRYPT_WIN32_AVAILABLE
|
||||
|
||||
|
||||
// ****************** tempalte stuff *******************
|
||||
|
||||
|
||||
#if defined(TAOCRYPT_MANUALLY_INSTANTIATE_TEMPLATES) && \
|
||||
!defined(TAOCRYPT_IMPORTS)
|
||||
#define TAOCRYPT_DLL_TEMPLATE_CLASS template class TAOCRYPT_DLL
|
||||
#elif defined(__MWERKS__)
|
||||
#define TAOCRYPT_DLL_TEMPLATE_CLASS extern class TAOCRYPT_DLL
|
||||
#else
|
||||
#define TAOCRYPT_DLL_TEMPLATE_CLASS extern template class TAOCRYPT_DLL
|
||||
#endif
|
||||
|
||||
|
||||
#if defined(TAOCRYPT_MANUALLY_INSTANTIATE_TEMPLATES) && \
|
||||
!defined(TAOCRYPT_EXPORTS)
|
||||
#define TAOCRYPT_STATIC_TEMPLATE_CLASS template class
|
||||
#elif defined(__MWERKS__)
|
||||
#define TAOCRYPT_STATIC_TEMPLATE_CLASS extern class
|
||||
#else
|
||||
#define TAOCRYPT_STATIC_TEMPLATE_CLASS extern template class
|
||||
#endif
|
||||
|
||||
|
||||
// ************** compile-time assertion ***************
|
||||
|
||||
template <bool b>
|
||||
struct CompileAssert
|
||||
{
|
||||
static char dummy[2*b-1];
|
||||
};
|
||||
|
||||
#define TAOCRYPT_COMPILE_ASSERT(assertion) \
|
||||
TAOCRYPT_COMPILE_ASSERT_INSTANCE(assertion, __LINE__)
|
||||
|
||||
#if defined(TAOCRYPT_EXPORTS) || defined(TAOCRYPT_IMPORTS)
|
||||
#define TAOCRYPT_COMPILE_ASSERT_INSTANCE(assertion, instance)
|
||||
#else
|
||||
#define TAOCRYPT_COMPILE_ASSERT_INSTANCE(assertion, instance) \
|
||||
(void)sizeof(CompileAssert<(assertion)>)
|
||||
#endif
|
||||
|
||||
#define TAOCRYPT_ASSERT_JOIN(X, Y) TAOCRYPT_DO_ASSERT_JOIN(X, Y)
|
||||
|
||||
#define TAOCRYPT_DO_ASSERT_JOIN(X, Y) X##Y
|
||||
|
||||
|
||||
/*************** helpers *****************************/
|
||||
|
||||
inline unsigned int BitsToBytes(unsigned int bitCount)
|
||||
{
|
||||
return ((bitCount+7)/(8));
|
||||
}
|
||||
|
||||
inline unsigned int BytesToWords(unsigned int byteCount)
|
||||
{
|
||||
return ((byteCount+WORD_SIZE-1)/WORD_SIZE);
|
||||
}
|
||||
|
||||
inline unsigned int BitsToWords(unsigned int bitCount)
|
||||
{
|
||||
return ((bitCount+WORD_BITS-1)/(WORD_BITS));
|
||||
}
|
||||
|
||||
inline void CopyWords(word* r, const word* a, word32 n)
|
||||
{
|
||||
for (word32 i = 0; i < n; i++)
|
||||
r[i] = a[i];
|
||||
}
|
||||
|
||||
inline unsigned int CountWords(const word* X, unsigned int N)
|
||||
{
|
||||
while (N && X[N-1]==0)
|
||||
N--;
|
||||
return N;
|
||||
}
|
||||
|
||||
inline void SetWords(word* r, word a, unsigned int n)
|
||||
{
|
||||
for (unsigned int i=0; i<n; i++)
|
||||
r[i] = a;
|
||||
}
|
||||
|
||||
enum ByteOrder { LittleEndianOrder = 0, BigEndianOrder = 1 };
|
||||
enum CipherDir {ENCRYPTION, DECRYPTION};
|
||||
|
||||
inline CipherDir ReverseDir(CipherDir dir)
|
||||
{
|
||||
return (dir == ENCRYPTION) ? DECRYPTION : ENCRYPTION;
|
||||
}
|
||||
|
||||
template <typename ENUM_TYPE, int VALUE>
|
||||
struct EnumToType
|
||||
{
|
||||
static ENUM_TYPE ToEnum() { return (ENUM_TYPE)VALUE; }
|
||||
};
|
||||
|
||||
typedef EnumToType<ByteOrder, LittleEndianOrder> LittleEndian;
|
||||
typedef EnumToType<ByteOrder, BigEndianOrder> BigEndian;
|
||||
|
||||
|
||||
#ifndef BIG_ENDIAN_ORDER
|
||||
typedef LittleEndian HostByteOrder;
|
||||
#else
|
||||
typedef BigEndian HostByteOrder;
|
||||
#endif
|
||||
|
||||
inline ByteOrder GetHostByteOrder()
|
||||
{
|
||||
return HostByteOrder::ToEnum();
|
||||
}
|
||||
|
||||
inline bool HostByteOrderIs(ByteOrder order)
|
||||
{
|
||||
return order == GetHostByteOrder();
|
||||
}
|
||||
|
||||
|
||||
void xorbuf(byte*, const byte*, unsigned int);
|
||||
|
||||
|
||||
template <class T>
|
||||
inline bool IsPowerOf2(T n)
|
||||
{
|
||||
return n > 0 && (n & (n-1)) == 0;
|
||||
}
|
||||
|
||||
template <class T1, class T2>
|
||||
inline T2 ModPowerOf2(T1 a, T2 b)
|
||||
{
|
||||
assert(IsPowerOf2(b));
|
||||
return T2(a) & (b-1);
|
||||
}
|
||||
|
||||
template <class T>
|
||||
inline T RoundDownToMultipleOf(T n, T m)
|
||||
{
|
||||
return n - (IsPowerOf2(m) ? ModPowerOf2(n, m) : (n%m));
|
||||
}
|
||||
|
||||
template <class T>
|
||||
inline T RoundUpToMultipleOf(T n, T m)
|
||||
{
|
||||
return RoundDownToMultipleOf(n+m-1, m);
|
||||
}
|
||||
|
||||
template <class T>
|
||||
inline unsigned int GetAlignment(T* dummy = 0) // VC60 workaround
|
||||
{
|
||||
#if (_MSC_VER >= 1300)
|
||||
return __alignof(T);
|
||||
#elif defined(__GNUC__)
|
||||
return __alignof__(T);
|
||||
#else
|
||||
return sizeof(T);
|
||||
#endif
|
||||
}
|
||||
|
||||
inline bool IsAlignedOn(const void* p, unsigned int alignment)
|
||||
{
|
||||
return IsPowerOf2(alignment) ? ModPowerOf2((size_t)p, alignment) == 0
|
||||
: (size_t)p % alignment == 0;
|
||||
}
|
||||
|
||||
template <class T>
|
||||
inline bool IsAligned(const void* p, T* dummy = 0) // VC60 workaround
|
||||
{
|
||||
return IsAlignedOn(p, GetAlignment<T>());
|
||||
}
|
||||
|
||||
|
||||
template <class T> inline T rotlFixed(T x, unsigned int y)
|
||||
{
|
||||
assert(y < sizeof(T)*8);
|
||||
return (x<<y) | (x>>(sizeof(T)*8-y));
|
||||
}
|
||||
|
||||
template <class T> inline T rotrFixed(T x, unsigned int y)
|
||||
{
|
||||
assert(y < sizeof(T)*8);
|
||||
return (x>>y) | (x<<(sizeof(T)*8-y));
|
||||
}
|
||||
|
||||
#ifdef INTEL_INTRINSICS
|
||||
|
||||
#pragma intrinsic(_lrotl, _lrotr)
|
||||
|
||||
template<> inline word32 rotlFixed(word32 x, word32 y)
|
||||
{
|
||||
assert(y < 32);
|
||||
return y ? _lrotl(x, y) : x;
|
||||
}
|
||||
|
||||
template<> inline word32 rotrFixed(word32 x, word32 y)
|
||||
{
|
||||
assert(y < 32);
|
||||
return y ? _lrotr(x, y) : x;
|
||||
}
|
||||
|
||||
#endif // INTEL_INTRINSICS
|
||||
|
||||
#ifndef min
|
||||
inline word32 min(word32 a, word32 b)
|
||||
{
|
||||
return a < b ? a : b;
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
inline word32 ByteReverse(word32 value)
|
||||
{
|
||||
#ifdef PPC_INTRINSICS
|
||||
// PPC: load reverse indexed instruction
|
||||
return (word32)__lwbrx(&value,0);
|
||||
#elif defined(FAST_ROTATE)
|
||||
// 5 instructions with rotate instruction, 9 without
|
||||
return (rotrFixed(value, 8U) & 0xff00ff00) |
|
||||
(rotlFixed(value, 8U) & 0x00ff00ff);
|
||||
#else
|
||||
// 6 instructions with rotate instruction, 8 without
|
||||
value = ((value & 0xFF00FF00) >> 8) | ((value & 0x00FF00FF) << 8);
|
||||
return rotlFixed(value, 16U);
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
#ifdef WORD64_AVAILABLE
|
||||
|
||||
inline word64 ByteReverse(word64 value)
|
||||
{
|
||||
#ifdef TAOCRYPT_SLOW_WORD64
|
||||
return (word64(ByteReverse(word32(value))) << 32) |
|
||||
ByteReverse(word32(value>>32));
|
||||
#else
|
||||
value = ((value & W64LIT(0xFF00FF00FF00FF00)) >> 8) |
|
||||
((value & W64LIT(0x00FF00FF00FF00FF)) << 8);
|
||||
value = ((value & W64LIT(0xFFFF0000FFFF0000)) >> 16) |
|
||||
((value & W64LIT(0x0000FFFF0000FFFF)) << 16);
|
||||
return rotlFixed(value, 32U);
|
||||
#endif
|
||||
}
|
||||
|
||||
#endif // WORD64_AVAILABLE
|
||||
|
||||
|
||||
template <typename T>
|
||||
inline void ByteReverse(T* out, const T* in, word32 byteCount)
|
||||
{
|
||||
assert(byteCount % sizeof(T) == 0);
|
||||
word32 count = byteCount/sizeof(T);
|
||||
for (word32 i=0; i<count; i++)
|
||||
out[i] = ByteReverse(in[i]);
|
||||
}
|
||||
|
||||
inline void ByteReverse(byte* out, const byte* in, word32 byteCount)
|
||||
{
|
||||
word32* o = reinterpret_cast<word32*>(out);
|
||||
const word32* i = reinterpret_cast<const word32*>(in);
|
||||
ByteReverse(o, i, byteCount);
|
||||
}
|
||||
|
||||
|
||||
template <class T>
|
||||
inline T ByteReverseIf(T value, ByteOrder order)
|
||||
{
|
||||
return HostByteOrderIs(order) ? value : ByteReverse(value);
|
||||
}
|
||||
|
||||
|
||||
template <typename T>
|
||||
inline void ByteReverseIf(T* out, const T* in, word32 bc, ByteOrder order)
|
||||
{
|
||||
if (!HostByteOrderIs(order))
|
||||
ByteReverse(out, in, bc);
|
||||
else if (out != in)
|
||||
memcpy(out, in, bc);
|
||||
}
|
||||
|
||||
|
||||
|
||||
// do Asm Reverse is host is Little and x86asm
|
||||
#ifdef LITTLE_ENDIAN_ORDER
|
||||
#ifdef TAOCRYPT_X86ASM_AVAILABLE
|
||||
#define LittleReverse AsmReverse
|
||||
#else
|
||||
#define LittleReverse ByteReverse
|
||||
#endif
|
||||
#else
|
||||
#define LittleReverse
|
||||
#endif
|
||||
|
||||
|
||||
// do Asm Reverse is host is Big and x86asm
|
||||
#ifdef BIG_ENDIAN_ORDER
|
||||
#ifdef TAOCRYPT_X86ASM_AVAILABLE
|
||||
#define BigReverse AsmReverse
|
||||
#else
|
||||
#define BigReverse ByteReverse
|
||||
#endif
|
||||
#else
|
||||
#define BigReverse
|
||||
#endif
|
||||
|
||||
|
||||
#ifdef TAOCRYPT_X86ASM_AVAILABLE
|
||||
|
||||
// faster than rotate, use bswap
|
||||
|
||||
inline word32 AsmReverse(word32 wd)
|
||||
{
|
||||
#ifdef __GNUC__
|
||||
__asm__
|
||||
(
|
||||
"bswap %1"
|
||||
: "=r"(wd)
|
||||
: "0"(wd)
|
||||
);
|
||||
#else
|
||||
__asm
|
||||
{
|
||||
mov eax, wd
|
||||
bswap eax
|
||||
mov wd, eax
|
||||
}
|
||||
#endif
|
||||
return wd;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
template <class T>
|
||||
inline void GetUserKey(ByteOrder order, T* out, word32 outlen, const byte* in,
|
||||
word32 inlen)
|
||||
{
|
||||
const unsigned int U = sizeof(T);
|
||||
assert(inlen <= outlen*U);
|
||||
memcpy(out, in, inlen);
|
||||
memset((byte *)out+inlen, 0, outlen*U-inlen);
|
||||
ByteReverseIf(out, out, RoundUpToMultipleOf(inlen, U), order);
|
||||
}
|
||||
|
||||
|
||||
#ifdef _MSC_VER
|
||||
// disable conversion warning
|
||||
#pragma warning(disable:4244)
|
||||
#endif
|
||||
|
||||
|
||||
inline byte UnalignedGetWordNonTemplate(ByteOrder order, const byte *block,
|
||||
byte*)
|
||||
{
|
||||
return block[0];
|
||||
}
|
||||
|
||||
inline word16 UnalignedGetWordNonTemplate(ByteOrder order, const byte* block,
|
||||
word16*)
|
||||
{
|
||||
return (order == BigEndianOrder)
|
||||
? block[1] | (block[0] << 8)
|
||||
: block[0] | (block[1] << 8);
|
||||
}
|
||||
|
||||
inline word32 UnalignedGetWordNonTemplate(ByteOrder order, const byte* block,
|
||||
word32*)
|
||||
{
|
||||
return (order == BigEndianOrder)
|
||||
? word32(block[3]) | (word32(block[2]) << 8) | (word32(block[1]) << 16)
|
||||
| (word32(block[0]) << 24)
|
||||
: word32(block[0]) | (word32(block[1]) << 8) | (word32(block[2]) << 16)
|
||||
| (word32(block[3]) << 24);
|
||||
}
|
||||
|
||||
template <class T>
|
||||
inline T UnalignedGetWord(ByteOrder order, const byte *block, T* dummy = 0)
|
||||
{
|
||||
return UnalignedGetWordNonTemplate(order, block, dummy);
|
||||
}
|
||||
|
||||
inline void UnalignedPutWord(ByteOrder order, byte *block, byte value,
|
||||
const byte *xorBlock = 0)
|
||||
{
|
||||
block[0] = xorBlock ? (value ^ xorBlock[0]) : value;
|
||||
}
|
||||
|
||||
#define GETBYTE(x, y) (unsigned int)byte((x)>>(8*(y)))
|
||||
|
||||
inline void UnalignedPutWord(ByteOrder order, byte *block, word16 value,
|
||||
const byte *xorBlock = 0)
|
||||
{
|
||||
if (order == BigEndianOrder)
|
||||
{
|
||||
block[0] = GETBYTE(value, 1);
|
||||
block[1] = GETBYTE(value, 0);
|
||||
}
|
||||
else
|
||||
{
|
||||
block[0] = GETBYTE(value, 0);
|
||||
block[1] = GETBYTE(value, 1);
|
||||
}
|
||||
|
||||
if (xorBlock)
|
||||
{
|
||||
block[0] ^= xorBlock[0];
|
||||
block[1] ^= xorBlock[1];
|
||||
}
|
||||
}
|
||||
|
||||
inline void UnalignedPutWord(ByteOrder order, byte* block, word32 value,
|
||||
const byte* xorBlock = 0)
|
||||
{
|
||||
if (order == BigEndianOrder)
|
||||
{
|
||||
block[0] = GETBYTE(value, 3);
|
||||
block[1] = GETBYTE(value, 2);
|
||||
block[2] = GETBYTE(value, 1);
|
||||
block[3] = GETBYTE(value, 0);
|
||||
}
|
||||
else
|
||||
{
|
||||
block[0] = GETBYTE(value, 0);
|
||||
block[1] = GETBYTE(value, 1);
|
||||
block[2] = GETBYTE(value, 2);
|
||||
block[3] = GETBYTE(value, 3);
|
||||
}
|
||||
|
||||
if (xorBlock)
|
||||
{
|
||||
block[0] ^= xorBlock[0];
|
||||
block[1] ^= xorBlock[1];
|
||||
block[2] ^= xorBlock[2];
|
||||
block[3] ^= xorBlock[3];
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
template <class T>
|
||||
inline T GetWord(bool assumeAligned, ByteOrder order, const byte *block)
|
||||
{
|
||||
if (assumeAligned)
|
||||
{
|
||||
assert(IsAligned<T>(block));
|
||||
return ByteReverseIf(*reinterpret_cast<const T *>(block), order);
|
||||
}
|
||||
else
|
||||
return UnalignedGetWord<T>(order, block);
|
||||
}
|
||||
|
||||
template <class T>
|
||||
inline void GetWord(bool assumeAligned, ByteOrder order, T &result,
|
||||
const byte *block)
|
||||
{
|
||||
result = GetWord<T>(assumeAligned, order, block);
|
||||
}
|
||||
|
||||
template <class T>
|
||||
inline void PutWord(bool assumeAligned, ByteOrder order, byte* block, T value,
|
||||
const byte *xorBlock = 0)
|
||||
{
|
||||
if (assumeAligned)
|
||||
{
|
||||
assert(IsAligned<T>(block));
|
||||
if (xorBlock)
|
||||
*reinterpret_cast<T *>(block) = ByteReverseIf(value, order)
|
||||
^ *reinterpret_cast<const T *>(xorBlock);
|
||||
else
|
||||
*reinterpret_cast<T *>(block) = ByteReverseIf(value, order);
|
||||
}
|
||||
else
|
||||
UnalignedPutWord(order, block, value, xorBlock);
|
||||
}
|
||||
|
||||
template <class T, class B, bool A=true>
|
||||
class GetBlock
|
||||
{
|
||||
public:
|
||||
GetBlock(const void *block)
|
||||
: m_block((const byte *)block) {}
|
||||
|
||||
template <class U>
|
||||
inline GetBlock<T, B, A> & operator()(U &x)
|
||||
{
|
||||
TAOCRYPT_COMPILE_ASSERT(sizeof(U) >= sizeof(T));
|
||||
x = GetWord<T>(A, B::ToEnum(), m_block);
|
||||
m_block += sizeof(T);
|
||||
return *this;
|
||||
}
|
||||
|
||||
private:
|
||||
const byte *m_block;
|
||||
};
|
||||
|
||||
template <class T, class B, bool A = true>
|
||||
class PutBlock
|
||||
{
|
||||
public:
|
||||
PutBlock(const void *xorBlock, void *block)
|
||||
: m_xorBlock((const byte *)xorBlock), m_block((byte *)block) {}
|
||||
|
||||
template <class U>
|
||||
inline PutBlock<T, B, A> & operator()(U x)
|
||||
{
|
||||
PutWord(A, B::ToEnum(), m_block, (T)x, m_xorBlock);
|
||||
m_block += sizeof(T);
|
||||
if (m_xorBlock)
|
||||
m_xorBlock += sizeof(T);
|
||||
return *this;
|
||||
}
|
||||
|
||||
private:
|
||||
const byte *m_xorBlock;
|
||||
byte *m_block;
|
||||
};
|
||||
|
||||
template <class T, class B, bool A=true>
|
||||
struct BlockGetAndPut
|
||||
{
|
||||
// function needed because of C++ grammatical ambiguity between
|
||||
// expression-statements and declarations
|
||||
static inline GetBlock<T, B, A> Get(const void *block)
|
||||
{return GetBlock<T, B, A>(block);}
|
||||
typedef PutBlock<T, B, A> Put;
|
||||
};
|
||||
|
||||
|
||||
|
||||
template <bool overflow> struct SafeShifter;
|
||||
|
||||
template<> struct SafeShifter<true>
|
||||
{
|
||||
template <class T>
|
||||
static inline T RightShift(T value, unsigned int bits)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
template <class T>
|
||||
static inline T LeftShift(T value, unsigned int bits)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
};
|
||||
|
||||
template<> struct SafeShifter<false>
|
||||
{
|
||||
template <class T>
|
||||
static inline T RightShift(T value, unsigned int bits)
|
||||
{
|
||||
return value >> bits;
|
||||
}
|
||||
|
||||
template <class T>
|
||||
static inline T LeftShift(T value, unsigned int bits)
|
||||
{
|
||||
return value << bits;
|
||||
}
|
||||
};
|
||||
|
||||
template <unsigned int bits, class T>
|
||||
inline T SafeRightShift(T value)
|
||||
{
|
||||
return SafeShifter<(bits>=(8*sizeof(T)))>::RightShift(value, bits);
|
||||
}
|
||||
|
||||
template <unsigned int bits, class T>
|
||||
inline T SafeLeftShift(T value)
|
||||
{
|
||||
return SafeShifter<(bits>=(8*sizeof(T)))>::LeftShift(value, bits);
|
||||
}
|
||||
|
||||
|
||||
inline
|
||||
word ShiftWordsLeftByBits(word* r, unsigned int n, unsigned int shiftBits)
|
||||
{
|
||||
assert (shiftBits<WORD_BITS);
|
||||
word u, carry=0;
|
||||
if (shiftBits)
|
||||
for (unsigned int i=0; i<n; i++)
|
||||
{
|
||||
u = r[i];
|
||||
r[i] = (u << shiftBits) | carry;
|
||||
carry = u >> (WORD_BITS-shiftBits);
|
||||
}
|
||||
return carry;
|
||||
}
|
||||
|
||||
|
||||
inline
|
||||
word ShiftWordsRightByBits(word* r, unsigned int n, unsigned int shiftBits)
|
||||
{
|
||||
assert (shiftBits<WORD_BITS);
|
||||
word u, carry=0;
|
||||
if (shiftBits)
|
||||
for (int i=n-1; i>=0; i--)
|
||||
{
|
||||
u = r[i];
|
||||
r[i] = (u >> shiftBits) | carry;
|
||||
carry = u << (WORD_BITS-shiftBits);
|
||||
}
|
||||
return carry;
|
||||
}
|
||||
|
||||
|
||||
inline
|
||||
void ShiftWordsLeftByWords(word* r, unsigned int n, unsigned int shiftWords)
|
||||
{
|
||||
shiftWords = min(shiftWords, n);
|
||||
if (shiftWords)
|
||||
{
|
||||
for (unsigned int i=n-1; i>=shiftWords; i--)
|
||||
r[i] = r[i-shiftWords];
|
||||
SetWords(r, 0, shiftWords);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
inline
|
||||
void ShiftWordsRightByWords(word* r, unsigned int n, unsigned int shiftWords)
|
||||
{
|
||||
shiftWords = min(shiftWords, n);
|
||||
if (shiftWords)
|
||||
{
|
||||
for (unsigned int i=0; i+shiftWords<n; i++)
|
||||
r[i] = r[i+shiftWords];
|
||||
SetWords(r+n-shiftWords, 0, shiftWords);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
template <class T1, class T2>
|
||||
inline T1 SaturatingSubtract(T1 a, T2 b)
|
||||
{
|
||||
TAOCRYPT_COMPILE_ASSERT_INSTANCE(T1(-1)>0, 0); // T1 is unsigned type
|
||||
TAOCRYPT_COMPILE_ASSERT_INSTANCE(T2(-1)>0, 1); // T2 is unsigned type
|
||||
return T1((a > b) ? (a - b) : 0);
|
||||
}
|
||||
|
||||
|
||||
// declares
|
||||
unsigned int BytePrecision(word value);
|
||||
unsigned int BitPrecision(word);
|
||||
word Crop(word value, unsigned int size);
|
||||
|
||||
|
||||
|
||||
} // namespace
|
||||
|
||||
#endif // TAO_CRYPT_MISC_HPP
|
||||
@@ -0,0 +1,165 @@
|
||||
/*
|
||||
Copyright (C) 2000-2007 MySQL AB
|
||||
|
||||
This program is free software; you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation; version 2 of the License.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program; see the file COPYING. If not, write to the
|
||||
Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
|
||||
MA 02110-1301 USA.
|
||||
*/
|
||||
|
||||
|
||||
/* based on Wei Dai's modarith.h from CryptoPP */
|
||||
|
||||
|
||||
#ifndef TAO_CRYPT_MODARITH_HPP
|
||||
#define TAO_CRYPT_MODARITH_HPP
|
||||
|
||||
#include "misc.hpp"
|
||||
#include "algebra.hpp"
|
||||
|
||||
namespace TaoCrypt {
|
||||
|
||||
|
||||
// ModularArithmetic
|
||||
class ModularArithmetic : public AbstractRing
|
||||
{
|
||||
public:
|
||||
|
||||
typedef int RandomizationParameter;
|
||||
typedef Integer Element;
|
||||
|
||||
ModularArithmetic(const Integer &modulus = Integer::One())
|
||||
: modulus(modulus), result((word)0, modulus.reg_.size()) {}
|
||||
|
||||
ModularArithmetic(const ModularArithmetic &ma)
|
||||
: AbstractRing(),
|
||||
modulus(ma.modulus), result((word)0, modulus.reg_.size()) {}
|
||||
|
||||
const Integer& GetModulus() const {return modulus;}
|
||||
void SetModulus(const Integer &newModulus)
|
||||
{
|
||||
modulus = newModulus;
|
||||
result.reg_.resize(modulus.reg_.size());
|
||||
}
|
||||
|
||||
virtual bool IsMontgomeryRepresentation() const {return false;}
|
||||
|
||||
virtual Integer ConvertIn(const Integer &a) const
|
||||
{return a%modulus;}
|
||||
|
||||
virtual Integer ConvertOut(const Integer &a) const
|
||||
{return a;}
|
||||
|
||||
const Integer& Half(const Integer &a) const;
|
||||
|
||||
bool Equal(const Integer &a, const Integer &b) const
|
||||
{return a==b;}
|
||||
|
||||
const Integer& Identity() const
|
||||
{return Integer::Zero();}
|
||||
|
||||
const Integer& Add(const Integer &a, const Integer &b) const;
|
||||
|
||||
Integer& Accumulate(Integer &a, const Integer &b) const;
|
||||
|
||||
const Integer& Inverse(const Integer &a) const;
|
||||
|
||||
const Integer& Subtract(const Integer &a, const Integer &b) const;
|
||||
|
||||
Integer& Reduce(Integer &a, const Integer &b) const;
|
||||
|
||||
const Integer& Double(const Integer &a) const
|
||||
{return Add(a, a);}
|
||||
|
||||
const Integer& MultiplicativeIdentity() const
|
||||
{return Integer::One();}
|
||||
|
||||
const Integer& Multiply(const Integer &a, const Integer &b) const
|
||||
{return result1 = a*b%modulus;}
|
||||
|
||||
const Integer& Square(const Integer &a) const
|
||||
{return result1 = a.Squared()%modulus;}
|
||||
|
||||
bool IsUnit(const Integer &a) const
|
||||
{return Integer::Gcd(a, modulus).IsUnit();}
|
||||
|
||||
const Integer& MultiplicativeInverse(const Integer &a) const
|
||||
{return result1 = a.InverseMod(modulus);}
|
||||
|
||||
const Integer& Divide(const Integer &a, const Integer &b) const
|
||||
{return Multiply(a, MultiplicativeInverse(b));}
|
||||
|
||||
Integer CascadeExponentiate(const Integer &x, const Integer &e1,
|
||||
const Integer &y, const Integer &e2) const;
|
||||
|
||||
void SimultaneousExponentiate(Element *results, const Element &base,
|
||||
const Integer *exponents, unsigned int exponentsCount) const;
|
||||
|
||||
unsigned int MaxElementBitLength() const
|
||||
{return (modulus-1).BitCount();}
|
||||
|
||||
unsigned int MaxElementByteLength() const
|
||||
{return (modulus-1).ByteCount();}
|
||||
|
||||
|
||||
static const RandomizationParameter DefaultRandomizationParameter;
|
||||
|
||||
protected:
|
||||
Integer modulus;
|
||||
mutable Integer result, result1;
|
||||
|
||||
};
|
||||
|
||||
|
||||
|
||||
//! do modular arithmetics in Montgomery representation for increased speed
|
||||
class MontgomeryRepresentation : public ModularArithmetic
|
||||
{
|
||||
public:
|
||||
MontgomeryRepresentation(const Integer &modulus); // modulus must be odd
|
||||
|
||||
bool IsMontgomeryRepresentation() const {return true;}
|
||||
|
||||
Integer ConvertIn(const Integer &a) const
|
||||
{return (a<<(WORD_BITS*modulus.reg_.size()))%modulus;}
|
||||
|
||||
Integer ConvertOut(const Integer &a) const;
|
||||
|
||||
const Integer& MultiplicativeIdentity() const
|
||||
{return result1 = Integer::Power2(WORD_BITS*modulus.reg_.size())%modulus;}
|
||||
|
||||
const Integer& Multiply(const Integer &a, const Integer &b) const;
|
||||
|
||||
const Integer& Square(const Integer &a) const;
|
||||
|
||||
const Integer& MultiplicativeInverse(const Integer &a) const;
|
||||
|
||||
Integer CascadeExponentiate(const Integer &x, const Integer &e1,
|
||||
const Integer &y, const Integer &e2) const
|
||||
{return AbstractRing::CascadeExponentiate(x, e1, y, e2);}
|
||||
|
||||
void SimultaneousExponentiate(Element *results, const Element &base,
|
||||
const Integer *exponents, unsigned int exponentsCount) const
|
||||
{AbstractRing::SimultaneousExponentiate(results, base,
|
||||
exponents, exponentsCount);}
|
||||
|
||||
private:
|
||||
Integer u;
|
||||
mutable AlignedWordBlock workspace;
|
||||
};
|
||||
|
||||
|
||||
|
||||
|
||||
} // namespace
|
||||
|
||||
#endif // TAO_CRYPT_MODARITH_HPP
|
||||
@@ -0,0 +1,155 @@
|
||||
/*
|
||||
Copyright (C) 2000-2007 MySQL AB
|
||||
|
||||
This program is free software; you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation; version 2 of the License.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program; see the file COPYING. If not, write to the
|
||||
Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
|
||||
MA 02110-1301 USA.
|
||||
*/
|
||||
|
||||
/* modes.hpp provides ECB and CBC modes for block cipher encryption/decryption
|
||||
*/
|
||||
|
||||
|
||||
#ifndef TAO_CRYPT_MODES_HPP
|
||||
#define TAO_CRYPT_MODES_HPP
|
||||
|
||||
#include "misc.hpp"
|
||||
|
||||
namespace TaoCrypt {
|
||||
|
||||
|
||||
enum Mode { ECB, CBC };
|
||||
|
||||
|
||||
|
||||
// BlockCipher abstraction
|
||||
template<CipherDir DIR, class T, Mode MODE>
|
||||
class BlockCipher {
|
||||
public:
|
||||
BlockCipher() : cipher_(DIR, MODE) {}
|
||||
|
||||
void Process(byte* c, const byte* p, word32 sz)
|
||||
{ cipher_.Process(c, p, sz); }
|
||||
void SetKey(const byte* k, word32 sz)
|
||||
{ cipher_.SetKey(k, sz, DIR); }
|
||||
void SetKey(const byte* k, word32 sz, const byte* iv)
|
||||
{ cipher_.SetKey(k, sz, DIR); cipher_.SetIV(iv); }
|
||||
private:
|
||||
T cipher_;
|
||||
|
||||
BlockCipher(const BlockCipher&); // hide copy
|
||||
BlockCipher& operator=(const BlockCipher&); // and assign
|
||||
};
|
||||
|
||||
|
||||
// Mode Base for block ciphers, static size
|
||||
class Mode_BASE : public virtual_base {
|
||||
public:
|
||||
enum { MaxBlockSz = 16 };
|
||||
|
||||
explicit Mode_BASE(int sz, CipherDir dir, Mode mode)
|
||||
: blockSz_(sz), reg_(reinterpret_cast<byte*>(r_)),
|
||||
tmp_(reinterpret_cast<byte*>(t_)), dir_(dir), mode_(mode)
|
||||
{
|
||||
assert(sz <= MaxBlockSz);
|
||||
}
|
||||
virtual ~Mode_BASE() {}
|
||||
|
||||
virtual void Process(byte*, const byte*, word32);
|
||||
|
||||
void SetIV(const byte* iv) { memcpy(reg_, iv, blockSz_); }
|
||||
protected:
|
||||
int blockSz_;
|
||||
byte* reg_;
|
||||
byte* tmp_;
|
||||
|
||||
word32 r_[MaxBlockSz / sizeof(word32)]; // align reg_ on word32
|
||||
word32 t_[MaxBlockSz / sizeof(word32)]; // align tmp_ on word32
|
||||
|
||||
CipherDir dir_;
|
||||
Mode mode_;
|
||||
|
||||
void ECB_Process(byte*, const byte*, word32);
|
||||
void CBC_Encrypt(byte*, const byte*, word32);
|
||||
void CBC_Decrypt(byte*, const byte*, word32);
|
||||
|
||||
Mode_BASE(const Mode_BASE&); // hide copy
|
||||
Mode_BASE& operator=(const Mode_BASE&); // and assign
|
||||
|
||||
private:
|
||||
virtual void ProcessAndXorBlock(const byte*, const byte*, byte*) const = 0;
|
||||
};
|
||||
|
||||
|
||||
inline void Mode_BASE::Process(byte* out, const byte* in, word32 sz)
|
||||
{
|
||||
if (mode_ == ECB)
|
||||
ECB_Process(out, in, sz);
|
||||
else if (mode_ == CBC)
|
||||
if (dir_ == ENCRYPTION)
|
||||
CBC_Encrypt(out, in, sz);
|
||||
else
|
||||
CBC_Decrypt(out, in, sz);
|
||||
}
|
||||
|
||||
|
||||
// ECB Process blocks
|
||||
inline void Mode_BASE::ECB_Process(byte* out, const byte* in, word32 sz)
|
||||
{
|
||||
word32 blocks = sz / blockSz_;
|
||||
|
||||
while (blocks--) {
|
||||
ProcessAndXorBlock(in, 0, out);
|
||||
out += blockSz_;
|
||||
in += blockSz_;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// CBC Encrypt
|
||||
inline void Mode_BASE::CBC_Encrypt(byte* out, const byte* in, word32 sz)
|
||||
{
|
||||
word32 blocks = sz / blockSz_;
|
||||
|
||||
while (blocks--) {
|
||||
xorbuf(reg_, in, blockSz_);
|
||||
ProcessAndXorBlock(reg_, 0, reg_);
|
||||
memcpy(out, reg_, blockSz_);
|
||||
out += blockSz_;
|
||||
in += blockSz_;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// CBC Decrypt
|
||||
inline void Mode_BASE::CBC_Decrypt(byte* out, const byte* in, word32 sz)
|
||||
{
|
||||
word32 blocks = sz / blockSz_;
|
||||
byte hold[MaxBlockSz];
|
||||
|
||||
while (blocks--) {
|
||||
memcpy(tmp_, in, blockSz_);
|
||||
ProcessAndXorBlock(tmp_, 0, out);
|
||||
xorbuf(out, reg_, blockSz_);
|
||||
memcpy(hold, reg_, blockSz_); // swap reg_ and tmp_
|
||||
memcpy(reg_, tmp_, blockSz_);
|
||||
memcpy(tmp_, hold, blockSz_);
|
||||
out += blockSz_;
|
||||
in += blockSz_;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
} // namespace
|
||||
|
||||
#endif // TAO_CRYPT_MODES_HPP
|
||||
@@ -0,0 +1,90 @@
|
||||
/*
|
||||
Copyright (C) 2000-2007 MySQL AB
|
||||
|
||||
This program is free software; you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation; version 2 of the License.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program; see the file COPYING. If not, write to the
|
||||
Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
|
||||
MA 02110-1301 USA.
|
||||
*/
|
||||
|
||||
/* pwdbased.hpp defines PBKDF2 from PKCS #5
|
||||
*/
|
||||
|
||||
|
||||
#ifndef TAO_CRYPT_PWDBASED_HPP
|
||||
#define TAO_CRYPT_PWDBASED_HPP
|
||||
|
||||
#include <string.h>
|
||||
#include "misc.hpp"
|
||||
#include "block.hpp"
|
||||
#include "hmac.hpp"
|
||||
|
||||
namespace TaoCrypt {
|
||||
|
||||
|
||||
// From PKCS #5, T must be type suitable for HMAC<T>
|
||||
template <class T>
|
||||
class PBKDF2_HMAC {
|
||||
public:
|
||||
word32 MaxDerivedKeyLength() const { return 0xFFFFFFFFU;} // avoid overflow
|
||||
|
||||
word32 DeriveKey(byte* derived, word32 dLen, const byte* pwd, word32 pLen,
|
||||
const byte* salt, word32 sLen, word32 iterations) const;
|
||||
};
|
||||
|
||||
|
||||
|
||||
template <class T>
|
||||
word32 PBKDF2_HMAC<T>::DeriveKey(byte* derived, word32 dLen, const byte* pwd,
|
||||
word32 pLen, const byte* salt, word32 sLen,
|
||||
word32 iterations) const
|
||||
{
|
||||
assert(dLen <= MaxDerivedKeyLength());
|
||||
assert(iterations > 0);
|
||||
|
||||
ByteBlock buffer(T::DIGEST_SIZE);
|
||||
HMAC<T> hmac;
|
||||
|
||||
hmac.SetKey(pwd, pLen);
|
||||
|
||||
word32 i = 1;
|
||||
|
||||
while (dLen > 0) {
|
||||
hmac.Update(salt, sLen);
|
||||
word32 j;
|
||||
for (j = 0; j < 4; j++) {
|
||||
byte b = i >> ((3-j)*8);
|
||||
hmac.Update(&b, 1);
|
||||
}
|
||||
hmac.Final(buffer.get_buffer());
|
||||
|
||||
word32 segmentLen = min(dLen, buffer.size());
|
||||
memcpy(derived, buffer.get_buffer(), segmentLen);
|
||||
|
||||
for (j = 1; j < iterations; j++) {
|
||||
hmac.Update(buffer.get_buffer(), buffer.size());
|
||||
hmac.Final(buffer.get_buffer());
|
||||
xorbuf(derived, buffer.get_buffer(), segmentLen);
|
||||
}
|
||||
derived += segmentLen;
|
||||
dLen -= segmentLen;
|
||||
i++;
|
||||
}
|
||||
return iterations;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
} // naemspace
|
||||
|
||||
#endif // TAO_CRYPT_PWDBASED_HPP
|
||||
@@ -0,0 +1,84 @@
|
||||
/*
|
||||
Copyright (C) 2000-2007 MySQL AB
|
||||
|
||||
This program is free software; you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation; version 2 of the License.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program; see the file COPYING. If not, write to the
|
||||
Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
|
||||
MA 02110-1301 USA.
|
||||
*/
|
||||
|
||||
/* random.hpp provides a crypto secure Random Number Generator using an OS
|
||||
specific seed
|
||||
*/
|
||||
|
||||
|
||||
#ifndef TAO_CRYPT_RANDOM_HPP
|
||||
#define TAO_CRYPT_RANDOM_HPP
|
||||
|
||||
#include "arc4.hpp"
|
||||
#include "error.hpp"
|
||||
|
||||
namespace TaoCrypt {
|
||||
|
||||
|
||||
// OS specific seeder
|
||||
class OS_Seed {
|
||||
public:
|
||||
OS_Seed();
|
||||
~OS_Seed();
|
||||
|
||||
void GenerateSeed(byte*, word32 sz);
|
||||
Error GetError() const { return error_; }
|
||||
private:
|
||||
#if defined(_WIN32)
|
||||
#if defined(_WIN64)
|
||||
typedef unsigned __int64 ProviderHandle;
|
||||
// type HCRYPTPROV, avoid #include <windows.h>
|
||||
#else
|
||||
typedef unsigned long ProviderHandle;
|
||||
#endif
|
||||
ProviderHandle handle_;
|
||||
#else
|
||||
int fd_;
|
||||
#endif
|
||||
Error error_;
|
||||
|
||||
OS_Seed(const OS_Seed&); // hide copy
|
||||
OS_Seed& operator=(const OS_Seed&); // hide assign
|
||||
};
|
||||
|
||||
|
||||
// secure Random Nnumber Generator
|
||||
class RandomNumberGenerator {
|
||||
public:
|
||||
RandomNumberGenerator();
|
||||
~RandomNumberGenerator() {}
|
||||
|
||||
void GenerateBlock(byte*, word32 sz);
|
||||
byte GenerateByte();
|
||||
|
||||
ErrorNumber GetError() const { return seed_.GetError().What(); }
|
||||
private:
|
||||
OS_Seed seed_;
|
||||
ARC4 cipher_;
|
||||
|
||||
RandomNumberGenerator(const RandomNumberGenerator&); // hide copy
|
||||
RandomNumberGenerator operator=(const RandomNumberGenerator&); // && assign
|
||||
};
|
||||
|
||||
|
||||
|
||||
|
||||
} // namespace
|
||||
|
||||
#endif // TAO_CRYPT_RANDOM_HPP
|
||||
|
||||
@@ -0,0 +1,69 @@
|
||||
/*
|
||||
Copyright (C) 2000-2007 MySQL AB
|
||||
|
||||
This program is free software; you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation; version 2 of the License.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program; see the file COPYING. If not, write to the
|
||||
Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
|
||||
MA 02110-1301 USA.
|
||||
*/
|
||||
|
||||
/* ripemd.hpp provides RIPEMD digest support
|
||||
*/
|
||||
|
||||
#ifndef TAO_CRYPT_RIPEMD_HPP
|
||||
#define TAO_CRYPT_RIPEMD_HPP
|
||||
|
||||
#include "hash.hpp"
|
||||
|
||||
|
||||
#if defined(TAOCRYPT_X86ASM_AVAILABLE) && defined(TAO_ASM)
|
||||
#define DO_RIPEMD_ASM
|
||||
#endif
|
||||
|
||||
namespace TaoCrypt {
|
||||
|
||||
|
||||
// RIPEMD160 digest
|
||||
class RIPEMD160 : public HASHwithTransform {
|
||||
public:
|
||||
enum { BLOCK_SIZE = 64, DIGEST_SIZE = 20, PAD_SIZE = 56,
|
||||
TAO_BYTE_ORDER = LittleEndianOrder }; // in Bytes
|
||||
RIPEMD160() : HASHwithTransform(DIGEST_SIZE / sizeof(word32), BLOCK_SIZE)
|
||||
{ Init(); }
|
||||
ByteOrder getByteOrder() const { return ByteOrder(TAO_BYTE_ORDER); }
|
||||
word32 getBlockSize() const { return BLOCK_SIZE; }
|
||||
word32 getDigestSize() const { return DIGEST_SIZE; }
|
||||
word32 getPadSize() const { return PAD_SIZE; }
|
||||
|
||||
RIPEMD160(const RIPEMD160&);
|
||||
RIPEMD160& operator= (const RIPEMD160&);
|
||||
|
||||
#ifdef DO_RIPEMD_ASM
|
||||
void Update(const byte*, word32);
|
||||
#endif
|
||||
void Init();
|
||||
void Swap(RIPEMD160&);
|
||||
private:
|
||||
void Transform();
|
||||
void AsmTransform(const byte* data, word32 times);
|
||||
};
|
||||
|
||||
inline void swap(RIPEMD160& a, RIPEMD160& b)
|
||||
{
|
||||
a.Swap(b);
|
||||
}
|
||||
|
||||
|
||||
} // namespace
|
||||
|
||||
#endif // TAO_CRYPT_RIPEMD_HPP
|
||||
|
||||
@@ -0,0 +1,250 @@
|
||||
/*
|
||||
Copyright (C) 2000-2007 MySQL AB
|
||||
|
||||
This program is free software; you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation; version 2 of the License.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program; see the file COPYING. If not, write to the
|
||||
Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
|
||||
MA 02110-1301 USA.
|
||||
*/
|
||||
|
||||
/* rsa.hpp provides RSA ES encrypt/decrypt, SSL (block type 1) sign and verify
|
||||
*/
|
||||
|
||||
#ifndef TAO_CRYPT_RSA_HPP
|
||||
#define TAO_CRYPT_RSA_HPP
|
||||
|
||||
#include "integer.hpp"
|
||||
#include "random.hpp"
|
||||
|
||||
|
||||
namespace TaoCrypt {
|
||||
|
||||
class Source;
|
||||
|
||||
|
||||
// Public Key Length helper
|
||||
class PK_Lengths {
|
||||
const Integer& image_;
|
||||
public:
|
||||
explicit PK_Lengths(const Integer& i) : image_(i) {}
|
||||
|
||||
word32 PaddedBlockBitLength() const {return image_.BitCount() - 1;}
|
||||
word32 PaddedBlockByteLength() const
|
||||
{return BitsToBytes(PaddedBlockBitLength());}
|
||||
|
||||
word32 FixedCiphertextLength() const {return image_.ByteCount();}
|
||||
word32 FixedMaxPlaintextLength() const
|
||||
{return SaturatingSubtract(PaddedBlockBitLength() / 8, 10U); }
|
||||
};
|
||||
|
||||
|
||||
// RSA Public Key
|
||||
class RSA_PublicKey {
|
||||
protected:
|
||||
Integer n_;
|
||||
Integer e_;
|
||||
public:
|
||||
RSA_PublicKey() {}
|
||||
explicit RSA_PublicKey(Source&);
|
||||
|
||||
void Initialize(const Integer& n, const Integer& e) {n_ = n; e_ = e;}
|
||||
void Initialize(Source&);
|
||||
|
||||
Integer ApplyFunction(const Integer& x) const;
|
||||
|
||||
const Integer& GetModulus() const {return n_;}
|
||||
const Integer& GetPublicExponent() const {return e_;}
|
||||
|
||||
void SetModulus(const Integer& n) {n_ = n;}
|
||||
void SetPublicExponent(const Integer& e) {e_ = e;}
|
||||
|
||||
word32 FixedCiphertextLength()
|
||||
{
|
||||
return PK_Lengths(n_).FixedCiphertextLength();
|
||||
}
|
||||
|
||||
RSA_PublicKey(const RSA_PublicKey& other) : n_(other.n_), e_(other.e_) {}
|
||||
RSA_PublicKey& operator=(const RSA_PublicKey& that)
|
||||
{
|
||||
RSA_PublicKey tmp(that);
|
||||
Swap(tmp);
|
||||
return *this;
|
||||
}
|
||||
|
||||
void Swap(RSA_PublicKey& other)
|
||||
{
|
||||
n_.Swap(other.n_);
|
||||
e_.Swap(other.e_);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
// RSA Private Key
|
||||
class RSA_PrivateKey : public RSA_PublicKey {
|
||||
Integer d_;
|
||||
Integer p_;
|
||||
Integer q_;
|
||||
Integer dp_;
|
||||
Integer dq_;
|
||||
Integer u_;
|
||||
public:
|
||||
RSA_PrivateKey() {}
|
||||
explicit RSA_PrivateKey(Source&);
|
||||
|
||||
void Initialize(const Integer& n, const Integer& e, const Integer& d,
|
||||
const Integer& p, const Integer& q, const Integer& dp,
|
||||
const Integer& dq, const Integer& u)
|
||||
{n_ = n; e_ = e; d_ = d; p_ = p; q_ = q; dp_ = dp; dq_ = dq; u_ = u;}
|
||||
void Initialize(Source&);
|
||||
|
||||
Integer CalculateInverse(RandomNumberGenerator&, const Integer&) const;
|
||||
|
||||
const Integer& GetPrime1() const {return p_;}
|
||||
const Integer& GetPrime2() const {return q_;}
|
||||
const Integer& GetPrivateExponent() const {return d_;}
|
||||
const Integer& GetModPrime1PrivateExponent() const {return dp_;}
|
||||
const Integer& GetModPrime2PrivateExponent() const {return dq_;}
|
||||
const Integer& GetMultiplicativeInverseOfPrime2ModPrime1() const
|
||||
{return u_;}
|
||||
|
||||
void SetPrime1(const Integer& p) {p_ = p;}
|
||||
void SetPrime2(const Integer& q) {q_ = q;}
|
||||
void SetPrivateExponent(const Integer& d) {d_ = d;}
|
||||
void SetModPrime1PrivateExponent(const Integer& dp) {dp_ = dp;}
|
||||
void SetModPrime2PrivateExponent(const Integer& dq) {dq_ = dq;}
|
||||
void SetMultiplicativeInverseOfPrime2ModPrime1(const Integer& u) {u_ = u;}
|
||||
private:
|
||||
RSA_PrivateKey(const RSA_PrivateKey&); // hide copy
|
||||
RSA_PrivateKey& operator=(const RSA_PrivateKey&); // and assign
|
||||
};
|
||||
|
||||
|
||||
// block type 2 padding
|
||||
class RSA_BlockType2 {
|
||||
public:
|
||||
void Pad(const byte*, word32, byte*, word32,
|
||||
RandomNumberGenerator&) const;
|
||||
word32 UnPad(const byte*, word32, byte*) const;
|
||||
};
|
||||
|
||||
|
||||
// block type 1 padding
|
||||
class RSA_BlockType1 {
|
||||
public:
|
||||
void Pad(const byte*, word32, byte*, word32,
|
||||
RandomNumberGenerator&) const;
|
||||
word32 UnPad(const byte*, word32, byte*) const;
|
||||
};
|
||||
|
||||
|
||||
// RSA Encryptor, can use any padding
|
||||
template<class Pad = RSA_BlockType2>
|
||||
class RSA_Encryptor {
|
||||
const RSA_PublicKey& key_;
|
||||
Pad padding_;
|
||||
public:
|
||||
explicit RSA_Encryptor(const RSA_PublicKey& k) : key_(k) {}
|
||||
|
||||
void Encrypt(const byte*, word32, byte*, RandomNumberGenerator&);
|
||||
bool SSL_Verify(const byte* msg, word32 sz, const byte* sig);
|
||||
};
|
||||
|
||||
|
||||
// RSA Decryptor, can use any padding
|
||||
template<class Pad = RSA_BlockType2>
|
||||
class RSA_Decryptor {
|
||||
const RSA_PrivateKey& key_;
|
||||
Pad padding_;
|
||||
public:
|
||||
explicit RSA_Decryptor(const RSA_PrivateKey& k) : key_(k) {}
|
||||
|
||||
word32 Decrypt(const byte*, word32, byte*, RandomNumberGenerator&);
|
||||
void SSL_Sign(const byte*, word32, byte*, RandomNumberGenerator&);
|
||||
};
|
||||
|
||||
|
||||
// Public Encrypt
|
||||
template<class Pad>
|
||||
void RSA_Encryptor<Pad>::Encrypt(const byte* plain, word32 sz, byte* cipher,
|
||||
RandomNumberGenerator& rng)
|
||||
{
|
||||
PK_Lengths lengths(key_.GetModulus());
|
||||
assert(sz <= lengths.FixedMaxPlaintextLength());
|
||||
|
||||
ByteBlock paddedBlock(lengths.PaddedBlockByteLength());
|
||||
padding_.Pad(plain, sz, paddedBlock.get_buffer(),
|
||||
lengths.PaddedBlockBitLength(), rng);
|
||||
|
||||
key_.ApplyFunction(Integer(paddedBlock.get_buffer(), paddedBlock.size())).
|
||||
Encode(cipher, lengths.FixedCiphertextLength());
|
||||
}
|
||||
|
||||
|
||||
// Private Decrypt
|
||||
template<class Pad>
|
||||
word32 RSA_Decryptor<Pad>::Decrypt(const byte* cipher, word32 sz, byte* plain,
|
||||
RandomNumberGenerator& rng)
|
||||
{
|
||||
PK_Lengths lengths(key_.GetModulus());
|
||||
assert(sz == lengths.FixedCiphertextLength());
|
||||
|
||||
if (sz != lengths.FixedCiphertextLength())
|
||||
return 0;
|
||||
|
||||
ByteBlock paddedBlock(lengths.PaddedBlockByteLength());
|
||||
Integer x = key_.CalculateInverse(rng, Integer(cipher,
|
||||
lengths.FixedCiphertextLength()).Ref());
|
||||
if (x.ByteCount() > paddedBlock.size())
|
||||
x = Integer::Zero(); // don't return false, prevents timing attack
|
||||
x.Encode(paddedBlock.get_buffer(), paddedBlock.size());
|
||||
return padding_.UnPad(paddedBlock.get_buffer(),
|
||||
lengths.PaddedBlockBitLength(), plain);
|
||||
}
|
||||
|
||||
|
||||
// Private SSL type (block 1) Encrypt
|
||||
template<class Pad>
|
||||
void RSA_Decryptor<Pad>::SSL_Sign(const byte* message, word32 sz, byte* sig,
|
||||
RandomNumberGenerator& rng)
|
||||
{
|
||||
RSA_PublicKey inverse;
|
||||
inverse.Initialize(key_.GetModulus(), key_.GetPrivateExponent());
|
||||
RSA_Encryptor<RSA_BlockType1> enc(inverse); // SSL Type
|
||||
enc.Encrypt(message, sz, sig, rng);
|
||||
}
|
||||
|
||||
|
||||
word32 SSL_Decrypt(const RSA_PublicKey& key, const byte* sig, byte* plain);
|
||||
|
||||
|
||||
// Public SSL type (block 1) Decrypt
|
||||
template<class Pad>
|
||||
bool RSA_Encryptor<Pad>::SSL_Verify(const byte* message, word32 sz,
|
||||
const byte* sig)
|
||||
{
|
||||
ByteBlock plain(PK_Lengths(key_.GetModulus()).FixedMaxPlaintextLength());
|
||||
if (SSL_Decrypt(key_, sig, plain.get_buffer()) != sz)
|
||||
return false; // not right justified or bad padding
|
||||
|
||||
if ( (memcmp(plain.get_buffer(), message, sz)) == 0)
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
typedef RSA_Encryptor<> RSAES_Encryptor;
|
||||
typedef RSA_Decryptor<> RSAES_Decryptor;
|
||||
|
||||
|
||||
} // namespace
|
||||
|
||||
#endif // TAO_CRYPT_RSA_HPP
|
||||
@@ -0,0 +1,72 @@
|
||||
/*
|
||||
Copyright (C) 2000-2007 MySQL AB
|
||||
|
||||
This program is free software; you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation; version 2 of the License.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program; see the file COPYING. If not, write to the
|
||||
Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
|
||||
MA 02110-1301 USA.
|
||||
*/
|
||||
|
||||
/* runtime.hpp provides C++ runtime support functions when building a pure C
|
||||
* version of yaSSL, user must define YASSL_PURE_C
|
||||
*/
|
||||
|
||||
|
||||
|
||||
#ifndef yaSSL_NEW_HPP
|
||||
#define yaSSL_NEW_HPP
|
||||
|
||||
#ifdef HAVE_CONFIG_H
|
||||
#include "config.h"
|
||||
#endif
|
||||
|
||||
#ifdef __sun
|
||||
|
||||
#include <assert.h>
|
||||
|
||||
// Handler for pure virtual functions
|
||||
namespace __Crun {
|
||||
static void pure_error(void)
|
||||
{
|
||||
assert("Pure virtual method called." == "Aborted");
|
||||
}
|
||||
} // namespace __Crun
|
||||
|
||||
#endif // __sun
|
||||
|
||||
|
||||
#if defined(__GNUC__) && !(defined(__ICC) || defined(__INTEL_COMPILER))
|
||||
|
||||
#if __GNUC__ > 2
|
||||
|
||||
extern "C" {
|
||||
#if !defined(DO_TAOCRYPT_KERNEL_MODE)
|
||||
#include <assert.h>
|
||||
#else
|
||||
#include "kernelc.hpp"
|
||||
#endif
|
||||
|
||||
/* Disallow inline __cxa_pure_virtual() */
|
||||
static int __cxa_pure_virtual() __attribute__((noinline, used));
|
||||
static int __cxa_pure_virtual()
|
||||
{
|
||||
// oops, pure virtual called!
|
||||
assert("Pure virtual method called." == "Aborted");
|
||||
return 0;
|
||||
}
|
||||
|
||||
} // extern "C"
|
||||
|
||||
#endif // __GNUC__ > 2
|
||||
#endif // compiler check
|
||||
#endif // yaSSL_NEW_HPP
|
||||
|
||||
@@ -0,0 +1,168 @@
|
||||
/*
|
||||
Copyright (C) 2000-2007 MySQL AB
|
||||
|
||||
This program is free software; you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation; version 2 of the License.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program; see the file COPYING. If not, write to the
|
||||
Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
|
||||
MA 02110-1301 USA.
|
||||
*/
|
||||
|
||||
/* sha.hpp provides SHA-1 digests, see RFC 3174
|
||||
*/
|
||||
|
||||
#ifndef TAO_CRYPT_SHA_HPP
|
||||
#define TAO_CRYPT_SHA_HPP
|
||||
|
||||
#include "hash.hpp"
|
||||
|
||||
|
||||
#if defined(TAOCRYPT_X86ASM_AVAILABLE) && defined(TAO_ASM)
|
||||
#define DO_SHA_ASM
|
||||
#endif
|
||||
|
||||
namespace TaoCrypt {
|
||||
|
||||
|
||||
// SHA-1 digest
|
||||
class SHA : public HASHwithTransform {
|
||||
public:
|
||||
enum { BLOCK_SIZE = 64, DIGEST_SIZE = 20, PAD_SIZE = 56,
|
||||
TAO_BYTE_ORDER = BigEndianOrder}; // in Bytes
|
||||
SHA() : HASHwithTransform(DIGEST_SIZE / sizeof(word32), BLOCK_SIZE)
|
||||
{ Init(); }
|
||||
ByteOrder getByteOrder() const { return ByteOrder(TAO_BYTE_ORDER); }
|
||||
word32 getBlockSize() const { return BLOCK_SIZE; }
|
||||
word32 getDigestSize() const { return DIGEST_SIZE; }
|
||||
word32 getPadSize() const { return PAD_SIZE; }
|
||||
|
||||
#ifdef DO_SHA_ASM
|
||||
void Update(const byte* data, word32 len);
|
||||
#endif
|
||||
void Init();
|
||||
|
||||
SHA(const SHA&);
|
||||
SHA& operator= (const SHA&);
|
||||
|
||||
void Swap(SHA&);
|
||||
private:
|
||||
void Transform();
|
||||
void AsmTransform(const byte* data, word32 times);
|
||||
};
|
||||
|
||||
|
||||
inline void swap(SHA& a, SHA& b)
|
||||
{
|
||||
a.Swap(b);
|
||||
}
|
||||
|
||||
// SHA-256 digest
|
||||
class SHA256 : public HASHwithTransform {
|
||||
public:
|
||||
enum { BLOCK_SIZE = 64, DIGEST_SIZE = 32, PAD_SIZE = 56,
|
||||
TAO_BYTE_ORDER = BigEndianOrder}; // in Bytes
|
||||
SHA256() : HASHwithTransform(DIGEST_SIZE / sizeof(word32), BLOCK_SIZE)
|
||||
{ Init(); }
|
||||
ByteOrder getByteOrder() const { return ByteOrder(TAO_BYTE_ORDER); }
|
||||
word32 getBlockSize() const { return BLOCK_SIZE; }
|
||||
word32 getDigestSize() const { return DIGEST_SIZE; }
|
||||
word32 getPadSize() const { return PAD_SIZE; }
|
||||
|
||||
void Init();
|
||||
|
||||
SHA256(const SHA256&);
|
||||
SHA256& operator= (const SHA256&);
|
||||
|
||||
void Swap(SHA256&);
|
||||
private:
|
||||
void Transform();
|
||||
};
|
||||
|
||||
|
||||
// SHA-224 digest
|
||||
class SHA224 : public HASHwithTransform {
|
||||
public:
|
||||
enum { BLOCK_SIZE = 64, DIGEST_SIZE = 28, PAD_SIZE = 56,
|
||||
TAO_BYTE_ORDER = BigEndianOrder}; // in Bytes
|
||||
SHA224() : HASHwithTransform(SHA256::DIGEST_SIZE /sizeof(word32),BLOCK_SIZE)
|
||||
{ Init(); }
|
||||
ByteOrder getByteOrder() const { return ByteOrder(TAO_BYTE_ORDER); }
|
||||
word32 getBlockSize() const { return BLOCK_SIZE; }
|
||||
word32 getDigestSize() const { return DIGEST_SIZE; }
|
||||
word32 getPadSize() const { return PAD_SIZE; }
|
||||
|
||||
void Init();
|
||||
|
||||
SHA224(const SHA224&);
|
||||
SHA224& operator= (const SHA224&);
|
||||
|
||||
void Swap(SHA224&);
|
||||
private:
|
||||
void Transform();
|
||||
};
|
||||
|
||||
|
||||
#ifdef WORD64_AVAILABLE
|
||||
|
||||
// SHA-512 digest
|
||||
class SHA512 : public HASH64withTransform {
|
||||
public:
|
||||
enum { BLOCK_SIZE = 128, DIGEST_SIZE = 64, PAD_SIZE = 112,
|
||||
TAO_BYTE_ORDER = BigEndianOrder}; // in Bytes
|
||||
SHA512() : HASH64withTransform(DIGEST_SIZE / sizeof(word64), BLOCK_SIZE)
|
||||
{ Init(); }
|
||||
ByteOrder getByteOrder() const { return ByteOrder(TAO_BYTE_ORDER); }
|
||||
word32 getBlockSize() const { return BLOCK_SIZE; }
|
||||
word32 getDigestSize() const { return DIGEST_SIZE; }
|
||||
word32 getPadSize() const { return PAD_SIZE; }
|
||||
|
||||
void Init();
|
||||
|
||||
SHA512(const SHA512&);
|
||||
SHA512& operator= (const SHA512&);
|
||||
|
||||
void Swap(SHA512&);
|
||||
private:
|
||||
void Transform();
|
||||
};
|
||||
|
||||
|
||||
// SHA-384 digest
|
||||
class SHA384 : public HASH64withTransform {
|
||||
public:
|
||||
enum { BLOCK_SIZE = 128, DIGEST_SIZE = 48, PAD_SIZE = 112,
|
||||
TAO_BYTE_ORDER = BigEndianOrder}; // in Bytes
|
||||
SHA384() : HASH64withTransform(SHA512::DIGEST_SIZE/ sizeof(word64),
|
||||
BLOCK_SIZE)
|
||||
{ Init(); }
|
||||
ByteOrder getByteOrder() const { return ByteOrder(TAO_BYTE_ORDER); }
|
||||
word32 getBlockSize() const { return BLOCK_SIZE; }
|
||||
word32 getDigestSize() const { return DIGEST_SIZE; }
|
||||
word32 getPadSize() const { return PAD_SIZE; }
|
||||
|
||||
void Init();
|
||||
|
||||
SHA384(const SHA384&);
|
||||
SHA384& operator= (const SHA384&);
|
||||
|
||||
void Swap(SHA384&);
|
||||
private:
|
||||
void Transform();
|
||||
};
|
||||
|
||||
#endif // WORD64_AVAILABLE
|
||||
|
||||
|
||||
} // namespace
|
||||
|
||||
|
||||
#endif // TAO_CRYPT_SHA_HPP
|
||||
|
||||
@@ -0,0 +1,94 @@
|
||||
/*
|
||||
Copyright (C) 2000-2007 MySQL AB
|
||||
|
||||
This program is free software; you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation; version 2 of the License.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program; see the file COPYING. If not, write to the
|
||||
Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
|
||||
MA 02110-1301 USA.
|
||||
*/
|
||||
|
||||
/* twofish.hpp defines Twofish
|
||||
*/
|
||||
|
||||
|
||||
#ifndef TAO_CRYPT_TWOFISH_HPP
|
||||
#define TAO_CRYPT_TWOFISH_HPP
|
||||
|
||||
#include "misc.hpp"
|
||||
#include "modes.hpp"
|
||||
#ifdef USE_SYS_STL
|
||||
#include <algorithm>
|
||||
#else
|
||||
#include "algorithm.hpp"
|
||||
#endif
|
||||
|
||||
|
||||
namespace STL = STL_NAMESPACE;
|
||||
|
||||
|
||||
#if defined(TAOCRYPT_X86ASM_AVAILABLE) && defined(TAO_ASM)
|
||||
#define DO_TWOFISH_ASM
|
||||
#endif
|
||||
|
||||
namespace TaoCrypt {
|
||||
|
||||
enum { TWOFISH_BLOCK_SIZE = 16 };
|
||||
|
||||
|
||||
// Twofish encryption and decryption, see
|
||||
class Twofish : public Mode_BASE {
|
||||
public:
|
||||
enum { BLOCK_SIZE = TWOFISH_BLOCK_SIZE };
|
||||
|
||||
Twofish(CipherDir DIR, Mode MODE)
|
||||
: Mode_BASE(BLOCK_SIZE, DIR, MODE) {}
|
||||
|
||||
#ifdef DO_TWOFISH_ASM
|
||||
void Process(byte*, const byte*, word32);
|
||||
#endif
|
||||
void SetKey(const byte* key, word32 sz, CipherDir fake = ENCRYPTION);
|
||||
void SetIV(const byte* iv) { memcpy(r_, iv, BLOCK_SIZE); }
|
||||
private:
|
||||
static const byte q_[2][256];
|
||||
static const word32 mds_[4][256];
|
||||
|
||||
word32 k_[40];
|
||||
word32 s_[4][256];
|
||||
|
||||
static word32 h0(word32 x, const word32 *key, unsigned int kLen);
|
||||
static word32 h(word32 x, const word32 *key, unsigned int kLen);
|
||||
|
||||
void ProcessAndXorBlock(const byte*, const byte*, byte*) const;
|
||||
|
||||
void encrypt(const byte*, const byte*, byte*) const;
|
||||
void decrypt(const byte*, const byte*, byte*) const;
|
||||
|
||||
void AsmEncrypt(const byte* inBlock, byte* outBlock) const;
|
||||
void AsmDecrypt(const byte* inBlock, byte* outBlock) const;
|
||||
|
||||
Twofish(const Twofish&); // hide copy
|
||||
Twofish& operator=(const Twofish&); // and assign
|
||||
};
|
||||
|
||||
|
||||
typedef BlockCipher<ENCRYPTION, Twofish, ECB> Twofish_ECB_Encryption;
|
||||
typedef BlockCipher<DECRYPTION, Twofish, ECB> Twofish_ECB_Decryption;
|
||||
|
||||
typedef BlockCipher<ENCRYPTION, Twofish, CBC> Twofish_CBC_Encryption;
|
||||
typedef BlockCipher<DECRYPTION, Twofish, CBC> Twofish_CBC_Decryption;
|
||||
|
||||
|
||||
|
||||
} // naemspace
|
||||
|
||||
#endif // TAO_CRYPT_TWOFISH_HPP
|
||||
|
||||
@@ -0,0 +1,80 @@
|
||||
/*
|
||||
Copyright (C) 2000-2007 MySQL AB
|
||||
|
||||
This program is free software; you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation; version 2 of the License.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program; see the file COPYING. If not, write to the
|
||||
Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
|
||||
MA 02110-1301 USA.
|
||||
*/
|
||||
|
||||
/* type_traits defines fundamental types
|
||||
* see discussion in C++ Templates, $19.1
|
||||
*/
|
||||
|
||||
|
||||
#ifndef TAO_CRYPT_TYPE_TRAITS_HPP
|
||||
#define TAO_CRYPT_TYPE_TRAITS_HPP
|
||||
|
||||
#include "types.hpp"
|
||||
|
||||
namespace TaoCrypt {
|
||||
|
||||
|
||||
// primary template: in general T is not a fundamental type
|
||||
|
||||
template <typename T>
|
||||
class IsFundamentalType {
|
||||
public:
|
||||
enum { Yes = 0, No = 1 };
|
||||
};
|
||||
|
||||
|
||||
// macro to specialize for fundamental types
|
||||
#define MK_FUNDAMENTAL_TYPE(T) \
|
||||
template<> class IsFundamentalType<T> { \
|
||||
public: \
|
||||
enum { Yes = 1, No = 0 }; \
|
||||
};
|
||||
|
||||
|
||||
MK_FUNDAMENTAL_TYPE(void)
|
||||
|
||||
#ifndef bool_defined
|
||||
MK_FUNDAMENTAL_TYPE(bool)
|
||||
#endif
|
||||
|
||||
MK_FUNDAMENTAL_TYPE( char)
|
||||
MK_FUNDAMENTAL_TYPE(signed char)
|
||||
MK_FUNDAMENTAL_TYPE(unsigned char)
|
||||
|
||||
MK_FUNDAMENTAL_TYPE(signed short)
|
||||
MK_FUNDAMENTAL_TYPE(unsigned short)
|
||||
MK_FUNDAMENTAL_TYPE(signed int)
|
||||
MK_FUNDAMENTAL_TYPE(unsigned int)
|
||||
MK_FUNDAMENTAL_TYPE(signed long)
|
||||
MK_FUNDAMENTAL_TYPE(unsigned long)
|
||||
|
||||
MK_FUNDAMENTAL_TYPE(float)
|
||||
MK_FUNDAMENTAL_TYPE( double)
|
||||
MK_FUNDAMENTAL_TYPE(long double)
|
||||
|
||||
#if defined(WORD64_AVAILABLE) && defined(WORD64_IS_DISTINCT_TYPE)
|
||||
MK_FUNDAMENTAL_TYPE(word64)
|
||||
#endif
|
||||
|
||||
|
||||
#undef MK_FUNDAMENTAL_TYPE
|
||||
|
||||
|
||||
} // namespace
|
||||
|
||||
#endif // TAO_CRYPT_TYPE_TRAITS_HPP
|
||||
@@ -0,0 +1,99 @@
|
||||
/*
|
||||
Copyright (C) 2000-2007 MySQL AB
|
||||
|
||||
This program is free software; you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation; version 2 of the License.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program; see the file COPYING. If not, write to the
|
||||
Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
|
||||
MA 02110-1301 USA.
|
||||
*/
|
||||
|
||||
/* based on Wei Dai's misc.h from CryptoPP, basic crypt types */
|
||||
|
||||
|
||||
#ifndef TAO_CRYPT_TYPES_HPP
|
||||
#define TAO_CRYPT_TYPES_HPP
|
||||
|
||||
#ifdef HAVE_CONFIG_H
|
||||
#include "config.h"
|
||||
#endif
|
||||
|
||||
namespace TaoCrypt {
|
||||
|
||||
|
||||
#if defined(WORDS_BIGENDIAN) || (defined(__MWERKS__) && !defined(__INTEL__))
|
||||
#define BIG_ENDIAN_ORDER
|
||||
#endif
|
||||
|
||||
#ifndef BIG_ENDIAN_ORDER
|
||||
#define LITTLE_ENDIAN_ORDER
|
||||
#endif
|
||||
|
||||
|
||||
typedef unsigned char byte;
|
||||
typedef unsigned short word16;
|
||||
typedef unsigned int word32;
|
||||
|
||||
#if defined(_MSC_VER) || defined(__BCPLUSPLUS__)
|
||||
#define WORD64_AVAILABLE
|
||||
#define WORD64_IS_DISTINCT_TYPE
|
||||
typedef unsigned __int64 word64;
|
||||
#define W64LIT(x) x##ui64
|
||||
#elif SIZEOF_LONG == 8
|
||||
#define WORD64_AVAILABLE
|
||||
typedef unsigned long word64;
|
||||
#define W64LIT(x) x##LL
|
||||
#elif SIZEOF_LONG_LONG == 8
|
||||
#define WORD64_AVAILABLE
|
||||
#define WORD64_IS_DISTINCT_TYPE
|
||||
typedef unsigned long long word64;
|
||||
#define W64LIT(x) x##LL
|
||||
#endif
|
||||
|
||||
|
||||
// compilers we've found 64-bit multiply insructions for
|
||||
#if defined(__GNUC__) || defined(_MSC_VER) || defined(__DECCXX)
|
||||
#if !(defined(__ICC) || defined(__INTEL_COMPILER))
|
||||
#define HAVE_64_MULTIPLY
|
||||
#endif
|
||||
#endif
|
||||
|
||||
|
||||
#if defined(HAVE_64_MULTIPLY) && (defined(__alpha__) || defined(__ia64__) \
|
||||
|| defined(_ARCH_PPC64) || defined(__mips64) || defined(__x86_64__) \
|
||||
|| defined(_M_X64) || defined(_M_IA64))
|
||||
// These platforms have 64-bit CPU registers. Unfortunately most C++ compilers
|
||||
// don't allow any way to access the 64-bit by 64-bit multiply instruction
|
||||
// without using assembly, so in order to use word64 as word, the assembly
|
||||
// instruction must be defined in Dword::Multiply().
|
||||
typedef word32 hword;
|
||||
typedef word64 word;
|
||||
#else
|
||||
#define TAOCRYPT_NATIVE_DWORD_AVAILABLE
|
||||
#ifdef WORD64_AVAILABLE
|
||||
#define TAOCRYPT_SLOW_WORD64
|
||||
typedef word16 hword;
|
||||
typedef word32 word;
|
||||
typedef word64 dword;
|
||||
#else
|
||||
typedef byte hword;
|
||||
typedef word16 word;
|
||||
typedef word32 dword;
|
||||
#endif
|
||||
#endif
|
||||
|
||||
const word32 WORD_SIZE = sizeof(word);
|
||||
const word32 WORD_BITS = WORD_SIZE * 8;
|
||||
|
||||
|
||||
} // namespace
|
||||
|
||||
#endif // TAO_CRYPT_TYPES_HPP
|
||||
@@ -0,0 +1,108 @@
|
||||
/*
|
||||
Copyright (C) 2000-2007 MySQL AB
|
||||
|
||||
This program is free software; you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation; version 2 of the License.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program; see the file COPYING. If not, write to the
|
||||
Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
|
||||
MA 02110-1301 USA.
|
||||
*/
|
||||
|
||||
|
||||
/* mySTL algorithm implements max, min, for_each, swap, find_if, copy,
|
||||
* copy_backward, fill
|
||||
*/
|
||||
|
||||
#ifndef mySTL_ALGORITHM_HPP
|
||||
#define mySTL_ALGORITHM_HPP
|
||||
|
||||
|
||||
namespace mySTL {
|
||||
|
||||
|
||||
template<typename T>
|
||||
inline const T& max(const T& a, const T&b)
|
||||
{
|
||||
return a < b ? b : a;
|
||||
}
|
||||
|
||||
|
||||
template<typename T>
|
||||
inline const T& min(const T& a, const T&b)
|
||||
{
|
||||
return b < a ? b : a;
|
||||
}
|
||||
|
||||
|
||||
template<typename InIter, typename Func>
|
||||
Func for_each(InIter first, InIter last, Func op)
|
||||
{
|
||||
while (first != last) {
|
||||
op(*first);
|
||||
++first;
|
||||
}
|
||||
return op;
|
||||
}
|
||||
|
||||
|
||||
template<typename T>
|
||||
inline void swap(T& a, T& b)
|
||||
{
|
||||
T tmp = a;
|
||||
a = b;
|
||||
b = tmp;
|
||||
}
|
||||
|
||||
|
||||
template<typename InIter, typename Pred>
|
||||
InIter find_if(InIter first, InIter last, Pred pred)
|
||||
{
|
||||
while (first != last && !pred(*first))
|
||||
++first;
|
||||
return first;
|
||||
}
|
||||
|
||||
|
||||
template<typename InputIter, typename OutputIter>
|
||||
inline OutputIter copy(InputIter first, InputIter last, OutputIter place)
|
||||
{
|
||||
while (first != last) {
|
||||
*place = *first;
|
||||
++first;
|
||||
++place;
|
||||
}
|
||||
return place;
|
||||
}
|
||||
|
||||
|
||||
template<typename InputIter, typename OutputIter>
|
||||
inline OutputIter
|
||||
copy_backward(InputIter first, InputIter last, OutputIter place)
|
||||
{
|
||||
while (first != last)
|
||||
*--place = *--last;
|
||||
return place;
|
||||
}
|
||||
|
||||
|
||||
template<typename InputIter, typename T>
|
||||
void fill(InputIter first, InputIter last, const T& v)
|
||||
{
|
||||
while (first != last) {
|
||||
*first = v;
|
||||
++first;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
} // namespace mySTL
|
||||
|
||||
#endif // mySTL_ALGORITHM_HPP
|
||||
@@ -0,0 +1,153 @@
|
||||
/*
|
||||
Copyright (C) 2000-2007 MySQL AB
|
||||
|
||||
This program is free software; you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation; version 2 of the License.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program; see the file COPYING. If not, write to the
|
||||
Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
|
||||
MA 02110-1301 USA.
|
||||
*/
|
||||
|
||||
|
||||
/* mySTL helpers implements misc constructs for vector and list
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef mySTL_HELPERS_HPP
|
||||
#define mySTL_HELPERS_HPP
|
||||
|
||||
#include <stdlib.h>
|
||||
#ifdef _MSC_VER
|
||||
#include <new>
|
||||
#endif
|
||||
|
||||
/*
|
||||
Workaround for the lack of operator new(size_t, void*)
|
||||
in IBM VA C++ 6.0
|
||||
Also used as a workaround to avoid including <new>
|
||||
*/
|
||||
struct Dummy {};
|
||||
|
||||
inline void* operator new(size_t size, Dummy* d)
|
||||
{
|
||||
return static_cast<void*>(d);
|
||||
}
|
||||
|
||||
// for compilers that want matching delete
|
||||
inline void operator delete(void* ptr, Dummy* d)
|
||||
{
|
||||
}
|
||||
|
||||
typedef Dummy* yassl_pointer;
|
||||
|
||||
namespace mySTL {
|
||||
|
||||
|
||||
template <typename T, typename T2>
|
||||
inline void construct(T* p, const T2& value)
|
||||
{
|
||||
new (reinterpret_cast<yassl_pointer>(p)) T(value);
|
||||
}
|
||||
|
||||
|
||||
template <typename T>
|
||||
inline void construct(T* p)
|
||||
{
|
||||
new (reinterpret_cast<yassl_pointer>(p)) T();
|
||||
}
|
||||
|
||||
|
||||
template <typename T>
|
||||
inline void destroy(T* p)
|
||||
{
|
||||
p->~T();
|
||||
}
|
||||
|
||||
|
||||
template <typename Iter>
|
||||
void destroy(Iter first, Iter last)
|
||||
{
|
||||
while (first != last) {
|
||||
destroy(&*first);
|
||||
++first;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
template <typename Iter, typename PlaceIter>
|
||||
PlaceIter uninit_copy(Iter first, Iter last, PlaceIter place)
|
||||
{
|
||||
while (first != last) {
|
||||
construct(&*place, *first);
|
||||
++first;
|
||||
++place;
|
||||
}
|
||||
return place;
|
||||
}
|
||||
|
||||
|
||||
template <typename PlaceIter, typename Size, typename T>
|
||||
PlaceIter uninit_fill_n(PlaceIter place, Size n, const T& value)
|
||||
{
|
||||
while (n) {
|
||||
construct(&*place, value);
|
||||
--n;
|
||||
++place;
|
||||
}
|
||||
return place;
|
||||
}
|
||||
|
||||
|
||||
template <typename T>
|
||||
T* GetArrayMemory(size_t items)
|
||||
{
|
||||
unsigned char* ret;
|
||||
|
||||
#ifdef YASSL_LIB
|
||||
ret = NEW_YS unsigned char[sizeof(T) * items];
|
||||
#else
|
||||
ret = NEW_TC unsigned char[sizeof(T) * items];
|
||||
#endif
|
||||
|
||||
return reinterpret_cast<T*>(ret);
|
||||
}
|
||||
|
||||
|
||||
template <typename T>
|
||||
void FreeArrayMemory(T* ptr)
|
||||
{
|
||||
unsigned char* p = reinterpret_cast<unsigned char*>(ptr);
|
||||
|
||||
#ifdef YASSL_LIB
|
||||
yaSSL::ysArrayDelete(p);
|
||||
#else
|
||||
TaoCrypt::tcArrayDelete(p);
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
|
||||
inline void* GetMemory(size_t bytes)
|
||||
{
|
||||
return GetArrayMemory<unsigned char>(bytes);
|
||||
}
|
||||
|
||||
|
||||
inline void FreeMemory(void* ptr)
|
||||
{
|
||||
FreeArrayMemory(ptr);
|
||||
}
|
||||
|
||||
|
||||
|
||||
} // namespace mySTL
|
||||
|
||||
#endif // mySTL_HELPERS_HPP
|
||||
@@ -0,0 +1,367 @@
|
||||
/*
|
||||
Copyright (C) 2000-2007 MySQL AB
|
||||
|
||||
This program is free software; you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation; version 2 of the License.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program; see the file COPYING. If not, write to the
|
||||
Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
|
||||
MA 02110-1301 USA.
|
||||
*/
|
||||
|
||||
|
||||
/* mySTL list implements a simple list
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef mySTL_LIST_HPP
|
||||
#define mySTL_LIST_HPP
|
||||
|
||||
|
||||
#include "helpers.hpp"
|
||||
|
||||
|
||||
namespace mySTL {
|
||||
|
||||
|
||||
|
||||
template<typename T>
|
||||
class list {
|
||||
|
||||
#ifdef __SUNPRO_CC
|
||||
/*
|
||||
Sun Forte 7 C++ v. 5.4 needs class 'node' public to be visible to
|
||||
the nested class 'iterator' (a non-standard behaviour).
|
||||
*/
|
||||
public:
|
||||
#endif
|
||||
|
||||
struct node {
|
||||
node(T t) : prev_(0), next_(0), value_(t) {}
|
||||
|
||||
node* prev_;
|
||||
node* next_;
|
||||
T value_;
|
||||
};
|
||||
public:
|
||||
list() : head_(0), tail_(0), sz_(0) {}
|
||||
~list();
|
||||
|
||||
void push_front(T);
|
||||
void pop_front();
|
||||
T front() const;
|
||||
void push_back(T);
|
||||
void pop_back();
|
||||
T back() const;
|
||||
bool remove(T);
|
||||
size_t size() const { return sz_; }
|
||||
bool empty() const { return sz_ == 0; }
|
||||
|
||||
class iterator {
|
||||
node* current_;
|
||||
public:
|
||||
explicit iterator(node* p = 0) : current_(p) {}
|
||||
|
||||
T& operator*() const
|
||||
{
|
||||
return current_->value_;
|
||||
}
|
||||
|
||||
T* operator->() const
|
||||
{
|
||||
return &(operator*());
|
||||
}
|
||||
|
||||
iterator& operator++()
|
||||
{
|
||||
current_ = current_->next_;
|
||||
return *this;
|
||||
}
|
||||
|
||||
iterator& operator--()
|
||||
{
|
||||
current_ = current_->prev_;
|
||||
return *this;
|
||||
}
|
||||
|
||||
iterator operator++(int)
|
||||
{
|
||||
iterator tmp = *this;
|
||||
current_ = current_->next_;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
iterator operator--(int)
|
||||
{
|
||||
iterator tmp = *this;
|
||||
current_ = current_->prev_;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
bool operator==(const iterator& other) const
|
||||
{
|
||||
return current_ == other.current_;
|
||||
}
|
||||
|
||||
bool operator!=(const iterator& other) const
|
||||
{
|
||||
return current_ != other.current_;
|
||||
}
|
||||
|
||||
friend class list<T>;
|
||||
};
|
||||
|
||||
|
||||
class reverse_iterator {
|
||||
node* current_;
|
||||
public:
|
||||
explicit reverse_iterator(node* p = 0) : current_(p) {}
|
||||
|
||||
T& operator*() const
|
||||
{
|
||||
return current_->value_;
|
||||
}
|
||||
|
||||
T* operator->() const
|
||||
{
|
||||
return &(operator*());
|
||||
}
|
||||
|
||||
reverse_iterator& operator++()
|
||||
{
|
||||
current_ = current_->prev_;
|
||||
return *this;
|
||||
}
|
||||
|
||||
reverse_iterator& operator--()
|
||||
{
|
||||
current_ = current_->next_;
|
||||
return *this;
|
||||
}
|
||||
|
||||
reverse_iterator operator++(int)
|
||||
{
|
||||
reverse_iterator tmp = *this;
|
||||
current_ = current_->prev_;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
reverse_iterator operator--(int)
|
||||
{
|
||||
reverse_iterator tmp = *this;
|
||||
current_ = current_->next_;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
bool operator==(const reverse_iterator& other) const
|
||||
{
|
||||
return current_ == other.current_;
|
||||
}
|
||||
|
||||
bool operator!=(const reverse_iterator& other) const
|
||||
{
|
||||
return current_ != other.current_;
|
||||
}
|
||||
|
||||
friend class list<T>;
|
||||
};
|
||||
|
||||
bool erase(iterator);
|
||||
|
||||
iterator begin() const { return iterator(head_); }
|
||||
reverse_iterator rbegin() const { return reverse_iterator(tail_); }
|
||||
iterator end() const { return iterator(); }
|
||||
reverse_iterator rend() const { return reverse_iterator(); }
|
||||
|
||||
typedef iterator const_iterator; // for now
|
||||
|
||||
class underflow {};
|
||||
class overflow {};
|
||||
private:
|
||||
node* head_;
|
||||
node* tail_;
|
||||
size_t sz_;
|
||||
|
||||
node* look_up(T);
|
||||
|
||||
list(const list&); // hide copy
|
||||
list& operator=(const list&); // and assign
|
||||
};
|
||||
|
||||
|
||||
template<typename T>
|
||||
list<T>::~list()
|
||||
{
|
||||
node* start = head_;
|
||||
node* next_;
|
||||
|
||||
for (; start; start = next_) {
|
||||
next_ = start->next_;
|
||||
destroy(start);
|
||||
FreeMemory(start);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
template<typename T>
|
||||
void list<T>::push_front(T t)
|
||||
{
|
||||
void* mem = GetMemory(sizeof(node));
|
||||
node* add = new (reinterpret_cast<yassl_pointer>(mem)) node(t);
|
||||
|
||||
if (head_) {
|
||||
add->next_ = head_;
|
||||
head_->prev_ = add;
|
||||
}
|
||||
else
|
||||
tail_ = add;
|
||||
|
||||
head_ = add;
|
||||
++sz_;
|
||||
}
|
||||
|
||||
|
||||
template<typename T>
|
||||
void list<T>::pop_front()
|
||||
{
|
||||
node* front = head_;
|
||||
|
||||
if (head_ == 0)
|
||||
return;
|
||||
else if (head_ == tail_)
|
||||
head_ = tail_ = 0;
|
||||
else {
|
||||
head_ = head_->next_;
|
||||
head_->prev_ = 0;
|
||||
}
|
||||
destroy(front);
|
||||
FreeMemory(front);
|
||||
--sz_;
|
||||
}
|
||||
|
||||
|
||||
template<typename T>
|
||||
T list<T>::front() const
|
||||
{
|
||||
if (head_ == 0) return T();
|
||||
return head_->value_;
|
||||
}
|
||||
|
||||
|
||||
template<typename T>
|
||||
void list<T>::push_back(T t)
|
||||
{
|
||||
void* mem = GetMemory(sizeof(node));
|
||||
node* add = new (reinterpret_cast<yassl_pointer>(mem)) node(t);
|
||||
|
||||
if (tail_) {
|
||||
tail_->next_ = add;
|
||||
add->prev_ = tail_;
|
||||
}
|
||||
else
|
||||
head_ = add;
|
||||
|
||||
tail_ = add;
|
||||
++sz_;
|
||||
}
|
||||
|
||||
|
||||
template<typename T>
|
||||
void list<T>::pop_back()
|
||||
{
|
||||
node* rear = tail_;
|
||||
|
||||
if (tail_ == 0)
|
||||
return;
|
||||
else if (tail_ == head_)
|
||||
tail_ = head_ = 0;
|
||||
else {
|
||||
tail_ = tail_->prev_;
|
||||
tail_->next_ = 0;
|
||||
}
|
||||
destroy(rear);
|
||||
FreeMemory(rear);
|
||||
--sz_;
|
||||
}
|
||||
|
||||
|
||||
template<typename T>
|
||||
T list<T>::back() const
|
||||
{
|
||||
if (tail_ == 0) return T();
|
||||
return tail_->value_;
|
||||
}
|
||||
|
||||
|
||||
template<typename T>
|
||||
typename list<T>::node* list<T>::look_up(T t)
|
||||
{
|
||||
node* list = head_;
|
||||
|
||||
if (list == 0) return 0;
|
||||
|
||||
for (; list; list = list->next_)
|
||||
if (list->value_ == t)
|
||||
return list;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
template<typename T>
|
||||
bool list<T>::remove(T t)
|
||||
{
|
||||
node* del = look_up(t);
|
||||
|
||||
if (del == 0)
|
||||
return false;
|
||||
else if (del == head_)
|
||||
pop_front();
|
||||
else if (del == tail_)
|
||||
pop_back();
|
||||
else {
|
||||
del->prev_->next_ = del->next_;
|
||||
del->next_->prev_ = del->prev_;
|
||||
|
||||
destroy(del);
|
||||
FreeMemory(del);
|
||||
--sz_;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
template<typename T>
|
||||
bool list<T>::erase(iterator iter)
|
||||
{
|
||||
node* del = iter.current_;
|
||||
|
||||
if (del == 0)
|
||||
return false;
|
||||
else if (del == head_)
|
||||
pop_front();
|
||||
else if (del == tail_)
|
||||
pop_back();
|
||||
else {
|
||||
del->prev_->next_ = del->next_;
|
||||
del->next_->prev_ = del->prev_;
|
||||
|
||||
destroy(del);
|
||||
FreeMemory(del);
|
||||
--sz_;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
|
||||
} // namespace mySTL
|
||||
|
||||
#endif // mySTL_LIST_HPP
|
||||
@@ -0,0 +1,136 @@
|
||||
/*
|
||||
Copyright (C) 2000-2007 MySQL AB
|
||||
|
||||
This program is free software; you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation; version 2 of the License.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program; see the file COPYING. If not, write to the
|
||||
Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
|
||||
MA 02110-1301 USA.
|
||||
*/
|
||||
|
||||
|
||||
/* mySTL memory implements auto_ptr
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef mySTL_MEMORY_HPP
|
||||
#define mySTL_MEMORY_HPP
|
||||
|
||||
#include "memory_array.hpp" // for auto_array
|
||||
|
||||
#ifdef _MSC_VER
|
||||
// disable operator-> warning for builtins
|
||||
#pragma warning(disable:4284)
|
||||
#endif
|
||||
|
||||
|
||||
namespace mySTL {
|
||||
|
||||
|
||||
template<typename T>
|
||||
struct auto_ptr_ref {
|
||||
T* ptr_;
|
||||
explicit auto_ptr_ref(T* p) : ptr_(p) {}
|
||||
};
|
||||
|
||||
|
||||
template<typename T>
|
||||
class auto_ptr {
|
||||
T* ptr_;
|
||||
|
||||
void Destroy()
|
||||
{
|
||||
#ifdef YASSL_LIB
|
||||
yaSSL::ysDelete(ptr_);
|
||||
#else
|
||||
TaoCrypt::tcDelete(ptr_);
|
||||
#endif
|
||||
}
|
||||
public:
|
||||
explicit auto_ptr(T* p = 0) : ptr_(p) {}
|
||||
|
||||
~auto_ptr()
|
||||
{
|
||||
Destroy();
|
||||
}
|
||||
|
||||
|
||||
auto_ptr(auto_ptr& other) : ptr_(other.release()) {}
|
||||
|
||||
auto_ptr& operator=(auto_ptr& that)
|
||||
{
|
||||
if (this != &that) {
|
||||
Destroy();
|
||||
ptr_ = that.release();
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
T* operator->() const
|
||||
{
|
||||
return ptr_;
|
||||
}
|
||||
|
||||
T& operator*() const
|
||||
{
|
||||
return *ptr_;
|
||||
}
|
||||
|
||||
T* get() const
|
||||
{
|
||||
return ptr_;
|
||||
}
|
||||
|
||||
T* release()
|
||||
{
|
||||
T* tmp = ptr_;
|
||||
ptr_ = 0;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
void reset(T* p = 0)
|
||||
{
|
||||
if (ptr_ != p) {
|
||||
Destroy();
|
||||
ptr_ = p;
|
||||
}
|
||||
}
|
||||
|
||||
// auto_ptr_ref conversions
|
||||
auto_ptr(auto_ptr_ref<T> ref) : ptr_(ref.ptr_) {}
|
||||
|
||||
auto_ptr& operator=(auto_ptr_ref<T> ref)
|
||||
{
|
||||
if (this->ptr_ != ref.ptr_) {
|
||||
Destroy();
|
||||
ptr_ = ref.ptr_;
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
template<typename T2>
|
||||
operator auto_ptr<T2>()
|
||||
{
|
||||
return auto_ptr<T2>(this->release());
|
||||
}
|
||||
|
||||
template<typename T2>
|
||||
operator auto_ptr_ref<T2>()
|
||||
{
|
||||
return auto_ptr_ref<T2>(this->release());
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
} // namespace mySTL
|
||||
|
||||
#endif // mySTL_MEMORY_HPP
|
||||
@@ -0,0 +1,135 @@
|
||||
/*
|
||||
Copyright (C) 2000-2007 MySQL AB
|
||||
|
||||
This program is free software; you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation; version 2 of the License.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program; see the file COPYING. If not, write to the
|
||||
Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
|
||||
MA 02110-1301 USA.
|
||||
*/
|
||||
|
||||
|
||||
/* mySTL memory_arry implements auto_array
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef mySTL_MEMORY_ARRAY_HPP
|
||||
#define mySTL_MEMORY_ARRAY_HPP
|
||||
|
||||
|
||||
#ifdef _MSC_VER
|
||||
// disable operator-> warning for builtins
|
||||
#pragma warning(disable:4284)
|
||||
#endif
|
||||
|
||||
|
||||
namespace mySTL {
|
||||
|
||||
|
||||
template<typename T>
|
||||
struct auto_array_ref {
|
||||
T* ptr_;
|
||||
explicit auto_array_ref(T* p) : ptr_(p) {}
|
||||
};
|
||||
|
||||
|
||||
template<typename T>
|
||||
class auto_array {
|
||||
T* ptr_;
|
||||
|
||||
void Destroy()
|
||||
{
|
||||
#ifdef YASSL_LIB
|
||||
yaSSL::ysArrayDelete(ptr_);
|
||||
#else
|
||||
TaoCrypt::tcArrayDelete(ptr_);
|
||||
#endif
|
||||
}
|
||||
public:
|
||||
explicit auto_array(T* p = 0) : ptr_(p) {}
|
||||
|
||||
~auto_array()
|
||||
{
|
||||
Destroy();
|
||||
}
|
||||
|
||||
|
||||
auto_array(auto_array& other) : ptr_(other.release()) {}
|
||||
|
||||
auto_array& operator=(auto_array& that)
|
||||
{
|
||||
if (this != &that) {
|
||||
Destroy();
|
||||
ptr_ = that.release();
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
T* operator->() const
|
||||
{
|
||||
return ptr_;
|
||||
}
|
||||
|
||||
T& operator*() const
|
||||
{
|
||||
return *ptr_;
|
||||
}
|
||||
|
||||
T* get() const
|
||||
{
|
||||
return ptr_;
|
||||
}
|
||||
|
||||
T* release()
|
||||
{
|
||||
T* tmp = ptr_;
|
||||
ptr_ = 0;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
void reset(T* p = 0)
|
||||
{
|
||||
if (ptr_ != p) {
|
||||
Destroy();
|
||||
ptr_ = p;
|
||||
}
|
||||
}
|
||||
|
||||
// auto_array_ref conversions
|
||||
auto_array(auto_array_ref<T> ref) : ptr_(ref.ptr_) {}
|
||||
|
||||
auto_array& operator=(auto_array_ref<T> ref)
|
||||
{
|
||||
if (this->ptr_ != ref.ptr_) {
|
||||
Destroy();
|
||||
ptr_ = ref.ptr_;
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
template<typename T2>
|
||||
operator auto_array<T2>()
|
||||
{
|
||||
return auto_array<T2>(this->release());
|
||||
}
|
||||
|
||||
template<typename T2>
|
||||
operator auto_array_ref<T2>()
|
||||
{
|
||||
return auto_array_ref<T2>(this->release());
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
} // namespace mySTL
|
||||
|
||||
#endif // mySTL_MEMORY_ARRAY_HPP
|
||||
@@ -0,0 +1,58 @@
|
||||
/*
|
||||
Copyright (C) 2000-2007 MySQL AB
|
||||
|
||||
This program is free software; you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation; version 2 of the License.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program; see the file COPYING. If not, write to the
|
||||
Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
|
||||
MA 02110-1301 USA.
|
||||
*/
|
||||
|
||||
|
||||
/* mySTL pair implements pair
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef mySTL_PAIR_HPP
|
||||
#define mySTL_PAIR_HPP
|
||||
|
||||
|
||||
|
||||
namespace mySTL {
|
||||
|
||||
|
||||
template<typename T1, typename T2>
|
||||
struct pair {
|
||||
typedef T1 first_type;
|
||||
typedef T2 second_type;
|
||||
|
||||
first_type first;
|
||||
second_type second;
|
||||
|
||||
pair() {}
|
||||
pair(const T1& t1, const T2& t2) : first(t1), second(t2) {}
|
||||
|
||||
template<typename U1, typename U2>
|
||||
pair(const pair<U1, U2>& p) : first(p.first), second(p.second) {}
|
||||
};
|
||||
|
||||
|
||||
template<typename T1, typename T2>
|
||||
inline pair<T1, T2> make_pair(const T1& a, const T2& b)
|
||||
{
|
||||
return pair<T1, T2>(a, b);
|
||||
}
|
||||
|
||||
|
||||
|
||||
} // namespace mySTL
|
||||
|
||||
#endif // mySTL_PAIR_HPP
|
||||
@@ -0,0 +1,77 @@
|
||||
/*
|
||||
Copyright (C) 2000-2007 MySQL AB
|
||||
|
||||
This program is free software; you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation; version 2 of the License.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program; see the file COPYING. If not, write to the
|
||||
Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
|
||||
MA 02110-1301 USA.
|
||||
*/
|
||||
|
||||
|
||||
/* mySTL memory implements exception, runtime_error
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef mySTL_STDEXCEPT_HPP
|
||||
#define mySTL_STDEXCEPT_HPP
|
||||
|
||||
|
||||
#include <string.h> // strncpy
|
||||
#include <assert.h> // assert
|
||||
#include <stdlib.h> // size_t
|
||||
|
||||
|
||||
namespace mySTL {
|
||||
|
||||
|
||||
class exception {
|
||||
public:
|
||||
exception() {}
|
||||
virtual ~exception() {} // to shut up compiler warnings
|
||||
|
||||
virtual const char* what() const { return ""; }
|
||||
|
||||
// for compiler generated call, never used
|
||||
static void operator delete(void*) { assert(0); }
|
||||
private:
|
||||
// don't allow dynamic creation of exceptions
|
||||
static void* operator new(size_t);
|
||||
};
|
||||
|
||||
|
||||
class named_exception : public exception {
|
||||
public:
|
||||
enum { NAME_SIZE = 80 };
|
||||
|
||||
explicit named_exception(const char* str)
|
||||
{
|
||||
strncpy(name_, str, NAME_SIZE);
|
||||
name_[NAME_SIZE - 1] = 0;
|
||||
}
|
||||
|
||||
virtual const char* what() const { return name_; }
|
||||
private:
|
||||
char name_[NAME_SIZE];
|
||||
};
|
||||
|
||||
|
||||
class runtime_error : public named_exception {
|
||||
public:
|
||||
explicit runtime_error(const char* str) : named_exception(str) {}
|
||||
};
|
||||
|
||||
|
||||
|
||||
|
||||
} // namespace mySTL
|
||||
|
||||
#endif // mySTL_STDEXCEPT_HPP
|
||||
@@ -0,0 +1,154 @@
|
||||
/*
|
||||
Copyright (C) 2000-2007 MySQL AB
|
||||
|
||||
This program is free software; you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation; version 2 of the License.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program; see the file COPYING. If not, write to the
|
||||
Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
|
||||
MA 02110-1301 USA.
|
||||
*/
|
||||
|
||||
|
||||
/* mySTL vector implements simple vector, w/ swap
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef mySTL_VECTOR_HPP
|
||||
#define mySTL_VECTOR_HPP
|
||||
|
||||
#include "helpers.hpp" // construct, destory, fill, etc.
|
||||
#include "algorithm.hpp" // swap
|
||||
#include <assert.h> // assert
|
||||
|
||||
|
||||
namespace mySTL {
|
||||
|
||||
|
||||
template <typename T>
|
||||
struct vector_base {
|
||||
T* start_;
|
||||
T* finish_;
|
||||
T* end_of_storage_;
|
||||
|
||||
vector_base() : start_(0), finish_(0), end_of_storage_(0) {}
|
||||
vector_base(size_t n)
|
||||
{
|
||||
start_ = GetArrayMemory<T>(n);
|
||||
finish_ = start_;
|
||||
end_of_storage_ = start_ + n;
|
||||
}
|
||||
|
||||
~vector_base()
|
||||
{
|
||||
FreeArrayMemory(start_);
|
||||
}
|
||||
|
||||
void Swap(vector_base& that)
|
||||
{
|
||||
swap(start_, that.start_);
|
||||
swap(finish_, that.finish_);
|
||||
swap(end_of_storage_, that.end_of_storage_);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
|
||||
template <typename T>
|
||||
class vector {
|
||||
public:
|
||||
typedef T* iterator;
|
||||
typedef const T* const_iterator;
|
||||
|
||||
vector() {}
|
||||
explicit vector(size_t n) : vec_(n)
|
||||
{
|
||||
vec_.finish_ = uninit_fill_n(vec_.start_, n, T());
|
||||
}
|
||||
|
||||
~vector() { destroy(vec_.start_, vec_.finish_); }
|
||||
|
||||
vector(const vector& other) : vec_(other.size())
|
||||
{
|
||||
vec_.finish_ = uninit_copy(other.vec_.start_, other.vec_.finish_,
|
||||
vec_.start_);
|
||||
}
|
||||
|
||||
size_t capacity() const { return vec_.end_of_storage_ - vec_.start_; }
|
||||
|
||||
size_t size() const { return vec_.finish_ - vec_.start_; }
|
||||
|
||||
T& operator[](size_t idx) { return *(vec_.start_ + idx); }
|
||||
const T& operator[](size_t idx) const { return *(vec_.start_ + idx); }
|
||||
|
||||
const T* begin() const { return vec_.start_; }
|
||||
const T* end() const { return vec_.finish_; }
|
||||
|
||||
void push_back(const T& v)
|
||||
{
|
||||
if (vec_.finish_ != vec_.end_of_storage_) {
|
||||
construct(vec_.finish_, v);
|
||||
++vec_.finish_;
|
||||
}
|
||||
else {
|
||||
vector tmp(size() * 2 + 1, *this);
|
||||
construct(tmp.vec_.finish_, v);
|
||||
++tmp.vec_.finish_;
|
||||
Swap(tmp);
|
||||
}
|
||||
}
|
||||
|
||||
void resize(size_t n, const T& v)
|
||||
{
|
||||
if (n == size()) return;
|
||||
|
||||
if (n < size()) {
|
||||
T* first = vec_.start_ + n;
|
||||
destroy(first, vec_.finish_);
|
||||
vec_.finish_ -= vec_.finish_ - first;
|
||||
}
|
||||
else {
|
||||
vector tmp(n, *this);
|
||||
tmp.vec_.finish_ = uninit_fill_n(tmp.vec_.finish_, n - size(), v);
|
||||
Swap(tmp);
|
||||
}
|
||||
}
|
||||
|
||||
void reserve(size_t n)
|
||||
{
|
||||
if (capacity() < n) {
|
||||
vector tmp(n, *this);
|
||||
Swap(tmp);
|
||||
}
|
||||
}
|
||||
|
||||
void Swap(vector& that)
|
||||
{
|
||||
vec_.Swap(that.vec_);
|
||||
}
|
||||
private:
|
||||
vector_base<T> vec_;
|
||||
|
||||
vector& operator=(const vector&); // hide assign
|
||||
|
||||
// for growing, n must be bigger than other size
|
||||
vector(size_t n, const vector& other) : vec_(n)
|
||||
{
|
||||
assert(n > other.size());
|
||||
vec_.finish_ = uninit_copy(other.vec_.start_, other.vec_.finish_,
|
||||
vec_.start_);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
|
||||
} // namespace mySTL
|
||||
|
||||
#endif // mySTL_VECTOR_HPP
|
||||
+1814
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,38 @@
|
||||
/*
|
||||
Copyright (C) 2000-2007 MySQL AB
|
||||
|
||||
This program is free software; you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation; version 2 of the License.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program; see the file COPYING. If not, write to the
|
||||
Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
|
||||
MA 02110-1301 USA.
|
||||
*/
|
||||
|
||||
/* based on Wei Dai's aestables.cpp from CryptoPP */
|
||||
|
||||
#include "runtime.hpp"
|
||||
#include "aes.hpp"
|
||||
|
||||
|
||||
namespace TaoCrypt {
|
||||
|
||||
|
||||
const word32 AES::rcon_[] = {
|
||||
0x01000000, 0x02000000, 0x04000000, 0x08000000,
|
||||
0x10000000, 0x20000000, 0x40000000, 0x80000000,
|
||||
0x1B000000, 0x36000000,
|
||||
/* for 128-bit blocks, Rijndael never uses more than 10 rcon values */
|
||||
};
|
||||
|
||||
|
||||
|
||||
|
||||
} // namespace
|
||||
@@ -0,0 +1,339 @@
|
||||
/*
|
||||
Copyright (C) 2000-2007 MySQL AB
|
||||
|
||||
This program is free software; you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation; version 2 of the License.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program; see the file COPYING. If not, write to the
|
||||
Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
|
||||
MA 02110-1301 USA.
|
||||
*/
|
||||
|
||||
/* based on Wei Dai's algebra.cpp from CryptoPP */
|
||||
#undef NDEBUG
|
||||
#define DEBUG // GCC 4.0 bug if NDEBUG and Optimize > 1
|
||||
|
||||
#include "runtime.hpp"
|
||||
#include "algebra.hpp"
|
||||
#ifdef USE_SYS_STL
|
||||
#include <vector>
|
||||
#else
|
||||
#include "vector.hpp"
|
||||
#endif
|
||||
|
||||
|
||||
namespace STL = STL_NAMESPACE;
|
||||
|
||||
|
||||
namespace TaoCrypt {
|
||||
|
||||
|
||||
const Integer& AbstractGroup::Double(const Element &a) const
|
||||
{
|
||||
return Add(a, a);
|
||||
}
|
||||
|
||||
const Integer& AbstractGroup::Subtract(const Element &a, const Element &b) const
|
||||
{
|
||||
// make copy of a in case Inverse() overwrites it
|
||||
Element a1(a);
|
||||
return Add(a1, Inverse(b));
|
||||
}
|
||||
|
||||
Integer& AbstractGroup::Accumulate(Element &a, const Element &b) const
|
||||
{
|
||||
return a = Add(a, b);
|
||||
}
|
||||
|
||||
Integer& AbstractGroup::Reduce(Element &a, const Element &b) const
|
||||
{
|
||||
return a = Subtract(a, b);
|
||||
}
|
||||
|
||||
const Integer& AbstractRing::Square(const Element &a) const
|
||||
{
|
||||
return Multiply(a, a);
|
||||
}
|
||||
|
||||
|
||||
const Integer& AbstractRing::Divide(const Element &a, const Element &b) const
|
||||
{
|
||||
// make copy of a in case MultiplicativeInverse() overwrites it
|
||||
Element a1(a);
|
||||
return Multiply(a1, MultiplicativeInverse(b));
|
||||
}
|
||||
|
||||
|
||||
const Integer& AbstractEuclideanDomain::Mod(const Element &a,
|
||||
const Element &b) const
|
||||
{
|
||||
Element q;
|
||||
DivisionAlgorithm(result, q, a, b);
|
||||
return result;
|
||||
}
|
||||
|
||||
const Integer& AbstractEuclideanDomain::Gcd(const Element &a,
|
||||
const Element &b) const
|
||||
{
|
||||
STL::vector<Element> g(3);
|
||||
g[0]= b;
|
||||
g[1]= a;
|
||||
unsigned int i0=0, i1=1, i2=2;
|
||||
|
||||
while (!Equal(g[i1], this->Identity()))
|
||||
{
|
||||
g[i2] = Mod(g[i0], g[i1]);
|
||||
unsigned int t = i0; i0 = i1; i1 = i2; i2 = t;
|
||||
}
|
||||
|
||||
return result = g[i0];
|
||||
}
|
||||
|
||||
|
||||
Integer AbstractGroup::ScalarMultiply(const Element &base,
|
||||
const Integer &exponent) const
|
||||
{
|
||||
Element result;
|
||||
SimultaneousMultiply(&result, base, &exponent, 1);
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
Integer AbstractGroup::CascadeScalarMultiply(const Element &x,
|
||||
const Integer &e1, const Element &y, const Integer &e2) const
|
||||
{
|
||||
const unsigned expLen = max(e1.BitCount(), e2.BitCount());
|
||||
if (expLen==0)
|
||||
return Identity();
|
||||
|
||||
const unsigned w = (expLen <= 46 ? 1 : (expLen <= 260 ? 2 : 3));
|
||||
const unsigned tableSize = 1<<w;
|
||||
STL::vector<Element> powerTable(tableSize << w);
|
||||
|
||||
powerTable[1] = x;
|
||||
powerTable[tableSize] = y;
|
||||
if (w==1)
|
||||
powerTable[3] = Add(x,y);
|
||||
else
|
||||
{
|
||||
powerTable[2] = Double(x);
|
||||
powerTable[2*tableSize] = Double(y);
|
||||
|
||||
unsigned i, j;
|
||||
|
||||
for (i=3; i<tableSize; i+=2)
|
||||
powerTable[i] = Add(powerTable[i-2], powerTable[2]);
|
||||
for (i=1; i<tableSize; i+=2)
|
||||
for (j=i+tableSize; j<(tableSize<<w); j+=tableSize)
|
||||
powerTable[j] = Add(powerTable[j-tableSize], y);
|
||||
|
||||
for (i=3*tableSize; i<(tableSize<<w); i+=2*tableSize)
|
||||
powerTable[i] = Add(powerTable[i-2*tableSize],
|
||||
powerTable[2*tableSize]);
|
||||
for (i=tableSize; i<(tableSize<<w); i+=2*tableSize)
|
||||
for (j=i+2; j<i+tableSize; j+=2)
|
||||
powerTable[j] = Add(powerTable[j-1], x);
|
||||
}
|
||||
|
||||
Element result;
|
||||
unsigned power1 = 0, power2 = 0, prevPosition = expLen-1;
|
||||
bool firstTime = true;
|
||||
|
||||
for (int i = expLen-1; i>=0; i--)
|
||||
{
|
||||
power1 = 2*power1 + e1.GetBit(i);
|
||||
power2 = 2*power2 + e2.GetBit(i);
|
||||
|
||||
if (i==0 || 2*power1 >= tableSize || 2*power2 >= tableSize)
|
||||
{
|
||||
unsigned squaresBefore = prevPosition-i;
|
||||
unsigned squaresAfter = 0;
|
||||
prevPosition = i;
|
||||
while ((power1 || power2) && power1%2 == 0 && power2%2==0)
|
||||
{
|
||||
power1 /= 2;
|
||||
power2 /= 2;
|
||||
squaresBefore--;
|
||||
squaresAfter++;
|
||||
}
|
||||
if (firstTime)
|
||||
{
|
||||
result = powerTable[(power2<<w) + power1];
|
||||
firstTime = false;
|
||||
}
|
||||
else
|
||||
{
|
||||
while (squaresBefore--)
|
||||
result = Double(result);
|
||||
if (power1 || power2)
|
||||
Accumulate(result, powerTable[(power2<<w) + power1]);
|
||||
}
|
||||
while (squaresAfter--)
|
||||
result = Double(result);
|
||||
power1 = power2 = 0;
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
struct WindowSlider
|
||||
{
|
||||
WindowSlider(const Integer &exp, bool fastNegate,
|
||||
unsigned int windowSizeIn=0)
|
||||
: exp(exp), windowModulus(Integer::One()), windowSize(windowSizeIn),
|
||||
windowBegin(0), fastNegate(fastNegate), firstTime(true),
|
||||
finished(false)
|
||||
{
|
||||
if (windowSize == 0)
|
||||
{
|
||||
unsigned int expLen = exp.BitCount();
|
||||
windowSize = expLen <= 17 ? 1 : (expLen <= 24 ? 2 :
|
||||
(expLen <= 70 ? 3 : (expLen <= 197 ? 4 : (expLen <= 539 ? 5 :
|
||||
(expLen <= 1434 ? 6 : 7)))));
|
||||
}
|
||||
windowModulus <<= windowSize;
|
||||
}
|
||||
|
||||
void FindNextWindow()
|
||||
{
|
||||
unsigned int expLen = exp.WordCount() * WORD_BITS;
|
||||
unsigned int skipCount = firstTime ? 0 : windowSize;
|
||||
firstTime = false;
|
||||
while (!exp.GetBit(skipCount))
|
||||
{
|
||||
if (skipCount >= expLen)
|
||||
{
|
||||
finished = true;
|
||||
return;
|
||||
}
|
||||
skipCount++;
|
||||
}
|
||||
|
||||
exp >>= skipCount;
|
||||
windowBegin += skipCount;
|
||||
expWindow = exp % (1 << windowSize);
|
||||
|
||||
if (fastNegate && exp.GetBit(windowSize))
|
||||
{
|
||||
negateNext = true;
|
||||
expWindow = (1 << windowSize) - expWindow;
|
||||
exp += windowModulus;
|
||||
}
|
||||
else
|
||||
negateNext = false;
|
||||
}
|
||||
|
||||
Integer exp, windowModulus;
|
||||
unsigned int windowSize, windowBegin, expWindow;
|
||||
bool fastNegate, negateNext, firstTime, finished;
|
||||
};
|
||||
|
||||
|
||||
void AbstractGroup::SimultaneousMultiply(Integer *results, const Integer &base,
|
||||
const Integer *expBegin, unsigned int expCount) const
|
||||
{
|
||||
STL::vector<STL::vector<Element> > buckets(expCount);
|
||||
STL::vector<WindowSlider> exponents;
|
||||
exponents.reserve(expCount);
|
||||
unsigned int i;
|
||||
|
||||
for (i=0; i<expCount; i++)
|
||||
{
|
||||
assert(expBegin->NotNegative());
|
||||
exponents.push_back(WindowSlider(*expBegin++, InversionIsFast(), 0));
|
||||
exponents[i].FindNextWindow();
|
||||
buckets[i].resize(1<<(exponents[i].windowSize-1), Identity());
|
||||
}
|
||||
|
||||
unsigned int expBitPosition = 0;
|
||||
Element g = base;
|
||||
bool notDone = true;
|
||||
|
||||
while (notDone)
|
||||
{
|
||||
notDone = false;
|
||||
for (i=0; i<expCount; i++)
|
||||
{
|
||||
if (!exponents[i].finished && expBitPosition ==
|
||||
exponents[i].windowBegin)
|
||||
{
|
||||
Element &bucket = buckets[i][exponents[i].expWindow/2];
|
||||
if (exponents[i].negateNext)
|
||||
Accumulate(bucket, Inverse(g));
|
||||
else
|
||||
Accumulate(bucket, g);
|
||||
exponents[i].FindNextWindow();
|
||||
}
|
||||
notDone = notDone || !exponents[i].finished;
|
||||
}
|
||||
|
||||
if (notDone)
|
||||
{
|
||||
g = Double(g);
|
||||
expBitPosition++;
|
||||
}
|
||||
}
|
||||
|
||||
for (i=0; i<expCount; i++)
|
||||
{
|
||||
Element &r = *results++;
|
||||
r = buckets[i][buckets[i].size()-1];
|
||||
if (buckets[i].size() > 1)
|
||||
{
|
||||
for (int j = buckets[i].size()-2; j >= 1; j--)
|
||||
{
|
||||
Accumulate(buckets[i][j], buckets[i][j+1]);
|
||||
Accumulate(r, buckets[i][j]);
|
||||
}
|
||||
Accumulate(buckets[i][0], buckets[i][1]);
|
||||
r = Add(Double(r), buckets[i][0]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Integer AbstractRing::Exponentiate(const Element &base,
|
||||
const Integer &exponent) const
|
||||
{
|
||||
Element result;
|
||||
SimultaneousExponentiate(&result, base, &exponent, 1);
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
Integer AbstractRing::CascadeExponentiate(const Element &x,
|
||||
const Integer &e1, const Element &y, const Integer &e2) const
|
||||
{
|
||||
return MultiplicativeGroup().AbstractGroup::CascadeScalarMultiply(
|
||||
x, e1, y, e2);
|
||||
}
|
||||
|
||||
|
||||
void AbstractRing::SimultaneousExponentiate(Integer *results,
|
||||
const Integer &base,
|
||||
const Integer *exponents, unsigned int expCount) const
|
||||
{
|
||||
MultiplicativeGroup().AbstractGroup::SimultaneousMultiply(results, base,
|
||||
exponents, expCount);
|
||||
}
|
||||
|
||||
|
||||
} // namespace
|
||||
|
||||
|
||||
#ifdef HAVE_EXPLICIT_TEMPLATE_INSTANTIATION
|
||||
namespace mySTL {
|
||||
template TaoCrypt::WindowSlider* uninit_copy<TaoCrypt::WindowSlider*, TaoCrypt::WindowSlider*>(TaoCrypt::WindowSlider*, TaoCrypt::WindowSlider*, TaoCrypt::WindowSlider*);
|
||||
template void destroy<TaoCrypt::WindowSlider*>(TaoCrypt::WindowSlider*, TaoCrypt::WindowSlider*);
|
||||
template TaoCrypt::WindowSlider* GetArrayMemory<TaoCrypt::WindowSlider>(size_t);
|
||||
template void FreeArrayMemory<TaoCrypt::WindowSlider>(TaoCrypt::WindowSlider*);
|
||||
}
|
||||
#endif
|
||||
|
||||
+235
@@ -0,0 +1,235 @@
|
||||
/*
|
||||
Copyright (C) 2000-2007 MySQL AB
|
||||
|
||||
This program is free software; you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation; version 2 of the License.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program; see the file COPYING. If not, write to the
|
||||
Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
|
||||
MA 02110-1301 USA.
|
||||
*/
|
||||
|
||||
/* based on Wei Dai's arc4.cpp from CryptoPP */
|
||||
|
||||
#include "runtime.hpp"
|
||||
#include "arc4.hpp"
|
||||
|
||||
|
||||
#if defined(TAOCRYPT_X86ASM_AVAILABLE) && defined(TAO_ASM)
|
||||
#define DO_ARC4_ASM
|
||||
#endif
|
||||
|
||||
|
||||
namespace TaoCrypt {
|
||||
|
||||
void ARC4::SetKey(const byte* key, word32 length)
|
||||
{
|
||||
x_ = 1;
|
||||
y_ = 0;
|
||||
|
||||
word32 i;
|
||||
|
||||
for (i = 0; i < STATE_SIZE; i++)
|
||||
state_[i] = i;
|
||||
|
||||
word32 keyIndex = 0, stateIndex = 0;
|
||||
|
||||
for (i = 0; i < STATE_SIZE; i++) {
|
||||
word32 a = state_[i];
|
||||
stateIndex += key[keyIndex] + a;
|
||||
stateIndex &= 0xFF;
|
||||
state_[i] = state_[stateIndex];
|
||||
state_[stateIndex] = a;
|
||||
|
||||
if (++keyIndex >= length)
|
||||
keyIndex = 0;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// local
|
||||
namespace {
|
||||
|
||||
inline unsigned int MakeByte(word32& x, word32& y, byte* s)
|
||||
{
|
||||
word32 a = s[x];
|
||||
y = (y+a) & 0xff;
|
||||
|
||||
word32 b = s[y];
|
||||
s[x] = b;
|
||||
s[y] = a;
|
||||
x = (x+1) & 0xff;
|
||||
|
||||
return s[(a+b) & 0xff];
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
|
||||
|
||||
void ARC4::Process(byte* out, const byte* in, word32 length)
|
||||
{
|
||||
if (length == 0) return;
|
||||
|
||||
#ifdef DO_ARC4_ASM
|
||||
if (isMMX) {
|
||||
AsmProcess(out, in, length);
|
||||
return;
|
||||
}
|
||||
#endif
|
||||
|
||||
byte *const s = state_;
|
||||
word32 x = x_;
|
||||
word32 y = y_;
|
||||
|
||||
if (in == out)
|
||||
while (length--)
|
||||
*out++ ^= MakeByte(x, y, s);
|
||||
else
|
||||
while(length--)
|
||||
*out++ = *in++ ^ MakeByte(x, y, s);
|
||||
x_ = x;
|
||||
y_ = y;
|
||||
}
|
||||
|
||||
|
||||
|
||||
#ifdef DO_ARC4_ASM
|
||||
|
||||
#ifdef _MSC_VER
|
||||
__declspec(naked)
|
||||
#else
|
||||
__attribute__ ((noinline))
|
||||
#endif
|
||||
void ARC4::AsmProcess(byte* out, const byte* in, word32 length)
|
||||
{
|
||||
#ifdef __GNUC__
|
||||
#define AS1(x) asm(#x);
|
||||
#define AS2(x, y) asm(#x ", " #y);
|
||||
|
||||
#define PROLOG() \
|
||||
asm(".intel_syntax noprefix"); \
|
||||
AS2( movd mm3, edi ) \
|
||||
AS2( movd mm4, ebx ) \
|
||||
AS2( movd mm5, esi ) \
|
||||
AS2( movd mm6, ebp ) \
|
||||
AS2( mov ecx, DWORD PTR [ebp + 8] ) \
|
||||
AS2( mov edi, DWORD PTR [ebp + 12] ) \
|
||||
AS2( mov esi, DWORD PTR [ebp + 16] ) \
|
||||
AS2( mov ebp, DWORD PTR [ebp + 20] )
|
||||
|
||||
#define EPILOG() \
|
||||
AS2( movd ebp, mm6 ) \
|
||||
AS2( movd esi, mm5 ) \
|
||||
AS2( movd ebx, mm4 ) \
|
||||
AS2( mov esp, ebp ) \
|
||||
AS2( movd edi, mm3 ) \
|
||||
AS1( emms ) \
|
||||
asm(".att_syntax");
|
||||
#else
|
||||
#define AS1(x) __asm x
|
||||
#define AS2(x, y) __asm x, y
|
||||
|
||||
#define PROLOG() \
|
||||
AS1( push ebp ) \
|
||||
AS2( mov ebp, esp ) \
|
||||
AS2( movd mm3, edi ) \
|
||||
AS2( movd mm4, ebx ) \
|
||||
AS2( movd mm5, esi ) \
|
||||
AS2( movd mm6, ebp ) \
|
||||
AS2( mov edi, DWORD PTR [ebp + 8] ) \
|
||||
AS2( mov esi, DWORD PTR [ebp + 12] ) \
|
||||
AS2( mov ebp, DWORD PTR [ebp + 16] )
|
||||
|
||||
#define EPILOG() \
|
||||
AS2( movd ebp, mm6 ) \
|
||||
AS2( movd esi, mm5 ) \
|
||||
AS2( movd ebx, mm4 ) \
|
||||
AS2( movd edi, mm3 ) \
|
||||
AS2( mov esp, ebp ) \
|
||||
AS1( pop ebp ) \
|
||||
AS1( emms ) \
|
||||
AS1( ret 12 )
|
||||
|
||||
#endif
|
||||
|
||||
PROLOG()
|
||||
|
||||
AS2( sub esp, 4 ) // make room
|
||||
|
||||
AS2( cmp ebp, 0 )
|
||||
AS1( jz nothing )
|
||||
|
||||
AS2( mov [esp], ebp ) // length
|
||||
|
||||
AS2( movzx edx, BYTE PTR [ecx + 1] ) // y
|
||||
AS2( lea ebp, [ecx + 2] ) // state_
|
||||
AS2( movzx ecx, BYTE PTR [ecx] ) // x
|
||||
|
||||
// setup loop
|
||||
// a = s[x];
|
||||
AS2( movzx eax, BYTE PTR [ebp + ecx] )
|
||||
|
||||
|
||||
AS1( begin: )
|
||||
|
||||
// y = (y+a) & 0xff;
|
||||
AS2( add edx, eax )
|
||||
AS2( and edx, 255 )
|
||||
|
||||
// b = s[y];
|
||||
AS2( movzx ebx, BYTE PTR [ebp + edx] )
|
||||
|
||||
// s[x] = b;
|
||||
AS2( mov [ebp + ecx], bl )
|
||||
|
||||
// s[y] = a;
|
||||
AS2( mov [ebp + edx], al )
|
||||
|
||||
// x = (x+1) & 0xff;
|
||||
AS1( inc ecx )
|
||||
AS2( and ecx, 255 )
|
||||
|
||||
//return s[(a+b) & 0xff];
|
||||
AS2( add eax, ebx )
|
||||
AS2( and eax, 255 )
|
||||
|
||||
AS2( movzx ebx, BYTE PTR [ebp + eax] )
|
||||
|
||||
// a = s[x]; for next round
|
||||
AS2( movzx eax, BYTE PTR [ebp + ecx] )
|
||||
|
||||
// xOr w/ inByte
|
||||
AS2( xor bl, BYTE PTR [esi] )
|
||||
AS1( inc esi )
|
||||
|
||||
// write to outByte
|
||||
AS2( mov [edi], bl )
|
||||
AS1( inc edi )
|
||||
|
||||
AS1( dec DWORD PTR [esp] )
|
||||
AS1( jnz begin )
|
||||
|
||||
|
||||
// write back to x_ and y_
|
||||
AS2( mov [ebp - 2], cl )
|
||||
AS2( mov [ebp - 1], dl )
|
||||
|
||||
|
||||
AS1( nothing: )
|
||||
|
||||
|
||||
EPILOG()
|
||||
}
|
||||
|
||||
#endif // DO_ARC4_ASM
|
||||
|
||||
|
||||
} // namespace
|
||||
+1185
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,303 @@
|
||||
/*
|
||||
Copyright (C) 2000-2007 MySQL AB
|
||||
|
||||
This program is free software; you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation; version 2 of the License.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program; see the file COPYING. If not, write to the
|
||||
Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
|
||||
MA 02110-1301 USA.
|
||||
*/
|
||||
|
||||
/* based on Wei Dai's bfinit.cpp from CryptoPP */
|
||||
|
||||
#include "runtime.hpp"
|
||||
#include "blowfish.hpp"
|
||||
|
||||
|
||||
namespace TaoCrypt {
|
||||
|
||||
const word32 Blowfish::p_init_[Blowfish::ROUNDS+2] =
|
||||
{
|
||||
608135816U, 2242054355U, 320440878U, 57701188U,
|
||||
2752067618U, 698298832U, 137296536U, 3964562569U,
|
||||
1160258022U, 953160567U, 3193202383U, 887688300U,
|
||||
3232508343U, 3380367581U, 1065670069U, 3041331479U,
|
||||
2450970073U, 2306472731U
|
||||
} ;
|
||||
|
||||
|
||||
const word32 Blowfish::s_init_[4*256] = {
|
||||
3509652390U, 2564797868U, 805139163U, 3491422135U,
|
||||
3101798381U, 1780907670U, 3128725573U, 4046225305U,
|
||||
614570311U, 3012652279U, 134345442U, 2240740374U,
|
||||
1667834072U, 1901547113U, 2757295779U, 4103290238U,
|
||||
227898511U, 1921955416U, 1904987480U, 2182433518U,
|
||||
2069144605U, 3260701109U, 2620446009U, 720527379U,
|
||||
3318853667U, 677414384U, 3393288472U, 3101374703U,
|
||||
2390351024U, 1614419982U, 1822297739U, 2954791486U,
|
||||
3608508353U, 3174124327U, 2024746970U, 1432378464U,
|
||||
3864339955U, 2857741204U, 1464375394U, 1676153920U,
|
||||
1439316330U, 715854006U, 3033291828U, 289532110U,
|
||||
2706671279U, 2087905683U, 3018724369U, 1668267050U,
|
||||
732546397U, 1947742710U, 3462151702U, 2609353502U,
|
||||
2950085171U, 1814351708U, 2050118529U, 680887927U,
|
||||
999245976U, 1800124847U, 3300911131U, 1713906067U,
|
||||
1641548236U, 4213287313U, 1216130144U, 1575780402U,
|
||||
4018429277U, 3917837745U, 3693486850U, 3949271944U,
|
||||
596196993U, 3549867205U, 258830323U, 2213823033U,
|
||||
772490370U, 2760122372U, 1774776394U, 2652871518U,
|
||||
566650946U, 4142492826U, 1728879713U, 2882767088U,
|
||||
1783734482U, 3629395816U, 2517608232U, 2874225571U,
|
||||
1861159788U, 326777828U, 3124490320U, 2130389656U,
|
||||
2716951837U, 967770486U, 1724537150U, 2185432712U,
|
||||
2364442137U, 1164943284U, 2105845187U, 998989502U,
|
||||
3765401048U, 2244026483U, 1075463327U, 1455516326U,
|
||||
1322494562U, 910128902U, 469688178U, 1117454909U,
|
||||
936433444U, 3490320968U, 3675253459U, 1240580251U,
|
||||
122909385U, 2157517691U, 634681816U, 4142456567U,
|
||||
3825094682U, 3061402683U, 2540495037U, 79693498U,
|
||||
3249098678U, 1084186820U, 1583128258U, 426386531U,
|
||||
1761308591U, 1047286709U, 322548459U, 995290223U,
|
||||
1845252383U, 2603652396U, 3431023940U, 2942221577U,
|
||||
3202600964U, 3727903485U, 1712269319U, 422464435U,
|
||||
3234572375U, 1170764815U, 3523960633U, 3117677531U,
|
||||
1434042557U, 442511882U, 3600875718U, 1076654713U,
|
||||
1738483198U, 4213154764U, 2393238008U, 3677496056U,
|
||||
1014306527U, 4251020053U, 793779912U, 2902807211U,
|
||||
842905082U, 4246964064U, 1395751752U, 1040244610U,
|
||||
2656851899U, 3396308128U, 445077038U, 3742853595U,
|
||||
3577915638U, 679411651U, 2892444358U, 2354009459U,
|
||||
1767581616U, 3150600392U, 3791627101U, 3102740896U,
|
||||
284835224U, 4246832056U, 1258075500U, 768725851U,
|
||||
2589189241U, 3069724005U, 3532540348U, 1274779536U,
|
||||
3789419226U, 2764799539U, 1660621633U, 3471099624U,
|
||||
4011903706U, 913787905U, 3497959166U, 737222580U,
|
||||
2514213453U, 2928710040U, 3937242737U, 1804850592U,
|
||||
3499020752U, 2949064160U, 2386320175U, 2390070455U,
|
||||
2415321851U, 4061277028U, 2290661394U, 2416832540U,
|
||||
1336762016U, 1754252060U, 3520065937U, 3014181293U,
|
||||
791618072U, 3188594551U, 3933548030U, 2332172193U,
|
||||
3852520463U, 3043980520U, 413987798U, 3465142937U,
|
||||
3030929376U, 4245938359U, 2093235073U, 3534596313U,
|
||||
375366246U, 2157278981U, 2479649556U, 555357303U,
|
||||
3870105701U, 2008414854U, 3344188149U, 4221384143U,
|
||||
3956125452U, 2067696032U, 3594591187U, 2921233993U,
|
||||
2428461U, 544322398U, 577241275U, 1471733935U,
|
||||
610547355U, 4027169054U, 1432588573U, 1507829418U,
|
||||
2025931657U, 3646575487U, 545086370U, 48609733U,
|
||||
2200306550U, 1653985193U, 298326376U, 1316178497U,
|
||||
3007786442U, 2064951626U, 458293330U, 2589141269U,
|
||||
3591329599U, 3164325604U, 727753846U, 2179363840U,
|
||||
146436021U, 1461446943U, 4069977195U, 705550613U,
|
||||
3059967265U, 3887724982U, 4281599278U, 3313849956U,
|
||||
1404054877U, 2845806497U, 146425753U, 1854211946U,
|
||||
|
||||
1266315497U, 3048417604U, 3681880366U, 3289982499U,
|
||||
2909710000U, 1235738493U, 2632868024U, 2414719590U,
|
||||
3970600049U, 1771706367U, 1449415276U, 3266420449U,
|
||||
422970021U, 1963543593U, 2690192192U, 3826793022U,
|
||||
1062508698U, 1531092325U, 1804592342U, 2583117782U,
|
||||
2714934279U, 4024971509U, 1294809318U, 4028980673U,
|
||||
1289560198U, 2221992742U, 1669523910U, 35572830U,
|
||||
157838143U, 1052438473U, 1016535060U, 1802137761U,
|
||||
1753167236U, 1386275462U, 3080475397U, 2857371447U,
|
||||
1040679964U, 2145300060U, 2390574316U, 1461121720U,
|
||||
2956646967U, 4031777805U, 4028374788U, 33600511U,
|
||||
2920084762U, 1018524850U, 629373528U, 3691585981U,
|
||||
3515945977U, 2091462646U, 2486323059U, 586499841U,
|
||||
988145025U, 935516892U, 3367335476U, 2599673255U,
|
||||
2839830854U, 265290510U, 3972581182U, 2759138881U,
|
||||
3795373465U, 1005194799U, 847297441U, 406762289U,
|
||||
1314163512U, 1332590856U, 1866599683U, 4127851711U,
|
||||
750260880U, 613907577U, 1450815602U, 3165620655U,
|
||||
3734664991U, 3650291728U, 3012275730U, 3704569646U,
|
||||
1427272223U, 778793252U, 1343938022U, 2676280711U,
|
||||
2052605720U, 1946737175U, 3164576444U, 3914038668U,
|
||||
3967478842U, 3682934266U, 1661551462U, 3294938066U,
|
||||
4011595847U, 840292616U, 3712170807U, 616741398U,
|
||||
312560963U, 711312465U, 1351876610U, 322626781U,
|
||||
1910503582U, 271666773U, 2175563734U, 1594956187U,
|
||||
70604529U, 3617834859U, 1007753275U, 1495573769U,
|
||||
4069517037U, 2549218298U, 2663038764U, 504708206U,
|
||||
2263041392U, 3941167025U, 2249088522U, 1514023603U,
|
||||
1998579484U, 1312622330U, 694541497U, 2582060303U,
|
||||
2151582166U, 1382467621U, 776784248U, 2618340202U,
|
||||
3323268794U, 2497899128U, 2784771155U, 503983604U,
|
||||
4076293799U, 907881277U, 423175695U, 432175456U,
|
||||
1378068232U, 4145222326U, 3954048622U, 3938656102U,
|
||||
3820766613U, 2793130115U, 2977904593U, 26017576U,
|
||||
3274890735U, 3194772133U, 1700274565U, 1756076034U,
|
||||
4006520079U, 3677328699U, 720338349U, 1533947780U,
|
||||
354530856U, 688349552U, 3973924725U, 1637815568U,
|
||||
332179504U, 3949051286U, 53804574U, 2852348879U,
|
||||
3044236432U, 1282449977U, 3583942155U, 3416972820U,
|
||||
4006381244U, 1617046695U, 2628476075U, 3002303598U,
|
||||
1686838959U, 431878346U, 2686675385U, 1700445008U,
|
||||
1080580658U, 1009431731U, 832498133U, 3223435511U,
|
||||
2605976345U, 2271191193U, 2516031870U, 1648197032U,
|
||||
4164389018U, 2548247927U, 300782431U, 375919233U,
|
||||
238389289U, 3353747414U, 2531188641U, 2019080857U,
|
||||
1475708069U, 455242339U, 2609103871U, 448939670U,
|
||||
3451063019U, 1395535956U, 2413381860U, 1841049896U,
|
||||
1491858159U, 885456874U, 4264095073U, 4001119347U,
|
||||
1565136089U, 3898914787U, 1108368660U, 540939232U,
|
||||
1173283510U, 2745871338U, 3681308437U, 4207628240U,
|
||||
3343053890U, 4016749493U, 1699691293U, 1103962373U,
|
||||
3625875870U, 2256883143U, 3830138730U, 1031889488U,
|
||||
3479347698U, 1535977030U, 4236805024U, 3251091107U,
|
||||
2132092099U, 1774941330U, 1199868427U, 1452454533U,
|
||||
157007616U, 2904115357U, 342012276U, 595725824U,
|
||||
1480756522U, 206960106U, 497939518U, 591360097U,
|
||||
863170706U, 2375253569U, 3596610801U, 1814182875U,
|
||||
2094937945U, 3421402208U, 1082520231U, 3463918190U,
|
||||
2785509508U, 435703966U, 3908032597U, 1641649973U,
|
||||
2842273706U, 3305899714U, 1510255612U, 2148256476U,
|
||||
2655287854U, 3276092548U, 4258621189U, 236887753U,
|
||||
3681803219U, 274041037U, 1734335097U, 3815195456U,
|
||||
3317970021U, 1899903192U, 1026095262U, 4050517792U,
|
||||
356393447U, 2410691914U, 3873677099U, 3682840055U,
|
||||
|
||||
3913112168U, 2491498743U, 4132185628U, 2489919796U,
|
||||
1091903735U, 1979897079U, 3170134830U, 3567386728U,
|
||||
3557303409U, 857797738U, 1136121015U, 1342202287U,
|
||||
507115054U, 2535736646U, 337727348U, 3213592640U,
|
||||
1301675037U, 2528481711U, 1895095763U, 1721773893U,
|
||||
3216771564U, 62756741U, 2142006736U, 835421444U,
|
||||
2531993523U, 1442658625U, 3659876326U, 2882144922U,
|
||||
676362277U, 1392781812U, 170690266U, 3921047035U,
|
||||
1759253602U, 3611846912U, 1745797284U, 664899054U,
|
||||
1329594018U, 3901205900U, 3045908486U, 2062866102U,
|
||||
2865634940U, 3543621612U, 3464012697U, 1080764994U,
|
||||
553557557U, 3656615353U, 3996768171U, 991055499U,
|
||||
499776247U, 1265440854U, 648242737U, 3940784050U,
|
||||
980351604U, 3713745714U, 1749149687U, 3396870395U,
|
||||
4211799374U, 3640570775U, 1161844396U, 3125318951U,
|
||||
1431517754U, 545492359U, 4268468663U, 3499529547U,
|
||||
1437099964U, 2702547544U, 3433638243U, 2581715763U,
|
||||
2787789398U, 1060185593U, 1593081372U, 2418618748U,
|
||||
4260947970U, 69676912U, 2159744348U, 86519011U,
|
||||
2512459080U, 3838209314U, 1220612927U, 3339683548U,
|
||||
133810670U, 1090789135U, 1078426020U, 1569222167U,
|
||||
845107691U, 3583754449U, 4072456591U, 1091646820U,
|
||||
628848692U, 1613405280U, 3757631651U, 526609435U,
|
||||
236106946U, 48312990U, 2942717905U, 3402727701U,
|
||||
1797494240U, 859738849U, 992217954U, 4005476642U,
|
||||
2243076622U, 3870952857U, 3732016268U, 765654824U,
|
||||
3490871365U, 2511836413U, 1685915746U, 3888969200U,
|
||||
1414112111U, 2273134842U, 3281911079U, 4080962846U,
|
||||
172450625U, 2569994100U, 980381355U, 4109958455U,
|
||||
2819808352U, 2716589560U, 2568741196U, 3681446669U,
|
||||
3329971472U, 1835478071U, 660984891U, 3704678404U,
|
||||
4045999559U, 3422617507U, 3040415634U, 1762651403U,
|
||||
1719377915U, 3470491036U, 2693910283U, 3642056355U,
|
||||
3138596744U, 1364962596U, 2073328063U, 1983633131U,
|
||||
926494387U, 3423689081U, 2150032023U, 4096667949U,
|
||||
1749200295U, 3328846651U, 309677260U, 2016342300U,
|
||||
1779581495U, 3079819751U, 111262694U, 1274766160U,
|
||||
443224088U, 298511866U, 1025883608U, 3806446537U,
|
||||
1145181785U, 168956806U, 3641502830U, 3584813610U,
|
||||
1689216846U, 3666258015U, 3200248200U, 1692713982U,
|
||||
2646376535U, 4042768518U, 1618508792U, 1610833997U,
|
||||
3523052358U, 4130873264U, 2001055236U, 3610705100U,
|
||||
2202168115U, 4028541809U, 2961195399U, 1006657119U,
|
||||
2006996926U, 3186142756U, 1430667929U, 3210227297U,
|
||||
1314452623U, 4074634658U, 4101304120U, 2273951170U,
|
||||
1399257539U, 3367210612U, 3027628629U, 1190975929U,
|
||||
2062231137U, 2333990788U, 2221543033U, 2438960610U,
|
||||
1181637006U, 548689776U, 2362791313U, 3372408396U,
|
||||
3104550113U, 3145860560U, 296247880U, 1970579870U,
|
||||
3078560182U, 3769228297U, 1714227617U, 3291629107U,
|
||||
3898220290U, 166772364U, 1251581989U, 493813264U,
|
||||
448347421U, 195405023U, 2709975567U, 677966185U,
|
||||
3703036547U, 1463355134U, 2715995803U, 1338867538U,
|
||||
1343315457U, 2802222074U, 2684532164U, 233230375U,
|
||||
2599980071U, 2000651841U, 3277868038U, 1638401717U,
|
||||
4028070440U, 3237316320U, 6314154U, 819756386U,
|
||||
300326615U, 590932579U, 1405279636U, 3267499572U,
|
||||
3150704214U, 2428286686U, 3959192993U, 3461946742U,
|
||||
1862657033U, 1266418056U, 963775037U, 2089974820U,
|
||||
2263052895U, 1917689273U, 448879540U, 3550394620U,
|
||||
3981727096U, 150775221U, 3627908307U, 1303187396U,
|
||||
508620638U, 2975983352U, 2726630617U, 1817252668U,
|
||||
1876281319U, 1457606340U, 908771278U, 3720792119U,
|
||||
3617206836U, 2455994898U, 1729034894U, 1080033504U,
|
||||
|
||||
976866871U, 3556439503U, 2881648439U, 1522871579U,
|
||||
1555064734U, 1336096578U, 3548522304U, 2579274686U,
|
||||
3574697629U, 3205460757U, 3593280638U, 3338716283U,
|
||||
3079412587U, 564236357U, 2993598910U, 1781952180U,
|
||||
1464380207U, 3163844217U, 3332601554U, 1699332808U,
|
||||
1393555694U, 1183702653U, 3581086237U, 1288719814U,
|
||||
691649499U, 2847557200U, 2895455976U, 3193889540U,
|
||||
2717570544U, 1781354906U, 1676643554U, 2592534050U,
|
||||
3230253752U, 1126444790U, 2770207658U, 2633158820U,
|
||||
2210423226U, 2615765581U, 2414155088U, 3127139286U,
|
||||
673620729U, 2805611233U, 1269405062U, 4015350505U,
|
||||
3341807571U, 4149409754U, 1057255273U, 2012875353U,
|
||||
2162469141U, 2276492801U, 2601117357U, 993977747U,
|
||||
3918593370U, 2654263191U, 753973209U, 36408145U,
|
||||
2530585658U, 25011837U, 3520020182U, 2088578344U,
|
||||
530523599U, 2918365339U, 1524020338U, 1518925132U,
|
||||
3760827505U, 3759777254U, 1202760957U, 3985898139U,
|
||||
3906192525U, 674977740U, 4174734889U, 2031300136U,
|
||||
2019492241U, 3983892565U, 4153806404U, 3822280332U,
|
||||
352677332U, 2297720250U, 60907813U, 90501309U,
|
||||
3286998549U, 1016092578U, 2535922412U, 2839152426U,
|
||||
457141659U, 509813237U, 4120667899U, 652014361U,
|
||||
1966332200U, 2975202805U, 55981186U, 2327461051U,
|
||||
676427537U, 3255491064U, 2882294119U, 3433927263U,
|
||||
1307055953U, 942726286U, 933058658U, 2468411793U,
|
||||
3933900994U, 4215176142U, 1361170020U, 2001714738U,
|
||||
2830558078U, 3274259782U, 1222529897U, 1679025792U,
|
||||
2729314320U, 3714953764U, 1770335741U, 151462246U,
|
||||
3013232138U, 1682292957U, 1483529935U, 471910574U,
|
||||
1539241949U, 458788160U, 3436315007U, 1807016891U,
|
||||
3718408830U, 978976581U, 1043663428U, 3165965781U,
|
||||
1927990952U, 4200891579U, 2372276910U, 3208408903U,
|
||||
3533431907U, 1412390302U, 2931980059U, 4132332400U,
|
||||
1947078029U, 3881505623U, 4168226417U, 2941484381U,
|
||||
1077988104U, 1320477388U, 886195818U, 18198404U,
|
||||
3786409000U, 2509781533U, 112762804U, 3463356488U,
|
||||
1866414978U, 891333506U, 18488651U, 661792760U,
|
||||
1628790961U, 3885187036U, 3141171499U, 876946877U,
|
||||
2693282273U, 1372485963U, 791857591U, 2686433993U,
|
||||
3759982718U, 3167212022U, 3472953795U, 2716379847U,
|
||||
445679433U, 3561995674U, 3504004811U, 3574258232U,
|
||||
54117162U, 3331405415U, 2381918588U, 3769707343U,
|
||||
4154350007U, 1140177722U, 4074052095U, 668550556U,
|
||||
3214352940U, 367459370U, 261225585U, 2610173221U,
|
||||
4209349473U, 3468074219U, 3265815641U, 314222801U,
|
||||
3066103646U, 3808782860U, 282218597U, 3406013506U,
|
||||
3773591054U, 379116347U, 1285071038U, 846784868U,
|
||||
2669647154U, 3771962079U, 3550491691U, 2305946142U,
|
||||
453669953U, 1268987020U, 3317592352U, 3279303384U,
|
||||
3744833421U, 2610507566U, 3859509063U, 266596637U,
|
||||
3847019092U, 517658769U, 3462560207U, 3443424879U,
|
||||
370717030U, 4247526661U, 2224018117U, 4143653529U,
|
||||
4112773975U, 2788324899U, 2477274417U, 1456262402U,
|
||||
2901442914U, 1517677493U, 1846949527U, 2295493580U,
|
||||
3734397586U, 2176403920U, 1280348187U, 1908823572U,
|
||||
3871786941U, 846861322U, 1172426758U, 3287448474U,
|
||||
3383383037U, 1655181056U, 3139813346U, 901632758U,
|
||||
1897031941U, 2986607138U, 3066810236U, 3447102507U,
|
||||
1393639104U, 373351379U, 950779232U, 625454576U,
|
||||
3124240540U, 4148612726U, 2007998917U, 544563296U,
|
||||
2244738638U, 2330496472U, 2058025392U, 1291430526U,
|
||||
424198748U, 50039436U, 29584100U, 3605783033U,
|
||||
2429876329U, 2791104160U, 1057563949U, 3255363231U,
|
||||
3075367218U, 3463963227U, 1469046755U, 985887462U
|
||||
};
|
||||
|
||||
|
||||
|
||||
|
||||
} // namespace
|
||||
|
||||
@@ -0,0 +1,342 @@
|
||||
/*
|
||||
Copyright (C) 2000-2007 MySQL AB
|
||||
|
||||
This program is free software; you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation; version 2 of the License.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program; see the file COPYING. If not, write to the
|
||||
Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
|
||||
MA 02110-1301 USA.
|
||||
*/
|
||||
|
||||
/* C++ code based on Wei Dai's blowfish.cpp from CryptoPP */
|
||||
/* x86 asm is original */
|
||||
|
||||
|
||||
#if defined(TAOCRYPT_KERNEL_MODE)
|
||||
#define DO_TAOCRYPT_KERNEL_MODE
|
||||
#endif // only some modules now support this
|
||||
|
||||
|
||||
#include "runtime.hpp"
|
||||
#include "blowfish.hpp"
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
namespace TaoCrypt {
|
||||
|
||||
|
||||
#if defined(DO_BLOWFISH_ASM)
|
||||
|
||||
// ia32 optimized version
|
||||
void Blowfish::Process(byte* out, const byte* in, word32 sz)
|
||||
{
|
||||
if (!isMMX) {
|
||||
Mode_BASE::Process(out, in, sz);
|
||||
return;
|
||||
}
|
||||
|
||||
word32 blocks = sz / BLOCK_SIZE;
|
||||
|
||||
if (mode_ == ECB)
|
||||
while (blocks--) {
|
||||
AsmProcess(in, out);
|
||||
out += BLOCK_SIZE;
|
||||
in += BLOCK_SIZE;
|
||||
}
|
||||
else if (mode_ == CBC)
|
||||
if (dir_ == ENCRYPTION)
|
||||
while (blocks--) {
|
||||
r_[0] ^= *(word32*)in;
|
||||
r_[1] ^= *(word32*)(in + 4);
|
||||
|
||||
AsmProcess((byte*)r_, (byte*)r_);
|
||||
|
||||
memcpy(out, r_, BLOCK_SIZE);
|
||||
|
||||
out += BLOCK_SIZE;
|
||||
in += BLOCK_SIZE;
|
||||
}
|
||||
else
|
||||
while (blocks--) {
|
||||
AsmProcess(in, out);
|
||||
|
||||
*(word32*)out ^= r_[0];
|
||||
*(word32*)(out + 4) ^= r_[1];
|
||||
|
||||
memcpy(r_, in, BLOCK_SIZE);
|
||||
|
||||
out += BLOCK_SIZE;
|
||||
in += BLOCK_SIZE;
|
||||
}
|
||||
}
|
||||
|
||||
#endif // DO_BLOWFISH_ASM
|
||||
|
||||
|
||||
void Blowfish::SetKey(const byte* key_string, word32 keylength, CipherDir dir)
|
||||
{
|
||||
assert(keylength >= 4 && keylength <= 56);
|
||||
|
||||
unsigned i, j=0, k;
|
||||
word32 data, dspace[2] = {0, 0};
|
||||
|
||||
memcpy(pbox_, p_init_, sizeof(p_init_));
|
||||
memcpy(sbox_, s_init_, sizeof(s_init_));
|
||||
|
||||
// Xor key string into encryption key vector
|
||||
for (i=0 ; i<ROUNDS+2 ; ++i) {
|
||||
data = 0;
|
||||
for (k=0 ; k<4 ; ++k )
|
||||
data = (data << 8) | key_string[j++ % keylength];
|
||||
pbox_[i] ^= data;
|
||||
}
|
||||
|
||||
crypt_block(dspace, pbox_);
|
||||
|
||||
for (i=0; i<ROUNDS; i+=2)
|
||||
crypt_block(pbox_ + i, pbox_ + i + 2);
|
||||
|
||||
crypt_block(pbox_ + ROUNDS, sbox_);
|
||||
|
||||
for (i=0; i < 4*256-2; i+=2)
|
||||
crypt_block(sbox_ + i, sbox_ + i + 2);
|
||||
|
||||
if (dir==DECRYPTION)
|
||||
for (i=0; i<(ROUNDS+2)/2; i++)
|
||||
STL::swap(pbox_[i], pbox_[ROUNDS+1-i]);
|
||||
}
|
||||
|
||||
|
||||
#define BFBYTE_0(x) ( x &0xFF)
|
||||
#define BFBYTE_1(x) ((x>> 8)&0xFF)
|
||||
#define BFBYTE_2(x) ((x>>16)&0xFF)
|
||||
#define BFBYTE_3(x) ( x>>24)
|
||||
|
||||
|
||||
#define BF_S(Put, Get, I) (\
|
||||
Put ^= p[I], \
|
||||
tmp = p[18 + BFBYTE_3(Get)], \
|
||||
tmp += p[274+ BFBYTE_2(Get)], \
|
||||
tmp ^= p[530+ BFBYTE_1(Get)], \
|
||||
tmp += p[786+ BFBYTE_0(Get)], \
|
||||
Put ^= tmp \
|
||||
)
|
||||
|
||||
|
||||
#define BF_ROUNDS \
|
||||
BF_S(right, left, 1); \
|
||||
BF_S(left, right, 2); \
|
||||
BF_S(right, left, 3); \
|
||||
BF_S(left, right, 4); \
|
||||
BF_S(right, left, 5); \
|
||||
BF_S(left, right, 6); \
|
||||
BF_S(right, left, 7); \
|
||||
BF_S(left, right, 8); \
|
||||
BF_S(right, left, 9); \
|
||||
BF_S(left, right, 10); \
|
||||
BF_S(right, left, 11); \
|
||||
BF_S(left, right, 12); \
|
||||
BF_S(right, left, 13); \
|
||||
BF_S(left, right, 14); \
|
||||
BF_S(right, left, 15); \
|
||||
BF_S(left, right, 16);
|
||||
|
||||
#define BF_EXTRA_ROUNDS \
|
||||
BF_S(right, left, 17); \
|
||||
BF_S(left, right, 18); \
|
||||
BF_S(right, left, 19); \
|
||||
BF_S(left, right, 20);
|
||||
|
||||
|
||||
// Used by key setup, no byte swapping
|
||||
void Blowfish::crypt_block(const word32 in[2], word32 out[2]) const
|
||||
{
|
||||
word32 left = in[0];
|
||||
word32 right = in[1];
|
||||
|
||||
const word32* p = pbox_;
|
||||
word32 tmp;
|
||||
|
||||
left ^= p[0];
|
||||
|
||||
BF_ROUNDS
|
||||
|
||||
#if ROUNDS == 20
|
||||
BF_EXTRA_ROUNDS
|
||||
#endif
|
||||
|
||||
right ^= p[ROUNDS + 1];
|
||||
|
||||
out[0] = right;
|
||||
out[1] = left;
|
||||
}
|
||||
|
||||
|
||||
typedef BlockGetAndPut<word32, BigEndian> gpBlock;
|
||||
|
||||
void Blowfish::ProcessAndXorBlock(const byte* in, const byte* xOr, byte* out)
|
||||
const
|
||||
{
|
||||
word32 tmp, left, right;
|
||||
const word32* p = pbox_;
|
||||
|
||||
gpBlock::Get(in)(left)(right);
|
||||
left ^= p[0];
|
||||
|
||||
BF_ROUNDS
|
||||
|
||||
#if ROUNDS == 20
|
||||
BF_EXTRA_ROUNDS
|
||||
#endif
|
||||
|
||||
right ^= p[ROUNDS + 1];
|
||||
|
||||
gpBlock::Put(xOr, out)(right)(left);
|
||||
}
|
||||
|
||||
|
||||
#if defined(DO_BLOWFISH_ASM)
|
||||
#ifdef __GNUC__
|
||||
#define AS1(x) asm(#x);
|
||||
#define AS2(x, y) asm(#x ", " #y);
|
||||
|
||||
#define PROLOG() \
|
||||
asm(".intel_syntax noprefix"); \
|
||||
AS2( movd mm3, edi ) \
|
||||
AS2( movd mm4, ebx ) \
|
||||
AS2( movd mm5, esi ) \
|
||||
AS2( mov ecx, DWORD PTR [ebp + 8] ) \
|
||||
AS2( mov esi, DWORD PTR [ebp + 12] )
|
||||
|
||||
#define EPILOG() \
|
||||
AS2( movd esi, mm5 ) \
|
||||
AS2( movd ebx, mm4 ) \
|
||||
AS2( movd edi, mm3 ) \
|
||||
AS1( emms ) \
|
||||
asm(".att_syntax");
|
||||
#else
|
||||
#define AS1(x) __asm x
|
||||
#define AS2(x, y) __asm x, y
|
||||
|
||||
#define PROLOG() \
|
||||
AS1( push ebp ) \
|
||||
AS2( mov ebp, esp ) \
|
||||
AS2( movd mm3, edi ) \
|
||||
AS2( movd mm4, ebx ) \
|
||||
AS2( movd mm5, esi ) \
|
||||
AS2( mov esi, DWORD PTR [ebp + 8] )
|
||||
|
||||
#define EPILOG() \
|
||||
AS2( movd esi, mm5 ) \
|
||||
AS2( movd ebx, mm4 ) \
|
||||
AS2( movd edi, mm3 ) \
|
||||
AS2( mov esp, ebp ) \
|
||||
AS1( pop ebp ) \
|
||||
AS1( emms ) \
|
||||
AS1( ret 8 )
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
#define BF_ROUND(P, G, I) \
|
||||
/* Put ^= p[I] */ \
|
||||
AS2( xor P, [edi + I*4] ) \
|
||||
/* tmp = p[18 + BFBYTE_3(Get)] */ \
|
||||
AS2( mov ecx, G ) \
|
||||
AS2( shr ecx, 16 ) \
|
||||
AS2( movzx edx, ch ) \
|
||||
AS2( mov esi, [edi + edx*4 + 72] ) \
|
||||
/* tmp += p[274+ BFBYTE_2(Get)] */ \
|
||||
AS2( movzx ecx, cl ) \
|
||||
AS2( add esi, [edi + ecx*4 + 1096] ) \
|
||||
/* tmp ^= p[530+ BFBYTE_1(Get)] */ \
|
||||
AS2( mov ecx, G ) \
|
||||
AS2( movzx edx, ch ) \
|
||||
AS2( xor esi, [edi + edx*4 + 2120] ) \
|
||||
/* tmp += p[786+ BFBYTE_0(Get)] */ \
|
||||
AS2( movzx ecx, cl ) \
|
||||
AS2( add esi, [edi + ecx*4 + 3144] ) \
|
||||
/* Put ^= tmp */ \
|
||||
AS2( xor P, esi )
|
||||
|
||||
|
||||
#ifdef _MSC_VER
|
||||
__declspec(naked)
|
||||
#endif
|
||||
void Blowfish::AsmProcess(const byte* inBlock, byte* outBlock) const
|
||||
{
|
||||
PROLOG()
|
||||
|
||||
#ifdef OLD_GCC_OFFSET
|
||||
AS2( lea edi, [ecx + 60] ) // pbox
|
||||
#else
|
||||
AS2( lea edi, [ecx + 56] ) // pbox
|
||||
#endif
|
||||
|
||||
AS2( mov eax, DWORD PTR [esi] )
|
||||
AS2( mov edx, DWORD PTR [edi] )
|
||||
AS1( bswap eax )
|
||||
|
||||
AS2( mov ebx, DWORD PTR [esi + 4] )
|
||||
AS2( xor eax, edx ) // left
|
||||
AS1( bswap ebx ) // right
|
||||
|
||||
|
||||
BF_ROUND(ebx, eax, 1)
|
||||
BF_ROUND(eax, ebx, 2)
|
||||
BF_ROUND(ebx, eax, 3)
|
||||
BF_ROUND(eax, ebx, 4)
|
||||
BF_ROUND(ebx, eax, 5)
|
||||
BF_ROUND(eax, ebx, 6)
|
||||
BF_ROUND(ebx, eax, 7)
|
||||
BF_ROUND(eax, ebx, 8)
|
||||
BF_ROUND(ebx, eax, 9)
|
||||
BF_ROUND(eax, ebx, 10)
|
||||
BF_ROUND(ebx, eax, 11)
|
||||
BF_ROUND(eax, ebx, 12)
|
||||
BF_ROUND(ebx, eax, 13)
|
||||
BF_ROUND(eax, ebx, 14)
|
||||
BF_ROUND(ebx, eax, 15)
|
||||
BF_ROUND(eax, ebx, 16)
|
||||
#if ROUNDS == 20
|
||||
BF_ROUND(ebx, eax, 17)
|
||||
BF_ROUND(eax, ebx, 18)
|
||||
BF_ROUND(ebx, eax, 19)
|
||||
BF_ROUND(eax, ebx, 20)
|
||||
|
||||
AS2( xor ebx, [edi + 84] ) // 20 + 1 (x4)
|
||||
#else
|
||||
AS2( xor ebx, [edi + 68] ) // 16 + 1 (x4)
|
||||
#endif
|
||||
|
||||
#ifdef __GNUC__
|
||||
AS2( mov edi, [ebp + 16] ) // outBlock
|
||||
#else
|
||||
AS2( mov edi, [ebp + 12] ) // outBlock
|
||||
#endif
|
||||
|
||||
AS1( bswap ebx )
|
||||
AS1( bswap eax )
|
||||
|
||||
AS2( mov [edi] , ebx )
|
||||
AS2( mov [edi + 4], eax )
|
||||
|
||||
EPILOG()
|
||||
}
|
||||
|
||||
|
||||
#endif // DO_BLOWFISH_ASM
|
||||
|
||||
|
||||
} // namespace
|
||||
|
||||
@@ -0,0 +1,253 @@
|
||||
/*
|
||||
Copyright (C) 2000-2007 MySQL AB
|
||||
|
||||
This program is free software; you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation; version 2 of the License.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program; see the file COPYING. If not, write to the
|
||||
Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
|
||||
MA 02110-1301 USA.
|
||||
*/
|
||||
|
||||
/* coding.cpp implements hex and base64 encoding/decoing
|
||||
*/
|
||||
|
||||
#include "runtime.hpp"
|
||||
#include "coding.hpp"
|
||||
#include "file.hpp"
|
||||
|
||||
|
||||
namespace TaoCrypt {
|
||||
|
||||
|
||||
namespace { // locals
|
||||
|
||||
const byte bad = 0xFF; // invalid encoding
|
||||
|
||||
const byte hexEncode[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
|
||||
'A', 'B', 'C', 'D', 'E', 'F'
|
||||
};
|
||||
|
||||
const byte hexDecode[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
|
||||
bad, bad, bad, bad, bad, bad, bad,
|
||||
10, 11, 12, 13, 14, 15
|
||||
}; // A starts at 0x41 not 0x3A
|
||||
|
||||
|
||||
const byte base64Encode[] = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J',
|
||||
'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T',
|
||||
'U', 'V', 'W', 'X', 'Y', 'Z',
|
||||
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',
|
||||
'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't',
|
||||
'u', 'v', 'w', 'x', 'y', 'z',
|
||||
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
|
||||
'+', '/'
|
||||
};
|
||||
|
||||
const byte base64Decode[] = { 62, bad, bad, bad, 63, // + starts at 0x2B
|
||||
52, 53, 54, 55, 56, 57, 58, 59, 60, 61,
|
||||
bad, bad, bad, bad, bad, bad, bad,
|
||||
0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
|
||||
10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
|
||||
20, 21, 22, 23, 24, 25,
|
||||
bad, bad, bad, bad, bad, bad,
|
||||
26, 27, 28, 29, 30, 31, 32, 33, 34, 35,
|
||||
36, 37, 38, 39, 40, 41, 42, 43, 44, 45,
|
||||
46, 47, 48, 49, 50, 51
|
||||
};
|
||||
|
||||
const byte pad = '=';
|
||||
const int pemLineSz = 64;
|
||||
|
||||
} // local namespace
|
||||
|
||||
|
||||
// Hex Encode
|
||||
void HexEncoder::Encode()
|
||||
{
|
||||
word32 bytes = plain_.size();
|
||||
encoded_.New(bytes * 2);
|
||||
|
||||
word32 i = 0;
|
||||
|
||||
while (bytes--) {
|
||||
byte p = plain_.next();
|
||||
|
||||
byte b = p >> 4;
|
||||
byte b2 = p & 0xF;
|
||||
|
||||
encoded_[i++] = hexEncode[b];
|
||||
encoded_[i++] = hexEncode[b2];
|
||||
}
|
||||
|
||||
plain_.reset(encoded_);
|
||||
}
|
||||
|
||||
|
||||
// Hex Decode
|
||||
void HexDecoder::Decode()
|
||||
{
|
||||
word32 bytes = coded_.size();
|
||||
assert((bytes % 2) == 0);
|
||||
decoded_.New(bytes / 2);
|
||||
|
||||
word32 i(0);
|
||||
|
||||
while (bytes) {
|
||||
byte b = coded_.next() - 0x30; // 0 starts at 0x30
|
||||
byte b2 = coded_.next() - 0x30;
|
||||
|
||||
// sanity checks
|
||||
assert( b < sizeof(hexDecode)/sizeof(hexDecode[0]) );
|
||||
assert( b2 < sizeof(hexDecode)/sizeof(hexDecode[0]) );
|
||||
|
||||
b = hexDecode[b];
|
||||
b2 = hexDecode[b2];
|
||||
|
||||
assert( b != bad && b2 != bad );
|
||||
|
||||
decoded_[i++] = (b << 4) | b2;
|
||||
bytes -= 2;
|
||||
}
|
||||
|
||||
coded_.reset(decoded_);
|
||||
}
|
||||
|
||||
|
||||
// Base 64 Encode
|
||||
void Base64Encoder::Encode()
|
||||
{
|
||||
word32 bytes = plain_.size();
|
||||
word32 outSz = (bytes + 3 - 1) / 3 * 4;
|
||||
|
||||
outSz += (outSz + pemLineSz - 1) / pemLineSz; // new lines
|
||||
encoded_.New(outSz);
|
||||
|
||||
word32 i = 0;
|
||||
word32 j = 0;
|
||||
|
||||
while (bytes > 2) {
|
||||
byte b1 = plain_.next();
|
||||
byte b2 = plain_.next();
|
||||
byte b3 = plain_.next();
|
||||
|
||||
// encoded idx
|
||||
byte e1 = b1 >> 2;
|
||||
byte e2 = ((b1 & 0x3) << 4) | (b2 >> 4);
|
||||
byte e3 = ((b2 & 0xF) << 2) | (b3 >> 6);
|
||||
byte e4 = b3 & 0x3F;
|
||||
|
||||
// store
|
||||
encoded_[i++] = base64Encode[e1];
|
||||
encoded_[i++] = base64Encode[e2];
|
||||
encoded_[i++] = base64Encode[e3];
|
||||
encoded_[i++] = base64Encode[e4];
|
||||
|
||||
bytes -= 3;
|
||||
|
||||
if ((++j % 16) == 0 && bytes)
|
||||
encoded_[i++] = '\n';
|
||||
}
|
||||
|
||||
// last integral
|
||||
if (bytes) {
|
||||
bool twoBytes = (bytes == 2);
|
||||
|
||||
byte b1 = plain_.next();
|
||||
byte b2 = (twoBytes) ? plain_.next() : 0;
|
||||
|
||||
byte e1 = b1 >> 2;
|
||||
byte e2 = ((b1 & 0x3) << 4) | (b2 >> 4);
|
||||
byte e3 = (b2 & 0xF) << 2;
|
||||
|
||||
encoded_[i++] = base64Encode[e1];
|
||||
encoded_[i++] = base64Encode[e2];
|
||||
encoded_[i++] = (twoBytes) ? base64Encode[e3] : pad;
|
||||
encoded_[i++] = pad;
|
||||
}
|
||||
|
||||
encoded_[i++] = '\n';
|
||||
assert(i == outSz);
|
||||
|
||||
plain_.reset(encoded_);
|
||||
}
|
||||
|
||||
|
||||
// Base 64 Decode
|
||||
void Base64Decoder::Decode()
|
||||
{
|
||||
word32 bytes = coded_.size();
|
||||
word32 plainSz = bytes - ((bytes + (pemLineSz - 1)) / pemLineSz);
|
||||
plainSz = (plainSz * 3 + 3) / 4;
|
||||
decoded_.New(plainSz);
|
||||
|
||||
word32 i = 0;
|
||||
word32 j = 0;
|
||||
|
||||
while (bytes > 3) {
|
||||
byte e1 = coded_.next();
|
||||
byte e2 = coded_.next();
|
||||
byte e3 = coded_.next();
|
||||
byte e4 = coded_.next();
|
||||
|
||||
// do asserts first
|
||||
if (e1 == 0) // end file 0's
|
||||
break;
|
||||
|
||||
bool pad3 = false;
|
||||
bool pad4 = false;
|
||||
if (e3 == pad)
|
||||
pad3 = true;
|
||||
if (e4 == pad)
|
||||
pad4 = true;
|
||||
|
||||
e1 = base64Decode[e1 - 0x2B];
|
||||
e2 = base64Decode[e2 - 0x2B];
|
||||
e3 = (e3 == pad) ? 0 : base64Decode[e3 - 0x2B];
|
||||
e4 = (e4 == pad) ? 0 : base64Decode[e4 - 0x2B];
|
||||
|
||||
byte b1 = (e1 << 2) | (e2 >> 4);
|
||||
byte b2 = ((e2 & 0xF) << 4) | (e3 >> 2);
|
||||
byte b3 = ((e3 & 0x3) << 6) | e4;
|
||||
|
||||
decoded_[i++] = b1;
|
||||
if (!pad3)
|
||||
decoded_[i++] = b2;
|
||||
if (!pad4)
|
||||
decoded_[i++] = b3;
|
||||
else
|
||||
break;
|
||||
|
||||
bytes -= 4;
|
||||
if ((++j % 16) == 0) {
|
||||
byte endLine = coded_.next();
|
||||
bytes--;
|
||||
while (endLine == ' ') { // remove possible whitespace
|
||||
endLine = coded_.next();
|
||||
bytes--;
|
||||
}
|
||||
if (endLine == '\r') {
|
||||
endLine = coded_.next();
|
||||
bytes--;
|
||||
}
|
||||
if (endLine != '\n') {
|
||||
coded_.SetError(PEM_E);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (i != decoded_.size())
|
||||
decoded_.resize(i);
|
||||
coded_.reset(decoded_);
|
||||
}
|
||||
|
||||
|
||||
} // namespace
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user