2022-07-27 13:51:53 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* crypto/cryptosoft.c
|
|
|
|
* $OpenBSD: cryptosoft.c,v 1.71 2014/07/13 23:24:47 deraadt Exp $
|
2022-07-18 09:00:30 +02:00
|
|
|
* The author of this code is Angelos D. Keromytis (angelos@cis.upenn.edu)
|
|
|
|
*
|
|
|
|
* This code was written by Angelos D. Keromytis in Athens, Greece, in
|
|
|
|
* February 2000. Network Security Technologies Inc. (NSTI) kindly
|
|
|
|
* supported the development of this code.
|
|
|
|
*
|
|
|
|
* Copyright (c) 2000, 2001 Angelos D. Keromytis
|
|
|
|
*
|
|
|
|
* Permission to use, copy, and modify this software with or without fee
|
|
|
|
* is hereby granted, provided that this entire notice is included in
|
|
|
|
* all source code copies of any software which is or includes a copy or
|
|
|
|
* modification of this software.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR
|
|
|
|
* IMPLIED WARRANTY. IN PARTICULAR, NONE OF THE AUTHORS MAKES ANY
|
|
|
|
* REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE
|
|
|
|
* MERCHANTABILITY OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR
|
|
|
|
* PURPOSE.
|
2022-07-27 13:51:53 +02:00
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
2022-07-18 09:00:30 +02:00
|
|
|
|
2022-07-28 11:52:21 +02:00
|
|
|
#include <assert.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <endian.h>
|
|
|
|
#include <nuttx/kmalloc.h>
|
2022-07-18 09:00:30 +02:00
|
|
|
#include <crypto/cryptodev.h>
|
|
|
|
#include <crypto/cryptosoft.h>
|
|
|
|
#include <crypto/xform.h>
|
2023-02-01 14:41:12 +01:00
|
|
|
#include <sys/param.h>
|
2022-07-18 09:00:30 +02:00
|
|
|
|
2022-07-28 11:52:21 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Pre-processor Definitions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifndef howmany
|
|
|
|
# define howmany(x, y) (((x) + ((y) - 1)) / (y))
|
|
|
|
#endif
|
|
|
|
|
2022-07-27 13:51:53 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Private Data
|
|
|
|
****************************************************************************/
|
2022-07-18 09:00:30 +02:00
|
|
|
|
2022-07-27 13:51:53 +02:00
|
|
|
FAR struct swcr_data **swcr_sessions = NULL;
|
|
|
|
uint32_t swcr_sesnum = 0;
|
|
|
|
int swcr_id = -1;
|
2022-07-18 09:00:30 +02:00
|
|
|
|
2022-07-27 13:51:53 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/* Apply a symmetric encryption/decryption algorithm. */
|
|
|
|
|
2022-07-28 11:52:21 +02:00
|
|
|
int swcr_encdec(FAR struct cryptop *crp, FAR struct cryptodesc *crd,
|
|
|
|
FAR struct swcr_data *sw, caddr_t buf)
|
2022-07-18 09:00:30 +02:00
|
|
|
{
|
2022-07-27 13:51:53 +02:00
|
|
|
unsigned char blk[EALG_MAX_BLOCK_LEN];
|
2022-11-11 09:00:25 +01:00
|
|
|
FAR unsigned char *iv;
|
2022-07-27 13:51:53 +02:00
|
|
|
FAR unsigned char *ivp;
|
|
|
|
FAR unsigned char *nivp;
|
|
|
|
unsigned char iv2[EALG_MAX_BLOCK_LEN];
|
|
|
|
FAR const struct enc_xform *exf;
|
|
|
|
int i;
|
|
|
|
int j;
|
|
|
|
int blks;
|
|
|
|
int ivlen;
|
|
|
|
|
|
|
|
exf = sw->sw_exf;
|
|
|
|
blks = exf->blocksize;
|
|
|
|
ivlen = exf->ivsize;
|
|
|
|
|
|
|
|
/* Check for non-padded data */
|
|
|
|
|
|
|
|
if (crd->crd_len % blks)
|
|
|
|
{
|
2022-07-28 11:52:21 +02:00
|
|
|
return -EINVAL;
|
2022-07-27 13:51:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Initialize the IV */
|
|
|
|
|
|
|
|
if (crd->crd_flags & CRD_F_ENCRYPT)
|
|
|
|
{
|
|
|
|
/* Do we need to write the IV */
|
|
|
|
|
|
|
|
if (!(crd->crd_flags & CRD_F_IV_PRESENT))
|
|
|
|
{
|
2022-11-11 09:00:25 +01:00
|
|
|
arc4random_buf(crd->crd_iv, ivlen);
|
|
|
|
bcopy(crd->crd_iv, buf + crd->crd_inject, ivlen);
|
2022-07-27 13:51:53 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Decryption */
|
|
|
|
|
|
|
|
/* IV explicitly provided ? */
|
|
|
|
|
2022-11-11 09:00:25 +01:00
|
|
|
if (!(crd->crd_flags & CRD_F_IV_EXPLICIT))
|
2022-07-27 13:51:53 +02:00
|
|
|
{
|
|
|
|
/* Get IV off buf */
|
|
|
|
|
2022-11-11 09:00:25 +01:00
|
|
|
bcopy(crd->crd_iv, buf + crd->crd_inject, ivlen);
|
2022-07-27 13:51:53 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-11 09:00:25 +01:00
|
|
|
iv = crd->crd_iv;
|
2022-07-27 13:51:53 +02:00
|
|
|
ivp = iv;
|
|
|
|
|
|
|
|
/* xforms that provide a reinit method perform all IV
|
|
|
|
* handling themselves.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (exf->reinit)
|
|
|
|
{
|
|
|
|
exf->reinit((caddr_t)sw->sw_kschedule, iv);
|
|
|
|
}
|
|
|
|
|
2022-07-28 11:52:21 +02:00
|
|
|
i = crd->crd_len;
|
2022-07-27 13:51:53 +02:00
|
|
|
|
2022-11-11 09:00:25 +01:00
|
|
|
buf = buf + crd->crd_skip;
|
2022-07-28 11:52:21 +02:00
|
|
|
while (i > 0)
|
|
|
|
{
|
|
|
|
bcopy(buf, blk, exf->blocksize);
|
|
|
|
buf += exf->blocksize;
|
|
|
|
if (exf->reinit)
|
2022-07-27 13:51:53 +02:00
|
|
|
{
|
2022-07-28 11:52:21 +02:00
|
|
|
if (crd->crd_flags & CRD_F_ENCRYPT)
|
2022-07-27 13:51:53 +02:00
|
|
|
{
|
2022-07-28 11:52:21 +02:00
|
|
|
exf->encrypt((caddr_t)sw->sw_kschedule,
|
|
|
|
blk);
|
2022-07-27 13:51:53 +02:00
|
|
|
}
|
2022-07-28 11:52:21 +02:00
|
|
|
else
|
2022-07-27 13:51:53 +02:00
|
|
|
{
|
2022-07-28 11:52:21 +02:00
|
|
|
exf->decrypt((caddr_t)sw->sw_kschedule,
|
|
|
|
blk);
|
2022-07-27 13:51:53 +02:00
|
|
|
}
|
2022-07-28 11:52:21 +02:00
|
|
|
}
|
|
|
|
else if (crd->crd_flags & CRD_F_ENCRYPT)
|
|
|
|
{
|
|
|
|
/* XOR with previous block */
|
2022-07-27 13:51:53 +02:00
|
|
|
|
2022-07-28 11:52:21 +02:00
|
|
|
for (j = 0; j < blks; j++)
|
|
|
|
blk[j] ^= ivp[j];
|
2022-07-27 13:51:53 +02:00
|
|
|
|
2022-07-28 11:52:21 +02:00
|
|
|
exf->encrypt((caddr_t)sw->sw_kschedule, blk);
|
2022-07-27 13:51:53 +02:00
|
|
|
|
2022-07-28 11:52:21 +02:00
|
|
|
/* Keep encrypted block for XOR'ng
|
|
|
|
* with next block
|
2022-07-27 13:51:53 +02:00
|
|
|
*/
|
|
|
|
|
2022-07-28 11:52:21 +02:00
|
|
|
bcopy(blk, iv, blks);
|
|
|
|
ivp = iv;
|
2022-07-27 13:51:53 +02:00
|
|
|
}
|
2022-07-28 11:52:21 +02:00
|
|
|
else
|
2022-07-27 13:51:53 +02:00
|
|
|
{
|
2022-07-28 11:52:21 +02:00
|
|
|
/* decrypt */
|
2022-07-27 13:51:53 +02:00
|
|
|
|
2022-07-28 11:52:21 +02:00
|
|
|
/* Keep encrypted block for XOR'ing
|
|
|
|
* with next block
|
2022-07-27 13:51:53 +02:00
|
|
|
*/
|
|
|
|
|
2022-07-28 11:52:21 +02:00
|
|
|
nivp = (ivp == iv) ? iv2 : iv;
|
|
|
|
bcopy(blk, nivp, blks);
|
2022-07-27 13:51:53 +02:00
|
|
|
|
2022-07-28 11:52:21 +02:00
|
|
|
exf->decrypt((caddr_t)sw->sw_kschedule, blk);
|
2022-07-27 13:51:53 +02:00
|
|
|
|
2022-07-28 11:52:21 +02:00
|
|
|
/* XOR with previous block */
|
2022-07-27 13:51:53 +02:00
|
|
|
|
2022-07-28 11:52:21 +02:00
|
|
|
for (j = 0; j < blks; j++)
|
|
|
|
{
|
|
|
|
blk[j] ^= ivp[j];
|
2022-07-27 13:51:53 +02:00
|
|
|
}
|
|
|
|
|
2022-07-28 11:52:21 +02:00
|
|
|
ivp = nivp;
|
|
|
|
}
|
2022-07-27 13:51:53 +02:00
|
|
|
|
2022-07-28 11:52:21 +02:00
|
|
|
bcopy(blk, crp->crp_dst, exf->blocksize);
|
|
|
|
crp->crp_dst += exf->blocksize;
|
2022-07-27 13:51:53 +02:00
|
|
|
|
2022-07-28 11:52:21 +02:00
|
|
|
i -= blks;
|
2022-07-27 13:51:53 +02:00
|
|
|
|
2022-07-28 11:52:21 +02:00
|
|
|
/* Could be done... */
|
2022-07-27 13:51:53 +02:00
|
|
|
|
2022-07-28 11:52:21 +02:00
|
|
|
if (i == 0)
|
|
|
|
{
|
|
|
|
break;
|
2022-07-27 13:51:53 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0; /* Done with encryption/decryption */
|
2022-07-18 09:00:30 +02:00
|
|
|
}
|
|
|
|
|
2022-07-27 13:51:53 +02:00
|
|
|
/* Compute keyed-hash authenticator. */
|
|
|
|
|
|
|
|
int swcr_authcompute(FAR struct cryptop *crp,
|
|
|
|
FAR struct cryptodesc *crd,
|
|
|
|
FAR struct swcr_data *sw,
|
2022-07-28 11:52:21 +02:00
|
|
|
caddr_t buf)
|
2022-07-18 09:00:30 +02:00
|
|
|
{
|
2022-07-27 13:51:53 +02:00
|
|
|
unsigned char aalg[AALG_MAX_RESULT_LEN];
|
2022-11-11 09:00:25 +01:00
|
|
|
FAR const struct auth_hash *axf = sw->sw_axf;
|
2022-07-27 13:51:53 +02:00
|
|
|
int err;
|
|
|
|
|
|
|
|
if (sw->sw_ictx == 0)
|
|
|
|
{
|
2022-07-28 11:52:21 +02:00
|
|
|
return -EINVAL;
|
2022-07-27 13:51:53 +02:00
|
|
|
}
|
|
|
|
|
2022-11-11 09:00:25 +01:00
|
|
|
err = axf->update(&sw->sw_ctx, (FAR uint8_t *)buf + crd->crd_skip,
|
|
|
|
crd->crd_len);
|
2022-07-27 13:51:53 +02:00
|
|
|
|
|
|
|
if (err)
|
|
|
|
{
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (crd->crd_flags & CRD_F_ESN)
|
|
|
|
{
|
2022-11-11 09:00:25 +01:00
|
|
|
axf->update(&sw->sw_ctx, crd->crd_esn, 4);
|
2022-07-27 13:51:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
switch (sw->sw_alg)
|
|
|
|
{
|
|
|
|
case CRYPTO_MD5_HMAC:
|
|
|
|
case CRYPTO_SHA1_HMAC:
|
|
|
|
case CRYPTO_RIPEMD160_HMAC:
|
|
|
|
case CRYPTO_SHA2_256_HMAC:
|
|
|
|
case CRYPTO_SHA2_384_HMAC:
|
|
|
|
case CRYPTO_SHA2_512_HMAC:
|
|
|
|
if (sw->sw_octx == NULL)
|
|
|
|
{
|
2022-07-28 11:52:21 +02:00
|
|
|
return -EINVAL;
|
2022-07-27 13:51:53 +02:00
|
|
|
}
|
|
|
|
|
2022-11-11 09:00:25 +01:00
|
|
|
if (crd->crd_flags & CRD_F_UPDATE)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
axf->final(aalg, &sw->sw_ctx);
|
|
|
|
bcopy(sw->sw_octx, &sw->sw_ctx, axf->ctxsize);
|
|
|
|
axf->update(&sw->sw_ctx, aalg, axf->hashsize);
|
|
|
|
axf->final((FAR uint8_t *)crp->crp_mac, &sw->sw_ctx);
|
|
|
|
bcopy(sw->sw_ictx, &sw->sw_ctx, axf->ctxsize);
|
2022-07-27 13:51:53 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
2022-07-18 09:00:30 +02:00
|
|
|
}
|
|
|
|
|
2023-07-14 14:49:47 +02:00
|
|
|
int swcr_hash(FAR struct cryptop *crp,
|
|
|
|
FAR struct cryptodesc *crd,
|
|
|
|
FAR struct swcr_data *sw,
|
|
|
|
caddr_t buf)
|
|
|
|
{
|
|
|
|
FAR const struct auth_hash *axf = sw->sw_axf;
|
|
|
|
|
|
|
|
if (crd->crd_flags & CRD_F_UPDATE)
|
|
|
|
{
|
|
|
|
return axf->update(&sw->sw_ctx, (FAR uint8_t *)buf + crd->crd_skip,
|
|
|
|
crd->crd_len);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
axf->final((FAR uint8_t *)crp->crp_mac, &sw->sw_ctx);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-07-27 13:51:53 +02:00
|
|
|
/* Apply a combined encryption-authentication transformation */
|
|
|
|
|
|
|
|
int swcr_authenc(FAR struct cryptop *crp)
|
2022-07-18 09:00:30 +02:00
|
|
|
{
|
2022-07-27 13:51:53 +02:00
|
|
|
uint32_t blkbuf[howmany(EALG_MAX_BLOCK_LEN, sizeof(uint32_t))];
|
|
|
|
FAR u_char *blk = (u_char *)blkbuf;
|
|
|
|
u_char aalg[AALG_MAX_RESULT_LEN];
|
|
|
|
u_char iv[EALG_MAX_BLOCK_LEN];
|
|
|
|
union authctx ctx;
|
|
|
|
FAR struct cryptodesc *crd;
|
|
|
|
FAR struct cryptodesc *crda = NULL;
|
|
|
|
FAR struct cryptodesc *crde = NULL;
|
|
|
|
FAR struct swcr_data *sw;
|
|
|
|
FAR struct swcr_data *swa;
|
|
|
|
FAR struct swcr_data *swe = NULL;
|
|
|
|
FAR const struct auth_hash *axf = NULL;
|
|
|
|
FAR const struct enc_xform *exf = NULL;
|
|
|
|
caddr_t buf = (caddr_t)crp->crp_buf;
|
|
|
|
FAR uint32_t *blkp;
|
|
|
|
int blksz = 0;
|
|
|
|
int ivlen = 0;
|
|
|
|
int iskip = 0;
|
|
|
|
int oskip = 0;
|
|
|
|
int aadlen;
|
|
|
|
int len;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (crd = crp->crp_desc; crd; crd = crd->crd_next)
|
|
|
|
{
|
|
|
|
for (sw = swcr_sessions[crp->crp_sid & 0xffffffff];
|
|
|
|
sw && sw->sw_alg != crd->crd_alg;
|
|
|
|
sw = sw->sw_next);
|
|
|
|
|
|
|
|
if (sw == NULL)
|
|
|
|
{
|
2022-07-28 11:52:21 +02:00
|
|
|
return -EINVAL;
|
2022-07-27 13:51:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
switch (sw->sw_alg)
|
|
|
|
{
|
|
|
|
case CRYPTO_AES_GCM_16:
|
|
|
|
case CRYPTO_AES_GMAC:
|
|
|
|
case CRYPTO_CHACHA20_POLY1305:
|
|
|
|
swe = sw;
|
|
|
|
crde = crd;
|
|
|
|
exf = swe->sw_exf;
|
|
|
|
ivlen = exf->ivsize;
|
|
|
|
break;
|
|
|
|
case CRYPTO_AES_128_GMAC:
|
|
|
|
case CRYPTO_AES_192_GMAC:
|
|
|
|
case CRYPTO_AES_256_GMAC:
|
|
|
|
case CRYPTO_CHACHA20_POLY1305_MAC:
|
|
|
|
swa = sw;
|
|
|
|
crda = crd;
|
|
|
|
axf = swa->sw_axf;
|
|
|
|
if (swa->sw_ictx == 0)
|
|
|
|
{
|
2022-07-28 11:52:21 +02:00
|
|
|
return -EINVAL;
|
2022-07-27 13:51:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bcopy(swa->sw_ictx, &ctx, axf->ctxsize);
|
|
|
|
blksz = axf->blocksize;
|
|
|
|
break;
|
|
|
|
default:
|
2022-07-28 11:52:21 +02:00
|
|
|
return -EINVAL;
|
2022-07-27 13:51:53 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (crde == NULL || crda == NULL)
|
|
|
|
{
|
2022-07-28 11:52:21 +02:00
|
|
|
return -EINVAL;
|
2022-07-27 13:51:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Initialize the IV */
|
|
|
|
|
|
|
|
if (crde->crd_flags & CRD_F_ENCRYPT)
|
|
|
|
{
|
|
|
|
/* IV explicitly provided ? */
|
|
|
|
|
|
|
|
if (crde->crd_flags & CRD_F_IV_EXPLICIT)
|
|
|
|
{
|
|
|
|
bcopy(crde->crd_iv, iv, ivlen);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
arc4random_buf(iv, ivlen);
|
|
|
|
}
|
|
|
|
|
2022-07-28 11:52:21 +02:00
|
|
|
if (!((crde->crd_flags) & CRD_F_IV_PRESENT))
|
2022-07-27 13:51:53 +02:00
|
|
|
{
|
2022-07-28 11:52:21 +02:00
|
|
|
bcopy(iv, buf + crde->crd_inject, ivlen);
|
2022-07-27 13:51:53 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Decryption */
|
|
|
|
|
|
|
|
/* IV explicitly provided ? */
|
|
|
|
|
|
|
|
if (crde->crd_flags & CRD_F_IV_EXPLICIT)
|
|
|
|
{
|
|
|
|
bcopy(crde->crd_iv, iv, ivlen);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Get IV off buf */
|
|
|
|
|
2022-07-28 11:52:21 +02:00
|
|
|
bcopy(iv, buf + crde->crd_inject, ivlen);
|
2022-07-27 13:51:53 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Supply MAC with IV */
|
|
|
|
|
|
|
|
if (axf->reinit)
|
|
|
|
{
|
|
|
|
axf->reinit(&ctx, iv, ivlen);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Supply MAC with AAD */
|
|
|
|
|
|
|
|
aadlen = crda->crd_len;
|
|
|
|
/* Section 5 of RFC 4106 specifies that AAD construction consists of
|
|
|
|
* {SPI, ESN, SN} whereas the real packet contains only {SPI, SN}.
|
|
|
|
* Unfortunately it doesn't follow a good example set in the Section
|
|
|
|
* 3.3.2.1 of RFC 4303 where upper part of the ESN, located in the
|
|
|
|
* external (to the packet) memory buffer, is processed by the hash
|
|
|
|
* function in the end thus allowing to retain simple programming
|
|
|
|
* interfaces and avoid kludges like the one below.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (crda->crd_flags & CRD_F_ESN)
|
|
|
|
{
|
|
|
|
aadlen += 4;
|
|
|
|
|
|
|
|
/* SPI */
|
|
|
|
|
2022-07-28 11:52:21 +02:00
|
|
|
bcopy(buf + crda->crd_skip, blk, 4);
|
2022-07-27 13:51:53 +02:00
|
|
|
iskip = 4; /* loop below will start with an offset of 4 */
|
|
|
|
|
|
|
|
/* ESN */
|
|
|
|
|
|
|
|
bcopy(crda->crd_esn, blk + 4, 4);
|
|
|
|
oskip = iskip + 4; /* offset output buffer blk by 8 */
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = iskip; i < crda->crd_len; i += axf->hashsize)
|
|
|
|
{
|
|
|
|
len = MIN(crda->crd_len - i, axf->hashsize - oskip);
|
2022-07-28 11:52:21 +02:00
|
|
|
bcopy(buf + crda->crd_skip + i, blk + oskip, len);
|
2022-07-27 13:51:53 +02:00
|
|
|
bzero(blk + len + oskip, axf->hashsize - len - oskip);
|
|
|
|
axf->update(&ctx, blk, axf->hashsize);
|
|
|
|
oskip = 0; /* reset initial output offset */
|
|
|
|
}
|
|
|
|
|
|
|
|
if (exf->reinit)
|
|
|
|
{
|
|
|
|
exf->reinit((caddr_t)swe->sw_kschedule, iv);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Do encryption/decryption with MAC */
|
|
|
|
|
|
|
|
for (i = 0; i < crde->crd_len; i += blksz)
|
|
|
|
{
|
|
|
|
len = MIN(crde->crd_len - i, blksz);
|
|
|
|
if (len < blksz)
|
|
|
|
{
|
|
|
|
bzero(blk, blksz);
|
|
|
|
}
|
|
|
|
|
2022-07-28 11:52:21 +02:00
|
|
|
bcopy(buf + i, blk, len);
|
2022-07-27 13:51:53 +02:00
|
|
|
if (crde->crd_flags & CRD_F_ENCRYPT)
|
|
|
|
{
|
|
|
|
exf->encrypt((caddr_t)swe->sw_kschedule, blk);
|
|
|
|
axf->update(&ctx, blk, len);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
axf->update(&ctx, blk, len);
|
|
|
|
exf->decrypt((caddr_t)swe->sw_kschedule, blk);
|
|
|
|
}
|
|
|
|
|
2022-07-28 11:52:21 +02:00
|
|
|
bcopy(blk, crp->crp_dst + i, len);
|
2022-07-27 13:51:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Do any required special finalization */
|
|
|
|
|
|
|
|
switch (crda->crd_alg)
|
|
|
|
{
|
|
|
|
case CRYPTO_AES_128_GMAC:
|
|
|
|
case CRYPTO_AES_192_GMAC:
|
|
|
|
case CRYPTO_AES_256_GMAC:
|
|
|
|
|
|
|
|
/* length block */
|
|
|
|
|
|
|
|
bzero(blk, axf->hashsize);
|
|
|
|
blkp = (uint32_t *)blk + 1;
|
|
|
|
*blkp = htobe32(aadlen * 8);
|
|
|
|
blkp = (uint32_t *)blk + 3;
|
|
|
|
*blkp = htobe32(crde->crd_len * 8);
|
|
|
|
axf->update(&ctx, blk, axf->hashsize);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case CRYPTO_CHACHA20_POLY1305_MAC:
|
|
|
|
|
|
|
|
/* length block */
|
|
|
|
|
|
|
|
bzero(blk, axf->hashsize);
|
|
|
|
blkp = (uint32_t *)blk;
|
|
|
|
*blkp = htole32(aadlen);
|
|
|
|
blkp = (uint32_t *)blk + 2;
|
|
|
|
*blkp = htole32(crde->crd_len);
|
|
|
|
axf->update(&ctx, blk, axf->hashsize);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Finalize MAC */
|
|
|
|
|
|
|
|
axf->final(aalg, &ctx);
|
|
|
|
|
|
|
|
/* Inject the authentication data */
|
|
|
|
|
2022-07-28 11:52:21 +02:00
|
|
|
bcopy(aalg, crp->crp_mac, axf->authsize);
|
2022-07-27 13:51:53 +02:00
|
|
|
|
|
|
|
return 0;
|
2022-07-18 09:00:30 +02:00
|
|
|
}
|
|
|
|
|
2022-07-27 13:51:53 +02:00
|
|
|
/* Apply a compression/decompression algorithm */
|
|
|
|
|
|
|
|
int swcr_compdec(FAR struct cryptodesc *crd, FAR struct swcr_data *sw,
|
|
|
|
caddr_t buf, int outtype)
|
2022-07-18 09:00:30 +02:00
|
|
|
{
|
2022-07-27 13:51:53 +02:00
|
|
|
FAR uint8_t *data;
|
|
|
|
FAR uint8_t *out;
|
|
|
|
FAR const struct comp_algo *cxf;
|
|
|
|
uint32_t result;
|
|
|
|
|
|
|
|
cxf = sw->sw_cxf;
|
|
|
|
|
|
|
|
/* We must handle the whole buffer of data in one time
|
|
|
|
* then if there is not all the data in the mbuf, we must
|
|
|
|
* copy in a buffer.
|
|
|
|
*/
|
|
|
|
|
2022-07-28 11:52:21 +02:00
|
|
|
data = kmm_malloc(crd->crd_len);
|
2022-07-27 13:51:53 +02:00
|
|
|
if (data == NULL)
|
|
|
|
{
|
2022-07-28 11:52:21 +02:00
|
|
|
return -EINVAL;
|
2022-07-27 13:51:53 +02:00
|
|
|
}
|
|
|
|
|
2022-07-28 11:52:21 +02:00
|
|
|
bcopy(buf + crd->crd_skip, data, crd->crd_len);
|
|
|
|
|
2022-07-27 13:51:53 +02:00
|
|
|
if (crd->crd_flags & CRD_F_COMP)
|
|
|
|
{
|
|
|
|
result = cxf->compress(data, crd->crd_len, &out);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
result = cxf->decompress(data, crd->crd_len, &out);
|
|
|
|
}
|
|
|
|
|
2022-07-28 11:52:21 +02:00
|
|
|
kmm_free(data);
|
2022-07-27 13:51:53 +02:00
|
|
|
if (result == 0)
|
|
|
|
{
|
2022-07-28 11:52:21 +02:00
|
|
|
return -EINVAL;
|
2022-07-27 13:51:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
sw->sw_size = result;
|
|
|
|
|
|
|
|
/* Check the compressed size when doing compression */
|
|
|
|
|
|
|
|
if (crd->crd_flags & CRD_F_COMP)
|
|
|
|
{
|
|
|
|
if (result > crd->crd_len)
|
|
|
|
{
|
|
|
|
/* Compression was useless, we lost time */
|
|
|
|
|
2022-07-28 11:52:21 +02:00
|
|
|
kmm_free(out);
|
2022-07-27 13:51:53 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-28 11:52:21 +02:00
|
|
|
bcopy(out, buf + crd->crd_skip, result);
|
|
|
|
kmm_free(out);
|
2022-07-27 13:51:53 +02:00
|
|
|
return 0;
|
2022-07-18 09:00:30 +02:00
|
|
|
}
|
|
|
|
|
2022-07-27 13:51:53 +02:00
|
|
|
/* Generate a new software session. */
|
|
|
|
|
|
|
|
int swcr_newsession(FAR uint32_t *sid, FAR struct cryptoini *cri)
|
2022-07-18 09:00:30 +02:00
|
|
|
{
|
2022-07-27 13:51:53 +02:00
|
|
|
FAR struct swcr_data **swd;
|
|
|
|
FAR const struct auth_hash *axf;
|
|
|
|
FAR const struct enc_xform *txf;
|
|
|
|
uint32_t i;
|
|
|
|
int k;
|
|
|
|
|
|
|
|
if (sid == NULL || cri == NULL)
|
|
|
|
{
|
2022-07-28 11:52:21 +02:00
|
|
|
return -EINVAL;
|
2022-07-27 13:51:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (swcr_sessions)
|
|
|
|
{
|
|
|
|
for (i = 1; i < swcr_sesnum; i++)
|
|
|
|
{
|
|
|
|
if (swcr_sessions[i] == NULL)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (swcr_sessions == NULL || i == swcr_sesnum)
|
|
|
|
{
|
|
|
|
if (swcr_sessions == NULL)
|
|
|
|
{
|
|
|
|
i = 1; /* We leave swcr_sessions[0] empty */
|
|
|
|
swcr_sesnum = CRYPTO_SW_SESSIONS;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
swcr_sesnum *= 2;
|
|
|
|
}
|
|
|
|
|
2022-07-28 11:52:21 +02:00
|
|
|
swd = kmm_calloc(swcr_sesnum, sizeof(struct swcr_data *));
|
2022-07-27 13:51:53 +02:00
|
|
|
if (swd == NULL)
|
|
|
|
{
|
|
|
|
/* Reset session number */
|
|
|
|
|
|
|
|
if (swcr_sesnum == CRYPTO_SW_SESSIONS)
|
|
|
|
{
|
|
|
|
swcr_sesnum = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
swcr_sesnum /= 2;
|
|
|
|
}
|
|
|
|
|
2022-07-28 11:52:21 +02:00
|
|
|
return -ENOBUFS;
|
2022-07-27 13:51:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Copy existing sessions */
|
|
|
|
|
|
|
|
if (swcr_sessions)
|
|
|
|
{
|
|
|
|
bcopy(swcr_sessions, swd,
|
|
|
|
(swcr_sesnum / 2) * sizeof(struct swcr_data *));
|
2022-07-28 11:52:21 +02:00
|
|
|
kmm_free(swcr_sessions);
|
2022-07-27 13:51:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
swcr_sessions = swd;
|
|
|
|
}
|
|
|
|
|
|
|
|
swd = &swcr_sessions[i];
|
|
|
|
*sid = i;
|
|
|
|
|
|
|
|
while (cri)
|
|
|
|
{
|
2022-07-28 11:52:21 +02:00
|
|
|
*swd = kmm_zalloc(sizeof(struct swcr_data));
|
2022-07-27 13:51:53 +02:00
|
|
|
if (*swd == NULL)
|
|
|
|
{
|
|
|
|
swcr_freesession(i);
|
2022-07-28 11:52:21 +02:00
|
|
|
return -ENOBUFS;
|
2022-07-27 13:51:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
switch (cri->cri_alg)
|
|
|
|
{
|
|
|
|
case CRYPTO_3DES_CBC:
|
|
|
|
txf = &enc_xform_3des;
|
|
|
|
goto enccommon;
|
|
|
|
case CRYPTO_BLF_CBC:
|
|
|
|
txf = &enc_xform_blf;
|
|
|
|
goto enccommon;
|
|
|
|
case CRYPTO_CAST_CBC:
|
|
|
|
txf = &enc_xform_cast5;
|
|
|
|
goto enccommon;
|
|
|
|
case CRYPTO_AES_CBC:
|
|
|
|
txf = &enc_xform_aes;
|
|
|
|
goto enccommon;
|
|
|
|
case CRYPTO_AES_CTR:
|
|
|
|
txf = &enc_xform_aes_ctr;
|
|
|
|
goto enccommon;
|
|
|
|
case CRYPTO_AES_XTS:
|
|
|
|
txf = &enc_xform_aes_xts;
|
|
|
|
goto enccommon;
|
|
|
|
case CRYPTO_AES_GCM_16:
|
|
|
|
txf = &enc_xform_aes_gcm;
|
|
|
|
goto enccommon;
|
|
|
|
case CRYPTO_AES_GMAC:
|
|
|
|
txf = &enc_xform_aes_gmac;
|
|
|
|
(*swd)->sw_exf = txf;
|
|
|
|
break;
|
|
|
|
case CRYPTO_CHACHA20_POLY1305:
|
|
|
|
txf = &enc_xform_chacha20_poly1305;
|
|
|
|
goto enccommon;
|
|
|
|
case CRYPTO_NULL:
|
|
|
|
txf = &enc_xform_null;
|
|
|
|
goto enccommon;
|
|
|
|
enccommon:
|
|
|
|
if (txf->ctxsize > 0)
|
|
|
|
{
|
2022-07-28 11:52:21 +02:00
|
|
|
(*swd)->sw_kschedule = kmm_zalloc(txf->ctxsize);
|
2022-07-27 13:51:53 +02:00
|
|
|
if ((*swd)->sw_kschedule == NULL)
|
|
|
|
{
|
|
|
|
swcr_freesession(i);
|
2022-07-28 11:52:21 +02:00
|
|
|
return -EINVAL;
|
2022-07-27 13:51:53 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (txf->setkey((*swd)->sw_kschedule,
|
|
|
|
(FAR uint8_t *)cri->cri_key,
|
|
|
|
cri->cri_klen / 8) < 0)
|
|
|
|
{
|
|
|
|
swcr_freesession(i);
|
2022-07-28 11:52:21 +02:00
|
|
|
return -EINVAL;
|
2022-07-27 13:51:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
(*swd)->sw_exf = txf;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case CRYPTO_MD5_HMAC:
|
|
|
|
axf = &auth_hash_hmac_md5_96;
|
|
|
|
goto authcommon;
|
|
|
|
case CRYPTO_SHA1_HMAC:
|
|
|
|
axf = &auth_hash_hmac_sha1_96;
|
|
|
|
goto authcommon;
|
|
|
|
case CRYPTO_RIPEMD160_HMAC:
|
|
|
|
axf = &auth_hash_hmac_ripemd_160_96;
|
|
|
|
goto authcommon;
|
|
|
|
case CRYPTO_SHA2_256_HMAC:
|
|
|
|
axf = &auth_hash_hmac_sha2_256_128;
|
|
|
|
goto authcommon;
|
|
|
|
case CRYPTO_SHA2_384_HMAC:
|
|
|
|
axf = &auth_hash_hmac_sha2_384_192;
|
|
|
|
goto authcommon;
|
|
|
|
case CRYPTO_SHA2_512_HMAC:
|
|
|
|
axf = &auth_hash_hmac_sha2_512_256;
|
|
|
|
authcommon:
|
2022-07-28 11:52:21 +02:00
|
|
|
(*swd)->sw_ictx = kmm_malloc(axf->ctxsize);
|
2022-07-27 13:51:53 +02:00
|
|
|
if ((*swd)->sw_ictx == NULL)
|
|
|
|
{
|
|
|
|
swcr_freesession(i);
|
2022-07-28 11:52:21 +02:00
|
|
|
return -ENOBUFS;
|
2022-07-27 13:51:53 +02:00
|
|
|
}
|
|
|
|
|
2022-07-28 11:52:21 +02:00
|
|
|
(*swd)->sw_octx = kmm_malloc(axf->ctxsize);
|
2022-07-27 13:51:53 +02:00
|
|
|
if ((*swd)->sw_octx == NULL)
|
|
|
|
{
|
|
|
|
swcr_freesession(i);
|
2022-07-28 11:52:21 +02:00
|
|
|
return -ENOBUFS;
|
2022-07-27 13:51:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for (k = 0; k < cri->cri_klen / 8; k++)
|
|
|
|
{
|
|
|
|
cri->cri_key[k] ^= HMAC_IPAD_VAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
axf->init((*swd)->sw_ictx);
|
|
|
|
axf->update((*swd)->sw_ictx, (FAR uint8_t *)cri->cri_key,
|
|
|
|
cri->cri_klen / 8);
|
|
|
|
axf->update((*swd)->sw_ictx, hmac_ipad_buffer,
|
|
|
|
axf->blocksize - (cri->cri_klen / 8));
|
|
|
|
|
|
|
|
for (k = 0; k < cri->cri_klen / 8; k++)
|
|
|
|
{
|
|
|
|
cri->cri_key[k] ^= (HMAC_IPAD_VAL ^ HMAC_OPAD_VAL);
|
|
|
|
}
|
|
|
|
|
|
|
|
axf->init((*swd)->sw_octx);
|
|
|
|
axf->update((*swd)->sw_octx, (FAR uint8_t *)cri->cri_key,
|
|
|
|
cri->cri_klen / 8);
|
|
|
|
axf->update((*swd)->sw_octx, hmac_opad_buffer,
|
|
|
|
axf->blocksize - (cri->cri_klen / 8));
|
|
|
|
|
|
|
|
for (k = 0; k < cri->cri_klen / 8; k++)
|
|
|
|
{
|
|
|
|
cri->cri_key[k] ^= HMAC_OPAD_VAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
(*swd)->sw_axf = axf;
|
2022-11-11 09:00:25 +01:00
|
|
|
bcopy((*swd)->sw_ictx, &(*swd)->sw_ctx, axf->ctxsize);
|
2022-07-27 13:51:53 +02:00
|
|
|
break;
|
|
|
|
|
2023-07-14 14:49:47 +02:00
|
|
|
case CRYPTO_MD5:
|
|
|
|
axf = &auth_hash_md5;
|
|
|
|
goto auth3common;
|
|
|
|
case CRYPTO_SHA1:
|
|
|
|
axf = &auth_hash_sha1;
|
|
|
|
goto auth3common;
|
2023-08-08 08:02:52 +02:00
|
|
|
case CRYPTO_SHA2_224:
|
|
|
|
axf = &auth_hash_sha2_224;
|
|
|
|
goto auth3common;
|
2023-07-14 14:49:47 +02:00
|
|
|
case CRYPTO_SHA2_256:
|
|
|
|
axf = &auth_hash_sha2_256;
|
|
|
|
goto auth3common;
|
2023-08-08 08:02:52 +02:00
|
|
|
case CRYPTO_SHA2_384:
|
|
|
|
axf = &auth_hash_sha2_384;
|
|
|
|
goto auth3common;
|
2023-07-14 14:49:47 +02:00
|
|
|
case CRYPTO_SHA2_512:
|
|
|
|
axf = &auth_hash_sha2_512;
|
|
|
|
|
|
|
|
auth3common:
|
|
|
|
(*swd)->sw_ictx = kmm_zalloc(axf->ctxsize);
|
|
|
|
if ((*swd)->sw_ictx == NULL)
|
|
|
|
{
|
|
|
|
swcr_freesession(i);
|
|
|
|
return -ENOBUFS;
|
|
|
|
}
|
|
|
|
|
|
|
|
axf->init((*swd)->sw_ictx);
|
|
|
|
(*swd)->sw_axf = axf;
|
|
|
|
bcopy((*swd)->sw_ictx, &(*swd)->sw_ctx, axf->ctxsize);
|
|
|
|
break;
|
|
|
|
|
2022-07-27 13:51:53 +02:00
|
|
|
case CRYPTO_AES_128_GMAC:
|
|
|
|
axf = &auth_hash_gmac_aes_128;
|
|
|
|
goto auth4common;
|
|
|
|
|
|
|
|
case CRYPTO_AES_192_GMAC:
|
|
|
|
axf = &auth_hash_gmac_aes_192;
|
|
|
|
goto auth4common;
|
|
|
|
|
|
|
|
case CRYPTO_AES_256_GMAC:
|
|
|
|
axf = &auth_hash_gmac_aes_256;
|
|
|
|
goto auth4common;
|
|
|
|
|
|
|
|
case CRYPTO_CHACHA20_POLY1305_MAC:
|
|
|
|
axf = &auth_hash_chacha20_poly1305;
|
|
|
|
|
|
|
|
auth4common:
|
2022-07-28 11:52:21 +02:00
|
|
|
(*swd)->sw_ictx = kmm_malloc(axf->ctxsize);
|
2022-07-27 13:51:53 +02:00
|
|
|
if ((*swd)->sw_ictx == NULL)
|
|
|
|
{
|
|
|
|
swcr_freesession(i);
|
2022-07-28 11:52:21 +02:00
|
|
|
return -ENOBUFS;
|
2022-07-27 13:51:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
axf->init((*swd)->sw_ictx);
|
|
|
|
axf->setkey((*swd)->sw_ictx, (FAR uint8_t *)cri->cri_key,
|
|
|
|
cri->cri_klen / 8);
|
|
|
|
(*swd)->sw_axf = axf;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case CRYPTO_ESN:
|
|
|
|
|
|
|
|
/* nothing to do */
|
|
|
|
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
swcr_freesession(i);
|
2022-07-28 11:52:21 +02:00
|
|
|
return -EINVAL;
|
2022-07-27 13:51:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
(*swd)->sw_alg = cri->cri_alg;
|
|
|
|
cri = cri->cri_next;
|
|
|
|
swd = &((*swd)->sw_next);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
2022-07-18 09:00:30 +02:00
|
|
|
}
|
|
|
|
|
2022-07-27 13:51:53 +02:00
|
|
|
/* Free a session. */
|
|
|
|
|
|
|
|
int swcr_freesession(uint64_t tid)
|
2022-07-18 09:00:30 +02:00
|
|
|
{
|
2022-07-27 13:51:53 +02:00
|
|
|
FAR struct swcr_data *swd;
|
|
|
|
FAR const struct enc_xform *txf;
|
|
|
|
FAR const struct auth_hash *axf;
|
|
|
|
uint32_t sid = ((uint32_t) tid) & 0xffffffff;
|
|
|
|
|
|
|
|
if (sid > swcr_sesnum || swcr_sessions == NULL ||
|
|
|
|
swcr_sessions[sid] == NULL)
|
|
|
|
{
|
2022-07-28 11:52:21 +02:00
|
|
|
return -EINVAL;
|
2022-07-27 13:51:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Silently accept and return */
|
|
|
|
|
|
|
|
if (sid == 0)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
while ((swd = swcr_sessions[sid]) != NULL)
|
|
|
|
{
|
|
|
|
swcr_sessions[sid] = swd->sw_next;
|
|
|
|
|
|
|
|
switch (swd->sw_alg)
|
|
|
|
{
|
|
|
|
case CRYPTO_3DES_CBC:
|
|
|
|
case CRYPTO_BLF_CBC:
|
|
|
|
case CRYPTO_CAST_CBC:
|
|
|
|
case CRYPTO_RIJNDAEL128_CBC:
|
|
|
|
case CRYPTO_AES_CTR:
|
|
|
|
case CRYPTO_AES_XTS:
|
|
|
|
case CRYPTO_AES_GCM_16:
|
|
|
|
case CRYPTO_AES_GMAC:
|
|
|
|
case CRYPTO_CHACHA20_POLY1305:
|
|
|
|
case CRYPTO_NULL:
|
|
|
|
txf = swd->sw_exf;
|
|
|
|
|
|
|
|
if (swd->sw_kschedule)
|
|
|
|
{
|
|
|
|
explicit_bzero(swd->sw_kschedule, txf->ctxsize);
|
2022-07-28 11:52:21 +02:00
|
|
|
kmm_free(swd->sw_kschedule);
|
2022-07-27 13:51:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case CRYPTO_MD5_HMAC:
|
|
|
|
case CRYPTO_SHA1_HMAC:
|
|
|
|
case CRYPTO_RIPEMD160_HMAC:
|
|
|
|
case CRYPTO_SHA2_256_HMAC:
|
|
|
|
case CRYPTO_SHA2_384_HMAC:
|
|
|
|
case CRYPTO_SHA2_512_HMAC:
|
|
|
|
axf = swd->sw_axf;
|
|
|
|
|
|
|
|
if (swd->sw_ictx)
|
|
|
|
{
|
|
|
|
explicit_bzero(swd->sw_ictx, axf->ctxsize);
|
2022-07-28 11:52:21 +02:00
|
|
|
kmm_free(swd->sw_ictx);
|
2022-07-27 13:51:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (swd->sw_octx)
|
|
|
|
{
|
|
|
|
explicit_bzero(swd->sw_octx, axf->ctxsize);
|
2022-07-28 11:52:21 +02:00
|
|
|
kmm_free(swd->sw_octx);
|
2022-07-27 13:51:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case CRYPTO_AES_128_GMAC:
|
|
|
|
case CRYPTO_AES_192_GMAC:
|
|
|
|
case CRYPTO_AES_256_GMAC:
|
|
|
|
case CRYPTO_CHACHA20_POLY1305_MAC:
|
2023-07-14 14:49:47 +02:00
|
|
|
case CRYPTO_MD5:
|
|
|
|
case CRYPTO_SHA1:
|
2023-08-08 08:02:52 +02:00
|
|
|
case CRYPTO_SHA2_224:
|
2023-07-14 14:49:47 +02:00
|
|
|
case CRYPTO_SHA2_256:
|
2023-08-08 08:02:52 +02:00
|
|
|
case CRYPTO_SHA2_384:
|
2023-07-14 14:49:47 +02:00
|
|
|
case CRYPTO_SHA2_512:
|
2022-07-27 13:51:53 +02:00
|
|
|
axf = swd->sw_axf;
|
|
|
|
|
|
|
|
if (swd->sw_ictx)
|
|
|
|
{
|
|
|
|
explicit_bzero(swd->sw_ictx, axf->ctxsize);
|
2022-07-28 11:52:21 +02:00
|
|
|
kmm_free(swd->sw_ictx);
|
2022-07-27 13:51:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2022-07-28 11:52:21 +02:00
|
|
|
kmm_free(swd);
|
2022-07-27 13:51:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
2022-07-18 09:00:30 +02:00
|
|
|
}
|
|
|
|
|
2022-07-27 13:51:53 +02:00
|
|
|
/* Process a software request. */
|
|
|
|
|
|
|
|
int swcr_process(struct cryptop *crp)
|
2022-07-18 09:00:30 +02:00
|
|
|
{
|
2022-07-27 13:51:53 +02:00
|
|
|
FAR struct cryptodesc *crd;
|
|
|
|
FAR struct swcr_data *sw;
|
|
|
|
uint32_t lid;
|
|
|
|
|
|
|
|
/* Sanity check */
|
|
|
|
|
|
|
|
if (crp == NULL)
|
|
|
|
{
|
2022-07-28 11:52:21 +02:00
|
|
|
return -EINVAL;
|
2022-07-27 13:51:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (crp->crp_desc == NULL || crp->crp_buf == NULL)
|
|
|
|
{
|
2022-07-28 11:52:21 +02:00
|
|
|
crp->crp_etype = -EINVAL;
|
2022-07-27 13:51:53 +02:00
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
lid = crp->crp_sid & 0xffffffff;
|
|
|
|
if (lid >= swcr_sesnum || lid == 0 || swcr_sessions[lid] == NULL)
|
|
|
|
{
|
2022-07-28 11:52:21 +02:00
|
|
|
crp->crp_etype = -ENOENT;
|
2022-07-27 13:51:53 +02:00
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Go through crypto descriptors, processing as we go */
|
|
|
|
|
|
|
|
for (crd = crp->crp_desc; crd; crd = crd->crd_next)
|
|
|
|
{
|
|
|
|
/* Find the crypto context.
|
|
|
|
* XXX Note that the logic here prevents us from having
|
|
|
|
* XXX the same algorithm multiple times in a session
|
|
|
|
* XXX (or rather, we can but it won't give us the right
|
|
|
|
* XXX results). To do that, we'd need some way of differentiating
|
|
|
|
* XXX between the various instances of an algorithm (so we can
|
|
|
|
* XXX locate the correct crypto context).
|
|
|
|
*/
|
|
|
|
|
|
|
|
for (sw = swcr_sessions[lid];
|
|
|
|
sw && sw->sw_alg != crd->crd_alg;
|
|
|
|
sw = sw->sw_next);
|
|
|
|
|
|
|
|
/* No such context ? */
|
|
|
|
|
|
|
|
if (sw == NULL)
|
|
|
|
{
|
2022-07-28 11:52:21 +02:00
|
|
|
crp->crp_etype = -EINVAL;
|
2022-07-27 13:51:53 +02:00
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (sw->sw_alg)
|
|
|
|
{
|
|
|
|
case CRYPTO_NULL:
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case CRYPTO_3DES_CBC:
|
|
|
|
case CRYPTO_BLF_CBC:
|
|
|
|
case CRYPTO_CAST_CBC:
|
|
|
|
case CRYPTO_RIJNDAEL128_CBC:
|
|
|
|
case CRYPTO_AES_CTR:
|
|
|
|
case CRYPTO_AES_XTS:
|
2022-07-28 11:52:21 +02:00
|
|
|
if ((crp->crp_etype = swcr_encdec(crp, crd, sw,
|
|
|
|
crp->crp_buf)) != 0)
|
2022-07-27 13:51:53 +02:00
|
|
|
{
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
case CRYPTO_MD5_HMAC:
|
|
|
|
case CRYPTO_SHA1_HMAC:
|
|
|
|
case CRYPTO_RIPEMD160_HMAC:
|
|
|
|
case CRYPTO_SHA2_256_HMAC:
|
|
|
|
case CRYPTO_SHA2_384_HMAC:
|
|
|
|
case CRYPTO_SHA2_512_HMAC:
|
|
|
|
if ((crp->crp_etype = swcr_authcompute(crp, crd, sw,
|
2022-07-28 11:52:21 +02:00
|
|
|
crp->crp_buf)) != 0)
|
2022-07-27 13:51:53 +02:00
|
|
|
{
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
2023-07-14 14:49:47 +02:00
|
|
|
case CRYPTO_MD5:
|
|
|
|
case CRYPTO_SHA1:
|
2023-08-08 08:02:52 +02:00
|
|
|
case CRYPTO_SHA2_224:
|
2023-07-14 14:49:47 +02:00
|
|
|
case CRYPTO_SHA2_256:
|
2023-08-08 08:02:52 +02:00
|
|
|
case CRYPTO_SHA2_384:
|
2023-07-14 14:49:47 +02:00
|
|
|
case CRYPTO_SHA2_512:
|
|
|
|
if ((crp->crp_etype = swcr_hash(crp, crd, sw,
|
|
|
|
crp->crp_buf)) != 0)
|
|
|
|
{
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
2022-07-27 13:51:53 +02:00
|
|
|
case CRYPTO_AES_GCM_16:
|
|
|
|
case CRYPTO_AES_GMAC:
|
|
|
|
case CRYPTO_AES_128_GMAC:
|
|
|
|
case CRYPTO_AES_192_GMAC:
|
|
|
|
case CRYPTO_AES_256_GMAC:
|
|
|
|
case CRYPTO_CHACHA20_POLY1305:
|
|
|
|
case CRYPTO_CHACHA20_POLY1305_MAC:
|
|
|
|
crp->crp_etype = swcr_authenc(crp);
|
|
|
|
goto done;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
|
|
|
/* Unknown/unsupported algorithm */
|
|
|
|
|
2022-07-28 11:52:21 +02:00
|
|
|
crp->crp_etype = -EINVAL;
|
2022-07-27 13:51:53 +02:00
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
}
|
2022-07-18 09:00:30 +02:00
|
|
|
|
|
|
|
done:
|
2022-07-27 13:51:53 +02:00
|
|
|
return 0;
|
2022-07-18 09:00:30 +02:00
|
|
|
}
|
|
|
|
|
2022-07-27 13:51:53 +02:00
|
|
|
/* Initialize the driver, called from the kernel main(). */
|
|
|
|
|
|
|
|
void swcr_init(void)
|
2022-07-18 09:00:30 +02:00
|
|
|
{
|
2022-07-27 13:51:53 +02:00
|
|
|
int algs[CRYPTO_ALGORITHM_MAX + 1];
|
|
|
|
int flags = CRYPTOCAP_F_SOFTWARE | CRYPTOCAP_F_ENCRYPT_MAC |
|
|
|
|
CRYPTOCAP_F_MAC_ENCRYPT;
|
|
|
|
|
|
|
|
swcr_id = crypto_get_driverid(flags);
|
|
|
|
if (swcr_id < 0)
|
|
|
|
{
|
|
|
|
/* This should never happen */
|
|
|
|
|
2022-07-28 11:52:21 +02:00
|
|
|
PANIC();
|
2022-07-27 13:51:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
algs[CRYPTO_3DES_CBC] = CRYPTO_ALG_FLAG_SUPPORTED;
|
|
|
|
algs[CRYPTO_BLF_CBC] = CRYPTO_ALG_FLAG_SUPPORTED;
|
|
|
|
algs[CRYPTO_CAST_CBC] = CRYPTO_ALG_FLAG_SUPPORTED;
|
|
|
|
algs[CRYPTO_MD5_HMAC] = CRYPTO_ALG_FLAG_SUPPORTED;
|
|
|
|
algs[CRYPTO_SHA1_HMAC] = CRYPTO_ALG_FLAG_SUPPORTED;
|
|
|
|
algs[CRYPTO_RIPEMD160_HMAC] = CRYPTO_ALG_FLAG_SUPPORTED;
|
|
|
|
algs[CRYPTO_RIJNDAEL128_CBC] = CRYPTO_ALG_FLAG_SUPPORTED;
|
|
|
|
algs[CRYPTO_AES_CTR] = CRYPTO_ALG_FLAG_SUPPORTED;
|
|
|
|
algs[CRYPTO_AES_XTS] = CRYPTO_ALG_FLAG_SUPPORTED;
|
|
|
|
algs[CRYPTO_AES_GCM_16] = CRYPTO_ALG_FLAG_SUPPORTED;
|
|
|
|
algs[CRYPTO_AES_GMAC] = CRYPTO_ALG_FLAG_SUPPORTED;
|
|
|
|
algs[CRYPTO_NULL] = CRYPTO_ALG_FLAG_SUPPORTED;
|
|
|
|
algs[CRYPTO_SHA2_256_HMAC] = CRYPTO_ALG_FLAG_SUPPORTED;
|
|
|
|
algs[CRYPTO_SHA2_384_HMAC] = CRYPTO_ALG_FLAG_SUPPORTED;
|
|
|
|
algs[CRYPTO_SHA2_512_HMAC] = CRYPTO_ALG_FLAG_SUPPORTED;
|
|
|
|
algs[CRYPTO_AES_128_GMAC] = CRYPTO_ALG_FLAG_SUPPORTED;
|
|
|
|
algs[CRYPTO_AES_192_GMAC] = CRYPTO_ALG_FLAG_SUPPORTED;
|
|
|
|
algs[CRYPTO_AES_256_GMAC] = CRYPTO_ALG_FLAG_SUPPORTED;
|
|
|
|
algs[CRYPTO_CHACHA20_POLY1305] = CRYPTO_ALG_FLAG_SUPPORTED;
|
|
|
|
algs[CRYPTO_CHACHA20_POLY1305_MAC] = CRYPTO_ALG_FLAG_SUPPORTED;
|
2023-07-14 14:49:47 +02:00
|
|
|
algs[CRYPTO_MD5] = CRYPTO_ALG_FLAG_SUPPORTED;
|
|
|
|
algs[CRYPTO_SHA1] = CRYPTO_ALG_FLAG_SUPPORTED;
|
2023-08-08 08:02:52 +02:00
|
|
|
algs[CRYPTO_SHA2_224] = CRYPTO_ALG_FLAG_SUPPORTED;
|
2023-07-14 14:49:47 +02:00
|
|
|
algs[CRYPTO_SHA2_256] = CRYPTO_ALG_FLAG_SUPPORTED;
|
2023-08-08 08:02:52 +02:00
|
|
|
algs[CRYPTO_SHA2_384] = CRYPTO_ALG_FLAG_SUPPORTED;
|
2023-07-14 14:49:47 +02:00
|
|
|
algs[CRYPTO_SHA2_512] = CRYPTO_ALG_FLAG_SUPPORTED;
|
2022-07-27 13:51:53 +02:00
|
|
|
algs[CRYPTO_ESN] = CRYPTO_ALG_FLAG_SUPPORTED;
|
|
|
|
|
|
|
|
crypto_register(swcr_id, algs, swcr_newsession,
|
|
|
|
swcr_freesession, swcr_process);
|
2022-07-18 09:00:30 +02:00
|
|
|
}
|