Coverage Report

Created: 2018-08-29 13:53

/src/openssl/crypto/bn/bn_word.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
BN_ULONG BN_mod_word(const BIGNUM *a, BN_ULONG w)
14
0
{
15
0
#ifndef BN_LLONG
16
0
    BN_ULONG ret = 0;
17
#else
18
    BN_ULLONG ret = 0;
19
#endif
20
    int i;
21
0
22
0
    if (w == 0)
23
0
        return (BN_ULONG)-1;
24
0
25
0
#ifndef BN_LLONG
26
0
    /*
27
0
     * If |w| is too long and we don't have BN_ULLONG then we need to fall
28
0
     * back to using BN_div_word
29
0
     */
30
0
    if (w > ((BN_ULONG)1 << BN_BITS4)) {
31
0
        BIGNUM *tmp = BN_dup(a);
32
0
        if (tmp == NULL)
33
0
            return (BN_ULONG)-1;
34
0
35
0
        ret = BN_div_word(tmp, w);
36
0
        BN_free(tmp);
37
0
38
0
        return ret;
39
0
    }
40
0
#endif
41
0
42
0
    bn_check_top(a);
43
0
    w &= BN_MASK2;
44
0
    for (i = a->top - 1; i >= 0; i--) {
45
0
#ifndef BN_LLONG
46
0
        /*
47
0
         * We can assume here that | w <= ((BN_ULONG)1 << BN_BITS4) | and so
48
0
         * | ret < ((BN_ULONG)1 << BN_BITS4) | and therefore the shifts here are
49
0
         * safe and will not overflow
50
0
         */
51
0
        ret = ((ret << BN_BITS4) | ((a->d[i] >> BN_BITS4) & BN_MASK2l)) % w;
52
0
        ret = ((ret << BN_BITS4) | (a->d[i] & BN_MASK2l)) % w;
53
#else
54
        ret = (BN_ULLONG) (((ret << (BN_ULLONG) BN_BITS2) | a->d[i]) %
55
                           (BN_ULLONG) w);
56
#endif
57
    }
58
0
    return (BN_ULONG)ret;
59
0
}
60
61
BN_ULONG BN_div_word(BIGNUM *a, BN_ULONG w)
62
166k
{
63
166k
    BN_ULONG ret = 0;
64
166k
    int i, j;
65
166k
66
166k
    bn_check_top(a);
67
166k
    w &= BN_MASK2;
68
166k
69
166k
    if (!w)
70
0
        /* actually this an error (division by zero) */
71
0
        return (BN_ULONG)-1;
72
166k
    if (a->top == 0)
73
0
        return 0;
74
166k
75
166k
    /* normalize input (so bn_div_words doesn't complain) */
76
166k
    j = BN_BITS2 - BN_num_bits_word(w);
77
166k
    w <<= j;
78
166k
    if (!BN_lshift(a, a, j))
79
0
        return (BN_ULONG)-1;
80
166k
81
19.7M
    for (i = a->top - 1; i >= 0; i--) {
82
19.6M
        BN_ULONG l, d;
83
19.6M
84
19.6M
        l = a->d[i];
85
19.6M
        d = bn_div_words(ret, l, w);
86
19.6M
        ret = (l - ((d * w) & BN_MASK2)) & BN_MASK2;
87
19.6M
        a->d[i] = d;
88
19.6M
    }
89
166k
    if ((a->top > 0) && (a->d[a->top - 1] == 0))
90
162k
        a->top--;
91
166k
    ret >>= j;
92
166k
    if (!a->top)
93
30.3k
        a->neg = 0; /* don't allow negative zero */
94
166k
    bn_check_top(a);
95
166k
    return ret;
96
166k
}
97
98
int BN_add_word(BIGNUM *a, BN_ULONG w)
99
1.06M
{
100
1.06M
    BN_ULONG l;
101
1.06M
    int i;
102
1.06M
103
1.06M
    bn_check_top(a);
104
1.06M
    w &= BN_MASK2;
105
1.06M
106
1.06M
    /* degenerate case: w is zero */
107
1.06M
    if (!w)
108
40.9k
        return 1;
109
1.01M
    /* degenerate case: a is zero */
110
1.01M
    if (BN_is_zero(a))
111
0
        return BN_set_word(a, w);
112
1.01M
    /* handle 'a' when negative */
113
1.01M
    if (a->neg) {
114
0
        a->neg = 0;
115
0
        i = BN_sub_word(a, w);
116
0
        if (!BN_is_zero(a))
117
0
            a->neg = !(a->neg);
118
0
        return i;
119
0
    }
120
2.03M
    for (i = 0; w != 0 && i < a->top; i++) {
121
1.01M
        a->d[i] = l = (a->d[i] + w) & BN_MASK2;
122
1.01M
        w = (w > l) ? 1 : 0;
123
1.01M
    }
124
1.01M
    if (w && i == a->top) {
125
0
        if (bn_wexpand(a, a->top + 1) == NULL)
126
0
            return 0;
127
0
        a->top++;
128
0
        a->d[i] = w;
129
0
    }
130
1.01M
    bn_check_top(a);
131
1.01M
    return 1;
132
1.01M
}
133
134
int BN_sub_word(BIGNUM *a, BN_ULONG w)
135
6.96k
{
136
6.96k
    int i;
137
6.96k
138
6.96k
    bn_check_top(a);
139
6.96k
    w &= BN_MASK2;
140
6.96k
141
6.96k
    /* degenerate case: w is zero */
142
6.96k
    if (!w)
143
0
        return 1;
144
6.96k
    /* degenerate case: a is zero */
145
6.96k
    if (BN_is_zero(a)) {
146
0
        i = BN_set_word(a, w);
147
0
        if (i != 0)
148
0
            BN_set_negative(a, 1);
149
0
        return i;
150
0
    }
151
6.96k
    /* handle 'a' when negative */
152
6.96k
    if (a->neg) {
153
0
        a->neg = 0;
154
0
        i = BN_add_word(a, w);
155
0
        a->neg = 1;
156
0
        return i;
157
0
    }
158
6.96k
159
6.96k
    if ((a->top == 1) && (a->d[0] < w)) {
160
0
        a->d[0] = w - a->d[0];
161
0
        a->neg = 1;
162
0
        return 1;
163
0
    }
164
6.96k
    i = 0;
165
9.49k
    for (;;) {
166
9.49k
        if (a->d[i] >= w) {
167
6.96k
            a->d[i] -= w;
168
6.96k
            break;
169
6.96k
        } else {
170
2.53k
            a->d[i] = (a->d[i] - w) & BN_MASK2;
171
2.53k
            i++;
172
2.53k
            w = 1;
173
2.53k
        }
174
9.49k
    }
175
6.96k
    if ((a->d[i] == 0) && (i == (a->top - 1)))
176
536
        a->top--;
177
6.96k
    bn_check_top(a);
178
6.96k
    return 1;
179
6.96k
}
180
181
int BN_mul_word(BIGNUM *a, BN_ULONG w)
182
0
{
183
0
    BN_ULONG ll;
184
0
185
0
    bn_check_top(a);
186
0
    w &= BN_MASK2;
187
0
    if (a->top) {
188
0
        if (w == 0)
189
0
            BN_zero(a);
190
0
        else {
191
0
            ll = bn_mul_words(a->d, a->d, a->top, w);
192
0
            if (ll) {
193
0
                if (bn_wexpand(a, a->top + 1) == NULL)
194
0
                    return 0;
195
0
                a->d[a->top++] = ll;
196
0
            }
197
0
        }
198
0
    }
199
0
    bn_check_top(a);
200
0
    return 1;
201
0
}