Hi all! This is just my first post in this mailing list!

I've just discover cryptodev-linux because I'm trying to use the crypto 
hardware that I've
in a DM8148 demoboard from TI. I would like to implement a 'md5sum' replace 
using
the hardware. At the moment I can verify the hardware it's working because the 
OpenSSL
is using it for the digest.

I can do

openssl dgst -md5 <file>

I would like to extract this functionality in a little tool like md5sum in 
order to compute
the md5 hash for a file. In the examples directory I can find the sha.c example 
but it's
working in a limited buffer. I've implement the next but I only get the md5 
digest from
the last block. Is it any way to do the hash from several blocks and 
'accumulate' the
md5 digest to get the final correct one?

Thanks!

---

Based on sha.c

/*
 * Demo on how to use /dev/crypto device for ciphering.
 *
 * Placed under public domain.
 *
 */
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <crypto/cryptodev.h>
#include "md5.h"


#define MD5_DIGEST_BLOCKSIZE    64


int md5_ctx_init(struct cryptodev_ctx* ctx, int cfd, const uint8_t *key, 
unsigned int key_size)
{
        memset(ctx, 0, sizeof(*ctx));
        ctx->cfd = cfd;

        if (key == NULL)
                ctx->sess.mac = CRYPTO_MD5;
        else {
                ctx->sess.mac = CRYPTO_MD5_HMAC;
                ctx->sess.mackeylen = key_size;
                ctx->sess.mackey = (void*)key;
        }

        if (ioctl(ctx->cfd, CIOCGSESSION, &ctx->sess)) {
                perror("ioctl(CIOCGSESSION)");
                return -1;
        }

        return 0;
}

void md5_ctx_deinit(struct cryptodev_ctx* ctx)
{
        if (ioctl(ctx->cfd, CIOCFSESSION, &ctx->sess.ses)) {
                perror("ioctl(CIOCFSESSION)");
        }
}

int md5_hash(struct cryptodev_ctx* ctx, const void* text, size_t size, void* 
digest)
{
        struct crypt_op cryp;
        void* p;

        /* check text and ciphertext alignment */
        if (ctx->alignmask) {
                p = (void*)(((unsigned long)text + ctx->alignmask) & 
~ctx->alignmask);
                if (text != p) {
                        fprintf(stderr, "text is not aligned\n");
                        return -1;
                }
        }

        memset(&cryp, 0, sizeof(cryp));

        /* Encrypt data.in to data.encrypted */
        cryp.ses = ctx->sess.ses;
        cryp.len = size;
        cryp.src = (void*)text;
        cryp.mac = digest;
        if (ioctl(ctx->cfd, CIOCCRYPT, &cryp)) {
                perror("ioctl(CIOCCRYPT)");
                return -1;
        }

        return 0;
}

int main( int argc, char **argv )
{
        int cfd = -1;
        int i, fd;
        struct cryptodev_ctx ctx;
        uint8_t digest[16];
        uint8_t block[MD5_DIGEST_BLOCKSIZE];
        size_t  nread;

        /* Check args */
        if (argc < 2) {
                printf("Usage: %s <filename>\n", argv[0]);
                return 1;
        }

        /* Open file to hash */
        fd = open(argv[1], O_RDONLY, 0);
        if (fd < 0) {
                printf("open(%s)", argv[1]);
                return 1;
        }

        /* Open the crypto device */
        cfd = open("/dev/crypto", O_RDWR, 0);
        if (cfd < 0) {
                perror("open(/dev/crypto)");
                return 1;
        }

        /* Set close-on-exec (not really neede here) */
        if (fcntl(cfd, F_SETFD, 1) == -1) {
                perror("fcntl(F_SETFD)");
                return 1;
        }

        md5_ctx_init(&ctx, cfd, NULL, 0);

        do {
                nread = read(fd, &block, MD5_DIGEST_BLOCKSIZE);
                if (nread < 0) {
                        perror("read()");
                        return 1;
                }
                if (nread > 0) {
                        // TODO: Only last block md5!! How can I propagate it 
to next op???
                        printf("block size: %d\n", nread);
                        md5_hash(&ctx, block, nread, digest);
                }
        } while (nread > 0);

        md5_ctx_deinit(&ctx);

        /* Print digest */
        for (i = 0; i < 16; i++) {
                printf("%02x", digest[i]);
        }
        printf("  %s\n", argv[1]);

        /* Close the original descriptor */
        if (close(cfd)) {
                perror("close(cfd)");
                return 1;
        }

        close(fd);

        return 0;
}

Este mensaje se dirige exclusivamente a su destinatario y puede contener 
información privilegiada o CONFIDENCIAL. Si no es vd. el destinatario indicado, 
queda notificado de que la utilización, divulgación y/o copia sin autorización 
está prohibida en virtud de la legislación vigente. Si ha recibido este mensaje 
por error, le rogamos que nos lo comunique inmediatamente por esta misma vía y 
proceda a su destrucción.

This message is intended exclusively for its addressee and may contain 
information that is CONFIDENTIAL and protected by professional privilege.
If you are not the intended recipient you are hereby notified that any 
dissemination, copy or disclosure of this communication is strictly prohibited 
by law. If this message has been received in error, please immediately notify 
us via e-mail and delete it.
_______________________________________________
Cryptodev-linux-devel mailing list
Cryptodev-linux-devel@gna.org
https://mail.gna.org/listinfo/cryptodev-linux-devel

Reply via email to