Blame SOURCES/ecc.c

9199b3
/*
9199b3
 * Copyright (C) 2011-2012 Free Software Foundation, Inc.
9199b3
 *
9199b3
 * Author: Nikos Mavrogiannopoulos
9199b3
 *
9199b3
 * This file is part of GnuTLS.
9199b3
 *
9199b3
 * The GnuTLS is free software; you can redistribute it and/or
9199b3
 * modify it under the terms of the GNU Lesser General Public License
9199b3
 * as published by the Free Software Foundation; either version 2.1 of
9199b3
 * the License, or (at your option) any later version.
9199b3
 *
9199b3
 * This library is distributed in the hope that it will be useful, but
9199b3
 * WITHOUT ANY WARRANTY; without even the implied warranty of
9199b3
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
9199b3
 * Lesser General Public License for more details.
9199b3
 *
9199b3
 * You should have received a copy of the GNU Lesser General Public License
9199b3
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
9199b3
 *
9199b3
 */
9199b3
9199b3
#include <gnutls_int.h>
9199b3
#include <algorithms.h>
9199b3
#include <gnutls_errors.h>
9199b3
#include <x509/common.h>
9199b3
9199b3
9199b3
/* Supported ECC curves
9199b3
 */
9199b3
9199b3
static const gnutls_ecc_curve_entry_st ecc_curves[] = {
9199b3
  {
9199b3
    .name = "SECP256R1", 
9199b3
    .oid = "1.2.840.10045.3.1.7",
9199b3
    .id = GNUTLS_ECC_CURVE_SECP256R1,
9199b3
    .tls_id = 23,
9199b3
    .size = 32,
9199b3
    .prime = "FFFFFFFF00000001000000000000000000000000FFFFFFFFFFFFFFFFFFFFFFFF",
9199b3
    .A = "FFFFFFFF00000001000000000000000000000000FFFFFFFFFFFFFFFFFFFFFFFC",
9199b3
    .B = "5AC635D8AA3A93E7B3EBBD55769886BC651D06B0CC53B0F63BCE3C3E27D2604B",
9199b3
    .order = "FFFFFFFF00000000FFFFFFFFFFFFFFFFBCE6FAADA7179E84F3B9CAC2FC632551",
9199b3
    .Gx = "6B17D1F2E12C4247F8BCE6E563A440F277037D812DEB33A0F4A13945D898C296",
9199b3
    .Gy = "4FE342E2FE1A7F9B8EE7EB4A7C0F9E162BCE33576B315ECECBB6406837BF51F5",
9199b3
  },
9199b3
  {
9199b3
    .name = "SECP384R1",
9199b3
    .oid = "1.3.132.0.34",
9199b3
    .id = GNUTLS_ECC_CURVE_SECP384R1,
9199b3
    .tls_id = 24,
9199b3
    .size = 48,
9199b3
    .prime = "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFFFF0000000000000000FFFFFFFF",
9199b3
    .A = "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFFFF0000000000000000FFFFFFFC",
9199b3
    .B = "B3312FA7E23EE7E4988E056BE3F82D19181D9C6EFE8141120314088F5013875AC656398D8A2ED19D2A85C8EDD3EC2AEF",
9199b3
    .order = "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFC7634D81F4372DDF581A0DB248B0A77AECEC196ACCC52973",
9199b3
    .Gx = "AA87CA22BE8B05378EB1C71EF320AD746E1D3B628BA79B9859F741E082542A385502F25DBF55296C3A545E3872760AB7",
9199b3
    .Gy = "3617DE4A96262C6F5D9E98BF9292DC29F8F41DBD289A147CE9DA3113B5F0B8C00A60B1CE1D7E819D7A431D7C90EA0E5F"
9199b3
  },
127c81
  {
127c81
    .name = "SECP521R1",
127c81
    .oid = "1.3.132.0.35",
127c81
    .id = GNUTLS_ECC_CURVE_SECP521R1,
127c81
    .tls_id = 25,
127c81
    .size = 66,
127c81
    .prime = "01FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
127c81
    .A = "01FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFC",
127c81
    .B = "0051953EB9618E1C9A1F929A21A0B68540EEA2DA725B99B315F3B8B489918EF109E156193951EC7E937B1652C0BD3BB1BF073573DF883D2C34F1EF451FD46B503F00",
127c81
    .order = "01FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFA51868783BF2F966B7FCC0148F709A5D03BB5C9B8899C47AEBB6FB71E91386409",
127c81
    .Gx =    "00C6858E06B70404E9CD9E3ECB662395B4429C648139053FB521F828AF606B4D3DBAA14B5E77EFE75928FE1DC127A2FFA8DE3348B3C1856A429BF97E7E31C2E5BD66",
127c81
    .Gy =    "011839296A789A3BC0045C8A5FB42C7D1BD998F54449579B446817AFBD17273E662C97EE72995EF42640C550B9013FAD0761353C7086A272C24088BE94769FD16650",
127c81
  },
9199b3
  {0, 0, 0}
9199b3
};
9199b3
9199b3
#define GNUTLS_ECC_CURVE_LOOP(b) \
9199b3
	{ const gnutls_ecc_curve_entry_st *p; \
9199b3
                for(p = ecc_curves; p->name != NULL; p++) { b ; } }
