Coverage Report

Created: 2018-08-29 13:53

/src/openssl/crypto/bn/bn_recp.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright 1995-2018 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
void BN_RECP_CTX_init(BN_RECP_CTX *recp)
14
0
{
15
0
    memset(recp, 0, sizeof(*recp));
16
0
    bn_init(&(recp->N));
17
0
    bn_init(&(recp->Nr));
18
0
}
19
20
BN_RECP_CTX *BN_RECP_CTX_new(void)
21
0
{
22
0
    BN_RECP_CTX *ret;
23
0
24
0
    if ((ret = OPENSSL_zalloc(sizeof(*ret))) == NULL) {
25
0
        BNerr(BN_F_BN_RECP_CTX_NEW, ERR_R_MALLOC_FAILURE);
26
0
        return NULL;
27
0
    }
28
0
29
0
    bn_init(&(ret->N));
30
0
    bn_init(&(ret->Nr));
31
0
    ret->flags = BN_FLG_MALLOCED;
32
0
    return ret;
33
0
}
34
35
void BN_RECP_CTX_free(BN_RECP_CTX *recp)
36
0
{
37
0
    if (recp == NULL)
38
0
        return;
39
0
    BN_free(&recp->N);
40
0
    BN_free(&recp->Nr);
41
0
    if (recp->flags & BN_FLG_MALLOCED)
42
0
        OPENSSL_free(recp);
43
0
}
44
45
int BN_RECP_CTX_set(BN_RECP_CTX *recp, const BIGNUM *d, BN_CTX *ctx)
46
0
{
47
0
    if (!BN_copy(&(recp->N), d))
48
0
        return 0;
49
0
    BN_zero(&(recp->Nr));
50
0
    recp->num_bits = BN_num_bits(d);
51
0
    recp->shift = 0;
52
0
    return 1;
53
0
}
54
55
int BN_mod_mul_reciprocal(BIGNUM *r, const BIGNUM *x, const BIGNUM *y,
56
                          BN_RECP_CTX *recp, BN_CTX *ctx)
57
0
{
58
0
    int ret = 0;
59
0
    BIGNUM *a;
60
0
    const BIGNUM *ca;
61
0
62
0
    BN_CTX_start(ctx);
63
0
    if ((a = BN_CTX_get(ctx)) == NULL)
64
0
        goto err;
65
0
    if (y != NULL) {
66
0
        if (x == y) {
67
0
            if (!BN_sqr(a, x, ctx))
68
0
                goto err;
69
0
        } else {
70
0
            if (!BN_mul(a, x, y, ctx))
71
0
                goto err;
72
0
        }
73
0
        ca = a;
74
0
    } else
75
0
        ca = x;                 /* Just do the mod */
76
0
77
0
    ret = BN_div_recp(NULL, r, ca, recp, ctx);
78
0
 err:
79
0
    BN_CTX_end(ctx);
80
0
    bn_check_top(r);
81
0
    return ret;
82
0
}
83
84
int BN_div_recp(BIGNUM *dv, BIGNUM *rem, const BIGNUM *m,
85
                BN_RECP_CTX *recp, BN_CTX *ctx)
86
0
{
87
0
    int i, j, ret = 0;
88
0
    BIGNUM *a, *b, *d, *r;
89
0
90
0
    BN_CTX_start(ctx);
91
0
    d = (dv != NULL) ? dv : BN_CTX_get(ctx);
92
0
    r = (rem != NULL) ? rem : BN_CTX_get(ctx);
93
0
    a = BN_CTX_get(ctx);
94
0
    b = BN_CTX_get(ctx);
95
0
    if (b == NULL)
96
0
        goto err;
97
0
98
0
    if (BN_ucmp(m, &(recp->N)) < 0) {
99
0
        BN_zero(d);
100
0
        if (!BN_copy(r, m)) {
101
0
            BN_CTX_end(ctx);
102
0
            return 0;
103
0
        }
104
0
        BN_CTX_end(ctx);
105
0
        return 1;
106
0
    }
107
0
108
0
    /*
109
0
     * We want the remainder Given input of ABCDEF / ab we need multiply
110
0
     * ABCDEF by 3 digests of the reciprocal of ab
111
0
     */
112
0
113
0
    /* i := max(BN_num_bits(m), 2*BN_num_bits(N)) */
114
0
    i = BN_num_bits(m);
115
0
    j = recp->num_bits << 1;
116
0
    if (j > i)
117
0
        i = j;
118
0
119
0
    /* Nr := round(2^i / N) */
120
0
    if (i != recp->shift)
121
0
        recp->shift = BN_reciprocal(&(recp->Nr), &(recp->N), i, ctx);
122
0
    /* BN_reciprocal could have returned -1 for an error */
123
0
    if (recp->shift == -1)
124
0
        goto err;
125
0
126
0
    /*-
127
0
     * d := |round(round(m / 2^BN_num_bits(N)) * recp->Nr / 2^(i - BN_num_bits(N)))|
128
0
     *    = |round(round(m / 2^BN_num_bits(N)) * round(2^i / N) / 2^(i - BN_num_bits(N)))|
129
0
     *   <= |(m / 2^BN_num_bits(N)) * (2^i / N) * (2^BN_num_bits(N) / 2^i)|
130
0
     *    = |m/N|
131
0
     */
132
0
    if (!BN_rshift(a, m, recp->num_bits))
133
0
        goto err;
134
0
    if (!BN_mul(b, a, &(recp->Nr), ctx))
135
0
        goto err;
136
0
    if (!BN_rshift(d, b, i - recp->num_bits))
137
0
        goto err;
138
0
    d->neg = 0;
139
0
140
0
    if (!BN_mul(b, &(recp->N), d, ctx))
141
0
        goto err;
142
0
    if (!BN_usub(r, m, b))
143
0
        goto err;
144
0
    r->neg = 0;
145
0
146
0
    j = 0;
147
0
    while (BN_ucmp(r, &(recp->N)) >= 0) {
148
0
        if (j++ > 2) {
149
0
            BNerr(BN_F_BN_DIV_RECP, BN_R_BAD_RECIPROCAL);
150
0
            goto err;
151
0
        }
152
0
        if (!BN_usub(r, r, &(recp->N)))
153
0
            goto err;
154
0
        if (!BN_add_word(d, 1))
155
0
            goto err;
156
0
    }
157
0
158
0
    r->neg = BN_is_zero(r) ? 0 : m->neg;
159
0
    d->neg = m->neg ^ recp->N.neg;
160
0
    ret = 1;
161
0
 err:
162
0
    BN_CTX_end(ctx);
163
0
    bn_check_top(dv);
164
0
    bn_check_top(rem);
165
0
    return ret;
166
0
}
167
168
/*
169
 * len is the expected size of the result We actually calculate with an extra
170
 * word of precision, so we can do faster division if the remainder is not
171
 * required.
172
 */
173
/* r := 2^len / m */
174
int BN_reciprocal(BIGNUM *r, const BIGNUM *m, int len, BN_CTX *ctx)
175
0
{
176
0
    int ret = -1;
177
0
    BIGNUM *t;
178
0
179
0
    BN_CTX_start(ctx);
180
0
    if ((t = BN_CTX_get(ctx)) == NULL)
181
0
        goto err;
182
0
183
0
    if (!BN_set_bit(t, len))
184
0
        goto err;
185
0
186
0
    if (!BN_div(r, NULL, t, m, ctx))
187
0
        goto err;
188
0
189
0
    ret = len;
190
0
 err:
191
0
    bn_check_top(r);
192
0
    BN_CTX_end(ctx);
193
0
    return ret;
194
0
}