Main Page | Modules | Alphabetical List | Data Structures | File List | Data Fields | Globals | Related Pages

sha1.c File Reference


Detailed Description

Secure Hashing Algorithm 1 implementation.

Description: This file implements the Secure Hashing Algorithm 1 as defined in FIPS PUB 180-1 published April 17, 1995.

The SHA-1, produces a 160-bit message digest for a given data stream. It should take about 2**n steps to find a message with the same digest as a given message and 2**(n/2) to find any two messages with the same digest, when n is the digest size in bits. Therefore, this algorithm can serve as a means of providing a "fingerprint" for a message.

Portability Issues: SHA-1 is defined in terms of 32-bit "words". This code uses <stdint.h> (included via "sha1.h" to define 32 and 8 bit unsigned integer types. If your C compiler does not support 32 bit unsigned integers, this code is not appropriate.

Caveats: SHA-1 is designed to work with messages less than 2^64 bits long. Although SHA-1 allows a message digest to be generated for messages of any number of bits less than 2^64, this implementation only works with messages with a length that is a multiple of the size of an 8-bit character.

Note:
This file comes from RFC 3174. Inclusion in gtk-gnutella is:
Author:
Raphael Manfredi
Date:
2002-2003

#include "common.h"
#include "sha1.h"
#include "misc.h"
#include "override.h"

Defines

#define SHA1CircularShift(bits, word)   (((word) << (bits)) | ((word) >> (32-(bits))))
 Define the SHA1 circular left shift macro.

#define INIT(x)
#define CRUNCH   *wp = SHA1CircularShift(1,wp[-3] ^ wp[-8] ^ wp[-14] ^ wp[-16])
#define ROTATE(k, mix)

Functions

 RCSID ("$Id:sha1.c, v 1.6 2005/06/29 14:24:27 daichik Exp $")
void SHA1PadMessage (SHA1Context *)
 SHA1PadMessage.

void SHA1ProcessMessageBlock (SHA1Context *)
 SHA1ProcessMessageBlock.

int SHA1Reset (SHA1Context *context)
 SHA1Reset.

int SHA1Result (SHA1Context *context, guint8 Message_Digest[SHA1HashSize])
 SHA1Result.

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


Define Documentation

#define CRUNCH   *wp = SHA1CircularShift(1,wp[-3] ^ wp[-8] ^ wp[-14] ^ wp[-16])
 

#define INIT  ) 
 

Value:

W[x] = (context->Message_Block[(x) * 4] << 24) | \
            (context->Message_Block[(x) * 4 + 1] << 16) | \
            (context->Message_Block[(x) * 4 +2] << 8) | \
            (context->Message_Block[(x) * 4 +3])

#define ROTATE k,
mix   ) 
 

Value:

temp = SHA1CircularShift(5,A) + (mix) + E + *wp++ + K[k]; \
    E = D; D = C; \
    C = SHA1CircularShift(30,B); \
    B = A; A = temp

#define SHA1CircularShift bits,
word   )     (((word) << (bits)) | ((word) >> (32-(bits))))
 

Define the SHA1 circular left shift macro.


Function Documentation

RCSID "$Id:sha1.  c,
v 1.6 2005/06/29 14:24:27 daichik Exp $" 
 

int SHA1Input SHA1Context context,
const guint8 *  message_array,
unsigned  length
 

SHA1Input.

Description: This function accepts an array of octets as the next portion of the message.

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

Returns: sha Error Code.

void SHA1PadMessage SHA1Context context  ) 
 

SHA1PadMessage.

Description: According to the standard, the message must be padded to an even 512 bits. The first padding bit must be a '1'. The last 64 bits represent the length of the original message. All bits in between should be 0. This function will pad the message according to those rules by filling the Message_Block array accordingly. It will also call the ProcessMessageBlock function provided appropriately. When it returns, it can be assumed that the message digest has been computed.

Parameters: context: [in/out] The context to pad ProcessMessageBlock: [in] The appropriate SHA*ProcessMessageBlock function Returns: Nothing.

void SHA1ProcessMessageBlock SHA1Context context  ) 
 

SHA1ProcessMessageBlock.

Description: This function will process the next 512 bits of the message stored in the Message_Block array.

Parameters: None.

Returns: Nothing.

Comments: Many of the variable names in this code, especially the single character names, were used because those were the names used in the publication.

int SHA1Reset SHA1Context context  ) 
 

SHA1Reset.

Description: This function will initialize the SHA1Context in preparation for computing a new SHA1 message digest.

Parameters: context: [in/out] The context to reset.

Returns: sha Error Code.

int SHA1Result SHA1Context context,
guint8  Message_Digest[SHA1HashSize]
 

SHA1Result.

Description: 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.

Parameters: context: [in/out] The context to use to calculate the SHA-1 hash. Message_Digest: [out] Where the digest is returned.

Returns: sha Error Code.


Generated on Sun Feb 12 10:50:08 2006 for Gtk-Gnutella by doxygen 1.3.6