-
Notifications
You must be signed in to change notification settings - Fork 4
/
main_qsc.go
182 lines (156 loc) · 4.74 KB
/
main_qsc.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
// Copyright 2019 IBM Corp.
//
// 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
//
// http://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.
// +build quantum
package main
import (
"context"
"fmt"
"strings"
kp "github.com/IBM/keyprotect-go-client"
)
func getQSCConfig() kp.ClientQSCConfig {
return kp.ClientQSCConfig{
AlgorithmID: kp.KP_QSC_ALGO_KYBER512,
}
}
func getConfigAuthToken() kp.ClientConfig {
return kp.ClientConfig{
BaseURL: kp.DefaultBaseQSCURL,
Authorization: "",
InstanceID: "",
Verbose: kp.VerboseFailOnly,
}
}
func getConfigAPIKey() kp.ClientConfig {
return kp.ClientConfig{
BaseURL: kp.DefaultBaseQSCURL,
APIKey: "",
TokenURL: kp.DefaultTokenURL,
InstanceID: "",
Verbose: kp.VerboseFailOnly,
}
}
func standardKeyOperations(api *kp.API) {
fmt.Println("\nCreating standard key")
rootkey, err := api.CreateStandardKey(context.Background(), "mynewstandardkey", nil)
if err != nil {
fmt.Println("Error while creating standard key: ", err)
return
}
fmt.Println("New key created: ", *rootkey)
fmt.Println("\nGetting standard key")
keyid := rootkey.ID
key, err := api.GetKey(context.Background(), keyid)
if err != nil {
fmt.Println("Get Key failed with error: ", err)
return
}
fmt.Printf("Key: %v\n", *key)
fmt.Println("\nDeleting standard key")
delKey, err := api.DeleteKey(context.Background(), keyid, kp.ReturnRepresentation)
if err != nil {
fmt.Println("Error while deleting: ", err)
return
}
fmt.Println("Deleted key: ", delKey)
}
func rootKeyOperations(api *kp.API) {
fmt.Println("\nCreating root key")
rootkey, err := api.CreateRootKey(context.Background(), "mynewrootkey", nil)
if err != nil {
fmt.Println("Error while creating root key: ", err)
return
}
fmt.Println("New key created: ", *rootkey)
fmt.Println("\nGetting root key")
keyid := rootkey.ID
key, err := api.GetKey(context.Background(), keyid)
if err != nil {
fmt.Println("Get Key failed with error: ", err)
return
}
fmt.Printf("Key: %v\n", *key)
fmt.Println("\nWrapping root key")
aad := []string{"string1", "string2", "string3"}
plaintext := []byte("NWvfrThUqP9aFmTWFgB86qztK2BuN0qIGg7K7kcCCRs=")
ciphertext, err := api.Wrap(context.Background(), keyid, plaintext, &aad)
if err != nil {
fmt.Println("Error wrapping the key: ", err)
return
}
fmt.Println("Wrapped key successfully")
fmt.Println("\nUnwrapping root key")
pt, err := api.Unwrap(context.Background(), keyid, ciphertext, &aad)
if err != nil {
fmt.Println("Error unwrapping key: ", err)
return
}
if strings.Compare(string(plaintext), string(pt)) == 0 {
fmt.Println("key wrapped and unwrapped successully")
}
fmt.Println("Creating a DEK")
DEK2, wrappedDek, err := api.WrapCreateDEK(context.Background(), keyid, nil)
if err != nil {
fmt.Println("Error while creating a DEK: ", err)
return
}
fmt.Println("Created DEK and wrapped the key")
pt2, err := api.Unwrap(context.Background(), keyid, wrappedDek, nil)
if err != nil {
fmt.Println("Error while unwrapping DEK: ", err)
return
}
if strings.Compare(string(DEK2), string(pt2)) == 0 {
fmt.Println("Unwrapped key successfully")
}
DEK1, wrappedDek1, err := api.WrapCreateDEK(context.Background(), keyid, &aad)
if err != nil {
fmt.Println("Error while creating a DEK with aad: ", err)
return
}
fmt.Println("Created DEK and wrapped the key with aad")
ptxt, err := api.Unwrap(context.Background(), keyid, wrappedDek1, &aad)
if err != nil {
fmt.Println("Error while unwrapping DEK: ", err)
return
}
if strings.Compare(string(DEK1), string(ptxt)) == 0 {
fmt.Println("Unwrapped key successfully with aad")
}
fmt.Println("\nDeleting root key")
delKey, err := api.DeleteKey(context.Background(), keyid, kp.ReturnRepresentation)
if err != nil {
fmt.Println("Error while deleting: ", err)
return
}
fmt.Println("Deleted key: ", delKey)
}
func main() {
options := getConfigAuthToken()
var l kp.Logger
api, err := kp.NewWithLogger(options, kp.DefaultTransport(), l, kp.WithQSC(getQSCConfig()))
if err != nil {
fmt.Println("Error creating kp client with QSC config")
return
}
standardKeyOperations(api)
rootKeyOperations(api)
fmt.Println("\nGetting all keys")
keys, err := api.GetKeys(context.Background(), 100, 0)
if err != nil {
fmt.Println("Get Keys failed with error: ", err)
return
}
fmt.Printf("Keys: %v\n", keys)
}