summaryrefslogtreecommitdiff
path: root/implement_padding_s2.c
diff options
context:
space:
mode:
authorJack Sangdahl <[email protected]>2024-06-15 22:27:25 -0600
committerJack Sangdahl <[email protected]>2024-06-15 22:27:25 -0600
commita59a5d144cc31cc8386e60dcc97be45a0dfd5256 (patch)
tree076d0adc9bc53538e7283e0248adef57ce2a9a51 /implement_padding_s2.c
downloadcryptopals-a59a5d144cc31cc8386e60dcc97be45a0dfd5256.tar.gz
cryptopals-a59a5d144cc31cc8386e60dcc97be45a0dfd5256.zip
Diffstat (limited to 'implement_padding_s2.c')
-rw-r--r--implement_padding_s2.c26
1 files changed, 26 insertions, 0 deletions
diff --git a/implement_padding_s2.c b/implement_padding_s2.c
new file mode 100644
index 0000000..304725b
--- /dev/null
+++ b/implement_padding_s2.c
@@ -0,0 +1,26 @@
+// https://cryptopals.com/sets/2/challenges/9
+#include "util/pkcs7_padding.h"
+#include <stdio.h>
+#include <string.h>
+
+/*
+ * a block cipher transforms fixed-size block (8/16 bytes) of plaintext into
+ * ciphertext. but we rarely want to transform a single block; we encrypt
+ * irregularly-sized messages. the odd size of these messages can be accounted
+ * for using padding, specifically PKCS#7 scheme. eg. "YELLOW SUBMARINE" padded
+ * to 20 bytes would be "YELLOW SUBMARINE\x04\x04\x04\x04" (0x04 indicates EOT
+ * or end-of-transmission)
+ */
+
+int main() {
+ uint8_t *str = (uint8_t *)"YELLOW SUBMARINE", *res;
+ size_t curr_len = strlen((char *)str);
+ ssize_t new_len = add_padding(str, curr_len, 20, &res);
+ if (new_len == -1) {
+ fprintf(stderr, "add_padding() failed\n");
+ exit(1);
+ }
+ fwrite(res, 1, new_len, stdout);
+ free(res);
+ printf("curr len: %zu\nnew_len: %zd\n", curr_len, new_len);
+}