Annotation of embedaddon/strongswan/src/libcharon/tests/utils/mock_nonce_gen.c, revision 1.1
1.1 ! misho 1: /*
! 2: * Copyright (C) 2016 Tobias Brunner
! 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 "mock_nonce_gen.h"
! 17:
! 18: typedef struct private_nonce_gen_t private_nonce_gen_t;
! 19:
! 20: struct private_nonce_gen_t {
! 21:
! 22: /**
! 23: * Public interface
! 24: */
! 25: nonce_gen_t public;
! 26:
! 27: /**
! 28: * Random number generator
! 29: */
! 30: rng_t* rng;
! 31:
! 32: /**
! 33: * First byte to set to the nonces
! 34: */
! 35: u_char first;
! 36: };
! 37:
! 38: METHOD(nonce_gen_t, get_nonce, bool,
! 39: private_nonce_gen_t *this, size_t size, uint8_t *buffer)
! 40: {
! 41: if (size > 0)
! 42: {
! 43: buffer[0] = this->first;
! 44: buffer++;
! 45: size--;
! 46: }
! 47: return this->rng->get_bytes(this->rng, size, buffer);
! 48: }
! 49:
! 50: METHOD(nonce_gen_t, allocate_nonce, bool,
! 51: private_nonce_gen_t *this, size_t size, chunk_t *chunk)
! 52: {
! 53: *chunk = chunk_alloc(size);
! 54: if (!get_nonce(this, chunk->len, chunk->ptr))
! 55: {
! 56: chunk_free(chunk);
! 57: return FALSE;
! 58: }
! 59: return TRUE;
! 60: }
! 61:
! 62: METHOD(nonce_gen_t, destroy, void,
! 63: private_nonce_gen_t *this)
! 64: {
! 65: DESTROY_IF(this->rng);
! 66: free(this);
! 67: }
! 68:
! 69: /*
! 70: * Described in header
! 71: */
! 72: nonce_gen_t *mock_nonce_gen_create(u_char first)
! 73: {
! 74: private_nonce_gen_t *this;
! 75:
! 76: INIT(this,
! 77: .public = {
! 78: .get_nonce = _get_nonce,
! 79: .allocate_nonce = _allocate_nonce,
! 80: .destroy = _destroy,
! 81: },
! 82: .rng = lib->crypto->create_rng(lib->crypto, RNG_WEAK),
! 83: .first = first,
! 84: );
! 85: if (!this->rng)
! 86: {
! 87: destroy(this);
! 88: return NULL;
! 89: }
! 90: return &this->public;
! 91: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>