From 259a07f169b9a1188c3a9efb3a5fb1fb67b4b04d Mon Sep 17 00:00:00 2001 From: Greg Hudson Date: Fri, 25 Feb 2011 19:53:04 +0000 Subject: [PATCH] Namespace-protect SHA-256 symbols. Build SHA-256 code independently of whether Fortuna was selected. git-svn-id: svn://anonsvn.mit.edu/krb5/trunk@24666 dc483132-0cff-0310-8789-dd5450dbe970 --- src/lib/crypto/builtin/sha2/sha2.h | 50 ++++---------------------- src/lib/crypto/builtin/sha2/sha256.c | 41 +++++++++++++++------ src/lib/crypto/builtin/sha2/t_sha256.c | 19 +++------- src/lib/crypto/krb/prng_fortuna.c | 14 ++++---- src/lib/crypto/libk5crypto.exports | 6 ++-- src/lib/crypto/nss/stubs.c | 12 +++---- src/lib/crypto/openssl/sha2/sha2.h | 6 ++-- src/lib/crypto/openssl/stubs.c | 12 +++---- 8 files changed, 68 insertions(+), 92 deletions(-) diff --git a/src/lib/crypto/builtin/sha2/sha2.h b/src/lib/crypto/builtin/sha2/sha2.h index 0cff88f4a..6c743e122 100644 --- a/src/lib/crypto/builtin/sha2/sha2.h +++ b/src/lib/crypto/builtin/sha2/sha2.h @@ -1,4 +1,5 @@ /* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */ +/* lib/crypto/builtin/sha2/sha2.h - SHA-256 declarations */ /* * Copyright (c) 1995 - 2001 Kungliga Tekniska Högskolan * (Royal Institute of Technology, Stockholm, Sweden). @@ -32,47 +33,10 @@ * SUCH DAMAGE. */ -/* $Id$ */ - -#ifndef HEIM_SHA_H -#define HEIM_SHA_H 1 +#ifndef SHA2_H +#define SHA2_H 1 #include -/* -#include -#include -#include -#ifdef KRB5 -#include -#endif -*/ -#ifndef min -#define min(a,b) (((a)>(b))?(b):(a)) -#endif - -/* Vector Crays doesn't have a good 32-bit type, or more precisely, - * int32_t as defined by isn't 32 bits, and we don't - * want to depend in being able to redefine this type. To cope with - * this we have to clamp the result in some places to [0,2^32); no - * need to do this on other machines. Did I say this was a mess? - */ - -#ifdef _CRAY -#define CRAYFIX(X) ((X) & 0xffffffff) -#else -#define CRAYFIX(X) (X) -#endif - -static inline uint32_t -cshift (uint32_t x, unsigned int n) -{ - x = CRAYFIX(x); - return CRAYFIX((x << n) | (x >> (32 - n))); -} - -/* - * SHA-2 256 - */ #define SHA256_DIGEST_LENGTH 32 @@ -84,8 +48,8 @@ struct sha256state { typedef struct sha256state SHA256_CTX; -void sha2Init (SHA256_CTX *); -void sha2Update (SHA256_CTX *, const void *, size_t); -void sha2Final (void *, SHA256_CTX *); +void k5_sha256_init(SHA256_CTX *); +void k5_sha256_update(SHA256_CTX *, const void *, size_t); +void k5_sha256_final(void *, SHA256_CTX *); -#endif /* HEIM_SHA_H */ +#endif /* SHA2_H */ diff --git a/src/lib/crypto/builtin/sha2/sha256.c b/src/lib/crypto/builtin/sha2/sha256.c index fb66bff70..23b1a26c0 100644 --- a/src/lib/crypto/builtin/sha2/sha256.c +++ b/src/lib/crypto/builtin/sha2/sha256.c @@ -1,5 +1,5 @@ /* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */ -/* lib/crypto/builtin/sha256.c */ +/* lib/crypto/builtin/sha2/sha256.c - SHA-256 implementation */ /* * Copyright (c) 2006 Kungliga Tekniska Högskolan * (Royal Institute of Technology, Stockholm, Sweden). @@ -36,7 +36,29 @@ #include #include "sha2.h" -#ifdef FORTUNA +#ifndef min +#define min(a,b) (((a)>(b))?(b):(a)) +#endif + +/* Vector Crays doesn't have a good 32-bit type, or more precisely, + * int32_t as defined by isn't 32 bits, and we don't + * want to depend in being able to redefine this type. To cope with + * this we have to clamp the result in some places to [0,2^32); no + * need to do this on other machines. Did I say this was a mess? + */ + +#ifdef _CRAY +#define CRAYFIX(X) ((X) & 0xffffffff) +#else +#define CRAYFIX(X) (X) +#endif + +static inline uint32_t +cshift (uint32_t x, unsigned int n) +{ + x = CRAYFIX(x); + return CRAYFIX((x << n) | (x >> (32 - n))); +} #define Ch(x,y,z) (((x) & (y)) ^ ((~(x)) & (z))) #define Maj(x,y,z) (((x) & (y)) ^ ((x) & (z)) ^ ((y) & (z))) @@ -77,7 +99,7 @@ static const uint32_t constant_256[64] = { }; void -sha2Init (SHA256_CTX *m) +k5_sha256_init(SHA256_CTX *m) { m->sz[0] = 0; m->sz[1] = 0; @@ -92,7 +114,7 @@ sha2Init (SHA256_CTX *m) } static void -calc (SHA256_CTX *m, uint32_t *in) +calc(SHA256_CTX *m, uint32_t *in) { uint32_t AA, BB, CC, DD, EE, FF, GG, HH; uint32_t data[64]; @@ -145,7 +167,7 @@ calc (SHA256_CTX *m, uint32_t *in) #if !defined(WORDS_BIGENDIAN) || defined(_CRAY) static inline uint32_t -swap_uint32_t (uint32_t t) +swap_uint32_t(uint32_t t) { #define ROL(x,n) ((x)<<(n))|((x)>>(32-(n))) uint32_t temp1, temp2; @@ -159,13 +181,13 @@ swap_uint32_t (uint32_t t) } #endif -struct x32{ +struct x32 { unsigned int a:32; unsigned int b:32; }; void -sha2Update (SHA256_CTX *m, const void *v, size_t len) +k5_sha256_update(SHA256_CTX *m, const void *v, size_t len) { const unsigned char *p = v; size_t old_sz = m->sz[0]; @@ -200,7 +222,7 @@ sha2Update (SHA256_CTX *m, const void *v, size_t len) } void -sha2Final (void *res, SHA256_CTX *m) +k5_sha256_final(void *res, SHA256_CTX *m) { unsigned char zeros[72]; unsigned offset = (m->sz[0] / 8) % 64; @@ -216,7 +238,7 @@ sha2Final (void *res, SHA256_CTX *m) zeros[dstart+2] = (m->sz[1] >> 8) & 0xff; zeros[dstart+1] = (m->sz[1] >> 16) & 0xff; zeros[dstart+0] = (m->sz[1] >> 24) & 0xff; - sha2Update (m, zeros, dstart + 8); + k5_sha256_update(m, zeros, dstart + 8); { int i; unsigned char *r = (unsigned char*)res; @@ -229,4 +251,3 @@ sha2Final (void *res, SHA256_CTX *m) } } } -#endif /* FORTUNA */ diff --git a/src/lib/crypto/builtin/sha2/t_sha256.c b/src/lib/crypto/builtin/sha2/t_sha256.c index bcad91093..4facb0f31 100644 --- a/src/lib/crypto/builtin/sha2/t_sha256.c +++ b/src/lib/crypto/builtin/sha2/t_sha256.c @@ -36,15 +36,6 @@ #include #include "sha2.h" -#ifndef FORTUNA -int -main (void) -{ - return 0; -} - -#else - #define ONE_MILLION_A "one million a's" struct test { @@ -81,18 +72,18 @@ main (void) for (t = tests; t->str; ++t) { - sha2Init(ctx); + k5_sha256_init(ctx); if(strcmp(t->str, ONE_MILLION_A) == 0) { int i; memset(buf, 'a', sizeof(buf)); for(i = 0; i < 1000; i++) { - sha2Update(ctx, buf, sizeof(buf)); + k5_sha256_update(ctx, buf, sizeof(buf)); } } else { - sha2Update(ctx, (unsigned char *)t->str, strlen(t->str)); + k5_sha256_update(ctx, (unsigned char *)t->str, strlen(t->str)); } - sha2Final(res, ctx); + k5_sha256_final(res, ctx); if (memcmp (res, t->hash, SHA256_DIGEST_LENGTH) != 0) { int i; @@ -123,4 +114,4 @@ main (void) printf ("success\n"); return 0; } -#endif /* FORTUNA */ + diff --git a/src/lib/crypto/krb/prng_fortuna.c b/src/lib/crypto/krb/prng_fortuna.c index 669a9163d..3acf96b3a 100644 --- a/src/lib/crypto/krb/prng_fortuna.c +++ b/src/lib/crypto/krb/prng_fortuna.c @@ -135,25 +135,25 @@ shad256_init(SHA256_CTX *ctx) /* Initialize the inner SHA-256 context and update it with a zero block. */ memset(zero, 0, sizeof(zero)); - sha2Init(ctx); - sha2Update(ctx, zero, sizeof(zero)); + k5_sha256_init(ctx); + k5_sha256_update(ctx, zero, sizeof(zero)); } static void shad256_update(SHA256_CTX *ctx, const unsigned char *data, int len) { /* Feed the input to the inner SHA-256 context. */ - sha2Update(ctx, data, len); + k5_sha256_update(ctx, data, len); } static void shad256_result(SHA256_CTX *ctx, unsigned char *dst) { /* Finalize the inner context, then feed the result back through SHA256. */ - sha2Final(dst, ctx); - sha2Init(ctx); - sha2Update(ctx, dst, SHA256_HASHSIZE); - sha2Final(dst, ctx); + k5_sha256_final(dst, ctx); + k5_sha256_init(ctx); + k5_sha256_update(ctx, dst, SHA256_HASHSIZE); + k5_sha256_final(dst, ctx); } /* Initialize state. */ diff --git a/src/lib/crypto/libk5crypto.exports b/src/lib/crypto/libk5crypto.exports index 810c90df8..6307303cf 100644 --- a/src/lib/crypto/libk5crypto.exports +++ b/src/lib/crypto/libk5crypto.exports @@ -102,8 +102,8 @@ krb5int_enc_aes256 krb5int_enc_camellia128 krb5int_enc_camellia256 krb5int_derive_key -sha2Final -sha2Init -sha2Update krb5int_aes_enc_blk krb5int_aes_enc_key +k5_sha256_final +k5_sha256_init +k5_sha256_update diff --git a/src/lib/crypto/nss/stubs.c b/src/lib/crypto/nss/stubs.c index 14ce87406..034e7e6f0 100644 --- a/src/lib/crypto/nss/stubs.c +++ b/src/lib/crypto/nss/stubs.c @@ -38,9 +38,9 @@ */ void krb5int_aes_enc_blk(void); void krb5int_aes_enc_key(void); -void sha2Final(void); -void sha2Init(void); -void sha2Update(void); +void k5_sha256_final(void); +void k5_sha256_init(void); +void k5_sha256_update(void); void krb5int_aes_enc_blk(void) { @@ -52,17 +52,17 @@ void krb5int_aes_enc_key(void) abort(); } -void sha2Final(void) +void k5_sha256_final(void) { abort(); } -void sha2Init(void) +void k5_sha256_init(void) { abort(); } -void sha2Update(void) +void k5_sha256_update(void) { abort(); } diff --git a/src/lib/crypto/openssl/sha2/sha2.h b/src/lib/crypto/openssl/sha2/sha2.h index 0f61d5dc4..afc1d4660 100644 --- a/src/lib/crypto/openssl/sha2/sha2.h +++ b/src/lib/crypto/openssl/sha2/sha2.h @@ -31,8 +31,8 @@ #define _SHA2_DEFINED -#define sha2Init SHA256_Init -#define sha2Update SHA256_Update -#define sha2Final SHA256_Final +#define k5_sha256_init SHA256_Init +#define k5_sha256_update SHA256_Update +#define k5_sha256_final SHA256_Final #endif /* _SHA2_DEFINED */ diff --git a/src/lib/crypto/openssl/stubs.c b/src/lib/crypto/openssl/stubs.c index 220df388a..1ee4d7b35 100644 --- a/src/lib/crypto/openssl/stubs.c +++ b/src/lib/crypto/openssl/stubs.c @@ -39,9 +39,9 @@ */ void krb5int_aes_enc_blk(void); void krb5int_aes_enc_key(void); -void sha2Final(void); -void sha2Init(void); -void sha2Update(void); +void k5_sha256_final(void); +void k5_sha256_init(void); +void k5_sha256_update(void); void krb5int_aes_enc_blk(void) { @@ -53,17 +53,17 @@ void krb5int_aes_enc_key(void) abort(); } -void sha2Final(void) +void k5_sha256_final(void) { abort(); } -void sha2Init(void) +void k5_sha256_init(void) { abort(); } -void sha2Update(void) +void k5_sha256_update(void) { abort(); } -- 2.26.2