ca2b96c71b9866558bd8d70ff9270d46c0fd3417
[dragonfly.git] / sys / dev / crypto / glxsb / glxsb_hash.c
1 /*-
2  * Copyright (c) 2006 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
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
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
29 #include <sys/param.h>
30 #include <sys/systm.h>
31 #include <sys/malloc.h>
32
33 #include <opencrypto/cryptosoft.h> /* for hmac_ipad_buffer and hmac_opad_buffer */
34 #include <opencrypto/xform.h>
35
36 #include "glxsb.h"
37
38 /*
39  * Implementation notes.
40  *
41  * The Geode LX Security Block provides AES-128-CBC acceleration.
42  * We implement all HMAC algorithms provided by crypto(9) framework so glxsb can work
43  * with ipsec(4)
44  *
45  * This code was stolen from crypto/via/padlock_hash.c
46  */
47
48 MALLOC_DECLARE(M_GLXSB);
49
50 static void
51 glxsb_hash_key_setup(struct glxsb_session *ses, caddr_t key, int klen)
52 {
53         struct auth_hash *axf;
54         int i;
55
56         klen /= 8;
57         axf = ses->ses_axf;
58
59         for (i = 0; i < klen; i++)
60                 key[i] ^= HMAC_IPAD_VAL;
61
62         axf->Init(ses->ses_ictx);
63         axf->Update(ses->ses_ictx, key, klen);
64         axf->Update(ses->ses_ictx, hmac_ipad_buffer, axf->blocksize - klen);
65
66         for (i = 0; i < klen; i++)
67                 key[i] ^= (HMAC_IPAD_VAL ^ HMAC_OPAD_VAL);
68
69         axf->Init(ses->ses_octx);
70         axf->Update(ses->ses_octx, key, klen);
71         axf->Update(ses->ses_octx, hmac_opad_buffer, axf->blocksize - klen);
72
73         for (i = 0; i < klen; i++)
74                 key[i] ^= HMAC_OPAD_VAL;
75 }
76
77 /*
78  * Compute keyed-hash authenticator.
79  */
80 static int
81 glxsb_authcompute(struct glxsb_session *ses, struct cryptodesc *crd,
82     caddr_t buf, int flags)
83 {
84         u_char hash[HASH_MAX_LEN];
85         struct auth_hash *axf;
86         union authctx ctx;
87         int error;
88
89         axf = ses->ses_axf;
90         bcopy(ses->ses_ictx, &ctx, axf->ctxsize);
91         error = crypto_apply(flags, buf, crd->crd_skip, crd->crd_len,
92             (int (*)(void *, void *, unsigned int))axf->Update, (caddr_t)&ctx);
93         if (error != 0)
94                 return (error);
95         axf->Final(hash, &ctx);
96
97         bcopy(ses->ses_octx, &ctx, axf->ctxsize);
98         axf->Update(&ctx, hash, axf->hashsize);
99         axf->Final(hash, &ctx);
100
101         /* Inject the authentication data */
102         crypto_copyback(flags, buf, crd->crd_inject,
103         ses->ses_mlen == 0 ? axf->hashsize : ses->ses_mlen, hash);
104         return (0);
105 }
106
107 int
108 glxsb_hash_setup(struct glxsb_session *ses, struct cryptoini *macini)
109 {
110
111         ses->ses_mlen = macini->cri_mlen;
112
113         /* Find software structure which describes HMAC algorithm. */
114         switch (macini->cri_alg) {
115         case CRYPTO_NULL_HMAC:
116                 ses->ses_axf = &auth_hash_null;
117                 break;
118         case CRYPTO_MD5_HMAC:
119                 ses->ses_axf = &auth_hash_hmac_md5;
120                 break;
121         case CRYPTO_SHA1_HMAC:
122                 ses->ses_axf = &auth_hash_hmac_sha1;
123                 break;
124         case CRYPTO_RIPEMD160_HMAC:
125                 ses->ses_axf = &auth_hash_hmac_ripemd_160;
126                 break;
127         case CRYPTO_SHA2_256_HMAC:
128                 ses->ses_axf = &auth_hash_hmac_sha2_256;
129                 break;
130         case CRYPTO_SHA2_384_HMAC:
131                 ses->ses_axf = &auth_hash_hmac_sha2_384;
132                 break;
133         case CRYPTO_SHA2_512_HMAC:
134                 ses->ses_axf = &auth_hash_hmac_sha2_512;
135                 break;
136         }
137
138         /* Allocate memory for HMAC inner and outer contexts. */
139         ses->ses_ictx = kmalloc(ses->ses_axf->ctxsize, M_GLXSB,
140             M_ZERO | M_NOWAIT);
141         ses->ses_octx = kmalloc(ses->ses_axf->ctxsize, M_GLXSB,
142             M_ZERO | M_NOWAIT);
143         if (ses->ses_ictx == NULL || ses->ses_octx == NULL)
144                 return (ENOMEM);
145
146         /* Setup key if given. */
147         if (macini->cri_key != NULL) {
148                 glxsb_hash_key_setup(ses, macini->cri_key,
149                     macini->cri_klen);
150         }
151         return (0);
152 }
153
154 int
155 glxsb_hash_process(struct glxsb_session *ses, struct cryptodesc *maccrd,
156     struct cryptop *crp)
157 {
158         int error;
159
160         if ((maccrd->crd_flags & CRD_F_KEY_EXPLICIT) != 0)
161                 glxsb_hash_key_setup(ses, maccrd->crd_key, maccrd->crd_klen);
162
163         error = glxsb_authcompute(ses, maccrd, crp->crp_buf, crp->crp_flags);
164         return (error);
165 }
166
167 void
168 glxsb_hash_free(struct glxsb_session *ses)
169 {
170
171         if (ses->ses_ictx != NULL) {
172                 bzero(ses->ses_ictx, ses->ses_axf->ctxsize);
173                 kfree(ses->ses_ictx, M_GLXSB);
174                 ses->ses_ictx = NULL;
175         }
176         if (ses->ses_octx != NULL) {
177                 bzero(ses->ses_octx, ses->ses_axf->ctxsize);
178                 kfree(ses->ses_octx, M_GLXSB);
179                 ses->ses_octx = NULL;
180         }
181 }