crypto: add support for EdDSA key pair generation · nodejs/node@3a95924 (original) (raw)

`@@ -5,6 +5,9 @@ const {

`

5

5

` generateKeyPairRSA,

`

6

6

` generateKeyPairDSA,

`

7

7

` generateKeyPairEC,

`

``

8

`+

generateKeyPairEdDSA,

`

``

9

`+

EVP_PKEY_ED25519,

`

``

10

`+

EVP_PKEY_ED448,

`

8

11

`OPENSSL_EC_NAMED_CURVE,

`

9

12

`OPENSSL_EC_EXPLICIT_CURVE

`

10

13

`} = internalBinding('crypto');

`

`@@ -119,18 +122,25 @@ function parseKeyEncoding(keyType, options) {

`

119

122

``

120

123

`function check(type, options, callback) {

`

121

124

`validateString(type, 'type');

`

122

``

`-

if (options == null || typeof options !== 'object')

`

123

``

`-

throw new ERR_INVALID_ARG_TYPE('options', 'object', options);

`

124

125

``

125

126

`// These will be set after parsing the type and type-specific options to make

`

126

127

`// the order a bit more intuitive.

`

127

128

`let cipher, passphrase, publicType, publicFormat, privateType, privateFormat;

`

128

129

``

``

130

`+

if (options !== undefined && typeof options !== 'object')

`

``

131

`+

throw new ERR_INVALID_ARG_TYPE('options', 'object', options);

`

``

132

+

``

133

`+

function needOptions() {

`

``

134

`+

if (options == null)

`

``

135

`+

throw new ERR_INVALID_ARG_TYPE('options', 'object', options);

`

``

136

`+

return options;

`

``

137

`+

}

`

``

138

+

129

139

`let impl;

`

130

140

`switch (type) {

`

131

141

`case 'rsa':

`

132

142

`{

`

133

``

`-

const { modulusLength } = options;

`

``

143

`+

const { modulusLength } = needOptions();

`

134

144

`if (!isUint32(modulusLength))

`

135

145

`throw new ERR_INVALID_OPT_VALUE('modulusLength', modulusLength);

`

136

146

``

`@@ -149,7 +159,7 @@ function check(type, options, callback) {

`

149

159

`break;

`

150

160

`case 'dsa':

`

151

161

`{

`

152

``

`-

const { modulusLength } = options;

`

``

162

`+

const { modulusLength } = needOptions();

`

153

163

`if (!isUint32(modulusLength))

`

154

164

`throw new ERR_INVALID_OPT_VALUE('modulusLength', modulusLength);

`

155

165

``

`@@ -168,7 +178,7 @@ function check(type, options, callback) {

`

168

178

`break;

`

169

179

`case 'ec':

`

170

180

`{

`

171

``

`-

const { namedCurve } = options;

`

``

181

`+

const { namedCurve } = needOptions();

`

172

182

`if (typeof namedCurve !== 'string')

`

173

183

`throw new ERR_INVALID_OPT_VALUE('namedCurve', namedCurve);

`

174

184

`let { paramEncoding } = options;

`

`@@ -185,19 +195,32 @@ function check(type, options, callback) {

`

185

195

`cipher, passphrase, wrap);

`

186

196

`}

`

187

197

`break;

`

``

198

`+

case 'ed25519':

`

``

199

`+

case 'ed448':

`

``

200

`+

{

`

``

201

`+

const id = type === 'ed25519' ? EVP_PKEY_ED25519 : EVP_PKEY_ED448;

`

``

202

`+

impl = (wrap) => generateKeyPairEdDSA(id,

`

``

203

`+

publicFormat, publicType,

`

``

204

`+

privateFormat, privateType,

`

``

205

`+

cipher, passphrase, wrap);

`

``

206

`+

}

`

``

207

`+

break;

`

188

208

`default:

`

189

209

`throw new ERR_INVALID_ARG_VALUE('type', type,

`

190

``

`-

"must be one of 'rsa', 'dsa', 'ec'");

`

``

210

`+

"must be one of 'rsa', 'dsa', 'ec', " +

`

``

211

`+

"'ed25519', 'ed448'");

`

191

212

`}

`

192

213

``

193

``

`-

({

`

194

``

`-

cipher,

`

195

``

`-

passphrase,

`

196

``

`-

publicType,

`

197

``

`-

publicFormat,

`

198

``

`-

privateType,

`

199

``

`-

privateFormat

`

200

``

`-

} = parseKeyEncoding(type, options));

`

``

214

`+

if (options) {

`

``

215

`+

({

`

``

216

`+

cipher,

`

``

217

`+

passphrase,

`

``

218

`+

publicType,

`

``

219

`+

publicFormat,

`

``

220

`+

privateType,

`

``

221

`+

privateFormat

`

``

222

`+

} = parseKeyEncoding(type, options));

`

``

223

`+

}

`

201

224

``

202

225

`return impl;

`

203

226

`}

`