1 /* $Id: algorithm.c,v 1.11.4.1 2005/06/28 22:38:02 manubsd 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
34 #include <sys/param.h>
35 #include <sys/types.h>
44 #include "crypto_openssl.h"
46 #include "algorithm.h"
48 #include "isakmp_var.h"
50 #include "ipsec_doi.h"
53 static struct hash_algorithm oakley_hashdef
[] = {
54 { "md5", algtype_md5
, OAKLEY_ATTR_HASH_ALG_MD5
,
55 eay_md5_init
, eay_md5_update
,
56 eay_md5_final
, eay_md5_hashlen
,
58 { "sha1", algtype_sha1
, OAKLEY_ATTR_HASH_ALG_SHA
,
59 eay_sha1_init
, eay_sha1_update
,
60 eay_sha1_final
, eay_sha1_hashlen
,
63 { "sha2_256", algtype_sha2_256
, OAKLEY_ATTR_HASH_ALG_SHA2_256
,
64 eay_sha2_256_init
, eay_sha2_256_update
,
65 eay_sha2_256_final
, eay_sha2_256_hashlen
,
67 { "sha2_384", algtype_sha2_384
, OAKLEY_ATTR_HASH_ALG_SHA2_384
,
68 eay_sha2_384_init
, eay_sha2_384_update
,
69 eay_sha2_384_final
, eay_sha2_384_hashlen
,
71 { "sha2_512", algtype_sha2_512
, OAKLEY_ATTR_HASH_ALG_SHA2_512
,
72 eay_sha2_512_init
, eay_sha2_512_update
,
73 eay_sha2_512_final
, eay_sha2_512_hashlen
,
78 static struct hmac_algorithm oakley_hmacdef
[] = {
79 { "hmac_md5", algtype_md5
, OAKLEY_ATTR_HASH_ALG_MD5
,
80 eay_hmacmd5_init
, eay_hmacmd5_update
,
81 eay_hmacmd5_final
, NULL
,
83 { "hmac_sha1", algtype_sha1
, OAKLEY_ATTR_HASH_ALG_SHA
,
84 eay_hmacsha1_init
, eay_hmacsha1_update
,
85 eay_hmacsha1_final
, NULL
,
88 { "hmac_sha2_256", algtype_sha2_256
, OAKLEY_ATTR_HASH_ALG_SHA2_256
,
89 eay_hmacsha2_256_init
, eay_hmacsha2_256_update
,
90 eay_hmacsha2_256_final
, NULL
,
91 eay_hmacsha2_256_one
, },
92 { "hmac_sha2_384", algtype_sha2_384
, OAKLEY_ATTR_HASH_ALG_SHA2_384
,
93 eay_hmacsha2_384_init
, eay_hmacsha2_384_update
,
94 eay_hmacsha2_384_final
, NULL
,
95 eay_hmacsha2_384_one
, },
96 { "hmac_sha2_512", algtype_sha2_512
, OAKLEY_ATTR_HASH_ALG_SHA2_512
,
97 eay_hmacsha2_512_init
, eay_hmacsha2_512_update
,
98 eay_hmacsha2_512_final
, NULL
,
99 eay_hmacsha2_512_one
, },
103 static struct enc_algorithm oakley_encdef
[] = {
104 { "des", algtype_des
, OAKLEY_ATTR_ENC_ALG_DES
, 8,
105 eay_des_encrypt
, eay_des_decrypt
,
106 eay_des_weakkey
, eay_des_keylen
, },
107 { "3des", algtype_3des
, OAKLEY_ATTR_ENC_ALG_3DES
, 8,
108 eay_3des_encrypt
, eay_3des_decrypt
,
109 eay_3des_weakkey
, eay_3des_keylen
, },
110 { "aes", algtype_aes
, OAKLEY_ATTR_ENC_ALG_AES
, 16,
111 eay_aes_encrypt
, eay_aes_decrypt
,
112 eay_aes_weakkey
, eay_aes_keylen
, },
115 static struct enc_algorithm ipsec_encdef
[] = {
116 { "des-iv64", algtype_des_iv64
, IPSECDOI_ESP_DES_IV64
, 8,
118 NULL
, eay_des_keylen
, },
119 { "des", algtype_des
, IPSECDOI_ESP_DES
, 8,
121 NULL
, eay_des_keylen
, },
122 { "3des", algtype_3des
, IPSECDOI_ESP_3DES
, 8,
124 NULL
, eay_3des_keylen
, },
125 { "des-iv32", algtype_des_iv32
, IPSECDOI_ESP_DES_IV32
, 8,
127 NULL
, eay_des_keylen
, },
128 { "null", algtype_null_enc
, IPSECDOI_ESP_NULL
, 8,
130 NULL
, eay_null_keylen
, },
131 { "aes", algtype_aes
, IPSECDOI_ESP_AES
, 16,
133 NULL
, eay_aes_keylen
, },
136 static struct hmac_algorithm ipsec_hmacdef
[] = {
137 { "md5", algtype_hmac_md5
, IPSECDOI_ATTR_AUTH_HMAC_MD5
,
139 NULL
, eay_md5_hashlen
,
141 { "sha1", algtype_hmac_sha1
, IPSECDOI_ATTR_AUTH_HMAC_SHA1
,
143 NULL
, eay_sha1_hashlen
,
145 { "null", algtype_non_auth
, IPSECDOI_ATTR_AUTH_NONE
,
147 NULL
, eay_null_hashlen
,
150 { "hmac_sha2_256", algtype_hmac_sha2_256
,IPSECDOI_ATTR_AUTH_HMAC_SHA2_256
,
152 NULL
, eay_sha2_256_hashlen
,
154 { "hmac_sha2_384", algtype_hmac_sha2_384
,IPSECDOI_ATTR_AUTH_HMAC_SHA2_384
,
156 NULL
, eay_sha2_384_hashlen
,
158 { "hmac_sha2_512", algtype_hmac_sha2_512
,IPSECDOI_ATTR_AUTH_HMAC_SHA2_512
,
160 NULL
, eay_sha2_512_hashlen
,
165 static struct misc_algorithm ipsec_compdef
[] = {
166 { "oui", algtype_oui
, IPSECDOI_IPCOMP_OUI
, },
167 { "deflate", algtype_deflate
, IPSECDOI_IPCOMP_DEFLATE
, },
168 { "lzs", algtype_lzs
, IPSECDOI_IPCOMP_LZS
, },
172 * In case of asymetric modes (hybrid xauth), what's racoon mode of
173 * operations ; it seems that the proposal should always use the
174 * initiator half (unless a server initiates a connection, which is
175 * not handled, and probably not useful).
177 static struct misc_algorithm oakley_authdef
[] = {
178 { "pre_shared_key", algtype_psk
, OAKLEY_ATTR_AUTH_METHOD_PSKEY
, },
179 { "dsssig", algtype_dsssig
, OAKLEY_ATTR_AUTH_METHOD_DSSSIG
, },
180 { "rsasig", algtype_rsasig
, OAKLEY_ATTR_AUTH_METHOD_RSASIG
, },
181 { "rsaenc", algtype_rsaenc
, OAKLEY_ATTR_AUTH_METHOD_RSAENC
, },
182 { "rsarev", algtype_rsarev
, OAKLEY_ATTR_AUTH_METHOD_RSAREV
, },
184 { "gssapi_krb", algtype_gssapikrb
,
185 OAKLEY_ATTR_AUTH_METHOD_GSSAPI_KRB
, },
188 { "hybrid_rsa_server", algtype_hybrid_rsa_s
,
189 OAKLEY_ATTR_AUTH_METHOD_HYBRID_RSA_R
, },
191 { "hybrid_dss_server", algtype_hybrid_dss_s
,
192 OAKLEY_ATTR_AUTH_METHOD_HYBRID_DSS_R
, },
194 { "xauth_psk_server", algtype_xauth_psk_s
,
195 OAKLEY_ATTR_AUTH_METHOD_XAUTH_PSKEY_R
, },
197 { "xauth_rsa_server", algtype_xauth_rsa_s
,
198 OAKLEY_ATTR_AUTH_METHOD_XAUTH_RSASIG_R
, },
200 { "hybrid_rsa_client", algtype_hybrid_rsa_c
,
201 OAKLEY_ATTR_AUTH_METHOD_HYBRID_RSA_I
, },
203 { "hybrid_dss_client", algtype_hybrid_dss_c
,
204 OAKLEY_ATTR_AUTH_METHOD_HYBRID_DSS_I
, },
206 { "xauth_psk_client", algtype_xauth_psk_c
,
207 OAKLEY_ATTR_AUTH_METHOD_XAUTH_PSKEY_I
, },
209 { "xauth_rsa_client", algtype_xauth_rsa_c
,
210 OAKLEY_ATTR_AUTH_METHOD_XAUTH_RSASIG_I
, },
214 static struct dh_algorithm oakley_dhdef
[] = {
215 { "modp768", algtype_modp768
, OAKLEY_ATTR_GRP_DESC_MODP768
,
217 { "modp1024", algtype_modp1024
, OAKLEY_ATTR_GRP_DESC_MODP1024
,
219 { "modp1536", algtype_modp1536
, OAKLEY_ATTR_GRP_DESC_MODP1536
,
221 { "modp2048", algtype_modp2048
, OAKLEY_ATTR_GRP_DESC_MODP2048
,
223 { "modp3072", algtype_modp3072
, OAKLEY_ATTR_GRP_DESC_MODP3072
,
225 { "modp4096", algtype_modp4096
, OAKLEY_ATTR_GRP_DESC_MODP4096
,
227 { "modp6144", algtype_modp6144
, OAKLEY_ATTR_GRP_DESC_MODP6144
,
229 { "modp8192", algtype_modp8192
, OAKLEY_ATTR_GRP_DESC_MODP8192
,
233 static struct hash_algorithm
*alg_oakley_hashdef
__P((int));
234 static struct hmac_algorithm
*alg_oakley_hmacdef
__P((int));
235 static struct enc_algorithm
*alg_oakley_encdef
__P((int));
236 static struct enc_algorithm
*alg_ipsec_encdef
__P((int));
237 static struct hmac_algorithm
*alg_ipsec_hmacdef
__P((int));
238 static struct dh_algorithm
*alg_oakley_dhdef
__P((int));
240 /* oakley hash algorithm */
241 static struct hash_algorithm
*
242 alg_oakley_hashdef(doi
)
247 for (i
= 0; i
< ARRAYLEN(oakley_hashdef
); i
++)
248 if (doi
== oakley_hashdef
[i
].doi
) {
249 plog(LLV_DEBUG
, LOCATION
, NULL
, "hash(%s)\n",
250 oakley_hashdef
[i
].name
);
251 return &oakley_hashdef
[i
];
257 alg_oakley_hashdef_ok(doi
)
260 struct hash_algorithm
*f
;
262 f
= alg_oakley_hashdef(doi
);
270 alg_oakley_hashdef_doi(type
)
275 for (i
= 0; i
< ARRAYLEN(oakley_hashdef
); i
++)
276 if (type
== oakley_hashdef
[i
].type
) {
277 res
= oakley_hashdef
[i
].doi
;
284 alg_oakley_hashdef_hashlen(doi
)
287 struct hash_algorithm
*f
;
289 f
= alg_oakley_hashdef(doi
);
290 if (f
== NULL
|| f
->hashlen
== NULL
)
293 return (f
->hashlen
)();
297 alg_oakley_hashdef_name (doi
)
300 struct hash_algorithm
*f
;
302 f
= alg_oakley_hashdef(doi
);
310 alg_oakley_hashdef_one(doi
, buf
)
314 struct hash_algorithm
*f
;
316 f
= alg_oakley_hashdef(doi
);
317 if (f
== NULL
|| f
->hashlen
== NULL
)
320 return (f
->one
)(buf
);
323 /* oakley hmac algorithm */
324 static struct hmac_algorithm
*
325 alg_oakley_hmacdef(doi
)
330 for (i
= 0; i
< ARRAYLEN(oakley_hmacdef
); i
++)
331 if (doi
== oakley_hmacdef
[i
].doi
) {
332 plog(LLV_DEBUG
, LOCATION
, NULL
, "hmac(%s)\n",
333 oakley_hmacdef
[i
].name
);
334 return &oakley_hmacdef
[i
];
340 alg_oakley_hmacdef_doi(type
)
345 for (i
= 0; i
< ARRAYLEN(oakley_hmacdef
); i
++)
346 if (type
== oakley_hmacdef
[i
].type
) {
347 res
= oakley_hmacdef
[i
].doi
;
354 alg_oakley_hmacdef_one(doi
, key
, buf
)
358 struct hmac_algorithm
*f
;
361 struct timeval start
, end
;
364 f
= alg_oakley_hmacdef(doi
);
365 if (f
== NULL
|| f
->one
== NULL
)
369 gettimeofday(&start
, NULL
);
372 res
= (f
->one
)(key
, buf
);
375 gettimeofday(&end
, NULL
);
376 syslog(LOG_NOTICE
, "%s(%s size=%zu): %8.6f", __func__
,
377 f
->name
, buf
->l
, timedelta(&start
, &end
));
383 /* oakley encryption algorithm */
384 static struct enc_algorithm
*
385 alg_oakley_encdef(doi
)
390 for (i
= 0; i
< ARRAYLEN(oakley_encdef
); i
++)
391 if (doi
== oakley_encdef
[i
].doi
) {
392 plog(LLV_DEBUG
, LOCATION
, NULL
, "encryption(%s)\n",
393 oakley_encdef
[i
].name
);
394 return &oakley_encdef
[i
];
400 alg_oakley_encdef_ok(doi
)
403 struct enc_algorithm
*f
;
405 f
= alg_oakley_encdef(doi
);
413 alg_oakley_encdef_doi(type
)
418 for (i
= 0; i
< ARRAYLEN(oakley_encdef
); i
++)
419 if (type
== oakley_encdef
[i
].type
) {
420 res
= oakley_encdef
[i
].doi
;
427 alg_oakley_encdef_keylen(doi
, len
)
430 struct enc_algorithm
*f
;
432 f
= alg_oakley_encdef(doi
);
433 if (f
== NULL
|| f
->keylen
== NULL
)
436 return (f
->keylen
)(len
);
440 alg_oakley_encdef_blocklen(doi
)
443 struct enc_algorithm
*f
;
445 f
= alg_oakley_encdef(doi
);
453 alg_oakley_encdef_name (doi
)
456 struct enc_algorithm
*f
;
458 f
= alg_oakley_encdef(doi
);
466 alg_oakley_encdef_decrypt(doi
, buf
, key
, iv
)
468 vchar_t
*buf
, *key
, *iv
;
471 struct enc_algorithm
*f
;
473 struct timeval start
, end
;
476 f
= alg_oakley_encdef(doi
);
477 if (f
== NULL
|| f
->decrypt
== NULL
)
481 gettimeofday(&start
, NULL
);
484 res
= (f
->decrypt
)(buf
, key
, iv
);
487 gettimeofday(&end
, NULL
);
488 syslog(LOG_NOTICE
, "%s(%s klen=%zu size=%zu): %8.6f", __func__
,
489 f
->name
, key
->l
<< 3, buf
->l
, timedelta(&start
, &end
));
495 alg_oakley_encdef_encrypt(doi
, buf
, key
, iv
)
497 vchar_t
*buf
, *key
, *iv
;
500 struct enc_algorithm
*f
;
502 struct timeval start
, end
;
505 f
= alg_oakley_encdef(doi
);
506 if (f
== NULL
|| f
->encrypt
== NULL
)
510 gettimeofday(&start
, NULL
);
513 res
= (f
->encrypt
)(buf
, key
, iv
);
516 gettimeofday(&end
, NULL
);
517 syslog(LOG_NOTICE
, "%s(%s klen=%zu size=%zu): %8.6f", __func__
,
518 f
->name
, key
->l
<< 3, buf
->l
, timedelta(&start
, &end
));
523 /* ipsec encryption algorithm */
524 static struct enc_algorithm
*
525 alg_ipsec_encdef(doi
)
530 for (i
= 0; i
< ARRAYLEN(ipsec_encdef
); i
++)
531 if (doi
== ipsec_encdef
[i
].doi
) {
532 plog(LLV_DEBUG
, LOCATION
, NULL
, "encryption(%s)\n",
533 ipsec_encdef
[i
].name
);
534 return &ipsec_encdef
[i
];
540 alg_ipsec_encdef_doi(type
)
545 for (i
= 0; i
< ARRAYLEN(ipsec_encdef
); i
++)
546 if (type
== ipsec_encdef
[i
].type
) {
547 res
= ipsec_encdef
[i
].doi
;
554 alg_ipsec_encdef_keylen(doi
, len
)
557 struct enc_algorithm
*f
;
559 f
= alg_ipsec_encdef(doi
);
560 if (f
== NULL
|| f
->keylen
== NULL
)
563 return (f
->keylen
)(len
);
566 /* ipsec hmac algorithm */
567 static struct hmac_algorithm
*
568 alg_ipsec_hmacdef(doi
)
573 for (i
= 0; i
< ARRAYLEN(ipsec_hmacdef
); i
++)
574 if (doi
== ipsec_hmacdef
[i
].doi
) {
575 plog(LLV_DEBUG
, LOCATION
, NULL
, "hmac(%s)\n",
576 ipsec_hmacdef
[i
].name
);
577 return &ipsec_hmacdef
[i
];
583 alg_ipsec_hmacdef_doi(type
)
588 for (i
= 0; i
< ARRAYLEN(ipsec_hmacdef
); i
++)
589 if (type
== ipsec_hmacdef
[i
].type
) {
590 res
= ipsec_hmacdef
[i
].doi
;
597 alg_ipsec_hmacdef_hashlen(doi
)
600 struct hmac_algorithm
*f
;
602 f
= alg_ipsec_hmacdef(doi
);
603 if (f
== NULL
|| f
->hashlen
== NULL
)
606 return (f
->hashlen
)();
611 alg_ipsec_compdef_doi(type
)
616 for (i
= 0; i
< ARRAYLEN(ipsec_compdef
); i
++)
617 if (type
== ipsec_compdef
[i
].type
) {
618 res
= ipsec_compdef
[i
].doi
;
625 static struct dh_algorithm
*
626 alg_oakley_dhdef(doi
)
631 for (i
= 0; i
< ARRAYLEN(oakley_dhdef
); i
++)
632 if (doi
== oakley_dhdef
[i
].doi
) {
633 plog(LLV_DEBUG
, LOCATION
, NULL
, "hmac(%s)\n",
634 oakley_dhdef
[i
].name
);
635 return &oakley_dhdef
[i
];
641 alg_oakley_dhdef_ok(doi
)
644 struct dh_algorithm
*f
;
646 f
= alg_oakley_dhdef(doi
);
654 alg_oakley_dhdef_doi(type
)
659 for (i
= 0; i
< ARRAYLEN(oakley_dhdef
); i
++)
660 if (type
== oakley_dhdef
[i
].type
) {
661 res
= oakley_dhdef
[i
].doi
;
668 alg_oakley_dhdef_group(doi
)
671 struct dh_algorithm
*f
;
673 f
= alg_oakley_dhdef(doi
);
674 if (f
== NULL
|| f
->dhgroup
== NULL
)
681 alg_oakley_dhdef_name (doi
)
684 struct dh_algorithm
*f
;
686 f
= alg_oakley_dhdef(doi
);
692 /* authentication method */
694 alg_oakley_authdef_doi(type
)
699 for (i
= 0; i
< ARRAYLEN(oakley_authdef
); i
++)
700 if (type
== oakley_authdef
[i
].type
) {
701 res
= oakley_authdef
[i
].doi
;
708 alg_oakley_authdef_name (doi
)
713 for (i
= 0; i
< ARRAYLEN(oakley_authdef
); i
++)
714 if (doi
== oakley_authdef
[i
].doi
) {
715 return oakley_authdef
[i
].name
;
721 * give the default key length
723 * 0: fixed key cipher, key length not allowed
724 * positive: default key length
727 default_keylen(class, type
)
732 case algclass_isakmp_enc
:
733 case algclass_ipsec_enc
:
740 case algtype_blowfish
:
742 case algtype_cast128
:
744 case algtype_twofish
:
757 check_keylen(class, type
, len
)
758 int class, type
, len
;
763 case algclass_isakmp_enc
:
764 case algclass_ipsec_enc
:
767 /* unknown class, punt */
768 plog(LLV_ERROR
, LOCATION
, NULL
,
769 "unknown algclass %d\n", class);
773 /* key length must be multiple of 8 bytes - RFC2451 2.2 */
775 case algtype_blowfish
:
777 case algtype_cast128
:
779 case algtype_twofish
:
781 plog(LLV_ERROR
, LOCATION
, NULL
,
782 "key length %d is not multiple of 8\n", len
);
788 /* key length range */
791 case algtype_blowfish
:
792 if (len
< 40 || 448 < len
)
796 if (len
< 40 || 2040 < len
)
799 case algtype_cast128
:
800 if (len
< 40 || 128 < len
)
804 if (!(len
== 128 || len
== 192 || len
== 256))
807 case algtype_twofish
:
808 if (len
< 40 || 256 < len
)
813 plog(LLV_ERROR
, LOCATION
, NULL
,
814 "key length is not allowed");
820 plog(LLV_ERROR
, LOCATION
, NULL
,
821 "key length out of range\n");
829 * convert algorithm type to DOI value.
834 algtype2doi(class, type
)
840 case algclass_ipsec_enc
:
841 res
= alg_ipsec_encdef_doi(type
);
843 case algclass_ipsec_auth
:
844 res
= alg_ipsec_hmacdef_doi(type
);
846 case algclass_ipsec_comp
:
847 res
= alg_ipsec_compdef_doi(type
);
849 case algclass_isakmp_enc
:
850 res
= alg_oakley_encdef_doi(type
);
852 case algclass_isakmp_hash
:
853 res
= alg_oakley_hashdef_doi(type
);
855 case algclass_isakmp_dh
:
856 res
= alg_oakley_dhdef_doi(type
);
858 case algclass_isakmp_ameth
:
859 res
= alg_oakley_authdef_doi(type
);
866 * convert algorithm class to DOI value.
875 case algclass_ipsec_enc
:
876 return IPSECDOI_PROTO_IPSEC_ESP
;
877 case algclass_ipsec_auth
:
878 return IPSECDOI_ATTR_AUTH
;
879 case algclass_ipsec_comp
:
880 return IPSECDOI_PROTO_IPCOMP
;
881 case algclass_isakmp_enc
:
882 return OAKLEY_ATTR_ENC_ALG
;
883 case algclass_isakmp_hash
:
884 return OAKLEY_ATTR_HASH_ALG
;
885 case algclass_isakmp_dh
:
886 return OAKLEY_ATTR_GRP_DESC
;
887 case algclass_isakmp_ameth
:
888 return OAKLEY_ATTR_AUTH_METHOD
;