sha1.c revision 25cc6a4036ecb7db695004fd80f58ac8a82aedeb
/*
* Copyright 2008 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
/*
* The basic framework for this code came from the reference
* implementation for MD5. That implementation is Copyright (C)
* 1991-2, RSA Data Security, Inc. Created 1991. All rights reserved.
*
* License to copy and use this software is granted provided that it
* is identified as the "RSA Data Security, Inc. MD5 Message-Digest
* Algorithm" in all material mentioning or referencing this software
* or this function.
*
* License is also granted to make and use derivative works provided
* that such works are identified as "derived from the RSA Data
* Security, Inc. MD5 Message-Digest Algorithm" in all material
* mentioning or referencing the derived work.
*
* RSA Data Security, Inc. makes no representations concerning either
* the merchantability of this software or the suitability of this
* software for any particular purpose. It is provided "as is"
* without express or implied warranty of any kind.
*
* These notices must be retained in any copies of any part of this
*
* NOTE: Cleaned-up and optimized, version of SHA1, based on the FIPS 180-1
* standard, available at http://www.itl.nist.gov/fipspubs/fip180-1.htm
* Not as fast as one would like -- further optimizations are encouraged
* and appreciated.
*/
#include <sys/sysmacros.h>
#include <sys/sha1_consts.h>
#ifndef _KERNEL
#include <strings.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/systeminfo.h>
#endif /* !_KERNEL */
#ifdef _LITTLE_ENDIAN
#include <sys/byteorder.h>
#define HAVE_HTONL
#endif
#if defined(__sparc)
#else
#endif
/*
* F, G, and H are the basic SHA1 functions.
*/
#define F(b, c, d) (((b) & (c)) | ((~b) & (d)))
#define G(b, c, d) ((b) ^ (c) ^ (d))
#define H(b, c, d) (((b) & (c)) | (((b)|(c)) & (d)))
/*
* ROTATE_LEFT rotates x left n bits.
*/
static __inline__ uint64_t
{
}
#else
#define ROTATE_LEFT(x, n) \
(((x) << (n)) | ((x) >> ((sizeof (x) * NBBY)-(n))))
#endif
/*
* SHA1Init()
*
* purpose: initializes the sha1 context and begins and sha1 digest operation
* input: SHA1_CTX * : the context to initializes.
* output: void
*/
void
{
/*
* load magic initialization constants. Tell lint
* that these constants are unsigned by using U.
*/
}
#ifdef VIS_SHA1
#ifdef _KERNEL
/* the alignment for block stores to save fp registers */
#define VIS_ALIGN (64)
extern int sha1_savefp(kfpu_t *, int);
extern void sha1_restorefp(kfpu_t *);
#endif /* _KERNEL */
/*
* VIS SHA-1 consts.
*/
0x8000000080000000ULL,
0x0002000200020002ULL,
0x5a8279996ed9eba1ULL,
0x8f1bbcdcca62c1d6ULL,
0x012389ab456789abULL};
/*
* SHA1Update()
*
* purpose: continues an sha1 digest operation, using the message block
* to update the context.
* input: SHA1_CTX * : the context to update
* void * : the message block
* size_t : the length of the message block in bytes
* output: void
*/
void
{
#ifdef _KERNEL
int usevis = 0;
#else
int usevis = 1;
#endif /* _KERNEL */
/* check for noop */
if (input_len == 0)
return;
/* compute number of bytes mod 64 */
/* update number of bits */
/* transform as many times as possible */
i = 0;
#ifdef _KERNEL
if (fpu_exists) {
int svfp_ok;
} else {
usevis = 0;
}
#endif /* _KERNEL */
/*
* general optimization:
*
* only do initial bcopy() and SHA1Transform() if
* buf_index != 0. if buf_index == 0, we're just
* wasting our time doing the bcopy() since there
* wasn't any data left over from a previous call to
* SHA1Update().
*/
if (buf_index) {
if (usevis) {
} else {
}
i = buf_len;
}
/*
* VIS SHA-1: uses the VIS 1.0 instructions to accelerate
* SHA-1 processing. This is achieved by "offloading" the
* computation of the message schedule (MS) to the VIS units.
* This allows the VIS computation of the message schedule
* to be performed in parallel with the standard integer
* processing of the remainder of the SHA-1 computation.
* performance by up to around 1.37X, compared to an optimized
* integer-only implementation.
*
* The VIS implementation of SHA1Transform has a different API
* to the standard integer version:
*
* void SHA1TransformVIS(
* uint64_t *, // Pointer to MS for ith block
* uint32_t *, // Pointer to ith block of message data
* uint32_t *, // Pointer to SHA state i.e ctx->state
* uint64_t *, // Pointer to various VIS constants
* )
*
* Note: the message data must by 4-byte aligned.
*
* Function requires VIS 1.0 support.
*
* Handling is provided to deal with arbitrary byte alingment
* of the input data but the performance gains are reduced
* for alignments other than 4-bytes.
*/
if (usevis) {
/*
* Main processing loop - input misaligned
*/
}
} else {
/*
* Main processing loop - input 8-byte aligned
*/
/* LINTED E_BAD_PTR_CAST_ALIGN */
}
}
#ifdef _KERNEL
#endif /* _KERNEL */
} else {
}
}
/*
* general optimization:
*
* if i and input_len are the same, return now instead
* of calling bcopy(), since the bcopy() in this case
* will be an expensive nop.
*/
if (input_len == i)
return;
buf_index = 0;
}
/* buffer remaining input */
}
#else /* VIS_SHA1 */
void
{
#if defined(__amd64)
#endif /* __amd64 */
/* check for noop */
if (input_len == 0)
return;
/* compute number of bytes mod 64 */
/* update number of bits */
/* transform as many times as possible */
i = 0;
/*
* general optimization:
*
* only do initial bcopy() and SHA1Transform() if
* buf_index != 0. if buf_index == 0, we're just
* wasting our time doing the bcopy() since there
* wasn't any data left over from a previous call to
* SHA1Update().
*/
if (buf_index) {
i = buf_len;
}
#if !defined(__amd64)
#else
if (block_count > 0) {
i += block_count << 6;
}
#endif /* !__amd64 */
/*
* general optimization:
*
* if i and input_len are the same, return now instead
* of calling bcopy(), since the bcopy() in this case
* will be an expensive nop.
*/
if (input_len == i)
return;
buf_index = 0;
}
/* buffer remaining input */
}
#endif /* VIS_SHA1 */
/*
* SHA1Final()
*
* purpose: ends an sha1 digest operation, finalizing the message digest and
* zeroing the context.
* input: uchar_t * : A buffer to store the digest.
* : The function actually uses void* because many
* : callers pass things other than uchar_t here.
* SHA1_CTX * : the context to finalize, save, and zero
* output: void
*/
void
{
/* store bit count, big endian */
/* pad out to 56 mod 64 */
/* append length (before padding) */
/* store state in digest */
/* zeroize sensitive information */
}
#if !defined(__amd64)
/*
* sparc optimization:
*
* on the sparc, we can load big endian 32-bit data easily. note that
* special care must be taken to ensure the address is 32-bit aligned.
* in the interest of speed, we don't check to make sure, since
* careful programming can guarantee this for us.
*/
#if defined(_BIG_ENDIAN)
#elif defined(HAVE_HTONL)
#else
/* little endian -- will work on big endian, but slowly */
#define LOAD_BIG_32(addr) \
#endif /* _BIG_ENDIAN */
/*
* SHA1Transform()
*/
#if defined(W_ARRAY)
#define W(n) w[n]
#else /* !defined(W_ARRAY) */
#define W(n) w_ ## n
#endif /* !defined(W_ARRAY) */
#if defined(__sparc)
/*
* sparc register window optimization:
*
* `a', `b', `c', `d', and `e' are passed into SHA1Transform
* explicitly since it increases the number of registers available to
* the compiler. under this scheme, these variables can be held in
* %i0 - %i4, which leaves more local and out registers available.
*
* purpose: sha1 transformation -- updates the digest based on `block'
* input: uint32_t : bytes 1 - 4 of the digest
* uint32_t : bytes 5 - 8 of the digest
* uint32_t : bytes 9 - 12 of the digest
* uint32_t : bytes 12 - 16 of the digest
* uint32_t : bytes 16 - 20 of the digest
* SHA1_CTX * : the context to update
* uint8_t [64]: the block to use to update the digest
* output: void
*/
void
{
/*
* sparc optimization:
*
* while it is somewhat counter-intuitive, on sparc, it is
* more efficient to place all the constants used in this
* function in an array and load the values out of the array
* than to manually load the constants. this is because
* setting a register to a 32-bit value takes two ops in most
* cases: a `sethi' and an `or', but loading a 32-bit value
* from memory only takes one `ld' (or `lduw' on v9). while
* this increases memory usage, the compiler can find enough
* other things to do while waiting to keep the pipeline does
* not stall. additionally, it is likely that many of these
* constants are cached so that later accesses do not even go
* out to the bus.
*
* this array is declared `static' to keep the compiler from
* having to bcopy() this array onto the stack frame of
* SHA1Transform() each time it is called -- which is
* unacceptably expensive.
*
* the `const' is to ensure that callers are good citizens and
* do not try to munge the array. since these routines are
* going to be called from inside multithreaded kernelland,
* this is a good safety check. -- `sha1_consts' will end up in
* .rodata.
*
* unfortunately, loading from an array in this manner hurts
* performance under Intel. So, there is a macro,
* SHA1_CONST(), used in SHA1Transform(), that either expands to
* a reference to this array, or to the actual constant,
* depending on what platform this code is compiled for.
*/
static const uint32_t sha1_consts[] = {
};
/*
* general optimization:
*
* use individual integers instead of using an array. this is a
* win, although the amount it wins by seems to vary quite a bit.
*/
/*
* sparc optimization:
*
* if `block' is already aligned on a 4-byte boundary, use
* LOAD_BIG_32() directly. otherwise, bcopy() into a
* buffer that *is* aligned on a 4-byte boundary and then do
* the LOAD_BIG_32() on that buffer. benchmarks have shown
* that using the bcopy() is better than loading the bytes
* individually and doing the endian-swap by hand.
*
* even though it's quite tempting to assign to do:
*
* blk = bcopy(ctx->buf_un.buf32, blk, sizeof (ctx->buf_un.buf32));
*
* and only have one set of LOAD_BIG_32()'s, the compiler
* *does not* like that, so please resist the urge.
*/
} else {
/*LINTED*/
/*LINTED*/
/*LINTED*/
/*LINTED*/
/*LINTED*/
/*LINTED*/
/*LINTED*/
/*LINTED*/
/*LINTED*/
/*LINTED*/
/*LINTED*/
/*LINTED*/
/*LINTED*/
/*LINTED*/
/*LINTED*/
/*LINTED*/
}
#else /* !defined(__sparc) */
void /* CSTYLED */
{
/* CSTYLED */
#if defined(W_ARRAY)
sha1word w[16];
#else /* !defined(W_ARRAY) */
#endif /* !defined(W_ARRAY) */
W(0) = LOAD_BIG_32((void *)(blk + 0));
#endif /* !defined(__sparc) */
/*
* general optimization:
*
* even though this approach is described in the standard as
* being slower algorithmically, it is 30-40% faster than the
* "faster" version under SPARC, because this version has more
* of the constraints specified at compile-time and uses fewer
* variables (and therefore has better register utilization)
* than its "speedier" brother. (i've tried both, trust me)
*
* for either method given in the spec, there is an "assignment"
* phase where the following takes place:
*
* tmp = (main_computation);
* e = d; d = c; c = rotate_left(b, 30); b = a; a = tmp;
*
* we can make the algorithm go faster by not doing this work,
* but just pretending that `d' is now `e', etc. this works
* really well and obviates the need for a temporary variable.
* however, we still explicitly perform the rotate action,
* since it is cheaper on SPARC to do it once than to have to
* do it over and over again.
*/
/* round 1 */
b = ROTATE_LEFT(b, 30);
a = ROTATE_LEFT(a, 30);
e = ROTATE_LEFT(e, 30);
d = ROTATE_LEFT(d, 30);
c = ROTATE_LEFT(c, 30);
b = ROTATE_LEFT(b, 30);
a = ROTATE_LEFT(a, 30);
e = ROTATE_LEFT(e, 30);
d = ROTATE_LEFT(d, 30);
c = ROTATE_LEFT(c, 30);
b = ROTATE_LEFT(b, 30);
a = ROTATE_LEFT(a, 30);
e = ROTATE_LEFT(e, 30);
d = ROTATE_LEFT(d, 30);
c = ROTATE_LEFT(c, 30);
b = ROTATE_LEFT(b, 30);
a = ROTATE_LEFT(a, 30);
e = ROTATE_LEFT(e, 30);
d = ROTATE_LEFT(d, 30);
c = ROTATE_LEFT(c, 30);
/* round 2 */
b = ROTATE_LEFT(b, 30);
a = ROTATE_LEFT(a, 30);
e = ROTATE_LEFT(e, 30);
d = ROTATE_LEFT(d, 30);
c = ROTATE_LEFT(c, 30);
b = ROTATE_LEFT(b, 30);
a = ROTATE_LEFT(a, 30);
e = ROTATE_LEFT(e, 30);
d = ROTATE_LEFT(d, 30);
c = ROTATE_LEFT(c, 30);
b = ROTATE_LEFT(b, 30);
a = ROTATE_LEFT(a, 30);
e = ROTATE_LEFT(e, 30);
d = ROTATE_LEFT(d, 30);
c = ROTATE_LEFT(c, 30);
b = ROTATE_LEFT(b, 30);
a = ROTATE_LEFT(a, 30);
e = ROTATE_LEFT(e, 30);
d = ROTATE_LEFT(d, 30);
c = ROTATE_LEFT(c, 30);
/* round 3 */
b = ROTATE_LEFT(b, 30);
a = ROTATE_LEFT(a, 30);
e = ROTATE_LEFT(e, 30);
d = ROTATE_LEFT(d, 30);
c = ROTATE_LEFT(c, 30);
b = ROTATE_LEFT(b, 30);
a = ROTATE_LEFT(a, 30);
e = ROTATE_LEFT(e, 30);
d = ROTATE_LEFT(d, 30);
c = ROTATE_LEFT(c, 30);
b = ROTATE_LEFT(b, 30);
a = ROTATE_LEFT(a, 30);
e = ROTATE_LEFT(e, 30);
d = ROTATE_LEFT(d, 30);
c = ROTATE_LEFT(c, 30);
b = ROTATE_LEFT(b, 30);
a = ROTATE_LEFT(a, 30);
e = ROTATE_LEFT(e, 30);
d = ROTATE_LEFT(d, 30);
c = ROTATE_LEFT(c, 30);
/* round 4 */
b = ROTATE_LEFT(b, 30);
a = ROTATE_LEFT(a, 30);
e = ROTATE_LEFT(e, 30);
d = ROTATE_LEFT(d, 30);
c = ROTATE_LEFT(c, 30);
b = ROTATE_LEFT(b, 30);
a = ROTATE_LEFT(a, 30);
e = ROTATE_LEFT(e, 30);
d = ROTATE_LEFT(d, 30);
c = ROTATE_LEFT(c, 30);
b = ROTATE_LEFT(b, 30);
a = ROTATE_LEFT(a, 30);
e = ROTATE_LEFT(e, 30);
d = ROTATE_LEFT(d, 30);
c = ROTATE_LEFT(c, 30);
b = ROTATE_LEFT(b, 30);
a = ROTATE_LEFT(a, 30);
e = ROTATE_LEFT(e, 30);
d = ROTATE_LEFT(d, 30);
SHA1_CONST(3);
/* zeroize sensitive information */
W(0) = W(1) = W(2) = W(3) = W(4) = W(5) = W(6) = W(7) = W(8) = 0;
W(9) = W(10) = W(11) = W(12) = W(13) = W(14) = W(15) = 0;
}
#endif /* !__amd64 */
/*
* Encode()
*
* purpose: to convert a list of numbers from little endian to big endian
* input: uint8_t * : place to store the converted big endian numbers
* uint32_t * : place to get numbers to convert from
* size_t : the length of the input in bytes
* output: void
*/
static void
{
size_t i, j;
#if defined(__sparc)
for (i = 0, j = 0; j < len; i++, j += 4) {
/* LINTED: pointer alignment */
}
} else {
#endif /* little endian -- will work on big endian, but slowly */
for (i = 0, j = 0; j < len; i++, j += 4) {
}
#if defined(__sparc)
}
#endif
}