Skip to content

Commit 194269d

Browse files
ThePassionatexiaoxiang781216
authored andcommitted
mbedtls-alt/poly1305-alt: add poly1305 alternative implementation
Signed-off-by: makejian <[email protected]>
1 parent 83d3256 commit 194269d

File tree

5 files changed

+144
-2
lines changed

5 files changed

+144
-2
lines changed

crypto/mbedtls/Kconfig

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -578,6 +578,11 @@ config MBEDTLS_MD5_ALT
578578
select MBEDTLS_ALT
579579
default n
580580

581+
config MBEDTLS_POLY1305_ALT
582+
bool "Enable Mbedt TLS POLY1305 module alted by nuttx crypto"
583+
select MBEDTLS_ALT
584+
default n
585+
581586
config MBEDTLS_RIPEMD160_ALT
582587
bool "Enable Mbedt TLS RIPEMD160 module alted by nuttx crypto"
583588
select MBEDTLS_ALT

crypto/mbedtls/Makefile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,10 @@ ifeq ($(CONFIG_MBEDTLS_MD5_ALT),y)
127127
CSRCS += $(APPDIR)/crypto/mbedtls/source/md5_alt.c
128128
endif
129129

130+
ifeq ($(CONFIG_MBEDTLS_POLY1305_ALT),y)
131+
CSRCS += $(APPDIR)/crypto/mbedtls/source/poly1305_alt.c
132+
endif
133+
130134
ifeq ($(CONFIG_MBEDTLS_RIPEMD160_ALT),y)
131135
CSRCS += $(APPDIR)/crypto/mbedtls/source/ripemd160_alt.c
132136
endif

crypto/mbedtls/include/mbedtls/mbedtls_config.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -374,8 +374,9 @@
374374
#ifdef CONFIG_MBEDTLS_MD5_ALT
375375
#define MBEDTLS_MD5_ALT
376376
#endif
377-
/* #define MBEDTLS_POLY1305_ALT
378-
*/
377+
#ifdef CONFIG_MBEDTLS_POLY1305_ALT
378+
#define MBEDTLS_POLY1305_ALT
379+
#endif
379380
#ifdef CONFIG_MBEDTLS_RIPEMD160_ALT
380381
#define MBEDTLS_RIPEMD160_ALT
381382
#endif

crypto/mbedtls/include/poly1305_alt.h

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/****************************************************************************
2+
* apps/crypto/mbedtls/include/poly1305_alt.h
3+
*
4+
* Licensed to the Apache Software Foundation (ASF) under one or more
5+
* contributor license agreements. See the NOTICE file distributed with
6+
* this work for additional information regarding copyright ownership. The
7+
* ASF licenses this file to you under the Apache License, Version 2.0 (the
8+
* "License"); you may not use this file except in compliance with the
9+
* License. You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
16+
* License for the specific language governing permissions and limitations
17+
* under the License.
18+
****************************************************************************/
19+
20+
#ifndef __APPS_CRYPTO_MBEDTLS_INCLUDE_POLY1305_ALT_H
21+
#define __APPS_CRYPTO_MBEDTLS_INCLUDE_POLY1305_ALT_H
22+
23+
/****************************************************************************
24+
* Included Files
25+
****************************************************************************/
26+
27+
#include "dev_alt.h"
28+
29+
#define mbedtls_poly1305_context cryptodev_context_t
30+
31+
#endif /* __APPS_CRYPTO_MBEDTLS_INCLUDE_POLY1305_ALT_H */

crypto/mbedtls/source/poly1305_alt.c

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
/****************************************************************************
2+
* apps/crypto/mbedtls/source/poly1305_alt.c
3+
*
4+
* Licensed to the Apache Software Foundation (ASF) under one or more
5+
* contributor license agreements. See the NOTICE file distributed with
6+
* this work for additional information regarding copyright ownership. The
7+
* ASF licenses this file to you under the Apache License, Version 2.0 (the
8+
* "License"); you may not use this file except in compliance with the
9+
* License. You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
16+
* License for the specific language governing permissions and limitations
17+
* under the License.
18+
****************************************************************************/
19+
20+
/****************************************************************************
21+
* Included Files
22+
****************************************************************************/
23+
24+
#include "mbedtls/error.h"
25+
#include "mbedtls/poly1305.h"
26+
27+
/****************************************************************************
28+
* Public Functions
29+
****************************************************************************/
30+
31+
void mbedtls_poly1305_init(FAR mbedtls_poly1305_context *ctx)
32+
{
33+
cryptodev_init(ctx);
34+
}
35+
36+
void mbedtls_poly1305_free(FAR mbedtls_poly1305_context *ctx)
37+
{
38+
cryptodev_free(ctx);
39+
}
40+
41+
int mbedtls_poly1305_starts(mbedtls_poly1305_context *ctx,
42+
const unsigned char key[32])
43+
{
44+
ctx->session.mac = CRYPTO_POLY1305;
45+
ctx->session.mackey = (caddr_t)key;
46+
ctx->session.mackeylen = 32;
47+
return cryptodev_get_session(ctx);
48+
}
49+
50+
int mbedtls_poly1305_update(FAR mbedtls_poly1305_context *ctx,
51+
FAR const unsigned char *input,
52+
size_t ilen)
53+
{
54+
ctx->crypt.op = COP_ENCRYPT;
55+
ctx->crypt.flags |= COP_FLAG_UPDATE;
56+
ctx->crypt.src = (caddr_t)input;
57+
ctx->crypt.len = ilen;
58+
return cryptodev_crypt(ctx);
59+
}
60+
61+
int mbedtls_poly1305_finish(FAR mbedtls_poly1305_context *ctx,
62+
unsigned char mac[16])
63+
{
64+
int ret;
65+
66+
ctx->crypt.op = COP_ENCRYPT;
67+
ctx->crypt.flags = 0;
68+
ctx->crypt.mac = (caddr_t)mac;
69+
ret = cryptodev_crypt(ctx);
70+
cryptodev_free_session(ctx);
71+
return ret;
72+
}
73+
74+
int mbedtls_poly1305_mac(const unsigned char key[32],
75+
FAR const unsigned char *input,
76+
size_t ilen,
77+
unsigned char mac[16])
78+
{
79+
mbedtls_poly1305_context ctx;
80+
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
81+
82+
mbedtls_poly1305_init(&ctx);
83+
84+
ret = mbedtls_poly1305_starts(&ctx, key);
85+
if (ret != 0)
86+
{
87+
goto cleanup;
88+
}
89+
90+
ret = mbedtls_poly1305_update(&ctx, input, ilen);
91+
if (ret != 0)
92+
{
93+
goto cleanup;
94+
}
95+
96+
ret = mbedtls_poly1305_finish(&ctx, mac);
97+
98+
cleanup:
99+
mbedtls_poly1305_free(&ctx);
100+
return ret;
101+
}

0 commit comments

Comments
 (0)