Fix BSD license name
[platform/upstream/libgcrypt.git] / cipher / ecc-common.h
1 /* ecc-common.h - Declarations of common ECC code
2  * Copyright (C) 2013 g10 Code GmbH
3  *
4  * This file is part of Libgcrypt.
5  *
6  * Libgcrypt is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU Lesser General Public License as
8  * published by the Free Software Foundation; either version 2.1 of
9  * the License, or (at your option) any later version.
10  *
11  * Libgcrypt is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this program; if not, see <http://www.gnu.org/licenses/>.
18  */
19
20 #ifndef GCRY_ECC_COMMON_H
21 #define GCRY_ECC_COMMON_H
22
23
24 /* Definition of a curve.  */
25 typedef struct
26 {
27   enum gcry_mpi_ec_models model;/* The model descrinbing this curve.  */
28   enum ecc_dialects dialect;    /* The dialect used with the curve.   */
29   gcry_mpi_t p;         /* Prime specifying the field GF(p).  */
30   gcry_mpi_t a;         /* First coefficient of the Weierstrass equation.  */
31   gcry_mpi_t b;         /* Second coefficient of the Weierstrass equation.
32                            or d as used by Twisted Edwards curves.  */
33   mpi_point_struct G;   /* Base point (generator).  */
34   gcry_mpi_t n;         /* Order of G.  */
35   const char *name;     /* Name of the curve or NULL.  */
36 } elliptic_curve_t;
37
38
39 typedef struct
40 {
41   elliptic_curve_t E;
42   mpi_point_struct Q; /* Q = [d]G  */
43 } ECC_public_key;
44
45
46 typedef struct
47 {
48   elliptic_curve_t E;
49   mpi_point_struct Q;
50   gcry_mpi_t d;
51 } ECC_secret_key;
52
53
54
55 /* Set the value from S into D.  */
56 static inline void
57 point_set (mpi_point_t d, mpi_point_t s)
58 {
59   mpi_set (d->x, s->x);
60   mpi_set (d->y, s->y);
61   mpi_set (d->z, s->z);
62 }
63
64 #define point_init(a)  _gcry_mpi_point_init ((a))
65 #define point_free(a)  _gcry_mpi_point_free_parts ((a))
66
67
68 /*-- ecc-curves.c --*/
69 gpg_err_code_t _gcry_ecc_fill_in_curve (unsigned int nbits,
70                                         const char *name,
71                                         elliptic_curve_t *curve,
72                                         unsigned int *r_nbits);
73 gpg_err_code_t _gcry_ecc_update_curve_param (const char *name,
74                                              enum gcry_mpi_ec_models *model,
75                                              enum ecc_dialects *dialect,
76                                              gcry_mpi_t *p, gcry_mpi_t *a,
77                                              gcry_mpi_t *b, gcry_mpi_t *g,
78                                              gcry_mpi_t *n);
79
80 const char *_gcry_ecc_get_curve (gcry_sexp_t keyparms,
81                                  int iterator,
82                                  unsigned int *r_nbits);
83 gcry_sexp_t _gcry_ecc_get_param_sexp (const char *name);
84
85 /*-- ecc-misc.c --*/
86 void _gcry_ecc_curve_free (elliptic_curve_t *E);
87 elliptic_curve_t _gcry_ecc_curve_copy (elliptic_curve_t E);
88 const char *_gcry_ecc_model2str (enum gcry_mpi_ec_models model);
89 const char *_gcry_ecc_dialect2str (enum ecc_dialects dialect);
90 gcry_mpi_t   _gcry_ecc_ec2os (gcry_mpi_t x, gcry_mpi_t y, gcry_mpi_t p);
91 gcry_err_code_t _gcry_ecc_os2ec (mpi_point_t result, gcry_mpi_t value);
92
93 mpi_point_t  _gcry_ecc_compute_public (mpi_point_t Q, mpi_ec_t ec,
94                                        mpi_point_t G, gcry_mpi_t d);
95
96 /*-- ecc.c --*/
97
98 /*-- ecc-ecdsa.c --*/
99 gpg_err_code_t _gcry_ecc_ecdsa_sign (gcry_mpi_t input, ECC_secret_key *skey,
100                                      gcry_mpi_t r, gcry_mpi_t s,
101                                      int flags, int hashalgo);
102 gpg_err_code_t _gcry_ecc_ecdsa_verify (gcry_mpi_t input, ECC_public_key *pkey,
103                                        gcry_mpi_t r, gcry_mpi_t s);
104
105 /*-- ecc-eddsa.c --*/
106 gpg_err_code_t _gcry_ecc_eddsa_recover_x (gcry_mpi_t x, gcry_mpi_t y, int sign,
107                                           mpi_ec_t ec);
108 gpg_err_code_t _gcry_ecc_eddsa_encodepoint (mpi_point_t point, mpi_ec_t ctx,
109                                             gcry_mpi_t x, gcry_mpi_t y,
110                                             int with_prefix,
111                                             unsigned char **r_buffer,
112                                             unsigned int *r_buflen);
113 gpg_err_code_t _gcry_ecc_eddsa_ensure_compact (gcry_mpi_t value,
114                                                unsigned int nbits);
115 gpg_err_code_t _gcry_ecc_eddsa_decodepoint (gcry_mpi_t pk, mpi_ec_t ctx,
116                                             mpi_point_t result,
117                                             unsigned char **r_encpk,
118                                             unsigned int *r_encpklen);
119 gpg_err_code_t _gcry_ecc_eddsa_compute_h_d (unsigned char **r_digest,
120                                             gcry_mpi_t d, mpi_ec_t ec);
121
122 gpg_err_code_t _gcry_ecc_eddsa_genkey (ECC_secret_key *sk,
123                                        elliptic_curve_t *E,
124                                        mpi_ec_t ctx,
125                                        gcry_random_level_t random_level);
126 gpg_err_code_t _gcry_ecc_eddsa_sign (gcry_mpi_t input,
127                                      ECC_secret_key *sk,
128                                      gcry_mpi_t r_r, gcry_mpi_t s,
129                                      int hashalgo, gcry_mpi_t pk);
130 gpg_err_code_t _gcry_ecc_eddsa_verify (gcry_mpi_t input,
131                                        ECC_public_key *pk,
132                                        gcry_mpi_t r, gcry_mpi_t s,
133                                        int hashalgo, gcry_mpi_t pkmpi);
134
135 /*-- ecc-gost.c --*/
136 gpg_err_code_t _gcry_ecc_gost_sign (gcry_mpi_t input, ECC_secret_key *skey,
137                                     gcry_mpi_t r, gcry_mpi_t s);
138 gpg_err_code_t _gcry_ecc_gost_verify (gcry_mpi_t input, ECC_public_key *pkey,
139                                       gcry_mpi_t r, gcry_mpi_t s);
140
141
142 #endif /*GCRY_ECC_COMMON_H*/