diff options
author | bcook <> | 2015-01-22 03:56:27 +0000 |
---|---|---|
committer | bcook <> | 2015-01-22 03:56:27 +0000 |
commit | d0c1b125a8d559134ebf2f9e48bceedaafc58367 (patch) | |
tree | b7be1e46dbc485da0d46a76d787588547ccbb609 | |
parent | e1dc98b778f8f1ba2eda4462bba9385d0b739e6b (diff) | |
download | openbsd-d0c1b125a8d559134ebf2f9e48bceedaafc58367.tar.gz openbsd-d0c1b125a8d559134ebf2f9e48bceedaafc58367.tar.bz2 openbsd-d0c1b125a8d559134ebf2f9e48bceedaafc58367.zip |
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@
-rw-r--r-- | src/lib/libcrypto/cryptlib.c | 43 | ||||
-rw-r--r-- | src/lib/libssl/src/crypto/cryptlib.c | 43 |
2 files changed, 30 insertions, 56 deletions
diff --git a/src/lib/libcrypto/cryptlib.c b/src/lib/libcrypto/cryptlib.c index 000f76a6a2..dc92ac89fe 100644 --- a/src/lib/libcrypto/cryptlib.c +++ b/src/lib/libcrypto/cryptlib.c | |||
@@ -1,4 +1,4 @@ | |||
1 | /* $OpenBSD: cryptlib.c,v 1.33 2014/07/22 02:21:20 beck Exp $ */ | 1 | /* $OpenBSD: cryptlib.c,v 1.34 2015/01/22 03:56:27 bcook Exp $ */ |
2 | /* ==================================================================== | 2 | /* ==================================================================== |
3 | * Copyright (c) 1998-2006 The OpenSSL Project. All rights reserved. | 3 | * Copyright (c) 1998-2006 The OpenSSL Project. All rights reserved. |
4 | * | 4 | * |
@@ -124,6 +124,7 @@ | |||
124 | #include <openssl/buffer.h> | 124 | #include <openssl/buffer.h> |
125 | #include <openssl/err.h> | 125 | #include <openssl/err.h> |
126 | #include <openssl/safestack.h> | 126 | #include <openssl/safestack.h> |
127 | #include <openssl/sha.h> | ||
127 | 128 | ||
128 | DECLARE_STACK_OF(CRYPTO_dynlock) | 129 | DECLARE_STACK_OF(CRYPTO_dynlock) |
129 | 130 | ||
@@ -425,39 +426,25 @@ CRYPTO_THREADID_set_numeric(CRYPTO_THREADID *id, unsigned long val) | |||
425 | id->val = val; | 426 | id->val = val; |
426 | } | 427 | } |
427 | 428 | ||
428 | static const unsigned char hash_coeffs[] = { 3, 5, 7, 11, 13, 17, 19, 23 }; | ||
429 | void | 429 | void |
430 | CRYPTO_THREADID_set_pointer(CRYPTO_THREADID *id, void *ptr) | 430 | CRYPTO_THREADID_set_pointer(CRYPTO_THREADID *id, void *ptr) |
431 | { | 431 | { |
432 | unsigned char *dest = (void *)&id->val; | ||
433 | unsigned int accum = 0; | ||
434 | unsigned char dnum = sizeof(id->val); | ||
435 | |||
436 | memset(id, 0, sizeof(*id)); | 432 | memset(id, 0, sizeof(*id)); |
437 | id->ptr = ptr; | 433 | id->ptr = ptr; |
438 | if (sizeof(id->val) >= sizeof(id->ptr)) { | 434 | #if LONG_MAX >= INTPTR_MAX |
439 | /* 'ptr' can be embedded in 'val' without loss of uniqueness */ | 435 | /*s u 'ptr' can be embedded in 'val' without loss of uniqueness */ |
440 | id->val = (unsigned long)id->ptr; | 436 | id->val = (unsigned long)id->ptr; |
441 | return; | 437 | #else |
442 | } | 438 | { |
443 | /* hash ptr ==> val. Each byte of 'val' gets the mod-256 total of a | 439 | SHA256_CTX ctx; |
444 | * linear function over the bytes in 'ptr', the co-efficients of which | 440 | uint8_t results[SHA256_DIGEST_LENGTH]; |
445 | * are a sequence of low-primes (hash_coeffs is an 8-element cycle) - | 441 | |
446 | * the starting prime for the sequence varies for each byte of 'val' | 442 | SHA256_Init(&ctx); |
447 | * (unique polynomials unless pointers are >64-bit). For added spice, | 443 | SHA256_Update(&ctx, (char *)(&id->ptr), sizeof(id->ptr)); |
448 | * the totals accumulate rather than restarting from zero, and the index | 444 | SHA256_Final(results, &ctx); |
449 | * of the 'val' byte is added each time (position dependence). If I was | 445 | memcpy(&id->val, results, sizeof(id->val)); |
450 | * a black-belt, I'd scan big-endian pointers in reverse to give | ||
451 | * low-order bits more play, but this isn't crypto and I'd prefer nobody | ||
452 | * mistake it as such. Plus I'm lazy. */ | ||
453 | while (dnum--) { | ||
454 | const unsigned char *src = (void *)&id->ptr; | ||
455 | unsigned char snum = sizeof(id->ptr); | ||
456 | while (snum--) | ||
457 | accum += *(src++) * hash_coeffs[(snum + dnum) & 7]; | ||
458 | accum += dnum; | ||
459 | *(dest++) = accum & 255; | ||
460 | } | 446 | } |
447 | #endif | ||
461 | } | 448 | } |
462 | 449 | ||
463 | int | 450 | int |
diff --git a/src/lib/libssl/src/crypto/cryptlib.c b/src/lib/libssl/src/crypto/cryptlib.c index 000f76a6a2..dc92ac89fe 100644 --- a/src/lib/libssl/src/crypto/cryptlib.c +++ b/src/lib/libssl/src/crypto/cryptlib.c | |||
@@ -1,4 +1,4 @@ | |||
1 | /* $OpenBSD: cryptlib.c,v 1.33 2014/07/22 02:21:20 beck Exp $ */ | 1 | /* $OpenBSD: cryptlib.c,v 1.34 2015/01/22 03:56:27 bcook Exp $ */ |
2 | /* ==================================================================== | 2 | /* ==================================================================== |
3 | * Copyright (c) 1998-2006 The OpenSSL Project. All rights reserved. | 3 | * Copyright (c) 1998-2006 The OpenSSL Project. All rights reserved. |
4 | * | 4 | * |
@@ -124,6 +124,7 @@ | |||
124 | #include <openssl/buffer.h> | 124 | #include <openssl/buffer.h> |
125 | #include <openssl/err.h> | 125 | #include <openssl/err.h> |
126 | #include <openssl/safestack.h> | 126 | #include <openssl/safestack.h> |
127 | #include <openssl/sha.h> | ||
127 | 128 | ||
128 | DECLARE_STACK_OF(CRYPTO_dynlock) | 129 | DECLARE_STACK_OF(CRYPTO_dynlock) |
129 | 130 | ||
@@ -425,39 +426,25 @@ CRYPTO_THREADID_set_numeric(CRYPTO_THREADID *id, unsigned long val) | |||
425 | id->val = val; | 426 | id->val = val; |
426 | } | 427 | } |
427 | 428 | ||
428 | static const unsigned char hash_coeffs[] = { 3, 5, 7, 11, 13, 17, 19, 23 }; | ||
429 | void | 429 | void |
430 | CRYPTO_THREADID_set_pointer(CRYPTO_THREADID *id, void *ptr) | 430 | CRYPTO_THREADID_set_pointer(CRYPTO_THREADID *id, void *ptr) |
431 | { | 431 | { |
432 | unsigned char *dest = (void *)&id->val; | ||
433 | unsigned int accum = 0; | ||
434 | unsigned char dnum = sizeof(id->val); | ||
435 | |||
436 | memset(id, 0, sizeof(*id)); | 432 | memset(id, 0, sizeof(*id)); |
437 | id->ptr = ptr; | 433 | id->ptr = ptr; |
438 | if (sizeof(id->val) >= sizeof(id->ptr)) { | 434 | #if LONG_MAX >= INTPTR_MAX |
439 | /* 'ptr' can be embedded in 'val' without loss of uniqueness */ | 435 | /*s u 'ptr' can be embedded in 'val' without loss of uniqueness */ |
440 | id->val = (unsigned long)id->ptr; | 436 | id->val = (unsigned long)id->ptr; |
441 | return; | 437 | #else |
442 | } | 438 | { |
443 | /* hash ptr ==> val. Each byte of 'val' gets the mod-256 total of a | 439 | SHA256_CTX ctx; |
444 | * linear function over the bytes in 'ptr', the co-efficients of which | 440 | uint8_t results[SHA256_DIGEST_LENGTH]; |
445 | * are a sequence of low-primes (hash_coeffs is an 8-element cycle) - | 441 | |
446 | * the starting prime for the sequence varies for each byte of 'val' | 442 | SHA256_Init(&ctx); |
447 | * (unique polynomials unless pointers are >64-bit). For added spice, | 443 | SHA256_Update(&ctx, (char *)(&id->ptr), sizeof(id->ptr)); |
448 | * the totals accumulate rather than restarting from zero, and the index | 444 | SHA256_Final(results, &ctx); |
449 | * of the 'val' byte is added each time (position dependence). If I was | 445 | memcpy(&id->val, results, sizeof(id->val)); |
450 | * a black-belt, I'd scan big-endian pointers in reverse to give | ||
451 | * low-order bits more play, but this isn't crypto and I'd prefer nobody | ||
452 | * mistake it as such. Plus I'm lazy. */ | ||
453 | while (dnum--) { | ||
454 | const unsigned char *src = (void *)&id->ptr; | ||
455 | unsigned char snum = sizeof(id->ptr); | ||
456 | while (snum--) | ||
457 | accum += *(src++) * hash_coeffs[(snum + dnum) & 7]; | ||
458 | accum += dnum; | ||
459 | *(dest++) = accum & 255; | ||
460 | } | 446 | } |
447 | #endif | ||
461 | } | 448 | } |
462 | 449 | ||
463 | int | 450 | int |