Skip to content

Commit

Permalink
Decoupling business layer from hardware layer in Signer component
Browse files Browse the repository at this point in the history
- Added HAL headers
- Added HAL implementation for Ledger
- Refactored necessary common sources
- Refactored necessary Signer sources
- Updated Signer Makefile to include HAL code
- Removed HSM_SIMULATOR define in favor of HSM_PLATFORM_* defines
- Included X86 (partial) implemenations of some HAL modules (exceptions, logging, hashing)
  • Loading branch information
amendelzon committed May 21, 2024
1 parent 5c19f28 commit 341a717
Show file tree
Hide file tree
Showing 55 changed files with 1,380 additions and 819 deletions.
30 changes: 18 additions & 12 deletions ledger/src/common/src/apdu.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
#ifndef __APDU_H
#define __APDU_H

#include "common_requirements.h"

// CLA for the entire protocol
#define CLA 0x80

Expand All @@ -40,25 +42,29 @@
#define CLAPOS 0

// APDU buffer getters
#define APDU_CLA() (G_io_apdu_buffer[CLAPOS])
#define APDU_CMD() (G_io_apdu_buffer[CMDPOS])
#define APDU_OP() (G_io_apdu_buffer[OP])
#define APDU_TXLEN() (G_io_apdu_buffer[TXLEN])
#define APDU_AT(pos) (G_io_apdu_buffer[pos])
#define APDU_CLA() (communication_get_msg_buffer()[CLAPOS])
#define APDU_CMD() (communication_get_msg_buffer()[CMDPOS])
#define APDU_OP() (communication_get_msg_buffer()[OP])
#define APDU_TXLEN() (communication_get_msg_buffer()[TXLEN])
#define APDU_AT(pos) (communication_get_msg_buffer()[pos])

// APDU buffer setters
#define SET_APDU_CLA() (G_io_apdu_buffer[CLAPOS] = CLA)
#define SET_APDU_CMD(cmd) (G_io_apdu_buffer[CMDPOS] = (cmd))
#define SET_APDU_OP(op) (G_io_apdu_buffer[OP] = (op))
#define SET_APDU_TXLEN(len) (G_io_apdu_buffer[TXLEN] = (len))
#define SET_APDU_AT(pos, value) (G_io_apdu_buffer[pos] = (value))
#define SET_APDU_CLA() (communication_get_msg_buffer()[CLAPOS] = CLA)
#define SET_APDU_CMD(cmd) (communication_get_msg_buffer()[CMDPOS] = (cmd))
#define SET_APDU_OP(op) (communication_get_msg_buffer()[OP] = (op))
#define SET_APDU_TXLEN(len) (communication_get_msg_buffer()[TXLEN] = (len))
#define SET_APDU_AT(pos, value) (communication_get_msg_buffer()[pos] = (value))

// Get pointer to payload within APDU buffer.
// No args, so it can be treated like an array pointer.
#define APDU_DATA_PTR (G_io_apdu_buffer + DATA)
#define APDU_DATA_PTR (communication_get_msg_buffer() + DATA)

// Total size of APDU
#define APDU_TOTAL_SIZE (communication_get_msg_buffer_size())
// Size of APDU elements
#define APDU_ELEMENT_SIZE (sizeof(communication_get_msg_buffer()[0]))
// Total size of APDU data part
#define APDU_TOTAL_DATA_SIZE (sizeof(G_io_apdu_buffer) - DATA)
#define APDU_TOTAL_DATA_SIZE (communication_get_msg_buffer_size() - DATA)
// Total size of APDU data part for outputting
// (need to leave space for result code)
#define APDU_RESULT_CODE_SIZE 2
Expand Down
5 changes: 2 additions & 3 deletions ledger/src/common/src/memutil.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,8 @@
#define __MEMUTIL_H

#include <stdint.h>
#include <stdbool.h>

#include "os.h"
#include "common_requirements.h"

#define MEMMOVE_ZERO_OFFSET 0

