Skip to content

Commit ea44e3d

Browse files
committed
crypto/cryptosoft: Add support for PBKDF2
This adds support for PBKDF2 (SHA1 and SHA256) while leveraging the existing infrastructure for HMAC. Signed-off-by: Vlad Pruteanu <pruteanuvlad1611@yahoo.com>
1 parent c568a34 commit ea44e3d

4 files changed

Lines changed: 91 additions & 2 deletions

File tree

boards/xtensa/esp32/esp32-devkitc/configs/crypto/defconfig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ CONFIG_NSH_BUILTIN_APPS=y
4444
CONFIG_NSH_FILEIOSIZE=512
4545
CONFIG_NSH_READLINE=y
4646
CONFIG_NSH_STRERROR=y
47+
CONFIG_TESTING_CRYPTO_PBKDF2=y
4748
CONFIG_PREALLOC_TIMERS=0
4849
CONFIG_RAM_SIZE=314688
4950
CONFIG_RAM_START=0x20000000

crypto/cryptodev.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,8 @@ static int cryptof_ioctl(FAR struct file *filep,
264264
case CRYPTO_SHA2_384:
265265
case CRYPTO_SHA2_512:
266266
case CRYPTO_CRC32:
267+
case CRYPTO_PBKDF2_HMAC_SHA1:
268+
case CRYPTO_PBKDF2_HMAC_SHA256:
267269
thash = true;
268270
break;
269271
default:
@@ -470,6 +472,11 @@ static int cryptodev_op(FAR struct csession *cse,
470472
crp.crp_mac = cop->mac;
471473
}
472474

475+
if (cop->iterations)
476+
{
477+
crp.crp_iter = cop->iterations;
478+
}
479+
473480
/* try the fast path first */
474481

475482
crp.crp_flags = CRYPTO_F_IOV | CRYPTO_F_NOQUEUE;

crypto/cryptosoft.c

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1156,6 +1156,9 @@ int swcr_authcompute(FAR struct cryptop *crp,
11561156
case CRYPTO_SHA2_256_HMAC:
11571157
case CRYPTO_SHA2_384_HMAC:
11581158
case CRYPTO_SHA2_512_HMAC:
1159+
case CRYPTO_PBKDF2_HMAC_SHA1:
1160+
case CRYPTO_PBKDF2_HMAC_SHA256:
1161+
11591162
if (sw->sw_octx == NULL)
11601163
{
11611164
return -EINVAL;
@@ -1656,12 +1659,14 @@ int swcr_newsession(FAR uint32_t *sid, FAR struct cryptoini *cri)
16561659
axf = &auth_hash_hmac_md5_96;
16571660
goto authcommon;
16581661
case CRYPTO_SHA1_HMAC:
1662+
case CRYPTO_PBKDF2_HMAC_SHA1:
16591663
axf = &auth_hash_hmac_sha1_96;
16601664
goto authcommon;
16611665
case CRYPTO_RIPEMD160_HMAC:
16621666
axf = &auth_hash_hmac_ripemd_160_96;
16631667
goto authcommon;
16641668
case CRYPTO_SHA2_256_HMAC:
1669+
case CRYPTO_PBKDF2_HMAC_SHA256:
16651670
axf = &auth_hash_hmac_sha2_256_128;
16661671
goto authcommon;
16671672
case CRYPTO_SHA2_384_HMAC:
@@ -2047,7 +2052,11 @@ int swcr_process(struct cryptop *crp)
20472052
}
20482053

20492054
break;
2055+
case CRYPTO_PBKDF2_HMAC_SHA1:
2056+
case CRYPTO_PBKDF2_HMAC_SHA256:
2057+
swcr_pbkdf2(crp, crd, sw, crp->crp_buf);
20502058

2059+
break;
20512060
case CRYPTO_MD5:
20522061
case CRYPTO_POLY1305:
20532062
case CRYPTO_RIPEMD160:
@@ -2090,6 +2099,71 @@ int swcr_process(struct cryptop *crp)
20902099
return 0;
20912100
}
20922101

2102+
int swcr_pbkdf2(FAR struct cryptop *crp,
2103+
FAR struct cryptodesc *crd,
2104+
FAR struct swcr_data *swd,
2105+
caddr_t buf)
2106+
{
2107+
uint8_t U[64];
2108+
uint8_t T[64];
2109+
uint8_t macbuf[64];
2110+
uint8_t ictx[256];
2111+
struct cryptop crp_dummy;
2112+
struct cryptodesc crd_dummy;
2113+
2114+
size_t generated = 0;
2115+
uint32_t blocknum;
2116+
uint32_t i;
2117+
uint32_t j;
2118+
2119+
crp_dummy.crp_mac = macbuf;
2120+
2121+
for (blocknum = 1; generated < crp->crp_olen; blocknum++)
2122+
{
2123+
uint8_t saltblk[crp->crp_ilen + 4];
2124+
2125+
memcpy(saltblk, crp->crp_buf, crp->crp_ilen);
2126+
*(uint32_t *)(saltblk + crp->crp_ilen) = htonl(blocknum);
2127+
2128+
memcpy(ictx, swd->sw_ictx, swd->sw_axf->ctxsize);
2129+
memcpy(&swd->sw_ctx, ictx, swd->sw_axf->ctxsize);
2130+
2131+
crd_dummy.crd_skip = 0;
2132+
crd_dummy.crd_flags = 0;
2133+
2134+
/* U1 */
2135+
2136+
crd_dummy.crd_len = crp->crp_ilen + 4;
2137+
swcr_authcompute(&crp_dummy, &crd_dummy, swd, (caddr_t)saltblk);
2138+
2139+
memcpy(U, macbuf, swd->sw_axf->hashsize);
2140+
memcpy(T, U, swd->sw_axf->hashsize);
2141+
2142+
/* U2..Uc */
2143+
2144+
for (i = 1; i < crp->crp_iter; i++)
2145+
{
2146+
memcpy(&swd->sw_ctx, ictx, swd->sw_axf->ctxsize);
2147+
2148+
crd_dummy.crd_len = swd->sw_axf->hashsize;
2149+
swcr_authcompute(&crp_dummy, &crd_dummy, swd, (caddr_t)U);
2150+
2151+
memcpy(U, macbuf, swd->sw_axf->hashsize);
2152+
2153+
for (j = 0; j < swd->sw_axf->hashsize; j++)
2154+
T[j] ^= U[j];
2155+
}
2156+
2157+
size_t tocopy = MIN(crp->crp_olen - generated,
2158+
swd->sw_axf->hashsize);
2159+
2160+
memcpy(crp->crp_mac + generated, T, tocopy);
2161+
generated += tocopy;
2162+
}
2163+
2164+
return 0;
2165+
}
2166+
20932167
int swcr_mod_exp(struct cryptkop *krp)
20942168
{
20952169
uint8_t *input = (uint8_t *)krp->krp_param[0].crp_p;
@@ -2353,6 +2427,8 @@ void swcr_init(void)
23532427
algs[CRYPTO_CRC32] = CRYPTO_ALG_FLAG_SUPPORTED;
23542428
algs[CRYPTO_AES_CMAC] = CRYPTO_ALG_FLAG_SUPPORTED;
23552429
algs[CRYPTO_AES_128_CMAC] = CRYPTO_ALG_FLAG_SUPPORTED;
2430+
algs[CRYPTO_PBKDF2_HMAC_SHA1] = CRYPTO_ALG_FLAG_SUPPORTED;
2431+
algs[CRYPTO_PBKDF2_HMAC_SHA256] = CRYPTO_ALG_FLAG_SUPPORTED;
23562432
algs[CRYPTO_ESN] = CRYPTO_ALG_FLAG_SUPPORTED;
23572433

23582434
crypto_register(swcr_id, algs, swcr_newsession,

include/crypto/cryptodev.h

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,8 +135,10 @@
135135
#define CRYPTO_CRC32 35
136136
#define CRYPTO_AES_CMAC 36
137137
#define CRYPTO_AES_128_CMAC 37
138-
#define CRYPTO_ESN 38 /* Support for Extended Sequence Numbers */
139-
#define CRYPTO_ALGORITHM_MAX 38 /* Keep updated */
138+
#define CRYPTO_PBKDF2_HMAC_SHA1 38
139+
#define CRYPTO_PBKDF2_HMAC_SHA256 39
140+
#define CRYPTO_ESN 40 /* Support for Extended Sequence Numbers */
141+
#define CRYPTO_ALGORITHM_MAX 40 /* Keep updated */
140142

141143
/* Algorithm flags */
142144

@@ -235,6 +237,7 @@ struct cryptop
235237
caddr_t crp_dst;
236238
caddr_t crp_iv;
237239
caddr_t crp_aad;
240+
int crp_iter;
238241
};
239242

240243
#define CRYPTO_BUF_IOV 0x1
@@ -408,6 +411,8 @@ struct crypt_op
408411
*/
409412

410413
uint16_t flags;
414+
uint32_t iterations;
415+
unsigned targetlen;
411416
unsigned len;
412417
unsigned olen;
413418
unsigned ivlen;

0 commit comments

Comments
 (0)