summaryrefslogtreecommitdiff
path: root/src/lib/libcrypto/bn/bn_word.c
diff options
context:
space:
mode:
authordjm <>2008-09-06 12:17:54 +0000
committerdjm <>2008-09-06 12:17:54 +0000
commit6b62d1fdd8a4fd35acfcc0c4bb1bf8b757fa8cda (patch)
tree7ccc28afe1789ea3dbedf72365f955d5b8e105b5 /src/lib/libcrypto/bn/bn_word.c
parent89181603212b41e95cde36b1be5a146ce8fb2935 (diff)
downloadopenbsd-6b62d1fdd8a4fd35acfcc0c4bb1bf8b757fa8cda.tar.gz
openbsd-6b62d1fdd8a4fd35acfcc0c4bb1bf8b757fa8cda.tar.bz2
openbsd-6b62d1fdd8a4fd35acfcc0c4bb1bf8b757fa8cda.zip
resolve conflicts
Diffstat (limited to 'src/lib/libcrypto/bn/bn_word.c')
-rw-r--r--src/lib/libcrypto/bn/bn_word.c67
1 files changed, 53 insertions, 14 deletions
diff --git a/src/lib/libcrypto/bn/bn_word.c b/src/lib/libcrypto/bn/bn_word.c
index de610ce54c..ee7b87c45c 100644
--- a/src/lib/libcrypto/bn/bn_word.c
+++ b/src/lib/libcrypto/bn/bn_word.c
@@ -69,6 +69,10 @@ BN_ULONG BN_mod_word(const BIGNUM *a, BN_ULONG w)
69#endif 69#endif
70 int i; 70 int i;
71 71
72 if (w == 0)
73 return (BN_ULONG)-1;
74
75 bn_check_top(a);
72 w&=BN_MASK2; 76 w&=BN_MASK2;
73 for (i=a->top-1; i>=0; i--) 77 for (i=a->top-1; i>=0; i--)
74 { 78 {
@@ -85,12 +89,24 @@ BN_ULONG BN_mod_word(const BIGNUM *a, BN_ULONG w)
85 89
86BN_ULONG BN_div_word(BIGNUM *a, BN_ULONG w) 90BN_ULONG BN_div_word(BIGNUM *a, BN_ULONG w)
87 { 91 {
88 BN_ULONG ret; 92 BN_ULONG ret = 0;
89 int i; 93 int i, j;
94
95 bn_check_top(a);
96 w &= BN_MASK2;
97
98 if (!w)
99 /* actually this an error (division by zero) */
100 return (BN_ULONG)-1;
101 if (a->top == 0)
102 return 0;
103
104 /* normalize input (so bn_div_words doesn't complain) */
105 j = BN_BITS2 - BN_num_bits_word(w);
106 w <<= j;
107 if (!BN_lshift(a, a, j))
108 return (BN_ULONG)-1;
90 109
91 if (a->top == 0) return(0);
92 ret=0;
93 w&=BN_MASK2;
94 for (i=a->top-1; i>=0; i--) 110 for (i=a->top-1; i>=0; i--)
95 { 111 {
96 BN_ULONG l,d; 112 BN_ULONG l,d;
@@ -102,6 +118,8 @@ BN_ULONG BN_div_word(BIGNUM *a, BN_ULONG w)
102 } 118 }
103 if ((a->top > 0) && (a->d[a->top-1] == 0)) 119 if ((a->top > 0) && (a->d[a->top-1] == 0))
104 a->top--; 120 a->top--;
121 ret >>= j;
122 bn_check_top(a);
105 return(ret); 123 return(ret);
106 } 124 }
107 125
@@ -110,9 +128,14 @@ int BN_add_word(BIGNUM *a, BN_ULONG w)
110 BN_ULONG l; 128 BN_ULONG l;
111 int i; 129 int i;
112 130
113 if ((w & BN_MASK2) == 0) 131 bn_check_top(a);
114 return(1); 132 w &= BN_MASK2;
115 133
134 /* degenerate case: w is zero */
135 if (!w) return 1;
136 /* degenerate case: a is zero */
137 if(BN_is_zero(a)) return BN_set_word(a, w);
138 /* handle 'a' when negative */
116 if (a->neg) 139 if (a->neg)
117 { 140 {
118 a->neg=0; 141 a->neg=0;
@@ -121,15 +144,17 @@ int BN_add_word(BIGNUM *a, BN_ULONG w)
121 a->neg=!(a->neg); 144 a->neg=!(a->neg);
122 return(i); 145 return(i);
123 } 146 }
124 w&=BN_MASK2; 147 /* Only expand (and risk failing) if it's possibly necessary */
125 if (bn_wexpand(a,a->top+1) == NULL) return(0); 148 if (((BN_ULONG)(a->d[a->top - 1] + 1) == 0) &&
149 (bn_wexpand(a,a->top+1) == NULL))
150 return(0);
126 i=0; 151 i=0;
127 for (;;) 152 for (;;)
128 { 153 {
129 if (i >= a->top) 154 if (i >= a->top)
130 l=w; 155 l=w;
131 else 156 else
132 l=(a->d[i]+(BN_ULONG)w)&BN_MASK2; 157 l=(a->d[i]+w)&BN_MASK2;
133 a->d[i]=l; 158 a->d[i]=l;
134 if (w > l) 159 if (w > l)
135 w=1; 160 w=1;
@@ -139,6 +164,7 @@ int BN_add_word(BIGNUM *a, BN_ULONG w)
139 } 164 }
140 if (i >= a->top) 165 if (i >= a->top)
141 a->top++; 166 a->top++;
167 bn_check_top(a);
142 return(1); 168 return(1);
143 } 169 }
144 170
@@ -146,10 +172,21 @@ int BN_sub_word(BIGNUM *a, BN_ULONG w)
146 { 172 {
147 int i; 173 int i;
148 174
149 if ((w & BN_MASK2) == 0) 175 bn_check_top(a);
150 return(1); 176 w &= BN_MASK2;
151 177
152 if (BN_is_zero(a) || a->neg) 178 /* degenerate case: w is zero */
179 if (!w) return 1;
180 /* degenerate case: a is zero */
181 if(BN_is_zero(a))
182 {
183 i = BN_set_word(a,w);
184 if (i != 0)
185 BN_set_negative(a, 1);
186 return i;
187 }
188 /* handle 'a' when negative */
189 if (a->neg)
153 { 190 {
154 a->neg=0; 191 a->neg=0;
155 i=BN_add_word(a,w); 192 i=BN_add_word(a,w);
@@ -157,7 +194,6 @@ int BN_sub_word(BIGNUM *a, BN_ULONG w)
157 return(i); 194 return(i);
158 } 195 }
159 196
160 w&=BN_MASK2;
161 if ((a->top == 1) && (a->d[0] < w)) 197 if ((a->top == 1) && (a->d[0] < w))
162 { 198 {
163 a->d[0]=w-a->d[0]; 199 a->d[0]=w-a->d[0];
@@ -181,6 +217,7 @@ int BN_sub_word(BIGNUM *a, BN_ULONG w)
181 } 217 }
182 if ((a->d[i] == 0) && (i == (a->top-1))) 218 if ((a->d[i] == 0) && (i == (a->top-1)))
183 a->top--; 219 a->top--;
220 bn_check_top(a);
184 return(1); 221 return(1);
185 } 222 }
186 223
@@ -188,6 +225,7 @@ int BN_mul_word(BIGNUM *a, BN_ULONG w)
188 { 225 {
189 BN_ULONG ll; 226 BN_ULONG ll;
190 227
228 bn_check_top(a);
191 w&=BN_MASK2; 229 w&=BN_MASK2;
192 if (a->top) 230 if (a->top)
193 { 231 {
@@ -203,6 +241,7 @@ int BN_mul_word(BIGNUM *a, BN_ULONG w)
203 } 241 }
204 } 242 }
205 } 243 }
244 bn_check_top(a);
206 return(1); 245 return(1);
207 } 246 }
208 247