Skip to content

Commit e919b95

Browse files
committed
fix: honor SLH-DSA key encoding options
1 parent 77d3c11 commit e919b95

1 file changed

Lines changed: 67 additions & 22 deletions

File tree

packages/react-native-quick-crypto/src/keys/generateKeyPair.ts

Lines changed: 67 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { ed_generateKeyPair } from '../ed';
2+
import { Buffer } from '@craftzdog/react-native-buffer';
23
import { rsa_generateKeyPairNode, rsa_generateKeyPairNodeSync } from '../rsa';
34
import { ec_generateKeyPairNode, ec_generateKeyPairNodeSync } from '../ec';
45
import { dsa_generateKeyPairNode, dsa_generateKeyPairNodeSync } from '../dsa';
@@ -50,12 +51,15 @@ function isSlhDsaType(type: string): type is SlhDsaKeyPairType {
5051
return type in SLH_DSA_TYPE_TO_VARIANT;
5152
}
5253

53-
function slhDsaGenerateKeyPairNodeSync(type: SlhDsaKeyPairType): {
54-
publicKey: PublicKeyObject;
55-
privateKey: PrivateKeyObject;
54+
function slhDsaFormatKeyPairOutput(
55+
slhdsa: SlhDsa,
56+
encoding: KeyPairGenConfig,
57+
): {
58+
publicKey: PublicKeyObject | string | ArrayBuffer;
59+
privateKey: PrivateKeyObject | string | ArrayBuffer;
5660
} {
57-
const slhdsa = new SlhDsa(SLH_DSA_TYPE_TO_VARIANT[type]);
58-
slhdsa.generateKeyPairSync();
61+
const { publicFormat, privateFormat, cipher, passphrase } = encoding;
62+
5963
const publicKey = KeyObject.createKeyObject(
6064
'public',
6165
slhdsa.getPublicKey(),
@@ -68,27 +72,68 @@ function slhDsaGenerateKeyPairNodeSync(type: SlhDsaKeyPairType): {
6872
KFormatType.DER,
6973
KeyEncoding.PKCS8,
7074
) as PrivateKeyObject;
71-
return { publicKey, privateKey };
75+
76+
let publicKeyOutput: PublicKeyObject | string | ArrayBuffer;
77+
let privateKeyOutput: PrivateKeyObject | string | ArrayBuffer;
78+
79+
if (publicFormat === -1) {
80+
publicKeyOutput = publicKey;
81+
} else {
82+
const format =
83+
publicFormat === KFormatType.PEM ? KFormatType.PEM : KFormatType.DER;
84+
const exported = publicKey.handle.exportKey(format, KeyEncoding.SPKI);
85+
if (format === KFormatType.PEM) {
86+
publicKeyOutput = Buffer.from(new Uint8Array(exported)).toString('utf-8');
87+
} else {
88+
publicKeyOutput = exported;
89+
}
90+
}
91+
92+
if (privateFormat === -1) {
93+
privateKeyOutput = privateKey;
94+
} else {
95+
const format =
96+
privateFormat === KFormatType.PEM ? KFormatType.PEM : KFormatType.DER;
97+
const exported = privateKey.handle.exportKey(
98+
format,
99+
KeyEncoding.PKCS8,
100+
cipher,
101+
passphrase,
102+
);
103+
if (format === KFormatType.PEM) {
104+
privateKeyOutput = Buffer.from(new Uint8Array(exported)).toString(
105+
'utf-8',
106+
);
107+
} else {
108+
privateKeyOutput = exported;
109+
}
110+
}
111+
112+
return { publicKey: publicKeyOutput, privateKey: privateKeyOutput };
113+
}
114+
115+
function slhDsaGenerateKeyPairNodeSync(
116+
type: SlhDsaKeyPairType,
117+
encoding: KeyPairGenConfig,
118+
): {
119+
publicKey: PublicKeyObject | string | ArrayBuffer;
120+
privateKey: PrivateKeyObject | string | ArrayBuffer;
121+
} {
122+
const slhdsa = new SlhDsa(SLH_DSA_TYPE_TO_VARIANT[type]);
123+
slhdsa.generateKeyPairSync();
124+
return slhDsaFormatKeyPairOutput(slhdsa, encoding);
72125
}
73126

74127
async function slhDsaGenerateKeyPairNode(
75128
type: SlhDsaKeyPairType,
76-
): Promise<{ publicKey: PublicKeyObject; privateKey: PrivateKeyObject }> {
129+
encoding: KeyPairGenConfig,
130+
): Promise<{
131+
publicKey: PublicKeyObject | string | ArrayBuffer;
132+
privateKey: PrivateKeyObject | string | ArrayBuffer;
133+
}> {
77134
const slhdsa = new SlhDsa(SLH_DSA_TYPE_TO_VARIANT[type]);
78135
await slhdsa.generateKeyPair();
79-
const publicKey = KeyObject.createKeyObject(
80-
'public',
81-
slhdsa.getPublicKey(),
82-
KFormatType.DER,
83-
KeyEncoding.SPKI,
84-
) as PublicKeyObject;
85-
const privateKey = KeyObject.createKeyObject(
86-
'private',
87-
slhdsa.getPrivateKey(),
88-
KFormatType.DER,
89-
KeyEncoding.PKCS8,
90-
) as PrivateKeyObject;
91-
return { publicKey, privateKey };
136+
return slhDsaFormatKeyPairOutput(slhdsa, encoding);
92137
}
93138

94139
export const generateKeyPair = (
@@ -243,7 +288,7 @@ function internalGenerateKeyPair(
243288
} else if (type === 'dh') {
244289
result = await dh_generateKeyPairNode(options, encoding);
245290
} else if (isSlhDsaType(type)) {
246-
result = await slhDsaGenerateKeyPairNode(type);
291+
result = await slhDsaGenerateKeyPairNode(type, encoding);
247292
} else {
248293
throw new Error(`Unsupported key type: ${type}`);
249294
}
@@ -275,7 +320,7 @@ function internalGenerateKeyPair(
275320
} else if (type === 'dh') {
276321
result = dh_generateKeyPairNodeSync(options, encoding);
277322
} else if (isSlhDsaType(type)) {
278-
result = slhDsaGenerateKeyPairNodeSync(type);
323+
result = slhDsaGenerateKeyPairNodeSync(type, encoding);
279324
} else {
280325
throw new Error(`Unsupported key type: ${type}`);
281326
}

0 commit comments

Comments
 (0)