use uint8_t instead of unsigned char

This commit is contained in:
Daniel Mendler 2019-10-29 08:44:51 +01:00
parent 98753c6718
commit b9977adfb8
No known key found for this signature in database
GPG Key ID: D88ADB2A2693CA43
12 changed files with 42 additions and 42 deletions

View File

@ -139,9 +139,9 @@ static int mtest_opponent(void)
/* test the sign/unsigned storage functions */ /* test the sign/unsigned storage functions */
rr = (unsigned)mp_sbin_size(&c); 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); 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) { if (mp_cmp(&c, &d) != MP_EQ) {
printf("mp_signed_bin failure!\n"); printf("mp_signed_bin failure!\n");
draw(&c); draw(&c);
@ -150,9 +150,9 @@ static int mtest_opponent(void)
} }
rr = (unsigned)mp_ubin_size(&c); 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); 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) { if (mp_cmp_mag(&c, &d) != MP_EQ) {
printf("mp_unsigned_bin failure!\n"); printf("mp_unsigned_bin failure!\n");
draw(&c); draw(&c);

View File

@ -2169,7 +2169,7 @@ static int test_mp_read_write_ubin(void)
{ {
mp_int a, b, c; mp_int a, b, c;
size_t size, len; size_t size, len;
unsigned char *buf = NULL; uint8_t *buf = NULL;
DOR(mp_init_multi(&a, &b, &c, NULL)); DOR(mp_init_multi(&a, &b, &c, NULL));
@ -2207,7 +2207,7 @@ static int test_mp_read_write_sbin(void)
{ {
mp_int a, b, c; mp_int a, b, c;
size_t size, len; size_t size, len;
unsigned char *buf = NULL; uint8_t *buf = NULL;
DOR(mp_init_multi(&a, &b, &c, NULL)); DOR(mp_init_multi(&a, &b, &c, NULL));
@ -2246,7 +2246,7 @@ static int test_mp_pack_unpack(void)
{ {
mp_int a, b; mp_int a, b;
size_t written, count; size_t written, count;
unsigned char *buf = NULL; uint8_t *buf = NULL;
mp_order order = MP_LSB_FIRST; mp_order order = MP_LSB_FIRST;
mp_endian endianess = MP_NATIVE_ENDIAN; mp_endian endianess = MP_NATIVE_ENDIAN;

View File

@ -2444,7 +2444,7 @@ $a$.
\index{mp\_to\_ubin} \index{mp\_to\_ubin}
\begin{alltt} \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} \end{alltt}
This will store $a$ into the buffer \texttt{buf} of size \texttt{maxlen} in big--endian format 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 storing the number of bytes written in \texttt{len}. Fortunately this is exactly what DER (or is
@ -2452,7 +2452,7 @@ it ASN?) requires. It does not store the sign of the integer.
\index{mp\_from\_ubin} \index{mp\_from\_ubin}
\begin{alltt} \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} \end{alltt}
This will read in an unsigned big--endian array of bytes (octets) from \texttt{b} of length 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. \texttt{size} into $a$. The resulting big--integer $a$ will always be positive.
@ -2462,8 +2462,8 @@ versions of the previous functions.
\index{mp\_sbin\_size} \index{mp\_from\_sbin} \index{mp\_to\_sbin} \index{mp\_sbin\_size} \index{mp\_from\_sbin} \index{mp\_to\_sbin}
\begin{alltt} \begin{alltt}
size_t mp_sbin_size(const mp_int *a); 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_from_sbin(mp_int *a, const uint8_t *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_to_sbin(const mp_int *a, uint8_t *b, size_t maxsize, size_t *len);
\end{alltt} \end{alltt}
They operate essentially the same as the unsigned copies except they prefix the data with zero or 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 non--zero byte depending on the sign. If the sign is \texttt{MP\_ZPOS} (e.g. not negative) the

View File

@ -4,7 +4,7 @@
/* SPDX-License-Identifier: Unlicense */ /* SPDX-License-Identifier: Unlicense */
/* read signed bin, big endian, first byte is 0==positive or 1==negative */ /* 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; mp_err err;
@ -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 */ /* 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; a->sign = MP_ZPOS;
} else { } else {
a->sign = MP_NEG; a->sign = MP_NEG;

View File

@ -3,8 +3,8 @@
/* LibTomMath, multiple-precision integer library -- Tom St Denis */ /* LibTomMath, multiple-precision integer library -- Tom St Denis */
/* SPDX-License-Identifier: Unlicense */ /* SPDX-License-Identifier: Unlicense */
/* reads a unsigned char array, assumes the msb is stored first [big endian] */ /* reads a uint8_t array, assumes the msb is stored first [big endian] */
mp_err mp_from_ubin(mp_int *a, const unsigned char *buf, size_t size) mp_err mp_from_ubin(mp_int *a, const uint8_t *buf, size_t size)
{ {
mp_err err; mp_err err;

View File

@ -11,7 +11,7 @@ mp_err mp_pack(void *rop, size_t maxcount, size_t *written, mp_order order, size
{ {
mp_err err; mp_err err;
size_t odd_nails, nail_bytes, i, j, count; size_t odd_nails, nail_bytes, i, j, count;
unsigned char odd_nail_mask; uint8_t odd_nail_mask;
mp_int t; mp_int t;
@ -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_nails = (nails % 8u);
odd_nail_mask = 0xff; odd_nail_mask = 0xff;
for (i = 0u; i < odd_nails; ++i) { 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; nail_bytes = nails / 8u;
for (i = 0u; i < count; ++i) { for (i = 0u; i < count; ++i) {
for (j = 0u; j < size; ++j) { for (j = 0u; j < size; ++j) {
unsigned char *byte = (unsigned char *)rop + uint8_t *byte = (uint8_t *)rop +
(((order == MP_LSB_FIRST) ? i : ((count - 1u) - i)) * size) + (((order == MP_LSB_FIRST) ? i : ((count - 1u) - i)) * size) +
((endian == MP_LITTLE_ENDIAN) ? j : ((size - 1u) - j)); ((endian == MP_LITTLE_ENDIAN) ? j : ((size - 1u) - j));
if (j >= (size - nail_bytes)) { if (j >= (size - nail_bytes)) {
*byte = 0; *byte = 0;
continue; 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) { if ((err = mp_div_2d(&t, (j == ((size - nail_bytes) - 1u)) ? (int)(8u - odd_nails) : 8, &t, NULL)) != MP_OKAY) {
goto LBL_ERR; goto LBL_ERR;

View File

@ -20,7 +20,7 @@
/* This is possibly the mother of all prime generation functions, muahahahahaha! */ /* 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) 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; int bsize, maskOR_msb_offset;
bool res; bool res;
mp_err err; mp_err err;
@ -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); bsize = (size>>3) + ((size&7)?1:0);
/* we need a buffer of bsize bytes */ /* 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) { if (tmp == NULL) {
return MP_MEM; return MP_MEM;
} }
/* calc the maskAND value for the MSbyte*/ /* 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 */ /* calc the maskOR_msb */
maskOR_msb = 0; maskOR_msb = 0;
maskOR_msb_offset = ((size & 7) == 1) ? 1 : 0; maskOR_msb_offset = ((size & 7) == 1) ? 1 : 0;
if ((flags & MP_PRIME_2MSB_ON) != 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 */ /* get the maskOR_lsb */
@ -68,7 +68,7 @@ mp_err mp_prime_rand(mp_int *a, int t, int size, int flags)
/* work over the MSbyte */ /* work over the MSbyte */
tmp[0] &= maskAND; tmp[0] &= maskAND;
tmp[0] |= (unsigned char)(1 << ((size - 1) & 7)); tmp[0] |= (uint8_t)(1 << ((size - 1) & 7));
/* mix in the maskORs */ /* mix in the maskORs */
tmp[maskOR_msb_offset] |= maskOR_msb; tmp[maskOR_msb_offset] |= maskOR_msb;

View File

@ -4,10 +4,10 @@
/* SPDX-License-Identifier: Unlicense */ /* SPDX-License-Identifier: Unlicense */
/* reverse an array, used for radix code */ /* 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; size_t ix, iy;
unsigned char t; uint8_t t;
ix = 0u; ix = 0u;
iy = len - 1u; iy = len - 1u;
@ -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 /* reverse the digits of the string. In this case _s points
* to the first digit [exluding the sign] of the number * 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 */ /* append a NULL so the string is properly terminated */
*str = '\0'; *str = '\0';

View File

@ -4,7 +4,7 @@
/* SPDX-License-Identifier: Unlicense */ /* SPDX-License-Identifier: Unlicense */
/* store in signed [big endian] format */ /* 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; mp_err err;
if (maxlen == 0u) { if (maxlen == 0u) {
@ -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) { if (written != NULL) {
(*written)++; (*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; return MP_OKAY;
} }
#endif #endif

View File

@ -4,7 +4,7 @@
/* SPDX-License-Identifier: Unlicense */ /* SPDX-License-Identifier: Unlicense */
/* store in unsigned [big endian] format */ /* 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; size_t x, count;
mp_err err; mp_err err;
@ -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;) { 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) { if ((err = mp_div_2d(&t, 8, &t, NULL)) != MP_OKAY) {
goto LBL_ERR; goto LBL_ERR;
} }

View File

@ -11,7 +11,7 @@ mp_err mp_unpack(mp_int *rop, size_t count, mp_order order, size_t size,
{ {
mp_err err; mp_err err;
size_t odd_nails, nail_bytes, i, j; size_t odd_nails, nail_bytes, i, j;
unsigned char odd_nail_mask; uint8_t odd_nail_mask;
mp_zero(rop); mp_zero(rop);
@ -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_nails = (nails % 8u);
odd_nail_mask = 0xff; odd_nail_mask = 0xff;
for (i = 0; i < odd_nails; ++i) { 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; nail_bytes = nails / 8u;
for (i = 0; i < count; ++i) { for (i = 0; i < count; ++i) {
for (j = 0; j < (size - nail_bytes); ++j) { for (j = 0; j < (size - nail_bytes); ++j) {
unsigned char byte = *((const unsigned char *)op + uint8_t byte = *((const uint8_t *)op +
(((order == MP_MSB_FIRST) ? i : ((count - 1u) - i)) * size) + (((order == MP_MSB_FIRST) ? i : ((count - 1u) - i)) * size) +
((endian == MP_BIG_ENDIAN) ? (j + nail_bytes) : (((size - 1u) - j) - nail_bytes))); ((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) { if ((err = mp_mul_2d(rop, (j == 0u) ? (int)(8u - odd_nails) : 8, rop)) != MP_OKAY) {
return err; return err;

View File

@ -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; int mp_count_bits(const mp_int *a) MP_WUR;
size_t mp_ubin_size(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_from_ubin(mp_int *a, const uint8_t *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_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; 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_from_sbin(mp_int *a, const uint8_t *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_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_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; mp_err mp_to_radix(const mp_int *a, char *str, size_t maxlen, size_t *written, int radix) MP_WUR;