Skip to content

Commit 10e8f68

Browse files
committed
SP ARM: big-endian support
Handle reading and writing from big-endian byte array when compiling for big endian. Rework little endian to be more effiecient too.
1 parent 5afa056 commit 10e8f68

4 files changed

Lines changed: 304 additions & 113 deletions

File tree

wolfcrypt/src/sp_arm32.c

Lines changed: 98 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,8 @@ static void sp_2048_from_bin(sp_digit* r, int size, const byte* a, int n)
9393
int j;
9494
byte* d;
9595

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

107-
d = (byte*)r;
108+
d = (byte*)(r + j);
109+
#ifdef BIG_ENDIAN_ORDER
108110
switch (i) {
109-
case 2: d[n - 1 - 2] = a[2]; //fallthrough
110-
case 1: d[n - 1 - 1] = a[1]; //fallthrough
111-
case 0: d[n - 1 - 0] = a[0]; //fallthrough
111+
case 2: d[1] = *(a++); //fallthrough
112+
case 1: d[2] = *(a++); //fallthrough
113+
case 0: d[3] = *a ; //fallthrough
112114
}
115+
#else
116+
switch (i) {
117+
case 2: d[2] = a[2]; //fallthrough
118+
case 1: d[1] = a[1]; //fallthrough
119+
case 0: d[0] = a[0]; //fallthrough
120+
}
121+
#endif
113122
j++;
114123
}
115124

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

