From: phk Date: Tue, 25 Nov 2008 11:45:59 +0000 (+0000) Subject: Neuther the FreeBSD specifics of SHA256 implementation. X-Git-Url: https://err.no/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=ef1ec30c2bbdd1dd6f43e868d66d585cd8a90770;p=varnish Neuther the FreeBSD specifics of SHA256 implementation. In the end, it comes down to lack of POSIX definition of a way to find out byte-endianess, sigh... git-svn-id: svn+ssh://projects.linpro.no/svn/varnish/trunk@3438 d4fa192b-c00b-0410-8231-f00ffab90ce4 --- diff --git a/varnish-cache/include/vsha256.h b/varnish-cache/include/vsha256.h index 11fc53ba..57866d23 100644 --- a/varnish-cache/include/vsha256.h +++ b/varnish-cache/include/vsha256.h @@ -29,7 +29,7 @@ #ifndef _SHA256_H_ #define _SHA256_H_ -#include +#include typedef struct SHA256Context { uint32_t state[8]; @@ -41,10 +41,6 @@ __BEGIN_DECLS void SHA256_Init(SHA256_CTX *); void SHA256_Update(SHA256_CTX *, const void *, size_t); void SHA256_Final(unsigned char [32], SHA256_CTX *); -char *SHA256_End(SHA256_CTX *, char *); -char *SHA256_File(const char *, char *); -char *SHA256_FileChunk(const char *, char *, off_t, off_t); -char *SHA256_Data(const void *, unsigned int, char *); __END_DECLS #endif /* !_SHA256_H_ */ diff --git a/varnish-cache/lib/libvarnish/vsha256.c b/varnish-cache/lib/libvarnish/vsha256.c index 4ca412f9..99d5f281 100644 --- a/varnish-cache/lib/libvarnish/vsha256.c +++ b/varnish-cache/lib/libvarnish/vsha256.c @@ -22,19 +22,17 @@ * 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. + * + * From: $FreeBSD: head/lib/libmd/sha256c.c 154479 2006-01-17 15:35:57Z phk $ */ -#include -__FBSDID("$FreeBSD: head/lib/libmd/sha256c.c 154479 2006-01-17 15:35:57Z phk $"); - -#include -#include +#include #include #include "vsha256.h" -#if BYTE_ORDER == BIG_ENDIAN +#if defined(BYTE_ORDER) && BYTE_ORDER == BIG_ENDIAN /* Copy a vector of big-endian uint32_t into a vector of bytes */ #define be32enc_vect(dst, src, len) \ @@ -44,7 +42,26 @@ __FBSDID("$FreeBSD: head/lib/libmd/sha256c.c 154479 2006-01-17 15:35:57Z phk $") #define be32dec_vect(dst, src, len) \ memcpy((void *)dst, (const void *)src, (size_t)len) -#else /* BYTE_ORDER != BIG_ENDIAN */ +#else /* BYTE_ORDER != BIG_ENDIAN or in doubt... */ + +static __inline uint32_t +mybe32dec(const void *pp) +{ + unsigned char const *p = (unsigned char const *)pp; + + return ((p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3]); +} + +static __inline void +mybe32enc(void *pp, uint32_t u) +{ + unsigned char *p = (unsigned char *)pp; + + p[0] = (u >> 24) & 0xff; + p[1] = (u >> 16) & 0xff; + p[2] = (u >> 8) & 0xff; + p[3] = u & 0xff; +} /* * Encode a length len/4 vector of (uint32_t) into a length len vector of @@ -56,7 +73,7 @@ be32enc_vect(unsigned char *dst, const uint32_t *src, size_t len) size_t i; for (i = 0; i < len / 4; i++) - be32enc(dst + i * 4, src[i]); + mybe32enc(dst + i * 4, src[i]); } /* @@ -69,10 +86,10 @@ be32dec_vect(uint32_t *dst, const unsigned char *src, size_t len) size_t i; for (i = 0; i < len / 4; i++) - dst[i] = be32dec(src + i * 4); + dst[i] = mybe32dec(src + i * 4); } -#endif /* BYTE_ORDER != BIG_ENDIAN */ +#endif /* Elementary functions used by SHA256 */ #define Ch(x, y, z) ((x & (y ^ z)) ^ z)