/home/dko/Projects/mobilec/src/security/xyssl-0.9/library/sha1.c File Reference

#include "xyssl/config.h"
#include "xyssl/sha1.h"
#include <string.h>
#include <stdio.h>

Go to the source code of this file.

Defines

#define GET_ULONG_BE(n, b, i)
#define PUT_ULONG_BE(n, b, i)
#define S(x, n)   ((x << n) | ((x & 0xFFFFFFFF) >> (32 - n)))
#define R(t)
#define P(a, b, c, d, e, x)
#define F(x, y, z)   (z ^ (x & (y ^ z)))
#define K   0x5A827999
#define F(x, y, z)   (x ^ y ^ z)
#define K   0x6ED9EBA1
#define F(x, y, z)   ((x & y) | (z & (x | y)))
#define K   0x8F1BBCDC
#define F(x, y, z)   (x ^ y ^ z)
#define K   0xCA62C1D6

Functions

void sha1_starts (sha1_context *ctx)
 SHA-1 context setup.
static void sha1_process (sha1_context *ctx, unsigned char data[64])
void sha1_update (sha1_context *ctx, unsigned char *input, int ilen)
 SHA-1 process buffer.
void sha1_finish (sha1_context *ctx, unsigned char output[20])
 SHA-1 final digest.
void sha1 (unsigned char *input, int ilen, unsigned char output[20])
 Output = SHA-1( input buffer ).
int sha1_file (char *path, unsigned char output[20])
 Output = SHA-1( file contents ).
void sha1_hmac_starts (sha1_context *ctx, unsigned char *key, int keylen)
 SHA-1 HMAC context setup.
void sha1_hmac_update (sha1_context *ctx, unsigned char *input, int ilen)
 SHA-1 HMAC process buffer.
void sha1_hmac_finish (sha1_context *ctx, unsigned char output[20])
 SHA-1 HMAC final digest.
void sha1_hmac (unsigned char *key, int keylen, unsigned char *input, int ilen, unsigned char output[20])
 Output = HMAC-SHA-1( hmac key, input buffer ).
int sha1_self_test (int verbose)
 Checkup routine.

Variables

static const unsigned char sha1_padding [64]
static unsigned char sha1_test_buf [3][57]
static const int sha1_test_buflen [3]
static const unsigned char sha1_test_sum [3][20]
static unsigned char sha1_hmac_test_key [7][26]
static const int sha1_hmac_test_keylen [7]
static unsigned char sha1_hmac_test_buf [7][74]
static const int sha1_hmac_test_buflen [7]
static const unsigned char sha1_hmac_test_sum [7][20]

Define Documentation

#define F ( x,
y,
 )     (x ^ y ^ z)
#define F ( x,
y,
 )     ((x & y) | (z & (x | y)))
#define F ( x,
y,
 )     (x ^ y ^ z)
#define F ( x,
y,
 )     (z ^ (x & (y ^ z)))
#define GET_ULONG_BE ( n,
b,
 ) 
Value:
{                                                       \
    (n) = ( (unsigned long) (b)[(i)    ] << 24 )        \
        | ( (unsigned long) (b)[(i) + 1] << 16 )        \
        | ( (unsigned long) (b)[(i) + 2] <<  8 )        \
        | ( (unsigned long) (b)[(i) + 3]       );       \
}

Definition at line 39 of file sha1.c.

#define K   0xCA62C1D6
#define K   0x8F1BBCDC
#define K   0x6ED9EBA1
#define K   0x5A827999

Referenced by sha4_process().

#define P ( a,
b,
c,
d,
e,
 ) 
Value:
{                                                       \
    e += S(a,5) + F(b,c,d) + K + x; b = S(b,30);        \
}
#define PUT_ULONG_BE ( n,
b,
 ) 
Value:
{                                                       \
    (b)[(i)    ] = (unsigned char) ( (n) >> 24 );       \
    (b)[(i) + 1] = (unsigned char) ( (n) >> 16 );       \
    (b)[(i) + 2] = (unsigned char) ( (n) >>  8 );       \
    (b)[(i) + 3] = (unsigned char) ( (n)       );       \
}

Definition at line 49 of file sha1.c.

#define R (  ) 
Value:
(                                                       \
    temp = W[(t -  3) & 0x0F] ^ W[(t - 8) & 0x0F] ^     \
           W[(t - 14) & 0x0F] ^ W[ t      & 0x0F],      \
    ( W[t & 0x0F] = S(temp,1) )                         \
)

Referenced by mpi_is_prime(), sha1_process(), and sha2_process().

