Annotation of embedaddon/strongswan/src/libstrongswan/plugins/wolfssl/wolfssl_hasher.c, revision 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: #include "wolfssl_hasher.h"
! 25: #include "wolfssl_util.h"
! 26:
! 27: #include <wolfssl/wolfcrypt/hash.h>
! 28:
! 29: typedef struct private_wolfssl_hasher_t private_wolfssl_hasher_t;
! 30:
! 31: /**
! 32: * Private data of wolfssl_hasher_t
! 33: */
! 34: struct private_wolfssl_hasher_t {
! 35:
! 36: /**
! 37: * Public interface
! 38: */
! 39: wolfssl_hasher_t public;
! 40:
! 41: /**
! 42: * The hasher to use
! 43: */
! 44: wc_HashAlg hasher;
! 45:
! 46: /**
! 47: * The hash algorithm
! 48: */
! 49: enum wc_HashType type;
! 50: };
! 51:
! 52: METHOD(hasher_t, get_hash_size, size_t,
! 53: private_wolfssl_hasher_t *this)
! 54: {
! 55: return wc_HashGetDigestSize(this->type);
! 56: }
! 57:
! 58: METHOD(hasher_t, reset, bool,
! 59: private_wolfssl_hasher_t *this)
! 60: {
! 61: return wc_HashInit(&this->hasher, this->type) == 0;
! 62: }
! 63:
! 64: METHOD(hasher_t, get_hash, bool,
! 65: private_wolfssl_hasher_t *this, chunk_t chunk, uint8_t *hash)
! 66: {
! 67: int ret;
! 68:
! 69: ret = wc_HashUpdate(&this->hasher, this->type, chunk.ptr, chunk.len);
! 70: if (ret == 0 && hash)
! 71: {
! 72: ret = wc_HashFinal(&this->hasher, this->type, hash);
! 73: if (ret == 0)
! 74: {
! 75: return reset(this);
! 76: }
! 77: }
! 78: return ret == 0;
! 79: }
! 80:
! 81: METHOD(hasher_t, allocate_hash, bool,
! 82: private_wolfssl_hasher_t *this, chunk_t chunk, chunk_t *hash)
! 83: {
! 84: if (hash)
! 85: {
! 86: *hash = chunk_alloc(get_hash_size(this));
! 87: return get_hash(this, chunk, hash->ptr);
! 88: }
! 89: return get_hash(this, chunk, NULL);
! 90: }
! 91:
! 92: METHOD(hasher_t, destroy, void,
! 93: private_wolfssl_hasher_t *this)
! 94: {
! 95: wc_HashFree(&this->hasher, this->type);
! 96: free(this);
! 97: }
! 98:
! 99: /*
! 100: * Described in header
! 101: */
! 102: wolfssl_hasher_t *wolfssl_hasher_create(hash_algorithm_t algo)
! 103: {
! 104: private_wolfssl_hasher_t *this;
! 105: enum wc_HashType type;
! 106:
! 107: if (!wolfssl_hash2type(algo, &type))
! 108: {
! 109: return NULL;
! 110: }
! 111:
! 112: INIT(this,
! 113: .public = {
! 114: .hasher = {
! 115: .get_hash = _get_hash,
! 116: .allocate_hash = _allocate_hash,
! 117: .get_hash_size = _get_hash_size,
! 118: .reset = _reset,
! 119: .destroy = _destroy,
! 120: },
! 121: },
! 122: .type = type,
! 123: );
! 124:
! 125: /* initialization */
! 126: if (!reset(this))
! 127: {
! 128: destroy(this);
! 129: return NULL;
! 130: }
! 131: return &this->public;
! 132: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>