This commit is contained in:
HailoRT-Automation
2022-03-29 19:08:05 +03:00
commit dd37bf9936
370 changed files with 80735 additions and 0 deletions

View File

@@ -0,0 +1,279 @@
/**
* Copyright (c) 2020-2022 Hailo Technologies Ltd. All rights reserved.
* Distributed under the MIT license (https://opensource.org/licenses/MIT)
**/
/**
* @file firmware_header_utils.c
* @brief Utilities for working with the firmware header.
**/
#include <stdint.h>
#include <stddef.h>
#include <stdbool.h>
#include "firmware_header.h"
#include "firmware_version.h"
#include "firmware_header_utils.h"
#include "utils.h"
#include "control_protocol.h"
/* when reading the firmware we don't want to read past the firmware_size,
so we have a consumed_firmware_offset that is updated _before_ accessing data at that offset
of firmware_base_address */
#define CONSUME_FIRMWARE(__size, __status) do { \
consumed_firmware_offset += (uint32_t) (__size); \
if ((firmware_size < (__size)) || (firmware_size < consumed_firmware_offset)) { \
status = __status; \
goto exit; \
} \
} while(0)
static HAILO_COMMON_STATUS_t firmware_header_utils__validate_fw_header(uintptr_t firmware_base_address,
uint32_t firmware_size,
uint32_t max_code_size,
uint32_t *outer_consumed_firmware_offset,
firmware_header_t **out_firmware_header,
firmware_type_t firmware_type)
{
HAILO_COMMON_STATUS_t status = HAILO_COMMON_STATUS__UNINITIALIZED;
firmware_header_t *firmware_header = NULL;
uint32_t consumed_firmware_offset = *outer_consumed_firmware_offset;
uint32_t firmware_magic = 0;
firmware_header = (firmware_header_t *) (firmware_base_address + consumed_firmware_offset);
CONSUME_FIRMWARE(sizeof(firmware_header_t), HAILO_STATUS__FIRMWARE_HEADER_UTILS__INVALID_FIRMWARE_HEADER_SIZE);
switch (firmware_type) {
case FIRMWARE_TYPE_HAILO8:
firmware_magic = FIRMWARE_HEADER_MAGIC_HAILO8;
break;
case FIRMWARE_TYPE_MERCURY:
firmware_magic = FIRMWARE_HEADER_MAGIC_MERCURY;
break;
default:
status = HAILO_STATUS__FIRMWARE_HEADER_UTILS__INVALID_FIRMWARE_TYPE;
goto exit;
}
if (firmware_magic != firmware_header->magic) {
status = HAILO_STATUS__FIRMWARE_HEADER_UTILS__INCORRECT_FIRMWARE_HEADER_MAGIC;
goto exit;
}
/* Validate that the firmware header version is supported */
switch(firmware_header->header_version) {
case FIRMWARE_HEADER_VERSION_INITIAL:
break;
default:
status = HAILO_STATUS__FIRMWARE_HEADER_UTILS__UNSUPPORTED_FIRMWARE__HEADER_VERSION;
goto exit;
break;
}
if (MINIMUM_FIRMWARE_CODE_SIZE > firmware_header->code_size) {
status = HAILO_STATUS__FIRMWARE_HEADER_UTILS__CODE_SIZE_BELOW_MINIMUM;
goto exit;
}
if (max_code_size < firmware_header->code_size) {
status = HAILO_STATUS__FIRMWARE_HEADER_UTILS__CODE_OVERRUNS_RAM_SIZE;
goto exit;
}
CONSUME_FIRMWARE(firmware_header->code_size, HAILO_STATUS__FIRMWARE_HEADER_UTILS__INVALID_FIRMWARE_CODE_SIZE);
*outer_consumed_firmware_offset = consumed_firmware_offset;
*out_firmware_header = firmware_header;
status = HAILO_COMMON_STATUS__SUCCESS;
exit:
return status;
}
static HAILO_COMMON_STATUS_t firmware_header_utils__validate_cert_header(uintptr_t firmware_base_address,
uint32_t firmware_size,
uint32_t *outer_consumed_firmware_offset,
secure_boot_certificate_t **out_firmware_cert)
{
secure_boot_certificate_t *firmware_cert = NULL;
HAILO_COMMON_STATUS_t status = HAILO_COMMON_STATUS__UNINITIALIZED;
uint32_t consumed_firmware_offset = *outer_consumed_firmware_offset;
firmware_cert = (secure_boot_certificate_t *) (firmware_base_address + consumed_firmware_offset);
CONSUME_FIRMWARE(sizeof(secure_boot_certificate_t), HAILO_STATUS__FIRMWARE_HEADER_UTILS__INVALID_CERT_HEADER_SIZE);
if ((MAXIMUM_FIRMWARE_CERT_KEY_SIZE < firmware_cert->key_size) ||
(MAXIMUM_FIRMWARE_CERT_CONTENT_SIZE < firmware_cert->content_size)) {
status = HAILO_STATUS__FIRMWARE_HEADER_UTILS__CERT_TOO_LARGE;
goto exit;
}
CONSUME_FIRMWARE(firmware_cert->key_size, HAILO_STATUS__FIRMWARE_HEADER_UTILS__INVALID_CERT_KEY_SIZE);
CONSUME_FIRMWARE(firmware_cert->content_size, HAILO_STATUS__FIRMWARE_HEADER_UTILS__INVALID_CERT_CONTENT_SIZE);
*outer_consumed_firmware_offset = consumed_firmware_offset;
*out_firmware_cert = firmware_cert;
status = HAILO_COMMON_STATUS__SUCCESS;
exit:
return status;
}
HAILO_COMMON_STATUS_t FIRMWARE_HEADER_UTILS__validate_fw_headers(uintptr_t firmware_base_address,
uint32_t firmware_size,
bool is_firmware_size_unknown,
firmware_header_t **out_app_firmware_header,
firmware_header_t **out_core_firmware_header,
secure_boot_certificate_t **out_firmware_cert,
firmware_type_t firmware_type)
{
firmware_header_t *app_firmware_header = NULL;
firmware_header_t *core_firmware_header = NULL;
secure_boot_certificate_t *firmware_cert = NULL;
HAILO_COMMON_STATUS_t status = HAILO_COMMON_STATUS__UNINITIALIZED;
uint32_t consumed_firmware_offset = 0;
status = firmware_header_utils__validate_fw_header(firmware_base_address, firmware_size, MAXIMUM_APP_FIRMWARE_CODE_SIZE,
&consumed_firmware_offset, &app_firmware_header, firmware_type);
if (HAILO_COMMON_STATUS__SUCCESS != status) {
status = HAILO_STATUS__FIRMWARE_HEADER_UTILS__INVALID_APP_CPU_FIRMWARE_HEADER;
goto exit;
}
status = firmware_header_utils__validate_cert_header(firmware_base_address, firmware_size,
&consumed_firmware_offset, &firmware_cert);
if (HAILO_COMMON_STATUS__SUCCESS != status) {
status = HAILO_STATUS__FIRMWARE_HEADER_UTILS__INVALID_APP_CPU_FIRMWARE_CERTIFICATE_HEADER;
goto exit;
}
status = firmware_header_utils__validate_fw_header(firmware_base_address, firmware_size, MAXIMUM_CORE_FIRMWARE_CODE_SIZE,
&consumed_firmware_offset, &core_firmware_header, firmware_type);
if (HAILO_COMMON_STATUS__SUCCESS != status) {
status = HAILO_STATUS__FIRMWARE_HEADER_UTILS__INVALID_CORE_CPU_FIRMWARE_HEADER;
goto exit;
}
if ((consumed_firmware_offset != firmware_size) && (!is_firmware_size_unknown)) {
/* it is an error if there is leftover data after the last firmware header */
status = HAILO_STATUS__FIRMWARE_HEADER_UTILS__LEFTOVER_DATA_AFTER_LAST_FIRMWARE_HEADER;
goto exit;
}
/* the out params are all optional */
if (NULL != out_app_firmware_header) {
*out_app_firmware_header = app_firmware_header;
}
if (NULL != out_firmware_cert) {
*out_firmware_cert = firmware_cert;
}
if (NULL != out_core_firmware_header) {
*out_core_firmware_header = core_firmware_header;
}
status = HAILO_COMMON_STATUS__SUCCESS;
exit:
return status;
}
HAILO_COMMON_STATUS_t FIRMWARE_HEADER_UTILS__validate_second_stage_headers(uintptr_t second_stage_base_size,
uint32_t second_stage_size,
firmware_header_t **out_second_stage_header,
firmware_type_t firmware_type)
{
firmware_header_t *second_stage_header = NULL;
secure_boot_certificate_t *second_stage_cert = NULL;
HAILO_COMMON_STATUS_t status = HAILO_COMMON_STATUS__UNINITIALIZED;
uint32_t consumed_second_stage_offset = 0;
status = firmware_header_utils__validate_fw_header(second_stage_base_size, second_stage_size, MAXIMUM_SECOND_STAGE_CODE_SIZE,
&consumed_second_stage_offset, &second_stage_header, firmware_type);
if (HAILO_COMMON_STATUS__SUCCESS != status) {
status = HAILO_STATUS__FIRMWARE_HEADER_UTILS__INVALID_APP_CPU_FIRMWARE_HEADER;
goto exit;
}
status = firmware_header_utils__validate_cert_header(second_stage_base_size, second_stage_size,
&consumed_second_stage_offset, &second_stage_cert);
if (HAILO_COMMON_STATUS__SUCCESS != status) {
status = HAILO_STATUS__FIRMWARE_HEADER_UTILS__INVALID_APP_CPU_FIRMWARE_CERTIFICATE_HEADER;
goto exit;
}
if (consumed_second_stage_offset != second_stage_size) {
/* it is an error if there is leftover data after the last firmware header */
status = HAILO_STATUS__FIRMWARE_HEADER_UTILS__LEFTOVER_DATA_AFTER_LAST_FIRMWARE_HEADER;
goto exit;
}
/* the out params are all optional */
if (NULL != out_second_stage_header) {
*out_second_stage_header = second_stage_header;
}
status = HAILO_COMMON_STATUS__SUCCESS;
exit:
return status;
}
FW_BINARY_TYPE_t FIRMWARE_HEADER_UTILS__get_fw_binary_type(uint32_t binary_revision)
{
FW_BINARY_TYPE_t fw_binary_type = FW_BINARY_TYPE_INVALID;
// Remove dev flag before checking binary type
binary_revision &= ~(REVISION_DEV_FLAG_BIT_MASK);
if (REVISION_SECOND_STAGE_FLAG_BIT_MASK == (binary_revision & REVISION_SECOND_STAGE_FLAG_BIT_MASK)) {
fw_binary_type = FW_BINARY_TYPE_SECOND_STAGE_BOOT;
} else if (0 == (binary_revision & (REVISION_APP_CORE_FLAG_BIT_MASK))) {
fw_binary_type = FW_BINARY_TYPE_APP_FIRMWARE;
} else if (REVISION_APP_CORE_FLAG_BIT_MASK == (binary_revision & (REVISION_APP_CORE_FLAG_BIT_MASK))) {
fw_binary_type = FW_BINARY_TYPE_CORE_FIRMWARE;
} else {
fw_binary_type = FW_BINARY_TYPE_INVALID;
}
return fw_binary_type;
}
HAILO_COMMON_STATUS_t FIRMWARE_HEADER_UTILS__is_binary_being_downgraded(const firmware_version_t *new_binary_version,
const firmware_version_t *minimum_allowed_binary_version)
{
bool is_binary_being_downgraded =
// Check if minimum allowed binary's major is greater than new binary's major
(minimum_allowed_binary_version->firmware_major > new_binary_version->firmware_major) ||
// Check if minimum allowed binary's minor is greater than new binary's minor (If major is the same)
((minimum_allowed_binary_version->firmware_major == new_binary_version->firmware_major) &&
(minimum_allowed_binary_version->firmware_minor > new_binary_version->firmware_minor)) ||
// Check if minimum allowed binary's revision is greater than new binary's revision (If major and minor are the same)
((minimum_allowed_binary_version->firmware_major == new_binary_version->firmware_major) &&
(minimum_allowed_binary_version->firmware_minor == new_binary_version->firmware_minor) &&
(GET_REVISION_NUMBER_VALUE(minimum_allowed_binary_version->firmware_revision) >
(GET_REVISION_NUMBER_VALUE(new_binary_version->firmware_revision))));
return is_binary_being_downgraded;
}
HAILO_COMMON_STATUS_t FIRMWARE_HEADER_UTILS__validate_binary_version(const firmware_version_t *new_binary_version,
const firmware_version_t *minimum_allowed_binary_version,
FW_BINARY_TYPE_t fw_binary_type)
{
HAILO_COMMON_STATUS_t status = HAILO_COMMON_STATUS__UNINITIALIZED;
// Make sure downgrade is not executed
if (FIRMWARE_HEADER_UTILS__is_binary_being_downgraded(new_binary_version, minimum_allowed_binary_version)) {
status = HAILO_STATUS__FIRMWARE_HEADER_UTILS__DETECTED_PROHIBITED_DOWNGRADE_ATTEMPT;
goto exit;
}
if (FIRMWARE_HEADER_UTILS__get_fw_binary_type(new_binary_version->firmware_revision) != fw_binary_type) {
status = HAILO_STATUS__FIRMWARE_HEADER_UTILS__INVALID_BINARY_TYPE;
goto exit;
}
status = HAILO_COMMON_STATUS__SUCCESS;
exit:
return status;
}

