1e71b7053SJung-uk Kim /*
2*b077aed3SPierre Pronchery * Copyright 2014-2020 The OpenSSL Project Authors. All Rights Reserved.
3e71b7053SJung-uk Kim *
4*b077aed3SPierre Pronchery * Licensed under the Apache License 2.0 (the "License"). You may not use
5e71b7053SJung-uk Kim * this file except in compliance with the License. You can obtain a copy
6e71b7053SJung-uk Kim * in the file LICENSE in the source distribution or at
7e71b7053SJung-uk Kim * https://www.openssl.org/source/license.html
8e71b7053SJung-uk Kim */
9e71b7053SJung-uk Kim
10e71b7053SJung-uk Kim #include "internal/cryptlib.h"
1117f01e99SJung-uk Kim #include "bn_local.h"
12e71b7053SJung-uk Kim
13e71b7053SJung-uk Kim /*
14e71b7053SJung-uk Kim * Determine the modified width-(w+1) Non-Adjacent Form (wNAF) of 'scalar'.
15e71b7053SJung-uk Kim * This is an array r[] of values that are either zero or odd with an
16e71b7053SJung-uk Kim * absolute value less than 2^w satisfying
17e71b7053SJung-uk Kim * scalar = \sum_j r[j]*2^j
18e71b7053SJung-uk Kim * where at most one of any w+1 consecutive digits is non-zero
19e71b7053SJung-uk Kim * with the exception that the most significant digit may be only
20e71b7053SJung-uk Kim * w-1 zeros away from that next non-zero digit.
21e71b7053SJung-uk Kim */
bn_compute_wNAF(const BIGNUM * scalar,int w,size_t * ret_len)22e71b7053SJung-uk Kim signed char *bn_compute_wNAF(const BIGNUM *scalar, int w, size_t *ret_len)
23e71b7053SJung-uk Kim {
24e71b7053SJung-uk Kim int window_val;
25e71b7053SJung-uk Kim signed char *r = NULL;
26e71b7053SJung-uk Kim int sign = 1;
27e71b7053SJung-uk Kim int bit, next_bit, mask;
28e71b7053SJung-uk Kim size_t len = 0, j;
29e71b7053SJung-uk Kim
30e71b7053SJung-uk Kim if (BN_is_zero(scalar)) {
31e71b7053SJung-uk Kim r = OPENSSL_malloc(1);
32e71b7053SJung-uk Kim if (r == NULL) {
33*b077aed3SPierre Pronchery ERR_raise(ERR_LIB_BN, ERR_R_MALLOC_FAILURE);
34e71b7053SJung-uk Kim goto err;
35e71b7053SJung-uk Kim }
36e71b7053SJung-uk Kim r[0] = 0;
37e71b7053SJung-uk Kim *ret_len = 1;
38e71b7053SJung-uk Kim return r;
39e71b7053SJung-uk Kim }
40e71b7053SJung-uk Kim
41e71b7053SJung-uk Kim if (w <= 0 || w > 7) { /* 'signed char' can represent integers with
42e71b7053SJung-uk Kim * absolute values less than 2^7 */
43*b077aed3SPierre Pronchery ERR_raise(ERR_LIB_BN, ERR_R_INTERNAL_ERROR);
44e71b7053SJung-uk Kim goto err;
45e71b7053SJung-uk Kim }
46e71b7053SJung-uk Kim bit = 1 << w; /* at most 128 */
47e71b7053SJung-uk Kim next_bit = bit << 1; /* at most 256 */
48e71b7053SJung-uk Kim mask = next_bit - 1; /* at most 255 */
49e71b7053SJung-uk Kim
50e71b7053SJung-uk Kim if (BN_is_negative(scalar)) {
51e71b7053SJung-uk Kim sign = -1;
52e71b7053SJung-uk Kim }
53e71b7053SJung-uk Kim
54e71b7053SJung-uk Kim if (scalar->d == NULL || scalar->top == 0) {
55*b077aed3SPierre Pronchery ERR_raise(ERR_LIB_BN, ERR_R_INTERNAL_ERROR);
56e71b7053SJung-uk Kim goto err;
57e71b7053SJung-uk Kim }
58e71b7053SJung-uk Kim
59e71b7053SJung-uk Kim len = BN_num_bits(scalar);
60e71b7053SJung-uk Kim r = OPENSSL_malloc(len + 1); /*
61e71b7053SJung-uk Kim * Modified wNAF may be one digit longer than binary representation
62e71b7053SJung-uk Kim * (*ret_len will be set to the actual length, i.e. at most
63e71b7053SJung-uk Kim * BN_num_bits(scalar) + 1)
64e71b7053SJung-uk Kim */
65e71b7053SJung-uk Kim if (r == NULL) {
66*b077aed3SPierre Pronchery ERR_raise(ERR_LIB_BN, ERR_R_MALLOC_FAILURE);
67e71b7053SJung-uk Kim goto err;
68e71b7053SJung-uk Kim }
69e71b7053SJung-uk Kim window_val = scalar->d[0] & mask;
70e71b7053SJung-uk Kim j = 0;
71e71b7053SJung-uk Kim while ((window_val != 0) || (j + w + 1 < len)) { /* if j+w+1 >= len,
72e71b7053SJung-uk Kim * window_val will not
73e71b7053SJung-uk Kim * increase */
74e71b7053SJung-uk Kim int digit = 0;
75e71b7053SJung-uk Kim
76e71b7053SJung-uk Kim /* 0 <= window_val <= 2^(w+1) */
77e71b7053SJung-uk Kim
78e71b7053SJung-uk Kim if (window_val & 1) {
79e71b7053SJung-uk Kim /* 0 < window_val < 2^(w+1) */
80e71b7053SJung-uk Kim
81e71b7053SJung-uk Kim if (window_val & bit) {
82e71b7053SJung-uk Kim digit = window_val - next_bit; /* -2^w < digit < 0 */
83e71b7053SJung-uk Kim
84e71b7053SJung-uk Kim #if 1 /* modified wNAF */
85e71b7053SJung-uk Kim if (j + w + 1 >= len) {
86e71b7053SJung-uk Kim /*
87e71b7053SJung-uk Kim * Special case for generating modified wNAFs:
88e71b7053SJung-uk Kim * no new bits will be added into window_val,
89e71b7053SJung-uk Kim * so using a positive digit here will decrease
90e71b7053SJung-uk Kim * the total length of the representation
91e71b7053SJung-uk Kim */
92e71b7053SJung-uk Kim
93e71b7053SJung-uk Kim digit = window_val & (mask >> 1); /* 0 < digit < 2^w */
94e71b7053SJung-uk Kim }
95e71b7053SJung-uk Kim #endif
96e71b7053SJung-uk Kim } else {
97e71b7053SJung-uk Kim digit = window_val; /* 0 < digit < 2^w */
98e71b7053SJung-uk Kim }
99e71b7053SJung-uk Kim
100e71b7053SJung-uk Kim if (digit <= -bit || digit >= bit || !(digit & 1)) {
101*b077aed3SPierre Pronchery ERR_raise(ERR_LIB_BN, ERR_R_INTERNAL_ERROR);
102e71b7053SJung-uk Kim goto err;
103e71b7053SJung-uk Kim }
104e71b7053SJung-uk Kim
105e71b7053SJung-uk Kim window_val -= digit;
106e71b7053SJung-uk Kim
107e71b7053SJung-uk Kim /*
108e71b7053SJung-uk Kim * now window_val is 0 or 2^(w+1) in standard wNAF generation;
109e71b7053SJung-uk Kim * for modified window NAFs, it may also be 2^w
110e71b7053SJung-uk Kim */
111e71b7053SJung-uk Kim if (window_val != 0 && window_val != next_bit
112e71b7053SJung-uk Kim && window_val != bit) {
113*b077aed3SPierre Pronchery ERR_raise(ERR_LIB_BN, ERR_R_INTERNAL_ERROR);
114e71b7053SJung-uk Kim goto err;
115e71b7053SJung-uk Kim }
116e71b7053SJung-uk Kim }
117e71b7053SJung-uk Kim
118e71b7053SJung-uk Kim r[j++] = sign * digit;
119e71b7053SJung-uk Kim
120e71b7053SJung-uk Kim window_val >>= 1;
121e71b7053SJung-uk Kim window_val += bit * BN_is_bit_set(scalar, j + w);
122e71b7053SJung-uk Kim
123e71b7053SJung-uk Kim if (window_val > next_bit) {
124*b077aed3SPierre Pronchery ERR_raise(ERR_LIB_BN, ERR_R_INTERNAL_ERROR);
125e71b7053SJung-uk Kim goto err;
126e71b7053SJung-uk Kim }
127e71b7053SJung-uk Kim }
128e71b7053SJung-uk Kim
129e71b7053SJung-uk Kim if (j > len + 1) {
130*b077aed3SPierre Pronchery ERR_raise(ERR_LIB_BN, ERR_R_INTERNAL_ERROR);
131e71b7053SJung-uk Kim goto err;
132e71b7053SJung-uk Kim }
133e71b7053SJung-uk Kim *ret_len = j;
134e71b7053SJung-uk Kim return r;
135e71b7053SJung-uk Kim
136e71b7053SJung-uk Kim err:
137e71b7053SJung-uk Kim OPENSSL_free(r);
138e71b7053SJung-uk Kim return NULL;
139e71b7053SJung-uk Kim }
140e71b7053SJung-uk Kim
bn_get_top(const BIGNUM * a)141e71b7053SJung-uk Kim int bn_get_top(const BIGNUM *a)
142e71b7053SJung-uk Kim {
143e71b7053SJung-uk Kim return a->top;
144e71b7053SJung-uk Kim }
145e71b7053SJung-uk Kim
bn_get_dmax(const BIGNUM * a)146e71b7053SJung-uk Kim int bn_get_dmax(const BIGNUM *a)
147e71b7053SJung-uk Kim {
148e71b7053SJung-uk Kim return a->dmax;
149e71b7053SJung-uk Kim }
150e71b7053SJung-uk Kim
bn_set_all_zero(BIGNUM * a)151e71b7053SJung-uk Kim void bn_set_all_zero(BIGNUM *a)
152e71b7053SJung-uk Kim {
153e71b7053SJung-uk Kim int i;
154e71b7053SJung-uk Kim
155e71b7053SJung-uk Kim for (i = a->top; i < a->dmax; i++)
156e71b7053SJung-uk Kim a->d[i] = 0;
157e71b7053SJung-uk Kim }
158e71b7053SJung-uk Kim
bn_copy_words(BN_ULONG * out,const BIGNUM * in,int size)159e71b7053SJung-uk Kim int bn_copy_words(BN_ULONG *out, const BIGNUM *in, int size)
160e71b7053SJung-uk Kim {
161e71b7053SJung-uk Kim if (in->top > size)
162e71b7053SJung-uk Kim return 0;
163e71b7053SJung-uk Kim
164e71b7053SJung-uk Kim memset(out, 0, sizeof(*out) * size);
165e71b7053SJung-uk Kim if (in->d != NULL)
166e71b7053SJung-uk Kim memcpy(out, in->d, sizeof(*out) * in->top);
167e71b7053SJung-uk Kim return 1;
168e71b7053SJung-uk Kim }
169e71b7053SJung-uk Kim
bn_get_words(const BIGNUM * a)170e71b7053SJung-uk Kim BN_ULONG *bn_get_words(const BIGNUM *a)
171e71b7053SJung-uk Kim {
172e71b7053SJung-uk Kim return a->d;
173e71b7053SJung-uk Kim }
174e71b7053SJung-uk Kim
bn_set_static_words(BIGNUM * a,const BN_ULONG * words,int size)175e71b7053SJung-uk Kim void bn_set_static_words(BIGNUM *a, const BN_ULONG *words, int size)
176e71b7053SJung-uk Kim {
177e71b7053SJung-uk Kim /*
178e71b7053SJung-uk Kim * |const| qualifier omission is compensated by BN_FLG_STATIC_DATA
179e71b7053SJung-uk Kim * flag, which effectively means "read-only data".
180e71b7053SJung-uk Kim */
181e71b7053SJung-uk Kim a->d = (BN_ULONG *)words;
182e71b7053SJung-uk Kim a->dmax = a->top = size;
183e71b7053SJung-uk Kim a->neg = 0;
184e71b7053SJung-uk Kim a->flags |= BN_FLG_STATIC_DATA;
185e71b7053SJung-uk Kim bn_correct_top(a);
186e71b7053SJung-uk Kim }
187e71b7053SJung-uk Kim
bn_set_words(BIGNUM * a,const BN_ULONG * words,int num_words)188e71b7053SJung-uk Kim int bn_set_words(BIGNUM *a, const BN_ULONG *words, int num_words)
189e71b7053SJung-uk Kim {
190e71b7053SJung-uk Kim if (bn_wexpand(a, num_words) == NULL) {
191*b077aed3SPierre Pronchery ERR_raise(ERR_LIB_BN, ERR_R_MALLOC_FAILURE);
192e71b7053SJung-uk Kim return 0;
193e71b7053SJung-uk Kim }
194e71b7053SJung-uk Kim
195e71b7053SJung-uk Kim memcpy(a->d, words, sizeof(BN_ULONG) * num_words);
196e71b7053SJung-uk Kim a->top = num_words;
197e71b7053SJung-uk Kim bn_correct_top(a);
198e71b7053SJung-uk Kim return 1;
199e71b7053SJung-uk Kim }
200