예제 #1
0
파일: sha1_test.c 프로젝트: RobinWuDev/Qt
int main(int argc, char **argv) {
  size_t i, j;
  uint8_t md[SHA_DIGEST_LENGTH];
  char md_hex[sizeof(md) * 2 + 1];
  int ok = 1;

  CRYPTO_library_init();

  for (i = 0; test[i] != NULL; i++) {
    EVP_Digest(test[i], strlen(test[i]), md, NULL, EVP_sha1(), NULL);
    for (j = 0; j < sizeof(md); j++) {
      sprintf(&md_hex[j * 2], "%02x", md[j]);
    }

    if (strcmp(md_hex, expected[i]) != 0) {
      fprintf(stderr, "#%u: got %s, wanted %s\n", (unsigned)i, md_hex,
              expected[i]);
      ok = 0;
    }
  }

  ok &= test_incremental();

  if (ok) {
    printf("PASS\n");
  }

  return ok ? 0 : 1;
}
예제 #2
0
int main(void)
{
    CRYPTO_library_init();

    const struct set_name_fn *pfn = name_fns;
    while (pfn->name) {
        const char *const *pname = names;
        while (*pname) {
            X509 *crt = make_cert();
            if (crt == NULL) {
                fprintf(stderr, "make_cert failed\n");
                return 1;
            }
            if (!pfn->fn(crt, *pname)) {
                fprintf(stderr, "X509 name setting failed\n");
                return 1;
            }
            run_cert(crt, *pname, pfn);
            X509_free(crt);
            ++pname;
        }
        ++pfn;
    }
    if (errors == 0) {
        printf("PASS\n");
    }
    return errors > 0 ? 1 : 0;
}
int main(void) {
  int ret = 1;
  BIO *out;

  CRYPTO_library_init();
  ERR_load_crypto_strings();

  out = BIO_new_fp(stdout, BIO_NOCLOSE);

  if (!test_builtin(out))
    goto err;

  ret = 0;

err:
  if (ret)
    BIO_printf(out, "\nECDSA test failed\n");
  else
    BIO_printf(out, "\nPASS\n");
  if (ret)
    BIO_print_errors(out);

  if (out != NULL)
    BIO_free(out);

  return ret;
}
예제 #4
0
int main(void) {
  uint8_t buf[82];
  size_t i;

  CRYPTO_library_init();
  ERR_load_crypto_strings();

  for (i = 0; i < sizeof(kTests) / sizeof(kTests[0]); i++) {
    const hkdf_test_vector_t *test = &kTests[i];
    if (!HKDF(buf, test->out_len, test->md_func(), test->ikm, test->ikm_len,
              test->salt, test->salt_len, test->info, test->info_len)) {
      fprintf(stderr, "Call to HKDF failed\n");
      ERR_print_errors_fp(stderr);
      return 1;
    }
    if (memcmp(buf, test->out, test->out_len) != 0) {
      fprintf(stderr, "%u: Resulting key material does not match test vector\n",
              (unsigned)i);
      return 1;
    }
  }

  printf("PASS\n");
  ERR_free_strings();
  return 0;
}
예제 #5
0
int main(void) {
  CRYPTO_library_init();
  ERR_load_crypto_strings();

  if (!test_EVP_DigestSignInit()) {
    fprintf(stderr, "EVP_DigestSignInit failed\n");
    return 1;
  }

  if (!test_EVP_DigestVerifyInit()) {
    fprintf(stderr, "EVP_DigestVerifyInit failed\n");
    return 1;
  }

  if (!test_EVP_DigestSignAlgorithm()) {
    fprintf(stderr, "EVP_DigestSignInit failed\n");
    return 1;
  }

  if (!test_EVP_DigestVerifyInitFromAlgorithm()) {
    fprintf(stderr, "EVP_DigestVerifyInitFromAlgorithm failed\n");
    return 1;
  }

  if (!test_d2i_AutoPrivateKey(kExampleRSAKeyDER, sizeof(kExampleRSAKeyDER),
                               EVP_PKEY_RSA)) {
    fprintf(stderr, "d2i_AutoPrivateKey(kExampleRSAKeyDER) failed\n");
    return 1;
  }

  if (!test_d2i_AutoPrivateKey(kExampleRSAKeyPKCS8, sizeof(kExampleRSAKeyPKCS8),
                               EVP_PKEY_RSA)) {
    fprintf(stderr, "d2i_AutoPrivateKey(kExampleRSAKeyPKCS8) failed\n");
    return 1;
  }

  if (!test_d2i_AutoPrivateKey(kExampleECKeyDER, sizeof(kExampleECKeyDER),
                               EVP_PKEY_EC)) {
    fprintf(stderr, "d2i_AutoPrivateKey(kExampleECKeyDER) failed\n");
    return 1;
  }

  if (!test_EVP_PKCS82PKEY()) {
    fprintf(stderr, "test_EVP_PKCS82PKEY failed\n");
    return 1;
  }

  printf("PASS\n");
  return 0;
}
예제 #6
0
int main(void) {
  CRYPTO_library_init();

  if (!test_cert_reparse(kPKCS7NSS, sizeof(kPKCS7NSS)) ||
      !test_cert_reparse(kPKCS7Windows, sizeof(kPKCS7Windows)) ||
      !test_crl_reparse(kOpenSSLCRL, sizeof(kOpenSSLCRL)) ||
      !test_pem_certs(kPEMCert) ||
      !test_pem_crls(kPEMCRL)) {
    return 1;
  }

  printf("PASS\n");
  return 0;
}
예제 #7
0
int main(void) {
  uint8_t buf[82], prk[EVP_MAX_MD_SIZE];
  size_t i, prk_len;

  CRYPTO_library_init();

  for (i = 0; i < OPENSSL_ARRAY_SIZE(kTests); i++) {
    const hkdf_test_vector_t *test = &kTests[i];
    if (!HKDF_extract(prk, &prk_len, test->md_func(), test->ikm, test->ikm_len,
                      test->salt, test->salt_len)) {
      fprintf(stderr, "Call to HKDF_extract failed\n");
      ERR_print_errors_fp(stderr);
      return 1;
    }
    if (prk_len != test->prk_len ||
        memcmp(prk, test->prk, test->prk_len) != 0) {
      fprintf(stderr, "%zu: Resulting PRK does not match test vector\n", i);
      return 1;
    }
    if (!HKDF_expand(buf, test->out_len, test->md_func(), prk, prk_len,
                     test->info, test->info_len)) {
      fprintf(stderr, "Call to HKDF_expand failed\n");
      ERR_print_errors_fp(stderr);
      return 1;
    }
    if (memcmp(buf, test->out, test->out_len) != 0) {
      fprintf(stderr,
              "%zu: Resulting key material does not match test vector\n", i);
      return 1;
    }

    if (!HKDF(buf, test->out_len, test->md_func(), test->ikm, test->ikm_len,
              test->salt, test->salt_len, test->info, test->info_len)) {
      fprintf(stderr, "Call to HKDF failed\n");
      ERR_print_errors_fp(stderr);
      return 1;
    }
    if (memcmp(buf, test->out, test->out_len) != 0) {
      fprintf(stderr,
              "%zu: Resulting key material does not match test vector\n", i);
      return 1;
    }
  }

  printf("PASS\n");
  ERR_free_strings();
  return 0;
}
예제 #8
0
int main(void) {
  int ret = 0;
  unsigned i;

  CRYPTO_library_init();

  for (i = 0; i < sizeof(test_cases) / sizeof(struct test_case); i++) {
    if (!run_test_case(i, &test_cases[i])) {
      ret = 1;
    }
  }

  if (ret == 0) {
    printf("PASS\n");
  }

  return ret;
}
예제 #9
0
int main(void) {
#if defined(OPENSSL_WINDOWS)
  WSADATA wsa_data;
  WORD wsa_version;
  int wsa_err;
#endif

  CRYPTO_library_init();
  ERR_load_crypto_strings();

#if defined(OPENSSL_WINDOWS)
  /* Initialize Winsock. */
  wsa_version = MAKEWORD(2, 2);
  wsa_err = WSAStartup(wsa_version, &wsa_data);
  if (wsa_err != 0) {
    fprintf(stderr, "WSAStartup failed: %d\n", wsa_err);
    return 1;
  }
  if (wsa_data.wVersion != wsa_version) {
    fprintf(stderr, "Didn't get expected version: %x\n", wsa_data.wVersion);
    return 1;
  }
#endif

  if (!test_socket_connect()) {
    return 1;
  }

  if (!test_printf()) {
    return 1;
  }

  if (!test_zero_copy_bio_pairs()) {
    return 1;
  }

  printf("PASS\n");
  return 0;
}
예제 #10
0
int main(int argc, char **argv) {
  CRYPTO_library_init();

  bio_err = BIO_new_fp(stderr, BIO_NOCLOSE);
  bio_out = BIO_new_fp(stdout, BIO_NOCLOSE);

  if (!test_generate() ||
      !test_verify(fips_sig, sizeof(fips_sig), 1) ||
      !test_verify(fips_sig_negative, sizeof(fips_sig_negative), -1) ||
      !test_verify(fips_sig_extra, sizeof(fips_sig_extra), -1) ||
      !test_verify(fips_sig_bad_length, sizeof(fips_sig_bad_length), -1) ||
      !test_verify(fips_sig_bad_r, sizeof(fips_sig_bad_r), 0)) {
    BIO_print_errors(bio_err);
    BIO_free(bio_err);
    BIO_free(bio_out);
    return 1;
  }

  BIO_free(bio_err);
  BIO_free(bio_out);
  printf("PASS\n");
  return 0;
}
예제 #11
0
int main(void) {
  CRYPTO_library_init();

  if (!test_skip() ||
      !test_get_u() ||
      !test_get_prefixed() ||
      !test_get_prefixed_bad() ||
      !test_get_asn1() ||
      !test_cbb_basic() ||
      !test_cbb_fixed() ||
      !test_cbb_finish_child() ||
      !test_cbb_misuse() ||
      !test_cbb_prefixed() ||
      !test_cbb_asn1() ||
      !test_ber_convert() ||
      !test_asn1_uint64() ||
      !test_get_optional_asn1_bool()) {
    return 1;
  }

  printf("PASS\n");
  return 0;
}
예제 #12
0
int main(int argc, char *argv[]) {
  int err = 0;
  int v;
  RSA *key;
  unsigned char ptext[256];
  unsigned char ctext[256];
  static unsigned char ptext_ex[] = "\x54\x85\x9b\x34\x2c\x49\xea\x2a";
  unsigned char ctext_ex[256];
  int plen;
  int clen = 0;
  int num;
  int n;

  CRYPTO_library_init();

  plen = sizeof(ptext_ex) - 1;

  for (v = 0; v < 3; v++) {
    key = RSA_new();
    switch (v) {
      case 0:
        clen = key1(key, ctext_ex);
        break;
      case 1:
        clen = key2(key, ctext_ex);
        break;
      case 2:
        clen = key3(key, ctext_ex);
        break;
      default:
        abort();
    }

    if (!RSA_check_key(key)) {
      printf("%d: RSA_check_key failed\n", v);
      err = 1;
      goto oaep;
    }

    num = RSA_public_encrypt(plen, ptext_ex, ctext, key, RSA_PKCS1_PADDING);
    if (num != clen) {
      printf("PKCS#1 v1.5 encryption failed!\n");
      err = 1;
      goto oaep;
    }

    num = RSA_private_decrypt(num, ctext, ptext, key, RSA_PKCS1_PADDING);
    if (num != plen || memcmp(ptext, ptext_ex, num) != 0) {
      printf("PKCS#1 v1.5 decryption failed!\n");
      err = 1;
    } else {
      printf("PKCS #1 v1.5 encryption/decryption ok\n");
    }

  oaep:
    ERR_clear_error();
    num =
        RSA_public_encrypt(plen, ptext_ex, ctext, key, RSA_PKCS1_OAEP_PADDING);
    if (num == -1) {
      printf("No OAEP support\n");
      goto next;
    }
    if (num != clen) {
      printf("OAEP encryption failed!\n");
      err = 1;
      goto next;
    }

    num = RSA_private_decrypt(num, ctext, ptext, key, RSA_PKCS1_OAEP_PADDING);
    if (num != plen || memcmp(ptext, ptext_ex, num) != 0) {
      printf("OAEP decryption (encrypted data) failed!\n");
      err = 1;
    } else if (memcmp(ctext, ctext_ex, num) == 0) {
      printf("OAEP test vector %d passed!\n", v);
    }

    /* Different ciphertexts (rsa_oaep.c without -DPKCS_TESTVECT).
       Try decrypting ctext_ex */

    num =
        RSA_private_decrypt(clen, ctext_ex, ptext, key, RSA_PKCS1_OAEP_PADDING);

    if (num != plen || memcmp(ptext, ptext_ex, num) != 0) {
      printf("OAEP decryption (test vector data) failed!\n");
      err = 1;
    } else {
      printf("OAEP encryption/decryption ok\n");
    }

    /* Try decrypting corrupted ciphertexts */
    for (n = 0; n < clen; ++n) {
      int b;
      unsigned char saved = ctext[n];
      for (b = 0; b < 256; ++b) {
        if (b == saved) {
          continue;
        }
        ctext[n] = b;
        num =
            RSA_private_decrypt(num, ctext, ptext, key, RSA_PKCS1_OAEP_PADDING);
        if (num > 0) {
          printf("Corrupt data decrypted!\n");
          err = 1;
        }
      }
    }

  next:
    RSA_free(key);
  }

  if (err != 0 ||
      !test_only_d_given() ||
      !test_recover_crt_params() ||
      !test_bad_key()) {
    err = 1;
  }

  if (err == 0) {
    printf("PASS\n");
  }
  return err;
}
예제 #13
0
파일: crypto.c 프로젝트: google/boringssl
int OPENSSL_init_crypto(uint64_t opts, const OPENSSL_INIT_SETTINGS *settings) {
  CRYPTO_library_init();
  return 1;
}
예제 #14
0
파일: ssl_algs.c 프로젝트: a397871706/plug
int SSL_library_init(void) {
  CRYPTO_library_init();
  return 1;
}
예제 #15
0
int main(int argc, char *argv[]) {
  unsigned i;
  char *p;
  int err = 0;
  uint8_t out[EVP_MAX_MD_SIZE];
  unsigned out_len;

  CRYPTO_library_init();

  for (i = 0; i < NUM_TESTS; i++) {
    const struct test_st *test = &kTests[i];

    /* Test using the one-shot API. */
    if (NULL == HMAC(EVP_md5(), test->key, test->key_len, test->data,
                     test->data_len, out, &out_len)) {
      fprintf(stderr, "%u: HMAC failed.\n", i);
      err++;
      continue;
    }
    p = to_hex(out, out_len);
    if (strcmp(p, test->hex_digest) != 0) {
      fprintf(stderr, "%u: got %s instead of %s\n", i, p, test->hex_digest);
      err++;
    }

    /* Test using HMAC_CTX. */
    HMAC_CTX ctx;
    HMAC_CTX_init(&ctx);
    if (!HMAC_Init_ex(&ctx, test->key, test->key_len, EVP_md5(), NULL) ||
        !HMAC_Update(&ctx, test->data, test->data_len) ||
        !HMAC_Final(&ctx, out, &out_len)) {
      fprintf(stderr, "%u: HMAC failed.\n", i);
      err++;
      HMAC_CTX_cleanup(&ctx);
      continue;
    }
    p = to_hex(out, out_len);
    if (strcmp(p, test->hex_digest) != 0) {
      fprintf(stderr, "%u: got %s instead of %s\n", i, p, test->hex_digest);
      err++;
    }

    /* Test that an HMAC_CTX may be reset with the same key. */
    if (!HMAC_Init_ex(&ctx, NULL, 0, EVP_md5(), NULL) ||
        !HMAC_Update(&ctx, test->data, test->data_len) ||
        !HMAC_Final(&ctx, out, &out_len)) {
      fprintf(stderr, "%u: HMAC failed.\n", i);
      err++;
      HMAC_CTX_cleanup(&ctx);
      continue;
    }
    p = to_hex(out, out_len);
    if (strcmp(p, test->hex_digest) != 0) {
      fprintf(stderr, "%u: got %s instead of %s\n", i, p, test->hex_digest);
      err++;
    }

    HMAC_CTX_cleanup(&ctx);
  }

  /* Test that HMAC() uses the empty key when called with key = NULL. */
  const struct test_st *test = &kTests[0];
  assert(test->key_len == 0);
  if (NULL == HMAC(EVP_md5(), NULL, 0, test->data, test->data_len, out,
                   &out_len)) {
    fprintf(stderr, "HMAC failed.\n");
    err++;
  } else {
    p = to_hex(out, out_len);
    if (strcmp(p, test->hex_digest) != 0) {
      fprintf(stderr, "got %s instead of %s\n", p, test->hex_digest);
      err++;
    }
  }

  /* Test that HMAC_Init, etc., uses the empty key when called initially with
   * key = NULL. */
  assert(test->key_len == 0);
  HMAC_CTX ctx;
  HMAC_CTX_init(&ctx);
  if (!HMAC_Init_ex(&ctx, NULL, 0, EVP_md5(), NULL) ||
      !HMAC_Update(&ctx, test->data, test->data_len) ||
      !HMAC_Final(&ctx, out, &out_len)) {
    fprintf(stderr, "HMAC failed.\n");
    err++;
  } else {
    p = to_hex(out, out_len);
    if (strcmp(p, test->hex_digest) != 0) {
      fprintf(stderr, "got %s instead of %s\n", p, test->hex_digest);
      err++;
    }
  }
  HMAC_CTX_cleanup(&ctx);

  if (err) {
    return 1;
  }

  printf("PASS\n");
  return 0;
}
예제 #16
0
int main(int argc, char *argv[]) {
  BN_GENCB _cb;
  DH *a;
  DH *b = NULL;
  char buf[12];
  unsigned char *abuf = NULL, *bbuf = NULL;
  int i, alen, blen, aout, bout, ret = 1;

  CRYPTO_library_init();

  BN_GENCB_set(&_cb, &cb, stdout);
  if (((a = DH_new()) == NULL) ||
      !DH_generate_parameters_ex(a, 64, DH_GENERATOR_5, &_cb)) {
    goto err;
  }

  if (!DH_check(a, &i)) {
    goto err;
  }
  if (i & DH_CHECK_P_NOT_PRIME) {
    puts("p value is not prime\n");
  }
  if (i & DH_CHECK_P_NOT_SAFE_PRIME) {
    puts("p value is not a safe prime\n");
  }
  if (i & DH_CHECK_UNABLE_TO_CHECK_GENERATOR) {
    puts("unable to check the generator value\n");
  }
  if (i & DH_CHECK_NOT_SUITABLE_GENERATOR) {
    puts("the g value is not a generator\n");
  }

  puts("\np    =");
  BN_print_fp(stdout, a->p);
  puts("\ng    =");
  BN_print_fp(stdout, a->g);
  puts("\n");

  b = DH_new();
  if (b == NULL) {
    goto err;
  }

  b->p = BN_dup(a->p);
  b->g = BN_dup(a->g);
  if (b->p == NULL || b->g == NULL) {
    goto err;
  }

  if (!DH_generate_key(a)) {
    goto err;
  }
  puts("pri 1=");
  BN_print_fp(stdout, a->priv_key);
  puts("\npub 1=");
  BN_print_fp(stdout, a->pub_key);
  puts("\n");

  if (!DH_generate_key(b)) {
    goto err;
  }
  puts("pri 2=");
  BN_print_fp(stdout, b->priv_key);
  puts("\npub 2=");
  BN_print_fp(stdout, b->pub_key);
  puts("\n");

  alen = DH_size(a);
  abuf = (unsigned char *)OPENSSL_malloc(alen);
  aout = DH_compute_key(abuf, b->pub_key, a);

  puts("key1 =");
  for (i = 0; i < aout; i++) {
    sprintf(buf, "%02X", abuf[i]);
    puts(buf);
  }
  puts("\n");

  blen = DH_size(b);
  bbuf = (unsigned char *)OPENSSL_malloc(blen);
  bout = DH_compute_key(bbuf, a->pub_key, b);

  puts("key2 =");
  for (i = 0; i < bout; i++) {
    sprintf(buf, "%02X", bbuf[i]);
    puts(buf);
  }
  puts("\n");
  if ((aout < 4) || (bout != aout) || (memcmp(abuf, bbuf, aout) != 0)) {
    fprintf(stderr, "Error in DH routines\n");
    ret = 1;
  } else {
    ret = 0;
  }

  if (!run_rfc5114_tests()) {
    ret = 1;
  }

err:
  ERR_print_errors_fp(stderr);

  if (abuf != NULL) {
    OPENSSL_free(abuf);
  }
  if (bbuf != NULL) {
    OPENSSL_free(bbuf);
  }
  if (b != NULL) {
    DH_free(b);
  }
  if (a != NULL) {
    DH_free(a);
  }
  return ret;
}
예제 #17
0
int main(int argc, char **argv) {
  _LHASH *lh;
  struct dummy_lhash dummy_lh = {NULL};
  unsigned i;

  CRYPTO_library_init();

  lh = lh_new(NULL, NULL);
  if (lh == NULL) {
    return 1;
  }

  for (i = 0; i < 100000; i++) {
    unsigned action;
    char *s, *s1, *s2;

    if (dummy_lh_num_items(&dummy_lh) != lh_num_items(lh)) {
      fprintf(stderr, "Length mismatch\n");
      return 1;
    }

    action = rand() % 3;
    switch (action) {
      case 0:
        s = rand_string();
        s1 = (char *)lh_retrieve(lh, s);
        s2 = dummy_lh_retrieve(&dummy_lh, s);
        if (s1 != NULL && (s2 == NULL || strcmp(s1, s2) != 0)) {
          fprintf(stderr, "lh_retrieve failure\n");
          abort();
        }
        free(s);
        break;

      case 1:
        s = rand_string();
        lh_insert(lh, (void **)&s1, s);
        dummy_lh_insert(&dummy_lh, &s2, strdup(s));

        if (s1 != NULL && (s2 == NULL || strcmp(s1, s2) != 0)) {
          fprintf(stderr, "lh_insert failure\n");
          abort();
        }

        if (s1) {
          free(s1);
        }
        if (s2) {
          free(s2);
        }
        break;

      case 2:
        s = rand_string();
        s1 = lh_delete(lh, s);
        s2 = dummy_lh_delete(&dummy_lh, s);

        if (s1 != NULL && (s2 == NULL || strcmp(s1, s2) != 0)) {
          fprintf(stderr, "lh_insert failure\n");
          abort();
        }

        if (s1) {
          free(s1);
        }
        if (s2) {
          free(s2);
        }
        free(s);
        break;

      default:
        abort();
    }
  }

  lh_doall(lh, free);
  lh_free(lh);
  dummy_lh_free(&dummy_lh);
  printf("PASS\n");
  return 0;
}