summaryrefslogtreecommitdiff
path: root/ctr_stream_cipher_mode_s3.c
diff options
context:
space:
mode:
Diffstat (limited to 'ctr_stream_cipher_mode_s3.c')
-rw-r--r--ctr_stream_cipher_mode_s3.c31
1 files changed, 31 insertions, 0 deletions
diff --git a/ctr_stream_cipher_mode_s3.c b/ctr_stream_cipher_mode_s3.c
new file mode 100644
index 0000000..f6fe932
--- /dev/null
+++ b/ctr_stream_cipher_mode_s3.c
@@ -0,0 +1,31 @@
+// https://cryptopals.com/sets/3/challenges/18
+#include "util/aes.h"
+#include "util/base64.h"
+
+/*
+ * CTR mode: instead of encrypting plaintext, CTR mode encrypts
+ * a running counter, producing a 16-byte block of keystream,
+ * which is XOR'd against plaintext.
+ * this mode does not require padding; when there is no more
+ * plaintext, XOR'ing & keystream generation just stops.
+ * decryption is identical.
+ */
+
+int main() {
+ char *str = "L77na/nrFsKvynd6HzOoG7GHTLXsTVu9qvY/"
+ "2syLXzhPweyyMTJULu/6/kXX0KSvoOLSFQ==";
+ size_t decoded_len;
+ uint8_t *decoded = base64_decode(str, &decoded_len);
+ unsigned char key[16] = "YELLOW SUBMARINE";
+ uint64_t nonce = 0;
+ unsigned char *decrypted = malloc(decoded_len);
+ if (!ctr_decrypt(decoded, decoded_len, key, nonce, decrypted)) {
+ fprintf(stderr, "CTR decryption error\n");
+ exit(1);
+ }
+ if (fwrite(decrypted, 1, decoded_len, stdout) != decoded_len) {
+ perror("fwrite");
+ exit(1);
+ }
+ return 0;
+}