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>
78 #include <crypto/cast128/cast128.h>
80 #include <net/net_osdep.h>
82 static int esp_null_mature __P((struct secasvar *));
83 static int esp_null_decrypt __P((struct mbuf *, size_t,
84 struct secasvar *, const struct esp_algorithm *, int));
85 static int esp_null_encrypt __P((struct mbuf *, size_t, size_t,
86 struct secasvar *, const struct esp_algorithm *, int));
87 static int esp_descbc_mature __P((struct secasvar *));
88 static int esp_descbc_ivlen __P((const struct esp_algorithm *,
90 static int esp_des_schedule __P((const struct esp_algorithm *,
92 static size_t esp_des_schedlen __P((const struct esp_algorithm *));
93 static int esp_des_blockdecrypt __P((const struct esp_algorithm *,
94 struct secasvar *, u_int8_t *, u_int8_t *));
95 static int esp_des_blockencrypt __P((const struct esp_algorithm *,
96 struct secasvar *, u_int8_t *, u_int8_t *));
97 static int esp_cbc_mature __P((struct secasvar *));
98 static int esp_blowfish_schedule __P((const struct esp_algorithm *,
100 static size_t esp_blowfish_schedlen __P((const struct esp_algorithm *));
101 static int esp_blowfish_blockdecrypt __P((const struct esp_algorithm *,
102 struct secasvar *, u_int8_t *, u_int8_t *));
103 static int esp_blowfish_blockencrypt __P((const struct esp_algorithm *,
104 struct secasvar *, u_int8_t *, u_int8_t *));
105 static int esp_cast128_schedule __P((const struct esp_algorithm *,
107 static size_t esp_cast128_schedlen __P((const struct esp_algorithm *));
108 static int esp_cast128_blockdecrypt __P((const struct esp_algorithm *,
109 struct secasvar *, u_int8_t *, u_int8_t *));
110 static int esp_cast128_blockencrypt __P((const struct esp_algorithm *,
111 struct secasvar *, u_int8_t *, u_int8_t *));
112 static int esp_3des_schedule __P((const struct esp_algorithm *,
114 static size_t esp_3des_schedlen __P((const struct esp_algorithm *));
115 static int esp_3des_blockdecrypt __P((const struct esp_algorithm *,
116 struct secasvar *, u_int8_t *, u_int8_t *));
117 static int esp_3des_blockencrypt __P((const struct esp_algorithm *,
118 struct secasvar *, u_int8_t *, u_int8_t *));
119 static int esp_common_ivlen __P((const struct esp_algorithm *,
121 static int esp_cbc_decrypt __P((struct mbuf *, size_t,
122 struct secasvar *, const struct esp_algorithm *, int));
123 static int esp_cbc_encrypt __P((struct mbuf *, size_t, size_t,
124 struct secasvar *, const struct esp_algorithm *, int));
128 static const struct esp_algorithm esp_algorithms[] = {
129 { 8, -1, esp_descbc_mature, 64, 64, esp_des_schedlen,
131 esp_descbc_ivlen, esp_cbc_decrypt,
132 esp_cbc_encrypt, esp_des_schedule,
133 esp_des_blockdecrypt, esp_des_blockencrypt, },
134 { 8, 8, esp_cbc_mature, 192, 192, esp_3des_schedlen,
136 esp_common_ivlen, esp_cbc_decrypt,
137 esp_cbc_encrypt, esp_3des_schedule,
138 esp_3des_blockdecrypt, esp_3des_blockencrypt, },
139 { 1, 0, esp_null_mature, 0, 2048, NULL, "null",
140 esp_common_ivlen, esp_null_decrypt,
141 esp_null_encrypt, NULL, },
142 { 8, 8, esp_cbc_mature, 40, 448, esp_blowfish_schedlen, "blowfish-cbc",
143 esp_common_ivlen, esp_cbc_decrypt,
144 esp_cbc_encrypt, esp_blowfish_schedule,
145 esp_blowfish_blockdecrypt, esp_blowfish_blockencrypt, },
146 { 8, 8, esp_cbc_mature, 40, 128, esp_cast128_schedlen,
148 esp_common_ivlen, esp_cbc_decrypt,
149 esp_cbc_encrypt, esp_cast128_schedule,
150 esp_cast128_blockdecrypt, esp_cast128_blockencrypt, },
151 { 16, 16, esp_cbc_mature, 128, 256, esp_rijndael_schedlen,
153 esp_common_ivlen, esp_cbc_decrypt,
154 esp_cbc_encrypt, esp_rijndael_schedule,
155 esp_rijndael_blockdecrypt, esp_rijndael_blockencrypt },
156 { 16, 8, esp_aesctr_mature, 160, 288, esp_aesctr_schedlen, "aes-ctr",
157 esp_common_ivlen, esp_aesctr_decrypt,
158 esp_aesctr_encrypt, esp_aesctr_schedule },
161 const struct esp_algorithm *
162 esp_algorithm_lookup(idx)
167 case SADB_EALG_DESCBC:
168 return &esp_algorithms[0];
169 case SADB_EALG_3DESCBC:
170 return &esp_algorithms[1];
172 return &esp_algorithms[2];
173 case SADB_X_EALG_BLOWFISHCBC:
174 return &esp_algorithms[3];
175 case SADB_X_EALG_CAST128CBC:
176 return &esp_algorithms[4];
177 case SADB_X_EALG_RIJNDAELCBC:
178 return &esp_algorithms[5];
179 case SADB_X_EALG_AESCTR:
180 return &esp_algorithms[6];
193 for (idx = 0; idx < sizeof(esp_algorithms)/sizeof(esp_algorithms[0]);
195 if (esp_algorithms[idx].ivlenval > ivlen)
196 ivlen = esp_algorithms[idx].ivlenval;
202 esp_schedule(algo, sav)
203 const struct esp_algorithm *algo;
204 struct secasvar *sav;
208 /* check for key length */
209 if (_KEYBITS(sav->key_enc) < algo->keymin ||
210 _KEYBITS(sav->key_enc) > algo->keymax) {
212 "esp_schedule %s: unsupported key length %d: "
213 "needs %d to %d bits\n", algo->name, _KEYBITS(sav->key_enc),
214 algo->keymin, algo->keymax));
218 /* already allocated */
219 if (sav->sched && sav->schedlen != 0)
221 /* no schedule necessary */
222 if (!algo->schedule || !algo->schedlen)
225 sav->schedlen = (*algo->schedlen)(algo);
226 sav->sched = malloc(sav->schedlen, M_SECA, M_NOWAIT);
232 error = (*algo->schedule)(algo, sav);
234 ipseclog((LOG_ERR, "esp_schedule %s: error %d\n",
236 bzero(sav->sched, sav->schedlen);
237 free(sav->sched, M_SECA);
246 struct secasvar *sav;
249 /* anything is okay */
254 esp_null_decrypt(m, off, sav, algo, ivlen)
256 size_t off; /* offset to ESP header */
257 struct secasvar *sav;
258 const struct esp_algorithm *algo;
262 return 0; /* do nothing */
266 esp_null_encrypt(m, off, plen, sav, algo, ivlen)
268 size_t off; /* offset to ESP header */
269 size_t plen; /* payload length (to be encrypted) */
270 struct secasvar *sav;
271 const struct esp_algorithm *algo;
275 return 0; /* do nothing */
279 esp_descbc_mature(sav)
280 struct secasvar *sav;
282 const struct esp_algorithm *algo;
284 if (!(sav->flags & SADB_X_EXT_OLD) && (sav->flags & SADB_X_EXT_IV4B)) {
285 ipseclog((LOG_ERR, "esp_cbc_mature: "
286 "algorithm incompatible with 4 octets IV length\n"));
291 ipseclog((LOG_ERR, "esp_descbc_mature: no key is given.\n"));
295 algo = esp_algorithm_lookup(sav->alg_enc);
298 "esp_descbc_mature: unsupported algorithm.\n"));
302 if (_KEYBITS(sav->key_enc) < algo->keymin ||
303 _KEYBITS(sav->key_enc) > algo->keymax) {
305 "esp_descbc_mature: invalid key length %d.\n",
306 _KEYBITS(sav->key_enc)));
311 if (des_is_weak_key((des_cblock *)_KEYBUF(sav->key_enc))) {
313 "esp_descbc_mature: weak key was passed.\n"));
321 esp_descbc_ivlen(algo, sav)
322 const struct esp_algorithm *algo;
323 struct secasvar *sav;
328 if ((sav->flags & SADB_X_EXT_OLD) && (sav->flags & SADB_X_EXT_IV4B))
330 if (!(sav->flags & SADB_X_EXT_OLD) && (sav->flags & SADB_X_EXT_DERIV))
336 esp_des_schedlen(algo)
337 const struct esp_algorithm *algo;
340 return sizeof(des_key_schedule);
344 esp_des_schedule(algo, sav)
345 const struct esp_algorithm *algo;
346 struct secasvar *sav;
349 if (des_key_sched((des_cblock *)_KEYBUF(sav->key_enc),
350 *(des_key_schedule *)sav->sched))
357 esp_des_blockdecrypt(algo, sav, s, d)
358 const struct esp_algorithm *algo;
359 struct secasvar *sav;
364 /* assumption: d has a good alignment */
365 bcopy(s, d, sizeof(DES_LONG) * 2);
366 des_ecb_encrypt((des_cblock *)d, (des_cblock *)d,
367 *(des_key_schedule *)sav->sched, DES_DECRYPT);
372 esp_des_blockencrypt(algo, sav, s, d)
373 const struct esp_algorithm *algo;
374 struct secasvar *sav;
379 /* assumption: d has a good alignment */
380 bcopy(s, d, sizeof(DES_LONG) * 2);
381 des_ecb_encrypt((des_cblock *)d, (des_cblock *)d,
382 *(des_key_schedule *)sav->sched, DES_ENCRYPT);
388 struct secasvar *sav;
391 const struct esp_algorithm *algo;
393 if (sav->flags & SADB_X_EXT_OLD) {
395 "esp_cbc_mature: algorithm incompatible with esp-old\n"));
398 if (sav->flags & SADB_X_EXT_DERIV) {
400 "esp_cbc_mature: algorithm incompatible with derived\n"));
405 ipseclog((LOG_ERR, "esp_cbc_mature: no key is given.\n"));
409 algo = esp_algorithm_lookup(sav->alg_enc);
412 "esp_cbc_mature %s: unsupported algorithm.\n", algo->name));
416 keylen = sav->key_enc->sadb_key_bits;
417 if (keylen < algo->keymin || algo->keymax < keylen) {
419 "esp_cbc_mature %s: invalid key length %d.\n",
420 algo->name, sav->key_enc->sadb_key_bits));
423 switch (sav->alg_enc) {
424 case SADB_EALG_3DESCBC:
426 if (des_is_weak_key((des_cblock *)_KEYBUF(sav->key_enc)) ||
427 des_is_weak_key((des_cblock *)(_KEYBUF(sav->key_enc) + 8)) ||
428 des_is_weak_key((des_cblock *)(_KEYBUF(sav->key_enc) + 16))) {
430 "esp_cbc_mature %s: weak key was passed.\n",
435 case SADB_X_EALG_BLOWFISHCBC:
436 case SADB_X_EALG_CAST128CBC:
438 case SADB_X_EALG_RIJNDAELCBC:
439 /* allows specific key sizes only */
440 if (!(keylen == 128 || keylen == 192 || keylen == 256)) {
442 "esp_cbc_mature %s: invalid key length %d.\n",
443 algo->name, keylen));
453 esp_blowfish_schedlen(algo)
454 const struct esp_algorithm *algo;
457 return sizeof(BF_KEY);
461 esp_blowfish_schedule(algo, sav)
462 const struct esp_algorithm *algo;
463 struct secasvar *sav;
466 BF_set_key((BF_KEY *)sav->sched, _KEYLEN(sav->key_enc),
467 _KEYBUF(sav->key_enc));
472 esp_blowfish_blockdecrypt(algo, sav, s, d)
473 const struct esp_algorithm *algo;
474 struct secasvar *sav;
479 BF_ecb_encrypt(s, d, (BF_KEY *)sav->sched, 0);
484 esp_blowfish_blockencrypt(algo, sav, s, d)
485 const struct esp_algorithm *algo;
486 struct secasvar *sav;
491 BF_ecb_encrypt(s, d, (BF_KEY *)sav->sched, 1);
496 esp_cast128_schedlen(algo)
497 const struct esp_algorithm *algo;
500 return sizeof(cast128_key);
504 esp_cast128_schedule(algo, sav)
505 const struct esp_algorithm *algo;
506 struct secasvar *sav;
509 cast128_setkey((cast128_key *)sav->sched, _KEYBUF(sav->key_enc),
510 _KEYLEN(sav->key_enc));
515 esp_cast128_blockdecrypt(algo, sav, s, d)
516 const struct esp_algorithm *algo;
517 struct secasvar *sav;
522 cast128_decrypt((cast128_key *)sav->sched, s, d);
527 esp_cast128_blockencrypt(algo, sav, s, d)
528 const struct esp_algorithm *algo;
529 struct secasvar *sav;
534 cast128_encrypt((cast128_key *)sav->sched, s, d);
539 esp_3des_schedlen(algo)
540 const struct esp_algorithm *algo;
543 return sizeof(des_key_schedule) * 3;
547 esp_3des_schedule(algo, sav)
548 const struct esp_algorithm *algo;
549 struct secasvar *sav;
556 p = (des_key_schedule *)sav->sched;
557 k = _KEYBUF(sav->key_enc);
558 for (i = 0; i < 3; i++) {
559 error = des_key_sched((des_cblock *)(k + 8 * i), p[i]);
567 esp_3des_blockdecrypt(algo, sav, s, d)
568 const struct esp_algorithm *algo;
569 struct secasvar *sav;
575 /* assumption: d has a good alignment */
576 p = (des_key_schedule *)sav->sched;
577 bcopy(s, d, sizeof(DES_LONG) * 2);
578 des_ecb3_encrypt((des_cblock *)d, (des_cblock *)d,
579 p[0], p[1], p[2], DES_DECRYPT);
584 esp_3des_blockencrypt(algo, sav, s, d)
585 const struct esp_algorithm *algo;
586 struct secasvar *sav;
592 /* assumption: d has a good alignment */
593 p = (des_key_schedule *)sav->sched;
594 bcopy(s, d, sizeof(DES_LONG) * 2);
595 des_ecb3_encrypt((des_cblock *)d, (des_cblock *)d,
596 p[0], p[1], p[2], DES_ENCRYPT);
601 esp_common_ivlen(algo, sav)
602 const struct esp_algorithm *algo;
603 struct secasvar *sav;
607 panic("esp_common_ivlen: unknown algorithm");
608 return algo->ivlenval;
612 esp_cbc_decrypt(m, off, sav, algo, ivlen)
615 struct secasvar *sav;
616 const struct esp_algorithm *algo;
620 struct mbuf *d, *d0, *dp;
621 int soff, doff; /* offset from the head of chain, to head of this mbuf */
622 int sn, dn; /* offset from the head of the mbuf, to meat */
623 size_t ivoff, bodyoff;
624 u_int8_t iv[MAXIVLEN], *ivp;
625 u_int8_t sbuf[MAXIVLEN], *sp;
633 if (ivlen != sav->ivlen || ivlen > sizeof(iv)) {
634 ipseclog((LOG_ERR, "esp_cbc_decrypt %s: "
635 "unsupported ivlen %d\n", algo->name, ivlen));
640 /* assumes blocklen == padbound */
641 blocklen = algo->padbound;
644 if (blocklen > sizeof(iv)) {
645 ipseclog((LOG_ERR, "esp_cbc_decrypt %s: "
646 "unsupported blocklen %d\n", algo->name, blocklen));
652 if (sav->flags & SADB_X_EXT_OLD) {
654 ivoff = off + sizeof(struct esp);
655 bodyoff = off + sizeof(struct esp) + ivlen;
659 if (sav->flags & SADB_X_EXT_DERIV) {
661 * draft-ietf-ipsec-ciph-des-derived-00.txt
662 * uses sequence number field as IV field.
664 ivoff = off + sizeof(struct esp);
665 bodyoff = off + sizeof(struct esp) + sizeof(u_int32_t);
666 ivlen = sizeof(u_int32_t);
669 ivoff = off + sizeof(struct newesp);
670 bodyoff = off + sizeof(struct newesp) + ivlen;
676 m_copydata(m, ivoff, ivlen, (caddr_t)iv);
679 if (ivlen == blocklen)
681 else if (ivlen == 4 && blocklen == 8) {
682 bcopy(&iv[0], &iv[4], 4);
688 ipseclog((LOG_ERR, "esp_cbc_encrypt %s: "
689 "unsupported ivlen/blocklen: %d %d\n",
690 algo->name, ivlen, blocklen));
695 if (m->m_pkthdr.len < bodyoff) {
696 ipseclog((LOG_ERR, "esp_cbc_decrypt %s: bad len %d/%lu\n",
697 algo->name, m->m_pkthdr.len, (unsigned long)bodyoff));
701 if ((m->m_pkthdr.len - bodyoff) % blocklen) {
702 ipseclog((LOG_ERR, "esp_cbc_decrypt %s: "
703 "payload length must be multiple of %d\n",
704 algo->name, blocklen));
711 soff = doff = sn = dn = 0;
715 while (soff < bodyoff) {
716 if (soff + s->m_len >= bodyoff) {
727 /* skip over empty mbuf */
728 while (s && s->m_len == 0)
731 while (soff < m->m_pkthdr.len) {
733 if (sn + blocklen <= s->m_len) {
734 /* body is continuous */
735 sp = mtod(s, u_int8_t *) + sn;
737 /* body is non-continuous */
738 m_copydata(s, sn, blocklen, sbuf);
743 if (!d || dn + blocklen > d->m_len) {
746 MGET(d, M_DONTWAIT, MT_DATA);
747 i = m->m_pkthdr.len - (soff + sn);
749 MCLGET(d, M_DONTWAIT);
750 if ((d->m_flags & M_EXT) == 0) {
766 d->m_len = (M_TRAILINGSPACE(d) / blocklen) * blocklen;
773 (*algo->blockdecrypt)(algo, sav, sp, mtod(d, u_int8_t *) + dn);
777 q = mtod(d, u_int8_t *) + dn;
778 for (i = 0; i < blocklen; i++)
783 bcopy(sbuf, iv, blocklen);
791 /* find the next source block */
792 while (s && sn >= s->m_len) {
798 /* skip over empty mbuf */
799 while (s && s->m_len == 0)
803 m_freem(scut->m_next);
804 scut->m_len = scutoff;
808 bzero(iv, sizeof(iv));
809 bzero(sbuf, sizeof(sbuf));
815 esp_cbc_encrypt(m, off, plen, sav, algo, ivlen)
819 struct secasvar *sav;
820 const struct esp_algorithm *algo;
824 struct mbuf *d, *d0, *dp;
825 int soff, doff; /* offset from the head of chain, to head of this mbuf */
826 int sn, dn; /* offset from the head of the mbuf, to meat */
827 size_t ivoff, bodyoff;
828 u_int8_t iv[MAXIVLEN], *ivp;
829 u_int8_t sbuf[MAXIVLEN], *sp;
837 if (ivlen != sav->ivlen || ivlen > sizeof(iv)) {
838 ipseclog((LOG_ERR, "esp_cbc_encrypt %s: "
839 "unsupported ivlen %d\n", algo->name, ivlen));
844 /* assumes blocklen == padbound */
845 blocklen = algo->padbound;
848 if (blocklen > sizeof(iv)) {
849 ipseclog((LOG_ERR, "esp_cbc_encrypt %s: "
850 "unsupported blocklen %d\n", algo->name, blocklen));
856 if (sav->flags & SADB_X_EXT_OLD) {
858 ivoff = off + sizeof(struct esp);
859 bodyoff = off + sizeof(struct esp) + ivlen;
863 if (sav->flags & SADB_X_EXT_DERIV) {
865 * draft-ietf-ipsec-ciph-des-derived-00.txt
866 * uses sequence number field as IV field.
868 ivoff = off + sizeof(struct esp);
869 bodyoff = off + sizeof(struct esp) + sizeof(u_int32_t);
870 ivlen = sizeof(u_int32_t);
873 ivoff = off + sizeof(struct newesp);
874 bodyoff = off + sizeof(struct newesp) + ivlen;
879 /* put iv into the packet. if we are in derived mode, use seqno. */
881 m_copydata(m, ivoff, ivlen, (caddr_t)iv);
883 bcopy(sav->iv, iv, ivlen);
884 /* maybe it is better to overwrite dest, not source */
885 m_copyback(m, ivoff, ivlen, (caddr_t)iv);
889 if (ivlen == blocklen)
891 else if (ivlen == 4 && blocklen == 8) {
892 bcopy(&iv[0], &iv[4], 4);
898 ipseclog((LOG_ERR, "esp_cbc_encrypt %s: "
899 "unsupported ivlen/blocklen: %d %d\n",
900 algo->name, ivlen, blocklen));
905 if (m->m_pkthdr.len < bodyoff) {
906 ipseclog((LOG_ERR, "esp_cbc_encrypt %s: bad len %d/%lu\n",
907 algo->name, m->m_pkthdr.len, (unsigned long)bodyoff));
911 if ((m->m_pkthdr.len - bodyoff) % blocklen) {
912 ipseclog((LOG_ERR, "esp_cbc_encrypt %s: "
913 "payload length must be multiple of %lu\n",
914 algo->name, (unsigned long)algo->padbound));
921 soff = doff = sn = dn = 0;
925 while (soff < bodyoff) {
926 if (soff + s->m_len >= bodyoff) {
937 /* skip over empty mbuf */
938 while (s && s->m_len == 0)
941 while (soff < m->m_pkthdr.len) {
943 if (sn + blocklen <= s->m_len) {
944 /* body is continuous */
945 sp = mtod(s, u_int8_t *) + sn;
947 /* body is non-continuous */
948 m_copydata(s, sn, blocklen, (caddr_t)sbuf);
953 if (!d || dn + blocklen > d->m_len) {
956 MGET(d, M_DONTWAIT, MT_DATA);
957 i = m->m_pkthdr.len - (soff + sn);
959 MCLGET(d, M_DONTWAIT);
960 if ((d->m_flags & M_EXT) == 0) {
976 d->m_len = (M_TRAILINGSPACE(d) / blocklen) * blocklen;
985 for (i = 0; i < blocklen; i++)
989 (*algo->blockencrypt)(algo, sav, sp, mtod(d, u_int8_t *) + dn);
992 ivp = mtod(d, u_int8_t *) + dn;
997 /* find the next source block */
998 while (s && sn >= s->m_len) {
1004 /* skip over empty mbuf */
1005 while (s && s->m_len == 0)
1009 m_freem(scut->m_next);
1010 scut->m_len = scutoff;
1014 bzero(iv, sizeof(iv));
1015 bzero(sbuf, sizeof(sbuf));
1017 key_sa_stir_iv(sav);
1022 /*------------------------------------------------------------*/
1024 /* does not free m0 on error */
1026 esp_auth(m0, skip, length, sav, sum)
1028 size_t skip; /* offset to ESP header */
1029 size_t length; /* payload length */
1030 struct secasvar *sav;
1035 struct ah_algorithm_state s;
1036 u_char sumbuf[AH_MAXSUMSIZE];
1037 const struct ah_algorithm *algo;
1042 if (m0->m_pkthdr.len < skip) {
1043 ipseclog((LOG_DEBUG, "esp_auth: mbuf length < skip\n"));
1046 if (m0->m_pkthdr.len < skip + length) {
1047 ipseclog((LOG_DEBUG,
1048 "esp_auth: mbuf length < skip + length\n"));
1052 * length of esp part (excluding authentication data) must be 4n,
1053 * since nexthdr must be at offset 4n+3.
1056 ipseclog((LOG_ERR, "esp_auth: length is not multiple of 4\n"));
1060 ipseclog((LOG_DEBUG, "esp_auth: NULL SA passed\n"));
1063 algo = ah_algorithm_lookup(sav->alg_auth);
1066 "esp_auth: bad ESP auth algorithm passed: %d\n",
1074 siz = (((*algo->sumsiz)(sav) + 3) & ~(4 - 1));
1075 if (sizeof(sumbuf) < siz) {
1076 ipseclog((LOG_DEBUG,
1077 "esp_auth: AH_MAXSUMSIZE is too small: siz=%lu\n",
1082 /* skip the header */
1085 panic("mbuf chain?");
1086 if (m->m_len <= skip) {
1096 error = (*algo->init)(&s, sav);
1100 while (0 < length) {
1102 panic("mbuf chain?");
1104 if (m->m_len - off < length) {
1105 (*algo->update)(&s, mtod(m, u_char *) + off,
1107 length -= m->m_len - off;
1111 (*algo->update)(&s, mtod(m, u_char *) + off, length);
1115 (*algo->result)(&s, sumbuf, sizeof(sumbuf));
1116 bcopy(sumbuf, sum, siz); /* XXX */