Skip to content

Commit dda1f8a

Browse files
committed
Harmonized behaviour when point length is invalid.
1 parent acc70b1 commit dda1f8a

File tree

4 files changed

+33
-34
lines changed

4 files changed

+33
-34
lines changed

src/ec/ec_p256_m15.c

+8-14
Original file line numberDiff line numberDiff line change
@@ -2039,12 +2039,13 @@ api_mul(unsigned char *G, size_t Glen,
20392039
p256_jacobian P;
20402040

20412041
(void)curve;
2042+
if (Glen != 65) {
2043+
return 0;
2044+
}
20422045
r = p256_decode(&P, G, Glen);
20432046
p256_mul(&P, x, xlen);
2044-
if (Glen >= 65) {
2045-
p256_to_affine(&P);
2046-
p256_encode(G, &P);
2047-
}
2047+
p256_to_affine(&P);
2048+
p256_encode(G, &P);
20482049
return r;
20492050
}
20502051

@@ -2059,16 +2060,6 @@ api_mulgen(unsigned char *R,
20592060
p256_to_affine(&P);
20602061
p256_encode(R, &P);
20612062
return 65;
2062-
2063-
/*
2064-
const unsigned char *G;
2065-
size_t Glen;
2066-
2067-
G = api_generator(curve, &Glen);
2068-
memcpy(R, G, Glen);
2069-
api_mul(R, Glen, x, xlen, curve);
2070-
return Glen;
2071-
*/
20722063
}
20732064

20742065
static uint32_t
@@ -2081,6 +2072,9 @@ api_muladd(unsigned char *A, const unsigned char *B, size_t len,
20812072
int i;
20822073

20832074
(void)curve;
2075+
if (len != 65) {
2076+
return 0;
2077+
}
20842078
r = p256_decode(&P, A, len);
20852079
p256_mul(&P, x, xlen);
20862080
if (B == NULL) {

src/ec/ec_p256_m31.c

+8-14
Original file line numberDiff line numberDiff line change
@@ -1384,12 +1384,13 @@ api_mul(unsigned char *G, size_t Glen,
13841384
p256_jacobian P;
13851385

13861386
(void)curve;
1387+
if (Glen != 65) {
1388+
return 0;
1389+
}
13871390
r = p256_decode(&P, G, Glen);
13881391
p256_mul(&P, x, xlen);
1389-
if (Glen >= 65) {
1390-
p256_to_affine(&P);
1391-
p256_encode(G, &P);
1392-
}
1392+
p256_to_affine(&P);
1393+
p256_encode(G, &P);
13931394
return r;
13941395
}
13951396

@@ -1404,16 +1405,6 @@ api_mulgen(unsigned char *R,
14041405
p256_to_affine(&P);
14051406
p256_encode(R, &P);
14061407
return 65;
1407-
1408-
/*
1409-
const unsigned char *G;
1410-
size_t Glen;
1411-
1412-
G = api_generator(curve, &Glen);
1413-
memcpy(R, G, Glen);
1414-
api_mul(R, Glen, x, xlen, curve);
1415-
return Glen;
1416-
*/
14171408
}
14181409

14191410
static uint32_t
@@ -1426,6 +1417,9 @@ api_muladd(unsigned char *A, const unsigned char *B, size_t len,
14261417
int i;
14271418

14281419
(void)curve;
1420+
if (len != 65) {
1421+
return 0;
1422+
}
14291423
r = p256_decode(&P, A, len);
14301424
p256_mul(&P, x, xlen);
14311425
if (B == NULL) {

src/ec/ec_prime_i15.c

+7-3
Original file line numberDiff line numberDiff line change
@@ -733,11 +733,12 @@ api_mul(unsigned char *G, size_t Glen,
733733
jacobian P;
734734

735735
cc = id_to_curve(curve);
736+
if (Glen != cc->point_len) {
737+
return 0;
738+
}
736739
r = point_decode(&P, G, Glen, cc);
737740
point_mul(&P, x, xlen, cc);
738-
if (Glen == cc->point_len) {
739-
point_encode(G, &P, cc);
740-
}
741+
point_encode(G, &P, cc);
741742
return r;
742743
}
743744

@@ -770,6 +771,9 @@ api_muladd(unsigned char *A, const unsigned char *B, size_t len,
770771
*/
771772

772773
cc = id_to_curve(curve);
774+
if (len != cc->point_len) {
775+
return 0;
776+
}
773777
r = point_decode(&P, A, len, cc);
774778
if (B == NULL) {
775779
size_t Glen;

src/ec/ec_prime_i31.c

+10-3
Original file line numberDiff line numberDiff line change
@@ -107,15 +107,16 @@ typedef struct {
107107
const uint32_t *b;
108108
const uint32_t *R2;
109109
uint32_t p0i;
110+
size_t point_len;
110111
} curve_params;
111112

112113
static inline const curve_params *
113114
id_to_curve(int curve)
114115
{
115116
static const curve_params pp[] = {
116-
{ P256_P, P256_B, P256_R2, 0x00000001 },
117-
{ P384_P, P384_B, P384_R2, 0x00000001 },
118-
{ P521_P, P521_B, P521_R2, 0x00000001 }
117+
{ P256_P, P256_B, P256_R2, 0x00000001, 65 },
118+
{ P384_P, P384_B, P384_R2, 0x00000001, 97 },
119+
{ P521_P, P521_B, P521_R2, 0x00000001, 133 }
119120
};
120121

121122
return &pp[curve - BR_EC_secp256r1];
@@ -734,6 +735,9 @@ api_mul(unsigned char *G, size_t Glen,
734735
jacobian P;
735736

736737
cc = id_to_curve(curve);
738+
if (Glen != cc->point_len) {
739+
return 0;
740+
}
737741
r = point_decode(&P, G, Glen, cc);
738742
point_mul(&P, x, xlen, cc);
739743
point_encode(G, &P, cc);
@@ -769,6 +773,9 @@ api_muladd(unsigned char *A, const unsigned char *B, size_t len,
769773
*/
770774

771775
cc = id_to_curve(curve);
776+
if (len != cc->point_len) {
777+
return 0;
778+
}
772779
r = point_decode(&P, A, len, cc);
773780
if (B == NULL) {
774781
size_t Glen;

0 commit comments

Comments
 (0)