Example #1
0
File: sha1.c Project: ireader/sdk
/*
* SHA1PadMessage
*
* Description:
*	According to the standard, the message must be padded to the next
*	even multiple of 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 helper function will pad
*	the message according to those rules by filling the Message_Block
*	array accordingly. When it returns, it can be assumed that the
*	message digest has been computed.
*
* Parameters:
*	context: [in/out]
*		The context to pad.
*	Pad_Byte: [in]
*		The last byte to add to the message block before the 0-padding
*		and length. This will contain the last bits of the message
*		followed by another single bit. If the message was an
*	exact multiple of 8-bits long, Pad_Byte will be 0x80.
*
* Returns:
*	Nothing.
*/
static void SHA1PadMessage(SHA1Context *context, uint8_t Pad_Byte)
{
	/*
	* Check to see if the current message block is too small to hold
	* the initial padding bits and length. If so, we will pad the
	* block, process it, and then continue padding into a second
	* block.
	*/
	if (context->Message_Block_Index >= (SHA1_Message_Block_Size - 8)) {
		context->Message_Block[context->Message_Block_Index++] = Pad_Byte;
		while (context->Message_Block_Index < SHA1_Message_Block_Size)
			context->Message_Block[context->Message_Block_Index++] = 0;
		SHA1ProcessMessageBlock(context);
	}
	else
		context->Message_Block[context->Message_Block_Index++] = Pad_Byte;

	while (context->Message_Block_Index < (SHA1_Message_Block_Size - 8))
		context->Message_Block[context->Message_Block_Index++] = 0;

	/*
	* Store the message length as the last 8 octets
	*/
	context->Message_Block[56] = (uint8_t)(context->Length_High >> 24);
	context->Message_Block[57] = (uint8_t)(context->Length_High >> 16);
	context->Message_Block[58] = (uint8_t)(context->Length_High >> 8);
	context->Message_Block[59] = (uint8_t)(context->Length_High);
	context->Message_Block[60] = (uint8_t)(context->Length_Low >> 24);
	context->Message_Block[61] = (uint8_t)(context->Length_Low >> 16);
	context->Message_Block[62] = (uint8_t)(context->Length_Low >> 8);
	context->Message_Block[63] = (uint8_t)(context->Length_Low);
	SHA1ProcessMessageBlock(context);
}
Example #2
0
void SHA1PadMessage(SHA1Context *context)
{
    /*
     *  Check to see if the current message block is too small to hold
     *  the initial padding bits and length.  If so, we will pad the
     *  block, process it, and then continue padding into a second
     *  block.
     */
    if (context->Message_Block_Index > 55)
    {
        context->Message_Block[context->Message_Block_Index++] = 0x80;
        while(context->Message_Block_Index < 64)
        {
            context->Message_Block[context->Message_Block_Index++] = 0;
        }

        SHA1ProcessMessageBlock(context);

        while(context->Message_Block_Index < 56)
        {
            context->Message_Block[context->Message_Block_Index++] = 0;
        }
    }
    else
    {
        context->Message_Block[context->Message_Block_Index++] = 0x80;
        while(context->Message_Block_Index < 56)
        {

            context->Message_Block[context->Message_Block_Index++] = 0;
        }
    }

    /*
     *  Store the message length as the last 8 octets
     */
    context->Message_Block[56] = 0;
    context->Message_Block[57] = 0;
    context->Message_Block[58] = 0;
    context->Message_Block[59] = 0;
/*
    context->Message_Block[60] = (UCHAR)(context->Length_Low >> 24);
    context->Message_Block[61] = (UCHAR)(context->Length_Low >> 16);
    context->Message_Block[62] = (UCHAR)(context->Length_Low >> 8);
    context->Message_Block[63] = (UCHAR)(context->Length_Low);
*/
/*
    context->Message_Block[60] = (UCHAR)(rr_32x8(context->Length_Low, 24));
    context->Message_Block[61] = (UCHAR)(rr_32x8(context->Length_Low, 16));
    context->Message_Block[62] = (UCHAR)(rr_32x8(context->Length_Low, 8));
    context->Message_Block[63] = (UCHAR)(context->Length_Low);
*/
	ULONG2PCHAR(context->Length_Low, (PCHAR)(context->Message_Block + 60));
    SHA1ProcessMessageBlock(context);
}
Example #3
0
File: main.c Project: xwaynec/eplab
void SHA1PadMessage(SHA1Context idata *context)
{
    /*
     *  Check to see if the current message block is too small to hold
     *  the initial padding bits and length.  If so, we will pad the
     *  block, process it, and then continue padding into a second
     *  block.
     */
    if (context->Message_Block_Index > 55)
    {
        context->Message_Block[context->Message_Block_Index++] = 0x80;
        while(context->Message_Block_Index < 64)
        {
            context->Message_Block[context->Message_Block_Index++] = 0;
        }

        SHA1ProcessMessageBlock(context);

        while(context->Message_Block_Index < 56)
        {
            context->Message_Block[context->Message_Block_Index++] = 0;
        }
    }
    else
    {
        context->Message_Block[context->Message_Block_Index++] = 0x80;
        while(context->Message_Block_Index < 56)
        {
            context->Message_Block[context->Message_Block_Index++] = 0;
        }
    }

    /*
     *  Store the message length as the last 8 octets
     */
    context->Message_Block[24] = (context->Length_High >> 24) & 0xFF;
    context->Message_Block[25] = (context->Length_High >> 16) & 0xFF;
    context->Message_Block[26] = (context->Length_High >> 8) & 0xFF;
    context->Message_Block[27] = (context->Length_High) & 0xFF;
    context->Message_Block[28] = (context->Length_Low >> 24) & 0xFF;
    context->Message_Block[29] = (context->Length_Low >> 16) & 0xFF;
    context->Message_Block[30] = (context->Length_Low >> 8) & 0xFF;
    context->Message_Block[31] = (context->Length_Low) & 0xFF;

    SHA1ProcessMessageBlock(context);

	//blink_led();
}
Example #4
0
/*
 *  SHA1Input
 *
 *  Description:
 *      This function accepts an array of octets as the next portion of
 *      the message.
 *
 *  Parameters:
 *      context: [in/out]
 *          The SHA-1 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:
 *      Nothing.
 *
 *  Comments:
 *
 */
