X-Git-Url: http://git.cascardo.eti.br/?a=blobdiff_plain;f=lib%2Fsha1.c;h=4f48ef21027e9d408f70fe5de5322111b3dc49e3;hb=8f79bb4d3999d993424e9578342b4130d10a556c;hp=f73f2d6477e28e32ceed79f2e57e78544bd6190e;hpb=5eccf359391f7fe2cdb0edbaaf5680895c115218;p=cascardo%2Fovs.git diff --git a/lib/sha1.c b/lib/sha1.c index f73f2d647..4f48ef210 100644 --- a/lib/sha1.c +++ b/lib/sha1.c @@ -1,7 +1,7 @@ /* * This file is from the Apache Portable Runtime Library. * The full upstream copyright and license statement is included below. - * Modifications copyright (c) 2009 Nicira Networks. + * Modifications copyright (c) 2009, 2010 Nicira, Inc. */ /* Licensed to the Apache Software Foundation (ASF) under one or more @@ -23,15 +23,18 @@ /* This software also makes use of the following component: * * NIST Secure Hash Algorithm - * heavily modified by Uwe Hollerbach uh@alumni.caltech edu - * from Peter C. Gutmann's implementation as found in - * Applied Cryptography by Bruce Schneier - * This code is hereby placed in the public domain + * heavily modified by Uwe Hollerbach uh@alumni.caltech edu + * from Peter C. Gutmann's implementation as found in + * Applied Cryptography by Bruce Schneier + * This code is hereby placed in the public domain */ #include #include "sha1.h" +#include #include +#include "compiler.h" +#include "util.h" /* a bit faster & bigger, if defined */ #define UNROLL_LOOPS @@ -62,10 +65,10 @@ f4(uint32_t x, uint32_t y, uint32_t z) } /* SHA constants */ -#define CONST1 0x5a827999L -#define CONST2 0x6ed9eba1L -#define CONST3 0x8f1bbcdcL -#define CONST4 0xca62c1d6L +#define CONST1 0x5a827999L +#define CONST2 0x6ed9eba1L +#define CONST3 0x8f1bbcdcL +#define CONST4 0xca62c1d6L /* 32-bit rotate */ static inline uint32_t @@ -148,25 +151,25 @@ sha_transform(struct sha1_ctx *sha_info) /* 'count' is the number of bytes to do an endian flip. */ static void -maybe_byte_reverse(uint32_t *buffer, int count) +maybe_byte_reverse(uint32_t *buffer OVS_UNUSED, int count OVS_UNUSED) { +#if !WORDS_BIGENDIAN int i; uint8_t ct[4], *cp; -#if !WORDS_BIGENDIAN - count /= sizeof(uint32_t); - cp = (uint8_t *) buffer; - for (i = 0; i < count; i++) { - ct[0] = cp[0]; - ct[1] = cp[1]; - ct[2] = cp[2]; - ct[3] = cp[3]; - cp[0] = ct[3]; - cp[1] = ct[2]; - cp[2] = ct[1]; - cp[3] = ct[0]; - cp += sizeof(uint32_t); - } + count /= sizeof(uint32_t); + cp = (uint8_t *) buffer; + for (i = 0; i < count; i++) { + ct[0] = cp[0]; + ct[1] = cp[1]; + ct[2] = cp[2]; + ct[3] = cp[3]; + cp[0] = ct[3]; + cp[1] = ct[2]; + cp[2] = ct[1]; + cp[3] = ct[0]; + cp += sizeof(uint32_t); + } #endif } @@ -279,3 +282,34 @@ sha1_bytes(const void *data, size_t n, uint8_t digest[SHA1_DIGEST_SIZE]) sha1_update(&ctx, data, n); sha1_final(&ctx, digest); } + +void +sha1_to_hex(const uint8_t digest[SHA1_DIGEST_SIZE], + char hex[SHA1_HEX_DIGEST_LEN + 1]) +{ + int i; + + for (i = 0; i < SHA1_DIGEST_SIZE; i++) { + *hex++ = "0123456789abcdef"[digest[i] >> 4]; + *hex++ = "0123456789abcdef"[digest[i] & 15]; + } + *hex = '\0'; +} + +bool +sha1_from_hex(uint8_t digest[SHA1_DIGEST_SIZE], const char *hex) +{ + int i; + + for (i = 0; i < SHA1_DIGEST_SIZE; i++) { + bool ok; + + digest[i] = hexits_value(hex, 2, &ok); + if (!ok) { + return false; + } + hex += 2; + } + return true; +} +