Beispiel #1
0
/* Parse a single option. */
static error_t
parse_opt (int key, char *arg, struct argp_state *state)
{
  /* Get the input argument from argp_parse, which we
     know is a pointer to our arguments structure. */
  struct arguments *arguments = state->input;
  int slot;
  long int address_arg;

  switch (key)
    {
    case 'a':

      /* TODO: Not working as expected */
      address_arg = strtol (arg,NULL,16);
      if (0 != address_arg)
        {
          arguments->address = address_arg;
          LCA_LOG (DEBUG, "Using address %u", address_arg);
        }
      else
        LCA_LOG (INFO, "Address not recognized, using default");

      break;
    case 'b':
      arguments->bus = arg;
      break;
    case 'q': case 's':
      arguments->silent = 1;
      break;
    case 'v':
      arguments->verbose = 1;
      lca_set_log_level (DEBUG);
      break;
    case 'f':
      arguments->input_file = arg;
      break;
    case OPT_UPDATE_SEED:
      arguments->update_seed = true;
      break;
    case 'k':
      slot = atoi (arg);
      if (slot < 0 || slot > 15)
        argp_usage (state);

      arguments->key_slot = slot;
      break;
    case 'c':
      if (!is_hex_arg (arg, 64))
        {
          fprintf (stderr, "%s\n", "Invalid Challenge.");
          argp_usage (state);
        }
      else
        arguments->challenge = arg;
      break;
    case OPT_SIGNATURE:
      if (!is_hex_arg (arg, 128))
        {
          fprintf (stderr, "%s\n", "Invalid P256 Signature.");
          argp_usage (state);
        }
      else
      arguments->signature = arg;
      break;
    case OPT_PUB_KEY:
      if (!is_hex_arg (arg, 130))
        {
          fprintf (stderr, "%s\n", "Invalid P256 Public Key.");
          argp_usage (state);
        }
      else
        arguments->pub_key = arg;
      break;
    case 'w':
      if (!is_hex_arg (arg, 64))
        {
          fprintf (stderr, "%s\n", "Invalid Data.");
          argp_usage (state);
        }
      else
        arguments->write_data = arg;
      break;
    case 'r':
      if (!is_hex_arg (arg, 64))
        {
          fprintf (stderr, "%s\n", "Invalid Challenge Response.");
          argp_usage (state);
        }
      else
        arguments->challenge_rsp = arg;
      break;
    case 'm':
      if (!is_hex_arg (arg, 26))
        {
          fprintf (stderr, "%s\n", "Invalid Meta Data.");
          argp_usage (state);
        }
      else
        arguments->meta = arg;
      break;
    case ARGP_KEY_ARG:
      if (state->arg_num >= NUM_ARGS)
        /* Too many arguments. */
        argp_usage (state);
      else
        arguments->args[state->arg_num] = arg;

      break;

    case ARGP_KEY_END:
      if (state->arg_num < NUM_ARGS)
        /* Not enough arguments. */
        argp_usage (state);
      break;

    default:
      return ARGP_ERR_UNKNOWN;
    }
  return 0;
}
Beispiel #2
0
int
hard_sign (const uint8_t *to_sign, size_t len,
           jwa_t alg, void *cookie,
           uint8_t **out, size_t *out_len)
{
    int fd = ecc108_fd;
    int rc = -1;
    struct lca_octet_buffer hash;
    gcry_sexp_t sig, digest;

    const unsigned int DLEN = gcry_md_get_algo_dlen (GCRY_MD_SHA256);
    hash.ptr = malloc (DLEN);
    hash.len = DLEN;
    assert (NULL != hash.ptr);

    gcry_md_hash_buffer (GCRY_MD_SHA256, hash.ptr, to_sign, len);

    //lca_set_log_level (DEBUG);
    lca_print_hex_string ("hash: ", hash.ptr, hash.len);
    lca_set_log_level (INFO);

    lca_wakeup(fd);

    struct lca_octet_buffer r =
        gen_nonce (fd, hash);

    assert (NULL != r.ptr);

    struct lca_octet_buffer s =
        lca_ecc_sign (fd, 0);

    assert (NULL != s.ptr);

    *out = s.ptr;
    *out_len = s.len;

    /* verify sig */
    /* r = */
    /*     gen_nonce (fd, hash); */

    /* assert (NULL != r.ptr); */

    /* struct lca_octet_buffer apkb = lca_make_buffer (sizeof (apk)); */

    /* memcpy (apkb.ptr, apk, sizeof(apk)); */



    /* assert (lca_ecc_verify (fd, apkb, s)); */

    lca_idle (fd);

    //lca_set_log_level (DEBUG);
    lca_print_hex_string ("Signature: ", s.ptr, s.len);
    lca_set_log_level (INFO);

    printf ("Len: %d\n", s.len);


    return 0;
}