/*
* Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
*
* Licensed under the OpenSSL license (the "License"). You may not use
* this file except in compliance with the License. You can obtain a copy
* in the file LICENSE in the source distribution or at
* https://www.openssl.org/source/license.html
*/
#include <assert.h>
#include "internal/cryptlib.h"
#include "bn_lcl.h"
#if defined(OPENSSL_NO_ASM) || !defined(OPENSSL_BN_ASM_PART_WORDS)
/*
* Here follows specialised variants of bn_add_words() and bn_sub_words().
* They have the property performing operations on arrays of different sizes.
* The sizes of those arrays is expressed through cl, which is the common
* length ( basically, min(len(a),len(b)) ), and dl, which is the delta
* between the two lengths, calculated as len(a)-len(b). All lengths are the
* number of BN_ULONGs... For the operations that require a result array as
* parameter, it must have the length cl+abs(dl). These functions should
* probably end up in bn_asm.c as soon as there are assembler counterparts
* for the systems that use assembler files.
*/
BN_ULONG bn_sub_part_words(BN_ULONG *r,
const BN_ULONG *a, const BN_ULONG *b,
int cl, int dl)
{
BN_ULONG c, t;
assert(cl >= 0);
c = bn_sub_words(r, a, b, cl);
if (dl == 0)
return c;
r += cl;
a += cl;
b += cl;
if (dl < 0) {
for (;;) {
t = b[0];
r[0] = (0 - t - c) & BN_MASK2;
if (t != 0)
c = 1;
if (++dl >= 0)
break;
t = b[1];
r[1] = (0 - t - c) & BN_MASK2;
if (t != 0)
c = 1;
if (++dl >= 0)
break;
t = b[2];
r[2] = (0 - t - c) & BN_MASK2;
if (t != 0)
c = 1;
if (++dl >= 0)
break;
t = b[3];
r[3] = (0 - t - c) & BN_MASK2;
if (t != 0)
c = 1;
if (++dl >= 0)
break;
b += 4;
r += 4;
}
} else {
int save_dl = dl;
while (c) {
t = a[0];
r[0] = (t - c) & BN_MASK2;
if (t != 0)
c = 0;
if (--dl <= 0)
break;
t = a[1];
r[1] = (t - c) & BN_MASK2;
if (t != 0)
c = 0;
if (--dl <= 0)
break;
t = a[2];
r[2] = (t - c) & BN_MASK2;
if (t != 0)
c = 0;
if (--dl <= 0)
break;
t = a[3];
r[3] = (t - c) & BN_MASK2;
if (t != 0)
c = 0;
if (--dl <= 0)
break;
save_dl = dl;
a += 4;
r += 4;
}
if (dl > 0) {
if (save_dl > dl) {
switch (save_dl - dl) {
case 1:
r[1] = a[1];
if (--dl <= 0)
break;
case 2:
r[2] = a[2];
if (--dl <= 0)
break;
case 3:
r[3] = a[3];
if (--dl <= 0)
break;
}
a += 4;
r += 4;
}
}
if (dl > 0) {
for (;;) {
r[0] = a[0];
if (--dl <= 0)
break;
r[1] = a[1];
if (--dl <= 0)
break;
r[2] = a[2];
if (--dl <= 0)
break;
r[3] = a[3];
if (--dl <= 0)
break;
a += 4;
r += 4;
}
}
}
return c;
}
#endif
#ifdef BN_RECURSION
/*
* Karatsuba recursive multiplication algorithm (cf. Knuth, The Art of
* Computer Programming, Vol. 2)
*/
/*-
* r is 2*n2 words in size,
* a and b are both n2 words in size.
* n2 must be a power of 2.
* We multiply and return the result.
* t must be 2*n2 words in size
* We calculate
* a[0]*b[0]
* a[0]*b[0]+a[1]*b[1]+(a[0]-a[1])*(b[1]-b[0])
* a[1]*b[1]
*/
/* dnX may not be positive, but n2/2+dnX has to be */
void bn_mul_recursive(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b, int n2,
int dna, int dnb, BN_ULONG *t)
{
int n = n2 / 2, c1, c2;
int tna = n + dna, tnb = n + dnb;
unsigned int neg, zero;
BN_ULONG ln, lo