kernel - Minor rerrangement to help w/ low level debugging
[dragonfly.git] / contrib / cryptsetup / lib / backends.c
blob488ccef09c7958dc7ac57549d90029612f768870
1 #include <stdio.h>
2 #include <string.h>
3 #include <stdlib.h>
4 #include <errno.h>
5 #include <openssl/evp.h>
7 #include "libcryptsetup.h"
8 #include "internal.h"
10 int init_crypto(void)
12 return 0;
15 int hash(const char *backend_name, const char *hash_name,
16 char *result, size_t size,
17 const char *passphrase, size_t sizep)
19 EVP_MD_CTX mdctx;
20 const EVP_MD *md;
21 size_t pad = 0;
22 int r = -ENOENT;
24 OpenSSL_add_all_digests();
25 md = EVP_get_digestbyname(hash_name);
26 if (md == NULL) {
27 set_error("Unknown hash type %s", hash_name);
28 goto out;
31 if (EVP_MD_size(md) > size) {
32 set_error("requested hash length (%zd) > key length (%zd)", EVP_MD_size(md), size);
33 return -EINVAL;
36 pad = size - EVP_MD_size(md);
38 EVP_DigestInit(&mdctx, md);
39 EVP_DigestUpdate(&mdctx, passphrase, sizep);
40 r = !EVP_DigestFinal(&mdctx, result, NULL);
42 if (pad) {
43 memset(result+size, 0, pad);
46 out:
47 return r;