Просмотр исходного кода

修复在64位平台异常问题

小权一句两句 3 лет назад
Родитель
Сommit
9fa3b2b2de
13 измененных файлов с 266 добавлено и 266 удалено
  1. 9 9
      include/tiny_aes.h
  2. 4 4
      include/tiny_base64.h
  3. 13 13
      include/tiny_md5.h
  4. 14 14
      include/tiny_sha1.h
  5. 15 15
      include/tiny_sha2.h
  6. 1 1
      include/tinycrypt_config.h
  7. 3 3
      samples/aes_sample.c
  8. 3 3
      samples/md5_sample.c
  9. 105 105
      src/tiny_aes.c
  10. 10 10
      src/tiny_base64.c
  11. 29 29
      src/tiny_md5.c
  12. 29 29
      src/tiny_sha1.c
  13. 31 31
      src/tiny_sha2.c

+ 9 - 9
include/tiny_aes.h

@@ -45,8 +45,8 @@
  */
 typedef struct {
     int nr;         /*!<  number of rounds  */
-    unsigned long *rk;  /*!<  AES round keys    */
-    unsigned long buf[68];  /*!<  unaligned data    */
+    uint32_t *rk;  /*!<  AES round keys    */
+    uint32_t buf[68];  /*!<  unaligned data    */
 } tiny_aes_context;
 
 #ifdef __cplusplus
@@ -60,7 +60,7 @@ extern "C" {
      * \param key      encryption key
      * \param keysize  must be 128, 192 or 256
      */
-    void tiny_aes_setkey_enc(tiny_aes_context * ctx, unsigned char *key, int keysize);
+    void tiny_aes_setkey_enc(tiny_aes_context * ctx, uint8_t *key, int keysize);
 
     /**
      * \brief          AES key schedule (decryption)
@@ -69,7 +69,7 @@ extern "C" {
      * \param key      decryption key
      * \param keysize  must be 128, 192 or 256
      */
-    void tiny_aes_setkey_dec(tiny_aes_context * ctx, unsigned char *key, int keysize);
+    void tiny_aes_setkey_dec(tiny_aes_context * ctx, uint8_t *key, int keysize);
 
     /**
      * \brief          AES-ECB block encryption/decryption
@@ -81,7 +81,7 @@ extern "C" {
      */
     void tiny_aes_crypt_ecb(tiny_aes_context * ctx,
                int mode,
-               unsigned char input[16], unsigned char output[16]);
+               uint8_t input[16], uint8_t output[16]);
 
     /**
      * \brief          AES-CBC buffer encryption/decryption
@@ -96,8 +96,8 @@ extern "C" {
     void tiny_aes_crypt_cbc(tiny_aes_context * ctx,
                int mode,
                int length,
-               unsigned char iv[16],
-               unsigned char *input, unsigned char *output);
+               uint8_t iv[16],
+               uint8_t *input, uint8_t *output);
 
     /**
      * \brief          AES-CFB128 buffer encryption/decryption
@@ -114,8 +114,8 @@ extern "C" {
                   int mode,
                   int length,
                   int *iv_off,
-                  unsigned char iv[16],
-                  unsigned char *input, unsigned char *output);
+                  uint8_t iv[16],
+                  uint8_t *input, uint8_t *output);
 
 #ifdef __cplusplus
 }

+ 4 - 4
include/tiny_base64.h

@@ -59,8 +59,8 @@ extern "C" {
      * \note           Call this function with *dlen = 0 to obtain the
      *                 required buffer size in *dlen
      */
-    int tiny_base64_encode(unsigned char *dst, int *dlen,
-              unsigned char *src, int slen);
+    int tiny_base64_encode(uint8_t *dst, int *dlen,
+              uint8_t *src, int slen);
 
     /**
      * \brief          Decode a base64-formatted buffer
@@ -78,8 +78,8 @@ extern "C" {
      * \note           Call this function with *dlen = 0 to obtain the
      *                 required buffer size in *dlen
      */
-    int tiny_base64_decode(unsigned char *dst, int *dlen,
-              unsigned char *src, int slen);
+    int tiny_base64_decode(uint8_t *dst, int *dlen,
+              uint8_t *src, int slen);
 
 #ifdef __cplusplus
 }

+ 13 - 13
include/tiny_md5.h

@@ -41,12 +41,12 @@
  * \brief          MD5 context structure
  */
 typedef struct {
-    unsigned long total[2]; /*!< number of bytes processed  */
-    unsigned long state[4]; /*!< intermediate digest state  */
-    unsigned char buffer[64];   /*!< data block being processed */
+    uint32_t total[2]; /*!< number of bytes processed  */
+    uint32_t state[4]; /*!< intermediate digest state  */
+    uint8_t buffer[64];   /*!< data block being processed */
 
-    unsigned char ipad[64]; /*!< HMAC: inner padding        */
-    unsigned char opad[64]; /*!< HMAC: outer padding        */
+    uint8_t ipad[64]; /*!< HMAC: inner padding        */
+    uint8_t opad[64]; /*!< HMAC: outer padding        */
 } tiny_md5_context;
 
 #ifdef __cplusplus
@@ -67,7 +67,7 @@ extern "C" {
      * \param input    buffer holding the  data
      * \param ilen     length of the input data
      */
-    void tiny_md5_update(tiny_md5_context * ctx, unsigned char *input, int ilen);
+    void tiny_md5_update(tiny_md5_context * ctx, uint8_t *input, int ilen);
 
     /**
      * \brief          MD5 final digest
@@ -75,7 +75,7 @@ extern "C" {
      * \param ctx      MD5 context
      * \param output   MD5 checksum result
      */
-    void tiny_md5_finish(tiny_md5_context * ctx, unsigned char output[16]);
+    void tiny_md5_finish(tiny_md5_context * ctx, uint8_t output[16]);
 
     /**
      * \brief          Output = MD5( input buffer )
@@ -84,7 +84,7 @@ extern "C" {
      * \param ilen     length of the input data
      * \param output   MD5 checksum result
      */
-    void tiny_md5(unsigned char *input, int ilen, unsigned char output[16]);
+    void tiny_md5(uint8_t *input, int ilen, uint8_t output[16]);
 
     /**
      * \brief          MD5 HMAC context setup
@@ -93,7 +93,7 @@ extern "C" {
      * \param key      HMAC secret key
      * \param keylen   length of the HMAC key
      */
-    void tiny_md5_hmac_starts(tiny_md5_context * ctx, unsigned char *key, int keylen);
+    void tiny_md5_hmac_starts(tiny_md5_context * ctx, uint8_t *key, int keylen);
 
     /**
      * \brief          MD5 HMAC process buffer
@@ -102,7 +102,7 @@ extern "C" {
      * \param input    buffer holding the  data
      * \param ilen     length of the input data
      */
-    void tiny_md5_hmac_update(tiny_md5_context * ctx, unsigned char *input, int ilen);
+    void tiny_md5_hmac_update(tiny_md5_context * ctx, uint8_t *input, int ilen);
 
     /**
      * \brief          MD5 HMAC final digest
@@ -110,7 +110,7 @@ extern "C" {
      * \param ctx      HMAC context
      * \param output   MD5 HMAC checksum result
      */
-    void tiny_md5_hmac_finish(tiny_md5_context * ctx, unsigned char output[16]);
+    void tiny_md5_hmac_finish(tiny_md5_context * ctx, uint8_t output[16]);
 
     /**
      * \brief          Output = HMAC-MD5( hmac key, input buffer )
@@ -121,8 +121,8 @@ extern "C" {
      * \param ilen     length of the input data
      * \param output   HMAC-MD5 result
      */
-    void tiny_md5_hmac(unsigned char *key, int keylen,
-              unsigned char *input, int ilen, unsigned char output[16]);
+    void tiny_md5_hmac(uint8_t *key, int keylen,
+              uint8_t *input, int ilen, uint8_t output[16]);
 
 #ifdef __cplusplus
 }

+ 14 - 14
include/tiny_sha1.h

@@ -41,12 +41,12 @@
  * \brief          SHA-1 context structure
  */
 typedef struct {
-    unsigned long total[2]; /*!< number of bytes processed  */
-    unsigned long state[5]; /*!< intermediate digest state  */
-    unsigned char buffer[64];   /*!< data block being processed */
+    uint32_t total[2]; /*!< number of bytes processed  */
+    uint32_t state[5]; /*!< intermediate digest state  */
+    uint8_t buffer[64];   /*!< data block being processed */
 
-    unsigned char ipad[64]; /*!< HMAC: inner padding        */
-    unsigned char opad[64]; /*!< HMAC: outer padding        */
+    uint8_t ipad[64]; /*!< HMAC: inner padding        */
+    uint8_t opad[64]; /*!< HMAC: outer padding        */
 } tiny_sha1_context;
 
 #ifdef __cplusplus
