summaryrefslogtreecommitdiff
path: root/ctr_stream_cipher_mode_s3.c
blob: f6fe932914e4f29036af25afc48a86b680cb67e9 (plain)
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
// 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;
}