Explicit conversions and minor changes to prevent MSVC compiler warnings
diff --git a/library/asn1write.c b/library/asn1write.c
index d4c1d8d..32d1c73 100644
--- a/library/asn1write.c
+++ b/library/asn1write.c
@@ -44,7 +44,7 @@
         if( *p - start < 1 )
             return( POLARSSL_ERR_ASN1_BUF_TOO_SMALL );
 
-        *--(*p) = len;
+        *--(*p) = (unsigned char) len;
         return( 1 );
     }
 
@@ -53,7 +53,7 @@
         if( *p - start < 2 )
             return( POLARSSL_ERR_ASN1_BUF_TOO_SMALL );
 
-        *--(*p) = len;
+        *--(*p) = (unsigned char) len;
         *--(*p) = 0x81;
         return( 2 );
     }
@@ -92,7 +92,7 @@
     (*p) -= len;
     memcpy( *p, buf, len );
 
-    return( len );
+    return( (int) len );
 }
 
 #if defined(POLARSSL_BIGNUM_C)
@@ -126,7 +126,7 @@
     ASN1_CHK_ADD( len, asn1_write_len( p, start, len ) );
     ASN1_CHK_ADD( len, asn1_write_tag( p, start, ASN1_INTEGER ) );
 
-    return( len );
+    return( (int) len );
 }
 #endif /* POLARSSL_BIGNUM_C */
 
@@ -140,7 +140,7 @@
     ASN1_CHK_ADD( len, asn1_write_len( p, start, 0) );
     ASN1_CHK_ADD( len, asn1_write_tag( p, start, ASN1_NULL ) );
 
-    return( len );
+    return( (int) len );
 }
 
 int asn1_write_oid( unsigned char **p, unsigned char *start,
@@ -154,7 +154,7 @@
     ASN1_CHK_ADD( len , asn1_write_len( p, start, len ) );
     ASN1_CHK_ADD( len , asn1_write_tag( p, start, ASN1_OID ) );
 
-    return( len );
+    return( (int) len );
 }
 
 int asn1_write_algorithm_identifier( unsigned char **p, unsigned char *start,
@@ -175,7 +175,7 @@
     ASN1_CHK_ADD( len, asn1_write_tag( p, start,
                                        ASN1_CONSTRUCTED | ASN1_SEQUENCE ) );
 
-    return( len );
+    return( (int) len );
 }
 
 int asn1_write_bool( unsigned char **p, unsigned char *start, int boolean )
@@ -192,7 +192,7 @@
     ASN1_CHK_ADD( len, asn1_write_len( p, start, len ) );
     ASN1_CHK_ADD( len, asn1_write_tag( p, start, ASN1_BOOLEAN ) );
 
-    return( len );
+    return( (int) len );
 }
 
 int asn1_write_int( unsigned char **p, unsigned char *start, int val )
@@ -222,7 +222,7 @@
     ASN1_CHK_ADD( len, asn1_write_len( p, start, len ) );
     ASN1_CHK_ADD( len, asn1_write_tag( p, start, ASN1_INTEGER ) );
 
