aboutsummaryrefslogtreecommitdiff
path: root/externals/libressl/crypto/bn/bn_shift.c
blob: eee343670226d08bd91fc25282e22265ae433fea (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/* $OpenBSD: bn_shift.c,v 1.21 2023/02/13 04:25:37 jsing Exp $ */
/*
 * Copyright (c) 2022, 2023 Joel Sing <jsing@openbsd.org>
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#include <openssl/bn.h>
#include <openssl/err.h>

#include "bn_local.h"

static inline int
bn_lshift(BIGNUM *r, const BIGNUM *a, int n)
{
	size_t count, shift_bits, shift_words;
	size_t lshift, rshift;
	ssize_t rstride;
	BN_ULONG *dst, *src;

	if (n < 0) {
		BNerror(BN_R_INVALID_LENGTH);
		return 0;
	}
	shift_bits = n;

	/*
	 * Left bit shift, potentially across word boundaries.
	 *
	 * When shift is not an exact multiple of BN_BITS2, the bottom bits of
	 * the previous word need to be right shifted and combined with the left
	 * shifted bits using bitwise OR. If shift is an exact multiple of
	 * BN_BITS2, the source for the left and right shifts are the same
	 * and the shifts become zero bits (which is effectively a memmove).
	 */
	shift_words = shift_bits / BN_BITS2;
	lshift = shift_bits % BN_BITS2;
	rshift = (BN_BITS2 - lshift) % BN_BITS2;
	rstride = 0 - (lshift + rshift) / BN_BITS2;

	if (a->top < 1) {
		BN_zero(r);
		return 1;
	}

	count = a->top + shift_words + 1;

	if (count < shift_words)
		return 0;

	if (!bn_wexpand(r, count))
		return 0;

	src = a->d + a->top - 1;
	dst = r->d + a->top + shift_words;

	/* Handle right shift for top most word. */
	*dst = (*src >> rshift) & rstride;
	dst--;

	/* Handle left shift and right shift for remaining words. */
	while (src > a->d) {
		*dst = *src << lshift | src[rstride] >> rshift;
		src--;
		dst--;
	}
	*dst = *src << lshift;

	/* Zero any additional words resulting from the left shift. */
	while (dst > r->d) {
		dst--;
		*dst = 0;
	}

	r->top = count;
	bn_correct_top(r);

	BN_set_negative(r, a->neg);

	return 1;
}

static inline int
bn_rshift(BIGNUM *r, const BIGNUM *a, int n)
{
	size_t count, shift_bits, shift_words;
	size_t lshift, rshift;
	ssize_t lstride;
	BN_ULONG *dst, *src;
	size_t i;

	if (n < 0) {
		BNerror(BN_R_INVALID_LENGTH);
		return 0;
	}
	shift_bits = n;

	/*
	 * Right bit shift, potentially across word boundaries.
	 *
	 * When shift is not an exact multiple of BN_BITS2, the top bits of
	 * the next word need to be left shifted and combined with the right
	 * shifted bits using bitwise OR. If shift is an exact multiple of
	 * BN_BITS2, the source for the left and right shifts are the same
	 * and the shifts become zero (which is effectively a memmove).
	 */
	shift_words = shift_bits / BN_BITS2;
	rshift = shift_bits % BN_BITS2;
	lshift = (BN_BITS2 - rshift) % BN_BITS2;
	lstride = (lshift + rshift) / BN_BITS2;

	if (a->top <= shift_words) {
		BN_zero(r);
		return 1;
	}
	count = a->top - shift_words;

	if (!bn_wexpand(r, count))
		return 0;

	src = a->d + shift_words;
	dst = r->d;

	for (i = 1; i < count; i++) {
		*dst = src[lstride] << lshift | *src >> rshift;
		src++;
		dst++;
	}
	*dst = *src >> rshift;

	r->top = count;
	bn_correct_top(r);

	BN_set_negative(r, a->neg);

	return 1;
}

int
BN_lshift1(BIGNUM *r, const BIGNUM *a)
{
	return bn_lshift(r, a, 1);
}

int
BN_lshift(BIGNUM *r, const BIGNUM *a, int n)
{
	return bn_lshift(r, a, n);
}

int
BN_rshift1(BIGNUM *r, const BIGNUM *a)
{
	return bn_rshift(r, a, 1);
}

int
BN_rshift(BIGNUM *r, const BIGNUM *a, int n)
{
	return bn_rshift(r, a, n);
}