Search code examples
c++bazeltink

How to build Google tink library using bazel in C++?


I want to use Google tink library for my app. I bound my code using android ndk. However, I couldn't build Google tink library with bazel. I have tried their examples on GitHub (https://github.com/google/tink). Moreover, I don't have any experience with bazel. Hence, if anyone knows how to build Google tink library in any way I am open to those options too.

I am using:

  • Ubuntu 20.04
  • Bazel 3.1.0
  • gcc verion is 9.3.0

I have tried to follow https://github.com/google/tink/blob/master/examples/cc/helloworld/README.md but it resulted in an error:

bazel build ...

Starting local Bazel server and connecting to it...
ERROR: error loading package 'javascript/aead/internal': Unable to find package for @npm//@bazel/typescript:index.bzl: The repository '@npm' could not be resolved.
INFO: Elapsed time: 6.455s
INFO: 0 processes.
FAILED: Build did NOT complete successfully (1 packages loaded)
    currently loading: javascript/binary ... (6 packages)

Solution

  • When the README.md was written, the entire project was in a single Bazel workspace. Nowadays the examples subdirectory is it's own separate workspace, so you need to cd into it before executing bazel.

    Correct instructions:

    # Build the code.
    git clone https://github.com/google/tink
    cd tink/examples/cc
    bazel build ...
    
    # Create some input.
    echo "some plaintext" > foo.txt
    
    # Encrypt.
    ./bazel-bin/helloworld/hello_world ./helloworld/aes128_gcm_test_keyset_json.txt \
        encrypt foo.txt "some aad" foo.encrypted
    
    # Decrypt.
    ./bazel-bin/helloworld/hello_world ./helloworld/aes128_gcm_test_keyset_json.txt \
        decrypt foo.encrypted "some aad" foo-decrypted.txt
    
    # Inspect the output.
    cat foo-decrypted.txt
    

    Note, the C++ implementation also supports using CMake, which may be easier to ramp up with for your purposes.