Skip to content

Commit

Permalink
use uint8_t instead of unsigned char
Browse files Browse the repository at this point in the history
  • Loading branch information
minad committed Oct 29, 2019
1 parent 98753c6 commit b9977ad
Show file tree
Hide file tree
Showing 12 changed files with 42 additions and 42 deletions.
8 changes: 4 additions & 4 deletions demo/mtest_opponent.c
Original file line number Diff line number Diff line change
Expand Up @@ -139,9 +139,9 @@ static int mtest_opponent(void)
/* test the sign/unsigned storage functions */

rr = (unsigned)mp_sbin_size(&c);
DO(mp_to_sbin(&c, (unsigned char *) cmd, (size_t)rr, NULL));
DO(mp_to_sbin(&c, (uint8_t *) cmd, (size_t)rr, NULL));
memset(cmd + rr, rand() & 0xFF, sizeof(cmd) - rr);
DO(mp_from_sbin(&d, (unsigned char *) cmd, (size_t)rr));
DO(mp_from_sbin(&d, (uint8_t *) cmd, (size_t)rr));
if (mp_cmp(&c, &d) != MP_EQ) {
printf("mp_signed_bin failure!\n");
draw(&c);
Expand All @@ -150,9 +150,9 @@ static int mtest_opponent(void)
}

