root/lib/sha1.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. sha1_init_ctx
  2. set_uint32
  3. sha1_read_ctx
  4. sha1_finish_ctx
  5. sha1_buffer
  6. sha1_process_bytes
  7. sha1_process_block

     1 /* sha1.c - Functions to compute SHA1 message digest of files or
     2    memory blocks according to the NIST specification FIPS-180-1.
     3 
     4    Copyright (C) 2000-2001, 2003-2006, 2008-2023 Free Software
     5    Foundation, Inc.
     6 
     7    This file is free software: you can redistribute it and/or modify
     8    it under the terms of the GNU Lesser General Public License as
     9    published by the Free Software Foundation; either version 2.1 of the
    10    License, or (at your option) any later version.
    11 
    12    This file is distributed in the hope that it will be useful,
    13    but WITHOUT ANY WARRANTY; without even the implied warranty of
    14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    15    GNU Lesser General Public License for more details.
    16 
    17    You should have received a copy of the GNU Lesser General Public License
    18    along with this program.  If not, see <https://www.gnu.org/licenses/>.  */
    19 
    20 /* Written by Scott G. Miller
    21    Credits:
    22       Robert Klep <robert@ilse.nl>  -- Expansion function fix
    23 */
    24 
    25 #include <config.h>
    26 
    27 /* Specification.  */
    28 #if HAVE_OPENSSL_SHA1
    29 # define GL_OPENSSL_INLINE _GL_EXTERN_INLINE
    30 #endif
    31 #include "sha1.h"
    32 
    33 #include <stdint.h>
    34 #include <string.h>
    35 
    36 #include <byteswap.h>
    37 #ifdef WORDS_BIGENDIAN
    38 # define SWAP(n) (n)
    39 #else
    40 # define SWAP(n) bswap_32 (n)
    41 #endif
    42 
    43 #if ! HAVE_OPENSSL_SHA1
    44 
    45 /* This array contains the bytes used to pad the buffer to the next
    46    64-byte boundary.  (RFC 1321, 3.1: Step 1)  */
    47 static const unsigned char fillbuf[64] = { 0x80, 0 /* , 0, 0, ...  */ };
    48 
    49 
    50 /* Take a pointer to a 160 bit block of data (five 32 bit ints) and
    51    initialize it to the start constants of the SHA1 algorithm.  This
    52    must be called before using hash in the call to sha1_hash.  */
    53 void
    54 sha1_init_ctx (struct sha1_ctx *ctx)
    55 {
    56   ctx->A = 0x67452301;
    57   ctx->B = 0xefcdab89;
    58   ctx->C = 0x98badcfe;
    59   ctx->D = 0x10325476;
    60   ctx->E = 0xc3d2e1f0;
    61 
    62   ctx->total[0] = ctx->total[1] = 0;
    63   ctx->buflen = 0;
    64 }
    65 
    66 /* Copy the 4 byte value from v into the memory location pointed to by *cp,
    67    If your architecture allows unaligned access this is equivalent to
    68    * (uint32_t *) cp = v  */
    69 static void
    70 set_uint32 (char *cp, uint32_t v)
    71 {
    72   memcpy (cp, &v, sizeof v);
    73 }
    74 
    75 /* Put result from CTX in first 20 bytes following RESBUF.  The result
    76    must be in little endian byte order.  */
    77 void *
    78 sha1_read_ctx (const struct sha1_ctx *ctx, void *resbuf)
    79 {
    80   char *r = resbuf;
    81   set_uint32 (r + 0 * sizeof ctx->A, SWAP (ctx->A));
    82   set_uint32 (r + 1 * sizeof ctx->B, SWAP (ctx->B));
    83   set_uint32 (r + 2 * sizeof ctx->C, SWAP (ctx->C));
    84   set_uint32 (r + 3 * sizeof ctx->D, SWAP (ctx->D));
    85   set_uint32 (r + 4 * sizeof ctx->E, SWAP (ctx->E));
    86 
    87   return resbuf;
    88 }
    89 
    90 /* Process the remaining bytes in the internal buffer and the usual
    91    prolog according to the standard and write the result to RESBUF.  */
    92 void *
    93 sha1_finish_ctx (struct sha1_ctx *ctx, void *resbuf)
    94 {
    95   /* Take yet unprocessed bytes into account.  */
    96   uint32_t bytes = ctx->buflen;
    97   size_t size = (bytes < 56) ? 64 / 4 : 64 * 2 / 4;
    98 
    99   /* Now count remaining bytes.  */
   100   ctx->total[0] += bytes;
   101   if (ctx->total[0] < bytes)
   102     ++ctx->total[1];
   103 
   104   /* Put the 64-bit file length in *bits* at the end of the buffer.  */
   105   ctx->buffer[size - 2] = SWAP ((ctx->total[1] << 3) | (ctx->total[0] >> 29));
   106   ctx->buffer[size - 1] = SWAP (ctx->total[0] << 3);
   107 
   108   memcpy (&((char *) ctx->buffer)[bytes], fillbuf, (size - 2) * 4 - bytes);
   109 
   110   /* Process last bytes.  */
   111   sha1_process_block (ctx->buffer, size * 4, ctx);
   112 
   113   return sha1_read_ctx (ctx, resbuf);
   114 }
   115 
   116 /* Compute SHA1 message digest for LEN bytes beginning at BUFFER.  The
   117    result is always in little endian byte order, so that a byte-wise
   118    output yields to the wanted ASCII representation of the message
   119    digest.  */
   120 void *
   121 sha1_buffer (const char *buffer, size_t len, void *resblock)
   122 {
   123   struct sha1_ctx ctx;
   124 
   125   /* Initialize the computation context.  */
   126   sha1_init_ctx (&ctx);
   127 
   128   /* Process whole buffer but last len % 64 bytes.  */
   129   sha1_process_bytes (buffer, len, &ctx);
   130 
   131   /* Put result in desired memory area.  */
   132   return sha1_finish_ctx (&ctx, resblock);
   133 }
   134 
   135 void
   136 sha1_process_bytes (const void *buffer, size_t len, struct sha1_ctx *ctx)
   137 {
   138   /* When we already have some bits in our internal buffer concatenate
   139      both inputs first.  */
   140   if (ctx->buflen != 0)
   141     {
   142       size_t left_over = ctx->buflen;
   143       size_t add = 128 - left_over > len ? len : 128 - left_over;
   144 
   145       memcpy (&((char *) ctx->buffer)[left_over], buffer, add);
   146       ctx->buflen += add;
   147 
   148       if (ctx->buflen > 64)
   149         {
   150           sha1_process_block (ctx->buffer, ctx->buflen & ~63, ctx);
   151 
   152           ctx->buflen &= 63;
   153           /* The regions in the following copy operation cannot overlap,
   154              because ctx->buflen < 64 ≤ (left_over + add) & ~63.  */
   155           memcpy (ctx->buffer,
   156                   &((char *) ctx->buffer)[(left_over + add) & ~63],
   157                   ctx->buflen);
   158         }
   159 
   160       buffer = (const char *) buffer + add;
   161       len -= add;
   162     }
   163 
   164   /* Process available complete blocks.  */
   165   if (len >= 64)
   166     {
   167 #if !(_STRING_ARCH_unaligned || _STRING_INLINE_unaligned)
   168 # define UNALIGNED_P(p) ((uintptr_t) (p) % alignof (uint32_t) != 0)
   169       if (UNALIGNED_P (buffer))
   170         while (len > 64)
   171           {
   172             sha1_process_block (memcpy (ctx->buffer, buffer, 64), 64, ctx);
   173             buffer = (const char *) buffer + 64;
   174             len -= 64;
   175           }
   176       else
   177 #endif
   178         {
   179           sha1_process_block (buffer, len & ~63, ctx);
   180           buffer = (const char *) buffer + (len & ~63);
   181           len &= 63;
   182         }
   183     }
   184 
   185   /* Move remaining bytes in internal buffer.  */
   186   if (len > 0)
   187     {
   188       size_t left_over = ctx->buflen;
   189 
   190       memcpy (&((char *) ctx->buffer)[left_over], buffer, len);
   191       left_over += len;
   192       if (left_over >= 64)
   193         {
   194           sha1_process_block (ctx->buffer, 64, ctx);
   195           left_over -= 64;
   196           /* The regions in the following copy operation cannot overlap,
   197              because left_over ≤ 64.  */
   198           memcpy (ctx->buffer, &ctx->buffer[16], left_over);
   199         }
   200       ctx->buflen = left_over;
   201     }
   202 }
   203 
   204 /* --- Code below is the primary difference between md5.c and sha1.c --- */
   205 
   206 /* SHA1 round constants */
   207 #define K1 0x5a827999
   208 #define K2 0x6ed9eba1
   209 #define K3 0x8f1bbcdc
   210 #define K4 0xca62c1d6
   211 
   212 /* Round functions.  Note that F2 is the same as F4.  */
   213 #define F1(B,C,D) ( D ^ ( B & ( C ^ D ) ) )
   214 #define F2(B,C,D) (B ^ C ^ D)
   215 #define F3(B,C,D) ( ( B & C ) | ( D & ( B | C ) ) )
   216 #define F4(B,C,D) (B ^ C ^ D)
   217 
   218 /* Process LEN bytes of BUFFER, accumulating context into CTX.
   219    It is assumed that LEN % 64 == 0.
   220    Most of this code comes from GnuPG's cipher/sha1.c.  */
   221 
   222 void
   223 sha1_process_block (const void *buffer, size_t len, struct sha1_ctx *ctx)
   224 {
   225   const uint32_t *words = buffer;
   226   size_t nwords = len / sizeof (uint32_t);
   227   const uint32_t *endp = words + nwords;
   228   uint32_t x[16];
   229   uint32_t a = ctx->A;
   230   uint32_t b = ctx->B;
   231   uint32_t c = ctx->C;
   232   uint32_t d = ctx->D;
   233   uint32_t e = ctx->E;
   234   uint32_t lolen = len;
   235 
   236   /* First increment the byte count.  RFC 1321 specifies the possible
   237      length of the file up to 2^64 bits.  Here we only compute the
   238      number of bytes.  Do a double word increment.  */
   239   ctx->total[0] += lolen;
   240   ctx->total[1] += (len >> 31 >> 1) + (ctx->total[0] < lolen);
   241 
   242 #define rol(x, n) (((x) << (n)) | ((uint32_t) (x) >> (32 - (n))))
   243 
   244 #define M(I) ( tm =   x[I&0x0f] ^ x[(I-14)&0x0f] \
   245                     ^ x[(I-8)&0x0f] ^ x[(I-3)&0x0f] \
   246                , (x[I&0x0f] = rol(tm, 1)) )
   247 
   248 #define R(A,B,C,D,E,F,K,M)  do { E += rol( A, 5 )     \
   249                                       + F( B, C, D )  \
   250                                       + K             \
   251                                       + M;            \
   252                                  B = rol( B, 30 );    \
   253                                } while(0)
   254 
   255   while (words < endp)
   256     {
   257       uint32_t tm;
   258       int t;
   259       for (t = 0; t < 16; t++)
   260         {
   261           x[t] = SWAP (*words);
   262           words++;
   263         }
   264 
   265       R( a, b, c, d, e, F1, K1, x[ 0] );
   266       R( e, a, b, c, d, F1, K1, x[ 1] );
   267       R( d, e, a, b, c, F1, K1, x[ 2] );
   268       R( c, d, e, a, b, F1, K1, x[ 3] );
   269       R( b, c, d, e, a, F1, K1, x[ 4] );
   270       R( a, b, c, d, e, F1, K1, x[ 5] );
   271       R( e, a, b, c, d, F1, K1, x[ 6] );
   272       R( d, e, a, b, c, F1, K1, x[ 7] );
   273       R( c, d, e, a, b, F1, K1, x[ 8] );
   274       R( b, c, d, e, a, F1, K1, x[ 9] );
   275       R( a, b, c, d, e, F1, K1, x[10] );
   276       R( e, a, b, c, d, F1, K1, x[11] );
   277       R( d, e, a, b, c, F1, K1, x[12] );
   278       R( c, d, e, a, b, F1, K1, x[13] );
   279       R( b, c, d, e, a, F1, K1, x[14] );
   280       R( a, b, c, d, e, F1, K1, x[15] );
   281       R( e, a, b, c, d, F1, K1, M(16) );
   282       R( d, e, a, b, c, F1, K1, M(17) );
   283       R( c, d, e, a, b, F1, K1, M(18) );
   284       R( b, c, d, e, a, F1, K1, M(19) );
   285       R( a, b, c, d, e, F2, K2, M(20) );
   286       R( e, a, b, c, d, F2, K2, M(21) );
   287       R( d, e, a, b, c, F2, K2, M(22) );
   288       R( c, d, e, a, b, F2, K2, M(23) );
   289       R( b, c, d, e, a, F2, K2, M(24) );
   290       R( a, b, c, d, e, F2, K2, M(25) );
   291       R( e, a, b, c, d, F2, K2, M(26) );
   292       R( d, e, a, b, c, F2, K2, M(27) );
   293       R( c, d, e, a, b, F2, K2, M(28) );
   294       R( b, c, d, e, a, F2, K2, M(29) );
   295       R( a, b, c, d, e, F2, K2, M(30) );
   296       R( e, a, b, c, d, F2, K2, M(31) );
   297       R( d, e, a, b, c, F2, K2, M(32) );
   298       R( c, d, e, a, b, F2, K2, M(33) );
   299       R( b, c, d, e, a, F2, K2, M(34) );
   300       R( a, b, c, d, e, F2, K2, M(35) );
   301       R( e, a, b, c, d, F2, K2, M(36) );
   302       R( d, e, a, b, c, F2, K2, M(37) );
   303       R( c, d, e, a, b, F2, K2, M(38) );
   304       R( b, c, d, e, a, F2, K2, M(39) );
   305       R( a, b, c, d, e, F3, K3, M(40) );
   306       R( e, a, b, c, d, F3, K3, M(41) );
   307       R( d, e, a, b, c, F3, K3, M(42) );
   308       R( c, d, e, a, b, F3, K3, M(43) );
   309       R( b, c, d, e, a, F3, K3, M(44) );
   310       R( a, b, c, d, e, F3, K3, M(45) );
   311       R( e, a, b, c, d, F3, K3, M(46) );
   312       R( d, e, a, b, c, F3, K3, M(47) );
   313       R( c, d, e, a, b, F3, K3, M(48) );
   314       R( b, c, d, e, a, F3, K3, M(49) );
   315       R( a, b, c, d, e, F3, K3, M(50) );
   316       R( e, a, b, c, d, F3, K3, M(51) );
   317       R( d, e, a, b, c, F3, K3, M(52) );
   318       R( c, d, e, a, b, F3, K3, M(53) );
   319       R( b, c, d, e, a, F3, K3, M(54) );
   320       R( a, b, c, d, e, F3, K3, M(55) );
   321       R( e, a, b, c, d, F3, K3, M(56) );
   322       R( d, e, a, b, c, F3, K3, M(57) );
   323       R( c, d, e, a, b, F3, K3, M(58) );
   324       R( b, c, d, e, a, F3, K3, M(59) );
   325       R( a, b, c, d, e, F4, K4, M(60) );
   326       R( e, a, b, c, d, F4, K4, M(61) );
   327       R( d, e, a, b, c, F4, K4, M(62) );
   328       R( c, d, e, a, b, F4, K4, M(63) );
   329       R( b, c, d, e, a, F4, K4, M(64) );
   330       R( a, b, c, d, e, F4, K4, M(65) );
   331       R( e, a, b, c, d, F4, K4, M(66) );
   332       R( d, e, a, b, c, F4, K4, M(67) );
   333       R( c, d, e, a, b, F4, K4, M(68) );
   334       R( b, c, d, e, a, F4, K4, M(69) );
   335       R( a, b, c, d, e, F4, K4, M(70) );
   336       R( e, a, b, c, d, F4, K4, M(71) );
   337       R( d, e, a, b, c, F4, K4, M(72) );
   338       R( c, d, e, a, b, F4, K4, M(73) );
   339       R( b, c, d, e, a, F4, K4, M(74) );
   340       R( a, b, c, d, e, F4, K4, M(75) );
   341       R( e, a, b, c, d, F4, K4, M(76) );
   342       R( d, e, a, b, c, F4, K4, M(77) );
   343       R( c, d, e, a, b, F4, K4, M(78) );
   344       R( b, c, d, e, a, F4, K4, M(79) );
   345 
   346       a = ctx->A += a;
   347       b = ctx->B += b;
   348       c = ctx->C += c;
   349       d = ctx->D += d;
   350       e = ctx->E += e;
   351     }
   352 }
   353 
   354 #endif
   355 
   356 /*
   357  * Hey Emacs!
   358  * Local Variables:
   359  * coding: utf-8
   360  * End:
   361  */

/* [<][>][^][v][top][bottom][index][help] */