mirror of
https://gitea.osmocom.org/sim-card/pysim.git
synced 2026-03-27 07:48:33 +03:00
euicc: Implement EID checksum verification + computation
Change-Id: I2cb342783137ee7e4b1be3b14e9c3747316f1995
This commit is contained in:
@@ -32,6 +32,36 @@ from pySim.filesystem import CardADF, CardApplication
|
|||||||
from pySim.utils import Hexstr, SwHexstr
|
from pySim.utils import Hexstr, SwHexstr
|
||||||
import pySim.global_platform
|
import pySim.global_platform
|
||||||
|
|
||||||
|
def compute_eid_checksum(eid) -> str:
|
||||||
|
"""Compute and add/replace check digits of an EID value according to GSMA SGP.29 Section 10."""
|
||||||
|
if type(eid) == str:
|
||||||
|
if len(eid) == 30:
|
||||||
|
# first pad by 2 digits
|
||||||
|
eid += "00"
|
||||||
|
elif len(eid) == 32:
|
||||||
|
# zero the last two digits
|
||||||
|
eid = eid[:-2] + "00"
|
||||||
|
else:
|
||||||
|
raise ValueError("and EID must be 30 or 32 digits")
|
||||||
|
eid_int = int(eid)
|
||||||
|
elif type(eid) == int:
|
||||||
|
eid_int = eid
|
||||||
|
if eid_int % 100:
|
||||||
|
# zero the last two digits
|
||||||
|
eid_int -= eid_int % 100
|
||||||
|
# Using the resulting 32 digits as a decimal integer, compute the remainder of that number on division by
|
||||||
|
# 97, Subtract the remainder from 98, and use the decimal result for the two check digits, if the result
|
||||||
|
# is one digit long, its value SHALL be prefixed by one digit of 0.
|
||||||
|
csum = 98 - (eid_int % 97)
|
||||||
|
eid_int += csum
|
||||||
|
return str(eid_int)
|
||||||
|
|
||||||
|
def verify_eid_checksum(eid) -> bool:
|
||||||
|
"""Verify the check digits of an EID value according to GSMA SGP.29 Section 10."""
|
||||||
|
# Using the 32 digits as a decimal integer, compute the remainder of that number on division by 97. If the
|
||||||
|
# remainder of the division is 1, the verification is successful; otherwise the EID is invalid.
|
||||||
|
return int(eid) % 97 == 1
|
||||||
|
|
||||||
class VersionAdapter(Adapter):
|
class VersionAdapter(Adapter):
|
||||||
"""convert an EUICC Version (3-int array) to a textual representation."""
|
"""convert an EUICC Version (3-int array) to a textual representation."""
|
||||||
|
|
||||||
|
|||||||
31
tests/test_euicc.py
Executable file
31
tests/test_euicc.py
Executable file
@@ -0,0 +1,31 @@
|
|||||||
|
#!/usr/bin/env python3
|
||||||
|
|
||||||
|
import unittest
|
||||||
|
|
||||||
|
from pySim.euicc import *
|
||||||
|
|
||||||
|
class TestEid(unittest.TestCase):
|
||||||
|
|
||||||
|
def test_eid_verify(self):
|
||||||
|
for eid in ['89049032123451234512345678901235', '89086030202200000022000023022943',
|
||||||
|
'89044045116727494800000004479366', 89044045116727494800000004479366]:
|
||||||
|
self.assertTrue(verify_eid_checksum(eid))
|
||||||
|
|
||||||
|
def test_eid_verify_wrong(self):
|
||||||
|
self.assertFalse(verify_eid_checksum('89049032123451234512345678901234'))
|
||||||
|
self.assertFalse(verify_eid_checksum(89049032123451234512345678901234))
|
||||||
|
|
||||||
|
def test_eid_encode_with_32_digits(self):
|
||||||
|
self.assertEquals(compute_eid_checksum('89049032123451234512345678901200'), '89049032123451234512345678901235')
|
||||||
|
self.assertEquals(compute_eid_checksum('89086030202200000022000023022900'), '89086030202200000022000023022943')
|
||||||
|
|
||||||
|
def test_eid_encode_with_30digits(self):
|
||||||
|
self.assertEquals(compute_eid_checksum('890490321234512345123456789012'), '89049032123451234512345678901235')
|
||||||
|
|
||||||
|
def test_eid_encode_with_wrong_csum(self):
|
||||||
|
# input: EID with wrong checksum
|
||||||
|
self.assertEquals(compute_eid_checksum('89049032123451234512345678901299'), '89049032123451234512345678901235')
|
||||||
|
self.assertEquals(compute_eid_checksum(89049032123451234512345678901299), '89049032123451234512345678901235')
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
unittest.main()
|
||||||
Reference in New Issue
Block a user