Annotation of embedaddon/strongswan/src/libstrongswan/plugins/wolfssl/wolfssl_sha1_prf.c, revision 1.1.1.1

1.1       misho       1: /*
                      2:  * Copyright (C) 2019 Sean Parkinson, wolfSSL Inc.
                      3:  *
                      4:  * Permission is hereby granted, free of charge, to any person obtaining a copy
                      5:  * of this software and associated documentation files (the "Software"), to deal
                      6:  * in the Software without restriction, including without limitation the rights
                      7:  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
                      8:  * copies of the Software, and to permit persons to whom the Software is
                      9:  * furnished to do so, subject to the following conditions:
                     10:  *
                     11:  * The above copyright notice and this permission notice shall be included in
                     12:  * all copies or substantial portions of the Software.
                     13:  *
                     14:  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
                     15:  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
                     16:  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
                     17:  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
                     18:  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
                     19:  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
                     20:  * THE SOFTWARE.
                     21:  */
                     22: 
                     23: #include "wolfssl_common.h"
                     24: 
                     25: #ifndef NO_SHA
                     26: 
                     27: #include "wolfssl_sha1_prf.h"
                     28: 
                     29: #include <wolfssl/wolfcrypt/sha.h>
                     30: #include <crypto/hashers/hasher.h>
                     31: 
                     32: typedef struct private_wolfssl_sha1_prf_t private_wolfssl_sha1_prf_t;
                     33: 
                     34: /**
                     35:  * Private data of an wolfssl_sha1_prf_t object.
                     36:  */
                     37: struct private_wolfssl_sha1_prf_t {
                     38: 
                     39:        /**
                     40:         * Public wolfssl_sha1_prf_t interface
                     41:         */
                     42:        wolfssl_sha1_prf_t public;
                     43: 
                     44:        /**
                     45:         * SHA1 context
                     46:         */
                     47:        wc_Sha sha1;
                     48: };
                     49: 
                     50: METHOD(prf_t, get_bytes, bool,
                     51:        private_wolfssl_sha1_prf_t *this, chunk_t seed, uint8_t *bytes)
                     52: {
                     53:        if (wc_ShaUpdate(&this->sha1, seed.ptr, seed.len) != 0)
                     54:        {
                     55:                return FALSE;
                     56:        }
                     57: 
                     58:        if (bytes)
                     59:        {
                     60:                uint32_t *hash = (uint32_t*)bytes;
                     61: 
                     62:                hash[0] = htonl(this->sha1.digest[0]);
                     63:                hash[1] = htonl(this->sha1.digest[1]);
                     64:                hash[2] = htonl(this->sha1.digest[2]);
                     65:                hash[3] = htonl(this->sha1.digest[3]);
                     66:                hash[4] = htonl(this->sha1.digest[4]);
                     67:        }
                     68:        return TRUE;
                     69: }
                     70: 
                     71: METHOD(prf_t, get_block_size, size_t,
                     72:        private_wolfssl_sha1_prf_t *this)
                     73: {
                     74:        return HASH_SIZE_SHA1;
                     75: }
                     76: 
                     77: METHOD(prf_t, allocate_bytes, bool,
                     78:        private_wolfssl_sha1_prf_t *this, chunk_t seed, chunk_t *chunk)
                     79: {
                     80:        if (chunk)
                     81:        {
                     82:                *chunk = chunk_alloc(HASH_SIZE_SHA1);
                     83:                return get_bytes(this, seed, chunk->ptr);
                     84:        }
                     85:        return get_bytes(this, seed, NULL);
                     86: }
                     87: 
                     88: METHOD(prf_t, get_key_size, size_t,
                     89:        private_wolfssl_sha1_prf_t *this)
                     90: {
                     91:        return HASH_SIZE_SHA1;
                     92: }
                     93: 
                     94: METHOD(prf_t, set_key, bool,
                     95:        private_wolfssl_sha1_prf_t *this, chunk_t key)
                     96: {
                     97:        if (wc_InitSha(&this->sha1) != 0)
                     98:        {
                     99:                return FALSE;
                    100:        }
                    101: 
                    102:        if (key.len % 4)
                    103:        {
                    104:                return FALSE;
                    105:        }
                    106:        if (key.len >= 4)
                    107:        {
                    108:                this->sha1.digest[0] ^= untoh32(key.ptr);
                    109:        }
                    110:        if (key.len >= 8)
                    111:        {
                    112:                this->sha1.digest[1] ^= untoh32(key.ptr + 4);
                    113:        }
                    114:        if (key.len >= 12)
                    115:        {
                    116:                this->sha1.digest[2] ^= untoh32(key.ptr + 8);
                    117:        }
                    118:        if (key.len >= 16)
                    119:        {
                    120:                this->sha1.digest[3] ^= untoh32(key.ptr + 12);
                    121:        }
                    122:        if (key.len >= 20)
                    123:        {
                    124:                this->sha1.digest[4] ^= untoh32(key.ptr + 16);
                    125:        }
                    126:        return TRUE;
                    127: }
                    128: 
                    129: METHOD(prf_t, destroy, void,
                    130:        private_wolfssl_sha1_prf_t *this)
                    131: {
                    132:        wc_ShaFree(&this->sha1);
                    133:        free(this);
                    134: }
                    135: 
                    136: /*
                    137:  * Described in header
                    138:  */
                    139: wolfssl_sha1_prf_t *wolfssl_sha1_prf_create(pseudo_random_function_t algo)
                    140: {
                    141:        private_wolfssl_sha1_prf_t *this;
                    142: 
                    143:        INIT(this,
                    144:                .public = {
                    145:                        .prf = {
                    146:                                .get_block_size = _get_block_size,
                    147:                                .get_bytes = _get_bytes,
                    148:                                .allocate_bytes = _allocate_bytes,
                    149:                                .get_key_size = _get_key_size,
                    150:                                .set_key = _set_key,
                    151:                                .destroy = _destroy,
                    152:                        },
                    153:                },
                    154:        );
                    155: 
                    156:        if (wc_InitSha(&this->sha1) != 0)
                    157:        {
                    158:                free(this);
                    159:                return NULL;
                    160:        }
                    161:        return &this->public;
                    162: }
                    163: 
                    164: #endif /* NO_SHA */

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