sha512.c File Reference

#include "polarssl/config.h"
#include "polarssl/sha512.h"
#include <stdio.h>
Include dependency graph for sha512.c:

Go to the source code of this file.

Defines

#define GET_UINT64_BE(n, b, i)
#define PUT_UINT64_BE(n, b, i)
#define SHR(x, n)   (x >> n)
#define ROTR(x, n)   (SHR(x,n) | (x << (64 - n)))
#define S0(x)   (ROTR(x, 1) ^ ROTR(x, 8) ^ SHR(x, 7))
#define S1(x)   (ROTR(x,19) ^ ROTR(x,61) ^ SHR(x, 6))
#define S2(x)   (ROTR(x,28) ^ ROTR(x,34) ^ ROTR(x,39))
#define S3(x)   (ROTR(x,14) ^ ROTR(x,18) ^ ROTR(x,41))
#define F0(x, y, z)   ((x & y) | (z & (x | y)))
#define F1(x, y, z)   (z ^ (x & (y ^ z)))
#define P(a, b, c, d, e, f, g, h, x, K)

Functions

void sha512_starts (sha512_context *ctx, int is384)
 SHA-512 context setup.
void sha512_process (sha512_context *ctx, const unsigned char data[128])
void sha512_update (sha512_context *ctx, const unsigned char *input, size_t ilen)
 SHA-512 process buffer.
void sha512_finish (sha512_context *ctx, unsigned char output[64])
 SHA-512 final digest.
void sha512 (const unsigned char *input, size_t ilen, unsigned char output[64], int is384)
 Output = SHA-512( input buffer ).
int sha512_file (const char *path, unsigned char output[64], int is384)
 Output = SHA-512( file contents ).
void sha512_hmac_starts (sha512_context *ctx, const unsigned char *key, size_t keylen, int is384)
 SHA-512 HMAC context setup.
void sha512_hmac_update (sha512_context *ctx, const unsigned char *input, size_t ilen)
 SHA-512 HMAC process buffer.
void sha512_hmac_finish (sha512_context *ctx, unsigned char output[64])
 SHA-512 HMAC final digest.
void sha512_hmac_reset (sha512_context *ctx)
 SHA-512 HMAC context reset.
void sha512_hmac (const unsigned char *key, size_t keylen, const unsigned char *input, size_t ilen, unsigned char output[64], int is384)
 Output = HMAC-SHA-512( hmac key, input buffer ).
int sha512_self_test (int verbose)
 Checkup routine.

Variables

static const uint64_t K [80]
static const unsigned char sha512_padding [128]
static unsigned char sha512_test_buf [3][113]
static const int sha512_test_buflen [3]
static const unsigned char sha512_test_sum [6][64]
static unsigned char sha512_hmac_test_key [7][26]
static const int sha512_hmac_test_keylen [7]
static unsigned char sha512_hmac_test_buf [7][153]
static const int sha512_hmac_test_buflen [7]
static const unsigned char sha512_hmac_test_sum [14][64]

Define Documentation

#define F0 ( x,
y,
 )     ((x & y) | (z & (x | y)))
#define F1 ( x,
y,
 )     (z ^ (x & (y ^ z)))
#define GET_UINT64_BE ( n,
b,
 ) 
Value:
{                                                       \
    (n) = ( (uint64_t) (b)[(i)    ] << 56 )       \
        | ( (uint64_t) (b)[(i) + 1] << 48 )       \
        | ( (uint64_t) (b)[(i) + 2] << 40 )       \
        | ( (uint64_t) (b)[(i) + 3] << 32 )       \
        | ( (uint64_t) (b)[(i) + 4] << 24 )       \
        | ( (uint64_t) (b)[(i) + 5] << 16 )       \
        | ( (uint64_t) (b)[(i) + 6] <<  8 )       \
        | ( (uint64_t) (b)[(i) + 7]       );      \
}

Definition at line 47 of file sha512.c.

Referenced by sha512_process().