-    return( len );
+    return( (int) len );
 }
 
 int asn1_write_printable_string( unsigned char **p, unsigned char *start,
@@ -237,7 +237,7 @@
     ASN1_CHK_ADD( len, asn1_write_len( p, start, len ) );
     ASN1_CHK_ADD( len, asn1_write_tag( p, start, ASN1_PRINTABLE_STRING ) );
 
-    return( len );
+    return( (int) len );
 }
 
 int asn1_write_ia5_string( unsigned char **p, unsigned char *start,
@@ -252,7 +252,7 @@
     ASN1_CHK_ADD( len, asn1_write_len( p, start, len ) );
     ASN1_CHK_ADD( len, asn1_write_tag( p, start, ASN1_IA5_STRING ) );
 
-    return( len );
+    return( (int) len );
 }
 
 int asn1_write_bitstring( unsigned char **p, unsigned char *start,
@@ -274,12 +274,12 @@
 
     // Write unused bits
     //
-    *--(*p) = size * 8 - bits;
+    *--(*p) = (unsigned char) (size * 8 - bits);
 
     ASN1_CHK_ADD( len, asn1_write_len( p, start, len ) );
     ASN1_CHK_ADD( len, asn1_write_tag( p, start, ASN1_BIT_STRING ) );
 
-    return( len );
+    return( (int) len );
 }
 
 int asn1_write_octet_string( unsigned char **p, unsigned char *start,
@@ -293,7 +293,7 @@
     ASN1_CHK_ADD( len, asn1_write_len( p, start, len ) );
     ASN1_CHK_ADD( len, asn1_write_tag( p, start, ASN1_OCTET_STRING ) );
 
-    return( len );
+    return( (int) len );
 }
 
 asn1_named_data *asn1_store_named_data( asn1_named_data **head,
diff --git a/library/base64.c b/library/base64.c
index 4b823b2..e9527db 100644
--- a/library/base64.c
+++ b/library/base64.c
@@ -137,7 +137,7 @@
     uint32_t j, x;
     unsigned char *p;
 
-    for( i = j = n = 0; i < slen; i++ )
+    for( i = n = j = 0; i < slen; i++ )
     {
         if( ( slen - i ) >= 2 &&
             src[i] == '\r' && src[i + 1] == '\n' )
diff --git a/library/cipher.c b/library/cipher.c
index d14781b..5edc39a 100644
--- a/library/cipher.c
+++ b/library/cipher.c
@@ -216,7 +216,7 @@
 #if defined(POLARSSL_GCM_C)
     if( POLARSSL_MODE_GCM == ctx->cipher_info->mode )
     {
-        return gcm_starts( ctx->cipher_ctx, ctx->operation,
+        return gcm_starts( (gcm_context *) ctx->cipher_ctx, ctx->operation,
                            ctx->iv, ctx->iv_size, ad, ad_len );
     }
 #endif
@@ -257,7 +257,8 @@
     if( ctx->cipher_info->mode == POLARSSL_MODE_GCM )
     {
         *olen = ilen;
-        return gcm_update( ctx->cipher_ctx, ilen, input, output );
+        return gcm_update( (gcm_context *) ctx->cipher_ctx, ilen, input,
+                           output );
     }
 #endif
 
@@ -414,7 +415,7 @@
 static int get_pkcs_padding( unsigned char *input, size_t input_len,
         size_t *data_len )
 {
-    unsigned int i, padding_len = 0;
+    size_t i, padding_len = 0;
 
     if( NULL == input || NULL == data_len )
         return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
@@ -487,7 +488,7 @@
 static int get_zeros_and_len_padding( unsigned char *input, size_t input_len,
                                       size_t *data_len )
 {
-    unsigned int i, padding_len = 0;
+    size_t i, padding_len = 0;
 
     if( NULL == input || NULL == data_len )
         return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
@@ -514,7 +515,7 @@
 static void add_zeros_padding( unsigned char *output,
                                size_t output_len, size_t data_len )
 {
-    unsigned char i;
+    size_t i;
 
     for( i = data_len; i < output_len; i++ )
         output[i] = 0x00;
@@ -693,7 +694,7 @@
 
 #if defined(POLARSSL_GCM_C)
     if( POLARSSL_MODE_GCM == ctx->cipher_info->mode )
-        return gcm_finish( ctx->cipher_ctx, tag, tag_len );
+        return gcm_finish( (gcm_context *) ctx->cipher_ctx, tag, tag_len );
 #endif
 
     return 0;
@@ -720,8 +721,11 @@
         if( tag_len > sizeof( check_tag ) )
             return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
 
-        if( 0 != ( ret = gcm_finish( ctx->cipher_ctx, check_tag, tag_len ) ) )
+        if( 0 != ( ret = gcm_finish( (gcm_context *) ctx->cipher_ctx,
+                                     check_tag, tag_len ) ) )
+        {
             return( ret );
+        }
 
         /* Check the tag in "constant-time" */
         for( diff = 0, i = 0; i < tag_len; i++ )
diff --git a/library/ctr_drbg.c b/library/ctr_drbg.c
index 54fb791..53b8b54 100644
--- a/library/ctr_drbg.c
+++ b/library/ctr_drbg.c
@@ -108,7 +108,8 @@
     unsigned char *p = buf, *iv;
     aes_context aes_ctx;
 
-    int i, j, buf_len, use_len;
+    int i, j;
+    size_t buf_len, use_len;
 
     memset( buf, 0, CTR_DRBG_MAX_SEED_INPUT + CTR_DRBG_BLOCKSIZE + 16 );
 
@@ -150,11 +151,12 @@
             for( i = 0; i < CTR_DRBG_BLOCKSIZE; i++ )
                 chain[i] ^= p[i];
             p += CTR_DRBG_BLOCKSIZE;
-            use_len -= CTR_DRBG_BLOCKSIZE;
+            use_len -= ( use_len >= CTR_DRBG_BLOCKSIZE ) ?
+                       CTR_DRBG_BLOCKSIZE : use_len;
 
             aes_crypt_ecb( &aes_ctx, AES_ENCRYPT, chain, chain );
         }
-        
+
         memcpy( tmp + j, chain, CTR_DRBG_BLOCKSIZE );
 
         /*
diff --git a/library/ecp.c b/library/ecp.c
index b7af16a..d3880be 100644
--- a/library/ecp.c
+++ b/library/ecp.c
@@ -442,7 +442,7 @@
     /*
      * write length to the first byte and update total length
      */
-    buf[0] = *olen;
+    buf[0] = (unsigned char) *olen;
     ++*olen;
 
     return 0;
@@ -1427,7 +1427,7 @@
 {
     int ret;
     unsigned char w, m_is_odd, p_eq_g;
-    size_t pre_len, naf_len, i, j;
+    size_t pre_len = 1, naf_len, i, j;
     signed char naf[ MAX_NAF_LEN ];
     ecp_point Q, *T = NULL, S[2];
     mpi M;
@@ -1469,7 +1469,7 @@
     if( w < 2 || w >= grp->nbits )
         w = 2;
 
-    pre_len = 1 << ( w - 1 );
+    pre_len <<= ( w - 1 );
     naf_len = grp->nbits / w + 1;
 
     /*
@@ -1478,7 +1478,8 @@
      */
     if( ! p_eq_g || grp->T == NULL )
     {
-        if( ( T = polarssl_malloc( pre_len * sizeof( ecp_point ) ) ) == NULL )
+        T = (ecp_point *) polarssl_malloc( pre_len * sizeof( ecp_point ) );
+        if( T == NULL )
         {
             ret = POLARSSL_ERR_ECP_MALLOC_FAILED;
             goto cleanup;
diff --git a/library/entropy_poll.c b/library/entropy_poll.c
index b5d9f78..eec8ec4 100644
--- a/library/entropy_poll.c
+++ b/library/entropy_poll.c
@@ -59,7 +59,7 @@
         return POLARSSL_ERR_ENTROPY_SOURCE_FAILED;
     }
 
-    if( CryptGenRandom( provider, len, output ) == FALSE )
+    if( CryptGenRandom( provider, (DWORD) len, output ) == FALSE )
         return POLARSSL_ERR_ENTROPY_SOURCE_FAILED;
 
     CryptReleaseContext( provider, 0 );
diff --git a/library/error.c b/library/error.c
index 47bfcc4..ff6fb07 100644
--- a/library/error.c
+++ b/library/error.c
@@ -169,8 +169,13 @@
     size_t len;
     int use_ret;
 
+    if( buflen == 0 )
+        return;
+
     memset( buf, 0x00, buflen );
-     
+    /* Reduce buflen to make sure MSVC _snprintf() ends with \0 as well */
+    buflen -= 1;
+
     if( ret < 0 )
         ret = -ret;
 
diff --git a/library/pk_wrap.c b/library/pk_wrap.c
index a26bc08..6f22b09 100644
--- a/library/pk_wrap.c
+++ b/library/pk_wrap.c
@@ -68,7 +68,7 @@
         return( POLARSSL_ERR_RSA_VERIFY_FAILED );
 
     return( rsa_pkcs1_verify( (rsa_context *) ctx, NULL, NULL,
-                RSA_PUBLIC, md_alg, hash_len, hash, sig ) );
+                RSA_PUBLIC, md_alg, (unsigned int) hash_len, hash, sig ) );
 }
 
 static int rsa_sign_wrap( void *ctx, md_type_t md_alg,
@@ -79,7 +79,7 @@
     *sig_len = ((rsa_context *) ctx)->len;
 
     return( rsa_pkcs1_sign( (rsa_context *) ctx, f_rng, p_rng, RSA_PRIVATE,
-                md_alg, hash_len, hash, sig ) );
+                md_alg, (unsigned int) hash_len, hash, sig ) );
 }
 
 static int rsa_decrypt_wrap( void *ctx,
@@ -361,7 +361,7 @@
     *sig_len = rsa_alt->key_len_func( rsa_alt->key );
 
     return( rsa_alt->sign_func( rsa_alt->key, f_rng, p_rng, RSA_PRIVATE,
-                md_alg, hash_len, hash, sig ) );
+                md_alg, (unsigned int) hash_len, hash, sig ) );
 }
 
 static int rsa_alt_decrypt_wrap( void *ctx,
diff --git a/library/pkcs12.c b/library/pkcs12.c
index 9ef557c..16821b0 100644
--- a/library/pkcs12.c
+++ b/library/pkcs12.c
@@ -225,7 +225,7 @@
                        const unsigned char *salt, size_t saltlen,
                        md_type_t md_type, int id, int iterations )
 {
-    int ret, i;
+    int ret;
     unsigned int j;
 
     unsigned char diversifier[128];
@@ -234,7 +234,7 @@
     unsigned char *p;
     unsigned char c;
 
-    size_t hlen, use_len, v;
+    size_t hlen, use_len, v, i;
 
     const md_info_t *md_info;
     md_context_t md_ctx;
@@ -281,7 +281,7 @@
             goto exit;
 
         // Perform remaining ( iterations - 1 ) recursive hash calculations
-        for( i = 1; i < iterations; i++ )
+        for( i = 1; i < (size_t) iterations; i++ )
         {
             if( ( ret = md( md_info, hash_output, hlen, hash_output ) ) != 0 )
                 goto exit;
diff --git a/library/pkcs5.c b/library/pkcs5.c
index c2aa06d..39aa5b9 100644
--- a/library/pkcs5.c
+++ b/library/pkcs5.c
@@ -269,7 +269,7 @@
         use_len = ( key_length < md_size ) ? key_length : md_size;
         memcpy( out_p, work, use_len );
 
-        key_length -= use_len;
+        key_length -= (uint32_t) use_len;
         out_p += use_len;
 
         for( i = 4; i > 0; i-- )
diff --git a/library/pkwrite.c b/library/pkwrite.c
index fcebd48..8b6d735 100644
--- a/library/pkwrite.c
+++ b/library/pkwrite.c
@@ -71,7 +71,7 @@
     ASN1_CHK_ADD( len, asn1_write_len( p, start, len ) );
     ASN1_CHK_ADD( len, asn1_write_tag( p, start, ASN1_CONSTRUCTED | ASN1_SEQUENCE ) );
 
-    return( len );
+    return( (int) len );
 }
 #endif /* POLARSSL_RSA_C */
 
@@ -99,7 +99,7 @@
     *p -= len;
     memcpy( *p, buf, len );
 
-    return( len );
+    return( (int) len );
 }
 
 /*
@@ -120,7 +120,7 @@
 
     ASN1_CHK_ADD( len, asn1_write_oid( p, start, oid, oid_len ) );
 
-    return( len );
+    return( (int) len );
 }
 #endif /* POLARSSL_ECP_C */
 
