aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAntoine Tenart <antoine.tenart@bootlin.com>2018-03-19 04:21:20 -0400
committerHerbert Xu <herbert@gondor.apana.org.au>2018-03-30 13:32:48 -0400
commit73f36ea703219115957a6a9f5dbe9d92b954eea4 (patch)
tree1828ee0d19217630f8d440ad1b98ce34a15be826
parent4505bb02ec87f24e4704716ef5a55b233805157d (diff)
crypto: inside-secure - hmac(sha256) support
This patch adds the hmac(sha256) support to the Inside Secure cryptographic engine driver. Signed-off-by: Antoine Tenart <antoine.tenart@bootlin.com> Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
-rw-r--r--drivers/crypto/inside-secure/safexcel.c3
-rw-r--r--drivers/crypto/inside-secure/safexcel.h1
-rw-r--r--drivers/crypto/inside-secure/safexcel_hash.c80
3 files changed, 75 insertions, 9 deletions
diff --git a/drivers/crypto/inside-secure/safexcel.c b/drivers/crypto/inside-secure/safexcel.c
index f7d7293de699..33595f41586f 100644
--- a/drivers/crypto/inside-secure/safexcel.c
+++ b/drivers/crypto/inside-secure/safexcel.c
@@ -354,7 +354,7 @@ static int safexcel_hw_init(struct safexcel_crypto_priv *priv)
354 val |= EIP197_PROTOCOL_ENCRYPT_ONLY | EIP197_PROTOCOL_HASH_ONLY; 354 val |= EIP197_PROTOCOL_ENCRYPT_ONLY | EIP197_PROTOCOL_HASH_ONLY;
355 val |= EIP197_ALG_AES_ECB | EIP197_ALG_AES_CBC; 355 val |= EIP197_ALG_AES_ECB | EIP197_ALG_AES_CBC;
356 val |= EIP197_ALG_SHA1 | EIP197_ALG_HMAC_SHA1; 356 val |= EIP197_ALG_SHA1 | EIP197_ALG_HMAC_SHA1;
357 val |= EIP197_ALG_SHA2; 357 val |= EIP197_ALG_SHA2 | EIP197_ALG_HMAC_SHA2;
358 writel(val, EIP197_PE(priv) + EIP197_PE_EIP96_FUNCTION_EN); 358 writel(val, EIP197_PE(priv) + EIP197_PE_EIP96_FUNCTION_EN);
359 359
360 /* Command Descriptor Rings prepare */ 360 /* Command Descriptor Rings prepare */
@@ -768,6 +768,7 @@ static struct safexcel_alg_template *safexcel_algs[] = {
768 &safexcel_alg_sha224, 768 &safexcel_alg_sha224,
769 &safexcel_alg_sha256, 769 &safexcel_alg_sha256,
770 &safexcel_alg_hmac_sha1, 770 &safexcel_alg_hmac_sha1,
771 &safexcel_alg_hmac_sha256,
771}; 772};
772 773
773static int safexcel_register_algorithms(struct safexcel_crypto_priv *priv) 774static int safexcel_register_algorithms(struct safexcel_crypto_priv *priv)
diff --git a/drivers/crypto/inside-secure/safexcel.h b/drivers/crypto/inside-secure/safexcel.h
index 295813920618..99e0f32452ff 100644
--- a/drivers/crypto/inside-secure/safexcel.h
+++ b/drivers/crypto/inside-secure/safexcel.h
@@ -633,5 +633,6 @@ extern struct safexcel_alg_template safexcel_alg_sha1;
633extern struct safexcel_alg_template safexcel_alg_sha224; 633extern struct safexcel_alg_template safexcel_alg_sha224;
634extern struct safexcel_alg_template safexcel_alg_sha256; 634extern struct safexcel_alg_template safexcel_alg_sha256;
635extern struct safexcel_alg_template safexcel_alg_hmac_sha1; 635extern struct safexcel_alg_template safexcel_alg_hmac_sha1;
636extern struct safexcel_alg_template safexcel_alg_hmac_sha256;
636 637
637#endif 638#endif
diff --git a/drivers/crypto/inside-secure/safexcel_hash.c b/drivers/crypto/inside-secure/safexcel_hash.c
index 31df97eec89a..e84a1ce66309 100644
--- a/drivers/crypto/inside-secure/safexcel_hash.c
+++ b/drivers/crypto/inside-secure/safexcel_hash.c
@@ -22,8 +22,8 @@ struct safexcel_ahash_ctx {
22 22
23 u32 alg; 23 u32 alg;
24 24
25 u32 ipad[SHA1_DIGEST_SIZE / sizeof(u32)]; 25 u32 ipad[SHA256_DIGEST_SIZE / sizeof(u32)];
26 u32 opad[SHA1_DIGEST_SIZE / sizeof(u32)]; 26 u32 opad[SHA256_DIGEST_SIZE / sizeof(u32)];
27}; 27};
28 28
29struct safexcel_ahash_req { 29struct safexcel_ahash_req {
@@ -963,20 +963,21 @@ free_ahash:
963 return ret; 963 return ret;
964} 964}
965 965
966static int safexcel_hmac_sha1_setkey(struct crypto_ahash *tfm, const u8 *key, 966static int safexcel_hmac_alg_setkey(struct crypto_ahash *tfm, const u8 *key,
967 unsigned int keylen) 967 unsigned int keylen, const char *alg,
968 unsigned int state_sz)
968{ 969{
969 struct safexcel_ahash_ctx *ctx = crypto_tfm_ctx(crypto_ahash_tfm(tfm)); 970 struct safexcel_ahash_ctx *ctx = crypto_tfm_ctx(crypto_ahash_tfm(tfm));
970 struct safexcel_crypto_priv *priv = ctx->priv; 971 struct safexcel_crypto_priv *priv = ctx->priv;
971 struct safexcel_ahash_export_state istate, ostate; 972 struct safexcel_ahash_export_state istate, ostate;
972 int ret, i; 973 int ret, i;
973 974
974 ret = safexcel_hmac_setkey("safexcel-sha1", key, keylen, &istate, &ostate); 975 ret = safexcel_hmac_setkey(alg, key, keylen, &istate, &ostate);
975 if (ret) 976 if (ret)
976 return ret; 977 return ret;
977 978
978 if (priv->version == EIP197 && ctx->base.ctxr) { 979 if (priv->version == EIP197 && ctx->base.ctxr) {
979 for (i = 0; i < SHA1_DIGEST_SIZE / sizeof(u32); i++) { 980 for (i = 0; i < state_sz / sizeof(u32); i++) {
980 if (ctx->ipad[i] != le32_to_cpu(istate.state[i]) || 981 if (ctx->ipad[i] != le32_to_cpu(istate.state[i]) ||
981 ctx->opad[i] != le32_to_cpu(ostate.state[i])) { 982 ctx->opad[i] != le32_to_cpu(ostate.state[i])) {
982 ctx->base.needs_inv = true; 983 ctx->base.needs_inv = true;
@@ -985,12 +986,19 @@ static int safexcel_hmac_sha1_setkey(struct crypto_ahash *tfm, const u8 *key,
985 } 986 }
986 } 987 }
987 988
988 memcpy(ctx->ipad, &istate.state, SHA1_DIGEST_SIZE); 989 memcpy(ctx->ipad, &istate.state, state_sz);
989 memcpy(ctx->opad, &ostate.state, SHA1_DIGEST_SIZE); 990 memcpy(ctx->opad, &ostate.state, state_sz);
990 991
991 return 0; 992 return 0;
992} 993}
993 994
995static int safexcel_hmac_sha1_setkey(struct crypto_ahash *tfm, const u8 *key,
996 unsigned int keylen)
997{
998 return safexcel_hmac_alg_setkey(tfm, key, keylen, "safexcel-sha1",
999 SHA1_DIGEST_SIZE);
1000}
1001
994struct safexcel_alg_template safexcel_alg_hmac_sha1 = { 1002struct safexcel_alg_template safexcel_alg_hmac_sha1 = {
995 .type = SAFEXCEL_ALG_TYPE_AHASH, 1003 .type = SAFEXCEL_ALG_TYPE_AHASH,
996 .alg.ahash = { 1004 .alg.ahash = {
@@ -1144,3 +1152,59 @@ struct safexcel_alg_template safexcel_alg_sha224 = {
1144 }, 1152 },
1145 }, 1153 },
1146}; 1154};
1155
1156static int safexcel_hmac_sha256_setkey(struct crypto_ahash *tfm, const u8 *key,
1157 unsigned int keylen)
1158{
1159 return safexcel_hmac_alg_setkey(tfm, key, keylen, "safexcel-sha256",
1160 SHA256_DIGEST_SIZE);
1161}
1162
1163static int safexcel_hmac_sha256_init(struct ahash_request *areq)
1164{
1165 struct safexcel_ahash_req *req = ahash_request_ctx(areq);
1166
1167 safexcel_sha256_init(areq);
1168 req->digest = CONTEXT_CONTROL_DIGEST_HMAC;
1169 return 0;
1170}
1171
1172static int safexcel_hmac_sha256_digest(struct ahash_request *areq)
1173{
1174 int ret = safexcel_hmac_sha256_init(areq);
1175
1176 if (ret)
1177 return ret;
1178
1179 return safexcel_ahash_finup(areq);
1180}
1181
1182struct safexcel_alg_template safexcel_alg_hmac_sha256 = {
1183 .type = SAFEXCEL_ALG_TYPE_AHASH,
1184 .alg.ahash = {
1185 .init = safexcel_hmac_sha256_init,
1186 .update = safexcel_ahash_update,
1187 .final = safexcel_ahash_final,
1188 .finup = safexcel_ahash_finup,
1189 .digest = safexcel_hmac_sha256_digest,
1190 .setkey = safexcel_hmac_sha256_setkey,
1191 .export = safexcel_ahash_export,
1192 .import = safexcel_ahash_import,
1193 .halg = {
1194 .digestsize = SHA256_DIGEST_SIZE,
1195 .statesize = sizeof(struct safexcel_ahash_export_state),
1196 .base = {
1197 .cra_name = "hmac(sha256)",
1198 .cra_driver_name = "safexcel-hmac-sha256",
1199 .cra_priority = 300,
1200 .cra_flags = CRYPTO_ALG_ASYNC |
1201 CRYPTO_ALG_KERN_DRIVER_ONLY,
1202 .cra_blocksize = SHA256_BLOCK_SIZE,
1203 .cra_ctxsize = sizeof(struct safexcel_ahash_ctx),
1204 .cra_init = safexcel_ahash_cra_init,
1205 .cra_exit = safexcel_ahash_cra_exit,
1206 .cra_module = THIS_MODULE,
1207 },
1208 },
1209 },
1210};