diff --git a/wolfcrypt/src/integer.c b/wolfcrypt/src/integer.c index 81987f96d..3da2bea02 100644 --- a/wolfcrypt/src/integer.c +++ b/wolfcrypt/src/integer.c @@ -5039,6 +5039,11 @@ int mp_lcm (mp_int * a, mp_int * b, mp_int * c) int res; mp_int t1, t2; + /* LCM of 0 and any number is undefined as 0 is not in the set of values + * being used. */ + if (mp_iszero (a) == MP_YES || mp_iszero (b) == MP_YES) { + return MP_VAL; + } if ((res = mp_init_multi (&t1, &t2, NULL, NULL, NULL, NULL)) != MP_OKAY) { return res; @@ -5083,6 +5088,10 @@ int mp_gcd (mp_int * a, mp_int * b, mp_int * c) /* either zero than gcd is the largest */ if (mp_iszero (a) == MP_YES) { + /* GCD of 0 and 0 is undefined as all integers divide 0. */ + if (mp_iszero (b) == MP_YES) { + return MP_VAL; + } return mp_abs (b, c); } if (mp_iszero (b) == MP_YES) { diff --git a/wolfcrypt/src/sp_int.c b/wolfcrypt/src/sp_int.c index 934037b97..f56302bec 100644 --- a/wolfcrypt/src/sp_int.c +++ b/wolfcrypt/src/sp_int.c @@ -13250,6 +13250,7 @@ int sp_gcd(sp_int* a, sp_int* b, sp_int* r) err = MP_VAL; } else if (sp_iszero(a)) { + /* GCD of 0 and 0 is undefined as all integers divide 0. */ if (sp_iszero(b)) { err = MP_VAL; } @@ -13360,6 +13361,10 @@ int sp_lcm(sp_int* a, sp_int* b, sp_int* r) if ((a == NULL) || (b == NULL) || (r == NULL)) { err = MP_VAL; } + + /* LCM of 0 and any number is undefined as 0 is not in the set of values + * being used. + */ if ((err == MP_OKAY) && (mp_iszero(a) || mp_iszero(b))) { err = MP_VAL; } diff --git a/wolfcrypt/src/tfm.c b/wolfcrypt/src/tfm.c index 8dab88207..505fc556f 100644 --- a/wolfcrypt/src/tfm.c +++ b/wolfcrypt/src/tfm.c @@ -5210,6 +5210,12 @@ int fp_lcm(fp_int *a, fp_int *b, fp_int *c) fp_int *t; #endif + /* LCM of 0 and any number is undefined as 0 is not in the set of values + * being used. */ + if (fp_iszero(a) == FP_YES || fp_iszero(b) == FP_YES) { + return FP_VAL; + } + #ifdef WOLFSSL_SMALL_STACK t = (fp_int*)XMALLOC(sizeof(fp_int) * 2, NULL, DYNAMIC_TYPE_BIGINT); if (t == NULL) { @@ -5249,6 +5255,11 @@ int fp_gcd(fp_int *a, fp_int *b, fp_int *c) fp_int *u, *v, *r; #endif + /* GCD of 0 and 0 is undefined as all integers divide 0. */ + if (fp_iszero(a) == FP_YES && fp_iszero(b) == FP_YES) { + return FP_VAL; + } + /* either zero than gcd is the largest */ if (fp_iszero (a) == FP_YES && fp_iszero (b) == FP_NO) { fp_abs (b, c);