eBPF-based security platform
crypto: check the result of every digest, KDF and name
Daniel Borkmann21158b8ddeeb
diff --git a/builtin-attest.c b/builtin-attest.cindex 8a989949..a21c593a 100644--- a/builtin-attest.c+++ b/builtin-attest.c@@ -271,11 +271,11 @@ static int apply_event(const char *spec, __u8 chain[32])memcpy(name, colon + 1, name_len);name[name_len] = '\0';if (kind_len == 4 && !strncmp(spec, "load", 4)) {- chain_upatch_event("shirudo-upatch", name, digest,- event);+ if (chain_upatch_event("shirudo-upatch", name, digest, event))+ return -EIO;} else if (kind_len == 6 && !strncmp(spec, "unload", 6)) {- chain_upatch_event("shirudo-unload", name, digest,- event);+ if (chain_upatch_event("shirudo-unload", name, digest, event))+ return -EIO;} else if (kind_len == 4 && !strncmp(spec, "tree", 4)) {__s64 seq;@@ -409,9 +409,12 @@ static int cmd_attest_predict(int argc, const char **argv)ret = 1;goto out;}- tpm_nv_name(NV_INDEX,- TPM_REGISTER_ATTRS |- TPM_NV_WRITTEN, policy, 32, name);+ if (tpm_nv_name(NV_INDEX, TPM_REGISTER_ATTRS | TPM_NV_WRITTEN,+ policy, 32, name)) {+ pr_err("attest predict: cannot compute the register name");+ ret = 1;+ goto out;+ }hex_encode(name, sizeof(name), hex);printf("register_name: %s\n", hex);if (chain_anchor(name, anchor, sizeof(anchor))) {diff --git a/testing/fuzz/fuzz-cms.c b/testing/fuzz/fuzz-cms.cindex 86f387cd..c5e102d2 100644--- a/testing/fuzz/fuzz-cms.c+++ b/testing/fuzz/fuzz-cms.c@@ -7,6 +7,8 @@* DER certificate of that length and a signature over the certificate.*/+#include <stdlib.h>+#include "crypto.h"#include "fuzz.h"#include "keys.h"@@ -32,16 +34,23 @@ int LLVMFuzzerTestOneInput(const __u8 *data, size_t size)keys_digest_der(der, der_len, sha, fp);if (!cert_read_der(der, der_len, &cert)) {- cert_pubkey(&cert, &pub);- verify_detached(der, der_len, sig, sig_len, &cert);- cms_signed_by(sig, sig_len, &cert);+ int signed_by = cms_signed_by(sig, sig_len, &cert);++ /* A signature that verifies is one that parses. */+ if (!verify_detached(der, der_len, sig, sig_len, &cert) &&+ signed_by < 0)+ abort();+ if (!cert_pubkey(&cert, &pub) && pub.n_len > sizeof(pub.n))+ abort();cert_free(&cert);}raw = xmalloc(sig_len + 1);if (raw) {- base64_decode((const char *)sig, sig_len, raw,- sig_len + 1, &n);+ /* Base64 never decodes to more bytes than it spans. */+ if (!base64_decode((const char *)sig, sig_len, raw,+ sig_len + 1, &n) && n > sig_len)+ abort();xfree(raw, sig_len + 1);}return 0;diff --git a/testing/fuzz/fuzz-tpm-wire.c b/testing/fuzz/fuzz-tpm-wire.cindex 27e98e24..796ae0fc 100644--- a/testing/fuzz/fuzz-tpm-wire.c+++ b/testing/fuzz/fuzz-tpm-wire.c@@ -6,20 +6,55 @@* names, quote and certification attestations and their signatures.*/+#include <stdbool.h>+#include <stdlib.h>+#include <string.h>++#include <shirudo/byteorder.h>+#include "fuzz.h"#include "tpm_wire.h"+static bool within(const __u8 *p, size_t n, const __u8 *data, size_t size)+{+ return p >= data && n <= size && (size_t)(p - data) <= size - n;+}++/*+ * What parses holds to what the parsers promise: a public area+ * marshals back to the bytes it came from, it has a name exactly when+ * its name algorithm is SHA-256, the ECC parser takes a subset of it,+ * a signature is the one fixed size, and an attestation points only+ * into the input.+ */int LLVMFuzzerTestOneInput(const __u8 *data, size_t size){- __u8 name[TPM_NAME_LEN], r[32], s[32];+ __u8 name[TPM_NAME_LEN], r[32], s[32], again[1024];struct tpm_ecc_public ecc;struct tpm_public pub;struct tpm_attest at;+ bool parsed, named;+ size_t len;- tpm_public_parse(data, size, &pub);- tpm_ecc_public_parse(data, size, &ecc);- tpm_object_name(data, size, name);- tpm_ecdsa_signature_parse(data, size, r, s);- tpm_attest_parse(data, size, &at);+ parsed = !tpm_public_parse(data, size, &pub);+ named = !tpm_object_name(data, size, name);+ if (named != (size >= 4 && get_be16(data + 2) == TPM_ALG_SHA256))+ abort();+ if (parsed &&+ (!named || tpm_public_marshal(&pub, again, sizeof(again), &len) ||+ len != size || memcmp(again, data, size)))+ abort();+ if (!tpm_ecc_public_parse(data, size, &ecc) &&+ (!parsed || pub.type != TPM_ALG_ECC))+ abort();+ if (!tpm_ecdsa_signature_parse(data, size, r, s) && size != 72)+ abort();+ if (!tpm_attest_parse(data, size, &at) &&+ (at.signer_len > TPM_NAME_LEN ||+ !within(at.extra, at.extra_len, data, size) ||+ (at.type == TPM_ST_ATTEST_NV &&+ (at.index_name_len > TPM_NAME_LEN ||+ !within(at.nv_data, at.nv_len, data, size)))))+ abort();return 0;}diff --git a/testing/helper/test-nv-cphash.c b/testing/helper/test-nv-cphash.cindex 06d52d80..dc66af5b 100644--- a/testing/helper/test-nv-cphash.c+++ b/testing/helper/test-nv-cphash.c@@ -175,11 +175,11 @@ int main(void)fprintf(stderr, "create TPM NV writer: %s\n", strerror(-err));goto out;}- if (tpm_policy_signed(zero, writer_public, policy)) {+ if (tpm_policy_signed(zero, writer_public, policy) ||+ tpm_policy_command_code(policy, TPM_CC_NV_EXTEND, policy)) {fprintf(stderr, "cannot construct the scratch NV writer policy\n");goto out;}- tpm_policy_command_code(policy, TPM_CC_NV_EXTEND, policy);if (tpm_writer_load(t, writer_seed, writer_public, &writer) ||tpm_nv_define(t, TEST_NV_INDEX, policy, NULL, 0))goto out;diff --git a/testing/helper/test-register.c b/testing/helper/test-register.cindex 1881ce86..bea63fba 100644--- a/testing/helper/test-register.c+++ b/testing/helper/test-register.c@@ -237,8 +237,9 @@ int main(int argc, char **argv)if (hex_decode(argv[3], payload, 32))return 2;- chain_upatch_event("shirudo-upatch", argv[2], payload,- event);+ if (chain_upatch_event("shirudo-upatch", argv[2], payload,+ event))+ return 1;ret = chain_extend(event, chain);printf("rc=%d\n", ret);if (ret)@@ -464,10 +465,11 @@ int main(int argc, char **argv)ret = tpm_nv_info(tpm, index, &nv);if (!ret)ret = chain_anchor(nv.name, anchor, sizeof(anchor));- if (!ret) {- sha256_calc(anchor, strlen(anchor), anchor_digest);+ if (!ret)+ ret = sha256_calc(anchor, strlen(anchor),+ anchor_digest);+ if (!ret)ret = tpm_pcr_extend(tpm, pcr, anchor_digest);- }if (!ret)ret = tpm_nv_read(tpm, index, value);if (!ret)diff --git a/testing/helper/test-tpm-wire.c b/testing/helper/test-tpm-wire.cindex 0c30fdd1..86dd582c 100644--- a/testing/helper/test-tpm-wire.c+++ b/testing/helper/test-tpm-wire.c@@ -83,7 +83,8 @@ int main(int argc, char **argv)if (hex_decode(argv[2], a, 32))return 2;- tpm_policy_command_code(a, command_code, out);+ if (tpm_policy_command_code(a, command_code, out))+ return 1;puthex(out, 32);return 0;}@@ -100,7 +101,8 @@ int main(int argc, char **argv)if (strcmp(argv[1], "nv-written") == 0 && argc == 4) {if (hex_decode(argv[2], a, 32))return 2;- tpm_policy_nv_written(a, atoi(argv[3]) != 0, out);+ if (tpm_policy_nv_written(a, atoi(argv[3]) != 0, out))+ return 1;puthex(out, 32);return 0;}@@ -108,8 +110,9 @@ int main(int argc, char **argv)if (hex_decode(argv[2], a, 32) ||hex_decode(argv[4], b, 32))return 2;- tpm_policy_pcr(a, (__u32)strtoul(argv[3], NULL, 0),- b, out);+ if (tpm_policy_pcr(a, (__u32)strtoul(argv[3], NULL, 0),+ b, out))+ return 1;puthex(out, 32);return 0;}@@ -117,7 +120,8 @@ int main(int argc, char **argv)if (hex_decode(argv[2], a, 32) ||hex_decode(argv[3], b, 32))return 2;- tpm_policy_or(a, b, out);+ if (tpm_policy_or(a, b, out))+ return 1;puthex(out, 32);return 0;}@@ -141,9 +145,10 @@ int main(int argc, char **argv)if (hex_decode(argv[4], a, 32))return 2;- tpm_nv_name((__u32)strtoul(argv[2], NULL, 0),- (__u32)strtoul(argv[3], NULL, 0), a, 32,- name);+ if (tpm_nv_name((__u32)strtoul(argv[2], NULL, 0),+ (__u32)strtoul(argv[3], NULL, 0), a, 32,+ name))+ return 1;puthex(name, sizeof(name));return 0;}@@ -218,7 +223,8 @@ int main(int argc, char **argv)gethex(argv[4], u, sizeof(u), &u_len) ||gethex(argv[5], v, sizeof(v), &v_len))return 2;- tpm_kdfe(z, argv[3], u, u_len, v, v_len, res);+ if (tpm_kdfe(z, argv[3], u, u_len, v, v_len, res))+ return 1;puthex(res, 32);return 0;}diff --git a/util/attest.c b/util/attest.cindex 5fb91d91..b16f231b 100644--- a/util/attest.c+++ b/util/attest.c@@ -560,12 +560,11 @@ static int on_record(void *ctx)__u8 payload[32], event[32];strscpy(r->label, p->upatch_name, sizeof(r->label));- if (!hex_decode(p->upatch_payload_hex, payload, 32)) {- chain_upatch_event(load ? "shirudo-upatch" :- "shirudo-unload", p->upatch_name,- payload, event);+ if (!hex_decode(p->upatch_payload_hex, payload, 32) &&+ !chain_upatch_event(load ? "shirudo-upatch" :+ "shirudo-unload", p->upatch_name,+ payload, event))r->derived = !memneq_explicit(event, r->digest, 32);- }} else if (!strcmp(p->type, "tree_op")) {__u8 operation[32], event[32];@@ -698,11 +697,12 @@ static int check_signed(struct reporter *rep, const char *what,say(rep, false, "%s: signature structure unparseable", what);return -EKEYREJECTED;}- sha256_calc(attest, *attest_len, digest);- ret = ecdsa_p256_verify(ak->x, ak->y, digest, r, s);+ ret = sha256_calc(attest, *attest_len, digest);+ if (!ret)+ ret = ecdsa_p256_verify(ak->x, ak->y, digest, r, s);say(rep, ret == 0, "%s: ECDSA signature by the attestation key", what);- tpm_qualified_name(TPM_RH_ENDORSEMENT, ak_name, qn);- say(rep, parsed->signer_len == TPM_NAME_LEN &&+ say(rep, !tpm_qualified_name(TPM_RH_ENDORSEMENT, ak_name, qn) &&+ parsed->signer_len == TPM_NAME_LEN &&!memneq_explicit(parsed->signer, qn, TPM_NAME_LEN),"%s: signed by the key as an endorsement primary", what);say(rep, parsed->extra_len == nonce_len &&@@ -727,7 +727,7 @@ int attest_verify(const char *evidence, size_t len,__u8 nonce[ATTEST_NONCE_MAX], chain[32] = {};__u8 branch_written[32], branch_init[32], policy[32], bind[64];__u8 expected_name[TPM_NAME_LEN], pcr_expected[32];- __u8 anchor_digest[32], cred[TPM_CREDENTIAL_LEN];+ __u8 anchor_digest[32] = {}, cred[TPM_CREDENTIAL_LEN];struct parse *p;size_t ak_public_len, ak_name_len, nonce_len, attest_len;size_t reg_name_len, reg_value_len, cred_len = 0, i, runtime = 0;@@ -926,8 +926,9 @@ int attest_verify(const char *evidence, size_t len,{__u8 computed[TPM_NAME_LEN];- tpm_object_name(ak_public, ak_public_len, computed);- say(&rep, !memneq_explicit(computed, ak_name, sizeof(computed)),+ say(&rep,+ !tpm_object_name(ak_public, ak_public_len, computed) &&+ !memneq_explicit(computed, ak_name, sizeof(computed)),"attestation key: name matches its public area");}if (expect->have_ak_name)@@ -954,9 +955,9 @@ int attest_verify(const char *evidence, size_t len,}parsed = tpm_public_parse(ek_public, ek_public_len,&ek_pub) == 0;- tpm_object_name(ek_public, ek_public_len, computed);- say(&rep,- parsed && !memneq_explicit(computed, ek_name, TPM_NAME_LEN),+ say(&rep, parsed &&+ !tpm_object_name(ek_public, ek_public_len, computed) &&+ !memneq_explicit(computed, ek_name, TPM_NAME_LEN),"endorsement key: name matches its public area");say(&rep, parsed && ek_pub.attrs == TPM_EK_ATTRS &&ek_pub.policy_len == 32 &&@@ -1003,19 +1004,25 @@ int attest_verify(const char *evidence, size_t len,quote.type == TPM_ST_ATTEST_QUOTE &"e.pcr_mask == (__u32)p->pcr_mask &&p->pcr_have == quote.pcr_mask;+ bool hashed = false;bool vouched;say(&rep, complete,"quote: covers the PCRs the document lists");if (complete && sha256_init(&ctx) == 0) {- for (bit = 0; bit < 24; bit++) {+ int err = 0;++ for (bit = 0; bit < 24 && !err; bit++) {if (quote.pcr_mask & (1u << bit))- sha256_update(&ctx,- p->pcrs[bit], 32);+ err = sha256_update(&ctx,+ p->pcrs[bit], 32);}- sha256_final(&ctx, digest);+ if (err)+ sha256_free(&ctx);+ else+ hashed = !sha256_final(&ctx, digest);}- vouched = complete &&+ vouched = hashed &&!memneq_explicit(digest, quote.pcr_digest, 32);say(&rep, vouched,"quote: PCR digest matches the listed values");@@ -1113,9 +1120,12 @@ int attest_verify(const char *evidence, size_t len,ret = -EKEYREJECTED;goto out;}- tpm_nv_name(NV_INDEX,- TPM_REGISTER_ATTRS | TPM_NV_WRITTEN,- policy, 32, expected_name);+ ret = tpm_nv_name(NV_INDEX, TPM_REGISTER_ATTRS | TPM_NV_WRITTEN,+ policy, 32, expected_name);+ if (ret) {+ say(&rep, false, "register: cannot compute the index name");+ goto out;+ }say(&rep, !memneq_explicit(expected_name, reg_name, TPM_NAME_LEN) &&!memneq_explicit(expected_name, p->init.name, TPM_NAME_LEN),"register: public area is bound to PCRs %u and %u as recorded",@@ -1175,9 +1185,10 @@ int attest_verify(const char *evidence, size_t len,{char anchor[CHAIN_ANCHOR_MAX];bool named = !chain_anchor(p->init.name, anchor,- sizeof(anchor));+ sizeof(anchor)) &&+ !sha256_calc(anchor, strlen(anchor),+ anchor_digest);- sha256_calc(anchor, strlen(anchor), anchor_digest);say(&rep, named &&!memneq_explicit(anchor_digest, p->init.anchor_digest, 32),"anchor: record digest matches the index name");@@ -1403,8 +1414,8 @@ int attest_challenge_make(const char *evidence, size_t len,"endorsement key: the pinned key");/* The key must be an endorsement key, not any decryption key. */- tpm_object_name(ek_public, ek_public_len, computed);- say(&rep, !memneq_explicit(computed, ek_name, TPM_NAME_LEN),+ say(&rep, !tpm_object_name(ek_public, ek_public_len, computed) &&+ !memneq_explicit(computed, ek_name, TPM_NAME_LEN),"endorsement key: name matches its public area");say(&rep, pub.attrs == TPM_EK_ATTRS && pub.policy_len == 32 &&!memneq_explicit(pub.policy, shirudo_tpm_ek_policy, 32),@@ -1475,9 +1486,9 @@ int attest_challenge_make(const char *evidence, size_t len,sizeof(secret), &secret_len))goto out;} else {- if (ecdh_p256_ephemeral(pub.x, pub.y, eph_x, eph_y, z))+ if (ecdh_p256_ephemeral(pub.x, pub.y, eph_x, eph_y, z) ||+ tpm_kdfe(z, "IDENTITY", eph_x, 32, pub.x, 32, seed))goto out;- tpm_kdfe(z, "IDENTITY", eph_x, 32, pub.x, 32, seed);secret[0] = 0;secret[1] = 32;memcpy(secret + 2, eph_x, 32);diff --git a/util/chain.c b/util/chain.cindex ebf319b4..d1112ffe 100644--- a/util/chain.c+++ b/util/chain.c@@ -137,7 +137,8 @@ void chain_tree_event(__s64 seq, const __u8 operation_digest[32],for (i = 0; i < 8; i++)buf[taglen + i] = (__u8)(s >> (56 - 8 * i));memcpy(buf + taglen + 8, operation_digest, 32);- sha256_calc(buf, taglen + 40, event);+ if (sha256_calc(buf, taglen + 40, event))+ memset(event, 0, 32);}int chain_fold_checked(__u8 chain[32], const __u8 event[32])@@ -200,12 +201,13 @@ static int register_expectation(const __u8 pcr_before[32],pr_err("tpm: compute signed register policy: %s", strerror(-ret));return ret;}- tpm_nv_name(NV_INDEX, TPM_REGISTER_ATTRS,- policy, TPM_NV_DIGEST_LEN, name_unwritten);- tpm_nv_name(NV_INDEX,- TPM_REGISTER_ATTRS | TPM_NV_WRITTEN,- policy, TPM_NV_DIGEST_LEN, name_written);- return 0;+ ret = tpm_nv_name(NV_INDEX, TPM_REGISTER_ATTRS,+ policy, TPM_NV_DIGEST_LEN, name_unwritten);+ if (!ret)+ ret = tpm_nv_name(NV_INDEX,+ TPM_REGISTER_ATTRS | TPM_NV_WRITTEN,+ policy, TPM_NV_DIGEST_LEN, name_written);+ return ret;}static bool name_equals(const struct tpm_nv_info *info,@@ -580,10 +582,10 @@ int chain_status(struct chain_status *st)st->anchored = false;goto out;}- tpm_nv_name(NV_INDEX,- TPM_REGISTER_ATTRS |- TPM_NV_WRITTEN, policy, 32, expected);- st->anchored = name_equals(&nv, expected);+ st->anchored = !tpm_nv_name(NV_INDEX,+ TPM_REGISTER_ATTRS | TPM_NV_WRITTEN,+ policy, 32, expected) &&+ name_equals(&nv, expected);}out:tpm_close(tpm);diff --git a/util/chain.h b/util/chain.hindex 0aa4b67a..e1814c89 100644--- a/util/chain.h+++ b/util/chain.h@@ -35,7 +35,7 @@#define CHAIN_ANCHOR_MAX 128int sha256_calc(const void *data, size_t len,- __u8 out[CHAIN_DIGEST_LEN]);+ __u8 out[CHAIN_DIGEST_LEN]) __must_check;void sha256_2(const void *a, size_t a_len,const void *b, size_t b_len,@@ -53,20 +53,20 @@ struct measured_upatch {int chain_init_event(const __u8 cert_sha[32],struct measured_upatch *set, size_t n,const __u8 tree_root[32],- __u8 set_digest[32], __u8 event[32]);+ __u8 set_digest[32], __u8 event[32]) __must_check;/* SHA256(tag || NUL || cert_sha256), tag "shirudo-key-revoke". */void chain_key_event(const char *tag, const __u8 cert_sha[32],__u8 event[32]);int chain_upatch_event(const char *tag, const char *name,- const __u8 payload[32], __u8 event[32]);+ const __u8 payload[32], __u8 event[32]) __must_check;void chain_tree_event(__s64 seq, const __u8 operation_digest[32],__u8 event[32]);-int chain_fold_checked(__u8 chain[32], const __u8 event[32]);+int chain_fold_checked(__u8 chain[32], const __u8 event[32]) __must_check;void chain_fold(__u8 chain[32], const __u8 event[32]);/* The PCR record anchoring a register name: nvpcr-init:shirudo:0x..:<name>. */int chain_anchor(const __u8 name[TPM_NAME_LEN],- char *out, size_t cap);+ char *out, size_t cap) __must_check;struct chain_init_info {__u8 writer_public[TPM_NV_RSA_BYTES];diff --git a/util/crypto.h b/util/crypto.hindex c0a7507b..5c96a668 100644--- a/util/crypto.h+++ b/util/crypto.h@@ -23,49 +23,50 @@ struct sha256_ctx {void *impl;};-int sha256_init(struct sha256_ctx *ctx);+int sha256_init(struct sha256_ctx *ctx) __must_check;int sha256_update(struct sha256_ctx *ctx,- const void *data, size_t len);+ const void *data, size_t len) __must_check;int sha256_final(struct sha256_ctx *ctx,- __u8 out[CRYPTO_SHA256_LEN]);+ __u8 out[CRYPTO_SHA256_LEN]) __must_check;void sha256_free(struct sha256_ctx *ctx);int sha256_buffer(const void *data, size_t len,- __u8 out[CRYPTO_SHA256_LEN]);+ __u8 out[CRYPTO_SHA256_LEN]) __must_check;int base64_encode(const __u8 *data, size_t len,- char *out, size_t out_sz);+ char *out, size_t out_sz) __must_check;/* 0 when valid, -EKEYREJECTED when not. */int ecdsa_p256_verify(const __u8 x[32], const __u8 y[32],const __u8 digest[CRYPTO_SHA256_LEN],- const __u8 r[32], const __u8 s[32]);+ const __u8 r[32], const __u8 s[32]) __must_check;struct cert {void *impl;};-int cert_read_file(const char *path, struct cert *cert);-int cert_read_fd(int fd, struct cert *cert);+int cert_read_file(const char *path, struct cert *cert) __must_check;+int cert_read_fd(int fd, struct cert *cert) __must_check;void cert_free(struct cert *cert);int cert_pubkey_digest(const struct cert *cert,- __u8 out[CRYPTO_SHA256_LEN]);+ __u8 out[CRYPTO_SHA256_LEN]) __must_check;/* The provider's name for the cert's public key, e.g. "ML-DSA-87". */int cert_key_algorithm(const struct cert *cert,- char *out, size_t out_sz);+ char *out, size_t out_sz) __must_check;/* The subject on one line, "O=Example, CN=fleet", control bytes escaped. */-int cert_subject(const struct cert *cert, char *out, size_t out_sz);+int cert_subject(const struct cert *cert, char *out, size_t out_sz)+ __must_check;/* The validity window as UTC dates, YYYY-MM-DD. */int cert_validity(const struct cert *cert, char not_before[11],- char not_after[11]);+ char not_after[11]) __must_check;int verify_detached(const __u8 *payload, size_t payload_len,const __u8 *signature, size_t signature_len,- const struct cert *cert);+ const struct cert *cert) __must_check;/** Whether a signer of the CMS blob names this certificate, by key id* or by issuer and serial: 1 when one does, 0 when none, -EINVAL when* the blob does not parse. Says nothing about the signature's validity.*/int cms_signed_by(const __u8 *signature, size_t signature_len,- const struct cert *cert);+ const struct cert *cert) __must_check;struct tls_ca {const char *path;@@ -84,15 +85,15 @@ void tls_ca_cleanup(struct tls_ca *ca);* is the extra trust anchor fed to the SSL_CTX callback. Returns 0 or a* negative errno; callers must not connect when it fails.*/-int tls_curl_policy(CURL *curl, struct tls_ca *ca);+int tls_curl_policy(CURL *curl, struct tls_ca *ca) __must_check;int hmac_sha256(const __u8 *key, size_t key_len,const void *data, size_t len,- __u8 out[CRYPTO_SHA256_LEN]);+ __u8 out[CRYPTO_SHA256_LEN]) __must_check;/* AES-128 in CFB mode with a zero IV and no padding. */int aes128_cfb_encrypt(const __u8 key[16], const __u8 *in,- size_t len, __u8 *out);+ size_t len, __u8 *out) __must_check;/** RSA-OAEP with SHA-256, MGF1-SHA-256 and the given label to the@@ -101,7 +102,7 @@ int aes128_cfb_encrypt(const __u8 key[16], const __u8 *in,int rsa_oaep_encrypt(const __u8 *n, size_t n_len, __u32 e,const void *label, size_t label_len,const __u8 *in, size_t in_len,- __u8 *out, size_t cap, size_t *out_len);+ __u8 *out, size_t cap, size_t *out_len) __must_check;/** One ephemeral P-256 ECDH against the point (x, y): the ephemeral@@ -109,13 +110,13 @@ int rsa_oaep_encrypt(const __u8 *n, size_t n_len, __u32 e,*/int ecdh_p256_ephemeral(const __u8 x[32], const __u8 y[32],__u8 eph_x[32], __u8 eph_y[32],- __u8 z[32]);+ __u8 z[32]) __must_check;int base64_decode(const char *in, size_t in_len, __u8 *out,- size_t cap, size_t *out_len);+ size_t cap, size_t *out_len) __must_check;int cert_read_der(const __u8 *der, size_t len,- struct cert *cert);+ struct cert *cert) __must_check;struct cert_pubkey {bool rsa;@@ -127,7 +128,7 @@ struct cert_pubkey {};int cert_pubkey(const struct cert *cert,- struct cert_pubkey *out);+ struct cert_pubkey *out) __must_check;/** Chain the certificate to the PEM bundle of trusted issuers at path.@@ -135,7 +136,8 @@ int cert_pubkey(const struct cert *cert,* static explanation; -EINVAL when the bundle cannot be loaded.*/int cert_verify_chain(const struct cert *cert,- const char *bundle_path, const char **reason);+ const char *bundle_path, const char **reason)+ __must_check;/* Log libcrypto's last error under where. */void openssl_complain(const char *where);diff --git a/util/eventlog.c b/util/eventlog.cindex 8bfd4dbe..5b9e82c7 100644--- a/util/eventlog.c+++ b/util/eventlog.c@@ -371,8 +371,7 @@ static int replay_line(const char *line, size_t len, bool is_init, void *ctx)memset(chain, 0, 32);memcpy(cat, chain, 32);memcpy(cat + 32, digest, 32);- sha256_calc(cat, sizeof(cat), chain);- return 0;+ return sha256_calc(cat, sizeof(cat), chain);}int eventlog_replay(__u8 out[32])diff --git a/util/tpm_wire.c b/util/tpm_wire.cindex 161a155c..8bf969f2 100644--- a/util/tpm_wire.c+++ b/util/tpm_wire.c@@ -274,9 +274,9 @@ int tpm_register_policy(__u32 pcr_mask,writer_public, policy);}-void tpm_nv_name(__u32 index, __u32 attrs,- const __u8 policy[32], __u16 data_size,- __u8 name[TPM_NAME_LEN])+int tpm_nv_name(__u32 index, __u32 attrs,+ const __u8 policy[32], __u16 data_size,+ __u8 name[TPM_NAME_LEN]){__u8 pub[4 + 2 + 4 + 2 + 32 + 2];@@ -287,8 +287,7 @@ void tpm_nv_name(__u32 index, __u32 attrs,memcpy(pub + 12, policy, 32);put_be16(pub + 44, data_size);put_be16(name, TPM_ALG_SHA256);- if (sha256_buffer(pub, sizeof(pub), name + 2))- memset(name, 0, TPM_NAME_LEN);+ return sha256_buffer(pub, sizeof(pub), name + 2);}int tpm_object_name(const __u8 *public, size_t len,@@ -301,20 +300,19 @@ int tpm_object_name(const __u8 *public, size_t len,alg != TPM_ALG_SHA256)return -EINVAL;put_be16(name, TPM_ALG_SHA256);- sha256_buffer(public, len, name + 2);- return 0;+ return sha256_buffer(public, len, name + 2);}-void tpm_qualified_name(__u32 hierarchy,- const __u8 name[TPM_NAME_LEN],- __u8 out[TPM_NAME_LEN])+int tpm_qualified_name(__u32 hierarchy,+ const __u8 name[TPM_NAME_LEN],+ __u8 out[TPM_NAME_LEN]){__u8 buf[4 + TPM_NAME_LEN];put_be32(buf, hierarchy);memcpy(buf + 4, name, TPM_NAME_LEN);put_be16(out, TPM_ALG_SHA256);- sha256_buffer(buf, sizeof(buf), out + 2);+ return sha256_buffer(buf, sizeof(buf), out + 2);}const __u8 shirudo_tpm_ek_policy[32] = {@@ -460,6 +458,7 @@ int tpm_kdfa(const __u8 *key, size_t key_len, const char *label,{__u8 buf[4 + 64 + 1 + 2 * TPM_NAME_LEN + 4], block[32];size_t label_len = strlen(label), done = 0, off, n;+ int ret = 0;__u32 i;if (label_len > 64 || u_len > TPM_NAME_LEN ||@@ -479,27 +478,28 @@ int tpm_kdfa(const __u8 *key, size_t key_len, const char *label,off += v_len;put_be32(buf + off, (__u32)(out_len * 8));off += 4;- if (hmac_sha256(key, key_len, buf, off, block))- return -EIO;+ if (hmac_sha256(key, key_len, buf, off, block)) {+ ret = -EIO;+ break;+ }n = out_len - done < 32 ? out_len - done : 32;memcpy(out + done, block, n);done += n;}memzero_explicit(block, sizeof(block));- return 0;+ return ret;}-void tpm_kdfe(const __u8 z[32], const char *label,- const __u8 *party_u, size_t u_len,- const __u8 *party_v, size_t v_len, __u8 out[32])+int tpm_kdfe(const __u8 z[32], const char *label,+ const __u8 *party_u, size_t u_len,+ const __u8 *party_v, size_t v_len, __u8 out[32]){__u8 buf[4 + 32 + 64 + 1 + 2 * 64];size_t label_len = strlen(label), off = 4;+ int ret;- if (label_len > 64 || u_len > 64 || v_len > 64) {- memset(out, 0, 32);- return;- }+ if (label_len > 64 || u_len > 64 || v_len > 64)+ return -EINVAL;put_be32(buf, 1);memcpy(buf + off, z, 32);off += 32;@@ -512,8 +512,9 @@ void tpm_kdfe(const __u8 z[32], const char *label,if (v_len)memcpy(buf + off, party_v, v_len);off += v_len;- sha256_buffer(buf, off, out);+ ret = sha256_buffer(buf, off, out);memzero_explicit(buf, sizeof(buf));+ return ret;}int tpm_credential_protect(const __u8 seed[32],diff --git a/util/tpm_wire.h b/util/tpm_wire.hindex 0fa03eaf..5a828f40 100644--- a/util/tpm_wire.h+++ b/util/tpm_wire.h@@ -48,19 +48,19 @@ BUILD_BUG_ON_MSG(TPM_NAME_LEN != 2 + TPM_DIGEST_LEN,void tpm_writer_public(const __u8 modulus[TPM_NV_RSA_BYTES],__u8 out[TPM_NV_RSA_PUBLIC_AREA_LEN]);int tpm_nv_extend_cphash(const __u8 name[TPM_NAME_LEN],- const __u8 event[32], __u8 out[32]);+ const __u8 event[32], __u8 out[32]) __must_check;int tpm_writer_auth_hash(const __u8 *nonce, size_t nonce_len,- const __u8 cphash[32], __u8 out[32]);+ const __u8 cphash[32], __u8 out[32]) __must_check;int tpm_policy_signed(const __u8 prev[32],const __u8 writer_public[TPM_NV_RSA_BYTES],- __u8 out[32]);+ __u8 out[32]) __must_check;int tpm_policy_command_code(const __u8 prev[TPM_DIGEST_LEN],__u32 command_code,- __u8 out[TPM_DIGEST_LEN]);+ __u8 out[TPM_DIGEST_LEN]) __must_check;int tpm_register_policy_final(const __u8 branch_written[32],const __u8 branch_init[32],const __u8 writer_public[TPM_NV_RSA_BYTES],- __u8 policy[32]);+ __u8 policy[32]) __must_check;/** Attributes of the measurement register: an extend-only NV index@@ -74,14 +74,14 @@ int tpm_register_policy_final(const __u8 branch_written[32],int tpm_policy_nv_written(const __u8 prev[TPM_DIGEST_LEN],bool written,- __u8 out[TPM_DIGEST_LEN]);+ __u8 out[TPM_DIGEST_LEN]) __must_check;int tpm_policy_pcr(const __u8 prev[TPM_DIGEST_LEN],__u32 pcr_mask,const __u8 pcr_digest[TPM_DIGEST_LEN],- __u8 out[TPM_DIGEST_LEN]);+ __u8 out[TPM_DIGEST_LEN]) __must_check;int tpm_policy_or(const __u8 a[TPM_DIGEST_LEN],const __u8 b[TPM_DIGEST_LEN],- __u8 out[TPM_DIGEST_LEN]);+ __u8 out[TPM_DIGEST_LEN]) __must_check;/** The register's write policy from the values the binding PCRs held@@ -95,25 +95,25 @@ int tpm_register_policy(__u32 pcr_mask,const __u8 writer_public[TPM_NV_RSA_BYTES],__u8 branch_written[32],__u8 branch_init[32],- __u8 policy[32]);+ __u8 policy[32]) __must_check;/* Name of an NV index: nameAlg || SHA-256(TPMS_NV_PUBLIC). */-void tpm_nv_name(__u32 index, __u32 attrs,- const __u8 policy[TPM_DIGEST_LEN],- __u16 data_size,- __u8 name[TPM_NAME_LEN]);+int tpm_nv_name(__u32 index, __u32 attrs,+ const __u8 policy[TPM_DIGEST_LEN],+ __u16 data_size,+ __u8 name[TPM_NAME_LEN]) __must_check;int tpm_object_name(const __u8 *public, size_t len,- __u8 name[TPM_NAME_LEN]);+ __u8 name[TPM_NAME_LEN]) __must_check;/** Qualified name of a primary object: nameAlg || SHA-256(hierarchy* handle || name). Attestations carry the signer's qualified name.*/#define TPM_RH_ENDORSEMENT 0x4000000Bu-void tpm_qualified_name(__u32 hierarchy,- const __u8 name[TPM_NAME_LEN],- __u8 out[TPM_NAME_LEN]);+int tpm_qualified_name(__u32 hierarchy,+ const __u8 name[TPM_NAME_LEN],+ __u8 out[TPM_NAME_LEN]) __must_check;/** The TCG default endorsement key templates: a restricted decryption@@ -149,9 +149,9 @@ struct tpm_public {};int tpm_public_parse(const __u8 *buf, size_t len,- struct tpm_public *out);+ struct tpm_public *out) __must_check;int tpm_public_marshal(const struct tpm_public *pub,- __u8 *out, size_t cap, size_t *len);+ __u8 *out, size_t cap, size_t *len) __must_check;struct tpm_ecc_public {__u32 attrs;@@ -163,7 +163,7 @@ struct tpm_ecc_public {};int tpm_ecc_public_parse(const __u8 *buf, size_t len,- struct tpm_ecc_public *out);+ struct tpm_ecc_public *out) __must_check;/** The key derivations of TPM 2.0 Part 1 with SHA-256: KDFa (SP800-108@@ -173,10 +173,10 @@ int tpm_ecc_public_parse(const __u8 *buf, size_t len,int tpm_kdfa(const __u8 *key, size_t key_len, const char *label,const __u8 *ctx_u, size_t u_len,const __u8 *ctx_v, size_t v_len,- __u8 *out, size_t out_len);-void tpm_kdfe(const __u8 z[32], const char *label,- const __u8 *party_u, size_t u_len,- const __u8 *party_v, size_t v_len, __u8 out[32]);+ __u8 *out, size_t out_len) __must_check;+int tpm_kdfe(const __u8 z[32], const char *label,+ const __u8 *party_u, size_t u_len,+ const __u8 *party_v, size_t v_len, __u8 out[32]) __must_check;/** Credential protection (Part 1, 24.4): from a seed shared with the@@ -190,10 +190,10 @@ void tpm_kdfe(const __u8 z[32], const char *label,int tpm_credential_protect(const __u8 seed[32],const __u8 name[TPM_NAME_LEN],const __u8 credential[TPM_CREDENTIAL_LEN],- __u8 out[TPM_ID_OBJECT_LEN]);+ __u8 out[TPM_ID_OBJECT_LEN]) __must_check;int tpm_ecdsa_signature_parse(const __u8 *buf, size_t len,- __u8 r[32], __u8 s[32]);+ __u8 r[32], __u8 s[32]) __must_check;struct tpm_attest {__u16 type;@@ -223,6 +223,6 @@ struct tpm_attest {* input stay valid as long as buf does.*/int tpm_attest_parse(const __u8 *buf, size_t len,- struct tpm_attest *out);+ struct tpm_attest *out) __must_check;#endif /* SHIRUDO_TPM_WIRE_H */