Skip to content

Commit 08fdbb0

Browse files
ThePassionatexiaoxiang781216
authored andcommitted
mbedtls-alt: aligned alternative implementation return value with mbedtls
mbedtls interfaces overwritten by nuttx crypto driver, change return value of mbedtls interfaces from return value of nuttx crypto driver into starndard return value of mbedtls Signed-off-by: makejian <[email protected]>
1 parent 4ecdb92 commit 08fdbb0

File tree

5 files changed

+89
-9
lines changed

5 files changed

+89
-9
lines changed

crypto/mbedtls/source/cmac_alt.c

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,12 @@ int mbedtls_cipher_cmac_update(FAR mbedtls_cipher_context_t *ctx,
107107
ctx->cmac_ctx->dev.crypt.flags |= COP_FLAG_UPDATE;
108108
ctx->cmac_ctx->dev.crypt.src = (caddr_t)input;
109109
ctx->cmac_ctx->dev.crypt.len = ilen;
110-
return cryptodev_crypt(&ctx->cmac_ctx->dev);
110+
if (cryptodev_crypt(&ctx->cmac_ctx->dev) != 0)
111+
{
112+
return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
113+
}
114+
115+
return 0;
111116
}
112117

113118
int mbedtls_cipher_cmac_finish(FAR mbedtls_cipher_context_t *ctx,
@@ -123,6 +128,11 @@ int mbedtls_cipher_cmac_finish(FAR mbedtls_cipher_context_t *ctx,
123128
ctx->cmac_ctx->dev.crypt.flags = 0;
124129
ctx->cmac_ctx->dev.crypt.mac = (caddr_t)output;
125130
ret = cryptodev_crypt(&ctx->cmac_ctx->dev);
131+
if (ret != 0)
132+
{
133+
ret = MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
134+
}
135+
126136
cryptodev_free_session(&ctx->cmac_ctx->dev);
127137
cryptodev_free(&ctx->cmac_ctx->dev);
128138
return ret;

crypto/mbedtls/source/poly1305_alt.c

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,12 @@ int mbedtls_poly1305_starts(FAR mbedtls_poly1305_context *ctx,
4646
ctx->session.mac = CRYPTO_POLY1305;
4747
ctx->session.mackey = (caddr_t)key;
4848
ctx->session.mackeylen = 32;
49-
return cryptodev_get_session(ctx);
49+
if (cryptodev_get_session(ctx) != 0)
50+
{
51+
return MBEDTLS_ERR_POLY1305_BAD_INPUT_DATA;
52+
}
53+
54+
return 0;
5055
}
5156

5257
int mbedtls_poly1305_update(FAR mbedtls_poly1305_context *ctx,
@@ -57,7 +62,12 @@ int mbedtls_poly1305_update(FAR mbedtls_poly1305_context *ctx,
5762
ctx->crypt.flags |= COP_FLAG_UPDATE;
5863
ctx->crypt.src = (caddr_t)input;
5964
ctx->crypt.len = ilen;
60-
return cryptodev_crypt(ctx);
65+
if (cryptodev_crypt(ctx) != 0)
66+
{
67+
return MBEDTLS_ERR_POLY1305_BAD_INPUT_DATA;
68+
}
69+
70+
return 0;
6171
}
6272

6373
int mbedtls_poly1305_finish(FAR mbedtls_poly1305_context *ctx,
@@ -69,6 +79,11 @@ int mbedtls_poly1305_finish(FAR mbedtls_poly1305_context *ctx,
6979
ctx->crypt.flags = 0;
7080
ctx->crypt.mac = (caddr_t)mac;
7181
ret = cryptodev_crypt(ctx);
82+
if (ret != 0)
83+
{
84+
ret = MBEDTLS_ERR_POLY1305_BAD_INPUT_DATA;
85+
}
86+
7287
cryptodev_free_session(ctx);
7388
return ret;
7489
}

crypto/mbedtls/source/sha1_alt.c

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,12 @@ void mbedtls_sha1_free(FAR mbedtls_sha1_context *ctx)
4848
int mbedtls_sha1_starts(FAR mbedtls_sha1_context *ctx)
4949
{
5050
ctx->session.mac = CRYPTO_SHA1;
51-
return cryptodev_get_session(ctx);
51+
if (cryptodev_get_session(ctx) != 0)
52+
{
53+
return MBEDTLS_ERR_SHA1_BAD_INPUT_DATA;
54+
}
55+
56+
return 0;
5257
}
5358

5459
int mbedtls_sha1_update(FAR mbedtls_sha1_context *ctx,
@@ -59,7 +64,12 @@ int mbedtls_sha1_update(FAR mbedtls_sha1_context *ctx,
5964
ctx->crypt.flags |= COP_FLAG_UPDATE;
6065
ctx->crypt.src = (caddr_t)input;
6166
ctx->crypt.len = ilen;
62-
return cryptodev_crypt(ctx);
67+
if (cryptodev_crypt(ctx) != 0)
68+
{
69+
return MBEDTLS_ERR_SHA1_BAD_INPUT_DATA;
70+
}
71+
72+
return 0;
6373
}
6474

6575
int mbedtls_sha1_finish(FAR mbedtls_sha1_context *ctx,
@@ -71,6 +81,11 @@ int mbedtls_sha1_finish(FAR mbedtls_sha1_context *ctx,
7181
ctx->crypt.flags = 0;
7282
ctx->crypt.mac = (caddr_t)output;
7383
ret = cryptodev_crypt(ctx);
84+
if (ret != 0)
85+
{
86+
ret = MBEDTLS_ERR_SHA1_BAD_INPUT_DATA;
87+
}
88+
7489
cryptodev_free_session(ctx);
7590
return ret;
7691
}

crypto/mbedtls/source/sha256_alt.c

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,11 @@ void mbedtls_sha256_free(FAR mbedtls_sha256_context *ctx)
4747

4848
int mbedtls_sha256_starts(FAR mbedtls_sha256_context *ctx, int is224)
4949
{
50+
if (is224 != 0 && is224 != 1)
51+
{
52+
return MBEDTLS_ERR_SHA256_BAD_INPUT_DATA;
53+
}
54+
5055
if (is224)
5156
{
5257
ctx->session.mac = CRYPTO_SHA2_224;
@@ -56,7 +61,12 @@ int mbedtls_sha256_starts(FAR mbedtls_sha256_context *ctx, int is224)
5661
ctx->session.mac = CRYPTO_SHA2_256;
5762
}
5863

59-
return cryptodev_get_session(ctx);
64+
if (cryptodev_get_session(ctx) != 0)
65+
{
66+
return MBEDTLS_ERR_SHA256_BAD_INPUT_DATA;
67+
}
68+
69+
return 0;
6070
}
6171

6272
int mbedtls_sha256_update(FAR mbedtls_sha256_context *ctx,
@@ -67,7 +77,12 @@ int mbedtls_sha256_update(FAR mbedtls_sha256_context *ctx,
6777
ctx->crypt.flags |= COP_FLAG_UPDATE;
6878
ctx->crypt.src = (caddr_t)input;
6979
ctx->crypt.len = ilen;
70-
return cryptodev_crypt(ctx);
80+
if (cryptodev_crypt(ctx) != 0)
81+
{
82+
return MBEDTLS_ERR_SHA256_BAD_INPUT_DATA;
83+
}
84+
85+
return 0;
7186
}
7287

7388
int mbedtls_sha256_finish(FAR mbedtls_sha256_context *ctx,
@@ -79,6 +94,11 @@ int mbedtls_sha256_finish(FAR mbedtls_sha256_context *ctx,
7994
ctx->crypt.flags = 0;
8095
ctx->crypt.mac = (caddr_t)output;
8196
ret = cryptodev_crypt(ctx);
97+
if (ret != 0)
98+
{
99+
ret = MBEDTLS_ERR_SHA256_BAD_INPUT_DATA;
100+
}
101+
82102
cryptodev_free_session(ctx);
83103
return ret;
84104
}

crypto/mbedtls/source/sha512_alt.c

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,11 @@ void mbedtls_sha512_free(FAR mbedtls_sha512_context *ctx)
4747

4848
int mbedtls_sha512_starts(FAR mbedtls_sha512_context *ctx, int is384)
4949
{
50+
if (is384 != 0 && is384 != 1)
51+
{
52+
return MBEDTLS_ERR_SHA512_BAD_INPUT_DATA;
53+
}
54+
5055
if (is384)
5156
{
5257
ctx->session.mac = CRYPTO_SHA2_384;
@@ -56,7 +61,12 @@ int mbedtls_sha512_starts(FAR mbedtls_sha512_context *ctx, int is384)
5661
ctx->session.mac = CRYPTO_SHA2_512;
5762
}
5863

59-
return cryptodev_get_session(ctx);
64+
if (cryptodev_get_session(ctx) != 0)
65+
{
66+
return MBEDTLS_ERR_SHA512_BAD_INPUT_DATA;
67+
}
68+
69+
return 0;
6070
}
6171

6272
int mbedtls_sha512_update(FAR mbedtls_sha512_context *ctx,
@@ -67,7 +77,12 @@ int mbedtls_sha512_update(FAR mbedtls_sha512_context *ctx,
6777
ctx->crypt.flags |= COP_FLAG_UPDATE;
6878
ctx->crypt.src = (caddr_t)input;
6979
ctx->crypt.len = ilen;
70-
return cryptodev_crypt(ctx);
80+
if (cryptodev_crypt(ctx) != 0)
81+
{
82+
return MBEDTLS_ERR_SHA512_BAD_INPUT_DATA;
83+
}
84+
85+
return 0;
7186
}
7287

7388
int mbedtls_sha512_finish(FAR mbedtls_sha512_context *ctx,
@@ -79,6 +94,11 @@ int mbedtls_sha512_finish(FAR mbedtls_sha512_context *ctx,
7994
ctx->crypt.flags = 0;
8095
ctx->crypt.mac = (caddr_t)output;
8196
ret = cryptodev_crypt(ctx);
97+
if (ret != 0)
98+
{
99+
ret = MBEDTLS_ERR_SHA512_BAD_INPUT_DATA;
100+
}
101+
82102
cryptodev_free_session(ctx);
83103
return ret;
84104
}

0 commit comments

Comments
 (0)