Skip to content

Commit af98f44

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

File tree

3 files changed

+262
-0
lines changed

3 files changed

+262
-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: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
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+
14+
<Message type="warning">
15+
Please note that we do not recommend using asymmetric encryption for anything other than key encryption.
16+
</Message>
17+
18+
## Configuring your environment variables
19+
20+
Configuring your environment variables allows the Go application to authenticate and use Scaleway's API and Key Manager.
21+
22+
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.
23+
24+
```bash
25+
export SCW_ACCESS_KEY="<API-ACCESS-KEY>"
26+
export SCW_SECRET_KEY="<API-SECRET-KEY>"
27+
export SCW_DEFAULT_ORGANIZATION_ID="<Scaleway-Organization-ID>"
28+
export SCW_DEFAULT_REGION="<region>"
29+
export SCW_API_URL="<api-URL>"
30+
```
31+
32+
## Encrypt data
33+
34+
This operation takes place locally, ensuring the plaintext message never leaves your environment unprotected.
35+
The public key can be fetched using the Key Manager API, parsed, and used to encrypt data with RSA-OAEP and SHA-256 padding.
36+
37+
```golang
38+
// encryptAsymmetric encrypts data on your local machine using an 'rsa_oaep_3072_sha256' key retrieved from Scaleway KMS.
39+
//
40+
// Parameters:
41+
// - keyID: The unique identifier of the asymmetric key stored in Scaleway KMS.
42+
// - message: The plaintext message that needs to be encrypted.
43+
//
44+
// Returns:
45+
// - error: An error if the encryption process fails, otherwise nil.
46+
func encryptAsymmetric(keyID string, message string) error {
47+
// Initialize the Scaleway client
48+
client, err := scw.NewClient(scw.WithEnv())
49+
if err != nil {
50+
panic(err)
51+
}
52+
kmsApi := key_manager.NewAPI(client)
53+
54+
// Retrieve the public key from Scaleway KMS.
55+
response, err := kmsApi.GetPublicKey(&key_manager.GetPublicKeyRequest{
56+
KeyID: keyID,
57+
})
58+
if err != nil {
59+
return fmt.Errorf("failed to get public key: %w", err)
60+
}
61+
62+
// Parse the public key. Note, this example assumes the public key is in the
63+
// RSA format.
64+
block, _ := pem.Decode([]byte(response.Pem))
65+
publicKey, err := x509.ParsePKCS1PublicKey(block.Bytes)
66+
if err != nil {
67+
return fmt.Errorf("failed to parse public key: %w", err)
68+
}
69+
70+
// Convert the message into bytes. Cryptographic plaintexts and
71+
// ciphertexts are always byte arrays.
72+
plaintext := []byte(message)
73+
74+
// Encrypt data using the RSA public key.
75+
ciphertext, err := rsa.EncryptOAEP(sha256.New(), rand.Reader, publicKey, plaintext, nil)
76+
if err != nil {
77+
return fmt.Errorf("rsa.EncryptOAEP: %w", err)
78+
}
79+
80+
fmt.Printf("Encrypted ciphertext: %s\n", ciphertext)
81+
return nil
82+
}
83+
```
84+
85+
<Message type="note">
86+
Please note that :
87+
- Encryption can also be performed using the Scaleway's Key Manager Encrypt method.
88+
- In the case of asymmetric encryption, the maximum payload size allowed depends on the key algo (190 bytes for `RSA_OAEP_2048_SHA256`, 318 bytes for `RSA_OAEP_3072_SHA256` and 446 bytes for `RSA_OAEP_4096_SHA256`).
89+
</Message>
90+
91+
## Decrypt data
92+
93+
To retrieve the original message, you must send the encrypted ciphertext to Scaleway Key Manager,
94+
which uses the private portion of the asymmetric key to decrypt it. This ensures your private key remains secure within
95+
Scaleway’s infrastructure.
96+
97+
```golang
98+
99+
// decryptAsymmetric attempts to decrypt a given ciphertext using an 'rsa_oaep_3072_sha256' key from Scaleway KMS.
100+
//
101+
// Parameters:
102+
// - keyID: The unique identifier of the asymmetric key stored in Scaleway KMS.
103+
// - ciphertext: The encrypted data that needs to be decrypted.
104+
//
105+
// Returns:
106+
// - error: An error if the decryption process fails, otherwise nil.
107+
func decryptAsymmetric(keyID string, ciphertext []byte) error {
108+
// Initialize the Scaleway client
109+
client, err := scw.NewClient(scw.WithEnv())
110+
if err != nil {
111+
panic(err)
112+
}
113+
kmsApi := key_manager.NewAPI(client)
114+
115+
// Build the request.
116+
req := &key_manager.DecryptRequest{
117+
KeyID: keyID,
118+
Ciphertext: ciphertext,
119+
}
120+
121+
// Call the API.
122+
result, err := kmsApi.Decrypt(req)
123+
if err != nil {
124+
return fmt.Errorf("failed to decrypt ciphertext: %w", err)
125+
}
126+
127+
fmt.Printf("Decrypted plaintext: %s", result.Plaintext)
128+
return nil
129+
}
130+
```
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
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+
// Note: Digest algorithm must match the key algorithm.
58+
// - Use SHA-256 for most algorithms (e.g., RSA_OAEP_3072_SHA256, EC_P256_SHA256).
59+
// - Use SHA-384 **only** for ECC_P384_SHA384.
60+
digest := sha256.New()
61+
if _, err = digest.Write(plaintext); err != nil {
62+
return fmt.Errorf("failed to create digest: %w", err)
63+
}
64+
65+
// Build the signing request.
66+
req := &key_manager.SignRequest{
67+
Digest: digest.Sum(nil),
68+
KeyID: keyID,
69+
}
70+
71+
// Call the API
72+
response, err = kmsApi.Sign(req)
73+
if err != nil {
74+
return fmt.Errorf("failed to sign digest: %w", err)
75+
}
76+
77+
fmt.Printf("Signed digest: %s", response.Signature)
78+
return nil
79+
}
80+
```
81+
82+
## Validating an elliptic curve signature
83+
84+
```golang
85+
// verifyAsymmetricSignature verifies that an 'ec_p256_sha256' signature is valid for a given message.
86+
//
87+
// Parameters:
88+
// - keyID: The unique identifier of the asymmetric key stored in Scaleway Key Manager.
89+
// - message: The plaintext message that was originally signed.
90+
// - signature: The signature obtained from a previous sign request.
91+
//
92+
// Returns:
93+
// - error: An error if the verification process fails or if the signature is invalid, otherwise nil.
94+
func verifyAsymmetricSignature(keyID string, message, signature []byte) error {
95+
// Initialize the Scaleway client
96+
client, err := scw.NewClient(scw.WithEnv())
97+
if err != nil {
98+
panic(err)
99+
}
100+
kmsApi := key_manager.NewAPI(client)
101+
102+
// Verify signature.
103+
// Calculate the digest of the message.
104+
digest := sha256.New()
105+
if _, err = digest.Write(message); err != nil {
106+
return fmt.Errorf("failed to create digest: %w", err)
107+
}
108+
109+
verify, err := kmsApi.Verify(&key_manager.VerifyRequest{
110+
KeyID: keyID,
111+
Digest: digest.Sum(nil),
112+
Signature: signature,
113+
})
114+
if err != nil {
115+
return err
116+
}
117+
118+
if verify.Valid {
119+
fmt.Printf("Verified signature!")
120+
}
121+
122+
return nil
123+
}
124+
```

0 commit comments

Comments
 (0)