CycloneBOOT 2.6.6
Secure Bootloader for ARM Cortex-M MCU
Loading...
Searching...
No Matches
utils.h
Go to the documentation of this file.
1
29#ifndef __UTILS_H
30#define __UTILS_H
31
32#include <stdio.h>
33#include <stdlib.h>
34#include <string.h>
35#include "debug.h"
36#include "core/crypto.h"
37// Defines MAX_HASH_DIGEST_SIZE
38#include "hash/hash_algorithms.h"
39
40#if defined(VARIANT_OPEN) || defined(VARIANT_EVAL) || defined(VARIANT_ULTIMATE)
41#include "cipher_modes/cbc.h"
42#include "cipher/cipher_algorithms.h"
43#include "pkix/pem_key_export.h"
44#endif
45
46#include "main.h"
47#include "header.h"
48#include "body.h"
49
50#define ARRAY_BYTES(arr) (sizeof((arr)[0]) * ARRAY_SIZE(arr))
51#define ARRAY_SBYTES(arr) ((ssize_t)ARRAY_BYTES(arr))
52
53#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
54#define ARRAY_SSIZE(arr) ((ptrdiff_t)ARRAY_SIZE(arr))
55
56// Magic number used to check cipher key
57#define CIPHER_MAGIC_NUMBER "5ef41578fcfbb9a98ffc218dde463d44"
58#define CIPHER_MAGIC_NUMBER_SIZE 16
59
60// Cipher block size
61#define CIPHER_BLOCK_SIZE 16
62
63typedef struct {
64 ImageHeader *header;
65 ImageBody *body;
67
68int read_file(const char *file_path, char **file_contents, size_t *file_size);
69int blockify(size_t blockSize, char **input, size_t inputSize, char **output, size_t *outputSize);
70void imageBuildCleanup(ImageBuild *build);
71const HashAlgo *hash_algo_by_name(const char *name);
72int init_crypto(CipherInfo *cipherInfo);
73int encrypt(char *plainData, size_t plainDataSize, char *cipherData, CipherInfo cipherInfo);
74int sign(CipherInfo *cipherInfo, CheckDataInfo *checkDataInfo, char *data, size_t dataLen, char **signData, size_t *signDataLen);
75int write_image_to_file(UpdateImage *image, CipherInfo *cipherInfo, const char *output_file_path);
76int generate_key_pair(struct builder_cli_configuration *cli_options, CipherInfo *cipherInfo);
77
78void dump_buffer(void *buffer, size_t buffer_size);
79void dump_buffer_2(void *buffer, size_t buffer_size, size_t columSize);
80void dumpHeader(ImageHeader *header);
81void dumpBody(ImageBody *body);
82void dumpFooter(char *check_data, size_t check_data_size);
83int seedInitVector(uint8_t *buffer, size_t length);
84void dump_cleartext_image(void *buffer, size_t buffer_size);
85void dump_ciphertext_image(void *buffer, size_t buffer_size);
86uint32_t version_string_to_int(const char *version);
87void parse_firmware_version(const char *version, ImageVersion *imageVersion);
88
89int is_hex(const char *str);
90int hex_string_to_byte_array(const char *hexString, unsigned char **byteArray, size_t *byteArraySize);
91
92#endif
Generate the body section of an update image.
Generate the header section of an update image.
CycloneBOOT compatible Update Image Builder.
Definition main.h:63
Definition main.h:47
Definition body.h:37
Buffers the image passes through while it is built.
Definition main.h:81
Definition utils.h:63
Definition cli.h:53
int read_file(const char *file_path, char **file_contents, size_t *file_size)
Read a file.
Definition utils.c:103
void parse_firmware_version(const char *version, ImageVersion *imageVersion)
Parse a "MA.MI.RE.BUILD" firmware version, BUILD in hex.
Definition utils.c:1042
void imageBuildCleanup(ImageBuild *build)
Release all working buffers.
Definition utils.c:1071
const HashAlgo * hash_algo_by_name(const char *name)
Map a hash/checksum algorithm name to its HashAlgo.
Definition utils.c:75
int seedInitVector(uint8_t *buffer, size_t length)
Generate an initialization vector with random data.
Definition utils.c:316
int blockify(size_t blockSize, char **input, size_t inputSize, char **output, size_t *outputSize)
Slice a given buffer in to a blocks of a given size.
Definition utils.c:180
int write_image_to_file(UpdateImage *image, CipherInfo *cipherInfo, const char *output_file_path)
Write the update image to a file.
Definition utils.c:555