src/share/native/sun/security/ec/impl/ec.c

Print this page




  38 
  39 #include "mplogic.h"
  40 #include "ec.h"
  41 #include "ecl.h"
  42 
  43 #include <sys/types.h>
  44 #ifndef _KERNEL
  45 #include <stdlib.h>
  46 #include <string.h>
  47 
  48 #ifndef _WIN32
  49 #include <stdio.h>
  50 #include <strings.h>
  51 #endif /* _WIN32 */
  52 
  53 #endif
  54 #include "ecl-exp.h"
  55 #include "mpi.h"
  56 #include "ecc_impl.h"
  57 
  58 #ifdef _KERNEL
  59 #define PORT_ZFree(p, l)                bzero((p), (l)); kmem_free((p), (l))
  60 #else
  61 #ifndef _WIN32
  62 #define PORT_ZFree(p, l)                bzero((p), (l)); free((p))
  63 #else
  64 #define PORT_ZFree(p, l)                memset((p), 0, (l)); free((p))
  65 #endif /* _WIN32 */
  66 #endif
  67 
  68 /*
  69  * Returns true if pointP is the point at infinity, false otherwise
  70  */
  71 PRBool
  72 ec_point_at_infinity(SECItem *pointP)
  73 {
  74     unsigned int i;
  75 
  76     for (i = 1; i < pointP->len; i++) {
  77         if (pointP->data[i] != 0x00) return PR_FALSE;
  78     }
  79 
  80     return PR_TRUE;
  81 }
  82 
  83 /*
  84  * Computes scalar point multiplication pointQ = k1 * G + k2 * pointP for
  85  * the curve whose parameters are encoded in params with base point G.
  86  */
  87 SECStatus


 405     /* CHECK_SEC_OK( RNG_GenerateGlobalRandomBytes(privKeyBytes, 2*len) );*/
 406     memcpy(privKeyBytes, random, randomlen);
 407 
 408     CHECK_MPI_OK( mp_read_unsigned_octets(&privKeyVal, privKeyBytes, 2*len) );
 409     CHECK_MPI_OK( mp_read_unsigned_octets(&order_1, order, len) );
 410     CHECK_MPI_OK( mp_set_int(&one, 1) );
 411     CHECK_MPI_OK( mp_sub(&order_1, &one, &order_1) );
 412     CHECK_MPI_OK( mp_mod(&privKeyVal, &order_1, &privKeyVal) );
 413     CHECK_MPI_OK( mp_add(&privKeyVal, &one, &privKeyVal) );
 414     CHECK_MPI_OK( mp_to_fixlen_octets(&privKeyVal, privKeyBytes, len) );
 415     memset(privKeyBytes+len, 0, len);
 416 cleanup:
 417     mp_clear(&privKeyVal);
 418     mp_clear(&order_1);
 419     mp_clear(&one);
 420     if (err < MP_OKAY) {
 421         MP_TO_SEC_ERROR(err);
 422         rv = SECFailure;
 423     }
 424     if (rv != SECSuccess && privKeyBytes) {
 425 #ifdef _KERNEL
 426         kmem_free(privKeyBytes, 2*len);
 427 #else
 428         free(privKeyBytes);
 429 #endif
 430         privKeyBytes = NULL;
 431     }
 432     return privKeyBytes;
 433 }
 434 
 435 /* Generates a new EC key pair. The private key is a random value and
 436  * the public key is the result of performing a scalar point multiplication
 437  * of that value with the curve's base point.
 438  */
 439 SECStatus
 440 EC_NewKey(ECParams *ecParams, ECPrivateKey **privKey,
 441     const unsigned char* random, int randomlen, int kmflag)
 442 {
 443     SECStatus rv = SECFailure;
 444     int len;
 445     unsigned char *privKeyBytes = NULL;
 446 
 447     if (!ecParams) {
 448         PORT_SetError(SEC_ERROR_INVALID_ARGS);
 449         return SECFailure;




  38 
  39 #include "mplogic.h"
  40 #include "ec.h"
  41 #include "ecl.h"
  42 
  43 #include <sys/types.h>
  44 #ifndef _KERNEL
  45 #include <stdlib.h>
  46 #include <string.h>
  47 
  48 #ifndef _WIN32
  49 #include <stdio.h>
  50 #include <strings.h>
  51 #endif /* _WIN32 */
  52 
  53 #endif
  54 #include "ecl-exp.h"
  55 #include "mpi.h"
  56 #include "ecc_impl.h"
  57 










  58 /*
  59  * Returns true if pointP is the point at infinity, false otherwise
  60  */
  61 PRBool
  62 ec_point_at_infinity(SECItem *pointP)
  63 {
  64     unsigned int i;
  65 
  66     for (i = 1; i < pointP->len; i++) {
  67         if (pointP->data[i] != 0x00) return PR_FALSE;
  68     }
  69 
  70     return PR_TRUE;
  71 }
  72 
  73 /*
  74  * Computes scalar point multiplication pointQ = k1 * G + k2 * pointP for
  75  * the curve whose parameters are encoded in params with base point G.
  76  */
  77 SECStatus


 395     /* CHECK_SEC_OK( RNG_GenerateGlobalRandomBytes(privKeyBytes, 2*len) );*/
 396     memcpy(privKeyBytes, random, randomlen);
 397 
 398     CHECK_MPI_OK( mp_read_unsigned_octets(&privKeyVal, privKeyBytes, 2*len) );
 399     CHECK_MPI_OK( mp_read_unsigned_octets(&order_1, order, len) );
 400     CHECK_MPI_OK( mp_set_int(&one, 1) );
 401     CHECK_MPI_OK( mp_sub(&order_1, &one, &order_1) );
 402     CHECK_MPI_OK( mp_mod(&privKeyVal, &order_1, &privKeyVal) );
 403     CHECK_MPI_OK( mp_add(&privKeyVal, &one, &privKeyVal) );
 404     CHECK_MPI_OK( mp_to_fixlen_octets(&privKeyVal, privKeyBytes, len) );
 405     memset(privKeyBytes+len, 0, len);
 406 cleanup:
 407     mp_clear(&privKeyVal);
 408     mp_clear(&order_1);
 409     mp_clear(&one);
 410     if (err < MP_OKAY) {
 411         MP_TO_SEC_ERROR(err);
 412         rv = SECFailure;
 413     }
 414     if (rv != SECSuccess && privKeyBytes) {
 415         PORT_ZFree(privKeyBytes, 2*len);




 416         privKeyBytes = NULL;
 417     }
 418     return privKeyBytes;
 419 }
 420 
 421 /* Generates a new EC key pair. The private key is a random value and
 422  * the public key is the result of performing a scalar point multiplication
 423  * of that value with the curve's base point.
 424  */
 425 SECStatus
 426 EC_NewKey(ECParams *ecParams, ECPrivateKey **privKey,
 427     const unsigned char* random, int randomlen, int kmflag)
 428 {
 429     SECStatus rv = SECFailure;
 430     int len;
 431     unsigned char *privKeyBytes = NULL;
 432 
 433     if (!ecParams) {
 434         PORT_SetError(SEC_ERROR_INVALID_ARGS);
 435         return SECFailure;