CycloneBOOT 2.6.6
Secure Bootloader for ARM Cortex-M MCU
Loading...
Searching...
No Matches
utils.h File Reference

Utility functions used throughout the application. More...

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "debug.h"
#include "core/crypto.h"
#include "hash/hash_algorithms.h"
#include "main.h"
#include "header.h"
#include "body.h"

Go to the source code of this file.

Classes

struct  UpdateImage
 

Macros

#define ARRAY_BYTES(arr)   (sizeof((arr)[0]) * ARRAY_SIZE(arr))
 
#define ARRAY_SBYTES(arr)   ((ssize_t)ARRAY_BYTES(arr))
 
#define ARRAY_SIZE(arr)   (sizeof(arr) / sizeof((arr)[0]))
 
#define ARRAY_SSIZE(arr)   ((ptrdiff_t)ARRAY_SIZE(arr))
 
#define CIPHER_MAGIC_NUMBER   "5ef41578fcfbb9a98ffc218dde463d44"
 
#define CIPHER_MAGIC_NUMBER_SIZE   16
 
#define CIPHER_BLOCK_SIZE   16
 

Functions

int read_file (const char *file_path, char **file_contents, size_t *file_size)
 Read a file.
 
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.
 
void imageBuildCleanup (ImageBuild *build)
 Release all working buffers.
 
const HashAlgo * hash_algo_by_name (const char *name)
 Map a hash/checksum algorithm name to its HashAlgo.
 
int init_crypto (CipherInfo *cipherInfo)
 
int encrypt (char *plainData, size_t plainDataSize, char *cipherData, CipherInfo cipherInfo)
 
int sign (CipherInfo *cipherInfo, CheckDataInfo *checkDataInfo, char *data, size_t dataLen, char **signData, size_t *signDataLen)
 
int write_image_to_file (UpdateImage *image, CipherInfo *cipherInfo, const char *output_file_path)
 Write the update image to a file.
 
int generate_key_pair (struct builder_cli_configuration *cli_options, CipherInfo *cipherInfo)
 
void dump_buffer (void *buffer, size_t buffer_size)
 
void dump_buffer_2 (void *buffer, size_t buffer_size, size_t columSize)
 
void dumpHeader (ImageHeader *header)
 
void dumpBody (ImageBody *body)
 
void dumpFooter (char *check_data, size_t check_data_size)
 
int seedInitVector (uint8_t *buffer, size_t length)
 Generate an initialization vector with random data.
 
void dump_cleartext_image (void *buffer, size_t buffer_size)
 
void dump_ciphertext_image (void *buffer, size_t buffer_size)
 
uint32_t version_string_to_int (const char *version)
 
void parse_firmware_version (const char *version, ImageVersion *imageVersion)
 Parse a "MA.MI.RE.BUILD" firmware version, BUILD in hex.
 
int is_hex (const char *str)
 
int hex_string_to_byte_array (const char *hexString, unsigned char **byteArray, size_t *byteArraySize)
 

Detailed Description

Utility functions used throughout the application.

License

SPDX-License-Identifier: GPL-2.0-or-later

Copyright (C) 2021-2026 Oryx Embedded SARL. All rights reserved.

This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.

Author
Oryx Embedded SARL (www.oryx-embedded.com)
Version
2.6.6

Function Documentation

◆ blockify()

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.

Parameters
[in]blockSizeSize of the blocks
[in]inputInput buffer
[in]inputSizeSize of the input buffer
[in]outputOutput buffer
[in]outputSizeSize of the output buffer
Returns
Status code

◆ hash_algo_by_name()

const HashAlgo * hash_algo_by_name ( const char *  name)

Map a hash/checksum algorithm name to its HashAlgo.

Parameters
[in]nameAlgorithm name: crc32, md5, sha1, sha224, sha256, sha384 or sha512.
Returns
Pointer to the const HashAlgo, or NULL if the name is unknown.

◆ parse_firmware_version()

void parse_firmware_version ( const char *  version,
ImageVersion *  imageVersion 
)

Parse a "MA.MI.RE.BUILD" firmware version, BUILD in hex.

Parameters
[in]versionFirmware version string
[out]imageVersionParsed version

◆ read_file()

int read_file ( const char *  file_path,
char **  file_contents,
size_t *  file_size 
)

Read a file.

Parameters
[in]file_pathPath to the file to be read
[in]file_contentsBuffer to store content of the file read
[in]file_sizePointer to the file size
Returns
Status code

◆ seedInitVector()

int seedInitVector ( uint8_t *  buffer,
size_t  length 
)

Generate an initialization vector with random data.

Parameters
[in]bufferBuffer containing initialization vector
[in]lengthBuffer length
Returns
Status code

◆ write_image_to_file()

int write_image_to_file ( UpdateImage *  image,
CipherInfo *  cipherInfo,
const char *  output_file_path 
)

Write the update image to a file.

Parameters
[in]imagePointer containing the update image to be rewritten to disk
[in]cipherInfoCrypto related information
[in]output_file_pathPath to write the image
Returns
Status code