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

Small improvements to rsa-encrypt and rsa-decrypt example programs.

parent 27344436
2012-04-14 Niels Möller <nisse@lysator.liu.se> 2012-04-14 Niels Möller <nisse@lysator.liu.se>
* examples/rsa-encrypt.c (process_file): Reorganized fread loop.
(usage): New function.
(main): Implemented --help option.
* examples/rsa-decrypt.c (process_file): Improved error message
for too short input file.
* aes-set-decrypt-key.c (gf2_log, gf2_exp): Deleted tables. * aes-set-decrypt-key.c (gf2_log, gf2_exp): Deleted tables.
(mult, inv_mix_column): Deleted functions. (mult, inv_mix_column): Deleted functions.
(mtable): New table. (mtable): New table.
......
...@@ -123,9 +123,12 @@ process_file(struct rsa_session *ctx, ...@@ -123,9 +123,12 @@ process_file(struct rsa_session *ctx,
unsigned padding; unsigned padding;
size = fread(buffer, 1, BUF_FINAL, in); size = fread(buffer, 1, BUF_FINAL, in);
if (size < BUF_FINAL || ferror(in)) if (size < BUF_FINAL)
{ {
werror("Reading input failed: %s\n", strerror(errno)); if (ferror(in))
werror("Reading input failed: %s\n", strerror(errno));
else
werror("Unexpected EOF on input.\n");
return 0; return 0;
} }
...@@ -133,7 +136,7 @@ process_file(struct rsa_session *ctx, ...@@ -133,7 +136,7 @@ process_file(struct rsa_session *ctx,
{ {
size = fread(buffer + BUF_FINAL, 1, BUF_SIZE, in); size = fread(buffer + BUF_FINAL, 1, BUF_SIZE, in);
if (ferror(in)) if (size < BUF_SIZE && ferror(in))
{ {
werror("Reading input failed: %s\n", strerror(errno)); werror("Reading input failed: %s\n", strerror(errno));
return 0; return 0;
......
...@@ -26,6 +26,7 @@ ...@@ -26,6 +26,7 @@
# include "config.h" # include "config.h"
#endif #endif
#include <assert.h>
#include <ctype.h> #include <ctype.h>
#include <errno.h> #include <errno.h>
#include <stdlib.h> #include <stdlib.h>
...@@ -93,35 +94,54 @@ write_bignum(FILE *f, mpz_t x) ...@@ -93,35 +94,54 @@ write_bignum(FILE *f, mpz_t x)
return res; return res;
} }
#define BLOCK_SIZE (AES_BLOCK_SIZE * 100)
static int static int
process_file(struct rsa_session *ctx, process_file(struct rsa_session *ctx,
FILE *in, FILE *out) FILE *in, FILE *out)
{ {
uint8_t buffer[AES_BLOCK_SIZE * 100]; uint8_t buffer[BLOCK_SIZE + SHA1_DIGEST_SIZE];
unsigned leftover;
unsigned padding;
padding = leftover = 0;
for (;;) for (;;)
{ {
size_t size = fread(buffer, 1, sizeof(buffer), in); size_t size = fread(buffer, 1, BLOCK_SIZE, in);
if (ferror(in))
{
werror("Reading input failed: %s\n", strerror(errno));
return 0;
}
hmac_sha1_update(&ctx->hmac, size, buffer); hmac_sha1_update(&ctx->hmac, size, buffer);
if (size < sizeof(buffer))
if (size < BLOCK_SIZE)
{ {
/* Setting padding != ends the loop */ unsigned leftover;
unsigned padding;
if (ferror(in))
{
werror("Reading input failed: %s\n", strerror(errno));
return 0;
}
leftover = size % AES_BLOCK_SIZE; leftover = size % AES_BLOCK_SIZE;
padding = AES_BLOCK_SIZE - leftover; padding = AES_BLOCK_SIZE - leftover;
size -= leftover;
if (!size) assert (size + padding <= BLOCK_SIZE);
break;
if (padding > 1)
yarrow256_random(&ctx->yarrow, padding - 1, buffer + size);
size += padding;
buffer[size - 1] = padding;
CBC_ENCRYPT(&ctx->aes, aes_encrypt, size, buffer, buffer);
assert (size + SHA1_DIGEST_SIZE <= sizeof(buffer));
hmac_sha1_digest(&ctx->hmac, SHA1_DIGEST_SIZE, buffer + size);
size += SHA1_DIGEST_SIZE;
if (!write_string(out, size, buffer))
{
werror("Writing output failed: %s\n", strerror(errno));
return 0;
}
return 1;
} }
CBC_ENCRYPT(&ctx->aes, aes_encrypt, size, buffer, buffer); CBC_ENCRYPT(&ctx->aes, aes_encrypt, size, buffer, buffer);
...@@ -130,29 +150,16 @@ process_file(struct rsa_session *ctx, ...@@ -130,29 +150,16 @@ process_file(struct rsa_session *ctx,
werror("Writing output failed: %s\n", strerror(errno)); werror("Writing output failed: %s\n", strerror(errno));
return 0; return 0;
} }
if (padding)
{
if (leftover)
memcpy(buffer, buffer + size, leftover);
break;
}
}
if (padding > 1)
yarrow256_random(&ctx->yarrow, padding - 1, buffer + leftover);
buffer[AES_BLOCK_SIZE - 1] = padding;
CBC_ENCRYPT(&ctx->aes, aes_encrypt, AES_BLOCK_SIZE, buffer, buffer);
hmac_sha1_digest(&ctx->hmac, SHA1_DIGEST_SIZE, buffer + AES_BLOCK_SIZE);
if (!write_string(out, AES_BLOCK_SIZE + SHA1_DIGEST_SIZE, buffer))
{
werror("Writing output failed: %s\n", strerror(errno));
return 0;
} }
}
return 1; static void
usage (FILE *out)
{
fprintf (out, "Usage: rsa-encrypt [OPTIONS] PUBLIC-KEY < cleartext\n"
"Options:\n"
" -r, --random=FILE seed file for randomness generator\n"
" --help display this help\n");
} }
int int
...@@ -167,8 +174,17 @@ main(int argc, char **argv) ...@@ -167,8 +174,17 @@ main(int argc, char **argv)
int c; int c;
const char *random_name = NULL; const char *random_name = NULL;
/* FIXME: --help option. */ enum { OPT_HELP = 300 };
while ( (c = getopt(argc, argv, "o:r:")) != -1)
static const struct option options[] =
{
/* Name, args, flag, val */
{ "help", no_argument, NULL, OPT_HELP },
{ "random", required_argument, NULL, 'r' },
{ NULL, 0, NULL, 0}
};
while ( (c = getopt_long(argc, argv, "o:r:", options, NULL)) != -1)
switch (c) switch (c)
{ {
case 'r': case 'r':
...@@ -178,6 +194,9 @@ main(int argc, char **argv) ...@@ -178,6 +194,9 @@ main(int argc, char **argv)
case '?': case '?':
return EXIT_FAILURE; return EXIT_FAILURE;
case OPT_HELP:
usage(stdout);
return EXIT_SUCCESS;
default: default:
abort(); abort();
} }
...@@ -187,7 +206,7 @@ main(int argc, char **argv) ...@@ -187,7 +206,7 @@ main(int argc, char **argv)
if (argc != 1) if (argc != 1)
{ {
werror("Usage: rsa-encrypt [-r random-file] PUBLIC-KEY < cleartext\n"); usage (stderr);
return EXIT_FAILURE; return EXIT_FAILURE;
} }
......
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