"Fossies" - the Fresh Open Source Software Archive

Member "tin-2.6.0/libcanlock/include/sha.h" (4 Jan 2021, 14362 Bytes) of package /linux/misc/tin-2.6.0.tar.xz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "sha.h" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: 2.4.5_vs_2.6.0.

    1 /**************************** sha.h ****************************/
    2 /***************** See RFC 6234 for details. *******************/
    3 /*
    4    Copyright (c) 2011 IETF Trust and the persons identified as
    5    authors of the code.  All rights reserved.
    6 
    7    Redistribution and use in source and binary forms, with or
    8    without modification, are permitted provided that the following
    9    conditions are met:
   10 
   11    - Redistributions of source code must retain the above
   12      copyright notice, this list of conditions and
   13      the following disclaimer.
   14 
   15    - Redistributions in binary form must reproduce the above
   16      copyright notice, this list of conditions and the following
   17      disclaimer in the documentation and/or other materials provided
   18      with the distribution.
   19 
   20    - Neither the name of Internet Society, IETF or IETF Trust, nor
   21      the names of specific contributors, may be used to endorse or
   22      promote products derived from this software without specific
   23      prior written permission.
   24 
   25    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
   26    CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
   27    INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
   28    MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   29    DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
   30    CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   31    SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
   32    NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
   33    LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   34    HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
   35    CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
   36    OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
   37    EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
   38 */
   39 #ifndef _SHA_H_
   40 #define _SHA_H_
   41 
   42 #pragma GCC visibility push(internal)
   43 
   44 /*
   45  *  Description:
   46  *      This file implements the Secure Hash Algorithms
   47  *      as defined in the U.S. National Institute of Standards
   48  *      and Technology Federal Information Processing Standards
   49  *      Publication (FIPS PUB) 180-3 published in October 2008
   50  *      and formerly defined in its predecessors, FIPS PUB 180-1
   51  *      and FIP PUB 180-2.
   52  *
   53  *      A combined document showing all algorithms is available at
   54  *              http://csrc.nist.gov/publications/fips/
   55  *                     fips180-3/fips180-3_final.pdf
   56  *
   57  *      The five hashes are defined in these sizes:
   58  *              SHA-1           20 byte / 160 bit
   59  *              SHA-224         28 byte / 224 bit
   60  *              SHA-256         32 byte / 256 bit
   61  *              SHA-384         48 byte / 384 bit
   62  *              SHA-512         64 byte / 512 bit
   63  *
   64  *  Compilation Note:
   65  *    These files may be compiled with two options:
   66  *        USE_32BIT_ONLY - use 32-bit arithmetic only, for systems
   67  *                         without 64-bit integers
   68  *
   69  *        USE_MODIFIED_MACROS - use alternate form of the SHA_Ch()
   70  *                         and SHA_Maj() macros that are equivalent
   71  *                         and potentially faster on many systems
   72  *
   73  */
   74 
   75 #include <stdint.h>
   76 /*
   77  * If you do not have the ISO standard stdint.h header file, then you
   78  * must typedef the following:
   79  *    name              meaning
   80  *  uint64_t         unsigned 64-bit integer
   81  *  uint32_t         unsigned 32-bit integer
   82  *  uint8_t          unsigned 8-bit integer (i.e., unsigned char)
   83  *  int_least16_t    integer of >= 16 bits
   84  *
   85  * See stdint-example.h
   86  */
   87 
   88 #ifndef _SHA_enum_
   89 #define _SHA_enum_
   90 /*
   91  *  All SHA functions return one of these values.
   92  */
   93 enum {
   94     shaSuccess = 0,
   95     shaNull,            /* Null pointer parameter */
   96     shaInputTooLong,    /* input data too long */
   97     shaStateError,      /* called Input after FinalBits or Result */
   98     shaBadParam         /* passed a bad parameter */
   99 };
  100 #endif /* _SHA_enum_ */
  101 
  102 /*
  103  *  These constants hold size information for each of the SHA
  104  *  hashing operations
  105  */
  106 enum {
  107     SHA1_Message_Block_Size = 64, SHA224_Message_Block_Size = 64,
  108     SHA256_Message_Block_Size = 64, SHA384_Message_Block_Size = 128,
  109     SHA512_Message_Block_Size = 128,
  110     USHA_Max_Message_Block_Size = SHA512_Message_Block_Size,
  111     SHA1HashSize = 20, SHA224HashSize = 28, SHA256HashSize = 32,
  112     SHA384HashSize = 48, SHA512HashSize = 64,
  113     USHAMaxHashSize = SHA512HashSize,
  114 
  115     SHA1HashSizeBits = 160, SHA224HashSizeBits = 224,
  116     SHA256HashSizeBits = 256, SHA384HashSizeBits = 384,
  117     SHA512HashSizeBits = 512, USHAMaxHashSizeBits = SHA512HashSizeBits
  118 };
  119 
  120 /*
  121  *  These constants are used in the USHA (Unified SHA) functions.
  122  */
  123 typedef enum SHAversion {
  124     SHA1, SHA224, SHA256, SHA384, SHA512
  125 } SHAversion;
  126 
  127 /*
  128  *  This structure will hold context information for the SHA-1
  129  *  hashing operation.
  130  */
  131 typedef struct SHA1Context {
  132     uint32_t Intermediate_Hash[SHA1HashSize/4]; /* Message Digest */
  133 
  134     uint32_t Length_High;               /* Message length in bits */
  135     uint32_t Length_Low;                /* Message length in bits */
  136 
  137     int_least16_t Message_Block_Index;  /* Message_Block array index */
  138                                         /* 512-bit message blocks */
  139     uint8_t Message_Block[SHA1_Message_Block_Size];
  140 
  141     int Computed;                   /* Is the hash computed? */
  142     int Corrupted;                  /* Cumulative corruption code */
  143 } SHA1Context;
  144 
  145 /*
  146  *  This structure will hold context information for the SHA-256
  147  *  hashing operation.
  148  */
  149 typedef struct SHA256Context {
  150     uint32_t Intermediate_Hash[SHA256HashSize/4]; /* Message Digest */
  151 
  152     uint32_t Length_High;               /* Message length in bits */
  153     uint32_t Length_Low;                /* Message length in bits */
  154 
  155     int_least16_t Message_Block_Index;  /* Message_Block array index */
  156                                         /* 512-bit message blocks */
  157     uint8_t Message_Block[SHA256_Message_Block_Size];
  158 
  159     int Computed;                   /* Is the hash computed? */
  160     int Corrupted;                  /* Cumulative corruption code */
  161 } SHA256Context;
  162 
  163 /*
  164  *  This structure will hold context information for the SHA-512
  165  *  hashing operation.
  166  */
  167 typedef struct SHA512Context {
  168 #ifdef USE_32BIT_ONLY
  169     uint32_t Intermediate_Hash[SHA512HashSize/4]; /* Message Digest  */
  170     uint32_t Length[4];                 /* Message length in bits */
  171 #else /* !USE_32BIT_ONLY */
  172     uint64_t Intermediate_Hash[SHA512HashSize/8]; /* Message Digest */
  173     uint64_t Length_High, Length_Low;   /* Message length in bits */
  174 #endif /* USE_32BIT_ONLY */
  175 
  176     int_least16_t Message_Block_Index;  /* Message_Block array index */
  177                                         /* 1024-bit message blocks */
  178     uint8_t Message_Block[SHA512_Message_Block_Size];
  179 
  180     int Computed;                   /* Is the hash computed?*/
  181     int Corrupted;                  /* Cumulative corruption code */
  182 } SHA512Context;
  183 
  184 /*
  185  *  This structure will hold context information for the SHA-224
  186  *  hashing operation.  It uses the SHA-256 structure for computation.
  187  */
  188 typedef struct SHA256Context SHA224Context;
  189 
  190 /*
  191  *  This structure will hold context information for the SHA-384
  192  *  hashing operation.  It uses the SHA-512 structure for computation.
  193  */
  194 typedef struct SHA512Context SHA384Context;
  195 
  196 /*
  197  *  This structure holds context information for all SHA
  198  *  hashing operations.
  199  */
  200 typedef struct USHAContext {
  201     int whichSha;               /* which SHA is being used */
  202     union {
  203       SHA1Context sha1Context;
  204       SHA224Context sha224Context; SHA256Context sha256Context;
  205       SHA384Context sha384Context; SHA512Context sha512Context;
  206     } ctx;
  207 } USHAContext;
  208 
  209 /*
  210  *  This structure will hold context information for the HMAC
  211  *  keyed-hashing operation.
  212  */
  213 typedef struct HMACContext {
  214     int whichSha;               /* which SHA is being used */
  215     int hashSize;               /* hash size of SHA being used */
  216     int blockSize;              /* block size of SHA being used */
  217     USHAContext shaContext;     /* SHA context */
  218     unsigned char k_opad[USHA_Max_Message_Block_Size];
  219                         /* outer padding - key XORd with opad */
  220     int Computed;               /* Is the MAC computed? */
  221     int Corrupted;              /* Cumulative corruption code */
  222 
  223 } HMACContext;
  224 
  225 /*
  226  *  This structure will hold context information for the HKDF
  227  *  extract-and-expand Key Derivation Functions.
  228  */
  229 typedef struct HKDFContext {
  230     int whichSha;               /* which SHA is being used */
  231     HMACContext hmacContext;
  232     int hashSize;               /* hash size of SHA being used */
  233     unsigned char prk[USHAMaxHashSize];
  234                         /* pseudo-random key - output of hkdfInput */
  235     int Computed;               /* Is the key material computed? */
  236     int Corrupted;              /* Cumulative corruption code */
  237 } HKDFContext;
  238 
  239 /*
  240  *  Function Prototypes
  241  */
  242 
  243 /* SHA-1 */
  244 extern int SHA1Reset(SHA1Context *);
  245 extern int SHA1Input(SHA1Context *, const uint8_t *bytes,
  246                      unsigned int bytecount);
  247 extern int SHA1FinalBits(SHA1Context *, uint8_t bits,
  248                          unsigned int bit_count);
  249 extern int SHA1Result(SHA1Context *,
  250                       uint8_t Message_Digest[SHA1HashSize]);
  251 
  252 /* SHA-224 */
  253 extern int SHA224Reset(SHA224Context *);
  254 extern int SHA224Input(SHA224Context *, const uint8_t *bytes,
  255                        unsigned int bytecount);
  256 extern int SHA224FinalBits(SHA224Context *, uint8_t bits,
  257                            unsigned int bit_count);
  258 extern int SHA224Result(SHA224Context *,
  259                         uint8_t Message_Digest[SHA224HashSize]);
  260 
  261 /* SHA-256 */
  262 extern int SHA256Reset(SHA256Context *);
  263 extern int SHA256Input(SHA256Context *, const uint8_t *bytes,
  264                        unsigned int bytecount);
  265 extern int SHA256FinalBits(SHA256Context *, uint8_t bits,
  266                            unsigned int bit_count);
  267 extern int SHA256Result(SHA256Context *,
  268                         uint8_t Message_Digest[SHA256HashSize]);
  269 
  270 /* SHA-384 */
  271 extern int SHA384Reset(SHA384Context *);
  272 extern int SHA384Input(SHA384Context *, const uint8_t *bytes,
  273                        unsigned int bytecount);
  274 extern int SHA384FinalBits(SHA384Context *, uint8_t bits,
  275                            unsigned int bit_count);
  276 extern int SHA384Result(SHA384Context *,
  277                         uint8_t Message_Digest[SHA384HashSize]);
  278 
  279 /* SHA-512 */
  280 extern int SHA512Reset(SHA512Context *);
  281 extern int SHA512Input(SHA512Context *, const uint8_t *bytes,
  282                        unsigned int bytecount);
  283 extern int SHA512FinalBits(SHA512Context *, uint8_t bits,
  284                            unsigned int bit_count);
  285 extern int SHA512Result(SHA512Context *,
  286                         uint8_t Message_Digest[SHA512HashSize]);
  287 
  288 /* Unified SHA functions, chosen by whichSha */
  289 extern int USHAReset(USHAContext *context, SHAversion whichSha);
  290 extern int USHAInput(USHAContext *context,
  291                      const uint8_t *bytes, unsigned int bytecount);
  292 extern int USHAFinalBits(USHAContext *context,
  293                          uint8_t bits, unsigned int bit_count);
  294 extern int USHAResult(USHAContext *context,
  295                       uint8_t Message_Digest[USHAMaxHashSize]);
  296 extern int USHABlockSize(enum SHAversion whichSha);
  297 extern int USHAHashSize(enum SHAversion whichSha);
  298 #if 0 /* not used */
  299 extern int USHAHashSizeBits(enum SHAversion whichSha);
  300 #endif /* 0 */
  301 extern const char *USHAHashName(enum SHAversion whichSha);
  302 
  303 /*
  304  * HMAC Keyed-Hashing for Message Authentication, RFC 2104,
  305  * for all SHAs.
  306  * This interface allows a fixed-length text input to be used.
  307  */
  308 extern int RFC2104Hmac(SHAversion whichSha, /* which SHA algorithm to use */
  309     const unsigned char *text,              /* pointer to data stream */
  310     int text_len,                           /* length of data stream */
  311     const unsigned char *key,               /* pointer to authentication key */
  312     int key_len,                            /* length of authentication key */
  313     uint8_t digest[USHAMaxHashSize]);       /* caller digest to fill in */
  314 
  315 /*
  316  * HMAC Keyed-Hashing for Message Authentication, RFC 2104,
  317  * for all SHAs.
  318  * This interface allows any length of text input to be used.
  319  */
  320 extern int RFC2104HmacReset(HMACContext *context, enum SHAversion whichSha,
  321                             const unsigned char *key, int key_len);
  322 extern int RFC2104HmacInput(HMACContext *context, const unsigned char *text,
  323                             int text_len);
  324 extern int RFC2104HmacFinalBits(HMACContext *context, uint8_t bits,
  325                                 unsigned int bit_count);
  326 extern int RFC2104HmacResult(HMACContext *context,
  327                              uint8_t digest[USHAMaxHashSize]);
  328 
  329 /*
  330  * HKDF HMAC-based Extract-and-Expand Key Derivation Function,
  331  * RFC 5869, for all SHAs.
  332  */
  333 extern int RFC5869Hkdf(SHAversion whichSha, const unsigned char *salt,
  334                        int salt_len, const unsigned char *ikm, int ikm_len,
  335                        const unsigned char *info, int info_len,
  336                        uint8_t okm[ ], int okm_len);
  337 extern int RFC5869HkdfExtract(SHAversion whichSha, const unsigned char *salt,
  338                               int salt_len, const unsigned char *ikm,
  339                               int ikm_len, uint8_t prk[USHAMaxHashSize]);
  340 extern int RFC5869HkdfExpand(SHAversion whichSha, const uint8_t prk[ ],
  341                              int prk_len, const unsigned char *info,
  342                              int info_len, uint8_t okm[ ], int okm_len);
  343 
  344 /*
  345  * HKDF HMAC-based Extract-and-Expand Key Derivation Function,
  346  * RFC 5869, for all SHAs.
  347  * This interface allows any length of text input to be used.
  348  */
  349 extern int RFC5869HkdfReset(HKDFContext *context, enum SHAversion whichSha,
  350                             const unsigned char *salt, int salt_len);
  351 extern int RFC5869HkdfInput(HKDFContext *context, const unsigned char *ikm,
  352                             int ikm_len);
  353 extern int RFC5869HkdfFinalBits(HKDFContext *context, uint8_t ikm_bits,
  354                                 unsigned int ikm_bit_count);
  355 extern int RFC5869HkdfResult(HKDFContext *context,
  356                              uint8_t prk[USHAMaxHashSize],
  357                              const unsigned char *info, int info_len,
  358                              uint8_t okm[USHAMaxHashSize], int okm_len);
  359 
  360 #pragma GCC visibility pop
  361 
  362 #endif /* _SHA_H_ */