pythonpost-quantum-cryptography

liboqs-python throws AttributeError: module 'oqs' has no attribute 'get_enabled_kem_mechanisms'


I'm trying to get this Open Quantum Safe example working: https://github.com/open-quantum-safe/liboqs-python/blob/main/examples/kem.py

I'm getting this error:

(myenv) user@mx:~
$ python3 '/home/user/Documents/Dev/quantum_algo_tests/# Key encapsulation Python example.py' 
Enabled KEM mechanisms:
Traceback (most recent call last):
  File "/home/user/Documents/Dev/quantum_algo_tests/# Key encapsulation Python example.py", line 9, in <module>
    kems = oqs.get_enabled_kem_mechanisms()
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
AttributeError: module 'oqs' has no attribute 'get_enabled_kem_mechanisms'
(myenv) user@mx:~
$

Running inspect to list functions I get:

ExpressionInput
FunctionNode
OQSInterpreter
oqs_engine

I tried a basic example from following the documentation:

import oqs
from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
import os
import base64

# Message to be encrypted
message = "Hello world!"

# Step 1: Key Encapsulation using Kyber
kemalg = "Kyber512"
with oqs.KeyEncapsulation(kemalg) as client:
    # Client generates keypair
    public_key = client.generate_keypair()

    # Server (could be another party) encapsulates secret with client's public key
    with oqs.KeyEncapsulation(kemalg) as server:
        ciphertext, shared_secret_server = server.encap_secret(public_key)
    
    # Client decapsulates to get the same shared secret
    shared_secret_client = client.decap_secret(ciphertext)

# The shared secret is now the same for both client and server and will be used as the encryption key
assert shared_secret_client == shared_secret_server, "Shared secrets do not match!"

# Step 2: Encrypt the message using AES-GCM with the shared secret
iv = os.urandom(12)
cipher = Cipher(algorithms.AES(shared_secret_client), modes.GCM(iv), backend=None)
encryptor = cipher.encryptor()
ciphertext = encryptor.update(message.encode()) + encryptor.finalize()

# The tag ensures the integrity of the message
tag = encryptor.tag

# Combine the IV, ciphertext, and tag into a single encrypted package
encrypted_message = base64.b64encode(iv + ciphertext + tag)

print(f"Encrypted message: {encrypted_message.decode()}")

# Step 3: Decryption process (using the shared secret derived from Kyber)

# Decrypt the message
decryptor = Cipher(algorithms.AES(shared_secret_server), modes.GCM(iv, tag), backend=None).decryptor()
decrypted_message = decryptor.update(ciphertext) + decryptor.finalize()

print(f"Decrypted message: {decrypted_message.decode()}")

I get the same error:

$ python3 /home/user/Documents/Dev/quantum_algo_tests/hello_world_example.py 
Traceback (most recent call last):
  File "/home/user/Documents/Dev/quantum_algo_tests/hello_world_example.py", line 11, in <module>
    with oqs.KeyEncapsulation(kemalg) as client:
         ^^^^^^^^^^^^^^^^^^^^
AttributeError: module 'oqs' has no attribute 'KeyEncapsulation'

Why is such a simple program throwing this error?


Solution

  • Chances are, liboqs just wasn't installed correctly. The GitHub page gives several installation steps, some of which are contradictory and could break your install. (When I tried following the instructions from top to bottom, I got a version conflict due to incompatible installs of oqs and liboqs-python.)

    The safest option is to exclusively follow the "Let liboqs-python install liboqs automatically" section and ignore everything preceding and following it.

    Just activate your desired venv, then paste the following lines into your terminal:

    git clone --depth=1 https://github.com/open-quantum-safe/liboqs-python
    cd liboqs-python
    pip install .