Mark Radbourne / Mbed 2 deprecated iothub_client_sample_amqp

Dependencies:   EthernetInterface NTPClient iothub_amqp_transport iothub_client mbed-rtos mbed

Fork of iothub_client_sample_amqp by Azure IoT

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers sha.h Source File

sha.h

00001 // Copyright (c) Microsoft. All rights reserved.
00002 // Licensed under the MIT license. See LICENSE file in the project root for full license information.
00003 
00004 /**************************** sha.h ****************************/
00005 /******************* See RFC 4634 for details ******************/
00006 #ifndef _SHA_H_
00007 #define _SHA_H_
00008 
00009 /*
00010  *  Description:
00011  *      This file implements the Secure Hash Signature Standard
00012  *      algorithms as defined in the National Institute of Standards
00013  *      and Technology Federal Information Processing Standards
00014  *      Publication (FIPS PUB) 180-1 published on April 17, 1995, 180-2
00015  *      published on August 1, 2002, and the FIPS PUB 180-2 Change
00016  *      Notice published on February 28, 2004.
00017  *
00018  *      A combined document showing all algorithms is available at
00019  *              http://csrc.nist.gov/publications/fips/
00020  *              fips180-2/fips180-2withchangenotice.pdf
00021  *
00022  *      The five hashes are defined in these sizes:
00023  *              SHA-1           20 byte / 160 bit
00024  *              SHA-224         28 byte / 224 bit
00025  *              SHA-256         32 byte / 256 bit
00026  *              SHA-384         48 byte / 384 bit
00027  *              SHA-512         64 byte / 512 bit
00028  */
00029 
00030 #include <stdint.h>
00031 /*
00032  * If you do not have the ISO standard stdint.h header file, then you
00033  * must typedef the following:
00034  *    name              meaning
00035  *  uint64_t         unsigned 64 bit integer
00036  *  uint32_t         unsigned 32 bit integer
00037  *  uint8_t          unsigned 8 bit integer (i.e., unsigned char)
00038  *  int_least16_t    integer of >= 16 bits
00039  *
00040  */
00041 
00042 #ifndef _SHA_enum_
00043 #define _SHA_enum_
00044 /*
00045  *  All SHA functions return one of these values.
00046  */
00047 enum {
00048     shaSuccess = 0,
00049     shaNull,            /* Null pointer parameter */
00050     shaInputTooLong,    /* input data too long */
00051     shaStateError,      /* called Input after FinalBits or Result */
00052     shaBadParam         /* passed a bad parameter */
00053 };
00054 #endif /* _SHA_enum_ */
00055 
00056 /*
00057  *  These constants hold size information for each of the SHA
00058  *  hashing operations
00059  */
00060 enum {
00061     SHA1_Message_Block_Size = 64, SHA224_Message_Block_Size = 64,
00062     SHA256_Message_Block_Size = 64, SHA384_Message_Block_Size = 128,
00063     SHA512_Message_Block_Size = 128,
00064     USHA_Max_Message_Block_Size = SHA512_Message_Block_Size,
00065 
00066     SHA1HashSize = 20, SHA224HashSize = 28, SHA256HashSize = 32,
00067     SHA384HashSize = 48, SHA512HashSize = 64,
00068     USHAMaxHashSize = SHA512HashSize,
00069 
00070     SHA1HashSizeBits = 160, SHA224HashSizeBits = 224,
00071     SHA256HashSizeBits = 256, SHA384HashSizeBits = 384,
00072     SHA512HashSizeBits = 512, USHAMaxHashSizeBits = SHA512HashSizeBits
00073 };
00074 
00075 /*
00076  *  These constants are used in the USHA (unified sha) functions.
00077  */
00078 typedef enum SHAversion {
00079     SHA1, SHA224, SHA256, SHA384, SHA512
00080 } SHAversion;
00081 
00082 /*
00083  *  This structure will hold context information for the SHA-1
00084  *  hashing operation.
00085  */
00086 typedef struct SHA1Context {
00087     uint32_t Intermediate_Hash[SHA1HashSize/4]; /* Message Digest */
00088 
00089     uint32_t Length_Low;                /* Message length in bits */
00090     uint32_t Length_High;               /* Message length in bits */
00091 
00092     int_least16_t Message_Block_Index;  /* Message_Block array index */
00093                                         /* 512-bit message blocks */
00094     uint8_t Message_Block[SHA1_Message_Block_Size];
00095 
00096     int Computed;                       /* Is the digest computed? */
00097     int Corrupted;                      /* Is the digest corrupted? */
00098 } SHA1Context;
00099 
00100 /*
00101  *  This structure will hold context information for the SHA-256
00102  *  hashing operation.
00103  */
00104 typedef struct SHA256Context {
00105     uint32_t Intermediate_Hash[SHA256HashSize/4]; /* Message Digest */
00106 
00107     uint32_t Length_Low;                /* Message length in bits */
00108     uint32_t Length_High;               /* Message length in bits */
00109 
00110     int_least16_t Message_Block_Index;  /* Message_Block array index */
00111                                         /* 512-bit message blocks */
00112     uint8_t Message_Block[SHA256_Message_Block_Size];
00113 
00114     int Computed;                       /* Is the digest computed? */
00115     int Corrupted;                      /* Is the digest corrupted? */
00116 } SHA256Context;
00117 
00118 /*
00119  *  This structure will hold context information for the SHA-512
00120  *  hashing operation.
00121  */
00122 typedef struct SHA512Context {
00123 #ifdef USE_32BIT_ONLY
00124     uint32_t Intermediate_Hash[SHA512HashSize/4]; /* Message Digest  */
00125     uint32_t Length[4];                 /* Message length in bits */
00126 #else /* !USE_32BIT_ONLY */
00127     uint64_t Intermediate_Hash[SHA512HashSize/8]; /* Message Digest */
00128     uint64_t Length_Low, Length_High;   /* Message length in bits */
00129 #endif /* USE_32BIT_ONLY */
00130 
00131     int_least16_t Message_Block_Index;  /* Message_Block array index */
00132                                         /* 1024-bit message blocks */
00133     uint8_t Message_Block[SHA512_Message_Block_Size];
00134 
00135     int Computed;                       /* Is the digest computed?*/
00136     int Corrupted;                      /* Is the digest corrupted? */
00137 } SHA512Context;
00138 
00139 /*
00140  *  This structure will hold context information for the SHA-224
00141  *  hashing operation. It uses the SHA-256 structure for computation.
00142  */
00143 typedef struct SHA256Context SHA224Context;
00144 
00145 /*
00146  *  This structure will hold context information for the SHA-384
00147  *  hashing operation. It uses the SHA-512 structure for computation.
00148  */
00149 typedef struct SHA512Context SHA384Context;
00150 
00151 /*
00152  *  This structure holds context information for all SHA
00153  *  hashing operations.
00154  */
00155 typedef struct USHAContext {
00156     int whichSha;               /* which SHA is being used */
00157     union {
00158       SHA1Context sha1Context;
00159       SHA224Context sha224Context; SHA256Context sha256Context;
00160       SHA384Context sha384Context; SHA512Context sha512Context;
00161     } ctx;
00162 } USHAContext;
00163 
00164 /*
00165  *  This structure will hold context information for the HMAC
00166  *  keyed hashing operation.
00167  */
00168 typedef struct HMACContext {
00169     int whichSha;               /* which SHA is being used */
00170     int hashSize;               /* hash size of SHA being used */
00171     int blockSize;              /* block size of SHA being used */
00172     USHAContext shaContext;     /* SHA context */
00173     unsigned char k_opad[USHA_Max_Message_Block_Size];
00174                         /* outer padding - key XORd with opad */
00175 } HMACContext;
00176 
00177 
00178 /*
00179  *  Function Prototypes
00180  */
00181 
00182 /* SHA-1 */
00183 extern int SHA1Reset(SHA1Context *);
00184 extern int SHA1Input(SHA1Context *, const uint8_t *bytes,
00185                      unsigned int bytecount);
00186 extern int SHA1FinalBits(SHA1Context *, const uint8_t bits,
00187                          unsigned int bitcount);
00188 extern int SHA1Result(SHA1Context *,
00189                       uint8_t Message_Digest[SHA1HashSize]);
00190 
00191 /* SHA-224 */
00192 extern int SHA224Reset(SHA224Context *);
00193 extern int SHA224Input(SHA224Context *, const uint8_t *bytes,
00194                        unsigned int bytecount);
00195 extern int SHA224FinalBits(SHA224Context *, const uint8_t bits,
00196                            unsigned int bitcount);
00197 extern int SHA224Result(SHA224Context *,
00198                         uint8_t Message_Digest[SHA224HashSize]);
00199 
00200 /* SHA-256 */
00201 extern int SHA256Reset(SHA256Context *);
00202 extern int SHA256Input(SHA256Context *, const uint8_t *bytes,
00203                        unsigned int bytecount);
00204 extern int SHA256FinalBits(SHA256Context *, const uint8_t bits,
00205                            unsigned int bitcount);
00206 extern int SHA256Result(SHA256Context *,
00207                         uint8_t Message_Digest[SHA256HashSize]);
00208 
00209 /* SHA-384 */
00210 extern int SHA384Reset(SHA384Context *);
00211 extern int SHA384Input(SHA384Context *, const uint8_t *bytes,
00212                        unsigned int bytecount);
00213 extern int SHA384FinalBits(SHA384Context *, const uint8_t bits,
00214                            unsigned int bitcount);
00215 extern int SHA384Result(SHA384Context *,
00216                         uint8_t Message_Digest[SHA384HashSize]);
00217 
00218 /* SHA-512 */
00219 extern int SHA512Reset(SHA512Context *);
00220 extern int SHA512Input(SHA512Context *, const uint8_t *bytes,
00221                        unsigned int bytecount);
00222 extern int SHA512FinalBits(SHA512Context *, const uint8_t bits,
00223                            unsigned int bitcount);
00224 extern int SHA512Result(SHA512Context *,
00225                         uint8_t Message_Digest[SHA512HashSize]);
00226 
00227 /* Unified SHA functions, chosen by whichSha */
00228 extern int USHAReset(USHAContext *, SHAversion whichSha);
00229 extern int USHAInput(USHAContext *,
00230                      const uint8_t *bytes, unsigned int bytecount);
00231 extern int USHAFinalBits(USHAContext *,
00232                          const uint8_t bits, unsigned int bitcount);
00233 extern int USHAResult(USHAContext *,
00234                       uint8_t Message_Digest[USHAMaxHashSize]);
00235 extern int USHABlockSize(enum SHAversion whichSha);
00236 extern int USHAHashSize(enum SHAversion whichSha);
00237 extern int USHAHashSizeBits(enum SHAversion whichSha);
00238 
00239 /*
00240  * HMAC Keyed-Hashing for Message Authentication, RFC2104,
00241  * for all SHAs.
00242  * This interface allows a fixed-length text input to be used.
00243  */
00244 extern int hmac(SHAversion whichSha, /* which SHA algorithm to use */
00245     const unsigned char *text,     /* pointer to data stream */
00246     int text_len,                  /* length of data stream */
00247     const unsigned char *key,      /* pointer to authentication key */
00248     int key_len,                   /* length of authentication key */
00249     uint8_t digest[USHAMaxHashSize]); /* caller digest to fill in */
00250 
00251 /*
00252  * HMAC Keyed-Hashing for Message Authentication, RFC2104,
00253  * for all SHAs.
00254  * This interface allows any length of text input to be used.
00255  */
00256 extern int hmacReset(HMACContext *ctx, enum SHAversion whichSha,
00257                      const unsigned char *key, int key_len);
00258 extern int hmacInput(HMACContext *ctx, const unsigned char *text,
00259                      int text_len);
00260 
00261 extern int hmacFinalBits(HMACContext *ctx, const uint8_t bits,
00262                          unsigned int bitcount);
00263 extern int hmacResult(HMACContext *ctx,
00264                       uint8_t digest[USHAMaxHashSize]);
00265 
00266 #endif /* _SHA_H_ */
00267