1*f9fbec18Smcpowers /* 2*f9fbec18Smcpowers * ***** BEGIN LICENSE BLOCK ***** 3*f9fbec18Smcpowers * Version: MPL 1.1/GPL 2.0/LGPL 2.1 4*f9fbec18Smcpowers * 5*f9fbec18Smcpowers * The contents of this file are subject to the Mozilla Public License Version 6*f9fbec18Smcpowers * 1.1 (the "License"); you may not use this file except in compliance with 7*f9fbec18Smcpowers * the License. You may obtain a copy of the License at 8*f9fbec18Smcpowers * http://www.mozilla.org/MPL/ 9*f9fbec18Smcpowers * 10*f9fbec18Smcpowers * Software distributed under the License is distributed on an "AS IS" basis, 11*f9fbec18Smcpowers * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 12*f9fbec18Smcpowers * for the specific language governing rights and limitations under the 13*f9fbec18Smcpowers * License. 14*f9fbec18Smcpowers * 15*f9fbec18Smcpowers * The Original Code is the elliptic curve math library for prime field curves. 16*f9fbec18Smcpowers * 17*f9fbec18Smcpowers * The Initial Developer of the Original Code is 18*f9fbec18Smcpowers * Sun Microsystems, Inc. 19*f9fbec18Smcpowers * Portions created by the Initial Developer are Copyright (C) 2003 20*f9fbec18Smcpowers * the Initial Developer. All Rights Reserved. 21*f9fbec18Smcpowers * 22*f9fbec18Smcpowers * Contributor(s): 23*f9fbec18Smcpowers * Douglas Stebila <douglas@stebila.ca>, Sun Microsystems Laboratories 24*f9fbec18Smcpowers * 25*f9fbec18Smcpowers * Alternatively, the contents of this file may be used under the terms of 26*f9fbec18Smcpowers * either the GNU General Public License Version 2 or later (the "GPL"), or 27*f9fbec18Smcpowers * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), 28*f9fbec18Smcpowers * in which case the provisions of the GPL or the LGPL are applicable instead 29*f9fbec18Smcpowers * of those above. If you wish to allow use of your version of this file only 30*f9fbec18Smcpowers * under the terms of either the GPL or the LGPL, and not to allow others to 31*f9fbec18Smcpowers * use your version of this file under the terms of the MPL, indicate your 32*f9fbec18Smcpowers * decision by deleting the provisions above and replace them with the notice 33*f9fbec18Smcpowers * and other provisions required by the GPL or the LGPL. If you do not delete 34*f9fbec18Smcpowers * the provisions above, a recipient may use your version of this file under 35*f9fbec18Smcpowers * the terms of any one of the MPL, the GPL or the LGPL. 36*f9fbec18Smcpowers * 37*f9fbec18Smcpowers * ***** END LICENSE BLOCK ***** */ 38*f9fbec18Smcpowers /* 39*f9fbec18Smcpowers * Copyright 2007 Sun Microsystems, Inc. All rights reserved. 40*f9fbec18Smcpowers * Use is subject to license terms. 41*f9fbec18Smcpowers * 42*f9fbec18Smcpowers * Sun elects to use this software under the MPL license. 43*f9fbec18Smcpowers */ 44*f9fbec18Smcpowers 45*f9fbec18Smcpowers #pragma ident "%Z%%M% %I% %E% SMI" 46*f9fbec18Smcpowers 47*f9fbec18Smcpowers #include "ecp.h" 48*f9fbec18Smcpowers #include "mpi.h" 49*f9fbec18Smcpowers #include "mplogic.h" 50*f9fbec18Smcpowers #include "mpi-priv.h" 51*f9fbec18Smcpowers #ifndef _KERNEL 52*f9fbec18Smcpowers #include <stdlib.h> 53*f9fbec18Smcpowers #endif 54*f9fbec18Smcpowers 55*f9fbec18Smcpowers #define ECP224_DIGITS ECL_CURVE_DIGITS(224) 56*f9fbec18Smcpowers 57*f9fbec18Smcpowers /* Fast modular reduction for p224 = 2^224 - 2^96 + 1. a can be r. Uses 58*f9fbec18Smcpowers * algorithm 7 from Brown, Hankerson, Lopez, Menezes. Software 59*f9fbec18Smcpowers * Implementation of the NIST Elliptic Curves over Prime Fields. */ 60*f9fbec18Smcpowers mp_err 61*f9fbec18Smcpowers ec_GFp_nistp224_mod(const mp_int *a, mp_int *r, const GFMethod *meth) 62*f9fbec18Smcpowers { 63*f9fbec18Smcpowers mp_err res = MP_OKAY; 64*f9fbec18Smcpowers mp_size a_used = MP_USED(a); 65*f9fbec18Smcpowers 66*f9fbec18Smcpowers int r3b; 67*f9fbec18Smcpowers mp_digit carry; 68*f9fbec18Smcpowers #ifdef ECL_THIRTY_TWO_BIT 69*f9fbec18Smcpowers mp_digit a6a = 0, a6b = 0, 70*f9fbec18Smcpowers a5a = 0, a5b = 0, a4a = 0, a4b = 0, a3a = 0, a3b = 0; 71*f9fbec18Smcpowers mp_digit r0a, r0b, r1a, r1b, r2a, r2b, r3a; 72*f9fbec18Smcpowers #else 73*f9fbec18Smcpowers mp_digit a6 = 0, a5 = 0, a4 = 0, a3b = 0, a5a = 0; 74*f9fbec18Smcpowers mp_digit a6b = 0, a6a_a5b = 0, a5b = 0, a5a_a4b = 0, a4a_a3b = 0; 75*f9fbec18Smcpowers mp_digit r0, r1, r2, r3; 76*f9fbec18Smcpowers #endif 77*f9fbec18Smcpowers 78*f9fbec18Smcpowers /* reduction not needed if a is not larger than field size */ 79*f9fbec18Smcpowers if (a_used < ECP224_DIGITS) { 80*f9fbec18Smcpowers if (a == r) return MP_OKAY; 81*f9fbec18Smcpowers return mp_copy(a, r); 82*f9fbec18Smcpowers } 83*f9fbec18Smcpowers /* for polynomials larger than twice the field size, use regular 84*f9fbec18Smcpowers * reduction */ 85*f9fbec18Smcpowers if (a_used > ECL_CURVE_DIGITS(224*2)) { 86*f9fbec18Smcpowers MP_CHECKOK(mp_mod(a, &meth->irr, r)); 87*f9fbec18Smcpowers } else { 88*f9fbec18Smcpowers #ifdef ECL_THIRTY_TWO_BIT 89*f9fbec18Smcpowers /* copy out upper words of a */ 90*f9fbec18Smcpowers switch (a_used) { 91*f9fbec18Smcpowers case 14: 92*f9fbec18Smcpowers a6b = MP_DIGIT(a, 13); 93*f9fbec18Smcpowers case 13: 94*f9fbec18Smcpowers a6a = MP_DIGIT(a, 12); 95*f9fbec18Smcpowers case 12: 96*f9fbec18Smcpowers a5b = MP_DIGIT(a, 11); 97*f9fbec18Smcpowers case 11: 98*f9fbec18Smcpowers a5a = MP_DIGIT(a, 10); 99*f9fbec18Smcpowers case 10: 100*f9fbec18Smcpowers a4b = MP_DIGIT(a, 9); 101*f9fbec18Smcpowers case 9: 102*f9fbec18Smcpowers a4a = MP_DIGIT(a, 8); 103*f9fbec18Smcpowers case 8: 104*f9fbec18Smcpowers a3b = MP_DIGIT(a, 7); 105*f9fbec18Smcpowers } 106*f9fbec18Smcpowers r3a = MP_DIGIT(a, 6); 107*f9fbec18Smcpowers r2b= MP_DIGIT(a, 5); 108*f9fbec18Smcpowers r2a= MP_DIGIT(a, 4); 109*f9fbec18Smcpowers r1b = MP_DIGIT(a, 3); 110*f9fbec18Smcpowers r1a = MP_DIGIT(a, 2); 111*f9fbec18Smcpowers r0b = MP_DIGIT(a, 1); 112*f9fbec18Smcpowers r0a = MP_DIGIT(a, 0); 113*f9fbec18Smcpowers 114*f9fbec18Smcpowers 115*f9fbec18Smcpowers /* implement r = (a3a,a2,a1,a0) 116*f9fbec18Smcpowers +(a5a, a4,a3b, 0) 117*f9fbec18Smcpowers +( 0, a6,a5b, 0) 118*f9fbec18Smcpowers -( 0 0, 0|a6b, a6a|a5b ) 119*f9fbec18Smcpowers -( a6b, a6a|a5b, a5a|a4b, a4a|a3b ) */ 120*f9fbec18Smcpowers MP_ADD_CARRY (r1b, a3b, r1b, 0, carry); 121*f9fbec18Smcpowers MP_ADD_CARRY (r2a, a4a, r2a, carry, carry); 122*f9fbec18Smcpowers MP_ADD_CARRY (r2b, a4b, r2b, carry, carry); 123*f9fbec18Smcpowers MP_ADD_CARRY (r3a, a5a, r3a, carry, carry); 124*f9fbec18Smcpowers r3b = carry; 125*f9fbec18Smcpowers MP_ADD_CARRY (r1b, a5b, r1b, 0, carry); 126*f9fbec18Smcpowers MP_ADD_CARRY (r2a, a6a, r2a, carry, carry); 127*f9fbec18Smcpowers MP_ADD_CARRY (r2b, a6b, r2b, carry, carry); 128*f9fbec18Smcpowers MP_ADD_CARRY (r3a, 0, r3a, carry, carry); 129*f9fbec18Smcpowers r3b += carry; 130*f9fbec18Smcpowers MP_SUB_BORROW(r0a, a3b, r0a, 0, carry); 131*f9fbec18Smcpowers MP_SUB_BORROW(r0b, a4a, r0b, carry, carry); 132*f9fbec18Smcpowers MP_SUB_BORROW(r1a, a4b, r1a, carry, carry); 133*f9fbec18Smcpowers MP_SUB_BORROW(r1b, a5a, r1b, carry, carry); 134*f9fbec18Smcpowers MP_SUB_BORROW(r2a, a5b, r2a, carry, carry); 135*f9fbec18Smcpowers MP_SUB_BORROW(r2b, a6a, r2b, carry, carry); 136*f9fbec18Smcpowers MP_SUB_BORROW(r3a, a6b, r3a, carry, carry); 137*f9fbec18Smcpowers r3b -= carry; 138*f9fbec18Smcpowers MP_SUB_BORROW(r0a, a5b, r0a, 0, carry); 139*f9fbec18Smcpowers MP_SUB_BORROW(r0b, a6a, r0b, carry, carry); 140*f9fbec18Smcpowers MP_SUB_BORROW(r1a, a6b, r1a, carry, carry); 141*f9fbec18Smcpowers if (carry) { 142*f9fbec18Smcpowers MP_SUB_BORROW(r1b, 0, r1b, carry, carry); 143*f9fbec18Smcpowers MP_SUB_BORROW(r2a, 0, r2a, carry, carry); 144*f9fbec18Smcpowers MP_SUB_BORROW(r2b, 0, r2b, carry, carry); 145*f9fbec18Smcpowers MP_SUB_BORROW(r3a, 0, r3a, carry, carry); 146*f9fbec18Smcpowers r3b -= carry; 147*f9fbec18Smcpowers } 148*f9fbec18Smcpowers 149*f9fbec18Smcpowers while (r3b > 0) { 150*f9fbec18Smcpowers int tmp; 151*f9fbec18Smcpowers MP_ADD_CARRY(r1b, r3b, r1b, 0, carry); 152*f9fbec18Smcpowers if (carry) { 153*f9fbec18Smcpowers MP_ADD_CARRY(r2a, 0, r2a, carry, carry); 154*f9fbec18Smcpowers MP_ADD_CARRY(r2b, 0, r2b, carry, carry); 155*f9fbec18Smcpowers MP_ADD_CARRY(r3a, 0, r3a, carry, carry); 156*f9fbec18Smcpowers } 157*f9fbec18Smcpowers tmp = carry; 158*f9fbec18Smcpowers MP_SUB_BORROW(r0a, r3b, r0a, 0, carry); 159*f9fbec18Smcpowers if (carry) { 160*f9fbec18Smcpowers MP_SUB_BORROW(r0b, 0, r0b, carry, carry); 161*f9fbec18Smcpowers MP_SUB_BORROW(r1a, 0, r1a, carry, carry); 162*f9fbec18Smcpowers MP_SUB_BORROW(r1b, 0, r1b, carry, carry); 163*f9fbec18Smcpowers MP_SUB_BORROW(r2a, 0, r2a, carry, carry); 164*f9fbec18Smcpowers MP_SUB_BORROW(r2b, 0, r2b, carry, carry); 165*f9fbec18Smcpowers MP_SUB_BORROW(r3a, 0, r3a, carry, carry); 166*f9fbec18Smcpowers tmp -= carry; 167*f9fbec18Smcpowers } 168*f9fbec18Smcpowers r3b = tmp; 169*f9fbec18Smcpowers } 170*f9fbec18Smcpowers 171*f9fbec18Smcpowers while (r3b < 0) { 172*f9fbec18Smcpowers mp_digit maxInt = MP_DIGIT_MAX; 173*f9fbec18Smcpowers MP_ADD_CARRY (r0a, 1, r0a, 0, carry); 174*f9fbec18Smcpowers MP_ADD_CARRY (r0b, 0, r0b, carry, carry); 175*f9fbec18Smcpowers MP_ADD_CARRY (r1a, 0, r1a, carry, carry); 176*f9fbec18Smcpowers MP_ADD_CARRY (r1b, maxInt, r1b, carry, carry); 177*f9fbec18Smcpowers MP_ADD_CARRY (r2a, maxInt, r2a, carry, carry); 178*f9fbec18Smcpowers MP_ADD_CARRY (r2b, maxInt, r2b, carry, carry); 179*f9fbec18Smcpowers MP_ADD_CARRY (r3a, maxInt, r3a, carry, carry); 180*f9fbec18Smcpowers r3b += carry; 181*f9fbec18Smcpowers } 182*f9fbec18Smcpowers /* check for final reduction */ 183*f9fbec18Smcpowers /* now the only way we are over is if the top 4 words are all ones */ 184*f9fbec18Smcpowers if ((r3a == MP_DIGIT_MAX) && (r2b == MP_DIGIT_MAX) 185*f9fbec18Smcpowers && (r2a == MP_DIGIT_MAX) && (r1b == MP_DIGIT_MAX) && 186*f9fbec18Smcpowers ((r1a != 0) || (r0b != 0) || (r0a != 0)) ) { 187*f9fbec18Smcpowers /* one last subraction */ 188*f9fbec18Smcpowers MP_SUB_BORROW(r0a, 1, r0a, 0, carry); 189*f9fbec18Smcpowers MP_SUB_BORROW(r0b, 0, r0b, carry, carry); 190*f9fbec18Smcpowers MP_SUB_BORROW(r1a, 0, r1a, carry, carry); 191*f9fbec18Smcpowers r1b = r2a = r2b = r3a = 0; 192*f9fbec18Smcpowers } 193*f9fbec18Smcpowers 194*f9fbec18Smcpowers 195*f9fbec18Smcpowers if (a != r) { 196*f9fbec18Smcpowers MP_CHECKOK(s_mp_pad(r, 7)); 197*f9fbec18Smcpowers } 198*f9fbec18Smcpowers /* set the lower words of r */ 199*f9fbec18Smcpowers MP_SIGN(r) = MP_ZPOS; 200*f9fbec18Smcpowers MP_USED(r) = 7; 201*f9fbec18Smcpowers MP_DIGIT(r, 6) = r3a; 202*f9fbec18Smcpowers MP_DIGIT(r, 5) = r2b; 203*f9fbec18Smcpowers MP_DIGIT(r, 4) = r2a; 204*f9fbec18Smcpowers MP_DIGIT(r, 3) = r1b; 205*f9fbec18Smcpowers MP_DIGIT(r, 2) = r1a; 206*f9fbec18Smcpowers MP_DIGIT(r, 1) = r0b; 207*f9fbec18Smcpowers MP_DIGIT(r, 0) = r0a; 208*f9fbec18Smcpowers #else 209*f9fbec18Smcpowers /* copy out upper words of a */ 210*f9fbec18Smcpowers switch (a_used) { 211*f9fbec18Smcpowers case 7: 212*f9fbec18Smcpowers a6 = MP_DIGIT(a, 6); 213*f9fbec18Smcpowers a6b = a6 >> 32; 214*f9fbec18Smcpowers a6a_a5b = a6 << 32; 215*f9fbec18Smcpowers case 6: 216*f9fbec18Smcpowers a5 = MP_DIGIT(a, 5); 217*f9fbec18Smcpowers a5b = a5 >> 32; 218*f9fbec18Smcpowers a6a_a5b |= a5b; 219*f9fbec18Smcpowers a5b = a5b << 32; 220*f9fbec18Smcpowers a5a_a4b = a5 << 32; 221*f9fbec18Smcpowers a5a = a5 & 0xffffffff; 222*f9fbec18Smcpowers case 5: 223*f9fbec18Smcpowers a4 = MP_DIGIT(a, 4); 224*f9fbec18Smcpowers a5a_a4b |= a4 >> 32; 225*f9fbec18Smcpowers a4a_a3b = a4 << 32; 226*f9fbec18Smcpowers case 4: 227*f9fbec18Smcpowers a3b = MP_DIGIT(a, 3) >> 32; 228*f9fbec18Smcpowers a4a_a3b |= a3b; 229*f9fbec18Smcpowers a3b = a3b << 32; 230*f9fbec18Smcpowers } 231*f9fbec18Smcpowers 232*f9fbec18Smcpowers r3 = MP_DIGIT(a, 3) & 0xffffffff; 233*f9fbec18Smcpowers r2 = MP_DIGIT(a, 2); 234*f9fbec18Smcpowers r1 = MP_DIGIT(a, 1); 235*f9fbec18Smcpowers r0 = MP_DIGIT(a, 0); 236*f9fbec18Smcpowers 237*f9fbec18Smcpowers /* implement r = (a3a,a2,a1,a0) 238*f9fbec18Smcpowers +(a5a, a4,a3b, 0) 239*f9fbec18Smcpowers +( 0, a6,a5b, 0) 240*f9fbec18Smcpowers -( 0 0, 0|a6b, a6a|a5b ) 241*f9fbec18Smcpowers -( a6b, a6a|a5b, a5a|a4b, a4a|a3b ) */ 242*f9fbec18Smcpowers MP_ADD_CARRY (r1, a3b, r1, 0, carry); 243*f9fbec18Smcpowers MP_ADD_CARRY (r2, a4 , r2, carry, carry); 244*f9fbec18Smcpowers MP_ADD_CARRY (r3, a5a, r3, carry, carry); 245*f9fbec18Smcpowers MP_ADD_CARRY (r1, a5b, r1, 0, carry); 246*f9fbec18Smcpowers MP_ADD_CARRY (r2, a6 , r2, carry, carry); 247*f9fbec18Smcpowers MP_ADD_CARRY (r3, 0, r3, carry, carry); 248*f9fbec18Smcpowers 249*f9fbec18Smcpowers MP_SUB_BORROW(r0, a4a_a3b, r0, 0, carry); 250*f9fbec18Smcpowers MP_SUB_BORROW(r1, a5a_a4b, r1, carry, carry); 251*f9fbec18Smcpowers MP_SUB_BORROW(r2, a6a_a5b, r2, carry, carry); 252*f9fbec18Smcpowers MP_SUB_BORROW(r3, a6b , r3, carry, carry); 253*f9fbec18Smcpowers MP_SUB_BORROW(r0, a6a_a5b, r0, 0, carry); 254*f9fbec18Smcpowers MP_SUB_BORROW(r1, a6b , r1, carry, carry); 255*f9fbec18Smcpowers if (carry) { 256*f9fbec18Smcpowers MP_SUB_BORROW(r2, 0, r2, carry, carry); 257*f9fbec18Smcpowers MP_SUB_BORROW(r3, 0, r3, carry, carry); 258*f9fbec18Smcpowers } 259*f9fbec18Smcpowers 260*f9fbec18Smcpowers 261*f9fbec18Smcpowers /* if the value is negative, r3 has a 2's complement 262*f9fbec18Smcpowers * high value */ 263*f9fbec18Smcpowers r3b = (int)(r3 >>32); 264*f9fbec18Smcpowers while (r3b > 0) { 265*f9fbec18Smcpowers r3 &= 0xffffffff; 266*f9fbec18Smcpowers MP_ADD_CARRY(r1,((mp_digit)r3b) << 32, r1, 0, carry); 267*f9fbec18Smcpowers if (carry) { 268*f9fbec18Smcpowers MP_ADD_CARRY(r2, 0, r2, carry, carry); 269*f9fbec18Smcpowers MP_ADD_CARRY(r3, 0, r3, carry, carry); 270*f9fbec18Smcpowers } 271*f9fbec18Smcpowers MP_SUB_BORROW(r0, r3b, r0, 0, carry); 272*f9fbec18Smcpowers if (carry) { 273*f9fbec18Smcpowers MP_SUB_BORROW(r1, 0, r1, carry, carry); 274*f9fbec18Smcpowers MP_SUB_BORROW(r2, 0, r2, carry, carry); 275*f9fbec18Smcpowers MP_SUB_BORROW(r3, 0, r3, carry, carry); 276*f9fbec18Smcpowers } 277*f9fbec18Smcpowers r3b = (int)(r3 >>32); 278*f9fbec18Smcpowers } 279*f9fbec18Smcpowers 280*f9fbec18Smcpowers while (r3b < 0) { 281*f9fbec18Smcpowers MP_ADD_CARRY (r0, 1, r0, 0, carry); 282*f9fbec18Smcpowers MP_ADD_CARRY (r1, MP_DIGIT_MAX <<32, r1, carry, carry); 283*f9fbec18Smcpowers MP_ADD_CARRY (r2, MP_DIGIT_MAX, r2, carry, carry); 284*f9fbec18Smcpowers MP_ADD_CARRY (r3, MP_DIGIT_MAX >> 32, r3, carry, carry); 285*f9fbec18Smcpowers r3b = (int)(r3 >>32); 286*f9fbec18Smcpowers } 287*f9fbec18Smcpowers /* check for final reduction */ 288*f9fbec18Smcpowers /* now the only way we are over is if the top 4 words are all ones */ 289*f9fbec18Smcpowers if ((r3 == (MP_DIGIT_MAX >> 32)) && (r2 == MP_DIGIT_MAX) 290*f9fbec18Smcpowers && ((r1 & MP_DIGIT_MAX << 32)== MP_DIGIT_MAX << 32) && 291*f9fbec18Smcpowers ((r1 != MP_DIGIT_MAX << 32 ) || (r0 != 0)) ) { 292*f9fbec18Smcpowers /* one last subraction */ 293*f9fbec18Smcpowers MP_SUB_BORROW(r0, 1, r0, 0, carry); 294*f9fbec18Smcpowers MP_SUB_BORROW(r1, 0, r1, carry, carry); 295*f9fbec18Smcpowers r2 = r3 = 0; 296*f9fbec18Smcpowers } 297*f9fbec18Smcpowers 298*f9fbec18Smcpowers 299*f9fbec18Smcpowers if (a != r) { 300*f9fbec18Smcpowers MP_CHECKOK(s_mp_pad(r, 4)); 301*f9fbec18Smcpowers } 302*f9fbec18Smcpowers /* set the lower words of r */ 303*f9fbec18Smcpowers MP_SIGN(r) = MP_ZPOS; 304*f9fbec18Smcpowers MP_USED(r) = 4; 305*f9fbec18Smcpowers MP_DIGIT(r, 3) = r3; 306*f9fbec18Smcpowers MP_DIGIT(r, 2) = r2; 307*f9fbec18Smcpowers MP_DIGIT(r, 1) = r1; 308*f9fbec18Smcpowers MP_DIGIT(r, 0) = r0; 309*f9fbec18Smcpowers #endif 310*f9fbec18Smcpowers } 311*f9fbec18Smcpowers 312*f9fbec18Smcpowers CLEANUP: 313*f9fbec18Smcpowers return res; 314*f9fbec18Smcpowers } 315*f9fbec18Smcpowers 316*f9fbec18Smcpowers /* Compute the square of polynomial a, reduce modulo p224. Store the 317*f9fbec18Smcpowers * result in r. r could be a. Uses optimized modular reduction for p224. 318*f9fbec18Smcpowers */ 319*f9fbec18Smcpowers mp_err 320*f9fbec18Smcpowers ec_GFp_nistp224_sqr(const mp_int *a, mp_int *r, const GFMethod *meth) 321*f9fbec18Smcpowers { 322*f9fbec18Smcpowers mp_err res = MP_OKAY; 323*f9fbec18Smcpowers 324*f9fbec18Smcpowers MP_CHECKOK(mp_sqr(a, r)); 325*f9fbec18Smcpowers MP_CHECKOK(ec_GFp_nistp224_mod(r, r, meth)); 326*f9fbec18Smcpowers CLEANUP: 327*f9fbec18Smcpowers return res; 328*f9fbec18Smcpowers } 329*f9fbec18Smcpowers 330*f9fbec18Smcpowers /* Compute the product of two polynomials a and b, reduce modulo p224. 331*f9fbec18Smcpowers * Store the result in r. r could be a or b; a could be b. Uses 332*f9fbec18Smcpowers * optimized modular reduction for p224. */ 333*f9fbec18Smcpowers mp_err 334*f9fbec18Smcpowers ec_GFp_nistp224_mul(const mp_int *a, const mp_int *b, mp_int *r, 335*f9fbec18Smcpowers const GFMethod *meth) 336*f9fbec18Smcpowers { 337*f9fbec18Smcpowers mp_err res = MP_OKAY; 338*f9fbec18Smcpowers 339*f9fbec18Smcpowers MP_CHECKOK(mp_mul(a, b, r)); 340*f9fbec18Smcpowers MP_CHECKOK(ec_GFp_nistp224_mod(r, r, meth)); 341*f9fbec18Smcpowers CLEANUP: 342*f9fbec18Smcpowers return res; 343*f9fbec18Smcpowers } 344*f9fbec18Smcpowers 345*f9fbec18Smcpowers /* Divides two field elements. If a is NULL, then returns the inverse of 346*f9fbec18Smcpowers * b. */ 347*f9fbec18Smcpowers mp_err 348*f9fbec18Smcpowers ec_GFp_nistp224_div(const mp_int *a, const mp_int *b, mp_int *r, 349*f9fbec18Smcpowers const GFMethod *meth) 350*f9fbec18Smcpowers { 351*f9fbec18Smcpowers mp_err res = MP_OKAY; 352*f9fbec18Smcpowers mp_int t; 353*f9fbec18Smcpowers 354*f9fbec18Smcpowers /* If a is NULL, then return the inverse of b, otherwise return a/b. */ 355*f9fbec18Smcpowers if (a == NULL) { 356*f9fbec18Smcpowers return mp_invmod(b, &meth->irr, r); 357*f9fbec18Smcpowers } else { 358*f9fbec18Smcpowers /* MPI doesn't support divmod, so we implement it using invmod and 359*f9fbec18Smcpowers * mulmod. */ 360*f9fbec18Smcpowers MP_CHECKOK(mp_init(&t, FLAG(b))); 361*f9fbec18Smcpowers MP_CHECKOK(mp_invmod(b, &meth->irr, &t)); 362*f9fbec18Smcpowers MP_CHECKOK(mp_mul(a, &t, r)); 363*f9fbec18Smcpowers MP_CHECKOK(ec_GFp_nistp224_mod(r, r, meth)); 364*f9fbec18Smcpowers CLEANUP: 365*f9fbec18Smcpowers mp_clear(&t); 366*f9fbec18Smcpowers return res; 367*f9fbec18Smcpowers } 368*f9fbec18Smcpowers } 369*f9fbec18Smcpowers 370*f9fbec18Smcpowers /* Wire in fast field arithmetic and precomputation of base point for 371*f9fbec18Smcpowers * named curves. */ 372*f9fbec18Smcpowers mp_err 373*f9fbec18Smcpowers ec_group_set_gfp224(ECGroup *group, ECCurveName name) 374*f9fbec18Smcpowers { 375*f9fbec18Smcpowers if (name == ECCurve_NIST_P224) { 376*f9fbec18Smcpowers group->meth->field_mod = &ec_GFp_nistp224_mod; 377*f9fbec18Smcpowers group->meth->field_mul = &ec_GFp_nistp224_mul; 378*f9fbec18Smcpowers group->meth->field_sqr = &ec_GFp_nistp224_sqr; 379*f9fbec18Smcpowers group->meth->field_div = &ec_GFp_nistp224_div; 380*f9fbec18Smcpowers } 381*f9fbec18Smcpowers return MP_OKAY; 382*f9fbec18Smcpowers } 383