Received: by 2002:ab2:7855:0:b0:1f9:5764:f03e with SMTP id m21csp984378lqp; Thu, 23 May 2024 06:08:58 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCVoWzt3MJ+zZS3KO7qIi5s+m743L7YO5MQ1oSg9DCZU2mzrrORh8VxOHhgQJyJh0jCIaWPQ/cD8ot5N9UVN6SWJQ79pr2rbCSOV7INTng== X-Google-Smtp-Source: AGHT+IE6K7jvNS4i6HHowz8cgNHV/6YbW2sEqOnkXuNwMnRQMAycvUjM28NLvBPUDN2+89RKrMwe X-Received: by 2002:a17:906:c17:b0:a59:edbc:193d with SMTP id a640c23a62f3a-a62281e1a9cmr451113266b.55.1716469738064; Thu, 23 May 2024 06:08:58 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1716469738; cv=pass; d=google.com; s=arc-20160816; b=bL+FRqL9nLoR/nz2KT3Uts7J/NY94b+neJ/I1nKeSIcKWXemG31Awuoinh8CWpLz8h u2z14xCr76FycZk28nihGo3P5BtbYdlX+nkBSCe19lb+iJFnQKeTp0lc7/ibUqb9udDs hrmTpeQhLbDXUFFqN+33IbBDkEr2J8ILzwXwIeEfYjAGC4of8nLSnN3Xcw08D97gDJXV SpZ7InuiDU1plAkjwwKB46rM7uc0PKISeceeb52iAxlQpB4aBXHD1LWrX09A4yInMsIk IOeaimj45NEXKtZJCRbJze54IGwqVJW7zqlTIQOPnrvw2AZ9GYBNHwABrMFGCd0FWBzl 3CbA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:message-id:date:subject:cc:to :from:dkim-signature; bh=Xf5MkI52KQU74lIz3mXiD929wWRMWnGipV3ggLe/gEc=; fh=9197gRsVigy7/E/zPC053HBDtKGJTObcqt2/kaVl2u8=; b=e2PMBtD7TPKgziSMDS8C/tKn3Ke/Qj3+IHSM8NXegET/isElmXh3AMguFXw5nMqtJk /BPPyMU6g6G/X+9uTRJeYedsQEE7z/3Ac9jnXcRzkUv1+4xEZYAKH0QZtMX0NZIi7+6v +gvd5NitNIo8wqwZrpzZ/McNaecIrx02+rKOlncJ1fScmO3HoexMCI4tNtYKvDK/wHJ3 8dGJDjHBuYftb5U+469jmzKLz+UUOH4EBQNM8Nb5hRuinCWyH35Km24hb2Iv3Zn2S5g3 BvLexGQxHnP9CdTNQRiIKFvnxF+hmAcyqjO/q+Mw1OTouOI8vWT3WeQfwZE5XgHr7ffb j6dw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=obwQ+HW3; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-crypto+bounces-4345-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-crypto+bounces-4345-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [2604:1380:4601:e00::3]) by mx.google.com with ESMTPS id a640c23a62f3a-a5a6f060d2dsi1209433266b.199.2024.05.23.06.08.57 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 23 May 2024 06:08:58 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-crypto+bounces-4345-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) client-ip=2604:1380:4601:e00::3; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=obwQ+HW3; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-crypto+bounces-4345-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-crypto+bounces-4345-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by am.mirrors.kernel.org (Postfix) with ESMTPS id A35831F2249B for ; Thu, 23 May 2024 13:08:57 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id A2E8414A4D9; Thu, 23 May 2024 13:08:50 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="obwQ+HW3" X-Original-To: linux-crypto@vger.kernel.org Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 5360D13B7BC; Thu, 23 May 2024 13:08:50 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716469730; cv=none; b=E8QLTW9oeSPa7m9dDi7gW2sjWYC6pkrnsq4zpBz4p6EgAihK34yaIUhQ8SkzUir3T1tosb922gWoyU0IDWSzLeSAb9PjzZ28PFjzGj72aDfLE86kaukkSxxfSFDhnb81z3OQHYY7sYnyXfNUGAG3T4WvjY12EwaNPxcXdLtSz9o= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716469730; c=relaxed/simple; bh=5x2a4GM3brTtaycRNakzuLxHq3n5FCWmESOoxOPKfDg=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version; b=PFxiQmoYRWCSgxc2jL/++tI6NIkK5UsD/BqmAj1ZxPMF+E7N1wDbEH7mQPTY+cvW2TizTAY9Dyjjkbk3DAudx2v0l4Q3fkYtx2Z3/tzrJqa8y9B0woRMXo2tYDYO1vy3QVksGELjUpeP7bE32OaBmZf8j/uvCZ+u5D3Zz2NkgMM= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=obwQ+HW3; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id 56226C2BD10; Thu, 23 May 2024 13:08:49 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1716469729; bh=5x2a4GM3brTtaycRNakzuLxHq3n5FCWmESOoxOPKfDg=; h=From:To:Cc:Subject:Date:From; b=obwQ+HW3dkBtcZ2A7SnkC9L0Csk8Z3mlB8nmpNniwjdIiufbdTCQo56oh4HCPaWqg gEmXAmN1/XFKQamXKamiG0EiecKHbxM74ffFuv1M1CFqqQ5d270zshNA9U6d9JvHEr xMLoKSllvP4XLwdLtjxmE6QqRB/Zhj3TV5R64qVgqmfnJ7zMWuMfcnsxBlK+PjB0eN K6QKqzPMv3XUgWJtJYzHQsQqc7AE2cu9x8lbgMxBfJVks5GfhOu9pD6CSLWJnAxLTW enWO6FfGpHLuFMcAtS9ukesEZZl2OPpxXVNFGMHFWMkiJgihPF5C46koke1xoUSkmt SQuShp1g9Zd5w== From: Jarkko Sakkinen To: linux-integrity@vger.kernel.org Cc: keyrings@vger.kernel.org, David Woodhouse , Eric Biggers , James Bottomley , Jarkko Sakkinen , Herbert Xu , "David S. Miller" , Andrew Morton , James Bottomley , Mimi Zohar , David Howells , Paul Moore , James Morris , "Serge E. Hallyn" , linux-crypto@vger.kernel.org (open list:CRYPTO API), linux-kernel@vger.kernel.org (open list), linux-security-module@vger.kernel.org (open list:SECURITY SUBSYSTEM) Subject: [PATCH] KEYS: trusted: Use ASN.1 encoded OID Date: Thu, 23 May 2024 16:08:35 +0300 Message-ID: <20240523130839.31265-1-jarkko@kernel.org> X-Mailer: git-send-email 2.45.1 Precedence: bulk X-Mailing-List: linux-crypto@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit There's no reason to encode OID_TPMSealedData at run-time, as it never changes. Replace it with the encoded version, which has exactly the same size: 67 81 05 0A 01 05 Include OBJECT IDENTIFIER (0x06) tag and length as the epilogue so that the OID can be simply copied to the blob. Signed-off-by: Jarkko Sakkinen --- include/linux/asn1_encoder.h | 4 - lib/asn1_encoder.c | 91 ----------------------- security/keys/trusted-keys/trusted_tpm2.c | 10 ++- 3 files changed, 7 insertions(+), 98 deletions(-) diff --git a/include/linux/asn1_encoder.h b/include/linux/asn1_encoder.h index 08cd0c2ad34f..afeefdfe2525 100644 --- a/include/linux/asn1_encoder.h +++ b/include/linux/asn1_encoder.h @@ -8,14 +8,10 @@ #include #include -#define asn1_oid_len(oid) (sizeof(oid)/sizeof(u32)) unsigned char * asn1_encode_integer(unsigned char *data, const unsigned char *end_data, s64 integer); unsigned char * -asn1_encode_oid(unsigned char *data, const unsigned char *end_data, - u32 oid[], int oid_len); -unsigned char * asn1_encode_tag(unsigned char *data, const unsigned char *end_data, u32 tag, const unsigned char *string, int len); unsigned char * diff --git a/lib/asn1_encoder.c b/lib/asn1_encoder.c index 0fd3c454a468..c0db3cbebe89 100644 --- a/lib/asn1_encoder.c +++ b/lib/asn1_encoder.c @@ -85,97 +85,6 @@ asn1_encode_integer(unsigned char *data, const unsigned char *end_data, } EXPORT_SYMBOL_GPL(asn1_encode_integer); -/* calculate the base 128 digit values setting the top bit of the first octet */ -static int asn1_encode_oid_digit(unsigned char **_data, int *data_len, u32 oid) -{ - unsigned char *data = *_data; - int start = 7 + 7 + 7 + 7; - int ret = 0; - - if (*data_len < 1) - return -EINVAL; - - /* quick case */ - if (oid == 0) { - *data++ = 0x80; - (*data_len)--; - goto out; - } - - while (oid >> start == 0) - start -= 7; - - while (start > 0 && *data_len > 0) { - u8 byte; - - byte = oid >> start; - oid = oid - (byte << start); - start -= 7; - byte |= 0x80; - *data++ = byte; - (*data_len)--; - } - - if (*data_len > 0) { - *data++ = oid; - (*data_len)--; - } else { - ret = -EINVAL; - } - - out: - *_data = data; - return ret; -} - -/** - * asn1_encode_oid() - encode an oid to ASN.1 - * @data: position to begin encoding at - * @end_data: end of data pointer, points one beyond last usable byte in @data - * @oid: array of oids - * @oid_len: length of oid array - * - * this encodes an OID up to ASN.1 when presented as an array of OID values - */ -unsigned char * -asn1_encode_oid(unsigned char *data, const unsigned char *end_data, - u32 oid[], int oid_len) -{ - int data_len = end_data - data; - unsigned char *d = data + 2; - int i, ret; - - if (WARN(oid_len < 2, "OID must have at least two elements")) - return ERR_PTR(-EINVAL); - - if (WARN(oid_len > 32, "OID is too large")) - return ERR_PTR(-EINVAL); - - if (IS_ERR(data)) - return data; - - - /* need at least 3 bytes for tag, length and OID encoding */ - if (data_len < 3) - return ERR_PTR(-EINVAL); - - data[0] = _tag(UNIV, PRIM, OID); - *d++ = oid[0] * 40 + oid[1]; - - data_len -= 3; - - for (i = 2; i < oid_len; i++) { - ret = asn1_encode_oid_digit(&d, &data_len, oid[i]); - if (ret < 0) - return ERR_PTR(ret); - } - - data[1] = d - data - 2; - - return d; -} -EXPORT_SYMBOL_GPL(asn1_encode_oid); - /** * asn1_encode_length() - encode a length to follow an ASN.1 tag * @data: pointer to encode at diff --git a/security/keys/trusted-keys/trusted_tpm2.c b/security/keys/trusted-keys/trusted_tpm2.c index 8b7dd73d94c1..dadeed35627c 100644 --- a/security/keys/trusted-keys/trusted_tpm2.c +++ b/security/keys/trusted-keys/trusted_tpm2.c @@ -26,7 +26,8 @@ static struct tpm2_hash tpm2_hash_map[] = { {HASH_ALGO_SM3_256, TPM_ALG_SM3_256}, }; -static u32 tpm2key_oid[] = { 2, 23, 133, 10, 1, 5 }; +/* Encoded OID_TPMSealedData. */ +static u8 OID_TPMSealedData_ASN1[] = {0x06, 0x06, 0x67, 0x81, 0x05, 0x0a, 0x01 0x05}; static int tpm2_key_encode(struct trusted_key_payload *payload, struct trusted_key_options *options, @@ -51,8 +52,8 @@ static int tpm2_key_encode(struct trusted_key_payload *payload, if (!scratch) return -ENOMEM; - work = asn1_encode_oid(work, end_work, tpm2key_oid, - asn1_oid_len(tpm2key_oid)); + work = memcpy(work, OID_TPMSealedData_ASN1, sizeof(OID_TPMSealedData_ASN1)); + work += OID_TPMSealedData_ASN1; if (options->blobauth_len == 0) { unsigned char bool[3], *w = bool; @@ -90,6 +91,9 @@ static int tpm2_key_encode(struct trusted_key_payload *payload, goto err; } + print_hex_dump(KERN_INFO, "", DUMP_PREFIX_NONE, 16, 1, + payload->blob, work1 - payload->blob, 0); + kfree(scratch); return work1 - payload->blob; -- 2.45.1