rr = (unsigned)mp_ubin_size(&c);
DO(mp_to_ubin(&c, (unsigned char *) cmd, (size_t)rr, NULL));
DO(mp_to_ubin(&c, (uint8_t *) cmd, (size_t)rr, NULL));
memset(cmd + rr, rand() & 0xFF, sizeof(cmd) - rr);
DO(mp_from_ubin(&d, (unsigned char *) cmd, (size_t)rr));
DO(mp_from_ubin(&d, (uint8_t *) cmd, (size_t)rr));
if (mp_cmp_mag(&c, &d) != MP_EQ) {
printf("mp_unsigned_bin failure!\n");
draw(&c);
Expand Down
6 changes: 3 additions & 3 deletions demo/test.c
Original file line number Diff line number Diff line change
Expand Up @@ -2169,7 +2169,7 @@ static int test_mp_read_write_ubin(void)
{
mp_int a, b, c;
size_t size, len;
unsigned char *buf = NULL;
uint8_t *buf = NULL;

DOR(mp_init_multi(&a, &b, &c, NULL));

Expand Down Expand Up @@ -2207,7 +2207,7 @@ static int test_mp_read_write_sbin(void)
{
mp_int a, b, c;
size_t size, len;
unsigned char *buf = NULL;
uint8_t *buf = NULL;

DOR(mp_init_multi(&a, &b, &c, NULL));

Expand Down Expand Up @@ -2246,7 +2246,7 @@ static int test_mp_pack_unpack(void)
{
mp_int a, b;
size_t written, count;
unsigned char *buf = NULL;
uint8_t *buf = NULL;

mp_order order = MP_LSB_FIRST;
mp_endian endianess = MP_NATIVE_ENDIAN;
Expand Down
8 changes: 4 additions & 4 deletions doc/bn.tex
Original file line number Diff line number Diff line change
Expand Up @@ -2444,15 +2444,15 @@ \section{Binary Conversions}

\index{mp\_to\_ubin}
\begin{alltt}
mp_err mp_to_ubin(const mp_int *a, unsigned char *buf, size_t maxlen, size_t *written)
mp_err mp_to_ubin(const mp_int *a, uint8_t *buf, size_t maxlen, size_t *written)
\end{alltt}
This will store $a$ into the buffer \texttt{buf} of size \texttt{maxlen} in big--endian format
storing the number of bytes written in \texttt{len}. Fortunately this is exactly what DER (or is
it ASN?) requires. It does not store the sign of the integer.

\index{mp\_from\_ubin}
\begin{alltt}
mp_err mp_from_ubin(mp_int *a, unsigned char *b, size_t size);
mp_err mp_from_ubin(mp_int *a, uint8_t *b, size_t size);
\end{alltt}
This will read in an unsigned big--endian array of bytes (octets) from \texttt{b} of length
\texttt{size} into $a$. The resulting big--integer $a$ will always be positive.
Expand All @@ -2462,8 +2462,8 @@ \section{Binary Conversions}
\index{mp\_sbin\_size} \index{mp\_from\_sbin} \index{mp\_to\_sbin}
\begin{alltt}
size_t mp_sbin_size(const mp_int *a);
mp_err mp_from_sbin(mp_int *a, const unsigned char *b, size_t size);
mp_err mp_to_sbin(const mp_int *a, unsigned char *b, size_t maxsize, size_t *len);
mp_err mp_from_sbin(mp_int *a, const uint8_t *b, size_t size);
mp_err mp_to_sbin(const mp_int *a, uint8_t *b, size_t maxsize, size_t *len);
\end{alltt}
They operate essentially the same as the unsigned copies except they prefix the data with zero or
non--zero byte depending on the sign. If the sign is \texttt{MP\_ZPOS} (e.g. not negative) the
Expand Down
4 changes: 2 additions & 2 deletions mp_from_sbin.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
/* SPDX-License-Identifier: Unlicense */

/* read signed bin, big endian, first byte is 0==positive or 1==negative */
mp_err mp_from_sbin(mp_int *a, const unsigned char *buf, size_t size)
mp_err mp_from_sbin(mp_int *a, const uint8_t *buf, size_t size)
{
mp_err err;

Expand All @@ -14,7 +14,7 @@ mp_err mp_from_sbin(mp_int *a, const unsigned char *buf, size_t size)
}

/* first byte is 0 for positive, non-zero for negative */
if (buf[0] == (unsigned char)0) {
if (buf[0] == (uint8_t)0) {
a->sign = MP_ZPOS;
} else {
a->sign = MP_NEG;
Expand Down
4 changes: 2 additions & 2 deletions mp_from_ubin.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
/* LibTomMath, multiple-precision integer library -- Tom St Denis */
/* SPDX-License-Identifier: Unlicense */

/* reads a unsigned char array, assumes the msb is stored first [big endian] */
mp_err mp_from_ubin(mp_int *a, const unsigned char *buf, size_t size)
/* reads a uint8_t array, assumes the msb is stored first [big endian] */
mp_err mp_from_ubin(mp_int *a, const uint8_t *buf, size_t size)
{
mp_err err;

Expand Down
12 changes: 6 additions & 6 deletions mp_pack.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ mp_err mp_pack(void *rop, size_t maxcount, size_t *written, mp_order order, size
{
mp_err err;
size_t odd_nails, nail_bytes, i, j, count;
unsigned char odd_nail_mask;
uint8_t odd_nail_mask;

mp_int t;

Expand All @@ -32,22 +32,22 @@ mp_err mp_pack(void *rop, size_t maxcount, size_t *written, mp_order order, size
odd_nails = (nails % 8u);
odd_nail_mask = 0xff;
for (i = 0u; i < odd_nails; ++i) {
odd_nail_mask ^= (unsigned char)(1u << (7u - i));
odd_nail_mask ^= (uint8_t)(1u << (7u - i));
}
nail_bytes = nails / 8u;

for (i = 0u; i < count; ++i) {
for (j = 0u; j < size; ++j) {
unsigned char *byte = (unsigned char *)rop +
(((order == MP_LSB_FIRST) ? i : ((count - 1u) - i)) * size) +
((endian == MP_LITTLE_ENDIAN) ? j : ((size - 1u) - j));
uint8_t *byte = (uint8_t *)rop +
(((order == MP_LSB_FIRST) ? i : ((count - 1u) - i)) * size) +
((endian == MP_LITTLE_ENDIAN) ? j : ((size - 1u) - j));

if (j >= (size - nail_bytes)) {
*byte = 0;
continue;
}

*byte = (unsigned char)((j == ((size - nail_bytes) - 1u)) ? (t.dp[0] & odd_nail_mask) : (t.dp[0] & 0xFFuL));
*byte = (uint8_t)((j == ((size - nail_bytes) - 1u)) ? (t.dp[0] & odd_nail_mask) : (t.dp[0] & 0xFFuL));

if ((err = mp_div_2d(&t, (j == ((size - nail_bytes) - 1u)) ? (int)(8u - odd_nails) : 8, &t, NULL)) != MP_OKAY) {
goto LBL_ERR;
Expand Down
10 changes: 5 additions & 5 deletions mp_prime_rand.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
/* This is possibly the mother of all prime generation functions, muahahahahaha! */
mp_err mp_prime_rand(mp_int *a, int t, int size, int flags)
{
unsigned char *tmp, maskAND, maskOR_msb, maskOR_lsb;
uint8_t *tmp, maskAND, maskOR_msb, maskOR_lsb;
int bsize, maskOR_msb_offset;
bool res;
mp_err err;
Expand All @@ -39,19 +39,19 @@ mp_err mp_prime_rand(mp_int *a, int t, int size, int flags)
bsize = (size>>3) + ((size&7)?1:0);

/* we need a buffer of bsize bytes */
tmp = (unsigned char *) MP_MALLOC((size_t)bsize);
tmp = (uint8_t *) MP_MALLOC((size_t)bsize);
if (tmp == NULL) {
return MP_MEM;
}

/* calc the maskAND value for the MSbyte*/
maskAND = ((size&7) == 0) ? 0xFFu : (unsigned char)(0xFFu >> (8 - (size & 7)));
maskAND = ((size&7) == 0) ? 0xFFu : (uint8_t)(0xFFu >> (8 - (size & 7)));

/* calc the maskOR_msb */
maskOR_msb = 0;
maskOR_msb_offset = ((size & 7) == 1) ? 1 : 0;
if ((flags & MP_PRIME_2MSB_ON) != 0) {
maskOR_msb |= (unsigned char)(0x80 >> ((9 - size) & 7));
maskOR_msb |= (uint8_t)(0x80 >> ((9 - size) & 7));
}

/* get the maskOR_lsb */
Expand All @@ -68,7 +68,7 @@ mp_err mp_prime_rand(mp_int *a, int t, int size, int flags)

/* work over the MSbyte */
tmp[0] &= maskAND;
tmp[0] |= (unsigned char)(1 << ((size - 1) & 7));
tmp[0] |= (uint8_t)(1 << ((size - 1) & 7));

/* mix in the maskORs */
tmp[maskOR_msb_offset] |= maskOR_msb;
Expand Down
6 changes: 3 additions & 3 deletions mp_to_radix.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
/* SPDX-License-Identifier: Unlicense */

/* reverse an array, used for radix code */
static void s_mp_reverse(unsigned char *s, size_t len)
static void s_mp_reverse(uint8_t *s, size_t len)
{
size_t ix, iy;
unsigned char t;
uint8_t t;

ix = 0u;
iy = len - 1u;
Expand Down Expand Up @@ -83,7 +83,7 @@ mp_err mp_to_radix(const mp_int *a, char *str, size_t maxlen, size_t *written, i
/* reverse the digits of the string. In this case _s points
* to the first digit [exluding the sign] of the number
*/
s_mp_reverse((unsigned char *)_s, digs);
s_mp_reverse((uint8_t *)_s, digs);

/* append a NULL so the string is properly terminated */
*str = '\0';
Expand Down
4 changes: 2 additions & 2 deletions mp_to_sbin.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
/* SPDX-License-Identifier: Unlicense */

/* store in signed [big endian] format */
mp_err mp_to_sbin(const mp_int *a, unsigned char *buf, size_t maxlen, size_t *written)
mp_err mp_to_sbin(const mp_int *a, uint8_t *buf, size_t maxlen, size_t *written)
{
mp_err err;
if (maxlen == 0u) {
Expand All @@ -16,7 +16,7 @@ mp_err mp_to_sbin(const mp_int *a, unsigned char *buf, size_t maxlen, size_t *wr
if (written != NULL) {
(*written)++;
}
buf[0] = (a->sign == MP_ZPOS) ? (unsigned char)0 : (unsigned char)1;
buf[0] = (a->sign == MP_ZPOS) ? (uint8_t)0 : (uint8_t)1;
return MP_OKAY;
}
#endif
4 changes: 2 additions & 2 deletions mp_to_ubin.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
/* SPDX-License-Identifier: Unlicense */

/* store in unsigned [big endian] format */
mp_err mp_to_ubin(const mp_int *a, unsigned char *buf, size_t maxlen, size_t *written)
mp_err mp_to_ubin(const mp_int *a, uint8_t *buf, size_t maxlen, size_t *written)
{
size_t x, count;
mp_err err;
Expand All @@ -20,7 +20,7 @@ mp_err mp_to_ubin(const mp_int *a, unsigned char *buf, size_t maxlen, size_t *wr
}

for (x = count; x --> 0u;) {
buf[x] = (unsigned char)(t.dp[0] & 255u);
buf[x] = (uint8_t)(t.dp[0] & 255u);
if ((err = mp_div_2d(&t, 8, &t, NULL)) != MP_OKAY) {
goto LBL_ERR;
}
Expand Down
10 changes: 5 additions & 5 deletions mp_unpack.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ mp_err mp_unpack(mp_int *rop, size_t count, mp_order order, size_t size,
{
mp_err err;
size_t odd_nails, nail_bytes, i, j;
unsigned char odd_nail_mask;
uint8_t odd_nail_mask;

mp_zero(rop);

Expand All @@ -22,15 +22,15 @@ mp_err mp_unpack(mp_int *rop, size_t count, mp_order order, size_t size,
odd_nails = (nails % 8u);
odd_nail_mask = 0xff;
for (i = 0; i < odd_nails; ++i) {
odd_nail_mask ^= (unsigned char)(1u << (7u - i));
odd_nail_mask ^= (uint8_t)(1u << (7u - i));
}
nail_bytes = nails / 8u;

for (i = 0; i < count; ++i) {
for (j = 0; j < (size - nail_bytes); ++j) {
unsigned char byte = *((const unsigned char *)op +
(((order == MP_MSB_FIRST) ? i : ((count - 1u) - i)) * size) +
((endian == MP_BIG_ENDIAN) ? (j + nail_bytes) : (((size - 1u) - j) - nail_bytes)));
uint8_t byte = *((const uint8_t *)op +
(((order == MP_MSB_FIRST) ? i : ((count - 1u) - i)) * size) +
((endian == MP_BIG_ENDIAN) ? (j + nail_bytes) : (((size - 1u) - j) - nail_bytes)));

if ((err = mp_mul_2d(rop, (j == 0u) ? (int)(8u - odd_nails) : 8, rop)) != MP_OKAY) {
return err;
Expand Down
8 changes: 4 additions & 4 deletions tommath.h
Original file line number Diff line number Diff line change
Expand Up @@ -570,12 +570,12 @@ mp_err mp_expt_u32(const mp_int *a, uint32_t b, mp_int *c) MP_WUR;
int mp_count_bits(const mp_int *a) MP_WUR;

size_t mp_ubin_size(const mp_int *a) MP_WUR;
mp_err mp_from_ubin(mp_int *a, const unsigned char *buf, size_t size) MP_WUR;
mp_err mp_to_ubin(const mp_int *a, unsigned char *buf, size_t maxlen, size_t *written) MP_WUR;
mp_err mp_from_ubin(mp_int *a, const uint8_t *buf, size_t size) MP_WUR;
mp_err mp_to_ubin(const mp_int *a, uint8_t *buf, size_t maxlen, size_t *written) MP_WUR;

size_t mp_sbin_size(const mp_int *a) MP_WUR;
mp_err mp_from_sbin(mp_int *a, const unsigned char *buf, size_t size) MP_WUR;
mp_err mp_to_sbin(const mp_int *a, unsigned char *buf, size_t maxlen, size_t *written) MP_WUR;
mp_err mp_from_sbin(mp_int *a, const uint8_t *buf, size_t size) MP_WUR;
mp_err mp_to_sbin(const mp_int *a, uint8_t *buf, size_t maxlen, size_t *written) MP_WUR;

mp_err mp_read_radix(mp_int *a, const char *str, int radix) MP_WUR;
mp_err mp_to_radix(const mp_int *a, char *str, size_t maxlen, size_t *written, int radix) MP_WUR;
Expand Down

0 comments on commit b9977ad

Please sign in to comment.