9199b3
9199b3
9199b3
/* Returns the TLS id of the given curve
9199b3
 */
9199b3
int
9199b3
_gnutls_tls_id_to_ecc_curve (int num)
9199b3
{
9199b3
  gnutls_ecc_curve_t ret = GNUTLS_ECC_CURVE_INVALID;
9199b3
9199b3
  GNUTLS_ECC_CURVE_LOOP (
9199b3
  if (p->tls_id == num) 
9199b3
    {
9199b3
      ret = p->id;
9199b3
      break;
9199b3
    }
9199b3
  );
9199b3
  
9199b3
  return ret;
9199b3
}
9199b3
9199b3
/**
9199b3
 * gnutls_ecc_curve_list:
9199b3
 *
9199b3
 * Get the list of supported elliptic curves.
9199b3
 *
9199b3
 * This function is not thread safe.
9199b3
 *
9199b3
 * Returns: Return a (0)-terminated list of #gnutls_ecc_curve_t
9199b3
 *   integers indicating the available curves.
9199b3
 **/
9199b3
const gnutls_ecc_curve_t *
9199b3
gnutls_ecc_curve_list (void)
9199b3
{
9199b3
static gnutls_ecc_curve_t supported_curves[MAX_ALGOS] = { 0 };
9199b3
9199b3
  if (supported_curves[0] == 0)
9199b3
    {
9199b3
      int i = 0;
9199b3
9199b3
      GNUTLS_ECC_CURVE_LOOP ( 
9199b3
        supported_curves[i++]=p->id;
9199b3
      );
9199b3
      supported_curves[i++]=0;
9199b3
    }
9199b3
9199b3
  return supported_curves;
9199b3
}
9199b3
9199b3
/* Maps numbers to TLS NamedCurve IDs (RFC4492).
9199b3
 * Returns a negative number on error.
9199b3
 */
9199b3
int
9199b3
_gnutls_ecc_curve_get_tls_id (gnutls_ecc_curve_t supported_ecc)
9199b3
{
9199b3
  int ret = GNUTLS_E_RECEIVED_ILLEGAL_PARAMETER;
9199b3
9199b3
  GNUTLS_ECC_CURVE_LOOP (
9199b3
  if (p->id == supported_ecc) 
9199b3
    {
9199b3
      ret = p->tls_id;
9199b3
      break;
9199b3
    }
9199b3
  );
9199b3
  
9199b3
  return ret;
9199b3
}
9199b3
9199b3
/*-
9199b3
 * _gnutls_oid_to_ecc_curve:
9199b3
 * @oid: is a curve's OID
9199b3
 *
9199b3
 * Returns: return a #gnutls_ecc_curve_t value corresponding to
9199b3
 *   the specified OID, or %GNUTLS_ECC_CURVE_INVALID on error.
9199b3
 -*/
9199b3
gnutls_ecc_curve_t _gnutls_oid_to_ecc_curve (const char* oid)
9199b3
{
9199b3
  gnutls_ecc_curve_t ret = GNUTLS_ECC_CURVE_INVALID;
9199b3
9199b3
  GNUTLS_ECC_CURVE_LOOP (
9199b3
  if (strcasecmp (p->oid, oid) == 0) 
9199b3
    {
9199b3
      ret = p->id;
9199b3
      break;
9199b3
    }
9199b3
  );
9199b3
9199b3
  return ret;
9199b3
}
9199b3
9199b3
/*-
9199b3
 * _gnutls_ecc_curve_get_id:
9199b3
 * @name: is a curve name
9199b3
 *
9199b3
 * The names are compared in a case insensitive way.
9199b3
 *
9199b3
 * Returns: return a #gnutls_ecc_curve_t value corresponding to
9199b3
 *   the specified curve, or %GNUTLS_ECC_CURVE_INVALID on error.
9199b3
 -*/
