1 /* $KAME: esp_core.c,v 1.11 2000/02/22 14:04:15 itojun Exp $ */
4 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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 * 3. Neither the name of the project nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 #if (defined(__FreeBSD__) && __FreeBSD__ >= 3) || defined(__NetBSD__)
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/malloc.h>
41 #include <sys/domain.h>
42 #include <sys/protosw.h>
43 #include <sys/socket.h>
44 #include <sys/errno.h>
46 #include <sys/kernel.h>
47 #include <sys/syslog.h>
50 #include <net/route.h>
52 #include <netinet/in.h>
53 #include <netinet/in_var.h>
55 #include <netinet/ip6.h>
56 #include <netinet6/ip6_var.h>
57 #include <netinet/icmp6.h>
60 #include <netinet6/ipsec.h>
61 #include <netinet6/ah.h>
62 #include <netinet6/esp.h>
63 #include <net/pfkeyv2.h>
64 #include <netkey/keydb.h>
65 #include <crypto/des/des.h>
66 #include <crypto/blowfish/blowfish.h>
67 #include <crypto/cast128/cast128.h>
68 #include <crypto/rc5/rc5.h>
70 #include <net/net_osdep.h>
72 static int esp_null_mature
__P((struct secasvar
*));
73 static int esp_null_ivlen
__P((struct secasvar
*));
74 static int esp_null_decrypt
__P((struct mbuf
*, size_t,
75 struct secasvar
*, struct esp_algorithm
*, int));
76 static int esp_null_encrypt
__P((struct mbuf
*, size_t, size_t,
77 struct secasvar
*, struct esp_algorithm
*, int));
78 static int esp_descbc_mature
__P((struct secasvar
*));
79 static int esp_descbc_ivlen
__P((struct secasvar
*));
80 static int esp_descbc_decrypt
__P((struct mbuf
*, size_t,
81 struct secasvar
*, struct esp_algorithm
*, int));
82 static int esp_descbc_encrypt
__P((struct mbuf
*, size_t, size_t,
83 struct secasvar
*, struct esp_algorithm
*, int));
84 static int esp_cbc_mature
__P((struct secasvar
*));
85 static int esp_blowfish_cbc_decrypt
__P((struct mbuf
*, size_t,
86 struct secasvar
*, struct esp_algorithm
*, int));
87 static int esp_blowfish_cbc_encrypt
__P((struct mbuf
*, size_t,
88 size_t, struct secasvar
*, struct esp_algorithm
*, int));
89 static int esp_blowfish_cbc_ivlen
__P((struct secasvar
*));
90 static int esp_cast128cbc_ivlen
__P((struct secasvar
*));
91 static int esp_cast128cbc_decrypt
__P((struct mbuf
*, size_t,
92 struct secasvar
*, struct esp_algorithm
*, int));
93 static int esp_cast128cbc_encrypt
__P((struct mbuf
*, size_t, size_t,
94 struct secasvar
*, struct esp_algorithm
*, int));
95 static int esp_3descbc_ivlen
__P((struct secasvar
*));
96 static int esp_3descbc_decrypt
__P((struct mbuf
*, size_t,
97 struct secasvar
*, struct esp_algorithm
*, int));
98 static int esp_3descbc_encrypt
__P((struct mbuf
*, size_t, size_t,
99 struct secasvar
*, struct esp_algorithm
*, int));
100 static int esp_rc5cbc_ivlen
__P((struct secasvar
*));
101 static int esp_rc5cbc_decrypt
__P((struct mbuf
*, size_t,
102 struct secasvar
*, struct esp_algorithm
*, int));
103 static int esp_rc5cbc_encrypt
__P((struct mbuf
*, size_t, size_t,
104 struct secasvar
*, struct esp_algorithm
*, int));
105 static void esp_increment_iv
__P((struct secasvar
*));
106 static caddr_t mbuf_find_offset
__P((struct mbuf
*, size_t, size_t));
108 /* NOTE: The order depends on SADB_EALG_x in netkey/keyv2.h */
109 struct esp_algorithm esp_algorithms
[] = {
110 { 0, 0, 0, 0, 0, 0, 0, },
111 { 8, esp_descbc_mature
, 64, 64,
112 esp_descbc_ivlen
, esp_descbc_decrypt
, esp_descbc_encrypt
, },
113 { 8, esp_cbc_mature
, 192, 192,
114 esp_3descbc_ivlen
, esp_3descbc_decrypt
, esp_3descbc_encrypt
, },
115 { 1, esp_null_mature
, 0, 2048,
116 esp_null_ivlen
, esp_null_decrypt
, esp_null_encrypt
, },
117 { 8, esp_cbc_mature
, 40, 448,
118 esp_blowfish_cbc_ivlen
, esp_blowfish_cbc_decrypt
,
119 esp_blowfish_cbc_encrypt
, },
120 { 8, esp_cbc_mature
, 40, 128,
121 esp_cast128cbc_ivlen
, esp_cast128cbc_decrypt
,
122 esp_cast128cbc_encrypt
, },
123 { 8, esp_cbc_mature
, 40, 2040,
124 esp_rc5cbc_ivlen
, esp_rc5cbc_decrypt
, esp_rc5cbc_encrypt
, },
128 * mbuf assumption: foo_encrypt() assumes that IV part is placed in a single
129 * mbuf, not across multiple mbufs.
134 struct secasvar
*sav
;
136 /* anything is okay */
142 struct secasvar
*sav
;
148 esp_null_decrypt(m
, off
, sav
, algo
, ivlen
)
150 size_t off
; /* offset to ESP header */
151 struct secasvar
*sav
;
152 struct esp_algorithm
*algo
;
155 return 0; /* do nothing */
159 esp_null_encrypt(m
, off
, plen
, sav
, algo
, ivlen
)
161 size_t off
; /* offset to ESP header */
162 size_t plen
; /* payload length (to be encrypted) */
163 struct secasvar
*sav
;
164 struct esp_algorithm
*algo
;
167 return 0; /* do nothing */
171 esp_descbc_mature(sav
)
172 struct secasvar
*sav
;
174 struct esp_algorithm
*algo
;
176 if (!(sav
->flags
& SADB_X_EXT_OLD
) && (sav
->flags
& SADB_X_EXT_IV4B
)) {
177 ipseclog((LOG_ERR
, "esp_cbc_mature: "
178 "algorithm incompatible with 4 octets IV length\n"));
183 ipseclog((LOG_ERR
, "esp_descbc_mature: no key is given.\n"));
186 algo
= &esp_algorithms
[sav
->alg_enc
];
187 if (_KEYBITS(sav
->key_enc
) < algo
->keymin
188 || algo
->keymax
< _KEYBITS(sav
->key_enc
)) {
190 "esp_descbc_mature: invalid key length %d.\n",
191 _KEYBITS(sav
->key_enc
)));
196 if (des_is_weak_key((C_Block
*)_KEYBUF(sav
->key_enc
))) {
198 "esp_descbc_mature: weak key was passed.\n"));
206 esp_descbc_ivlen(sav
)
207 struct secasvar
*sav
;
209 if (sav
&& (sav
->flags
& SADB_X_EXT_OLD
) && (sav
->flags
& SADB_X_EXT_IV4B
))
212 if (sav
&& !(sav
->flags
& SADB_X_EXT_OLD
) && (sav
->flags
& SADB_X_EXT_DERIV
))
219 esp_descbc_decrypt(m
, off
, sav
, algo
, ivlen
)
221 size_t off
; /* offset to ESP header */
222 struct secasvar
*sav
;
223 struct esp_algorithm
*algo
;
235 if (ivlen
!= sav
->ivlen
) {
236 ipseclog((LOG_ERR
, "esp_descbc_decrypt: bad ivlen %d/%d\n",
240 if (_KEYBITS(sav
->key_enc
) < algo
->keymin
241 || algo
->keymax
< _KEYBITS(sav
->key_enc
)) {
242 ipseclog((LOG_ERR
, "esp_descbc_decrypt: bad keylen %d\n",
243 _KEYBITS(sav
->key_enc
)));
247 if (sav
->flags
& SADB_X_EXT_OLD
) {
249 ivoff
= off
+ sizeof(struct esp
);
250 bodyoff
= off
+ sizeof(struct esp
) + ivlen
;
254 if (sav
->flags
& SADB_X_EXT_DERIV
) {
256 * draft-ietf-ipsec-ciph-des-derived-00.txt
257 * uses sequence number field as IV field.
258 * This draft has been deleted, but you can get from
259 * ftp://ftp.kame.net/pub/internet-drafts/.
261 ivoff
= off
+ sizeof(struct esp
);
262 bodyoff
= off
+ sizeof(struct esp
) + sizeof(u_int32_t
);
263 ivlen
= sizeof(u_int32_t
);
266 ivoff
= off
+ sizeof(struct newesp
);
267 bodyoff
= off
+ sizeof(struct newesp
) + ivlen
;
273 m_copydata(m
, ivoff
, 4, &tiv
[0]);
274 m_copydata(m
, ivoff
, 4, &tiv
[4]);
279 } else if (ivlen
== 8) {
281 m_copydata(m
, ivoff
, 8, &tiv
[0]);
283 ipseclog((LOG_ERR
, "esp_descbc_decrypt: unsupported ivlen %d\n",
288 plen
= m
->m_pkthdr
.len
;
290 panic("esp_descbc_decrypt: too short packet: len=%lu",
295 ipseclog((LOG_ERR
, "esp_descbc_decrypt: "
296 "payload length must be multiple of 8\n"));
304 deserr
= des_key_sched((C_Block
*)_KEYBUF(sav
->key_enc
), ks
);
307 "esp_descbc_decrypt: key error %d\n", deserr
));
311 des_cbc_encrypt(m
, bodyoff
, plen
, ks
, (C_Block
*)iv
, DES_DECRYPT
);
314 bzero(&ks
, sizeof(des_key_schedule
));
318 bzero(&tiv
[0], sizeof(tiv
));
324 esp_descbc_encrypt(m
, off
, plen
, sav
, algo
, ivlen
)
326 size_t off
; /* offset to ESP header */
327 size_t plen
; /* payload length (to be decrypted) */
328 struct secasvar
*sav
;
329 struct esp_algorithm
*algo
;
342 ipseclog((LOG_ERR
, "esp_descbc_encrypt: "
343 "payload length must be multiple of 8\n"));
346 if (sav
->ivlen
!= ivlen
) {
347 ipseclog((LOG_ERR
, "esp_descbc_encrypt: bad ivlen %d/%d\n",
351 if (_KEYBITS(sav
->key_enc
) < algo
->keymin
352 || algo
->keymax
< _KEYBITS(sav
->key_enc
)) {
353 ipseclog((LOG_ERR
, "esp_descbc_encrypt: bad keylen %d\n",
354 _KEYBITS(sav
->key_enc
)));
358 if (sav
->flags
& SADB_X_EXT_OLD
) {
361 * draft-ietf-ipsec-ciph-des-derived-00.txt
362 * uses sequence number field as IV field.
363 * This draft has been deleted, see above.
365 ivoff
= off
+ sizeof(struct esp
);
366 bodyoff
= off
+ sizeof(struct esp
) + ivlen
;
370 if (sav
->flags
& SADB_X_EXT_DERIV
) {
372 * draft-ietf-ipsec-ciph-des-derived-00.txt
373 * uses sequence number field as IV field.
374 * This draft has been deleted, see above.
376 ivoff
= off
+ sizeof(struct esp
);
377 bodyoff
= off
+ sizeof(struct esp
) + sizeof(u_int32_t
);
378 ivlen
= sizeof(u_int32_t
);
381 ivoff
= off
+ sizeof(struct newesp
);
382 bodyoff
= off
+ sizeof(struct newesp
) + ivlen
;
387 if (m
->m_pkthdr
.len
< bodyoff
)
388 panic("assumption failed: mbuf too short");
389 iv
= mbuf_find_offset(m
, ivoff
, ivlen
);
391 panic("assumption failed: bad mbuf chain");
394 bcopy(sav
->iv
, &tiv
[0], 4);
395 bcopy(sav
->iv
, &tiv
[4], 4);
400 bcopy(&tiv
[0], iv
, 4);
403 bcopy(iv
, &tiv
[0], 4);
404 bcopy(iv
, &tiv
[4], 4);
411 } else if (ivlen
== 8)
412 bcopy((caddr_t
)sav
->iv
, (caddr_t
)iv
, ivlen
);
415 "esp_descbc_encrypt: unsupported ivlen %d\n", ivlen
));
423 deserr
= des_key_sched((C_Block
*)_KEYBUF(sav
->key_enc
), ks
);
426 "esp_descbc_encrypt: key error %d\n", deserr
));
430 des_cbc_encrypt(m
, bodyoff
, plen
, ks
, (C_Block
*)iv
, DES_ENCRYPT
);
433 bzero(&ks
, sizeof(des_key_schedule
));
436 esp_increment_iv(sav
);
439 bzero(&tiv
[0], sizeof(tiv
));
446 struct secasvar
*sav
;
449 struct esp_algorithm
*algo
;
451 if (sav
->flags
& SADB_X_EXT_OLD
) {
453 "esp_cbc_mature: algorithm incompatible with esp-old\n"));
456 if (sav
->flags
& SADB_X_EXT_DERIV
) {
458 "esp_cbc_mature: algorithm incompatible with derived\n"));
464 "esp_cbc_mature: no key is given.\n"));
467 algo
= &esp_algorithms
[sav
->alg_enc
];
468 keylen
= sav
->key_enc
->sadb_key_bits
;
469 if (keylen
< algo
->keymin
|| algo
->keymax
< keylen
) {
470 ipseclog((LOG_ERR
, "esp_cbc_mature: invalid key length %d.\n",
471 sav
->key_enc
->sadb_key_bits
));
474 switch (sav
->alg_enc
) {
475 case SADB_EALG_3DESCBC
:
477 if (des_is_weak_key((C_Block
*)_KEYBUF(sav
->key_enc
))
478 || des_is_weak_key((C_Block
*)(_KEYBUF(sav
->key_enc
) + 8))
479 || des_is_weak_key((C_Block
*)(_KEYBUF(sav
->key_enc
) + 16))) {
481 "esp_cbc_mature: weak key was passed.\n"));
485 case SADB_EALG_BLOWFISHCBC
:
486 case SADB_EALG_CAST128CBC
:
487 case SADB_EALG_RC5CBC
:
495 esp_blowfish_cbc_decrypt(m
, off
, sav
, algo
, ivlen
)
497 size_t off
; /* offset to ESP header */
498 struct secasvar
*sav
;
499 struct esp_algorithm
*algo
;
507 static BF_KEY key
; /* made static to avoid kernel stack overflow */
511 if (sav
->ivlen
!= ivlen
) {
513 "esp_blowfish_cbc_decrypt: bad ivlen %d/%d\n",
517 if (_KEYBITS(sav
->key_enc
) < algo
->keymin
518 || algo
->keymax
< _KEYBITS(sav
->key_enc
)) {
520 "esp_blowfish_cbc_decrypt: unsupported key length %d: "
521 "need %d to %d bits\n", _KEYBITS(sav
->key_enc
),
522 algo
->keymin
, algo
->keymax
));
525 if (sav
->flags
& SADB_X_EXT_OLD
) {
527 "esp_blowfish_cbc_decrypt: unsupported ESP version\n"));
532 "esp_blowfish_cbc_decrypt: unsupported ivlen %d\n", ivlen
));
536 ivoff
= off
+ sizeof(struct newesp
);
537 bodyoff
= off
+ sizeof(struct newesp
) + ivlen
;
539 m_copydata(m
, ivoff
, 8, &tiv
[0]);
541 plen
= m
->m_pkthdr
.len
;
543 panic("esp_blowfish_cbc_decrypt: too short packet: len=%lu",
548 ipseclog((LOG_ERR
, "esp_blowfish_cbc_decrypt: "
549 "payload length must be multiple of 8\n"));
554 s
= splsoftnet(); /* XXX correct? */
556 s
= splnet(); /* XXX correct? */
559 BF_set_key(&key
, _KEYBITS(sav
->key_enc
) / 8, _KEYBUF(sav
->key_enc
));
560 BF_cbc_encrypt_m(m
, bodyoff
, plen
, &key
, iv
, BF_DECRYPT
);
563 bzero(&key
, sizeof(BF_KEY
));
568 bzero(&tiv
[0], sizeof(tiv
));
574 esp_blowfish_cbc_encrypt(m
, off
, plen
, sav
, algo
, ivlen
)
576 size_t off
; /* offset to ESP header */
577 size_t plen
; /* payload length (to be decrypted) */
578 struct secasvar
*sav
;
579 struct esp_algorithm
*algo
;
585 static BF_KEY key
; /* made static to avoid kernel stack overflow */
590 ipseclog((LOG_ERR
, "esp_blowfish_cbc_encrypt: "
591 "payload length must be multiple of 8\n"));
594 if (sav
->ivlen
!= ivlen
) {
596 "esp_blowfish_cbc_encrypt: bad ivlen %d/%d\n",
600 if (_KEYBITS(sav
->key_enc
) < algo
->keymin
601 || algo
->keymax
< _KEYBITS(sav
->key_enc
)) {
603 "esp_blowfish_cbc_encrypt: unsupported key length %d: "
604 "need %d to %d bits\n", _KEYBITS(sav
->key_enc
),
605 algo
->keymin
, algo
->keymax
));
608 if (sav
->flags
& SADB_X_EXT_OLD
) {
610 "esp_blowfish_cbc_encrypt: unsupported ESP version\n"));
615 "esp_blowfish_cbc_encrypt: unsupported ivlen %d\n", ivlen
));
619 ivoff
= off
+ sizeof(struct newesp
);
620 bodyoff
= off
+ sizeof(struct newesp
) + ivlen
;
622 if (m
->m_pkthdr
.len
< bodyoff
)
623 panic("assumption failed: mbuf too short");
624 iv
= mbuf_find_offset(m
, ivoff
, ivlen
);
626 panic("assumption failed: bad mbuf chain");
628 bcopy((caddr_t
)sav
->iv
, (caddr_t
)iv
, ivlen
);
631 s
= splsoftnet(); /* XXX correct? */
633 s
= splnet(); /* XXX correct? */
636 BF_set_key(&key
, _KEYBITS(sav
->key_enc
) / 8, _KEYBUF(sav
->key_enc
));
637 BF_cbc_encrypt_m(m
, bodyoff
, plen
, &key
, iv
, BF_ENCRYPT
);
640 bzero(&key
, sizeof(BF_KEY
));
644 esp_increment_iv(sav
);
650 esp_blowfish_cbc_ivlen(sav
)
651 struct secasvar
*sav
;
657 esp_cast128cbc_ivlen(sav
)
658 struct secasvar
*sav
;
664 esp_cast128cbc_decrypt(m
, off
, sav
, algo
, ivlen
)
667 struct secasvar
*sav
;
668 struct esp_algorithm
*algo
;
677 if (ivlen
!= sav
->ivlen
) {
678 ipseclog((LOG_ERR
, "esp_cast128cbc_decrypt: bad ivlen %d/%d\n",
682 if (_KEYBITS(sav
->key_enc
) < algo
->keymin
683 || _KEYBITS(sav
->key_enc
) > algo
->keymax
) {
685 "esp_cast128cbc_decrypt: unsupported key length %d: "
686 "need %d to %d bits\n", _KEYBITS(sav
->key_enc
),
687 algo
->keymin
, algo
->keymax
));
690 if (sav
->flags
& SADB_X_EXT_OLD
) {
692 "esp_cast128cbc_decrypt: unsupported ESP version\n"));
697 "esp_cast128cbc_decrypt: unsupported ivlen %d\n", ivlen
));
701 ivoff
= off
+ sizeof(struct newesp
);
702 bodyoff
= off
+ sizeof(struct newesp
) + ivlen
;
704 /* copy mbuf's IV into iv */
705 m_copydata(m
, ivoff
, 8, iv
);
707 plen
= m
->m_pkthdr
.len
;
708 if (plen
< bodyoff
) {
709 panic("esp_cast128cbc_decrypt: too short packet: len=%lu\n",
715 ipseclog((LOG_ERR
, "esp_cast128cbc_decrypt: "
716 "payload length must be multiple of 8\n"));
723 u_int32_t subkey
[32];
725 bzero(key
, sizeof(key
));
726 bcopy(_KEYBUF(sav
->key_enc
), key
, _KEYLEN(sav
->key_enc
));
728 set_cast128_subkey(subkey
, key
);
729 cast128_cbc_process(m
, bodyoff
, plen
, subkey
, iv
,
730 _KEYBITS(sav
->key_enc
) / 8, CAST128_DECRYPT
);
733 bzero(subkey
, sizeof(subkey
));
734 bzero(key
, sizeof(key
));
741 esp_cast128cbc_encrypt(m
, off
, plen
, sav
, algo
, ivlen
)
745 struct secasvar
*sav
;
746 struct esp_algorithm
*algo
;
755 ipseclog((LOG_ERR
, "esp_cast128cbc_encrypt: "
756 "payload length must be multiple of 8\n"));
759 if (sav
->ivlen
!= ivlen
) {
760 ipseclog((LOG_ERR
, "esp_cast128cbc_encrypt: bad ivlen %d/%d\n",
764 if (_KEYBITS(sav
->key_enc
) < algo
->keymin
765 || _KEYBITS(sav
->key_enc
) > algo
->keymax
) {
767 "esp_cast128cbc_encrypt: unsupported key length %d: "
768 "needs %d to %d bits\n", _KEYBITS(sav
->key_enc
),
769 algo
->keymin
, algo
->keymax
));
772 if (sav
->flags
& SADB_X_EXT_OLD
) {
774 "esp_cast128cbc_encrypt: unsupported ESP version\n"));
779 "esp_cast128cbc_encrypt: unsupported ivlen %d\n", ivlen
));
783 ivoff
= off
+ sizeof(struct newesp
);
784 bodyoff
= off
+ sizeof(struct newesp
) + ivlen
;
786 if (m
->m_pkthdr
.len
< bodyoff
)
787 panic("assumption failed: mbuf too short");
788 iv
= mbuf_find_offset(m
, ivoff
, ivlen
);
790 panic("assumption failed: bad mbuf chain");
792 bcopy(sav
->iv
, iv
, ivlen
);
797 u_int32_t subkey
[32];
799 bzero(key
, sizeof(key
));
800 bcopy(_KEYBUF(sav
->key_enc
), key
, _KEYLEN(sav
->key_enc
));
802 set_cast128_subkey(subkey
, key
);
803 cast128_cbc_process(m
, bodyoff
, plen
, subkey
, iv
,
804 _KEYBITS(sav
->key_enc
) / 8, CAST128_ENCRYPT
);
807 bzero(subkey
, sizeof(subkey
));
808 bzero(key
, sizeof(key
));
811 esp_increment_iv(sav
);
817 esp_3descbc_ivlen(sav
)
818 struct secasvar
*sav
;
824 esp_3descbc_decrypt(m
, off
, sav
, algo
, ivlen
)
827 struct secasvar
*sav
;
828 struct esp_algorithm
*algo
;
838 if (ivlen
!= sav
->ivlen
) {
839 ipseclog((LOG_ERR
, "esp_3descbc_decrypt: bad ivlen %d/%d\n",
843 if (_KEYBITS(sav
->key_enc
) < algo
->keymin
844 || algo
->keymax
< _KEYBITS(sav
->key_enc
)) {
845 ipseclog((LOG_ERR
, "esp_3descbc_decrypt: bad keylen %d\n",
846 _KEYBITS(sav
->key_enc
)));
849 if (sav
->flags
& SADB_X_EXT_OLD
) {
851 "esp_3descbc_decrypt: unsupported ESP version\n"));
856 "esp_3descbc_decrypt: unsupported ivlen %d\n", ivlen
));
860 ivoff
= off
+ sizeof(struct newesp
);
861 bodyoff
= off
+ sizeof(struct newesp
) + ivlen
;
863 m_copydata(m
, ivoff
, 8, &tiv
[0]);
865 plen
= m
->m_pkthdr
.len
;
867 panic("esp_3descbc_decrypt: too short packet: len=%lu",
873 ipseclog((LOG_ERR
, "esp_3descbc_decrypt: "
874 "payload length must be multiple of 8\n"));
881 des_key_schedule ks
[3];
883 deserr
[0] = des_key_sched((C_Block
*)_KEYBUF(sav
->key_enc
),ks
[0]);
884 deserr
[1] = des_key_sched((C_Block
*)(_KEYBUF(sav
->key_enc
) + 8), ks
[1]);
885 deserr
[2] = des_key_sched((C_Block
*)(_KEYBUF(sav
->key_enc
) + 16), ks
[2]);
886 if ((deserr
[0] != 0) || (deserr
[1] != 0) || (deserr
[2] != 0)) {
887 ipseclog((LOG_ERR
, "esp_3descbc_decrypt: key error %d/%d/%d\n",
888 deserr
[0], deserr
[1], deserr
[2]));
892 des_3cbc_process(m
, bodyoff
, plen
, ks
, (C_Block
*)iv
, DES_DECRYPT
);
895 bzero(ks
[0], sizeof(des_key_schedule
)*3);
899 bzero(&tiv
[0], sizeof(tiv
));
905 esp_3descbc_encrypt(m
, off
, plen
, sav
, algo
, ivlen
)
909 struct secasvar
*sav
;
910 struct esp_algorithm
*algo
;
919 ipseclog((LOG_ERR
, "esp_3descbc_encrypt: "
920 "payload length must be multiple of 8\n"));
923 if (sav
->ivlen
!= ivlen
) {
924 ipseclog((LOG_ERR
, "esp_3descbc_encrypt: bad ivlen %d/%d\n",
928 if (_KEYBITS(sav
->key_enc
) < algo
->keymin
929 || algo
->keymax
< _KEYBITS(sav
->key_enc
)) {
930 ipseclog((LOG_ERR
, "esp_3descbc_encrypt: bad keylen %d\n",
931 _KEYBITS(sav
->key_enc
)));
934 if (sav
->flags
& SADB_X_EXT_OLD
) {
936 "esp_3descbc_encrypt: unsupported ESP version\n"));
941 "esp_3descbc_encrypt: unsupported ivlen %d\n", ivlen
));
945 ivoff
= off
+ sizeof(struct newesp
);
946 bodyoff
= off
+ sizeof(struct newesp
) + ivlen
;
948 if (m
->m_pkthdr
.len
< bodyoff
)
949 panic("assumption failed: mbuf too short");
950 iv
= mbuf_find_offset(m
, ivoff
, ivlen
);
952 panic("assumption failed: bad mbuf chain");
954 bcopy((caddr_t
)sav
->iv
, (caddr_t
)iv
, ivlen
);
959 des_key_schedule ks
[3];
961 deserr
[0] = des_key_sched((C_Block
*)_KEYBUF(sav
->key_enc
), ks
[0]);
962 deserr
[1] = des_key_sched((C_Block
*)(_KEYBUF(sav
->key_enc
) + 8), ks
[1]);
963 deserr
[2] = des_key_sched((C_Block
*)(_KEYBUF(sav
->key_enc
) + 16), ks
[2]);
964 if ((deserr
[0] != 0) || (deserr
[1] != 0) || (deserr
[2] != 0)) {
965 ipseclog((LOG_ERR
, "esp_3descbc_encrypt: key error %d/%d/%d\n",
966 deserr
[0], deserr
[1], deserr
[2]));
970 des_3cbc_process(m
, bodyoff
, plen
, ks
, (C_Block
*)iv
, DES_ENCRYPT
);
973 bzero(ks
[0], sizeof(des_key_schedule
)*3);
976 esp_increment_iv(sav
);
982 esp_rc5cbc_ivlen(sav
)
983 struct secasvar
*sav
;
989 esp_rc5cbc_decrypt(m
, off
, sav
, algo
, ivlen
)
992 struct secasvar
*sav
;
993 struct esp_algorithm
*algo
;
1002 if (sav
->ivlen
!= ivlen
) {
1003 ipseclog((LOG_ERR
, "esp_rc5cbc_decrypt: bad ivlen %d/%d\n",
1004 ivlen
, sav
->ivlen
));
1007 if ((_KEYBITS(sav
->key_enc
) < 40) || (_KEYBITS(sav
->key_enc
) > 2040)) {
1009 "esp_rc5cbc_decrypt: unsupported key length %d: "
1010 "need 40 to 2040 bit\n", _KEYBITS(sav
->key_enc
)));
1013 if (sav
->flags
& SADB_X_EXT_OLD
) {
1015 "esp_rc5cbc_decrypt: unsupported ESP version\n"));
1019 ipseclog((LOG_ERR
, "esp_rc5cbc_decrypt: unsupported ivlen %d\n",
1024 ivoff
= off
+ sizeof(struct newesp
);
1025 bodyoff
= off
+ sizeof(struct newesp
) + ivlen
;
1027 /* copy mbuf's IV into iv */
1028 m_copydata(m
, ivoff
, 8, iv
);
1030 plen
= m
->m_pkthdr
.len
;
1031 if (plen
< bodyoff
) {
1032 panic("esp_rc5cbc_decrypt: too short packet: len=%lu",
1038 ipseclog((LOG_ERR
, "esp_rc5cbc_decrypt: "
1039 "payload length must be multiple of 8\n"));
1047 set_rc5_expandkey(e_key
, _KEYBUF(sav
->key_enc
),
1048 _KEYBITS(sav
->key_enc
) / 8, 16);
1049 rc5_cbc_process(m
, bodyoff
, plen
, e_key
, iv
, RC5_DECRYPT
);
1052 bzero(e_key
, sizeof(e_key
));
1059 esp_rc5cbc_encrypt(m
, off
, plen
, sav
, algo
, ivlen
)
1063 struct secasvar
*sav
;
1064 struct esp_algorithm
*algo
;
1073 ipseclog((LOG_ERR
, "esp_rc5cbc_encrypt: "
1074 "payload length must be multiple of 8\n"));
1077 if (sav
->ivlen
!= ivlen
) {
1078 ipseclog((LOG_ERR
, "esp_rc5cbc_encrypt: bad ivlen %d/%d\n",
1079 ivlen
, sav
->ivlen
));
1082 if (_KEYBITS(sav
->key_enc
) < algo
->keymin
1083 || _KEYBITS(sav
->key_enc
) > algo
->keymax
) {
1085 "esp_rc5cbc_encrypt: unsupported key length %d: "
1086 "need %d to %d bits\n", _KEYBITS(sav
->key_enc
),
1087 algo
->keymin
, algo
->keymax
));
1090 if (sav
->flags
& SADB_X_EXT_OLD
) {
1092 "esp_rc5cbc_encrypt: unsupported ESP version\n"));
1096 ipseclog((LOG_ERR
, "esp_rc5cbc_encrypt: unsupported ivlen %d\n",
1101 ivoff
= off
+ sizeof(struct newesp
);
1102 bodyoff
= off
+ sizeof(struct newesp
) + ivlen
;
1104 if (m
->m_pkthdr
.len
< bodyoff
)
1105 panic("assumption failed: mbuf too short");
1106 iv
= mbuf_find_offset(m
, ivoff
, ivlen
);
1108 panic("assumption failed: bad mbuf chain");
1110 bcopy(sav
->iv
, iv
, ivlen
);
1116 set_rc5_expandkey(e_key
, _KEYBUF(sav
->key_enc
),
1117 _KEYBITS(sav
->key_enc
) / 8, 16);
1118 rc5_cbc_process(m
, bodyoff
, plen
, e_key
, iv
, RC5_ENCRYPT
);
1121 bzero(e_key
, sizeof(e_key
));
1124 esp_increment_iv(sav
);
1133 esp_increment_iv(sav
)
1134 struct secasvar
*sav
;
1140 #if !(defined(__FreeBSD__) && __FreeBSD__ >= 3)
1141 y
= time
.tv_sec
& 0xff;
1143 y
= time_second
& 0xff;
1146 x
= (u_int8_t
*)sav
->iv
;
1147 for (i
= 0; i
< sav
->ivlen
; i
++) {
1148 *x
= (*x
+ y
) & 0xff;
1154 mbuf_find_offset(m
, off
, len
)
1162 if (m
->m_pkthdr
.len
< off
|| m
->m_pkthdr
.len
< off
+ len
)
1163 return (caddr_t
)NULL
;
1165 for (n
= m
; n
; n
= n
->m_next
) {
1166 if (cnt
+ n
->m_len
<= off
) {
1170 if (cnt
<= off
&& off
< cnt
+ n
->m_len
1171 && cnt
<= off
+ len
&& off
+ len
<= cnt
+ n
->m_len
) {
1172 return mtod(n
, caddr_t
) + off
- cnt
;
1174 return (caddr_t
)NULL
;
1176 return (caddr_t
)NULL
;
1179 /*------------------------------------------------------------*/
1182 esp_auth(m0
, skip
, length
, sav
, sum
)
1184 size_t skip
; /* offset to ESP header */
1185 size_t length
; /* payload length */
1186 struct secasvar
*sav
;
1191 struct ah_algorithm_state s
;
1192 u_char sumbuf
[AH_MAXSUMSIZE
];
1193 struct ah_algorithm
*algo
;
1197 if (m0
->m_pkthdr
.len
< skip
) {
1198 ipseclog((LOG_DEBUG
, "esp_auth: mbuf length < skip\n"));
1201 if (m0
->m_pkthdr
.len
< skip
+ length
) {
1202 ipseclog((LOG_DEBUG
,
1203 "esp_auth: mbuf length < skip + length\n"));
1207 * length of esp part (excluding authentication data) must be 4n,
1208 * since nexthdr must be at offset 4n+3.
1211 ipseclog((LOG_ERR
, "esp_auth: length is not multiple of 4\n"));
1215 ipseclog((LOG_DEBUG
, "esp_auth: NULL SA passed\n"));
1218 if (!sav
->alg_auth
) {
1220 "esp_auth: bad ESP auth algorithm passed: %d\n",
1228 algo
= &ah_algorithms
[sav
->alg_auth
];
1229 siz
= (((*algo
->sumsiz
)(sav
) + 3) & ~(4 - 1));
1230 if (sizeof(sumbuf
) < siz
) {
1231 ipseclog((LOG_DEBUG
,
1232 "esp_auth: AH_MAXSUMSIZE is too small: siz=%lu\n",
1237 /* skip the header */
1240 panic("mbuf chain?");
1241 if (m
->m_len
<= skip
) {
1251 (*algo
->init
)(&s
, sav
);
1252 while (0 < length
) {
1254 panic("mbuf chain?");
1256 if (m
->m_len
- off
< length
) {
1257 (*algo
->update
)(&s
, mtod(m
, u_char
*) + off
,
1259 length
-= m
->m_len
- off
;
1263 (*algo
->update
)(&s
, mtod(m
, u_char
*) + off
, length
);
1267 (*algo
->result
)(&s
, sumbuf
);
1268 bcopy(sumbuf
, sum
, siz
); /*XXX*/