Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-multiarch-jdk8u
Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/native/sun/security/ec/impl/mpmontg.c
38918 views
1
/*
2
* Copyright (c) 2007, 2011, Oracle and/or its affiliates. All rights reserved.
3
* Use is subject to license terms.
4
*
5
* This library is free software; you can redistribute it and/or
6
* modify it under the terms of the GNU Lesser General Public
7
* License as published by the Free Software Foundation; either
8
* version 2.1 of the License, or (at your option) any later version.
9
*
10
* This library is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13
* Lesser General Public License for more details.
14
*
15
* You should have received a copy of the GNU Lesser General Public License
16
* along with this library; if not, write to the Free Software Foundation,
17
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18
*
19
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20
* or visit www.oracle.com if you need additional information or have any
21
* questions.
22
*/
23
24
/* *********************************************************************
25
*
26
* The Original Code is the Netscape security libraries.
27
*
28
* The Initial Developer of the Original Code is
29
* Netscape Communications Corporation.
30
* Portions created by the Initial Developer are Copyright (C) 2000
31
* the Initial Developer. All Rights Reserved.
32
*
33
* Contributor(s):
34
* Sheueling Chang Shantz <[email protected]>,
35
* Stephen Fung <[email protected]>, and
36
* Douglas Stebila <[email protected]> of Sun Laboratories.
37
*
38
*********************************************************************** */
39
40
/* This file implements moduluar exponentiation using Montgomery's
41
* method for modular reduction. This file implements the method
42
* described as "Improvement 1" in the paper "A Cryptogrpahic Library for
43
* the Motorola DSP56000" by Stephen R. Dusse' and Burton S. Kaliski Jr.
44
* published in "Advances in Cryptology: Proceedings of EUROCRYPT '90"
45
* "Lecture Notes in Computer Science" volume 473, 1991, pg 230-244,
46
* published by Springer Verlag.
47
*/
48
49
#define MP_USING_CACHE_SAFE_MOD_EXP 1
50
#ifndef _KERNEL
51
#include <string.h>
52
#include <stddef.h> /* ptrdiff_t */
53
#endif
54
#include "mpi-priv.h"
55
#include "mplogic.h"
56
#include "mpprime.h"
57
#ifdef MP_USING_MONT_MULF
58
#include "montmulf.h"
59
#endif
60
61
/* if MP_CHAR_STORE_SLOW is defined, we */
62
/* need to know endianness of this platform. */
63
#ifdef MP_CHAR_STORE_SLOW
64
#if !defined(MP_IS_BIG_ENDIAN) && !defined(MP_IS_LITTLE_ENDIAN)
65
#error "You must define MP_IS_BIG_ENDIAN or MP_IS_LITTLE_ENDIAN\n" \
66
" if you define MP_CHAR_STORE_SLOW."
67
#endif
68
#endif
69
70
#ifndef STATIC
71
#define STATIC
72
#endif
73
74
#define MAX_ODD_INTS 32 /* 2 ** (WINDOW_BITS - 1) */
75
76
#ifndef _KERNEL
77
#if defined(_WIN32_WCE)
78
#define ABORT res = MP_UNDEF; goto CLEANUP
79
#else
80
#define ABORT abort()
81
#endif
82
#else
83
#define ABORT res = MP_UNDEF; goto CLEANUP
84
#endif /* _KERNEL */
85
86
/* computes T = REDC(T), 2^b == R */
87
mp_err s_mp_redc(mp_int *T, mp_mont_modulus *mmm)
88
{
89
mp_err res;
90
mp_size i;
91
92
i = MP_USED(T) + MP_USED(&mmm->N) + 2;
93
MP_CHECKOK( s_mp_pad(T, i) );
94
for (i = 0; i < MP_USED(&mmm->N); ++i ) {
95
mp_digit m_i = MP_DIGIT(T, i) * mmm->n0prime;
96
/* T += N * m_i * (MP_RADIX ** i); */
97
MP_CHECKOK( s_mp_mul_d_add_offset(&mmm->N, m_i, T, i) );
98
}
99
s_mp_clamp(T);
100
101
/* T /= R */
102
s_mp_div_2d(T, mmm->b);
103
104
if ((res = s_mp_cmp(T, &mmm->N)) >= 0) {
105
/* T = T - N */
106
MP_CHECKOK( s_mp_sub(T, &mmm->N) );
107
#ifdef DEBUG
108
if ((res = mp_cmp(T, &mmm->N)) >= 0) {
109
res = MP_UNDEF;
110
goto CLEANUP;
111
}
112
#endif
113
}
114
res = MP_OKAY;
115
CLEANUP:
116
return res;
117
}
118
119
#if !defined(MP_ASSEMBLY_MUL_MONT) && !defined(MP_MONT_USE_MP_MUL)
120
mp_err s_mp_mul_mont(const mp_int *a, const mp_int *b, mp_int *c,
121
mp_mont_modulus *mmm)
122
{
123
mp_digit *pb;
124
mp_digit m_i;
125
mp_err res;
126
mp_size ib;
127
mp_size useda, usedb;
128
129
ARGCHK(a != NULL && b != NULL && c != NULL, MP_BADARG);
130
131
if (MP_USED(a) < MP_USED(b)) {
132
const mp_int *xch = b; /* switch a and b, to do fewer outer loops */
133
b = a;
134
a = xch;
135
}
136
137
MP_USED(c) = 1; MP_DIGIT(c, 0) = 0;
138
ib = MP_USED(a) + MP_MAX(MP_USED(b), MP_USED(&mmm->N)) + 2;
139
if((res = s_mp_pad(c, ib)) != MP_OKAY)
140
goto CLEANUP;
141
142
useda = MP_USED(a);
143
pb = MP_DIGITS(b);
144
s_mpv_mul_d(MP_DIGITS(a), useda, *pb++, MP_DIGITS(c));
145
s_mp_setz(MP_DIGITS(c) + useda + 1, ib - (useda + 1));
146
m_i = MP_DIGIT(c, 0) * mmm->n0prime;
147
s_mp_mul_d_add_offset(&mmm->N, m_i, c, 0);
148
149
/* Outer loop: Digits of b */
150
usedb = MP_USED(b);
151
for (ib = 1; ib < usedb; ib++) {
152
mp_digit b_i = *pb++;
153
154
/* Inner product: Digits of a */
155
if (b_i)
156
s_mpv_mul_d_add_prop(MP_DIGITS(a), useda, b_i, MP_DIGITS(c) + ib);
157
m_i = MP_DIGIT(c, ib) * mmm->n0prime;
158
s_mp_mul_d_add_offset(&mmm->N, m_i, c, ib);
159
}
160
if (usedb < MP_USED(&mmm->N)) {
161
for (usedb = MP_USED(&mmm->N); ib < usedb; ++ib ) {
162
m_i = MP_DIGIT(c, ib) * mmm->n0prime;
163
s_mp_mul_d_add_offset(&mmm->N, m_i, c, ib);
164
}
165
}
166
s_mp_clamp(c);
167
s_mp_div_2d(c, mmm->b);
168
if (s_mp_cmp(c, &mmm->N) >= 0) {
169
MP_CHECKOK( s_mp_sub(c, &mmm->N) );
170
}
171
res = MP_OKAY;
172
173
CLEANUP:
174
return res;
175
}
176
#endif
177
178