2 /* $KAME: esp_core.c,v 1.50 2000/11/02 12:27:38 itojun Exp $ */
5 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the project nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 #include "opt_inet6.h"
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/malloc.h>
40 #include <sys/domain.h>
41 #include <sys/protosw.h>
42 #include <sys/socket.h>
43 #include <sys/errno.h>
45 #include <sys/syslog.h>
48 #include <net/route.h>
50 #include <netinet/in.h>
51 #include <netinet/in_var.h>
53 #include <netinet/ip6.h>
54 #include <netinet6/ip6_var.h>
55 #include <netinet/icmp6.h>
58 #include <netinet6/ipsec.h>
60 #include <netinet6/ipsec6.h>
62 #include <netinet6/ah.h>
64 #include <netinet6/ah6.h>
66 #include <netinet6/esp.h>
68 #include <netinet6/esp6.h>
70 #include <netinet6/esp_rijndael.h>
71 #include <netinet6/esp_aesctr.h>
72 #include <net/pfkeyv2.h>
73 #include <netkey/keydb.h>
74 #include <netkey/key.h>
76 #include <crypto/des/des.h>
77 #include <crypto/blowfish/blowfish.h>
79 #include <opencrypto/cast.h>
80 #define cast128_key cast_key
81 #define cast128_setkey(key, rawkey, keybytes) \
82 cast_setkey((key), (rawkey), (keybytes))
83 #define cast128_encrypt(key, inblock, outblock) \
84 cast_encrypt((key), (inblock), (outblock))
85 #define cast128_decrypt(key, inblock, outblock) \
86 cast_decrypt((key), (inblock), (outblock))
88 #include <net/net_osdep.h>
90 static int esp_null_mature __P((struct secasvar *));
91 static int esp_null_decrypt __P((struct mbuf *, size_t,
92 struct secasvar *, const struct esp_algorithm *, int));
93 static int esp_null_encrypt __P((struct mbuf *, size_t, size_t,
94 struct secasvar *, const struct esp_algorithm *, int));
95 static int esp_descbc_mature __P((struct secasvar *));
96 static int esp_descbc_ivlen __P((const struct esp_algorithm *,
98 static int esp_des_schedule __P((const struct esp_algorithm *,
100 static size_t esp_des_schedlen __P((const struct esp_algorithm *));
101 static int esp_des_blockdecrypt __P((const struct esp_algorithm *,
102 struct secasvar *, u_int8_t *, u_int8_t *));
103 static int esp_des_blockencrypt __P((const struct esp_algorithm *,
104 struct secasvar *, u_int8_t *, u_int8_t *));
105 static int esp_cbc_mature __P((struct secasvar *));
106 static int esp_blowfish_schedule __P((const struct esp_algorithm *,
108 static size_t esp_blowfish_schedlen __P((const struct esp_algorithm *));
109 static int esp_blowfish_blockdecrypt __P((const struct esp_algorithm *,
110 struct secasvar *, u_int8_t *, u_int8_t *));
111 static int esp_blowfish_blockencrypt __P((const struct esp_algorithm *,
112 struct secasvar *, u_int8_t *, u_int8_t *));
113 static int esp_cast128_schedule __P((const struct esp_algorithm *,
115 static size_t esp_cast128_schedlen __P((const struct esp_algorithm *));
116 static int esp_cast128_blockdecrypt __P((const struct esp_algorithm *,
117 struct secasvar *, u_int8_t *, u_int8_t *));
118 static int esp_cast128_blockencrypt __P((const struct esp_algorithm *,
119 struct secasvar *, u_int8_t *, u_int8_t *));
120 static int esp_3des_schedule __P((const struct esp_algorithm *,
122 static size_t esp_3des_schedlen __P((const struct esp_algorithm *));
123 static int esp_3des_blockdecrypt __P((const struct esp_algorithm *,
124 struct secasvar *, u_int8_t *, u_int8_t *));
125 static int esp_3des_blockencrypt __P((const struct esp_algorithm *,
126 struct secasvar *, u_int8_t *, u_int8_t *));
127 static int esp_common_ivlen __P((const struct esp_algorithm *,
129 static int esp_cbc_decrypt __P((struct mbuf *, size_t,
130 struct secasvar *, const struct esp_algorithm *, int));
131 static int esp_cbc_encrypt __P((struct mbuf *, size_t, size_t,
132 struct secasvar *, const struct esp_algorithm *, int));
136 static const struct esp_algorithm esp_algorithms[] = {
137 { 8, -1, esp_descbc_mature, 64, 64, esp_des_schedlen,
139 esp_descbc_ivlen, esp_cbc_decrypt,
140 esp_cbc_encrypt, esp_des_schedule,
141 esp_des_blockdecrypt, esp_des_blockencrypt, },
142 { 8, 8, esp_cbc_mature, 192, 192, esp_3des_schedlen,
144 esp_common_ivlen, esp_cbc_decrypt,
145 esp_cbc_encrypt, esp_3des_schedule,
146 esp_3des_blockdecrypt, esp_3des_blockencrypt, },
147 { 1, 0, esp_null_mature, 0, 2048, NULL, "null",
148 esp_common_ivlen, esp_null_decrypt,
149 esp_null_encrypt, NULL, },
150 { 8, 8, esp_cbc_mature, 40, 448, esp_blowfish_schedlen, "blowfish-cbc",
151 esp_common_ivlen, esp_cbc_decrypt,
152 esp_cbc_encrypt, esp_blowfish_schedule,
153 esp_blowfish_blockdecrypt, esp_blowfish_blockencrypt, },
154 { 8, 8, esp_cbc_mature, 40, 128, esp_cast128_schedlen,
156 esp_common_ivlen, esp_cbc_decrypt,
157 esp_cbc_encrypt, esp_cast128_schedule,
158 esp_cast128_blockdecrypt, esp_cast128_blockencrypt, },
159 { 16, 16, esp_cbc_mature, 128, 256, esp_rijndael_schedlen,
161 esp_common_ivlen, esp_cbc_decrypt,
162 esp_cbc_encrypt, esp_rijndael_schedule,
163 esp_rijndael_blockdecrypt, esp_rijndael_blockencrypt },
164 { 16, 8, esp_aesctr_mature, 160, 288, esp_aesctr_schedlen, "aes-ctr",
165 esp_common_ivlen, esp_aesctr_decrypt,
166 esp_aesctr_encrypt, esp_aesctr_schedule },
169 const struct esp_algorithm *
170 esp_algorithm_lookup(idx)
175 case SADB_EALG_DESCBC:
176 return &esp_algorithms[0];
177 case SADB_EALG_3DESCBC:
178 return &esp_algorithms[1];
180 return &esp_algorithms[2];
181 case SADB_X_EALG_BLOWFISHCBC:
182 return &esp_algorithms[3];
183 case SADB_X_EALG_CAST128CBC:
184 return &esp_algorithms[4];
185 case SADB_X_EALG_RIJNDAELCBC:
186 return &esp_algorithms[5];
187 case SADB_X_EALG_AESCTR:
188 return &esp_algorithms[6];
201 for (idx = 0; idx < sizeof(esp_algorithms)/sizeof(esp_algorithms[0]);
203 if (esp_algorithms[idx].ivlenval > ivlen)
204 ivlen = esp_algorithms[idx].ivlenval;
210 esp_schedule(algo, sav)
211 const struct esp_algorithm *algo;
212 struct secasvar *sav;
216 /* check for key length */
217 if (_KEYBITS(sav->key_enc) < algo->keymin ||
218 _KEYBITS(sav->key_enc) > algo->keymax) {
220 "esp_schedule %s: unsupported key length %d: "
221 "needs %d to %d bits\n", algo->name, _KEYBITS(sav->key_enc),
222 algo->keymin, algo->keymax));
226 /* already allocated */
227 if (sav->sched && sav->schedlen != 0)
229 /* no schedule necessary */
230 if (!algo->schedule || !algo->schedlen)
233 sav->schedlen = (*algo->schedlen)(algo);
234 sav->sched = malloc(sav->schedlen, M_SECA, M_NOWAIT);
240 error = (*algo->schedule)(algo, sav);
242 ipseclog((LOG_ERR, "esp_schedule %s: error %d\n",
244 bzero(sav->sched, sav->schedlen);
245 free(sav->sched, M_SECA);
254 struct secasvar *sav;
257 /* anything is okay */
262 esp_null_decrypt(m, off, sav, algo, ivlen)
264 size_t off; /* offset to ESP header */
265 struct secasvar *sav;
266 const struct esp_algorithm *algo;
270 return 0; /* do nothing */
274 esp_null_encrypt(m, off, plen, sav, algo, ivlen)
276 size_t off; /* offset to ESP header */
277 size_t plen; /* payload length (to be encrypted) */
278 struct secasvar *sav;
279 const struct esp_algorithm *algo;
283 return 0; /* do nothing */
287 esp_descbc_mature(sav)
288 struct secasvar *sav;
290 const struct esp_algorithm *algo;
292 if (!(sav->flags & SADB_X_EXT_OLD) && (sav->flags & SADB_X_EXT_IV4B)) {
293 ipseclog((LOG_ERR, "esp_cbc_mature: "
294 "algorithm incompatible with 4 octets IV length\n"));
299 ipseclog((LOG_ERR, "esp_descbc_mature: no key is given.\n"));
303 algo = esp_algorithm_lookup(sav->alg_enc);
306 "esp_descbc_mature: unsupported algorithm.\n"));
310 if (_KEYBITS(sav->key_enc) < algo->keymin ||
311 _KEYBITS(sav->key_enc) > algo->keymax) {
313 "esp_descbc_mature: invalid key length %d.\n",
314 _KEYBITS(sav->key_enc)));
319 if (des_is_weak_key((des_cblock *)_KEYBUF(sav->key_enc))) {
321 "esp_descbc_mature: weak key was passed.\n"));
329 esp_descbc_ivlen(algo, sav)
330 const struct esp_algorithm *algo;
331 struct secasvar *sav;
336 if ((sav->flags & SADB_X_EXT_OLD) && (sav->flags & SADB_X_EXT_IV4B))
338 if (!(sav->flags & SADB_X_EXT_OLD) && (sav->flags & SADB_X_EXT_DERIV))
344 esp_des_schedlen(algo)
345 const struct esp_algorithm *algo;
348 return sizeof(des_key_schedule);
352 esp_des_schedule(algo, sav)
353 const struct esp_algorithm *algo;
354 struct secasvar *sav;
357 if (des_key_sched((des_cblock *)_KEYBUF(sav->key_enc),
358 *(des_key_schedule *)sav->sched))
365 esp_des_blockdecrypt(algo, sav, s, d)
366 const struct esp_algorithm *algo;
367 struct secasvar *sav;
372 /* assumption: d has a good alignment */
373 bcopy(s, d, sizeof(DES_LONG) * 2);
374 des_ecb_encrypt((des_cblock *)d, (des_cblock *)d,
375 *(des_key_schedule *)sav->sched, DES_DECRYPT);
380 esp_des_blockencrypt(algo, sav, s, d)
381 const struct esp_algorithm *algo;
382 struct secasvar *sav;
387 /* assumption: d has a good alignment */
388 bcopy(s, d, sizeof(DES_LONG) * 2);
389 des_ecb_encrypt((des_cblock *)d, (des_cblock *)d,
390 *(des_key_schedule *)sav->sched, DES_ENCRYPT);
396 struct secasvar *sav;
399 const struct esp_algorithm *algo;
401 if (sav->flags & SADB_X_EXT_OLD) {
403 "esp_cbc_mature: algorithm incompatible with esp-old\n"));
406 if (sav->flags & SADB_X_EXT_DERIV) {
408 "esp_cbc_mature: algorithm incompatible with derived\n"));
413 ipseclog((LOG_ERR, "esp_cbc_mature: no key is given.\n"));
417 algo = esp_algorithm_lookup(sav->alg_enc);
420 "esp_cbc_mature %s: unsupported algorithm.\n", algo->name));
424 keylen = sav->key_enc->sadb_key_bits;
425 if (keylen < algo->keymin || algo->keymax < keylen) {
427 "esp_cbc_mature %s: invalid key length %d.\n",
428 algo->name, sav->key_enc->sadb_key_bits));
431 switch (sav->alg_enc) {
432 case SADB_EALG_3DESCBC:
434 if (des_is_weak_key((des_cblock *)_KEYBUF(sav->key_enc)) ||
435 des_is_weak_key((des_cblock *)(_KEYBUF(sav->key_enc) + 8)) ||
436 des_is_weak_key((des_cblock *)(_KEYBUF(sav->key_enc) + 16))) {
438 "esp_cbc_mature %s: weak key was passed.\n",
443 case SADB_X_EALG_BLOWFISHCBC:
444 case SADB_X_EALG_CAST128CBC:
446 case SADB_X_EALG_RIJNDAELCBC:
447 /* allows specific key sizes only */
448 if (!(keylen == 128 || keylen == 192 || keylen == 256)) {
450 "esp_cbc_mature %s: invalid key length %d.\n",
451 algo->name, keylen));
461 esp_blowfish_schedlen(algo)
462 const struct esp_algorithm *algo;
465 return sizeof(BF_KEY);
469 esp_blowfish_schedule(algo, sav)
470 const struct esp_algorithm *algo;
471 struct secasvar *sav;
474 BF_set_key((BF_KEY *)sav->sched, _KEYLEN(sav->key_enc),
475 _KEYBUF(sav->key_enc));
480 esp_blowfish_blockdecrypt(algo, sav, s, d)
481 const struct esp_algorithm *algo;
482 struct secasvar *sav;
487 BF_ecb_encrypt(s, d, (BF_KEY *)sav->sched, 0);
492 esp_blowfish_blockencrypt(algo, sav, s, d)
493 const struct esp_algorithm *algo;
494 struct secasvar *sav;
499 BF_ecb_encrypt(s, d, (BF_KEY *)sav->sched, 1);
504 esp_cast128_schedlen(algo)
505 const struct esp_algorithm *algo;
508 return sizeof(cast128_key);
512 esp_cast128_schedule(algo, sav)
513 const struct esp_algorithm *algo;
514 struct secasvar *sav;
517 cast128_setkey((cast128_key *)sav->sched, _KEYBUF(sav->key_enc),
518 _KEYLEN(sav->key_enc));
523 esp_cast128_blockdecrypt(algo, sav, s, d)
524 const struct esp_algorithm *algo;
525 struct secasvar *sav;
530 cast128_decrypt((cast128_key *)sav->sched, s, d);
535 esp_cast128_blockencrypt(algo, sav, s, d)
536 const struct esp_algorithm *algo;
537 struct secasvar *sav;
542 cast128_encrypt((cast128_key *)sav->sched, s, d);
547 esp_3des_schedlen(algo)
548 const struct esp_algorithm *algo;
551 return sizeof(des_key_schedule) * 3;
555 esp_3des_schedule(algo, sav)
556 const struct esp_algorithm *algo;
557 struct secasvar *sav;
564 p = (des_key_schedule *)sav->sched;
565 k = _KEYBUF(sav->key_enc);
566 for (i = 0; i < 3; i++) {
567 error = des_key_sched((des_cblock *)(k + 8 * i), p[i]);
575 esp_3des_blockdecrypt(algo, sav, s, d)
576 const struct esp_algorithm *algo;
577 struct secasvar *sav;
583 /* assumption: d has a good alignment */
584 p = (des_key_schedule *)sav->sched;
585 bcopy(s, d, sizeof(DES_LONG) * 2);
586 des_ecb3_encrypt((des_cblock *)d, (des_cblock *)d,
587 p[0], p[1], p[2], DES_DECRYPT);
592 esp_3des_blockencrypt(algo, sav, s, d)
593 const struct esp_algorithm *algo;
594 struct secasvar *sav;
600 /* assumption: d has a good alignment */
601 p = (des_key_schedule *)sav->sched;
602 bcopy(s, d, sizeof(DES_LONG) * 2);
603 des_ecb3_encrypt((des_cblock *)d, (des_cblock *)d,
604 p[0], p[1], p[2], DES_ENCRYPT);
609 esp_common_ivlen(algo, sav)
610 const struct esp_algorithm *algo;
611 struct secasvar *sav;
615 panic("esp_common_ivlen: unknown algorithm");
616 return algo->ivlenval;
620 esp_cbc_decrypt(m, off, sav, algo, ivlen)
623 struct secasvar *sav;
624 const struct esp_algorithm *algo;
628 struct mbuf *d, *d0, *dp;
629 int soff, doff; /* offset from the head of chain, to head of this mbuf */
630 int sn, dn; /* offset from the head of the mbuf, to meat */
631 size_t ivoff, bodyoff;
632 u_int8_t iv[MAXIVLEN], *ivp;
633 u_int8_t sbuf[MAXIVLEN], *sp;
641 if (ivlen != sav->ivlen || ivlen > sizeof(iv)) {
642 ipseclog((LOG_ERR, "esp_cbc_decrypt %s: "
643 "unsupported ivlen %d\n", algo->name, ivlen));
648 /* assumes blocklen == padbound */
649 blocklen = algo->padbound;
652 if (blocklen > sizeof(iv)) {
653 ipseclog((LOG_ERR, "esp_cbc_decrypt %s: "
654 "unsupported blocklen %d\n", algo->name, blocklen));
660 if (sav->flags & SADB_X_EXT_OLD) {
662 ivoff = off + sizeof(struct esp);
663 bodyoff = off + sizeof(struct esp) + ivlen;
667 if (sav->flags & SADB_X_EXT_DERIV) {
669 * draft-ietf-ipsec-ciph-des-derived-00.txt
670 * uses sequence number field as IV field.
672 ivoff = off + sizeof(struct esp);
673 bodyoff = off + sizeof(struct esp) + sizeof(u_int32_t);
674 ivlen = sizeof(u_int32_t);
677 ivoff = off + sizeof(struct newesp);
678 bodyoff = off + sizeof(struct newesp) + ivlen;
684 m_copydata(m, ivoff, ivlen, (caddr_t)iv);
687 if (ivlen == blocklen)
689 else if (ivlen == 4 && blocklen == 8) {
690 bcopy(&iv[0], &iv[4], 4);
696 ipseclog((LOG_ERR, "esp_cbc_encrypt %s: "
697 "unsupported ivlen/blocklen: %d %d\n",
698 algo->name, ivlen, blocklen));
703 if (m->m_pkthdr.len < bodyoff) {
704 ipseclog((LOG_ERR, "esp_cbc_decrypt %s: bad len %d/%lu\n",
705 algo->name, m->m_pkthdr.len, (unsigned long)bodyoff));
709 if ((m->m_pkthdr.len - bodyoff) % blocklen) {
710 ipseclog((LOG_ERR, "esp_cbc_decrypt %s: "
711 "payload length must be multiple of %d\n",
712 algo->name, blocklen));
719 soff = doff = sn = dn = 0;
723 while (soff < bodyoff) {
724 if (soff + s->m_len >= bodyoff) {
735 /* skip over empty mbuf */
736 while (s && s->m_len == 0)
739 while (soff < m->m_pkthdr.len) {
741 if (sn + blocklen <= s->m_len) {
742 /* body is continuous */
743 sp = mtod(s, u_int8_t *) + sn;
745 /* body is non-continuous */
746 m_copydata(s, sn, blocklen, sbuf);
751 if (!d || dn + blocklen > d->m_len) {
754 MGET(d, M_DONTWAIT, MT_DATA);
755 i = m->m_pkthdr.len - (soff + sn);
757 MCLGET(d, M_DONTWAIT);
758 if ((d->m_flags & M_EXT) == 0) {
774 d->m_len = (M_TRAILINGSPACE(d) / blocklen) * blocklen;
781 (*algo->blockdecrypt)(algo, sav, sp, mtod(d, u_int8_t *) + dn);
785 q = mtod(d, u_int8_t *) + dn;
786 for (i = 0; i < blocklen; i++)
791 bcopy(sbuf, iv, blocklen);
799 /* find the next source block */
800 while (s && sn >= s->m_len) {
806 /* skip over empty mbuf */
807 while (s && s->m_len == 0)
811 m_freem(scut->m_next);
812 scut->m_len = scutoff;
816 bzero(iv, sizeof(iv));
817 bzero(sbuf, sizeof(sbuf));
823 esp_cbc_encrypt(m, off, plen, sav, algo, ivlen)
827 struct secasvar *sav;
828 const struct esp_algorithm *algo;
832 struct mbuf *d, *d0, *dp;
833 int soff, doff; /* offset from the head of chain, to head of this mbuf */
834 int sn, dn; /* offset from the head of the mbuf, to meat */
835 size_t ivoff, bodyoff;
836 u_int8_t iv[MAXIVLEN], *ivp;
837 u_int8_t sbuf[MAXIVLEN], *sp;
845 if (ivlen != sav->ivlen || ivlen > sizeof(iv)) {
846 ipseclog((LOG_ERR, "esp_cbc_encrypt %s: "
847 "unsupported ivlen %d\n", algo->name, ivlen));
852 /* assumes blocklen == padbound */
853 blocklen = algo->padbound;
856 if (blocklen > sizeof(iv)) {
857 ipseclog((LOG_ERR, "esp_cbc_encrypt %s: "
858 "unsupported blocklen %d\n", algo->name, blocklen));
864 if (sav->flags & SADB_X_EXT_OLD) {
866 ivoff = off + sizeof(struct esp);
867 bodyoff = off + sizeof(struct esp) + ivlen;
871 if (sav->flags & SADB_X_EXT_DERIV) {
873 * draft-ietf-ipsec-ciph-des-derived-00.txt
874 * uses sequence number field as IV field.
876 ivoff = off + sizeof(struct esp);
877 bodyoff = off + sizeof(struct esp) + sizeof(u_int32_t);
878 ivlen = sizeof(u_int32_t);
881 ivoff = off + sizeof(struct newesp);
882 bodyoff = off + sizeof(struct newesp) + ivlen;
887 /* put iv into the packet. if we are in derived mode, use seqno. */
889 m_copydata(m, ivoff, ivlen, (caddr_t)iv);
891 bcopy(sav->iv, iv, ivlen);
892 /* maybe it is better to overwrite dest, not source */
893 m_copyback(m, ivoff, ivlen, (caddr_t)iv);
897 if (ivlen == blocklen)
899 else if (ivlen == 4 && blocklen == 8) {
900 bcopy(&iv[0], &iv[4], 4);
906 ipseclog((LOG_ERR, "esp_cbc_encrypt %s: "
907 "unsupported ivlen/blocklen: %d %d\n",
908 algo->name, ivlen, blocklen));
913 if (m->m_pkthdr.len < bodyoff) {
914 ipseclog((LOG_ERR, "esp_cbc_encrypt %s: bad len %d/%lu\n",
915 algo->name, m->m_pkthdr.len, (unsigned long)bodyoff));
919 if ((m->m_pkthdr.len - bodyoff) % blocklen) {
920 ipseclog((LOG_ERR, "esp_cbc_encrypt %s: "
921 "payload length must be multiple of %lu\n",
922 algo->name, (unsigned long)algo->padbound));
929 soff = doff = sn = dn = 0;
933 while (soff < bodyoff) {
934 if (soff + s->m_len >= bodyoff) {
945 /* skip over empty mbuf */
946 while (s && s->m_len == 0)
949 while (soff < m->m_pkthdr.len) {
951 if (sn + blocklen <= s->m_len) {
952 /* body is continuous */
953 sp = mtod(s, u_int8_t *) + sn;
955 /* body is non-continuous */
956 m_copydata(s, sn, blocklen, (caddr_t)sbuf);
961 if (!d || dn + blocklen > d->m_len) {
964 MGET(d, M_DONTWAIT, MT_DATA);
965 i = m->m_pkthdr.len - (soff + sn);
967 MCLGET(d, M_DONTWAIT);
968 if ((d->m_flags & M_EXT) == 0) {
984 d->m_len = (M_TRAILINGSPACE(d) / blocklen) * blocklen;
993 for (i = 0; i < blocklen; i++)
997 (*algo->blockencrypt)(algo, sav, sp, mtod(d, u_int8_t *) + dn);
1000 ivp = mtod(d, u_int8_t *) + dn;
1005 /* find the next source block */
1006 while (s && sn >= s->m_len) {
1012 /* skip over empty mbuf */
1013 while (s && s->m_len == 0)
1017 m_freem(scut->m_next);
1018 scut->m_len = scutoff;
1022 bzero(iv, sizeof(iv));
1023 bzero(sbuf, sizeof(sbuf));
1025 key_sa_stir_iv(sav);
1030 /*------------------------------------------------------------*/
1032 /* does not free m0 on error */
1034 esp_auth(m0, skip, length, sav, sum)
1036 size_t skip; /* offset to ESP header */
1037 size_t length; /* payload length */
1038 struct secasvar *sav;
1043 struct ah_algorithm_state s;
1044 u_char sumbuf[AH_MAXSUMSIZE];
1045 const struct ah_algorithm *algo;
1050 if (m0->m_pkthdr.len < skip) {
1051 ipseclog((LOG_DEBUG, "esp_auth: mbuf length < skip\n"));
1054 if (m0->m_pkthdr.len < skip + length) {
1055 ipseclog((LOG_DEBUG,
1056 "esp_auth: mbuf length < skip + length\n"));
1060 * length of esp part (excluding authentication data) must be 4n,
1061 * since nexthdr must be at offset 4n+3.
1064 ipseclog((LOG_ERR, "esp_auth: length is not multiple of 4\n"));
1068 ipseclog((LOG_DEBUG, "esp_auth: NULL SA passed\n"));
1071 algo = ah_algorithm_lookup(sav->alg_auth);
1074 "esp_auth: bad ESP auth algorithm passed: %d\n",
1082 siz = (((*algo->sumsiz)(sav) + 3) & ~(4 - 1));
1083 if (sizeof(sumbuf) < siz) {
1084 ipseclog((LOG_DEBUG,
1085 "esp_auth: AH_MAXSUMSIZE is too small: siz=%lu\n",
1090 /* skip the header */
1093 panic("mbuf chain?");
1094 if (m->m_len <= skip) {
1104 error = (*algo->init)(&s, sav);
1108 while (0 < length) {
1110 panic("mbuf chain?");
1112 if (m->m_len - off < length) {
1113 (*algo->update)(&s, mtod(m, u_char *) + off,
1115 length -= m->m_len - off;
1119 (*algo->update)(&s, mtod(m, u_char *) + off, length);
1123 (*algo->result)(&s, sumbuf, sizeof(sumbuf));
1124 bcopy(sumbuf, sum, siz); /* XXX */