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.
This commit is contained in:
Dmitry Podgorny
2015-10-09 11:29:52 +03:00
parent 3b55c20879
commit f326c2f42f
5 changed files with 56 additions and 51 deletions

View File

@@ -1165,10 +1165,11 @@ int _handle_component_auth(xmpp_conn_t * const conn)
/* Feed the session id and passphrase to the algorithm. /* Feed the session id and passphrase to the algorithm.
* We need to compute SHA1(session_id + passphrase) * We need to compute SHA1(session_id + passphrase)
*/ */
SHA1_Init(&mdctx); crypto_SHA1_Init(&mdctx);
SHA1_Update(&mdctx, (uint8_t*)conn->stream_id, strlen(conn->stream_id)); crypto_SHA1_Update(&mdctx, (uint8_t*)conn->stream_id,
SHA1_Update(&mdctx, (uint8_t*)conn->pass, strlen(conn->pass)); strlen(conn->stream_id));
SHA1_Final(&mdctx, md_value); 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); digest = xmpp_alloc(conn->ctx, 2*sizeof(md_value)+1);
if (digest) { if (digest) {

View File

@@ -106,7 +106,7 @@ static void Hash_df(uint8_t *input_string, size_t input_string_len,
conj[0] = counter; conj[0] = counter;
store_be32((uint32_t)no_of_bytes_to_return * 8, conj + 1); store_be32((uint32_t)no_of_bytes_to_return * 8, conj + 1);
memcpy(conj + 5, input_string, input_string_len); 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); 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); memcpy(data, V, seedlen);
for (i = 1; i <= m; ++i) { for (i = 1; i <= m; ++i) {
offset = (i - 1) * outlen; offset = (i - 1) * outlen;
SHA1(data, seedlen, W + offset); crypto_SHA1(data, seedlen, W + offset);
/* increase data by 1 */ /* increase data by 1 */
arr_add(data, sizeof(data), &i1, 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; V3[0] = 3;
memcpy(V3 + 1, ctx->V, seedlen); 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), ctx->C, sizeof(ctx->C));
arr_add(ctx->V, sizeof(ctx->V), H, sizeof(H)); arr_add(ctx->V, sizeof(ctx->V), H, sizeof(H));
store_be32(ctx->reseed_counter, reseed_counter); store_be32(ctx->reseed_counter, reseed_counter);

View File

@@ -33,9 +33,9 @@
static const uint8_t ipad = 0x36; static const uint8_t ipad = 0x36;
static const uint8_t opad = 0x5C; static const uint8_t opad = 0x5C;
static void HMAC_SHA1(const uint8_t *key, size_t key_len, static void crypto_HMAC_SHA1(const uint8_t *key, size_t key_len,
const uint8_t *text, size_t len, const uint8_t *text, size_t len,
uint8_t *digest) uint8_t *digest)
{ {
uint8_t key_pad[BLOCK_SIZE]; uint8_t key_pad[BLOCK_SIZE];
uint8_t key_ipad[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); memcpy(key_pad, key, key_len);
} else { } else {
/* according to RFC2104 */ /* according to RFC2104 */
SHA1(key, key_len, key_pad); crypto_SHA1(key, key_len, key_pad);
} }
for (i = 0; i < BLOCK_SIZE; i++) { 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; key_opad[i] = key_pad[i] ^ opad;
} }
SHA1_Init(&ctx); crypto_SHA1_Init(&ctx);
SHA1_Update(&ctx, key_ipad, BLOCK_SIZE); crypto_SHA1_Update(&ctx, key_ipad, BLOCK_SIZE);
SHA1_Update(&ctx, text, len); crypto_SHA1_Update(&ctx, text, len);
SHA1_Final(&ctx, sha_digest); crypto_SHA1_Final(&ctx, sha_digest);
SHA1_Init(&ctx); crypto_SHA1_Init(&ctx);
SHA1_Update(&ctx, key_opad, BLOCK_SIZE); crypto_SHA1_Update(&ctx, key_opad, BLOCK_SIZE);
SHA1_Update(&ctx, sha_digest, SHA1_DIGEST_SIZE); crypto_SHA1_Update(&ctx, sha_digest, SHA1_DIGEST_SIZE);
SHA1_Final(&ctx, digest); crypto_SHA1_Final(&ctx, digest);
} }
static void SCRAM_SHA1_Hi(const uint8_t *text, size_t len, 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)); memcpy(&tmp[salt_len], int1, sizeof(int1));
/* 'text' for Hi is a 'key' for HMAC */ /* '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); memcpy(tmp, digest, SHA1_DIGEST_SIZE);
for (j = 1; j < i; j++) { 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++) { for (k = 0; k < SHA1_DIGEST_SIZE; k++) {
digest[k] ^= tmp[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 */ /* XXX: Normalize(password) is omitted */
SCRAM_SHA1_Hi(password, len, salt, salt_len, i, salted); SCRAM_SHA1_Hi(password, len, salt, salt_len, i, salted);
HMAC_SHA1(salted, SHA1_DIGEST_SIZE, (uint8_t *)"Client Key", crypto_HMAC_SHA1(salted, SHA1_DIGEST_SIZE, (uint8_t *)"Client Key",
strlen("Client Key"), key); strlen("Client Key"), key);
} }
void SCRAM_SHA1_ClientSignature(const uint8_t *ClientKey, 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]; uint8_t stored[SHA1_DIGEST_SIZE];
SHA1(ClientKey, SHA1_DIGEST_SIZE, stored); crypto_SHA1(ClientKey, SHA1_DIGEST_SIZE, stored);
HMAC_SHA1(stored, SHA1_DIGEST_SIZE, AuthMessage, len, sign); crypto_HMAC_SHA1(stored, SHA1_DIGEST_SIZE, AuthMessage, len, sign);
} }
void SCRAM_SHA1_ClientProof(const uint8_t *ClientKey, void SCRAM_SHA1_ClientProof(const uint8_t *ClientKey,

View File

@@ -202,7 +202,7 @@ static void SHA1_Transform(uint32_t state[5], const uint8_t buffer[64])
/* SHA1Init - Initialize new context */ /* SHA1Init - Initialize new context */
void SHA1_Init(SHA1_CTX* context) void crypto_SHA1_Init(SHA1_CTX* context)
{ {
/* SHA1 initialization constants */ /* SHA1 initialization constants */
context->state[0] = 0x67452301; context->state[0] = 0x67452301;
@@ -215,7 +215,8 @@ void SHA1_Init(SHA1_CTX* context)
/* Run your data through this. */ /* 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; 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. */ /* 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; uint32_t i;
uint8_t finalcount[8]; 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)] finalcount[i] = (unsigned char)((context->count[(i >= 4 ? 0 : 1)]
>> ((3-(i & 3)) * 8) ) & 255); /* Endian independent */ >> ((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) { 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++) { for (i = 0; i < SHA1_DIGEST_SIZE; i++) {
digest[i] = (uint8_t) digest[i] = (uint8_t)
((context->state[i>>2] >> ((3-(i & 3)) * 8) ) & 255); ((context->state[i>>2] >> ((3-(i & 3)) * 8) ) & 255);
@@ -275,12 +276,13 @@ void SHA1_Final(SHA1_CTX* context, uint8_t* digest)
#endif #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_CTX ctx;
SHA1_Init(&ctx); crypto_SHA1_Init(&ctx);
SHA1_Update(&ctx, data, len); crypto_SHA1_Update(&ctx, data, len);
SHA1_Final(&ctx, digest); crypto_SHA1_Final(&ctx, digest);
} }
/*************************************************************/ /*************************************************************/
@@ -308,12 +310,12 @@ FILE* file;
return(-1); return(-1);
} }
} }
SHA1_Init(&context); crypto_SHA1_Init(&context);
while (!feof(file)) { /* note: what if ferror(file) */ while (!feof(file)) { /* note: what if ferror(file) */
i = fread(buffer, 1, 16384, 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); fclose(file);
for (i = 0; i < SHA1_DIGEST_SIZE/4; i++) { for (i = 0; i < SHA1_DIGEST_SIZE/4; i++) {
for (j = 0; j < 4; j++) { for (j = 0; j < 4; j++) {
@@ -366,9 +368,10 @@ int main(int argc, char** argv)
fprintf(stdout, "verifying SHA-1 implementation... "); fprintf(stdout, "verifying SHA-1 implementation... ");
for (k = 0; k < 2; k++){ for (k = 0; k < 2; k++){
SHA1_Init(&context); crypto_SHA1_Init(&context);
SHA1_Update(&context, (uint8_t*)test_data[k], strlen(test_data[k])); crypto_SHA1_Update(&context, (uint8_t*)test_data[k],
SHA1_Final(&context, digest); strlen(test_data[k]));
crypto_SHA1_Final(&context, digest);
digest_to_hex(digest, output); digest_to_hex(digest, output);
if (strcmp(output, test_results[k])) { if (strcmp(output, test_results[k])) {
@@ -380,10 +383,10 @@ int main(int argc, char** argv)
} }
} }
/* million 'a' vector we feed separately */ /* million 'a' vector we feed separately */
SHA1_Init(&context); crypto_SHA1_Init(&context);
for (k = 0; k < 1000000; k++) for (k = 0; k < 1000000; k++)
SHA1_Update(&context, (uint8_t*)"a", 1); crypto_SHA1_Update(&context, (uint8_t*)"a", 1);
SHA1_Final(&context, digest); crypto_SHA1_Final(&context, digest);
digest_to_hex(digest, output); digest_to_hex(digest, output);
if (strcmp(output, test_results[2])) { if (strcmp(output, test_results[2])) {
fprintf(stdout, "FAIL\n"); fprintf(stdout, "FAIL\n");

View File

@@ -5,8 +5,8 @@
* SHA-1 hash API. * SHA-1 hash API.
*/ */
#ifndef __SHA1_H #ifndef __LIBSTROPHE_SHA1_H__
#define __SHA1_H #define __LIBSTROPHE_SHA1_H__
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
@@ -23,13 +23,14 @@ typedef struct {
#define SHA1_DIGEST_SIZE 20 #define SHA1_DIGEST_SIZE 20
void SHA1_Init(SHA1_CTX* context); void crypto_SHA1_Init(SHA1_CTX* context);
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,
void SHA1_Final(SHA1_CTX* context, uint8_t* digest); const size_t len);
void SHA1(const uint8_t* data, size_t len, uint8_t* digest); 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 #ifdef __cplusplus
} }
#endif #endif
#endif /* __SHA1_H */ #endif /* __LIBSTROPHE_SHA1_H__ */