9199b3
gnutls_ecc_curve_t
9199b3
_gnutls_ecc_curve_get_id (const char *name)
9199b3
{
9199b3
  gnutls_ecc_curve_t ret = GNUTLS_ECC_CURVE_INVALID;
9199b3
9199b3
  GNUTLS_ECC_CURVE_LOOP (
9199b3
  if (strcasecmp (p->name, name) == 0) 
9199b3
    {
9199b3
      ret = p->id;
9199b3
      break;
9199b3
    }
9199b3
  );
9199b3
9199b3
  return ret;
9199b3
}
9199b3
9199b3
/*-
9199b3
 * _gnutls_ecc_bits_to_curve:
9199b3
 * @bits: is a security parameter in bits
9199b3
 *
9199b3
 * Returns: return a #gnutls_ecc_curve_t value corresponding to
9199b3
 *   the specified bit length, or %GNUTLS_ECC_CURVE_INVALID on error.
9199b3
 -*/
9199b3
gnutls_ecc_curve_t
9199b3
_gnutls_ecc_bits_to_curve (int bits)
9199b3
{
9199b3
  gnutls_ecc_curve_t ret = GNUTLS_ECC_CURVE_SECP224R1;
9199b3
9199b3
  GNUTLS_ECC_CURVE_LOOP (
9199b3
    if (8*p->size >= bits)
9199b3
      {
9199b3
        ret = p->id;
9199b3
        break;
9199b3
      }
9199b3
  );
9199b3
9199b3
  return ret;
9199b3
}
9199b3
9199b3
/**
9199b3
 * gnutls_ecc_curve_get_name:
9199b3
 * @curve: is an ECC curve
9199b3
 *
9199b3
 * Convert a #gnutls_ecc_curve_t value to a string.
9199b3
 *
9199b3
 * Returns: a string that contains the name of the specified
9199b3
 *   curve or %NULL.
9199b3
 *
9199b3
 * Since: 3.0
9199b3
 **/
9199b3
const char *
9199b3
gnutls_ecc_curve_get_name (gnutls_ecc_curve_t curve)
9199b3
{
9199b3
  const char *ret = NULL;
9199b3
9199b3
  GNUTLS_ECC_CURVE_LOOP(
9199b3
    if (p->id == curve)
9199b3
      {
9199b3
        ret = p->name;
9199b3
        break;
9199b3
      }
9199b3
  );
9199b3
9199b3
  return ret;
9199b3
}
9199b3
9199b3
/*-
9199b3
 * _gnutls_ecc_curve_get_oid:
9199b3
 * @curve: is an ECC curve
9199b3
 *
9199b3
 * Convert a #gnutls_ecc_curve_t value to a string.
9199b3
 *
9199b3
 * Returns: a string that contains the name of the specified
9199b3
 *   curve or %NULL.
9199b3
 -*/
9199b3
const char *
9199b3
_gnutls_ecc_curve_get_oid (gnutls_ecc_curve_t curve)
9199b3
{
9199b3
  const char *ret = NULL;
9199b3
9199b3
  GNUTLS_ECC_CURVE_LOOP(
9199b3
    if (p->id == curve)
9199b3
      {
9199b3
        ret = p->oid;
9199b3
        break;
9199b3
      }
9199b3
  );
9199b3
9199b3
  return ret;
9199b3
}
9199b3
9199b3
/*-
9199b3
 * _gnutls_ecc_curve_get_params:
9199b3
 * @curve: is an ECC curve
9199b3
 *
9199b3
 * Returns the information on a curve.
9199b3
 *
9199b3
 * Returns: a pointer to #gnutls_ecc_curve_entry_st or %NULL.
9199b3
 -*/
9199b3
const gnutls_ecc_curve_entry_st *
9199b3
_gnutls_ecc_curve_get_params (gnutls_ecc_curve_t curve)
9199b3
{
9199b3
  const gnutls_ecc_curve_entry_st *ret = NULL;
9199b3
9199b3
  GNUTLS_ECC_CURVE_LOOP(
9199b3
    if (p->id == curve)
9199b3
      {
9199b3
        ret = p;
9199b3
        break;
9199b3
      }
9199b3
  );
9199b3
9199b3
  return ret;
9199b3
}
9199b3
9199b3
/**
9199b3
 * gnutls_ecc_curve_get_size:
9199b3
 * @curve: is an ECC curve
9199b3
 *
9199b3
 * Returns the size in bytes of the curve.
9199b3
 *
9199b3
 * Returns: a the size or (0).
9199b3
 *
9199b3
 * Since: 3.0
9199b3
 **/
9199b3
int gnutls_ecc_curve_get_size (gnutls_ecc_curve_t curve)
9199b3
{
9199b3
  int ret = 0;
9199b3
9199b3
  GNUTLS_ECC_CURVE_LOOP(
9199b3
    if (p->id == curve)
9199b3
      {
9199b3
        ret = p->size;
9199b3
        break;
9199b3
      }
9199b3
  );
9199b3
9199b3
  return ret;
9199b3
}