@@ -142,7 +142,7 @@
 #endif
         return( POLARSSL_ERR_PK_FEATURE_UNAVAILABLE );
 
-    return( len );
+    return( (int) len );
 }
 
 int pk_write_pubkey_der( pk_context *key, unsigned char *buf, size_t size )
@@ -189,7 +189,7 @@
     ASN1_CHK_ADD( len, asn1_write_len( &c, buf, len ) );
     ASN1_CHK_ADD( len, asn1_write_tag( &c, buf, ASN1_CONSTRUCTED | ASN1_SEQUENCE ) );
 
-    return( len );
+    return( (int) len );
 }
 
 int pk_write_key_der( pk_context *key, unsigned char *buf, size_t size )
@@ -273,7 +273,7 @@
 #endif
         return( POLARSSL_ERR_PK_FEATURE_UNAVAILABLE );
 
-    return( len );
+    return( (int) len );
 }
 
 #if defined(POLARSSL_PEM_WRITE_C)
diff --git a/library/rsa.c b/library/rsa.c
index 2713b5c..210ea46 100644
--- a/library/rsa.c
+++ b/library/rsa.c
@@ -1018,11 +1018,11 @@
          * Digest ::= OCTET STRING
          */
         *p++ = ASN1_SEQUENCE | ASN1_CONSTRUCTED;
