Resolve [-Wstringop-overflow=]

This commit is contained in:
Minty-Meeo 2023-03-23 10:42:37 -05:00 committed by get
parent 3282082599
commit 2bb619a508
2 changed files with 23 additions and 18 deletions

View File

@ -3,42 +3,45 @@
#include "Common/Crypto/bn.h" #include "Common/Crypto/bn.h"
#include <cstddef>
#include <cstdio> #include <cstdio>
#include <cstring> #include <cstring>
#include "Common/CommonTypes.h" #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); 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); 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); 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; u8 c = 0;
for (int i = n - 1; i >= 0; --i) for (size_t i = n; i > 0;)
{ {
--i;
u32 dig = N[i] + c; u32 dig = N[i] + c;
c = (a[i] < dig); c = (a[i] < dig);
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; 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; u32 dig = a[i] + b[i] + c;
c = (dig >= 0x100); c = (dig >= 0x100);
d[i] = dig; 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); 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); 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) 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]; u8 t[512];
bn_zero(d, n); bn_zero(d, n);
d[n - 1] = 1; 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) 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 // 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]; u8 t[512], s[512];

View File

@ -3,13 +3,15 @@
#pragma once #pragma once
#include <cstddef>
#include "Common/CommonTypes.h" #include "Common/CommonTypes.h"
// bignum arithmetic // bignum arithmetic
int bn_compare(const u8* a, const u8* b, int n); int bn_compare(const u8* a, const u8* b, size_t n);
void bn_sub_modulus(u8* a, const u8* N, int 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, int 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, int 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, int n); // only for prime 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, int n, const u8* e, int en); void bn_exp(u8* d, const u8* a, const u8* N, size_t n, const u8* e, size_t en);