void sha1_hash(const unsigned char *data, unsigned int length, sha1_ctx *ctx) {
    if (!length) {
        return;
    }

    if (ctx->Computed || ctx->Corrupted) {
        ctx->Corrupted = 1;
        return;
    }

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

        ctx->Length_Low += 8;
        /* Force it to 32 bits */
        ctx->Length_Low &= 0xFFFFFFFF;
        if (ctx->Length_Low == 0) {
            ctx->Length_High++;
            /* Force it to 32 bits */
            ctx->Length_High &= 0xFFFFFFFF;
            if (ctx->Length_High == 0) {
                /* Message is too long */
                ctx->Corrupted = 1;
            }
        }

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

        data++;
    }
}
Example #5
0
void SHA1PadMessage(SHA1Context *context)
{
    /*
     *  Check to see if the current message block is too small to hold
     *  the initial padding bits and length.  If so, we will pad the
     *  block, process it, and then continue padding into a second
     *  block.
     */
    if (context->Message_Block_Index > 55)
    {
        context->Message_Block[context->Message_Block_Index++] = 0x80;
        while(context->Message_Block_Index < 64)
        {
            context->Message_Block[context->Message_Block_Index++] = 0;
        }

        SHA1ProcessMessageBlock(context);

        while(context->Message_Block_Index < 56)
        {
            context->Message_Block[context->Message_Block_Index++] = 0;
        }
    }
    else
    {
        context->Message_Block[context->Message_Block_Index++] = 0x80;
        while(context->Message_Block_Index < 56)
        {

            context->Message_Block[context->Message_Block_Index++] = 0;
        }
    }

    /*
     *  Store the message length as the last 8 octets
     */
    context->Message_Block[56] = context->Length_High >> 24;
    context->Message_Block[57] = context->Length_High >> 16;
    context->Message_Block[58] = context->Length_High >> 8;
    context->Message_Block[59] = context->Length_High;
    context->Message_Block[60] = context->Length_Low >> 24;
    context->Message_Block[61] = context->Length_Low >> 16;
    context->Message_Block[62] = context->Length_Low >> 8;
    context->Message_Block[63] = context->Length_Low;

    SHA1ProcessMessageBlock(context);
}
Example #6
0
void SHA1PadMessage(SHA1Context *context) {
	context->Message_Block[context->Message_Block_Index++] = 0x80;
	if (context->Message_Block_Index > 56) {
		//was 55, one shift
		while(context->Message_Block_Index < 64)
			context->Message_Block[context->Message_Block_Index++] = 0;
		SHA1ProcessMessageBlock(context);
		while(context->Message_Block_Index < 56)
			context->Message_Block[context->Message_Block_Index++] = 0;
	} else
		while(context->Message_Block_Index < 56)
			context->Message_Block[context->Message_Block_Index++] = 0;
	context->Message_Block[56] = (context->Length_High >> 24) & 0xFF;
	context->Message_Block[57] = (context->Length_High >> 16) & 0xFF;
	context->Message_Block[58] = (context->Length_High >> 8) & 0xFF;
	context->Message_Block[59] = (context->Length_High) & 0xFF;
	context->Message_Block[60] = (context->Length_Low >> 24) & 0xFF;
	context->Message_Block[61] = (context->Length_Low >> 16) & 0xFF;
	context->Message_Block[62] = (context->Length_Low >> 8) & 0xFF;
	context->Message_Block[63] = (context->Length_Low) & 0xFF;
	SHA1ProcessMessageBlock(context);
}
Example #7
0
/*
 *  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.
 *
 */