View File

@@ -0,0 +1,135 @@
/**
* Copyright (c) 2020-2022 Hailo Technologies Ltd. All rights reserved.
* Distributed under the MIT license (https://opensource.org/licenses/MIT)
**/
/**
* @file firmware_status.c
* @brief Defines firmware status codes.
**/
#include <stdint.h>
#include "firmware_status.h"
#include <string.h>
#ifdef FIRMWARE_ARCH
#pragma pack(push, 1)
typedef struct {
const char *status_name;
uint32_t status_id;
} FIRMWARE_STATUS__status_record_t;
typedef struct {
const char *module_name;
uint32_t module_id;
} FIRMWARE_STATUS__module_record_t;
#pragma pack(pop)
#define FIRMWARE_STATUS_SECTION __attribute__((section(".firmware_statuses")))
#define FIRMWARE_MODULE__X(module) static const char module##_str[] FIRMWARE_STATUS_SECTION = #module;
#define FIRMWARE_STATUS__X(name) static const char name##_str[] FIRMWARE_STATUS_SECTION = #name;
FIRMWARE_STATUS__VARIABLES
#undef FIRMWARE_STATUS__X
#undef FIRMWARE_MODULE__X
const FIRMWARE_STATUS__module_record_t FIRMWARE_STATUS__module_records[] FIRMWARE_STATUS_SECTION = {
#define FIRMWARE_MODULE__X(module) { .module_id = module, .module_name = module##_str },
#define FIRMWARE_STATUS__X(name)
FIRMWARE_STATUS__VARIABLES
#undef FIRMWARE_STATUS__X
#undef FIRMWARE_MODULE__X
};
const FIRMWARE_STATUS__status_record_t FIRMWARE_STATUS__status_records[] FIRMWARE_STATUS_SECTION = {
#define FIRMWARE_MODULE__X(module)
#define FIRMWARE_STATUS__X(name) { .status_id = name, .status_name = name##_str },
FIRMWARE_STATUS__VARIABLES
#undef FIRMWARE_STATUS__X
#undef FIRMWARE_MODULE__X
};
#endif
#ifndef FIRMWARE_ARCH
static const char *FIRMWARE_STATUS__textual_format[] =
{
#define FIRMWARE_MODULE__X(module)
#define FIRMWARE_STATUS__X(name) #name,
FIRMWARE_STATUS__VARIABLES
#undef FIRMWARE_STATUS__X
#undef FIRMWARE_MODULE__X
};
/* The FIRMWARE_STATUS__textual_format array stores the strings in "absolute" order.
In order for us to know the absolute index of each status we store an array that for each module stores
the absolute index of it's first status.
This way we can compute the absolute index in O(1) time. */
#define HELPER_INDEX_NAME(__name) __HELPER_FIRMWARE_STATUS__##__name
/* the helper indices counts all module names and statuses.
the goal here is to be able to count how many statuses occured prior to each module.
we mark the module starts with the module__START elements, and in order
to calculate the number of statuses prior to the current module, we take the
module's module__START value, and subtract the number of __START element
of previous modules, which is the enum value of the module. */
typedef enum {
#define FIRMWARE_MODULE__X(module) HELPER_INDEX_NAME(module##__START),
#define FIRMWARE_STATUS__X(name) HELPER_INDEX_NAME(name),
FIRMWARE_STATUS__VARIABLES
#undef FIRMWARE_STATUS__X
#undef FIRMWARE_MODULE__X
} __FIRMWARE_STATUS__helper_indices_t;
static const uint32_t FIRMWARE_STATUS__absolute_module_indices[] = {
#define FIRMWARE_MODULE__X(module) HELPER_INDEX_NAME(module##__START) - module,
#define FIRMWARE_STATUS__X(name)
FIRMWARE_STATUS__VARIABLES
#undef FIRMWARE_STATUS__X
#undef FIRMWARE_MODULE__X
ARRAY_LENGTH(FIRMWARE_STATUS__textual_format)
};
HAILO_COMMON_STATUS_t FIRMWARE_STATUS__get_textual(FIRMWARE_STATUS_t fw_status, const char **text)
{
HAILO_COMMON_STATUS_t status = HAILO_COMMON_STATUS__UNINITIALIZED;
uint32_t module_id = 0;
uint32_t module_abs_index = 0;
uint32_t next_module_abs_index = 0;
uint32_t status_value = 0;
if (NULL == text) {
status = HAILO_STATUS__FIRMWARE_STATUS__NULL_ARGUMENT_PASSED;
goto exit;
}
if (FIRMWARE_STATUS__COMPONENT_ID != FIRMWARE_STATUS__COMPONENT_GET(fw_status)) {
status = HAILO_STATUS__FIRMWARE_STATUS__INVALID_COMPONENT_ID;
goto exit;
}
module_id = FIRMWARE_STATUS__MODULE_INDEX_GET(fw_status);
if (FIRMWARE_MODULE_COUNT <= module_id) {
status = HAILO_STATUS__FIRMWARE_STATUS__INVALID_MODULE_ID;
goto exit;
}
module_abs_index = FIRMWARE_STATUS__absolute_module_indices[module_id];
next_module_abs_index = FIRMWARE_STATUS__absolute_module_indices[module_id+1];
status_value = (uint32_t)FIRMWARE_STATUS__VALUE_GET(fw_status) - 1; /* status values start at 1 */
/* check status value is in the correct range */
if (status_value >= next_module_abs_index - module_abs_index) {
status = HAILO_STATUS__FIRMWARE_STATUS__INVALID_STATUS_VALUE;
goto exit;
}
*text = FIRMWARE_STATUS__textual_format[module_abs_index + status_value];
status = HAILO_COMMON_STATUS__SUCCESS;
exit:
return status;
}
#endif

