Hex Artifact Content

Artifact cf44cc0e4dd4c91e12a16b90b6fd5d19274ee576:


0000: 2f 2a 0a 20 2a 20 20 73 68 61 31 2e 68 0a 20 2a  /*. *  sha1.h. *
0010: 0a 20 2a 20 20 44 65 73 63 72 69 70 74 69 6f 6e  . *  Description
0020: 3a 0a 20 2a 20 20 20 20 20 20 54 68 69 73 20 69  :. *      This i
0030: 73 20 74 68 65 20 68 65 61 64 65 72 20 66 69 6c  s the header fil
0040: 65 20 66 6f 72 20 63 6f 64 65 20 77 68 69 63 68  e for code which
0050: 20 69 6d 70 6c 65 6d 65 6e 74 73 20 74 68 65 20   implements the 
0060: 53 65 63 75 72 65 0a 20 2a 20 20 20 20 20 20 48  Secure. *      H
0070: 61 73 68 69 6e 67 20 41 6c 67 6f 72 69 74 68 6d  ashing Algorithm
0080: 20 31 20 61 73 20 64 65 66 69 6e 65 64 20 69 6e   1 as defined in
0090: 20 46 49 50 53 20 50 55 42 20 31 38 30 2d 31 20   FIPS PUB 180-1 
00a0: 70 75 62 6c 69 73 68 65 64 0a 20 2a 20 20 20 20  published. *    
00b0: 20 20 41 70 72 69 6c 20 31 37 2c 20 31 39 39 35    April 17, 1995
00c0: 2e 0a 20 2a 0a 20 2a 20 20 20 20 20 20 4d 61 6e  .. *. *      Man
00d0: 79 20 6f 66 20 74 68 65 20 76 61 72 69 61 62 6c  y of the variabl
00e0: 65 20 6e 61 6d 65 73 20 69 6e 20 74 68 69 73 20  e names in this 
00f0: 63 6f 64 65 2c 20 65 73 70 65 63 69 61 6c 6c 79  code, especially
0100: 20 74 68 65 0a 20 2a 20 20 20 20 20 20 73 69 6e   the. *      sin
0110: 67 6c 65 20 63 68 61 72 61 63 74 65 72 20 6e 61  gle character na
0120: 6d 65 73 2c 20 77 65 72 65 20 75 73 65 64 20 62  mes, were used b
0130: 65 63 61 75 73 65 20 74 68 6f 73 65 20 77 65 72  ecause those wer
0140: 65 20 74 68 65 20 6e 61 6d 65 73 0a 20 2a 20 20  e the names. *  
0150: 20 20 20 20 75 73 65 64 20 69 6e 20 74 68 65 20      used in the 
0160: 70 75 62 6c 69 63 61 74 69 6f 6e 2e 0a 20 2a 0a  publication.. *.
0170: 20 2a 20 20 20 20 20 20 50 6c 65 61 73 65 20 72   *      Please r
0180: 65 61 64 20 74 68 65 20 66 69 6c 65 20 73 68 61  ead the file sha
0190: 31 2e 63 20 66 6f 72 20 6d 6f 72 65 20 69 6e 66  1.c for more inf
01a0: 6f 72 6d 61 74 69 6f 6e 2e 0a 20 2a 0a 20 2a 2f  ormation.. *. */
01b0: 0a 0a 23 69 66 6e 64 65 66 20 5f 53 48 41 31 5f  ..#ifndef _SHA1_
01c0: 48 5f 0a 23 64 65 66 69 6e 65 20 5f 53 48 41 31  H_.#define _SHA1
01d0: 5f 48 5f 0a 0a 23 69 6e 63 6c 75 64 65 20 22 63  _H_..#include "c
01e0: 6f 6e 66 69 67 2e 68 22 0a 0a 23 69 66 64 65 66  onfig.h"..#ifdef
01f0: 20 48 41 56 45 5f 53 54 44 49 4e 54 5f 48 0a 23   HAVE_STDINT_H.#
0200: 20 20 69 6e 63 6c 75 64 65 20 3c 73 74 64 69 6e    include <stdin
0210: 74 2e 68 3e 0a 23 65 6e 64 69 66 0a 23 69 66 64  t.h>.#endif.#ifd
0220: 65 66 20 48 41 56 45 5f 49 4e 54 54 59 50 45 53  ef HAVE_INTTYPES
0230: 5f 48 0a 23 20 20 69 6e 63 6c 75 64 65 20 3c 69  _H.#  include <i
0240: 6e 74 74 79 70 65 73 2e 68 3e 0a 23 65 6e 64 69  nttypes.h>.#endi
0250: 66 0a 0a 2f 2a 0a 20 2a 20 49 66 20 79 6f 75 20  f../*. * If you 
0260: 64 6f 20 6e 6f 74 20 68 61 76 65 20 74 68 65 20  do not have the 
0270: 49 53 4f 20 73 74 61 6e 64 61 72 64 20 73 74 64  ISO standard std
0280: 69 6e 74 2e 68 20 68 65 61 64 65 72 20 66 69 6c  int.h header fil
0290: 65 2c 20 74 68 65 6e 20 79 6f 75 0a 20 2a 20 6d  e, then you. * m
02a0: 75 73 74 20 74 79 70 64 65 66 20 74 68 65 20 66  ust typdef the f
02b0: 6f 6c 6c 6f 77 69 6e 67 3a 0a 20 2a 20 20 20 20  ollowing:. *    
02c0: 6e 61 6d 65 20 20 20 20 20 20 20 20 20 20 20 20  name            
02d0: 20 20 6d 65 61 6e 69 6e 67 0a 20 2a 20 20 75 69    meaning. *  ui
02e0: 6e 74 33 32 5f 74 20 20 20 20 20 20 20 20 20 75  nt32_t         u
02f0: 6e 73 69 67 6e 65 64 20 33 32 20 62 69 74 20 69  nsigned 32 bit i
0300: 6e 74 65 67 65 72 0a 20 2a 20 20 75 69 6e 74 38  nteger. *  uint8
0310: 5f 74 20 20 20 20 20 20 20 20 20 20 75 6e 73 69  _t          unsi
0320: 67 6e 65 64 20 38 20 62 69 74 20 69 6e 74 65 67  gned 8 bit integ
0330: 65 72 20 28 69 2e 65 2e 2c 20 75 6e 73 69 67 6e  er (i.e., unsign
0340: 65 64 20 63 68 61 72 29 0a 20 2a 20 20 69 6e 74  ed char). *  int
0350: 5f 6c 65 61 73 74 31 36 5f 74 20 20 20 20 69 6e  _least16_t    in
0360: 74 65 67 65 72 20 6f 66 20 3e 3d 20 31 36 20 62  teger of >= 16 b
0370: 69 74 73 0a 20 2a 0a 20 2a 2f 0a 0a 23 69 66 6e  its. *. */..#ifn
0380: 64 65 66 20 5f 53 48 41 5f 65 6e 75 6d 5f 0a 23  def _SHA_enum_.#
0390: 64 65 66 69 6e 65 20 5f 53 48 41 5f 65 6e 75 6d  define _SHA_enum
03a0: 5f 0a 65 6e 75 6d 0a 7b 0a 20 20 20 20 73 68 61  _.enum.{.    sha
03b0: 53 75 63 63 65 73 73 20 3d 20 30 2c 0a 20 20 20  Success = 0,.   
03c0: 20 73 68 61 4e 75 6c 6c 2c 20 20 20 20 20 20 20   shaNull,       
03d0: 20 20 20 20 20 2f 2a 20 4e 75 6c 6c 20 70 6f 69       /* Null poi
03e0: 6e 74 65 72 20 70 61 72 61 6d 65 74 65 72 20 2a  nter parameter *
03f0: 2f 0a 20 20 20 20 73 68 61 49 6e 70 75 74 54 6f  /.    shaInputTo
0400: 6f 4c 6f 6e 67 2c 20 20 20 20 2f 2a 20 69 6e 70  oLong,    /* inp
0410: 75 74 20 64 61 74 61 20 74 6f 6f 20 6c 6f 6e 67  ut data too long
0420: 20 2a 2f 0a 20 20 20 20 73 68 61 53 74 61 74 65   */.    shaState
0430: 45 72 72 6f 72 20 20 20 20 20 20 20 2f 2a 20 63  Error       /* c
0440: 61 6c 6c 65 64 20 49 6e 70 75 74 20 61 66 74 65  alled Input afte
0450: 72 20 52 65 73 75 6c 74 20 2a 2f 0a 7d 3b 0a 23  r Result */.};.#
0460: 65 6e 64 69 66 0a 23 64 65 66 69 6e 65 20 53 48  endif.#define SH
0470: 41 31 48 61 73 68 53 69 7a 65 20 32 30 0a 0a 2f  A1HashSize 20../
0480: 2a 0a 20 2a 20 20 54 68 69 73 20 73 74 72 75 63  *. *  This struc
0490: 74 75 72 65 20 77 69 6c 6c 20 68 6f 6c 64 20 63  ture will hold c
04a0: 6f 6e 74 65 78 74 20 69 6e 66 6f 72 6d 61 74 69  ontext informati
04b0: 6f 6e 20 66 6f 72 20 74 68 65 20 53 48 41 2d 31  on for the SHA-1
04c0: 0a 20 2a 20 20 68 61 73 68 69 6e 67 20 6f 70 65  . *  hashing ope
04d0: 72 61 74 69 6f 6e 0a 20 2a 2f 0a 74 79 70 65 64  ration. */.typed
04e0: 65 66 20 73 74 72 75 63 74 20 53 48 41 31 43 6f  ef struct SHA1Co
04f0: 6e 74 65 78 74 0a 7b 0a 20 20 20 20 75 69 6e 74  ntext.{.    uint
0500: 33 32 5f 74 20 49 6e 74 65 72 6d 65 64 69 61 74  32_t Intermediat
0510: 65 5f 48 61 73 68 5b 53 48 41 31 48 61 73 68 53  e_Hash[SHA1HashS
0520: 69 7a 65 2f 34 5d 3b 20 2f 2a 20 4d 65 73 73 61  ize/4]; /* Messa
0530: 67 65 20 44 69 67 65 73 74 20 20 2a 2f 0a 0a 20  ge Digest  */.. 
0540: 20 20 20 75 69 6e 74 33 32 5f 74 20 4c 65 6e 67     uint32_t Leng
0550: 74 68 5f 4c 6f 77 3b 20 20 20 20 20 20 20 20 20  th_Low;         
0560: 20 20 20 2f 2a 20 4d 65 73 73 61 67 65 20 6c 65     /* Message le
0570: 6e 67 74 68 20 69 6e 20 62 69 74 73 20 20 20 20  ngth in bits    
0580: 20 20 2a 2f 0a 20 20 20 20 75 69 6e 74 33 32 5f    */.    uint32_
0590: 74 20 4c 65 6e 67 74 68 5f 48 69 67 68 3b 20 20  t Length_High;  
05a0: 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 65 73 73           /* Mess
05b0: 61 67 65 20 6c 65 6e 67 74 68 20 69 6e 20 62 69  age length in bi
05c0: 74 73 20 20 20 20 20 20 2a 2f 0a 0a 20 20 20 20  ts      */..    
05d0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
05e0: 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e             /* In
05f0: 64 65 78 20 69 6e 74 6f 20 6d 65 73 73 61 67 65  dex into message
0600: 20 62 6c 6f 63 6b 20 61 72 72 61 79 20 20 20 2a   block array   *
0610: 2f 0a 20 20 20 20 69 6e 74 5f 6c 65 61 73 74 31  /.    int_least1
0620: 36 5f 74 20 4d 65 73 73 61 67 65 5f 42 6c 6f 63  6_t Message_Bloc
0630: 6b 5f 49 6e 64 65 78 3b 0a 20 20 20 20 75 69 6e  k_Index;.    uin
0640: 74 38 5f 74 20 4d 65 73 73 61 67 65 5f 42 6c 6f  t8_t Message_Blo
0650: 63 6b 5b 36 34 5d 3b 20 20 20 20 20 20 2f 2a 20  ck[64];      /* 
0660: 35 31 32 2d 62 69 74 20 6d 65 73 73 61 67 65 20  512-bit message 
0670: 62 6c 6f 63 6b 73 20 20 20 20 20 20 2a 2f 0a 0a  blocks      */..
0680: 20 20 20 20 69 6e 74 20 43 6f 6d 70 75 74 65 64      int Computed
0690: 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
06a0: 2f 2a 20 49 73 20 74 68 65 20 64 69 67 65 73 74  /* Is the digest
06b0: 20 63 6f 6d 70 75 74 65 64 3f 20 20 20 20 20 20   computed?      
06c0: 20 20 20 2a 2f 0a 20 20 20 20 69 6e 74 20 43 6f     */.    int Co
06d0: 72 72 75 70 74 65 64 3b 20 20 20 20 20 20 20 20  rrupted;        
06e0: 20 20 20 20 20 2f 2a 20 49 73 20 74 68 65 20 6d       /* Is the m
06f0: 65 73 73 61 67 65 20 64 69 67 65 73 74 20 63 6f  essage digest co
0700: 72 72 75 70 74 65 64 3f 20 2a 2f 0a 7d 20 53 48  rrupted? */.} SH
0710: 41 31 43 6f 6e 74 65 78 74 3b 0a 0a 2f 2a 0a 20  A1Context;../*. 
0720: 2a 20 20 46 75 6e 63 74 69 6f 6e 20 50 72 6f 74  *  Function Prot
0730: 6f 74 79 70 65 73 0a 20 2a 2f 0a 73 74 61 74 69  otypes. */.stati
0740: 63 20 69 6e 74 20 53 48 41 31 52 65 73 65 74 28  c int SHA1Reset(
0750: 20 20 53 48 41 31 43 6f 6e 74 65 78 74 20 2a 29    SHA1Context *)
0760: 3b 0a 73 74 61 74 69 63 20 69 6e 74 20 53 48 41  ;.static int SHA
0770: 31 49 6e 70 75 74 28 20 20 53 48 41 31 43 6f 6e  1Input(  SHA1Con
0780: 74 65 78 74 20 2a 2c 0a 20 20 20 20 20 20 20 20  text *,.        
0790: 20 20 20 20 20 20 20 20 63 6f 6e 73 74 20 75 69          const ui
07a0: 6e 74 38 5f 74 20 2a 2c 0a 20 20 20 20 20 20 20  nt8_t *,.       
07b0: 20 20 20 20 20 20 20 20 20 75 6e 73 69 67 6e 65           unsigne
07c0: 64 20 69 6e 74 29 3b 0a 73 74 61 74 69 63 20 69  d int);.static i
07d0: 6e 74 20 53 48 41 31 52 65 73 75 6c 74 28 20 53  nt SHA1Result( S
07e0: 48 41 31 43 6f 6e 74 65 78 74 20 2a 2c 0a 20 20  HA1Context *,.  
07f0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 75 69                ui
0800: 6e 74 38 5f 74 20 4d 65 73 73 61 67 65 5f 44 69  nt8_t Message_Di
0810: 67 65 73 74 5b 53 48 41 31 48 61 73 68 53 69 7a  gest[SHA1HashSiz
0820: 65 5d 29 3b 0a 0a 23 65 6e 64 69 66 0a           e]);..#endif.