#define S ( x,
 )     ((x << n) | ((x & 0xFFFFFFFF) >> (32 - n)))

Function Documentation

void sha1 ( unsigned char *  input,
int  ilen,
unsigned char  output[20] 
)

Output = SHA-1( input buffer ).

Parameters:
input buffer holding the data
ilen length of the input data
output SHA-1 checksum result

Definition at line 313 of file sha1.c.

References sha1_finish(), sha1_starts(), and sha1_update().

Referenced by main(), rsa_self_test(), sha1_hmac_starts(), ssl_calc_verify(), ssl_derive_keys(), ssl_mac_sha1(), ssl_parse_finished(), ssl_parse_server_key_exchange(), ssl_write_finished(), ssl_write_server_key_exchange(), and x509_hash().

int sha1_file ( char *  path,
unsigned char  output[20] 
)

Output = SHA-1( file contents ).

Parameters:
path input file name
output SHA-1 checksum result
Returns:
0 if successful, 1 if fopen failed, or 2 if fread failed

Definition at line 327 of file sha1.c.

References buf, f, sha1_finish(), sha1_starts(), and sha1_update().

Referenced by main(), and sha1_wrapper().

void sha1_finish ( sha1_context ctx,
unsigned char  output[20] 
)
void sha1_hmac ( unsigned char *  key,
int  keylen,
unsigned char *  input,
int  ilen,
unsigned char  output[20] 
)

Output = HMAC-SHA-1( hmac key, input buffer ).

Parameters:
key HMAC secret key
keylen length of the HMAC key
input buffer holding the data
ilen length of the input data
output HMAC-SHA-1 result

Definition at line 413 of file sha1.c.

References sha1_hmac_finish(), sha1_hmac_starts(), and sha1_hmac_update().

Referenced by ssl_decrypt_buf(), ssl_encrypt_buf(), and tls1_prf().

void sha1_hmac_finish ( sha1_context ctx,
unsigned char  output[20] 
)

SHA-1 HMAC final digest.

Parameters:
ctx HMAC context
output SHA-1 HMAC checksum result

Definition at line 397 of file sha1.c.

References sha1_context::opad, sha1_finish(), sha1_starts(), and sha1_update().

Referenced by sha1_hmac(), and sha1_self_test().

void sha1_hmac_starts ( sha1_context ctx,
unsigned char *  key,
int  keylen 
)

SHA-1 HMAC context setup.

Parameters:
ctx HMAC context to be initialized
key HMAC secret key
keylen length of the HMAC key

Definition at line 359 of file sha1.c.

References sha1_context::ipad, sha1_context::opad, sha1(), sha1_starts(), and sha1_update().

Referenced by sha1_hmac(), and sha1_self_test().

void sha1_hmac_update ( sha1_context ctx,
unsigned char *  input,
int  ilen 
)

SHA-1 HMAC process buffer.

Parameters:
ctx HMAC context
input buffer holding the data
ilen length of the input data

Definition at line 389 of file sha1.c.

References sha1_update().

Referenced by sha1_hmac(), and sha1_self_test().

static void sha1_process ( sha1_context ctx,
unsigned char  data[64] 
) [static]

Definition at line 73 of file sha1.c.

References GET_ULONG_BE, P, R, and sha1_context::state.

Referenced by sha1_update().

int sha1_self_test ( int  verbose  ) 
void sha1_starts ( sha1_context ctx  ) 
void sha1_update ( sha1_context ctx,
unsigned char *  input,
int  ilen 
)

Variable Documentation

unsigned char sha1_hmac_test_buf[7][74] [static]
Initial value:
{
    { "Hi There" },
    { "what do ya want for nothing?" },
    { "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
      "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
      "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
      "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
      "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD" },
    { "\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD"
      "\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD"
      "\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD"
      "\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD"
      "\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD" },
    { "Test With Truncation" },
    { "Test Using Larger Than Block-Size Key - Hash Key First" },
    { "Test Using Larger Than Block-Size Key and Larger"
      " Than One Block-Size Data" }
}

Definition at line 475 of file sha1.c.

Referenced by sha1_self_test().

const int sha1_hmac_test_buflen[7] [static]
Initial value:
{
    8, 28, 50, 50, 20, 54, 73
}

Definition at line 495 of file sha1.c.

Referenced by sha1_self_test().

