Deleted Added
sdiff udiff text old ( 275732 ) new ( 293306 )
full compact
1/*-
2 * Copyright (c) 2005-2010 Pawel Jakub Dawidek <pjd@FreeBSD.org>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright

--- 11 unchanged lines hidden (view full) ---

20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 */
26
27#include <sys/cdefs.h>
28__FBSDID("$FreeBSD: head/sys/geom/eli/g_eli_hmac.c 293306 2016-01-07 05:47:34Z allanjude $");
29
30#include <sys/param.h>
31#ifdef _KERNEL
32#include <sys/systm.h>
33#include <sys/kernel.h>
34#include <sys/malloc.h>
35#else
36#include <stdint.h>
37#include <string.h>
38#include <strings.h>
39#include <errno.h>
40#include <assert.h>
41#include <openssl/evp.h>
42#define _OpenSSL_
43#endif
44#include <geom/eli/g_eli.h>
45
46void
47g_eli_crypto_hmac_init(struct hmac_ctx *ctx, const uint8_t *hkey,
48 size_t hkeylen)
49{
50 u_char k_ipad[128], key[128];
51 SHA512_CTX lctx;
52 u_int i;
53

--- 55 unchanged lines hidden (view full) ---

109 size_t datasize, uint8_t *md, size_t mdsize)
110{
111 struct hmac_ctx ctx;
112
113 g_eli_crypto_hmac_init(&ctx, hkey, hkeysize);
114 g_eli_crypto_hmac_update(&ctx, data, datasize);
115 g_eli_crypto_hmac_final(&ctx, md, mdsize);
116}
117
118/*
119 * Here we generate IV. It is unique for every sector.
120 */
121void
122g_eli_crypto_ivgen(struct g_eli_softc *sc, off_t offset, u_char *iv,
123 size_t size)
124{
125 uint8_t off[8];
126
127 if ((sc->sc_flags & G_ELI_FLAG_NATIVE_BYTE_ORDER) != 0)
128 bcopy(&offset, off, sizeof(off));
129 else
130 le64enc(off, (uint64_t)offset);
131
132 switch (sc->sc_ealgo) {
133 case CRYPTO_AES_XTS:
134 bcopy(off, iv, sizeof(off));
135 bzero(iv + sizeof(off), size - sizeof(off));
136 break;
137 default:
138 {
139 u_char hash[SHA256_DIGEST_LENGTH];
140 SHA256_CTX ctx;
141
142 /* Copy precalculated SHA256 context for IV-Key. */
143 bcopy(&sc->sc_ivctx, &ctx, sizeof(ctx));
144 SHA256_Update(&ctx, off, sizeof(off));
145 SHA256_Final(hash, &ctx);
146 bcopy(hash, iv, MIN(sizeof(hash), size));
147 break;
148 }
149 }
150}