Skip to content

Commit e826064

Browse files
committed
crypto: make context a const pointer
1 parent bf2a886 commit e826064

File tree

2 files changed

+65
-9
lines changed

2 files changed

+65
-9
lines changed

gc/ogc/sha.h

Lines changed: 58 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,68 @@ typedef struct
4747
u32 lower_length;
4848
} sha_context;
4949

50+
/*!
51+
* \fn s32 SHA_Init(void)
52+
* \brief Initializes the SHA1 subsystem. This call could be done in the early stages of your main()
53+
*
54+
* \return 0 or higher on success, otherwise the returned error code
55+
*/
5056
s32 SHA_Init(void);
57+
58+
/*!
59+
* \fn s32 SHA_Close(void)
60+
* \brief Closes the SHA1 subsystem handlers. This call could be done when exiting your application or before reloading IOS
61+
*
62+
* \return 0 or higher on success, otherwise the returned error code
63+
*/
5164
s32 SHA_Close(void);
65+
66+
/*!
67+
* \fn s32 SHA_Calculate(const void* data, const u32 data_size, void* message_digest)
68+
* \brief Calculates the SHA1 hash of the given data, and puts it in message_digest
69+
*
70+
* \param[in] data pointer to the data to hash. if it is not 64-byte aligned an internal buffer will be used
71+
* \param[in] data_size size of the data to hash
72+
* \param[out] message_digest pointer to where to write the hash to
73+
*
74+
* \return 0 or higher on success, otherwise the returned error code
75+
*/
5276
s32 SHA_Calculate(const void* data, const u32 data_size, void* message_digest);
5377

54-
s32 SHA_InitializeContext(sha_context* context);
55-
s32 SHA_Input(sha_context* context, const void* data, const u32 data_size);
56-
s32 SHA_Finalize(sha_context* context, const void* data, const u32 data_size, void* message_digest);
78+
/*!
79+
* \fn s32 SHA_InitializeContext(sha_context* context)
80+
* \brief Initializes the given sha context
81+
*
82+
* \param[in] context pointer to the sha_context to initialize
83+
*
84+
* \return 0 or higher on success, otherwise the returned error code
85+
*/
86+
s32 SHA_InitializeContext(const sha_context* context);
87+
88+
/*!
89+
* \fn s32 SHA_Input(const sha_context* context, const void* data, const u32 data_size)
90+
* \brief Adds data to the given sha_context and hashes it
91+
*
92+
* \param[in] context pointer to the sha_context to use
93+
* \param[in] data pointer to the data to hash. if it is not 64-byte aligned an internal buffer will be used
94+
* \param[in] data_size size of the data to hash
95+
*
96+
* \return 0 or higher on success, otherwise the returned error code
97+
*/
98+
s32 SHA_Input(const sha_context* context, const void* data, const u32 data_size);
99+
100+
/*!
101+
* \fn s32 SHA_Finalize(const sha_context* context, const void* data, const u32 data_size, void* message_digest)
102+
* \brief Calculates the final SHA1 hash of the given context and last data, and puts it in message_digest
103+
*
104+
* \param[in] context pointer to the sha_context to use
105+
* \param[in] data pointer to the data to hash. if it is not 64-byte aligned an internal buffer will be used
106+
* \param[in] data_size size of the data to hash
107+
* \param[out] message_digest pointer to where to write the final SHA1 hash to
108+
*
109+
* \return 0 or higher on success, otherwise the returned error code
110+
*/
111+
s32 SHA_Finalize(const sha_context* context, const void* data, const u32 data_size, void* message_digest);
57112

58113
#ifdef __cplusplus
59114
}

libogc/sha.c

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ typedef enum
4646
} ShaCommand;
4747
static u8 input_buffer[0x1000] ATTRIBUTE_ALIGN(64);
4848

49-
static s32 SHA_ExecuteCommand(const ShaCommand command, sha_context* context, const void* in_data, const u32 data_size, void* out_data)
49+
static s32 SHA_ExecuteCommand(const ShaCommand command, const sha_context* context, const void* in_data, const u32 data_size, void* out_data)
5050
{
5151
ioctlv* params = (ioctlv*)iosAlloc(__sha_hid, sizeof(ioctlv) * 3);
5252
if(params == NULL)
@@ -127,7 +127,7 @@ s32 SHA_Close(void)
127127
return 0;
128128
}
129129

130-
s32 SHA_InitializeContext(sha_context* context)
130+
s32 SHA_InitializeContext(const sha_context* context)
131131
{
132132
if(context == NULL)
133133
return -1;
@@ -148,18 +148,19 @@ s32 SHA_InitializeContext(sha_context* context)
148148
return ret;
149149
}
150150

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

156-
if(((u32)context) & 0x1F)
157+
if(((u32)context) & 0x1F || data_size & 0x3F)
157158
return -4;
158159

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

162-
s32 SHA_Finalize(sha_context* context, const void* data, const u32 data_size, void* message_digest)
163+
s32 SHA_Finalize(const sha_context* context, const void* data, const u32 data_size, void* message_digest)
163164
{
164165
if(context == NULL || message_digest == NULL || data_size == 0 || data == NULL)
165166
return -1;
@@ -172,7 +173,7 @@ s32 SHA_Finalize(sha_context* context, const void* data, const u32 data_size, vo
172173

173174
s32 SHA_Calculate(const void* data, const u32 data_size, void* message_digest)
174175
{
175-
sha_context context ATTRIBUTE_ALIGN(32);
176+
sha_context context ATTRIBUTE_ALIGN(32) = {0};
176177
s32 ret = SHA_InitializeContext(&context);
177178
if(ret < 0)
178179
return ret;

0 commit comments

Comments
 (0)