aboutsummaryrefslogtreecommitdiff
path: root/pyecsca/ec/signature.py
diff options
context:
space:
mode:
authorJ08nY2019-08-19 23:33:30 +0200
committerJ08nY2019-08-19 23:33:30 +0200
commit5ed98234f195643f94065e45a8d5d2554728b1e4 (patch)
treedef78887fa6d76c7e1f2214ed0be971abeaffb60 /pyecsca/ec/signature.py
parent512b5d2b5ae3350d1fc52fdd90d193bace00eed7 (diff)
downloadpyecsca-5ed98234f195643f94065e45a8d5d2554728b1e4.tar.gz
pyecsca-5ed98234f195643f94065e45a8d5d2554728b1e4.tar.zst
pyecsca-5ed98234f195643f94065e45a8d5d2554728b1e4.zip
Diffstat (limited to 'pyecsca/ec/signature.py')
-rw-r--r--pyecsca/ec/signature.py11
1 files changed, 8 insertions, 3 deletions
diff --git a/pyecsca/ec/signature.py b/pyecsca/ec/signature.py
index 4d1e43b..8318c45 100644
--- a/pyecsca/ec/signature.py
+++ b/pyecsca/ec/signature.py
@@ -91,11 +91,13 @@ class Signature(object):
return Mod(nonce, self.mult.group.order)
def _do_sign(self, nonce: Mod, digest: bytes) -> SignatureResult:
+ z = int.from_bytes(digest, byteorder="big")
+ if z.bit_length() > self.mult.group.order.bit_length():
+ z >>= z.bit_length() - self.mult.group.order.bit_length()
point = self.mult.multiply(int(nonce), self.mult.group.generator)
affine_point = point.to_affine() #  TODO: add to context
r = Mod(int(affine_point.x), self.mult.group.order)
- s = nonce.inverse() * (Mod(int.from_bytes(digest, byteorder="big"),
- self.mult.group.order) + r * self.privkey)
+ s = nonce.inverse() * (Mod(z, self.mult.group.order) + r * self.privkey)
return SignatureResult(int(r), int(s), digest=digest, nonce=int(nonce),
privkey=self.privkey)
@@ -118,8 +120,11 @@ class Signature(object):
return self._do_sign(k, digest)
def _do_verify(self, signature: SignatureResult, digest: bytes) -> bool:
+ z = int.from_bytes(digest, byteorder="big")
+ if z.bit_length() > self.mult.group.order.bit_length():
+ z >>= z.bit_length() - self.mult.group.order.bit_length()
c = Mod(signature.s, self.mult.group.order).inverse()
- u1 = Mod(int.from_bytes(digest, byteorder="big"), self.mult.group.order) * c
+ u1 = Mod(z, self.mult.group.order) * c
u2 = Mod(signature.r, self.mult.group.order) * c
p1 = self.mult.multiply(int(u1), self.mult.group.generator)
p2 = self.mult.multiply(int(u2), self.pubkey)