18290-
for (i = n - 1,j = 0; i >= 3; i -= 4) {
18299+
j = 0;
18300+
for (i = n - 1; i >= 3; i -= 4) {
1829118301
r[j] = ((sp_digit)a[i - 0] << 0) |
1829218302
((sp_digit)a[i - 1] << 8) |
1829318303
((sp_digit)a[i - 2] << 16) |
@@ -18298,12 +18308,20 @@ static void sp_3072_from_bin(sp_digit* r, int size, const byte* a, int n)
1829818308
if (i >= 0) {
1829918309
r[j] = 0;
1830018310

18301-
d = (byte*)r;
18311+
d = (byte*)(r + j);
18312+
#ifdef BIG_ENDIAN_ORDER
18313+
switch (i) {
18314+
case 2: d[1] = *(a++); //fallthrough
18315+
case 1: d[2] = *(a++); //fallthrough
18316+
case 0: d[3] = *a ; //fallthrough
18317+
}
18318+
#else
1830218319
switch (i) {
18303-
case 2: d[n - 1 - 2] = a[2]; //fallthrough
18304-
case 1: d[n - 1 - 1] = a[1]; //fallthrough
18305-
case 0: d[n - 1 - 0] = a[0]; //fallthrough
18320+
case 2: d[2] = a[2]; //fallthrough
18321+
case 1: d[1] = a[1]; //fallthrough
18322+
case 0: d[0] = a[0]; //fallthrough
1830618323
}
18324+
#endif
1830718325
j++;
1830818326
}
1830918327

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

45802-
for (i = n - 1,j = 0; i >= 3; i -= 4) {
45820+
j = 0;
45821+
for (i = n - 1; i >= 3; i -= 4) {
4580345822
r[j] = ((sp_digit)a[i - 0] << 0) |
4580445823
((sp_digit)a[i - 1] << 8) |
4580545824
((sp_digit)a[i - 2] << 16) |
@@ -45810,12 +45829,20 @@ static void sp_4096_from_bin(sp_digit* r, int size, const byte* a, int n)
4581045829
if (i >= 0) {
4581145830
r[j] = 0;
4581245831

45813-
d = (byte*)r;
45832+
d = (byte*)(r + j);
45833+
#ifdef BIG_ENDIAN_ORDER
45834+
switch (i) {
45835+
case 2: d[1] = *(a++); //fallthrough
45836+
case 1: d[2] = *(a++); //fallthrough
45837+
case 0: d[3] = *a ; //fallthrough
45838+
}
45839+
#else
4581445840
switch (i) {
45815-
case 2: d[n - 1 - 2] = a[2]; //fallthrough
45816-
case 1: d[n - 1 - 1] = a[1]; //fallthrough
45817-
case 0: d[n - 1 - 0] = a[0]; //fallthrough
45841+
case 2: d[2] = a[2]; //fallthrough
45842+
case 1: d[1] = a[1]; //fallthrough
45843+
case 0: d[0] = a[0]; //fallthrough
4581845844
}
45845+
#endif
4581945846
j++;
4582045847
}
4582145848

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

76446-
for (i = n - 1,j = 0; i >= 3; i -= 4) {
76473+
j = 0;
76474+
for (i = n - 1; i >= 3; i -= 4) {
7644776475
r[j] = ((sp_digit)a[i - 0] << 0) |
7644876476
((sp_digit)a[i - 1] << 8) |
7644976477
((sp_digit)a[i - 2] << 16) |
@@ -76454,12 +76482,20 @@ static void sp_256_from_bin(sp_digit* r, int size, const byte* a, int n)
7645476482
if (i >= 0) {
7645576483
r[j] = 0;
7645676484

76457-
d = (byte*)r;
76485+
d = (byte*)(r + j);
76486+
#ifdef BIG_ENDIAN_ORDER
76487+
switch (i) {
76488+
case 2: d[1] = *(a++); //fallthrough
76489+
case 1: d[2] = *(a++); //fallthrough
76490+
case 0: d[3] = *a ; //fallthrough
76491+
}
76492+
#else
7645876493
switch (i) {
76459-
case 2: d[n - 1 - 2] = a[2]; //fallthrough
76460-
case 1: d[n - 1 - 1] = a[1]; //fallthrough
76461-
case 0: d[n - 1 - 0] = a[0]; //fallthrough
76494+
case 2: d[2] = a[2]; //fallthrough
76495+
case 1: d[1] = a[1]; //fallthrough
76496+
case 0: d[0] = a[0]; //fallthrough
7646276497
}
76498+
#endif
7646376499
j++;
7646476500
}
7646576501

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

94230-
for (i = n - 1,j = 0; i >= 3; i -= 4) {
94266+
j = 0;
94267+
for (i = n - 1; i >= 3; i -= 4) {
9423194268
r[j] = ((sp_digit)a[i - 0] << 0) |
9423294269
((sp_digit)a[i - 1] << 8) |
9423394270
((sp_digit)a[i - 2] << 16) |
@@ -94238,12 +94275,20 @@ static void sp_384_from_bin(sp_digit* r, int size, const byte* a, int n)
9423894275
if (i >= 0) {
9423994276
r[j] = 0;
9424094277

94241-
d = (byte*)r;
94278+
d = (byte*)(r + j);
94279+
#ifdef BIG_ENDIAN_ORDER
9424294280
switch (i) {
94243-
case 2: d[n - 1 - 2] = a[2]; //fallthrough
94244-
case 1: d[n - 1 - 1] = a[1]; //fallthrough
94245-
case 0: d[n - 1 - 0] = a[0]; //fallthrough
94281+
case 2: d[1] = *(a++); //fallthrough
94282+
case 1: d[2] = *(a++); //fallthrough
94283+
case 0: d[3] = *a ; //fallthrough
9424694284
}
94285+
#else
94286+
switch (i) {
94287+
case 2: d[2] = a[2]; //fallthrough
94288+
case 1: d[1] = a[1]; //fallthrough
94289+
case 0: d[0] = a[0]; //fallthrough
94290+
}
94291+
#endif
9424794292
j++;
9424894293
}
9424994294

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

122003-
for (i = n - 1,j = 0; i >= 3; i -= 4) {
122048+
j = 0;
122049+
for (i = n - 1; i >= 3; i -= 4) {
122004122050
r[j] = ((sp_digit)a[i - 0] << 0) |
122005122051
((sp_digit)a[i - 1] << 8) |
122006122052
((sp_digit)a[i - 2] << 16) |
@@ -122011,12 +122057,20 @@ static void sp_521_from_bin(sp_digit* r, int size, const byte* a, int n)
122011122057
if (i >= 0) {
122012122058
r[j] = 0;
122013122059

122014-
d = (byte*)r;
122060+
d = (byte*)(r + j);
122061+
#ifdef BIG_ENDIAN_ORDER
122062+
switch (i) {
122063+
case 2: d[1] = *(a++); //fallthrough
122064+
case 1: d[2] = *(a++); //fallthrough
122065+
case 0: d[3] = *a ; //fallthrough
122066+
}
122067+
#else
122015122068
switch (i) {
122016-
case 2: d[n - 1 - 2] = a[2]; //fallthrough
122017-
case 1: d[n - 1 - 1] = a[1]; //fallthrough
122018-
case 0: d[n - 1 - 0] = a[0]; //fallthrough
122069+
case 2: d[2] = a[2]; //fallthrough
122070+
case 1: d[1] = a[1]; //fallthrough
122071+
case 0: d[0] = a[0]; //fallthrough
122019122072
}
122073+
#endif
122020122074
j++;
122021122075
}
122022122076

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

156653-
for (i = n - 1,j = 0; i >= 3; i -= 4) {
156707+
j = 0;
156708+
for (i = n - 1; i >= 3; i -= 4) {
156654156709
r[j] = ((sp_digit)a[i - 0] << 0) |
156655156710
((sp_digit)a[i - 1] << 8) |
156656156711
((sp_digit)a[i - 2] << 16) |
@@ -156661,12 +156716,20 @@ static void sp_1024_from_bin(sp_digit* r, int size, const byte* a, int n)
156661156716
if (i >= 0) {
156662156717
r[j] = 0;
156663156718

156664-
d = (byte*)r;
156719+
d = (byte*)(r + j);
156720+
#ifdef BIG_ENDIAN_ORDER
156665156721
switch (i) {
156666-
case 2: d[n - 1 - 2] = a[2]; //fallthrough
156667-
case 1: d[n - 1 - 1] = a[1]; //fallthrough
156668-
case 0: d[n - 1 - 0] = a[0]; //fallthrough
156722+
case 2: d[1] = *(a++); //fallthrough
156723+
case 1: d[2] = *(a++); //fallthrough
156724+
case 0: d[3] = *a ; //fallthrough
156669156725
}
156726+
#else
156727+
switch (i) {
156728+
case 2: d[2] = a[2]; //fallthrough
156729+
case 1: d[1] = a[1]; //fallthrough
156730+
case 0: d[0] = a[0]; //fallthrough
156731+
}
156732+
#endif
156670156733
j++;
156671156734
}
156672156735

0 commit comments

Comments
 (0)