Skip to content

Commit b4bb10d

Browse files
author
Mélanie Marques
committed
feat(key_manager): add documentation about creating and verify digital signatures
1 parent 3c8842d commit b4bb10d

File tree

3 files changed

+248
-0
lines changed

3 files changed

+248
-0
lines changed

menu/navigation.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -730,6 +730,14 @@
730730
{
731731
"label": "Encrypting and decrypting data streams with Streaming AEAD, Tink and Key Manager",
732732
"slug": "encrypt-decrypt-keys-with-streaming-aead-tink"
733+
},
734+
{
735+
"label": "Encrypting and decrypting data with an asymmetric key",
736+
"slug": "encrypt-decrypt-asymmetric-key-with-go-sdk"
737+
},
738+
{
739+
"label": "Create and validate signatures using Scaleway Go SDK and Key Manager",
740+
"slug": "sign-verify-key-with-go-sdk"
733741
}
734742
],
735743
"label": "API/CLI",
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
h1: Encrypting and decrypting data with an asymmetric key
2+
paragraph: Learn how to encrypt and decrypt data with an asymmetric key using Key Manager with Scaleway Go SDK.
3+
tags: key sensitive-data encrypt decrypt asymmetric digest
4+
dates:
5+
validation: 2025-02-06
6+
posted: 2025-02-06
7+
---
8+
9+
Scaleway's Key Manager provides a secure way to manage asymmetric keys, allowing you to offload sensitive cryptographic
10+
operations to a managed service. In this guide, you'll learn how to integrate the Scaleway Go SDK to encrypt and decrypt
11+
data using an rsa_oaep_3072_sha256 key directly through the Key Manager API.
12+
13+
## Configuring your environment variables
14+
15+
Configuring your environment variables allows the Go application to authenticate and use Scaleway's API and Key Manager.
16+
17+
Open a terminal and paste the following commands to export your environment variables. Make sure that you add your **own variables**. You can also use a Scaleway configuration file.
18+
19+
```bash
20+
export SCW_ACCESS_KEY="<API-ACCESS-KEY>"
21+
export SCW_SECRET_KEY="<API-SECRET-KEY>"
22+
export SCW_DEFAULT_ORGANIZATION_ID="<Scaleway-Organization-ID>"
23+
export SCW_DEFAULT_REGION="<region>"
24+
export SCW_API_URL="<api-URL>"
25+
```
26+
27+
## Encrypt data
28+
29+
This operation takes place locally, ensuring the plaintext message never leaves your environment unprotected.
30+
The public key can be fetched using the Key Manager API, parsed, and used to encrypt data with RSA-OAEP and SHA-256 padding.
31+
32+
```golang
33+
// encryptAsymmetric encrypts data on your local machine using an 'rsa_oaep_3072_sha256' key retrieved from Scaleway KMS.
34+
//
35+
// Parameters:
36+
// - keyID: The unique identifier of the asymmetric key stored in Scaleway KMS.
37+
// - message: The plaintext message that needs to be encrypted.
38+
//
39+
// Returns:
40+
// - error: An error if the encryption process fails, otherwise nil.
41+
func encryptAsymmetric(keyID string, message string) error {
42+
// Initialize the Scaleway client
43+
client, err := scw.NewClient(scw.WithEnv())
44+
if err != nil {
45+
panic(err)
46+
}
47+
kmsApi := key_manager.NewAPI(client)
48+
49+
// Retrieve the public key from Scaleway KMS.
50+
response, err := kmsApi.GetPublicKey(&key_manager.GetPublicKeyRequest{
51+
KeyID: keyID,
52+
})
53+
if err != nil {
54+
return fmt.Errorf("failed to get public key: %w", err)
55+
}
56+
57+
// Parse the public key. Note, this example assumes the public key is in the
58+
// RSA format.
59+
block, _ := pem.Decode([]byte(response.Pem))
60+
publicKey, err := x509.ParsePKCS1PublicKey(block.Bytes)
61+
if err != nil {
62+
return fmt.Errorf("failed to parse public key: %w", err)
63+
}
64+
65+
// Convert the message into bytes. Cryptographic plaintexts and
66+
// ciphertexts are always byte arrays.
67+
plaintext := []byte(message)
68+
69+
// Encrypt data using the RSA public key.
70+
ciphertext, err := rsa.EncryptOAEP(sha256.New(), rand.Reader, publicKey, plaintext, nil)
71+
if err != nil {
72+
return fmt.Errorf("rsa.EncryptOAEP: %w", err)
73+
}
74+
75+
fmt.Printf("Encrypted ciphertext: %s\n", ciphertext)
76+
return nil
77+
}
78+
```
79+
80+
## Decrypt data
81+
82+
To retrieve the original message, you must send the encrypted ciphertext to Scaleway Key Manager,
83+
which uses the private portion of the asymmetric key to decrypt it. This ensures your private key remains secure within
84+
Scaleway’s infrastructure.
85+
86+
```golang
87+
88+
// decryptAsymmetric attempts to decrypt a given ciphertext using an 'rsa_oaep_3072_sha256' key from Scaleway KMS.
89+
//
90+
// Parameters:
91+
// - keyID: The unique identifier of the asymmetric key stored in Scaleway KMS.
92+
// - ciphertext: The encrypted data that needs to be decrypted.
93+
//
94+
// Returns:
95+
// - error: An error if the decryption process fails, otherwise nil.
96+
func decryptAsymmetric(keyID string, ciphertext []byte) error {
97+
// Initialize the Scaleway client
98+
client, err := scw.NewClient(scw.WithEnv())
99+
if err != nil {
100+
panic(err)
101+
}
102+
kmsApi := key_manager.NewAPI(client)
103+
104+
// Build the request.
105+
req := &key_manager.DecryptRequest{
106+
KeyID: keyID,
107+
Ciphertext: ciphertext,
108+
}
109+
110+
// Call the API.
111+
result, err := kmsApi.Decrypt(req)
112+
if err != nil {
113+
return fmt.Errorf("failed to decrypt ciphertext: %w", err)
114+
}
115+
116+
fmt.Printf("Decrypted plaintext: %s", result.Plaintext)
117+
return nil
118+
}
119+
```
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
---
2+
meta:
3+
title: Create and validate signatures using Scaleway Go SDK and Key Manager
4+
description: Learn how to create and validate signature using Key Manager with Scaleway Go SDK.
5+
content:
6+
h1: Create and validate signatures using Scaleway Go SDK and Key Manager
7+
paragraph: Learn how to create and validate signature using Key Manager with Scaleway Go SDK.
8+
tags: key sensitive-data signature verification sign verify digest
9+
dates:
10+
validation: 2025-02-06
11+
posted: 2025-02-06
12+
---
13+
14+
This page shows you how to perform asymmetric signing and verification using Scaleway Key Manager (KMS).
15+
16+
In this tutorial, we will walk you through setting up your environment to interact with the Scaleway API and Key Manager,
17+
and then demonstrate how to sign and verify messages using asymmetric keys.
18+
19+
## Configuring your environment variables
20+
21+
Configuring your environment variables allows the Go application to authenticate and use Scaleway's API and Key Manager.
22+
23+
Open a terminal and paste the following commands to export your environment variables. Make sure that you add your **own variables**. You can also use a Scaleway configuration file.
24+
25+
```bash
26+
export SCW_ACCESS_KEY="<API-ACCESS-KEY>"
27+
export SCW_SECRET_KEY="<API-SECRET-KEY>"
28+
export SCW_DEFAULT_ORGANIZATION_ID="<Scaleway-Organization-ID>"
29+
export SCW_DEFAULT_REGION="<region>"
30+
export SCW_API_URL="<api-URL>"
31+
```
32+
33+
## Creating a signature
34+
35+
```golang
36+
// signAsymmetric signs a plaintext message using a saved asymmetric private key 'ec_p256_sha256'
37+
// stored in Scaleway Key Manager.
38+
//
39+
// Parameters:
40+
// - keyID: The unique identifier of the asymmetric key stored in Scaleway Key Manager.
41+
// - message: The plaintext message that needs to be signed.
42+
//
43+
// Returns:
44+
// - err: An error if the signing process fails, otherwise nil.
45+
func signAsymmetric(keyID string, message string) error {
46+
// Initialize the Scaleway client
47+
client, err := scw.NewClient(scw.WithEnv())
48+
if err != nil {
49+
panic(err)
50+
}
51+
kmsApi := key_manager.NewAPI(client)
52+
53+
// Convert the message into bytes. Cryptographic plaintexts and ciphertexts are always byte arrays.
54+
plaintext := []byte(message)
55+
56+
// Calculate the digest of the message.
57+
digest := sha256.New()
58+
if _, err = digest.Write(plaintext); err != nil {
59+
return fmt.Errorf("failed to create digest: %w", err)
60+
}
61+
62+
// Build the signing request.
63+
req := &key_manager.SignRequest{
64+
Digest: digest.Sum(nil),
65+
KeyID: keyID,
66+
}
67+
68+
// Call the API
69+
response, err = kmsApi.Sign(req)
70+
if err != nil {
71+
return fmt.Errorf("failed to sign digest: %w", err)
72+
}
73+
74+
fmt.Printf("Signed digest: %s", response.Signature)
75+
return nil
76+
}
77+
```
78+
79+
## Validating an elliptic curve signature
80+
81+
```golang
82+
// verifyAsymmetricSignature verifies that an 'ec_p256_sha256' signature is valid for a given message.
83+
//
84+
// Parameters:
85+
// - keyID: The unique identifier of the asymmetric key stored in Scaleway Key Manager.
86+
// - message: The plaintext message that was originally signed.
87+
// - signature: The signature obtained from a previous sign request.
88+
//
89+
// Returns:
90+
// - error: An error if the verification process fails or if the signature is invalid, otherwise nil.
91+
func verifyAsymmetricSignature(keyID string, message, signature []byte) error {
92+
// Initialize the Scaleway client
93+
client, err := scw.NewClient(scw.WithEnv())
94+
if err != nil {
95+
panic(err)
96+
}
97+
kmsApi := key_manager.NewAPI(client)
98+
99+
// Verify signature.
100+
// Calculate the digest of the message.
101+
digest := sha256.New()
102+
if _, err = digest.Write(message); err != nil {
103+
return fmt.Errorf("failed to create digest: %w", err)
104+
}
105+
106+
verify, err := kmsApi.Verify(&key_manager.VerifyRequest{
107+
KeyID: keyID,
108+
Digest: digest.Sum(nil),
109+
Signature: signature,
110+
})
111+
if err != nil {
112+
return err
113+
}
114+
115+
if verify.Valid {
116+
fmt.Printf("Verified signature!")
117+
}
118+
119+
return nil
120+
}
121+
```

0 commit comments

Comments
 (0)