Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
stenzek
GitHub Repository: stenzek/duckstation
Path: blob/master/dep/rcheevos/include/rc_hash.h
4246 views
1
#ifndef RC_HASH_H
2
#define RC_HASH_H
3
4
#include <stddef.h>
5
#include <stdio.h>
6
#include <stdint.h>
7
8
#include "rc_consoles.h"
9
10
RC_BEGIN_C_DECLS
11
12
/* ===================================================== */
13
14
/* specifies a function to call when an error occurs to display the error message */
15
typedef void (RC_CCONV *rc_hash_message_callback)(const char*);
16
17
/* [deprecated] set callbacks in rc_hash_iterator_t */
18
RC_EXPORT void RC_CCONV rc_hash_init_error_message_callback(rc_hash_message_callback callback);
19
20
/* specifies a function to call for verbose logging */
21
/* [deprecated] set callbacks in rc_hash_iterator_t */
22
RC_EXPORT void rc_hash_init_verbose_message_callback(rc_hash_message_callback callback);
23
24
/* ===================================================== */
25
26
/* opens a file */
27
typedef void* (RC_CCONV *rc_hash_filereader_open_file_handler)(const char* path_utf8);
28
29
/* moves the file pointer - standard fseek parameters */
30
typedef void (RC_CCONV *rc_hash_filereader_seek_handler)(void* file_handle, int64_t offset, int origin);
31
32
/* locates the file pointer */
33
typedef int64_t (RC_CCONV *rc_hash_filereader_tell_handler)(void* file_handle);
34
35
/* reads the specified number of bytes from the file starting at the read pointer.
36
* returns the number of bytes actually read.
37
*/
38
typedef size_t (RC_CCONV *rc_hash_filereader_read_handler)(void* file_handle, void* buffer, size_t requested_bytes);
39
40
/* closes the file */
41
typedef void (RC_CCONV *rc_hash_filereader_close_file_handler)(void* file_handle);
42
43
typedef struct rc_hash_filereader
44
{
45
rc_hash_filereader_open_file_handler open;
46
rc_hash_filereader_seek_handler seek;
47
rc_hash_filereader_tell_handler tell;
48
rc_hash_filereader_read_handler read;
49
rc_hash_filereader_close_file_handler close;
50
} rc_hash_filereader_t;
51
52
/* [deprecated] set callbacks in rc_hash_iterator_t */
53
RC_EXPORT void RC_CCONV rc_hash_init_custom_filereader(struct rc_hash_filereader* reader);
54
55
/* ===================================================== */
56
57
#define RC_HASH_CDTRACK_FIRST_DATA ((uint32_t)-1) /* the first data track (skip audio tracks) */
58
#define RC_HASH_CDTRACK_LAST ((uint32_t)-2) /* the last data/audio track */
59
#define RC_HASH_CDTRACK_LARGEST ((uint32_t)-3) /* the largest data/audio track */
60
#define RC_HASH_CDTRACK_FIRST_OF_SECOND_SESSION ((uint32_t)-4) /* the first data/audio track of the second session */
61
62
/* opens a track from the specified file. see the RC_HASH_CDTRACK_ defines for special tracks.
63
* returns a handle to be passed to the other functions, or NULL if the track could not be opened.
64
*/
65
typedef void* (RC_CCONV *rc_hash_cdreader_open_track_handler)(const char* path, uint32_t track);
66
typedef void* (RC_CCONV* rc_hash_cdreader_open_track_filereader_handler)(const char* path, uint32_t track, const rc_hash_filereader_t* filereader);
67
68
/* attempts to read the specified number of bytes from the file starting at the specified absolute sector.
69
* returns the number of bytes actually read.
70
*/
71
typedef size_t (RC_CCONV *rc_hash_cdreader_read_sector_handler)(void* track_handle, uint32_t sector, void* buffer, size_t requested_bytes);
72
73
/* closes the track handle */
74
typedef void (RC_CCONV *rc_hash_cdreader_close_track_handler)(void* track_handle);
75
76
/* gets the absolute sector index for the first sector of a track */
77
typedef uint32_t(RC_CCONV *rc_hash_cdreader_first_track_sector_handler)(void* track_handle);
78
79
typedef struct rc_hash_cdreader
80
{
81
rc_hash_cdreader_open_track_handler open_track;
82
rc_hash_cdreader_read_sector_handler read_sector;
83
rc_hash_cdreader_close_track_handler close_track;
84
rc_hash_cdreader_first_track_sector_handler first_track_sector;
85
rc_hash_cdreader_open_track_filereader_handler open_track_filereader;
86
} rc_hash_cdreader_t;
87
88
RC_EXPORT void RC_CCONV rc_hash_get_default_cdreader(struct rc_hash_cdreader* cdreader);
89
/* [deprecated] don't set callbacks in rc_hash_iterator_t */
90
RC_EXPORT void RC_CCONV rc_hash_init_default_cdreader(void);
91
/* [deprecated] set callbacks in rc_hash_iterator_t */
92
RC_EXPORT void RC_CCONV rc_hash_init_custom_cdreader(struct rc_hash_cdreader* reader);
93
94
#ifndef RC_HASH_NO_ENCRYPTED
95
96
/* specifies a function called to obtain a 3DS CIA decryption normal key.
97
* this key would be derived from slot0x3DKeyX and the common key specified by the passed index.
98
* the normal key should be written in big endian format
99
* returns non-zero on success, or zero on failure.
100
*/
101
typedef int (RC_CCONV *rc_hash_3ds_get_cia_normal_key_func)(uint8_t common_key_index, uint8_t out_normal_key[16]);
102
/* [deprecated] set callbacks in rc_hash_iterator_t */
103
RC_EXPORT void RC_CCONV rc_hash_init_3ds_get_cia_normal_key_func(rc_hash_3ds_get_cia_normal_key_func func);
104
105
/* specifies a function called to obtain 3DS NCCH decryption normal keys.
106
* the primary key will always use slot0x2CKeyX and the passed primary KeyY.
107
* the secondary key will use the KeyX slot passed
108
* the secondary KeyY will be identical to the primary keyY if the passed program id is NULL
109
* if the program id is not null, then the secondary KeyY will be obtained with "seed crypto"
110
* with "seed crypto" the 8 byte program id can be used to obtain a 16 byte "seed" within the seeddb.bin firmware file
111
* the primary KeyY then the seed will then be hashed with SHA256, and the upper 16 bytes of the digest will be the secondary KeyY used
112
* the normal keys should be written in big endian format
113
* returns non-zero on success, or zero on failure.
114
*/
115
typedef int (RC_CCONV *rc_hash_3ds_get_ncch_normal_keys_func)(uint8_t primary_key_y[16], uint8_t secondary_key_x_slot, uint8_t* optional_program_id,
116
uint8_t out_primary_key[16], uint8_t out_secondary_key[16]);
117
/* [deprecated] set callbacks in rc_hash_iterator_t */
118
RC_EXPORT void RC_CCONV rc_hash_init_3ds_get_ncch_normal_keys_func(rc_hash_3ds_get_ncch_normal_keys_func func);
119
120
#endif
121
122
/* ===================================================== */
123
124
typedef struct rc_hash_callbacks {
125
rc_hash_message_callback verbose_message;
126
rc_hash_message_callback error_message;
127
128
rc_hash_filereader_t filereader;
129
rc_hash_cdreader_t cdreader;
130
131
#ifndef RC_HASH_NO_ENCRYPTED
132
struct rc_hash_encryption_callbacks {
133
rc_hash_3ds_get_cia_normal_key_func get_3ds_cia_normal_key;
134
rc_hash_3ds_get_ncch_normal_keys_func get_3ds_ncch_normal_keys;
135
} encryption;
136
#endif
137
} rc_hash_callbacks_t;
138
139
/* data for rc_hash_iterate
140
*/
141
typedef struct rc_hash_iterator {
142
const uint8_t* buffer;
143
size_t buffer_size;
144
uint8_t consoles[12];
145
int index;
146
const char* path;
147
148
rc_hash_callbacks_t callbacks;
149
} rc_hash_iterator_t;
150
151
/* initializes a rc_hash_iterator
152
* - path must be provided
153
* - if buffer and buffer_size are provided, path may be a filename (i.e. for something extracted from a zip file)
154
*/
155
RC_EXPORT void RC_CCONV rc_hash_initialize_iterator(rc_hash_iterator_t* iterator, const char* path, const uint8_t* buffer, size_t buffer_size);
156
157
/* releases resources associated to a rc_hash_iterator
158
*/
159
RC_EXPORT void RC_CCONV rc_hash_destroy_iterator(rc_hash_iterator_t* iterator);
160
161
/* generates the next hash for the data in the rc_hash_iterator.
162
* returns non-zero if a hash was generated, or zero if no more hashes can be generated for the data.
163
*/
164
RC_EXPORT int RC_CCONV rc_hash_iterate(char hash[33], rc_hash_iterator_t* iterator);
165
166
/* generates a hash for the data in the rc_hash_iterator.
167
* returns non-zero if a hash was generated.
168
*/
169
RC_EXPORT int RC_CCONV rc_hash_generate(char hash[33], uint32_t console_id, const rc_hash_iterator_t* iterator);
170
171
/* ===================================================== */
172
173
/* generates a hash from a block of memory.
174
* returns non-zero on success, or zero on failure.
175
*/
176
/* [deprecated] use rc_hash_generate instead */
177
RC_EXPORT int RC_CCONV rc_hash_generate_from_buffer(char hash[33], uint32_t console_id, const uint8_t* buffer, size_t buffer_size);
178
179
/* generates a hash from a file.
180
* returns non-zero on success, or zero on failure.
181
*/
182
/* [deprecated] use rc_hash_generate instead */
183
RC_EXPORT int RC_CCONV rc_hash_generate_from_file(char hash[33], uint32_t console_id, const char* path);
184
185
/* ===================================================== */
186
187
RC_END_C_DECLS
188
189
#endif /* RC_HASH_H */
190
191