Skip to content

Commit

Permalink
crypto: make context a const pointer
Browse files Browse the repository at this point in the history
  • Loading branch information
DacoTaco committed Dec 22, 2024
1 parent bf2a886 commit e826064
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 9 deletions.
61 changes: 58 additions & 3 deletions gc/ogc/sha.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,68 @@ typedef struct
u32 lower_length;
} sha_context;

/*!
* \fn s32 SHA_Init(void)
* \brief Initializes the SHA1 subsystem. This call could be done in the early stages of your main()
*
* \return 0 or higher on success, otherwise the returned error code
*/
s32 SHA_Init(void);

/*!
* \fn s32 SHA_Close(void)
* \brief Closes the SHA1 subsystem handlers. This call could be done when exiting your application or before reloading IOS
*
* \return 0 or higher on success, otherwise the returned error code
*/
s32 SHA_Close(void);

/*!
* \fn s32 SHA_Calculate(const void* data, const u32 data_size, void* message_digest)
* \brief Calculates the SHA1 hash of the given data, and puts it in message_digest
*
* \param[in] data pointer to the data to hash. if it is not 64-byte aligned an internal buffer will be used
* \param[in] data_size size of the data to hash
* \param[out] message_digest pointer to where to write the hash to
*
* \return 0 or higher on success, otherwise the returned error code
*/
s32 SHA_Calculate(const void* data, const u32 data_size, void* message_digest);

s32 SHA_InitializeContext(sha_context* context);
s32 SHA_Input(sha_context* context, const void* data, const u32 data_size);
s32 SHA_Finalize(sha_context* context, const void* data, const u32 data_size, void* message_digest);
/*!
* \fn s32 SHA_InitializeContext(sha_context* context)
* \brief Initializes the given sha context
*
* \param[in] context pointer to the sha_context to initialize
*
* \return 0 or higher on success, otherwise the returned error code
*/
s32 SHA_InitializeContext(const sha_context* context);

/*!
* \fn s32 SHA_Input(const sha_context* context, const void* data, const u32 data_size)
* \brief Adds data to the given sha_context and hashes it
*
* \param[in] context pointer to the sha_context to use
* \param[in] data pointer to the data to hash. if it is not 64-byte aligned an internal buffer will be used
* \param[in] data_size size of the data to hash
*
* \return 0 or higher on success, otherwise the returned error code
*/
s32 SHA_Input(const sha_context* context, const void* data, const u32 data_size);

/*!
* \fn s32 SHA_Finalize(const sha_context* context, const void* data, const u32 data_size, void* message_digest)
* \brief Calculates the final SHA1 hash of the given context and last data, and puts it in message_digest
*
* \param[in] context pointer to the sha_context to use
* \param[in] data pointer to the data to hash. if it is not 64-byte aligned an internal buffer will be used
* \param[in] data_size size of the data to hash
* \param[out] message_digest pointer to where to write the final SHA1 hash to
*
* \return 0 or higher on success, otherwise the returned error code
*/
s32 SHA_Finalize(const sha_context* context, const void* data, const u32 data_size, void* message_digest);

#ifdef __cplusplus
}
Expand Down
13 changes: 7 additions & 6 deletions libogc/sha.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ typedef enum
} ShaCommand;
static u8 input_buffer[0x1000] ATTRIBUTE_ALIGN(64);

static s32 SHA_ExecuteCommand(const ShaCommand command, sha_context* context, const void* in_data, const u32 data_size, void* out_data)
static s32 SHA_ExecuteCommand(const ShaCommand command, const sha_context* context, const void* in_data, const u32 data_size, void* out_data)
{
ioctlv* params = (ioctlv*)iosAlloc(__sha_hid, sizeof(ioctlv) * 3);
if(params == NULL)
Expand Down Expand Up @@ -127,7 +127,7 @@ s32 SHA_Close(void)
return 0;
}

s32 SHA_InitializeContext(sha_context* context)
s32 SHA_InitializeContext(const sha_context* context)
{
if(context == NULL)
return -1;
Expand All @@ -148,18 +148,19 @@ s32 SHA_InitializeContext(sha_context* context)
return ret;
}

s32 SHA_Input(sha_context* context, const void* data, const u32 data_size)
s32 SHA_Input(const sha_context* context, const void* data, const u32 data_size)
{
//when adding data, it should be in 64-byte blocks.
if(context == NULL || data == NULL || data_size == 0)
return -1;

if(((u32)context) & 0x1F)
if(((u32)context) & 0x1F || data_size & 0x3F)
return -4;

return SHA_ExecuteCommand(AddData, context, data, data_size, NULL);
}

s32 SHA_Finalize(sha_context* context, const void* data, const u32 data_size, void* message_digest)
s32 SHA_Finalize(const sha_context* context, const void* data, const u32 data_size, void* message_digest)
{
if(context == NULL || message_digest == NULL || data_size == 0 || data == NULL)
return -1;
Expand All @@ -172,7 +173,7 @@ s32 SHA_Finalize(sha_context* context, const void* data, const u32 data_size, vo

s32 SHA_Calculate(const void* data, const u32 data_size, void* message_digest)
{
sha_context context ATTRIBUTE_ALIGN(32);
sha_context context ATTRIBUTE_ALIGN(32) = {0};
s32 ret = SHA_InitializeContext(&context);
if(ret < 0)
return ret;
Expand Down

0 comments on commit e826064

Please sign in to comment.