From 2bb619a508ecb3df24be709345fae7ef0dbd434d Mon Sep 17 00:00:00 2001 From: Minty-Meeo <45425365+Minty-Meeo@users.noreply.github.com> Date: Thu, 23 Mar 2023 10:42:37 -0500 Subject: [PATCH] Resolve [-Wstringop-overflow=] --- Source/Core/Common/Crypto/bn.cpp | 27 +++++++++++++++------------ Source/Core/Common/Crypto/bn.h | 14 ++++++++------ 2 files changed, 23 insertions(+), 18 deletions(-) diff --git a/Source/Core/Common/Crypto/bn.cpp b/Source/Core/Common/Crypto/bn.cpp index 3dc8c75280..28bd216824 100644 --- a/Source/Core/Common/Crypto/bn.cpp +++ b/Source/Core/Common/Crypto/bn.cpp @@ -3,42 +3,45 @@ #include "Common/Crypto/bn.h" +#include #include #include #include "Common/CommonTypes.h" -static void bn_zero(u8* d, int n) +static void bn_zero(u8* d, const size_t n) { std::memset(d, 0, n); } -static void bn_copy(u8* d, const u8* a, int n) +static void bn_copy(u8* d, const u8* a, const size_t n) { std::memcpy(d, a, n); } -int bn_compare(const u8* a, const u8* b, int n) +int bn_compare(const u8* a, const u8* b, const size_t n) { return std::memcmp(a, b, n); } -void bn_sub_modulus(u8* a, const u8* N, int n) +void bn_sub_modulus(u8* a, const u8* N, const size_t n) { u8 c = 0; - for (int i = n - 1; i >= 0; --i) + for (size_t i = n; i > 0;) { + --i; u32 dig = N[i] + c; c = (a[i] < dig); a[i] -= dig; } } -void bn_add(u8* d, const u8* a, const u8* b, const u8* N, int n) +void bn_add(u8* d, const u8* a, const u8* b, const u8* N, const size_t n) { u8 c = 0; - for (int i = n - 1; i >= 0; --i) + for (size_t i = n; i > 0;) { + --i; u32 dig = a[i] + b[i] + c; c = (dig >= 0x100); d[i] = dig; @@ -51,11 +54,11 @@ void bn_add(u8* d, const u8* a, const u8* b, const u8* N, int n) bn_sub_modulus(d, N, n); } -void bn_mul(u8* d, const u8* a, const u8* b, const u8* N, int n) +void bn_mul(u8* d, const u8* a, const u8* b, const u8* N, const size_t n) { bn_zero(d, n); - for (int i = 0; i < n; i++) + for (size_t i = 0; i < n; i++) { for (u8 mask = 0x80; mask != 0; mask >>= 1) { @@ -66,13 +69,13 @@ void bn_mul(u8* d, const u8* a, const u8* b, const u8* N, int n) } } -void bn_exp(u8* d, const u8* a, const u8* N, int n, const u8* e, int en) +void bn_exp(u8* d, const u8* a, const u8* N, const size_t n, const u8* e, const size_t en) { u8 t[512]; bn_zero(d, n); d[n - 1] = 1; - for (int i = 0; i < en; i++) + for (size_t i = 0; i < en; i++) { for (u8 mask = 0x80; mask != 0; mask >>= 1) { @@ -86,7 +89,7 @@ void bn_exp(u8* d, const u8* a, const u8* N, int n, const u8* e, int en) } // only for prime N -- stupid but lazy, see if I care -void bn_inv(u8* d, const u8* a, const u8* N, int n) +void bn_inv(u8* d, const u8* a, const u8* N, const size_t n) { u8 t[512], s[512]; diff --git a/Source/Core/Common/Crypto/bn.h b/Source/Core/Common/Crypto/bn.h index 4070fd5f00..2dcc8c864b 100644 --- a/Source/Core/Common/Crypto/bn.h +++ b/Source/Core/Common/Crypto/bn.h @@ -3,13 +3,15 @@ #pragma once +#include + #include "Common/CommonTypes.h" // bignum arithmetic -int bn_compare(const u8* a, const u8* b, int n); -void bn_sub_modulus(u8* a, const u8* N, int n); -void bn_add(u8* d, const u8* a, const u8* b, const u8* N, int n); -void bn_mul(u8* d, const u8* a, const u8* b, const u8* N, int n); -void bn_inv(u8* d, const u8* a, const u8* N, int n); // only for prime N -void bn_exp(u8* d, const u8* a, const u8* N, int n, const u8* e, int en); +int bn_compare(const u8* a, const u8* b, size_t n); +void bn_sub_modulus(u8* a, const u8* N, size_t n); +void bn_add(u8* d, const u8* a, const u8* b, const u8* N, size_t n); +void bn_mul(u8* d, const u8* a, const u8* b, const u8* N, size_t n); +void bn_inv(u8* d, const u8* a, const u8* N, size_t n); // only for prime N +void bn_exp(u8* d, const u8* a, const u8* N, size_t n, const u8* e, size_t en);