diff --git a/base16-decode.c b/base16-decode.c
index 20ae2ab3c8237488aae4967028a906fc2cf51d3d..4dc8abd41aef670d67ec944062d4a25c55912c37 100644
--- a/base16-decode.c
+++ b/base16-decode.c
@@ -93,17 +93,17 @@ base16_decode_single(struct base16_decode_ctx *ctx,
 
 int
 base16_decode_update(struct base16_decode_ctx *ctx,
-		     unsigned *dst_length,
+		     size_t *dst_length,
 		     uint8_t *dst,
-		     unsigned src_length,
+		     size_t src_length,
 		     const uint8_t *src)
 {
-  unsigned done;
-  unsigned i;
+  size_t done;
+  size_t i;
 
   assert(*dst_length >= BASE16_DECODE_LENGTH(src_length));
   
-  for (i = 0, done = 0; i<src_length; i++)
+  for (i = done = 0; i<src_length; i++)
     switch(base16_decode_single(ctx, dst + done, src[i]))
       {
       case -1:
diff --git a/base16-encode.c b/base16-encode.c
index a4e1b68f0b8b7773cd3b0fc14eac490f4ef92ab5..8d1e20b32877be3ecc882878ef8c7898150212d7 100644
--- a/base16-encode.c
+++ b/base16-encode.c
@@ -47,11 +47,11 @@ base16_encode_single(uint8_t *dst,
 /* Always stores BASE16_ENCODE_LENGTH(length) digits in dst. */
 void
 base16_encode_update(uint8_t *dst,
-		     unsigned length,
+		     size_t length,
 		     const uint8_t *src)
 {
-  unsigned i;
+  size_t i;
   
-  for (i = 0, dst; i<length; i++, dst += 2)
+  for (i = 0; i<length; i++, dst += 2)
     base16_encode_single(dst, src[i]);
 }
diff --git a/base16-meta.c b/base16-meta.c
index 2f7aa34aa2b7b65bb5c2f8acb1dfc58e572ef4aa..a182871a93189e3f41d6c8b1e797f68620599f61 100644
--- a/base16-meta.c
+++ b/base16-meta.c
@@ -29,25 +29,29 @@
 #include "base16.h"
 
 /* Same as the macros with the same name */
-static unsigned
-base16_encode_length(unsigned length)
+static nettle_armor_length_func base16_encode_length;
+static size_t
+base16_encode_length(size_t length)
 {
   return BASE16_ENCODE_LENGTH(length);
 }
 
-static unsigned
-base16_decode_length(unsigned length)
+static nettle_armor_length_func base16_decode_length;
+static size_t
+base16_decode_length(size_t length)
 {
   return BASE16_DECODE_LENGTH(length);
 }
 
+static nettle_armor_init_func base16_encode_init;
 static void
-base16_encode_init(void *ctx)
-{ (void) ctx; }
+base16_encode_init(void *ctx UNUSED)
+{ }
 
-static unsigned
+static nettle_armor_encode_update_func base16_encode_update_wrapper;
+static size_t
 base16_encode_update_wrapper(void *ctx UNUSED, uint8_t *dst,
-			     unsigned length, const uint8_t *src)
+			     size_t length, const uint8_t *src)
 {
   base16_encode_update(dst, length, src);
   return BASE16_ENCODE_LENGTH(length);
@@ -56,9 +60,12 @@ base16_encode_update_wrapper(void *ctx UNUSED, uint8_t *dst,
 #undef base16_encode_update
 #define base16_encode_update base16_encode_update_wrapper
 
-static unsigned
-base16_encode_final(void *ctx, uint8_t *dst)
-{ (void) ctx; (void) dst; return 0; }
+static nettle_armor_encode_final_func base16_encode_final;
+static size_t
+base16_encode_final(void *ctx UNUSED, uint8_t *dst UNUSED)
+{
+  return 0;
+}
 
 
 #define BASE16_ENCODE_FINAL_LENGTH 0
diff --git a/base16.h b/base16.h
index 5eae3325bf62546b22b4e76c20bcb911ed848415..5642293006bd583c94dbe0c99a99bd245afdb4a3 100644
--- a/base16.h
+++ b/base16.h
@@ -54,7 +54,7 @@ base16_encode_single(uint8_t *dst,
 /* Always stores BASE16_ENCODE_LENGTH(length) digits in dst. */
 void
 base16_encode_update(uint8_t *dst,
-		     unsigned length,
+		     size_t length,
 		     const uint8_t *src);
 
 
@@ -90,9 +90,9 @@ base16_decode_single(struct base16_decode_ctx *ctx,
  * too small. FIXME: Return some error instead? */
 int
 base16_decode_update(struct base16_decode_ctx *ctx,
-		     unsigned *dst_length,
+		     size_t *dst_length,
 		     uint8_t *dst,
-		     unsigned src_length,
+		     size_t src_length,
 		     const uint8_t *src);
 
 /* Returns 1 on success. */
diff --git a/base64-decode.c b/base64-decode.c
index 9624c07d20e39ee18f2800872c529529070b71a0..c7c739afe5a315fa7f3fb55d5a630c0105974184 100644
--- a/base64-decode.c
+++ b/base64-decode.c
@@ -114,13 +114,13 @@ base64_decode_single(struct base64_decode_ctx *ctx,
 
 int
 base64_decode_update(struct base64_decode_ctx *ctx,
-		     unsigned *dst_length,
+		     size_t *dst_length,
 		     uint8_t *dst,
-		     unsigned src_length,
+		     size_t src_length,
 		     const uint8_t *src)
 {
-  unsigned done;
-  unsigned i;
+  size_t done;
+  size_t i;
 
   assert(*dst_length >= BASE64_DECODE_LENGTH(src_length));
   
diff --git a/base64-encode.c b/base64-encode.c
index b594923f30064c232f127520c2ec22db38e65aea..658c9b64a55311052f33d614da998d0cb9337ace 100644
--- a/base64-encode.c
+++ b/base64-encode.c
@@ -39,7 +39,7 @@ static const uint8_t encode_table[64] =
 #define ENCODE(x) (encode_table[0x3F & (x)])
 
 void
-base64_encode_raw(uint8_t *dst, unsigned length, const uint8_t *src)
+base64_encode_raw(uint8_t *dst, size_t length, const uint8_t *src)
 {
   const uint8_t *in = src + length;
   uint8_t *out = dst + BASE64_ENCODE_RAW_LENGTH(length);
@@ -140,7 +140,7 @@ base64_encode_init(struct base64_encode_ctx *ctx)
 }
 
 /* Encodes a single byte. */
-unsigned
+size_t
 base64_encode_single(struct base64_encode_ctx *ctx,
 		     uint8_t *dst,
 		     uint8_t src)
@@ -165,16 +165,16 @@ base64_encode_single(struct base64_encode_ctx *ctx,
 
 /* Returns the number of output characters. DST should point to an
  * area of size at least BASE64_ENCODE_LENGTH(length). */
-unsigned
+size_t
 base64_encode_update(struct base64_encode_ctx *ctx,
 		     uint8_t *dst,
-		     unsigned length,
+		     size_t length,
 		     const uint8_t *src)
 {
-  unsigned done = 0;
-  unsigned left = length;
+  size_t done = 0;
+  size_t left = length;
   unsigned left_over;
-  unsigned bulk;
+  size_t bulk;
   
   while (ctx->bits && left)
     {
@@ -208,7 +208,7 @@ base64_encode_update(struct base64_encode_ctx *ctx,
 
 /* DST should point to an area of size at least
  * BASE64_ENCODE_FINAL_SIZE */
-unsigned
+size_t
 base64_encode_final(struct base64_encode_ctx *ctx,
 		    uint8_t *dst)
 {
diff --git a/base64-meta.c b/base64-meta.c
index f1ccea0fb54b77e4f1fdbcda4f58e091feef4d25..cb33c67503692f2bf45fef1fdbdd890b983b1b8a 100644
--- a/base64-meta.c
+++ b/base64-meta.c
@@ -29,14 +29,16 @@
 #include "base64.h"
 
 /* Same as the macros with the same name */
-static unsigned
-base64_encode_length(unsigned length)
+static nettle_armor_length_func base64_encode_length;
+static size_t
+base64_encode_length(size_t length)
 {
   return BASE64_ENCODE_LENGTH(length);
 }
 
-static unsigned
-base64_decode_length(unsigned length)
+static nettle_armor_length_func base64_decode_length;
+static size_t
+base64_decode_length(size_t length)
 {
   return BASE64_DECODE_LENGTH(length);
 }
diff --git a/base64.h b/base64.h
index b2bd8a8b3f543b8f9c50ef4b16ecefa09fdc2bb2..94ed52ae2af8a89d7a5c3bcbb4628caf3c3e47ab 100644
--- a/base64.h
+++ b/base64.h
@@ -71,22 +71,22 @@ void
 base64_encode_init(struct base64_encode_ctx *ctx);
 
 /* Encodes a single byte. Returns amount of output (always 1 or 2). */
-unsigned
+size_t
 base64_encode_single(struct base64_encode_ctx *ctx,
 		     uint8_t *dst,
 		     uint8_t src);
 
 /* Returns the number of output characters. DST should point to an
  * area of size at least BASE64_ENCODE_LENGTH(length). */
-unsigned
+size_t
 base64_encode_update(struct base64_encode_ctx *ctx,
 		     uint8_t *dst,
-		     unsigned length,
+		     size_t length,
 		     const uint8_t *src);
 
 /* DST should point to an area of size at least
  * BASE64_ENCODE_FINAL_LENGTH */
-unsigned
+size_t
 base64_encode_final(struct base64_encode_ctx *ctx,
 		    uint8_t *dst);
 
@@ -96,7 +96,7 @@ base64_encode_final(struct base64_encode_ctx *ctx,
  * Generates exactly BASE64_ENCODE_RAW_LENGTH(length) bytes of output.
  * Supports overlapped operation, if src <= dst. */
 void
-base64_encode_raw(uint8_t *dst, unsigned length, const uint8_t *src);
+base64_encode_raw(uint8_t *dst, size_t length, const uint8_t *src);
 
 void
 base64_encode_group(uint8_t *dst, uint32_t group);
@@ -137,9 +137,9 @@ base64_decode_single(struct base64_decode_ctx *ctx,
  * too small. FIXME: Return some error instead? */
 int
 base64_decode_update(struct base64_decode_ctx *ctx,
-		     unsigned *dst_length,
+		     size_t *dst_length,
 		     uint8_t *dst,
-		     unsigned src_length,
+		     size_t src_length,
 		     const uint8_t *src);
 
 /* Returns 1 on success. */
diff --git a/buffer.c b/buffer.c
index 869e6a33918b497c701553514a4b11321f28df37..d2aeadef345fe781675c95742eb2cbbaf301bf6c 100644
--- a/buffer.c
+++ b/buffer.c
@@ -35,13 +35,13 @@
 
 int
 nettle_buffer_grow(struct nettle_buffer *buffer,
-		   unsigned length)
+		   size_t length)
 {
   assert(buffer->size <= buffer->alloc);
   
   if (buffer->size + length > buffer->alloc)
     {
-      unsigned alloc;
+      size_t alloc;
       uint8_t *p;
       
       if (!buffer->realloc)
@@ -72,7 +72,7 @@ nettle_buffer_init_realloc(struct nettle_buffer *buffer,
 
 void
 nettle_buffer_init_size(struct nettle_buffer *buffer,
-			unsigned length, uint8_t *space)
+			size_t length, uint8_t *space)
 {
   buffer->contents = space;
   buffer->alloc = length;
@@ -100,7 +100,7 @@ nettle_buffer_reset(struct nettle_buffer *buffer)
 
 uint8_t *
 nettle_buffer_space(struct nettle_buffer *buffer,
-		    unsigned length)
+		    size_t length)
 {
   uint8_t *p;
 
@@ -114,7 +114,7 @@ nettle_buffer_space(struct nettle_buffer *buffer,
      
 int
 nettle_buffer_write(struct nettle_buffer *buffer,
-		    unsigned length, const uint8_t *data)
+		    size_t length, const uint8_t *data)
 {
   uint8_t *p = nettle_buffer_space(buffer, length);
   if (p)
diff --git a/buffer.h b/buffer.h
index 3bd37a2f9e645f9ca6b8c7182c73017a2f6b9c3d..cbc691d30cb823ecacbd668d76e5b84477cb7d8e 100644
--- a/buffer.h
+++ b/buffer.h
@@ -36,13 +36,13 @@ struct nettle_buffer
 {
   uint8_t *contents;
   /* Allocated size */
-  unsigned alloc;
+  size_t alloc;
 
   void *realloc_ctx;
   nettle_realloc_func *realloc;
 
   /* Current size */
-  unsigned size;
+  size_t size;
 };
 
 /* Initializes a buffer that uses plain realloc */
@@ -57,7 +57,7 @@ nettle_buffer_init_realloc(struct nettle_buffer *buffer,
 /* Initializes a buffer of fix size */
 void
 nettle_buffer_init_size(struct nettle_buffer *buffer,
-			unsigned length, uint8_t *space);
+			size_t length, uint8_t *space);
 
 void
 nettle_buffer_clear(struct nettle_buffer *buffer);
@@ -68,7 +68,7 @@ nettle_buffer_reset(struct nettle_buffer *buffer);
 
 int
 nettle_buffer_grow(struct nettle_buffer *buffer,
-		   unsigned length);
+		   size_t length);
 
 #define NETTLE_BUFFER_PUTC(buffer, c) \
 ( (((buffer)->size < (buffer)->alloc) || nettle_buffer_grow((buffer), 1)) \
@@ -76,7 +76,7 @@ nettle_buffer_grow(struct nettle_buffer *buffer,
 
 int
 nettle_buffer_write(struct nettle_buffer *buffer,
-		    unsigned length, const uint8_t *data);
+		    size_t length, const uint8_t *data);
 
 /* Like nettle_buffer_write, but instead of copying data to the
  * buffer, it returns a pointer to the area where the caller can copy
@@ -84,7 +84,7 @@ nettle_buffer_write(struct nettle_buffer *buffer,
  * reallocate the buffer. */
 uint8_t *
 nettle_buffer_space(struct nettle_buffer *buffer,
-		    unsigned length);
+		    size_t length);
 
 /* Copy the contents of SRC to the end of DST. */
 int
diff --git a/examples/base16dec.c b/examples/base16dec.c
index 75c8ca592f98df01d7dbf27d8103039fd08ab9f2..4b124d284e63ef0537791ee682938d5abcd297f9 100644
--- a/examples/base16dec.c
+++ b/examples/base16dec.c
@@ -65,7 +65,7 @@ main(int argc UNUSED, char **argv UNUSED)
   for (;;)
     {
       int nbytes; /* Number of bytes read frmo disk at each iteration */
-      unsigned decoded_bytes; /* Bytes actually generated at each iteration */
+      size_t decoded_bytes; /* Bytes actually generated at each iteration */
 
       nbytes = fread(buffer, 1, CHUNK_SIZE, stdin);
 
@@ -75,30 +75,30 @@ main(int argc UNUSED, char **argv UNUSED)
 	  return EXIT_FAILURE;
 	}
       
-    decoded_bytes = BASE16_DECODE_LENGTH(nbytes);
-
-    /* Decodes one chunk: */
-    if (!base16_decode_update(&b16_ctx, &decoded_bytes, result, nbytes, buffer))
-      {
-	werror ("Error decoding input (not base16?)\n");
-	return EXIT_FAILURE;
-      }
-
-    if (!write_string (stdout, decoded_bytes, result))
-      {
-	werror ("Error writing file: %s\n", strerror(errno));
-	return EXIT_FAILURE;
-      }
-    if (nbytes < CHUNK_SIZE)
-      {
-	/* Check if decoding finalized OK: */
-	if (!base16_decode_final(&b16_ctx))
-	  {
-	    werror("Decoding did not finish properly.\n");
-	    return EXIT_FAILURE;
-	  }
-	break;
-      }
+      decoded_bytes = BASE16_DECODE_LENGTH(nbytes);
+
+      /* Decodes one chunk: */
+      if (!base16_decode_update(&b16_ctx, &decoded_bytes, result, nbytes, buffer))
+	{
+	  werror ("Error decoding input (not base16?)\n");
+	  return EXIT_FAILURE;
+	}
+
+      if (!write_string (stdout, decoded_bytes, result))
+	{
+	  werror ("Error writing file: %s\n", strerror(errno));
+	  return EXIT_FAILURE;
+	}
+      if (nbytes < CHUNK_SIZE)
+	{
+	  /* Check if decoding finalized OK: */
+	  if (!base16_decode_final(&b16_ctx))
+	    {
+	      werror("Decoding did not finish properly.\n");
+	      return EXIT_FAILURE;
+	    }
+	  break;
+	}
     }
 
   if (fflush (stdout) != 0)
diff --git a/examples/base64dec.c b/examples/base64dec.c
index a2fbaedbaecc5f6fe1b6d9ff115bc5fb4409cec6..653940eb970d25d0d440c63ae64e67b735126a1f 100644
--- a/examples/base64dec.c
+++ b/examples/base64dec.c
@@ -65,7 +65,7 @@ main(int argc UNUSED, char **argv UNUSED)
   for (;;)
     {
       int nbytes; /* Number of bytes read frmo disk at each iteration */
-      unsigned decoded_bytes; /* Bytes actually generated at each iteration */
+      size_t decoded_bytes; /* Bytes actually generated at each iteration */
 
       nbytes = fread(buffer, 1, CHUNK_SIZE, stdin);
 
diff --git a/nettle-types.h b/nettle-types.h
index 0c3727237e0aafa164ab1e3de4d5645dbea26180..cd6ad15dbb199a4990d3fbd8b39009dd0f3dadfb 100644
--- a/nettle-types.h
+++ b/nettle-types.h
@@ -67,20 +67,20 @@ typedef void nettle_hash_digest_func(void *ctx,
 
 /* ASCII armor codecs. NOTE: Experimental and subject to change. */
 
-typedef unsigned nettle_armor_length_func(unsigned length);
+typedef size_t nettle_armor_length_func(size_t length);
 typedef void nettle_armor_init_func(void *ctx);
 
-typedef unsigned nettle_armor_encode_update_func(void *ctx,
-						 uint8_t *dst,
-						 unsigned src_length,
-						 const uint8_t *src);
+typedef size_t nettle_armor_encode_update_func(void *ctx,
+					       uint8_t *dst,
+					       size_t src_length,
+					       const uint8_t *src);
 
-typedef unsigned nettle_armor_encode_final_func(void *ctx, uint8_t *dst);
+typedef size_t nettle_armor_encode_final_func(void *ctx, uint8_t *dst);
 
 typedef int nettle_armor_decode_update_func(void *ctx,
-					    unsigned *dst_length,
+					    size_t *dst_length,
 					    uint8_t *dst,
-					    unsigned src_length,
+					    size_t src_length,
 					    const uint8_t *src);
 
 typedef int nettle_armor_decode_final_func(void *ctx);
diff --git a/sexp-format.c b/sexp-format.c
index 93d1fd988a696f4cb9a812cb2a72da5396b59230..548c5f1ddb5db9bc348d801af56626ecfe68bbf0 100644
--- a/sexp-format.c
+++ b/sexp-format.c
@@ -40,14 +40,14 @@
 
 static unsigned
 format_prefix(struct nettle_buffer *buffer,
-	      unsigned length)
+	      size_t length)
 {
-  unsigned digit = 1;
+  size_t digit = 1;
   unsigned prefix_length = 1;
   
   for (;;)
     {
-      unsigned next = digit * 10;
+      size_t next = digit * 10;
       if (next > length)
 	break;
 
@@ -68,9 +68,9 @@ format_prefix(struct nettle_buffer *buffer,
   return prefix_length + 1;
 }
 
-static unsigned
+static size_t
 format_string(struct nettle_buffer *buffer,
-	      unsigned length, const uint8_t *s)
+	      size_t length, const uint8_t *s)
 {
   unsigned prefix_length = format_prefix(buffer, length);
   if (!prefix_length)
@@ -82,11 +82,11 @@ format_string(struct nettle_buffer *buffer,
   return prefix_length + length;
 }
 
-unsigned
+size_t
 sexp_vformat(struct nettle_buffer *buffer, const char *format, va_list args)
 {
   unsigned nesting = 0;
-  unsigned done = 0;
+  size_t done = 0;
 
   for (;;)
     switch (*format++)
@@ -94,8 +94,8 @@ sexp_vformat(struct nettle_buffer *buffer, const char *format, va_list args)
       default:
 	{
 	  const char *start = format - 1;
-	  unsigned length = 1 + strcspn(format, "()% \t");
-	  unsigned output_length = format_string(buffer, length, start);
+	  size_t length = 1 + strcspn(format, "()% \t");
+	  size_t output_length = format_string(buffer, length, start);
 	  if (!output_length)
 	    return 0;
 	  
@@ -154,8 +154,8 @@ sexp_vformat(struct nettle_buffer *buffer, const char *format, va_list args)
 	    case 's':
 	      {
 		const char *s;
-		unsigned length;
-		unsigned output_length;
+		size_t length;
+		size_t output_length;
 		
 		if (nul_flag)
 		  {
@@ -164,7 +164,7 @@ sexp_vformat(struct nettle_buffer *buffer, const char *format, va_list args)
 		  }
 		else
 		  {
-		    length = va_arg(args, unsigned);
+		    length = va_arg(args, size_t);
 		    s = va_arg(args, const char *);
 		  }
 		
@@ -178,8 +178,8 @@ sexp_vformat(struct nettle_buffer *buffer, const char *format, va_list args)
 	    case 't':
 	      {
 		const char *s;
-		unsigned length;
-		unsigned output_length;
+		size_t length;
+		size_t output_length;
 		
 		if (nul_flag)
 		  {
@@ -191,7 +191,7 @@ sexp_vformat(struct nettle_buffer *buffer, const char *format, va_list args)
 		  }
 		else
 		  {
-		    length = va_arg(args, unsigned);
+		    length = va_arg(args, size_t);
 		    s = va_arg(args, const char *);
 		    if (!s)
 		      break;
@@ -218,7 +218,7 @@ sexp_vformat(struct nettle_buffer *buffer, const char *format, va_list args)
 	    case 'l':
 	      {
 		const char *s;
-		unsigned length;
+		size_t length;
 		
 		if (nul_flag)
 		  {
@@ -227,7 +227,7 @@ sexp_vformat(struct nettle_buffer *buffer, const char *format, va_list args)
 		  }
 		else
 		  {
-		    length = va_arg(args, unsigned);
+		    length = va_arg(args, size_t);
 		    s = va_arg(args, const char *);
 		  }
 
@@ -291,7 +291,7 @@ sexp_vformat(struct nettle_buffer *buffer, const char *format, va_list args)
 	    case 'b':
 	      {
 		const MP_INT *n = va_arg(args, const MP_INT *);
-		unsigned length;
+		size_t length;
 		unsigned prefix_length;
 	      
 		length = nettle_mpz_sizeinbase_256_s(n);
@@ -319,11 +319,11 @@ sexp_vformat(struct nettle_buffer *buffer, const char *format, va_list args)
       }
 }
 
-unsigned
+size_t
 sexp_format(struct nettle_buffer *buffer, const char *format, ...)
 {
   va_list args;
-  unsigned done;
+  size_t done;
   
   va_start(args, format);
   done = sexp_vformat(buffer, format, args);
diff --git a/sexp-transport-format.c b/sexp-transport-format.c
index cb7f3f1f5fae81bc14f1b89d538c3b656ac7120b..7d83ac48bb5c075313fad4f8c35f43e62c61b362 100644
--- a/sexp-transport-format.c
+++ b/sexp-transport-format.c
@@ -32,13 +32,13 @@
 #include "base64.h"
 #include "buffer.h"
 
-unsigned
+size_t
 sexp_transport_vformat(struct nettle_buffer *buffer,
 		       const char *format, va_list args)
 {
-  unsigned start = 0;
-  unsigned length;
-  unsigned base64_length;
+  size_t start = 0;
+  size_t length;
+  size_t base64_length;
 
   if (buffer)
     {
@@ -70,11 +70,11 @@ sexp_transport_vformat(struct nettle_buffer *buffer,
   return base64_length + 2;
 }
 
-unsigned
+size_t
 sexp_transport_format(struct nettle_buffer *buffer,
 		      const char *format, ...)
 {
-  unsigned done;
+  size_t done;
   va_list args;
 
   va_start(args, format);
diff --git a/sexp-transport.c b/sexp-transport.c
index 0adcac2783f7d2088d09beac6771bc93260ce278..e65832f3ed3e5ea9996fbb70cc2422407a1213d5 100644
--- a/sexp-transport.c
+++ b/sexp-transport.c
@@ -37,13 +37,13 @@
 /* NOTE: Decodes the input string in place */
 int
 sexp_transport_iterator_first(struct sexp_iterator *iterator,
-			      unsigned length, uint8_t *input)
+			      size_t length, uint8_t *input)
 {
   /* We first base64 decode any transport encoded sexp at the start of
    * the input. */
 
-  unsigned in = 0;
-  unsigned out = 0;
+  size_t in = 0;
+  size_t out = 0;
 
   while (in < length)
     switch(input[in])
@@ -64,8 +64,8 @@ sexp_transport_iterator_first(struct sexp_iterator *iterator,
 	{
 	  /* Found transport encoding */
 	  struct base64_decode_ctx ctx;
-	  unsigned coded_length;
-	  unsigned end;
+	  size_t coded_length;
+	  size_t end;
 
 	  for (end = ++in; end < length && input[end] != '}'; end++)
 	    ;
diff --git a/sexp.c b/sexp.c
index 69b83652a79aa91177bb2c165f96cbce425f0142..2d0017c287172449734c944136917acdea20a73c 100644
--- a/sexp.c
+++ b/sexp.c
@@ -57,7 +57,7 @@ sexp_iterator_init(struct sexp_iterator *iterator,
 
 static int
 sexp_iterator_simple(struct sexp_iterator *iterator,
-		     unsigned *size,
+		     size_t *size,
 		     const uint8_t **string)
 {
   unsigned length = 0;
@@ -156,7 +156,7 @@ sexp_iterator_parse(struct sexp_iterator *iterator)
 
 int
 sexp_iterator_first(struct sexp_iterator *iterator,
-		    unsigned length, const uint8_t *input)
+		    size_t length, const uint8_t *input)
 {
   sexp_iterator_init(iterator, length, input);
   return sexp_iterator_parse(iterator);
@@ -232,9 +232,9 @@ sexp_iterator_exit_lists(struct sexp_iterator *iterator,
 
 const uint8_t *
 sexp_iterator_subexpr(struct sexp_iterator *iterator,
-		      unsigned *length)
+		      size_t *length)
 {
-  unsigned start = iterator->start;
+  size_t start = iterator->start;
   if (!sexp_iterator_next(iterator))
     return 0;
 
@@ -251,7 +251,7 @@ sexp_iterator_get_uint32(struct sexp_iterator *iterator,
       && iterator->atom_length
       && iterator->atom[0] < 0x80)
     {
-      unsigned length = iterator->atom_length;
+      size_t length = iterator->atom_length;
       const uint8_t *p = iterator->atom;
 
       /* Skip leading zeros. */
diff --git a/sexp.h b/sexp.h
index 7b68358d3653bc3b201b0f14a50bba1be878ea51..19700a46f0629140f97b0ca4d8c0aa985a9165f4 100644
--- a/sexp.h
+++ b/sexp.h
@@ -55,22 +55,22 @@ enum sexp_type
 
 struct sexp_iterator
 {
-  unsigned length;
+  size_t length;
   const uint8_t *buffer;
 
   /* Points at the start of the current sub expression. */
-  unsigned start;
+  size_t start;
   /* If type is SEXP_LIST, pos points at the start of the current
    * element. Otherwise, it points at the end. */
-  unsigned pos;
+  size_t pos;
   unsigned level;
 
   enum sexp_type type;
   
-  unsigned display_length;
+  size_t display_length;
   const uint8_t *display;
 
-  unsigned atom_length;
+  size_t atom_length;
   const uint8_t *atom;
 };
 
@@ -80,12 +80,12 @@ struct sexp_iterator
 /* Initializes the iterator. */
 int
 sexp_iterator_first(struct sexp_iterator *iterator,
-		    unsigned length, const uint8_t *input);
+		    size_t length, const uint8_t *input);
 
 /* NOTE: Decodes the input string in place */
 int
 sexp_transport_iterator_first(struct sexp_iterator *iterator,
-			      unsigned length, uint8_t *input);
+			      size_t length, uint8_t *input);
 
 int
 sexp_iterator_next(struct sexp_iterator *iterator);
@@ -110,7 +110,7 @@ sexp_iterator_exit_lists(struct sexp_iterator *iterator,
  * sexp_iterator_next. */
 const uint8_t *
 sexp_iterator_subexpr(struct sexp_iterator *iterator,
-		      unsigned *length);
+		      size_t *length);
 
 int
 sexp_iterator_get_uint32(struct sexp_iterator *iterator,
@@ -160,10 +160,10 @@ struct nettle_buffer;
  * separates tokens but is otherwise ignored) and the following
  * formatting specifiers:
  *
- *   %s   String represented as unsigned length, const uint8_t *data.
+ *   %s   String represented as size_t length, const uint8_t *data.
  *
  *   %t   Optional display type, represented as
- *        unsigned display_length, const uint8_t *display,
+ *        size_t display_length, const uint8_t *display,
  *        display == NULL means no display type.
  *
  *   %i   Non-negative small integer, uint32_t.
@@ -171,7 +171,7 @@ struct nettle_buffer;
  *   %b   Non-negative bignum, mpz_t.
  *
  *   %l   Literal string (no length added), typically a balanced
- *        subexpression. Represented as unsigned length, const uint8_t
+ *        subexpression. Represented as size_t length, const uint8_t
  *        *data.
  *
  *   %(, %)  Allows insertion of unbalanced parenthesis.
@@ -183,19 +183,19 @@ struct nettle_buffer;
  *        const uint8_t * argument.
  */
  
-unsigned
+size_t
 sexp_format(struct nettle_buffer *buffer,
 	    const char *format, ...);
 
-unsigned
+size_t
 sexp_vformat(struct nettle_buffer *buffer,
 	     const char *format, va_list args);
 
-unsigned
+size_t
 sexp_transport_format(struct nettle_buffer *buffer,
 		      const char *format, ...);
 
-unsigned
+size_t
 sexp_transport_vformat(struct nettle_buffer *buffer,
 		       const char *format, va_list args);
 
diff --git a/testsuite/base64-test.c b/testsuite/base64-test.c
index b2388aee372beb7010a0fcd0d852087aeb9482af..f512d258f462d23eaefa4181c7812fe14645bd6a 100644
--- a/testsuite/base64-test.c
+++ b/testsuite/base64-test.c
@@ -32,7 +32,7 @@ test_main(void)
     /* Test overlapping areas */
     uint8_t buffer[] = "Helloxxxx";
     struct base64_decode_ctx ctx;
-    unsigned dst_length;
+    size_t dst_length;
     
     ASSERT(BASE64_ENCODE_RAW_LENGTH(5) == 8);
     base64_encode_raw(buffer, 5, buffer);
diff --git a/testsuite/sexp-format-test.c b/testsuite/sexp-format-test.c
index 736922b07b6d60e830498b51dcf2ce5efdb3286b..548527969335819b683800cfcd030bb619e49d4d 100644
--- a/testsuite/sexp-format-test.c
+++ b/testsuite/sexp-format-test.c
@@ -62,7 +62,7 @@ test_main(void)
     
     nettle_buffer_init(&buffer);  
     ASSERT(sexp_format(&buffer, "(%0s%l)",
-		       "foo", 7, "(4:bar)")
+		       "foo", (size_t) 7, "(4:bar)")
 	   == strlen(e));
     
     ASSERT(buffer.size == strlen(e));
@@ -121,10 +121,10 @@ test_main(void)
     const uint8_t e[] = ")3:foo(3:bar";
 
     nettle_buffer_init(&buffer);
-    ASSERT(sexp_format(&buffer, "%)foo%(%s", 3, "bar")
+    ASSERT(sexp_format(&buffer, "%)foo%(%s", (size_t) 3, "bar")
 	   == strlen(e));
     
-    ASSERT(sexp_format(NULL, "%)foo%(%s", 3, "bar")
+    ASSERT(sexp_format(NULL, "%)foo%(%s", (size_t) 3, "bar")
 	   == strlen(e));
     
     ASSERT(buffer.size == strlen(e));
diff --git a/testsuite/testutils.c b/testsuite/testutils.c
index a264b4cab86bb08e69b45e2d2c1d55c0ae9fe7b9..c757c75d564d10b6cc01765a3fdc6e684c48566e 100644
--- a/testsuite/testutils.c
+++ b/testsuite/testutils.c
@@ -607,12 +607,12 @@ test_armor(const struct nettle_armor *armor,
            const uint8_t *data,
            const uint8_t *ascii)
 {
-  unsigned ascii_length = strlen(ascii);
+  size_t ascii_length = strlen(ascii);
   uint8_t *buffer = xalloc(1 + ascii_length);
   uint8_t *check = xalloc(1 + armor->decode_length(ascii_length));
   void *encode = xalloc(armor->encode_context_size);
   void *decode = xalloc(armor->decode_context_size);
-  unsigned done;
+  size_t done;
 
   ASSERT(ascii_length
 	 <= (armor->encode_length(data_length) + armor->encode_final_length));
diff --git a/tools/input.c b/tools/input.c
index 2069e07c03af7157b3ac2f124d09a6b312275edb..f12788def9e31cb27f7840326b071679587f1a72 100644
--- a/tools/input.c
+++ b/tools/input.c
@@ -63,7 +63,7 @@ sexp_get_char(struct sexp_input *input)
   if (input->coding)
     for (;;)
       {
-	unsigned done;
+	size_t done;
 
 	sexp_get_raw_char(input);
 	if (input->ctype == SEXP_EOF_CHAR)
diff --git a/tools/pkcs1-conv.c b/tools/pkcs1-conv.c
index 231b2acd4348161808b2612dae77e9dd04cf3edd..13b9ba05c156b5df8489ab3ddef470db8b2a81ff 100644
--- a/tools/pkcs1-conv.c
+++ b/tools/pkcs1-conv.c
@@ -128,9 +128,9 @@ pem_ws[33] = {
 
 /* Returns 1 on match, otherwise 0. */ 
 static int
-match_pem_start(unsigned length, const uint8_t *line,
-		unsigned *marker_start,
-		unsigned *marker_length)
+match_pem_start(size_t length, const uint8_t *line,
+		size_t *marker_start,
+		size_t *marker_length)
 {
   while (length > 0 && PEM_IS_SPACE(line[length - 1]))
     length--;
@@ -152,8 +152,8 @@ match_pem_start(unsigned length, const uint8_t *line,
 /* Returns 1 on match, -1 if the line is of the right form except for
    the marker, otherwise 0. */ 
 static int
-match_pem_end(unsigned length, const uint8_t *line,
-	      unsigned marker_length,
+match_pem_end(size_t length, const uint8_t *line,
+	      size_t marker_length,
 	      const uint8_t *marker)
 {
   while (length > 0 && PEM_IS_SPACE(line[length - 1]))
@@ -178,10 +178,10 @@ match_pem_end(unsigned length, const uint8_t *line,
 struct pem_info
 {
   /* The FOO part in "-----BEGIN FOO-----" */
-  unsigned marker_start;
-  unsigned marker_length;
-  unsigned data_start;
-  unsigned data_length;
+  size_t marker_start;
+  size_t marker_length;
+  size_t data_start;
+  size_t data_length;
 };
 
 static int
@@ -211,7 +211,7 @@ read_pem(struct nettle_buffer *buffer, FILE *f,
 
   for (;;)
     {
-      unsigned line_start = buffer->size;
+      size_t line_start = buffer->size;
 
       if (read_line(buffer, f) != 1)
 	return 0;
@@ -236,7 +236,7 @@ read_pem(struct nettle_buffer *buffer, FILE *f,
 
 static int
 decode_base64(struct nettle_buffer *buffer,
-	      unsigned start, unsigned *length)
+	      size_t start, size_t *length)
 {
   struct base64_decode_ctx ctx;
   
@@ -257,7 +257,7 @@ decode_base64(struct nettle_buffer *buffer,
 }
 
 static int
-convert_rsa_public_key(struct nettle_buffer *buffer, unsigned length, const uint8_t *data)
+convert_rsa_public_key(struct nettle_buffer *buffer, size_t length, const uint8_t *data)
 {
   struct rsa_public_key pub;
   int res;
@@ -281,7 +281,7 @@ convert_rsa_public_key(struct nettle_buffer *buffer, unsigned length, const uint
 }
 
 static int
-convert_rsa_private_key(struct nettle_buffer *buffer, unsigned length, const uint8_t *data)
+convert_rsa_private_key(struct nettle_buffer *buffer, size_t length, const uint8_t *data)
 {
   struct rsa_public_key pub;
   struct rsa_private_key priv;
@@ -309,7 +309,7 @@ convert_rsa_private_key(struct nettle_buffer *buffer, unsigned length, const uin
 }
 
 static int
-convert_dsa_private_key(struct nettle_buffer *buffer, unsigned length, const uint8_t *data)
+convert_dsa_private_key(struct nettle_buffer *buffer, size_t length, const uint8_t *data)
 {
   struct dsa_public_key pub;
   struct dsa_private_key priv;
@@ -338,7 +338,7 @@ convert_dsa_private_key(struct nettle_buffer *buffer, unsigned length, const uin
 
 /* Returns 1 on success, 0 on error, and -1 for unsupported algorithms. */
 static int
-convert_public_key(struct nettle_buffer *buffer, unsigned length, const uint8_t *data)
+convert_public_key(struct nettle_buffer *buffer, size_t length, const uint8_t *data)
 {
   /* SubjectPublicKeyInfo ::= SEQUENCE {
          algorithm		AlgorithmIdentifier,
@@ -459,7 +459,7 @@ convert_public_key(struct nettle_buffer *buffer, unsigned length, const uint8_t
 static int
 convert_type(struct nettle_buffer *buffer,
 	     enum object_type type,
-	     unsigned length, const uint8_t *data)
+	     size_t length, const uint8_t *data)
 {
   int res;