#define P ( a,
b,
c,
d,
e,
f,
g,
h,
x,
K   ) 
Value:
{                                               \
    temp1 = h + S3(e) + F1(e,f,g) + K + x;      \
    temp2 = S2(a) + F0(a,b,c);                  \
    d += temp1; h = temp1 + temp2;              \
}
#define PUT_UINT64_BE ( n,
b,
 ) 
Value:
{                                                       \
    (b)[(i)    ] = (unsigned char) ( (n) >> 56 );       \
    (b)[(i) + 1] = (unsigned char) ( (n) >> 48 );       \
    (b)[(i) + 2] = (unsigned char) ( (n) >> 40 );       \
    (b)[(i) + 3] = (unsigned char) ( (n) >> 32 );       \
    (b)[(i) + 4] = (unsigned char) ( (n) >> 24 );       \
    (b)[(i) + 5] = (unsigned char) ( (n) >> 16 );       \
    (b)[(i) + 6] = (unsigned char) ( (n) >>  8 );       \
    (b)[(i) + 7] = (unsigned char) ( (n)       );       \
}

Definition at line 61 of file sha512.c.

Referenced by sha512_finish().

#define ROTR ( x,
 )     (SHR(x,n) | (x << (64 - n)))
#define S0 (  )     (ROTR(x, 1) ^ ROTR(x, 8) ^ SHR(x, 7))
#define S1 (  )     (ROTR(x,19) ^ ROTR(x,61) ^ SHR(x, 6))
#define S2 (  )     (ROTR(x,28) ^ ROTR(x,34) ^ ROTR(x,39))
#define S3 (  )     (ROTR(x,14) ^ ROTR(x,18) ^ ROTR(x,41))
#define SHR ( x,
 )     (x >> n)

Function Documentation

void sha512 ( const unsigned char *  input,
size_t  ilen,
unsigned char  output[64],
int  is384 
)

Output = SHA-512( input buffer ).

Parameters:
input buffer holding the data
ilen length of the input data
output SHA-384/512 checksum result
is384 0 = use SHA512, 1 = use SHA384

Definition at line 318 of file sha512.c.

References sha512_finish(), sha512_starts(), and sha512_update().

Referenced by entropy_func(), entropy_update(), sha384_wrap(), sha512_hmac_starts(), sha512_wrap(), ssl_calc_finished_tls_sha384(), ssl_calc_verify_tls_sha384(), test_suite_sha384(), and test_suite_sha512().

int sha512_file ( const char *  path,
unsigned char  output[64],
int  is384 
)

Output = SHA-512( file contents ).

Parameters:
path input file name
output SHA-384/512 checksum result
is384 0 = use SHA512, 1 = use SHA384
Returns:
0 if successful, or POLARSSL_ERR_SHA512_FILE_IO_ERROR

Definition at line 334 of file sha512.c.

References POLARSSL_ERR_SHA512_FILE_IO_ERROR, sha512_finish(), sha512_starts(), and sha512_update().

Referenced by sha384_file_wrap(), sha512_file_wrap(), test_suite_sha384_file(), and test_suite_sha512_file().

void sha512_finish ( sha512_context ctx,
unsigned char  output[64] 
)
void sha512_hmac ( const unsigned char *  key,
size_t  keylen,
const unsigned char *  input,
size_t  ilen,
unsigned char  output[64],
int  is384 
)

