Paul Mackerras | a6ef351 | 2005-04-22 23:08:43 -0700 | [diff] [blame] | 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 | |
Nicolas Pitre | 9126f00 | 2008-10-01 14:05:20 -0400 | [diff] [blame] | 13 | extern void ppc_sha1_core(uint32_t *hash, const unsigned char *p, |
| 14 | unsigned int nblocks); |
Paul Mackerras | a6ef351 | 2005-04-22 23:08:43 -0700 | [diff] [blame] | 15 | |
Nicolas Pitre | 9126f00 | 2008-10-01 14:05:20 -0400 | [diff] [blame] | 16 | int ppc_SHA1_Init(ppc_SHA_CTX *c) |
Paul Mackerras | a6ef351 | 2005-04-22 23:08:43 -0700 | [diff] [blame] | 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 | |
Nicolas Pitre | 9126f00 | 2008-10-01 14:05:20 -0400 | [diff] [blame] | 28 | int ppc_SHA1_Update(ppc_SHA_CTX *c, const void *ptr, unsigned long n) |
Paul Mackerras | a6ef351 | 2005-04-22 23:08:43 -0700 | [diff] [blame] | 29 | { |
| 30 | unsigned long nb; |
| 31 | const unsigned char *p = ptr; |
| 32 | |
Paul Mackerras | b47f509 | 2006-06-19 09:25:16 +1000 | [diff] [blame] | 33 | c->len += (uint64_t) n << 3; |
Paul Mackerras | a6ef351 | 2005-04-22 23:08:43 -0700 | [diff] [blame] | 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) { |
Nicolas Pitre | 9126f00 | 2008-10-01 14:05:20 -0400 | [diff] [blame] | 41 | ppc_sha1_core(c->hash, c->buf.b, 1); |
Paul Mackerras | a6ef351 | 2005-04-22 23:08:43 -0700 | [diff] [blame] | 42 | c->cnt = 0; |
| 43 | } |
| 44 | } else { |
| 45 | nb = n >> 6; |
Nicolas Pitre | 9126f00 | 2008-10-01 14:05:20 -0400 | [diff] [blame] | 46 | ppc_sha1_core(c->hash, p, nb); |
Paul Mackerras | a6ef351 | 2005-04-22 23:08:43 -0700 | [diff] [blame] | 47 | nb <<= 6; |
| 48 | } |
| 49 | n -= nb; |
| 50 | p += nb; |
| 51 | } |
| 52 | return 0; |
Junio C Hamano | a6080a0 | 2007-06-07 00:04:01 -0700 | [diff] [blame] | 53 | } |
Paul Mackerras | a6ef351 | 2005-04-22 23:08:43 -0700 | [diff] [blame] | 54 | |
Nicolas Pitre | 9126f00 | 2008-10-01 14:05:20 -0400 | [diff] [blame] | 55 | int ppc_SHA1_Final(unsigned char *hash, ppc_SHA_CTX *c) |
Paul Mackerras | a6ef351 | 2005-04-22 23:08:43 -0700 | [diff] [blame] | 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); |
Nicolas Pitre | 9126f00 | 2008-10-01 14:05:20 -0400 | [diff] [blame] | 63 | ppc_sha1_core(c->hash, c->buf.b, 1); |
Paul Mackerras | a6ef351 | 2005-04-22 23:08:43 -0700 | [diff] [blame] | 64 | cnt = 0; |
| 65 | } |
| 66 | if (cnt < 56) |
| 67 | memset(&c->buf.b[cnt], 0, 56 - cnt); |
| 68 | c->buf.l[7] = c->len; |
Nicolas Pitre | 9126f00 | 2008-10-01 14:05:20 -0400 | [diff] [blame] | 69 | ppc_sha1_core(c->hash, c->buf.b, 1); |
Paul Mackerras | a6ef351 | 2005-04-22 23:08:43 -0700 | [diff] [blame] | 70 | memcpy(hash, c->hash, 20); |
| 71 | return 0; |
| 72 | } |