Annotation of embedaddon/strongswan/src/libstrongswan/plugins/openssl/openssl_sha1_prf.c, revision 1.1.1.1

1.1       misho       1: /*
                      2:  * Copyright (C) 2010 Martin Willi
                      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 <openssl/opensslconf.h>
                     17: 
                     18: #ifndef OPENSSL_NO_SHA1
                     19: 
                     20: #include "openssl_sha1_prf.h"
                     21: 
                     22: #include <openssl/sha.h>
                     23: #include <crypto/hashers/hasher.h>
                     24: 
                     25: typedef struct private_openssl_sha1_prf_t private_openssl_sha1_prf_t;
                     26: 
                     27: /**
                     28:  * Private data of an openssl_sha1_prf_t object.
                     29:  */
                     30: struct private_openssl_sha1_prf_t {
                     31: 
                     32:        /**
                     33:         * Public openssl_sha1_prf_t interface.
                     34:         */
                     35:        openssl_sha1_prf_t public;
                     36: 
                     37:        /**
                     38:         * SHA1 context
                     39:         */
                     40:        SHA_CTX ctx;
                     41: };
                     42: 
                     43: METHOD(prf_t, get_bytes, bool,
                     44:        private_openssl_sha1_prf_t *this, chunk_t seed, uint8_t *bytes)
                     45: {
                     46: #if OPENSSL_VERSION_NUMBER >= 0x10000000L
                     47:        if (!SHA1_Update(&this->ctx, seed.ptr, seed.len))
                     48:        {
                     49:                return FALSE;
                     50:        }
                     51: #else /* OPENSSL_VERSION_NUMBER < 1.0 */
                     52:        SHA1_Update(&this->ctx, seed.ptr, seed.len);
                     53: #endif
                     54: 
                     55:        if (bytes)
                     56:        {
                     57:                uint32_t *hash = (uint32_t*)bytes;
                     58: 
                     59:                hash[0] = htonl(this->ctx.h0);
                     60:                hash[1] = htonl(this->ctx.h1);
                     61:                hash[2] = htonl(this->ctx.h2);
                     62:                hash[3] = htonl(this->ctx.h3);
                     63:                hash[4] = htonl(this->ctx.h4);
                     64:        }
                     65: 
                     66:        return TRUE;
                     67: }
                     68: 
                     69: METHOD(prf_t, get_block_size, size_t,
                     70:        private_openssl_sha1_prf_t *this)
                     71: {
                     72:        return HASH_SIZE_SHA1;
                     73: }
                     74: 
                     75: METHOD(prf_t, allocate_bytes, bool,
                     76:        private_openssl_sha1_prf_t *this, chunk_t seed, chunk_t *chunk)
                     77: {
                     78:        if (chunk)
                     79:        {
                     80:                *chunk = chunk_alloc(HASH_SIZE_SHA1);
                     81:                return get_bytes(this, seed, chunk->ptr);
                     82:        }
                     83:        return get_bytes(this, seed, NULL);
                     84: }
                     85: 
                     86: METHOD(prf_t, get_key_size, size_t,
                     87:        private_openssl_sha1_prf_t *this)
                     88: {
                     89:        return HASH_SIZE_SHA1;
                     90: }
                     91: 
                     92: METHOD(prf_t, set_key, bool,
                     93:        private_openssl_sha1_prf_t *this, chunk_t key)
                     94: {
                     95: #if OPENSSL_VERSION_NUMBER >= 0x10000000L
                     96:        if (!SHA1_Init(&this->ctx))
                     97:        {
                     98:                return FALSE;
                     99:        }
                    100: #else /* OPENSSL_VERSION_NUMBER < 1.0 */
                    101:        SHA1_Init(&this->ctx);
                    102: #endif
                    103: 
                    104:        if (key.len % 4)
                    105:        {
                    106:                return FALSE;
                    107:        }
                    108:        if (key.len >= 4)
                    109:        {
                    110:                this->ctx.h0 ^= untoh32(key.ptr);
                    111:        }
                    112:        if (key.len >= 8)
                    113:        {
                    114:                this->ctx.h1 ^= untoh32(key.ptr + 4);
                    115:        }
                    116:        if (key.len >= 12)
                    117:        {
                    118:                this->ctx.h2 ^= untoh32(key.ptr + 8);
                    119:        }
                    120:        if (key.len >= 16)
                    121:        {
                    122:                this->ctx.h3 ^= untoh32(key.ptr + 12);
                    123:        }
                    124:        if (key.len >= 20)
                    125:        {
                    126:                this->ctx.h4 ^= untoh32(key.ptr + 16);
                    127:        }
                    128:        return TRUE;
                    129: }
                    130: 
                    131: METHOD(prf_t, destroy, void,
                    132:        private_openssl_sha1_prf_t *this)
                    133: {
                    134:        free(this);
                    135: }
                    136: 
                    137: /**
                    138:  * See header
                    139:  */
                    140: openssl_sha1_prf_t *openssl_sha1_prf_create(pseudo_random_function_t algo)
                    141: {
                    142:        private_openssl_sha1_prf_t *this;
                    143: 
                    144:        if (algo != PRF_KEYED_SHA1)
                    145:        {
                    146:                return NULL;
                    147:        }
                    148: 
                    149:        INIT(this,
                    150:                .public = {
                    151:                        .prf = {
                    152:                                .get_block_size = _get_block_size,
                    153:                                .get_bytes = _get_bytes,
                    154:                                .allocate_bytes = _allocate_bytes,
                    155:                                .get_key_size = _get_key_size,
                    156:                                .set_key = _set_key,
                    157:                                .destroy = _destroy,
                    158:                        },
                    159:                },
                    160:        );
                    161: 
                    162:        return &this->public;
                    163: }
                    164: 
                    165: #endif /* OPENSSL_NO_SHA1 */

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