comparison src/c/openssl.c @ 1368:b2bc8bcd546f

Switch from libmhash to OpenSSL
author Adam Chlipala <adam@chlipala.net>
date Sun, 26 Dec 2010 11:13:37 -0500
parents src/c/mhash.c@236dc296c32d
children 882556b3029d
comparison
equal deleted inserted replaced
1367:8301ee4ab58c 1368:b2bc8bcd546f
1 #include "config.h"
2
3 #include <stdlib.h>
4 #include <unistd.h>
5 #include <sys/types.h>
6 #include <sys/stat.h>
7 #include <fcntl.h>
8 #include <stdio.h>
9 #include <string.h>
10
11 #include <openssl/sha.h>
12
13 #define PASSSIZE 4
14
15 int uw_hash_blocksize = 32;
16
17 static int password[PASSSIZE];
18
19 char *uw_sig_file = NULL;
20
21 static void random_password() {
22 int i;
23
24 for (i = 0; i < PASSSIZE; ++i)
25 password[i] = rand();
26 }
27
28 void uw_init_crypto() {
29 if (uw_sig_file) {
30 int fd;
31
32 if (access(uw_sig_file, F_OK)) {
33 random_password();
34
35 if ((fd = open(uw_sig_file, O_WRONLY | O_CREAT, 0700)) < 0) {
36 fprintf(stderr, "Can't open signature file %s\n", uw_sig_file);
37 perror("open");
38 exit(1);
39 }
40
41 if (write(fd, &password, sizeof password) != sizeof password) {
42 fprintf(stderr, "Error writing signature file\n");
43 exit(1);
44 }
45
46 close(fd);
47 } else {
48 if ((fd = open(uw_sig_file, O_RDONLY)) < 0) {
49 fprintf(stderr, "Can't open signature file %s\n", uw_sig_file);
50 perror("open");
51 exit(1);
52 }
53
54 if (read(fd, &password, sizeof password) != sizeof password) {
55 fprintf(stderr, "Error reading signature file\n");
56 exit(1);
57 }
58
59 close(fd);
60 }
61 } else
62 random_password();
63 }
64
65 void uw_sign(const char *in, unsigned char *out) {
66 SHA256_CTX c;
67
68 SHA256_Init(&c);
69 SHA256_Update(&c, password, sizeof password);
70 SHA256_Update(&c, in, strlen(in));
71 SHA256_Final(out, &c);
72 }