summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorjsing <>2025-05-25 04:58:32 +0000
committerjsing <>2025-05-25 04:58:32 +0000
commit48472c192c88669e44bdba626a93217f709ca4d4 (patch)
tree956884db6964cf66be11beb92fc6b846c09b44a1 /src
parent9c476c7a531ca0620e1274e88ba48ca60110fc7a (diff)
downloadopenbsd-48472c192c88669e44bdba626a93217f709ca4d4.tar.gz
openbsd-48472c192c88669e44bdba626a93217f709ca4d4.tar.bz2
openbsd-48472c192c88669e44bdba626a93217f709ca4d4.zip
Provide bn_mod_{add,sub,mul}_words().
These implement constant time modular addition, subtraction and multiplication in the Montegomery domain. ok tb@
Diffstat (limited to 'src')
-rw-r--r--src/lib/libcrypto/Makefile3
-rw-r--r--src/lib/libcrypto/bn/bn_internal.h12
-rw-r--r--src/lib/libcrypto/bn/bn_mod_words.c78
-rw-r--r--src/lib/libcrypto/bn/bn_mont.c6
4 files changed, 94 insertions, 5 deletions
diff --git a/src/lib/libcrypto/Makefile b/src/lib/libcrypto/Makefile
index a05042986c..dd64f07f48 100644
--- a/src/lib/libcrypto/Makefile
+++ b/src/lib/libcrypto/Makefile
@@ -1,4 +1,4 @@
1# $OpenBSD: Makefile,v 1.232 2025/05/25 04:53:05 jsing Exp $ 1# $OpenBSD: Makefile,v 1.233 2025/05/25 04:58:32 jsing Exp $
2 2
3LIB= crypto 3LIB= crypto
4LIBREBUILD=y 4LIBREBUILD=y
@@ -173,6 +173,7 @@ SRCS+= bn_kron.c
173SRCS+= bn_lib.c 173SRCS+= bn_lib.c
174SRCS+= bn_mod.c 174SRCS+= bn_mod.c
175SRCS+= bn_mod_sqrt.c 175SRCS+= bn_mod_sqrt.c
176SRCS+= bn_mod_words.c
176SRCS+= bn_mont.c 177SRCS+= bn_mont.c
177SRCS+= bn_mul.c 178SRCS+= bn_mul.c
178SRCS+= bn_prime.c 179SRCS+= bn_prime.c
diff --git a/src/lib/libcrypto/bn/bn_internal.h b/src/lib/libcrypto/bn/bn_internal.h
index 895a194c93..b6e903553f 100644
--- a/src/lib/libcrypto/bn/bn_internal.h
+++ b/src/lib/libcrypto/bn/bn_internal.h
@@ -1,4 +1,4 @@
1/* $OpenBSD: bn_internal.h,v 1.17 2025/05/25 04:53:05 jsing Exp $ */ 1/* $OpenBSD: bn_internal.h,v 1.18 2025/05/25 04:58:32 jsing Exp $ */
2/* 2/*
3 * Copyright (c) 2023 Joel Sing <jsing@openbsd.org> 3 * Copyright (c) 2023 Joel Sing <jsing@openbsd.org>
4 * 4 *
@@ -35,6 +35,16 @@ BN_ULONG bn_add_words_masked(BN_ULONG *r, const BN_ULONG *a, const BN_ULONG *b,
35 BN_ULONG mask, size_t n); 35 BN_ULONG mask, size_t n);
36BN_ULONG bn_sub_words_masked(BN_ULONG *r, const BN_ULONG *a, const BN_ULONG *b, 36BN_ULONG bn_sub_words_masked(BN_ULONG *r, const BN_ULONG *a, const BN_ULONG *b,
37 BN_ULONG mask, size_t n); 37 BN_ULONG mask, size_t n);
38void bn_mod_add_words(BN_ULONG *r, const BN_ULONG *a, const BN_ULONG *b,
39 const BN_ULONG *m, size_t n);
40void bn_mod_sub_words(BN_ULONG *r, const BN_ULONG *a, const BN_ULONG *b,
41 const BN_ULONG *m, size_t n);
42void bn_mod_mul_words(BN_ULONG *r, const BN_ULONG *a, const BN_ULONG *b,
43 const BN_ULONG *m, BN_ULONG *t, BN_ULONG m0, size_t n);
44
45void bn_montgomery_multiply_words(BN_ULONG *rp, const BN_ULONG *ap,
46 const BN_ULONG *bp, const BN_ULONG *np, BN_ULONG *tp, BN_ULONG n0,
47 int n_len);
38 48
39#ifndef HAVE_BN_CT_NE_ZERO 49#ifndef HAVE_BN_CT_NE_ZERO
40static inline int 50static inline int
diff --git a/src/lib/libcrypto/bn/bn_mod_words.c b/src/lib/libcrypto/bn/bn_mod_words.c
new file mode 100644
index 0000000000..8971f9f306
--- /dev/null
+++ b/src/lib/libcrypto/bn/bn_mod_words.c
@@ -0,0 +1,78 @@
1/* $OpenBSD: bn_mod_words.c,v 1.1 2025/05/25 04:58:32 jsing Exp $ */
2/*
3 * Copyright (c) 2024 Joel Sing <jsing@openbsd.org>
4 *
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 */
17
18#include "bn_local.h"
19#include "bn_internal.h"
20
21/*
22 * bn_mod_add_words() computes r[] = (a[] + b[]) mod m[], where a, b, r and
23 * m are arrays of words with length n (r may be the same as a or b).
24 */
25#ifndef HAVE_BN_MOD_ADD_WORDS
26void
27bn_mod_add_words(BN_ULONG *r, const BN_ULONG *a, const BN_ULONG *b,
28 const BN_ULONG *m, size_t n)
29{
30 BN_ULONG carry, mask;
31
32 /*
33 * Compute a + b, then compute r - m to determine if r >= m, considering
34 * any carry that resulted from the addition. Finally complete a
35 * conditional subtraction of r - m.
36 */
37 /* XXX - change bn_add_words to use size_t. */
38 carry = bn_add_words(r, a, b, n);
39 mask = ~(carry - bn_sub_words_borrow(r, m, n));
40 bn_sub_words_masked(r, r, m, mask, n);
41}
42#endif
43
44/*
45 * bn_mod_sub_words() computes r[] = (a[] - b[]) mod m[], where a, b, r and
46 * m are arrays of words with length n (r may be the same as a or b).
47 */
48#ifndef HAVE_BN_MOD_SUB_WORDS
49void
50bn_mod_sub_words(BN_ULONG *r, const BN_ULONG *a, const BN_ULONG *b,
51 const BN_ULONG *m, size_t n)
52{
53 BN_ULONG borrow, mask;
54
55 /*
56 * Compute a - b, then complete a conditional addition of r + m
57 * based on the resulting borrow.
58 */
59 /* XXX - change bn_sub_words to use size_t. */
60 borrow = bn_sub_words(r, a, b, n);
61 mask = (0 - borrow);
62 bn_add_words_masked(r, r, m, mask, n);
63}
64#endif
65
66/*
67 * bn_mod_mul_words() computes r[] = (a[] * b[]) mod m[], where a, b, r and
68 * m are arrays of words with length n (r may be the same as a or b) in the
69 * Montgomery domain. The result remains in the Montgomery domain.
70 */
71#ifndef HAVE_BN_MOD_MUL_WORDS
72void
73bn_mod_mul_words(BN_ULONG *r, const BN_ULONG *a, const BN_ULONG *b,
74 const BN_ULONG *m, BN_ULONG *t, BN_ULONG m0, size_t n)
75{
76 bn_montgomery_multiply_words(r, a, b, m, t, m0, n);
77}
78#endif
diff --git a/src/lib/libcrypto/bn/bn_mont.c b/src/lib/libcrypto/bn/bn_mont.c
index edd7bcd0c8..ce88b23ca9 100644
--- a/src/lib/libcrypto/bn/bn_mont.c
+++ b/src/lib/libcrypto/bn/bn_mont.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: bn_mont.c,v 1.66 2025/03/09 15:22:40 tb Exp $ */ 1/* $OpenBSD: bn_mont.c,v 1.67 2025/05/25 04:58:32 jsing Exp $ */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) 2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved. 3 * All rights reserved.
4 * 4 *
@@ -417,7 +417,7 @@ bn_mod_mul_montgomery_simple(BIGNUM *r, const BIGNUM *a, const BIGNUM *b,
417 return ret; 417 return ret;
418} 418}
419 419
420static void 420static inline void
421bn_montgomery_multiply_word(const BN_ULONG *ap, BN_ULONG b, const BN_ULONG *np, 421bn_montgomery_multiply_word(const BN_ULONG *ap, BN_ULONG b, const BN_ULONG *np,
422 BN_ULONG *tp, BN_ULONG w, BN_ULONG *carry_a, BN_ULONG *carry_n, int n_len) 422 BN_ULONG *tp, BN_ULONG w, BN_ULONG *carry_a, BN_ULONG *carry_n, int n_len)
423{ 423{
@@ -452,7 +452,7 @@ bn_montgomery_multiply_word(const BN_ULONG *ap, BN_ULONG b, const BN_ULONG *np,
452 * given word arrays. The caller must ensure that rp, ap, bp and np are all 452 * given word arrays. The caller must ensure that rp, ap, bp and np are all
453 * n_len words in length, while tp must be n_len * 2 + 2 words in length. 453 * n_len words in length, while tp must be n_len * 2 + 2 words in length.
454 */ 454 */
455static void 455void
456bn_montgomery_multiply_words(BN_ULONG *rp, const BN_ULONG *ap, const BN_ULONG *bp, 456bn_montgomery_multiply_words(BN_ULONG *rp, const BN_ULONG *ap, const BN_ULONG *bp,
457 const BN_ULONG *np, BN_ULONG *tp, BN_ULONG n0, int n_len) 457 const BN_ULONG *np, BN_ULONG *tp, BN_ULONG n0, int n_len)
458{ 458{