13
13
# See the License for the specific language governing permissions and
14
14
# limitations under the License.rom googleapiclient import discovery
15
15
16
- # [START kms_asymmetric_imports]
17
16
import base64
18
17
import hashlib
19
18
20
19
from cryptography .exceptions import InvalidSignature
21
20
from cryptography .hazmat .backends import default_backend
22
21
from cryptography .hazmat .primitives import hashes , serialization
23
22
from cryptography .hazmat .primitives .asymmetric import ec , padding , utils
24
- # [END kms_asymmetric_imports]
25
23
26
24
27
25
# [START kms_get_asymmetric_public]
28
26
def getAsymmetricPublicKey (client , key_path ):
29
27
"""
30
28
Retrieves the public key from a saved asymmetric key pair on Cloud KMS
29
+
30
+ Requires:
31
+ cryptography.hazmat.backends.default_backend
32
+ cryptography.hazmat.primitives.serialization
31
33
"""
32
34
request = client .projects () \
33
35
.locations () \
@@ -47,6 +49,9 @@ def decryptRSA(ciphertext, client, key_path):
47
49
"""
48
50
Decrypt the input ciphertext (bytes) using an
49
51
'RSA_DECRYPT_OAEP_2048_SHA256' private key stored on Cloud KMS
52
+
53
+ Requires:
54
+ base64
50
55
"""
51
56
request_body = {'ciphertext' : base64 .b64encode (ciphertext ).decode ('utf-8' )}
52
57
request = client .projects () \
@@ -67,6 +72,10 @@ def encryptRSA(plaintext, client, key_path):
67
72
"""
68
73
Encrypt the input plaintext (bytes) locally using an
69
74
'RSA_DECRYPT_OAEP_2048_SHA256' public key retrieved from Cloud KMS
75
+
76
+ Requires:
77
+ cryptography.hazmat.primitives.asymmetric.padding
78
+ cryptography.hazmat.primitives.hashes
70
79
"""
71
80
public_key = getAsymmetricPublicKey (client , key_path )
72
81
pad = padding .OAEP (mgf = padding .MGF1 (algorithm = hashes .SHA256 ()),
@@ -80,6 +89,10 @@ def encryptRSA(plaintext, client, key_path):
80
89
def signAsymmetric (message , client , key_path ):
81
90
"""
82
91
Create a signature for a message using a private key stored on Cloud KMS
92
+
93
+ Requires:
94
+ base64
95
+ hashlib
83
96
"""
84
97
# Note: some key algorithms will require a different hash function
85
98
# For example, EC_SIGN_P384_SHA384 requires SHA384
@@ -104,6 +117,13 @@ def verifySignatureRSA(signature, message, client, key_path):
104
117
"""
105
118
Verify the validity of an 'RSA_SIGN_PSS_2048_SHA256' signature for the
106
119
specified message
120
+
121
+ Requires:
122
+ cryptography.exceptions.InvalidSignature
123
+ cryptography.hazmat.primitives.asymmetric.padding
124
+ cryptography.hazmat.primitives.asymmetric.utils
125
+ cryptography.hazmat.primitives.hashes
126
+ hashlib
107
127
"""
108
128
public_key = getAsymmetricPublicKey (client , key_path )
109
129
digest_bytes = hashlib .sha256 (message ).digest ()
@@ -127,6 +147,13 @@ def verifySignatureEC(signature, message, client, key_path):
127
147
"""
128
148
Verify the validity of an 'EC_SIGN_P256_SHA256' signature
129
149
for the specified message
150
+
151
+ Requires:
152
+ cryptography.exceptions.InvalidSignature
153
+ cryptography.hazmat.primitives.asymmetric.ec
154
+ cryptography.hazmat.primitives.asymmetric.utils
155
+ cryptography.hazmat.primitives.hashes
156
+ hashlib
130
157
"""
131
158
public_key = getAsymmetricPublicKey (client , key_path )
132
159
digest_bytes = hashlib .sha256 (message ).digest ()
0 commit comments