summaryrefslogtreecommitdiff
path: root/src/c/openssl.c
diff options
context:
space:
mode:
authorGravatar Adam Chlipala <adam@chlipala.net>2010-12-26 11:13:37 -0500
committerGravatar Adam Chlipala <adam@chlipala.net>2010-12-26 11:13:37 -0500
commitbc7000ce141d30e4db4842edd8fa067ac1ec191d (patch)
treed59424249e21ce243abae8ec042deb0fdeccb3d7 /src/c/openssl.c
parenta090c56aad2934d397d23df2a7b17156b12695a1 (diff)
Switch from libmhash to OpenSSL
Diffstat (limited to 'src/c/openssl.c')
-rw-r--r--src/c/openssl.c72
1 files changed, 72 insertions, 0 deletions
diff --git a/src/c/openssl.c b/src/c/openssl.c
new file mode 100644
index 00000000..6a998e29
--- /dev/null
+++ b/src/c/openssl.c
@@ -0,0 +1,72 @@
+#include "config.h"
+
+#include <stdlib.h>
+#include <unistd.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <string.h>
+
+#include <openssl/sha.h>
+
+#define PASSSIZE 4
+
+int uw_hash_blocksize = 32;
+
+static int password[PASSSIZE];
+
+char *uw_sig_file = NULL;
+
+static void random_password() {
+ int i;
+
+ for (i = 0; i < PASSSIZE; ++i)
+ password[i] = rand();
+}
+
+void uw_init_crypto() {
+ if (uw_sig_file) {
+ int fd;
+
+ if (access(uw_sig_file, F_OK)) {
+ random_password();
+
+ if ((fd = open(uw_sig_file, O_WRONLY | O_CREAT, 0700)) < 0) {
+ fprintf(stderr, "Can't open signature file %s\n", uw_sig_file);
+ perror("open");
+ exit(1);
+ }
+
+ if (write(fd, &password, sizeof password) != sizeof password) {
+ fprintf(stderr, "Error writing signature file\n");
+ exit(1);
+ }
+
+ close(fd);
+ } else {
+ if ((fd = open(uw_sig_file, O_RDONLY)) < 0) {
+ fprintf(stderr, "Can't open signature file %s\n", uw_sig_file);
+ perror("open");
+ exit(1);
+ }
+
+ if (read(fd, &password, sizeof password) != sizeof password) {
+ fprintf(stderr, "Error reading signature file\n");
+ exit(1);
+ }
+
+ close(fd);
+ }
+ } else
+ random_password();
+}
+
+void uw_sign(const char *in, unsigned char *out) {
+ SHA256_CTX c;
+
+ SHA256_Init(&c);
+ SHA256_Update(&c, password, sizeof password);
+ SHA256_Update(&c, in, strlen(in));
+ SHA256_Final(out, &c);
+}