Ejemplo n.º 1
0
 def from_public_point(klass, point, curve=NIST192p, hashfunc=sha1):
     self = klass(_error__please_use_generate=True)
     self.curve = curve
     self.default_hashfunc = hashfunc
     self.pubkey = ecdsa.Public_key(curve.generator, point)
     self.pubkey.order = curve.order
     return self
Ejemplo n.º 2
0
 def from_secret_exponent(klass, secexp, curve=NIST192p):
     self = klass()
     self.curve = curve
     self.baselen = curve.baselen
     n = curve.order
     assert 1 <= secexp < n
     pubkey_point = curve.generator*secexp
     pubkey = ecdsa.Public_key(curve.generator, pubkey_point)
     pubkey.order = n
     self.verifying_key = VerifyingKey.from_public_point(pubkey_point, curve)
     self.privkey = ecdsa.Private_key(pubkey, secexp)
     self.privkey.order = n
     return self
Ejemplo n.º 3
0
 def from_secret_exponent(klass, secexp, curve=NIST192p, hashfunc=sha1):
     self = klass(_error__please_use_generate=True)
     self.curve = curve
     self.default_hashfunc = hashfunc
     self.baselen = curve.baselen
     n = curve.order
     assert 1 <= secexp < n
     pubkey_point = curve.generator * secexp
     pubkey = ecdsa.Public_key(curve.generator, pubkey_point)
     pubkey.order = n
     self.verifying_key = VerifyingKey.from_public_point(
         pubkey_point, curve, hashfunc)
     self.privkey = ecdsa.Private_key(pubkey, secexp)
     self.privkey.order = n
     return self
Ejemplo n.º 4
0
 def check(self, image, offset):
     x = binascii.hexlify(self.x)
     y = binascii.hexlify(self.y)
     pubkey = ecdsa.Point(ecdsa.curve_256, long(x, 16), long(y, 16))
     pubkey = ecdsa.Public_key(ecdsa.g, pubkey)
     h = hashlib.sha256(image[:offset+96]).digest()
     hh = long(binascii.hexlify(self.h), 16)
     signature = ecdsa.Signature(long(binascii.hexlify(self.r), 16),
                                 long(binascii.hexlify(self.s), 16))
     if (x, y) == devkey:
         keyname = "Mycelium development key"
     elif (x, y) == mainkey:
         keyname = "Mycelium firmware signing key"
     else:
         keyname = "unknown key"
     return h == self.h, pubkey.verifies(hh, signature), keyname
Ejemplo n.º 5
0
 def from_public_point(klass, point, curve=NIST192p):
     self = klass()
     self.curve = curve
     self.pubkey = ecdsa.Public_key(curve.generator, point)
     self.pubkey.order = curve.order
     return self
Ejemplo n.º 6
0
        meta = meta[:k2] + correct_sha[0x10:] + ('\x00' * 0xC) + meta[k2 +
                                                                      0x10:]
    else:
        print "Section already has correct hash"

# reinsert decrypted metadata into file
f = f[:meta_offset + 0x20] + meta_keys + meta_header + meta + f[meta_offset +
                                                                meta_len:]

# self signature is now invalid, so we need to fix it
print "Fixing signature"
sig_len = struct.unpack('>Q', meta_header[0x0:0x8])[0]
hash = int(hashlib.sha1(f[:sig_len]).hexdigest(), 16)
curve = ellipticcurve.CurveFp(p, a, b)
g = ellipticcurve.Point(curve, Gx, Gy, N)
pubkey = ecdsa.Public_key(g, g * priv)
privkey = ecdsa.Private_key(pubkey, priv)
sig = privkey.sign(hash, randrange(1, N))
meta = meta[:sig_len - meta_offset - 0x80] + '\x00' + pack(
    sig.r, 160) + '\x00' + pack(sig.s,
                                160) + meta[sig_len - meta_offset - 0x80 + 42:]

# reencrypt metadata and reinsert into self
print "Re-encrypting and inserting metadata"
meta, crap = aes128ctr(meta, meta_key, meta_iv)
f = f[:meta_offset +
      0x20] + meta_keys_enc + meta_header_enc + meta + f[meta_offset +
                                                         meta_len:]

# dump output to file
out = open(out_filename, 'wb')
Ejemplo n.º 7
0
 def __init__(self, privkey):
     self.pubkey = ecdsa.Public_key(ecdsa.g, ecdsa.g * privkey)
     self.privkey = ecdsa.Private_key(self.pubkey, privkey)