155714Skris/* crypto/bn/bn_word.c */
255714Skris/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
355714Skris * All rights reserved.
455714Skris *
555714Skris * This package is an SSL implementation written
655714Skris * by Eric Young (eay@cryptsoft.com).
755714Skris * The implementation was written so as to conform with Netscapes SSL.
8280304Sjkim *
955714Skris * This library is free for commercial and non-commercial use as long as
1055714Skris * the following conditions are aheared to.  The following conditions
1155714Skris * apply to all code found in this distribution, be it the RC4, RSA,
1255714Skris * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
1355714Skris * included with this distribution is covered by the same copyright terms
1455714Skris * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15280304Sjkim *
1655714Skris * Copyright remains Eric Young's, and as such any Copyright notices in
1755714Skris * the code are not to be removed.
1855714Skris * If this package is used in a product, Eric Young should be given attribution
1955714Skris * as the author of the parts of the library used.
2055714Skris * This can be in the form of a textual message at program startup or
2155714Skris * in documentation (online or textual) provided with the package.
22280304Sjkim *
2355714Skris * Redistribution and use in source and binary forms, with or without
2455714Skris * modification, are permitted provided that the following conditions
2555714Skris * are met:
2655714Skris * 1. Redistributions of source code must retain the copyright
2755714Skris *    notice, this list of conditions and the following disclaimer.
2855714Skris * 2. Redistributions in binary form must reproduce the above copyright
2955714Skris *    notice, this list of conditions and the following disclaimer in the
3055714Skris *    documentation and/or other materials provided with the distribution.
3155714Skris * 3. All advertising materials mentioning features or use of this software
3255714Skris *    must display the following acknowledgement:
3355714Skris *    "This product includes cryptographic software written by
3455714Skris *     Eric Young (eay@cryptsoft.com)"
3555714Skris *    The word 'cryptographic' can be left out if the rouines from the library
3655714Skris *    being used are not cryptographic related :-).
37280304Sjkim * 4. If you include any Windows specific code (or a derivative thereof) from
3855714Skris *    the apps directory (application code) you must include an acknowledgement:
3955714Skris *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40280304Sjkim *
4155714Skris * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
4255714Skris * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
4355714Skris * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
4455714Skris * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
4555714Skris * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
4655714Skris * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
4755714Skris * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
4855714Skris * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
4955714Skris * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
5055714Skris * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
5155714Skris * SUCH DAMAGE.
52280304Sjkim *
5355714Skris * The licence and distribution terms for any publically available version or
5455714Skris * derivative of this code cannot be changed.  i.e. this code cannot simply be
5555714Skris * copied and put under another distribution licence
5655714Skris * [including the GNU Public Licence.]
5755714Skris */
5855714Skris
5955714Skris#include <stdio.h>
6055714Skris#include "cryptlib.h"
6155714Skris#include "bn_lcl.h"
6255714Skris
6359191SkrisBN_ULONG BN_mod_word(const BIGNUM *a, BN_ULONG w)
64280304Sjkim{
6555714Skris#ifndef BN_LLONG
66280304Sjkim    BN_ULONG ret = 0;
6755714Skris#else
68280304Sjkim    BN_ULLONG ret = 0;
6955714Skris#endif
70280304Sjkim    int i;
7155714Skris
72280304Sjkim    if (w == 0)
73280304Sjkim        return (BN_ULONG)-1;
74160814Ssimon
75280304Sjkim    bn_check_top(a);
76280304Sjkim    w &= BN_MASK2;
77280304Sjkim    for (i = a->top - 1; i >= 0; i--) {
7855714Skris#ifndef BN_LLONG
79280304Sjkim        ret = ((ret << BN_BITS4) | ((a->d[i] >> BN_BITS4) & BN_MASK2l)) % w;
80280304Sjkim        ret = ((ret << BN_BITS4) | (a->d[i] & BN_MASK2l)) % w;
8155714Skris#else
82280304Sjkim        ret = (BN_ULLONG) (((ret << (BN_ULLONG) BN_BITS2) | a->d[i]) %
83280304Sjkim                           (BN_ULLONG) w);
8455714Skris#endif
85280304Sjkim    }
86280304Sjkim    return ((BN_ULONG)ret);
87280304Sjkim}
8855714Skris
8955714SkrisBN_ULONG BN_div_word(BIGNUM *a, BN_ULONG w)
90280304Sjkim{
91280304Sjkim    BN_ULONG ret = 0;
92280304Sjkim    int i, j;
9355714Skris
94280304Sjkim    bn_check_top(a);
95280304Sjkim    w &= BN_MASK2;
96160814Ssimon
97280304Sjkim    if (!w)
98280304Sjkim        /* actually this an error (division by zero) */
99280304Sjkim        return (BN_ULONG)-1;
100280304Sjkim    if (a->top == 0)
101280304Sjkim        return 0;
102160814Ssimon
103280304Sjkim    /* normalize input (so bn_div_words doesn't complain) */
104280304Sjkim    j = BN_BITS2 - BN_num_bits_word(w);
105280304Sjkim    w <<= j;
106280304Sjkim    if (!BN_lshift(a, a, j))
107280304Sjkim        return (BN_ULONG)-1;
108160814Ssimon
109280304Sjkim    for (i = a->top - 1; i >= 0; i--) {
110280304Sjkim        BN_ULONG l, d;
11155714Skris
112280304Sjkim        l = a->d[i];
113280304Sjkim        d = bn_div_words(ret, l, w);
114280304Sjkim        ret = (l - ((d * w) & BN_MASK2)) & BN_MASK2;
115280304Sjkim        a->d[i] = d;
116280304Sjkim    }
117280304Sjkim    if ((a->top > 0) && (a->d[a->top - 1] == 0))
118280304Sjkim        a->top--;
119280304Sjkim    ret >>= j;
120280304Sjkim    bn_check_top(a);
121280304Sjkim    return (ret);
122280304Sjkim}
123280304Sjkim
12455714Skrisint BN_add_word(BIGNUM *a, BN_ULONG w)
125280304Sjkim{
126280304Sjkim    BN_ULONG l;
127280304Sjkim    int i;
12855714Skris
129280304Sjkim    bn_check_top(a);
130280304Sjkim    w &= BN_MASK2;
131160814Ssimon
132280304Sjkim    /* degenerate case: w is zero */
133280304Sjkim    if (!w)
134280304Sjkim        return 1;
135280304Sjkim    /* degenerate case: a is zero */
136280304Sjkim    if (BN_is_zero(a))
137280304Sjkim        return BN_set_word(a, w);
138280304Sjkim    /* handle 'a' when negative */
139280304Sjkim    if (a->neg) {
140280304Sjkim        a->neg = 0;
141280304Sjkim        i = BN_sub_word(a, w);
142280304Sjkim        if (!BN_is_zero(a))
143280304Sjkim            a->neg = !(a->neg);
144280304Sjkim        return (i);
145280304Sjkim    }
146280304Sjkim    for (i = 0; w != 0 && i < a->top; i++) {
147280304Sjkim        a->d[i] = l = (a->d[i] + w) & BN_MASK2;
148280304Sjkim        w = (w > l) ? 1 : 0;
149280304Sjkim    }
150280304Sjkim    if (w && i == a->top) {
151280304Sjkim        if (bn_wexpand(a, a->top + 1) == NULL)
152280304Sjkim            return 0;
153280304Sjkim        a->top++;
154280304Sjkim        a->d[i] = w;
155280304Sjkim    }
156280304Sjkim    bn_check_top(a);
157280304Sjkim    return (1);
158280304Sjkim}
15955714Skris
16055714Skrisint BN_sub_word(BIGNUM *a, BN_ULONG w)
161280304Sjkim{
162280304Sjkim    int i;
16355714Skris
164280304Sjkim    bn_check_top(a);
165280304Sjkim    w &= BN_MASK2;
166160814Ssimon
167280304Sjkim    /* degenerate case: w is zero */
168280304Sjkim    if (!w)
169280304Sjkim        return 1;
170280304Sjkim    /* degenerate case: a is zero */
171280304Sjkim    if (BN_is_zero(a)) {
172280304Sjkim        i = BN_set_word(a, w);
173280304Sjkim        if (i != 0)
174280304Sjkim            BN_set_negative(a, 1);
175280304Sjkim        return i;
176280304Sjkim    }
177280304Sjkim    /* handle 'a' when negative */
178280304Sjkim    if (a->neg) {
179280304Sjkim        a->neg = 0;
180280304Sjkim        i = BN_add_word(a, w);
181280304Sjkim        a->neg = 1;
182280304Sjkim        return (i);
183280304Sjkim    }
18455714Skris
185280304Sjkim    if ((a->top == 1) && (a->d[0] < w)) {
186280304Sjkim        a->d[0] = w - a->d[0];
187280304Sjkim        a->neg = 1;
188280304Sjkim        return (1);
189280304Sjkim    }
190280304Sjkim    i = 0;
191280304Sjkim    for (;;) {
192280304Sjkim        if (a->d[i] >= w) {
193280304Sjkim            a->d[i] -= w;
194280304Sjkim            break;
195280304Sjkim        } else {
196280304Sjkim            a->d[i] = (a->d[i] - w) & BN_MASK2;
197280304Sjkim            i++;
198280304Sjkim            w = 1;
199280304Sjkim        }
200280304Sjkim    }
201280304Sjkim    if ((a->d[i] == 0) && (i == (a->top - 1)))
202280304Sjkim        a->top--;
203280304Sjkim    bn_check_top(a);
204280304Sjkim    return (1);
205280304Sjkim}
20655714Skris
20755714Skrisint BN_mul_word(BIGNUM *a, BN_ULONG w)
208280304Sjkim{
209280304Sjkim    BN_ULONG ll;
21055714Skris
211280304Sjkim    bn_check_top(a);
212280304Sjkim    w &= BN_MASK2;
213280304Sjkim    if (a->top) {
214280304Sjkim        if (w == 0)
215280304Sjkim            BN_zero(a);
216280304Sjkim        else {
217280304Sjkim            ll = bn_mul_words(a->d, a->d, a->top, w);
218280304Sjkim            if (ll) {
219280304Sjkim                if (bn_wexpand(a, a->top + 1) == NULL)
220280304Sjkim                    return (0);
221280304Sjkim                a->d[a->top++] = ll;
222280304Sjkim            }
223280304Sjkim        }
224280304Sjkim    }
225280304Sjkim    bn_check_top(a);
226280304Sjkim    return (1);
227280304Sjkim}
228