Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-9.1 required=3.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_PATCH,MAILING_LIST_MULTI, SIGNED_OFF_BY,SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 213C8C4360F for ; Wed, 20 Mar 2019 02:03:48 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id CE92D217F4 for ; Wed, 20 Mar 2019 02:03:47 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=nxp.com header.i=@nxp.com header.b="jhGvlbs2" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727461AbfCTCDq (ORCPT ); Tue, 19 Mar 2019 22:03:46 -0400 Received: from mail-eopbgr60053.outbound.protection.outlook.com ([40.107.6.53]:36672 "EHLO EUR04-DB3-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1727358AbfCTCDq (ORCPT ); Tue, 19 Mar 2019 22:03:46 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=nxp.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=piN3qpLOkhyuVUm166R7+ki1achFxxpq3XDBuK+Zs80=; b=jhGvlbs2ZRGpw+hAd3jsrBZSQxIll5eT113aiYEZeYaxW4KcX27AEzuJR5rGmYA+lIEWPOB5M/j9e3FqQrk7rpALoDHri/LpdTe7wGrsB9WfuocunFhzzvr8nCWz0EGZ0+A/woamEvZBo/F5Y9EoW3BeS9i9KmwvNvBC7a8gjCc= Received: from DB7PR04MB4252.eurprd04.prod.outlook.com (52.135.131.26) by DB7PR04MB4651.eurprd04.prod.outlook.com (52.135.138.161) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.1709.13; Wed, 20 Mar 2019 02:03:36 +0000 Received: from DB7PR04MB4252.eurprd04.prod.outlook.com ([fe80::9ca:d78:c154:25b5]) by DB7PR04MB4252.eurprd04.prod.outlook.com ([fe80::9ca:d78:c154:25b5%4]) with mapi id 15.20.1709.015; Wed, 20 Mar 2019 02:03:36 +0000 From: Vakul Garg To: "netdev@vger.kernel.org" CC: "borisp@mellanox.com" , "aviadye@mellanox.com" , "davejwatson@fb.com" , "davem@davemloft.net" , "doronrk@fb.com" , Vakul Garg Subject: [PATCH net-next v2] net/tls: Add support of AES128-CCM based ciphers Thread-Topic: [PATCH net-next v2] net/tls: Add support of AES128-CCM based ciphers Thread-Index: AQHU3sEhtCxyZM0/30mYoHh9q7nF8Q== Date: Wed, 20 Mar 2019 02:03:36 +0000 Message-ID: <20190320020051.26034-1-vakul.garg@nxp.com> Accept-Language: en-US Content-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: x-clientproxiedby: BM1PR01CA0133.INDPRD01.PROD.OUTLOOK.COM (2603:1096:b00:40::27) To DB7PR04MB4252.eurprd04.prod.outlook.com (2603:10a6:5:27::26) authentication-results: spf=none (sender IP is ) smtp.mailfrom=vakul.garg@nxp.com; x-ms-exchange-messagesentrepresentingtype: 1 x-mailer: git-send-email 2.13.6 x-originating-ip: [92.120.1.70] x-ms-publictraffictype: Email x-ms-office365-filtering-correlation-id: 90eae028-5df9-4934-c355-08d6acd84389 x-ms-office365-filtering-ht: Tenant x-microsoft-antispam: BCL:0;PCL:0;RULEID:(2390118)(7020095)(4652040)(8989299)(5600127)(711020)(4605104)(4618075)(4534185)(4627221)(201703031133081)(201702281549075)(8990200)(2017052603328)(7153060)(7193020);SRVR:DB7PR04MB4651; x-ms-traffictypediagnostic: DB7PR04MB4651: x-microsoft-antispam-prvs: x-forefront-prvs: 098291215C x-forefront-antispam-report: SFV:NSPM;SFS:(10009020)(396003)(39860400002)(376002)(136003)(346002)(366004)(199004)(189003)(106356001)(14444005)(81166006)(14454004)(476003)(2616005)(256004)(99286004)(6916009)(478600001)(8936002)(86362001)(186003)(486006)(305945005)(97736004)(2501003)(105586002)(81156014)(7736002)(44832011)(4326008)(50226002)(1076003)(2351001)(68736007)(102836004)(25786009)(6506007)(5660300002)(71190400001)(5640700003)(66066001)(1730700003)(6486002)(386003)(6436002)(6116002)(52116002)(54906003)(316002)(2906002)(53936002)(6512007)(71200400001)(36756003)(26005)(3846002)(8676002);DIR:OUT;SFP:1101;SCL:1;SRVR:DB7PR04MB4651;H:DB7PR04MB4252.eurprd04.prod.outlook.com;FPR:;SPF:None;LANG:en;PTR:InfoNoRecords;A:1;MX:1; received-spf: None (protection.outlook.com: nxp.com does not designate permitted sender hosts) x-ms-exchange-senderadcheck: 1 x-microsoft-antispam-message-info: IZTtRkYeIMq0vzfeSy021+5v7gnX9bfDotqXpMbW5lfAe6jKaAKEJYXZSWNCWbw4gF437WF65qbx8pZlg241t21IB57IohLvjaCr6GDfeeI7SunKVhOaguHxJ6a9+mD601sD21xP+sFTIMXFzM4JfJ9EpLI7AhD9LXC7rHhHCTfjK2wlfdopHSnMmHU5stFFIUrPB1OSwyssEWAKWDcQ1K84vIFFsRmkdtDaPKy6sHLpoqRW6XA+GcInGcCAuVlECcSqVEmNtlJ8kFEvgsIMQSF4hn5RZLgjZF79+W3w2k2ZrlI28Eqg0ZCHWk7Ei3tjnhpMn856CyfWA7rA53sdciA/xM5MSMBjnBZJUJyDXpEjJJ+hGBm7bkMyn0cQDwlBHKp7kTZzWf5BofhK1CKXh6gSO8Crsb6hY7t1zo62Muc= Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 X-OriginatorOrg: nxp.com X-MS-Exchange-CrossTenant-Network-Message-Id: 90eae028-5df9-4934-c355-08d6acd84389 X-MS-Exchange-CrossTenant-originalarrivaltime: 20 Mar 2019 02:03:36.7780 (UTC) X-MS-Exchange-CrossTenant-fromentityheader: Hosted X-MS-Exchange-CrossTenant-id: 686ea1d3-bc2b-4c6f-a92c-d99c5c301635 X-MS-Exchange-CrossTenant-mailboxtype: HOSTED X-MS-Exchange-Transport-CrossTenantHeadersStamped: DB7PR04MB4651 Sender: netdev-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org Added support for AES128-CCM based record encryption. AES128-CCM is similar to AES128-GCM. Both of them have same salt/iv/mac size. The notable difference between the two is that while invoking AES128-CCM operation, the salt||nonce (which is passed as IV) has to be prefixed with a hardcoded value '2'. Further, CCM implementation in kernel requires IV passed in crypto_aead_request() to be full '16' bytes. Therefore, the record structure 'struct tls_rec' has been modified to reserve '16' bytes for IV. This works for both GCM and CCM based cipher. Signed-off-by: Vakul Garg --- Changes in v2: - Defined a CPP macro to replace hardcoded constant '2' used in first byte of CCM IV. include/net/tls.h | 15 +++++++++-- include/uapi/linux/tls.h | 15 +++++++++++ net/tls/tls_main.c | 31 ++++++++++++---------- net/tls/tls_sw.c | 67 ++++++++++++++++++++++++++++++++++++--------= ---- 4 files changed, 97 insertions(+), 31 deletions(-) diff --git a/include/net/tls.h b/include/net/tls.h index a5a938583295..3ce71d78414c 100644 --- a/include/net/tls.h +++ b/include/net/tls.h @@ -60,6 +60,17 @@ #define TLS_AAD_SPACE_SIZE 13 #define TLS_DEVICE_NAME_MAX 32 =20 +#define MAX_IV_SIZE 16 + +/* For AES-CCM, the full 16-bytes of IV is made of '4' fields of given siz= es. + * + * IV[16] =3D b0[1] || implicit nonce[4] || explicit nonce[8] || length[3] + * + * The field 'length' is encoded in field 'b0' as '(length width - 1)'. + * Hence b0 contains (3 - 1) =3D 2. + */ +#define TLS_AES_CCM_IV_B0_BYTE 2 + /* * This structure defines the routines for Inline TLS driver. * The following routines are optional and filled with a @@ -123,8 +134,7 @@ struct tls_rec { struct scatterlist sg_content_type; =20 char aad_space[TLS_AAD_SPACE_SIZE]; - u8 iv_data[TLS_CIPHER_AES_GCM_128_IV_SIZE + - TLS_CIPHER_AES_GCM_128_SALT_SIZE]; + u8 iv_data[MAX_IV_SIZE]; struct aead_request aead_req; u8 aead_req_ctx[]; }; @@ -219,6 +229,7 @@ struct tls_prot_info { u16 tag_size; u16 overhead_size; u16 iv_size; + u16 salt_size; u16 rec_seq_size; u16 aad_size; u16 tail_size; diff --git a/include/uapi/linux/tls.h b/include/uapi/linux/tls.h index 401d6f01de6a..5b9c26753e46 100644 --- a/include/uapi/linux/tls.h +++ b/include/uapi/linux/tls.h @@ -70,6 +70,13 @@ #define TLS_CIPHER_AES_GCM_256_TAG_SIZE 16 #define TLS_CIPHER_AES_GCM_256_REC_SEQ_SIZE 8 =20 +#define TLS_CIPHER_AES_CCM_128 53 +#define TLS_CIPHER_AES_CCM_128_IV_SIZE 8 +#define TLS_CIPHER_AES_CCM_128_KEY_SIZE 16 +#define TLS_CIPHER_AES_CCM_128_SALT_SIZE 4 +#define TLS_CIPHER_AES_CCM_128_TAG_SIZE 16 +#define TLS_CIPHER_AES_CCM_128_REC_SEQ_SIZE 8 + #define TLS_SET_RECORD_TYPE 1 #define TLS_GET_RECORD_TYPE 2 =20 @@ -94,4 +101,12 @@ struct tls12_crypto_info_aes_gcm_256 { unsigned char rec_seq[TLS_CIPHER_AES_GCM_256_REC_SEQ_SIZE]; }; =20 +struct tls12_crypto_info_aes_ccm_128 { + struct tls_crypto_info info; + unsigned char iv[TLS_CIPHER_AES_CCM_128_IV_SIZE]; + unsigned char key[TLS_CIPHER_AES_CCM_128_KEY_SIZE]; + unsigned char salt[TLS_CIPHER_AES_CCM_128_SALT_SIZE]; + unsigned char rec_seq[TLS_CIPHER_AES_CCM_128_REC_SEQ_SIZE]; +}; + #endif /* _UAPI_LINUX_TLS_H */ diff --git a/net/tls/tls_main.c b/net/tls/tls_main.c index df921a2904b9..0e24edab2535 100644 --- a/net/tls/tls_main.c +++ b/net/tls/tls_main.c @@ -469,27 +469,32 @@ static int do_tls_setsockopt_conf(struct sock *sk, ch= ar __user *optval, =20 switch (crypto_info->cipher_type) { case TLS_CIPHER_AES_GCM_128: + optsize =3D sizeof(struct tls12_crypto_info_aes_gcm_128); + break; case TLS_CIPHER_AES_GCM_256: { - optsize =3D crypto_info->cipher_type =3D=3D TLS_CIPHER_AES_GCM_128 ? - sizeof(struct tls12_crypto_info_aes_gcm_128) : - sizeof(struct tls12_crypto_info_aes_gcm_256); - if (optlen !=3D optsize) { - rc =3D -EINVAL; - goto err_crypto_info; - } - rc =3D copy_from_user(crypto_info + 1, optval + sizeof(*crypto_info), - optlen - sizeof(*crypto_info)); - if (rc) { - rc =3D -EFAULT; - goto err_crypto_info; - } + optsize =3D sizeof(struct tls12_crypto_info_aes_gcm_256); break; } + case TLS_CIPHER_AES_CCM_128: + optsize =3D sizeof(struct tls12_crypto_info_aes_ccm_128); + break; default: rc =3D -EINVAL; goto err_crypto_info; } =20 + if (optlen !=3D optsize) { + rc =3D -EINVAL; + goto err_crypto_info; + } + + rc =3D copy_from_user(crypto_info + 1, optval + sizeof(*crypto_info), + optlen - sizeof(*crypto_info)); + if (rc) { + rc =3D -EFAULT; + goto err_crypto_info; + } + if (tx) { #ifdef CONFIG_TLS_DEVICE rc =3D tls_set_device_offload(sk, ctx); diff --git a/net/tls/tls_sw.c b/net/tls/tls_sw.c index 425351ac2a9b..f635c103581e 100644 --- a/net/tls/tls_sw.c +++ b/net/tls/tls_sw.c @@ -42,8 +42,6 @@ #include #include =20 -#define MAX_IV_SIZE TLS_CIPHER_AES_GCM_128_IV_SIZE - static int __skb_nsg(struct sk_buff *skb, int offset, int len, unsigned int recursion_level) { @@ -479,11 +477,18 @@ static int tls_do_encryption(struct sock *sk, struct tls_rec *rec =3D ctx->open_rec; struct sk_msg *msg_en =3D &rec->msg_encrypted; struct scatterlist *sge =3D sk_msg_elem(msg_en, start); - int rc; + int rc, iv_offset =3D 0; + + /* For CCM based ciphers, first byte of IV is a constant */ + if (prot->cipher_type =3D=3D TLS_CIPHER_AES_CCM_128) { + rec->iv_data[0] =3D TLS_AES_CCM_IV_B0_BYTE; + iv_offset =3D 1; + } + + memcpy(&rec->iv_data[iv_offset], tls_ctx->tx.iv, + prot->iv_size + prot->salt_size); =20 - memcpy(rec->iv_data, tls_ctx->tx.iv, sizeof(rec->iv_data)); - xor_iv_with_seq(prot->version, rec->iv_data, - tls_ctx->tx.rec_seq); + xor_iv_with_seq(prot->version, rec->iv_data, tls_ctx->tx.rec_seq); =20 sge->offset +=3D prot->prepend_size; sge->length -=3D prot->prepend_size; @@ -1344,6 +1349,7 @@ static int decrypt_internal(struct sock *sk, struct s= k_buff *skb, struct scatterlist *sgout =3D NULL; const int data_len =3D rxm->full_len - prot->overhead_size + prot->tail_size; + int iv_offset =3D 0; =20 if (*zc && (out_iov || out_sg)) { if (out_iov) @@ -1386,18 +1392,25 @@ static int decrypt_internal(struct sock *sk, struct= sk_buff *skb, aad =3D (u8 *)(sgout + n_sgout); iv =3D aad + prot->aad_size; =20 + /* For CCM based ciphers, first byte of nonce+iv is always '2' */ + if (prot->cipher_type =3D=3D TLS_CIPHER_AES_CCM_128) { + iv[0] =3D 2; + iv_offset =3D 1; + } + /* Prepare IV */ err =3D skb_copy_bits(skb, rxm->offset + TLS_HEADER_SIZE, - iv + TLS_CIPHER_AES_GCM_128_SALT_SIZE, + iv + iv_offset + prot->salt_size, prot->iv_size); if (err < 0) { kfree(mem); return err; } if (prot->version =3D=3D TLS_1_3_VERSION) - memcpy(iv, tls_ctx->rx.iv, crypto_aead_ivsize(ctx->aead_recv)); + memcpy(iv + iv_offset, tls_ctx->rx.iv, + crypto_aead_ivsize(ctx->aead_recv)); else - memcpy(iv, tls_ctx->rx.iv, TLS_CIPHER_AES_GCM_128_SALT_SIZE); + memcpy(iv + iv_offset, tls_ctx->rx.iv, prot->salt_size); =20 xor_iv_with_seq(prot->version, iv, tls_ctx->rx.rec_seq); =20 @@ -2152,14 +2165,15 @@ int tls_set_sw_offload(struct sock *sk, struct tls_= context *ctx, int tx) struct tls_crypto_info *crypto_info; struct tls12_crypto_info_aes_gcm_128 *gcm_128_info; struct tls12_crypto_info_aes_gcm_256 *gcm_256_info; + struct tls12_crypto_info_aes_ccm_128 *ccm_128_info; struct tls_sw_context_tx *sw_ctx_tx =3D NULL; struct tls_sw_context_rx *sw_ctx_rx =3D NULL; struct cipher_context *cctx; struct crypto_aead **aead; struct strp_callbacks cb; - u16 nonce_size, tag_size, iv_size, rec_seq_size; + u16 nonce_size, tag_size, iv_size, rec_seq_size, salt_size; struct crypto_tfm *tfm; - char *iv, *rec_seq, *key, *salt; + char *iv, *rec_seq, *key, *salt, *cipher_name; size_t keysize; int rc =3D 0; =20 @@ -2224,6 +2238,8 @@ int tls_set_sw_offload(struct sock *sk, struct tls_co= ntext *ctx, int tx) keysize =3D TLS_CIPHER_AES_GCM_128_KEY_SIZE; key =3D gcm_128_info->key; salt =3D gcm_128_info->salt; + salt_size =3D TLS_CIPHER_AES_GCM_128_SALT_SIZE; + cipher_name =3D "gcm(aes)"; break; } case TLS_CIPHER_AES_GCM_256: { @@ -2239,6 +2255,25 @@ int tls_set_sw_offload(struct sock *sk, struct tls_c= ontext *ctx, int tx) keysize =3D TLS_CIPHER_AES_GCM_256_KEY_SIZE; key =3D gcm_256_info->key; salt =3D gcm_256_info->salt; + salt_size =3D TLS_CIPHER_AES_GCM_256_SALT_SIZE; + cipher_name =3D "gcm(aes)"; + break; + } + case TLS_CIPHER_AES_CCM_128: { + nonce_size =3D TLS_CIPHER_AES_CCM_128_IV_SIZE; + tag_size =3D TLS_CIPHER_AES_CCM_128_TAG_SIZE; + iv_size =3D TLS_CIPHER_AES_CCM_128_IV_SIZE; + iv =3D ((struct tls12_crypto_info_aes_ccm_128 *)crypto_info)->iv; + rec_seq_size =3D TLS_CIPHER_AES_CCM_128_REC_SEQ_SIZE; + rec_seq =3D + ((struct tls12_crypto_info_aes_ccm_128 *)crypto_info)->rec_seq; + ccm_128_info =3D + (struct tls12_crypto_info_aes_ccm_128 *)crypto_info; + keysize =3D TLS_CIPHER_AES_CCM_128_KEY_SIZE; + key =3D ccm_128_info->key; + salt =3D ccm_128_info->salt; + salt_size =3D TLS_CIPHER_AES_CCM_128_SALT_SIZE; + cipher_name =3D "ccm(aes)"; break; } default: @@ -2268,16 +2303,16 @@ int tls_set_sw_offload(struct sock *sk, struct tls_= context *ctx, int tx) prot->overhead_size =3D prot->prepend_size + prot->tag_size + prot->tail_size; prot->iv_size =3D iv_size; - cctx->iv =3D kmalloc(iv_size + TLS_CIPHER_AES_GCM_128_SALT_SIZE, - GFP_KERNEL); + prot->salt_size =3D salt_size; + cctx->iv =3D kmalloc(iv_size + salt_size, GFP_KERNEL); if (!cctx->iv) { rc =3D -ENOMEM; goto free_priv; } /* Note: 128 & 256 bit salt are the same size */ - memcpy(cctx->iv, salt, TLS_CIPHER_AES_GCM_128_SALT_SIZE); - memcpy(cctx->iv + TLS_CIPHER_AES_GCM_128_SALT_SIZE, iv, iv_size); prot->rec_seq_size =3D rec_seq_size; + memcpy(cctx->iv, salt, salt_size); + memcpy(cctx->iv + salt_size, iv, iv_size); cctx->rec_seq =3D kmemdup(rec_seq, rec_seq_size, GFP_KERNEL); if (!cctx->rec_seq) { rc =3D -ENOMEM; @@ -2285,7 +2320,7 @@ int tls_set_sw_offload(struct sock *sk, struct tls_co= ntext *ctx, int tx) } =20 if (!*aead) { - *aead =3D crypto_alloc_aead("gcm(aes)", 0, 0); + *aead =3D crypto_alloc_aead(cipher_name, 0, 0); if (IS_ERR(*aead)) { rc =3D PTR_ERR(*aead); *aead =3D NULL; --=20 2.13.6