int SHA1Input(    SHA1Context    *context,
                  const uint8_t  *message_array,
                  unsigned       length)
{
    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;
}
Example #8
0
/*
 *  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 SHA1Input(SHA1Context *context, PCHAR message_array, UCHAR length)
{
	while(length--)
	{
		context->Message_Block[context->Message_Block_Index++] = *message_array;
		context->Length_Low += 8;

		if (context->Message_Block_Index == 64)
		{
			SHA1ProcessMessageBlock(context);
		}
		message_array++;
	}
}
Example #9
0
void SHA1PadMessage(SHA1Context *context)
{
	if (context->Message_Block_Index > 55)
	{
		context->Message_Block[context->Message_Block_Index++] = 0x80;
		while(context->Message_Block_Index < 64)
		{
			context->Message_Block[context->Message_Block_Index++] = 0;
		}

		SHA1ProcessMessageBlock(context);

		while(context->Message_Block_Index < 56)
		{
			context->Message_Block[context->Message_Block_Index++] = 0;
		}
	}
	else
	{
		context->Message_Block[context->Message_Block_Index++] = 0x80;
		while(context->Message_Block_Index < 56)
		{
			context->Message_Block[context->Message_Block_Index++] = 0;
		}
	}

	context->Message_Block[56] = (context->Length_High >> 24) & 0xFF;
	context->Message_Block[57] = (context->Length_High >> 16) & 0xFF;
	context->Message_Block[58] = (context->Length_High >> 8) & 0xFF;
	context->Message_Block[59] = (context->Length_High) & 0xFF;
	context->Message_Block[60] = (context->Length_Low >> 24) & 0xFF;
	context->Message_Block[61] = (context->Length_Low >> 16) & 0xFF;
	context->Message_Block[62] = (context->Length_Low >> 8) & 0xFF;
	context->Message_Block[63] = (context->Length_Low) & 0xFF;

	SHA1ProcessMessageBlock(context);
}
Example #10
0
static void SHA1PadMessage(SHA1Context *context)
{
   if (!context)
      return;

   /*
    *  Check to see if the current message block is too small to hold
    *  the initial padding bits and length.  If so, we will pad the
    *  block, process it, and then continue padding into a second
    *  block.
    */
   context->Message_Block[context->Message_Block_Index++] = 0x80;

   if (context->Message_Block_Index > 55)
   {
      while(context->Message_Block_Index < 64)
         context->Message_Block[context->Message_Block_Index++] = 0;

      SHA1ProcessMessageBlock(context);
   }

   while(context->Message_Block_Index < 56)
      context->Message_Block[context->Message_Block_Index++] = 0;

   /*  Store the message length as the last 8 octets */
   context->Message_Block[56] = (context->Length_High >> 24) & 0xFF;
   context->Message_Block[57] = (context->Length_High >> 16) & 0xFF;
   context->Message_Block[58] = (context->Length_High >> 8) & 0xFF;
   context->Message_Block[59] = (context->Length_High) & 0xFF;
   context->Message_Block[60] = (context->Length_Low >> 24) & 0xFF;
   context->Message_Block[61] = (context->Length_Low >> 16) & 0xFF;
   context->Message_Block[62] = (context->Length_Low >> 8) & 0xFF;
   context->Message_Block[63] = (context->Length_Low) & 0xFF;

   SHA1ProcessMessageBlock(context);
}
Example #11
0
File: sha1.c Project: ireader/sdk
/*
* 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 octets representing the next portion of
*		the message.
*	length: [in]
*		The length of the message in message_array.
*
* Returns:
*	sha Error Code.
*
*/
int SHA1Input(SHA1Context *context, const uint8_t *message_array, unsigned int length)
{
	if (!context) return shaNull;
	if (!length) return shaSuccess;
	if (!message_array) return shaNull;
	if (context->Computed) return context->Corrupted = shaStateError;
	if (context->Corrupted) return context->Corrupted;
	while (length--) {
		context->Message_Block[context->Message_Block_Index++] =
			*message_array;
		if ((SHA1AddLength(context, 8) == shaSuccess) &&
			(context->Message_Block_Index == SHA1_Message_Block_Size))
			SHA1ProcessMessageBlock(context);
		message_array++;
	}
	return context->Corrupted;
}
Example #12
0
/*  
 *  SHA1Input
 *
 *  Description:
 *      This function accepts an array of octets as the next portion of
 *      the message.
 *
 *  Parameters:
 *      context: [in/out]
 *          The SHA-1 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:
 *      Nothing.
 *
 *  Comments:
 *
 */
