Key Management in AWS: Exploring CloudHSM
Published: 2024-01-11 18:46:40

In the ever-evolving landscape of cloud computing, securing sensitive data is paramount. As organizations increasingly migrate their workloads to the cloud, effective key management becomes a critical aspect of their overall security strategy. AWS, as a leading cloud services provider, offers two prominent solutions for key management: AWS CloudHSM and AWS Key Management Service (KMS).

In this blog we will explore **CloudHSM**


Before we deep dive into services, we should first understand about Cryptography


# Cryptography

Cryptography is the practice of techniques for securing communication and data from adversaries. It involves the use of mathematical algorithms to transform information into a format that is unintelligible to unauthorized users, ensuring confidentiality, integrity, authentication, and non-repudiation

Key management is one of the concepts used in Key management is one of the concepts used in Cryptography

## Key management

Key management refers to the process of generating, storing, distributing, and disposing of **cryptographic keys** in a secure and organized manner. Cryptographic keys are essential components in the field of cryptography, used to encrypt and decrypt information, authenticate users, and ensure the integrity of data.

The cryptographic keys are fundamental to ensuring the security of communication, protecting sensitive data, and maintaining the integrity of information. There are different types of cryptographic keys, and their roles can be broadly categorized into two main types: symmetric keys and asymmetric keys.

### Symmetric Key Encryption

Symmetric-key cryptography is a term used for cryptographic algorithms that use the same key for encryption and for decryption.

The commonly used algorithms that are used for symmetric key encryption are:

- AES (Advanced Encryption Standard)
- DES (Data Encryption Standard)

 

![SymmetricKey](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ajcmo9ymdas1joz5ut67.jpg)

>  (Source: The SSL Store)

 

### Asymmetric Key Encryption / Public key Encryption

It is a cryptographic system in which keys come in pairs. The transformation performed by one of the keys can only be undone with the other key. One key (the private key) is kept secret while the other is made public.

The commonly used algorithms that are used for asymmetric key encryption are:

- RSA(Rivest–Shamir–Adleman)

- DSA (Digital Signature Algorithm)


![AsymmetricKey](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9lf3yjdrg1l90cgz48xz.png)
> (Source: appViewX)


If we store the cryptographic keys in our system it can be a security issue therefore the **Hardware Security Modules (HSMs)** are used for storing cryptographic keys due to their specialized design and features that enhance the security and protection of sensitive key material.

### Hardware Security Module(HSM)

A Hardware Security Module is a specialized, highly trusted physical device that performs all major cryptographic operations, including encryption, decryption, authentication, key management, key exchange, and more. HSMs are specialized security devices, with the sole objective of hiding and protecting cryptographic materials

**Types of HSMs**

There are two main types of Hardware Security Module:

- General Purpose

General Purpose HSMs can utilize the most common encryption algorithms and are primarily used with Public Key Infrastructures, cryptowallets, and other basic sensitive data.

- Payment and Transaction

These types of HSM are created with the protection of payment card information and other types of sensitive transaction information. These types of Hardware Security modules are narrower in the types of organizations they can work within, but they are ideal to help comply with **Payment Card Industry Data Security Standards (PCI DSS)**.

The main reason to use HSMs is that it is Tamper-resistant, tamper-evident, and tamper-proof systems to provide extremely secure physical systems.

![HSM](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7lf38ojz6n6rpbdmo465.png)
> (Source: cpl.thalesgroup.com)


We need our key to keep somewhere and manage somewhere and most of the industry uses HSM but it can be **very costly** to use, to overcome this problem AWS launched a service called **CloudHSM** where you can use a pay-as-you-go model, allowing you to pay for the resources you consume. This can be cost-effective for organizations with dynamic or unpredictable workloads.



## CloudHSM

AWS CloudHSM provides cloud-based hardware security modules (HSMs) for generating and using your own encryption keys in the AWS Cloud. With CloudHSM, you can manage your own encryption keys using FIPS 140-2 Level 3 validated HSMs and integrate with your applications using industry-standard APIs.[For more detail](https://docs.aws.amazon.com/cloudhsm/latest/userguide/introduction.html)


![cloudHSM](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/admps2xn9mukscvqan6x.png)


**Let's create a CloudHSM cluster: **

-  Go to aws CloudHSM service and click on **create cluster**

![createCluster](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/j1wge4gu21ti7p15j7k2.png)

-  Select the VPC and Availability zones based on how many devices you want and then click next


![cluster2](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vaski3c5zum6c8wvdo8a.png)

-  Add the backup retention period

After this time period, the cluster backup will be deleted


![time](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/578bz0uesxfyydq59xgi.png)

-  Click on Create cluster

![clickCluster](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zg38auf9rolziyfl8d22.png)

After a few minutes cluster will be created

![cluster created](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xog3jtkzsskg6fsl0x64.png) 

Here you can see that the cluster is uninitialized which means we need to add HSM appliance to it  

- Click on the cluster and then click on Initialize

![initialize](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/12pr8swaribw4m32u8ii.png)

- select the AZ to create the HSM and click on create


![hsm](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/690fihhhtjq2xxcceq98.png)

Now you can see one HSM is added to the cluster


![added](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ip5e1koor4wlw47rzxrx.png)

- Now download the Certificate signing request

![sign](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/huavp2ecbkjllokj64sl.png)


Download the CSR and sign it.

**To sign the CSR follow the steps:**

- Generate the Private key

Use the command `openssl genrsa -aes256 -out customerCA.key 2048` to generate the private key

**Note** Remember the pass phrase it will be use in next step

- To sign the CSR use the command

`openssl req -new -x509 -days 3652 -key customerCA.key -out customerCA.crt` 

Here **customerCA.crt** is a certificate that you downloaded

fill the details to sign the certificate


![certificate](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/caiqf80ouxu0gjc67u93.png)

- Sign in the cluster CSR 

use the following command :


`openssl x509 -req -days 3652 -in <cluster ID>_ClusterCsr.csr \
                              -CA customerCA.crt \
                              -CAkey customerCA.key \
                              -CAcreateserial \
                              -out <cluster ID>_CustomerHsmCertificate.crt`

- Now upload the certificates


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2uxgcw80813sjqx5qg2p.png)

**Note: ** The cluster certificate will be the one with cluster id

Now you can see that your cluster is initiated

![clusterInitiated](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/p5hytsqmuiopk6ryvtup.png)

- Now set the password for the cluster and activate it

use the following [aws documentation](https://docs.aws.amazon.com/cloudhsm/latest/userguide/activate-cluster.html) to activate cluster

After following the above doc you will see that now your cluster is activated

![Created](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vodbezoebzybsfacac41.png)

That's how you can create a CloudHSM cluster and add HSM device to it and now you can start using it to store your keys.

Hope you find this blog insightful 🌟

Thank you!