? patch Index: .cvsignore =================================================================== RCS file: /cvsroot/lsh/lsh/src/nettle/.cvsignore,v retrieving revision 1.6 diff -u -r1.6 .cvsignore --- .cvsignore 12 Jan 2002 14:36:02 -0000 1.6 +++ .cvsignore 3 Feb 2002 03:37:22 -0000 @@ -18,7 +18,7 @@ nettle.dvi nettle.fn nettle.html -nettle.info +nettle.info* nettle.ky nettle.log nettle.pg Index: Makefile.am =================================================================== RCS file: /cvsroot/lsh/lsh/src/nettle/Makefile.am,v retrieving revision 1.27 diff -u -r1.27 Makefile.am --- Makefile.am 22 Jan 2002 16:31:19 -0000 1.27 +++ Makefile.am 3 Feb 2002 03:37:22 -0000 @@ -5,7 +5,7 @@ libnettleincludedir = $(includedir)/nettle lib_LIBRARIES = libnettle.a -libnettleinclude_HEADERS = aes.h arcfour.h blowfish.h +libnettleinclude_HEADERS = aes.h arcfour.h blowfish.h \ base64.h cast128.h \ cbc.h \ des.h des-compat.h \ Index: base64.c =================================================================== RCS file: /cvsroot/lsh/lsh/src/nettle/base64.c,v retrieving revision 1.1 diff -u -r1.1 base64.c --- base64.c 22 Jan 2002 16:30:31 -0000 1.1 +++ base64.c 3 Feb 2002 03:37:22 -0000 @@ -94,38 +94,42 @@ return out - dst; } +void +base64_decode_init(struct base64_ctx *ctx) +{ + ctx->shift = 10; + ctx->accum = 0; +} + unsigned -base64_decode(uint8_t *dst, - unsigned src_length, - const uint8_t *src) +base64_decode_update(struct base64_ctx *ctx, + uint8_t *dst, + unsigned src_length, + const uint8_t *src) { uint8_t *out = dst; - uint32_t accum = 0; - int shift = 10; - while (src_length > 0) + for (;;) { - const int data = decode_table[*src]; + int data; + if (src_length == 0) return out - dst; + data = decode_table[*src]; switch (data) { default: - accum |= data << shift; - shift -= 6; - if (shift <= 2) + ctx->accum |= data << ctx->shift; + ctx->shift -= 6; + if (ctx->shift <= 2) { - *out++ = accum >> 8; - accum = accum << 8; - shift += 8; + *out++ = ctx->accum >> 8; + ctx->accum <<= 8; + ctx->shift += 8; } case TABLE_INVALID: case TABLE_SPACE: case TABLE_END: - break; + ++src; + --src_length; } - - ++src; - --src_length; } - - return out - dst; } Index: base64.h =================================================================== RCS file: /cvsroot/lsh/lsh/src/nettle/base64.h,v retrieving revision 1.2 diff -u -r1.2 base64.h --- base64.h 22 Jan 2002 16:36:23 -0000 1.2 +++ base64.h 3 Feb 2002 03:37:22 -0000 @@ -1,4 +1,4 @@ -/* armor.h +/* base64.h * * "ASCII armor" codecs. */ @@ -23,24 +23,43 @@ * MA 02111-1307, USA. */ -#ifndef NETTLE_ARMOR_H_INCLUDED -#define NETTLE_ARMOR_H_INCLUDED +#ifndef NETTLE_BASE64_H_INCLUDED +#define NETTLE_BASE64_H_INCLUDED #include /* Base64 encoding */ -#define BASE64_ASCII_BLOCK_SIZE 4 -#define BASE64_RAW_BLOCK_SIZE 3 +#define BASE64_BINARY_BLOCK_SIZE 3 +#define BASE64_TEXT_BLOCK_SIZE 4 unsigned /* Returns the length of encoded data */ base64_encode(uint8_t *dst, unsigned src_length, const uint8_t *src); +/* Precise length of encoded data (including padding) */ +#define BASE64_ENCODE_LENGTH(src_length) \ + ((BASE64_BINARY_BLOCK_SIZE - 1 + (src_length)) \ + / BASE64_BINARY_BLOCK_SIZE * BASE64_TEXT_BLOCK_SIZE) + +struct base64_ctx /* Internal, do not modify */ +{ + uint16_t accum; /* Partial byte accumulated so far, filled msb first */ + int16_t shift; /* Bitshift for the next 6-bit segment added to buffer */ +}; + +void +base64_decode_init(struct base64_ctx *ctx); + unsigned /* Returns the length of decoded data */ -base64_decode(uint8_t *dst, - unsigned src_length, - const uint8_t *src); +base64_decode_update(struct base64_ctx *ctx, + uint8_t *dst, + unsigned src_length, + const uint8_t *src); + +/* Maximum length of decoded data */ +#define BASE64_DECODE_LENGTH(src_length) \ + ((src_length) * BASE64_BINARY_BLOCK_SIZE / BASE64_TEXT_BLOCK_SIZE) -#endif /* NETTLE_ARMOR_H_INCLUDED */ +#endif /* NETTLE_BASE64_H_INCLUDED */ Index: configure.in =================================================================== RCS file: /cvsroot/lsh/lsh/src/nettle/configure.in,v retrieving revision 1.9 diff -u -r1.9 configure.in --- configure.in 31 Jan 2002 09:57:46 -0000 1.9 +++ configure.in 3 Feb 2002 03:37:22 -0000 @@ -5,6 +5,8 @@ AC_INIT(arcfour.c) +AC_PREREQ(2.50) dnl This surely isn't right. What is? + AM_INIT_AUTOMAKE(nettle, 1.5) AM_CONFIG_HEADER(config.h) Index: nettle-meta.h =================================================================== RCS file: /cvsroot/lsh/lsh/src/nettle/nettle-meta.h,v retrieving revision 1.3 diff -u -r1.3 nettle-meta.h --- nettle-meta.h 22 Jan 2002 16:32:08 -0000 1.3 +++ nettle-meta.h 3 Feb 2002 03:37:22 -0000 @@ -126,30 +126,37 @@ /* ASCII armor codecs */ -typedef unsigned (*nettle_armor_func)(uint8_t *dst, - unsigned src_length, - const uint8_t *src); +typedef unsigned (*nettle_armor_encode_func)(uint8_t *dst, + unsigned src_length, + const uint8_t *src); +typedef void (*nettle_armor_init_func)(void *ctx); +typedef unsigned (*nettle_armor_update_func)(void *ctx, + uint8_t *dst, + unsigned src_length, + const uint8_t *src); struct nettle_armor { const char *name; - - unsigned ascii_block_size; - unsigned raw_block_size; - - nettle_armor_func encode; - nettle_armor_func decode; + unsigned context_size; + unsigned text_block_size; + unsigned binary_block_size; + nettle_armor_encode_func encode; + nettle_armor_init_func decode_init; + nettle_armor_update_func decode_update; }; -#define _NETTLE_ARMOR(name, NAME) { \ - #name, \ - NAME##_ASCII_BLOCK_SIZE, \ - NAME##_RAW_BLOCK_SIZE, \ - name##_encode, \ - name##_decode \ +#define _NETTLE_ARMOR(name, NAME) { \ + #name, \ + sizeof(struct name##_ctx), \ + NAME##_TEXT_BLOCK_SIZE, \ + NAME##_BINARY_BLOCK_SIZE, \ + (nettle_armor_encode_func) name##_encode, \ + (nettle_armor_init_func) name##_decode_init, \ + (nettle_armor_update_func) name##_decode_update \ } extern const struct nettle_armor nettle_base64; -extern const struct nettle_armor nettle_base16; +/* extern const struct nettle_armor nettle_base16; */ #endif /* NETTLE_META_H_INCLUDED */ Index: testsuite/testutils.c =================================================================== RCS file: /cvsroot/lsh/lsh/src/nettle/testsuite/testutils.c,v retrieving revision 1.8 diff -u -r1.8 testutils.c --- testsuite/testutils.c 22 Jan 2002 16:34:20 -0000 1.8 +++ testsuite/testutils.c 3 Feb 2002 03:37:23 -0000 @@ -228,11 +228,12 @@ const uint8_t *data, const uint8_t *ascii) { + void *ctx = alloca(armor->context_size); uint8_t *buffer = alloca(1 + strlen(ascii)); uint8_t *check = alloca(1 + data_length); - memset(buffer, 0, 1 + strlen(ascii)); - memset(check, 0, 1 + data_length); + memset(buffer, 0x33, 1 + strlen(ascii)); + memset(check, 0x55, 1 + data_length); if (strlen(ascii) != armor->encode(buffer, data_length, data)) FAIL(); @@ -240,16 +241,17 @@ if (!MEMEQ(strlen(ascii), buffer, ascii)) FAIL(); - if (buffer[strlen(ascii)]) + if (0x33 != buffer[strlen(ascii)]) FAIL(); - if (data_length != armor->decode(check, strlen(ascii), buffer)) + armor->decode_init(ctx); + if (data_length != armor->decode_update(ctx, check, strlen(ascii), buffer)) FAIL(); if (!MEMEQ(data_length, check, data)) FAIL(); - if (check[data_length]) + if (0x55 != check[data_length]) FAIL(); }