summaryrefslogtreecommitdiff
path: root/src/lib/libcrypto/bn/arch
diff options
context:
space:
mode:
authorjsing <>2023-02-02 18:39:26 +0000
committerjsing <>2023-02-02 18:39:26 +0000
commitcea09ef9db2c57982c7c7d5eb3808c823c8e16ba (patch)
tree1674698c28777afc06c9e776cbb2058197484d33 /src/lib/libcrypto/bn/arch
parent9a98d1b0a691d9784ff03bc4640dc297775687f0 (diff)
downloadopenbsd-cea09ef9db2c57982c7c7d5eb3808c823c8e16ba.tar.gz
openbsd-cea09ef9db2c57982c7c7d5eb3808c823c8e16ba.tar.bz2
openbsd-cea09ef9db2c57982c7c7d5eb3808c823c8e16ba.zip
Refactor BN_uadd() and BN_usub().
Unlike bn_add_words()/bn_sub_words(), the s2n-bignum bignum_add() and bignum_sub() functions correctly handle inputs with differing word lengths. This means that they can be called directly, without needing to fix up any remaining words manually. Split BN_uadd() in two - the default bn_add() implementation calls bn_add_words(), before handling the carry for any remaining words. Likewise split BN_usub() in two - the default bn_sub() implementation calls bn_sub_words(), before handling the borrow for any remaining words. On amd64, provide an implementation of bn_add() that calls s2n-bignum's bignum_add() directly, similarly with an implementation of bn_sub() that calls s2n-bignum's bignum_sub() directly. ok tb@
Diffstat (limited to 'src/lib/libcrypto/bn/arch')
-rw-r--r--src/lib/libcrypto/bn/arch/amd64/bn_arch.c20
-rw-r--r--src/lib/libcrypto/bn/arch/amd64/bn_arch.h4
2 files changed, 22 insertions, 2 deletions
diff --git a/src/lib/libcrypto/bn/arch/amd64/bn_arch.c b/src/lib/libcrypto/bn/arch/amd64/bn_arch.c
index aedefc76e2..dc3000fe8b 100644
--- a/src/lib/libcrypto/bn/arch/amd64/bn_arch.c
+++ b/src/lib/libcrypto/bn/arch/amd64/bn_arch.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: bn_arch.c,v 1.2 2023/01/29 14:00:41 jsing Exp $ */ 1/* $OpenBSD: bn_arch.c,v 1.3 2023/02/02 18:39:26 jsing Exp $ */
2/* 2/*
3 * Copyright (c) 2023 Joel Sing <jsing@openbsd.org> 3 * Copyright (c) 2023 Joel Sing <jsing@openbsd.org>
4 * 4 *
@@ -21,6 +21,15 @@
21#include "bn_local.h" 21#include "bn_local.h"
22#include "s2n_bignum.h" 22#include "s2n_bignum.h"
23 23
24#ifdef HAVE_BN_ADD
25BN_ULONG
26bn_add(BIGNUM *r, int rn, const BIGNUM *a, const BIGNUM *b)
27{
28 return bignum_add(rn, (uint64_t *)r->d, a->top, (uint64_t *)a->d,
29 b->top, (uint64_t *)b->d);
30}
31#endif
32
24#ifdef HAVE_BN_ADD_WORDS 33#ifdef HAVE_BN_ADD_WORDS
25BN_ULONG 34BN_ULONG
26bn_add_words(BN_ULONG *rd, const BN_ULONG *ad, const BN_ULONG *bd, int n) 35bn_add_words(BN_ULONG *rd, const BN_ULONG *ad, const BN_ULONG *bd, int n)
@@ -30,6 +39,15 @@ bn_add_words(BN_ULONG *rd, const BN_ULONG *ad, const BN_ULONG *bd, int n)
30} 39}
31#endif 40#endif
32 41
42#ifdef HAVE_BN_SUB
43BN_ULONG
44bn_sub(BIGNUM *r, int rn, const BIGNUM *a, const BIGNUM *b)
45{
46 return bignum_sub(rn, (uint64_t *)r->d, a->top, (uint64_t *)a->d,
47 b->top, (uint64_t *)b->d);
48}
49#endif
50
33#ifdef HAVE_BN_SUB_WORDS 51#ifdef HAVE_BN_SUB_WORDS
34BN_ULONG 52BN_ULONG
35bn_sub_words(BN_ULONG *rd, const BN_ULONG *ad, const BN_ULONG *bd, int n) 53bn_sub_words(BN_ULONG *rd, const BN_ULONG *ad, const BN_ULONG *bd, int n)
diff --git a/src/lib/libcrypto/bn/arch/amd64/bn_arch.h b/src/lib/libcrypto/bn/arch/amd64/bn_arch.h
index 9e4b6b9442..c41a84409b 100644
--- a/src/lib/libcrypto/bn/arch/amd64/bn_arch.h
+++ b/src/lib/libcrypto/bn/arch/amd64/bn_arch.h
@@ -1,4 +1,4 @@
1/* $OpenBSD: bn_arch.h,v 1.9 2023/01/31 05:53:49 jsing Exp $ */ 1/* $OpenBSD: bn_arch.h,v 1.10 2023/02/02 18:39:26 jsing Exp $ */
2/* 2/*
3 * Copyright (c) 2023 Joel Sing <jsing@openbsd.org> 3 * Copyright (c) 2023 Joel Sing <jsing@openbsd.org>
4 * 4 *
@@ -22,6 +22,7 @@
22 22
23#ifndef OPENSSL_NO_ASM 23#ifndef OPENSSL_NO_ASM
24 24
25#define HAVE_BN_ADD
25#define HAVE_BN_ADD_WORDS 26#define HAVE_BN_ADD_WORDS
26 27
27#define HAVE_BN_DIV_WORDS 28#define HAVE_BN_DIV_WORDS
@@ -36,6 +37,7 @@
36#define HAVE_BN_SQR_COMBA8 37#define HAVE_BN_SQR_COMBA8
37#define HAVE_BN_SQR_WORDS 38#define HAVE_BN_SQR_WORDS
38 39
40#define HAVE_BN_SUB
39#define HAVE_BN_SUB_WORDS 41#define HAVE_BN_SUB_WORDS
40 42
41#if defined(__GNUC__) 43#if defined(__GNUC__)