summaryrefslogtreecommitdiff
path: root/detect_aes_in_ecb_s1.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 /detect_aes_in_ecb_s1.c
downloadcryptopals-a59a5d144cc31cc8386e60dcc97be45a0dfd5256.tar.gz
cryptopals-a59a5d144cc31cc8386e60dcc97be45a0dfd5256.zip
Diffstat (limited to 'detect_aes_in_ecb_s1.c')
-rw-r--r--detect_aes_in_ecb_s1.c38
1 files changed, 38 insertions, 0 deletions
diff --git a/detect_aes_in_ecb_s1.c b/detect_aes_in_ecb_s1.c
new file mode 100644
index 0000000..b9befe5
--- /dev/null
+++ b/detect_aes_in_ecb_s1.c
@@ -0,0 +1,38 @@
+// https://cryptopals.com/sets/1/challenges/8
+#include "util/hex.h"
+#include <stddef.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+/*
+ * data/c8_s1.txt contains one ECB-encrypted hex-encoded ciphertext.
+ * note, the problem with ECB is that it is stateless and deterministic;
+ * the same plaintext block will always produce the same 16 byte ciphertext.
+ */
+
+int main() {
+ char *line = NULL;
+ size_t capacity = 0, line_num = 1;
+ ssize_t len;
+ while ((len = getline(&line, &capacity, stdin)) > 0) {
+ line[len - 1] = '\0'; // truncate \n's
+ uint8_t *buf = read_hex(line, len - 1);
+ /*
+ * there are 10 blocks of 16 bytes (128bits) and now
+ * we have to determine which of those could be produced
+ * by AES-128 ECB (electronic code book) mode.
+ * we can simply look if any pair fo those blocks
+ * (10 * 9 / 2 = 45) are the same.
+ */
+ for (size_t i = 0; i < 10; i++)
+ for (size_t j = i + 1; j < 10; j++)
+ if (memcmp(buf + 16 * i, buf + 16 * j, 16) == 0)
+ printf("found equal blocks...\n"
+ "line: %zu\ni: %zu\nj: %zu\n",
+ line_num, i, j);
+ line_num++;
+ free(buf);
+ }
+}