Annotation of embedaddon/strongswan/src/libstrongswan/plugins/af_alg/af_alg_hasher.c, revision 1.1.1.1

1.1       misho       1: /*
                      2:  * Copyright (C) 2010 Martin Willi
                      3:  * Copyright (C) 2010 revosec AG
                      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 "af_alg_hasher.h"
                     17: #include "af_alg_ops.h"
                     18: 
                     19: typedef struct private_af_alg_hasher_t private_af_alg_hasher_t;
                     20: 
                     21: /**
                     22:  * Private data of af_alg_hasher_t
                     23:  */
                     24: struct private_af_alg_hasher_t {
                     25: 
                     26:        /**
                     27:         * Public part of this class.
                     28:         */
                     29:        af_alg_hasher_t public;
                     30: 
                     31:        /**
                     32:         * AF_ALG operations
                     33:         */
                     34:        af_alg_ops_t *ops;
                     35: 
                     36:        /**
                     37:         * Size of the hash
                     38:         */
                     39:        size_t size;
                     40: };
                     41: 
                     42: /**
                     43:  * Algorithm database
                     44:  */
                     45: static struct {
                     46:        hash_algorithm_t id;
                     47:        char *name;
                     48:        size_t size;
                     49: } algs[AF_ALG_HASHER] = {
                     50:        {HASH_MD4,                      "md4",                  HASH_SIZE_MD4           },
                     51:        {HASH_MD5,                      "md5",                  HASH_SIZE_MD5           },
                     52:        {HASH_SHA1,                     "sha1",                 HASH_SIZE_SHA1          },
                     53:        {HASH_SHA224,           "sha224",               HASH_SIZE_SHA224        },
                     54:        {HASH_SHA256,           "sha256",               HASH_SIZE_SHA256        },
                     55:        {HASH_SHA384,           "sha384",               HASH_SIZE_SHA384        },
                     56:        {HASH_SHA512,           "sha512",               HASH_SIZE_SHA512        },
                     57: };
                     58: 
                     59: /**
                     60:  * See header.
                     61:  */
                     62: void af_alg_hasher_probe(plugin_feature_t *features, int *pos)
                     63: {
                     64:        af_alg_ops_t *ops;
                     65:        int i;
                     66: 
                     67:        for (i = 0; i < countof(algs); i++)
                     68:        {
                     69:                ops = af_alg_ops_create("hash", algs[i].name);
                     70:                if (ops)
                     71:                {
                     72:                        ops->destroy(ops);
                     73:                        features[(*pos)++] = PLUGIN_PROVIDE(HASHER, algs[i].id);
                     74:                }
                     75:        }
                     76: }
                     77: 
                     78: /**
                     79:  * Get the kernel algorithm string and hash size for our identifier
                     80:  */
                     81: static size_t lookup_alg(hash_algorithm_t algo, char **name)
                     82: {
                     83:        int i;
                     84: 
                     85:        for (i = 0; i < countof(algs); i++)
                     86:        {
                     87:                if (algs[i].id == algo)
                     88:                {
                     89:                        *name = algs[i].name;
                     90:                        return algs[i].size;
                     91:                }
                     92:        }
                     93:        return 0;
                     94: }
                     95: 
                     96: METHOD(hasher_t, get_hash_size, size_t,
                     97:        private_af_alg_hasher_t *this)
                     98: {
                     99:        return this->size;
                    100: }
                    101: 
                    102: METHOD(hasher_t, reset, bool,
                    103:        private_af_alg_hasher_t *this)
                    104: {
                    105:        this->ops->reset(this->ops);
                    106:        return TRUE;
                    107: }
                    108: 
                    109: METHOD(hasher_t, get_hash, bool,
                    110:        private_af_alg_hasher_t *this, chunk_t chunk, uint8_t *hash)
                    111: {
                    112:        return this->ops->hash(this->ops, chunk, hash, this->size);
                    113: }
                    114: 
                    115: METHOD(hasher_t, allocate_hash, bool,
                    116:        private_af_alg_hasher_t *this, chunk_t chunk, chunk_t *hash)
                    117: {
                    118:        if (hash)
                    119:        {
                    120:                *hash = chunk_alloc(get_hash_size(this));
                    121:                return get_hash(this, chunk, hash->ptr);
                    122:        }
                    123:        return get_hash(this, chunk, NULL);
                    124: }
                    125: 
                    126: METHOD(hasher_t, destroy, void,
                    127:        private_af_alg_hasher_t *this)
                    128: {
                    129:        this->ops->destroy(this->ops);
                    130:        free(this);
                    131: }
                    132: 
                    133: /*
                    134:  * Described in header
                    135:  */
                    136: af_alg_hasher_t *af_alg_hasher_create(hash_algorithm_t algo)
                    137: {
                    138:        private_af_alg_hasher_t *this;
                    139:        char *name;
                    140:        size_t size;
                    141: 
                    142:        size = lookup_alg(algo, &name);
                    143:        if (!size)
                    144:        {       /* not supported by kernel */
                    145:                return NULL;
                    146:        }
                    147: 
                    148:        INIT(this,
                    149:                .public = {
                    150:                        .hasher = {
                    151:                                .get_hash = _get_hash,
                    152:                                .allocate_hash = _allocate_hash,
                    153:                                .get_hash_size = _get_hash_size,
                    154:                                .reset = _reset,
                    155:                                .destroy = _destroy,
                    156:                        },
                    157:                },
                    158:                .ops = af_alg_ops_create("hash", name),
                    159:                .size = size,
                    160:        );
                    161:        if (!this->ops)
                    162:        {
                    163:                free(this);
                    164:                return NULL;
                    165:        }
                    166:        return &this->public;
                    167: }

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