// File: /workspace/github_c_makefile_star2_O0/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
subq $0x100, %rsp            # imm = 0x100
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
callq 0x1080
movl %eax, %edi
callq 0x1060
movq %rax, -0x18(%rbp)
cmpl $0x2, -0x8(%rbp)
jne 0x12b8
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x28(%rbp)
movq -0x18(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x20(%rbp)
jmp 0x12f9
cmpl $0x3, -0x8(%rbp)
jne 0x12d8
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x28(%rbp)
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movq %rax, -0x20(%rbp)
jmp 0x12f7
movq -0x10(%rbp), %rax
movq (%rax), %rsi
leaq 0x4d1e(%rip), %rdi      # 0x6004
movb $0x0, %al
callq 0x1090
movl $0x1, %edi
callq 0x1150
jmp 0x12f9
movq -0x18(%rbp), %rax
movq 0x20(%rax), %rax
movq %rax, -0x30(%rbp)
movq -0x30(%rbp), %rdx
leaq -0x38(%rbp), %rdi
leaq 0x4d0d(%rip), %rsi      # 0x6021
movb $0x0, %al
callq 0x10f0
movq -0x38(%rbp), %rdx
movq -0x20(%rbp), %rcx
leaq -0x40(%rbp), %rdi
leaq 0x4cfc(%rip), %rsi      # 0x602a
movb $0x0, %al
callq 0x10f0
movq -0x40(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x1750
movq 0x10(%rax), %rax
movq %rax, -0x50(%rbp)
movq -0x50(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x18d0
movq 0x10(%rax), %rax
movq %rax, -0x58(%rbp)
movq -0x38(%rbp), %rdx
movq -0x20(%rbp), %rcx
leaq -0x48(%rbp), %rdi
leaq 0x4cd1(%rip), %rsi      # 0x6043
movb $0x0, %al
callq 0x10f0
movq -0x48(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x1750
movq 0x10(%rax), %rax
movq %rax, -0x60(%rbp)
leaq 0x4cc7(%rip), %rdi      # 0x605c
callq 0x1100
movq %rax, -0x68(%rbp)
leaq -0xb0(%rbp), %rax
movq %rax, -0x100(%rbp)
movq -0x68(%rbp), %rax
movq %rax, -0xf8(%rbp)
movq -0x68(%rbp), %rdi
callq 0x1070
movq -0x100(%rbp), %rdi
movq -0xf8(%rbp), %rsi
movq %rax, %rdx
callq 0x4390
movl $0x0, -0xb4(%rbp)
cmpl $0x40, -0xb4(%rbp)
jge 0x141d
movslq -0xb4(%rbp), %rax
movzbl -0xb0(%rbp,%rax), %esi
movq -0x60(%rbp), %rax
movslq -0xb4(%rbp), %rcx
movzbl (%rax,%rcx), %edx
xorl %esi, %edx
movb %dl, (%rax,%rcx)
movl -0xb4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xb4(%rbp)
jmp 0x13e0
movq -0x28(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x1750
movq %rax, -0xc0(%rbp)
movl $0x40, %edi
xorl %eax, %eax
movl %eax, %esi
callq 0x1680
movq %rax, -0xc8(%rbp)
movq -0xc8(%rbp), %rax
movq 0x10(%rax), %rdi
movq -0xc0(%rbp), %rax
movq 0x10(%rax), %rsi
movq -0xc0(%rbp), %rax
movq (%rax), %rdx
callq 0x4390
movl $0x80, %edi
xorl %eax, %eax
movl %eax, %esi
callq 0x1680
movq %rax, -0xd8(%rbp)
movq -0xd8(%rbp), %rax
movq 0x8(%rax), %rdi
movq -0xc8(%rbp), %rax
movq 0x10(%rax), %rdx
movq -0xc8(%rbp), %rax
movq (%rax), %rcx
movq -0x60(%rbp), %r8
leaq -0xd0(%rbp), %rsi
callq 0x4790
movq -0xd0(%rbp), %rdi
callq 0x10e0
movq %rax, -0xe8(%rbp)
movq -0xe8(%rbp), %rdi
movq -0xd8(%rbp), %rax
movq 0x8(%rax), %rdx
movq -0xd0(%rbp), %rcx
movq -0x58(%rbp), %r8
leaq -0xe0(%rbp), %rsi
callq 0x4d60
movl %eax, -0xec(%rbp)
cmpl $0x0, -0xec(%rbp)
je 0x1507
leaq 0x4b70(%rip), %rdi      # 0x6072
callq 0x15a0
movq -0x50(%rbp), %rsi
leaq 0x4b76(%rip), %rdi      # 0x6088
movb $0x0, %al
callq 0x1090
movl $0x0, -0xf0(%rbp)
movslq -0xf0(%rbp), %rax
movq -0xd8(%rbp), %rcx
cmpq (%rcx), %rax
jae 0x1591
movq -0xd8(%rbp), %rax
movq 0x10(%rax), %rax
movslq -0xf0(%rbp), %rcx
movzbl (%rax,%rcx), %esi
leaq 0x4b49(%rip), %rdi      # 0x609c
movb $0x0, %al
callq 0x1090
movl -0xf0(%rbp), %eax
addl $0x1, %eax
movl $0x20, %ecx
cltd
idivl %ecx
cmpl $0x0, %edx
jne 0x157e
leaq 0x4aa8(%rip), %rdi      # 0x601f
movb $0x0, %al
callq 0x1090
jmp 0x1580
movl -0xf0(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xf0(%rbp)
jmp 0x1523
movl -0x4(%rbp), %eax
addq $0x100, %rsp            # imm = 0x100
popq %rbp
retq
nopl (%rax)