diff options
Diffstat (limited to 'externals/mysql/extlib')
144 files changed, 44758 insertions, 0 deletions
diff --git a/externals/mysql/extlib/dbug/dbug.c b/externals/mysql/extlib/dbug/dbug.c new file mode 100644 index 00000000000..68932adf1e1 --- /dev/null +++ b/externals/mysql/extlib/dbug/dbug.c @@ -0,0 +1,2583 @@ +/****************************************************************************** + * * + * N O T I C E * + * * + * Copyright Abandoned, 1987, Fred Fish * + * * + * * + * This previously copyrighted work has been placed into the public * + * domain by the author and may be freely used for any purpose, * + * private or commercial. * + * * + * Because of the number of inquiries I was receiving about the use * + * of this product in commercially developed works I have decided to * + * simply make it public domain to further its unrestricted use. I * + * specifically would be most happy to see this material become a * + * part of the standard Unix distributions by AT&T and the Berkeley * + * Computer Science Research Group, and a standard part of the GNU * + * system from the Free Software Foundation. * + * * + * I would appreciate it, as a courtesy, if this notice is left in * + * all copies and derivative works. Thank you. * + * * + * The author makes no warranty of any kind with respect to this * + * product and explicitly disclaims any implied warranties of mer- * + * chantability or fitness for any particular purpose. * + * * + ****************************************************************************** + */ + +/* + * FILE + * + * dbug.c runtime support routines for dbug package + * + * SCCS + * + * @(#)dbug.c 1.25 7/25/89 + * + * DESCRIPTION + * + * These are the runtime support routines for the dbug package. + * The dbug package has two main components; the user include + * file containing various macro definitions, and the runtime + * support routines which are called from the macro expansions. + * + * Externally visible functions in the runtime support module + * use the naming convention pattern "_db_xx...xx_", thus + * they are unlikely to collide with user defined function names. + * + * AUTHOR(S) + * + * Fred Fish (base code) + * Enhanced Software Technologies, Tempe, AZ + * asuvax!mcdphx!estinc!fnf + * + * Binayak Banerjee (profiling enhancements) + * seismo!bpa!sjuvax!bbanerje + * + * Michael Widenius: + * DBUG_DUMP - To dump a block of memory. + * PUSH_FLAG "O" - To be used insted of "o" if we + * want flushing after each write + * PUSH_FLAG "A" - as 'O', but we will append to the out file instead + * of creating a new one. + * Check of malloc on entry/exit (option "S") + * + * Sergei Golubchik: + * DBUG_EXECUTE_IF + * incremental mode (-#+t:-d,info ...) + * DBUG_SET, _db_explain_ + * thread-local settings + * negative lists (-#-d,info => everything but "info") + * + * function/ syntax + * (the logic is - think of a call stack as of a path. + * "function" means only this function, "function/" means the hierarchy. + * in the future, filters like function1/function2 could be supported. + * following this logic glob(7) wildcards are supported.) + * + */ + +/* + We can't have SAFE_MUTEX defined here as this will cause recursion + in pthread_mutex_lock +*/ + +#undef SAFE_MUTEX +#include <my_global.h> +#include <m_string.h> +#include <errno.h> + +#ifdef HAVE_FNMATCH_H +#include <fnmatch.h> +#else +#define fnmatch(A,B,C) strcmp(A,B) +#endif + +#if defined(MSDOS) || defined(__WIN__) +#include <process.h> +#endif + +#ifndef DBUG_OFF + + +/* + * Manifest constants which may be "tuned" if desired. + */ + +#define PRINTBUF 1024 /* Print buffer size */ +#define INDENT 2 /* Indentation per trace level */ +#define MAXDEPTH 200 /* Maximum trace depth default */ + +/* + * The following flags are used to determine which + * capabilities the user has enabled with the settings + * push macro. + * + * TRACE_ON is also used in _db_stack_frame_->level + * (until we add flags to _db_stack_frame_, increasing it by 4 bytes) + */ + +#define DEBUG_ON (1 << 1) /* Debug enabled */ +#define FILE_ON (1 << 2) /* File name print enabled */ +#define LINE_ON (1 << 3) /* Line number print enabled */ +#define DEPTH_ON (1 << 4) /* Function nest level print enabled */ +#define PROCESS_ON (1 << 5) /* Process name print enabled */ +#define NUMBER_ON (1 << 6) /* Number each line of output */ +#define PROFILE_ON (1 << 7) /* Print out profiling code */ +#define PID_ON (1 << 8) /* Identify each line with process id */ +#define TIMESTAMP_ON (1 << 9) /* timestamp every line of output */ +#define SANITY_CHECK_ON (1 << 10) /* Check safemalloc on DBUG_ENTER */ +#define FLUSH_ON_WRITE (1 << 11) /* Flush on every write */ +#define OPEN_APPEND (1 << 12) /* Open for append */ +#define TRACE_ON ((uint)1 << 31) /* Trace enabled. MUST be the highest bit!*/ + +#define TRACING (cs->stack->flags & TRACE_ON) +#define DEBUGGING (cs->stack->flags & DEBUG_ON) +#define PROFILING (cs->stack->flags & PROFILE_ON) + +/* + * Typedefs to make things more obvious. + */ + +#define BOOLEAN my_bool + +/* + * Make it easy to change storage classes if necessary. + */ + +#define IMPORT extern /* Names defined externally */ +#define EXPORT /* Allocated here, available globally */ +#define AUTO auto /* Names to be allocated on stack */ +#define REGISTER register /* Names to be placed in registers */ + +/* + * The default file for profiling. Could also add another flag + * (G?) which allowed the user to specify this. + * + * If the automatic variables get allocated on the stack in + * reverse order from their declarations, then define AUTOS_REVERSE to 1. + * This is used by the code that keeps track of stack usage. For + * forward allocation, the difference in the dbug frame pointers + * represents stack used by the callee function. For reverse allocation, + * the difference represents stack used by the caller function. + * + */ + +#define PROF_FILE "dbugmon.out" +#define PROF_EFMT "E\t%ld\t%s\n" +#define PROF_SFMT "S\t%lx\t%lx\t%s\n" +#define PROF_XFMT "X\t%ld\t%s\n" + +#ifdef M_I386 /* predefined by xenix 386 compiler */ +#define AUTOS_REVERSE 1 +#else +#define AUTOS_REVERSE 0 +#endif + +/* + * Externally supplied functions. + */ + +#ifndef HAVE_PERROR +static void perror(); /* Fake system/library error print routine */ +#endif + +#ifdef SAFEMALLOC +IMPORT int _sanity(const char *file,uint line); /* safemalloc sanity checker */ +#else +#define _sanity(X,Y) (1) +#endif + +/* + * The user may specify a list of functions to trace or + * debug. These lists are kept in a linear linked list, + * a very simple implementation. + */ + +struct link { + struct link *next_link; /* Pointer to the next link */ + char flags; + char str[1]; /* Pointer to link's contents */ +}; + +/* flags for struct link and return flags of InList */ +#define SUBDIR 1 /* this MUST be 1 */ +#define INCLUDE 2 +#define EXCLUDE 4 +/* this is not a struct link flag, but only a return flags of InList */ +#define MATCHED 65536 +#define NOT_MATCHED 0 + +/* + * Debugging settings can be pushed or popped off of a + * stack which is implemented as a linked list. Note + * that the head of the list is the current settings and the + * stack is pushed by adding a new settings to the head of the + * list or popped by removing the first link. + * + * Note: if out_file is NULL, the other fields are not initialized at all! + */ + +struct settings { + uint flags; /* Current settings flags */ + uint maxdepth; /* Current maximum trace depth */ + uint delay; /* Delay after each output line */ + uint sub_level; /* Sub this from code_state->level */ + FILE *out_file; /* Current output stream */ + FILE *prof_file; /* Current profiling stream */ + char name[FN_REFLEN]; /* Name of output file */ + struct link *functions; /* List of functions */ + struct link *p_functions; /* List of profiled functions */ + struct link *keywords; /* List of debug keywords */ + struct link *processes; /* List of process names */ + struct settings *next; /* Next settings in the list */ +}; + +#define is_shared(S, V) ((S)->next && (S)->next->V == (S)->V) + +/* + * Local variables not seen by user. + */ + + +static BOOLEAN init_done= FALSE; /* Set to TRUE when initialization done */ +static struct settings init_settings; +static const char *db_process= 0;/* Pointer to process name; argv[0] */ +my_bool _dbug_on_= TRUE; /* FALSE if no debugging at all */ + +typedef struct _db_code_state_ { + const char *process; /* Pointer to process name; usually argv[0] */ + const char *func; /* Name of current user function */ + const char *file; /* Name of current user file */ + struct _db_stack_frame_ *framep; /* Pointer to current frame */ + struct settings *stack; /* debugging settings */ + const char *jmpfunc; /* Remember current function for setjmp */ + const char *jmpfile; /* Remember current file for setjmp */ + int lineno; /* Current debugger output line number */ + uint level; /* Current function nesting level */ + int jmplevel; /* Remember nesting level at setjmp() */ + +/* + * The following variables are used to hold the state information + * between the call to _db_pargs_() and _db_doprnt_(), during + * expansion of the DBUG_PRINT macro. This is the only macro + * that currently uses these variables. + * + * These variables are currently used only by _db_pargs_() and + * _db_doprnt_(). + */ + + uint u_line; /* User source code line number */ + int locked; /* If locked with _db_lock_file_ */ + const char *u_keyword; /* Keyword for current macro */ +} CODE_STATE; + +/* + The test below is so we could call functions with DBUG_ENTER before + my_thread_init(). +*/ +#define get_code_state_if_not_set_or_return if (!cs && !((cs=code_state()))) return +#define get_code_state_or_return if (!((cs=code_state()))) return + + /* Handling lists */ +#define ListAdd(A,B,C) ListAddDel(A,B,C,INCLUDE) +#define ListDel(A,B,C) ListAddDel(A,B,C,EXCLUDE) +static struct link *ListAddDel(struct link *, const char *, const char *, int); +static struct link *ListCopy(struct link *); +static int InList(struct link *linkp,const char *cp); +static uint ListFlags(struct link *linkp); +static void FreeList(struct link *linkp); + + /* OpenClose debug output stream */ +static void DBUGOpenFile(CODE_STATE *,const char *, const char *, int); +static void DBUGCloseFile(CODE_STATE *cs, FILE *fp); + /* Push current debug settings */ +static void PushState(CODE_STATE *cs); + /* Free memory associated with debug state. */ +static void FreeState (CODE_STATE *cs, struct settings *state, int free_state); + /* Test for tracing enabled */ +static int DoTrace(CODE_STATE *cs); +/* + return values of DoTrace. + Can also be used as bitmask: ret & DO_TRACE +*/ +#define DO_TRACE 1 +#define DONT_TRACE 2 +#define ENABLE_TRACE 3 +#define DISABLE_TRACE 4 + + /* Test to see if file is writable */ +#if defined(HAVE_ACCESS) && !defined(MSDOS) +static BOOLEAN Writable(const char *pathname); + /* Change file owner and group */ +static void ChangeOwner(CODE_STATE *cs, char *pathname); + /* Allocate memory for runtime support */ +#endif + +static void DoPrefix(CODE_STATE *cs, uint line); + +static char *DbugMalloc(size_t size); +static const char *BaseName(const char *pathname); +static void Indent(CODE_STATE *cs, int indent); +static void DbugFlush(CODE_STATE *); +static void DbugExit(const char *why); +static const char *DbugStrTok(const char *s); +static void DbugFprintf(FILE *stream, const char* format, va_list args); + +#ifndef THREAD + /* Open profile output stream */ +static FILE *OpenProfile(CODE_STATE *cs, const char *name); + /* Profile if asked for it */ +static BOOLEAN DoProfile(CODE_STATE *); + /* Return current user time (ms) */ +static unsigned long Clock(void); +#endif + +/* + * Miscellaneous printf format strings. + */ + +#define ERR_MISSING_RETURN "%s: missing DBUG_RETURN or DBUG_VOID_RETURN macro in function \"%s\"\n" +#define ERR_OPEN "%s: can't open debug output stream \"%s\": " +#define ERR_CLOSE "%s: can't close debug file: " +#define ERR_ABORT "%s: debugger aborting because %s\n" +#define ERR_CHOWN "%s: can't change owner/group of \"%s\": " + +/* + * Macros and defines for testing file accessibility under UNIX and MSDOS. + */ + +#undef EXISTS +#if !defined(HAVE_ACCESS) || defined(MSDOS) +#define EXISTS(pathname) (FALSE) /* Assume no existance */ +#define Writable(name) (TRUE) +#else +#define EXISTS(pathname) (access(pathname, F_OK) == 0) +#define WRITABLE(pathname) (access(pathname, W_OK) == 0) +#endif +#ifndef MSDOS +#define ChangeOwner(cs,name) +#endif + + +/* +** Macros to allow dbugging with threads +*/ + +#ifdef THREAD +#include <my_pthread.h> +static pthread_mutex_t THR_LOCK_dbug; + +static CODE_STATE *code_state(void) +{ + CODE_STATE *cs, **cs_ptr; + + /* + _dbug_on_ is reset if we don't plan to use any debug commands at all and + we want to run on maximum speed + */ + if (!_dbug_on_) + return 0; + + if (!init_done) + { + init_done=TRUE; + pthread_mutex_init(&THR_LOCK_dbug,MY_MUTEX_INIT_FAST); + bzero(&init_settings, sizeof(init_settings)); + init_settings.out_file=stderr; + init_settings.flags=OPEN_APPEND; + } + + if (!(cs_ptr= (CODE_STATE**) my_thread_var_dbug())) + return 0; /* Thread not initialised */ + if (!(cs= *cs_ptr)) + { + cs=(CODE_STATE*) DbugMalloc(sizeof(*cs)); + bzero((uchar*) cs,sizeof(*cs)); + cs->process= db_process ? db_process : "dbug"; + cs->func="?func"; + cs->file="?file"; + cs->stack=&init_settings; + *cs_ptr= cs; + } + return cs; +} + +#else /* !THREAD */ + +static CODE_STATE static_code_state= +{ + "dbug", "?func", "?file", NULL, &init_settings, + NullS, NullS, 0,0,0,0,0,NullS +}; + +static CODE_STATE *code_state(void) +{ + if (!init_done) + { + bzero(&init_settings, sizeof(init_settings)); + init_settings.out_file=stderr; + init_settings.flags=OPEN_APPEND; + init_done=TRUE; + } + return &static_code_state; +} + +#define pthread_mutex_lock(A) {} +#define pthread_mutex_unlock(A) {} +#endif + +/* + * Translate some calls among different systems. + */ + +#ifdef HAVE_SLEEP +/* sleep() wants seconds */ +#define Delay(A) sleep(((uint) A)/10) +#else +#define Delay(A) (0) +#endif + +/* + * FUNCTION + * + * _db_process_ give the name to the current process/thread + * + * SYNOPSIS + * + * VOID _process_(name) + * char *name; + * + */ + +void _db_process_(const char *name) +{ + CODE_STATE *cs; + + if (!db_process) + db_process= name; + + get_code_state_or_return; + cs->process= name; +} + +/* + * FUNCTION + * + * DbugParse parse control string and set current debugger settings + * + * DESCRIPTION + * + * Given pointer to a debug control string in "control", + * parses the control string, and sets + * up a current debug settings. + * + * The debug control string is a sequence of colon separated fields + * as follows: + * + * [+]<field_1>:<field_2>:...:<field_N> + * + * Each field consists of a mandatory flag character followed by + * an optional "," and comma separated list of modifiers: + * + * [sign]flag[,modifier,modifier,...,modifier] + * + * See the manual for the list of supported signs, flags, and modifiers + * + * For convenience, any leading "-#" is stripped off. + * + * RETURN + * 1 - a list of functions ("f" flag) was possibly changed + * 0 - a list of functions was not changed + */ + +int DbugParse(CODE_STATE *cs, const char *control) +{ + const char *end; + int rel, f_used=0; + struct settings *stack; + + stack= cs->stack; + + if (control[0] == '-' && control[1] == '#') + control+=2; + + rel= control[0] == '+' || control[0] == '-'; + if ((!rel || (!stack->out_file && !stack->next))) + { + FreeState(cs, stack, 0); + stack->flags= 0; + stack->delay= 0; + stack->maxdepth= 0; + stack->sub_level= 0; + stack->out_file= stderr; + stack->prof_file= NULL; + stack->functions= NULL; + stack->p_functions= NULL; + stack->keywords= NULL; + stack->processes= NULL; + } + else if (!stack->out_file) + { + stack->flags= stack->next->flags; + stack->delay= stack->next->delay; + stack->maxdepth= stack->next->maxdepth; + stack->sub_level= stack->next->sub_level; + strcpy(stack->name, stack->next->name); + stack->out_file= stack->next->out_file; + stack->prof_file= stack->next->prof_file; + if (stack->next == &init_settings) + { + /* never share with the global parent - it can change under your feet */ + stack->functions= ListCopy(init_settings.functions); + stack->p_functions= ListCopy(init_settings.p_functions); + stack->keywords= ListCopy(init_settings.keywords); + stack->processes= ListCopy(init_settings.processes); + } + else + { + stack->functions= stack->next->functions; + stack->p_functions= stack->next->p_functions; + stack->keywords= stack->next->keywords; + stack->processes= stack->next->processes; + } + } + + end= DbugStrTok(control); + while (control < end) + { + int c, sign= (*control == '+') ? 1 : (*control == '-') ? -1 : 0; + if (sign) control++; + c= *control++; + if (*control == ',') control++; + /* XXX when adding new cases here, don't forget _db_explain_ ! */ + switch (c) { + case 'd': + if (sign < 0 && control == end) + { + if (!is_shared(stack, keywords)) + FreeList(stack->keywords); + stack->keywords=NULL; + stack->flags &= ~DEBUG_ON; + break; + } + if (rel && is_shared(stack, keywords)) + stack->keywords= ListCopy(stack->keywords); + if (sign < 0) + { + if (DEBUGGING) + stack->keywords= ListDel(stack->keywords, control, end); + break; + } + stack->keywords= ListAdd(stack->keywords, control, end); + stack->flags |= DEBUG_ON; + break; + case 'D': + stack->delay= atoi(control); + break; + case 'f': + f_used= 1; + if (sign < 0 && control == end) + { + if (!is_shared(stack,functions)) + FreeList(stack->functions); + stack->functions=NULL; + break; + } + if (rel && is_shared(stack,functions)) + stack->functions= ListCopy(stack->functions); + if (sign < 0) + stack->functions= ListDel(stack->functions, control, end); + else + stack->functions= ListAdd(stack->functions, control, end); + break; + case 'F': + if (sign < 0) + stack->flags &= ~FILE_ON; + else + stack->flags |= FILE_ON; + break; + case 'i': + if (sign < 0) + stack->flags &= ~PID_ON; + else + stack->flags |= PID_ON; + break; +#ifndef THREAD + case 'g': + if (OpenProfile(cs, PROF_FILE)) + { + stack->flags |= PROFILE_ON; + stack->p_functions= ListAdd(stack->p_functions, control, end); + } + break; +#endif + case 'L': + if (sign < 0) + stack->flags &= ~LINE_ON; + else + stack->flags |= LINE_ON; + break; + case 'n': + if (sign < 0) + stack->flags &= ~DEPTH_ON; + else + stack->flags |= DEPTH_ON; + break; + case 'N': + if (sign < 0) + stack->flags &= ~NUMBER_ON; + else + stack->flags |= NUMBER_ON; + break; + case 'A': + case 'O': + stack->flags |= FLUSH_ON_WRITE; + /* fall through */ + case 'a': + case 'o': + if (sign < 0) + { + if (!is_shared(stack, out_file)) + DBUGCloseFile(cs, stack->out_file); + stack->flags &= ~FLUSH_ON_WRITE; + stack->out_file= stderr; + break; + } + if (c == 'a' || c == 'A') + stack->flags |= OPEN_APPEND; + else + stack->flags &= ~OPEN_APPEND; + if (control != end) + DBUGOpenFile(cs, control, end, stack->flags & OPEN_APPEND); + else + DBUGOpenFile(cs, "-",0,0); + break; + case 'p': + if (sign < 0 && control == end) + { + if (!is_shared(stack,processes)) + FreeList(stack->processes); + stack->processes=NULL; + break; + } + if (rel && is_shared(stack, processes)) + stack->processes= ListCopy(stack->processes); + if (sign < 0) + stack->processes= ListDel(stack->processes, control, end); + else + stack->processes= ListAdd(stack->processes, control, end); + break; + case 'P': + if (sign < 0) + stack->flags &= ~PROCESS_ON; + else + stack->flags |= PROCESS_ON; + break; + case 'r': + stack->sub_level= cs->level; + break; + case 't': + if (sign < 0) + { + if (control != end) + stack->maxdepth-= atoi(control); + else + stack->maxdepth= 0; + } + else + { + if (control != end) + stack->maxdepth+= atoi(control); + else + stack->maxdepth= MAXDEPTH; + } + if (stack->maxdepth > 0) + stack->flags |= TRACE_ON; + else + stack->flags &= ~TRACE_ON; + break; + case 'T': + if (sign < 0) + stack->flags &= ~TIMESTAMP_ON; + else + stack->flags |= TIMESTAMP_ON; + break; + case 'S': + if (sign < 0) + stack->flags &= ~SANITY_CHECK_ON; + else + stack->flags |= SANITY_CHECK_ON; + break; + } + if (!*end) + break; + control=end+1; + end= DbugStrTok(control); + } + return !rel || f_used; +} + +#define framep_trace_flag(cs, frp) (frp ? \ + frp->level & TRACE_ON : \ + (ListFlags(cs->stack->functions) & INCLUDE) ? \ + 0 : (uint)TRACE_ON) + +void FixTraceFlags_helper(CODE_STATE *cs, const char *func, + struct _db_stack_frame_ *framep) +{ + if (framep->prev) + FixTraceFlags_helper(cs, framep->func, framep->prev); + + cs->func= func; + cs->level= framep->level & ~TRACE_ON; + framep->level= cs->level | framep_trace_flag(cs, framep->prev); + /* + we don't set cs->framep correctly, even though DoTrace uses it. + It's ok, because cs->framep may only affect DO_TRACE/DONT_TRACE return + values, but we ignore them here anyway + */ + switch(DoTrace(cs)) { + case ENABLE_TRACE: + framep->level|= TRACE_ON; + break; + case DISABLE_TRACE: + framep->level&= ~TRACE_ON; + break; + } +} + +#define fflags(cs) cs->stack->out_file ? ListFlags(cs->stack->functions) : TRACE_ON; + +void FixTraceFlags(uint old_fflags, CODE_STATE *cs) +{ + const char *func; + uint new_fflags, traceon, level; + struct _db_stack_frame_ *framep; + + /* + first (a.k.a. safety) check: + if we haven't started tracing yet, no call stack at all - we're safe. + */ + framep=cs->framep; + if (framep == 0) + return; + + /* + Ok, the tracing has started, call stack isn't empty. + + second check: does the new list have a SUBDIR rule ? + */ + new_fflags=fflags(cs); + if (new_fflags & SUBDIR) + goto yuck; + + /* + Ok, new list doesn't use SUBDIR. + + third check: we do NOT need to re-scan if + neither old nor new lists used SUBDIR flag and if a default behavior + (whether an unlisted function is traced) hasn't changed. + Default behavior depends on whether there're INCLUDE elements in the list. + */ + if (!(old_fflags & SUBDIR) && !((new_fflags^old_fflags) & INCLUDE)) + return; + + /* + Ok, old list may've used SUBDIR, or defaults could've changed. + + fourth check: are we inside a currently active SUBDIR rule ? + go up the call stack, if TRACE_ON flag ever changes its value - we are. + */ + for (traceon=framep->level; framep; framep=framep->prev) + if ((traceon ^ framep->level) & TRACE_ON) + goto yuck; + + /* + Ok, TRACE_ON flag doesn't change in the call stack. + + fifth check: but is the top-most value equal to a default one ? + */ + if (((traceon & TRACE_ON) != 0) == ((new_fflags & INCLUDE) == 0)) + return; + +yuck: + /* + Yuck! function list was changed, and one of the currently active rules + was possibly affected. For example, a tracing could've been enabled or + disabled for a function somewhere up the call stack. + To react correctly, we must go up the call stack all the way to + the top and re-match rules to set TRACE_ON bit correctly. + + We must traverse the stack forwards, not backwards. + That's what a recursive helper is doing. + It'll destroy two CODE_STATE fields, save them now. + */ + func= cs->func; + level= cs->level; + FixTraceFlags_helper(cs, func, cs->framep); + /* now we only need to restore CODE_STATE fields, and we're done */ + cs->func= func; + cs->level= level; +} + +/* + * FUNCTION + * + * _db_set_ set current debugger settings + * + * SYNOPSIS + * + * VOID _db_set_(control) + * char *control; + * + * DESCRIPTION + * + * Given pointer to a debug control string in "control", + * parses the control string, and sets up a current debug + * settings. Pushes a new debug settings if the current is + * set to the initial debugger settings. + * + */ + +void _db_set_(const char *control) +{ + CODE_STATE *cs; + uint old_fflags; + get_code_state_or_return; + old_fflags=fflags(cs); + if (cs->stack == &init_settings) + PushState(cs); + if (DbugParse(cs, control)) + FixTraceFlags(old_fflags, cs); +} + +/* + * FUNCTION + * + * _db_push_ push current debugger settings and set up new one + * + * SYNOPSIS + * + * VOID _db_push_(control) + * char *control; + * + * DESCRIPTION + * + * Given pointer to a debug control string in "control", pushes + * the current debug settings, parses the control string, and sets + * up a new debug settings with DbugParse() + * + */ + +void _db_push_(const char *control) +{ + CODE_STATE *cs; + uint old_fflags; + get_code_state_or_return; + old_fflags=fflags(cs); + PushState(cs); + if (DbugParse(cs, control)) + FixTraceFlags(old_fflags, cs); +} + + +/** + Returns TRUE if session-local settings have been set. +*/ + +int _db_is_pushed_() +{ + CODE_STATE *cs= NULL; + get_code_state_or_return FALSE; + return (cs->stack != &init_settings); +} + +/* + * FUNCTION + * + * _db_set_init_ set initial debugger settings + * + * SYNOPSIS + * + * VOID _db_set_init_(control) + * char *control; + * + * DESCRIPTION + * see _db_set_ + * + */ + +void _db_set_init_(const char *control) +{ + CODE_STATE tmp_cs; + bzero((uchar*) &tmp_cs, sizeof(tmp_cs)); + tmp_cs.stack= &init_settings; + DbugParse(&tmp_cs, control); +} + +/* + * FUNCTION + * + * _db_pop_ pop the debug stack + * + * DESCRIPTION + * + * Pops the debug stack, returning the debug settings to its + * condition prior to the most recent _db_push_ invocation. + * Note that the pop will fail if it would remove the last + * valid settings from the stack. This prevents user errors + * in the push/pop sequence from screwing up the debugger. + * Maybe there should be some kind of warning printed if the + * user tries to pop too many states. + * + */ + +void _db_pop_() +{ + struct settings *discard; + uint old_fflags; + CODE_STATE *cs; + + get_code_state_or_return; + + discard= cs->stack; + if (discard != &init_settings) + { + old_fflags=fflags(cs); + cs->stack= discard->next; + FreeState(cs, discard, 1); + FixTraceFlags(old_fflags, cs); + } +} + +/* + * FUNCTION + * + * _db_explain_ generates 'control' string for the current settings + * + * RETURN + * 0 - ok + * 1 - buffer too short, output truncated + * + */ + +/* helper macros */ +#define char_to_buf(C) do { \ + *buf++=(C); \ + if (buf >= end) goto overflow; \ + } while (0) +#define str_to_buf(S) do { \ + char_to_buf(','); \ + buf=strnmov(buf, (S), len+1); \ + if (buf >= end) goto overflow; \ + } while (0) +#define list_to_buf(l, f) do { \ + struct link *listp=(l); \ + while (listp) \ + { \ + if (listp->flags & (f)) \ + { \ + str_to_buf(listp->str); \ + if (listp->flags & SUBDIR) \ + char_to_buf('/'); \ + } \ + listp=listp->next_link; \ + } \ + } while (0) +#define int_to_buf(i) do { \ + char b[50]; \ + int10_to_str((i), b, 10); \ + str_to_buf(b); \ + } while (0) +#define colon_to_buf do { \ + if (buf != start) char_to_buf(':'); \ + } while(0) +#define op_int_to_buf(C, val, def) do { \ + if ((val) != (def)) \ + { \ + colon_to_buf; \ + char_to_buf((C)); \ + int_to_buf(val); \ + } \ + } while (0) +#define op_intf_to_buf(C, val, def, cond) do { \ + if ((cond)) \ + { \ + colon_to_buf; \ + char_to_buf((C)); \ + if ((val) != (def)) int_to_buf(val); \ + } \ + } while (0) +#define op_str_to_buf(C, val, cond) do { \ + if ((cond)) \ + { \ + char *s=(val); \ + colon_to_buf; \ + char_to_buf((C)); \ + if (*s) str_to_buf(s); \ + } \ + } while (0) +#define op_list_to_buf(C, val, cond) do { \ + if ((cond)) \ + { \ + int f=ListFlags(val); \ + colon_to_buf; \ + char_to_buf((C)); \ + if (f & INCLUDE) \ + list_to_buf(val, INCLUDE); \ + if (f & EXCLUDE) \ + { \ + colon_to_buf; \ + char_to_buf('-'); \ + char_to_buf((C)); \ + list_to_buf(val, EXCLUDE); \ + } \ + } \ + } while (0) +#define op_bool_to_buf(C, cond) do { \ + if ((cond)) \ + { \ + colon_to_buf; \ + char_to_buf((C)); \ + } \ + } while (0) + +int _db_explain_ (CODE_STATE *cs, char *buf, size_t len) +{ + char *start=buf, *end=buf+len-4; + + get_code_state_if_not_set_or_return *buf=0; + + op_list_to_buf('d', cs->stack->keywords, DEBUGGING); + op_int_to_buf ('D', cs->stack->delay, 0); + op_list_to_buf('f', cs->stack->functions, cs->stack->functions); + op_bool_to_buf('F', cs->stack->flags & FILE_ON); + op_bool_to_buf('i', cs->stack->flags & PID_ON); + op_list_to_buf('g', cs->stack->p_functions, PROFILING); + op_bool_to_buf('L', cs->stack->flags & LINE_ON); + op_bool_to_buf('n', cs->stack->flags & DEPTH_ON); + op_bool_to_buf('N', cs->stack->flags & NUMBER_ON); + op_str_to_buf( + ((cs->stack->flags & FLUSH_ON_WRITE ? 0 : 32) | + (cs->stack->flags & OPEN_APPEND ? 'A' : 'O')), + cs->stack->name, cs->stack->out_file != stderr); + op_list_to_buf('p', cs->stack->processes, cs->stack->processes); + op_bool_to_buf('P', cs->stack->flags & PROCESS_ON); + op_bool_to_buf('r', cs->stack->sub_level != 0); + op_intf_to_buf('t', cs->stack->maxdepth, MAXDEPTH, TRACING); + op_bool_to_buf('T', cs->stack->flags & TIMESTAMP_ON); + op_bool_to_buf('S', cs->stack->flags & SANITY_CHECK_ON); + + *buf= '\0'; + return 0; + +overflow: + *end++= '.'; + *end++= '.'; + *end++= '.'; + *end= '\0'; + return 1; +} + +#undef char_to_buf +#undef str_to_buf +#undef list_to_buf +#undef int_to_buf +#undef colon_to_buf +#undef op_int_to_buf +#undef op_intf_to_buf +#undef op_str_to_buf +#undef op_list_to_buf +#undef op_bool_to_buf + +/* + * FUNCTION + * + * _db_explain_init_ explain initial debugger settings + * + * DESCRIPTION + * see _db_explain_ + */ + +int _db_explain_init_(char *buf, size_t len) +{ + CODE_STATE cs; + bzero((uchar*) &cs,sizeof(cs)); + cs.stack=&init_settings; + return _db_explain_(&cs, buf, len); +} + +/* + * FUNCTION + * + * _db_enter_ process entry point to user function + * + * SYNOPSIS + * + * VOID _db_enter_(_func_, _file_, _line_, _stack_frame_) + * char *_func_; points to current function name + * char *_file_; points to current file name + * int _line_; called from source line number + * struct _db_stack_frame_ allocated on the caller's stack + * + * DESCRIPTION + * + * Called at the beginning of each user function to tell + * the debugger that a new function has been entered. + * Note that the pointers to the previous user function + * name and previous user file name are stored on the + * caller's stack (this is why the ENTER macro must be + * the first "executable" code in a function, since it + * allocates these storage locations). The previous nesting + * level is also stored on the callers stack for internal + * self consistency checks. + * + * Also prints a trace line if tracing is enabled and + * increments the current function nesting depth. + * + * Note that this mechanism allows the debugger to know + * what the current user function is at all times, without + * maintaining an internal stack for the function names. + * + */ + +void _db_enter_(const char *_func_, const char *_file_, + uint _line_, struct _db_stack_frame_ *_stack_frame_) +{ + int save_errno; + CODE_STATE *cs; + if (!((cs=code_state()))) + { + _stack_frame_->level= 0; /* Set to avoid valgrind warnings if dbug is enabled later */ + _stack_frame_->prev= 0; + return; + } + save_errno= errno; + + _stack_frame_->func= cs->func; + _stack_frame_->file= cs->file; + cs->func= _func_; + cs->file= _file_; + _stack_frame_->prev= cs->framep; + _stack_frame_->level= ++cs->level | framep_trace_flag(cs, cs->framep); + cs->framep= _stack_frame_; +#ifndef THREAD + if (DoProfile(cs)) + { + long stackused; + if (cs->framep->prev == NULL) + stackused= 0; + else + { + stackused= (char*)(cs->framep->prev) - (char*)(cs->framep); + stackused= stackused > 0 ? stackused : -stackused; + } + (void) fprintf(cs->stack->prof_file, PROF_EFMT , Clock(), cs->func); + (void) fprintf(cs->stack->prof_file, PROF_SFMT, (ulong) cs->framep, stackused, + AUTOS_REVERSE ? _stack_frame_->func : cs->func); + (void) fflush(cs->stack->prof_file); + } +#endif + switch (DoTrace(cs)) { + case ENABLE_TRACE: + cs->framep->level|= TRACE_ON; + if (!TRACING) break; + /* fall through */ + case DO_TRACE: + if ((cs->stack->flags & SANITY_CHECK_ON) && _sanity(_file_,_line_)) + cs->stack->flags &= ~SANITY_CHECK_ON; + if (TRACING) + { + if (!cs->locked) + pthread_mutex_lock(&THR_LOCK_dbug); + DoPrefix(cs, _line_); + Indent(cs, cs->level); + (void) fprintf(cs->stack->out_file, ">%s\n", cs->func); + DbugFlush(cs); /* This does a unlock */ + } + break; + case DISABLE_TRACE: + cs->framep->level&= ~TRACE_ON; + /* fall through */ + case DONT_TRACE: + break; + } + errno=save_errno; +} + +/* + * FUNCTION + * + * _db_return_ process exit from user function + * + * SYNOPSIS + * + * VOID _db_return_(_line_, _stack_frame_) + * int _line_; current source line number + * struct _db_stack_frame_ allocated on the caller's stack + * + * DESCRIPTION + * + * Called just before user function executes an explicit or implicit + * return. Prints a trace line if trace is enabled, decrements + * the current nesting level, and restores the current function and + * file names from the defunct function's stack. + * + */ + +/* helper macro */ +void _db_return_(uint _line_, struct _db_stack_frame_ *_stack_frame_) +{ + int save_errno=errno; + uint _slevel_= _stack_frame_->level & ~TRACE_ON; + CODE_STATE *cs; + get_code_state_or_return; + + if (cs->level != _slevel_) + { + if (!cs->locked) + pthread_mutex_lock(&THR_LOCK_dbug); + (void) fprintf(cs->stack->out_file, ERR_MISSING_RETURN, cs->process, + cs->func); + DbugFlush(cs); + } + else + { +#ifndef THREAD + if (DoProfile(cs)) + (void) fprintf(cs->stack->prof_file, PROF_XFMT, Clock(), cs->func); +#endif + if (DoTrace(cs) & DO_TRACE) + { + if ((cs->stack->flags & SANITY_CHECK_ON) && + _sanity(_stack_frame_->file,_line_)) + cs->stack->flags &= ~SANITY_CHECK_ON; + if (TRACING) + { + if (!cs->locked) + pthread_mutex_lock(&THR_LOCK_dbug); + DoPrefix(cs, _line_); + Indent(cs, cs->level); + (void) fprintf(cs->stack->out_file, "<%s\n", cs->func); + DbugFlush(cs); + } + } + } + /* + Check to not set level < 0. This can happen if DBUG was disabled when + function was entered and enabled in function. + */ + cs->level= _slevel_ != 0 ? _slevel_ - 1 : 0; + cs->func= _stack_frame_->func; + cs->file= _stack_frame_->file; + if (cs->framep != NULL) + cs->framep= cs->framep->prev; + errno=save_errno; +} + + +/* + * FUNCTION + * + * _db_pargs_ log arguments for subsequent use by _db_doprnt_() + * + * SYNOPSIS + * + * VOID _db_pargs_(_line_, keyword) + * int _line_; + * char *keyword; + * + * DESCRIPTION + * + * The new universal printing macro DBUG_PRINT, which replaces + * all forms of the DBUG_N macros, needs two calls to runtime + * support routines. The first, this function, remembers arguments + * that are used by the subsequent call to _db_doprnt_(). + * + */ + +void _db_pargs_(uint _line_, const char *keyword) +{ + CODE_STATE *cs; + get_code_state_or_return; + cs->u_line= _line_; + cs->u_keyword= keyword; +} + + +/* + * FUNCTION + * + * _db_doprnt_ handle print of debug lines + * + * SYNOPSIS + * + * VOID _db_doprnt_(format, va_alist) + * char *format; + * va_dcl; + * + * DESCRIPTION + * + * When invoked via one of the DBUG macros, tests the current keyword + * set by calling _db_pargs_() to see if that macro has been selected + * for processing via the debugger control string, and if so, handles + * printing of the arguments via the format string. The line number + * of the DBUG macro in the source is found in u_line. + * + * Note that the format string SHOULD NOT include a terminating + * newline, this is supplied automatically. + * + */ + +#include <stdarg.h> + +void _db_doprnt_(const char *format,...) +{ + va_list args; + CODE_STATE *cs; + get_code_state_or_return; + + va_start(args,format); + + if (_db_keyword_(cs, cs->u_keyword, 0)) + { + int save_errno=errno; + if (!cs->locked) + pthread_mutex_lock(&THR_LOCK_dbug); + DoPrefix(cs, cs->u_line); + if (TRACING) + Indent(cs, cs->level + 1); + else + (void) fprintf(cs->stack->out_file, "%s: ", cs->func); + (void) fprintf(cs->stack->out_file, "%s: ", cs->u_keyword); + DbugFprintf(cs->stack->out_file, format, args); + DbugFlush(cs); + errno=save_errno; + } + va_end(args); +} + +/* + * fprintf clone with consistent, platform independent output for + * problematic formats like %p, %zd and %lld. + */ +static void DbugFprintf(FILE *stream, const char* format, va_list args) +{ + char cvtbuf[1024]; + size_t len; + len = my_vsnprintf(cvtbuf, sizeof(cvtbuf), format, args); + (void) fprintf(stream, "%s\n", cvtbuf); +} + + +/* + * FUNCTION + * + * _db_dump_ dump a string in hex + * + * SYNOPSIS + * + * void _db_dump_(_line_,keyword,memory,length) + * int _line_; current source line number + * char *keyword; + * char *memory; Memory to print + * int length; Bytes to print + * + * DESCRIPTION + * Dump N characters in a binary array. + * Is used to examine corrputed memory or arrays. + */ + +void _db_dump_(uint _line_, const char *keyword, + const unsigned char *memory, size_t length) +{ + int pos; + char dbuff[90]; + CODE_STATE *cs; + get_code_state_or_return; + + if (_db_keyword_(cs, keyword, 0)) + { + if (!cs->locked) + pthread_mutex_lock(&THR_LOCK_dbug); + DoPrefix(cs, _line_); + if (TRACING) + { + Indent(cs, cs->level + 1); + pos= min(max(cs->level-cs->stack->sub_level,0)*INDENT,80); + } + else + { + fprintf(cs->stack->out_file, "%s: ", cs->func); + } + sprintf(dbuff,"%s: Memory: 0x%lx Bytes: (%ld)\n", + keyword, (ulong) memory, (long) length); + (void) fputs(dbuff,cs->stack->out_file); + + pos=0; + while (length-- > 0) + { + uint tmp= *((unsigned char*) memory++); + if ((pos+=3) >= 80) + { + fputc('\n',cs->stack->out_file); + pos=3; + } + fputc(_dig_vec_upper[((tmp >> 4) & 15)], cs->stack->out_file); + fputc(_dig_vec_upper[tmp & 15], cs->stack->out_file); + fputc(' ',cs->stack->out_file); + } + (void) fputc('\n',cs->stack->out_file); + DbugFlush(cs); + } +} + + +/* + * FUNCTION + * + * ListAddDel modify the list according to debug control string + * + * DESCRIPTION + * + * Given pointer to a comma separated list of strings in "cltp", + * parses the list, and modifies "listp", returning a pointer + * to the new list. + * + * The mode of operation is defined by "todo" parameter. + * + * If it is INCLUDE, elements (strings from "cltp") are added to the + * list, they will have INCLUDE flag set. If the list already contains + * the string in question, new element is not added, but a flag of + * the existing element is adjusted (INCLUDE bit is set, EXCLUDE bit + * is removed). + * + * If it is EXCLUDE, elements are added to the list with the EXCLUDE + * flag set. If the list already contains the string in question, + * it is removed, new element is not added. + */ + +static struct link *ListAddDel(struct link *head, const char *ctlp, + const char *end, int todo) +{ + const char *start; + struct link **cur; + size_t len; + int subdir; + + ctlp--; +next: + while (++ctlp < end) + { + start= ctlp; + subdir=0; + while (ctlp < end && *ctlp != ',') + ctlp++; + len=ctlp-start; + if (start[len-1] == '/') + { + len--; + subdir=SUBDIR; + } + if (len == 0) continue; + for (cur=&head; *cur; cur=&((*cur)->next_link)) + { + if (!strncmp((*cur)->str, start, len)) + { + if ((*cur)->flags & todo) /* same action ? */ + (*cur)->flags|= subdir; /* just merge the SUBDIR flag */ + else if (todo == EXCLUDE) + { + struct link *delme=*cur; + *cur=(*cur)->next_link; + free((void*) delme); + } + else + { + (*cur)->flags&=~(EXCLUDE & SUBDIR); + (*cur)->flags|=INCLUDE | subdir; + } + goto next; + } + } + *cur= (struct link *) DbugMalloc(sizeof(struct link)+len); + memcpy((*cur)->str, start, len); + (*cur)->str[len]=0; + (*cur)->flags=todo | subdir; + (*cur)->next_link=0; + } + return head; +} + +/* + * FUNCTION + * + * ListCopy make a copy of the list + * + * SYNOPSIS + * + * static struct link *ListCopy(orig) + * struct link *orig; + * + * DESCRIPTION + * + * Given pointer to list, which contains a copy of every element from + * the original list. + * + * the orig pointer can be NULL + * + * Note that since each link is added at the head of the list, + * the final list will be in "reverse order", which is not + * significant for our usage here. + * + */ + +static struct link *ListCopy(struct link *orig) +{ + struct link *new_malloc; + struct link *head; + size_t len; + + head= NULL; + while (orig != NULL) + { + len= strlen(orig->str); + new_malloc= (struct link *) DbugMalloc(sizeof(struct link)+len); + memcpy(new_malloc->str, orig->str, len); + new_malloc->str[len]= 0; + new_malloc->flags=orig->flags; + new_malloc->next_link= head; + head= new_malloc; + orig= orig->next_link; + } + return head; +} + +/* + * FUNCTION + * + * InList test a given string for member of a given list + * + * DESCRIPTION + * + * Tests the string pointed to by "cp" to determine if it is in + * the list pointed to by "linkp". Linkp points to the first + * link in the list. If linkp is NULL or contains only EXCLUDE + * elements then the string is treated as if it is in the list. + * This may seem rather strange at first but leads to the desired + * operation if no list is given. The net effect is that all + * strings will be accepted when there is no list, and when there + * is a list, only those strings in the list will be accepted. + * + * RETURN + * combination of SUBDIR, INCLUDE, EXCLUDE, MATCHED flags + * + */ + +static int InList(struct link *linkp, const char *cp) +{ + int result; + + for (result=MATCHED; linkp != NULL; linkp= linkp->next_link) + { + if (!fnmatch(linkp->str, cp, 0)) + return linkp->flags; + if (!(linkp->flags & EXCLUDE)) + result=NOT_MATCHED; + if (linkp->flags & SUBDIR) + result|=SUBDIR; + } + return result; +} + +/* + * FUNCTION + * + * ListFlags returns aggregated list flags (ORed over all elements) + * + */ + +static uint ListFlags(struct link *linkp) +{ + uint f; + for (f=0; linkp != NULL; linkp= linkp->next_link) + f|= linkp->flags; + return f; +} + +/* + * FUNCTION + * + * PushState push current settings onto stack and set up new one + * + * SYNOPSIS + * + * static VOID PushState() + * + * DESCRIPTION + * + * Pushes the current settings on the settings stack, and creates + * a new settings. The new settings is NOT initialized + * + * The settings stack is a linked list of settings, with the new + * settings added at the head. This allows the stack to grow + * to the limits of memory if necessary. + * + */ + +static void PushState(CODE_STATE *cs) +{ + struct settings *new_malloc; + + new_malloc= (struct settings *) DbugMalloc(sizeof(struct settings)); + bzero(new_malloc, sizeof(*new_malloc)); + new_malloc->next= cs->stack; + cs->stack= new_malloc; +} + +/* + * FUNCTION + * + * FreeState Free memory associated with a struct state. + * + * SYNOPSIS + * + * static void FreeState (state) + * struct state *state; + * int free_state; + * + * DESCRIPTION + * + * Deallocates the memory allocated for various information in a + * state. If free_state is set, also free 'state' + * + */ +static void FreeState(CODE_STATE *cs, struct settings *state, int free_state) +{ + if (!is_shared(state, keywords)) + FreeList(state->keywords); + if (!is_shared(state, functions)) + FreeList(state->functions); + if (!is_shared(state, processes)) + FreeList(state->processes); + if (!is_shared(state, p_functions)) + FreeList(state->p_functions); + + if (!is_shared(state, out_file)) + DBUGCloseFile(cs, state->out_file); + else + (void) fflush(state->out_file); + + if (!is_shared(state, prof_file)) + DBUGCloseFile(cs, state->prof_file); + else + (void) fflush(state->prof_file); + + if (free_state) + free((void*) state); +} + + +/* + * FUNCTION + * + * _db_end_ End debugging, freeing state stack memory. + * + * SYNOPSIS + * + * static VOID _db_end_ () + * + * DESCRIPTION + * + * Ends debugging, de-allocating the memory allocated to the + * state stack. + * + * To be called at the very end of the program. + * + */ +void _db_end_() +{ + struct settings *discard; + static struct settings tmp; + CODE_STATE *cs; + /* + Set _dbug_on_ to be able to do full reset even when DEBUGGER_OFF was + called after dbug was initialized + */ + _dbug_on_= 1; + get_code_state_or_return; + + while ((discard= cs->stack)) + { + if (discard == &init_settings) + break; + cs->stack= discard->next; + FreeState(cs, discard, 1); + } + tmp= init_settings; + + /* Use mutex lock to make it less likely anyone access out_file */ + pthread_mutex_lock(&THR_LOCK_dbug); + init_settings.flags= OPEN_APPEND; + init_settings.out_file= stderr; + init_settings.prof_file= stderr; + init_settings.maxdepth= 0; + init_settings.delay= 0; + init_settings.sub_level= 0; + init_settings.functions= 0; + init_settings.p_functions= 0; + init_settings.keywords= 0; + init_settings.processes= 0; + pthread_mutex_unlock(&THR_LOCK_dbug); + FreeState(cs, &tmp, 0); +} + + +/* + * FUNCTION + * + * DoTrace check to see if tracing is current enabled + * + * DESCRIPTION + * + * Checks to see if dbug in this function is enabled based on + * whether the maximum trace depth has been reached, the current + * function is selected, and the current process is selected. + * + */ + +static int DoTrace(CODE_STATE *cs) +{ + if ((cs->stack->maxdepth == 0 || cs->level <= cs->stack->maxdepth) && + InList(cs->stack->processes, cs->process) & (MATCHED|INCLUDE)) + switch(InList(cs->stack->functions, cs->func)) { + case INCLUDE|SUBDIR: return ENABLE_TRACE; + case INCLUDE: return DO_TRACE; + case MATCHED|SUBDIR: + case NOT_MATCHED|SUBDIR: + case MATCHED: return framep_trace_flag(cs, cs->framep) ? + DO_TRACE : DONT_TRACE; + case EXCLUDE: + case NOT_MATCHED: return DONT_TRACE; + case EXCLUDE|SUBDIR: return DISABLE_TRACE; + } + return DONT_TRACE; +} + + +/* + * FUNCTION + * + * DoProfile check to see if profiling is current enabled + * + * SYNOPSIS + * + * static BOOLEAN DoProfile() + * + * DESCRIPTION + * + * Checks to see if profiling is enabled based on whether the + * user has specified profiling, the maximum trace depth has + * not yet been reached, the current function is selected, + * and the current process is selected. Returns TRUE if + * profiling is enabled, FALSE otherwise. + * + */ + +#ifndef THREAD +static BOOLEAN DoProfile(CODE_STATE *cs) +{ + return PROFILING && + cs->level <= cs->stack->maxdepth && + InList(cs->stack->p_functions, cs->func) & (INCLUDE|MATCHED) && + InList(cs->stack->processes, cs->process) & (INCLUDE|MATCHED); +} +#endif + +FILE *_db_fp_(void) +{ + CODE_STATE *cs; + get_code_state_or_return NULL; + return cs->stack->out_file; +} + +/* + * FUNCTION + * + * _db_keyword_ test keyword for member of keyword list + * + * DESCRIPTION + * + * Test a keyword to determine if it is in the currently active + * keyword list. If strict=0, a keyword is accepted + * if the list is null, otherwise it must match one of the list + * members. When debugging is not on, no keywords are accepted. + * After the maximum trace level is exceeded, no keywords are + * accepted (this behavior subject to change). Additionally, + * the current function and process must be accepted based on + * their respective lists. + * + * Returns TRUE if keyword accepted, FALSE otherwise. + * + */ + +BOOLEAN _db_keyword_(CODE_STATE *cs, const char *keyword, int strict) +{ + get_code_state_if_not_set_or_return FALSE; + strict=strict ? INCLUDE : INCLUDE|MATCHED; + + return DEBUGGING && DoTrace(cs) & DO_TRACE && + InList(cs->stack->keywords, keyword) & strict; +} + +/* + * FUNCTION + * + * Indent indent a line to the given indentation level + * + * SYNOPSIS + * + * static VOID Indent(indent) + * int indent; + * + * DESCRIPTION + * + * Indent a line to the given level. Note that this is + * a simple minded but portable implementation. + * There are better ways. + * + * Also, the indent must be scaled by the compile time option + * of character positions per nesting level. + * + */ + +static void Indent(CODE_STATE *cs, int indent) +{ + REGISTER int count; + + indent= max(indent-1-cs->stack->sub_level,0)*INDENT; + for (count= 0; count < indent ; count++) + { + if ((count % INDENT) == 0) + fputc('|',cs->stack->out_file); + else + fputc(' ',cs->stack->out_file); + } +} + + +/* + * FUNCTION + * + * FreeList free all memory associated with a linked list + * + * SYNOPSIS + * + * static VOID FreeList(linkp) + * struct link *linkp; + * + * DESCRIPTION + * + * Given pointer to the head of a linked list, frees all + * memory held by the list and the members of the list. + * + */ + +static void FreeList(struct link *linkp) +{ + REGISTER struct link *old; + + while (linkp != NULL) + { + old= linkp; + linkp= linkp->next_link; + free((void*) old); + } +} + + +/* + * FUNCTION + * + * DoPrefix print debugger line prefix prior to indentation + * + * SYNOPSIS + * + * static VOID DoPrefix(_line_) + * int _line_; + * + * DESCRIPTION + * + * Print prefix common to all debugger output lines, prior to + * doing indentation if necessary. Print such information as + * current process name, current source file name and line number, + * and current function nesting depth. + * + */ + +static void DoPrefix(CODE_STATE *cs, uint _line_) +{ + cs->lineno++; + if (cs->stack->flags & PID_ON) + { +#ifdef THREAD + (void) fprintf(cs->stack->out_file, "%-7s: ", my_thread_name()); +#else + (void) fprintf(cs->stack->out_file, "%5d: ", (int) getpid()); +#endif + } + if (cs->stack->flags & NUMBER_ON) + (void) fprintf(cs->stack->out_file, "%5d: ", cs->lineno); + if (cs->stack->flags & TIMESTAMP_ON) + { +#ifdef __WIN__ + /* FIXME This doesn't give microseconds as in Unix case, and the resolution is + in system ticks, 10 ms intervals. See my_getsystime.c for high res */ + SYSTEMTIME loc_t; + GetLocalTime(&loc_t); + (void) fprintf (cs->stack->out_file, + /* "%04d-%02d-%02d " */ + "%02d:%02d:%02d.%06d ", + /*tm_p->tm_year + 1900, tm_p->tm_mon + 1, tm_p->tm_mday,*/ + loc_t.wHour, loc_t.wMinute, loc_t.wSecond, loc_t.wMilliseconds); +#else + struct timeval tv; + struct tm *tm_p; + if (gettimeofday(&tv, NULL) != -1) + { + if ((tm_p= localtime((const time_t *)&tv.tv_sec))) + { + (void) fprintf (cs->stack->out_file, + /* "%04d-%02d-%02d " */ + "%02d:%02d:%02d.%06d ", + /*tm_p->tm_year + 1900, tm_p->tm_mon + 1, tm_p->tm_mday,*/ + tm_p->tm_hour, tm_p->tm_min, tm_p->tm_sec, + (int) (tv.tv_usec)); + } + } +#endif + } + if (cs->stack->flags & PROCESS_ON) + (void) fprintf(cs->stack->out_file, "%s: ", cs->process); + if (cs->stack->flags & FILE_ON) + (void) fprintf(cs->stack->out_file, "%14s: ", BaseName(cs->file)); + if (cs->stack->flags & LINE_ON) + (void) fprintf(cs->stack->out_file, "%5d: ", _line_); + if (cs->stack->flags & DEPTH_ON) + (void) fprintf(cs->stack->out_file, "%4d: ", cs->level); +} + + +/* + * FUNCTION + * + * DBUGOpenFile open new output stream for debugger output + * + * SYNOPSIS + * + * static VOID DBUGOpenFile(name) + * char *name; + * + * DESCRIPTION + * + * Given name of a new file (or "-" for stdout) opens the file + * and sets the output stream to the new file. + * + */ + +static void DBUGOpenFile(CODE_STATE *cs, + const char *name,const char *end,int append) +{ + REGISTER FILE *fp; + REGISTER BOOLEAN newfile; + + if (name != NULL) + { + if (end) + { + size_t len=end-name; + memcpy(cs->stack->name, name, len); + cs->stack->name[len]=0; + } + else + strmov(cs->stack->name,name); + name=cs->stack->name; + if (strcmp(name, "-") == 0) + { + cs->stack->out_file= stdout; + cs->stack->flags |= FLUSH_ON_WRITE; + cs->stack->name[0]=0; + } + else + { + if (!Writable(name)) + { + (void) fprintf(stderr, ERR_OPEN, cs->process, name); + perror(""); + fflush(stderr); + } + else + { + newfile= !EXISTS(name); + if (!(fp= fopen(name, append ? "a+" : "w"))) + { + (void) fprintf(stderr, ERR_OPEN, cs->process, name); + perror(""); + fflush(stderr); + } + else + { + cs->stack->out_file= fp; + if (newfile) + { + ChangeOwner(cs, name); + } + } + } + } + } +} + + +/* + * FUNCTION + * + * OpenProfile open new output stream for profiler output + * + * SYNOPSIS + * + * static FILE *OpenProfile(name) + * char *name; + * + * DESCRIPTION + * + * Given name of a new file, opens the file + * and sets the profiler output stream to the new file. + * + * It is currently unclear whether the prefered behavior is + * to truncate any existing file, or simply append to it. + * The latter behavior would be desirable for collecting + * accumulated runtime history over a number of separate + * runs. It might take some changes to the analyzer program + * though, and the notes that Binayak sent with the profiling + * diffs indicated that append was the normal mode, but this + * does not appear to agree with the actual code. I haven't + * investigated at this time [fnf; 24-Jul-87]. + */ + +#ifndef THREAD +static FILE *OpenProfile(CODE_STATE *cs, const char *name) +{ + REGISTER FILE *fp; + REGISTER BOOLEAN newfile; + + fp=0; + if (!Writable(name)) + { + (void) fprintf(cs->stack->out_file, ERR_OPEN, cs->process, name); + perror(""); + (void) Delay(cs->stack->delay); + } + else + { + newfile= !EXISTS(name); + if (!(fp= fopen(name, "w"))) + { + (void) fprintf(cs->stack->out_file, ERR_OPEN, cs->process, name); + perror(""); + } + else + { + cs->stack->prof_file= fp; + if (newfile) + { + ChangeOwner(cs, name); + } + } + } + return fp; +} +#endif + +/* + * FUNCTION + * + * DBUGCloseFile close the debug output stream + * + * SYNOPSIS + * + * static VOID DBUGCloseFile(fp) + * FILE *fp; + * + * DESCRIPTION + * + * Closes the debug output stream unless it is standard output + * or standard error. + * + */ + +static void DBUGCloseFile(CODE_STATE *cs, FILE *fp) +{ + if (fp && fp != stderr && fp != stdout && fclose(fp) == EOF) + { + pthread_mutex_lock(&THR_LOCK_dbug); + (void) fprintf(cs->stack->out_file, ERR_CLOSE, cs->process); + perror(""); + DbugFlush(cs); + } +} + + +/* + * FUNCTION + * + * DbugExit print error message and exit + * + * SYNOPSIS + * + * static VOID DbugExit(why) + * char *why; + * + * DESCRIPTION + * + * Prints error message using current process name, the reason for + * aborting (typically out of memory), and exits with status 1. + * This should probably be changed to use a status code + * defined in the user's debugger include file. + * + */ + +static void DbugExit(const char *why) +{ + CODE_STATE *cs=code_state(); + (void) fprintf(stderr, ERR_ABORT, cs ? cs->process : "(null)", why); + (void) fflush(stderr); + exit(1); +} + + +/* + * FUNCTION + * + * DbugMalloc allocate memory for debugger runtime support + * + * SYNOPSIS + * + * static long *DbugMalloc(size) + * int size; + * + * DESCRIPTION + * + * Allocate more memory for debugger runtime support functions. + * Failure to to allocate the requested number of bytes is + * immediately fatal to the current process. This may be + * rather unfriendly behavior. It might be better to simply + * print a warning message, freeze the current debugger cs, + * and continue execution. + * + */ + +static char *DbugMalloc(size_t size) +{ + register char *new_malloc; + + if (!(new_malloc= (char*) malloc(size))) + DbugExit("out of memory"); + return new_malloc; +} + + +/* + * strtok lookalike - splits on ':', magically handles ::, :\ and :/ + */ + +static const char *DbugStrTok(const char *s) +{ + while (s[0] && (s[0] != ':' || + (s[1] == '\\' || s[1] == '/' || (s[1] == ':' && s++)))) + s++; + return s; +} + + +/* + * FUNCTION + * + * BaseName strip leading pathname components from name + * + * SYNOPSIS + * + * static char *BaseName(pathname) + * char *pathname; + * + * DESCRIPTION + * + * Given pointer to a complete pathname, locates the base file + * name at the end of the pathname and returns a pointer to + * it. + * + */ + +static const char *BaseName(const char *pathname) +{ + register const char *base; + + base= strrchr(pathname, FN_LIBCHAR); + if (base++ == NullS) + base= pathname; + return base; +} + + +/* + * FUNCTION + * + * Writable test to see if a pathname is writable/creatable + * + * SYNOPSIS + * + * static BOOLEAN Writable(pathname) + * char *pathname; + * + * DESCRIPTION + * + * Because the debugger might be linked in with a program that + * runs with the set-uid-bit (suid) set, we have to be careful + * about opening a user named file for debug output. This consists + * of checking the file for write access with the real user id, + * or checking the directory where the file will be created. + * + * Returns TRUE if the user would normally be allowed write or + * create access to the named file. Returns FALSE otherwise. + * + */ + + +#ifndef Writable + +static BOOLEAN Writable(const char *pathname) +{ + REGISTER BOOLEAN granted; + REGISTER char *lastslash; + + granted= FALSE; + if (EXISTS(pathname)) + { + if (WRITABLE(pathname)) + granted= TRUE; + } + else + { + lastslash= strrchr(pathname, '/'); + if (lastslash != NULL) + *lastslash= '\0'; + else + pathname= "."; + if (WRITABLE(pathname)) + granted= TRUE; + if (lastslash != NULL) + *lastslash= '/'; + } + return granted; +} +#endif + + +/* + * FUNCTION + * + * ChangeOwner change owner to real user for suid programs + * + * SYNOPSIS + * + * static VOID ChangeOwner(pathname) + * + * DESCRIPTION + * + * For unix systems, change the owner of the newly created debug + * file to the real owner. This is strictly for the benefit of + * programs that are running with the set-user-id bit set. + * + * Note that at this point, the fact that pathname represents + * a newly created file has already been established. If the + * program that the debugger is linked to is not running with + * the suid bit set, then this operation is redundant (but + * harmless). + * + */ + +#ifndef ChangeOwner +static void ChangeOwner(CODE_STATE *cs, char *pathname) +{ + if (chown(pathname, getuid(), getgid()) == -1) + { + (void) fprintf(stderr, ERR_CHOWN, cs->process, pathname); + perror(""); + (void) fflush(stderr); + } +} +#endif + + +/* + * FUNCTION + * + * _db_setjmp_ save debugger environment + * + * SYNOPSIS + * + * VOID _db_setjmp_() + * + * DESCRIPTION + * + * Invoked as part of the user's DBUG_SETJMP macro to save + * the debugger environment in parallel with saving the user's + * environment. + * + */ + +#ifdef HAVE_LONGJMP + +EXPORT void _db_setjmp_() +{ + CODE_STATE *cs; + get_code_state_or_return; + + cs->jmplevel= cs->level; + cs->jmpfunc= cs->func; + cs->jmpfile= cs->file; +} + +/* + * FUNCTION + * + * _db_longjmp_ restore previously saved debugger environment + * + * SYNOPSIS + * + * VOID _db_longjmp_() + * + * DESCRIPTION + * + * Invoked as part of the user's DBUG_LONGJMP macro to restore + * the debugger environment in parallel with restoring the user's + * previously saved environment. + * + */ + +EXPORT void _db_longjmp_() +{ + CODE_STATE *cs; + get_code_state_or_return; + + cs->level= cs->jmplevel; + if (cs->jmpfunc) + cs->func= cs->jmpfunc; + if (cs->jmpfile) + cs->file= cs->jmpfile; +} +#endif + +/* + * FUNCTION + * + * perror perror simulation for systems that don't have it + * + * SYNOPSIS + * + * static VOID perror(s) + * char *s; + * + * DESCRIPTION + * + * Perror produces a message on the standard error stream which + * provides more information about the library or system error + * just encountered. The argument string s is printed, followed + * by a ':', a blank, and then a message and a newline. + * + * An undocumented feature of the unix perror is that if the string + * 's' is a null string (NOT a NULL pointer!), then the ':' and + * blank are not printed. + * + * This version just complains about an "unknown system error". + * + */ + +#ifndef HAVE_PERROR +static void perror(s) +char *s; +{ + if (s && *s != '\0') + (void) fprintf(stderr, "%s: ", s); + (void) fprintf(stderr, "<unknown system error>\n"); +} +#endif /* HAVE_PERROR */ + + + /* flush dbug-stream, free mutex lock & wait delay */ + /* This is because some systems (MSDOS!!) dosn't flush fileheader */ + /* and dbug-file isn't readable after a system crash !! */ + +static void DbugFlush(CODE_STATE *cs) +{ + if (cs->stack->flags & FLUSH_ON_WRITE) + { + (void) fflush(cs->stack->out_file); + if (cs->stack->delay) + (void) Delay(cs->stack->delay); + } + if (!cs->locked) + pthread_mutex_unlock(&THR_LOCK_dbug); +} /* DbugFlush */ + + +/* For debugging */ + +void _db_flush_() +{ + CODE_STATE *cs; + get_code_state_or_return; + (void) fflush(cs->stack->out_file); +} + + +void _db_lock_file_() +{ + CODE_STATE *cs; + get_code_state_or_return; + pthread_mutex_lock(&THR_LOCK_dbug); + cs->locked=1; +} + +void _db_unlock_file_() +{ + CODE_STATE *cs; + get_code_state_or_return; + cs->locked=0; + pthread_mutex_unlock(&THR_LOCK_dbug); +} + +/* + * Here we need the definitions of the clock routine. Add your + * own for whatever system that you have. + */ + +#ifndef THREAD +#if defined(HAVE_GETRUSAGE) + +#include <sys/param.h> +#include <sys/resource.h> + +/* extern int getrusage(int, struct rusage *); */ + +/* + * Returns the user time in milliseconds used by this process so + * far. + */ + +static unsigned long Clock() +{ + struct rusage ru; + + (void) getrusage(RUSAGE_SELF, &ru); + return ru.ru_utime.tv_sec*1000 + ru.ru_utime.tv_usec/1000; +} + +#elif defined(MSDOS) || defined(__WIN__) + +static ulong Clock() +{ + return clock()*(1000/CLOCKS_PER_SEC); +} +#elif defined(amiga) + +struct DateStamp { /* Yes, this is a hack, but doing it right */ + long ds_Days; /* is incredibly ugly without splitting this */ + long ds_Minute; /* off into a separate file */ + long ds_Tick; +}; + +static int first_clock= TRUE; +static struct DateStamp begin; +static struct DateStamp elapsed; + +static unsigned long Clock() +{ + register struct DateStamp *now; + register unsigned long millisec= 0; + extern VOID *AllocMem(); + + now= (struct DateStamp *) AllocMem((long) sizeof(struct DateStamp), 0L); + if (now != NULL) + { + if (first_clock == TRUE) + { + first_clock= FALSE; + (void) DateStamp(now); + begin= *now; + } + (void) DateStamp(now); + millisec= 24 * 3600 * (1000 / HZ) * (now->ds_Days - begin.ds_Days); + millisec += 60 * (1000 / HZ) * (now->ds_Minute - begin.ds_Minute); + millisec += (1000 / HZ) * (now->ds_Tick - begin.ds_Tick); + (void) FreeMem(now, (long) sizeof(struct DateStamp)); + } + return millisec; +} +#else +static unsigned long Clock() +{ + return 0; +} +#endif /* RUSAGE */ +#endif /* THREADS */ + +#ifdef NO_VARARGS + +/* + * Fake vfprintf for systems that don't support it. If this + * doesn't work, you are probably SOL... + */ + +static int vfprintf(stream, format, ap) +FILE *stream; +char *format; +va_list ap; +{ + int rtnval; + ARGS_DCL; + + ARG0= va_arg(ap, ARGS_TYPE); + ARG1= va_arg(ap, ARGS_TYPE); + ARG2= va_arg(ap, ARGS_TYPE); + ARG3= va_arg(ap, ARGS_TYPE); + ARG4= va_arg(ap, ARGS_TYPE); + ARG5= va_arg(ap, ARGS_TYPE); + ARG6= va_arg(ap, ARGS_TYPE); + ARG7= va_arg(ap, ARGS_TYPE); + ARG8= va_arg(ap, ARGS_TYPE); + ARG9= va_arg(ap, ARGS_TYPE); + rtnval= fprintf(stream, format, ARGS_LIST); + return rtnval; +} + +#endif /* NO_VARARGS */ + +#else + +/* + * Dummy function, workaround for MySQL bug#14420 related + * build failure on a platform where linking with an empty + * archive fails. + * + * This block can be removed as soon as a fix for bug#14420 + * is implemented. + */ +int i_am_a_dummy_function() { + return 0; +} + +#endif diff --git a/externals/mysql/extlib/dbug/dbug_analyze.c b/externals/mysql/extlib/dbug/dbug_analyze.c new file mode 100644 index 00000000000..3263b2ccc59 --- /dev/null +++ b/externals/mysql/extlib/dbug/dbug_analyze.c @@ -0,0 +1,726 @@ +/* + * Analyze the profile file (cmon.out) written out by the dbug + * routines with profiling enabled. + * + * Copyright June 1987, Binayak Banerjee + * All rights reserved. + * + * This program may be freely distributed under the same terms and + * conditions as Fred Fish's Dbug package. + * + * Compile with -- cc -O -s -o %s analyze.c + * + * Analyze will read an trace file created by the dbug package + * (when run with traceing enabled). It will then produce a + * summary on standard output listing the name of each traced + * function, the number of times it was called, the percentage + * of total calls, the time spent executing the function, the + * proportion of the total time and the 'importance'. The last + * is a metric which is obtained by multiplying the proportions + * of calls and the proportions of time for each function. The + * greater the importance, the more likely it is that a speedup + * could be obtained by reducing the time taken by that function. + * + * Note that the timing values that you obtain are only rough + * measures. The overhead of the dbug package is included + * within. However, there is no need to link in special profiled + * libraries and the like. + * + * CHANGES: + * + * 2-Mar-89: fnf + * Changes to support tracking of stack usage. This required + * reordering the fields in the profile log file to make + * parsing of different record types easier. Corresponding + * changes made in dbug runtime library. Also used this + * opportunity to reformat the code more to my liking (my + * apologies to Binayak Banerjee for "uglifying" his code). + * + * 24-Jul-87: fnf + * Because I tend to use functions names like + * "ExternalFunctionDoingSomething", I've rearranged the + * printout to put the function name last in each line, so + * long names don't screw up the formatting unless they are + * *very* long and wrap around the screen width... + * + * 24-Jul-87: fnf + * Modified to put out table very similar to Unix profiler + * by default, but also puts out original verbose table + * if invoked with -v flag. + */ + +#include <my_global.h> +#include <m_string.h> +#include <my_pthread.h> + +static char *my_name; +static int verbose; + +/* + * Structure of the stack. + */ + +#define PRO_FILE "dbugmon.out" /* Default output file name */ +#define STACKSIZ 100 /* Maximum function nesting */ +#define MAXPROCS 10000 /* Maximum number of function calls */ + +# ifdef BSD +# include <sysexits.h> +# else +# define EX_SOFTWARE 1 +# define EX_DATAERR 1 +# define EX_USAGE 1 +# define EX_OSERR 1 +# define EX_IOERR 1 +#ifndef EX_OK +# define EX_OK 0 +#endif +# endif + +#define __MERF_OO_ "%s: Malloc Failed in %s: %d\n" + +#define MALLOC(Ptr,Num,Typ) do /* Malloc w/error checking & exit */ \ + if (!(Ptr = (Typ *)malloc((Num)*(sizeof(Typ))))) \ + {fprintf(stderr,__MERF_OO_,my_name,__FILE__,__LINE__);\ + exit(EX_OSERR);} while(0) + +#define Malloc(Ptr,Num,Typ) do /* Weaker version of above */\ + if (!(Ptr = (Typ *)malloc((Num)*(sizeof(Typ))))) \ + fprintf(stderr,__MERF_OO_,my_name,__FILE__,__LINE__);\ + while(0) + +#define FILEOPEN(Fp,Fn,Mod) do /* File open with error exit */ \ + if (!(Fp = fopen(Fn,Mod)))\ + {fprintf(stderr,"%s: Couldn't open %s\n",my_name,Fn);\ + exit(EX_IOERR);} while(0) + +#define Fileopen(Fp,Fn,Mod) do /* Weaker version of above */ \ + if(!(Fp = fopen(Fn,Mod))) \ + fprintf(stderr,"%s: Couldn't open %s\n",my_name,Fn);\ + while(0) + + +struct stack_t { + unsigned int pos; /* which function? */ + unsigned long time; /* Time that this was entered */ + unsigned long children; /* Time spent in called funcs */ +}; + +static struct stack_t fn_stack[STACKSIZ+1]; + +static unsigned int stacktop = 0; /* Lowest stack position is a dummy */ + +static unsigned long tot_time = 0; +static unsigned long tot_calls = 0; +static unsigned long highstack = 0; +static unsigned long lowstack = (ulong) ~0; + +/* + * top() returns a pointer to the top item on the stack. + * (was a function, now a macro) + */ + +#define top() &fn_stack[stacktop] + +/* + * Push - Push the given record on the stack. + */ + +void push (name_pos, time_entered) +register unsigned int name_pos; +register unsigned long time_entered; +{ + register struct stack_t *t; + + DBUG_ENTER("push"); + if (++stacktop > STACKSIZ) { + fprintf (DBUG_FILE,"%s: stack overflow (%s:%d)\n", + my_name, __FILE__, __LINE__); + exit (EX_SOFTWARE); + } + DBUG_PRINT ("push", ("%d %ld",name_pos,time_entered)); + t = &fn_stack[stacktop]; + t -> pos = name_pos; + t -> time = time_entered; + t -> children = 0; + DBUG_VOID_RETURN; +} + +/* + * Pop - pop the top item off the stack, assigning the field values + * to the arguments. Returns 0 on stack underflow, or on popping first + * item off stack. + */ + +unsigned int pop (name_pos, time_entered, child_time) +register unsigned int *name_pos; +register unsigned long *time_entered; +register unsigned long *child_time; +{ + register struct stack_t *temp; + register unsigned int rtnval; + + DBUG_ENTER ("pop"); + + if (stacktop < 1) { + rtnval = 0; + } else { + temp = &fn_stack[stacktop]; + *name_pos = temp->pos; + *time_entered = temp->time; + *child_time = temp->children; + DBUG_PRINT ("pop", ("%d %lu %lu",*name_pos,*time_entered,*child_time)); + rtnval = stacktop--; + } + DBUG_RETURN (rtnval); +} + +/* + * We keep the function info in another array (serves as a simple + * symbol table) + */ + +struct module_t { + char *name; + unsigned long m_time; + unsigned long m_calls; + unsigned long m_stkuse; +}; + +static struct module_t modules[MAXPROCS]; + +/* + * We keep a binary search tree in order to look up function names + * quickly (and sort them at the end. + */ + +struct bnode { + unsigned int lchild; /* Index of left subtree */ + unsigned int rchild; /* Index of right subtree */ + unsigned int pos; /* Index of module_name entry */ +}; + +static struct bnode s_table[MAXPROCS]; + +static unsigned int n_items = 0; /* No. of items in the array so far */ + +/* + * Need a function to allocate space for a string and squirrel it away. + */ + +char *strsave (s) +char *s; +{ + register char *retval; + register unsigned int len; + + DBUG_ENTER ("strsave"); + DBUG_PRINT ("strsave", ("%s",s)); + if (!s || (len = strlen (s)) == 0) { + DBUG_RETURN (0); + } + MALLOC (retval, ++len, char); + strcpy (retval, s); + DBUG_RETURN (retval); +} + +/* + * add() - adds m_name to the table (if not already there), and returns + * the index of its location in the table. Checks s_table (which is a + * binary search tree) to see whether or not it should be added. + */ + +unsigned int add (m_name) +char *m_name; +{ + register unsigned int ind = 0; + register int cmp; + + DBUG_ENTER ("add"); + if (n_items == 0) { /* First item to be added */ + s_table[0].pos = ind; + s_table[0].lchild = s_table[0].rchild = MAXPROCS; + addit: + modules[n_items].name = strsave (m_name); + modules[n_items].m_time = 0; + modules[n_items].m_calls = 0; + modules[n_items].m_stkuse = 0; + DBUG_RETURN (n_items++); + } + while ((cmp = strcmp (m_name,modules[ind].name))) { + if (cmp < 0) { /* In left subtree */ + if (s_table[ind].lchild == MAXPROCS) { + /* Add as left child */ + if (n_items >= MAXPROCS) { + fprintf (DBUG_FILE, + "%s: Too many functions being profiled\n", + my_name); + exit (EX_SOFTWARE); + } + s_table[n_items].pos = s_table[ind].lchild = n_items; + s_table[n_items].lchild = s_table[n_items].rchild = MAXPROCS; +#ifdef notdef + modules[n_items].name = strsave (m_name); + modules[n_items].m_time = modules[n_items].m_calls = 0; + DBUG_RETURN (n_items++); +#else + goto addit; +#endif + + } + ind = s_table[ind].lchild; /* else traverse l-tree */ + } else { + if (s_table[ind].rchild == MAXPROCS) { + /* Add as right child */ + if (n_items >= MAXPROCS) { + fprintf (DBUG_FILE, + "%s: Too many functions being profiled\n", + my_name); + exit (EX_SOFTWARE); + } + s_table[n_items].pos = s_table[ind].rchild = n_items; + s_table[n_items].lchild = s_table[n_items].rchild = MAXPROCS; +#ifdef notdef + modules[n_items].name = strsave (m_name); + modules[n_items].m_time = modules[n_items].m_calls = 0; + DBUG_RETURN (n_items++); +#else + goto addit; +#endif + + } + ind = s_table[ind].rchild; /* else traverse r-tree */ + } + } + DBUG_RETURN (ind); +} + +/* + * process() - process the input file, filling in the modules table. + */ + +void process (inf) +FILE *inf; +{ + char buf[BUFSIZ]; + char fn_name[64]; /* Max length of fn_name */ + unsigned long fn_time; + unsigned long fn_sbot; + unsigned long fn_ssz; + unsigned long lastuse; + unsigned int pos; + unsigned long local_time; + unsigned int oldpos; + unsigned long oldtime; + unsigned long oldchild; + struct stack_t *t; + + DBUG_ENTER ("process"); + while (fgets (buf,BUFSIZ,inf) != NULL) { + switch (buf[0]) { + case 'E': + sscanf (buf+2, "%ld %64s", &fn_time, fn_name); + DBUG_PRINT ("erec", ("%ld %s", fn_time, fn_name)); + pos = add (fn_name); + push (pos, fn_time); + break; + case 'X': + sscanf (buf+2, "%ld %64s", &fn_time, fn_name); + DBUG_PRINT ("xrec", ("%ld %s", fn_time, fn_name)); + pos = add (fn_name); + /* + * An exited function implies that all stacked + * functions are also exited, until the matching + * function is found on the stack. + */ + while (pop (&oldpos, &oldtime, &oldchild)) { + DBUG_PRINT ("popped", ("%lu %lu", oldtime, oldchild)); + local_time = fn_time - oldtime; + t = top (); + t -> children += local_time; + DBUG_PRINT ("update", ("%s", modules[t -> pos].name)); + DBUG_PRINT ("update", ("%lu", t -> children)); + local_time -= oldchild; + modules[oldpos].m_time += local_time; + modules[oldpos].m_calls++; + tot_time += local_time; + tot_calls++; + if (pos == oldpos) { + goto next_line; /* Should be a break2 */ + } + } + /* + * Assume that item seen started at time 0. + * (True for function main). But initialize + * it so that it works the next time too. + */ + t = top (); + local_time = fn_time - t -> time - t -> children; + t -> time = fn_time; t -> children = 0; + modules[pos].m_time += local_time; + modules[pos].m_calls++; + tot_time += local_time; + tot_calls++; + break; + case 'S': + sscanf (buf+2, "%lx %lx %64s", &fn_sbot, &fn_ssz, fn_name); + DBUG_PRINT ("srec", ("%lx %lx %s", fn_sbot, fn_ssz, fn_name)); + pos = add (fn_name); + lastuse = modules[pos].m_stkuse; +#if 0 + /* + * Needs further thought. Stack use is determined by + * difference in stack between two functions with DBUG_ENTER + * macros. If A calls B calls C, where A and C have the + * macros, and B doesn't, then B's stack use will be lumped + * in with either A's or C's. If somewhere else A calls + * C directly, the stack use will seem to change. Just + * take the biggest for now... + */ + if (lastuse > 0 && lastuse != fn_ssz) { + fprintf (stderr, + "warning - %s stack use changed (%lx to %lx)\n", + fn_name, lastuse, fn_ssz); + } +#endif + if (fn_ssz > lastuse) { + modules[pos].m_stkuse = fn_ssz; + } + if (fn_sbot > highstack) { + highstack = fn_sbot; + } else if (fn_sbot < lowstack) { + lowstack = fn_sbot; + } + break; + default: + fprintf (stderr, "unknown record type '%c'\n", buf[0]); + break; + } + next_line:; + } + + /* + * Now, we've hit eof. If we still have stuff stacked, then we + * assume that the user called exit, so give everything the exited + * time of fn_time. + */ + while (pop (&oldpos,&oldtime,&oldchild)) { + local_time = fn_time - oldtime; + t = top (); + t -> children += local_time; + local_time -= oldchild; + modules[oldpos].m_time += local_time; + modules[oldpos].m_calls++; + tot_time += local_time; + tot_calls++; + } + DBUG_VOID_RETURN; +} + +/* + * out_header () -- print out the header of the report. + */ + +void out_header (outf) +FILE *outf; +{ + DBUG_ENTER ("out_header"); + if (verbose) { + fprintf (outf, "Profile of Execution\n"); + fprintf (outf, "Execution times are in milliseconds\n\n"); + fprintf (outf, " Calls\t\t\t Time\n"); + fprintf (outf, " -----\t\t\t ----\n"); + fprintf (outf, "Times\tPercentage\tTime Spent\tPercentage\n"); + fprintf (outf, "Called\tof total\tin Function\tof total Importance\tFunction\n"); + fprintf (outf, "======\t==========\t===========\t========== ==========\t========\t\n"); + } else { + fprintf (outf, "%ld bytes of stack used, from %lx down to %lx\n\n", + highstack - lowstack, highstack, lowstack); + fprintf (outf, + " %%time sec #call ms/call %%calls weight stack name\n"); + } + DBUG_VOID_RETURN; +} + +/* + * out_trailer () - writes out the summary line of the report. + */ + +void out_trailer (outf,sum_calls,sum_time) +FILE *outf; +unsigned long int sum_calls, sum_time; +{ + DBUG_ENTER ("out_trailer"); + if (verbose) + { + fprintf(outf, "======\t==========\t===========\t==========\t========\n"); + fprintf(outf, "%6ld\t%10.2f\t%11ld\t%10.2f\t\t%-15s\n", + sum_calls, 100.0, sum_time, 100.0, "Totals"); + } + DBUG_VOID_RETURN; +} + +/* + * out_item () - prints out the output line for a single entry, + * and sets the calls and time fields appropriately. + */ + +void out_item (outf, m,called,timed) +FILE *outf; +register struct module_t *m; +unsigned long int *called, *timed; +{ + char *name = m -> name; + register unsigned int calls = m -> m_calls; + register unsigned long local_time = m -> m_time; + register unsigned long stkuse = m -> m_stkuse; + unsigned int import; + double per_time = 0.0; + double per_calls = 0.0; + double ms_per_call, local_ftime; + + DBUG_ENTER ("out_item"); + + if (tot_time > 0) { + per_time = (double) (local_time * 100) / (double) tot_time; + } + if (tot_calls > 0) { + per_calls = (double) (calls * 100) / (double) tot_calls; + } + import = (unsigned int) (per_time * per_calls); + + if (verbose) { + fprintf (outf, "%6d\t%10.2f\t%11ld\t%10.2f %10d\t%-15s\n", + calls, per_calls, local_time, per_time, import, name); + } else { + ms_per_call = local_time; + ms_per_call /= calls; + local_ftime = local_time; + local_ftime /= 1000; + fprintf(outf, "%8.2f%8.3f%8u%8.3f%8.2f%8u%8lu %-s\n", + per_time, local_ftime, calls, ms_per_call, per_calls, import, + stkuse, name); + } + *called = calls; + *timed = local_time; + DBUG_VOID_RETURN; +} + +/* + * out_body (outf, root,s_calls,s_time) -- Performs an inorder traversal + * on the binary search tree (root). Calls out_item to actually print + * the item out. + */ + +void out_body (outf, root,s_calls,s_time) +FILE *outf; +register unsigned int root; +register unsigned long int *s_calls, *s_time; +{ + unsigned long int calls, local_time; + + DBUG_ENTER ("out_body"); + DBUG_PRINT ("out_body", ("%lu,%lu",*s_calls,*s_time)); + if (root == MAXPROCS) { + DBUG_PRINT ("out_body", ("%lu,%lu",*s_calls,*s_time)); + } else { + while (root != MAXPROCS) { + out_body (outf, s_table[root].lchild,s_calls,s_time); + out_item (outf, &modules[s_table[root].pos],&calls,&local_time); + DBUG_PRINT ("out_body", ("-- %lu -- %lu --", calls, local_time)); + *s_calls += calls; + *s_time += local_time; + root = s_table[root].rchild; + } + DBUG_PRINT ("out_body", ("%lu,%lu", *s_calls, *s_time)); + } + DBUG_VOID_RETURN; +} + +/* + * output () - print out a nice sorted output report on outf. + */ + +void output (outf) +FILE *outf; +{ + unsigned long int sum_calls = 0; + unsigned long int sum_time = 0; + + DBUG_ENTER ("output"); + if (n_items == 0) { + fprintf (outf, "%s: No functions to trace\n", my_name); + exit (EX_DATAERR); + } + out_header (outf); + out_body (outf, 0,&sum_calls,&sum_time); + out_trailer (outf, sum_calls,sum_time); + DBUG_VOID_RETURN; +} + + +#define usage() fprintf (DBUG_FILE,"Usage: %s [-v] [prof-file]\n",my_name) + +#ifdef MSDOS +extern int getopt(int argc, char **argv, char *opts); +#endif +extern int optind; +extern char *optarg; + +int main (int argc, char **argv) +{ + register int c; + int badflg = 0; + FILE *infile; + FILE *outfile = {stdout}; + +#ifdef THREAD +#if defined(HAVE_PTHREAD_INIT) + pthread_init(); /* Must be called before DBUG_ENTER */ +#endif + my_thread_global_init(); +#endif /* THREAD */ + { + DBUG_ENTER ("main"); + DBUG_PROCESS (argv[0]); + my_name = argv[0]; + while ((c = getopt (argc,argv,"#:v")) != EOF) { + switch (c) { + case '#': /* Debugging Macro enable */ + DBUG_PUSH (optarg); + break; + case 'v': /* Verbose mode */ + verbose++; + break; + default: + badflg++; + break; + } + } + if (badflg) { + usage (); + DBUG_RETURN (EX_USAGE); + } + if (optind < argc) { + FILEOPEN (infile, argv[optind], "r"); + } else { + FILEOPEN (infile, PRO_FILE, "r"); + } + process (infile); + output (outfile); + DBUG_RETURN (EX_OK); +} +} + +#ifdef MSDOS + +/* + * From std-unix@ut-sally.UUCP (Moderator, John Quarterman) Sun Nov 3 14:34:15 1985 + * Relay-Version: version B 2.10.3 4.3bsd-beta 6/6/85; site gatech.CSNET + * Posting-Version: version B 2.10.2 9/18/84; site ut-sally.UUCP + * Path: gatech!akgua!mhuxv!mhuxt!mhuxr!ulysses!allegra!mit-eddie!genrad!panda!talcott!harvard!seismo!ut-sally!std-unix + * From: std-unix@ut-sally.UUCP (Moderator, John Quarterman) + * Newsgroups: mod.std.unix + * Subject: public domain AT&T getopt source + * Message-ID: <3352@ut-sally.UUCP> + * Date: 3 Nov 85 19:34:15 GMT + * Date-Received: 4 Nov 85 12:25:09 GMT + * Organization: IEEE/P1003 Portable Operating System Environment Committee + * Lines: 91 + * Approved: jsq@ut-sally.UUCP + * + * Here's something you've all been waiting for: the AT&T public domain + * source for getopt(3). It is the code which was given out at the 1985 + * UNIFORUM conference in Dallas. I obtained it by electronic mail + * directly from AT&T. The people there assure me that it is indeed + * in the public domain. + * + * There is no manual page. That is because the one they gave out at + * UNIFORUM was slightly different from the current System V Release 2 + * manual page. The difference apparently involved a note about the + * famous rules 5 and 6, recommending using white space between an option + * and its first argument, and not grouping options that have arguments. + * Getopt itself is currently lenient about both of these things White + * space is allowed, but not mandatory, and the last option in a group can + * have an argument. That particular version of the man page evidently + * has no official existence, and my source at AT&T did not send a copy. + * The current SVR2 man page reflects the actual behavor of this getopt. + * However, I am not about to post a copy of anything licensed by AT&T. + * + * I will submit this source to Berkeley as a bug fix. + * + * I, personally, make no claims or guarantees of any kind about the + * following source. I did compile it to get some confidence that + * it arrived whole, but beyond that you're on your own. + * + */ + +/*LINTLIBRARY*/ + +int opterr = 1; +int optind = 1; +int optopt; +char *optarg; + +static void _ERR(s,c,argv) +char *s; +int c; +char *argv[]; +{ + char errbuf[3]; + + if (opterr) { + errbuf[0] = c; + errbuf[1] = '\n'; + (void) fprintf(stderr, "%s", argv[0]); + (void) fprintf(stderr, "%s", s); + (void) fprintf(stderr, "%s", errbuf); + } +} + +int getopt(argc, argv, opts) +int argc; +char **argv, *opts; +{ + static int sp = 1; + register int c; + register char *cp; + + if(sp == 1) + if(optind >= argc || + argv[optind][0] != '-' || argv[optind][1] == '\0') + return(EOF); + else if(strcmp(argv[optind], "--") == 0) { + optind++; + return(EOF); + } + optopt = c = argv[optind][sp]; + if(c == ':' || (cp=strchr(opts, c)) == NULL) { + _ERR(": illegal option -- ", c, argv); + if(argv[optind][++sp] == '\0') { + optind++; + sp = 1; + } + return('?'); + } + if(*++cp == ':') { + if(argv[optind][sp+1] != '\0') + optarg = &argv[optind++][sp+1]; + else if(++optind >= argc) { + _ERR(": option requires an argument -- ", c, argv); + sp = 1; + return('?'); + } else + optarg = argv[optind++]; + sp = 1; + } else { + if(argv[optind][++sp] == '\0') { + sp = 1; + optind++; + } + optarg = NULL; + } + return(c); +} + +#endif /* !unix && !xenix */ diff --git a/externals/mysql/extlib/dbug/dbug_long.h b/externals/mysql/extlib/dbug/dbug_long.h new file mode 100644 index 00000000000..829df181ef1 --- /dev/null +++ b/externals/mysql/extlib/dbug/dbug_long.h @@ -0,0 +1,160 @@ +#error This file is not used in MySQL - see ../include/my_dbug.h instead +/****************************************************************************** + * * + * N O T I C E * + * * + * Copyright Abandoned, 1987, Fred Fish * + * * + * * + * This previously copyrighted work has been placed into the public * + * domain by the author and may be freely used for any purpose, * + * private or commercial. * + * * + * Because of the number of inquiries I was receiving about the use * + * of this product in commercially developed works I have decided to * + * simply make it public domain to further its unrestricted use. I * + * specifically would be most happy to see this material become a * + * part of the standard Unix distributions by AT&T and the Berkeley * + * Computer Science Research Group, and a standard part of the GNU * + * system from the Free Software Foundation. * + * * + * I would appreciate it, as a courtesy, if this notice is left in * + * all copies and derivative works. Thank you. * + * * + * The author makes no warranty of any kind with respect to this * + * product and explicitly disclaims any implied warranties of mer- * + * chantability or fitness for any particular purpose. * + * * + ****************************************************************************** + */ + +/* + * FILE + * + * dbug.h user include file for programs using the dbug package + * + * SYNOPSIS + * + * #include <local/dbug.h> + * + * SCCS ID + * + * @(#)dbug.h 1.13 7/17/89 + * + * DESCRIPTION + * + * Programs which use the dbug package must include this file. + * It contains the appropriate macros to call support routines + * in the dbug runtime library. + * + * To disable compilation of the macro expansions define the + * preprocessor symbol "DBUG_OFF". This will result in null + * macros expansions so that the resulting code will be smaller + * and faster. (The difference may be smaller than you think + * so this step is recommended only when absolutely necessary). + * In general, tradeoffs between space and efficiency are + * decided in favor of efficiency since space is seldom a + * problem on the new machines). + * + * All externally visible symbol names follow the pattern + * "_db_xxx..xx_" to minimize the possibility of a dbug package + * symbol colliding with a user defined symbol. + * + * The DBUG_<N> style macros are obsolete and should not be used + * in new code. Macros to map them to instances of DBUG_PRINT + * are provided for compatibility with older code. They may go + * away completely in subsequent releases. + * + * AUTHOR + * + * Fred Fish + * (Currently employed by Motorola Computer Division, Tempe, Az.) + * hao!noao!mcdsun!fnf + * (602) 438-3614 + * + */ + +/* + * Internally used dbug variables which must be global. + */ + +#ifndef DBUG_OFF + extern int _db_on_; /* TRUE if debug currently enabled */ + extern FILE *_db_fp_; /* Current debug output stream */ + extern char *_db_process_; /* Name of current process */ + extern int _db_keyword_ (); /* Accept/reject keyword */ + extern void _db_push_ (); /* Push state, set up new state */ + extern void _db_pop_ (); /* Pop previous debug state */ + extern void _db_enter_ (); /* New user function entered */ + extern void _db_return_ (); /* User function return */ + extern void _db_pargs_ (); /* Remember args for line */ + extern void _db_doprnt_ (); /* Print debug output */ + extern void _db_setjmp_ (); /* Save debugger environment */ + extern void _db_longjmp_ (); /* Restore debugger environment */ + extern void _db_dump_(); /* Dump memory */ +# endif + + +/* + * These macros provide a user interface into functions in the + * dbug runtime support library. They isolate users from changes + * in the MACROS and/or runtime support. + * + * The symbols "__LINE__" and "__FILE__" are expanded by the + * preprocessor to the current source file line number and file + * name respectively. + * + * WARNING --- Because the DBUG_ENTER macro allocates space on + * the user function's stack, it must precede any executable + * statements in the user function. + * + */ + +# ifdef DBUG_OFF +# define DBUG_ENTER(a1) +# define DBUG_RETURN(a1) return(a1) +# define DBUG_VOID_RETURN return +# define DBUG_EXECUTE(keyword,a1) +# define DBUG_PRINT(keyword,arglist) +# define DBUG_2(keyword,format) /* Obsolete */ +# define DBUG_3(keyword,format,a1) /* Obsolete */ +# define DBUG_4(keyword,format,a1,a2) /* Obsolete */ +# define DBUG_5(keyword,format,a1,a2,a3) /* Obsolete */ +# define DBUG_PUSH(a1) +# define DBUG_POP() +# define DBUG_PROCESS(a1) +# define DBUG_FILE (stderr) +# define DBUG_SETJMP setjmp +# define DBUG_LONGJMP longjmp +# define DBUG_DUMP(keyword,a1) +# else +# define DBUG_ENTER(a) \ + auto char *_db_func_; auto char *_db_file_; auto int _db_level_; \ + auto char **_db_framep_; \ + _db_enter_ (a,__FILE__,__LINE__,&_db_func_,&_db_file_,&_db_level_, \ + &_db_framep_) +# define DBUG_LEAVE \ + (_db_return_ (__LINE__, &_db_func_, &_db_file_, &_db_level_)) +# define DBUG_RETURN(a1) return (DBUG_LEAVE, (a1)) +/* define DBUG_RETURN(a1) {DBUG_LEAVE; return(a1);} Alternate form */ +# define DBUG_VOID_RETURN {DBUG_LEAVE; return;} +# define DBUG_EXECUTE(keyword,a1) \ + {if (_db_on_) {if (_db_keyword_ (keyword)) { a1 }}} +# define DBUG_PRINT(keyword,arglist) \ + {if (_db_on_) {_db_pargs_(__LINE__,keyword); _db_doprnt_ arglist;}} +# define DBUG_2(keyword,format) \ + DBUG_PRINT(keyword,(format)) /* Obsolete */ +# define DBUG_3(keyword,format,a1) \ + DBUG_PRINT(keyword,(format,a1)) /* Obsolete */ +# define DBUG_4(keyword,format,a1,a2) \ + DBUG_PRINT(keyword,(format,a1,a2)) /* Obsolete */ +# define DBUG_5(keyword,format,a1,a2,a3) \ + DBUG_PRINT(keyword,(format,a1,a2,a3)) /* Obsolete */ +# define DBUG_PUSH(a1) _db_push_ (a1) +# define DBUG_POP() _db_pop_ () +# define DBUG_PROCESS(a1) (_db_process_ = a1) +# define DBUG_FILE (_db_fp_) +# define DBUG_SETJMP(a1) (_db_setjmp_ (), setjmp (a1)) +# define DBUG_LONGJMP(a1,a2) (_db_longjmp_ (), longjmp (a1, a2)) +# define DBUG_DUMP(keyword,a1,a2) _db_dump_(__LINE__,keyword,a1,a2) +# endif diff --git a/externals/mysql/extlib/dbug/example1.c b/externals/mysql/extlib/dbug/example1.c new file mode 100644 index 00000000000..7b3c3fcd63d --- /dev/null +++ b/externals/mysql/extlib/dbug/example1.c @@ -0,0 +1,10 @@ +main (argc, argv) +int argc; +char *argv[]; +{ + printf ("argv[0] = %d\n", argv[0]); + /* + * Rest of program + */ + printf ("== done ==\n"); +} diff --git a/externals/mysql/extlib/dbug/example2.c b/externals/mysql/extlib/dbug/example2.c new file mode 100644 index 00000000000..75fc1321b13 --- /dev/null +++ b/externals/mysql/extlib/dbug/example2.c @@ -0,0 +1,15 @@ +int debug = 0; + +main (argc, argv) +int argc; +char *argv[]; +{ + /* printf ("argv = %x\n", argv) */ + if (debug) printf ("argv[0] = %d\n", argv[0]); + /* + * Rest of program + */ +#ifdef DEBUG + printf ("== done ==\n"); +#endif +} diff --git a/externals/mysql/extlib/dbug/example3.c b/externals/mysql/extlib/dbug/example3.c new file mode 100644 index 00000000000..c035cdfffa0 --- /dev/null +++ b/externals/mysql/extlib/dbug/example3.c @@ -0,0 +1,14 @@ +main (argc, argv) +int argc; +char *argv[]; +{ +# ifdef DEBUG + printf ("argv[0] = %d\n", argv[0]); +# endif + /* + * Rest of program + */ +# ifdef DEBUG + printf ("== done ==\n"); +# endif +} diff --git a/externals/mysql/extlib/dbug/factorial.c b/externals/mysql/extlib/dbug/factorial.c new file mode 100644 index 00000000000..7b190ea8d8e --- /dev/null +++ b/externals/mysql/extlib/dbug/factorial.c @@ -0,0 +1,27 @@ +#ifdef DBUG_OFF /* We are testing dbug */ + +int factorial(register int value) { + if(value > 1) { + value *= factorial(value-1); + } + return value; +} + +#else + +#include <my_global.h> + +int factorial ( +register int value) +{ + DBUG_ENTER ("factorial"); + DBUG_PRINT ("find", ("find %d factorial", value)); + if (value > 1) { + value *= factorial (value - 1); + } + DBUG_PRINT ("result", ("result is %d", value)); + DBUG_RETURN (value); +} + +#endif + diff --git a/externals/mysql/extlib/dbug/main.c b/externals/mysql/extlib/dbug/main.c new file mode 100644 index 00000000000..00e80c8ba31 --- /dev/null +++ b/externals/mysql/extlib/dbug/main.c @@ -0,0 +1,24 @@ +#include <dbug.h> + +int main (argc, argv) +int argc; +char *argv[]; +{ + int result, ix; + extern int factorial(int); + DBUG_ENTER ("main"); + DBUG_PROCESS (argv[0]); + for (ix = 1; ix < argc && argv[ix][0] == '-'; ix++) { + switch (argv[ix][1]) { + case '#': + DBUG_PUSH (&(argv[ix][2])); + break; + } + } + for (; ix < argc; ix++) { + DBUG_PRINT ("args", ("argv[%d] = %s", ix, argv[ix])); + result = factorial (atoi(argv[ix])); + printf ("%d\n", result); + } + DBUG_RETURN (0); +} diff --git a/externals/mysql/extlib/dbug/my_main.c b/externals/mysql/extlib/dbug/my_main.c new file mode 100644 index 00000000000..31c15aa67aa --- /dev/null +++ b/externals/mysql/extlib/dbug/my_main.c @@ -0,0 +1,42 @@ +/* + this is modified version of the original example main.c + fixed so that it could compile and run in MySQL source tree +*/ + +#ifdef DBUG_OFF /* We are testing dbug */ +#undef DBUG_OFF +#endif + +#include <my_global.h> /* This includes dbug.h */ +#include <my_pthread.h> + +int main (argc, argv) +int argc; +char *argv[]; +{ + register int result, ix; + extern int factorial(int); +#if defined(HAVE_PTHREAD_INIT) && defined(THREAD) + pthread_init(); /* Must be called before DBUG_ENTER */ +#endif +#ifdef THREAD + my_thread_global_init(); +#endif + { + DBUG_ENTER ("main"); + DBUG_PROCESS (argv[0]); + for (ix = 1; ix < argc && argv[ix][0] == '-'; ix++) { + switch (argv[ix][1]) { + case '#': + DBUG_PUSH (&(argv[ix][2])); + break; + } + } + for (; ix < argc; ix++) { + DBUG_PRINT ("args", ("argv[%d] = %s", ix, argv[ix])); + result = factorial (atoi(argv[ix])); + printf ("%d\n", result); + } + DBUG_RETURN (0); + } +} diff --git a/externals/mysql/extlib/dbug/sanity.c b/externals/mysql/extlib/dbug/sanity.c new file mode 100644 index 00000000000..df43fc14ba9 --- /dev/null +++ b/externals/mysql/extlib/dbug/sanity.c @@ -0,0 +1,13 @@ +/* Declarate _sanity() if not declared in main program */ + +#include <my_global.h> + +extern int _sanity(const char *file,uint line); + +#if defined(SAFEMALLOC) && !defined(MASTER) /* Avoid errors in MySQL */ +int _sanity(const char * file __attribute__((unused)), + uint line __attribute__((unused))) +{ + return 0; +} +#endif diff --git a/externals/mysql/extlib/dbug/tests.c b/externals/mysql/extlib/dbug/tests.c new file mode 100644 index 00000000000..d76266d34a3 --- /dev/null +++ b/externals/mysql/extlib/dbug/tests.c @@ -0,0 +1,87 @@ +/* + A program to test DBUG features. Used by tests-t.pl +*/ + +char *push1=0; + +#include <my_global.h> /* This includes dbug.h */ +#include <my_pthread.h> +#include <string.h> + +const char *func3() +{ + DBUG_ENTER("func3"); + DBUG_RETURN(DBUG_EVALUATE("ret3", "ok", "ko")); +} + +void func2() +{ + const char *s; + DBUG_ENTER("func2"); + s=func3(); + DBUG_PRINT("info", ("s=%s", s)); + DBUG_VOID_RETURN; +} + +int func1() +{ + DBUG_ENTER("func1"); + func2(); + if (push1) + { + DBUG_PUSH(push1); + fprintf(DBUG_FILE, "=> push1\n"); + } + DBUG_RETURN(10); +} + +int main (int argc, char *argv[]) +{ + int i; +#ifdef DBUG_OFF + return 1; +#endif + if (argc == 1) + return 0; + +#if defined(HAVE_PTHREAD_INIT) && defined(THREAD) + pthread_init(); /* Must be called before DBUG_ENTER */ +#endif +#ifdef THREAD + my_thread_global_init(); +#endif + dup2(1, 2); + for (i = 1; i < argc; i++) + { + if (strncmp(argv[i], "--push1=", 8) == 0) + push1=argv[i]+8; + else + DBUG_PUSH (argv[i]); + } + { + DBUG_ENTER ("main"); + DBUG_PROCESS ("dbug-tests"); + func1(); + DBUG_EXECUTE_IF("dump", + { + char s[1000]; + DBUG_EXPLAIN(s, sizeof(s)-1); + DBUG_DUMP("dump", (uchar*)s, strlen(s)); + }); + DBUG_EXECUTE_IF("push", DBUG_PUSH("+t"); ); + DBUG_EXECUTE("execute", fprintf(DBUG_FILE, "=> execute\n"); ); + DBUG_EXECUTE_IF("set", DBUG_SET("+F"); ); + fprintf(DBUG_FILE, "=> evaluate: %s\n", + DBUG_EVALUATE("evaluate", "ON", "OFF")); + fprintf(DBUG_FILE, "=> evaluate_if: %s\n", + DBUG_EVALUATE_IF("evaluate_if", "ON", "OFF")); + DBUG_EXECUTE_IF("pop", DBUG_POP(); ); + { + char s[1000] __attribute__((unused)); + DBUG_EXPLAIN(s, sizeof(s)-1); + DBUG_PRINT("explain", ("dbug explained: %s", s)); + } + func2(); + DBUG_RETURN (0); + } +} diff --git a/externals/mysql/extlib/regex/cclass.h b/externals/mysql/extlib/regex/cclass.h new file mode 100644 index 00000000000..b877b5dee6b --- /dev/null +++ b/externals/mysql/extlib/regex/cclass.h @@ -0,0 +1,22 @@ +/* character-class table */ + +#define CCLASS_ALNUM 0 +#define CCLASS_ALPHA 1 +#define CCLASS_BLANK 2 +#define CCLASS_CNTRL 3 +#define CCLASS_DIGIT 4 +#define CCLASS_GRAPH 5 +#define CCLASS_LOWER 6 +#define CCLASS_PRINT 7 +#define CCLASS_PUNCT 8 +#define CCLASS_SPACE 9 +#define CCLASS_UPPER 10 +#define CCLASS_XDIGIT 11 +#define CCLASS_LAST 12 + +extern struct cclass { + const char *name; + const char *chars; + const char *multis; + uint mask; +} cclasses[]; diff --git a/externals/mysql/extlib/regex/cname.h b/externals/mysql/extlib/regex/cname.h new file mode 100644 index 00000000000..06865f3e102 --- /dev/null +++ b/externals/mysql/extlib/regex/cname.h @@ -0,0 +1,104 @@ +/* character-name table */ +static struct cname { + const char *name; + const char code; +} cnames[] = { + {"NUL", '\0'}, + {"SOH", '\001'}, + {"STX", '\002'}, + {"ETX", '\003'}, + {"EOT", '\004'}, + {"ENQ", '\005'}, + {"ACK", '\006'}, + {"BEL", '\007'}, + {"alert", '\007'}, + {"BS", '\010'}, + {"backspace", '\b'}, + {"HT", '\011'}, + {"tab", '\t'}, + {"LF", '\012'}, + {"newline", '\n'}, + {"VT", '\013'}, + {"vertical-tab",'\v'}, + {"FF", '\014'}, + {"form-feed", '\f'}, + {"CR", '\015'}, + {"carriage-return", '\r'}, + {"SO", '\016'}, + {"SI", '\017'}, + {"DLE", '\020'}, + {"DC1", '\021'}, + {"DC2", '\022'}, + {"DC3", '\023'}, + {"DC4", '\024'}, + {"NAK", '\025'}, + {"SYN", '\026'}, + {"ETB", '\027'}, + {"CAN", '\030'}, + {"EM", '\031'}, + {"SUB", '\032'}, + {"ESC", '\033'}, + {"IS4", '\034'}, + {"FS", '\034'}, + {"IS3", '\035'}, + {"GS", '\035'}, + {"IS2", '\036'}, + {"RS", '\036'}, + {"IS1", '\037'}, + {"US", '\037'}, + {"space", ' '}, + {"exclamation-mark", '!'}, + {"quotation-mark", '"'}, + {"number-sign", '#'}, + {"dollar-sign", '$'}, + {"percent-sign", '%'}, + {"ampersand", '&'}, + {"apostrophe", '\''}, + {"left-parenthesis", '('}, + {"right-parenthesis", ')'}, + {"asterisk", '*'}, + {"plus-sign", '+'}, + {"comma", ','}, + {"hyphen", '-'}, + {"hyphen-minus", '-'}, + {"period", '.'}, + {"full-stop", '.'}, + {"slash", '/'}, + {"solidus", '/'}, + {"zero", '0'}, + {"one", '1'}, + {"two", '2'}, + {"three", '3'}, + {"four", '4'}, + {"five", '5'}, + {"six", '6'}, + {"seven", '7'}, + {"eight", '8'}, + {"nine", '9'}, + {"colon", ':'}, + {"semicolon", ';'}, + {"less-than-sign", '<'}, + {"equals-sign", '='}, + {"greater-than-sign", '>'}, + {"question-mark", '?'}, + {"commercial-at", '@'}, + {"left-square-bracket", '['}, + {"backslash", '\\'}, + {"reverse-solidus", '\\'}, + {"right-square-bracket",']'}, + {"circumflex", '^'}, + {"circumflex-accent", '^'}, + {"underscore", '_'}, + {"low-line", '_'}, + {"grave-accent", '`'}, + {"left-brace", '{'}, + {"left-curly-bracket", '{'}, + {"vertical-line", '|'}, + {"right-brace", '}'}, + {"right-curly-bracket", '}'}, + {"tilde", '~'}, + {"DEL", '\177'}, + {NULL, 0}, +}; + + diff --git a/externals/mysql/extlib/regex/debug.c b/externals/mysql/extlib/regex/debug.c new file mode 100644 index 00000000000..271b09bb27a --- /dev/null +++ b/externals/mysql/extlib/regex/debug.c @@ -0,0 +1,246 @@ +#include <my_global.h> +#include <m_ctype.h> +#include <m_string.h> +#include <sys/types.h> + +#include "my_regex.h" +#include "utils.h" +#include "regex2.h" +#include "debug.ih" + +/* Added extra paramter to regchar to remove static buffer ; Monty 96.11.27 */ + +/* + - regprint - print a regexp for debugging + == void regprint(regex_t *r, FILE *d); + */ +void +regprint(r, d) +my_regex_t *r; +FILE *d; +{ + register struct re_guts *g = r->re_g; + register int i; + register int c; + register int last; + int nincat[NC]; + char buf[10]; + + fprintf(d, "%ld states, %d categories", (long)g->nstates, + g->ncategories); + fprintf(d, ", first %ld last %ld", (long)g->firststate, + (long)g->laststate); + if (g->iflags&USEBOL) + fprintf(d, ", USEBOL"); + if (g->iflags&USEEOL) + fprintf(d, ", USEEOL"); + if (g->iflags&BAD) + fprintf(d, ", BAD"); + if (g->nsub > 0) + fprintf(d, ", nsub=%ld", (long)g->nsub); + if (g->must != NULL) + fprintf(d, ", must(%ld) `%*s'", (long)g->mlen, (int)g->mlen, + g->must); + if (g->backrefs) + fprintf(d, ", backrefs"); + if (g->nplus > 0) + fprintf(d, ", nplus %ld", (long)g->nplus); + fprintf(d, "\n"); + s_print(r->charset, g, d); + for (i = 0; i < g->ncategories; i++) { + nincat[i] = 0; + for (c = CHAR_MIN; c <= CHAR_MAX; c++) + if (g->categories[c] == i) + nincat[i]++; + } + fprintf(d, "cc0#%d", nincat[0]); + for (i = 1; i < g->ncategories; i++) + if (nincat[i] == 1) { + for (c = CHAR_MIN; c <= CHAR_MAX; c++) + if (g->categories[c] == i) + break; + fprintf(d, ", %d=%s", i, regchar(r->charset,c,buf)); + } + fprintf(d, "\n"); + for (i = 1; i < g->ncategories; i++) + if (nincat[i] != 1) { + fprintf(d, "cc%d\t", i); + last = -1; + for (c = CHAR_MIN; c <= CHAR_MAX+1; c++) /* +1 does flush */ + if (c <= CHAR_MAX && g->categories[c] == i) { + if (last < 0) { + fprintf(d, "%s", regchar(r->charset,c,buf)); + last = c; + } + } else { + if (last >= 0) { + if (last != c-1) + fprintf(d, "-%s", + regchar(r->charset,c-1,buf)); + last = -1; + } + } + fprintf(d, "\n"); + } +} + +/* + - s_print - print the strip for debugging + == static void s_print(register struct re_guts *g, FILE *d); + */ +static void +s_print(charset, g, d) +CHARSET_INFO *charset; +register struct re_guts *g; +FILE *d; +{ + register sop *s; + register cset *cs; + register int i; + register int done = 0; + register sop opnd; + register int col = 0; + register int last; + register sopno offset = 2; + char buf[10]; +# define GAP() { if (offset % 5 == 0) { \ + if (col > 40) { \ + fprintf(d, "\n\t"); \ + col = 0; \ + } else { \ + fprintf(d, " "); \ + col++; \ + } \ + } else \ + col++; \ + offset++; \ + } + + if (OP(g->strip[0]) != OEND) + fprintf(d, "missing initial OEND!\n"); + for (s = &g->strip[1]; !done; s++) { + opnd = OPND(*s); + switch (OP(*s)) { + case OEND: + fprintf(d, "\n"); + done = 1; + break; + case OCHAR: + if (strchr("\\|()^$.[+*?{}!<> ", (char)opnd) != NULL) + fprintf(d, "\\%c", (char)opnd); + else + fprintf(d, "%s", regchar(charset,(char)opnd,buf)); + break; + case OBOL: + fprintf(d, "^"); + break; + case OEOL: + fprintf(d, "$"); + break; + case OBOW: + fprintf(d, "\\{"); + break; + case OEOW: + fprintf(d, "\\}"); + break; + case OANY: + fprintf(d, "."); + break; + case OANYOF: + fprintf(d, "[(%ld)", (long)opnd); + cs = &g->sets[opnd]; + last = -1; + for (i = 0; i < g->csetsize+1; i++) /* +1 flushes */ + if (CHIN(cs, i) && i < g->csetsize) { + if (last < 0) { + fprintf(d, "%s", regchar(charset,i,buf)); + last = i; + } + } else { + if (last >= 0) { + if (last != i-1) + fprintf(d, "-%s", + regchar(charset,i-1,buf)); + last = -1; + } + } + fprintf(d, "]"); + break; + case OBACK_: + fprintf(d, "(\\<%ld>", (long)opnd); + break; + case O_BACK: + fprintf(d, "<%ld>\\)", (long)opnd); + break; + case OPLUS_: + fprintf(d, "(+"); + if (OP(*(s+opnd)) != O_PLUS) + fprintf(d, "<%ld>", (long)opnd); + break; + case O_PLUS: + if (OP(*(s-opnd)) != OPLUS_) + fprintf(d, "<%ld>", (long)opnd); + fprintf(d, "+)"); + break; + case OQUEST_: + fprintf(d, "(?"); + if (OP(*(s+opnd)) != O_QUEST) + fprintf(d, "<%ld>", (long)opnd); + break; + case O_QUEST: + if (OP(*(s-opnd)) != OQUEST_) + fprintf(d, "<%ld>", (long)opnd); + fprintf(d, "?)"); + break; + case OLPAREN: + fprintf(d, "((<%ld>", (long)opnd); + break; + case ORPAREN: + fprintf(d, "<%ld>))", (long)opnd); + break; + case OCH_: + fprintf(d, "<"); + if (OP(*(s+opnd)) != OOR2) + fprintf(d, "<%ld>", (long)opnd); + break; + case OOR1: + if (OP(*(s-opnd)) != OOR1 && OP(*(s-opnd)) != OCH_) + fprintf(d, "<%ld>", (long)opnd); + fprintf(d, "|"); + break; + case OOR2: + fprintf(d, "|"); + if (OP(*(s+opnd)) != OOR2 && OP(*(s+opnd)) != O_CH) + fprintf(d, "<%ld>", (long)opnd); + break; + case O_CH: + if (OP(*(s-opnd)) != OOR1) + fprintf(d, "<%ld>", (long)opnd); + fprintf(d, ">"); + break; + default: + fprintf(d, "!%ld(%ld)!", OP(*s), opnd); + break; + } + if (!done) + GAP(); + } +} + +/* + - regchar - make a character printable + == static char *regchar(int ch); + */ +static char * /* -> representation */ +regchar(charset,ch,buf) +CHARSET_INFO *charset; +int ch; +char *buf; +{ + + if (my_isprint(charset,ch) || ch == ' ') + sprintf(buf, "%c", ch); + else + sprintf(buf, "\\%o", ch); + return(buf); +} diff --git a/externals/mysql/extlib/regex/engine.c b/externals/mysql/extlib/regex/engine.c new file mode 100644 index 00000000000..1968ca61a96 --- /dev/null +++ b/externals/mysql/extlib/regex/engine.c @@ -0,0 +1,1031 @@ +/* + * The matching engine and friends. This file is #included by regexec.c + * after suitable #defines of a variety of macros used herein, so that + * different state representations can be used without duplicating masses + * of code. + */ + +#ifdef SNAMES +#define matcher smatcher +#define fast sfast +#define slow sslow +#define dissect sdissect +#define backref sbackref +#define step sstep +#define print sprint +#define at sat +#define match smat +#endif +#ifdef LNAMES +#define matcher lmatcher +#define fast lfast +#define slow lslow +#define dissect ldissect +#define backref lbackref +#define step lstep +#define print lprint +#define at lat +#define match lmat +#endif + +/* another structure passed up and down to avoid zillions of parameters */ +struct match { + struct re_guts *g; + int eflags; + my_regmatch_t *pmatch; /* [nsub+1] (0 element unused) */ + char *offp; /* offsets work from here */ + char *beginp; /* start of string -- virtual NUL precedes */ + char *endp; /* end of string -- virtual NUL here */ + char *coldp; /* can be no match starting before here */ + char **lastpos; /* [nplus+1] */ + STATEVARS; + states st; /* current states */ + states fresh; /* states for a fresh start */ + states tmp; /* temporary */ + states empty; /* empty set of states */ +}; + +#include "engine.ih" + +#ifdef REDEBUG +#define SP(t, s, c) print(m, t, s, c, stdout) +#define AT(t, p1, p2, s1, s2) at(m, t, p1, p2, s1, s2) +#define NOTE(str) { if (m->eflags®_TRACE) printf("=%s\n", (str)); } +#else +#define SP(t, s, c) /* nothing */ +#define AT(t, p1, p2, s1, s2) /* nothing */ +#define NOTE(s) /* nothing */ +#endif + +/* + - matcher - the actual matching engine + == static int matcher(register struct re_guts *g, char *string, \ + == size_t nmatch, regmatch_t pmatch[], int eflags); + */ +static int /* 0 success, REG_NOMATCH failure */ +matcher(charset,g, str, nmatch, pmatch, eflags) +CHARSET_INFO *charset; +register struct re_guts *g; +char *str; +size_t nmatch; +my_regmatch_t pmatch[]; +int eflags; +{ + register char *endp; + register uint i; + struct match mv; + register struct match *m = &mv; + register char *dp; + register const sopno gf = g->firststate+1; /* +1 for OEND */ + register const sopno gl = g->laststate; + char *start; + char *stop; + + /* simplify the situation where possible */ + if (g->cflags®_NOSUB) + nmatch = 0; + if (eflags®_STARTEND) { + start = str + pmatch[0].rm_so; + stop = str + pmatch[0].rm_eo; + } else { + start = str; + stop = start + strlen(start); + } + if (stop < start) + return(REG_INVARG); + + /* prescreening; this does wonders for this rather slow code */ + if (g->must != NULL) { + for (dp = start; dp < stop; dp++) + if (*dp == g->must[0] && stop - dp >= g->mlen && + memcmp(dp, g->must, (size_t)g->mlen) == 0) + break; + if (dp == stop) /* we didn't find g->must */ + return(REG_NOMATCH); + } + + /* match struct setup */ + m->g = g; + m->eflags = eflags; + m->pmatch = NULL; + m->lastpos = NULL; + m->offp = str; + m->beginp = start; + m->endp = stop; + STATESETUP(m, 4); + SETUP(m->st); + SETUP(m->fresh); + SETUP(m->tmp); + SETUP(m->empty); + CLEAR(m->empty); + + /* this loop does only one repetition except for backrefs */ + for (;;) { + endp = fast(charset, m, start, stop, gf, gl); + if (endp == NULL) { /* a miss */ + if (m->pmatch != NULL) + free((char *)m->pmatch); + if (m->lastpos != NULL) + free((char *)m->lastpos); + STATETEARDOWN(m); + return(REG_NOMATCH); + } + if (nmatch == 0 && !g->backrefs) + break; /* no further info needed */ + + /* where? */ + assert(m->coldp != NULL); + for (;;) { + NOTE("finding start"); + endp = slow(charset, m, m->coldp, stop, gf, gl); + if (endp != NULL) + break; + assert(m->coldp < m->endp); + m->coldp++; + } + if (nmatch == 1 && !g->backrefs) + break; /* no further info needed */ + + /* oh my, he wants the subexpressions... */ + if (m->pmatch == NULL) + m->pmatch = (my_regmatch_t *)malloc((m->g->nsub + 1) * + sizeof(my_regmatch_t)); + if (m->pmatch == NULL) { + if (m->lastpos != NULL) + free((char *)m->lastpos); + STATETEARDOWN(m); + return(REG_ESPACE); + } + for (i = 1; i <= m->g->nsub; i++) + m->pmatch[i].rm_so = m->pmatch[i].rm_eo = -1; + if (!g->backrefs && !(m->eflags®_BACKR)) { + NOTE("dissecting"); + dp = dissect(charset, m, m->coldp, endp, gf, gl); + } else { + if (g->nplus > 0 && m->lastpos == NULL) + m->lastpos = (char **)malloc((g->nplus+1) * + sizeof(char *)); + if (g->nplus > 0 && m->lastpos == NULL) { + free(m->pmatch); + STATETEARDOWN(m); + return(REG_ESPACE); + } + NOTE("backref dissect"); + dp = backref(charset, m, m->coldp, endp, gf, gl, (sopno)0); + } + if (dp != NULL) + break; + + /* uh-oh... we couldn't find a subexpression-level match */ + assert(g->backrefs); /* must be back references doing it */ + assert(g->nplus == 0 || m->lastpos != NULL); + for (;;) { + if (dp != NULL || endp <= m->coldp) + break; /* defeat */ + NOTE("backoff"); + endp = slow(charset, m, m->coldp, endp-1, gf, gl); + if (endp == NULL) + break; /* defeat */ + /* try it on a shorter possibility */ +#ifndef NDEBUG + for (i = 1; i <= m->g->nsub; i++) { + assert(m->pmatch[i].rm_so == -1); + assert(m->pmatch[i].rm_eo == -1); + } +#endif + NOTE("backoff dissect"); + dp = backref(charset, m, m->coldp, endp, gf, gl, (sopno)0); + } + assert(dp == NULL || dp == endp); + if (dp != NULL) /* found a shorter one */ + break; + + /* despite initial appearances, there is no match here */ + NOTE("false alarm"); + start = m->coldp + 1; /* recycle starting later */ + assert(start <= stop); + } + + /* fill in the details if requested */ + if (nmatch > 0) { + pmatch[0].rm_so = m->coldp - m->offp; + pmatch[0].rm_eo = endp - m->offp; + } + if (nmatch > 1) { + assert(m->pmatch != NULL); + for (i = 1; i < nmatch; i++) + if (i <= m->g->nsub) + pmatch[i] = m->pmatch[i]; + else { + pmatch[i].rm_so = -1; + pmatch[i].rm_eo = -1; + } + } + + if (m->pmatch != NULL) + free((char *)m->pmatch); + if (m->lastpos != NULL) + free((char *)m->lastpos); + STATETEARDOWN(m); + return(0); +} + +/* + - dissect - figure out what matched what, no back references + == static char *dissect(register struct match *m, char *start, \ + == char *stop, sopno startst, sopno stopst); + */ +static char * /* == stop (success) always */ +dissect(charset, m, start, stop, startst, stopst) +CHARSET_INFO *charset; +register struct match *m; +char *start; +char *stop; +sopno startst; +sopno stopst; +{ + register uint i; + register sopno ss; /* start sop of current subRE */ + register sopno es; /* end sop of current subRE */ + register char *sp; /* start of string matched by it */ + register char *stp; /* string matched by it cannot pass here */ + register char *rest; /* start of rest of string */ + register char *tail; /* string unmatched by rest of RE */ + register sopno ssub; /* start sop of subsubRE */ + register sopno esub; /* end sop of subsubRE */ + register char *ssp; /* start of string matched by subsubRE */ + register char *sep; /* end of string matched by subsubRE */ + register char *oldssp; /* previous ssp */ + register char *dp; /* used in debug mode to check asserts */ + + AT("diss", start, stop, startst, stopst); + sp = start; + for (ss = startst; ss < stopst; ss = es) { + /* identify end of subRE */ + es = ss; + switch (OP(m->g->strip[es])) { + case OPLUS_: + case OQUEST_: + es += OPND(m->g->strip[es]); + break; + case OCH_: + while (OP(m->g->strip[es]) != O_CH) + es += OPND(m->g->strip[es]); + break; + } + es++; + + /* figure out what it matched */ + switch (OP(m->g->strip[ss])) { + case OEND: + assert(nope); + break; + case OCHAR: + sp++; + break; + case OBOL: + case OEOL: + case OBOW: + case OEOW: + break; + case OANY: + case OANYOF: + sp++; + break; + case OBACK_: + case O_BACK: + assert(nope); + break; + /* cases where length of match is hard to find */ + case OQUEST_: + stp = stop; + for (;;) { + /* how long could this one be? */ + rest = slow(charset, m, sp, stp, ss, es); + assert(rest != NULL); /* it did match */ + /* could the rest match the rest? */ + tail = slow(charset, m, rest, stop, es, stopst); + if (tail == stop) + break; /* yes! */ + /* no -- try a shorter match for this one */ + stp = rest - 1; + assert(stp >= sp); /* it did work */ + } + ssub = ss + 1; + esub = es - 1; + /* did innards match? */ + if (slow(charset, m, sp, rest, ssub, esub) != NULL) { + dp = dissect(charset, m, sp, rest, ssub, esub); + assert(dp == rest); + } else /* no */ + assert(sp == rest); + sp = rest; + break; + case OPLUS_: + stp = stop; + for (;;) { + /* how long could this one be? */ + rest = slow(charset, m, sp, stp, ss, es); + assert(rest != NULL); /* it did match */ + /* could the rest match the rest? */ + tail = slow(charset, m, rest, stop, es, stopst); + if (tail == stop) + break; /* yes! */ + /* no -- try a shorter match for this one */ + stp = rest - 1; + assert(stp >= sp); /* it did work */ + } + ssub = ss + 1; + esub = es - 1; + ssp = sp; + oldssp = ssp; + for (;;) { /* find last match of innards */ + sep = slow(charset, m, ssp, rest, ssub, esub); + if (sep == NULL || sep == ssp) + break; /* failed or matched null */ + oldssp = ssp; /* on to next try */ + ssp = sep; + } + if (sep == NULL) { + /* last successful match */ + sep = ssp; + ssp = oldssp; + } + assert(sep == rest); /* must exhaust substring */ + assert(slow(charset, m, ssp, sep, ssub, esub) == rest); + dp = dissect(charset, m, ssp, sep, ssub, esub); + assert(dp == sep); + sp = rest; + break; + case OCH_: + stp = stop; + for (;;) { + /* how long could this one be? */ + rest = slow(charset, m, sp, stp, ss, es); + assert(rest != NULL); /* it did match */ + /* could the rest match the rest? */ + tail = slow(charset, m, rest, stop, es, stopst); + if (tail == stop) + break; /* yes! */ + /* no -- try a shorter match for this one */ + stp = rest - 1; + assert(stp >= sp); /* it did work */ + } + ssub = ss + 1; + esub = ss + OPND(m->g->strip[ss]) - 1; + assert(OP(m->g->strip[esub]) == OOR1); + for (;;) { /* find first matching branch */ + if (slow(charset, m, sp, rest, ssub, esub) == rest) + break; /* it matched all of it */ + /* that one missed, try next one */ + assert(OP(m->g->strip[esub]) == OOR1); + esub++; + assert(OP(m->g->strip[esub]) == OOR2); + ssub = esub + 1; + esub += OPND(m->g->strip[esub]); + if (OP(m->g->strip[esub]) == OOR2) + esub--; + else + assert(OP(m->g->strip[esub]) == O_CH); + } + dp = dissect(charset, m, sp, rest, ssub, esub); + assert(dp == rest); + sp = rest; + break; + case O_PLUS: + case O_QUEST: + case OOR1: + case OOR2: + case O_CH: + assert(nope); + break; + case OLPAREN: + i = OPND(m->g->strip[ss]); + assert(0 < i && i <= m->g->nsub); + m->pmatch[i].rm_so = sp - m->offp; + break; + case ORPAREN: + i = OPND(m->g->strip[ss]); + assert(0 < i && i <= m->g->nsub); + m->pmatch[i].rm_eo = sp - m->offp; + break; + default: /* uh oh */ + assert(nope); + break; + } + } + + assert(sp == stop); + return(sp); +} + +/* + - backref - figure out what matched what, figuring in back references + == static char *backref(register struct match *m, char *start, \ + == char *stop, sopno startst, sopno stopst, sopno lev); + */ +static char * /* == stop (success) or NULL (failure) */ +backref(charset,m, start, stop, startst, stopst, lev) +CHARSET_INFO *charset; +register struct match *m; +char *start; +char *stop; +sopno startst; +sopno stopst; +sopno lev; /* PLUS nesting level */ +{ + register uint i; + register sopno ss; /* start sop of current subRE */ + register char *sp; /* start of string matched by it */ + register sopno ssub; /* start sop of subsubRE */ + register sopno esub; /* end sop of subsubRE */ + register char *ssp; /* start of string matched by subsubRE */ + register char *dp; + register size_t len; + register int hard; + register sop s; + register regoff_t offsave; + register cset *cs; + + AT("back", start, stop, startst, stopst); + sp = start; + + /* get as far as we can with easy stuff */ + hard = 0; + for (ss = startst; !hard && ss < stopst; ss++) + switch (OP(s = m->g->strip[ss])) { + case OCHAR: + if (sp == stop || *sp++ != (char)OPND(s)) + return(NULL); + break; + case OANY: + if (sp == stop) + return(NULL); + sp++; + break; + case OANYOF: + cs = &m->g->sets[OPND(s)]; + if (sp == stop || !CHIN(cs, *sp++)) + return(NULL); + break; + case OBOL: + if ( (sp == m->beginp && !(m->eflags®_NOTBOL)) || + (sp < m->endp && *(sp-1) == '\n' && + (m->g->cflags®_NEWLINE)) ) + { /* yes */ } + else + return(NULL); + break; + case OEOL: + if ( (sp == m->endp && !(m->eflags®_NOTEOL)) || + (sp < m->endp && *sp == '\n' && + (m->g->cflags®_NEWLINE)) ) + { /* yes */ } + else + return(NULL); + break; + case OBOW: + if (( (sp == m->beginp && !(m->eflags®_NOTBOL)) || + (sp < m->endp && *(sp-1) == '\n' && + (m->g->cflags®_NEWLINE)) || + (sp > m->beginp && + !ISWORD(charset,*(sp-1))) ) && + (sp < m->endp && ISWORD(charset,*sp)) ) + { /* yes */ } + else + return(NULL); + break; + case OEOW: + if (( (sp == m->endp && !(m->eflags®_NOTEOL)) || + (sp < m->endp && *sp == '\n' && + (m->g->cflags®_NEWLINE)) || + (sp < m->endp && !ISWORD(charset,*sp)) ) && + (sp > m->beginp && ISWORD(charset,*(sp-1))) ) + { /* yes */ } + else + return(NULL); + break; + case O_QUEST: + break; + case OOR1: /* matches null but needs to skip */ + ss++; + s = m->g->strip[ss]; + do { + assert(OP(s) == OOR2); + ss += OPND(s); + } while (OP(s = m->g->strip[ss]) != O_CH); + /* note that the ss++ gets us past the O_CH */ + break; + default: /* have to make a choice */ + hard = 1; + break; + } + if (!hard) { /* that was it! */ + if (sp != stop) + return(NULL); + return(sp); + } + ss--; /* adjust for the for's final increment */ + + /* the hard stuff */ + AT("hard", sp, stop, ss, stopst); + s = m->g->strip[ss]; + switch (OP(s)) { + case OBACK_: /* the vilest depths */ + i = OPND(s); + assert(0 < i && i <= m->g->nsub); + if (m->pmatch[i].rm_eo == -1) + return(NULL); + assert(m->pmatch[i].rm_so != -1); + len = m->pmatch[i].rm_eo - m->pmatch[i].rm_so; + assert((size_t) (stop - m->beginp) >= len); + if (sp > stop - len) + return(NULL); /* not enough left to match */ + ssp = m->offp + m->pmatch[i].rm_so; + if (memcmp(sp, ssp, len) != 0) + return(NULL); + while (m->g->strip[ss] != SOP(O_BACK, i)) + ss++; + return(backref(charset, m, sp+len, stop, ss+1, stopst, lev)); + break; + case OQUEST_: /* to null or not */ + dp = backref(charset, m, sp, stop, ss+1, stopst, lev); + if (dp != NULL) + return(dp); /* not */ + return(backref(charset, m, sp, stop, ss+OPND(s)+1, stopst, lev)); + break; + case OPLUS_: + assert(m->lastpos != NULL); + assert(lev+1 <= m->g->nplus); + m->lastpos[lev+1] = sp; + return(backref(charset, m, sp, stop, ss+1, stopst, lev+1)); + break; + case O_PLUS: + if (sp == m->lastpos[lev]) /* last pass matched null */ + return(backref(charset, m, sp, stop, ss+1, stopst, lev-1)); + /* try another pass */ + m->lastpos[lev] = sp; + dp = backref(charset, m, sp, stop, ss-OPND(s)+1, stopst, lev); + if (dp == NULL) + return(backref(charset, m, sp, stop, ss+1, stopst, lev-1)); + else + return(dp); + break; + case OCH_: /* find the right one, if any */ + ssub = ss + 1; + esub = ss + OPND(s) - 1; + assert(OP(m->g->strip[esub]) == OOR1); + for (;;) { /* find first matching branch */ + dp = backref(charset, m, sp, stop, ssub, esub, lev); + if (dp != NULL) + return(dp); + /* that one missed, try next one */ + if (OP(m->g->strip[esub]) == O_CH) + return(NULL); /* there is none */ + esub++; + assert(OP(m->g->strip[esub]) == OOR2); + ssub = esub + 1; + esub += OPND(m->g->strip[esub]); + if (OP(m->g->strip[esub]) == OOR2) + esub--; + else + assert(OP(m->g->strip[esub]) == O_CH); + } + break; + case OLPAREN: /* must undo assignment if rest fails */ + i = OPND(s); + assert(0 < i && i <= m->g->nsub); + offsave = m->pmatch[i].rm_so; + m->pmatch[i].rm_so = sp - m->offp; + dp = backref(charset, m, sp, stop, ss+1, stopst, lev); + if (dp != NULL) + return(dp); + m->pmatch[i].rm_so = offsave; + return(NULL); + break; + case ORPAREN: /* must undo assignment if rest fails */ + i = OPND(s); + assert(0 < i && i <= m->g->nsub); + offsave = m->pmatch[i].rm_eo; + m->pmatch[i].rm_eo = sp - m->offp; + dp = backref(charset, m, sp, stop, ss+1, stopst, lev); + if (dp != NULL) + return(dp); + m->pmatch[i].rm_eo = offsave; + return(NULL); + break; + default: /* uh oh */ + assert(nope); + break; + } + + /* "can't happen" */ + assert(nope); + /* NOTREACHED */ + return 0; /* Keep gcc happy */ +} + +/* + - fast - step through the string at top speed + == static char *fast(register struct match *m, char *start, \ + == char *stop, sopno startst, sopno stopst); + */ +static char * /* where tentative match ended, or NULL */ +fast(charset, m, start, stop, startst, stopst) +CHARSET_INFO *charset; +register struct match *m; +char *start; +char *stop; +sopno startst; +sopno stopst; +{ + register states st = m->st; + register states fresh = m->fresh; + register states tmp = m->tmp; + register char *p = start; + register int c = (start == m->beginp) ? OUT : *(start-1); + register int lastc; /* previous c */ + register int flagch; + register int i; + register char *coldp; /* last p after which no match was underway */ + + CLEAR(st); + SET1(st, startst); + st = step(m->g, startst, stopst, st, NOTHING, st); + ASSIGN(fresh, st); + SP("start", st, *p); + coldp = NULL; + for (;;) { + /* next character */ + lastc = c; + c = (p == m->endp) ? OUT : *p; + if (EQ(st, fresh)) + coldp = p; + + /* is there an EOL and/or BOL between lastc and c? */ + flagch = '\0'; + i = 0; + if ( (lastc == '\n' && m->g->cflags®_NEWLINE) || + (lastc == OUT && !(m->eflags®_NOTBOL)) ) { + flagch = BOL; + i = m->g->nbol; + } + if ( (c == '\n' && m->g->cflags®_NEWLINE) || + (c == OUT && !(m->eflags®_NOTEOL)) ) { + flagch = (flagch == BOL) ? BOLEOL : EOL; + i += m->g->neol; + } + if (i != 0) { + for (; i > 0; i--) + st = step(m->g, startst, stopst, st, flagch, st); + SP("boleol", st, c); + } + + /* how about a word boundary? */ + if ( (flagch == BOL || (lastc != OUT && !ISWORD(charset,lastc))) && + (c != OUT && ISWORD(charset,c)) ) { + flagch = BOW; + } + if ( (lastc != OUT && ISWORD(charset,lastc)) && + (flagch == EOL || (c != OUT && !ISWORD(charset,c))) ) { + flagch = EOW; + } + if (flagch == BOW || flagch == EOW) { + st = step(m->g, startst, stopst, st, flagch, st); + SP("boweow", st, c); + } + + /* are we done? */ + if (ISSET(st, stopst) || p == stop) + break; /* NOTE BREAK OUT */ + + /* no, we must deal with this character */ + ASSIGN(tmp, st); + ASSIGN(st, fresh); + assert(c != OUT); + st = step(m->g, startst, stopst, tmp, c, st); + SP("aft", st, c); + assert(EQ(step(m->g, startst, stopst, st, NOTHING, st), st)); + p++; + } + + assert(coldp != NULL); + m->coldp = coldp; + if (ISSET(st, stopst)) + return(p+1); + else + return(NULL); +} + +/* + - slow - step through the string more deliberately + == static char *slow(register struct match *m, char *start, \ + == char *stop, sopno startst, sopno stopst); + */ +static char * /* where it ended */ +slow(charset, m, start, stop, startst, stopst) +CHARSET_INFO *charset; +register struct match *m; +char *start; +char *stop; +sopno startst; +sopno stopst; +{ + register states st = m->st; + register states empty = m->empty; + register states tmp = m->tmp; + register char *p = start; + register int c = (start == m->beginp) ? OUT : *(start-1); + register int lastc; /* previous c */ + register int flagch; + register int i; + register char *matchp; /* last p at which a match ended */ + + AT("slow", start, stop, startst, stopst); + CLEAR(st); + SET1(st, startst); + SP("sstart", st, *p); + st = step(m->g, startst, stopst, st, NOTHING, st); + matchp = NULL; + for (;;) { + /* next character */ + lastc = c; + c = (p == m->endp) ? OUT : *p; + + /* is there an EOL and/or BOL between lastc and c? */ + flagch = '\0'; + i = 0; + if ( (lastc == '\n' && m->g->cflags®_NEWLINE) || + (lastc == OUT && !(m->eflags®_NOTBOL)) ) { + flagch = BOL; + i = m->g->nbol; + } + if ( (c == '\n' && m->g->cflags®_NEWLINE) || + (c == OUT && !(m->eflags®_NOTEOL)) ) { + flagch = (flagch == BOL) ? BOLEOL : EOL; + i += m->g->neol; + } + if (i != 0) { + for (; i > 0; i--) + st = step(m->g, startst, stopst, st, flagch, st); + SP("sboleol", st, c); + } + + /* how about a word boundary? */ + if ( (flagch == BOL || (lastc != OUT && !ISWORD(charset,lastc))) && + (c != OUT && ISWORD(charset,c)) ) { + flagch = BOW; + } + if ( (lastc != OUT && ISWORD(charset,lastc)) && + (flagch == EOL || (c != OUT && !ISWORD(charset,c))) ) { + flagch = EOW; + } + if (flagch == BOW || flagch == EOW) { + st = step(m->g, startst, stopst, st, flagch, st); + SP("sboweow", st, c); + } + + /* are we done? */ + if (ISSET(st, stopst)) + matchp = p; + if (EQ(st, empty) || p == stop) + break; /* NOTE BREAK OUT */ + + /* no, we must deal with this character */ + ASSIGN(tmp, st); + ASSIGN(st, empty); + assert(c != OUT); + st = step(m->g, startst, stopst, tmp, c, st); + SP("saft", st, c); + assert(EQ(step(m->g, startst, stopst, st, NOTHING, st), st)); + p++; + } + + return(matchp); +} + + +/* + - step - map set of states reachable before char to set reachable after + == static states step(register struct re_guts *g, sopno start, sopno stop, \ + == register states bef, int ch, register states aft); + == #define BOL (OUT+1) + == #define EOL (BOL+1) + == #define BOLEOL (BOL+2) + == #define NOTHING (BOL+3) + == #define BOW (BOL+4) + == #define EOW (BOL+5) + == #define CODEMAX (BOL+5) // highest code used + == #define NONCHAR(c) ((c) > CHAR_MAX) + == #define NNONCHAR (CODEMAX-CHAR_MAX) + */ +static states +step(g, start, stop, bef, ch, aft) +register struct re_guts *g; +sopno start; /* start state within strip */ +sopno stop; /* state after stop state within strip */ +register states bef; /* states reachable before */ +int ch; /* character or NONCHAR code */ +register states aft; /* states already known reachable after */ +{ + register cset *cs; + register sop s; + register sopno pc; + register onestate here; /* note, macros know this name */ + register sopno look; + register onestate i; /* Changed from int by Monty */ + + for (pc = start, INIT(here, pc); pc != stop; pc++, INC(here)) { + s = g->strip[pc]; + switch (OP(s)) { + case OEND: + assert(pc == stop-1); + break; + case OCHAR: + /* only characters can match */ + assert(!NONCHAR(ch) || ch != (char)OPND(s)); + if (ch == (char)OPND(s)) + FWD(aft, bef, 1); + break; + case OBOL: + if (ch == BOL || ch == BOLEOL) + FWD(aft, bef, 1); + break; + case OEOL: + if (ch == EOL || ch == BOLEOL) + FWD(aft, bef, 1); + break; + case OBOW: + if (ch == BOW) + FWD(aft, bef, 1); + break; + case OEOW: + if (ch == EOW) + FWD(aft, bef, 1); + break; + case OANY: + if (!NONCHAR(ch)) + FWD(aft, bef, 1); + break; + case OANYOF: + cs = &g->sets[OPND(s)]; + if (!NONCHAR(ch) && CHIN(cs, ch)) + FWD(aft, bef, 1); + break; + case OBACK_: /* ignored here */ + case O_BACK: + FWD(aft, aft, 1); + break; + case OPLUS_: /* forward, this is just an empty */ + FWD(aft, aft, 1); + break; + case O_PLUS: /* both forward and back */ + FWD(aft, aft, 1); + i = ISSETBACK(aft, OPND(s)); + BACK(aft, aft, OPND(s)); + if (!i && ISSETBACK(aft, OPND(s))) { + /* oho, must reconsider loop body */ + pc -= OPND(s) + 1; + INIT(here, pc); + } + break; + case OQUEST_: /* two branches, both forward */ + FWD(aft, aft, 1); + FWD(aft, aft, OPND(s)); + break; + case O_QUEST: /* just an empty */ + FWD(aft, aft, 1); + break; + case OLPAREN: /* not significant here */ + case ORPAREN: + FWD(aft, aft, 1); + break; + case OCH_: /* mark the first two branches */ + FWD(aft, aft, 1); + assert(OP(g->strip[pc+OPND(s)]) == OOR2); + FWD(aft, aft, OPND(s)); + break; + case OOR1: /* done a branch, find the O_CH */ + if (ISSTATEIN(aft, here)) { + for (look = 1; + OP(s = g->strip[pc+look]) != O_CH; + look += OPND(s)) + assert(OP(s) == OOR2); + FWD(aft, aft, look); + } + break; + case OOR2: /* propagate OCH_'s marking */ + FWD(aft, aft, 1); + if (OP(g->strip[pc+OPND(s)]) != O_CH) { + assert(OP(g->strip[pc+OPND(s)]) == OOR2); + FWD(aft, aft, OPND(s)); + } + break; + case O_CH: /* just empty */ + FWD(aft, aft, 1); + break; + default: /* ooooops... */ + assert(nope); + break; + } + } + + return(aft); +} + +#ifdef REDEBUG +/* + - print - print a set of states + == #ifdef REDEBUG + == static void print(struct match *m, char *caption, states st, \ + == int ch, FILE *d); + == #endif + */ +static void +print(m, caption, st, ch, d) +struct match *m; +char *caption; +states st; +int ch; +FILE *d; +{ + register struct re_guts *g = m->g; + register int i; + register int first = 1; + char buf[10]; + + if (!(m->eflags®_TRACE)) + return; + + fprintf(d, "%s", caption); + if (ch != '\0') + fprintf(d, " %s", printchar(ch,buf)); + for (i = 0; i < g->nstates; i++) + if (ISSET(st, i)) { + fprintf(d, "%s%d", (first) ? "\t" : ", ", i); + first = 0; + } + fprintf(d, "\n"); +} + +/* + - at - print current situation + == #ifdef REDEBUG + == static void at(struct match *m, char *title, char *start, char *stop, \ + == sopno startst, sopno stopst); + == #endif + */ +static void +at(m, title, start, stop, startst, stopst) +struct match *m; +char *title; +char *start; +char *stop; +sopno startst; +sopno stopst; +{ + char buf[10]; + if (!(m->eflags®_TRACE)) + return; + + printf("%s %s-", title, printchar(*start,buf)); + printf("%s ", printchar(*stop,buf)); + printf("%ld-%ld\n", (long)startst, (long)stopst,buf); +} + +#ifndef PCHARDONE +#define PCHARDONE /* never again */ +/* + - printchar - make a character printable + == #ifdef REDEBUG + == static char *printchar(int ch); + == #endif + * + * Is this identical to regchar() over in debug.c? Well, yes. But a + * duplicate here avoids having a debugging-capable regexec.o tied to + * a matching debug.o, and this is convenient. It all disappears in + * the non-debug compilation anyway, so it doesn't matter much. + */ +static char * /* -> representation */ +printchar(ch,pbuf) +int ch; +char *pbuf; +{ + if (isprint(ch) || ch == ' ') + sprintf(pbuf, "%c", ch); + else + sprintf(pbuf, "\\%o", ch); + return(pbuf); +} +#endif +#endif + +#undef matcher +#undef fast +#undef slow +#undef dissect +#undef backref +#undef step +#undef print +#undef at +#undef match diff --git a/externals/mysql/extlib/regex/main.c b/externals/mysql/extlib/regex/main.c new file mode 100644 index 00000000000..fa97ca89047 --- /dev/null +++ b/externals/mysql/extlib/regex/main.c @@ -0,0 +1,509 @@ +#include <my_global.h> +#include <m_string.h> +#include <sys/types.h> +#include <assert.h> + +#include "my_regex.h" +#include "main.ih" + +char *progname; +int debug = 0; +int line = 0; +int status = 0; + +int copts = REG_EXTENDED; +int eopts = 0; +regoff_t startoff = 0; +regoff_t endoff = 0; + + +extern int split(); +extern void regprint(); + +/* + - main - do the simple case, hand off to regress() for regression + */ +int main(argc, argv) +int argc; +char *argv[]; +{ + my_regex_t re; +# define NS 10 + my_regmatch_t subs[NS]; + char erbuf[100]; + int err; + size_t len; + int c; + int errflg = 0; + register int i; + extern int optind; + extern char *optarg; + + progname = argv[0]; + + while ((c = getopt(argc, argv, "c:e:S:E:x")) != EOF) + switch (c) { + case 'c': /* compile options */ + copts = options('c', optarg); + break; + case 'e': /* execute options */ + eopts = options('e', optarg); + break; + case 'S': /* start offset */ + startoff = (regoff_t)atoi(optarg); + break; + case 'E': /* end offset */ + endoff = (regoff_t)atoi(optarg); + break; + case 'x': /* Debugging. */ + debug++; + break; + case '?': + default: + errflg++; + break; + } + if (errflg) { + fprintf(stderr, "usage: %s ", progname); + fprintf(stderr, "[-c copt][-C][-d] [re]\n"); + exit(2); + } + + if (optind >= argc) { + regress(stdin); + exit(status); + } + + err = my_regcomp(&re, argv[optind++], copts, &my_charset_latin1); + if (err) { + len = my_regerror(err, &re, erbuf, sizeof(erbuf)); + fprintf(stderr, "error %s, %d/%d `%s'\n", + eprint(err), (int) len, (int) sizeof(erbuf), erbuf); + exit(status); + } + regprint(&re, stdout); + + if (optind >= argc) { + my_regfree(&re); + exit(status); + } + + if (eopts®_STARTEND) { + subs[0].rm_so = startoff; + subs[0].rm_eo = strlen(argv[optind]) - endoff; + } + err = my_regexec(&re, argv[optind], (size_t)NS, subs, eopts); + if (err) { + len = my_regerror(err, &re, erbuf, sizeof(erbuf)); + fprintf(stderr, "error %s, %d/%d `%s'\n", + eprint(err), (int) len, (int) sizeof(erbuf), erbuf); + exit(status); + } + if (!(copts®_NOSUB)) { + len = (int)(subs[0].rm_eo - subs[0].rm_so); + if (subs[0].rm_so != -1) { + if (len != 0) + printf("match `%.*s'\n", (int)len, + argv[optind] + subs[0].rm_so); + else + printf("match `'@%.1s\n", + argv[optind] + subs[0].rm_so); + } + for (i = 1; i < NS; i++) + if (subs[i].rm_so != -1) + printf("(%d) `%.*s'\n", i, + (int)(subs[i].rm_eo - subs[i].rm_so), + argv[optind] + subs[i].rm_so); + } + exit(status); +} + +/* + - regress - main loop of regression test + == void regress(FILE *in); + */ +void +regress(in) +FILE *in; +{ + char inbuf[1000]; +# define MAXF 10 + char *f[MAXF]; + int nf; + int i; + char erbuf[100]; + size_t ne; + const char *badpat = "invalid regular expression"; +# define SHORT 10 + const char *bpname = "REG_BADPAT"; + my_regex_t re; + + while (fgets(inbuf, sizeof(inbuf), in) != NULL) { + line++; + if (inbuf[0] == '#' || inbuf[0] == '\n') + continue; /* NOTE CONTINUE */ + inbuf[strlen(inbuf)-1] = '\0'; /* get rid of stupid \n */ + if (debug) + fprintf(stdout, "%d:\n", line); + nf = split(inbuf, f, MAXF, "\t\t"); + if (nf < 3) { + fprintf(stderr, "bad input, line %d\n", line); + exit(1); + } + for (i = 0; i < nf; i++) + if (strcmp(f[i], "\"\"") == 0) + f[i] = (char*) ""; + if (nf <= 3) + f[3] = NULL; + if (nf <= 4) + f[4] = NULL; + rx_try(f[0], f[1], f[2], f[3], f[4], options('c', f[1])); + if (opt('&', f[1])) /* try with either type of RE */ + rx_try(f[0], f[1], f[2], f[3], f[4], + options('c', f[1]) &~ REG_EXTENDED); + } + + ne = my_regerror(REG_BADPAT, (my_regex_t *)NULL, erbuf, sizeof(erbuf)); + if (strcmp(erbuf, badpat) != 0 || ne != strlen(badpat)+1) { + fprintf(stderr, "end: regerror() test gave `%s' not `%s'\n", + erbuf, badpat); + status = 1; + } + ne = my_regerror(REG_BADPAT, (my_regex_t *)NULL, erbuf, (size_t)SHORT); + if (strncmp(erbuf, badpat, SHORT-1) != 0 || erbuf[SHORT-1] != '\0' || + ne != strlen(badpat)+1) { + fprintf(stderr, "end: regerror() short test gave `%s' not `%.*s'\n", + erbuf, SHORT-1, badpat); + status = 1; + } + ne = my_regerror(REG_ITOA|REG_BADPAT, (my_regex_t *)NULL, erbuf, sizeof(erbuf)); + if (strcmp(erbuf, bpname) != 0 || ne != strlen(bpname)+1) { + fprintf(stderr, "end: regerror() ITOA test gave `%s' not `%s'\n", + erbuf, bpname); + status = 1; + } + re.re_endp = bpname; + ne = my_regerror(REG_ATOI, &re, erbuf, sizeof(erbuf)); + if (atoi(erbuf) != (int)REG_BADPAT) { + fprintf(stderr, "end: regerror() ATOI test gave `%s' not `%ld'\n", + erbuf, (long)REG_BADPAT); + status = 1; + } else if (ne != strlen(erbuf)+1) { + fprintf(stderr, "end: regerror() ATOI test len(`%s') = %ld\n", + erbuf, (long)REG_BADPAT); + status = 1; + } +} + +/* + - rx_try - try it, and report on problems + == void rx_try(char *f0, char *f1, char *f2, char *f3, char *f4, int opts); + */ +void +rx_try(f0, f1, f2, f3, f4, opts) +char *f0; +char *f1; +char *f2; +char *f3; +char *f4; +int opts; /* may not match f1 */ +{ + my_regex_t re; +# define NSUBS 10 + my_regmatch_t subs[NSUBS]; +# define NSHOULD 15 + char *should[NSHOULD]; + int nshould; + char erbuf[100]; + int err; + int len; + const char *type = (opts & REG_EXTENDED) ? "ERE" : "BRE"; + register int i; + char *grump; + char f0copy[1000]; + char f2copy[1000]; + + strcpy(f0copy, f0); + re.re_endp = (opts®_PEND) ? f0copy + strlen(f0copy) : NULL; + fixstr(f0copy); + err = my_regcomp(&re, f0copy, opts, &my_charset_latin1); + if (err != 0 && (!opt('C', f1) || err != efind(f2))) { + /* unexpected error or wrong error */ + len = my_regerror(err, &re, erbuf, sizeof(erbuf)); + fprintf(stderr, "%d: %s error %s, %d/%d `%s'\n", + line, type, eprint(err), len, + (int) sizeof(erbuf), erbuf); + status = 1; + } else if (err == 0 && opt('C', f1)) { + /* unexpected success */ + fprintf(stderr, "%d: %s should have given REG_%s\n", + line, type, f2); + status = 1; + err = 1; /* so we won't try regexec */ + } + + if (err != 0) { + my_regfree(&re); + return; + } + + strcpy(f2copy, f2); + fixstr(f2copy); + + if (options('e', f1)®_STARTEND) { + if (strchr(f2, '(') == NULL || strchr(f2, ')') == NULL) + fprintf(stderr, "%d: bad STARTEND syntax\n", line); + subs[0].rm_so = strchr(f2, '(') - f2 + 1; + subs[0].rm_eo = strchr(f2, ')') - f2; + } + err = my_regexec(&re, f2copy, NSUBS, subs, options('e', f1)); + + if (err != 0 && (f3 != NULL || err != REG_NOMATCH)) { + /* unexpected error or wrong error */ + len = my_regerror(err, &re, erbuf, sizeof(erbuf)); + fprintf(stderr, "%d: %s exec error %s, %d/%d `%s'\n", + line, type, eprint(err), len, + (int) sizeof(erbuf), erbuf); + status = 1; + } else if (err != 0) { + /* nothing more to check */ + } else if (f3 == NULL) { + /* unexpected success */ + fprintf(stderr, "%d: %s exec should have failed\n", + line, type); + status = 1; + err = 1; /* just on principle */ + } else if (opts®_NOSUB) { + /* nothing more to check */ + } else if ((grump = check(f2, subs[0], f3)) != NULL) { + fprintf(stderr, "%d: %s %s\n", line, type, grump); + status = 1; + err = 1; + } + + if (err != 0 || f4 == NULL) { + my_regfree(&re); + return; + } + + for (i = 1; i < NSHOULD; i++) + should[i] = NULL; + nshould = split(f4, should+1, NSHOULD-1, ","); + if (nshould == 0) { + nshould = 1; + should[1] = (char*) ""; + } + for (i = 1; i < NSUBS; i++) { + grump = check(f2, subs[i], should[i]); + if (grump != NULL) { + fprintf(stderr, "%d: %s $%d %s\n", line, + type, i, grump); + status = 1; + err = 1; + } + } + + my_regfree(&re); +} + +/* + - options - pick options out of a regression-test string + == int options(int type, char *s); + */ +int +options(type, s) +int type; /* 'c' compile, 'e' exec */ +char *s; +{ + register char *p; + register int o = (type == 'c') ? copts : eopts; + register const char *legal = (type == 'c') ? "bisnmp" : "^$#tl"; + + for (p = s; *p != '\0'; p++) + if (strchr(legal, *p) != NULL) + switch (*p) { + case 'b': + o &= ~REG_EXTENDED; + break; + case 'i': + o |= REG_ICASE; + break; + case 's': + o |= REG_NOSUB; + break; + case 'n': + o |= REG_NEWLINE; + break; + case 'm': + o &= ~REG_EXTENDED; + o |= REG_NOSPEC; + break; + case 'p': + o |= REG_PEND; + break; + case '^': + o |= REG_NOTBOL; + break; + case '$': + o |= REG_NOTEOL; + break; + case '#': + o |= REG_STARTEND; + break; + case 't': /* trace */ + o |= REG_TRACE; + break; + case 'l': /* force long representation */ + o |= REG_LARGE; + break; + case 'r': /* force backref use */ + o |= REG_BACKR; + break; + } + return(o); +} + +/* + - opt - is a particular option in a regression string? + == int opt(int c, char *s); + */ +int /* predicate */ +opt(c, s) +int c; +char *s; +{ + return(strchr(s, c) != NULL); +} + +/* + - fixstr - transform magic characters in strings + == void fixstr(register char *p); + */ +void +fixstr(p) +register char *p; +{ + if (p == NULL) + return; + + for (; *p != '\0'; p++) + if (*p == 'N') + *p = '\n'; + else if (*p == 'T') + *p = '\t'; + else if (*p == 'S') + *p = ' '; + else if (*p == 'Z') + *p = '\0'; +} + +/* + - check - check a substring match + == char *check(char *str, regmatch_t sub, char *should); + */ +char * /* NULL or complaint */ +check(str, sub, should) +char *str; +my_regmatch_t sub; +char *should; +{ + register int len; + register int shlen; + register char *p; + static char grump[500]; + register char *at = NULL; + + if (should != NULL && strcmp(should, "-") == 0) + should = NULL; + if (should != NULL && should[0] == '@') { + at = should + 1; + should = (char*) ""; + } + + /* check rm_so and rm_eo for consistency */ + if (sub.rm_so > sub.rm_eo || (sub.rm_so == -1 && sub.rm_eo != -1) || + (sub.rm_so != -1 && sub.rm_eo == -1) || + (sub.rm_so != -1 && sub.rm_so < 0) || + (sub.rm_eo != -1 && sub.rm_eo < 0) ) { + sprintf(grump, "start %ld end %ld", (long)sub.rm_so, + (long)sub.rm_eo); + return(grump); + } + + /* check for no match */ + if (sub.rm_so == -1 && should == NULL) + return(NULL); + if (sub.rm_so == -1) + return((char*) "did not match"); + + /* check for in range */ + if ((int) sub.rm_eo > (int) strlen(str)) { + sprintf(grump, "start %ld end %ld, past end of string", + (long)sub.rm_so, (long)sub.rm_eo); + return(grump); + } + + len = (int)(sub.rm_eo - sub.rm_so); + shlen = (int)strlen(should); + p = str + sub.rm_so; + + /* check for not supposed to match */ + if (should == NULL) { + sprintf(grump, "matched `%.*s'", len, p); + return(grump); + } + + /* check for wrong match */ + if (len != shlen || strncmp(p, should, (size_t)shlen) != 0) { + sprintf(grump, "matched `%.*s' instead", len, p); + return(grump); + } + if (shlen > 0) + return(NULL); + + /* check null match in right place */ + if (at == NULL) + return(NULL); + shlen = strlen(at); + if (shlen == 0) + shlen = 1; /* force check for end-of-string */ + if (strncmp(p, at, shlen) != 0) { + sprintf(grump, "matched null at `%.20s'", p); + return(grump); + } + return(NULL); +} + +/* + - eprint - convert error number to name + == static char *eprint(int err); + */ +static char * +eprint(err) +int err; +{ + static char epbuf[100]; + size_t len; + + len = my_regerror(REG_ITOA|err, (my_regex_t *)NULL, epbuf, sizeof(epbuf)); + assert(len <= sizeof(epbuf)); + return(epbuf); +} + +/* + - efind - convert error name to number + == static int efind(char *name); + */ +static int +efind(name) +char *name; +{ + static char efbuf[100]; + my_regex_t re; + + sprintf(efbuf, "REG_%s", name); + assert(strlen(efbuf) < sizeof(efbuf)); + re.re_endp = efbuf; + (void) my_regerror(REG_ATOI, &re, efbuf, sizeof(efbuf)); + return(atoi(efbuf)); +} diff --git a/externals/mysql/extlib/regex/my_regex.h b/externals/mysql/extlib/regex/my_regex.h new file mode 100644 index 00000000000..0d1cedf5430 --- /dev/null +++ b/externals/mysql/extlib/regex/my_regex.h @@ -0,0 +1,86 @@ +#ifndef _REGEX_H_ +#define _REGEX_H_ /* never again */ +/* ========= begin header generated by ./mkh ========= */ +#ifdef __cplusplus +extern "C" { +#endif + +#include "m_ctype.h" + +/* === regex2.h === */ +#ifdef _WIN64 +typedef __int64 regoff_t; +#else +typedef off_t regoff_t; +#endif +struct re_guts; /* none of your business :-) */ +typedef struct { + int re_magic; + size_t re_nsub; /* number of parenthesized subexpressions */ + const char *re_endp; /* end pointer for REG_PEND */ + struct re_guts *re_g; /* none of your business :-) */ + CHARSET_INFO *charset; /* For ctype things */ +} my_regex_t; +typedef struct { + regoff_t rm_so; /* start of match */ + regoff_t rm_eo; /* end of match */ +} my_regmatch_t; + + +/* === regcomp.c === */ +extern int my_regcomp(my_regex_t *, const char *, int, CHARSET_INFO *charset); +#define REG_BASIC 0000 +#define REG_EXTENDED 0001 +#define REG_ICASE 0002 +#define REG_NOSUB 0004 +#define REG_NEWLINE 0010 +#define REG_NOSPEC 0020 +#define REG_PEND 0040 +#define REG_DUMP 0200 + + +/* === regerror.c === */ +#define REG_NOMATCH 1 +#define REG_BADPAT 2 +#define REG_ECOLLATE 3 +#define REG_ECTYPE 4 +#define REG_EESCAPE 5 +#define REG_ESUBREG 6 +#define REG_EBRACK 7 +#define REG_EPAREN 8 +#define REG_EBRACE 9 +#define REG_BADBR 10 +#define REG_ERANGE 11 +#define REG_ESPACE 12 +#define REG_BADRPT 13 +#define REG_EMPTY 14 +#define REG_ASSERT 15 +#define REG_INVARG 16 +#define REG_ATOI 255 /* convert name to number (!) */ +#define REG_ITOA 0400 /* convert number to name (!) */ +extern size_t my_regerror(int, const my_regex_t *, char *, size_t); + + +/* === regexec.c === */ +extern int my_regexec(const my_regex_t *, const char *, size_t, my_regmatch_t [], int); +#define REG_NOTBOL 00001 +#define REG_NOTEOL 00002 +#define REG_STARTEND 00004 +#define REG_TRACE 00400 /* tracing of execution */ +#define REG_LARGE 01000 /* force large representation */ +#define REG_BACKR 02000 /* force use of backref code */ + + +/* === regfree.c === */ +extern void my_regfree(my_regex_t *); + +/* === reginit.c === */ + +extern void my_regex_init(CHARSET_INFO *cs); /* Should be called for multithread progs */ +extern void my_regex_end(void); /* If one wants a clean end */ + +#ifdef __cplusplus +} +#endif +/* ========= end header generated by ./mkh ========= */ +#endif diff --git a/externals/mysql/extlib/regex/regcomp.c b/externals/mysql/extlib/regex/regcomp.c new file mode 100644 index 00000000000..398e132d97d --- /dev/null +++ b/externals/mysql/extlib/regex/regcomp.c @@ -0,0 +1,1672 @@ +#include <my_global.h> +#include <m_string.h> +#include <m_ctype.h> +#ifdef __WIN__ +#include <limits.h> +#endif + +#include "my_regex.h" +#include "utils.h" +#include "regex2.h" + +#include "cclass.h" +#include "cname.h" + +/* + * parse structure, passed up and down to avoid global variables and + * other clumsinesses + */ +struct parse { + char *next; /* next character in RE */ + char *end; /* end of string (-> NUL normally) */ + int error; /* has an error been seen? */ + sop *strip; /* malloced strip */ + sopno ssize; /* malloced strip size (allocated) */ + sopno slen; /* malloced strip length (used) */ + int ncsalloc; /* number of csets allocated */ + struct re_guts *g; +# define NPAREN 10 /* we need to remember () 1-9 for back refs */ + sopno pbegin[NPAREN]; /* -> ( ([0] unused) */ + sopno pend[NPAREN]; /* -> ) ([0] unused) */ + CHARSET_INFO *charset; /* for ctype things */ +}; + +#include "regcomp.ih" + +static char nuls[10]; /* place to point scanner in event of error */ + +struct cclass cclasses[CCLASS_LAST+1]= { + { "alnum", "","", _MY_U | _MY_L | _MY_NMR}, + { "alpha", "","", _MY_U | _MY_L }, + { "blank", "","", _MY_B }, + { "cntrl", "","", _MY_CTR }, + { "digit", "","", _MY_NMR }, + { "graph", "","", _MY_PNT | _MY_U | _MY_L | _MY_NMR}, + { "lower", "","", _MY_L }, + { "print", "","", _MY_PNT | _MY_U | _MY_L | _MY_NMR | _MY_B }, + { "punct", "","", _MY_PNT }, + { "space", "","", _MY_SPC }, + { "upper", "","", _MY_U }, + { "xdigit", "","", _MY_X }, + { NULL,NULL,NULL, 0 } +}; + +/* + * macros for use with parse structure + * BEWARE: these know that the parse structure is named `p' !!! + */ +#define PEEK() (*p->next) +#define PEEK2() (*(p->next+1)) +#define MORE() (p->next < p->end) +#define MORE2() (p->next+1 < p->end) +#define SEE(c) (MORE() && PEEK() == (c)) +#define SEETWO(a, b) (MORE() && MORE2() && PEEK() == (a) && PEEK2() == (b)) +#define EAT(c) ((SEE(c)) ? (NEXT(), 1) : 0) +#define EATTWO(a, b) ((SEETWO(a, b)) ? (NEXT2(), 1) : 0) +#define NEXT() (p->next++) +#define NEXT2() (p->next += 2) +#define NEXTn(n) (p->next += (n)) +#define GETNEXT() (*p->next++) +#define SETERROR(e) seterr(p, (e)) +#define REQUIRE(co, e) ((co) || SETERROR(e)) +#define MUSTSEE(c, e) (REQUIRE(MORE() && PEEK() == (c), e)) +#define MUSTEAT(c, e) (REQUIRE(MORE() && GETNEXT() == (c), e)) +#define MUSTNOTSEE(c, e) (REQUIRE(!MORE() || PEEK() != (c), e)) +#define EMIT(op, sopnd) doemit(p, (sop)(op), (size_t)(sopnd)) +#define INSERT(op, pos) doinsert(p, (sop)(op), HERE()-(pos)+1, pos) +#define AHEAD(pos) dofwd(p, pos, HERE()-(pos)) +#define ASTERN(sop, pos) EMIT(sop, HERE()-pos) +#define HERE() (p->slen) +#define THERE() (p->slen - 1) +#define THERETHERE() (p->slen - 2) +#define DROP(n) (p->slen -= (n)) + +#ifndef NDEBUG +static int never = 0; /* for use in asserts; shuts lint up */ +#else +#define never 0 /* some <assert.h>s have bugs too */ +#endif + +/* + - regcomp - interface for parser and compilation + = extern int regcomp(regex_t *, const char *, int); + = #define REG_BASIC 0000 + = #define REG_EXTENDED 0001 + = #define REG_ICASE 0002 + = #define REG_NOSUB 0004 + = #define REG_NEWLINE 0010 + = #define REG_NOSPEC 0020 + = #define REG_PEND 0040 + = #define REG_DUMP 0200 + */ +int /* 0 success, otherwise REG_something */ +my_regcomp(preg, pattern, cflags, charset) +my_regex_t *preg; +const char *pattern; +int cflags; +CHARSET_INFO *charset; +{ + struct parse pa; + register struct re_guts *g; + register struct parse *p = &pa; + register int i; + register size_t len; +#ifdef REDEBUG +# define GOODFLAGS(f) (f) +#else +# define GOODFLAGS(f) ((f)&~REG_DUMP) +#endif + + my_regex_init(charset); /* Init cclass if neaded */ + preg->charset=charset; + cflags = GOODFLAGS(cflags); + if ((cflags®_EXTENDED) && (cflags®_NOSPEC)) + return(REG_INVARG); + + if (cflags®_PEND) { + if (preg->re_endp < pattern) + return(REG_INVARG); + len = preg->re_endp - pattern; + } else + len = strlen((char *)pattern); + + /* do the mallocs early so failure handling is easy */ + g = (struct re_guts *)malloc(sizeof(struct re_guts) + + (NC-1)*sizeof(cat_t)); + if (g == NULL) + return(REG_ESPACE); + p->ssize = (long) (len/(size_t)2*(size_t)3 + (size_t)1); /* ugh */ + p->strip = (sop *)malloc(p->ssize * sizeof(sop)); + p->slen = 0; + if (p->strip == NULL) { + free((char *)g); + return(REG_ESPACE); + } + + /* set things up */ + p->g = g; + p->next = (char *)pattern; /* convenience; we do not modify it */ + p->end = p->next + len; + p->error = 0; + p->ncsalloc = 0; + p->charset = preg->charset; + for (i = 0; i < NPAREN; i++) { + p->pbegin[i] = 0; + p->pend[i] = 0; + } + g->csetsize = NC; + g->sets = NULL; + g->setbits = NULL; + g->ncsets = 0; + g->cflags = cflags; + g->iflags = 0; + g->nbol = 0; + g->neol = 0; + g->must = NULL; + g->mlen = 0; + g->nsub = 0; + g->ncategories = 1; /* category 0 is "everything else" */ + g->categories = &g->catspace[-(CHAR_MIN)]; + (void) memset((char *)g->catspace, 0, NC*sizeof(cat_t)); + g->backrefs = 0; + + /* do it */ + EMIT(OEND, 0); + g->firststate = THERE(); + if (cflags®_EXTENDED) + p_ere(p, OUT); + else if (cflags®_NOSPEC) + p_str(p); + else + p_bre(p, OUT, OUT); + EMIT(OEND, 0); + g->laststate = THERE(); + + /* tidy up loose ends and fill things in */ + categorize(p, g); + stripsnug(p, g); + findmust(p, g); + g->nplus = pluscount(p, g); + g->magic = MAGIC2; + preg->re_nsub = g->nsub; + preg->re_g = g; + preg->re_magic = MAGIC1; +#ifndef REDEBUG + /* not debugging, so can't rely on the assert() in regexec() */ + if (g->iflags&BAD) + SETERROR(REG_ASSERT); +#endif + + /* win or lose, we're done */ + if (p->error != 0) /* lose */ + my_regfree(preg); + return(p->error); +} + +/* + - p_ere - ERE parser top level, concatenation and alternation + == static void p_ere(register struct parse *p, int stop); + */ +static void +p_ere(p, stop) +register struct parse *p; +int stop; /* character this ERE should end at */ +{ + register char c; + register sopno prevback; + register sopno prevfwd; + register sopno conc; + register int first = 1; /* is this the first alternative? */ + LINT_INIT(prevback); LINT_INIT(prevfwd); + for (;;) { + /* do a bunch of concatenated expressions */ + conc = HERE(); + while (MORE() && (c = PEEK()) != '|' && c != stop) + p_ere_exp(p); + if(REQUIRE(HERE() != conc, REG_EMPTY)) {}/* require nonempty */ + + if (!EAT('|')) + break; /* NOTE BREAK OUT */ + + if (first) { + INSERT(OCH_, conc); /* offset is wrong */ + prevfwd = conc; + prevback = conc; + first = 0; + } + ASTERN(OOR1, prevback); + prevback = THERE(); + AHEAD(prevfwd); /* fix previous offset */ + prevfwd = HERE(); + EMIT(OOR2, 0); /* offset is very wrong */ + } + + if (!first) { /* tail-end fixups */ + AHEAD(prevfwd); + ASTERN(O_CH, prevback); + } + + assert(!MORE() || SEE(stop)); +} + +/* + - p_ere_exp - parse one subERE, an atom possibly followed by a repetition op + == static void p_ere_exp(register struct parse *p); + */ +static void +p_ere_exp(p) +register struct parse *p; +{ + register char c; + register sopno pos; + register int count; + register int count2; + register sopno subno; + int wascaret = 0; + + assert(MORE()); /* caller should have ensured this */ + c = GETNEXT(); + + pos = HERE(); + switch (c) { + case '(': + if(REQUIRE(MORE(), REG_EPAREN)) {} + p->g->nsub++; + subno = (sopno) p->g->nsub; + if (subno < NPAREN) + p->pbegin[subno] = HERE(); + EMIT(OLPAREN, subno); + if (!SEE(')')) + p_ere(p, ')'); + if (subno < NPAREN) { + p->pend[subno] = HERE(); + assert(p->pend[subno] != 0); + } + EMIT(ORPAREN, subno); + if(MUSTEAT(')', REG_EPAREN)) {} + break; +#ifndef POSIX_MISTAKE + case ')': /* happens only if no current unmatched ( */ + /* + * You may ask, why the ifndef? Because I didn't notice + * this until slightly too late for 1003.2, and none of the + * other 1003.2 regular-expression reviewers noticed it at + * all. So an unmatched ) is legal POSIX, at least until + * we can get it fixed. + */ + SETERROR(REG_EPAREN); + break; +#endif + case '^': + EMIT(OBOL, 0); + p->g->iflags |= USEBOL; + p->g->nbol++; + wascaret = 1; + break; + case '$': + EMIT(OEOL, 0); + p->g->iflags |= USEEOL; + p->g->neol++; + break; + case '|': + SETERROR(REG_EMPTY); + break; + case '*': + case '+': + case '?': + SETERROR(REG_BADRPT); + break; + case '.': + if (p->g->cflags®_NEWLINE) + nonnewline(p); + else + EMIT(OANY, 0); + break; + case '[': + p_bracket(p); + break; + case '\\': + if(REQUIRE(MORE(), REG_EESCAPE)) {} + c = GETNEXT(); + ordinary(p, c); + break; + case '{': /* okay as ordinary except if digit follows */ + if(REQUIRE(!MORE() || !my_isdigit(p->charset,PEEK()), REG_BADRPT)) {} + /* FALLTHROUGH */ + default: + ordinary(p, c); + break; + } + + if (!MORE()) + return; + c = PEEK(); + /* we call { a repetition if followed by a digit */ + if (!( c == '*' || c == '+' || c == '?' || + (c == '{' && MORE2() && + my_isdigit(p->charset,PEEK2())) )) + return; /* no repetition, we're done */ + NEXT(); + + if(REQUIRE(!wascaret, REG_BADRPT)) {} + switch (c) { + case '*': /* implemented as +? */ + /* this case does not require the (y|) trick, noKLUDGE */ + INSERT(OPLUS_, pos); + ASTERN(O_PLUS, pos); + INSERT(OQUEST_, pos); + ASTERN(O_QUEST, pos); + break; + case '+': + INSERT(OPLUS_, pos); + ASTERN(O_PLUS, pos); + break; + case '?': + /* KLUDGE: emit y? as (y|) until subtle bug gets fixed */ + INSERT(OCH_, pos); /* offset slightly wrong */ + ASTERN(OOR1, pos); /* this one's right */ + AHEAD(pos); /* fix the OCH_ */ + EMIT(OOR2, 0); /* offset very wrong... */ + AHEAD(THERE()); /* ...so fix it */ + ASTERN(O_CH, THERETHERE()); + break; + case '{': + count = p_count(p); + if (EAT(',')) { + if (my_isdigit(p->charset,PEEK())) { + count2 = p_count(p); + if(REQUIRE(count <= count2, REG_BADBR)) {} + } else /* single number with comma */ + count2 = RE_INFINITY; + } else /* just a single number */ + count2 = count; + repeat(p, pos, count, count2); + if (!EAT('}')) { /* error heuristics */ + while (MORE() && PEEK() != '}') + NEXT(); + if(REQUIRE(MORE(), REG_EBRACE)) {} + SETERROR(REG_BADBR); + } + break; + } + + if (!MORE()) + return; + c = PEEK(); + if (!( c == '*' || c == '+' || c == '?' || + (c == '{' && MORE2() && + my_isdigit(p->charset,PEEK2())) ) ) + return; + SETERROR(REG_BADRPT); +} + +/* + - p_str - string (no metacharacters) "parser" + == static void p_str(register struct parse *p); + */ +static void +p_str(p) +register struct parse *p; +{ + if(REQUIRE(MORE(), REG_EMPTY)) {} + while (MORE()) + ordinary(p, GETNEXT()); +} + +/* + - p_bre - BRE parser top level, anchoring and concatenation + == static void p_bre(register struct parse *p, register int end1, \ + == register int end2); + * Giving end1 as OUT essentially eliminates the end1/end2 check. + * + * This implementation is a bit of a kludge, in that a trailing $ is first + * taken as an ordinary character and then revised to be an anchor. The + * only undesirable side effect is that '$' gets included as a character + * category in such cases. This is fairly harmless; not worth fixing. + * The amount of lookahead needed to avoid this kludge is excessive. + */ +static void +p_bre(p, end1, end2) +register struct parse *p; +register int end1; /* first terminating character */ +register int end2; /* second terminating character */ +{ + register sopno start = HERE(); + register int first = 1; /* first subexpression? */ + register int wasdollar = 0; + + if (EAT('^')) { + EMIT(OBOL, 0); + p->g->iflags |= USEBOL; + p->g->nbol++; + } + while (MORE() && !SEETWO(end1, end2)) { + wasdollar = p_simp_re(p, first); + first = 0; + } + if (wasdollar) { /* oops, that was a trailing anchor */ + DROP(1); + EMIT(OEOL, 0); + p->g->iflags |= USEEOL; + p->g->neol++; + } + + if(REQUIRE(HERE() != start, REG_EMPTY)) {} /* require nonempty */ +} + +/* + - p_simp_re - parse a simple RE, an atom possibly followed by a repetition + == static int p_simp_re(register struct parse *p, int starordinary); + */ +static int /* was the simple RE an unbackslashed $? */ +p_simp_re(p, starordinary) +register struct parse *p; +int starordinary; /* is a leading * an ordinary character? */ +{ + register int c; + register int count; + register int count2; + register sopno pos; + register int i; + register sopno subno; +# define BACKSL (1<<CHAR_BIT) + + pos = HERE(); /* repetion op, if any, covers from here */ + + assert(MORE()); /* caller should have ensured this */ + c = GETNEXT(); + if (c == '\\') { + if(REQUIRE(MORE(), REG_EESCAPE)) {} + c = BACKSL | (unsigned char)GETNEXT(); + } + switch (c) { + case '.': + if (p->g->cflags®_NEWLINE) + nonnewline(p); + else + EMIT(OANY, 0); + break; + case '[': + p_bracket(p); + break; + case BACKSL|'{': + SETERROR(REG_BADRPT); + break; + case BACKSL|'(': + p->g->nsub++; + subno = (sopno) p->g->nsub; + if (subno < NPAREN) + p->pbegin[subno] = HERE(); + EMIT(OLPAREN, subno); + /* the MORE here is an error heuristic */ + if (MORE() && !SEETWO('\\', ')')) + p_bre(p, '\\', ')'); + if (subno < NPAREN) { + p->pend[subno] = HERE(); + assert(p->pend[subno] != 0); + } + EMIT(ORPAREN, subno); + if(REQUIRE(EATTWO('\\', ')'), REG_EPAREN)) {} + break; + case BACKSL|')': /* should not get here -- must be user */ + case BACKSL|'}': + SETERROR(REG_EPAREN); + break; + case BACKSL|'1': + case BACKSL|'2': + case BACKSL|'3': + case BACKSL|'4': + case BACKSL|'5': + case BACKSL|'6': + case BACKSL|'7': + case BACKSL|'8': + case BACKSL|'9': + i = (c&~BACKSL) - '0'; + assert(i < NPAREN); + if (p->pend[i] != 0) { + assert((uint) i <= p->g->nsub); + EMIT(OBACK_, i); + assert(p->pbegin[i] != 0); + assert(OP(p->strip[p->pbegin[i]]) == OLPAREN); + assert(OP(p->strip[p->pend[i]]) == ORPAREN); + (void) dupl(p, p->pbegin[i]+1, p->pend[i]); + EMIT(O_BACK, i); + } else + SETERROR(REG_ESUBREG); + p->g->backrefs = 1; + break; + case '*': + if(REQUIRE(starordinary, REG_BADRPT)) {} + /* FALLTHROUGH */ + default: + ordinary(p, c &~ BACKSL); + break; + } + + if (EAT('*')) { /* implemented as +? */ + /* this case does not require the (y|) trick, noKLUDGE */ + INSERT(OPLUS_, pos); + ASTERN(O_PLUS, pos); + INSERT(OQUEST_, pos); + ASTERN(O_QUEST, pos); + } else if (EATTWO('\\', '{')) { + count = p_count(p); + if (EAT(',')) { + if (MORE() && my_isdigit(p->charset,PEEK())) { + count2 = p_count(p); + if(REQUIRE(count <= count2, REG_BADBR)) {} + } else /* single number with comma */ + count2 = RE_INFINITY; + } else /* just a single number */ + count2 = count; + repeat(p, pos, count, count2); + if (!EATTWO('\\', '}')) { /* error heuristics */ + while (MORE() && !SEETWO('\\', '}')) + NEXT(); + if(REQUIRE(MORE(), REG_EBRACE)) {} + SETERROR(REG_BADBR); + } + } else if (c == (unsigned char)'$') /* $ (but not \$) ends it */ + return(1); + + return(0); +} + +/* + - p_count - parse a repetition count + == static int p_count(register struct parse *p); + */ +static int /* the value */ +p_count(p) +register struct parse *p; +{ + register int count = 0; + register int ndigits = 0; + + while (MORE() && my_isdigit(p->charset,PEEK()) && count <= DUPMAX) { + count = count*10 + (GETNEXT() - '0'); + ndigits++; + } + + if(REQUIRE(ndigits > 0 && count <= DUPMAX, REG_BADBR)) {} + return(count); +} + +/* + - p_bracket - parse a bracketed character list + == static void p_bracket(register struct parse *p); + * + * Note a significant property of this code: if the allocset() did SETERROR, + * no set operations are done. + */ +static void +p_bracket(p) +register struct parse *p; +{ + register cset *cs = allocset(p); + register int invert = 0; + + /* Dept of Truly Sickening Special-Case Kludges */ + if (p->next + 5 < p->end && strncmp(p->next, "[:<:]]", 6) == 0) { + EMIT(OBOW, 0); + NEXTn(6); + return; + } + if (p->next + 5 < p->end && strncmp(p->next, "[:>:]]", 6) == 0) { + EMIT(OEOW, 0); + NEXTn(6); + return; + } + + if (EAT('^')) + invert++; /* make note to invert set at end */ + if (EAT(']')) + CHadd(cs, ']'); + else if (EAT('-')) + CHadd(cs, '-'); + while (MORE() && PEEK() != ']' && !SEETWO('-', ']')) + p_b_term(p, cs); + if (EAT('-')) + CHadd(cs, '-'); + if(MUSTEAT(']', REG_EBRACK)) {} + + if (p->error != 0) /* don't mess things up further */ + return; + + if (p->g->cflags®_ICASE) { + register int i; + register int ci; + + for (i = p->g->csetsize - 1; i >= 0; i--) + if (CHIN(cs, i) && my_isalpha(p->charset,i)) { + ci = othercase(p->charset,i); + if (ci != i) + CHadd(cs, ci); + } + if (cs->multis != NULL) + mccase(p, cs); + } + if (invert) { + register int i; + + for (i = p->g->csetsize - 1; i >= 0; i--) + if (CHIN(cs, i)) + CHsub(cs, i); + else + CHadd(cs, i); + if (p->g->cflags®_NEWLINE) + CHsub(cs, '\n'); + if (cs->multis != NULL) + mcinvert(p, cs); + } + + assert(cs->multis == NULL); /* xxx */ + + if (nch(p, cs) == 1) { /* optimize singleton sets */ + ordinary(p, firstch(p, cs)); + freeset(p, cs); + } else + EMIT(OANYOF, freezeset(p, cs)); +} + +/* + - p_b_term - parse one term of a bracketed character list + == static void p_b_term(register struct parse *p, register cset *cs); + */ +static void +p_b_term(p, cs) +register struct parse *p; +register cset *cs; +{ + register char c; + register char start, finish; + register int i; + + /* classify what we've got */ + switch ((MORE()) ? PEEK() : '\0') { + case '[': + c = (MORE2()) ? PEEK2() : '\0'; + break; + case '-': + SETERROR(REG_ERANGE); + return; /* NOTE RETURN */ + break; + default: + c = '\0'; + break; + } + + switch (c) { + case ':': /* character class */ + NEXT2(); + if(REQUIRE(MORE(), REG_EBRACK)) {} + c = PEEK(); + if(REQUIRE(c != '-' && c != ']', REG_ECTYPE)) {} + p_b_cclass(p, cs); + if(REQUIRE(MORE(), REG_EBRACK)) {} + if(REQUIRE(EATTWO(':', ']'), REG_ECTYPE)) {} + break; + case '=': /* equivalence class */ + NEXT2(); + if(REQUIRE(MORE(), REG_EBRACK)) {} + c = PEEK(); + if(REQUIRE(c != '-' && c != ']', REG_ECOLLATE)) {} + p_b_eclass(p, cs); + if(REQUIRE(MORE(), REG_EBRACK)) {} + if(REQUIRE(EATTWO('=', ']'), REG_ECOLLATE)) {} + break; + default: /* symbol, ordinary character, or range */ +/* xxx revision needed for multichar stuff */ + start = p_b_symbol(p); + if (SEE('-') && MORE2() && PEEK2() != ']') { + /* range */ + NEXT(); + if (EAT('-')) + finish = '-'; + else + finish = p_b_symbol(p); + } else + finish = start; +/* xxx what about signed chars here... */ + if(REQUIRE(start <= finish, REG_ERANGE)) {} + for (i = start; i <= finish; i++) + CHadd(cs, i); + break; + } +} + +/* + - p_b_cclass - parse a character-class name and deal with it + == static void p_b_cclass(register struct parse *p, register cset *cs); + */ +static void +p_b_cclass(p, cs) +register struct parse *p; +register cset *cs; +{ + register char *sp = p->next; + register struct cclass *cp; + register size_t len; + + while (MORE() && my_isalpha(p->charset,PEEK())) + NEXT(); + len = p->next - sp; + for (cp = cclasses; cp->name != NULL; cp++) + if (strncmp(cp->name, sp, len) == 0 && cp->name[len] == '\0') + break; + if (cp->name == NULL) { + /* oops, didn't find it */ + SETERROR(REG_ECTYPE); + return; + } + +#ifndef USE_ORIG_REGEX_CODE + { + register size_t i; + for (i=1 ; i<256 ; i++) + if (p->charset->ctype[i+1] & cp->mask) + CHadd(cs, i); + } +#else + { + register char *u = (char*) cp->chars; + register char c; + + while ((c = *u++) != '\0') + CHadd(cs, c); + + for (u = (char*) cp->multis; *u != '\0'; u += strlen(u) + 1) + MCadd(p, cs, u); + } +#endif + +} + +/* + - p_b_eclass - parse an equivalence-class name and deal with it + == static void p_b_eclass(register struct parse *p, register cset *cs); + * + * This implementation is incomplete. xxx + */ +static void +p_b_eclass(p, cs) +register struct parse *p; +register cset *cs; +{ + register char c; + + c = p_b_coll_elem(p, '='); + CHadd(cs, c); +} + +/* + - p_b_symbol - parse a character or [..]ed multicharacter collating symbol + == static char p_b_symbol(register struct parse *p); + */ +static char /* value of symbol */ +p_b_symbol(p) +register struct parse *p; +{ + register char value; + + if(REQUIRE(MORE(), REG_EBRACK)) {} + if (!EATTWO('[', '.')) + return(GETNEXT()); + + /* collating symbol */ + value = p_b_coll_elem(p, '.'); + if(REQUIRE(EATTWO('.', ']'), REG_ECOLLATE)) {} + return(value); +} + +/* + - p_b_coll_elem - parse a collating-element name and look it up + == static char p_b_coll_elem(register struct parse *p, int endc); + */ +static char /* value of collating element */ +p_b_coll_elem(p, endc) +register struct parse *p; +int endc; /* name ended by endc,']' */ +{ + register char *sp = p->next; + register struct cname *cp; +#ifdef _WIN64 + register __int64 len; +#else + register int len; +#endif + while (MORE() && !SEETWO(endc, ']')) + NEXT(); + if (!MORE()) { + SETERROR(REG_EBRACK); + return(0); + } + len = p->next - sp; + for (cp = cnames; cp->name != NULL; cp++) + if (strncmp(cp->name, sp, len) == 0 && cp->name[len] == '\0') + return(cp->code); /* known name */ + if (len == 1) + return(*sp); /* single character */ + SETERROR(REG_ECOLLATE); /* neither */ + return(0); +} + +/* + - othercase - return the case counterpart of an alphabetic + == static char othercase(int ch); + */ +static char /* if no counterpart, return ch */ +othercase(charset,ch) +CHARSET_INFO *charset; +int ch; +{ + /* + In MySQL some multi-byte character sets + have 'ctype' array but don't have 'to_lower' + and 'to_upper' arrays. In this case we handle + only basic latin letters a..z and A..Z. + + If 'to_lower' and 'to_upper' arrays are empty in a character set, + then my_isalpha(cs, ch) should never return TRUE for characters + other than basic latin letters. Otherwise it should be + considered as a mistake in character set definition. + */ + assert(my_isalpha(charset,ch)); + if (my_isupper(charset,ch)) + { + return(charset->to_lower ? my_tolower(charset,ch) : + ch - 'A' + 'a'); + } + else if (my_islower(charset,ch)) + { + return(charset->to_upper ? my_toupper(charset,ch) : + ch - 'a' + 'A'); + } + else /* peculiar, but could happen */ + return(ch); +} + +/* + - bothcases - emit a dualcase version of a two-case character + == static void bothcases(register struct parse *p, int ch); + * + * Boy, is this implementation ever a kludge... + */ +static void +bothcases(p, ch) +register struct parse *p; +int ch; +{ + register char *oldnext = p->next; + register char *oldend = p->end; + char bracket[3]; + + assert(othercase(p->charset, ch) != ch); /* p_bracket() would recurse */ + p->next = bracket; + p->end = bracket+2; + bracket[0] = ch; + bracket[1] = ']'; + bracket[2] = '\0'; + p_bracket(p); + assert(p->next == bracket+2); + p->next = oldnext; + p->end = oldend; +} + +/* + - ordinary - emit an ordinary character + == static void ordinary(register struct parse *p, register int ch); + */ +static void +ordinary(p, ch) +register struct parse *p; +register int ch; +{ + register cat_t *cap = p->g->categories; + + if ((p->g->cflags®_ICASE) && my_isalpha(p->charset,ch) && + othercase(p->charset,ch) != ch) + bothcases(p, ch); + else { + EMIT(OCHAR, (unsigned char)ch); + if (cap[ch] == 0) + cap[ch] = p->g->ncategories++; + } +} + +/* + - nonnewline - emit REG_NEWLINE version of OANY + == static void nonnewline(register struct parse *p); + * + * Boy, is this implementation ever a kludge... + */ +static void +nonnewline(p) +register struct parse *p; +{ + register char *oldnext = p->next; + register char *oldend = p->end; + char bracket[4]; + + p->next = bracket; + p->end = bracket+3; + bracket[0] = '^'; + bracket[1] = '\n'; + bracket[2] = ']'; + bracket[3] = '\0'; + p_bracket(p); + assert(p->next == bracket+3); + p->next = oldnext; + p->end = oldend; +} + +/* + - repeat - generate code for a bounded repetition, recursively if needed + == static void repeat(register struct parse *p, sopno start, int from, int to); + */ +static void +repeat(p, start, from, to) +register struct parse *p; +sopno start; /* operand from here to end of strip */ +int from; /* repeated from this number */ +int to; /* to this number of times (maybe RE_INFINITY) */ +{ + register sopno finish = HERE(); +# define N 2 +# define INF 3 +# define REP(f, t) ((f)*8 + (t)) +# define MAP(n) (((n) <= 1) ? (n) : ((n) == RE_INFINITY) ? INF : N) + register sopno copy; + + if (p->error != 0) /* head off possible runaway recursion */ + return; + + assert(from <= to); + + switch (REP(MAP(from), MAP(to))) { + case REP(0, 0): /* must be user doing this */ + DROP(finish-start); /* drop the operand */ + break; + case REP(0, 1): /* as x{1,1}? */ + case REP(0, N): /* as x{1,n}? */ + case REP(0, INF): /* as x{1,}? */ + /* KLUDGE: emit y? as (y|) until subtle bug gets fixed */ + INSERT(OCH_, start); /* offset is wrong... */ + repeat(p, start+1, 1, to); + ASTERN(OOR1, start); + AHEAD(start); /* ... fix it */ + EMIT(OOR2, 0); + AHEAD(THERE()); + ASTERN(O_CH, THERETHERE()); + break; + case REP(1, 1): /* trivial case */ + /* done */ + break; + case REP(1, N): /* as x?x{1,n-1} */ + /* KLUDGE: emit y? as (y|) until subtle bug gets fixed */ + INSERT(OCH_, start); + ASTERN(OOR1, start); + AHEAD(start); + EMIT(OOR2, 0); /* offset very wrong... */ + AHEAD(THERE()); /* ...so fix it */ + ASTERN(O_CH, THERETHERE()); + copy = dupl(p, start+1, finish+1); + assert(copy == finish+4); + repeat(p, copy, 1, to-1); + break; + case REP(1, INF): /* as x+ */ + INSERT(OPLUS_, start); + ASTERN(O_PLUS, start); + break; + case REP(N, N): /* as xx{m-1,n-1} */ + copy = dupl(p, start, finish); + repeat(p, copy, from-1, to-1); + break; + case REP(N, INF): /* as xx{n-1,INF} */ + copy = dupl(p, start, finish); + repeat(p, copy, from-1, to); + break; + default: /* "can't happen" */ + SETERROR(REG_ASSERT); /* just in case */ + break; + } +} + +/* + - seterr - set an error condition + == static int seterr(register struct parse *p, int e); + */ +static int /* useless but makes type checking happy */ +seterr(p, e) +register struct parse *p; +int e; +{ + if (p->error == 0) /* keep earliest error condition */ + p->error = e; + p->next = nuls; /* try to bring things to a halt */ + p->end = nuls; + return(0); /* make the return value well-defined */ +} + +/* + - allocset - allocate a set of characters for [] + == static cset *allocset(register struct parse *p); + */ +static cset * +allocset(p) +register struct parse *p; +{ + register int no = p->g->ncsets++; + register size_t nc; + register size_t nbytes; + register cset *cs; + register size_t css = (size_t)p->g->csetsize; + register int i; + + if (no >= p->ncsalloc) { /* need another column of space */ + p->ncsalloc += CHAR_BIT; + nc = p->ncsalloc; + assert(nc % CHAR_BIT == 0); + nbytes = nc / CHAR_BIT * css; + if (p->g->sets == NULL) + p->g->sets = (cset *)malloc(nc * sizeof(cset)); + else + p->g->sets = (cset *)realloc((char *)p->g->sets, + nc * sizeof(cset)); + if (p->g->setbits == NULL) + p->g->setbits = (uch *)malloc(nbytes); + else { + p->g->setbits = (uch *)realloc((char *)p->g->setbits, + nbytes); + /* xxx this isn't right if setbits is now NULL */ + for (i = 0; i < no; i++) + p->g->sets[i].ptr = p->g->setbits + css*(i/CHAR_BIT); + } + if (p->g->sets != NULL && p->g->setbits != NULL) + (void) memset((char *)p->g->setbits + (nbytes - css), + 0, css); + else { + no = 0; + SETERROR(REG_ESPACE); + /* caller's responsibility not to do set ops */ + } + } + + assert(p->g->sets != NULL); /* xxx */ + cs = &p->g->sets[no]; + cs->ptr = p->g->setbits + css*((no)/CHAR_BIT); + cs->mask = 1 << ((no) % CHAR_BIT); + cs->hash = 0; + cs->smultis = 0; + cs->multis = NULL; + + return(cs); +} + +/* + - freeset - free a now-unused set + == static void freeset(register struct parse *p, register cset *cs); + */ +static void +freeset(p, cs) +register struct parse *p; +register cset *cs; +{ + register size_t i; + register cset *top = &p->g->sets[p->g->ncsets]; + register size_t css = (size_t)p->g->csetsize; + + for (i = 0; i < css; i++) + CHsub(cs, i); + if (cs == top-1) /* recover only the easy case */ + p->g->ncsets--; +} + +/* + - freezeset - final processing on a set of characters + == static int freezeset(register struct parse *p, register cset *cs); + * + * The main task here is merging identical sets. This is usually a waste + * of time (although the hash code minimizes the overhead), but can win + * big if REG_ICASE is being used. REG_ICASE, by the way, is why the hash + * is done using addition rather than xor -- all ASCII [aA] sets xor to + * the same value! + */ +static int /* set number */ +freezeset(p, cs) +register struct parse *p; +register cset *cs; +{ + register uch h = cs->hash; + register size_t i; + register cset *top = &p->g->sets[p->g->ncsets]; + register cset *cs2; + register size_t css = (size_t)p->g->csetsize; + + /* look for an earlier one which is the same */ + for (cs2 = &p->g->sets[0]; cs2 < top; cs2++) + if (cs2->hash == h && cs2 != cs) { + /* maybe */ + for (i = 0; i < css; i++) + if (!!CHIN(cs2, i) != !!CHIN(cs, i)) + break; /* no */ + if (i == css) + break; /* yes */ + } + + if (cs2 < top) { /* found one */ + freeset(p, cs); + cs = cs2; + } + + return((int)(cs - p->g->sets)); +} + +/* + - firstch - return first character in a set (which must have at least one) + == static int firstch(register struct parse *p, register cset *cs); + */ +static int /* character; there is no "none" value */ +firstch(p, cs) +register struct parse *p; +register cset *cs; +{ + register size_t i; + register size_t css = (size_t)p->g->csetsize; + + for (i = 0; i < css; i++) + if (CHIN(cs, i)) + return((char)i); + assert(never); + return(0); /* arbitrary */ +} + +/* + - nch - number of characters in a set + == static int nch(register struct parse *p, register cset *cs); + */ +static int +nch(p, cs) +register struct parse *p; +register cset *cs; +{ + register size_t i; + register size_t css = (size_t)p->g->csetsize; + register int n = 0; + + for (i = 0; i < css; i++) + if (CHIN(cs, i)) + n++; + return(n); +} + +#ifdef USE_ORIG_REGEX_CODE +/* + - mcadd - add a collating element to a cset + == static void mcadd(register struct parse *p, register cset *cs, \ + == register char *cp); + */ +static void +mcadd(p, cs, cp) +register struct parse *p; +register cset *cs; +register char *cp; +{ + register size_t oldend = cs->smultis; + + cs->smultis += strlen(cp) + 1; + if (cs->multis == NULL) + cs->multis = malloc(cs->smultis); + else + cs->multis = realloc(cs->multis, cs->smultis); + if (cs->multis == NULL) { + SETERROR(REG_ESPACE); + return; + } + + (void) strcpy(cs->multis + oldend - 1, cp); + cs->multis[cs->smultis - 1] = '\0'; +} +#endif + +#ifdef NOT_USED +/* + - mcsub - subtract a collating element from a cset + == static void mcsub(register cset *cs, register char *cp); + */ +static void +mcsub(cs, cp) +register cset *cs; +register char *cp; +{ + register char *fp = mcfind(cs, cp); + register size_t len = strlen(fp); + + assert(fp != NULL); + (void) memmove(fp, fp + len + 1, + cs->smultis - (fp + len + 1 - cs->multis)); + cs->smultis -= len; + + if (cs->smultis == 0) { + free(cs->multis); + cs->multis = NULL; + return; + } + + cs->multis = realloc(cs->multis, cs->smultis); + assert(cs->multis != NULL); +} + +/* + - mcin - is a collating element in a cset? + == static int mcin(register cset *cs, register char *cp); + */ +static int +mcin(cs, cp) +register cset *cs; +register char *cp; +{ + return(mcfind(cs, cp) != NULL); +} + +/* + - mcfind - find a collating element in a cset + == static char *mcfind(register cset *cs, register char *cp); + */ +static char * +mcfind(cs, cp) +register cset *cs; +register char *cp; +{ + register char *p; + + if (cs->multis == NULL) + return(NULL); + for (p = cs->multis; *p != '\0'; p += strlen(p) + 1) + if (strcmp(cp, p) == 0) + return(p); + return(NULL); +} +#endif + +/* + - mcinvert - invert the list of collating elements in a cset + == static void mcinvert(register struct parse *p, register cset *cs); + * + * This would have to know the set of possibilities. Implementation + * is deferred. + */ +static void +mcinvert(p, cs) + register struct parse *p __attribute__((unused)); + register cset *cs __attribute__((unused)); +{ + assert(cs->multis == NULL); /* xxx */ +} + +/* + - mccase - add case counterparts of the list of collating elements in a cset + == static void mccase(register struct parse *p, register cset *cs); + * + * This would have to know the set of possibilities. Implementation + * is deferred. + */ +static void +mccase(p, cs) +register struct parse *p __attribute__((unused)); +register cset *cs __attribute__((unused)); +{ + assert(cs->multis == NULL); /* xxx */ +} + +/* + - isinsets - is this character in any sets? + == static int isinsets(register struct re_guts *g, int c); + */ +static int /* predicate */ +isinsets(g, c) +register struct re_guts *g; +int c; +{ + register uch *col; + register int i; + register int ncols = (g->ncsets+(CHAR_BIT-1)) / CHAR_BIT; + register unsigned uc = (unsigned char)c; + + for (i = 0, col = g->setbits; i < ncols; i++, col += g->csetsize) + if (col[uc] != 0) + return(1); + return(0); +} + +/* + - samesets - are these two characters in exactly the same sets? + == static int samesets(register struct re_guts *g, int c1, int c2); + */ +static int /* predicate */ +samesets(g, c1, c2) +register struct re_guts *g; +int c1; +int c2; +{ + register uch *col; + register int i; + register int ncols = (g->ncsets+(CHAR_BIT-1)) / CHAR_BIT; + register unsigned uc1 = (unsigned char)c1; + register unsigned uc2 = (unsigned char)c2; + + for (i = 0, col = g->setbits; i < ncols; i++, col += g->csetsize) + if (col[uc1] != col[uc2]) + return(0); + return(1); +} + +/* + - categorize - sort out character categories + == static void categorize(struct parse *p, register struct re_guts *g); + */ +static void +categorize(p, g) +struct parse *p; +register struct re_guts *g; +{ + register cat_t *cats = g->categories; + register int c; + register int c2; + register cat_t cat; + + /* avoid making error situations worse */ + if (p->error != 0) + return; + + for (c = CHAR_MIN; c <= CHAR_MAX; c++) + if (cats[c] == 0 && isinsets(g, c)) { + cat = g->ncategories++; + cats[c] = cat; + for (c2 = c+1; c2 <= CHAR_MAX; c2++) + if (cats[c2] == 0 && samesets(g, c, c2)) + cats[c2] = cat; + } +} + +/* + - dupl - emit a duplicate of a bunch of sops + == static sopno dupl(register struct parse *p, sopno start, sopno finish); + */ +static sopno /* start of duplicate */ +dupl(p, start, finish) +register struct parse *p; +sopno start; /* from here */ +sopno finish; /* to this less one */ +{ + register sopno ret = HERE(); + register sopno len = finish - start; + + assert(finish >= start); + if (len == 0) + return(ret); + enlarge(p, p->ssize + len); /* this many unexpected additions */ + assert(p->ssize >= p->slen + len); + (void) memcpy((char *)(p->strip + p->slen), + (char *)(p->strip + start), (size_t)len*sizeof(sop)); + p->slen += len; + return(ret); +} + +/* + - doemit - emit a strip operator + == static void doemit(register struct parse *p, sop op, size_t opnd); + * + * It might seem better to implement this as a macro with a function as + * hard-case backup, but it's just too big and messy unless there are + * some changes to the data structures. Maybe later. + */ +static void +doemit(p, op, opnd) +register struct parse *p; +sop op; +size_t opnd; +{ + /* avoid making error situations worse */ + if (p->error != 0) + return; + + /* deal with oversize operands ("can't happen", more or less) */ + assert(opnd < 1<<OPSHIFT); + + /* deal with undersized strip */ + if (p->slen >= p->ssize) + enlarge(p, (p->ssize+1) / 2 * 3); /* +50% */ + assert(p->slen < p->ssize); + + /* finally, it's all reduced to the easy case */ + p->strip[p->slen++] = SOP(op, opnd); +} + +/* + - doinsert - insert a sop into the strip + == static void doinsert(register struct parse *p, sop op, size_t opnd, sopno pos); + */ +static void +doinsert(p, op, opnd, pos) +register struct parse *p; +sop op; +size_t opnd; +sopno pos; +{ + register sopno sn; + register sop s; + register int i; + + /* avoid making error situations worse */ + if (p->error != 0) + return; + + sn = HERE(); + EMIT(op, opnd); /* do checks, ensure space */ + assert(HERE() == sn+1); + s = p->strip[sn]; + + /* adjust paren pointers */ + assert(pos > 0); + for (i = 1; i < NPAREN; i++) { + if (p->pbegin[i] >= pos) { + p->pbegin[i]++; + } + if (p->pend[i] >= pos) { + p->pend[i]++; + } + } + { + int length=(HERE()-pos-1)*sizeof(sop); + bmove_upp((uchar *) &p->strip[pos+1]+length, + (uchar *) &p->strip[pos]+length, + length); + } +#ifdef OLD_CODE + memmove((char *)&p->strip[pos+1], (char *)&p->strip[pos], + (HERE()-pos-1)*sizeof(sop)); +#endif + p->strip[pos] = s; +} + +/* + - dofwd - complete a forward reference + == static void dofwd(register struct parse *p, sopno pos, sop value); + */ +static void +dofwd(p, pos, value) +register struct parse *p; +register sopno pos; +sop value; +{ + /* avoid making error situations worse */ + if (p->error != 0) + return; + + assert(value < 1<<OPSHIFT); + p->strip[pos] = OP(p->strip[pos]) | value; +} + +/* + - enlarge - enlarge the strip + == static void enlarge(register struct parse *p, sopno size); + */ +static void +enlarge(p, size) +register struct parse *p; +register sopno size; +{ + register sop *sp; + + if (p->ssize >= size) + return; + + sp = (sop *)realloc(p->strip, size*sizeof(sop)); + if (sp == NULL) { + SETERROR(REG_ESPACE); + return; + } + p->strip = sp; + p->ssize = size; +} + +/* + - stripsnug - compact the strip + == static void stripsnug(register struct parse *p, register struct re_guts *g); + */ +static void +stripsnug(p, g) +register struct parse *p; +register struct re_guts *g; +{ + g->nstates = p->slen; + g->strip = (sop *)realloc((char *)p->strip, p->slen * sizeof(sop)); + if (g->strip == NULL) { + SETERROR(REG_ESPACE); + g->strip = p->strip; + } +} + +/* + - findmust - fill in must and mlen with longest mandatory literal string + == static void findmust(register struct parse *p, register struct re_guts *g); + * + * This algorithm could do fancy things like analyzing the operands of | + * for common subsequences. Someday. This code is simple and finds most + * of the interesting cases. + * + * Note that must and mlen got initialized during setup. + */ +static void +findmust(p, g) +struct parse *p; +register struct re_guts *g; +{ + register sop *scan; + sop *start; + register sop *newstart; + register sopno newlen; + register sop s; + register char *cp; + register sopno i; + LINT_INIT(start); LINT_INIT(newstart); + /* avoid making error situations worse */ + if (p->error != 0) + return; + + /* find the longest OCHAR sequence in strip */ + newlen = 0; + scan = g->strip + 1; + do { + s = *scan++; + switch (OP(s)) { + case OCHAR: /* sequence member */ + if (newlen == 0) /* new sequence */ + newstart = scan - 1; + newlen++; + break; + case OPLUS_: /* things that don't break one */ + case OLPAREN: + case ORPAREN: + break; + case OQUEST_: /* things that must be skipped */ + case OCH_: + scan--; + do { + scan += OPND(s); + s = *scan; + /* assert() interferes w debug printouts */ + if (OP(s) != O_QUEST && OP(s) != O_CH && + OP(s) != OOR2) { + g->iflags |= BAD; + return; + } + } while (OP(s) != O_QUEST && OP(s) != O_CH); + /* fallthrough */ + default: /* things that break a sequence */ + if (newlen > g->mlen) { /* ends one */ + start = newstart; + g->mlen = newlen; + } + newlen = 0; + break; + } + } while (OP(s) != OEND); + + if (g->mlen == 0) /* there isn't one */ + return; + + /* turn it into a character string */ + g->must = malloc((size_t)g->mlen + 1); + if (g->must == NULL) { /* argh; just forget it */ + g->mlen = 0; + return; + } + cp = g->must; + scan = start; + for (i = g->mlen; i > 0; i--) { + while (OP(s = *scan++) != OCHAR) + continue; + assert(cp < g->must + g->mlen); + *cp++ = (char)OPND(s); + } + assert(cp == g->must + g->mlen); + *cp++ = '\0'; /* just on general principles */ +} + +/* + - pluscount - count + nesting + == static sopno pluscount(register struct parse *p, register struct re_guts *g); + */ +static sopno /* nesting depth */ +pluscount(p, g) +struct parse *p; +register struct re_guts *g; +{ + register sop *scan; + register sop s; + register sopno plusnest = 0; + register sopno maxnest = 0; + + if (p->error != 0) + return(0); /* there may not be an OEND */ + + scan = g->strip + 1; + do { + s = *scan++; + switch (OP(s)) { + case OPLUS_: + plusnest++; + break; + case O_PLUS: + if (plusnest > maxnest) + maxnest = plusnest; + plusnest--; + break; + } + } while (OP(s) != OEND); + if (plusnest != 0) + g->iflags |= BAD; + return(maxnest); +} diff --git a/externals/mysql/extlib/regex/regerror.c b/externals/mysql/extlib/regex/regerror.c new file mode 100644 index 00000000000..489f2e35abb --- /dev/null +++ b/externals/mysql/extlib/regex/regerror.c @@ -0,0 +1,116 @@ +#include <my_global.h> +#include <m_string.h> +#include <m_ctype.h> + +#include "my_regex.h" +#include "utils.h" +#include "regerror.ih" + +/* + = #define REG_NOMATCH 1 + = #define REG_BADPAT 2 + = #define REG_ECOLLATE 3 + = #define REG_ECTYPE 4 + = #define REG_EESCAPE 5 + = #define REG_ESUBREG 6 + = #define REG_EBRACK 7 + = #define REG_EPAREN 8 + = #define REG_EBRACE 9 + = #define REG_BADBR 10 + = #define REG_ERANGE 11 + = #define REG_ESPACE 12 + = #define REG_BADRPT 13 + = #define REG_EMPTY 14 + = #define REG_ASSERT 15 + = #define REG_INVARG 16 + = #define REG_ATOI 255 // convert name to number (!) + = #define REG_ITOA 0400 // convert number to name (!) + */ +static struct rerr { + int code; + const char *name; + const char *explain; +} rerrs[] = { + {REG_NOMATCH, "REG_NOMATCH", "regexec() failed to match"}, + {REG_BADPAT, "REG_BADPAT", "invalid regular expression"}, + {REG_ECOLLATE, "REG_ECOLLATE", "invalid collating element"}, + {REG_ECTYPE, "REG_ECTYPE", "invalid character class"}, + {REG_EESCAPE, "REG_EESCAPE", "trailing backslash (\\)"}, + {REG_ESUBREG, "REG_ESUBREG", "invalid backreference number"}, + {REG_EBRACK, "REG_EBRACK", "brackets ([ ]) not balanced"}, + {REG_EPAREN, "REG_EPAREN", "parentheses not balanced"}, + {REG_EBRACE, "REG_EBRACE", "braces not balanced"}, + {REG_BADBR, "REG_BADBR", "invalid repetition count(s)"}, + {REG_ERANGE, "REG_ERANGE", "invalid character range"}, + {REG_ESPACE, "REG_ESPACE", "out of memory"}, + {REG_BADRPT, "REG_BADRPT", "repetition-operator operand invalid"}, + {REG_EMPTY, "REG_EMPTY", "empty (sub)expression"}, + {REG_ASSERT, "REG_ASSERT", "\"can't happen\" -- you found a bug"}, + {REG_INVARG, "REG_INVARG", "invalid argument to regex routine"}, + {0, "", "*** unknown regexp error code ***"}, +}; + +/* + - regerror - the interface to error numbers + = extern size_t regerror(int, const regex_t *, char *, size_t); + */ +/* ARGSUSED */ +size_t +my_regerror(int errcode, const my_regex_t *preg, char *errbuf, size_t errbuf_size) +{ + register struct rerr *r; + register size_t len; + register int target = errcode &~ REG_ITOA; + register char *s; + char convbuf[50]; + + if (errcode == REG_ATOI) + s = regatoi(preg, convbuf); + else { + for (r = rerrs; r->code != 0; r++) + if (r->code == target) + break; + + if (errcode®_ITOA) { + if (r->code != 0) + (void) strcpy(convbuf, r->name); + else + sprintf(convbuf, "REG_0x%x", target); + assert(strlen(convbuf) < sizeof(convbuf)); + s = convbuf; + } else + s = (char*) r->explain; + } + + len = strlen(s) + 1; + if (errbuf_size > 0) { + if (errbuf_size > len) + (void) strcpy(errbuf, s); + else { + (void) strncpy(errbuf, s, errbuf_size-1); + errbuf[errbuf_size-1] = '\0'; + } + } + + return(len); +} + +/* + - regatoi - internal routine to implement REG_ATOI + == static char *regatoi(const regex_t *preg, char *localbuf); + */ +static char * +regatoi(preg, localbuf) +const my_regex_t *preg; +char *localbuf; +{ + register struct rerr *r; + for (r = rerrs; r->code != 0; r++) + if (strcmp(r->name, preg->re_endp) == 0) + break; + if (r->code == 0) + return((char*) "0"); + + sprintf(localbuf, "%d", r->code); + return(localbuf); +} diff --git a/externals/mysql/extlib/regex/regex2.h b/externals/mysql/extlib/regex/regex2.h new file mode 100644 index 00000000000..bba54ea2054 --- /dev/null +++ b/externals/mysql/extlib/regex/regex2.h @@ -0,0 +1,145 @@ +/* + * First, the stuff that ends up in the outside-world include file + = typedef off_t regoff_t; + = typedef struct { + = int re_magic; + = size_t re_nsub; // number of parenthesized subexpressions + = const char *re_endp; // end pointer for REG_PEND + = struct re_guts *re_g; // none of your business :-) + = } regex_t; + = typedef struct { + = regoff_t rm_so; // start of match + = regoff_t rm_eo; // end of match + = } regmatch_t; + */ +/* + * internals of regex_t + */ +#ifndef __regex2_h__ +#define __regex2_h__ + +#define MAGIC1 ((('r'^0200)<<8) | 'e') + +/* + * The internal representation is a *strip*, a sequence of + * operators ending with an endmarker. (Some terminology etc. is a + * historical relic of earlier versions which used multiple strips.) + * Certain oddities in the representation are there to permit running + * the machinery backwards; in particular, any deviation from sequential + * flow must be marked at both its source and its destination. Some + * fine points: + * + * - OPLUS_ and O_PLUS are *inside* the loop they create. + * - OQUEST_ and O_QUEST are *outside* the bypass they create. + * - OCH_ and O_CH are *outside* the multi-way branch they create, while + * OOR1 and OOR2 are respectively the end and the beginning of one of + * the branches. Note that there is an implicit OOR2 following OCH_ + * and an implicit OOR1 preceding O_CH. + * + * In state representations, an operator's bit is on to signify a state + * immediately *preceding* "execution" of that operator. + */ +typedef unsigned long sop; /* strip operator */ +typedef long sopno; +#define OPRMASK 0xf8000000 +#define OPDMASK 0x07ffffff +#define OPSHIFT ((unsigned long)27) +#define OP(n) ((unsigned long) ((n)&OPRMASK)) +#define OPND(n) ((unsigned long) ((n)&OPDMASK)) +#define SOP(op, opnd) (unsigned long) ((op)|(opnd)) +#define OSHIFT(A) ((unsigned long) (A) << OPSHIFT) +/* operators meaning operand */ +/* (back, fwd are offsets) */ +#define OEND OSHIFT(1) /* endmarker - */ +#define OCHAR OSHIFT(2) /* character unsigned char */ +#define OBOL OSHIFT(3) /* left anchor - */ +#define OEOL OSHIFT(4) /* right anchor - */ +#define OANY OSHIFT(5) /* . - */ +#define OANYOF OSHIFT(6) /* [...] set number */ +#define OBACK_ OSHIFT(7) /* begin \d paren number */ +#define O_BACK OSHIFT(8) /* end \d paren number */ +#define OPLUS_ OSHIFT(9) /* + prefix fwd to suffix */ +#define O_PLUS OSHIFT(10) /* + suffix back to prefix */ +#define OQUEST_ OSHIFT(11) /* ? prefix fwd to suffix */ +#define O_QUEST OSHIFT(12) /* ? suffix back to prefix */ +#define OLPAREN OSHIFT(13) /* ( fwd to ) */ +#define ORPAREN OSHIFT(14) /* ) back to ( */ +#define OCH_ OSHIFT(15) /* begin choice fwd to OOR2 */ +#define OOR1 OSHIFT(16) /* | pt. 1 back to OOR1 or OCH_ */ +#define OOR2 OSHIFT(17) /* | pt. 2 fwd to OOR2 or O_CH */ +#define O_CH OSHIFT(18) /* end choice back to OOR1 */ +#define OBOW OSHIFT(19) /* begin word - */ +#define OEOW OSHIFT(20) /* end word - */ + +/* + * Structure for [] character-set representation. Character sets are + * done as bit vectors, grouped 8 to a byte vector for compactness. + * The individual set therefore has both a pointer to the byte vector + * and a mask to pick out the relevant bit of each byte. A hash code + * simplifies testing whether two sets could be identical. + * + * This will get trickier for multicharacter collating elements. As + * preliminary hooks for dealing with such things, we also carry along + * a string of multi-character elements, and decide the size of the + * vectors at run time. + */ +#ifdef __WIN__ +typedef unsigned char uch ; +#endif + +typedef struct { + uch *ptr; /* -> uch [csetsize] */ + uch mask; /* bit within array */ + uch hash; /* hash code */ + size_t smultis; + char *multis; /* -> char[smulti] ab\0cd\0ef\0\0 */ +} cset; +/* note that CHadd and CHsub are unsafe, and CHIN doesn't yield 0/1 */ +#define CHadd(cs, c) ((cs)->ptr[(uch)(c)] |= (cs)->mask, (cs)->hash += (uch) (c)) +#define CHsub(cs, c) ((cs)->ptr[(uch)(c)] &= ~(cs)->mask, (cs)->hash -= (uch)(c)) +#define CHIN(cs, c) ((cs)->ptr[(uch)(c)] & (cs)->mask) +#define MCadd(p, cs, cp) mcadd(p, cs, cp) /* regcomp() internal fns */ +#define MCsub(p, cs, cp) mcsub(p, cs, cp) +#define MCin(p, cs, cp) mcin(p, cs, cp) + +/* stuff for character categories */ +typedef unsigned char cat_t; + +/* + * main compiled-expression structure + */ +struct re_guts { + int magic; +# define MAGIC2 ((('R'^0200)<<8)|'E') + sop *strip; /* malloced area for strip */ + int csetsize; /* number of bits in a cset vector */ + int ncsets; /* number of csets in use */ + cset *sets; /* -> cset [ncsets] */ + uch *setbits; /* -> uch[csetsize][ncsets/CHAR_BIT] */ + int cflags; /* copy of regcomp() cflags argument */ + sopno nstates; /* = number of sops */ + sopno firststate; /* the initial OEND (normally 0) */ + sopno laststate; /* the final OEND */ + int iflags; /* internal flags */ +# define USEBOL 01 /* used ^ */ +# define USEEOL 02 /* used $ */ +# define BAD 04 /* something wrong */ + int nbol; /* number of ^ used */ + int neol; /* number of $ used */ + int ncategories; /* how many character categories */ + cat_t *categories; /* ->catspace[-CHAR_MIN] */ + char *must; /* match must contain this string */ + int mlen; /* length of must */ + size_t nsub; /* copy of re_nsub */ + int backrefs; /* does it use back references? */ + sopno nplus; /* how deep does it nest +s? */ + /* catspace must be last */ + cat_t catspace[1]; /* actually [NC] */ +}; + +/* misc utilities */ +#undef OUT /* May be defined in windows */ +#define OUT (CHAR_MAX+1) /* a non-character value */ +#define ISWORD(s,c) (my_isalnum(s,c) || (c) == '_') + +#endif /* __regex2_h__ */ diff --git a/externals/mysql/extlib/regex/regexec.c b/externals/mysql/extlib/regex/regexec.c new file mode 100644 index 00000000000..338c1bfa7fe --- /dev/null +++ b/externals/mysql/extlib/regex/regexec.c @@ -0,0 +1,139 @@ +/* + * the outer shell of regexec() + * + * This file includes engine.c *twice*, after muchos fiddling with the + * macros that code uses. This lets the same code operate on two different + * representations for state sets. + */ +#include <my_global.h> +#include <m_string.h> +#include <m_ctype.h> +#ifdef __WIN__ +#include <limits.h> +#endif +#include "my_regex.h" +#include "utils.h" +#include "regex2.h" + +/* for use in asserts */ +#define nope 0 + +/* macros for manipulating states, small version */ +#define states long +#define states1 long /* for later use in regexec() decision. Ensure Win64 definition is correct.*/ +#define CLEAR(v) ((v) = 0) +#define SET0(v, n) ((v) &= ~((states) 1 << (n))) +#define SET1(v, n) ((v) |= (states) 1 << (n)) +#define ISSET(v, n) ((v) & ((states) 1 << (n))) +#define ASSIGN(d, s) ((d) = (s)) +#define EQ(a, b) ((a) == (b)) +#define STATEVARS int dummy /* dummy version */ +#define STATESETUP(m, n) /* nothing */ +#define STATETEARDOWN(m) /* nothing */ +#define SETUP(v) ((v) = 0) +#define onestate long /* Changed from int by Monty */ +#define INIT(o, n) ((o) = (unsigned states)1 << (n)) +#define INC(o) ((o) <<= 1) +#define ISSTATEIN(v, o) ((v) & (o)) +/* some abbreviations; note that some of these know variable names! */ +/* do "if I'm here, I can also be there" etc without branches */ +#define FWD(dst, src, n) ((dst) |= ((unsigned states)(src)&(here)) << (n)) +#define BACK(dst, src, n) ((dst) |= ((unsigned states)(src)&(here)) >> (n)) +#define ISSETBACK(v, n) ((v) & ((unsigned states)here >> (n))) +/* function names */ +#define SNAMES /* engine.c looks after details */ + +#include "engine.c" + +/* now undo things */ +#undef states +#undef CLEAR +#undef SET0 +#undef SET1 +#undef ISSET +#undef ASSIGN +#undef EQ +#undef STATEVARS +#undef STATESETUP +#undef STATETEARDOWN +#undef SETUP +#undef onestate +#undef INIT +#undef INC +#undef ISSTATEIN +#undef FWD +#undef BACK +#undef ISSETBACK +#undef SNAMES + +/* macros for manipulating states, large version */ +#define states char * +#define CLEAR(v) memset(v, 0, m->g->nstates) +#define SET0(v, n) ((v)[n] = 0) +#define SET1(v, n) ((v)[n] = 1) +#define ISSET(v, n) ((v)[n]) +#define ASSIGN(d, s) memcpy(d, s, m->g->nstates) +#define EQ(a, b) (memcmp(a, b, m->g->nstates) == 0) +#define STATEVARS int vn; char *space +#define STATESETUP(m, nv) { (m)->space = malloc((nv)*(m)->g->nstates); \ + if ((m)->space == NULL) return(REG_ESPACE); \ + (m)->vn = 0; } +#define STATETEARDOWN(m) { free((m)->space); } +#define SETUP(v) ((v) = &m->space[m->vn++ * m->g->nstates]) +#define onestate int +#define INIT(o, n) ((o) = (n)) +#define INC(o) ((o)++) +#define ISSTATEIN(v, o) ((v)[o]) +/* some abbreviations; note that some of these know variable names! */ +/* do "if I'm here, I can also be there" etc without branches */ +#define FWD(dst, src, n) ((dst)[here+(n)] |= (src)[here]) +#define BACK(dst, src, n) ((dst)[here-(n)] |= (src)[here]) +#define ISSETBACK(v, n) ((v)[here - (n)]) +/* function names */ +#define LNAMES /* flag */ + +#include "engine.c" + +/* + - regexec - interface for matching + = extern int regexec(const regex_t *, const char *, size_t, \ + = regmatch_t [], int); + = #define REG_NOTBOL 00001 + = #define REG_NOTEOL 00002 + = #define REG_STARTEND 00004 + = #define REG_TRACE 00400 // tracing of execution + = #define REG_LARGE 01000 // force large representation + = #define REG_BACKR 02000 // force use of backref code + * + * We put this here so we can exploit knowledge of the state representation + * when choosing which matcher to call. Also, by this point the matchers + * have been prototyped. + */ +int /* 0 success, REG_NOMATCH failure */ +my_regexec(preg, str, nmatch, pmatch, eflags) +const my_regex_t *preg; +const char *str; +size_t nmatch; +my_regmatch_t pmatch[]; +int eflags; +{ + register struct re_guts *g = preg->re_g; +#ifdef REDEBUG +# define GOODFLAGS(f) (f) +#else +# define GOODFLAGS(f) ((f)&(REG_NOTBOL|REG_NOTEOL|REG_STARTEND)) +#endif + + if (preg->re_magic != MAGIC1 || g->magic != MAGIC2) + return(REG_BADPAT); + assert(!(g->iflags&BAD)); + if (g->iflags&BAD) /* backstop for no-debug case */ + return(REG_BADPAT); + eflags = GOODFLAGS(eflags); + + if ((size_t) g->nstates <= CHAR_BIT*sizeof(states1) && + !(eflags®_LARGE)) + return(smatcher(preg->charset, g, (char *)str, nmatch, pmatch, eflags)); + else + return(lmatcher(preg->charset, g, (char *)str, nmatch, pmatch, eflags)); +} diff --git a/externals/mysql/extlib/regex/regexp.c b/externals/mysql/extlib/regex/regexp.c new file mode 100644 index 00000000000..8ddf90f2943 --- /dev/null +++ b/externals/mysql/extlib/regex/regexp.c @@ -0,0 +1,1348 @@ +/* + * + * regexp.c - regular expression matching + * + * DESCRIPTION + * + * Underneath the reformatting and comment blocks which were added to + * make it consistent with the rest of the code, you will find a + * modified version of Henry Specer's regular expression library. + * Henry's functions were modified to provide the minimal regular + * expression matching, as required by P1003. Henry's code was + * copyrighted, and copy of the copyright message and restrictions + * are provided, verbatim, below: + * + * Copyright (c) 1986 by University of Toronto. + * Written by Henry Spencer. Not derived from licensed software. + * + * Permission is granted to anyone to use this software for any + * purpose on any computer system, and to redistribute it freely, + * subject to the following restrictions: + * + * 1. The author is not responsible for the consequences of use of + * this software, no matter how awful, even if they arise + * from defects in it. + * + * 2. The origin of this software must not be misrepresented, either + * by explicit claim or by omission. + * + * 3. Altered versions must be plainly marked as such, and must not + * be misrepresented as being the original software. + * + * + * This version modified by Ian Phillipps to return pointer to terminating + * NUL on substitution string. [ Temp mail address ex-igp@camcon.co.uk ] + * + * Altered by amylaar to support excompatible option and the + * operators \< and >\ . ( 7.Sep. 1991 ) + * + * regsub altered by amylaar to take an additional parameter specifying + * maximum number of bytes that can be written to the memory region + * pointed to by dest + * + * Also altered by Fredrik Hubinette to handle the + operator and + * eight-bit chars. Mars 22 1996 + * + * + * Beware that some of this code is subtly aware of the way operator + * precedence is structured in regular expressions. Serious changes in + * regular-expression syntax might require a total rethink. + * + * AUTHORS + * + * Mark H. Colburn, NAPS International (mark@jhereg.mn.org) + * Henry Spencer, University of Torronto (henry@utzoo.edu) + * + * Sponsored by The USENIX Association for public distribution. + * + */ + +/* Headers */ +#include "my_global.h" +#include <ctype.h> +#include "regexp.h" +#ifdef __WIN__ +#include <string.h> +#else +#include "memory.h" +#include "error.h" +#endif + +/* + * The "internal use only" fields in regexp.h are present to pass info from + * compile to execute that permits the execute phase to run lots faster on + * simple cases. They are: + * + * regstart char that must begin a match; '\0' if none obvious + * reganch is the match anchored (at beginning-of-line only)? + * regmust string (pointer into program) that match must include, or NULL + * regmlen length of regmust string + * + * Regstart and reganch permit very fast decisions on suitable starting points + * for a match, cutting down the work a lot. Regmust permits fast rejection + * of lines that cannot possibly match. The regmust tests are costly enough + * that regcomp() supplies a regmust only if the r.e. contains something + * potentially expensive (at present, the only such thing detected is * or + + * at the start of the r.e., which can involve a lot of backup). Regmlen is + * supplied because the test in regexec() needs it and regcomp() is computing + * it anyway. + */ + +/* + * Structure for regexp "program". This is essentially a linear encoding + * of a nondeterministic finite-state machine (aka syntax charts or + * "railroad normal form" in parsing technology). Each node is an opcode + * plus a "nxt" pointer, possibly plus an operand. "Nxt" pointers of + * all nodes except BRANCH implement concatenation; a "nxt" pointer with + * a BRANCH on both ends of it is connecting two alternatives. (Here we + * have one of the subtle syntax dependencies: an individual BRANCH (as + * opposed to a collection of them) is never concatenated with anything + * because of operator precedence.) The operand of some types of node is + * a literal string; for others, it is a node leading into a sub-FSM. In + * particular, the operand of a BRANCH node is the first node of the branch. + * (NB this is *not* a tree structure: the tail of the branch connects + * to the thing following the set of BRANCHes.) The opcodes are: + */ + +/* definition number opnd? meaning */ +#define END 0 /* no End of program. */ +#define BOL 1 /* no Match "" at beginning of line. */ +#define EOL 2 /* no Match "" at end of line. */ +#define ANY 3 /* no Match any one character. */ +#define ANYOF 4 /* str Match any character in this string. */ +#define ANYBUT 5 /* str Match any character not in this + * string. */ +#define BRANCH 6 /* node Match this alternative, or the + * nxt... */ +#define BACK 7 /* no Match "", "nxt" ptr points backward. */ +#define EXACTLY 8 /* str Match this string. */ +#define NOTHING 9 /* no Match empty string. */ +#define STAR 10 /* node Match this (simple) thing 0 or more + * times. */ +#define WORDSTART 11 /* node matching a start of a word */ +#define WORDEND 12 /* node matching an end of a word */ +#define OPEN 20 /* no Mark this point in input as start of + * #n. */ + /* OPEN+1 is number 1, etc. */ +#define CLOSE 30 /* no Analogous to OPEN. */ + +/* + * Opcode notes: + * + * BRANCH The set of branches constituting a single choice are hooked + * together with their "nxt" pointers, since precedence prevents + * anything being concatenated to any individual branch. The + * "nxt" pointer of the last BRANCH in a choice points to the + * thing following the whole choice. This is also where the + * final "nxt" pointer of each individual branch points; each + * branch starts with the operand node of a BRANCH node. + * + * BACK Normal "nxt" pointers all implicitly point forward; BACK + * exists to make loop structures possible. + * + * STAR complex '*', are implemented as circular BRANCH structures + * using BACK. Simple cases (one character per match) are + * implemented with STAR for speed and to minimize recursive + * plunges. + * + * OPEN,CLOSE ...are numbered at compile time. + */ + +/* + * A node is one char of opcode followed by two chars of "nxt" pointer. + * "Nxt" pointers are stored as two 8-bit pieces, high order first. The + * value is a positive offset from the opcode of the node containing it. + * An operand, if any, simply follows the node. (Note that much of the + * code generation knows about this implicit relationship.) + * + * Using two bytes for the "nxt" pointer is vast overkill for most things, + * but allows patterns to get big without disasters. + */ +#define OP(p) (*(p)) +#define NEXT(p) (((*((p)+1)&0377)<<8) + (*((p)+2)&0377)) +#define OPERAND(p) ((p) + 3) + +/* + * The first byte of the regexp internal "program" is actually this magic + * number; the start node begins in the second byte. + */ +#define MAGIC 0234 + +/* + * Utility definitions. + */ + +#ifdef __WIN__ +#define error(X,Y) fprintf(stderr, X, Y) +#endif +#define regerror(X) error("Regexp: %s\n",X); +#define SPECIAL 0x100 +#define LBRAC ('('|SPECIAL) +#define RBRAC (')'|SPECIAL) +#define ASTERIX ('*'|SPECIAL) +#define PLUS ('+'|SPECIAL) +#define OR_OP ('|'|SPECIAL) +#define DOLLAR ('$'|SPECIAL) +#define DOT ('.'|SPECIAL) +#define CARET ('^'|SPECIAL) +#define LSQBRAC ('['|SPECIAL) +#define RSQBRAC (']'|SPECIAL) +#define LSHBRAC ('<'|SPECIAL) +#define RSHBRAC ('>'|SPECIAL) +#define FAIL(m) { regerror(m); return(NULL); } +#define ISMULT(c) ((c) == ASTERIX || (c)==PLUS) +#define META "^$.[()|*+\\" +#ifndef CHARBITS +#define CHARBITS 0xff +#define UCHARAT(p) ((int)*(unsigned char *)(p)) +#else +#define UCHARAT(p) ((int)*(p)&CHARBITS) +#endif +#define ISWORDPART(c) ( isalnum(c) || (c) == '_' ) + +/* + * Flags to be passed up and down. + */ +#define HASWIDTH 01 /* Known never to match null string. */ +#define SIMPLE 02 /* Simple enough to be STAR operand. */ +#define SPSTART 04 /* Starts with * */ +#define WORST 0 /* Worst case. */ +#ifdef __WIN__ +#define STRCHR(A,B) strchr(A,B) +#endif + +/* + * Global work variables for regcomp(). + */ +static short *regparse; /* Input-scan pointer. */ +static int regnpar; /* () count. */ +static char regdummy; +static char *regcode; /* Code-emit pointer; ®dummy = don't. */ +static long regsize; /* Code size. */ + +/* + * Forward declarations for regcomp()'s friends. + */ +#ifndef STATIC +#define STATIC static +#endif +STATIC char *reg(); +STATIC char *regbranch(); +STATIC char *regpiece(); +STATIC char *regatom(); +STATIC char *regnode(); +STATIC char *regnext(); +STATIC void regc(); +STATIC void reginsert(); +STATIC void regtail(); +STATIC void regoptail(); + +/* + - regcomp - compile a regular expression into internal code + * + * We can't allocate space until we know how big the compiled form will be, + * but we can't compile it (and thus know how big it is) until we've got a + * place to put the code. So we cheat: we compile it twice, once with code + * generation turned off and size counting turned on, and once "for real". + * This also means that we don't allocate space until we are sure that the + * thing really will compile successfully, and we never have to move the + * code and thus invalidate pointers into it. (Note that it has to be in + * one piece because free() must be able to free it all.) + * + * Beware that the optimization-preparation code in here knows about some + * of the structure of the compiled regexp. + */ +regexp *regcomp(exp,excompat) +char *exp; +int excompat; /* \( \) operators like in unix ex */ +{ + register regexp *r; + register char *scan; + register char *longest; + register int len; + int flags; + short *exp2,*dest,c; + + if (exp == (char *)NULL) + FAIL("NULL argument"); + + exp2=(short*)malloc( (strlen(exp)+1) * (sizeof(short[8])/sizeof(char[8])) ); + for ( scan=exp,dest=exp2;( c= UCHARAT(scan++)); ) { + switch (c) { + case '(': + case ')': + *dest++ = excompat ? c : c | SPECIAL; + break; + case '.': + case '*': + case '+': + case '|': + case '$': + case '^': + case '[': + case ']': + *dest++ = c | SPECIAL; + break; + case '\\': + switch ( c = *scan++ ) { + case '(': + case ')': + *dest++ = excompat ? c | SPECIAL : c; + break; + case '<': + case '>': + *dest++ = c | SPECIAL; + break; + case '{': + case '}': + FAIL("sorry, unimplemented operator"); + case 'b': *dest++ = '\b'; break; + case 't': *dest++ = '\t'; break; + case 'r': *dest++ = '\r'; break; + default: + *dest++ = c; + } + break; + default: + *dest++ = c; + } + } + *dest=0; + /* First pass: determine size, legality. */ + regparse = exp2; + regnpar = 1; + regsize = 0L; + regcode = ®dummy; + regc(MAGIC); + if (reg(0, &flags) == (char *)NULL) + return ((regexp *)NULL); + + /* Small enough for pointer-storage convention? */ + if (regsize >= 32767L) /* Probably could be 65535L. */ + FAIL("regexp too big"); + + /* Allocate space. */ + r = (regexp *) malloc(sizeof(regexp) + (unsigned) regsize); + if (r == (regexp *) NULL) + FAIL("out of space"); + (void) bzero(r, sizeof(regexp) + (unsigned)regsize); + + /* Second pass: emit code. */ + regparse = exp2; + regnpar = 1; + regcode = r->program; + regc(MAGIC); + if (reg(0, &flags) == NULL) + return ((regexp *) NULL); + + /* Dig out information for optimizations. */ + r->regstart = '\0'; /* Worst-case defaults. */ + r->reganch = 0; + r->regmust = NULL; + r->regmlen = 0; + scan = r->program + 1; /* First BRANCH. */ + if (OP(regnext(scan)) == END) { /* Only one top-level choice. */ + scan = OPERAND(scan); + + /* Starting-point info. */ + if (OP(scan) == EXACTLY) + r->regstart = *OPERAND(scan); + else if (OP(scan) == BOL) + r->reganch++; + + /* + * If there's something expensive in the r.e., find the longest + * literal string that must appear and make it the regmust. Resolve + * ties in favor of later strings, since the regstart check works + * with the beginning of the r.e. and avoiding duplication + * strengthens checking. Not a strong reason, but sufficient in the + * absence of others. + */ + if (flags & SPSTART) { + longest = NULL; + len = 0; + for (; scan != NULL; scan = regnext(scan)) + if (OP(scan) == EXACTLY && + (int)strlen(OPERAND(scan)) >= len) { + longest = OPERAND(scan); + len = strlen(OPERAND(scan)); + } + r->regmust = longest; + r->regmlen = len; + } + } + free((char*)exp2); + return (r); +} + +/* + - reg - regular expression, i.e. main body or parenthesized thing + * + * Caller must absorb opening parenthesis. + * + * Combining parenthesis handling with the base level of regular expression + * is a trifle forced, but the need to tie the tails of the branches to what + * follows makes it hard to avoid. + */ +static char *reg(paren, flagp) +int paren; /* Parenthesized? */ +int *flagp; +{ + register char *ret; + register char *br; + register char *ender; + register int parno=0; /* make gcc happy */ + int flags; + + *flagp = HASWIDTH; /* Tentatively. */ + + /* Make an OPEN node, if parenthesized. */ + if (paren) { + if (regnpar >= NSUBEXP) + FAIL("too many ()"); + parno = regnpar; + regnpar++; + ret = regnode(OPEN + parno); + } else + ret = (char *)NULL; + + /* Pick up the branches, linking them together. */ + br = regbranch(&flags); + if (br == (char *)NULL) + return ((char *)NULL); + if (ret != (char *)NULL) + regtail(ret, br); /* OPEN -> first. */ + else + ret = br; + if (!(flags & HASWIDTH)) + *flagp &= ~HASWIDTH; + *flagp |= flags & SPSTART; + while (*regparse == OR_OP) { + regparse++; + br = regbranch(&flags); + if (br == (char *)NULL) + return ((char *)NULL); + regtail(ret, br); /* BRANCH -> BRANCH. */ + if (!(flags & HASWIDTH)) + *flagp &= ~HASWIDTH; + *flagp |= flags & SPSTART; + } + + /* Make a closing node, and hook it on the end. */ + ender = regnode((paren) ? CLOSE + parno : END); + regtail(ret, ender); + + /* Hook the tails of the branches to the closing node. */ + for (br = ret; br != (char *)NULL; br = regnext(br)) + regoptail(br, ender); + + /* Check for proper termination. */ + if (paren && *regparse++ != RBRAC) { + FAIL("unmatched ()"); + } else if (!paren && *regparse != '\0') { + if (*regparse == RBRAC) { + FAIL("unmatched ()"); + } else + FAIL("junk on end");/* "Can't happen". */ + /* NOTREACHED */ + } + return (ret); +} + +/* + - regbranch - one alternative of an | operator + * + * Implements the concatenation operator. + */ +static char *regbranch(flagp) +int *flagp; +{ + register char *ret; + register char *chain; + register char *latest; + int flags; + + *flagp = WORST; /* Tentatively. */ + + ret = regnode(BRANCH); + chain = (char *)NULL; + while (*regparse != '\0' && *regparse != OR_OP && *regparse != RBRAC) { + latest = regpiece(&flags); + if (latest == (char *)NULL) + return ((char *)NULL); + *flagp |= flags & HASWIDTH; + if (chain == (char *)NULL) /* First piece. */ + *flagp |= flags & SPSTART; + else + regtail(chain, latest); + chain = latest; + } + if (chain == (char *)NULL) /* Loop ran zero times. */ + regnode(NOTHING); + + return (ret); +} + +/* + - regpiece - something followed by possible [*] + * + * Note that the branching code sequence used for * is somewhat optimized: + * they use the same NOTHING node as both the endmarker for their branch + * list and the body of the last branch. It might seem that this node could + * be dispensed with entirely, but the endmarker role is not redundant. + */ +static char *regpiece(flagp) +int *flagp; +{ + register char *ret; + register short op; + /* register char *nxt; */ + int flags; + + ret = regatom(&flags); + if (ret == (char *)NULL) + return ((char *)NULL); + + op = *regparse; + if (!ISMULT(op)) { + *flagp = flags; + return (ret); + } + if (!(flags & HASWIDTH)) + FAIL("* or + operand could be empty"); + *flagp = (WORST | SPSTART); + + if(op == ASTERIX) + { + if (flags & SIMPLE) + { + reginsert(STAR, ret); + } + else + { + /* Emit x* as (x&|), where & means "self". */ + reginsert(BRANCH, ret); /* Either x */ + regoptail(ret, regnode(BACK)); /* and loop */ + regoptail(ret, ret); /* back */ + regtail(ret, regnode(BRANCH)); /* or */ + regtail(ret, regnode(NOTHING)); /* null. */ + } + } + else if(op == PLUS) + { + /* Emit a+ as (a&) where & means "self" /Fredrik Hubinette */ + char *tmp; + tmp=regnode(BACK); + reginsert(BRANCH, tmp); + regtail(ret, tmp); + regoptail(tmp, ret); + regtail(ret, regnode(BRANCH)); + regtail(ret, regnode(NOTHING)); + } + + regparse++; + if (ISMULT(*regparse)) + FAIL("nested * or +"); + + return (ret); +} + + +/* + - regatom - the lowest level + * + * Optimization: gobbles an entire sequence of ordinary characters so that + * it can turn them into a single node, which is smaller to store and + * faster to run. + */ +static char *regatom(flagp) +int *flagp; +{ + register char *ret; + int flags; + + *flagp = WORST; /* Tentatively. */ + + switch (*regparse++) { + case CARET: + ret = regnode(BOL); + break; + case DOLLAR: + ret = regnode(EOL); + break; + case DOT: + ret = regnode(ANY); + *flagp |= HASWIDTH | SIMPLE; + break; + case LSHBRAC: + ret = regnode(WORDSTART); + break; + case RSHBRAC: + ret = regnode(WORDEND); + break; + case LSQBRAC:{ + register int class; + register int classend; + + if (*regparse == CARET) { /* Complement of range. */ + ret = regnode(ANYBUT); + regparse++; + } else + ret = regnode(ANYOF); + if (*regparse == RSQBRAC || *regparse == '-') + regc(*regparse++); + while (*regparse != '\0' && *regparse != RSQBRAC) { + if (*regparse == '-') { + regparse++; + if (*regparse == RSQBRAC || *regparse == '\0') + regc('-'); + else { + class = (CHARBITS & *(regparse - 2)) + 1; + classend = (CHARBITS & *(regparse)); + if (class > classend + 1) + FAIL("invalid [] range"); + for (; class <= classend; class++) + regc(class); + regparse++; + } + } else + regc(*regparse++); + } + regc('\0'); + if (*regparse != RSQBRAC) + FAIL("unmatched []"); + regparse++; + *flagp |= HASWIDTH | SIMPLE; + } + break; + case LBRAC: + ret = reg(1, &flags); + if (ret == (char *)NULL) + return ((char *)NULL); + *flagp |= flags & (HASWIDTH | SPSTART); + break; + case '\0': + case OR_OP: + case RBRAC: + FAIL("internal urp"); /* Supposed to be caught earlier. */ + + case ASTERIX: + FAIL("* follows nothing\n"); + + default:{ + register int len; + register short ender; + + regparse--; + for (len=0; regparse[len] && + !(regparse[len]&SPECIAL) && regparse[len] != RSQBRAC; len++) ; + if (len <= 0) + { + FAIL("internal disaster"); + } + ender = *(regparse + len); + if (len > 1 && ISMULT(ender)) + len--; /* Back off clear of * operand. */ + *flagp |= HASWIDTH; + if (len == 1) + *flagp |= SIMPLE; + ret = regnode(EXACTLY); + while (len > 0) { + regc(*regparse++); + len--; + } + regc('\0'); + } + break; + } + + return (ret); +} + +/* + - regnode - emit a node + */ +static char *regnode(op) +char op; +{ + register char *ret; + register char *ptr; + + ret = regcode; + if (ret == ®dummy) { + regsize += 3; + return (ret); + } + ptr = ret; + *ptr++ = op; + *ptr++ = '\0'; /* Null "nxt" pointer. */ + *ptr++ = '\0'; + regcode = ptr; + + return (ret); +} + +/* + - regc - emit (if appropriate) a byte of code + */ +static void regc(b) +char b; +{ + if (regcode != ®dummy) + *regcode++ = b; + else + regsize++; +} + +/* + - reginsert - insert an operator in front of already-emitted operand + * + * Means relocating the operand. + */ +static void reginsert(op, opnd) +char op; +char *opnd; +{ + register char *src; + register char *dst; + register char *place; + + if (regcode == ®dummy) { + regsize += 3; + return; + } + src = regcode; + regcode += 3; + dst = regcode; + while (src > opnd) + *--dst = *--src; + + place = opnd; /* Op node, where operand used to be. */ + *place++ = op; + *place++ = '\0'; + *place++ = '\0'; +} + +/* + - regtail - set the next-pointer at the end of a node chain + */ +static void regtail(p, val) +char *p; +char *val; +{ + register char *scan; + register char *temp; + register int offset; + + if (p == ®dummy) + return; + + /* Find last node. */ + scan = p; + for (;;) { + temp = regnext(scan); + if (temp == (char *)NULL) + break; + scan = temp; + } + + if (OP(scan) == BACK) + offset = scan - val; + else + offset = val - scan; + *(scan + 1) = (offset >> 8) & 0377; + *(scan + 2) = offset & 0377; +} + +/* + - regoptail - regtail on operand of first argument; nop if operandless + */ +static void regoptail(p, val) +char *p; +char *val; +{ + /* "Operandless" and "op != BRANCH" are synonymous in practice. */ + if (p == (char *)NULL || p == ®dummy || OP(p) != BRANCH) + return; + regtail(OPERAND(p), val); +} + +/* + * regexec and friends + */ + +/* + * Global work variables for regexec(). + */ +static char *reginput; /* String-input pointer. */ +static char *regbol; /* Beginning of input, for ^ check. */ +static char **regstartp; /* Pointer to startp array. */ +static char **regendp; /* Ditto for endp. */ + +/* + * Forwards. + */ +STATIC int regtry(); +STATIC int regmatch(); +STATIC int regrepeat(); + +#ifdef DEBUG +int regnarrate = 0; +void regdump(); +STATIC char *regprop(); +#endif + +/* + - regexec - match a regexp against a string + */ +int regexec(prog, string) +register regexp *prog; +register char *string; +{ + register char *s; + + /* Be paranoid... */ + if (prog == (regexp *)NULL || string == (char *)NULL) { + regerror("NULL parameter"); + return (0); + } + /* Check validity of program. */ + if (UCHARAT(prog->program) != MAGIC) { + regerror("corrupted program"); + return (0); + } + /* If there is a "must appear" string, look for it. */ + if (prog->regmust != (char *)NULL) { + s = string; + while ((s = STRCHR(s, prog->regmust[0])) != (char *)NULL) { + if (strncmp(s, prog->regmust, prog->regmlen) == 0) + break; /* Found it. */ + s++; + } + if (s == (char *)NULL) /* Not present. */ + return (0); + } + /* Mark beginning of line for ^ . */ + regbol = string; + + /* Simplest case: anchored match need be tried only once. */ + if (prog->reganch) + return (regtry(prog, string)); + + /* Messy cases: unanchored match. */ + s = string; + if (prog->regstart != '\0') + /* We know what char it must start with. */ + while ((s = STRCHR(s, prog->regstart)) != (char *)NULL) { + if (regtry(prog, s)) + return (1); + s++; + } + else + /* We don't -- general case. */ + do { + if (regtry(prog, s)) + return (1); + } while (*s++ != '\0'); + + /* Failure. */ + return (0); +} + +/* + - regtry - try match at specific point + */ + +static int regtry(regexp *prog, char *string) +{ + register int i; + register char **sp; + register char **ep; + + reginput = string; + regstartp = prog->startp; + regendp = prog->endp; + + sp = prog->startp; + ep = prog->endp; + for (i = NSUBEXP; i > 0; i--) { + *sp++ = (char *)NULL; + *ep++ = (char *)NULL; + } + if (regmatch(prog->program + 1)) { + prog->startp[0] = string; + prog->endp[0] = reginput; + return (1); + } else + return (0); +} + +/* + - regmatch - main matching routine + * + * Conceptually the strategy is simple: check to see whether the current + * node matches, call self recursively to see whether the rest matches, + * and then act accordingly. In practice we make some effort to avoid + * recursion, in particular by going through "ordinary" nodes (that don't + * need to know whether the rest of the match failed) by a loop instead of + * by recursion. + */ + +static int regmatch(char *prog) +{ + register char *scan; /* Current node. */ + char *nxt; /* nxt node. */ + + scan = prog; +#ifdef DEBUG + if (scan != (char *)NULL && regnarrate) + fprintf(stderr, "%s(\n", regprop(scan)); +#endif + while (scan != (char *)NULL) { +#ifdef DEBUG + if (regnarrate) + fprintf(stderr, "%s...\n", regprop(scan)); +#endif + nxt = regnext(scan); + + switch (OP(scan)) { + case BOL: + if (reginput != regbol) + return (0); + break; + case EOL: + if (*reginput != '\0') + return (0); + break; + case ANY: + if (*reginput == '\0') + return (0); + reginput++; + break; + case WORDSTART: + if (reginput == regbol) + break; + if (*reginput == '\0' || + ISWORDPART( *(reginput-1) ) || !ISWORDPART( *reginput ) ) + return (0); + break; + case WORDEND: + if (*reginput == '\0') + break; + if ( reginput == regbol || + !ISWORDPART( *(reginput-1) ) || ISWORDPART( *reginput ) ) + return (0); + break; + case EXACTLY:{ + register int len; + register char *opnd; + + opnd = OPERAND(scan); + /* Inline the first character, for speed. */ + if (*opnd != *reginput) + return (0); + len = strlen(opnd); + if (len > 1 && strncmp(opnd, reginput, len) != 0) + return (0); + reginput += len; + } + break; + case ANYOF: + if (*reginput == '\0' || + STRCHR(OPERAND(scan), *reginput) == (char *)NULL) + return (0); + reginput++; + break; + case ANYBUT: + if (*reginput == '\0' || + STRCHR(OPERAND(scan), *reginput) != (char *)NULL) + return (0); + reginput++; + break; + case NOTHING: + break; + case BACK: + break; + case OPEN + 1: + case OPEN + 2: + case OPEN + 3: + case OPEN + 4: + case OPEN + 5: + case OPEN + 6: + case OPEN + 7: + case OPEN + 8: + case OPEN + 9:{ + register int no; + register char *save; + + no = OP(scan) - OPEN; + save = reginput; + + if (regmatch(nxt)) { + /* + * Don't set startp if some later invocation of the same + * parentheses already has. + */ + if (regstartp[no] == (char *)NULL) + regstartp[no] = save; + return (1); + } else + return (0); + } + + case CLOSE + 1: + case CLOSE + 2: + case CLOSE + 3: + case CLOSE + 4: + case CLOSE + 5: + case CLOSE + 6: + case CLOSE + 7: + case CLOSE + 8: + case CLOSE + 9:{ + register int no; + register char *save; + + no = OP(scan) - CLOSE; + save = reginput; + + if (regmatch(nxt)) { + /* + * Don't set endp if some later invocation of the same + * parentheses already has. + */ + if (regendp[no] == (char *)NULL) + regendp[no] = save; + return (1); + } else + return (0); + } + + case BRANCH:{ + register char *save; + + if (OP(nxt) != BRANCH) /* No choice. */ + nxt = OPERAND(scan); /* Avoid recursion. */ + else { + do { + save = reginput; + if (regmatch(OPERAND(scan))) + return (1); + reginput = save; + scan = regnext(scan); + } while (scan != (char *)NULL && OP(scan) == BRANCH); + return (0); + /* NOTREACHED */ + } + } + break; + case STAR:{ + register char nextch; + register int no; + register char *save; + register int minimum; + + /* + * Lookahead to avoid useless match attempts when we know + * what character comes next. + */ + nextch = '\0'; + if (OP(nxt) == EXACTLY) + nextch = *OPERAND(nxt); + minimum = (OP(scan) == STAR) ? 0 : 1; + save = reginput; + no = regrepeat(OPERAND(scan)); + while (no >= minimum) { + /* If it could work, try it. */ + if (nextch == '\0' || *reginput == nextch) + if (regmatch(nxt)) + return (1); + /* Couldn't or didn't -- back up. */ + no--; + reginput = save + no; + } + return (0); + } + + case END: + return (1); /* Success! */ + + default: + regerror("memory corruption"); + return (0); + + } + + scan = nxt; + } + + /* + * We get here only if there's trouble -- normally "case END" is the + * terminating point. + */ + regerror("corrupted pointers"); + return (0); +} + +/* + - regrepeat - repeatedly match something simple, report how many + */ + +static int regrepeat(char *p) +{ + register int count = 0; + register char *scan; + register char *opnd; + + scan = reginput; + opnd = OPERAND(p); + switch (OP(p)) { + case ANY: + count = strlen(scan); + scan += count; + break; + case EXACTLY: + while (*opnd == *scan) { + count++; + scan++; + } + break; + case ANYOF: + while (*scan != '\0' && STRCHR(opnd, *scan) != (char *)NULL) { + count++; + scan++; + } + break; + case ANYBUT: + while (*scan != '\0' && STRCHR(opnd, *scan) == (char *)NULL) { + count++; + scan++; + } + break; + default: /* Oh dear. Called inappropriately. */ + regerror("internal foulup"); + count = 0; /* Best compromise. */ + break; + } + reginput = scan; + + return (count); +} + + +/* + - regnext - dig the "nxt" pointer out of a node + */ + +static char *regnext(register char *p) +{ + register int offset; + + if (p == ®dummy) + return ((char *)NULL); + + offset = NEXT(p); + if (offset == 0) + return ((char *)NULL); + + if (OP(p) == BACK) + return (p - offset); + else + return (p + offset); +} + +#ifdef DEBUG + +STATIC char *regprop(); + +/* + - regdump - dump a regexp onto stdout in vaguely comprehensible form + */ +void regdump(regexp *r) +{ + register char *s; + register char op = EXACTLY; /* Arbitrary non-END op. */ + register char *nxt; + + s = r->program + 1; + while (op != END) { /* While that wasn't END last time... */ + op = OP(s); + printf("%2ld%s", (long)(s - r->program), regprop(s)); /* Where, what. */ + nxt = regnext(s); + if (nxt == (char *)NULL) /* nxt ptr. */ + printf("(0)"); + else + printf("(%ld)", (long)( (s - r->program) + (nxt - s))); + s += 3; + if (op == ANYOF || op == ANYBUT || op == EXACTLY) { + /* Literal string, where present. */ + while (*s != '\0') { + putchar(*s); + s++; + } + s++; + } + putchar('\n'); + } + + /* Header fields of interest. */ + if (r->regstart != '\0') + printf("start `%c' ", r->regstart); + if (r->reganch) + printf("anchored "); + if (r->regmust != (char *)NULL) + printf("must have \"%s\"", r->regmust); + printf("\n"); +} + +/* + - regprop - printable representation of opcode + */ + +static char *regprop(char *op) +{ + register char *p; + static char buf[50]; + + strcpy(buf, ":"); + + switch (OP(op)) { + case BOL: + p = "BOL"; + break; + case EOL: + p = "EOL"; + break; + case ANY: + p = "ANY"; + break; + case ANYOF: + p = "ANYOF"; + break; + case ANYBUT: + p = "ANYBUT"; + break; + case BRANCH: + p = "BRANCH"; + break; + case EXACTLY: + p = "EXACTLY"; + break; + case NOTHING: + p = "NOTHING"; + break; + case BACK: + p = "BACK"; + break; + case END: + p = "END"; + break; + case OPEN + 1: + case OPEN + 2: + case OPEN + 3: + case OPEN + 4: + case OPEN + 5: + case OPEN + 6: + case OPEN + 7: + case OPEN + 8: + case OPEN + 9: + sprintf(buf + strlen(buf), "OPEN%d", OP(op) - OPEN); + p = (char *)NULL; + break; + case CLOSE + 1: + case CLOSE + 2: + case CLOSE + 3: + case CLOSE + 4: + case CLOSE + 5: + case CLOSE + 6: + case CLOSE + 7: + case CLOSE + 8: + case CLOSE + 9: + sprintf(buf + strlen(buf), "CLOSE%d", OP(op) - CLOSE); + p = (char *)NULL; + break; + case STAR: + p = "STAR"; + break; + default: + regerror("corrupted opcode"); + p=(char *)NULL; + break; + } + if (p != (char *)NULL) + strcat(buf, p); + return (buf); +} +#endif + +/* + - regsub - perform substitutions after a regexp match + */ + +char *regsub(regexp *prog, char *source, char *dest, int n) +{ + register char *src; + register char *dst; + register char c; + register int no; + register int len; + extern char *strncpy(); + + if (prog == (regexp *)NULL || + source == (char *)NULL || dest == (char *)NULL) { + regerror("NULL parm to regsub"); + return NULL; + } + if (UCHARAT(prog->program) != MAGIC) { + regerror("damaged regexp fed to regsub"); + return NULL; + } + src = source; + dst = dest; + while ((c = *src++) != '\0') { + if (c == '&') + no = 0; + else if (c == '\\' && '0' <= *src && *src <= '9') + no = *src++ - '0'; + else + no = -1; + + if (no < 0) { /* Ordinary character. */ + if (c == '\\' && (*src == '\\' || *src == '&')) + c = *src++; + if (--n < 0) { /* amylaar */ + regerror("line too long"); + return NULL; + } + *dst++ = c; + } else if (prog->startp[no] != (char *)NULL && + prog->endp[no] != (char *)NULL) { + len = prog->endp[no] - prog->startp[no]; + if ( (n-=len) < 0 ) { /* amylaar */ + regerror("line too long"); + return NULL; + } + strncpy(dst, prog->startp[no], len); + dst += len; + if (len != 0 && *(dst - 1) == '\0') { /* strncpy hit NUL. */ + regerror("damaged match string"); + return NULL; + } + } + } + if (--n < 0) { /* amylaar */ + regerror("line too long"); + return NULL; + } + *dst = '\0'; + return dst; +} + + +#if 0 /* Use the local regerror() in ed.c */ + +void regerror(char *s) +{ + fprintf(stderr, "regexp(3): %s", s); + exit(1); +} +#endif /* 0 */ diff --git a/externals/mysql/extlib/regex/regfree.c b/externals/mysql/extlib/regex/regfree.c new file mode 100644 index 00000000000..f764fcdf84e --- /dev/null +++ b/externals/mysql/extlib/regex/regfree.c @@ -0,0 +1,38 @@ +#include <my_global.h> +#include <sys/types.h> +#include <stdio.h> +#include <stdlib.h> +#include "my_regex.h" + +#include "utils.h" +#include "regex2.h" + +/* + - regfree - free everything + = extern void regfree(regex_t *); + */ +void +my_regfree(preg) +my_regex_t *preg; +{ + register struct re_guts *g; + + if (preg->re_magic != MAGIC1) /* oops */ + return; /* nice to complain, but hard */ + + g = preg->re_g; + if (g == NULL || g->magic != MAGIC2) /* oops again */ + return; + preg->re_magic = 0; /* mark it invalid */ + g->magic = 0; /* mark it invalid */ + + if (g->strip != NULL) + free((char *)g->strip); + if (g->sets != NULL) + free((char *)g->sets); + if (g->setbits != NULL) + free((char *)g->setbits); + if (g->must != NULL) + free(g->must); + free((char *)g); +} diff --git a/externals/mysql/extlib/regex/reginit.c b/externals/mysql/extlib/regex/reginit.c new file mode 100644 index 00000000000..5980de24030 --- /dev/null +++ b/externals/mysql/extlib/regex/reginit.c @@ -0,0 +1,81 @@ +/* Init cclasses array from ctypes */ + +#include <my_global.h> +#include <m_ctype.h> +#include <m_string.h> +#include "cclass.h" + +static my_bool regex_inited=0; + +void my_regex_init(CHARSET_INFO *cs) +{ + char buff[CCLASS_LAST][256]; + int count[CCLASS_LAST]; + uint i; + + if (!regex_inited) + { + regex_inited=1; + bzero((uchar*) &count,sizeof(count)); + + for (i=1 ; i<= 255; i++) + { + if (my_isalnum(cs,i)) + buff[CCLASS_ALNUM][count[CCLASS_ALNUM]++]=(char) i; + if (my_isalpha(cs,i)) + buff[CCLASS_ALPHA][count[CCLASS_ALPHA]++]=(char) i; + if (my_iscntrl(cs,i)) + buff[CCLASS_CNTRL][count[CCLASS_CNTRL]++]=(char) i; + if (my_isdigit(cs,i)) + buff[CCLASS_DIGIT][count[CCLASS_DIGIT]++]=(char) i; + if (my_isgraph(cs,i)) + buff[CCLASS_GRAPH][count[CCLASS_GRAPH]++]=(char) i; + if (my_islower(cs,i)) + buff[CCLASS_LOWER][count[CCLASS_LOWER]++]=(char) i; + if (my_isprint(cs,i)) + buff[CCLASS_PRINT][count[CCLASS_PRINT]++]=(char) i; + if (my_ispunct(cs,i)) + buff[CCLASS_PUNCT][count[CCLASS_PUNCT]++]=(char) i; + if (my_isspace(cs,i)) + buff[CCLASS_SPACE][count[CCLASS_SPACE]++]=(char) i; + if (my_isupper(cs,i)) + buff[CCLASS_UPPER][count[CCLASS_UPPER]++]=(char) i; + if (my_isxdigit(cs,i)) + buff[CCLASS_XDIGIT][count[CCLASS_XDIGIT]++]=(char) i; + } + buff[CCLASS_BLANK][0]=' '; + buff[CCLASS_BLANK][1]='\t'; + count[CCLASS_BLANK]=2; + for (i=0; i < CCLASS_LAST ; i++) + { + char *tmp=(char*) malloc(count[i]+1); + if (!tmp) + { + /* + This is very unlikely to happen as this function is called once + at program startup + */ + fprintf(stderr, + "Fatal error: Can't allocate memory in regex_init\n"); + exit(1); + } + memcpy(tmp,buff[i],count[i]*sizeof(char)); + tmp[count[i]]=0; + cclasses[i].chars=tmp; + } + } + return; +} + +void my_regex_end() +{ + if (regex_inited) + { + int i; + for (i=0; i < CCLASS_LAST ; i++) + free((char*) cclasses[i].chars); + regex_inited=0; + } +} + + diff --git a/externals/mysql/extlib/regex/split.c b/externals/mysql/extlib/regex/split.c new file mode 100644 index 00000000000..bd2a53c01e3 --- /dev/null +++ b/externals/mysql/extlib/regex/split.c @@ -0,0 +1,316 @@ +#include <stdio.h> +#include <string.h> + +/* + - split - divide a string into fields, like awk split() + = int split(char *string, char *fields[], int nfields, char *sep); + */ +int /* number of fields, including overflow */ +split(string, fields, nfields, sep) +char *string; +char *fields[]; /* list is not NULL-terminated */ +int nfields; /* number of entries available in fields[] */ +char *sep; /* "" white, "c" single char, "ab" [ab]+ */ +{ + register char *p = string; + register char c; /* latest character */ + register char sepc = sep[0]; + register char sepc2; + register int fn; + register char **fp = fields; + register char *sepp; + register int trimtrail; + + /* white space */ + if (sepc == '\0') { + while ((c = *p++) == ' ' || c == '\t') + continue; + p--; + trimtrail = 1; + sep = (char*) " \t"; /* note, code below knows this is 2 long */ + sepc = ' '; + } else + trimtrail = 0; + sepc2 = sep[1]; /* now we can safely pick this up */ + + /* catch empties */ + if (*p == '\0') + return(0); + + /* single separator */ + if (sepc2 == '\0') { + fn = nfields; + for (;;) { + *fp++ = p; + fn--; + if (fn == 0) + break; + while ((c = *p++) != sepc) + if (c == '\0') + return(nfields - fn); + *(p-1) = '\0'; + } + /* we have overflowed the fields vector -- just count them */ + fn = nfields; + for (;;) { + while ((c = *p++) != sepc) + if (c == '\0') + return(fn); + fn++; + } + /* not reached */ + } + + /* two separators */ + if (sep[2] == '\0') { + fn = nfields; + for (;;) { + *fp++ = p; + fn--; + while ((c = *p++) != sepc && c != sepc2) + if (c == '\0') { + if (trimtrail && **(fp-1) == '\0') + fn++; + return(nfields - fn); + } + if (fn == 0) + break; + *(p-1) = '\0'; + while ((c = *p++) == sepc || c == sepc2) + continue; + p--; + } + /* we have overflowed the fields vector -- just count them */ + fn = nfields; + while (c != '\0') { + while ((c = *p++) == sepc || c == sepc2) + continue; + p--; + fn++; + while ((c = *p++) != '\0' && c != sepc && c != sepc2) + continue; + } + /* might have to trim trailing white space */ + if (trimtrail) { + p--; + while ((c = *--p) == sepc || c == sepc2) + continue; + p++; + if (*p != '\0') { + if (fn == nfields+1) + *p = '\0'; + fn--; + } + } + return(fn); + } + + /* n separators */ + fn = 0; + for (;;) { + if (fn < nfields) + *fp++ = p; + fn++; + for (;;) { + c = *p++; + if (c == '\0') + return(fn); + sepp = sep; + while ((sepc = *sepp++) != '\0' && sepc != c) + continue; + if (sepc != '\0') /* it was a separator */ + break; + } + if (fn < nfields) + *(p-1) = '\0'; + for (;;) { + c = *p++; + sepp = sep; + while ((sepc = *sepp++) != '\0' && sepc != c) + continue; + if (sepc == '\0') /* it wasn't a separator */ + break; + } + p--; + } + + /* not reached */ +} + +#ifdef TEST_SPLIT + + +/* + * test program + * pgm runs regression + * pgm sep splits stdin lines by sep + * pgm str sep splits str by sep + * pgm str sep n splits str by sep n times + */ +int +main(argc, argv) +int argc; +char *argv[]; +{ + char buf[512]; + register int n; +# define MNF 10 + char *fields[MNF]; + + if (argc > 4) + for (n = atoi(argv[3]); n > 0; n--) { + (void) strcpy(buf, argv[1]); + } + else if (argc > 3) + for (n = atoi(argv[3]); n > 0; n--) { + (void) strcpy(buf, argv[1]); + (void) split(buf, fields, MNF, argv[2]); + } + else if (argc > 2) + dosplit(argv[1], argv[2]); + else if (argc > 1) + while (fgets(buf, sizeof(buf), stdin) != NULL) { + buf[strlen(buf)-1] = '\0'; /* stomp newline */ + dosplit(buf, argv[1]); + } + else + regress(); + + exit(0); +} + +dosplit(string, seps) +char *string; +char *seps; +{ +# define NF 5 + char *fields[NF]; + register int nf; + + nf = split(string, fields, NF, seps); + print(nf, NF, fields); +} + +print(nf, nfp, fields) +int nf; +int nfp; +char *fields[]; +{ + register int fn; + register int bound; + + bound = (nf > nfp) ? nfp : nf; + printf("%d:\t", nf); + for (fn = 0; fn < bound; fn++) + printf("\"%s\"%s", fields[fn], (fn+1 < nf) ? ", " : "\n"); +} + +#define RNF 5 /* some table entries know this */ +struct { + char *str; + char *seps; + int nf; + char *fi[RNF]; +} tests[] = { + "", " ", 0, { "" }, + " ", " ", 2, { "", "" }, + "x", " ", 1, { "x" }, + "xy", " ", 1, { "xy" }, + "x y", " ", 2, { "x", "y" }, + "abc def g ", " ", 5, { "abc", "def", "", "g", "" }, + " a bcd", " ", 4, { "", "", "a", "bcd" }, + "a b c d e f", " ", 6, { "a", "b", "c", "d", "e f" }, + " a b c d ", " ", 6, { "", "a", "b", "c", "d " }, + + "", " _", 0, { "" }, + " ", " _", 2, { "", "" }, + "x", " _", 1, { "x" }, + "x y", " _", 2, { "x", "y" }, + "ab _ cd", " _", 2, { "ab", "cd" }, + " a_b c ", " _", 5, { "", "a", "b", "c", "" }, + "a b c_d e f", " _", 6, { "a", "b", "c", "d", "e f" }, + " a b c d ", " _", 6, { "", "a", "b", "c", "d " }, + + "", " _~", 0, { "" }, + " ", " _~", 2, { "", "" }, + "x", " _~", 1, { "x" }, + "x y", " _~", 2, { "x", "y" }, + "ab _~ cd", " _~", 2, { "ab", "cd" }, + " a_b c~", " _~", 5, { "", "a", "b", "c", "" }, + "a b_c d~e f", " _~", 6, { "a", "b", "c", "d", "e f" }, + "~a b c d ", " _~", 6, { "", "a", "b", "c", "d " }, + + "", " _~-", 0, { "" }, + " ", " _~-", 2, { "", "" }, + "x", " _~-", 1, { "x" }, + "x y", " _~-", 2, { "x", "y" }, + "ab _~- cd", " _~-", 2, { "ab", "cd" }, + " a_b c~", " _~-", 5, { "", "a", "b", "c", "" }, + "a b_c-d~e f", " _~-", 6, { "a", "b", "c", "d", "e f" }, + "~a-b c d ", " _~-", 6, { "", "a", "b", "c", "d " }, + + "", " ", 0, { "" }, + " ", " ", 2, { "", "" }, + "x", " ", 1, { "x" }, + "xy", " ", 1, { "xy" }, + "x y", " ", 2, { "x", "y" }, + "abc def g ", " ", 4, { "abc", "def", "g", "" }, + " a bcd", " ", 3, { "", "a", "bcd" }, + "a b c d e f", " ", 6, { "a", "b", "c", "d", "e f" }, + " a b c d ", " ", 6, { "", "a", "b", "c", "d " }, + + "", "", 0, { "" }, + " ", "", 0, { "" }, + "x", "", 1, { "x" }, + "xy", "", 1, { "xy" }, + "x y", "", 2, { "x", "y" }, + "abc def g ", "", 3, { "abc", "def", "g" }, + "\t a bcd", "", 2, { "a", "bcd" }, + " a \tb\t c ", "", 3, { "a", "b", "c" }, + "a b c d e ", "", 5, { "a", "b", "c", "d", "e" }, + "a b\tc d e f", "", 6, { "a", "b", "c", "d", "e f" }, + " a b c d e f ", "", 6, { "a", "b", "c", "d", "e f " }, + + NULL, NULL, 0, { NULL }, +}; + +regress() +{ + char buf[512]; + register int n; + char *fields[RNF+1]; + register int nf; + register int i; + register int printit; + register char *f; + + for (n = 0; tests[n].str != NULL; n++) { + (void) strcpy(buf, tests[n].str); + fields[RNF] = NULL; + nf = split(buf, fields, RNF, tests[n].seps); + printit = 0; + if (nf != tests[n].nf) { + printf("split `%s' by `%s' gave %d fields, not %d\n", + tests[n].str, tests[n].seps, nf, tests[n].nf); + printit = 1; + } else if (fields[RNF] != NULL) { + printf("split() went beyond array end\n"); + printit = 1; + } else { + for (i = 0; i < nf && i < RNF; i++) { + f = fields[i]; + if (f == NULL) + f = "(NULL)"; + if (strcmp(f, tests[n].fi[i]) != 0) { + printf("split `%s' by `%s', field %d is `%s', not `%s'\n", + tests[n].str, tests[n].seps, + i, fields[i], tests[n].fi[i]); + printit = 1; + } + } + } + if (printit) + print(nf, RNF, fields); + } +} +#endif diff --git a/externals/mysql/extlib/regex/utils.h b/externals/mysql/extlib/regex/utils.h new file mode 100644 index 00000000000..8f85b705bb5 --- /dev/null +++ b/externals/mysql/extlib/regex/utils.h @@ -0,0 +1,22 @@ +/* utility definitions */ +#ifdef _POSIX2_RE_DUP_MAX +#define DUPMAX _POSIX2_RE_DUP_MAX /* xxx is this right? */ +#else +#define DUPMAX 255 +#endif +#define RE_INFINITY (DUPMAX + 1) +#define NC (CHAR_MAX - CHAR_MIN + 1) +typedef unsigned char uch; + +/* switch off assertions (if not already off) if no REDEBUG */ +#ifndef REDEBUG +#ifndef NDEBUG +#define NDEBUG /* no assertions please */ +#endif +#endif +#include <assert.h> + +/* for old systems with bcopy() but no memmove() */ +#ifdef USEBCOPY +#define memmove(d, s, c) bcopy(s, d, c) +#endif diff --git a/externals/mysql/extlib/yassl/include/buffer.hpp b/externals/mysql/extlib/yassl/include/buffer.hpp new file mode 100644 index 00000000000..a51bca9a630 --- /dev/null +++ b/externals/mysql/extlib/yassl/include/buffer.hpp @@ -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 diff --git a/externals/mysql/extlib/yassl/include/cert_wrapper.hpp b/externals/mysql/extlib/yassl/include/cert_wrapper.hpp new file mode 100644 index 00000000000..572b9f87293 --- /dev/null +++ b/externals/mysql/extlib/yassl/include/cert_wrapper.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 diff --git a/externals/mysql/extlib/yassl/include/config.h b/externals/mysql/extlib/yassl/include/config.h new file mode 100644 index 00000000000..adea8dbe8ea --- /dev/null +++ b/externals/mysql/extlib/yassl/include/config.h @@ -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/" diff --git a/externals/mysql/extlib/yassl/include/crypto_wrapper.hpp b/externals/mysql/extlib/yassl/include/crypto_wrapper.hpp new file mode 100644 index 00000000000..07b5925265a --- /dev/null +++ b/externals/mysql/extlib/yassl/include/crypto_wrapper.hpp @@ -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 diff --git a/externals/mysql/extlib/yassl/include/factory.hpp b/externals/mysql/extlib/yassl/include/factory.hpp new file mode 100644 index 00000000000..e66e32dcdf6 --- /dev/null +++ b/externals/mysql/extlib/yassl/include/factory.hpp @@ -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 diff --git a/externals/mysql/extlib/yassl/include/handshake.hpp b/externals/mysql/extlib/yassl/include/handshake.hpp new file mode 100644 index 00000000000..549a31bf3e9 --- /dev/null +++ b/externals/mysql/extlib/yassl/include/handshake.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 diff --git a/externals/mysql/extlib/yassl/include/lock.hpp b/externals/mysql/extlib/yassl/include/lock.hpp new file mode 100644 index 00000000000..0525943e45d --- /dev/null +++ b/externals/mysql/extlib/yassl/include/lock.hpp @@ -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 diff --git a/externals/mysql/extlib/yassl/include/log.hpp b/externals/mysql/extlib/yassl/include/log.hpp new file mode 100644 index 00000000000..fb480eeefb6 --- /dev/null +++ b/externals/mysql/extlib/yassl/include/log.hpp @@ -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 diff --git a/externals/mysql/extlib/yassl/include/openssl/crypto.h b/externals/mysql/extlib/yassl/include/openssl/crypto.h new file mode 100644 index 00000000000..f53e5231027 --- /dev/null +++ b/externals/mysql/extlib/yassl/include/openssl/crypto.h @@ -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__ */ + diff --git a/externals/mysql/extlib/yassl/include/openssl/des.h b/externals/mysql/extlib/yassl/include/openssl/des.h new file mode 100644 index 00000000000..67be7eecfb9 --- /dev/null +++ b/externals/mysql/extlib/yassl/include/openssl/des.h @@ -0,0 +1 @@ +/* des.h for openssl */ diff --git a/externals/mysql/extlib/yassl/include/openssl/des_old.h b/externals/mysql/extlib/yassl/include/openssl/des_old.h new file mode 100644 index 00000000000..40e8fbc02af --- /dev/null +++ b/externals/mysql/extlib/yassl/include/openssl/des_old.h @@ -0,0 +1 @@ +/* des_old.h for openvn */ diff --git a/externals/mysql/extlib/yassl/include/openssl/engine.h b/externals/mysql/extlib/yassl/include/openssl/engine.h new file mode 100644 index 00000000000..39952fcae84 --- /dev/null +++ b/externals/mysql/extlib/yassl/include/openssl/engine.h @@ -0,0 +1,5 @@ +/* engine.h for libcurl */ + +#undef HAVE_OPENSSL_ENGINE_H + + diff --git a/externals/mysql/extlib/yassl/include/openssl/err.h b/externals/mysql/extlib/yassl/include/openssl/err.h new file mode 100644 index 00000000000..45ac1ca2469 --- /dev/null +++ b/externals/mysql/extlib/yassl/include/openssl/err.h @@ -0,0 +1,8 @@ +/* err.h for openssl */ + +#ifndef yaSSL_err_h__ +#define yaSSL_err_h__ + + + +#endif /* yaSSL_err_h__ */ diff --git a/externals/mysql/extlib/yassl/include/openssl/evp.h b/externals/mysql/extlib/yassl/include/openssl/evp.h new file mode 100644 index 00000000000..1d66b08df46 --- /dev/null +++ b/externals/mysql/extlib/yassl/include/openssl/evp.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 */ diff --git a/externals/mysql/extlib/yassl/include/openssl/generate_prefix_files.pl b/externals/mysql/extlib/yassl/include/openssl/generate_prefix_files.pl new file mode 100644 index 00000000000..da591b31332 --- /dev/null +++ b/externals/mysql/extlib/yassl/include/openssl/generate_prefix_files.pl @@ -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"); + diff --git a/externals/mysql/extlib/yassl/include/openssl/hmac.h b/externals/mysql/extlib/yassl/include/openssl/hmac.h new file mode 100644 index 00000000000..a2eae4c08c1 --- /dev/null +++ b/externals/mysql/extlib/yassl/include/openssl/hmac.h @@ -0,0 +1 @@ +/* hmac.h for openvpn */ diff --git a/externals/mysql/extlib/yassl/include/openssl/lhash.h b/externals/mysql/extlib/yassl/include/openssl/lhash.h new file mode 100644 index 00000000000..01f8535f869 --- /dev/null +++ b/externals/mysql/extlib/yassl/include/openssl/lhash.h @@ -0,0 +1,2 @@ +/* lhash.h for openSSL */ + diff --git a/externals/mysql/extlib/yassl/include/openssl/md4.h b/externals/mysql/extlib/yassl/include/openssl/md4.h new file mode 100644 index 00000000000..2e99f977fca --- /dev/null +++ b/externals/mysql/extlib/yassl/include/openssl/md4.h @@ -0,0 +1 @@ +/* md4.h for libcurl */ diff --git a/externals/mysql/extlib/yassl/include/openssl/md5.h b/externals/mysql/extlib/yassl/include/openssl/md5.h new file mode 100644 index 00000000000..dfaf9799c44 --- /dev/null +++ b/externals/mysql/extlib/yassl/include/openssl/md5.h @@ -0,0 +1,4 @@ +/* md5.h for openssl */ + +#include "ssl.h" /* in there for now */ + diff --git a/externals/mysql/extlib/yassl/include/openssl/objects.h b/externals/mysql/extlib/yassl/include/openssl/objects.h new file mode 100644 index 00000000000..99f2326e51b --- /dev/null +++ b/externals/mysql/extlib/yassl/include/openssl/objects.h @@ -0,0 +1 @@ +/* objects.h for openvpn */ diff --git a/externals/mysql/extlib/yassl/include/openssl/opensslv.h b/externals/mysql/extlib/yassl/include/openssl/opensslv.h new file mode 100644 index 00000000000..d932130684f --- /dev/null +++ b/externals/mysql/extlib/yassl/include/openssl/opensslv.h @@ -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__ */ + diff --git a/externals/mysql/extlib/yassl/include/openssl/pem.h b/externals/mysql/extlib/yassl/include/openssl/pem.h new file mode 100644 index 00000000000..b4c63d56a4d --- /dev/null +++ b/externals/mysql/extlib/yassl/include/openssl/pem.h @@ -0,0 +1 @@ +/* pem.h for libcurl */ diff --git a/externals/mysql/extlib/yassl/include/openssl/pkcs12.h b/externals/mysql/extlib/yassl/include/openssl/pkcs12.h new file mode 100644 index 00000000000..e452fc879c4 --- /dev/null +++ b/externals/mysql/extlib/yassl/include/openssl/pkcs12.h @@ -0,0 +1,5 @@ +/* pkcs12.h for libcurl */ + + +#undef HAVE_OPENSSL_PKCS12_H + diff --git a/externals/mysql/extlib/yassl/include/openssl/prefix_crypto.h b/externals/mysql/extlib/yassl/include/openssl/prefix_crypto.h new file mode 100644 index 00000000000..3fa5f32c627 --- /dev/null +++ b/externals/mysql/extlib/yassl/include/openssl/prefix_crypto.h @@ -0,0 +1 @@ +#define SSLeay_version yaSSLeay_version diff --git a/externals/mysql/extlib/yassl/include/openssl/prefix_ssl.h b/externals/mysql/extlib/yassl/include/openssl/prefix_ssl.h new file mode 100644 index 00000000000..138d9fb8821 --- /dev/null +++ b/externals/mysql/extlib/yassl/include/openssl/prefix_ssl.h @@ -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 diff --git a/externals/mysql/extlib/yassl/include/openssl/rand.h b/externals/mysql/extlib/yassl/include/openssl/rand.h new file mode 100644 index 00000000000..df9c9020346 --- /dev/null +++ b/externals/mysql/extlib/yassl/include/openssl/rand.h @@ -0,0 +1,2 @@ +/* rand.h for openSSL */ + diff --git a/externals/mysql/extlib/yassl/include/openssl/rsa.h b/externals/mysql/extlib/yassl/include/openssl/rsa.h new file mode 100644 index 00000000000..fe64e655bdc --- /dev/null +++ b/externals/mysql/extlib/yassl/include/openssl/rsa.h @@ -0,0 +1,10 @@ +/* rsa.h for openSSL */ + + +#ifndef yaSSL_rsa_h__ +#define yaSSL_rsa_h__ + +enum { RSA_F4 = 1 }; + + +#endif /* yaSSL_rsa_h__ */ diff --git a/externals/mysql/extlib/yassl/include/openssl/sha.h b/externals/mysql/extlib/yassl/include/openssl/sha.h new file mode 100644 index 00000000000..bb487c05c2e --- /dev/null +++ b/externals/mysql/extlib/yassl/include/openssl/sha.h @@ -0,0 +1 @@ +/* sha.h for openvpn */ diff --git a/externals/mysql/extlib/yassl/include/openssl/ssl.h b/externals/mysql/extlib/yassl/include/openssl/ssl.h new file mode 100644 index 00000000000..05b34a0dc45 --- /dev/null +++ b/externals/mysql/extlib/yassl/include/openssl/ssl.h @@ -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__ */ diff --git a/externals/mysql/extlib/yassl/include/openssl/x509.h b/externals/mysql/extlib/yassl/include/openssl/x509.h new file mode 100644 index 00000000000..dcd847c0337 --- /dev/null +++ b/externals/mysql/extlib/yassl/include/openssl/x509.h @@ -0,0 +1 @@ +/* x509.h for libcurl */ diff --git a/externals/mysql/extlib/yassl/include/openssl/x509v3.h b/externals/mysql/extlib/yassl/include/openssl/x509v3.h new file mode 100644 index 00000000000..adf94af8f48 --- /dev/null +++ b/externals/mysql/extlib/yassl/include/openssl/x509v3.h @@ -0,0 +1 @@ +/* x509v3.h for libcurl */ diff --git a/externals/mysql/extlib/yassl/include/socket_wrapper.hpp b/externals/mysql/extlib/yassl/include/socket_wrapper.hpp new file mode 100644 index 00000000000..308704c2af0 --- /dev/null +++ b/externals/mysql/extlib/yassl/include/socket_wrapper.hpp @@ -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 diff --git a/externals/mysql/extlib/yassl/include/timer.hpp b/externals/mysql/extlib/yassl/include/timer.hpp new file mode 100644 index 00000000000..725e73cf453 --- /dev/null +++ b/externals/mysql/extlib/yassl/include/timer.hpp @@ -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 diff --git a/externals/mysql/extlib/yassl/include/yassl.hpp b/externals/mysql/extlib/yassl/include/yassl.hpp new file mode 100644 index 00000000000..29e0a5d94ec --- /dev/null +++ b/externals/mysql/extlib/yassl/include/yassl.hpp @@ -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 diff --git a/externals/mysql/extlib/yassl/include/yassl_error.hpp b/externals/mysql/extlib/yassl/include/yassl_error.hpp new file mode 100644 index 00000000000..63fa9a761ba --- /dev/null +++ b/externals/mysql/extlib/yassl/include/yassl_error.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 diff --git a/externals/mysql/extlib/yassl/include/yassl_imp.hpp b/externals/mysql/extlib/yassl/include/yassl_imp.hpp new file mode 100644 index 00000000000..8893ba8a8ba --- /dev/null +++ b/externals/mysql/extlib/yassl/include/yassl_imp.hpp @@ -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 diff --git a/externals/mysql/extlib/yassl/include/yassl_int.hpp b/externals/mysql/extlib/yassl/include/yassl_int.hpp new file mode 100644 index 00000000000..d18dc41860c --- /dev/null +++ b/externals/mysql/extlib/yassl/include/yassl_int.hpp @@ -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 diff --git a/externals/mysql/extlib/yassl/include/yassl_types.hpp b/externals/mysql/extlib/yassl/include/yassl_types.hpp new file mode 100644 index 00000000000..1af9e90f901 --- /dev/null +++ b/externals/mysql/extlib/yassl/include/yassl_types.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 diff --git a/externals/mysql/extlib/yassl/src/buffer.cpp b/externals/mysql/extlib/yassl/src/buffer.cpp new file mode 100644 index 00000000000..0c3f23b0cb8 --- /dev/null +++ b/externals/mysql/extlib/yassl/src/buffer.cpp @@ -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 + diff --git a/externals/mysql/extlib/yassl/src/cert_wrapper.cpp b/externals/mysql/extlib/yassl/src/cert_wrapper.cpp new file mode 100644 index 00000000000..ebec0882265 --- /dev/null +++ b/externals/mysql/extlib/yassl/src/cert_wrapper.cpp @@ -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 diff --git a/externals/mysql/extlib/yassl/src/crypto_wrapper.cpp b/externals/mysql/extlib/yassl/src/crypto_wrapper.cpp new file mode 100644 index 00000000000..28d7f1b5693 --- /dev/null +++ b/externals/mysql/extlib/yassl/src/crypto_wrapper.cpp @@ -0,0 +1,1013 @@ +/* + 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 source implements the policies for the cipher + * components used by SSL. + * + * The implementation relies on a specfic library, taoCrypt. + */ + +#if !defined(USE_CRYPTOPP_LIB) + +#include "runtime.hpp" +#include "crypto_wrapper.hpp" +#include "cert_wrapper.hpp" + +#include "md5.hpp" +#include "sha.hpp" +#include "ripemd.hpp" +#include "hmac.hpp" +#include "modes.hpp" +#include "des.hpp" +#include "arc4.hpp" +#include "aes.hpp" +#include "rsa.hpp" +#include "dsa.hpp" +#include "dh.hpp" +#include "random.hpp" +#include "file.hpp" +#include "coding.hpp" + + +namespace yaSSL { + + +// MD5 Implementation +struct MD5::MD5Impl { + TaoCrypt::MD5 md5_; + MD5Impl() {} + explicit MD5Impl(const TaoCrypt::MD5& md5) : md5_(md5) {} +}; + + +MD5::MD5() : pimpl_(NEW_YS MD5Impl) {} + + +MD5::~MD5() { ysDelete(pimpl_); } + + +MD5::MD5(const MD5& that) : Digest(), pimpl_(NEW_YS + MD5Impl(that.pimpl_->md5_)) {} + + +MD5& MD5::operator=(const MD5& that) +{ + pimpl_->md5_ = that.pimpl_->md5_; + return *this; +} + + +uint MD5::get_digestSize() const +{ + return MD5_LEN; +} + + +uint MD5::get_padSize() const +{ + return PAD_MD5; +} + + +// Fill out with MD5 digest from in that is sz bytes, out must be >= digest sz +void MD5::get_digest(byte* out, const byte* in, unsigned int sz) +{ + pimpl_->md5_.Update(in, sz); + pimpl_->md5_.Final(out); +} + +// Fill out with MD5 digest from previous updates +void MD5::get_digest(byte* out) +{ + pimpl_->md5_.Final(out); +} + + +// Update the current digest +void MD5::update(const byte* in, unsigned int sz) +{ + pimpl_->md5_.Update(in, sz); +} + + +// SHA Implementation +struct SHA::SHAImpl { + TaoCrypt::SHA sha_; + SHAImpl() {} + explicit SHAImpl(const TaoCrypt::SHA& sha) : sha_(sha) {} +}; + + +SHA::SHA() : pimpl_(NEW_YS SHAImpl) {} + + +SHA::~SHA() { ysDelete(pimpl_); } + + +SHA::SHA(const SHA& that) : Digest(), pimpl_(NEW_YS SHAImpl(that.pimpl_->sha_)) {} + +SHA& SHA::operator=(const SHA& that) +{ + pimpl_->sha_ = that.pimpl_->sha_; + return *this; +} + + +uint SHA::get_digestSize() const +{ + return SHA_LEN; +} + + +uint SHA::get_padSize() const +{ + return PAD_SHA; +} + + +// Fill out with SHA digest from in that is sz bytes, out must be >= digest sz +void SHA::get_digest(byte* out, const byte* in, unsigned int sz) +{ + pimpl_->sha_.Update(in, sz); + pimpl_->sha_.Final(out); +} + + +// Fill out with SHA digest from previous updates +void SHA::get_digest(byte* out) +{ + pimpl_->sha_.Final(out); +} + + +// Update the current digest +void SHA::update(const byte* in, unsigned int sz) +{ + pimpl_->sha_.Update(in, sz); +} + + +// RMD-160 Implementation +struct RMD::RMDImpl { + TaoCrypt::RIPEMD160 rmd_; + RMDImpl() {} + explicit RMDImpl(const TaoCrypt::RIPEMD160& rmd) : rmd_(rmd) {} +}; + + +RMD::RMD() : pimpl_(NEW_YS RMDImpl) {} + + +RMD::~RMD() { ysDelete(pimpl_); } + + +RMD::RMD(const RMD& that) : Digest(), pimpl_(NEW_YS RMDImpl(that.pimpl_->rmd_)) {} + +RMD& RMD::operator=(const RMD& that) +{ + pimpl_->rmd_ = that.pimpl_->rmd_; + return *this; +} + + +uint RMD::get_digestSize() const +{ + return RMD_LEN; +} + + +uint RMD::get_padSize() const +{ + return PAD_RMD; +} + + +// Fill out with RMD digest from in that is sz bytes, out must be >= digest sz +void RMD::get_digest(byte* out, const byte* in, unsigned int sz) +{ + pimpl_->rmd_.Update(in, sz); + pimpl_->rmd_.Final(out); +} + + +// Fill out with RMD digest from previous updates +void RMD::get_digest(byte* out) +{ + pimpl_->rmd_.Final(out); +} + + +// Update the current digest +void RMD::update(const byte* in, unsigned int sz) +{ + pimpl_->rmd_.Update(in, sz); +} + + +// HMAC_MD5 Implementation +struct HMAC_MD5::HMAC_MD5Impl { + TaoCrypt::HMAC<TaoCrypt::MD5> mac_; + HMAC_MD5Impl() {} +}; + + +HMAC_MD5::HMAC_MD5(const byte* secret, unsigned int len) + : pimpl_(NEW_YS HMAC_MD5Impl) +{ + pimpl_->mac_.SetKey(secret, len); +} + + +HMAC_MD5::~HMAC_MD5() { ysDelete(pimpl_); } + + +uint HMAC_MD5::get_digestSize() const +{ + return MD5_LEN; +} + + +uint HMAC_MD5::get_padSize() const +{ + return PAD_MD5; +} + + +// Fill out with MD5 digest from in that is sz bytes, out must be >= digest sz +void HMAC_MD5::get_digest(byte* out, const byte* in, unsigned int sz) +{ + pimpl_->mac_.Update(in, sz); + pimpl_->mac_.Final(out); +} + +// Fill out with MD5 digest from previous updates +void HMAC_MD5::get_digest(byte* out) +{ + pimpl_->mac_.Final(out); +} + + +// Update the current digest +void HMAC_MD5::update(const byte* in, unsigned int sz) +{ + pimpl_->mac_.Update(in, sz); +} + + +// HMAC_SHA Implementation +struct HMAC_SHA::HMAC_SHAImpl { + TaoCrypt::HMAC<TaoCrypt::SHA> mac_; + HMAC_SHAImpl() {} +}; + + +HMAC_SHA::HMAC_SHA(const byte* secret, unsigned int len) + : pimpl_(NEW_YS HMAC_SHAImpl) +{ + pimpl_->mac_.SetKey(secret, len); +} + + +HMAC_SHA::~HMAC_SHA() { ysDelete(pimpl_); } + + +uint HMAC_SHA::get_digestSize() const +{ + return SHA_LEN; +} + + +uint HMAC_SHA::get_padSize() const +{ + return PAD_SHA; +} + + +// Fill out with SHA digest from in that is sz bytes, out must be >= digest sz +void HMAC_SHA::get_digest(byte* out, const byte* in, unsigned int sz) +{ + pimpl_->mac_.Update(in, sz); + pimpl_->mac_.Final(out); +} + +// Fill out with SHA digest from previous updates +void HMAC_SHA::get_digest(byte* out) +{ + pimpl_->mac_.Final(out); +} + + +// Update the current digest +void HMAC_SHA::update(const byte* in, unsigned int sz) +{ + pimpl_->mac_.Update(in, sz); +} + + + +// HMAC_RMD Implementation +struct HMAC_RMD::HMAC_RMDImpl { + TaoCrypt::HMAC<TaoCrypt::RIPEMD160> mac_; + HMAC_RMDImpl() {} +}; + + +HMAC_RMD::HMAC_RMD(const byte* secret, unsigned int len) + : pimpl_(NEW_YS HMAC_RMDImpl) +{ + pimpl_->mac_.SetKey(secret, len); +} + + +HMAC_RMD::~HMAC_RMD() { ysDelete(pimpl_); } + + +uint HMAC_RMD::get_digestSize() const +{ + return RMD_LEN; +} + + +uint HMAC_RMD::get_padSize() const +{ + return PAD_RMD; +} + + +// Fill out with RMD digest from in that is sz bytes, out must be >= digest sz +void HMAC_RMD::get_digest(byte* out, const byte* in, unsigned int sz) +{ + pimpl_->mac_.Update(in, sz); + pimpl_->mac_.Final(out); +} + +// Fill out with RMD digest from previous updates +void HMAC_RMD::get_digest(byte* out) +{ + pimpl_->mac_.Final(out); +} + + +// Update the current digest +void HMAC_RMD::update(const byte* in, unsigned int sz) +{ + pimpl_->mac_.Update(in, sz); +} + + +struct DES::DESImpl { + TaoCrypt::DES_CBC_Encryption encryption; + TaoCrypt::DES_CBC_Decryption decryption; +}; + + +DES::DES() : pimpl_(NEW_YS DESImpl) {} + +DES::~DES() { ysDelete(pimpl_); } + + +void DES::set_encryptKey(const byte* k, const byte* iv) +{ + pimpl_->encryption.SetKey(k, DES_KEY_SZ, iv); +} + + +void DES::set_decryptKey(const byte* k, const byte* iv) +{ + pimpl_->decryption.SetKey(k, DES_KEY_SZ, iv); +} + +// DES encrypt plain of length sz into cipher +void DES::encrypt(byte* cipher, const byte* plain, unsigned int sz) +{ + pimpl_->encryption.Process(cipher, plain, sz); +} + + +// DES decrypt cipher of length sz into plain +void DES::decrypt(byte* plain, const byte* cipher, unsigned int sz) +{ + pimpl_->decryption.Process(plain, cipher, sz); +} + + +struct DES_EDE::DES_EDEImpl { + TaoCrypt::DES_EDE3_CBC_Encryption encryption; + TaoCrypt::DES_EDE3_CBC_Decryption decryption; +}; + + +DES_EDE::DES_EDE() : pimpl_(NEW_YS DES_EDEImpl) {} + +DES_EDE::~DES_EDE() { ysDelete(pimpl_); } + + +void DES_EDE::set_encryptKey(const byte* k, const byte* iv) +{ + pimpl_->encryption.SetKey(k, DES_EDE_KEY_SZ, iv); +} + + +void DES_EDE::set_decryptKey(const byte* k, const byte* iv) +{ + pimpl_->decryption.SetKey(k, DES_EDE_KEY_SZ, iv); +} + + +// 3DES encrypt plain of length sz into cipher +void DES_EDE::encrypt(byte* cipher, const byte* plain, unsigned int sz) +{ + pimpl_->encryption.Process(cipher, plain, sz); +} + + +// 3DES decrypt cipher of length sz into plain +void DES_EDE::decrypt(byte* plain, const byte* cipher, unsigned int sz) +{ + pimpl_->decryption.Process(plain, cipher, sz); +} + + +// Implementation of alledged RC4 +struct RC4::RC4Impl { + TaoCrypt::ARC4::Encryption encryption; + TaoCrypt::ARC4::Decryption decryption; +}; + + +RC4::RC4() : pimpl_(NEW_YS RC4Impl) {} + +RC4::~RC4() { ysDelete(pimpl_); } + + +void RC4::set_encryptKey(const byte* k, const byte*) +{ + pimpl_->encryption.SetKey(k, RC4_KEY_SZ); +} + + +void RC4::set_decryptKey(const byte* k, const byte*) +{ + pimpl_->decryption.SetKey(k, RC4_KEY_SZ); +} + + +// RC4 encrypt plain of length sz into cipher +void RC4::encrypt(byte* cipher, const byte* plain, unsigned int sz) +{ + pimpl_->encryption.Process(cipher, plain, sz); +} + + +// RC4 decrypt cipher of length sz into plain +void RC4::decrypt(byte* plain, const byte* cipher, unsigned int sz) +{ + pimpl_->decryption.Process(plain, cipher, sz); +} + + + +// Implementation of AES +struct AES::AESImpl { + TaoCrypt::AES_CBC_Encryption encryption; + TaoCrypt::AES_CBC_Decryption decryption; + unsigned int keySz_; + + AESImpl(unsigned int ks) : keySz_(ks) {} +}; + + +AES::AES(unsigned int ks) : pimpl_(NEW_YS AESImpl(ks)) {} + +AES::~AES() { ysDelete(pimpl_); } + + +int AES::get_keySize() const +{ + return pimpl_->keySz_; +} + + +void AES::set_encryptKey(const byte* k, const byte* iv) +{ + pimpl_->encryption.SetKey(k, pimpl_->keySz_, iv); +} + + +void AES::set_decryptKey(const byte* k, const byte* iv) +{ + pimpl_->decryption.SetKey(k, pimpl_->keySz_, iv); +} + + +// AES encrypt plain of length sz into cipher +void AES::encrypt(byte* cipher, const byte* plain, unsigned int sz) +{ + pimpl_->encryption.Process(cipher, plain, sz); +} + + +// AES decrypt cipher of length sz into plain +void AES::decrypt(byte* plain, const byte* cipher, unsigned int sz) +{ + pimpl_->decryption.Process(plain, cipher, sz); +} + + +struct RandomPool::RandomImpl { + TaoCrypt::RandomNumberGenerator RNG_; +}; + +RandomPool::RandomPool() : pimpl_(NEW_YS RandomImpl) {} + +RandomPool::~RandomPool() { ysDelete(pimpl_); } + +int RandomPool::GetError() const +{ + return pimpl_->RNG_.GetError(); +} + +void RandomPool::Fill(opaque* dst, uint sz) const +{ + pimpl_->RNG_.GenerateBlock(dst, sz); +} + + +// Implementation of DSS Authentication +struct DSS::DSSImpl { + void SetPublic (const byte*, unsigned int); + void SetPrivate(const byte*, unsigned int); + TaoCrypt::DSA_PublicKey publicKey_; + TaoCrypt::DSA_PrivateKey privateKey_; +}; + + +// Decode and store the public key +void DSS::DSSImpl::SetPublic(const byte* key, unsigned int sz) +{ + TaoCrypt::Source source(key, sz); + publicKey_.Initialize(source); +} + + +// Decode and store the public key +void DSS::DSSImpl::SetPrivate(const byte* key, unsigned int sz) +{ + TaoCrypt::Source source(key, sz); + privateKey_.Initialize(source); + publicKey_ = TaoCrypt::DSA_PublicKey(privateKey_); + +} + + +// Set public or private key +DSS::DSS(const byte* key, unsigned int sz, bool publicKey) + : pimpl_(NEW_YS DSSImpl) +{ + if (publicKey) + pimpl_->SetPublic(key, sz); + else + pimpl_->SetPrivate(key, sz); +} + + +DSS::~DSS() +{ + ysDelete(pimpl_); +} + + +uint DSS::get_signatureLength() const +{ + return pimpl_->publicKey_.SignatureLength(); +} + + +// DSS Sign message of length sz into sig +void DSS::sign(byte* sig, const byte* sha_digest, unsigned int /* shaSz */, + const RandomPool& random) +{ + using namespace TaoCrypt; + + DSA_Signer signer(pimpl_->privateKey_); + signer.Sign(sha_digest, sig, random.pimpl_->RNG_); +} + + +// DSS Verify message of length sz against sig, is it correct? +bool DSS::verify(const byte* sha_digest, unsigned int /* shaSz */, + const byte* sig, unsigned int /* sigSz */) +{ + using namespace TaoCrypt; + + DSA_Verifier ver(pimpl_->publicKey_); + return ver.Verify(sha_digest, sig); +} + + +// Implementation of RSA key interface +struct RSA::RSAImpl { + void SetPublic (const byte*, unsigned int); + void SetPrivate(const byte*, unsigned int); + TaoCrypt::RSA_PublicKey publicKey_; + TaoCrypt::RSA_PrivateKey privateKey_; +}; + + +// Decode and store the public key +void RSA::RSAImpl::SetPublic(const byte* key, unsigned int sz) +{ + TaoCrypt::Source source(key, sz); + publicKey_.Initialize(source); +} + + +// Decode and store the private key +void RSA::RSAImpl::SetPrivate(const byte* key, unsigned int sz) +{ + TaoCrypt::Source source(key, sz); + privateKey_.Initialize(source); + publicKey_ = TaoCrypt::RSA_PublicKey(privateKey_); +} + + +// Set public or private key +RSA::RSA(const byte* key, unsigned int sz, bool publicKey) + : pimpl_(NEW_YS RSAImpl) +{ + if (publicKey) + pimpl_->SetPublic(key, sz); + else + pimpl_->SetPrivate(key, sz); +} + +RSA::~RSA() +{ + ysDelete(pimpl_); +} + + +// get cipher text length, varies on key size +unsigned int RSA::get_cipherLength() const +{ + return pimpl_->publicKey_.FixedCiphertextLength(); +} + + +// get signautre length, varies on key size +unsigned int RSA::get_signatureLength() const +{ + return get_cipherLength(); +} + + +// RSA Sign message of length sz into sig +void RSA::sign(byte* sig, const byte* message, unsigned int sz, + const RandomPool& random) +{ + TaoCrypt::RSAES_Decryptor dec(pimpl_->privateKey_); + dec.SSL_Sign(message, sz, sig, random.pimpl_->RNG_); +} + + +// RSA Verify message of length sz against sig +bool RSA::verify(const byte* message, unsigned int sz, const byte* sig, + unsigned int) +{ + TaoCrypt::RSAES_Encryptor enc(pimpl_->publicKey_); + return enc.SSL_Verify(message, sz, sig); +} + + +// RSA public encrypt plain of length sz into cipher +void RSA::encrypt(byte* cipher, const byte* plain, unsigned int sz, + const RandomPool& random) +{ + + TaoCrypt::RSAES_Encryptor enc(pimpl_->publicKey_); + enc.Encrypt(plain, sz, cipher, random.pimpl_->RNG_); +} + + +// RSA private decrypt cipher of length sz into plain +void RSA::decrypt(byte* plain, const byte* cipher, unsigned int sz, + const RandomPool& random) +{ + TaoCrypt::RSAES_Decryptor dec(pimpl_->privateKey_); + dec.Decrypt(cipher, sz, plain, random.pimpl_->RNG_); +} + + +struct Integer::IntegerImpl { + TaoCrypt::Integer int_; + + IntegerImpl() {} + explicit IntegerImpl(const TaoCrypt::Integer& i) : int_(i) {} +}; + +Integer::Integer() : pimpl_(NEW_YS IntegerImpl) {} + +Integer::~Integer() { ysDelete(pimpl_); } + + + +Integer::Integer(const Integer& other) : pimpl_(NEW_YS + IntegerImpl(other.pimpl_->int_)) +{} + + +Integer& Integer::operator=(const Integer& that) +{ + pimpl_->int_ = that.pimpl_->int_; + + return *this; +} + + +void Integer::assign(const byte* num, unsigned int sz) +{ + pimpl_->int_ = TaoCrypt::Integer(num, sz); +} + + +struct DiffieHellman::DHImpl { + TaoCrypt::DH dh_; + TaoCrypt::RandomNumberGenerator& ranPool_; + byte* publicKey_; + byte* privateKey_; + byte* agreedKey_; + + DHImpl(TaoCrypt::RandomNumberGenerator& r) : ranPool_(r), publicKey_(0), + privateKey_(0), agreedKey_(0) {} + ~DHImpl() + { + ysArrayDelete(agreedKey_); + ysArrayDelete(privateKey_); + ysArrayDelete(publicKey_); + } + + DHImpl(const DHImpl& that) : dh_(that.dh_), ranPool_(that.ranPool_), + publicKey_(0), privateKey_(0), agreedKey_(0) + { + uint length = dh_.GetByteLength(); + AllocKeys(length, length, length); + } + + void AllocKeys(unsigned int pubSz, unsigned int privSz, unsigned int agrSz) + { + publicKey_ = NEW_YS byte[pubSz]; + privateKey_ = NEW_YS byte[privSz]; + agreedKey_ = NEW_YS byte[agrSz]; + } +}; + + + +/* +// server Side DH, server's view +DiffieHellman::DiffieHellman(const char* file, const RandomPool& random) + : pimpl_(NEW_YS DHImpl(random.pimpl_->RNG_)) +{ + using namespace TaoCrypt; + Source source; + FileSource(file, source); + if (source.size() == 0) + return; // TODO add error state, and force check + HexDecoder hd(source); + + pimpl_->dh_.Initialize(source); + + uint length = pimpl_->dh_.GetByteLength(); + + pimpl_->AllocKeys(length, length, length); + pimpl_->dh_.GenerateKeyPair(pimpl_->ranPool_, pimpl_->privateKey_, + pimpl_->publicKey_); +} +*/ + + +// server Side DH, client's view +DiffieHellman::DiffieHellman(const byte* p, unsigned int pSz, const byte* g, + unsigned int gSz, const byte* pub, + unsigned int pubSz, const RandomPool& random) + : pimpl_(NEW_YS DHImpl(random.pimpl_->RNG_)) +{ + using TaoCrypt::Integer; + + pimpl_->dh_.Initialize(Integer(p, pSz).Ref(), Integer(g, gSz).Ref()); + pimpl_->publicKey_ = NEW_YS opaque[pubSz]; + memcpy(pimpl_->publicKey_, pub, pubSz); +} + + +// Server Side DH, server's view +DiffieHellman::DiffieHellman(const Integer& p, const Integer& g, + const RandomPool& random) +: pimpl_(NEW_YS DHImpl(random.pimpl_->RNG_)) +{ + using TaoCrypt::Integer; + + pimpl_->dh_.Initialize(p.pimpl_->int_, g.pimpl_->int_); + + uint length = pimpl_->dh_.GetByteLength(); + + pimpl_->AllocKeys(length, length, length); + pimpl_->dh_.GenerateKeyPair(pimpl_->ranPool_, pimpl_->privateKey_, + pimpl_->publicKey_); +} + +DiffieHellman::~DiffieHellman() { ysDelete(pimpl_); } + + +// Client side and view, use server that for p and g +DiffieHellman::DiffieHellman(const DiffieHellman& that) + : pimpl_(NEW_YS DHImpl(*that.pimpl_)) +{ + pimpl_->dh_.GenerateKeyPair(pimpl_->ranPool_, pimpl_->privateKey_, + pimpl_->publicKey_); +} + + +DiffieHellman& DiffieHellman::operator=(const DiffieHellman& that) +{ + pimpl_->dh_ = that.pimpl_->dh_; + pimpl_->dh_.GenerateKeyPair(pimpl_->ranPool_, pimpl_->privateKey_, + pimpl_->publicKey_); + return *this; +} + + +void DiffieHellman::makeAgreement(const byte* other, unsigned int otherSz) +{ + pimpl_->dh_.Agree(pimpl_->agreedKey_, pimpl_->privateKey_, other, otherSz); +} + + +uint DiffieHellman::get_agreedKeyLength() const +{ + return pimpl_->dh_.GetByteLength(); +} + + +const byte* DiffieHellman::get_agreedKey() const +{ + return pimpl_->agreedKey_; +} + + +const byte* DiffieHellman::get_publicKey() const +{ + return pimpl_->publicKey_; +} + + +void DiffieHellman::set_sizes(int& pSz, int& gSz, int& pubSz) const +{ + using TaoCrypt::Integer; + Integer p = pimpl_->dh_.GetP(); + Integer g = pimpl_->dh_.GetG(); + + pSz = p.ByteCount(); + gSz = g.ByteCount(); + pubSz = pimpl_->dh_.GetByteLength(); +} + + +void DiffieHellman::get_parms(byte* bp, byte* bg, byte* bpub) const +{ + using TaoCrypt::Integer; + Integer p = pimpl_->dh_.GetP(); + Integer g = pimpl_->dh_.GetG(); + + p.Encode(bp, p.ByteCount()); + g.Encode(bg, g.ByteCount()); + memcpy(bpub, pimpl_->publicKey_, pimpl_->dh_.GetByteLength()); +} + + +// convert PEM file to DER x509 type +x509* PemToDer(FILE* file, CertType type, EncryptedInfo* info) +{ + using namespace TaoCrypt; + + char header[80]; + char footer[80]; + + if (type == Cert) { + strncpy(header, "-----BEGIN CERTIFICATE-----", sizeof(header)); + strncpy(footer, "-----END CERTIFICATE-----", sizeof(footer)); + } else { + strncpy(header, "-----BEGIN RSA PRIVATE KEY-----", sizeof(header)); + strncpy(footer, "-----END RSA PRIVATE KEY-----", sizeof(header)); + } + + long begin = -1; + long end = 0; + bool foundEnd = false; + + char line[80]; + + while(fgets(line, sizeof(line), file)) + if (strncmp(header, line, strlen(header)) == 0) { + begin = ftell(file); + break; + } + + // remove encrypted header if there + if (fgets(line, sizeof(line), file)) { + char encHeader[] = "Proc-Type"; + if (strncmp(encHeader, line, strlen(encHeader)) == 0 && + fgets(line,sizeof(line), file)) { + + char* start = strstr(line, "DES"); + char* finish = strstr(line, ","); + if (!start) + start = strstr(line, "AES"); + + if (!info) return 0; + + if ( start && finish && (start < finish)) { + memcpy(info->name, start, finish - start); + info->name[finish - start] = 0; + memcpy(info->iv, finish + 1, sizeof(info->iv)); + + char* newline = strstr(line, "\r"); + if (!newline) newline = strstr(line, "\n"); + if (newline && (newline > finish)) { + info->ivSz = newline - (finish + 1); + info->set = true; + } + } + fgets(line,sizeof(line), file); // get blank line + begin = ftell(file); + } + + } + + while(fgets(line, sizeof(line), file)) + if (strncmp(footer, line, strlen(footer)) == 0) { + foundEnd = true; + break; + } + else + end = ftell(file); + + if (begin == -1 || !foundEnd) + return 0; + + input_buffer tmp(end - begin); + fseek(file, begin, SEEK_SET); + size_t bytes = fread(tmp.get_buffer(), end - begin, 1, file); + if (bytes != 1) + return 0; + + Source der(tmp.get_buffer(), end - begin); + Base64Decoder b64Dec(der); + + uint sz = der.size(); + mySTL::auto_ptr<x509> x(NEW_YS x509(sz)); + memcpy(x->use_buffer(), der.get_buffer(), sz); + + return x.release(); +} + + +} // namespace + + +#ifdef HAVE_EXPLICIT_TEMPLATE_INSTANTIATION +namespace yaSSL { +template void ysDelete<DiffieHellman::DHImpl>(DiffieHellman::DHImpl*); +template void ysDelete<Integer::IntegerImpl>(Integer::IntegerImpl*); +template void ysDelete<RSA::RSAImpl>(RSA::RSAImpl*); +template void ysDelete<DSS::DSSImpl>(DSS::DSSImpl*); +template void ysDelete<RandomPool::RandomImpl>(RandomPool::RandomImpl*); +template void ysDelete<AES::AESImpl>(AES::AESImpl*); +template void ysDelete<RC4::RC4Impl>(RC4::RC4Impl*); +template void ysDelete<DES_EDE::DES_EDEImpl>(DES_EDE::DES_EDEImpl*); +template void ysDelete<DES::DESImpl>(DES::DESImpl*); +template void ysDelete<HMAC_RMD::HMAC_RMDImpl>(HMAC_RMD::HMAC_RMDImpl*); +template void ysDelete<HMAC_SHA::HMAC_SHAImpl>(HMAC_SHA::HMAC_SHAImpl*); +template void ysDelete<HMAC_MD5::HMAC_MD5Impl>(HMAC_MD5::HMAC_MD5Impl*); +template void ysDelete<RMD::RMDImpl>(RMD::RMDImpl*); +template void ysDelete<SHA::SHAImpl>(SHA::SHAImpl*); +template void ysDelete<MD5::MD5Impl>(MD5::MD5Impl*); +} +#endif // HAVE_EXPLICIT_TEMPLATE_INSTANTIATION + +#endif // !USE_CRYPTOPP_LIB diff --git a/externals/mysql/extlib/yassl/src/dummy.cpp b/externals/mysql/extlib/yassl/src/dummy.cpp new file mode 100644 index 00000000000..19b7fe887cd --- /dev/null +++ b/externals/mysql/extlib/yassl/src/dummy.cpp @@ -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. +*/ diff --git a/externals/mysql/extlib/yassl/src/handshake.cpp b/externals/mysql/extlib/yassl/src/handshake.cpp new file mode 100644 index 00000000000..262b5cb3b8b --- /dev/null +++ b/externals/mysql/extlib/yassl/src/handshake.cpp @@ -0,0 +1,1117 @@ +/* + 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 source implements functions for creating and reading + * the various handshake messages. + */ + + + +#include "runtime.hpp" +#include "handshake.hpp" +#include "yassl_int.hpp" + + +namespace yaSSL { + + + +// Build a client hello message from cipher suites and compression method +void buildClientHello(SSL& ssl, ClientHello& hello) +{ + // store for pre master secret + ssl.useSecurity().use_connection().chVersion_ = hello.client_version_; + + ssl.getCrypto().get_random().Fill(hello.random_, RAN_LEN); + if (ssl.getSecurity().get_resuming()) { + hello.id_len_ = ID_LEN; + memcpy(hello.session_id_, ssl.getSecurity().get_resume().GetID(), + ID_LEN); + } + else + hello.id_len_ = 0; + hello.suite_len_ = ssl.getSecurity().get_parms().suites_size_; + memcpy(hello.cipher_suites_, ssl.getSecurity().get_parms().suites_, + hello.suite_len_); + hello.comp_len_ = 1; + + hello.set_length(sizeof(ProtocolVersion) + + RAN_LEN + + hello.id_len_ + sizeof(hello.id_len_) + + hello.suite_len_ + sizeof(hello.suite_len_) + + hello.comp_len_ + sizeof(hello.comp_len_)); +} + + +// Build a server hello message +void buildServerHello(SSL& ssl, ServerHello& hello) +{ + if (ssl.getSecurity().get_resuming()) { + memcpy(hello.random_,ssl.getSecurity().get_connection().server_random_, + RAN_LEN); + memcpy(hello.session_id_, ssl.getSecurity().get_resume().GetID(), + ID_LEN); + } + else { + ssl.getCrypto().get_random().Fill(hello.random_, RAN_LEN); + ssl.getCrypto().get_random().Fill(hello.session_id_, ID_LEN); + } + hello.id_len_ = ID_LEN; + ssl.set_sessionID(hello.session_id_); + + hello.cipher_suite_[0] = ssl.getSecurity().get_parms().suite_[0]; + hello.cipher_suite_[1] = ssl.getSecurity().get_parms().suite_[1]; + hello.compression_method_ = hello.compression_method_; + + hello.set_length(sizeof(ProtocolVersion) + RAN_LEN + ID_LEN + + sizeof(hello.id_len_) + SUITE_LEN + SIZEOF_ENUM); +} + + +// add handshake from buffer into md5 and sha hashes, use handshake header +void hashHandShake(SSL& ssl, const input_buffer& input, uint sz) +{ + const opaque* buffer = input.get_buffer() + input.get_current() - + HANDSHAKE_HEADER; + sz += HANDSHAKE_HEADER; + ssl.useHashes().use_MD5().update(buffer, sz); + ssl.useHashes().use_SHA().update(buffer, sz); +} + + +// locals +namespace { + +// Write a plaintext record to buffer +void buildOutput(output_buffer& buffer, const RecordLayerHeader& rlHdr, + const Message& msg) +{ + buffer.allocate(RECORD_HEADER + rlHdr.length_); + buffer << rlHdr << msg; +} + + +// Write a plaintext record to buffer +void buildOutput(output_buffer& buffer, const RecordLayerHeader& rlHdr, + const HandShakeHeader& hsHdr, const HandShakeBase& shake) +{ + buffer.allocate(RECORD_HEADER + rlHdr.length_); + buffer << rlHdr << hsHdr << shake; +} + + +// Build Record Layer header for Message without handshake header +void buildHeader(SSL& ssl, RecordLayerHeader& rlHeader, const Message& msg) +{ + ProtocolVersion pv = ssl.getSecurity().get_connection().version_; + rlHeader.type_ = msg.get_type(); + rlHeader.version_.major_ = pv.major_; + rlHeader.version_.minor_ = pv.minor_; + rlHeader.length_ = msg.get_length(); +} + + +// Build HandShake and RecordLayer Headers for handshake output +void buildHeaders(SSL& ssl, HandShakeHeader& hsHeader, + RecordLayerHeader& rlHeader, const HandShakeBase& shake) +{ + int sz = shake.get_length(); + + hsHeader.set_type(shake.get_type()); + hsHeader.set_length(sz); + + ProtocolVersion pv = ssl.getSecurity().get_connection().version_; + rlHeader.type_ = handshake; + rlHeader.version_.major_ = pv.major_; + rlHeader.version_.minor_ = pv.minor_; + rlHeader.length_ = sz + HANDSHAKE_HEADER; +} + + +// add handshake from buffer into md5 and sha hashes, exclude record header +void hashHandShake(SSL& ssl, const output_buffer& output, bool removeIV = false) +{ + uint sz = output.get_size() - RECORD_HEADER; + + const opaque* buffer = output.get_buffer() + RECORD_HEADER; + + if (removeIV) { // TLSv1_1 IV + uint blockSz = ssl.getCrypto().get_cipher().get_blockSize(); + sz -= blockSz; + buffer += blockSz; + } + + ssl.useHashes().use_MD5().update(buffer, sz); + ssl.useHashes().use_SHA().update(buffer, sz); +} + + +// calculate MD5 hash for finished +void buildMD5(SSL& ssl, Finished& fin, const opaque* sender) +{ + + opaque md5_result[MD5_LEN]; + opaque md5_inner[SIZEOF_SENDER + SECRET_LEN + PAD_MD5]; + opaque md5_outer[SECRET_LEN + PAD_MD5 + MD5_LEN]; + + const opaque* master_secret = + ssl.getSecurity().get_connection().master_secret_; + + // make md5 inner + memcpy(md5_inner, sender, SIZEOF_SENDER); + memcpy(&md5_inner[SIZEOF_SENDER], master_secret, SECRET_LEN); + memcpy(&md5_inner[SIZEOF_SENDER + SECRET_LEN], PAD1, PAD_MD5); + + ssl.useHashes().use_MD5().get_digest(md5_result, md5_inner, + sizeof(md5_inner)); + + // make md5 outer + memcpy(md5_outer, master_secret, SECRET_LEN); + memcpy(&md5_outer[SECRET_LEN], PAD2, PAD_MD5); + memcpy(&md5_outer[SECRET_LEN + PAD_MD5], md5_result, MD5_LEN); + + ssl.useHashes().use_MD5().get_digest(fin.set_md5(), md5_outer, + sizeof(md5_outer)); +} + + +// calculate SHA hash for finished +void buildSHA(SSL& ssl, Finished& fin, const opaque* sender) +{ + + opaque sha_result[SHA_LEN]; + opaque sha_inner[SIZEOF_SENDER + SECRET_LEN + PAD_SHA]; + opaque sha_outer[SECRET_LEN + PAD_SHA + SHA_LEN]; + + const opaque* master_secret = + ssl.getSecurity().get_connection().master_secret_; + + // make sha inner + memcpy(sha_inner, sender, SIZEOF_SENDER); + memcpy(&sha_inner[SIZEOF_SENDER], master_secret, SECRET_LEN); + memcpy(&sha_inner[SIZEOF_SENDER + SECRET_LEN], PAD1, PAD_SHA); + + ssl.useHashes().use_SHA().get_digest(sha_result, sha_inner, + sizeof(sha_inner)); + + // make sha outer + memcpy(sha_outer, master_secret, SECRET_LEN); + memcpy(&sha_outer[SECRET_LEN], PAD2, PAD_SHA); + memcpy(&sha_outer[SECRET_LEN + PAD_SHA], sha_result, SHA_LEN); + + ssl.useHashes().use_SHA().get_digest(fin.set_sha(), sha_outer, + sizeof(sha_outer)); +} + + +// decrypt input message in place, store size in case needed later +void decrypt_message(SSL& ssl, input_buffer& input, uint sz) +{ + input_buffer plain(sz); + opaque* cipher = input.get_buffer() + input.get_current(); + + ssl.useCrypto().use_cipher().decrypt(plain.get_buffer(), cipher, sz); + memcpy(cipher, plain.get_buffer(), sz); + ssl.useSecurity().use_parms().encrypt_size_ = sz; + + if (ssl.isTLSv1_1()) // IV + input.set_current(input.get_current() + + ssl.getCrypto().get_cipher().get_blockSize()); +} + + +// output operator for input_buffer +output_buffer& operator<<(output_buffer& output, const input_buffer& input) +{ + output.write(input.get_buffer(), input.get_size()); + return output; +} + + +// write headers, handshake hash, mac, pad, and encrypt +void cipherFinished(SSL& ssl, Finished& fin, output_buffer& output) +{ + uint digestSz = ssl.getCrypto().get_digest().get_digestSize(); + uint finishedSz = ssl.isTLS() ? TLS_FINISHED_SZ : FINISHED_SZ; + uint sz = RECORD_HEADER + HANDSHAKE_HEADER + finishedSz + digestSz; + uint pad = 0; + uint blockSz = ssl.getCrypto().get_cipher().get_blockSize(); + + if (ssl.getSecurity().get_parms().cipher_type_ == block) { + if (ssl.isTLSv1_1()) + sz += blockSz; // IV + sz += 1; // pad byte + pad = (sz - RECORD_HEADER) % blockSz; + pad = blockSz - pad; + sz += pad; + } + + RecordLayerHeader rlHeader; + HandShakeHeader hsHeader; + buildHeaders(ssl, hsHeader, rlHeader, fin); + rlHeader.length_ = sz - RECORD_HEADER; // record header includes mac + // and pad, hanshake doesn't + input_buffer iv; + if (ssl.isTLSv1_1() && ssl.getSecurity().get_parms().cipher_type_== block){ + iv.allocate(blockSz); + ssl.getCrypto().get_random().Fill(iv.get_buffer(), blockSz); + iv.add_size(blockSz); + } + uint ivSz = iv.get_size(); + output.allocate(sz); + output << rlHeader << iv << hsHeader << fin; + + hashHandShake(ssl, output, ssl.isTLSv1_1() ? true : false); + opaque digest[SHA_LEN]; // max size + if (ssl.isTLS()) + TLS_hmac(ssl, digest, output.get_buffer() + RECORD_HEADER + ivSz, + output.get_size() - RECORD_HEADER - ivSz, handshake); + else + hmac(ssl, digest, output.get_buffer() + RECORD_HEADER, + output.get_size() - RECORD_HEADER, handshake); + output.write(digest, digestSz); + + if (ssl.getSecurity().get_parms().cipher_type_ == block) + for (uint i = 0; i <= pad; i++) output[AUTO] = pad; // pad byte gets + // pad value too + input_buffer cipher(rlHeader.length_); + ssl.useCrypto().use_cipher().encrypt(cipher.get_buffer(), + output.get_buffer() + RECORD_HEADER, output.get_size() - RECORD_HEADER); + output.set_current(RECORD_HEADER); + output.write(cipher.get_buffer(), cipher.get_capacity()); +} + + +// build an encrypted data or alert message for output +void buildMessage(SSL& ssl, output_buffer& output, const Message& msg) +{ + uint digestSz = ssl.getCrypto().get_digest().get_digestSize(); + uint sz = RECORD_HEADER + msg.get_length() + digestSz; + uint pad = 0; + uint blockSz = ssl.getCrypto().get_cipher().get_blockSize(); + + if (ssl.getSecurity().get_parms().cipher_type_ == block) { + if (ssl.isTLSv1_1()) // IV + sz += blockSz; + sz += 1; // pad byte + pad = (sz - RECORD_HEADER) % blockSz; + pad = blockSz - pad; + sz += pad; + } + + RecordLayerHeader rlHeader; + buildHeader(ssl, rlHeader, msg); + rlHeader.length_ = sz - RECORD_HEADER; // record header includes mac + // and pad, hanshake doesn't + input_buffer iv; + if (ssl.isTLSv1_1() && ssl.getSecurity().get_parms().cipher_type_== block){ + iv.allocate(blockSz); + ssl.getCrypto().get_random().Fill(iv.get_buffer(), blockSz); + iv.add_size(blockSz); + } + + uint ivSz = iv.get_size(); + output.allocate(sz); + output << rlHeader << iv << msg; + + opaque digest[SHA_LEN]; // max size + if (ssl.isTLS()) + TLS_hmac(ssl, digest, output.get_buffer() + RECORD_HEADER + ivSz, + output.get_size() - RECORD_HEADER - ivSz, msg.get_type()); + else + hmac(ssl, digest, output.get_buffer() + RECORD_HEADER, + output.get_size() - RECORD_HEADER, msg.get_type()); + output.write(digest, digestSz); + + if (ssl.getSecurity().get_parms().cipher_type_ == block) + for (uint i = 0; i <= pad; i++) output[AUTO] = pad; // pad byte gets + // pad value too + input_buffer cipher(rlHeader.length_); + ssl.useCrypto().use_cipher().encrypt(cipher.get_buffer(), + output.get_buffer() + RECORD_HEADER, output.get_size() - RECORD_HEADER); + output.set_current(RECORD_HEADER); + output.write(cipher.get_buffer(), cipher.get_capacity()); +} + + +// build alert message +void buildAlert(SSL& ssl, output_buffer& output, const Alert& alert) +{ + if (ssl.getSecurity().get_parms().pending_ == false) // encrypted + buildMessage(ssl, output, alert); + else { + RecordLayerHeader rlHeader; + buildHeader(ssl, rlHeader, alert); + buildOutput(output, rlHeader, alert); + } +} + + +// build TLS finished message +void buildFinishedTLS(SSL& ssl, Finished& fin, const opaque* sender) +{ + opaque handshake_hash[FINISHED_SZ]; + + ssl.useHashes().use_MD5().get_digest(handshake_hash); + ssl.useHashes().use_SHA().get_digest(&handshake_hash[MD5_LEN]); + + const opaque* side; + if ( strncmp((const char*)sender, (const char*)client, SIZEOF_SENDER) == 0) + side = tls_client; + else + side = tls_server; + + PRF(fin.set_md5(), TLS_FINISHED_SZ, + ssl.getSecurity().get_connection().master_secret_, SECRET_LEN, + side, FINISHED_LABEL_SZ, + handshake_hash, FINISHED_SZ); + + fin.set_length(TLS_FINISHED_SZ); // shorter length for TLS +} + + +// compute p_hash for MD5 or SHA-1 for TLSv1 PRF +void p_hash(output_buffer& result, const output_buffer& secret, + const output_buffer& seed, MACAlgorithm hash) +{ + uint len = hash == md5 ? MD5_LEN : SHA_LEN; + uint times = result.get_capacity() / len; + uint lastLen = result.get_capacity() % len; + opaque previous[SHA_LEN]; // max size + opaque current[SHA_LEN]; // max size + mySTL::auto_ptr<Digest> hmac; + + if (lastLen) times += 1; + + if (hash == md5) + hmac.reset(NEW_YS HMAC_MD5(secret.get_buffer(), secret.get_size())); + else + hmac.reset(NEW_YS HMAC_SHA(secret.get_buffer(), secret.get_size())); + // A0 = seed + hmac->get_digest(previous, seed.get_buffer(), seed.get_size());// A1 + uint lastTime = times - 1; + + for (uint i = 0; i < times; i++) { + hmac->update(previous, len); + hmac->get_digest(current, seed.get_buffer(), seed.get_size()); + + if (lastLen && (i == lastTime)) + result.write(current, lastLen); + else { + result.write(current, len); + //memcpy(previous, current, len); + hmac->get_digest(previous, previous, len); + } + } +} + + +// calculate XOR for TLSv1 PRF +void get_xor(byte *digest, uint digLen, output_buffer& md5, + output_buffer& sha) +{ + for (uint i = 0; i < digLen; i++) + digest[i] = md5[AUTO] ^ sha[AUTO]; +} + + +// build MD5 part of certificate verify +void buildMD5_CertVerify(SSL& ssl, byte* digest) +{ + opaque md5_result[MD5_LEN]; + opaque md5_inner[SECRET_LEN + PAD_MD5]; + opaque md5_outer[SECRET_LEN + PAD_MD5 + MD5_LEN]; + + const opaque* master_secret = + ssl.getSecurity().get_connection().master_secret_; + + // make md5 inner + memcpy(md5_inner, master_secret, SECRET_LEN); + memcpy(&md5_inner[SECRET_LEN], PAD1, PAD_MD5); + + ssl.useHashes().use_MD5().get_digest(md5_result, md5_inner, + sizeof(md5_inner)); + + // make md5 outer + memcpy(md5_outer, master_secret, SECRET_LEN); + memcpy(&md5_outer[SECRET_LEN], PAD2, PAD_MD5); + memcpy(&md5_outer[SECRET_LEN + PAD_MD5], md5_result, MD5_LEN); + + ssl.useHashes().use_MD5().get_digest(digest, md5_outer, sizeof(md5_outer)); +} + + +// build SHA part of certificate verify +void buildSHA_CertVerify(SSL& ssl, byte* digest) +{ + opaque sha_result[SHA_LEN]; + opaque sha_inner[SECRET_LEN + PAD_SHA]; + opaque sha_outer[SECRET_LEN + PAD_SHA + SHA_LEN]; + + const opaque* master_secret = + ssl.getSecurity().get_connection().master_secret_; + + // make sha inner + memcpy(sha_inner, master_secret, SECRET_LEN); + memcpy(&sha_inner[SECRET_LEN], PAD1, PAD_SHA); + + ssl.useHashes().use_SHA().get_digest(sha_result, sha_inner, + sizeof(sha_inner)); + + // make sha outer + memcpy(sha_outer, master_secret, SECRET_LEN); + memcpy(&sha_outer[SECRET_LEN], PAD2, PAD_SHA); + memcpy(&sha_outer[SECRET_LEN + PAD_SHA], sha_result, SHA_LEN); + + ssl.useHashes().use_SHA().get_digest(digest, sha_outer, sizeof(sha_outer)); +} + + +} // namespace for locals + + +// some clients still send sslv2 client hello +void ProcessOldClientHello(input_buffer& input, SSL& ssl) +{ + if (input.get_remaining() < 2) { + ssl.SetError(bad_input); + return; + } + byte b0 = input[AUTO]; + byte b1 = input[AUTO]; + + uint16 sz = ((b0 & 0x7f) << 8) | b1; + + if (sz > input.get_remaining()) { + ssl.SetError(bad_input); + return; + } + + // hashHandShake manually + const opaque* buffer = input.get_buffer() + input.get_current(); + ssl.useHashes().use_MD5().update(buffer, sz); + ssl.useHashes().use_SHA().update(buffer, sz); + + b1 = input[AUTO]; // does this value mean client_hello? + + ClientHello ch; + ch.client_version_.major_ = input[AUTO]; + ch.client_version_.minor_ = input[AUTO]; + + byte len[2]; + + input.read(len, sizeof(len)); + ato16(len, ch.suite_len_); + + input.read(len, sizeof(len)); + uint16 sessionLen; + ato16(len, sessionLen); + ch.id_len_ = sessionLen; + + input.read(len, sizeof(len)); + uint16 randomLen; + ato16(len, randomLen); + if (ch.suite_len_ > MAX_SUITE_SZ || sessionLen > ID_LEN || + randomLen > RAN_LEN) { + ssl.SetError(bad_input); + return; + } + + int j = 0; + for (uint16 i = 0; i < ch.suite_len_; i += 3) { + byte first = input[AUTO]; + if (first) // sslv2 type + input.read(len, SUITE_LEN); // skip + else { + input.read(&ch.cipher_suites_[j], SUITE_LEN); + j += SUITE_LEN; + } + } + ch.suite_len_ = j; + + if (ch.id_len_) + input.read(ch.session_id_, ch.id_len_); + + if (randomLen < RAN_LEN) + memset(ch.random_, 0, RAN_LEN - randomLen); + input.read(&ch.random_[RAN_LEN - randomLen], randomLen); + + + ch.Process(input, ssl); +} + + +// Build a finished message, see 7.6.9 +void buildFinished(SSL& ssl, Finished& fin, const opaque* sender) +{ + // store current states, building requires get_digest which resets state + MD5 md5(ssl.getHashes().get_MD5()); + SHA sha(ssl.getHashes().get_SHA()); + + if (ssl.isTLS()) + buildFinishedTLS(ssl, fin, sender); + else { + buildMD5(ssl, fin, sender); + buildSHA(ssl, fin, sender); + } + + // restore + ssl.useHashes().use_MD5() = md5; + ssl.useHashes().use_SHA() = sha; +} + + +/* compute SSLv3 HMAC into digest see + * buffer is of sz size and includes HandShake Header but not a Record Header + * verify means to check peers hmac +*/ +void hmac(SSL& ssl, byte* digest, const byte* buffer, uint sz, + ContentType content, bool verify) +{ + Digest& mac = ssl.useCrypto().use_digest(); + opaque inner[SHA_LEN + PAD_MD5 + SEQ_SZ + SIZEOF_ENUM + LENGTH_SZ]; + opaque outer[SHA_LEN + PAD_MD5 + SHA_LEN]; + opaque result[SHA_LEN]; // max possible sizes + uint digestSz = mac.get_digestSize(); // actual sizes + uint padSz = mac.get_padSize(); + uint innerSz = digestSz + padSz + SEQ_SZ + SIZEOF_ENUM + LENGTH_SZ; + uint outerSz = digestSz + padSz + digestSz; + + // data + const opaque* mac_secret = ssl.get_macSecret(verify); + opaque seq[SEQ_SZ] = { 0x00, 0x00, 0x00, 0x00 }; + opaque length[LENGTH_SZ]; + c16toa(sz, length); + c32toa(ssl.get_SEQIncrement(verify), &seq[sizeof(uint32)]); + + // make inner + memcpy(inner, mac_secret, digestSz); + memcpy(&inner[digestSz], PAD1, padSz); + memcpy(&inner[digestSz + padSz], seq, SEQ_SZ); + inner[digestSz + padSz + SEQ_SZ] = content; + memcpy(&inner[digestSz + padSz + SEQ_SZ + SIZEOF_ENUM], length, LENGTH_SZ); + + mac.update(inner, innerSz); + mac.get_digest(result, buffer, sz); // append content buffer + + // make outer + memcpy(outer, mac_secret, digestSz); + memcpy(&outer[digestSz], PAD2, padSz); + memcpy(&outer[digestSz + padSz], result, digestSz); + + mac.get_digest(digest, outer, outerSz); +} + + +// TLS type HAMC +void TLS_hmac(SSL& ssl, byte* digest, const byte* buffer, uint sz, + ContentType content, bool verify) +{ + mySTL::auto_ptr<Digest> hmac; + opaque seq[SEQ_SZ] = { 0x00, 0x00, 0x00, 0x00 }; + opaque length[LENGTH_SZ]; + opaque inner[SIZEOF_ENUM + VERSION_SZ + LENGTH_SZ]; // type + version + len + + c16toa(sz, length); + c32toa(ssl.get_SEQIncrement(verify), &seq[sizeof(uint32)]); + + MACAlgorithm algo = ssl.getSecurity().get_parms().mac_algorithm_; + + if (algo == sha) + hmac.reset(NEW_YS HMAC_SHA(ssl.get_macSecret(verify), SHA_LEN)); + else if (algo == rmd) + hmac.reset(NEW_YS HMAC_RMD(ssl.get_macSecret(verify), RMD_LEN)); + else + hmac.reset(NEW_YS HMAC_MD5(ssl.get_macSecret(verify), MD5_LEN)); + + hmac->update(seq, SEQ_SZ); // seq_num + inner[0] = content; // type + inner[SIZEOF_ENUM] = ssl.getSecurity().get_connection().version_.major_; + inner[SIZEOF_ENUM + SIZEOF_ENUM] = + ssl.getSecurity().get_connection().version_.minor_; // version + memcpy(&inner[SIZEOF_ENUM + VERSION_SZ], length, LENGTH_SZ); // length + hmac->update(inner, sizeof(inner)); + hmac->get_digest(digest, buffer, sz); // content +} + + +// compute TLSv1 PRF (pseudo random function using HMAC) +void PRF(byte* digest, uint digLen, const byte* secret, uint secLen, + const byte* label, uint labLen, const byte* seed, uint seedLen) +{ + uint half = (secLen + 1) / 2; + + output_buffer md5_half(half); + output_buffer sha_half(half); + output_buffer labelSeed(labLen + seedLen); + + md5_half.write(secret, half); + sha_half.write(secret + half - secLen % 2, half); + labelSeed.write(label, labLen); + labelSeed.write(seed, seedLen); + + output_buffer md5_result(digLen); + output_buffer sha_result(digLen); + + p_hash(md5_result, md5_half, labelSeed, md5); + p_hash(sha_result, sha_half, labelSeed, sha); + + md5_result.set_current(0); + sha_result.set_current(0); + get_xor(digest, digLen, md5_result, sha_result); +} + + +// build certificate hashes +void build_certHashes(SSL& ssl, Hashes& hashes) +{ + // store current states, building requires get_digest which resets state + MD5 md5(ssl.getHashes().get_MD5()); + SHA sha(ssl.getHashes().get_SHA()); + + if (ssl.isTLS()) { + ssl.useHashes().use_MD5().get_digest(hashes.md5_); + ssl.useHashes().use_SHA().get_digest(hashes.sha_); + } + else { + buildMD5_CertVerify(ssl, hashes.md5_); + buildSHA_CertVerify(ssl, hashes.sha_); + } + + // restore + ssl.useHashes().use_MD5() = md5; + ssl.useHashes().use_SHA() = sha; +} + + + +// do process input requests, return 0 is done, 1 is call again to complete +int DoProcessReply(SSL& ssl) +{ + // wait for input if blocking + if (!ssl.useSocket().wait()) { + ssl.SetError(receive_error); + return 0; + } + uint ready = ssl.getSocket().get_ready(); + if (!ready) return 1; + + // add buffered data if its there + input_buffer* buffered = ssl.useBuffers().TakeRawInput(); + uint buffSz = buffered ? buffered->get_size() : 0; + input_buffer buffer(buffSz + ready); + if (buffSz) { + buffer.assign(buffered->get_buffer(), buffSz); + ysDelete(buffered); + buffered = 0; + } + + // add new data + uint read = ssl.useSocket().receive(buffer.get_buffer() + buffSz, ready); + if (read == static_cast<uint>(-1)) { + ssl.SetError(receive_error); + return 0; + } + buffer.add_size(read); + uint offset = 0; + const MessageFactory& mf = ssl.getFactory().getMessage(); + + // old style sslv2 client hello? + if (ssl.getSecurity().get_parms().entity_ == server_end && + ssl.getStates().getServer() == clientNull) + if (buffer.peek() != handshake) { + ProcessOldClientHello(buffer, ssl); + if (ssl.GetError()) + return 0; + } + + while(!buffer.eof()) { + // each record + RecordLayerHeader hdr; + bool needHdr = false; + + if (static_cast<uint>(RECORD_HEADER) > buffer.get_remaining()) + needHdr = true; + else { + buffer >> hdr; + ssl.verifyState(hdr); + } + + // make sure we have enough input in buffer to process this record + if (needHdr || hdr.length_ > buffer.get_remaining()) { + // put header in front for next time processing + uint extra = needHdr ? 0 : RECORD_HEADER; + uint sz = buffer.get_remaining() + extra; + ssl.useBuffers().SetRawInput(NEW_YS input_buffer(sz, + buffer.get_buffer() + buffer.get_current() - extra, sz)); + return 1; + } + + while (buffer.get_current() < hdr.length_ + RECORD_HEADER + offset) { + // each message in record, can be more than 1 if not encrypted + if (ssl.getSecurity().get_parms().pending_ == false) // cipher on + decrypt_message(ssl, buffer, hdr.length_); + + mySTL::auto_ptr<Message> msg(mf.CreateObject(hdr.type_)); + if (!msg.get()) { + ssl.SetError(factory_error); + return 0; + } + buffer >> *msg; + msg->Process(buffer, ssl); + if (ssl.GetError()) + return 0; + } + offset += hdr.length_ + RECORD_HEADER; + } + return 0; +} + + +// process input requests +void processReply(SSL& ssl) +{ + if (ssl.GetError()) return; + + if (DoProcessReply(ssl)) + // didn't complete process + if (!ssl.getSocket().IsNonBlocking()) { + // keep trying now, blocking ok + while (!ssl.GetError()) + if (DoProcessReply(ssl) == 0) break; + } + else + // user will have try again later, non blocking + ssl.SetError(YasslError(SSL_ERROR_WANT_READ)); +} + + +// send client_hello, no buffering +void sendClientHello(SSL& ssl) +{ + ssl.verifyState(serverNull); + if (ssl.GetError()) return; + + ClientHello ch(ssl.getSecurity().get_connection().version_, + ssl.getSecurity().get_connection().compression_); + RecordLayerHeader rlHeader; + HandShakeHeader hsHeader; + output_buffer out; + + buildClientHello(ssl, ch); + ssl.set_random(ch.get_random(), client_end); + buildHeaders(ssl, hsHeader, rlHeader, ch); + buildOutput(out, rlHeader, hsHeader, ch); + hashHandShake(ssl, out); + + ssl.Send(out.get_buffer(), out.get_size()); +} + + +// send client key exchange +void sendClientKeyExchange(SSL& ssl, BufferOutput buffer) +{ + ssl.verifyState(serverHelloDoneComplete); + if (ssl.GetError()) return; + + ClientKeyExchange ck(ssl); + ck.build(ssl); + ssl.makeMasterSecret(); + + RecordLayerHeader rlHeader; + HandShakeHeader hsHeader; + mySTL::auto_ptr<output_buffer> out(NEW_YS output_buffer); + buildHeaders(ssl, hsHeader, rlHeader, ck); + buildOutput(*out.get(), rlHeader, hsHeader, ck); + hashHandShake(ssl, *out.get()); + + if (buffer == buffered) + ssl.addBuffer(out.release()); + else + ssl.Send(out->get_buffer(), out->get_size()); +} + + +// send server key exchange +void sendServerKeyExchange(SSL& ssl, BufferOutput buffer) +{ + if (ssl.GetError()) return; + ServerKeyExchange sk(ssl); + sk.build(ssl); + + RecordLayerHeader rlHeader; + HandShakeHeader hsHeader; + mySTL::auto_ptr<output_buffer> out(NEW_YS output_buffer); + buildHeaders(ssl, hsHeader, rlHeader, sk); + buildOutput(*out.get(), rlHeader, hsHeader, sk); + hashHandShake(ssl, *out.get()); + + if (buffer == buffered) + ssl.addBuffer(out.release()); + else + ssl.Send(out->get_buffer(), out->get_size()); +} + + +// send change cipher +void sendChangeCipher(SSL& ssl, BufferOutput buffer) +{ + if (ssl.getSecurity().get_parms().entity_ == server_end) + if (ssl.getSecurity().get_resuming()) + ssl.verifyState(clientKeyExchangeComplete); + else + ssl.verifyState(clientFinishedComplete); + if (ssl.GetError()) return; + + ChangeCipherSpec ccs; + RecordLayerHeader rlHeader; + buildHeader(ssl, rlHeader, ccs); + mySTL::auto_ptr<output_buffer> out(NEW_YS output_buffer); + buildOutput(*out.get(), rlHeader, ccs); + + if (buffer == buffered) + ssl.addBuffer(out.release()); + else + ssl.Send(out->get_buffer(), out->get_size()); +} + + +// send finished +void sendFinished(SSL& ssl, ConnectionEnd side, BufferOutput buffer) +{ + if (ssl.GetError()) return; + + Finished fin; + buildFinished(ssl, fin, side == client_end ? client : server); + mySTL::auto_ptr<output_buffer> out(NEW_YS output_buffer); + cipherFinished(ssl, fin, *out.get()); // hashes handshake + + if (ssl.getSecurity().get_resuming()) { + if (side == server_end) + buildFinished(ssl, ssl.useHashes().use_verify(), client); // client + } + else { + if (!ssl.getSecurity().GetContext()->GetSessionCacheOff()) + GetSessions().add(ssl); // store session + if (side == client_end) + buildFinished(ssl, ssl.useHashes().use_verify(), server); // server + } + ssl.useSecurity().use_connection().CleanMaster(); + + if (buffer == buffered) + ssl.addBuffer(out.release()); + else + ssl.Send(out->get_buffer(), out->get_size()); +} + + +// send data +int sendData(SSL& ssl, const void* buffer, int sz) +{ + if (ssl.GetError() == YasslError(SSL_ERROR_WANT_READ)) + ssl.SetError(no_error); + + ssl.verfiyHandShakeComplete(); + if (ssl.GetError()) return -1; + int sent = 0; + + for (;;) { + int len = min(sz - sent, MAX_RECORD_SIZE); + output_buffer out; + input_buffer tmp; + + Data data; + + if (ssl.CompressionOn()) { + if (Compress(static_cast<const opaque*>(buffer) + sent, len, + tmp) == -1) { + ssl.SetError(compress_error); + return -1; + } + data.SetData(tmp.get_size(), tmp.get_buffer()); + } + else + data.SetData(len, static_cast<const opaque*>(buffer) + sent); + + buildMessage(ssl, out, data); + ssl.Send(out.get_buffer(), out.get_size()); + + if (ssl.GetError()) return -1; + sent += len; + if (sent == sz) break; + } + ssl.useLog().ShowData(sent, true); + return sent; +} + + +// send alert +int sendAlert(SSL& ssl, const Alert& alert) +{ + output_buffer out; + buildAlert(ssl, out, alert); + ssl.Send(out.get_buffer(), out.get_size()); + + return alert.get_length(); +} + + +// process input data +int receiveData(SSL& ssl, Data& data, bool peek) +{ + if (ssl.GetError() == YasslError(SSL_ERROR_WANT_READ)) + ssl.SetError(no_error); + + ssl.verfiyHandShakeComplete(); + if (ssl.GetError()) return -1; + + if (!ssl.HasData()) + processReply(ssl); + + if (peek) + ssl.PeekData(data); + else + ssl.fillData(data); + + ssl.useLog().ShowData(data.get_length()); + if (ssl.GetError()) return -1; + + if (data.get_length() == 0 && ssl.getSocket().WouldBlock()) { + ssl.SetError(YasslError(SSL_ERROR_WANT_READ)); + return SSL_WOULD_BLOCK; + } + return data.get_length(); +} + + +// send server hello +void sendServerHello(SSL& ssl, BufferOutput buffer) +{ + if (ssl.getSecurity().get_resuming()) + ssl.verifyState(clientKeyExchangeComplete); + else + ssl.verifyState(clientHelloComplete); + if (ssl.GetError()) return; + + ServerHello sh(ssl.getSecurity().get_connection().version_, + ssl.getSecurity().get_connection().compression_); + RecordLayerHeader rlHeader; + HandShakeHeader hsHeader; + mySTL::auto_ptr<output_buffer> out(NEW_YS output_buffer); + + buildServerHello(ssl, sh); + ssl.set_random(sh.get_random(), server_end); + buildHeaders(ssl, hsHeader, rlHeader, sh); + buildOutput(*out.get(), rlHeader, hsHeader, sh); + hashHandShake(ssl, *out.get()); + + if (buffer == buffered) + ssl.addBuffer(out.release()); + else + ssl.Send(out->get_buffer(), out->get_size()); +} + + +// send server hello done +void sendServerHelloDone(SSL& ssl, BufferOutput buffer) +{ + if (ssl.GetError()) return; + + ServerHelloDone shd; + RecordLayerHeader rlHeader; + HandShakeHeader hsHeader; + mySTL::auto_ptr<output_buffer> out(NEW_YS output_buffer); + + buildHeaders(ssl, hsHeader, rlHeader, shd); + buildOutput(*out.get(), rlHeader, hsHeader, shd); + hashHandShake(ssl, *out.get()); + + if (buffer == buffered) + ssl.addBuffer(out.release()); + else + ssl.Send(out->get_buffer(), out->get_size()); +} + + +// send certificate +void sendCertificate(SSL& ssl, BufferOutput buffer) +{ + if (ssl.GetError()) return; + + Certificate cert(ssl.getCrypto().get_certManager().get_cert()); + RecordLayerHeader rlHeader; + HandShakeHeader hsHeader; + mySTL::auto_ptr<output_buffer> out(NEW_YS output_buffer); + + buildHeaders(ssl, hsHeader, rlHeader, cert); + buildOutput(*out.get(), rlHeader, hsHeader, cert); + hashHandShake(ssl, *out.get()); + + if (buffer == buffered) + ssl.addBuffer(out.release()); + else + ssl.Send(out->get_buffer(), out->get_size()); +} + + +// send certificate request +void sendCertificateRequest(SSL& ssl, BufferOutput buffer) +{ + if (ssl.GetError()) return; + + CertificateRequest request; + request.Build(); + RecordLayerHeader rlHeader; + HandShakeHeader hsHeader; + mySTL::auto_ptr<output_buffer> out(NEW_YS output_buffer); + + buildHeaders(ssl, hsHeader, rlHeader, request); + buildOutput(*out.get(), rlHeader, hsHeader, request); + hashHandShake(ssl, *out.get()); + + if (buffer == buffered) + ssl.addBuffer(out.release()); + else + ssl.Send(out->get_buffer(), out->get_size()); +} + + +// send certificate verify +void sendCertificateVerify(SSL& ssl, BufferOutput buffer) +{ + if (ssl.GetError()) return; + + CertificateVerify verify; + verify.Build(ssl); + RecordLayerHeader rlHeader; + HandShakeHeader hsHeader; + mySTL::auto_ptr<output_buffer> out(NEW_YS output_buffer); + + buildHeaders(ssl, hsHeader, rlHeader, verify); + buildOutput(*out.get(), rlHeader, hsHeader, verify); + hashHandShake(ssl, *out.get()); + + if (buffer == buffered) + ssl.addBuffer(out.release()); + else + ssl.Send(out->get_buffer(), out->get_size()); +} + + +} // namespace diff --git a/externals/mysql/extlib/yassl/src/lock.cpp b/externals/mysql/extlib/yassl/src/lock.cpp new file mode 100644 index 00000000000..6d8e9c17477 --- /dev/null +++ b/externals/mysql/extlib/yassl/src/lock.cpp @@ -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 + diff --git a/externals/mysql/extlib/yassl/src/log.cpp b/externals/mysql/extlib/yassl/src/log.cpp new file mode 100644 index 00000000000..35db4136a54 --- /dev/null +++ b/externals/mysql/extlib/yassl/src/log.cpp @@ -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 diff --git a/externals/mysql/extlib/yassl/src/socket_wrapper.cpp b/externals/mysql/extlib/yassl/src/socket_wrapper.cpp new file mode 100644 index 00000000000..eee5d47377f --- /dev/null +++ b/externals/mysql/extlib/yassl/src/socket_wrapper.cpp @@ -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 arnetheduck@gmail.com + 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 diff --git a/externals/mysql/extlib/yassl/src/ssl.cpp b/externals/mysql/extlib/yassl/src/ssl.cpp new file mode 100644 index 00000000000..29aa034f885 --- /dev/null +++ b/externals/mysql/extlib/yassl/src/ssl.cpp @@ -0,0 +1,1672 @@ +/* + 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. +*/ + +/* SSL source implements all openssl compatibility API functions + * + * TODO: notes are mostly api additions to allow compilation with mysql + * they don't affect normal modes but should be provided for completeness + + * stunnel functions at end of file + */ + + + + +/* see man pages for function descriptions */ + +#include "runtime.hpp" +#include "openssl/ssl.h" +#include "handshake.hpp" +#include "yassl_int.hpp" +#include "md5.hpp" // for TaoCrypt MD5 size assert +#include "md4.hpp" // for TaoCrypt MD4 size assert +#include "file.hpp" // for TaoCrypt Source +#include "coding.hpp" // HexDecoder +#include "helpers.hpp" // for placement new hack +#include <stdio.h> + +#ifdef _WIN32 + #include <windows.h> // FindFirstFile etc.. +#else + #include <sys/types.h> // file helper + #include <sys/stat.h> // stat + #include <dirent.h> // opendir +#endif + + +namespace yaSSL { + + + +int read_file(SSL_CTX* ctx, const char* file, int format, CertType type) +{ + if (format != SSL_FILETYPE_ASN1 && format != SSL_FILETYPE_PEM) + return SSL_BAD_FILETYPE; + + if (file == NULL || !file[0]) + return SSL_BAD_FILE; + + FILE* input = fopen(file, "rb"); + if (!input) + return SSL_BAD_FILE; + + if (type == CA) { + // may have a bunch of CAs + x509* ptr; + while ( (ptr = PemToDer(input, Cert)) ) + ctx->AddCA(ptr); + + if (!feof(input)) { + fclose(input); + return SSL_BAD_FILE; + } + } + else { + x509*& x = (type == Cert) ? ctx->certificate_ : ctx->privateKey_; + + if (format == SSL_FILETYPE_ASN1) { + fseek(input, 0, SEEK_END); + long sz = ftell(input); + rewind(input); + x = NEW_YS x509(sz); // takes ownership + size_t bytes = fread(x->use_buffer(), sz, 1, input); + if (bytes != 1) { + fclose(input); + return SSL_BAD_FILE; + } + } + else { + EncryptedInfo info; + x = PemToDer(input, type, &info); + if (!x) { + fclose(input); + return SSL_BAD_FILE; + } + if (info.set) { + // decrypt + char password[80]; + pem_password_cb cb = ctx->GetPasswordCb(); + if (!cb) { + fclose(input); + return SSL_BAD_FILE; + } + int passwordSz = cb(password, sizeof(password), 0, + ctx->GetUserData()); + byte key[AES_256_KEY_SZ]; // max sizes + byte iv[AES_IV_SZ]; + + // use file's salt for key derivation, but not real iv + TaoCrypt::Source source(info.iv, info.ivSz); + TaoCrypt::HexDecoder dec(source); + memcpy(info.iv, source.get_buffer(), min((uint)sizeof(info.iv), + source.size())); + EVP_BytesToKey(info.name, "MD5", info.iv, (byte*)password, + passwordSz, 1, key, iv); + + mySTL::auto_ptr<BulkCipher> cipher; + if (strncmp(info.name, "DES-CBC", 7) == 0) + cipher.reset(NEW_YS DES); + else if (strncmp(info.name, "DES-EDE3-CBC", 13) == 0) + cipher.reset(NEW_YS DES_EDE); + else if (strncmp(info.name, "AES-128-CBC", 13) == 0) + cipher.reset(NEW_YS AES(AES_128_KEY_SZ)); + else if (strncmp(info.name, "AES-192-CBC", 13) == 0) + cipher.reset(NEW_YS AES(AES_192_KEY_SZ)); + else if (strncmp(info.name, "AES-256-CBC", 13) == 0) + cipher.reset(NEW_YS AES(AES_256_KEY_SZ)); + else { + fclose(input); + return SSL_BAD_FILE; + } + cipher->set_decryptKey(key, info.iv); + mySTL::auto_ptr<x509> newx(NEW_YS x509(x->get_length())); + cipher->decrypt(newx->use_buffer(), x->get_buffer(), + x->get_length()); + ysDelete(x); + x = newx.release(); + } + } + } + fclose(input); + return SSL_SUCCESS; +} + + +extern "C" { + + +SSL_METHOD* SSLv3_method() +{ + return SSLv3_client_method(); +} + + +SSL_METHOD* SSLv3_server_method() +{ + return NEW_YS SSL_METHOD(server_end, ProtocolVersion(3,0)); +} + + +SSL_METHOD* SSLv3_client_method() +{ + return NEW_YS SSL_METHOD(client_end, ProtocolVersion(3,0)); +} + + +SSL_METHOD* TLSv1_server_method() +{ + return NEW_YS SSL_METHOD(server_end, ProtocolVersion(3,1)); +} + + +SSL_METHOD* TLSv1_client_method() +{ + return NEW_YS SSL_METHOD(client_end, ProtocolVersion(3,1)); +} + + +SSL_METHOD* TLSv1_1_server_method() +{ + return NEW_YS SSL_METHOD(server_end, ProtocolVersion(3,2)); +} + + +SSL_METHOD* TLSv1_1_client_method() +{ + return NEW_YS SSL_METHOD(client_end, ProtocolVersion(3,2)); +} + + +SSL_METHOD* SSLv23_server_method() +{ + // compatibility only, no version 2 support, but does SSL 3 and TLS 1 + return NEW_YS SSL_METHOD(server_end, ProtocolVersion(3,2), true); +} + + +SSL_METHOD* SSLv23_client_method() +{ + // compatibility only, no version 2 support, but does SSL 3 and TLS 1 + // though it sends TLS1 hello not SSLv2 so SSLv3 only servers will decline + // TODO: maybe add support to send SSLv2 hello ??? + return NEW_YS SSL_METHOD(client_end, ProtocolVersion(3,2), true); +} + + +SSL_CTX* SSL_CTX_new(SSL_METHOD* method) +{ + return NEW_YS SSL_CTX(method); +} + + +void SSL_CTX_free(SSL_CTX* ctx) +{ + ysDelete(ctx); +} + + +SSL* SSL_new(SSL_CTX* ctx) +{ + return NEW_YS SSL(ctx); +} + + +void SSL_free(SSL* ssl) +{ + ysDelete(ssl); +} + + +int SSL_set_fd(SSL* ssl, YASSL_SOCKET_T fd) +{ + ssl->useSocket().set_fd(fd); + return SSL_SUCCESS; +} + + +YASSL_SOCKET_T SSL_get_fd(const SSL* ssl) +{ + return ssl->getSocket().get_fd(); +} + + +// if you get an error from connect see note at top of README +int SSL_connect(SSL* ssl) +{ + if (ssl->GetError() == YasslError(SSL_ERROR_WANT_READ)) + ssl->SetError(no_error); + + ClientState neededState; + + switch (ssl->getStates().GetConnect()) { + + case CONNECT_BEGIN : + sendClientHello(*ssl); + if (!ssl->GetError()) + ssl->useStates().UseConnect() = CLIENT_HELLO_SENT; + + case CLIENT_HELLO_SENT : + neededState = ssl->getSecurity().get_resuming() ? + serverFinishedComplete : serverHelloDoneComplete; + while (ssl->getStates().getClient() < neededState) { + if (ssl->GetError()) break; + processReply(*ssl); + } + if (!ssl->GetError()) + ssl->useStates().UseConnect() = FIRST_REPLY_DONE; + + case FIRST_REPLY_DONE : + if(ssl->getCrypto().get_certManager().sendVerify()) + sendCertificate(*ssl); + + if (!ssl->getSecurity().get_resuming()) + sendClientKeyExchange(*ssl); + + if(ssl->getCrypto().get_certManager().sendVerify()) + sendCertificateVerify(*ssl); + + sendChangeCipher(*ssl); + sendFinished(*ssl, client_end); + ssl->flushBuffer(); + + if (!ssl->GetError()) + ssl->useStates().UseConnect() = FINISHED_DONE; + + case FINISHED_DONE : + if (!ssl->getSecurity().get_resuming()) + while (ssl->getStates().getClient() < serverFinishedComplete) { + if (ssl->GetError()) break; + processReply(*ssl); + } + if (!ssl->GetError()) + ssl->useStates().UseConnect() = SECOND_REPLY_DONE; + + case SECOND_REPLY_DONE : + ssl->verifyState(serverFinishedComplete); + ssl->useLog().ShowTCP(ssl->getSocket().get_fd()); + + if (ssl->GetError()) { + GetErrors().Add(ssl->GetError()); + return SSL_FATAL_ERROR; + } + return SSL_SUCCESS; + + default : + return SSL_FATAL_ERROR; // unkown state + } +} + + +int SSL_write(SSL* ssl, const void* buffer, int sz) +{ + return sendData(*ssl, buffer, sz); +} + + +int SSL_read(SSL* ssl, void* buffer, int sz) +{ + Data data(min(sz, MAX_RECORD_SIZE), static_cast<opaque*>(buffer)); + return receiveData(*ssl, data); +} + + +int SSL_accept(SSL* ssl) +{ + if (ssl->GetError() == YasslError(SSL_ERROR_WANT_READ)) + ssl->SetError(no_error); + + switch (ssl->getStates().GetAccept()) { + + case ACCEPT_BEGIN : + processReply(*ssl); + if (!ssl->GetError()) + ssl->useStates().UseAccept() = ACCEPT_FIRST_REPLY_DONE; + + case ACCEPT_FIRST_REPLY_DONE : + sendServerHello(*ssl); + + if (!ssl->getSecurity().get_resuming()) { + sendCertificate(*ssl); + + if (ssl->getSecurity().get_connection().send_server_key_) + sendServerKeyExchange(*ssl); + + if(ssl->getCrypto().get_certManager().verifyPeer()) + sendCertificateRequest(*ssl); + + sendServerHelloDone(*ssl); + ssl->flushBuffer(); + } + + if (!ssl->GetError()) + ssl->useStates().UseAccept() = SERVER_HELLO_DONE; + + case SERVER_HELLO_DONE : + if (!ssl->getSecurity().get_resuming()) { + while (ssl->getStates().getServer() < clientFinishedComplete) { + if (ssl->GetError()) break; + processReply(*ssl); + } + } + if (!ssl->GetError()) + ssl->useStates().UseAccept() = ACCEPT_SECOND_REPLY_DONE; + + case ACCEPT_SECOND_REPLY_DONE : + sendChangeCipher(*ssl); + sendFinished(*ssl, server_end); + ssl->flushBuffer(); + + if (!ssl->GetError()) + ssl->useStates().UseAccept() = ACCEPT_FINISHED_DONE; + + case ACCEPT_FINISHED_DONE : + if (ssl->getSecurity().get_resuming()) { + while (ssl->getStates().getServer() < clientFinishedComplete) { + if (ssl->GetError()) break; + processReply(*ssl); + } + } + if (!ssl->GetError()) + ssl->useStates().UseAccept() = ACCEPT_THIRD_REPLY_DONE; + + case ACCEPT_THIRD_REPLY_DONE : + ssl->useLog().ShowTCP(ssl->getSocket().get_fd()); + + if (ssl->GetError()) { + GetErrors().Add(ssl->GetError()); + return SSL_FATAL_ERROR; + } + return SSL_SUCCESS; + + default: + return SSL_FATAL_ERROR; // unknown state + } +} + + +int SSL_do_handshake(SSL* ssl) +{ + if (ssl->getSecurity().get_parms().entity_ == client_end) + return SSL_connect(ssl); + else + return SSL_accept(ssl); +} + + +int SSL_clear(SSL* ssl) +{ + GetErrors().Remove(); + + return SSL_SUCCESS; +} + + +int SSL_shutdown(SSL* ssl) +{ + if (!ssl->GetQuietShutdown()) { + Alert alert(warning, close_notify); + sendAlert(*ssl, alert); + } + ssl->useLog().ShowTCP(ssl->getSocket().get_fd(), true); + + GetErrors().Remove(); + + return SSL_SUCCESS; +} + + +void SSL_set_quiet_shutdown(SSL *ssl,int mode) +{ + ssl->SetQuietShutdown(mode != 0); +} + + +int SSL_get_quiet_shutdown(SSL *ssl) +{ + return ssl->GetQuietShutdown(); +} + + +/* on by default but allow user to turn off */ +long SSL_CTX_set_session_cache_mode(SSL_CTX* ctx, long mode) +{ + if (mode == SSL_SESS_CACHE_OFF) + ctx->SetSessionCacheOff(); + + if (mode == SSL_SESS_CACHE_NO_AUTO_CLEAR) + ctx->SetSessionCacheFlushOff(); + + return SSL_SUCCESS; +} + + +SSL_SESSION* SSL_get_session(SSL* ssl) +{ + if (ssl->getSecurity().GetContext()->GetSessionCacheOff()) + return 0; + + return GetSessions().lookup( + ssl->getSecurity().get_connection().sessionID_); +} + + +int SSL_set_session(SSL* ssl, SSL_SESSION* session) +{ + if (ssl->getSecurity().GetContext()->GetSessionCacheOff()) + return SSL_FAILURE; + + ssl->set_session(session); + return SSL_SUCCESS; +} + + +int SSL_session_reused(SSL* ssl) +{ + return ssl->getSecurity().get_resuming(); +} + + +long SSL_SESSION_set_timeout(SSL_SESSION* sess, long t) +{ + if (!sess) + return SSL_ERROR_NONE; + + sess->SetTimeOut(t); + return SSL_SUCCESS; +} + + +long SSL_get_default_timeout(SSL* /*ssl*/) +{ + return DEFAULT_TIMEOUT; +} + + +void SSL_flush_sessions(SSL_CTX *ctx, long /* tm */) +{ + if (ctx->GetSessionCacheOff()) + return; + + GetSessions().Flush(); +} + + +const char* SSL_get_cipher_name(SSL* ssl) +{ + return SSL_get_cipher(ssl); +} + + +const char* SSL_get_cipher(SSL* ssl) +{ + return ssl->getSecurity().get_parms().cipher_name_; +} + + +// SSLv2 only, not implemented +char* SSL_get_shared_ciphers(SSL* /*ssl*/, char* buf, int len) +{ + return strncpy(buf, "Not Implemented, SSLv2 only", len); +} + + +const char* SSL_get_cipher_list(SSL* ssl, int priority) +{ + if (priority < 0 || priority >= MAX_CIPHERS) + return 0; + + if (ssl->getSecurity().get_parms().cipher_list_[priority][0]) + return ssl->getSecurity().get_parms().cipher_list_[priority]; + + return 0; +} + + +int SSL_CTX_set_cipher_list(SSL_CTX* ctx, const char* list) +{ + if (ctx->SetCipherList(list)) + return SSL_SUCCESS; + else + return SSL_FAILURE; +} + + +const char* SSL_get_version(SSL* ssl) +{ + static const char* version3 = "SSLv3"; + static const char* version31 = "TLSv1"; + + return ssl->isTLS() ? version31 : version3; +} + +const char* SSLeay_version(int) +{ + static const char* version = "SSLeay yaSSL compatibility"; + return version; +} + + +int SSL_get_error(SSL* ssl, int /*previous*/) +{ + return ssl->getStates().What(); +} + + + +/* turn on yaSSL zlib compression + returns 0 for success, else error (not built in) + only need to turn on for client, becuase server on by default if built in + but calling for server will tell you whether it's available or not +*/ +int SSL_set_compression(SSL* ssl) /* Chad didn't rename to ya~ because it is prob. bug. */ +{ + return ssl->SetCompression(); +} + + + +X509* SSL_get_peer_certificate(SSL* ssl) +{ + return ssl->getCrypto().get_certManager().get_peerX509(); +} + + +void X509_free(X509* /*x*/) +{ + // peer cert set for deletion during destruction + // no need to delete now +} + + +X509* X509_STORE_CTX_get_current_cert(X509_STORE_CTX* ctx) +{ + return ctx->current_cert; +} + + +int X509_STORE_CTX_get_error(X509_STORE_CTX* ctx) +{ + return ctx->error; +} + + +int X509_STORE_CTX_get_error_depth(X509_STORE_CTX* ctx) +{ + return ctx->error_depth; +} + + +// copy name into buffer, at most sz bytes, if buffer is null +// will malloc buffer, caller responsible for freeing +char* X509_NAME_oneline(X509_NAME* name, char* buffer, int sz) +{ + if (!name->GetName()) return buffer; + + int len = (int)strlen(name->GetName()) + 1; + int copySz = min(len, sz); + + if (!buffer) { + buffer = (char*)malloc(len); + if (!buffer) return buffer; + copySz = len; + } + + if (copySz == 0) + return buffer; + + memcpy(buffer, name->GetName(), copySz - 1); + buffer[copySz - 1] = 0; + + return buffer; +} + + +X509_NAME* X509_get_issuer_name(X509* x) +{ + return x->GetIssuer(); +} + + +X509_NAME* X509_get_subject_name(X509* x) +{ + return x->GetSubject(); +} + + +void SSL_load_error_strings() // compatibility only +{} + + +void SSL_set_connect_state(SSL*) +{ + // already a client by default +} + + +void SSL_set_accept_state(SSL* ssl) +{ + ssl->useSecurity().use_parms().entity_ = server_end; +} + + +long SSL_get_verify_result(SSL*) +{ + // won't get here if not OK + return X509_V_OK; +} + + +long SSL_CTX_sess_set_cache_size(SSL_CTX* /*ctx*/, long /*sz*/) +{ + // unlimited size, can't set for now + return 0; +} + + +long SSL_CTX_get_session_cache_mode(SSL_CTX*) +{ + // always 0, unlimited size for now + return 0; +} + + +long SSL_CTX_set_tmp_dh(SSL_CTX* ctx, DH* dh) +{ + if (ctx->SetDH(*dh)) + return SSL_SUCCESS; + else + return SSL_FAILURE; +} + + +int SSL_CTX_use_certificate_file(SSL_CTX* ctx, const char* file, int format) +{ + return read_file(ctx, file, format, Cert); +} + + +int SSL_CTX_use_PrivateKey_file(SSL_CTX* ctx, const char* file, int format) +{ + return read_file(ctx, file, format, PrivateKey); +} + + +void SSL_CTX_set_verify(SSL_CTX* ctx, int mode, VerifyCallback vc) +{ + if (mode & SSL_VERIFY_PEER) + ctx->setVerifyPeer(); + + if (mode == SSL_VERIFY_NONE) + ctx->setVerifyNone(); + + if (mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT) + ctx->setFailNoCert(); + + ctx->setVerifyCallback(vc); +} + + +int SSL_CTX_load_verify_locations(SSL_CTX* ctx, const char* file, + const char* path) +{ + int ret = SSL_SUCCESS; + const int HALF_PATH = 128; + + if (file) ret = read_file(ctx, file, SSL_FILETYPE_PEM, CA); + + if (ret == SSL_SUCCESS && path) { + // call read_file for each reqular file in path +#ifdef _WIN32 + + WIN32_FIND_DATA FindFileData; + HANDLE hFind; + + char name[MAX_PATH + 1]; // directory specification + strncpy(name, path, MAX_PATH - 3); + strncat(name, "\\*", 3); + + hFind = FindFirstFile(name, &FindFileData); + if (hFind == INVALID_HANDLE_VALUE) return SSL_BAD_PATH; + + do { + if (FindFileData.dwFileAttributes != FILE_ATTRIBUTE_DIRECTORY) { + strncpy(name, path, MAX_PATH - 2 - HALF_PATH); + strncat(name, "\\", 2); + strncat(name, FindFileData.cFileName, HALF_PATH); + ret = read_file(ctx, name, SSL_FILETYPE_PEM, CA); + } + } while (ret == SSL_SUCCESS && FindNextFile(hFind, &FindFileData)); + + FindClose(hFind); + +#else // _WIN32 + + const int MAX_PATH = 260; + + DIR* dir = opendir(path); + if (!dir) return SSL_BAD_PATH; + + struct dirent* entry; + struct stat buf; + char name[MAX_PATH + 1]; + + while (ret == SSL_SUCCESS && (entry = readdir(dir))) { + strncpy(name, path, MAX_PATH - 1 - HALF_PATH); + strncat(name, "/", 1); + strncat(name, entry->d_name, HALF_PATH); + if (stat(name, &buf) < 0) return SSL_BAD_STAT; + + if (S_ISREG(buf.st_mode)) + ret = read_file(ctx, name, SSL_FILETYPE_PEM, CA); + } + + closedir(dir); + +#endif + } + + return ret; +} + + +int SSL_CTX_set_default_verify_paths(SSL_CTX* /*ctx*/) +{ + // TODO: figure out way to set/store default path, then call load_verify + return SSL_NOT_IMPLEMENTED; +} + + +int SSL_CTX_set_session_id_context(SSL_CTX*, const unsigned char*, + unsigned int) +{ + // No application specific context needed for yaSSL + return SSL_SUCCESS; +} + + +int SSL_CTX_check_private_key(SSL_CTX* /*ctx*/) +{ + // TODO: check private against public for RSA match + return SSL_NOT_IMPLEMENTED; +} + + +// TODO: all session stats +long SSL_CTX_sess_accept(SSL_CTX* ctx) +{ + return ctx->GetStats().accept_; +} + + +long SSL_CTX_sess_connect(SSL_CTX* ctx) +{ + return ctx->GetStats().connect_; +} + + +long SSL_CTX_sess_accept_good(SSL_CTX* ctx) +{ + return ctx->GetStats().acceptGood_; +} + + +long SSL_CTX_sess_connect_good(SSL_CTX* ctx) +{ + return ctx->GetStats().connectGood_; +} + + +long SSL_CTX_sess_accept_renegotiate(SSL_CTX* ctx) +{ + return ctx->GetStats().acceptRenegotiate_; +} + + +long SSL_CTX_sess_connect_renegotiate(SSL_CTX* ctx) +{ + return ctx->GetStats().connectRenegotiate_; +} + + +long SSL_CTX_sess_hits(SSL_CTX* ctx) +{ + return ctx->GetStats().hits_; +} + + +long SSL_CTX_sess_cb_hits(SSL_CTX* ctx) +{ + return ctx->GetStats().cbHits_; +} + + +long SSL_CTX_sess_cache_full(SSL_CTX* ctx) +{ + return ctx->GetStats().cacheFull_; +} + + +long SSL_CTX_sess_misses(SSL_CTX* ctx) +{ + return ctx->GetStats().misses_; +} + + +long SSL_CTX_sess_timeouts(SSL_CTX* ctx) +{ + return ctx->GetStats().timeouts_; +} + + +long SSL_CTX_sess_number(SSL_CTX* ctx) +{ + return ctx->GetStats().number_; +} + + +long SSL_CTX_sess_get_cache_size(SSL_CTX* ctx) +{ + return ctx->GetStats().getCacheSize_; +} +// end session stats TODO: + + +int SSL_CTX_get_verify_mode(SSL_CTX* ctx) +{ + return ctx->GetStats().verifyMode_; +} + + +int SSL_get_verify_mode(SSL* ssl) +{ + return ssl->getSecurity().GetContext()->GetStats().verifyMode_; +} + + +int SSL_CTX_get_verify_depth(SSL_CTX* ctx) +{ + return ctx->GetStats().verifyDepth_; +} + + +int SSL_get_verify_depth(SSL* ssl) +{ + return ssl->getSecurity().GetContext()->GetStats().verifyDepth_; +} + + +long SSL_CTX_set_options(SSL_CTX*, long) +{ + // TDOD: + return SSL_SUCCESS; +} + + +void SSL_CTX_set_info_callback(SSL_CTX*, void (*)()) +{ + // TDOD: +} + + +void OpenSSL_add_all_algorithms() // compatibility only +{} + + +int SSL_library_init() // compatiblity only +{ + return 1; +} + + +DH* DH_new(void) +{ + DH* dh = NEW_YS DH; + if (dh) + dh->p = dh->g = 0; + return dh; +} + + +void DH_free(DH* dh) +{ + ysDelete(dh->g); + ysDelete(dh->p); + ysDelete(dh); +} + + +// convert positive big-endian num of length sz into retVal, which may need to +// be created +BIGNUM* BN_bin2bn(const unsigned char* num, int sz, BIGNUM* retVal) +{ + bool created = false; + mySTL::auto_ptr<BIGNUM> bn; + + if (!retVal) { + created = true; + bn.reset(NEW_YS BIGNUM); + retVal = bn.get(); + } + + retVal->assign(num, sz); + + if (created) + return bn.release(); + else + return retVal; +} + + +unsigned long ERR_get_error_line_data(const char**, int*, const char**, int *) +{ + //return SSL_NOT_IMPLEMENTED; + return 0; +} + + +void ERR_print_errors_fp(FILE* /*fp*/) +{ + // need ssl access to implement TODO: + //fprintf(fp, "%s", ssl.get_states().errorString_.c_str()); +} + + +char* ERR_error_string(unsigned long errNumber, char* buffer) +{ + static char* msg = (char*)"Please supply a buffer for error string"; + + if (buffer) { + SetErrorString(YasslError(errNumber), buffer); + return buffer; + } + + return msg; +} + + +const char* X509_verify_cert_error_string(long /* error */) +{ + // TODO: + static const char* msg = "Not Implemented"; + return msg; +} + + +const EVP_MD* EVP_md5(void) +{ + static const char* type = "MD5"; + return type; +} + + +const EVP_CIPHER* EVP_des_ede3_cbc(void) +{ + static const char* type = "DES-EDE3-CBC"; + return type; +} + + +int EVP_BytesToKey(const EVP_CIPHER* type, const EVP_MD* md, const byte* salt, + const byte* data, int sz, int count, byte* key, byte* iv) +{ + // only support MD5 for now + if (strncmp(md, "MD5", 3)) return 0; + + int keyLen = 0; + int ivLen = 0; + + // only support CBC DES and AES for now + if (strncmp(type, "DES-CBC", 7) == 0) { + keyLen = DES_KEY_SZ; + ivLen = DES_IV_SZ; + } + else if (strncmp(type, "DES-EDE3-CBC", 12) == 0) { + keyLen = DES_EDE_KEY_SZ; + ivLen = DES_IV_SZ; + } + else if (strncmp(type, "AES-128-CBC", 11) == 0) { + keyLen = AES_128_KEY_SZ; + ivLen = AES_IV_SZ; + } + else if (strncmp(type, "AES-192-CBC", 11) == 0) { + keyLen = AES_192_KEY_SZ; + ivLen = AES_IV_SZ; + } + else if (strncmp(type, "AES-256-CBC", 11) == 0) { + keyLen = AES_256_KEY_SZ; + ivLen = AES_IV_SZ; + } + else + return 0; + + yaSSL::MD5 myMD; + uint digestSz = myMD.get_digestSize(); + byte digest[SHA_LEN]; // max size + + int keyLeft = keyLen; + int ivLeft = ivLen; + int keyOutput = 0; + + while (keyOutput < (keyLen + ivLen)) { + int digestLeft = digestSz; + // D_(i - 1) + if (keyOutput) // first time D_0 is empty + myMD.update(digest, digestSz); + // data + myMD.update(data, sz); + // salt + if (salt) + myMD.update(salt, EVP_SALT_SZ); + myMD.get_digest(digest); + // count + for (int j = 1; j < count; j++) { + myMD.update(digest, digestSz); + myMD.get_digest(digest); + } + + if (keyLeft) { + int store = min(keyLeft, static_cast<int>(digestSz)); + memcpy(&key[keyLen - keyLeft], digest, store); + + keyOutput += store; + keyLeft -= store; + digestLeft -= store; + } + + if (ivLeft && digestLeft) { + int store = min(ivLeft, digestLeft); + memcpy(&iv[ivLen - ivLeft], &digest[digestSz - digestLeft], store); + + keyOutput += store; + ivLeft -= store; + } + } + assert(keyOutput == (keyLen + ivLen)); + return keyOutput; +} + + + +void DES_set_key_unchecked(const_DES_cblock* key, DES_key_schedule* schedule) +{ + memcpy(schedule, key, sizeof(const_DES_cblock)); +} + + +void DES_ede3_cbc_encrypt(const byte* input, byte* output, long sz, + DES_key_schedule* ks1, DES_key_schedule* ks2, + DES_key_schedule* ks3, DES_cblock* ivec, int enc) +{ + DES_EDE des; + byte key[DES_EDE_KEY_SZ]; + + memcpy(key, *ks1, DES_BLOCK); + memcpy(&key[DES_BLOCK], *ks2, DES_BLOCK); + memcpy(&key[DES_BLOCK * 2], *ks3, DES_BLOCK); + + if (enc) { + des.set_encryptKey(key, *ivec); + des.encrypt(output, input, sz); + } + else { + des.set_decryptKey(key, *ivec); + des.decrypt(output, input, sz); + } +} + + +// functions for libcurl +int RAND_status() +{ + return 1; /* TaoCrypt provides enough seed */ +} + + +int DES_set_key(const_DES_cblock* key, DES_key_schedule* schedule) +{ + memcpy(schedule, key, sizeof(const_DES_cblock)); + return 1; +} + + +void DES_set_odd_parity(DES_cblock* key) +{ + // not needed now for TaoCrypt +} + + +void DES_ecb_encrypt(DES_cblock* input, DES_cblock* output, + DES_key_schedule* key, int enc) +{ + DES des; + + if (enc) { + des.set_encryptKey(*key, 0); + des.encrypt(*output, *input, DES_BLOCK); + } + else { + des.set_decryptKey(*key, 0); + des.decrypt(*output, *input, DES_BLOCK); + } +} + + +void SSL_CTX_set_default_passwd_cb_userdata(SSL_CTX* ctx, void* userdata) +{ + ctx->SetUserData(userdata); +} + + +X509* SSL_get_certificate(SSL* ssl) +{ + // only used to pass to get_privatekey which isn't used + return 0; +} + + +EVP_PKEY* SSL_get_privatekey(SSL* ssl) +{ + // only called, not used + return 0; +} + + +void SSL_SESSION_free(SSL_SESSION* session) +{ + // managed by singleton +} + + + +EVP_PKEY* X509_get_pubkey(X509* x) +{ + // called, not used though + return 0; +} + + +int EVP_PKEY_copy_parameters(EVP_PKEY* to, const EVP_PKEY* from) +{ + // called, not used though + return 0; +} + + +void EVP_PKEY_free(EVP_PKEY* pkey) +{ + // never allocated from above +} + + +void ERR_error_string_n(unsigned long e, char *buf, size_t len) +{ + if (len) ERR_error_string(e, buf); +} + + +void ERR_free_strings(void) +{ + // handled internally +} + + +void EVP_cleanup(void) +{ + // nothing to do yet +} + + +ASN1_TIME* X509_get_notBefore(X509* x) +{ + if (x) return x->GetBefore(); + return 0; +} + + +ASN1_TIME* X509_get_notAfter(X509* x) +{ + if (x) return x->GetAfter(); + return 0; +} + + +SSL_METHOD* SSLv2_client_method(void) /* will never work, no v 2 */ +{ + return 0; +} + + +SSL_SESSION* SSL_get1_session(SSL* ssl) /* what's ref count */ +{ + return SSL_get_session(ssl); +} + + +void GENERAL_NAMES_free(STACK_OF(GENERAL_NAME) *x) +{ + // no extension names supported yet +} + + +int sk_GENERAL_NAME_num(STACK_OF(GENERAL_NAME) *x) +{ + // no extension names supported yet + return 0; +} + + +GENERAL_NAME* sk_GENERAL_NAME_value(STACK_OF(GENERAL_NAME) *x, int i) +{ + // no extension names supported yet + return 0; +} + + +unsigned char* ASN1_STRING_data(ASN1_STRING* x) +{ + if (x) return x->data; + return 0; +} + + +int ASN1_STRING_length(ASN1_STRING* x) +{ + if (x) return x->length; + return 0; +} + + +int ASN1_STRING_type(ASN1_STRING *x) +{ + if (x) return x->type; + return 0; +} + + +int X509_NAME_get_index_by_NID(X509_NAME* name,int nid, int lastpos) +{ + int idx = -1; // not found + const char* start = &name->GetName()[lastpos + 1]; + + switch (nid) { + case NID_commonName: + const char* found = strstr(start, "/CN="); + if (found) { + found += 4; // advance to str + idx = found - start + lastpos + 1; + } + break; + } + + return idx; +} + + +ASN1_STRING* X509_NAME_ENTRY_get_data(X509_NAME_ENTRY* ne) +{ + // the same in yaSSL + return ne; +} + + +X509_NAME_ENTRY* X509_NAME_get_entry(X509_NAME* name, int loc) +{ + return name->GetEntry(loc); +} + + +// already formatted, caller responsible for freeing *out +int ASN1_STRING_to_UTF8(unsigned char** out, ASN1_STRING* in) +{ + if (!in) return 0; + + *out = (unsigned char*)malloc(in->length + 1); + if (*out) { + memcpy(*out, in->data, in->length); + (*out)[in->length] = 0; + } + return in->length; +} + + +void* X509_get_ext_d2i(X509* x, int nid, int* crit, int* idx) +{ + // no extensions supported yet + return 0; +} + + +void MD4_Init(MD4_CTX* md4) +{ + // make sure we have a big enough buffer + typedef char ok[sizeof(md4->buffer) >= sizeof(TaoCrypt::MD4) ? 1 : -1]; + (void) sizeof(ok); + + // using TaoCrypt since no dynamic memory allocated + // and no destructor will be called + new (reinterpret_cast<yassl_pointer>(md4->buffer)) TaoCrypt::MD4(); +} + + +void MD4_Update(MD4_CTX* md4, const void* data, unsigned long sz) +{ + reinterpret_cast<TaoCrypt::MD4*>(md4->buffer)->Update( + static_cast<const byte*>(data), static_cast<unsigned int>(sz)); +} + + +void MD4_Final(unsigned char* hash, MD4_CTX* md4) +{ + reinterpret_cast<TaoCrypt::MD4*>(md4->buffer)->Final(hash); +} + + +void MD5_Init(MD5_CTX* md5) +{ + // make sure we have a big enough buffer + typedef char ok[sizeof(md5->buffer) >= sizeof(TaoCrypt::MD5) ? 1 : -1]; + (void) sizeof(ok); + + // using TaoCrypt since no dynamic memory allocated + // and no destructor will be called + new (reinterpret_cast<yassl_pointer>(md5->buffer)) TaoCrypt::MD5(); +} + + +void MD5_Update(MD5_CTX* md5, const void* data, unsigned long sz) +{ + reinterpret_cast<TaoCrypt::MD5*>(md5->buffer)->Update( + static_cast<const byte*>(data), static_cast<unsigned int>(sz)); +} + + +void MD5_Final(unsigned char* hash, MD5_CTX* md5) +{ + reinterpret_cast<TaoCrypt::MD5*>(md5->buffer)->Final(hash); +} + + +int RAND_bytes(unsigned char* buf, int num) +{ + RandomPool ran; + + if (ran.GetError()) return 0; + + ran.Fill(buf, num); + return 1; +} + + +int SSL_peek(SSL* ssl, void* buffer, int sz) +{ + Data data(min(sz, MAX_RECORD_SIZE), static_cast<opaque*>(buffer)); + return receiveData(*ssl, data, true); +} + + +int SSL_pending(SSL* ssl) +{ + // Just in case there's pending data that hasn't been processed yet... + char c; + SSL_peek(ssl, &c, 1); + + return ssl->bufferedData(); +} + + +void SSL_CTX_set_default_passwd_cb(SSL_CTX* ctx, pem_password_cb cb) +{ + ctx->SetPasswordCb(cb); +} + + +int SSLeay_add_ssl_algorithms() // compatibility only +{ + return 1; +} + + +void ERR_remove_state(unsigned long) +{ + GetErrors().Remove(); +} + + +int ERR_GET_REASON(int l) +{ + return l & 0xfff; +} + + +unsigned long err_helper(bool peek = false) +{ + int ysError = GetErrors().Lookup(peek); + + // translate cert error for libcurl, it uses OpenSSL hex code + switch (ysError) { + case TaoCrypt::SIG_OTHER_E: + return CERTFICATE_ERROR; + break; + default : + return 0; + } + + return 0; // shut up compiler +} + + +unsigned long ERR_peek_error() +{ + return err_helper(true); +} + + +unsigned long ERR_get_error() +{ + return err_helper(); +} + + + // functions for stunnel + + void RAND_screen() + { + // TODO: + } + + + const char* RAND_file_name(char*, size_t) + { + // TODO: + return 0; + } + + + int RAND_write_file(const char*) + { + // TODO: + return 0; + } + + + int RAND_load_file(const char*, long) + { + // TODO: + return 0; + } + + + void RSA_free(RSA*) + { + // TODO: + } + + + RSA* RSA_generate_key(int, unsigned long, void(*)(int, int, void*), void*) + { + // TODO: + return 0; + } + + + int X509_LOOKUP_add_dir(X509_LOOKUP*, const char*, long) + { + // TODO: + return SSL_SUCCESS; + } + + + int X509_LOOKUP_load_file(X509_LOOKUP*, const char*, long) + { + // TODO: + return SSL_SUCCESS; + } + + + X509_LOOKUP_METHOD* X509_LOOKUP_hash_dir(void) + { + // TODO: + return 0; + } + + + X509_LOOKUP_METHOD* X509_LOOKUP_file(void) + { + // TODO: + return 0; + } + + + X509_LOOKUP* X509_STORE_add_lookup(X509_STORE*, X509_LOOKUP_METHOD*) + { + // TODO: + return 0; + } + + + int X509_STORE_get_by_subject(X509_STORE_CTX*, int, X509_NAME*, X509_OBJECT*) + { + // TODO: + return SSL_SUCCESS; + } + + + X509_STORE* X509_STORE_new(void) + { + // TODO: + return 0; + } + + char* SSL_alert_type_string_long(int) + { + // TODO: + return 0; + } + + + char* SSL_alert_desc_string_long(int) + { + // TODO: + return 0; + } + + + char* SSL_state_string_long(SSL*) + { + // TODO: + return 0; + } + + + void SSL_CTX_set_tmp_rsa_callback(SSL_CTX*, RSA*(*)(SSL*, int, int)) + { + // TDOD: + } + + + long SSL_CTX_set_timeout(SSL_CTX*, long) + { + // TDOD: + return SSL_SUCCESS; + } + + + int SSL_CTX_use_certificate_chain_file(SSL_CTX*, const char*) + { + // TDOD: + return SSL_SUCCESS; + } + + + int SSL_CTX_use_RSAPrivateKey_file(SSL_CTX*, const char*, int) + { + // TDOD: + return SSL_SUCCESS; + } + + + int SSL_set_rfd(SSL*, int) + { + return SSL_SUCCESS; // TODO: + } + + + int SSL_set_wfd(SSL*, int) + { + return SSL_SUCCESS; // TODO: + } + + + int SSL_want_read(SSL*) + { + return 0; // TODO: + } + + + int SSL_want_write(SSL*) + { + return 0; // TODO: + } + + + void SSL_set_shutdown(SSL*, int) + { + // TODO: + } + + + SSL_CIPHER* SSL_get_current_cipher(SSL*) + { + // TODO: + return 0; + } + + + char* SSL_CIPHER_description(SSL_CIPHER*, char*, int) + { + // TODO: + return 0; + } + + + + // end stunnel needs + + +} // extern "C" +} // namespace diff --git a/externals/mysql/extlib/yassl/src/template_instnt.cpp b/externals/mysql/extlib/yassl/src/template_instnt.cpp new file mode 100644 index 00000000000..fe3a251b865 --- /dev/null +++ b/externals/mysql/extlib/yassl/src/template_instnt.cpp @@ -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 + diff --git a/externals/mysql/extlib/yassl/src/timer.cpp b/externals/mysql/extlib/yassl/src/timer.cpp new file mode 100644 index 00000000000..c1286b0724c --- /dev/null +++ b/externals/mysql/extlib/yassl/src/timer.cpp @@ -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 diff --git a/externals/mysql/extlib/yassl/src/yassl.cpp b/externals/mysql/extlib/yassl/src/yassl.cpp new file mode 100644 index 00000000000..815277ce6f3 --- /dev/null +++ b/externals/mysql/extlib/yassl/src/yassl.cpp @@ -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 diff --git a/externals/mysql/extlib/yassl/src/yassl_error.cpp b/externals/mysql/extlib/yassl/src/yassl_error.cpp new file mode 100644 index 00000000000..a1ef8578da6 --- /dev/null +++ b/externals/mysql/extlib/yassl/src/yassl_error.cpp @@ -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 diff --git a/externals/mysql/extlib/yassl/src/yassl_imp.cpp b/externals/mysql/extlib/yassl/src/yassl_imp.cpp new file mode 100644 index 00000000000..20dfe50f132 --- /dev/null +++ b/externals/mysql/extlib/yassl/src/yassl_imp.cpp @@ -0,0 +1,2287 @@ +/* + 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 source implements all SSL.v3 secification structures. + */ + +#include "runtime.hpp" +#include "yassl_int.hpp" +#include "handshake.hpp" + +#include "asn.hpp" // provide crypto wrapper?? + + + +namespace yaSSL { + + +namespace { // locals + +bool isTLS(ProtocolVersion pv) +{ + if (pv.major_ >= 3 && pv.minor_ >= 1) + return true; + + return false; +} + + +} // namespace (locals) + + +void hashHandShake(SSL&, const input_buffer&, uint); + + +ProtocolVersion::ProtocolVersion(uint8 maj, uint8 min) + : major_(maj), minor_(min) +{} + + +// construct key exchange with known ssl parms +void ClientKeyExchange::createKey(SSL& ssl) +{ + const ClientKeyFactory& ckf = ssl.getFactory().getClientKey(); + client_key_ = ckf.CreateObject(ssl.getSecurity().get_parms().kea_); + + if (!client_key_) + ssl.SetError(factory_error); +} + + +// construct key exchange with known ssl parms +void ServerKeyExchange::createKey(SSL& ssl) +{ + const ServerKeyFactory& skf = ssl.getFactory().getServerKey(); + server_key_ = skf.CreateObject(ssl.getSecurity().get_parms().kea_); + + if (!server_key_) + ssl.SetError(factory_error); +} + + +// build/set PreMaster secret and encrypt, client side +void EncryptedPreMasterSecret::build(SSL& ssl) +{ + opaque tmp[SECRET_LEN]; + memset(tmp, 0, sizeof(tmp)); + ssl.getCrypto().get_random().Fill(tmp, SECRET_LEN); + ProtocolVersion pv = ssl.getSecurity().get_connection().chVersion_; + tmp[0] = pv.major_; + tmp[1] = pv.minor_; + ssl.set_preMaster(tmp, SECRET_LEN); + + const CertManager& cert = ssl.getCrypto().get_certManager(); + RSA rsa(cert.get_peerKey(), cert.get_peerKeyLength()); + bool tls = ssl.isTLS(); // if TLS, put length for encrypted data + alloc(rsa.get_cipherLength() + (tls ? 2 : 0)); + byte* holder = secret_; + if (tls) { + byte len[2]; + c16toa(rsa.get_cipherLength(), len); + memcpy(secret_, len, sizeof(len)); + holder += 2; + } + rsa.encrypt(holder, tmp, SECRET_LEN, ssl.getCrypto().get_random()); +} + + +// build/set premaster and Client Public key, client side +void ClientDiffieHellmanPublic::build(SSL& ssl) +{ + DiffieHellman& dhServer = ssl.useCrypto().use_dh(); + DiffieHellman dhClient(dhServer); + + uint keyLength = dhClient.get_agreedKeyLength(); // pub and agree same + + alloc(keyLength, true); + dhClient.makeAgreement(dhServer.get_publicKey(), keyLength); + c16toa(keyLength, Yc_); + memcpy(Yc_ + KEY_OFFSET, dhClient.get_publicKey(), keyLength); + + // because of encoding first byte might be zero, don't use it for preMaster + if (*dhClient.get_agreedKey() == 0) + ssl.set_preMaster(dhClient.get_agreedKey() + 1, keyLength - 1); + else + ssl.set_preMaster(dhClient.get_agreedKey(), keyLength); +} + + +// build server exhange, server side +void DH_Server::build(SSL& ssl) +{ + DiffieHellman& dhServer = ssl.useCrypto().use_dh(); + + int pSz, gSz, pubSz; + dhServer.set_sizes(pSz, gSz, pubSz); + dhServer.get_parms(parms_.alloc_p(pSz), parms_.alloc_g(gSz), + parms_.alloc_pub(pubSz)); + + short sigSz = 0; + mySTL::auto_ptr<Auth> auth; + const CertManager& cert = ssl.getCrypto().get_certManager(); + + if (ssl.getSecurity().get_parms().sig_algo_ == rsa_sa_algo) + { + if (cert.get_keyType() != rsa_sa_algo) { + ssl.SetError(privateKey_error); + return; + } + auth.reset(NEW_YS RSA(cert.get_privateKey(), + cert.get_privateKeyLength(), false)); + } + else { + if (cert.get_keyType() != dsa_sa_algo) { + ssl.SetError(privateKey_error); + return; + } + auth.reset(NEW_YS DSS(cert.get_privateKey(), + cert.get_privateKeyLength(), false)); + sigSz += DSS_ENCODED_EXTRA; + } + + sigSz += auth->get_signatureLength(); + if (!sigSz) { + ssl.SetError(privateKey_error); + return; + } + + length_ = 8; // pLen + gLen + YsLen + SigLen + length_ += pSz + gSz + pubSz + sigSz; + + output_buffer tmp(length_); + byte len[2]; + // P + c16toa(pSz, len); + tmp.write(len, sizeof(len)); + tmp.write(parms_.get_p(), pSz); + // G + c16toa(gSz, len); + tmp.write(len, sizeof(len)); + tmp.write(parms_.get_g(), gSz); + // Ys + c16toa(pubSz, len); + tmp.write(len, sizeof(len)); + tmp.write(parms_.get_pub(), pubSz); + + // Sig + byte hash[FINISHED_SZ]; + MD5 md5; + SHA sha; + signature_ = NEW_YS byte[sigSz]; + + const Connection& conn = ssl.getSecurity().get_connection(); + // md5 + md5.update(conn.client_random_, RAN_LEN); + md5.update(conn.server_random_, RAN_LEN); + md5.update(tmp.get_buffer(), tmp.get_size()); + md5.get_digest(hash); + + // sha + sha.update(conn.client_random_, RAN_LEN); + sha.update(conn.server_random_, RAN_LEN); + sha.update(tmp.get_buffer(), tmp.get_size()); + sha.get_digest(&hash[MD5_LEN]); + + if (ssl.getSecurity().get_parms().sig_algo_ == rsa_sa_algo) + auth->sign(signature_, hash, sizeof(hash), + ssl.getCrypto().get_random()); + else { + auth->sign(signature_, &hash[MD5_LEN], SHA_LEN, + ssl.getCrypto().get_random()); + byte encoded[DSS_SIG_SZ + DSS_ENCODED_EXTRA]; + TaoCrypt::EncodeDSA_Signature(signature_, encoded); + memcpy(signature_, encoded, sizeof(encoded)); + } + + c16toa(sigSz, len); + tmp.write(len, sizeof(len)); + tmp.write(signature_, sigSz); + + // key message + keyMessage_ = NEW_YS opaque[length_]; + memcpy(keyMessage_, tmp.get_buffer(), tmp.get_size()); +} + + +// read PreMaster secret and decrypt, server side +void EncryptedPreMasterSecret::read(SSL& ssl, input_buffer& input) +{ + const CertManager& cert = ssl.getCrypto().get_certManager(); + RSA rsa(cert.get_privateKey(), cert.get_privateKeyLength(), false); + uint16 cipherLen = rsa.get_cipherLength(); + if (ssl.isTLS()) { + byte len[2]; + input.read(len, sizeof(len)); + ato16(len, cipherLen); + } + alloc(cipherLen); + input.read(secret_, length_); + + opaque preMasterSecret[SECRET_LEN]; + rsa.decrypt(preMasterSecret, secret_, length_, + ssl.getCrypto().get_random()); + + ProtocolVersion pv = ssl.getSecurity().get_connection().chVersion_; + if (pv.major_ != preMasterSecret[0] || pv.minor_ != preMasterSecret[1]) + ssl.SetError(pms_version_error); // continue deriving for timing attack + + ssl.set_preMaster(preMasterSecret, SECRET_LEN); + ssl.makeMasterSecret(); +} + + +EncryptedPreMasterSecret::EncryptedPreMasterSecret() + : secret_(0), length_(0) +{} + + +EncryptedPreMasterSecret::~EncryptedPreMasterSecret() +{ + ysArrayDelete(secret_); +} + + +int EncryptedPreMasterSecret::get_length() const +{ + return length_; +} + + +opaque* EncryptedPreMasterSecret::get_clientKey() const +{ + return secret_; +} + + +void EncryptedPreMasterSecret::alloc(int sz) +{ + length_ = sz; + secret_ = NEW_YS opaque[sz]; +} + + +// read client's public key, server side +void ClientDiffieHellmanPublic::read(SSL& ssl, input_buffer& input) +{ + DiffieHellman& dh = ssl.useCrypto().use_dh(); + + uint16 keyLength; + byte tmp[2]; + tmp[0] = input[AUTO]; + tmp[1] = input[AUTO]; + ato16(tmp, keyLength); + + alloc(keyLength); + input.read(Yc_, keyLength); + dh.makeAgreement(Yc_, keyLength); + + // because of encoding, first byte might be 0, don't use for preMaster + if (*dh.get_agreedKey() == 0) + ssl.set_preMaster(dh.get_agreedKey() + 1, dh.get_agreedKeyLength() - 1); + else + ssl.set_preMaster(dh.get_agreedKey(), dh.get_agreedKeyLength()); + ssl.makeMasterSecret(); +} + + +ClientDiffieHellmanPublic::ClientDiffieHellmanPublic() + : length_(0), Yc_(0) +{} + + +ClientDiffieHellmanPublic::~ClientDiffieHellmanPublic() +{ + ysArrayDelete(Yc_); +} + + +int ClientDiffieHellmanPublic::get_length() const +{ + return length_; +} + + +opaque* ClientDiffieHellmanPublic::get_clientKey() const +{ + return Yc_; +} + + +void ClientDiffieHellmanPublic::alloc(int sz, bool offset) +{ + length_ = sz + (offset ? KEY_OFFSET : 0); + Yc_ = NEW_YS opaque[length_]; +} + + +// read server's p, g, public key and sig, client side +void DH_Server::read(SSL& ssl, input_buffer& input) +{ + uint16 length, messageTotal = 6; // pSz + gSz + pubSz + byte tmp[2]; + + // p + tmp[0] = input[AUTO]; + tmp[1] = input[AUTO]; + ato16(tmp, length); + messageTotal += length; + + input.read(parms_.alloc_p(length), length); + + // g + tmp[0] = input[AUTO]; + tmp[1] = input[AUTO]; + ato16(tmp, length); + messageTotal += length; + + input.read(parms_.alloc_g(length), length); + + // pub + tmp[0] = input[AUTO]; + tmp[1] = input[AUTO]; + ato16(tmp, length); + messageTotal += length; + + input.read(parms_.alloc_pub(length), length); + + // save message for hash verify + input_buffer message(messageTotal); + input.set_current(input.get_current() - messageTotal); + input.read(message.get_buffer(), messageTotal); + message.add_size(messageTotal); + + // signature + tmp[0] = input[AUTO]; + tmp[1] = input[AUTO]; + ato16(tmp, length); + + signature_ = NEW_YS byte[length]; + input.read(signature_, length); + + // verify signature + byte hash[FINISHED_SZ]; + MD5 md5; + SHA sha; + + const Connection& conn = ssl.getSecurity().get_connection(); + // md5 + md5.update(conn.client_random_, RAN_LEN); + md5.update(conn.server_random_, RAN_LEN); + md5.update(message.get_buffer(), message.get_size()); + md5.get_digest(hash); + + // sha + sha.update(conn.client_random_, RAN_LEN); + sha.update(conn.server_random_, RAN_LEN); + sha.update(message.get_buffer(), message.get_size()); + sha.get_digest(&hash[MD5_LEN]); + + const CertManager& cert = ssl.getCrypto().get_certManager(); + + if (ssl.getSecurity().get_parms().sig_algo_ == rsa_sa_algo) { + RSA rsa(cert.get_peerKey(), cert.get_peerKeyLength()); + if (!rsa.verify(hash, sizeof(hash), signature_, length)) + ssl.SetError(verify_error); + } + else { + byte decodedSig[DSS_SIG_SZ]; + length = TaoCrypt::DecodeDSA_Signature(decodedSig, signature_, length); + + DSS dss(cert.get_peerKey(), cert.get_peerKeyLength()); + if (!dss.verify(&hash[MD5_LEN], SHA_LEN, decodedSig, length)) + ssl.SetError(verify_error); + } + + // save input + ssl.useCrypto().SetDH(NEW_YS DiffieHellman(parms_.get_p(), + parms_.get_pSize(), parms_.get_g(), parms_.get_gSize(), + parms_.get_pub(), parms_.get_pubSize(), + ssl.getCrypto().get_random())); +} + + +DH_Server::DH_Server() + : signature_(0), length_(0), keyMessage_(0) +{} + + +DH_Server::~DH_Server() +{ + ysArrayDelete(keyMessage_); + ysArrayDelete(signature_); +} + + +int DH_Server::get_length() const +{ + return length_; +} + + +opaque* DH_Server::get_serverKey() const +{ + return keyMessage_; +} + + +// set available suites +Parameters::Parameters(ConnectionEnd ce, const Ciphers& ciphers, + ProtocolVersion pv, bool haveDH) : entity_(ce) +{ + pending_ = true; // suite not set yet + strncpy(cipher_name_, "NONE", 5); + + removeDH_ = !haveDH; // only use on server side for set suites + + if (ciphers.setSuites_) { // use user set list + suites_size_ = ciphers.suiteSz_; + memcpy(suites_, ciphers.suites_, ciphers.suiteSz_); + SetCipherNames(); + } + else + SetSuites(pv, ce == server_end && removeDH_); // defaults + +} + + +void Parameters::SetSuites(ProtocolVersion pv, bool removeDH, bool removeRSA, + bool removeDSA) +{ + int i = 0; + // available suites, best first + // when adding more, make sure cipher_names is updated and + // MAX_CIPHERS is big enough + + if (isTLS(pv)) { + if (!removeDH) { + if (!removeRSA) { + suites_[i++] = 0x00; + suites_[i++] = TLS_DHE_RSA_WITH_AES_256_CBC_SHA; + } + if (!removeDSA) { + suites_[i++] = 0x00; + suites_[i++] = TLS_DHE_DSS_WITH_AES_256_CBC_SHA; + } + } + if (!removeRSA) { + suites_[i++] = 0x00; + suites_[i++] = TLS_RSA_WITH_AES_256_CBC_SHA; + } + if (!removeDH) { + if (!removeRSA) { + suites_[i++] = 0x00; + suites_[i++] = TLS_DHE_RSA_WITH_AES_128_CBC_SHA; + } + if (!removeDSA) { + suites_[i++] = 0x00; + suites_[i++] = TLS_DHE_DSS_WITH_AES_128_CBC_SHA; + } + } + if (!removeRSA) { + suites_[i++] = 0x00; + suites_[i++] = TLS_RSA_WITH_AES_128_CBC_SHA; + suites_[i++] = 0x00; + suites_[i++] = TLS_RSA_WITH_AES_256_CBC_RMD160; + suites_[i++] = 0x00; + suites_[i++] = TLS_RSA_WITH_AES_128_CBC_RMD160; + suites_[i++] = 0x00; + suites_[i++] = TLS_RSA_WITH_3DES_EDE_CBC_RMD160; + } + if (!removeDH) { + if (!removeRSA) { + suites_[i++] = 0x00; + suites_[i++] = TLS_DHE_RSA_WITH_AES_256_CBC_RMD160; + suites_[i++] = 0x00; + suites_[i++] = TLS_DHE_RSA_WITH_AES_128_CBC_RMD160; + suites_[i++] = 0x00; + suites_[i++] = TLS_DHE_RSA_WITH_3DES_EDE_CBC_RMD160; + } + if (!removeDSA) { + suites_[i++] = 0x00; + suites_[i++] = TLS_DHE_DSS_WITH_AES_256_CBC_RMD160; + suites_[i++] = 0x00; + suites_[i++] = TLS_DHE_DSS_WITH_AES_128_CBC_RMD160; + suites_[i++] = 0x00; + suites_[i++] = TLS_DHE_DSS_WITH_3DES_EDE_CBC_RMD160; + } + } + } + + if (!removeRSA) { + suites_[i++] = 0x00; + suites_[i++] = SSL_RSA_WITH_RC4_128_SHA; + suites_[i++] = 0x00; + suites_[i++] = SSL_RSA_WITH_RC4_128_MD5; + + suites_[i++] = 0x00; + suites_[i++] = SSL_RSA_WITH_3DES_EDE_CBC_SHA; + suites_[i++] = 0x00; + suites_[i++] = SSL_RSA_WITH_DES_CBC_SHA; + } + if (!removeDH) { + if (!removeRSA) { + suites_[i++] = 0x00; + suites_[i++] = SSL_DHE_RSA_WITH_3DES_EDE_CBC_SHA; + } + if (!removeDSA) { + suites_[i++] = 0x00; + suites_[i++] = SSL_DHE_DSS_WITH_3DES_EDE_CBC_SHA; + } + if (!removeRSA) { + suites_[i++] = 0x00; + suites_[i++] = SSL_DHE_RSA_WITH_DES_CBC_SHA; + } + if (!removeDSA) { + suites_[i++] = 0x00; + suites_[i++] = SSL_DHE_DSS_WITH_DES_CBC_SHA; + } + } + + suites_size_ = i; + + SetCipherNames(); +} + + +void Parameters::SetCipherNames() +{ + const int suites = suites_size_ / 2; + int pos = 0; + + for (int j = 0; j < suites; j++) { + int index = suites_[j*2 + 1]; // every other suite is suite id + size_t len = strlen(cipher_names[index]) + 1; + strncpy(cipher_list_[pos++], cipher_names[index], len); + } + cipher_list_[pos][0] = 0; +} + + +// input operator for RecordLayerHeader, adjust stream +input_buffer& operator>>(input_buffer& input, RecordLayerHeader& hdr) +{ + hdr.type_ = ContentType(input[AUTO]); + hdr.version_.major_ = input[AUTO]; + hdr.version_.minor_ = input[AUTO]; + + // length + byte tmp[2]; + tmp[0] = input[AUTO]; + tmp[1] = input[AUTO]; + ato16(tmp, hdr.length_); + + return input; +} + + +// output operator for RecordLayerHeader +output_buffer& operator<<(output_buffer& output, const RecordLayerHeader& hdr) +{ + output[AUTO] = hdr.type_; + output[AUTO] = hdr.version_.major_; + output[AUTO] = hdr.version_.minor_; + + // length + byte tmp[2]; + c16toa(hdr.length_, tmp); + output[AUTO] = tmp[0]; + output[AUTO] = tmp[1]; + + return output; +} + + +// virtual input operator for Messages +input_buffer& operator>>(input_buffer& input, Message& msg) +{ + return msg.set(input); +} + +// virtual output operator for Messages +output_buffer& operator<<(output_buffer& output, const Message& msg) +{ + return msg.get(output); +} + + +// input operator for HandShakeHeader +input_buffer& operator>>(input_buffer& input, HandShakeHeader& hs) +{ + hs.type_ = HandShakeType(input[AUTO]); + + hs.length_[0] = input[AUTO]; + hs.length_[1] = input[AUTO]; + hs.length_[2] = input[AUTO]; + + return input; +} + + +// output operator for HandShakeHeader +output_buffer& operator<<(output_buffer& output, const HandShakeHeader& hdr) +{ + output[AUTO] = hdr.type_; + output.write(hdr.length_, sizeof(hdr.length_)); + return output; +} + + +// HandShake Header Processing function +void HandShakeHeader::Process(input_buffer& input, SSL& ssl) +{ + ssl.verifyState(*this); + if (ssl.GetError()) return; + const HandShakeFactory& hsf = ssl.getFactory().getHandShake(); + mySTL::auto_ptr<HandShakeBase> hs(hsf.CreateObject(type_)); + if (!hs.get()) { + ssl.SetError(factory_error); + return; + } + + uint len = c24to32(length_); + if (len > input.get_remaining()) { + ssl.SetError(bad_input); + return; + } + hashHandShake(ssl, input, len); + + hs->set_length(len); + input >> *hs; + hs->Process(input, ssl); +} + + +ContentType HandShakeHeader::get_type() const +{ + return handshake; +} + + +uint16 HandShakeHeader::get_length() const +{ + return c24to32(length_); +} + + +HandShakeType HandShakeHeader::get_handshakeType() const +{ + return type_; +} + + +void HandShakeHeader::set_type(HandShakeType hst) +{ + type_ = hst; +} + + +void HandShakeHeader::set_length(uint32 u32) +{ + c32to24(u32, length_); +} + + +input_buffer& HandShakeHeader::set(input_buffer& in) +{ + return in >> *this; +} + + +output_buffer& HandShakeHeader::get(output_buffer& out) const +{ + return out << *this; +} + + + +int HandShakeBase::get_length() const +{ + return length_; +} + + +void HandShakeBase::set_length(int l) +{ + length_ = l; +} + + +// for building buffer's type field +HandShakeType HandShakeBase::get_type() const +{ + return no_shake; +} + + +input_buffer& HandShakeBase::set(input_buffer& in) +{ + return in; +} + + +output_buffer& HandShakeBase::get(output_buffer& out) const +{ + return out; +} + + +void HandShakeBase::Process(input_buffer&, SSL&) +{} + + +input_buffer& HelloRequest::set(input_buffer& in) +{ + return in; +} + + +output_buffer& HelloRequest::get(output_buffer& out) const +{ + return out; +} + + +void HelloRequest::Process(input_buffer&, SSL&) +{} + + +HandShakeType HelloRequest::get_type() const +{ + return hello_request; +} + + +// input operator for CipherSpec +input_buffer& operator>>(input_buffer& input, ChangeCipherSpec& cs) +{ + cs.type_ = CipherChoice(input[AUTO]); + return input; +} + +// output operator for CipherSpec +output_buffer& operator<<(output_buffer& output, const ChangeCipherSpec& cs) +{ + output[AUTO] = cs.type_; + return output; +} + + +ChangeCipherSpec::ChangeCipherSpec() + : type_(change_cipher_spec_choice) +{} + + +input_buffer& ChangeCipherSpec::set(input_buffer& in) +{ + return in >> *this; +} + + +output_buffer& ChangeCipherSpec::get(output_buffer& out) const +{ + return out << *this; +} + + +ContentType ChangeCipherSpec::get_type() const +{ + return change_cipher_spec; +} + + +uint16 ChangeCipherSpec::get_length() const +{ + return SIZEOF_ENUM; +} + + +// CipherSpec processing handler +void ChangeCipherSpec::Process(input_buffer&, SSL& ssl) +{ + ssl.useSecurity().use_parms().pending_ = false; + if (ssl.getSecurity().get_resuming()) { + if (ssl.getSecurity().get_parms().entity_ == client_end) + buildFinished(ssl, ssl.useHashes().use_verify(), server); // server + } + else if (ssl.getSecurity().get_parms().entity_ == server_end) + buildFinished(ssl, ssl.useHashes().use_verify(), client); // client +} + + +Alert::Alert(AlertLevel al, AlertDescription ad) + : level_(al), description_(ad) +{} + + +ContentType Alert::get_type() const +{ + return alert; +} + + +uint16 Alert::get_length() const +{ + return SIZEOF_ENUM * 2; +} + + +input_buffer& Alert::set(input_buffer& in) +{ + return in >> *this; +} + + +output_buffer& Alert::get(output_buffer& out) const +{ + return out << *this; +} + + +// input operator for Alert +input_buffer& operator>>(input_buffer& input, Alert& a) +{ + a.level_ = AlertLevel(input[AUTO]); + a.description_ = AlertDescription(input[AUTO]); + + return input; +} + + +// output operator for Alert +output_buffer& operator<<(output_buffer& output, const Alert& a) +{ + output[AUTO] = a.level_; + output[AUTO] = a.description_; + return output; +} + + +// Alert processing handler +void Alert::Process(input_buffer& input, SSL& ssl) +{ + if (ssl.getSecurity().get_parms().pending_ == false) { // encrypted alert + int aSz = get_length(); // alert size already read on input + opaque verify[SHA_LEN]; + const opaque* data = input.get_buffer() + input.get_current() - aSz; + + if (ssl.isTLS()) + TLS_hmac(ssl, verify, data, aSz, alert, true); + else + hmac(ssl, verify, data, aSz, alert, true); + + // read mac and fill + int digestSz = ssl.getCrypto().get_digest().get_digestSize(); + opaque mac[SHA_LEN]; + input.read(mac, digestSz); + + if (ssl.getSecurity().get_parms().cipher_type_ == block) { + int ivExtra = 0; + opaque fill; + + if (ssl.isTLSv1_1()) + ivExtra = ssl.getCrypto().get_cipher().get_blockSize(); + int padSz = ssl.getSecurity().get_parms().encrypt_size_ - ivExtra - + aSz - digestSz; + for (int i = 0; i < padSz; i++) + fill = input[AUTO]; + } + + // verify + if (memcmp(mac, verify, digestSz)) { + ssl.SetError(verify_error); + return; + } + } + if (level_ == fatal) { + ssl.useStates().useRecord() = recordNotReady; + ssl.useStates().useHandShake() = handShakeNotReady; + ssl.SetError(YasslError(description_)); + } +} + + +Data::Data() + : length_(0), buffer_(0), write_buffer_(0) +{} + + +Data::Data(uint16 len, opaque* b) + : length_(len), buffer_(b), write_buffer_(0) +{} + + +void Data::SetData(uint16 len, const opaque* buffer) +{ + assert(write_buffer_ == 0); + + length_ = len; + write_buffer_ = buffer; +} + +input_buffer& Data::set(input_buffer& in) +{ + return in; +} + + +output_buffer& Data::get(output_buffer& out) const +{ + return out << *this; +} + + +ContentType Data::get_type() const +{ + return application_data; +} + + +uint16 Data::get_length() const +{ + return length_; +} + + +void Data::set_length(uint16 l) +{ + length_ = l; +} + + +opaque* Data::set_buffer() +{ + return buffer_; +} + + +// output operator for Data +output_buffer& operator<<(output_buffer& output, const Data& data) +{ + output.write(data.write_buffer_, data.length_); + return output; +} + + +// Process handler for Data +void Data::Process(input_buffer& input, SSL& ssl) +{ + int msgSz = ssl.getSecurity().get_parms().encrypt_size_; + int pad = 0, padByte = 0; + int ivExtra = 0; + + if (ssl.getSecurity().get_parms().cipher_type_ == block) { + if (ssl.isTLSv1_1()) // IV + ivExtra = ssl.getCrypto().get_cipher().get_blockSize(); + pad = *(input.get_buffer() + input.get_current() + msgSz -ivExtra - 1); + padByte = 1; + } + int digestSz = ssl.getCrypto().get_digest().get_digestSize(); + int dataSz = msgSz - ivExtra - digestSz - pad - padByte; + opaque verify[SHA_LEN]; + + const byte* rawData = input.get_buffer() + input.get_current(); + + // read data + if (dataSz) { // could be compressed + if (ssl.CompressionOn()) { + input_buffer tmp; + if (DeCompress(input, dataSz, tmp) == -1) { + ssl.SetError(decompress_error); + return; + } + ssl.addData(NEW_YS input_buffer(tmp.get_size(), + tmp.get_buffer(), tmp.get_size())); + } + else { + input_buffer* data; + ssl.addData(data = NEW_YS input_buffer(dataSz)); + input.read(data->get_buffer(), dataSz); + data->add_size(dataSz); + } + + if (ssl.isTLS()) + TLS_hmac(ssl, verify, rawData, dataSz, application_data, true); + else + hmac(ssl, verify, rawData, dataSz, application_data, true); + } + + // read mac and fill + opaque mac[SHA_LEN]; + opaque fill; + input.read(mac, digestSz); + for (int i = 0; i < pad; i++) + fill = input[AUTO]; + if (padByte) + fill = input[AUTO]; + + // verify + if (dataSz) { + if (memcmp(mac, verify, digestSz)) { + ssl.SetError(verify_error); + return; + } + } + else + ssl.get_SEQIncrement(true); // even though no data, increment verify +} + + +// virtual input operator for HandShakes +input_buffer& operator>>(input_buffer& input, HandShakeBase& hs) +{ + return hs.set(input); +} + + +// virtual output operator for HandShakes +output_buffer& operator<<(output_buffer& output, const HandShakeBase& hs) +{ + return hs.get(output); +} + + +Certificate::Certificate(const x509* cert) : cert_(cert) +{ + set_length(cert_->get_length() + 2 * CERT_HEADER); // list and cert size +} + + +const opaque* Certificate::get_buffer() const +{ + return cert_->get_buffer(); +} + + +// output operator for Certificate +output_buffer& operator<<(output_buffer& output, const Certificate& cert) +{ + uint sz = cert.get_length() - 2 * CERT_HEADER; + opaque tmp[CERT_HEADER]; + + c32to24(sz + CERT_HEADER, tmp); + output.write(tmp, CERT_HEADER); + c32to24(sz, tmp); + output.write(tmp, CERT_HEADER); + output.write(cert.get_buffer(), sz); + + return output; +} + + +// certificate processing handler +void Certificate::Process(input_buffer& input, SSL& ssl) +{ + CertManager& cm = ssl.useCrypto().use_certManager(); + + uint32 list_sz; + byte tmp[3]; + + tmp[0] = input[AUTO]; + tmp[1] = input[AUTO]; + tmp[2] = input[AUTO]; + c24to32(tmp, list_sz); + + while (list_sz) { + // cert size + uint32 cert_sz; + tmp[0] = input[AUTO]; + tmp[1] = input[AUTO]; + tmp[2] = input[AUTO]; + c24to32(tmp, cert_sz); + + x509* myCert; + cm.AddPeerCert(myCert = NEW_YS x509(cert_sz)); + input.read(myCert->use_buffer(), myCert->get_length()); + + list_sz -= cert_sz + CERT_HEADER; + } + if (int err = cm.Validate()) + ssl.SetError(YasslError(err)); + else if (ssl.getSecurity().get_parms().entity_ == client_end) + ssl.useStates().useClient() = serverCertComplete; +} + + +Certificate::Certificate() + : cert_(0) +{} + + +input_buffer& Certificate::set(input_buffer& in) +{ + return in; +} + + +output_buffer& Certificate::get(output_buffer& out) const +{ + return out << *this; +} + + +HandShakeType Certificate::get_type() const +{ + return certificate; +} + + +ServerDHParams::ServerDHParams() + : pSz_(0), gSz_(0), pubSz_(0), p_(0), g_(0), Ys_(0) +{} + + +ServerDHParams::~ServerDHParams() +{ + ysArrayDelete(Ys_); + ysArrayDelete(g_); + ysArrayDelete(p_); +} + + +int ServerDHParams::get_pSize() const +{ + return pSz_; +} + + +int ServerDHParams::get_gSize() const +{ + return gSz_; +} + + +int ServerDHParams::get_pubSize() const +{ + return pubSz_; +} + + +const opaque* ServerDHParams::get_p() const +{ + return p_; +} + + +const opaque* ServerDHParams::get_g() const +{ + return g_; +} + + +const opaque* ServerDHParams::get_pub() const +{ + return Ys_; +} + + +opaque* ServerDHParams::alloc_p(int sz) +{ + p_ = NEW_YS opaque[pSz_ = sz]; + return p_; +} + + +opaque* ServerDHParams::alloc_g(int sz) +{ + g_ = NEW_YS opaque[gSz_ = sz]; + return g_; +} + + +opaque* ServerDHParams::alloc_pub(int sz) +{ + Ys_ = NEW_YS opaque[pubSz_ = sz]; + return Ys_; +} + + +int ServerKeyBase::get_length() const +{ + return 0; +} + + +opaque* ServerKeyBase::get_serverKey() const +{ + return 0; +} + + +// input operator for ServerHello +input_buffer& operator>>(input_buffer& input, ServerHello& hello) +{ + // Protocol + hello.server_version_.major_ = input[AUTO]; + hello.server_version_.minor_ = input[AUTO]; + + // Random + input.read(hello.random_, RAN_LEN); + + // Session + hello.id_len_ = input[AUTO]; + if (hello.id_len_) + input.read(hello.session_id_, hello.id_len_); + + // Suites + hello.cipher_suite_[0] = input[AUTO]; + hello.cipher_suite_[1] = input[AUTO]; + + // Compression + hello.compression_method_ = CompressionMethod(input[AUTO]); + + return input; +} + + +// output operator for ServerHello +output_buffer& operator<<(output_buffer& output, const ServerHello& hello) +{ + // Protocol + output[AUTO] = hello.server_version_.major_; + output[AUTO] = hello.server_version_.minor_; + + // Random + output.write(hello.random_, RAN_LEN); + + // Session + output[AUTO] = hello.id_len_; + output.write(hello.session_id_, ID_LEN); + + // Suites + output[AUTO] = hello.cipher_suite_[0]; + output[AUTO] = hello.cipher_suite_[1]; + + // Compression + output[AUTO] = hello.compression_method_; + + return output; +} + + +// Server Hello processing handler +void ServerHello::Process(input_buffer&, SSL& ssl) +{ + if (ssl.GetMultiProtocol()) { // SSLv23 support + if (ssl.isTLS() && server_version_.minor_ < 1) + // downgrade to SSLv3 + ssl.useSecurity().use_connection().TurnOffTLS(); + else if (ssl.isTLSv1_1() && server_version_.minor_ == 1) + // downdrage to TLSv1 + ssl.useSecurity().use_connection().TurnOffTLS1_1(); + } + else if (ssl.isTLSv1_1() && server_version_.minor_ < 2) { + ssl.SetError(badVersion_error); + return; + } + else if (ssl.isTLS() && server_version_.minor_ < 1) { + ssl.SetError(badVersion_error); + return; + } + else if (!ssl.isTLS() && (server_version_.major_ == 3 && + server_version_.minor_ >= 1)) { + ssl.SetError(badVersion_error); + return; + } + ssl.set_pending(cipher_suite_[1]); + ssl.set_random(random_, server_end); + if (id_len_) + ssl.set_sessionID(session_id_); + else + ssl.useSecurity().use_connection().sessionID_Set_ = false; + + if (ssl.getSecurity().get_resuming()) + if (memcmp(session_id_, ssl.getSecurity().get_resume().GetID(), + ID_LEN) == 0) { + ssl.set_masterSecret(ssl.getSecurity().get_resume().GetSecret()); + if (ssl.isTLS()) + ssl.deriveTLSKeys(); + else + ssl.deriveKeys(); + ssl.useStates().useClient() = serverHelloDoneComplete; + return; + } + else { + ssl.useSecurity().set_resuming(false); + ssl.useLog().Trace("server denied resumption"); + } + + if (ssl.CompressionOn() && !compression_method_) + ssl.UnSetCompression(); // server isn't supporting yaSSL zlib request + + ssl.useStates().useClient() = serverHelloComplete; +} + + +ServerHello::ServerHello() +{ + memset(random_, 0, RAN_LEN); + memset(session_id_, 0, ID_LEN); +} + + +ServerHello::ServerHello(ProtocolVersion pv, bool useCompression) + : server_version_(pv), + compression_method_(useCompression ? zlib : no_compression) +{ + memset(random_, 0, RAN_LEN); + memset(session_id_, 0, ID_LEN); +} + + +input_buffer& ServerHello::set(input_buffer& in) +{ + return in >> *this; +} + + +output_buffer& ServerHello::get(output_buffer& out) const +{ + return out << *this; +} + + +HandShakeType ServerHello::get_type() const +{ + return server_hello; +} + + +const opaque* ServerHello::get_random() const +{ + return random_; +} + + +// Server Hello Done processing handler +void ServerHelloDone::Process(input_buffer&, SSL& ssl) +{ + ssl.useStates().useClient() = serverHelloDoneComplete; +} + + +ServerHelloDone::ServerHelloDone() +{ + set_length(0); +} + + +input_buffer& ServerHelloDone::set(input_buffer& in) +{ + return in; +} + + +output_buffer& ServerHelloDone::get(output_buffer& out) const +{ + return out; +} + + +HandShakeType ServerHelloDone::get_type() const +{ + return server_hello_done; +} + + +int ClientKeyBase::get_length() const +{ + return 0; +} + + +opaque* ClientKeyBase::get_clientKey() const +{ + return 0; +} + + +// input operator for Client Hello +input_buffer& operator>>(input_buffer& input, ClientHello& hello) +{ + uint begin = input.get_current(); // could have extensions at end + + // Protocol + hello.client_version_.major_ = input[AUTO]; + hello.client_version_.minor_ = input[AUTO]; + + // Random + input.read(hello.random_, RAN_LEN); + + // Session + hello.id_len_ = input[AUTO]; + if (hello.id_len_) input.read(hello.session_id_, ID_LEN); + + // Suites + byte tmp[2]; + uint16 len; + tmp[0] = input[AUTO]; + tmp[1] = input[AUTO]; + ato16(tmp, len); + + hello.suite_len_ = min(len, static_cast<uint16>(MAX_SUITE_SZ)); + input.read(hello.cipher_suites_, hello.suite_len_); + if (len > hello.suite_len_) // ignore extra suites + input.set_current(input.get_current() + len - hello.suite_len_); + + // Compression + hello.comp_len_ = input[AUTO]; + hello.compression_methods_ = no_compression; + while (hello.comp_len_--) { + CompressionMethod cm = CompressionMethod(input[AUTO]); + if (cm == zlib) + hello.compression_methods_ = zlib; + } + + uint read = input.get_current() - begin; + uint expected = hello.get_length(); + + // ignore client hello extensions for now + if (read < expected) + input.set_current(input.get_current() + expected - read); + + return input; +} + + +// output operaotr for Client Hello +output_buffer& operator<<(output_buffer& output, const ClientHello& hello) +{ + // Protocol + output[AUTO] = hello.client_version_.major_; + output[AUTO] = hello.client_version_.minor_; + + // Random + output.write(hello.random_, RAN_LEN); + + // Session + output[AUTO] = hello.id_len_; + if (hello.id_len_) output.write(hello.session_id_, ID_LEN); + + // Suites + byte tmp[2]; + c16toa(hello.suite_len_, tmp); + output[AUTO] = tmp[0]; + output[AUTO] = tmp[1]; + output.write(hello.cipher_suites_, hello.suite_len_); + + // Compression + output[AUTO] = hello.comp_len_; + output[AUTO] = hello.compression_methods_; + + return output; +} + + +// Client Hello processing handler +void ClientHello::Process(input_buffer&, SSL& ssl) +{ + // store version for pre master secret + ssl.useSecurity().use_connection().chVersion_ = client_version_; + + if (client_version_.major_ != 3) { + ssl.SetError(badVersion_error); + return; + } + if (ssl.GetMultiProtocol()) { // SSLv23 support + if (ssl.isTLS() && client_version_.minor_ < 1) { + // downgrade to SSLv3 + ssl.useSecurity().use_connection().TurnOffTLS(); + ProtocolVersion pv = ssl.getSecurity().get_connection().version_; + bool removeDH = ssl.getSecurity().get_parms().removeDH_; + bool removeRSA = false; + bool removeDSA = false; + + const CertManager& cm = ssl.getCrypto().get_certManager(); + if (cm.get_keyType() == rsa_sa_algo) + removeDSA = true; + else + removeRSA = true; + + // reset w/ SSL suites + ssl.useSecurity().use_parms().SetSuites(pv, removeDH, removeRSA, + removeDSA); + } + else if (ssl.isTLSv1_1() && client_version_.minor_ == 1) + // downgrade to TLSv1, but use same suites + ssl.useSecurity().use_connection().TurnOffTLS1_1(); + } + else if (ssl.isTLSv1_1() && client_version_.minor_ < 2) { + ssl.SetError(badVersion_error); + return; + } + else if (ssl.isTLS() && client_version_.minor_ < 1) { + ssl.SetError(badVersion_error); + return; + } + else if (!ssl.isTLS() && client_version_.minor_ >= 1) { + ssl.SetError(badVersion_error); + return; + } + + ssl.set_random(random_, client_end); + + while (id_len_) { // trying to resume + SSL_SESSION* session = 0; + if (!ssl.getSecurity().GetContext()->GetSessionCacheOff()) + session = GetSessions().lookup(session_id_); + if (!session) { + ssl.useLog().Trace("session lookup failed"); + break; + } + ssl.set_session(session); + ssl.useSecurity().set_resuming(true); + ssl.matchSuite(session->GetSuite(), SUITE_LEN); + ssl.set_pending(ssl.getSecurity().get_parms().suite_[1]); + ssl.set_masterSecret(session->GetSecret()); + + opaque serverRandom[RAN_LEN]; + ssl.getCrypto().get_random().Fill(serverRandom, sizeof(serverRandom)); + ssl.set_random(serverRandom, server_end); + if (ssl.isTLS()) + ssl.deriveTLSKeys(); + else + ssl.deriveKeys(); + ssl.useStates().useServer() = clientKeyExchangeComplete; + return; + } + ssl.matchSuite(cipher_suites_, suite_len_); + if (ssl.GetError()) return; + ssl.set_pending(ssl.getSecurity().get_parms().suite_[1]); + + if (compression_methods_ == zlib) + ssl.SetCompression(); + + ssl.useStates().useServer() = clientHelloComplete; +} + + +input_buffer& ClientHello::set(input_buffer& in) +{ + return in >> *this; +} + + +output_buffer& ClientHello::get(output_buffer& out) const +{ + return out << *this; +} + + +HandShakeType ClientHello::get_type() const +{ + return client_hello; +} + + +const opaque* ClientHello::get_random() const +{ + return random_; +} + + +ClientHello::ClientHello() +{ + memset(random_, 0, RAN_LEN); +} + + +ClientHello::ClientHello(ProtocolVersion pv, bool useCompression) + : client_version_(pv), + compression_methods_(useCompression ? zlib : no_compression) +{ + memset(random_, 0, RAN_LEN); +} + + +// output operator for ServerKeyExchange +output_buffer& operator<<(output_buffer& output, const ServerKeyExchange& sk) +{ + output.write(sk.getKey(), sk.getKeyLength()); + return output; +} + + +// Server Key Exchange processing handler +void ServerKeyExchange::Process(input_buffer& input, SSL& ssl) +{ + createKey(ssl); + if (ssl.GetError()) return; + server_key_->read(ssl, input); + + ssl.useStates().useClient() = serverKeyExchangeComplete; +} + + +ServerKeyExchange::ServerKeyExchange(SSL& ssl) +{ + createKey(ssl); +} + + +ServerKeyExchange::ServerKeyExchange() + : server_key_(0) +{} + + +ServerKeyExchange::~ServerKeyExchange() +{ + ysDelete(server_key_); +} + + +void ServerKeyExchange::build(SSL& ssl) +{ + server_key_->build(ssl); + set_length(server_key_->get_length()); +} + + +const opaque* ServerKeyExchange::getKey() const +{ + return server_key_->get_serverKey(); +} + + +int ServerKeyExchange::getKeyLength() const +{ + return server_key_->get_length(); +} + + +input_buffer& ServerKeyExchange::set(input_buffer& in) +{ + return in; // process does +} + + +output_buffer& ServerKeyExchange::get(output_buffer& out) const +{ + return out << *this; +} + + +HandShakeType ServerKeyExchange::get_type() const +{ + return server_key_exchange; +} + + +// CertificateRequest +CertificateRequest::CertificateRequest() + : typeTotal_(0) +{ + memset(certificate_types_, 0, sizeof(certificate_types_)); +} + + +CertificateRequest::~CertificateRequest() +{ + + STL::for_each(certificate_authorities_.begin(), + certificate_authorities_.end(), + del_ptr_zero()) ; +} + + +void CertificateRequest::Build() +{ + certificate_types_[0] = rsa_sign; + certificate_types_[1] = dss_sign; + + typeTotal_ = 2; + + uint16 authCount = 0; + uint16 authSz = 0; + + for (int j = 0; j < authCount; j++) { + int sz = REQUEST_HEADER + MIN_DIS_SIZE; + DistinguishedName dn; + certificate_authorities_.push_back(dn = NEW_YS byte[sz]); + + opaque tmp[REQUEST_HEADER]; + c16toa(MIN_DIS_SIZE, tmp); + memcpy(dn, tmp, sizeof(tmp)); + + // fill w/ junk for now + memcpy(dn, tmp, MIN_DIS_SIZE); + authSz += sz; + } + + set_length(SIZEOF_ENUM + typeTotal_ + REQUEST_HEADER + authSz); +} + + +input_buffer& CertificateRequest::set(input_buffer& in) +{ + return in >> *this; +} + + +output_buffer& CertificateRequest::get(output_buffer& out) const +{ + return out << *this; +} + + +// input operator for CertificateRequest +input_buffer& operator>>(input_buffer& input, CertificateRequest& request) +{ + // types + request.typeTotal_ = input[AUTO]; + for (int i = 0; i < request.typeTotal_; i++) + request.certificate_types_[i] = ClientCertificateType(input[AUTO]); + + byte tmp[REQUEST_HEADER]; + input.read(tmp, sizeof(tmp)); + uint16 sz; + ato16(tmp, sz); + + // authorities + while (sz) { + uint16 dnSz; + input.read(tmp, sizeof(tmp)); + ato16(tmp, dnSz); + + DistinguishedName dn; + request.certificate_authorities_.push_back(dn = NEW_YS + byte[REQUEST_HEADER + dnSz]); + memcpy(dn, tmp, REQUEST_HEADER); + input.read(&dn[REQUEST_HEADER], dnSz); + + sz -= dnSz + REQUEST_HEADER; + } + + return input; +} + + +// output operator for CertificateRequest +output_buffer& operator<<(output_buffer& output, + const CertificateRequest& request) +{ + // types + output[AUTO] = request.typeTotal_; + for (int i = 0; i < request.typeTotal_; i++) + output[AUTO] = request.certificate_types_[i]; + + // authorities + opaque tmp[REQUEST_HEADER]; + c16toa(request.get_length() - SIZEOF_ENUM - + request.typeTotal_ - REQUEST_HEADER, tmp); + output.write(tmp, sizeof(tmp)); + + STL::list<DistinguishedName>::const_iterator first = + request.certificate_authorities_.begin(); + STL::list<DistinguishedName>::const_iterator last = + request.certificate_authorities_.end(); + while (first != last) { + uint16 sz; + ato16(*first, sz); + output.write(*first, sz + REQUEST_HEADER); + + ++first; + } + + return output; +} + + +// CertificateRequest processing handler +void CertificateRequest::Process(input_buffer&, SSL& ssl) +{ + CertManager& cm = ssl.useCrypto().use_certManager(); + + // make sure user provided cert and key before sending and using + if (cm.get_cert() && cm.get_privateKey()) + cm.setSendVerify(); +} + + +HandShakeType CertificateRequest::get_type() const +{ + return certificate_request; +} + + +// CertificateVerify +CertificateVerify::CertificateVerify() : signature_(0) +{} + + +CertificateVerify::~CertificateVerify() +{ + ysArrayDelete(signature_); +} + + +void CertificateVerify::Build(SSL& ssl) +{ + build_certHashes(ssl, hashes_); + + uint16 sz = 0; + byte len[VERIFY_HEADER]; + mySTL::auto_array<byte> sig; + + // sign + const CertManager& cert = ssl.getCrypto().get_certManager(); + if (cert.get_keyType() == rsa_sa_algo) { + RSA rsa(cert.get_privateKey(), cert.get_privateKeyLength(), false); + + sz = rsa.get_cipherLength() + VERIFY_HEADER; + sig.reset(NEW_YS byte[sz]); + + c16toa(sz - VERIFY_HEADER, len); + memcpy(sig.get(), len, VERIFY_HEADER); + rsa.sign(sig.get() + VERIFY_HEADER, hashes_.md5_, sizeof(Hashes), + ssl.getCrypto().get_random()); + } + else { // DSA + DSS dss(cert.get_privateKey(), cert.get_privateKeyLength(), false); + + sz = DSS_SIG_SZ + DSS_ENCODED_EXTRA + VERIFY_HEADER; + sig.reset(NEW_YS byte[sz]); + + c16toa(sz - VERIFY_HEADER, len); + memcpy(sig.get(), len, VERIFY_HEADER); + dss.sign(sig.get() + VERIFY_HEADER, hashes_.sha_, SHA_LEN, + ssl.getCrypto().get_random()); + + byte encoded[DSS_SIG_SZ + DSS_ENCODED_EXTRA]; + TaoCrypt::EncodeDSA_Signature(sig.get() + VERIFY_HEADER, encoded); + memcpy(sig.get() + VERIFY_HEADER, encoded, sizeof(encoded)); + } + set_length(sz); + signature_ = sig.release(); +} + + +input_buffer& CertificateVerify::set(input_buffer& in) +{ + return in >> *this; +} + + +output_buffer& CertificateVerify::get(output_buffer& out) const +{ + return out << *this; +} + + +// input operator for CertificateVerify +input_buffer& operator>>(input_buffer& input, CertificateVerify& request) +{ + byte tmp[VERIFY_HEADER]; + input.read(tmp, sizeof(tmp)); + + uint16 sz = 0; + ato16(tmp, sz); + request.set_length(sz); + + request.signature_ = NEW_YS byte[sz]; + input.read(request.signature_, sz); + + return input; +} + + +// output operator for CertificateVerify +output_buffer& operator<<(output_buffer& output, + const CertificateVerify& verify) +{ + output.write(verify.signature_, verify.get_length()); + + return output; +} + + +// CertificateVerify processing handler +void CertificateVerify::Process(input_buffer&, SSL& ssl) +{ + const Hashes& hashVerify = ssl.getHashes().get_certVerify(); + const CertManager& cert = ssl.getCrypto().get_certManager(); + + if (cert.get_peerKeyType() == rsa_sa_algo) { + RSA rsa(cert.get_peerKey(), cert.get_peerKeyLength()); + + if (!rsa.verify(hashVerify.md5_, sizeof(hashVerify), signature_, + get_length())) + ssl.SetError(verify_error); + } + else { // DSA + byte decodedSig[DSS_SIG_SZ]; + TaoCrypt::DecodeDSA_Signature(decodedSig, signature_, get_length()); + + DSS dss(cert.get_peerKey(), cert.get_peerKeyLength()); + if (!dss.verify(hashVerify.sha_, SHA_LEN, decodedSig, get_length())) + ssl.SetError(verify_error); + } +} + + +HandShakeType CertificateVerify::get_type() const +{ + return certificate_verify; +} + + +// output operator for ClientKeyExchange +output_buffer& operator<<(output_buffer& output, const ClientKeyExchange& ck) +{ + output.write(ck.getKey(), ck.getKeyLength()); + return output; +} + + +// Client Key Exchange processing handler +void ClientKeyExchange::Process(input_buffer& input, SSL& ssl) +{ + createKey(ssl); + if (ssl.GetError()) return; + client_key_->read(ssl, input); + + if (ssl.getCrypto().get_certManager().verifyPeer()) + build_certHashes(ssl, ssl.useHashes().use_certVerify()); + + ssl.useStates().useServer() = clientKeyExchangeComplete; +} + + +ClientKeyExchange::ClientKeyExchange(SSL& ssl) +{ + createKey(ssl); +} + + +ClientKeyExchange::ClientKeyExchange() + : client_key_(0) +{} + + +ClientKeyExchange::~ClientKeyExchange() +{ + ysDelete(client_key_); +} + + +void ClientKeyExchange::build(SSL& ssl) +{ + client_key_->build(ssl); + set_length(client_key_->get_length()); +} + +const opaque* ClientKeyExchange::getKey() const +{ + return client_key_->get_clientKey(); +} + + +int ClientKeyExchange::getKeyLength() const +{ + return client_key_->get_length(); +} + + +input_buffer& ClientKeyExchange::set(input_buffer& in) +{ + return in; +} + + +output_buffer& ClientKeyExchange::get(output_buffer& out) const +{ + return out << *this; +} + + +HandShakeType ClientKeyExchange::get_type() const +{ + return client_key_exchange; +} + + +// input operator for Finished +input_buffer& operator>>(input_buffer& input, Finished&) +{ + /* do in process */ + + return input; +} + +// output operator for Finished +output_buffer& operator<<(output_buffer& output, const Finished& fin) +{ + if (fin.get_length() == FINISHED_SZ) { + output.write(fin.hashes_.md5_, MD5_LEN); + output.write(fin.hashes_.sha_, SHA_LEN); + } + else // TLS_FINISHED_SZ + output.write(fin.hashes_.md5_, TLS_FINISHED_SZ); + + return output; +} + + +// Finished processing handler +void Finished::Process(input_buffer& input, SSL& ssl) +{ + // verify hashes + const Finished& verify = ssl.getHashes().get_verify(); + uint finishedSz = ssl.isTLS() ? TLS_FINISHED_SZ : FINISHED_SZ; + + input.read(hashes_.md5_, finishedSz); + + if (memcmp(&hashes_, &verify.hashes_, finishedSz)) { + ssl.SetError(verify_error); + return; + } + + // read verify mac + opaque verifyMAC[SHA_LEN]; + uint macSz = finishedSz + HANDSHAKE_HEADER; + + if (ssl.isTLS()) + TLS_hmac(ssl, verifyMAC, input.get_buffer() + input.get_current() + - macSz, macSz, handshake, true); + else + hmac(ssl, verifyMAC, input.get_buffer() + input.get_current() - macSz, + macSz, handshake, true); + + // read mac and fill + opaque mac[SHA_LEN]; // max size + int digestSz = ssl.getCrypto().get_digest().get_digestSize(); + input.read(mac, digestSz); + + uint ivExtra = 0; + if (ssl.getSecurity().get_parms().cipher_type_ == block) + if (ssl.isTLSv1_1()) + ivExtra = ssl.getCrypto().get_cipher().get_blockSize(); + + opaque fill; + int padSz = ssl.getSecurity().get_parms().encrypt_size_ - ivExtra - + HANDSHAKE_HEADER - finishedSz - digestSz; + for (int i = 0; i < padSz; i++) + fill = input[AUTO]; + + // verify mac + if (memcmp(mac, verifyMAC, digestSz)) { + ssl.SetError(verify_error); + return; + } + + // update states + ssl.useStates().useHandShake() = handShakeReady; + if (ssl.getSecurity().get_parms().entity_ == client_end) + ssl.useStates().useClient() = serverFinishedComplete; + else + ssl.useStates().useServer() = clientFinishedComplete; +} + + +Finished::Finished() +{ + set_length(FINISHED_SZ); +} + + +uint8* Finished::set_md5() +{ + return hashes_.md5_; +} + + +uint8* Finished::set_sha() +{ + return hashes_.sha_; +} + + +input_buffer& Finished::set(input_buffer& in) +{ + return in >> *this; +} + + +output_buffer& Finished::get(output_buffer& out) const +{ + return out << *this; +} + + +HandShakeType Finished::get_type() const +{ + return finished; +} + + +void clean(volatile opaque* p, uint sz, RandomPool& ran) +{ + uint i(0); + + for (i = 0; i < sz; ++i) + p[i] = 0; + + ran.Fill(const_cast<opaque*>(p), sz); + + for (i = 0; i < sz; ++i) + p[i] = 0; +} + + + +Connection::Connection(ProtocolVersion v, RandomPool& ran) + : pre_master_secret_(0), sequence_number_(0), peer_sequence_number_(0), + pre_secret_len_(0), send_server_key_(false), master_clean_(false), + TLS_(v.major_ >= 3 && v.minor_ >= 1), + TLSv1_1_(v.major_ >= 3 && v.minor_ >= 2), compression_(false), + version_(v), random_(ran) +{ + memset(sessionID_, 0, sizeof(sessionID_)); +} + + +Connection::~Connection() +{ + CleanMaster(); CleanPreMaster(); ysArrayDelete(pre_master_secret_); +} + + +void Connection::AllocPreSecret(uint sz) +{ + pre_master_secret_ = NEW_YS opaque[pre_secret_len_ = sz]; +} + + +void Connection::TurnOffTLS() +{ + TLS_ = false; + version_.minor_ = 0; +} + + +void Connection::TurnOffTLS1_1() +{ + TLSv1_1_ = false; + version_.minor_ = 1; +} + + +// wipeout master secret +void Connection::CleanMaster() +{ + if (!master_clean_) { + volatile opaque* p = master_secret_; + clean(p, SECRET_LEN, random_); + master_clean_ = true; + } +} + + +// wipeout pre master secret +void Connection::CleanPreMaster() +{ + if (pre_master_secret_) { + volatile opaque* p = pre_master_secret_; + clean(p, pre_secret_len_, random_); + + ysArrayDelete(pre_master_secret_); + pre_master_secret_ = 0; + } +} + + +// Create functions for message factory +Message* CreateCipherSpec() { return NEW_YS ChangeCipherSpec; } +Message* CreateAlert() { return NEW_YS Alert; } +Message* CreateHandShake() { return NEW_YS HandShakeHeader; } +Message* CreateData() { return NEW_YS Data; } + +// Create functions for handshake factory +HandShakeBase* CreateHelloRequest() { return NEW_YS HelloRequest; } +HandShakeBase* CreateClientHello() { return NEW_YS ClientHello; } +HandShakeBase* CreateServerHello() { return NEW_YS ServerHello; } +HandShakeBase* CreateCertificate() { return NEW_YS Certificate; } +HandShakeBase* CreateServerKeyExchange() { return NEW_YS ServerKeyExchange;} +HandShakeBase* CreateCertificateRequest() { return NEW_YS + CertificateRequest; } +HandShakeBase* CreateServerHelloDone() { return NEW_YS ServerHelloDone; } +HandShakeBase* CreateCertificateVerify() { return NEW_YS CertificateVerify;} +HandShakeBase* CreateClientKeyExchange() { return NEW_YS ClientKeyExchange;} +HandShakeBase* CreateFinished() { return NEW_YS Finished; } + +// Create functions for server key exchange factory +ServerKeyBase* CreateRSAServerKEA() { return NEW_YS RSA_Server; } +ServerKeyBase* CreateDHServerKEA() { return NEW_YS DH_Server; } +ServerKeyBase* CreateFortezzaServerKEA() { return NEW_YS Fortezza_Server; } + +// Create functions for client key exchange factory +ClientKeyBase* CreateRSAClient() { return NEW_YS + EncryptedPreMasterSecret; } +ClientKeyBase* CreateDHClient() { return NEW_YS + ClientDiffieHellmanPublic; } +ClientKeyBase* CreateFortezzaClient() { return NEW_YS FortezzaKeys; } + + +// Constructor calls this to Register compile time callbacks +void InitMessageFactory(MessageFactory& mf) +{ + mf.Reserve(4); + mf.Register(alert, CreateAlert); + mf.Register(change_cipher_spec, CreateCipherSpec); + mf.Register(handshake, CreateHandShake); + mf.Register(application_data, CreateData); +} + + +// Constructor calls this to Register compile time callbacks +void InitHandShakeFactory(HandShakeFactory& hsf) +{ + hsf.Reserve(10); + hsf.Register(hello_request, CreateHelloRequest); + hsf.Register(client_hello, CreateClientHello); + hsf.Register(server_hello, CreateServerHello); + hsf.Register(certificate, CreateCertificate); + hsf.Register(server_key_exchange, CreateServerKeyExchange); + hsf.Register(certificate_request, CreateCertificateRequest); + hsf.Register(server_hello_done, CreateServerHelloDone); + hsf.Register(certificate_verify, CreateCertificateVerify); + hsf.Register(client_key_exchange, CreateClientKeyExchange); + hsf.Register(finished, CreateFinished); +} + + +// Constructor calls this to Register compile time callbacks +void InitServerKeyFactory(ServerKeyFactory& skf) +{ + skf.Reserve(3); + skf.Register(rsa_kea, CreateRSAServerKEA); + skf.Register(diffie_hellman_kea, CreateDHServerKEA); + skf.Register(fortezza_kea, CreateFortezzaServerKEA); +} + + +// Constructor calls this to Register compile time callbacks +void InitClientKeyFactory(ClientKeyFactory& ckf) +{ + ckf.Reserve(3); + ckf.Register(rsa_kea, CreateRSAClient); + ckf.Register(diffie_hellman_kea, CreateDHClient); + ckf.Register(fortezza_kea, CreateFortezzaClient); +} + + +} // namespace diff --git a/externals/mysql/extlib/yassl/src/yassl_int.cpp b/externals/mysql/extlib/yassl/src/yassl_int.cpp new file mode 100644 index 00000000000..b7f91d72166 --- /dev/null +++ b/externals/mysql/extlib/yassl/src/yassl_int.cpp @@ -0,0 +1,2580 @@ +/* + 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 source implements SSL supporting types not specified in the + * draft along with type conversion functions. + */ + +#include "runtime.hpp" +#include "yassl_int.hpp" +#include "handshake.hpp" +#include "timer.hpp" + +#ifdef _POSIX_THREADS + #include "pthread.h" +#endif + + +#ifdef HAVE_LIBZ + #include "zlib.h" +#endif + + +#ifdef YASSL_PURE_C + + void* operator new(size_t sz, yaSSL::new_t) + { + void* ptr = malloc(sz ? sz : 1); + if (!ptr) abort(); + + return ptr; + } + + + void operator delete(void* ptr, yaSSL::new_t) + { + if (ptr) free(ptr); + } + + + void* operator new[](size_t sz, yaSSL::new_t nt) + { + return ::operator new(sz, nt); + } + + + void operator delete[](void* ptr, yaSSL::new_t nt) + { + ::operator delete(ptr, nt); + } + + namespace yaSSL { + + new_t ys; // for yaSSL library new + + } + +#endif // YASSL_PURE_C + + +namespace yaSSL { + + + + + + +// convert a 32 bit integer into a 24 bit one +void c32to24(uint32 u32, uint24& u24) +{ + u24[0] = (u32 >> 16) & 0xff; + u24[1] = (u32 >> 8) & 0xff; + u24[2] = u32 & 0xff; +} + + +// convert a 24 bit integer into a 32 bit one +void c24to32(const uint24 u24, uint32& u32) +{ + u32 = 0; + u32 = (u24[0] << 16) | (u24[1] << 8) | u24[2]; +} + + +// convert with return for ease of use +uint32 c24to32(const uint24 u24) +{ + uint32 ret; + c24to32(u24, ret); + + return ret; +} + + +// using a for opaque since underlying type is unsgined char and o is not a +// good leading identifier + +// convert opaque to 16 bit integer +void ato16(const opaque* c, uint16& u16) +{ + u16 = 0; + u16 = (c[0] << 8) | (c[1]); +} + + +// convert (copy) opaque to 24 bit integer +void ato24(const opaque* c, uint24& u24) +{ + u24[0] = c[0]; + u24[1] = c[1]; + u24[2] = c[2]; +} + + +// convert 16 bit integer to opaque +void c16toa(uint16 u16, opaque* c) +{ + c[0] = (u16 >> 8) & 0xff; + c[1] = u16 & 0xff; +} + + +// convert 24 bit integer to opaque +void c24toa(const uint24 u24, opaque* c) +{ + c[0] = u24[0]; + c[1] = u24[1]; + c[2] = u24[2]; +} + + +// convert 32 bit integer to opaque +void c32toa(uint32 u32, opaque* c) +{ + c[0] = (u32 >> 24) & 0xff; + c[1] = (u32 >> 16) & 0xff; + c[2] = (u32 >> 8) & 0xff; + c[3] = u32 & 0xff; +} + + +States::States() : recordLayer_(recordReady), handshakeLayer_(preHandshake), + clientState_(serverNull), serverState_(clientNull), + connectState_(CONNECT_BEGIN), acceptState_(ACCEPT_BEGIN), + what_(no_error) {} + +const RecordLayerState& States::getRecord() const +{ + return recordLayer_; +} + + +const HandShakeState& States::getHandShake() const +{ + return handshakeLayer_; +} + + +const ClientState& States::getClient() const +{ + return clientState_; +} + + +const ServerState& States::getServer() const +{ + return serverState_; +} + + +const ConnectState& States::GetConnect() const +{ + return connectState_; +} + + +const AcceptState& States::GetAccept() const +{ + return acceptState_; +} + + +const char* States::getString() const +{ + return errorString_; +} + + +YasslError States::What() const +{ + return what_; +} + + +RecordLayerState& States::useRecord() +{ + return recordLayer_; +} + + +HandShakeState& States::useHandShake() +{ + return handshakeLayer_; +} + + +ClientState& States::useClient() +{ + return clientState_; +} + + +ServerState& States::useServer() +{ + return serverState_; +} + + +ConnectState& States::UseConnect() +{ + return connectState_; +} + + +AcceptState& States::UseAccept() +{ + return acceptState_; +} + + +char* States::useString() +{ + return errorString_; +} + + +void States::SetError(YasslError ye) +{ + what_ = ye; +} + + +sslFactory::sslFactory() : + messageFactory_(InitMessageFactory), + handShakeFactory_(InitHandShakeFactory), + serverKeyFactory_(InitServerKeyFactory), + clientKeyFactory_(InitClientKeyFactory) +{} + + +const MessageFactory& sslFactory::getMessage() const +{ + return messageFactory_; +} + + +const HandShakeFactory& sslFactory::getHandShake() const +{ + return handShakeFactory_; +} + + +const ServerKeyFactory& sslFactory::getServerKey() const +{ + return serverKeyFactory_; +} + + +const ClientKeyFactory& sslFactory::getClientKey() const +{ + return clientKeyFactory_; +} + + +// extract context parameters and store +SSL::SSL(SSL_CTX* ctx) + : secure_(ctx->getMethod()->getVersion(), crypto_.use_random(), + ctx->getMethod()->getSide(), ctx->GetCiphers(), ctx, + ctx->GetDH_Parms().set_), quietShutdown_(false), has_data_(false) +{ + if (int err = crypto_.get_random().GetError()) { + SetError(YasslError(err)); + return; + } + + CertManager& cm = crypto_.use_certManager(); + cm.CopySelfCert(ctx->getCert()); + + bool serverSide = secure_.use_parms().entity_ == server_end; + + if (ctx->getKey()) { + if (int err = cm.SetPrivateKey(*ctx->getKey())) { + SetError(YasslError(err)); + return; + } + else if (serverSide) { + // remove RSA or DSA suites depending on cert key type + ProtocolVersion pv = secure_.get_connection().version_; + + bool removeDH = secure_.use_parms().removeDH_; + bool removeRSA = false; + bool removeDSA = false; + + if (cm.get_keyType() == rsa_sa_algo) + removeDSA = true; + else + removeRSA = true; + secure_.use_parms().SetSuites(pv, removeDH, removeRSA, removeDSA); + } + } + else if (serverSide) { + SetError(no_key_file); + return; + } + + if (ctx->getMethod()->verifyPeer()) + cm.setVerifyPeer(); + if (ctx->getMethod()->verifyNone()) + cm.setVerifyNone(); + if (ctx->getMethod()->failNoCert()) + cm.setFailNoCert(); + cm.setVerifyCallback(ctx->getVerifyCallback()); + + if (serverSide) + crypto_.SetDH(ctx->GetDH_Parms()); + + const SSL_CTX::CertList& ca = ctx->GetCA_List(); + SSL_CTX::CertList::const_iterator first(ca.begin()); + SSL_CTX::CertList::const_iterator last(ca.end()); + + while (first != last) { + if (int err = cm.CopyCaCert(*first)) { + SetError(YasslError(err)); + return; + } + ++first; + } +} + + +// store pending security parameters from Server Hello +void SSL::set_pending(Cipher suite) +{ + Parameters& parms = secure_.use_parms(); + + switch (suite) { + + case TLS_RSA_WITH_AES_256_CBC_SHA: + parms.bulk_cipher_algorithm_ = aes; + parms.mac_algorithm_ = sha; + parms.kea_ = rsa_kea; + parms.hash_size_ = SHA_LEN; + parms.key_size_ = AES_256_KEY_SZ; + parms.iv_size_ = AES_BLOCK_SZ; + parms.cipher_type_ = block; + crypto_.setDigest(NEW_YS SHA); + crypto_.setCipher(NEW_YS AES(AES_256_KEY_SZ)); + strncpy(parms.cipher_name_, cipher_names[TLS_RSA_WITH_AES_256_CBC_SHA], + MAX_SUITE_NAME); + break; + + case TLS_RSA_WITH_AES_128_CBC_SHA: + parms.bulk_cipher_algorithm_ = aes; + parms.mac_algorithm_ = sha; + parms.kea_ = rsa_kea; + parms.hash_size_ = SHA_LEN; + parms.key_size_ = AES_128_KEY_SZ; + parms.iv_size_ = AES_BLOCK_SZ; + parms.cipher_type_ = block; + crypto_.setDigest(NEW_YS SHA); + crypto_.setCipher(NEW_YS AES); + strncpy(parms.cipher_name_, cipher_names[TLS_RSA_WITH_AES_128_CBC_SHA], + MAX_SUITE_NAME); + break; + + case SSL_RSA_WITH_3DES_EDE_CBC_SHA: + parms.bulk_cipher_algorithm_ = triple_des; + parms.mac_algorithm_ = sha; + parms.kea_ = rsa_kea; + parms.hash_size_ = SHA_LEN; + parms.key_size_ = DES_EDE_KEY_SZ; + parms.iv_size_ = DES_IV_SZ; + parms.cipher_type_ = block; + crypto_.setDigest(NEW_YS SHA); + crypto_.setCipher(NEW_YS DES_EDE); + strncpy(parms.cipher_name_, cipher_names[SSL_RSA_WITH_3DES_EDE_CBC_SHA] + , MAX_SUITE_NAME); + break; + + case SSL_RSA_WITH_DES_CBC_SHA: + parms.bulk_cipher_algorithm_ = des; + parms.mac_algorithm_ = sha; + parms.kea_ = rsa_kea; + parms.hash_size_ = SHA_LEN; + parms.key_size_ = DES_KEY_SZ; + parms.iv_size_ = DES_IV_SZ; + parms.cipher_type_ = block; + crypto_.setDigest(NEW_YS SHA); + crypto_.setCipher(NEW_YS DES); + strncpy(parms.cipher_name_, cipher_names[SSL_RSA_WITH_DES_CBC_SHA], + MAX_SUITE_NAME); + break; + + case SSL_RSA_WITH_RC4_128_SHA: + parms.bulk_cipher_algorithm_ = rc4; + parms.mac_algorithm_ = sha; + parms.kea_ = rsa_kea; + parms.hash_size_ = SHA_LEN; + parms.key_size_ = RC4_KEY_SZ; + parms.iv_size_ = 0; + parms.cipher_type_ = stream; + crypto_.setDigest(NEW_YS SHA); + crypto_.setCipher(NEW_YS RC4); + strncpy(parms.cipher_name_, cipher_names[SSL_RSA_WITH_RC4_128_SHA], + MAX_SUITE_NAME); + break; + + case SSL_RSA_WITH_RC4_128_MD5: + parms.bulk_cipher_algorithm_ = rc4; + parms.mac_algorithm_ = md5; + parms.kea_ = rsa_kea; + parms.hash_size_ = MD5_LEN; + parms.key_size_ = RC4_KEY_SZ; + parms.iv_size_ = 0; + parms.cipher_type_ = stream; + crypto_.setDigest(NEW_YS MD5); + crypto_.setCipher(NEW_YS RC4); + strncpy(parms.cipher_name_, cipher_names[SSL_RSA_WITH_RC4_128_MD5], + MAX_SUITE_NAME); + break; + + case SSL_DHE_RSA_WITH_DES_CBC_SHA: + parms.bulk_cipher_algorithm_ = des; + parms.mac_algorithm_ = sha; + parms.kea_ = diffie_hellman_kea; + parms.sig_algo_ = rsa_sa_algo; + parms.hash_size_ = SHA_LEN; + parms.key_size_ = DES_KEY_SZ; + parms.iv_size_ = DES_IV_SZ; + parms.cipher_type_ = block; + secure_.use_connection().send_server_key_ = true; // eph + crypto_.setDigest(NEW_YS SHA); + crypto_.setCipher(NEW_YS DES); + strncpy(parms.cipher_name_, cipher_names[SSL_DHE_RSA_WITH_DES_CBC_SHA], + MAX_SUITE_NAME); + break; + + case SSL_DHE_RSA_WITH_3DES_EDE_CBC_SHA: + parms.bulk_cipher_algorithm_ = triple_des; + parms.mac_algorithm_ = sha; + parms.kea_ = diffie_hellman_kea; + parms.sig_algo_ = rsa_sa_algo; + parms.hash_size_ = SHA_LEN; + parms.key_size_ = DES_EDE_KEY_SZ; + parms.iv_size_ = DES_IV_SZ; + parms.cipher_type_ = block; + secure_.use_connection().send_server_key_ = true; // eph + crypto_.setDigest(NEW_YS SHA); + crypto_.setCipher(NEW_YS DES_EDE); + strncpy(parms.cipher_name_, + cipher_names[SSL_DHE_RSA_WITH_3DES_EDE_CBC_SHA], MAX_SUITE_NAME); + break; + + case TLS_DHE_RSA_WITH_AES_256_CBC_SHA: + parms.bulk_cipher_algorithm_ = aes; + parms.mac_algorithm_ = sha; + parms.kea_ = diffie_hellman_kea; + parms.sig_algo_ = rsa_sa_algo; + parms.hash_size_ = SHA_LEN; + parms.key_size_ = AES_256_KEY_SZ; + parms.iv_size_ = AES_BLOCK_SZ; + parms.cipher_type_ = block; + secure_.use_connection().send_server_key_ = true; // eph + crypto_.setDigest(NEW_YS SHA); + crypto_.setCipher(NEW_YS AES(AES_256_KEY_SZ)); + strncpy(parms.cipher_name_, + cipher_names[TLS_DHE_RSA_WITH_AES_256_CBC_SHA], MAX_SUITE_NAME); + break; + + case TLS_DHE_RSA_WITH_AES_128_CBC_SHA: + parms.bulk_cipher_algorithm_ = aes; + parms.mac_algorithm_ = sha; + parms.kea_ = diffie_hellman_kea; + parms.sig_algo_ = rsa_sa_algo; + parms.hash_size_ = SHA_LEN; + parms.key_size_ = AES_128_KEY_SZ; + parms.iv_size_ = AES_BLOCK_SZ; + parms.cipher_type_ = block; + secure_.use_connection().send_server_key_ = true; // eph + crypto_.setDigest(NEW_YS SHA); + crypto_.setCipher(NEW_YS AES); + strncpy(parms.cipher_name_, + cipher_names[TLS_DHE_RSA_WITH_AES_128_CBC_SHA], MAX_SUITE_NAME); + break; + + case SSL_DHE_DSS_WITH_DES_CBC_SHA: + parms.bulk_cipher_algorithm_ = des; + parms.mac_algorithm_ = sha; + parms.kea_ = diffie_hellman_kea; + parms.sig_algo_ = dsa_sa_algo; + parms.hash_size_ = SHA_LEN; + parms.key_size_ = DES_KEY_SZ; + parms.iv_size_ = DES_IV_SZ; + parms.cipher_type_ = block; + secure_.use_connection().send_server_key_ = true; // eph + crypto_.setDigest(NEW_YS SHA); + crypto_.setCipher(NEW_YS DES); + strncpy(parms.cipher_name_, cipher_names[SSL_DHE_DSS_WITH_DES_CBC_SHA], + MAX_SUITE_NAME); + break; + + case SSL_DHE_DSS_WITH_3DES_EDE_CBC_SHA: + parms.bulk_cipher_algorithm_ = triple_des; + parms.mac_algorithm_ = sha; + parms.kea_ = diffie_hellman_kea; + parms.sig_algo_ = dsa_sa_algo; + parms.hash_size_ = SHA_LEN; + parms.key_size_ = DES_EDE_KEY_SZ; + parms.iv_size_ = DES_IV_SZ; + parms.cipher_type_ = block; + secure_.use_connection().send_server_key_ = true; // eph + crypto_.setDigest(NEW_YS SHA); + crypto_.setCipher(NEW_YS DES_EDE); + strncpy(parms.cipher_name_, + cipher_names[SSL_DHE_DSS_WITH_3DES_EDE_CBC_SHA], MAX_SUITE_NAME); + break; + + case TLS_DHE_DSS_WITH_AES_256_CBC_SHA: + parms.bulk_cipher_algorithm_ = aes; + parms.mac_algorithm_ = sha; + parms.kea_ = diffie_hellman_kea; + parms.sig_algo_ = dsa_sa_algo; + parms.hash_size_ = SHA_LEN; + parms.key_size_ = AES_256_KEY_SZ; + parms.iv_size_ = AES_BLOCK_SZ; + parms.cipher_type_ = block; + secure_.use_connection().send_server_key_ = true; // eph + crypto_.setDigest(NEW_YS SHA); + crypto_.setCipher(NEW_YS AES(AES_256_KEY_SZ)); + strncpy(parms.cipher_name_, + cipher_names[TLS_DHE_DSS_WITH_AES_256_CBC_SHA], MAX_SUITE_NAME); + break; + + case TLS_DHE_DSS_WITH_AES_128_CBC_SHA: + parms.bulk_cipher_algorithm_ = aes; + parms.mac_algorithm_ = sha; + parms.kea_ = diffie_hellman_kea; + parms.sig_algo_ = dsa_sa_algo; + parms.hash_size_ = SHA_LEN; + parms.key_size_ = AES_128_KEY_SZ; + parms.iv_size_ = AES_BLOCK_SZ; + parms.cipher_type_ = block; + secure_.use_connection().send_server_key_ = true; // eph + crypto_.setDigest(NEW_YS SHA); + crypto_.setCipher(NEW_YS AES); + strncpy(parms.cipher_name_, + cipher_names[TLS_DHE_DSS_WITH_AES_128_CBC_SHA], MAX_SUITE_NAME); + break; + + case TLS_RSA_WITH_AES_256_CBC_RMD160: + parms.bulk_cipher_algorithm_ = aes; + parms.mac_algorithm_ = rmd; + parms.kea_ = rsa_kea; + parms.hash_size_ = RMD_LEN; + parms.key_size_ = AES_256_KEY_SZ; + parms.iv_size_ = AES_BLOCK_SZ; + parms.cipher_type_ = block; + crypto_.setDigest(NEW_YS RMD); + crypto_.setCipher(NEW_YS AES(AES_256_KEY_SZ)); + strncpy(parms.cipher_name_, + cipher_names[TLS_RSA_WITH_AES_256_CBC_RMD160], MAX_SUITE_NAME); + break; + + case TLS_RSA_WITH_AES_128_CBC_RMD160: + parms.bulk_cipher_algorithm_ = aes; + parms.mac_algorithm_ = rmd; + parms.kea_ = rsa_kea; + parms.hash_size_ = RMD_LEN; + parms.key_size_ = AES_128_KEY_SZ; + parms.iv_size_ = AES_BLOCK_SZ; + parms.cipher_type_ = block; + crypto_.setDigest(NEW_YS RMD); + crypto_.setCipher(NEW_YS AES); + strncpy(parms.cipher_name_, + cipher_names[TLS_RSA_WITH_AES_128_CBC_RMD160], MAX_SUITE_NAME); + break; + + case TLS_RSA_WITH_3DES_EDE_CBC_RMD160: + parms.bulk_cipher_algorithm_ = triple_des; + parms.mac_algorithm_ = rmd; + parms.kea_ = rsa_kea; + parms.hash_size_ = RMD_LEN; + parms.key_size_ = DES_EDE_KEY_SZ; + parms.iv_size_ = DES_IV_SZ; + parms.cipher_type_ = block; + crypto_.setDigest(NEW_YS RMD); + crypto_.setCipher(NEW_YS DES_EDE); + strncpy(parms.cipher_name_, + cipher_names[TLS_RSA_WITH_3DES_EDE_CBC_RMD160], MAX_SUITE_NAME); + break; + + case TLS_DHE_RSA_WITH_3DES_EDE_CBC_RMD160: + parms.bulk_cipher_algorithm_ = triple_des; + parms.mac_algorithm_ = rmd; + parms.kea_ = diffie_hellman_kea; + parms.sig_algo_ = rsa_sa_algo; + parms.hash_size_ = RMD_LEN; + parms.key_size_ = DES_EDE_KEY_SZ; + parms.iv_size_ = DES_IV_SZ; + parms.cipher_type_ = block; + secure_.use_connection().send_server_key_ = true; // eph + crypto_.setDigest(NEW_YS RMD); + crypto_.setCipher(NEW_YS DES_EDE); + strncpy(parms.cipher_name_, + cipher_names[TLS_DHE_RSA_WITH_3DES_EDE_CBC_RMD160], + MAX_SUITE_NAME); + break; + + case TLS_DHE_RSA_WITH_AES_256_CBC_RMD160: + parms.bulk_cipher_algorithm_ = aes; + parms.mac_algorithm_ = rmd; + parms.kea_ = diffie_hellman_kea; + parms.sig_algo_ = rsa_sa_algo; + parms.hash_size_ = RMD_LEN; + parms.key_size_ = AES_256_KEY_SZ; + parms.iv_size_ = AES_BLOCK_SZ; + parms.cipher_type_ = block; + secure_.use_connection().send_server_key_ = true; // eph + crypto_.setDigest(NEW_YS RMD); + crypto_.setCipher(NEW_YS AES(AES_256_KEY_SZ)); + strncpy(parms.cipher_name_, + cipher_names[TLS_DHE_RSA_WITH_AES_256_CBC_RMD160], + MAX_SUITE_NAME); + break; + + case TLS_DHE_RSA_WITH_AES_128_CBC_RMD160: + parms.bulk_cipher_algorithm_ = aes; + parms.mac_algorithm_ = rmd; + parms.kea_ = diffie_hellman_kea; + parms.sig_algo_ = rsa_sa_algo; + parms.hash_size_ = RMD_LEN; + parms.key_size_ = AES_128_KEY_SZ; + parms.iv_size_ = AES_BLOCK_SZ; + parms.cipher_type_ = block; + secure_.use_connection().send_server_key_ = true; // eph + crypto_.setDigest(NEW_YS RMD); + crypto_.setCipher(NEW_YS AES); + strncpy(parms.cipher_name_, + cipher_names[TLS_DHE_RSA_WITH_AES_128_CBC_RMD160], + MAX_SUITE_NAME); + break; + + case TLS_DHE_DSS_WITH_3DES_EDE_CBC_RMD160: + parms.bulk_cipher_algorithm_ = triple_des; + parms.mac_algorithm_ = rmd; + parms.kea_ = diffie_hellman_kea; + parms.sig_algo_ = dsa_sa_algo; + parms.hash_size_ = RMD_LEN; + parms.key_size_ = DES_EDE_KEY_SZ; + parms.iv_size_ = DES_IV_SZ; + parms.cipher_type_ = block; + secure_.use_connection().send_server_key_ = true; // eph + crypto_.setDigest(NEW_YS RMD); + crypto_.setCipher(NEW_YS DES_EDE); + strncpy(parms.cipher_name_, + cipher_names[TLS_DHE_DSS_WITH_3DES_EDE_CBC_RMD160], + MAX_SUITE_NAME); + break; + + case TLS_DHE_DSS_WITH_AES_256_CBC_RMD160: + parms.bulk_cipher_algorithm_ = aes; + parms.mac_algorithm_ = rmd; + parms.kea_ = diffie_hellman_kea; + parms.sig_algo_ = dsa_sa_algo; + parms.hash_size_ = RMD_LEN; + parms.key_size_ = AES_256_KEY_SZ; + parms.iv_size_ = AES_BLOCK_SZ; + parms.cipher_type_ = block; + secure_.use_connection().send_server_key_ = true; // eph + crypto_.setDigest(NEW_YS RMD); + crypto_.setCipher(NEW_YS AES(AES_256_KEY_SZ)); + strncpy(parms.cipher_name_, + cipher_names[TLS_DHE_DSS_WITH_AES_256_CBC_RMD160], + MAX_SUITE_NAME); + break; + + case TLS_DHE_DSS_WITH_AES_128_CBC_RMD160: + parms.bulk_cipher_algorithm_ = aes; + parms.mac_algorithm_ = rmd; + parms.kea_ = diffie_hellman_kea; + parms.sig_algo_ = dsa_sa_algo; + parms.hash_size_ = RMD_LEN; + parms.key_size_ = AES_128_KEY_SZ; + parms.iv_size_ = AES_BLOCK_SZ; + parms.cipher_type_ = block; + secure_.use_connection().send_server_key_ = true; // eph + crypto_.setDigest(NEW_YS RMD); + crypto_.setCipher(NEW_YS AES); + strncpy(parms.cipher_name_, + cipher_names[TLS_DHE_DSS_WITH_AES_128_CBC_RMD160], + MAX_SUITE_NAME); + break; + + default: + SetError(unknown_cipher); + } +} + + +// store peer's random +void SSL::set_random(const opaque* random, ConnectionEnd sender) +{ + if (sender == client_end) + memcpy(secure_.use_connection().client_random_, random, RAN_LEN); + else + memcpy(secure_.use_connection().server_random_, random, RAN_LEN); +} + + +// store client pre master secret +void SSL::set_preMaster(const opaque* pre, uint sz) +{ + secure_.use_connection().AllocPreSecret(sz); + memcpy(secure_.use_connection().pre_master_secret_, pre, sz); +} + + +// set yaSSL zlib type compression +int SSL::SetCompression() +{ +#ifdef HAVE_LIBZ + secure_.use_connection().compression_ = true; + return 0; +#else + return -1; // not built in +#endif +} + + +// unset yaSSL zlib type compression +void SSL::UnSetCompression() +{ + secure_.use_connection().compression_ = false; +} + + +// is yaSSL zlib compression on +bool SSL::CompressionOn() const +{ + return secure_.get_connection().compression_; +} + + +// store master secret +void SSL::set_masterSecret(const opaque* sec) +{ + memcpy(secure_.use_connection().master_secret_, sec, SECRET_LEN); +} + +// store server issued id +void SSL::set_sessionID(const opaque* sessionID) +{ + memcpy(secure_.use_connection().sessionID_, sessionID, ID_LEN); + secure_.use_connection().sessionID_Set_ = true; +} + + +// store error +void SSL::SetError(YasslError ye) +{ + states_.SetError(ye); + //strncpy(states_.useString(), e.what(), mySTL::named_exception::NAME_SIZE); + // TODO: add string here +} + + +// set the quiet shutdown mode (close_nofiy not sent or received on shutdown) +void SSL::SetQuietShutdown(bool mode) +{ + quietShutdown_ = mode; +} + + +Buffers& SSL::useBuffers() +{ + return buffers_; +} + + +// locals +namespace { + +// DeriveKeys and MasterSecret helper sets prefix letters +static bool setPrefix(opaque* sha_input, int i) +{ + switch (i) { + case 0: + memcpy(sha_input, "A", 1); + break; + case 1: + memcpy(sha_input, "BB", 2); + break; + case 2: + memcpy(sha_input, "CCC", 3); + break; + case 3: + memcpy(sha_input, "DDDD", 4); + break; + case 4: + memcpy(sha_input, "EEEEE", 5); + break; + case 5: + memcpy(sha_input, "FFFFFF", 6); + break; + case 6: + memcpy(sha_input, "GGGGGGG", 7); + break; + default: + return false; // prefix_error + } + return true; +} + + +const char handshake_order[] = "Out of order HandShake Message!"; + + +} // namespcae for locals + + +void SSL::order_error() +{ + SetError(out_of_order); +} + + +// Create and store the master secret see page 32, 6.1 +void SSL::makeMasterSecret() +{ + if (isTLS()) + makeTLSMasterSecret(); + else { + opaque sha_output[SHA_LEN]; + + const uint& preSz = secure_.get_connection().pre_secret_len_; + output_buffer md5_input(preSz + SHA_LEN); + output_buffer sha_input(PREFIX + preSz + 2 * RAN_LEN); + + MD5 md5; + SHA sha; + + md5_input.write(secure_.get_connection().pre_master_secret_, preSz); + + for (int i = 0; i < MASTER_ROUNDS; ++i) { + opaque prefix[PREFIX]; + if (!setPrefix(prefix, i)) { + SetError(prefix_error); + return; + } + + sha_input.set_current(0); + sha_input.write(prefix, i + 1); + + sha_input.write(secure_.get_connection().pre_master_secret_,preSz); + sha_input.write(secure_.get_connection().client_random_, RAN_LEN); + sha_input.write(secure_.get_connection().server_random_, RAN_LEN); + sha.get_digest(sha_output, sha_input.get_buffer(), + sha_input.get_size()); + + md5_input.set_current(preSz); + md5_input.write(sha_output, SHA_LEN); + md5.get_digest(&secure_.use_connection().master_secret_[i*MD5_LEN], + md5_input.get_buffer(), md5_input.get_size()); + } + deriveKeys(); + } + secure_.use_connection().CleanPreMaster(); +} + + +// create TLSv1 master secret +void SSL::makeTLSMasterSecret() +{ + opaque seed[SEED_LEN]; + + memcpy(seed, secure_.get_connection().client_random_, RAN_LEN); + memcpy(&seed[RAN_LEN], secure_.get_connection().server_random_, RAN_LEN); + + PRF(secure_.use_connection().master_secret_, SECRET_LEN, + secure_.get_connection().pre_master_secret_, + secure_.get_connection().pre_secret_len_, + master_label, MASTER_LABEL_SZ, + seed, SEED_LEN); + + deriveTLSKeys(); +} + + +// derive mac, write, and iv keys for server and client, see page 34, 6.2.2 +void SSL::deriveKeys() +{ + int length = 2 * secure_.get_parms().hash_size_ + + 2 * secure_.get_parms().key_size_ + + 2 * secure_.get_parms().iv_size_; + int rounds = (length + MD5_LEN - 1 ) / MD5_LEN; + input_buffer key_data(rounds * MD5_LEN); + + opaque sha_output[SHA_LEN]; + opaque md5_input[SECRET_LEN + SHA_LEN]; + opaque sha_input[KEY_PREFIX + SECRET_LEN + 2 * RAN_LEN]; + + MD5 md5; + SHA sha; + + memcpy(md5_input, secure_.get_connection().master_secret_, SECRET_LEN); + + for (int i = 0; i < rounds; ++i) { + int j = i + 1; + if (!setPrefix(sha_input, i)) { + SetError(prefix_error); + return; + } + + memcpy(&sha_input[j], secure_.get_connection().master_secret_, + SECRET_LEN); + memcpy(&sha_input[j+SECRET_LEN], + secure_.get_connection().server_random_, RAN_LEN); + memcpy(&sha_input[j + SECRET_LEN + RAN_LEN], + secure_.get_connection().client_random_, RAN_LEN); + sha.get_digest(sha_output, sha_input, + sizeof(sha_input) - KEY_PREFIX + j); + + memcpy(&md5_input[SECRET_LEN], sha_output, SHA_LEN); + md5.get_digest(key_data.get_buffer() + i * MD5_LEN, + md5_input, sizeof(md5_input)); + } + storeKeys(key_data.get_buffer()); +} + + +// derive mac, write, and iv keys for server and client +void SSL::deriveTLSKeys() +{ + int length = 2 * secure_.get_parms().hash_size_ + + 2 * secure_.get_parms().key_size_ + + 2 * secure_.get_parms().iv_size_; + opaque seed[SEED_LEN]; + input_buffer key_data(length); + + memcpy(seed, secure_.get_connection().server_random_, RAN_LEN); + memcpy(&seed[RAN_LEN], secure_.get_connection().client_random_, RAN_LEN); + + PRF(key_data.get_buffer(), length, secure_.get_connection().master_secret_, + SECRET_LEN, key_label, KEY_LABEL_SZ, seed, SEED_LEN); + + storeKeys(key_data.get_buffer()); +} + + +// store mac, write, and iv keys for client and server +void SSL::storeKeys(const opaque* key_data) +{ + int sz = secure_.get_parms().hash_size_; + memcpy(secure_.use_connection().client_write_MAC_secret_, key_data, sz); + int i = sz; + memcpy(secure_.use_connection().server_write_MAC_secret_,&key_data[i], sz); + i += sz; + + sz = secure_.get_parms().key_size_; + memcpy(secure_.use_connection().client_write_key_, &key_data[i], sz); + i += sz; + memcpy(secure_.use_connection().server_write_key_, &key_data[i], sz); + i += sz; + + sz = secure_.get_parms().iv_size_; + memcpy(secure_.use_connection().client_write_IV_, &key_data[i], sz); + i += sz; + memcpy(secure_.use_connection().server_write_IV_, &key_data[i], sz); + + setKeys(); +} + + +// set encrypt/decrypt keys and ivs +void SSL::setKeys() +{ + Connection& conn = secure_.use_connection(); + + if (secure_.get_parms().entity_ == client_end) { + crypto_.use_cipher().set_encryptKey(conn.client_write_key_, + conn.client_write_IV_); + crypto_.use_cipher().set_decryptKey(conn.server_write_key_, + conn.server_write_IV_); + } + else { + crypto_.use_cipher().set_encryptKey(conn.server_write_key_, + conn.server_write_IV_); + crypto_.use_cipher().set_decryptKey(conn.client_write_key_, + conn.client_write_IV_); + } +} + + + +// local functors +namespace yassl_int_cpp_local1 { // for explicit templates + +struct SumData { + uint total_; + SumData() : total_(0) {} + void operator()(input_buffer* data) { total_ += data->get_remaining(); } +}; + + +struct SumBuffer { + uint total_; + SumBuffer() : total_(0) {} + void operator()(output_buffer* buffer) { total_ += buffer->get_size(); } +}; + +} // namespace for locals +using namespace yassl_int_cpp_local1; + + +uint SSL::bufferedData() +{ + return STL::for_each(buffers_.getData().begin(),buffers_.getData().end(), + SumData()).total_; +} + + +// use input buffer to fill data +void SSL::fillData(Data& data) +{ + if (GetError()) return; + uint dataSz = data.get_length(); // input, data size to fill + size_t elements = buffers_.getData().size(); + + data.set_length(0); // output, actual data filled + dataSz = min(dataSz, bufferedData()); + + for (size_t i = 0; i < elements; i++) { + input_buffer* front = buffers_.getData().front(); + uint frontSz = front->get_remaining(); + uint readSz = min(dataSz - data.get_length(), frontSz); + + front->read(data.set_buffer() + data.get_length(), readSz); + data.set_length(data.get_length() + readSz); + + if (readSz == frontSz) { + buffers_.useData().pop_front(); + ysDelete(front); + } + if (data.get_length() == dataSz) + break; + } + + if (buffers_.getData().size() == 0) has_data_ = false; // none left +} + + +// like Fill but keep data in buffer +void SSL::PeekData(Data& data) +{ + if (GetError()) return; + uint dataSz = data.get_length(); // input, data size to fill + size_t elements = buffers_.getData().size(); + + data.set_length(0); // output, actual data filled + dataSz = min(dataSz, bufferedData()); + + Buffers::inputList::iterator front = buffers_.useData().begin(); + + while (elements) { + uint frontSz = (*front)->get_remaining(); + uint readSz = min(dataSz - data.get_length(), frontSz); + uint before = (*front)->get_current(); + + (*front)->read(data.set_buffer() + data.get_length(), readSz); + data.set_length(data.get_length() + readSz); + (*front)->set_current(before); + + if (data.get_length() == dataSz) + break; + + elements--; + front++; + } +} + + +// flush output buffer +void SSL::flushBuffer() +{ + if (GetError()) return; + + uint sz = STL::for_each(buffers_.getHandShake().begin(), + buffers_.getHandShake().end(), + SumBuffer()).total_; + output_buffer out(sz); + size_t elements = buffers_.getHandShake().size(); + + for (size_t i = 0; i < elements; i++) { + output_buffer* front = buffers_.getHandShake().front(); + out.write(front->get_buffer(), front->get_size()); + + buffers_.useHandShake().pop_front(); + ysDelete(front); + } + Send(out.get_buffer(), out.get_size()); +} + + +void SSL::Send(const byte* buffer, uint sz) +{ + if (socket_.send(buffer, sz) != sz) + SetError(send_error); +} + + +// get sequence number, if verify get peer's +uint SSL::get_SEQIncrement(bool verify) +{ + if (verify) + return secure_.use_connection().peer_sequence_number_++; + else + return secure_.use_connection().sequence_number_++; +} + + +const byte* SSL::get_macSecret(bool verify) +{ + if ( (secure_.get_parms().entity_ == client_end && !verify) || + (secure_.get_parms().entity_ == server_end && verify) ) + return secure_.get_connection().client_write_MAC_secret_; + else + return secure_.get_connection().server_write_MAC_secret_; +} + + +void SSL::verifyState(const RecordLayerHeader& rlHeader) +{ + if (GetError()) return; + + if (rlHeader.version_.major_ != 3 || rlHeader.version_.minor_ > 2) { + SetError(badVersion_error); + return; + } + + if (states_.getRecord() == recordNotReady || + (rlHeader.type_ == application_data && // data and handshake + states_.getHandShake() != handShakeReady) ) // isn't complete yet + SetError(record_layer); +} + + +void SSL::verifyState(const HandShakeHeader& hsHeader) +{ + if (GetError()) return; + + if (states_.getHandShake() == handShakeNotReady) { + SetError(handshake_layer); + return; + } + + if (secure_.get_parms().entity_ == client_end) + verifyClientState(hsHeader.get_handshakeType()); + else + verifyServerState(hsHeader.get_handshakeType()); +} + + +void SSL::verifyState(ClientState cs) +{ + if (GetError()) return; + if (states_.getClient() != cs) order_error(); +} + + +void SSL::verifyState(ServerState ss) +{ + if (GetError()) return; + if (states_.getServer() != ss) order_error(); +} + + +void SSL::verfiyHandShakeComplete() +{ + if (GetError()) return; + if (states_.getHandShake() != handShakeReady) order_error(); +} + + +void SSL::verifyClientState(HandShakeType hsType) +{ + if (GetError()) return; + + switch(hsType) { + case server_hello : + if (states_.getClient() != serverNull) + order_error(); + break; + case certificate : + if (states_.getClient() != serverHelloComplete) + order_error(); + break; + case server_key_exchange : + if (states_.getClient() != serverCertComplete) + order_error(); + break; + case certificate_request : + if (states_.getClient() != serverCertComplete && + states_.getClient() != serverKeyExchangeComplete) + order_error(); + break; + case server_hello_done : + if (states_.getClient() != serverCertComplete && + states_.getClient() != serverKeyExchangeComplete) + order_error(); + break; + case finished : + if (states_.getClient() != serverHelloDoneComplete || + secure_.get_parms().pending_) // no change + order_error(); // cipher yet + break; + default : + order_error(); + }; +} + + +void SSL::verifyServerState(HandShakeType hsType) +{ + if (GetError()) return; + + switch(hsType) { + case client_hello : + if (states_.getServer() != clientNull) + order_error(); + break; + case certificate : + if (states_.getServer() != clientHelloComplete) + order_error(); + break; + case client_key_exchange : + if (states_.getServer() != clientHelloComplete) + order_error(); + break; + case certificate_verify : + if (states_.getServer() != clientKeyExchangeComplete) + order_error(); + break; + case finished : + if (states_.getServer() != clientKeyExchangeComplete || + secure_.get_parms().pending_) // no change + order_error(); // cipher yet + break; + default : + order_error(); + }; +} + + +// try to find a suite match +void SSL::matchSuite(const opaque* peer, uint length) +{ + if (length == 0 || (length % 2) != 0) { + SetError(bad_input); + return; + } + + // start with best, if a match we are good, Ciphers are at odd index + // since all SSL and TLS ciphers have 0x00 first byte + for (uint i = 1; i < secure_.get_parms().suites_size_; i += 2) + for (uint j = 1; j < length; j+= 2) + if (secure_.use_parms().suites_[i] == peer[j]) { + secure_.use_parms().suite_[0] = 0x00; + secure_.use_parms().suite_[1] = peer[j]; + + return; + } + + SetError(match_error); +} + + +void SSL::set_session(SSL_SESSION* s) +{ + if (getSecurity().GetContext()->GetSessionCacheOff()) + return; + + if (s && GetSessions().lookup(s->GetID(), &secure_.use_resume())) { + secure_.set_resuming(true); + crypto_.use_certManager().setPeerX509(s->GetPeerX509()); + } +} + + +const Crypto& SSL::getCrypto() const +{ + return crypto_; +} + + +const Security& SSL::getSecurity() const +{ + return secure_; +} + + +const States& SSL::getStates() const +{ + return states_; +} + + +const sslHashes& SSL::getHashes() const +{ + return hashes_; +} + + +const sslFactory& SSL::getFactory() const +{ + return GetSSL_Factory(); +} + + +const Socket& SSL::getSocket() const +{ + return socket_; +} + + +YasslError SSL::GetError() const +{ + return states_.What(); +} + + +bool SSL::GetQuietShutdown() const +{ + return quietShutdown_; +} + + +bool SSL::GetMultiProtocol() const +{ + return secure_.GetContext()->getMethod()->multipleProtocol(); +} + + +Crypto& SSL::useCrypto() +{ + return crypto_; +} + + +Security& SSL::useSecurity() +{ + return secure_; +} + + +States& SSL::useStates() +{ + return states_; +} + + +sslHashes& SSL::useHashes() +{ + return hashes_; +} + + +Socket& SSL::useSocket() +{ + return socket_; +} + + +Log& SSL::useLog() +{ + return log_; +} + + +bool SSL::isTLS() const +{ + return secure_.get_connection().TLS_; +} + + +bool SSL::isTLSv1_1() const +{ + return secure_.get_connection().TLSv1_1_; +} + + +// is there buffered data available, optimization to remove iteration on buffer +bool SSL::HasData() const +{ + return has_data_; +} + + +void SSL::addData(input_buffer* data) +{ + buffers_.useData().push_back(data); + if (!has_data_) has_data_ = true; +} + + +void SSL::addBuffer(output_buffer* b) +{ + buffers_.useHandShake().push_back(b); +} + + +void SSL_SESSION::CopyX509(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); +} + + +// store connection parameters +SSL_SESSION::SSL_SESSION(const SSL& ssl, RandomPool& ran) + : timeout_(DEFAULT_TIMEOUT), random_(ran), peerX509_(0) +{ + const Connection& conn = ssl.getSecurity().get_connection(); + + memcpy(sessionID_, conn.sessionID_, ID_LEN); + memcpy(master_secret_, conn.master_secret_, SECRET_LEN); + memcpy(suite_, ssl.getSecurity().get_parms().suite_, SUITE_LEN); + + bornOn_ = lowResTimer(); + + CopyX509(ssl.getCrypto().get_certManager().get_peerX509()); +} + + +// for resumption copy in ssl::parameters +SSL_SESSION::SSL_SESSION(RandomPool& ran) + : bornOn_(0), timeout_(0), random_(ran), peerX509_(0) +{ + memset(sessionID_, 0, ID_LEN); + memset(master_secret_, 0, SECRET_LEN); + memset(suite_, 0, SUITE_LEN); +} + + +SSL_SESSION& SSL_SESSION::operator=(const SSL_SESSION& that) +{ + memcpy(sessionID_, that.sessionID_, ID_LEN); + memcpy(master_secret_, that.master_secret_, SECRET_LEN); + memcpy(suite_, that.suite_, SUITE_LEN); + + bornOn_ = that.bornOn_; + timeout_ = that.timeout_; + + if (peerX509_) { + ysDelete(peerX509_); + peerX509_ = 0; + } + CopyX509(that.peerX509_); + + return *this; +} + + +const opaque* SSL_SESSION::GetID() const +{ + return sessionID_; +} + + +const opaque* SSL_SESSION::GetSecret() const +{ + return master_secret_; +} + + +const Cipher* SSL_SESSION::GetSuite() const +{ + return suite_; +} + + +X509* SSL_SESSION::GetPeerX509() const +{ + return peerX509_; +} + + +uint SSL_SESSION::GetBornOn() const +{ + return bornOn_; +} + + +uint SSL_SESSION::GetTimeOut() const +{ + return timeout_; +} + + +void SSL_SESSION::SetTimeOut(uint t) +{ + timeout_ = t; +} + + +extern void clean(volatile opaque*, uint, RandomPool&); + + +// clean up secret data +SSL_SESSION::~SSL_SESSION() +{ + volatile opaque* p = master_secret_; + clean(p, SECRET_LEN, random_); + + ysDelete(peerX509_); +} + + +static Sessions* sessionsInstance = 0; + +Sessions& GetSessions() +{ + if (!sessionsInstance) + sessionsInstance = NEW_YS Sessions; + return *sessionsInstance; +} + + +static sslFactory* sslFactoryInstance = 0; + +sslFactory& GetSSL_Factory() +{ + if (!sslFactoryInstance) + sslFactoryInstance = NEW_YS sslFactory; + return *sslFactoryInstance; +} + + +static Errors* errorsInstance = 0; + +Errors& GetErrors() +{ + if (!errorsInstance) + errorsInstance = NEW_YS Errors; + return *errorsInstance; +} + + +typedef Mutex::Lock Lock; + + + +void Sessions::add(const SSL& ssl) +{ + if (ssl.getSecurity().get_connection().sessionID_Set_) { + Lock guard(mutex_); + list_.push_back(NEW_YS SSL_SESSION(ssl, random_)); + count_++; + } + + if (count_ > SESSION_FLUSH_COUNT) + if (!ssl.getSecurity().GetContext()->GetSessionCacheFlushOff()) + Flush(); +} + + +Sessions::~Sessions() +{ + STL::for_each(list_.begin(), list_.end(), del_ptr_zero()); +} + + +// locals +namespace yassl_int_cpp_local2 { // for explicit templates + +typedef STL::list<SSL_SESSION*>::iterator sess_iterator; +typedef STL::list<ThreadError>::iterator thr_iterator; + +struct sess_match { + const opaque* id_; + explicit sess_match(const opaque* p) : id_(p) {} + + bool operator()(SSL_SESSION* sess) + { + if ( memcmp(sess->GetID(), id_, ID_LEN) == 0) + return true; + return false; + } +}; + + +THREAD_ID_T GetSelf() +{ +#ifndef _POSIX_THREADS + return GetCurrentThreadId(); +#else + return pthread_self(); +#endif +} + +struct thr_match { + THREAD_ID_T id_; + explicit thr_match() : id_(GetSelf()) {} + + bool operator()(ThreadError thr) + { + if (thr.threadID_ == id_) + return true; + return false; + } +}; + + +} // local namespace +using namespace yassl_int_cpp_local2; + + +// lookup session by id, return a copy if space provided +SSL_SESSION* Sessions::lookup(const opaque* id, SSL_SESSION* copy) +{ + Lock guard(mutex_); + sess_iterator find = STL::find_if(list_.begin(), list_.end(), + sess_match(id)); + if (find != list_.end()) { + uint current = lowResTimer(); + if ( ((*find)->GetBornOn() + (*find)->GetTimeOut()) < current) { + del_ptr_zero()(*find); + list_.erase(find); + return 0; + } + if (copy) + *copy = *(*find); + return *find; + } + return 0; +} + + +// remove a session by id +void Sessions::remove(const opaque* id) +{ + Lock guard(mutex_); + sess_iterator find = STL::find_if(list_.begin(), list_.end(), + sess_match(id)); + if (find != list_.end()) { + del_ptr_zero()(*find); + list_.erase(find); + } +} + + +// flush expired sessions from cache +void Sessions::Flush() +{ + Lock guard(mutex_); + sess_iterator next = list_.begin(); + uint current = lowResTimer(); + + while (next != list_.end()) { + sess_iterator si = next; + ++next; + if ( ((*si)->GetBornOn() + (*si)->GetTimeOut()) < current) { + del_ptr_zero()(*si); + list_.erase(si); + } + } + count_ = 0; // reset flush counter +} + + +// remove a self thread error +void Errors::Remove() +{ + Lock guard(mutex_); + thr_iterator find = STL::find_if(list_.begin(), list_.end(), + thr_match()); + if (find != list_.end()) + list_.erase(find); +} + + +// lookup self error code +int Errors::Lookup(bool peek) +{ + Lock guard(mutex_); + thr_iterator find = STL::find_if(list_.begin(), list_.end(), + thr_match()); + if (find != list_.end()) { + int ret = find->errorID_; + if (!peek) + list_.erase(find); + return ret; + } + else + return 0; +} + + +// add a new error code for self +void Errors::Add(int error) +{ + ThreadError add; + add.errorID_ = error; + add.threadID_ = GetSelf(); + + Remove(); // may have old error + + Lock guard(mutex_); + list_.push_back(add); +} + + +SSL_METHOD::SSL_METHOD(ConnectionEnd ce, ProtocolVersion pv, bool multiProto) + : version_(pv), side_(ce), verifyPeer_(false), verifyNone_(false), + failNoCert_(false), multipleProtocol_(multiProto) +{} + + +ProtocolVersion SSL_METHOD::getVersion() const +{ + return version_; +} + + +ConnectionEnd SSL_METHOD::getSide() const +{ + return side_; +} + + +void SSL_METHOD::setVerifyPeer() +{ + verifyPeer_ = true; +} + + +void SSL_METHOD::setVerifyNone() +{ + verifyNone_ = true; +} + + +void SSL_METHOD::setFailNoCert() +{ + failNoCert_ = true; +} + + +bool SSL_METHOD::verifyPeer() const +{ + return verifyPeer_; +} + + +bool SSL_METHOD::verifyNone() const +{ + return verifyNone_; +} + + +bool SSL_METHOD::failNoCert() const +{ + return failNoCert_; +} + + +bool SSL_METHOD::multipleProtocol() const +{ + return multipleProtocol_; +} + + +SSL_CTX::SSL_CTX(SSL_METHOD* meth) + : method_(meth), certificate_(0), privateKey_(0), passwordCb_(0), + userData_(0), sessionCacheOff_(false), sessionCacheFlushOff_(false), + verifyCallback_(0) +{} + + +SSL_CTX::~SSL_CTX() +{ + ysDelete(method_); + ysDelete(certificate_); + ysDelete(privateKey_); + + STL::for_each(caList_.begin(), caList_.end(), del_ptr_zero()); +} + + +void SSL_CTX::AddCA(x509* ca) +{ + caList_.push_back(ca); +} + + +const SSL_CTX::CertList& +SSL_CTX::GetCA_List() const +{ + return caList_; +} + + +const VerifyCallback SSL_CTX::getVerifyCallback() const +{ + return verifyCallback_; +} + + +const x509* SSL_CTX::getCert() const +{ + return certificate_; +} + + +const x509* SSL_CTX::getKey() const +{ + return privateKey_; +} + + +const SSL_METHOD* SSL_CTX::getMethod() const +{ + return method_; +} + + +const Ciphers& SSL_CTX::GetCiphers() const +{ + return ciphers_; +} + + +const DH_Parms& SSL_CTX::GetDH_Parms() const +{ + return dhParms_; +} + + +const Stats& SSL_CTX::GetStats() const +{ + return stats_; +} + + +pem_password_cb SSL_CTX::GetPasswordCb() const +{ + return passwordCb_; +} + + +void SSL_CTX::SetPasswordCb(pem_password_cb cb) +{ + passwordCb_ = cb; +} + + +void* SSL_CTX::GetUserData() const +{ + return userData_; +} + + +bool SSL_CTX::GetSessionCacheOff() const +{ + return sessionCacheOff_; +} + + +bool SSL_CTX::GetSessionCacheFlushOff() const +{ + return sessionCacheFlushOff_; +} + + +void SSL_CTX::SetUserData(void* data) +{ + userData_ = data; +} + + +void SSL_CTX::SetSessionCacheOff() +{ + sessionCacheOff_ = true; +} + + +void SSL_CTX::SetSessionCacheFlushOff() +{ + sessionCacheFlushOff_ = true; +} + + +void SSL_CTX::setVerifyPeer() +{ + method_->setVerifyPeer(); +} + + +void SSL_CTX::setVerifyNone() +{ + method_->setVerifyNone(); +} + + +void SSL_CTX::setFailNoCert() +{ + method_->setFailNoCert(); +} + + +void SSL_CTX::setVerifyCallback(VerifyCallback vc) +{ + verifyCallback_ = vc; +} + + +bool SSL_CTX::SetDH(const DH& dh) +{ + dhParms_.p_ = dh.p->int_; + dhParms_.g_ = dh.g->int_; + + return dhParms_.set_ = true; +} + + +bool SSL_CTX::SetCipherList(const char* list) +{ + if (!list) + return false; + + bool ret = false; + char name[MAX_SUITE_NAME]; + + char needle[] = ":"; + char* haystack = const_cast<char*>(list); + char* prev; + + const int suiteSz = sizeof(cipher_names) / sizeof(cipher_names[0]); + int idx = 0; + + for(;;) { + size_t len; + prev = haystack; + haystack = strstr(haystack, needle); + + if (!haystack) // last cipher + len = min(sizeof(name), strlen(prev)); + else + len = min(sizeof(name), (size_t)(haystack - prev)); + + strncpy(name, prev, len); + name[(len == sizeof(name)) ? len - 1 : len] = 0; + + for (int i = 0; i < suiteSz; i++) + if (strncmp(name, cipher_names[i], sizeof(name)) == 0) { + + ciphers_.suites_[idx++] = 0x00; // first byte always zero + ciphers_.suites_[idx++] = i; + + if (!ret) ret = true; // found at least one + break; + } + if (!haystack) break; + haystack++; + } + + if (ret) { + ciphers_.setSuites_ = true; + ciphers_.suiteSz_ = idx; + } + + return ret; +} + + +void SSL_CTX::IncrementStats(StatsField fd) +{ + + Lock guard(mutex_); + + switch (fd) { + + case Accept: + ++stats_.accept_; + break; + + case Connect: + ++stats_.connect_; + break; + + case AcceptGood: + ++stats_.acceptGood_; + break; + + case ConnectGood: + ++stats_.connectGood_; + break; + + case AcceptRenegotiate: + ++stats_.acceptRenegotiate_; + break; + + case ConnectRenegotiate: + ++stats_.connectRenegotiate_; + break; + + case Hits: + ++stats_.hits_; + break; + + case CbHits: + ++stats_.cbHits_; + break; + + case CacheFull: + ++stats_.cacheFull_; + break; + + case Misses: + ++stats_.misses_; + break; + + case Timeouts: + ++stats_.timeouts_; + break; + + case Number: + ++stats_.number_; + break; + + case GetCacheSize: + ++stats_.getCacheSize_; + break; + + case VerifyMode: + ++stats_.verifyMode_; + break; + + case VerifyDepth: + ++stats_.verifyDepth_; + break; + + default: + break; + } +} + + +Crypto::Crypto() + : digest_(0), cipher_(0), dh_(0) +{} + + +Crypto::~Crypto() +{ + ysDelete(dh_); + ysDelete(cipher_); + ysDelete(digest_); +} + + +const Digest& Crypto::get_digest() const +{ + return *digest_; +} + + +const BulkCipher& Crypto::get_cipher() const +{ + return *cipher_; +} + + +const DiffieHellman& Crypto::get_dh() const +{ + return *dh_; +} + + +const RandomPool& Crypto::get_random() const +{ + return random_; +} + + +const CertManager& Crypto::get_certManager() const +{ + return cert_; +} + + + +Digest& Crypto::use_digest() +{ + return *digest_; +} + + +BulkCipher& Crypto::use_cipher() +{ + return *cipher_; +} + + +DiffieHellman& Crypto::use_dh() +{ + return *dh_; +} + + +RandomPool& Crypto::use_random() +{ + return random_; +} + + +CertManager& Crypto::use_certManager() +{ + return cert_; +} + + + +void Crypto::SetDH(DiffieHellman* dh) +{ + dh_ = dh; +} + + +void Crypto::SetDH(const DH_Parms& dh) +{ + if (dh.set_) + dh_ = NEW_YS DiffieHellman(dh.p_, dh.g_, random_); +} + + +bool Crypto::DhSet() +{ + return dh_ != 0; +} + + +void Crypto::setDigest(Digest* digest) +{ + digest_ = digest; +} + + +void Crypto::setCipher(BulkCipher* c) +{ + cipher_ = c; +} + + +const MD5& sslHashes::get_MD5() const +{ + return md5HandShake_; +} + + +const SHA& sslHashes::get_SHA() const +{ + return shaHandShake_; +} + + +const Finished& sslHashes::get_verify() const +{ + return verify_; +} + + +const Hashes& sslHashes::get_certVerify() const +{ + return certVerify_; +} + + +MD5& sslHashes::use_MD5(){ + return md5HandShake_; +} + + +SHA& sslHashes::use_SHA() +{ + return shaHandShake_; +} + + +Finished& sslHashes::use_verify() +{ + return verify_; +} + + +Hashes& sslHashes::use_certVerify() +{ + return certVerify_; +} + + +Buffers::Buffers() : rawInput_(0) +{} + + +Buffers::~Buffers() +{ + STL::for_each(handShakeList_.begin(), handShakeList_.end(), + del_ptr_zero()) ; + STL::for_each(dataList_.begin(), dataList_.end(), + del_ptr_zero()) ; + ysDelete(rawInput_); +} + + +void Buffers::SetRawInput(input_buffer* ib) +{ + assert(rawInput_ == 0); + rawInput_ = ib; +} + + +input_buffer* Buffers::TakeRawInput() +{ + input_buffer* ret = rawInput_; + rawInput_ = 0; + + return ret; +} + + +const Buffers::inputList& Buffers::getData() const +{ + return dataList_; +} + + +const Buffers::outputList& Buffers::getHandShake() const +{ + return handShakeList_; +} + + +Buffers::inputList& Buffers::useData() +{ + return dataList_; +} + + +Buffers::outputList& Buffers::useHandShake() +{ + return handShakeList_; +} + + +Security::Security(ProtocolVersion pv, RandomPool& ran, ConnectionEnd ce, + const Ciphers& ciphers, SSL_CTX* ctx, bool haveDH) + : conn_(pv, ran), parms_(ce, ciphers, pv, haveDH), resumeSession_(ran), + ctx_(ctx), resuming_(false) +{} + + +const Connection& Security::get_connection() const +{ + return conn_; +} + + +const SSL_CTX* Security::GetContext() const +{ + return ctx_; +} + + +const Parameters& Security::get_parms() const +{ + return parms_; +} + + +const SSL_SESSION& Security::get_resume() const +{ + return resumeSession_; +} + + +bool Security::get_resuming() const +{ + return resuming_; +} + + +Connection& Security::use_connection() +{ + return conn_; +} + + +Parameters& Security::use_parms() +{ + return parms_; +} + + +SSL_SESSION& Security::use_resume() +{ + return resumeSession_; +} + + +void Security::set_resuming(bool b) +{ + resuming_ = b; +} + + +X509_NAME::X509_NAME(const char* n, size_t sz) + : name_(0), sz_(sz) +{ + if (sz) { + name_ = NEW_YS char[sz]; + memcpy(name_, n, sz); + } + entry_.data = 0; +} + + +X509_NAME::~X509_NAME() +{ + ysArrayDelete(name_); + ysArrayDelete(entry_.data); +} + + +const char* X509_NAME::GetName() const +{ + return name_; +} + + +size_t X509_NAME::GetLength() const +{ + return sz_; +} + + +X509::X509(const char* i, size_t iSz, const char* s, size_t sSz, + const char* b, int bSz, const char* a, int aSz) + : issuer_(i, iSz), subject_(s, sSz), + beforeDate_(b, bSz), afterDate_(a, aSz) +{} + + +X509_NAME* X509::GetIssuer() +{ + return &issuer_; +} + + +X509_NAME* X509::GetSubject() +{ + return &subject_; +} + + +ASN1_STRING* X509::GetBefore() +{ + return beforeDate_.GetString(); +} + + +ASN1_STRING* X509::GetAfter() +{ + return afterDate_.GetString(); +} + + +ASN1_STRING* X509_NAME::GetEntry(int i) +{ + if (i < 0 || i >= int(sz_)) + return 0; + + if (entry_.data) + ysArrayDelete(entry_.data); + entry_.data = NEW_YS byte[sz_]; // max size; + + memcpy(entry_.data, &name_[i], sz_ - i); + if (entry_.data[sz_ -i - 1]) { + entry_.data[sz_ - i] = 0; + entry_.length = int(sz_) - i; + } + else + entry_.length = int(sz_) - i - 1; + entry_.type = 0; + + return &entry_; +} + + +StringHolder::StringHolder(const char* str, int sz) +{ + asnString_.length = sz; + asnString_.data = NEW_YS byte[sz + 1]; + memcpy(asnString_.data, str, sz); + asnString_.type = 0; // not used for now +} + + +StringHolder::~StringHolder() +{ + ysArrayDelete(asnString_.data); +} + + +ASN1_STRING* StringHolder::GetString() +{ + return &asnString_; +} + + +#ifdef HAVE_LIBZ + + void* myAlloc(void* /* opaque */, unsigned int item, unsigned int size) + { + return NEW_YS unsigned char[item * size]; + } + + + void myFree(void* /* opaque */, void* memory) + { + unsigned char* ptr = static_cast<unsigned char*>(memory); + yaSSL::ysArrayDelete(ptr); + } + + + // put size in front of compressed data + int Compress(const byte* in, int sz, input_buffer& buffer) + { + byte tmp[LENGTH_SZ]; + z_stream c_stream; /* compression stream */ + + buffer.allocate(sz + sizeof(uint16) + COMPRESS_EXTRA); + + c_stream.zalloc = myAlloc; + c_stream.zfree = myFree; + c_stream.opaque = (voidpf)0; + + c_stream.next_in = const_cast<byte*>(in); + c_stream.avail_in = sz; + c_stream.next_out = buffer.get_buffer() + sizeof(tmp); + c_stream.avail_out = buffer.get_capacity() - sizeof(tmp); + + if (deflateInit(&c_stream, 8) != Z_OK) return -1; + int err = deflate(&c_stream, Z_FINISH); + deflateEnd(&c_stream); + if (err != Z_OK && err != Z_STREAM_END) return -1; + + c16toa(sz, tmp); + memcpy(buffer.get_buffer(), tmp, sizeof(tmp)); + buffer.add_size(c_stream.total_out + sizeof(tmp)); + + return 0; + } + + + // get uncompressed size in front + int DeCompress(input_buffer& in, int sz, input_buffer& out) + { + byte tmp[LENGTH_SZ]; + + in.read(tmp, sizeof(tmp)); + + uint16 len; + ato16(tmp, len); + + out.allocate(len); + + z_stream d_stream; /* decompression stream */ + + d_stream.zalloc = myAlloc; + d_stream.zfree = myFree; + d_stream.opaque = (voidpf)0; + + d_stream.next_in = in.get_buffer() + in.get_current(); + d_stream.avail_in = sz - sizeof(tmp); + d_stream.next_out = out.get_buffer(); + d_stream.avail_out = out.get_capacity(); + + if (inflateInit(&d_stream) != Z_OK) return -1; + int err = inflate(&d_stream, Z_FINISH); + inflateEnd(&d_stream); + if (err != Z_OK && err != Z_STREAM_END) return -1; + + out.add_size(d_stream.total_out); + in.set_current(in.get_current() + sz - sizeof(tmp)); + + return 0; + } + + +#else // LIBZ + + // these versions should never get called + int Compress(const byte* in, int sz, input_buffer& buffer) + { + assert(0); + return -1; + } + + + int DeCompress(input_buffer& in, int sz, input_buffer& out) + { + assert(0); + return -1; + } + + +#endif // LIBZ + + +} // namespace + + + +extern "C" void yaSSL_CleanUp() +{ + TaoCrypt::CleanUp(); + yaSSL::ysDelete(yaSSL::sslFactoryInstance); + yaSSL::ysDelete(yaSSL::sessionsInstance); + yaSSL::ysDelete(yaSSL::errorsInstance); + + // In case user calls more than once, prevent seg fault + yaSSL::sslFactoryInstance = 0; + yaSSL::sessionsInstance = 0; + yaSSL::errorsInstance = 0; +} + + +#ifdef HAVE_EXPLICIT_TEMPLATE_INSTANTIATION +namespace mySTL { +template yaSSL::yassl_int_cpp_local1::SumData for_each<mySTL::list<yaSSL::input_buffer*>::iterator, yaSSL::yassl_int_cpp_local1::SumData>(mySTL::list<yaSSL::input_buffer*>::iterator, mySTL::list<yaSSL::input_buffer*>::iterator, yaSSL::yassl_int_cpp_local1::SumData); +template yaSSL::yassl_int_cpp_local1::SumBuffer for_each<mySTL::list<yaSSL::output_buffer*>::iterator, yaSSL::yassl_int_cpp_local1::SumBuffer>(mySTL::list<yaSSL::output_buffer*>::iterator, mySTL::list<yaSSL::output_buffer*>::iterator, yaSSL::yassl_int_cpp_local1::SumBuffer); +template mySTL::list<yaSSL::SSL_SESSION*>::iterator find_if<mySTL::list<yaSSL::SSL_SESSION*>::iterator, yaSSL::yassl_int_cpp_local2::sess_match>(mySTL::list<yaSSL::SSL_SESSION*>::iterator, mySTL::list<yaSSL::SSL_SESSION*>::iterator, yaSSL::yassl_int_cpp_local2::sess_match); +template mySTL::list<yaSSL::ThreadError>::iterator find_if<mySTL::list<yaSSL::ThreadError>::iterator, yaSSL::yassl_int_cpp_local2::thr_match>(mySTL::list<yaSSL::ThreadError>::iterator, mySTL::list<yaSSL::ThreadError>::iterator, yaSSL::yassl_int_cpp_local2::thr_match); +} +#endif + diff --git a/externals/mysql/extlib/yassl/taocrypt/include/aes.hpp b/externals/mysql/extlib/yassl/taocrypt/include/aes.hpp new file mode 100644 index 00000000000..dc19c98a83a --- /dev/null +++ b/externals/mysql/extlib/yassl/taocrypt/include/aes.hpp @@ -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 diff --git a/externals/mysql/extlib/yassl/taocrypt/include/algebra.hpp b/externals/mysql/extlib/yassl/taocrypt/include/algebra.hpp new file mode 100644 index 00000000000..298ef115a4a --- /dev/null +++ b/externals/mysql/extlib/yassl/taocrypt/include/algebra.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 diff --git a/externals/mysql/extlib/yassl/taocrypt/include/arc4.hpp b/externals/mysql/extlib/yassl/taocrypt/include/arc4.hpp new file mode 100644 index 00000000000..757e1a50d71 --- /dev/null +++ b/externals/mysql/extlib/yassl/taocrypt/include/arc4.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 + diff --git a/externals/mysql/extlib/yassl/taocrypt/include/asn.hpp b/externals/mysql/extlib/yassl/taocrypt/include/asn.hpp new file mode 100644 index 00000000000..1c1850cb47e --- /dev/null +++ b/externals/mysql/extlib/yassl/taocrypt/include/asn.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 diff --git a/externals/mysql/extlib/yassl/taocrypt/include/block.hpp b/externals/mysql/extlib/yassl/taocrypt/include/block.hpp new file mode 100644 index 00000000000..529a91eee08 --- /dev/null +++ b/externals/mysql/extlib/yassl/taocrypt/include/block.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 diff --git a/externals/mysql/extlib/yassl/taocrypt/include/blowfish.hpp b/externals/mysql/extlib/yassl/taocrypt/include/blowfish.hpp new file mode 100644 index 00000000000..90d2c014b4c --- /dev/null +++ b/externals/mysql/extlib/yassl/taocrypt/include/blowfish.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 + diff --git a/externals/mysql/extlib/yassl/taocrypt/include/coding.hpp b/externals/mysql/extlib/yassl/taocrypt/include/coding.hpp new file mode 100644 index 00000000000..3b1c0687604 --- /dev/null +++ b/externals/mysql/extlib/yassl/taocrypt/include/coding.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 diff --git a/externals/mysql/extlib/yassl/taocrypt/include/des.hpp b/externals/mysql/extlib/yassl/taocrypt/include/des.hpp new file mode 100644 index 00000000000..f99a289392f --- /dev/null +++ b/externals/mysql/extlib/yassl/taocrypt/include/des.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 diff --git a/externals/mysql/extlib/yassl/taocrypt/include/dh.hpp b/externals/mysql/extlib/yassl/taocrypt/include/dh.hpp new file mode 100644 index 00000000000..b7724cbd80d --- /dev/null +++ b/externals/mysql/extlib/yassl/taocrypt/include/dh.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 diff --git a/externals/mysql/extlib/yassl/taocrypt/include/dsa.hpp b/externals/mysql/extlib/yassl/taocrypt/include/dsa.hpp new file mode 100644 index 00000000000..f185b852cdc --- /dev/null +++ b/externals/mysql/extlib/yassl/taocrypt/include/dsa.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 diff --git a/externals/mysql/extlib/yassl/taocrypt/include/error.hpp b/externals/mysql/extlib/yassl/taocrypt/include/error.hpp new file mode 100644 index 00000000000..ef8b065d2f1 --- /dev/null +++ b/externals/mysql/extlib/yassl/taocrypt/include/error.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 diff --git a/externals/mysql/extlib/yassl/taocrypt/include/file.hpp b/externals/mysql/extlib/yassl/taocrypt/include/file.hpp new file mode 100644 index 00000000000..0f85b46fdb2 --- /dev/null +++ b/externals/mysql/extlib/yassl/taocrypt/include/file.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 diff --git a/externals/mysql/extlib/yassl/taocrypt/include/hash.hpp b/externals/mysql/extlib/yassl/taocrypt/include/hash.hpp new file mode 100644 index 00000000000..fa5f6c04720 --- /dev/null +++ b/externals/mysql/extlib/yassl/taocrypt/include/hash.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 diff --git a/externals/mysql/extlib/yassl/taocrypt/include/hmac.hpp b/externals/mysql/extlib/yassl/taocrypt/include/hmac.hpp new file mode 100644 index 00000000000..1d486514e06 --- /dev/null +++ b/externals/mysql/extlib/yassl/taocrypt/include/hmac.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 diff --git a/externals/mysql/extlib/yassl/taocrypt/include/integer.hpp b/externals/mysql/extlib/yassl/taocrypt/include/integer.hpp new file mode 100644 index 00000000000..186d62bb866 --- /dev/null +++ b/externals/mysql/extlib/yassl/taocrypt/include/integer.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 diff --git a/externals/mysql/extlib/yassl/taocrypt/include/kernelc.hpp b/externals/mysql/extlib/yassl/taocrypt/include/kernelc.hpp new file mode 100644 index 00000000000..daa3762d5dd --- /dev/null +++ b/externals/mysql/extlib/yassl/taocrypt/include/kernelc.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 diff --git a/externals/mysql/extlib/yassl/taocrypt/include/md2.hpp b/externals/mysql/extlib/yassl/taocrypt/include/md2.hpp new file mode 100644 index 00000000000..cb13d86ec07 --- /dev/null +++ b/externals/mysql/extlib/yassl/taocrypt/include/md2.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 + diff --git a/externals/mysql/extlib/yassl/taocrypt/include/md4.hpp b/externals/mysql/extlib/yassl/taocrypt/include/md4.hpp new file mode 100644 index 00000000000..28c2bc7efd1 --- /dev/null +++ b/externals/mysql/extlib/yassl/taocrypt/include/md4.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 + diff --git a/externals/mysql/extlib/yassl/taocrypt/include/md5.hpp b/externals/mysql/extlib/yassl/taocrypt/include/md5.hpp new file mode 100644 index 00000000000..9b461543675 --- /dev/null +++ b/externals/mysql/extlib/yassl/taocrypt/include/md5.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 + diff --git a/externals/mysql/extlib/yassl/taocrypt/include/misc.hpp b/externals/mysql/extlib/yassl/taocrypt/include/misc.hpp new file mode 100644 index 00000000000..a101a14bcdb --- /dev/null +++ b/externals/mysql/extlib/yassl/taocrypt/include/misc.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 diff --git a/externals/mysql/extlib/yassl/taocrypt/include/modarith.hpp b/externals/mysql/extlib/yassl/taocrypt/include/modarith.hpp new file mode 100644 index 00000000000..501a8129b90 --- /dev/null +++ b/externals/mysql/extlib/yassl/taocrypt/include/modarith.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 diff --git a/externals/mysql/extlib/yassl/taocrypt/include/modes.hpp b/externals/mysql/extlib/yassl/taocrypt/include/modes.hpp new file mode 100644 index 00000000000..d1ebce7568b --- /dev/null +++ b/externals/mysql/extlib/yassl/taocrypt/include/modes.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 diff --git a/externals/mysql/extlib/yassl/taocrypt/include/pwdbased.hpp b/externals/mysql/extlib/yassl/taocrypt/include/pwdbased.hpp new file mode 100644 index 00000000000..5ece1a8f43b --- /dev/null +++ b/externals/mysql/extlib/yassl/taocrypt/include/pwdbased.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 diff --git a/externals/mysql/extlib/yassl/taocrypt/include/random.hpp b/externals/mysql/extlib/yassl/taocrypt/include/random.hpp new file mode 100644 index 00000000000..91058e8c5cf --- /dev/null +++ b/externals/mysql/extlib/yassl/taocrypt/include/random.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 + diff --git a/externals/mysql/extlib/yassl/taocrypt/include/ripemd.hpp b/externals/mysql/extlib/yassl/taocrypt/include/ripemd.hpp new file mode 100644 index 00000000000..a63f92ceadd --- /dev/null +++ b/externals/mysql/extlib/yassl/taocrypt/include/ripemd.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 + diff --git a/externals/mysql/extlib/yassl/taocrypt/include/rsa.hpp b/externals/mysql/extlib/yassl/taocrypt/include/rsa.hpp new file mode 100644 index 00000000000..c895ab6fd34 --- /dev/null +++ b/externals/mysql/extlib/yassl/taocrypt/include/rsa.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 diff --git a/externals/mysql/extlib/yassl/taocrypt/include/runtime.hpp b/externals/mysql/extlib/yassl/taocrypt/include/runtime.hpp new file mode 100644 index 00000000000..6ece50d635f --- /dev/null +++ b/externals/mysql/extlib/yassl/taocrypt/include/runtime.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 + diff --git a/externals/mysql/extlib/yassl/taocrypt/include/sha.hpp b/externals/mysql/extlib/yassl/taocrypt/include/sha.hpp new file mode 100644 index 00000000000..c0b4368121b --- /dev/null +++ b/externals/mysql/extlib/yassl/taocrypt/include/sha.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 + diff --git a/externals/mysql/extlib/yassl/taocrypt/include/twofish.hpp b/externals/mysql/extlib/yassl/taocrypt/include/twofish.hpp new file mode 100644 index 00000000000..bc6f89a9f39 --- /dev/null +++ b/externals/mysql/extlib/yassl/taocrypt/include/twofish.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 + diff --git a/externals/mysql/extlib/yassl/taocrypt/include/type_traits.hpp b/externals/mysql/extlib/yassl/taocrypt/include/type_traits.hpp new file mode 100644 index 00000000000..7a664c816b0 --- /dev/null +++ b/externals/mysql/extlib/yassl/taocrypt/include/type_traits.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 diff --git a/externals/mysql/extlib/yassl/taocrypt/include/types.hpp b/externals/mysql/extlib/yassl/taocrypt/include/types.hpp new file mode 100644 index 00000000000..3efdcdfbccb --- /dev/null +++ b/externals/mysql/extlib/yassl/taocrypt/include/types.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 diff --git a/externals/mysql/extlib/yassl/taocrypt/mySTL/algorithm.hpp b/externals/mysql/extlib/yassl/taocrypt/mySTL/algorithm.hpp new file mode 100644 index 00000000000..d8bc29a0bb9 --- /dev/null +++ b/externals/mysql/extlib/yassl/taocrypt/mySTL/algorithm.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 diff --git a/externals/mysql/extlib/yassl/taocrypt/mySTL/helpers.hpp b/externals/mysql/extlib/yassl/taocrypt/mySTL/helpers.hpp new file mode 100644 index 00000000000..c0495a71d84 --- /dev/null +++ b/externals/mysql/extlib/yassl/taocrypt/mySTL/helpers.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 diff --git a/externals/mysql/extlib/yassl/taocrypt/mySTL/list.hpp b/externals/mysql/extlib/yassl/taocrypt/mySTL/list.hpp new file mode 100644 index 00000000000..6a081cba5ad --- /dev/null +++ b/externals/mysql/extlib/yassl/taocrypt/mySTL/list.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 diff --git a/externals/mysql/extlib/yassl/taocrypt/mySTL/memory.hpp b/externals/mysql/extlib/yassl/taocrypt/mySTL/memory.hpp new file mode 100644 index 00000000000..b239aa6f907 --- /dev/null +++ b/externals/mysql/extlib/yassl/taocrypt/mySTL/memory.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 diff --git a/externals/mysql/extlib/yassl/taocrypt/mySTL/memory_array.hpp b/externals/mysql/extlib/yassl/taocrypt/mySTL/memory_array.hpp new file mode 100644 index 00000000000..a044498cd98 --- /dev/null +++ b/externals/mysql/extlib/yassl/taocrypt/mySTL/memory_array.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 diff --git a/externals/mysql/extlib/yassl/taocrypt/mySTL/pair.hpp b/externals/mysql/extlib/yassl/taocrypt/mySTL/pair.hpp new file mode 100644 index 00000000000..be2dbb14b88 --- /dev/null +++ b/externals/mysql/extlib/yassl/taocrypt/mySTL/pair.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 diff --git a/externals/mysql/extlib/yassl/taocrypt/mySTL/stdexcept.hpp b/externals/mysql/extlib/yassl/taocrypt/mySTL/stdexcept.hpp new file mode 100644 index 00000000000..9696995248d --- /dev/null +++ b/externals/mysql/extlib/yassl/taocrypt/mySTL/stdexcept.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 diff --git a/externals/mysql/extlib/yassl/taocrypt/mySTL/vector.hpp b/externals/mysql/extlib/yassl/taocrypt/mySTL/vector.hpp new file mode 100644 index 00000000000..8ba8813ca70 --- /dev/null +++ b/externals/mysql/extlib/yassl/taocrypt/mySTL/vector.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 diff --git a/externals/mysql/extlib/yassl/taocrypt/src/aes.cpp b/externals/mysql/extlib/yassl/taocrypt/src/aes.cpp new file mode 100644 index 00000000000..b2b42d3dcf0 --- /dev/null +++ b/externals/mysql/extlib/yassl/taocrypt/src/aes.cpp @@ -0,0 +1,1814 @@ +/* + 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++ based on Wei Dai's aes.cpp from CryptoPP */ +/* x86 asm original */ + +#if defined(TAOCRYPT_KERNEL_MODE) + #define DO_TAOCRYPT_KERNEL_MODE +#endif // only some modules now support this + +#include "runtime.hpp" +#include "aes.hpp" + + +namespace TaoCrypt { + + +#if defined(DO_AES_ASM) + +// ia32 optimized version +void AES::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--) { + if (dir_ == ENCRYPTION) + AsmEncrypt(in, out, (void*)Te0); + else + AsmDecrypt(in, out, (void*)Td0); + out += BLOCK_SIZE; + in += BLOCK_SIZE; + } + else if (mode_ == CBC) + if (dir_ == ENCRYPTION) + while (blocks--) { + r_[0] ^= *(word32*)in; + r_[1] ^= *(word32*)(in + 4); + r_[2] ^= *(word32*)(in + 8); + r_[3] ^= *(word32*)(in + 12); + + AsmEncrypt((byte*)r_, (byte*)r_, (void*)Te0); + + memcpy(out, r_, BLOCK_SIZE); + out += BLOCK_SIZE; + in += BLOCK_SIZE; + } + else + while (blocks--) { + AsmDecrypt(in, out, (void*)Td0); + + *(word32*)out ^= r_[0]; + *(word32*)(out + 4) ^= r_[1]; + *(word32*)(out + 8) ^= r_[2]; + *(word32*)(out + 12) ^= r_[3]; + + memcpy(r_, in, BLOCK_SIZE); + out += BLOCK_SIZE; + in += BLOCK_SIZE; + } +} + +#endif // DO_AES_ASM + + +void AES::SetKey(const byte* userKey, word32 keylen, CipherDir /*dummy*/) +{ + assert( (keylen == 16) || (keylen == 24) || (keylen == 32) ); + + rounds_ = keylen/4 + 6; + + word32 temp, *rk = key_; + unsigned int i=0; + + GetUserKey(BigEndianOrder, rk, keylen/4, userKey, keylen); + + switch(keylen) + { + case 16: + while (true) + { + temp = rk[3]; + rk[4] = rk[0] ^ + (Te4[GETBYTE(temp, 2)] & 0xff000000) ^ + (Te4[GETBYTE(temp, 1)] & 0x00ff0000) ^ + (Te4[GETBYTE(temp, 0)] & 0x0000ff00) ^ + (Te4[GETBYTE(temp, 3)] & 0x000000ff) ^ + rcon_[i]; + rk[5] = rk[1] ^ rk[4]; + rk[6] = rk[2] ^ rk[5]; + rk[7] = rk[3] ^ rk[6]; + if (++i == 10) + break; + rk += 4; + } + break; + + case 24: + while (true) // for (;;) here triggers a bug in VC60 SP4 w/ Pro Pack + { + temp = rk[ 5]; + rk[ 6] = rk[ 0] ^ + (Te4[GETBYTE(temp, 2)] & 0xff000000) ^ + (Te4[GETBYTE(temp, 1)] & 0x00ff0000) ^ + (Te4[GETBYTE(temp, 0)] & 0x0000ff00) ^ + (Te4[GETBYTE(temp, 3)] & 0x000000ff) ^ + rcon_[i]; + rk[ 7] = rk[ 1] ^ rk[ 6]; + rk[ 8] = rk[ 2] ^ rk[ 7]; + rk[ 9] = rk[ 3] ^ rk[ 8]; + if (++i == 8) + break; + rk[10] = rk[ 4] ^ rk[ 9]; + rk[11] = rk[ 5] ^ rk[10]; + rk += 6; + } + break; + + case 32: + while (true) + { + temp = rk[ 7]; + rk[ 8] = rk[ 0] ^ + (Te4[GETBYTE(temp, 2)] & 0xff000000) ^ + (Te4[GETBYTE(temp, 1)] & 0x00ff0000) ^ + (Te4[GETBYTE(temp, 0)] & 0x0000ff00) ^ + (Te4[GETBYTE(temp, 3)] & 0x000000ff) ^ + rcon_[i]; + rk[ 9] = rk[ 1] ^ rk[ 8]; + rk[10] = rk[ 2] ^ rk[ 9]; + rk[11] = rk[ 3] ^ rk[10]; + if (++i == 7) + break; + temp = rk[11]; + rk[12] = rk[ 4] ^ + (Te4[GETBYTE(temp, 3)] & 0xff000000) ^ + (Te4[GETBYTE(temp, 2)] & 0x00ff0000) ^ + (Te4[GETBYTE(temp, 1)] & 0x0000ff00) ^ + (Te4[GETBYTE(temp, 0)] & 0x000000ff); + rk[13] = rk[ 5] ^ rk[12]; + rk[14] = rk[ 6] ^ rk[13]; + rk[15] = rk[ 7] ^ rk[14]; + + rk += 8; + } + break; + } + + if (dir_ == DECRYPTION) + { + unsigned int i, j; + rk = key_; + + /* invert the order of the round keys: */ + for (i = 0, j = 4*rounds_; i < j; i += 4, j -= 4) { + temp = rk[i ]; rk[i ] = rk[j ]; rk[j ] = temp; + temp = rk[i + 1]; rk[i + 1] = rk[j + 1]; rk[j + 1] = temp; + temp = rk[i + 2]; rk[i + 2] = rk[j + 2]; rk[j + 2] = temp; + temp = rk[i + 3]; rk[i + 3] = rk[j + 3]; rk[j + 3] = temp; + } + // apply the inverse MixColumn transform to all round keys but the + // first and the last: + for (i = 1; i < rounds_; i++) { + rk += 4; + rk[0] = + Td0[Te4[GETBYTE(rk[0], 3)] & 0xff] ^ + Td1[Te4[GETBYTE(rk[0], 2)] & 0xff] ^ + Td2[Te4[GETBYTE(rk[0], 1)] & 0xff] ^ + Td3[Te4[GETBYTE(rk[0], 0)] & 0xff]; + rk[1] = + Td0[Te4[GETBYTE(rk[1], 3)] & 0xff] ^ + Td1[Te4[GETBYTE(rk[1], 2)] & 0xff] ^ + Td2[Te4[GETBYTE(rk[1], 1)] & 0xff] ^ + Td3[Te4[GETBYTE(rk[1], 0)] & 0xff]; + rk[2] = + Td0[Te4[GETBYTE(rk[2], 3)] & 0xff] ^ + Td1[Te4[GETBYTE(rk[2], 2)] & 0xff] ^ + Td2[Te4[GETBYTE(rk[2], 1)] & 0xff] ^ + Td3[Te4[GETBYTE(rk[2], 0)] & 0xff]; + rk[3] = + Td0[Te4[GETBYTE(rk[3], 3)] & 0xff] ^ + Td1[Te4[GETBYTE(rk[3], 2)] & 0xff] ^ + Td2[Te4[GETBYTE(rk[3], 1)] & 0xff] ^ + Td3[Te4[GETBYTE(rk[3], 0)] & 0xff]; + } + } +} + + +void AES::ProcessAndXorBlock(const byte* in, const byte* xOr, byte* out) const +{ + if (dir_ == ENCRYPTION) + encrypt(in, xOr, out); + else + decrypt(in, xOr, out); +} + + +typedef BlockGetAndPut<word32, BigEndian> gpBlock; + + +void AES::encrypt(const byte* inBlock, const byte* xorBlock, + byte* outBlock) const +{ + word32 s0, s1, s2, s3; + word32 t0, t1, t2, t3; + + const word32 *rk = key_; + /* + * map byte array block to cipher state + * and add initial round key: + */ + gpBlock::Get(inBlock)(s0)(s1)(s2)(s3); + s0 ^= rk[0]; + s1 ^= rk[1]; + s2 ^= rk[2]; + s3 ^= rk[3]; + + /* + * Nr - 1 full rounds: + */ + + unsigned int r = rounds_ >> 1; + for (;;) { + t0 = + Te0[GETBYTE(s0, 3)] ^ + Te1[GETBYTE(s1, 2)] ^ + Te2[GETBYTE(s2, 1)] ^ + Te3[GETBYTE(s3, 0)] ^ + rk[4]; + t1 = + Te0[GETBYTE(s1, 3)] ^ + Te1[GETBYTE(s2, 2)] ^ + Te2[GETBYTE(s3, 1)] ^ + Te3[GETBYTE(s0, 0)] ^ + rk[5]; + t2 = + Te0[GETBYTE(s2, 3)] ^ + Te1[GETBYTE(s3, 2)] ^ + Te2[GETBYTE(s0, 1)] ^ + Te3[GETBYTE(s1, 0)] ^ + rk[6]; + t3 = + Te0[GETBYTE(s3, 3)] ^ + Te1[GETBYTE(s0, 2)] ^ + Te2[GETBYTE(s1, 1)] ^ + Te3[GETBYTE(s2, 0)] ^ + rk[7]; + + rk += 8; + if (--r == 0) { + break; + } + + s0 = + Te0[GETBYTE(t0, 3)] ^ + Te1[GETBYTE(t1, 2)] ^ + Te2[GETBYTE(t2, 1)] ^ + Te3[GETBYTE(t3, 0)] ^ + rk[0]; + s1 = + Te0[GETBYTE(t1, 3)] ^ + Te1[GETBYTE(t2, 2)] ^ + Te2[GETBYTE(t3, 1)] ^ + Te3[GETBYTE(t0, 0)] ^ + rk[1]; + s2 = + Te0[GETBYTE(t2, 3)] ^ + Te1[GETBYTE(t3, 2)] ^ + Te2[GETBYTE(t0, 1)] ^ + Te3[GETBYTE(t1, 0)] ^ + rk[2]; + s3 = + Te0[GETBYTE(t3, 3)] ^ + Te1[GETBYTE(t0, 2)] ^ + Te2[GETBYTE(t1, 1)] ^ + Te3[GETBYTE(t2, 0)] ^ + rk[3]; + } + + /* + * apply last round and + * map cipher state to byte array block: + */ + + s0 = + (Te4[GETBYTE(t0, 3)] & 0xff000000) ^ + (Te4[GETBYTE(t1, 2)] & 0x00ff0000) ^ + (Te4[GETBYTE(t2, 1)] & 0x0000ff00) ^ + (Te4[GETBYTE(t3, 0)] & 0x000000ff) ^ + rk[0]; + s1 = + (Te4[GETBYTE(t1, 3)] & 0xff000000) ^ + (Te4[GETBYTE(t2, 2)] & 0x00ff0000) ^ + (Te4[GETBYTE(t3, 1)] & 0x0000ff00) ^ + (Te4[GETBYTE(t0, 0)] & 0x000000ff) ^ + rk[1]; + s2 = + (Te4[GETBYTE(t2, 3)] & 0xff000000) ^ + (Te4[GETBYTE(t3, 2)] & 0x00ff0000) ^ + (Te4[GETBYTE(t0, 1)] & 0x0000ff00) ^ + (Te4[GETBYTE(t1, 0)] & 0x000000ff) ^ + rk[2]; + s3 = + (Te4[GETBYTE(t3, 3)] & 0xff000000) ^ + (Te4[GETBYTE(t0, 2)] & 0x00ff0000) ^ + (Te4[GETBYTE(t1, 1)] & 0x0000ff00) ^ + (Te4[GETBYTE(t2, 0)] & 0x000000ff) ^ + rk[3]; + + + gpBlock::Put(xorBlock, outBlock)(s0)(s1)(s2)(s3); +} + + +void AES::decrypt(const byte* inBlock, const byte* xorBlock, + byte* outBlock) const +{ + word32 s0, s1, s2, s3; + word32 t0, t1, t2, t3; + const word32* rk = key_; + + /* + * map byte array block to cipher state + * and add initial round key: + */ + gpBlock::Get(inBlock)(s0)(s1)(s2)(s3); + s0 ^= rk[0]; + s1 ^= rk[1]; + s2 ^= rk[2]; + s3 ^= rk[3]; + + /* + * Nr - 1 full rounds: + */ + + unsigned int r = rounds_ >> 1; + for (;;) { + t0 = + Td0[GETBYTE(s0, 3)] ^ + Td1[GETBYTE(s3, 2)] ^ + Td2[GETBYTE(s2, 1)] ^ + Td3[GETBYTE(s1, 0)] ^ + rk[4]; + t1 = + Td0[GETBYTE(s1, 3)] ^ + Td1[GETBYTE(s0, 2)] ^ + Td2[GETBYTE(s3, 1)] ^ + Td3[GETBYTE(s2, 0)] ^ + rk[5]; + t2 = + Td0[GETBYTE(s2, 3)] ^ + Td1[GETBYTE(s1, 2)] ^ + Td2[GETBYTE(s0, 1)] ^ + Td3[GETBYTE(s3, 0)] ^ + rk[6]; + t3 = + Td0[GETBYTE(s3, 3)] ^ + Td1[GETBYTE(s2, 2)] ^ + Td2[GETBYTE(s1, 1)] ^ + Td3[GETBYTE(s0, 0)] ^ + rk[7]; + + rk += 8; + if (--r == 0) { + break; + } + + s0 = + Td0[GETBYTE(t0, 3)] ^ + Td1[GETBYTE(t3, 2)] ^ + Td2[GETBYTE(t2, 1)] ^ + Td3[GETBYTE(t1, 0)] ^ + rk[0]; + s1 = + Td0[GETBYTE(t1, 3)] ^ + Td1[GETBYTE(t0, 2)] ^ + Td2[GETBYTE(t3, 1)] ^ + Td3[GETBYTE(t2, 0)] ^ + rk[1]; + s2 = + Td0[GETBYTE(t2, 3)] ^ + Td1[GETBYTE(t1, 2)] ^ + Td2[GETBYTE(t0, 1)] ^ + Td3[GETBYTE(t3, 0)] ^ + rk[2]; + s3 = + Td0[GETBYTE(t3, 3)] ^ + Td1[GETBYTE(t2, 2)] ^ + Td2[GETBYTE(t1, 1)] ^ + Td3[GETBYTE(t0, 0)] ^ + rk[3]; + } + /* + * apply last round and + * map cipher state to byte array block: + */ + s0 = + (Td4[GETBYTE(t0, 3)] & 0xff000000) ^ + (Td4[GETBYTE(t3, 2)] & 0x00ff0000) ^ + (Td4[GETBYTE(t2, 1)] & 0x0000ff00) ^ + (Td4[GETBYTE(t1, 0)] & 0x000000ff) ^ + rk[0]; + s1 = + (Td4[GETBYTE(t1, 3)] & 0xff000000) ^ + (Td4[GETBYTE(t0, 2)] & 0x00ff0000) ^ + (Td4[GETBYTE(t3, 1)] & 0x0000ff00) ^ + (Td4[GETBYTE(t2, 0)] & 0x000000ff) ^ + rk[1]; + s2 = + (Td4[GETBYTE(t2, 3)] & 0xff000000) ^ + (Td4[GETBYTE(t1, 2)] & 0x00ff0000) ^ + (Td4[GETBYTE(t0, 1)] & 0x0000ff00) ^ + (Td4[GETBYTE(t3, 0)] & 0x000000ff) ^ + rk[2]; + s3 = + (Td4[GETBYTE(t3, 3)] & 0xff000000) ^ + (Td4[GETBYTE(t2, 2)] & 0x00ff0000) ^ + (Td4[GETBYTE(t1, 1)] & 0x0000ff00) ^ + (Td4[GETBYTE(t0, 0)] & 0x000000ff) ^ + rk[3]; + + gpBlock::Put(xorBlock, outBlock)(s0)(s1)(s2)(s3); +} + + +#if defined(DO_AES_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( sub esp, 4 ) \ + AS2( movd mm7, ebp ) \ + AS2( mov [ebp - 4], esi ) \ + AS2( mov ecx, DWORD PTR [ebp + 8] ) \ + AS2( mov esi, DWORD PTR [ebp + 12] ) \ + AS2( mov ebp, DWORD PTR [ebp + 20] ) + + #define EPILOG() \ + AS2( mov esi, [ebp - 4] ) \ + AS2( mov esp, ebp ) \ + 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( sub esp, 4 ) \ + AS2( movd mm7, ebp ) \ + AS2( mov [ebp - 4], esi ) \ + AS2( mov esi, DWORD PTR [ebp + 8] ) \ + AS2( mov ebp, DWORD PTR [ebp + 16] ) + + // ebp is restored at end + #define EPILOG() \ + AS2( mov esi, [ebp - 4] ) \ + AS2( movd ebx, mm4 ) \ + AS2( movd edi, mm3 ) \ + AS2( mov esp, ebp ) \ + AS1( pop ebp ) \ + AS1( emms ) \ + AS1( ret 12 ) + + + #endif + + +#ifdef _MSC_VER + __declspec(naked) +#endif +void AES::AsmEncrypt(const byte* inBlock, byte* outBlock, void* boxes) const +{ + + PROLOG() + + #ifdef OLD_GCC_OFFSET + AS2( mov edx, DWORD PTR [ecx + 60] ) // rounds + AS2( lea edi, [ecx + 64] ) // rk + #else + AS2( mov edx, DWORD PTR [ecx + 56] ) // rounds + AS2( lea edi, [ecx + 60] ) // rk + #endif + + AS1( dec edx ) + AS2( movd mm6, edi ) // save rk + AS2( movd mm5, edx ) // save rounds + + AS2( mov eax, DWORD PTR [esi] ) + AS2( mov ebx, DWORD PTR [esi + 4] ) + AS2( mov ecx, DWORD PTR [esi + 8] ) + AS2( mov edx, DWORD PTR [esi + 12] ) + + AS1( bswap eax ) + AS1( bswap ebx ) + AS1( bswap ecx ) + AS1( bswap edx ) + + AS2( xor eax, DWORD PTR [edi] ) // s0 + AS2( xor ebx, DWORD PTR [edi + 4] ) // s1 + AS2( xor ecx, DWORD PTR [edi + 8] ) // s2 + AS2( xor edx, DWORD PTR [edi + 12] ) // s3 + + AS1(loop1: ) + /* Put0 (mm0) = + Te0[get0,rs 24] ^ + Te1[get1,rs 16] ^ + Te2[get2,rs 8] ^ + Te3[get3,rs 0] + */ + + AS2( mov esi, eax ) + AS2( shr esi, 24 ) + AS2( mov esi, DWORD PTR [ebp + esi*4] ) + + AS2( mov edi, ebx ) + AS2( shr edi, 16 ) + AS2( and edi, 255 ) + AS2( xor esi, DWORD PTR [ebp + 1024 + edi*4] ) + + AS2( movzx edi, ch ) + AS2( xor esi, DWORD PTR [ebp + 2048 + edi*4] ) + + AS2( movzx edi, dl ) + AS2( xor esi, DWORD PTR [ebp + 3072 + edi*4] ) + + AS2( movd mm0, esi ) + + /* Put1 (mm1) = + Te0[get1,rs 24] ^ + Te1[get2,rs 16] ^ + Te2[get3,rs 8] ^ + Te3[get0,rs 0] + */ + + AS2( mov esi, ebx ) + AS2( shr esi, 24 ) + AS2( mov esi, DWORD PTR [ebp + esi*4] ) + + AS2( mov edi, ecx ) + AS2( shr edi, 16 ) + AS2( and edi, 255 ) + AS2( xor esi, DWORD PTR [ebp + 1024 + edi*4] ) + + AS2( movzx edi, dh ) + AS2( xor esi, DWORD PTR [ebp + 2048 + edi*4] ) + + AS2( movzx edi, al ) + AS2( xor esi, DWORD PTR [ebp + 3072 + edi*4] ) + + AS2( movd mm1, esi ) + + + /* Put2 (mm2) = + Te0[get2,rs 24] ^ + Te1[get3,rs 16] ^ + Te2[get0,rs 8] ^ + Te3[get1,rs 0] + */ + + AS2( mov esi, ecx ) + AS2( shr esi, 24 ) + AS2( mov esi, DWORD PTR [ebp + esi*4] ) + + AS2( mov edi, edx ) + AS2( shr edi, 16 ) + AS2( and edi, 255 ) + AS2( xor esi, DWORD PTR [ebp + 1024 + edi*4] ) + + AS2( movzx edi, ah ) + AS2( xor esi, DWORD PTR [ebp + 2048 + edi*4] ) + + AS2( movzx edi, bl ) + AS2( xor esi, DWORD PTR [ebp + 3072 + edi*4] ) + + AS2( movd mm2, esi ) + + /* Put3 (edx) = + Te0[get3,rs 24] ^ + Te1[get0,rs 16] ^ + Te2[get1,rs 8] ^ + Te3[get2,rs 0] + */ + + AS2( mov esi, edx ) + AS2( shr esi, 24 ) + AS2( mov edx, DWORD PTR [ebp + esi*4] ) + + AS2( mov edi, eax ) + AS2( shr edi, 16 ) + AS2( and edi, 255 ) + AS2( xor edx, DWORD PTR [ebp + 1024 + edi*4] ) + + AS2( movzx esi, bh ) + AS2( xor edx, DWORD PTR [ebp + 2048 + esi*4] ) + + AS2( movzx edi, cl ) + AS2( xor edx, DWORD PTR [ebp + 3072 + edi*4] ) + + // xOr + + AS2( movd esi, mm6 ) // rk + + AS2( movd eax, mm0 ) + AS2( add esi, 16 ) + AS2( movd ebx, mm1 ) + AS2( movd mm6, esi ) // save back + AS2( movd ecx, mm2 ) + + AS2( xor eax, DWORD PTR [esi] ) + AS2( xor ebx, DWORD PTR [esi + 4] ) + AS2( movd edi, mm5 ) + AS2( xor ecx, DWORD PTR [esi + 8] ) + AS2( xor edx, DWORD PTR [esi + 12] ) + + AS1( dec edi ) + AS2( movd mm5, edi ) + + AS1( jnz loop1 ) + + // last round + /* + Put0 (mm0) = + (Te4[get0, rs24] & 0xff000000) ^ h = 4278190080 + (Te4[get1, rs16] & 0x00ff0000) ^ h = 16711680 + (Te4[get2, rs 8] & 0x0000ff00) ^ h = 65280 + (Te4[get3, rs 0] & 0x000000ff) h = 255 + */ + AS2( mov esi, eax ) + AS2( shr esi, 24 ) + AS2( mov esi, DWORD PTR [ebp + 4096 + esi*4] ) + AS2( and esi, 4278190080 ) + + AS2( mov edi, ebx ) + AS2( shr edi, 16 ) + AS2( and edi, 255 ) + AS2( mov edi, DWORD PTR [ebp + 4096 + edi*4] ) + AS2( and edi, 16711680 ) + AS2( xor esi, edi ) + + AS2( movzx edi, ch ) + AS2( mov edi, DWORD PTR [ebp + 4096 + edi*4] ) + AS2( and edi, 65280 ) + AS2( xor esi, edi ) + + AS2( movzx edi, dl ) + AS2( mov edi, DWORD PTR [ebp + 4096 + edi*4] ) + AS2( and edi, 255 ) + AS2( xor esi, edi ) + + AS2( movd mm0, esi ) + + /* + Put1 (mm1) = + (Te4[get1, rs24] & 0xff000000) ^ h = 4278190080 + (Te4[get2, rs16] & 0x00ff0000) ^ h = 16711680 + (Te4[get3, rs 8] & 0x0000ff00) ^ h = 65280 + (Te4[get0, rs 0] & 0x000000ff) h = 255 + */ + AS2( mov esi, ebx ) + AS2( shr esi, 24 ) + AS2( mov esi, DWORD PTR [ebp + 4096 + esi*4] ) + AS2( and esi, 4278190080 ) + + AS2( mov edi, ecx ) + AS2( shr edi, 16 ) + AS2( and edi, 255 ) + AS2( mov edi, DWORD PTR [ebp + 4096 + edi*4] ) + AS2( and edi, 16711680 ) + AS2( xor esi, edi ) + + AS2( movzx edi, dh ) + AS2( mov edi, DWORD PTR [ebp + 4096 + edi*4] ) + AS2( and edi, 65280 ) + AS2( xor esi, edi ) + + AS2( movzx edi, al ) + AS2( mov edi, DWORD PTR [ebp + 4096 + edi*4] ) + AS2( and edi, 255 ) + AS2( xor esi, edi ) + + AS2( movd mm1, esi ) + + /* + Put2 (mm2) = + (Te4[get2, rs24] & 0xff000000) ^ h = 4278190080 + (Te4[get3, rs16] & 0x00ff0000) ^ h = 16711680 + (Te4[get0, rs 8] & 0x0000ff00) ^ h = 65280 + (Te4[get1, rs 0] & 0x000000ff) h = 255 + */ + AS2( mov esi, ecx ) + AS2( shr esi, 24 ) + AS2( mov esi, DWORD PTR [ebp + 4096 + esi*4] ) + AS2( and esi, 4278190080 ) + + AS2( mov edi, edx ) + AS2( shr edi, 16 ) + AS2( and edi, 255 ) + AS2( mov edi, DWORD PTR [ebp + 4096 + edi*4] ) + AS2( and edi, 16711680 ) + AS2( xor esi, edi ) + + AS2( movzx edi, ah ) + AS2( mov edi, DWORD PTR [ebp + 4096 + edi*4] ) + AS2( and edi, 65280 ) + AS2( xor esi, edi ) + + AS2( movzx edi, bl ) + AS2( mov edi, DWORD PTR [ebp + 4096 + edi*4] ) + AS2( and edi, 255 ) + AS2( xor esi, edi ) + + AS2( movd mm2, esi ) + + /* + Put3 (edx) = + (Te4[get3, rs24] & 0xff000000) ^ h = 4278190080 + (Te4[get0, rs16] & 0x00ff0000) ^ h = 16711680 + (Te4[get1, rs 8] & 0x0000ff00) ^ h = 65280 + (Te4[get2, rs 0] & 0x000000ff) h = 255 + */ + AS2( mov esi, edx ) + AS2( shr esi, 24 ) + AS2( mov edx, DWORD PTR [ebp + 4096 + esi*4] ) + AS2( and edx, 4278190080 ) + + AS2( mov edi, eax ) + AS2( shr edi, 16 ) + AS2( and edi, 255 ) + AS2( mov esi, DWORD PTR [ebp + 4096 + edi*4] ) + AS2( and esi, 16711680 ) + AS2( xor edx, esi ) + + AS2( movzx esi, bh ) + AS2( mov edi, DWORD PTR [ebp + 4096 + esi*4] ) + AS2( and edi, 65280 ) + AS2( xor edx, edi ) + + AS2( movzx edi, cl ) + AS2( mov esi, DWORD PTR [ebp + 4096 + edi*4] ) + AS2( and esi, 255 ) + AS2( xor edx, esi ) + + + // xOr + AS2( movd eax, mm0 ) + AS2( movd esi, mm6 ) // rk + AS2( movd ebx, mm1 ) + AS2( add esi, 16 ) + AS2( movd ecx, mm2 ) + + AS2( xor eax, DWORD PTR [esi] ) + AS2( xor ebx, DWORD PTR [esi + 4] ) + AS2( xor ecx, DWORD PTR [esi + 8] ) + AS2( xor edx, DWORD PTR [esi + 12] ) + + // end + AS2( movd ebp, mm7 ) + + // swap + AS1( bswap eax ) + AS1( bswap ebx ) + + // store + #ifdef __GNUC__ + AS2( mov esi, DWORD PTR [ebp + 16] ) // outBlock + #else + AS2( mov esi, DWORD PTR [ebp + 12] ) // outBlock + #endif + + AS1( bswap ecx ) + AS1( bswap edx ) + + AS2( mov DWORD PTR [esi], eax ) + AS2( mov DWORD PTR [esi + 4], ebx ) + AS2( mov DWORD PTR [esi + 8], ecx ) + AS2( mov DWORD PTR [esi + 12], edx ) + + + EPILOG() +} + + +#ifdef _MSC_VER + __declspec(naked) +#endif +void AES::AsmDecrypt(const byte* inBlock, byte* outBlock, void* boxes) const +{ + + PROLOG() + + #ifdef OLD_GCC_OFFSET + AS2( mov edx, DWORD PTR [ecx + 60] ) // rounds + AS2( lea edi, [ecx + 64] ) // rk + #else + AS2( mov edx, DWORD PTR [ecx + 56] ) // rounds + AS2( lea edi, [ecx + 60] ) // rk + #endif + + AS1( dec edx ) + AS2( movd mm6, edi ) // save rk + AS2( movd mm5, edx ) // save rounds + + AS2( mov eax, DWORD PTR [esi] ) + AS2( mov ebx, DWORD PTR [esi + 4] ) + AS2( mov ecx, DWORD PTR [esi + 8] ) + AS2( mov edx, DWORD PTR [esi + 12] ) + + AS1( bswap eax ) + AS1( bswap ebx ) + AS1( bswap ecx ) + AS1( bswap edx ) + + AS2( xor eax, DWORD PTR [edi] ) // s0 + AS2( xor ebx, DWORD PTR [edi + 4] ) // s1 + AS2( xor ecx, DWORD PTR [edi + 8] ) // s2 + AS2( xor edx, DWORD PTR [edi + 12] ) // s3 + + + AS1(loop2: ) + /* Put0 (mm0) = + Td0[GETBYTE(get0, rs24)] ^ + Td1[GETBYTE(get3, rs16)] ^ + Td2[GETBYTE(get2, rs 8)] ^ + Td3[GETBYTE(tet1, )] + */ + AS2( mov esi, eax ) + AS2( shr esi, 24 ) + AS2( mov esi, DWORD PTR [ebp + esi*4] ) + + AS2( mov edi, edx ) + AS2( shr edi, 16 ) + AS2( and edi, 255 ) + AS2( xor esi, DWORD PTR [ebp + 1024 + edi*4] ) + + AS2( movzx edi, ch ) + AS2( xor esi, DWORD PTR [ebp + 2048 + edi*4] ) + + AS2( movzx edi, bl ) + AS2( xor esi, DWORD PTR [ebp + 3072 + edi*4] ) + + AS2( movd mm0, esi ) + + /* Put1 (mm1) = + Td0[GETBYTE(get1, rs24)] ^ + Td1[GETBYTE(get0, rs16)] ^ + Td2[GETBYTE(get3, rs 8)] ^ + Td3[GETBYTE(tet2, )] + */ + AS2( mov esi, ebx ) + AS2( shr esi, 24 ) + AS2( mov esi, DWORD PTR [ebp + esi*4] ) + + AS2( mov edi, eax ) + AS2( shr edi, 16 ) + AS2( and edi, 255 ) + AS2( xor esi, DWORD PTR [ebp + 1024 + edi*4] ) + + AS2( movzx edi, dh ) + AS2( xor esi, DWORD PTR [ebp + 2048 + edi*4] ) + + AS2( movzx edi, cl ) + AS2( xor esi, DWORD PTR [ebp + 3072 + edi*4] ) + + AS2( movd mm1, esi ) + + /* Put2 (mm2) = + Td0[GETBYTE(get2, rs24)] ^ + Td1[GETBYTE(get1, rs16)] ^ + Td2[GETBYTE(get0, rs 8)] ^ + Td3[GETBYTE(tet3, )] + */ + AS2( mov esi, ecx ) + AS2( shr esi, 24 ) + AS2( mov esi, DWORD PTR [ebp + esi*4] ) + + AS2( mov edi, ebx ) + AS2( shr edi, 16 ) + AS2( and edi, 255 ) + AS2( xor esi, DWORD PTR [ebp + 1024 + edi*4] ) + + AS2( movzx edi, ah ) + AS2( xor esi, DWORD PTR [ebp + 2048 + edi*4] ) + + AS2( movzx edi, dl ) + AS2( xor esi, DWORD PTR [ebp + 3072 + edi*4] ) + + AS2( movd mm2, esi ) + + /* Put3 (edx) = + Td0[GETBYTE(get3, rs24)] ^ + Td1[GETBYTE(get2, rs16)] ^ + Td2[GETBYTE(get1, rs 8)] ^ + Td3[GETBYTE(tet0, )] + */ + AS2( mov esi, edx ) + AS2( shr esi, 24 ) + AS2( mov edx, DWORD PTR [ebp + esi*4] ) + + AS2( mov edi, ecx ) + AS2( shr edi, 16 ) + AS2( and edi, 255 ) + AS2( xor edx, DWORD PTR [ebp + 1024 + edi*4] ) + + AS2( movzx esi, bh ) + AS2( xor edx, DWORD PTR [ebp + 2048 + esi*4] ) + + AS2( movzx edi, al ) + AS2( xor edx, DWORD PTR [ebp + 3072 + edi*4] ) + + + // xOr + + AS2( movd esi, mm6 ) // rk + AS2( add esi, 16 ) + AS2( movd mm6, esi ) // save back + + AS2( movd eax, mm0 ) + AS2( movd ebx, mm1 ) + AS2( movd ecx, mm2 ) + + AS2( xor eax, DWORD PTR [esi] ) + AS2( xor ebx, DWORD PTR [esi + 4] ) + AS2( xor ecx, DWORD PTR [esi + 8] ) + AS2( xor edx, DWORD PTR [esi + 12] ) + + AS2( movd edi, mm5 ) + AS1( dec edi ) + AS2( movd mm5, edi ) + + AS1( jnz loop2 ) + + // last round + /* + Put0 (mm0) = + (Td4[get0, rs24] & 0xff000000) ^ h = 4278190080 + (Td4[get3, rs16] & 0x00ff0000) ^ h = 16711680 + (Td4[get2, rs 8] & 0x0000ff00) ^ h = 65280 + (Td4[get1, rs 0] & 0x000000ff) h = 255 + */ + AS2( mov esi, eax ) + AS2( shr esi, 24 ) + AS2( mov esi, DWORD PTR [ebp + 4096 + esi*4] ) + AS2( and esi, 4278190080 ) + + AS2( mov edi, edx ) + AS2( shr edi, 16 ) + AS2( and edi, 255 ) + AS2( mov edi, DWORD PTR [ebp + 4096 + edi*4] ) + AS2( and edi, 16711680 ) + AS2( xor esi, edi ) + + AS2( movzx edi, ch ) + AS2( mov edi, DWORD PTR [ebp + 4096 + edi*4] ) + AS2( and edi, 65280 ) + AS2( xor esi, edi ) + + AS2( movzx edi, bl ) + AS2( mov edi, DWORD PTR [ebp + 4096 + edi*4] ) + AS2( and edi, 255 ) + AS2( xor esi, edi ) + + AS2( movd mm0, esi ) + + /* + Put1 (mm1) = + (Td4[get1, rs24] & 0xff000000) ^ h = 4278190080 + (Td4[get0, rs16] & 0x00ff0000) ^ h = 16711680 + (Td4[get3, rs 8] & 0x0000ff00) ^ h = 65280 + (Td4[get2, rs 0] & 0x000000ff) h = 255 + */ + AS2( mov esi, ebx ) + AS2( shr esi, 24 ) + AS2( mov esi, DWORD PTR [ebp + 4096 + esi*4] ) + AS2( and esi, 4278190080 ) + + AS2( mov edi, eax ) + AS2( shr edi, 16 ) + AS2( and edi, 255 ) + AS2( mov edi, DWORD PTR [ebp + 4096 + edi*4] ) + AS2( and edi, 16711680 ) + AS2( xor esi, edi ) + + AS2( movzx edi, dh ) + AS2( mov edi, DWORD PTR [ebp + 4096 + edi*4] ) + AS2( and edi, 65280 ) + AS2( xor esi, edi ) + + AS2( movzx edi, cl ) + AS2( mov edi, DWORD PTR [ebp + 4096 + edi*4] ) + AS2( and edi, 255 ) + AS2( xor esi, edi ) + + AS2( movd mm1, esi ) + + /* + Put2 (mm2) = + (Td4[get2, rs24] & 0xff000000) ^ h = 4278190080 + (Td4[get1, rs16] & 0x00ff0000) ^ h = 16711680 + (Td4[get0, rs 8] & 0x0000ff00) ^ h = 65280 + (Td4[get3, rs 0] & 0x000000ff) h = 255 + */ + AS2( mov esi, ecx ) + AS2( shr esi, 24 ) + AS2( mov esi, DWORD PTR [ebp + 4096 + esi*4] ) + AS2( and esi, 4278190080 ) + + AS2( mov edi, ebx ) + AS2( shr edi, 16 ) + AS2( and edi, 255 ) + AS2( mov edi, DWORD PTR [ebp + 4096 + edi*4] ) + AS2( and edi, 16711680 ) + AS2( xor esi, edi ) + + AS2( movzx edi, ah ) + AS2( mov edi, DWORD PTR [ebp + 4096 + edi*4] ) + AS2( and edi, 65280 ) + AS2( xor esi, edi ) + + AS2( movzx edi, dl ) + AS2( mov edi, DWORD PTR [ebp + 4096 + edi*4] ) + AS2( and edi, 255 ) + AS2( xor esi, edi ) + + AS2( movd mm2, esi ) + + /* + Put3 (edx) = + (Td4[get3, rs24] & 0xff000000) ^ h = 4278190080 + (Td4[get2, rs16] & 0x00ff0000) ^ h = 16711680 + (Td4[get1, rs 8] & 0x0000ff00) ^ h = 65280 + (Td4[get0, rs 0] & 0x000000ff) h = 255 + */ + AS2( mov esi, edx ) + AS2( shr esi, 24 ) + AS2( mov edx, DWORD PTR [ebp + 4096 + esi*4] ) + AS2( and edx, 4278190080 ) + + AS2( mov edi, ecx ) + AS2( shr edi, 16 ) + AS2( and edi, 255 ) + AS2( mov esi, DWORD PTR [ebp + 4096 + edi*4] ) + AS2( and esi, 16711680 ) + AS2( xor edx, esi ) + + AS2( movzx esi, bh ) + AS2( mov edi, DWORD PTR [ebp + 4096 + esi*4] ) + AS2( and edi, 65280 ) + AS2( xor edx, edi ) + + AS2( movzx edi, al ) + AS2( mov esi, DWORD PTR [ebp + 4096 + edi*4] ) + AS2( and esi, 255 ) + AS2( xor edx, esi ) + + + // xOr + AS2( movd esi, mm6 ) // rk + AS2( add esi, 16 ) + + AS2( movd eax, mm0 ) + AS2( movd ebx, mm1 ) + AS2( movd ecx, mm2 ) + + AS2( xor eax, DWORD PTR [esi] ) + AS2( xor ebx, DWORD PTR [esi + 4] ) + AS2( xor ecx, DWORD PTR [esi + 8] ) + AS2( xor edx, DWORD PTR [esi + 12] ) + + // end + AS2( movd ebp, mm7 ) + + // swap + AS1( bswap eax ) + AS1( bswap ebx ) + AS1( bswap ecx ) + AS1( bswap edx ) + + // store + #ifdef __GNUC__ + AS2( mov esi, DWORD PTR [ebp + 16] ) // outBlock + #else + AS2( mov esi, DWORD PTR [ebp + 12] ) // outBlock + #endif + AS2( mov DWORD PTR [esi], eax ) + AS2( mov DWORD PTR [esi + 4], ebx ) + AS2( mov DWORD PTR [esi + 8], ecx ) + AS2( mov DWORD PTR [esi + 12], edx ) + + + EPILOG() +} + + + +#endif // defined(DO_AES_ASM) + + + +const word32 AES::Te[5][256] = { +{ + 0xc66363a5U, 0xf87c7c84U, 0xee777799U, 0xf67b7b8dU, + 0xfff2f20dU, 0xd66b6bbdU, 0xde6f6fb1U, 0x91c5c554U, + 0x60303050U, 0x02010103U, 0xce6767a9U, 0x562b2b7dU, + 0xe7fefe19U, 0xb5d7d762U, 0x4dababe6U, 0xec76769aU, + 0x8fcaca45U, 0x1f82829dU, 0x89c9c940U, 0xfa7d7d87U, + 0xeffafa15U, 0xb25959ebU, 0x8e4747c9U, 0xfbf0f00bU, + 0x41adadecU, 0xb3d4d467U, 0x5fa2a2fdU, 0x45afafeaU, + 0x239c9cbfU, 0x53a4a4f7U, 0xe4727296U, 0x9bc0c05bU, + 0x75b7b7c2U, 0xe1fdfd1cU, 0x3d9393aeU, 0x4c26266aU, + 0x6c36365aU, 0x7e3f3f41U, 0xf5f7f702U, 0x83cccc4fU, + 0x6834345cU, 0x51a5a5f4U, 0xd1e5e534U, 0xf9f1f108U, + 0xe2717193U, 0xabd8d873U, 0x62313153U, 0x2a15153fU, + 0x0804040cU, 0x95c7c752U, 0x46232365U, 0x9dc3c35eU, + 0x30181828U, 0x379696a1U, 0x0a05050fU, 0x2f9a9ab5U, + 0x0e070709U, 0x24121236U, 0x1b80809bU, 0xdfe2e23dU, + 0xcdebeb26U, 0x4e272769U, 0x7fb2b2cdU, 0xea75759fU, + 0x1209091bU, 0x1d83839eU, 0x582c2c74U, 0x341a1a2eU, + 0x361b1b2dU, 0xdc6e6eb2U, 0xb45a5aeeU, 0x5ba0a0fbU, + 0xa45252f6U, 0x763b3b4dU, 0xb7d6d661U, 0x7db3b3ceU, + 0x5229297bU, 0xdde3e33eU, 0x5e2f2f71U, 0x13848497U, + 0xa65353f5U, 0xb9d1d168U, 0x00000000U, 0xc1eded2cU, + 0x40202060U, 0xe3fcfc1fU, 0x79b1b1c8U, 0xb65b5bedU, + 0xd46a6abeU, 0x8dcbcb46U, 0x67bebed9U, 0x7239394bU, + 0x944a4adeU, 0x984c4cd4U, 0xb05858e8U, 0x85cfcf4aU, + 0xbbd0d06bU, 0xc5efef2aU, 0x4faaaae5U, 0xedfbfb16U, + 0x864343c5U, 0x9a4d4dd7U, 0x66333355U, 0x11858594U, + 0x8a4545cfU, 0xe9f9f910U, 0x04020206U, 0xfe7f7f81U, + 0xa05050f0U, 0x783c3c44U, 0x259f9fbaU, 0x4ba8a8e3U, + 0xa25151f3U, 0x5da3a3feU, 0x804040c0U, 0x058f8f8aU, + 0x3f9292adU, 0x219d9dbcU, 0x70383848U, 0xf1f5f504U, + 0x63bcbcdfU, 0x77b6b6c1U, 0xafdada75U, 0x42212163U, + 0x20101030U, 0xe5ffff1aU, 0xfdf3f30eU, 0xbfd2d26dU, + 0x81cdcd4cU, 0x180c0c14U, 0x26131335U, 0xc3ecec2fU, + 0xbe5f5fe1U, 0x359797a2U, 0x884444ccU, 0x2e171739U, + 0x93c4c457U, 0x55a7a7f2U, 0xfc7e7e82U, 0x7a3d3d47U, + 0xc86464acU, 0xba5d5de7U, 0x3219192bU, 0xe6737395U, + 0xc06060a0U, 0x19818198U, 0x9e4f4fd1U, 0xa3dcdc7fU, + 0x44222266U, 0x542a2a7eU, 0x3b9090abU, 0x0b888883U, + 0x8c4646caU, 0xc7eeee29U, 0x6bb8b8d3U, 0x2814143cU, + 0xa7dede79U, 0xbc5e5ee2U, 0x160b0b1dU, 0xaddbdb76U, + 0xdbe0e03bU, 0x64323256U, 0x743a3a4eU, 0x140a0a1eU, + 0x924949dbU, 0x0c06060aU, 0x4824246cU, 0xb85c5ce4U, + 0x9fc2c25dU, 0xbdd3d36eU, 0x43acacefU, 0xc46262a6U, + 0x399191a8U, 0x319595a4U, 0xd3e4e437U, 0xf279798bU, + 0xd5e7e732U, 0x8bc8c843U, 0x6e373759U, 0xda6d6db7U, + 0x018d8d8cU, 0xb1d5d564U, 0x9c4e4ed2U, 0x49a9a9e0U, + 0xd86c6cb4U, 0xac5656faU, 0xf3f4f407U, 0xcfeaea25U, + 0xca6565afU, 0xf47a7a8eU, 0x47aeaee9U, 0x10080818U, + 0x6fbabad5U, 0xf0787888U, 0x4a25256fU, 0x5c2e2e72U, + 0x381c1c24U, 0x57a6a6f1U, 0x73b4b4c7U, 0x97c6c651U, + 0xcbe8e823U, 0xa1dddd7cU, 0xe874749cU, 0x3e1f1f21U, + 0x964b4bddU, 0x61bdbddcU, 0x0d8b8b86U, 0x0f8a8a85U, + 0xe0707090U, 0x7c3e3e42U, 0x71b5b5c4U, 0xcc6666aaU, + 0x904848d8U, 0x06030305U, 0xf7f6f601U, 0x1c0e0e12U, + 0xc26161a3U, 0x6a35355fU, 0xae5757f9U, 0x69b9b9d0U, + 0x17868691U, 0x99c1c158U, 0x3a1d1d27U, 0x279e9eb9U, + 0xd9e1e138U, 0xebf8f813U, 0x2b9898b3U, 0x22111133U, + 0xd26969bbU, 0xa9d9d970U, 0x078e8e89U, 0x339494a7U, + 0x2d9b9bb6U, 0x3c1e1e22U, 0x15878792U, 0xc9e9e920U, + 0x87cece49U, 0xaa5555ffU, 0x50282878U, 0xa5dfdf7aU, + 0x038c8c8fU, 0x59a1a1f8U, 0x09898980U, 0x1a0d0d17U, + 0x65bfbfdaU, 0xd7e6e631U, 0x844242c6U, 0xd06868b8U, + 0x824141c3U, 0x299999b0U, 0x5a2d2d77U, 0x1e0f0f11U, + 0x7bb0b0cbU, 0xa85454fcU, 0x6dbbbbd6U, 0x2c16163aU, +}, +{ + 0xa5c66363U, 0x84f87c7cU, 0x99ee7777U, 0x8df67b7bU, + 0x0dfff2f2U, 0xbdd66b6bU, 0xb1de6f6fU, 0x5491c5c5U, + 0x50603030U, 0x03020101U, 0xa9ce6767U, 0x7d562b2bU, + 0x19e7fefeU, 0x62b5d7d7U, 0xe64dababU, 0x9aec7676U, + 0x458fcacaU, 0x9d1f8282U, 0x4089c9c9U, 0x87fa7d7dU, + 0x15effafaU, 0xebb25959U, 0xc98e4747U, 0x0bfbf0f0U, + 0xec41adadU, 0x67b3d4d4U, 0xfd5fa2a2U, 0xea45afafU, + 0xbf239c9cU, 0xf753a4a4U, 0x96e47272U, 0x5b9bc0c0U, + 0xc275b7b7U, 0x1ce1fdfdU, 0xae3d9393U, 0x6a4c2626U, + 0x5a6c3636U, 0x417e3f3fU, 0x02f5f7f7U, 0x4f83ccccU, + 0x5c683434U, 0xf451a5a5U, 0x34d1e5e5U, 0x08f9f1f1U, + 0x93e27171U, 0x73abd8d8U, 0x53623131U, 0x3f2a1515U, + 0x0c080404U, 0x5295c7c7U, 0x65462323U, 0x5e9dc3c3U, + 0x28301818U, 0xa1379696U, 0x0f0a0505U, 0xb52f9a9aU, + 0x090e0707U, 0x36241212U, 0x9b1b8080U, 0x3ddfe2e2U, + 0x26cdebebU, 0x694e2727U, 0xcd7fb2b2U, 0x9fea7575U, + 0x1b120909U, 0x9e1d8383U, 0x74582c2cU, 0x2e341a1aU, + 0x2d361b1bU, 0xb2dc6e6eU, 0xeeb45a5aU, 0xfb5ba0a0U, + 0xf6a45252U, 0x4d763b3bU, 0x61b7d6d6U, 0xce7db3b3U, + 0x7b522929U, 0x3edde3e3U, 0x715e2f2fU, 0x97138484U, + 0xf5a65353U, 0x68b9d1d1U, 0x00000000U, 0x2cc1ededU, + 0x60402020U, 0x1fe3fcfcU, 0xc879b1b1U, 0xedb65b5bU, + 0xbed46a6aU, 0x468dcbcbU, 0xd967bebeU, 0x4b723939U, + 0xde944a4aU, 0xd4984c4cU, 0xe8b05858U, 0x4a85cfcfU, + 0x6bbbd0d0U, 0x2ac5efefU, 0xe54faaaaU, 0x16edfbfbU, + 0xc5864343U, 0xd79a4d4dU, 0x55663333U, 0x94118585U, + 0xcf8a4545U, 0x10e9f9f9U, 0x06040202U, 0x81fe7f7fU, + 0xf0a05050U, 0x44783c3cU, 0xba259f9fU, 0xe34ba8a8U, + 0xf3a25151U, 0xfe5da3a3U, 0xc0804040U, 0x8a058f8fU, + 0xad3f9292U, 0xbc219d9dU, 0x48703838U, 0x04f1f5f5U, + 0xdf63bcbcU, 0xc177b6b6U, 0x75afdadaU, 0x63422121U, + 0x30201010U, 0x1ae5ffffU, 0x0efdf3f3U, 0x6dbfd2d2U, + 0x4c81cdcdU, 0x14180c0cU, 0x35261313U, 0x2fc3ececU, + 0xe1be5f5fU, 0xa2359797U, 0xcc884444U, 0x392e1717U, + 0x5793c4c4U, 0xf255a7a7U, 0x82fc7e7eU, 0x477a3d3dU, + 0xacc86464U, 0xe7ba5d5dU, 0x2b321919U, 0x95e67373U, + 0xa0c06060U, 0x98198181U, 0xd19e4f4fU, 0x7fa3dcdcU, + 0x66442222U, 0x7e542a2aU, 0xab3b9090U, 0x830b8888U, + 0xca8c4646U, 0x29c7eeeeU, 0xd36bb8b8U, 0x3c281414U, + 0x79a7dedeU, 0xe2bc5e5eU, 0x1d160b0bU, 0x76addbdbU, + 0x3bdbe0e0U, 0x56643232U, 0x4e743a3aU, 0x1e140a0aU, + 0xdb924949U, 0x0a0c0606U, 0x6c482424U, 0xe4b85c5cU, + 0x5d9fc2c2U, 0x6ebdd3d3U, 0xef43acacU, 0xa6c46262U, + 0xa8399191U, 0xa4319595U, 0x37d3e4e4U, 0x8bf27979U, + 0x32d5e7e7U, 0x438bc8c8U, 0x596e3737U, 0xb7da6d6dU, + 0x8c018d8dU, 0x64b1d5d5U, 0xd29c4e4eU, 0xe049a9a9U, + 0xb4d86c6cU, 0xfaac5656U, 0x07f3f4f4U, 0x25cfeaeaU, + 0xafca6565U, 0x8ef47a7aU, 0xe947aeaeU, 0x18100808U, + 0xd56fbabaU, 0x88f07878U, 0x6f4a2525U, 0x725c2e2eU, + 0x24381c1cU, 0xf157a6a6U, 0xc773b4b4U, 0x5197c6c6U, + 0x23cbe8e8U, 0x7ca1ddddU, 0x9ce87474U, 0x213e1f1fU, + 0xdd964b4bU, 0xdc61bdbdU, 0x860d8b8bU, 0x850f8a8aU, + 0x90e07070U, 0x427c3e3eU, 0xc471b5b5U, 0xaacc6666U, + 0xd8904848U, 0x05060303U, 0x01f7f6f6U, 0x121c0e0eU, + 0xa3c26161U, 0x5f6a3535U, 0xf9ae5757U, 0xd069b9b9U, + 0x91178686U, 0x5899c1c1U, 0x273a1d1dU, 0xb9279e9eU, + 0x38d9e1e1U, 0x13ebf8f8U, 0xb32b9898U, 0x33221111U, + 0xbbd26969U, 0x70a9d9d9U, 0x89078e8eU, 0xa7339494U, + 0xb62d9b9bU, 0x223c1e1eU, 0x92158787U, 0x20c9e9e9U, + 0x4987ceceU, 0xffaa5555U, 0x78502828U, 0x7aa5dfdfU, + 0x8f038c8cU, 0xf859a1a1U, 0x80098989U, 0x171a0d0dU, + 0xda65bfbfU, 0x31d7e6e6U, 0xc6844242U, 0xb8d06868U, + 0xc3824141U, 0xb0299999U, 0x775a2d2dU, 0x111e0f0fU, + 0xcb7bb0b0U, 0xfca85454U, 0xd66dbbbbU, 0x3a2c1616U, +}, +{ + 0x63a5c663U, 0x7c84f87cU, 0x7799ee77U, 0x7b8df67bU, + 0xf20dfff2U, 0x6bbdd66bU, 0x6fb1de6fU, 0xc55491c5U, + 0x30506030U, 0x01030201U, 0x67a9ce67U, 0x2b7d562bU, + 0xfe19e7feU, 0xd762b5d7U, 0xabe64dabU, 0x769aec76U, + 0xca458fcaU, 0x829d1f82U, 0xc94089c9U, 0x7d87fa7dU, + 0xfa15effaU, 0x59ebb259U, 0x47c98e47U, 0xf00bfbf0U, + 0xadec41adU, 0xd467b3d4U, 0xa2fd5fa2U, 0xafea45afU, + 0x9cbf239cU, 0xa4f753a4U, 0x7296e472U, 0xc05b9bc0U, + 0xb7c275b7U, 0xfd1ce1fdU, 0x93ae3d93U, 0x266a4c26U, + 0x365a6c36U, 0x3f417e3fU, 0xf702f5f7U, 0xcc4f83ccU, + 0x345c6834U, 0xa5f451a5U, 0xe534d1e5U, 0xf108f9f1U, + 0x7193e271U, 0xd873abd8U, 0x31536231U, 0x153f2a15U, + 0x040c0804U, 0xc75295c7U, 0x23654623U, 0xc35e9dc3U, + 0x18283018U, 0x96a13796U, 0x050f0a05U, 0x9ab52f9aU, + 0x07090e07U, 0x12362412U, 0x809b1b80U, 0xe23ddfe2U, + 0xeb26cdebU, 0x27694e27U, 0xb2cd7fb2U, 0x759fea75U, + 0x091b1209U, 0x839e1d83U, 0x2c74582cU, 0x1a2e341aU, + 0x1b2d361bU, 0x6eb2dc6eU, 0x5aeeb45aU, 0xa0fb5ba0U, + 0x52f6a452U, 0x3b4d763bU, 0xd661b7d6U, 0xb3ce7db3U, + 0x297b5229U, 0xe33edde3U, 0x2f715e2fU, 0x84971384U, + 0x53f5a653U, 0xd168b9d1U, 0x00000000U, 0xed2cc1edU, + 0x20604020U, 0xfc1fe3fcU, 0xb1c879b1U, 0x5bedb65bU, + 0x6abed46aU, 0xcb468dcbU, 0xbed967beU, 0x394b7239U, + 0x4ade944aU, 0x4cd4984cU, 0x58e8b058U, 0xcf4a85cfU, + 0xd06bbbd0U, 0xef2ac5efU, 0xaae54faaU, 0xfb16edfbU, + 0x43c58643U, 0x4dd79a4dU, 0x33556633U, 0x85941185U, + 0x45cf8a45U, 0xf910e9f9U, 0x02060402U, 0x7f81fe7fU, + 0x50f0a050U, 0x3c44783cU, 0x9fba259fU, 0xa8e34ba8U, + 0x51f3a251U, 0xa3fe5da3U, 0x40c08040U, 0x8f8a058fU, + 0x92ad3f92U, 0x9dbc219dU, 0x38487038U, 0xf504f1f5U, + 0xbcdf63bcU, 0xb6c177b6U, 0xda75afdaU, 0x21634221U, + 0x10302010U, 0xff1ae5ffU, 0xf30efdf3U, 0xd26dbfd2U, + 0xcd4c81cdU, 0x0c14180cU, 0x13352613U, 0xec2fc3ecU, + 0x5fe1be5fU, 0x97a23597U, 0x44cc8844U, 0x17392e17U, + 0xc45793c4U, 0xa7f255a7U, 0x7e82fc7eU, 0x3d477a3dU, + 0x64acc864U, 0x5de7ba5dU, 0x192b3219U, 0x7395e673U, + 0x60a0c060U, 0x81981981U, 0x4fd19e4fU, 0xdc7fa3dcU, + 0x22664422U, 0x2a7e542aU, 0x90ab3b90U, 0x88830b88U, + 0x46ca8c46U, 0xee29c7eeU, 0xb8d36bb8U, 0x143c2814U, + 0xde79a7deU, 0x5ee2bc5eU, 0x0b1d160bU, 0xdb76addbU, + 0xe03bdbe0U, 0x32566432U, 0x3a4e743aU, 0x0a1e140aU, + 0x49db9249U, 0x060a0c06U, 0x246c4824U, 0x5ce4b85cU, + 0xc25d9fc2U, 0xd36ebdd3U, 0xacef43acU, 0x62a6c462U, + 0x91a83991U, 0x95a43195U, 0xe437d3e4U, 0x798bf279U, + 0xe732d5e7U, 0xc8438bc8U, 0x37596e37U, 0x6db7da6dU, + 0x8d8c018dU, 0xd564b1d5U, 0x4ed29c4eU, 0xa9e049a9U, + 0x6cb4d86cU, 0x56faac56U, 0xf407f3f4U, 0xea25cfeaU, + 0x65afca65U, 0x7a8ef47aU, 0xaee947aeU, 0x08181008U, + 0xbad56fbaU, 0x7888f078U, 0x256f4a25U, 0x2e725c2eU, + 0x1c24381cU, 0xa6f157a6U, 0xb4c773b4U, 0xc65197c6U, + 0xe823cbe8U, 0xdd7ca1ddU, 0x749ce874U, 0x1f213e1fU, + 0x4bdd964bU, 0xbddc61bdU, 0x8b860d8bU, 0x8a850f8aU, + 0x7090e070U, 0x3e427c3eU, 0xb5c471b5U, 0x66aacc66U, + 0x48d89048U, 0x03050603U, 0xf601f7f6U, 0x0e121c0eU, + 0x61a3c261U, 0x355f6a35U, 0x57f9ae57U, 0xb9d069b9U, + 0x86911786U, 0xc15899c1U, 0x1d273a1dU, 0x9eb9279eU, + 0xe138d9e1U, 0xf813ebf8U, 0x98b32b98U, 0x11332211U, + 0x69bbd269U, 0xd970a9d9U, 0x8e89078eU, 0x94a73394U, + 0x9bb62d9bU, 0x1e223c1eU, 0x87921587U, 0xe920c9e9U, + 0xce4987ceU, 0x55ffaa55U, 0x28785028U, 0xdf7aa5dfU, + 0x8c8f038cU, 0xa1f859a1U, 0x89800989U, 0x0d171a0dU, + 0xbfda65bfU, 0xe631d7e6U, 0x42c68442U, 0x68b8d068U, + 0x41c38241U, 0x99b02999U, 0x2d775a2dU, 0x0f111e0fU, + 0xb0cb7bb0U, 0x54fca854U, 0xbbd66dbbU, 0x163a2c16U, +}, +{ + 0x6363a5c6U, 0x7c7c84f8U, 0x777799eeU, 0x7b7b8df6U, + 0xf2f20dffU, 0x6b6bbdd6U, 0x6f6fb1deU, 0xc5c55491U, + 0x30305060U, 0x01010302U, 0x6767a9ceU, 0x2b2b7d56U, + 0xfefe19e7U, 0xd7d762b5U, 0xababe64dU, 0x76769aecU, + 0xcaca458fU, 0x82829d1fU, 0xc9c94089U, 0x7d7d87faU, + 0xfafa15efU, 0x5959ebb2U, 0x4747c98eU, 0xf0f00bfbU, + 0xadadec41U, 0xd4d467b3U, 0xa2a2fd5fU, 0xafafea45U, + 0x9c9cbf23U, 0xa4a4f753U, 0x727296e4U, 0xc0c05b9bU, + 0xb7b7c275U, 0xfdfd1ce1U, 0x9393ae3dU, 0x26266a4cU, + 0x36365a6cU, 0x3f3f417eU, 0xf7f702f5U, 0xcccc4f83U, + 0x34345c68U, 0xa5a5f451U, 0xe5e534d1U, 0xf1f108f9U, + 0x717193e2U, 0xd8d873abU, 0x31315362U, 0x15153f2aU, + 0x04040c08U, 0xc7c75295U, 0x23236546U, 0xc3c35e9dU, + 0x18182830U, 0x9696a137U, 0x05050f0aU, 0x9a9ab52fU, + 0x0707090eU, 0x12123624U, 0x80809b1bU, 0xe2e23ddfU, + 0xebeb26cdU, 0x2727694eU, 0xb2b2cd7fU, 0x75759feaU, + 0x09091b12U, 0x83839e1dU, 0x2c2c7458U, 0x1a1a2e34U, + 0x1b1b2d36U, 0x6e6eb2dcU, 0x5a5aeeb4U, 0xa0a0fb5bU, + 0x5252f6a4U, 0x3b3b4d76U, 0xd6d661b7U, 0xb3b3ce7dU, + 0x29297b52U, 0xe3e33eddU, 0x2f2f715eU, 0x84849713U, + 0x5353f5a6U, 0xd1d168b9U, 0x00000000U, 0xeded2cc1U, + 0x20206040U, 0xfcfc1fe3U, 0xb1b1c879U, 0x5b5bedb6U, + 0x6a6abed4U, 0xcbcb468dU, 0xbebed967U, 0x39394b72U, + 0x4a4ade94U, 0x4c4cd498U, 0x5858e8b0U, 0xcfcf4a85U, + 0xd0d06bbbU, 0xefef2ac5U, 0xaaaae54fU, 0xfbfb16edU, + 0x4343c586U, 0x4d4dd79aU, 0x33335566U, 0x85859411U, + 0x4545cf8aU, 0xf9f910e9U, 0x02020604U, 0x7f7f81feU, + 0x5050f0a0U, 0x3c3c4478U, 0x9f9fba25U, 0xa8a8e34bU, + 0x5151f3a2U, 0xa3a3fe5dU, 0x4040c080U, 0x8f8f8a05U, + 0x9292ad3fU, 0x9d9dbc21U, 0x38384870U, 0xf5f504f1U, + 0xbcbcdf63U, 0xb6b6c177U, 0xdada75afU, 0x21216342U, + 0x10103020U, 0xffff1ae5U, 0xf3f30efdU, 0xd2d26dbfU, + 0xcdcd4c81U, 0x0c0c1418U, 0x13133526U, 0xecec2fc3U, + 0x5f5fe1beU, 0x9797a235U, 0x4444cc88U, 0x1717392eU, + 0xc4c45793U, 0xa7a7f255U, 0x7e7e82fcU, 0x3d3d477aU, + 0x6464acc8U, 0x5d5de7baU, 0x19192b32U, 0x737395e6U, + 0x6060a0c0U, 0x81819819U, 0x4f4fd19eU, 0xdcdc7fa3U, + 0x22226644U, 0x2a2a7e54U, 0x9090ab3bU, 0x8888830bU, + 0x4646ca8cU, 0xeeee29c7U, 0xb8b8d36bU, 0x14143c28U, + 0xdede79a7U, 0x5e5ee2bcU, 0x0b0b1d16U, 0xdbdb76adU, + 0xe0e03bdbU, 0x32325664U, 0x3a3a4e74U, 0x0a0a1e14U, + 0x4949db92U, 0x06060a0cU, 0x24246c48U, 0x5c5ce4b8U, + 0xc2c25d9fU, 0xd3d36ebdU, 0xacacef43U, 0x6262a6c4U, + 0x9191a839U, 0x9595a431U, 0xe4e437d3U, 0x79798bf2U, + 0xe7e732d5U, 0xc8c8438bU, 0x3737596eU, 0x6d6db7daU, + 0x8d8d8c01U, 0xd5d564b1U, 0x4e4ed29cU, 0xa9a9e049U, + 0x6c6cb4d8U, 0x5656faacU, 0xf4f407f3U, 0xeaea25cfU, + 0x6565afcaU, 0x7a7a8ef4U, 0xaeaee947U, 0x08081810U, + 0xbabad56fU, 0x787888f0U, 0x25256f4aU, 0x2e2e725cU, + 0x1c1c2438U, 0xa6a6f157U, 0xb4b4c773U, 0xc6c65197U, + 0xe8e823cbU, 0xdddd7ca1U, 0x74749ce8U, 0x1f1f213eU, + 0x4b4bdd96U, 0xbdbddc61U, 0x8b8b860dU, 0x8a8a850fU, + 0x707090e0U, 0x3e3e427cU, 0xb5b5c471U, 0x6666aaccU, + 0x4848d890U, 0x03030506U, 0xf6f601f7U, 0x0e0e121cU, + 0x6161a3c2U, 0x35355f6aU, 0x5757f9aeU, 0xb9b9d069U, + 0x86869117U, 0xc1c15899U, 0x1d1d273aU, 0x9e9eb927U, + 0xe1e138d9U, 0xf8f813ebU, 0x9898b32bU, 0x11113322U, + 0x6969bbd2U, 0xd9d970a9U, 0x8e8e8907U, 0x9494a733U, + 0x9b9bb62dU, 0x1e1e223cU, 0x87879215U, 0xe9e920c9U, + 0xcece4987U, 0x5555ffaaU, 0x28287850U, 0xdfdf7aa5U, + 0x8c8c8f03U, 0xa1a1f859U, 0x89898009U, 0x0d0d171aU, + 0xbfbfda65U, 0xe6e631d7U, 0x4242c684U, 0x6868b8d0U, + 0x4141c382U, 0x9999b029U, 0x2d2d775aU, 0x0f0f111eU, + 0xb0b0cb7bU, 0x5454fca8U, 0xbbbbd66dU, 0x16163a2cU, +}, +{ + 0x63636363U, 0x7c7c7c7cU, 0x77777777U, 0x7b7b7b7bU, + 0xf2f2f2f2U, 0x6b6b6b6bU, 0x6f6f6f6fU, 0xc5c5c5c5U, + 0x30303030U, 0x01010101U, 0x67676767U, 0x2b2b2b2bU, + 0xfefefefeU, 0xd7d7d7d7U, 0xababababU, 0x76767676U, + 0xcacacacaU, 0x82828282U, 0xc9c9c9c9U, 0x7d7d7d7dU, + 0xfafafafaU, 0x59595959U, 0x47474747U, 0xf0f0f0f0U, + 0xadadadadU, 0xd4d4d4d4U, 0xa2a2a2a2U, 0xafafafafU, + 0x9c9c9c9cU, 0xa4a4a4a4U, 0x72727272U, 0xc0c0c0c0U, + 0xb7b7b7b7U, 0xfdfdfdfdU, 0x93939393U, 0x26262626U, + 0x36363636U, 0x3f3f3f3fU, 0xf7f7f7f7U, 0xccccccccU, + 0x34343434U, 0xa5a5a5a5U, 0xe5e5e5e5U, 0xf1f1f1f1U, + 0x71717171U, 0xd8d8d8d8U, 0x31313131U, 0x15151515U, + 0x04040404U, 0xc7c7c7c7U, 0x23232323U, 0xc3c3c3c3U, + 0x18181818U, 0x96969696U, 0x05050505U, 0x9a9a9a9aU, + 0x07070707U, 0x12121212U, 0x80808080U, 0xe2e2e2e2U, + 0xebebebebU, 0x27272727U, 0xb2b2b2b2U, 0x75757575U, + 0x09090909U, 0x83838383U, 0x2c2c2c2cU, 0x1a1a1a1aU, + 0x1b1b1b1bU, 0x6e6e6e6eU, 0x5a5a5a5aU, 0xa0a0a0a0U, + 0x52525252U, 0x3b3b3b3bU, 0xd6d6d6d6U, 0xb3b3b3b3U, + 0x29292929U, 0xe3e3e3e3U, 0x2f2f2f2fU, 0x84848484U, + 0x53535353U, 0xd1d1d1d1U, 0x00000000U, 0xededededU, + 0x20202020U, 0xfcfcfcfcU, 0xb1b1b1b1U, 0x5b5b5b5bU, + 0x6a6a6a6aU, 0xcbcbcbcbU, 0xbebebebeU, 0x39393939U, + 0x4a4a4a4aU, 0x4c4c4c4cU, 0x58585858U, 0xcfcfcfcfU, + 0xd0d0d0d0U, 0xefefefefU, 0xaaaaaaaaU, 0xfbfbfbfbU, + 0x43434343U, 0x4d4d4d4dU, 0x33333333U, 0x85858585U, + 0x45454545U, 0xf9f9f9f9U, 0x02020202U, 0x7f7f7f7fU, + 0x50505050U, 0x3c3c3c3cU, 0x9f9f9f9fU, 0xa8a8a8a8U, + 0x51515151U, 0xa3a3a3a3U, 0x40404040U, 0x8f8f8f8fU, + 0x92929292U, 0x9d9d9d9dU, 0x38383838U, 0xf5f5f5f5U, + 0xbcbcbcbcU, 0xb6b6b6b6U, 0xdadadadaU, 0x21212121U, + 0x10101010U, 0xffffffffU, 0xf3f3f3f3U, 0xd2d2d2d2U, + 0xcdcdcdcdU, 0x0c0c0c0cU, 0x13131313U, 0xececececU, + 0x5f5f5f5fU, 0x97979797U, 0x44444444U, 0x17171717U, + 0xc4c4c4c4U, 0xa7a7a7a7U, 0x7e7e7e7eU, 0x3d3d3d3dU, + 0x64646464U, 0x5d5d5d5dU, 0x19191919U, 0x73737373U, + 0x60606060U, 0x81818181U, 0x4f4f4f4fU, 0xdcdcdcdcU, + 0x22222222U, 0x2a2a2a2aU, 0x90909090U, 0x88888888U, + 0x46464646U, 0xeeeeeeeeU, 0xb8b8b8b8U, 0x14141414U, + 0xdedededeU, 0x5e5e5e5eU, 0x0b0b0b0bU, 0xdbdbdbdbU, + 0xe0e0e0e0U, 0x32323232U, 0x3a3a3a3aU, 0x0a0a0a0aU, + 0x49494949U, 0x06060606U, 0x24242424U, 0x5c5c5c5cU, + 0xc2c2c2c2U, 0xd3d3d3d3U, 0xacacacacU, 0x62626262U, + 0x91919191U, 0x95959595U, 0xe4e4e4e4U, 0x79797979U, + 0xe7e7e7e7U, 0xc8c8c8c8U, 0x37373737U, 0x6d6d6d6dU, + 0x8d8d8d8dU, 0xd5d5d5d5U, 0x4e4e4e4eU, 0xa9a9a9a9U, + 0x6c6c6c6cU, 0x56565656U, 0xf4f4f4f4U, 0xeaeaeaeaU, + 0x65656565U, 0x7a7a7a7aU, 0xaeaeaeaeU, 0x08080808U, + 0xbabababaU, 0x78787878U, 0x25252525U, 0x2e2e2e2eU, + 0x1c1c1c1cU, 0xa6a6a6a6U, 0xb4b4b4b4U, 0xc6c6c6c6U, + 0xe8e8e8e8U, 0xddddddddU, 0x74747474U, 0x1f1f1f1fU, + 0x4b4b4b4bU, 0xbdbdbdbdU, 0x8b8b8b8bU, 0x8a8a8a8aU, + 0x70707070U, 0x3e3e3e3eU, 0xb5b5b5b5U, 0x66666666U, + 0x48484848U, 0x03030303U, 0xf6f6f6f6U, 0x0e0e0e0eU, + 0x61616161U, 0x35353535U, 0x57575757U, 0xb9b9b9b9U, + 0x86868686U, 0xc1c1c1c1U, 0x1d1d1d1dU, 0x9e9e9e9eU, + 0xe1e1e1e1U, 0xf8f8f8f8U, 0x98989898U, 0x11111111U, + 0x69696969U, 0xd9d9d9d9U, 0x8e8e8e8eU, 0x94949494U, + 0x9b9b9b9bU, 0x1e1e1e1eU, 0x87878787U, 0xe9e9e9e9U, + 0xcecececeU, 0x55555555U, 0x28282828U, 0xdfdfdfdfU, + 0x8c8c8c8cU, 0xa1a1a1a1U, 0x89898989U, 0x0d0d0d0dU, + 0xbfbfbfbfU, 0xe6e6e6e6U, 0x42424242U, 0x68686868U, + 0x41414141U, 0x99999999U, 0x2d2d2d2dU, 0x0f0f0f0fU, + 0xb0b0b0b0U, 0x54545454U, 0xbbbbbbbbU, 0x16161616U, +} +}; + + +const word32 AES::Td[5][256] = { +{ + 0x51f4a750U, 0x7e416553U, 0x1a17a4c3U, 0x3a275e96U, + 0x3bab6bcbU, 0x1f9d45f1U, 0xacfa58abU, 0x4be30393U, + 0x2030fa55U, 0xad766df6U, 0x88cc7691U, 0xf5024c25U, + 0x4fe5d7fcU, 0xc52acbd7U, 0x26354480U, 0xb562a38fU, + 0xdeb15a49U, 0x25ba1b67U, 0x45ea0e98U, 0x5dfec0e1U, + 0xc32f7502U, 0x814cf012U, 0x8d4697a3U, 0x6bd3f9c6U, + 0x038f5fe7U, 0x15929c95U, 0xbf6d7aebU, 0x955259daU, + 0xd4be832dU, 0x587421d3U, 0x49e06929U, 0x8ec9c844U, + 0x75c2896aU, 0xf48e7978U, 0x99583e6bU, 0x27b971ddU, + 0xbee14fb6U, 0xf088ad17U, 0xc920ac66U, 0x7dce3ab4U, + 0x63df4a18U, 0xe51a3182U, 0x97513360U, 0x62537f45U, + 0xb16477e0U, 0xbb6bae84U, 0xfe81a01cU, 0xf9082b94U, + 0x70486858U, 0x8f45fd19U, 0x94de6c87U, 0x527bf8b7U, + 0xab73d323U, 0x724b02e2U, 0xe31f8f57U, 0x6655ab2aU, + 0xb2eb2807U, 0x2fb5c203U, 0x86c57b9aU, 0xd33708a5U, + 0x302887f2U, 0x23bfa5b2U, 0x02036abaU, 0xed16825cU, + 0x8acf1c2bU, 0xa779b492U, 0xf307f2f0U, 0x4e69e2a1U, + 0x65daf4cdU, 0x0605bed5U, 0xd134621fU, 0xc4a6fe8aU, + 0x342e539dU, 0xa2f355a0U, 0x058ae132U, 0xa4f6eb75U, + 0x0b83ec39U, 0x4060efaaU, 0x5e719f06U, 0xbd6e1051U, + 0x3e218af9U, 0x96dd063dU, 0xdd3e05aeU, 0x4de6bd46U, + 0x91548db5U, 0x71c45d05U, 0x0406d46fU, 0x605015ffU, + 0x1998fb24U, 0xd6bde997U, 0x894043ccU, 0x67d99e77U, + 0xb0e842bdU, 0x07898b88U, 0xe7195b38U, 0x79c8eedbU, + 0xa17c0a47U, 0x7c420fe9U, 0xf8841ec9U, 0x00000000U, + 0x09808683U, 0x322bed48U, 0x1e1170acU, 0x6c5a724eU, + 0xfd0efffbU, 0x0f853856U, 0x3daed51eU, 0x362d3927U, + 0x0a0fd964U, 0x685ca621U, 0x9b5b54d1U, 0x24362e3aU, + 0x0c0a67b1U, 0x9357e70fU, 0xb4ee96d2U, 0x1b9b919eU, + 0x80c0c54fU, 0x61dc20a2U, 0x5a774b69U, 0x1c121a16U, + 0xe293ba0aU, 0xc0a02ae5U, 0x3c22e043U, 0x121b171dU, + 0x0e090d0bU, 0xf28bc7adU, 0x2db6a8b9U, 0x141ea9c8U, + 0x57f11985U, 0xaf75074cU, 0xee99ddbbU, 0xa37f60fdU, + 0xf701269fU, 0x5c72f5bcU, 0x44663bc5U, 0x5bfb7e34U, + 0x8b432976U, 0xcb23c6dcU, 0xb6edfc68U, 0xb8e4f163U, + 0xd731dccaU, 0x42638510U, 0x13972240U, 0x84c61120U, + 0x854a247dU, 0xd2bb3df8U, 0xaef93211U, 0xc729a16dU, + 0x1d9e2f4bU, 0xdcb230f3U, 0x0d8652ecU, 0x77c1e3d0U, + 0x2bb3166cU, 0xa970b999U, 0x119448faU, 0x47e96422U, + 0xa8fc8cc4U, 0xa0f03f1aU, 0x567d2cd8U, 0x223390efU, + 0x87494ec7U, 0xd938d1c1U, 0x8ccaa2feU, 0x98d40b36U, + 0xa6f581cfU, 0xa57ade28U, 0xdab78e26U, 0x3fadbfa4U, + 0x2c3a9de4U, 0x5078920dU, 0x6a5fcc9bU, 0x547e4662U, + 0xf68d13c2U, 0x90d8b8e8U, 0x2e39f75eU, 0x82c3aff5U, + 0x9f5d80beU, 0x69d0937cU, 0x6fd52da9U, 0xcf2512b3U, + 0xc8ac993bU, 0x10187da7U, 0xe89c636eU, 0xdb3bbb7bU, + 0xcd267809U, 0x6e5918f4U, 0xec9ab701U, 0x834f9aa8U, + 0xe6956e65U, 0xaaffe67eU, 0x21bccf08U, 0xef15e8e6U, + 0xbae79bd9U, 0x4a6f36ceU, 0xea9f09d4U, 0x29b07cd6U, + 0x31a4b2afU, 0x2a3f2331U, 0xc6a59430U, 0x35a266c0U, + 0x744ebc37U, 0xfc82caa6U, 0xe090d0b0U, 0x33a7d815U, + 0xf104984aU, 0x41ecdaf7U, 0x7fcd500eU, 0x1791f62fU, + 0x764dd68dU, 0x43efb04dU, 0xccaa4d54U, 0xe49604dfU, + 0x9ed1b5e3U, 0x4c6a881bU, 0xc12c1fb8U, 0x4665517fU, + 0x9d5eea04U, 0x018c355dU, 0xfa877473U, 0xfb0b412eU, + 0xb3671d5aU, 0x92dbd252U, 0xe9105633U, 0x6dd64713U, + 0x9ad7618cU, 0x37a10c7aU, 0x59f8148eU, 0xeb133c89U, + 0xcea927eeU, 0xb761c935U, 0xe11ce5edU, 0x7a47b13cU, + 0x9cd2df59U, 0x55f2733fU, 0x1814ce79U, 0x73c737bfU, + 0x53f7cdeaU, 0x5ffdaa5bU, 0xdf3d6f14U, 0x7844db86U, + 0xcaaff381U, 0xb968c43eU, 0x3824342cU, 0xc2a3405fU, + 0x161dc372U, 0xbce2250cU, 0x283c498bU, 0xff0d9541U, + 0x39a80171U, 0x080cb3deU, 0xd8b4e49cU, 0x6456c190U, + 0x7bcb8461U, 0xd532b670U, 0x486c5c74U, 0xd0b85742U, +}, +{ + 0x5051f4a7U, 0x537e4165U, 0xc31a17a4U, 0x963a275eU, + 0xcb3bab6bU, 0xf11f9d45U, 0xabacfa58U, 0x934be303U, + 0x552030faU, 0xf6ad766dU, 0x9188cc76U, 0x25f5024cU, + 0xfc4fe5d7U, 0xd7c52acbU, 0x80263544U, 0x8fb562a3U, + 0x49deb15aU, 0x6725ba1bU, 0x9845ea0eU, 0xe15dfec0U, + 0x02c32f75U, 0x12814cf0U, 0xa38d4697U, 0xc66bd3f9U, + 0xe7038f5fU, 0x9515929cU, 0xebbf6d7aU, 0xda955259U, + 0x2dd4be83U, 0xd3587421U, 0x2949e069U, 0x448ec9c8U, + 0x6a75c289U, 0x78f48e79U, 0x6b99583eU, 0xdd27b971U, + 0xb6bee14fU, 0x17f088adU, 0x66c920acU, 0xb47dce3aU, + 0x1863df4aU, 0x82e51a31U, 0x60975133U, 0x4562537fU, + 0xe0b16477U, 0x84bb6baeU, 0x1cfe81a0U, 0x94f9082bU, + 0x58704868U, 0x198f45fdU, 0x8794de6cU, 0xb7527bf8U, + 0x23ab73d3U, 0xe2724b02U, 0x57e31f8fU, 0x2a6655abU, + 0x07b2eb28U, 0x032fb5c2U, 0x9a86c57bU, 0xa5d33708U, + 0xf2302887U, 0xb223bfa5U, 0xba02036aU, 0x5ced1682U, + 0x2b8acf1cU, 0x92a779b4U, 0xf0f307f2U, 0xa14e69e2U, + 0xcd65daf4U, 0xd50605beU, 0x1fd13462U, 0x8ac4a6feU, + 0x9d342e53U, 0xa0a2f355U, 0x32058ae1U, 0x75a4f6ebU, + 0x390b83ecU, 0xaa4060efU, 0x065e719fU, 0x51bd6e10U, + 0xf93e218aU, 0x3d96dd06U, 0xaedd3e05U, 0x464de6bdU, + 0xb591548dU, 0x0571c45dU, 0x6f0406d4U, 0xff605015U, + 0x241998fbU, 0x97d6bde9U, 0xcc894043U, 0x7767d99eU, + 0xbdb0e842U, 0x8807898bU, 0x38e7195bU, 0xdb79c8eeU, + 0x47a17c0aU, 0xe97c420fU, 0xc9f8841eU, 0x00000000U, + 0x83098086U, 0x48322bedU, 0xac1e1170U, 0x4e6c5a72U, + 0xfbfd0effU, 0x560f8538U, 0x1e3daed5U, 0x27362d39U, + 0x640a0fd9U, 0x21685ca6U, 0xd19b5b54U, 0x3a24362eU, + 0xb10c0a67U, 0x0f9357e7U, 0xd2b4ee96U, 0x9e1b9b91U, + 0x4f80c0c5U, 0xa261dc20U, 0x695a774bU, 0x161c121aU, + 0x0ae293baU, 0xe5c0a02aU, 0x433c22e0U, 0x1d121b17U, + 0x0b0e090dU, 0xadf28bc7U, 0xb92db6a8U, 0xc8141ea9U, + 0x8557f119U, 0x4caf7507U, 0xbbee99ddU, 0xfda37f60U, + 0x9ff70126U, 0xbc5c72f5U, 0xc544663bU, 0x345bfb7eU, + 0x768b4329U, 0xdccb23c6U, 0x68b6edfcU, 0x63b8e4f1U, + 0xcad731dcU, 0x10426385U, 0x40139722U, 0x2084c611U, + 0x7d854a24U, 0xf8d2bb3dU, 0x11aef932U, 0x6dc729a1U, + 0x4b1d9e2fU, 0xf3dcb230U, 0xec0d8652U, 0xd077c1e3U, + 0x6c2bb316U, 0x99a970b9U, 0xfa119448U, 0x2247e964U, + 0xc4a8fc8cU, 0x1aa0f03fU, 0xd8567d2cU, 0xef223390U, + 0xc787494eU, 0xc1d938d1U, 0xfe8ccaa2U, 0x3698d40bU, + 0xcfa6f581U, 0x28a57adeU, 0x26dab78eU, 0xa43fadbfU, + 0xe42c3a9dU, 0x0d507892U, 0x9b6a5fccU, 0x62547e46U, + 0xc2f68d13U, 0xe890d8b8U, 0x5e2e39f7U, 0xf582c3afU, + 0xbe9f5d80U, 0x7c69d093U, 0xa96fd52dU, 0xb3cf2512U, + 0x3bc8ac99U, 0xa710187dU, 0x6ee89c63U, 0x7bdb3bbbU, + 0x09cd2678U, 0xf46e5918U, 0x01ec9ab7U, 0xa8834f9aU, + 0x65e6956eU, 0x7eaaffe6U, 0x0821bccfU, 0xe6ef15e8U, + 0xd9bae79bU, 0xce4a6f36U, 0xd4ea9f09U, 0xd629b07cU, + 0xaf31a4b2U, 0x312a3f23U, 0x30c6a594U, 0xc035a266U, + 0x37744ebcU, 0xa6fc82caU, 0xb0e090d0U, 0x1533a7d8U, + 0x4af10498U, 0xf741ecdaU, 0x0e7fcd50U, 0x2f1791f6U, + 0x8d764dd6U, 0x4d43efb0U, 0x54ccaa4dU, 0xdfe49604U, + 0xe39ed1b5U, 0x1b4c6a88U, 0xb8c12c1fU, 0x7f466551U, + 0x049d5eeaU, 0x5d018c35U, 0x73fa8774U, 0x2efb0b41U, + 0x5ab3671dU, 0x5292dbd2U, 0x33e91056U, 0x136dd647U, + 0x8c9ad761U, 0x7a37a10cU, 0x8e59f814U, 0x89eb133cU, + 0xeecea927U, 0x35b761c9U, 0xede11ce5U, 0x3c7a47b1U, + 0x599cd2dfU, 0x3f55f273U, 0x791814ceU, 0xbf73c737U, + 0xea53f7cdU, 0x5b5ffdaaU, 0x14df3d6fU, 0x867844dbU, + 0x81caaff3U, 0x3eb968c4U, 0x2c382434U, 0x5fc2a340U, + 0x72161dc3U, 0x0cbce225U, 0x8b283c49U, 0x41ff0d95U, + 0x7139a801U, 0xde080cb3U, 0x9cd8b4e4U, 0x906456c1U, + 0x617bcb84U, 0x70d532b6U, 0x74486c5cU, 0x42d0b857U, +}, +{ + 0xa75051f4U, 0x65537e41U, 0xa4c31a17U, 0x5e963a27U, + 0x6bcb3babU, 0x45f11f9dU, 0x58abacfaU, 0x03934be3U, + 0xfa552030U, 0x6df6ad76U, 0x769188ccU, 0x4c25f502U, + 0xd7fc4fe5U, 0xcbd7c52aU, 0x44802635U, 0xa38fb562U, + 0x5a49deb1U, 0x1b6725baU, 0x0e9845eaU, 0xc0e15dfeU, + 0x7502c32fU, 0xf012814cU, 0x97a38d46U, 0xf9c66bd3U, + 0x5fe7038fU, 0x9c951592U, 0x7aebbf6dU, 0x59da9552U, + 0x832dd4beU, 0x21d35874U, 0x692949e0U, 0xc8448ec9U, + 0x896a75c2U, 0x7978f48eU, 0x3e6b9958U, 0x71dd27b9U, + 0x4fb6bee1U, 0xad17f088U, 0xac66c920U, 0x3ab47dceU, + 0x4a1863dfU, 0x3182e51aU, 0x33609751U, 0x7f456253U, + 0x77e0b164U, 0xae84bb6bU, 0xa01cfe81U, 0x2b94f908U, + 0x68587048U, 0xfd198f45U, 0x6c8794deU, 0xf8b7527bU, + 0xd323ab73U, 0x02e2724bU, 0x8f57e31fU, 0xab2a6655U, + 0x2807b2ebU, 0xc2032fb5U, 0x7b9a86c5U, 0x08a5d337U, + 0x87f23028U, 0xa5b223bfU, 0x6aba0203U, 0x825ced16U, + 0x1c2b8acfU, 0xb492a779U, 0xf2f0f307U, 0xe2a14e69U, + 0xf4cd65daU, 0xbed50605U, 0x621fd134U, 0xfe8ac4a6U, + 0x539d342eU, 0x55a0a2f3U, 0xe132058aU, 0xeb75a4f6U, + 0xec390b83U, 0xefaa4060U, 0x9f065e71U, 0x1051bd6eU, + + 0x8af93e21U, 0x063d96ddU, 0x05aedd3eU, 0xbd464de6U, + 0x8db59154U, 0x5d0571c4U, 0xd46f0406U, 0x15ff6050U, + 0xfb241998U, 0xe997d6bdU, 0x43cc8940U, 0x9e7767d9U, + 0x42bdb0e8U, 0x8b880789U, 0x5b38e719U, 0xeedb79c8U, + 0x0a47a17cU, 0x0fe97c42U, 0x1ec9f884U, 0x00000000U, + 0x86830980U, 0xed48322bU, 0x70ac1e11U, 0x724e6c5aU, + 0xfffbfd0eU, 0x38560f85U, 0xd51e3daeU, 0x3927362dU, + 0xd9640a0fU, 0xa621685cU, 0x54d19b5bU, 0x2e3a2436U, + 0x67b10c0aU, 0xe70f9357U, 0x96d2b4eeU, 0x919e1b9bU, + 0xc54f80c0U, 0x20a261dcU, 0x4b695a77U, 0x1a161c12U, + 0xba0ae293U, 0x2ae5c0a0U, 0xe0433c22U, 0x171d121bU, + 0x0d0b0e09U, 0xc7adf28bU, 0xa8b92db6U, 0xa9c8141eU, + 0x198557f1U, 0x074caf75U, 0xddbbee99U, 0x60fda37fU, + 0x269ff701U, 0xf5bc5c72U, 0x3bc54466U, 0x7e345bfbU, + 0x29768b43U, 0xc6dccb23U, 0xfc68b6edU, 0xf163b8e4U, + 0xdccad731U, 0x85104263U, 0x22401397U, 0x112084c6U, + 0x247d854aU, 0x3df8d2bbU, 0x3211aef9U, 0xa16dc729U, + 0x2f4b1d9eU, 0x30f3dcb2U, 0x52ec0d86U, 0xe3d077c1U, + 0x166c2bb3U, 0xb999a970U, 0x48fa1194U, 0x642247e9U, + 0x8cc4a8fcU, 0x3f1aa0f0U, 0x2cd8567dU, 0x90ef2233U, + 0x4ec78749U, 0xd1c1d938U, 0xa2fe8ccaU, 0x0b3698d4U, + 0x81cfa6f5U, 0xde28a57aU, 0x8e26dab7U, 0xbfa43fadU, + 0x9de42c3aU, 0x920d5078U, 0xcc9b6a5fU, 0x4662547eU, + 0x13c2f68dU, 0xb8e890d8U, 0xf75e2e39U, 0xaff582c3U, + 0x80be9f5dU, 0x937c69d0U, 0x2da96fd5U, 0x12b3cf25U, + 0x993bc8acU, 0x7da71018U, 0x636ee89cU, 0xbb7bdb3bU, + 0x7809cd26U, 0x18f46e59U, 0xb701ec9aU, 0x9aa8834fU, + 0x6e65e695U, 0xe67eaaffU, 0xcf0821bcU, 0xe8e6ef15U, + 0x9bd9bae7U, 0x36ce4a6fU, 0x09d4ea9fU, 0x7cd629b0U, + 0xb2af31a4U, 0x23312a3fU, 0x9430c6a5U, 0x66c035a2U, + 0xbc37744eU, 0xcaa6fc82U, 0xd0b0e090U, 0xd81533a7U, + 0x984af104U, 0xdaf741ecU, 0x500e7fcdU, 0xf62f1791U, + 0xd68d764dU, 0xb04d43efU, 0x4d54ccaaU, 0x04dfe496U, + 0xb5e39ed1U, 0x881b4c6aU, 0x1fb8c12cU, 0x517f4665U, + 0xea049d5eU, 0x355d018cU, 0x7473fa87U, 0x412efb0bU, + 0x1d5ab367U, 0xd25292dbU, 0x5633e910U, 0x47136dd6U, + 0x618c9ad7U, 0x0c7a37a1U, 0x148e59f8U, 0x3c89eb13U, + 0x27eecea9U, 0xc935b761U, 0xe5ede11cU, 0xb13c7a47U, + 0xdf599cd2U, 0x733f55f2U, 0xce791814U, 0x37bf73c7U, + 0xcdea53f7U, 0xaa5b5ffdU, 0x6f14df3dU, 0xdb867844U, + 0xf381caafU, 0xc43eb968U, 0x342c3824U, 0x405fc2a3U, + 0xc372161dU, 0x250cbce2U, 0x498b283cU, 0x9541ff0dU, + 0x017139a8U, 0xb3de080cU, 0xe49cd8b4U, 0xc1906456U, + 0x84617bcbU, 0xb670d532U, 0x5c74486cU, 0x5742d0b8U, +}, +{ + 0xf4a75051U, 0x4165537eU, 0x17a4c31aU, 0x275e963aU, + 0xab6bcb3bU, 0x9d45f11fU, 0xfa58abacU, 0xe303934bU, + 0x30fa5520U, 0x766df6adU, 0xcc769188U, 0x024c25f5U, + 0xe5d7fc4fU, 0x2acbd7c5U, 0x35448026U, 0x62a38fb5U, + 0xb15a49deU, 0xba1b6725U, 0xea0e9845U, 0xfec0e15dU, + 0x2f7502c3U, 0x4cf01281U, 0x4697a38dU, 0xd3f9c66bU, + 0x8f5fe703U, 0x929c9515U, 0x6d7aebbfU, 0x5259da95U, + 0xbe832dd4U, 0x7421d358U, 0xe0692949U, 0xc9c8448eU, + 0xc2896a75U, 0x8e7978f4U, 0x583e6b99U, 0xb971dd27U, + 0xe14fb6beU, 0x88ad17f0U, 0x20ac66c9U, 0xce3ab47dU, + 0xdf4a1863U, 0x1a3182e5U, 0x51336097U, 0x537f4562U, + 0x6477e0b1U, 0x6bae84bbU, 0x81a01cfeU, 0x082b94f9U, + 0x48685870U, 0x45fd198fU, 0xde6c8794U, 0x7bf8b752U, + 0x73d323abU, 0x4b02e272U, 0x1f8f57e3U, 0x55ab2a66U, + 0xeb2807b2U, 0xb5c2032fU, 0xc57b9a86U, 0x3708a5d3U, + 0x2887f230U, 0xbfa5b223U, 0x036aba02U, 0x16825cedU, + 0xcf1c2b8aU, 0x79b492a7U, 0x07f2f0f3U, 0x69e2a14eU, + 0xdaf4cd65U, 0x05bed506U, 0x34621fd1U, 0xa6fe8ac4U, + 0x2e539d34U, 0xf355a0a2U, 0x8ae13205U, 0xf6eb75a4U, + 0x83ec390bU, 0x60efaa40U, 0x719f065eU, 0x6e1051bdU, + 0x218af93eU, 0xdd063d96U, 0x3e05aeddU, 0xe6bd464dU, + 0x548db591U, 0xc45d0571U, 0x06d46f04U, 0x5015ff60U, + 0x98fb2419U, 0xbde997d6U, 0x4043cc89U, 0xd99e7767U, + 0xe842bdb0U, 0x898b8807U, 0x195b38e7U, 0xc8eedb79U, + 0x7c0a47a1U, 0x420fe97cU, 0x841ec9f8U, 0x00000000U, + 0x80868309U, 0x2bed4832U, 0x1170ac1eU, 0x5a724e6cU, + 0x0efffbfdU, 0x8538560fU, 0xaed51e3dU, 0x2d392736U, + 0x0fd9640aU, 0x5ca62168U, 0x5b54d19bU, 0x362e3a24U, + 0x0a67b10cU, 0x57e70f93U, 0xee96d2b4U, 0x9b919e1bU, + 0xc0c54f80U, 0xdc20a261U, 0x774b695aU, 0x121a161cU, + 0x93ba0ae2U, 0xa02ae5c0U, 0x22e0433cU, 0x1b171d12U, + 0x090d0b0eU, 0x8bc7adf2U, 0xb6a8b92dU, 0x1ea9c814U, + 0xf1198557U, 0x75074cafU, 0x99ddbbeeU, 0x7f60fda3U, + 0x01269ff7U, 0x72f5bc5cU, 0x663bc544U, 0xfb7e345bU, + 0x4329768bU, 0x23c6dccbU, 0xedfc68b6U, 0xe4f163b8U, + 0x31dccad7U, 0x63851042U, 0x97224013U, 0xc6112084U, + 0x4a247d85U, 0xbb3df8d2U, 0xf93211aeU, 0x29a16dc7U, + 0x9e2f4b1dU, 0xb230f3dcU, 0x8652ec0dU, 0xc1e3d077U, + 0xb3166c2bU, 0x70b999a9U, 0x9448fa11U, 0xe9642247U, + 0xfc8cc4a8U, 0xf03f1aa0U, 0x7d2cd856U, 0x3390ef22U, + 0x494ec787U, 0x38d1c1d9U, 0xcaa2fe8cU, 0xd40b3698U, + 0xf581cfa6U, 0x7ade28a5U, 0xb78e26daU, 0xadbfa43fU, + 0x3a9de42cU, 0x78920d50U, 0x5fcc9b6aU, 0x7e466254U, + 0x8d13c2f6U, 0xd8b8e890U, 0x39f75e2eU, 0xc3aff582U, + 0x5d80be9fU, 0xd0937c69U, 0xd52da96fU, 0x2512b3cfU, + 0xac993bc8U, 0x187da710U, 0x9c636ee8U, 0x3bbb7bdbU, + 0x267809cdU, 0x5918f46eU, 0x9ab701ecU, 0x4f9aa883U, + 0x956e65e6U, 0xffe67eaaU, 0xbccf0821U, 0x15e8e6efU, + 0xe79bd9baU, 0x6f36ce4aU, 0x9f09d4eaU, 0xb07cd629U, + 0xa4b2af31U, 0x3f23312aU, 0xa59430c6U, 0xa266c035U, + 0x4ebc3774U, 0x82caa6fcU, 0x90d0b0e0U, 0xa7d81533U, + 0x04984af1U, 0xecdaf741U, 0xcd500e7fU, 0x91f62f17U, + 0x4dd68d76U, 0xefb04d43U, 0xaa4d54ccU, 0x9604dfe4U, + 0xd1b5e39eU, 0x6a881b4cU, 0x2c1fb8c1U, 0x65517f46U, + 0x5eea049dU, 0x8c355d01U, 0x877473faU, 0x0b412efbU, + 0x671d5ab3U, 0xdbd25292U, 0x105633e9U, 0xd647136dU, + 0xd7618c9aU, 0xa10c7a37U, 0xf8148e59U, 0x133c89ebU, + 0xa927eeceU, 0x61c935b7U, 0x1ce5ede1U, 0x47b13c7aU, + 0xd2df599cU, 0xf2733f55U, 0x14ce7918U, 0xc737bf73U, + 0xf7cdea53U, 0xfdaa5b5fU, 0x3d6f14dfU, 0x44db8678U, + 0xaff381caU, 0x68c43eb9U, 0x24342c38U, 0xa3405fc2U, + 0x1dc37216U, 0xe2250cbcU, 0x3c498b28U, 0x0d9541ffU, + 0xa8017139U, 0x0cb3de08U, 0xb4e49cd8U, 0x56c19064U, + 0xcb84617bU, 0x32b670d5U, 0x6c5c7448U, 0xb85742d0U, +}, +{ + 0x52525252U, 0x09090909U, 0x6a6a6a6aU, 0xd5d5d5d5U, + 0x30303030U, 0x36363636U, 0xa5a5a5a5U, 0x38383838U, + 0xbfbfbfbfU, 0x40404040U, 0xa3a3a3a3U, 0x9e9e9e9eU, + 0x81818181U, 0xf3f3f3f3U, 0xd7d7d7d7U, 0xfbfbfbfbU, + 0x7c7c7c7cU, 0xe3e3e3e3U, 0x39393939U, 0x82828282U, + 0x9b9b9b9bU, 0x2f2f2f2fU, 0xffffffffU, 0x87878787U, + 0x34343434U, 0x8e8e8e8eU, 0x43434343U, 0x44444444U, + 0xc4c4c4c4U, 0xdedededeU, 0xe9e9e9e9U, 0xcbcbcbcbU, + 0x54545454U, 0x7b7b7b7bU, 0x94949494U, 0x32323232U, + 0xa6a6a6a6U, 0xc2c2c2c2U, 0x23232323U, 0x3d3d3d3dU, + 0xeeeeeeeeU, 0x4c4c4c4cU, 0x95959595U, 0x0b0b0b0bU, + 0x42424242U, 0xfafafafaU, 0xc3c3c3c3U, 0x4e4e4e4eU, + 0x08080808U, 0x2e2e2e2eU, 0xa1a1a1a1U, 0x66666666U, + 0x28282828U, 0xd9d9d9d9U, 0x24242424U, 0xb2b2b2b2U, + 0x76767676U, 0x5b5b5b5bU, 0xa2a2a2a2U, 0x49494949U, + 0x6d6d6d6dU, 0x8b8b8b8bU, 0xd1d1d1d1U, 0x25252525U, + 0x72727272U, 0xf8f8f8f8U, 0xf6f6f6f6U, 0x64646464U, + 0x86868686U, 0x68686868U, 0x98989898U, 0x16161616U, + 0xd4d4d4d4U, 0xa4a4a4a4U, 0x5c5c5c5cU, 0xccccccccU, + 0x5d5d5d5dU, 0x65656565U, 0xb6b6b6b6U, 0x92929292U, + 0x6c6c6c6cU, 0x70707070U, 0x48484848U, 0x50505050U, + 0xfdfdfdfdU, 0xededededU, 0xb9b9b9b9U, 0xdadadadaU, + 0x5e5e5e5eU, 0x15151515U, 0x46464646U, 0x57575757U, + 0xa7a7a7a7U, 0x8d8d8d8dU, 0x9d9d9d9dU, 0x84848484U, + 0x90909090U, 0xd8d8d8d8U, 0xababababU, 0x00000000U, + 0x8c8c8c8cU, 0xbcbcbcbcU, 0xd3d3d3d3U, 0x0a0a0a0aU, + 0xf7f7f7f7U, 0xe4e4e4e4U, 0x58585858U, 0x05050505U, + 0xb8b8b8b8U, 0xb3b3b3b3U, 0x45454545U, 0x06060606U, + 0xd0d0d0d0U, 0x2c2c2c2cU, 0x1e1e1e1eU, 0x8f8f8f8fU, + 0xcacacacaU, 0x3f3f3f3fU, 0x0f0f0f0fU, 0x02020202U, + 0xc1c1c1c1U, 0xafafafafU, 0xbdbdbdbdU, 0x03030303U, + 0x01010101U, 0x13131313U, 0x8a8a8a8aU, 0x6b6b6b6bU, + 0x3a3a3a3aU, 0x91919191U, 0x11111111U, 0x41414141U, + 0x4f4f4f4fU, 0x67676767U, 0xdcdcdcdcU, 0xeaeaeaeaU, + 0x97979797U, 0xf2f2f2f2U, 0xcfcfcfcfU, 0xcecececeU, + 0xf0f0f0f0U, 0xb4b4b4b4U, 0xe6e6e6e6U, 0x73737373U, + 0x96969696U, 0xacacacacU, 0x74747474U, 0x22222222U, + 0xe7e7e7e7U, 0xadadadadU, 0x35353535U, 0x85858585U, + 0xe2e2e2e2U, 0xf9f9f9f9U, 0x37373737U, 0xe8e8e8e8U, + 0x1c1c1c1cU, 0x75757575U, 0xdfdfdfdfU, 0x6e6e6e6eU, + 0x47474747U, 0xf1f1f1f1U, 0x1a1a1a1aU, 0x71717171U, + 0x1d1d1d1dU, 0x29292929U, 0xc5c5c5c5U, 0x89898989U, + 0x6f6f6f6fU, 0xb7b7b7b7U, 0x62626262U, 0x0e0e0e0eU, + 0xaaaaaaaaU, 0x18181818U, 0xbebebebeU, 0x1b1b1b1bU, + 0xfcfcfcfcU, 0x56565656U, 0x3e3e3e3eU, 0x4b4b4b4bU, + 0xc6c6c6c6U, 0xd2d2d2d2U, 0x79797979U, 0x20202020U, + 0x9a9a9a9aU, 0xdbdbdbdbU, 0xc0c0c0c0U, 0xfefefefeU, + 0x78787878U, 0xcdcdcdcdU, 0x5a5a5a5aU, 0xf4f4f4f4U, + 0x1f1f1f1fU, 0xddddddddU, 0xa8a8a8a8U, 0x33333333U, + 0x88888888U, 0x07070707U, 0xc7c7c7c7U, 0x31313131U, + 0xb1b1b1b1U, 0x12121212U, 0x10101010U, 0x59595959U, + 0x27272727U, 0x80808080U, 0xececececU, 0x5f5f5f5fU, + 0x60606060U, 0x51515151U, 0x7f7f7f7fU, 0xa9a9a9a9U, + 0x19191919U, 0xb5b5b5b5U, 0x4a4a4a4aU, 0x0d0d0d0dU, + 0x2d2d2d2dU, 0xe5e5e5e5U, 0x7a7a7a7aU, 0x9f9f9f9fU, + 0x93939393U, 0xc9c9c9c9U, 0x9c9c9c9cU, 0xefefefefU, + 0xa0a0a0a0U, 0xe0e0e0e0U, 0x3b3b3b3bU, 0x4d4d4d4dU, + 0xaeaeaeaeU, 0x2a2a2a2aU, 0xf5f5f5f5U, 0xb0b0b0b0U, + 0xc8c8c8c8U, 0xebebebebU, 0xbbbbbbbbU, 0x3c3c3c3cU, + 0x83838383U, 0x53535353U, 0x99999999U, 0x61616161U, + 0x17171717U, 0x2b2b2b2bU, 0x04040404U, 0x7e7e7e7eU, + 0xbabababaU, 0x77777777U, 0xd6d6d6d6U, 0x26262626U, + 0xe1e1e1e1U, 0x69696969U, 0x14141414U, 0x63636363U, + 0x55555555U, 0x21212121U, 0x0c0c0c0cU, 0x7d7d7d7dU, +} +}; + + +const word32* AES::Te0 = AES::Te[0]; +const word32* AES::Te1 = AES::Te[1]; +const word32* AES::Te2 = AES::Te[2]; +const word32* AES::Te3 = AES::Te[3]; +const word32* AES::Te4 = AES::Te[4]; + +const word32* AES::Td0 = AES::Td[0]; +const word32* AES::Td1 = AES::Td[1]; +const word32* AES::Td2 = AES::Td[2]; +const word32* AES::Td3 = AES::Td[3]; +const word32* AES::Td4 = AES::Td[4]; + + + +} // namespace + diff --git a/externals/mysql/extlib/yassl/taocrypt/src/aestables.cpp b/externals/mysql/extlib/yassl/taocrypt/src/aestables.cpp new file mode 100644 index 00000000000..e9d6b7a80cb --- /dev/null +++ b/externals/mysql/extlib/yassl/taocrypt/src/aestables.cpp @@ -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 diff --git a/externals/mysql/extlib/yassl/taocrypt/src/algebra.cpp b/externals/mysql/extlib/yassl/taocrypt/src/algebra.cpp new file mode 100644 index 00000000000..cb597c41552 --- /dev/null +++ b/externals/mysql/extlib/yassl/taocrypt/src/algebra.cpp @@ -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 + diff --git a/externals/mysql/extlib/yassl/taocrypt/src/arc4.cpp b/externals/mysql/extlib/yassl/taocrypt/src/arc4.cpp new file mode 100644 index 00000000000..0944cc31837 --- /dev/null +++ b/externals/mysql/extlib/yassl/taocrypt/src/arc4.cpp @@ -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 diff --git a/externals/mysql/extlib/yassl/taocrypt/src/asn.cpp b/externals/mysql/extlib/yassl/taocrypt/src/asn.cpp new file mode 100644 index 00000000000..a06ab658c7b --- /dev/null +++ b/externals/mysql/extlib/yassl/taocrypt/src/asn.cpp @@ -0,0 +1,1185 @@ +/* + 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.cpp implements ASN1 BER, PublicKey, and x509v3 decoding +*/ + +#include "runtime.hpp" +#include "asn.hpp" +#include "file.hpp" +#include "integer.hpp" +#include "rsa.hpp" +#include "dsa.hpp" +#include "dh.hpp" +#include "md5.hpp" +#include "md2.hpp" +#include "sha.hpp" +#include "coding.hpp" +#include <time.h> // gmtime(); +#include "memory.hpp" // some auto_ptr don't have reset, also need auto_array + + +namespace TaoCrypt { + +namespace { // locals + + +// to the minute +bool operator>(tm& a, tm& b) +{ + if (a.tm_year > b.tm_year) + return true; + + if (a.tm_year == b.tm_year && a.tm_mon > b.tm_mon) + return true; + + if (a.tm_year == b.tm_year && a.tm_mon == b.tm_mon && a.tm_mday >b.tm_mday) + return true; + + if (a.tm_year == b.tm_year && a.tm_mon == b.tm_mon && + a.tm_mday == b.tm_mday && a.tm_hour > b.tm_hour) + return true; + + if (a.tm_year == b.tm_year && a.tm_mon == b.tm_mon && + a.tm_mday == b.tm_mday && a.tm_hour == b.tm_hour && + a.tm_min > b.tm_min) + return true; + + return false; +} + + +bool operator<(tm& a, tm&b) +{ + return !(a>b); +} + + +// like atoi but only use first byte +word32 btoi(byte b) +{ + return b - 0x30; +} + + +// two byte date/time, add to value +void GetTime(int& value, const byte* date, int& i) +{ + value += btoi(date[i++]) * 10; + value += btoi(date[i++]); +} + + +// Make sure before and after dates are valid +bool ValidateDate(const byte* date, byte format, CertDecoder::DateType dt) +{ + tm certTime; + memset(&certTime, 0, sizeof(certTime)); + int i = 0; + + if (format == UTC_TIME) { + if (btoi(date[0]) >= 5) + certTime.tm_year = 1900; + else + certTime.tm_year = 2000; + } + else { // format == GENERALIZED_TIME + certTime.tm_year += btoi(date[i++]) * 1000; + certTime.tm_year += btoi(date[i++]) * 100; + } + + GetTime(certTime.tm_year, date, i); certTime.tm_year -= 1900; // adjust + GetTime(certTime.tm_mon, date, i); certTime.tm_mon -= 1; // adjust + GetTime(certTime.tm_mday, date, i); + GetTime(certTime.tm_hour, date, i); + GetTime(certTime.tm_min, date, i); + GetTime(certTime.tm_sec, date, i); + + assert(date[i] == 'Z'); // only Zulu supported for this profile + + time_t ltime = time(0); + tm* localTime = gmtime(<ime); + + if (dt == CertDecoder::BEFORE) { + if (*localTime < certTime) + return false; + } + else + if (*localTime > certTime) + return false; + + return true; +} + + +class BadCertificate {}; + +} // local namespace + + + +// used by Integer as well +word32 GetLength(Source& source) +{ + word32 length = 0; + + byte b = source.next(); + if (b >= LONG_LENGTH) { + word32 bytes = b & 0x7F; + + while (bytes--) { + b = source.next(); + length = (length << 8) | b; + } + } + else + length = b; + + return length; +} + + +word32 SetLength(word32 length, byte* output) +{ + word32 i = 0; + + if (length < LONG_LENGTH) + output[i++] = length; + else { + output[i++] = BytePrecision(length) | 0x80; + + for (int j = BytePrecision(length); j; --j) { + output[i] = length >> (j - 1) * 8; + i++; + } + } + return i; +} + + +PublicKey::PublicKey(const byte* k, word32 s) : key_(0), sz_(0) +{ + if (s) { + SetSize(s); + SetKey(k); + } +} + + +void PublicKey::SetSize(word32 s) +{ + sz_ = s; + key_ = NEW_TC byte[sz_]; +} + + +void PublicKey::SetKey(const byte* k) +{ + memcpy(key_, k, sz_); +} + + +void PublicKey::AddToEnd(const byte* data, word32 len) +{ + mySTL::auto_array<byte> tmp(NEW_TC byte[sz_ + len]); + + memcpy(tmp.get(), key_, sz_); + memcpy(tmp.get() + sz_, data, len); + + byte* del = 0; + STL::swap(del, key_); + tcArrayDelete(del); + + key_ = tmp.release(); + sz_ += len; +} + + +Signer::Signer(const byte* k, word32 kSz, const char* n, const byte* h) + : key_(k, kSz) +{ + int sz = strlen(n); + memcpy(name_, n, sz); + name_[sz] = 0; + + memcpy(hash_, h, SHA::DIGEST_SIZE); +} + +Signer::~Signer() +{ +} + + +Error BER_Decoder::GetError() +{ + return source_.GetError(); +} + + +Integer& BER_Decoder::GetInteger(Integer& integer) +{ + if (!source_.GetError().What()) + integer.Decode(source_); + return integer; +} + + +// Read a Sequence, return length +word32 BER_Decoder::GetSequence() +{ + if (source_.GetError().What()) return 0; + + byte b = source_.next(); + if (b != (SEQUENCE | CONSTRUCTED)) { + source_.SetError(SEQUENCE_E); + return 0; + } + + return GetLength(source_); +} + + +// Read a Sequence, return length +word32 BER_Decoder::GetSet() +{ + if (source_.GetError().What()) return 0; + + byte b = source_.next(); + if (b != (SET | CONSTRUCTED)) { + source_.SetError(SET_E); + return 0; + } + + return GetLength(source_); +} + + +// Read Version, return it +word32 BER_Decoder::GetVersion() +{ + if (source_.GetError().What()) return 0; + + byte b = source_.next(); + if (b != INTEGER) { + source_.SetError(INTEGER_E); + return 0; + } + + b = source_.next(); + if (b != 0x01) { + source_.SetError(VERSION_E); + return 0; + } + + return source_.next(); +} + + +// Read ExplicitVersion, return it or 0 if not there (not an error) +word32 BER_Decoder::GetExplicitVersion() +{ + if (source_.GetError().What()) return 0; + + byte b = source_.next(); + + if (b == (CONTEXT_SPECIFIC | CONSTRUCTED)) { // not an error if not here + source_.next(); + return GetVersion(); + } + else + source_.prev(); // put back + + return 0; +} + + +// Decode a BER encoded RSA Private Key +void RSA_Private_Decoder::Decode(RSA_PrivateKey& key) +{ + ReadHeader(); + if (source_.GetError().What()) return; + // public + key.SetModulus(GetInteger(Integer().Ref())); + key.SetPublicExponent(GetInteger(Integer().Ref())); + + // private + key.SetPrivateExponent(GetInteger(Integer().Ref())); + key.SetPrime1(GetInteger(Integer().Ref())); + key.SetPrime2(GetInteger(Integer().Ref())); + key.SetModPrime1PrivateExponent(GetInteger(Integer().Ref())); + key.SetModPrime2PrivateExponent(GetInteger(Integer().Ref())); + key.SetMultiplicativeInverseOfPrime2ModPrime1(GetInteger(Integer().Ref())); +} + + +void RSA_Private_Decoder::ReadHeader() +{ + GetSequence(); + GetVersion(); +} + + +// Decode a BER encoded DSA Private Key +void DSA_Private_Decoder::Decode(DSA_PrivateKey& key) +{ + ReadHeader(); + if (source_.GetError().What()) return; + // group parameters + key.SetModulus(GetInteger(Integer().Ref())); + key.SetSubGroupOrder(GetInteger(Integer().Ref())); + key.SetSubGroupGenerator(GetInteger(Integer().Ref())); + + // key + key.SetPublicPart(GetInteger(Integer().Ref())); + key.SetPrivatePart(GetInteger(Integer().Ref())); +} + + +void DSA_Private_Decoder::ReadHeader() +{ + GetSequence(); + GetVersion(); +} + + +// Decode a BER encoded RSA Public Key +void RSA_Public_Decoder::Decode(RSA_PublicKey& key) +{ + ReadHeader(); + if (source_.GetError().What()) return; + + // public key + key.SetModulus(GetInteger(Integer().Ref())); + key.SetPublicExponent(GetInteger(Integer().Ref())); +} + + +void RSA_Public_Decoder::ReadHeader() +{ + GetSequence(); +} + + +// Decode a BER encoded DSA Public Key +void DSA_Public_Decoder::Decode(DSA_PublicKey& key) +{ + ReadHeader(); + if (source_.GetError().What()) return; + + // group parameters + key.SetModulus(GetInteger(Integer().Ref())); + key.SetSubGroupOrder(GetInteger(Integer().Ref())); + key.SetSubGroupGenerator(GetInteger(Integer().Ref())); + + // key + key.SetPublicPart(GetInteger(Integer().Ref())); +} + + +void DSA_Public_Decoder::ReadHeader() +{ + GetSequence(); +} + + +void DH_Decoder::ReadHeader() +{ + GetSequence(); +} + + +// Decode a BER encoded Diffie-Hellman Key +void DH_Decoder::Decode(DH& key) +{ + ReadHeader(); + if (source_.GetError().What()) return; + + // group parms + key.SetP(GetInteger(Integer().Ref())); + key.SetG(GetInteger(Integer().Ref())); +} + + +CertDecoder::CertDecoder(Source& s, bool decode, SignerList* signers, + bool noVerify, CertType ct) + : BER_Decoder(s), certBegin_(0), sigIndex_(0), sigLength_(0), + signature_(0), verify_(!noVerify) +{ + issuer_[0] = 0; + subject_[0] = 0; + + if (decode) + Decode(signers, ct); + +} + + +CertDecoder::~CertDecoder() +{ + tcArrayDelete(signature_); +} + + +// process certificate header, set signature offset +void CertDecoder::ReadHeader() +{ + if (source_.GetError().What()) return; + + GetSequence(); // total + certBegin_ = source_.get_index(); + + sigIndex_ = GetSequence(); // this cert + sigIndex_ += source_.get_index(); + + GetExplicitVersion(); // version + GetInteger(Integer().Ref()); // serial number +} + + +// Decode a x509v3 Certificate +void CertDecoder::Decode(SignerList* signers, CertType ct) +{ + if (source_.GetError().What()) return; + DecodeToKey(); + if (source_.GetError().What()) return; + + if (source_.get_index() != sigIndex_) + source_.set_index(sigIndex_); + + word32 confirmOID = GetAlgoId(); + GetSignature(); + if (source_.GetError().What()) return; + + if ( confirmOID != signatureOID_ ) { + source_.SetError(SIG_OID_E); + return; + } + + if (ct != CA && verify_ && !ValidateSignature(signers)) + source_.SetError(SIG_OTHER_E); +} + + +void CertDecoder::DecodeToKey() +{ + ReadHeader(); + signatureOID_ = GetAlgoId(); + GetName(ISSUER); + GetValidity(); + GetName(SUBJECT); + GetKey(); +} + + +// Read public key +void CertDecoder::GetKey() +{ + if (source_.GetError().What()) return; + + GetSequence(); + keyOID_ = GetAlgoId(); + + if (keyOID_ == RSAk) { + byte b = source_.next(); + if (b != BIT_STRING) { + source_.SetError(BIT_STR_E); + return; + } + b = source_.next(); // length, future + b = source_.next(); + while(b != 0) + b = source_.next(); + } + else if (keyOID_ == DSAk) + ; // do nothing + else { + source_.SetError(UNKNOWN_OID_E); + return; + } + + StoreKey(); + if (keyOID_ == DSAk) + AddDSA(); +} + + +// Save public key +void CertDecoder::StoreKey() +{ + if (source_.GetError().What()) return; + + word32 read = source_.get_index(); + word32 length = GetSequence(); + + read = source_.get_index() - read; + length += read; + + while (read--) source_.prev(); + + key_.SetSize(length); + key_.SetKey(source_.get_current()); + source_.advance(length); +} + + +// DSA has public key after group +void CertDecoder::AddDSA() +{ + if (source_.GetError().What()) return; + + byte b = source_.next(); + if (b != BIT_STRING) { + source_.SetError(BIT_STR_E); + return; + } + b = source_.next(); // length, future + b = source_.next(); + while(b != 0) + b = source_.next(); + + word32 idx = source_.get_index(); + b = source_.next(); + if (b != INTEGER) { + source_.SetError(INTEGER_E); + return; + } + + word32 length = GetLength(source_); + length += source_.get_index() - idx; + + key_.AddToEnd(source_.get_buffer() + idx, length); +} + + +// process algo OID by summing, return it +word32 CertDecoder::GetAlgoId() +{ + if (source_.GetError().What()) return 0; + word32 length = GetSequence(); + + byte b = source_.next(); + if (b != OBJECT_IDENTIFIER) { + source_.SetError(OBJECT_ID_E); + return 0; + } + + length = GetLength(source_); + word32 oid = 0; + + while(length--) + oid += source_.next(); // just sum it up for now + + if (oid != SHAwDSA && oid != DSAk) { + b = source_.next(); // should have NULL tag and 0 + + if (b != TAG_NULL) { + source_.SetError(TAG_NULL_E); + return 0; + } + + b = source_.next(); + if (b != 0) { + source_.SetError(EXPECT_0_E); + return 0; + } + } + + return oid; +} + + +// read cert signature, store in signature_ +word32 CertDecoder::GetSignature() +{ + if (source_.GetError().What()) return 0; + byte b = source_.next(); + + if (b != BIT_STRING) { + source_.SetError(BIT_STR_E); + return 0; + } + + sigLength_ = GetLength(source_); + + b = source_.next(); + if (b != 0) { + source_.SetError(EXPECT_0_E); + return 0; + } + sigLength_--; + + signature_ = NEW_TC byte[sigLength_]; + memcpy(signature_, source_.get_current(), sigLength_); + source_.advance(sigLength_); + + return sigLength_; +} + + +// read cert digest, store in signature_ +word32 CertDecoder::GetDigest() +{ + if (source_.GetError().What()) return 0; + byte b = source_.next(); + + if (b != OCTET_STRING) { + source_.SetError(OCTET_STR_E); + return 0; + } + + sigLength_ = GetLength(source_); + + signature_ = NEW_TC byte[sigLength_]; + memcpy(signature_, source_.get_current(), sigLength_); + source_.advance(sigLength_); + + return sigLength_; +} + + +// process NAME, either issuer or subject +void CertDecoder::GetName(NameType nt) +{ + if (source_.GetError().What()) return; + + SHA sha; + word32 length = GetSequence(); // length of all distinguished names + assert (length < ASN_NAME_MAX); + length += source_.get_index(); + + char* ptr = (nt == ISSUER) ? issuer_ : subject_; + word32 idx = 0; + + while (source_.get_index() < length) { + GetSet(); + GetSequence(); + + byte b = source_.next(); + if (b != OBJECT_IDENTIFIER) { + source_.SetError(OBJECT_ID_E); + return; + } + + word32 oidSz = GetLength(source_); + byte joint[2]; + memcpy(joint, source_.get_current(), sizeof(joint)); + + // v1 name types + if (joint[0] == 0x55 && joint[1] == 0x04) { + source_.advance(2); + byte id = source_.next(); + b = source_.next(); // strType + word32 strLen = GetLength(source_); + bool copy = false; + + if (id == COMMON_NAME) { + memcpy(&ptr[idx], "/CN=", 4); + idx += 4; + copy = true; + } + else if (id == SUR_NAME) { + memcpy(&ptr[idx], "/SN=", 4); + idx += 4; + copy = true; + } + else if (id == COUNTRY_NAME) { + memcpy(&ptr[idx], "/C=", 3); + idx += 3; + copy = true; + } + else if (id == LOCALITY_NAME) { + memcpy(&ptr[idx], "/L=", 3); + idx += 3; + copy = true; + } + else if (id == STATE_NAME) { + memcpy(&ptr[idx], "/ST=", 4); + idx += 4; + copy = true; + } + else if (id == ORG_NAME) { + memcpy(&ptr[idx], "/O=", 3); + idx += 3; + copy = true; + } + else if (id == ORGUNIT_NAME) { + memcpy(&ptr[idx], "/OU=", 4); + idx += 4; + copy = true; + } + + if (copy) { + memcpy(&ptr[idx], source_.get_current(), strLen); + idx += strLen; + } + + sha.Update(source_.get_current(), strLen); + source_.advance(strLen); + } + else { + bool email = false; + if (joint[0] == 0x2a && joint[1] == 0x86) // email id hdr + email = true; + + source_.advance(oidSz + 1); + word32 length = GetLength(source_); + + if (email) { + memcpy(&ptr[idx], "/emailAddress=", 14); + idx += 14; + + memcpy(&ptr[idx], source_.get_current(), length); + idx += length; + } + + source_.advance(length); + } + } + ptr[idx++] = 0; + + if (nt == ISSUER) + sha.Final(issuerHash_); + else + sha.Final(subjectHash_); +} + + +// process a Date, either BEFORE or AFTER +void CertDecoder::GetDate(DateType dt) +{ + if (source_.GetError().What()) return; + + byte b = source_.next(); + if (b != UTC_TIME && b != GENERALIZED_TIME) { + source_.SetError(TIME_E); + return; + } + + word32 length = GetLength(source_); + byte date[MAX_DATE_SZ]; + if (length > MAX_DATE_SZ || length < MIN_DATE_SZ) { + source_.SetError(DATE_SZ_E); + return; + } + + memcpy(date, source_.get_current(), length); + source_.advance(length); + + if (!ValidateDate(date, b, dt) && verify_) + if (dt == BEFORE) + source_.SetError(BEFORE_DATE_E); + else + source_.SetError(AFTER_DATE_E); + + // save for later use + if (dt == BEFORE) { + memcpy(beforeDate_, date, length); + beforeDate_[length] = 0; + } + else { // after + memcpy(afterDate_, date, length); + afterDate_[length] = 0; + } +} + + +void CertDecoder::GetValidity() +{ + if (source_.GetError().What()) return; + + GetSequence(); + GetDate(BEFORE); + GetDate(AFTER); +} + + +bool CertDecoder::ValidateSelfSignature() +{ + Source pub(key_.GetKey(), key_.size()); + return ConfirmSignature(pub); +} + + +// extract compare signature hash from plain and place into digest +void CertDecoder::GetCompareHash(const byte* plain, word32 sz, byte* digest, + word32 digSz) +{ + if (source_.GetError().What()) return; + + Source s(plain, sz); + CertDecoder dec(s, false); + + dec.GetSequence(); + dec.GetAlgoId(); + dec.GetDigest(); + + if (dec.sigLength_ > digSz) { + source_.SetError(SIG_LEN_E); + return; + } + + memcpy(digest, dec.signature_, dec.sigLength_); +} + + +// validate signature signed by someone else +bool CertDecoder::ValidateSignature(SignerList* signers) +{ + assert(signers); + + SignerList::iterator first = signers->begin(); + SignerList::iterator last = signers->end(); + + while (first != last) { + if ( memcmp(issuerHash_, (*first)->GetHash(), SHA::DIGEST_SIZE) == 0) { + + const PublicKey& iKey = (*first)->GetPublicKey(); + Source pub(iKey.GetKey(), iKey.size()); + return ConfirmSignature(pub); + } + ++first; + } + return false; +} + + +// confirm certificate signature +bool CertDecoder::ConfirmSignature(Source& pub) +{ + HashType ht; + mySTL::auto_ptr<HASH> hasher; + + if (signatureOID_ == MD5wRSA) { + hasher.reset(NEW_TC MD5); + ht = MD5h; + } + else if (signatureOID_ == MD2wRSA) { + hasher.reset(NEW_TC MD2); + ht = MD2h; + } + else if (signatureOID_ == SHAwRSA || signatureOID_ == SHAwDSA) { + hasher.reset(NEW_TC SHA); + ht = SHAh; + } + else { + source_.SetError(UNKOWN_SIG_E); + return false; + } + + byte digest[SHA::DIGEST_SIZE]; // largest size + + hasher->Update(source_.get_buffer() + certBegin_, sigIndex_ - certBegin_); + hasher->Final(digest); + + if (keyOID_ == RSAk) { + // put in ASN.1 signature format + Source build; + Signature_Encoder(digest, hasher->getDigestSize(), ht, build); + + RSA_PublicKey pubKey(pub); + RSAES_Encryptor enc(pubKey); + + return enc.SSL_Verify(build.get_buffer(), build.size(), signature_); + } + else { // DSA + // extract r and s from sequence + byte seqDecoded[DSA_SIG_SZ]; + DecodeDSA_Signature(seqDecoded, signature_, sigLength_); + + DSA_PublicKey pubKey(pub); + DSA_Verifier ver(pubKey); + + return ver.Verify(digest, seqDecoded); + } +} + + +Signature_Encoder::Signature_Encoder(const byte* dig, word32 digSz, + HashType digOID, Source& source) +{ + // build bottom up + + // Digest + byte digArray[MAX_DIGEST_SZ]; + word32 digestSz = SetDigest(dig, digSz, digArray); + + // AlgoID + byte algoArray[MAX_ALGO_SZ]; + word32 algoSz = SetAlgoID(digOID, algoArray); + + // Sequence + byte seqArray[MAX_SEQ_SZ]; + word32 seqSz = SetSequence(digestSz + algoSz, seqArray); + + source.grow(seqSz + algoSz + digestSz); // make sure enough room + source.add(seqArray, seqSz); + source.add(algoArray, algoSz); + source.add(digArray, digestSz); +} + + + +word32 Signature_Encoder::SetDigest(const byte* d, word32 dSz, byte* output) +{ + output[0] = OCTET_STRING; + output[1] = dSz; + memcpy(&output[2], d, dSz); + + return dSz + 2; +} + + + +word32 DER_Encoder::SetAlgoID(HashType aOID, byte* output) +{ + // adding TAG_NULL and 0 to end + static const byte shaAlgoID[] = { 0x2b, 0x0e, 0x03, 0x02, 0x1a, + 0x05, 0x00 }; + static const byte md5AlgoID[] = { 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, + 0x02, 0x05, 0x05, 0x00 }; + static const byte md2AlgoID[] = { 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, + 0x02, 0x02, 0x05, 0x00}; + + int algoSz = 0; + const byte* algoName = 0; + + switch (aOID) { + case SHAh: + algoSz = sizeof(shaAlgoID); + algoName = shaAlgoID; + break; + + case MD2h: + algoSz = sizeof(md2AlgoID); + algoName = md2AlgoID; + break; + + case MD5h: + algoSz = sizeof(md5AlgoID); + algoName = md5AlgoID; + break; + + default: + error_.SetError(UNKOWN_HASH_E); + return 0; + } + + + byte ID_Length[MAX_LENGTH_SZ]; + word32 idSz = SetLength(algoSz - 2, ID_Length); // don't include TAG_NULL/0 + + byte seqArray[MAX_SEQ_SZ + 1]; // add object_id to end + word32 seqSz = SetSequence(idSz + algoSz + 1, seqArray); + seqArray[seqSz++] = OBJECT_IDENTIFIER; + + memcpy(output, seqArray, seqSz); + memcpy(output + seqSz, ID_Length, idSz); + memcpy(output + seqSz + idSz, algoName, algoSz); + + return seqSz + idSz + algoSz; +} + + +word32 SetSequence(word32 len, byte* output) +{ + + output[0] = SEQUENCE | CONSTRUCTED; + return SetLength(len, output + 1) + 1; +} + + +word32 EncodeDSA_Signature(const byte* signature, byte* output) +{ + Integer r(signature, 20); + Integer s(signature + 20, 20); + + return EncodeDSA_Signature(r, s, output); +} + + +word32 EncodeDSA_Signature(const Integer& r, const Integer& s, byte* output) +{ + word32 rSz = r.ByteCount(); + word32 sSz = s.ByteCount(); + + byte rLen[MAX_LENGTH_SZ + 1]; + byte sLen[MAX_LENGTH_SZ + 1]; + + rLen[0] = INTEGER; + sLen[0] = INTEGER; + + word32 rLenSz = SetLength(rSz, &rLen[1]) + 1; + word32 sLenSz = SetLength(sSz, &sLen[1]) + 1; + + byte seqArray[MAX_SEQ_SZ]; + + word32 seqSz = SetSequence(rLenSz + rSz + sLenSz + sSz, seqArray); + + // seq + memcpy(output, seqArray, seqSz); + // r + memcpy(output + seqSz, rLen, rLenSz); + r.Encode(output + seqSz + rLenSz, rSz); + // s + memcpy(output + seqSz + rLenSz + rSz, sLen, sLenSz); + s.Encode(output + seqSz + rLenSz + rSz + sLenSz, sSz); + + return seqSz + rLenSz + rSz + sLenSz + sSz; +} + + +// put sequence encoded dsa signature into decoded in 2 20 byte integers +word32 DecodeDSA_Signature(byte* decoded, const byte* encoded, word32 sz) +{ + Source source(encoded, sz); + + if (source.next() != (SEQUENCE | CONSTRUCTED)) { + source.SetError(SEQUENCE_E); + return 0; + } + + GetLength(source); // total + + // r + if (source.next() != INTEGER) { + source.SetError(INTEGER_E); + return 0; + } + word32 rLen = GetLength(source); + if (rLen != 20) + if (rLen == 21) { // zero at front, eat + source.next(); + --rLen; + } + else if (rLen == 19) { // add zero to front so 20 bytes + decoded[0] = 0; + decoded++; + } + else { + source.SetError(DSA_SZ_E); + return 0; + } + memcpy(decoded, source.get_buffer() + source.get_index(), rLen); + source.advance(rLen); + + // s + if (source.next() != INTEGER) { + source.SetError(INTEGER_E); + return 0; + } + word32 sLen = GetLength(source); + if (sLen != 20) + if (sLen == 21) { + source.next(); // zero at front, eat + --sLen; + } + else if (sLen == 19) { + decoded[rLen] = 0; // add zero to front so 20 bytes + decoded++; + } + else { + source.SetError(DSA_SZ_E); + return 0; + } + memcpy(decoded + rLen, source.get_buffer() + source.get_index(), sLen); + source.advance(sLen); + + return 40; +} + + +// Get Cert in PEM format from BEGIN to END +int GetCert(Source& source) +{ + char header[] = "-----BEGIN CERTIFICATE-----"; + char footer[] = "-----END CERTIFICATE-----"; + + char* begin = strstr((char*)source.get_buffer(), header); + char* end = strstr((char*)source.get_buffer(), footer); + + if (!begin || !end || begin >= end) return -1; + + end += strlen(footer); + if (*end == '\r') end++; + + Source tmp((byte*)begin, end - begin + 1); + source.Swap(tmp); + + return 0; +} + + + +// Decode a BER encoded PKCS12 structure +void PKCS12_Decoder::Decode() +{ + ReadHeader(); + if (source_.GetError().What()) return; + + // Get AuthSafe + + GetSequence(); + + // get object id + byte obj_id = source_.next(); + if (obj_id != OBJECT_IDENTIFIER) { + source_.SetError(OBJECT_ID_E); + return; + } + + word32 length = GetLength(source_); + + word32 algo_sum = 0; + while (length--) + algo_sum += source_.next(); + + + + + + + // Get MacData optional + /* + mac digestInfo like certdecoder::getdigest? + macsalt octet string + iter integer + + */ +} + + +void PKCS12_Decoder::ReadHeader() +{ + // Gets Version + GetSequence(); + GetVersion(); +} + + +// Get Cert in PEM format from pkcs12 file +int GetPKCS_Cert(const char* password, Source& source) +{ + PKCS12_Decoder pkcs12(source); + pkcs12.Decode(); + + return 0; +} + + + +} // namespace diff --git a/externals/mysql/extlib/yassl/taocrypt/src/bftables.cpp b/externals/mysql/extlib/yassl/taocrypt/src/bftables.cpp new file mode 100644 index 00000000000..46469472e6a --- /dev/null +++ b/externals/mysql/extlib/yassl/taocrypt/src/bftables.cpp @@ -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 + diff --git a/externals/mysql/extlib/yassl/taocrypt/src/blowfish.cpp b/externals/mysql/extlib/yassl/taocrypt/src/blowfish.cpp new file mode 100644 index 00000000000..66ff4d829d7 --- /dev/null +++ b/externals/mysql/extlib/yassl/taocrypt/src/blowfish.cpp @@ -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 + diff --git a/externals/mysql/extlib/yassl/taocrypt/src/coding.cpp b/externals/mysql/extlib/yassl/taocrypt/src/coding.cpp new file mode 100644 index 00000000000..7a9d50aaac9 --- /dev/null +++ b/externals/mysql/extlib/yassl/taocrypt/src/coding.cpp @@ -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 diff --git a/externals/mysql/extlib/yassl/taocrypt/src/crypto.cpp b/externals/mysql/extlib/yassl/taocrypt/src/crypto.cpp new file mode 100644 index 00000000000..90d406bf0c2 --- /dev/null +++ b/externals/mysql/extlib/yassl/taocrypt/src/crypto.cpp @@ -0,0 +1,37 @@ +/* + 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. +*/ + +/* put features that other apps expect from OpenSSL type crypto */ + + + +extern "C" { + + // for libcurl configure test, these are the signatures they use + // locking handled internally by library + char CRYPTO_lock() { return 0;} + char CRYPTO_add_lock() { return 0;} + + + // for openvpn, test are the signatures they use + char EVP_CIPHER_CTX_init() { return 0; } + char CRYPTO_mem_ctrl() { return 0; } +} // extern "C" + + + diff --git a/externals/mysql/extlib/yassl/taocrypt/src/des.cpp b/externals/mysql/extlib/yassl/taocrypt/src/des.cpp new file mode 100644 index 00000000000..5cffeaea9c9 --- /dev/null +++ b/externals/mysql/extlib/yassl/taocrypt/src/des.cpp @@ -0,0 +1,776 @@ +/* + 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++ part based on Wei Dai's des.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 "des.hpp" +#ifdef USE_SYS_STL + #include <algorithm> +#else + #include "algorithm.hpp" +#endif + + +namespace STL = STL_NAMESPACE; + + + +namespace TaoCrypt { + + +/* permuted choice table (key) */ +static const byte pc1[] = { + 57, 49, 41, 33, 25, 17, 9, + 1, 58, 50, 42, 34, 26, 18, + 10, 2, 59, 51, 43, 35, 27, + 19, 11, 3, 60, 52, 44, 36, + + 63, 55, 47, 39, 31, 23, 15, + 7, 62, 54, 46, 38, 30, 22, + 14, 6, 61, 53, 45, 37, 29, + 21, 13, 5, 28, 20, 12, 4 +}; + +/* number left rotations of pc1 */ +static const byte totrot[] = { + 1,2,4,6,8,10,12,14,15,17,19,21,23,25,27,28 +}; + +/* permuted choice key (table) */ +static const byte pc2[] = { + 14, 17, 11, 24, 1, 5, + 3, 28, 15, 6, 21, 10, + 23, 19, 12, 4, 26, 8, + 16, 7, 27, 20, 13, 2, + 41, 52, 31, 37, 47, 55, + 30, 40, 51, 45, 33, 48, + 44, 49, 39, 56, 34, 53, + 46, 42, 50, 36, 29, 32 +}; + +/* End of DES-defined tables */ + +/* bit 0 is left-most in byte */ +static const int bytebit[] = { + 0200,0100,040,020,010,04,02,01 +}; + +const word32 Spbox[8][64] = { +{ +0x01010400,0x00000000,0x00010000,0x01010404, +0x01010004,0x00010404,0x00000004,0x00010000, +0x00000400,0x01010400,0x01010404,0x00000400, +0x01000404,0x01010004,0x01000000,0x00000004, +0x00000404,0x01000400,0x01000400,0x00010400, +0x00010400,0x01010000,0x01010000,0x01000404, +0x00010004,0x01000004,0x01000004,0x00010004, +0x00000000,0x00000404,0x00010404,0x01000000, +0x00010000,0x01010404,0x00000004,0x01010000, +0x01010400,0x01000000,0x01000000,0x00000400, +0x01010004,0x00010000,0x00010400,0x01000004, +0x00000400,0x00000004,0x01000404,0x00010404, +0x01010404,0x00010004,0x01010000,0x01000404, +0x01000004,0x00000404,0x00010404,0x01010400, +0x00000404,0x01000400,0x01000400,0x00000000, +0x00010004,0x00010400,0x00000000,0x01010004}, +{ +0x80108020,0x80008000,0x00008000,0x00108020, +0x00100000,0x00000020,0x80100020,0x80008020, +0x80000020,0x80108020,0x80108000,0x80000000, +0x80008000,0x00100000,0x00000020,0x80100020, +0x00108000,0x00100020,0x80008020,0x00000000, +0x80000000,0x00008000,0x00108020,0x80100000, +0x00100020,0x80000020,0x00000000,0x00108000, +0x00008020,0x80108000,0x80100000,0x00008020, +0x00000000,0x00108020,0x80100020,0x00100000, +0x80008020,0x80100000,0x80108000,0x00008000, +0x80100000,0x80008000,0x00000020,0x80108020, +0x00108020,0x00000020,0x00008000,0x80000000, +0x00008020,0x80108000,0x00100000,0x80000020, +0x00100020,0x80008020,0x80000020,0x00100020, +0x00108000,0x00000000,0x80008000,0x00008020, +0x80000000,0x80100020,0x80108020,0x00108000}, +{ +0x00000208,0x08020200,0x00000000,0x08020008, +0x08000200,0x00000000,0x00020208,0x08000200, +0x00020008,0x08000008,0x08000008,0x00020000, +0x08020208,0x00020008,0x08020000,0x00000208, +0x08000000,0x00000008,0x08020200,0x00000200, +0x00020200,0x08020000,0x08020008,0x00020208, +0x08000208,0x00020200,0x00020000,0x08000208, +0x00000008,0x08020208,0x00000200,0x08000000, +0x08020200,0x08000000,0x00020008,0x00000208, +0x00020000,0x08020200,0x08000200,0x00000000, +0x00000200,0x00020008,0x08020208,0x08000200, +0x08000008,0x00000200,0x00000000,0x08020008, +0x08000208,0x00020000,0x08000000,0x08020208, +0x00000008,0x00020208,0x00020200,0x08000008, +0x08020000,0x08000208,0x00000208,0x08020000, +0x00020208,0x00000008,0x08020008,0x00020200}, +{ +0x00802001,0x00002081,0x00002081,0x00000080, +0x00802080,0x00800081,0x00800001,0x00002001, +0x00000000,0x00802000,0x00802000,0x00802081, +0x00000081,0x00000000,0x00800080,0x00800001, +0x00000001,0x00002000,0x00800000,0x00802001, +0x00000080,0x00800000,0x00002001,0x00002080, +0x00800081,0x00000001,0x00002080,0x00800080, +0x00002000,0x00802080,0x00802081,0x00000081, +0x00800080,0x00800001,0x00802000,0x00802081, +0x00000081,0x00000000,0x00000000,0x00802000, +0x00002080,0x00800080,0x00800081,0x00000001, +0x00802001,0x00002081,0x00002081,0x00000080, +0x00802081,0x00000081,0x00000001,0x00002000, +0x00800001,0x00002001,0x00802080,0x00800081, +0x00002001,0x00002080,0x00800000,0x00802001, +0x00000080,0x00800000,0x00002000,0x00802080}, +{ +0x00000100,0x02080100,0x02080000,0x42000100, +0x00080000,0x00000100,0x40000000,0x02080000, +0x40080100,0x00080000,0x02000100,0x40080100, +0x42000100,0x42080000,0x00080100,0x40000000, +0x02000000,0x40080000,0x40080000,0x00000000, +0x40000100,0x42080100,0x42080100,0x02000100, +0x42080000,0x40000100,0x00000000,0x42000000, +0x02080100,0x02000000,0x42000000,0x00080100, +0x00080000,0x42000100,0x00000100,0x02000000, +0x40000000,0x02080000,0x42000100,0x40080100, +0x02000100,0x40000000,0x42080000,0x02080100, +0x40080100,0x00000100,0x02000000,0x42080000, +0x42080100,0x00080100,0x42000000,0x42080100, +0x02080000,0x00000000,0x40080000,0x42000000, +0x00080100,0x02000100,0x40000100,0x00080000, +0x00000000,0x40080000,0x02080100,0x40000100}, +{ +0x20000010,0x20400000,0x00004000,0x20404010, +0x20400000,0x00000010,0x20404010,0x00400000, +0x20004000,0x00404010,0x00400000,0x20000010, +0x00400010,0x20004000,0x20000000,0x00004010, +0x00000000,0x00400010,0x20004010,0x00004000, +0x00404000,0x20004010,0x00000010,0x20400010, +0x20400010,0x00000000,0x00404010,0x20404000, +0x00004010,0x00404000,0x20404000,0x20000000, +0x20004000,0x00000010,0x20400010,0x00404000, +0x20404010,0x00400000,0x00004010,0x20000010, +0x00400000,0x20004000,0x20000000,0x00004010, +0x20000010,0x20404010,0x00404000,0x20400000, +0x00404010,0x20404000,0x00000000,0x20400010, +0x00000010,0x00004000,0x20400000,0x00404010, +0x00004000,0x00400010,0x20004010,0x00000000, +0x20404000,0x20000000,0x00400010,0x20004010}, +{ +0x00200000,0x04200002,0x04000802,0x00000000, +0x00000800,0x04000802,0x00200802,0x04200800, +0x04200802,0x00200000,0x00000000,0x04000002, +0x00000002,0x04000000,0x04200002,0x00000802, +0x04000800,0x00200802,0x00200002,0x04000800, +0x04000002,0x04200000,0x04200800,0x00200002, +0x04200000,0x00000800,0x00000802,0x04200802, +0x00200800,0x00000002,0x04000000,0x00200800, +0x04000000,0x00200800,0x00200000,0x04000802, +0x04000802,0x04200002,0x04200002,0x00000002, +0x00200002,0x04000000,0x04000800,0x00200000, +0x04200800,0x00000802,0x00200802,0x04200800, +0x00000802,0x04000002,0x04200802,0x04200000, +0x00200800,0x00000000,0x00000002,0x04200802, +0x00000000,0x00200802,0x04200000,0x00000800, +0x04000002,0x04000800,0x00000800,0x00200002}, +{ +0x10001040,0x00001000,0x00040000,0x10041040, +0x10000000,0x10001040,0x00000040,0x10000000, +0x00040040,0x10040000,0x10041040,0x00041000, +0x10041000,0x00041040,0x00001000,0x00000040, +0x10040000,0x10000040,0x10001000,0x00001040, +0x00041000,0x00040040,0x10040040,0x10041000, +0x00001040,0x00000000,0x00000000,0x10040040, +0x10000040,0x10001000,0x00041040,0x00040000, +0x00041040,0x00040000,0x10041000,0x00001000, +0x00000040,0x10040040,0x00001000,0x00041040, +0x10001000,0x00000040,0x10000040,0x10040000, +0x10040040,0x10000000,0x00040000,0x10001040, +0x00000000,0x10041040,0x00040040,0x10000040, +0x10040000,0x10001000,0x10001040,0x00000000, +0x10041040,0x00041000,0x00041000,0x00001040, +0x00001040,0x00040040,0x10000000,0x10041000} +}; + + +void BasicDES::SetKey(const byte* key, word32 /*length*/, CipherDir dir) +{ + byte buffer[56+56+8]; + byte *const pc1m = buffer; /* place to modify pc1 into */ + byte *const pcr = pc1m + 56; /* place to rotate pc1 into */ + byte *const ks = pcr + 56; + register int i,j,l; + int m; + + for (j = 0; j < 56; j++) { /* convert pc1 to bits of key */ + l = pc1[j] - 1; /* integer bit location */ + m = l & 07; /* find bit */ + pc1m[j] = (key[l >> 3] & /* find which key byte l is in */ + bytebit[m]) /* and which bit of that byte */ + ? 1 : 0; /* and store 1-bit result */ + } + for (i = 0; i < 16; i++) { /* key chunk for each iteration */ + memset(ks, 0, 8); /* Clear key schedule */ + for (j = 0; j < 56; j++) /* rotate pc1 the right amount */ + pcr[j] = pc1m[(l = j + totrot[i]) < (j < 28 ? 28 : 56) ? l: l-28]; + /* rotate left and right halves independently */ + for (j = 0; j < 48; j++){ /* select bits individually */ + /* check bit that goes to ks[j] */ + if (pcr[pc2[j] - 1]){ + /* mask it in if it's there */ + l= j % 6; + ks[j/6] |= bytebit[l] >> 2; + } + } + /* Now convert to odd/even interleaved form for use in F */ + k_[2*i] = ((word32)ks[0] << 24) + | ((word32)ks[2] << 16) + | ((word32)ks[4] << 8) + | ((word32)ks[6]); + k_[2*i + 1] = ((word32)ks[1] << 24) + | ((word32)ks[3] << 16) + | ((word32)ks[5] << 8) + | ((word32)ks[7]); + } + + // reverse key schedule order + if (dir == DECRYPTION) + for (i = 0; i < 16; i += 2) { + STL::swap(k_[i], k_[32 - 2 - i]); + STL::swap(k_[i+1], k_[32 - 1 - i]); + } + +} + +static inline void IPERM(word32& left, word32& right) +{ + word32 work; + + right = rotlFixed(right, 4U); + work = (left ^ right) & 0xf0f0f0f0; + left ^= work; + + right = rotrFixed(right^work, 20U); + work = (left ^ right) & 0xffff0000; + left ^= work; + + right = rotrFixed(right^work, 18U); + work = (left ^ right) & 0x33333333; + left ^= work; + + right = rotrFixed(right^work, 6U); + work = (left ^ right) & 0x00ff00ff; + left ^= work; + + right = rotlFixed(right^work, 9U); + work = (left ^ right) & 0xaaaaaaaa; + left = rotlFixed(left^work, 1U); + right ^= work; +} + +static inline void FPERM(word32& left, word32& right) +{ + word32 work; + + right = rotrFixed(right, 1U); + work = (left ^ right) & 0xaaaaaaaa; + right ^= work; + left = rotrFixed(left^work, 9U); + work = (left ^ right) & 0x00ff00ff; + right ^= work; + left = rotlFixed(left^work, 6U); + work = (left ^ right) & 0x33333333; + right ^= work; + left = rotlFixed(left^work, 18U); + work = (left ^ right) & 0xffff0000; + right ^= work; + left = rotlFixed(left^work, 20U); + work = (left ^ right) & 0xf0f0f0f0; + right ^= work; + left = rotrFixed(left^work, 4U); +} + + +void BasicDES::RawProcessBlock(word32& lIn, word32& rIn) const +{ + word32 l = lIn, r = rIn; + const word32* kptr = k_; + + for (unsigned i=0; i<8; i++) + { + word32 work = rotrFixed(r, 4U) ^ kptr[4*i+0]; + l ^= Spbox[6][(work) & 0x3f] + ^ Spbox[4][(work >> 8) & 0x3f] + ^ Spbox[2][(work >> 16) & 0x3f] + ^ Spbox[0][(work >> 24) & 0x3f]; + work = r ^ kptr[4*i+1]; + l ^= Spbox[7][(work) & 0x3f] + ^ Spbox[5][(work >> 8) & 0x3f] + ^ Spbox[3][(work >> 16) & 0x3f] + ^ Spbox[1][(work >> 24) & 0x3f]; + + work = rotrFixed(l, 4U) ^ kptr[4*i+2]; + r ^= Spbox[6][(work) & 0x3f] + ^ Spbox[4][(work >> 8) & 0x3f] + ^ Spbox[2][(work >> 16) & 0x3f] + ^ Spbox[0][(work >> 24) & 0x3f]; + work = l ^ kptr[4*i+3]; + r ^= Spbox[7][(work) & 0x3f] + ^ Spbox[5][(work >> 8) & 0x3f] + ^ Spbox[3][(work >> 16) & 0x3f] + ^ Spbox[1][(work >> 24) & 0x3f]; + } + + lIn = l; rIn = r; +} + + + +typedef BlockGetAndPut<word32, BigEndian> Block; + + +void DES::ProcessAndXorBlock(const byte* in, const byte* xOr, byte* out) const +{ + word32 l,r; + Block::Get(in)(l)(r); + IPERM(l,r); + + RawProcessBlock(l, r); + + FPERM(l,r); + Block::Put(xOr, out)(r)(l); +} + + +void DES_EDE2::SetKey(const byte* key, word32 sz, CipherDir dir) +{ + des1_.SetKey(key, sz, dir); + des2_.SetKey(key + 8, sz, ReverseDir(dir)); +} + + +void DES_EDE2::ProcessAndXorBlock(const byte* in, const byte* xOr, + byte* out) const +{ + word32 l,r; + Block::Get(in)(l)(r); + IPERM(l,r); + + des1_.RawProcessBlock(l, r); + des2_.RawProcessBlock(r, l); + des1_.RawProcessBlock(l, r); + + FPERM(l,r); + Block::Put(xOr, out)(r)(l); +} + + +void DES_EDE3::SetKey(const byte* key, word32 sz, CipherDir dir) +{ + des1_.SetKey(key+(dir==ENCRYPTION?0:2*8), sz, dir); + des2_.SetKey(key+8, sz, ReverseDir(dir)); + des3_.SetKey(key+(dir==DECRYPTION?0:2*8), sz, dir); +} + + + +#if defined(DO_DES_ASM) + +// ia32 optimized version +void DES_EDE3::Process(byte* out, const byte* in, word32 sz) +{ + if (!isMMX) { + Mode_BASE::Process(out, in, sz); + return; + } + + word32 blocks = sz / DES_BLOCK_SIZE; + + if (mode_ == CBC) + if (dir_ == ENCRYPTION) + while (blocks--) { + r_[0] ^= *(word32*)in; + r_[1] ^= *(word32*)(in + 4); + + AsmProcess((byte*)r_, (byte*)r_, (void*)Spbox); + + memcpy(out, r_, DES_BLOCK_SIZE); + + in += DES_BLOCK_SIZE; + out += DES_BLOCK_SIZE; + } + else + while (blocks--) { + AsmProcess(in, out, (void*)Spbox); + + *(word32*)out ^= r_[0]; + *(word32*)(out + 4) ^= r_[1]; + + memcpy(r_, in, DES_BLOCK_SIZE); + + out += DES_BLOCK_SIZE; + in += DES_BLOCK_SIZE; + } + else + while (blocks--) { + AsmProcess(in, out, (void*)Spbox); + + out += DES_BLOCK_SIZE; + in += DES_BLOCK_SIZE; + } +} + +#endif // DO_DES_ASM + + +void DES_EDE3::ProcessAndXorBlock(const byte* in, const byte* xOr, + byte* out) const +{ + word32 l,r; + Block::Get(in)(l)(r); + IPERM(l,r); + + des1_.RawProcessBlock(l, r); + des2_.RawProcessBlock(r, l); + des3_.RawProcessBlock(l, r); + + FPERM(l,r); + Block::Put(xOr, out)(r)(l); +} + + +#if defined(DO_DES_ASM) + +/* Uses IPERM algorithm from above + + left is in eax + right is in ebx + + uses ecx +*/ +#define AsmIPERM() {\ + AS2( rol ebx, 4 ) \ + AS2( mov ecx, eax ) \ + AS2( xor ecx, ebx ) \ + AS2( and ecx, 0xf0f0f0f0 ) \ + AS2( xor ebx, ecx ) \ + AS2( xor eax, ecx ) \ + AS2( ror ebx, 20 ) \ + AS2( mov ecx, eax ) \ + AS2( xor ecx, ebx ) \ + AS2( and ecx, 0xffff0000 ) \ + AS2( xor ebx, ecx ) \ + AS2( xor eax, ecx ) \ + AS2( ror ebx, 18 ) \ + AS2( mov ecx, eax ) \ + AS2( xor ecx, ebx ) \ + AS2( and ecx, 0x33333333 ) \ + AS2( xor ebx, ecx ) \ + AS2( xor eax, ecx ) \ + AS2( ror ebx, 6 ) \ + AS2( mov ecx, eax ) \ + AS2( xor ecx, ebx ) \ + AS2( and ecx, 0x00ff00ff ) \ + AS2( xor ebx, ecx ) \ + AS2( xor eax, ecx ) \ + AS2( rol ebx, 9 ) \ + AS2( mov ecx, eax ) \ + AS2( xor ecx, ebx ) \ + AS2( and ecx, 0xaaaaaaaa ) \ + AS2( xor eax, ecx ) \ + AS2( rol eax, 1 ) \ + AS2( xor ebx, ecx ) } + + +/* Uses FPERM algorithm from above + + left is in eax + right is in ebx + + uses ecx +*/ +#define AsmFPERM() {\ + AS2( ror ebx, 1 ) \ + AS2( mov ecx, eax ) \ + AS2( xor ecx, ebx ) \ + AS2( and ecx, 0xaaaaaaaa ) \ + AS2( xor eax, ecx ) \ + AS2( xor ebx, ecx ) \ + AS2( ror eax, 9 ) \ + AS2( mov ecx, ebx ) \ + AS2( xor ecx, eax ) \ + AS2( and ecx, 0x00ff00ff ) \ + AS2( xor eax, ecx ) \ + AS2( xor ebx, ecx ) \ + AS2( rol eax, 6 ) \ + AS2( mov ecx, ebx ) \ + AS2( xor ecx, eax ) \ + AS2( and ecx, 0x33333333 ) \ + AS2( xor eax, ecx ) \ + AS2( xor ebx, ecx ) \ + AS2( rol eax, 18 ) \ + AS2( mov ecx, ebx ) \ + AS2( xor ecx, eax ) \ + AS2( and ecx, 0xffff0000 ) \ + AS2( xor eax, ecx ) \ + AS2( xor ebx, ecx ) \ + AS2( rol eax, 20 ) \ + AS2( mov ecx, ebx ) \ + AS2( xor ecx, eax ) \ + AS2( and ecx, 0xf0f0f0f0 ) \ + AS2( xor eax, ecx ) \ + AS2( xor ebx, ecx ) \ + AS2( ror eax, 4 ) } + + + + +/* DesRound implements this algorithm: + + word32 work = rotrFixed(r, 4U) ^ key[0]; + l ^= Spbox[6][(work) & 0x3f] + ^ Spbox[4][(work >> 8) & 0x3f] + ^ Spbox[2][(work >> 16) & 0x3f] + ^ Spbox[0][(work >> 24) & 0x3f]; + work = r ^ key[1]; + l ^= Spbox[7][(work) & 0x3f] + ^ Spbox[5][(work >> 8) & 0x3f] + ^ Spbox[3][(work >> 16) & 0x3f] + ^ Spbox[1][(work >> 24) & 0x3f]; + + work = rotrFixed(l, 4U) ^ key[2]; + r ^= Spbox[6][(work) & 0x3f] + ^ Spbox[4][(work >> 8) & 0x3f] + ^ Spbox[2][(work >> 16) & 0x3f] + ^ Spbox[0][(work >> 24) & 0x3f]; + work = l ^ key[3]; + r ^= Spbox[7][(work) & 0x3f] + ^ Spbox[5][(work >> 8) & 0x3f] + ^ Spbox[3][(work >> 16) & 0x3f] + ^ Spbox[1][(work >> 24) & 0x3f]; + + left is in aex + right is in ebx + key is in edx + + edvances key for next round + + uses ecx, esi, and edi +*/ +#define DesRound() \ + AS2( mov ecx, ebx )\ + AS2( mov esi, DWORD PTR [edx] )\ + AS2( ror ecx, 4 )\ + AS2( xor ecx, esi )\ + AS2( and ecx, 0x3f3f3f3f )\ + AS2( movzx esi, cl )\ + AS2( movzx edi, ch )\ + AS2( xor eax, [ebp + esi*4 + 6*256] )\ + AS2( shr ecx, 16 )\ + AS2( xor eax, [ebp + edi*4 + 4*256] )\ + AS2( movzx esi, cl )\ + AS2( movzx edi, ch )\ + AS2( xor eax, [ebp + esi*4 + 2*256] )\ + AS2( mov esi, DWORD PTR [edx + 4] )\ + AS2( xor eax, [ebp + edi*4] )\ + AS2( mov ecx, ebx )\ + AS2( xor ecx, esi )\ + AS2( and ecx, 0x3f3f3f3f )\ + AS2( movzx esi, cl )\ + AS2( movzx edi, ch )\ + AS2( xor eax, [ebp + esi*4 + 7*256] )\ + AS2( shr ecx, 16 )\ + AS2( xor eax, [ebp + edi*4 + 5*256] )\ + AS2( movzx esi, cl )\ + AS2( movzx edi, ch )\ + AS2( xor eax, [ebp + esi*4 + 3*256] )\ + AS2( mov esi, DWORD PTR [edx + 8] )\ + AS2( xor eax, [ebp + edi*4 + 1*256] )\ + AS2( mov ecx, eax )\ + AS2( ror ecx, 4 )\ + AS2( xor ecx, esi )\ + AS2( and ecx, 0x3f3f3f3f )\ + AS2( movzx esi, cl )\ + AS2( movzx edi, ch )\ + AS2( xor ebx, [ebp + esi*4 + 6*256] )\ + AS2( shr ecx, 16 )\ + AS2( xor ebx, [ebp + edi*4 + 4*256] )\ + AS2( movzx esi, cl )\ + AS2( movzx edi, ch )\ + AS2( xor ebx, [ebp + esi*4 + 2*256] )\ + AS2( mov esi, DWORD PTR [edx + 12] )\ + AS2( xor ebx, [ebp + edi*4] )\ + AS2( mov ecx, eax )\ + AS2( xor ecx, esi )\ + AS2( and ecx, 0x3f3f3f3f )\ + AS2( movzx esi, cl )\ + AS2( movzx edi, ch )\ + AS2( xor ebx, [ebp + esi*4 + 7*256] )\ + AS2( shr ecx, 16 )\ + AS2( xor ebx, [ebp + edi*4 + 5*256] )\ + AS2( movzx esi, cl )\ + AS2( movzx edi, ch )\ + AS2( xor ebx, [ebp + esi*4 + 3*256] )\ + AS2( add edx, 16 )\ + AS2( xor ebx, [ebp + edi*4 + 1*256] ) + + +#ifdef _MSC_VER + __declspec(naked) +#endif +void DES_EDE3::AsmProcess(const byte* in, byte* out, void* box) const +{ +#ifdef __GNUC__ + #define AS1(x) asm(#x); + #define AS2(x, y) asm(#x ", " #y); + + asm(".intel_syntax noprefix"); + + #define PROLOG() \ + AS2( movd mm3, edi ) \ + AS2( movd mm4, ebx ) \ + AS2( movd mm5, esi ) \ + AS2( movd mm6, ebp ) \ + AS2( mov edx, DWORD PTR [ebp + 8] ) \ + AS2( mov esi, DWORD PTR [ebp + 12] ) \ + AS2( mov ebp, DWORD PTR [ebp + 20] ) + + // ebp restored at end + #define EPILOG() \ + AS2( movd edi, mm3 ) \ + AS2( movd ebx, mm4 ) \ + AS2( movd esi, mm5 ) \ + 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 esi, DWORD PTR [ebp + 8] ) \ + AS2( mov edx, ecx ) \ + AS2( mov ebp, DWORD PTR [ebp + 16] ) + + // ebp restored at end + #define EPILOG() \ + AS2( movd edi, mm3 ) \ + AS2( movd ebx, mm4 ) \ + AS2( movd esi, mm5 ) \ + AS2( mov esp, ebp ) \ + AS1( pop ebp ) \ + AS1( emms ) \ + AS1( ret 12 ) + +#endif + + + PROLOG() + + AS2( movd mm2, edx ) + + #ifdef OLD_GCC_OFFSET + AS2( add edx, 60 ) // des1 = des1 key + #else + AS2( add edx, 56 ) // des1 = des1 key + #endif + + AS2( mov eax, DWORD PTR [esi] ) + AS2( mov ebx, DWORD PTR [esi + 4] ) + AS1( bswap eax ) // left + AS1( bswap ebx ) // right + + AsmIPERM() + + DesRound() // 1 + DesRound() // 2 + DesRound() // 3 + DesRound() // 4 + DesRound() // 5 + DesRound() // 6 + DesRound() // 7 + DesRound() // 8 + + // swap left and right + AS2( xchg eax, ebx ) + + DesRound() // 1 + DesRound() // 2 + DesRound() // 3 + DesRound() // 4 + DesRound() // 5 + DesRound() // 6 + DesRound() // 7 + DesRound() // 8 + + // swap left and right + AS2( xchg eax, ebx ) + + DesRound() // 1 + DesRound() // 2 + DesRound() // 3 + DesRound() // 4 + DesRound() // 5 + DesRound() // 6 + DesRound() // 7 + DesRound() // 8 + + AsmFPERM() + + //end + AS2( movd ebp, mm6 ) + + // swap and write out + AS1( bswap ebx ) + AS1( bswap eax ) + +#ifdef __GNUC__ + AS2( mov esi, DWORD PTR [ebp + 16] ) // outBlock +#else + AS2( mov esi, DWORD PTR [ebp + 12] ) // outBlock +#endif + + AS2( mov DWORD PTR [esi], ebx ) // right first + AS2( mov DWORD PTR [esi + 4], eax ) + + + EPILOG() +} + + + +#endif // defined(DO_DES_ASM) + + +} // namespace diff --git a/externals/mysql/extlib/yassl/taocrypt/src/dh.cpp b/externals/mysql/extlib/yassl/taocrypt/src/dh.cpp new file mode 100644 index 00000000000..d6a61cfb1f3 --- /dev/null +++ b/externals/mysql/extlib/yassl/taocrypt/src/dh.cpp @@ -0,0 +1,103 @@ +/* + 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.cpp implements Diffie-Hellman support +*/ + +#include "runtime.hpp" +#include "dh.hpp" +#include "asn.hpp" +#include <math.h> + +namespace TaoCrypt { + + +namespace { // locals + +unsigned int DiscreteLogWorkFactor(unsigned int n) +{ + // assuming discrete log takes about the same time as factoring + if (n<5) + return 0; + else + return (unsigned int)(2.4 * pow((double)n, 1.0/3.0) * + pow(log(double(n)), 2.0/3.0) - 5); +} + +} // namespace locals + + +// Generate a DH Key Pair +void DH::GenerateKeyPair(RandomNumberGenerator& rng, byte* priv, byte* pub) +{ + GeneratePrivate(rng, priv); + GeneratePublic(priv, pub); +} + + +// Generate private value +void DH::GeneratePrivate(RandomNumberGenerator& rng, byte* priv) +{ + Integer x(rng, Integer::One(), min(p_ - 1, + Integer::Power2(2*DiscreteLogWorkFactor(p_.BitCount())) ) ); + x.Encode(priv, p_.ByteCount()); +} + + +// Generate public value +void DH::GeneratePublic(const byte* priv, byte* pub) +{ + const word32 bc(p_.ByteCount()); + Integer x(priv, bc); + Integer y(a_exp_b_mod_c(g_, x, p_)); + y.Encode(pub, bc); +} + + +// Generate Agreement +void DH::Agree(byte* agree, const byte* priv, const byte* otherPub, word32 + otherSz) +{ + const word32 bc(p_.ByteCount()); + Integer x(priv, bc); + Integer y; + if (otherSz) + y.Decode(otherPub, otherSz); + else + y.Decode(otherPub, bc); + + Integer z(a_exp_b_mod_c(y, x, p_)); + z.Encode(agree, bc); +} + + +DH::DH(Source& source) +{ + Initialize(source); +} + + +void DH::Initialize(Source& source) +{ + DH_Decoder decoder(source); + decoder.Decode(*this); +} + + +} // namespace diff --git a/externals/mysql/extlib/yassl/taocrypt/src/dsa.cpp b/externals/mysql/extlib/yassl/taocrypt/src/dsa.cpp new file mode 100644 index 00000000000..79ee91e76f9 --- /dev/null +++ b/externals/mysql/extlib/yassl/taocrypt/src/dsa.cpp @@ -0,0 +1,271 @@ +/* + 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. +*/ + + +#include "runtime.hpp" +#include "dsa.hpp" +#include "sha.hpp" +#include "asn.hpp" +#include "modarith.hpp" + + +namespace TaoCrypt { + + +void DSA_PublicKey::Swap(DSA_PublicKey& other) +{ + p_.Swap(other.p_); + q_.Swap(other.q_); + g_.Swap(other.g_); + y_.Swap(other.y_); +} + + +DSA_PublicKey::DSA_PublicKey(const DSA_PublicKey& other) + : p_(other.p_), q_(other.q_), g_(other.g_), y_(other.y_) +{} + + +DSA_PublicKey& DSA_PublicKey::operator=(const DSA_PublicKey& that) +{ + DSA_PublicKey tmp(that); + Swap(tmp); + return *this; +} + + +DSA_PublicKey::DSA_PublicKey(Source& source) +{ + Initialize(source); +} + + +void DSA_PublicKey::Initialize(Source& source) +{ + DSA_Public_Decoder decoder(source); + decoder.Decode(*this); +} + + +void DSA_PublicKey::Initialize(const Integer& p, const Integer& q, + const Integer& g, const Integer& y) +{ + p_ = p; + q_ = q; + g_ = g; + y_ = y; +} + + +const Integer& DSA_PublicKey::GetModulus() const +{ + return p_; +} + +const Integer& DSA_PublicKey::GetSubGroupOrder() const +{ + return q_; +} + + +const Integer& DSA_PublicKey::GetSubGroupGenerator() const +{ + return g_; +} + + +const Integer& DSA_PublicKey::GetPublicPart() const +{ + return y_; +} + + +void DSA_PublicKey::SetModulus(const Integer& p) +{ + p_ = p; +} + + +void DSA_PublicKey::SetSubGroupOrder(const Integer& q) +{ + q_ = q; +} + + +void DSA_PublicKey::SetSubGroupGenerator(const Integer& g) +{ + g_ = g; +} + + +void DSA_PublicKey::SetPublicPart(const Integer& y) +{ + y_ = y; +} + + +word32 DSA_PublicKey::SignatureLength() const +{ + return GetSubGroupOrder().ByteCount() * 2; // r and s +} + + + +DSA_PrivateKey::DSA_PrivateKey(Source& source) +{ + Initialize(source); +} + + +void DSA_PrivateKey::Initialize(Source& source) +{ + DSA_Private_Decoder decoder(source); + decoder.Decode(*this); +} + + +void DSA_PrivateKey::Initialize(const Integer& p, const Integer& q, + const Integer& g, const Integer& y, + const Integer& x) +{ + DSA_PublicKey::Initialize(p, q, g, y); + x_ = x; +} + + +const Integer& DSA_PrivateKey::GetPrivatePart() const +{ + return x_; +} + + +void DSA_PrivateKey::SetPrivatePart(const Integer& x) +{ + x_ = x; +} + + +DSA_Signer::DSA_Signer(const DSA_PrivateKey& key) + : key_(key) +{} + + +word32 DSA_Signer::Sign(const byte* sha_digest, byte* sig, + RandomNumberGenerator& rng) +{ + const Integer& p = key_.GetModulus(); + const Integer& q = key_.GetSubGroupOrder(); + const Integer& g = key_.GetSubGroupGenerator(); + const Integer& x = key_.GetPrivatePart(); + + Integer k(rng, 1, q - 1); + + r_ = a_exp_b_mod_c(g, k, p); + r_ %= q; + + Integer H(sha_digest, SHA::DIGEST_SIZE); // sha Hash(m) + + Integer kInv = k.InverseMod(q); + s_ = (kInv * (H + x*r_)) % q; + + assert(!!r_ && !!s_); + + int rSz = r_.ByteCount(); + + if (rSz == 19) { + sig[0] = 0; + sig++; + } + + r_.Encode(sig, rSz); + + int sSz = s_.ByteCount(); + + if (sSz == 19) { + sig[rSz] = 0; + sig++; + } + + s_.Encode(sig + rSz, sSz); + + return 40; +} + + +DSA_Verifier::DSA_Verifier(const DSA_PublicKey& key) + : key_(key) +{} + + +bool DSA_Verifier::Verify(const byte* sha_digest, const byte* sig) +{ + const Integer& p = key_.GetModulus(); + const Integer& q = key_.GetSubGroupOrder(); + const Integer& g = key_.GetSubGroupGenerator(); + const Integer& y = key_.GetPublicPart(); + + int sz = q.ByteCount(); + + r_.Decode(sig, sz); + s_.Decode(sig + sz, sz); + + if (r_ >= q || r_ < 1 || s_ >= q || s_ < 1) + return false; + + Integer H(sha_digest, SHA::DIGEST_SIZE); // sha Hash(m) + + Integer w = s_.InverseMod(q); + Integer u1 = (H * w) % q; + Integer u2 = (r_ * w) % q; + + // verify r == ((g^u1 * y^u2) mod p) mod q + ModularArithmetic ma(p); + Integer v = ma.CascadeExponentiate(g, u1, y, u2); + v %= q; + + return r_ == v; +} + + + + +const Integer& DSA_Signer::GetR() const +{ + return r_; +} + + +const Integer& DSA_Signer::GetS() const +{ + return s_; +} + + +const Integer& DSA_Verifier::GetR() const +{ + return r_; +} + + +const Integer& DSA_Verifier::GetS() const +{ + return s_; +} + + +} // namespace diff --git a/externals/mysql/extlib/yassl/taocrypt/src/file.cpp b/externals/mysql/extlib/yassl/taocrypt/src/file.cpp new file mode 100644 index 00000000000..0498038a04b --- /dev/null +++ b/externals/mysql/extlib/yassl/taocrypt/src/file.cpp @@ -0,0 +1,115 @@ +/* + 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.cpp implements File Sources and Sinks +*/ + +#include "runtime.hpp" +#include "file.hpp" + + +namespace TaoCrypt { + + +FileSource::FileSource(const char* fname, Source& source) +{ + file_ = fopen(fname, "rb"); + if (file_) get(source); +} + + +FileSource::~FileSource() +{ + if (file_) + fclose(file_); +} + + + +// return size of source from beginning or current position +word32 FileSource::size(bool use_current) +{ + long current = ftell(file_); + long begin = current; + + if (!use_current) { + fseek(file_, 0, SEEK_SET); + begin = ftell(file_); + } + + fseek(file_, 0, SEEK_END); + long end = ftell(file_); + + fseek(file_, current, SEEK_SET); + + return end - begin; +} + + +word32 FileSource::size_left() +{ + return size(true); +} + + +// fill file source from source +word32 FileSource::get(Source& source) +{ + word32 sz(size()); + if (source.size() < sz) + source.grow(sz); + + size_t bytes = fread(source.buffer_.get_buffer(), 1, sz, file_); + + if (bytes == 1) + return sz; + else + return 0; +} + + +FileSink::FileSink(const char* fname, Source& source) +{ + file_ = fopen(fname, "wb"); + if (file_) put(source); +} + + +FileSink::~FileSink() +{ + if (file_) + fclose(file_); +} + + +// fill source from file sink +void FileSink::put(Source& source) +{ + fwrite(source.get_buffer(), 1, source.size(), file_); +} + + +// swap with other and reset to beginning +void Source::reset(ByteBlock& otherBlock) +{ + buffer_.Swap(otherBlock); + current_ = 0; +} + + +} // namespace diff --git a/externals/mysql/extlib/yassl/taocrypt/src/hash.cpp b/externals/mysql/extlib/yassl/taocrypt/src/hash.cpp new file mode 100644 index 00000000000..c51dc42a909 --- /dev/null +++ b/externals/mysql/extlib/yassl/taocrypt/src/hash.cpp @@ -0,0 +1,196 @@ +/* + 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.cpp implements a base for digest types +*/ + +#include "runtime.hpp" +#include <string.h> +#include <assert.h> + +#include "hash.hpp" + + +namespace TaoCrypt { + + +HASHwithTransform::HASHwithTransform(word32 digSz, word32 buffSz) +{ + assert(digSz <= MaxDigestSz); + assert(buffSz <= MaxBufferSz); +} + + +void HASHwithTransform::AddLength(word32 len) +{ + HashLengthType tmp = loLen_; + if ( (loLen_ += len) < tmp) + hiLen_++; // carry low to high + hiLen_ += SafeRightShift<8*sizeof(HashLengthType)>(len); +} + + +// Update digest with data of size len, do in blocks +void HASHwithTransform::Update(const byte* data, word32 len) +{ + // do block size increments + word32 blockSz = getBlockSize(); + byte* local = reinterpret_cast<byte*>(buffer_); + + while (len) { + word32 add = min(len, blockSz - buffLen_); + memcpy(&local[buffLen_], data, add); + + buffLen_ += add; + data += add; + len -= add; + + if (buffLen_ == blockSz) { + ByteReverseIf(local, local, blockSz, getByteOrder()); + Transform(); + AddLength(blockSz); + buffLen_ = 0; + } + } +} + + +// Final process, place digest in hash +void HASHwithTransform::Final(byte* hash) +{ + word32 blockSz = getBlockSize(); + word32 digestSz = getDigestSize(); + word32 padSz = getPadSize(); + ByteOrder order = getByteOrder(); + + AddLength(buffLen_); // before adding pads + HashLengthType preLoLen = GetBitCountLo(); + HashLengthType preHiLen = GetBitCountHi(); + byte* local = reinterpret_cast<byte*>(buffer_); + + local[buffLen_++] = 0x80; // add 1 + + // pad with zeros + if (buffLen_ > padSz) { + memset(&local[buffLen_], 0, blockSz - buffLen_); + buffLen_ += blockSz - buffLen_; + + ByteReverseIf(local, local, blockSz, order); + Transform(); + buffLen_ = 0; + } + memset(&local[buffLen_], 0, padSz - buffLen_); + + ByteReverseIf(local, local, blockSz, order); + + memcpy(&local[padSz], order ? &preHiLen : &preLoLen, sizeof(preLoLen)); + memcpy(&local[padSz+4], order ? &preLoLen : &preHiLen, sizeof(preLoLen)); + + Transform(); + ByteReverseIf(digest_, digest_, digestSz, order); + memcpy(hash, digest_, digestSz); + + Init(); // reset state +} + + +#ifdef WORD64_AVAILABLE + +HASH64withTransform::HASH64withTransform(word32 digSz, word32 buffSz) +{ + assert(digSz <= MaxDigestSz); + assert(buffSz <= MaxBufferSz); +} + + +void HASH64withTransform::AddLength(word32 len) +{ + HashLengthType tmp = loLen_; + if ( (loLen_ += len) < tmp) + hiLen_++; // carry low to high + hiLen_ += SafeRightShift<8*sizeof(HashLengthType)>(len); +} + + +// Update digest with data of size len, do in blocks +void HASH64withTransform::Update(const byte* data, word32 len) +{ + // do block size increments + word32 blockSz = getBlockSize(); + byte* local = reinterpret_cast<byte*>(buffer_); + + while (len) { + word32 add = min(len, blockSz - buffLen_); + memcpy(&local[buffLen_], data, add); + + buffLen_ += add; + data += add; + len -= add; + + if (buffLen_ == blockSz) { + ByteReverseIf(buffer_, buffer_, blockSz, getByteOrder()); + Transform(); + AddLength(blockSz); + buffLen_ = 0; + } + } +} + + +// Final process, place digest in hash +void HASH64withTransform::Final(byte* hash) +{ + word32 blockSz = getBlockSize(); + word32 digestSz = getDigestSize(); + word32 padSz = getPadSize(); + ByteOrder order = getByteOrder(); + + AddLength(buffLen_); // before adding pads + HashLengthType preLoLen = GetBitCountLo(); + HashLengthType preHiLen = GetBitCountHi(); + byte* local = reinterpret_cast<byte*>(buffer_); + + local[buffLen_++] = 0x80; // add 1 + + // pad with zeros + if (buffLen_ > padSz) { + memset(&local[buffLen_], 0, blockSz - buffLen_); + buffLen_ += blockSz - buffLen_; + + ByteReverseIf(buffer_, buffer_, blockSz, order); + Transform(); + buffLen_ = 0; + } + memset(&local[buffLen_], 0, padSz - buffLen_); + + ByteReverseIf(buffer_, buffer_, padSz, order); + + buffer_[blockSz / sizeof(word64) - 2] = order ? preHiLen : preLoLen; + buffer_[blockSz / sizeof(word64) - 1] = order ? preLoLen : preHiLen; + + Transform(); + ByteReverseIf(digest_, digest_, digestSz, order); + memcpy(hash, digest_, digestSz); + + Init(); // reset state +} + +#endif // WORD64_AVAILABLE + + +} // namespace diff --git a/externals/mysql/extlib/yassl/taocrypt/src/integer.cpp b/externals/mysql/extlib/yassl/taocrypt/src/integer.cpp new file mode 100644 index 00000000000..85733b88aa9 --- /dev/null +++ b/externals/mysql/extlib/yassl/taocrypt/src/integer.cpp @@ -0,0 +1,3964 @@ +/* + 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.cpp from CryptoPP */ + +#include "runtime.hpp" +#include "integer.hpp" +#include "modarith.hpp" +#include "asn.hpp" + + + +#ifdef __DECCXX + #include <c_asm.h> // for asm overflow assembly +#endif + +#if defined(_M_X64) || defined(_M_IA64) + #include <intrin.h> +#pragma intrinsic(_umul128) +#endif + + +#ifdef __GNUC__ + #include <signal.h> + #include <setjmp.h> +#endif + + +#ifdef SSE2_INTRINSICS_AVAILABLE + #ifdef __GNUC__ + #include <xmmintrin.h> + #ifdef TAOCRYPT_MEMALIGN_AVAILABLE + #include <malloc.h> + #else + #include <stdlib.h> + #endif + #else + #include <emmintrin.h> + #endif +#elif defined(_MSC_VER) && defined(_M_IX86) + #pragma message("You do not seem to have the Visual C++ Processor Pack ") + #pragma message("installed, so use of SSE2 intrinsics will be disabled.") +#elif defined(__GNUC__) && defined(__i386__) +/* #warning You do not have GCC 3.3 or later, or did not specify the -msse2 \ + compiler option. Use of SSE2 intrinsics will be disabled. +*/ +#endif + + +namespace TaoCrypt { + + +#ifdef SSE2_INTRINSICS_AVAILABLE + +template <class T> +CPP_TYPENAME AlignedAllocator<T>::pointer AlignedAllocator<T>::allocate( + size_type n, const void *) +{ + CheckSize(n); + if (n == 0) + return 0; + if (n >= 4) + { + void* p; + #ifdef TAOCRYPT_MM_MALLOC_AVAILABLE + p = _mm_malloc(sizeof(T)*n, 16); + #elif defined(TAOCRYPT_MEMALIGN_AVAILABLE) + p = memalign(16, sizeof(T)*n); + #elif defined(TAOCRYPT_MALLOC_ALIGNMENT_IS_16) + p = malloc(sizeof(T)*n); + #else + p = (byte *)malloc(sizeof(T)*n + 8); + // assume malloc alignment is at least 8 + #endif + + #ifdef TAOCRYPT_NO_ALIGNED_ALLOC + assert(m_pBlock == 0); + m_pBlock = p; + if (!IsAlignedOn(p, 16)) + { + assert(IsAlignedOn(p, 8)); + p = (byte *)p + 8; + } + #endif + + assert(IsAlignedOn(p, 16)); + return (T*)p; + } + return NEW_TC T[n]; +} + + +template <class T> +void AlignedAllocator<T>::deallocate(void* p, size_type n) +{ + memset(p, 0, n*sizeof(T)); + if (n >= 4) + { + #ifdef TAOCRYPT_MM_MALLOC_AVAILABLE + _mm_free(p); + #elif defined(TAOCRYPT_NO_ALIGNED_ALLOC) + assert(m_pBlock == p || (byte*)m_pBlock+8 == p); + free(m_pBlock); + m_pBlock = 0; + #else + free(p); + #endif + } + else + tcArrayDelete((T *)p); +} + +#endif // SSE2 + + +// ******** start of integer needs + +// start 5.2.1 adds DWord and Word ******** + +// ******************************************************** + +class DWord { +public: +DWord() {} + +#ifdef TAOCRYPT_NATIVE_DWORD_AVAILABLE + explicit DWord(word low) + { + whole_ = low; + } +#else + explicit DWord(word low) + { + halfs_.low = low; + halfs_.high = 0; + } +#endif + + DWord(word low, word high) + { + halfs_.low = low; + halfs_.high = high; + } + + static DWord Multiply(word a, word b) + { + DWord r; + + #ifdef TAOCRYPT_NATIVE_DWORD_AVAILABLE + r.whole_ = (dword)a * b; + + #elif defined(_M_X64) || defined(_M_IA64) + r.halfs_.low = _umul128(a, b, &r.halfs_.high); + + #elif defined(__alpha__) + r.halfs_.low = a*b; + #ifdef __GNUC__ + __asm__("umulh %1,%2,%0" : "=r" (r.halfs_.high) + : "r" (a), "r" (b)); + #elif defined(__DECCXX) + r.halfs_.high = asm("umulh %a0, %a1, %v0", a, b); + #else + #error unknown alpha compiler + #endif + + #elif defined(__ia64__) + r.halfs_.low = a*b; + __asm__("xmpy.hu %0=%1,%2" : "=f" (r.halfs_.high) + : "f" (a), "f" (b)); + + #elif defined(_ARCH_PPC64) + r.halfs_.low = a*b; + __asm__("mulhdu %0,%1,%2" : "=r" (r.halfs_.high) + : "r" (a), "r" (b) : "cc"); + + #elif defined(__x86_64__) + __asm__("mulq %3" : "=d" (r.halfs_.high), "=a" (r.halfs_.low) : + "a" (a), "rm" (b) : "cc"); + + #elif defined(__mips64) + __asm__("dmultu %2,%3" : "=h" (r.halfs_.high), "=l" (r.halfs_.low) + : "r" (a), "r" (b)); + + #elif defined(_M_IX86) + // for testing + word64 t = (word64)a * b; + r.halfs_.high = ((word32 *)(&t))[1]; + r.halfs_.low = (word32)t; + #else + #error can not implement DWord + #endif + + return r; + } + + static DWord MultiplyAndAdd(word a, word b, word c) + { + DWord r = Multiply(a, b); + return r += c; + } + + DWord & operator+=(word a) + { + #ifdef TAOCRYPT_NATIVE_DWORD_AVAILABLE + whole_ = whole_ + a; + #else + halfs_.low += a; + halfs_.high += (halfs_.low < a); + #endif + return *this; + } + + DWord operator+(word a) + { + DWord r; + #ifdef TAOCRYPT_NATIVE_DWORD_AVAILABLE + r.whole_ = whole_ + a; + #else + r.halfs_.low = halfs_.low + a; + r.halfs_.high = halfs_.high + (r.halfs_.low < a); + #endif + return r; + } + + DWord operator-(DWord a) + { + DWord r; + #ifdef TAOCRYPT_NATIVE_DWORD_AVAILABLE + r.whole_ = whole_ - a.whole_; + #else + r.halfs_.low = halfs_.low - a.halfs_.low; + r.halfs_.high = halfs_.high - a.halfs_.high - + (r.halfs_.low > halfs_.low); + #endif + return r; + } + + DWord operator-(word a) + { + DWord r; + #ifdef TAOCRYPT_NATIVE_DWORD_AVAILABLE + r.whole_ = whole_ - a; + #else + r.halfs_.low = halfs_.low - a; + r.halfs_.high = halfs_.high - (r.halfs_.low > halfs_.low); + #endif + return r; + } + + // returns quotient, which must fit in a word + word operator/(word divisor); + + word operator%(word a); + + bool operator!() const + { + #ifdef TAOCRYPT_NATIVE_DWORD_AVAILABLE + return !whole_; + #else + return !halfs_.high && !halfs_.low; + #endif + } + + word GetLowHalf() const {return halfs_.low;} + word GetHighHalf() const {return halfs_.high;} + word GetHighHalfAsBorrow() const {return 0-halfs_.high;} + +private: + union + { + #ifdef TAOCRYPT_NATIVE_DWORD_AVAILABLE + dword whole_; + #endif + struct + { + #ifdef LITTLE_ENDIAN_ORDER + word low; + word high; + #else + word high; + word low; + #endif + } halfs_; + }; +}; + + +class Word { +public: + Word() {} + + Word(word value) + { + whole_ = value; + } + + Word(hword low, hword high) + { + whole_ = low | (word(high) << (WORD_BITS/2)); + } + + static Word Multiply(hword a, hword b) + { + Word r; + r.whole_ = (word)a * b; + return r; + } + + Word operator-(Word a) + { + Word r; + r.whole_ = whole_ - a.whole_; + return r; + } + + Word operator-(hword a) + { + Word r; + r.whole_ = whole_ - a; + return r; + } + + // returns quotient, which must fit in a word + hword operator/(hword divisor) + { + return hword(whole_ / divisor); + } + + bool operator!() const + { + return !whole_; + } + + word GetWhole() const {return whole_;} + hword GetLowHalf() const {return hword(whole_);} + hword GetHighHalf() const {return hword(whole_>>(WORD_BITS/2));} + hword GetHighHalfAsBorrow() const {return 0-hword(whole_>>(WORD_BITS/2));} + +private: + word whole_; +}; + + +// dummy is VC60 compiler bug workaround +// do a 3 word by 2 word divide, returns quotient and leaves remainder in A +template <class S, class D> +S DivideThreeWordsByTwo(S* A, S B0, S B1, D* dummy_VC6_WorkAround = 0) +{ + // assert {A[2],A[1]} < {B1,B0}, so quotient can fit in a S + assert(A[2] < B1 || (A[2]==B1 && A[1] < B0)); + + // estimate the quotient: do a 2 S by 1 S divide + S Q; + if (S(B1+1) == 0) + Q = A[2]; + else + Q = D(A[1], A[2]) / S(B1+1); + + // now subtract Q*B from A + D p = D::Multiply(B0, Q); + D u = (D) A[0] - p.GetLowHalf(); + A[0] = u.GetLowHalf(); + u = (D) A[1] - p.GetHighHalf() - u.GetHighHalfAsBorrow() - + D::Multiply(B1, Q); + A[1] = u.GetLowHalf(); + A[2] += u.GetHighHalf(); + + // Q <= actual quotient, so fix it + while (A[2] || A[1] > B1 || (A[1]==B1 && A[0]>=B0)) + { + u = (D) A[0] - B0; + A[0] = u.GetLowHalf(); + u = (D) A[1] - B1 - u.GetHighHalfAsBorrow(); + A[1] = u.GetLowHalf(); + A[2] += u.GetHighHalf(); + Q++; + assert(Q); // shouldn't overflow + } + + return Q; +} + + +// do a 4 word by 2 word divide, returns 2 word quotient in Q0 and Q1 +template <class S, class D> +inline D DivideFourWordsByTwo(S *T, const D &Al, const D &Ah, const D &B) +{ + if (!B) // if divisor is 0, we assume divisor==2**(2*WORD_BITS) + return D(Ah.GetLowHalf(), Ah.GetHighHalf()); + else + { + S Q[2]; + T[0] = Al.GetLowHalf(); + T[1] = Al.GetHighHalf(); + T[2] = Ah.GetLowHalf(); + T[3] = Ah.GetHighHalf(); + Q[1] = DivideThreeWordsByTwo<S, D>(T+1, B.GetLowHalf(), + B.GetHighHalf()); + Q[0] = DivideThreeWordsByTwo<S, D>(T, B.GetLowHalf(), B.GetHighHalf()); + return D(Q[0], Q[1]); + } +} + + +// returns quotient, which must fit in a word +inline word DWord::operator/(word a) +{ + #ifdef TAOCRYPT_NATIVE_DWORD_AVAILABLE + return word(whole_ / a); + #else + hword r[4]; + return DivideFourWordsByTwo<hword, Word>(r, halfs_.low, + halfs_.high, a).GetWhole(); + #endif +} + +inline word DWord::operator%(word a) +{ + #ifdef TAOCRYPT_NATIVE_DWORD_AVAILABLE + return word(whole_ % a); + #else + if (a < (word(1) << (WORD_BITS/2))) + { + hword h = hword(a); + word r = halfs_.high % h; + r = ((halfs_.low >> (WORD_BITS/2)) + (r << (WORD_BITS/2))) % h; + return hword((hword(halfs_.low) + (r << (WORD_BITS/2))) % h); + } + else + { + hword r[4]; + DivideFourWordsByTwo<hword, Word>(r, halfs_.low, halfs_.high, a); + return Word(r[0], r[1]).GetWhole(); + } + #endif +} + + + +// end 5.2.1 DWord and Word adds + + + + + +static const unsigned int RoundupSizeTable[] = {2, 2, 2, 4, 4, 8, 8, 8, 8}; + +static inline unsigned int RoundupSize(unsigned int n) +{ + if (n<=8) + return RoundupSizeTable[n]; + else if (n<=16) + return 16; + else if (n<=32) + return 32; + else if (n<=64) + return 64; + else return 1U << BitPrecision(n-1); +} + + +static int Compare(const word *A, const word *B, unsigned int N) +{ + while (N--) + if (A[N] > B[N]) + return 1; + else if (A[N] < B[N]) + return -1; + + return 0; +} + +static word Increment(word *A, unsigned int N, word B=1) +{ + assert(N); + word t = A[0]; + A[0] = t+B; + if (A[0] >= t) + return 0; + for (unsigned i=1; i<N; i++) + if (++A[i]) + return 0; + return 1; +} + +static word Decrement(word *A, unsigned int N, word B=1) +{ + assert(N); + word t = A[0]; + A[0] = t-B; + if (A[0] <= t) + return 0; + for (unsigned i=1; i<N; i++) + if (A[i]--) + return 0; + return 1; +} + +static void TwosComplement(word *A, unsigned int N) +{ + Decrement(A, N); + for (unsigned i=0; i<N; i++) + A[i] = ~A[i]; +} + + +static word LinearMultiply(word *C, const word *A, word B, unsigned int N) +{ + word carry=0; + for(unsigned i=0; i<N; i++) + { + DWord p = DWord::MultiplyAndAdd(A[i], B, carry); + C[i] = p.GetLowHalf(); + carry = p.GetHighHalf(); + } + return carry; +} + + +static word AtomicInverseModPower2(word A) +{ + assert(A%2==1); + + word R=A%8; + + for (unsigned i=3; i<WORD_BITS; i*=2) + R = R*(2-R*A); + + assert(word(R*A)==1); + return R; +} + + +// ******************************************************** + +class Portable +{ +public: + static word TAOCRYPT_CDECL Add(word *C, const word *A, const word *B, + unsigned int N); + static word TAOCRYPT_CDECL Subtract(word *C, const word *A, const word*B, + unsigned int N); + static void TAOCRYPT_CDECL Multiply2(word *C, const word *A, const word *B); + static word TAOCRYPT_CDECL Multiply2Add(word *C, + const word *A, const word *B); + static void TAOCRYPT_CDECL Multiply4(word *C, const word *A, const word *B); + static void TAOCRYPT_CDECL Multiply8(word *C, const word *A, const word *B); + static unsigned int TAOCRYPT_CDECL MultiplyRecursionLimit() {return 8;} + + static void TAOCRYPT_CDECL Multiply2Bottom(word *C, const word *A, + const word *B); + static void TAOCRYPT_CDECL Multiply4Bottom(word *C, const word *A, + const word *B); + static void TAOCRYPT_CDECL Multiply8Bottom(word *C, const word *A, + const word *B); + static unsigned int TAOCRYPT_CDECL MultiplyBottomRecursionLimit(){return 8;} + + static void TAOCRYPT_CDECL Square2(word *R, const word *A); + static void TAOCRYPT_CDECL Square4(word *R, const word *A); + static void TAOCRYPT_CDECL Square8(word *R, const word *A) {assert(false);} + static unsigned int TAOCRYPT_CDECL SquareRecursionLimit() {return 4;} +}; + +word Portable::Add(word *C, const word *A, const word *B, unsigned int N) +{ + assert (N%2 == 0); + + DWord u(0, 0); + for (unsigned int i = 0; i < N; i+=2) + { + u = DWord(A[i]) + B[i] + u.GetHighHalf(); + C[i] = u.GetLowHalf(); + u = DWord(A[i+1]) + B[i+1] + u.GetHighHalf(); + C[i+1] = u.GetLowHalf(); + } + return u.GetHighHalf(); +} + +word Portable::Subtract(word *C, const word *A, const word *B, unsigned int N) +{ + assert (N%2 == 0); + + DWord u(0, 0); + for (unsigned int i = 0; i < N; i+=2) + { + u = (DWord) A[i] - B[i] - u.GetHighHalfAsBorrow(); + C[i] = u.GetLowHalf(); + u = (DWord) A[i+1] - B[i+1] - u.GetHighHalfAsBorrow(); + C[i+1] = u.GetLowHalf(); + } + return 0-u.GetHighHalf(); +} + +void Portable::Multiply2(word *C, const word *A, const word *B) +{ +/* + word s; + dword d; + + if (A1 >= A0) + if (B0 >= B1) + { + s = 0; + d = (dword)(A1-A0)*(B0-B1); + } + else + { + s = (A1-A0); + d = (dword)s*(word)(B0-B1); + } + else + if (B0 > B1) + { + s = (B0-B1); + d = (word)(A1-A0)*(dword)s; + } + else + { + s = 0; + d = (dword)(A0-A1)*(B1-B0); + } +*/ + // this segment is the branchless equivalent of above + word D[4] = {A[1]-A[0], A[0]-A[1], B[0]-B[1], B[1]-B[0]}; + unsigned int ai = A[1] < A[0]; + unsigned int bi = B[0] < B[1]; + unsigned int di = ai & bi; + DWord d = DWord::Multiply(D[di], D[di+2]); + D[1] = D[3] = 0; + unsigned int si = ai + !bi; + word s = D[si]; + + DWord A0B0 = DWord::Multiply(A[0], B[0]); + C[0] = A0B0.GetLowHalf(); + + DWord A1B1 = DWord::Multiply(A[1], B[1]); + DWord t = (DWord) A0B0.GetHighHalf() + A0B0.GetLowHalf() + d.GetLowHalf() + + A1B1.GetLowHalf(); + C[1] = t.GetLowHalf(); + + t = A1B1 + t.GetHighHalf() + A0B0.GetHighHalf() + d.GetHighHalf() + + A1B1.GetHighHalf() - s; + C[2] = t.GetLowHalf(); + C[3] = t.GetHighHalf(); +} + +void Portable::Multiply2Bottom(word *C, const word *A, const word *B) +{ + DWord t = DWord::Multiply(A[0], B[0]); + C[0] = t.GetLowHalf(); + C[1] = t.GetHighHalf() + A[0]*B[1] + A[1]*B[0]; +} + +word Portable::Multiply2Add(word *C, const word *A, const word *B) +{ + word D[4] = {A[1]-A[0], A[0]-A[1], B[0]-B[1], B[1]-B[0]}; + unsigned int ai = A[1] < A[0]; + unsigned int bi = B[0] < B[1]; + unsigned int di = ai & bi; + DWord d = DWord::Multiply(D[di], D[di+2]); + D[1] = D[3] = 0; + unsigned int si = ai + !bi; + word s = D[si]; + + DWord A0B0 = DWord::Multiply(A[0], B[0]); + DWord t = A0B0 + C[0]; + C[0] = t.GetLowHalf(); + + DWord A1B1 = DWord::Multiply(A[1], B[1]); + t = (DWord) t.GetHighHalf() + A0B0.GetLowHalf() + d.GetLowHalf() + + A1B1.GetLowHalf() + C[1]; + C[1] = t.GetLowHalf(); + + t = (DWord) t.GetHighHalf() + A1B1.GetLowHalf() + A0B0.GetHighHalf() + + d.GetHighHalf() + A1B1.GetHighHalf() - s + C[2]; + C[2] = t.GetLowHalf(); + + t = (DWord) t.GetHighHalf() + A1B1.GetHighHalf() + C[3]; + C[3] = t.GetLowHalf(); + return t.GetHighHalf(); +} + + +#define MulAcc(x, y) \ + p = DWord::MultiplyAndAdd(A[x], B[y], c); \ + c = p.GetLowHalf(); \ + p = (DWord) d + p.GetHighHalf(); \ + d = p.GetLowHalf(); \ + e += p.GetHighHalf(); + +#define SaveMulAcc(s, x, y) \ + R[s] = c; \ + p = DWord::MultiplyAndAdd(A[x], B[y], d); \ + c = p.GetLowHalf(); \ + p = (DWord) e + p.GetHighHalf(); \ + d = p.GetLowHalf(); \ + e = p.GetHighHalf(); + +#define SquAcc(x, y) \ + q = DWord::Multiply(A[x], A[y]); \ + p = q + c; \ + c = p.GetLowHalf(); \ + p = (DWord) d + p.GetHighHalf(); \ + d = p.GetLowHalf(); \ + e += p.GetHighHalf(); \ + p = q + c; \ + c = p.GetLowHalf(); \ + p = (DWord) d + p.GetHighHalf(); \ + d = p.GetLowHalf(); \ + e += p.GetHighHalf(); + +#define SaveSquAcc(s, x, y) \ + R[s] = c; \ + q = DWord::Multiply(A[x], A[y]); \ + p = q + d; \ + c = p.GetLowHalf(); \ + p = (DWord) e + p.GetHighHalf(); \ + d = p.GetLowHalf(); \ + e = p.GetHighHalf(); \ + p = q + c; \ + c = p.GetLowHalf(); \ + p = (DWord) d + p.GetHighHalf(); \ + d = p.GetLowHalf(); \ + e += p.GetHighHalf(); + + +void Portable::Multiply4(word *R, const word *A, const word *B) +{ + DWord p; + word c, d, e; + + p = DWord::Multiply(A[0], B[0]); + R[0] = p.GetLowHalf(); + c = p.GetHighHalf(); + d = e = 0; + + MulAcc(0, 1); + MulAcc(1, 0); + + SaveMulAcc(1, 2, 0); + MulAcc(1, 1); + MulAcc(0, 2); + + SaveMulAcc(2, 0, 3); + MulAcc(1, 2); + MulAcc(2, 1); + MulAcc(3, 0); + + SaveMulAcc(3, 3, 1); + MulAcc(2, 2); + MulAcc(1, 3); + + SaveMulAcc(4, 2, 3); + MulAcc(3, 2); + + R[5] = c; + p = DWord::MultiplyAndAdd(A[3], B[3], d); + R[6] = p.GetLowHalf(); + R[7] = e + p.GetHighHalf(); +} + +void Portable::Square2(word *R, const word *A) +{ + DWord p, q; + word c, d, e; + + p = DWord::Multiply(A[0], A[0]); + R[0] = p.GetLowHalf(); + c = p.GetHighHalf(); + d = e = 0; + + SquAcc(0, 1); + + R[1] = c; + p = DWord::MultiplyAndAdd(A[1], A[1], d); + R[2] = p.GetLowHalf(); + R[3] = e + p.GetHighHalf(); +} + +void Portable::Square4(word *R, const word *A) +{ +#ifdef _MSC_VER + // VC60 workaround: MSVC 6.0 has an optimization bug that makes + // (dword)A*B where either A or B has been cast to a dword before + // very expensive. Revisit this function when this + // bug is fixed. + Multiply4(R, A, A); +#else + const word *B = A; + DWord p, q; + word c, d, e; + + p = DWord::Multiply(A[0], A[0]); + R[0] = p.GetLowHalf(); + c = p.GetHighHalf(); + d = e = 0; + + SquAcc(0, 1); + + SaveSquAcc(1, 2, 0); + MulAcc(1, 1); + + SaveSquAcc(2, 0, 3); + SquAcc(1, 2); + + SaveSquAcc(3, 3, 1); + MulAcc(2, 2); + + SaveSquAcc(4, 2, 3); + + R[5] = c; + p = DWord::MultiplyAndAdd(A[3], A[3], d); + R[6] = p.GetLowHalf(); + R[7] = e + p.GetHighHalf(); +#endif +} + +void Portable::Multiply8(word *R, const word *A, const word *B) +{ + DWord p; + word c, d, e; + + p = DWord::Multiply(A[0], B[0]); + R[0] = p.GetLowHalf(); + c = p.GetHighHalf(); + d = e = 0; + + MulAcc(0, 1); + MulAcc(1, 0); + + SaveMulAcc(1, 2, 0); + MulAcc(1, 1); + MulAcc(0, 2); + + SaveMulAcc(2, 0, 3); + MulAcc(1, 2); + MulAcc(2, 1); + MulAcc(3, 0); + + SaveMulAcc(3, 0, 4); + MulAcc(1, 3); + MulAcc(2, 2); + MulAcc(3, 1); + MulAcc(4, 0); + + SaveMulAcc(4, 0, 5); + MulAcc(1, 4); + MulAcc(2, 3); + MulAcc(3, 2); + MulAcc(4, 1); + MulAcc(5, 0); + + SaveMulAcc(5, 0, 6); + MulAcc(1, 5); + MulAcc(2, 4); + MulAcc(3, 3); + MulAcc(4, 2); + MulAcc(5, 1); + MulAcc(6, 0); + + SaveMulAcc(6, 0, 7); + MulAcc(1, 6); + MulAcc(2, 5); + MulAcc(3, 4); + MulAcc(4, 3); + MulAcc(5, 2); + MulAcc(6, 1); + MulAcc(7, 0); + + SaveMulAcc(7, 1, 7); + MulAcc(2, 6); + MulAcc(3, 5); + MulAcc(4, 4); + MulAcc(5, 3); + MulAcc(6, 2); + MulAcc(7, 1); + + SaveMulAcc(8, 2, 7); + MulAcc(3, 6); + MulAcc(4, 5); + MulAcc(5, 4); + MulAcc(6, 3); + MulAcc(7, 2); + + SaveMulAcc(9, 3, 7); + MulAcc(4, 6); + MulAcc(5, 5); + MulAcc(6, 4); + MulAcc(7, 3); + + SaveMulAcc(10, 4, 7); + MulAcc(5, 6); + MulAcc(6, 5); + MulAcc(7, 4); + + SaveMulAcc(11, 5, 7); + MulAcc(6, 6); + MulAcc(7, 5); + + SaveMulAcc(12, 6, 7); + MulAcc(7, 6); + + R[13] = c; + p = DWord::MultiplyAndAdd(A[7], B[7], d); + R[14] = p.GetLowHalf(); + R[15] = e + p.GetHighHalf(); +} + +void Portable::Multiply4Bottom(word *R, const word *A, const word *B) +{ + DWord p; + word c, d, e; + + p = DWord::Multiply(A[0], B[0]); + R[0] = p.GetLowHalf(); + c = p.GetHighHalf(); + d = e = 0; + + MulAcc(0, 1); + MulAcc(1, 0); + + SaveMulAcc(1, 2, 0); + MulAcc(1, 1); + MulAcc(0, 2); + + R[2] = c; + R[3] = d + A[0] * B[3] + A[1] * B[2] + A[2] * B[1] + A[3] * B[0]; +} + +void Portable::Multiply8Bottom(word *R, const word *A, const word *B) +{ + DWord p; + word c, d, e; + + p = DWord::Multiply(A[0], B[0]); + R[0] = p.GetLowHalf(); + c = p.GetHighHalf(); + d = e = 0; + + MulAcc(0, 1); + MulAcc(1, 0); + + SaveMulAcc(1, 2, 0); + MulAcc(1, 1); + MulAcc(0, 2); + + SaveMulAcc(2, 0, 3); + MulAcc(1, 2); + MulAcc(2, 1); + MulAcc(3, 0); + + SaveMulAcc(3, 0, 4); + MulAcc(1, 3); + MulAcc(2, 2); + MulAcc(3, 1); + MulAcc(4, 0); + + SaveMulAcc(4, 0, 5); + MulAcc(1, 4); + MulAcc(2, 3); + MulAcc(3, 2); + MulAcc(4, 1); + MulAcc(5, 0); + + SaveMulAcc(5, 0, 6); + MulAcc(1, 5); + MulAcc(2, 4); + MulAcc(3, 3); + MulAcc(4, 2); + MulAcc(5, 1); + MulAcc(6, 0); + + R[6] = c; + R[7] = d + A[0] * B[7] + A[1] * B[6] + A[2] * B[5] + A[3] * B[4] + + A[4] * B[3] + A[5] * B[2] + A[6] * B[1] + A[7] * B[0]; +} + + +#undef MulAcc +#undef SaveMulAcc +#undef SquAcc +#undef SaveSquAcc + +// optimized + +#ifdef TAOCRYPT_X86ASM_AVAILABLE + +// ************** x86 feature detection *************** + + +#ifdef SSE2_INTRINSICS_AVAILABLE + +#ifndef _MSC_VER + static jmp_buf s_env; + static void SigIllHandler(int) + { + longjmp(s_env, 1); + } +#endif + +static bool HasSSE2() +{ + if (!IsPentium()) + return false; + + word32 cpuid[4]; + CpuId(1, cpuid); + if ((cpuid[3] & (1 << 26)) == 0) + return false; + +#ifdef _MSC_VER + __try + { + __asm xorpd xmm0, xmm0 // executing SSE2 instruction + } + __except (1) + { + return false; + } + return true; +#else + typedef void (*SigHandler)(int); + + SigHandler oldHandler = signal(SIGILL, SigIllHandler); + if (oldHandler == SIG_ERR) + return false; + + bool result = true; + if (setjmp(s_env)) + result = false; + else + __asm __volatile ("xorpd %xmm0, %xmm0"); + + signal(SIGILL, oldHandler); + return result; +#endif +} +#endif // SSE2_INTRINSICS_AVAILABLE + + +static bool IsP4() +{ + if (!IsPentium()) + return false; + + word32 cpuid[4]; + + CpuId(1, cpuid); + return ((cpuid[0] >> 8) & 0xf) == 0xf; +} + +// ************** Pentium/P4 optimizations *************** + +class PentiumOptimized : public Portable +{ +public: + static word TAOCRYPT_CDECL Add(word *C, const word *A, const word *B, + unsigned int N); + static word TAOCRYPT_CDECL Subtract(word *C, const word *A, const word *B, + unsigned int N); + static void TAOCRYPT_CDECL Multiply4(word *C, const word *A, + const word *B); + static void TAOCRYPT_CDECL Multiply8(word *C, const word *A, + const word *B); + static void TAOCRYPT_CDECL Multiply8Bottom(word *C, const word *A, + const word *B); +}; + +class P4Optimized +{ +public: + static word TAOCRYPT_CDECL Add(word *C, const word *A, const word *B, + unsigned int N); + static word TAOCRYPT_CDECL Subtract(word *C, const word *A, const word *B, + unsigned int N); +#ifdef SSE2_INTRINSICS_AVAILABLE + static void TAOCRYPT_CDECL Multiply4(word *C, const word *A, + const word *B); + static void TAOCRYPT_CDECL Multiply8(word *C, const word *A, + const word *B); + static void TAOCRYPT_CDECL Multiply8Bottom(word *C, const word *A, + const word *B); +#endif +}; + +typedef word (TAOCRYPT_CDECL * PAddSub)(word *C, const word *A, const word *B, + unsigned int N); +typedef void (TAOCRYPT_CDECL * PMul)(word *C, const word *A, const word *B); + +static PAddSub s_pAdd, s_pSub; +#ifdef SSE2_INTRINSICS_AVAILABLE +static PMul s_pMul4, s_pMul8, s_pMul8B; +#endif + +static void SetPentiumFunctionPointers() +{ + if (!IsPentium()) + { + s_pAdd = &Portable::Add; + s_pSub = &Portable::Subtract; + } + else if (IsP4()) + { + s_pAdd = &P4Optimized::Add; + s_pSub = &P4Optimized::Subtract; + } + else + { + s_pAdd = &PentiumOptimized::Add; + s_pSub = &PentiumOptimized::Subtract; + } + +#ifdef SSE2_INTRINSICS_AVAILABLE + if (!IsPentium()) + { + s_pMul4 = &Portable::Multiply4; + s_pMul8 = &Portable::Multiply8; + s_pMul8B = &Portable::Multiply8Bottom; + } + else if (HasSSE2()) + { + s_pMul4 = &P4Optimized::Multiply4; + s_pMul8 = &P4Optimized::Multiply8; + s_pMul8B = &P4Optimized::Multiply8Bottom; + } + else + { + s_pMul4 = &PentiumOptimized::Multiply4; + s_pMul8 = &PentiumOptimized::Multiply8; + s_pMul8B = &PentiumOptimized::Multiply8Bottom; + } +#endif +} + +static const char s_RunAtStartupSetPentiumFunctionPointers = + (SetPentiumFunctionPointers(), 0); + + +class LowLevel : public PentiumOptimized +{ +public: + inline static word Add(word *C, const word *A, const word *B, + unsigned int N) + {return s_pAdd(C, A, B, N);} + inline static word Subtract(word *C, const word *A, const word *B, + unsigned int N) + {return s_pSub(C, A, B, N);} + inline static void Square4(word *R, const word *A) + {Multiply4(R, A, A);} +#ifdef SSE2_INTRINSICS_AVAILABLE + inline static void Multiply4(word *C, const word *A, const word *B) + {s_pMul4(C, A, B);} + inline static void Multiply8(word *C, const word *A, const word *B) + {s_pMul8(C, A, B);} + inline static void Multiply8Bottom(word *C, const word *A, const word *B) + {s_pMul8B(C, A, B);} +#endif +}; + +// use some tricks to share assembly code between MSVC and GCC +#ifdef _MSC_VER + #define TAOCRYPT_NAKED __declspec(naked) + #define AS1(x) __asm x + #define AS2(x, y) __asm x, y + #define AddPrologue \ + __asm push ebp \ + __asm push ebx \ + __asm push esi \ + __asm push edi \ + __asm mov ecx, [esp+20] \ + __asm mov edx, [esp+24] \ + __asm mov ebx, [esp+28] \ + __asm mov esi, [esp+32] + #define AddEpilogue \ + __asm pop edi \ + __asm pop esi \ + __asm pop ebx \ + __asm pop ebp \ + __asm ret + #define MulPrologue \ + __asm push ebp \ + __asm push ebx \ + __asm push esi \ + __asm push edi \ + __asm mov ecx, [esp+28] \ + __asm mov esi, [esp+24] \ + __asm push [esp+20] + #define MulEpilogue \ + __asm add esp, 4 \ + __asm pop edi \ + __asm pop esi \ + __asm pop ebx \ + __asm pop ebp \ + __asm ret +#else + #define TAOCRYPT_NAKED + #define AS1(x) #x ";" + #define AS2(x, y) #x ", " #y ";" + #define AddPrologue \ + __asm__ __volatile__ \ + ( \ + "push %%ebx;" /* save this manually, in case of -fPIC */ \ + "mov %2, %%ebx;" \ + ".intel_syntax noprefix;" \ + "push ebp;" + #define AddEpilogue \ + "pop ebp;" \ + ".att_syntax prefix;" \ + "pop %%ebx;" \ + : \ + : "c" (C), "d" (A), "m" (B), "S" (N) \ + : "%edi", "memory", "cc" \ + ); + #define MulPrologue \ + __asm__ __volatile__ \ + ( \ + "push %%ebx;" /* save this manually, in case of -fPIC */ \ + "push %%ebp;" \ + "push %0;" \ + ".intel_syntax noprefix;" + #define MulEpilogue \ + "add esp, 4;" \ + "pop ebp;" \ + "pop ebx;" \ + ".att_syntax prefix;" \ + : \ + : "rm" (Z), "S" (X), "c" (Y) \ + : "%eax", "%edx", "%edi", "memory", "cc" \ + ); +#endif + +TAOCRYPT_NAKED word PentiumOptimized::Add(word *C, const word *A, + const word *B, unsigned int N) +{ + AddPrologue + + // now: ebx = B, ecx = C, edx = A, esi = N + AS2( sub ecx, edx) // hold the distance between C & A so we + // can add this to A to get C + AS2( xor eax, eax) // clear eax + + AS2( sub eax, esi) // eax is a negative index from end of B + AS2( lea ebx, [ebx+4*esi]) // ebx is end of B + + AS2( sar eax, 1) // unit of eax is now dwords; this also + // clears the carry flag + AS1( jz loopendAdd) // if no dwords then nothing to do + + AS1(loopstartAdd:) + AS2( mov esi,[edx]) // load lower word of A + AS2( mov ebp,[edx+4]) // load higher word of A + + AS2( mov edi,[ebx+8*eax]) // load lower word of B + AS2( lea edx,[edx+8]) // advance A and C + + AS2( adc esi,edi) // add lower words + AS2( mov edi,[ebx+8*eax+4]) // load higher word of B + + AS2( adc ebp,edi) // add higher words + AS1( inc eax) // advance B + + AS2( mov [edx+ecx-8],esi) // store lower word result + AS2( mov [edx+ecx-4],ebp) // store higher word result + + AS1( jnz loopstartAdd) // loop until eax overflows and becomes zero + + AS1(loopendAdd:) + AS2( adc eax, 0) // store carry into eax (return result register) + + AddEpilogue +} + +TAOCRYPT_NAKED word PentiumOptimized::Subtract(word *C, const word *A, + const word *B, unsigned int N) +{ + AddPrologue + + // now: ebx = B, ecx = C, edx = A, esi = N + AS2( sub ecx, edx) // hold the distance between C & A so we + // can add this to A to get C + AS2( xor eax, eax) // clear eax + + AS2( sub eax, esi) // eax is a negative index from end of B + AS2( lea ebx, [ebx+4*esi]) // ebx is end of B + + AS2( sar eax, 1) // unit of eax is now dwords; this also + // clears the carry flag + AS1( jz loopendSub) // if no dwords then nothing to do + + AS1(loopstartSub:) + AS2( mov esi,[edx]) // load lower word of A + AS2( mov ebp,[edx+4]) // load higher word of A + + AS2( mov edi,[ebx+8*eax]) // load lower word of B + AS2( lea edx,[edx+8]) // advance A and C + + AS2( sbb esi,edi) // subtract lower words + AS2( mov edi,[ebx+8*eax+4]) // load higher word of B + + AS2( sbb ebp,edi) // subtract higher words + AS1( inc eax) // advance B + + AS2( mov [edx+ecx-8],esi) // store lower word result + AS2( mov [edx+ecx-4],ebp) // store higher word result + + AS1( jnz loopstartSub) // loop until eax overflows and becomes zero + + AS1(loopendSub:) + AS2( adc eax, 0) // store carry into eax (return result register) + + AddEpilogue +} + +// On Pentium 4, the adc and sbb instructions are very expensive, so avoid them. + +TAOCRYPT_NAKED word P4Optimized::Add(word *C, const word *A, const word *B, + unsigned int N) +{ + AddPrologue + + // now: ebx = B, ecx = C, edx = A, esi = N + AS2( xor eax, eax) + AS1( neg esi) + AS1( jz loopendAddP4) // if no dwords then nothing to do + + AS2( mov edi, [edx]) + AS2( mov ebp, [ebx]) + AS1( jmp carry1AddP4) + + AS1(loopstartAddP4:) + AS2( mov edi, [edx+8]) + AS2( add ecx, 8) + AS2( add edx, 8) + AS2( mov ebp, [ebx]) + AS2( add edi, eax) + AS1( jc carry1AddP4) + AS2( xor eax, eax) + + AS1(carry1AddP4:) + AS2( add edi, ebp) + AS2( mov ebp, 1) + AS2( mov [ecx], edi) + AS2( mov edi, [edx+4]) + AS2( cmovc eax, ebp) + AS2( mov ebp, [ebx+4]) + AS2( add ebx, 8) + AS2( add edi, eax) + AS1( jc carry2AddP4) + AS2( xor eax, eax) + + AS1(carry2AddP4:) + AS2( add edi, ebp) + AS2( mov ebp, 1) + AS2( cmovc eax, ebp) + AS2( mov [ecx+4], edi) + AS2( add esi, 2) + AS1( jnz loopstartAddP4) + + AS1(loopendAddP4:) + + AddEpilogue +} + +TAOCRYPT_NAKED word P4Optimized::Subtract(word *C, const word *A, + const word *B, unsigned int N) +{ + AddPrologue + + // now: ebx = B, ecx = C, edx = A, esi = N + AS2( xor eax, eax) + AS1( neg esi) + AS1( jz loopendSubP4) // if no dwords then nothing to do + + AS2( mov edi, [edx]) + AS2( mov ebp, [ebx]) + AS1( jmp carry1SubP4) + + AS1(loopstartSubP4:) + AS2( mov edi, [edx+8]) + AS2( add edx, 8) + AS2( add ecx, 8) + AS2( mov ebp, [ebx]) + AS2( sub edi, eax) + AS1( jc carry1SubP4) + AS2( xor eax, eax) + + AS1(carry1SubP4:) + AS2( sub edi, ebp) + AS2( mov ebp, 1) + AS2( mov [ecx], edi) + AS2( mov edi, [edx+4]) + AS2( cmovc eax, ebp) + AS2( mov ebp, [ebx+4]) + AS2( add ebx, 8) + AS2( sub edi, eax) + AS1( jc carry2SubP4) + AS2( xor eax, eax) + + AS1(carry2SubP4:) + AS2( sub edi, ebp) + AS2( mov ebp, 1) + AS2( cmovc eax, ebp) + AS2( mov [ecx+4], edi) + AS2( add esi, 2) + AS1( jnz loopstartSubP4) + + AS1(loopendSubP4:) + + AddEpilogue +} + +// multiply assembly code originally contributed by Leonard Janke + +#define MulStartup \ + AS2(xor ebp, ebp) \ + AS2(xor edi, edi) \ + AS2(xor ebx, ebx) + +#define MulShiftCarry \ + AS2(mov ebp, edx) \ + AS2(mov edi, ebx) \ + AS2(xor ebx, ebx) + +#define MulAccumulateBottom(i,j) \ + AS2(mov eax, [ecx+4*j]) \ + AS2(imul eax, dword ptr [esi+4*i]) \ + AS2(add ebp, eax) + +#define MulAccumulate(i,j) \ + AS2(mov eax, [ecx+4*j]) \ + AS1(mul dword ptr [esi+4*i]) \ + AS2(add ebp, eax) \ + AS2(adc edi, edx) \ + AS2(adc bl, bh) + +#define MulStoreDigit(i) \ + AS2(mov edx, edi) \ + AS2(mov edi, [esp]) \ + AS2(mov [edi+4*i], ebp) + +#define MulLastDiagonal(digits) \ + AS2(mov eax, [ecx+4*(digits-1)]) \ + AS1(mul dword ptr [esi+4*(digits-1)]) \ + AS2(add ebp, eax) \ + AS2(adc edx, edi) \ + AS2(mov edi, [esp]) \ + AS2(mov [edi+4*(2*digits-2)], ebp) \ + AS2(mov [edi+4*(2*digits-1)], edx) + +TAOCRYPT_NAKED void PentiumOptimized::Multiply4(word* Z, const word* X, + const word* Y) +{ + MulPrologue + // now: [esp] = Z, esi = X, ecx = Y + MulStartup + MulAccumulate(0,0) + MulStoreDigit(0) + MulShiftCarry + + MulAccumulate(1,0) + MulAccumulate(0,1) + MulStoreDigit(1) + MulShiftCarry + + MulAccumulate(2,0) + MulAccumulate(1,1) + MulAccumulate(0,2) + MulStoreDigit(2) + MulShiftCarry + + MulAccumulate(3,0) + MulAccumulate(2,1) + MulAccumulate(1,2) + MulAccumulate(0,3) + MulStoreDigit(3) + MulShiftCarry + + MulAccumulate(3,1) + MulAccumulate(2,2) + MulAccumulate(1,3) + MulStoreDigit(4) + MulShiftCarry + + MulAccumulate(3,2) + MulAccumulate(2,3) + MulStoreDigit(5) + MulShiftCarry + + MulLastDiagonal(4) + MulEpilogue +} + +TAOCRYPT_NAKED void PentiumOptimized::Multiply8(word* Z, const word* X, + const word* Y) +{ + MulPrologue + // now: [esp] = Z, esi = X, ecx = Y + MulStartup + MulAccumulate(0,0) + MulStoreDigit(0) + MulShiftCarry + + MulAccumulate(1,0) + MulAccumulate(0,1) + MulStoreDigit(1) + MulShiftCarry + + MulAccumulate(2,0) + MulAccumulate(1,1) + MulAccumulate(0,2) + MulStoreDigit(2) + MulShiftCarry + + MulAccumulate(3,0) + MulAccumulate(2,1) + MulAccumulate(1,2) + MulAccumulate(0,3) + MulStoreDigit(3) + MulShiftCarry + + MulAccumulate(4,0) + MulAccumulate(3,1) + MulAccumulate(2,2) + MulAccumulate(1,3) + MulAccumulate(0,4) + MulStoreDigit(4) + MulShiftCarry + + MulAccumulate(5,0) + MulAccumulate(4,1) + MulAccumulate(3,2) + MulAccumulate(2,3) + MulAccumulate(1,4) + MulAccumulate(0,5) + MulStoreDigit(5) + MulShiftCarry + + MulAccumulate(6,0) + MulAccumulate(5,1) + MulAccumulate(4,2) + MulAccumulate(3,3) + MulAccumulate(2,4) + MulAccumulate(1,5) + MulAccumulate(0,6) + MulStoreDigit(6) + MulShiftCarry + + MulAccumulate(7,0) + MulAccumulate(6,1) + MulAccumulate(5,2) + MulAccumulate(4,3) + MulAccumulate(3,4) + MulAccumulate(2,5) + MulAccumulate(1,6) + MulAccumulate(0,7) + MulStoreDigit(7) + MulShiftCarry + + MulAccumulate(7,1) + MulAccumulate(6,2) + MulAccumulate(5,3) + MulAccumulate(4,4) + MulAccumulate(3,5) + MulAccumulate(2,6) + MulAccumulate(1,7) + MulStoreDigit(8) + MulShiftCarry + + MulAccumulate(7,2) + MulAccumulate(6,3) + MulAccumulate(5,4) + MulAccumulate(4,5) + MulAccumulate(3,6) + MulAccumulate(2,7) + MulStoreDigit(9) + MulShiftCarry + + MulAccumulate(7,3) + MulAccumulate(6,4) + MulAccumulate(5,5) + MulAccumulate(4,6) + MulAccumulate(3,7) + MulStoreDigit(10) + MulShiftCarry + + MulAccumulate(7,4) + MulAccumulate(6,5) + MulAccumulate(5,6) + MulAccumulate(4,7) + MulStoreDigit(11) + MulShiftCarry + + MulAccumulate(7,5) + MulAccumulate(6,6) + MulAccumulate(5,7) + MulStoreDigit(12) + MulShiftCarry + + MulAccumulate(7,6) + MulAccumulate(6,7) + MulStoreDigit(13) + MulShiftCarry + + MulLastDiagonal(8) + MulEpilogue +} + +TAOCRYPT_NAKED void PentiumOptimized::Multiply8Bottom(word* Z, const word* X, + const word* Y) +{ + MulPrologue + // now: [esp] = Z, esi = X, ecx = Y + MulStartup + MulAccumulate(0,0) + MulStoreDigit(0) + MulShiftCarry + + MulAccumulate(1,0) + MulAccumulate(0,1) + MulStoreDigit(1) + MulShiftCarry + + MulAccumulate(2,0) + MulAccumulate(1,1) + MulAccumulate(0,2) + MulStoreDigit(2) + MulShiftCarry + + MulAccumulate(3,0) + MulAccumulate(2,1) + MulAccumulate(1,2) + MulAccumulate(0,3) + MulStoreDigit(3) + MulShiftCarry + + MulAccumulate(4,0) + MulAccumulate(3,1) + MulAccumulate(2,2) + MulAccumulate(1,3) + MulAccumulate(0,4) + MulStoreDigit(4) + MulShiftCarry + + MulAccumulate(5,0) + MulAccumulate(4,1) + MulAccumulate(3,2) + MulAccumulate(2,3) + MulAccumulate(1,4) + MulAccumulate(0,5) + MulStoreDigit(5) + MulShiftCarry + + MulAccumulate(6,0) + MulAccumulate(5,1) + MulAccumulate(4,2) + MulAccumulate(3,3) + MulAccumulate(2,4) + MulAccumulate(1,5) + MulAccumulate(0,6) + MulStoreDigit(6) + MulShiftCarry + + MulAccumulateBottom(7,0) + MulAccumulateBottom(6,1) + MulAccumulateBottom(5,2) + MulAccumulateBottom(4,3) + MulAccumulateBottom(3,4) + MulAccumulateBottom(2,5) + MulAccumulateBottom(1,6) + MulAccumulateBottom(0,7) + MulStoreDigit(7) + MulEpilogue +} + +#undef AS1 +#undef AS2 + +#else // not x86 - no processor specific code at this layer + +typedef Portable LowLevel; + +#endif + +#ifdef SSE2_INTRINSICS_AVAILABLE + +#ifdef __GNUC__ +#define TAOCRYPT_FASTCALL +#else +#define TAOCRYPT_FASTCALL __fastcall +#endif + +static void TAOCRYPT_FASTCALL P4_Mul(__m128i *C, const __m128i *A, + const __m128i *B) +{ + __m128i a3210 = _mm_load_si128(A); + __m128i b3210 = _mm_load_si128(B); + + __m128i sum; + + __m128i z = _mm_setzero_si128(); + __m128i a2b2_a0b0 = _mm_mul_epu32(a3210, b3210); + C[0] = a2b2_a0b0; + + __m128i a3120 = _mm_shuffle_epi32(a3210, _MM_SHUFFLE(3, 1, 2, 0)); + __m128i b3021 = _mm_shuffle_epi32(b3210, _MM_SHUFFLE(3, 0, 2, 1)); + __m128i a1b0_a0b1 = _mm_mul_epu32(a3120, b3021); + __m128i a1b0 = _mm_unpackhi_epi32(a1b0_a0b1, z); + __m128i a0b1 = _mm_unpacklo_epi32(a1b0_a0b1, z); + C[1] = _mm_add_epi64(a1b0, a0b1); + + __m128i a31 = _mm_srli_epi64(a3210, 32); + __m128i b31 = _mm_srli_epi64(b3210, 32); + __m128i a3b3_a1b1 = _mm_mul_epu32(a31, b31); + C[6] = a3b3_a1b1; + + __m128i a1b1 = _mm_unpacklo_epi32(a3b3_a1b1, z); + __m128i b3012 = _mm_shuffle_epi32(b3210, _MM_SHUFFLE(3, 0, 1, 2)); + __m128i a2b0_a0b2 = _mm_mul_epu32(a3210, b3012); + __m128i a0b2 = _mm_unpacklo_epi32(a2b0_a0b2, z); + __m128i a2b0 = _mm_unpackhi_epi32(a2b0_a0b2, z); + sum = _mm_add_epi64(a1b1, a0b2); + C[2] = _mm_add_epi64(sum, a2b0); + + __m128i a2301 = _mm_shuffle_epi32(a3210, _MM_SHUFFLE(2, 3, 0, 1)); + __m128i b2103 = _mm_shuffle_epi32(b3210, _MM_SHUFFLE(2, 1, 0, 3)); + __m128i a3b0_a1b2 = _mm_mul_epu32(a2301, b3012); + __m128i a2b1_a0b3 = _mm_mul_epu32(a3210, b2103); + __m128i a3b0 = _mm_unpackhi_epi32(a3b0_a1b2, z); + __m128i a1b2 = _mm_unpacklo_epi32(a3b0_a1b2, z); + __m128i a2b1 = _mm_unpackhi_epi32(a2b1_a0b3, z); + __m128i a0b3 = _mm_unpacklo_epi32(a2b1_a0b3, z); + __m128i sum1 = _mm_add_epi64(a3b0, a1b2); + sum = _mm_add_epi64(a2b1, a0b3); + C[3] = _mm_add_epi64(sum, sum1); + + __m128i a3b1_a1b3 = _mm_mul_epu32(a2301, b2103); + __m128i a2b2 = _mm_unpackhi_epi32(a2b2_a0b0, z); + __m128i a3b1 = _mm_unpackhi_epi32(a3b1_a1b3, z); + __m128i a1b3 = _mm_unpacklo_epi32(a3b1_a1b3, z); + sum = _mm_add_epi64(a2b2, a3b1); + C[4] = _mm_add_epi64(sum, a1b3); + + __m128i a1302 = _mm_shuffle_epi32(a3210, _MM_SHUFFLE(1, 3, 0, 2)); + __m128i b1203 = _mm_shuffle_epi32(b3210, _MM_SHUFFLE(1, 2, 0, 3)); + __m128i a3b2_a2b3 = _mm_mul_epu32(a1302, b1203); + __m128i a3b2 = _mm_unpackhi_epi32(a3b2_a2b3, z); + __m128i a2b3 = _mm_unpacklo_epi32(a3b2_a2b3, z); + C[5] = _mm_add_epi64(a3b2, a2b3); +} + +void P4Optimized::Multiply4(word *C, const word *A, const word *B) +{ + __m128i temp[7]; + const word *w = (word *)temp; + const __m64 *mw = (__m64 *)w; + + P4_Mul(temp, (__m128i *)A, (__m128i *)B); + + C[0] = w[0]; + + __m64 s1, s2; + + __m64 w1 = _mm_cvtsi32_si64(w[1]); + __m64 w4 = mw[2]; + __m64 w6 = mw[3]; + __m64 w8 = mw[4]; + __m64 w10 = mw[5]; + __m64 w12 = mw[6]; + __m64 w14 = mw[7]; + __m64 w16 = mw[8]; + __m64 w18 = mw[9]; + __m64 w20 = mw[10]; + __m64 w22 = mw[11]; + __m64 w26 = _mm_cvtsi32_si64(w[26]); + + s1 = _mm_add_si64(w1, w4); + C[1] = _mm_cvtsi64_si32(s1); + s1 = _mm_srli_si64(s1, 32); + + s2 = _mm_add_si64(w6, w8); + s1 = _mm_add_si64(s1, s2); + C[2] = _mm_cvtsi64_si32(s1); + s1 = _mm_srli_si64(s1, 32); + + s2 = _mm_add_si64(w10, w12); + s1 = _mm_add_si64(s1, s2); + C[3] = _mm_cvtsi64_si32(s1); + s1 = _mm_srli_si64(s1, 32); + + s2 = _mm_add_si64(w14, w16); + s1 = _mm_add_si64(s1, s2); + C[4] = _mm_cvtsi64_si32(s1); + s1 = _mm_srli_si64(s1, 32); + + s2 = _mm_add_si64(w18, w20); + s1 = _mm_add_si64(s1, s2); + C[5] = _mm_cvtsi64_si32(s1); + s1 = _mm_srli_si64(s1, 32); + + s2 = _mm_add_si64(w22, w26); + s1 = _mm_add_si64(s1, s2); + C[6] = _mm_cvtsi64_si32(s1); + s1 = _mm_srli_si64(s1, 32); + + C[7] = _mm_cvtsi64_si32(s1) + w[27]; + _mm_empty(); +} + +void P4Optimized::Multiply8(word *C, const word *A, const word *B) +{ + __m128i temp[28]; + const word *w = (word *)temp; + const __m64 *mw = (__m64 *)w; + const word *x = (word *)temp+7*4; + const __m64 *mx = (__m64 *)x; + const word *y = (word *)temp+7*4*2; + const __m64 *my = (__m64 *)y; + const word *z = (word *)temp+7*4*3; + const __m64 *mz = (__m64 *)z; + + P4_Mul(temp, (__m128i *)A, (__m128i *)B); + + P4_Mul(temp+7, (__m128i *)A+1, (__m128i *)B); + + P4_Mul(temp+14, (__m128i *)A, (__m128i *)B+1); + + P4_Mul(temp+21, (__m128i *)A+1, (__m128i *)B+1); + + C[0] = w[0]; + + __m64 s1, s2, s3, s4; + + __m64 w1 = _mm_cvtsi32_si64(w[1]); + __m64 w4 = mw[2]; + __m64 w6 = mw[3]; + __m64 w8 = mw[4]; + __m64 w10 = mw[5]; + __m64 w12 = mw[6]; + __m64 w14 = mw[7]; + __m64 w16 = mw[8]; + __m64 w18 = mw[9]; + __m64 w20 = mw[10]; + __m64 w22 = mw[11]; + __m64 w26 = _mm_cvtsi32_si64(w[26]); + __m64 w27 = _mm_cvtsi32_si64(w[27]); + + __m64 x0 = _mm_cvtsi32_si64(x[0]); + __m64 x1 = _mm_cvtsi32_si64(x[1]); + __m64 x4 = mx[2]; + __m64 x6 = mx[3]; + __m64 x8 = mx[4]; + __m64 x10 = mx[5]; + __m64 x12 = mx[6]; + __m64 x14 = mx[7]; + __m64 x16 = mx[8]; + __m64 x18 = mx[9]; + __m64 x20 = mx[10]; + __m64 x22 = mx[11]; + __m64 x26 = _mm_cvtsi32_si64(x[26]); + __m64 x27 = _mm_cvtsi32_si64(x[27]); + + __m64 y0 = _mm_cvtsi32_si64(y[0]); + __m64 y1 = _mm_cvtsi32_si64(y[1]); + __m64 y4 = my[2]; + __m64 y6 = my[3]; + __m64 y8 = my[4]; + __m64 y10 = my[5]; + __m64 y12 = my[6]; + __m64 y14 = my[7]; + __m64 y16 = my[8]; + __m64 y18 = my[9]; + __m64 y20 = my[10]; + __m64 y22 = my[11]; + __m64 y26 = _mm_cvtsi32_si64(y[26]); + __m64 y27 = _mm_cvtsi32_si64(y[27]); + + __m64 z0 = _mm_cvtsi32_si64(z[0]); + __m64 z1 = _mm_cvtsi32_si64(z[1]); + __m64 z4 = mz[2]; + __m64 z6 = mz[3]; + __m64 z8 = mz[4]; + __m64 z10 = mz[5]; + __m64 z12 = mz[6]; + __m64 z14 = mz[7]; + __m64 z16 = mz[8]; + __m64 z18 = mz[9]; + __m64 z20 = mz[10]; + __m64 z22 = mz[11]; + __m64 z26 = _mm_cvtsi32_si64(z[26]); + + s1 = _mm_add_si64(w1, w4); + C[1] = _mm_cvtsi64_si32(s1); + s1 = _mm_srli_si64(s1, 32); + + s2 = _mm_add_si64(w6, w8); + s1 = _mm_add_si64(s1, s2); + C[2] = _mm_cvtsi64_si32(s1); + s1 = _mm_srli_si64(s1, 32); + + s2 = _mm_add_si64(w10, w12); + s1 = _mm_add_si64(s1, s2); + C[3] = _mm_cvtsi64_si32(s1); + s1 = _mm_srli_si64(s1, 32); + + s3 = _mm_add_si64(x0, y0); + s2 = _mm_add_si64(w14, w16); + s1 = _mm_add_si64(s1, s3); + s1 = _mm_add_si64(s1, s2); + C[4] = _mm_cvtsi64_si32(s1); + s1 = _mm_srli_si64(s1, 32); + + s3 = _mm_add_si64(x1, y1); + s4 = _mm_add_si64(x4, y4); + s1 = _mm_add_si64(s1, w18); + s3 = _mm_add_si64(s3, s4); + s1 = _mm_add_si64(s1, w20); + s1 = _mm_add_si64(s1, s3); + C[5] = _mm_cvtsi64_si32(s1); + s1 = _mm_srli_si64(s1, 32); + + s3 = _mm_add_si64(x6, y6); + s4 = _mm_add_si64(x8, y8); + s1 = _mm_add_si64(s1, w22); + s3 = _mm_add_si64(s3, s4); + s1 = _mm_add_si64(s1, w26); + s1 = _mm_add_si64(s1, s3); + C[6] = _mm_cvtsi64_si32(s1); + s1 = _mm_srli_si64(s1, 32); + + s3 = _mm_add_si64(x10, y10); + s4 = _mm_add_si64(x12, y12); + s1 = _mm_add_si64(s1, w27); + s3 = _mm_add_si64(s3, s4); + s1 = _mm_add_si64(s1, s3); + C[7] = _mm_cvtsi64_si32(s1); + s1 = _mm_srli_si64(s1, 32); + + s3 = _mm_add_si64(x14, y14); + s4 = _mm_add_si64(x16, y16); + s1 = _mm_add_si64(s1, z0); + s3 = _mm_add_si64(s3, s4); + s1 = _mm_add_si64(s1, s3); + C[8] = _mm_cvtsi64_si32(s1); + s1 = _mm_srli_si64(s1, 32); + + s3 = _mm_add_si64(x18, y18); + s4 = _mm_add_si64(x20, y20); + s1 = _mm_add_si64(s1, z1); + s3 = _mm_add_si64(s3, s4); + s1 = _mm_add_si64(s1, z4); + s1 = _mm_add_si64(s1, s3); + C[9] = _mm_cvtsi64_si32(s1); + s1 = _mm_srli_si64(s1, 32); + + s3 = _mm_add_si64(x22, y22); + s4 = _mm_add_si64(x26, y26); + s1 = _mm_add_si64(s1, z6); + s3 = _mm_add_si64(s3, s4); + s1 = _mm_add_si64(s1, z8); + s1 = _mm_add_si64(s1, s3); + C[10] = _mm_cvtsi64_si32(s1); + s1 = _mm_srli_si64(s1, 32); + + s3 = _mm_add_si64(x27, y27); + s1 = _mm_add_si64(s1, z10); + s1 = _mm_add_si64(s1, z12); + s1 = _mm_add_si64(s1, s3); + C[11] = _mm_cvtsi64_si32(s1); + s1 = _mm_srli_si64(s1, 32); + + s3 = _mm_add_si64(z14, z16); + s1 = _mm_add_si64(s1, s3); + C[12] = _mm_cvtsi64_si32(s1); + s1 = _mm_srli_si64(s1, 32); + + s3 = _mm_add_si64(z18, z20); + s1 = _mm_add_si64(s1, s3); + C[13] = _mm_cvtsi64_si32(s1); + s1 = _mm_srli_si64(s1, 32); + + s3 = _mm_add_si64(z22, z26); + s1 = _mm_add_si64(s1, s3); + C[14] = _mm_cvtsi64_si32(s1); + s1 = _mm_srli_si64(s1, 32); + + C[15] = z[27] + _mm_cvtsi64_si32(s1); + _mm_empty(); +} + +void P4Optimized::Multiply8Bottom(word *C, const word *A, const word *B) +{ + __m128i temp[21]; + const word *w = (word *)temp; + const __m64 *mw = (__m64 *)w; + const word *x = (word *)temp+7*4; + const __m64 *mx = (__m64 *)x; + const word *y = (word *)temp+7*4*2; + const __m64 *my = (__m64 *)y; + + P4_Mul(temp, (__m128i *)A, (__m128i *)B); + + P4_Mul(temp+7, (__m128i *)A+1, (__m128i *)B); + + P4_Mul(temp+14, (__m128i *)A, (__m128i *)B+1); + + C[0] = w[0]; + + __m64 s1, s2, s3, s4; + + __m64 w1 = _mm_cvtsi32_si64(w[1]); + __m64 w4 = mw[2]; + __m64 w6 = mw[3]; + __m64 w8 = mw[4]; + __m64 w10 = mw[5]; + __m64 w12 = mw[6]; + __m64 w14 = mw[7]; + __m64 w16 = mw[8]; + __m64 w18 = mw[9]; + __m64 w20 = mw[10]; + __m64 w22 = mw[11]; + __m64 w26 = _mm_cvtsi32_si64(w[26]); + + __m64 x0 = _mm_cvtsi32_si64(x[0]); + __m64 x1 = _mm_cvtsi32_si64(x[1]); + __m64 x4 = mx[2]; + __m64 x6 = mx[3]; + __m64 x8 = mx[4]; + + __m64 y0 = _mm_cvtsi32_si64(y[0]); + __m64 y1 = _mm_cvtsi32_si64(y[1]); + __m64 y4 = my[2]; + __m64 y6 = my[3]; + __m64 y8 = my[4]; + + s1 = _mm_add_si64(w1, w4); + C[1] = _mm_cvtsi64_si32(s1); + s1 = _mm_srli_si64(s1, 32); + + s2 = _mm_add_si64(w6, w8); + s1 = _mm_add_si64(s1, s2); + C[2] = _mm_cvtsi64_si32(s1); + s1 = _mm_srli_si64(s1, 32); + + s2 = _mm_add_si64(w10, w12); + s1 = _mm_add_si64(s1, s2); + C[3] = _mm_cvtsi64_si32(s1); + s1 = _mm_srli_si64(s1, 32); + + s3 = _mm_add_si64(x0, y0); + s2 = _mm_add_si64(w14, w16); + s1 = _mm_add_si64(s1, s3); + s1 = _mm_add_si64(s1, s2); + C[4] = _mm_cvtsi64_si32(s1); + s1 = _mm_srli_si64(s1, 32); + + s3 = _mm_add_si64(x1, y1); + s4 = _mm_add_si64(x4, y4); + s1 = _mm_add_si64(s1, w18); + s3 = _mm_add_si64(s3, s4); + s1 = _mm_add_si64(s1, w20); + s1 = _mm_add_si64(s1, s3); + C[5] = _mm_cvtsi64_si32(s1); + s1 = _mm_srli_si64(s1, 32); + + s3 = _mm_add_si64(x6, y6); + s4 = _mm_add_si64(x8, y8); + s1 = _mm_add_si64(s1, w22); + s3 = _mm_add_si64(s3, s4); + s1 = _mm_add_si64(s1, w26); + s1 = _mm_add_si64(s1, s3); + C[6] = _mm_cvtsi64_si32(s1); + s1 = _mm_srli_si64(s1, 32); + + C[7] = _mm_cvtsi64_si32(s1) + w[27] + x[10] + y[10] + x[12] + y[12]; + _mm_empty(); +} + +#endif // #ifdef SSE2_INTRINSICS_AVAILABLE + +// end optimized + +// ******************************************************** + +#define A0 A +#define A1 (A+N2) +#define B0 B +#define B1 (B+N2) + +#define T0 T +#define T1 (T+N2) +#define T2 (T+N) +#define T3 (T+N+N2) + +#define R0 R +#define R1 (R+N2) +#define R2 (R+N) +#define R3 (R+N+N2) + +//VC60 workaround: compiler bug triggered without the extra dummy parameters + +// R[2*N] - result = A*B +// T[2*N] - temporary work space +// A[N] --- multiplier +// B[N] --- multiplicant + + +void RecursiveMultiply(word *R, word *T, const word *A, const word *B, + unsigned int N) +{ + assert(N>=2 && N%2==0); + + if (LowLevel::MultiplyRecursionLimit() >= 8 && N==8) + LowLevel::Multiply8(R, A, B); + else if (LowLevel::MultiplyRecursionLimit() >= 4 && N==4) + LowLevel::Multiply4(R, A, B); + else if (N==2) + LowLevel::Multiply2(R, A, B); + else + { + const unsigned int N2 = N/2; + int carry; + + int aComp = Compare(A0, A1, N2); + int bComp = Compare(B0, B1, N2); + + switch (2*aComp + aComp + bComp) + { + case -4: + LowLevel::Subtract(R0, A1, A0, N2); + LowLevel::Subtract(R1, B0, B1, N2); + RecursiveMultiply(T0, T2, R0, R1, N2); + LowLevel::Subtract(T1, T1, R0, N2); + carry = -1; + break; + case -2: + LowLevel::Subtract(R0, A1, A0, N2); + LowLevel::Subtract(R1, B0, B1, N2); + RecursiveMultiply(T0, T2, R0, R1, N2); + carry = 0; + break; + case 2: + LowLevel::Subtract(R0, A0, A1, N2); + LowLevel::Subtract(R1, B1, B0, N2); + RecursiveMultiply(T0, T2, R0, R1, N2); + carry = 0; + break; + case 4: + LowLevel::Subtract(R0, A1, A0, N2); + LowLevel::Subtract(R1, B0, B1, N2); + RecursiveMultiply(T0, T2, R0, R1, N2); + LowLevel::Subtract(T1, T1, R1, N2); + carry = -1; + break; + default: + SetWords(T0, 0, N); + carry = 0; + } + + RecursiveMultiply(R0, T2, A0, B0, N2); + RecursiveMultiply(R2, T2, A1, B1, N2); + + // now T[01] holds (A1-A0)*(B0-B1),R[01] holds A0*B0, R[23] holds A1*B1 + + carry += LowLevel::Add(T0, T0, R0, N); + carry += LowLevel::Add(T0, T0, R2, N); + carry += LowLevel::Add(R1, R1, T0, N); + + assert (carry >= 0 && carry <= 2); + Increment(R3, N2, carry); + } +} + + +void RecursiveSquare(word *R, word *T, const word *A, unsigned int N) +{ + assert(N && N%2==0); + if (LowLevel::SquareRecursionLimit() >= 8 && N==8) + LowLevel::Square8(R, A); + if (LowLevel::SquareRecursionLimit() >= 4 && N==4) + LowLevel::Square4(R, A); + else if (N==2) + LowLevel::Square2(R, A); + else + { + const unsigned int N2 = N/2; + + RecursiveSquare(R0, T2, A0, N2); + RecursiveSquare(R2, T2, A1, N2); + RecursiveMultiply(T0, T2, A0, A1, N2); + + word carry = LowLevel::Add(R1, R1, T0, N); + carry += LowLevel::Add(R1, R1, T0, N); + Increment(R3, N2, carry); + } +} + + +// R[N] - bottom half of A*B +// T[N] - temporary work space +// A[N] - multiplier +// B[N] - multiplicant + + +void RecursiveMultiplyBottom(word *R, word *T, const word *A, const word *B, + unsigned int N) +{ + assert(N>=2 && N%2==0); + if (LowLevel::MultiplyBottomRecursionLimit() >= 8 && N==8) + LowLevel::Multiply8Bottom(R, A, B); + else if (LowLevel::MultiplyBottomRecursionLimit() >= 4 && N==4) + LowLevel::Multiply4Bottom(R, A, B); + else if (N==2) + LowLevel::Multiply2Bottom(R, A, B); + else + { + const unsigned int N2 = N/2; + + RecursiveMultiply(R, T, A0, B0, N2); + RecursiveMultiplyBottom(T0, T1, A1, B0, N2); + LowLevel::Add(R1, R1, T0, N2); + RecursiveMultiplyBottom(T0, T1, A0, B1, N2); + LowLevel::Add(R1, R1, T0, N2); + } +} + + +void RecursiveMultiplyTop(word *R, word *T, const word *L, const word *A, + const word *B, unsigned int N) +{ + assert(N>=2 && N%2==0); + + if (N==4) + { + LowLevel::Multiply4(T, A, B); + memcpy(R, T+4, 4*WORD_SIZE); + } + else if (N==2) + { + LowLevel::Multiply2(T, A, B); + memcpy(R, T+2, 2*WORD_SIZE); + } + else + { + const unsigned int N2 = N/2; + int carry; + + int aComp = Compare(A0, A1, N2); + int bComp = Compare(B0, B1, N2); + + switch (2*aComp + aComp + bComp) + { + case -4: + LowLevel::Subtract(R0, A1, A0, N2); + LowLevel::Subtract(R1, B0, B1, N2); + RecursiveMultiply(T0, T2, R0, R1, N2); + LowLevel::Subtract(T1, T1, R0, N2); + carry = -1; + break; + case -2: + LowLevel::Subtract(R0, A1, A0, N2); + LowLevel::Subtract(R1, B0, B1, N2); + RecursiveMultiply(T0, T2, R0, R1, N2); + carry = 0; + break; + case 2: + LowLevel::Subtract(R0, A0, A1, N2); + LowLevel::Subtract(R1, B1, B0, N2); + RecursiveMultiply(T0, T2, R0, R1, N2); + carry = 0; + break; + case 4: + LowLevel::Subtract(R0, A1, A0, N2); + LowLevel::Subtract(R1, B0, B1, N2); + RecursiveMultiply(T0, T2, R0, R1, N2); + LowLevel::Subtract(T1, T1, R1, N2); + carry = -1; + break; + default: + SetWords(T0, 0, N); + carry = 0; + } + + RecursiveMultiply(T2, R0, A1, B1, N2); + + // now T[01] holds (A1-A0)*(B0-B1), T[23] holds A1*B1 + + word c2 = LowLevel::Subtract(R0, L+N2, L, N2); + c2 += LowLevel::Subtract(R0, R0, T0, N2); + word t = (Compare(R0, T2, N2) == -1); + + carry += t; + carry += Increment(R0, N2, c2+t); + carry += LowLevel::Add(R0, R0, T1, N2); + carry += LowLevel::Add(R0, R0, T3, N2); + assert (carry >= 0 && carry <= 2); + + CopyWords(R1, T3, N2); + Increment(R1, N2, carry); + } +} + + +inline word Add(word *C, const word *A, const word *B, unsigned int N) +{ + return LowLevel::Add(C, A, B, N); +} + +inline word Subtract(word *C, const word *A, const word *B, unsigned int N) +{ + return LowLevel::Subtract(C, A, B, N); +} + +inline void Multiply(word *R, word *T, const word *A, const word *B, + unsigned int N) +{ + RecursiveMultiply(R, T, A, B, N); +} + +inline void Square(word *R, word *T, const word *A, unsigned int N) +{ + RecursiveSquare(R, T, A, N); +} + + +void AsymmetricMultiply(word *R, word *T, const word *A, unsigned int NA, + const word *B, unsigned int NB) +{ + if (NA == NB) + { + if (A == B) + Square(R, T, A, NA); + else + Multiply(R, T, A, B, NA); + + return; + } + + if (NA > NB) + { + STL::swap(A, B); + STL::swap(NA, NB); + } + + assert(NB % NA == 0); + assert((NB/NA)%2 == 0); // NB is an even multiple of NA + + if (NA==2 && !A[1]) + { + switch (A[0]) + { + case 0: + SetWords(R, 0, NB+2); + return; + case 1: + CopyWords(R, B, NB); + R[NB] = R[NB+1] = 0; + return; + default: + R[NB] = LinearMultiply(R, B, A[0], NB); + R[NB+1] = 0; + return; + } + } + + Multiply(R, T, A, B, NA); + CopyWords(T+2*NA, R+NA, NA); + + unsigned i; + + for (i=2*NA; i<NB; i+=2*NA) + Multiply(T+NA+i, T, A, B+i, NA); + for (i=NA; i<NB; i+=2*NA) + Multiply(R+i, T, A, B+i, NA); + + if (Add(R+NA, R+NA, T+2*NA, NB-NA)) + Increment(R+NB, NA); +} + + +void PositiveMultiply(Integer& product, const Integer& a, const Integer& b) +{ + unsigned int aSize = RoundupSize(a.WordCount()); + unsigned int bSize = RoundupSize(b.WordCount()); + + product.reg_.CleanNew(RoundupSize(aSize + bSize)); + product.sign_ = Integer::POSITIVE; + + AlignedWordBlock workspace(aSize + bSize); + AsymmetricMultiply(product.reg_.get_buffer(), workspace.get_buffer(), + a.reg_.get_buffer(), aSize, b.reg_.get_buffer(), bSize); +} + +void Multiply(Integer &product, const Integer &a, const Integer &b) +{ + PositiveMultiply(product, a, b); + + if (a.NotNegative() != b.NotNegative()) + product.Negate(); +} + + +static inline unsigned int EvenWordCount(const word *X, unsigned int N) +{ + while (N && X[N-2]==0 && X[N-1]==0) + N-=2; + return N; +} + + +unsigned int AlmostInverse(word *R, word *T, const word *A, unsigned int NA, + const word *M, unsigned int N) +{ + assert(NA<=N && N && N%2==0); + + word *b = T; + word *c = T+N; + word *f = T+2*N; + word *g = T+3*N; + unsigned int bcLen=2, fgLen=EvenWordCount(M, N); + unsigned int k=0, s=0; + + SetWords(T, 0, 3*N); + b[0]=1; + CopyWords(f, A, NA); + CopyWords(g, M, N); + + while (1) + { + word t=f[0]; + while (!t) + { + if (EvenWordCount(f, fgLen)==0) + { + SetWords(R, 0, N); + return 0; + } + + ShiftWordsRightByWords(f, fgLen, 1); + if (c[bcLen-1]) bcLen+=2; + assert(bcLen <= N); + ShiftWordsLeftByWords(c, bcLen, 1); + k+=WORD_BITS; + t=f[0]; + } + + unsigned int i=0; + while (t%2 == 0) + { + t>>=1; + i++; + } + k+=i; + + if (t==1 && f[1]==0 && EvenWordCount(f, fgLen)==2) + { + if (s%2==0) + CopyWords(R, b, N); + else + Subtract(R, M, b, N); + return k; + } + + ShiftWordsRightByBits(f, fgLen, i); + t=ShiftWordsLeftByBits(c, bcLen, i); + if (t) + { + c[bcLen] = t; + bcLen+=2; + assert(bcLen <= N); + } + + if (f[fgLen-2]==0 && g[fgLen-2]==0 && f[fgLen-1]==0 && g[fgLen-1]==0) + fgLen-=2; + + if (Compare(f, g, fgLen)==-1) + { + STL::swap(f, g); + STL::swap(b, c); + s++; + } + + Subtract(f, f, g, fgLen); + + if (Add(b, b, c, bcLen)) + { + b[bcLen] = 1; + bcLen+=2; + assert(bcLen <= N); + } + } +} + +// R[N] - result = A/(2^k) mod M +// A[N] - input +// M[N] - modulus + +void DivideByPower2Mod(word *R, const word *A, unsigned int k, const word *M, + unsigned int N) +{ + CopyWords(R, A, N); + + while (k--) + { + if (R[0]%2==0) + ShiftWordsRightByBits(R, N, 1); + else + { + word carry = Add(R, R, M, N); + ShiftWordsRightByBits(R, N, 1); + R[N-1] += carry<<(WORD_BITS-1); + } + } +} + +// R[N] - result = A*(2^k) mod M +// A[N] - input +// M[N] - modulus + +void MultiplyByPower2Mod(word *R, const word *A, unsigned int k, const word *M, + unsigned int N) +{ + CopyWords(R, A, N); + + while (k--) + if (ShiftWordsLeftByBits(R, N, 1) || Compare(R, M, N)>=0) + Subtract(R, R, M, N); +} + + +// ********** end of integer needs + + +Integer::Integer() + : reg_(2), sign_(POSITIVE) +{ + reg_[0] = reg_[1] = 0; +} + + +Integer::Integer(const Integer& t) + : reg_(RoundupSize(t.WordCount())), sign_(t.sign_) +{ + CopyWords(reg_.get_buffer(), t.reg_.get_buffer(), reg_.size()); +} + + +Integer::Integer(signed long value) + : reg_(2) +{ + if (value >= 0) + sign_ = POSITIVE; + else + { + sign_ = NEGATIVE; + value = -value; + } + reg_[0] = word(value); + reg_[1] = word(SafeRightShift<WORD_BITS, unsigned long>(value)); +} + + +Integer::Integer(Sign s, word high, word low) + : reg_(2), sign_(s) +{ + reg_[0] = low; + reg_[1] = high; +} + + +Integer::Integer(word value, unsigned int length) + : reg_(RoundupSize(length)), sign_(POSITIVE) +{ + reg_[0] = value; + SetWords(reg_ + 1, 0, reg_.size() - 1); +} + + +Integer::Integer(const byte *encodedInteger, unsigned int byteCount, + Signedness s) +{ + Decode(encodedInteger, byteCount, s); +} + +class BadBER {}; + +// BER Decode Source +Integer::Integer(Source& source) + : reg_(2), sign_(POSITIVE) +{ + Decode(source); +} + +void Integer::Decode(Source& source) +{ + byte b = source.next(); + if (b != INTEGER) { + source.SetError(INTEGER_E); + return; + } + + word32 length = GetLength(source); + + if ( (b = source.next()) == 0x00) + length--; + else + source.prev(); + + unsigned int words = (length + WORD_SIZE - 1) / WORD_SIZE; + words = RoundupSize(words); + if (words > reg_.size()) reg_.CleanNew(words); + + for (int j = length; j > 0; j--) { + b = source.next(); + reg_ [(j-1) / WORD_SIZE] |= (word)b << ((j-1) % WORD_SIZE) * 8; + } +} + + +void Integer::Decode(const byte* input, unsigned int inputLen, Signedness s) +{ + unsigned int idx(0); + byte b = input[idx++]; + sign_ = ((s==SIGNED) && (b & 0x80)) ? NEGATIVE : POSITIVE; + + while (inputLen>0 && (sign_==POSITIVE ? b==0 : b==0xff)) + { + inputLen--; + b = input[idx++]; + } + + reg_.CleanNew(RoundupSize(BytesToWords(inputLen))); + + --idx; + for (unsigned int i=inputLen; i > 0; i--) + { + b = input[idx++]; + reg_[(i-1)/WORD_SIZE] |= (word)b << ((i-1)%WORD_SIZE)*8; + } + + if (sign_ == NEGATIVE) + { + for (unsigned i=inputLen; i<reg_.size()*WORD_SIZE; i++) + reg_[i/WORD_SIZE] |= (word)0xff << (i%WORD_SIZE)*8; + TwosComplement(reg_.get_buffer(), reg_.size()); + } +} + + +unsigned int Integer::Encode(byte* output, unsigned int outputLen, + Signedness signedness) const +{ + unsigned int idx(0); + if (signedness == UNSIGNED || NotNegative()) + { + for (unsigned int i=outputLen; i > 0; i--) + output[idx++] = GetByte(i-1); + } + else + { + // take two's complement of *this + Integer temp = Integer::Power2(8*max(ByteCount(), outputLen)) + *this; + for (unsigned i=0; i<outputLen; i++) + output[idx++] = temp.GetByte(outputLen-i-1); + } + return outputLen; +} + + +static Integer* zero = 0; + +const Integer &Integer::Zero() +{ + if (!zero) + zero = NEW_TC Integer; + return *zero; +} + + +static Integer* one = 0; + +const Integer &Integer::One() +{ + if (!one) + one = NEW_TC Integer(1,2); + return *one; +} + + +// Clean up static singleton holders, not a leak, but helpful to have gone +// when checking for leaks +void CleanUp() +{ + tcDelete(one); + tcDelete(zero); + + // In case user calls more than once, prevent seg fault + one = 0; + zero = 0; +} + +Integer::Integer(RandomNumberGenerator& rng, const Integer& min, + const Integer& max) +{ + Randomize(rng, min, max); +} + + +void Integer::Randomize(RandomNumberGenerator& rng, unsigned int nbits) +{ + const unsigned int nbytes = nbits/8 + 1; + ByteBlock buf(nbytes); + rng.GenerateBlock(buf.get_buffer(), nbytes); + if (nbytes) + buf[0] = (byte)Crop(buf[0], nbits % 8); + Decode(buf.get_buffer(), nbytes, UNSIGNED); +} + +void Integer::Randomize(RandomNumberGenerator& rng, const Integer& min, + const Integer& max) +{ + assert(min <= max); + + Integer range = max - min; + const unsigned int nbits = range.BitCount(); + + do + { + Randomize(rng, nbits); + } + while (*this > range); + + *this += min; +} + + +Integer Integer::Power2(unsigned int e) +{ + Integer r((word)0, BitsToWords(e + 1)); + r.SetBit(e); + return r; +} + + +void Integer::SetBit(unsigned int n, bool value) +{ + if (value) + { + reg_.CleanGrow(RoundupSize(BitsToWords(n + 1))); + reg_[n / WORD_BITS] |= (word(1) << (n % WORD_BITS)); + } + else + { + if (n / WORD_BITS < reg_.size()) + reg_[n / WORD_BITS] &= ~(word(1) << (n % WORD_BITS)); + } +} + + +void Integer::SetByte(unsigned int n, byte value) +{ + reg_.CleanGrow(RoundupSize(BytesToWords(n+1))); + reg_[n/WORD_SIZE] &= ~(word(0xff) << 8*(n%WORD_SIZE)); + reg_[n/WORD_SIZE] |= (word(value) << 8*(n%WORD_SIZE)); +} + + +void Integer::Negate() +{ + if (!!(*this)) // don't flip sign if *this==0 + sign_ = Sign(1 - sign_); +} + + +bool Integer::operator!() const +{ + return IsNegative() ? false : (reg_[0]==0 && WordCount()==0); +} + + +Integer& Integer::operator=(const Integer& t) +{ + if (this != &t) + { + reg_.New(RoundupSize(t.WordCount())); + CopyWords(reg_.get_buffer(), t.reg_.get_buffer(), reg_.size()); + sign_ = t.sign_; + } + return *this; +} + + +Integer& Integer::operator+=(const Integer& t) +{ + reg_.CleanGrow(t.reg_.size()); + if (NotNegative()) + { + if (t.NotNegative()) + PositiveAdd(*this, *this, t); + else + PositiveSubtract(*this, *this, t); + } + else + { + if (t.NotNegative()) + PositiveSubtract(*this, t, *this); + else + { + PositiveAdd(*this, *this, t); + sign_ = Integer::NEGATIVE; + } + } + return *this; +} + + +Integer Integer::operator-() const +{ + Integer result(*this); + result.Negate(); + return result; +} + + +Integer& Integer::operator-=(const Integer& t) +{ + reg_.CleanGrow(t.reg_.size()); + if (NotNegative()) + { + if (t.NotNegative()) + PositiveSubtract(*this, *this, t); + else + PositiveAdd(*this, *this, t); + } + else + { + if (t.NotNegative()) + { + PositiveAdd(*this, *this, t); + sign_ = Integer::NEGATIVE; + } + else + PositiveSubtract(*this, t, *this); + } + return *this; +} + + +Integer& Integer::operator++() +{ + if (NotNegative()) + { + if (Increment(reg_.get_buffer(), reg_.size())) + { + reg_.CleanGrow(2*reg_.size()); + reg_[reg_.size()/2]=1; + } + } + else + { + word borrow = Decrement(reg_.get_buffer(), reg_.size()); + assert(!borrow); + if (WordCount()==0) + *this = Zero(); + } + return *this; +} + +Integer& Integer::operator--() +{ + if (IsNegative()) + { + if (Increment(reg_.get_buffer(), reg_.size())) + { + reg_.CleanGrow(2*reg_.size()); + reg_[reg_.size()/2]=1; + } + } + else + { + if (Decrement(reg_.get_buffer(), reg_.size())) + *this = -One(); + } + return *this; +} + + +Integer& Integer::operator<<=(unsigned int n) +{ + const unsigned int wordCount = WordCount(); + const unsigned int shiftWords = n / WORD_BITS; + const unsigned int shiftBits = n % WORD_BITS; + + reg_.CleanGrow(RoundupSize(wordCount+BitsToWords(n))); + ShiftWordsLeftByWords(reg_.get_buffer(), wordCount + shiftWords, + shiftWords); + ShiftWordsLeftByBits(reg_+shiftWords, wordCount+BitsToWords(shiftBits), + shiftBits); + return *this; +} + +Integer& Integer::operator>>=(unsigned int n) +{ + const unsigned int wordCount = WordCount(); + const unsigned int shiftWords = n / WORD_BITS; + const unsigned int shiftBits = n % WORD_BITS; + + ShiftWordsRightByWords(reg_.get_buffer(), wordCount, shiftWords); + if (wordCount > shiftWords) + ShiftWordsRightByBits(reg_.get_buffer(), wordCount-shiftWords, + shiftBits); + if (IsNegative() && WordCount()==0) // avoid -0 + *this = Zero(); + return *this; +} + + +void PositiveAdd(Integer& sum, const Integer& a, const Integer& b) +{ + word carry; + if (a.reg_.size() == b.reg_.size()) + carry = Add(sum.reg_.get_buffer(), a.reg_.get_buffer(), + b.reg_.get_buffer(), a.reg_.size()); + else if (a.reg_.size() > b.reg_.size()) + { + carry = Add(sum.reg_.get_buffer(), a.reg_.get_buffer(), + b.reg_.get_buffer(), b.reg_.size()); + CopyWords(sum.reg_+b.reg_.size(), a.reg_+b.reg_.size(), + a.reg_.size()-b.reg_.size()); + carry = Increment(sum.reg_+b.reg_.size(), a.reg_.size()-b.reg_.size(), + carry); + } + else + { + carry = Add(sum.reg_.get_buffer(), a.reg_.get_buffer(), + b.reg_.get_buffer(), a.reg_.size()); + CopyWords(sum.reg_+a.reg_.size(), b.reg_+a.reg_.size(), + b.reg_.size()-a.reg_.size()); + carry = Increment(sum.reg_+a.reg_.size(), b.reg_.size()-a.reg_.size(), + carry); + } + + if (carry) + { + sum.reg_.CleanGrow(2*sum.reg_.size()); + sum.reg_[sum.reg_.size()/2] = 1; + } + sum.sign_ = Integer::POSITIVE; +} + +void PositiveSubtract(Integer &diff, const Integer &a, const Integer& b) +{ + unsigned aSize = a.WordCount(); + aSize += aSize%2; + unsigned bSize = b.WordCount(); + bSize += bSize%2; + + if (aSize == bSize) + { + if (Compare(a.reg_.get_buffer(), b.reg_.get_buffer(), aSize) >= 0) + { + Subtract(diff.reg_.get_buffer(), a.reg_.get_buffer(), + b.reg_.get_buffer(), aSize); + diff.sign_ = Integer::POSITIVE; + } + else + { + Subtract(diff.reg_.get_buffer(), b.reg_.get_buffer(), + a.reg_.get_buffer(), aSize); + diff.sign_ = Integer::NEGATIVE; + } + } + else if (aSize > bSize) + { + word borrow = Subtract(diff.reg_.get_buffer(), a.reg_.get_buffer(), + b.reg_.get_buffer(), bSize); + CopyWords(diff.reg_+bSize, a.reg_+bSize, aSize-bSize); + borrow = Decrement(diff.reg_+bSize, aSize-bSize, borrow); + assert(!borrow); + diff.sign_ = Integer::POSITIVE; + } + else + { + word borrow = Subtract(diff.reg_.get_buffer(), b.reg_.get_buffer(), + a.reg_.get_buffer(), aSize); + CopyWords(diff.reg_+aSize, b.reg_+aSize, bSize-aSize); + borrow = Decrement(diff.reg_+aSize, bSize-aSize, borrow); + assert(!borrow); + diff.sign_ = Integer::NEGATIVE; + } +} + + +unsigned int Integer::MinEncodedSize(Signedness signedness) const +{ + unsigned int outputLen = max(1U, ByteCount()); + if (signedness == UNSIGNED) + return outputLen; + if (NotNegative() && (GetByte(outputLen-1) & 0x80)) + outputLen++; + if (IsNegative() && *this < -Power2(outputLen*8-1)) + outputLen++; + return outputLen; +} + + +int Integer::Compare(const Integer& t) const +{ + if (NotNegative()) + { + if (t.NotNegative()) + return PositiveCompare(t); + else + return 1; + } + else + { + if (t.NotNegative()) + return -1; + else + return -PositiveCompare(t); + } +} + + +int Integer::PositiveCompare(const Integer& t) const +{ + unsigned size = WordCount(), tSize = t.WordCount(); + + if (size == tSize) + return TaoCrypt::Compare(reg_.get_buffer(), t.reg_.get_buffer(), size); + else + return size > tSize ? 1 : -1; +} + + +bool Integer::GetBit(unsigned int n) const +{ + if (n/WORD_BITS >= reg_.size()) + return 0; + else + return bool((reg_[n/WORD_BITS] >> (n % WORD_BITS)) & 1); +} + + +unsigned long Integer::GetBits(unsigned int i, unsigned int n) const +{ + assert(n <= sizeof(unsigned long)*8); + unsigned long v = 0; + for (unsigned int j=0; j<n; j++) + v |= GetBit(i+j) << j; + return v; +} + + +byte Integer::GetByte(unsigned int n) const +{ + if (n/WORD_SIZE >= reg_.size()) + return 0; + else + return byte(reg_[n/WORD_SIZE] >> ((n%WORD_SIZE)*8)); +} + + +unsigned int Integer::BitCount() const +{ + unsigned wordCount = WordCount(); + if (wordCount) + return (wordCount-1)*WORD_BITS + BitPrecision(reg_[wordCount-1]); + else + return 0; +} + + +unsigned int Integer::ByteCount() const +{ + unsigned wordCount = WordCount(); + if (wordCount) + return (wordCount-1)*WORD_SIZE + BytePrecision(reg_[wordCount-1]); + else + return 0; +} + + +unsigned int Integer::WordCount() const +{ + return CountWords(reg_.get_buffer(), reg_.size()); +} + + +bool Integer::IsConvertableToLong() const +{ + if (ByteCount() > sizeof(long)) + return false; + + unsigned long value = reg_[0]; + value += SafeLeftShift<WORD_BITS, unsigned long>(reg_[1]); + + if (sign_ == POSITIVE) + return (signed long)value >= 0; + else + return -(signed long)value < 0; +} + + +signed long Integer::ConvertToLong() const +{ + assert(IsConvertableToLong()); + + unsigned long value = reg_[0]; + value += SafeLeftShift<WORD_BITS, unsigned long>(reg_[1]); + return sign_ == POSITIVE ? value : -(signed long)value; +} + + +void Integer::Swap(Integer& a) +{ + reg_.Swap(a.reg_); + STL::swap(sign_, a.sign_); +} + + +Integer Integer::Plus(const Integer& b) const +{ + Integer sum((word)0, max(reg_.size(), b.reg_.size())); + if (NotNegative()) + { + if (b.NotNegative()) + PositiveAdd(sum, *this, b); + else + PositiveSubtract(sum, *this, b); + } + else + { + if (b.NotNegative()) + PositiveSubtract(sum, b, *this); + else + { + PositiveAdd(sum, *this, b); + sum.sign_ = Integer::NEGATIVE; + } + } + return sum; +} + + +Integer Integer::Minus(const Integer& b) const +{ + Integer diff((word)0, max(reg_.size(), b.reg_.size())); + if (NotNegative()) + { + if (b.NotNegative()) + PositiveSubtract(diff, *this, b); + else + PositiveAdd(diff, *this, b); + } + else + { + if (b.NotNegative()) + { + PositiveAdd(diff, *this, b); + diff.sign_ = Integer::NEGATIVE; + } + else + PositiveSubtract(diff, b, *this); + } + return diff; +} + + +Integer Integer::Times(const Integer &b) const +{ + Integer product; + Multiply(product, *this, b); + return product; +} + + +#undef A0 +#undef A1 +#undef B0 +#undef B1 + +#undef T0 +#undef T1 +#undef T2 +#undef T3 + +#undef R0 +#undef R1 +#undef R2 +#undef R3 + + +static inline void AtomicDivide(word *Q, const word *A, const word *B) +{ + word T[4]; + DWord q = DivideFourWordsByTwo<word, DWord>(T, DWord(A[0], A[1]), + DWord(A[2], A[3]), DWord(B[0], B[1])); + Q[0] = q.GetLowHalf(); + Q[1] = q.GetHighHalf(); + +#ifndef NDEBUG + if (B[0] || B[1]) + { + // multiply quotient and divisor and add remainder, make sure it + // equals dividend + assert(!T[2] && !T[3] && (T[1] < B[1] || (T[1]==B[1] && T[0]<B[0]))); + word P[4]; + Portable::Multiply2(P, Q, B); + Add(P, P, T, 4); + assert(memcmp(P, A, 4*WORD_SIZE)==0); + } +#endif +} + + +// for use by Divide(), corrects the underestimated quotient {Q1,Q0} +static void CorrectQuotientEstimate(word *R, word *T, word *Q, const word *B, + unsigned int N) +{ + assert(N && N%2==0); + + if (Q[1]) + { + T[N] = T[N+1] = 0; + unsigned i; + for (i=0; i<N; i+=4) + LowLevel::Multiply2(T+i, Q, B+i); + for (i=2; i<N; i+=4) + if (LowLevel::Multiply2Add(T+i, Q, B+i)) + T[i+5] += (++T[i+4]==0); + } + else + { + T[N] = LinearMultiply(T, B, Q[0], N); + T[N+1] = 0; + } + + word borrow = Subtract(R, R, T, N+2); + assert(!borrow && !R[N+1]); + + while (R[N] || Compare(R, B, N) >= 0) + { + R[N] -= Subtract(R, R, B, N); + Q[1] += (++Q[0]==0); + assert(Q[0] || Q[1]); // no overflow + } +} + +// R[NB] -------- remainder = A%B +// Q[NA-NB+2] --- quotient = A/B +// T[NA+2*NB+4] - temp work space +// A[NA] -------- dividend +// B[NB] -------- divisor + + +void Divide(word* R, word* Q, word* T, const word* A, unsigned int NA, + const word* B, unsigned int NB) +{ + assert(NA && NB && NA%2==0 && NB%2==0); + assert(B[NB-1] || B[NB-2]); + assert(NB <= NA); + + // set up temporary work space + word *const TA=T; + word *const TB=T+NA+2; + word *const TP=T+NA+2+NB; + + // copy B into TB and normalize it so that TB has highest bit set to 1 + unsigned shiftWords = (B[NB-1]==0); + TB[0] = TB[NB-1] = 0; + CopyWords(TB+shiftWords, B, NB-shiftWords); + unsigned shiftBits = WORD_BITS - BitPrecision(TB[NB-1]); + assert(shiftBits < WORD_BITS); + ShiftWordsLeftByBits(TB, NB, shiftBits); + + // copy A into TA and normalize it + TA[0] = TA[NA] = TA[NA+1] = 0; + CopyWords(TA+shiftWords, A, NA); + ShiftWordsLeftByBits(TA, NA+2, shiftBits); + + if (TA[NA+1]==0 && TA[NA] <= 1) + { + Q[NA-NB+1] = Q[NA-NB] = 0; + while (TA[NA] || Compare(TA+NA-NB, TB, NB) >= 0) + { + TA[NA] -= Subtract(TA+NA-NB, TA+NA-NB, TB, NB); + ++Q[NA-NB]; + } + } + else + { + NA+=2; + assert(Compare(TA+NA-NB, TB, NB) < 0); + } + + word BT[2]; + BT[0] = TB[NB-2] + 1; + BT[1] = TB[NB-1] + (BT[0]==0); + + // start reducing TA mod TB, 2 words at a time + for (unsigned i=NA-2; i>=NB; i-=2) + { + AtomicDivide(Q+i-NB, TA+i-2, BT); + CorrectQuotientEstimate(TA+i-NB, TP, Q+i-NB, TB, NB); + } + + // copy TA into R, and denormalize it + CopyWords(R, TA+shiftWords, NB); + ShiftWordsRightByBits(R, NB, shiftBits); +} + + +void PositiveDivide(Integer& remainder, Integer& quotient, + const Integer& a, const Integer& b) +{ + unsigned aSize = a.WordCount(); + unsigned bSize = b.WordCount(); + + assert(bSize); + + if (a.PositiveCompare(b) == -1) + { + remainder = a; + remainder.sign_ = Integer::POSITIVE; + quotient = Integer::Zero(); + return; + } + + aSize += aSize%2; // round up to next even number + bSize += bSize%2; + + remainder.reg_.CleanNew(RoundupSize(bSize)); + remainder.sign_ = Integer::POSITIVE; + quotient.reg_.CleanNew(RoundupSize(aSize-bSize+2)); + quotient.sign_ = Integer::POSITIVE; + + AlignedWordBlock T(aSize+2*bSize+4); + Divide(remainder.reg_.get_buffer(), quotient.reg_.get_buffer(), + T.get_buffer(), a.reg_.get_buffer(), aSize, b.reg_.get_buffer(), + bSize); +} + +void Integer::Divide(Integer &remainder, Integer "ient, + const Integer ÷nd, const Integer &divisor) +{ + PositiveDivide(remainder, quotient, dividend, divisor); + + if (dividend.IsNegative()) + { + quotient.Negate(); + if (remainder.NotZero()) + { + --quotient; + remainder = divisor.AbsoluteValue() - remainder; + } + } + + if (divisor.IsNegative()) + quotient.Negate(); +} + +void Integer::DivideByPowerOf2(Integer &r, Integer &q, const Integer &a, + unsigned int n) +{ + q = a; + q >>= n; + + const unsigned int wordCount = BitsToWords(n); + if (wordCount <= a.WordCount()) + { + r.reg_.resize(RoundupSize(wordCount)); + CopyWords(r.reg_.get_buffer(), a.reg_.get_buffer(), wordCount); + SetWords(r.reg_+wordCount, 0, r.reg_.size()-wordCount); + if (n % WORD_BITS != 0) + r.reg_[wordCount-1] %= (word(1) << (n % WORD_BITS)); + } + else + { + r.reg_.resize(RoundupSize(a.WordCount())); + CopyWords(r.reg_.get_buffer(), a.reg_.get_buffer(), r.reg_.size()); + } + r.sign_ = POSITIVE; + + if (a.IsNegative() && r.NotZero()) + { + --q; + r = Power2(n) - r; + } +} + +Integer Integer::DividedBy(const Integer &b) const +{ + Integer remainder, quotient; + Integer::Divide(remainder, quotient, *this, b); + return quotient; +} + +Integer Integer::Modulo(const Integer &b) const +{ + Integer remainder, quotient; + Integer::Divide(remainder, quotient, *this, b); + return remainder; +} + +void Integer::Divide(word &remainder, Integer "ient, + const Integer ÷nd, word divisor) +{ + assert(divisor); + + if ((divisor & (divisor-1)) == 0) // divisor is a power of 2 + { + quotient = dividend >> (BitPrecision(divisor)-1); + remainder = dividend.reg_[0] & (divisor-1); + return; + } + + unsigned int i = dividend.WordCount(); + quotient.reg_.CleanNew(RoundupSize(i)); + remainder = 0; + while (i--) + { + quotient.reg_[i] = DWord(dividend.reg_[i], remainder) / divisor; + remainder = DWord(dividend.reg_[i], remainder) % divisor; + } + + if (dividend.NotNegative()) + quotient.sign_ = POSITIVE; + else + { + quotient.sign_ = NEGATIVE; + if (remainder) + { + --quotient; + remainder = divisor - remainder; + } + } +} + +Integer Integer::DividedBy(word b) const +{ + word remainder; + Integer quotient; + Integer::Divide(remainder, quotient, *this, b); + return quotient; +} + +word Integer::Modulo(word divisor) const +{ + assert(divisor); + + word remainder; + + if ((divisor & (divisor-1)) == 0) // divisor is a power of 2 + remainder = reg_[0] & (divisor-1); + else + { + unsigned int i = WordCount(); + + if (divisor <= 5) + { + DWord sum(0, 0); + while (i--) + sum += reg_[i]; + remainder = sum % divisor; + } + else + { + remainder = 0; + while (i--) + remainder = DWord(reg_[i], remainder) % divisor; + } + } + + if (IsNegative() && remainder) + remainder = divisor - remainder; + + return remainder; +} + + +Integer Integer::AbsoluteValue() const +{ + Integer result(*this); + result.sign_ = POSITIVE; + return result; +} + + +Integer Integer::SquareRoot() const +{ + if (!IsPositive()) + return Zero(); + + // overestimate square root + Integer x, y = Power2((BitCount()+1)/2); + assert(y*y >= *this); + + do + { + x = y; + y = (x + *this/x) >> 1; + } while (y<x); + + return x; +} + +bool Integer::IsSquare() const +{ + Integer r = SquareRoot(); + return *this == r.Squared(); +} + +bool Integer::IsUnit() const +{ + return (WordCount() == 1) && (reg_[0] == 1); +} + +Integer Integer::MultiplicativeInverse() const +{ + return IsUnit() ? *this : Zero(); +} + +Integer a_times_b_mod_c(const Integer &x, const Integer& y, const Integer& m) +{ + return x*y%m; +} + +Integer a_exp_b_mod_c(const Integer &x, const Integer& e, const Integer& m) +{ + ModularArithmetic mr(m); + return mr.Exponentiate(x, e); +} + +Integer Integer::Gcd(const Integer &a, const Integer &b) +{ + return EuclideanDomainOf().Gcd(a, b); +} + +Integer Integer::InverseMod(const Integer &m) const +{ + assert(m.NotNegative()); + + if (IsNegative() || *this>=m) + return (*this%m).InverseMod(m); + + if (m.IsEven()) + { + if (!m || IsEven()) + return Zero(); // no inverse + if (*this == One()) + return One(); + + Integer u = m.InverseMod(*this); + return !u ? Zero() : (m*(*this-u)+1)/(*this); + } + + AlignedWordBlock T(m.reg_.size() * 4); + Integer r((word)0, m.reg_.size()); + unsigned k = AlmostInverse(r.reg_.get_buffer(), T.get_buffer(), + reg_.get_buffer(), reg_.size(), + m.reg_.get_buffer(), m.reg_.size()); + DivideByPower2Mod(r.reg_.get_buffer(), r.reg_.get_buffer(), k, + m.reg_.get_buffer(), m.reg_.size()); + return r; +} + +word Integer::InverseMod(const word mod) const +{ + word g0 = mod, g1 = *this % mod; + word v0 = 0, v1 = 1; + word y; + + while (g1) + { + if (g1 == 1) + return v1; + y = g0 / g1; + g0 = g0 % g1; + v0 += y * v1; + + if (!g0) + break; + if (g0 == 1) + return mod-v0; + y = g1 / g0; + g1 = g1 % g0; + v1 += y * v0; + } + return 0; +} + +// ********* ModArith stuff + +const Integer& ModularArithmetic::Half(const Integer &a) const +{ + if (a.reg_.size()==modulus.reg_.size()) + { + TaoCrypt::DivideByPower2Mod(result.reg_.begin(), a.reg_.begin(), 1, + modulus.reg_.begin(), a.reg_.size()); + return result; + } + else + return result1 = (a.IsEven() ? (a >> 1) : ((a+modulus) >> 1)); +} + +const Integer& ModularArithmetic::Add(const Integer &a, const Integer &b) const +{ + if (a.reg_.size()==modulus.reg_.size() && + b.reg_.size()==modulus.reg_.size()) + { + if (TaoCrypt::Add(result.reg_.begin(), a.reg_.begin(), b.reg_.begin(), + a.reg_.size()) + || Compare(result.reg_.get_buffer(), modulus.reg_.get_buffer(), + a.reg_.size()) >= 0) + { + TaoCrypt::Subtract(result.reg_.begin(), result.reg_.begin(), + modulus.reg_.begin(), a.reg_.size()); + } + return result; + } + else + { + result1 = a+b; + if (result1 >= modulus) + result1 -= modulus; + return result1; + } +} + +Integer& ModularArithmetic::Accumulate(Integer &a, const Integer &b) const +{ + if (a.reg_.size()==modulus.reg_.size() && + b.reg_.size()==modulus.reg_.size()) + { + if (TaoCrypt::Add(a.reg_.get_buffer(), a.reg_.get_buffer(), + b.reg_.get_buffer(), a.reg_.size()) + || Compare(a.reg_.get_buffer(), modulus.reg_.get_buffer(), + a.reg_.size()) >= 0) + { + TaoCrypt::Subtract(a.reg_.get_buffer(), a.reg_.get_buffer(), + modulus.reg_.get_buffer(), a.reg_.size()); + } + } + else + { + a+=b; + if (a>=modulus) + a-=modulus; + } + + return a; +} + +const Integer& ModularArithmetic::Subtract(const Integer &a, + const Integer &b) const +{ + if (a.reg_.size()==modulus.reg_.size() && + b.reg_.size()==modulus.reg_.size()) + { + if (TaoCrypt::Subtract(result.reg_.begin(), a.reg_.begin(), + b.reg_.begin(), a.reg_.size())) + TaoCrypt::Add(result.reg_.begin(), result.reg_.begin(), + modulus.reg_.begin(), a.reg_.size()); + return result; + } + else + { + result1 = a-b; + if (result1.IsNegative()) + result1 += modulus; + return result1; + } +} + +Integer& ModularArithmetic::Reduce(Integer &a, const Integer &b) const +{ + if (a.reg_.size()==modulus.reg_.size() && + b.reg_.size()==modulus.reg_.size()) + { + if (TaoCrypt::Subtract(a.reg_.get_buffer(), a.reg_.get_buffer(), + b.reg_.get_buffer(), a.reg_.size())) + TaoCrypt::Add(a.reg_.get_buffer(), a.reg_.get_buffer(), + modulus.reg_.get_buffer(), a.reg_.size()); + } + else + { + a-=b; + if (a.IsNegative()) + a+=modulus; + } + + return a; +} + +const Integer& ModularArithmetic::Inverse(const Integer &a) const +{ + if (!a) + return a; + + CopyWords(result.reg_.begin(), modulus.reg_.begin(), modulus.reg_.size()); + if (TaoCrypt::Subtract(result.reg_.begin(), result.reg_.begin(), + a.reg_.begin(), a.reg_.size())) + Decrement(result.reg_.begin()+a.reg_.size(), 1, + modulus.reg_.size()-a.reg_.size()); + + return result; +} + +Integer ModularArithmetic::CascadeExponentiate(const Integer &x, + const Integer &e1, const Integer &y, const Integer &e2) const +{ + if (modulus.IsOdd()) + { + MontgomeryRepresentation dr(modulus); + return dr.ConvertOut(dr.CascadeExponentiate(dr.ConvertIn(x), e1, + dr.ConvertIn(y), e2)); + } + else + return AbstractRing::CascadeExponentiate(x, e1, y, e2); +} + +void ModularArithmetic::SimultaneousExponentiate(Integer *results, + const Integer &base, const Integer *exponents, + unsigned int exponentsCount) const +{ + if (modulus.IsOdd()) + { + MontgomeryRepresentation dr(modulus); + dr.SimultaneousExponentiate(results, dr.ConvertIn(base), exponents, + exponentsCount); + for (unsigned int i=0; i<exponentsCount; i++) + results[i] = dr.ConvertOut(results[i]); + } + else + AbstractRing::SimultaneousExponentiate(results, base, + exponents, exponentsCount); +} + + +// ******************************************************** + +#define A0 A +#define A1 (A+N2) +#define B0 B +#define B1 (B+N2) + +#define T0 T +#define T1 (T+N2) +#define T2 (T+N) +#define T3 (T+N+N2) + +#define R0 R +#define R1 (R+N2) +#define R2 (R+N) +#define R3 (R+N+N2) + + +inline void MultiplyBottom(word *R, word *T, const word *A, const word *B, + unsigned int N) +{ + RecursiveMultiplyBottom(R, T, A, B, N); +} + +inline void MultiplyTop(word *R, word *T, const word *L, const word *A, + const word *B, unsigned int N) +{ + RecursiveMultiplyTop(R, T, L, A, B, N); +} + + +// R[N] --- result = X/(2**(WORD_BITS*N)) mod M +// T[3*N] - temporary work space +// X[2*N] - number to be reduced +// M[N] --- modulus +// U[N] --- multiplicative inverse of M mod 2**(WORD_BITS*N) + +void MontgomeryReduce(word *R, word *T, const word *X, const word *M, + const word *U, unsigned int N) +{ + MultiplyBottom(R, T, X, U, N); + MultiplyTop(T, T+N, X, R, M, N); + word borrow = Subtract(T, X+N, T, N); + // defend against timing attack by doing this Add even when not needed + word carry = Add(T+N, T, M, N); + assert(carry || !borrow); + CopyWords(R, T + (borrow ? N : 0), N); +} + +// R[N] ----- result = A inverse mod 2**(WORD_BITS*N) +// T[3*N/2] - temporary work space +// A[N] ----- an odd number as input + +void RecursiveInverseModPower2(word *R, word *T, const word *A, unsigned int N) +{ + if (N==2) + { + T[0] = AtomicInverseModPower2(A[0]); + T[1] = 0; + LowLevel::Multiply2Bottom(T+2, T, A); + TwosComplement(T+2, 2); + Increment(T+2, 2, 2); + LowLevel::Multiply2Bottom(R, T, T+2); + } + else + { + const unsigned int N2 = N/2; + RecursiveInverseModPower2(R0, T0, A0, N2); + T0[0] = 1; + SetWords(T0+1, 0, N2-1); + MultiplyTop(R1, T1, T0, R0, A0, N2); + MultiplyBottom(T0, T1, R0, A1, N2); + Add(T0, R1, T0, N2); + TwosComplement(T0, N2); + MultiplyBottom(R1, T1, R0, T0, N2); + } +} + + +#undef A0 +#undef A1 +#undef B0 +#undef B1 + +#undef T0 +#undef T1 +#undef T2 +#undef T3 + +#undef R0 +#undef R1 +#undef R2 +#undef R3 + + +// modulus must be odd +MontgomeryRepresentation::MontgomeryRepresentation(const Integer &m) + : ModularArithmetic(m), + u((word)0, modulus.reg_.size()), + workspace(5*modulus.reg_.size()) +{ + assert(modulus.IsOdd()); + RecursiveInverseModPower2(u.reg_.get_buffer(), workspace.get_buffer(), + modulus.reg_.get_buffer(), modulus.reg_.size()); +} + +const Integer& MontgomeryRepresentation::Multiply(const Integer &a, + const Integer &b) const +{ + word *const T = workspace.begin(); + word *const R = result.reg_.begin(); + const unsigned int N = modulus.reg_.size(); + assert(a.reg_.size()<=N && b.reg_.size()<=N); + + AsymmetricMultiply(T, T+2*N, a.reg_.get_buffer(), a.reg_.size(), + b.reg_.get_buffer(), b.reg_.size()); + SetWords(T+a.reg_.size()+b.reg_.size(),0, 2*N-a.reg_.size()-b.reg_.size()); + MontgomeryReduce(R, T+2*N, T, modulus.reg_.get_buffer(), + u.reg_.get_buffer(), N); + return result; +} + +const Integer& MontgomeryRepresentation::Square(const Integer &a) const +{ + word *const T = workspace.begin(); + word *const R = result.reg_.begin(); + const unsigned int N = modulus.reg_.size(); + assert(a.reg_.size()<=N); + + TaoCrypt::Square(T, T+2*N, a.reg_.get_buffer(), a.reg_.size()); + SetWords(T+2*a.reg_.size(), 0, 2*N-2*a.reg_.size()); + MontgomeryReduce(R, T+2*N, T, modulus.reg_.get_buffer(), + u.reg_.get_buffer(), N); + return result; +} + +Integer MontgomeryRepresentation::ConvertOut(const Integer &a) const +{ + word *const T = workspace.begin(); + word *const R = result.reg_.begin(); + const unsigned int N = modulus.reg_.size(); + assert(a.reg_.size()<=N); + + CopyWords(T, a.reg_.get_buffer(), a.reg_.size()); + SetWords(T+a.reg_.size(), 0, 2*N-a.reg_.size()); + MontgomeryReduce(R, T+2*N, T, modulus.reg_.get_buffer(), + u.reg_.get_buffer(), N); + return result; +} + +const Integer& MontgomeryRepresentation::MultiplicativeInverse( + const Integer &a) const +{ +// return (EuclideanMultiplicativeInverse(a, modulus)<< +// (2*WORD_BITS*modulus.reg_.size()))%modulus; + word *const T = workspace.begin(); + word *const R = result.reg_.begin(); + const unsigned int N = modulus.reg_.size(); + assert(a.reg_.size()<=N); + + CopyWords(T, a.reg_.get_buffer(), a.reg_.size()); + SetWords(T+a.reg_.size(), 0, 2*N-a.reg_.size()); + MontgomeryReduce(R, T+2*N, T, modulus.reg_.get_buffer(), + u.reg_.get_buffer(), N); + unsigned k = AlmostInverse(R, T, R, N, modulus.reg_.get_buffer(), N); + +// cout << "k=" << k << " N*32=" << 32*N << endl; + + if (k>N*WORD_BITS) + DivideByPower2Mod(R, R, k-N*WORD_BITS, modulus.reg_.get_buffer(), N); + else + MultiplyByPower2Mod(R, R, N*WORD_BITS-k, modulus.reg_.get_buffer(), N); + + return result; +} + + +// mod Root stuff +Integer ModularRoot(const Integer &a, const Integer &dp, const Integer &dq, + const Integer &p, const Integer &q, const Integer &u) +{ + Integer p2 = ModularExponentiation((a % p), dp, p); + Integer q2 = ModularExponentiation((a % q), dq, q); + return CRT(p2, p, q2, q, u); +} + +Integer CRT(const Integer &xp, const Integer &p, const Integer &xq, + const Integer &q, const Integer &u) +{ + // isn't operator overloading great? + return p * (u * (xq-xp) % q) + xp; +} + + +#ifdef HAVE_EXPLICIT_TEMPLATE_INSTANTIATION +#ifndef TAOCRYPT_NATIVE_DWORD_AVAILABLE +template hword DivideThreeWordsByTwo<hword, Word>(hword*, hword, hword, Word*); +#endif +template word DivideThreeWordsByTwo<word, DWord>(word*, word, word, DWord*); +#ifdef SSE2_INTRINSICS_AVAILABLE +template class AlignedAllocator<word>; +#endif +#endif + + +} // namespace + diff --git a/externals/mysql/extlib/yassl/taocrypt/src/md2.cpp b/externals/mysql/extlib/yassl/taocrypt/src/md2.cpp new file mode 100644 index 00000000000..b835e245718 --- /dev/null +++ b/externals/mysql/extlib/yassl/taocrypt/src/md2.cpp @@ -0,0 +1,125 @@ +/* + 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 md2.cpp from CryptoPP */ + +#include "runtime.hpp" +#include "md2.hpp" +#include <string.h> + +namespace TaoCrypt { + + +MD2::MD2() + : X_(X_SIZE), C_(BLOCK_SIZE), buffer_(BLOCK_SIZE) +{ + Init(); +} + +void MD2::Init() +{ + memset(X_.get_buffer(), 0, X_SIZE); + memset(C_.get_buffer(), 0, BLOCK_SIZE); + memset(buffer_.get_buffer(), 0, BLOCK_SIZE); + count_ = 0; +} + + +void MD2::Update(const byte* data, word32 len) +{ + + static const byte S[256] = + { + 41, 46, 67, 201, 162, 216, 124, 1, 61, 54, 84, 161, 236, 240, 6, + 19, 98, 167, 5, 243, 192, 199, 115, 140, 152, 147, 43, 217, 188, + 76, 130, 202, 30, 155, 87, 60, 253, 212, 224, 22, 103, 66, 111, 24, + 138, 23, 229, 18, 190, 78, 196, 214, 218, 158, 222, 73, 160, 251, + 245, 142, 187, 47, 238, 122, 169, 104, 121, 145, 21, 178, 7, 63, + 148, 194, 16, 137, 11, 34, 95, 33, 128, 127, 93, 154, 90, 144, 50, + 39, 53, 62, 204, 231, 191, 247, 151, 3, 255, 25, 48, 179, 72, 165, + 181, 209, 215, 94, 146, 42, 172, 86, 170, 198, 79, 184, 56, 210, + 150, 164, 125, 182, 118, 252, 107, 226, 156, 116, 4, 241, 69, 157, + 112, 89, 100, 113, 135, 32, 134, 91, 207, 101, 230, 45, 168, 2, 27, + 96, 37, 173, 174, 176, 185, 246, 28, 70, 97, 105, 52, 64, 126, 15, + 85, 71, 163, 35, 221, 81, 175, 58, 195, 92, 249, 206, 186, 197, + 234, 38, 44, 83, 13, 110, 133, 40, 132, 9, 211, 223, 205, 244, 65, + 129, 77, 82, 106, 220, 55, 200, 108, 193, 171, 250, 36, 225, 123, + 8, 12, 189, 177, 74, 120, 136, 149, 139, 227, 99, 232, 109, 233, + 203, 213, 254, 59, 0, 29, 57, 242, 239, 183, 14, 102, 88, 208, 228, + 166, 119, 114, 248, 235, 117, 75, 10, 49, 68, 80, 180, 143, 237, + 31, 26, 219, 153, 141, 51, 159, 17, 131, 20 + }; + + while (len) { + word32 L = (PAD_SIZE - count_) < len ? (PAD_SIZE - count_) : len; + memcpy(buffer_.get_buffer() + count_, data, L); + count_ += L; + data += L; + len -= L; + + if (count_==PAD_SIZE) { + count_ = 0; + memcpy(X_.get_buffer() + PAD_SIZE, buffer_.get_buffer(), PAD_SIZE); + byte t = C_[15]; + + int i; + for(i = 0; i < PAD_SIZE; i++) { + X_[32 + i] = X_[PAD_SIZE + i] ^ X_[i]; + t = C_[i] ^= S[buffer_[i] ^ t]; + } + + t=0; + for(i = 0; i < 18; i++) { + for(int j = 0; j < X_SIZE; j += 8) { + t = X_[j+0] ^= S[t]; + t = X_[j+1] ^= S[t]; + t = X_[j+2] ^= S[t]; + t = X_[j+3] ^= S[t]; + t = X_[j+4] ^= S[t]; + t = X_[j+5] ^= S[t]; + t = X_[j+6] ^= S[t]; + t = X_[j+7] ^= S[t]; + } + t = (t + i) & 0xFF; + } + } + } +} + + +void MD2::Final(byte *hash) +{ + byte padding[BLOCK_SIZE]; + word32 padLen = PAD_SIZE - count_; + + for (word32 i = 0; i < padLen; i++) + padding[i] = static_cast<byte>(padLen); + + Update(padding, padLen); + Update(C_.get_buffer(), BLOCK_SIZE); + + memcpy(hash, X_.get_buffer(), DIGEST_SIZE); + + Init(); +} + + + + +} // namespace diff --git a/externals/mysql/extlib/yassl/taocrypt/src/md4.cpp b/externals/mysql/extlib/yassl/taocrypt/src/md4.cpp new file mode 100644 index 00000000000..cf17c218809 --- /dev/null +++ b/externals/mysql/extlib/yassl/taocrypt/src/md4.cpp @@ -0,0 +1,157 @@ +/* + 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 md4.cpp from CryptoPP */ + +#include "runtime.hpp" +#include "md4.hpp" +#ifdef USE_SYS_STL + #include <algorithm> +#else + #include "algorithm.hpp" +#endif + + +namespace STL = STL_NAMESPACE; + + +namespace TaoCrypt { + +void MD4::Init() +{ + digest_[0] = 0x67452301L; + digest_[1] = 0xefcdab89L; + digest_[2] = 0x98badcfeL; + digest_[3] = 0x10325476L; + + buffLen_ = 0; + loLen_ = 0; + hiLen_ = 0; +} + + +MD4::MD4(const MD4& that) : HASHwithTransform(DIGEST_SIZE / sizeof(word32), + BLOCK_SIZE) +{ + buffLen_ = that.buffLen_; + loLen_ = that.loLen_; + hiLen_ = that.hiLen_; + + memcpy(digest_, that.digest_, DIGEST_SIZE); + memcpy(buffer_, that.buffer_, BLOCK_SIZE); +} + +MD4& MD4::operator= (const MD4& that) +{ + MD4 tmp(that); + Swap(tmp); + + return *this; +} + + +void MD4::Swap(MD4& other) +{ + STL::swap(loLen_, other.loLen_); + STL::swap(hiLen_, other.hiLen_); + STL::swap(buffLen_, other.buffLen_); + + memcpy(digest_, other.digest_, DIGEST_SIZE); + memcpy(buffer_, other.buffer_, BLOCK_SIZE); +} + + +void MD4::Transform() +{ +#define F(x, y, z) ((z) ^ ((x) & ((y) ^ (z)))) +#define G(x, y, z) (((x) & (y)) | ((x) & (z)) | ((y) & (z))) +#define H(x, y, z) ((x) ^ (y) ^ (z)) + + word32 A, B, C, D; + + A = digest_[0]; + B = digest_[1]; + C = digest_[2]; + D = digest_[3]; + +#define function(a,b,c,d,k,s) a=rotlFixed(a+F(b,c,d)+buffer_[k],s); + function(A,B,C,D, 0, 3); + function(D,A,B,C, 1, 7); + function(C,D,A,B, 2,11); + function(B,C,D,A, 3,19); + function(A,B,C,D, 4, 3); + function(D,A,B,C, 5, 7); + function(C,D,A,B, 6,11); + function(B,C,D,A, 7,19); + function(A,B,C,D, 8, 3); + function(D,A,B,C, 9, 7); + function(C,D,A,B,10,11); + function(B,C,D,A,11,19); + function(A,B,C,D,12, 3); + function(D,A,B,C,13, 7); + function(C,D,A,B,14,11); + function(B,C,D,A,15,19); + +#undef function +#define function(a,b,c,d,k,s) a=rotlFixed(a+G(b,c,d)+buffer_[k]+0x5a827999,s); + function(A,B,C,D, 0, 3); + function(D,A,B,C, 4, 5); + function(C,D,A,B, 8, 9); + function(B,C,D,A,12,13); + function(A,B,C,D, 1, 3); + function(D,A,B,C, 5, 5); + function(C,D,A,B, 9, 9); + function(B,C,D,A,13,13); + function(A,B,C,D, 2, 3); + function(D,A,B,C, 6, 5); + function(C,D,A,B,10, 9); + function(B,C,D,A,14,13); + function(A,B,C,D, 3, 3); + function(D,A,B,C, 7, 5); + function(C,D,A,B,11, 9); + function(B,C,D,A,15,13); + +#undef function +#define function(a,b,c,d,k,s) a=rotlFixed(a+H(b,c,d)+buffer_[k]+0x6ed9eba1,s); + function(A,B,C,D, 0, 3); + function(D,A,B,C, 8, 9); + function(C,D,A,B, 4,11); + function(B,C,D,A,12,15); + function(A,B,C,D, 2, 3); + function(D,A,B,C,10, 9); + function(C,D,A,B, 6,11); + function(B,C,D,A,14,15); + function(A,B,C,D, 1, 3); + function(D,A,B,C, 9, 9); + function(C,D,A,B, 5,11); + function(B,C,D,A,13,15); + function(A,B,C,D, 3, 3); + function(D,A,B,C,11, 9); + function(C,D,A,B, 7,11); + function(B,C,D,A,15,15); + + digest_[0] += A; + digest_[1] += B; + digest_[2] += C; + digest_[3] += D; +} + + +} // namespace + diff --git a/externals/mysql/extlib/yassl/taocrypt/src/md5.cpp b/externals/mysql/extlib/yassl/taocrypt/src/md5.cpp new file mode 100644 index 00000000000..f18e0290c90 --- /dev/null +++ b/externals/mysql/extlib/yassl/taocrypt/src/md5.cpp @@ -0,0 +1,498 @@ +/* + 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 md5.cpp from CryptoPP */ + +#include "runtime.hpp" +#include "md5.hpp" +#ifdef USE_SYS_STL + #include <algorithm> +#else + #include "algorithm.hpp" +#endif + + +namespace STL = STL_NAMESPACE; + + + +namespace TaoCrypt { + +void MD5::Init() +{ + digest_[0] = 0x67452301L; + digest_[1] = 0xefcdab89L; + digest_[2] = 0x98badcfeL; + digest_[3] = 0x10325476L; + + buffLen_ = 0; + loLen_ = 0; + hiLen_ = 0; +} + + +MD5::MD5(const MD5& that) : HASHwithTransform(DIGEST_SIZE / sizeof(word32), + BLOCK_SIZE) +{ + buffLen_ = that.buffLen_; + loLen_ = that.loLen_; + hiLen_ = that.hiLen_; + + memcpy(digest_, that.digest_, DIGEST_SIZE); + memcpy(buffer_, that.buffer_, BLOCK_SIZE); +} + +MD5& MD5::operator= (const MD5& that) +{ + MD5 tmp(that); + Swap(tmp); + + return *this; +} + + +void MD5::Swap(MD5& other) +{ + STL::swap(loLen_, other.loLen_); + STL::swap(hiLen_, other.hiLen_); + STL::swap(buffLen_, other.buffLen_); + + memcpy(digest_, other.digest_, DIGEST_SIZE); + memcpy(buffer_, other.buffer_, BLOCK_SIZE); +} + + +#ifdef DO_MD5_ASM + +// Update digest with data of size len +void MD5::Update(const byte* data, word32 len) +{ + if (!isMMX) { + HASHwithTransform::Update(data, len); + return; + } + + byte* local = reinterpret_cast<byte*>(buffer_); + + // remove buffered data if possible + if (buffLen_) { + word32 add = min(len, BLOCK_SIZE - buffLen_); + memcpy(&local[buffLen_], data, add); + + buffLen_ += add; + data += add; + len -= add; + + if (buffLen_ == BLOCK_SIZE) { + Transform(); + AddLength(BLOCK_SIZE); + buffLen_ = 0; + } + } + + // at once for asm + if (buffLen_ == 0) { + word32 times = len / BLOCK_SIZE; + if (times) { + AsmTransform(data, times); + const word32 add = BLOCK_SIZE * times; + AddLength(add); + len -= add; + data += add; + } + } + + // cache any data left + if (len) { + memcpy(&local[buffLen_], data, len); + buffLen_ += len; + } +} + + + + +/* + // w = rotlFixed(w + f(x, y, z) + index[edi] + data, s) + x +#define ASMMD5STEP(f, w, x, y, z, index, data, s) \ + f(x, y, z) \ + AS2( mov ebp, [edi + index * 4] ) \ + AS2( lea w, [esi + w + data] ) \ + AS2( add w, ebp ) \ + AS2( rol w, s ) \ + AS2( add w, x ) + + + // F1(x, y, z) (z ^ (x & (y ^ z))) + // place in esi +#define ASMF1(x, y, z) \ + AS2( mov esi, y ) \ + AS2( xor esi, z ) \ + AS2( and esi, x ) \ + AS2( xor esi, z ) + + +#define ASMF2(x, y, z) ASMF1(z, x, y) + + + // F3(x ^ y ^ z) + // place in esi +#define ASMF3(x, y, z) \ + AS2( mov esi, x ) \ + AS2( xor esi, y ) \ + AS2( xor esi, z ) + + + + // F4(x, y, z) (y ^ (x | ~z)) + // place in esi +#define ASMF4(x, y, z) \ + AS2( mov esi, z ) \ + AS1( not esi ) \ + AS2( or esi, x ) \ + AS2( xor esi, y ) +*/ + + + // combine above ASMMD5STEP(f w/ each f ASMF1 - F4 + + // esi already set up, after using set for next round + // ebp already set up, set up using next round index + +#define MD5STEP1(w, x, y, z, index, data, s) \ + AS2( xor esi, z ) \ + AS2( and esi, x ) \ + AS2( lea w, [ebp + w + data] ) \ + AS2( xor esi, z ) \ + AS2( add w, esi ) \ + AS2( mov esi, x ) \ + AS2( rol w, s ) \ + AS2( mov ebp, [edi + index * 4] ) \ + AS2( add w, x ) + +#define MD5STEP2(w, x, y, z, index, data, s) \ + AS2( xor esi, x ) \ + AS2( and esi, z ) \ + AS2( lea w, [ebp + w + data] ) \ + AS2( xor esi, y ) \ + AS2( add w, esi ) \ + AS2( mov esi, x ) \ + AS2( rol w, s ) \ + AS2( mov ebp, [edi + index * 4] ) \ + AS2( add w, x ) + + +#define MD5STEP3(w, x, y, z, index, data, s) \ + AS2( xor esi, z ) \ + AS2( lea w, [ebp + w + data] ) \ + AS2( xor esi, x ) \ + AS2( add w, esi ) \ + AS2( mov esi, x ) \ + AS2( rol w, s ) \ + AS2( mov ebp, [edi + index * 4] ) \ + AS2( add w, x ) + + +#define MD5STEP4(w, x, y, z, index, data, s) \ + AS2( or esi, x ) \ + AS2( lea w, [ebp + w + data] ) \ + AS2( xor esi, y ) \ + AS2( add w, esi ) \ + AS2( mov esi, y ) \ + AS2( rol w, s ) \ + AS1( not esi ) \ + AS2( mov ebp, [edi + index * 4] ) \ + AS2( add w, x ) + + + +#ifdef _MSC_VER + __declspec(naked) +#endif +void MD5::AsmTransform(const byte* data, word32 times) +{ +#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 eax, DWORD PTR [ebp + 16] ) + + #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 eax, DWORD PTR [ebp + 12] ) + + #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 8 ) + +#endif + + + PROLOG() + + AS2( mov esi, ecx ) + + #ifdef OLD_GCC_OFFSET + AS2( add esi, 20 ) // digest_[0] + #else + AS2( add esi, 16 ) // digest_[0] + #endif + + AS2( movd mm2, eax ) // store times_ + AS2( movd mm1, esi ) // store digest_ + + AS2( mov eax, [esi] ) // a + AS2( mov ebx, [esi + 4] ) // b + AS2( mov ecx, [esi + 8] ) // c + AS2( mov edx, [esi + 12] ) // d + +AS1(loopStart:) + + // set up + AS2( mov esi, ecx ) + AS2( mov ebp, [edi] ) + + MD5STEP1( eax, ebx, ecx, edx, 1, 0xd76aa478, 7) + MD5STEP1( edx, eax, ebx, ecx, 2, 0xe8c7b756, 12) + MD5STEP1( ecx, edx, eax, ebx, 3, 0x242070db, 17) + MD5STEP1( ebx, ecx, edx, eax, 4, 0xc1bdceee, 22) + MD5STEP1( eax, ebx, ecx, edx, 5, 0xf57c0faf, 7) + MD5STEP1( edx, eax, ebx, ecx, 6, 0x4787c62a, 12) + MD5STEP1( ecx, edx, eax, ebx, 7, 0xa8304613, 17) + MD5STEP1( ebx, ecx, edx, eax, 8, 0xfd469501, 22) + MD5STEP1( eax, ebx, ecx, edx, 9, 0x698098d8, 7) + MD5STEP1( edx, eax, ebx, ecx, 10, 0x8b44f7af, 12) + MD5STEP1( ecx, edx, eax, ebx, 11, 0xffff5bb1, 17) + MD5STEP1( ebx, ecx, edx, eax, 12, 0x895cd7be, 22) + MD5STEP1( eax, ebx, ecx, edx, 13, 0x6b901122, 7) + MD5STEP1( edx, eax, ebx, ecx, 14, 0xfd987193, 12) + MD5STEP1( ecx, edx, eax, ebx, 15, 0xa679438e, 17) + MD5STEP1( ebx, ecx, edx, eax, 1, 0x49b40821, 22) + + MD5STEP2( eax, ebx, ecx, edx, 6, 0xf61e2562, 5) + MD5STEP2( edx, eax, ebx, ecx, 11, 0xc040b340, 9) + MD5STEP2( ecx, edx, eax, ebx, 0, 0x265e5a51, 14) + MD5STEP2( ebx, ecx, edx, eax, 5, 0xe9b6c7aa, 20) + MD5STEP2( eax, ebx, ecx, edx, 10, 0xd62f105d, 5) + MD5STEP2( edx, eax, ebx, ecx, 15, 0x02441453, 9) + MD5STEP2( ecx, edx, eax, ebx, 4, 0xd8a1e681, 14) + MD5STEP2( ebx, ecx, edx, eax, 9, 0xe7d3fbc8, 20) + MD5STEP2( eax, ebx, ecx, edx, 14, 0x21e1cde6, 5) + MD5STEP2( edx, eax, ebx, ecx, 3, 0xc33707d6, 9) + MD5STEP2( ecx, edx, eax, ebx, 8, 0xf4d50d87, 14) + MD5STEP2( ebx, ecx, edx, eax, 13, 0x455a14ed, 20) + MD5STEP2( eax, ebx, ecx, edx, 2, 0xa9e3e905, 5) + MD5STEP2( edx, eax, ebx, ecx, 7, 0xfcefa3f8, 9) + MD5STEP2( ecx, edx, eax, ebx, 12, 0x676f02d9, 14) + MD5STEP2( ebx, ecx, edx, eax, 5, 0x8d2a4c8a, 20) + + MD5STEP3( eax, ebx, ecx, edx, 8, 0xfffa3942, 4) + MD5STEP3( edx, eax, ebx, ecx, 11, 0x8771f681, 11) + MD5STEP3( ecx, edx, eax, ebx, 14, 0x6d9d6122, 16) + MD5STEP3( ebx, ecx, edx, eax, 1, 0xfde5380c, 23) + MD5STEP3( eax, ebx, ecx, edx, 4, 0xa4beea44, 4) + MD5STEP3( edx, eax, ebx, ecx, 7, 0x4bdecfa9, 11) + MD5STEP3( ecx, edx, eax, ebx, 10, 0xf6bb4b60, 16) + MD5STEP3( ebx, ecx, edx, eax, 13, 0xbebfbc70, 23) + MD5STEP3( eax, ebx, ecx, edx, 0, 0x289b7ec6, 4) + MD5STEP3( edx, eax, ebx, ecx, 3, 0xeaa127fa, 11) + MD5STEP3( ecx, edx, eax, ebx, 6, 0xd4ef3085, 16) + MD5STEP3( ebx, ecx, edx, eax, 9, 0x04881d05, 23) + MD5STEP3( eax, ebx, ecx, edx, 12, 0xd9d4d039, 4) + MD5STEP3( edx, eax, ebx, ecx, 15, 0xe6db99e5, 11) + MD5STEP3( ecx, edx, eax, ebx, 2, 0x1fa27cf8, 16) + MD5STEP3( ebx, ecx, edx, eax, 0, 0xc4ac5665, 23) + + // setup + AS2( mov esi, edx ) + AS1( not esi ) + + MD5STEP4( eax, ebx, ecx, edx, 7, 0xf4292244, 6) + MD5STEP4( edx, eax, ebx, ecx, 14, 0x432aff97, 10) + MD5STEP4( ecx, edx, eax, ebx, 5, 0xab9423a7, 15) + MD5STEP4( ebx, ecx, edx, eax, 12, 0xfc93a039, 21) + MD5STEP4( eax, ebx, ecx, edx, 3, 0x655b59c3, 6) + MD5STEP4( edx, eax, ebx, ecx, 10, 0x8f0ccc92, 10) + MD5STEP4( ecx, edx, eax, ebx, 1, 0xffeff47d, 15) + MD5STEP4( ebx, ecx, edx, eax, 8, 0x85845dd1, 21) + MD5STEP4( eax, ebx, ecx, edx, 15, 0x6fa87e4f, 6) + MD5STEP4( edx, eax, ebx, ecx, 6, 0xfe2ce6e0, 10) + MD5STEP4( ecx, edx, eax, ebx, 13, 0xa3014314, 15) + MD5STEP4( ebx, ecx, edx, eax, 4, 0x4e0811a1, 21) + MD5STEP4( eax, ebx, ecx, edx, 11, 0xf7537e82, 6) + MD5STEP4( edx, eax, ebx, ecx, 2, 0xbd3af235, 10) + MD5STEP4( ecx, edx, eax, ebx, 9, 0x2ad7d2bb, 15) + MD5STEP4( ebx, ecx, edx, eax, 9, 0xeb86d391, 21) + + AS2( movd esi, mm1 ) // digest_ + + AS2( add [esi], eax ) // write out + AS2( add [esi + 4], ebx ) + AS2( add [esi + 8], ecx ) + AS2( add [esi + 12], edx ) + + AS2( add edi, 64 ) + + AS2( mov eax, [esi] ) + AS2( mov ebx, [esi + 4] ) + AS2( mov ecx, [esi + 8] ) + AS2( mov edx, [esi + 12] ) + + AS2( movd ebp, mm2 ) // times + AS1( dec ebp ) + AS2( movd mm2, ebp ) + AS1( jnz loopStart ) + + + EPILOG() +} + + +#endif // DO_MD5_ASM + + +void MD5::Transform() +{ +#define F1(x, y, z) (z ^ (x & (y ^ z))) +#define F2(x, y, z) F1(z, x, y) +#define F3(x, y, z) (x ^ y ^ z) +#define F4(x, y, z) (y ^ (x | ~z)) + +#define MD5STEP(f, w, x, y, z, data, s) \ + w = rotlFixed(w + f(x, y, z) + data, s) + x + + // Copy context->state[] to working vars + word32 a = digest_[0]; + word32 b = digest_[1]; + word32 c = digest_[2]; + word32 d = digest_[3]; + + MD5STEP(F1, a, b, c, d, buffer_[0] + 0xd76aa478, 7); + MD5STEP(F1, d, a, b, c, buffer_[1] + 0xe8c7b756, 12); + MD5STEP(F1, c, d, a, b, buffer_[2] + 0x242070db, 17); + MD5STEP(F1, b, c, d, a, buffer_[3] + 0xc1bdceee, 22); + MD5STEP(F1, a, b, c, d, buffer_[4] + 0xf57c0faf, 7); + MD5STEP(F1, d, a, b, c, buffer_[5] + 0x4787c62a, 12); + MD5STEP(F1, c, d, a, b, buffer_[6] + 0xa8304613, 17); + MD5STEP(F1, b, c, d, a, buffer_[7] + 0xfd469501, 22); + MD5STEP(F1, a, b, c, d, buffer_[8] + 0x698098d8, 7); + MD5STEP(F1, d, a, b, c, buffer_[9] + 0x8b44f7af, 12); + MD5STEP(F1, c, d, a, b, buffer_[10] + 0xffff5bb1, 17); + MD5STEP(F1, b, c, d, a, buffer_[11] + 0x895cd7be, 22); + MD5STEP(F1, a, b, c, d, buffer_[12] + 0x6b901122, 7); + MD5STEP(F1, d, a, b, c, buffer_[13] + 0xfd987193, 12); + MD5STEP(F1, c, d, a, b, buffer_[14] + 0xa679438e, 17); + MD5STEP(F1, b, c, d, a, buffer_[15] + 0x49b40821, 22); + + MD5STEP(F2, a, b, c, d, buffer_[1] + 0xf61e2562, 5); + MD5STEP(F2, d, a, b, c, buffer_[6] + 0xc040b340, 9); + MD5STEP(F2, c, d, a, b, buffer_[11] + 0x265e5a51, 14); + MD5STEP(F2, b, c, d, a, buffer_[0] + 0xe9b6c7aa, 20); + MD5STEP(F2, a, b, c, d, buffer_[5] + 0xd62f105d, 5); + MD5STEP(F2, d, a, b, c, buffer_[10] + 0x02441453, 9); + MD5STEP(F2, c, d, a, b, buffer_[15] + 0xd8a1e681, 14); + MD5STEP(F2, b, c, d, a, buffer_[4] + 0xe7d3fbc8, 20); + MD5STEP(F2, a, b, c, d, buffer_[9] + 0x21e1cde6, 5); + MD5STEP(F2, d, a, b, c, buffer_[14] + 0xc33707d6, 9); + MD5STEP(F2, c, d, a, b, buffer_[3] + 0xf4d50d87, 14); + MD5STEP(F2, b, c, d, a, buffer_[8] + 0x455a14ed, 20); + MD5STEP(F2, a, b, c, d, buffer_[13] + 0xa9e3e905, 5); + MD5STEP(F2, d, a, b, c, buffer_[2] + 0xfcefa3f8, 9); + MD5STEP(F2, c, d, a, b, buffer_[7] + 0x676f02d9, 14); + MD5STEP(F2, b, c, d, a, buffer_[12] + 0x8d2a4c8a, 20); + + MD5STEP(F3, a, b, c, d, buffer_[5] + 0xfffa3942, 4); + MD5STEP(F3, d, a, b, c, buffer_[8] + 0x8771f681, 11); + MD5STEP(F3, c, d, a, b, buffer_[11] + 0x6d9d6122, 16); + MD5STEP(F3, b, c, d, a, buffer_[14] + 0xfde5380c, 23); + MD5STEP(F3, a, b, c, d, buffer_[1] + 0xa4beea44, 4); + MD5STEP(F3, d, a, b, c, buffer_[4] + 0x4bdecfa9, 11); + MD5STEP(F3, c, d, a, b, buffer_[7] + 0xf6bb4b60, 16); + MD5STEP(F3, b, c, d, a, buffer_[10] + 0xbebfbc70, 23); + MD5STEP(F3, a, b, c, d, buffer_[13] + 0x289b7ec6, 4); + MD5STEP(F3, d, a, b, c, buffer_[0] + 0xeaa127fa, 11); + MD5STEP(F3, c, d, a, b, buffer_[3] + 0xd4ef3085, 16); + MD5STEP(F3, b, c, d, a, buffer_[6] + 0x04881d05, 23); + MD5STEP(F3, a, b, c, d, buffer_[9] + 0xd9d4d039, 4); + MD5STEP(F3, d, a, b, c, buffer_[12] + 0xe6db99e5, 11); + MD5STEP(F3, c, d, a, b, buffer_[15] + 0x1fa27cf8, 16); + MD5STEP(F3, b, c, d, a, buffer_[2] + 0xc4ac5665, 23); + + MD5STEP(F4, a, b, c, d, buffer_[0] + 0xf4292244, 6); + MD5STEP(F4, d, a, b, c, buffer_[7] + 0x432aff97, 10); + MD5STEP(F4, c, d, a, b, buffer_[14] + 0xab9423a7, 15); + MD5STEP(F4, b, c, d, a, buffer_[5] + 0xfc93a039, 21); + MD5STEP(F4, a, b, c, d, buffer_[12] + 0x655b59c3, 6); + MD5STEP(F4, d, a, b, c, buffer_[3] + 0x8f0ccc92, 10); + MD5STEP(F4, c, d, a, b, buffer_[10] + 0xffeff47d, 15); + MD5STEP(F4, b, c, d, a, buffer_[1] + 0x85845dd1, 21); + MD5STEP(F4, a, b, c, d, buffer_[8] + 0x6fa87e4f, 6); + MD5STEP(F4, d, a, b, c, buffer_[15] + 0xfe2ce6e0, 10); + MD5STEP(F4, c, d, a, b, buffer_[6] + 0xa3014314, 15); + MD5STEP(F4, b, c, d, a, buffer_[13] + 0x4e0811a1, 21); + MD5STEP(F4, a, b, c, d, buffer_[4] + 0xf7537e82, 6); + MD5STEP(F4, d, a, b, c, buffer_[11] + 0xbd3af235, 10); + MD5STEP(F4, c, d, a, b, buffer_[2] + 0x2ad7d2bb, 15); + MD5STEP(F4, b, c, d, a, buffer_[9] + 0xeb86d391, 21); + + // Add the working vars back into digest state[] + digest_[0] += a; + digest_[1] += b; + digest_[2] += c; + digest_[3] += d; + + // Wipe variables + a = b = c = d = 0; +} + + +} // namespace + diff --git a/externals/mysql/extlib/yassl/taocrypt/src/misc.cpp b/externals/mysql/extlib/yassl/taocrypt/src/misc.cpp new file mode 100644 index 00000000000..402645c93fd --- /dev/null +++ b/externals/mysql/extlib/yassl/taocrypt/src/misc.cpp @@ -0,0 +1,296 @@ +/* + 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.cpp from CryptoPP */ + + +#include "runtime.hpp" +#include "misc.hpp" + + +#ifdef __GNUC__ + #include <signal.h> + #include <setjmp.h> +#endif + +#ifdef USE_SYS_STL + #include <algorithm> +#else + #include "algorithm.hpp" +#endif + +namespace STL = STL_NAMESPACE; + + +#ifdef YASSL_PURE_C + + void* operator new(size_t sz, TaoCrypt::new_t) + { + void* ptr = malloc(sz ? sz : 1); + if (!ptr) abort(); + + return ptr; + } + + + void operator delete(void* ptr, TaoCrypt::new_t) + { + if (ptr) free(ptr); + } + + + void* operator new[](size_t sz, TaoCrypt::new_t nt) + { + return ::operator new(sz, nt); + } + + + void operator delete[](void* ptr, TaoCrypt::new_t nt) + { + ::operator delete(ptr, nt); + } + + + /* uncomment to test + // make sure not using globals anywhere by forgetting to use overloaded + void* operator new(size_t sz); + + void operator delete(void* ptr); + + void* operator new[](size_t sz); + + void operator delete[](void* ptr); + */ + + + namespace TaoCrypt { + + new_t tc; // for library new + + } + +#if defined(__ICC) || defined(__INTEL_COMPILER) + +extern "C" { + + int __cxa_pure_virtual() { + assert("Pure virtual method called." == "Aborted"); + return 0; + } + +} // extern "C" + +#endif + +#endif // YASSL_PURE_C + + +namespace TaoCrypt { + + +inline void XorWords(word* r, const word* a, unsigned int n) +{ + for (unsigned int i=0; i<n; i++) + r[i] ^= a[i]; +} + + +void xorbuf(byte* buf, const byte* mask, unsigned int count) +{ + if (((size_t)buf | (size_t)mask | count) % WORD_SIZE == 0) + XorWords((word *)buf, (const word *)mask, count/WORD_SIZE); + else + { + for (unsigned int i=0; i<count; i++) + buf[i] ^= mask[i]; + } +} + + +unsigned int BytePrecision(word value) +{ + unsigned int i; + for (i=sizeof(value); i; --i) + if (value >> (i-1)*8) + break; + + return i; +} + + +unsigned int BitPrecision(word value) +{ + if (!value) + return 0; + + unsigned int l = 0, + h = 8 * sizeof(value); + + while (h-l > 1) + { + unsigned int t = (l+h)/2; + if (value >> t) + l = t; + else + h = t; + } + + return h; +} + + +word Crop(word value, unsigned int size) +{ + if (size < 8*sizeof(value)) + return (value & ((1L << size) - 1)); + else + return value; +} + + + +#ifdef TAOCRYPT_X86ASM_AVAILABLE + +#ifndef _MSC_VER + static jmp_buf s_env; + static void SigIllHandler(int) + { + longjmp(s_env, 1); + } +#endif + + +bool HaveCpuId() +{ +#ifdef _MSC_VER + __try + { + __asm + { + mov eax, 0 + cpuid + } + } + __except (1) + { + return false; + } + return true; +#else + word32 eax, ebx; + __asm__ __volatile + ( + /* Put EFLAGS in eax and ebx */ + "pushf;" + "pushf;" + "pop %0;" + "movl %0,%1;" + + /* Flip the cpuid bit and store back in EFLAGS */ + "xorl $0x200000,%0;" + "push %0;" + "popf;" + + /* Read EFLAGS again */ + "pushf;" + "pop %0;" + "popf" + : "=r" (eax), "=r" (ebx) + : + : "cc" + ); + + if (eax == ebx) + return false; + return true; +#endif +} + + +void CpuId(word32 input, word32 *output) +{ +#ifdef __GNUC__ + __asm__ + ( + // save ebx in case -fPIC is being used + "push %%ebx; cpuid; mov %%ebx, %%edi; pop %%ebx" + : "=a" (output[0]), "=D" (output[1]), "=c" (output[2]), "=d"(output[3]) + : "a" (input) + ); +#else + __asm + { + mov eax, input + cpuid + mov edi, output + mov [edi], eax + mov [edi+4], ebx + mov [edi+8], ecx + mov [edi+12], edx + } +#endif +} + + +bool IsPentium() +{ + if (!HaveCpuId()) + return false; + + word32 cpuid[4]; + + CpuId(0, cpuid); + STL::swap(cpuid[2], cpuid[3]); + if (memcmp(cpuid+1, "GenuineIntel", 12) != 0) + return false; + + CpuId(1, cpuid); + byte family = ((cpuid[0] >> 8) & 0xf); + if (family < 5) + return false; + + return true; +} + + + +static bool IsMmx() +{ + if (!IsPentium()) + return false; + + word32 cpuid[4]; + + CpuId(1, cpuid); + if ((cpuid[3] & (1 << 23)) == 0) + return false; + + return true; +} + + +bool isMMX = IsMmx(); + + +#endif // TAOCRYPT_X86ASM_AVAILABLE + + + + +} // namespace + diff --git a/externals/mysql/extlib/yassl/taocrypt/src/random.cpp b/externals/mysql/extlib/yassl/taocrypt/src/random.cpp new file mode 100644 index 00000000000..89fd5f7c7bc --- /dev/null +++ b/externals/mysql/extlib/yassl/taocrypt/src/random.cpp @@ -0,0 +1,200 @@ +/* + 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.cpp implements a crypto secure Random Number Generator using an OS + specific seed, switch to /dev/random for more security but may block +*/ + +#include "runtime.hpp" +#include "random.hpp" +#include <string.h> +#include <time.h> + +#if defined(_WIN32) + #define _WIN32_WINNT 0x0400 + #include <windows.h> + #include <wincrypt.h> +#else + #include <errno.h> + #include <fcntl.h> + #include <unistd.h> +#endif // _WIN32 + +namespace TaoCrypt { + + +// Get seed and key cipher +RandomNumberGenerator::RandomNumberGenerator() +{ + byte key[32]; + byte junk[256]; + + seed_.GenerateSeed(key, sizeof(key)); + cipher_.SetKey(key, sizeof(key)); + GenerateBlock(junk, sizeof(junk)); // rid initial state +} + + +// place a generated block in output +void RandomNumberGenerator::GenerateBlock(byte* output, word32 sz) +{ + memset(output, 0, sz); + cipher_.Process(output, output, sz); +} + + +byte RandomNumberGenerator::GenerateByte() +{ + byte b; + GenerateBlock(&b, 1); + + return b; +} + + +#if defined(_WIN32) + +/* The OS_Seed implementation for windows */ + +OS_Seed::OS_Seed() +{ + if(!CryptAcquireContext(&handle_, 0, 0, PROV_RSA_FULL, + CRYPT_VERIFYCONTEXT)) + error_.SetError(WINCRYPT_E); +} + + +OS_Seed::~OS_Seed() +{ + CryptReleaseContext(handle_, 0); +} + + +void OS_Seed::GenerateSeed(byte* output, word32 sz) +{ + if (!CryptGenRandom(handle_, sz, output)) + error_.SetError(CRYPTGEN_E); +} + + +#elif defined(__NETWARE__) + +/* The OS_Seed implementation for Netware */ + +#include <nks/thread.h> +#include <nks/plat.h> + +// Loop on high resulution Read Time Stamp Counter +static void NetwareSeed(byte* output, word32 sz) +{ + word32 tscResult; + + for (word32 i = 0; i < sz; i += sizeof(tscResult)) { + #if defined(__GNUC__) + asm volatile("rdtsc" : "=A" (tscResult)); + #else + #ifdef __MWERKS__ + asm { + #else + __asm { + #endif + rdtsc + mov tscResult, eax + } + #endif + + memcpy(output, &tscResult, sizeof(tscResult)); + output += sizeof(tscResult); + + NXThreadYield(); // induce more variance + } +} + + +OS_Seed::OS_Seed() +{ +} + + +OS_Seed::~OS_Seed() +{ +} + + +void OS_Seed::GenerateSeed(byte* output, word32 sz) +{ + /* + Try to use NXSeedRandom as it will generate a strong + seed using the onboard 82802 chip + + As it's not always supported, fallback to default + implementation if an error is returned + */ + + if (NXSeedRandom(sz, output) != 0) + { + NetwareSeed(output, sz); + } +} + + +#else + +/* The default OS_Seed implementation */ + +OS_Seed::OS_Seed() +{ + fd_ = open("/dev/urandom",O_RDONLY); + if (fd_ == -1) { + fd_ = open("/dev/random",O_RDONLY); + if (fd_ == -1) + error_.SetError(OPEN_RAN_E); + } +} + + +OS_Seed::~OS_Seed() +{ + close(fd_); +} + + +// may block +void OS_Seed::GenerateSeed(byte* output, word32 sz) +{ + while (sz) { + int len = read(fd_, output, sz); + if (len == -1) { + error_.SetError(READ_RAN_E); + return; + } + + sz -= len; + output += len; + + if (sz) + sleep(1); + } +} + +#endif // _WIN32 + + + +} // namespace diff --git a/externals/mysql/extlib/yassl/taocrypt/src/ripemd.cpp b/externals/mysql/extlib/yassl/taocrypt/src/ripemd.cpp new file mode 100644 index 00000000000..a738c197bf2 --- /dev/null +++ b/externals/mysql/extlib/yassl/taocrypt/src/ripemd.cpp @@ -0,0 +1,834 @@ +/* + 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 ripemd.cpp from CryptoPP */ + +#include "runtime.hpp" +#include "ripemd.hpp" +#ifdef USE_SYS_STL + #include <algorithm> +#else + #include "algorithm.hpp" +#endif + + +namespace STL = STL_NAMESPACE; + + + +namespace TaoCrypt { + +void RIPEMD160::Init() +{ + digest_[0] = 0x67452301L; + digest_[1] = 0xefcdab89L; + digest_[2] = 0x98badcfeL; + digest_[3] = 0x10325476L; + digest_[4] = 0xc3d2e1f0L; + + buffLen_ = 0; + loLen_ = 0; + hiLen_ = 0; +} + + +RIPEMD160::RIPEMD160(const RIPEMD160& that) + : HASHwithTransform(DIGEST_SIZE / sizeof(word32), BLOCK_SIZE) +{ + buffLen_ = that.buffLen_; + loLen_ = that.loLen_; + hiLen_ = that.hiLen_; + + memcpy(digest_, that.digest_, DIGEST_SIZE); + memcpy(buffer_, that.buffer_, BLOCK_SIZE); +} + + +RIPEMD160& RIPEMD160::operator= (const RIPEMD160& that) +{ + RIPEMD160 tmp(that); + Swap(tmp); + + return *this; +} + + +void RIPEMD160::Swap(RIPEMD160& other) +{ + STL::swap(loLen_, other.loLen_); + STL::swap(hiLen_, other.hiLen_); + STL::swap(buffLen_, other.buffLen_); + + memcpy(digest_, other.digest_, DIGEST_SIZE); + memcpy(buffer_, other.buffer_, BLOCK_SIZE); +} + + +#ifdef DO_RIPEMD_ASM + +// Update digest with data of size len +void RIPEMD160::Update(const byte* data, word32 len) +{ + if (!isMMX) { + HASHwithTransform::Update(data, len); + return; + } + + byte* local = reinterpret_cast<byte*>(buffer_); + + // remove buffered data if possible + if (buffLen_) { + word32 add = min(len, BLOCK_SIZE - buffLen_); + memcpy(&local[buffLen_], data, add); + + buffLen_ += add; + data += add; + len -= add; + + if (buffLen_ == BLOCK_SIZE) { + Transform(); + AddLength(BLOCK_SIZE); + buffLen_ = 0; + } + } + + // all at once for asm + if (buffLen_ == 0) { + word32 times = len / BLOCK_SIZE; + if (times) { + AsmTransform(data, times); + const word32 add = BLOCK_SIZE * times; + AddLength(add); + len -= add; + data += add; + } + } + + // cache any data left + if (len) { + memcpy(&local[buffLen_], data, len); + buffLen_ += len; + } +} + +#endif // DO_RIPEMD_ASM + + +// for all +#define F(x, y, z) (x ^ y ^ z) +#define G(x, y, z) (z ^ (x & (y^z))) +#define H(x, y, z) (z ^ (x | ~y)) +#define I(x, y, z) (y ^ (z & (x^y))) +#define J(x, y, z) (x ^ (y | ~z)) + +#define k0 0 +#define k1 0x5a827999 +#define k2 0x6ed9eba1 +#define k3 0x8f1bbcdc +#define k4 0xa953fd4e +#define k5 0x50a28be6 +#define k6 0x5c4dd124 +#define k7 0x6d703ef3 +#define k8 0x7a6d76e9 +#define k9 0 + +// for 160 and 320 +#define Subround(f, a, b, c, d, e, x, s, k) \ + a += f(b, c, d) + x + k;\ + a = rotlFixed((word32)a, s) + e;\ + c = rotlFixed((word32)c, 10U) + + +void RIPEMD160::Transform() +{ + unsigned long a1, b1, c1, d1, e1, a2, b2, c2, d2, e2; + a1 = a2 = digest_[0]; + b1 = b2 = digest_[1]; + c1 = c2 = digest_[2]; + d1 = d2 = digest_[3]; + e1 = e2 = digest_[4]; + + Subround(F, a1, b1, c1, d1, e1, buffer_[ 0], 11, k0); + Subround(F, e1, a1, b1, c1, d1, buffer_[ 1], 14, k0); + Subround(F, d1, e1, a1, b1, c1, buffer_[ 2], 15, k0); + Subround(F, c1, d1, e1, a1, b1, buffer_[ 3], 12, k0); + Subround(F, b1, c1, d1, e1, a1, buffer_[ 4], 5, k0); + Subround(F, a1, b1, c1, d1, e1, buffer_[ 5], 8, k0); + Subround(F, e1, a1, b1, c1, d1, buffer_[ 6], 7, k0); + Subround(F, d1, e1, a1, b1, c1, buffer_[ 7], 9, k0); + Subround(F, c1, d1, e1, a1, b1, buffer_[ 8], 11, k0); + Subround(F, b1, c1, d1, e1, a1, buffer_[ 9], 13, k0); + Subround(F, a1, b1, c1, d1, e1, buffer_[10], 14, k0); + Subround(F, e1, a1, b1, c1, d1, buffer_[11], 15, k0); + Subround(F, d1, e1, a1, b1, c1, buffer_[12], 6, k0); + Subround(F, c1, d1, e1, a1, b1, buffer_[13], 7, k0); + Subround(F, b1, c1, d1, e1, a1, buffer_[14], 9, k0); + Subround(F, a1, b1, c1, d1, e1, buffer_[15], 8, k0); + + Subround(G, e1, a1, b1, c1, d1, buffer_[ 7], 7, k1); + Subround(G, d1, e1, a1, b1, c1, buffer_[ 4], 6, k1); + Subround(G, c1, d1, e1, a1, b1, buffer_[13], 8, k1); + Subround(G, b1, c1, d1, e1, a1, buffer_[ 1], 13, k1); + Subround(G, a1, b1, c1, d1, e1, buffer_[10], 11, k1); + Subround(G, e1, a1, b1, c1, d1, buffer_[ 6], 9, k1); + Subround(G, d1, e1, a1, b1, c1, buffer_[15], 7, k1); + Subround(G, c1, d1, e1, a1, b1, buffer_[ 3], 15, k1); + Subround(G, b1, c1, d1, e1, a1, buffer_[12], 7, k1); + Subround(G, a1, b1, c1, d1, e1, buffer_[ 0], 12, k1); + Subround(G, e1, a1, b1, c1, d1, buffer_[ 9], 15, k1); + Subround(G, d1, e1, a1, b1, c1, buffer_[ 5], 9, k1); + Subround(G, c1, d1, e1, a1, b1, buffer_[ 2], 11, k1); + Subround(G, b1, c1, d1, e1, a1, buffer_[14], 7, k1); + Subround(G, a1, b1, c1, d1, e1, buffer_[11], 13, k1); + Subround(G, e1, a1, b1, c1, d1, buffer_[ 8], 12, k1); + + Subround(H, d1, e1, a1, b1, c1, buffer_[ 3], 11, k2); + Subround(H, c1, d1, e1, a1, b1, buffer_[10], 13, k2); + Subround(H, b1, c1, d1, e1, a1, buffer_[14], 6, k2); + Subround(H, a1, b1, c1, d1, e1, buffer_[ 4], 7, k2); + Subround(H, e1, a1, b1, c1, d1, buffer_[ 9], 14, k2); + Subround(H, d1, e1, a1, b1, c1, buffer_[15], 9, k2); + Subround(H, c1, d1, e1, a1, b1, buffer_[ 8], 13, k2); + Subround(H, b1, c1, d1, e1, a1, buffer_[ 1], 15, k2); + Subround(H, a1, b1, c1, d1, e1, buffer_[ 2], 14, k2); + Subround(H, e1, a1, b1, c1, d1, buffer_[ 7], 8, k2); + Subround(H, d1, e1, a1, b1, c1, buffer_[ 0], 13, k2); + Subround(H, c1, d1, e1, a1, b1, buffer_[ 6], 6, k2); + Subround(H, b1, c1, d1, e1, a1, buffer_[13], 5, k2); + Subround(H, a1, b1, c1, d1, e1, buffer_[11], 12, k2); + Subround(H, e1, a1, b1, c1, d1, buffer_[ 5], 7, k2); + Subround(H, d1, e1, a1, b1, c1, buffer_[12], 5, k2); + + Subround(I, c1, d1, e1, a1, b1, buffer_[ 1], 11, k3); + Subround(I, b1, c1, d1, e1, a1, buffer_[ 9], 12, k3); + Subround(I, a1, b1, c1, d1, e1, buffer_[11], 14, k3); + Subround(I, e1, a1, b1, c1, d1, buffer_[10], 15, k3); + Subround(I, d1, e1, a1, b1, c1, buffer_[ 0], 14, k3); + Subround(I, c1, d1, e1, a1, b1, buffer_[ 8], 15, k3); + Subround(I, b1, c1, d1, e1, a1, buffer_[12], 9, k3); + Subround(I, a1, b1, c1, d1, e1, buffer_[ 4], 8, k3); + Subround(I, e1, a1, b1, c1, d1, buffer_[13], 9, k3); + Subround(I, d1, e1, a1, b1, c1, buffer_[ 3], 14, k3); + Subround(I, c1, d1, e1, a1, b1, buffer_[ 7], 5, k3); + Subround(I, b1, c1, d1, e1, a1, buffer_[15], 6, k3); + Subround(I, a1, b1, c1, d1, e1, buffer_[14], 8, k3); + Subround(I, e1, a1, b1, c1, d1, buffer_[ 5], 6, k3); + Subround(I, d1, e1, a1, b1, c1, buffer_[ 6], 5, k3); + Subround(I, c1, d1, e1, a1, b1, buffer_[ 2], 12, k3); + + Subround(J, b1, c1, d1, e1, a1, buffer_[ 4], 9, k4); + Subround(J, a1, b1, c1, d1, e1, buffer_[ 0], 15, k4); + Subround(J, e1, a1, b1, c1, d1, buffer_[ 5], 5, k4); + Subround(J, d1, e1, a1, b1, c1, buffer_[ 9], 11, k4); + Subround(J, c1, d1, e1, a1, b1, buffer_[ 7], 6, k4); + Subround(J, b1, c1, d1, e1, a1, buffer_[12], 8, k4); + Subround(J, a1, b1, c1, d1, e1, buffer_[ 2], 13, k4); + Subround(J, e1, a1, b1, c1, d1, buffer_[10], 12, k4); + Subround(J, d1, e1, a1, b1, c1, buffer_[14], 5, k4); + Subround(J, c1, d1, e1, a1, b1, buffer_[ 1], 12, k4); + Subround(J, b1, c1, d1, e1, a1, buffer_[ 3], 13, k4); + Subround(J, a1, b1, c1, d1, e1, buffer_[ 8], 14, k4); + Subround(J, e1, a1, b1, c1, d1, buffer_[11], 11, k4); + Subround(J, d1, e1, a1, b1, c1, buffer_[ 6], 8, k4); + Subround(J, c1, d1, e1, a1, b1, buffer_[15], 5, k4); + Subround(J, b1, c1, d1, e1, a1, buffer_[13], 6, k4); + + Subround(J, a2, b2, c2, d2, e2, buffer_[ 5], 8, k5); + Subround(J, e2, a2, b2, c2, d2, buffer_[14], 9, k5); + Subround(J, d2, e2, a2, b2, c2, buffer_[ 7], 9, k5); + Subround(J, c2, d2, e2, a2, b2, buffer_[ 0], 11, k5); + Subround(J, b2, c2, d2, e2, a2, buffer_[ 9], 13, k5); + Subround(J, a2, b2, c2, d2, e2, buffer_[ 2], 15, k5); + Subround(J, e2, a2, b2, c2, d2, buffer_[11], 15, k5); + Subround(J, d2, e2, a2, b2, c2, buffer_[ 4], 5, k5); + Subround(J, c2, d2, e2, a2, b2, buffer_[13], 7, k5); + Subround(J, b2, c2, d2, e2, a2, buffer_[ 6], 7, k5); + Subround(J, a2, b2, c2, d2, e2, buffer_[15], 8, k5); + Subround(J, e2, a2, b2, c2, d2, buffer_[ 8], 11, k5); + Subround(J, d2, e2, a2, b2, c2, buffer_[ 1], 14, k5); + Subround(J, c2, d2, e2, a2, b2, buffer_[10], 14, k5); + Subround(J, b2, c2, d2, e2, a2, buffer_[ 3], 12, k5); + Subround(J, a2, b2, c2, d2, e2, buffer_[12], 6, k5); + + Subround(I, e2, a2, b2, c2, d2, buffer_[ 6], 9, k6); + Subround(I, d2, e2, a2, b2, c2, buffer_[11], 13, k6); + Subround(I, c2, d2, e2, a2, b2, buffer_[ 3], 15, k6); + Subround(I, b2, c2, d2, e2, a2, buffer_[ 7], 7, k6); + Subround(I, a2, b2, c2, d2, e2, buffer_[ 0], 12, k6); + Subround(I, e2, a2, b2, c2, d2, buffer_[13], 8, k6); + Subround(I, d2, e2, a2, b2, c2, buffer_[ 5], 9, k6); + Subround(I, c2, d2, e2, a2, b2, buffer_[10], 11, k6); + Subround(I, b2, c2, d2, e2, a2, buffer_[14], 7, k6); + Subround(I, a2, b2, c2, d2, e2, buffer_[15], 7, k6); + Subround(I, e2, a2, b2, c2, d2, buffer_[ 8], 12, k6); + Subround(I, d2, e2, a2, b2, c2, buffer_[12], 7, k6); + Subround(I, c2, d2, e2, a2, b2, buffer_[ 4], 6, k6); + Subround(I, b2, c2, d2, e2, a2, buffer_[ 9], 15, k6); + Subround(I, a2, b2, c2, d2, e2, buffer_[ 1], 13, k6); + Subround(I, e2, a2, b2, c2, d2, buffer_[ 2], 11, k6); + + Subround(H, d2, e2, a2, b2, c2, buffer_[15], 9, k7); + Subround(H, c2, d2, e2, a2, b2, buffer_[ 5], 7, k7); + Subround(H, b2, c2, d2, e2, a2, buffer_[ 1], 15, k7); + Subround(H, a2, b2, c2, d2, e2, buffer_[ 3], 11, k7); + Subround(H, e2, a2, b2, c2, d2, buffer_[ 7], 8, k7); + Subround(H, d2, e2, a2, b2, c2, buffer_[14], 6, k7); + Subround(H, c2, d2, e2, a2, b2, buffer_[ 6], 6, k7); + Subround(H, b2, c2, d2, e2, a2, buffer_[ 9], 14, k7); + Subround(H, a2, b2, c2, d2, e2, buffer_[11], 12, k7); + Subround(H, e2, a2, b2, c2, d2, buffer_[ 8], 13, k7); + Subround(H, d2, e2, a2, b2, c2, buffer_[12], 5, k7); + Subround(H, c2, d2, e2, a2, b2, buffer_[ 2], 14, k7); + Subround(H, b2, c2, d2, e2, a2, buffer_[10], 13, k7); + Subround(H, a2, b2, c2, d2, e2, buffer_[ 0], 13, k7); + Subround(H, e2, a2, b2, c2, d2, buffer_[ 4], 7, k7); + Subround(H, d2, e2, a2, b2, c2, buffer_[13], 5, k7); + + Subround(G, c2, d2, e2, a2, b2, buffer_[ 8], 15, k8); + Subround(G, b2, c2, d2, e2, a2, buffer_[ 6], 5, k8); + Subround(G, a2, b2, c2, d2, e2, buffer_[ 4], 8, k8); + Subround(G, e2, a2, b2, c2, d2, buffer_[ 1], 11, k8); + Subround(G, d2, e2, a2, b2, c2, buffer_[ 3], 14, k8); + Subround(G, c2, d2, e2, a2, b2, buffer_[11], 14, k8); + Subround(G, b2, c2, d2, e2, a2, buffer_[15], 6, k8); + Subround(G, a2, b2, c2, d2, e2, buffer_[ 0], 14, k8); + Subround(G, e2, a2, b2, c2, d2, buffer_[ 5], 6, k8); + Subround(G, d2, e2, a2, b2, c2, buffer_[12], 9, k8); + Subround(G, c2, d2, e2, a2, b2, buffer_[ 2], 12, k8); + Subround(G, b2, c2, d2, e2, a2, buffer_[13], 9, k8); + Subround(G, a2, b2, c2, d2, e2, buffer_[ 9], 12, k8); + Subround(G, e2, a2, b2, c2, d2, buffer_[ 7], 5, k8); + Subround(G, d2, e2, a2, b2, c2, buffer_[10], 15, k8); + Subround(G, c2, d2, e2, a2, b2, buffer_[14], 8, k8); + + Subround(F, b2, c2, d2, e2, a2, buffer_[12], 8, k9); + Subround(F, a2, b2, c2, d2, e2, buffer_[15], 5, k9); + Subround(F, e2, a2, b2, c2, d2, buffer_[10], 12, k9); + Subround(F, d2, e2, a2, b2, c2, buffer_[ 4], 9, k9); + Subround(F, c2, d2, e2, a2, b2, buffer_[ 1], 12, k9); + Subround(F, b2, c2, d2, e2, a2, buffer_[ 5], 5, k9); + Subround(F, a2, b2, c2, d2, e2, buffer_[ 8], 14, k9); + Subround(F, e2, a2, b2, c2, d2, buffer_[ 7], 6, k9); + Subround(F, d2, e2, a2, b2, c2, buffer_[ 6], 8, k9); + Subround(F, c2, d2, e2, a2, b2, buffer_[ 2], 13, k9); + Subround(F, b2, c2, d2, e2, a2, buffer_[13], 6, k9); + Subround(F, a2, b2, c2, d2, e2, buffer_[14], 5, k9); + Subround(F, e2, a2, b2, c2, d2, buffer_[ 0], 15, k9); + Subround(F, d2, e2, a2, b2, c2, buffer_[ 3], 13, k9); + Subround(F, c2, d2, e2, a2, b2, buffer_[ 9], 11, k9); + Subround(F, b2, c2, d2, e2, a2, buffer_[11], 11, k9); + + c1 = digest_[1] + c1 + d2; + digest_[1] = digest_[2] + d1 + e2; + digest_[2] = digest_[3] + e1 + a2; + digest_[3] = digest_[4] + a1 + b2; + digest_[4] = digest_[0] + b1 + c2; + digest_[0] = c1; +} + + +#ifdef DO_RIPEMD_ASM + +/* + // F(x ^ y ^ z) + // place in esi +#define ASMF(x, y, z) \ + AS2( mov esi, x ) \ + AS2( xor esi, y ) \ + AS2( xor esi, z ) + + + // G(z ^ (x & (y^z))) + // place in esi +#define ASMG(x, y, z) \ + AS2( mov esi, z ) \ + AS2( xor esi, y ) \ + AS2( and esi, x ) \ + AS2( xor esi, z ) + + + // H(z ^ (x | ~y)) + // place in esi +#define ASMH(x, y, z) \ + AS2( mov esi, y ) \ + AS1( not esi ) \ + AS2( or esi, x ) \ + AS2( xor esi, z ) + + + // I(y ^ (z & (x^y))) + // place in esi +#define ASMI(x, y, z) \ + AS2( mov esi, y ) \ + AS2( xor esi, x ) \ + AS2( and esi, z ) \ + AS2( xor esi, y ) + + + // J(x ^ (y | ~z))) + // place in esi +#define ASMJ(x, y, z) \ + AS2( mov esi, z ) \ + AS1( not esi ) \ + AS2( or esi, y ) \ + AS2( xor esi, x ) + + +// for 160 and 320 +// #define ASMSubround(f, a, b, c, d, e, i, s, k) +// a += f(b, c, d) + data[i] + k; +// a = rotlFixed((word32)a, s) + e; +// c = rotlFixed((word32)c, 10U) + +#define ASMSubround(f, a, b, c, d, e, index, s, k) \ + // a += f(b, c, d) + data[i] + k \ + AS2( mov esp, [edi + index * 4] ) \ + f(b, c, d) \ + AS2( add esi, k ) \ + AS2( add esi, esp ) \ + AS2( add a, esi ) \ + // a = rotlFixed((word32)a, s) + e \ + AS2( rol a, s ) \ + AS2( rol c, 10 ) \ + // c = rotlFixed((word32)c, 10U) \ + AS2( add a, e ) +*/ + + +// combine F into subround w/ setup +// esi already has c, setup for next round when done +// esp already has edi[index], setup for next round when done + +#define ASMSubroundF(a, b, c, d, e, index, s) \ + /* a += (b ^ c ^ d) + data[i] + k */ \ + AS2( xor esi, b ) \ + AS2( add a, [edi + index * 4] ) \ + AS2( xor esi, d ) \ + AS2( add a, esi ) \ + /* a = rotlFixed((word32)a, s) + e */ \ + AS2( mov esi, b ) \ + AS2( rol a, s ) \ + /* c = rotlFixed((word32)c, 10U) */ \ + AS2( rol c, 10 ) \ + AS2( add a, e ) + + +// combine G into subround w/ setup +// esi already has c, setup for next round when done +// esp already has edi[index], setup for next round when done + +#define ASMSubroundG(a, b, c, d, e, index, s, k) \ + /* a += (d ^ (b & (c^d))) + data[i] + k */ \ + AS2( xor esi, d ) \ + AS2( and esi, b ) \ + AS2( add a, [edi + index * 4] ) \ + AS2( xor esi, d ) \ + AS2( lea a, [esi + a + k] ) \ + /* a = rotlFixed((word32)a, s) + e */ \ + AS2( mov esi, b ) \ + AS2( rol a, s ) \ + /* c = rotlFixed((word32)c, 10U) */ \ + AS2( rol c, 10 ) \ + AS2( add a, e ) + + +// combine H into subround w/ setup +// esi already has c, setup for next round when done +// esp already has edi[index], setup for next round when done + +#define ASMSubroundH(a, b, c, d, e, index, s, k) \ + /* a += (d ^ (b | ~c)) + data[i] + k */ \ + AS1( not esi ) \ + AS2( or esi, b ) \ + AS2( add a, [edi + index * 4] ) \ + AS2( xor esi, d ) \ + AS2( lea a, [esi + a + k] ) \ + /* a = rotlFixed((word32)a, s) + e */ \ + AS2( mov esi, b ) \ + AS2( rol a, s ) \ + /* c = rotlFixed((word32)c, 10U) */ \ + AS2( rol c, 10 ) \ + AS2( add a, e ) + + +// combine I into subround w/ setup +// esi already has c, setup for next round when done +// esp already has edi[index], setup for next round when done + +#define ASMSubroundI(a, b, c, d, e, index, s, k) \ + /* a += (c ^ (d & (b^c))) + data[i] + k */ \ + AS2( xor esi, b ) \ + AS2( and esi, d ) \ + AS2( add a, [edi + index * 4] ) \ + AS2( xor esi, c ) \ + AS2( lea a, [esi + a + k] ) \ + /* a = rotlFixed((word32)a, s) + e */ \ + AS2( mov esi, b ) \ + AS2( rol a, s ) \ + /* c = rotlFixed((word32)c, 10U) */ \ + AS2( rol c, 10 ) \ + AS2( add a, e ) + + +// combine J into subround w/ setup +// esi already has d, setup for next round when done +// esp already has edi[index], setup for next round when done + +#define ASMSubroundJ(a, b, c, d, e, index, s, k) \ + /* a += (b ^ (c | ~d))) + data[i] + k */ \ + AS1( not esi ) \ + AS2( or esi, c ) \ + /* c = rotlFixed((word32)c, 10U) */ \ + AS2( add a, [edi + index * 4] ) \ + AS2( xor esi, b ) \ + AS2( rol c, 10 ) \ + AS2( lea a, [esi + a + k] ) \ + /* a = rotlFixed((word32)a, s) + e */ \ + AS2( rol a, s ) \ + AS2( mov esi, c ) \ + AS2( add a, e ) + + +#ifdef _MSC_VER + __declspec(naked) +#endif +void RIPEMD160::AsmTransform(const byte* data, word32 times) +{ +#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 edx, DWORD PTR [ebp + 16] ) + + #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 edx, DWORD PTR [ebp + 12] ) + + #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 8 ) + +#endif + + PROLOG() + + #ifdef OLD_GCC_OFFSET + AS2( lea esi, [ecx + 20] ) // digest_[0] + #else + AS2( lea esi, [ecx + 16] ) // digest_[0] + #endif + + AS2( sub esp, 24 ) // make room for tmp a1 - e1 + AS2( movd mm1, esi ) // store digest_ + +AS1( loopStart: ) + + AS2( movd mm2, edx ) // store times_ + + AS2( mov eax, [esi] ) // a1 + AS2( mov ebx, [esi + 4] ) // b1 + AS2( mov ecx, [esi + 8] ) // c1 + AS2( mov edx, [esi + 12] ) // d1 + AS2( mov ebp, [esi + 16] ) // e1 + + // setup + AS2( mov esi, ecx ) + + ASMSubroundF( eax, ebx, ecx, edx, ebp, 0, 11) + ASMSubroundF( ebp, eax, ebx, ecx, edx, 1, 14) + ASMSubroundF( edx, ebp, eax, ebx, ecx, 2, 15) + ASMSubroundF( ecx, edx, ebp, eax, ebx, 3, 12) + ASMSubroundF( ebx, ecx, edx, ebp, eax, 4, 5) + ASMSubroundF( eax, ebx, ecx, edx, ebp, 5, 8) + ASMSubroundF( ebp, eax, ebx, ecx, edx, 6, 7) + ASMSubroundF( edx, ebp, eax, ebx, ecx, 7, 9) + ASMSubroundF( ecx, edx, ebp, eax, ebx, 8, 11) + ASMSubroundF( ebx, ecx, edx, ebp, eax, 9, 13) + ASMSubroundF( eax, ebx, ecx, edx, ebp, 10, 14) + ASMSubroundF( ebp, eax, ebx, ecx, edx, 11, 15) + ASMSubroundF( edx, ebp, eax, ebx, ecx, 12, 6) + ASMSubroundF( ecx, edx, ebp, eax, ebx, 13, 7) + ASMSubroundF( ebx, ecx, edx, ebp, eax, 14, 9) + ASMSubroundF( eax, ebx, ecx, edx, ebp, 15, 8) + + ASMSubroundG( ebp, eax, ebx, ecx, edx, 7, 7, k1) + ASMSubroundG( edx, ebp, eax, ebx, ecx, 4, 6, k1) + ASMSubroundG( ecx, edx, ebp, eax, ebx, 13, 8, k1) + ASMSubroundG( ebx, ecx, edx, ebp, eax, 1, 13, k1) + ASMSubroundG( eax, ebx, ecx, edx, ebp, 10, 11, k1) + ASMSubroundG( ebp, eax, ebx, ecx, edx, 6, 9, k1) + ASMSubroundG( edx, ebp, eax, ebx, ecx, 15, 7, k1) + ASMSubroundG( ecx, edx, ebp, eax, ebx, 3, 15, k1) + ASMSubroundG( ebx, ecx, edx, ebp, eax, 12, 7, k1) + ASMSubroundG( eax, ebx, ecx, edx, ebp, 0, 12, k1) + ASMSubroundG( ebp, eax, ebx, ecx, edx, 9, 15, k1) + ASMSubroundG( edx, ebp, eax, ebx, ecx, 5, 9, k1) + ASMSubroundG( ecx, edx, ebp, eax, ebx, 2, 11, k1) + ASMSubroundG( ebx, ecx, edx, ebp, eax, 14, 7, k1) + ASMSubroundG( eax, ebx, ecx, edx, ebp, 11, 13, k1) + ASMSubroundG( ebp, eax, ebx, ecx, edx, 8, 12, k1) + + ASMSubroundH( edx, ebp, eax, ebx, ecx, 3, 11, k2) + ASMSubroundH( ecx, edx, ebp, eax, ebx, 10, 13, k2) + ASMSubroundH( ebx, ecx, edx, ebp, eax, 14, 6, k2) + ASMSubroundH( eax, ebx, ecx, edx, ebp, 4, 7, k2) + ASMSubroundH( ebp, eax, ebx, ecx, edx, 9, 14, k2) + ASMSubroundH( edx, ebp, eax, ebx, ecx, 15, 9, k2) + ASMSubroundH( ecx, edx, ebp, eax, ebx, 8, 13, k2) + ASMSubroundH( ebx, ecx, edx, ebp, eax, 1, 15, k2) + ASMSubroundH( eax, ebx, ecx, edx, ebp, 2, 14, k2) + ASMSubroundH( ebp, eax, ebx, ecx, edx, 7, 8, k2) + ASMSubroundH( edx, ebp, eax, ebx, ecx, 0, 13, k2) + ASMSubroundH( ecx, edx, ebp, eax, ebx, 6, 6, k2) + ASMSubroundH( ebx, ecx, edx, ebp, eax, 13, 5, k2) + ASMSubroundH( eax, ebx, ecx, edx, ebp, 11, 12, k2) + ASMSubroundH( ebp, eax, ebx, ecx, edx, 5, 7, k2) + ASMSubroundH( edx, ebp, eax, ebx, ecx, 12, 5, k2) + + ASMSubroundI( ecx, edx, ebp, eax, ebx, 1, 11, k3) + ASMSubroundI( ebx, ecx, edx, ebp, eax, 9, 12, k3) + ASMSubroundI( eax, ebx, ecx, edx, ebp, 11, 14, k3) + ASMSubroundI( ebp, eax, ebx, ecx, edx, 10, 15, k3) + ASMSubroundI( edx, ebp, eax, ebx, ecx, 0, 14, k3) + ASMSubroundI( ecx, edx, ebp, eax, ebx, 8, 15, k3) + ASMSubroundI( ebx, ecx, edx, ebp, eax, 12, 9, k3) + ASMSubroundI( eax, ebx, ecx, edx, ebp, 4, 8, k3) + ASMSubroundI( ebp, eax, ebx, ecx, edx, 13, 9, k3) + ASMSubroundI( edx, ebp, eax, ebx, ecx, 3, 14, k3) + ASMSubroundI( ecx, edx, ebp, eax, ebx, 7, 5, k3) + ASMSubroundI( ebx, ecx, edx, ebp, eax, 15, 6, k3) + ASMSubroundI( eax, ebx, ecx, edx, ebp, 14, 8, k3) + ASMSubroundI( ebp, eax, ebx, ecx, edx, 5, 6, k3) + ASMSubroundI( edx, ebp, eax, ebx, ecx, 6, 5, k3) + ASMSubroundI( ecx, edx, ebp, eax, ebx, 2, 12, k3) + + // setup + AS2( mov esi, ebp ) + + ASMSubroundJ( ebx, ecx, edx, ebp, eax, 4, 9, k4) + ASMSubroundJ( eax, ebx, ecx, edx, ebp, 0, 15, k4) + ASMSubroundJ( ebp, eax, ebx, ecx, edx, 5, 5, k4) + ASMSubroundJ( edx, ebp, eax, ebx, ecx, 9, 11, k4) + ASMSubroundJ( ecx, edx, ebp, eax, ebx, 7, 6, k4) + ASMSubroundJ( ebx, ecx, edx, ebp, eax, 12, 8, k4) + ASMSubroundJ( eax, ebx, ecx, edx, ebp, 2, 13, k4) + ASMSubroundJ( ebp, eax, ebx, ecx, edx, 10, 12, k4) + ASMSubroundJ( edx, ebp, eax, ebx, ecx, 14, 5, k4) + ASMSubroundJ( ecx, edx, ebp, eax, ebx, 1, 12, k4) + ASMSubroundJ( ebx, ecx, edx, ebp, eax, 3, 13, k4) + ASMSubroundJ( eax, ebx, ecx, edx, ebp, 8, 14, k4) + ASMSubroundJ( ebp, eax, ebx, ecx, edx, 11, 11, k4) + ASMSubroundJ( edx, ebp, eax, ebx, ecx, 6, 8, k4) + ASMSubroundJ( ecx, edx, ebp, eax, ebx, 15, 5, k4) + ASMSubroundJ( ebx, ecx, edx, ebp, eax, 13, 6, k4) + + // store a1 - e1 on stack + AS2( movd esi, mm1 ) // digest_ + + AS2( mov [esp], eax ) + AS2( mov [esp + 4], ebx ) + AS2( mov [esp + 8], ecx ) + AS2( mov [esp + 12], edx ) + AS2( mov [esp + 16], ebp ) + + AS2( mov eax, [esi] ) // a2 + AS2( mov ebx, [esi + 4] ) // b2 + AS2( mov ecx, [esi + 8] ) // c2 + AS2( mov edx, [esi + 12] ) // d2 + AS2( mov ebp, [esi + 16] ) // e2 + + + // setup + AS2( mov esi, edx ) + + ASMSubroundJ( eax, ebx, ecx, edx, ebp, 5, 8, k5) + ASMSubroundJ( ebp, eax, ebx, ecx, edx, 14, 9, k5) + ASMSubroundJ( edx, ebp, eax, ebx, ecx, 7, 9, k5) + ASMSubroundJ( ecx, edx, ebp, eax, ebx, 0, 11, k5) + ASMSubroundJ( ebx, ecx, edx, ebp, eax, 9, 13, k5) + ASMSubroundJ( eax, ebx, ecx, edx, ebp, 2, 15, k5) + ASMSubroundJ( ebp, eax, ebx, ecx, edx, 11, 15, k5) + ASMSubroundJ( edx, ebp, eax, ebx, ecx, 4, 5, k5) + ASMSubroundJ( ecx, edx, ebp, eax, ebx, 13, 7, k5) + ASMSubroundJ( ebx, ecx, edx, ebp, eax, 6, 7, k5) + ASMSubroundJ( eax, ebx, ecx, edx, ebp, 15, 8, k5) + ASMSubroundJ( ebp, eax, ebx, ecx, edx, 8, 11, k5) + ASMSubroundJ( edx, ebp, eax, ebx, ecx, 1, 14, k5) + ASMSubroundJ( ecx, edx, ebp, eax, ebx, 10, 14, k5) + ASMSubroundJ( ebx, ecx, edx, ebp, eax, 3, 12, k5) + ASMSubroundJ( eax, ebx, ecx, edx, ebp, 12, 6, k5) + + // setup + AS2( mov esi, ebx ) + + ASMSubroundI( ebp, eax, ebx, ecx, edx, 6, 9, k6) + ASMSubroundI( edx, ebp, eax, ebx, ecx, 11, 13, k6) + ASMSubroundI( ecx, edx, ebp, eax, ebx, 3, 15, k6) + ASMSubroundI( ebx, ecx, edx, ebp, eax, 7, 7, k6) + ASMSubroundI( eax, ebx, ecx, edx, ebp, 0, 12, k6) + ASMSubroundI( ebp, eax, ebx, ecx, edx, 13, 8, k6) + ASMSubroundI( edx, ebp, eax, ebx, ecx, 5, 9, k6) + ASMSubroundI( ecx, edx, ebp, eax, ebx, 10, 11, k6) + ASMSubroundI( ebx, ecx, edx, ebp, eax, 14, 7, k6) + ASMSubroundI( eax, ebx, ecx, edx, ebp, 15, 7, k6) + ASMSubroundI( ebp, eax, ebx, ecx, edx, 8, 12, k6) + ASMSubroundI( edx, ebp, eax, ebx, ecx, 12, 7, k6) + ASMSubroundI( ecx, edx, ebp, eax, ebx, 4, 6, k6) + ASMSubroundI( ebx, ecx, edx, ebp, eax, 9, 15, k6) + ASMSubroundI( eax, ebx, ecx, edx, ebp, 1, 13, k6) + ASMSubroundI( ebp, eax, ebx, ecx, edx, 2, 11, k6) + + ASMSubroundH( edx, ebp, eax, ebx, ecx, 15, 9, k7) + ASMSubroundH( ecx, edx, ebp, eax, ebx, 5, 7, k7) + ASMSubroundH( ebx, ecx, edx, ebp, eax, 1, 15, k7) + ASMSubroundH( eax, ebx, ecx, edx, ebp, 3, 11, k7) + ASMSubroundH( ebp, eax, ebx, ecx, edx, 7, 8, k7) + ASMSubroundH( edx, ebp, eax, ebx, ecx, 14, 6, k7) + ASMSubroundH( ecx, edx, ebp, eax, ebx, 6, 6, k7) + ASMSubroundH( ebx, ecx, edx, ebp, eax, 9, 14, k7) + ASMSubroundH( eax, ebx, ecx, edx, ebp, 11, 12, k7) + ASMSubroundH( ebp, eax, ebx, ecx, edx, 8, 13, k7) + ASMSubroundH( edx, ebp, eax, ebx, ecx, 12, 5, k7) + ASMSubroundH( ecx, edx, ebp, eax, ebx, 2, 14, k7) + ASMSubroundH( ebx, ecx, edx, ebp, eax, 10, 13, k7) + ASMSubroundH( eax, ebx, ecx, edx, ebp, 0, 13, k7) + ASMSubroundH( ebp, eax, ebx, ecx, edx, 4, 7, k7) + ASMSubroundH( edx, ebp, eax, ebx, ecx, 13, 5, k7) + + ASMSubroundG( ecx, edx, ebp, eax, ebx, 8, 15, k8) + ASMSubroundG( ebx, ecx, edx, ebp, eax, 6, 5, k8) + ASMSubroundG( eax, ebx, ecx, edx, ebp, 4, 8, k8) + ASMSubroundG( ebp, eax, ebx, ecx, edx, 1, 11, k8) + ASMSubroundG( edx, ebp, eax, ebx, ecx, 3, 14, k8) + ASMSubroundG( ecx, edx, ebp, eax, ebx, 11, 14, k8) + ASMSubroundG( ebx, ecx, edx, ebp, eax, 15, 6, k8) + ASMSubroundG( eax, ebx, ecx, edx, ebp, 0, 14, k8) + ASMSubroundG( ebp, eax, ebx, ecx, edx, 5, 6, k8) + ASMSubroundG( edx, ebp, eax, ebx, ecx, 12, 9, k8) + ASMSubroundG( ecx, edx, ebp, eax, ebx, 2, 12, k8) + ASMSubroundG( ebx, ecx, edx, ebp, eax, 13, 9, k8) + ASMSubroundG( eax, ebx, ecx, edx, ebp, 9, 12, k8) + ASMSubroundG( ebp, eax, ebx, ecx, edx, 7, 5, k8) + ASMSubroundG( edx, ebp, eax, ebx, ecx, 10, 15, k8) + ASMSubroundG( ecx, edx, ebp, eax, ebx, 14, 8, k8) + + ASMSubroundF( ebx, ecx, edx, ebp, eax, 12, 8) + ASMSubroundF( eax, ebx, ecx, edx, ebp, 15, 5) + ASMSubroundF( ebp, eax, ebx, ecx, edx, 10, 12) + ASMSubroundF( edx, ebp, eax, ebx, ecx, 4, 9) + ASMSubroundF( ecx, edx, ebp, eax, ebx, 1, 12) + ASMSubroundF( ebx, ecx, edx, ebp, eax, 5, 5) + ASMSubroundF( eax, ebx, ecx, edx, ebp, 8, 14) + ASMSubroundF( ebp, eax, ebx, ecx, edx, 7, 6) + ASMSubroundF( edx, ebp, eax, ebx, ecx, 6, 8) + ASMSubroundF( ecx, edx, ebp, eax, ebx, 2, 13) + ASMSubroundF( ebx, ecx, edx, ebp, eax, 13, 6) + ASMSubroundF( eax, ebx, ecx, edx, ebp, 14, 5) + ASMSubroundF( ebp, eax, ebx, ecx, edx, 0, 15) + ASMSubroundF( edx, ebp, eax, ebx, ecx, 3, 13) + ASMSubroundF( ecx, edx, ebp, eax, ebx, 9, 11) + ASMSubroundF( ebx, ecx, edx, ebp, eax, 11, 11) + + // advance data and store for next round + AS2( add edi, 64 ) + AS2( movd esi, mm1 ) // digest_ + AS2( movd mm0, edi ) // store + + // now edi as tmp + + // c1 = digest_[1] + c1 + d2; + AS2( add [esp + 8], edx ) // + d2 + AS2( mov edi, [esi + 4] ) // digest_[1] + AS2( add [esp + 8], edi ) + + // digest_[1] = digest_[2] + d1 + e2; + AS2( mov [esi + 4], ebp ) // e2 + AS2( mov edi, [esp + 12] ) // d1 + AS2( add edi, [esi + 8] ) // digest_[2] + AS2( add [esi + 4], edi ) + + // digest_[2] = digest_[3] + e1 + a2; + AS2( mov [esi + 8], eax ) // a2 + AS2( mov edi, [esp + 16] ) // e1 + AS2( add edi, [esi + 12] ) // digest_[3] + AS2( add [esi + 8], edi ) + + // digest_[3] = digest_[4] + a1 + b2; + AS2( mov [esi + 12], ebx ) // b2 + AS2( mov edi, [esp] ) // a1 + AS2( add edi, [esi + 16] ) // digest_[4] + AS2( add [esi + 12], edi ) + + // digest_[4] = digest_[0] + b1 + c2; + AS2( mov [esi + 16], ecx ) // c2 + AS2( mov edi, [esp + 4] ) // b1 + AS2( add edi, [esi] ) // digest_[0] + AS2( add [esi + 16], edi ) + + // digest_[0] = c1; + AS2( mov edi, [esp + 8] ) // c1 + AS2( mov [esi], edi ) + + // setup for loop back + AS2( movd edx, mm2 ) // times + AS2( movd edi, mm0 ) // data, already advanced + AS1( dec edx ) + AS1( jnz loopStart ) + + + EPILOG() +} + + +#endif // DO_RIPEMD_ASM + + +} // namespace TaoCrypt diff --git a/externals/mysql/extlib/yassl/taocrypt/src/rsa.cpp b/externals/mysql/extlib/yassl/taocrypt/src/rsa.cpp new file mode 100644 index 00000000000..57d19a59c76 --- /dev/null +++ b/externals/mysql/extlib/yassl/taocrypt/src/rsa.cpp @@ -0,0 +1,210 @@ +/* + 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 rsa.cpp from CryptoPP */ + +#include "runtime.hpp" +#include "rsa.hpp" +#include "asn.hpp" +#include "modarith.hpp" + + + +namespace TaoCrypt { + + +Integer RSA_PublicKey::ApplyFunction(const Integer& x) const +{ + return a_exp_b_mod_c(x, e_, n_); +} + + +RSA_PublicKey::RSA_PublicKey(Source& source) +{ + Initialize(source); +} + + +void RSA_PublicKey::Initialize(Source& source) +{ + RSA_Public_Decoder decoder(source); + decoder.Decode(*this); +} + + +Integer RSA_PrivateKey::CalculateInverse(RandomNumberGenerator& rng, + const Integer& x) const +{ + ModularArithmetic modn(n_); + + Integer r(rng, Integer::One(), n_ - Integer::One()); + Integer re = modn.Exponentiate(r, e_); + re = modn.Multiply(re, x); // blind + + // here we follow the notation of PKCS #1 and let u=q inverse mod p + // but in ModRoot, u=p inverse mod q, so we reverse the order of p and q + + Integer y = ModularRoot(re, dq_, dp_, q_, p_, u_); + y = modn.Divide(y, r); // unblind + assert(modn.Exponentiate(y, e_) == x); // check + + return y; +} + + +RSA_PrivateKey::RSA_PrivateKey(Source& source) +{ + Initialize(source); +} + + +void RSA_PrivateKey::Initialize(Source& source) +{ + RSA_Private_Decoder decoder(source); + decoder.Decode(*this); +} + + +void RSA_BlockType2::Pad(const byte *input, word32 inputLen, byte *pkcsBlock, + word32 pkcsBlockLen, RandomNumberGenerator& rng) const +{ + // convert from bit length to byte length + if (pkcsBlockLen % 8 != 0) + { + pkcsBlock[0] = 0; + pkcsBlock++; + } + pkcsBlockLen /= 8; + + pkcsBlock[0] = 2; // block type 2 + + // pad with non-zero random bytes + word32 padLen = pkcsBlockLen - inputLen - 1; + rng.GenerateBlock(&pkcsBlock[1], padLen); + for (word32 i = 1; i < padLen; i++) + if (pkcsBlock[i] == 0) pkcsBlock[i] = 0x01; + + pkcsBlock[pkcsBlockLen-inputLen-1] = 0; // separator + memcpy(pkcsBlock+pkcsBlockLen-inputLen, input, inputLen); +} + +word32 RSA_BlockType2::UnPad(const byte *pkcsBlock, unsigned int pkcsBlockLen, + byte *output) const +{ + bool invalid = false; + unsigned int maxOutputLen = SaturatingSubtract(pkcsBlockLen / 8, 10U); + + // convert from bit length to byte length + if (pkcsBlockLen % 8 != 0) + { + invalid = (pkcsBlock[0] != 0) || invalid; + pkcsBlock++; + } + pkcsBlockLen /= 8; + + // Require block type 2. + invalid = (pkcsBlock[0] != 2) || invalid; + + // skip past the padding until we find the separator + unsigned i=1; + while (i<pkcsBlockLen && pkcsBlock[i++]) { // null body + } + assert(i==pkcsBlockLen || pkcsBlock[i-1]==0); + + unsigned int outputLen = pkcsBlockLen - i; + invalid = (outputLen > maxOutputLen) || invalid; + + if (invalid) + return 0; + + memcpy (output, pkcsBlock+i, outputLen); + return outputLen; +} + + +void RSA_BlockType1::Pad(const byte* input, word32 inputLen, byte* pkcsBlock, + word32 pkcsBlockLen, RandomNumberGenerator&) const +{ + // convert from bit length to byte length + if (pkcsBlockLen % 8 != 0) + { + pkcsBlock[0] = 0; + pkcsBlock++; + } + pkcsBlockLen /= 8; + + pkcsBlock[0] = 1; // block type 1 for SSL + + // pad with 0xff bytes + memset(&pkcsBlock[1], 0xFF, pkcsBlockLen - inputLen - 2); + + pkcsBlock[pkcsBlockLen-inputLen-1] = 0; // separator + memcpy(pkcsBlock+pkcsBlockLen-inputLen, input, inputLen); +} + + +word32 RSA_BlockType1::UnPad(const byte* pkcsBlock, word32 pkcsBlockLen, + byte* output) const +{ + bool invalid = false; + unsigned int maxOutputLen = SaturatingSubtract(pkcsBlockLen / 8, 10U); + + // convert from bit length to byte length + if (pkcsBlockLen % 8 != 0) + { + invalid = (pkcsBlock[0] != 0) || invalid; + pkcsBlock++; + } + pkcsBlockLen /= 8; + + // Require block type 1 for SSL. + invalid = (pkcsBlock[0] != 1) || invalid; + + // skip past the padding until we find the separator + unsigned i=1; + while (i<pkcsBlockLen && pkcsBlock[i++]) { // null body + } + assert(i==pkcsBlockLen || pkcsBlock[i-1]==0); + + unsigned int outputLen = pkcsBlockLen - i; + invalid = (outputLen > maxOutputLen) || invalid; + + if (invalid) + return 0; + + memcpy(output, pkcsBlock+i, outputLen); + return outputLen; +} + + +word32 SSL_Decrypt(const RSA_PublicKey& key, const byte* sig, byte* plain) +{ + PK_Lengths lengths(key.GetModulus()); + + ByteBlock paddedBlock(BitsToBytes(lengths.PaddedBlockBitLength())); + Integer x = key.ApplyFunction(Integer(sig, + lengths.FixedCiphertextLength())); + if (x.ByteCount() > paddedBlock.size()) + x = Integer::Zero(); + x.Encode(paddedBlock.get_buffer(), paddedBlock.size()); + return RSA_BlockType1().UnPad(paddedBlock.get_buffer(), + lengths.PaddedBlockBitLength(), plain); +} + + +} // namespace diff --git a/externals/mysql/extlib/yassl/taocrypt/src/sha.cpp b/externals/mysql/extlib/yassl/taocrypt/src/sha.cpp new file mode 100644 index 00000000000..ef165a342ad --- /dev/null +++ b/externals/mysql/extlib/yassl/taocrypt/src/sha.cpp @@ -0,0 +1,1023 @@ +/* + 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 sha.cpp from CryptoPP */ + +#include "runtime.hpp" +#include <string.h> +#include "sha.hpp" +#ifdef USE_SYS_STL + #include <algorithm> +#else + #include "algorithm.hpp" +#endif + + +namespace STL = STL_NAMESPACE; + + + +namespace TaoCrypt { + +#define blk0(i) (W[i] = buffer_[i]) +#define blk1(i) (W[i&15] = \ + rotlFixed(W[(i+13)&15]^W[(i+8)&15]^W[(i+2)&15]^W[i&15],1)) + +#define f1(x,y,z) (z^(x &(y^z))) +#define f2(x,y,z) (x^y^z) +#define f3(x,y,z) ((x&y)|(z&(x|y))) +#define f4(x,y,z) (x^y^z) + +// (R0+R1), R2, R3, R4 are the different operations used in SHA1 +#define R0(v,w,x,y,z,i) z+= f1(w,x,y) + blk0(i) + 0x5A827999+ \ + rotlFixed(v,5); w = rotlFixed(w,30); +#define R1(v,w,x,y,z,i) z+= f1(w,x,y) + blk1(i) + 0x5A827999+ \ + rotlFixed(v,5); w = rotlFixed(w,30); +#define R2(v,w,x,y,z,i) z+= f2(w,x,y) + blk1(i) + 0x6ED9EBA1+ \ + rotlFixed(v,5); w = rotlFixed(w,30); +#define R3(v,w,x,y,z,i) z+= f3(w,x,y) + blk1(i) + 0x8F1BBCDC+ \ + rotlFixed(v,5); w = rotlFixed(w,30); +#define R4(v,w,x,y,z,i) z+= f4(w,x,y) + blk1(i) + 0xCA62C1D6+ \ + rotlFixed(v,5); w = rotlFixed(w,30); + + +void SHA::Init() +{ + digest_[0] = 0x67452301L; + digest_[1] = 0xEFCDAB89L; + digest_[2] = 0x98BADCFEL; + digest_[3] = 0x10325476L; + digest_[4] = 0xC3D2E1F0L; + + buffLen_ = 0; + loLen_ = 0; + hiLen_ = 0; +} + +void SHA256::Init() +{ + digest_[0] = 0x6A09E667L; + digest_[1] = 0xBB67AE85L; + digest_[2] = 0x3C6EF372L; + digest_[3] = 0xA54FF53AL; + digest_[4] = 0x510E527FL; + digest_[5] = 0x9B05688CL; + digest_[6] = 0x1F83D9ABL; + digest_[7] = 0x5BE0CD19L; + + buffLen_ = 0; + loLen_ = 0; + hiLen_ = 0; +} + + +void SHA224::Init() +{ + digest_[0] = 0xc1059ed8; + digest_[1] = 0x367cd507; + digest_[2] = 0x3070dd17; + digest_[3] = 0xf70e5939; + digest_[4] = 0xffc00b31; + digest_[5] = 0x68581511; + digest_[6] = 0x64f98fa7; + digest_[7] = 0xbefa4fa4; + + buffLen_ = 0; + loLen_ = 0; + hiLen_ = 0; +} + + +#ifdef WORD64_AVAILABLE + +void SHA512::Init() +{ + digest_[0] = W64LIT(0x6a09e667f3bcc908); + digest_[1] = W64LIT(0xbb67ae8584caa73b); + digest_[2] = W64LIT(0x3c6ef372fe94f82b); + digest_[3] = W64LIT(0xa54ff53a5f1d36f1); + digest_[4] = W64LIT(0x510e527fade682d1); + digest_[5] = W64LIT(0x9b05688c2b3e6c1f); + digest_[6] = W64LIT(0x1f83d9abfb41bd6b); + digest_[7] = W64LIT(0x5be0cd19137e2179); + + buffLen_ = 0; + loLen_ = 0; + hiLen_ = 0; +} + + +void SHA384::Init() +{ + digest_[0] = W64LIT(0xcbbb9d5dc1059ed8); + digest_[1] = W64LIT(0x629a292a367cd507); + digest_[2] = W64LIT(0x9159015a3070dd17); + digest_[3] = W64LIT(0x152fecd8f70e5939); + digest_[4] = W64LIT(0x67332667ffc00b31); + digest_[5] = W64LIT(0x8eb44a8768581511); + digest_[6] = W64LIT(0xdb0c2e0d64f98fa7); + digest_[7] = W64LIT(0x47b5481dbefa4fa4); + + buffLen_ = 0; + loLen_ = 0; + hiLen_ = 0; +} + +#endif // WORD64_AVAILABLE + + +SHA::SHA(const SHA& that) : HASHwithTransform(DIGEST_SIZE / sizeof(word32), + BLOCK_SIZE) +{ + buffLen_ = that.buffLen_; + loLen_ = that.loLen_; + hiLen_ = that.hiLen_; + + memcpy(digest_, that.digest_, DIGEST_SIZE); + memcpy(buffer_, that.buffer_, BLOCK_SIZE); +} + + +SHA256::SHA256(const SHA256& that) : HASHwithTransform(DIGEST_SIZE / + sizeof(word32), BLOCK_SIZE) +{ + buffLen_ = that.buffLen_; + loLen_ = that.loLen_; + hiLen_ = that.hiLen_; + + memcpy(digest_, that.digest_, DIGEST_SIZE); + memcpy(buffer_, that.buffer_, BLOCK_SIZE); +} + + +SHA224::SHA224(const SHA224& that) : HASHwithTransform(SHA256::DIGEST_SIZE / + sizeof(word32), BLOCK_SIZE) +{ + buffLen_ = that.buffLen_; + loLen_ = that.loLen_; + hiLen_ = that.hiLen_; + + memcpy(digest_, that.digest_, DIGEST_SIZE); + memcpy(buffer_, that.buffer_, BLOCK_SIZE); +} + + +#ifdef WORD64_AVAILABLE + +SHA512::SHA512(const SHA512& that) : HASH64withTransform(DIGEST_SIZE / + sizeof(word64), BLOCK_SIZE) +{ + buffLen_ = that.buffLen_; + loLen_ = that.loLen_; + hiLen_ = that.hiLen_; + + memcpy(digest_, that.digest_, DIGEST_SIZE); + memcpy(buffer_, that.buffer_, BLOCK_SIZE); +} + + +SHA384::SHA384(const SHA384& that) : HASH64withTransform(SHA512::DIGEST_SIZE / + sizeof(word64), BLOCK_SIZE) +{ + buffLen_ = that.buffLen_; + loLen_ = that.loLen_; + hiLen_ = that.hiLen_; + + memcpy(digest_, that.digest_, DIGEST_SIZE); + memcpy(buffer_, that.buffer_, BLOCK_SIZE); +} + +#endif // WORD64_AVAILABLE + + +SHA& SHA::operator= (const SHA& that) +{ + SHA tmp(that); + Swap(tmp); + + return *this; +} + + +SHA256& SHA256::operator= (const SHA256& that) +{ + SHA256 tmp(that); + Swap(tmp); + + return *this; +} + + +SHA224& SHA224::operator= (const SHA224& that) +{ + SHA224 tmp(that); + Swap(tmp); + + return *this; +} + + +#ifdef WORD64_AVAILABLE + +SHA512& SHA512::operator= (const SHA512& that) +{ + SHA512 tmp(that); + Swap(tmp); + + return *this; +} + + +SHA384& SHA384::operator= (const SHA384& that) +{ + SHA384 tmp(that); + Swap(tmp); + + return *this; +} + +#endif // WORD64_AVAILABLE + + +void SHA::Swap(SHA& other) +{ + STL::swap(loLen_, other.loLen_); + STL::swap(hiLen_, other.hiLen_); + STL::swap(buffLen_, other.buffLen_); + + memcpy(digest_, other.digest_, DIGEST_SIZE); + memcpy(buffer_, other.buffer_, BLOCK_SIZE); +} + + +void SHA256::Swap(SHA256& other) +{ + STL::swap(loLen_, other.loLen_); + STL::swap(hiLen_, other.hiLen_); + STL::swap(buffLen_, other.buffLen_); + + memcpy(digest_, other.digest_, DIGEST_SIZE); + memcpy(buffer_, other.buffer_, BLOCK_SIZE); +} + + +void SHA224::Swap(SHA224& other) +{ + STL::swap(loLen_, other.loLen_); + STL::swap(hiLen_, other.hiLen_); + STL::swap(buffLen_, other.buffLen_); + + memcpy(digest_, other.digest_, DIGEST_SIZE); + memcpy(buffer_, other.buffer_, BLOCK_SIZE); +} + + +#ifdef WORD64_AVAILABLE + +void SHA512::Swap(SHA512& other) +{ + STL::swap(loLen_, other.loLen_); + STL::swap(hiLen_, other.hiLen_); + STL::swap(buffLen_, other.buffLen_); + + memcpy(digest_, other.digest_, DIGEST_SIZE); + memcpy(buffer_, other.buffer_, BLOCK_SIZE); +} + + +void SHA384::Swap(SHA384& other) +{ + STL::swap(loLen_, other.loLen_); + STL::swap(hiLen_, other.hiLen_); + STL::swap(buffLen_, other.buffLen_); + + memcpy(digest_, other.digest_, DIGEST_SIZE); + memcpy(buffer_, other.buffer_, BLOCK_SIZE); +} + +#endif // WORD64_AVIALABLE + + +#ifdef DO_SHA_ASM + +// Update digest with data of size len +void SHA::Update(const byte* data, word32 len) +{ + if (!isMMX) { + HASHwithTransform::Update(data, len); + return; + } + + byte* local = reinterpret_cast<byte*>(buffer_); + + // remove buffered data if possible + if (buffLen_) { + word32 add = min(len, BLOCK_SIZE - buffLen_); + memcpy(&local[buffLen_], data, add); + + buffLen_ += add; + data += add; + len -= add; + + if (buffLen_ == BLOCK_SIZE) { + ByteReverse(local, local, BLOCK_SIZE); + Transform(); + AddLength(BLOCK_SIZE); + buffLen_ = 0; + } + } + + // all at once for asm + if (buffLen_ == 0) { + word32 times = len / BLOCK_SIZE; + if (times) { + AsmTransform(data, times); + const word32 add = BLOCK_SIZE * times; + AddLength(add); + len -= add; + data += add; + } + } + + // cache any data left + if (len) { + memcpy(&local[buffLen_], data, len); + buffLen_ += len; + } +} + +#endif // DO_SHA_ASM + + +void SHA::Transform() +{ + word32 W[BLOCK_SIZE / sizeof(word32)]; + + // Copy context->state[] to working vars + word32 a = digest_[0]; + word32 b = digest_[1]; + word32 c = digest_[2]; + word32 d = digest_[3]; + word32 e = digest_[4]; + + // 4 rounds of 20 operations each. Loop unrolled. + R0(a,b,c,d,e, 0); R0(e,a,b,c,d, 1); R0(d,e,a,b,c, 2); R0(c,d,e,a,b, 3); + R0(b,c,d,e,a, 4); R0(a,b,c,d,e, 5); R0(e,a,b,c,d, 6); R0(d,e,a,b,c, 7); + R0(c,d,e,a,b, 8); R0(b,c,d,e,a, 9); R0(a,b,c,d,e,10); R0(e,a,b,c,d,11); + R0(d,e,a,b,c,12); R0(c,d,e,a,b,13); R0(b,c,d,e,a,14); R0(a,b,c,d,e,15); + + R1(e,a,b,c,d,16); R1(d,e,a,b,c,17); R1(c,d,e,a,b,18); R1(b,c,d,e,a,19); + + R2(a,b,c,d,e,20); R2(e,a,b,c,d,21); R2(d,e,a,b,c,22); R2(c,d,e,a,b,23); + R2(b,c,d,e,a,24); R2(a,b,c,d,e,25); R2(e,a,b,c,d,26); R2(d,e,a,b,c,27); + R2(c,d,e,a,b,28); R2(b,c,d,e,a,29); R2(a,b,c,d,e,30); R2(e,a,b,c,d,31); + R2(d,e,a,b,c,32); R2(c,d,e,a,b,33); R2(b,c,d,e,a,34); R2(a,b,c,d,e,35); + R2(e,a,b,c,d,36); R2(d,e,a,b,c,37); R2(c,d,e,a,b,38); R2(b,c,d,e,a,39); + + R3(a,b,c,d,e,40); R3(e,a,b,c,d,41); R3(d,e,a,b,c,42); R3(c,d,e,a,b,43); + R3(b,c,d,e,a,44); R3(a,b,c,d,e,45); R3(e,a,b,c,d,46); R3(d,e,a,b,c,47); + R3(c,d,e,a,b,48); R3(b,c,d,e,a,49); R3(a,b,c,d,e,50); R3(e,a,b,c,d,51); + R3(d,e,a,b,c,52); R3(c,d,e,a,b,53); R3(b,c,d,e,a,54); R3(a,b,c,d,e,55); + R3(e,a,b,c,d,56); R3(d,e,a,b,c,57); R3(c,d,e,a,b,58); R3(b,c,d,e,a,59); + + R4(a,b,c,d,e,60); R4(e,a,b,c,d,61); R4(d,e,a,b,c,62); R4(c,d,e,a,b,63); + R4(b,c,d,e,a,64); R4(a,b,c,d,e,65); R4(e,a,b,c,d,66); R4(d,e,a,b,c,67); + R4(c,d,e,a,b,68); R4(b,c,d,e,a,69); R4(a,b,c,d,e,70); R4(e,a,b,c,d,71); + R4(d,e,a,b,c,72); R4(c,d,e,a,b,73); R4(b,c,d,e,a,74); R4(a,b,c,d,e,75); + R4(e,a,b,c,d,76); R4(d,e,a,b,c,77); R4(c,d,e,a,b,78); R4(b,c,d,e,a,79); + + // Add the working vars back into digest state[] + digest_[0] += a; + digest_[1] += b; + digest_[2] += c; + digest_[3] += d; + digest_[4] += e; + + // Wipe variables + a = b = c = d = e = 0; + memset(W, 0, sizeof(W)); +} + + +#define blk2(i) (W[i&15]+=s1(W[(i-2)&15])+W[(i-7)&15]+s0(W[(i-15)&15])) + +#define Ch(x,y,z) (z^(x&(y^z))) +#define Maj(x,y,z) ((x&y)|(z&(x|y))) + +#define a(i) T[(0-i)&7] +#define b(i) T[(1-i)&7] +#define c(i) T[(2-i)&7] +#define d(i) T[(3-i)&7] +#define e(i) T[(4-i)&7] +#define f(i) T[(5-i)&7] +#define g(i) T[(6-i)&7] +#define h(i) T[(7-i)&7] + +#define R(i) h(i)+=S1(e(i))+Ch(e(i),f(i),g(i))+K[i+j]+(j?blk2(i):blk0(i));\ + d(i)+=h(i);h(i)+=S0(a(i))+Maj(a(i),b(i),c(i)) + +// for SHA256 +#define S0(x) (rotrFixed(x,2)^rotrFixed(x,13)^rotrFixed(x,22)) +#define S1(x) (rotrFixed(x,6)^rotrFixed(x,11)^rotrFixed(x,25)) +#define s0(x) (rotrFixed(x,7)^rotrFixed(x,18)^(x>>3)) +#define s1(x) (rotrFixed(x,17)^rotrFixed(x,19)^(x>>10)) + + +static const word32 K256[64] = { + 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, + 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5, + 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, + 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174, + 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, + 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da, + 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, + 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967, + 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, + 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85, + 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, + 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070, + 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, + 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3, + 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, + 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2 +}; + + +static void Transform256(word32* digest_, word32* buffer_) +{ + const word32* K = K256; + + word32 W[16]; + word32 T[8]; + + // Copy digest to working vars + memcpy(T, digest_, sizeof(T)); + + // 64 operations, partially loop unrolled + for (unsigned int j = 0; j < 64; j += 16) { + R( 0); R( 1); R( 2); R( 3); + R( 4); R( 5); R( 6); R( 7); + R( 8); R( 9); R(10); R(11); + R(12); R(13); R(14); R(15); + } + + // Add the working vars back into digest + digest_[0] += a(0); + digest_[1] += b(0); + digest_[2] += c(0); + digest_[3] += d(0); + digest_[4] += e(0); + digest_[5] += f(0); + digest_[6] += g(0); + digest_[7] += h(0); + + // Wipe variables + memset(W, 0, sizeof(W)); + memset(T, 0, sizeof(T)); +} + + +// undef for 256 +#undef S0 +#undef S1 +#undef s0 +#undef s1 + + +void SHA256::Transform() +{ + Transform256(digest_, buffer_); +} + + +void SHA224::Transform() +{ + Transform256(digest_, buffer_); +} + + +#ifdef WORD64_AVAILABLE + +static const word64 K512[80] = { + W64LIT(0x428a2f98d728ae22), W64LIT(0x7137449123ef65cd), + W64LIT(0xb5c0fbcfec4d3b2f), W64LIT(0xe9b5dba58189dbbc), + W64LIT(0x3956c25bf348b538), W64LIT(0x59f111f1b605d019), + W64LIT(0x923f82a4af194f9b), W64LIT(0xab1c5ed5da6d8118), + W64LIT(0xd807aa98a3030242), W64LIT(0x12835b0145706fbe), + W64LIT(0x243185be4ee4b28c), W64LIT(0x550c7dc3d5ffb4e2), + W64LIT(0x72be5d74f27b896f), W64LIT(0x80deb1fe3b1696b1), + W64LIT(0x9bdc06a725c71235), W64LIT(0xc19bf174cf692694), + W64LIT(0xe49b69c19ef14ad2), W64LIT(0xefbe4786384f25e3), + W64LIT(0x0fc19dc68b8cd5b5), W64LIT(0x240ca1cc77ac9c65), + W64LIT(0x2de92c6f592b0275), W64LIT(0x4a7484aa6ea6e483), + W64LIT(0x5cb0a9dcbd41fbd4), W64LIT(0x76f988da831153b5), + W64LIT(0x983e5152ee66dfab), W64LIT(0xa831c66d2db43210), + W64LIT(0xb00327c898fb213f), W64LIT(0xbf597fc7beef0ee4), + W64LIT(0xc6e00bf33da88fc2), W64LIT(0xd5a79147930aa725), + W64LIT(0x06ca6351e003826f), W64LIT(0x142929670a0e6e70), + W64LIT(0x27b70a8546d22ffc), W64LIT(0x2e1b21385c26c926), + W64LIT(0x4d2c6dfc5ac42aed), W64LIT(0x53380d139d95b3df), + W64LIT(0x650a73548baf63de), W64LIT(0x766a0abb3c77b2a8), + W64LIT(0x81c2c92e47edaee6), W64LIT(0x92722c851482353b), + W64LIT(0xa2bfe8a14cf10364), W64LIT(0xa81a664bbc423001), + W64LIT(0xc24b8b70d0f89791), W64LIT(0xc76c51a30654be30), + W64LIT(0xd192e819d6ef5218), W64LIT(0xd69906245565a910), + W64LIT(0xf40e35855771202a), W64LIT(0x106aa07032bbd1b8), + W64LIT(0x19a4c116b8d2d0c8), W64LIT(0x1e376c085141ab53), + W64LIT(0x2748774cdf8eeb99), W64LIT(0x34b0bcb5e19b48a8), + W64LIT(0x391c0cb3c5c95a63), W64LIT(0x4ed8aa4ae3418acb), + W64LIT(0x5b9cca4f7763e373), W64LIT(0x682e6ff3d6b2b8a3), + W64LIT(0x748f82ee5defb2fc), W64LIT(0x78a5636f43172f60), + W64LIT(0x84c87814a1f0ab72), W64LIT(0x8cc702081a6439ec), + W64LIT(0x90befffa23631e28), W64LIT(0xa4506cebde82bde9), + W64LIT(0xbef9a3f7b2c67915), W64LIT(0xc67178f2e372532b), + W64LIT(0xca273eceea26619c), W64LIT(0xd186b8c721c0c207), + W64LIT(0xeada7dd6cde0eb1e), W64LIT(0xf57d4f7fee6ed178), + W64LIT(0x06f067aa72176fba), W64LIT(0x0a637dc5a2c898a6), + W64LIT(0x113f9804bef90dae), W64LIT(0x1b710b35131c471b), + W64LIT(0x28db77f523047d84), W64LIT(0x32caab7b40c72493), + W64LIT(0x3c9ebe0a15c9bebc), W64LIT(0x431d67c49c100d4c), + W64LIT(0x4cc5d4becb3e42b6), W64LIT(0x597f299cfc657e2a), + W64LIT(0x5fcb6fab3ad6faec), W64LIT(0x6c44198c4a475817) +}; + + +// for SHA512 +#define S0(x) (rotrFixed(x,28)^rotrFixed(x,34)^rotrFixed(x,39)) +#define S1(x) (rotrFixed(x,14)^rotrFixed(x,18)^rotrFixed(x,41)) +#define s0(x) (rotrFixed(x,1)^rotrFixed(x,8)^(x>>7)) +#define s1(x) (rotrFixed(x,19)^rotrFixed(x,61)^(x>>6)) + + +static void Transform512(word64* digest_, word64* buffer_) +{ + const word64* K = K512; + + word64 W[16]; + word64 T[8]; + + // Copy digest to working vars + memcpy(T, digest_, sizeof(T)); + + // 64 operations, partially loop unrolled + for (unsigned int j = 0; j < 80; j += 16) { + R( 0); R( 1); R( 2); R( 3); + R( 4); R( 5); R( 6); R( 7); + R( 8); R( 9); R(10); R(11); + R(12); R(13); R(14); R(15); + } + + // Add the working vars back into digest + + digest_[0] += a(0); + digest_[1] += b(0); + digest_[2] += c(0); + digest_[3] += d(0); + digest_[4] += e(0); + digest_[5] += f(0); + digest_[6] += g(0); + digest_[7] += h(0); + + // Wipe variables + memset(W, 0, sizeof(W)); + memset(T, 0, sizeof(T)); +} + + +void SHA512::Transform() +{ + Transform512(digest_, buffer_); +} + + +void SHA384::Transform() +{ + Transform512(digest_, buffer_); +} + +#endif // WORD64_AVIALABLE + + +#ifdef DO_SHA_ASM + +// f1(x,y,z) (z^(x &(y^z))) +// place in esi +#define ASMf1(x,y,z) \ + AS2( mov esi, y ) \ + AS2( xor esi, z ) \ + AS2( and esi, x ) \ + AS2( xor esi, z ) + + +// R0(v,w,x,y,z,i) = +// z+= f1(w,x,y) + W[i] + 0x5A827999 + rotlFixed(v,5); +// w = rotlFixed(w,30); + +// use esi for f +// use edi as tmp + + +#define ASMR0(v,w,x,y,z,i) \ + AS2( mov esi, x ) \ + AS2( mov edi, [esp + i * 4] ) \ + AS2( xor esi, y ) \ + AS2( and esi, w ) \ + AS2( lea z, [edi + z + 0x5A827999] ) \ + AS2( mov edi, v ) \ + AS2( xor esi, y ) \ + AS2( rol edi, 5 ) \ + AS2( add z, esi ) \ + AS2( rol w, 30 ) \ + AS2( add z, edi ) + + +/* Some macro stuff, but older gas ( < 2,16 ) can't process &, so do by hand + % won't work on gas at all + +#define xstr(s) str(s) +#define str(s) #s + +#define WOFF1(a) ( a & 15) +#define WOFF2(a) ((a + 2) & 15) +#define WOFF3(a) ((a + 8) & 15) +#define WOFF4(a) ((a + 13) & 15) + +#ifdef __GNUC__ + #define WGET1(i) asm("mov esp, [edi - "xstr(WOFF1(i))" * 4] "); + #define WGET2(i) asm("xor esp, [edi - "xstr(WOFF2(i))" * 4] "); + #define WGET3(i) asm("xor esp, [edi - "xstr(WOFF3(i))" * 4] "); + #define WGET4(i) asm("xor esp, [edi - "xstr(WOFF4(i))" * 4] "); + #define WPUT1(i) asm("mov [edi - "xstr(WOFF1(i))" * 4], esp "); +#else + #define WGET1(i) AS2( mov esp, [edi - WOFF1(i) * 4] ) + #define WGET2(i) AS2( xor esp, [edi - WOFF2(i) * 4] ) + #define WGET3(i) AS2( xor esp, [edi - WOFF3(i) * 4] ) + #define WGET4(i) AS2( xor esp, [edi - WOFF4(i) * 4] ) + #define WPUT1(i) AS2( mov [edi - WOFF1(i) * 4], esp ) +#endif +*/ + +// ASMR1 = ASMR0 but use esp for W calcs + +#define ASMR1(v,w,x,y,z,i,W1,W2,W3,W4) \ + AS2( mov edi, [esp + W1 * 4] ) \ + AS2( mov esi, x ) \ + AS2( xor edi, [esp + W2 * 4] ) \ + AS2( xor esi, y ) \ + AS2( xor edi, [esp + W3 * 4] ) \ + AS2( and esi, w ) \ + AS2( xor edi, [esp + W4 * 4] ) \ + AS2( rol edi, 1 ) \ + AS2( xor esi, y ) \ + AS2( mov [esp + W1 * 4], edi ) \ + AS2( lea z, [edi + z + 0x5A827999] ) \ + AS2( mov edi, v ) \ + AS2( rol edi, 5 ) \ + AS2( add z, esi ) \ + AS2( rol w, 30 ) \ + AS2( add z, edi ) + + +// ASMR2 = ASMR1 but f is xor, xor instead + +#define ASMR2(v,w,x,y,z,i,W1,W2,W3,W4) \ + AS2( mov edi, [esp + W1 * 4] ) \ + AS2( mov esi, x ) \ + AS2( xor edi, [esp + W2 * 4] ) \ + AS2( xor esi, y ) \ + AS2( xor edi, [esp + W3 * 4] ) \ + AS2( xor esi, w ) \ + AS2( xor edi, [esp + W4 * 4] ) \ + AS2( rol edi, 1 ) \ + AS2( add z, esi ) \ + AS2( mov [esp + W1 * 4], edi ) \ + AS2( lea z, [edi + z + 0x6ED9EBA1] ) \ + AS2( mov edi, v ) \ + AS2( rol edi, 5 ) \ + AS2( rol w, 30 ) \ + AS2( add z, edi ) + + +// ASMR3 = ASMR2 but f is (x&y)|(z&(x|y)) +// which is (w&x)|(y&(w|x)) + +#define ASMR3(v,w,x,y,z,i,W1,W2,W3,W4) \ + AS2( mov edi, [esp + W1 * 4] ) \ + AS2( mov esi, x ) \ + AS2( xor edi, [esp + W2 * 4] ) \ + AS2( or esi, w ) \ + AS2( xor edi, [esp + W3 * 4] ) \ + AS2( and esi, y ) \ + AS2( xor edi, [esp + W4 * 4] ) \ + AS2( movd mm0, esi ) \ + AS2( rol edi, 1 ) \ + AS2( mov esi, x ) \ + AS2( mov [esp + W1 * 4], edi ) \ + AS2( and esi, w ) \ + AS2( lea z, [edi + z + 0x8F1BBCDC] ) \ + AS2( movd edi, mm0 ) \ + AS2( or esi, edi ) \ + AS2( mov edi, v ) \ + AS2( rol edi, 5 ) \ + AS2( add z, esi ) \ + AS2( rol w, 30 ) \ + AS2( add z, edi ) + + +// ASMR4 = ASMR2 but different constant + +#define ASMR4(v,w,x,y,z,i,W1,W2,W3,W4) \ + AS2( mov edi, [esp + W1 * 4] ) \ + AS2( mov esi, x ) \ + AS2( xor edi, [esp + W2 * 4] ) \ + AS2( xor esi, y ) \ + AS2( xor edi, [esp + W3 * 4] ) \ + AS2( xor esi, w ) \ + AS2( xor edi, [esp + W4 * 4] ) \ + AS2( rol edi, 1 ) \ + AS2( add z, esi ) \ + AS2( mov [esp + W1 * 4], edi ) \ + AS2( lea z, [edi + z + 0xCA62C1D6] ) \ + AS2( mov edi, v ) \ + AS2( rol edi, 5 ) \ + AS2( rol w, 30 ) \ + AS2( add z, edi ) + + +#ifdef _MSC_VER + __declspec(naked) +#endif +void SHA::AsmTransform(const byte* data, word32 times) +{ +#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 eax, DWORD PTR [ebp + 16] ) + + #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, data ) \ + AS2( mov eax, times ) + + #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 8 ) +#endif + + PROLOG() + + AS2( mov esi, ecx ) + + #ifdef OLD_GCC_OFFSET + AS2( add esi, 20 ) // digest_[0] + #else + AS2( add esi, 16 ) // digest_[0] + #endif + + AS2( movd mm2, eax ) // store times_ + AS2( movd mm1, esi ) // store digest_ + + AS2( sub esp, 68 ) // make room on stack + +AS1( loopStart: ) + + // byte reverse 16 words of input, 4 at a time, put on stack for W[] + + // part 1 + AS2( mov eax, [edi] ) + AS2( mov ebx, [edi + 4] ) + AS2( mov ecx, [edi + 8] ) + AS2( mov edx, [edi + 12] ) + + AS1( bswap eax ) + AS1( bswap ebx ) + AS1( bswap ecx ) + AS1( bswap edx ) + + AS2( mov [esp], eax ) + AS2( mov [esp + 4], ebx ) + AS2( mov [esp + 8], ecx ) + AS2( mov [esp + 12], edx ) + + // part 2 + AS2( mov eax, [edi + 16] ) + AS2( mov ebx, [edi + 20] ) + AS2( mov ecx, [edi + 24] ) + AS2( mov edx, [edi + 28] ) + + AS1( bswap eax ) + AS1( bswap ebx ) + AS1( bswap ecx ) + AS1( bswap edx ) + + AS2( mov [esp + 16], eax ) + AS2( mov [esp + 20], ebx ) + AS2( mov [esp + 24], ecx ) + AS2( mov [esp + 28], edx ) + + + // part 3 + AS2( mov eax, [edi + 32] ) + AS2( mov ebx, [edi + 36] ) + AS2( mov ecx, [edi + 40] ) + AS2( mov edx, [edi + 44] ) + + AS1( bswap eax ) + AS1( bswap ebx ) + AS1( bswap ecx ) + AS1( bswap edx ) + + AS2( mov [esp + 32], eax ) + AS2( mov [esp + 36], ebx ) + AS2( mov [esp + 40], ecx ) + AS2( mov [esp + 44], edx ) + + + // part 4 + AS2( mov eax, [edi + 48] ) + AS2( mov ebx, [edi + 52] ) + AS2( mov ecx, [edi + 56] ) + AS2( mov edx, [edi + 60] ) + + AS1( bswap eax ) + AS1( bswap ebx ) + AS1( bswap ecx ) + AS1( bswap edx ) + + AS2( mov [esp + 48], eax ) + AS2( mov [esp + 52], ebx ) + AS2( mov [esp + 56], ecx ) + AS2( mov [esp + 60], edx ) + + AS2( mov [esp + 64], edi ) // store edi for end + + // read from digest_ + AS2( mov eax, [esi] ) // a1 + AS2( mov ebx, [esi + 4] ) // b1 + AS2( mov ecx, [esi + 8] ) // c1 + AS2( mov edx, [esi + 12] ) // d1 + AS2( mov ebp, [esi + 16] ) // e1 + + + ASMR0(eax, ebx, ecx, edx, ebp, 0) + ASMR0(ebp, eax, ebx, ecx, edx, 1) + ASMR0(edx, ebp, eax, ebx, ecx, 2) + ASMR0(ecx, edx, ebp, eax, ebx, 3) + ASMR0(ebx, ecx, edx, ebp, eax, 4) + ASMR0(eax, ebx, ecx, edx, ebp, 5) + ASMR0(ebp, eax, ebx, ecx, edx, 6) + ASMR0(edx, ebp, eax, ebx, ecx, 7) + ASMR0(ecx, edx, ebp, eax, ebx, 8) + ASMR0(ebx, ecx, edx, ebp, eax, 9) + ASMR0(eax, ebx, ecx, edx, ebp, 10) + ASMR0(ebp, eax, ebx, ecx, edx, 11) + ASMR0(edx, ebp, eax, ebx, ecx, 12) + ASMR0(ecx, edx, ebp, eax, ebx, 13) + ASMR0(ebx, ecx, edx, ebp, eax, 14) + ASMR0(eax, ebx, ecx, edx, ebp, 15) + + ASMR1(ebp, eax, ebx, ecx, edx, 16, 0, 2, 8, 13) + ASMR1(edx, ebp, eax, ebx, ecx, 17, 1, 3, 9, 14) + ASMR1(ecx, edx, ebp, eax, ebx, 18, 2, 4, 10, 15) + ASMR1(ebx, ecx, edx, ebp, eax, 19, 3, 5, 11, 0) + + ASMR2(eax, ebx, ecx, edx, ebp, 20, 4, 6, 12, 1) + ASMR2(ebp, eax, ebx, ecx, edx, 21, 5, 7, 13, 2) + ASMR2(edx, ebp, eax, ebx, ecx, 22, 6, 8, 14, 3) + ASMR2(ecx, edx, ebp, eax, ebx, 23, 7, 9, 15, 4) + ASMR2(ebx, ecx, edx, ebp, eax, 24, 8, 10, 0, 5) + ASMR2(eax, ebx, ecx, edx, ebp, 25, 9, 11, 1, 6) + ASMR2(ebp, eax, ebx, ecx, edx, 26, 10, 12, 2, 7) + ASMR2(edx, ebp, eax, ebx, ecx, 27, 11, 13, 3, 8) + ASMR2(ecx, edx, ebp, eax, ebx, 28, 12, 14, 4, 9) + ASMR2(ebx, ecx, edx, ebp, eax, 29, 13, 15, 5, 10) + ASMR2(eax, ebx, ecx, edx, ebp, 30, 14, 0, 6, 11) + ASMR2(ebp, eax, ebx, ecx, edx, 31, 15, 1, 7, 12) + ASMR2(edx, ebp, eax, ebx, ecx, 32, 0, 2, 8, 13) + ASMR2(ecx, edx, ebp, eax, ebx, 33, 1, 3, 9, 14) + ASMR2(ebx, ecx, edx, ebp, eax, 34, 2, 4, 10, 15) + ASMR2(eax, ebx, ecx, edx, ebp, 35, 3, 5, 11, 0) + ASMR2(ebp, eax, ebx, ecx, edx, 36, 4, 6, 12, 1) + ASMR2(edx, ebp, eax, ebx, ecx, 37, 5, 7, 13, 2) + ASMR2(ecx, edx, ebp, eax, ebx, 38, 6, 8, 14, 3) + ASMR2(ebx, ecx, edx, ebp, eax, 39, 7, 9, 15, 4) + + + ASMR3(eax, ebx, ecx, edx, ebp, 40, 8, 10, 0, 5) + ASMR3(ebp, eax, ebx, ecx, edx, 41, 9, 11, 1, 6) + ASMR3(edx, ebp, eax, ebx, ecx, 42, 10, 12, 2, 7) + ASMR3(ecx, edx, ebp, eax, ebx, 43, 11, 13, 3, 8) + ASMR3(ebx, ecx, edx, ebp, eax, 44, 12, 14, 4, 9) + ASMR3(eax, ebx, ecx, edx, ebp, 45, 13, 15, 5, 10) + ASMR3(ebp, eax, ebx, ecx, edx, 46, 14, 0, 6, 11) + ASMR3(edx, ebp, eax, ebx, ecx, 47, 15, 1, 7, 12) + ASMR3(ecx, edx, ebp, eax, ebx, 48, 0, 2, 8, 13) + ASMR3(ebx, ecx, edx, ebp, eax, 49, 1, 3, 9, 14) + ASMR3(eax, ebx, ecx, edx, ebp, 50, 2, 4, 10, 15) + ASMR3(ebp, eax, ebx, ecx, edx, 51, 3, 5, 11, 0) + ASMR3(edx, ebp, eax, ebx, ecx, 52, 4, 6, 12, 1) + ASMR3(ecx, edx, ebp, eax, ebx, 53, 5, 7, 13, 2) + ASMR3(ebx, ecx, edx, ebp, eax, 54, 6, 8, 14, 3) + ASMR3(eax, ebx, ecx, edx, ebp, 55, 7, 9, 15, 4) + ASMR3(ebp, eax, ebx, ecx, edx, 56, 8, 10, 0, 5) + ASMR3(edx, ebp, eax, ebx, ecx, 57, 9, 11, 1, 6) + ASMR3(ecx, edx, ebp, eax, ebx, 58, 10, 12, 2, 7) + ASMR3(ebx, ecx, edx, ebp, eax, 59, 11, 13, 3, 8) + + ASMR4(eax, ebx, ecx, edx, ebp, 60, 12, 14, 4, 9) + ASMR4(ebp, eax, ebx, ecx, edx, 61, 13, 15, 5, 10) + ASMR4(edx, ebp, eax, ebx, ecx, 62, 14, 0, 6, 11) + ASMR4(ecx, edx, ebp, eax, ebx, 63, 15, 1, 7, 12) + ASMR4(ebx, ecx, edx, ebp, eax, 64, 0, 2, 8, 13) + ASMR4(eax, ebx, ecx, edx, ebp, 65, 1, 3, 9, 14) + ASMR4(ebp, eax, ebx, ecx, edx, 66, 2, 4, 10, 15) + ASMR4(edx, ebp, eax, ebx, ecx, 67, 3, 5, 11, 0) + ASMR4(ecx, edx, ebp, eax, ebx, 68, 4, 6, 12, 1) + ASMR4(ebx, ecx, edx, ebp, eax, 69, 5, 7, 13, 2) + ASMR4(eax, ebx, ecx, edx, ebp, 70, 6, 8, 14, 3) + ASMR4(ebp, eax, ebx, ecx, edx, 71, 7, 9, 15, 4) + ASMR4(edx, ebp, eax, ebx, ecx, 72, 8, 10, 0, 5) + ASMR4(ecx, edx, ebp, eax, ebx, 73, 9, 11, 1, 6) + ASMR4(ebx, ecx, edx, ebp, eax, 74, 10, 12, 2, 7) + ASMR4(eax, ebx, ecx, edx, ebp, 75, 11, 13, 3, 8) + ASMR4(ebp, eax, ebx, ecx, edx, 76, 12, 14, 4, 9) + ASMR4(edx, ebp, eax, ebx, ecx, 77, 13, 15, 5, 10) + ASMR4(ecx, edx, ebp, eax, ebx, 78, 14, 0, 6, 11) + ASMR4(ebx, ecx, edx, ebp, eax, 79, 15, 1, 7, 12) + + + AS2( movd esi, mm1 ) // digest_ + + AS2( add [esi], eax ) // write out + AS2( add [esi + 4], ebx ) + AS2( add [esi + 8], ecx ) + AS2( add [esi + 12], edx ) + AS2( add [esi + 16], ebp ) + + // setup next round + AS2( movd ebp, mm2 ) // times + + AS2( mov edi, DWORD PTR [esp + 64] ) // data + + AS2( add edi, 64 ) // next round of data + AS2( mov [esp + 64], edi ) // restore + + AS1( dec ebp ) + AS2( movd mm2, ebp ) + AS1( jnz loopStart ) + + + EPILOG() +} + + +#endif // DO_SHA_ASM + +} // namespace diff --git a/externals/mysql/extlib/yassl/taocrypt/src/template_instnt.cpp b/externals/mysql/extlib/yassl/taocrypt/src/template_instnt.cpp new file mode 100644 index 00000000000..390da58e191 --- /dev/null +++ b/externals/mysql/extlib/yassl/taocrypt/src/template_instnt.cpp @@ -0,0 +1,82 @@ +/* + 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 "integer.hpp" +#include "rsa.hpp" +#include "sha.hpp" +#include "md5.hpp" +#include "hmac.hpp" +#include "ripemd.hpp" +#include "pwdbased.hpp" +#include "algebra.hpp" +#include "vector.hpp" +#include "hash.hpp" + +#ifdef HAVE_EXPLICIT_TEMPLATE_INSTANTIATION +namespace TaoCrypt { + +#if defined(SSE2_INTRINSICS_AVAILABLE) +template AlignedAllocator<unsigned int>::pointer StdReallocate<unsigned int, AlignedAllocator<unsigned int> >(AlignedAllocator<unsigned int>&, unsigned int*, AlignedAllocator<unsigned int>::size_type, AlignedAllocator<unsigned int>::size_type, bool); +#endif + +template class RSA_Decryptor<RSA_BlockType2>; +template class RSA_Encryptor<RSA_BlockType1>; +template class RSA_Encryptor<RSA_BlockType2>; +template void tcDelete<HASH>(HASH*); +template void tcDelete<Integer>(Integer*); +template void tcArrayDelete<byte>(byte*); +template AllocatorWithCleanup<byte>::pointer StdReallocate<byte, AllocatorWithCleanup<byte> >(AllocatorWithCleanup<byte>&, byte*, AllocatorWithCleanup<byte>::size_type, AllocatorWithCleanup<byte>::size_type, bool); +template void tcArrayDelete<word>(word*); +template AllocatorWithCleanup<word>::pointer StdReallocate<word, AllocatorWithCleanup<word> >(AllocatorWithCleanup<word>&, word*, AllocatorWithCleanup<word>::size_type, AllocatorWithCleanup<word>::size_type, bool); + +#ifndef TAOCRYPT_SLOW_WORD64 // defined when word != word32 +template void tcArrayDelete<word32>(word32*); +template AllocatorWithCleanup<word32>::pointer StdReallocate<word32, AllocatorWithCleanup<word32> >(AllocatorWithCleanup<word32>&, word32*, AllocatorWithCleanup<word32>::size_type, AllocatorWithCleanup<word32>::size_type, bool); +#endif + +template void tcArrayDelete<char>(char*); + +template class PBKDF2_HMAC<SHA>; +template class HMAC<MD5>; +template class HMAC<SHA>; +template class HMAC<RIPEMD160>; + +} + +namespace mySTL { +template vector<TaoCrypt::Integer>* uninit_fill_n<vector<TaoCrypt::Integer>*, size_t, vector<TaoCrypt::Integer> >(vector<TaoCrypt::Integer>*, size_t, vector<TaoCrypt::Integer> const&); +template void destroy<vector<TaoCrypt::Integer>*>(vector<TaoCrypt::Integer>*, vector<TaoCrypt::Integer>*); +template TaoCrypt::Integer* uninit_copy<TaoCrypt::Integer*, TaoCrypt::Integer*>(TaoCrypt::Integer*, TaoCrypt::Integer*, TaoCrypt::Integer*); +template TaoCrypt::Integer* uninit_fill_n<TaoCrypt::Integer*, size_t, TaoCrypt::Integer>(TaoCrypt::Integer*, size_t, TaoCrypt::Integer const&); +template void destroy<TaoCrypt::Integer*>(TaoCrypt::Integer*, TaoCrypt::Integer*); +template TaoCrypt::byte* GetArrayMemory<TaoCrypt::byte>(size_t); +template void FreeArrayMemory<TaoCrypt::byte>(TaoCrypt::byte*); +template TaoCrypt::Integer* GetArrayMemory<TaoCrypt::Integer>(size_t); +template void FreeArrayMemory<TaoCrypt::Integer>(TaoCrypt::Integer*); +template vector<TaoCrypt::Integer>* GetArrayMemory<vector<TaoCrypt::Integer> >(size_t); +template void FreeArrayMemory<vector<TaoCrypt::Integer> >(vector<TaoCrypt::Integer>*); +template void FreeArrayMemory<void>(void*); +} + +#endif diff --git a/externals/mysql/extlib/yassl/taocrypt/src/tftables.cpp b/externals/mysql/extlib/yassl/taocrypt/src/tftables.cpp new file mode 100644 index 00000000000..6917507b8b2 --- /dev/null +++ b/externals/mysql/extlib/yassl/taocrypt/src/tftables.cpp @@ -0,0 +1,349 @@ +/* + 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 tftables.cpp from CryptoPP */ + +#include "runtime.hpp" +#include "twofish.hpp" + + +namespace TaoCrypt { + + +const byte Twofish::q_[2][256] = { +{ + 0xA9, 0x67, 0xB3, 0xE8, 0x04, 0xFD, 0xA3, 0x76, 0x9A, 0x92, 0x80, 0x78, + 0xE4, 0xDD, 0xD1, 0x38, 0x0D, 0xC6, 0x35, 0x98, 0x18, 0xF7, 0xEC, 0x6C, + 0x43, 0x75, 0x37, 0x26, 0xFA, 0x13, 0x94, 0x48, 0xF2, 0xD0, 0x8B, 0x30, + 0x84, 0x54, 0xDF, 0x23, 0x19, 0x5B, 0x3D, 0x59, 0xF3, 0xAE, 0xA2, 0x82, + 0x63, 0x01, 0x83, 0x2E, 0xD9, 0x51, 0x9B, 0x7C, 0xA6, 0xEB, 0xA5, 0xBE, + 0x16, 0x0C, 0xE3, 0x61, 0xC0, 0x8C, 0x3A, 0xF5, 0x73, 0x2C, 0x25, 0x0B, + 0xBB, 0x4E, 0x89, 0x6B, 0x53, 0x6A, 0xB4, 0xF1, 0xE1, 0xE6, 0xBD, 0x45, + 0xE2, 0xF4, 0xB6, 0x66, 0xCC, 0x95, 0x03, 0x56, 0xD4, 0x1C, 0x1E, 0xD7, + 0xFB, 0xC3, 0x8E, 0xB5, 0xE9, 0xCF, 0xBF, 0xBA, 0xEA, 0x77, 0x39, 0xAF, + 0x33, 0xC9, 0x62, 0x71, 0x81, 0x79, 0x09, 0xAD, 0x24, 0xCD, 0xF9, 0xD8, + 0xE5, 0xC5, 0xB9, 0x4D, 0x44, 0x08, 0x86, 0xE7, 0xA1, 0x1D, 0xAA, 0xED, + 0x06, 0x70, 0xB2, 0xD2, 0x41, 0x7B, 0xA0, 0x11, 0x31, 0xC2, 0x27, 0x90, + 0x20, 0xF6, 0x60, 0xFF, 0x96, 0x5C, 0xB1, 0xAB, 0x9E, 0x9C, 0x52, 0x1B, + 0x5F, 0x93, 0x0A, 0xEF, 0x91, 0x85, 0x49, 0xEE, 0x2D, 0x4F, 0x8F, 0x3B, + 0x47, 0x87, 0x6D, 0x46, 0xD6, 0x3E, 0x69, 0x64, 0x2A, 0xCE, 0xCB, 0x2F, + 0xFC, 0x97, 0x05, 0x7A, 0xAC, 0x7F, 0xD5, 0x1A, 0x4B, 0x0E, 0xA7, 0x5A, + 0x28, 0x14, 0x3F, 0x29, 0x88, 0x3C, 0x4C, 0x02, 0xB8, 0xDA, 0xB0, 0x17, + 0x55, 0x1F, 0x8A, 0x7D, 0x57, 0xC7, 0x8D, 0x74, 0xB7, 0xC4, 0x9F, 0x72, + 0x7E, 0x15, 0x22, 0x12, 0x58, 0x07, 0x99, 0x34, 0x6E, 0x50, 0xDE, 0x68, + 0x65, 0xBC, 0xDB, 0xF8, 0xC8, 0xA8, 0x2B, 0x40, 0xDC, 0xFE, 0x32, 0xA4, + 0xCA, 0x10, 0x21, 0xF0, 0xD3, 0x5D, 0x0F, 0x00, 0x6F, 0x9D, 0x36, 0x42, + 0x4A, 0x5E, 0xC1, 0xE0 +}, +{ + 0x75, 0xF3, 0xC6, 0xF4, 0xDB, 0x7B, 0xFB, 0xC8, 0x4A, 0xD3, 0xE6, 0x6B, + 0x45, 0x7D, 0xE8, 0x4B, 0xD6, 0x32, 0xD8, 0xFD, 0x37, 0x71, 0xF1, 0xE1, + 0x30, 0x0F, 0xF8, 0x1B, 0x87, 0xFA, 0x06, 0x3F, 0x5E, 0xBA, 0xAE, 0x5B, + 0x8A, 0x00, 0xBC, 0x9D, 0x6D, 0xC1, 0xB1, 0x0E, 0x80, 0x5D, 0xD2, 0xD5, + 0xA0, 0x84, 0x07, 0x14, 0xB5, 0x90, 0x2C, 0xA3, 0xB2, 0x73, 0x4C, 0x54, + 0x92, 0x74, 0x36, 0x51, 0x38, 0xB0, 0xBD, 0x5A, 0xFC, 0x60, 0x62, 0x96, + 0x6C, 0x42, 0xF7, 0x10, 0x7C, 0x28, 0x27, 0x8C, 0x13, 0x95, 0x9C, 0xC7, + 0x24, 0x46, 0x3B, 0x70, 0xCA, 0xE3, 0x85, 0xCB, 0x11, 0xD0, 0x93, 0xB8, + 0xA6, 0x83, 0x20, 0xFF, 0x9F, 0x77, 0xC3, 0xCC, 0x03, 0x6F, 0x08, 0xBF, + 0x40, 0xE7, 0x2B, 0xE2, 0x79, 0x0C, 0xAA, 0x82, 0x41, 0x3A, 0xEA, 0xB9, + 0xE4, 0x9A, 0xA4, 0x97, 0x7E, 0xDA, 0x7A, 0x17, 0x66, 0x94, 0xA1, 0x1D, + 0x3D, 0xF0, 0xDE, 0xB3, 0x0B, 0x72, 0xA7, 0x1C, 0xEF, 0xD1, 0x53, 0x3E, + 0x8F, 0x33, 0x26, 0x5F, 0xEC, 0x76, 0x2A, 0x49, 0x81, 0x88, 0xEE, 0x21, + 0xC4, 0x1A, 0xEB, 0xD9, 0xC5, 0x39, 0x99, 0xCD, 0xAD, 0x31, 0x8B, 0x01, + 0x18, 0x23, 0xDD, 0x1F, 0x4E, 0x2D, 0xF9, 0x48, 0x4F, 0xF2, 0x65, 0x8E, + 0x78, 0x5C, 0x58, 0x19, 0x8D, 0xE5, 0x98, 0x57, 0x67, 0x7F, 0x05, 0x64, + 0xAF, 0x63, 0xB6, 0xFE, 0xF5, 0xB7, 0x3C, 0xA5, 0xCE, 0xE9, 0x68, 0x44, + 0xE0, 0x4D, 0x43, 0x69, 0x29, 0x2E, 0xAC, 0x15, 0x59, 0xA8, 0x0A, 0x9E, + 0x6E, 0x47, 0xDF, 0x34, 0x35, 0x6A, 0xCF, 0xDC, 0x22, 0xC9, 0xC0, 0x9B, + 0x89, 0xD4, 0xED, 0xAB, 0x12, 0xA2, 0x0D, 0x52, 0xBB, 0x02, 0x2F, 0xA9, + 0xD7, 0x61, 0x1E, 0xB4, 0x50, 0x04, 0xF6, 0xC2, 0x16, 0x25, 0x86, 0x56, + 0x55, 0x09, 0xBE, 0x91 +} +}; + + +const word32 Twofish::mds_[4][256] = { + { + 0xbcbc3275, 0xecec21f3, 0x202043c6, 0xb3b3c9f4, + 0xdada03db, 0x02028b7b, 0xe2e22bfb, 0x9e9efac8, + 0xc9c9ec4a, 0xd4d409d3, 0x18186be6, 0x1e1e9f6b, + 0x98980e45, 0xb2b2387d, 0xa6a6d2e8, 0x2626b74b, + 0x3c3c57d6, 0x93938a32, 0x8282eed8, 0x525298fd, + 0x7b7bd437, 0xbbbb3771, 0x5b5b97f1, 0x474783e1, + 0x24243c30, 0x5151e20f, 0xbabac6f8, 0x4a4af31b, + 0xbfbf4887, 0x0d0d70fa, 0xb0b0b306, 0x7575de3f, + 0xd2d2fd5e, 0x7d7d20ba, 0x666631ae, 0x3a3aa35b, + 0x59591c8a, 0x00000000, 0xcdcd93bc, 0x1a1ae09d, + 0xaeae2c6d, 0x7f7fabc1, 0x2b2bc7b1, 0xbebeb90e, + 0xe0e0a080, 0x8a8a105d, 0x3b3b52d2, 0x6464bad5, + 0xd8d888a0, 0xe7e7a584, 0x5f5fe807, 0x1b1b1114, + 0x2c2cc2b5, 0xfcfcb490, 0x3131272c, 0x808065a3, + 0x73732ab2, 0x0c0c8173, 0x79795f4c, 0x6b6b4154, + 0x4b4b0292, 0x53536974, 0x94948f36, 0x83831f51, + 0x2a2a3638, 0xc4c49cb0, 0x2222c8bd, 0xd5d5f85a, + 0xbdbdc3fc, 0x48487860, 0xffffce62, 0x4c4c0796, + 0x4141776c, 0xc7c7e642, 0xebeb24f7, 0x1c1c1410, + 0x5d5d637c, 0x36362228, 0x6767c027, 0xe9e9af8c, + 0x4444f913, 0x1414ea95, 0xf5f5bb9c, 0xcfcf18c7, + 0x3f3f2d24, 0xc0c0e346, 0x7272db3b, 0x54546c70, + 0x29294cca, 0xf0f035e3, 0x0808fe85, 0xc6c617cb, + 0xf3f34f11, 0x8c8ce4d0, 0xa4a45993, 0xcaca96b8, + 0x68683ba6, 0xb8b84d83, 0x38382820, 0xe5e52eff, + 0xadad569f, 0x0b0b8477, 0xc8c81dc3, 0x9999ffcc, + 0x5858ed03, 0x19199a6f, 0x0e0e0a08, 0x95957ebf, + 0x70705040, 0xf7f730e7, 0x6e6ecf2b, 0x1f1f6ee2, + 0xb5b53d79, 0x09090f0c, 0x616134aa, 0x57571682, + 0x9f9f0b41, 0x9d9d803a, 0x111164ea, 0x2525cdb9, + 0xafafdde4, 0x4545089a, 0xdfdf8da4, 0xa3a35c97, + 0xeaead57e, 0x353558da, 0xededd07a, 0x4343fc17, + 0xf8f8cb66, 0xfbfbb194, 0x3737d3a1, 0xfafa401d, + 0xc2c2683d, 0xb4b4ccf0, 0x32325dde, 0x9c9c71b3, + 0x5656e70b, 0xe3e3da72, 0x878760a7, 0x15151b1c, + 0xf9f93aef, 0x6363bfd1, 0x3434a953, 0x9a9a853e, + 0xb1b1428f, 0x7c7cd133, 0x88889b26, 0x3d3da65f, + 0xa1a1d7ec, 0xe4e4df76, 0x8181942a, 0x91910149, + 0x0f0ffb81, 0xeeeeaa88, 0x161661ee, 0xd7d77321, + 0x9797f5c4, 0xa5a5a81a, 0xfefe3feb, 0x6d6db5d9, + 0x7878aec5, 0xc5c56d39, 0x1d1de599, 0x7676a4cd, + 0x3e3edcad, 0xcbcb6731, 0xb6b6478b, 0xefef5b01, + 0x12121e18, 0x6060c523, 0x6a6ab0dd, 0x4d4df61f, + 0xcecee94e, 0xdede7c2d, 0x55559df9, 0x7e7e5a48, + 0x2121b24f, 0x03037af2, 0xa0a02665, 0x5e5e198e, + 0x5a5a6678, 0x65654b5c, 0x62624e58, 0xfdfd4519, + 0x0606f48d, 0x404086e5, 0xf2f2be98, 0x3333ac57, + 0x17179067, 0x05058e7f, 0xe8e85e05, 0x4f4f7d64, + 0x89896aaf, 0x10109563, 0x74742fb6, 0x0a0a75fe, + 0x5c5c92f5, 0x9b9b74b7, 0x2d2d333c, 0x3030d6a5, + 0x2e2e49ce, 0x494989e9, 0x46467268, 0x77775544, + 0xa8a8d8e0, 0x9696044d, 0x2828bd43, 0xa9a92969, + 0xd9d97929, 0x8686912e, 0xd1d187ac, 0xf4f44a15, + 0x8d8d1559, 0xd6d682a8, 0xb9b9bc0a, 0x42420d9e, + 0xf6f6c16e, 0x2f2fb847, 0xdddd06df, 0x23233934, + 0xcccc6235, 0xf1f1c46a, 0xc1c112cf, 0x8585ebdc, + 0x8f8f9e22, 0x7171a1c9, 0x9090f0c0, 0xaaaa539b, + 0x0101f189, 0x8b8be1d4, 0x4e4e8ced, 0x8e8e6fab, + 0xababa212, 0x6f6f3ea2, 0xe6e6540d, 0xdbdbf252, + 0x92927bbb, 0xb7b7b602, 0x6969ca2f, 0x3939d9a9, + 0xd3d30cd7, 0xa7a72361, 0xa2a2ad1e, 0xc3c399b4, + 0x6c6c4450, 0x07070504, 0x04047ff6, 0x272746c2, + 0xacaca716, 0xd0d07625, 0x50501386, 0xdcdcf756, + 0x84841a55, 0xe1e15109, 0x7a7a25be, 0x1313ef91 + }, + { + 0xa9d93939, 0x67901717, 0xb3719c9c, 0xe8d2a6a6, + 0x04050707, 0xfd985252, 0xa3658080, 0x76dfe4e4, + 0x9a084545, 0x92024b4b, 0x80a0e0e0, 0x78665a5a, + 0xe4ddafaf, 0xddb06a6a, 0xd1bf6363, 0x38362a2a, + 0x0d54e6e6, 0xc6432020, 0x3562cccc, 0x98bef2f2, + 0x181e1212, 0xf724ebeb, 0xecd7a1a1, 0x6c774141, + 0x43bd2828, 0x7532bcbc, 0x37d47b7b, 0x269b8888, + 0xfa700d0d, 0x13f94444, 0x94b1fbfb, 0x485a7e7e, + 0xf27a0303, 0xd0e48c8c, 0x8b47b6b6, 0x303c2424, + 0x84a5e7e7, 0x54416b6b, 0xdf06dddd, 0x23c56060, + 0x1945fdfd, 0x5ba33a3a, 0x3d68c2c2, 0x59158d8d, + 0xf321ecec, 0xae316666, 0xa23e6f6f, 0x82165757, + 0x63951010, 0x015befef, 0x834db8b8, 0x2e918686, + 0xd9b56d6d, 0x511f8383, 0x9b53aaaa, 0x7c635d5d, + 0xa63b6868, 0xeb3ffefe, 0xa5d63030, 0xbe257a7a, + 0x16a7acac, 0x0c0f0909, 0xe335f0f0, 0x6123a7a7, + 0xc0f09090, 0x8cafe9e9, 0x3a809d9d, 0xf5925c5c, + 0x73810c0c, 0x2c273131, 0x2576d0d0, 0x0be75656, + 0xbb7b9292, 0x4ee9cece, 0x89f10101, 0x6b9f1e1e, + 0x53a93434, 0x6ac4f1f1, 0xb499c3c3, 0xf1975b5b, + 0xe1834747, 0xe66b1818, 0xbdc82222, 0x450e9898, + 0xe26e1f1f, 0xf4c9b3b3, 0xb62f7474, 0x66cbf8f8, + 0xccff9999, 0x95ea1414, 0x03ed5858, 0x56f7dcdc, + 0xd4e18b8b, 0x1c1b1515, 0x1eada2a2, 0xd70cd3d3, + 0xfb2be2e2, 0xc31dc8c8, 0x8e195e5e, 0xb5c22c2c, + 0xe9894949, 0xcf12c1c1, 0xbf7e9595, 0xba207d7d, + 0xea641111, 0x77840b0b, 0x396dc5c5, 0xaf6a8989, + 0x33d17c7c, 0xc9a17171, 0x62ceffff, 0x7137bbbb, + 0x81fb0f0f, 0x793db5b5, 0x0951e1e1, 0xaddc3e3e, + 0x242d3f3f, 0xcda47676, 0xf99d5555, 0xd8ee8282, + 0xe5864040, 0xc5ae7878, 0xb9cd2525, 0x4d049696, + 0x44557777, 0x080a0e0e, 0x86135050, 0xe730f7f7, + 0xa1d33737, 0x1d40fafa, 0xaa346161, 0xed8c4e4e, + 0x06b3b0b0, 0x706c5454, 0xb22a7373, 0xd2523b3b, + 0x410b9f9f, 0x7b8b0202, 0xa088d8d8, 0x114ff3f3, + 0x3167cbcb, 0xc2462727, 0x27c06767, 0x90b4fcfc, + 0x20283838, 0xf67f0404, 0x60784848, 0xff2ee5e5, + 0x96074c4c, 0x5c4b6565, 0xb1c72b2b, 0xab6f8e8e, + 0x9e0d4242, 0x9cbbf5f5, 0x52f2dbdb, 0x1bf34a4a, + 0x5fa63d3d, 0x9359a4a4, 0x0abcb9b9, 0xef3af9f9, + 0x91ef1313, 0x85fe0808, 0x49019191, 0xee611616, + 0x2d7cdede, 0x4fb22121, 0x8f42b1b1, 0x3bdb7272, + 0x47b82f2f, 0x8748bfbf, 0x6d2caeae, 0x46e3c0c0, + 0xd6573c3c, 0x3e859a9a, 0x6929a9a9, 0x647d4f4f, + 0x2a948181, 0xce492e2e, 0xcb17c6c6, 0x2fca6969, + 0xfcc3bdbd, 0x975ca3a3, 0x055ee8e8, 0x7ad0eded, + 0xac87d1d1, 0x7f8e0505, 0xd5ba6464, 0x1aa8a5a5, + 0x4bb72626, 0x0eb9bebe, 0xa7608787, 0x5af8d5d5, + 0x28223636, 0x14111b1b, 0x3fde7575, 0x2979d9d9, + 0x88aaeeee, 0x3c332d2d, 0x4c5f7979, 0x02b6b7b7, + 0xb896caca, 0xda583535, 0xb09cc4c4, 0x17fc4343, + 0x551a8484, 0x1ff64d4d, 0x8a1c5959, 0x7d38b2b2, + 0x57ac3333, 0xc718cfcf, 0x8df40606, 0x74695353, + 0xb7749b9b, 0xc4f59797, 0x9f56adad, 0x72dae3e3, + 0x7ed5eaea, 0x154af4f4, 0x229e8f8f, 0x12a2abab, + 0x584e6262, 0x07e85f5f, 0x99e51d1d, 0x34392323, + 0x6ec1f6f6, 0x50446c6c, 0xde5d3232, 0x68724646, + 0x6526a0a0, 0xbc93cdcd, 0xdb03dada, 0xf8c6baba, + 0xc8fa9e9e, 0xa882d6d6, 0x2bcf6e6e, 0x40507070, + 0xdceb8585, 0xfe750a0a, 0x328a9393, 0xa48ddfdf, + 0xca4c2929, 0x10141c1c, 0x2173d7d7, 0xf0ccb4b4, + 0xd309d4d4, 0x5d108a8a, 0x0fe25151, 0x00000000, + 0x6f9a1919, 0x9de01a1a, 0x368f9494, 0x42e6c7c7, + 0x4aecc9c9, 0x5efdd2d2, 0xc1ab7f7f, 0xe0d8a8a8 + }, + { + 0xbc75bc32, 0xecf3ec21, 0x20c62043, 0xb3f4b3c9, + 0xdadbda03, 0x027b028b, 0xe2fbe22b, 0x9ec89efa, + 0xc94ac9ec, 0xd4d3d409, 0x18e6186b, 0x1e6b1e9f, + 0x9845980e, 0xb27db238, 0xa6e8a6d2, 0x264b26b7, + 0x3cd63c57, 0x9332938a, 0x82d882ee, 0x52fd5298, + 0x7b377bd4, 0xbb71bb37, 0x5bf15b97, 0x47e14783, + 0x2430243c, 0x510f51e2, 0xbaf8bac6, 0x4a1b4af3, + 0xbf87bf48, 0x0dfa0d70, 0xb006b0b3, 0x753f75de, + 0xd25ed2fd, 0x7dba7d20, 0x66ae6631, 0x3a5b3aa3, + 0x598a591c, 0x00000000, 0xcdbccd93, 0x1a9d1ae0, + 0xae6dae2c, 0x7fc17fab, 0x2bb12bc7, 0xbe0ebeb9, + 0xe080e0a0, 0x8a5d8a10, 0x3bd23b52, 0x64d564ba, + 0xd8a0d888, 0xe784e7a5, 0x5f075fe8, 0x1b141b11, + 0x2cb52cc2, 0xfc90fcb4, 0x312c3127, 0x80a38065, + 0x73b2732a, 0x0c730c81, 0x794c795f, 0x6b546b41, + 0x4b924b02, 0x53745369, 0x9436948f, 0x8351831f, + 0x2a382a36, 0xc4b0c49c, 0x22bd22c8, 0xd55ad5f8, + 0xbdfcbdc3, 0x48604878, 0xff62ffce, 0x4c964c07, + 0x416c4177, 0xc742c7e6, 0xebf7eb24, 0x1c101c14, + 0x5d7c5d63, 0x36283622, 0x672767c0, 0xe98ce9af, + 0x441344f9, 0x149514ea, 0xf59cf5bb, 0xcfc7cf18, + 0x3f243f2d, 0xc046c0e3, 0x723b72db, 0x5470546c, + 0x29ca294c, 0xf0e3f035, 0x088508fe, 0xc6cbc617, + 0xf311f34f, 0x8cd08ce4, 0xa493a459, 0xcab8ca96, + 0x68a6683b, 0xb883b84d, 0x38203828, 0xe5ffe52e, + 0xad9fad56, 0x0b770b84, 0xc8c3c81d, 0x99cc99ff, + 0x580358ed, 0x196f199a, 0x0e080e0a, 0x95bf957e, + 0x70407050, 0xf7e7f730, 0x6e2b6ecf, 0x1fe21f6e, + 0xb579b53d, 0x090c090f, 0x61aa6134, 0x57825716, + 0x9f419f0b, 0x9d3a9d80, 0x11ea1164, 0x25b925cd, + 0xafe4afdd, 0x459a4508, 0xdfa4df8d, 0xa397a35c, + 0xea7eead5, 0x35da3558, 0xed7aedd0, 0x431743fc, + 0xf866f8cb, 0xfb94fbb1, 0x37a137d3, 0xfa1dfa40, + 0xc23dc268, 0xb4f0b4cc, 0x32de325d, 0x9cb39c71, + 0x560b56e7, 0xe372e3da, 0x87a78760, 0x151c151b, + 0xf9eff93a, 0x63d163bf, 0x345334a9, 0x9a3e9a85, + 0xb18fb142, 0x7c337cd1, 0x8826889b, 0x3d5f3da6, + 0xa1eca1d7, 0xe476e4df, 0x812a8194, 0x91499101, + 0x0f810ffb, 0xee88eeaa, 0x16ee1661, 0xd721d773, + 0x97c497f5, 0xa51aa5a8, 0xfeebfe3f, 0x6dd96db5, + 0x78c578ae, 0xc539c56d, 0x1d991de5, 0x76cd76a4, + 0x3ead3edc, 0xcb31cb67, 0xb68bb647, 0xef01ef5b, + 0x1218121e, 0x602360c5, 0x6add6ab0, 0x4d1f4df6, + 0xce4ecee9, 0xde2dde7c, 0x55f9559d, 0x7e487e5a, + 0x214f21b2, 0x03f2037a, 0xa065a026, 0x5e8e5e19, + 0x5a785a66, 0x655c654b, 0x6258624e, 0xfd19fd45, + 0x068d06f4, 0x40e54086, 0xf298f2be, 0x335733ac, + 0x17671790, 0x057f058e, 0xe805e85e, 0x4f644f7d, + 0x89af896a, 0x10631095, 0x74b6742f, 0x0afe0a75, + 0x5cf55c92, 0x9bb79b74, 0x2d3c2d33, 0x30a530d6, + 0x2ece2e49, 0x49e94989, 0x46684672, 0x77447755, + 0xa8e0a8d8, 0x964d9604, 0x284328bd, 0xa969a929, + 0xd929d979, 0x862e8691, 0xd1acd187, 0xf415f44a, + 0x8d598d15, 0xd6a8d682, 0xb90ab9bc, 0x429e420d, + 0xf66ef6c1, 0x2f472fb8, 0xdddfdd06, 0x23342339, + 0xcc35cc62, 0xf16af1c4, 0xc1cfc112, 0x85dc85eb, + 0x8f228f9e, 0x71c971a1, 0x90c090f0, 0xaa9baa53, + 0x018901f1, 0x8bd48be1, 0x4eed4e8c, 0x8eab8e6f, + 0xab12aba2, 0x6fa26f3e, 0xe60de654, 0xdb52dbf2, + 0x92bb927b, 0xb702b7b6, 0x692f69ca, 0x39a939d9, + 0xd3d7d30c, 0xa761a723, 0xa21ea2ad, 0xc3b4c399, + 0x6c506c44, 0x07040705, 0x04f6047f, 0x27c22746, + 0xac16aca7, 0xd025d076, 0x50865013, 0xdc56dcf7, + 0x8455841a, 0xe109e151, 0x7abe7a25, 0x139113ef + }, + { + 0xd939a9d9, 0x90176790, 0x719cb371, 0xd2a6e8d2, + 0x05070405, 0x9852fd98, 0x6580a365, 0xdfe476df, + 0x08459a08, 0x024b9202, 0xa0e080a0, 0x665a7866, + 0xddafe4dd, 0xb06addb0, 0xbf63d1bf, 0x362a3836, + 0x54e60d54, 0x4320c643, 0x62cc3562, 0xbef298be, + 0x1e12181e, 0x24ebf724, 0xd7a1ecd7, 0x77416c77, + 0xbd2843bd, 0x32bc7532, 0xd47b37d4, 0x9b88269b, + 0x700dfa70, 0xf94413f9, 0xb1fb94b1, 0x5a7e485a, + 0x7a03f27a, 0xe48cd0e4, 0x47b68b47, 0x3c24303c, + 0xa5e784a5, 0x416b5441, 0x06dddf06, 0xc56023c5, + 0x45fd1945, 0xa33a5ba3, 0x68c23d68, 0x158d5915, + 0x21ecf321, 0x3166ae31, 0x3e6fa23e, 0x16578216, + 0x95106395, 0x5bef015b, 0x4db8834d, 0x91862e91, + 0xb56dd9b5, 0x1f83511f, 0x53aa9b53, 0x635d7c63, + 0x3b68a63b, 0x3ffeeb3f, 0xd630a5d6, 0x257abe25, + 0xa7ac16a7, 0x0f090c0f, 0x35f0e335, 0x23a76123, + 0xf090c0f0, 0xafe98caf, 0x809d3a80, 0x925cf592, + 0x810c7381, 0x27312c27, 0x76d02576, 0xe7560be7, + 0x7b92bb7b, 0xe9ce4ee9, 0xf10189f1, 0x9f1e6b9f, + 0xa93453a9, 0xc4f16ac4, 0x99c3b499, 0x975bf197, + 0x8347e183, 0x6b18e66b, 0xc822bdc8, 0x0e98450e, + 0x6e1fe26e, 0xc9b3f4c9, 0x2f74b62f, 0xcbf866cb, + 0xff99ccff, 0xea1495ea, 0xed5803ed, 0xf7dc56f7, + 0xe18bd4e1, 0x1b151c1b, 0xada21ead, 0x0cd3d70c, + 0x2be2fb2b, 0x1dc8c31d, 0x195e8e19, 0xc22cb5c2, + 0x8949e989, 0x12c1cf12, 0x7e95bf7e, 0x207dba20, + 0x6411ea64, 0x840b7784, 0x6dc5396d, 0x6a89af6a, + 0xd17c33d1, 0xa171c9a1, 0xceff62ce, 0x37bb7137, + 0xfb0f81fb, 0x3db5793d, 0x51e10951, 0xdc3eaddc, + 0x2d3f242d, 0xa476cda4, 0x9d55f99d, 0xee82d8ee, + 0x8640e586, 0xae78c5ae, 0xcd25b9cd, 0x04964d04, + 0x55774455, 0x0a0e080a, 0x13508613, 0x30f7e730, + 0xd337a1d3, 0x40fa1d40, 0x3461aa34, 0x8c4eed8c, + 0xb3b006b3, 0x6c54706c, 0x2a73b22a, 0x523bd252, + 0x0b9f410b, 0x8b027b8b, 0x88d8a088, 0x4ff3114f, + 0x67cb3167, 0x4627c246, 0xc06727c0, 0xb4fc90b4, + 0x28382028, 0x7f04f67f, 0x78486078, 0x2ee5ff2e, + 0x074c9607, 0x4b655c4b, 0xc72bb1c7, 0x6f8eab6f, + 0x0d429e0d, 0xbbf59cbb, 0xf2db52f2, 0xf34a1bf3, + 0xa63d5fa6, 0x59a49359, 0xbcb90abc, 0x3af9ef3a, + 0xef1391ef, 0xfe0885fe, 0x01914901, 0x6116ee61, + 0x7cde2d7c, 0xb2214fb2, 0x42b18f42, 0xdb723bdb, + 0xb82f47b8, 0x48bf8748, 0x2cae6d2c, 0xe3c046e3, + 0x573cd657, 0x859a3e85, 0x29a96929, 0x7d4f647d, + 0x94812a94, 0x492ece49, 0x17c6cb17, 0xca692fca, + 0xc3bdfcc3, 0x5ca3975c, 0x5ee8055e, 0xd0ed7ad0, + 0x87d1ac87, 0x8e057f8e, 0xba64d5ba, 0xa8a51aa8, + 0xb7264bb7, 0xb9be0eb9, 0x6087a760, 0xf8d55af8, + 0x22362822, 0x111b1411, 0xde753fde, 0x79d92979, + 0xaaee88aa, 0x332d3c33, 0x5f794c5f, 0xb6b702b6, + 0x96cab896, 0x5835da58, 0x9cc4b09c, 0xfc4317fc, + 0x1a84551a, 0xf64d1ff6, 0x1c598a1c, 0x38b27d38, + 0xac3357ac, 0x18cfc718, 0xf4068df4, 0x69537469, + 0x749bb774, 0xf597c4f5, 0x56ad9f56, 0xdae372da, + 0xd5ea7ed5, 0x4af4154a, 0x9e8f229e, 0xa2ab12a2, + 0x4e62584e, 0xe85f07e8, 0xe51d99e5, 0x39233439, + 0xc1f66ec1, 0x446c5044, 0x5d32de5d, 0x72466872, + 0x26a06526, 0x93cdbc93, 0x03dadb03, 0xc6baf8c6, + 0xfa9ec8fa, 0x82d6a882, 0xcf6e2bcf, 0x50704050, + 0xeb85dceb, 0x750afe75, 0x8a93328a, 0x8ddfa48d, + 0x4c29ca4c, 0x141c1014, 0x73d72173, 0xccb4f0cc, + 0x09d4d309, 0x108a5d10, 0xe2510fe2, 0x00000000, + 0x9a196f9a, 0xe01a9de0, 0x8f94368f, 0xe6c742e6, + 0xecc94aec, 0xfdd25efd, 0xab7fc1ab, 0xd8a8e0d8 + } +}; + + +} // namespace + diff --git a/externals/mysql/extlib/yassl/taocrypt/src/twofish.cpp b/externals/mysql/extlib/yassl/taocrypt/src/twofish.cpp new file mode 100644 index 00000000000..84dd35f9191 --- /dev/null +++ b/externals/mysql/extlib/yassl/taocrypt/src/twofish.cpp @@ -0,0 +1,575 @@ +/* + 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++ based on Wei Dai's twofish.cpp from CryptoPP */ +/* x86 asm original */ + + +#if defined(TAOCRYPT_KERNEL_MODE) + #define DO_TAOCRYPT_KERNEL_MODE +#endif // only some modules now support this + +#include "runtime.hpp" +#include "twofish.hpp" + + + +namespace TaoCrypt { + + +#if defined(DO_TWOFISH_ASM) + +// ia32 optimized version +void Twofish::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--) { + if (dir_ == ENCRYPTION) + AsmEncrypt(in, out); + else + AsmDecrypt(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); + r_[2] ^= *(word32*)(in + 8); + r_[3] ^= *(word32*)(in + 12); + + AsmEncrypt((byte*)r_, (byte*)r_); + memcpy(out, r_, BLOCK_SIZE); + + out += BLOCK_SIZE; + in += BLOCK_SIZE; + } + else + while (blocks--) { + AsmDecrypt(in, out); + + *(word32*)out ^= r_[0]; + *(word32*)(out + 4) ^= r_[1]; + *(word32*)(out + 8) ^= r_[2]; + *(word32*)(out + 12) ^= r_[3]; + + memcpy(r_, in, BLOCK_SIZE); + + out += BLOCK_SIZE; + in += BLOCK_SIZE; + } +} + +#endif // DO_TWOFISH_ASM + + +namespace { // locals + +// compute (c * x^4) mod (x^4 + (a + 1/a) * x^3 + a * x^2 + (a + 1/a) * x + 1) +// over GF(256) +static inline unsigned int Mod(unsigned int c) +{ + static const unsigned int modulus = 0x14d; + unsigned int c2 = (c<<1) ^ ((c & 0x80) ? modulus : 0); + unsigned int c1 = c2 ^ (c>>1) ^ ((c & 1) ? (modulus>>1) : 0); + return c | (c1 << 8) | (c2 << 16) | (c1 << 24); +} + +// compute RS(12,8) code with the above polynomial as generator +// this is equivalent to multiplying by the RS matrix +static word32 ReedSolomon(word32 high, word32 low) +{ + for (unsigned int i=0; i<8; i++) { + high = Mod(high>>24) ^ (high<<8) ^ (low>>24); + low <<= 8; + } + return high; +} + +} // local namespace + + + +inline word32 Twofish::h0(word32 x, const word32* key, unsigned int kLen) +{ + x = x | (x<<8) | (x<<16) | (x<<24); + switch(kLen) + { +#define Q(a, b, c, d, t) q_[a][GETBYTE(t,0)] ^ (q_[b][GETBYTE(t,1)] << 8) ^ \ + (q_[c][GETBYTE(t,2)] << 16) ^ (q_[d][GETBYTE(t,3)] << 24) + case 4: x = Q(1, 0, 0, 1, x) ^ key[6]; + case 3: x = Q(1, 1, 0, 0, x) ^ key[4]; + case 2: x = Q(0, 1, 0, 1, x) ^ key[2]; + x = Q(0, 0, 1, 1, x) ^ key[0]; + } + return x; +} + +inline word32 Twofish::h(word32 x, const word32* key, unsigned int kLen) +{ + x = h0(x, key, kLen); + return mds_[0][GETBYTE(x,0)] ^ mds_[1][GETBYTE(x,1)] ^ + mds_[2][GETBYTE(x,2)] ^ mds_[3][GETBYTE(x,3)]; +} + + +void Twofish::SetKey(const byte* userKey, word32 keylen, CipherDir /*dummy*/) +{ + assert(keylen >= 16 && keylen <= 32); + + unsigned int len = (keylen <= 16 ? 2 : (keylen <= 24 ? 3 : 4)); + word32 key[8]; + GetUserKey(LittleEndianOrder, key, len*2, userKey, keylen); + + unsigned int i; + for (i=0; i<40; i+=2) { + word32 a = h(i, key, len); + word32 b = rotlFixed(h(i+1, key+1, len), 8); + k_[i] = a+b; + k_[i+1] = rotlFixed(a+2*b, 9); + } + + word32 svec[8]; + for (i=0; i<len; i++) + svec[2*(len-i-1)] = ReedSolomon(key[2*i+1], key[2*i]); + + for (i=0; i<256; i++) { + word32 t = h0(i, svec, len); + s_[0][i] = mds_[0][GETBYTE(t, 0)]; + s_[1][i] = mds_[1][GETBYTE(t, 1)]; + s_[2][i] = mds_[2][GETBYTE(t, 2)]; + s_[3][i] = mds_[3][GETBYTE(t, 3)]; + } +} + + +void Twofish::ProcessAndXorBlock(const byte* in, const byte* xOr, byte* out) + const +{ + if (dir_ == ENCRYPTION) + encrypt(in, xOr, out); + else + decrypt(in, xOr, out); +} + +#define G1(x) (s_[0][GETBYTE(x,0)] ^ s_[1][GETBYTE(x,1)] ^ \ + s_[2][GETBYTE(x,2)] ^ s_[3][GETBYTE(x,3)]) +#define G2(x) (s_[0][GETBYTE(x,3)] ^ s_[1][GETBYTE(x,0)] ^ \ + s_[2][GETBYTE(x,1)] ^ s_[3][GETBYTE(x,2)]) + +#define ENCROUND(n, a, b, c, d) \ + x = G1 (a); y = G2 (b); \ + x += y; y += x + k[2 * (n) + 1]; \ + (c) ^= x + k[2 * (n)]; \ + (c) = rotrFixed(c, 1); \ + (d) = rotlFixed(d, 1) ^ y + +#define ENCCYCLE(n) \ + ENCROUND (2 * (n), a, b, c, d); \ + ENCROUND (2 * (n) + 1, c, d, a, b) + +#define DECROUND(n, a, b, c, d) \ + x = G1 (a); y = G2 (b); \ + x += y; y += x; \ + (d) ^= y + k[2 * (n) + 1]; \ + (d) = rotrFixed(d, 1); \ + (c) = rotlFixed(c, 1); \ + (c) ^= (x + k[2 * (n)]) + +#define DECCYCLE(n) \ + DECROUND (2 * (n) + 1, c, d, a, b); \ + DECROUND (2 * (n), a, b, c, d) + + +typedef BlockGetAndPut<word32, LittleEndian> gpBlock; + +void Twofish::encrypt(const byte* inBlock, const byte* xorBlock, + byte* outBlock) const +{ + word32 x, y, a, b, c, d; + + gpBlock::Get(inBlock)(a)(b)(c)(d); + + a ^= k_[0]; + b ^= k_[1]; + c ^= k_[2]; + d ^= k_[3]; + + const word32 *k = k_+8; + + ENCCYCLE (0); + ENCCYCLE (1); + ENCCYCLE (2); + ENCCYCLE (3); + ENCCYCLE (4); + ENCCYCLE (5); + ENCCYCLE (6); + ENCCYCLE (7); + + c ^= k_[4]; + d ^= k_[5]; + a ^= k_[6]; + b ^= k_[7]; + + gpBlock::Put(xorBlock, outBlock)(c)(d)(a)(b); +} + + +void Twofish::decrypt(const byte* inBlock, const byte* xorBlock, + byte* outBlock) const +{ + word32 x, y, a, b, c, d; + + gpBlock::Get(inBlock)(c)(d)(a)(b); + + c ^= k_[4]; + d ^= k_[5]; + a ^= k_[6]; + b ^= k_[7]; + + const word32 *k = k_+8; + DECCYCLE (7); + DECCYCLE (6); + DECCYCLE (5); + DECCYCLE (4); + DECCYCLE (3); + DECCYCLE (2); + DECCYCLE (1); + DECCYCLE (0); + + a ^= k_[0]; + b ^= k_[1]; + c ^= k_[2]; + d ^= k_[3]; + + gpBlock::Put(xorBlock, outBlock)(a)(b)(c)(d); +} + + + +#if defined(DO_TWOFISH_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( movd mm6, ebp ) \ + AS2( mov edi, DWORD PTR [ebp + 8] ) \ + AS2( mov esi, DWORD PTR [ebp + 12] ) + + #define EPILOG() \ + AS2( movd esp, mm6 ) \ + 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( movd mm6, ebp ) \ + AS2( mov edi, ecx ) \ + AS2( mov esi, DWORD PTR [ebp + 8] ) + + /* ebp already set */ + #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 + + + + + // x = esi, y = [esp], s_ = ebp + // edi always open for G1 and G2 + // G1 also uses edx after save and restore + // G2 also uses eax after save and restore + // and ecx for tmp [esp] which Rounds also use + // and restore from mm7 + + // x = G1(a) bytes(0,1,2,3) +#define ASMG1(z, zl, zh) \ + AS2( movd mm2, edx ) \ + AS2( movzx edi, zl ) \ + AS2( mov esi, DWORD PTR [ebp + edi*4] ) \ + AS2( movzx edx, zh ) \ + AS2( xor esi, DWORD PTR 1024[ebp + edx*4] ) \ + \ + AS2( mov edx, z ) \ + AS2( shr edx, 16 ) \ + AS2( movzx edi, dl ) \ + AS2( xor esi, DWORD PTR 2048[ebp + edi*4] ) \ + AS2( movzx edx, dh ) \ + AS2( xor esi, DWORD PTR 3072[ebp + edx*4] ) \ + AS2( movd edx, mm2 ) + + + // y = G2(b) bytes(3,0,1,2) [ put y into ecx for Rounds ] +#define ASMG2(z, zl, zh) \ + AS2( movd mm7, ecx ) \ + AS2( movd mm2, eax ) \ + AS2( mov edi, z ) \ + AS2( shr edi, 24 ) \ + AS2( mov ecx, DWORD PTR [ebp + edi*4] ) \ + AS2( movzx eax, zl ) \ + AS2( xor ecx, DWORD PTR 1024[ebp + eax*4] ) \ + \ + AS2( mov eax, z ) \ + AS2( shr eax, 16 ) \ + AS2( movzx edi, zh ) \ + AS2( xor ecx, DWORD PTR 2048[ebp + edi*4] ) \ + AS2( movzx eax, al ) \ + AS2( xor ecx, DWORD PTR 3072[ebp + eax*4] ) \ + AS2( movd eax, mm2 ) + + + // encrypt Round (n), + // x = esi, k = ebp, edi open + // y is in ecx from G2, restore when done from mm7 + // before C (which be same register!) +#define ASMENCROUND(N, A, A2, A3, B, B2, B3, C, D) \ + /* setup s_ */ \ + AS2( movd ebp, mm1 ) \ + ASMG1(A, A2, A3) \ + ASMG2(B, B2, B3) \ + /* setup k */ \ + AS2( movd ebp, mm0 ) \ + /* x += y */ \ + AS2( add esi, ecx ) \ + AS2( add ebp, 32 ) \ + /* y += x + k[2 * (n) + 1] */ \ + AS2( add ecx, esi ) \ + AS2( rol D, 1 ) \ + AS2( add ecx, DWORD PTR [ebp + 8 * N + 4] ) \ + /* (d) = rotlFixed(d, 1) ^ y */ \ + AS2( xor D, ecx ) \ + AS2( movd ecx, mm7 ) \ + /* (c) ^= x + k[2 * (n)] */ \ + AS2( mov edi, esi ) \ + AS2( add edi, DWORD PTR [ebp + 8 * N] ) \ + AS2( xor C, edi ) \ + /* (c) = rotrFixed(c, 1) */ \ + AS2( ror C, 1 ) + + + // decrypt Round (n), + // x = esi, k = ebp, edi open + // y is in ecx from G2, restore ecx from mm7 when done +#define ASMDECROUND(N, A, A2, A3, B, B2, B3, C, D) \ + /* setup s_ */ \ + AS2( movd ebp, mm1 ) \ + ASMG1(A, A2, A3) \ + ASMG2(B, B2, B3) \ + /* setup k */ \ + AS2( movd ebp, mm0 ) \ + /* x += y */ \ + AS2( add esi, ecx ) \ + AS2( add ebp, 32 ) \ + /* y += x */ \ + AS2( add ecx, esi ) \ + /* (d) ^= y + k[2 * (n) + 1] */ \ + AS2( mov edi, DWORD PTR [ebp + 8 * N + 4] ) \ + AS2( add edi, ecx ) \ + AS2( movd ecx, mm7 ) \ + AS2( xor D, edi ) \ + /* (d) = rotrFixed(d, 1) */ \ + AS2( ror D, 1 ) \ + /* (c) = rotlFixed(c, 1) */ \ + AS2( rol C, 1 ) \ + /* (c) ^= (x + k[2 * (n)]) */ \ + AS2( mov edi, esi ) \ + AS2( add edi, DWORD PTR [ebp + 8 * N] ) \ + AS2( xor C, edi ) + + +#ifdef _MSC_VER + __declspec(naked) +#endif +void Twofish::AsmEncrypt(const byte* inBlock, byte* outBlock) const +{ + PROLOG() + + #ifdef OLD_GCC_OFFSET + AS2( add edi, 60 ) // k_ + #else + AS2( add edi, 56 ) // k_ + #endif + + AS2( mov ebp, edi ) + + AS2( mov eax, DWORD PTR [esi] ) // a + AS2( movd mm0, edi ) // store k_ + AS2( mov ebx, DWORD PTR [esi + 4] ) // b + AS2( add ebp, 160 ) // s_[0] + AS2( mov ecx, DWORD PTR [esi + 8] ) // c + AS2( movd mm1, ebp ) // store s_ + AS2( mov edx, DWORD PTR [esi + 12] ) // d + + AS2( xor eax, DWORD PTR [edi] ) // k_[0] + AS2( xor ebx, DWORD PTR [edi + 4] ) // [1] + AS2( xor ecx, DWORD PTR [edi + 8] ) // [2] + AS2( xor edx, DWORD PTR [edi + 12] ) // [3] + + + ASMENCROUND( 0, eax, al, ah, ebx, bl, bh, ecx, edx) + ASMENCROUND( 1, ecx, cl, ch, edx, dl, dh, eax, ebx) + ASMENCROUND( 2, eax, al, ah, ebx, bl, bh, ecx, edx) + ASMENCROUND( 3, ecx, cl, ch, edx, dl, dh, eax, ebx) + ASMENCROUND( 4, eax, al, ah, ebx, bl, bh, ecx, edx) + ASMENCROUND( 5, ecx, cl, ch, edx, dl, dh, eax, ebx) + ASMENCROUND( 6, eax, al, ah, ebx, bl, bh, ecx, edx) + ASMENCROUND( 7, ecx, cl, ch, edx, dl, dh, eax, ebx) + ASMENCROUND( 8, eax, al, ah, ebx, bl, bh, ecx, edx) + ASMENCROUND( 9, ecx, cl, ch, edx, dl, dh, eax, ebx) + ASMENCROUND(10, eax, al, ah, ebx, bl, bh, ecx, edx) + ASMENCROUND(11, ecx, cl, ch, edx, dl, dh, eax, ebx) + ASMENCROUND(12, eax, al, ah, ebx, bl, bh, ecx, edx) + ASMENCROUND(13, ecx, cl, ch, edx, dl, dh, eax, ebx) + ASMENCROUND(14, eax, al, ah, ebx, bl, bh, ecx, edx) + ASMENCROUND(15, ecx, cl, ch, edx, dl, dh, eax, ebx) + + + AS2( movd ebp, mm6 ) + AS2( movd esi, mm0 ) // k_ + #ifdef __GNUC__ + AS2( mov edi, [ebp + 16] ) // outBlock + #else + AS2( mov edi, [ebp + 12] ) // outBlock + #endif + + AS2( xor ecx, DWORD PTR [esi + 16] ) // k_[4] + AS2( xor edx, DWORD PTR [esi + 20] ) // k_[5] + AS2( xor eax, DWORD PTR [esi + 24] ) // k_[6] + AS2( xor ebx, DWORD PTR [esi + 28] ) // k_[7] + + AS2( mov [edi], ecx ) // write out + AS2( mov [edi + 4], edx ) // write out + AS2( mov [edi + 8], eax ) // write out + AS2( mov [edi + 12], ebx ) // write out + + + EPILOG() +} + + +#ifdef _MSC_VER + __declspec(naked) +#endif +void Twofish::AsmDecrypt(const byte* inBlock, byte* outBlock) const +{ + PROLOG() + + #ifdef OLD_GCC_OFFSET + AS2( add edi, 60 ) // k_ + #else + AS2( add edi, 56 ) // k_ + #endif + + AS2( mov ebp, edi ) + + AS2( mov ecx, DWORD PTR [esi] ) // c + AS2( movd mm0, edi ) // store k_ + AS2( mov edx, DWORD PTR [esi + 4] ) // d + AS2( add ebp, 160 ) // s_[0] + AS2( mov eax, DWORD PTR [esi + 8] ) // a + AS2( movd mm1, ebp ) // store s_ + AS2( mov ebx, DWORD PTR [esi + 12] ) // b + + AS2( xor ecx, DWORD PTR [edi + 16] ) // k_[4] + AS2( xor edx, DWORD PTR [edi + 20] ) // [5] + AS2( xor eax, DWORD PTR [edi + 24] ) // [6] + AS2( xor ebx, DWORD PTR [edi + 28] ) // [7] + + + ASMDECROUND(15, ecx, cl, ch, edx, dl, dh, eax, ebx) + ASMDECROUND(14, eax, al, ah, ebx, bl, bh, ecx, edx) + ASMDECROUND(13, ecx, cl, ch, edx, dl, dh, eax, ebx) + ASMDECROUND(12, eax, al, ah, ebx, bl, bh, ecx, edx) + ASMDECROUND(11, ecx, cl, ch, edx, dl, dh, eax, ebx) + ASMDECROUND(10, eax, al, ah, ebx, bl, bh, ecx, edx) + ASMDECROUND( 9, ecx, cl, ch, edx, dl, dh, eax, ebx) + ASMDECROUND( 8, eax, al, ah, ebx, bl, bh, ecx, edx) + ASMDECROUND( 7, ecx, cl, ch, edx, dl, dh, eax, ebx) + ASMDECROUND( 6, eax, al, ah, ebx, bl, bh, ecx, edx) + ASMDECROUND( 5, ecx, cl, ch, edx, dl, dh, eax, ebx) + ASMDECROUND( 4, eax, al, ah, ebx, bl, bh, ecx, edx) + ASMDECROUND( 3, ecx, cl, ch, edx, dl, dh, eax, ebx) + ASMDECROUND( 2, eax, al, ah, ebx, bl, bh, ecx, edx) + ASMDECROUND( 1, ecx, cl, ch, edx, dl, dh, eax, ebx) + ASMDECROUND( 0, eax, al, ah, ebx, bl, bh, ecx, edx) + + + AS2( movd ebp, mm6 ) + AS2( movd esi, mm0 ) // k_ + #ifdef __GNUC__ + AS2( mov edi, [ebp + 16] ) // outBlock + #else + AS2( mov edi, [ebp + 12] ) // outBlock + #endif + + AS2( xor eax, DWORD PTR [esi ] ) // k_[0] + AS2( xor ebx, DWORD PTR [esi + 4] ) // k_[1] + AS2( xor ecx, DWORD PTR [esi + 8] ) // k_[2] + AS2( xor edx, DWORD PTR [esi + 12] ) // k_[3] + + AS2( mov [edi], eax ) // write out + AS2( mov [edi + 4], ebx ) // write out + AS2( mov [edi + 8], ecx ) // write out + AS2( mov [edi + 12], edx ) // write out + + + EPILOG() +} + + + +#endif // defined(DO_TWOFISH_ASM) + + + + + +} // namespace + + |