Commit b9977adfb8115ab14ce050238fa4e1eb76d3f81c

Daniel Mendler 2019-10-29T08:44:51

use uint8_t instead of unsigned char

diff --git a/demo/mtest_opponent.c b/demo/mtest_opponent.c
index 6c49de2..1beab5f 100644
--- a/demo/mtest_opponent.c
+++ b/demo/mtest_opponent.c
@@ -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);
@@ -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);
diff --git a/demo/test.c b/demo/test.c
index 7a42330..8e37f71 100644
--- a/demo/test.c
+++ b/demo/test.c
@@ -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));
 
@@ -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));
 
@@ -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;
diff --git a/doc/bn.tex b/doc/bn.tex
index 1e8ac74..b8a6404 100644
--- a/doc/bn.tex
+++ b/doc/bn.tex
@@ -2444,7 +2444,7 @@ $a$.
 
 \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
@@ -2452,7 +2452,7 @@ 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.
@@ -2462,8 +2462,8 @@ versions of the previous functions.
 \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
diff --git a/mp_from_sbin.c b/mp_from_sbin.c
index 4335d88..c6e87d7 100644
--- a/mp_from_sbin.c
+++ b/mp_from_sbin.c
@@ -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;
 
@@ -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;
diff --git a/mp_from_ubin.c b/mp_from_ubin.c
index 315ff08..ae79be3 100644
--- a/mp_from_ubin.c
+++ b/mp_from_ubin.c
@@ -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;
 
diff --git a/mp_pack.c b/mp_pack.c
index ec0f62f..447f1fd 100644
--- a/mp_pack.c
+++ b/mp_pack.c
@@ -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;
 
@@ -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;
diff --git a/mp_prime_rand.c b/mp_prime_rand.c
index ff6df9c..8476b4f 100644
--- a/mp_prime_rand.c
+++ b/mp_prime_rand.c
@@ -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;
@@ -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 */
@@ -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;
diff --git a/mp_to_radix.c b/mp_to_radix.c
index 78ad989..c1ea233 100644
--- a/mp_to_radix.c
+++ b/mp_to_radix.c
@@ -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;
@@ -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';
diff --git a/mp_to_sbin.c b/mp_to_sbin.c
index ed21adc..8225d13 100644
--- a/mp_to_sbin.c
+++ b/mp_to_sbin.c
@@ -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) {
@@ -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
diff --git a/mp_to_ubin.c b/mp_to_ubin.c
index 2583503..e8643cc 100644
--- a/mp_to_ubin.c
+++ b/mp_to_ubin.c
@@ -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;
@@ -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;
       }
diff --git a/mp_unpack.c b/mp_unpack.c
index 5305b43..f0127fa 100644
--- a/mp_unpack.c
+++ b/mp_unpack.c
@@ -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);
 
@@ -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;
diff --git a/tommath.h b/tommath.h
index 8742126..86b19d3 100644
--- a/tommath.h
+++ b/tommath.h
@@ -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;