+static int test_contexts_digest(const char *name)
+{
+ EVP_MD *type;
+ ERR_set_mark();
+ T((type = (EVP_MD *)EVP_get_digestbyname(name))
+ || (type = EVP_MD_fetch(NULL, name, NULL)));
+ ERR_pop_to_mark();
+
+ printf(cBLUE "Digest test for %s" cNORM "\n", name);
+ int ret = test_contexts_digest_or_legacy_mac(type, 0);
+ EVP_MD_free(type);
+ return ret;
+}
+
+static int test_contexts_mac(const char *name)
+{
+ int ret = 0, test = 0;
+ unsigned char K[32] = {1};
+ const EVP_MD *type = EVP_get_digestbyname(name);
+ EVP_MAC *mac;
+
+ if (type) {
+ printf(cBLUE "Mac via EVP_MD test for %s" cNORM "\n", name);
+ return test_contexts_digest_or_legacy_mac(type, 1);
+ }
+
+ T(mac = EVP_MAC_fetch(NULL, name, NULL));
+ printf(cBLUE "Mac test for %s" cNORM "\n", name);
+
+ /* produce base mac */
+ EVP_MAC_CTX *ctx;
+ unsigned char pt[TEST_SIZE] = {1};
+ unsigned char b[EVP_MAX_MD_SIZE] = {0};
+ unsigned char c[EVP_MAX_MD_SIZE] = {0};
+ size_t outlen, tmplen;
+
+ /* Simply mac whole input. */
+ T(ctx = EVP_MAC_CTX_new(mac));
+ T(EVP_MAC_init(ctx, K, sizeof(K), NULL));
+ T(EVP_MAC_update(ctx, pt, sizeof(pt)));
+ T(EVP_MAC_final(ctx, b, &tmplen, sizeof(b)));
+ EVP_MAC_CTX_free(ctx);
+
+ /* Mac with rolling input. */
+ printf(" cloned contexts: ");
+ T(ctx = EVP_MAC_CTX_new(mac));
+ T(EVP_MAC_init(ctx, K, sizeof(K), NULL));
+ int i;
+ for (i = 0; i < TEST_SIZE / STEP_SIZE; i++) {
+ T(EVP_MAC_update(ctx, pt + STEP_SIZE * i, STEP_SIZE));
+ }
+ outlen = i * STEP_SIZE;
+ T(EVP_MAC_final(ctx, c, &tmplen, sizeof(c)));
+ EVP_MAC_CTX_free(ctx);
+ EVP_MAC_free(mac);
+
+ /* Rolling mac should give the same result as the simple mac. */
+ TEST_ASSERT(outlen != TEST_SIZE || memcmp(c, b, EVP_MAX_MD_SIZE));
+
+ if (test) {
+ printf(" b[%d] = ", (int)outlen);
+ hexdump(b, outlen);
+ printf(" c[%d] = ", (int)outlen);
+ hexdump(c, outlen);
+ }
+ ret |= test;
+
+ return ret;
+}
+