Expand All @@ -54,7 +53,7 @@ __attribute__((always_inline)) static inline int safe_memmove(

return false;
} else {
os_memmove(
platform_memmove(
(unsigned char *)dst + dst_off, (unsigned char *)src + src_off, n);
return true;
}
Expand Down
21 changes: 19 additions & 2 deletions ledger/src/common/src/runtime.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,27 @@
#ifndef __RUNTIME_H
#define __RUNTIME_H

#ifdef HSM_SIMULATOR
#if defined(HSM_PLATFORM_LEDGER)

// We can't include any HAL headers here because
// the Ledger UI does not know anything about it
#include "os.h"

#define NON_VOLATILE const

#elif defined(HSM_PLATFORM_X86)

#include "hal/platform.h"
#include "hal/exceptions.h"

#include "ui_deps.h"

#define PIC(x) (x)

#define NON_VOLATILE

#else
#define NON_VOLATILE const
#error "HSM Platform undefined"
#endif

#endif // __RUNTIME_H
75 changes: 75 additions & 0 deletions ledger/src/hal/include/hal/communication.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/**
* The MIT License (MIT)
*
* Copyright (c) 2021 RSK Labs Ltd
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to
* deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*/

#ifndef __HAL_COMMUNICATION_H
#define __HAL_COMMUNICATION_H

#include <stddef.h>
#include <stdint.h>
#include <stdbool.h>

/**
* @brief Initializes the communication module
*
* @param msg_buffer The buffer to use for communication
* @param msg_buffer_size The size of the message buffer in bytes
*
* @returns whether the initialisation succeeded
*/
bool communication_init(unsigned char* msg_buffer, size_t msg_buffer_size);

/**
* @brief Get a pointer to the message buffer
*
* @returns a pointer to the message buffer
*/
unsigned char* communication_get_msg_buffer();

/**
* @brief Get the message buffer size
*
* @returns the message buffer size
*/
size_t communication_get_msg_buffer_size();

/**
* @brief Exchanges bytes with the host. This function blocks until the host sends a
* message.
*
* The message exchanges data with the host using the msg_buffer. If there are any bytes
* to transmit, they are transmitted first. After that the function blocks until a new
* message is received from the host.
*
* @param tx The number of bytes sent to the host
*
* @returns the number of bytes received from the host
*/
unsigned short communication_io_exchange(unsigned short tx);

/**
* @brief Finalizes the communication module
*/
void communication_finalize(void);

#endif // __HAL_COMMUNICATION_H
71 changes: 71 additions & 0 deletions ledger/src/hal/include/hal/endorsement.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/**
* The MIT License (MIT)
*
* Copyright (c) 2021 RSK Labs Ltd
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to
* deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*/

#ifndef __HAL_ENDORSEMENT_H
#define __HAL_ENDORSEMENT_H

#include <stddef.h>
#include <stdint.h>
#include <stdbool.h>

/**
* @brief Endorses the given message
*
* @param msg The message to attest
* @param msg_size The size of the message to attest
* @param signature_out Where the signature should be output
* @param signature_out_length [in/out] the length of the output buffer /
* length of the produced signature
*
* @returns whether endorsement succeeded
*/
bool endorsement_sign(uint8_t* msg, size_t msg_size,
uint8_t* signature_out,
uint8_t* signature_out_length);

/**
* @brief Grabs the hash of the currently running code
*
* @param code_hash_out Where the code hash should be output
* @param code_hash_out_length [in/out] the length of the output buffer /
* length of the produced code hash
*
* @returns whether code hash gathering succeeded
*/
bool endorsement_get_code_hash(uint8_t* code_hash_out,
uint8_t* code_hash_out_length);

/**
* @brief Grabs the endorsement public key
*
* @param public_key_out Where the public key should be output
* @param public_key_out_length [in/out] the length of the output buffer /
* length of the produced public key
*
* @returns whether public key gathering succeeded
*/
bool endorsement_get_public_key(uint8_t* public_key_out,
uint8_t* public_key_out_length);

#endif // __HAL_ENDORSEMENT_H
Loading

0 comments on commit 341a717

Please sign in to comment.