Skip to content

Commit 947b24c

Browse files
ThePassionatexiaoxiang781216
authored andcommitted
crypto/poly1305: export poly1305 mac algorithm via /dev/crypto
Signed-off-by: makejian <[email protected]>
1 parent 8628cc9 commit 947b24c

File tree

5 files changed

+60
-9
lines changed

5 files changed

+60
-9
lines changed

crypto/cryptodev.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,7 @@ static int cryptof_ioctl(FAR struct file *filep,
248248
case CRYPTO_SHA2_512_HMAC:
249249
case CRYPTO_AES_128_GMAC:
250250
case CRYPTO_MD5:
251+
case CRYPTO_POLY1305:
251252
case CRYPTO_RIPEMD160:
252253
case CRYPTO_SHA1:
253254
case CRYPTO_SHA2_224:

crypto/cryptosoft.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -831,6 +831,10 @@ int swcr_newsession(FAR uint32_t *sid, FAR struct cryptoini *cri)
831831
axf = &auth_hash_gmac_aes_256;
832832
goto auth4common;
833833

834+
case CRYPTO_POLY1305:
835+
axf = &auth_hash_poly1305;
836+
goto auth4common;
837+
834838
case CRYPTO_CHACHA20_POLY1305_MAC:
835839
axf = &auth_hash_chacha20_poly1305;
836840

@@ -845,6 +849,7 @@ int swcr_newsession(FAR uint32_t *sid, FAR struct cryptoini *cri)
845849
axf->init((*swd)->sw_ictx);
846850
axf->setkey((*swd)->sw_ictx, (FAR uint8_t *)cri->cri_key,
847851
cri->cri_klen / 8);
852+
bcopy((*swd)->sw_ictx, &(*swd)->sw_ctx, axf->ctxsize);
848853
(*swd)->sw_axf = axf;
849854
break;
850855

@@ -944,6 +949,7 @@ int swcr_freesession(uint64_t tid)
944949
case CRYPTO_AES_256_GMAC:
945950
case CRYPTO_CHACHA20_POLY1305_MAC:
946951
case CRYPTO_MD5:
952+
case CRYPTO_POLY1305:
947953
case CRYPTO_RIPEMD160:
948954
case CRYPTO_SHA1:
949955
case CRYPTO_SHA2_224:
@@ -1077,6 +1083,7 @@ int swcr_process(struct cryptop *crp)
10771083
break;
10781084

10791085
case CRYPTO_MD5:
1086+
case CRYPTO_POLY1305:
10801087
case CRYPTO_RIPEMD160:
10811088
case CRYPTO_SHA1:
10821089
case CRYPTO_SHA2_224:
@@ -1216,6 +1223,7 @@ void swcr_init(void)
12161223
algs[CRYPTO_CHACHA20_POLY1305] = CRYPTO_ALG_FLAG_SUPPORTED;
12171224
algs[CRYPTO_CHACHA20_POLY1305_MAC] = CRYPTO_ALG_FLAG_SUPPORTED;
12181225
algs[CRYPTO_MD5] = CRYPTO_ALG_FLAG_SUPPORTED;
1226+
algs[CRYPTO_POLY1305] = CRYPTO_ALG_FLAG_SUPPORTED;
12191227
algs[CRYPTO_RIPEMD160] = CRYPTO_ALG_FLAG_SUPPORTED;
12201228
algs[CRYPTO_SHA1] = CRYPTO_ALG_FLAG_SUPPORTED;
12211229
algs[CRYPTO_SHA2_224] = CRYPTO_ALG_FLAG_SUPPORTED;

crypto/xform.c

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@
7171
#include <crypto/xform.h>
7272
#include <crypto/gmac.h>
7373
#include <crypto/chachapoly.h>
74+
#include <crypto/poly1305.h>
7475

7576
#include "des_locl.h"
7677

@@ -117,6 +118,10 @@ void aes_xts_reinit(caddr_t, FAR uint8_t *);
117118
void aes_gcm_reinit(caddr_t, FAR uint8_t *);
118119
void aes_ofb_reinit(caddr_t, FAR uint8_t *);
119120

121+
void null_init(FAR void *);
122+
void poly1305_setkey(FAR void *, FAR const uint8_t *, uint16_t);
123+
int poly1305update_int(FAR void *, FAR const uint8_t *, size_t);
124+
int poly1305_final(FAR uint8_t *, FAR void *);
120125
int md5update_int(FAR void *, FAR const uint8_t *, size_t);
121126
int sha1update_int(FAR void *, FAR const uint8_t *, size_t);
122127
int rmd160update_int(FAR void *, FAR const uint8_t *, size_t);
@@ -389,6 +394,15 @@ const struct auth_hash auth_hash_md5 =
389394
(void (*) (FAR uint8_t *, FAR void *)) md5final
390395
};
391396

397+
const struct auth_hash auth_hash_poly1305 =
398+
{
399+
CRYPTO_POLY1305, "POLY1305",
400+
0, 16, 16, sizeof(poly1305_state), poly1305_block_size,
401+
(void (*) (FAR void *)) null_init, poly1305_setkey, NULL,
402+
poly1305update_int,
403+
(void (*) (FAR uint8_t *, FAR void *)) poly1305_final
404+
};
405+
392406
const struct auth_hash auth_hash_ripemd_160 =
393407
{
394408
CRYPTO_RIPEMD160, "RIPEMD160",
@@ -566,7 +580,9 @@ void aes_ctr_crypt(caddr_t key, FAR uint8_t *data)
566580
for (i = AESCTR_BLOCKSIZE - 1;
567581
i >= AESCTR_NONCESIZE + AESCTR_IVSIZE; i--)
568582
{
569-
if (++ctx->ac_block[i]) /* continue on overflow */
583+
/* continue on overflow */
584+
585+
if (++ctx->ac_block[i])
570586
{
571587
break;
572588
}
@@ -798,6 +814,30 @@ void aes_cfb128_decrypt(caddr_t key, FAR uint8_t *data)
798814

799815
/* And now for auth. */
800816

817+
void null_init(FAR void *ctx)
818+
{
819+
}
820+
821+
void poly1305_setkey(FAR void *sched, FAR const uint8_t *key, uint16_t len)
822+
{
823+
FAR struct poly1305_state *ctx;
824+
825+
ctx = (FAR struct poly1305_state *)sched;
826+
poly1305_begin(ctx, key);
827+
}
828+
829+
int poly1305update_int(FAR void *ctx, FAR const uint8_t *buf, size_t len)
830+
{
831+
poly1305_update(ctx, buf, len);
832+
return 0;
833+
}
834+
835+
int poly1305_final(FAR uint8_t *digest, FAR void *ctx)
836+
{
837+
poly1305_finish(ctx, digest);
838+
return 0;
839+
}
840+
801841
int rmd160update_int(FAR void *ctx, FAR const uint8_t *buf, size_t len)
802842
{
803843
rmd160update(ctx, buf, len);

include/crypto/cryptodev.h

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -117,14 +117,15 @@
117117
#define CRYPTO_CHACHA20_POLY1305 24
118118
#define CRYPTO_CHACHA20_POLY1305_MAC 25
119119
#define CRYPTO_MD5 26
120-
#define CRYPTO_RIPEMD160 27
121-
#define CRYPTO_SHA1 28
122-
#define CRYPTO_SHA2_224 29
123-
#define CRYPTO_SHA2_256 30
124-
#define CRYPTO_SHA2_384 31
125-
#define CRYPTO_SHA2_512 32
126-
#define CRYPTO_ESN 33 /* Support for Extended Sequence Numbers */
127-
#define CRYPTO_ALGORITHM_MAX 33 /* Keep updated */
120+
#define CRYPTO_POLY1305 27
121+
#define CRYPTO_RIPEMD160 28
122+
#define CRYPTO_SHA1 29
123+
#define CRYPTO_SHA2_224 30
124+
#define CRYPTO_SHA2_256 31
125+
#define CRYPTO_SHA2_384 32
126+
#define CRYPTO_SHA2_512 33
127+
#define CRYPTO_ESN 34 /* Support for Extended Sequence Numbers */
128+
#define CRYPTO_ALGORITHM_MAX 34 /* Keep updated */
128129

129130
/* Algorithm flags */
130131

include/crypto/xform.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ extern const struct auth_hash auth_hash_gmac_aes_192;
121121
extern const struct auth_hash auth_hash_gmac_aes_256;
122122
extern const struct auth_hash auth_hash_chacha20_poly1305;
123123
extern const struct auth_hash auth_hash_md5;
124+
extern const struct auth_hash auth_hash_poly1305;
124125
extern const struct auth_hash auth_hash_ripemd_160;
125126
extern const struct auth_hash auth_hash_sha1;
126127
extern const struct auth_hash auth_hash_sha2_224;

0 commit comments

Comments
 (0)