Debian Codegraph @

plugin/proto/algorithm.go

raw
1 // Copyright The Notary Project Authors.
2 // Licensed under the Apache License, Version 2.0 (the "License");
3 // you may not use this file except in compliance with the License.
4 // You may obtain a copy of the License at
5 //
6 // http://www.apache.org/licenses/LICENSE-2.0
7 //
8 // Unless required by applicable law or agreed to in writing, software
9 // distributed under the License is distributed on an "AS IS" BASIS,
10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 // See the License for the specific language governing permissions and
12 // limitations under the License.
13
14 package proto
15
16 import (
17 "errors"
18 "fmt"
19
20 "github.com/notaryproject/notation-core-go/signature"
21 "github.com/notaryproject/notation-plugin-framework-go/plugin"
22 )
23
24 // KeySpec is type of the signing algorithm, including algorithm and size.
25 //
26 // Deprecated: KeySpec exists for historical compatibility and should not be used.
27 // To access KeySpec, use the notation-plugin-framework-go's [plugin.KeySpec] type.
28 type KeySpec = plugin.KeySpec
29
30 // one of the following supported key spec names.
31 //
32 // Deprecated: KeySpec exists for historical compatibility and should not be used.
33 // To access KeySpec, use the notation-plugin-framework-go's [plugin.KeySpec].
34 //
35 // [keys spec]: https://github.com/notaryproject/notaryproject/blob/main/specs/signature-specification.md#algorithm-selection
36 const (
37 KeySpecRSA2048 = plugin.KeySpecRSA2048
38 KeySpecRSA3072 = plugin.KeySpecRSA3072
39 KeySpecRSA4096 = plugin.KeySpecRSA4096
40 KeySpecEC256 = plugin.KeySpecEC256
41 KeySpecEC384 = plugin.KeySpecEC384
42 KeySpecEC521 = plugin.KeySpecEC521
43 )
44
45 // EncodeKeySpec returns the name of a keySpec according to the spec.
46 func EncodeKeySpec(k signature.KeySpec) (plugin.KeySpec, error) {
47 switch k.Type {
48 case signature.KeyTypeEC:
49 switch k.Size {
50 case 256:
51 return plugin.KeySpecEC256, nil
52 case 384:
53 return plugin.KeySpecEC384, nil
54 case 521:
55 return plugin.KeySpecEC521, nil
56 }
57 case signature.KeyTypeRSA:
58 switch k.Size {
59 case 2048:
60 return plugin.KeySpecRSA2048, nil
61 case 3072:
62 return plugin.KeySpecRSA3072, nil
63 case 4096:
64 return plugin.KeySpecRSA4096, nil
65 }
66 }
67 return "", fmt.Errorf("invalid KeySpec %q", k)
68 }
69
70 // DecodeKeySpec parses keySpec name to a signature.keySpec type.
71 func DecodeKeySpec(k plugin.KeySpec) (keySpec signature.KeySpec, err error) {
72 switch k {
73 case plugin.KeySpecRSA2048:
74 keySpec.Size = 2048
75 keySpec.Type = signature.KeyTypeRSA
76 case plugin.KeySpecRSA3072:
77 keySpec.Size = 3072
78 keySpec.Type = signature.KeyTypeRSA
79 case plugin.KeySpecRSA4096:
80 keySpec.Size = 4096
81 keySpec.Type = signature.KeyTypeRSA
82 case plugin.KeySpecEC256:
83 keySpec.Size = 256
84 keySpec.Type = signature.KeyTypeEC
85 case plugin.KeySpecEC384:
86 keySpec.Size = 384
87 keySpec.Type = signature.KeyTypeEC
88 case plugin.KeySpecEC521:
89 keySpec.Size = 521
90 keySpec.Type = signature.KeyTypeEC
91 default:
92 keySpec = signature.KeySpec{}
93 err = errors.New("unknown key spec")
94 }
95 return
96 }
97
98 // HashAlgorithm is the type of hash algorithm.
99 //
100 // Deprecated: HashAlgorithm exists for historical compatibility and should not be used.
101 // To access HashAlgorithm, use the notation-plugin-framework-go's [plugin.HashAlgorithm] type.
102 type HashAlgorithm = plugin.HashAlgorithm
103
104 // one of the following supported hash algorithm names.
105 //
106 // Deprecated: HashAlgorithm exists for historical compatibility and should not be used.
107 // To access HashAlgorithm, use the notation-plugin-framework-go's [plugin.HashAlgorithm] type.
108 //
109 // [hash algorithm]: https://github.com/notaryproject/notaryproject/blob/main/specs/signature-specification.md#algorithm-selection
110 const (
111 HashAlgorithmSHA256 = plugin.HashAlgorithmSHA256
112 HashAlgorithmSHA384 = plugin.HashAlgorithmSHA384
113 HashAlgorithmSHA512 = plugin.HashAlgorithmSHA512
114 )
115
116 // HashAlgorithmFromKeySpec returns the name of hash function according to the spec.
117 func HashAlgorithmFromKeySpec(k signature.KeySpec) (plugin.HashAlgorithm, error) {
118 switch k.Type {
119 case signature.KeyTypeEC:
120 switch k.Size {
121 case 256:
122 return plugin.HashAlgorithmSHA256, nil
123 case 384:
124 return plugin.HashAlgorithmSHA384, nil
125 case 521:
126 return plugin.HashAlgorithmSHA512, nil
127 }
128 case signature.KeyTypeRSA:
129 switch k.Size {
130 case 2048:
131 return plugin.HashAlgorithmSHA256, nil
132 case 3072:
133 return plugin.HashAlgorithmSHA384, nil
134 case 4096:
135 return plugin.HashAlgorithmSHA512, nil
136 }
137 }
138 return "", fmt.Errorf("invalid KeySpec %q", k)
139 }
140
141 // SignatureAlgorithm is the type of signature algorithm
142 //
143 // Deprecated: SignatureAlgorithm exists for historical compatibility and should not be used.
144 // To access SignatureAlgorithm, use the notation-plugin-framework-go's [plugin.SignatureAlgorithm] type.
145 type SignatureAlgorithm = plugin.SignatureAlgorithm
146
147 // one of the following supported [signing algorithm] names.
148 //
149 // Deprecated: SignatureAlgorithm exists for historical compatibility and should not be used.
150 // To access SignatureAlgorithm, use the notation-plugin-framework-go's [plugin.SignatureAlgorithm] type.
151 //
152 // [signing algorithm]: https://github.com/notaryproject/notaryproject/blob/main/specs/signature-specification.md#algorithm-selection
153 const (
154 SignatureAlgorithmECDSA_SHA256 = plugin.SignatureAlgorithmECDSA_SHA256
155 SignatureAlgorithmECDSA_SHA384 = plugin.SignatureAlgorithmECDSA_SHA384
156 SignatureAlgorithmECDSA_SHA512 = plugin.SignatureAlgorithmECDSA_SHA512
157 SignatureAlgorithmRSASSA_PSS_SHA256 = plugin.SignatureAlgorithmRSASSA_PSS_SHA256
158 SignatureAlgorithmRSASSA_PSS_SHA384 = plugin.SignatureAlgorithmRSASSA_PSS_SHA384
159 SignatureAlgorithmRSASSA_PSS_SHA512 = plugin.SignatureAlgorithmRSASSA_PSS_SHA512
160 )
161
162 // EncodeSigningAlgorithm returns the signing algorithm name of an algorithm
163 // according to the spec.
164 func EncodeSigningAlgorithm(alg signature.Algorithm) (plugin.SignatureAlgorithm, error) {
165 switch alg {
166 case signature.AlgorithmES256:
167 return plugin.SignatureAlgorithmECDSA_SHA256, nil
168 case signature.AlgorithmES384:
169 return plugin.SignatureAlgorithmECDSA_SHA384, nil
170 case signature.AlgorithmES512:
171 return plugin.SignatureAlgorithmECDSA_SHA512, nil
172 case signature.AlgorithmPS256:
173 return plugin.SignatureAlgorithmRSASSA_PSS_SHA256, nil
174 case signature.AlgorithmPS384:
175 return plugin.SignatureAlgorithmRSASSA_PSS_SHA384, nil
176 case signature.AlgorithmPS512:
177 return plugin.SignatureAlgorithmRSASSA_PSS_SHA512, nil
178 }
179 return "", fmt.Errorf("invalid algorithm %q", alg)
180 }
181
182 // DecodeSigningAlgorithm parses the signing algorithm name from a given string.
183 func DecodeSigningAlgorithm(raw plugin.SignatureAlgorithm) (signature.Algorithm, error) {
184 switch raw {
185 case plugin.SignatureAlgorithmECDSA_SHA256:
186 return signature.AlgorithmES256, nil
187 case plugin.SignatureAlgorithmECDSA_SHA384:
188 return signature.AlgorithmES384, nil
189 case plugin.SignatureAlgorithmECDSA_SHA512:
190 return signature.AlgorithmES512, nil
191 case plugin.SignatureAlgorithmRSASSA_PSS_SHA256:
192 return signature.AlgorithmPS256, nil
193 case plugin.SignatureAlgorithmRSASSA_PSS_SHA384:
194 return signature.AlgorithmPS384, nil
195 case plugin.SignatureAlgorithmRSASSA_PSS_SHA512:
196 return signature.AlgorithmPS512, nil
197 }
198 return 0, errors.New("unknown signing algorithm")
199 }