commit 9155c685452c0b123472016b01acda1d2fedd1eb
parent 51368350b7b7c7a23fdc51cc86906c81572f9179
Author: sin <sin@2f30.org>
Date: Tue, 21 May 2019 14:40:58 +0300
Remove some magic numbers
Diffstat:
4 files changed, 41 insertions(+), 22 deletions(-)
diff --git a/bencrypt.c b/bencrypt.c
@@ -22,7 +22,8 @@ extern struct param param;
#define EDNONETYPE 0x300
#define EDCHACHATYPE 0x301
-#define EDSIZE (8 + 8 + 24)
+#define NONCESIZE crypto_aead_xchacha20poly1305_ietf_NPUBBYTES
+#define EDSIZE (8 + 8 + NONCESIZE)
extern int pack(unsigned char *, char *, ...);
extern int unpack(unsigned char *, char *, ...);
@@ -54,19 +55,21 @@ struct ectx {
/* Encryption descriptor */
struct ed {
- uint16_t type; /* encryption algorithm type */
+ uint16_t type; /* encryption algorithm type */
unsigned char reserved[6]; /* should be set to 0 when writing */
- uint64_t size; /* size of encrypted block */
- unsigned char nonce[crypto_aead_xchacha20poly1305_ietf_NPUBBYTES];
+ uint64_t size; /* size of encrypted block */
+ unsigned char nonce[NONCESIZE]; /* unpredictable nonce used when encrypting */
};
/* Unpack encryption descriptor */
static int
unpacked(unsigned char *buf, struct ed *ed)
{
+ char fmt[BUFSIZ];
int n;
- n = unpack(buf, "s'6q'24",
+ snprintf(fmt, sizeof(fmt), "s'6q'%d", NONCESIZE);
+ n = unpack(buf, fmt,
&ed->type,
ed->reserved,
&ed->size,
@@ -80,9 +83,11 @@ unpacked(unsigned char *buf, struct ed *ed)
static int
packed(unsigned char *buf, struct ed *ed)
{
+ char fmt[BUFSIZ];
int n;
- n = pack(buf, "s'6q'24",
+ snprintf(fmt, sizeof(fmt), "s'6q'%d", NONCESIZE);
+ n = pack(buf, fmt,
ed->type,
ed->reserved,
ed->size,
diff --git a/bstorage.c b/bstorage.c
@@ -117,9 +117,11 @@ static RB_GENERATE(bdcache, bd, rbe, bd_cmp)
static int
unpackbhdr(unsigned char *buf, struct bhdr *bhdr)
{
+ char fmt[BUFSIZ];
int n;
- n = unpack(buf, "'16qq",
+ snprintf(fmt, sizeof(fmt), "'%dqq", NBHDRMAGIC);
+ n = unpack(buf, fmt,
bhdr->magic,
&bhdr->flags,
&bhdr->nbd);
@@ -132,9 +134,11 @@ unpackbhdr(unsigned char *buf, struct bhdr *bhdr)
static int
packbhdr(unsigned char *buf, struct bhdr *bhdr)
{
+ char fmt[BUFSIZ];
int n;
- n = pack(buf, "'16qq",
+ snprintf(fmt, sizeof(fmt), "'%dqq", NBHDRMAGIC);
+ n = pack(buf, fmt,
bhdr->magic,
bhdr->flags,
bhdr->nbd);
diff --git a/snap.c b/snap.c
@@ -35,7 +35,8 @@
#define VMAJSHIFT 8
#define VMAJMASK 0xff
-#define SHDRSIZE (NSHDRMAGIC + 24 + 8 + 8)
+#define CRYPTOHDRSIZE crypto_secretstream_xchacha20poly1305_HEADERBYTES
+#define SHDRSIZE (NSHDRMAGIC + CRYPTOHDRSIZE + 8 + 8)
extern struct param param;
@@ -45,15 +46,15 @@ extern int unpack(unsigned char *, char *, ...);
/* Snapshot header structure */
struct shdr {
- char magic[NSHDRMAGIC]; /* magic number for file(1) */
- unsigned char header[crypto_secretstream_xchacha20poly1305_HEADERBYTES];
- uint64_t flags; /* version number */
- uint64_t nbd; /* number of block hashes */
+ char magic[NSHDRMAGIC]; /* magic number for file(1) */
+ unsigned char header[CRYPTOHDRSIZE]; /* xchacha20-poly1305 crypto header size */
+ uint64_t flags; /* version number */
+ uint64_t nbd; /* number of block hashes */
};
struct mdnode {
- unsigned char md[MDSIZE]; /* hash of block */
- TAILQ_ENTRY(mdnode) e; /* mdhead link node */
+ unsigned char md[MDSIZE]; /* hash of block */
+ TAILQ_ENTRY(mdnode) e; /* mdhead link node */
};
struct sctx {
@@ -69,9 +70,11 @@ struct sctx {
static int
unpackshdr(unsigned char *buf, struct shdr *shdr)
{
+ char fmt[BUFSIZ];
int n;
- n = unpack(buf, "'16'24qq",
+ snprintf(fmt, sizeof(fmt), "'%d'%dqq", NSHDRMAGIC, CRYPTOHDRSIZE);
+ n = unpack(buf, fmt,
shdr->magic,
shdr->header,
&shdr->flags,
@@ -85,9 +88,11 @@ unpackshdr(unsigned char *buf, struct shdr *shdr)
static int
packshdr(unsigned char *buf, struct shdr *shdr)
{
+ char fmt[BUFSIZ];
int n;
- n = pack(buf, "'16'24qq",
+ snprintf(fmt, sizeof(fmt), "'%d'%dqq", NSHDRMAGIC, CRYPTOHDRSIZE);
+ n = pack(buf, fmt,
shdr->magic,
shdr->header,
shdr->flags,
diff --git a/state.c b/state.c
@@ -29,9 +29,10 @@
#define ENONETYPE 0
#define ECHACHATYPE 1
+#define NONCESIZE crypto_aead_xchacha20poly1305_ietf_NPUBBYTES
#define MSEEDSIZE 4
-#define CSEEDSIZE (MSEEDSIZE + 16)
-#define SHDRSIZE (8 + 24 + CSEEDSIZE)
+#define CSEEDSIZE (MSEEDSIZE + crypto_aead_xchacha20poly1305_ietf_ABYTES)
+#define SHDRSIZE (8 + NONCESIZE + CSEEDSIZE)
/* misc helpers */
extern int pack(unsigned char *, char *, ...);
@@ -39,7 +40,7 @@ extern int unpack(unsigned char *, char *, ...);
struct shdr {
uint64_t flags;
- unsigned char nonce[crypto_aead_xchacha20poly1305_ietf_NPUBBYTES];
+ unsigned char nonce[NONCESIZE];
unsigned char seed[CSEEDSIZE];
};
@@ -47,9 +48,11 @@ struct shdr {
static int
unpackshdr(unsigned char *buf, struct shdr *shdr)
{
+ char fmt[BUFSIZ];
int n;
- n = unpack(buf, "q'24'20",
+ snprintf(fmt, sizeof(fmt), "q'%d'%d", NONCESIZE, CSEEDSIZE);
+ n = unpack(buf, fmt,
&shdr->flags,
shdr->nonce,
shdr->seed);
@@ -61,9 +64,11 @@ unpackshdr(unsigned char *buf, struct shdr *shdr)
static int
packshdr(unsigned char *buf, struct shdr *shdr)
{
+ char fmt[BUFSIZ];
int n;
- n = pack(buf, "q'24'20",
+ snprintf(fmt, sizeof(fmt), "q'%d'%d", NONCESIZE, CSEEDSIZE);
+ n = pack(buf, fmt,
shdr->flags,
shdr->nonce,
shdr->seed);