97 #ifndef OPENSSL_NO_EC2M
100 #define MAX_ITERATIONS 50
102 static const BN_ULONG SQR_tb[16] =
103 { 0, 1, 4, 5, 16, 17, 20, 21,
104 64, 65, 68, 69, 80, 81, 84, 85 };
106 #if defined(SIXTY_FOUR_BIT) || defined(SIXTY_FOUR_BIT_LONG)
108 SQR_tb[(w) >> 60 & 0xF] << 56 | SQR_tb[(w) >> 56 & 0xF] << 48 | \
109 SQR_tb[(w) >> 52 & 0xF] << 40 | SQR_tb[(w) >> 48 & 0xF] << 32 | \
110 SQR_tb[(w) >> 44 & 0xF] << 24 | SQR_tb[(w) >> 40 & 0xF] << 16 | \
111 SQR_tb[(w) >> 36 & 0xF] << 8 | SQR_tb[(w) >> 32 & 0xF]
113 SQR_tb[(w) >> 28 & 0xF] << 56 | SQR_tb[(w) >> 24 & 0xF] << 48 | \
114 SQR_tb[(w) >> 20 & 0xF] << 40 | SQR_tb[(w) >> 16 & 0xF] << 32 | \
115 SQR_tb[(w) >> 12 & 0xF] << 24 | SQR_tb[(w) >> 8 & 0xF] << 16 | \
116 SQR_tb[(w) >> 4 & 0xF] << 8 | SQR_tb[(w) & 0xF]
118 #ifdef THIRTY_TWO_BIT
120 SQR_tb[(w) >> 28 & 0xF] << 24 | SQR_tb[(w) >> 24 & 0xF] << 16 | \
121 SQR_tb[(w) >> 20 & 0xF] << 8 | SQR_tb[(w) >> 16 & 0xF]
123 SQR_tb[(w) >> 12 & 0xF] << 24 | SQR_tb[(w) >> 8 & 0xF] << 16 | \
124 SQR_tb[(w) >> 4 & 0xF] << 8 | SQR_tb[(w) & 0xF]
127 #if !defined(OPENSSL_BN_ASM_GF2m)
133 #ifdef THIRTY_TWO_BIT
134 static void bn_GF2m_mul_1x1(BN_ULONG *r1, BN_ULONG *r0,
const BN_ULONG
a,
const BN_ULONG
b)
136 register BN_ULONG
h, l, s;
137 BN_ULONG tab[8], top2b = a >> 30;
138 register BN_ULONG a1, a2, a4;
140 a1 = a & (0x3FFFFFFF); a2 = a1 << 1; a4 = a2 << 1;
142 tab[0] = 0; tab[1] = a1; tab[2] = a2; tab[3] = a1^a2;
143 tab[4] = a4; tab[5] = a1^a4; tab[6] = a2^a4; tab[7] = a1^a2^a4;
145 s = tab[b & 0x7]; l = s;
146 s = tab[b >> 3 & 0x7]; l ^= s << 3; h = s >> 29;
147 s = tab[b >> 6 & 0x7]; l ^= s << 6; h ^= s >> 26;
148 s = tab[b >> 9 & 0x7]; l ^= s << 9; h ^= s >> 23;
149 s = tab[b >> 12 & 0x7]; l ^= s << 12; h ^= s >> 20;
150 s = tab[b >> 15 & 0x7]; l ^= s << 15; h ^= s >> 17;
151 s = tab[b >> 18 & 0x7]; l ^= s << 18; h ^= s >> 14;
152 s = tab[b >> 21 & 0x7]; l ^= s << 21; h ^= s >> 11;
153 s = tab[b >> 24 & 0x7]; l ^= s << 24; h ^= s >> 8;
154 s = tab[b >> 27 & 0x7]; l ^= s << 27; h ^= s >> 5;
155 s = tab[b >> 30 ]; l ^= s << 30; h ^= s >> 2;
159 if (top2b & 01) { l ^= b << 30; h ^= b >> 2; }
160 if (top2b & 02) { l ^= b << 31; h ^= b >> 1; }
165 #if defined(SIXTY_FOUR_BIT) || defined(SIXTY_FOUR_BIT_LONG)
166 static void bn_GF2m_mul_1x1(BN_ULONG *r1, BN_ULONG *r0,
const BN_ULONG a,
const BN_ULONG b)
168 register BN_ULONG
h, l, s;
169 BN_ULONG tab[16], top3b = a >> 61;
170 register BN_ULONG a1, a2, a4, a8;
172 a1 = a & (0x1FFFFFFFFFFFFFFFULL); a2 = a1 << 1; a4 = a2 << 1; a8 = a4 << 1;
174 tab[ 0] = 0; tab[ 1] = a1; tab[ 2] = a2; tab[ 3] = a1^a2;
175 tab[ 4] = a4; tab[ 5] = a1^a4; tab[ 6] = a2^a4; tab[ 7] = a1^a2^a4;
176 tab[ 8] = a8; tab[ 9] = a1^a8; tab[10] = a2^a8; tab[11] = a1^a2^a8;
177 tab[12] = a4^a8; tab[13] = a1^a4^a8; tab[14] = a2^a4^a8; tab[15] = a1^a2^a4^a8;
179 s = tab[b & 0xF]; l = s;
180 s = tab[b >> 4 & 0xF]; l ^= s << 4; h = s >> 60;
181 s = tab[b >> 8 & 0xF]; l ^= s << 8; h ^= s >> 56;
182 s = tab[b >> 12 & 0xF]; l ^= s << 12; h ^= s >> 52;
183 s = tab[b >> 16 & 0xF]; l ^= s << 16; h ^= s >> 48;
184 s = tab[b >> 20 & 0xF]; l ^= s << 20; h ^= s >> 44;
185 s = tab[b >> 24 & 0xF]; l ^= s << 24; h ^= s >> 40;
186 s = tab[b >> 28 & 0xF]; l ^= s << 28; h ^= s >> 36;
187 s = tab[b >> 32 & 0xF]; l ^= s << 32; h ^= s >> 32;
188 s = tab[b >> 36 & 0xF]; l ^= s << 36; h ^= s >> 28;
189 s = tab[b >> 40 & 0xF]; l ^= s << 40; h ^= s >> 24;
190 s = tab[b >> 44 & 0xF]; l ^= s << 44; h ^= s >> 20;
191 s = tab[b >> 48 & 0xF]; l ^= s << 48; h ^= s >> 16;
192 s = tab[b >> 52 & 0xF]; l ^= s << 52; h ^= s >> 12;
193 s = tab[b >> 56 & 0xF]; l ^= s << 56; h ^= s >> 8;
194 s = tab[b >> 60 ]; l ^= s << 60; h ^= s >> 4;
198 if (top3b & 01) { l ^= b << 61; h ^= b >> 3; }
199 if (top3b & 02) { l ^= b << 62; h ^= b >> 2; }
200 if (top3b & 04) { l ^= b << 63; h ^= b >> 1; }
211 static void bn_GF2m_mul_2x2(BN_ULONG *r,
const BN_ULONG a1,
const BN_ULONG a0,
const BN_ULONG b1,
const BN_ULONG b0)
215 bn_GF2m_mul_1x1(r+3, r+2, a1, b1);
216 bn_GF2m_mul_1x1(r+1, r, a0, b0);
217 bn_GF2m_mul_1x1(&m1, &m0, a0 ^ a1, b0 ^ b1);
219 r[2] ^= m1 ^ r[1] ^ r[3];
220 r[1] = r[3] ^ r[2] ^ r[0] ^ m1 ^ m0;
223 void bn_GF2m_mul_2x2(BN_ULONG *r, BN_ULONG a1, BN_ULONG a0, BN_ULONG b1, BN_ULONG b0);
237 if (a->
top < b->
top) { at =
b; bt =
a; }
238 else { at =
a; bt =
b; }
243 for (i = 0; i < bt->
top; i++)
245 r->
d[i] = at->
d[i] ^ bt->
d[i];
247 for (; i < at->
top; i++)
288 for (j = 0; j < a->
top; j++)
297 dN = p[0] / BN_BITS2;
298 for (j = r->
top - 1; j > dN;)
301 if (z[j] == 0) { j--;
continue; }
304 for (k = 1; p[k] != 0; k++)
308 d0 = n % BN_BITS2; d1 = BN_BITS2 - d0;
311 if (d0) z[j-n-1] ^= (zz<<d1);
316 d0 = p[0] % BN_BITS2;
318 z[j-n] ^= (zz >> d0);
319 if (d0) z[j-n-1] ^= (zz << d1);
326 d0 = p[0] % BN_BITS2;
333 z[dN] = (z[dN] << d1) >> d1;
338 for (k = 1; p[k] != 0; k++)
344 d0 = p[k] % BN_BITS2;
347 tmp_ulong = zz >> d1;
372 if (!ret || ret > (
int)(
sizeof(arr)/
sizeof(arr[0])))
388 int zlen, i, j,
k, ret = 0;
390 BN_ULONG x1, x0, y1, y0, zz[4];
403 zlen = a->
top + b->
top + 4;
407 for (i = 0; i < zlen; i++) s->
d[i] = 0;
409 for (j = 0; j < b->
top; j += 2)
412 y1 = ((j+1) == b->
top) ? 0 : b->
d[j+1];
413 for (i = 0; i < a->
top; i += 2)
416 x1 = ((i+1) == a->
top) ? 0 : a->
d[i+1];
417 bn_GF2m_mul_2x2(zz, x1, x0, y1, y0);
418 for (k = 0; k < 4; k++) s->
d[i+j+k] ^= zz[k];
447 if ((arr = (
int *)
OPENSSL_malloc(
sizeof(
int) * max)) == NULL)
goto err;
449 if (!ret || ret > max)
473 for (i = a->
top - 1; i >= 0; i--)
475 s->
d[2*i+1] = SQR1(a->
d[i]);
476 s->
d[2*i ] = SQR0(a->
d[i]);
503 if ((arr = (
int *)
OPENSSL_malloc(
sizeof(
int) * max)) == NULL)
goto err;
505 if (!ret || ret > max)
525 BIGNUM *
b, *
c = NULL, *
u = NULL, *v = NULL, *tmp;
562 tmp =
u; u = v; v = tmp;
563 tmp =
b; b = c; c = tmp;
574 BN_ULONG *udp,*bdp,*vdp,*cdp;
577 for (i=u->top;i<top;i++) udp[i] = 0;
581 for (i=1;i<top;i++) bdp[i] = 0;
584 for (i=0;i<top;i++) cdp[i] = 0;
592 while (ubits && !(udp[0]&1))
594 BN_ULONG u0,u1,b0,b1,mask;
598 mask = (BN_ULONG)0-(b0&1);
600 for (i=0;i<top-1;i++)
603 udp[i] = ((u0>>1)|(u1<<(BN_BITS2-1)))&BN_MASK2;
605 b1 = bdp[i+1]^(p->
d[i+1]&mask);
606 bdp[i] = ((b0>>1)|(b1<<(BN_BITS2-1)))&BN_MASK2;
614 if (ubits<=BN_BITS2 && udp[0]==1)
break;
618 i = ubits; ubits = vbits; vbits = i;
619 tmp =
u; u = v; v = tmp;
620 tmp =
b; b = c; c = tmp;
621 udp = vdp; vdp = v->
d;
622 bdp = cdp; cdp = c->
d;
632 int utop = (ubits-1)/BN_BITS2;
634 while ((ul=udp[utop])==0 && utop) utop--;
669 if ((field =
BN_CTX_get(ctx)) == NULL)
goto err;
681 #ifndef OPENSSL_SUN_GF2M_DIV
696 if (xinv == NULL)
goto err;
729 if (v == NULL)
goto err;
797 if ((field =
BN_CTX_get(ctx)) == NULL)
goto err;
825 return (
BN_copy(r, a) != NULL);
833 for (i = n - 1; i >= 0; i--)
864 if ((arr = (
int *)
OPENSSL_malloc(
sizeof(
int) * max)) == NULL)
goto err;
866 if (!ret || ret > max)
922 if ((arr = (
int *)
OPENSSL_malloc(
sizeof(
int) * max)) == NULL)
goto err;
924 if (!ret || ret > max)
941 int ret = 0, count = 0, j;
942 BIGNUM *
a, *z, *rho, *w, *w2, *tmp;
957 if (w == NULL)
goto err;
972 for (j = 1; j <= (p[0] - 1) / 2; j++)
985 if (tmp == NULL)
goto err;
988 if (!
BN_rand(rho, p[0], 0, 0))
goto err;
991 if (!
BN_copy(w, rho))
goto err;
992 for (j = 1; j <= p[0] - 1; j++)
1041 max)) == NULL)
goto err;
1043 if (!ret || ret > max)
1069 for (i = a->
top - 1; i >= 0; i--)
1075 for (j = BN_BITS2 - 1; j >= 0; j--)
1079 if (k < max) p[k] = BN_BITS2 * i + j;
1103 for (i = 0; p[i] != -1; i++)