@@ -67,7 +67,7 @@ extern "C" {
      * \param input    buffer holding the  data
      * \param ilen     length of the input data
      */
-    void tiny_sha1_update(tiny_sha1_context * ctx, unsigned char *input, int ilen);
+    void tiny_sha1_update(tiny_sha1_context * ctx, uint8_t *input, int ilen);
 
     /**
      * \brief          SHA-1 final digest
@@ -75,7 +75,7 @@ extern "C" {
      * \param ctx      SHA-1 context
      * \param output   SHA-1 checksum result
      */
-    void tiny_sha1_finish(tiny_sha1_context * ctx, unsigned char output[20]);
+    void tiny_sha1_finish(tiny_sha1_context * ctx, uint8_t output[20]);
 
     /**
      * \brief          Output = SHA-1( input buffer )
@@ -84,7 +84,7 @@ extern "C" {
      * \param ilen     length of the input data
      * \param output   SHA-1 checksum result
      */
-    void tiny_sha1(unsigned char *input, int ilen, unsigned char output[20]);
+    void tiny_sha1(uint8_t *input, int ilen, uint8_t output[20]);
 
     /**
      * \brief          SHA-1 HMAC context setup
@@ -93,7 +93,7 @@ extern "C" {
      * \param key      HMAC secret key
      * \param keylen   length of the HMAC key
      */
-    void tiny_sha1_hmac_starts(tiny_sha1_context * ctx, unsigned char *key,
+    void tiny_sha1_hmac_starts(tiny_sha1_context * ctx, uint8_t *key,
                   int keylen);
 
     /**
@@ -103,7 +103,7 @@ extern "C" {
      * \param input    buffer holding the  data
      * \param ilen     length of the input data
      */
-    void tiny_sha1_hmac_update(tiny_sha1_context * ctx, unsigned char *input,
+    void tiny_sha1_hmac_update(tiny_sha1_context * ctx, uint8_t *input,
                   int ilen);
 
     /**
@@ -112,7 +112,7 @@ extern "C" {
      * \param ctx      HMAC context
      * \param output   SHA-1 HMAC checksum result
      */
-    void tiny_sha1_hmac_finish(tiny_sha1_context * ctx, unsigned char output[20]);
+    void tiny_sha1_hmac_finish(tiny_sha1_context * ctx, uint8_t output[20]);
 
     /**
      * \brief          Output = HMAC-SHA-1( hmac key, input buffer )
@@ -123,9 +123,9 @@ extern "C" {
      * \param ilen     length of the input data
      * \param output   HMAC-SHA-1 result
      */
-    void tiny_sha1_hmac(unsigned char *key, int keylen,
-               unsigned char *input, int ilen,
-               unsigned char output[20]);
+    void tiny_sha1_hmac(uint8_t *key, int keylen,
+               uint8_t *input, int ilen,
+               uint8_t output[20]);
 
 #ifdef __cplusplus
 }

+ 15 - 15
include/tiny_sha2.h

@@ -41,12 +41,12 @@
  * \brief          SHA-256 context structure
  */
 typedef struct {
-    unsigned long total[2]; /*!< number of bytes processed  */
-    unsigned long state[8]; /*!< intermediate digest state  */
-    unsigned char buffer[64];   /*!< data block being processed */
+    uint32_t total[2]; /*!< number of bytes processed  */
+    uint32_t state[8]; /*!< intermediate digest state  */
+    uint8_t buffer[64];   /*!< data block being processed */
 
-    unsigned char ipad[64]; /*!< HMAC: inner padding        */
-    unsigned char opad[64]; /*!< HMAC: outer padding        */
+    uint8_t ipad[64]; /*!< HMAC: inner padding        */
+    uint8_t opad[64]; /*!< HMAC: outer padding        */
     int is224;      /*!< 0 => SHA-256, else SHA-224 */
 } tiny_sha2_context;
 
@@ -69,7 +69,7 @@ extern "C" {
      * \param input    buffer holding the  data
      * \param ilen     length of the input data
      */
-    void tiny_sha2_update(tiny_sha2_context * ctx, unsigned char *input, int ilen);
+    void tiny_sha2_update(tiny_sha2_context * ctx, uint8_t *input, int ilen);
 
     /**
      * \brief          SHA-256 final digest
@@ -77,7 +77,7 @@ extern "C" {
      * \param ctx      SHA-256 context
      * \param output   SHA-224/256 checksum result
      */
-    void tiny_sha2_finish(tiny_sha2_context * ctx, unsigned char output[32]);
+    void tiny_sha2_finish(tiny_sha2_context * ctx, uint8_t output[32]);
 
     /**
      * \brief          Output = SHA-256( input buffer )
@@ -87,8 +87,8 @@ extern "C" {
      * \param output   SHA-224/256 checksum result
      * \param is224    0 = use SHA256, 1 = use SHA224
      */
-    void tiny_sha2(unsigned char *input, int ilen,
-          unsigned char output[32], int is224);
+    void tiny_sha2(uint8_t *input, int ilen,
+          uint8_t output[32], int is224);
 
     /**
      * \brief          SHA-256 HMAC context setup
@@ -98,7 +98,7 @@ extern "C" {
      * \param keylen   length of the HMAC key
      * \param is224    0 = use SHA256, 1 = use SHA224
      */
-    void tiny_sha2_hmac_starts(tiny_sha2_context * ctx, unsigned char *key,
+    void tiny_sha2_hmac_starts(tiny_sha2_context * ctx, uint8_t *key,
                   int keylen, int is224);
 
     /**
@@ -108,7 +108,7 @@ extern "C" {
      * \param input    buffer holding the  data
      * \param ilen     length of the input data
      */
-    void tiny_sha2_hmac_update(tiny_sha2_context * ctx, unsigned char *input,
+    void tiny_sha2_hmac_update(tiny_sha2_context * ctx, uint8_t *input,
                   int ilen);
 
     /**
@@ -117,7 +117,7 @@ extern "C" {
      * \param ctx      HMAC context
      * \param output   SHA-224/256 HMAC checksum result
      */
-    void tiny_sha2_hmac_finish(tiny_sha2_context * ctx, unsigned char output[32]);
+    void tiny_sha2_hmac_finish(tiny_sha2_context * ctx, uint8_t output[32]);
 
     /**
      * \brief          Output = HMAC-SHA-256( hmac key, input buffer )
@@ -129,9 +129,9 @@ extern "C" {
      * \param output   HMAC-SHA-224/256 result
      * \param is224    0 = use SHA256, 1 = use SHA224
      */
-    void tiny_sha2_hmac(unsigned char *key, int keylen,
-               unsigned char *input, int ilen,
-               unsigned char output[32], int is224);
+    void tiny_sha2_hmac(uint8_t *key, int keylen,
+               uint8_t *input, int ilen,
+               uint8_t output[32], int is224);
 
 #ifdef __cplusplus
 }

+ 1 - 1
include/tinycrypt_config.h

@@ -38,6 +38,6 @@
 #ifndef TINY_CRYPT_CONFIG_H__
 #define TINY_CRYPT_CONFIG_H__
 
-#include <rtconfig.h>
+#include <rtthread.h>
 
 #endif

+ 3 - 3
samples/aes_sample.c

@@ -21,9 +21,9 @@ static rt_err_t test_tiny_aes(void)
     uint8_t iv[16 + 1];
     uint8_t private_key[32 + 1];
 
-    unsigned char data[] = "1234567890123456";
-    unsigned char data_encrypt[32];
-    unsigned char data_decrypt[32];
+    uint8_t data[] = "1234567890123456";
+    uint8_t data_encrypt[32];
+    uint8_t data_decrypt[32];
 
     /* encrypt */
     rt_memcpy(iv, TEST_TINY_AES_IV, rt_strlen(TEST_TINY_AES_IV));

+ 3 - 3
samples/md5_sample.c

@@ -13,7 +13,7 @@
 #include <tiny_md5.h>
 
 
-static const unsigned char md5_test_buf[7][81] =
+static const uint8_t md5_test_buf[7][81] =
 {
     { "" },
     { "a" },
@@ -30,7 +30,7 @@ static const size_t md5_test_buflen[7] =
     0, 1, 3, 14, 26, 62, 80
 };
 
-static const unsigned char md5_test_sum[7][16] =
+static const uint8_t md5_test_sum[7][16] =
 {
     { 0xD4, 0x1D, 0x8C, 0xD9, 0x8F, 0x00, 0xB2, 0x04,
       0xE9, 0x80, 0x09, 0x98, 0xEC, 0xF8, 0x42, 0x7E },
@@ -51,7 +51,7 @@ static const unsigned char md5_test_sum[7][16] =
 static rt_err_t test_tiny_md5(void)
 {
     int i, ret = 0;
-    unsigned char md5sum[16];
+    uint8_t md5sum[16];
 
     for (i = 0; i < 7; i++)
     {

+ 105 - 105
src/tiny_aes.c

@@ -54,20 +54,20 @@
 #ifndef GET_ULONG_LE
 #define GET_ULONG_LE(n,b,i)                             \
 {                                                       \
-    (n) = ( (unsigned long) (b)[(i)    ]       )        \
-        | ( (unsigned long) (b)[(i) + 1] <<  8 )        \
-        | ( (unsigned long) (b)[(i) + 2] << 16 )        \
-        | ( (unsigned long) (b)[(i) + 3] << 24 );       \
+    (n) = ( (uint32_t) (b)[(i)    ]       )        \
+        | ( (uint32_t) (b)[(i) + 1] <<  8 )        \
+        | ( (uint32_t) (b)[(i) + 2] << 16 )        \
+        | ( (uint32_t) (b)[(i) + 3] << 24 );       \
 }
 #endif
 
 #ifndef PUT_ULONG_LE
 #define PUT_ULONG_LE(n,b,i)                             \
 {                                                       \
-    (b)[(i)    ] = (unsigned char) ( (n)       );       \
-    (b)[(i) + 1] = (unsigned char) ( (n) >>  8 );       \
-    (b)[(i) + 2] = (unsigned char) ( (n) >> 16 );       \
-    (b)[(i) + 3] = (unsigned char) ( (n) >> 24 );       \
+    (b)[(i)    ] = (uint8_t) ( (n)       );       \
+    (b)[(i) + 1] = (uint8_t) ( (n) >>  8 );       \
+    (b)[(i) + 2] = (uint8_t) ( (n) >> 16 );       \
+    (b)[(i) + 3] = (uint8_t) ( (n) >> 24 );       \
 }
 #endif
 
@@ -75,7 +75,7 @@
 /*
  * Forward S-box
  */
-static const unsigned char FSb[256] = {
+static const uint8_t FSb[256] = {
     0x63, 0x7C, 0x77, 0x7B, 0xF2, 0x6B, 0x6F, 0xC5,
     0x30, 0x01, 0x67, 0x2B, 0xFE, 0xD7, 0xAB, 0x76,
     0xCA, 0x82, 0xC9, 0x7D, 0xFA, 0x59, 0x47, 0xF0,
@@ -181,22 +181,22 @@ static const unsigned char FSb[256] = {
     V(CB,B0,B0,7B), V(FC,54,54,A8), V(D6,BB,BB,6D), V(3A,16,16,2C)
 
 #define V(a,b,c,d) 0x##a##b##c##d
-static const unsigned long FT0[256] = { FT };
+static const uint32_t FT0[256] = { FT };
 
 #undef V
 
 #define V(a,b,c,d) 0x##b##c##d##a
-static const unsigned long FT1[256] = { FT };
+static const uint32_t FT1[256] = { FT };
 
 #undef V
 
 #define V(a,b,c,d) 0x##c##d##a##b
-static const unsigned long FT2[256] = { FT };
+static const uint32_t FT2[256] = { FT };
 
 #undef V
 
 #define V(a,b,c,d) 0x##d##a##b##c
-static const unsigned long FT3[256] = { FT };
+static const uint32_t FT3[256] = { FT };
 
 #undef V
 
@@ -205,7 +205,7 @@ static const unsigned long FT3[256] = { FT };
 /*
  * Reverse S-box
  */
-static const unsigned char RSb[256] = {
+static const uint8_t RSb[256] = {
     0x52, 0x09, 0x6A, 0xD5, 0x30, 0x36, 0xA5, 0x38,
     0xBF, 0x40, 0xA3, 0x9E, 0x81, 0xF3, 0xD7, 0xFB,
     0x7C, 0xE3, 0x39, 0x82, 0x9B, 0x2F, 0xFF, 0x87,
@@ -311,22 +311,22 @@ static const unsigned char RSb[256] = {
     V(61,84,CB,7B), V(70,B6,32,D5), V(74,5C,6C,48), V(42,57,B8,D0)
 
 #define V(a,b,c,d) 0x##a##b##c##d
-static const unsigned long RT0[256] = { RT };
+static const uint32_t RT0[256] = { RT };
 
 #undef V
 
 #define V(a,b,c,d) 0x##b##c##d##a
-static const unsigned long RT1[256] = { RT };
+static const uint32_t RT1[256] = { RT };
 
 #undef V
 
 #define V(a,b,c,d) 0x##c##d##a##b
-static const unsigned long RT2[256] = { RT };
+static const uint32_t RT2[256] = { RT };
 
 #undef V
 
 #define V(a,b,c,d) 0x##d##a##b##c
-static const unsigned long RT3[256] = { RT };
+static const uint32_t RT3[256] = { RT };
 
 #undef V
 
@@ -335,7 +335,7 @@ static const unsigned long RT3[256] = { RT };
 /*
  * Round constants
  */
-static const unsigned long RCON[10] = {
+static const uint32_t RCON[10] = {
     0x00000001, 0x00000002, 0x00000004, 0x00000008,
     0x00000010, 0x00000020, 0x00000040, 0x00000080,
     0x0000001B, 0x00000036
@@ -346,25 +346,25 @@ static const unsigned long RCON[10] = {
 /*
  * Forward S-box & tables
  */
-static unsigned char FSb[256];
-static unsigned long FT0[256];
-static unsigned long FT1[256];
-static unsigned long FT2[256];
-static unsigned long FT3[256];
+static uint8_t FSb[256];
+static uint32_t FT0[256];
+static uint32_t FT1[256];
+static uint32_t FT2[256];
+static uint32_t FT3[256];
 
 /*
  * Reverse S-box & tables
  */
-static unsigned char RSb[256];
-static unsigned long RT0[256];
-static unsigned long RT1[256];
-static unsigned long RT2[256];
-static unsigned long RT3[256];
+static uint8_t RSb[256];
+static uint32_t RT0[256];
+static uint32_t RT1[256];
+static uint32_t RT2[256];
+static uint32_t RT3[256];
 
 /*
  * Round constants
  */
-static unsigned long RCON[10];
+static uint32_t RCON[10];
 
 /*
  * Tables generation code
@@ -394,7 +394,7 @@ static void aes_gen_tables(void)
      * calculate the round constants
      */
     for (i = 0, x = 1; i < 10; i++) {
-        RCON[i] = (unsigned long)x;
+        RCON[i] = (uint32_t)x;
         x = XTIME(x) & 0xFF;
     }
 
@@ -417,8 +417,8 @@ static void aes_gen_tables(void)
         y = ((y << 1) | (y >> 7)) & 0xFF;
         x ^= y ^ 0x63;
 
-        FSb[i] = (unsigned char)x;
-        RSb[x] = (unsigned char)i;
+        FSb[i] = (uint8_t)x;
+        RSb[x] = (uint8_t)i;
     }
 
     /*
@@ -429,9 +429,9 @@ static void aes_gen_tables(void)
         y = XTIME(x) & 0xFF;
         z = (y ^ x) & 0xFF;
 
-        FT0[i] = ((unsigned long)y) ^
-            ((unsigned long)x << 8) ^
-            ((unsigned long)x << 16) ^ ((unsigned long)z << 24);
+        FT0[i] = ((uint32_t)y) ^
+            ((uint32_t)x << 8) ^
+            ((uint32_t)x << 16) ^ ((uint32_t)z << 24);
 
         FT1[i] = ROTL8(FT0[i]);
         FT2[i] = ROTL8(FT1[i]);
@@ -439,10 +439,10 @@ static void aes_gen_tables(void)
 
         x = RSb[i];
 
-        RT0[i] = ((unsigned long)MUL(0x0E, x)) ^
-            ((unsigned long)MUL(0x09, x) << 8) ^
-            ((unsigned long)MUL(0x0D, x) << 16) ^
-            ((unsigned long)MUL(0x0B, x) << 24);
+        RT0[i] = ((uint32_t)MUL(0x0E, x)) ^
+            ((uint32_t)MUL(0x09, x) << 8) ^
+            ((uint32_t)MUL(0x0D, x) << 16) ^
+            ((uint32_t)MUL(0x0B, x) << 24);
 
         RT1[i] = ROTL8(RT0[i]);
         RT2[i] = ROTL8(RT1[i]);
@@ -455,10 +455,10 @@ static void aes_gen_tables(void)
 /*
  * AES key schedule (encryption)
  */
-void tiny_aes_setkey_enc(tiny_aes_context * ctx, unsigned char *key, int keysize)
+void tiny_aes_setkey_enc(tiny_aes_context * ctx, uint8_t *key, int keysize)
 {
     int i;
-    unsigned long *RK;
+    uint32_t *RK;
 
 #if !defined(TINY_CRYPT_AES_ROM_TABLES)
     if (aes_init_done == 0) {
@@ -492,10 +492,10 @@ void tiny_aes_setkey_enc(tiny_aes_context * ctx, unsigned char *key, int keysize
 
         for (i = 0; i < 10; i++, RK += 4) {
             RK[4] = RK[0] ^ RCON[i] ^
-                ((unsigned long)FSb[(RK[3] >> 8) & 0xFF]) ^
-                ((unsigned long)FSb[(RK[3] >> 16) & 0xFF] << 8) ^
-                ((unsigned long)FSb[(RK[3] >> 24) & 0xFF] << 16) ^
-                ((unsigned long)FSb[(RK[3]) & 0xFF] << 24);
+                ((uint32_t)FSb[(RK[3] >> 8) & 0xFF]) ^
+                ((uint32_t)FSb[(RK[3] >> 16) & 0xFF] << 8) ^
+                ((uint32_t)FSb[(RK[3] >> 24) & 0xFF] << 16) ^
+                ((uint32_t)FSb[(RK[3]) & 0xFF] << 24);
 
             RK[5] = RK[1] ^ RK[4];
             RK[6] = RK[2] ^ RK[5];
@@ -507,10 +507,10 @@ void tiny_aes_setkey_enc(tiny_aes_context * ctx, unsigned char *key, int keysize
 
         for (i = 0; i < 8; i++, RK += 6) {
             RK[6] = RK[0] ^ RCON[i] ^
-                ((unsigned long)FSb[(RK[5] >> 8) & 0xFF]) ^
-                ((unsigned long)FSb[(RK[5] >> 16) & 0xFF] << 8) ^
-                ((unsigned long)FSb[(RK[5] >> 24) & 0xFF] << 16) ^
-                ((unsigned long)FSb[(RK[5]) & 0xFF] << 24);
+                ((uint32_t)FSb[(RK[5] >> 8) & 0xFF]) ^
+                ((uint32_t)FSb[(RK[5] >> 16) & 0xFF] << 8) ^
+                ((uint32_t)FSb[(RK[5] >> 24) & 0xFF] << 16) ^
+                ((uint32_t)FSb[(RK[5]) & 0xFF] << 24);
 
             RK[7] = RK[1] ^ RK[6];
             RK[8] = RK[2] ^ RK[7];
@@ -524,20 +524,20 @@ void tiny_aes_setkey_enc(tiny_aes_context * ctx, unsigned char *key, int keysize
 
         for (i = 0; i < 7; i++, RK += 8) {
             RK[8] = RK[0] ^ RCON[i] ^
-                ((unsigned long)FSb[(RK[7] >> 8) & 0xFF]) ^
-                ((unsigned long)FSb[(RK[7] >> 16) & 0xFF] << 8) ^
-                ((unsigned long)FSb[(RK[7] >> 24) & 0xFF] << 16) ^
-                ((unsigned long)FSb[(RK[7]) & 0xFF] << 24);
+                ((uint32_t)FSb[(RK[7] >> 8) & 0xFF]) ^
+                ((uint32_t)FSb[(RK[7] >> 16) & 0xFF] << 8) ^
+                ((uint32_t)FSb[(RK[7] >> 24) & 0xFF] << 16) ^
+                ((uint32_t)FSb[(RK[7]) & 0xFF] << 24);
 
             RK[9] = RK[1] ^ RK[8];
             RK[10] = RK[2] ^ RK[9];
             RK[11] = RK[3] ^ RK[10];
 
             RK[12] = RK[4] ^
-                ((unsigned long)FSb[(RK[11]) & 0xFF]) ^
-                ((unsigned long)FSb[(RK[11] >> 8) & 0xFF] << 8) ^
-                ((unsigned long)FSb[(RK[11] >> 16) & 0xFF] << 16) ^
-                ((unsigned long)FSb[(RK[11] >> 24) & 0xFF] << 24);
+                ((uint32_t)FSb[(RK[11]) & 0xFF]) ^
+                ((uint32_t)FSb[(RK[11] >> 8) & 0xFF] << 8) ^
+                ((uint32_t)FSb[(RK[11] >> 16) & 0xFF] << 16) ^
+                ((uint32_t)FSb[(RK[11] >> 24) & 0xFF] << 24);
 
             RK[13] = RK[5] ^ RK[12];
             RK[14] = RK[6] ^ RK[13];
@@ -554,12 +554,12 @@ void tiny_aes_setkey_enc(tiny_aes_context * ctx, unsigned char *key, int keysize
 /*
  * AES key schedule (decryption)
  */
-void tiny_aes_setkey_dec(tiny_aes_context * ctx, unsigned char *key, int keysize)
+void tiny_aes_setkey_dec(tiny_aes_context * ctx, uint8_t *key, int keysize)
 {
     int i, j;
     tiny_aes_context cty;
-    unsigned long *RK;
-    unsigned long *SK;
+    uint32_t *RK;
+    uint32_t *SK;
 
     switch (keysize) {
     case 128:
@@ -652,10 +652,10 @@ void tiny_aes_setkey_dec(tiny_aes_context * ctx, unsigned char *key, int keysize
  * AES-ECB block encryption/decryption
  */
 void tiny_aes_crypt_ecb(tiny_aes_context * ctx,
-           int mode, unsigned char input[16], unsigned char output[16])
+           int mode, uint8_t input[16], uint8_t output[16])
 {
     int i;
-    unsigned long *RK, X0, X1, X2, X3, Y0, Y1, Y2, Y3;
+    uint32_t *RK, X0, X1, X2, X3, Y0, Y1, Y2, Y3;
 
     RK = ctx->rk;
 
@@ -677,28 +677,28 @@ void tiny_aes_crypt_ecb(tiny_aes_context * ctx,
         AES_RROUND(Y0, Y1, Y2, Y3, X0, X1, X2, X3);
 
         X0 = *RK++ ^
-            ((unsigned long)RSb[(Y0) & 0xFF]) ^
-            ((unsigned long)RSb[(Y3 >> 8) & 0xFF] << 8) ^
-            ((unsigned long)RSb[(Y2 >> 16) & 0xFF] << 16) ^
-            ((unsigned long)RSb[(Y1 >> 24) & 0xFF] << 24);
+            ((uint32_t)RSb[(Y0) & 0xFF]) ^
+            ((uint32_t)RSb[(Y3 >> 8) & 0xFF] << 8) ^
+            ((uint32_t)RSb[(Y2 >> 16) & 0xFF] << 16) ^
+            ((uint32_t)RSb[(Y1 >> 24) & 0xFF] << 24);
 
         X1 = *RK++ ^
-            ((unsigned long)RSb[(Y1) & 0xFF]) ^
-            ((unsigned long)RSb[(Y0 >> 8) & 0xFF] << 8) ^
-            ((unsigned long)RSb[(Y3 >> 16) & 0xFF] << 16) ^
-            ((unsigned long)RSb[(Y2 >> 24) & 0xFF] << 24);
+            ((uint32_t)RSb[(Y1) & 0xFF]) ^
+            ((uint32_t)RSb[(Y0 >> 8) & 0xFF] << 8) ^
+            ((uint32_t)RSb[(Y3 >> 16) & 0xFF] << 16) ^
+            ((uint32_t)RSb[(Y2 >> 24) & 0xFF] << 24);
 
         X2 = *RK++ ^
-            ((unsigned long)RSb[(Y2) & 0xFF]) ^
-            ((unsigned long)RSb[(Y1 >> 8) & 0xFF] << 8) ^
-            ((unsigned long)RSb[(Y0 >> 16) & 0xFF] << 16) ^
-            ((unsigned long)RSb[(Y3 >> 24) & 0xFF] << 24);
+            ((uint32_t)RSb[(Y2) & 0xFF]) ^
+            ((uint32_t)RSb[(Y1 >> 8) & 0xFF] << 8) ^
+            ((uint32_t)RSb[(Y0 >> 16) & 0xFF] << 16) ^
+            ((uint32_t)RSb[(Y3 >> 24) & 0xFF] << 24);
 
         X3 = *RK++ ^
-            ((unsigned long)RSb[(Y3) & 0xFF]) ^
-            ((unsigned long)RSb[(Y2 >> 8) & 0xFF] << 8) ^
-            ((unsigned long)RSb[(Y1 >> 16) & 0xFF] << 16) ^
-            ((unsigned long)RSb[(Y0 >> 24) & 0xFF] << 24);
+            ((uint32_t)RSb[(Y3) & 0xFF]) ^
+            ((uint32_t)RSb[(Y2 >> 8) & 0xFF] << 8) ^
+            ((uint32_t)RSb[(Y1 >> 16) & 0xFF] << 16) ^
+            ((uint32_t)RSb[(Y0 >> 24) & 0xFF] << 24);
     } else {        /* AES_ENCRYPT */
         for (i = (ctx->nr >> 1) - 1; i > 0; i--) {
             AES_FROUND(Y0, Y1, Y2, Y3, X0, X1, X2, X3);
@@ -708,28 +708,28 @@ void tiny_aes_crypt_ecb(tiny_aes_context * ctx,
         AES_FROUND(Y0, Y1, Y2, Y3, X0, X1, X2, X3);
 
         X0 = *RK++ ^
-            ((unsigned long)FSb[(Y0) & 0xFF]) ^
-            ((unsigned long)FSb[(Y1 >> 8) & 0xFF] << 8) ^
-            ((unsigned long)FSb[(Y2 >> 16) & 0xFF] << 16) ^
-            ((unsigned long)FSb[(Y3 >> 24) & 0xFF] << 24);
+            ((uint32_t)FSb[(Y0) & 0xFF]) ^
+            ((uint32_t)FSb[(Y1 >> 8) & 0xFF] << 8) ^
+            ((uint32_t)FSb[(Y2 >> 16) & 0xFF] << 16) ^
+            ((uint32_t)FSb[(Y3 >> 24) & 0xFF] << 24);
 
         X1 = *RK++ ^
-            ((unsigned long)FSb[(Y1) & 0xFF]) ^
-            ((unsigned long)FSb[(Y2 >> 8) & 0xFF] << 8) ^
-            ((unsigned long)FSb[(Y3 >> 16) & 0xFF] << 16) ^
-            ((unsigned long)FSb[(Y0 >> 24) & 0xFF] << 24);
+            ((uint32_t)FSb[(Y1) & 0xFF]) ^
+            ((uint32_t)FSb[(Y2 >> 8) & 0xFF] << 8) ^
+            ((uint32_t)FSb[(Y3 >> 16) & 0xFF] << 16) ^
+            ((uint32_t)FSb[(Y0 >> 24) & 0xFF] << 24);
 
         X2 = *RK++ ^
-            ((unsigned long)FSb[(Y2) & 0xFF]) ^
-            ((unsigned long)FSb[(Y3 >> 8) & 0xFF] << 8) ^
-            ((unsigned long)FSb[(Y0 >> 16) & 0xFF] << 16) ^
-            ((unsigned long)FSb[(Y1 >> 24) & 0xFF] << 24);
+            ((uint32_t)FSb[(Y2) & 0xFF]) ^
+            ((uint32_t)FSb[(Y3 >> 8) & 0xFF] << 8) ^
+            ((uint32_t)FSb[(Y0 >> 16) & 0xFF] << 16) ^
+            ((uint32_t)FSb[(Y1 >> 24) & 0xFF] << 24);
 
         X3 = *RK++ ^
-            ((unsigned long)FSb[(Y3) & 0xFF]) ^
-            ((unsigned long)FSb[(Y0 >> 8) & 0xFF] << 8) ^
-            ((unsigned long)FSb[(Y1 >> 16) & 0xFF] << 16) ^
-            ((unsigned long)FSb[(Y2 >> 24) & 0xFF] << 24);
+            ((uint32_t)FSb[(Y3) & 0xFF]) ^
+            ((uint32_t)FSb[(Y0 >> 8) & 0xFF] << 8) ^
+            ((uint32_t)FSb[(Y1 >> 16) & 0xFF] << 16) ^
+            ((uint32_t)FSb[(Y2 >> 24) & 0xFF] << 24);
     }
 
     PUT_ULONG_LE(X0, output, 0);
@@ -744,11 +744,11 @@ void tiny_aes_crypt_ecb(tiny_aes_context * ctx,
 void tiny_aes_crypt_cbc(tiny_aes_context * ctx,
            int mode,
            int length,
-           unsigned char iv[16],
-           unsigned char *input, unsigned char *output)
+           uint8_t iv[16],
+           uint8_t *input, uint8_t *output)
 {
     int i;
-    unsigned char temp[16];
+    uint8_t temp[16];
 
     if (mode == AES_DECRYPT) {
         while (length > 0) {
@@ -756,7 +756,7 @@ void tiny_aes_crypt_cbc(tiny_aes_context * ctx,
             tiny_aes_crypt_ecb(ctx, mode, input, output);
 
             for (i = 0; i < 16; i++)
-                output[i] = (unsigned char)(output[i] ^ iv[i]);
+                output[i] = (uint8_t)(output[i] ^ iv[i]);
 
             memcpy(iv, temp, 16);
 
@@ -767,7 +767,7 @@ void tiny_aes_crypt_cbc(tiny_aes_context * ctx,
     } else {
         while (length > 0) {
             for (i = 0; i < 16; i++)
-                output[i] = (unsigned char)(input[i] ^ iv[i]);
+                output[i] = (uint8_t)(input[i] ^ iv[i]);
 
             tiny_aes_crypt_ecb(ctx, mode, output, output);
             memcpy(iv, output, 16);
@@ -786,8 +786,8 @@ void tiny_aes_crypt_cfb128(tiny_aes_context * ctx,
               int mode,
               int length,
               int *iv_off,
-              unsigned char iv[16],
-              unsigned char *input, unsigned char *output)
+              uint8_t iv[16],
+              uint8_t *input, uint8_t *output)
 {
     int c, n = *iv_off;
 
@@ -797,8 +797,8 @@ void tiny_aes_crypt_cfb128(tiny_aes_context * ctx,
                 tiny_aes_crypt_ecb(ctx, AES_ENCRYPT, iv, iv);
 
             c = *input++;
-            *output++ = (unsigned char)(c ^ iv[n]);
-            iv[n] = (unsigned char)c;
+            *output++ = (uint8_t)(c ^ iv[n]);
+            iv[n] = (uint8_t)c;
 
             n = (n + 1) & 0x0F;
         }
@@ -807,7 +807,7 @@ void tiny_aes_crypt_cfb128(tiny_aes_context * ctx,
             if (n == 0)
                 tiny_aes_crypt_ecb(ctx, AES_ENCRYPT, iv, iv);
 
-            iv[n] = *output++ = (unsigned char)(iv[n] ^ *input++);
+            iv[n] = *output++ = (uint8_t)(iv[n] ^ *input++);
 
             n = (n + 1) & 0x0F;
         }

+ 10 - 10
src/tiny_base64.c

@@ -41,7 +41,7 @@
 
 #include "tinycrypt.h"
 
-static const unsigned char base64_enc_map[64] = {
+static const uint8_t base64_enc_map[64] = {
     'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J',
     'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T',
     'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd',
@@ -51,7 +51,7 @@ static const unsigned char base64_enc_map[64] = {
     '8', '9', '+', '/'
 };
 
-static const unsigned char base64_dec_map[128] = {
+static const uint8_t base64_dec_map[128] = {
     127, 127, 127, 127, 127, 127, 127, 127, 127, 127,
     127, 127, 127, 127, 127, 127, 127, 127, 127, 127,
     127, 127, 127, 127, 127, 127, 127, 127, 127, 127,
@@ -70,11 +70,11 @@ static const unsigned char base64_dec_map[128] = {
 /*
  * Encode a buffer into base64 format
  */
-int tiny_base64_encode(unsigned char *dst, int *dlen, unsigned char *src, int slen)
+int tiny_base64_encode(uint8_t *dst, int *dlen, uint8_t *src, int slen)
 {
     int i, n;
     int C1, C2, C3;
-    unsigned char *p;
+    uint8_t *p;
 
     if (slen == 0)
         return (0);
@@ -134,11 +134,11 @@ int tiny_base64_encode(unsigned char *dst, int *dlen, unsigned char *src, int sl
 /*
  * Decode a base64-formatted buffer
  */
-int tiny_base64_decode(unsigned char *dst, int *dlen, unsigned char *src, int slen)
+int tiny_base64_decode(uint8_t *dst, int *dlen, uint8_t *src, int slen)
 {
     int i, j, n;
-    unsigned long x;
-    unsigned char *p;
+    uint32_t x;
+    uint8_t *p;
 
     for (i = j = n = 0; i < slen; i++) {
         if ((slen - i) >= 2 && src[i] == '\r' && src[i + 1] == '\n')
@@ -179,11 +179,11 @@ int tiny_base64_decode(unsigned char *dst, int *dlen, unsigned char *src, int sl
         if (++n == 4) {
             n = 0;
             if (j > 0)
-                *p++ = (unsigned char)(x >> 16);
+                *p++ = (uint8_t)(x >> 16);
             if (j > 1)
-                *p++ = (unsigned char)(x >> 8);
+                *p++ = (uint8_t)(x >> 8);
             if (j > 2)
-                *p++ = (unsigned char)(x);
+                *p++ = (uint8_t)(x);
         }
     }
 

+ 29 - 29
src/tiny_md5.c

@@ -55,20 +55,20 @@
 #ifndef GET_ULONG_LE
 #define GET_ULONG_LE(n,b,i)                             \
     {                                                   \
-        (n) = ( (unsigned long) (b)[(i)    ]       )    \
-            | ( (unsigned long) (b)[(i) + 1] <<  8 )    \
-            | ( (unsigned long) (b)[(i) + 2] << 16 )    \
-            | ( (unsigned long) (b)[(i) + 3] << 24 );   \
+        (n) = ( (uint32_t) (b)[(i)    ]       )    \
+            | ( (uint32_t) (b)[(i) + 1] <<  8 )    \
+            | ( (uint32_t) (b)[(i) + 2] << 16 )    \
+            | ( (uint32_t) (b)[(i) + 3] << 24 );   \
     }
 #endif
 
 #ifndef PUT_ULONG_LE
 #define PUT_ULONG_LE(n,b,i)                             \
     {                                                   \
-        (b)[(i)    ] = (unsigned char) ( (n)       );   \
-        (b)[(i) + 1] = (unsigned char) ( (n) >>  8 );   \
-        (b)[(i) + 2] = (unsigned char) ( (n) >> 16 );   \
-        (b)[(i) + 3] = (unsigned char) ( (n) >> 24 );   \
+        (b)[(i)    ] = (uint8_t) ( (n)       );   \
+        (b)[(i) + 1] = (uint8_t) ( (n) >>  8 );   \
+        (b)[(i) + 2] = (uint8_t) ( (n) >> 16 );   \
+        (b)[(i) + 3] = (uint8_t) ( (n) >> 24 );   \
     }
 #endif
 
@@ -86,9 +86,9 @@ void tiny_md5_starts(tiny_md5_context * ctx)
     ctx->state[3] = 0x10325476;
 }
 
-static void md5_process(tiny_md5_context * ctx, unsigned char data[64])
+static void md5_process(tiny_md5_context * ctx, uint8_t data[64])
 {
-    unsigned long X[16], A, B, C, D;
+    uint32_t X[16], A, B, C, D;
 
     GET_ULONG_LE(X[0], data, 0);
     GET_ULONG_LE(X[1], data, 4);
@@ -212,10 +212,10 @@ static void md5_process(tiny_md5_context * ctx, unsigned char data[64])
 /*
  * MD5 process buffer
  */
-void tiny_md5_update(tiny_md5_context * ctx, unsigned char *input, int ilen)
+void tiny_md5_update(tiny_md5_context * ctx, uint8_t *input, int ilen)
 {
     int fill;
-    unsigned long left;
+    uint32_t left;
 
     if (ilen <= 0)
         return;
@@ -226,7 +226,7 @@ void tiny_md5_update(tiny_md5_context * ctx, unsigned char *input, int ilen)
     ctx->total[0] += ilen;
     ctx->total[0] &= 0xFFFFFFFF;
 
-    if (ctx->total[0] < (unsigned long)ilen)
+    if (ctx->total[0] < (uint32_t)ilen)
         ctx->total[1]++;
 
     if (left && ilen >= fill) {
@@ -248,7 +248,7 @@ void tiny_md5_update(tiny_md5_context * ctx, unsigned char *input, int ilen)
     }
 }
 
-static const unsigned char md5_padding[64] = {
+static const uint8_t md5_padding[64] = {
     0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
@@ -258,11 +258,11 @@ static const unsigned char md5_padding[64] = {
 /*
  * MD5 final digest
  */
-void tiny_md5_finish(tiny_md5_context * ctx, unsigned char output[16])
+void tiny_md5_finish(tiny_md5_context * ctx, uint8_t output[16])
 {
-    unsigned long last, padn;
-    unsigned long high, low;
-    unsigned char msglen[8];
+    uint32_t last, padn;
+    uint32_t high, low;
+    uint8_t msglen[8];
 
     high = (ctx->total[0] >> 29)
         | (ctx->total[1] << 3);
@@ -274,7 +274,7 @@ void tiny_md5_finish(tiny_md5_context * ctx, unsigned char output[16])
     last = ctx->total[0] & 0x3F;
     padn = (last < 56) ? (56 - last) : (120 - last);
 
-    tiny_md5_update(ctx, (unsigned char *)md5_padding, padn);
+    tiny_md5_update(ctx, (uint8_t *)md5_padding, padn);
     tiny_md5_update(ctx, msglen, 8);
 
     PUT_ULONG_LE(ctx->state[0], output, 0);
@@ -286,7 +286,7 @@ void tiny_md5_finish(tiny_md5_context * ctx, unsigned char output[16])
 /*
  * output = MD5( input buffer )
  */
-void tiny_md5(unsigned char *input, int ilen, unsigned char output[16])
+void tiny_md5(uint8_t *input, int ilen, uint8_t output[16])
 {
     tiny_md5_context ctx;
 
@@ -300,10 +300,10 @@ void tiny_md5(unsigned char *input, int ilen, unsigned char output[16])
 /*
  * MD5 HMAC context setup
  */
-void tiny_md5_hmac_starts(tiny_md5_context * ctx, unsigned char *key, int keylen)
+void tiny_md5_hmac_starts(tiny_md5_context * ctx, uint8_t *key, int keylen)
 {
     int i;
-    unsigned char sum[16];
+    uint8_t sum[16];
 
     if (keylen > 64) {
         tiny_md5(key, keylen, sum);
@@ -315,8 +315,8 @@ void tiny_md5_hmac_starts(tiny_md5_context * ctx, unsigned char *key, int keylen
     memset(ctx->opad, 0x5C, 64);
 
     for (i = 0; i < keylen; i++) {
-        ctx->ipad[i] = (unsigned char)(ctx->ipad[i] ^ key[i]);
-        ctx->opad[i] = (unsigned char)(ctx->opad[i] ^ key[i]);
+        ctx->ipad[i] = (uint8_t)(ctx->ipad[i] ^ key[i]);
+        ctx->opad[i] = (uint8_t)(ctx->opad[i] ^ key[i]);
     }
 
     tiny_md5_starts(ctx);
@@ -328,7 +328,7 @@ void tiny_md5_hmac_starts(tiny_md5_context * ctx, unsigned char *key, int keylen
 /*
  * MD5 HMAC process buffer
  */
-void tiny_md5_hmac_update(tiny_md5_context * ctx, unsigned char *input, int ilen)
+void tiny_md5_hmac_update(tiny_md5_context * ctx, uint8_t *input, int ilen)
 {
     tiny_md5_update(ctx, input, ilen);
 }
@@ -336,9 +336,9 @@ void tiny_md5_hmac_update(tiny_md5_context * ctx, unsigned char *input, int ilen
 /*
  * MD5 HMAC final digest
  */
-void tiny_md5_hmac_finish(tiny_md5_context * ctx, unsigned char output[16])
+void tiny_md5_hmac_finish(tiny_md5_context * ctx, uint8_t output[16])
 {
-    unsigned char tmpbuf[16];
+    uint8_t tmpbuf[16];
 
     tiny_md5_finish(ctx, tmpbuf);
     tiny_md5_starts(ctx);
@@ -352,8 +352,8 @@ void tiny_md5_hmac_finish(tiny_md5_context * ctx, unsigned char output[16])
 /*
  * output = HMAC-MD5( hmac key, input buffer )
  */
-void tiny_md5_hmac(unsigned char *key, int keylen, unsigned char *input, int ilen,
-          unsigned char output[16])
+void tiny_md5_hmac(uint8_t *key, int keylen, uint8_t *input, int ilen,
+          uint8_t output[16])
 {
     tiny_md5_context ctx;
 

+ 29 - 29
src/tiny_sha1.c

@@ -55,20 +55,20 @@
 #ifndef GET_ULONG_BE
 #define GET_ULONG_BE(n,b,i)                             \
     {                                                   \
-        (n) = ( (unsigned long) (b)[(i)    ] << 24 )    \
-            | ( (unsigned long) (b)[(i) + 1] << 16 )    \
-            | ( (unsigned long) (b)[(i) + 2] <<  8 )    \
-            | ( (unsigned long) (b)[(i) + 3]       );   \
+        (n) = ( (uint32_t) (b)[(i)    ] << 24 )    \
+            | ( (uint32_t) (b)[(i) + 1] << 16 )    \
+            | ( (uint32_t) (b)[(i) + 2] <<  8 )    \
+            | ( (uint32_t) (b)[(i) + 3]       );   \
     }
 #endif
 
 #ifndef PUT_ULONG_BE
 #define PUT_ULONG_BE(n,b,i)                             \
     {                                                   \
-        (b)[(i)    ] = (unsigned char) ( (n) >> 24 );   \
-        (b)[(i) + 1] = (unsigned char) ( (n) >> 16 );   \
-        (b)[(i) + 2] = (unsigned char) ( (n) >>  8 );   \
-        (b)[(i) + 3] = (unsigned char) ( (n)       );   \
+        (b)[(i)    ] = (uint8_t) ( (n) >> 24 );   \
+        (b)[(i) + 1] = (uint8_t) ( (n) >> 16 );   \
+        (b)[(i) + 2] = (uint8_t) ( (n) >>  8 );   \
+        (b)[(i) + 3] = (uint8_t) ( (n)       );   \
     }
 #endif
 
@@ -87,9 +87,9 @@ void tiny_sha1_starts(tiny_sha1_context * ctx)
     ctx->state[4] = 0xC3D2E1F0;
 }
 
-static void sha1_process(tiny_sha1_context * ctx, unsigned char data[64])
+static void sha1_process(tiny_sha1_context * ctx, uint8_t data[64])
 {
-    unsigned long temp, W[16], A, B, C, D, E;
+    uint32_t temp, W[16], A, B, C, D, E;
 
     GET_ULONG_BE(W[0], data, 0);
     GET_ULONG_BE(W[1], data, 4);
@@ -246,10 +246,10 @@ static void sha1_process(tiny_sha1_context * ctx, unsigned char data[64])
 /*
  * SHA-1 process buffer
  */
-void tiny_sha1_update(tiny_sha1_context * ctx, unsigned char *input, int ilen)
+void tiny_sha1_update(tiny_sha1_context * ctx, uint8_t *input, int ilen)
 {
     int fill;
-    unsigned long left;
+    uint32_t left;
 
     if (ilen <= 0)
         return;
@@ -260,7 +260,7 @@ void tiny_sha1_update(tiny_sha1_context * ctx, unsigned char *input, int ilen)
     ctx->total[0] += ilen;
     ctx->total[0] &= 0xFFFFFFFF;
 
-    if (ctx->total[0] < (unsigned long)ilen)
+    if (ctx->total[0] < (uint32_t)ilen)
         ctx->total[1]++;
 
     if (left && ilen >= fill) {
@@ -282,7 +282,7 @@ void tiny_sha1_update(tiny_sha1_context * ctx, unsigned char *input, int ilen)
     }
 }
 
-static const unsigned char sha1_padding[64] = {
+static const uint8_t sha1_padding[64] = {
     0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
@@ -292,11 +292,11 @@ static const unsigned char sha1_padding[64] = {
 /*
  * SHA-1 final digest
  */
-void tiny_sha1_finish(tiny_sha1_context * ctx, unsigned char output[20])
+void tiny_sha1_finish(tiny_sha1_context * ctx, uint8_t output[20])
 {
-    unsigned long last, padn;
-    unsigned long high, low;
-    unsigned char msglen[8];
+    uint32_t last, padn;
+    uint32_t high, low;
+    uint8_t msglen[8];
 
     high = (ctx->total[0] >> 29)
         | (ctx->total[1] << 3);
@@ -308,7 +308,7 @@ void tiny_sha1_finish(tiny_sha1_context * ctx, unsigned char output[20])
     last = ctx->total[0] & 0x3F;
     padn = (last < 56) ? (56 - last) : (120 - last);
 
-    tiny_sha1_update(ctx, (unsigned char *)sha1_padding, padn);
+    tiny_sha1_update(ctx, (uint8_t *)sha1_padding, padn);
     tiny_sha1_update(ctx, msglen, 8);
 
     PUT_ULONG_BE(ctx->state[0], output, 0);
@@ -321,7 +321,7 @@ void tiny_sha1_finish(tiny_sha1_context * ctx, unsigned char output[20])
 /*
  * output = SHA-1( input buffer )
  */
-void tiny_sha1(unsigned char *input, int ilen, unsigned char output[20])
+void tiny_sha1(uint8_t *input, int ilen, uint8_t output[20])
 {
     tiny_sha1_context ctx;
 
@@ -335,10 +335,10 @@ void tiny_sha1(unsigned char *input, int ilen, unsigned char output[20])
 /*
  * SHA-1 HMAC context setup
  */
-void tiny_sha1_hmac_starts(tiny_sha1_context * ctx, unsigned char *key, int keylen)
+void tiny_sha1_hmac_starts(tiny_sha1_context * ctx, uint8_t *key, int keylen)
 {
     int i;
-    unsigned char sum[20];
+    uint8_t sum[20];
 
     if (keylen > 64) {
         tiny_sha1(key, keylen, sum);
@@ -350,8 +350,8 @@ void tiny_sha1_hmac_starts(tiny_sha1_context * ctx, unsigned char *key, int keyl
     memset(ctx->opad, 0x5C, 64);
 
     for (i = 0; i < keylen; i++) {
-        ctx->ipad[i] = (unsigned char)(ctx->ipad[i] ^ key[i]);
-        ctx->opad[i] = (unsigned char)(ctx->opad[i] ^ key[i]);
+        ctx->ipad[i] = (uint8_t)(ctx->ipad[i] ^ key[i]);
+        ctx->opad[i] = (uint8_t)(ctx->opad[i] ^ key[i]);
     }
 
     tiny_sha1_starts(ctx);
@@ -363,7 +363,7 @@ void tiny_sha1_hmac_starts(tiny_sha1_context * ctx, unsigned char *key, int keyl
 /*
  * SHA-1 HMAC process buffer
  */
-void tiny_sha1_hmac_update(tiny_sha1_context * ctx, unsigned char *input, int ilen)
+void tiny_sha1_hmac_update(tiny_sha1_context * ctx, uint8_t *input, int ilen)
 {
     tiny_sha1_update(ctx, input, ilen);
 }
@@ -371,9 +371,9 @@ void tiny_sha1_hmac_update(tiny_sha1_context * ctx, unsigned char *input, int il
 /*
  * SHA-1 HMAC final digest
  */
-void tiny_sha1_hmac_finish(tiny_sha1_context * ctx, unsigned char output[20])
+void tiny_sha1_hmac_finish(tiny_sha1_context * ctx, uint8_t output[20])
 {
-    unsigned char tmpbuf[20];
+    uint8_t tmpbuf[20];
 
     tiny_sha1_finish(ctx, tmpbuf);
     tiny_sha1_starts(ctx);
@@ -387,8 +387,8 @@ void tiny_sha1_hmac_finish(tiny_sha1_context * ctx, unsigned char output[20])
 /*
  * output = HMAC-SHA-1( hmac key, input buffer )
  */
-void tiny_sha1_hmac(unsigned char *key, int keylen,
-           unsigned char *input, int ilen, unsigned char output[20])
+void tiny_sha1_hmac(uint8_t *key, int keylen,
+           uint8_t *input, int ilen, uint8_t output[20])
 {
     tiny_sha1_context ctx;
 

+ 31 - 31
src/tiny_sha2.c

@@ -55,20 +55,20 @@
 #ifndef GET_ULONG_BE
 #define GET_ULONG_BE(n,b,i)                             \
     {                                                   \
-        (n) = ( (unsigned long) (b)[(i)    ] << 24 )    \
-            | ( (unsigned long) (b)[(i) + 1] << 16 )    \
-            | ( (unsigned long) (b)[(i) + 2] <<  8 )    \
-            | ( (unsigned long) (b)[(i) + 3]       );   \
+        (n) = ( (uint32_t) (b)[(i)    ] << 24 )    \
+            | ( (uint32_t) (b)[(i) + 1] << 16 )    \
+            | ( (uint32_t) (b)[(i) + 2] <<  8 )    \
+            | ( (uint32_t) (b)[(i) + 3]       );   \
     }
 #endif
 
 #ifndef PUT_ULONG_BE
 #define PUT_ULONG_BE(n,b,i)                             \
     {                                                   \
-        (b)[(i)    ] = (unsigned char) ( (n) >> 24 );   \
-        (b)[(i) + 1] = (unsigned char) ( (n) >> 16 );   \
-        (b)[(i) + 2] = (unsigned char) ( (n) >>  8 );   \
-        (b)[(i) + 3] = (unsigned char) ( (n)       );   \
+        (b)[(i)    ] = (uint8_t) ( (n) >> 24 );   \
+        (b)[(i) + 1] = (uint8_t) ( (n) >> 16 );   \
+        (b)[(i) + 2] = (uint8_t) ( (n) >>  8 );   \
+        (b)[(i) + 3] = (uint8_t) ( (n)       );   \
     }
 #endif
 
@@ -105,10 +105,10 @@ void tiny_sha2_starts(tiny_sha2_context * ctx, int is224)
     ctx->is224 = is224;
 }
 
-static void sha2_process(tiny_sha2_context * ctx, unsigned char data[64])
+static void sha2_process(tiny_sha2_context * ctx, uint8_t data[64])
 {
-    unsigned long temp1, temp2, W[64];
-    unsigned long A, B, C, D, E, F, G, H;
+    uint32_t temp1, temp2, W[64];
+    uint32_t A, B, C, D, E, F, G, H;
 
     GET_ULONG_BE(W[0], data, 0);
     GET_ULONG_BE(W[1], data, 4);
@@ -239,10 +239,10 @@ static void sha2_process(tiny_sha2_context * ctx, unsigned char data[64])
 /*
  * SHA-256 process buffer
  */
-void tiny_sha2_update(tiny_sha2_context * ctx, unsigned char *input, int ilen)
+void tiny_sha2_update(tiny_sha2_context * ctx, uint8_t *input, int ilen)
 {
     int fill;
-    unsigned long left;
+    uint32_t left;
 
     if (ilen <= 0)
         return;
@@ -253,7 +253,7 @@ void tiny_sha2_update(tiny_sha2_context * ctx, unsigned char *input, int ilen)
     ctx->total[0] += ilen;
     ctx->total[0] &= 0xFFFFFFFF;
 
-    if (ctx->total[0] < (unsigned long)ilen)
+    if (ctx->total[0] < (uint32_t)ilen)
         ctx->total[1]++;
 
     if (left && ilen >= fill) {
@@ -275,7 +275,7 @@ void tiny_sha2_update(tiny_sha2_context * ctx, unsigned char *input, int ilen)
     }
 }
 
-static const unsigned char sha2_padding[64] = {
+static const uint8_t sha2_padding[64] = {
     0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
@@ -285,11 +285,11 @@ static const unsigned char sha2_padding[64] = {
 /*
  * SHA-256 final digest
  */
-void tiny_sha2_finish(tiny_sha2_context * ctx, unsigned char output[32])
+void tiny_sha2_finish(tiny_sha2_context * ctx, uint8_t output[32])
 {
-    unsigned long last, padn;
-    unsigned long high, low;
-    unsigned char msglen[8];
+    uint32_t last, padn;
+    uint32_t high, low;
+    uint8_t msglen[8];
 
     high = (ctx->total[0] >> 29)
         | (ctx->total[1] << 3);
@@ -301,7 +301,7 @@ void tiny_sha2_finish(tiny_sha2_context * ctx, unsigned char output[32])
     last = ctx->total[0] & 0x3F;
     padn = (last < 56) ? (56 - last) : (120 - last);
 
-    tiny_sha2_update(ctx, (unsigned char *)sha2_padding, padn);
+    tiny_sha2_update(ctx, (uint8_t *)sha2_padding, padn);
     tiny_sha2_update(ctx, msglen, 8);
 
     PUT_ULONG_BE(ctx->state[0], output, 0);
@@ -319,7 +319,7 @@ void tiny_sha2_finish(tiny_sha2_context * ctx, unsigned char output[32])
 /*
  * output = SHA-256( input buffer )
  */
-void tiny_sha2(unsigned char *input, int ilen, unsigned char output[32], int is224)
+void tiny_sha2(uint8_t *input, int ilen, uint8_t output[32], int is224)
 {
     tiny_sha2_context ctx;
 
@@ -333,11 +333,11 @@ void tiny_sha2(unsigned char *input, int ilen, unsigned char output[32], int is2
 /*
  * SHA-256 HMAC context setup
  */
-void tiny_sha2_hmac_starts(tiny_sha2_context * ctx, unsigned char *key, int keylen,
+void tiny_sha2_hmac_starts(tiny_sha2_context * ctx, uint8_t *key, int keylen,
               int is224)
 {
     int i;
-    unsigned char sum[32];
+    uint8_t sum[32];
 
     if (keylen > 64) {
         tiny_sha2(key, keylen, sum, is224);
@@ -349,8 +349,8 @@ void tiny_sha2_hmac_starts(tiny_sha2_context * ctx, unsigned char *key, int keyl
     memset(ctx->opad, 0x5C, 64);
 
     for (i = 0; i < keylen; i++) {
-        ctx->ipad[i] = (unsigned char)(ctx->ipad[i] ^ key[i]);
-        ctx->opad[i] = (unsigned char)(ctx->opad[i] ^ key[i]);
+        ctx->ipad[i] = (uint8_t)(ctx->ipad[i] ^ key[i]);
+        ctx->opad[i] = (uint8_t)(ctx->opad[i] ^ key[i]);
     }
 
     tiny_sha2_starts(ctx, is224);
@@ -362,7 +362,7 @@ void tiny_sha2_hmac_starts(tiny_sha2_context * ctx, unsigned char *key, int keyl
 /*
  * SHA-256 HMAC process buffer
  */
-void tiny_sha2_hmac_update(tiny_sha2_context * ctx, unsigned char *input, int ilen)
+void tiny_sha2_hmac_update(tiny_sha2_context * ctx, uint8_t *input, int ilen)
 {
     tiny_sha2_update(ctx, input, ilen);
 }
@@ -370,10 +370,10 @@ void tiny_sha2_hmac_update(tiny_sha2_context * ctx, unsigned char *input, int il
 /*
  * SHA-256 HMAC final digest
  */
-void tiny_sha2_hmac_finish(tiny_sha2_context * ctx, unsigned char output[32])
+void tiny_sha2_hmac_finish(tiny_sha2_context * ctx, uint8_t output[32])
 {
     int is224, hlen;
-    unsigned char tmpbuf[32];
+    uint8_t tmpbuf[32];
 
     is224 = ctx->is224;
     hlen = (is224 == 0) ? 32 : 28;
@@ -390,9 +390,9 @@ void tiny_sha2_hmac_finish(tiny_sha2_context * ctx, unsigned char output[32])
 /*
  * output = HMAC-SHA-256( hmac key, input buffer )
  */
-void tiny_sha2_hmac(unsigned char *key, int keylen,
-           unsigned char *input, int ilen,
-           unsigned char output[32], int is224)
+void tiny_sha2_hmac(uint8_t *key, int keylen,
+           uint8_t *input, int ilen,
+           uint8_t output[32], int is224)
 {
     tiny_sha2_context ctx;