1 /* Copyright 2016 Brian Smith.
2  *
3  * Permission to use, copy, modify, and/or distribute this software for any
4  * purpose with or without fee is hereby granted, provided that the above
5  * copyright notice and this permission notice appear in all copies.
6  *
7  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHORS DISCLAIM ALL WARRANTIES
8  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
9  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
10  * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
11  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
12  * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
13  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
14 
15 #include "./p256_shared.h"
16 
17 #include "../../limbs/limbs.h"
18 
19 #if !defined(OPENSSL_USE_NISTZ256)
20 
21 typedef Limb ScalarMont[P256_LIMBS];
22 typedef Limb Scalar[P256_LIMBS];
23 
24 #include "../bn/internal.h"
25 
p256_scalar_mul_mont(ScalarMont r,const ScalarMont a,const ScalarMont b)26 void p256_scalar_mul_mont(ScalarMont r, const ScalarMont a,
27                               const ScalarMont b) {
28   static const BN_ULONG N[] = {
29     TOBN(0xf3b9cac2, 0xfc632551),
30     TOBN(0xbce6faad, 0xa7179e84),
31     TOBN(0xffffffff, 0xffffffff),
32     TOBN(0xffffffff, 0x00000000),
33   };
34   static const BN_ULONG N_N0[] = {
35     BN_MONT_CTX_N0(0xccd1c8aa, 0xee00bc4f)
36   };
37   /* XXX: Inefficient. TODO: optimize with dedicated multiplication routine. */
38   bn_mul_mont(r, a, b, N, N_N0, P256_LIMBS);
39 }
40 
41 /* XXX: Inefficient. TODO: optimize with dedicated squaring routine. */
p256_scalar_sqr_rep_mont(ScalarMont r,const ScalarMont a,Limb rep)42 void p256_scalar_sqr_rep_mont(ScalarMont r, const ScalarMont a, Limb rep) {
43   dev_assert_secret(rep >= 1);
44   p256_scalar_mul_mont(r, a, a);
45   for (Limb i = 1; i < rep; ++i) {
46     p256_scalar_mul_mont(r, r, r);
47   }
48 }
49 
50 #endif
51