__attribute__((visibility("default"))) int openssl_sign_data(
    const keymaster0_device_t*, const void* params, const uint8_t* keyBlob,
    const size_t keyBlobLength, const uint8_t* data, const size_t dataLength, uint8_t** signedData,
    size_t* signedDataLength) {
    if (data == NULL) {
        ALOGW("input data to sign == NULL");
        return -1;
    } else if (signedData == NULL || signedDataLength == NULL) {
        ALOGW("output signature buffer == NULL");
        return -1;
    }

    Unique_EVP_PKEY pkey(unwrap_key(keyBlob, keyBlobLength));
    if (pkey.get() == NULL) {
        return -1;
    }

    int type = EVP_PKEY_type(pkey->type);
    if (type == EVP_PKEY_DSA) {
        const keymaster_dsa_sign_params_t* sign_params =
            reinterpret_cast<const keymaster_dsa_sign_params_t*>(params);
        return sign_dsa(pkey.get(), const_cast<keymaster_dsa_sign_params_t*>(sign_params), data,
                        dataLength, signedData, signedDataLength);
    } else if (type == EVP_PKEY_EC) {
        const keymaster_ec_sign_params_t* sign_params =
            reinterpret_cast<const keymaster_ec_sign_params_t*>(params);
        return sign_ec(pkey.get(), const_cast<keymaster_ec_sign_params_t*>(sign_params), data,
                       dataLength, signedData, signedDataLength);
    } else if (type == EVP_PKEY_RSA) {
        const keymaster_rsa_sign_params_t* sign_params =
            reinterpret_cast<const keymaster_rsa_sign_params_t*>(params);
        return sign_rsa(pkey.get(), const_cast<keymaster_rsa_sign_params_t*>(sign_params), data,
                        dataLength, signedData, signedDataLength);
    } else {
        ALOGW("Unsupported key type");
        return -1;
    }
}
Exemplo n.º 2
0
int main(int argc,char *argv[]) {

  DSA *dsa;
  int err;
  unsigned char digest[] = "hiaasdasdaasdadasdaa";
  struct hip_dsa_sig_contents sig;
  int i = 0;

  fprintf(stderr, "main start\n");

  ERR_load_crypto_strings();

  /* set_random_seed(); */

  dsa = create_dsa_key(512 /* bits */);
  if (!dsa) {
    fprintf(stderr, "create_dsa_key\n");
    exit(1);
  }

  fprintf(stderr, "\nbignum in bits: p=%d q=%d g=%d priv=%d pub=%d\n",
	  BN_num_bits(dsa->p), BN_num_bits(dsa->q), BN_num_bits(dsa->g),
	  BN_num_bits(dsa->priv_key), BN_num_bits(dsa->pub_key));
  fprintf(stderr, "DSA_sign message (len=%d) '%s' .. ", strlen(digest), digest);

  err = sign_dsa(digest, strlen(digest), &sig, dsa);
  if (err) {
    fprintf(stderr, "failed\n");
    ERR_print_errors_fp(stderr);
    DSA_free(dsa);
    exit(1);
  } else {
    fprintf(stderr, "ok\n");
  }

  fprintf(stderr, "signature type=%d\n", sig.type);
  fprintf(stderr, " R=");
  for (i = 0; i < HIP_DSA_SIG_R_SIZE; i++)
    fprintf(stderr, "%.2x", (unsigned char)sig.r[i]);
  fprintf(stderr, "\n");
  fprintf(stderr, " S=");
  for (i = 0; i < HIP_DSA_SIG_S_SIZE; i++)
    fprintf(stderr, "%.2x", (unsigned char)sig.s[i]);
  fprintf(stderr, "\n");

  /* digest[0] ^= 255; */ /* fail test */

  fprintf(stderr, "DSA_verify .. ");
  err = verify_dsa(digest, strlen(digest), &sig, dsa);
  if (err) {
    fprintf(stderr, "failed\n");
    ERR_print_errors_fp(stderr);
    DSA_free(dsa);
    exit(1);
  } else {
    fprintf(stderr, "ok\n");
  }

  fprintf(stderr, "DSA_free\n");
  DSA_free(dsa);

 return 0;
}