From f326c2f42fc6156a2f767c168f00a017c8ff33ea Mon Sep 17 00:00:00 2001 From: Dmitry Podgorny Date: Fri, 9 Oct 2015 11:29:52 +0300 Subject: [PATCH] sha1: added prefix crypto_ to SHA1 API sha1.c contains symbols that overlap with libcrypto. This patch fixes linking with libstrophe as static library. Prefix crypto_ will help to group other crypto API in the future. --- src/auth.c | 9 +++++---- src/rand.c | 6 +++--- src/scram.c | 36 ++++++++++++++++++------------------ src/sha1.c | 41 ++++++++++++++++++++++------------------- src/sha1.h | 15 ++++++++------- 5 files changed, 56 insertions(+), 51 deletions(-) diff --git a/src/auth.c b/src/auth.c index 191b331..0b9d033 100644 --- a/src/auth.c +++ b/src/auth.c @@ -1165,10 +1165,11 @@ int _handle_component_auth(xmpp_conn_t * const conn) /* Feed the session id and passphrase to the algorithm. * We need to compute SHA1(session_id + passphrase) */ - SHA1_Init(&mdctx); - SHA1_Update(&mdctx, (uint8_t*)conn->stream_id, strlen(conn->stream_id)); - SHA1_Update(&mdctx, (uint8_t*)conn->pass, strlen(conn->pass)); - SHA1_Final(&mdctx, md_value); + crypto_SHA1_Init(&mdctx); + crypto_SHA1_Update(&mdctx, (uint8_t*)conn->stream_id, + strlen(conn->stream_id)); + crypto_SHA1_Update(&mdctx, (uint8_t*)conn->pass, strlen(conn->pass)); + crypto_SHA1_Final(&mdctx, md_value); digest = xmpp_alloc(conn->ctx, 2*sizeof(md_value)+1); if (digest) { diff --git a/src/rand.c b/src/rand.c index 6777fe8..0134d82 100644 --- a/src/rand.c +++ b/src/rand.c @@ -106,7 +106,7 @@ static void Hash_df(uint8_t *input_string, size_t input_string_len, conj[0] = counter; store_be32((uint32_t)no_of_bytes_to_return * 8, conj + 1); memcpy(conj + 5, input_string, input_string_len); - SHA1(conj, input_string_len + 5, temp + offset); + crypto_SHA1(conj, input_string_len + 5, temp + offset); } memcpy(output_string, temp, no_of_bytes_to_return); @@ -173,7 +173,7 @@ static void Hashgen(uint8_t *V, uint8_t *output, memcpy(data, V, seedlen); for (i = 1; i <= m; ++i) { offset = (i - 1) * outlen; - SHA1(data, seedlen, W + offset); + crypto_SHA1(data, seedlen, W + offset); /* increase data by 1 */ arr_add(data, sizeof(data), &i1, 1); } @@ -196,7 +196,7 @@ static int Hash_DRBG_Generate(Hash_DRBG_CTX *ctx, uint8_t *output, V3[0] = 3; memcpy(V3 + 1, ctx->V, seedlen); - SHA1(V3, sizeof(V3), H); + crypto_SHA1(V3, sizeof(V3), H); arr_add(ctx->V, sizeof(ctx->V), ctx->C, sizeof(ctx->C)); arr_add(ctx->V, sizeof(ctx->V), H, sizeof(H)); store_be32(ctx->reseed_counter, reseed_counter); diff --git a/src/scram.c b/src/scram.c index 5066d60..8c35e41 100644 --- a/src/scram.c +++ b/src/scram.c @@ -33,9 +33,9 @@ static const uint8_t ipad = 0x36; static const uint8_t opad = 0x5C; -static void HMAC_SHA1(const uint8_t *key, size_t key_len, - const uint8_t *text, size_t len, - uint8_t *digest) +static void crypto_HMAC_SHA1(const uint8_t *key, size_t key_len, + const uint8_t *text, size_t len, + uint8_t *digest) { uint8_t key_pad[BLOCK_SIZE]; uint8_t key_ipad[BLOCK_SIZE]; @@ -49,7 +49,7 @@ static void HMAC_SHA1(const uint8_t *key, size_t key_len, memcpy(key_pad, key, key_len); } else { /* according to RFC2104 */ - SHA1(key, key_len, key_pad); + crypto_SHA1(key, key_len, key_pad); } for (i = 0; i < BLOCK_SIZE; i++) { @@ -57,15 +57,15 @@ static void HMAC_SHA1(const uint8_t *key, size_t key_len, key_opad[i] = key_pad[i] ^ opad; } - SHA1_Init(&ctx); - SHA1_Update(&ctx, key_ipad, BLOCK_SIZE); - SHA1_Update(&ctx, text, len); - SHA1_Final(&ctx, sha_digest); + crypto_SHA1_Init(&ctx); + crypto_SHA1_Update(&ctx, key_ipad, BLOCK_SIZE); + crypto_SHA1_Update(&ctx, text, len); + crypto_SHA1_Final(&ctx, sha_digest); - SHA1_Init(&ctx); - SHA1_Update(&ctx, key_opad, BLOCK_SIZE); - SHA1_Update(&ctx, sha_digest, SHA1_DIGEST_SIZE); - SHA1_Final(&ctx, digest); + crypto_SHA1_Init(&ctx); + crypto_SHA1_Update(&ctx, key_opad, BLOCK_SIZE); + crypto_SHA1_Update(&ctx, sha_digest, SHA1_DIGEST_SIZE); + crypto_SHA1_Final(&ctx, digest); } static void SCRAM_SHA1_Hi(const uint8_t *text, size_t len, @@ -90,11 +90,11 @@ static void SCRAM_SHA1_Hi(const uint8_t *text, size_t len, memcpy(&tmp[salt_len], int1, sizeof(int1)); /* 'text' for Hi is a 'key' for HMAC */ - HMAC_SHA1(text, len, tmp, salt_len + sizeof(int1), digest); + crypto_HMAC_SHA1(text, len, tmp, salt_len + sizeof(int1), digest); memcpy(tmp, digest, SHA1_DIGEST_SIZE); for (j = 1; j < i; j++) { - HMAC_SHA1(text, len, tmp, SHA1_DIGEST_SIZE, tmp); + crypto_HMAC_SHA1(text, len, tmp, SHA1_DIGEST_SIZE, tmp); for (k = 0; k < SHA1_DIGEST_SIZE; k++) { digest[k] ^= tmp[k]; } @@ -110,8 +110,8 @@ void SCRAM_SHA1_ClientKey(const uint8_t *password, size_t len, /* XXX: Normalize(password) is omitted */ SCRAM_SHA1_Hi(password, len, salt, salt_len, i, salted); - HMAC_SHA1(salted, SHA1_DIGEST_SIZE, (uint8_t *)"Client Key", - strlen("Client Key"), key); + crypto_HMAC_SHA1(salted, SHA1_DIGEST_SIZE, (uint8_t *)"Client Key", + strlen("Client Key"), key); } void SCRAM_SHA1_ClientSignature(const uint8_t *ClientKey, @@ -120,8 +120,8 @@ void SCRAM_SHA1_ClientSignature(const uint8_t *ClientKey, { uint8_t stored[SHA1_DIGEST_SIZE]; - SHA1(ClientKey, SHA1_DIGEST_SIZE, stored); - HMAC_SHA1(stored, SHA1_DIGEST_SIZE, AuthMessage, len, sign); + crypto_SHA1(ClientKey, SHA1_DIGEST_SIZE, stored); + crypto_HMAC_SHA1(stored, SHA1_DIGEST_SIZE, AuthMessage, len, sign); } void SCRAM_SHA1_ClientProof(const uint8_t *ClientKey, diff --git a/src/sha1.c b/src/sha1.c index 0dd3d76..f3559d8 100644 --- a/src/sha1.c +++ b/src/sha1.c @@ -202,7 +202,7 @@ static void SHA1_Transform(uint32_t state[5], const uint8_t buffer[64]) /* SHA1Init - Initialize new context */ -void SHA1_Init(SHA1_CTX* context) +void crypto_SHA1_Init(SHA1_CTX* context) { /* SHA1 initialization constants */ context->state[0] = 0x67452301; @@ -215,7 +215,8 @@ void SHA1_Init(SHA1_CTX* context) /* Run your data through this. */ -void SHA1_Update(SHA1_CTX* context, const uint8_t* data, const size_t len) +void crypto_SHA1_Update(SHA1_CTX* context, const uint8_t* data, + const size_t len) { size_t i, j; @@ -244,7 +245,7 @@ void SHA1_Update(SHA1_CTX* context, const uint8_t* data, const size_t len) /* Add padding and return the message digest. */ -void SHA1_Final(SHA1_CTX* context, uint8_t* digest) +void crypto_SHA1_Final(SHA1_CTX* context, uint8_t* digest) { uint32_t i; uint8_t finalcount[8]; @@ -253,11 +254,11 @@ void SHA1_Final(SHA1_CTX* context, uint8_t* digest) finalcount[i] = (unsigned char)((context->count[(i >= 4 ? 0 : 1)] >> ((3-(i & 3)) * 8) ) & 255); /* Endian independent */ } - SHA1_Update(context, (uint8_t *)"\200", 1); + crypto_SHA1_Update(context, (uint8_t *)"\200", 1); while ((context->count[0] & 504) != 448) { - SHA1_Update(context, (uint8_t *)"\0", 1); + crypto_SHA1_Update(context, (uint8_t *)"\0", 1); } - SHA1_Update(context, finalcount, 8); /* Should cause a SHA1_Transform() */ + crypto_SHA1_Update(context, finalcount, 8); /* Should cause a SHA1_Transform() */ for (i = 0; i < SHA1_DIGEST_SIZE; i++) { digest[i] = (uint8_t) ((context->state[i>>2] >> ((3-(i & 3)) * 8) ) & 255); @@ -275,12 +276,13 @@ void SHA1_Final(SHA1_CTX* context, uint8_t* digest) #endif } -void SHA1(const uint8_t* data, size_t len, uint8_t* digest) + +void crypto_SHA1(const uint8_t* data, size_t len, uint8_t* digest) { SHA1_CTX ctx; - SHA1_Init(&ctx); - SHA1_Update(&ctx, data, len); - SHA1_Final(&ctx, digest); + crypto_SHA1_Init(&ctx); + crypto_SHA1_Update(&ctx, data, len); + crypto_SHA1_Final(&ctx, digest); } /*************************************************************/ @@ -308,12 +310,12 @@ FILE* file; return(-1); } } - SHA1_Init(&context); + crypto_SHA1_Init(&context); while (!feof(file)) { /* note: what if ferror(file) */ i = fread(buffer, 1, 16384, file); - SHA1_Update(&context, buffer, i); + crypto_SHA1_Update(&context, buffer, i); } - SHA1_Final(&context, digest); + crypto_SHA1_Final(&context, digest); fclose(file); for (i = 0; i < SHA1_DIGEST_SIZE/4; i++) { for (j = 0; j < 4; j++) { @@ -366,9 +368,10 @@ int main(int argc, char** argv) fprintf(stdout, "verifying SHA-1 implementation... "); for (k = 0; k < 2; k++){ - SHA1_Init(&context); - SHA1_Update(&context, (uint8_t*)test_data[k], strlen(test_data[k])); - SHA1_Final(&context, digest); + crypto_SHA1_Init(&context); + crypto_SHA1_Update(&context, (uint8_t*)test_data[k], + strlen(test_data[k])); + crypto_SHA1_Final(&context, digest); digest_to_hex(digest, output); if (strcmp(output, test_results[k])) { @@ -380,10 +383,10 @@ int main(int argc, char** argv) } } /* million 'a' vector we feed separately */ - SHA1_Init(&context); + crypto_SHA1_Init(&context); for (k = 0; k < 1000000; k++) - SHA1_Update(&context, (uint8_t*)"a", 1); - SHA1_Final(&context, digest); + crypto_SHA1_Update(&context, (uint8_t*)"a", 1); + crypto_SHA1_Final(&context, digest); digest_to_hex(digest, output); if (strcmp(output, test_results[2])) { fprintf(stdout, "FAIL\n"); diff --git a/src/sha1.h b/src/sha1.h index fc6fcbf..8dd7700 100644 --- a/src/sha1.h +++ b/src/sha1.h @@ -5,8 +5,8 @@ * SHA-1 hash API. */ -#ifndef __SHA1_H -#define __SHA1_H +#ifndef __LIBSTROPHE_SHA1_H__ +#define __LIBSTROPHE_SHA1_H__ #ifdef __cplusplus extern "C" { @@ -23,13 +23,14 @@ typedef struct { #define SHA1_DIGEST_SIZE 20 -void SHA1_Init(SHA1_CTX* context); -void SHA1_Update(SHA1_CTX* context, const uint8_t* data, const size_t len); -void SHA1_Final(SHA1_CTX* context, uint8_t* digest); -void SHA1(const uint8_t* data, size_t len, uint8_t* digest); +void crypto_SHA1_Init(SHA1_CTX* context); +void crypto_SHA1_Update(SHA1_CTX* context, const uint8_t* data, + const size_t len); +void crypto_SHA1_Final(SHA1_CTX* context, uint8_t* digest); +void crypto_SHA1(const uint8_t* data, size_t len, uint8_t* digest); #ifdef __cplusplus } #endif -#endif /* __SHA1_H */ +#endif /* __LIBSTROPHE_SHA1_H__ */