def decrypt(self, message, context): decrypted_message_length = c_int(0) if ( themis.themis_secure_cell_decrypt_context_imprint( self.key, len(self.key), message, len(message), context, context_length, None, byref(decrypted_message_length), ) != -4 ): raise themis_exception( THEMIS_CODES.FAIL, "themis_secure_cell_decrypt_context_imprint (decrypted message length determination) error", ) decrypted_message = create_string_buffer(decrypted_message_length.value) if ( themis.themis_secure_cell_decrypt_context_imprint( self.key, len(self.key), message, len(message), context, context_length, decrypted_message, byref(decrypted_message_length), ) != 0 ): raise themis_exception(THEMIS_CODES.FAIL, "themis_secure_cell_decrypt_context_imprint error") return string_at(decrypted_message, decrypted_message_length.value)
def encrypt(self, message, context): encrypted_message_length=c_int(0); if themis.themis_secure_cell_encrypt_user_split(self.key ,len(self.key), message, len(message), context, context_length, None, byref(encrypted_message_length)) != -4: raise themis_exception("themis_secure_cell_encrypt_user_split (encrypted message length determination) error"); encrypted_message=create_string_buffer(encrypted_message_length.value); if themis.themis_secure_cell_encrypt_user_split(self.key ,len(self.key), message, len(message), context, context_length, encrypted_message, byref(encrypted_message_length)) != 0: raise themis_exception("themis_secure_cell_encrypt_user_split error"); return string_at(encrypted_message, encrypted_message_length.value);
def decrypt(self, message, context): decrypted_message_length=c_int(0); if themis.themis_secure_cell_decrypt_user_split(self.key ,len(self.key), message, len(message), context, context_length, None, byref(decrypted_message_length)) != -4: raise themis_exception(THEMIS_CODES.FAIL, "themis_secure_cell_decrypt_user_split (decrypted message length determination) error"); decrypted_message=create_string_buffer(decrypted_message_length.value); if themis.themis_secure_cell_decrypt_user_split(self.key ,len(self.key), message, len(message), context, context_length, decrypted_message, byref(decrypted_message_length)) != 0: raise themis_exception(THEMIS_CODES.FAIL, "themis_secure_cell_decrypt_user_split error"); return string_at(decrypted_message, decrypted_message_length.value);
def decrypt(self, message, context): decrypted_message_length=c_int(0); res=themis.themis_secure_cell_decrypt_auto_split(self.key ,len(self.key), None, 0, message, len(message), context, len(context), None, byref(decrypted_message_length)); if res!=-4: raise themis_exception("themis_secure_cell_decrypt_auto_split (decrypted message length determination) error "+`res`); decrypted_message=create_string_buffer(decrypted_message_length.value); if themis.themis_secure_cell_decrypt_auto_split(self.key ,len(self.key), None, 0, message, len(message), context, len(context), decrypted_message, byref(decrypted_message_length)) != 0: raise themis_exception("themis_secure_cell_decrypt_auto_split error"); return string_at(decrypted_message, decrypted_message_length.value);
def wrap(self, message): encrypted_message_length=c_int(0); res=themis.themis_secure_message_wrap(self.private_key ,len(self.private_key), self.peer_public_key, len(self.peer_public_key), message, len(message), None, byref(encrypted_message_length)); if res!=-4: raise themis_exception("themis_secure_message_wrap (wrapped message length determination) error " + `res`); encrypted_message=create_string_buffer(encrypted_message_length.value); res=themis.themis_secure_message_wrap(self.private_key ,len(self.private_key), self.peer_public_key, len(self.peer_public_key), message, len(message), encrypted_message, byref(encrypted_message_length)); if res!=0: raise themis_exception("themis_secure_message_wrap error " + `res`); return string_at(encrypted_message, encrypted_message_length.value);
def unwrap(self,message): plain_message_length=c_int(0); res=themis.themis_secure_message_unwrap(self.private_key, len(self.private_key), self.peer_public_key ,len(self.peer_public_key), message, len(message), None, byref(plain_message_length)); if res!=-4: raise themis_exception("themis_secure_message_unwrap (plain message length determination) error " + `res`); plain_message=create_string_buffer(plain_message_length.value); res=themis.themis_secure_message_unwrap(self.private_key, len(self.private_key), self.peer_public_key ,len(self.peer_public_key), message, len(message), plain_message, byref(plain_message_length)); if res!=0: raise themis_exception("themis_secure_message_unwrap decrypting error " + `res`); return string_at(plain_message, plain_message_length.value);
def unwrap(self,message): plain_message_length=c_int(0); res=themis.themis_secure_message_unwrap(self.private_key, len(self.private_key), self.peer_public_key ,len(self.peer_public_key), message, len(message), None, byref(plain_message_length)); if res!=THEMIS_CODES.BUFFER_TOO_SMALL: raise themis_exception(res, "themis_secure_message_unwrap (plain message length determination) error"); plain_message=create_string_buffer(plain_message_length.value); res=themis.themis_secure_message_unwrap(self.private_key, len(self.private_key), self.peer_public_key ,len(self.peer_public_key), message, len(message), plain_message, byref(plain_message_length)); if res!=THEMIS_CODES.SUCCESS: raise themis_exception(res,"themis_secure_message_unwrap decrypting error"); return string_at(plain_message, plain_message_length.value);
def connect_request(self): req_size=ctypes.c_int(0); res = themis.secure_session_generate_connect_request(self.session_ctx, None, ctypes.byref(req_size)); if res!=-4: raise exception.themis_exception("secure_session_generate_connect_request (buffer_length determination) failed: " + str(res)); req_buffer=ctypes.create_string_buffer(req_size.value); res = themis.secure_session_generate_connect_request(self.session_ctx, ctypes.byref(req_buffer), ctypes.byref(req_size)); if res!=0: raise exception.themis_exception("secure_session_generate_connect_request failed: " + str(res)); return ctypes.string_at(req_buffer, req_size);
def sverify(public_key, message): plain_message_length=c_int(0); res=themis.themis_secure_message_unwrap(None, 0, public_key ,len(public_key), message, len(message), None, byref(plain_message_length)); if res!=THEMIS_CODES.BUFFER_TOO_SMALL: raise themis_exception(res, "themis_secure_message_unwrap (plain message length determination) error"); plain_message=create_string_buffer(plain_message_length.value); res=themis.themis_secure_message_unwrap(None, 0, public_key ,len(public_key), message, len(message), plain_message, byref(plain_message_length)); if res!=THEMIS_CODES.SUCCESS: raise themis_exception(res,"themis_secure_message_unwrap verification error"); return string_at(plain_message, plain_message_length.value);
def connect_request(self): req_size=ctypes.c_int(0); res = themis.secure_session_generate_connect_request(self.session_ctx, None, ctypes.byref(req_size)); if res!=THEMIS_CODES.BUFFER_TOO_SMALL: raise exception.themis_exception(res, "secure_session_generate_connect_request (buffer_length determination) failed"); req_buffer=ctypes.create_string_buffer(req_size.value); res = themis.secure_session_generate_connect_request(self.session_ctx, ctypes.byref(req_buffer), ctypes.byref(req_size)); if res!=THEMIS_CODES.SUCCESS: raise exception.themis_exception(res, "secure_session_generate_connect_request failed"); return ctypes.string_at(req_buffer, req_size);
def wrap(self, message): encrypted_message_length=c_int(0); res=themis.themis_secure_message_wrap(self.private_key ,len(self.private_key), self.peer_public_key, len(self.peer_public_key), message, len(message), None, byref(encrypted_message_length)); if res!=THEMIS_CODES.BUFFER_TOO_SMALL: raise themis_exception(res,"themis_secure_message_wrap (wrapped message length determination) error"); encrypted_message=create_string_buffer(encrypted_message_length.value); res=themis.themis_secure_message_wrap(self.private_key ,len(self.private_key), self.peer_public_key, len(self.peer_public_key), message, len(message), encrypted_message, byref(encrypted_message_length)); if res!=THEMIS_CODES.SUCCESS: raise themis_exception(res, "themis_secure_message_wrap error"); return string_at(encrypted_message, encrypted_message_length.value);
def encrypt(self, message): encrypted_message_length=c_int(0); context_length=c_int(0); if themis.themis_secure_cell_encrypt_auto_split(self.key ,len(self.key), None, 0, message, len(message), None, byref(context_length), None, byref(encrypted_message_length)) != -4: raise themis_exception(THEMIS_CODES.FAIL, "themis_secure_cell_encrypt_auto_split (encrypted message and context length determination) error"); encrypted_message=create_string_buffer(encrypted_message_length.value); context=create_string_buffer(context_length.value); if themis.themis_secure_cell_encrypt_auto_split(self.key ,len(self.key), None, 0, message, len(message), context, byref(context_length), encrypted_message, byref(encrypted_message_length)) != 0: raise themis_exception(THEMIS_CODES.FAIL, "themis_secure_cell_encrypt_auto_split error"); return (string_at(encrypted_message, encrypted_message_length.value), string_at(context, context_length));
def wrap(self, message): send_message=ctypes.create_string_buffer(message); wrapped_message_length=ctypes.c_int(0); res = themis.secure_session_wrap(self.session_ctx, ctypes.byref(send_message), len(message), 0, ctypes.byref(wrapped_message_length)); if res!=THEMIS_CODES.BUFFER_TOO_SMALL: raise exception.themis_exception(res,"secure_session_wrap (buffer_length determination) failed"); wrapped_message=ctypes.create_string_buffer(wrapped_message_length.value); res = themis.secure_session_wrap(self.session_ctx, ctypes.byref(send_message), len(message), ctypes.byref(wrapped_message), ctypes.byref(wrapped_message_length)); if res!=THEMIS_CODES.SUCCESS: raise exception.themis_exception(res,"secure_session_wrap failed"); return ctypes.string_at(wrapped_message, wrapped_message_length);
def __init__(self, alg): self.private_key_length = c_int(4096); self.public_key_length = c_int(4096); self.private_key = create_string_buffer(self.private_key_length.value); self.public_key = create_string_buffer(self.public_key_length.value); if alg == "EC" : if themis.themis_gen_ec_key_pair(self.private_key, byref(self.private_key_length), self.public_key, byref(self.public_key_length)) != 0: raise themis_exception(THEMIS_CODES.FAIL, "themis_gen_ec_key_pair error") elif alg == "RSA" : if themis.themis_gen_rsa_key_pair(self.private_key, byref(self.private_key_length), self.public_key, byref(self.public_key_length)) !=0: raise themis_exception(THEMIS_CODES.FAIL,"themis_gen_rsa_key_pair error")
def wrap(self, message): send_message=ctypes.create_string_buffer(message); wrapped_message_length=ctypes.c_int(0); res = themis.secure_session_wrap(self.session_ctx, ctypes.byref(send_message), len(message), 0, ctypes.byref(wrapped_message_length)); if res!=-4: raise exception.themis_exception("secure_session_wrap (buffer_length determination) failed: " + str(res)); wrapped_message=ctypes.create_string_buffer(wrapped_message_length.value); res = themis.secure_session_wrap(self.session_ctx, ctypes.byref(send_message), len(message), ctypes.byref(wrapped_message), ctypes.byref(wrapped_message_length)); if res!=0: raise exception.themis_exception("secure_session_wrap failed: " + str(res)); return ctypes.string_at(wrapped_message, wrapped_message_length);
def encrypt(self, message, context=None): if context==None: context_length=0; else: context_length=len(context); encrypted_message_length=c_int(0); if themis.themis_secure_cell_encrypt_full(self.key ,len(self.key), context, context_length, message, len(message), None, byref(encrypted_message_length)) != -4: raise themis_exception(THEMIS_CODES.FAIL, "themis_secure_cell_encrypt_full (encrypted message length determination) error"); encrypted_message=create_string_buffer(encrypted_message_length.value); if themis.themis_secure_cell_encrypt_full(self.key ,len(self.key), context, context_length, message, len(message), encrypted_message, byref(encrypted_message_length)) != 0: raise themis_exception(THEMIS_CODES.FAIL, "themis_secure_cell_encrypt_full error"); return string_at(encrypted_message, encrypted_message_length.value);
def unwrap(self, message): wrapped_message=ctypes.create_string_buffer(message); unwrapped_message_length=ctypes.c_int(0); res=themis.secure_session_unwrap(self.session_ctx, wrapped_message, len(message), 0, ctypes.byref(unwrapped_message_length)); if res==0: return (res, ""); if res!=-4: raise exception.themis_exception("secure_session_unwrap (buffer_length determination) failed: " + str(res)); unwrapped_message=ctypes.create_string_buffer(unwrapped_message_length.value); res=themis.secure_session_unwrap(self.session_ctx, wrapped_message, len(message), ctypes.byref(unwrapped_message), ctypes.byref(unwrapped_message_length)); if res<0: raise exception.themis_exception("secure_session_unwrap failed: " + str(res)); return (res, ctypes.string_at(unwrapped_message, unwrapped_message_length));
def unwrap(self, message): wrapped_message=ctypes.create_string_buffer(message); unwrapped_message_length=ctypes.c_int(0); res=themis.secure_session_unwrap(self.session_ctx, wrapped_message, len(message), 0, ctypes.byref(unwrapped_message_length)); if res==THEMIS_CODES.SUCCESS: return sstring(""); if res!=THEMIS_CODES.BUFFER_TOO_SMALL: raise exception.themis_exception(res,"secure_session_unwrap (buffer_length determination) failed"); unwrapped_message=ctypes.create_string_buffer(unwrapped_message_length.value); res=themis.secure_session_unwrap(self.session_ctx, wrapped_message, len(message), ctypes.byref(unwrapped_message), ctypes.byref(unwrapped_message_length)); rez=sstring(ctypes.string_at(unwrapped_message, unwrapped_message_length)); if res==THEMIS_CODES.SEND_AS_IS: rez.set_control(); elif res!=THEMIS_CODES.SUCCESS: raise exception.themis_exception(res,"secure_session_unwrap failed"); return rez;
def decrypt(self, message, context): decrypted_message_length = c_int(0) if themis.themis_secure_cell_decrypt_user_split( self.key, len(self.key), message, len(message), context, context_length, None, byref(decrypted_message_length)) != -4: raise themis_exception( "themis_secure_cell_decrypt_user_split (decrypted message length determination) error" ) decrypted_message = create_string_buffer( decrypted_message_length.value) if themis.themis_secure_cell_decrypt_user_split( self.key, len(self.key), message, len(message), context, context_length, decrypted_message, byref(decrypted_message_length)) != 0: raise themis_exception( "themis_secure_cell_decrypt_user_split error") return string_at(decrypted_message, decrypted_message_length.value)
def receive(self): message=ctypes.create_string_buffer(1024); message_length=ctypes.c_size_t(1024); res=themis.secure_session_receive(self.session_ctx, message, message_length); if res == -2222 : raise exception.themis_exception("secure_session_receive failed: " + str(res)); elif res<0: return ""; return ctypes.string_at(message, res);
def receive(self): message=ctypes.create_string_buffer(1024); message_length=ctypes.c_size_t(1024); res=themis.secure_session_receive(self.session_ctx, message, message_length); if res == THEMIS_CODES.NETWORK_ERROR: raise exception.themis_exception(res, "secure_session_receive failed"); elif res<0: return ""; return ctypes.string_at(message, res);
def __init__(self, user_id, sign_key, transport): # user_id - user identification ("server" for example), sign_key - private key of session owner, transport - refference for transport_t object. self.session_ctx=ctypes.POINTER(ctypes.c_int); if transport != None: self.lp_conn=lp_conn_type(ctypes.py_object(transport)); self.transport_=transport_t(on_send_, on_receive_, on_change_status_, on_get_pub_key_, self.lp_conn); self.session_ctx=ssession_create(ctypes.byref(ctypes.create_string_buffer(user_id)), len(user_id), ctypes.byref(ctypes.create_string_buffer(sign_key)), len(sign_key), ctypes.byref(self.transport_)); else: self.session_ctx=ssession_create(ctypes.byref(ctypes.create_string_buffer(user_id)), len(user_id), ctypes.byref(ctypes.create_string_buffer(sign_key)), len(sign_key), 0); if self.session_ctx==None: raise exception.themis_exception(THEMIS_CODES.FAIL, "secure_session_create fail");
def __init__(self, user_id, sign_key, transport): self.session_ctx=ctypes.POINTER(ctypes.c_int); if transport != None: self.lp_conn=lp_conn_type(ctypes.py_object(transport)); self.transport_=transport_t(on_send_, on_receive_, on_change_status_, on_get_pub_key_, self.lp_conn); self.session_ctx=ssession_create(ctypes.byref(ctypes.create_string_buffer(user_id)), len(user_id), ctypes.byref(ctypes.create_string_buffer(sign_key)), len(sign_key), ctypes.byref(self.transport_)); else: self.session_ctx=ssession_create(ctypes.byref(ctypes.create_string_buffer(user_id)), len(user_id), ctypes.byref(ctypes.create_string_buffer(sign_key)), len(sign_key), 0); if self.session_ctx==None: raise exception.themis_exception("secure_session_create fail");
def encrypt(self, message, context=None): if context == None: context_length = 0 else: context_length = len(context) encrypted_message_length = c_int(0) if themis.themis_secure_cell_encrypt_full( self.key, len(self.key), context, context_length, message, len(message), None, byref(encrypted_message_length)) != -4: raise themis_exception( "themis_secure_cell_encrypt_full (encrypted message length determination) error" ) encrypted_message = create_string_buffer( encrypted_message_length.value) if themis.themis_secure_cell_encrypt_full( self.key, len(self.key), context, context_length, message, len(message), encrypted_message, byref(encrypted_message_length)) != 0: raise themis_exception("themis_secure_cell_encrypt_full error") return string_at(encrypted_message, encrypted_message_length.value)
def encrypt(self, message): encrypted_message_length = c_int(0) context_length = c_int(0) if ( themis.themis_secure_cell_encrypt_token_protect( self.key, len(self.key), None, 0, message, len(message), None, byref(context_length), None, byref(encrypted_message_length), ) != -4 ): raise themis_exception( THEMIS_CODES.FAIL, "themis_secure_cell_encrypt_token_protect (encrypted message and context length determination) error", ) encrypted_message = create_string_buffer(encrypted_message_length.value) context = create_string_buffer(context_length.value) if ( themis.themis_secure_cell_encrypt_token_protect( self.key, len(self.key), None, 0, message, len(message), context, byref(context_length), encrypted_message, byref(encrypted_message_length), ) != 0 ): raise themis_exception(THEMIS_CODES.FAIL, "themis_secure_cell_encrypt_token_protect error") return (string_at(encrypted_message, encrypted_message_length.value), string_at(context, context_length))
def encrypt(self, message): encrypted_message_length = c_int(0) context_length = c_int(0) if themis.themis_secure_cell_encrypt_auto_split( self.key, len(self.key), None, 0, message, len(message), None, byref(context_length), None, byref(encrypted_message_length)) != -4: raise themis_exception( "themis_secure_cell_encrypt_auto_split (encrypted message and context length determination) error" ) encrypted_message = create_string_buffer( encrypted_message_length.value) context = create_string_buffer(context_length.value) if themis.themis_secure_cell_encrypt_auto_split( self.key, len(self.key), None, 0, message, len(message), context, byref(context_length), encrypted_message, byref(encrypted_message_length)) != 0: raise themis_exception( "themis_secure_cell_encrypt_auto_split error") return (string_at(encrypted_message, encrypted_message_length.value), string_at(context, context_length))
def decrypt(self, message, context): decrypted_message_length = c_int(0) res = themis.themis_secure_cell_decrypt_token_protect( self.key, len(self.key), None, 0, message, len(message), context, len(context), None, byref(decrypted_message_length), ) if res != -4: raise themis_exception( THEMIS_CODES.FAIL, "themis_secure_cell_decrypt_token_protect (decrypted message length determination) error " + ` res `, ) decrypted_message = create_string_buffer(decrypted_message_length.value) if ( themis.themis_secure_cell_decrypt_token_protect( self.key, len(self.key), None, 0, message, len(message), context, len(context), decrypted_message, byref(decrypted_message_length), ) != 0 ): raise themis_exception(THEMIS_CODES.FAIL, "themis_secure_cell_decrypt_token_protect error") return string_at(decrypted_message, decrypted_message_length.value)
def send(self, message): send_message=ctypes.create_string_buffer(message); res = themis.secure_session_send(self.session_ctx, ctypes.byref(send_message), len(message)); if res == -2222: raise exception.themis_exception("secure_session_send failed: " + str(res)); return res;
def connect(self): res = themis.secure_session_connect(self.session_ctx); if res != 0: raise exception.themis_exception("secure_session_connect failed: " + str(res));
def connect(self): res = themis.secure_session_connect(self.session_ctx); if res != THEMIS_CODES.SUCCESS: raise exception.themis_exception(res, "secure_session_connect failed");
def send(self, message): send_message=ctypes.create_string_buffer(message); res = themis.secure_session_send(self.session_ctx, ctypes.byref(send_message), len(message)); if res == THEMIS_CODES.NETWORK_ERROR: raise exception.themis_exception(res, "secure_session_send failed"); return res;