ext/digest/sha2/sha2hl.c


DEFINITIONS

This source file includes following functions.
  1. SHA256_File
  2. SHA256_End
  3. SHA256_Data
  4. SHA384_File
  5. SHA384_End
  6. SHA384_Data
  7. SHA512_File
  8. SHA512_End
  9. SHA512_Data


   1  /* $NetBSD: sha2hl.c,v 1.1 2001/03/12 09:08:40 agc Exp $         */
   2  /* $RoughId: sha2hl.c,v 1.2 2001/07/13 19:49:10 knu Exp $ */
   3  /* $Id: sha2hl.c,v 1.1 2001/07/13 20:06:14 knu Exp $ */
   4  
   5  /*
   6   * sha2hl.c
   7   * This code includes some functions taken from sha2.c, hence the
   8   * following licence reproduction.
   9   *
  10   * This code is not a verbatim copy, since some routines have been added,
  11   * and some bugs have been fixed.
  12   *
  13   * Version 1.0.0beta1
  14   *
  15   * Written by Aaron D. Gifford <me@aarongifford.com>
  16   *
  17   * Copyright 2000 Aaron D. Gifford.  All rights reserved.
  18   *
  19   * Redistribution and use in source and binary forms, with or without
  20   * modification, are permitted provided that the following conditions
  21   * are met:
  22   * 1. Redistributions of source code must retain the above copyright
  23   *    notice, this list of conditions and the following disclaimer.
  24   * 2. Redistributions in binary form must reproduce the above copyright
  25   *    notice, this list of conditions and the following disclaimer in the
  26   *    documentation and/or other materials provided with the distribution.
  27   * 3. Neither the name of the copyright holder nor the names of contributors
  28   *    may be used to endorse or promote products derived from this software
  29   *    without specific prior written permission.
  30   * 
  31   * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) AND CONTRIBUTOR(S) ``AS IS'' AND
  32   * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  33   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  34   * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR(S) OR CONTRIBUTOR(S) BE LIABLE
  35   * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  36   * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  37   * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  38   * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  39   * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  40   * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  41   * SUCH DAMAGE.
  42   *
  43   */
  44  
  45  #include "sha2.h"
  46  
  47  #ifndef lint
  48  /* __RCSID("$NetBSD: sha2hl.c,v 1.1 2001/03/12 09:08:40 agc Exp $"); */
  49  #endif                          /* not lint */
  50  
  51  /* #include "namespace.h" */
  52  
  53  #include <assert.h>
  54  #include <errno.h>
  55  #include <fcntl.h>
  56  #include <stdio.h>
  57  #include <string.h>
  58  #include <stdlib.h>
  59  #if defined(HAVE_UNISTD_H)
  60  # include <unistd.h>
  61  #endif
  62  
  63  #ifndef _DIAGASSERT
  64  #define _DIAGASSERT(cond)       assert(cond)
  65  #endif
  66  
  67  /*
  68   * Constant used by SHA256/384/512_End() functions for converting the
  69   * digest to a readable hexadecimal character string:
  70   */
  71  static const char sha2_hex_digits[] = "0123456789abcdef";
  72  
  73  char           *
  74  SHA256_File(char *filename, char *buf)
  75  {
  76          uint8_t         buffer[BUFSIZ * 20];
  77          SHA256_CTX      ctx;
  78          int             fd, num, oerrno;
  79  
  80          _DIAGASSERT(filename != NULL);
  81          /* XXX: buf may be NULL ? */
  82  
  83          SHA256_Init(&ctx);
  84  
  85          if ((fd = open(filename, O_RDONLY)) < 0)
  86                  return (0);
  87  
  88          while ((num = read(fd, buffer, sizeof(buffer))) > 0)
  89                  SHA256_Update(&ctx, buffer, (size_t) num);
  90  
  91          oerrno = errno;
  92          close(fd);
  93          errno = oerrno;
  94          return (num < 0 ? 0 : SHA256_End(&ctx, buf));
  95  }
  96  
  97  
  98  char           *
  99  SHA256_End(SHA256_CTX *ctx, char buffer[])
 100  {
 101          uint8_t         digest[SHA256_DIGEST_LENGTH], *d = digest;
 102          uint8_t        *ret;
 103          int             i;
 104  
 105          /* Sanity check: */
 106          assert(ctx != NULL);
 107  
 108          if ((ret = buffer) != NULL) {
 109                  SHA256_Final(digest, ctx);
 110  
 111                  for (i = 0; i < SHA256_DIGEST_LENGTH; i++) {
 112                          *buffer++ = sha2_hex_digits[(*d & 0xf0) >> 4];
 113                          *buffer++ = sha2_hex_digits[*d & 0x0f];
 114                          d++;
 115                  }
 116                  *buffer = (char) 0;
 117          } else {
 118                  (void) memset(ctx, 0, sizeof(SHA256_CTX));
 119          }
 120          (void) memset(digest, 0, SHA256_DIGEST_LENGTH);
 121          return ret;
 122  }
 123  
 124  char           *
 125  SHA256_Data(const uint8_t * data, size_t len, char *digest)
 126  {
 127          SHA256_CTX      ctx;
 128  
 129          SHA256_Init(&ctx);
 130          SHA256_Update(&ctx, data, len);
 131          return SHA256_End(&ctx, digest);
 132  }
 133  
 134  char           *
 135  SHA384_File(char *filename, char *buf)
 136  {
 137          SHA384_CTX      ctx;
 138          uint8_t         buffer[BUFSIZ * 20];
 139          int             fd, num, oerrno;
 140  
 141          _DIAGASSERT(filename != NULL);
 142          /* XXX: buf may be NULL ? */
 143  
 144          SHA384_Init(&ctx);
 145  
 146          if ((fd = open(filename, O_RDONLY)) < 0)
 147                  return (0);
 148  
 149          while ((num = read(fd, buffer, sizeof(buffer))) > 0)
 150                  SHA384_Update(&ctx, buffer, (size_t) num);
 151  
 152          oerrno = errno;
 153          close(fd);
 154          errno = oerrno;
 155          return (num < 0 ? 0 : SHA384_End(&ctx, buf));
 156  }
 157  
 158  char           *
 159  SHA384_End(SHA384_CTX * ctx, char buffer[])
 160  {
 161          uint8_t         digest[SHA384_DIGEST_LENGTH], *d = digest;
 162          uint8_t        *ret;
 163          int             i;
 164  
 165          /* Sanity check: */
 166          assert(ctx != NULL);
 167  
 168          if ((ret = buffer) != NULL) {
 169                  SHA384_Final(digest, ctx);
 170  
 171                  for (i = 0; i < SHA384_DIGEST_LENGTH; i++) {
 172                          *buffer++ = sha2_hex_digits[(*d & 0xf0) >> 4];
 173                          *buffer++ = sha2_hex_digits[*d & 0x0f];
 174                          d++;
 175                  }
 176                  *buffer = (char) 0;
 177          } else {
 178                  (void) memset(ctx, 0, sizeof(SHA384_CTX));
 179          }
 180          (void) memset(digest, 0, SHA384_DIGEST_LENGTH);
 181          return ret;
 182  }
 183  
 184  char           *
 185  SHA384_Data(const uint8_t * data, size_t len, char *digest)
 186  {
 187          SHA384_CTX      ctx;
 188  
 189          SHA384_Init(&ctx);
 190          SHA384_Update(&ctx, data, len);
 191          return SHA384_End(&ctx, digest);
 192  }
 193  
 194  char           *
 195  SHA512_File(char *filename, char *buf)
 196  {
 197          SHA512_CTX      ctx;
 198          uint8_t         buffer[BUFSIZ * 20];
 199          int             fd, num, oerrno;
 200  
 201          _DIAGASSERT(filename != NULL);
 202          /* XXX: buf may be NULL ? */
 203  
 204          SHA512_Init(&ctx);
 205  
 206          if ((fd = open(filename, O_RDONLY)) < 0)
 207                  return (0);
 208  
 209          while ((num = read(fd, buffer, sizeof(buffer))) > 0)
 210                  SHA512_Update(&ctx, buffer, (size_t) num);
 211  
 212          oerrno = errno;
 213          close(fd);
 214          errno = oerrno;
 215          return (num < 0 ? 0 : SHA512_End(&ctx, buf));
 216  }
 217  
 218  char           *
 219  SHA512_End(SHA512_CTX * ctx, char buffer[])
 220  {
 221          uint8_t         digest[SHA512_DIGEST_LENGTH], *d = digest;
 222          uint8_t        *ret;
 223          int             i;
 224  
 225          /* Sanity check: */
 226          assert(ctx != NULL);
 227  
 228          if ((ret = buffer) != NULL) {
 229                  SHA512_Final(digest, ctx);
 230  
 231                  for (i = 0; i < SHA512_DIGEST_LENGTH; i++) {
 232                          *buffer++ = sha2_hex_digits[(*d & 0xf0) >> 4];
 233                          *buffer++ = sha2_hex_digits[*d & 0x0f];
 234                          d++;
 235                  }
 236                  *buffer = (char) 0;
 237          } else {
 238                  (void) memset(ctx, 0, sizeof(SHA512_CTX));
 239          }
 240          (void) memset(digest, 0, SHA512_DIGEST_LENGTH);
 241          return ret;
 242  }
 243  
 244  char           *
 245  SHA512_Data(const uint8_t * data, size_t len, char *digest)
 246  {
 247          SHA512_CTX      ctx;
 248  
 249          SHA512_Init(&ctx);
 250          SHA512_Update(&ctx, data, len);
 251          return SHA512_End(&ctx, digest);
 252  }