sha.c revision 7c478bd95313f5f23a4c958a745db2134aa03244
/*
* Copyright 1999-2002 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
/*
* Cylink Corporation � 1998
*
* This software is licensed by Cylink to the Internet Software Consortium to
* promote implementation of royalty free public key cryptography within IETF
* standards. Cylink wishes to expressly thank the contributions of Dr.
* Martin Hellman, Whitfield Diffie, Ralph Merkle and Stanford University for
* their contributions to Internet Security. In accordance with the terms of
* this license, ISC is authorized to distribute and sublicense this software
* for the practice of IETF standards.
*
* The software includes BigNum, written by Colin Plumb and licensed by Philip
* R. Zimmermann for royalty free use and distribution with Cylink's
* software. Use of BigNum as a stand alone product or component is
* specifically prohibited.
*
* Disclaimer of All Warranties. THIS SOFTWARE IS BEING PROVIDED "AS IS",
* WITHOUT ANY EXPRESSED OR IMPLIED WARRANTY OF ANY KIND WHATSOEVER. IN
* PARTICULAR, WITHOUT LIMITATION ON THE GENERALITY OF THE FOREGOING, CYLINK
* MAKES NO REPRESENTATION OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR
* PURPOSE.
*
* Cylink or its representatives shall not be liable for tort, indirect,
* special or consequential damages such as loss of profits or loss of
* goodwill from the use or inability to use the software for any purpose or
* for any reason whatsoever.
*
* EXPORT LAW: Export of the Foundations Suite may be subject to compliance
* with the rules and regulations promulgated from time to time by the Bureau
* of Export Administration, United States Department of Commerce, which
* restrict the export and re-export of certain products and technical data.
* If the export of the Foundations Suite is controlled under such rules and
* regulations, then the Foundations Suite shall not be exported or
* re-exported, directly or indirectly, (a) without all export or re-export
* licenses and governmental approvals required by any applicable laws, or (b)
* in violation of any applicable prohibition against the export or re-export
* of any part of the Foundations Suite. All export licenses for software
* containing the Foundations Suite are the sole responsibility of the licensee.
*/
/****************************************************************************
* FILENAME: cencrint.c PRODUCT NAME: CRYPTOGRAPHIC TOOLKIT
*
* FILE STATUS:
*
* DESCRIPTION: Cryptographic Toolkit Internal Functions File
*
* PRIVATE FUNCTIONS:
*
*
* void shaTransform( u_int32_t *state, uchar *block )
* void SHAInitK( SHA_context *hash_context )
* int MySHA( uchar *message, u_int16_t message_bytes,
* uchar *hash_result )
* int MySHAFinal( SHA_context *hash_context, uchar *hash_result )
*
*
* Copyright (c) Cylink Corporation 1994. All rights reserved.
*
* REVISION HISTORY:
*
*
* 24 Sep 94 KPZ Initial release
* 10 Oct 94 KPZ Fixed bugs in Add(), DivRem()
* 12 Oct 94 KPZ Modified shaTransform()
* 14 Oct 94 GKL Second version (big endian support)
* 26 Oct 94 GKL (alignment for big endian support & ERR_ALLOC)
* 08 Nov 94 GKL Added input parameters check to Inverse
* 08 Dec 94 GKL Added YIELD_context to Expo, VerPrime and GenPrime
*
****************************************************************************/
#pragma ident "%Z%%M% %I% %E% SMI"
/****************************************************************************
* INCLUDE FILES
****************************************************************************/
#include "port_before.h"
/* system files */
#ifdef VXD
#include <vtoolsc.h>
#else
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#endif
/* program files */
#include "cylink.h"
#include "ctk_endian.h"
#include "toolkit.h"
#include "cencrint.h"
#include "sha.h"
#include "port_after.h"
/****************************************************************************
* NAME: int SHA( uchar *message,
* u_int16_t message_bytes,
* uchar *hash_result )
*
* DESCRIPTION: Compute a Secure Hash Function.
*
* INPUTS:
* PARAMETERS:
* uchar *message Pointer to message
* u_int16_t message_bytes Number of bytes in message
* uchar *hash_result Pointer to message digest
*
* OUTPUT:
* PARAMETERS:
* uchar *hash_result Message digest
*
* RETURN:
* SUCCESS No errors
* ERR_INPUT_LEN Invalid length for input data(zero bytes)
* REVISION HISTORY:
*
* 24 Sep 94 KPZ Initial release
*
****************************************************************************/
uchar *hash_result )
{
if (message_bytes == 0 )
{
return status; /* invalid length for input data */
}
!= SUCCESS )
{
return status; /* error */
}
{
return status; /* error */
}
return status;
}
/****************************************************************************
* PRIVATE FUNCTIONS DEFINITIONS
****************************************************************************/
/****************************************************************************
* NAME: void shaTransform( u_int32_t *state,
* uchar *block )
*
* DESCRIPTION: Perform SHS transformation.
*
* INPUTS:
* PARAMETERS:
* SHA_context *hash_context Pointer to SHA_context structure
* OUTPUT:
*
* SHA_context *hash_context Pointer to SHA_context structure
* (updated)
* REVISION HISTORY:
*
* 24 sep 94 KPZ Initial release
* 12 Oct 94 KPZ Modified buffers copy
* 14 Oct 94 GKL Second version (big endian support)
* 1 Sep 95 AAB Speedup the function
****************************************************************************/
{
u_int32_t W[80];
u_int32_t A,B,C,D,E; /*,temp;*/
#ifdef CTK_LITTLE_ENDIAN /*TKL00201*/
#endif /*TKL00201*/
/* Expand the 16 words into 80 words */
/*Set up first buffer*/
A = state[0];
B = state[1];
C = state[2];
D = state[3];
E = state[4];
/* Heavy mangling, in 4 sub-rounds of 20 iterations each. */
state[0] += A;
state[1] += B;
state[2] += C;
state[3] += D;
state[4] += E;
}
/****************************************************************************
* NAME: void SHAInitK( SHA_context *hash_context )
*
* DESCRIPTION: Initialize Secure Hash Function for generate
* random number for DSS.
*
* INPUTS:
* PARAMETERS:
* SHA_context *hash_context SHA context structure
* OUTPUT:
* PARAMETERS:
* SHA_context *hash_context Initialized SHA context structure
*
* RETURN:
*
* REVISION HISTORY:
*
* 24 Sep 94 KPZ Initial release
* 14 Oct 94 GKL Second version (big endian support)
*
****************************************************************************/
{
/*Set up first buffer*/
/* on28 port: silence compiler warnings by changing 0x...L to 0x...U */
/*Initialise buffer */
}
/****************************************************************************
* NAME: int MySHA( uchar *message,
* u_int16_t message_bytes,
* uchar *hash_result )
*
* DESCRIPTION: Compute a Secure Hash Function.
*
* INPUTS:
* PARAMETERS:
* uchar *message Pointer to message
* u_int16_t message_bytes Number of bytes in message
* uchar *hash_result Pointer to message digest
*
* OUTPUT:
* PARAMETERS:
* uchar *hash_result Message digest
*
* RETURN:
* SUCCESS No errors
* ERR_INPUT_LEN Invalid length for input data(zero bytes)
* REVISION HISTORY:
*
* 24 Sep 94 KPZ Initial release
*
****************************************************************************/
uchar *hash_result )
{
if (message_bytes == 0 )
{
return status; /* invalid length for input data */
}
#ifdef CTK_BIG_ENDIAN
#endif
#ifdef CTK_BIG_ENDIAN
#endif
{
return status; /* error */
}
{
return status; /* error */
}
return status;
}
/****************************************************************************
* NAME: int MySHAFinal( SHA_context *hash_context,
* uchar *hash_result )
* DESCRIPTION: Finalize Secure Hash Function
*
* INPUTS:
* PARAMETERS:
* SHA_context *hash_context SHA context structure
* uchar *hash_result Pointer to hash
* OUTPUT:
* PARAMETERS:
* uchar *hash_result Final value
* RETURN:
* SUCCESS No errors
* ERR_INPUT_LEN Invalid length for input data (zero bytes)
* REVISION HISTORY:
*
* 24 Sep 94 KPZ Initial release
* 10 Oct 94 KPZ Modified for arbitrary message length
* 14 Oct 94 GKL Second version (big endian support)
*
****************************************************************************/
uchar *hash_result )
{
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
};
{
return status;
}
/* Save number of bits */
/* Pad out to 56 mod 64.*/
/* Append length (before padding) */
/* Set order of hash_context */
/* Store state in digest */
/* Zeroize sensitive information.*/
#if defined ( ORD_16 ) && defined( CTK_BIG_ENDIAN )
#endif
return status;
}
/****************************************************************************
* NAME: int SHAUpdate( SHA_context *hash_context,
* uchar *message,
* u_int16_t message_bytes )
* DESCRIPTION: Update Secure Hash Function
*
* INPUTS:
* PARAMETERS:
* SHA_context *hash_context SHA context structure
* uchar *message Pointer to message
* u_int16_t message_bytes Number of bytes
* OUTPUT:
* PARAMETERS:
* SHA_context *hash_context Updated SHA context structure
*
* RETURN:
* SUCCESS No errors
* ERR_INPUT_LEN Invalid length for input data (zero bytes)
* REVISION HISTORY:
*
* 24 Sep 94 KPZ Initial release
* 10 Oct 94 KPZ Modified for arbitrary message length
*
****************************************************************************/
{
if ( message_bytes == 0 )
{
return status;
}
/* Compute number of bytes mod 64 */
/* Update number of bits */
{
}
/* Transform as many times as possible.*/
if ( message_bytes >= partLen )
{
{
}
index = 0;
}
else
{
i = 0;
}
/* Buffer remaining input */
message_bytes - i );
return status;
}
/****************************************************************************
* NAME: void SHAInit( SHA_context *hash_context )
*
* DESCRIPTION: Initialize Secure Hash Function
*
* INPUTS:
* PARAMETERS:
* SHA_context *hash_context SHA context structure
* OUTPUT:
* PARAMETERS:
* SHA_context *hash_context Initialized SHA context structure
*
* RETURN:
*
* REVISION HISTORY:
*
* 24 Sep 94 KPZ Initial release
*
****************************************************************************/
{
/*Set up first buffer*/
/* Initialise buffer */
/*Initialize bit count*/
}
/****************************************************************************
* NAME: int SHAFinal( SHA_context *hash_context,
* uchar *hash_result )
* DESCRIPTION: Finalize Secure Hash Function
*
* INPUTS:
* PARAMETERS:
* SHA_context *hash_context SHA context structure
* uchar *hash_result Pointer to hash
* OUTPUT:
* PARAMETERS:
* uchar *hash_result Final value
* RETURN:
* SUCCESS No errors
* ERR_INPUT_LEN Invalid length for input data (zero bytes)
* REVISION HISTORY:
*
* 24 Sep 94 KPZ Initial release
* 10 Oct 94 KPZ Modified for arbitrary message length
* 14 Oct 94 GKL Second version (big endian support)
*
****************************************************************************/
uchar *hash_result )
{
#ifdef CTK_BIG_ENDIAN
{
}
#endif
if (DataOrder)
{
}
return status;
}
/****************************************************************************
* NAME: int GetPasswordKeySHA( u_int16_t Password_bytes,
* uchar *Password,
* uchar *salt,
* u_int16_t Count,
* uchar *K,
* uchar *IV )
*
*
* INPUTS:
* PARAMETERS:
* u_int16_t Password_bytes Number of bytes in password
* uchar *Password Pointer to password
* uchar *salt Pointer to salt(8-byte)
* u_int16_t Count Number of iteration
* OUTPUT:
* PARAMETERS:
* uchar *IV Pointer to initialization vector
* RETURN:
* SUCCESS No errors
* ERR_COUNT Invalid iteration count (zero)
* ERR_INPUT_LEN Invalid length for input data(zero bytes)
* ERR_ALLOC Insufficient memory
* REVISION HISTORY:
*
* 24 Sep 94 KPZ Initial release
* 26 Oct 94 GKL (ERR_ALLOC)
*
****************************************************************************/
uchar *K,
{
if ( Count == 0 ) /* invalid iteration count (zero) */
{
return status;
}
{
return status; /* ERR_ALLOC insufficient memory */
}
if ( Password_bytes != 0 ) /* if number of bytes password non equals zero */
{
}
/* Compute message digest */
if (!DataOrder)
{
}
{
return status;
}
Count --; /* decrement Count */
/* Count times compute message digest */
while ( Count != 0 )
{
{
return status;
}
if (!DataOrder)
{
}
Count --;
}
return status;
}