summaryrefslogtreecommitdiff
path: root/chip
diff options
context:
space:
mode:
authornagendra modadugu <ngm@google.com>2016-02-04 18:03:01 -0800
committerchrome-bot <chrome-bot@chromium.org>2016-02-08 13:47:49 -0800
commite68019e3492a62980d5303cecb9cc29cf96cc170 (patch)
treeabb6d3f966f7070d37438e031b3a82358ec88b9d /chip
parentcb663d93a27702f247fc798e4d0a7068c82dcd0f (diff)
downloadchrome-ec-e68019e3492a62980d5303cecb9cc29cf96cc170.tar.gz
CR50: enable the bignum library to handle word un-aligned keys
The TPM2 api does not require keys to be word-aligned, so have the compiler generate alignment-safe reads where necessary. BRANCH=none BUG=chrome-os-partner:43025,chrome-os-partner:47524 TEST=tests under test/tpm2/ pass, more TCG tests pass. Change-Id: I247e29f2bec139ab7ed4010ffb58cdae77ba9e0b Signed-off-by: nagendra modadugu <ngm@google.com> Reviewed-on: https://chromium-review.googlesource.com/326201 Commit-Ready: Nagendra Modadugu <ngm@google.com> Tested-by: Nagendra Modadugu <ngm@google.com> Reviewed-by: Vadim Bendebury <vbendeb@chromium.org>
Diffstat (limited to 'chip')
-rw-r--r--chip/g/dcrypto/bn.c51
-rw-r--r--chip/g/dcrypto/internal.h6
-rw-r--r--chip/g/dcrypto/rsa.c4
3 files changed, 31 insertions, 30 deletions
diff --git a/chip/g/dcrypto/bn.c b/chip/g/dcrypto/bn.c
index 829d10e602..f4aaf11912 100644
--- a/chip/g/dcrypto/bn.c
+++ b/chip/g/dcrypto/bn.c
@@ -18,16 +18,14 @@ void bn_init(struct BIGNUM *b, void *buf, size_t len)
{
/* Only word-multiple sized buffers accepted. */
assert((len & 0x3) == 0);
- /* Only word-aligned buffers accepted. */
- assert(((uintptr_t) buf & 0x3) == 0);
b->dmax = len / BN_BYTES;
dcrypto_memset(buf, 0x00, len);
- b->d = (uint32_t *) buf;
+ b->d = (struct access_helper *) buf;
}
int bn_check_topbit(const struct BIGNUM *N)
{
- return N->d[N->dmax - 1] >> 31;
+ return BN_DIGIT(N, N->dmax - 1) >> 31;
}
/* a[n]. */
@@ -43,7 +41,7 @@ static int bn_is_bit_set(const struct BIGNUM *a, int n)
if (a->dmax <= i)
return 0;
- return (a->d[i] >> j) & 1;
+ return (BN_DIGIT(a, i) >> j) & 1;
}
/* a[] >= b[]. */
@@ -52,9 +50,9 @@ static int bn_gte(const struct BIGNUM *a, const struct BIGNUM *b)
{
int i;
- for (i = a->dmax - 1; a->d[i] == b->d[i] && i > 0; --i)
+ for (i = a->dmax - 1; BN_DIGIT(a, i) == BN_DIGIT(b, i) && i > 0; --i)
;
- return a->d[i] >= b->d[i];
+ return BN_DIGIT(a, i) >= BN_DIGIT(b, i);
}
/* c[] = c[] - a[], assumes c > a. */
@@ -64,8 +62,8 @@ static uint32_t bn_sub(struct BIGNUM *c, const struct BIGNUM *a)
int i;
for (i = 0; i < a->dmax; i++) {
- A += (uint64_t) c->d[i] - a->d[i];
- c->d[i] = (uint32_t) A;
+ A += (uint64_t) BN_DIGIT(c, i) - BN_DIGIT(a, i);
+ BN_DIGIT(c, i) = (uint32_t) A;
A >>= 32;
}
return (uint32_t) A; /* 0 or -1. */
@@ -78,8 +76,8 @@ static uint32_t bn_add(struct BIGNUM *c, const struct BIGNUM *a)
int i;
for (i = 0; i < a->dmax; ++i) {
- A += (uint64_t) c->d[i] + a->d[i];
- c->d[i] = (uint32_t) A;
+ A += (uint64_t) BN_DIGIT(c, i) + BN_DIGIT(a, i);
+ BN_DIGIT(c, i) = (uint32_t) A;
A >>= 32;
}
@@ -94,9 +92,9 @@ static uint32_t bn_lshift(struct BIGNUM *r)
uint32_t carry = 0;
for (i = 0; i < r->dmax; i++) {
- w = (r->d[i] << 1) | carry;
- carry = r->d[i] >> 31;
- r->d[i] = w;
+ w = (BN_DIGIT(r, i) << 1) | carry;
+ carry = BN_DIGIT(r, i) >> 31;
+ BN_DIGIT(r, i) = w;
}
return carry;
}
@@ -113,20 +111,21 @@ static void bn_mont_mul_add(struct BIGNUM *c, const uint32_t a,
{
register uint64_t tmp;
- tmp = c->d[0] + (uint64_t) a * b->d[0];
+ tmp = BN_DIGIT(c, 0) + (uint64_t) a * BN_DIGIT(b, 0);
A = tmp >> 32;
d0 = (uint32_t) tmp * (uint32_t) nprime;
- tmp = (uint32_t)tmp + (uint64_t) d0 * N->d[0];
+ tmp = (uint32_t)tmp + (uint64_t) d0 * BN_DIGIT(N, 0);
B = tmp >> 32;
}
for (i = 0; i < N->dmax - 1;) {
register uint64_t tmp;
- tmp = A + (uint64_t) a * b->d[i + 1] + c->d[i + 1];
+ tmp = A + (uint64_t) a * BN_DIGIT(b, i + 1) +
+ BN_DIGIT(c, i + 1);
A = tmp >> 32;
- tmp = B + (uint64_t) d0 * N->d[i + 1] + (uint32_t) tmp;
- c->d[i] = (uint32_t) tmp;
+ tmp = B + (uint64_t) d0 * BN_DIGIT(N, i + 1) + (uint32_t) tmp;
+ BN_DIGIT(c, i) = (uint32_t) tmp;
B = tmp >> 32;
++i;
}
@@ -134,7 +133,7 @@ static void bn_mont_mul_add(struct BIGNUM *c, const uint32_t a,
{
uint64_t tmp = (uint64_t) A + B;
- c->d[i] = (uint32_t) tmp;
+ BN_DIGIT(c, i) = (uint32_t) tmp;
A = tmp >> 32; /* 0 or 1. */
if (A)
bn_sub(c, N);
@@ -149,11 +148,11 @@ static void bn_mont_mul(struct BIGNUM *c, const struct BIGNUM *a,
int i;
for (i = 0; i < N->dmax; i++)
- c->d[i] = 0;
+ BN_DIGIT(c, i) = 0;
- bn_mont_mul_add(c, a ? a->d[0] : 1, b, nprime, N);
+ bn_mont_mul_add(c, a ? BN_DIGIT(a, 0) : 1, b, nprime, N);
for (i = 1; i < N->dmax; i++)
- bn_mont_mul_add(c, a ? a->d[i] : 0, b, nprime, N);
+ bn_mont_mul_add(c, a ? BN_DIGIT(a, i) : 0, b, nprime, N);
}
/* Mongomery R * R % N, R = 1 << (1 + log2N). */
@@ -206,11 +205,11 @@ void bn_mont_modexp(struct BIGNUM *output, const struct BIGNUM *input,
bn_init(&acc, acc_buf, bn_size(N));
bn_init(&aR, aR_buf, bn_size(N));
- nprime = bn_compute_nprime(N->d[0]);
+ nprime = bn_compute_nprime(BN_DIGIT(N, 0));
bn_compute_RR(&RR, N);
bn_mont_mul(&acc, NULL, &RR, nprime, N); /* R = 1 * RR / R % N */
bn_mont_mul(&aR, input, &RR, nprime, N); /* aR = a * RR / R % N */
- output->d[0] = 1;
+ BN_DIGIT(output, 0) = 1;
/* TODO(ngm): burn stack space and use windowing. */
for (i = exp->dmax * BN_BITS2 - 1; i >= 0; i--) {
@@ -232,7 +231,7 @@ void bn_mont_modexp(struct BIGNUM *output, const struct BIGNUM *input,
bn_mont_mul(output, NULL, &acc, nprime, N); /* Convert out. */
/* Copy to output buffer if necessary. */
- if (acc.d != acc_buf) {
+ if (acc.d != (struct access_helper *) acc_buf) {
memcpy(acc.d, acc_buf, bn_size(output));
*output = acc;
}
diff --git a/chip/g/dcrypto/internal.h b/chip/g/dcrypto/internal.h
index 61db833a53..36aa35f6bf 100644
--- a/chip/g/dcrypto/internal.h
+++ b/chip/g/dcrypto/internal.h
@@ -89,10 +89,12 @@ void dcrypto_sha_wait(enum sha_mode mode, uint32_t *digest);
#define BN_BYTES 4
struct BIGNUM {
- uint32_t dmax; /* Size of d, in 32-bit words. */
- uint32_t *d; /* Word array, little endian format ... */
+ uint32_t dmax; /* Size of d, in 32-bit words. */
+ struct access_helper *d; /* Word array, little endian format ... */
};
+#define BN_DIGIT(b, i) ((b)->d[(i)].udata)
+
void bn_init(struct BIGNUM *bn, void *buf, size_t len);
#define bn_size(b) ((b)->dmax * BN_BYTES)
int bn_check_topbit(const struct BIGNUM *N);
diff --git a/chip/g/dcrypto/rsa.c b/chip/g/dcrypto/rsa.c
index d9774a2101..b6128923ef 100644
--- a/chip/g/dcrypto/rsa.c
+++ b/chip/g/dcrypto/rsa.c
@@ -339,7 +339,7 @@ int DCRYPTO_rsa_encrypt(struct RSA *rsa, uint8_t *out, uint32_t *out_len,
bn_init(&padded, padded_buf, bn_size(&rsa->N));
bn_init(&encrypted, out, bn_size(&rsa->N));
bn_init(&e, e_buf, sizeof(e_buf));
- *e.d = rsa->e;
+ BN_DIGIT(&e, 0) = rsa->e;
switch (padding) {
case PADDING_MODE_OAEP:
@@ -479,7 +479,7 @@ int DCRYPTO_rsa_verify(struct RSA *rsa, const uint8_t *digest,
memcpy(signature_buf, sig, bn_size(&rsa->N));
bn_init(&padded, padded_buf, bn_size(&rsa->N));
bn_init(&e, e_buf, sizeof(e_buf));
- *e.d = rsa->e;
+ BN_DIGIT(&e, 0) = rsa->e;
/* Reverse from big-endian to little-endian notation. */
reverse((uint8_t *) signature.d, signature.dmax * BN_BYTES);