Skip to content
Snippets Groups Projects
Commit 433c7b23 authored by Thai Duong's avatar Thai Duong
Browse files

Basic infrastructure.

- Directory structure follows TensorFlow.
- Rules for protobuf building are based on https://github.com/pubref/rules_protobuf, as I can't find anything simpler.
- Continuous integration with Kokoro (work in progress).

I also copied some existing Java/CC code from Bartosz and Daniel's experimental folders.

Change-Id: I7f993934d149c114320e3388fee84fbdafd7ba81
parents
No related branches found
No related tags found
No related merge requests found
.DS_Store
/bazel-*
*.swp
# Cloud Crypto SDK
An open-source SDK that provides cloud customers with cryptographic
functionalities needed to extend key management offering of Cloud KMS.
In particular, Cloud KMS needs support for “Envelope Encryption”, i.e., a
client-side encryption of data with user-generated keys protected by KMS
encryption: cloud user generates a data encryption key (DEK) locally,
encrypts data with DEK, sends DEK to Storky to be encrypted (with a key
managed by Storky), and stores encrypted DEK with encrypted data; at a later
point user can retrieve encrypted data and DEK, use Storky to decrypt DEK,
and use decrypted DEK to decrypt the data. A guiding principles for the
design of the SDK are security, simplicity, and resistance to user errors.
git_repository(
name = "org_pubref_rules_protobuf",
remote = "https://github.com/pubref/rules_protobuf",
# need this to fix https://github.com/pubref/rules_protobuf/issues/50
commit = "be63ed9cb3140ec23e4df5118fca9a3f98640cf6",
)
load("@org_pubref_rules_protobuf//java:rules.bzl", "java_proto_repositories")
java_proto_repositories()
load("@org_pubref_rules_protobuf//cpp:rules.bzl", "cpp_proto_repositories")
cpp_proto_repositories()
cc/BUILD 0 → 100644
COMMON_DEPS = [
# this library provides Status, StatusOr, StringPiece, etc.
"@com_github_google_protobuf//:protobuf_lite",
]
cc_library(
name = "aead",
hdrs = ["public/aead.h"],
deps = COMMON_DEPS,
)
# tests
cc_test(
name = "k2proto_test",
size = "small",
srcs = ["k2proto_test.cc"],
copts = ["-Iexternal/gtest/include"],
deps = [
"//proto:cc",
"@gtest//:gtest",
],
)
#include "gtest/gtest.h"
#include "proto/k2.pb.h"
using cloud::k2::Keyset;
class K2ProtoTest : public ::testing::Test {
protected:
virtual void SetUp() {
}
virtual void TearDown() {
}
};
TEST_F(K2ProtoTest, testKeysetBasic) {
Keyset keyset;
keyset.set_primary_key_id(1);
EXPECT_EQ(1, keyset.primary_key_id());
}
int main(int ac, char* av[]) {
testing::InitGoogleTest(&ac, av);
return RUN_ALL_TESTS();
}
// AEAD primitive (Authenticated Encryption with Associated Data, RFC 5116).
// TODO(przydatek): add documentation.
#ifndef K2_PUBLIC_AEAD_H_
#define K2_PUBLIC_AEAD_H_
#include "google/protobuf/stubs/stringpiece.h"
#include "google/protobuf/stubs/statusor.h"
namespace cloud {
namespace k2 {
using google::protobuf::util::StatusOr;
using google::protobuf::StringPiece;
class Aead {
public:
virtual StatusOr<std::string> Encrypt(
StringPiece plaintext, StringPiece associated_data) const = 0;
virtual StatusOr<std::string> Decrypt(
StringPiece ciphertext, StringPiece associated_data) const = 0;
virtual ~Aead() {}
};
} // namespace k2
} // namespace cloud
#endif // K2_PUBLIC_AEAD_H_
java_library(
name = "aead",
srcs = [
"src/main/java/com/google/cloud/k2/Aead.java",
],
)
# tests
java_test(
name = "K2ProtoTest",
size = "small",
srcs = ["src/test/java/com/google/cloud/k2/K2ProtoTest.java"],
test_class = "com.google.cloud.k2.K2ProtoTest",
deps = [
"//proto:java",
"@junit_junit_4//jar",
],
)
package com.google.cloud.k2;
import java.security.GeneralSecurityException;
/**
* The interface for authenticated encryption with additional authenticated
* data. Implementations of this interface are secure against adaptive chosen
* ciphertext attacks.
*/
public interface Aead {
byte[] encrypt(byte[] plaintext, byte[] aad) throws GeneralSecurityException;
byte[] decrypt(byte[] ciphertext, byte[] aad) throws GeneralSecurityException;
}
package com.google.cloud.k2;
import static org.junit.Assert.assertEquals;
import com.google.cloud.k2.K2Proto.Keyset;
import org.junit.Test;
/**
* A simple integration test to see whether protobuf is built correctly.
*/
public class K2ProtoTest {
@Test
public void testKeysetBasic() throws Exception {
Keyset keyset = Keyset.newBuilder()
.setPrimaryKeyId(1)
.build();
assertEquals(1, keyset.getPrimaryKeyId());
}
}
package(default_visibility = ["//visibility:public"])
load("@org_pubref_rules_protobuf//cpp:rules.bzl", "cc_proto_library")
load("@org_pubref_rules_protobuf//java:rules.bzl", "java_proto_library")
filegroup(
name = "protos",
srcs = ["k2.proto"],
)
cc_proto_library(
name = "cc",
protos = [":protos"],
verbose = 0,
with_grpc = False,
imports = [
"external/com_github_google_protobuf/src/",
],
inputs = [
"@com_github_google_protobuf//:well_known_protos",
],
)
java_proto_library(
name = "java",
protos = [":protos"],
verbose = 0,
with_grpc = False,
imports = [
"external/com_github_google_protobuf/src/",
],
inputs = [
"@com_github_google_protobuf//:well_known_protos",
],
)
// Definitions for K2 library.
syntax = "proto3";
package cloud.k2;
import "google/protobuf/any.proto";
import "google/protobuf/timestamp.proto";
option java_package = "com.google.cloud.k2";
option java_outer_classname = "K2Proto";
// Each instantiation of a K2 primitive is identified by key_type,
// which is a global URL pointing to a *Key proto that holds key material
// and other parameters of the instantiation. For each key_type, in addition
// to the *Key proto, there exist two related structures:
// 1. *Params: parameters of an instantiation of the primitive,
// needed when a key is being used.
// 2. *KeyFormat: parameters needed to generate a new key; these
// include the corresponding Params, since when a factory generates
// a key based on KeyFormat, it must add Params to the resulting
// key proto with the actual key material.
// The actual *KeyFormat proto is wrapped in a KeyFormat message.
message KeyFormat {
string key_type = 1;
google.protobuf.Any format = 2; // Contains specific *KeyFormat proto.
}
// Each *Key proto by convention contains a version field, which
// identifies the version of implementation that can work with this key.
// message SomeInstantiationKey {
// uint32 version = 1;
// ...
// }
// Version is a monotonic counter: each implementation of a primitive
// has its associated "current version", which starts at 0 and is incremented
// upon updates of the code/key proto. A key with version n needs
// an implementation version n or higher to work.
// For public key primitives, the public and private keys are distinct entities
// and represent distinct primitives. However, by convention, the private key
// of a public-key primitive contains the corresponding public key proto.
// TODO(przydatek): consider following convention:
// If the key_type does not need parameters then it should
// be possible to skip the format buffer.
// If the key_type does have parameters then we should make the
// format required.
// A K2 user works usually not with single keys, but with keysets,
// to enable key rotation. The keys in a keyset can belong to different
// implementations/key types, but must all implement the same primitive.
// Any given keyset (and any given key) can be used for one primitive only.
message Keyset {
enum KeyStatus {
UNKNOWN_STATUS = 0;
ENABLED = 1; // Can be used for crypto operations.
DISABLED = 2; // Cannot be used, but exists and can become ENABLED.
DESTROYED = 3; // Key data does not exist in this Keyset any more.
}
message Key {
// Contains the actual, instantiation specific key proto.
// By convention, each key proto contains a version field.
google.protobuf.Any key_data = 1;
KeyStatus status = 2;
// Identifies a key within a keyset, is a part of metadata
// of a ciphertext/signature.
int32 key_id = 3;
// Note: info for key management is still work-in-progress (go/k2-km)
// TODO(przydatek): update this proto once go/k2-km is stable.
// Optional fields, used by key management tools, but not used
// by actual cryptographic operations.
google.protobuf.Timestamp generated_at = 101;
google.protobuf.Timestamp valid_until = 102;
}
// Provides the parameters necessary to generate a new key.
// TODO(przydatek): consider keeping the format outside of a keyset.
// Required.
KeyFormat key_format = 2;
// Identifies key used to generate new crypto data (encrypt, sign).
// Required.
int32 primary_key_id = 3;
// Actual keys in the Keyset.
// Required.
repeated Key keys = 4;
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment