Skip to content

Instantly share code, notes, and snippets.

@leiless
Last active November 25, 2021 11:31
Show Gist options
  • Save leiless/53ba95b20b6552a532f73a1823481628 to your computer and use it in GitHub Desktop.
Save leiless/53ba95b20b6552a532f73a1823481628 to your computer and use it in GitHub Desktop.
ISA-L Crypto MD5 submit & flush example (a single hash job)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <assert.h>
#include <isa-l_crypto.h>
#include <endian.h>
static MD5_HASH_CTX *
submit_flush(MD5_HASH_CTX_MGR *mgr, MD5_HASH_CTX *ctx, const void *buffer, HASH_CTX_FLAG flags) {
uint32_t len = strlen((char *) buffer);
MD5_HASH_CTX *job = md5_ctx_mgr_submit(mgr, ctx, buffer, len, flags);
if (job != NULL) {
assert(job == ctx);
assert(job->error == HASH_CTX_ERROR_NONE);
}
job = md5_ctx_mgr_flush(mgr);
if (job != NULL) {
assert(job == ctx);
assert(job->error == HASH_CTX_ERROR_NONE);
}
return job;
}
static void print_md5(MD5_HASH_CTX *job) {
const uint32_t *p = job->job.result_digest;
printf("%x%x%x%x\n",
htobe32(p[0]),
htobe32(p[1]),
htobe32(p[2]),
htobe32(p[3])
);
}
int main(void) {
printf("ISA-L crypto version: %u.%u.%u\n",
ISAL_CRYPTO_MAJOR_VERSION, ISAL_CRYPTO_MINOR_VERSION, ISAL_CRYPTO_PATCH_VERSION);
MD5_HASH_CTX_MGR *mgr = NULL;
int e = posix_memalign((void *) &mgr, 16, sizeof *mgr);
assert(e == 0);
assert(mgr != NULL);
md5_ctx_mgr_init(mgr);
MD5_HASH_CTX ctx;
hash_ctx_init(&ctx);
(void) submit_flush(mgr, &ctx, "a", HASH_FIRST);
(void) submit_flush(mgr, &ctx, "b", HASH_UPDATE);
(void) submit_flush(mgr, &ctx, "c", HASH_LAST);
assert(ctx.error == HASH_CTX_ERROR_NONE);
print_md5(&ctx);
free(mgr);
return 0;
}
@leiless
Copy link
Author

leiless commented Nov 8, 2021

$ gcc -Wall -Werror isal_crypto_submit_flush.c -lisal_crypto && ./a.out
ISA-L crypto version: 2.24.0
900150983cd24fb0d6963f7d28e17f72

$ echo -n abc | md5sum | cut -d' ' -f1
900150983cd24fb0d6963f7d28e17f72

@leiless
Copy link
Author

leiless commented Nov 25, 2021

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cstdint>
#include <cassert>
#include <isa-l_crypto.h>
#include <endian.h>
#include <unistd.h>
#include <sys/time.h>

#define HASH_CTX_MGR    SHA256_HASH_CTX_MGR
#define HASH_CTX        SHA256_HASH_CTX
#define BLOCK_SIZE      SHA256_BLOCK_SIZE
#define ctx_mgr_init    sha256_ctx_mgr_init
#define ctx_mgr_submit  sha256_ctx_mgr_submit
#define ctx_mgr_flush   sha256_ctx_mgr_flush

#define ARRAY_SIZE(a)   (sizeof(a) / sizeof(*(a)))

static void print_hash(HASH_CTX *job, bool htobe) {
    const auto *p = job->job.result_digest;
    auto bytes_count = sizeof job->job.result_digest[0];

    for (size_t i = 0; i < ARRAY_SIZE(job->job.result_digest); i++) {
        if (htobe) {
            if (bytes_count == 4) {
                printf("%08x", htobe32(p[i]));
            } else if (bytes_count == 8) {
                printf("%016lx", htobe64(p[i]));
            } else {
                assert(0);
            }
        } else {
            if (bytes_count == 4) {
                printf("%08x", p[i]);
            } else if (bytes_count == 8) {
                printf("%016lx", p[i]);
            } else {
                assert(0);
            }
        }
    }

    putchar('\n');
}

static HASH_CTX *
submit_flush(HASH_CTX_MGR *mgr, HASH_CTX *ctx, const void *buffer, uint32_t len, HASH_CTX_FLAG flags) {
    HASH_CTX *job = ctx_mgr_submit(mgr, ctx, buffer, len, flags);
    if (job == nullptr && hash_ctx_processing(ctx)) {
        HASH_CTX *job2 = ctx_mgr_flush(mgr);
        if (job2 != nullptr) {
            assert(job2 == ctx);
            assert(job2->error == HASH_CTX_ERROR_NONE);
            if (flags & HASH_LAST) {
                job = job2;
            }
        }
    }
    return job;
}

static void calc_hash(const char *filename) {
    HASH_CTX_MGR *mgr = nullptr;

    int e = posix_memalign((void **) &mgr, 16, sizeof *mgr);
    assert(e == 0);
    assert(mgr != nullptr);
    ctx_mgr_init(mgr);

    HASH_CTX ctx;
    hash_ctx_init(&ctx);

    FILE *fp = fopen(filename, "r");
    //char buffer[BLOCK_SIZE * 2];
    char buffer[4 * 1024 * 1024];
    int i = 0;
    size_t nRead;
    HASH_CTX_FLAG flag;
    while (!feof(fp)) {
        //size_t x = ((unsigned long) random()) % sizeof buffer;
        nRead = fread(&buffer, 1, sizeof buffer, fp);
        e = ferror(fp);
        assert(e == 0);
        flag = i != 0 ? HASH_UPDATE : HASH_FIRST;
        submit_flush(mgr, &ctx, buffer, nRead, flag);
        i = 1;
    }
    (void) fclose(fp);

    HASH_CTX *ctx2 = submit_flush(mgr, &ctx, nullptr, 0, HASH_LAST);
    assert(ctx2 != nullptr);
    assert(ctx2 == &ctx);
    assert(ctx2->error == HASH_CTX_ERROR_NONE);

    print_hash(&ctx, false);

    free(mgr);
}

static void rand_init() {
    struct timeval tv;
    gettimeofday(&tv, nullptr);
    srandom((getpid() << 16) ^ tv.tv_sec ^ tv.tv_usec);
}

int main(int argc, const char *argv[]) {
    if (argc <= 1) {
        printf("ISA-L crypto version: %u.%u.%u\n",
               ISAL_CRYPTO_MAJOR_VERSION, ISAL_CRYPTO_MINOR_VERSION, ISAL_CRYPTO_PATCH_VERSION);
        return 0;
    }

    rand_init();

    for (int i = 1; i < argc; i++) {
        calc_hash(argv[i]);
    }

    return 0;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment