Annotation of embedaddon/strongswan/src/libimcv/pts/pts_meas_algo.c, revision 1.1.1.1

1.1       misho       1: /*
                      2:  * Copyright (C) 2011-2014 Andreas Steffen
                      3:  * HSR Hochschule fuer Technik Rapperswil
                      4:  *
                      5:  * This program is free software; you can redistribute it and/or modify it
                      6:  * under the terms of the GNU General Public License as published by the
                      7:  * Free Software Foundation; either version 2 of the License, or (at your
                      8:  * option) any later version.  See <http://www.fsf.org/copyleft/gpl.txt>.
                      9:  *
                     10:  * This program is distributed in the hope that it will be useful, but
                     11:  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
                     12:  * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
                     13:  * for more details.
                     14:  */
                     15: 
                     16: #include "pts_meas_algo.h"
                     17: 
                     18: #include <utils/debug.h>
                     19: 
                     20: ENUM_BEGIN(pts_meas_algorithm_names, PTS_MEAS_ALGO_NONE, PTS_MEAS_ALGO_NONE,
                     21:        "None");
                     22: ENUM_NEXT(pts_meas_algorithm_names,    PTS_MEAS_ALGO_SHA384, PTS_MEAS_ALGO_SHA384,
                     23:                                                                        PTS_MEAS_ALGO_NONE,
                     24:        "SHA384");
                     25: ENUM_NEXT(pts_meas_algorithm_names,    PTS_MEAS_ALGO_SHA256, PTS_MEAS_ALGO_SHA256,
                     26:                                                                        PTS_MEAS_ALGO_SHA384,
                     27:        "SHA256");
                     28: ENUM_NEXT(pts_meas_algorithm_names,    PTS_MEAS_ALGO_SHA1, PTS_MEAS_ALGO_SHA1,
                     29:                                                                        PTS_MEAS_ALGO_SHA256,
                     30:        "SHA1");
                     31: ENUM_END(pts_meas_algorithm_names,  PTS_MEAS_ALGO_SHA1);
                     32: 
                     33: /**
                     34:  * Described in header.
                     35:  */
                     36: bool pts_meas_algo_probe(pts_meas_algorithms_t *algorithms)
                     37: {
                     38:        enumerator_t *enumerator;
                     39:        hash_algorithm_t hash_alg;
                     40:        const char *plugin_name;
                     41:        char format1[] = "  %s PTS measurement algorithm %N[%s] available";
                     42:        char format2[] = "  %s PTS measurement algorithm %N not available";
                     43: 
                     44:        *algorithms = 0;
                     45: 
                     46:        enumerator = lib->crypto->create_hasher_enumerator(lib->crypto);
                     47:        while (enumerator->enumerate(enumerator, &hash_alg, &plugin_name))
                     48:        {
                     49:                if (hash_alg == HASH_SHA1)
                     50:                {
                     51:                        *algorithms |= PTS_MEAS_ALGO_SHA1;
                     52:                        DBG2(DBG_PTS, format1, "mandatory", hash_algorithm_names, hash_alg,
                     53:                                                                  plugin_name);
                     54:                }
                     55:                else if (hash_alg == HASH_SHA256)
                     56:                {
                     57:                        *algorithms |= PTS_MEAS_ALGO_SHA256;
                     58:                        DBG2(DBG_PTS, format1, "mandatory", hash_algorithm_names, hash_alg,
                     59:                                                                  plugin_name);
                     60:                }
                     61:                else if (hash_alg == HASH_SHA384)
                     62:                {
                     63:                        *algorithms |= PTS_MEAS_ALGO_SHA384;
                     64:                        DBG2(DBG_PTS, format1, "optional ", hash_algorithm_names, hash_alg,
                     65:                                                                  plugin_name);
                     66:                }
                     67:        }
                     68:        enumerator->destroy(enumerator);
                     69: 
                     70:        if (!(*algorithms & PTS_MEAS_ALGO_SHA384))
                     71:        {
                     72:                DBG1(DBG_PTS, format2, "optional ", hash_algorithm_names, HASH_SHA384);
                     73:        }
                     74:        if ((*algorithms & PTS_MEAS_ALGO_SHA1) &&
                     75:                (*algorithms & PTS_MEAS_ALGO_SHA256))
                     76:        {
                     77:                return TRUE;
                     78:        }
                     79:        if (!(*algorithms & PTS_MEAS_ALGO_SHA1))
                     80:        {
                     81:                DBG1(DBG_PTS, format2, "mandatory", hash_algorithm_names, HASH_SHA1);
                     82:        }
                     83:        if (!(*algorithms & PTS_MEAS_ALGO_SHA256))
                     84:        {
                     85:                DBG1(DBG_PTS, format2, "mandatory", hash_algorithm_names, HASH_SHA256);
                     86:        }
                     87:        return FALSE;
                     88: }
                     89: 
                     90: /**
                     91:  * Described in header.
                     92:  */
                     93: bool pts_meas_algo_update(char *hash_alg, pts_meas_algorithms_t *algorithms)
                     94: {
                     95:        if (strcaseeq(hash_alg, "sha384") || strcaseeq(hash_alg, "sha2_384"))
                     96:        {
                     97:                /* nothing to update, all algorithms are supported */
                     98:                return TRUE;
                     99:        }
                    100:        if (strcaseeq(hash_alg, "sha256") || strcaseeq(hash_alg, "sha2_256"))
                    101:        {
                    102:                /* remove SHA384algorithm */
                    103:                *algorithms &= ~PTS_MEAS_ALGO_SHA384;
                    104:                return TRUE;
                    105:        }
                    106:        if (strcaseeq(hash_alg, "sha1"))
                    107:        {
                    108:                /* remove SHA384 and SHA256 algorithms */
                    109:                *algorithms &= ~(PTS_MEAS_ALGO_SHA384 | PTS_MEAS_ALGO_SHA256);
                    110:                return TRUE;
                    111:        }
                    112:        DBG1(DBG_PTS, "unknown hash algorithm '%s' configured", hash_alg);
                    113:        return FALSE;
                    114: }
                    115: 
                    116: /**
                    117:  * Described in header.
                    118:  */
                    119: pts_meas_algorithms_t pts_meas_algo_select(pts_meas_algorithms_t supported_algos,
                    120:                                                                                   pts_meas_algorithms_t offered_algos)
                    121: {
                    122:        if ((supported_algos & PTS_MEAS_ALGO_SHA384) &&
                    123:                (offered_algos   & PTS_MEAS_ALGO_SHA384))
                    124:        {
                    125:                return PTS_MEAS_ALGO_SHA384;
                    126:        }
                    127:        if ((supported_algos & PTS_MEAS_ALGO_SHA256) &&
                    128:                (offered_algos   & PTS_MEAS_ALGO_SHA256))
                    129:        {
                    130:                return PTS_MEAS_ALGO_SHA256;
                    131:        }
                    132:        if ((supported_algos & PTS_MEAS_ALGO_SHA1) &&
                    133:                (offered_algos   & PTS_MEAS_ALGO_SHA1))
                    134:        {
                    135:                return PTS_MEAS_ALGO_SHA1;
                    136:        }
                    137:        return PTS_MEAS_ALGO_NONE;
                    138: }
                    139: 
                    140: /**
                    141:  * Described in header.
                    142:  */
                    143: hash_algorithm_t pts_meas_algo_to_hash(pts_meas_algorithms_t algorithm)
                    144: {
                    145:        switch (algorithm)
                    146:        {
                    147:                case PTS_MEAS_ALGO_SHA1:
                    148:                        return HASH_SHA1;
                    149:                case PTS_MEAS_ALGO_SHA256:
                    150:                        return HASH_SHA256;
                    151:                case PTS_MEAS_ALGO_SHA384:
                    152:                        return HASH_SHA384;
                    153:                default:
                    154:                        return HASH_UNKNOWN;
                    155:        }
                    156: }
                    157: 
                    158: /**
                    159:  * Described in header.
                    160:  */
                    161: pts_meas_algorithms_t pts_meas_algo_from_hash(hash_algorithm_t algorithm)
                    162: {
                    163:        switch (algorithm)
                    164:        {
                    165:                case HASH_SHA1:
                    166:                        return PTS_MEAS_ALGO_SHA1;
                    167:                case HASH_SHA256:
                    168:                        return PTS_MEAS_ALGO_SHA256;
                    169:                case HASH_SHA384:
                    170:                        return PTS_MEAS_ALGO_SHA384;
                    171:                default:
                    172:                        return PTS_MEAS_ALGO_NONE;
                    173:        }
                    174: }
                    175: 
                    176: /**
                    177:  * Described in header.
                    178:  */
                    179: size_t pts_meas_algo_hash_size(pts_meas_algorithms_t algorithm)
                    180: {
                    181:        switch (algorithm)
                    182:        {
                    183:                case PTS_MEAS_ALGO_SHA1:
                    184:                        return HASH_SIZE_SHA1;
                    185:                case PTS_MEAS_ALGO_SHA256:
                    186:                        return HASH_SIZE_SHA256;
                    187:                case PTS_MEAS_ALGO_SHA384:
                    188:                        return HASH_SIZE_SHA384;
                    189:                case PTS_MEAS_ALGO_NONE:
                    190:                default:
                    191:                        return 0;
                    192:        }
                    193: }
                    194: 

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