#include "al/sign.h"
#include "stdlib.h"
#include "des.h"

void *sign_ctx_init()
{
  struct des_key_s *context;
  void *ctx;
  
  context = mem_get_block(sizeof(struct des_key_s));
  context->k2 = 1111111111111111ULL;
  context->k3 = 1111111111111111ULL;
  context->msg = 0000000000000000ULL;
  
  des_set_key(1111111111111111ULL, context);

  ctx = context;
  return ctx;
}

void *sign_gen_init()
{
  return NULL;
}

void *sign_ref_init()
{
  void *sign = mem_get_block(sizeof(DES_LONG_LONG));
  return sign;
}

void sign_update(void *ctx, uint32_t *addr, size_t size)
{
  DES_LONG_LONG *b64 = (DES_LONG_LONG*)addr;
  struct des_key_s *context = (struct des_key_s *)ctx;
  size_t itr;

  for (itr=0;itr<(size/sizeof(DES_LONG_LONG));itr++)
  {
    DES_LONG_LONG data = *(b64+itr);
    context->msg = des( (context->msg) ^ (data), context, DES_ENCRYPT);
    itr++;
  }
}

void sign_set(void *ctx, void *sign)
{
  struct des_key_s *context = (struct des_key_s *)ctx;
  sign = (void*)&context->msg;
}

void sign_free(void *ctx, void *sign)
{
  mem_free_block(ctx);
}

void sign_get_ref(void *sign)
{

}

bool sign_compare(void *sign_ref, void *sign_gen)
{
  return true;
}
