def test_generate_data_key__invalid_key_spec(): kms = boto3.client('kms', region_name='us-west-2') key = kms.create_key() key_id = key['KeyMetadata']['KeyId'] with assert_raises(JSONResponseError): kms.generate_data_key(KeyId=key_id, KeySpec='AES_1024', EncryptionContext={'Key': 'Value'})
def putSecret(name, secret, version, kms_key="alias/credstash", region="us-east-1", table="credential-store", context=None): ''' put a secret called `name` into the secret-store, protected by the key kms_key ''' kms = boto.kms.connect_to_region(region) # generate a a 64 byte key. # Half will be for data encryption, the other half for HMAC try: kms_response = kms.generate_data_key(kms_key, context, 64) except: raise KmsError("Could not generate key using KMS key %s" % kms_key) data_key = kms_response['Plaintext'][:32] hmac_key = kms_response['Plaintext'][32:] wrapped_key = kms_response['CiphertextBlob'] enc_ctr = Counter.new(128) encryptor = AES.new(data_key, AES.MODE_CTR, counter=enc_ctr) c_text = encryptor.encrypt(secret) # compute an HMAC using the hmac key and the ciphertext hmac = HMAC(hmac_key, msg=c_text, digestmod=SHA256) b64hmac = hmac.hexdigest() secretStore = Table(table, connection=boto.dynamodb2.connect_to_region(region)) data = {} data['name'] = name data['version'] = version if version != "" else "1" data['key'] = b64encode(wrapped_key) data['contents'] = b64encode(c_text) data['hmac'] = b64hmac return secretStore.put_item(data=data)
def putSecret(name, secret, version, kms_key="alias/credstash", region="us-east-1", context=None): ''' put a secret called `name` into the secret-store, protected by the key kms_key ''' if not context: context = {} kms = boto3.client('kms', region_name=region) # generate a a 64 byte key. # Half will be for data encryption, the other half for HMAC # try: kms_response = kms.generate_data_key(KeyId=kms_key, EncryptionContext=context, NumberOfBytes=64) # except: # raise KmsError("Could not generate key using KMS key %s" % kms_key) data_key = kms_response['Plaintext'][:32] hmac_key = kms_response['Plaintext'][32:] wrapped_key = kms_response['CiphertextBlob'] enc_ctr = Counter.new(128) encryptor = AES.new(data_key, AES.MODE_CTR, counter=enc_ctr) c_text = encryptor.encrypt(secret) # compute an HMAC using the hmac key and the ciphertext hmac = HMAC(hmac_key, msg=c_text, digestmod=SHA256) b64hmac = hmac.hexdigest() data = {} data['name'] = name data['version'] = version if version != "" else "1" data['key'] = b64encode(wrapped_key).decode('utf-8') data['contents'] = b64encode(c_text).decode('utf-8') data['hmac'] = b64hmac with open('{0}.{1}.json'.format(name,data['version']), 'w') as fp: json.dump(data, fp)
def get_data_key(encryption_context=None, region='us-east-1'): # This method will generate a new data key kms = boto.kms.connect_to_region(region_name=region) # generate_data_key output: # {'Plaintext': '<binary blob>', 'KeyId': 'arn:aws:kms:us-east-1:000000000000:key/1234abcd-12ab-12ab-12ab-123456abcdef', 'CiphertextBlob': '<binary blob>'} data_key = kms.generate_data_key(key_id='alias/kaurna', encryption_context=encryption_context, key_spec='AES_256') return data_key
def test_generate_data_key__AES_128(): kms = boto3.client('kms', region_name='us-west-2') key = kms.create_key() key_id = key['KeyMetadata']['KeyId'] response = kms.generate_data_key(KeyId=key_id, KeySpec='AES_128', EncryptionContext={'Key': 'Value'}) plaintext = response['Plaintext'] response_key_id = response['KeyId'] assert len(plaintext) == 16 assert response_key_id == key_id
def test_boto3_generate_data_key(): kms = boto3.client("kms", region_name="us-west-2") key = kms.create_key() key_id = key["KeyMetadata"]["KeyId"] key_arn = key["KeyMetadata"]["Arn"] response = kms.generate_data_key(KeyId=key_id, NumberOfBytes=32) # CiphertextBlob must NOT be base64-encoded with assert_raises(Exception): base64.b64decode(response["CiphertextBlob"], validate=True) # Plaintext must NOT be base64-encoded with assert_raises(Exception): base64.b64decode(response["Plaintext"], validate=True) response["KeyId"].should.equal(key_arn)
def putSecret(name, secret, version, kms_key="alias/credstash", region="us-east-1", table="credential-store", context=None): ''' put a secret called `name` into the secret-store, protected by the key kms_key ''' if not context: context = {} kms = boto3.client('kms', region_name=region) # generate a a 64 byte key. # Half will be for data encryption, the other half for HMAC try: kms_response = kms.generate_data_key(KeyId=kms_key, EncryptionContext=context, NumberOfBytes=64) except: raise KmsError("Could not generate key using KMS key %s" % kms_key) data_key = kms_response['Plaintext'][:32] hmac_key = kms_response['Plaintext'][32:] wrapped_key = kms_response['CiphertextBlob'] enc_ctr = Counter.new(128) encryptor = AES.new(data_key, AES.MODE_CTR, counter=enc_ctr) c_text = encryptor.encrypt(secret) # compute an HMAC using the hmac key and the ciphertext hmac = HMAC(hmac_key, msg=c_text, digestmod=SHA256) b64hmac = hmac.hexdigest() secretStore = Table(table, connection=boto.dynamodb2.connect_to_region(region)) data = {} data['name'] = name data['version'] = version if version != "" else "1" data['key'] = b64encode(wrapped_key).decode('utf-8') data['contents'] = b64encode(c_text).decode('utf-8') data['hmac'] = b64hmac return secretStore.put_item(data=data)