// File: /workspace/github_c_makefile_star2_O1/rongarret[P]clmm/sign.c
// Function: main
int main(int argc, char** argv) {

  struct passwd *pw = getpwuid(getuid());

  c_string key_id, filename;

  if (argc == 2) {
    filename = argv[1];
    key_id = pw->pw_name;
  } else if (argc == 3) {
    filename = argv[1];
    key_id = argv[2];
  } else {
    printf("Usage: %s filename [key_id]\n", argv[0]);
    exit(1);
  }

  c_string homedir = pw->pw_dir;
  c_string clmm_path, pk_path, sk_path;
  asprintf(&clmm_path, "%s/.clmm", homedir);

  asprintf(&pk_path, "%s/%s.public_signing_key", clmm_path, key_id);
  c_string pk_b64 = (c_string)file_contents(pk_path, 0)->data;
  u8* pk = b64decode(pk_b64, 0)->data;

  asprintf(&sk_path, "%s/%s.secret_signing_key", clmm_path, key_id);
  u8* sk = file_contents(sk_path, 0)->data;

  // Decrypt the secret key
  char *passwd = getpass("Enter a pass phrase: ");
  u8 passwd_hash[crypto_hash_BYTES];
  crypto_hash(passwd_hash, (u8*)passwd, strlen(passwd));
  for (int i=0; i<ssk_size; i++) sk[i] ^= passwd_hash[i];

  // Read the file to sign

  p_vector msg = file_contents(filename, 0);
  p_vector hash = mkvector(crypto_hash_BYTES, 0);
  crypto_hash(hash->data, msg->data, msg->size);

  // Sign it

  u64 smlen;
  p_vector signature = mkvector(crypto_sign_BYTES + crypto_hash_BYTES, 0);
  crypto_sign(signature->padding, &smlen, hash->data, hash->size, sk);

  // Verify the signature

  u64 mlen;  // What a crazy API
  u8* buffer = malloc(smlen);
  int status = crypto_sign_open(buffer, &mlen, signature->padding, smlen, pk);
  if (status) die("Incorrect pass phrase");

  // Output the signature
  printf("CLMM signed by: %s\n", pk_b64);
  for (int i=0; i<signature->size; i++) {
    printf("%02x", signature->data[i]);
    if ((i+1)%32 == 0) printf("\n");
  }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x78, %rsp
movq %rsi, %rbx
movl %edi, %r14d
callq 0x1090
movl %eax, %edi
callq 0x1070
movq %rax, %rcx
cmpl $0x2, %r14d
je 0x12f6
cmpl $0x3, %r14d
jne 0x14a4
leaq 0x10(%rbx), %rcx
movq (%rcx), %r12
movq 0x8(%rbx), %r15
movq 0x20(%rax), %rdx
leaq 0x2d19(%rip), %rsi      # 0x4021
xorl %r14d, %r14d
leaq -0x58(%rbp), %r13
movq %r13, %rdi
xorl %eax, %eax
callq 0x1120
movq (%r13), %rdx
leaq 0x2d06(%rip), %rsi      # 0x402a
leaq -0x50(%rbp), %rbx
movq %rbx, %rdi
movq %r12, %rcx
xorl %eax, %eax
callq 0x1120
movq (%rbx), %rdi
xorl %esi, %esi
callq 0x15e0
movq 0x10(%rax), %rbx
movq %rbx, %rdi
xorl %esi, %esi
callq 0x1719
movq 0x10(%rax), %rax
movq %rax, -0x30(%rbp)
movq (%r13), %rdx
leaq 0x2ce3(%rip), %rsi      # 0x4043
leaq -0x48(%rbp), %r13
movq %r13, %rdi
movq %r12, %rcx
xorl %eax, %eax
callq 0x1120
movq (%r13), %rdi
xorl %esi, %esi
callq 0x15e0
movq 0x10(%rax), %r12
leaq 0x2cd5(%rip), %rdi      # 0x405c
callq 0x1130
movq %rax, %r13
movq %rax, %rdi
callq 0x1080
leaq -0xa0(%rbp), %rdi
movq %r13, %rsi
movq %rax, %rdx
callq 0x2e30
movb -0xa0(%rbp,%r14), %al
xorb %al, (%r12,%r14)
incq %r14
cmpq $0x40, %r14
jne 0x13a9
movq %r15, %rdi
xorl %esi, %esi
callq 0x15e0
movq %rax, %r15
movl $0x40, %edi
xorl %esi, %esi
callq 0x1572
movq %rax, %r13
movq 0x10(%rax), %rdi
movq (%r15), %rdx
movq 0x10(%r15), %rsi
callq 0x2e30
movl $0x80, %edi
xorl %esi, %esi
callq 0x1572
movq %rax, %r15
movq 0x8(%rax), %rdi
movq (%r13), %rcx
movq 0x10(%r13), %rdx
leaq -0x40(%rbp), %r13
movq %r13, %rsi
movq %r12, %r8
callq 0x30c8
movq (%r13), %r12
movq %r12, %rdi
callq 0x1110
movq 0x8(%r15), %rdx
leaq -0x38(%rbp), %rsi
movq %rax, %rdi
movq %r12, %rcx
movq -0x30(%rbp), %r8
callq 0x3406
testl %eax, %eax
je 0x1447
leaq 0x2c30(%rip), %rdi      # 0x4072
callq 0x14c0
leaq 0x2c3a(%rip), %rdi      # 0x4088
movq %rbx, %rsi
xorl %eax, %eax
callq 0x10a0
cmpq $0x0, (%r15)
je 0x1493
leaq 0x2c37(%rip), %rbx      # 0x409c
xorl %r14d, %r14d
movq 0x10(%r15), %rax
movzbl (%rax,%r14), %esi
movq %rbx, %rdi
xorl %eax, %eax
callq 0x10a0
incq %r14
testb $0x1f, %r14b
jne 0x148e
movl $0xa, %edi
callq 0x1030
cmpq %r14, (%r15)
ja 0x1468
xorl %eax, %eax
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq (%rbx), %rsi
leaq 0x2b56(%rip), %rdi      # 0x4004
xorl %eax, %eax
callq 0x10a0
movl $0x1, %edi
callq 0x1180
nop