Skip to content
Snippets Groups Projects
Commit 0c57a71a authored by Niels Möller's avatar Niels Möller
Browse files

* examples/rsa-keygen.c: Use functions from io.c.

* examples/rsa-sign.c: Likewise.

Rev: src/nettle/examples/rsa-sign.c:1.2
parent 129d8042
No related branches found
No related tags found
No related merge requests found
...@@ -26,7 +26,7 @@ ...@@ -26,7 +26,7 @@
# include "config.h" # include "config.h"
#endif /* HAVE_CONFIG_H */ #endif /* HAVE_CONFIG_H */
#if !HAVE_LIBGMP #if !WITH_PUBLIC_KEY
int int
main(int argc, char **argv) main(int argc, char **argv)
{ {
...@@ -35,54 +35,15 @@ main(int argc, char **argv) ...@@ -35,54 +35,15 @@ main(int argc, char **argv)
"and recompile Nettle\n"); "and recompile Nettle\n");
return EXIT_FAILURE; return EXIT_FAILURE;
} }
#endif /* !HAVE_LIBGMP */ #else /* WITH_PUBLIC_KEY */
#include <errno.h> #include <errno.h>
#include <stdlib.h> #include <stdlib.h>
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include "rsa.h" #include "rsa.h"
#include "io.h"
#define BUFSIZE 1000
static int
read_key(const char *name,
struct rsa_private_key *key)
{
uint8_t buffer[BUFSIZE];
unsigned done;
int fd = open(name, O_RDONLY);
if (fd < 0)
{
fprintf(stderr, "Failed to open `%s': %s\n",
name, strerror(errno));
return 0;
}
for (done = 0; done < sizeof(buffer) ;)
{
int res = read(fd, buffer, sizeof(buffer) - done);
if (!res)
break;
else if (res < 0 && errno == EINTR)
continue;
else if (res < 0)
{
fprintf(stderr, "Failed reading `%s': %s\n",
name, strerror(errno));
return 0;
}
else
done += res;
}
return rsa_keypair_from_sexp(NULL, key,
done, buffer);
}
int int
main(int argc, char **argv) main(int argc, char **argv)
...@@ -99,31 +60,19 @@ main(int argc, char **argv) ...@@ -99,31 +60,19 @@ main(int argc, char **argv)
rsa_init_private_key(&key); rsa_init_private_key(&key);
if (!read_key(argv[1], &key)) if (!read_rsa_key(argv[1], NULL, &key))
{ {
fprintf(stderr, "Invalid key\n"); fprintf(stderr, "Invalid key\n");
return EXIT_FAILURE; return EXIT_FAILURE;
} }
sha1_init(&hash); sha1_init(&hash);
for (;;) if (!hash_file(&nettle_sha1, &hash, stdin))
{
uint8_t buffer[BUFSIZE];
int res = read(STDIN_FILENO, buffer, sizeof(buffer));
if (!res)
/* EOF */
break;
else if (res < 0 && errno == EINTR)
continue;
else if (res < 0)
{ {
fprintf(stderr, "Failed reading stdin: %s\n", fprintf(stderr, "Failed reading stdin: %s\n",
strerror(errno)); strerror(errno));
return 0; return 0;
} }
else
sha1_update(&hash, res, buffer);
}
mpz_init(s); mpz_init(s);
rsa_sha1_sign(&key, &hash, s); rsa_sha1_sign(&key, &hash, s);
...@@ -142,3 +91,4 @@ main(int argc, char **argv) ...@@ -142,3 +91,4 @@ main(int argc, char **argv)
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }
#endif /* WITH_PUBLIC_KEY */
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment