Commit 61990ebc authored by Niels Möller's avatar Niels Möller
Browse files

Merge branch 'armor-signedness' into master-updates

parents ecfc1125 1edcf521
2017-09-24 Niels Möller <nisse@lysator.liu.se>
* tools/pkcs1-conv.c (base64_decode_in_place): New helper
function.
(decode_base64): Use it.
* sexp-transport-format.c (base64_encode_in_place): New helper
function.
(sexp_transport_vformat): Use it.
* testsuite/base64-test.c (test_fuzz_once): Update to use char
type where appropriate.
(test_main): Use helper functions base64_encode_in_place and
base64_decode_in_place (copied to this file).
* testsuite/testutils.c (tstring_data): Use uint8_t for data
argument.
* testsuite/testutils.h (SDATA): Use US macro to cast data
argument.
2017-09-14 Niels Möller <nisse@lysator.liu.se>
* hkdf.c: Delete unneeded includes. Use Nettle licensing notice.
......
......@@ -66,14 +66,16 @@ hex_decode_table[0x80] =
int
base16_decode_single(struct base16_decode_ctx *ctx,
uint8_t *dst,
uint8_t src)
char src)
{
/* Avoid signed char for indexing. */
unsigned char usrc = src;
int digit;
if (src >= 0x80)
if (usrc >= 0x80)
return -1;
digit = hex_decode_table[src];
digit = hex_decode_table[usrc];
switch (digit)
{
case -1:
......@@ -104,7 +106,7 @@ base16_decode_update(struct base16_decode_ctx *ctx,
size_t *dst_length,
uint8_t *dst,
size_t src_length,
const uint8_t *src)
const char *src)
{
size_t done;
size_t i;
......
......@@ -45,7 +45,7 @@ hex_digits[16] = "0123456789abcdef";
/* Encodes a single byte. Always stores two digits in dst[0] and dst[1]. */
void
base16_encode_single(uint8_t *dst,
base16_encode_single(char *dst,
uint8_t src)
{
dst[0] = DIGIT(src/0x10);
......@@ -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,
base16_encode_update(char *dst,
size_t length,
const uint8_t *src)
{
......
......@@ -59,7 +59,7 @@ base16_encode_init(void *ctx UNUSED)
static nettle_armor_encode_update_func base16_encode_update_wrapper;
static size_t
base16_encode_update_wrapper(void *ctx UNUSED, uint8_t *dst,
base16_encode_update_wrapper(void *ctx UNUSED, char *dst,
size_t length, const uint8_t *src)
{
base16_encode_update(dst, length, src);
......@@ -71,7 +71,7 @@ base16_encode_update_wrapper(void *ctx UNUSED, uint8_t *dst,
static nettle_armor_encode_final_func base16_encode_final;
static size_t
base16_encode_final(void *ctx UNUSED, uint8_t *dst UNUSED)
base16_encode_final(void *ctx UNUSED, char *dst UNUSED)
{
return 0;
}
......
......@@ -56,12 +56,12 @@ extern "C" {
/* Encodes a single byte. Always stores two digits in dst[0] and dst[1]. */
void
base16_encode_single(uint8_t *dst,
base16_encode_single(char *dst,
uint8_t src);
/* Always stores BASE16_ENCODE_LENGTH(length) digits in dst. */
void
base16_encode_update(uint8_t *dst,
base16_encode_update(char *dst,
size_t length,
const uint8_t *src);
......@@ -86,7 +86,7 @@ base16_decode_init(struct base16_decode_ctx *ctx);
int
base16_decode_single(struct base16_decode_ctx *ctx,
uint8_t *dst,
uint8_t src);
char src);
/* Returns 1 on success, 0 on error. DST should point to an area of
* size at least BASE16_DECODE_LENGTH(length). The amount of data
......@@ -97,7 +97,7 @@ base16_decode_update(struct base16_decode_ctx *ctx,
size_t *dst_length,
uint8_t *dst,
size_t src_length,
const uint8_t *src);
const char *src);
/* Returns 1 on success. */
int
......
......@@ -73,9 +73,9 @@ base64_decode_init(struct base64_decode_ctx *ctx)
int
base64_decode_single(struct base64_decode_ctx *ctx,
uint8_t *dst,
uint8_t src)
char src)
{
int data = ctx->table[src];
int data = ctx->table[(uint8_t) src];
switch(data)
{
......@@ -122,7 +122,7 @@ base64_decode_update(struct base64_decode_ctx *ctx,
size_t *dst_length,
uint8_t *dst,
size_t src_length,
const uint8_t *src)
const char *src)
{
size_t done;
size_t i;
......
......@@ -41,11 +41,11 @@
#define ENCODE(alphabet,x) ((alphabet)[0x3F & (x)])
static void
encode_raw(const uint8_t *alphabet,
uint8_t *dst, size_t length, const uint8_t *src)
encode_raw(const char *alphabet,
char *dst, size_t length, const uint8_t *src)
{
const uint8_t *in = src + length;
uint8_t *out = dst + BASE64_ENCODE_RAW_LENGTH(length);
char *out = dst + BASE64_ENCODE_RAW_LENGTH(length);
unsigned left_over = length % 3;
......@@ -83,19 +83,19 @@ encode_raw(const uint8_t *alphabet,
assert(out == dst);
}
static const uint8_t base64_encode_table[64] =
static const char base64_encode_table[64] =
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz"
"0123456789+/";
void
base64_encode_raw(uint8_t *dst, size_t length, const uint8_t *src)
base64_encode_raw(char *dst, size_t length, const uint8_t *src)
{
encode_raw(base64_encode_table, dst, length, src);
}
void
base64_encode_group(uint8_t *dst, uint32_t group)
base64_encode_group(char *dst, uint32_t group)
{
*dst++ = ENCODE(base64_encode_table, (group >> 18));
*dst++ = ENCODE(base64_encode_table, (group >> 12));
......@@ -113,7 +113,7 @@ base64_encode_init(struct base64_encode_ctx *ctx)
/* Encodes a single byte. */
size_t
base64_encode_single(struct base64_encode_ctx *ctx,
uint8_t *dst,
char *dst,
uint8_t src)
{
unsigned done = 0;
......@@ -138,7 +138,7 @@ base64_encode_single(struct base64_encode_ctx *ctx,
* area of size at least BASE64_ENCODE_LENGTH(length). */
size_t
base64_encode_update(struct base64_encode_ctx *ctx,
uint8_t *dst,
char *dst,
size_t length,
const uint8_t *src)
{
......@@ -181,7 +181,7 @@ base64_encode_update(struct base64_encode_ctx *ctx,
* BASE64_ENCODE_FINAL_SIZE */
size_t
base64_encode_final(struct base64_encode_ctx *ctx,
uint8_t *dst)
char *dst)
{
unsigned done = 0;
unsigned bits = ctx->bits;
......
......@@ -73,7 +73,7 @@ extern "C" {
struct base64_encode_ctx
{
const uint8_t *alphabet; /* Alphabet to use for encoding */
const char *alphabet; /* Alphabet to use for encoding */
unsigned short word; /* Leftover bits */
unsigned char bits; /* Number of bits, always 0, 2, or 4. */
};
......@@ -89,14 +89,14 @@ base64url_encode_init(struct base64_encode_ctx *ctx);
/* Encodes a single byte. Returns amount of output (always 1 or 2). */
size_t
base64_encode_single(struct base64_encode_ctx *ctx,
uint8_t *dst,
char *dst,
uint8_t src);
/* Returns the number of output characters. DST should point to an
* area of size at least BASE64_ENCODE_LENGTH(length). */
size_t
base64_encode_update(struct base64_encode_ctx *ctx,
uint8_t *dst,
char *dst,
size_t length,
const uint8_t *src);
......@@ -104,18 +104,20 @@ base64_encode_update(struct base64_encode_ctx *ctx,
* BASE64_ENCODE_FINAL_LENGTH */
size_t
base64_encode_final(struct base64_encode_ctx *ctx,
uint8_t *dst);
char *dst);
/* Lower level functions */
/* Encodes a string in one go, including any padding at the end.
* Generates exactly BASE64_ENCODE_RAW_LENGTH(length) bytes of output.
* Supports overlapped operation, if src <= dst. */
* Supports overlapped operation, if src <= dst. FIXME: Use of overlap
* is deprecated, if needed there should be a separate public fucntion
* to do that.*/
void
base64_encode_raw(uint8_t *dst, size_t length, const uint8_t *src);
base64_encode_raw(char *dst, size_t length, const uint8_t *src);
void
base64_encode_group(uint8_t *dst, uint32_t group);
base64_encode_group(char *dst, uint32_t group);
/* Base64 decoding */
......@@ -147,7 +149,7 @@ base64url_decode_init(struct base64_decode_ctx *ctx);
int
base64_decode_single(struct base64_decode_ctx *ctx,
uint8_t *dst,
uint8_t src);
char src);
/* Returns 1 on success, 0 on error. DST should point to an area of
* size at least BASE64_DECODE_LENGTH(length). The amount of data
......@@ -157,7 +159,7 @@ base64_decode_update(struct base64_decode_ctx *ctx,
size_t *dst_length,
uint8_t *dst,
size_t src_length,
const uint8_t *src);
const char *src);
/* Returns 1 on success. */
int
......
......@@ -38,7 +38,7 @@
void
base64url_encode_init(struct base64_encode_ctx *ctx)
{
static const uint8_t base64url_encode_table[64] =
static const char base64url_encode_table[64] =
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz"
"0123456789-_";
......
......@@ -58,7 +58,7 @@ int
main(int argc UNUSED, char **argv UNUSED)
{
/* "buffer" will hold the bytes from disk: */
uint8_t * buffer = xalloc (CHUNK_SIZE);
char * buffer = xalloc (CHUNK_SIZE);
/* "result" will hold bytes before output: */
uint8_t * result = xalloc (DECODED_SIZE);
......
......@@ -70,7 +70,7 @@ main(int argc UNUSED, char **argv UNUSED)
/* "buffer" will hold the bytes from disk: */
uint8_t buffer[CHUNK_SIZE];
/* "result" will hold bytes before output: */
uint8_t result[ENCODED_SIZE + 1];
char result[ENCODED_SIZE + 1];
unsigned nbytes; /* Number of bytes read from stdin */
int encoded_bytes; /* Total number of bytes encoded per iteration */
......
......@@ -58,7 +58,7 @@ int
main(int argc UNUSED, char **argv UNUSED)
{
/* "buffer" will hold the bytes from disk: */
uint8_t * buffer = xalloc (CHUNK_SIZE);
char * buffer = xalloc (CHUNK_SIZE);
/* "result" will hold bytes before output: */
uint8_t * result = xalloc (DECODED_SIZE);
......
......@@ -72,7 +72,7 @@ main(int argc UNUSED, char **argv UNUSED)
/* "buffer" will hold the bytes from disk: */
uint8_t buffer[CHUNK_SIZE];
/* "result" is the result vector: */
uint8_t result[ENCODED_SIZE + BASE64_ENCODE_FINAL_LENGTH + 1];
char result[ENCODED_SIZE + BASE64_ENCODE_FINAL_LENGTH + 1];
unsigned nbytes; /* Number of bytes read from stdin */
int encoded_bytes; /* total number of bytes encoded per iteration */
nbytes = fread(buffer,1,CHUNK_SIZE,stdin);
......
......@@ -89,17 +89,17 @@ typedef size_t nettle_armor_length_func(size_t length);
typedef void nettle_armor_init_func(void *ctx);
typedef size_t nettle_armor_encode_update_func(void *ctx,
uint8_t *dst,
char *dst,
size_t src_length,
const uint8_t *src);
typedef size_t nettle_armor_encode_final_func(void *ctx, uint8_t *dst);
typedef size_t nettle_armor_encode_final_func(void *ctx, char *dst);
typedef int nettle_armor_decode_update_func(void *ctx,
size_t *dst_length,
uint8_t *dst,
size_t src_length,
const uint8_t *src);
const char *src);
typedef int nettle_armor_decode_final_func(void *ctx);
......
......@@ -371,8 +371,8 @@ pgp_armor(struct nettle_buffer *buffer,
length -= BINARY_PER_LINE, data += BINARY_PER_LINE)
{
unsigned done;
uint8_t *p
= nettle_buffer_space(buffer, TEXT_PER_LINE);
char *p
= (char *) nettle_buffer_space(buffer, TEXT_PER_LINE);
if (!p)
return 0;
......@@ -393,8 +393,8 @@ pgp_armor(struct nettle_buffer *buffer,
+ BASE64_ENCODE_FINAL_LENGTH;
unsigned done;
uint8_t *p
= nettle_buffer_space(buffer, text_size);
char *p
= (char *) nettle_buffer_space(buffer, text_size);
if (!p)
return 0;
......@@ -412,7 +412,7 @@ pgp_armor(struct nettle_buffer *buffer,
return 0;
{
uint8_t *p = nettle_buffer_space(buffer, 4);
char *p = (char *) nettle_buffer_space(buffer, 4);
if (!p)
return 0;
base64_encode_group(p, crc);
......
......@@ -40,6 +40,12 @@
#include "base64.h"
#include "buffer.h"
static inline void
base64_encode_in_place (size_t length, uint8_t *data)
{
base64_encode_raw ((char *) data, length, data);
}
size_t
sexp_transport_vformat(struct nettle_buffer *buffer,
const char *format, va_list args)
......@@ -68,8 +74,7 @@ sexp_transport_vformat(struct nettle_buffer *buffer,
if (!nettle_buffer_space(buffer, base64_length - length))
return 0;
base64_encode_raw(buffer->contents + start,
length, buffer->contents + start);
base64_encode_in_place(length, buffer->contents + start);
if (!NETTLE_BUFFER_PUTC(buffer, '}'))
return 0;
......
......@@ -84,7 +84,7 @@ sexp_transport_iterator_first(struct sexp_iterator *iterator,
base64_decode_init(&ctx);
if (base64_decode_update(&ctx, &coded_length, input + out,
end - in, input + in)
end - in, (const char*) (input + in))
&& base64_decode_final(&ctx))
{
out += coded_length;
......
......@@ -9,7 +9,7 @@ test_fuzz_once(struct base64_encode_ctx *encode,
{
size_t base64_len = BASE64_ENCODE_RAW_LENGTH (size);
size_t out_len;
uint8_t *base64 = xalloc (base64_len + 2);
char *base64 = xalloc (base64_len + 2);
uint8_t *decoded = xalloc (size + 2);
*base64++ = 0x12;
......@@ -66,6 +66,20 @@ test_fuzz(void)
}
}
static inline void
base64_encode_in_place (size_t length, uint8_t *data)
{
base64_encode_raw ((char *) data, length, data);
}
static inline int
base64_decode_in_place (struct base64_decode_ctx *ctx, size_t *dst_length,
size_t length, uint8_t *data)
{
return base64_decode_update (ctx, dst_length,
data, length, (const char *) data);
}
void
test_main(void)
{
......@@ -111,12 +125,12 @@ test_main(void)
size_t dst_length;
ASSERT(BASE64_ENCODE_RAW_LENGTH(5) == 8);
base64_encode_raw(buffer, 5, buffer);
base64_encode_in_place(5, buffer);
ASSERT(MEMEQ(9, buffer, "SGVsbG8=x"));
base64_decode_init(&ctx);
dst_length = 0; /* Output parameter only. */
ASSERT(base64_decode_update(&ctx, &dst_length, buffer, 8, buffer));
ASSERT(base64_decode_in_place(&ctx, &dst_length, 8, buffer));
ASSERT(dst_length == 5);
ASSERT(MEMEQ(9, buffer, "HelloG8=x"));
......
......@@ -62,7 +62,7 @@ tstring_clear(void)
}
struct tstring *
tstring_data(size_t length, const char *data)
tstring_data(size_t length, const uint8_t *data)
{
struct tstring *s = tstring_alloc (length);
memcpy (s->data, data, length);
......@@ -566,7 +566,7 @@ test_armor(const struct nettle_armor *armor,
const char *ascii)
{
size_t ascii_length = strlen(ascii);
uint8_t *buffer = xalloc(1 + ascii_length);
char *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);
......
......@@ -60,7 +60,7 @@ void
tstring_clear(void);
struct tstring *
tstring_data(size_t length, const char *data);
tstring_data(size_t length, const uint8_t *data);
struct tstring *
tstring_hex(const char *hex);
......@@ -286,7 +286,7 @@ test_ecc_mul_h (unsigned curve, unsigned n, const mp_limb_t *p);
#define LDUP(x) strlen(x), strdup(x)
#define SHEX(x) (tstring_hex(x))
#define SDATA(x) ((const struct tstring *)tstring_data(LLENGTH(x), x))
#define SDATA(x) ((const struct tstring *)tstring_data(LLENGTH(x), US(x)))
#define H(x) (SHEX(x)->data)
#define MEMEQ(length, a, b) (!memcmp((a), (b), (length)))
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment