]>
Commit | Line | Data |
---|---|---|
4f22f405 | 1 | /* |
edea42c6 | 2 | * Copyright 1995-2017 The OpenSSL Project Authors. All Rights Reserved. |
d02b48c6 | 3 | * |
4f22f405 RS |
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 | |
d02b48c6 RE |
8 | */ |
9 | ||
b39fc560 | 10 | #include "internal/cryptlib.h" |
d02b48c6 RE |
11 | #include "bn_lcl.h" |
12 | ||
6b691a5c | 13 | void BN_RECP_CTX_init(BN_RECP_CTX *recp) |
0f113f3e | 14 | { |
318447bc | 15 | memset(recp, 0, sizeof(*recp)); |
d59c7c81 RS |
16 | bn_init(&(recp->N)); |
17 | bn_init(&(recp->Nr)); | |
0f113f3e | 18 | } |
dfeab068 | 19 | |
6b691a5c | 20 | BN_RECP_CTX *BN_RECP_CTX_new(void) |
0f113f3e MC |
21 | { |
22 | BN_RECP_CTX *ret; | |
dfeab068 | 23 | |
a2d0baa2 | 24 | if ((ret = OPENSSL_zalloc(sizeof(*ret))) == NULL) |
0f113f3e | 25 | return (NULL); |
dfeab068 | 26 | |
318447bc F |
27 | bn_init(&(ret->N)); |
28 | bn_init(&(ret->Nr)); | |
0f113f3e MC |
29 | ret->flags = BN_FLG_MALLOCED; |
30 | return (ret); | |
31 | } | |
dfeab068 | 32 | |
6b691a5c | 33 | void BN_RECP_CTX_free(BN_RECP_CTX *recp) |
0f113f3e MC |
34 | { |
35 | if (recp == NULL) | |
36 | return; | |
e03ddfae | 37 | |
0f113f3e MC |
38 | BN_free(&(recp->N)); |
39 | BN_free(&(recp->Nr)); | |
40 | if (recp->flags & BN_FLG_MALLOCED) | |
41 | OPENSSL_free(recp); | |
42 | } | |
dfeab068 | 43 | |
84c15db5 | 44 | int BN_RECP_CTX_set(BN_RECP_CTX *recp, const BIGNUM *d, BN_CTX *ctx) |
0f113f3e MC |
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 | } | |
dfeab068 | 53 | |
020fc820 | 54 | int BN_mod_mul_reciprocal(BIGNUM *r, const BIGNUM *x, const BIGNUM *y, |
0f113f3e MC |
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 | } | |
dfeab068 | 82 | |
020fc820 | 83 | int BN_div_recp(BIGNUM *dv, BIGNUM *rem, const BIGNUM *m, |
0f113f3e MC |
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); | |
edea42c6 PY |
90 | d = (dv != NULL) ? dv : BN_CTX_get(ctx); |
91 | r = (rem != NULL) ? rem : BN_CTX_get(ctx); | |
0f113f3e MC |
92 | a = BN_CTX_get(ctx); |
93 | b = BN_CTX_get(ctx); | |
edea42c6 | 94 | if (b == NULL) |
0f113f3e MC |
95 | goto err; |
96 | ||
97 | if (BN_ucmp(m, &(recp->N)) < 0) { | |
98 | BN_zero(d); | |
3f6c7691 AG |
99 | if (!BN_copy(r, m)) { |
100 | BN_CTX_end(ctx); | |
0f113f3e | 101 | return 0; |
3f6c7691 | 102 | } |
0f113f3e MC |
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; | |
dfeab068 | 124 | |
e95bbc3c AP |
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 | */ | |
0f113f3e MC |
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; | |
dfeab068 | 144 | |
0f113f3e MC |
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 | } | |
d02b48c6 | 156 | |
0f113f3e MC |
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. | |
dfeab068 | 171 | */ |
78a0c1f1 | 172 | /* r := 2^len / m */ |
6343829a | 173 | int BN_reciprocal(BIGNUM *r, const BIGNUM *m, int len, BN_CTX *ctx) |
0f113f3e MC |
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 | } |