-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstegosaurus.c
211 lines (181 loc) · 6.29 KB
/
stegosaurus.c
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <FreeImage.h>
#include <openssl/evp.h>
typedef struct {
char* key;
char* iv;
FIBITMAP* bitmap;
FREE_IMAGE_FORMAT format;
char* text;
int textLength;
} EncryptionInfo;
typedef struct {
char* cipherText;
int cipherLength;
} CipherInfo;
void usage_error_handler(void) {
fprintf(stderr, "Usage: stegosaurus [--key key] [--iv iv] [--img imgname] [--text plaintext]\n");
exit(EXIT_FAILURE);
}
void unsupported_image_error_handler(char* input_filename) {
fprintf(stderr, "Unsupported image format: %s\n", input_filename);
exit(EXIT_FAILURE);
}
void image_load_error_handler(char* input_filename) {
fprintf(stderr, "Failed to load image: %s\n", input_filename);
exit(EXIT_FAILURE);
}
void encryption_error_handler(void) {
fprintf(stderr, "Failed to encrypt text\n");
exit(EXIT_FAILURE);
}
void data_too_large_error_handler(size_t maxCapacity) {
fprintf(stderr, "Data too large to embed in the image. Maximum capacity is %zu bytes\n", maxCapacity);
exit(EXIT_FAILURE);
}
void unsupported_format_error_handler(void) {
fprintf(stderr, "Unsupported image format. Only 24-bit and 32-bit images are supported\n");
exit(EXIT_FAILURE);
}
void convert_to_bmp(char* input_filename, EncryptionInfo* encInfo) {
FREE_IMAGE_FORMAT format = FreeImage_GetFileType(input_filename, 0);
if (format == FIF_UNKNOWN) {
format = FreeImage_GetFIFFromFilename(input_filename);
}
if (format == FIF_UNKNOWN) {
unsupported_image_error_handler(input_filename);
}
encInfo->format = format;
encInfo->bitmap = FreeImage_Load(format, input_filename, 0);
if (!encInfo->bitmap) {
image_load_error_handler(input_filename);
}
}
EncryptionInfo process_command_line(int argc, char* argv[]) {
// Ignore program name
argv++;
argc--;
EncryptionInfo encInfo = {NULL, NULL, NULL, NULL};
while (argc >= 2 && strncmp(argv[0], "--", 2) == 0 && strlen(argv[1]) > 0) {
if (!strcmp(argv[0], "--key") && !encInfo.key) {
// Using 256 bit AES
encInfo.key = (char*)malloc(257);
if (strlen(argv[1]) > 256) usage_error_handler();
snprintf(encInfo.key, 257, "%s", argv[1]);
argc -= 2;
argv += 2;
}
else if (!strcmp(argv[0], "--iv") && !encInfo.iv) {
// IV size for 256 bit AES (i.e. 256 bit key) is 128 bits
encInfo.iv = (char*)malloc(129);
if (strlen(argv[1]) > 128) usage_error_handler();
snprintf(encInfo.iv, 129, "%s", argv[1]);
argc -= 2;
argv += 2;
}
else if (!strcmp(argv[0], "--img") && !encInfo.bitmap) {
convert_to_bmp(argv[1], &encInfo);
argc -= 2;
argv += 2;
}
else if (!strcmp(argv[0], "--text") && !encInfo.text) {
encInfo.text = argv[1];
encInfo.textLength = strlen(encInfo.text);
argc -= 2;
argv += 2;
}
else {
usage_error_handler();
}
}
if (!encInfo.key || !encInfo.iv || !encInfo.bitmap || !encInfo.text) {
usage_error_handler();
}
if (argc) {
// We have left over arguments - this is a problem
usage_error_handler();
}
return encInfo;
}
CipherInfo encrypt_text(EncryptionInfo encInfo) {
EVP_CIPHER_CTX* ctx;
CipherInfo cipherInfo = { NULL, 0 };
int len;
// Set up our context
if (!(ctx = EVP_CIPHER_CTX_new())) {
encryption_error();
}
if (1 != EVP_EncryptInit_ex(ctx, EVP_aes_256_cbc(), NULL, encInfo.key, encInfo.iv)) {
encryption_error();
}
// Need another block
cipherInfo.cipherText = (unsigned char*)malloc(encInfo.textLength + EVP_CIPHER_block_size(EVP_aes_256_cbc()));
if (1 != EVP_EncryptUpdate(ctx, cipherInfo.cipherText, &len, encInfo.text, encInfo.textLength)) {
encryption_error();
}
cipherInfo.cipherLength = len;
if (1 != EVP_EncryptFinal_ex(ctx, cipherInfo.cipherText + len, &len)) {
encryption_error();
}
cipherInfo.cipherLength += len;
// Clean up
EVP_CIPHER_CTX_free(ctx);
return cipherInfo;
}
void embed_cipher(EncryptionInfo* encInfo, CipherInfo cipherInfo) {
BYTE* bits = FreeImage_GetBits(encInfo->bitmap);
unsigned width = FreeImage_GetWidth(encInfo->bitmap);
unsigned height = FreeImage_GetHeight(encInfo->bitmap);
unsigned pitch = FreeImage_GetPitch(encInfo->bitmap);
unsigned bitsPerPixel = FreeImage_GetBPP(encInfo->bitmap);
if (bitsPerPixel != 24 && bitsPerPixel != 32) {
unsupported_format_error_handler();
}
size_t maxCapacity = (width * height * (bitsPerPixel / 8)) / 8;
if (cipherInfo.cipherLength > maxCapacity) {
data_too_large_error_handler(maxCapacity);
}
size_t dataID = 0;
for (unsigned y = 0; y < height; ++y) {
BYTE* pixel = bits + y * pitch;
for (unsigned x = 0; x < width; ++x) {
for (unsigned channel = 0; channel < (bitsPerPixel / 8); ++channel) {
if (dataID < cipherInfo.cipherLength * 8) {
// Embed one bit of data into the LSB of the current channel
pixel[channel] = (pixel[channel] & 0xFE) | ((cipherInfo.cipherText[dataID / 8] >> (7 - (dataID % 8))) & 1);
dataID++;
}
}
pixel += (bitsPerPixel / 8); // Move to the next pixel
}
}
if (dataID < cipherInfo.cipherLength * 8) {
fprintf(stderr, "Warning: Not all data was embedded into the image. Image may be too small\n");
}
}
void free_encryption_info(EncryptionInfo* encInfo) {
if (encInfo->bitmap) {
FreeImage_Unload(encInfo->bitmap);
}
if (encInfo->key) {
free(encInfo->key);
}
if (encInfo->iv) {
free(encInfo->iv);
}
}
void free_cipher_info(CipherInfo* cipherInfo) {
if (cipherInfo->cipherText) {
free(cipherInfo->cipherText);
}
}
int main(int argc, char* argv[]) {
EncryptionInfo encInfo = process_command_line(argc, argv);
CipherInfo cipherInfo = encrypt_text(encInfo);
printf("Cipher text: %s\n", cipherInfo.cipherText);
printf("Cipher length: %d\n", cipherInfo.cipherLength);
embed_cipher(&encInfo, cipherInfo);
return 0;
}