-        *p++ = 0x08 + oid_size + hashlen;
+        *p++ = (unsigned char) ( 0x08 + oid_size + hashlen );
         *p++ = ASN1_SEQUENCE | ASN1_CONSTRUCTED;
-        *p++ = 0x04 + oid_size;
+        *p++ = (unsigned char) ( 0x04 + oid_size );
         *p++ = ASN1_OID;
-        *p++ = oid_size;
+        *p++ = oid_size & 0xFF;
         memcpy( p, oid, oid_size );
         p += oid_size;
         *p++ = ASN1_NULL;
diff --git a/library/ssl_cli.c b/library/ssl_cli.c
index 81d8e88..1db11ba 100644
--- a/library/ssl_cli.c
+++ b/library/ssl_cli.c
@@ -1714,7 +1714,7 @@
         i = 6;
 
         ret = dhm_make_public( &ssl->handshake->dhm_ctx,
-                                mpi_size( &ssl->handshake->dhm_ctx.P ),
+                                (int) mpi_size( &ssl->handshake->dhm_ctx.P ),
                                &ssl->out_msg[i], n,
                                 ssl->f_rng, ssl->p_rng );
         if( ret != 0 )
@@ -1845,7 +1845,7 @@
         ssl->out_msg[7 + ssl->psk_identity_len] = (unsigned char)( n      );
 
         ret = dhm_make_public( &ssl->handshake->dhm_ctx,
-                                mpi_size( &ssl->handshake->dhm_ctx.P ),
+                                (int) mpi_size( &ssl->handshake->dhm_ctx.P ),
                                &ssl->out_msg[8 + ssl->psk_identity_len], n,
                                 ssl->f_rng, ssl->p_rng );
         if( ret != 0 )
