From c34714da9738d07fe979cbb00940fa26db35cde7 Mon Sep 17 00:00:00 2001 From: guenther <> Date: Fri, 11 Sep 2015 09:18:27 +0000 Subject: [PATCH] Wrap blowfish, sha*, md5, and rmd160 so that internal calls go direct ok deraadt@ --- src/lib/libc/crypt/blowfish.c | 15 ++++++++++++++- src/lib/libc/hash/helper.c | 6 +++++- src/lib/libc/hash/md5.c | 7 ++++++- src/lib/libc/hash/rmd160.c | 5 +++++ src/lib/libc/hash/sha1.c | 7 ++++++- src/lib/libc/hash/sha2.c | 34 +++++++++++++++++++++++++++------- src/lib/libc/hash/siphash.c | 7 ++++++- 7 files changed, 69 insertions(+), 12 deletions(-) diff --git a/src/lib/libc/crypt/blowfish.c b/src/lib/libc/crypt/blowfish.c index c337df8a..a658e602 100644 --- a/src/lib/libc/crypt/blowfish.c +++ b/src/lib/libc/crypt/blowfish.c @@ -1,4 +1,4 @@ -/* $OpenBSD: blowfish.c,v 1.18 2004/11/02 17:23:26 hshoexer Exp $ */ +/* $OpenBSD: blowfish.c,v 1.19 2015/09/11 09:18:27 guenther Exp $ */ /* * Blowfish block cipher for OpenBSD * Copyright 1997 Niels Provos @@ -87,6 +87,7 @@ Blowfish_encipher(blf_ctx *c, u_int32_t *xl, u_int32_t *xr) *xl = Xr ^ p[17]; *xr = Xl; } +DEF_WEAK(Blowfish_encipher); void Blowfish_decipher(blf_ctx *c, u_int32_t *xl, u_int32_t *xr) @@ -112,6 +113,7 @@ Blowfish_decipher(blf_ctx *c, u_int32_t *xl, u_int32_t *xr) *xl = Xr ^ p[0]; *xr = Xl; } +DEF_WEAK(Blowfish_decipher); void Blowfish_initstate(blf_ctx *c) @@ -391,6 +393,7 @@ Blowfish_initstate(blf_ctx *c) *c = initstate; } +DEF_WEAK(Blowfish_initstate); u_int32_t Blowfish_stream2word(const u_int8_t *data, u_int16_t databytes, @@ -412,6 +415,7 @@ Blowfish_stream2word(const u_int8_t *data, u_int16_t databytes, *current = j; return temp; } +DEF_WEAK(Blowfish_stream2word); void Blowfish_expand0state(blf_ctx *c, const u_int8_t *key, u_int16_t keybytes) @@ -449,6 +453,7 @@ Blowfish_expand0state(blf_ctx *c, const u_int8_t *key, u_int16_t keybytes) } } } +DEF_WEAK(Blowfish_expand0state); void @@ -493,6 +498,7 @@ Blowfish_expandstate(blf_ctx *c, const u_int8_t *data, u_int16_t databytes, } } +DEF_WEAK(Blowfish_expandstate); void blf_key(blf_ctx *c, const u_int8_t *k, u_int16_t len) @@ -503,6 +509,7 @@ blf_key(blf_ctx *c, const u_int8_t *k, u_int16_t len) /* Transform S-boxes and subkeys with key */ Blowfish_expand0state(c, k, len); } +DEF_WEAK(blf_key); void blf_enc(blf_ctx *c, u_int32_t *data, u_int16_t blocks) @@ -516,6 +523,7 @@ blf_enc(blf_ctx *c, u_int32_t *data, u_int16_t blocks) d += 2; } } +DEF_WEAK(blf_enc); void blf_dec(blf_ctx *c, u_int32_t *data, u_int16_t blocks) @@ -529,6 +537,7 @@ blf_dec(blf_ctx *c, u_int32_t *data, u_int16_t blocks) d += 2; } } +DEF_WEAK(blf_dec); void blf_ecb_encrypt(blf_ctx *c, u_int8_t *data, u_int32_t len) @@ -551,6 +560,7 @@ blf_ecb_encrypt(blf_ctx *c, u_int8_t *data, u_int32_t len) data += 8; } } +DEF_WEAK(blf_ecb_encrypt); void blf_ecb_decrypt(blf_ctx *c, u_int8_t *data, u_int32_t len) @@ -573,6 +583,7 @@ blf_ecb_decrypt(blf_ctx *c, u_int8_t *data, u_int32_t len) data += 8; } } +DEF_WEAK(blf_ecb_decrypt); void blf_cbc_encrypt(blf_ctx *c, u_int8_t *iv, u_int8_t *data, u_int32_t len) @@ -598,6 +609,7 @@ blf_cbc_encrypt(blf_ctx *c, u_int8_t *iv, u_int8_t *data, u_int32_t len) data += 8; } } +DEF_WEAK(blf_cbc_encrypt); void blf_cbc_decrypt(blf_ctx *c, u_int8_t *iva, u_int8_t *data, u_int32_t len) @@ -639,6 +651,7 @@ blf_cbc_decrypt(blf_ctx *c, u_int8_t *iva, u_int8_t *data, u_int32_t len) for (j = 0; j < 8; j++) data[j] ^= iva[j]; } +DEF_WEAK(blf_cbc_decrypt); #if 0 void diff --git a/src/lib/libc/hash/helper.c b/src/lib/libc/hash/helper.c index ee174176..b5a93866 100644 --- a/src/lib/libc/hash/helper.c +++ b/src/lib/libc/hash/helper.c @@ -1,4 +1,4 @@ -/* $OpenBSD: helper.c,v 1.13 2015/01/16 16:48:51 deraadt Exp $ */ +/* $OpenBSD: helper.c,v 1.14 2015/09/11 09:18:27 guenther Exp $ */ /* * Copyright (c) 2000 Poul-Henning Kamp @@ -55,6 +55,7 @@ HASHEnd(HASH_CTX *ctx, char *buf) explicit_bzero(digest, sizeof(digest)); return (buf); } +DEF_WEAK(HASHEnd); char * HASHFileChunk(const char *filename, char *buf, off_t off, off_t len) @@ -92,12 +93,14 @@ HASHFileChunk(const char *filename, char *buf, off_t off, off_t len) errno = save_errno; return (nr < 0 ? NULL : HASHEnd(&ctx, buf)); } +DEF_WEAK(HASHFileChunk); char * HASHFile(const char *filename, char *buf) { return (HASHFileChunk(filename, buf, (off_t)0, (off_t)0)); } +DEF_WEAK(HASHFile); char * HASHData(const u_char *data, size_t len, char *buf) @@ -108,3 +111,4 @@ HASHData(const u_char *data, size_t len, char *buf) HASHUpdate(&ctx, data, len); return (HASHEnd(&ctx, buf)); } +DEF_WEAK(HASHData); diff --git a/src/lib/libc/hash/md5.c b/src/lib/libc/hash/md5.c index 2428ed52..435e5ac8 100644 --- a/src/lib/libc/hash/md5.c +++ b/src/lib/libc/hash/md5.c @@ -1,4 +1,4 @@ -/* $OpenBSD: md5.c,v 1.10 2015/01/15 13:05:59 millert Exp $ */ +/* $OpenBSD: md5.c,v 1.11 2015/09/11 09:18:27 guenther Exp $ */ /* * This code implements the MD5 message-digest algorithm. @@ -56,6 +56,7 @@ MD5Init(MD5_CTX *ctx) ctx->state[2] = 0x98badcfe; ctx->state[3] = 0x10325476; } +DEF_WEAK(MD5Init); /* * Update context to reflect the concatenation of another buffer full @@ -94,6 +95,7 @@ MD5Update(MD5_CTX *ctx, const unsigned char *input, size_t len) if (len != 0) memcpy(ctx->buffer + have, input, len); } +DEF_WEAK(MD5Update); /* * Pad pad to 64-byte boundary with the bit pattern @@ -116,6 +118,7 @@ MD5Pad(MD5_CTX *ctx) MD5Update(ctx, PADDING, padlen - 8); /* padlen - 8 <= 64 */ MD5Update(ctx, count, 8); } +DEF_WEAK(MD5Pad); /* * Final wrapup--call MD5Pad, fill in digest and zero out ctx. @@ -130,6 +133,7 @@ MD5Final(unsigned char digest[MD5_DIGEST_LENGTH], MD5_CTX *ctx) PUT_32BIT_LE(digest + i * 4, ctx->state[i]); explicit_bzero(ctx, sizeof(*ctx)); } +DEF_WEAK(MD5Final); /* The four core functions - F1 is optimized somewhat */ @@ -244,3 +248,4 @@ MD5Transform(u_int32_t state[4], const u_int8_t block[MD5_BLOCK_LENGTH]) state[2] += c; state[3] += d; } +DEF_WEAK(MD5Transform); diff --git a/src/lib/libc/hash/rmd160.c b/src/lib/libc/hash/rmd160.c index 3abc7cc7..3bf58012 100644 --- a/src/lib/libc/hash/rmd160.c +++ b/src/lib/libc/hash/rmd160.c @@ -98,6 +98,7 @@ RMD160Init(RMD160_CTX *ctx) ctx->state[3] = H3; ctx->state[4] = H4; } +DEF_WEAK(RMD160Init); void RMD160Update(RMD160_CTX *ctx, const u_int8_t *input, size_t len) @@ -125,6 +126,7 @@ RMD160Update(RMD160_CTX *ctx, const u_int8_t *input, size_t len) if (off < len) memcpy(ctx->buffer + have, input+off, len-off); } +DEF_WEAK(RMD160Update); void RMD160Pad(RMD160_CTX *ctx) @@ -144,6 +146,7 @@ RMD160Pad(RMD160_CTX *ctx) RMD160Update(ctx, PADDING, padlen - 8); /* padlen - 8 <= 64 */ RMD160Update(ctx, size, 8); } +DEF_WEAK(RMD160Pad); void RMD160Final(u_int8_t digest[RMD160_DIGEST_LENGTH], RMD160_CTX *ctx) @@ -155,6 +158,7 @@ RMD160Final(u_int8_t digest[RMD160_DIGEST_LENGTH], RMD160_CTX *ctx) PUT_32BIT_LE(digest + i*4, ctx->state[i]); explicit_bzero(ctx, sizeof (*ctx)); } +DEF_WEAK(RMD160Final); void RMD160Transform(u_int32_t state[5], const u_int8_t block[RMD160_BLOCK_LENGTH]) @@ -367,3 +371,4 @@ RMD160Transform(u_int32_t state[5], const u_int8_t block[RMD160_BLOCK_LENGTH]) state[4] = state[0] + bb + c; state[0] = t; } +DEF_WEAK(RMD160Transform); diff --git a/src/lib/libc/hash/sha1.c b/src/lib/libc/hash/sha1.c index fbaab9ba..4a6cc325 100644 --- a/src/lib/libc/hash/sha1.c +++ b/src/lib/libc/hash/sha1.c @@ -1,4 +1,4 @@ -/* $OpenBSD: sha1.c,v 1.25 2015/01/16 16:48:51 deraadt Exp $ */ +/* $OpenBSD: sha1.c,v 1.26 2015/09/11 09:18:27 guenther Exp $ */ /* * SHA-1 in C @@ -98,6 +98,7 @@ SHA1Transform(u_int32_t state[5], const u_int8_t buffer[SHA1_BLOCK_LENGTH]) /* Wipe variables */ a = b = c = d = e = 0; } +DEF_WEAK(SHA1Transform); /* @@ -115,6 +116,7 @@ SHA1Init(SHA1_CTX *context) context->state[3] = 0x10325476; context->state[4] = 0xC3D2E1F0; } +DEF_WEAK(SHA1Init); /* @@ -138,6 +140,7 @@ SHA1Update(SHA1_CTX *context, const u_int8_t *data, size_t len) } (void)memcpy(&context->buffer[j], &data[i], len - i); } +DEF_WEAK(SHA1Update); /* @@ -158,6 +161,7 @@ SHA1Pad(SHA1_CTX *context) SHA1Update(context, (u_int8_t *)"\0", 1); SHA1Update(context, finalcount, 8); /* Should cause a SHA1Transform() */ } +DEF_WEAK(SHA1Pad); void SHA1Final(u_int8_t digest[SHA1_DIGEST_LENGTH], SHA1_CTX *context) @@ -171,3 +175,4 @@ SHA1Final(u_int8_t digest[SHA1_DIGEST_LENGTH], SHA1_CTX *context) } explicit_bzero(context, sizeof(*context)); } +DEF_WEAK(SHA1Final); diff --git a/src/lib/libc/hash/sha2.c b/src/lib/libc/hash/sha2.c index 4842e429..16486bcb 100644 --- a/src/lib/libc/hash/sha2.c +++ b/src/lib/libc/hash/sha2.c @@ -1,4 +1,4 @@ -/* $OpenBSD: sha2.c,v 1.23 2015/01/15 13:05:59 millert Exp $ */ +/* $OpenBSD: sha2.c,v 1.24 2015/09/11 09:18:27 guenther Exp $ */ /* * FILE: sha2.c @@ -297,10 +297,14 @@ SHA224Init(SHA2_CTX *context) memset(context->buffer, 0, sizeof(context->buffer)); context->bitcount[0] = 0; } +DEF_WEAK(SHA224Init); -__weak_alias(SHA224Transform, SHA256Transform); -__weak_alias(SHA224Update, SHA256Update); -__weak_alias(SHA224Pad, SHA256Pad); +MAKE_CLONE(SHA224Transform, SHA256Transform); +MAKE_CLONE(SHA224Update, SHA256Update); +MAKE_CLONE(SHA224Pad, SHA256Pad); +DEF_WEAK(SHA224Transform); +DEF_WEAK(SHA224Update); +DEF_WEAK(SHA224Pad); void SHA224Final(u_int8_t digest[SHA224_DIGEST_LENGTH], SHA2_CTX *context) @@ -318,6 +322,7 @@ SHA224Final(u_int8_t digest[SHA224_DIGEST_LENGTH], SHA2_CTX *context) #endif explicit_bzero(context, sizeof(*context)); } +DEF_WEAK(SHA224Final); #endif /* !defined(SHA2_SMALL) */ /*** SHA-256: *********************************************************/ @@ -329,6 +334,7 @@ SHA256Init(SHA2_CTX *context) memset(context->buffer, 0, sizeof(context->buffer)); context->bitcount[0] = 0; } +DEF_WEAK(SHA256Init); #ifdef SHA2_UNROLL_TRANSFORM @@ -487,6 +493,7 @@ SHA256Transform(u_int32_t state[8], const u_int8_t data[SHA256_BLOCK_LENGTH]) } #endif /* SHA2_UNROLL_TRANSFORM */ +DEF_WEAK(SHA256Transform); void SHA256Update(SHA2_CTX *context, const u_int8_t *data, size_t len) @@ -533,6 +540,7 @@ SHA256Update(SHA2_CTX *context, const u_int8_t *data, size_t len) /* Clean up: */ usedspace = freespace = 0; } +DEF_WEAK(SHA256Update); void SHA256Pad(SHA2_CTX *context) @@ -576,6 +584,7 @@ SHA256Pad(SHA2_CTX *context) /* Clean up: */ usedspace = 0; } +DEF_WEAK(SHA256Pad); void SHA256Final(u_int8_t digest[SHA256_DIGEST_LENGTH], SHA2_CTX *context) @@ -593,6 +602,7 @@ SHA256Final(u_int8_t digest[SHA256_DIGEST_LENGTH], SHA2_CTX *context) #endif explicit_bzero(context, sizeof(*context)); } +DEF_WEAK(SHA256Final); /*** SHA-512: *********************************************************/ @@ -604,6 +614,7 @@ SHA512Init(SHA2_CTX *context) memset(context->buffer, 0, sizeof(context->buffer)); context->bitcount[0] = context->bitcount[1] = 0; } +DEF_WEAK(SHA512Init); #ifdef SHA2_UNROLL_TRANSFORM @@ -763,6 +774,7 @@ SHA512Transform(u_int64_t state[8], const u_int8_t data[SHA512_BLOCK_LENGTH]) } #endif /* SHA2_UNROLL_TRANSFORM */ +DEF_WEAK(SHA512Transform); void SHA512Update(SHA2_CTX *context, const u_int8_t *data, size_t len) @@ -809,6 +821,7 @@ SHA512Update(SHA2_CTX *context, const u_int8_t *data, size_t len) /* Clean up: */ usedspace = freespace = 0; } +DEF_WEAK(SHA512Update); void SHA512Pad(SHA2_CTX *context) @@ -852,6 +865,7 @@ SHA512Pad(SHA2_CTX *context) /* Clean up: */ usedspace = 0; } +DEF_WEAK(SHA512Pad); void SHA512Final(u_int8_t digest[SHA512_DIGEST_LENGTH], SHA2_CTX *context) @@ -869,6 +883,7 @@ SHA512Final(u_int8_t digest[SHA512_DIGEST_LENGTH], SHA2_CTX *context) #endif explicit_bzero(context, sizeof(*context)); } +DEF_WEAK(SHA512Final); #if !defined(SHA2_SMALL) @@ -881,10 +896,14 @@ SHA384Init(SHA2_CTX *context) memset(context->buffer, 0, sizeof(context->buffer)); context->bitcount[0] = context->bitcount[1] = 0; } +DEF_WEAK(SHA384Init); -__weak_alias(SHA384Transform, SHA512Transform); -__weak_alias(SHA384Update, SHA512Update); -__weak_alias(SHA384Pad, SHA512Pad); +MAKE_CLONE(SHA384Transform, SHA512Transform); +MAKE_CLONE(SHA384Update, SHA512Update); +MAKE_CLONE(SHA384Pad, SHA512Pad); +DEF_WEAK(SHA384Transform); +DEF_WEAK(SHA384Update); +DEF_WEAK(SHA384Pad); void SHA384Final(u_int8_t digest[SHA384_DIGEST_LENGTH], SHA2_CTX *context) @@ -903,4 +922,5 @@ SHA384Final(u_int8_t digest[SHA384_DIGEST_LENGTH], SHA2_CTX *context) /* Zero out state data */ explicit_bzero(context, sizeof(*context)); } +DEF_WEAK(SHA384Final); #endif /* !defined(SHA2_SMALL) */ diff --git a/src/lib/libc/hash/siphash.c b/src/lib/libc/hash/siphash.c index 1e1b9077..0f056a35 100644 --- a/src/lib/libc/hash/siphash.c +++ b/src/lib/libc/hash/siphash.c @@ -1,4 +1,4 @@ -/* $OpenBSD: siphash.c,v 1.4 2015/02/20 11:51:03 tedu Exp $ */ +/* $OpenBSD: siphash.c,v 1.5 2015/09/11 09:18:27 guenther Exp $ */ /*- * Copyright (c) 2013 Andre Oppermann @@ -68,6 +68,7 @@ SipHash_Init(SIPHASH_CTX *ctx, const SIPHASH_KEY *key) memset(ctx->buf, 0, sizeof(ctx->buf)); ctx->bytes = 0; } +DEF_WEAK(SipHash_Init); void SipHash_Update(SIPHASH_CTX *ctx, int rc, int rf, const void *src, size_t len) @@ -105,6 +106,7 @@ SipHash_Update(SIPHASH_CTX *ctx, int rc, int rf, const void *src, size_t len) if (len > 0) memcpy(&ctx->buf[used], ptr, len); } +DEF_WEAK(SipHash_Update); void SipHash_Final(void *dst, SIPHASH_CTX *ctx, int rc, int rf) @@ -115,6 +117,7 @@ SipHash_Final(void *dst, SIPHASH_CTX *ctx, int rc, int rf) *(uint64_t *)dst = htole64(r); } +DEF_WEAK(SipHash_Final); uint64_t SipHash_End(SIPHASH_CTX *ctx, int rc, int rf) @@ -135,6 +138,7 @@ SipHash_End(SIPHASH_CTX *ctx, int rc, int rf) explicit_bzero(ctx, sizeof(*ctx)); return (r); } +DEF_WEAK(SipHash_End); uint64_t SipHash(const SIPHASH_KEY *key, int rc, int rf, const void *src, size_t len) @@ -145,6 +149,7 @@ SipHash(const SIPHASH_KEY *key, int rc, int rf, const void *src, size_t len) SipHash_Update(&ctx, rc, rf, src, len); return (SipHash_End(&ctx, rc, rf)); } +DEF_WEAK(SipHash); #define SIP_ROTL(x, b) ((x) << (b)) | ( (x) >> (64 - (b)))