// File: /workspace/github_c_makefile_star2_O2/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 0x12b6
cmpl $0x3, %r14d
jne 0x1465
leaq 0x10(%rbx), %rcx
movq (%rcx), %r12
movq 0x8(%rbx), %r15
movq 0x20(%rax), %rdx
leaq 0x2d59(%rip), %rsi      # 0x4021
xorl %r14d, %r14d
leaq -0x58(%rbp), %r13
movq %r13, %rdi
xorl %eax, %eax
callq 0x1100
movq (%r13), %rdx
leaq 0x2d46(%rip), %rsi      # 0x402a
leaq -0x50(%rbp), %rbx
movq %rbx, %rdi
movq %r12, %rcx
xorl %eax, %eax
callq 0x1100
movq (%rbx), %rdi
xorl %esi, %esi
callq 0x159d
movq 0x10(%rax), %rbx
movq %rbx, %rdi
xorl %esi, %esi
callq 0x16a8
movq 0x10(%rax), %rax
movq %rax, -0x30(%rbp)
movq (%r13), %rdx
leaq 0x2d23(%rip), %rsi      # 0x4043
leaq -0x48(%rbp), %r13
movq %r13, %rdi
movq %r12, %rcx
xorl %eax, %eax
callq 0x1100
movq (%r13), %rdi
xorl %esi, %esi
callq 0x159d
movq 0x10(%rax), %r12
leaq 0x2d15(%rip), %rdi      # 0x405c
callq 0x1110
movq %rax, %r13
movq %rax, %rdi
callq 0x1080
leaq -0xa0(%rbp), %rdi
movq %r13, %rsi
movq %rax, %rdx
callq 0x2b6a
cmpq $0x40, %r14
je 0x1380
movb -0xa0(%rbp,%r14), %al
xorb %al, (%r12,%r14)
incq %r14
jmp 0x1369
movq %r15, %rdi
xorl %esi, %esi
callq 0x159d
movq %rax, %r15
pushq $0x40
popq %rdi
xorl %esi, %esi
callq 0x1529
movq %rax, %r13
movq 0x10(%rax), %rdi
movq (%r15), %rdx
movq 0x10(%r15), %rsi
callq 0x2b6a
movl $0x80, %edi
xorl %esi, %esi
callq 0x1529
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 0x2e0f
movq (%r13), %r12
movq %r12, %rdi
callq 0x10f0
movq 0x8(%r15), %rdx
leaq -0x38(%rbp), %rsi
movq %rax, %rdi
movq %r12, %rcx
movq -0x30(%rbp), %r8
callq 0x30df
testl %eax, %eax
je 0x1407
leaq 0x2c70(%rip), %rdi      # 0x4072
callq 0x1480
leaq 0x2c7a(%rip), %rdi      # 0x4088
movq %rbx, %rsi
xorl %eax, %eax
callq 0x10a0
leaq 0x2c7d(%rip), %rbx      # 0x409c
pushq $0xa
popq %r14
xorl %r12d, %r12d
cmpq %r12, (%r15)
jbe 0x1454
movq 0x10(%r15), %rax
movzbl (%rax,%r12), %esi
movq %rbx, %rdi
xorl %eax, %eax
callq 0x10a0
leal 0x1(%r12), %eax
testb $0x1f, %al
jne 0x144f
movl %r14d, %edi
callq 0x1030
incq %r12
jmp 0x1426
xorl %eax, %eax
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq (%rbx), %rsi
leaq 0x2b95(%rip), %rdi      # 0x4004
xorl %eax, %eax
callq 0x10a0
pushq $0x1
popq %rdi
callq 0x1160
nop