-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcryptopals_utils.h
108 lines (76 loc) · 4.3 KB
/
cryptopals_utils.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#pragma once
#include <stdint.h>
#include <stddef.h>
#include <stdbool.h>
#include <stdarg.h>
typedef struct byte_array {
uint8_t * bytes;
size_t len;
} byte_array;
#define NO_BA (byte_array){NULL, -1ULL}
// Allocate memory for a byte array. Be sure to run free_byte_array when finished
byte_array alloc_byte_array(size_t len);
// Free memory for a byte array
void free_byte_array(byte_array x);
// Free multiple byte arrays. Use NO_BA to end list.
void free_byte_arrays(byte_array x, ...);
// Print byte array in hex format
void print_byte_array(const byte_array x);
// Creates a new byte array with the ASCII hex representation of the bytes of original array.
// New byte array will be null-terminated to allow tools reading C-strings to use it.
byte_array byte_array_to_hex_byte_array(const byte_array x);
// Print byte array in ASCII format
void print_byte_array_ascii(const byte_array x);
// Print byte array with blocks separated by separator character
void print_byte_array_blocks(const byte_array x, size_t block_size, char separator);
void print_byte_array_ascii_blocks(const byte_array x, size_t block_size, char separator);
// Convert single hex character to integer value as byte
uint8_t hex_char_to_byte(uint8_t hex_char);
// Take in null-terminated string of hex characters and convert to byte array (allocates byte_array)
byte_array hex_to_bytes(const char * hex_str);
// Convert null-terminated string to byte array, not including the null byte (allocates byte_array)
byte_array cstring_to_bytes(const char * str);
// converts a byte array to base64 null-terminated string (allocates string)
uint8_t * byte_array_to_base64(const byte_array ba);
// converts null-terminated hex string to null-terminated base64 string (allocates string)
uint8_t * hex_to_base64(const char * hex_str);
// converts null-terminated base64 string to byte array (allocates byte_array)
byte_array base64_to_bytes(const char * base64_str);
// copy subset of byte array to new array, start at index x (inclusive) end at index y (exclusive)
byte_array sub_byte_array(const byte_array ba, size_t x, size_t y);
// copy byte array to a new array
byte_array copy_byte_array(const byte_array ba);
// new byte array with contents of old plus one null byte
byte_array append_null_byte(const byte_array x);
// create new byte array which is concatenation of x and y
byte_array append_byte_arrays(const byte_array x, const byte_array y);
// create new byte array which is concatenation of x, y, and z
byte_array append_three_byte_arrays(const byte_array x, const byte_array y, const byte_array z);
// create new byte array which is concatenation of x, separator character, and y
byte_array join_byte_arrays(const byte_array x, char sep, const byte_array y);
// true if equal, false if not
bool byte_arrays_equal(const byte_array x, const byte_array y);
// Calculates XOR of byte array X and byte array Y and stores it in byte array Z,
// or do "byte_array z = xor_byte_arrays(NO_BA, x, y);" to allocate a new byte array.
// If x and y have different lengths, z will have the minimum of the two lengths.
byte_array xor_byte_arrays(byte_array z, const byte_array x, const byte_array y);
// XOR block_size worth of bytes from x and y and place result at z.
// Does not do any bounds checking.
void xor_block(uint8_t * z, const uint8_t * x, const uint8_t * y, size_t block_size);
// Set all bytes of byte array to the value c
void set_all_bytes(byte_array ba, uint8_t c);
// Count 1 bits in a byte
size_t pop_count_byte(uint8_t b);
// Count 1 bits in a byte array
size_t pop_count_byte_array(const byte_array ba);
// Calculate hamming distance (number of differing bits) in two byte arrays
size_t hamming_distance(const byte_array x, const byte_array y);
// Read file into byte array
byte_array file_to_bytes(const char * filename);
// Read file with base64 string on multiple lines and convert to byte array
byte_array base64_file_to_bytes(const char * filename);
// Read file with one base64 string per line. Allocate array of byte arrays, converting each line
// to a byte array. Write at address provided in first argument number of byte arrays in array.
byte_array * base64_each_line_to_bytes(size_t * num_byte_arrays, const char * filename);
// Free array of byte arrays generated by base64_each_line_to_bytes
void free_array_of_byte_arrays(byte_array * ba_p, size_t num_byte_arrays);