1d9e397b599b13d642138480a28c14db7a136bf0Adam Langley/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) 2d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * All rights reserved. 3d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * 4d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * This package is an SSL implementation written 5d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * by Eric Young (eay@cryptsoft.com). 6d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * The implementation was written so as to conform with Netscapes SSL. 7d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * 8d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * This library is free for commercial and non-commercial use as long as 9d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * the following conditions are aheared to. The following conditions 10d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * apply to all code found in this distribution, be it the RC4, RSA, 11d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * lhash, DES, etc., code; not just the SSL code. The SSL documentation 12d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * included with this distribution is covered by the same copyright terms 13d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * except that the holder is Tim Hudson (tjh@cryptsoft.com). 14d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * 15d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * Copyright remains Eric Young's, and as such any Copyright notices in 16d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * the code are not to be removed. 17d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * If this package is used in a product, Eric Young should be given attribution 18d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * as the author of the parts of the library used. 19d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * This can be in the form of a textual message at program startup or 20d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * in documentation (online or textual) provided with the package. 21d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * 22d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * Redistribution and use in source and binary forms, with or without 23d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * modification, are permitted provided that the following conditions 24d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * are met: 25d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * 1. Redistributions of source code must retain the copyright 26d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * notice, this list of conditions and the following disclaimer. 27d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * 2. Redistributions in binary form must reproduce the above copyright 28d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * notice, this list of conditions and the following disclaimer in the 29d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * documentation and/or other materials provided with the distribution. 30d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * 3. All advertising materials mentioning features or use of this software 31d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * must display the following acknowledgement: 32d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * "This product includes cryptographic software written by 33d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * Eric Young (eay@cryptsoft.com)" 34d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * The word 'cryptographic' can be left out if the rouines from the library 35d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * being used are not cryptographic related :-). 36d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * 4. If you include any Windows specific code (or a derivative thereof) from 37d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * the apps directory (application code) you must include an acknowledgement: 38d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" 39d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * 40d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND 41d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 42d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 43d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 44d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 45d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 46d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 47d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 48d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 49d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 50d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * SUCH DAMAGE. 51d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * 52d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * The licence and distribution terms for any publically available version or 53d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * derivative of this code cannot be changed. i.e. this code cannot simply be 54d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * copied and put under another distribution licence 55d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * [including the GNU Public Licence.] */ 56d9e397b599b13d642138480a28c14db7a136bf0Adam Langley 57d9e397b599b13d642138480a28c14db7a136bf0Adam Langley#include <openssl/bn.h> 58d9e397b599b13d642138480a28c14db7a136bf0Adam Langley 59d9e397b599b13d642138480a28c14db7a136bf0Adam Langley#include <string.h> 60d9e397b599b13d642138480a28c14db7a136bf0Adam Langley 61f4e427204234da139fd0585def4b4e22502e33f0Adam Langley#include <openssl/err.h> 62f4e427204234da139fd0585def4b4e22502e33f0Adam Langley 63d9e397b599b13d642138480a28c14db7a136bf0Adam Langley#include "internal.h" 64d9e397b599b13d642138480a28c14db7a136bf0Adam Langley 65d9e397b599b13d642138480a28c14db7a136bf0Adam Langley 66d9e397b599b13d642138480a28c14db7a136bf0Adam Langleyint BN_lshift(BIGNUM *r, const BIGNUM *a, int n) { 67d9e397b599b13d642138480a28c14db7a136bf0Adam Langley int i, nw, lb, rb; 68d9e397b599b13d642138480a28c14db7a136bf0Adam Langley BN_ULONG *t, *f; 69d9e397b599b13d642138480a28c14db7a136bf0Adam Langley BN_ULONG l; 70d9e397b599b13d642138480a28c14db7a136bf0Adam Langley 71f4e427204234da139fd0585def4b4e22502e33f0Adam Langley if (n < 0) { 72b8494591d1b1a143f3b192d845c238bbf3bc629dKenny Root OPENSSL_PUT_ERROR(BN, BN_R_NEGATIVE_NUMBER); 73f4e427204234da139fd0585def4b4e22502e33f0Adam Langley return 0; 74f4e427204234da139fd0585def4b4e22502e33f0Adam Langley } 75f4e427204234da139fd0585def4b4e22502e33f0Adam Langley 76d9e397b599b13d642138480a28c14db7a136bf0Adam Langley r->neg = a->neg; 77d9e397b599b13d642138480a28c14db7a136bf0Adam Langley nw = n / BN_BITS2; 78d9e397b599b13d642138480a28c14db7a136bf0Adam Langley if (bn_wexpand(r, a->top + nw + 1) == NULL) { 79d9e397b599b13d642138480a28c14db7a136bf0Adam Langley return 0; 80d9e397b599b13d642138480a28c14db7a136bf0Adam Langley } 81d9e397b599b13d642138480a28c14db7a136bf0Adam Langley lb = n % BN_BITS2; 82d9e397b599b13d642138480a28c14db7a136bf0Adam Langley rb = BN_BITS2 - lb; 83d9e397b599b13d642138480a28c14db7a136bf0Adam Langley f = a->d; 84d9e397b599b13d642138480a28c14db7a136bf0Adam Langley t = r->d; 85d9e397b599b13d642138480a28c14db7a136bf0Adam Langley t[a->top + nw] = 0; 86d9e397b599b13d642138480a28c14db7a136bf0Adam Langley if (lb == 0) { 87d9e397b599b13d642138480a28c14db7a136bf0Adam Langley for (i = a->top - 1; i >= 0; i--) { 88d9e397b599b13d642138480a28c14db7a136bf0Adam Langley t[nw + i] = f[i]; 89d9e397b599b13d642138480a28c14db7a136bf0Adam Langley } 90d9e397b599b13d642138480a28c14db7a136bf0Adam Langley } else { 91d9e397b599b13d642138480a28c14db7a136bf0Adam Langley for (i = a->top - 1; i >= 0; i--) { 92d9e397b599b13d642138480a28c14db7a136bf0Adam Langley l = f[i]; 93d9e397b599b13d642138480a28c14db7a136bf0Adam Langley t[nw + i + 1] |= (l >> rb) & BN_MASK2; 94d9e397b599b13d642138480a28c14db7a136bf0Adam Langley t[nw + i] = (l << lb) & BN_MASK2; 95d9e397b599b13d642138480a28c14db7a136bf0Adam Langley } 96d9e397b599b13d642138480a28c14db7a136bf0Adam Langley } 97d9e397b599b13d642138480a28c14db7a136bf0Adam Langley memset(t, 0, nw * sizeof(t[0])); 98d9e397b599b13d642138480a28c14db7a136bf0Adam Langley r->top = a->top + nw + 1; 99d9e397b599b13d642138480a28c14db7a136bf0Adam Langley bn_correct_top(r); 100d9e397b599b13d642138480a28c14db7a136bf0Adam Langley 101d9e397b599b13d642138480a28c14db7a136bf0Adam Langley return 1; 102d9e397b599b13d642138480a28c14db7a136bf0Adam Langley} 103d9e397b599b13d642138480a28c14db7a136bf0Adam Langley 104d9e397b599b13d642138480a28c14db7a136bf0Adam Langleyint BN_lshift1(BIGNUM *r, const BIGNUM *a) { 105d9e397b599b13d642138480a28c14db7a136bf0Adam Langley BN_ULONG *ap, *rp, t, c; 106d9e397b599b13d642138480a28c14db7a136bf0Adam Langley int i; 107d9e397b599b13d642138480a28c14db7a136bf0Adam Langley 108d9e397b599b13d642138480a28c14db7a136bf0Adam Langley if (r != a) { 109d9e397b599b13d642138480a28c14db7a136bf0Adam Langley r->neg = a->neg; 110d9e397b599b13d642138480a28c14db7a136bf0Adam Langley if (bn_wexpand(r, a->top + 1) == NULL) { 111d9e397b599b13d642138480a28c14db7a136bf0Adam Langley return 0; 112d9e397b599b13d642138480a28c14db7a136bf0Adam Langley } 113d9e397b599b13d642138480a28c14db7a136bf0Adam Langley r->top = a->top; 114d9e397b599b13d642138480a28c14db7a136bf0Adam Langley } else { 115d9e397b599b13d642138480a28c14db7a136bf0Adam Langley if (bn_wexpand(r, a->top + 1) == NULL) { 116d9e397b599b13d642138480a28c14db7a136bf0Adam Langley return 0; 117d9e397b599b13d642138480a28c14db7a136bf0Adam Langley } 118d9e397b599b13d642138480a28c14db7a136bf0Adam Langley } 119d9e397b599b13d642138480a28c14db7a136bf0Adam Langley ap = a->d; 120d9e397b599b13d642138480a28c14db7a136bf0Adam Langley rp = r->d; 121d9e397b599b13d642138480a28c14db7a136bf0Adam Langley c = 0; 122d9e397b599b13d642138480a28c14db7a136bf0Adam Langley for (i = 0; i < a->top; i++) { 123d9e397b599b13d642138480a28c14db7a136bf0Adam Langley t = *(ap++); 124d9e397b599b13d642138480a28c14db7a136bf0Adam Langley *(rp++) = ((t << 1) | c) & BN_MASK2; 125d9e397b599b13d642138480a28c14db7a136bf0Adam Langley c = (t & BN_TBIT) ? 1 : 0; 126d9e397b599b13d642138480a28c14db7a136bf0Adam Langley } 127d9e397b599b13d642138480a28c14db7a136bf0Adam Langley if (c) { 128d9e397b599b13d642138480a28c14db7a136bf0Adam Langley *rp = 1; 129d9e397b599b13d642138480a28c14db7a136bf0Adam Langley r->top++; 130d9e397b599b13d642138480a28c14db7a136bf0Adam Langley } 131d9e397b599b13d642138480a28c14db7a136bf0Adam Langley 132d9e397b599b13d642138480a28c14db7a136bf0Adam Langley return 1; 133d9e397b599b13d642138480a28c14db7a136bf0Adam Langley} 134d9e397b599b13d642138480a28c14db7a136bf0Adam Langley 135d9e397b599b13d642138480a28c14db7a136bf0Adam Langleyint BN_rshift(BIGNUM *r, const BIGNUM *a, int n) { 136d9e397b599b13d642138480a28c14db7a136bf0Adam Langley int i, j, nw, lb, rb; 137d9e397b599b13d642138480a28c14db7a136bf0Adam Langley BN_ULONG *t, *f; 138d9e397b599b13d642138480a28c14db7a136bf0Adam Langley BN_ULONG l, tmp; 139d9e397b599b13d642138480a28c14db7a136bf0Adam Langley 140f4e427204234da139fd0585def4b4e22502e33f0Adam Langley if (n < 0) { 141b8494591d1b1a143f3b192d845c238bbf3bc629dKenny Root OPENSSL_PUT_ERROR(BN, BN_R_NEGATIVE_NUMBER); 142f4e427204234da139fd0585def4b4e22502e33f0Adam Langley return 0; 143f4e427204234da139fd0585def4b4e22502e33f0Adam Langley } 144f4e427204234da139fd0585def4b4e22502e33f0Adam Langley 145d9e397b599b13d642138480a28c14db7a136bf0Adam Langley nw = n / BN_BITS2; 146d9e397b599b13d642138480a28c14db7a136bf0Adam Langley rb = n % BN_BITS2; 147d9e397b599b13d642138480a28c14db7a136bf0Adam Langley lb = BN_BITS2 - rb; 148d9e397b599b13d642138480a28c14db7a136bf0Adam Langley if (nw >= a->top || a->top == 0) { 149d9e397b599b13d642138480a28c14db7a136bf0Adam Langley BN_zero(r); 150d9e397b599b13d642138480a28c14db7a136bf0Adam Langley return 1; 151d9e397b599b13d642138480a28c14db7a136bf0Adam Langley } 152d9e397b599b13d642138480a28c14db7a136bf0Adam Langley i = (BN_num_bits(a) - n + (BN_BITS2 - 1)) / BN_BITS2; 153d9e397b599b13d642138480a28c14db7a136bf0Adam Langley if (r != a) { 154d9e397b599b13d642138480a28c14db7a136bf0Adam Langley r->neg = a->neg; 155d9e397b599b13d642138480a28c14db7a136bf0Adam Langley if (bn_wexpand(r, i) == NULL) { 156d9e397b599b13d642138480a28c14db7a136bf0Adam Langley return 0; 157d9e397b599b13d642138480a28c14db7a136bf0Adam Langley } 158d9e397b599b13d642138480a28c14db7a136bf0Adam Langley } else { 159d9e397b599b13d642138480a28c14db7a136bf0Adam Langley if (n == 0) { 160d9e397b599b13d642138480a28c14db7a136bf0Adam Langley return 1; /* or the copying loop will go berserk */ 161d9e397b599b13d642138480a28c14db7a136bf0Adam Langley } 162d9e397b599b13d642138480a28c14db7a136bf0Adam Langley } 163d9e397b599b13d642138480a28c14db7a136bf0Adam Langley 164d9e397b599b13d642138480a28c14db7a136bf0Adam Langley f = &(a->d[nw]); 165d9e397b599b13d642138480a28c14db7a136bf0Adam Langley t = r->d; 166d9e397b599b13d642138480a28c14db7a136bf0Adam Langley j = a->top - nw; 167d9e397b599b13d642138480a28c14db7a136bf0Adam Langley r->top = i; 168d9e397b599b13d642138480a28c14db7a136bf0Adam Langley 169d9e397b599b13d642138480a28c14db7a136bf0Adam Langley if (rb == 0) { 170d9e397b599b13d642138480a28c14db7a136bf0Adam Langley for (i = j; i != 0; i--) { 171d9e397b599b13d642138480a28c14db7a136bf0Adam Langley *(t++) = *(f++); 172d9e397b599b13d642138480a28c14db7a136bf0Adam Langley } 173d9e397b599b13d642138480a28c14db7a136bf0Adam Langley } else { 174d9e397b599b13d642138480a28c14db7a136bf0Adam Langley l = *(f++); 175d9e397b599b13d642138480a28c14db7a136bf0Adam Langley for (i = j - 1; i != 0; i--) { 176d9e397b599b13d642138480a28c14db7a136bf0Adam Langley tmp = (l >> rb) & BN_MASK2; 177d9e397b599b13d642138480a28c14db7a136bf0Adam Langley l = *(f++); 178d9e397b599b13d642138480a28c14db7a136bf0Adam Langley *(t++) = (tmp | (l << lb)) & BN_MASK2; 179d9e397b599b13d642138480a28c14db7a136bf0Adam Langley } 180d9e397b599b13d642138480a28c14db7a136bf0Adam Langley if ((l = (l >> rb) & BN_MASK2)) { 181d9e397b599b13d642138480a28c14db7a136bf0Adam Langley *(t) = l; 182d9e397b599b13d642138480a28c14db7a136bf0Adam Langley } 183d9e397b599b13d642138480a28c14db7a136bf0Adam Langley } 184d9e397b599b13d642138480a28c14db7a136bf0Adam Langley 185d9e397b599b13d642138480a28c14db7a136bf0Adam Langley return 1; 186d9e397b599b13d642138480a28c14db7a136bf0Adam Langley} 187d9e397b599b13d642138480a28c14db7a136bf0Adam Langley 188d9e397b599b13d642138480a28c14db7a136bf0Adam Langleyint BN_rshift1(BIGNUM *r, const BIGNUM *a) { 189d9e397b599b13d642138480a28c14db7a136bf0Adam Langley BN_ULONG *ap, *rp, t, c; 190d9e397b599b13d642138480a28c14db7a136bf0Adam Langley int i, j; 191d9e397b599b13d642138480a28c14db7a136bf0Adam Langley 192d9e397b599b13d642138480a28c14db7a136bf0Adam Langley if (BN_is_zero(a)) { 193d9e397b599b13d642138480a28c14db7a136bf0Adam Langley BN_zero(r); 194d9e397b599b13d642138480a28c14db7a136bf0Adam Langley return 1; 195d9e397b599b13d642138480a28c14db7a136bf0Adam Langley } 196d9e397b599b13d642138480a28c14db7a136bf0Adam Langley i = a->top; 197d9e397b599b13d642138480a28c14db7a136bf0Adam Langley ap = a->d; 198d9e397b599b13d642138480a28c14db7a136bf0Adam Langley j = i - (ap[i - 1] == 1); 199d9e397b599b13d642138480a28c14db7a136bf0Adam Langley if (a != r) { 200d9e397b599b13d642138480a28c14db7a136bf0Adam Langley if (bn_wexpand(r, j) == NULL) { 201d9e397b599b13d642138480a28c14db7a136bf0Adam Langley return 0; 202d9e397b599b13d642138480a28c14db7a136bf0Adam Langley } 203d9e397b599b13d642138480a28c14db7a136bf0Adam Langley r->neg = a->neg; 204d9e397b599b13d642138480a28c14db7a136bf0Adam Langley } 205d9e397b599b13d642138480a28c14db7a136bf0Adam Langley rp = r->d; 206d9e397b599b13d642138480a28c14db7a136bf0Adam Langley t = ap[--i]; 207d9e397b599b13d642138480a28c14db7a136bf0Adam Langley c = (t & 1) ? BN_TBIT : 0; 208d9e397b599b13d642138480a28c14db7a136bf0Adam Langley if (t >>= 1) { 209d9e397b599b13d642138480a28c14db7a136bf0Adam Langley rp[i] = t; 210d9e397b599b13d642138480a28c14db7a136bf0Adam Langley } 211d9e397b599b13d642138480a28c14db7a136bf0Adam Langley while (i > 0) { 212d9e397b599b13d642138480a28c14db7a136bf0Adam Langley t = ap[--i]; 213d9e397b599b13d642138480a28c14db7a136bf0Adam Langley rp[i] = ((t >> 1) & BN_MASK2) | c; 214d9e397b599b13d642138480a28c14db7a136bf0Adam Langley c = (t & 1) ? BN_TBIT : 0; 215d9e397b599b13d642138480a28c14db7a136bf0Adam Langley } 216d9e397b599b13d642138480a28c14db7a136bf0Adam Langley r->top = j; 217d9e397b599b13d642138480a28c14db7a136bf0Adam Langley 218d9e397b599b13d642138480a28c14db7a136bf0Adam Langley return 1; 219d9e397b599b13d642138480a28c14db7a136bf0Adam Langley} 220d9e397b599b13d642138480a28c14db7a136bf0Adam Langley 221d9e397b599b13d642138480a28c14db7a136bf0Adam Langleyint BN_set_bit(BIGNUM *a, int n) { 222d9e397b599b13d642138480a28c14db7a136bf0Adam Langley int i, j, k; 223d9e397b599b13d642138480a28c14db7a136bf0Adam Langley 224d9e397b599b13d642138480a28c14db7a136bf0Adam Langley if (n < 0) { 225d9e397b599b13d642138480a28c14db7a136bf0Adam Langley return 0; 226d9e397b599b13d642138480a28c14db7a136bf0Adam Langley } 227d9e397b599b13d642138480a28c14db7a136bf0Adam Langley 228d9e397b599b13d642138480a28c14db7a136bf0Adam Langley i = n / BN_BITS2; 229d9e397b599b13d642138480a28c14db7a136bf0Adam Langley j = n % BN_BITS2; 230d9e397b599b13d642138480a28c14db7a136bf0Adam Langley if (a->top <= i) { 231d9e397b599b13d642138480a28c14db7a136bf0Adam Langley if (bn_wexpand(a, i + 1) == NULL) { 232d9e397b599b13d642138480a28c14db7a136bf0Adam Langley return 0; 233d9e397b599b13d642138480a28c14db7a136bf0Adam Langley } 234d9e397b599b13d642138480a28c14db7a136bf0Adam Langley for (k = a->top; k < i + 1; k++) { 235d9e397b599b13d642138480a28c14db7a136bf0Adam Langley a->d[k] = 0; 236d9e397b599b13d642138480a28c14db7a136bf0Adam Langley } 237d9e397b599b13d642138480a28c14db7a136bf0Adam Langley a->top = i + 1; 238d9e397b599b13d642138480a28c14db7a136bf0Adam Langley } 239d9e397b599b13d642138480a28c14db7a136bf0Adam Langley 240d9e397b599b13d642138480a28c14db7a136bf0Adam Langley a->d[i] |= (((BN_ULONG)1) << j); 241d9e397b599b13d642138480a28c14db7a136bf0Adam Langley 242d9e397b599b13d642138480a28c14db7a136bf0Adam Langley return 1; 243d9e397b599b13d642138480a28c14db7a136bf0Adam Langley} 244d9e397b599b13d642138480a28c14db7a136bf0Adam Langley 245d9e397b599b13d642138480a28c14db7a136bf0Adam Langleyint BN_clear_bit(BIGNUM *a, int n) { 246d9e397b599b13d642138480a28c14db7a136bf0Adam Langley int i, j; 247d9e397b599b13d642138480a28c14db7a136bf0Adam Langley 248d9e397b599b13d642138480a28c14db7a136bf0Adam Langley if (n < 0) { 249d9e397b599b13d642138480a28c14db7a136bf0Adam Langley return 0; 250d9e397b599b13d642138480a28c14db7a136bf0Adam Langley } 251d9e397b599b13d642138480a28c14db7a136bf0Adam Langley 252d9e397b599b13d642138480a28c14db7a136bf0Adam Langley i = n / BN_BITS2; 253d9e397b599b13d642138480a28c14db7a136bf0Adam Langley j = n % BN_BITS2; 254d9e397b599b13d642138480a28c14db7a136bf0Adam Langley if (a->top <= i) { 255d9e397b599b13d642138480a28c14db7a136bf0Adam Langley return 0; 256d9e397b599b13d642138480a28c14db7a136bf0Adam Langley } 257d9e397b599b13d642138480a28c14db7a136bf0Adam Langley 258d9e397b599b13d642138480a28c14db7a136bf0Adam Langley a->d[i] &= (~(((BN_ULONG)1) << j)); 259d9e397b599b13d642138480a28c14db7a136bf0Adam Langley bn_correct_top(a); 260d9e397b599b13d642138480a28c14db7a136bf0Adam Langley return 1; 261d9e397b599b13d642138480a28c14db7a136bf0Adam Langley} 262d9e397b599b13d642138480a28c14db7a136bf0Adam Langley 263d9e397b599b13d642138480a28c14db7a136bf0Adam Langleyint BN_is_bit_set(const BIGNUM *a, int n) { 264d9e397b599b13d642138480a28c14db7a136bf0Adam Langley int i, j; 265d9e397b599b13d642138480a28c14db7a136bf0Adam Langley 266d9e397b599b13d642138480a28c14db7a136bf0Adam Langley if (n < 0) { 267d9e397b599b13d642138480a28c14db7a136bf0Adam Langley return 0; 268d9e397b599b13d642138480a28c14db7a136bf0Adam Langley } 269d9e397b599b13d642138480a28c14db7a136bf0Adam Langley i = n / BN_BITS2; 270d9e397b599b13d642138480a28c14db7a136bf0Adam Langley j = n % BN_BITS2; 271d9e397b599b13d642138480a28c14db7a136bf0Adam Langley if (a->top <= i) { 272d9e397b599b13d642138480a28c14db7a136bf0Adam Langley return 0; 273d9e397b599b13d642138480a28c14db7a136bf0Adam Langley } 274d9e397b599b13d642138480a28c14db7a136bf0Adam Langley 275d9e397b599b13d642138480a28c14db7a136bf0Adam Langley return (a->d[i]>>j)&1; 276d9e397b599b13d642138480a28c14db7a136bf0Adam Langley} 277d9e397b599b13d642138480a28c14db7a136bf0Adam Langley 278d9e397b599b13d642138480a28c14db7a136bf0Adam Langleyint BN_mask_bits(BIGNUM *a, int n) { 279d9e397b599b13d642138480a28c14db7a136bf0Adam Langley int b, w; 280d9e397b599b13d642138480a28c14db7a136bf0Adam Langley 281d9e397b599b13d642138480a28c14db7a136bf0Adam Langley if (n < 0) { 282d9e397b599b13d642138480a28c14db7a136bf0Adam Langley return 0; 283d9e397b599b13d642138480a28c14db7a136bf0Adam Langley } 284d9e397b599b13d642138480a28c14db7a136bf0Adam Langley 285d9e397b599b13d642138480a28c14db7a136bf0Adam Langley w = n / BN_BITS2; 286d9e397b599b13d642138480a28c14db7a136bf0Adam Langley b = n % BN_BITS2; 287d9e397b599b13d642138480a28c14db7a136bf0Adam Langley if (w >= a->top) { 288d9e397b599b13d642138480a28c14db7a136bf0Adam Langley return 0; 289d9e397b599b13d642138480a28c14db7a136bf0Adam Langley } 290d9e397b599b13d642138480a28c14db7a136bf0Adam Langley if (b == 0) { 291d9e397b599b13d642138480a28c14db7a136bf0Adam Langley a->top = w; 292d9e397b599b13d642138480a28c14db7a136bf0Adam Langley } else { 293d9e397b599b13d642138480a28c14db7a136bf0Adam Langley a->top = w + 1; 294d9e397b599b13d642138480a28c14db7a136bf0Adam Langley a->d[w] &= ~(BN_MASK2 << b); 295d9e397b599b13d642138480a28c14db7a136bf0Adam Langley } 296d9e397b599b13d642138480a28c14db7a136bf0Adam Langley 297d9e397b599b13d642138480a28c14db7a136bf0Adam Langley bn_correct_top(a); 298d9e397b599b13d642138480a28c14db7a136bf0Adam Langley return 1; 299d9e397b599b13d642138480a28c14db7a136bf0Adam Langley} 300