|
| 1 | +from unittest.case import TestCase |
| 2 | +from ellipticcurve import curve, PublicKey, Signature, Ecdsa, PrivateKey |
| 3 | + |
| 4 | + |
| 5 | +class CurveTest(TestCase): |
| 6 | + |
| 7 | + def testSupportedCurve(self): |
| 8 | + newCurve = curve.CurveFp( |
| 9 | + name="secp256k1", |
| 10 | + A=0x0000000000000000000000000000000000000000000000000000000000000000, |
| 11 | + B=0x0000000000000000000000000000000000000000000000000000000000000007, |
| 12 | + P=0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2f, |
| 13 | + N=0xfffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141, |
| 14 | + Gx=0x79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798, |
| 15 | + Gy=0x483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8, |
| 16 | + oid=[1, 3, 132, 0, 10] |
| 17 | + ) |
| 18 | + privateKey1 = PrivateKey(curve=newCurve) |
| 19 | + publicKey1 = privateKey1.publicKey() |
| 20 | + |
| 21 | + privateKeyPem = privateKey1.toPem() |
| 22 | + publicKeyPem = publicKey1.toPem() |
| 23 | + |
| 24 | + privateKey2 = PrivateKey.fromPem(privateKeyPem) |
| 25 | + publicKey2 = PublicKey.fromPem(publicKeyPem) |
| 26 | + |
| 27 | + message = "test" |
| 28 | + |
| 29 | + signatureBase64 = Ecdsa.sign(message=message, privateKey=privateKey2).toBase64() |
| 30 | + signature = Signature.fromBase64(signatureBase64) |
| 31 | + |
| 32 | + self.assertTrue(Ecdsa.verify(message=message, signature=signature, publicKey=publicKey2)) |
| 33 | + |
| 34 | + def testAddNewCurve(self): |
| 35 | + newCurve = curve.CurveFp( |
| 36 | + name="frp256v1", |
| 37 | + A=0xf1fd178c0b3ad58f10126de8ce42435b3961adbcabc8ca6de8fcf353d86e9c00, |
| 38 | + B=0xee353fca5428a9300d4aba754a44c00fdfec0c9ae4b1a1803075ed967b7bb73f, |
| 39 | + P=0xf1fd178c0b3ad58f10126de8ce42435b3961adbcabc8ca6de8fcf353d86e9c03, |
| 40 | + N=0xf1fd178c0b3ad58f10126de8ce42435b53dc67e140d2bf941ffdd459c6d655e1, |
| 41 | + Gx=0xb6b3d4c356c139eb31183d4749d423958c27d2dcaf98b70164c97a2dd98f5cff, |
| 42 | + Gy=0x6142e0f7c8b204911f9271f0f3ecef8c2701c307e8e4c9e183115a1554062cfb, |
| 43 | + oid=[1, 2, 250, 1, 223, 101, 256, 1] |
| 44 | + ) |
| 45 | + curve.add(newCurve) |
| 46 | + privateKey1 = PrivateKey(curve=newCurve) |
| 47 | + publicKey1 = privateKey1.publicKey() |
| 48 | + |
| 49 | + privateKeyPem = privateKey1.toPem() |
| 50 | + publicKeyPem = publicKey1.toPem() |
| 51 | + |
| 52 | + privateKey2 = PrivateKey.fromPem(privateKeyPem) |
| 53 | + publicKey2 = PublicKey.fromPem(publicKeyPem) |
| 54 | + |
| 55 | + message = "test" |
| 56 | + |
| 57 | + signatureBase64 = Ecdsa.sign(message=message, privateKey=privateKey2).toBase64() |
| 58 | + signature = Signature.fromBase64(signatureBase64) |
| 59 | + |
| 60 | + self.assertTrue(Ecdsa.verify(message=message, signature=signature, publicKey=publicKey2)) |
| 61 | + |
| 62 | + def testUnsupportedCurve(self): |
| 63 | + newCurve = curve.CurveFp( |
| 64 | + name="frp256v1", |
| 65 | + A=0xf1fd178c0b3ad58f10126de8ce42435b3961adbcabc8ca6de8fcf353d86e9c00, |
| 66 | + B=0xee353fca5428a9300d4aba754a44c00fdfec0c9ae4b1a1803075ed967b7bb73f, |
| 67 | + P=0xf1fd178c0b3ad58f10126de8ce42435b3961adbcabc8ca6de8fcf353d86e9c03, |
| 68 | + N=0xf1fd178c0b3ad58f10126de8ce42435b53dc67e140d2bf941ffdd459c6d655e1, |
| 69 | + Gx=0xb6b3d4c356c139eb31183d4749d423958c27d2dcaf98b70164c97a2dd98f5cff, |
| 70 | + Gy=0x6142e0f7c8b204911f9271f0f3ecef8c2701c307e8e4c9e183115a1554062cfb, |
| 71 | + oid=[1, 2, 250, 1, 223, 101, 256, 1] |
| 72 | + ) |
| 73 | + |
| 74 | + privateKeyPem = PrivateKey(curve=newCurve).toPem() |
| 75 | + publicKeyPem = PrivateKey(curve=newCurve).publicKey().toPem() |
| 76 | + |
| 77 | + with self.assertRaises(Exception) as context: |
| 78 | + privateKey = PrivateKey.fromPem(privateKeyPem) |
| 79 | + self.assertTrue('Unknown curve' in str(context.exception)) |
| 80 | + |
| 81 | + with self.assertRaises(Exception) as context: |
| 82 | + publicKey = PublicKey.fromPem(publicKeyPem) |
| 83 | + self.assertTrue('Unknown curve' in str(context.exception)) |
| 84 | + |
| 85 | + |
0 commit comments