Received: by 2002:a25:4158:0:0:0:0:0 with SMTP id o85csp600430yba; Sun, 31 Mar 2019 07:47:36 -0700 (PDT) X-Google-Smtp-Source: APXvYqwhlgq7eDFG4rOT0O5wEMrCgWVPLAsMkXv+Q8CqBBbjHgVt7iIRdd1XcbXYORhdGLFfYEQe X-Received: by 2002:a62:174c:: with SMTP id 73mr57851559pfx.33.1554043656658; Sun, 31 Mar 2019 07:47:36 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1554043656; cv=none; d=google.com; s=arc-20160816; b=xtcd85NGPhSK/Sz55o26ePSteMxRvBb5yvMzJCEAfBYBf+FfHRqNVigdlpW+G5j1mO 3njCIDdRbG5zfNNN1hVofHCyKtJsZ6Dc2cXEhjsN/yXGA6uvKcd9gc+BYGWB9H7Ej4zE tcehiqQlFFZuDH//Ao6RjEZOYBH7n4pm5Ass0dpd3d0z0pIOzzh+PHZqCalUde1vSOYu 0geXrKJe+zwr+Nx2yF1RiqVknwrFespH9II/mc/O029vxsE+Tuo0iKtUYevA6UApN7Tw bqMNMBVwt0Dp+bb24LU6+lPng/IBkJR0AQoXWcFVG8aDWB/py5CsTdaWTSl65ThEcxkX gq+A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :message-id:date:subject:to:from; bh=6BVT5iEa6/xONH8/1SZ38+lMspee2XBpV5xjnOCJ/no=; b=kh/PjGrXKrFObZ1uyWXex9C0fOYgkmQ6DPoL7QOK+80pZh0MeqG5ouyCIS7btrxEe5 ZBAP86aP2fkXZzyWvYSJITrXikk5QFYgqH1vJYdrbfEk6D5V1H0EamPifgKLO8aEy4FC qHO4weyLlu+6ZSIFoISK7wCpA75yxPRwZl0ue3lSe1TNHuhjUq9ZMzuUlnkA9OiFIMTh k7t2UIKgyKMKpfgdMc3xK8Z449/MMaoxo75u6qUmjU+9VAISPHwRcfcwA7lB2ZVKC4y8 MxEEoZ4XoGjn9K/B8h3oAwWwV1haHnN/gRoUVvVuwVxjCwVmubExbZYhDPzOCcJ6cuQO +0fQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id x4si6603602pll.344.2019.03.31.07.47.20; Sun, 31 Mar 2019 07:47:36 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1731254AbfCaOqn (ORCPT + 99 others); Sun, 31 Mar 2019 10:46:43 -0400 Received: from vmicros1.altlinux.org ([194.107.17.57]:41198 "EHLO vmicros1.altlinux.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1731130AbfCaOqm (ORCPT ); Sun, 31 Mar 2019 10:46:42 -0400 Received: from imap.altlinux.org (imap.altlinux.org [194.107.17.38]) by vmicros1.altlinux.org (Postfix) with ESMTP id E40C072CCAE; Sun, 31 Mar 2019 17:46:38 +0300 (MSK) Received: from beacon.altlinux.org (unknown [185.6.174.98]) by imap.altlinux.org (Postfix) with ESMTPSA id B87014A4A16; Sun, 31 Mar 2019 17:46:38 +0300 (MSK) From: Vitaly Chikunov To: Herbert Xu , David Howells , Denis Kenzior , keyrings@vger.kernel.org, linux-crypto@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH] KEYS: asym_tpm: move setting set_pub_key parameters into derive_pub_key Date: Sun, 31 Mar 2019 17:46:12 +0300 Message-Id: <20190331144612.22029-1-vt@altlinux.org> X-Mailer: git-send-email 2.11.0 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org The only reason for derive_pub_key() is to take a TPM-blob formatted public key and convert it to ASN.1 format understood by crypto_akcipher_set_pub_key. Because we are changing set_pub_key input format (adding key parameters) - update that function to generate proper zero values instead of generating them each time after the call. Suggested-by: Denis Kenzior Signed-off-by: Vitaly Chikunov --- This should be applied after "crypto: add EC-RDSA (GOST 34.10) algorithm" patchset. crypto/asymmetric_keys/asym_tpm.c | 34 ++++++++++++++-------------------- 1 file changed, 14 insertions(+), 20 deletions(-) diff --git a/crypto/asymmetric_keys/asym_tpm.c b/crypto/asymmetric_keys/asym_tpm.c index d95d7ec50e5a..b138acf3a5e6 100644 --- a/crypto/asymmetric_keys/asym_tpm.c +++ b/crypto/asymmetric_keys/asym_tpm.c @@ -276,6 +276,10 @@ static int tpm_sign(struct tpm_buf *tb, return datalen; } + +/* Room to fit two u32 zeros for algo id and parameters length. */ +#define SETKEY_PARAMS_SIZE (sizeof(u32) * 2) + /* * Maximum buffer size for the BER/DER encoded public key. The public key * is of the form SEQUENCE { INTEGER n, INTEGER e } where n is a maximum 2048 @@ -286,8 +290,9 @@ static int tpm_sign(struct tpm_buf *tb, * - 257 bytes of n * - max 2 bytes for INTEGER e type/length * - 3 bytes of e + * - 4+4 of zeros for set_pub_key parameters (SETKEY_PARAMS_SIZE) */ -#define PUB_KEY_BUF_SIZE (4 + 4 + 257 + 2 + 3) +#define PUB_KEY_BUF_SIZE (4 + 4 + 257 + 2 + 3 + SETKEY_PARAMS_SIZE) /* * Provide a part of a description of the key for /proc/keys. @@ -364,6 +369,8 @@ static uint32_t derive_pub_key(const void *pub_key, uint32_t len, uint8_t *buf) cur = encode_tag_length(cur, 0x02, sizeof(e)); memcpy(cur, e, sizeof(e)); cur += sizeof(e); + /* Zero parameters to satisfy set_pub_key ABI. */ + memset(cur, 0, SETKEY_PARAMS_SIZE); return cur - buf; } @@ -395,12 +402,6 @@ static int determine_akcipher(const char *encoding, const char *hash_algo, return -ENOPKG; } -static u8 *tpm_pack_u32(u8 *dst, u32 val) -{ - memcpy(dst, &val, sizeof(val)); - return dst + sizeof(val); -} - /* * Query information about a key. */ @@ -428,14 +429,11 @@ static int tpm_key_query(const struct kernel_pkey_params *params, der_pub_key_len = derive_pub_key(tk->pub_key, tk->pub_key_len, der_pub_key); - pkey = kmalloc(der_pub_key_len + sizeof(u32) * 2, GFP_KERNEL); + pkey = kmalloc(der_pub_key_len + SETKEY_PARAMS_SIZE, GFP_KERNEL); if (!pkey) goto error_free_tfm; - memcpy(pkey, der_pub_key, der_pub_key_len); + memcpy(pkey, der_pub_key, der_pub_key_len + SETKEY_PARAMS_SIZE); ptr = pkey + der_pub_key_len; - /* Set dummy parameters to satisfy set_pub_key ABI. */ - ptr = tpm_pack_u32(ptr, 0); /* algo */ - ptr = tpm_pack_u32(ptr, 0); /* parameter length */ ret = crypto_akcipher_set_pub_key(tfm, pkey, der_pub_key_len); if (ret < 0) @@ -493,13 +491,11 @@ static int tpm_key_encrypt(struct tpm_key *tk, der_pub_key_len = derive_pub_key(tk->pub_key, tk->pub_key_len, der_pub_key); - pkey = kmalloc(der_pub_key_len + sizeof(u32) * 2, GFP_KERNEL); + pkey = kmalloc(der_pub_key_len + SETKEY_PARAMS_SIZE, GFP_KERNEL); if (!pkey) goto error_free_tfm; - memcpy(pkey, der_pub_key, der_pub_key_len); + memcpy(pkey, der_pub_key, der_pub_key_len + SETKEY_PARAMS_SIZE); ptr = pkey + der_pub_key_len; - ptr = tpm_pack_u32(ptr, 0); /* algo */ - ptr = tpm_pack_u32(ptr, 0); /* parameter length */ ret = crypto_akcipher_set_pub_key(tfm, pkey, der_pub_key_len); if (ret < 0) @@ -798,13 +794,11 @@ static int tpm_key_verify_signature(const struct key *key, der_pub_key_len = derive_pub_key(tk->pub_key, tk->pub_key_len, der_pub_key); - pkey = kmalloc(der_pub_key_len + sizeof(u32) * 2, GFP_KERNEL); + pkey = kmalloc(der_pub_key_len + SETKEY_PARAMS_SIZE, GFP_KERNEL); if (!pkey) goto error_free_tfm; - memcpy(pkey, der_pub_key, der_pub_key_len); + memcpy(pkey, der_pub_key, der_pub_key_len + SETKEY_PARAMS_SIZE); ptr = pkey + der_pub_key_len; - ptr = tpm_pack_u32(ptr, 0); /* algo */ - ptr = tpm_pack_u32(ptr, 0); /* parameter length */ ret = crypto_akcipher_set_pub_key(tfm, pkey, der_pub_key_len); if (ret < 0) -- 2.11.0