From patchwork Mon Nov 18 15:38:38 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Andrey Smirnov X-Patchwork-Id: 11249891 X-Patchwork-Delegate: herbert@gondor.apana.org.au Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 66D7414E5 for ; Mon, 18 Nov 2019 15:39:09 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 461C22195D for ; Mon, 18 Nov 2019 15:39:09 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="Dae4jeld" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727199AbfKRPjI (ORCPT ); Mon, 18 Nov 2019 10:39:08 -0500 Received: from mail-pf1-f193.google.com ([209.85.210.193]:43301 "EHLO mail-pf1-f193.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727178AbfKRPjI (ORCPT ); Mon, 18 Nov 2019 10:39:08 -0500 Received: by mail-pf1-f193.google.com with SMTP id 3so10552586pfb.10; Mon, 18 Nov 2019 07:39:07 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=5auEHawVnFdIZOUx2eKKQZZ0ecTFrj9LceutMYE+Ee4=; b=Dae4jelduY/7mwP6L9S5eC7kyvEr5yRcUEq3yVpaa0nLbbb8ucUOaKEfVIb7a6PXA8 yY+H4e5SJBvltckLJjiBwUqNsFjPURSX74GtvH3gN5Mf2RYCHE1DK4HnszcMpG275/yC 1J7TQBqT+ISmV9BFiXID6l7dlKot+lHWfzXTIW4fP8OzcDVrhNM4NdssWJfQTVcyfni3 CWXqWHXWsr+I6XtE4gxNuCsZezC3UBj9mF+WRQkI02tugsoqAzAkUj639Ij/IvjS4R1c 2aA7TeAV6nRin8hlXviXP9vOsL+2rg4kFOQDDWqlkfyjIoWTb1tZtKLSQKLtNlaGtmDm SpFA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=5auEHawVnFdIZOUx2eKKQZZ0ecTFrj9LceutMYE+Ee4=; b=aRY8b4NBW9Fa1aahCFSn+Vh+0xeTDUpezfulqR4rj/2nA2JV0XW9oXjM9rNUjxY/mr UvIAdqtn1wRp+3C42pze4Hi/HaN/c0DPQf4o5d1xTT+9MjS6h9oMDeQWSm5HLuYcjHr9 51c36tTVX9eZJZk0XkB2W2eTRuUSgOvSHTo6W+X+aKCKWxE75Nii/VFuZcO4hnwF6oup 0d2S44WGvdtb+FwqLsDpV2Rh/LyTnHg7bgIFwTqxnVr+RVi3dn+kwj2P6x1lUXq4jjh9 zkE+dqaord+S1oovvNSUA4imNokna/dS4aDJFCJV7JiJJjkW3bHHndYp35jtD2LVoxxT WJoA== X-Gm-Message-State: APjAAAVTdNxY0EdvVRviizQxGDoAabMiRbBA+BOiEJVXo29V+Q0e4xqn 7spH+QAQmbrh/hZh5WFSkYi3MuZs X-Google-Smtp-Source: APXvYqyLMc9147zNSg9VNfa4KVv1O59d8dqRFpucn5Gm7q35ZdFFkmPYnt132kFIYcfq1AVfiYKuFQ== X-Received: by 2002:a65:5c8b:: with SMTP id a11mr33789990pgt.60.1574091546704; Mon, 18 Nov 2019 07:39:06 -0800 (PST) Received: from localhost.hsd1.wa.comcast.net ([2601:602:847f:811f:babe:8e8d:b27e:e6d7]) by smtp.gmail.com with ESMTPSA id z7sm23573732pfr.165.2019.11.18.07.39.03 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 18 Nov 2019 07:39:05 -0800 (PST) From: Andrey Smirnov To: linux-crypto@vger.kernel.org Cc: Andrey Smirnov , Aymen Sghaier , Vipul Kumar , Chris Healy , Lucas Stach , =?utf-8?q?Horia_Geant=C4=83?= , Herbert Xu , Iuliana Prodan , linux-kernel@vger.kernel.org, linux-imx@nxp.com Subject: [PATCH v2 1/6] crypto: caam - RNG4 TRNG errata Date: Mon, 18 Nov 2019 07:38:38 -0800 Message-Id: <20191118153843.28136-2-andrew.smirnov@gmail.com> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20191118153843.28136-1-andrew.smirnov@gmail.com> References: <20191118153843.28136-1-andrew.smirnov@gmail.com> MIME-Version: 1.0 Sender: linux-crypto-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org The TRNG as used in RNG4, used in CAAM has a documentation issue. The effect is that it is possible that the entropy used to instantiate the DRBG may be old entropy, rather than newly generated entropy. There is proper programming guidance, but it is not in the documentation. Signed-off-by: Aymen Sghaier Signed-off-by: Vipul Kumar [andrew.smirnov@gmail.com ported to upstream kernel] Signed-off-by: Andrey Smirnov Cc: Chris Healy Cc: Lucas Stach Cc: Horia Geantă Cc: Herbert Xu Cc: Iuliana Prodan Cc: linux-crypto@vger.kernel.org Cc: linux-kernel@vger.kernel.org Cc: linux-imx@nxp.com --- drivers/crypto/caam/ctrl.c | 11 ++++++++--- drivers/crypto/caam/regs.h | 3 ++- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/drivers/crypto/caam/ctrl.c b/drivers/crypto/caam/ctrl.c index d7c3c3805693..df4db10e9fca 100644 --- a/drivers/crypto/caam/ctrl.c +++ b/drivers/crypto/caam/ctrl.c @@ -338,8 +338,12 @@ static void kick_trng(struct platform_device *pdev, int ent_delay) ctrl = (struct caam_ctrl __iomem *)ctrlpriv->ctrl; r4tst = &ctrl->r4tst[0]; - /* put RNG4 into program mode */ - clrsetbits_32(&r4tst->rtmctl, 0, RTMCTL_PRGM); + /* + * Setting both RTMCTL:PRGM and RTMCTL:TRNG_ACC causes TRNG to + * properly invalidate the entropy in the entropy register and + * force re-generation. + */ + clrsetbits_32(&r4tst->rtmctl, 0, RTMCTL_PRGM | RTMCTL_ACC); /* * Performance-wise, it does not make sense to @@ -369,7 +373,8 @@ static void kick_trng(struct platform_device *pdev, int ent_delay) * select raw sampling in both entropy shifter * and statistical checker; ; put RNG4 into run mode */ - clrsetbits_32(&r4tst->rtmctl, RTMCTL_PRGM, RTMCTL_SAMP_MODE_RAW_ES_SC); + clrsetbits_32(&r4tst->rtmctl, RTMCTL_PRGM | RTMCTL_ACC, + RTMCTL_SAMP_MODE_RAW_ES_SC); } static int caam_get_era_from_hw(struct caam_ctrl __iomem *ctrl) diff --git a/drivers/crypto/caam/regs.h b/drivers/crypto/caam/regs.h index 05127b70527d..c191e8fd0fa7 100644 --- a/drivers/crypto/caam/regs.h +++ b/drivers/crypto/caam/regs.h @@ -487,7 +487,8 @@ struct rngtst { /* RNG4 TRNG test registers */ struct rng4tst { -#define RTMCTL_PRGM 0x00010000 /* 1 -> program mode, 0 -> run mode */ +#define RTMCTL_ACC BIT(5) /* TRNG access mode */ +#define RTMCTL_PRGM BIT(16) /* 1 -> program mode, 0 -> run mode */ #define RTMCTL_SAMP_MODE_VON_NEUMANN_ES_SC 0 /* use von Neumann data in both entropy shifter and statistical checker */ From patchwork Mon Nov 18 15:38:39 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Andrey Smirnov X-Patchwork-Id: 11249893 X-Patchwork-Delegate: herbert@gondor.apana.org.au Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 9FBBC14E5 for ; Mon, 18 Nov 2019 15:39:13 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 7EEA0219F9 for ; Mon, 18 Nov 2019 15:39:13 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="YX/opi9X" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727315AbfKRPjM (ORCPT ); Mon, 18 Nov 2019 10:39:12 -0500 Received: from mail-pf1-f196.google.com ([209.85.210.196]:44687 "EHLO mail-pf1-f196.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727178AbfKRPjM (ORCPT ); Mon, 18 Nov 2019 10:39:12 -0500 Received: by mail-pf1-f196.google.com with SMTP id q26so10541644pfn.11; Mon, 18 Nov 2019 07:39:10 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=1DtsNhw9v1gDBQhovceUYU94x/2uJ8teNAnS6FI6Te8=; b=YX/opi9XbQozDOHj8iix34o7GnRefRa8T4hAmQXBDBnSCfrTzVw5FyVg4cMNrksXv7 gQ05gy3X5hjvb91Fut8SZ0tWJdC5onxl/plTkTuPCgHeHfv7/BYaFJVbuKMTWiaf/luv C5w0fAAtxIHGppNpYcmISXiC8oFO3AfgbDJYUk8iXmwNzJX2wdhgF6PoXaXEm1vNyjQD Qe1KuxigDF4g7PoCxVis71KcPRm8lerkZeQUuPMqkYvX1CM1o+c+6mzpMXMoN6zSwrzL /RsZdWGoKNeGVg1hjk8nucrpwViRH+V9neXKzU+5Tvn5Sa56NJzqFYrVWozN4N941U11 suKA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=1DtsNhw9v1gDBQhovceUYU94x/2uJ8teNAnS6FI6Te8=; b=b0qtSbNAHebNRSQ4bjqYRBHaGnxYLgfSMc0m8P8NQUnUPqeW2rWYMBFAhgYrI3g83H XND53ElwQ6NTKfY7/4VxKqcWW3mN+SBJcCtCx8Mm/0gBSvmRDRxstzO/BuWb4+HxTqCM Or0ARNR+9vE3Lz9xS+eOvJGXhkNUe7JRSUvtotxtqkjtR6IviMEFAAPOtI0/Cm9UEY3U GGcSk2oAMiImlx607S+SNxpv4RlUWKI/befUF4vaCG7k+1IKC6isK0mqUgs2wnrmKP5H 2pVzd7sdhPm0eoS9nFcRo1zCXgznTgX4uD6RHoOorEz+vghgqiDJ9iPTpA6Qc350xA5Z X7tA== X-Gm-Message-State: APjAAAWXsUlIiJh71Xs+/w/dhXgu1mPSnLkTvhDTY3oi6/5aVs4E+5du AW6EaOtjFRsNxPjGCh9gc6vHlxiH X-Google-Smtp-Source: APXvYqyfrvFA61p36vU/Qkin+JW/xz50rR5phwkWA4SxquJZhKZjESxXD9lwpTilwRUxLCMwsYYneQ== X-Received: by 2002:a63:a34d:: with SMTP id v13mr6588285pgn.343.1574091549118; Mon, 18 Nov 2019 07:39:09 -0800 (PST) Received: from localhost.hsd1.wa.comcast.net ([2601:602:847f:811f:babe:8e8d:b27e:e6d7]) by smtp.gmail.com with ESMTPSA id z7sm23573732pfr.165.2019.11.18.07.39.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 18 Nov 2019 07:39:08 -0800 (PST) From: Andrey Smirnov To: linux-crypto@vger.kernel.org Cc: Andrey Smirnov , Chris Healy , Lucas Stach , =?utf-8?q?Horia_Geant=C4=83?= , Herbert Xu , Iuliana Prodan , linux-kernel@vger.kernel.org, linux-imx@nxp.com Subject: [PATCH v2 2/6] crypto: caam - enable prediction resistance in HRWNG Date: Mon, 18 Nov 2019 07:38:39 -0800 Message-Id: <20191118153843.28136-3-andrew.smirnov@gmail.com> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20191118153843.28136-1-andrew.smirnov@gmail.com> References: <20191118153843.28136-1-andrew.smirnov@gmail.com> MIME-Version: 1.0 Sender: linux-crypto-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org Instantiate CAAM RNG with prediction resistance enabled to improve its quality. Signed-off-by: Andrey Smirnov Cc: Chris Healy Cc: Lucas Stach Cc: Horia Geantă Cc: Herbert Xu Cc: Iuliana Prodan Cc: linux-crypto@vger.kernel.org Cc: linux-kernel@vger.kernel.org Cc: linux-imx@nxp.com --- drivers/crypto/caam/caamrng.c | 3 ++- drivers/crypto/caam/ctrl.c | 8 +++++--- drivers/crypto/caam/desc.h | 2 ++ drivers/crypto/caam/regs.h | 4 +++- 4 files changed, 12 insertions(+), 5 deletions(-) diff --git a/drivers/crypto/caam/caamrng.c b/drivers/crypto/caam/caamrng.c index e8baacaabe07..6dde8ae3cd9b 100644 --- a/drivers/crypto/caam/caamrng.c +++ b/drivers/crypto/caam/caamrng.c @@ -202,7 +202,8 @@ static inline int rng_create_sh_desc(struct caam_rng_ctx *ctx) init_sh_desc(desc, HDR_SHARE_SERIAL); /* Generate random bytes */ - append_operation(desc, OP_ALG_ALGSEL_RNG | OP_TYPE_CLASS1_ALG); + append_operation(desc, OP_ALG_ALGSEL_RNG | OP_TYPE_CLASS1_ALG | + OP_ALG_PR_ON); /* Store bytes */ append_seq_fifo_store(desc, RN_BUF_SIZE, FIFOST_TYPE_RNGSTORE); diff --git a/drivers/crypto/caam/ctrl.c b/drivers/crypto/caam/ctrl.c index df4db10e9fca..a1c879820286 100644 --- a/drivers/crypto/caam/ctrl.c +++ b/drivers/crypto/caam/ctrl.c @@ -36,7 +36,8 @@ static void build_instantiation_desc(u32 *desc, int handle, int do_sk) init_job_desc(desc, 0); op_flags = OP_TYPE_CLASS1_ALG | OP_ALG_ALGSEL_RNG | - (handle << OP_ALG_AAI_SHIFT) | OP_ALG_AS_INIT; + (handle << OP_ALG_AAI_SHIFT) | OP_ALG_AS_INIT | + OP_ALG_PR_ON; /* INIT RNG in non-test mode */ append_operation(desc, op_flags); @@ -275,11 +276,12 @@ static int instantiate_rng(struct device *ctrldev, int state_handle_mask, return -ENOMEM; for (sh_idx = 0; sh_idx < RNG4_MAX_HANDLES; sh_idx++) { + const u32 rdsta_mask = (RDSTA_PR0 | RDSTA_IF0) << sh_idx; /* * If the corresponding bit is set, this state handle * was initialized by somebody else, so it's left alone. */ - if ((1 << sh_idx) & state_handle_mask) + if (rdsta_mask & state_handle_mask) continue; /* Create the descriptor for instantiating RNG State Handle */ @@ -302,7 +304,7 @@ static int instantiate_rng(struct device *ctrldev, int state_handle_mask, rdsta_val = rd_reg32(&ctrl->r4tst[0].rdsta) & RDSTA_IFMASK; if ((status && status != JRSTA_SSRC_JUMP_HALT_CC) || - !(rdsta_val & (1 << sh_idx))) { + (rdsta_val & rdsta_mask) != rdsta_mask) { ret = -EAGAIN; break; } diff --git a/drivers/crypto/caam/desc.h b/drivers/crypto/caam/desc.h index 4b6854bf896a..e796d3cb9be8 100644 --- a/drivers/crypto/caam/desc.h +++ b/drivers/crypto/caam/desc.h @@ -1254,6 +1254,8 @@ #define OP_ALG_ICV_OFF (0 << OP_ALG_ICV_SHIFT) #define OP_ALG_ICV_ON (1 << OP_ALG_ICV_SHIFT) +#define OP_ALG_PR_ON BIT(1) + #define OP_ALG_DIR_SHIFT 0 #define OP_ALG_DIR_MASK 1 #define OP_ALG_DECRYPT 0 diff --git a/drivers/crypto/caam/regs.h b/drivers/crypto/caam/regs.h index c191e8fd0fa7..fe1f8c1409fd 100644 --- a/drivers/crypto/caam/regs.h +++ b/drivers/crypto/caam/regs.h @@ -524,9 +524,11 @@ struct rng4tst { u32 rsvd1[40]; #define RDSTA_SKVT 0x80000000 #define RDSTA_SKVN 0x40000000 +#define RDSTA_PR0 BIT(4) +#define RDSTA_PR1 BIT(5) #define RDSTA_IF0 0x00000001 #define RDSTA_IF1 0x00000002 -#define RDSTA_IFMASK (RDSTA_IF1 | RDSTA_IF0) +#define RDSTA_IFMASK (RDSTA_PR1 | RDSTA_PR0 | RDSTA_IF1 | RDSTA_IF0) u32 rdsta; u32 rsvd2[15]; }; From patchwork Mon Nov 18 15:38:40 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Andrey Smirnov X-Patchwork-Id: 11249895 X-Patchwork-Delegate: herbert@gondor.apana.org.au Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id DD7C5109A for ; Mon, 18 Nov 2019 15:39:16 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id BD23321A45 for ; Mon, 18 Nov 2019 15:39:16 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="dAU5c9Yv" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727352AbfKRPjP (ORCPT ); Mon, 18 Nov 2019 10:39:15 -0500 Received: from mail-pl1-f194.google.com ([209.85.214.194]:39390 "EHLO mail-pl1-f194.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727319AbfKRPjN (ORCPT ); Mon, 18 Nov 2019 10:39:13 -0500 Received: by mail-pl1-f194.google.com with SMTP id o9so9995640plk.6; Mon, 18 Nov 2019 07:39:11 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=ZVobmFgrwdoULTp3hehMSizYkD3NBn4CVCiP3rG4R64=; b=dAU5c9YvrvAc5FwNHT1gO7uVf5sn76QBMhamX3mNn89SPn4RdD1CsA1iWljKHhIPxr 4Py+yW74C74stwVu+8ZeWVGV2EatiP83gmZFXeUJP1OHcp5og5ZosH+QHArynj75CRGl MqT/S8DGhyQq8qtetRmdOKCJhcx9TAJ7Q8iqRqwFi3U/RX8jejXyhBbOJeT3M643GVGZ tI47Ec83XSBGvUh/C5pb9FyPWA0AwraU3hqFbNu9W9VwCMyfivDZSi1hCCWaeOOk1Lz1 LkKD6nZGdyAT6RbYPdoDLRTROTUF6l/4gc0mZZAE9895EPNiuryNOLislreWrlb4CkFi 7meA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=ZVobmFgrwdoULTp3hehMSizYkD3NBn4CVCiP3rG4R64=; b=tPXyaG9GUu8VbCBbY1MaSFVZswPai9iKSc1z6zvap6tYG+3Vwf9L3h2woqNUM6eALJ p9luHBKbiynd3RF8QtFc5zFidzdlHaTZfSQrMYRAkneg3byOplkF94QN7euhUFqkESew 8wgXm4ie/P0xUkoRVMRBZxx3wPOlYpMP+uLZKhfmHgbckVcpA2i+Cy/nwGQGHY013P/V ym7wRO7c7uc6zssqr3NzBzx61JYP5nuquvGOWe/mAvVDq/+39ZuVJry2c0k47tuo4iPI mR9uvrc17wj6+PTVR9i/Ek/QzfJinWBLSI3WUc1zlV2KLUlxdcbAqnMO3TUDPIreG3/c d4cQ== X-Gm-Message-State: APjAAAW6n+0I0ciddkFenTbMoc14GWMibXQVckJcLzlgZgOW00Q+3d0n 1yPvR5d4JXaL20og5lvGlVc6xqU4 X-Google-Smtp-Source: APXvYqx/mcdg6RhldBSfa/qG2V1ZbdvXkp4Y2z6VJpMtTjT36r207423+O74pXkVyaUWGRWe8epSkg== X-Received: by 2002:a17:902:b482:: with SMTP id y2mr30780936plr.128.1574091550693; Mon, 18 Nov 2019 07:39:10 -0800 (PST) Received: from localhost.hsd1.wa.comcast.net ([2601:602:847f:811f:babe:8e8d:b27e:e6d7]) by smtp.gmail.com with ESMTPSA id z7sm23573732pfr.165.2019.11.18.07.39.09 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 18 Nov 2019 07:39:09 -0800 (PST) From: Andrey Smirnov To: linux-crypto@vger.kernel.org Cc: Andrey Smirnov , Chris Healy , Lucas Stach , =?utf-8?q?Horia_Geant=C4=83?= , Herbert Xu , Iuliana Prodan , linux-imx@nxp.com, linux-kernel@vger.kernel.org Subject: [PATCH v2 3/6] crypto: caam - allocate RNG instantiation descriptor with GFP_DMA Date: Mon, 18 Nov 2019 07:38:40 -0800 Message-Id: <20191118153843.28136-4-andrew.smirnov@gmail.com> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20191118153843.28136-1-andrew.smirnov@gmail.com> References: <20191118153843.28136-1-andrew.smirnov@gmail.com> MIME-Version: 1.0 Sender: linux-crypto-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org Be consistent with the rest of the codebase and use GFP_DMA when allocating memory for a CAAM JR descriptor. Signed-off-by: Andrey Smirnov Cc: Chris Healy Cc: Lucas Stach Cc: Horia Geantă Cc: Herbert Xu Cc: Iuliana Prodan Cc: linux-imx@nxp.com Cc: linux-crypto@vger.kernel.org Cc: linux-kernel@vger.kernel.org --- drivers/crypto/caam/ctrl.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/drivers/crypto/caam/ctrl.c b/drivers/crypto/caam/ctrl.c index a1c879820286..8054ec29d35a 100644 --- a/drivers/crypto/caam/ctrl.c +++ b/drivers/crypto/caam/ctrl.c @@ -194,7 +194,7 @@ static int deinstantiate_rng(struct device *ctrldev, int state_handle_mask) u32 *desc, status; int sh_idx, ret = 0; - desc = kmalloc(CAAM_CMD_SZ * 3, GFP_KERNEL); + desc = kmalloc(CAAM_CMD_SZ * 3, GFP_KERNEL | GFP_DMA); if (!desc) return -ENOMEM; @@ -271,7 +271,7 @@ static int instantiate_rng(struct device *ctrldev, int state_handle_mask, int ret = 0, sh_idx; ctrl = (struct caam_ctrl __iomem *)ctrlpriv->ctrl; - desc = kmalloc(CAAM_CMD_SZ * 7, GFP_KERNEL); + desc = kmalloc(CAAM_CMD_SZ * 7, GFP_KERNEL | GFP_DMA); if (!desc) return -ENOMEM; From patchwork Mon Nov 18 15:38:41 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Andrey Smirnov X-Patchwork-Id: 11249901 X-Patchwork-Delegate: herbert@gondor.apana.org.au Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 78ECB1593 for ; Mon, 18 Nov 2019 15:39:31 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 57C3121A44 for ; Mon, 18 Nov 2019 15:39:31 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="ktx0tP9p" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727345AbfKRPjP (ORCPT ); Mon, 18 Nov 2019 10:39:15 -0500 Received: from mail-pj1-f66.google.com ([209.85.216.66]:35385 "EHLO mail-pj1-f66.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727178AbfKRPjO (ORCPT ); Mon, 18 Nov 2019 10:39:14 -0500 Received: by mail-pj1-f66.google.com with SMTP id s8so1596909pji.2; Mon, 18 Nov 2019 07:39:13 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=/0qBoFw4TTKJCwj76xXQwMD3BLGcsNlmJQFR+DDRRNA=; b=ktx0tP9pH60eN9aPZBjnRm2UpDemGEl/URXEpga/lFst4i0KLSS/7TPUFoDhxpa26M tvl9REv38le+PESAVwxNvfXGJaqtQeiStSzA7v1XTFUgRRtnrwERkTYwNJGbmiyUGQSe zeIWod/7Wkv4XNoaFePshJ2Cpn5UE/1P3HfXTLono1SYAe8qpsFI0XrOvxPyRFTA9ax+ plmalPas1Q7pgHQW4I4netabjdvyqICftmTeJWoz6zMUqc1mP/qkGTT98mWt0WsNfwbV tM8N4g1WyTwQWWQmZwtMrCHn3p9GHTppbejxOcJX/GWCJmI/dECoQKbGHKfls53qZ3CB zGFA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=/0qBoFw4TTKJCwj76xXQwMD3BLGcsNlmJQFR+DDRRNA=; b=cu2q+zl08C9iQRc7bS/oCBdMb9UCIOTR+aqocMwbHYQUVQ5soiBCYkQGQLRY5p0r19 YCq9rkiWVMyd1LKw4ynSDi2hc29rtoPrK2PEKheKqbQcUswnLFgGPBQ1ARgdPgkgXlmI jt0tUlGncohxeO5j4lctEly9qyO1hJ3tI0cWOXcVSr+myV2FnLoytDWgxcwJjEMI6WJ6 mwWuHmvBSR+E4YSvGCn1nU3a+iJLIF2GxVMYhPSRiBhg2th3x268KqmR3mMqjDbQRhUt A8NJnzxpMzwotYMeF5mr8j+OGQGwAH1h7GRMc07WomcBgh6NRYKiU00YoHyFwEx96Kg8 iM1g== X-Gm-Message-State: APjAAAVAhV8ar9iu0vGEpWPqt5WHcRYm1e0fWmI9pq84oYyFwFsiBsAe mj+h9CdQordabSfmShUOKAYUJBOa X-Google-Smtp-Source: APXvYqy0vNBUrXJkTkeo2skZSf+t1BJbD6tKO8M7yWidP8uPVQ8aZk4Ndum+nXrxEmATuvgBH7Bp5A== X-Received: by 2002:a17:902:8506:: with SMTP id bj6mr28179377plb.342.1574091552248; Mon, 18 Nov 2019 07:39:12 -0800 (PST) Received: from localhost.hsd1.wa.comcast.net ([2601:602:847f:811f:babe:8e8d:b27e:e6d7]) by smtp.gmail.com with ESMTPSA id z7sm23573732pfr.165.2019.11.18.07.39.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 18 Nov 2019 07:39:11 -0800 (PST) From: Andrey Smirnov To: linux-crypto@vger.kernel.org Cc: Andrey Smirnov , Chris Healy , Lucas Stach , =?utf-8?q?Horia_Geant=C4=83?= , Herbert Xu , Iuliana Prodan , linux-imx@nxp.com, linux-kernel@vger.kernel.org Subject: [PATCH v2 4/6] crypto: caam - move RNG presense check into a shared function Date: Mon, 18 Nov 2019 07:38:41 -0800 Message-Id: <20191118153843.28136-5-andrew.smirnov@gmail.com> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20191118153843.28136-1-andrew.smirnov@gmail.com> References: <20191118153843.28136-1-andrew.smirnov@gmail.com> MIME-Version: 1.0 Sender: linux-crypto-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org Move the code to check if RNG block is instantitated into a shared function. This will be used by commits that follow. Signed-off-by: Andrey Smirnov Cc: Chris Healy Cc: Lucas Stach Cc: Horia Geantă Cc: Herbert Xu Cc: Iuliana Prodan Cc: linux-imx@nxp.com Cc: linux-crypto@vger.kernel.org Cc: linux-kernel@vger.kernel.org --- drivers/crypto/caam/caamrng.c | 10 +--------- drivers/crypto/caam/intern.h | 14 ++++++++++++++ 2 files changed, 15 insertions(+), 9 deletions(-) diff --git a/drivers/crypto/caam/caamrng.c b/drivers/crypto/caam/caamrng.c index 6dde8ae3cd9b..70ddfbf90ac7 100644 --- a/drivers/crypto/caam/caamrng.c +++ b/drivers/crypto/caam/caamrng.c @@ -314,19 +314,11 @@ void caam_rng_exit(void) int caam_rng_init(struct device *ctrldev) { struct device *dev; - u32 rng_inst; struct caam_drv_private *priv = dev_get_drvdata(ctrldev); int err; init_done = false; - /* Check for an instantiated RNG before registration */ - if (priv->era < 10) - rng_inst = (rd_reg32(&priv->ctrl->perfmon.cha_num_ls) & - CHA_ID_LS_RNG_MASK) >> CHA_ID_LS_RNG_SHIFT; - else - rng_inst = rd_reg32(&priv->ctrl->vreg.rng) & CHA_VER_NUM_MASK; - - if (!rng_inst) + if (!caam_has_rng(priv)) return 0; dev = caam_jr_alloc(); diff --git a/drivers/crypto/caam/intern.h b/drivers/crypto/caam/intern.h index c7c10c90464b..f815e1ad4608 100644 --- a/drivers/crypto/caam/intern.h +++ b/drivers/crypto/caam/intern.h @@ -104,6 +104,20 @@ struct caam_drv_private { #endif }; +static inline bool caam_has_rng(struct caam_drv_private *priv) +{ + u32 rng_inst; + + /* Check for an instantiated RNG before registration */ + if (priv->era < 10) + rng_inst = (rd_reg32(&priv->ctrl->perfmon.cha_num_ls) & + CHA_ID_LS_RNG_MASK) >> CHA_ID_LS_RNG_SHIFT; + else + rng_inst = rd_reg32(&priv->ctrl->vreg.rng) & CHA_VER_NUM_MASK; + + return rng_inst; +} + #ifdef CONFIG_CRYPTO_DEV_FSL_CAAM_CRYPTO_API int caam_algapi_init(struct device *dev); From patchwork Mon Nov 18 15:38:42 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Andrey Smirnov X-Patchwork-Id: 11249899 X-Patchwork-Delegate: herbert@gondor.apana.org.au Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 2433B14E5 for ; Mon, 18 Nov 2019 15:39:31 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id D8D1821D7F for ; Mon, 18 Nov 2019 15:39:30 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="CumxP9PY" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727363AbfKRPj1 (ORCPT ); Mon, 18 Nov 2019 10:39:27 -0500 Received: from mail-pj1-f68.google.com ([209.85.216.68]:41589 "EHLO mail-pj1-f68.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727343AbfKRPjP (ORCPT ); Mon, 18 Nov 2019 10:39:15 -0500 Received: by mail-pj1-f68.google.com with SMTP id gc1so1596977pjb.8; Mon, 18 Nov 2019 07:39:15 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=UBIPSAzXSUe4u1QmBoUH87nVqYOKyW0QwHWrJku+GeU=; b=CumxP9PYWYNuWZoHLLcw7/bxTqaZTE8Gh6rIA3jnrbC7St77SqbNT30QfMZIJtbIog D0VXBxuHGmPg1n+SxwJYzD3UDZoHGWcD4Ve1ObJ/t6X2okk5vLBBBf6LLwxUahiWnSZR gIzQ3VPsx0Wzp2Dep4fozMC019vrYBmZoBizGbWC+IuattYSbofSOiyDcoBzZdeEvEZ8 2qBBMAYbvyfanEtthuSaWhDZlKkK177J9SJnzDs8LEoO1rIm6GCs+qibGG5mumIkhZ8j Re1nxPjckhN14M7eRy6ywkli7BnPApLgH1KijjgLee45ec9n4evur2/Wph7+CpvoRJzn s72Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=UBIPSAzXSUe4u1QmBoUH87nVqYOKyW0QwHWrJku+GeU=; b=NadCuvtCH7lkB6aZf6R4NvraLNgGA/HhCMgbUYdhCPx2ozgm4Zd0BkMphSYnHthUSw 3mzNgms13I48WqaP/4cgDdORjppwheUKDm+MOqOHLnRwjhSyQxI/scFhTfxEnAmTOBGt nvfEEg7KYJIp+MyqGaYCX2mEHnf1QlFz7mPDyOj7W8goBwTzN0GUVtC+JulMwSt+rvrj iKFQDNaZFnTGh4lwNa5y9dFFfWPlB4k8VL9JHvni1ycROHHjaJK0csvZP1OurkH5VJ9b /thXa54Y0PS2JmPWrTSlQOpVTJzVV2Q6dQn91u/auJCI21bSexRQOIfH/9n5I1uxH80c qSJg== X-Gm-Message-State: APjAAAXaDJcP5IWg/zXhjD+g18epSLCHP04AVWzlP69KMGKK88jDjziC PJzyIVm1P53OSCJp1hjdilLiNgfq X-Google-Smtp-Source: APXvYqwqwU886ot986aE7UCg3V5RKR/BDbQShWuD6kIEUH/oOBmti72+4wga2qWbqs0oXNyABRRmhg== X-Received: by 2002:a17:90a:f491:: with SMTP id bx17mr33829pjb.124.1574091554108; Mon, 18 Nov 2019 07:39:14 -0800 (PST) Received: from localhost.hsd1.wa.comcast.net ([2601:602:847f:811f:babe:8e8d:b27e:e6d7]) by smtp.gmail.com with ESMTPSA id z7sm23573732pfr.165.2019.11.18.07.39.12 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 18 Nov 2019 07:39:13 -0800 (PST) From: Andrey Smirnov To: linux-crypto@vger.kernel.org Cc: Andrey Smirnov , Chris Healy , Lucas Stach , =?utf-8?q?Horia_Geant=C4=83?= , Herbert Xu , Iuliana Prodan , linux-imx@nxp.com, linux-kernel@vger.kernel.org Subject: [PATCH v2 5/6] crypto: caam - replace DRNG with TRNG for use with hw_random Date: Mon, 18 Nov 2019 07:38:42 -0800 Message-Id: <20191118153843.28136-6-andrew.smirnov@gmail.com> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20191118153843.28136-1-andrew.smirnov@gmail.com> References: <20191118153843.28136-1-andrew.smirnov@gmail.com> MIME-Version: 1.0 Sender: linux-crypto-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org In order to give CAAM-generated random data highest quarlity raiting (999), replace current code that uses DRNG with code that fetches data straight out of TRNG used to seed aforementioned DRNG. Signed-off-by: Andrey Smirnov Cc: Chris Healy Cc: Lucas Stach Cc: Horia Geantă Cc: Herbert Xu Cc: Iuliana Prodan Cc: linux-imx@nxp.com Cc: linux-crypto@vger.kernel.org Cc: linux-kernel@vger.kernel.org --- drivers/crypto/caam/Kconfig | 17 +- drivers/crypto/caam/Makefile | 2 +- drivers/crypto/caam/caamrng.c | 351 ---------------------------------- drivers/crypto/caam/ctrl.c | 6 + drivers/crypto/caam/intern.h | 9 +- drivers/crypto/caam/jr.c | 2 - drivers/crypto/caam/regs.h | 7 +- drivers/crypto/caam/trng.c | 85 ++++++++ 8 files changed, 107 insertions(+), 372 deletions(-) delete mode 100644 drivers/crypto/caam/caamrng.c create mode 100644 drivers/crypto/caam/trng.c diff --git a/drivers/crypto/caam/Kconfig b/drivers/crypto/caam/Kconfig index 137ed3df0c74..22116a8e2ff3 100644 --- a/drivers/crypto/caam/Kconfig +++ b/drivers/crypto/caam/Kconfig @@ -31,6 +31,14 @@ config CRYPTO_DEV_FSL_CAAM_DEBUG Selecting this will enable printing of various debug information in the CAAM driver. +config CRYPTO_DEV_FSL_CAAM_RNG_API + bool "Register caam device for hwrng API" + default y + select HW_RANDOM + help + Selecting this will register the hardware TRNG to + the hw_random API for suppying the kernel entropy pool. + menuconfig CRYPTO_DEV_FSL_CAAM_JR tristate "Freescale CAAM Job Ring driver backend" default y @@ -138,15 +146,6 @@ config CRYPTO_DEV_FSL_CAAM_PKC_API Supported cryptographic primitives: encryption, decryption, signature and verification. -config CRYPTO_DEV_FSL_CAAM_RNG_API - bool "Register caam device for hwrng API" - default y - select CRYPTO_RNG - select HW_RANDOM - help - Selecting this will register the SEC4 hardware rng to - the hw_random API for suppying the kernel entropy pool. - endif # CRYPTO_DEV_FSL_CAAM_JR endif # CRYPTO_DEV_FSL_CAAM diff --git a/drivers/crypto/caam/Makefile b/drivers/crypto/caam/Makefile index 68d5cc0f28e2..04884fc087f9 100644 --- a/drivers/crypto/caam/Makefile +++ b/drivers/crypto/caam/Makefile @@ -15,11 +15,11 @@ obj-$(CONFIG_CRYPTO_DEV_FSL_CAAM_CRYPTO_API_DESC) += caamalg_desc.o obj-$(CONFIG_CRYPTO_DEV_FSL_CAAM_AHASH_API_DESC) += caamhash_desc.o caam-y := ctrl.o +caam-$(CONFIG_CRYPTO_DEV_FSL_CAAM_RNG_API) += trng.o caam_jr-y := jr.o key_gen.o caam_jr-$(CONFIG_CRYPTO_DEV_FSL_CAAM_CRYPTO_API) += caamalg.o caam_jr-$(CONFIG_CRYPTO_DEV_FSL_CAAM_CRYPTO_API_QI) += caamalg_qi.o caam_jr-$(CONFIG_CRYPTO_DEV_FSL_CAAM_AHASH_API) += caamhash.o -caam_jr-$(CONFIG_CRYPTO_DEV_FSL_CAAM_RNG_API) += caamrng.o caam_jr-$(CONFIG_CRYPTO_DEV_FSL_CAAM_PKC_API) += caampkc.o pkc_desc.o caam-$(CONFIG_CRYPTO_DEV_FSL_CAAM_CRYPTO_API_QI) += qi.o diff --git a/drivers/crypto/caam/caamrng.c b/drivers/crypto/caam/caamrng.c deleted file mode 100644 index 70ddfbf90ac7..000000000000 --- a/drivers/crypto/caam/caamrng.c +++ /dev/null @@ -1,351 +0,0 @@ -// SPDX-License-Identifier: GPL-2.0+ -/* - * caam - Freescale FSL CAAM support for hw_random - * - * Copyright 2011 Freescale Semiconductor, Inc. - * Copyright 2018-2019 NXP - * - * Based on caamalg.c crypto API driver. - * - * relationship between job descriptors to shared descriptors: - * - * --------------- -------------- - * | JobDesc #0 |-------------------->| ShareDesc | - * | *(buffer 0) | |------------->| (generate) | - * --------------- | | (move) | - * | | (store) | - * --------------- | -------------- - * | JobDesc #1 |------| - * | *(buffer 1) | - * --------------- - * - * A job desc looks like this: - * - * --------------------- - * | Header | - * | ShareDesc Pointer | - * | SEQ_OUT_PTR | - * | (output buffer) | - * --------------------- - * - * The SharedDesc never changes, and each job descriptor points to one of two - * buffers for each device, from which the data will be copied into the - * requested destination - */ - -#include -#include -#include - -#include "compat.h" - -#include "regs.h" -#include "intern.h" -#include "desc_constr.h" -#include "jr.h" -#include "error.h" - -/* - * Maximum buffer size: maximum number of random, cache-aligned bytes that - * will be generated and moved to seq out ptr (extlen not allowed) - */ -#define RN_BUF_SIZE (0xffff / L1_CACHE_BYTES * \ - L1_CACHE_BYTES) - -/* length of descriptors */ -#define DESC_JOB_O_LEN (CAAM_CMD_SZ * 2 + CAAM_PTR_SZ_MAX * 2) -#define DESC_RNG_LEN (3 * CAAM_CMD_SZ) - -/* Buffer, its dma address and lock */ -struct buf_data { - u8 buf[RN_BUF_SIZE] ____cacheline_aligned; - dma_addr_t addr; - struct completion filled; - u32 hw_desc[DESC_JOB_O_LEN]; -#define BUF_NOT_EMPTY 0 -#define BUF_EMPTY 1 -#define BUF_PENDING 2 /* Empty, but with job pending --don't submit another */ - atomic_t empty; -}; - -/* rng per-device context */ -struct caam_rng_ctx { - struct device *jrdev; - dma_addr_t sh_desc_dma; - u32 sh_desc[DESC_RNG_LEN]; - unsigned int cur_buf_idx; - int current_buf; - struct buf_data bufs[2]; -}; - -static struct caam_rng_ctx *rng_ctx; - -/* - * Variable used to avoid double free of resources in case - * algorithm registration was unsuccessful - */ -static bool init_done; - -static inline void rng_unmap_buf(struct device *jrdev, struct buf_data *bd) -{ - if (bd->addr) - dma_unmap_single(jrdev, bd->addr, RN_BUF_SIZE, - DMA_FROM_DEVICE); -} - -static inline void rng_unmap_ctx(struct caam_rng_ctx *ctx) -{ - struct device *jrdev = ctx->jrdev; - - if (ctx->sh_desc_dma) - dma_unmap_single(jrdev, ctx->sh_desc_dma, - desc_bytes(ctx->sh_desc), DMA_TO_DEVICE); - rng_unmap_buf(jrdev, &ctx->bufs[0]); - rng_unmap_buf(jrdev, &ctx->bufs[1]); -} - -static void rng_done(struct device *jrdev, u32 *desc, u32 err, void *context) -{ - struct buf_data *bd; - - bd = container_of(desc, struct buf_data, hw_desc[0]); - - if (err) - caam_jr_strstatus(jrdev, err); - - atomic_set(&bd->empty, BUF_NOT_EMPTY); - complete(&bd->filled); - - /* Buffer refilled, invalidate cache */ - dma_sync_single_for_cpu(jrdev, bd->addr, RN_BUF_SIZE, DMA_FROM_DEVICE); - - print_hex_dump_debug("rng refreshed buf@: ", DUMP_PREFIX_ADDRESS, 16, 4, - bd->buf, RN_BUF_SIZE, 1); -} - -static inline int submit_job(struct caam_rng_ctx *ctx, int to_current) -{ - struct buf_data *bd = &ctx->bufs[!(to_current ^ ctx->current_buf)]; - struct device *jrdev = ctx->jrdev; - u32 *desc = bd->hw_desc; - int err; - - dev_dbg(jrdev, "submitting job %d\n", !(to_current ^ ctx->current_buf)); - init_completion(&bd->filled); - err = caam_jr_enqueue(jrdev, desc, rng_done, ctx); - if (err) - complete(&bd->filled); /* don't wait on failed job*/ - else - atomic_inc(&bd->empty); /* note if pending */ - - return err; -} - -static int caam_read(struct hwrng *rng, void *data, size_t max, bool wait) -{ - struct caam_rng_ctx *ctx = rng_ctx; - struct buf_data *bd = &ctx->bufs[ctx->current_buf]; - int next_buf_idx, copied_idx; - int err; - - if (atomic_read(&bd->empty)) { - /* try to submit job if there wasn't one */ - if (atomic_read(&bd->empty) == BUF_EMPTY) { - err = submit_job(ctx, 1); - /* if can't submit job, can't even wait */ - if (err) - return 0; - } - /* no immediate data, so exit if not waiting */ - if (!wait) - return 0; - - /* waiting for pending job */ - if (atomic_read(&bd->empty)) - wait_for_completion(&bd->filled); - } - - next_buf_idx = ctx->cur_buf_idx + max; - dev_dbg(ctx->jrdev, "%s: start reading at buffer %d, idx %d\n", - __func__, ctx->current_buf, ctx->cur_buf_idx); - - /* if enough data in current buffer */ - if (next_buf_idx < RN_BUF_SIZE) { - memcpy(data, bd->buf + ctx->cur_buf_idx, max); - ctx->cur_buf_idx = next_buf_idx; - return max; - } - - /* else, copy what's left... */ - copied_idx = RN_BUF_SIZE - ctx->cur_buf_idx; - memcpy(data, bd->buf + ctx->cur_buf_idx, copied_idx); - ctx->cur_buf_idx = 0; - atomic_set(&bd->empty, BUF_EMPTY); - - /* ...refill... */ - submit_job(ctx, 1); - - /* and use next buffer */ - ctx->current_buf = !ctx->current_buf; - dev_dbg(ctx->jrdev, "switched to buffer %d\n", ctx->current_buf); - - /* since there already is some data read, don't wait */ - return copied_idx + caam_read(rng, data + copied_idx, - max - copied_idx, false); -} - -static inline int rng_create_sh_desc(struct caam_rng_ctx *ctx) -{ - struct device *jrdev = ctx->jrdev; - u32 *desc = ctx->sh_desc; - - init_sh_desc(desc, HDR_SHARE_SERIAL); - - /* Generate random bytes */ - append_operation(desc, OP_ALG_ALGSEL_RNG | OP_TYPE_CLASS1_ALG | - OP_ALG_PR_ON); - - /* Store bytes */ - append_seq_fifo_store(desc, RN_BUF_SIZE, FIFOST_TYPE_RNGSTORE); - - ctx->sh_desc_dma = dma_map_single(jrdev, desc, desc_bytes(desc), - DMA_TO_DEVICE); - if (dma_mapping_error(jrdev, ctx->sh_desc_dma)) { - dev_err(jrdev, "unable to map shared descriptor\n"); - return -ENOMEM; - } - - print_hex_dump_debug("rng shdesc@: ", DUMP_PREFIX_ADDRESS, 16, 4, - desc, desc_bytes(desc), 1); - - return 0; -} - -static inline int rng_create_job_desc(struct caam_rng_ctx *ctx, int buf_id) -{ - struct device *jrdev = ctx->jrdev; - struct buf_data *bd = &ctx->bufs[buf_id]; - u32 *desc = bd->hw_desc; - int sh_len = desc_len(ctx->sh_desc); - - init_job_desc_shared(desc, ctx->sh_desc_dma, sh_len, HDR_SHARE_DEFER | - HDR_REVERSE); - - bd->addr = dma_map_single(jrdev, bd->buf, RN_BUF_SIZE, DMA_FROM_DEVICE); - if (dma_mapping_error(jrdev, bd->addr)) { - dev_err(jrdev, "unable to map dst\n"); - return -ENOMEM; - } - - append_seq_out_ptr_intlen(desc, bd->addr, RN_BUF_SIZE, 0); - - print_hex_dump_debug("rng job desc@: ", DUMP_PREFIX_ADDRESS, 16, 4, - desc, desc_bytes(desc), 1); - - return 0; -} - -static void caam_cleanup(struct hwrng *rng) -{ - int i; - struct buf_data *bd; - - for (i = 0; i < 2; i++) { - bd = &rng_ctx->bufs[i]; - if (atomic_read(&bd->empty) == BUF_PENDING) - wait_for_completion(&bd->filled); - } - - rng_unmap_ctx(rng_ctx); -} - -static int caam_init_buf(struct caam_rng_ctx *ctx, int buf_id) -{ - struct buf_data *bd = &ctx->bufs[buf_id]; - int err; - - err = rng_create_job_desc(ctx, buf_id); - if (err) - return err; - - atomic_set(&bd->empty, BUF_EMPTY); - submit_job(ctx, buf_id == ctx->current_buf); - wait_for_completion(&bd->filled); - - return 0; -} - -static int caam_init_rng(struct caam_rng_ctx *ctx, struct device *jrdev) -{ - int err; - - ctx->jrdev = jrdev; - - err = rng_create_sh_desc(ctx); - if (err) - return err; - - ctx->current_buf = 0; - ctx->cur_buf_idx = 0; - - err = caam_init_buf(ctx, 0); - if (err) - return err; - - return caam_init_buf(ctx, 1); -} - -static struct hwrng caam_rng = { - .name = "rng-caam", - .cleanup = caam_cleanup, - .read = caam_read, -}; - -void caam_rng_exit(void) -{ - if (!init_done) - return; - - caam_jr_free(rng_ctx->jrdev); - hwrng_unregister(&caam_rng); - kfree(rng_ctx); -} - -int caam_rng_init(struct device *ctrldev) -{ - struct device *dev; - struct caam_drv_private *priv = dev_get_drvdata(ctrldev); - int err; - init_done = false; - - if (!caam_has_rng(priv)) - return 0; - - dev = caam_jr_alloc(); - if (IS_ERR(dev)) { - pr_err("Job Ring Device allocation for transform failed\n"); - return PTR_ERR(dev); - } - rng_ctx = kmalloc(sizeof(*rng_ctx), GFP_DMA | GFP_KERNEL); - if (!rng_ctx) { - err = -ENOMEM; - goto free_caam_alloc; - } - err = caam_init_rng(rng_ctx, dev); - if (err) - goto free_rng_ctx; - - dev_info(dev, "registering rng-caam\n"); - - err = hwrng_register(&caam_rng); - if (!err) { - init_done = true; - return err; - } - -free_rng_ctx: - kfree(rng_ctx); -free_caam_alloc: - caam_jr_free(dev); - return err; -} diff --git a/drivers/crypto/caam/ctrl.c b/drivers/crypto/caam/ctrl.c index 8054ec29d35a..adbdef06bcf9 100644 --- a/drivers/crypto/caam/ctrl.c +++ b/drivers/crypto/caam/ctrl.c @@ -907,6 +907,12 @@ static int caam_probe(struct platform_device *pdev) &ctrlpriv->ctl_tdsk_wrap); #endif + ret = caam_trng_register(dev); + if (ret) { + dev_err(dev, "Failed to register HWRNG interface\n"); + return ret; + } + ret = devm_of_platform_populate(dev); if (ret) dev_err(dev, "JR platform devices creation error\n"); diff --git a/drivers/crypto/caam/intern.h b/drivers/crypto/caam/intern.h index f815e1ad4608..54bb04aa86bd 100644 --- a/drivers/crypto/caam/intern.h +++ b/drivers/crypto/caam/intern.h @@ -174,20 +174,15 @@ static inline void caam_pkc_exit(void) #ifdef CONFIG_CRYPTO_DEV_FSL_CAAM_RNG_API -int caam_rng_init(struct device *dev); -void caam_rng_exit(void); +int caam_trng_register(struct device *dev); #else -static inline int caam_rng_init(struct device *dev) +static inline int caam_trng_register(struct device *dev) { return 0; } -static inline void caam_rng_exit(void) -{ -} - #endif /* CONFIG_CRYPTO_DEV_FSL_CAAM_RNG_API */ #ifdef CONFIG_CAAM_QI diff --git a/drivers/crypto/caam/jr.c b/drivers/crypto/caam/jr.c index fc97cde27059..c745b7044fe6 100644 --- a/drivers/crypto/caam/jr.c +++ b/drivers/crypto/caam/jr.c @@ -37,7 +37,6 @@ static void register_algs(struct device *dev) caam_algapi_init(dev); caam_algapi_hash_init(dev); caam_pkc_init(dev); - caam_rng_init(dev); caam_qi_algapi_init(dev); algs_unlock: @@ -53,7 +52,6 @@ static void unregister_algs(void) caam_qi_algapi_exit(); - caam_rng_exit(); caam_pkc_exit(); caam_algapi_hash_exit(); caam_algapi_exit(); diff --git a/drivers/crypto/caam/regs.h b/drivers/crypto/caam/regs.h index fe1f8c1409fd..262399387e27 100644 --- a/drivers/crypto/caam/regs.h +++ b/drivers/crypto/caam/regs.h @@ -488,6 +488,7 @@ struct rngtst { /* RNG4 TRNG test registers */ struct rng4tst { #define RTMCTL_ACC BIT(5) /* TRNG access mode */ +#define RTMCTL_ENT_VAL BIT(10) #define RTMCTL_PRGM BIT(16) /* 1 -> program mode, 0 -> run mode */ #define RTMCTL_SAMP_MODE_VON_NEUMANN_ES_SC 0 /* use von Neumann data in both entropy shifter and @@ -521,7 +522,9 @@ struct rng4tst { u32 rtfrqmax; /* PRGM=1: freq. count max. limit register */ u32 rtfrqcnt; /* PRGM=0: freq. count register */ }; - u32 rsvd1[40]; + u32 rsvd1[8]; + u32 rtent[16]; /* RTENT0 - RTENT15 */ + u32 rsvd2[16]; /* RTPKRCNTn */ #define RDSTA_SKVT 0x80000000 #define RDSTA_SKVN 0x40000000 #define RDSTA_PR0 BIT(4) @@ -530,7 +533,7 @@ struct rng4tst { #define RDSTA_IF1 0x00000002 #define RDSTA_IFMASK (RDSTA_PR1 | RDSTA_PR0 | RDSTA_IF1 | RDSTA_IF0) u32 rdsta; - u32 rsvd2[15]; + u32 rsvd3[15]; }; /* diff --git a/drivers/crypto/caam/trng.c b/drivers/crypto/caam/trng.c new file mode 100644 index 000000000000..ab2af786543e --- /dev/null +++ b/drivers/crypto/caam/trng.c @@ -0,0 +1,85 @@ +// SPDX-License-Identifier: GPL-2.0+ +/* + * hw_random interface for TRNG generator in CAAM RNG block + * + * Copyright 2019 Zoidac Inflight Innovations + * + */ + +#include + +#include "compat.h" +#include "regs.h" +#include "intern.h" + +struct caam_trng_ctx { + struct rng4tst __iomem *r4tst; + struct hwrng rng; +}; + +static bool caam_trng_busy(struct caam_trng_ctx *ctx) +{ + return !(rd_reg32(&ctx->r4tst->rtmctl) & RTMCTL_ENT_VAL); +} + +static int caam_trng_read(struct hwrng *rng, void *data, size_t max, bool wait) +{ + struct caam_trng_ctx *ctx = (void *)rng->priv; + u32 rtent[ARRAY_SIZE(ctx->r4tst->rtent)]; + size_t residue = max; + + clrsetbits_32(&ctx->r4tst->rtmctl, 0, RTMCTL_ACC); + + do { + const size_t chunk = min(residue, sizeof(rtent)); + unsigned int i; + + while (caam_trng_busy(ctx)) { + if (wait) + cpu_relax(); + else + goto out; + } + + for (i = 0; i < DIV_ROUND_UP(chunk, sizeof(u32)); i++) + rtent[i] = rd_reg32(&ctx->r4tst->rtent[i]); + + memcpy(data, rtent, chunk); + + residue -= chunk; + data += chunk; + } while (residue); + +out: + clrsetbits_32(&ctx->r4tst->rtmctl, RTMCTL_ACC, 0); + return max - residue; +} + +int caam_trng_register(struct device *ctrldev) +{ + struct caam_drv_private *priv = dev_get_drvdata(ctrldev); + + if (caam_has_rng(priv)) { + struct caam_trng_ctx *ctx; + int err; + + ctx = devm_kzalloc(ctrldev, sizeof(*ctx), GFP_KERNEL); + if (!ctx) + return -ENOMEM; + + ctx->r4tst = &priv->ctrl->r4tst[0]; + + ctx->rng.name = "trng-caam"; + ctx->rng.read = caam_trng_read; + ctx->rng.priv = (unsigned long)ctx; + ctx->rng.quality = 999; + + dev_info(ctrldev, "registering %s\n", ctx->rng.name); + + err = devm_hwrng_register(ctrldev, &ctx->rng); + if (err) + return err; + } + + return 0; +} From patchwork Mon Nov 18 15:38:43 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Andrey Smirnov X-Patchwork-Id: 11249897 X-Patchwork-Delegate: herbert@gondor.apana.org.au Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 4D76A109A for ; Mon, 18 Nov 2019 15:39:19 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 19C3D222A4 for ; Mon, 18 Nov 2019 15:39:19 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="paoK1bSy" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727378AbfKRPjR (ORCPT ); Mon, 18 Nov 2019 10:39:17 -0500 Received: from mail-pg1-f193.google.com ([209.85.215.193]:43303 "EHLO mail-pg1-f193.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727363AbfKRPjR (ORCPT ); Mon, 18 Nov 2019 10:39:17 -0500 Received: by mail-pg1-f193.google.com with SMTP id b1so323162pgq.10; Mon, 18 Nov 2019 07:39:16 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=nxVu6jtA8AokWDkx0sh9PMR+Vf8gqiLuOYYBOFYUbUI=; b=paoK1bSyBmaR478X5wtbVD1sMaRCe3xDBzY5GaROJUIXUfcXnnQY0YlcFgPQS9v1xE ypA+dKOqLMGnJD27Rw2p9dk+ZVp4sv2Cs+csBb9IL2C+S+4+5TKXhQZpxy87L5rKQ4Vs snO4iqOvYPtnO/vn4ysMKI49mRyQf1ZWUsKV/9SoauEkC04Jxgxmh8iAJRlDDmOgEoj2 SHMxPCvMLY3DXDzMwWJCDGWVk7YSUvYT479Y0EYaQzZs5+2mJxRLZ4G0hbZ65U/ziXjN 6XJ/C+e7V+s6Oyhu5RDOlYlNxemb4bafA9/WZajA3ix5SOnO/04P/yOBhubz3LjX4TZn zatg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=nxVu6jtA8AokWDkx0sh9PMR+Vf8gqiLuOYYBOFYUbUI=; b=eTLJl9IGBHqtut8CKzTXuPp+QRJ64YgGJ69m8BHCAz0XVGLfeDjzLWOgLwjTka2Ua/ Bj8OTyO0WUwWyeojOTQiYJOC8wl5opiIi+EVJ5XUIoLbrlscnLafARhF9OtyfcDTkS4B k4s5lhx+YMIPTl/puEKsr+gVDFUepS+3A4QUTAo/iPJkcjVE/HtgdhyH34KFhYWMOLuN 6+onJUOH+FuLnJcvFjAeqIscKdSoAwDw/0shrwmCMvZd3swFmMLYXMOCQSJsvBtBu1gA Y1bP6/U5gHbNjHrVf0g1qstHu+ucwLzrPuY5FxOCrVSUiXNcSzXBUiSmiLOVPZP0Zeuv Dlng== X-Gm-Message-State: APjAAAUZ56zVxfZAx66lXfl8Tu8u5KA4HL89Dqtf05kDGDXUR/wnRkMd K/NwTnq5XzryKenoT3E3cF4La/lI X-Google-Smtp-Source: APXvYqxnJe1hUOWvhUxaZOItcJOGk4eZQLs8/gUxLtC6ubS6Enqy4Q08rPmkl4jzdzDg1cnmJWsRpQ== X-Received: by 2002:a62:545:: with SMTP id 66mr34016944pff.1.1574091555794; Mon, 18 Nov 2019 07:39:15 -0800 (PST) Received: from localhost.hsd1.wa.comcast.net ([2601:602:847f:811f:babe:8e8d:b27e:e6d7]) by smtp.gmail.com with ESMTPSA id z7sm23573732pfr.165.2019.11.18.07.39.14 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 18 Nov 2019 07:39:14 -0800 (PST) From: Andrey Smirnov To: linux-crypto@vger.kernel.org Cc: Andrey Smirnov , Chris Healy , Lucas Stach , =?utf-8?q?Horia_Geant=C4=83?= , Herbert Xu , Iuliana Prodan , linux-imx@nxp.com, linux-kernel@vger.kernel.org Subject: [PATCH v2 6/6] crypto: caam - expose SEC4 DRNG via crypto RNG API Date: Mon, 18 Nov 2019 07:38:43 -0800 Message-Id: <20191118153843.28136-7-andrew.smirnov@gmail.com> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20191118153843.28136-1-andrew.smirnov@gmail.com> References: <20191118153843.28136-1-andrew.smirnov@gmail.com> MIME-Version: 1.0 Sender: linux-crypto-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org Expose SEC4 DRNG IP block using crypto RNG API so it could be used both by kernel and userspace code. Signed-off-by: Andrey Smirnov Cc: Chris Healy Cc: Lucas Stach Cc: Horia Geantă Cc: Herbert Xu Cc: Iuliana Prodan Cc: linux-imx@nxp.com Cc: linux-crypto@vger.kernel.org Cc: linux-kernel@vger.kernel.org --- drivers/crypto/caam/Kconfig | 8 ++ drivers/crypto/caam/Makefile | 1 + drivers/crypto/caam/drng.c | 175 +++++++++++++++++++++++++++++++++++ drivers/crypto/caam/intern.h | 13 +++ drivers/crypto/caam/jr.c | 1 + 5 files changed, 198 insertions(+) create mode 100644 drivers/crypto/caam/drng.c diff --git a/drivers/crypto/caam/Kconfig b/drivers/crypto/caam/Kconfig index 22116a8e2ff3..11a8f9c02448 100644 --- a/drivers/crypto/caam/Kconfig +++ b/drivers/crypto/caam/Kconfig @@ -146,6 +146,14 @@ config CRYPTO_DEV_FSL_CAAM_PKC_API Supported cryptographic primitives: encryption, decryption, signature and verification. +config CRYPTO_DEV_FSL_CAAM_DRNG_API + bool "Register caam device for hwrng API" + default y + select CRYPTO_RNG + help + Selecting this will register the SEC4 DRNG to + the crypto RNG API. + endif # CRYPTO_DEV_FSL_CAAM_JR endif # CRYPTO_DEV_FSL_CAAM diff --git a/drivers/crypto/caam/Makefile b/drivers/crypto/caam/Makefile index 04884fc087f9..02b7ed8823ce 100644 --- a/drivers/crypto/caam/Makefile +++ b/drivers/crypto/caam/Makefile @@ -20,6 +20,7 @@ caam_jr-y := jr.o key_gen.o caam_jr-$(CONFIG_CRYPTO_DEV_FSL_CAAM_CRYPTO_API) += caamalg.o caam_jr-$(CONFIG_CRYPTO_DEV_FSL_CAAM_CRYPTO_API_QI) += caamalg_qi.o caam_jr-$(CONFIG_CRYPTO_DEV_FSL_CAAM_AHASH_API) += caamhash.o +caam_jr-$(CONFIG_CRYPTO_DEV_FSL_CAAM_DRNG_API) += drng.o caam_jr-$(CONFIG_CRYPTO_DEV_FSL_CAAM_PKC_API) += caampkc.o pkc_desc.o caam-$(CONFIG_CRYPTO_DEV_FSL_CAAM_CRYPTO_API_QI) += qi.o diff --git a/drivers/crypto/caam/drng.c b/drivers/crypto/caam/drng.c new file mode 100644 index 000000000000..75dbe4abdaa3 --- /dev/null +++ b/drivers/crypto/caam/drng.c @@ -0,0 +1,175 @@ +// SPDX-License-Identifier: GPL-2.0+ +/* + * Driver to expose SEC4 DRNG via crypto RNG API + * + * Copyright 2019 Zodiac Inflight Innovations + * + * Based on CAAM SEC4 hw_random driver + * + * Copyright 2011 Freescale Semiconductor, Inc. + * Copyright 2018-2019 NXP + * + * Based on caamalg.c crypto API driver. + * + */ + +#include +#include + +#include + +#include "compat.h" + +#include "regs.h" +#include "intern.h" +#include "desc_constr.h" +#include "jr.h" +#include "error.h" + +#define CAAM_DRNG_MAX_FIFO_STORE_SIZE ((unsigned int)U16_MAX) + +/* rng per-device context */ +struct caam_drng_ctx { + struct device *jrdev; + struct completion done; +}; + +static void rng_done(struct device *jrdev, u32 *desc, u32 err, void *context) +{ + struct caam_drng_ctx *ctx = context; + + if (err) + caam_jr_strstatus(jrdev, err); + + complete(&ctx->done); +} + +static int caam_drng_generate(struct crypto_rng *tfm, + const u8 *src, unsigned int slen, + u8 *dst, unsigned int dlen) +{ + struct caam_drng_ctx *ctx = crypto_rng_ctx(tfm); + struct device *jrdev = ctx->jrdev; + unsigned int residue = dlen; + dma_addr_t dst_dma, cur_dma; + u32 *desc; + int ret; + + desc = kzalloc(5 * CAAM_CMD_SZ + CAAM_PTR_SZ_MAX, + GFP_KERNEL | GFP_DMA); + if (!desc) + return -ENOMEM; + + cur_dma = dst_dma = dma_map_single(jrdev, dst, dlen, DMA_FROM_DEVICE); + if (dma_mapping_error(jrdev, dst_dma)) { + dev_err(jrdev, "unable to map destination memory\n"); + ret = -ENOMEM; + goto free_mem; + } + + do { + const unsigned int chunk = min(residue, + CAAM_DRNG_MAX_FIFO_STORE_SIZE); + + init_job_desc(desc, 0); /* 1 word */ + /* Generate random bytes */ + append_operation(desc, OP_ALG_ALGSEL_RNG | OP_TYPE_CLASS1_ALG | + OP_ALG_PR_ON); /* 1 word */ + /* Store bytes */ + append_seq_out_ptr_intlen(desc, cur_dma, chunk, 0); + append_seq_fifo_store(desc, chunk, FIFOST_TYPE_RNGSTORE); + + print_hex_dump_debug("rng job desc@: ", DUMP_PREFIX_ADDRESS, + 16, 4, desc, desc_bytes(desc), 1); + + init_completion(&ctx->done); + ret = caam_jr_enqueue(jrdev, desc, rng_done, ctx); + if (ret) + break; + + wait_for_completion(&ctx->done); + + cur_dma += chunk; + residue -= chunk; + } while (residue); + + dma_unmap_single(jrdev, dst_dma, dlen, DMA_FROM_DEVICE); +free_mem: + kfree(desc); + return ret; +} + +static int caam_drng_init(struct crypto_tfm *tfm) +{ + struct caam_drng_ctx *ctx = crypto_tfm_ctx(tfm); + int ret; + + ctx->jrdev = caam_jr_alloc(); + ret = PTR_ERR_OR_ZERO(ctx->jrdev); + if (ret) { + pr_err("Job Ring Device allocation for transform failed\n"); + return ret; + } + + return 0; +} + +static void caam_drng_exit(struct crypto_tfm *tfm) +{ + struct caam_drng_ctx *ctx = crypto_tfm_ctx(tfm); + + caam_jr_free(ctx->jrdev); +} + +static int caam_drng_seed(struct crypto_rng *tfm, + const u8 *seed, unsigned int slen) +{ + return 0; +} + +static struct rng_alg caam_drng_alg = { + .generate = caam_drng_generate, + .seed = caam_drng_seed, + .seedsize = 0, + .base = { + .cra_name = "drng-caam", + .cra_driver_name = "drng-caam", + .cra_priority = 300, + .cra_ctxsize = sizeof(struct caam_drng_ctx), + .cra_module = THIS_MODULE, + .cra_init = caam_drng_init, + .cra_exit = caam_drng_exit, + }, +}; + +static void caam_drng_unregister(void *data) +{ + crypto_unregister_rng(&caam_drng_alg); +} + +int caam_drng_register(struct device *ctrldev) +{ + struct caam_drv_private *priv = dev_get_drvdata(ctrldev); + + if (caam_has_rng(priv)) { + int ret; + + ret = crypto_register_rng(&caam_drng_alg); + if (ret) { + dev_err(ctrldev, + "couldn't register rng crypto alg: %d\n", + ret); + return ret; + } + + ret = devm_add_action_or_reset(ctrldev, caam_drng_unregister, + NULL); + if (ret) + return ret; + + dev_info(ctrldev, + "registering %s\n", caam_drng_alg.base.cra_name); + } + + return 0; +} diff --git a/drivers/crypto/caam/intern.h b/drivers/crypto/caam/intern.h index 54bb04aa86bd..0c81eefd13a9 100644 --- a/drivers/crypto/caam/intern.h +++ b/drivers/crypto/caam/intern.h @@ -185,6 +185,19 @@ static inline int caam_trng_register(struct device *dev) #endif /* CONFIG_CRYPTO_DEV_FSL_CAAM_RNG_API */ +#ifdef CONFIG_CRYPTO_DEV_FSL_CAAM_DRNG_API + +int caam_drng_register(struct device *dev); + +#else + +static inline int caam_drng_register(struct device *dev) +{ + return 0; +} + +#endif /* CONFIG_CRYPTO_DEV_FSL_CAAM_DRNG_API */ + #ifdef CONFIG_CAAM_QI int caam_qi_algapi_init(struct device *dev); diff --git a/drivers/crypto/caam/jr.c b/drivers/crypto/caam/jr.c index c745b7044fe6..e68ba0606e3f 100644 --- a/drivers/crypto/caam/jr.c +++ b/drivers/crypto/caam/jr.c @@ -38,6 +38,7 @@ static void register_algs(struct device *dev) caam_algapi_hash_init(dev); caam_pkc_init(dev); caam_qi_algapi_init(dev); + caam_drng_register(dev); algs_unlock: mutex_unlock(&algs_lock);