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" 
  54 static struct hash_algorithm oakley_hashdef
[] = { 
  55 { "md5",        algtype_md5
,            OAKLEY_ATTR_HASH_ALG_MD5
, 
  56                 eay_md5_init
,           eay_md5_update
, 
  57                 eay_md5_final
,          eay_md5_hashlen
, 
  59 { "sha1",       algtype_sha1
,           OAKLEY_ATTR_HASH_ALG_SHA
, 
  60                 eay_sha1_init
,          eay_sha1_update
, 
  61                 eay_sha1_final
,         eay_sha1_hashlen
, 
  64 { "sha2_256",   algtype_sha2_256
,       OAKLEY_ATTR_HASH_ALG_SHA2_256
, 
  65                 eay_sha2_256_init
,      eay_sha2_256_update
, 
  66                 eay_sha2_256_final
,     eay_sha2_256_hashlen
, 
  68 { "sha2_384",   algtype_sha2_384
,       OAKLEY_ATTR_HASH_ALG_SHA2_384
, 
  69                 eay_sha2_384_init
,      eay_sha2_384_update
, 
  70                 eay_sha2_384_final
,     eay_sha2_384_hashlen
, 
  72 { "sha2_512",   algtype_sha2_512
,       OAKLEY_ATTR_HASH_ALG_SHA2_512
, 
  73                 eay_sha2_512_init
,      eay_sha2_512_update
, 
  74                 eay_sha2_512_final
,     eay_sha2_512_hashlen
, 
  80 static struct hmac_algorithm oakley_hmacdef
[] = { 
  81 { "hmac_md5",   algtype_hmac_md5_128
,       OAKLEY_ATTR_HASH_ALG_MD5
, 
  82                 eay_hmacmd5_init
,       eay_hmacmd5_update
, 
  83                 eay_hmacmd5_final
,      NULL
, 
  85 { "hmac_sha1",  algtype_hmac_sha1_160
,          OAKLEY_ATTR_HASH_ALG_SHA
, 
  86                 eay_hmacsha1_init
,      eay_hmacsha1_update
, 
  87                 eay_hmacsha1_final
,     NULL
, 
  90 { "hmac_sha2_256",      algtype_sha2_256
,       OAKLEY_ATTR_HASH_ALG_SHA2_256
, 
  91                 eay_hmacsha2_256_init
,  eay_hmacsha2_256_update
, 
  92                 eay_hmacsha2_256_final
, NULL
, 
  93                 eay_hmacsha2_256_one
, }, 
  94 { "hmac_sha2_384",      algtype_sha2_384
,       OAKLEY_ATTR_HASH_ALG_SHA2_384
, 
  95                 eay_hmacsha2_384_init
,  eay_hmacsha2_384_update
, 
  96                 eay_hmacsha2_384_final
, NULL
, 
  97                 eay_hmacsha2_384_one
, }, 
  98 { "hmac_sha2_512",      algtype_sha2_512
,       OAKLEY_ATTR_HASH_ALG_SHA2_512
, 
  99                 eay_hmacsha2_512_init
,  eay_hmacsha2_512_update
, 
 100                 eay_hmacsha2_512_final
, NULL
, 
 101                 eay_hmacsha2_512_one
, }, 
 105 static struct enc_algorithm oakley_encdef
[] = { 
 106 { "des",        algtype_des
,            OAKLEY_ATTR_ENC_ALG_DES
, 
 108                 eay_des_encrypt
,        eay_des_decrypt
, 
 109                 eay_des_weakkey
,        eay_des_keylen
, }, 
 110 { "3des",       algtype_3des
,           OAKLEY_ATTR_ENC_ALG_3DES
, 
 112                 eay_3des_encrypt
,       eay_3des_decrypt
, 
 113                 eay_3des_weakkey
,       eay_3des_keylen
, }, 
 114 { "aes",        algtype_aes
,    OAKLEY_ATTR_ENC_ALG_AES
, 
 116                 eay_aes_encrypt
,        eay_aes_decrypt
, 
 117                 eay_aes_weakkey
,        eay_aes_keylen
, }, 
 120 static struct enc_algorithm ipsec_encdef
[] = { 
 121 { "des-iv64",   algtype_des_iv64
,       IPSECDOI_ESP_DES_IV64
,          8, 
 123                 NULL
,                   eay_des_keylen
, }, 
 124 { "des",        algtype_des
,            IPSECDOI_ESP_DES
,               8, 
 126                 NULL
,                   eay_des_keylen
, }, 
 127 { "3des",       algtype_3des
,           IPSECDOI_ESP_3DES
,              8, 
 129                 NULL
,                   eay_3des_keylen
, }, 
 130 { "des-iv32",   algtype_des_iv32
,       IPSECDOI_ESP_DES_IV32
,          8, 
 132                 NULL
,                   eay_des_keylen
, }, 
 133 { "null",       algtype_null_enc
,       IPSECDOI_ESP_NULL
,              8, 
 135                 NULL
,                   eay_null_keylen
, }, 
 136 { "aes",        algtype_aes
,            IPSECDOI_ESP_AES
,               16, 
 138                 NULL
,                   eay_aes_keylen
, }, 
 141 static struct hmac_algorithm ipsec_hmacdef
[] = { 
 142 { "hmac_md5_96",        algtype_hmac_md5_96
,    IPSECDOI_ATTR_AUTH_HMAC_MD5_96
, 
 144                 NULL
,                   eay_md5_hashlen
, 
 146 { "hmac_sha1_96",       algtype_hmac_sha1_96
,   IPSECDOI_ATTR_AUTH_HMAC_SHA1_96
, 
 148                 NULL
,                   eay_sha1_hashlen
, 
 150 { "md5",        algtype_hmac_md5_128
,   IPSECDOI_ATTR_AUTH_HMAC_MD5
, 
 152                 NULL
,                   eay_md5_hashlen
, 
 154 { "sha1",       algtype_hmac_sha1_160
,  IPSECDOI_ATTR_AUTH_HMAC_SHA1
, 
 156                 NULL
,                   eay_sha1_hashlen
, 
 158 { "null",       algtype_non_auth
,       IPSECDOI_ATTR_AUTH_NONE
, 
 160                 NULL
,                   eay_null_hashlen
, 
 163 { "hmac_sha2_256",      algtype_hmac_sha2_256
,IPSECDOI_ATTR_AUTH_HMAC_SHA2_256
, 
 165                 NULL
,                   eay_sha2_256_hashlen
, 
 167 { "hmac_sha2_384",      algtype_hmac_sha2_384
,IPSECDOI_ATTR_AUTH_HMAC_SHA2_384
, 
 169                 NULL
,                   eay_sha2_384_hashlen
, 
 171 { "hmac_sha2_512",      algtype_hmac_sha2_512
,IPSECDOI_ATTR_AUTH_HMAC_SHA2_512
, 
 173                 NULL
,                   eay_sha2_512_hashlen
, 
 178 static struct misc_algorithm ipsec_compdef
[] = { 
 179 { "oui",        algtype_oui
,            IPSECDOI_IPCOMP_OUI
, }, 
 180 { "deflate",    algtype_deflate
,        IPSECDOI_IPCOMP_DEFLATE
, }, 
 181 { "lzs",        algtype_lzs
,            IPSECDOI_IPCOMP_LZS
, }, 
 185  * In case of asymetric modes (hybrid xauth), what's racoon mode of 
 186  * operations ; it seems that the proposal should always use the 
 187  * initiator half (unless a server initiates a connection, which is 
 188  * not handled, and probably not useful). 
 190 static struct misc_algorithm oakley_authdef
[] = { 
 191 { "pre_shared_key",     algtype_psk
,    OAKLEY_ATTR_AUTH_METHOD_PSKEY
, }, 
 192 { "dsssig",             algtype_dsssig
, OAKLEY_ATTR_AUTH_METHOD_DSSSIG
, }, 
 193 { "rsasig",             algtype_rsasig
, OAKLEY_ATTR_AUTH_METHOD_RSASIG
, }, 
 194 { "rsaenc",             algtype_rsaenc
, OAKLEY_ATTR_AUTH_METHOD_RSAENC
, }, 
 195 { "rsarev",             algtype_rsarev
, OAKLEY_ATTR_AUTH_METHOD_RSAREV
, }, 
 197 { "gssapi_krb",         algtype_gssapikrb
, 
 198     OAKLEY_ATTR_AUTH_METHOD_GSSAPI_KRB
, }, 
 201 { "hybrid_rsa_server",  algtype_hybrid_rsa_s
,    
 202     OAKLEY_ATTR_AUTH_METHOD_HYBRID_RSA_R
, }, 
 204 { "hybrid_dss_server",  algtype_hybrid_dss_s
,    
 205     OAKLEY_ATTR_AUTH_METHOD_HYBRID_DSS_R
, }, 
 207 { "xauth_psk_server",   algtype_xauth_psk_s
,     
 208     OAKLEY_ATTR_AUTH_METHOD_XAUTH_PSKEY_R
, }, 
 210 { "xauth_rsa_server",   algtype_xauth_rsa_s
,     
 211     OAKLEY_ATTR_AUTH_METHOD_XAUTH_RSASIG_R
, }, 
 213 { "hybrid_rsa_client",  algtype_hybrid_rsa_c
,    
 214     OAKLEY_ATTR_AUTH_METHOD_HYBRID_RSA_I
, }, 
 216 { "hybrid_dss_client",  algtype_hybrid_dss_c
,    
 217     OAKLEY_ATTR_AUTH_METHOD_HYBRID_DSS_I
, }, 
 219 { "xauth_psk_client",   algtype_xauth_psk_c
,     
 220     OAKLEY_ATTR_AUTH_METHOD_XAUTH_PSKEY_I
, }, 
 222 { "xauth_rsa_client",   algtype_xauth_rsa_c
,     
 223     OAKLEY_ATTR_AUTH_METHOD_XAUTH_RSASIG_I
, }, 
 225 { "eap_psk_client", algtype_eap_psk_c
, 
 226     OAKLEY_ATTR_AUTH_METHOD_EAP_PSKEY_I
, }, 
 228 { "eap_rsa_client", algtype_eap_rsa_c
, 
 229     OAKLEY_ATTR_AUTH_METHOD_EAP_RSASIG_I
, }, 
 233 static struct dh_algorithm oakley_dhdef
[] = { 
 234 { "modp768",    algtype_modp768
,        OAKLEY_ATTR_GRP_DESC_MODP768
, 
 236 { "modp1024",   algtype_modp1024
,       OAKLEY_ATTR_GRP_DESC_MODP1024
, 
 238 { "modp1536",   algtype_modp1536
,       OAKLEY_ATTR_GRP_DESC_MODP1536
, 
 240 { "modp2048",   algtype_modp2048
,       OAKLEY_ATTR_GRP_DESC_MODP2048
, 
 242 { "modp3072",   algtype_modp3072
,       OAKLEY_ATTR_GRP_DESC_MODP3072
, 
 244 { "modp4096",   algtype_modp4096
,       OAKLEY_ATTR_GRP_DESC_MODP4096
, 
 246 { "modp6144",   algtype_modp6144
,       OAKLEY_ATTR_GRP_DESC_MODP6144
, 
 248 { "modp8192",   algtype_modp8192
,       OAKLEY_ATTR_GRP_DESC_MODP8192
, 
 252 static struct hash_algorithm 
*alg_oakley_hashdef (int); 
 253 static struct hmac_algorithm 
*alg_oakley_hmacdef (int); 
 254 static struct enc_algorithm 
*alg_oakley_encdef (int); 
 255 static struct enc_algorithm 
*alg_ipsec_encdef (int); 
 256 static struct hmac_algorithm 
*alg_ipsec_hmacdef (int); 
 257 static struct dh_algorithm 
*alg_oakley_dhdef (int); 
 259 /* oakley hash algorithm */ 
 260 static struct hash_algorithm 
* 
 261 alg_oakley_hashdef(doi
) 
 266         for (i 
= 0; i 
< ARRAYLEN(oakley_hashdef
); i
++) 
 267                 if (doi 
== oakley_hashdef
[i
].doi
) { 
 268                         plog(ASL_LEVEL_DEBUG
, "hash(%s)\n", 
 269                                 oakley_hashdef
[i
].name
); 
 270                         return &oakley_hashdef
[i
]; 
 276 alg_oakley_hashdef_ok(doi
) 
 279         struct hash_algorithm 
*f
; 
 281         f 
= alg_oakley_hashdef(doi
); 
 289 alg_oakley_hashdef_doi(type
) 
 294         for (i 
= 0; i 
< ARRAYLEN(oakley_hashdef
); i
++) 
 295                 if (type 
== oakley_hashdef
[i
].type
) { 
 296                         res 
= oakley_hashdef
[i
].doi
; 
 303 alg_oakley_hashdef_hashlen(doi
) 
 306         struct hash_algorithm 
*f
; 
 308         f 
= alg_oakley_hashdef(doi
); 
 309         if (f 
== NULL 
|| f
->hashlen 
== NULL
) 
 312         return (f
->hashlen
)(); 
 316 alg_oakley_hashdef_name (doi
) 
 319         struct hash_algorithm 
*f
; 
 321         f 
= alg_oakley_hashdef(doi
); 
 329 alg_oakley_hashdef_one(doi
, buf
) 
 333         struct hash_algorithm 
*f
; 
 335         f 
= alg_oakley_hashdef(doi
); 
 336         if (f 
== NULL 
|| f
->hashlen 
== NULL
) 
 339         return (f
->one
)(buf
); 
 342 /* oakley hmac algorithm */ 
 343 static struct hmac_algorithm 
* 
 344 alg_oakley_hmacdef(doi
) 
 349         for (i 
= 0; i 
< ARRAYLEN(oakley_hmacdef
); i
++) 
 350                 if (doi 
== oakley_hmacdef
[i
].doi
) { 
 351                         plog(ASL_LEVEL_DEBUG
, "hmac(%s)\n", 
 352                                 oakley_hmacdef
[i
].name
); 
 353                         return &oakley_hmacdef
[i
]; 
 359 alg_oakley_hmacdef_doi(type
) 
 364         for (i 
= 0; i 
< ARRAYLEN(oakley_hmacdef
); i
++) 
 365                 if (type 
== oakley_hmacdef
[i
].type
) { 
 366                         res 
= oakley_hmacdef
[i
].doi
; 
 373 alg_oakley_hmacdef_one(doi
, key
, buf
) 
 377         struct hmac_algorithm 
*f
; 
 380         struct timeval start
, end
; 
 383         f 
= alg_oakley_hmacdef(doi
); 
 384         if (f 
== NULL 
|| f
->one 
== NULL
) 
 388         gettimeofday(&start
, NULL
); 
 391         res 
= (f
->one
)(key
, buf
); 
 394         gettimeofday(&end
, NULL
); 
 395         plog(ASL_LEVEL_NOTICE
, "%s(%s size=%zu): %8.6f", __func__
, 
 396                 f
->name
, buf
->l
, timedelta(&start
, &end
)); 
 402 /* oakley encryption algorithm */ 
 403 static struct enc_algorithm 
* 
 404 alg_oakley_encdef(doi
) 
 409         for (i 
= 0; i 
< ARRAYLEN(oakley_encdef
); i
++) 
 410                 if (doi 
== oakley_encdef
[i
].doi
) { 
 411                         plog(ASL_LEVEL_DEBUG
, "encryption(%s)\n", 
 412                                 oakley_encdef
[i
].name
); 
 413                         return &oakley_encdef
[i
]; 
 419 alg_oakley_encdef_ok(doi
) 
 422         struct enc_algorithm 
*f
; 
 424         f 
= alg_oakley_encdef(doi
); 
 432 alg_oakley_encdef_doi(type
) 
 437         for (i 
= 0; i 
< ARRAYLEN(oakley_encdef
); i
++) 
 438                 if (type 
== oakley_encdef
[i
].type
) { 
 439                         res 
= oakley_encdef
[i
].doi
; 
 446 alg_oakley_encdef_keylen(doi
, len
) 
 449         struct enc_algorithm 
*f
; 
 451         f 
= alg_oakley_encdef(doi
); 
 452         if (f 
== NULL 
|| f
->keylen 
== NULL
) 
 455         return (f
->keylen
)(len
); 
 459 alg_oakley_encdef_blocklen(doi
) 
 462         struct enc_algorithm 
*f
; 
 464         f 
= alg_oakley_encdef(doi
); 
 472 alg_oakley_encdef_name (doi
) 
 475         struct enc_algorithm 
*f
; 
 477         f 
= alg_oakley_encdef(doi
); 
 485 alg_oakley_encdef_decrypt(doi
, buf
, key
, iv
) 
 487         vchar_t 
*buf
, *key
, *iv
; 
 490         struct enc_algorithm 
*f
; 
 492         struct timeval start
, end
; 
 495         f 
= alg_oakley_encdef(doi
); 
 496         if (f 
== NULL 
|| f
->decrypt 
== NULL
) 
 500         gettimeofday(&start
, NULL
); 
 503         res 
= (f
->decrypt
)(buf
, key
, iv
); 
 506         gettimeofday(&end
, NULL
); 
 507         plog(ASL_LEVEL_NOTICE
, "%s(%s klen=%zu size=%zu): %8.6f", __func__
, 
 508                 f
->name
, key
->l 
<< 3, buf
->l
, timedelta(&start
, &end
)); 
 514 alg_oakley_encdef_encrypt(doi
, buf
, key
, iv
) 
 516         vchar_t 
*buf
, *key
, *iv
; 
 519         struct enc_algorithm 
*f
; 
 521         struct timeval start
, end
; 
 524         f 
= alg_oakley_encdef(doi
); 
 525         if (f 
== NULL 
|| f
->encrypt 
== NULL
) 
 529         gettimeofday(&start
, NULL
); 
 532         res 
= (f
->encrypt
)(buf
, key
, iv
); 
 535         gettimeofday(&end
, NULL
); 
 536         plog(ASL_LEVEL_NOTICE
, "%s(%s klen=%zu size=%zu): %8.6f", __func__
, 
 537                 f
->name
, key
->l 
<< 3, buf
->l
, timedelta(&start
, &end
)); 
 542 /* ipsec encryption algorithm */ 
 543 static struct enc_algorithm 
* 
 544 alg_ipsec_encdef(doi
) 
 549         for (i 
= 0; i 
< ARRAYLEN(ipsec_encdef
); i
++) 
 550                 if (doi 
== ipsec_encdef
[i
].doi
) { 
 551                         plog(ASL_LEVEL_DEBUG
, "encryption(%s)\n", 
 552                                 ipsec_encdef
[i
].name
); 
 553                         return &ipsec_encdef
[i
]; 
 559 alg_ipsec_encdef_doi(type
) 
 564         for (i 
= 0; i 
< ARRAYLEN(ipsec_encdef
); i
++) 
 565                 if (type 
== ipsec_encdef
[i
].type
) { 
 566                         res 
= ipsec_encdef
[i
].doi
; 
 573 alg_ipsec_encdef_keylen(doi
, len
) 
 576         struct enc_algorithm 
*f
; 
 578         f 
= alg_ipsec_encdef(doi
); 
 579         if (f 
== NULL 
|| f
->keylen 
== NULL
) 
 582         return (f
->keylen
)(len
); 
 585 /* ipsec hmac algorithm */ 
 586 static struct hmac_algorithm 
* 
 587 alg_ipsec_hmacdef(doi
) 
 592         for (i 
= 0; i 
< ARRAYLEN(ipsec_hmacdef
); i
++) 
 593                 if (doi 
== ipsec_hmacdef
[i
].doi
) { 
 594                         plog(ASL_LEVEL_DEBUG
, "hmac(%s)\n", 
 595                                 ipsec_hmacdef
[i
].name
); 
 596                         return &ipsec_hmacdef
[i
]; 
 602 alg_ipsec_hmacdef_doi(type
) 
 607         for (i 
= 0; i 
< ARRAYLEN(ipsec_hmacdef
); i
++) 
 608                 if (type 
== ipsec_hmacdef
[i
].type
) { 
 609                         res 
= ipsec_hmacdef
[i
].doi
; 
 616 alg_ipsec_hmacdef_hashlen(doi
) 
 619         struct hmac_algorithm 
*f
; 
 621         f 
= alg_ipsec_hmacdef(doi
); 
 622         if (f 
== NULL 
|| f
->hashlen 
== NULL
) 
 625         return (f
->hashlen
)(); 
 630 alg_ipsec_compdef_doi(type
) 
 635         for (i 
= 0; i 
< ARRAYLEN(ipsec_compdef
); i
++) 
 636                 if (type 
== ipsec_compdef
[i
].type
) { 
 637                         res 
= ipsec_compdef
[i
].doi
; 
 644 static struct dh_algorithm 
* 
 645 alg_oakley_dhdef(doi
) 
 650         for (i 
= 0; i 
< ARRAYLEN(oakley_dhdef
); i
++) 
 651                 if (doi 
== oakley_dhdef
[i
].doi
) { 
 652                         plog(ASL_LEVEL_DEBUG
, "hmac(%s)\n", 
 653                                 oakley_dhdef
[i
].name
); 
 654                         return &oakley_dhdef
[i
]; 
 660 alg_oakley_dhdef_ok(doi
) 
 663         struct dh_algorithm 
*f
; 
 665         f 
= alg_oakley_dhdef(doi
); 
 673 alg_oakley_dhdef_doi(type
) 
 678         for (i 
= 0; i 
< ARRAYLEN(oakley_dhdef
); i
++) 
 679                 if (type 
== oakley_dhdef
[i
].type
) { 
 680                         res 
= oakley_dhdef
[i
].doi
; 
 687 alg_oakley_dhdef_group(doi
) 
 690         struct dh_algorithm 
*f
; 
 692         f 
= alg_oakley_dhdef(doi
); 
 693         if (f 
== NULL 
|| f
->dhgroup 
== NULL
) 
 700 alg_oakley_dhdef_name (doi
) 
 703         struct dh_algorithm 
*f
; 
 705         f 
= alg_oakley_dhdef(doi
); 
 711 /* authentication method */ 
 713 alg_oakley_authdef_doi(type
) 
 718         for (i 
= 0; i 
< ARRAYLEN(oakley_authdef
); i
++) 
 719                 if (type 
== oakley_authdef
[i
].type
) { 
 720                         res 
= oakley_authdef
[i
].doi
; 
 727 alg_oakley_authdef_name (doi
) 
 732         for (i 
= 0; i 
< ARRAYLEN(oakley_authdef
); i
++) 
 733                 if (doi 
== oakley_authdef
[i
].doi
) { 
 734                         return oakley_authdef
[i
].name
; 
 741  * give the default key length 
 743  *      0:              fixed key cipher, key length not allowed 
 744  *      positive:       default key length 
 747 default_keylen(class, type
) 
 752         case algclass_isakmp_enc
: 
 753         case algclass_ipsec_enc
: 
 760         case algtype_blowfish
: 
 762         case algtype_cast128
: 
 764         case algtype_twofish
: 
 777 check_keylen(class, type
, len
) 
 778         int class, type
, len
; 
 783         case algclass_isakmp_enc
: 
 784         case algclass_ipsec_enc
: 
 787                 /* unknown class, punt */ 
 789                         "unknown algorithm class %d\n", class); 
 793         /* key length must be multiple of 8 bytes - RFC2451 2.2 */ 
 795         case algtype_blowfish
: 
 797         case algtype_cast128
: 
 799         case algtype_twofish
: 
 802                                 "key length %d is not multiple of 8\n", len
); 
 808         /* key length range */ 
 811         case algtype_blowfish
: 
 812                 if (len 
< 40 || 448 < len
) 
 816                 if (len 
< 40 || 2040 < len
) 
 819         case algtype_cast128
: 
 820                 if (len 
< 40 || 128 < len
) 
 824                 if (!(len 
== 128 || len 
== 192 || len 
== 256)) 
 827         case algtype_twofish
: 
 828                 if (len 
< 40 || 256 < len
) 
 834                                 "key length is not allowed"); 
 841                         "key length out of range\n"); 
 849  * convert algorithm type to DOI value. 
 854 algtype2doi(class, type
) 
 860         case algclass_ipsec_enc
: 
 861                 res 
= alg_ipsec_encdef_doi(type
); 
 863         case algclass_ipsec_auth
: 
 864                 res 
= alg_ipsec_hmacdef_doi(type
); 
 866         case algclass_ipsec_comp
: 
 867                 res 
= alg_ipsec_compdef_doi(type
); 
 869         case algclass_isakmp_enc
: 
 870                 res 
=  alg_oakley_encdef_doi(type
); 
 872         case algclass_isakmp_hash
: 
 873                 res 
= alg_oakley_hashdef_doi(type
); 
 875         case algclass_isakmp_dh
: 
 876                 res 
= alg_oakley_dhdef_doi(type
); 
 878         case algclass_isakmp_ameth
: 
 879                 res 
= alg_oakley_authdef_doi(type
); 
 887  * convert algorithm class to DOI value. 
 896         case algclass_ipsec_enc
: 
 897                 return IPSECDOI_PROTO_IPSEC_ESP
; 
 898         case algclass_ipsec_auth
: 
 899                 return IPSECDOI_ATTR_AUTH
; 
 900         case algclass_ipsec_comp
: 
 901                 return IPSECDOI_PROTO_IPCOMP
; 
 902         case algclass_isakmp_enc
: 
 903                 return OAKLEY_ATTR_ENC_ALG
; 
 904         case algclass_isakmp_hash
: 
 905                 return OAKLEY_ATTR_HASH_ALG
; 
 906         case algclass_isakmp_dh
: 
 907                 return OAKLEY_ATTR_GRP_DESC
; 
 908         case algclass_isakmp_ameth
: 
 909                 return OAKLEY_ATTR_AUTH_METHOD
;