def test_aes_gcm(self): key = b'Sixteen byte key' plain_text = b'Attack at dawn' hdr = b'To your eyes only' nonce, mac, cipher_text = AESHandler.aes_gcm_encrypt(plain_text, hdr, key) decrypt_out = AESHandler.aes_gcm_decrypt(cipher_text, hdr, nonce, mac, key) self.assertEqual(plain_text, decrypt_out)
def decrypt_with_gcm_mode(nonce: bytes, mac_tag: bytes, cipher_text: bytes, private_key: bytes, hdr: bytes, encode_g_tilde: bytes): if not isinstance(private_key, bytes): raise SDKException( ErrorCode.other_error( 'the length of private key should be 32 bytes.')) if len(private_key) != 32: raise SDKException( ErrorCode.other_error( 'the length of private key should be 32 bytes.')) str_g_tilde_x = encode_g_tilde[1:33] str_g_tilde_y = encode_g_tilde[33:65] g_tilde_x = string_to_number(str_g_tilde_x) g_tilde_y = string_to_number(str_g_tilde_y) g_tilde = Point(NIST256p.curve, g_tilde_x, g_tilde_y, NIST256p.order) h_tilde = g_tilde * SigningKey.from_string( string=private_key, curve=NIST256p).privkey.secret_multiplier seed = b''.join( [encode_g_tilde, number_to_string(h_tilde.x(), NIST256p.order)]) aes_key = pbkdf2(seed, 32) plain_text = AESHandler.aes_gcm_decrypt(cipher_text, hdr, nonce, mac_tag, aes_key) return plain_text
def decrypt_with_gcm_mode(nonce: bytes, mac_tag: bytes, cipher_text: bytes, private_key: bytes, hdr: bytes, encode_g_tilde: bytes): aes_key = ECIES.generate_decrypt_aes_key(private_key, encode_g_tilde) plain_text = AESHandler.aes_gcm_decrypt(cipher_text, hdr, nonce, mac_tag, aes_key) return plain_text