-
Notifications
You must be signed in to change notification settings - Fork 0
/
dek-example_test.go
80 lines (71 loc) · 2.54 KB
/
dek-example_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
// Copyright 2021 - MinIO, Inc. All rights reserved.
// Use of this source code is governed by the AGPLv3
// license that can be found in the LICENSE file.
package kes_test
import (
"context"
"crypto/aes"
"crypto/cipher"
"crypto/rand"
"crypto/tls"
"io"
"log"
"github.com/minio/kes"
)
func ExampleClient_GenerateKey() {
// First, load the client TLS private key / certificate to
// authenticate against the KES server.
const (
keyFile = "./root.key"
certFile = "./root.cert"
)
certificate, err := tls.LoadX509KeyPair(certFile, keyFile)
if err != nil {
log.Fatalf("Failed to load TLS certificate for client (mTLS) authentication: %v", err)
}
// Then, generate a new data encryption key (DEK). The DEK contains a
// plaintext key as well as a ciphertext version. The ciphertext is the
// plaintext key encrypted by the KES server with the key named 'keyName'.
// Only the KES server can decrypt the ciphertext key.
const (
endpoint = "https://play.min.io:7373"
keyName = "my-key"
)
client := kes.NewClient(endpoint, certificate)
key, err := client.GenerateKey(context.Background(), keyName, nil)
if err != nil {
log.Fatalf("Failed to generate a new data encryption key: %v", err)
}
// Finally, use AES-GCM to encrypt a short message using the plaintext key.
// The actual ciphertext, the encrypted key, the nonce and the associated data
// can be stored on some untrusted location. The ciphertext can only be decrypted
// by contacting the KES server - once the plaintext key is no longer accessible.
block, err := aes.NewCipher(key.Plaintext)
if err != nil {
log.Fatalf("Failed to create AES instance: %v", err)
}
gcm, err := cipher.NewGCM(block)
if err != nil {
log.Fatalf("Failed to create AES-GCM instance: %v", err)
}
var (
message = []byte("Hello World")
nonce = mustRandom(rand.Reader, gcm.NonceSize())
associatedData = []byte("my-file.text")
)
ciphertext := gcm.Seal(nil, nonce, message, associatedData)
// Now store the ciphertext as well as the key.Ciphertext, the nonce
// and the associatedData. The key.Ciphertext contains the encrypted
// version of the key used to encrypt the message.
// It needs to be sent to the KES server to obtain the plaintext key
// which is needed to decrypt the ciphertext (using the nonce and
// associatedData) and obtain the message again.
_, _, _, _ = ciphertext, key.Ciphertext, nonce, associatedData
}
func mustRandom(random io.Reader, size int) []byte {
v := make([]byte, size)
if _, err := io.ReadFull(random, v); err != nil {
panic(err)
}
return v
}