-
Notifications
You must be signed in to change notification settings - Fork 1.8k
feat(cloudkms): Add code samples for KMS KEMs. #5391
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
iontzialla
wants to merge
2
commits into
GoogleCloudPlatform:main
Choose a base branch
from
iontzialla:add-kms-kems-samples
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
// Copyright 2025 Google LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// https://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package kms | ||
|
||
// [START kms_decapsulate] | ||
import ( | ||
"context" | ||
"fmt" | ||
"hash/crc32" | ||
"io" | ||
|
||
kms "cloud.google.com/go/kms/apiv1" | ||
"cloud.google.com/go/kms/apiv1/kmspb" | ||
"google.golang.org/protobuf/types/known/wrapperspb" | ||
) | ||
|
||
// decapsulate decapsulates the given ciphertext using a saved private key of purpose | ||
// KEY_ENCAPSULATION stored in KMS. | ||
func decapsulate(w io.Writer, keyVersionName string, ciphertext []byte) error { | ||
// keyVersionName := "projects/my-project/locations/us-east1/keyRings/my-key-ring/cryptoKeys/my-key/cryptoKeyVersions/1" | ||
// ciphertext := []byte("...") | ||
|
||
// Create the client. | ||
ctx := context.Background() | ||
client, err := kms.NewKeyManagementClient(ctx) | ||
if err != nil { | ||
return fmt.Errorf("failed to create kms client: %w", err) | ||
} | ||
defer client.Close() | ||
|
||
// crc32c calculates the CRC32C checksum of the given data. | ||
crc32c := func(data []byte) uint32 { | ||
t := crc32.MakeTable(crc32.Castagnoli) | ||
return crc32.Checksum(data, t) | ||
} | ||
|
||
// Optional but recommended: Compute ciphertext's CRC32C. | ||
ciphertextCRC32C := crc32c(ciphertext) | ||
|
||
// Build the request. | ||
req := &kmspb.DecapsulateRequest{ | ||
Name: keyVersionName, | ||
Ciphertext: ciphertext, | ||
CiphertextCrc32C: wrapperspb.Int64(int64(ciphertextCRC32C)), | ||
} | ||
|
||
// Call the API. | ||
result, err := client.Decapsulate(ctx, req) | ||
if err != nil { | ||
return fmt.Errorf("failed to decapsulate: %w", err) | ||
} | ||
|
||
// Optional, but recommended: perform integrity verification on the response. | ||
// For more details on ensuring E2E in-transit integrity to and from Cloud KMS visit: | ||
// https://cloud.google.com/kms/docs/data-integrity-guidelines | ||
if !result.GetVerifiedCiphertextCrc32C() { | ||
return fmt.Errorf("Decapsulate: request corrupted in-transit") | ||
} | ||
if result.GetName() != req.GetName() { | ||
return fmt.Errorf("Decapsulate: request corrupted in-transit") | ||
} | ||
iontzialla marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if int64(crc32c(result.GetSharedSecret())) != result.GetSharedSecretCrc32C() { | ||
return fmt.Errorf("Decapsulate: response corrupted in-transit") | ||
} | ||
|
||
fmt.Fprintf(w, "Decapsulated plaintext: %x", result.GetSharedSecret()) | ||
return nil | ||
} | ||
|
||
// [END kms_decapsulate] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
// Copyright 2025 Google LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// https://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package kms | ||
|
||
// [START kms_encapsulate_mlkem] | ||
import ( | ||
"context" | ||
"crypto/mlkem" | ||
"fmt" | ||
"hash/crc32" | ||
"io" | ||
|
||
kms "cloud.google.com/go/kms/apiv1" | ||
"cloud.google.com/go/kms/apiv1/kmspb" | ||
) | ||
|
||
// encapsulateMLKEM demonstrates how to encapsulate a shared secret using an ML-KEM-768 public key | ||
// from Cloud KMS. | ||
func encapsulateMLKEM(w io.Writer, keyVersionName string) error { | ||
// keyVersionName := "projects/my-project/locations/us-east1/keyRings/my-key-ring/cryptoKeys/my-key/cryptoKeyVersions/1" | ||
|
||
// Create the client. | ||
ctx := context.Background() | ||
client, err := kms.NewKeyManagementClient(ctx) | ||
if err != nil { | ||
return fmt.Errorf("failed to create kms client: %w", err) | ||
} | ||
defer client.Close() | ||
|
||
// crc32c calculates the CRC32C checksum of the given data. | ||
crc32c := func(data []byte) uint32 { | ||
t := crc32.MakeTable(crc32.Castagnoli) | ||
return crc32.Checksum(data, t) | ||
} | ||
iontzialla marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
// Build the request to get the public key in NIST PQC format. | ||
req := &kmspb.GetPublicKeyRequest{ | ||
Name: keyVersionName, | ||
PublicKeyFormat: kmspb.PublicKey_NIST_PQC, | ||
} | ||
|
||
// Call the API to get the public key. | ||
response, err := client.GetPublicKey(ctx, req) | ||
if err != nil { | ||
return fmt.Errorf("failed to get public key: %w", err) | ||
} | ||
|
||
// Optional, but recommended: perform integrity verification on the response. | ||
// For more details on ensuring E2E in-transit integrity to and from Cloud KMS visit: | ||
// https://cloud.google.com/kms/docs/data-integrity-guidelines | ||
if response.GetName() != req.GetName() { | ||
return fmt.Errorf("GetPublicKey: request corrupted in-transit") | ||
} | ||
if response.GetPublicKeyFormat() != req.GetPublicKeyFormat() { | ||
return fmt.Errorf("GetPublicKey: request corrupted in-transit") | ||
} | ||
if int64(crc32c(response.GetPublicKey().GetData())) != response.GetPublicKey().GetCrc32CChecksum().GetValue() { | ||
return fmt.Errorf("GetPublicKey: response corrupted in-transit") | ||
} | ||
iontzialla marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
// Use the public key with crypto/mlkem to encapsulate a shared secret. | ||
ek, err := mlkem.NewEncapsulationKey768(response.GetPublicKey().GetData()) | ||
if err != nil { | ||
return fmt.Errorf("NewEncapsulationKey768: %w", err) | ||
} | ||
sharedSecret, ciphertext := ek.Encapsulate() | ||
|
||
fmt.Fprintf(w, "Encapsulated ciphertext: %x\n", ciphertext) | ||
fmt.Fprintf(w, "Shared secret: %x\n", sharedSecret) | ||
return nil | ||
} | ||
|
||
// [END kms_encapsulate_mlkem] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.