diff --git a/library/ssl_srv.c b/library/ssl_srv.c
index aa75430..d4f56fa 100644
--- a/library/ssl_srv.c
+++ b/library/ssl_srv.c
@@ -1568,8 +1568,8 @@
 #if defined(POLARSSL_HAVE_TIME)
     time_t t;
 #endif
-    int ret, n;
-    size_t olen, ext_len = 0;
+    int ret;
+    size_t olen, ext_len = 0, n;
     unsigned char *buf, *p;
 
     SSL_DEBUG_MSG( 2, ( "=> write server hello" ) );
@@ -1813,7 +1813,7 @@
     p[1 + ct_len++] = SSL_CERT_TYPE_ECDSA_SIGN;
 #endif
 
-    p[0] = ct_len++;
+    p[0] = (unsigned char) ct_len++;
     p += ct_len;
 
     sa_len = 0;
@@ -1969,7 +1969,7 @@
         }
 
         if( ( ret = dhm_make_params( &ssl->handshake->dhm_ctx,
-                                      mpi_size( &ssl->handshake->dhm_ctx.P ),
+                                      (int) mpi_size( &ssl->handshake->dhm_ctx.P ),
                                       p,
                                       &len, ssl->f_rng, ssl->p_rng ) ) != 0 )
         {
diff --git a/library/ssl_tls.c b/library/ssl_tls.c
index 3f3bf11..065693a 100644
--- a/library/ssl_tls.c
+++ b/library/ssl_tls.c
@@ -80,7 +80,8 @@
     {
         int ret;
 
-        if( ( dst->peer_cert = polarssl_malloc( sizeof(x509_crt) ) ) == NULL )
+        dst->peer_cert = (x509_crt *) polarssl_malloc( sizeof(x509_crt) );
+        if( dst->peer_cert == NULL )
             return( POLARSSL_ERR_SSL_MALLOC_FAILED );
 
         x509_crt_init( dst->peer_cert );
@@ -98,7 +99,8 @@
 #if defined(POLARSSL_SSL_SESSION_TICKETS)
     if( src->ticket != NULL )
     {
-        if( ( dst->ticket = polarssl_malloc( src->ticket_len ) ) == NULL )
+        dst->ticket = (unsigned char *) polarssl_malloc( src->ticket_len );
+        if( dst->ticket == NULL )
             return( POLARSSL_ERR_SSL_MALLOC_FAILED );
 
         memcpy( dst->ticket, src->ticket, src->ticket_len );
@@ -149,7 +151,7 @@
      */
     for( i = 0; i < dlen / 16; i++ )
     {
-        memset( padding, 'A' + i, 1 + i );
+        memset( padding, (unsigned char) ('A' + i), 1 + i );
 
         sha1_starts( &sha1 );
         sha1_update( &sha1, padding, 1 + i );
@@ -362,7 +364,7 @@
     unsigned char *key2;
     unsigned char *mac_enc;
     unsigned char *mac_dec;
-    unsigned int iv_copy_len;
+    size_t iv_copy_len;
     const cipher_info_t *cipher_info;
     const md_info_t *md_info;
 
@@ -1595,7 +1597,7 @@
          * correctly. (We round down instead of up, so -56 is the correct
          * value for our calculations instead of -55)
          */
-        int j, extra_run = 0;
+        size_t j, extra_run = 0;
         extra_run = ( 13 + ssl->in_msglen + padlen + 8 ) / 64 -
                     ( 13 + ssl->in_msglen          + 8 ) / 64;
 
@@ -3128,17 +3130,26 @@
     if( ssl->transform_negotiate )
         ssl_transform_free( ssl->transform_negotiate );
     else
-        ssl->transform_negotiate = polarssl_malloc( sizeof(ssl_transform) );
+    {
+        ssl->transform_negotiate =
+            (ssl_transform *) polarssl_malloc( sizeof(ssl_transform) );
+    }
 
     if( ssl->session_negotiate )
         ssl_session_free( ssl->session_negotiate );
     else
-        ssl->session_negotiate = polarssl_malloc( sizeof(ssl_session) );
+    {
+        ssl->session_negotiate =
+            (ssl_session *) polarssl_malloc( sizeof(ssl_session) );
+    }
 
     if( ssl->handshake )
         ssl_handshake_free( ssl->handshake );
     else
-        ssl->handshake = polarssl_malloc( sizeof(ssl_handshake_params) );
+    {
+        ssl->handshake = (ssl_handshake_params *)
+            polarssl_malloc( sizeof(ssl_handshake_params) );
+    }
 
     if( ssl->handshake == NULL ||
         ssl->transform_negotiate == NULL ||
@@ -3329,7 +3340,8 @@
     if( ssl->ticket_keys != NULL )
         return( 0 );
 
-    if( ( tkeys = polarssl_malloc( sizeof( ssl_ticket_keys ) ) ) == NULL )
+    tkeys = (ssl_ticket_keys *) polarssl_malloc( sizeof(ssl_ticket_keys) );
+    if( tkeys == NULL )
         return( POLARSSL_ERR_SSL_MALLOC_FAILED );
 
     if( ( ret = ssl->f_rng( ssl->p_rng, tkeys->key_name, 16 ) ) != 0 )
@@ -3461,7 +3473,8 @@
 {
     ssl_key_cert *key_cert, *last;
 
-    if( ( key_cert = polarssl_malloc( sizeof( ssl_key_cert ) ) ) == NULL )
+    key_cert = (ssl_key_cert *) polarssl_malloc( sizeof(ssl_key_cert) );
+    if( key_cert == NULL )
         return( NULL );
 
     memset( key_cert, 0, sizeof( ssl_key_cert ) );
@@ -3512,7 +3525,8 @@
     if( key_cert == NULL )
         return( POLARSSL_ERR_SSL_MALLOC_FAILED );
 
-    if( ( key_cert->key = polarssl_malloc( sizeof( pk_context ) ) ) == NULL )
+    key_cert->key = (pk_context *) polarssl_malloc( sizeof(pk_context) );
+    if( key_cert->key == NULL )
         return( POLARSSL_ERR_SSL_MALLOC_FAILED );
 
     pk_init( key_cert->key );
@@ -3521,7 +3535,7 @@
     if( ret != 0 )
         return( ret );
 
-    if( ( ret = rsa_copy( key_cert->key->pk_ctx, rsa_key ) ) != 0 )
+    if( ( ret = rsa_copy( pk_rsa( *key_cert->key ), rsa_key ) ) != 0 )
         return( ret );
 
     key_cert->cert = own_cert;
@@ -3543,7 +3557,8 @@
     if( key_cert == NULL )
         return( POLARSSL_ERR_SSL_MALLOC_FAILED );
 
-    if( ( key_cert->key = polarssl_malloc( sizeof( pk_context ) ) ) == NULL )
+    key_cert->key = (pk_context *) polarssl_malloc( sizeof(pk_context) );
+    if( key_cert->key == NULL )
         return( POLARSSL_ERR_SSL_MALLOC_FAILED );
 
     pk_init( key_cert->key );
@@ -3575,8 +3590,8 @@
     ssl->psk_len = psk_len;
     ssl->psk_identity_len = psk_identity_len;
 
-    ssl->psk = polarssl_malloc( ssl->psk_len );
-    ssl->psk_identity = polarssl_malloc( ssl->psk_identity_len );
+    ssl->psk = (unsigned char *) polarssl_malloc( ssl->psk_len );
+    ssl->psk_identity = (unsigned char *) polarssl_malloc( ssl->psk_identity_len );
 
     if( ssl->psk == NULL || ssl->psk_identity == NULL )
         return( POLARSSL_ERR_SSL_MALLOC_FAILED );
diff --git a/library/x509_create.c b/library/x509_create.c
index d3d5851..4a15e7d 100644
--- a/library/x509_create.c
+++ b/library/x509_create.c
@@ -159,7 +159,7 @@
     ASN1_CHK_ADD( len, asn1_write_len( p, start, len ) );
     ASN1_CHK_ADD( len, asn1_write_tag( p, start, ASN1_CONSTRUCTED | ASN1_SET ) );
 
-    return( len );
+    return( (int) len );
 }
 
 int x509_write_names( unsigned char **p, unsigned char *start,
@@ -180,7 +180,7 @@
     ASN1_CHK_ADD( len, asn1_write_len( p, start, len ) );
     ASN1_CHK_ADD( len, asn1_write_tag( p, start, ASN1_CONSTRUCTED | ASN1_SEQUENCE ) );
 
-    return( len );
+    return( (int) len );
 }
 
 int x509_write_sig( unsigned char **p, unsigned char *start,
@@ -208,7 +208,7 @@
     ASN1_CHK_ADD( len, asn1_write_algorithm_identifier( p, start, oid,
                                                         oid_len, 0 ) );
 
-    return( len );
+    return( (int) len );
 }
 
 static int x509_write_extension( unsigned char **p, unsigned char *start,
@@ -235,7 +235,7 @@
     ASN1_CHK_ADD( len, asn1_write_len( p, start, len ) );
     ASN1_CHK_ADD( len, asn1_write_tag( p, start, ASN1_CONSTRUCTED | ASN1_SEQUENCE ) );
 
-    return( len );
+    return( (int) len );
 }
 
 /*
@@ -261,7 +261,7 @@
         cur_ext = cur_ext->next;
     }
 
-    return( len );
+    return( (int) len );
 }
 
 #endif /* POLARSSL_X509_CREATE_C */
diff --git a/library/x509_crt.c b/library/x509_crt.c
index 91d4626..d7723ac 100644
--- a/library/x509_crt.c
+++ b/library/x509_crt.c
@@ -940,7 +940,7 @@
     WCHAR szDir[MAX_PATH];
     char filename[MAX_PATH];
 	char *p;
-    int len = strlen( path );
+    int len = (int) strlen( path );
 
 	WIN32_FIND_DATAW file_data;
     HANDLE hFind;
diff --git a/library/x509write_crt.c b/library/x509write_crt.c
index db5ae0a..c3db3c4 100644
--- a/library/x509write_crt.c
+++ b/library/x509write_crt.c
@@ -270,7 +270,7 @@
         ASN1_CHK_ADD( len, asn1_write_tag( p, start, ASN1_GENERALIZED_TIME ) );
     }
 
-    return( len );
+    return( (int) len );
 }
 
 int x509write_crt_der( x509write_cert *ctx, unsigned char *buf, size_t size,
@@ -396,7 +396,7 @@
     ASN1_CHK_ADD( len, asn1_write_len( &c2, buf, len ) );
     ASN1_CHK_ADD( len, asn1_write_tag( &c2, buf, ASN1_CONSTRUCTED | ASN1_SEQUENCE ) );
 
-    return( len );
+    return( (int) len );
 }
 
 #define PEM_BEGIN_CRT           "-----BEGIN CERTIFICATE-----\n"
diff --git a/library/x509write_csr.c b/library/x509write_csr.c
index 1eb2afb..febed67 100644
--- a/library/x509write_csr.c
+++ b/library/x509write_csr.c
@@ -209,7 +209,7 @@
     ASN1_CHK_ADD( len, asn1_write_len( &c2, buf, len ) );
     ASN1_CHK_ADD( len, asn1_write_tag( &c2, buf, ASN1_CONSTRUCTED | ASN1_SEQUENCE ) );
 
-    return( len );
+    return( (int) len );
 }
 
 #define PEM_BEGIN_CSR           "-----BEGIN CERTIFICATE REQUEST-----\n"