291
common/src/md5.c Normal file
View File

@@ -0,0 +1,291 @@
/*
* This is an OpenSSL-compatible implementation of the RSA Data Security, Inc.
* MD5 Message-Digest Algorithm (RFC 1321).
*
* Homepage:
* http://openwall.info/wiki/people/solar/software/public-domain-source-code/md5
*
* Author:
* Alexander Peslyak, better known as Solar Designer <solar at openwall.com>
*
* This software was written by Alexander Peslyak in 2001. No copyright is
* claimed, and the software is hereby placed in the public domain.
* In case this attempt to disclaim copyright and place the software in the
* public domain is deemed null and void, then the software is
* Copyright (c) 2001 Alexander Peslyak and it is hereby released to the
* general public under the following terms:
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted.
*
* There's ABSOLUTELY NO WARRANTY, express or implied.
*
* (This is a heavily cut-down "BSD license".)
*
* This differs from Colin Plumb's older public domain implementation in that
* no exactly 32-bit integer data type is required (any 32-bit or wider
* unsigned integer data type will do), there's no compile-time endianness
* configuration, and the function prototypes match OpenSSL's. No code from
* Colin Plumb's implementation has been reused; this comment merely compares
* the properties of the two independent implementations.
*
* The primary goals of this implementation are portability and ease of use.
* It is meant to be fast, but not as fast as possible. Some known
* optimizations are not included to reduce source code size and avoid
* compile-time configuration.
*/
#ifndef HAVE_OPENSSL
#include <string.h>
#include "md5.h"
/*
* The basic MD5 functions.
*
* F and G are optimized compared to their RFC 1321 definitions for
* architectures that lack an AND-NOT instruction, just like in Colin Plumb's
* implementation.
*/
#define F(x, y, z) ((z) ^ ((x) & ((y) ^ (z))))
#define G(x, y, z) ((y) ^ ((z) & ((x) ^ (y))))
#define H(x, y, z) (((x) ^ (y)) ^ (z))
#define H2(x, y, z) ((x) ^ ((y) ^ (z)))
#define I(x, y, z) ((y) ^ ((x) | ~(z)))
/*
* The MD5 transformation for all four rounds.
*/
#define STEP(f, a, b, c, d, x, t, s) \
(a) += f((b), (c), (d)) + (x) + (t); \
(a) = (((a) << (s)) | (((a) & 0xffffffff) >> (32 - (s)))); \
(a) += (b);
/*
* SET reads 4 input bytes in little-endian byte order and stores them in a
* properly aligned word in host byte order.
*
* The check for little-endian architectures that tolerate unaligned memory
* accesses is just an optimization. Nothing will break if it fails to detect
* a suitable architecture.
*
* Unfortunately, this optimization may be a C strict aliasing rules violation
* if the caller's data buffer has effective type that cannot be aliased by
* MD5_u32plus. In practice, this problem may occur if these MD5 routines are
* inlined into a calling function, or with future and dangerously advanced
* link-time optimizations. For the time being, keeping these MD5 routines in
* their own translation unit avoids the problem.
*/
#if defined(__i386__) || defined(__x86_64__) || defined(__vax__)
#define SET(n) \
(*(MD5_u32plus *)&ptr[(n) * 4])
#define GET(n) \
SET(n)
#else
#define SET(n) \
(ctx->block[(n)] = \
(MD5_u32plus)ptr[(n) * 4] | \
((MD5_u32plus)ptr[(n) * 4 + 1] << 8) | \
((MD5_u32plus)ptr[(n) * 4 + 2] << 16) | \
((MD5_u32plus)ptr[(n) * 4 + 3] << 24))
#define GET(n) \
(ctx->block[(n)])
#endif
/*
* This processes one or more 64-byte data blocks, but does NOT update the bit
* counters. There are no alignment requirements.
*/
static const void *body(MD5_CTX *ctx, const void *data, size_t size)
{
const unsigned char *ptr;
MD5_u32plus a, b, c, d;
MD5_u32plus saved_a, saved_b, saved_c, saved_d;
ptr = (const unsigned char *)data;
a = ctx->a;
b = ctx->b;
c = ctx->c;
d = ctx->d;
do {
saved_a = a;
saved_b = b;
saved_c = c;
saved_d = d;
/* Round 1 */
STEP(F, a, b, c, d, SET(0), 0xd76aa478, 7)
STEP(F, d, a, b, c, SET(1), 0xe8c7b756, 12)
STEP(F, c, d, a, b, SET(2), 0x242070db, 17)
STEP(F, b, c, d, a, SET(3), 0xc1bdceee, 22)
STEP(F, a, b, c, d, SET(4), 0xf57c0faf, 7)
STEP(F, d, a, b, c, SET(5), 0x4787c62a, 12)
STEP(F, c, d, a, b, SET(6), 0xa8304613, 17)
STEP(F, b, c, d, a, SET(7), 0xfd469501, 22)
STEP(F, a, b, c, d, SET(8), 0x698098d8, 7)
STEP(F, d, a, b, c, SET(9), 0x8b44f7af, 12)
STEP(F, c, d, a, b, SET(10), 0xffff5bb1, 17)
STEP(F, b, c, d, a, SET(11), 0x895cd7be, 22)
STEP(F, a, b, c, d, SET(12), 0x6b901122, 7)
STEP(F, d, a, b, c, SET(13), 0xfd987193, 12)
STEP(F, c, d, a, b, SET(14), 0xa679438e, 17)
STEP(F, b, c, d, a, SET(15), 0x49b40821, 22)
/* Round 2 */
STEP(G, a, b, c, d, GET(1), 0xf61e2562, 5)
STEP(G, d, a, b, c, GET(6), 0xc040b340, 9)
STEP(G, c, d, a, b, GET(11), 0x265e5a51, 14)
STEP(G, b, c, d, a, GET(0), 0xe9b6c7aa, 20)
STEP(G, a, b, c, d, GET(5), 0xd62f105d, 5)
STEP(G, d, a, b, c, GET(10), 0x02441453, 9)
STEP(G, c, d, a, b, GET(15), 0xd8a1e681, 14)
STEP(G, b, c, d, a, GET(4), 0xe7d3fbc8, 20)
STEP(G, a, b, c, d, GET(9), 0x21e1cde6, 5)
STEP(G, d, a, b, c, GET(14), 0xc33707d6, 9)
STEP(G, c, d, a, b, GET(3), 0xf4d50d87, 14)
STEP(G, b, c, d, a, GET(8), 0x455a14ed, 20)
STEP(G, a, b, c, d, GET(13), 0xa9e3e905, 5)
STEP(G, d, a, b, c, GET(2), 0xfcefa3f8, 9)
STEP(G, c, d, a, b, GET(7), 0x676f02d9, 14)
STEP(G, b, c, d, a, GET(12), 0x8d2a4c8a, 20)
/* Round 3 */
STEP(H, a, b, c, d, GET(5), 0xfffa3942, 4)
STEP(H2, d, a, b, c, GET(8), 0x8771f681, 11)
STEP(H, c, d, a, b, GET(11), 0x6d9d6122, 16)
STEP(H2, b, c, d, a, GET(14), 0xfde5380c, 23)
STEP(H, a, b, c, d, GET(1), 0xa4beea44, 4)
STEP(H2, d, a, b, c, GET(4), 0x4bdecfa9, 11)
STEP(H, c, d, a, b, GET(7), 0xf6bb4b60, 16)
STEP(H2, b, c, d, a, GET(10), 0xbebfbc70, 23)
STEP(H, a, b, c, d, GET(13), 0x289b7ec6, 4)
STEP(H2, d, a, b, c, GET(0), 0xeaa127fa, 11)
STEP(H, c, d, a, b, GET(3), 0xd4ef3085, 16)
STEP(H2, b, c, d, a, GET(6), 0x04881d05, 23)
STEP(H, a, b, c, d, GET(9), 0xd9d4d039, 4)
STEP(H2, d, a, b, c, GET(12), 0xe6db99e5, 11)
STEP(H, c, d, a, b, GET(15), 0x1fa27cf8, 16)
STEP(H2, b, c, d, a, GET(2), 0xc4ac5665, 23)
/* Round 4 */
STEP(I, a, b, c, d, GET(0), 0xf4292244, 6)
STEP(I, d, a, b, c, GET(7), 0x432aff97, 10)
STEP(I, c, d, a, b, GET(14), 0xab9423a7, 15)
STEP(I, b, c, d, a, GET(5), 0xfc93a039, 21)
STEP(I, a, b, c, d, GET(12), 0x655b59c3, 6)
STEP(I, d, a, b, c, GET(3), 0x8f0ccc92, 10)
STEP(I, c, d, a, b, GET(10), 0xffeff47d, 15)
STEP(I, b, c, d, a, GET(1), 0x85845dd1, 21)
STEP(I, a, b, c, d, GET(8), 0x6fa87e4f, 6)
STEP(I, d, a, b, c, GET(15), 0xfe2ce6e0, 10)
STEP(I, c, d, a, b, GET(6), 0xa3014314, 15)
STEP(I, b, c, d, a, GET(13), 0x4e0811a1, 21)
STEP(I, a, b, c, d, GET(4), 0xf7537e82, 6)
STEP(I, d, a, b, c, GET(11), 0xbd3af235, 10)
STEP(I, c, d, a, b, GET(2), 0x2ad7d2bb, 15)
STEP(I, b, c, d, a, GET(9), 0xeb86d391, 21)
a += saved_a;
b += saved_b;
c += saved_c;
d += saved_d;
ptr += 64;
} while (size -= 64);
ctx->a = a;
ctx->b = b;
ctx->c = c;
ctx->d = d;
return ptr;
}
void MD5_Init(MD5_CTX *ctx)
{
ctx->a = 0x67452301;
ctx->b = 0xefcdab89;
ctx->c = 0x98badcfe;
ctx->d = 0x10325476;
ctx->lo = 0;
ctx->hi = 0;
}
void MD5_Update(MD5_CTX *ctx, const void *data, size_t size)
{
MD5_u32plus saved_lo;
size_t used, available;
saved_lo = ctx->lo;
if ((ctx->lo = (saved_lo + size) & 0x1fffffff) < saved_lo)
ctx->hi++;
ctx->hi += size >> 29;
used = saved_lo & 0x3f;
if (used) {
available = 64 - used;
if (size < available) {
memcpy(&ctx->buffer[used], data, size);
return;
}
memcpy(&ctx->buffer[used], data, available);
data = (const unsigned char *)data + available;
size -= available;
body(ctx, ctx->buffer, 64);
}
if (size >= 64) {
data = body(ctx, data, size & ~(size_t)0x3f);
size &= 0x3f;
}
memcpy(ctx->buffer, data, size);
}
#define OUT(dst, src) \
(dst)[0] = (unsigned char)(src); \
(dst)[1] = (unsigned char)((src) >> 8); \
(dst)[2] = (unsigned char)((src) >> 16); \
(dst)[3] = (unsigned char)((src) >> 24);
void MD5_Final(unsigned char *result, MD5_CTX *ctx)
{
unsigned long used, available;
used = ctx->lo & 0x3f;
ctx->buffer[used++] = 0x80;
available = 64 - used;
if (available < 8) {
memset(&ctx->buffer[used], 0, available);
body(ctx, ctx->buffer, 64);
used = 0;
available = 64;
}
memset(&ctx->buffer[used], 0, available - 8);
ctx->lo <<= 3;
OUT(&ctx->buffer[56], ctx->lo)
OUT(&ctx->buffer[60], ctx->hi)
body(ctx, ctx->buffer, 64);
OUT(&result[0], ctx->a)
OUT(&result[4], ctx->b)
OUT(&result[8], ctx->c)
OUT(&result[12], ctx->d)
memset(ctx, 0, sizeof(*ctx));
}
#endif