source: trunk/cscrypt/bn_word.c@ 3181

Last change on this file since 3181 was 3181, checked in by dingo35, 10 years ago

Adding threadsafety FIXMEs, feel free to join checking..

File size: 5.1 KB
Line 
1//FIXME Not checked on threadsafety yet; after checking please remove this line
2/* crypto/bn/bn_word.c */
3/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
4 * All rights reserved.
5 *
6 * This package is an SSL implementation written
7 * by Eric Young (eay@cryptsoft.com).
8 * The implementation was written so as to conform with Netscapes SSL.
9 *
10 * This library is free for commercial and non-commercial use as long as
11 * the following conditions are aheared to. The following conditions
12 * apply to all code found in this distribution, be it the RC4, RSA,
13 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
14 * included with this distribution is covered by the same copyright terms
15 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
16 *
17 * Copyright remains Eric Young's, and as such any Copyright notices in
18 * the code are not to be removed.
19 * If this package is used in a product, Eric Young should be given attribution
20 * as the author of the parts of the library used.
21 * This can be in the form of a textual message at program startup or
22 * in documentation (online or textual) provided with the package.
23 *
24 * Redistribution and use in source and binary forms, with or without
25 * modification, are permitted provided that the following conditions
26 * are met:
27 * 1. Redistributions of source code must retain the copyright
28 * notice, this list of conditions and the following disclaimer.
29 * 2. Redistributions in binary form must reproduce the above copyright
30 * notice, this list of conditions and the following disclaimer in the
31 * documentation and/or other materials provided with the distribution.
32 * 3. All advertising materials mentioning features or use of this software
33 * must display the following acknowledgement:
34 * "This product includes cryptographic software written by
35 * Eric Young (eay@cryptsoft.com)"
36 * The word 'cryptographic' can be left out if the rouines from the library
37 * being used are not cryptographic related :-).
38 * 4. If you include any Windows specific code (or a derivative thereof) from
39 * the apps directory (application code) you must include an acknowledgement:
40 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
41 *
42 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
43 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
44 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
45 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
46 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
47 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
48 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
49 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
50 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
51 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
52 * SUCH DAMAGE.
53 *
54 * The licence and distribution terms for any publically available version or
55 * derivative of this code cannot be changed. i.e. this code cannot simply be
56 * copied and put under another distribution licence
57 * [including the GNU Public Licence.]
58 */
59
60#include <stdio.h>
61#include "bn_lcl.h"
62#include "openssl_mods.h"
63
64BN_ULONG BN_mod_word(const BIGNUM *a, BN_ULONG w)
65 {
66#ifndef BN_LLONG
67 BN_ULONG ret=0;
68#else
69 BN_ULLONG ret=0;
70#endif
71 int i;
72
73 w&=BN_MASK2;
74 for (i=a->top-1; i>=0; i--)
75 {
76#ifndef BN_LLONG
77 ret=((ret<<BN_BITS4)|((a->d[i]>>BN_BITS4)&BN_MASK2l))%w;
78 ret=((ret<<BN_BITS4)|(a->d[i]&BN_MASK2l))%w;
79#else
80 ret=(BN_ULLONG)(((ret<<(BN_ULLONG)BN_BITS2)|a->d[i])%
81 (BN_ULLONG)w);
82#endif
83 }
84 return((BN_ULONG)ret);
85 }
86
87BN_ULONG BN_div_word(BIGNUM *a, BN_ULONG w)
88 {
89 BN_ULONG ret;
90 int i;
91
92 if (a->top == 0) return(0);
93 ret=0;
94 w&=BN_MASK2;
95 for (i=a->top-1; i>=0; i--)
96 {
97 BN_ULONG l,d;
98
99 l=a->d[i];
100 d=bn_div_words(ret,l,w);
101 ret=(l-((d*w)&BN_MASK2))&BN_MASK2;
102 a->d[i]=d;
103 }
104 if ((a->top > 0) && (a->d[a->top-1] == 0))
105 a->top--;
106 return(ret);
107 }
108
109int BN_add_word(BIGNUM *a, BN_ULONG w)
110 {
111 BN_ULONG l;
112 int i;
113
114 if (a->neg)
115 {
116 a->neg=0;
117 i=BN_sub_word(a,w);
118 if (!BN_is_zero(a))
119 a->neg=!(a->neg);
120 return(i);
121 }
122 w&=BN_MASK2;
123 if (bn_wexpand(a,a->top+1) == NULL) return(0);
124 i=0;
125 for (;;)
126 {
127 l=(a->d[i]+(BN_ULONG)w)&BN_MASK2;
128 a->d[i]=l;
129 if (w > l)
130 w=1;
131 else
132 break;
133 i++;
134 }
135 if (i >= a->top)
136 a->top++;
137 return(1);
138 }
139
140int BN_sub_word(BIGNUM *a, BN_ULONG w)
141 {
142 int i;
143
144 if (BN_is_zero(a) || a->neg)
145 {
146 a->neg=0;
147 i=BN_add_word(a,w);
148 a->neg=1;
149 return(i);
150 }
151
152 w&=BN_MASK2;
153 if ((a->top == 1) && (a->d[0] < w))
154 {
155 a->d[0]=w-a->d[0];
156 a->neg=1;
157 return(1);
158 }
159 i=0;
160 for (;;)
161 {
162 if (a->d[i] >= w)
163 {
164 a->d[i]-=w;
165 break;
166 }
167 else
168 {
169 a->d[i]=(a->d[i]-w)&BN_MASK2;
170 i++;
171 w=1;
172 }
173 }
174 if ((a->d[i] == 0) && (i == (a->top-1)))
175 a->top--;
176 return(1);
177 }
178
179int BN_mul_word(BIGNUM *a, BN_ULONG w)
180 {
181 BN_ULONG ll;
182
183 w&=BN_MASK2;
184 if (a->top)
185 {
186 if (w == 0)
187 BN_zero(a);
188 else
189 {
190 ll=bn_mul_words(a->d,a->d,a->top,w);
191 if (ll)
192 {
193 if (bn_wexpand(a,a->top+1) == NULL) return(0);
194 a->d[a->top++]=ll;
195 }
196 }
197 }
198 return(1);
199 }
200
Note: See TracBrowser for help on using the repository browser.