New "i15" implementation of big integers (faster, and constant-time, on ARM Cortex...
[BearSSL] / src / int / i15_ext1.c
1 /*
2 * Copyright (c) 2017 Thomas Pornin <pornin@bolet.org>
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining
5 * a copy of this software and associated documentation files (the
6 * "Software"), to deal in the Software without restriction, including
7 * without limitation the rights to use, copy, modify, merge, publish,
8 * distribute, sublicense, and/or sell copies of the Software, and to
9 * permit persons to whom the Software is furnished to do so, subject to
10 * the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
19 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
20 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 */
24
25 #include "inner.h"
26
27 /*
28 * This file contains some additional functions for "i15" big integers.
29 * These functions are needed to support ECDSA.
30 */
31
32 /* see inner.h */
33 void
34 br_i15_rshift(uint16_t *x, int count)
35 {
36 size_t u, len;
37 unsigned r;
38
39 len = (x[0] + 15) >> 4;
40 if (len == 0) {
41 return;
42 }
43 r = x[1] >> count;
44 for (u = 2; u <= len; u ++) {
45 unsigned w;
46
47 w = x[u];
48 x[u - 1] = ((w << (15 - count)) | r) & 0x7FFF;
49 r = w >> count;
50 }
51 x[len] = r;
52 }
53
54 /* see inner.h */
55 uint32_t
56 br_i15_bit_length(uint16_t *x, size_t xlen)
57 {
58 uint32_t tw, twk;
59
60 tw = 0;
61 twk = 0;
62 while (xlen -- > 0) {
63 uint32_t w, c;
64
65 c = EQ(tw, 0);
66 w = x[xlen];
67 tw = MUX(c, w, tw);
68 twk = MUX(c, (uint32_t)xlen, twk);
69 }
70 return (twk << 4) + BIT_LENGTH(tw);
71 }
72
73 /* see inner.h */
74 void
75 br_i15_decode(uint16_t *x, const void *src, size_t len)
76 {
77 const unsigned char *buf;
78 size_t v;
79 uint32_t acc;
80 int acc_len;
81
82 buf = src;
83 v = 1;
84 acc = 0;
85 acc_len = 0;
86 while (len -- > 0) {
87 uint32_t b;
88
89 b = buf[len];
90 acc |= (b << acc_len);
91 acc_len += 8;
92 if (acc_len >= 15) {
93 x[v ++] = acc & 0x7FFF;
94 acc_len -= 15;
95 acc >>= 15;
96 }
97 }
98 if (acc_len != 0) {
99 x[v ++] = acc;
100 }
101 x[0] = br_i15_bit_length(x + 1, v - 1);
102 }
103
104 /* see inner.h */
105 void
106 br_i15_from_monty(uint16_t *x, const uint16_t *m, uint16_t m0i)
107 {
108 size_t len, u, v;
109
110 len = (m[0] + 15) >> 4;
111 for (u = 0; u < len; u ++) {
112 uint32_t f, cc;
113
114 f = MUL15(x[1], m0i) & 0x7FFF;
115 cc = 0;
116 for (v = 0; v < len; v ++) {
117 uint32_t z;
118
119 z = (uint32_t)x[v + 1] + MUL15(f, m[v + 1]) + cc;
120 cc = z >> 15;
121 if (v != 0) {
122 x[v] = z & 0x7FFF;
123 }
124 }
125 x[len] = cc;
126 }
127
128 /*
129 * We may have to do an extra subtraction, but only if the
130 * value in x[] is indeed greater than or equal to that of m[],
131 * which is why we must do two calls (first call computes the
132 * carry, second call performs the subtraction only if the carry
133 * is 0).
134 */
135 br_i15_sub(x, m, NOT(br_i15_sub(x, m, 0)));
136 }