Debian Codegraph @

rmt/cipher.c

raw symbols SCIP
1 #include "config.h" // IWYU pragma: keep
2 #include <sys/types.h>
3 #include <fcntl.h>
4 #include <unistd.h>
5 #include <stdlib.h>
6 #include <stdio.h>
7 #include <syslog.h>
8 #include <string.h>
9 #include <strings.h>
10 #include <errno.h>
11 #include <openssl/evp.h>
12
13 char *cipher (char *, int, int);
14 int decrypt(void);
15
16 /*
17 * Encrypt or decrypt buf, returning a pointer to the transformed data,
18 * or NULL on error.
19 * The returned data is the same size as the input data,
20 * which means we must turn off cipher padding, and require that buflen
21 * be a multiple of the cipher block size (8 for Blowfish).
22 * To keep things simple, the return value is a malloc'd
23 * buffer that is overwritten on each call.
24 *
25 * Ken Lalonde <[email protected]>, 2003
26 */
27 char *
28 cipher(char *buf, int buflen, int do_encrypt)
29 {
30 EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new();
31 static unsigned char *out = NULL; /* return value, grown as necessary */
32 static int outlen = 0;
33 static int init = 0, which, blocksize;
34 int n;
35
36 if (!init) {
37 static const EVP_CIPHER *cipher;
38 unsigned char key[EVP_MAX_KEY_LENGTH], iv[EVP_MAX_IV_LENGTH];
39 // Read key from $HOME/.ermt.key
40 const char *keyfile = ".ermt.key";
41 unsigned char buf[128];
42 FILE *fp;
43 int i;
44
45 openlog("ermt", LOG_PID, LOG_DAEMON);
46 // Not needed: OpenSSL_add_all_algorithms();
47 // Not needed: ERR_load_crypto_strings();
48 cipher = EVP_bf_cbc(); // or: EVP_aes_{128,192,256}_cbc()
49 // We want the ability to decrypt the output
50 // using "openssl enc -d -kfile K -nopad -nosalt", which
51 // means we must read the key file the same way
52 // openssl does. But be careful: if the key contains
53 // \0 or \r or \n, the effective size will be reduced.
54 if ((fp = fopen(keyfile, "r")) == NULL) {
55 syslog(LOG_ERR, "Can't open key file %s: %m", keyfile);
56 return NULL;
57 }
58 buf[0] = '\0';
59 if (!fgets((char*)buf, sizeof buf, fp)) {
60 syslog(LOG_ERR, "Error reading key file %s: %m", keyfile);
61 return NULL;
62 }
63 fclose(fp);
64 i = strlen((char*)buf);
65 if ((i > 0) &&
66 ((buf[i-1] == '\n') || (buf[i-1] == '\r')))
67 buf[--i]='\0';
68 if ((i > 0) &&
69 ((buf[i-1] == '\n') || (buf[i-1] == '\r')))
70 buf[--i]='\0';
71 if (i < 1) {
72 syslog(LOG_ERR, "zero length key");
73 errno = EINVAL;
74 return NULL;
75 }
76 EVP_BytesToKey(cipher, EVP_md5(), NULL,
77 buf, strlen((char*)buf), 1, key, iv);
78 EVP_CIPHER_CTX_init(ctx);
79 EVP_CipherInit_ex(ctx, cipher, NULL, key, iv, do_encrypt);
80 EVP_CIPHER_CTX_set_padding(ctx, 0); // -nopad
81 OPENSSL_cleanse(buf, sizeof buf);
82 OPENSSL_cleanse(key, sizeof key);
83 OPENSSL_cleanse(iv, sizeof iv);
84 blocksize = EVP_CIPHER_CTX_block_size(ctx);
85 which = do_encrypt;
86 init = 1;
87 }
88 if (which != do_encrypt) {
89 syslog(LOG_ERR, "Cannot switch modes");
90 errno = EINVAL;
91 return NULL;
92 }
93 if ((buflen % blocksize) != 0) {
94 syslog(LOG_ERR, "Buffer size is not a multiple of cipher block size");
95 errno = EINVAL;
96 return NULL;
97 }
98 if (outlen < buflen+blocksize) {
99 outlen = (buflen+blocksize) * 2;
100 out = realloc(out, outlen);
101 }
102 if (!EVP_CipherUpdate(ctx, out, &n, (unsigned char*)buf, buflen)) {
103 syslog(LOG_ERR, "EVP_CipherUpdate failed");
104 errno = EINVAL;
105 return NULL;
106 }
107 if (n != buflen) {
108 syslog(LOG_ERR, "EVP_CipherUpdate: %d != %d", n, buflen);
109 errno = EINVAL;
110 return NULL;
111 }
112 // assert(ctx->buf_len == 0);
113 EVP_CIPHER_CTX_free(ctx);
114 return (char*)out;
115 }
116
117 /* Decrypt stdin to stdout, and exit */
118 int
119 decrypt(void)
120 {
121 char buf[8*1024], *cp;
122 int n;
123
124 while ((n = fread(buf, 1, sizeof buf, stdin)) > 0) {
125 if ((cp = cipher(buf, n, 0)) == NULL) {
126 fprintf(stderr, "ermt: Error decoding input; see daemon.err syslog\n");
127 exit(1);
128 }
129 fwrite(cp, 1, n, stdout);
130 }
131 if (ferror(stdin)) {
132 perror("ermt: stdin: read");
133 exit(1);
134 }
135 if (fflush(stdout)) {
136 perror("ermt: stdout: write");
137 exit(1);
138 }
139 exit(0);
140 }