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