Asterisk - The Open Source Telephony Project
18.5.0
|
Based on the RFC 6234. More...
#include <asterisk/sha1.h>
Go to the source code of this file.
Macros | |
#define | SHA1_ROTL(bits, word) (((word) << (bits)) | ((word) >> (32-(bits)))) |
#define | SHA1AddLength(context, length) |
Functions | |
int | SHA1FinalBits (SHA1Context *context, uint8_t message_bits, unsigned int length) |
SHA1FinalBits Add in any final bits of the message. More... | |
static void | SHA1Finalize (SHA1Context *context, uint8_t Pad_Byte) |
This helper function finishes off the digest calculations. More... | |
int | SHA1Input (SHA1Context *context, const uint8_t *message_array, unsigned length) |
SHA1Input. More... | |
static void | SHA1PadMessage (SHA1Context *context, uint8_t Pad_Byte) |
Pad message to be 512 bits. More... | |
static void | SHA1ProcessMessageBlock (SHA1Context *context) |
Process the next 512 bits of the message stored in the Message_Block array. More... | |
int | SHA1Reset (SHA1Context *context) |
SHA1Reset. More... | |
int | SHA1Result (SHA1Context *context, uint8_t Message_Digest[SHA1HashSize]) |
Variables | |
static uint32_t | addTemp |
Based on the RFC 6234.
Copyright (c) 2011 IETF Trust and the persons identified as authors of the code. All rights reserved.
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Description: This file implements the Secure Hash Algorithm SHA-1 as defined in the U.S. National Institute of Standards and Technology Federal Information Processing Standards Publication (FIPS PUB) 180-3 published in October 2008 and formerly defined in its predecessors, FIPS PUB 180-1 and FIP PUB 180-2.
A combined document showing all algorithms is available at http://csrc.nist.gov/publications/fips/ fips180-3/fips180-3_final.pdf
The SHA-1 algorithm produces a 160-bit message digest for a given data stream that 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 "sha.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. This implementation uses SHA1Input() to hash the bits that are a multiple of the size of an 8-bit octet, and then optionally uses SHA1FinalBits() to hash the final few bits of the input.
Definition in file sha1.c.
Define the SHA1 circular left shift macro
Definition at line 74 of file sha1.c.
Referenced by SHA1ProcessMessageBlock().
#define SHA1AddLength | ( | context, | |
length | |||
) |
Definition at line 82 of file sha1.c.
Referenced by SHA1FinalBits(), and SHA1Input().
int SHA1FinalBits | ( | SHA1Context * | context, |
uint8_t | message_bits, | ||
unsigned int | length | ||
) |
SHA1FinalBits Add in any final bits of the message.
context | [in/out] The SHA context to update. |
message_bits | [in] The final bits of the message, in the upper portion of the byte. (Use 0b###00000 instead of 0b00000### to input the three bits ###.) |
length | [in] * The number of bits in message_bits, between 1 and 7. |
Definition at line 179 of file sha1.c.
References SHA1Context::Computed, SHA1Context::Corrupted, SHA1AddLength, SHA1Finalize(), shaBadParam, shaNull, shaStateError, and shaSuccess.
|
static |
This helper function finishes off the digest calculations.
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. |
Definition at line 346 of file sha1.c.
References SHA1Context::Computed, SHA1Context::Length_High, SHA1Context::Length_Low, SHA1Context::Message_Block, SHA1_Message_Block_Size, and SHA1PadMessage().
Referenced by SHA1FinalBits(), and SHA1Result().
int SHA1Input | ( | SHA1Context * | context, |
const uint8_t * | message_array, | ||
unsigned | length | ||
) |
SHA1Input.
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. This function accepts an array of octets as the next portion of the message. |
Definition at line 133 of file sha1.c.
References SHA1Context::Computed, SHA1Context::Corrupted, SHA1Context::Message_Block, SHA1Context::Message_Block_Index, SHA1_Message_Block_Size, SHA1AddLength, SHA1ProcessMessageBlock(), shaNull, shaStateError, and shaSuccess.
|
static |
Pad message to be 512 bits.
context | [in/out] The context to pad. |
Pad_Byte | [in] Last padding byte. |
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.
Definition at line 374 of file sha1.c.
References SHA1Context::Length_High, SHA1Context::Length_Low, SHA1Context::Message_Block, SHA1Context::Message_Block_Index, SHA1_Message_Block_Size, and SHA1ProcessMessageBlock().
Referenced by SHA1Finalize().
|
static |
Process the next 512 bits of the message stored in the Message_Block array.
context | [in/out] The SHA context to update |
Definition at line 260 of file sha1.c.
References SHA1Context::Intermediate_Hash, SHA1Context::Message_Block, SHA1Context::Message_Block_Index, SHA1_ROTL, SHA_Ch, SHA_Maj, and SHA_Parity.
Referenced by SHA1Input(), and SHA1PadMessage().
int SHA1Reset | ( | SHA1Context * | context | ) |
SHA1Reset.
context | the context to be reset. This function will initialize the SHA1Context in preparation for computing a new SHA1 message digest. |
Definition at line 101 of file sha1.c.
References SHA1Context::Computed, SHA1Context::Corrupted, SHA1Context::Intermediate_Hash, SHA1Context::Length_High, SHA1Context::Length_Low, SHA1Context::Message_Block_Index, shaNull, and shaSuccess.
Referenced by ast_sha1_hash(), and ast_sha1_hash_uint().
int SHA1Result | ( | SHA1Context * | context, |
uint8_t | Message_Digest[SHA1HashSize] | ||
) |
Definition at line 226 of file sha1.c.
References SHA1Context::Computed, SHA1Context::Corrupted, SHA1Context::Intermediate_Hash, SHA1Finalize(), SHA1HashSize, shaNull, and shaSuccess.
Referenced by ast_sha1_hash(), and ast_sha1_hash_uint().