SHA-1 in C
By Steve Reid <sreid@sea-to-sky.net>
100% Public Domain
+[I interpet this as a blanket permision -iwj.]
Note: parts of this file have been removed or modified to work in secnet.
Instead of using this file in new projects, I suggest you use the
Still 100% PD
Modified to run on Compaq Alpha hardware.
-
+-----------------
+(Further modifications as part of secnet. See git history for full details.
+ - Ian Jackson et al)
*/
/*
/* #define SHA1HANDSOFF */
#include "secnet.h"
+#include "util.h"
#include <stdio.h>
#include <string.h>
/*************************************************************/
/* Everything below here is the interface to secnet */
-static void *sha1_init(void)
+static void sha1_init(void *sst)
{
- SHA1_CTX *ctx;
+ SHA1_CTX *ctx=sst;
- ctx=safe_malloc(sizeof(*ctx),"sha1_init");
SHA1Init(ctx);
-
- return ctx;
}
static void sha1_update(void *sst, const void *buf, int32_t len)
SHA1_CTX *ctx=sst;
SHA1Final(digest,ctx);
- free(ctx);
}
struct sha1 {
struct hash_if ops;
};
+static struct sha1 st[1];
+struct hash_if *const sha1_hash_if = &st->ops;
+
void sha1_module(dict_t *dict)
{
- struct sha1 *st;
- void *ctx;
cstring_t testinput="abcdbcdecdefdefgefghfghigh"
"ijhijkijkljklmklmnlmnomnopnopq";
uint8_t expected[20]=
uint8_t digest[20];
int i;
- st=safe_malloc(sizeof(*st),"sha1_module");
st->cl.description="sha1";
st->cl.type=CL_HASH;
st->cl.apply=NULL;
st->cl.interface=&st->ops;
- st->ops.len=20;
+ st->ops.hlen=20;
+ st->ops.slen=sizeof(SHA1_CTX);
st->ops.init=sha1_init;
st->ops.update=sha1_update;
st->ops.final=sha1_final;
dict_add(dict,"sha1",new_closure(&st->cl));
- ctx=sha1_init();
- sha1_update(ctx,testinput,strlen(testinput));
- sha1_final(ctx,digest);
+ hash_hash(&st->ops,testinput,strlen(testinput),digest);
for (i=0; i<20; i++) {
if (digest[i]!=expected[i]) {
fatal("sha1 module failed self-test");