From acff29e6f269a96e071cfb3a3df21db14e087ce2 Mon Sep 17 00:00:00 2001 From: ccli8 Date: Thu, 21 Dec 2017 16:05:55 +0800 Subject: [PATCH] [NUC472/M487] Fix context clone corner case in SHA alter. As destination/source contexts are the same, we return immediately. --- .../targets/TARGET_NUVOTON/TARGET_M480/sha/sha1_alt.c | 5 +++++ .../targets/TARGET_NUVOTON/TARGET_M480/sha/sha256_alt.c | 5 +++++ .../targets/TARGET_NUVOTON/TARGET_M480/sha/sha512_alt.c | 5 +++++ .../targets/TARGET_NUVOTON/TARGET_NUC472/sha/sha1_alt.c | 5 +++++ .../targets/TARGET_NUVOTON/TARGET_NUC472/sha/sha256_alt.c | 5 +++++ 5 files changed, 25 insertions(+) diff --git a/features/mbedtls/targets/TARGET_NUVOTON/TARGET_M480/sha/sha1_alt.c b/features/mbedtls/targets/TARGET_NUVOTON/TARGET_M480/sha/sha1_alt.c index 761c8c33d9..35d7122610 100644 --- a/features/mbedtls/targets/TARGET_NUVOTON/TARGET_M480/sha/sha1_alt.c +++ b/features/mbedtls/targets/TARGET_NUVOTON/TARGET_M480/sha/sha1_alt.c @@ -63,6 +63,11 @@ void mbedtls_sha1_free(mbedtls_sha1_context *ctx) void mbedtls_sha1_clone(mbedtls_sha1_context *dst, const mbedtls_sha1_context *src) { + // Corner case: Destination/source contexts are the same + if (dst == src) { + return; + } + // If dst is H/W context, we need to change it to S/W context first before cloning to. if (dst->active_ctx == &dst->hw_ctx) { mbedtls_sha1_free(dst); diff --git a/features/mbedtls/targets/TARGET_NUVOTON/TARGET_M480/sha/sha256_alt.c b/features/mbedtls/targets/TARGET_NUVOTON/TARGET_M480/sha/sha256_alt.c index 09ec8e4335..c4e27dfa5f 100644 --- a/features/mbedtls/targets/TARGET_NUVOTON/TARGET_M480/sha/sha256_alt.c +++ b/features/mbedtls/targets/TARGET_NUVOTON/TARGET_M480/sha/sha256_alt.c @@ -63,6 +63,11 @@ void mbedtls_sha256_free(mbedtls_sha256_context *ctx) void mbedtls_sha256_clone(mbedtls_sha256_context *dst, const mbedtls_sha256_context *src) { + // Corner case: Destination/source contexts are the same + if (dst == src) { + return; + } + // If dst is H/W context, we need to change it to S/W context first before cloning to. if (dst->active_ctx == &dst->hw_ctx) { mbedtls_sha256_free(dst); diff --git a/features/mbedtls/targets/TARGET_NUVOTON/TARGET_M480/sha/sha512_alt.c b/features/mbedtls/targets/TARGET_NUVOTON/TARGET_M480/sha/sha512_alt.c index ce25a5e861..48baf9132f 100644 --- a/features/mbedtls/targets/TARGET_NUVOTON/TARGET_M480/sha/sha512_alt.c +++ b/features/mbedtls/targets/TARGET_NUVOTON/TARGET_M480/sha/sha512_alt.c @@ -63,6 +63,11 @@ void mbedtls_sha512_free(mbedtls_sha512_context *ctx) void mbedtls_sha512_clone(mbedtls_sha512_context *dst, const mbedtls_sha512_context *src) { + // Corner case: Destination/source contexts are the same + if (dst == src) { + return; + } + // If dst is H/W context, we need to change it to S/W context first before cloning to. if (dst->active_ctx == &dst->hw_ctx) { mbedtls_sha512_free(dst); diff --git a/features/mbedtls/targets/TARGET_NUVOTON/TARGET_NUC472/sha/sha1_alt.c b/features/mbedtls/targets/TARGET_NUVOTON/TARGET_NUC472/sha/sha1_alt.c index 761c8c33d9..35d7122610 100644 --- a/features/mbedtls/targets/TARGET_NUVOTON/TARGET_NUC472/sha/sha1_alt.c +++ b/features/mbedtls/targets/TARGET_NUVOTON/TARGET_NUC472/sha/sha1_alt.c @@ -63,6 +63,11 @@ void mbedtls_sha1_free(mbedtls_sha1_context *ctx) void mbedtls_sha1_clone(mbedtls_sha1_context *dst, const mbedtls_sha1_context *src) { + // Corner case: Destination/source contexts are the same + if (dst == src) { + return; + } + // If dst is H/W context, we need to change it to S/W context first before cloning to. if (dst->active_ctx == &dst->hw_ctx) { mbedtls_sha1_free(dst); diff --git a/features/mbedtls/targets/TARGET_NUVOTON/TARGET_NUC472/sha/sha256_alt.c b/features/mbedtls/targets/TARGET_NUVOTON/TARGET_NUC472/sha/sha256_alt.c index 57ed6d5b3c..b576d7a767 100644 --- a/features/mbedtls/targets/TARGET_NUVOTON/TARGET_NUC472/sha/sha256_alt.c +++ b/features/mbedtls/targets/TARGET_NUVOTON/TARGET_NUC472/sha/sha256_alt.c @@ -63,6 +63,11 @@ void mbedtls_sha256_free(mbedtls_sha256_context *ctx) void mbedtls_sha256_clone(mbedtls_sha256_context *dst, const mbedtls_sha256_context *src) { + // Corner case: Destination/source contexts are the same + if (dst == src) { + return; + } + // If dst is H/W context, we need to change it to S/W context first before cloning to. if (dst->active_ctx == &dst->hw_ctx) { mbedtls_sha256_free(dst);