2 * 2008+ Copyright (c) Evgeniy Polyakov <zbr@ioremap.net>
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.
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
;
34 static void dnet_transform_final(void *dst
, const void *src
, unsigned int *rsize
, unsigned int rs
)
37 memcpy((char *)dst
, src
, *rsize
);
38 memset((char *)dst
+ *rsize
, 0, rs
- *rsize
);
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];
52 struct sha512_ctx ctx
;
54 sha512_init_ctx(&ctx
);
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
);
69 sha512_buffer(src
, size
, hash
);
71 dnet_transform_final(dst
, hash
, dsize
, rs
);
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
);
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
;
90 e
= malloc(sizeof(struct dnet_local_crypto_engine
) + nsize
);
94 memset(e
, 0, sizeof(struct dnet_local_crypto_engine
));
97 memcpy(e
->ns
, ns
, nsize
);
100 err
= dnet_lock_init(&e
->lock
);
102 dnet_log_raw(n
, DNET_LOG_ERROR
, "Failed to initialize transform lock: %d.\n", err
);
106 t
->transform
= dnet_local_digest_transform
;
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
;
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
;