Skip to content

Commit

Permalink
Merge pull request #7410 from SparkiDev/sp_arm_big_endian
Browse files Browse the repository at this point in the history
SP: big-endian support
  • Loading branch information
JacobBarthelmeh authored Nov 27, 2024
2 parents d620e93 + 10e8f68 commit 5e13fc2
Show file tree
Hide file tree
Showing 4 changed files with 304 additions and 113 deletions.
133 changes: 98 additions & 35 deletions wolfcrypt/src/sp_arm32.c
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,8 @@ static void sp_2048_from_bin(sp_digit* r, int size, const byte* a, int n)
int j;
byte* d;

for (i = n - 1,j = 0; i >= 3; i -= 4) {
j = 0;
for (i = n - 1; i >= 3; i -= 4) {
r[j] = ((sp_digit)a[i - 0] << 0) |
((sp_digit)a[i - 1] << 8) |
((sp_digit)a[i - 2] << 16) |
Expand All @@ -104,12 +105,20 @@ static void sp_2048_from_bin(sp_digit* r, int size, const byte* a, int n)
if (i >= 0) {
r[j] = 0;

d = (byte*)r;
d = (byte*)(r + j);
#ifdef BIG_ENDIAN_ORDER
switch (i) {
case 2: d[n - 1 - 2] = a[2]; //fallthrough
case 1: d[n - 1 - 1] = a[1]; //fallthrough
case 0: d[n - 1 - 0] = a[0]; //fallthrough
case 2: d[1] = *(a++); //fallthrough
case 1: d[2] = *(a++); //fallthrough
case 0: d[3] = *a ; //fallthrough
}
#else
switch (i) {
case 2: d[2] = a[2]; //fallthrough
case 1: d[1] = a[1]; //fallthrough
case 0: d[0] = a[0]; //fallthrough
}
#endif
j++;
}

Expand Down Expand Up @@ -18287,7 +18296,8 @@ static void sp_3072_from_bin(sp_digit* r, int size, const byte* a, int n)
int j;
byte* d;

for (i = n - 1,j = 0; i >= 3; i -= 4) {
j = 0;
for (i = n - 1; i >= 3; i -= 4) {
r[j] = ((sp_digit)a[i - 0] << 0) |
((sp_digit)a[i - 1] << 8) |
((sp_digit)a[i - 2] << 16) |
Expand All @@ -18298,12 +18308,20 @@ static void sp_3072_from_bin(sp_digit* r, int size, const byte* a, int n)
if (i >= 0) {
r[j] = 0;

d = (byte*)r;
d = (byte*)(r + j);
#ifdef BIG_ENDIAN_ORDER
switch (i) {
case 2: d[1] = *(a++); //fallthrough
case 1: d[2] = *(a++); //fallthrough
case 0: d[3] = *a ; //fallthrough
}
#else
switch (i) {
case 2: d[n - 1 - 2] = a[2]; //fallthrough
case 1: d[n - 1 - 1] = a[1]; //fallthrough
case 0: d[n - 1 - 0] = a[0]; //fallthrough
case 2: d[2] = a[2]; //fallthrough
case 1: d[1] = a[1]; //fallthrough
case 0: d[0] = a[0]; //fallthrough
}
#endif
j++;
}

Expand Down Expand Up @@ -45799,7 +45817,8 @@ static void sp_4096_from_bin(sp_digit* r, int size, const byte* a, int n)
int j;
byte* d;

for (i = n - 1,j = 0; i >= 3; i -= 4) {
j = 0;
for (i = n - 1; i >= 3; i -= 4) {
r[j] = ((sp_digit)a[i - 0] << 0) |
((sp_digit)a[i - 1] << 8) |
((sp_digit)a[i - 2] << 16) |
Expand All @@ -45810,12 +45829,20 @@ static void sp_4096_from_bin(sp_digit* r, int size, const byte* a, int n)
if (i >= 0) {
r[j] = 0;

d = (byte*)r;
d = (byte*)(r + j);
#ifdef BIG_ENDIAN_ORDER
switch (i) {
case 2: d[1] = *(a++); //fallthrough
case 1: d[2] = *(a++); //fallthrough
case 0: d[3] = *a ; //fallthrough
}
#else
switch (i) {
case 2: d[n - 1 - 2] = a[2]; //fallthrough
case 1: d[n - 1 - 1] = a[1]; //fallthrough
case 0: d[n - 1 - 0] = a[0]; //fallthrough
case 2: d[2] = a[2]; //fallthrough
case 1: d[1] = a[1]; //fallthrough
case 0: d[0] = a[0]; //fallthrough
}
#endif
j++;
}

Expand Down Expand Up @@ -76443,7 +76470,8 @@ static void sp_256_from_bin(sp_digit* r, int size, const byte* a, int n)
int j;
byte* d;

for (i = n - 1,j = 0; i >= 3; i -= 4) {
j = 0;
for (i = n - 1; i >= 3; i -= 4) {
r[j] = ((sp_digit)a[i - 0] << 0) |
((sp_digit)a[i - 1] << 8) |
((sp_digit)a[i - 2] << 16) |
Expand All @@ -76454,12 +76482,20 @@ static void sp_256_from_bin(sp_digit* r, int size, const byte* a, int n)
if (i >= 0) {
r[j] = 0;

d = (byte*)r;
d = (byte*)(r + j);
#ifdef BIG_ENDIAN_ORDER
switch (i) {
case 2: d[1] = *(a++); //fallthrough
case 1: d[2] = *(a++); //fallthrough
case 0: d[3] = *a ; //fallthrough
}
#else
switch (i) {
case 2: d[n - 1 - 2] = a[2]; //fallthrough
case 1: d[n - 1 - 1] = a[1]; //fallthrough
case 0: d[n - 1 - 0] = a[0]; //fallthrough
case 2: d[2] = a[2]; //fallthrough
case 1: d[1] = a[1]; //fallthrough
case 0: d[0] = a[0]; //fallthrough
}
#endif
j++;
}

Expand Down Expand Up @@ -94227,7 +94263,8 @@ static void sp_384_from_bin(sp_digit* r, int size, const byte* a, int n)
int j;
byte* d;

for (i = n - 1,j = 0; i >= 3; i -= 4) {
j = 0;
for (i = n - 1; i >= 3; i -= 4) {
r[j] = ((sp_digit)a[i - 0] << 0) |
((sp_digit)a[i - 1] << 8) |
((sp_digit)a[i - 2] << 16) |
Expand All @@ -94238,12 +94275,20 @@ static void sp_384_from_bin(sp_digit* r, int size, const byte* a, int n)
if (i >= 0) {
r[j] = 0;

d = (byte*)r;
d = (byte*)(r + j);
#ifdef BIG_ENDIAN_ORDER
switch (i) {
case 2: d[n - 1 - 2] = a[2]; //fallthrough
case 1: d[n - 1 - 1] = a[1]; //fallthrough
case 0: d[n - 1 - 0] = a[0]; //fallthrough
case 2: d[1] = *(a++); //fallthrough
case 1: d[2] = *(a++); //fallthrough
case 0: d[3] = *a ; //fallthrough
}
#else
switch (i) {
case 2: d[2] = a[2]; //fallthrough
case 1: d[1] = a[1]; //fallthrough
case 0: d[0] = a[0]; //fallthrough
}
#endif
j++;
}

Expand Down Expand Up @@ -122000,7 +122045,8 @@ static void sp_521_from_bin(sp_digit* r, int size, const byte* a, int n)
int j;
byte* d;

for (i = n - 1,j = 0; i >= 3; i -= 4) {
j = 0;
for (i = n - 1; i >= 3; i -= 4) {
r[j] = ((sp_digit)a[i - 0] << 0) |
((sp_digit)a[i - 1] << 8) |
((sp_digit)a[i - 2] << 16) |
Expand All @@ -122011,12 +122057,20 @@ static void sp_521_from_bin(sp_digit* r, int size, const byte* a, int n)
if (i >= 0) {
r[j] = 0;

d = (byte*)r;
d = (byte*)(r + j);
#ifdef BIG_ENDIAN_ORDER
switch (i) {
case 2: d[1] = *(a++); //fallthrough
case 1: d[2] = *(a++); //fallthrough
case 0: d[3] = *a ; //fallthrough
}
#else
switch (i) {
case 2: d[n - 1 - 2] = a[2]; //fallthrough
case 1: d[n - 1 - 1] = a[1]; //fallthrough
case 0: d[n - 1 - 0] = a[0]; //fallthrough
case 2: d[2] = a[2]; //fallthrough
case 1: d[1] = a[1]; //fallthrough
case 0: d[0] = a[0]; //fallthrough
}
#endif
j++;
}

Expand Down Expand Up @@ -156650,7 +156704,8 @@ static void sp_1024_from_bin(sp_digit* r, int size, const byte* a, int n)
int j;
byte* d;

for (i = n - 1,j = 0; i >= 3; i -= 4) {
j = 0;
for (i = n - 1; i >= 3; i -= 4) {
r[j] = ((sp_digit)a[i - 0] << 0) |
((sp_digit)a[i - 1] << 8) |
((sp_digit)a[i - 2] << 16) |
Expand All @@ -156661,12 +156716,20 @@ static void sp_1024_from_bin(sp_digit* r, int size, const byte* a, int n)
if (i >= 0) {
r[j] = 0;

d = (byte*)r;
d = (byte*)(r + j);
#ifdef BIG_ENDIAN_ORDER
switch (i) {
case 2: d[n - 1 - 2] = a[2]; //fallthrough
case 1: d[n - 1 - 1] = a[1]; //fallthrough
case 0: d[n - 1 - 0] = a[0]; //fallthrough
case 2: d[1] = *(a++); //fallthrough
case 1: d[2] = *(a++); //fallthrough
case 0: d[3] = *a ; //fallthrough
}
#else
switch (i) {
case 2: d[2] = a[2]; //fallthrough
case 1: d[1] = a[1]; //fallthrough
case 0: d[0] = a[0]; //fallthrough
}
#endif
j++;
}

Expand Down
Loading

0 comments on commit 5e13fc2

Please sign in to comment.