]>
git.saurik.com Git - apple/security.git/blob - OSX/libsecurity_cryptkit/lib/elliptic.h
1 /* Copyright (c) 1998,2011,2014 Apple Inc. All Rights Reserved.
3 * NOTICE: USE OF THE MATERIALS ACCOMPANYING THIS NOTICE IS SUBJECT
4 * TO THE TERMS OF THE SIGNED "FAST ELLIPTIC ENCRYPTION (FEE) REFERENCE
5 * SOURCE CODE EVALUATION AGREEMENT" BETWEEN APPLE, INC. AND THE
6 * ORIGINAL LICENSEE THAT OBTAINED THESE MATERIALS FROM APPLE,
7 * INC. ANY USE OF THESE MATERIALS NOT PERMITTED BY SUCH AGREEMENT WILL
8 * EXPOSE YOU TO LIABILITY.
9 ***************************************************************************
11 * elliptic.h - Fast Elliptic Encryption functions.
16 * Changed to compile with C++.
24 #include "giantIntegers.h"
26 #include "curveParams.h"
33 * Twist, or "which curve", parameter.
35 #define CURVE_PLUS ((int)1)
36 #define CURVE_MINUS ((int)(-1))
39 int twist
; // CURVE_PLUS or CURVE_MINUS
40 giant x
; // x coord of public key
43 * only valid for (twist == CURVE_PLUS) and curveType CT_WEIERSTRASS.
44 * Otherwise it's a zero-value giant.
46 giant y
; // y coord of public key
49 * Note: this module never allocs or frees a curveParams structs.
50 * This field is always maintained by clients of this module.
52 curveParams
*cp
; // common curve parameters
55 typedef keystruct
*key
;
58 * Select which curve is the default curve for calculating signatures and
59 * doing key exchange. This *must* be CURVE_PLUS for key exchange to work
60 * with ECDSA keys and curves.
62 #define DEFAULT_CURVE CURVE_PLUS
64 key
new_public(curveParams
*cp
, int twist
);
67 * Specify private data for key created by new_public().
70 void set_priv_key_giant(key k
, giant privGiant
);
73 * Generate new key with twist and k->x from old_key.
75 key
new_public_with_key(key old_key
, curveParams
*cp
);
78 * Returns 1 if all parameters of two keys are equal, else returns 0.
80 int key_equal(key first
, key second
);
83 * De-allocate an allocated key.
85 void free_key(key pub
);
88 * x3 = x1 + x2 on the curve, with sign ambiguity s.
90 * Note that int s is not just the twist field, because both s = +-1 must
91 * be tested in general.
93 void elliptic_add(giant x1
, giant x2
, giant x3
, curveParams
*par
, int s
);
96 * Values for the 's', or sign, argument to elliptic_add().
99 #define SIGN_MINUS (-1)
103 * Elliptic multiply: x := n * {x, 1}
105 void elliptic_simple(giant x
, giant n
, curveParams
*par
);
108 * General elliptic multiply: {xx, zz} := k * {xx, zz}
110 void elliptic(giant xx
, giant zz
, giant k
, curveParams
*par
);
113 * Returns CURVE_PLUS or CURVE_MINUS, indicating which curve a particular
114 * x coordinate resides on.
116 int which_curve(giant x
, curveParams
*par
);
121 void make_base_prim(curveParams
*cp
);
124 * return a new giant that is the pad from private data and public key
126 giant
make_pad(giant privGiant
, key publicKey
);
129 * Returns non-zero if x(p1) cannot be the x-coordinate of the
130 * sum of two points whose respective x-coordinates are x(p2), x(p3).
132 int signature_compare(giant p0x
, giant p1x
, giant p2x
, curveParams
*par
);
135 * Set g := g mod curveOrder;
136 * force g to be between 2 and (curveOrder-2), inclusive.
138 void curveOrderJustify(giant g
, giant curveOrder
);
140 void lesserX1OrderJustify(giant g
, curveParams
*cp
);
141 void x1OrderPlusJustify(giant g
, curveParams
*cp
);
142 void x1OrderPlusMod(giant g
, curveParams
*cp
);
144 void calcX1OrderPlusRecip(curveParams
*cp
);
147 * x := x mod basePrime.
149 void feemod(curveParams
*par
, giant x
);
152 * For a given curveParams, calculate minBytes and maxDigits.
154 void calcGiantSizes(curveParams
*cp
);
155 unsigned giantMinBytes(unsigned q
, int k
);
156 unsigned giantMaxDigits(unsigned minBytes
);
158 int binvg_cp(curveParams
*cp
, giant x
);
159 int binvg_x1OrderPlus(curveParams
*cp
, giant x
);
165 #endif /*_CK_NSFEE_H_*/