Function crypto::sha1::sha1_digest_block [] [src]

pub fn sha1_digest_block(state: &mut [u32; 5], block: &[u8])

Process a block with the SHA-1 algorithm. (See more...)

SHA-1 is a cryptographic hash function, and as such, it operates on an arbitrary number of bytes. This function operates on a fixed number of bytes. If you call this function with anything other than 64 bytes, then it will panic! This function takes two arguments:

  • state is reference to an array of 5 words.
  • block is reference to a slice of 64 bytes.

If you want the function that performs a message digest on an arbitrary number of bytes, then see also the Sha1 struct above.

Implementation

First, some background. Both ARM and Intel are releasing documentation that they plan to include instruction set extensions for SHA1 and SHA256 sometime in the near future. Second, LLVM won't lower these intrinsics yet, so these functions were written emulate these instructions. Finally, the block function implemented with these emulated intrinsics turned out to be quite fast! What follows is a discussion of this CPU-level view of the SHA-1 algorithm and how it relates to the mathematical definition.

The SHA instruction set extensions can be divided up into two categories:

  • message work schedule update calculation ("schedule" v., "work" n.)
  • message block 80-round digest calculation ("digest" v., "block" n.)

The schedule-related functions can be used to easily perform 4 rounds of the message work schedule update calculation, as shown below:

macro_rules! schedule_x4 {
    ($v0:expr, $v1:expr, $v2:expr, $v3:expr) => (
        sha1msg2(sha1msg1($v0, $v1) ^ $v2, $v3)
    )
}

macro_rules! round_x4 {
    ($h0:ident, $h1:ident, $wk:expr, $i:expr) => (
        sha1rnds4($h0, sha1_first_half($h1, $wk), $i)
    )
}

and also shown above is how the digest-related functions can be used to perform 4 rounds of the message block digest calculation.