ppc / sha1.con commit Merge branch 'jt/t5551-test-chunked' (d5df41c)
   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
  13void ppc_sha1_core(uint32_t *hash, const unsigned char *p,
  14                   unsigned int nblocks);
  15
  16int ppc_SHA1_Init(ppc_SHA_CTX *c)
  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
  28int ppc_SHA1_Update(ppc_SHA_CTX *c, const void *ptr, unsigned long n)
  29{
  30        unsigned long nb;
  31        const unsigned char *p = ptr;
  32
  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                                ppc_sha1_core(c->hash, c->buf.b, 1);
  42                                c->cnt = 0;
  43                        }
  44                } else {
  45                        nb = n >> 6;
  46                        ppc_sha1_core(c->hash, p, nb);
  47                        nb <<= 6;
  48                }
  49                n -= nb;
  50                p += nb;
  51        }
  52        return 0;
  53}
  54
  55int ppc_SHA1_Final(unsigned char *hash, ppc_SHA_CTX *c)
  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);
  63                ppc_sha1_core(c->hash, c->buf.b, 1);
  64                cnt = 0;
  65        }
  66        if (cnt < 56)
  67                memset(&c->buf.b[cnt], 0, 56 - cnt);
  68        c->buf.l[7] = c->len;
  69        ppc_sha1_core(c->hash, c->buf.b, 1);
  70        memcpy(hash, c->hash, 20);
  71        return 0;
  72}