aboutsummaryrefslogblamecommitdiff
path: root/tests/test-pbkdf2.c
blob: f3072a7d920dce922b15620b01a57e16549f80e5 (plain) (tree)
generated by cgit v1.2.3 (git 2.25.1) at 2024-09-20 08:47:12 +0000
s="p">(keybuf), curve)) != HAL_OK) return printf("hal_ecdsa_key_gen() failed: %s\n", hal_error_string(err)), 0; printf("Generating digest\n"); uint8_t hashbuf[hash_descriptor->digest_length]; { const uint8_t plaintext[] = "So long, and thanks..."; uint8_t statebuf[hash_descriptor->hash_state_length]; hal_hash_state_t *state = NULL; if ((err = hal_hash_initialize(NULL, hash_descriptor, &state, statebuf, sizeof(statebuf))) != HAL_OK || (err = hal_hash_update(state, plaintext, strlen((const char *) plaintext))) != HAL_OK || (err = hal_hash_finalize(state, hashbuf, sizeof(hashbuf))) != HAL_OK) return printf("Couldn't hash plaintext: %s\n", hal_error_string(err)), 0; } /* * Lazy but probably-good-enough guess on signature size -- want * explicit number in ecdsa_curve_t? */ uint8_t sigbuf[hash_descriptor->digest_length * 3]; size_t siglen; printf("Signing\n"); if ((err = hal_ecdsa_sign(NULL, key, hashbuf, sizeof(hashbuf), sigbuf, &siglen, sizeof(sigbuf))) != HAL_OK) return printf("hal_ecdsa_sign() failed: %s\n", hal_error_string(err)), 0; printf("Verifying\n"); if ((err = hal_ecdsa_verify(NULL, key, hashbuf, sizeof(hashbuf), sigbuf, siglen)) != HAL_OK) return printf("hal_ecdsa_verify() failed: %s\n", hal_error_string(err)), 0; return 1; } /* * Time a test. */ static void _time_check(const struct timeval t0, const int ok) { struct timeval t; gettimeofday(&t, NULL); t.tv_sec -= t0.tv_sec; t.tv_usec -= t0.tv_usec; if (t.tv_usec < 0) { t.tv_usec += 1000000; t.tv_sec -= 1; } printf("Elapsed time %lu.%06lu seconds, %s\n", (unsigned long) t.tv_sec, (unsigned long) t.tv_usec, ok ? "OK" : "FAILED"); } #define time_check(_expr_) \ do { \ struct timeval _t; \ gettimeofday(&_t, NULL); \ int _ok = (_expr_); \ _time_check(_t, _ok); \ ok &= _ok; \ } while (0) static void show_core(const hal_core_t *core, const char *whinge) { const hal_core_info_t *core_info = hal_core_info(core); if (core_info != NULL) printf("\"%8.8s\" \"%4.4s\"\n", core_info->name, core_info->version); else if (whinge != NULL) printf("%s core not present\n", whinge); } int main(void) { const hal_core_t *sha256_core = hal_core_find(SHA256_NAME, NULL); const hal_core_t *sha512_core = hal_core_find(SHA512_NAME, NULL); const hal_core_t *csprng_core = hal_core_find(CSPRNG_NAME, NULL); show_core(sha256_core, "sha-256"); show_core(sha512_core, "sha-512"); show_core(csprng_core, "csprng"); int ok = 1; #if HAL_ECDSA_DEBUG_ONLY_STATIC_TEST_VECTOR_RANDOM /* * Test vectors (where we have them). */ for (int i = 0; i < sizeof(ecdsa_tc)/sizeof(*ecdsa_tc); i++) time_check(test_against_static_vectors(&ecdsa_tc[i])); #endif /* * Generate/sign/verify test for each curve. */ if (csprng_core != NULL && sha256_core != NULL) { time_check(test_keygen_sign_verify(HAL_CURVE_P256)); } if (csprng_core != NULL && sha512_core != NULL) { time_check(test_keygen_sign_verify(HAL_CURVE_P384)); time_check(test_keygen_sign_verify(HAL_CURVE_P521)); } return !ok; } /* * Local variables: * indent-tabs-mode: nil * End: */