ctc_ecc.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /* ctc_ecc.h
  2. *
  3. * Copyright (C) 2006-2011 Sawtooth Consulting Ltd.
  4. *
  5. * This file is part of CyaSSL.
  6. *
  7. * CyaSSL is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * CyaSSL is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
  20. */
  21. #ifdef HAVE_ECC
  22. #ifndef CTAO_CRYPT_ECC_H
  23. #define CTAO_CRYPT_ECC_H
  24. #include "types.h"
  25. #include "integer.h"
  26. #include "random.h"
  27. #ifdef __cplusplus
  28. extern "C" {
  29. #endif
  30. enum {
  31. ECC_PUBLICKEY = 1,
  32. ECC_PRIVATEKEY = 2,
  33. ECC_MAXNAME = 16, /* MAX CURVE NAME LENGTH */
  34. SIG_HEADER_SZ = 6, /* ECC signature header size */
  35. ECC_BUFSIZE = 256, /* for exported keys temp buffer */
  36. ECC_MAXSIZE = 66 /* MAX Private Key size */
  37. };
  38. /* ECC set type defined a NIST GF(p) curve */
  39. typedef struct {
  40. int size; /* The size of the curve in octets */
  41. char* name; /* name of this curve */
  42. char* prime; /* prime that defines the field the curve is in (hex) */
  43. char* B; /* fields B param (hex) */
  44. char* order; /* order of the curve (hex) */
  45. char* Gx; /* x coordinate of the base point on curve (hex) */
  46. char* Gy; /* y coordinate of the base point on curve (hex) */
  47. } ecc_set_type;
  48. /* A point on an ECC curve, stored in Jacbobian format such that (x,y,z) =>
  49. (x/z^2, y/z^3, 1) when interpreted as affine */
  50. typedef struct {
  51. mp_int x; /* The x coordinate */
  52. mp_int y; /* The y coordinate */
  53. mp_int z; /* The z coordinate */
  54. } ecc_point;
  55. /* An ECC Key */
  56. typedef struct {
  57. int type; /* Public or Private */
  58. int idx; /* Index into the ecc_sets[] for the parameters of
  59. this curve if -1, this key is using user supplied
  60. curve in dp */
  61. const ecc_set_type* dp; /* domain parameters, either points to NIST
  62. curves (idx >= 0) or user supplied */
  63. ecc_point pubkey; /* public key */
  64. mp_int k; /* private key */
  65. } ecc_key;
  66. /* ECC predefined curve sets */
  67. extern const ecc_set_type ecc_sets[];
  68. int ecc_make_key(RNG* rng, int keysize, ecc_key* key);
  69. int ecc_shared_secret(ecc_key* private_key, ecc_key* public_key, byte* out,
  70. word32* outlen);
  71. int ecc_sign_hash(const byte* in, word32 inlen, byte* out, word32 *outlen,
  72. RNG* rng, ecc_key* key);
  73. int ecc_verify_hash(const byte* sig, word32 siglen, byte* hash, word32 hashlen,
  74. int* stat, ecc_key* key);
  75. void ecc_init(ecc_key* key);
  76. void ecc_free(ecc_key* key);
  77. /* ASN key helpers */
  78. int ecc_export_x963(ecc_key*, byte* out, word32* outLen);
  79. int ecc_import_x963(const byte* in, word32 inLen, ecc_key* key);
  80. int ecc_import_private_key(const byte* priv, word32 privSz, const byte* pub,
  81. word32 pubSz, ecc_key* key);
  82. /* size helper */
  83. int ecc_size(ecc_key* key);
  84. int ecc_sig_size(ecc_key* key);
  85. /* TODO: fix mutex types */
  86. #define MUTEX_GLOBAL(x) int (x);
  87. #define MUTEX_LOCK(x)
  88. #define MUTEX_UNLOCK(x)
  89. #ifdef __cplusplus
  90. } /* extern "C" */
  91. #endif
  92. #endif /* CTAO_CRYPT_ECC_H */
  93. #endif /* HAVE_ECC */