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