def generate_for_nist(self, size): '''Generate public key.''' if str(size) not in ['192', '224', '256', '384', '521']: raise ValueError, 'Can only use nist curves.' else: self.size = str(size) self.ec = nist(self.size) # secret integer s self.s = randint(10, self.ec.modulus) self.b = self.s * self.ec.base_point
def from_file(self, filename, key_type): ''' Read public or private key (depending on key_type) from file. The file is ascii and the first line contains the curve size; i) if key_type is private then it has the secret key s (base 10) in the second line, and ii) if key_type is public the other three lines contain the x, y and z coordinate of point B respectively (base 10). ''' f = open(filename, 'r') # read curve size line = f.readline().strip() if line not in ['192', '224', '256', '384', '521']: raise BadKeyFile, 'Cannot read curve.' else: self.size = line self.ec = nist(line) if key_type == 'public': coords = [] # coordinates for i in range(3): line = f.readline().strip() try: coords.append(int(line, 10)) except ValueError: raise BadKeyFile, 'Cannot read point coordinates.' self.b = self.ec.point(*coords) elif key_type == 'private': line = f.readline().strip() try: self.s = int(line, 10) self.b = self.s * self.ec.base_point except ValueError: raise BadKeyFile, 'Cannot read secret integer s.'
def setUp(self): self.ec = EllipticCurve(10, 5, 13) self.nist = nist(192)