Skip to content

Commit

Permalink
Merge branch 'master' into simplify_extension
Browse files Browse the repository at this point in the history
  • Loading branch information
ep69 committed Jul 2, 2018
2 parents 5a33fa6 + 028a2bd commit 23a469d
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 17 deletions.
3 changes: 3 additions & 0 deletions scripts/tls.py
Expand Up @@ -11,6 +11,7 @@
import os
import os.path
import socket
import struct
import time
import getopt
import binascii
Expand Down Expand Up @@ -484,6 +485,8 @@ def handshake(self, connection):
start = time.clock()
connection.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY,
1)
connection.setsockopt(socket.SOL_SOCKET, socket.SO_LINGER,
struct.pack('ii', 1, 1))
connection.handshakeServer(certChain=cert_chain,
privateKey=privateKey,
verifierDB=verifierDB,
Expand Down
4 changes: 2 additions & 2 deletions tlslite/tlsconnection.py
Expand Up @@ -2211,9 +2211,9 @@ def _serverTLS13Handshake(self, settings, clientHello, cipherSuite,
ext = SupportedGroupsExtension()
groups = [getattr(GroupName, i) for i in settings.keyShares]
groups += [getattr(GroupName, i) for i in settings.eccCurves
if i not in groups]
if getattr(GroupName, i) not in groups]
groups += [getattr(GroupName, i) for i in settings.dhGroups
if i not in groups]
if getattr(GroupName, i) not in groups]
if groups:
ext.create(groups)
ee_extensions.append(ext)
Expand Down
31 changes: 17 additions & 14 deletions tlslite/utils/cryptomath.py
Expand Up @@ -253,8 +253,7 @@ def numBytes(n):
# **************************************************************************

def getRandomNumber(low, high):
if low >= high:
raise AssertionError()
assert low < high
howManyBits = numBits(high)
howManyBytes = numBytes(high)
lastBits = howManyBits % 8
Expand Down Expand Up @@ -296,15 +295,8 @@ def powMod(base, power, modulus):
modulus = gmpy.mpz(modulus)
result = pow(base, power, modulus)
return compatLong(result)

else:
def powMod(base, power, modulus):
if power < 0:
result = pow(base, power*-1, modulus)
result = invMod(result, modulus)
return result
else:
return pow(base, power, modulus)
powMod = pow


def divceil(divident, divisor):
Expand Down Expand Up @@ -353,9 +345,15 @@ def isPrime(n, iterations=5, display=False, sieve=makeSieve(1000)):
a = getRandomNumber(2, n)
return True


def getRandomPrime(bits, display=False):
if bits < 10:
raise AssertionError()
"""
Generate a random prime number of a given size.
the number will be 'bits' bits long (i.e. generated number will be
larger than `(2^(bits-1) * 3 ) / 2` but smaller than 2^bits.
"""
assert bits >= 10
#The 1.5 ensures the 2 MSBs are set
#Thus, when used for p,q in RSA, n will have its MSB set
#
Expand All @@ -374,10 +372,15 @@ def getRandomPrime(bits, display=False):
if isPrime(p, display=display):
return p


#Unused at the moment...
def getRandomSafePrime(bits, display=False):
if bits < 10:
raise AssertionError()
"""Generate a random safe prime.
Will generate a prime `bits` bits long (see getRandomPrime) such that
the (p-1)/2 will also be prime.
"""
assert bits >= 10
#The 1.5 ensures the 2 MSBs are set
#Thus, when used for p,q in RSA, n will have its MSB set
#
Expand Down
26 changes: 25 additions & 1 deletion unit_tests/test_tlslite_utils_cryptomath.py
Expand Up @@ -16,7 +16,8 @@
from tlslite.utils.cryptomath import isPrime, numBits, numBytes, \
numberToByteArray, MD5, SHA1, secureHash, HMAC_MD5, HMAC_SHA1, \
HMAC_SHA256, HMAC_SHA384, HKDF_expand, bytesToNumber, \
HKDF_expand_label, derive_secret, numberToMPI, mpiToNumber
HKDF_expand_label, derive_secret, numberToMPI, mpiToNumber, \
getRandomPrime, getRandomSafePrime, powMod
from tlslite.handshakehashes import HandshakeHashes

class TestIsPrime(unittest.TestCase):
Expand Down Expand Up @@ -193,6 +194,16 @@ def test_numBits(self, number):
def test_numBytes(self, number):
self.assertEqual(numBytes(number), self.num_bytes(number))


class TestPowMod(unittest.TestCase):
def test_with_small_numbers(self):
self.assertEqual(2**10, powMod(2, 10, 10**6))

def test_with_mod(self):
self.assertEqual(4, powMod(3, 10, 5))
self.assertEqual(2, powMod(3, 11, 5))


class TestHMACMethods(unittest.TestCase):
def test_HMAC_MD5(self):
self.assertEqual(HMAC_MD5(b'abc', b'def'),
Expand Down Expand Up @@ -518,3 +529,16 @@ def test_fromMPI(self):
def test_fromMPI_with_negative_number(self):
with self.assertRaises(ValueError):
mpiToNumber(bytearray(b'\x00\x00\x00\x01\xc8'))


class TestPrimeGeneration(unittest.TestCase):
def test_getRangomPrime(self):
r = getRandomPrime(20)
self.assertEqual(numBits(r), 20)
self.assertTrue(isPrime(r))

def test_getRandomSafePrime(self):
r = getRandomSafePrime(20)
self.assertEqual(numBits(r), 20)
self.assertTrue(isPrime(r))
self.assertTrue(isPrime((r-1)//2))

0 comments on commit 23a469d

Please sign in to comment.