2016-05-17 18:52:22 +00:00
|
|
|
/*
|
Fix issues in ia32 RDRAND asm leading to reduced entropy
This patch fixes two issues in the ia32 RDRAND assembly code that result in a
(possibly significant) loss of entropy.
The first, less significant, issue is that, by returning success as 0 from
OPENSSL_ia32_rdrand() and OPENSSL_ia32_rdseed(), a subtle bias was introduced.
Specifically, because the assembly routine copied the remaining number of
retries over the result when RDRAND/RDSEED returned 'successful but zero', a
bias towards values 1-8 (primarily 8) was introduced.
The second, more worrying issue was that, due to a mixup in registers, when a
buffer that was not size 0 or 1 mod 8 was passed to OPENSSL_ia32_rdrand_bytes
or OPENSSL_ia32_rdseed_bytes, the last (n mod 8) bytes were all the same value.
This issue impacts only the 64-bit variant of the assembly.
This change fixes both issues by first eliminating the only use of
OPENSSL_ia32_rdrand, replacing it with OPENSSL_ia32_rdrand_bytes, and fixes the
register mixup in OPENSSL_ia32_rdrand_bytes. It also adds a sanity test for
OPENSSL_ia32_rdrand_bytes and OPENSSL_ia32_rdseed_bytes to help catch problems
of this nature in the future.
Reviewed-by: Andy Polyakov <appro@openssl.org>
Reviewed-by: Rich Salz <rsalz@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/5342)
2018-03-07 21:01:06 +00:00
|
|
|
* Copyright 2011-2018 The OpenSSL Project Authors. All Rights Reserved.
|
2011-08-10 18:52:42 +00:00
|
|
|
*
|
2016-05-17 18:52:22 +00:00
|
|
|
* Licensed under the OpenSSL license (the "License"). You may not use
|
|
|
|
* this file except in compliance with the License. You can obtain a copy
|
|
|
|
* in the file LICENSE in the source distribution or at
|
|
|
|
* https://www.openssl.org/source/license.html
|
2011-08-10 18:52:42 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <openssl/opensslconf.h>
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
2017-08-22 12:35:43 +00:00
|
|
|
#include "internal/engine.h"
|
2011-08-11 21:12:17 +00:00
|
|
|
#include <openssl/rand.h>
|
|
|
|
#include <openssl/err.h>
|
2016-02-08 16:43:03 +00:00
|
|
|
#include <openssl/crypto.h>
|
2011-08-10 18:52:42 +00:00
|
|
|
|
|
|
|
#if (defined(__i386) || defined(__i386__) || defined(_M_IX86) || \
|
|
|
|
defined(__x86_64) || defined(__x86_64__) || \
|
2011-08-14 08:30:56 +00:00
|
|
|
defined(_M_AMD64) || defined (_M_X64)) && defined(OPENSSL_CPUID_OBJ)
|
2011-08-10 18:52:42 +00:00
|
|
|
|
Fix issues in ia32 RDRAND asm leading to reduced entropy
This patch fixes two issues in the ia32 RDRAND assembly code that result in a
(possibly significant) loss of entropy.
The first, less significant, issue is that, by returning success as 0 from
OPENSSL_ia32_rdrand() and OPENSSL_ia32_rdseed(), a subtle bias was introduced.
Specifically, because the assembly routine copied the remaining number of
retries over the result when RDRAND/RDSEED returned 'successful but zero', a
bias towards values 1-8 (primarily 8) was introduced.
The second, more worrying issue was that, due to a mixup in registers, when a
buffer that was not size 0 or 1 mod 8 was passed to OPENSSL_ia32_rdrand_bytes
or OPENSSL_ia32_rdseed_bytes, the last (n mod 8) bytes were all the same value.
This issue impacts only the 64-bit variant of the assembly.
This change fixes both issues by first eliminating the only use of
OPENSSL_ia32_rdrand, replacing it with OPENSSL_ia32_rdrand_bytes, and fixes the
register mixup in OPENSSL_ia32_rdrand_bytes. It also adds a sanity test for
OPENSSL_ia32_rdrand_bytes and OPENSSL_ia32_rdseed_bytes to help catch problems
of this nature in the future.
Reviewed-by: Andy Polyakov <appro@openssl.org>
Reviewed-by: Rich Salz <rsalz@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/5342)
2018-03-07 21:01:06 +00:00
|
|
|
size_t OPENSSL_ia32_rdrand_bytes(unsigned char *buf, size_t len);
|
2011-08-10 18:52:42 +00:00
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
static int get_random_bytes(unsigned char *buf, int num)
|
|
|
|
{
|
Fix issues in ia32 RDRAND asm leading to reduced entropy
This patch fixes two issues in the ia32 RDRAND assembly code that result in a
(possibly significant) loss of entropy.
The first, less significant, issue is that, by returning success as 0 from
OPENSSL_ia32_rdrand() and OPENSSL_ia32_rdseed(), a subtle bias was introduced.
Specifically, because the assembly routine copied the remaining number of
retries over the result when RDRAND/RDSEED returned 'successful but zero', a
bias towards values 1-8 (primarily 8) was introduced.
The second, more worrying issue was that, due to a mixup in registers, when a
buffer that was not size 0 or 1 mod 8 was passed to OPENSSL_ia32_rdrand_bytes
or OPENSSL_ia32_rdseed_bytes, the last (n mod 8) bytes were all the same value.
This issue impacts only the 64-bit variant of the assembly.
This change fixes both issues by first eliminating the only use of
OPENSSL_ia32_rdrand, replacing it with OPENSSL_ia32_rdrand_bytes, and fixes the
register mixup in OPENSSL_ia32_rdrand_bytes. It also adds a sanity test for
OPENSSL_ia32_rdrand_bytes and OPENSSL_ia32_rdseed_bytes to help catch problems
of this nature in the future.
Reviewed-by: Andy Polyakov <appro@openssl.org>
Reviewed-by: Rich Salz <rsalz@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/5342)
2018-03-07 21:01:06 +00:00
|
|
|
if (num < 0) {
|
|
|
|
return 0;
|
2015-01-22 03:40:55 +00:00
|
|
|
}
|
|
|
|
|
Fix issues in ia32 RDRAND asm leading to reduced entropy
This patch fixes two issues in the ia32 RDRAND assembly code that result in a
(possibly significant) loss of entropy.
The first, less significant, issue is that, by returning success as 0 from
OPENSSL_ia32_rdrand() and OPENSSL_ia32_rdseed(), a subtle bias was introduced.
Specifically, because the assembly routine copied the remaining number of
retries over the result when RDRAND/RDSEED returned 'successful but zero', a
bias towards values 1-8 (primarily 8) was introduced.
The second, more worrying issue was that, due to a mixup in registers, when a
buffer that was not size 0 or 1 mod 8 was passed to OPENSSL_ia32_rdrand_bytes
or OPENSSL_ia32_rdseed_bytes, the last (n mod 8) bytes were all the same value.
This issue impacts only the 64-bit variant of the assembly.
This change fixes both issues by first eliminating the only use of
OPENSSL_ia32_rdrand, replacing it with OPENSSL_ia32_rdrand_bytes, and fixes the
register mixup in OPENSSL_ia32_rdrand_bytes. It also adds a sanity test for
OPENSSL_ia32_rdrand_bytes and OPENSSL_ia32_rdseed_bytes to help catch problems
of this nature in the future.
Reviewed-by: Andy Polyakov <appro@openssl.org>
Reviewed-by: Rich Salz <rsalz@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/5342)
2018-03-07 21:01:06 +00:00
|
|
|
return (size_t)num == OPENSSL_ia32_rdrand_bytes(buf, (size_t)num);
|
2015-01-22 03:40:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int random_status(void)
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static RAND_METHOD rdrand_meth = {
|
|
|
|
NULL, /* seed */
|
|
|
|
get_random_bytes,
|
|
|
|
NULL, /* cleanup */
|
|
|
|
NULL, /* add */
|
|
|
|
get_random_bytes,
|
|
|
|
random_status,
|
|
|
|
};
|
2011-08-10 18:52:42 +00:00
|
|
|
|
|
|
|
static int rdrand_init(ENGINE *e)
|
2015-01-22 03:40:55 +00:00
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
2011-08-10 18:52:42 +00:00
|
|
|
|
|
|
|
static const char *engine_e_rdrand_id = "rdrand";
|
|
|
|
static const char *engine_e_rdrand_name = "Intel RDRAND engine";
|
|
|
|
|
|
|
|
static int bind_helper(ENGINE *e)
|
2015-01-22 03:40:55 +00:00
|
|
|
{
|
|
|
|
if (!ENGINE_set_id(e, engine_e_rdrand_id) ||
|
|
|
|
!ENGINE_set_name(e, engine_e_rdrand_name) ||
|
|
|
|
!ENGINE_set_flags(e, ENGINE_FLAGS_NO_REGISTER_ALL) ||
|
|
|
|
!ENGINE_set_init_function(e, rdrand_init) ||
|
|
|
|
!ENGINE_set_RAND(e, &rdrand_meth))
|
|
|
|
return 0;
|
2011-08-10 18:52:42 +00:00
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
return 1;
|
|
|
|
}
|
2011-08-10 18:52:42 +00:00
|
|
|
|
|
|
|
static ENGINE *ENGINE_rdrand(void)
|
2015-01-22 03:40:55 +00:00
|
|
|
{
|
|
|
|
ENGINE *ret = ENGINE_new();
|
2015-10-30 11:12:26 +00:00
|
|
|
if (ret == NULL)
|
2015-01-22 03:40:55 +00:00
|
|
|
return NULL;
|
|
|
|
if (!bind_helper(ret)) {
|
|
|
|
ENGINE_free(ret);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2016-04-12 11:20:16 +00:00
|
|
|
void engine_load_rdrand_int(void)
|
2015-01-22 03:40:55 +00:00
|
|
|
{
|
|
|
|
extern unsigned int OPENSSL_ia32cap_P[];
|
|
|
|
|
|
|
|
if (OPENSSL_ia32cap_P[1] & (1 << (62 - 32))) {
|
|
|
|
ENGINE *toadd = ENGINE_rdrand();
|
|
|
|
if (!toadd)
|
|
|
|
return;
|
|
|
|
ENGINE_add(toadd);
|
|
|
|
ENGINE_free(toadd);
|
|
|
|
ERR_clear_error();
|
|
|
|
}
|
|
|
|
}
|
2011-08-10 18:52:42 +00:00
|
|
|
#else
|
2016-04-12 11:20:16 +00:00
|
|
|
void engine_load_rdrand_int(void)
|
2015-01-22 03:40:55 +00:00
|
|
|
{
|
|
|
|
}
|
2011-08-10 18:52:42 +00:00
|
|
|
#endif
|