Return to botan_rng.c CVS log | Up to [ELWIX - Embedded LightWeight unIX -] / embedaddon / strongswan / src / libstrongswan / plugins / botan |
1.1 misho 1: /* 2: * Copyright (C) 2018 René Korthaus 3: * Rohde & Schwarz Cybersecurity GmbH 4: * 5: * Permission is hereby granted, free of charge, to any person obtaining a copy 6: * of this software and associated documentation files (the "Software"), to deal 7: * in the Software without restriction, including without limitation the rights 8: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9: * copies of the Software, and to permit persons to whom the Software is 10: * furnished to do so, subject to the following conditions: 11: * 12: * The above copyright notice and this permission notice shall be included in 13: * all copies or substantial portions of the Software. 14: * 15: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21: * THE SOFTWARE. 22: */ 23: 24: #include "botan_rng.h" 1.1.1.2 ! misho 25: #include "botan_util.h" 1.1 misho 26: 27: #include <botan/build.h> 28: 29: #ifdef BOTAN_HAS_HMAC_DRBG 30: 31: #include <botan/ffi.h> 32: 33: typedef struct private_botan_random_t private_botan_random_t; 34: 35: /** 36: * Private data of an botan_rng_t object. 37: */ 38: struct private_botan_random_t { 39: 40: /** 41: * Public botan_rnd_t interface. 42: */ 43: botan_random_t public; 44: 45: /** 46: * RNG quality of this instance 47: */ 48: rng_quality_t quality; 49: 50: /** 51: * RNG instance 52: */ 53: botan_rng_t rng; 54: }; 55: 56: METHOD(rng_t, get_bytes, bool, 57: private_botan_random_t *this, size_t bytes, uint8_t *buffer) 58: { 59: return botan_rng_get(this->rng, buffer, bytes) == 0; 60: } 61: 62: METHOD(rng_t, allocate_bytes, bool, 63: private_botan_random_t *this, size_t bytes, chunk_t *chunk) 64: { 65: *chunk = chunk_alloc(bytes); 66: if (!get_bytes(this, chunk->len, chunk->ptr)) 67: { 68: chunk_free(chunk); 69: return FALSE; 70: } 71: return TRUE; 72: } 73: 74: METHOD(rng_t, destroy, void, 75: private_botan_random_t *this) 76: { 77: botan_rng_destroy(this->rng); 78: free(this); 79: } 80: 81: /* 82: * Described in header 83: */ 84: botan_random_t *botan_rng_create(rng_quality_t quality) 85: { 86: private_botan_random_t *this; 1.1.1.2 ! misho 87: const char *rng_name; 1.1 misho 88: 1.1.1.2 ! misho 89: rng_name = botan_map_rng_quality(quality); ! 90: if (!rng_name) 1.1 misho 91: { 1.1.1.2 ! misho 92: return NULL; 1.1 misho 93: } 94: 95: INIT(this, 96: .public = { 97: .rng = { 98: .get_bytes = _get_bytes, 99: .allocate_bytes = _allocate_bytes, 100: .destroy = _destroy, 101: }, 102: }, 103: .quality = quality, 104: ); 105: 106: if (botan_rng_init(&this->rng, rng_name)) 107: { 108: free(this); 109: return NULL; 110: } 111: return &this->public; 112: } 113: 114: #endif