Thu Apr 28 2011 17:15:25

Asterisk developer's documentation


sha1.c

Go to the documentation of this file.
00001 /*! \file
00002  *
00003  * \brief Based on the RFC 3174
00004  * 
00005  * Full Copyright Statement
00006  *
00007  *  Copyright (C) The Internet Society (2001).  All Rights Reserved.
00008  *
00009  *  This document and translations of it may be copied and furnished to
00010  *  others, and derivative works that comment on or otherwise explain it
00011  *  or assist in its implementation may be prepared, copied, published
00012  *  and distributed, in whole or in part, without restriction of any
00013  *  kind, provided that the above copyright notice and this paragraph are
00014  *  included on all such copies and derivative works.  However, this
00015  *  document itself may not be modified in any way, such as by removing
00016  *  the copyright notice or references to the Internet Society or other
00017  *  Internet organizations, except as needed for the purpose of
00018  *  developing Internet standards in which case the procedures for
00019  *  copyrights defined in the Internet Standards process must be
00020  *  followed, or as required to translate it into languages other than
00021  *  English.
00022  *
00023  *  The limited permissions granted above are perpetual and will not be
00024  *  revoked by the Internet Society or its successors or assigns.
00025 
00026  *  This document and the information contained herein is provided on an
00027  *  "AS IS" basis and THE INTERNET SOCIETY AND THE INTERNET ENGINEERING
00028  *  TASK FORCE DISCLAIMS ALL WARRANTIES, EXPRESS OR IMPLIED, INCLUDING
00029  *  BUT NOT LIMITED TO ANY WARRANTY THAT THE USE OF THE INFORMATION
00030  *  HEREIN WILL NOT INFRINGE ANY RIGHTS OR ANY IMPLIED WARRANTIES OF
00031  *  MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
00032  *
00033  *
00034  * 
00035  *  Description:
00036  *   This file implements the Secure Hashing Algorithm 1 as
00037  *   defined in FIPS PUB 180-1 published April 17, 1995.
00038  *
00039  *   The SHA-1, produces a 160-bit message digest for a given
00040  *   data stream.  It should take about 2**n steps to find a
00041  *   message with the same digest as a given message and
00042  *   2**(n/2) to find any two messages with the same digest,
00043  *   when n is the digest size in bits.  Therefore, this
00044  *   algorithm can serve as a means of providing a
00045  *   "fingerprint" for a message.
00046  *
00047  *  Portability Issues:
00048  *   SHA-1 is defined in terms of 32-bit "words".  This code
00049  *   uses <stdint.h> (included via "sha1.h" to define 32 and 8
00050  *   bit unsigned integer types.  If your C compiler does not
00051  *   support 32 bit unsigned integers, this code is not
00052  *   appropriate.
00053  *
00054  *  Caveats:
00055  *   SHA-1 is designed to work with messages less than 2^64 bits
00056  *   long.  Although SHA-1 allows a message digest to be generated
00057  *   for messages of any number of bits less than 2^64, this
00058  *   implementation only works with messages with a length that is
00059  *   a multiple of the size of an 8-bit character.
00060  *
00061  */
00062 
00063 #include "asterisk.h"
00064 #include "asterisk/sha1.h"
00065 
00066 /*! Define the SHA1 circular left shift macro */
00067 #define SHA1CircularShift(bits,word) \
00068                (((word) << (bits)) | ((word) >> (32-(bits))))
00069 
00070 /* Local Function Prototyptes */
00071 void SHA1PadMessage(SHA1Context *);
00072 void SHA1ProcessMessageBlock(SHA1Context *);
00073 
00074 /*!
00075  * \brief SHA1Reset
00076  * \param context the context to be reset.
00077  * This function will initialize the SHA1Context in preparation
00078  * for computing a new SHA1 message digest.
00079  * \return sha Error Code.
00080  */
00081 int SHA1Reset(SHA1Context *context)
00082 {
00083    if (!context) {
00084       return shaNull;
00085    }
00086 
00087    context->Length_Low             = 0;
00088    context->Length_High            = 0;
00089    context->Message_Block_Index    = 0;
00090 
00091    context->Intermediate_Hash[0]   = 0x67452301;
00092    context->Intermediate_Hash[1]   = 0xEFCDAB89;
00093    context->Intermediate_Hash[2]   = 0x98BADCFE;
00094    context->Intermediate_Hash[3]   = 0x10325476;
00095    context->Intermediate_Hash[4]   = 0xC3D2E1F0;
00096 
00097    context->Computed               = 0;
00098    context->Corrupted              = 0;
00099 
00100    return shaSuccess;
00101 }
00102 
00103 /*!
00104  * \brief SHA1Result
00105  * \param context [in/out] The context to use to calculate the SHA-1 hash.
00106  * \param Message_Digest [out] Where the digest is returned.
00107  *  This function will return the 160-bit message digest into the
00108  *  Message_Digest array  provided by the caller.
00109  * \note The first octet of hash is stored in the 0th element, 
00110  *    the last octet of hash in the 19th element.
00111  * \return sha Error Code.
00112  */
00113 int SHA1Result( SHA1Context *context,
00114                uint8_t Message_Digest[SHA1HashSize])
00115 {
00116    int i;
00117 
00118    if (!context || !Message_Digest) {
00119       return shaNull;
00120    }
00121 
00122    if (context->Corrupted) {
00123       return context->Corrupted;
00124    }
00125 
00126    if (!context->Computed) {
00127       SHA1PadMessage(context);
00128       for (i = 0; i < 64; ++i) {
00129          /* message may be sensitive, clear it out */
00130          context->Message_Block[i] = 0;
00131       }
00132       context->Length_Low = 0;    /* and clear length */
00133       context->Length_High = 0;
00134       context->Computed = 1;
00135    }
00136 
00137    for (i = 0; i < SHA1HashSize; ++i) {
00138       Message_Digest[i] = context->Intermediate_Hash[i >> 2] >> 8 * ( 3 - ( i & 0x03 ) );
00139    }
00140 
00141    return shaSuccess;
00142 }
00143 
00144 /*!
00145  *  \brief SHA1Input
00146  * \param context [in/out] The SHA context to update
00147  * \param message_array [in] An array of characters representing the next portion of
00148  *       the message.
00149  * \param length [in] The length of the message in message_array.
00150  *  This function accepts an array of octets as the next portion
00151  *  of the message.
00152  * \return sha Error Code.
00153  */
00154 int SHA1Input(SHA1Context *context, const uint8_t *message_array, unsigned length)
00155 {
00156    if (!length) {
00157       return shaSuccess;
00158    }
00159 
00160    if (!context || !message_array) {
00161       return shaNull;
00162    }
00163 
00164    if (context->Computed) {
00165       context->Corrupted = shaStateError;
00166       return shaStateError;
00167    }
00168 
00169    if (context->Corrupted) {
00170       return context->Corrupted;
00171    }
00172 
00173    while (length-- && !context->Corrupted) {
00174       context->Message_Block[context->Message_Block_Index++] = (*message_array & 0xFF);
00175 
00176       context->Length_Low += 8;
00177       if (context->Length_Low == 0) {
00178          context->Length_High++;
00179          if (context->Length_High == 0) {
00180             /* Message is too long */
00181             context->Corrupted = 1;
00182          }
00183       }
00184 
00185       if (context->Message_Block_Index == 64) {
00186          SHA1ProcessMessageBlock(context);
00187       }
00188 
00189       message_array++;
00190    }
00191 
00192    return shaSuccess;
00193 }
00194 
00195 /*!
00196  * \brief Process the next 512 bits of the message stored in the Message_Block array.
00197  * \param context [in/out] The SHA context to update
00198  * \note  Many of the variable names in this code, especially the
00199  *   single character names, were used because those were the
00200  *   names used in the publication.
00201  * \returns nothing.
00202  */
00203 void SHA1ProcessMessageBlock(SHA1Context *context)
00204 {
00205    const uint32_t K[] =     {     /* Constants defined in SHA-1  */
00206                             0x5A827999,
00207                             0x6ED9EBA1,
00208                             0x8F1BBCDC,
00209                             0xCA62C1D6
00210                             };
00211    int           t;                 /* Loop counter                */
00212    uint32_t    temp;              /* Temporary word value        */
00213    uint32_t    W[80];             /* Word sequence               */
00214    uint32_t    A, B, C, D, E;     /* Word buffers                */
00215 
00216    /*
00217     *  Initialize the first 16 words in the array W
00218     */
00219    for (t = 0; t < 16; t++) {
00220       W[t] = context->Message_Block[t * 4] << 24;
00221       W[t] |= context->Message_Block[t * 4 + 1] << 16;
00222       W[t] |= context->Message_Block[t * 4 + 2] << 8;
00223       W[t] |= context->Message_Block[t * 4 + 3];
00224    }
00225 
00226    for (t = 16; t < 80; t++) {
00227       W[t] = SHA1CircularShift(1,W[t-3] ^ W[t-8] ^ W[t-14] ^ W[t-16]);
00228    }
00229 
00230    A = context->Intermediate_Hash[0];
00231    B = context->Intermediate_Hash[1];
00232    C = context->Intermediate_Hash[2];
00233    D = context->Intermediate_Hash[3];
00234    E = context->Intermediate_Hash[4];
00235 
00236    for (t = 0; t < 20; t++) {
00237       temp = SHA1CircularShift(5,A) + ((B & C) | ((~B) & D)) + E + W[t] + K[0];
00238       E = D;
00239       D = C;
00240       C = SHA1CircularShift(30,B);
00241       B = A;
00242       A = temp;
00243    }
00244 
00245    for (t = 20; t < 40; t++) {
00246       temp = SHA1CircularShift(5,A) + (B ^ C ^ D) + E + W[t] + K[1];
00247       E = D;
00248       D = C;
00249       C = SHA1CircularShift(30,B);
00250       B = A;
00251       A = temp;
00252    }
00253 
00254    for (t = 40; t < 60; t++) {
00255       temp = SHA1CircularShift(5,A) + ((B & C) | (B & D) | (C & D)) + E + W[t] + K[2];
00256       E = D;
00257       D = C;
00258       C = SHA1CircularShift(30,B);
00259       B = A;
00260       A = temp;
00261    }
00262 
00263    for (t = 60; t < 80; t++) {
00264       temp = SHA1CircularShift(5,A) + (B ^ C ^ D) + E + W[t] + K[3];
00265       E = D;
00266       D = C;
00267       C = SHA1CircularShift(30,B);
00268       B = A;
00269       A = temp;
00270    }
00271 
00272    context->Intermediate_Hash[0] += A;
00273    context->Intermediate_Hash[1] += B;
00274    context->Intermediate_Hash[2] += C;
00275    context->Intermediate_Hash[3] += D;
00276    context->Intermediate_Hash[4] += E;
00277 
00278    context->Message_Block_Index = 0;
00279 }
00280 
00281 
00282 /*!
00283  * \brief Pad message to be 512 bits.
00284  * \param context [in/out]  The context to pad.
00285  * 
00286  * According to the standard, the message must be padded to an even
00287  *  512 bits.  The first padding bit must be a '1'.  The last 64
00288  *  bits represent the length of the original message.  All bits in
00289  *  between should be 0.  This function will pad the message
00290  *  according to those rules by filling the Message_Block array
00291  *  accordingly.  It will also call the ProcessMessageBlock function
00292  *  provided appropriately.  When it returns, it can be assumed that
00293  *  the message digest has been computed.
00294  *
00295  * \returns nothing.
00296  */
00297 
00298 void SHA1PadMessage(SHA1Context *context)
00299 {
00300    /*
00301     *  Check to see if the current message block is too small to hold
00302     *  the initial padding bits and length.  If so, we will pad the
00303     *  block, process it, and then continue padding into a second
00304     *  block.
00305     */
00306    if (context->Message_Block_Index > 55) {
00307       context->Message_Block[context->Message_Block_Index++] = 0x80;
00308       while (context->Message_Block_Index < 64) {
00309          context->Message_Block[context->Message_Block_Index++] = 0;
00310       }
00311 
00312       SHA1ProcessMessageBlock(context);
00313 
00314       while (context->Message_Block_Index < 56) {
00315          context->Message_Block[context->Message_Block_Index++] = 0;
00316       }
00317    } else {
00318       context->Message_Block[context->Message_Block_Index++] = 0x80;
00319       while (context->Message_Block_Index < 56) {
00320          context->Message_Block[context->Message_Block_Index++] = 0;
00321       }
00322    }
00323 
00324    /*
00325     *  Store the message length as the last 8 octets
00326     */
00327    context->Message_Block[56] = context->Length_High >> 24;
00328    context->Message_Block[57] = context->Length_High >> 16;
00329    context->Message_Block[58] = context->Length_High >> 8;
00330    context->Message_Block[59] = context->Length_High;
00331    context->Message_Block[60] = context->Length_Low >> 24;
00332    context->Message_Block[61] = context->Length_Low >> 16;
00333    context->Message_Block[62] = context->Length_Low >> 8;
00334    context->Message_Block[63] = context->Length_Low;
00335 
00336    SHA1ProcessMessageBlock(context);
00337 }