static void
SHA1Input(     SHA1Context         *context,
                    const unsigned char *message_array,
                    uint32_t            length)
{
    if (!length)
    {
        return;
    }

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

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

        context->Length_Low += 8;
        /* Force it to 32 bits */
        context->Length_Low &= 0xFFFFFFFF;
        if (context->Length_Low == 0)
        {
            context->Length_High++;
            /* Force it to 32 bits */
            context->Length_High &= 0xFFFFFFFF;
            if (context->Length_High == 0)
            {
                /* Message is too long */
                context->Corrupted = 1;
            }
        }

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

        message_array++;
    }
}
Example #13
0
void SHA1Input(SHA1Context *context, const unsigned char *message_array, unsigned length) {
	if (!length)
		return;
	if (context->Computed || context->Corrupted) {
		context->Corrupted = 1;
		return;
	}

	while(length-- && !context->Corrupted) {
		context->Message_Block[context->Message_Block_Index++] = (*message_array & 0xFF);
		context->Length_Low += 8;
		context->Length_Low &= 0xFFFFFFFF;
		if (!context->Length_Low && !(context->Length_High=((1+context->Length_High)&0xFFFFFFFF)))
			context->Corrupted = 1; // too long message
		if (context->Message_Block_Index == 64)
			SHA1ProcessMessageBlock(context);
		message_array++;
	}
}
Example #14
0
File: main.c Project: xwaynec/eplab
void SHA1Input(SHA1Context idata *context,unsigned char idata *message_array,unsigned idata length)
{
    if (!length)
    {
        return;
    }

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

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

		//blink_led();
        context->Length_Low += 8;
        /* Force it to 32 bits */
        context->Length_Low &= 0xFFFFFFFF;
        if (context->Length_Low == 0)
        {
            context->Length_High++;
            /* Force it to 32 bits */
            context->Length_High &= 0xFFFFFFFF;
            if (context->Length_High == 0)
            {
                /* Message is too long */
                context->Corrupted = 1;
            }
        }

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

        message_array++;
    }
}