author | vinnie |
Tue, 11 Aug 2009 16:52:26 +0100 | |
changeset 3492 | e549cea58864 |
permissions | -rw-r--r-- |
3492
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1 |
/* ********************************************************************* |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2 |
* |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3 |
* Sun elects to have this file available under and governed by the |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4 |
* Mozilla Public License Version 1.1 ("MPL") (see |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
5 |
* http://www.mozilla.org/MPL/ for full license text). For the avoidance |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
6 |
* of doubt and subject to the following, Sun also elects to allow |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
7 |
* licensees to use this file under the MPL, the GNU General Public |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
8 |
* License version 2 only or the Lesser General Public License version |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
9 |
* 2.1 only. Any references to the "GNU General Public License version 2 |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
10 |
* or later" or "GPL" in the following shall be construed to mean the |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
11 |
* GNU General Public License version 2 only. Any references to the "GNU |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
12 |
* Lesser General Public License version 2.1 or later" or "LGPL" in the |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
13 |
* following shall be construed to mean the GNU Lesser General Public |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
14 |
* License version 2.1 only. However, the following notice accompanied |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
15 |
* the original version of this file: |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
16 |
* |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
17 |
* Version: MPL 1.1/GPL 2.0/LGPL 2.1 |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
18 |
* |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
19 |
* The contents of this file are subject to the Mozilla Public License Version |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
20 |
* 1.1 (the "License"); you may not use this file except in compliance with |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
21 |
* the License. You may obtain a copy of the License at |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
22 |
* http://www.mozilla.org/MPL/ |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
23 |
* |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
24 |
* Software distributed under the License is distributed on an "AS IS" basis, |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
25 |
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
26 |
* for the specific language governing rights and limitations under the |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
27 |
* License. |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
28 |
* |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
29 |
* The Original Code is the elliptic curve math library. |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
30 |
* |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
31 |
* The Initial Developer of the Original Code is |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
32 |
* Sun Microsystems, Inc. |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
33 |
* Portions created by the Initial Developer are Copyright (C) 2003 |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
34 |
* the Initial Developer. All Rights Reserved. |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
35 |
* |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
36 |
* Contributor(s): |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
37 |
* Douglas Stebila <douglas@stebila.ca>, Sun Microsystems Laboratories |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
38 |
* |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
39 |
* Alternatively, the contents of this file may be used under the terms of |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
40 |
* either the GNU General Public License Version 2 or later (the "GPL"), or |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
41 |
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
42 |
* in which case the provisions of the GPL or the LGPL are applicable instead |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
43 |
* of those above. If you wish to allow use of your version of this file only |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
44 |
* under the terms of either the GPL or the LGPL, and not to allow others to |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
45 |
* use your version of this file under the terms of the MPL, indicate your |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
46 |
* decision by deleting the provisions above and replace them with the notice |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
47 |
* and other provisions required by the GPL or the LGPL. If you do not delete |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
48 |
* the provisions above, a recipient may use your version of this file under |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
49 |
* the terms of any one of the MPL, the GPL or the LGPL. |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
50 |
* |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
51 |
*********************************************************************** */ |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
52 |
/* |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
53 |
* Copyright 2007 Sun Microsystems, Inc. All rights reserved. |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
54 |
* Use is subject to license terms. |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
55 |
*/ |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
56 |
|
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
57 |
#pragma ident "%Z%%M% %I% %E% SMI" |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
58 |
|
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
59 |
/* Uses Montgomery reduction for field arithmetic. See mpi/mpmontg.c for |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
60 |
* code implementation. */ |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
61 |
|
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
62 |
#include "mpi.h" |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
63 |
#include "mplogic.h" |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
64 |
#include "mpi-priv.h" |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
65 |
#include "ecl-priv.h" |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
66 |
#include "ecp.h" |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
67 |
#ifndef _KERNEL |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
68 |
#include <stdlib.h> |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
69 |
#include <stdio.h> |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
70 |
#endif |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
71 |
|
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
72 |
/* Construct a generic GFMethod for arithmetic over prime fields with |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
73 |
* irreducible irr. */ |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
74 |
GFMethod * |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
75 |
GFMethod_consGFp_mont(const mp_int *irr) |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
76 |
{ |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
77 |
mp_err res = MP_OKAY; |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
78 |
int i; |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
79 |
GFMethod *meth = NULL; |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
80 |
mp_mont_modulus *mmm; |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
81 |
|
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
82 |
meth = GFMethod_consGFp(irr); |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
83 |
if (meth == NULL) |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
84 |
return NULL; |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
85 |
|
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
86 |
#ifdef _KERNEL |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
87 |
mmm = (mp_mont_modulus *) kmem_alloc(sizeof(mp_mont_modulus), |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
88 |
FLAG(irr)); |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
89 |
#else |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
90 |
mmm = (mp_mont_modulus *) malloc(sizeof(mp_mont_modulus)); |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
91 |
#endif |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
92 |
if (mmm == NULL) { |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
93 |
res = MP_MEM; |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
94 |
goto CLEANUP; |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
95 |
} |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
96 |
|
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
97 |
meth->field_mul = &ec_GFp_mul_mont; |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
98 |
meth->field_sqr = &ec_GFp_sqr_mont; |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
99 |
meth->field_div = &ec_GFp_div_mont; |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
100 |
meth->field_enc = &ec_GFp_enc_mont; |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
101 |
meth->field_dec = &ec_GFp_dec_mont; |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
102 |
meth->extra1 = mmm; |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
103 |
meth->extra2 = NULL; |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
104 |
meth->extra_free = &ec_GFp_extra_free_mont; |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
105 |
|
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
106 |
mmm->N = meth->irr; |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
107 |
i = mpl_significant_bits(&meth->irr); |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
108 |
i += MP_DIGIT_BIT - 1; |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
109 |
mmm->b = i - i % MP_DIGIT_BIT; |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
110 |
mmm->n0prime = 0 - s_mp_invmod_radix(MP_DIGIT(&meth->irr, 0)); |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
111 |
|
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
112 |
CLEANUP: |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
113 |
if (res != MP_OKAY) { |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
114 |
GFMethod_free(meth); |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
115 |
return NULL; |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
116 |
} |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
117 |
return meth; |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
118 |
} |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
119 |
|
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
120 |
/* Wrapper functions for generic prime field arithmetic. */ |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
121 |
|
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
122 |
/* Field multiplication using Montgomery reduction. */ |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
123 |
mp_err |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
124 |
ec_GFp_mul_mont(const mp_int *a, const mp_int *b, mp_int *r, |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
125 |
const GFMethod *meth) |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
126 |
{ |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
127 |
mp_err res = MP_OKAY; |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
128 |
|
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
129 |
#ifdef MP_MONT_USE_MP_MUL |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
130 |
/* if MP_MONT_USE_MP_MUL is defined, then the function s_mp_mul_mont |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
131 |
* is not implemented and we have to use mp_mul and s_mp_redc directly |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
132 |
*/ |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
133 |
MP_CHECKOK(mp_mul(a, b, r)); |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
134 |
MP_CHECKOK(s_mp_redc(r, (mp_mont_modulus *) meth->extra1)); |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
135 |
#else |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
136 |
mp_int s; |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
137 |
|
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
138 |
MP_DIGITS(&s) = 0; |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
139 |
/* s_mp_mul_mont doesn't allow source and destination to be the same */ |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
140 |
if ((a == r) || (b == r)) { |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
141 |
MP_CHECKOK(mp_init(&s, FLAG(a))); |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
142 |
MP_CHECKOK(s_mp_mul_mont |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
143 |
(a, b, &s, (mp_mont_modulus *) meth->extra1)); |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
144 |
MP_CHECKOK(mp_copy(&s, r)); |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
145 |
mp_clear(&s); |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
146 |
} else { |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
147 |
return s_mp_mul_mont(a, b, r, (mp_mont_modulus *) meth->extra1); |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
148 |
} |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
149 |
#endif |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
150 |
CLEANUP: |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
151 |
return res; |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
152 |
} |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
153 |
|
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
154 |
/* Field squaring using Montgomery reduction. */ |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
155 |
mp_err |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
156 |
ec_GFp_sqr_mont(const mp_int *a, mp_int *r, const GFMethod *meth) |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
157 |
{ |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
158 |
return ec_GFp_mul_mont(a, a, r, meth); |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
159 |
} |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
160 |
|
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
161 |
/* Field division using Montgomery reduction. */ |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
162 |
mp_err |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
163 |
ec_GFp_div_mont(const mp_int *a, const mp_int *b, mp_int *r, |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
164 |
const GFMethod *meth) |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
165 |
{ |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
166 |
mp_err res = MP_OKAY; |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
167 |
|
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
168 |
/* if A=aZ represents a encoded in montgomery coordinates with Z and # |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
169 |
* and \ respectively represent multiplication and division in |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
170 |
* montgomery coordinates, then A\B = (a/b)Z = (A/B)Z and Binv = |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
171 |
* (1/b)Z = (1/B)(Z^2) where B # Binv = Z */ |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
172 |
MP_CHECKOK(ec_GFp_div(a, b, r, meth)); |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
173 |
MP_CHECKOK(ec_GFp_enc_mont(r, r, meth)); |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
174 |
if (a == NULL) { |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
175 |
MP_CHECKOK(ec_GFp_enc_mont(r, r, meth)); |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
176 |
} |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
177 |
CLEANUP: |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
178 |
return res; |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
179 |
} |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
180 |
|
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
181 |
/* Encode a field element in Montgomery form. See s_mp_to_mont in |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
182 |
* mpi/mpmontg.c */ |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
183 |
mp_err |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
184 |
ec_GFp_enc_mont(const mp_int *a, mp_int *r, const GFMethod *meth) |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
185 |
{ |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
186 |
mp_mont_modulus *mmm; |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
187 |
mp_err res = MP_OKAY; |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
188 |
|
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
189 |
mmm = (mp_mont_modulus *) meth->extra1; |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
190 |
MP_CHECKOK(mpl_lsh(a, r, mmm->b)); |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
191 |
MP_CHECKOK(mp_mod(r, &mmm->N, r)); |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
192 |
CLEANUP: |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
193 |
return res; |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
194 |
} |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
195 |
|
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
196 |
/* Decode a field element from Montgomery form. */ |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
197 |
mp_err |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
198 |
ec_GFp_dec_mont(const mp_int *a, mp_int *r, const GFMethod *meth) |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
199 |
{ |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
200 |
mp_err res = MP_OKAY; |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
201 |
|
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
202 |
if (a != r) { |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
203 |
MP_CHECKOK(mp_copy(a, r)); |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
204 |
} |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
205 |
MP_CHECKOK(s_mp_redc(r, (mp_mont_modulus *) meth->extra1)); |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
206 |
CLEANUP: |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
207 |
return res; |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
208 |
} |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
209 |
|
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
210 |
/* Free the memory allocated to the extra fields of Montgomery GFMethod |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
211 |
* object. */ |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
212 |
void |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
213 |
ec_GFp_extra_free_mont(GFMethod *meth) |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
214 |
{ |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
215 |
if (meth->extra1 != NULL) { |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
216 |
#ifdef _KERNEL |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
217 |
kmem_free(meth->extra1, sizeof(mp_mont_modulus)); |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
218 |
#else |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
219 |
free(meth->extra1); |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
220 |
#endif |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
221 |
meth->extra1 = NULL; |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
222 |
} |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
223 |
} |