Populate cache on READ command when DNET_IO_FLAGS_CACHE ioflag is set
[elliptics.git] / library / crypto.c
blob2ee2a847fca3ffd9945a961efcb089743cfa7d6d
1 /*
2 * 2008+ Copyright (c) Evgeniy Polyakov <zbr@ioremap.net>
3 * All rights reserved.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
16 #include <stdio.h>
17 #include <stdlib.h>
18 #include <unistd.h>
19 #include <fcntl.h>
20 #include <signal.h>
22 #include "elliptics.h"
23 #include "elliptics/interface.h"
25 #include "crypto/sha512.h"
27 struct dnet_local_crypto_engine
29 struct dnet_lock lock;
30 void *ns;
31 int nsize;
34 static void dnet_transform_final(void *dst, const void *src, unsigned int *rsize, unsigned int rs)
36 if (*rsize < rs) {
37 memcpy((char *)dst, src, *rsize);
38 memset((char *)dst + *rsize, 0, rs - *rsize);
39 } else {
40 memcpy(dst, src, rs);
41 *rsize = rs;
45 static int dnet_local_digest_transform(void *priv, const void *src, uint64_t size,
46 void *dst, unsigned int *dsize, unsigned int flags __unused)
48 struct dnet_local_crypto_engine *e = priv;
49 unsigned int rs = *dsize;
50 unsigned char hash[64];
51 #if 1
52 struct sha512_ctx ctx;
54 sha512_init_ctx(&ctx);
56 if (e->nsize) {
57 char x = '\0';
59 dnet_lock_lock(&e->lock);
60 sha512_process_bytes(e->ns, e->nsize, &ctx);
61 sha512_process_bytes(&x, 1, &ctx);
62 dnet_lock_unlock(&e->lock);
65 sha512_process_bytes(src, size, &ctx);
66 sha512_finish_ctx(&ctx, hash);
68 #else
69 sha512_buffer(src, size, hash);
70 #endif
71 dnet_transform_final(dst, hash, dsize, rs);
72 return 0;
75 void dnet_crypto_cleanup(struct dnet_node *n)
77 struct dnet_transform *t = &n->transform;
78 struct dnet_local_crypto_engine *e = t->priv;
80 dnet_lock_destroy(&e->lock);
81 free(e);
84 int dnet_crypto_init(struct dnet_node *n, void *ns, int nsize)
86 struct dnet_local_crypto_engine *e;
87 struct dnet_transform *t = &n->transform;
88 int err = -ENOMEM;
90 e = malloc(sizeof(struct dnet_local_crypto_engine) + nsize);
91 if (!e)
92 goto err_out_exit;
94 memset(e, 0, sizeof(struct dnet_local_crypto_engine));
95 e->ns = e + 1;
97 memcpy(e->ns, ns, nsize);
98 e->nsize = nsize;
100 err = dnet_lock_init(&e->lock);
101 if (err) {
102 dnet_log_raw(n, DNET_LOG_ERROR, "Failed to initialize transform lock: %d.\n", err);
103 goto err_out_free;
106 t->transform = dnet_local_digest_transform;
107 t->priv = e;
109 return 0;
111 err_out_free:
112 free(e);
113 err_out_exit:
114 return err;
117 void *dnet_node_get_ns(struct dnet_node *n, int *nsize)
119 struct dnet_transform *t = &n->transform;
120 struct dnet_local_crypto_engine *e = t->priv;
122 *nsize = e->nsize;
123 return e->ns;
126 void dnet_node_set_ns(struct dnet_node *n, void *ns, int nsize)
128 struct dnet_transform *t = &n->transform;
129 struct dnet_local_crypto_engine *e = t->priv;
131 e->ns = ns;
132 e->nsize = nsize;