/src/openssl/crypto/bn/bn_shift.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved. |
3 | | * |
4 | | * Licensed under the OpenSSL license (the "License"). You may not use |
5 | | * this file except in compliance with the License. You can obtain a copy |
6 | | * in the file LICENSE in the source distribution or at |
7 | | * https://www.openssl.org/source/license.html |
8 | | */ |
9 | | |
10 | | #include "internal/cryptlib.h" |
11 | | #include "bn_lcl.h" |
12 | | |
13 | | int BN_lshift1(BIGNUM *r, const BIGNUM *a) |
14 | 0 | { |
15 | 0 | register BN_ULONG *ap, *rp, t, c; |
16 | 0 | int i; |
17 | 0 |
|
18 | 0 | bn_check_top(r); |
19 | 0 | bn_check_top(a); |
20 | 0 |
|
21 | 0 | if (r != a) { |
22 | 0 | r->neg = a->neg; |
23 | 0 | if (bn_wexpand(r, a->top + 1) == NULL) |
24 | 0 | return 0; |
25 | 0 | r->top = a->top; |
26 | 0 | } else { |
27 | 0 | if (bn_wexpand(r, a->top + 1) == NULL) |
28 | 0 | return 0; |
29 | 0 | } |
30 | 0 | ap = a->d; |
31 | 0 | rp = r->d; |
32 | 0 | c = 0; |
33 | 0 | for (i = 0; i < a->top; i++) { |
34 | 0 | t = *(ap++); |
35 | 0 | *(rp++) = ((t << 1) | c) & BN_MASK2; |
36 | 0 | c = (t & BN_TBIT) ? 1 : 0; |
37 | 0 | } |
38 | 0 | if (c) { |
39 | 0 | *rp = 1; |
40 | 0 | r->top++; |
41 | 0 | } |
42 | 0 | bn_check_top(r); |
43 | 0 | return 1; |
44 | 0 | } |
45 | | |
46 | | int BN_rshift1(BIGNUM *r, const BIGNUM *a) |
47 | 0 | { |
48 | 0 | BN_ULONG *ap, *rp, t, c; |
49 | 0 | int i, j; |
50 | 0 |
|
51 | 0 | bn_check_top(r); |
52 | 0 | bn_check_top(a); |
53 | 0 |
|
54 | 0 | if (BN_is_zero(a)) { |
55 | 0 | BN_zero(r); |
56 | 0 | return 1; |
57 | 0 | } |
58 | 0 | i = a->top; |
59 | 0 | ap = a->d; |
60 | 0 | j = i - (ap[i - 1] == 1); |
61 | 0 | if (a != r) { |
62 | 0 | if (bn_wexpand(r, j) == NULL) |
63 | 0 | return 0; |
64 | 0 | r->neg = a->neg; |
65 | 0 | } |
66 | 0 | rp = r->d; |
67 | 0 | t = ap[--i]; |
68 | 0 | c = (t & 1) ? BN_TBIT : 0; |
69 | 0 | if (t >>= 1) |
70 | 0 | rp[i] = t; |
71 | 0 | while (i > 0) { |
72 | 0 | t = ap[--i]; |
73 | 0 | rp[i] = ((t >> 1) & BN_MASK2) | c; |
74 | 0 | c = (t & 1) ? BN_TBIT : 0; |
75 | 0 | } |
76 | 0 | r->top = j; |
77 | 0 | if (!r->top) |
78 | 0 | r->neg = 0; /* don't allow negative zero */ |
79 | 0 | bn_check_top(r); |
80 | 0 | return 1; |
81 | 0 | } |
82 | | |
83 | | int BN_lshift(BIGNUM *r, const BIGNUM *a, int n) |
84 | 1.22M | { |
85 | 1.22M | int i, nw, lb, rb; |
86 | 1.22M | BN_ULONG *t, *f; |
87 | 1.22M | BN_ULONG l; |
88 | 1.22M | |
89 | 1.22M | bn_check_top(r); |
90 | 1.22M | bn_check_top(a); |
91 | 1.22M | |
92 | 1.22M | if (n < 0) { |
93 | 0 | BNerr(BN_F_BN_LSHIFT, BN_R_INVALID_SHIFT); |
94 | 0 | return 0; |
95 | 0 | } |
96 | 1.22M | |
97 | 1.22M | nw = n / BN_BITS2; |
98 | 1.22M | if (bn_wexpand(r, a->top + nw + 1) == NULL) |
99 | 1.22M | return 0; |
100 | 1.22M | r->neg = a->neg; |
101 | 1.22M | lb = n % BN_BITS2; |
102 | 1.22M | rb = BN_BITS2 - lb; |
103 | 1.22M | f = a->d; |
104 | 1.22M | t = r->d; |
105 | 1.22M | t[a->top + nw] = 0; |
106 | 1.22M | if (lb == 0) |
107 | 19.7M | for (i = a->top - 1; i >= 0; i--) |
108 | 19.6M | t[nw + i] = f[i]; |
109 | 1.06M | else |
110 | 176M | for (i = a->top - 1; i >= 0; i--) { |
111 | 175M | l = f[i]; |
112 | 175M | t[nw + i + 1] |= (l >> rb) & BN_MASK2; |
113 | 175M | t[nw + i] = (l << lb) & BN_MASK2; |
114 | 175M | } |
115 | 1.22M | memset(t, 0, sizeof(*t) * nw); |
116 | 1.22M | r->top = a->top + nw + 1; |
117 | 1.22M | bn_correct_top(r); |
118 | 1.22M | bn_check_top(r); |
119 | 1.22M | return 1; |
120 | 1.22M | } |
121 | | |
122 | | int BN_rshift(BIGNUM *r, const BIGNUM *a, int n) |
123 | 0 | { |
124 | 0 | int i, j, nw, lb, rb; |
125 | 0 | BN_ULONG *t, *f; |
126 | 0 | BN_ULONG l, tmp; |
127 | 0 |
|
128 | 0 | bn_check_top(r); |
129 | 0 | bn_check_top(a); |
130 | 0 |
|
131 | 0 | if (n < 0) { |
132 | 0 | BNerr(BN_F_BN_RSHIFT, BN_R_INVALID_SHIFT); |
133 | 0 | return 0; |
134 | 0 | } |
135 | 0 |
|
136 | 0 | nw = n / BN_BITS2; |
137 | 0 | rb = n % BN_BITS2; |
138 | 0 | lb = BN_BITS2 - rb; |
139 | 0 | if (nw >= a->top || a->top == 0) { |
140 | 0 | BN_zero(r); |
141 | 0 | return 1; |
142 | 0 | } |
143 | 0 | i = (BN_num_bits(a) - n + (BN_BITS2 - 1)) / BN_BITS2; |
144 | 0 | if (r != a) { |
145 | 0 | if (bn_wexpand(r, i) == NULL) |
146 | 0 | return 0; |
147 | 0 | r->neg = a->neg; |
148 | 0 | } else { |
149 | 0 | if (n == 0) |
150 | 0 | return 1; /* or the copying loop will go berserk */ |
151 | 0 | } |
152 | 0 | |
153 | 0 | f = &(a->d[nw]); |
154 | 0 | t = r->d; |
155 | 0 | j = a->top - nw; |
156 | 0 | r->top = i; |
157 | 0 |
|
158 | 0 | if (rb == 0) { |
159 | 0 | for (i = j; i != 0; i--) |
160 | 0 | *(t++) = *(f++); |
161 | 0 | } else { |
162 | 0 | l = *(f++); |
163 | 0 | for (i = j - 1; i != 0; i--) { |
164 | 0 | tmp = (l >> rb) & BN_MASK2; |
165 | 0 | l = *(f++); |
166 | 0 | *(t++) = (tmp | (l << lb)) & BN_MASK2; |
167 | 0 | } |
168 | 0 | if ((l = (l >> rb) & BN_MASK2)) |
169 | 0 | *(t) = l; |
170 | 0 | } |
171 | 0 | if (!r->top) |
172 | 0 | r->neg = 0; /* don't allow negative zero */ |
173 | 0 | bn_check_top(r); |
174 | 0 | return 1; |
175 | 0 | } |