Annotation of embedaddon/strongswan/src/libstrongswan/crypto/crypters/crypter.c, revision 1.1.1.1

1.1       misho       1: /*
                      2:  * Copyright (C) 2005-2006 Martin Willi
                      3:  * Copyright (C) 2005 Jan Hutter
                      4:  * HSR Hochschule fuer Technik Rapperswil
                      5:  *
                      6:  * This program is free software; you can redistribute it and/or modify it
                      7:  * under the terms of the GNU General Public License as published by the
                      8:  * Free Software Foundation; either version 2 of the License, or (at your
                      9:  * option) any later version.  See <http://www.fsf.org/copyleft/gpl.txt>.
                     10:  *
                     11:  * This program is distributed in the hope that it will be useful, but
                     12:  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
                     13:  * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
                     14:  * for more details.
                     15:  */
                     16: 
                     17: #include <asn1/oid.h>
                     18: 
                     19: #include "crypter.h"
                     20: 
                     21: ENUM_BEGIN(encryption_algorithm_names, ENCR_DES_IV64, ENCR_DES_IV32,
                     22:        "DES_IV64",
                     23:        "DES_CBC",
                     24:        "3DES_CBC",
                     25:        "RC5_CBC",
                     26:        "IDEA_CBC",
                     27:        "CAST_CBC",
                     28:        "BLOWFISH_CBC",
                     29:        "3IDEA",
                     30:        "DES_IV32");
                     31: ENUM_NEXT(encryption_algorithm_names, ENCR_NULL, ENCR_AES_CCM_ICV16, ENCR_DES_IV32,
                     32:        "NULL",
                     33:        "AES_CBC",
                     34:        "AES_CTR",
                     35:        "AES_CCM_8",
                     36:        "AES_CCM_12",
                     37:        "AES_CCM_16");
                     38: ENUM_NEXT(encryption_algorithm_names, ENCR_AES_GCM_ICV8, ENCR_NULL_AUTH_AES_GMAC, ENCR_AES_CCM_ICV16,
                     39:        "AES_GCM_8",
                     40:        "AES_GCM_12",
                     41:        "AES_GCM_16",
                     42:        "NULL_AES_GMAC");
                     43: ENUM_NEXT(encryption_algorithm_names, ENCR_CAMELLIA_CBC, ENCR_CHACHA20_POLY1305, ENCR_NULL_AUTH_AES_GMAC,
                     44:        "CAMELLIA_CBC",
                     45:        "CAMELLIA_CTR",
                     46:        "CAMELLIA_CCM_8",
                     47:        "CAMELLIA_CCM_12",
                     48:        "CAMELLIA_CCM_16",
                     49:        "CHACHA20_POLY1305");
                     50: ENUM_NEXT(encryption_algorithm_names, ENCR_UNDEFINED, ENCR_AES_ECB, ENCR_CHACHA20_POLY1305,
                     51:        "UNDEFINED",
                     52:        "DES_ECB",
                     53:        "SERPENT_CBC",
                     54:        "TWOFISH_CBC",
                     55:        "RC2_CBC",
                     56:        "AES_ECB");
                     57: ENUM_END(encryption_algorithm_names, ENCR_AES_ECB);
                     58: 
                     59: /*
                     60:  * Described in header.
                     61:  */
                     62: encryption_algorithm_t encryption_algorithm_from_oid(int oid, size_t *key_size)
                     63: {
                     64:        encryption_algorithm_t alg;
                     65:        size_t alg_key_size;
                     66: 
                     67:        switch (oid)
                     68:        {
                     69:                case OID_DES_CBC:
                     70:                        alg = ENCR_DES;
                     71:                        alg_key_size = 0;
                     72:                        break;
                     73:                case OID_3DES_EDE_CBC:
                     74:                        alg = ENCR_3DES;
                     75:                        alg_key_size = 0;
                     76:                        break;
                     77:                case OID_AES128_CBC:
                     78:                        alg = ENCR_AES_CBC;
                     79:                        alg_key_size = 128;
                     80:                        break;
                     81:                case OID_AES192_CBC:
                     82:                        alg = ENCR_AES_CBC;
                     83:                        alg_key_size = 192;
                     84:                        break;
                     85:                case OID_AES256_CBC:
                     86:                        alg = ENCR_AES_CBC;
                     87:                        alg_key_size = 256;
                     88:                        break;
                     89:                case OID_CAMELLIA128_CBC:
                     90:                        alg = ENCR_CAMELLIA_CBC;
                     91:                        alg_key_size = 128;
                     92:                        break;
                     93:                case OID_CAMELLIA192_CBC:
                     94:                        alg = ENCR_CAMELLIA_CBC;
                     95:                        alg_key_size = 192;
                     96:                        break;
                     97:                case OID_CAMELLIA256_CBC:
                     98:                        alg = ENCR_CAMELLIA_CBC;
                     99:                        alg_key_size = 256;
                    100:                        break;
                    101:                case OID_BLOWFISH_CBC:
                    102:                        alg = ENCR_BLOWFISH;
                    103:                        alg_key_size = 0;
                    104:                        break;
                    105:                default:
                    106:                        alg = ENCR_UNDEFINED;
                    107:                        alg_key_size = 0;
                    108:        }
                    109:        if (key_size)
                    110:        {
                    111:                        *key_size = alg_key_size;
                    112:        }
                    113:        return alg;
                    114: }
                    115: 
                    116: /*
                    117:  * Described in header.
                    118:  */
                    119: int encryption_algorithm_to_oid(encryption_algorithm_t alg, size_t key_size)
                    120: {
                    121:        int oid;
                    122: 
                    123:        switch(alg)
                    124:        {
                    125:                case ENCR_DES:
                    126:                        oid = OID_DES_CBC;
                    127:                        break;
                    128:                case ENCR_3DES:
                    129:                        oid = OID_3DES_EDE_CBC;
                    130:                        break;
                    131:                case ENCR_AES_CBC:
                    132:                        switch (key_size)
                    133:                        {
                    134:                                case 128:
                    135:                                        oid = OID_AES128_CBC;
                    136:                                        break;
                    137:                                case 192:
                    138:                                        oid = OID_AES192_CBC;
                    139:                                        break;
                    140:                                case 256:
                    141:                                        oid = OID_AES256_CBC;
                    142:                                        break;
                    143:                                default:
                    144:                                        oid = OID_UNKNOWN;
                    145:                        }
                    146:                        break;
                    147:                case ENCR_CAMELLIA_CBC:
                    148:                        switch (key_size)
                    149:                        {
                    150:                                case 128:
                    151:                                        oid = OID_CAMELLIA128_CBC;
                    152:                                        break;
                    153:                                case 192:
                    154:                                        oid = OID_CAMELLIA192_CBC;
                    155:                                        break;
                    156:                                case 256:
                    157:                                        oid = OID_CAMELLIA256_CBC;
                    158:                                        break;
                    159:                                default:
                    160:                                        oid = OID_UNKNOWN;
                    161:                        }
                    162:                        break;
                    163:                case ENCR_BLOWFISH:
                    164:                        oid = OID_BLOWFISH_CBC;
                    165:                        break;
                    166:                default:
                    167:                        oid = OID_UNKNOWN;
                    168:        }
                    169:        return oid;
                    170: }
                    171: 
                    172: /*
                    173:  * Described in header.
                    174:  */
                    175: bool encryption_algorithm_is_aead(encryption_algorithm_t alg)
                    176: {
                    177:        switch (alg)
                    178:        {
                    179:                case ENCR_AES_CCM_ICV8:
                    180:                case ENCR_AES_CCM_ICV12:
                    181:                case ENCR_AES_CCM_ICV16:
                    182:                case ENCR_AES_GCM_ICV8:
                    183:                case ENCR_AES_GCM_ICV12:
                    184:                case ENCR_AES_GCM_ICV16:
                    185:                case ENCR_NULL_AUTH_AES_GMAC:
                    186:                case ENCR_CAMELLIA_CCM_ICV8:
                    187:                case ENCR_CAMELLIA_CCM_ICV12:
                    188:                case ENCR_CAMELLIA_CCM_ICV16:
                    189:                case ENCR_CHACHA20_POLY1305:
                    190:                        return TRUE;
                    191:                default:
                    192:                        return FALSE;
                    193:        }
                    194: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>