Skip to content

Commit

Permalink
Switch from old to new MD5 API in tester3 (quad).
Browse files Browse the repository at this point in the history
Propagate change from shibatch#489 to quad-tester.
  • Loading branch information
blapie committed Oct 10, 2024
1 parent dcf7ebd commit 3caed40
Showing 1 changed file with 41 additions and 21 deletions.
62 changes: 41 additions & 21 deletions src/quad-tester/tester3printf.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#include <string.h>
#include <assert.h>

#include <openssl/md5.h>
#include <openssl/evp.h>

#include "sleefquad.h"

Expand All @@ -24,7 +24,7 @@ static void convertEndianness(void *ptr, int len) {
#endif
}

static void testem(MD5_CTX *ctx, Sleef_quad val, char *types) {
static void testem(EVP_MD_CTX *ctx, Sleef_quad val, char *types) {
for(int alt=0;alt<2;alt++) {
for(int zero=0;zero<2;zero++) {
for(int left=0;left<2;left++) {
Expand All @@ -43,10 +43,10 @@ static void testem(MD5_CTX *ctx, Sleef_quad val, char *types) {

r = Sleef_snprintf(buf, 99, fmt, &val);
assert(r < 100);
MD5_Update(ctx, buf, r < 0 ? 0 : r);
EVP_DigestUpdate(ctx, buf, r < 0 ? 0 : r);
q = Sleef_strtoq(buf, NULL);
convertEndianness(&q, sizeof(q));
MD5_Update(ctx, &q, sizeof(Sleef_quad));
EVP_DigestUpdate(ctx, &q, sizeof(Sleef_quad));

for(int width=0;width<=40;width += 2) {
snprintf(fmt, 99, "%%%s%s%s%s%s%d.%s",
Expand All @@ -59,10 +59,10 @@ static void testem(MD5_CTX *ctx, Sleef_quad val, char *types) {

r = Sleef_snprintf(buf, 99, fmt, &val);
assert(r < 100);
MD5_Update(ctx, buf, r < 0 ? 0 : r);
EVP_DigestUpdate(ctx, buf, r < 0 ? 0 : r);
q = Sleef_strtoq(buf, NULL);
convertEndianness(&q, sizeof(q));
MD5_Update(ctx, &q, sizeof(Sleef_quad));
EVP_DigestUpdate(ctx, &q, sizeof(Sleef_quad));
}

for(int prec=0;prec<=40;prec += 3) {
Expand All @@ -77,10 +77,10 @@ static void testem(MD5_CTX *ctx, Sleef_quad val, char *types) {

r = Sleef_snprintf(buf, 99, fmt, &val);
assert(r < 100);
MD5_Update(ctx, buf, r < 0 ? 0 : r);
EVP_DigestUpdate(ctx, buf, r < 0 ? 0 : r);
q = Sleef_strtoq(buf, NULL);
convertEndianness(&q, sizeof(q));
MD5_Update(ctx, &q, sizeof(Sleef_quad));
EVP_DigestUpdate(ctx, &q, sizeof(Sleef_quad));
}

snprintf(fmt, 99, "%%%s%s%s%s%s.%d%s",
Expand All @@ -93,10 +93,10 @@ static void testem(MD5_CTX *ctx, Sleef_quad val, char *types) {

r = Sleef_snprintf(buf, 99, fmt, &val);
assert(r < 100);
MD5_Update(ctx, buf, r < 0 ? 0 : r);
EVP_DigestUpdate(ctx, buf, r < 0 ? 0 : r);
q = Sleef_strtoq(buf, NULL);
convertEndianness(&q, sizeof(q));
MD5_Update(ctx, &q, sizeof(Sleef_quad));
EVP_DigestUpdate(ctx, &q, sizeof(Sleef_quad));
}
}
}
Expand Down Expand Up @@ -233,21 +233,41 @@ int main(int argc, char **argv) {
//

for(int j=0;j<4;j++) {
MD5_CTX ctx;
memset(&ctx, 0, sizeof(MD5_CTX));
MD5_Init(&ctx);
// Init digest
EVP_MD_CTX *ctx; ctx = EVP_MD_CTX_new();
if (!ctx) {
fprintf(stderr, "Error creating context.\n");
return 0;
}
if (!EVP_DigestInit_ex(ctx, EVP_md5(), NULL)) {
fprintf(stderr, "Error initializing context.\n");
return 0;
}

// Run test and update digest
for(int i=0;i<sizeof(vals)/sizeof(Sleef_quad);i++) {
testem(&ctx, vals[i], types[j]);
testem(&ctx, Sleef_negq1_purec(vals[i]), types[j]);
testem(ctx, vals[i], types[j]);
testem(ctx, Sleef_negq1_purec(vals[i]), types[j]);
}

unsigned char d[16], mes[64], buf[64];
MD5_Final(d, &ctx);

snprintf((char *)mes, 60, "%s %02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
types[j], d[0],d[1],d[2],d[3],d[4],d[5],d[6],d[7],
d[8],d[9],d[10],d[11],d[12],d[13],d[14],d[15]);
// Check digest
unsigned int md5_digest_len = EVP_MD_size(EVP_md5());
unsigned char *md5_digest;
md5_digest = (unsigned char *)malloc(md5_digest_len);
if (!EVP_DigestFinal_ex(ctx, md5_digest, &md5_digest_len)) {
fprintf(stderr, "Error finalizing digest.\n");
return 0;
}
EVP_MD_CTX_free(ctx);
unsigned char mes[64], buf[64];
memset(mes, 0, 64);
sprintf((char *)mes, "%s ", types[j]);
char tmp[3] = { 0 };
for (int i = 0; i < md5_digest_len; i++) {
sprintf(tmp, "%02x", md5_digest[i]);
strcat((char *)mes, tmp);
}
free(md5_digest);

if (fp != NULL) {
fgets((char *)buf, 60, fp);
Expand Down

0 comments on commit 3caed40

Please sign in to comment.