1/*	$OpenBSD: bn_arch.c,v 1.7 2023/06/24 16:01:44 jsing Exp $ */
2/*
3 * Copyright (c) 2023 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 <openssl/bn.h>
19
20#include "bn_arch.h"
21#include "bn_local.h"
22#include "s2n_bignum.h"
23
24#ifdef HAVE_BN_ADD
25BN_ULONG
26bn_add(BN_ULONG *r, int r_len, const BN_ULONG *a, int a_len, const BN_ULONG *b,
27    int b_len)
28{
29	return bignum_add(r_len, (uint64_t *)r, a_len, (uint64_t *)a,
30	    b_len, (uint64_t *)b);
31}
32#endif
33
34
35#ifdef HAVE_BN_ADD_WORDS
36BN_ULONG
37bn_add_words(BN_ULONG *rd, const BN_ULONG *ad, const BN_ULONG *bd, int n)
38{
39	return bignum_add(n, (uint64_t *)rd, n, (uint64_t *)ad, n,
40	    (uint64_t *)bd);
41}
42#endif
43
44#ifdef HAVE_BN_SUB
45BN_ULONG
46bn_sub(BN_ULONG *r, int r_len, const BN_ULONG *a, int a_len, const BN_ULONG *b,
47    int b_len)
48{
49	return bignum_sub(r_len, (uint64_t *)r, a_len, (uint64_t *)a,
50	    b_len, (uint64_t *)b);
51}
52#endif
53
54#ifdef HAVE_BN_SUB_WORDS
55BN_ULONG
56bn_sub_words(BN_ULONG *rd, const BN_ULONG *ad, const BN_ULONG *bd, int n)
57{
58	return bignum_sub(n, (uint64_t *)rd, n, (uint64_t *)ad, n,
59	    (uint64_t *)bd);
60}
61#endif
62
63#ifdef HAVE_BN_MUL_ADD_WORDS
64BN_ULONG
65bn_mul_add_words(BN_ULONG *rd, const BN_ULONG *ad, int num, BN_ULONG w)
66{
67	return bignum_cmadd(num, (uint64_t *)rd, w, num, (uint64_t *)ad);
68}
69#endif
70
71#ifdef HAVE_BN_MUL_WORDS
72BN_ULONG
73bn_mul_words(BN_ULONG *rd, const BN_ULONG *ad, int num, BN_ULONG w)
74{
75	return bignum_cmul(num, (uint64_t *)rd, w, num, (uint64_t *)ad);
76}
77#endif
78
79#ifdef HAVE_BN_MUL_COMBA4
80void
81bn_mul_comba4(BN_ULONG *rd, BN_ULONG *ad, BN_ULONG *bd)
82{
83	/* XXX - consider using non-alt on CPUs that have the ADX extension. */
84	bignum_mul_4_8_alt((uint64_t *)rd, (uint64_t *)ad, (uint64_t *)bd);
85}
86#endif
87
88#ifdef HAVE_BN_MUL_COMBA8
89void
90bn_mul_comba8(BN_ULONG *rd, BN_ULONG *ad, BN_ULONG *bd)
91{
92	/* XXX - consider using non-alt on CPUs that have the ADX extension. */
93	bignum_mul_8_16_alt((uint64_t *)rd, (uint64_t *)ad, (uint64_t *)bd);
94}
95#endif
96
97#ifdef HAVE_BN_SQR
98int
99bn_sqr(BIGNUM *r, const BIGNUM *a, int r_len, BN_CTX *ctx)
100{
101	bignum_sqr(r_len, (uint64_t *)r->d, a->top, (uint64_t *)a->d);
102
103	return 1;
104}
105#endif
106
107#ifdef HAVE_BN_SQR_COMBA4
108void
109bn_sqr_comba4(BN_ULONG *rd, const BN_ULONG *ad)
110{
111	/* XXX - consider using non-alt on CPUs that have the ADX extension. */
112	bignum_sqr_4_8_alt((uint64_t *)rd, (uint64_t *)ad);
113}
114#endif
115
116#ifdef HAVE_BN_SQR_COMBA8
117void
118bn_sqr_comba8(BN_ULONG *rd, const BN_ULONG *ad)
119{
120	/* XXX - consider using non-alt on CPUs that have the ADX extension. */
121	bignum_sqr_8_16_alt((uint64_t *)rd, (uint64_t *)ad);
122}
123#endif
124
125#ifdef HAVE_BN_WORD_CLZ
126int
127bn_word_clz(BN_ULONG w)
128{
129	return word_clz(w);
130}
131#endif
132