#include <libc.h>
#include <mp.h>
#include <libsec.h>
.Ti DigestState* md4(uchar *data, ulong dlen, uchar *digest, DigestState *state)
.Ti DigestState* md5(uchar *data, ulong dlen, uchar *digest, DigestState *state)
char* md5pickle(MD5state *state)
MD5state* md5unpickle(char *p);
.Ti DigestState* sha1(uchar *data, ulong dlen, uchar *digest, DigestState *state)
char* sha1pickle(MD5state *state)
MD5state* sha1unpickle(char *p);
.Ti DigestState* aes(uchar *data, ulong dlen, uchar *digest, DigestState *state)
.Ti DigestState* hmac_x(uchar *p, ulong len, uchar *key, ulong klen, uchar *digest, DigestState *s, DigestState*(*x)(uchar*, ulong, uchar*, DigestState*), int xlen)
.Ti DigestState* hmac_md5(uchar *data, ulong dlen, uchar *key, ulong klen, uchar *digest, DigestState *state)
.Ti DigestState* hmac_sha1(uchar *data, ulong dlen, uchar *key, ulong klen, uchar *digest, DigestState *state)
.Ti DigestState* hmac_aes(uchar *data, ulong dlen, uchar *key, ulong klen, uchar *digest, DigestState *state)
The routines md4 , md5 , sha1 , aes , hmac_md5 , hmac_sha1 , and hmac_aes differ only in the length of the resulting digest and in the security of the hash. Usage for each is the same. The first call to the routine should have nil as the state parameter. This call returns a state which can be used to chain subsequent calls. The last call should have digest .RL non- nil . Digest must point to a buffer of at least the size of the digest produced. This last call will free the state and copy the result into digest .
The constants MD4dlen , MD5dlen , and SHA1dlen define the lengths of the digests.
Hmac_md5 , hmac_sha1 . and hmac_aes are used slightly differently. These hash algorithms are keyed and require a key to be specified on every call. The digest lengths for these hashes are MD5dlen , SHA1dlen , and AESdlen respectively. These routines all call hmac_x internally, but hmac_x is not intended for general use.
The functions md5pickle and sha1pickle marshal the state of a digest for transmission. Md5unpickle and sha1unpickle unmarshal a pickled digest. All four routines return a pointer to a newly malloc (2)'d object.
To chain a number of buffers together, bounded on each end by some secret:
.EX char buf[256]; uchar digest[MD5dlen]; DigestState *s; s = md5("my password", 11, nil, nil); while((n = read(fd, buf, 256)) > 0) md5(buf, n, nil, s); md5("drowssap ym", 11, digest, s);