Thu Apr 28 2011 16:57:18

Asterisk developer's documentation


sha1.h File Reference

This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  SHA1Context
 This structure will hold context information for the SHA-1 hashing operation. More...

Defines

#define _SHA_enum_
#define SHA1HashSize   20

Typedefs

typedef struct SHA1Context SHA1Context
 This structure will hold context information for the SHA-1 hashing operation.

Enumerations

enum  { shaSuccess = 0, shaNull, shaInputTooLong, shaStateError }

Functions

int SHA1Input (SHA1Context *, const uint8_t *, unsigned int)
 SHA1Input.
int SHA1Reset (SHA1Context *)
 SHA1Reset.
int SHA1Result (SHA1Context *, uint8_t Message_Digest[SHA1HashSize])
 SHA1Result.

Define Documentation

#define _SHA_enum_

Definition at line 32 of file sha1.h.

#define SHA1HashSize   20

Definition at line 41 of file sha1.h.

Referenced by SHA1Result().


Typedef Documentation

typedef struct SHA1Context SHA1Context

This structure will hold context information for the SHA-1 hashing operation.


Enumeration Type Documentation

anonymous enum
Enumerator:
shaSuccess 
shaNull 
shaInputTooLong 
shaStateError 

Definition at line 33 of file sha1.h.

{
    shaSuccess = 0,
    shaNull,            /* Null pointer parameter */
    shaInputTooLong,    /* input data too long */
    shaStateError       /* called Input after Result */
};

Function Documentation

int SHA1Input ( SHA1Context context,
const uint8_t *  message_array,
unsigned  length 
)

SHA1Input.

Parameters:
context[in/out] The SHA context to update
message_array[in] An array of characters representing the next portion of the message.
length[in] The length of the message in message_array. This function accepts an array of octets as the next portion of the message.
Returns:
sha Error Code.

Definition at line 154 of file sha1.c.

References SHA1Context::Computed, SHA1Context::Corrupted, SHA1Context::Length_High, SHA1Context::Length_Low, SHA1Context::Message_Block, SHA1Context::Message_Block_Index, SHA1ProcessMessageBlock(), shaNull, shaStateError, and shaSuccess.

Referenced by ast_sha1_hash().

{
   if (!length) {
      return shaSuccess;
   }

   if (!context || !message_array) {
      return shaNull;
   }

   if (context->Computed) {
      context->Corrupted = shaStateError;
      return shaStateError;
   }

   if (context->Corrupted) {
      return context->Corrupted;
   }

   while (length-- && !context->Corrupted) {
      context->Message_Block[context->Message_Block_Index++] = (*message_array & 0xFF);

      context->Length_Low += 8;
      if (context->Length_Low == 0) {
         context->Length_High++;
         if (context->Length_High == 0) {
            /* Message is too long */
            context->Corrupted = 1;
         }
      }

      if (context->Message_Block_Index == 64) {
         SHA1ProcessMessageBlock(context);
      }

      message_array++;
   }

   return shaSuccess;
}
int SHA1Reset ( SHA1Context context)

SHA1Reset.

Parameters:
contextthe context to be reset. This function will initialize the SHA1Context in preparation for computing a new SHA1 message digest.
Returns:
sha Error Code.

Definition at line 81 of file sha1.c.

References SHA1Context::Computed, SHA1Context::Corrupted, SHA1Context::Intermediate_Hash, SHA1Context::Length_High, SHA1Context::Length_Low, SHA1Context::Message_Block_Index, shaNull, and shaSuccess.

Referenced by ast_sha1_hash().

{
   if (!context) {
      return shaNull;
   }

   context->Length_Low             = 0;
   context->Length_High            = 0;
   context->Message_Block_Index    = 0;

   context->Intermediate_Hash[0]   = 0x67452301;
   context->Intermediate_Hash[1]   = 0xEFCDAB89;
   context->Intermediate_Hash[2]   = 0x98BADCFE;
   context->Intermediate_Hash[3]   = 0x10325476;
   context->Intermediate_Hash[4]   = 0xC3D2E1F0;

   context->Computed               = 0;
   context->Corrupted              = 0;

   return shaSuccess;
}
int SHA1Result ( SHA1Context context,
uint8_t  Message_Digest[SHA1HashSize] 
)

SHA1Result.

Parameters:
context[in/out] The context to use to calculate the SHA-1 hash.
Message_Digest[out] Where the digest is returned. This function will return the 160-bit message digest into the Message_Digest array provided by the caller.
Note:
The first octet of hash is stored in the 0th element, the last octet of hash in the 19th element.
Returns:
sha Error Code.

Definition at line 113 of file sha1.c.

References SHA1Context::Computed, SHA1Context::Corrupted, SHA1Context::Intermediate_Hash, SHA1Context::Length_High, SHA1Context::Length_Low, SHA1Context::Message_Block, SHA1HashSize, SHA1PadMessage(), shaNull, and shaSuccess.

Referenced by ast_sha1_hash().

{
   int i;

   if (!context || !Message_Digest) {
      return shaNull;
   }

   if (context->Corrupted) {
      return context->Corrupted;
   }

   if (!context->Computed) {
      SHA1PadMessage(context);
      for (i = 0; i < 64; ++i) {
         /* message may be sensitive, clear it out */
         context->Message_Block[i] = 0;
      }
      context->Length_Low = 0;    /* and clear length */
      context->Length_High = 0;
      context->Computed = 1;
   }

   for (i = 0; i < SHA1HashSize; ++i) {
      Message_Digest[i] = context->Intermediate_Hash[i >> 2] >> 8 * ( 3 - ( i & 0x03 ) );
   }

   return shaSuccess;
}