summaryrefslogtreecommitdiff
path: root/src/lib/libcrypto/bn/bn_primitives.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/libcrypto/bn/bn_primitives.c')
-rw-r--r--src/lib/libcrypto/bn/bn_primitives.c63
1 files changed, 63 insertions, 0 deletions
diff --git a/src/lib/libcrypto/bn/bn_primitives.c b/src/lib/libcrypto/bn/bn_primitives.c
new file mode 100644
index 0000000000..e9caec4818
--- /dev/null
+++ b/src/lib/libcrypto/bn/bn_primitives.c
@@ -0,0 +1,63 @@
1/* $OpenBSD: bn_primitives.c,v 1.1 2023/06/21 07:41:55 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_internal.h"
22#include "bn_local.h"
23
24#ifndef HAVE_BN_WORD_CLZ
25int
26bn_word_clz(BN_ULONG w)
27{
28 BN_ULONG bits, mask, shift;
29
30 bits = shift = BN_BITS2;
31 mask = 0;
32
33 while ((shift >>= 1) != 0) {
34 bits += (shift & mask) - (shift & ~mask);
35 mask = bn_ct_ne_zero_mask(w >> bits);
36 }
37 bits += 1 & mask;
38
39 bits -= bn_ct_eq_zero(w);
40
41 return BN_BITS2 - bits;
42}
43#endif
44
45#ifndef HAVE_BN_BITSIZE
46int
47bn_bitsize(const BIGNUM *bn)
48{
49 BN_ULONG n = 0, x = 0;
50 BN_ULONG mask, w;
51 int i = 0;
52
53 while (i < bn->top) {
54 w = bn->d[i];
55 mask = bn_ct_ne_zero_mask(w);
56 n = ((BN_ULONG)i & mask) | (n & ~mask);
57 x = (w & mask) | (x & ~mask);
58 i++;
59 }
60
61 return (n + 1) * BN_BITS2 - bn_word_clz(x);
62}
63#endif