| 1 |
/* Copyright 2014 Adobe Systems Incorporated (http://www.adobe.com/). All Rights Reserved. |
| 2 |
This software is licensed as OpenSource, under the Apache License, Version 2.0. |
| 3 |
This license is available at: http://opensource.org/licenses/Apache-2.0. */ |
| 4 |
|
| 5 |
#ifndef SHA1_H |
| 6 |
#define SHA1_H |
| 7 |
#include <stdlib.h> /* to get size_t */ |
| 8 |
|
| 9 |
/********************************************** |
| 10 |
|
| 11 |
Expected use... |
| 12 |
|
| 13 |
client calls sha1_init. sha1_init will use the memory callback |
| 14 |
to request memory for sha1_ctx. sha1_ctx will be initialized |
| 15 |
and a pointer to it will be returned to signal success. |
| 16 |
|
| 17 |
client calls sha1_update (with sha1_ctx pointer returned by sha1_init) |
| 18 |
one or more times with data to be included in the hash. |
| 19 |
Clients are expected to do reasonable buffering of input |
| 20 |
data. Calls may be made with 1 byte, but better performance |
| 21 |
will result with larger buffers (say 64 or more bytes). |
| 22 |
|
| 23 |
client calls sha1_finalize when all of the data to be hashed |
| 24 |
has been accumulated. sha1_finalize returns the hash |
| 25 |
via the client supplied sha1_hash and calls the client |
| 26 |
supplied sha1_free to return the work context. |
| 27 |
|
| 28 |
Note: |
| 29 |
client is responsible for the memory callbacks. In particular, |
| 30 |
the sha1_malloc and sha1_free procedures are expected to honor |
| 31 |
all of the ANSI C rules with respect to buffer alignment. |
| 32 |
|
| 33 |
The implementation is completely thread safe and reentrant. |
| 34 |
Separate client threads may safely interleave calls to the |
| 35 |
relevant sha1 procedures. A client, from one or more threads |
| 36 |
may have more than one hash accumulation in progress concurrently. |
| 37 |
(Of course, each hash accumulation must have its own context |
| 38 |
returned by sha1_init etc.) |
| 39 |
|
| 40 |
Data to be hashed and the resulting hash are treated as unsigned |
| 41 |
char data. This code will produce identical results on |
| 42 |
any system, regardless of byte order or word size. The client |
| 43 |
must understand and accommodate byte order issues if multi-byte |
| 44 |
binary data is hashed and compared on different platforms. |
| 45 |
|
| 46 |
*******************************************************/ |
| 47 |
|
| 48 |
typedef struct sha1_ctx *sha1_pctx; /* pointer to sha1 private work area */ |
| 49 |
/* the contents of this work area are private |
| 50 |
to the implementation of sha1 and must |
| 51 |
be preserved without modification between |
| 52 |
calls to sha1_update and sha1_finalize. |
| 53 |
The size of this work area is about 100 |
| 54 |
bytes. */ |
| 55 |
|
| 56 |
typedef void *sha1_malloc(size_t size, void *hook); |
| 57 |
/* client supplied callback procedure */ |
| 58 |
/* this client supplied malloc procedure |
| 59 |
has the same requirements as the standard C |
| 60 |
library malloc. The sha1 library depends on |
| 61 |
the client malloc procedure to honor alignment |
| 62 |
requirements. hook is a client context |
| 63 |
pointer passed through to the client |
| 64 |
callback procedure unmodified by |
| 65 |
sha1_init. */ |
| 66 |
typedef void sha1_free(sha1_pctx ctx, void *hook); |
| 67 |
/* client supplied callback procedure */ |
| 68 |
/* this client supplied free procedure is used |
| 69 |
to return the memory allocated during sha1_init |
| 70 |
via its callback to the client procedure. |
| 71 |
hook is a client context pointer passed |
| 72 |
through to the client callback procedure |
| 73 |
unmodified by sha1_free. */ |
| 74 |
|
| 75 |
typedef unsigned char sha1_hash[20]; |
| 76 |
/* hash is returned into this client provided |
| 77 |
space */ |
| 78 |
|
| 79 |
#ifdef __cplusplus |
| 80 |
extern "C" { |
| 81 |
#endif |
| 82 |
|
| 83 |
sha1_pctx sha1_init(sha1_malloc client_malloc, void *hook); |
| 84 |
/* called once per hash generation */ |
| 85 |
/* returned context must be passed with |
| 86 |
unmodified contents to subsequent calls */ |
| 87 |
/* sha1_malloc is client provided malloc |
| 88 |
procedure */ |
| 89 |
|
| 90 |
/* return valid pointer for success, |
| 91 |
NULL for failure. |
| 92 |
failure will occur |
| 93 |
if client_malloc callback is NULL or |
| 94 |
if client_malloc callback returns NULL or |
| 95 |
if sizeof(Card32) != 4 (the implementation |
| 96 |
requires that a 4 byte unsigned integer |
| 97 |
type exists) |
| 98 |
*/ |
| 99 |
|
| 100 |
int sha1_update(sha1_pctx ctx, unsigned char *buffer, size_t len); |
| 101 |
/* called 1 or more times to accumulate hash |
| 102 |
value. */ |
| 103 |
|
| 104 |
/* return 0 for success, 1 for failure. |
| 105 |
failure will occur if ctx is NULL or |
| 106 |
buffer is NULL and len > 0. */ |
| 107 |
|
| 108 |
int sha1_finalize(sha1_pctx ctx, sha1_free client_free, |
| 109 |
sha1_hash hash, void *hook); |
| 110 |
/* called once after all data has been accumulated |
| 111 |
via calls to sha1 update. Note that hash value |
| 112 |
is returned to the client in hash and that the |
| 113 |
context (ctx) is freed via a call to |
| 114 |
client_free */ |
| 115 |
|
| 116 |
/* return 0 for success, 1 for failure. |
| 117 |
failure will occur if callback is NULL or |
| 118 |
hash is NULL, or ctx is NULL */ |
| 119 |
|
| 120 |
#ifdef __cplusplus |
| 121 |
} |
| 122 |
#endif |
| 123 |
|
| 124 |
/* Testing: |
| 125 |
|
| 126 |
This code has been tested on sparc sunos gcc |
| 127 |
sparc solaris (sun cc product compiler) |
| 128 |
Pentium G6/200 linux gcc |
| 129 |
win32/ppro/vc6 |
| 130 |
|
| 131 |
Test cases include all the samples in FIPS 180-1. |
| 132 |
|
| 133 |
*/ |
| 134 |
|
| 135 |
/* Performance: |
| 136 |
|
| 137 |
On Pentium G6/200 linux gcc with 512 byte buffers passed to sha1_update |
| 138 |
hash rate is about 4,000,000 bytes/sec. |
| 139 |
|
| 140 |
*/ |
| 141 |
#endif /* SHA1_H */ |