-
-
Notifications
You must be signed in to change notification settings - Fork 35.4k
crypto: add support for SM2 #37066
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
tniessen
wants to merge
4
commits into
nodejs:main
Choose a base branch
from
tniessen:crypto-add-support-for-sm2
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
crypto: add support for SM2 #37066
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
70988ea
crypto: add 'sm2' key type and key pair generation
tniessen 4282617
crypto: add support for SM2 one-shot signatures
tniessen a52008c
crypto: allow adjusting key type on import
tniessen a76c7e4
test: ensure EC keys on the SM2 curve work
tniessen File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
crypto: allow adjusting key type on import
- Loading branch information
commit a52008c0e9b5d57bc16759feaa78e73333b95478
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -20,6 +20,16 @@ const { | |
| kKeyEncodingPKCS8, | ||
| kKeyEncodingSPKI, | ||
| kKeyEncodingSEC1, | ||
| EVP_PKEY_DH, | ||
| EVP_PKEY_DSA, | ||
| EVP_PKEY_EC, | ||
| EVP_PKEY_ED25519, | ||
| EVP_PKEY_ED448, | ||
| EVP_PKEY_RSA, | ||
| EVP_PKEY_RSA_PSS, | ||
| EVP_PKEY_SM2, | ||
| EVP_PKEY_X25519, | ||
| EVP_PKEY_X448 | ||
| } = internalBinding('crypto'); | ||
|
|
||
| const { | ||
|
|
@@ -359,13 +369,41 @@ function prepareAsymmetricKey(key, ctx) { | |
| // Expect PEM by default, mostly for backward compatibility. | ||
| return { format: kKeyFormatPEM, data: getArrayBufferOrView(key, 'key') }; | ||
| } else if (typeof key === 'object') { | ||
| const { key: data, encoding } = key; | ||
| const { key: data, encoding, asymmetricKeyType: typeStr } = key; | ||
|
|
||
| let asymmetricKeyType; | ||
| if (typeStr == null) | ||
| asymmetricKeyType = undefined; | ||
| else if (typeStr === 'dh') | ||
| asymmetricKeyType = EVP_PKEY_DH; | ||
| else if (typeStr === 'dsa') | ||
| asymmetricKeyType = EVP_PKEY_DSA; | ||
| else if (typeStr === 'ec') | ||
| asymmetricKeyType = EVP_PKEY_EC; | ||
| else if (typeStr === 'ed25519') | ||
| asymmetricKeyType = EVP_PKEY_ED25519; | ||
| else if (typeStr === 'ed448') | ||
| asymmetricKeyType = EVP_PKEY_ED448; | ||
| else if (typeStr === 'rsa') | ||
| asymmetricKeyType = EVP_PKEY_RSA; | ||
| else if (typeStr === 'rsa-pss') | ||
| asymmetricKeyType = EVP_PKEY_RSA_PSS; | ||
| else if (typeStr === 'sm2') | ||
| asymmetricKeyType = EVP_PKEY_SM2; | ||
| else if (typeStr === 'x25519') | ||
| asymmetricKeyType = EVP_PKEY_X25519; | ||
| else if (typeStr === 'x448') | ||
| asymmetricKeyType = EVP_PKEY_X448; | ||
| else | ||
| throw new ERR_INVALID_ARG_VALUE('options.asymmetricKeyType', typeStr); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A switch statement would be nicer here from a code readability point of view. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I suggest key-map object instead: const keyTypeMap = {
'dh': EVP_PKEY_DH,
'dsa': EVP_PKEY_DSA,
'ec': EVP_PKEY_EC,
'ed25519': EVP_PKEY_ED25519,
'ed448': EVP_PKEY_ED448,
'rsa': EVP_PKEY_RSA,
'rsa-pss': EVP_PKEY_RSA_PSS,
'sm2': EVP_PKEY_SM2,
'x25519': EVP_PKEY_X25519,
'x448': EVP_PKEY_X448,
null: undefined // Map null to undefined
};
const asymmetricKeyType = keyTypeMap[typeStr];
if (asymmetricKeyType === undefined) { // if asymmetricKeyType null or not in keyTypeMap
throw new ERR_INVALID_ARG_VALUE('options.asymmetricKeyType', typeStr);
} |
||
|
|
||
| // The 'key' property can be a KeyObject as well to allow specifying | ||
| // additional options such as padding along with the key. | ||
| const common = { asymmetricKeyType }; | ||
| if (isKeyObject(data)) | ||
| return { data: getKeyObjectHandle(data, ctx) }; | ||
| return { data: getKeyObjectHandle(data, ctx), ...common }; | ||
| else if (isCryptoKey(data)) | ||
| return { data: getKeyObjectHandle(data[kKeyObject], ctx) }; | ||
| return { data: getKeyObjectHandle(data[kKeyObject], ctx), ...common }; | ||
| // Either PEM or DER using PKCS#1 or SPKI. | ||
| if (!isStringOrBuffer(data)) { | ||
| throw new ERR_INVALID_ARG_TYPE( | ||
|
|
@@ -378,6 +416,7 @@ function prepareAsymmetricKey(key, ctx) { | |
| (ctx === kConsumePrivate || ctx === kCreatePrivate) ? false : undefined; | ||
| return { | ||
| data: getArrayBufferOrView(data, 'key', encoding), | ||
| ...common, | ||
| ...parseKeyEncoding(key, undefined, isPublic) | ||
| }; | ||
| } | ||
|
|
@@ -428,17 +467,19 @@ function createSecretKey(key, encoding) { | |
| } | ||
|
|
||
| function createPublicKey(key) { | ||
| const { format, type, data } = prepareAsymmetricKey(key, kCreatePublic); | ||
| const { format, type, data, passphrase, asymmetricKeyType } = | ||
| prepareAsymmetricKey(key, kCreatePublic); | ||
| const handle = new KeyObjectHandle(); | ||
| handle.init(kKeyTypePublic, data, format, type); | ||
| handle.init(kKeyTypePublic, data, format, type, passphrase, asymmetricKeyType); | ||
| return new PublicKeyObject(handle); | ||
| } | ||
|
|
||
| function createPrivateKey(key) { | ||
| const { format, type, data, passphrase } = | ||
| const { format, type, data, passphrase, asymmetricKeyType } = | ||
| prepareAsymmetricKey(key, kCreatePrivate); | ||
| const handle = new KeyObjectHandle(); | ||
| handle.init(kKeyTypePrivate, data, format, type, passphrase); | ||
| handle.init(kKeyTypePrivate, data, format, type, passphrase, | ||
| asymmetricKeyType); | ||
| return new PrivateKeyObject(handle); | ||
| } | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would be good to document the expected values here (or link to where they are documented)
(I know there's a paragraph added below but listing the values here would be helpful)