diff options
author | jsing <> | 2025-08-02 15:44:09 +0000 |
---|---|---|
committer | jsing <> | 2025-08-02 15:44:09 +0000 |
commit | c2a138d43f713ed803eed13034f57ed2c5f72dc7 (patch) | |
tree | c8109e9d88fa52c6af035cfe6ce1868562a9cae3 /src | |
parent | 767b2822ebb9d7b6b9ecab56928ee5f68f673eec (diff) | |
download | openbsd-c2a138d43f713ed803eed13034f57ed2c5f72dc7.tar.gz openbsd-c2a138d43f713ed803eed13034f57ed2c5f72dc7.tar.bz2 openbsd-c2a138d43f713ed803eed13034f57ed2c5f72dc7.zip |
Provide constant time conditional selection between EC_FIELD_ELEMENTs.
Provide a ec_field_element_select() function that allows for constant time
conditional selection between two EC_FIELD_ELEMENTs. This will become a
building block for constant time point multiplication.
ok tb@
Diffstat (limited to 'src')
-rw-r--r-- | src/lib/libcrypto/ec/ec_field.c | 15 | ||||
-rw-r--r-- | src/lib/libcrypto/ec/ec_internal.h | 4 |
2 files changed, 17 insertions, 2 deletions
diff --git a/src/lib/libcrypto/ec/ec_field.c b/src/lib/libcrypto/ec/ec_field.c index ec1c7d11e0..0513b9f410 100644 --- a/src/lib/libcrypto/ec/ec_field.c +++ b/src/lib/libcrypto/ec/ec_field.c | |||
@@ -1,4 +1,4 @@ | |||
1 | /* $OpenBSD: ec_field.c,v 1.1 2025/05/25 05:12:05 jsing Exp $ */ | 1 | /* $OpenBSD: ec_field.c,v 1.2 2025/08/02 15:44:09 jsing Exp $ */ |
2 | /* | 2 | /* |
3 | * Copyright (c) 2024 Joel Sing <jsing@openbsd.org> | 3 | * Copyright (c) 2024 Joel Sing <jsing@openbsd.org> |
4 | * | 4 | * |
@@ -131,6 +131,19 @@ ec_field_element_copy(EC_FIELD_ELEMENT *dst, const EC_FIELD_ELEMENT *src) | |||
131 | memcpy(dst, src, sizeof(EC_FIELD_ELEMENT)); | 131 | memcpy(dst, src, sizeof(EC_FIELD_ELEMENT)); |
132 | } | 132 | } |
133 | 133 | ||
134 | void | ||
135 | ec_field_element_select(const EC_FIELD_MODULUS *fm, EC_FIELD_ELEMENT *r, | ||
136 | const EC_FIELD_ELEMENT *a, const EC_FIELD_ELEMENT *b, int conditional) | ||
137 | { | ||
138 | BN_ULONG mask; | ||
139 | int i; | ||
140 | |||
141 | mask = bn_ct_eq_zero_mask(conditional); | ||
142 | |||
143 | for (i = 0; i < fm->n; i++) | ||
144 | r->w[i] = (a->w[i] & mask) | (b->w[i] & ~mask); | ||
145 | } | ||
146 | |||
134 | int | 147 | int |
135 | ec_field_element_equal(const EC_FIELD_MODULUS *fm, const EC_FIELD_ELEMENT *a, | 148 | ec_field_element_equal(const EC_FIELD_MODULUS *fm, const EC_FIELD_ELEMENT *a, |
136 | const EC_FIELD_ELEMENT *b) | 149 | const EC_FIELD_ELEMENT *b) |
diff --git a/src/lib/libcrypto/ec/ec_internal.h b/src/lib/libcrypto/ec/ec_internal.h index 29b447e8c9..327d9ea94d 100644 --- a/src/lib/libcrypto/ec/ec_internal.h +++ b/src/lib/libcrypto/ec/ec_internal.h | |||
@@ -1,4 +1,4 @@ | |||
1 | /* $OpenBSD: ec_internal.h,v 1.1 2025/05/25 05:12:05 jsing Exp $ */ | 1 | /* $OpenBSD: ec_internal.h,v 1.2 2025/08/02 15:44:09 jsing Exp $ */ |
2 | /* | 2 | /* |
3 | * Copyright (c) 2024 Joel Sing <jsing@openbsd.org> | 3 | * Copyright (c) 2024 Joel Sing <jsing@openbsd.org> |
4 | * | 4 | * |
@@ -46,6 +46,8 @@ int ec_field_element_to_bn(const EC_FIELD_MODULUS *fm, const EC_FIELD_ELEMENT *f | |||
46 | BIGNUM *bn, BN_CTX *ctx); | 46 | BIGNUM *bn, BN_CTX *ctx); |
47 | 47 | ||
48 | void ec_field_element_copy(EC_FIELD_ELEMENT *dst, const EC_FIELD_ELEMENT *src); | 48 | void ec_field_element_copy(EC_FIELD_ELEMENT *dst, const EC_FIELD_ELEMENT *src); |
49 | void ec_field_element_select(const EC_FIELD_MODULUS *fm, EC_FIELD_ELEMENT *r, | ||
50 | const EC_FIELD_ELEMENT *a, const EC_FIELD_ELEMENT *b, int conditional); | ||
49 | 51 | ||
50 | int ec_field_element_equal(const EC_FIELD_MODULUS *fm, const EC_FIELD_ELEMENT *a, | 52 | int ec_field_element_equal(const EC_FIELD_MODULUS *fm, const EC_FIELD_ELEMENT *a, |
51 | const EC_FIELD_ELEMENT *b); | 53 | const EC_FIELD_ELEMENT *b); |