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