root/maint/gnulib/lib/sm3-stream.c

/* [previous][next][first][last][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. sm3_stream

   1 /* sm3.c - Functions to compute SM3 message digest of files or memory blocks
   2    according to the specification GM/T 004-2012 Cryptographic Hash Algorithm
   3    SM3, published by State Encryption Management Bureau, China.
   4 
   5    SM3 cryptographic hash algorithm.
   6    <http://www.sca.gov.cn/sca/xwdt/2010-12/17/content_1002389.shtml>
   7 
   8    Copyright (C) 2017-2021 Free Software Foundation, Inc.
   9 
  10    This file is free software: you can redistribute it and/or modify
  11    it under the terms of the GNU Lesser General Public License as
  12    published by the Free Software Foundation; either version 2.1 of the
  13    License, or (at your option) any later version.
  14 
  15    This file is distributed in the hope that it will be useful,
  16    but WITHOUT ANY WARRANTY; without even the implied warranty of
  17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  18    GNU Lesser General Public License for more details.
  19 
  20    You should have received a copy of the GNU Lesser General Public License
  21    along with this program.  If not, see <https://www.gnu.org/licenses/>.  */
  22 
  23 /* Written by Jia Zhang <qianyue.zj@alibaba-inc.com>, 2017,
  24    considerably copypasting from David Madore's sha256.c */
  25 
  26 #include <config.h>
  27 
  28 /* Specification.  */
  29 #if HAVE_OPENSSL_SM3
  30 # define GL_OPENSSL_INLINE _GL_EXTERN_INLINE
  31 #endif
  32 #include "sm3.h"
  33 
  34 #include <stdlib.h>
  35 
  36 #if USE_UNLOCKED_IO
  37 # include "unlocked-io.h"
  38 #endif
  39 
  40 #define BLOCKSIZE 32768
  41 #if BLOCKSIZE % 64 != 0
  42 # error "invalid BLOCKSIZE"
  43 #endif
  44 
  45 /* Compute SM3 message digest for bytes read from STREAM.  The
  46    resulting message digest number will be written into the 32 bytes
  47    beginning at RESBLOCK.  */
  48 int
  49 sm3_stream (FILE *stream, void *resblock)
     /* [previous][next][first][last][top][bottom][index][help] */
  50 {
  51   struct sm3_ctx ctx;
  52   size_t sum;
  53 
  54   char *buffer = malloc (BLOCKSIZE + 72);
  55   if (!buffer)
  56     return 1;
  57 
  58   /* Initialize the computation context.  */
  59   sm3_init_ctx (&ctx);
  60 
  61   /* Iterate over full file contents.  */
  62   while (1)
  63     {
  64       /* We read the file in blocks of BLOCKSIZE bytes.  One call of the
  65          computation function processes the whole buffer so that with the
  66          next round of the loop another block can be read.  */
  67       size_t n;
  68       sum = 0;
  69 
  70       /* Read block.  Take care for partial reads.  */
  71       while (1)
  72         {
  73           n = fread (buffer + sum, 1, BLOCKSIZE - sum, stream);
  74 
  75           sum += n;
  76 
  77           if (sum == BLOCKSIZE)
  78             break;
  79 
  80           if (n == 0)
  81             {
  82               /* Check for the error flag IFF N == 0, so that we don't
  83                  exit the loop after a partial read due to e.g., EAGAIN
  84                  or EWOULDBLOCK.  */
  85               if (ferror (stream))
  86                 {
  87                   free (buffer);
  88                   return 1;
  89                 }
  90               goto process_partial_block;
  91             }
  92 
  93           /* We've read at least one byte, so ignore errors.  But always
  94              check for EOF, since feof may be true even though N > 0.
  95              Otherwise, we could end up calling fread after EOF.  */
  96           if (feof (stream))
  97             goto process_partial_block;
  98         }
  99 
 100       /* Process buffer with BLOCKSIZE bytes.  Note that
 101                         BLOCKSIZE % 64 == 0
 102        */
 103       sm3_process_block (buffer, BLOCKSIZE, &ctx);
 104     }
 105 
 106  process_partial_block:;
 107 
 108   /* Process any remaining bytes.  */
 109   if (sum > 0)
 110     sm3_process_bytes (buffer, sum, &ctx);
 111 
 112   /* Construct result in desired memory.  */
 113   sm3_finish_ctx (&ctx, resblock);
 114   free (buffer);
 115   return 0;
 116 }
 117 
 118 /*
 119  * Hey Emacs!
 120  * Local Variables:
 121  * coding: utf-8
 122  * End:
 123  */

/* [previous][next][first][last][top][bottom][index][help] */