Added support for SHA3

pull/11/head
Daniele Lacamera 2020-01-07 20:02:50 +01:00
parent c57c4aeba9
commit 0b4cbfcb4d
4 changed files with 99 additions and 3 deletions

View File

@ -47,11 +47,12 @@ else:
"using USE_LOCAL_WOLFSSL\n")
# default values
MPAPI_ENABLED = 0
MPAPI_ENABLED = 1
SHA_ENABLED = 1
SHA256_ENABLED = 1
SHA384_ENABLED = 1
SHA512_ENABLED = 1
SHA3_ENABLED = 1
DES3_ENABLED = 1
AES_ENABLED = 1
HMAC_ENABLED = 1
@ -87,6 +88,11 @@ if featureDetection == 1:
else:
SHA512_ENABLED = 0
if '#define WOLFSSL_SHA3' in optionsHeaderStr:
SHA3_ENABLED = 1
else:
SHA3_ENABLED = 0
if '#define NO_DES3' in optionsHeaderStr:
DES3_ENABLED = 0
else:
@ -134,6 +140,7 @@ ffibuilder.set_source(
#include <wolfssl/wolfcrypt/sha.h>
#include <wolfssl/wolfcrypt/sha256.h>
#include <wolfssl/wolfcrypt/sha512.h>
#include <wolfssl/wolfcrypt/sha3.h>
#include <wolfssl/wolfcrypt/hmac.h>
@ -153,6 +160,7 @@ ffibuilder.set_source(
int SHA256_ENABLED = """ + str(SHA256_ENABLED) + """;
int SHA384_ENABLED = """ + str(SHA384_ENABLED) + """;
int SHA512_ENABLED = """ + str(SHA512_ENABLED) + """;
int SHA3_ENABLED = """ + str(SHA3_ENABLED) + """;
int DES3_ENABLED = """ + str(DES3_ENABLED) + """;
int AES_ENABLED = """ + str(AES_ENABLED) + """;
int HMAC_ENABLED = """ + str(HMAC_ENABLED) + """;
@ -173,6 +181,7 @@ _cdef = """
int SHA256_ENABLED;
int SHA384_ENABLED;
int SHA512_ENABLED;
int SHA3_ENABLED;
int DES3_ENABLED;
int AES_ENABLED;
int HMAC_ENABLED;
@ -234,6 +243,22 @@ if (SHA512_ENABLED == 1):
int wc_Sha512Update(wc_Sha512*, const byte*, word32);
int wc_Sha512Final(wc_Sha512*, byte*);
"""
if (SHA3_ENABLED == 1):
_cdef += """
typedef struct { ...; } wc_Sha3;
int wc_InitSha3_224(wc_Sha3*, void *, int);
int wc_InitSha3_256(wc_Sha3*, void *, int);
int wc_InitSha3_384(wc_Sha3*, void *, int);
int wc_InitSha3_512(wc_Sha3*, void *, int);
int wc_Sha3_224_Update(wc_Sha3*, const byte*, word32);
int wc_Sha3_256_Update(wc_Sha3*, const byte*, word32);
int wc_Sha3_384_Update(wc_Sha3*, const byte*, word32);
int wc_Sha3_512_Update(wc_Sha3*, const byte*, word32);
int wc_Sha3_224_Final(wc_Sha3*, byte*);
int wc_Sha3_256_Final(wc_Sha3*, byte*);
int wc_Sha3_384_Final(wc_Sha3*, byte*);
int wc_Sha3_512_Final(wc_Sha3*, byte*);
"""
if (DES3_ENABLED == 1):
_cdef += """

View File

@ -153,11 +153,11 @@ def make_flags(prefix):
flags.append("--enable-sha")
flags.append("--enable-sha384")
flags.append("--enable-sha512")
flags.append("--enable-sha3")
flags.append("--enable-hkdf")
flags.append("--disable-md5")
flags.append("--disable-sha224")
flags.append("--disable-sha3")
flags.append("--disable-poly1305")
# asymmetric ciphers

View File

@ -192,6 +192,67 @@ if _lib.SHA512_ENABLED:
def _final(self, obj, ret):
return _lib.wc_Sha512Final(obj, ret)
if _lib.SHA3_ENABLED:
class Sha3(_Hash):
"""
**SHA3 ** is a cryptographic hash function family
standardized by **NIST**.
It produces from [ **224-bit | 28 bytes** ] up to [ **512-bit | 64 bytes] message digests.
Using SHA3-384 by default, unless a different digest size is passed through __init__.
"""
_native_type = "wc_Sha3 *"
_native_size = _ffi.sizeof("wc_Sha3")
def __init__(self): # pylint: disable=W0231
self._native_object = _ffi.new(self._native_type)
self.digest_size = 48
ret = self._init()
if ret < 0: # pragma: no cover
raise WolfCryptError("Sha3 init error (%d)" % ret)
def __init__(self, string, size=48): # pylint: disable=W0231
self._native_object = _ffi.new(self._native_type)
self.digest_size = size
ret = self._init()
if ret < 0: # pragma: no cover
raise WolfCryptError("Sha3 init error (%d)" % ret)
if string:
self.update(string)
def _init(self):
if (self.digest_size != 28 and
self.digest_size != 32 and
self.digest_size != 48 and
self.digest_size != 64):
return -1
if self.digest_size == 28:
return _lib.wc_InitSha3_224(self._native_object, _ffi.NULL, 0)
if self.digest_size == 32:
return _lib.wc_InitSha3_256(self._native_object, _ffi.NULL, 0)
if self.digest_size == 48:
return _lib.wc_InitSha3_384(self._native_object, _ffi.NULL, 0)
if self.digest_size == 64:
return _lib.wc_InitSha3_512(self._native_object, _ffi.NULL, 0)
def _update(self, data):
if self.digest_size == 28:
return _lib.wc_Sha3_224_Update(self._native_object, data, len(data))
if self.digest_size == 32:
return _lib.wc_Sha3_256_Update(self._native_object, data, len(data))
if self.digest_size == 48:
return _lib.wc_Sha3_384_Update(self._native_object, data, len(data))
if self.digest_size == 64:
return _lib.wc_Sha3_512_Update(self._native_object, data, len(data))
def _final(self, obj, ret):
if self.digest_size == 28:
return _lib.wc_Sha3_224_Final(obj, ret)
if self.digest_size == 32:
return _lib.wc_Sha3_256_Final(obj, ret)
if self.digest_size == 48:
return _lib.wc_Sha3_384_Final(obj, ret)
if self.digest_size == 64:
return _lib.wc_Sha3_512_Final(obj, ret)
# Hmac types

View File

@ -38,6 +38,9 @@ if _lib.SHA384_ENABLED:
if _lib.SHA512_ENABLED:
from wolfcrypt.hashes import Sha512
if _lib.SHA3_ENABLED:
from wolfcrypt.hashes import Sha3
if _lib.HMAC_ENABLED:
if _lib.SHA_ENABLED:
from wolfcrypt.hashes import HmacSha
@ -81,7 +84,12 @@ def vectors():
"e8fff55e644ee8a106aae19c07f91b3f" +
"2a2a6d40dfa7302c0fa6a1a9a5bfa03f")
)
if _lib.SHA3_ENABLED:
vectorArray[Sha3]=TestVector(
digest=t2b("6170dedf06f83c3305ec18b7558384a5" +
"a62d86e42c143d416aaec32f971986c1" +
"e84edf61df308cc6d8c310d1956e1908")
)
if _lib.HMAC_ENABLED:
if _lib.SHA_ENABLED:
vectorArray[HmacSha]=TestVector(
@ -117,6 +125,8 @@ if _lib.SHA384_ENABLED:
hash_params.append(Sha384)
if _lib.SHA512_ENABLED:
hash_params.append(Sha512)
if _lib.SHA3_ENABLED:
hash_params.append(Sha3)
hmac_params = []
if _lib.HMAC_ENABLED: