Skip to content

Commit 7a627db

Browse files
Pascal van Leeuwenherbertx
authored andcommitted
crypto: inside-secure - Added support for the AES-CMAC ahash
This patch adds support for the AES-CMAC authentication algorithm. Signed-off-by: Pascal van Leeuwen <pvanleeuwen@verimatrix.com> Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
1 parent 38f21b4 commit 7a627db

3 files changed

Lines changed: 101 additions & 0 deletions

File tree

‎drivers/crypto/inside-secure/safexcel.c‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1172,6 +1172,7 @@ static struct safexcel_alg_template *safexcel_algs[] = {
11721172
&safexcel_alg_crc32,
11731173
&safexcel_alg_cbcmac,
11741174
&safexcel_alg_xcbcmac,
1175+
&safexcel_alg_cmac,
11751176
};
11761177

11771178
static int safexcel_register_algorithms(struct safexcel_crypto_priv *priv)

‎drivers/crypto/inside-secure/safexcel.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -857,5 +857,6 @@ extern struct safexcel_alg_template safexcel_alg_ccm;
857857
extern struct safexcel_alg_template safexcel_alg_crc32;
858858
extern struct safexcel_alg_template safexcel_alg_cbcmac;
859859
extern struct safexcel_alg_template safexcel_alg_xcbcmac;
860+
extern struct safexcel_alg_template safexcel_alg_cmac;
860861

861862
#endif

‎drivers/crypto/inside-secure/safexcel_hash.c‎

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2122,3 +2122,102 @@ struct safexcel_alg_template safexcel_alg_xcbcmac = {
21222122
},
21232123
},
21242124
};
2125+
2126+
static int safexcel_cmac_setkey(struct crypto_ahash *tfm, const u8 *key,
2127+
unsigned int len)
2128+
{
2129+
struct safexcel_ahash_ctx *ctx = crypto_tfm_ctx(crypto_ahash_tfm(tfm));
2130+
struct crypto_aes_ctx aes;
2131+
__be64 consts[4];
2132+
u64 _const[2];
2133+
u8 msb_mask, gfmask;
2134+
int ret, i;
2135+
2136+
ret = aes_expandkey(&aes, key, len);
2137+
if (ret) {
2138+
crypto_ahash_set_flags(tfm, CRYPTO_TFM_RES_BAD_KEY_LEN);
2139+
return ret;
2140+
}
2141+
2142+
for (i = 0; i < len / sizeof(u32); i++)
2143+
ctx->ipad[i + 8] = cpu_to_be32(aes.key_enc[i]);
2144+
2145+
/* precompute the CMAC key material */
2146+
crypto_cipher_clear_flags(ctx->kaes, CRYPTO_TFM_REQ_MASK);
2147+
crypto_cipher_set_flags(ctx->kaes, crypto_ahash_get_flags(tfm) &
2148+
CRYPTO_TFM_REQ_MASK);
2149+
ret = crypto_cipher_setkey(ctx->kaes, key, len);
2150+
crypto_ahash_set_flags(tfm, crypto_cipher_get_flags(ctx->kaes) &
2151+
CRYPTO_TFM_RES_MASK);
2152+
if (ret)
2153+
return ret;
2154+
2155+
/* code below borrowed from crypto/cmac.c */
2156+
/* encrypt the zero block */
2157+
memset(consts, 0, AES_BLOCK_SIZE);
2158+
crypto_cipher_encrypt_one(ctx->kaes, (u8 *)consts, (u8 *)consts);
2159+
2160+
gfmask = 0x87;
2161+
_const[0] = be64_to_cpu(consts[1]);
2162+
_const[1] = be64_to_cpu(consts[0]);
2163+
2164+
/* gf(2^128) multiply zero-ciphertext with u and u^2 */
2165+
for (i = 0; i < 4; i += 2) {
2166+
msb_mask = ((s64)_const[1] >> 63) & gfmask;
2167+
_const[1] = (_const[1] << 1) | (_const[0] >> 63);
2168+
_const[0] = (_const[0] << 1) ^ msb_mask;
2169+
2170+
consts[i + 0] = cpu_to_be64(_const[1]);
2171+
consts[i + 1] = cpu_to_be64(_const[0]);
2172+
}
2173+
/* end of code borrowed from crypto/cmac.c */
2174+
2175+
for (i = 0; i < 2 * AES_BLOCK_SIZE / sizeof(u32); i++)
2176+
ctx->ipad[i] = cpu_to_be32(((u32 *)consts)[i]);
2177+
2178+
if (len == AES_KEYSIZE_192) {
2179+
ctx->alg = CONTEXT_CONTROL_CRYPTO_ALG_XCBC192;
2180+
ctx->key_sz = AES_MAX_KEY_SIZE + 2 * AES_BLOCK_SIZE;
2181+
} else if (len == AES_KEYSIZE_256) {
2182+
ctx->alg = CONTEXT_CONTROL_CRYPTO_ALG_XCBC256;
2183+
ctx->key_sz = AES_MAX_KEY_SIZE + 2 * AES_BLOCK_SIZE;
2184+
} else {
2185+
ctx->alg = CONTEXT_CONTROL_CRYPTO_ALG_XCBC128;
2186+
ctx->key_sz = AES_MIN_KEY_SIZE + 2 * AES_BLOCK_SIZE;
2187+
}
2188+
ctx->cbcmac = false;
2189+
2190+
memzero_explicit(&aes, sizeof(aes));
2191+
return 0;
2192+
}
2193+
2194+
struct safexcel_alg_template safexcel_alg_cmac = {
2195+
.type = SAFEXCEL_ALG_TYPE_AHASH,
2196+
.algo_mask = 0,
2197+
.alg.ahash = {
2198+
.init = safexcel_cbcmac_init,
2199+
.update = safexcel_ahash_update,
2200+
.final = safexcel_ahash_final,
2201+
.finup = safexcel_ahash_finup,
2202+
.digest = safexcel_cbcmac_digest,
2203+
.setkey = safexcel_cmac_setkey,
2204+
.export = safexcel_ahash_export,
2205+
.import = safexcel_ahash_import,
2206+
.halg = {
2207+
.digestsize = AES_BLOCK_SIZE,
2208+
.statesize = sizeof(struct safexcel_ahash_export_state),
2209+
.base = {
2210+
.cra_name = "cmac(aes)",
2211+
.cra_driver_name = "safexcel-cmac-aes",
2212+
.cra_priority = SAFEXCEL_CRA_PRIORITY,
2213+
.cra_flags = CRYPTO_ALG_ASYNC |
2214+
CRYPTO_ALG_KERN_DRIVER_ONLY,
2215+
.cra_blocksize = AES_BLOCK_SIZE,
2216+
.cra_ctxsize = sizeof(struct safexcel_ahash_ctx),
2217+
.cra_init = safexcel_xcbcmac_cra_init,
2218+
.cra_exit = safexcel_xcbcmac_cra_exit,
2219+
.cra_module = THIS_MODULE,
2220+
},
2221+
},
2222+
},
2223+
};

0 commit comments

Comments
 (0)