annotate src/c/openssl.c @ 2239:f70a91f7810d

Add limited support for parsing SQL arithmetic.
author Ziv Scully <ziv@mit.edu>
date Tue, 07 Jul 2015 00:07:24 -0700
parents 882556b3029d
children 8a01e8f21de9
rev   line source
adamc@1268 1 #include "config.h"
adamc@1268 2
adam@1368 3 #include <stdlib.h>
adam@1368 4 #include <unistd.h>
adam@1368 5 #include <sys/types.h>
adam@1368 6 #include <sys/stat.h>
adamc@1145 7 #include <fcntl.h>
adam@1368 8 #include <stdio.h>
adam@1368 9 #include <string.h>
adamc@1104 10
adam@1368 11 #include <openssl/sha.h>
adam@2105 12 #include <openssl/rand.h>
adam@1368 13
adamc@1104 14 #define PASSSIZE 4
adamc@1104 15
adam@1368 16 int uw_hash_blocksize = 32;
adamc@1104 17
adamc@1104 18 static int password[PASSSIZE];
adamc@1104 19
adamc@1145 20 char *uw_sig_file = NULL;
adamc@1145 21
adamc@1145 22 static void random_password() {
adam@2105 23 if (!RAND_bytes((unsigned char *)password, sizeof password)) {
adam@2105 24 fprintf(stderr, "Error generating random password\n");
adam@2105 25 perror("RAND_bytes");
adam@2105 26 exit(1);
adam@2105 27 }
adamc@1145 28 }
adamc@1145 29
adamc@1104 30 void uw_init_crypto() {
adamc@1145 31 if (uw_sig_file) {
adamc@1145 32 int fd;
adamc@1145 33
adamc@1155 34 if (access(uw_sig_file, F_OK)) {
adamc@1145 35 random_password();
adamc@1145 36
adamc@1145 37 if ((fd = open(uw_sig_file, O_WRONLY | O_CREAT, 0700)) < 0) {
adamc@1145 38 fprintf(stderr, "Can't open signature file %s\n", uw_sig_file);
adamc@1145 39 perror("open");
adamc@1145 40 exit(1);
adamc@1145 41 }
adamc@1145 42
adamc@1145 43 if (write(fd, &password, sizeof password) != sizeof password) {
adamc@1145 44 fprintf(stderr, "Error writing signature file\n");
adamc@1145 45 exit(1);
adamc@1145 46 }
adamc@1145 47
adamc@1145 48 close(fd);
adamc@1145 49 } else {
adamc@1145 50 if ((fd = open(uw_sig_file, O_RDONLY)) < 0) {
adamc@1145 51 fprintf(stderr, "Can't open signature file %s\n", uw_sig_file);
adamc@1145 52 perror("open");
adamc@1145 53 exit(1);
adamc@1145 54 }
adamc@1145 55
adamc@1145 56 if (read(fd, &password, sizeof password) != sizeof password) {
adamc@1145 57 fprintf(stderr, "Error reading signature file\n");
adamc@1145 58 exit(1);
adamc@1145 59 }
adamc@1145 60
adamc@1145 61 close(fd);
adamc@1145 62 }
adamc@1145 63 } else
adamc@1145 64 random_password();
adamc@1104 65 }
adamc@1104 66
adam@1368 67 void uw_sign(const char *in, unsigned char *out) {
adam@1368 68 SHA256_CTX c;
adamc@1104 69
adam@1368 70 SHA256_Init(&c);
adam@1368 71 SHA256_Update(&c, password, sizeof password);
adam@1368 72 SHA256_Update(&c, in, strlen(in));
adam@1368 73 SHA256_Final(out, &c);
adamc@1104 74 }