unsigned char sha1_hmac_test_key[7][26] [static]
Initial value:
{
    { "\x0B\x0B\x0B\x0B\x0B\x0B\x0B\x0B\x0B\x0B\x0B\x0B\x0B\x0B\x0B\x0B"
      "\x0B\x0B\x0B\x0B" },
    { "Jefe" },
    { "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
      "\xAA\xAA\xAA\xAA" },
    { "\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0A\x0B\x0C\x0D\x0E\x0F\x10"
      "\x11\x12\x13\x14\x15\x16\x17\x18\x19" },
    { "\x0C\x0C\x0C\x0C\x0C\x0C\x0C\x0C\x0C\x0C\x0C\x0C\x0C\x0C\x0C\x0C"
      "\x0C\x0C\x0C\x0C" },
    { "" }, 
    { "" }
}

Definition at line 455 of file sha1.c.

Referenced by sha1_self_test().

const int sha1_hmac_test_keylen[7] [static]
Initial value:
{
    20, 4, 20, 25, 20, 80, 80
}

Definition at line 470 of file sha1.c.

Referenced by sha1_self_test().

const unsigned char sha1_hmac_test_sum[7][20] [static]
Initial value:
{
    { 0xB6, 0x17, 0x31, 0x86, 0x55, 0x05, 0x72, 0x64, 0xE2, 0x8B,
      0xC0, 0xB6, 0xFB, 0x37, 0x8C, 0x8E, 0xF1, 0x46, 0xBE, 0x00 },
    { 0xEF, 0xFC, 0xDF, 0x6A, 0xE5, 0xEB, 0x2F, 0xA2, 0xD2, 0x74,
      0x16, 0xD5, 0xF1, 0x84, 0xDF, 0x9C, 0x25, 0x9A, 0x7C, 0x79 },
    { 0x12, 0x5D, 0x73, 0x42, 0xB9, 0xAC, 0x11, 0xCD, 0x91, 0xA3,
      0x9A, 0xF4, 0x8A, 0xA1, 0x7B, 0x4F, 0x63, 0xF1, 0x75, 0xD3 },
    { 0x4C, 0x90, 0x07, 0xF4, 0x02, 0x62, 0x50, 0xC6, 0xBC, 0x84,
      0x14, 0xF9, 0xBF, 0x50, 0xC8, 0x6C, 0x2D, 0x72, 0x35, 0xDA },
    { 0x4C, 0x1A, 0x03, 0x42, 0x4B, 0x55, 0xE0, 0x7F, 0xE7, 0xF2,
      0x7B, 0xE1 },
    { 0xAA, 0x4A, 0xE5, 0xE1, 0x52, 0x72, 0xD0, 0x0E, 0x95, 0x70,
      0x56, 0x37, 0xCE, 0x8A, 0x3B, 0x55, 0xED, 0x40, 0x21, 0x12 },
    { 0xE8, 0xE9, 0x9D, 0x0F, 0x45, 0x23, 0x7D, 0x78, 0x6D, 0x6B,
      0xBA, 0xA7, 0x96, 0x5C, 0x78, 0x08, 0xBB, 0xFF, 0x1A, 0x91 }
}

Definition at line 500 of file sha1.c.

Referenced by sha1_self_test().

const unsigned char sha1_padding[64] [static]
Initial value:
{
 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
}

Definition at line 273 of file sha1.c.

Referenced by sha1_finish().

unsigned char sha1_test_buf[3][57] [static]
Initial value:
 
{
    { "abc" },
    { "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq" },
    { "" }
}

Definition at line 430 of file sha1.c.

Referenced by sha1_self_test().

const int sha1_test_buflen[3] [static]
Initial value:
{
    3, 56, 1000
}

Definition at line 437 of file sha1.c.

Referenced by sha1_self_test().

const unsigned char sha1_test_sum[3][20] [static]
Initial value:
{
    { 0xA9, 0x99, 0x3E, 0x36, 0x47, 0x06, 0x81, 0x6A, 0xBA, 0x3E,
      0x25, 0x71, 0x78, 0x50, 0xC2, 0x6C, 0x9C, 0xD0, 0xD8, 0x9D },
    { 0x84, 0x98, 0x3E, 0x44, 0x1C, 0x3B, 0xD2, 0x6E, 0xBA, 0xAE,
      0x4A, 0xA1, 0xF9, 0x51, 0x29, 0xE5, 0xE5, 0x46, 0x70, 0xF1 },
    { 0x34, 0xAA, 0x97, 0x3C, 0xD4, 0xC4, 0xDA, 0xA4, 0xF6, 0x1E,
      0xEB, 0x2B, 0xDB, 0xAD, 0x27, 0x31, 0x65, 0x34, 0x01, 0x6F }
}

Definition at line 442 of file sha1.c.

Referenced by sha1_self_test().

Generated on Tue Dec 28 15:15:29 2010 for Mobile-C by  doxygen 1.6.3