signify.c (5141B)
1 /* see LICENSE file for copyright and license details */ 2 #include <fcntl.h> 3 #include <stdio.h> 4 #include <string.h> 5 #include <unistd.h> 6 7 #include "../base64.h" 8 #include "../sha512.h" 9 #include "../signify.h" 10 #include "../util.h" 11 12 #define COMMENT "untrusted comment: " 13 14 /* the layouts signify writes, byte for byte */ 15 struct pubfile { 16 uint8_t alg[2]; 17 uint8_t keynum[8]; 18 uint8_t pk[ED25519_PUBLIC]; 19 }; 20 21 struct secfile { 22 uint8_t alg[2]; 23 uint8_t kdfalg[2]; 24 uint8_t rounds[4]; 25 uint8_t salt[16]; 26 uint8_t checksum[8]; 27 uint8_t keynum[8]; 28 uint8_t sk[ED25519_SECRET]; 29 }; 30 31 struct sigfile { 32 uint8_t alg[2]; 33 uint8_t keynum[8]; 34 uint8_t sig[ED25519_SIG]; 35 }; 36 37 static int 38 randombytes(uint8_t *buf, size_t n) 39 { 40 int fd = open("/dev/urandom", O_RDONLY); 41 ssize_t r; 42 size_t got = 0; 43 44 if (fd < 0) { 45 weprintf("open /dev/urandom:"); 46 return -1; 47 } 48 while (got < n && (r = read(fd, buf + got, n - got)) > 0) 49 got += (size_t)r; 50 close(fd); 51 if (got < n) { 52 weprintf("read /dev/urandom:"); 53 return -1; 54 } 55 return 0; 56 } 57 58 /* comment and blob into path, which must not be there yet */ 59 static int 60 writefile(const char *path, const char *comment, const void *blob, size_t n, int mode) 61 { 62 char b64[256]; 63 FILE *fp; 64 int fd; 65 66 if ((fd = open(path, O_WRONLY | O_CREAT | O_EXCL, mode)) < 0) { 67 weprintf("open %s:", path); 68 return -1; 69 } 70 if (!(fp = fdopen(fd, "w"))) { 71 close(fd); 72 return -1; 73 } 74 base64_encode(b64, blob, n); 75 fprintf(fp, COMMENT "%s\n%s\n", comment, b64); 76 return fshut(fp, path) ? -1 : 0; 77 } 78 79 /* the blob of path's second line, exactly n bytes of it */ 80 static int 81 readfile(const char *path, void *blob, size_t n) 82 { 83 char line[512]; 84 FILE *fp; 85 int ok; 86 87 if (!(fp = fopen(path, "r"))) { 88 weprintf("open %s:", path); 89 return -1; 90 } 91 ok = fgets(line, sizeof(line), fp) && !strncmp(line, COMMENT, strlen(COMMENT)) 92 && fgets(line, sizeof(line), fp) && base64_decode(blob, n, line, strlen(line)) == (long)n; 93 fclose(fp); 94 if (!ok) { 95 weprintf("%s: not a signify file of the right kind\n", path); 96 return -1; 97 } 98 if (memcmp(blob, "Ed", 2)) { 99 weprintf("%s: not an ed25519 key or signature\n", path); 100 return -1; 101 } 102 return 0; 103 } 104 105 static void 106 checksum(uint8_t out[8], const uint8_t sk[ED25519_SECRET]) 107 { 108 struct sha512 h; 109 uint8_t d[SHA512_DIGEST_LENGTH]; 110 111 sha512_init(&h); 112 sha512_update(&h, sk, ED25519_SECRET); 113 sha512_sum(&h, d); 114 memcpy(out, d, 8); 115 } 116 117 int 118 signify_generate(const char *pubpath, const char *secpath, const char *comment) 119 { 120 struct pubfile pub; 121 struct secfile sec; 122 uint8_t seed[ED25519_SEED]; 123 char c[200]; 124 int rc; 125 126 memset(&sec, 0, sizeof(sec)); 127 if (randombytes(seed, sizeof(seed)) || randombytes(sec.keynum, sizeof(sec.keynum)) 128 || randombytes(sec.salt, sizeof(sec.salt))) 129 return -1; 130 ed25519_keypair(pub.pk, sec.sk, seed); 131 memcpy(sec.alg, "Ed", 2); 132 memcpy(sec.kdfalg, "BK", 2); 133 checksum(sec.checksum, sec.sk); 134 memcpy(pub.alg, "Ed", 2); 135 memcpy(pub.keynum, sec.keynum, 8); 136 137 snprintf(c, sizeof(c), "%s secret key", comment); 138 rc = writefile(secpath, c, &sec, sizeof(sec), 0600); 139 if (rc == 0) { 140 snprintf(c, sizeof(c), "%s public key", comment); 141 rc = writefile(pubpath, c, &pub, sizeof(pub), 0644); 142 } 143 memset(&sec, 0, sizeof(sec)); 144 memset(seed, 0, sizeof(seed)); 145 return rc; 146 } 147 148 int 149 signify_read_pub(const char *path, struct signify_pub *out) 150 { 151 struct pubfile f; 152 153 if (readfile(path, &f, sizeof(f))) 154 return -1; 155 memcpy(out->keynum, f.keynum, 8); 156 memcpy(out->pk, f.pk, ED25519_PUBLIC); 157 return 0; 158 } 159 160 int 161 signify_read_sec(const char *path, struct signify_sec *out) 162 { 163 struct secfile f; 164 uint8_t sum[8]; 165 166 if (readfile(path, &f, sizeof(f))) 167 return -1; 168 if (memcmp(f.kdfalg, "BK", 2) || f.rounds[0] | f.rounds[1] | f.rounds[2] | f.rounds[3]) { 169 weprintf("%s: a key with a passphrase, make one without (signify -n)\n", path); 170 return -1; 171 } 172 checksum(sum, f.sk); 173 if (memcmp(sum, f.checksum, 8)) { 174 weprintf("%s: the key does not match its checksum\n", path); 175 return -1; 176 } 177 memcpy(out->keynum, f.keynum, 8); 178 memcpy(out->sk, f.sk, ED25519_SECRET); 179 memset(&f, 0, sizeof(f)); 180 return 0; 181 } 182 183 int 184 signify_sign( 185 const struct signify_sec *sec, 186 const uint8_t *m, 187 size_t n, 188 const char *pubname, 189 const char *sigpath 190 ) 191 { 192 struct sigfile f; 193 char c[300]; 194 195 memcpy(f.alg, "Ed", 2); 196 memcpy(f.keynum, sec->keynum, 8); 197 ed25519_sign(f.sig, m, n, sec->sk); 198 snprintf(c, sizeof(c), "verify with %s", pubname); 199 unlink(sigpath); 200 return writefile(sigpath, c, &f, sizeof(f), 0644); 201 } 202 203 int 204 signify_verify(const struct signify_pub *pub, const uint8_t *m, size_t n, const char *sigpath) 205 { 206 struct sigfile f; 207 208 if (readfile(sigpath, &f, sizeof(f))) 209 return -1; 210 if (memcmp(f.keynum, pub->keynum, 8)) { 211 weprintf("%s: signed with another key\n", sigpath); 212 return -1; 213 } 214 if (ed25519_verify(f.sig, m, n, pub->pk)) { 215 weprintf("%s: the signature does not match\n", sigpath); 216 return -1; 217 } 218 return 0; 219 }