xref: /freebsd/sys/dev/glxsb/glxsb_hash.c (revision 59c8e88e72633afbc47a4ace0d2170d00d51f7dc)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2006 Pawel Jakub Dawidek <pjd@FreeBSD.org>
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #include <sys/param.h>
30 #include <sys/systm.h>
31 #include <sys/malloc.h>
32 
33 #include <opencrypto/xform.h>
34 
35 #include "glxsb.h"
36 
37 /*
38  * Implementation notes.
39  *
40  * The Geode LX Security Block provides AES-128-CBC acceleration.
41  * We implement all HMAC algorithms provided by crypto(9) framework so glxsb can work
42  * with ipsec(4)
43  *
44  * This code was stolen from crypto/via/padlock_hash.c
45  */
46 
47 MALLOC_DECLARE(M_GLXSB);
48 
49 static void
50 glxsb_hash_key_setup(struct glxsb_session *ses, const char *key, int klen)
51 {
52 	const struct auth_hash *axf;
53 
54 	axf = ses->ses_axf;
55 	hmac_init_ipad(axf, key, klen, ses->ses_ictx);
56 	hmac_init_opad(axf, key, klen, ses->ses_octx);
57 }
58 
59 /*
60  * Compute keyed-hash authenticator.
61  */
62 static int
63 glxsb_authcompute(struct glxsb_session *ses, struct cryptop *crp)
64 {
65 	u_char hash[HASH_MAX_LEN];
66 	const struct auth_hash *axf;
67 	union authctx ctx;
68 	int error;
69 
70 	axf = ses->ses_axf;
71 	bcopy(ses->ses_ictx, &ctx, axf->ctxsize);
72 	error = crypto_apply(crp, crp->crp_aad_start, crp->crp_aad_length,
73 	    axf->Update, &ctx);
74 	if (error != 0)
75 		return (error);
76 	error = crypto_apply(crp, crp->crp_payload_start,
77 	    crp->crp_payload_length, axf->Update, &ctx);
78 	if (error != 0)
79 		return (error);
80 
81 	axf->Final(hash, &ctx);
82 
83 	bcopy(ses->ses_octx, &ctx, axf->ctxsize);
84 	axf->Update(&ctx, hash, axf->hashsize);
85 	axf->Final(hash, &ctx);
86 	explicit_bzero(&ctx, sizeof(ctx));
87 
88 	/* Verify or inject the authentication data */
89 	if (crp->crp_op & CRYPTO_OP_VERIFY_DIGEST) {
90 		u_char hash2[HASH_MAX_LEN];
91 
92 		crypto_copydata(crp, crp->crp_digest_start, ses->ses_mlen,
93 		    hash2);
94 		if (timingsafe_bcmp(hash, hash2, ses->ses_mlen) != 0)
95 			error = EBADMSG;
96 		explicit_bzero(hash2, sizeof(hash2));
97 	} else
98 		crypto_copyback(crp, crp->crp_digest_start, ses->ses_mlen,
99 		    hash);
100 	explicit_bzero(hash, sizeof(hash));
101 	return (error);
102 }
103 
104 int
105 glxsb_hash_setup(struct glxsb_session *ses,
106     const struct crypto_session_params *csp)
107 {
108 
109 	ses->ses_axf = crypto_auth_hash(csp);
110 	if (csp->csp_auth_mlen == 0)
111 		ses->ses_mlen = ses->ses_axf->hashsize;
112 	else
113 		ses->ses_mlen = csp->csp_auth_mlen;
114 
115 	/* Allocate memory for HMAC inner and outer contexts. */
116 	ses->ses_ictx = malloc(ses->ses_axf->ctxsize, M_GLXSB,
117 	    M_ZERO | M_NOWAIT);
118 	ses->ses_octx = malloc(ses->ses_axf->ctxsize, M_GLXSB,
119 	    M_ZERO | M_NOWAIT);
120 	if (ses->ses_ictx == NULL || ses->ses_octx == NULL)
121 		return (ENOMEM);
122 
123 	/* Setup key if given. */
124 	if (csp->csp_auth_key != NULL) {
125 		glxsb_hash_key_setup(ses, csp->csp_auth_key,
126 		    csp->csp_auth_klen);
127 	}
128 	return (0);
129 }
130 
131 int
132 glxsb_hash_process(struct glxsb_session *ses,
133     const struct crypto_session_params *csp, struct cryptop *crp)
134 {
135 	int error;
136 
137 	if (crp->crp_auth_key != NULL)
138 		glxsb_hash_key_setup(ses, crp->crp_auth_key,
139 		    csp->csp_auth_klen);
140 
141 	error = glxsb_authcompute(ses, crp);
142 	return (error);
143 }
144 
145 void
146 glxsb_hash_free(struct glxsb_session *ses)
147 {
148 
149 	if (ses->ses_ictx != NULL) {
150 		zfree(ses->ses_ictx, M_GLXSB);
151 		ses->ses_ictx = NULL;
152 	}
153 	if (ses->ses_octx != NULL) {
154 		zfree(ses->ses_octx, M_GLXSB);
155 		ses->ses_octx = NULL;
156 	}
157 }
158