[Nym3-commit] r105 - trunk

nym3-devel@lists.noreply.org nym3-devel@lists.noreply.org
Wed, 18 Aug 2004 13:46:42 +0200


Author: jr
Date: 2004-08-18 13:46:40 +0200 (Wed, 18 Aug 2004)
New Revision: 105

Modified:
   trunk/Message.py
   trunk/User.py
Log:
starting the crypto
- change the way we represent keys 
  str -> long int in Newpk (Message.py)
  str -> mixminion.Crypto.RSA objects
- modify class Newpk to take these change into account


Modified: trunk/Message.py
===================================================================
--- trunk/Message.py	2004-08-06 12:00:17 UTC (rev 104)
+++ trunk/Message.py	2004-08-18 11:46:40 UTC (rev 105)
@@ -37,6 +37,16 @@
 			return aux(n / 256, chr(n % 256) + ac, mod - 1)
 	return aux(n, "", mod)
 
+def nbBits(n):
+	"""return the number of bits of a strictly positive integer,
+	0 otherwise"""
+	res = 0
+	p = 1
+	while(n >= p):
+		p = 2 * p
+		res = res + 1
+	return res
+
 def isMidList(l,n):
 	"""true if l is a list of strings of length n"""
 	def isMid(e):
@@ -384,8 +394,11 @@
 	
 class Newpk(CommandCToS):
 	"""Newpk command
-	self.kid : identity key RSA ASN-1 encoded (str)
-	self.kenc : encryption key RSA ASN-1 encoded (str)"""
+	self.kid : identity key RSA modulus BE encoded (str)
+	self.kenc : encryption key RSA modulus BE  encoded (str)
+	self.idf : number of bits of kid is 1024*idf
+	self.encf : number of bits of kid is 1024*encf"""
+	
 	def __init__(self):
 		"""Build a Newpk empty object"""
 		pass
@@ -395,11 +408,13 @@
 		return 3
 
 	def fromData(self, sid, senc):
-		"""Fill a Newpk Object from 2 strings containing an ASN-1 encoded RSA key"""
-		id_l = len(sid)
-		id_l2 = len(senc)
-		if(((id_l != 128) and (id_l != 256)) or ((id_l2 != 128) and (id_l2 != 256))):
+		"""Fill a Newpk Object from 2 modulus (int)"""
+		id_l = nbBits(sid)
+		id_l2 = nbBits(senc)
+		if( ((id_l != 1024) and (id_l != 2048)) or ( (id_l2 != 1024) and (id_l2 != 2048))):
 			raise BadArgument("Newpk.fromData : sid or senc as not a valid size")
+		self.idf = id_l / 1024
+		self.encf = id_l2 / 1024
 		self.kid = sid
 		self.kenc = senc
 		#command body is small enough
@@ -414,17 +429,22 @@
 			id_l2 = cs-2 - id_l
 			if((id_l2 != 128) and (id_l2 != 256)):
 				raise ParseError("Bad Formed Command : Newpk")
-			self.kid = sr.next(id_l)
-			self.kenc = sr.next(id_l2)
+
+			self.kid = strToIntBE(sr.next(id_l))
+			self.kenc = strToIntBE(sr.next(id_l2))
+			self.idf = id_l / 128
+			self.encf = id_l2 / 128
 		except IndexError:
 			raise ParseError("Bad Formed Command : Newpk")
 
 	def __str__(self):
 		"""Give the string of the Command that would be sent in a control message"""
 		#TODO check assuming len(kid)=128 or 256
-		s = intToStrBE(len(self.kid), 2) + self.kid + self.kenc
-		assert len(s) < pow(256, 3)
-		return chr(3) + intToStrBE(len(s), 3) + s
+
+		s=intToStrBE(128 * self.idf, 2) + intToStrBE(self.kid, 128*self.idf) + intToStrBE(self.kenc, 128*self.encf)
+		if(len(s)>=pow(256,3)): #should not be possible, otherwise bug
+			raise BadArgument("Newpk.__str__ : command body too long")
+		return chr(3)+intToStrBE(len(s),3)+s
 	
 class Relay(CommandCToS):
 	"""Relay command
@@ -973,7 +993,7 @@
 	assert s1 == s2
 	print "test Newpk"
 	C = Newpk()
-	C.fromData("1" * 256, "2" * 128)
+	C.fromData(166043335247949820064815071273366034639989291753710681801054469261751398297628983820646212658964492977698778059504084070552574143058521756622243987832034798616497725850572149696353832190479836330444809381945631021624844072571174471185526330192208148191737102653794442385041774144222636560699168166510090606307L,166043335247949820064815071273366034639989291753710681801054469261751398297628983820646212658964492977698778059504084070552574143058521756622243987832034798616497725850572149696353832190479836330444809381945631021624844072571174471185526330192208148191737102653794442385041774144222636560699168166510090606307L)
 	s1 = str(C)
 	S = StrReader(s1)
 	D = S.readCommandCToS()

Modified: trunk/User.py
===================================================================
--- trunk/User.py	2004-08-06 12:00:17 UTC (rev 104)
+++ trunk/User.py	2004-08-18 11:46:40 UTC (rev 105)
@@ -9,9 +9,11 @@
 import time
 import mixminion.Common
 from Message import intToStrBE
+from mixminion.Crypto import pk_from_modulus
 
 surb_len = Common.surbLength
 lifeCycle = Common.lifeCycle
+
 class NoSuchUser(Exception): pass
 
 class AlreadySuchUser(Exception): pass
@@ -323,8 +325,9 @@
 	    else: return
 
     def setKeys(self, kid, kenc):
-	self.data['idKey'] = kid
-	self.data['encKey'] = kenc
+        """given 2 modulus (int) set the public keys of the user"""
+	self.data['idKey'] = pk_from_modulus(kid)
+	self.data['encKey'] = pk_from_modulus(kenc)
 
     def isInitialized(self):
 	"""True if the first 3 commands :