Commit f72158ff authored by Dominik Ermel's avatar Dominik Ermel Committed by Andrzej Puzdrowski
Browse files

bootutil: Allow SHA512 with TinyCrypt



The commit enables SHA512 support, for image hashing, with
TinyCrypt.
Although on 32bit machines the SHA256 will be faster than
SHA512, benefit of enabling the SHA512 is that you have only
one algorithm compiled in which reduces size of code.

Signed-off-by: default avatarDominik Ermel <dominik.ermel@nordicsemi.no>
parent 4b8a37bb
Loading
Loading
Loading
Loading
+20 −0
Original line number Diff line number Diff line
@@ -69,7 +69,11 @@
#endif /* MCUBOOT_USE_MBED_TLS */

#if defined(MCUBOOT_USE_TINYCRYPT)
#if defined(MCUBOOT_SHA512)
    #include <tinycrypt/sha512.h>
#else
    #include <tinycrypt/sha256.h>
#endif
    #include <tinycrypt/constants.h>
#endif /* MCUBOOT_USE_TINYCRYPT */

@@ -192,11 +196,19 @@ static inline int bootutil_sha_finish(bootutil_sha_context *ctx,
#endif /* MCUBOOT_USE_MBED_TLS */

#if defined(MCUBOOT_USE_TINYCRYPT)
#if defined(MCUBOOT_SHA512)
typedef struct tc_sha512_state_struct bootutil_sha_context;
#else
typedef struct tc_sha256_state_struct bootutil_sha_context;
#endif

static inline int bootutil_sha_init(bootutil_sha_context *ctx)
{
#if defined(MCUBOOT_SHA512)
    tc_sha512_init(ctx);
#else
    tc_sha256_init(ctx);
#endif
    return 0;
}

@@ -210,13 +222,21 @@ static inline int bootutil_sha_update(bootutil_sha_context *ctx,
                                      const void *data,
                                      uint32_t data_len)
{
#if defined(MCUBOOT_SHA512)
    return tc_sha512_update(ctx, data, data_len);
#else
    return tc_sha256_update(ctx, data, data_len);
#endif
}

static inline int bootutil_sha_finish(bootutil_sha_context *ctx,
                                      uint8_t *output)
{
#if defined(MCUBOOT_SHA512)
    return tc_sha512_final(output, ctx);
#else
    return tc_sha256_final(output, ctx);
#endif
}
#endif /* MCUBOOT_USE_TINYCRYPT */

+6 −3
Original line number Diff line number Diff line
@@ -21,12 +21,14 @@
#include "bootutil/crypto/common.h"
#include "bootutil/crypto/sha.h"

#define EDDSA_SIGNATURE_LENGTH 64

static const uint8_t ed25519_pubkey_oid[] = MBEDTLS_OID_ISO_IDENTIFIED_ORG "\x65\x70";
#define NUM_ED25519_BYTES 32

extern int ED25519_verify(const uint8_t *message, size_t message_len,
                          const uint8_t signature[64],
                          const uint8_t public_key[32]);
                          const uint8_t signature[EDDSA_SIGNATURE_LENGTH],
                          const uint8_t public_key[NUM_ED25519_BYTES]);

/*
 * Parse the public key used for signing.
@@ -76,7 +78,8 @@ bootutil_verify_sig(uint8_t *hash, uint32_t hlen, uint8_t *sig, size_t slen,
    uint8_t *pubkey;
    uint8_t *end;

    if (hlen != IMAGE_HASH_SIZE || slen != 64) {
    if (hlen != IMAGE_HASH_SIZE ||
        slen != EDDSA_SIGNATURE_LENGTH) {
        FIH_SET(fih_rc, FIH_FAILURE);
        goto out;
    }