diff --git a/crypto/cryptodev.c b/crypto/cryptodev.c index 4fab9c1778..1392980443 100644 --- a/crypto/cryptodev.c +++ b/crypto/cryptodev.c @@ -248,6 +248,7 @@ static int cryptof_ioctl(FAR struct file *filep, case CRYPTO_SHA2_512_HMAC: case CRYPTO_AES_128_GMAC: case CRYPTO_MD5: + case CRYPTO_POLY1305: case CRYPTO_RIPEMD160: case CRYPTO_SHA1: case CRYPTO_SHA2_224: diff --git a/crypto/cryptosoft.c b/crypto/cryptosoft.c index ed36542c52..d0a73ad121 100644 --- a/crypto/cryptosoft.c +++ b/crypto/cryptosoft.c @@ -831,6 +831,10 @@ int swcr_newsession(FAR uint32_t *sid, FAR struct cryptoini *cri) axf = &auth_hash_gmac_aes_256; goto auth4common; + case CRYPTO_POLY1305: + axf = &auth_hash_poly1305; + goto auth4common; + case CRYPTO_CHACHA20_POLY1305_MAC: axf = &auth_hash_chacha20_poly1305; @@ -845,6 +849,7 @@ int swcr_newsession(FAR uint32_t *sid, FAR struct cryptoini *cri) axf->init((*swd)->sw_ictx); axf->setkey((*swd)->sw_ictx, (FAR uint8_t *)cri->cri_key, cri->cri_klen / 8); + bcopy((*swd)->sw_ictx, &(*swd)->sw_ctx, axf->ctxsize); (*swd)->sw_axf = axf; break; @@ -944,6 +949,7 @@ int swcr_freesession(uint64_t tid) case CRYPTO_AES_256_GMAC: case CRYPTO_CHACHA20_POLY1305_MAC: case CRYPTO_MD5: + case CRYPTO_POLY1305: case CRYPTO_RIPEMD160: case CRYPTO_SHA1: case CRYPTO_SHA2_224: @@ -1077,6 +1083,7 @@ int swcr_process(struct cryptop *crp) break; case CRYPTO_MD5: + case CRYPTO_POLY1305: case CRYPTO_RIPEMD160: case CRYPTO_SHA1: case CRYPTO_SHA2_224: @@ -1216,6 +1223,7 @@ void swcr_init(void) algs[CRYPTO_CHACHA20_POLY1305] = CRYPTO_ALG_FLAG_SUPPORTED; algs[CRYPTO_CHACHA20_POLY1305_MAC] = CRYPTO_ALG_FLAG_SUPPORTED; algs[CRYPTO_MD5] = CRYPTO_ALG_FLAG_SUPPORTED; + algs[CRYPTO_POLY1305] = CRYPTO_ALG_FLAG_SUPPORTED; algs[CRYPTO_RIPEMD160] = CRYPTO_ALG_FLAG_SUPPORTED; algs[CRYPTO_SHA1] = CRYPTO_ALG_FLAG_SUPPORTED; algs[CRYPTO_SHA2_224] = CRYPTO_ALG_FLAG_SUPPORTED; diff --git a/crypto/xform.c b/crypto/xform.c index b16bb989fd..e126b9414d 100644 --- a/crypto/xform.c +++ b/crypto/xform.c @@ -71,6 +71,7 @@ #include #include #include +#include #include "des_locl.h" @@ -117,6 +118,10 @@ void aes_xts_reinit(caddr_t, FAR uint8_t *); void aes_gcm_reinit(caddr_t, FAR uint8_t *); void aes_ofb_reinit(caddr_t, FAR uint8_t *); +void null_init(FAR void *); +void poly1305_setkey(FAR void *, FAR const uint8_t *, uint16_t); +int poly1305update_int(FAR void *, FAR const uint8_t *, size_t); +int poly1305_final(FAR uint8_t *, FAR void *); int md5update_int(FAR void *, FAR const uint8_t *, size_t); int sha1update_int(FAR void *, FAR const uint8_t *, size_t); int rmd160update_int(FAR void *, FAR const uint8_t *, size_t); @@ -389,6 +394,15 @@ const struct auth_hash auth_hash_md5 = (void (*) (FAR uint8_t *, FAR void *)) md5final }; +const struct auth_hash auth_hash_poly1305 = +{ + CRYPTO_POLY1305, "POLY1305", + 0, 16, 16, sizeof(poly1305_state), poly1305_block_size, + (void (*) (FAR void *)) null_init, poly1305_setkey, NULL, + poly1305update_int, + (void (*) (FAR uint8_t *, FAR void *)) poly1305_final +}; + const struct auth_hash auth_hash_ripemd_160 = { CRYPTO_RIPEMD160, "RIPEMD160", @@ -566,7 +580,9 @@ void aes_ctr_crypt(caddr_t key, FAR uint8_t *data) for (i = AESCTR_BLOCKSIZE - 1; i >= AESCTR_NONCESIZE + AESCTR_IVSIZE; i--) { - if (++ctx->ac_block[i]) /* continue on overflow */ + /* continue on overflow */ + + if (++ctx->ac_block[i]) { break; } @@ -798,6 +814,30 @@ void aes_cfb128_decrypt(caddr_t key, FAR uint8_t *data) /* And now for auth. */ +void null_init(FAR void *ctx) +{ +} + +void poly1305_setkey(FAR void *sched, FAR const uint8_t *key, uint16_t len) +{ + FAR struct poly1305_state *ctx; + + ctx = (FAR struct poly1305_state *)sched; + poly1305_begin(ctx, key); +} + +int poly1305update_int(FAR void *ctx, FAR const uint8_t *buf, size_t len) +{ + poly1305_update(ctx, buf, len); + return 0; +} + +int poly1305_final(FAR uint8_t *digest, FAR void *ctx) +{ + poly1305_finish(ctx, digest); + return 0; +} + int rmd160update_int(FAR void *ctx, FAR const uint8_t *buf, size_t len) { rmd160update(ctx, buf, len); diff --git a/include/crypto/cryptodev.h b/include/crypto/cryptodev.h index 7d18402182..27ccf97cda 100644 --- a/include/crypto/cryptodev.h +++ b/include/crypto/cryptodev.h @@ -117,14 +117,15 @@ #define CRYPTO_CHACHA20_POLY1305 24 #define CRYPTO_CHACHA20_POLY1305_MAC 25 #define CRYPTO_MD5 26 -#define CRYPTO_RIPEMD160 27 -#define CRYPTO_SHA1 28 -#define CRYPTO_SHA2_224 29 -#define CRYPTO_SHA2_256 30 -#define CRYPTO_SHA2_384 31 -#define CRYPTO_SHA2_512 32 -#define CRYPTO_ESN 33 /* Support for Extended Sequence Numbers */ -#define CRYPTO_ALGORITHM_MAX 33 /* Keep updated */ +#define CRYPTO_POLY1305 27 +#define CRYPTO_RIPEMD160 28 +#define CRYPTO_SHA1 29 +#define CRYPTO_SHA2_224 30 +#define CRYPTO_SHA2_256 31 +#define CRYPTO_SHA2_384 32 +#define CRYPTO_SHA2_512 33 +#define CRYPTO_ESN 34 /* Support for Extended Sequence Numbers */ +#define CRYPTO_ALGORITHM_MAX 34 /* Keep updated */ /* Algorithm flags */ diff --git a/include/crypto/xform.h b/include/crypto/xform.h index 196057ea13..c8dd3024ab 100644 --- a/include/crypto/xform.h +++ b/include/crypto/xform.h @@ -121,6 +121,7 @@ extern const struct auth_hash auth_hash_gmac_aes_192; extern const struct auth_hash auth_hash_gmac_aes_256; extern const struct auth_hash auth_hash_chacha20_poly1305; extern const struct auth_hash auth_hash_md5; +extern const struct auth_hash auth_hash_poly1305; extern const struct auth_hash auth_hash_ripemd_160; extern const struct auth_hash auth_hash_sha1; extern const struct auth_hash auth_hash_sha2_224;