diff --git a/crypto/cryptodev.c b/crypto/cryptodev.c index a2dd8bfd05..67d31f0591 100644 --- a/crypto/cryptodev.c +++ b/crypto/cryptodev.c @@ -255,6 +255,7 @@ static int cryptof_ioctl(FAR struct file *filep, case CRYPTO_SHA2_256: case CRYPTO_SHA2_384: case CRYPTO_SHA2_512: + case CRYPTO_CRC32: thash = true; break; default: diff --git a/crypto/cryptosoft.c b/crypto/cryptosoft.c index bc14798592..637d38f502 100644 --- a/crypto/cryptosoft.c +++ b/crypto/cryptosoft.c @@ -835,6 +835,10 @@ int swcr_newsession(FAR uint32_t *sid, FAR struct cryptoini *cri) axf = &auth_hash_poly1305; goto auth4common; + case CRYPTO_CRC32: + axf = &auth_hash_crc32; + goto auth4common; + case CRYPTO_CHACHA20_POLY1305_MAC: axf = &auth_hash_chacha20_poly1305; @@ -956,6 +960,7 @@ int swcr_freesession(uint64_t tid) case CRYPTO_SHA2_256: case CRYPTO_SHA2_384: case CRYPTO_SHA2_512: + case CRYPTO_CRC32: axf = swd->sw_axf; if (swd->sw_ictx) @@ -1090,6 +1095,7 @@ int swcr_process(struct cryptop *crp) case CRYPTO_SHA2_256: case CRYPTO_SHA2_384: case CRYPTO_SHA2_512: + case CRYPTO_CRC32: if ((crp->crp_etype = swcr_hash(crp, crd, sw, crp->crp_buf)) != 0) { @@ -1230,6 +1236,7 @@ void swcr_init(void) algs[CRYPTO_SHA2_256] = CRYPTO_ALG_FLAG_SUPPORTED; algs[CRYPTO_SHA2_384] = CRYPTO_ALG_FLAG_SUPPORTED; algs[CRYPTO_SHA2_512] = CRYPTO_ALG_FLAG_SUPPORTED; + algs[CRYPTO_CRC32] = CRYPTO_ALG_FLAG_SUPPORTED; algs[CRYPTO_ESN] = CRYPTO_ALG_FLAG_SUPPORTED; crypto_register(swcr_id, algs, swcr_newsession, diff --git a/crypto/xform.c b/crypto/xform.c index e126b9414d..842320801a 100644 --- a/crypto/xform.c +++ b/crypto/xform.c @@ -72,9 +72,16 @@ #include #include #include +#include #include "des_locl.h" +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +#define CRC32_XOR_VALUE 0xFFFFFFFFUL + /**************************************************************************** * Public Functions ****************************************************************************/ @@ -129,6 +136,9 @@ int sha224update_int(FAR void *, FAR const uint8_t *, size_t); int sha256update_int(FAR void *, FAR const uint8_t *, size_t); int sha384update_int(FAR void *, FAR const uint8_t *, size_t); int sha512update_int(FAR void *, FAR const uint8_t *, size_t); +void crc32setkey(FAR void *, FAR const uint8_t *, uint16_t); +int crc32update(FAR void *, FAR const uint8_t *, size_t); +void crc32final(FAR uint8_t *, FAR void *); struct aes_ctr_ctx { @@ -457,6 +467,13 @@ const struct auth_hash auth_hash_sha2_512 = (void (*)(FAR uint8_t *, FAR void *)) sha512final }; +const struct auth_hash auth_hash_crc32 = +{ + CRYPTO_CRC32, "CRC32", + 0, 32, 0, sizeof(uint32_t), 1, + null_init, crc32setkey, NULL, crc32update, crc32final +}; + /* Encryption wrapper routines. */ void des3_encrypt(caddr_t key, FAR uint8_t *blk) @@ -879,3 +896,24 @@ int sha512update_int(FAR void *ctx, FAR const uint8_t *buf, size_t len) sha512update(ctx, buf, len); return 0; } + +void crc32setkey(FAR void *ctx, FAR const uint8_t *key, uint16_t len) +{ + FAR uint32_t *val = (FAR uint32_t *)key; + uint32_t tmp = (*val) ^ CRC32_XOR_VALUE; + memcpy(ctx, &tmp, len); +} + +int crc32update(FAR void *ctx, FAR const uint8_t *buf, size_t len) +{ + FAR uint32_t *startval = (FAR uint32_t *)ctx; + *startval = crc32part(buf, len, *startval); + return 0; +} + +void crc32final(FAR uint8_t *digest, FAR void *ctx) +{ + FAR uint32_t *val = (FAR uint32_t *)ctx; + uint32_t result = (*val) ^ CRC32_XOR_VALUE; + memcpy(digest, &result, sizeof(uint32_t)); +} diff --git a/include/crypto/cryptodev.h b/include/crypto/cryptodev.h index 6e579e9516..4398975510 100644 --- a/include/crypto/cryptodev.h +++ b/include/crypto/cryptodev.h @@ -124,8 +124,9 @@ #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 */ +#define CRYPTO_CRC32 34 +#define CRYPTO_ESN 35 /* Support for Extended Sequence Numbers */ +#define CRYPTO_ALGORITHM_MAX 35 /* Keep updated */ /* Algorithm flags */ diff --git a/include/crypto/xform.h b/include/crypto/xform.h index 96462676f1..253b10475f 100644 --- a/include/crypto/xform.h +++ b/include/crypto/xform.h @@ -128,5 +128,6 @@ extern const struct auth_hash auth_hash_sha2_224; extern const struct auth_hash auth_hash_sha2_256; extern const struct auth_hash auth_hash_sha2_384; extern const struct auth_hash auth_hash_sha2_512; +extern const struct auth_hash auth_hash_crc32; #endif /* __INCLUDE_CRYPTO_XFORM_H */