diff options
Diffstat (limited to 'common')
-rw-r--r-- | common/certinfo.c | 91 | ||||
-rw-r--r-- | common/certinfo.h | 33 | ||||
-rw-r--r-- | common/eurephia_directions.h | 28 | ||||
-rw-r--r-- | common/eurephia_getsym.c | 44 | ||||
-rw-r--r-- | common/eurephia_getsym.h | 26 | ||||
-rw-r--r-- | common/eurephia_log.c | 77 | ||||
-rw-r--r-- | common/eurephia_log.h | 50 | ||||
-rw-r--r-- | common/eurephia_nullsafe.h | 33 | ||||
-rw-r--r-- | common/eurephia_values.c | 120 | ||||
-rw-r--r-- | common/eurephia_values.h | 36 | ||||
-rw-r--r-- | common/passwd.c | 67 | ||||
-rw-r--r-- | common/passwd.h | 27 | ||||
-rw-r--r-- | common/sha512.c | 480 | ||||
-rw-r--r-- | common/sha512.h | 72 |
14 files changed, 1184 insertions, 0 deletions
diff --git a/common/certinfo.c b/common/certinfo.c new file mode 100644 index 0000000..bb68a53 --- /dev/null +++ b/common/certinfo.c @@ -0,0 +1,91 @@ +/* certinfo.c -- Functions to parse and process the X509 TLS id string + * + * GPLv2 - Copyright (C) 2008 David Sommerseth <dazo@users.sourceforge.net> + * + * 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; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + */ + +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +#include <eurephia_nullsafe.h> +#include <certinfo.h> + + +#define comp_attrib(s, v) ( (v == NULL || strlen_nullsafe(v) < 1) ? 0 : (strcmp(v, s) == 0) ) + +certinfo *parse_tlsid(const char *input) { + char tmp[130], *mainp, *origptr, *sub, *tok, *tok2; + certinfo *ret = NULL; + + if( (input == NULL) || strlen(input) < 5) + return NULL; + + ret = (certinfo *) malloc(sizeof(certinfo)+2); + bzero(ret, sizeof(certinfo)+2); + bzero(&tmp, 130); + + mainp = strdup(input); + origptr = mainp; + tok = strsep(&mainp, "/\0"); + while( tok != NULL ) { + if( (tok != NULL) && (strlen(tok) > 0 ) ) { + sub = strdup(tok); + tok2 = strsep(&sub, "=\0"); + + if( comp_attrib("O\0", tok2) ) { + ret->org = strdup(strsep(&sub, "=\0")); + } else if( comp_attrib("CN\0", tok2) ) { + ret->common_name = strdup(strsep(&sub, "=\0")); + } else if( comp_attrib("emailAddress\0", tok2) ) { + ret->email = strdup(strsep(&sub, "=\0")); + } + if( tok2 != NULL ) { + free(tok2); tok2 = NULL; + } + } + tok = strsep(&mainp, "/\0"); + } + free(origptr); mainp = NULL; origptr = NULL; + + /* Make sure we at least have empty NULL terminated strings */ + if( ret->org == NULL ) { + ret->org = strdup("\0"); + } + if( ret->common_name == NULL ) { + ret->common_name = strdup("\0"); + } + if( ret->email == NULL ) { + ret->email = strdup("\0"); + } + + return ret; +} + +void free_certinfo(certinfo *p) { + if( p == NULL ) + return; + + if( p->org != NULL ) + free(p->org); + if( p->common_name != NULL ) + free(p->common_name); + if( p->email != NULL ) + free(p->email); + + free(p); +} diff --git a/common/certinfo.h b/common/certinfo.h new file mode 100644 index 0000000..d9496bd --- /dev/null +++ b/common/certinfo.h @@ -0,0 +1,33 @@ +/* certinfo.h -- Structure used when parsing X509 identification string + * + * GPLv2 - Copyright (C) 2008 David Sommerseth <dazo@users.sourceforge.net> + * + * 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; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + */ + +#ifndef __CERTINFO_H_ +#define __CERTINFO_H_ + +typedef struct _certinfo { + char *org; + char *common_name; + char *email; +} certinfo; + +certinfo *parse_tlsid(const char *); +void free_certinfo(certinfo *); + +#endif diff --git a/common/eurephia_directions.h b/common/eurephia_directions.h new file mode 100644 index 0000000..b410475 --- /dev/null +++ b/common/eurephia_directions.h @@ -0,0 +1,28 @@ +/* eurephia_directions.h -- Macro to decide the best search directon on dual-way pointer chains + * + * GPLv2 - Copyright (C) 2008 David Sommerseth <dazo@users.sourceforge.net> + * + * 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; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + */ + +#ifndef _EUREPHIA_DIRECTIONS_H +#define _EUREPHIA_DIRECTIONS_H + +#define DIR_R 'R' +#define DIR_L 'L' +#define DIRECTION(s,d,l) (s>d ? ((((l-s)+d) > (s-d)) ? DIR_L : DIR_R) : (((d-s) > ((l-d)+s)) ? DIR_L : DIR_R)) + +#endif diff --git a/common/eurephia_getsym.c b/common/eurephia_getsym.c new file mode 100644 index 0000000..dc85c29 --- /dev/null +++ b/common/eurephia_getsym.c @@ -0,0 +1,44 @@ +/* eurephia_getsym.c -- Retrieves symbols from dlopened libraries + * + * GPLv2 - Copyright (C) 2008 David Sommerseth <dazo@users.sourceforge.net> + * + * 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; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + */ + + +#include <stdio.h> +#include <dlfcn.h> + +#include "eurephia_struct.h" +#include "eurephia_log.h" + +void *eGetSym(eurephiaCTX *ctx, void *dlh, const char *symnam) +{ + void *func = NULL; + + if( ctx->fatal_error > 0 ) { + return NULL; + } + + DEBUG(ctx, 30, "Locating eDBlink function '%s'", symnam); + func = dlsym(dlh, symnam); + if( func == NULL ) { + eurephia_log(ctx, LOG_PANIC, 0, "Could not find needed '%s' function in eDBlink driver", symnam); + ctx->fatal_error = 1; + } + return func; +} + diff --git a/common/eurephia_getsym.h b/common/eurephia_getsym.h new file mode 100644 index 0000000..2f1fbb0 --- /dev/null +++ b/common/eurephia_getsym.h @@ -0,0 +1,26 @@ +/* eurephia_getsym.h -- Retrieves symbols from dlopened libraries + * + * GPLv2 - Copyright (C) 2008 David Sommerseth <dazo@users.sourceforge.net> + * + * 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; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + */ + +#ifndef EUREPHIA_GETSYM_H_ +#define EUREPHIA_GETSYM_H_ + +void *eGetSym(eurephiaCTX *ctx, const void *dlh, const char *symnam) ; + +#endif /* !EUREPHIA_GETSYM_H_ */ diff --git a/common/eurephia_log.c b/common/eurephia_log.c new file mode 100644 index 0000000..9ea6a8e --- /dev/null +++ b/common/eurephia_log.c @@ -0,0 +1,77 @@ +/* eurephia_log.c -- eurephia logging + * + * GPLv2 - Copyright (C) 2008 David Sommerseth <dazo@users.sourceforge.net> + * + * 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; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + */ + +#include <stdio.h> +#include <stdarg.h> +#include <string.h> +#include <time.h> +#include <pthread.h> + +#include "eurephia_struct.h" +#include "eurephia_log.h" + +const char *erp_logtypes[] = { + "\0", + "-- INFO -- \0", + "-- DEBUG -- \0", + "** WARNING ** \0", + "** ERROR ** \0", + "** CRITICAL ** \0", + "** - FATAL - ** \0", + "** * PANIC * ** \0" +}; + +// POSIX Mutex to avoild simultaneously logging activity from +// several threads at the same time +pthread_mutex_t log_mutex = PTHREAD_MUTEX_INITIALIZER; + +// Simple log function ... Write log data to the context log file +void eurephia_log(eurephiaCTX *ctx, int logdst, int loglvl, const char *fmt, ... ) { + + // Only log if we have an open log file and which has high enough log level + if( (ctx->log != NULL) && (ctx->loglevel >= loglvl) ) { + va_list ap; + char tstmp_str[200]; + time_t tstmp; + struct tm *loctstmp; + + // Get timestamp + memset(&tstmp_str, 0, 200); + tstmp = time(NULL); + loctstmp = localtime(&tstmp); + if( loctstmp != NULL ) { + if( strftime(tstmp_str, 198, "%Y-%m-%d %H:%M:%S %Z", loctstmp) == 0 ) { + snprintf(tstmp_str, 198, "(error getting timestamp string)"); + } + } else { + snprintf(tstmp_str, 198, "(error getting timestamp)"); + } + + va_start(ap, fmt); + pthread_mutex_lock(&log_mutex); // Block other threads from writing when we write + fprintf(ctx->log, "[%s] %s [%i] ", tstmp_str, erp_logtypes[logdst], loglvl); + vfprintf(ctx->log, fmt, ap); + fprintf(ctx->log, "\n"); + fflush(ctx->log); + pthread_mutex_unlock(&log_mutex); // Unblock other threads + va_end(ap); + } +} + diff --git a/common/eurephia_log.h b/common/eurephia_log.h new file mode 100644 index 0000000..9a2b227 --- /dev/null +++ b/common/eurephia_log.h @@ -0,0 +1,50 @@ +/* eurephia_log.h -- eurephia logging module + * + * GPLv2 - Copyright (C) 2008 David Sommerseth <dazo@users.sourceforge.net> + * + * 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; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + */ + +#ifndef EUREPHIA_LOG_H_ +#define EUREPHIA_LOG_H_ + +#include <eurephia_struct.h> + +#define LOG_INFO 1 +#define LOG_DEBUG 2 +#define LOG_WARNING 3 +#define LOG_ERROR 4 +#define LOG_CRITICAL 5 +#define LOG_FATAL 6 +#define LOG_PANIC 7 + +#ifdef ENABLE_DEBUG +#warning ###### DEBUG LOGGING IS ENABLED - THIS COULD BE A SECURITY ISSUE ###### +#define DEBUG(ctx, lvl, rest...) eurephia_log(ctx, LOG_DEBUG, lvl, ## rest); +#else +#define DEBUG(ctx, lvl, rest...) {}; +#endif + +#ifdef SHOW_SECRETS +#warning ########################################################################################## +#warning ## ## +#warning ## DEBUG LOGGING WITH SHOW_SECRETS IS ENABLED - THIS WILL LOG PASSWORDS IN CLEAR TEXT ## +#warning ## ## +#warning ########################################################################################## +#endif +void eurephia_log(eurephiaCTX *ctx, int logdst, int loglvl, const char *fmt, ... ); + +#endif /* !EUREPHIA_LOG_H_ */ diff --git a/common/eurephia_nullsafe.h b/common/eurephia_nullsafe.h new file mode 100644 index 0000000..a980cfe --- /dev/null +++ b/common/eurephia_nullsafe.h @@ -0,0 +1,33 @@ +/* eurephia_nullsafe.h + * + * standard C string functions, which is made NULL safe by checking + * if input value is NULL before performing the action. + * + * + * GPLv2 - Copyright (C) 2008 David Sommerseth <dazo@users.sourceforge.net> + * + * 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; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * +*/ + +#ifndef EUREPHIA_NULLSAFE_H_ +#define EUREPHIA_NULLSAFE_H_ + +#define atoi_nullsafe(str) (str != NULL ? atoi(str) : 0) +#define strdup_nullsafe(str) (str != NULL ? strdup(str) : NULL) +#define strlen_nullsafe(str) (str != NULL ? strlen(str) : 0) +#define free_nullsafe(str) if( str != NULL ) { free(str); str = NULL;} + +#endif /* !EUREPHIA_NULLSAFE_H_ */ diff --git a/common/eurephia_values.c b/common/eurephia_values.c new file mode 100644 index 0000000..d5473f9 --- /dev/null +++ b/common/eurephia_values.c @@ -0,0 +1,120 @@ +/* eurephia_values.c -- Generic interface for processing key->value pairs + * + * GPLv2 - Copyright (C) 2008 David Sommerseth <dazo@users.sourceforge.net> + * + * 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; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +#include <eurephia_nullsafe.h> +#include <eurephia_struct.h> +#include <eurephia_log.h> + + +eurephiaVALUES *eGet_valuestruct(eurephiaVALUES *vls, const char *key) +{ + eurephiaVALUES *ptr = NULL; + + if( vls == NULL ) { + return NULL; + } + + ptr = vls; + while( ptr != NULL ) { + if( (key != NULL) && (ptr->key != NULL) && (strcmp(key, ptr->key) == 0) ) { + return ptr; + } + ptr = ptr->next; + } + return NULL; +} + + +char *eGet_value(eurephiaVALUES *vls, const char *key) +{ + eurephiaVALUES *ptr = NULL; + + ptr = eGet_valuestruct(vls, key); + return (ptr != NULL ? ptr->val : NULL); +} + + +eurephiaVALUES *eCreate_value_space(eurephiaCTX *ctx, int evgid) +{ + eurephiaVALUES *ptr = NULL; + + DEBUG(ctx, 32, "Function call: eCreate_value_space(ctx, %i)", evgid); + + ptr = (eurephiaVALUES *) malloc(sizeof(eurephiaVALUES) + 2); + if( ptr == NULL ) { + eurephia_log(ctx, LOG_PANIC, 0, "Could not allocate memory for a new eurephiaVALUE struct"); + return NULL; + } + memset(ptr, 0, sizeof(eurephiaVALUES) + 2); + ptr->evgid = evgid; + return ptr; +} + + +void eAdd_value(eurephiaCTX *ctx, eurephiaVALUES *vls, const char *key, const char *val) +{ + eurephiaVALUES *ptr = NULL, *ptr2 = NULL; + int vid = 0; + + DEBUG(ctx, 31, "Function call: eAdd_value(ctx, vls(%i), '%s', '%s')", + (vls != NULL ? vls->evid : -1), key, val); + + // Allocate buffer and safe values + ptr = eCreate_value_space(ctx, vls->evid); + if( ptr == NULL ) { + eurephia_log(ctx, LOG_FATAL, 0, "Could not add the new value"); + return; + } + ptr->key = strdup_nullsafe(key); + ptr->val = strdup_nullsafe(val); + ptr->evgid = vls->evgid; + + // Add values to the value chain, loop to the end and append it + ptr2 = vls; + while( ptr2->next != NULL ) { + vid = ptr2->evid; + ptr2 = ptr2->next; + } + ptr->evid = vid+1; // Increase the value counter + ptr2->next = ptr; +} + + +void do_free_vals(eurephiaVALUES *vls) { + if( vls->next != NULL ) { + do_free_vals(vls->next); + } + free_nullsafe(vls->key); + free_nullsafe(vls->val); + free_nullsafe(vls); +} + +void eFree_values_func(eurephiaCTX *ctx, eurephiaVALUES *vls) { + DEBUG(ctx, 31, "Function call: eFree_values(ctx, vls(%i))", + (vls != NULL ? vls->evid : -1)); + + if( (vls == NULL) ) { + return; + } + do_free_vals(vls); +} diff --git a/common/eurephia_values.h b/common/eurephia_values.h new file mode 100644 index 0000000..8a48730 --- /dev/null +++ b/common/eurephia_values.h @@ -0,0 +1,36 @@ +/* eurephia_values.h -- Generic interface for processing key->value pairs + * + * GPLv2 - Copyright (C) 2008 David Sommerseth <dazo@users.sourceforge.net> + * + * 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; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + */ + +#ifndef EUREPHIA_VALUES_H_ +# define EUREPHIA_VALUES_H_ + +#include <eurephia_struct.h> + +eurephiaVALUES *eGet_valuestruct(eurephiaVALUES *vls, const char *key); +char *eGet_value(eurephiaVALUES *vls, const char *key); + +eurephiaVALUES *eCreate_value_space(eurephiaCTX *ctx, int evid); + +void eAdd_value(eurephiaCTX *ctx, eurephiaVALUES *vls, const char *key, const char *val); + +#define eFree_values(c, v) { eFree_values_func(c, v); v = NULL; } +void eFree_values_func(eurephiaCTX *ctx, eurephiaVALUES *vls); + +#endif /* !EUREPHIA_VALUES_H_ */ diff --git a/common/passwd.c b/common/passwd.c new file mode 100644 index 0000000..c77fccb --- /dev/null +++ b/common/passwd.c @@ -0,0 +1,67 @@ +/* passwd.c -- Generates a SHA512 hash of a clear-text password + * + * GPLv2 - Copyright (C) 2008 David Sommerseth <dazo@users.sourceforge.net> + * + * 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; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + */ + +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <stdint.h> + +#include "eurephia_nullsafe.h" +#include "sha512.h" + +char *_passwdhash(const char *pwd, const char *file, const int line) { + SHA512Context sha; + uint8_t sha_res[SHA512_HASH_SIZE]; + char *ret = NULL, *ptr = NULL; + unsigned len = 0, i; + + len = strlen_nullsafe(pwd); + if( (pwd == NULL) && (len == 0) ) { + return NULL; + } + + // Generate SHA512 hash of password + memset(&sha, 0, sizeof(SHA512Context)); + memset(&sha_res, 0, sizeof(sha_res)); + SHA512Init(&sha); + SHA512Update(&sha, pwd, len); + SHA512Final(&sha, sha_res); + + // Allocate memory for the return buffer +#ifdef MEMWATCH + ret = (char *) mwMalloc((SHA512_HASH_SIZE*2)+3, file, line); +#else + ret = (char *) malloc((SHA512_HASH_SIZE*2)+3); +#endif + memset(ret, 0,(SHA512_HASH_SIZE*2)+3); + ptr = ret; + + // Generate a readable string of the hash + for( i = 0; i < SHA512_HASH_SIZE; i++ ) { + sprintf(ptr, "%02x", sha_res[i]); + ptr += 2; + } + + // Cleanup - remove hash data from memory + memset(&sha, 0, sizeof(SHA512Context)); + memset(&sha_res, 0, sizeof(sha_res)); + + return ret; +} diff --git a/common/passwd.h b/common/passwd.h new file mode 100644 index 0000000..44b4ca2 --- /dev/null +++ b/common/passwd.h @@ -0,0 +1,27 @@ +/* passwd.h -- Creates SHA512 hash from a clear text password + * + * GPLv2 - Copyright (C) 2008 David Sommerseth <dazo@users.sourceforge.net> + * + * 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; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + */ + +#ifndef PASSWD_H +#define PASSWD_H + +#define passwdhash(p) _passwdhash(p, __FILE__, __LINE__) +char *_passwdhash(const char *pwd, const char *, const int); + +#endif diff --git a/common/sha512.c b/common/sha512.c new file mode 100644 index 0000000..37f4a72 --- /dev/null +++ b/common/sha512.c @@ -0,0 +1,480 @@ +/*- + * + * This code is extracted from the SourceForce.net project found at: + * + * https://sourceforge.net/projects/sha/ + * + * + * Copyright (c) 2001-2003 Allan Saddi <allan@saddi.com> + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY ALLAN SADDI AND HIS CONTRIBUTORS ``AS IS'' + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL ALLAN SADDI OR HIS CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + * $Id: sha512.c 680 2003-07-25 21:58:07Z asaddi $ + */ + +/* + * Define WORDS_BIGENDIAN if compiling on a big-endian architecture. + * + * Define SHA512_TEST to test the implementation using the NIST's + * sample messages. The output should be: + * + * ddaf35a193617aba cc417349ae204131 12e6fa4e89a97ea2 0a9eeee64b55d39a + * 2192992a274fc1a8 36ba3c23a3feebbd 454d4423643ce80e 2a9ac94fa54ca49f + * 8e959b75dae313da 8cf4f72814fc143f 8f7779c6eb9f7fa1 7299aeadb6889018 + * 501d289e4900f7e4 331b99dec4b5433a c7d329eeb6dd2654 5e96e55b874be909 + * e718483d0ce76964 4e2e42c7bc15b463 8e1f98b13b204428 5632a803afa973eb + * de0ff244877ea60a 4cb0432ce577c31b eb009c5c2c49aa2e 4eadb217ad8cc09b + */ + +#include <stdint.h> +#include <string.h> + +#include "sha512.h" + +#ifndef lint +static const char rcsid[] = + "$Id: sha512.c 680 2003-07-25 21:58:07Z asaddi $"; +#endif /* !lint */ + +#define ROTL(x, n) (((x) << (n)) | ((x) >> (32 - (n)))) +#define ROTR(x, n) (((x) >> (n)) | ((x) << (32 - (n)))) +#define ROTL64(x, n) (((x) << (n)) | ((x) >> (64 - (n)))) +#define ROTR64(x, n) (((x) >> (n)) | ((x) << (64 - (n)))) + +#define Ch(x, y, z) ((z) ^ ((x) & ((y) ^ (z)))) +#define Maj(x, y, z) (((x) & ((y) | (z))) | ((y) & (z))) +#define SIGMA0(x) (ROTR64((x), 28) ^ ROTR64((x), 34) ^ ROTR64((x), 39)) +#define SIGMA1(x) (ROTR64((x), 14) ^ ROTR64((x), 18) ^ ROTR64((x), 41)) +#define sigma0(x) (ROTR64((x), 1) ^ ROTR64((x), 8) ^ ((x) >> 7)) +#define sigma1(x) (ROTR64((x), 19) ^ ROTR64((x), 61) ^ ((x) >> 6)) + +#define DO_ROUND() { \ + t1 = h + SIGMA1(e) + Ch(e, f, g) + *(Kp++) + *(W++); \ + t2 = SIGMA0(a) + Maj(a, b, c); \ + h = g; \ + g = f; \ + f = e; \ + e = d + t1; \ + d = c; \ + c = b; \ + b = a; \ + a = t1 + t2; \ +} + +static const uint64_t K[80] = { + 0x428a2f98d728ae22LL, 0x7137449123ef65cdLL, + 0xb5c0fbcfec4d3b2fLL, 0xe9b5dba58189dbbcLL, + 0x3956c25bf348b538LL, 0x59f111f1b605d019LL, + 0x923f82a4af194f9bLL, 0xab1c5ed5da6d8118LL, + 0xd807aa98a3030242LL, 0x12835b0145706fbeLL, + 0x243185be4ee4b28cLL, 0x550c7dc3d5ffb4e2LL, + 0x72be5d74f27b896fLL, 0x80deb1fe3b1696b1LL, + 0x9bdc06a725c71235LL, 0xc19bf174cf692694LL, + 0xe49b69c19ef14ad2LL, 0xefbe4786384f25e3LL, + 0x0fc19dc68b8cd5b5LL, 0x240ca1cc77ac9c65LL, + 0x2de92c6f592b0275LL, 0x4a7484aa6ea6e483LL, + 0x5cb0a9dcbd41fbd4LL, 0x76f988da831153b5LL, + 0x983e5152ee66dfabLL, 0xa831c66d2db43210LL, + 0xb00327c898fb213fLL, 0xbf597fc7beef0ee4LL, + 0xc6e00bf33da88fc2LL, 0xd5a79147930aa725LL, + 0x06ca6351e003826fLL, 0x142929670a0e6e70LL, + 0x27b70a8546d22ffcLL, 0x2e1b21385c26c926LL, + 0x4d2c6dfc5ac42aedLL, 0x53380d139d95b3dfLL, + 0x650a73548baf63deLL, 0x766a0abb3c77b2a8LL, + 0x81c2c92e47edaee6LL, 0x92722c851482353bLL, + 0xa2bfe8a14cf10364LL, 0xa81a664bbc423001LL, + 0xc24b8b70d0f89791LL, 0xc76c51a30654be30LL, + 0xd192e819d6ef5218LL, 0xd69906245565a910LL, + 0xf40e35855771202aLL, 0x106aa07032bbd1b8LL, + 0x19a4c116b8d2d0c8LL, 0x1e376c085141ab53LL, + 0x2748774cdf8eeb99LL, 0x34b0bcb5e19b48a8LL, + 0x391c0cb3c5c95a63LL, 0x4ed8aa4ae3418acbLL, + 0x5b9cca4f7763e373LL, 0x682e6ff3d6b2b8a3LL, + 0x748f82ee5defb2fcLL, 0x78a5636f43172f60LL, + 0x84c87814a1f0ab72LL, 0x8cc702081a6439ecLL, + 0x90befffa23631e28LL, 0xa4506cebde82bde9LL, + 0xbef9a3f7b2c67915LL, 0xc67178f2e372532bLL, + 0xca273eceea26619cLL, 0xd186b8c721c0c207LL, + 0xeada7dd6cde0eb1eLL, 0xf57d4f7fee6ed178LL, + 0x06f067aa72176fbaLL, 0x0a637dc5a2c898a6LL, + 0x113f9804bef90daeLL, 0x1b710b35131c471bLL, + 0x28db77f523047d84LL, 0x32caab7b40c72493LL, + 0x3c9ebe0a15c9bebcLL, 0x431d67c49c100d4cLL, + 0x4cc5d4becb3e42b6LL, 0x597f299cfc657e2aLL, + 0x5fcb6fab3ad6faecLL, 0x6c44198c4a475817LL +}; + +#ifndef RUNTIME_ENDIAN + +#ifdef WORDS_BIGENDIAN + +#define BYTESWAP(x) (x) +#define BYTESWAP64(x) (x) + +#else /* WORDS_BIGENDIAN */ + +#define BYTESWAP(x) ((ROTR((x), 8) & 0xff00ff00L) | \ + (ROTL((x), 8) & 0x00ff00ffL)) +#define BYTESWAP64(x) _byteswap64(x) + +static inline uint64_t _byteswap64(uint64_t x) +{ + uint32_t a = x >> 32; + uint32_t b = (uint32_t) x; + return ((uint64_t) BYTESWAP(b) << 32) | (uint64_t) BYTESWAP(a); +} + +#endif /* WORDS_BIGENDIAN */ + +#else /* !RUNTIME_ENDIAN */ + +#define BYTESWAP(x) _byteswap(sc->littleEndian, x) +#define BYTESWAP64(x) _byteswap64(sc->littleEndian, x) + +#define _BYTESWAP(x) ((ROTR((x), 8) & 0xff00ff00L) | \ + (ROTL((x), 8) & 0x00ff00ffL)) +#define _BYTESWAP64(x) __byteswap64(x) + +static inline uint64_t __byteswap64(uint64_t x) +{ + uint32_t a = x >> 32; + uint32_t b = (uint32_t) x; + return ((uint64_t) _BYTESWAP(b) << 32) | (uint64_t) _BYTESWAP(a); +} + +static inline uint32_t _byteswap(int littleEndian, uint32_t x) +{ + if (!littleEndian) + return x; + else + return _BYTESWAP(x); +} + +static inline uint64_t _byteswap64(int littleEndian, uint64_t x) +{ + if (!littleEndian) + return x; + else + return _BYTESWAP64(x); +} + +static inline void setEndian(int *littleEndianp) +{ + union { + uint32_t w; + uint8_t b[4]; + } endian; + + endian.w = 1L; + *littleEndianp = endian.b[0] != 0; +} + +#endif /* !RUNTIME_ENDIAN */ + +static const uint8_t padding[128] = { + 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 +}; + +void +SHA512Init (SHA512Context *sc) +{ +#ifdef RUNTIME_ENDIAN + setEndian (&sc->littleEndian); +#endif /* RUNTIME_ENDIAN */ + + sc->totalLength[0] = 0LL; + sc->totalLength[1] = 0LL; + sc->hash[0] = 0x6a09e667f3bcc908LL; + sc->hash[1] = 0xbb67ae8584caa73bLL; + sc->hash[2] = 0x3c6ef372fe94f82bLL; + sc->hash[3] = 0xa54ff53a5f1d36f1LL; + sc->hash[4] = 0x510e527fade682d1LL; + sc->hash[5] = 0x9b05688c2b3e6c1fLL; + sc->hash[6] = 0x1f83d9abfb41bd6bLL; + sc->hash[7] = 0x5be0cd19137e2179LL; + sc->bufferLength = 0L; +} + +static void +burnStack (int size) +{ + char buf[128]; + + memset (buf, 0, sizeof (buf)); + size -= sizeof (buf); + if (size > 0) + burnStack (size); +} + +static void +SHA512Guts (SHA512Context *sc, const uint64_t *cbuf) +{ + uint64_t buf[80]; + uint64_t *W, *W2, *W7, *W15, *W16; + uint64_t a, b, c, d, e, f, g, h; + uint64_t t1, t2; + const uint64_t *Kp; + int i; + + W = buf; + + for (i = 15; i >= 0; i--) { + *(W++) = BYTESWAP64(*cbuf); + cbuf++; + } + + W16 = &buf[0]; + W15 = &buf[1]; + W7 = &buf[9]; + W2 = &buf[14]; + + for (i = 63; i >= 0; i--) { + *(W++) = sigma1(*W2) + *(W7++) + sigma0(*W15) + *(W16++); + W2++; + W15++; + } + + a = sc->hash[0]; + b = sc->hash[1]; + c = sc->hash[2]; + d = sc->hash[3]; + e = sc->hash[4]; + f = sc->hash[5]; + g = sc->hash[6]; + h = sc->hash[7]; + + Kp = K; + W = buf; + + for (i = 79; i >= 0; i--) + DO_ROUND(); + + sc->hash[0] += a; + sc->hash[1] += b; + sc->hash[2] += c; + sc->hash[3] += d; + sc->hash[4] += e; + sc->hash[5] += f; + sc->hash[6] += g; + sc->hash[7] += h; +} + +void +SHA512Update (SHA512Context *sc, const void *vdata, uint32_t len) +{ + const uint8_t *data = vdata; + uint32_t bufferBytesLeft; + uint32_t bytesToCopy; + uint64_t carryCheck; + int needBurn = 0; + +#ifdef SHA512_FAST_COPY + if (sc->bufferLength) { + bufferBytesLeft = 128L - sc->bufferLength; + + bytesToCopy = bufferBytesLeft; + if (bytesToCopy > len) + bytesToCopy = len; + + memcpy (&sc->buffer.bytes[sc->bufferLength], data, bytesToCopy); + + carryCheck = sc->totalLength[1]; + sc->totalLength[1] += bytesToCopy * 8L; + if (sc->totalLength[1] < carryCheck) + sc->totalLength[0]++; + + sc->bufferLength += bytesToCopy; + data += bytesToCopy; + len -= bytesToCopy; + + if (sc->bufferLength == 128L) { + SHA512Guts (sc, sc->buffer.words); + needBurn = 1; + sc->bufferLength = 0L; + } + } + + while (len > 127) { + carryCheck = sc->totalLength[1]; + sc->totalLength[1] += 1024L; + if (sc->totalLength[1] < carryCheck) + sc->totalLength[0]++; + + SHA512Guts (sc, data); + needBurn = 1; + + data += 128L; + len -= 128L; + } + + if (len) { + memcpy (&sc->buffer.bytes[sc->bufferLength], data, len); + + carryCheck = sc->totalLength[1]; + sc->totalLength[1] += len * 8L; + if (sc->totalLength[1] < carryCheck) + sc->totalLength[0]++; + + sc->bufferLength += len; + } +#else /* SHA512_FAST_COPY */ + while (len) { + bufferBytesLeft = 128L - sc->bufferLength; + + bytesToCopy = bufferBytesLeft; + if (bytesToCopy > len) + bytesToCopy = len; + + memcpy (&sc->buffer.bytes[sc->bufferLength], data, bytesToCopy); + + carryCheck = sc->totalLength[1]; + sc->totalLength[1] += bytesToCopy * 8L; + if (sc->totalLength[1] < carryCheck) + sc->totalLength[0]++; + + sc->bufferLength += bytesToCopy; + data += bytesToCopy; + len -= bytesToCopy; + + if (sc->bufferLength == 128L) { + SHA512Guts (sc, sc->buffer.words); + needBurn = 1; + sc->bufferLength = 0L; + } + } +#endif /* SHA512_FAST_COPY */ + + if (needBurn) + burnStack (sizeof (uint64_t[90]) + sizeof (uint64_t *[6]) + sizeof (int)); +} + +void +SHA512Final (SHA512Context *sc, uint8_t hash[SHA512_HASH_SIZE]) +{ + uint32_t bytesToPad; + uint64_t lengthPad[2]; + int i; + + bytesToPad = 240L - sc->bufferLength; + if (bytesToPad > 128L) + bytesToPad -= 128L; + + lengthPad[0] = BYTESWAP64(sc->totalLength[0]); + lengthPad[1] = BYTESWAP64(sc->totalLength[1]); + + SHA512Update (sc, padding, bytesToPad); + SHA512Update (sc, lengthPad, 16L); + + if (hash) { + for (i = 0; i < SHA512_HASH_WORDS; i++) { +#ifdef SHA384_FAST_COPY + *((uint64_t *) hash) = BYTESWAP64(sc->hash[i]); +#else /* SHA384_FAST_COPY */ + hash[0] = (uint8_t) (sc->hash[i] >> 56); + hash[1] = (uint8_t) (sc->hash[i] >> 48); + hash[2] = (uint8_t) (sc->hash[i] >> 40); + hash[3] = (uint8_t) (sc->hash[i] >> 32); + hash[4] = (uint8_t) (sc->hash[i] >> 24); + hash[5] = (uint8_t) (sc->hash[i] >> 16); + hash[6] = (uint8_t) (sc->hash[i] >> 8); + hash[7] = (uint8_t) sc->hash[i]; +#endif /* SHA384_FAST_COPY */ + hash += 8; + } + } +} + +#ifdef SHA512_TEST + +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +int +main (int argc, char *argv[]) +{ + SHA512Context foo; + uint8_t hash[SHA512_HASH_SIZE]; + char buf[1000]; + int i; + + SHA512Init (&foo); + SHA512Update (&foo, "abc", 3); + SHA512Final (&foo, hash); + + for (i = 0; i < SHA512_HASH_SIZE;) { + printf ("%02x", hash[i++]); + if (!(i % 8)) + printf (" "); + if (!(i % 32)) + printf ("\n"); + } + + SHA512Init (&foo); + SHA512Update (&foo, + "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmn" + "hijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu", + 112); + SHA512Final (&foo, hash); + + for (i = 0; i < SHA512_HASH_SIZE;) { + printf ("%02x", hash[i++]); + if (!(i % 8)) + printf (" "); + if (!(i % 32)) + printf ("\n"); + } + + SHA512Init (&foo); + memset (buf, 'a', sizeof (buf)); + for (i = 0; i < 1000; i++) + SHA512Update (&foo, buf, sizeof (buf)); + SHA512Final (&foo, hash); + + for (i = 0; i < SHA512_HASH_SIZE;) { + printf ("%02x", hash[i++]); + if (!(i % 8)) + printf (" "); + if (!(i % 32)) + printf ("\n"); + } + + exit (0); +} + +#endif /* SHA512_TEST */ diff --git a/common/sha512.h b/common/sha512.h new file mode 100644 index 0000000..a6962c2 --- /dev/null +++ b/common/sha512.h @@ -0,0 +1,72 @@ +/*- + * + * This code is extracted from the SourceForce.net project found at: + * + * https://sourceforge.net/projects/sha/ + * + * + * Copyright (c) 2001-2003 Allan Saddi <allan@saddi.com> + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY ALLAN SADDI AND HIS CONTRIBUTORS ``AS IS'' + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL ALLAN SADDI OR HIS CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + * $Id: sha512.h 350 2003-02-23 22:12:33Z asaddi $ + */ + +#ifndef _SHA512_H +#define _SHA512_H + +#include <stdint.h> + +#define SHA512_HASH_SIZE 64 + +/* Hash size in 64-bit words */ +#define SHA512_HASH_WORDS 8 + +struct _SHA512Context { + uint64_t totalLength[2]; + uint64_t hash[SHA512_HASH_WORDS]; + uint32_t bufferLength; + union { + uint64_t words[16]; + uint8_t bytes[128]; + } buffer; +#ifdef RUNTIME_ENDIAN + int littleEndian; +#endif /* RUNTIME_ENDIAN */ +}; + +typedef struct _SHA512Context SHA512Context; + +#ifdef __cplusplus +extern "C" { +#endif + +void SHA512Init (SHA512Context *sc); +void SHA512Update (SHA512Context *sc, const void *data, uint32_t len); +void SHA512Final (SHA512Context *sc, uint8_t hash[SHA512_HASH_SIZE]); + +#ifdef __cplusplus +} +#endif + +#endif /* !_SHA512_H */ |