Compare commits

...
10 Commits
7 changed files with 515 additions and 104 deletions

No files matched your search

+9
View File
@@ -138,6 +138,9 @@ static const MonoRuntimeInfo supported_runtimes[] = {
static MonoCreateDomainFunc create_domain_hook;
static MonoFreeDomainFunc free_domain_hook;
/* AOT cache configuration */
static MonoAotCacheConfig aot_cache_config;
/* This is intentionally not in the header file, so people don't misuse it. */
extern void _mono_debug_init_corlib (MonoDomain *domain);
@@ -2776,3 +2779,9 @@ mono_enable_debug_domain_unload (gboolean enable)
{
debug_domain_unload = enable;
}
MonoAotCacheConfig *
mono_get_aot_cache_config (void)
{
return &aot_cache_config;
}
+10
View File
@@ -523,6 +523,14 @@ struct _MonoMethodSignature {
MonoType *params [MONO_ZERO_LEN_ARRAY];
};
/*
* AOT cache configuration loaded from config files.
* Doesn't really belong here.
*/
typedef struct {
GSList *assemblies;
} MonoAotCacheConfig;
#define MONO_SIZEOF_METHOD_SIGNATURE (sizeof (struct _MonoMethodSignature) - MONO_ZERO_LEN_ARRAY * SIZEOF_VOID_P)
static inline gboolean
@@ -785,5 +793,7 @@ MonoMethod *mono_get_method_constrained_with_method (MonoImage *image, MonoMetho
void mono_type_set_alignment (MonoTypeEnum type, int align) MONO_INTERNAL;
MonoAotCacheConfig *mono_get_aot_cache_config (void) MONO_INTERNAL;
#endif /* __MONO_METADATA_INTERNALS_H__ */
+40 -1
View File
@@ -357,6 +357,45 @@ legacyUEP_handler = {
NULL, /* finish */
};
static void
aot_cache_start (gpointer user_data,
const gchar *element_name,
const gchar **attribute_names,
const gchar **attribute_values)
{
int i;
MonoAotCacheConfig *config;
if (strcmp (element_name, "aotcache") != 0)
return;
config = mono_get_aot_cache_config ();
for (i = 0; attribute_names [i]; ++i) {
if (!strcmp (attribute_names [i], "assemblies")) {
char **parts, **ptr;
char *part;
parts = g_strsplit (attribute_values [i], " ", -1);
for (ptr = parts; ptr && *ptr; ptr ++) {
part = *ptr;
config->assemblies = g_slist_prepend (config->assemblies, g_strdup (part));
}
g_strfreev (parts);
}
}
}
static const MonoParseHandler
aot_cache_handler = {
"aotcache",
NULL, /* init */
aot_cache_start,
NULL, /* text */
NULL, /* end */
NULL, /* finish */
};
static int inited = 0;
static void
@@ -366,6 +405,7 @@ mono_config_init (void)
config_handlers = g_hash_table_new (g_str_hash, g_str_equal);
g_hash_table_insert (config_handlers, (gpointer) dllmap_handler.element_name, (gpointer) &dllmap_handler);
g_hash_table_insert (config_handlers, (gpointer) legacyUEP_handler.element_name, (gpointer) &legacyUEP_handler);
g_hash_table_insert (config_handlers, (gpointer) aot_cache_handler.element_name, (gpointer) &aot_cache_handler);
}
void
@@ -407,7 +447,6 @@ mono_config_parse_file_with_context (ParseState *state, const char *filename)
if (!g_file_get_contents (filename, &text, &len, NULL))
return 0;
offset = 0;
if (len > 3 && text [0] == '\xef' && text [1] == (gchar) '\xbb' && text [2] == '\xbf')
offset = 3; /* Skip UTF-8 BOM */
+229 -102
View File
@@ -55,12 +55,17 @@
#include <mono/utils/mono-mmap.h>
#include "mono/utils/mono-compiler.h"
#include <mono/utils/mono-counters.h>
#include <mono/utils/mono-digest.h>
#include "mini.h"
#include "version.h"
#ifndef DISABLE_AOT
#ifdef TARGET_OSX
#define ENABLE_AOT_CACHE
#endif
#ifdef TARGET_WIN32
#define SHARED_EXT ".dll"
#elif ((defined(__ppc__) || defined(__powerpc__) || defined(__ppc64__)) || defined(__MACH__)) && !defined(__linux__)
@@ -167,15 +172,9 @@ static GHashTable *ji_to_amodule;
/*
* Whenever to AOT compile loaded assemblies on demand and store them in
* a cache under $HOME/.mono/aot-cache.
* a cache.
*/
static gboolean use_aot_cache = FALSE;
/*
* Whenever to spawn a new process to AOT a file or do it in-process. Only relevant if
* use_aot_cache is TRUE.
*/
static gboolean spawn_compiler = TRUE;
static gboolean enable_aot_cache = FALSE;
/* For debugging */
static gint32 mono_last_aot_method = -1;
@@ -243,7 +242,7 @@ load_image (MonoAotModule *amodule, int index, gboolean set_error)
assembly = mono_assembly_load (&amodule->image_names [index], amodule->assembly->basedir, &status);
if (!assembly) {
mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT module %s is unusable because dependency %s is not found.\n", amodule->aot_name, amodule->image_names [index].name);
mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT: module %s is unusable because dependency %s is not found.\n", amodule->aot_name, amodule->image_names [index].name);
amodule->out_of_date = TRUE;
if (set_error) {
@@ -255,7 +254,7 @@ load_image (MonoAotModule *amodule, int index, gboolean set_error)
}
if (strcmp (assembly->image->guid, amodule->image_guids [index])) {
mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT module %s is unusable (GUID of dependent assembly %s doesn't match (expected '%s', got '%s').\n", amodule->aot_name, amodule->image_names [index].name, amodule->image_guids [index], assembly->image->guid);
mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT: module %s is unusable (GUID of dependent assembly %s doesn't match (expected '%s', got '%s').\n", amodule->aot_name, amodule->image_names [index].name, amodule->image_guids [index], assembly->image->guid);
amodule->out_of_date = TRUE;
return NULL;
}
@@ -1272,132 +1271,249 @@ decode_resolve_method_ref (MonoAotModule *module, guint8 *buf, guint8 **endbuf)
return decode_resolve_method_ref_with_target (module, NULL, buf, endbuf);
}
#ifdef ENABLE_AOT_CACHE
/* AOT CACHE */
/*
* FIXME:
* - Add options for controlling the cache size
* - Handle full cache by deleting old assemblies lru style
* - Add options for controlling the set of assemblies to be included/excluded
* - Maybe add a threshold after an assembly is AOT compiled
* - Add options for enabling this for specific main assemblies
* - Cache failures
* - Add a config file
* - Add a way to disable by the user
* - Avoid waiting for the aot process to finish ?
* (less overhead, but multiple processes could aot the same assembly at the same time)
* - locking
*/
/* The cache directory */
static char *cache_dir;
/* The number of assemblies AOTed in this run */
static int cache_count;
/* Whenever to AOT in-process */
static gboolean in_process;
static void
create_cache_structure (void)
collect_assemblies (gpointer data, gpointer user_data)
{
const char *home;
char *tmp;
int err;
MonoAssembly *ass = data;
GSList **l = user_data;
home = g_get_home_dir ();
if (!home)
return;
*l = g_slist_prepend (*l, ass);
}
tmp = g_build_filename (home, ".mono", NULL);
if (!g_file_test (tmp, G_FILE_TEST_IS_DIR)) {
mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT creating directory %s", tmp);
#ifdef HOST_WIN32
err = mkdir (tmp);
#else
err = mkdir (tmp, 0777);
#endif
if (err) {
mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT failed: %s", g_strerror (errno));
g_free (tmp);
return;
}
#define SHA1_DIGEST_LENGTH 20
/*
* get_aot_config_hash:
*
* Return a hash for all the version information an AOT module depends on.
*/
static G_GNUC_UNUSED char*
get_aot_config_hash (MonoAssembly *assembly)
{
char *build_info;
GSList *l, *assembly_list = NULL;
GString *s;
int i;
guint8 digest [SHA1_DIGEST_LENGTH];
char *digest_str;
build_info = mono_get_runtime_build_info ();
s = g_string_new (build_info);
mono_assembly_foreach (collect_assemblies, &assembly_list);
/*
* The assembly list includes the current assembly as well, no need
* to add it.
*/
for (l = assembly_list; l; l = l->next) {
MonoAssembly *ass = l->data;
g_string_append (s, "_");
g_string_append (s, ass->aname.name);
g_string_append (s, "_");
g_string_append (s, ass->image->guid);
}
g_free (tmp);
tmp = g_build_filename (home, ".mono", "aot-cache", NULL);
if (!g_file_test (tmp, G_FILE_TEST_IS_DIR)) {
mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT creating directory %s", tmp);
#ifdef HOST_WIN32
err = mkdir (tmp);
#else
err = mkdir (tmp, 0777);
#endif
if (err) {
mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT failed: %s", g_strerror (errno));
g_free (tmp);
return;
}
for (i = 0; i < s->len; ++i) {
if (!isalnum (s->str [i]) && s->str [i] != '-')
s->str [i] = '_';
}
g_free (tmp);
mono_sha1_get_digest ((guint8*)s->str, s->len, digest);
digest_str = g_malloc0 ((SHA1_DIGEST_LENGTH * 2) + 1);
for (i = 0; i < SHA1_DIGEST_LENGTH; ++i)
sprintf (digest_str + (i * 2), "%02x", digest [i]);
mono_trace (G_LOG_LEVEL_MESSAGE, MONO_TRACE_AOT, "AOT: file dependencies: %s, hash %s", s->str, digest_str);
g_string_free (s, TRUE);
return digest_str;
}
static void
aot_cache_init (void)
{
enable_aot_cache = TRUE;
in_process = TRUE;
}
/*
* load_aot_module_from_cache:
* aot_cache_load_module:
*
* Experimental code to AOT compile loaded assemblies on demand.
*
* FIXME:
* - Add environment variable MONO_AOT_CACHE_OPTIONS
* - Add options for controlling the cache size
* - Handle full cache by deleting old assemblies lru style
* - Add options for excluding assemblies during development
* - Maybe add a threshold after an assembly is AOT compiled
* - invoking a new mono process is a security risk
* - recompile the AOT module if one of its dependencies changes
* Load the AOT image corresponding to ASSEMBLY from the aot cache, AOTing it if neccessary.
*/
static MonoDl*
load_aot_module_from_cache (MonoAssembly *assembly, char **aot_name)
aot_cache_load_module (MonoAssembly *assembly, char **aot_name)
{
char *fname, *cmd, *tmp2, *aot_options;
MonoAotCacheConfig *config;
GSList *l;
char *fname, *tmp2, *aot_options;
const char *home;
MonoDl *module;
gboolean res;
gchar *out, *err;
gint exit_status;
char *hash;
int pid;
*aot_name = NULL;
if (image_is_dynamic (assembly->image))
return NULL;
create_cache_structure ();
/* Check in the list of assemblies enabled for aot caching */
config = mono_get_aot_cache_config ();
for (l = config->assemblies; l; l = l->next) {
char *n = l->data;
home = g_get_home_dir ();
if (!strcmp (assembly->aname.name, n))
break;
}
if (!l)
return NULL;
tmp2 = g_strdup_printf ("%s-%s%s", assembly->image->assembly_name, assembly->image->guid, SHARED_EXT);
fname = g_build_filename (home, ".mono", "aot-cache", tmp2, NULL);
if (!cache_dir) {
home = g_get_home_dir ();
if (!home)
return NULL;
cache_dir = g_strdup_printf ("%s/Library/Caches/mono/aot-cache", home);
if (!g_file_test (cache_dir, G_FILE_TEST_EXISTS|G_FILE_TEST_IS_DIR))
g_mkdir_with_parents (cache_dir, 0777);
}
/*
* The same assembly can be used in multiple configurations, i.e. multiple
* versions of the runtime, with multiple versions of dependent assemblies etc.
* To handle this, we compute a version string containing all this information, hash it,
* and use the hash as a filename suffix.
*/
hash = get_aot_config_hash (assembly);
tmp2 = g_strdup_printf ("%s-%s%s", assembly->image->assembly_name, hash, SHARED_EXT);
fname = g_build_filename (cache_dir, tmp2, NULL);
*aot_name = fname;
g_free (tmp2);
mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT trying to load from cache: '%s'.", fname);
mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT: loading from cache: '%s'.", fname);
module = mono_dl_open (fname, MONO_DL_LAZY, NULL);
if (!module) {
mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT not found.");
if (module)
return module;
mono_trace (G_LOG_LEVEL_MESSAGE, MONO_TRACE_AOT, "AOT precompiling assembly '%s'... ", assembly->image->name);
mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT: not found.");
if (!strcmp (assembly->aname.name, "mscorlib") && !mono_defaults.corlib)
/* Can't AOT this during startup */
return NULL;
/* Only AOT one assembly per run to avoid slowing down execution too much */
if (cache_count > 0)
return NULL;
cache_count ++;
mono_trace (G_LOG_LEVEL_MESSAGE, MONO_TRACE_AOT, "AOT: compiling assembly '%s'... ", assembly->image->name);
/*
* We need to invoke the AOT compiler here. There are multiple approaches:
* - spawn a new runtime process. This can be hard when running with mkbundle, and
* its hard to make the new process load the same set of assemblies.
* - doing it in-process. This exposes the current process to bugs/leaks/side effects of
* the AOT compiler.
* - fork a new process and do the work there. This is the current approach.
*/
if (in_process) {
FILE *logfile;
char *logfile_name;
logfile_name = g_strdup_printf ("%s/aot.log", cache_dir);
logfile = fopen (logfile_name, "a+");
aot_options = g_strdup_printf ("outfile=%s", fname);
res = mono_compile_assembly (assembly, mono_parse_default_optimizations (NULL), aot_options);
// FIXME: Cache failures
} else {
pid = fork ();
if (pid == 0) {
FILE *logfile;
char *logfile_name;
if (spawn_compiler) {
/* FIXME: security */
/* FIXME: Has to pass the assembly loading path to the child process */
cmd = g_strdup_printf ("mono -O=all --aot=%s %s", aot_options, assembly->image->name);
/* Child */
res = g_spawn_command_line_sync (cmd, &out, &err, &exit_status, NULL);
logfile_name = g_strdup_printf ("%s/aot.log", cache_dir);
logfile = fopen (logfile_name, "a+");
g_free (logfile_name);
#if !defined(HOST_WIN32) && !defined(__ppc__) && !defined(__ppc64__) && !defined(__powerpc__)
if (res) {
if (!WIFEXITED (exit_status) && (WEXITSTATUS (exit_status) == 0))
mono_trace (G_LOG_LEVEL_MESSAGE, MONO_TRACE_AOT, "AOT failed: %s.", err);
else
mono_trace (G_LOG_LEVEL_MESSAGE, MONO_TRACE_AOT, "AOT succeeded.");
g_free (out);
g_free (err);
}
#endif
g_free (cmd);
} else {
dup2 (fileno (logfile), 1);
dup2 (fileno (logfile), 2);
aot_options = g_strdup_printf ("outfile=%s", fname);
res = mono_compile_assembly (assembly, mono_parse_default_optimizations (NULL), aot_options);
if (!res) {
mono_trace (G_LOG_LEVEL_MESSAGE, MONO_TRACE_AOT, "AOT failed.");
exit (1);
} else {
mono_trace (G_LOG_LEVEL_MESSAGE, MONO_TRACE_AOT, "AOT succeeded.");
exit (0);
}
} else {
/* Parent */
waitpid (pid, &exit_status, 0);
if (!WIFEXITED (exit_status) && (WEXITSTATUS (exit_status) == 0))
mono_trace (G_LOG_LEVEL_MESSAGE, MONO_TRACE_AOT, "AOT: failed.");
else
mono_trace (G_LOG_LEVEL_MESSAGE, MONO_TRACE_AOT, "AOT: succeeded.");
}
module = mono_dl_open (fname, MONO_DL_LAZY, NULL);
g_free (aot_options);
}
module = mono_dl_open (fname, MONO_DL_LAZY, NULL);
return module;
}
#else
static void
aot_cache_init (void)
{
}
static MonoDl*
aot_cache_load_module (MonoAssembly *assembly, char **aot_name)
{
return NULL;
}
#endif
static void
find_symbol (MonoDl *module, gpointer *globals, const char *name, gpointer *value)
{
@@ -1589,6 +1705,10 @@ load_aot_module (MonoAssembly *assembly, gpointer user_data)
if (mono_security_cas_enabled ())
return;
if (enable_aot_cache && !strcmp (assembly->aname.name, "mscorlib") && !mono_defaults.corlib)
/* Loaded later from mono_aot_get_method () */
return;
mono_aot_lock ();
if (static_aot_modules)
info = g_hash_table_lookup (static_aot_modules, assembly->aname.name);
@@ -1603,9 +1723,9 @@ load_aot_module (MonoAssembly *assembly, gpointer user_data)
mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "Found statically linked AOT module '%s'.\n", aot_name);
globals = info->globals;
} else {
if (use_aot_cache)
sofile = load_aot_module_from_cache (assembly, &aot_name);
else {
if (enable_aot_cache) {
sofile = aot_cache_load_module (assembly, &aot_name);
} else {
char *err;
aot_name = g_strdup_printf ("%s%s", assembly->image->name, SHARED_EXT);
@@ -1652,7 +1772,7 @@ load_aot_module (MonoAssembly *assembly, gpointer user_data)
fprintf (stderr, "Failed to load AOT module '%s' while running in aot-only mode: %s.\n", aot_name, msg);
exit (1);
} else {
mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT module %s is unusable: %s.\n", aot_name, msg);
mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT: module %s is unusable: %s.\n", aot_name, msg);
}
g_free (msg);
g_free (aot_name);
@@ -1873,14 +1993,14 @@ load_aot_module (MonoAssembly *assembly, gpointer user_data)
}
if (amodule->out_of_date) {
mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT Module %s is unusable because a dependency is out-of-date.\n", assembly->image->name);
mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT: Module %s is unusable because a dependency is out-of-date.\n", assembly->image->name);
if (mono_aot_only) {
fprintf (stderr, "Failed to load AOT module '%s' while running in aot-only mode because a dependency cannot be found or it is out of date.\n", aot_name);
exit (1);
}
}
else
mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT loaded AOT Module for %s.\n", assembly->image->name);
mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT: loaded AOT Module for %s.\n", assembly->image->name);
}
/*
@@ -1943,8 +2063,9 @@ mono_aot_init (void)
if (g_getenv ("MONO_LASTAOT"))
mono_last_aot_method = atoi (g_getenv ("MONO_LASTAOT"));
if (g_getenv ("MONO_AOT_CACHE"))
use_aot_cache = TRUE;
#ifdef ENABLE_AOT_CACHE
aot_cache_init ();
#endif
}
void
@@ -3324,7 +3445,7 @@ load_method (MonoDomain *domain, MonoAotModule *amodule, MonoImage *image, MonoM
if (!method)
method = mono_get_method (image, token, NULL);
full_name = mono_method_full_name (method, TRUE);
mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_AOT, "AOT NOT FOUND: %s.", full_name);
mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_AOT, "AOT: NOT FOUND: %s.", full_name);
g_free (full_name);
}
return NULL;
@@ -3443,7 +3564,7 @@ load_method (MonoDomain *domain, MonoAotModule *amodule, MonoImage *image, MonoM
if (!jinfo)
jinfo = mono_aot_find_jit_info (domain, amodule->assembly->image, code);
mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_AOT, "AOT FOUND method %s [%p - %p %p]", full_name, code, code + jinfo->code_size, info);
mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_AOT, "AOT: FOUND method %s [%p - %p %p]", full_name, code, code + jinfo->code_size, info);
g_free (full_name);
}
@@ -3639,6 +3760,12 @@ mono_aot_get_method (MonoDomain *domain, MonoMethod *method)
MonoAotModule *amodule = klass->image->aot_module;
guint8 *code;
if (enable_aot_cache && !amodule && klass->image == mono_defaults.corlib) {
/* This cannot be AOTed during startup, so do it now */
load_aot_module (klass->image->assembly, NULL);
amodule = klass->image->aot_module;
}
if (!amodule)
return NULL;
@@ -4149,7 +4276,7 @@ load_function_full (MonoAotModule *amodule, const char *name, MonoTrampInfo **ou
if (!code)
g_error ("Symbol '%s' not found in AOT file '%s'.\n", name, amodule->aot_name);
mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_AOT, "AOT FOUND function '%s' in AOT file '%s'.", name, amodule->aot_name);
mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_AOT, "AOT: FOUND function '%s' in AOT file '%s'.", name, amodule->aot_name);
/* Load info */
@@ -4770,7 +4897,7 @@ mono_aot_set_make_unreadable (gboolean unreadable)
make_unreadable = unreadable;
if (make_unreadable && !inited) {
mono_counters_register ("AOT pagefaults", MONO_COUNTER_JIT | MONO_COUNTER_INT, &n_pagefaults);
mono_counters_register ("AOT: pagefaults", MONO_COUNTER_JIT | MONO_COUNTER_INT, &n_pagefaults);
}
}
+3 -1
View File
@@ -112,7 +112,9 @@ monoutils_sources = \
bsearch.c \
mono-signal-handler.h \
mono-conc-hashtable.h \
mono-conc-hashtable.c
mono-conc-hashtable.c \
sha1.h \
sha1.c
arch_sources =
+176
View File
@@ -0,0 +1,176 @@
/* $OpenBSD: sha1.c,v 1.22 2012/08/27 21:05:43 miod Exp $ */
/*
* SHA-1 in C
* By Steve Reid <[email protected]>
* 100% Public Domain
*
* Test Vectors (from FIPS PUB 180-1)
* "abc"
* A9993E36 4706816A BA3E2571 7850C26C 9CD0D89D
* "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"
* 84983E44 1C3BD26E BAAE4AA1 F95129E5 E54670F1
* A million repetitions of "a"
* 34AA973C D4C4DAA4 F61EEB2B DBAD2731 6534016F
*/
#include <sys/param.h>
#include <string.h>
#include <stdint.h>
#include <sha1.h>
#define rol(value, bits) (((value) << (bits)) | ((value) >> (32 - (bits))))
/*
* blk0() and blk() perform the initial expand.
* I got the idea of expanding during the round function from SSLeay
*/
#if BYTE_ORDER == LITTLE_ENDIAN
# define blk0(i) (block->l[i] = (rol(block->l[i],24)&0xFF00FF00) \
|(rol(block->l[i],8)&0x00FF00FF))
#else
# define blk0(i) block->l[i]
#endif
#define blk(i) (block->l[i&15] = rol(block->l[(i+13)&15]^block->l[(i+8)&15] \
^block->l[(i+2)&15]^block->l[i&15],1))
/*
* (R0+R1), R2, R3, R4 are the different operations (rounds) used in SHA1
*/
#define R0(v,w,x,y,z,i) z+=((w&(x^y))^y)+blk0(i)+0x5A827999+rol(v,5);w=rol(w,30);
#define R1(v,w,x,y,z,i) z+=((w&(x^y))^y)+blk(i)+0x5A827999+rol(v,5);w=rol(w,30);
#define R2(v,w,x,y,z,i) z+=(w^x^y)+blk(i)+0x6ED9EBA1+rol(v,5);w=rol(w,30);
#define R3(v,w,x,y,z,i) z+=(((w|x)&y)|(w&x))+blk(i)+0x8F1BBCDC+rol(v,5);w=rol(w,30);
#define R4(v,w,x,y,z,i) z+=(w^x^y)+blk(i)+0xCA62C1D6+rol(v,5);w=rol(w,30);
typedef union {
u_int8_t c[64];
u_int32_t l[16];
} CHAR64LONG16;
/*
* Hash a single 512-bit block. This is the core of the algorithm.
*/
void
SHA1Transform(u_int32_t state[5], const u_int8_t buffer[SHA1_BLOCK_LENGTH])
{
u_int32_t a, b, c, d, e;
u_int8_t workspace[SHA1_BLOCK_LENGTH];
CHAR64LONG16 *block = (CHAR64LONG16 *)workspace;
(void)memcpy(block, buffer, SHA1_BLOCK_LENGTH);
/* Copy context->state[] to working vars */
a = state[0];
b = state[1];
c = state[2];
d = state[3];
e = state[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 context.state[] */
state[0] += a;
state[1] += b;
state[2] += c;
state[3] += d;
state[4] += e;
/* Wipe variables */
a = b = c = d = e = 0;
}
/*
* SHA1Init - Initialize new context
*/
void
SHA1Init(SHA1_CTX *context)
{
/* SHA1 initialization constants */
context->count = 0;
context->state[0] = 0x67452301;
context->state[1] = 0xEFCDAB89;
context->state[2] = 0x98BADCFE;
context->state[3] = 0x10325476;
context->state[4] = 0xC3D2E1F0;
}
/*
* Run your data through this.
*/
void
SHA1Update(SHA1_CTX *context, const u_int8_t *data, size_t len)
{
size_t i, j;
j = (size_t)((context->count >> 3) & 63);
context->count += (len << 3);
if ((j + len) > 63) {
(void)memcpy(&context->buffer[j], data, (i = 64-j));
SHA1Transform(context->state, context->buffer);
for ( ; i + 63 < len; i += 64)
SHA1Transform(context->state, (u_int8_t *)&data[i]);
j = 0;
} else {
i = 0;
}
(void)memcpy(&context->buffer[j], &data[i], len - i);
}
/*
* Add padding and return the message digest.
*/
void
SHA1Pad(SHA1_CTX *context)
{
u_int8_t finalcount[8];
u_int i;
for (i = 0; i < 8; i++) {
finalcount[i] = (u_int8_t)((context->count >>
((7 - (i & 7)) * 8)) & 255); /* Endian independent */
}
SHA1Update(context, (u_int8_t *)"\200", 1);
while ((context->count & 504) != 448)
SHA1Update(context, (u_int8_t *)"\0", 1);
SHA1Update(context, finalcount, 8); /* Should cause a SHA1Transform() */
}
void
SHA1Final(u_int8_t digest[SHA1_DIGEST_LENGTH], SHA1_CTX *context)
{
u_int i;
SHA1Pad(context);
if (digest) {
for (i = 0; i < SHA1_DIGEST_LENGTH; i++) {
digest[i] = (u_int8_t)
((context->state[i>>2] >> ((3-(i & 3)) * 8) ) & 255);
}
memset(context, 0, sizeof(*context));
}
}
+48
View File
@@ -0,0 +1,48 @@
/* $OpenBSD: sha1.h,v 1.24 2012/12/05 23:19:57 deraadt Exp $ */
/*
* SHA-1 in C
* By Steve Reid <[email protected]>
* 100% Public Domain
*/
#ifndef _SHA1_H
#define _SHA1_H
#define SHA1_BLOCK_LENGTH 64
#define SHA1_DIGEST_LENGTH 20
#define SHA1_DIGEST_STRING_LENGTH (SHA1_DIGEST_LENGTH * 2 + 1)
typedef struct {
u_int32_t state[5];
u_int64_t count;
u_int8_t buffer[SHA1_BLOCK_LENGTH];
} SHA1_CTX;
__BEGIN_DECLS
void SHA1Init(SHA1_CTX *);
void SHA1Pad(SHA1_CTX *);
void SHA1Transform(u_int32_t [5], const u_int8_t [SHA1_BLOCK_LENGTH]);
void SHA1Update(SHA1_CTX *, const u_int8_t *, size_t);
void SHA1Final(u_int8_t [SHA1_DIGEST_LENGTH], SHA1_CTX *);
char *SHA1End(SHA1_CTX *, char *);
char *SHA1File(const char *, char *);
char *SHA1FileChunk(const char *, char *, off_t, off_t);
char *SHA1Data(const u_int8_t *, size_t, char *);
__END_DECLS
#define HTONDIGEST(x) do { \
x[0] = htonl(x[0]); \
x[1] = htonl(x[1]); \
x[2] = htonl(x[2]); \
x[3] = htonl(x[3]); \
x[4] = htonl(x[4]); } while (0)
#define NTOHDIGEST(x) do { \
x[0] = ntohl(x[0]); \
x[1] = ntohl(x[1]); \
x[2] = ntohl(x[2]); \
x[3] = ntohl(x[3]); \
x[4] = ntohl(x[4]); } while (0)
#endif /* _SHA1_H */