summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorQuentin Carbonneaux2022-04-06 22:14:34 +0200
committerQuentin Carbonneaux2022-04-06 22:48:59 +0200
commit12d614a845913b79129507e477aba757c81e8c73 (patch)
tree58531813a1362dca718d5abf66321331f470830c
parentdbdc3d542de6e0b9c00b89cdfab86d72a10a9e61 (diff)
nits
-rw-r--r--sdar/arg.h2
-rw-r--r--sdar/cache.c6
-rw-r--r--sdar/key.c47
-rw-r--r--sdar/main.c10
-rw-r--r--sdar/slice.c2
-rw-r--r--sdar/stash.c8
6 files changed, 36 insertions, 39 deletions
diff --git a/sdar/arg.h b/sdar/arg.h
index 4450ddb..c97bd16 100644
--- a/sdar/arg.h
+++ b/sdar/arg.h
@@ -1,5 +1,3 @@
-/* thanks Michael Forney for this */
-
#define ARGBEGIN \
for (;;) { \
if (argc > 0) \
diff --git a/sdar/cache.c b/sdar/cache.c
index b3bb4fd..1fae8dd 100644
--- a/sdar/cache.c
+++ b/sdar/cache.c
@@ -69,7 +69,7 @@ cput(Cache *c, hmac_t key, Block *val)
b = *val;
b.seg = (b.lz4 != 0) | b.seg << 1;
- return (mmhput(&c->hash, key, &b) != 0);
+ return mmhput(&c->hash, key, &b) != 0;
}
int
@@ -81,7 +81,7 @@ cputseg(Cache *c, uchar id[Segidsz], int n)
assert(n > 0);
memcpy(key, id, Segidsz);
b.seg = -n;
- return (mmhput(&c->hash, key, &b) != 0);
+ return mmhput(&c->hash, key, &b) != 0;
}
int
@@ -94,7 +94,7 @@ cget(Cache *c, hmac_t key, Block *b)
memcpy(b, p, MMH_VALSZ); /* p may be unaligned */
b->lz4 = b->seg & 1;
b->seg >>= 1;
- return (b->seg <= 0);
+ return b->seg <= 0;
}
int
diff --git a/sdar/key.c b/sdar/key.c
index a706a6c..59c0400 100644
--- a/sdar/key.c
+++ b/sdar/key.c
@@ -6,15 +6,14 @@
#include <randombytes.h>
MAKESURE(nacl_nonce_len_is_Noncesz, crypto_secretbox_NONCEBYTES == Noncesz);
-MAKESURE(nacl_key_len_is_Keysz, crypto_secretbox_KEYBYTES == Keysz);
-MAKESURE(nacl_plain_pad_is_Plnpad, crypto_secretbox_ZEROBYTES == Plnpad);
+MAKESURE(nacl_key_len_is_Keysz, crypto_secretbox_KEYBYTES == Keysz);
+MAKESURE(nacl_plain_pad_is_Plnpad, crypto_secretbox_ZEROBYTES == Plnpad);
MAKESURE(nacl_cipher_pad_is_Cippad, crypto_secretbox_BOXZEROBYTES == Cippad);
-MAKESURE(cipher_pad_leq_plain_pad, Cippad <= Plnpad);
enum {
Saltsz = 32,
- /* https://blog.filippo.io/the-scrypt-parameters/ */
- ScryptN = 1<<14, /* was 17 */
+ /* see https://blog.filippo.io/the-scrypt-parameters/ */
+ ScryptN = 1 << 14,
Scryptr = 8,
Scryptp = 1,
};
@@ -68,37 +67,43 @@ keydump()
enum {
Osalt = Magicsz,
- Ohmackey = Osalt + Saltsz,
- Opub = Ohmackey + Keysz,
+ Ohmac = Osalt + Saltsz,
+ Opub = Ohmac + Keysz,
Osec = Opub + Keysz,
- Oend = Osec + (Plnpad - Cippad) + Keysz,
+ Oend = Osec + Cipovh + Keysz,
};
+static int
+pbkdf(char *p, long np, uchar *salt, uchar *nk)
+{
+ return scrypt(
+ (uint8_t*)p, np, salt, Saltsz,
+ ScryptN, Scryptr, Scryptp,
+ nk, Noncesz + Keysz) != 0;
+}
+
int
keywrite(char *p, long np, FILE *f)
{
uchar nk[Noncesz + Keysz];
uchar cip[Plnpad + Keysz];
uchar buf[Oend];
- int ret;
assert(key.sec);
memcpy(buf, keymagic, Magicsz);
randombytes(buf + Osalt, Saltsz);
- memcpy(buf + Ohmackey, key.hmac, Keysz);
+ memcpy(buf + Ohmac, key.hmac, Keysz);
memcpy(buf + Opub, key.pub, Keysz);
- ret = scrypt((uint8_t*)p, np, buf + Osalt, Saltsz,
- ScryptN, Scryptr, Scryptp, nk, sizeof nk);
- if (ret != 0)
+ if (pbkdf(p, np, buf + Osalt, nk))
return 1;
memset(sec, 0, Plnpad);
crypto_secretbox(cip, sec, Plnpad + Keysz, nk, nk + Noncesz);
memcpy(buf + Osec, cip + Cippad, Oend - Osec);
- return (fwrite(buf, Oend, 1, f) != 1);
+ return fwrite(buf, Oend, 1, f) != 1;
}
char *
@@ -112,7 +117,7 @@ keyreadpub(FILE *f)
if (memcmp(buf, keymagic, Magicsz) != 0)
return "invalid key file";
- memcpy(hmac, buf + Ohmackey, Keysz);
+ memcpy(hmac, buf + Ohmac, Keysz);
memcpy(pub, buf + Opub, Keysz);
key.hmac = hmac;
@@ -127,7 +132,6 @@ keyreadsec(char *p, long np, FILE *f)
uchar nk[Noncesz + Keysz];
uchar cip[Plnpad + Keysz];
uchar buf[Oend];
- int ret;
if (fread(buf, Oend, 1, f) != 1)
return "io error";
@@ -135,19 +139,16 @@ keyreadsec(char *p, long np, FILE *f)
if (memcmp(buf, keymagic, Magicsz) != 0)
return "invalid key file";
- memcpy(hmac, buf + Ohmackey, Keysz);
+ memcpy(hmac, buf + Ohmac, Keysz);
memcpy(pub, buf + Opub, Keysz);
- ret = scrypt((uint8_t*)p, np, buf + Osalt, Saltsz,
- ScryptN, Scryptr, Scryptp, nk, sizeof nk);
- if (ret != 0)
+ if (pbkdf(p, np, buf + Osalt, nk))
return "out of memory";
memcpy(cip + Cippad, buf + Osec, Oend - Osec);
memset(cip, 0, Cippad);
- ret = crypto_secretbox_open(sec, cip, Plnpad + Keysz,
- nk, nk + Noncesz);
- if (ret != 0)
+ if (crypto_secretbox_open(
+ sec, cip, Plnpad + Keysz, nk, nk + Noncesz) != 0)
return "decryption failed";
key.hmac = hmac;
diff --git a/sdar/main.c b/sdar/main.c
index a463661..d07891f 100644
--- a/sdar/main.c
+++ b/sdar/main.c
@@ -460,7 +460,6 @@ idbatch(Writer *w)
goto error;
if (memcmp(cmd+2, "end\n", 4) == 0) {
- /* end of data stream */
if (werr || writerdone(w, &a)) {
pkt("err");
werr = 0;
@@ -474,7 +473,6 @@ idbatch(Writer *w)
if (memcmp(cmd+2, "raw ", 4) != 0)
goto error;
- /* write more data in w */
if (readnum(&len, stdin))
goto error;
while (len > 0) {
@@ -703,13 +701,13 @@ static int
emitblock(Block *b, hmac_t hmac, uchar segp[Prepsz], void *arg)
{
Slice s;
- FILE *f;
+ FILE *segf;
- f = arg;
- if (fseek(f, Seghdsz + b->off, SEEK_SET) == 1)
+ segf = arg;
+ if (fseek(segf, Seghdsz + b->off, SEEK_SET) == 1)
return 1;
s = newsl();
- if (decrypt(&s, f, b->len, segp, b->off)) {
+ if (decrypt(&s, segf, b->len, segp, b->off)) {
logs("E could not read block");
err("could not read block");
freesl(s);
diff --git a/sdar/slice.c b/sdar/slice.c
index c2ecc18..e7bf35b 100644
--- a/sdar/slice.c
+++ b/sdar/slice.c
@@ -41,7 +41,7 @@ mksl(uchar *buf, long len)
return (Slice){buf, len};
}
-/* buffers life cycle must adhere to a
+/* buffer life cycles must adhere to a
* stack discipline */
Slice
newsl()
diff --git a/sdar/stash.c b/sdar/stash.c
index 12abf67..56284d2 100644
--- a/sdar/stash.c
+++ b/sdar/stash.c
@@ -218,11 +218,11 @@ writeseg(Arch *a, char *msg, FILE *f)
if (fseek(a->data, 0, SEEK_SET) == -1)
goto out;
- for (len = 0;; len += n) {
- n = fread(sb.buf, 1, Maxblk, a->data);
- if (n == 0)
+ for (len = 0;; len += sb.len) {
+ slread(&sb, Maxblk, a->data);
+ if (sb.len == 0)
break;
- if (fwrite(sb.buf, 1, n, f) != (size_t)n)
+ if (slwrite(sb, f))
goto out;
}
if (len != h->dlen) {