-
-
Notifications
You must be signed in to change notification settings - Fork 8.3k
extmod/mbedtls: Support Alternative Functions Implemented in Python. #15905
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
2644f57
extmod/modtls_mbedtls: Add a thread-global ptr for current SSL context.
iabdalkader 68f1c20
extmod/modtls_mbedtls: Support alternate sign callbacks in Python.
iabdalkader bab6a01
lib/arduino-lib: Update submodule.
iabdalkader 28009a7
stm32/boards/ARDUINO_PORTENTA_H7: Add SE05x driver.
iabdalkader 57bc98f
stm32/boards/ARDUINO_NICLA_VISION: Add SE05x driver.
iabdalkader 4c54335
stm32/boards/ARDUINO_OPTA: Add Opta expansion module.
iabdalkader File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
/* | ||
* This file is part of the MicroPython project, http://micropython.org/ | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
iabdalkader marked this conversation as resolved.
Show resolved
Hide resolved
|
||
* | ||
* Copyright The Mbed TLS Contributors | ||
* Copyright (c) 2024 Damien P. George | ||
* | ||
* This file provides default fallback functions for use with alternate | ||
* cryptography functions implemented in Python. | ||
*/ | ||
#if MICROPY_PY_SSL_ECDSA_SIGN_ALT | ||
#if defined(MBEDTLS_ECP_RESTARTABLE) || defined(MBEDTLS_ECDSA_DETERMINISTIC) | ||
#error "MICROPY_PY_SSL_ECDSA_SIGN_ALT cannot be used with MBEDTLS_ECP_RESTARTABLE or MBEDTLS_ECDSA_DETERMINISTIC" | ||
#endif | ||
|
||
#include <string.h> | ||
#define MBEDTLS_ALLOW_PRIVATE_ACCESS | ||
#include "mbedtls/platform.h" | ||
#include "mbedtls/ssl.h" | ||
#include "mbedtls/error.h" | ||
#include "mbedtls/ecdsa.h" | ||
#include "mbedtls/asn1write.h" | ||
|
||
iabdalkader marked this conversation as resolved.
Show resolved
Hide resolved
|
||
extern int micropy_mbedtls_ecdsa_sign_alt(const mbedtls_mpi *d, const unsigned char *hash, size_t hlen, | ||
unsigned char *sig, size_t sig_size, size_t *slen); | ||
|
||
|
||
// Compute and write signature | ||
// See lib/mbedtls/library/ecdsa.c:688 | ||
// | ||
// Note: To avoid duplicating a lot of code, MBEDTLS_ECDSA_SIGN_ALT is not defined, | ||
// which allows the default mbedtls_ecdsa_sign to be used as a fallback function. | ||
// However, mbedtls_ecdsa_sign cannot be wrapped because it is called internally | ||
// within its object file, so we wrap mbedtls_ecdsa_read/write_signature instead. | ||
int __wrap_mbedtls_ecdsa_write_signature(mbedtls_ecdsa_context *ctx, | ||
mbedtls_md_type_t md_alg, | ||
const unsigned char *hash, size_t hlen, | ||
unsigned char *sig, size_t sig_size, size_t *slen, | ||
int (*f_rng)(void *, unsigned char *, size_t), | ||
void *p_rng) { | ||
|
||
(void)md_alg; | ||
|
||
if (f_rng == NULL) { | ||
return MBEDTLS_ERR_ECP_BAD_INPUT_DATA; | ||
} | ||
|
||
// Check if curve is supported for ECDSA. | ||
if (!mbedtls_ecdsa_can_do(ctx->grp.id) || ctx->grp.N.p == NULL) { | ||
return MBEDTLS_ERR_ECP_BAD_INPUT_DATA; | ||
} | ||
|
||
// Try signing with the alternative function first. | ||
int ret = micropy_mbedtls_ecdsa_sign_alt(&ctx->d, hash, hlen, sig, sig_size, slen); | ||
|
||
// Fallback to the default mbedtls implementation if needed. | ||
if (ret == MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED) { | ||
mbedtls_mpi r, s; | ||
mbedtls_mpi_init(&r); | ||
mbedtls_mpi_init(&s); | ||
|
||
iabdalkader marked this conversation as resolved.
Show resolved
Hide resolved
|
||
size_t len = 0; | ||
unsigned char buf[MBEDTLS_ECDSA_MAX_LEN] = { 0 }; | ||
unsigned char *p = buf + sizeof(buf); | ||
|
||
MBEDTLS_MPI_CHK(mbedtls_ecdsa_sign(&ctx->grp, &r, &s, &ctx->d, hash, hlen, f_rng, p_rng)); | ||
MBEDTLS_ASN1_CHK_CLEANUP_ADD(len, mbedtls_asn1_write_mpi(&p, buf, &s)); | ||
MBEDTLS_ASN1_CHK_CLEANUP_ADD(len, mbedtls_asn1_write_mpi(&p, buf, &r)); | ||
MBEDTLS_ASN1_CHK_CLEANUP_ADD(len, mbedtls_asn1_write_len(&p, buf, len)); | ||
MBEDTLS_ASN1_CHK_CLEANUP_ADD(len, mbedtls_asn1_write_tag(&p, buf, MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)); | ||
|
||
if (len > sig_size) { | ||
ret = MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL; | ||
} else { | ||
ret = 0; | ||
*slen = len; | ||
memcpy(sig, p, len); | ||
} | ||
|
||
cleanup: | ||
mbedtls_mpi_free(&r); | ||
mbedtls_mpi_free(&s); | ||
} | ||
|
||
return ret; | ||
} | ||
#endif |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Submodule arduino-lib
updated
10 files
+2 −2 | .github/workflows/python-linter.yml | |
+91 −0 | lib/opta/example.py | |
+6 −0 | lib/opta/manifest.py | |
+444 −0 | lib/opta/opta.py | |
+40 −0 | lib/se05x/examples/sign_verify.py | |
+94 −0 | lib/se05x/examples/write_read_verify.py | |
+6 −0 | lib/se05x/manifest.py | |
+22 −0 | lib/se05x/se05x/__init__.py | |
+389 −0 | lib/se05x/se05x/iso7816.py | |
+253 −0 | lib/se05x/se05x/se05x.py |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,3 +16,6 @@ | |
|
||
# RPC | ||
require("msgpackrpc", library="arduino-lib") | ||
|
||
# SE05x driver | ||
require("se05x", library="arduino-lib") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.