Skip to content

eBPF-based security platform

crypto: size base64 output with one pair of macros

John Fastabend2508804e6d8b
diff --git a/testing/fuzz/fuzz-quote-check.c b/testing/fuzz/fuzz-quote-check.c
index 22ca3460..1c09489e 100644
--- a/testing/fuzz/fuzz-quote-check.c
+++ b/testing/fuzz/fuzz-quote-check.c
@@ -16,6 +16,7 @@
#include <shirudo/string.h>
+#include "crypto.h"
#include "hex.h"
#include "json.h"
#include "tools/quote-check/row.h"
diff --git a/tools/quote-check/row.h b/tools/quote-check/row.h
index 17f2f145..aef3ff2a 100644
--- a/tools/quote-check/row.h
+++ b/tools/quote-check/row.h
@@ -13,9 +13,9 @@
#include <shirudo/misc.h>
+#include "crypto.h"
#include "tpm.h"
-#define BASE64_LEN(n) (4 * (((n) + 2) / 3))
#define QUOTE_BODY_HEX_LEN (2 * TPM_DIGEST_LEN)
/* attest, sig, ak and body_sha256 with the three tabs between them. */
#define QUOTE_ROW_MAX \
diff --git a/util/attest.c b/util/attest.c
index fde0ae67..74056fe2 100644
--- a/util/attest.c
+++ b/util/attest.c
@@ -43,7 +43,7 @@
#define EK_CERT_MAX 4096
#define QUOTE_ATTEMPTS 3
#define RECORDS_MAX 4096
-#define EK_CERT_B64_MAX (4 * ((EK_CERT_MAX + 2) / 3) + 1)
+#define EK_CERT_B64_MAX BASE64_SIZE(EK_CERT_MAX)
/* restricted | sign | fixedTPM | fixedParent | sensitiveDataOrigin */
#define AK_ATTRS_REQUIRED 0x00050032u
@@ -85,7 +85,7 @@ static int read_file_base64(const char *path, char **out, size_t *out_cap)
xfree(raw, LOG_MAX_BYTES + 1);
return ret;
}
- cap = 4 * ((len + 2) / 3) + 1;
+ cap = BASE64_SIZE(len);
*out = xmalloc(cap);
ret = *out ? base64_encode(raw, len, *out, cap) : -ENOMEM;
xfree(raw, LOG_MAX_BYTES + 1);
@@ -240,7 +240,7 @@ int attest_evidence_config(const __u8 *nonce, size_t nonce_len,
}
if (tpm_ek_certificate(tpm, ek_cert, EK_CERT_MAX, &ek_cert_len,
&ek_cert_rsa) == 0) {
- ek_b64_cap = 4 * ((ek_cert_len + 2) / 3) + 1;
+ ek_b64_cap = BASE64_SIZE(ek_cert_len);
ek_b64 = xmalloc(ek_b64_cap);
if (!ek_b64 || base64_encode(ek_cert, ek_cert_len, ek_b64,
ek_b64_cap)) {
diff --git a/util/bpm_oci.c b/util/bpm_oci.c
index e28421bb..a7f072a9 100644
--- a/util/bpm_oci.c
+++ b/util/bpm_oci.c
@@ -260,7 +260,7 @@ static int encode_basic(const char *user, const char *pass,
goto out;
}
len = strlen(raw);
- need = 4 * ((len + 2) / 3) + 1;
+ need = BASE64_SIZE(len);
if (need > out_sz) {
ret = -ENOSPC;
goto out;
diff --git a/util/crypto.h b/util/crypto.h
index c01b2c74..4c6e6df8 100644
--- a/util/crypto.h
+++ b/util/crypto.h
@@ -31,6 +31,13 @@ int sha256_final(struct sha256_ctx *ctx,
void sha256_free(struct sha256_ctx *ctx);
int sha256_buffer(const void *data, size_t len,
__u8 out[CRYPTO_SHA256_LEN]) __must_check;
+/*
+ * BASE64_LEN() is the length of the base64 of n bytes and BASE64_SIZE()
+ * the out_sz base64_encode() needs for them, its terminator included.
+ */
+#define BASE64_LEN(n) (4 * (((n) + 2) / 3))
+#define BASE64_SIZE(n) (BASE64_LEN(n) + 1)
+
int base64_encode(const __u8 *data, size_t len,
char *out, size_t out_sz) __must_check;
diff --git a/util/crypto_openssl.c b/util/crypto_openssl.c
index fc17319d..a60e50c9 100644
--- a/util/crypto_openssl.c
+++ b/util/crypto_openssl.c
@@ -141,7 +141,7 @@ int base64_encode(const __u8 *data, size_t len,
if (!data || !out || len > INT_MAX)
return -EINVAL;
- need = 4 * ((len + 2) / 3) + 1;
+ need = BASE64_SIZE(len);
if (out_sz < need)
return -EINVAL;
if (EVP_EncodeBlock((__u8 *)out, data, (int)len) < 0)
diff --git a/util/keepalive.c b/util/keepalive.c
index 3461b1aa..3616dcb9 100644
--- a/util/keepalive.c
+++ b/util/keepalive.c
@@ -50,7 +50,7 @@ int keepalive_mask(const __u8 *codes, size_t n, char *b64, size_t cap)
return -EINVAL;
raw[i / CODES_PER_BYTE] |= codes[i] << (CODE_BITS * slot);
}
- if (cap < 4 * ((len + 2) / 3) + 1)
+ if (cap < BASE64_SIZE(len))
return -E2BIG;
return base64_encode(raw, len, b64, cap);
}
diff --git a/util/keepalive.h b/util/keepalive.h
index dab79714..dcf334f3 100644
--- a/util/keepalive.h
+++ b/util/keepalive.h
@@ -17,10 +17,11 @@
#include "bpf/lib/tamper.h"
#include "bpm_tree.h"
#include "conditions.h"
+#include "crypto.h"
#define KEEPALIVE_MASK_OPS 512
#define KEEPALIVE_MASK_BYTES (KEEPALIVE_MASK_OPS / 4)
-#define KEEPALIVE_MASK_B64_MAX (4 * ((KEEPALIVE_MASK_BYTES + 2) / 3) + 1)
+#define KEEPALIVE_MASK_B64_MAX BASE64_SIZE(KEEPALIVE_MASK_BYTES)
/*
* The body reports the last this many operations up to the head of the
diff --git a/util/keepalive_quote.c b/util/keepalive_quote.c
index eb3e4223..81c911a0 100644
--- a/util/keepalive_quote.c
+++ b/util/keepalive_quote.c
@@ -12,9 +12,9 @@ noinline_for_stack int
keepalive_quote_format(const struct tpm_attestation *att,
const struct tpm_key *ak, char *out, size_t cap)
{
- char attest[KEEPALIVE_B64_SIZE(TPM_ATTEST_MAX)];
- char sig[KEEPALIVE_B64_SIZE(TPM_SIGNATURE_MAX)];
- char public[KEEPALIVE_B64_SIZE(TPM_PUBLIC_MAX)];
+ char attest[BASE64_SIZE(TPM_ATTEST_MAX)];
+ char sig[BASE64_SIZE(TPM_SIGNATURE_MAX)];
+ char public[BASE64_SIZE(TPM_PUBLIC_MAX)];
int ret;
ret = base64_encode(att->attest, att->attest_len, attest,
diff --git a/util/keepalive_quote.h b/util/keepalive_quote.h
index c7bb6761..90ad8ffc 100644
--- a/util/keepalive_quote.h
+++ b/util/keepalive_quote.h
@@ -13,15 +13,14 @@
#include <shirudo/misc.h>
+#include "crypto.h"
#include "tpm.h"
-#define KEEPALIVE_B64_SIZE(n) (4 * (((n) + 2) / 3) + 1)
-
/* Holds the longest quote and its terminator. */
#define KEEPALIVE_QUOTE_MAX (sizeof("attest=;sig=;ak=") + \
- KEEPALIVE_B64_SIZE(TPM_ATTEST_MAX) + \
- KEEPALIVE_B64_SIZE(TPM_SIGNATURE_MAX) + \
- KEEPALIVE_B64_SIZE(TPM_PUBLIC_MAX))
+ BASE64_SIZE(TPM_ATTEST_MAX) + \
+ BASE64_SIZE(TPM_SIGNATURE_MAX) + \
+ BASE64_SIZE(TPM_PUBLIC_MAX))
/*
* Writes attest=<TPMS_ATTEST>;sig=<TPMT_SIGNATURE>;ak=<TPMT_PUBLIC> in