Skip to content

feat(kms): demo script for dinamo interface#117

Merged
pranavjain97 merged 3 commits into
masterfrom
WP5535/demo-script-dinamo
Aug 14, 2025
Merged

feat(kms): demo script for dinamo interface#117
pranavjain97 merged 3 commits into
masterfrom
WP5535/demo-script-dinamo

Conversation

@cpatino-intive

Copy link
Copy Markdown
Contributor

TASKS: WP-5535

@cpatino-intive cpatino-intive marked this pull request as draft August 12, 2025 21:36
@cpatino-intive cpatino-intive force-pushed the WP5535/demo-script-dinamo branch from 96ccd55 to f63cd60 Compare August 12, 2025 22:44
@cpatino-intive cpatino-intive marked this pull request as ready for review August 12, 2025 22:45

@pranavjain97 pranavjain97 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pretty solid documentation! Just some minor changes needed to clarify that this is just a suggested reference implementation and not set in stone.

Comment thread demo-kms-script/dinamo-interface.md Outdated
Comment on lines +81 to +86
**Key Features:**
- Environment-based configuration
- Automatic connection cleanup using try/finally
- Error handling for disconnect failures
- Generic typing for return values
- Centralized connection logic for all HSM operations

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is too specific, can remove

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

Comment thread demo-kms-script/dinamo-interface.md Outdated

```typescript
async generateDataKey(rootKey: string, keySpec: DataKeyTypeType): Promise<GenerateDataKeyKmsRes> {
return await this.withClient(async (client) => {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

worth mentioning why this is needed? (to not have dangling connections to the HSM)

Comment thread demo-kms-script/dinamo-interface.md Outdated
@@ -0,0 +1,266 @@
# Dinamo HSM KMS Implementation Documentation

This document provides comprehensive documentation for the KMS API's integration with Dinamo HSM, covering the complete request-response flow from API handlers to HSM operations.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
This document provides comprehensive documentation for the KMS API's integration with Dinamo HSM, covering the complete request-response flow from API handlers to HSM operations.
This document provides a reference implementation for integrating the 4 KMS API's with Dinamo HSM

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

Comment thread demo-kms-script/dinamo-interface.md Outdated
| `POST /generateDataKey` | `generateDataKey.ts` | `generateDataKey()` | Create/export AES key |
| `POST /decryptDataKey` | `decryptDataKey.ts` | `decryptDataKey()` | Local SJCL decryption |

## Envelope Encryption Pattern

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
## Envelope Encryption Pattern
## Envelope Encryption Pattern (Recommended)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

@pranavjain97 pranavjain97 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should note that the code references are in JavaScript, but this would ideally be implemented in C++ or Rust to allow explicit memory management to wipe out the plaintext data keys after use


// 2. Export plaintext key material
const exportedKey = await client.key.exportSymmetric(dataKeyName);
const plaintextKey = exportedKey.toString('base64');

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

need to add a comment here in bold saying the plaintext key should be wiped out of memory after use.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated

Comment thread demo-kms-script/dinamo-interface.md Outdated
Comment on lines +161 to +189
let plaintextKey: string | null = null;

try {
const result = await this.generateDataKey(rootKey, 'AES-256');
plaintextKey = result.plaintextKey;

// Use the plaintext key immediately
const encryptedData = encrypt(plaintextKey, sensitiveData);

return {
encryptedKey: result.encryptedKey,
encryptedData: encryptedData
};
} finally {
// **CRITICAL**: Explicitly wipe plaintext key from memory
if (plaintextKey) {
// Overwrite with random data multiple times
for (let i = 0; i < 3; i++) {
plaintextKey = crypto.randomBytes(plaintextKey.length).toString('base64');
}
plaintextKey = null;
}

// Force garbage collection (if available)
if (global.gc) {
global.gc();
}
}
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would not clean the plaintext key from memory. JS strings are immutable.
That's why we need to suggest they either implement the KMS-API in a c++ like language, or use typed arrays like a Uint8Array for all sensitive data (which would still not work if the provider returned the plaintext in string)

@pranavjain97 pranavjain97 merged commit 033811a into master Aug 14, 2025
4 checks passed
@pranavjain97 pranavjain97 deleted the WP5535/demo-script-dinamo branch August 14, 2025 17:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants