def proof_export(proof): bproof = ffi.new("byte_t **") bproof[0] = ffi.NULL size = ffi.new("uint32_t *") if lib.groupsig_proof_export(bproof, size, proof) == constants.IERROR: raise Exception('Error exporting proof.') b64proof = base64.b64encode(ffi.buffer(bproof[0], size[0])) # lib.free(bproof[0]) return b64proof
def convert(bsigs, grpkey, bldkey, msg=ffi.NULL, mgrkey=ffi.NULL): """ Converts a set of blinded group signatures, in schemes that support it. Optional parameters may be ignored in some schemes. Parameters: bsigs: An array containing the blinded group signatures. grpkey: The group key. bldkey: The public part of the blinding keypair. msg: Optional. The blinded messages associated to the signatures. mgrkey: Optional. The manager key used for conversion. Returns: An array containing the converted blinded group signatures. On error, an exception is thrown. """ _csigs = [ lib.groupsig_blindsig_init(grpkey.scheme) for i in range(len(bsigs)) ] csigs = ffi.new("groupsig_blindsig_t* []", _csigs) if lib.groupsig_convert(csigs, bsigs, len(bsigs), grpkey, mgrkey, bldkey, msg) == lib.IERROR: raise Exception('Error converting signatures.') return csigs
def verify_batch(sigs, msgs, grpkey): """ Verifies a group signature. Parameters: sigs: The array of signatures to verify. msgs: The array signed messages. Each message may be of type _bytes_ or a UTF-8 string. grpkey: The group key. Returns: True if the signatures are all valid, False otherwise. On error, an exception is thrown. """ _b = ffi.new("uint8_t *") _msgs = [] for i in range(len(msgs)): if isinstance(msgs[i], bytes): _msg = lib.message_from_bytes(msgs[i], len(msg[i])) else: _msg = lib.message_from_string(msgs[i].encode('utf8')) _msgs.append(_msg) if lib.groupsig_verify_batch(_b, sigs, _msgs, len(sigs), grpkey) == lib.IERROR: raise Exception('Error verifying message.') else: if _b[0] == 1: return True if _b[0] == 0: return False
def open(sig, mgrkey, grpkey, gml=ffi.NULL, crl=ffi.NULL): """ Opens a group signature, in schemes that support it. Optional parameters may be ignored in some schemes. Parameters: sig: The signature to open. mgrkey: The opener key. grpkey: The group key. gml: Optional. The GML. crl: Optional. The CRL (Certificate Revocation List). Returns: A native object with two fields: 'index': An integer identifying the signer within the GML. 'proof': Optional field. Will be a native object in schemes that provide verifiable openings. On error, an exception is thrown. """ _index = ffi.new("uint64_t *") proof = lib.groupsig_proof_init(sig.scheme) if lib.groupsig_open(_index, proof, crl, sig, grpkey, mgrkey, gml) == lib.IERROR: raise Exception('Error opening signature') return {'index': _index[0], 'proof': proof}
def verify(sig, msg, grpkey): """ Verifies a group signature. Parameters: sig: The signature to verify. msg: The signed message. May be of type _bytes_ or a UTF-8 string. grpkey: The group key. Returns: True if the signature is valid, False otherwise. On error, an exception is thrown. """ _b = ffi.new("uint8_t *") if isinstance(msg, bytes): _msg = lib.message_from_bytes(msg, len(msg)) else: _msg = lib.message_from_string(msg.encode('utf8')) if lib.groupsig_verify(_b, sig, _msg, grpkey) == lib.IERROR: raise Exception('Error verifying message.') else: if _b[0] == 1: return True if _b[0] == 0: return False
def join_mem(step, grpkey, msgin=ffi.NULL, memkey=ffi.NULL): """ Runs a member step of the join process. As a result of this function, a message of the join process will be generated. In the final member call, the member key will also be returned. Optional parameters may be ignored in some schemes. Parameters: step: The step of the join process to execute. grpkey: The group key. msgin: Optional. The input message from a previous step of the join process. memkey: Optional. A (possibly partially filled) member key. Returns: An object containing: msgout: A native object (of message type) with the message to send to the manager. memkey: When step is the last step in the join process, the final member key. On error, an exception is thrown. """ msgout = ffi.new("message_t **") msgout[0] = ffi.NULL if memkey == ffi.NULL: _memkey = lib.groupsig_mem_key_init(grpkey.scheme) else: _memkey = memkey if lib.groupsig_join_mem(msgout, _memkey, step, msgin, grpkey) == lib.IERROR: raise Exception('Error running join_mem operation.') return {'msgout': msgout[0], 'memkey': _memkey}
def join_mgr(step, mgrkey, grpkey, msgin=ffi.NULL, gml=ffi.NULL): """ Runs a manager step of the join process. As a result of this function, a message of the join process will be generated. Optional parameters may be ignored in some schemes. Parameters: step: The step of the join process to execute. mgrkey: The manager key. grpkey: The group key. msgin: Optional. The input message from a previous step of the join process. gml: Optional. The GML. Returns: A native object (of message type) containing the next message to send, if any. On error, an exception is thrown. """ msgout = ffi.new("message_t **") msgout[0] = ffi.NULL if lib.groupsig_join_mgr(msgout, gml, mgrkey, step, msgin, grpkey) == lib.IERROR: raise Exception('Error running join_mgr operation.') return msgout[0]
def proof_to_string(proof): _str = ffi.new("char *") _str = lib.groupsig_proof_to_string(proof) if _str == ffi.NULL: raise Exception('Error converting proof to string.') return ffi.string(_str).decode('utf8')
def gml_export(gml): """ Exports a GML to a Base64 string. Parameters: gml: The GML to export. Returns: A Base64 string. On error, an Exception is thrown. """ bgml = ffi.new("byte_t **") bgml[0] = ffi.NULL size = ffi.new("uint32_t *") if lib.gml_export(bgml, size, gml) == constants.IERROR: raise Exception('Error exporting GML.') b64gml = base64.b64encode(ffi.buffer(bgml[0], size[0])) # lib.free(bgml[0]) return b64gml
def grpkey_export(grpkey): """ Exports the given group key to a Base64 string. Parameters: grpkey: The native group key data structure. Returns: A Base64 string. On error, an Exception is thrown. """ bkey = ffi.new("byte_t **") bkey[0] = ffi.NULL size = ffi.new("uint32_t *") if lib.groupsig_grp_key_export(bkey, size, grpkey) == constants.IERROR: raise Exception('Error exporting group key.') b64 = base64.b64encode(ffi.buffer(bkey[0],size[0])) b64 = b64.decode('utf-8').replace('\n', '') # lib.free(bkey[0]) return b64
def bldkey_export_pub(bldkey): """ Exports the public part of given blinding keypair to a Base64 string. Parameters: bldkey: The blinding key to export. Returns: The produced Base64 string. On error, an Exception is thrown. """ bkey = ffi.new("byte_t **") bkey[0] = ffi.NULL size = ffi.new("uint32_t *") if lib.groupsig_bld_key_export_pub(bkey, size, bldkey) == constants.IERROR: raise Exception('Error exporting blinding public key.') b64key = base64.b64encode(ffi.buffer(bkey[0],size[0])) b64key = b64key.decode('utf-8').replace('\n', '') # lib.free(bkey[0]) return b64key
def blindsig_export(sig): """ Exports the given blinded signature to a Base64 string. Parameters: sig: The blinded signature to export. Returns: The produced Base64 string. On error, an Exception is thrown. """ bsig = ffi.new("byte_t **") bsig[0] = ffi.NULL size = ffi.new("uint32_t *") if lib.groupsig_blindsig_export(bsig, size, sig) == constants.IERROR: raise Exception('Error exporting blindsig.') b64sig = base64.b64encode(ffi.buffer(bsig[0], size[0])) b64sig = b64sig.decode('utf-8').replace('\n', '') # lib.free(bsig[0]) return b64sig
def signature_export(sig): """ Exports the given group signature a Base64 string. Parameters: sig: The native group signature data structure. Returns: A Base64 string. On error, an Exception is thrown. """ bsig = ffi.new("byte_t **") bsig[0] = ffi.NULL size = ffi.new("uint32_t *") if lib.groupsig_signature_export(bsig, size, sig) == constants.IERROR: raise Exception('Error exporting signature.') b64sig = base64.b64encode(ffi.buffer(bsig[0], size[0])) b64sig = b64sig.decode('utf-8').replace('\n', '') # lib.free(bsig[0]) return b64sig
def open_verify(proof, sig, grpkey): _b = ffi.new("uint8_t *") if lib.groupsig_open_verify(_b, proof, sig, grpkey) == lib.IERROR: raise Exception('Error verifying open proof') else: if _b[0] == 1: return True if _b[0] == 0: return False
def blind(grpkey, sig, msg, bldkey=ffi.NULL): """ Blinds a group signature, in schemes that support it. Parameters: grpkey: The group key. sig: The signature to blind. msg: The message associated to the signature. bldkey: Optional. The key used for blinding. If unspecified, a random key will be internally generated and returned. Returns: An object containing: bldkey: The key used to blind the signature. bsig: The blinded signature. On error, an exception is thrown. """ if bldkey == ffi.NULL: _bldkey = ffi.new("groupsig_key_t **") _bldkey[0] = ffi.NULL else: _bldkey = ffi.new("groupsig_key_t **") _bldkey[0] = bldkey bsig = lib.groupsig_blindsig_init(sig.scheme) if isinstance(msg, bytes): _msg = lib.message_from_bytes(msg, len(msg)) else: _msg = lib.message_from_string(msg.encode('utf8')) if lib.groupsig_blind(bsig, _bldkey, grpkey, sig, _msg) == lib.IERROR: raise Exception('Error blinding signature.') if bldkey == ffi.NULL: return {'bldkey': _bldkey[0], 'bsig': bsig} else: return {'bldkey': _bldkey, 'bsig': bsig}
def blindsig_to_string(sig): """ Returns a human readable string corresponding to the given blinded signature. Parameters: sig: The blinded signature to print. Returns: The produced string. On error, an Exception is thrown. """ _str = ffi.new("char *") _str = lib.groupsig_blindsig_to_string(sig) if _str == ffi.NULL: raise Exception('Error converting blindsig to string.') return ffi.string(_str).decode('utf8')
def memkey_to_string(key): """ Returns a human readable string for the given member key. Parameters: key: The native member key data structure. Returns: A human readable string. On error, an Exception is thrown. """ _str = ffi.new("char *") _str = lib.groupsig_mem_key_to_string(key) if _str == ffi.NULL: raise Exception('Error converting member key to string.') return ffi.string(_str).decode('utf8')
def message_to_base64(msg): """ Exports the given message object to a Base64 string. Parameters: msg: The message to export to a string. Returns: A Base64 string. On error, an Exception is thrown. """ _str = ffi.new("char *") _str = lib.message_to_base64(msg) if _str == ffi.NULL: raise Exception('Error converting message to a Base64 string.') return ffi.string(_str).decode('utf8')
def signature_to_string(sig): """ Returns a human readable string for the given group signature. Parameters: sig: The group signature. Returns: A human readable string. On error, an Exception is thrown. """ _str = ffi.new("char *") _str = lib.groupsig_signature_to_string(sig) if _str == ffi.NULL: raise Exception('Error converting signature to string.') return ffi.string(_str).decode('utf8')
def message_to_string(msg): """ Exports the given message object to a UTF-8 string. Use only for messages that are ensured to be strings. Parameters: msg: The message to export to a string. Returns: A UTF-8 string. On error, an Exception is thrown. """ _str = ffi.new("char *") _str = lib.message_to_string(msg) if _str == ffi.NULL: raise Exception('Error converting message to string.') return ffi.string(_str).decode('utf8')
def get_joinseq(scheme): """ Returns the number of messages exchanged between the manager and member during a join process of schemes of the given type Parameters: scheme: The code specifying the type of scheme. Returns: The number of messages to be exchanged. """ msgs = ffi.new("uint8_t *") if lib.groupsig_get_joinseq(scheme, msgs) == lib.IERROR: raise Exception('Error getting number of messages in Join.') return msgs[0]
def get_joinstart(scheme): """ Informs whether the manager of the member initiates the join process. Parameters: scheme: The code specifying the type of scheme. Returns: 0 if the manager starts the join process, 1 if the member starts. """ start = ffi.new("uint8_t *") if lib.groupsig_get_joinstart(scheme, start) == lib.IERROR: raise Exception('Error getting starting party in Join.') return start[0]