]>
Commit | Line | Data |
---|---|---|
a6ef3518 PM |
1 | /* |
2 | * SHA-1 implementation. | |
3 | * | |
4 | * Copyright (C) 2005 Paul Mackerras <paulus@samba.org> | |
5 | * | |
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" | |
12 | ||
55454427 | 13 | void ppc_sha1_core(uint32_t *hash, const unsigned char *p, |
9126f009 | 14 | unsigned int nblocks); |
a6ef3518 | 15 | |
9126f009 | 16 | int ppc_SHA1_Init(ppc_SHA_CTX *c) |
a6ef3518 PM |
17 | { |
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; | |
26 | } | |
27 | ||
9126f009 | 28 | int ppc_SHA1_Update(ppc_SHA_CTX *c, const void *ptr, unsigned long n) |
a6ef3518 PM |
29 | { |
30 | unsigned long nb; | |
31 | const unsigned char *p = ptr; | |
32 | ||
b47f509b | 33 | c->len += (uint64_t) n << 3; |
a6ef3518 PM |
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) { | |
9126f009 | 41 | ppc_sha1_core(c->hash, c->buf.b, 1); |
a6ef3518 PM |
42 | c->cnt = 0; |
43 | } | |
44 | } else { | |
45 | nb = n >> 6; | |
9126f009 | 46 | ppc_sha1_core(c->hash, p, nb); |
a6ef3518 PM |
47 | nb <<= 6; |
48 | } | |
49 | n -= nb; | |
50 | p += nb; | |
51 | } | |
52 | return 0; | |
a6080a0a | 53 | } |
a6ef3518 | 54 | |
9126f009 | 55 | int ppc_SHA1_Final(unsigned char *hash, ppc_SHA_CTX *c) |
a6ef3518 PM |
56 | { |
57 | unsigned int cnt = c->cnt; | |
58 | ||
59 | c->buf.b[cnt++] = 0x80; | |
60 | if (cnt > 56) { | |
61 | if (cnt < 64) | |
62 | memset(&c->buf.b[cnt], 0, 64 - cnt); | |
9126f009 | 63 | ppc_sha1_core(c->hash, c->buf.b, 1); |
a6ef3518 PM |
64 | cnt = 0; |
65 | } | |
66 | if (cnt < 56) | |
67 | memset(&c->buf.b[cnt], 0, 56 - cnt); | |
68 | c->buf.l[7] = c->len; | |
9126f009 | 69 | ppc_sha1_core(c->hash, c->buf.b, 1); |
a6ef3518 PM |
70 | memcpy(hash, c->hash, 20); |
71 | return 0; | |
72 | } |