提交 89d8aade 编写于 作者: A Andy Polyakov

bn/bn_lib.c: make BN_bn2binpad computationally constant-time.

"Computationally constant-time" means that it might still leak
information about input's length, but only in cases when input
is missing complete BN_ULONG limbs. But even then leak is possible
only if attacker can observe memory access pattern with limb
granularity.
Reviewed-by: NRich Salz <rsalz@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/5254)
上级 1e839545
...@@ -12,6 +12,7 @@ ...@@ -12,6 +12,7 @@
#include "internal/cryptlib.h" #include "internal/cryptlib.h"
#include "bn_lcl.h" #include "bn_lcl.h"
#include <openssl/opensslconf.h> #include <openssl/opensslconf.h>
#include "internal/constant_time_locl.h"
/* This stuff appears to be completely unused, so is deprecated */ /* This stuff appears to be completely unused, so is deprecated */
#if OPENSSL_API_COMPAT < 0x00908000L #if OPENSSL_API_COMPAT < 0x00908000L
...@@ -416,24 +417,30 @@ BIGNUM *BN_bin2bn(const unsigned char *s, int len, BIGNUM *ret) ...@@ -416,24 +417,30 @@ BIGNUM *BN_bin2bn(const unsigned char *s, int len, BIGNUM *ret)
/* ignore negative */ /* ignore negative */
static int bn2binpad(const BIGNUM *a, unsigned char *to, int tolen) static int bn2binpad(const BIGNUM *a, unsigned char *to, int tolen)
{ {
int i; int i, j, top;
BN_ULONG l; BN_ULONG l;
bn_check_top(a);
i = BN_num_bytes(a); i = BN_num_bytes(a);
if (tolen == -1) if (tolen == -1)
tolen = i; tolen = i;
else if (tolen < i) else if (tolen < i)
return -1; return -1;
/* Add leading zeroes if necessary */
if (tolen > i) { if (i == 0) {
memset(to, 0, tolen - i); OPENSSL_cleanse(to, tolen);
to += tolen - i; return tolen;
} }
while (i--) {
top = a->top * BN_BYTES;
for (i = 0, j = tolen; j > 0; i++) {
unsigned int mask;
mask = constant_time_lt(i, top);
i -= 1 & ~mask; /* stay on top limb */
l = a->d[i / BN_BYTES]; l = a->d[i / BN_BYTES];
*(to++) = (unsigned char)(l >> (8 * (i % BN_BYTES))) & 0xff; to[--j] = (unsigned char)(l >> (8 * (i % BN_BYTES)) & mask);
} }
return tolen; return tolen;
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册