From f86e0285497631defbfa7a1e7bcaf38b1b47d5cf Mon Sep 17 00:00:00 2001 From: bcook Date: Thu, 22 Jan 2015 03:56:27 +0000 Subject: [PATCH] Assume that the size of a pointer will not change at runtime. Change the runtime check for whether a long is smaller than a pointer to a compile-time check. Replace the silly hash for LLP64 platforms. ok tedu@ --- lib/libcrypto/cryptlib.c | 43 +++++++++++--------------------- lib/libssl/src/crypto/cryptlib.c | 43 +++++++++++--------------------- 2 files changed, 30 insertions(+), 56 deletions(-) diff --git a/lib/libcrypto/cryptlib.c b/lib/libcrypto/cryptlib.c index 000f76a6a23..dc92ac89fea 100644 --- a/lib/libcrypto/cryptlib.c +++ b/lib/libcrypto/cryptlib.c @@ -1,4 +1,4 @@ -/* $OpenBSD: cryptlib.c,v 1.33 2014/07/22 02:21:20 beck Exp $ */ +/* $OpenBSD: cryptlib.c,v 1.34 2015/01/22 03:56:27 bcook Exp $ */ /* ==================================================================== * Copyright (c) 1998-2006 The OpenSSL Project. All rights reserved. * @@ -124,6 +124,7 @@ #include #include #include +#include DECLARE_STACK_OF(CRYPTO_dynlock) @@ -425,39 +426,25 @@ CRYPTO_THREADID_set_numeric(CRYPTO_THREADID *id, unsigned long val) id->val = val; } -static const unsigned char hash_coeffs[] = { 3, 5, 7, 11, 13, 17, 19, 23 }; void CRYPTO_THREADID_set_pointer(CRYPTO_THREADID *id, void *ptr) { - unsigned char *dest = (void *)&id->val; - unsigned int accum = 0; - unsigned char dnum = sizeof(id->val); - memset(id, 0, sizeof(*id)); id->ptr = ptr; - if (sizeof(id->val) >= sizeof(id->ptr)) { - /* 'ptr' can be embedded in 'val' without loss of uniqueness */ - id->val = (unsigned long)id->ptr; - return; - } - /* hash ptr ==> val. Each byte of 'val' gets the mod-256 total of a - * linear function over the bytes in 'ptr', the co-efficients of which - * are a sequence of low-primes (hash_coeffs is an 8-element cycle) - - * the starting prime for the sequence varies for each byte of 'val' - * (unique polynomials unless pointers are >64-bit). For added spice, - * the totals accumulate rather than restarting from zero, and the index - * of the 'val' byte is added each time (position dependence). If I was - * a black-belt, I'd scan big-endian pointers in reverse to give - * low-order bits more play, but this isn't crypto and I'd prefer nobody - * mistake it as such. Plus I'm lazy. */ - while (dnum--) { - const unsigned char *src = (void *)&id->ptr; - unsigned char snum = sizeof(id->ptr); - while (snum--) - accum += *(src++) * hash_coeffs[(snum + dnum) & 7]; - accum += dnum; - *(dest++) = accum & 255; +#if LONG_MAX >= INTPTR_MAX + /*s u 'ptr' can be embedded in 'val' without loss of uniqueness */ + id->val = (unsigned long)id->ptr; +#else + { + SHA256_CTX ctx; + uint8_t results[SHA256_DIGEST_LENGTH]; + + SHA256_Init(&ctx); + SHA256_Update(&ctx, (char *)(&id->ptr), sizeof(id->ptr)); + SHA256_Final(results, &ctx); + memcpy(&id->val, results, sizeof(id->val)); } +#endif } int diff --git a/lib/libssl/src/crypto/cryptlib.c b/lib/libssl/src/crypto/cryptlib.c index 000f76a6a23..dc92ac89fea 100644 --- a/lib/libssl/src/crypto/cryptlib.c +++ b/lib/libssl/src/crypto/cryptlib.c @@ -1,4 +1,4 @@ -/* $OpenBSD: cryptlib.c,v 1.33 2014/07/22 02:21:20 beck Exp $ */ +/* $OpenBSD: cryptlib.c,v 1.34 2015/01/22 03:56:27 bcook Exp $ */ /* ==================================================================== * Copyright (c) 1998-2006 The OpenSSL Project. All rights reserved. * @@ -124,6 +124,7 @@ #include #include #include +#include DECLARE_STACK_OF(CRYPTO_dynlock) @@ -425,39 +426,25 @@ CRYPTO_THREADID_set_numeric(CRYPTO_THREADID *id, unsigned long val) id->val = val; } -static const unsigned char hash_coeffs[] = { 3, 5, 7, 11, 13, 17, 19, 23 }; void CRYPTO_THREADID_set_pointer(CRYPTO_THREADID *id, void *ptr) { - unsigned char *dest = (void *)&id->val; - unsigned int accum = 0; - unsigned char dnum = sizeof(id->val); - memset(id, 0, sizeof(*id)); id->ptr = ptr; - if (sizeof(id->val) >= sizeof(id->ptr)) { - /* 'ptr' can be embedded in 'val' without loss of uniqueness */ - id->val = (unsigned long)id->ptr; - return; - } - /* hash ptr ==> val. Each byte of 'val' gets the mod-256 total of a - * linear function over the bytes in 'ptr', the co-efficients of which - * are a sequence of low-primes (hash_coeffs is an 8-element cycle) - - * the starting prime for the sequence varies for each byte of 'val' - * (unique polynomials unless pointers are >64-bit). For added spice, - * the totals accumulate rather than restarting from zero, and the index - * of the 'val' byte is added each time (position dependence). If I was - * a black-belt, I'd scan big-endian pointers in reverse to give - * low-order bits more play, but this isn't crypto and I'd prefer nobody - * mistake it as such. Plus I'm lazy. */ - while (dnum--) { - const unsigned char *src = (void *)&id->ptr; - unsigned char snum = sizeof(id->ptr); - while (snum--) - accum += *(src++) * hash_coeffs[(snum + dnum) & 7]; - accum += dnum; - *(dest++) = accum & 255; +#if LONG_MAX >= INTPTR_MAX + /*s u 'ptr' can be embedded in 'val' without loss of uniqueness */ + id->val = (unsigned long)id->ptr; +#else + { + SHA256_CTX ctx; + uint8_t results[SHA256_DIGEST_LENGTH]; + + SHA256_Init(&ctx); + SHA256_Update(&ctx, (char *)(&id->ptr), sizeof(id->ptr)); + SHA256_Final(results, &ctx); + memcpy(&id->val, results, sizeof(id->val)); } +#endif } int -- 2.20.1