+static int test_contexts_digest(int nid, int mac)
+{
+ int ret = 0, test = 0;
+ unsigned char K[32] = {1};
+ const EVP_MD *type = EVP_get_digestbynid(nid);
+ const char *name = EVP_MD_name(type);
+
+ printf(cBLUE "Digest test for %s (nid %d)" cNORM "\n", name, nid);
+
+ /* produce base digest */
+ EVP_MD_CTX *ctx, *save;
+ unsigned char pt[TEST_SIZE] = {1};
+ unsigned char b[EVP_MAX_MD_SIZE] = {0};
+ unsigned char c[EVP_MAX_MD_SIZE];
+ unsigned int outlen, tmplen;
+
+ /* Simply digest whole input. */
+ T(ctx = EVP_MD_CTX_new());
+ T(EVP_DigestInit_ex(ctx, type, NULL));
+ if (mac)
+ T(EVP_MD_CTX_ctrl(ctx, EVP_MD_CTRL_SET_KEY, sizeof(K), (void *)K));
+ T(EVP_DigestUpdate(ctx, pt, sizeof(pt)));
+ T(EVP_DigestFinal_ex(ctx, b, &tmplen));
+ save = ctx; /* will be not freed while cloning */
+
+ /* cloned digest */
+ EVP_MD_CTX_reset(ctx); /* test double reset */
+ EVP_MD_CTX_reset(ctx);
+ T(EVP_DigestInit_ex(ctx, type, NULL));
+ if (mac)
+ T(EVP_MD_CTX_ctrl(ctx, EVP_MD_CTRL_SET_KEY, sizeof(K), (void *)K));
+ printf(" cloned contexts: ");
+ memset(c, 0, sizeof(c));
+ int i;
+ for (i = 0; i < TEST_SIZE / STEP_SIZE; i++) {
+ /* Clone and continue digesting next part of input. */
+ EVP_MD_CTX *copy;
+ T(copy = EVP_MD_CTX_new());
+ T(EVP_MD_CTX_copy_ex(copy, ctx));
+
+ /* rolling */
+ if (save != ctx)
+ EVP_MD_CTX_free(ctx);
+ ctx = copy;
+
+ T(EVP_DigestUpdate(ctx, pt + STEP_SIZE * i, STEP_SIZE));
+ }
+ outlen = i * STEP_SIZE;
+ T(EVP_DigestFinal_ex(ctx, c, &tmplen));
+ /* Should be same as the simple digest. */
+ TEST_ASSERT(outlen != TEST_SIZE || memcmp(c, b, EVP_MAX_MD_SIZE));
+ EVP_MD_CTX_free(ctx);
+ if (test) {
+ printf(" b[%d] = ", outlen);
+ hexdump(b, outlen);
+ printf(" c[%d] = ", outlen);
+ hexdump(c, outlen);
+ }
+ ret |= test;
+
+ /* Resume original context, what if it's damaged? */
+ printf(" base context: ");
+ memset(c, 0, sizeof(c));
+ T(EVP_DigestUpdate(save, pt, sizeof(pt)));
+ T(EVP_DigestFinal_ex(save, c, &tmplen));
+ TEST_ASSERT(outlen != TEST_SIZE || memcmp(c, b, EVP_MAX_MD_SIZE));
+ EVP_MD_CTX_free(save);
+ if (test) {
+ printf(" b[%d] = ", outlen);
+ hexdump(b, outlen);
+ printf(" c[%d] = ", outlen);
+ hexdump(c, outlen);
+ }
+ ret |= test;
+
+ return ret;
+}
+
+static struct testcase_cipher {