1 /*
2 * Copyright (c) 2007, 2015, Oracle and/or its affiliates. All rights reserved.
3 * Use is subject to license terms.
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public License
16 * along with this library; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24 /* *********************************************************************
25 *
26 * The Original Code is the Elliptic Curve Cryptography library.
27 *
28 * The Initial Developer of the Original Code is
29 * Sun Microsystems, Inc.
30 * Portions created by the Initial Developer are Copyright (C) 2003
31 * the Initial Developer. All Rights Reserved.
32 *
33 * Contributor(s):
34 * Dr Vipul Gupta <vipul.gupta@sun.com> and
35 * Douglas Stebila <douglas@stebila.ca>, Sun Microsystems Laboratories
36 *
37 * Last Modified Date from the Original Code: April 2015
38 *********************************************************************** */
39
40 #include "mplogic.h"
41 #include "ec.h"
42 #include "ecl.h"
43
44 #include <sys/types.h>
45 #ifndef _KERNEL
46 #include <stdlib.h>
47 #include <string.h>
48
49 #ifndef _WIN32
50 #include <stdio.h>
51 #include <strings.h>
52 #endif /* _WIN32 */
53
54 #endif
55 #include "ecl-exp.h"
56 #include "mpi.h"
57 #include "ecc_impl.h"
241 }
242
243 /* Generates a new EC key pair. The private key is a supplied
244 * value and the public key is the result of performing a scalar
245 * point multiplication of that value with the curve's base point.
246 */
247 SECStatus
248 ec_NewKey(ECParams *ecParams, ECPrivateKey **privKey,
249 const unsigned char *privKeyBytes, int privKeyLen, int kmflag)
250 {
251 SECStatus rv = SECFailure;
252 PRArenaPool *arena;
253 ECPrivateKey *key;
254 mp_int k;
255 mp_err err = MP_OKAY;
256 int len;
257
258 #if EC_DEBUG
259 printf("ec_NewKey called\n");
260 #endif
261
262 if (!ecParams || !privKey || !privKeyBytes || (privKeyLen < 0)) {
263 PORT_SetError(SEC_ERROR_INVALID_ARGS);
264 return SECFailure;
265 }
266
267 /* Initialize an arena for the EC key. */
268 if (!(arena = PORT_NewArena(NSS_FREEBL_DEFAULT_CHUNKSIZE)))
269 return SECFailure;
270
271 key = (ECPrivateKey *)PORT_ArenaZAlloc(arena, sizeof(ECPrivateKey),
272 kmflag);
273 if (!key) {
274 PORT_FreeArena(arena, PR_TRUE);
275 return SECFailure;
276 }
277
278 /* Set the version number (SEC 1 section C.4 says it should be 1) */
279 SECITEM_AllocItem(arena, &key->version, 1, kmflag);
280 key->version.data[0] = 1;
|
1 /*
2 * Copyright (c) 2007, 2016, Oracle and/or its affiliates. All rights reserved.
3 * Use is subject to license terms.
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public License
16 * along with this library; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24 /* *********************************************************************
25 *
26 * The Original Code is the Elliptic Curve Cryptography library.
27 *
28 * The Initial Developer of the Original Code is
29 * Sun Microsystems, Inc.
30 * Portions created by the Initial Developer are Copyright (C) 2003
31 * the Initial Developer. All Rights Reserved.
32 *
33 * Contributor(s):
34 * Dr Vipul Gupta <vipul.gupta@sun.com> and
35 * Douglas Stebila <douglas@stebila.ca>, Sun Microsystems Laboratories
36 *
37 * Last Modified Date from the Original Code: Nov 2016
38 *********************************************************************** */
39
40 #include "mplogic.h"
41 #include "ec.h"
42 #include "ecl.h"
43
44 #include <sys/types.h>
45 #ifndef _KERNEL
46 #include <stdlib.h>
47 #include <string.h>
48
49 #ifndef _WIN32
50 #include <stdio.h>
51 #include <strings.h>
52 #endif /* _WIN32 */
53
54 #endif
55 #include "ecl-exp.h"
56 #include "mpi.h"
57 #include "ecc_impl.h"
241 }
242
243 /* Generates a new EC key pair. The private key is a supplied
244 * value and the public key is the result of performing a scalar
245 * point multiplication of that value with the curve's base point.
246 */
247 SECStatus
248 ec_NewKey(ECParams *ecParams, ECPrivateKey **privKey,
249 const unsigned char *privKeyBytes, int privKeyLen, int kmflag)
250 {
251 SECStatus rv = SECFailure;
252 PRArenaPool *arena;
253 ECPrivateKey *key;
254 mp_int k;
255 mp_err err = MP_OKAY;
256 int len;
257
258 #if EC_DEBUG
259 printf("ec_NewKey called\n");
260 #endif
261 k.dp = (mp_digit*)NULL;
262
263 if (!ecParams || !privKey || !privKeyBytes || (privKeyLen < 0)) {
264 PORT_SetError(SEC_ERROR_INVALID_ARGS);
265 return SECFailure;
266 }
267
268 /* Initialize an arena for the EC key. */
269 if (!(arena = PORT_NewArena(NSS_FREEBL_DEFAULT_CHUNKSIZE)))
270 return SECFailure;
271
272 key = (ECPrivateKey *)PORT_ArenaZAlloc(arena, sizeof(ECPrivateKey),
273 kmflag);
274 if (!key) {
275 PORT_FreeArena(arena, PR_TRUE);
276 return SECFailure;
277 }
278
279 /* Set the version number (SEC 1 section C.4 says it should be 1) */
280 SECITEM_AllocItem(arena, &key->version, 1, kmflag);
281 key->version.data[0] = 1;
|