]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/ec/ecp_nistputil.c
Copyright consolidation 04/10
[thirdparty/openssl.git] / crypto / ec / ecp_nistputil.c
CommitLineData
3e00b4c9
BM
1/*
2 * Written by Bodo Moeller for the OpenSSL project.
3 */
4/* Copyright 2011 Google Inc.
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 *
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 */
19
e0d6132b 20#include <openssl/opensslconf.h>
effaf4de
RS
21#ifdef OPENSSL_NO_EC_NISTP_64_GCC_128
22NON_EMPTY_TRANSLATION_UNIT
23#else
3e00b4c9
BM
24
25/*
26 * Common utility functions for ecp_nistp224.c, ecp_nistp256.c, ecp_nistp521.c.
27 */
28
0f113f3e
MC
29# include <stddef.h>
30# include "ec_lcl.h"
3e00b4c9 31
0f113f3e
MC
32/*
33 * Convert an array of points into affine coordinates. (If the point at
34 * infinity is found (Z = 0), it remains unchanged.) This function is
35 * essentially an equivalent to EC_POINTs_make_affine(), but works with the
36 * internal representation of points as used by ecp_nistp###.c rather than
37 * with (BIGNUM-based) EC_POINT data structures. point_array is the
38 * input/output buffer ('num' points in projective form, i.e. three
39 * coordinates each), based on an internal representation of field elements
40 * of size 'felem_size'. tmp_felems needs to point to a temporary array of
41 * 'num'+1 field elements for storage of intermediate values.
3e00b4c9
BM
42 */
43void ec_GFp_nistp_points_make_affine_internal(size_t num, void *point_array,
0f113f3e
MC
44 size_t felem_size,
45 void *tmp_felems,
46 void (*felem_one) (void *out),
47 int (*felem_is_zero) (const void
48 *in),
49 void (*felem_assign) (void *out,
50 const void
51 *in),
52 void (*felem_square) (void *out,
53 const void
54 *in),
55 void (*felem_mul) (void *out,
56 const void
57 *in1,
58 const void
59 *in2),
60 void (*felem_inv) (void *out,
61 const void
62 *in),
63 void (*felem_contract) (void
64 *out,
65 const
66 void
67 *in))
68{
69 int i = 0;
3e00b4c9 70
0f113f3e
MC
71# define tmp_felem(I) (&((char *)tmp_felems)[(I) * felem_size])
72# define X(I) (&((char *)point_array)[3*(I) * felem_size])
73# define Y(I) (&((char *)point_array)[(3*(I) + 1) * felem_size])
74# define Z(I) (&((char *)point_array)[(3*(I) + 2) * felem_size])
3e00b4c9 75
0f113f3e
MC
76 if (!felem_is_zero(Z(0)))
77 felem_assign(tmp_felem(0), Z(0));
78 else
79 felem_one(tmp_felem(0));
80 for (i = 1; i < (int)num; i++) {
81 if (!felem_is_zero(Z(i)))
82 felem_mul(tmp_felem(i), tmp_felem(i - 1), Z(i));
83 else
84 felem_assign(tmp_felem(i), tmp_felem(i - 1));
85 }
86 /*
87 * Now each tmp_felem(i) is the product of Z(0) .. Z(i), skipping any
88 * zero-valued factors: if Z(i) = 0, we essentially pretend that Z(i) = 1
89 */
3e00b4c9 90
0f113f3e
MC
91 felem_inv(tmp_felem(num - 1), tmp_felem(num - 1));
92 for (i = num - 1; i >= 0; i--) {
93 if (i > 0)
94 /*
95 * tmp_felem(i-1) is the product of Z(0) .. Z(i-1), tmp_felem(i)
96 * is the inverse of the product of Z(0) .. Z(i)
97 */
98 /* 1/Z(i) */
99 felem_mul(tmp_felem(num), tmp_felem(i - 1), tmp_felem(i));
100 else
101 felem_assign(tmp_felem(num), tmp_felem(0)); /* 1/Z(0) */
3e00b4c9 102
0f113f3e
MC
103 if (!felem_is_zero(Z(i))) {
104 if (i > 0)
105 /*
106 * For next iteration, replace tmp_felem(i-1) by its inverse
107 */
108 felem_mul(tmp_felem(i - 1), tmp_felem(i), Z(i));
3e00b4c9 109
0f113f3e
MC
110 /*
111 * Convert point (X, Y, Z) into affine form (X/(Z^2), Y/(Z^3), 1)
112 */
113 felem_square(Z(i), tmp_felem(num)); /* 1/(Z^2) */
114 felem_mul(X(i), X(i), Z(i)); /* X/(Z^2) */
115 felem_mul(Z(i), Z(i), tmp_felem(num)); /* 1/(Z^3) */
116 felem_mul(Y(i), Y(i), Z(i)); /* Y/(Z^3) */
117 felem_contract(X(i), X(i));
118 felem_contract(Y(i), Y(i));
119 felem_one(Z(i));
120 } else {
121 if (i > 0)
122 /*
123 * For next iteration, replace tmp_felem(i-1) by its inverse
124 */
125 felem_assign(tmp_felem(i - 1), tmp_felem(i));
126 }
127 }
128}
3e00b4c9 129
1d97c843 130/*-
3e00b4c9
BM
131 * This function looks at 5+1 scalar bits (5 current, 1 adjacent less
132 * significant bit), and recodes them into a signed digit for use in fast point
133 * multiplication: the use of signed rather than unsigned digits means that
134 * fewer points need to be precomputed, given that point inversion is easy
135 * (a precomputed point dP makes -dP available as well).
136 *
137 * BACKGROUND:
138 *
139 * Signed digits for multiplication were introduced by Booth ("A signed binary
140 * multiplication technique", Quart. Journ. Mech. and Applied Math., vol. IV,
141 * pt. 2 (1951), pp. 236-240), in that case for multiplication of integers.
142 * Booth's original encoding did not generally improve the density of nonzero
143 * digits over the binary representation, and was merely meant to simplify the
144 * handling of signed factors given in two's complement; but it has since been
145 * shown to be the basis of various signed-digit representations that do have
146 * further advantages, including the wNAF, using the following general approach:
147 *
148 * (1) Given a binary representation
149 *
150 * b_k ... b_2 b_1 b_0,
151 *
152 * of a nonnegative integer (b_k in {0, 1}), rewrite it in digits 0, 1, -1
153 * by using bit-wise subtraction as follows:
154 *
155 * b_k b_(k-1) ... b_2 b_1 b_0
156 * - b_k ... b_3 b_2 b_1 b_0
157 * -------------------------------------
158 * s_k b_(k-1) ... s_3 s_2 s_1 s_0
159 *
160 * A left-shift followed by subtraction of the original value yields a new
161 * representation of the same value, using signed bits s_i = b_(i+1) - b_i.
162 * This representation from Booth's paper has since appeared in the
163 * literature under a variety of different names including "reversed binary
164 * form", "alternating greedy expansion", "mutual opposite form", and
165 * "sign-alternating {+-1}-representation".
166 *
167 * An interesting property is that among the nonzero bits, values 1 and -1
168 * strictly alternate.
169 *
170 * (2) Various window schemes can be applied to the Booth representation of
171 * integers: for example, right-to-left sliding windows yield the wNAF
172 * (a signed-digit encoding independently discovered by various researchers
173 * in the 1990s), and left-to-right sliding windows yield a left-to-right
174 * equivalent of the wNAF (independently discovered by various researchers
175 * around 2004).
176 *
177 * To prevent leaking information through side channels in point multiplication,
178 * we need to recode the given integer into a regular pattern: sliding windows
179 * as in wNAFs won't do, we need their fixed-window equivalent -- which is a few
180 * decades older: we'll be using the so-called "modified Booth encoding" due to
181 * MacSorley ("High-speed arithmetic in binary computers", Proc. IRE, vol. 49
182 * (1961), pp. 67-91), in a radix-2^5 setting. That is, we always combine five
183 * signed bits into a signed digit:
184 *
185 * s_(4j + 4) s_(4j + 3) s_(4j + 2) s_(4j + 1) s_(4j)
186 *
187 * The sign-alternating property implies that the resulting digit values are
188 * integers from -16 to 16.
189 *
190 * Of course, we don't actually need to compute the signed digits s_i as an
191 * intermediate step (that's just a nice way to see how this scheme relates
192 * to the wNAF): a direct computation obtains the recoded digit from the
193 * six bits b_(4j + 4) ... b_(4j - 1).
194 *
195 * This function takes those five bits as an integer (0 .. 63), writing the
196 * recoded digit to *sign (0 for positive, 1 for negative) and *digit (absolute
197 * value, in the range 0 .. 8). Note that this integer essentially provides the
198 * input bits "shifted to the left" by one position: for example, the input to
199 * compute the least significant recoded digit, given that there's no bit b_-1,
200 * has to be b_4 b_3 b_2 b_1 b_0 0.
201 *
202 */
0f113f3e
MC
203void ec_GFp_nistp_recode_scalar_bits(unsigned char *sign,
204 unsigned char *digit, unsigned char in)
205{
206 unsigned char s, d;
3e00b4c9 207
0f113f3e
MC
208 s = ~((in >> 5) - 1); /* sets all bits to MSB(in), 'in' seen as
209 * 6-bit value */
210 d = (1 << 6) - in - 1;
211 d = (d & s) | (in & ~s);
212 d = (d >> 1) + (d & 1);
3e00b4c9 213
0f113f3e
MC
214 *sign = s & 1;
215 *digit = d;
216}
3e00b4c9 217#endif