Merge commit 'junio/next' into next
[git/platforms/storm.git] / ppc / sha1.c
blob738e36c1e81def4822ccc2a66bc2761402a07f26
1 /*
2 * SHA-1 implementation.
4 * Copyright (C) 2005 Paul Mackerras <paulus@samba.org>
6 * This version assumes we are running on a big-endian machine.
7 * It calls an external sha1_core() to process blocks of 64 bytes.
8 */
9 #include <stdio.h>
10 #include <string.h>
11 #include "sha1.h"
13 extern void sha1_core(uint32_t *hash, const unsigned char *p,
14 unsigned int nblocks);
16 int SHA1_Init(SHA_CTX *c)
18 c->hash[0] = 0x67452301;
19 c->hash[1] = 0xEFCDAB89;
20 c->hash[2] = 0x98BADCFE;
21 c->hash[3] = 0x10325476;
22 c->hash[4] = 0xC3D2E1F0;
23 c->len = 0;
24 c->cnt = 0;
25 return 0;
28 int SHA1_Update(SHA_CTX *c, const void *ptr, unsigned long n)
30 unsigned long nb;
31 const unsigned char *p = ptr;
33 c->len += (uint64_t) n << 3;
34 while (n != 0) {
35 if (c->cnt || n < 64) {
36 nb = 64 - c->cnt;
37 if (nb > n)
38 nb = n;
39 memcpy(&c->buf.b[c->cnt], p, nb);
40 if ((c->cnt += nb) == 64) {
41 sha1_core(c->hash, c->buf.b, 1);
42 c->cnt = 0;
44 } else {
45 nb = n >> 6;
46 sha1_core(c->hash, p, nb);
47 nb <<= 6;
49 n -= nb;
50 p += nb;
52 return 0;
55 int SHA1_Final(unsigned char *hash, SHA_CTX *c)
57 unsigned int cnt = c->cnt;
59 c->buf.b[cnt++] = 0x80;
60 if (cnt > 56) {
61 if (cnt < 64)
62 memset(&c->buf.b[cnt], 0, 64 - cnt);
63 sha1_core(c->hash, c->buf.b, 1);
64 cnt = 0;
66 if (cnt < 56)
67 memset(&c->buf.b[cnt], 0, 56 - cnt);
68 c->buf.l[7] = c->len;
69 sha1_core(c->hash, c->buf.b, 1);
70 memcpy(hash, c->hash, 20);
71 return 0;