Skip to content

Commit f99bae7

Browse files
committed
Add comments
1 parent 12b7ae6 commit f99bae7

File tree

1 file changed

+26
-12
lines changed

1 file changed

+26
-12
lines changed

main.c

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,31 +7,43 @@
77
#include <time.h>
88
#include "base64.h"
99

10-
11-
char *g_header_b64 = NULL;
12-
char *g_payload_b64 = NULL;
13-
char *g_signature_b64 = NULL;
14-
unsigned char *g_to_encrypt = NULL;
15-
unsigned char *g_signature = NULL;
16-
10+
char *g_header_b64 = NULL; // Holds the Base64 header of the original JWT
11+
char *g_payload_b64 = NULL; // Holds the Base64 payload of the original JWT
12+
char *g_signature_b64 = NULL; // Holds the Base64 signature of the original JWT
13+
unsigned char *g_to_encrypt = NULL; // Holds the part of the JWT that needs to be hashed
14+
unsigned char *g_signature = NULL; // Holds the Base64 *decoded* signature of the original JWT
15+
16+
// Some lengths of buffers. Useful to avoid computing it multiple times.
17+
// Also, not all strings used finish with a '\0' for optimization purposes.
18+
// In that case, we need to have the length
1719
size_t g_header_b64_len = 0;
1820
size_t g_payload_b64_len = 0;
1921
size_t g_signature_b64_len = 0;
2022
size_t g_signature_len = 0;
2123
size_t g_to_encrypt_len = 0;
2224

25+
// The alphabet to use when brute-forcing
2326
char *g_alphabet = NULL;
2427
size_t g_alphabet_len = 0;
2528

26-
unsigned char* g_result = NULL;
29+
// Holds the computed signature at each iteration to compare it with the original
30+
// signature
31+
unsigned char *g_result = NULL;
2732
unsigned int g_result_len = 0;
2833

2934
char *g_buffer = NULL;
3035

36+
// The hash function to apply the HMAC to
3137
EVP_MD *g_evp_md = NULL;
3238

39+
/**
40+
* Check if the signature produced with "secret
41+
* of size "secrent_len" (without the '\0') matches the original
42+
* signature.
43+
* Return true if it matches, false otherwise
44+
*/
3345
bool check(const char *secret, size_t secret_len) {
34-
// Hash to_encrypt using HMAC-SHA256 into result
46+
// Hash to_encrypt using HMAC into result
3547
HMAC(
3648
g_evp_md,
3749
(const unsigned char *) secret, secret_len,
@@ -66,7 +78,7 @@ char *bruteSequential(int start, int maxLen)
6678
for (int i = start; i <= maxLen; ++i)
6779
{
6880
if (bruteImpl(g_buffer, 0, i))
69-
return strdup(g_buffer);
81+
return strdup(g_buffer);
7082
}
7183

7284
return NULL;
@@ -97,11 +109,10 @@ int main(int argc, char **argv) {
97109

98110
g_alphabet_len = strlen(g_alphabet);
99111

100-
// Split it into header, payload and signature
112+
// Split the JWT into header, payload and signature
101113
g_header_b64 = strtok(jwt, ".");
102114
g_payload_b64 = strtok(NULL, ".");
103115
g_signature_b64 = strtok(NULL, ".");
104-
105116
g_header_b64_len = strlen(g_header_b64);
106117
g_payload_b64_len = strlen(g_payload_b64);
107118
g_signature_b64_len = strlen(g_signature_b64);
@@ -120,8 +131,11 @@ int main(int argc, char **argv) {
120131
// is returned by this function
121132
g_signature_len = Base64decode((char *) g_signature, (const char *) g_signature_b64);
122133

134+
// Allocate the buffer used to hold the calculated signature
123135
g_result = malloc(EVP_MAX_MD_SIZE);
124136
g_buffer = malloc(max_len + 1);
137+
138+
// The chosen hash function is SHA-256
125139
g_evp_md = (EVP_MD *) EVP_sha256();
126140

127141
clock_t start = clock(), diff;

0 commit comments

Comments
 (0)