adds RNG wrappers

gh-pages
Moisés Guimarães 2016-04-14 21:32:39 -03:00
parent efa336be2b
commit 691c59bd64
3 changed files with 104 additions and 1 deletions

View File

@ -28,6 +28,7 @@ setup(
install_requires=["cffi>=1.5.2"], install_requires=["cffi>=1.5.2"],
cffi_modules=[ cffi_modules=[
"./wolfcrypt/build_hashes.py:ffi", "./wolfcrypt/build_hashes.py:ffi",
"./wolfcrypt/build_ciphers.py:ffi" "./wolfcrypt/build_ciphers.py:ffi",
"./wolfcrypt/build_random.py:ffi"
], ],
) )

View File

@ -0,0 +1,53 @@
# build_random.py
#
# Copyright (C) 2006-2016 wolfSSL Inc.
#
# This file is part of wolfSSL. (formerly known as CyaSSL)
#
# wolfSSL is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# wolfSSL is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
import os
from cffi import FFI
ffi = FFI()
ffi.set_source("wolfcrypt._random",
"""
#include <wolfssl/options.h>
#include <wolfssl/wolfcrypt/random.h>
""",
include_dirs=["/usr/local/include"],
library_dirs=["/usr/local/lib"],
libraries=["wolfssl"],
)
ffi.cdef(
"""
typedef unsigned char byte;
typedef unsigned int word32;
typedef struct { ...; } WC_RNG;
int wc_InitRng(WC_RNG*);
int wc_RNG_GenerateBlock(WC_RNG*, byte*, word32);
int wc_RNG_GenerateByte(WC_RNG*, byte*);
int wc_FreeRng(WC_RNG*);
"""
)
if __name__ == "__main__":
ffi.compile(verbose=1)

View File

@ -0,0 +1,49 @@
# random.py
#
# Copyright (C) 2006-2016 wolfSSL Inc.
#
# This file is part of wolfSSL. (formerly known as CyaSSL)
#
# wolfSSL is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# wolfSSL is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
from wolfcrypt._random import ffi
from wolfcrypt._random import lib
class Random(object):
def __init__(self):
self.native_object = ffi.new("WC_RNG *")
if lib.wc_InitRng(self.native_object) != 0:
self.native_object = None
def __del__(self):
if self.native_object:
lib.wc_FreeRng(self.native_object)
def byte(self):
ret = "\0"
lib.wc_RNG_GenerateByte(self.native_object, ret)
return ret
def bytes(self, length):
ret = "\0" * length
lib.wc_RNG_GenerateBlock(self.native_object, ret, length)
return ret