Output = HMAC-SHA-512( 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-384/512 result
is384 0 = use SHA512, 1 = use SHA384

Definition at line 436 of file sha512.c.

References sha512_hmac_finish(), sha512_hmac_starts(), and sha512_hmac_update().

Referenced by sha384_hmac_wrap(), sha512_hmac_wrap(), test_suite_sha384_hmac(), test_suite_sha512_hmac(), and tls_prf_sha384().

void sha512_hmac_finish ( sha512_context ctx,
unsigned char  output[64] 
)

SHA-512 HMAC final digest.

Parameters:
ctx HMAC context
output SHA-384/512 HMAC checksum result

Definition at line 407 of file sha512.c.

References sha512_context::is384, sha512_context::opad, sha512_finish(), sha512_starts(), and sha512_update().

Referenced by sha384_hmac_finish_wrap(), sha512_hmac(), sha512_hmac_finish_wrap(), and sha512_self_test().

void sha512_hmac_reset ( sha512_context ctx  ) 

SHA-512 HMAC context reset.

Parameters:
ctx HMAC context to be reset

Definition at line 427 of file sha512.c.

References sha512_context::ipad, sha512_context::is384, sha512_starts(), and sha512_update().

Referenced by sha384_hmac_reset_wrap(), and sha512_hmac_reset_wrap().

void sha512_hmac_starts ( sha512_context ctx,
const unsigned char *  key,
size_t  keylen,
int  is384 
)

SHA-512 HMAC context setup.

Parameters:
ctx HMAC context to be initialized
is384 0 = use SHA512, 1 = use SHA384
key HMAC secret key
keylen length of the HMAC key

Definition at line 367 of file sha512.c.

References sha512_context::ipad, sha512_context::opad, sha512(), sha512_starts(), and sha512_update().

Referenced by sha384_hmac_starts_wrap(), sha512_hmac(), sha512_hmac_starts_wrap(), and sha512_self_test().

void sha512_hmac_update ( sha512_context ctx,
const unsigned char *  input,
size_t  ilen 
)

SHA-512 HMAC process buffer.

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

Definition at line 398 of file sha512.c.

References sha512_update().

Referenced by sha384_hmac_update_wrap(), sha512_hmac(), sha512_hmac_update_wrap(), and sha512_self_test().

void sha512_process ( sha512_context ctx,
const unsigned char  data[128] 
)

Definition at line 157 of file sha512.c.

References A, F, GET_UINT64_BE, K, P, S0, S1, and sha512_context::state.

Referenced by sha384_process_wrap(), sha512_process_wrap(), and sha512_update().

int sha512_self_test ( int  verbose  ) 
void sha512_starts ( sha512_context ctx,
int  is384 
)

SHA-512 context setup.

Parameters:
ctx context to be initialized
is384 0 = use SHA512, 1 = use SHA384

Definition at line 124 of file sha512.c.

References sha512_context::is384, sha512_context::state, sha512_context::total, and UL64.

Referenced by entropy_func(), entropy_init(), sha384_starts_wrap(), sha512(), sha512_file(), sha512_hmac_finish(), sha512_hmac_reset(), sha512_hmac_starts(), sha512_self_test(), sha512_starts_wrap(), and ssl_handshake_init().

void sha512_update ( sha512_context ctx,
const unsigned char *  input,
size_t  ilen 
)

Variable Documentation

const uint64_t K[80] [static]

Definition at line 77 of file sha512.c.

unsigned char sha512_hmac_test_buf[7][153] [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" },
    { "This is a test using a larger than block-size key "
      "and a larger than block-size data. The key needs to "
      "be hashed before being used by the HMAC algorithm." }
}

Definition at line 543 of file sha512.c.

Referenced by sha512_self_test().

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

Definition at line 564 of file sha512.c.

Referenced by sha512_self_test().

unsigned char sha512_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 523 of file sha512.c.

Referenced by sha512_self_test().

const int sha512_hmac_test_keylen[7] [static]
Initial value:
{
    20, 4, 20, 25, 20, 131, 131
}

Definition at line 538 of file sha512.c.

Referenced by sha512_self_test().

const unsigned char sha512_hmac_test_sum[14][64] [static]

Definition at line 569 of file sha512.c.

Referenced by sha512_self_test().

const unsigned char sha512_padding[128] [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,
    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, 0
}

Definition at line 265 of file sha512.c.

Referenced by sha512_finish().

unsigned char sha512_test_buf[3][113] [static]
Initial value:
 
{
    { "abc" },
    { "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmn"
      "hijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu" },
    { "" }
}

Definition at line 454 of file sha512.c.

Referenced by sha512_self_test().

const int sha512_test_buflen[3] [static]
Initial value:
{
    3, 112, 1000
}

Definition at line 462 of file sha512.c.

Referenced by sha512_self_test().

const unsigned char sha512_test_sum[6][64] [static]

Definition at line 467 of file sha512.c.

Referenced by sha512_self_test().


Generated on 9 Apr 2014 for PolarSSL v1.3.2 by  doxygen 1.6.1