Skip to content

Instantly share code, notes, and snippets.

@typelogic
Created December 14, 2020 06:33
Show Gist options
  • Save typelogic/ca529170c749b11ec16cc7c558201a19 to your computer and use it in GitHub Desktop.
Save typelogic/ca529170c749b11ec16cc7c558201a19 to your computer and use it in GitHub Desktop.
import sys, os, signal
import pysodium
import ctypes
import ctypes.util
import api_pb2
import traceback
import logging
import unittest
import binascii
import base64
def signal_handler(sig, frame):
print('You pressed Ctrl+C!')
sys.exit(0)
signal.signal(signal.SIGINT, signal_handler)
lib = 0
def load():
global lib
# lib = CDLL("lib/libidpasslite.so")
# Linux -> export LD_LIBRARY_PATH=/path/to/lib/:$LB_LIBRARY_PATH
# Windows -> set PATH=c:/path/to/lib;%PATH%
idpasslite = ctypes.util.find_library('idpasslite') or ctypes.util.find_library('libidpasslite')
if idpasslite is None:
raise ValueError('Unable to find idpasslite library')
lib = ctypes.cdll.LoadLibrary(idpasslite)
if not lib._name:
raise ValueError('Unable to correctly load idpasslite library')
lib.idpass_lite_init.argtypes = [
ctypes.POINTER(ctypes.c_ubyte),
ctypes.c_int,
ctypes.POINTER(ctypes.c_ubyte),
ctypes.c_int
]
lib.idpass_lite_init.restype = ctypes.c_void_p
ident = api_pb2.Ident()
ident.surName = "Doe"
ident.givenName = "John"
buf = ident.SerializeToString()
print(type(buf))
print(len(buf))
ba = bytearray(buf)
keyset = api_pb2.KeySet()
pk, sk = pysodium.crypto_sign_keypair()
keyset.encryptionKey = pysodium.randombytes(32)
keyset.signatureKey = sk
ks = bytearray(keyset.SerializeToString())
# <class 'TypeError'>: expected LP_c_ubyte instance instead of bytes
# expected c_ubyte instead of bytes
ctx = lib.idpass_lite_init(
(ctypes.c_ubyte * len(ks))(*ks),
len(ks),
None,
0)
'''
lib.idpass_api_ioctl.argtypes = [ ctypes.c_void_p, ctypes.POINTER(ctypes.c_int), ctypes.POINTER(ctypes.c_ubyte), ctypes.c_int ]
lib.idpass_api_ioctl.restype = ctypes.POINTER(ctypes.c_ubyte)
# These argtypes/restype signatures declarations here are optional but helps in a certain way
lib.idpass_api_ioctl.argtypes = [ ctypes.c_void_p, ctypes.POINTER(ctypes.c_int), ctypes.POINTER(ctypes.c_ubyte), ctypes.c_int ]
lib.idpass_api_ioctl.restype = ctypes.POINTER(ctypes.c_ubyte)
lib.idpass_api_compare_face_photo.argtypes = [ ctypes.c_void_p, ctypes.POINTER(ctypes.c_ubyte), ctypes.c_int, ctypes.POINTER(ctypes.c_ubyte), ctypes.c_int, ctypes.POINTER(ctypes.c_float) ]
lib.idpass_api_compare_face_photo.restype = ctypes.c_int
# doesn't need ctx and can be invoke as a class method just like the generate key methods
lib.idpass_api_compare_face_template.argtypes = [ ctypes.POINTER(ctypes.c_ubyte), ctypes.c_int, ctypes.POINTER(ctypes.c_ubyte), ctypes.c_int, ctypes.POINTER(ctypes.c_float) ]
lib.idpass_api_compare_face_template.restype = ctypes.c_int
lib.idpass_api_face128d.argtypes = [ ctypes.c_void_p, ctypes.POINTER(ctypes.c_ubyte), ctypes.c_int, ctypes.POINTER(ctypes.c_float) ]
lib.idpass_api_face128d.restype = ctypes.c_int
lib.idpass_api_face64d.argtypes = [ ctypes.c_void_p, ctypes.POINTER(ctypes.c_ubyte), ctypes.c_int, ctypes.POINTER(ctypes.c_float) ]
lib.idpass_api_face64d.restype = ctypes.c_int
# since output size already known, then caller must pre-allocate the last param as 128*4 bytes and then pass into function
lib.idpass_api_face128dbuf.argtypes = [ ctypes.c_void_p, ctypes.POINTER(ctypes.c_ubyte), ctypes.c_int, ctypes.POINTER(ctypes.c_ubyte) ]
lib.idpass_api_face128dbuf.restype = ctypes.c_int
# since output size already known, then caller must pre-allocate the last param as 64*2 bytes and then pass into function
lib.idpass_api_face64dbuf.argtypes = [ ctypes.c_void_p, ctypes.POINTER(ctypes.c_ubyte), ctypes.c_int, ctypes.POINTER(ctypes.c_ubyte) ]
lib.idpass_api_face64dbuf.restype = ctypes.c_int
lib.idpass_api_generate_encryption_key.argtypes = [ ctypes.POINTER(ctypes.c_ubyte), ctypes.c_int ]
lib.idpass_api_generate_encryption_key.restype = ctypes.c_int
lib.idpass_api_generate_secret_signature_key.argtypes = [ ctypes.POINTER(ctypes.c_ubyte), ctypes.c_int ]
lib.idpass_api_generate_secret_signature_key.restype = ctypes.c_int
lib.idpass_api_create_card_with_face.argtypes = [ ctypes.c_void_p, ctypes.POINTER(ctypes.c_int), ctypes.c_char_p, ctypes.c_char_p, ctypes.c_char_p,
ctypes.c_char_p, ctypes.c_char_p, ctypes.POINTER(ctypes.c_ubyte), ctypes.c_int, ctypes.c_char_p, ctypes.c_int, ctypes.c_char_p, ctypes.c_int ]
lib.idpass_api_create_card_with_face.restype = ctypes.POINTER(ctypes.c_ubyte)
lib.idpass_api_qrpixel2.argtypes = [ ctypes.c_void_p, ctypes.POINTER(ctypes.c_int), ctypes.POINTER(ctypes.c_ubyte), ctypes.c_int, ctypes.POINTER(ctypes.c_int) ]
lib.idpass_api_qrpixel2.restype = ctypes.POINTER(ctypes.c_ubyte)
lib.idpass_api_freemem.argtypes = [ ctypes.c_void_p, ctypes.c_void_p ]
lib.idpass_api_freemem.restype = None
# These argtypes/restype signatures declarations here are optional but helps in a certain way
lib.idpass_api_ioctl.argtypes = [ ctypes.c_void_p, ctypes.POINTER(ctypes.c_int), ctypes.POINTER(ctypes.c_ubyte), ctypes.c_int ]
lib.idpass_api_ioctl.restype = ctypes.POINTER(ctypes.c_ubyte)
lib.idpass_api_compare_face_photo.argtypes = [ ctypes.c_void_p, ctypes.POINTER(ctypes.c_ubyte), ctypes.c_int, ctypes.POINTER(ctypes.c_ubyte), ctypes.c_int, ctypes.POINTER(ctypes.c_float) ]
lib.idpass_api_compare_face_photo.restype = ctypes.c_int
# doesn't need ctx and can be invoke as a class method just like the generate key methods
lib.idpass_api_compare_face_template.argtypes = [ ctypes.POINTER(ctypes.c_ubyte), ctypes.c_int, ctypes.POINTER(ctypes.c_ubyte), ctypes.c_int, ctypes.POINTER(ctypes.c_float) ]
lib.idpass_api_compare_face_template.restype = ctypes.c_int
lib.idpass_api_face128d.argtypes = [ ctypes.c_void_p, ctypes.POINTER(ctypes.c_ubyte), ctypes.c_int, ctypes.POINTER(ctypes.c_float) ]
lib.idpass_api_face128d.restype = ctypes.c_int
lib.idpass_api_face64d.argtypes = [ ctypes.c_void_p, ctypes.POINTER(ctypes.c_ubyte), ctypes.c_int, ctypes.POINTER(ctypes.c_float) ]
lib.idpass_api_face64d.restype = ctypes.c_int
# since output size already known, then caller must pre-allocate the last param as 128*4 bytes and then pass into function
lib.idpass_api_face128dbuf.argtypes = [ ctypes.c_void_p, ctypes.POINTER(ctypes.c_ubyte), ctypes.c_int, ctypes.POINTER(ctypes.c_ubyte) ]
lib.idpass_api_face128dbuf.restype = ctypes.c_int
# since output size already known, then caller must pre-allocate the last param as 64*2 bytes and then pass into function
lib.idpass_api_face64dbuf.argtypes = [ ctypes.c_void_p, ctypes.POINTER(ctypes.c_ubyte), ctypes.c_int, ctypes.POINTER(ctypes.c_ubyte) ]
lib.idpass_api_face64dbuf.restype = ctypes.c_int
lib.idpass_api_generate_encryption_key.argtypes = [ ctypes.POINTER(ctypes.c_ubyte), ctypes.c_int ]
lib.idpass_api_generate_encryption_key.restype = ctypes.c_int
lib.idpass_api_generate_secret_signature_key.argtypes = [ ctypes.POINTER(ctypes.c_ubyte), ctypes.c_int ]
lib.idpass_api_generate_secret_signature_key.restype = ctypes.c_int
lib.idpass_api_init.argtypes = [ ctypes.POINTER(ctypes.c_ubyte), ctypes.c_int, ctypes.POINTER(ctypes.c_ubyte), ctypes.c_int, ctypes.POINTER(ctypes.c_ubyte), ctypes.c_int ]
lib.idpass_api_init.restype = ctypes.c_void_p
lib.idpass_api_create_card_with_face.argtypes = [ ctypes.c_void_p, ctypes.POINTER(ctypes.c_int), ctypes.c_char_p, ctypes.c_char_p, ctypes.c_char_p,
ctypes.c_char_p, ctypes.c_char_p, ctypes.POINTER(ctypes.c_ubyte), ctypes.c_int, ctypes.c_char_p, ctypes.c_int, ctypes.c_char_p, ctypes.c_int ]
lib.idpass_api_create_card_with_face.restype = ctypes.POINTER(ctypes.c_ubyte)
lib.idpass_api_qrpixel2.argtypes = [ ctypes.c_void_p, ctypes.POINTER(ctypes.c_int), ctypes.POINTER(ctypes.c_ubyte), ctypes.c_int, ctypes.POINTER(ctypes.c_int) ]
lib.idpass_api_qrpixel2.restype = ctypes.POINTER(ctypes.c_ubyte)
lib.idpass_api_freemem.argtypes = [ ctypes.c_void_p, ctypes.c_void_p ]
lib.idpass_api_freemem.restype = None
'''
print("***")
def sodium_test():
pk, sk = pysodium.crypto_sign_keypair()
signed = pysodium.crypto_sign(b'howdy', sk)
changed = signed[:pysodium.crypto_sign_BYTES] + b'0' + signed[pysodium.crypto_sign_BYTES + 1:]
try:
a = pysodium.crypto_sign_open(signed, pk)
print(a)
pysodium.crypto_sign_open(changed, pk)
except ValueError as e:
print(e)
print("--> ", sys.exc_info()[0])
def main():
sodium_test()
load()
print("hello")
c1 = api_pb2.Certificate()
c2 = api_pb2.Certificate()
c3 = api_pb2.Certificate()
r1 = api_pb2.Certificates()
a = c1.SerializeToString()
print(len(a))
try:
id1 = api_pb2.Ident()
id1.surName = "John"
id1.givenName = "Doe"
b = id1.SerializeToString()
print(len(b))
c = id1.surName
print(c)
d = api_pb2.Ident()
print("surName = {}".format(d.surName))
e = d.ParseFromString(b)
print(d.surName)
except AttributeError as e:
print(e)
except IOError as e:
print(e)
#print("I/O error({0}): {1}".format(errno, strerror))
except ValueError as e:
print(e)
except Exception as e:
logging.error(traceback.format_exc())
print("Unexpected error:", sys.exc_info()[0])
main()
print("--oOo--")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment