summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authortb <>2024-11-12 10:44:25 +0000
committertb <>2024-11-12 10:44:25 +0000
commit62ef3857e7799820c922e269826a302dbfb1416a (patch)
treee3394b5fbb144659bde7d7f9b6cda5721b9b3ca3
parent87d0c26dfb60b5b40ce00952f8f5c491bc12ddad (diff)
downloadopenbsd-62ef3857e7799820c922e269826a302dbfb1416a.tar.gz
openbsd-62ef3857e7799820c922e269826a302dbfb1416a.tar.bz2
openbsd-62ef3857e7799820c922e269826a302dbfb1416a.zip
Merge ecp_mont.c into ecp_methods.c
-rw-r--r--src/lib/libcrypto/Makefile3
-rw-r--r--src/lib/libcrypto/ec/ecp_methods.c206
-rw-r--r--src/lib/libcrypto/ec/ecp_mont.c270
3 files changed, 206 insertions, 273 deletions
diff --git a/src/lib/libcrypto/Makefile b/src/lib/libcrypto/Makefile
index 796f6a2419..f42ac2b9bf 100644
--- a/src/lib/libcrypto/Makefile
+++ b/src/lib/libcrypto/Makefile
@@ -1,4 +1,4 @@
1# $OpenBSD: Makefile,v 1.226 2024/11/12 10:25:16 tb Exp $ 1# $OpenBSD: Makefile,v 1.227 2024/11/12 10:44:25 tb Exp $
2 2
3LIB= crypto 3LIB= crypto
4LIBREBUILD=y 4LIBREBUILD=y
@@ -287,7 +287,6 @@ SRCS+= ec_mult.c
287SRCS+= ec_pmeth.c 287SRCS+= ec_pmeth.c
288SRCS+= eck_prn.c 288SRCS+= eck_prn.c
289SRCS+= ecp_methods.c 289SRCS+= ecp_methods.c
290SRCS+= ecp_mont.c
291SRCS+= ecx_methods.c 290SRCS+= ecx_methods.c
292 291
293# ecdh/ 292# ecdh/
diff --git a/src/lib/libcrypto/ec/ecp_methods.c b/src/lib/libcrypto/ec/ecp_methods.c
index 9cce6880df..f3c9f05850 100644
--- a/src/lib/libcrypto/ec/ecp_methods.c
+++ b/src/lib/libcrypto/ec/ecp_methods.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: ecp_methods.c,v 1.2 2024/11/12 10:26:06 tb Exp $ */ 1/* $OpenBSD: ecp_methods.c,v 1.3 2024/11/12 10:44:25 tb Exp $ */
2/* Includes code written by Lenka Fibikova <fibikova@exp-math.uni-essen.de> 2/* Includes code written by Lenka Fibikova <fibikova@exp-math.uni-essen.de>
3 * for the OpenSSL project. 3 * for the OpenSSL project.
4 * Includes code written by Bodo Moeller for the OpenSSL project. 4 * Includes code written by Bodo Moeller for the OpenSSL project.
@@ -1608,6 +1608,160 @@ ec_GFp_simple_mul_double_nonct(const EC_GROUP *group, EC_POINT *r,
1608 return ec_wNAF_mul(group, r, g_scalar, 1, &point, &p_scalar, ctx); 1608 return ec_wNAF_mul(group, r, g_scalar, 1, &point, &p_scalar, ctx);
1609} 1609}
1610 1610
1611static void
1612ec_GFp_mont_group_clear(EC_GROUP *group)
1613{
1614 BN_MONT_CTX_free(group->mont_ctx);
1615 group->mont_ctx = NULL;
1616
1617 BN_free(group->mont_one);
1618 group->mont_one = NULL;
1619}
1620
1621static int
1622ec_GFp_mont_group_init(EC_GROUP *group)
1623{
1624 int ok;
1625
1626 ok = ec_GFp_simple_group_init(group);
1627 group->mont_ctx = NULL;
1628 group->mont_one = NULL;
1629 return ok;
1630}
1631
1632static void
1633ec_GFp_mont_group_finish(EC_GROUP *group)
1634{
1635 ec_GFp_mont_group_clear(group);
1636 ec_GFp_simple_group_finish(group);
1637}
1638
1639static int
1640ec_GFp_mont_group_copy(EC_GROUP *dest, const EC_GROUP *src)
1641{
1642 ec_GFp_mont_group_clear(dest);
1643
1644 if (!ec_GFp_simple_group_copy(dest, src))
1645 return 0;
1646
1647 if (src->mont_ctx != NULL) {
1648 dest->mont_ctx = BN_MONT_CTX_new();
1649 if (dest->mont_ctx == NULL)
1650 return 0;
1651 if (!BN_MONT_CTX_copy(dest->mont_ctx, src->mont_ctx))
1652 goto err;
1653 }
1654 if (src->mont_one != NULL) {
1655 dest->mont_one = BN_dup(src->mont_one);
1656 if (dest->mont_one == NULL)
1657 goto err;
1658 }
1659 return 1;
1660
1661 err:
1662 if (dest->mont_ctx != NULL) {
1663 BN_MONT_CTX_free(dest->mont_ctx);
1664 dest->mont_ctx = NULL;
1665 }
1666 return 0;
1667}
1668
1669static int
1670ec_GFp_mont_group_set_curve(EC_GROUP *group, const BIGNUM *p, const BIGNUM *a,
1671 const BIGNUM *b, BN_CTX *ctx)
1672{
1673 BN_MONT_CTX *mont = NULL;
1674 BIGNUM *one = NULL;
1675 int ret = 0;
1676
1677 ec_GFp_mont_group_clear(group);
1678
1679 mont = BN_MONT_CTX_new();
1680 if (mont == NULL)
1681 goto err;
1682 if (!BN_MONT_CTX_set(mont, p, ctx)) {
1683 ECerror(ERR_R_BN_LIB);
1684 goto err;
1685 }
1686 one = BN_new();
1687 if (one == NULL)
1688 goto err;
1689 if (!BN_to_montgomery(one, BN_value_one(), mont, ctx))
1690 goto err;
1691
1692 group->mont_ctx = mont;
1693 mont = NULL;
1694 group->mont_one = one;
1695 one = NULL;
1696
1697 ret = ec_GFp_simple_group_set_curve(group, p, a, b, ctx);
1698 if (!ret)
1699 ec_GFp_mont_group_clear(group);
1700
1701 err:
1702 BN_MONT_CTX_free(mont);
1703 BN_free(one);
1704
1705 return ret;
1706}
1707
1708static int
1709ec_GFp_mont_field_mul(const EC_GROUP *group, BIGNUM *r, const BIGNUM *a,
1710 const BIGNUM *b, BN_CTX *ctx)
1711{
1712 if (group->mont_ctx == NULL) {
1713 ECerror(EC_R_NOT_INITIALIZED);
1714 return 0;
1715 }
1716 return BN_mod_mul_montgomery(r, a, b, group->mont_ctx, ctx);
1717}
1718
1719static int
1720ec_GFp_mont_field_sqr(const EC_GROUP *group, BIGNUM *r, const BIGNUM *a,
1721 BN_CTX *ctx)
1722{
1723 if (group->mont_ctx == NULL) {
1724 ECerror(EC_R_NOT_INITIALIZED);
1725 return 0;
1726 }
1727 return BN_mod_mul_montgomery(r, a, a, group->mont_ctx, ctx);
1728}
1729
1730static int
1731ec_GFp_mont_field_encode(const EC_GROUP *group, BIGNUM *r, const BIGNUM *a,
1732 BN_CTX *ctx)
1733{
1734 if (group->mont_ctx == NULL) {
1735 ECerror(EC_R_NOT_INITIALIZED);
1736 return 0;
1737 }
1738 return BN_to_montgomery(r, a, group->mont_ctx, ctx);
1739}
1740
1741static int
1742ec_GFp_mont_field_decode(const EC_GROUP *group, BIGNUM *r, const BIGNUM *a,
1743 BN_CTX *ctx)
1744{
1745 if (group->mont_ctx == NULL) {
1746 ECerror(EC_R_NOT_INITIALIZED);
1747 return 0;
1748 }
1749 return BN_from_montgomery(r, a, group->mont_ctx, ctx);
1750}
1751
1752static int
1753ec_GFp_mont_field_set_to_one(const EC_GROUP *group, BIGNUM *r, BN_CTX *ctx)
1754{
1755 if (group->mont_one == NULL) {
1756 ECerror(EC_R_NOT_INITIALIZED);
1757 return 0;
1758 }
1759 if (!bn_copy(r, group->mont_one))
1760 return 0;
1761
1762 return 1;
1763}
1764
1611static const EC_METHOD ec_GFp_simple_method = { 1765static const EC_METHOD ec_GFp_simple_method = {
1612 .field_type = NID_X9_62_prime_field, 1766 .field_type = NID_X9_62_prime_field,
1613 .group_init = ec_GFp_simple_group_init, 1767 .group_init = ec_GFp_simple_group_init,
@@ -1654,3 +1808,53 @@ EC_GFp_simple_method(void)
1654 return &ec_GFp_simple_method; 1808 return &ec_GFp_simple_method;
1655} 1809}
1656LCRYPTO_ALIAS(EC_GFp_simple_method); 1810LCRYPTO_ALIAS(EC_GFp_simple_method);
1811
1812static const EC_METHOD ec_GFp_mont_method = {
1813 .field_type = NID_X9_62_prime_field,
1814 .group_init = ec_GFp_mont_group_init,
1815 .group_finish = ec_GFp_mont_group_finish,
1816 .group_copy = ec_GFp_mont_group_copy,
1817 .group_set_curve = ec_GFp_mont_group_set_curve,
1818 .group_get_curve = ec_GFp_simple_group_get_curve,
1819 .group_get_degree = ec_GFp_simple_group_get_degree,
1820 .group_order_bits = ec_group_simple_order_bits,
1821 .group_check_discriminant = ec_GFp_simple_group_check_discriminant,
1822 .point_init = ec_GFp_simple_point_init,
1823 .point_finish = ec_GFp_simple_point_finish,
1824 .point_copy = ec_GFp_simple_point_copy,
1825 .point_set_to_infinity = ec_GFp_simple_point_set_to_infinity,
1826 .point_set_Jprojective_coordinates =
1827 ec_GFp_simple_set_Jprojective_coordinates,
1828 .point_get_Jprojective_coordinates =
1829 ec_GFp_simple_get_Jprojective_coordinates,
1830 .point_set_affine_coordinates =
1831 ec_GFp_simple_point_set_affine_coordinates,
1832 .point_get_affine_coordinates =
1833 ec_GFp_simple_point_get_affine_coordinates,
1834 .point_set_compressed_coordinates =
1835 ec_GFp_simple_set_compressed_coordinates,
1836 .add = ec_GFp_simple_add,
1837 .dbl = ec_GFp_simple_dbl,
1838 .invert = ec_GFp_simple_invert,
1839 .is_at_infinity = ec_GFp_simple_is_at_infinity,
1840 .is_on_curve = ec_GFp_simple_is_on_curve,
1841 .point_cmp = ec_GFp_simple_cmp,
1842 .make_affine = ec_GFp_simple_make_affine,
1843 .points_make_affine = ec_GFp_simple_points_make_affine,
1844 .mul_generator_ct = ec_GFp_simple_mul_generator_ct,
1845 .mul_single_ct = ec_GFp_simple_mul_single_ct,
1846 .mul_double_nonct = ec_GFp_simple_mul_double_nonct,
1847 .field_mul = ec_GFp_mont_field_mul,
1848 .field_sqr = ec_GFp_mont_field_sqr,
1849 .field_encode = ec_GFp_mont_field_encode,
1850 .field_decode = ec_GFp_mont_field_decode,
1851 .field_set_to_one = ec_GFp_mont_field_set_to_one,
1852 .blind_coordinates = ec_GFp_simple_blind_coordinates,
1853};
1854
1855const EC_METHOD *
1856EC_GFp_mont_method(void)
1857{
1858 return &ec_GFp_mont_method;
1859}
1860LCRYPTO_ALIAS(EC_GFp_mont_method);
diff --git a/src/lib/libcrypto/ec/ecp_mont.c b/src/lib/libcrypto/ec/ecp_mont.c
deleted file mode 100644
index 8fd7ebc7b9..0000000000
--- a/src/lib/libcrypto/ec/ecp_mont.c
+++ /dev/null
@@ -1,270 +0,0 @@
1/* $OpenBSD: ecp_mont.c,v 1.31 2024/10/31 15:37:53 tb Exp $ */
2/*
3 * Originally written by Bodo Moeller for the OpenSSL project.
4 */
5/* ====================================================================
6 * Copyright (c) 1998-2001 The OpenSSL Project. All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 *
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 *
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in
17 * the documentation and/or other materials provided with the
18 * distribution.
19 *
20 * 3. All advertising materials mentioning features or use of this
21 * software must display the following acknowledgment:
22 * "This product includes software developed by the OpenSSL Project
23 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
24 *
25 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26 * endorse or promote products derived from this software without
27 * prior written permission. For written permission, please contact
28 * openssl-core@openssl.org.
29 *
30 * 5. Products derived from this software may not be called "OpenSSL"
31 * nor may "OpenSSL" appear in their names without prior written
32 * permission of the OpenSSL Project.
33 *
34 * 6. Redistributions of any form whatsoever must retain the following
35 * acknowledgment:
36 * "This product includes software developed by the OpenSSL Project
37 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
38 *
39 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
43 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50 * OF THE POSSIBILITY OF SUCH DAMAGE.
51 * ====================================================================
52 *
53 * This product includes cryptographic software written by Eric Young
54 * (eay@cryptsoft.com). This product includes software written by Tim
55 * Hudson (tjh@cryptsoft.com).
56 *
57 */
58/* ====================================================================
59 * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
60 * Portions of this software developed by SUN MICROSYSTEMS, INC.,
61 * and contributed to the OpenSSL project.
62 */
63
64#include <openssl/err.h>
65
66#include "ec_local.h"
67
68static void
69ec_GFp_mont_group_clear(EC_GROUP *group)
70{
71 BN_MONT_CTX_free(group->mont_ctx);
72 group->mont_ctx = NULL;
73
74 BN_free(group->mont_one);
75 group->mont_one = NULL;
76}
77
78static int
79ec_GFp_mont_group_init(EC_GROUP *group)
80{
81 int ok;
82
83 ok = ec_GFp_simple_group_init(group);
84 group->mont_ctx = NULL;
85 group->mont_one = NULL;
86 return ok;
87}
88
89static void
90ec_GFp_mont_group_finish(EC_GROUP *group)
91{
92 ec_GFp_mont_group_clear(group);
93 ec_GFp_simple_group_finish(group);
94}
95
96static int
97ec_GFp_mont_group_copy(EC_GROUP *dest, const EC_GROUP *src)
98{
99 ec_GFp_mont_group_clear(dest);
100
101 if (!ec_GFp_simple_group_copy(dest, src))
102 return 0;
103
104 if (src->mont_ctx != NULL) {
105 dest->mont_ctx = BN_MONT_CTX_new();
106 if (dest->mont_ctx == NULL)
107 return 0;
108 if (!BN_MONT_CTX_copy(dest->mont_ctx, src->mont_ctx))
109 goto err;
110 }
111 if (src->mont_one != NULL) {
112 dest->mont_one = BN_dup(src->mont_one);
113 if (dest->mont_one == NULL)
114 goto err;
115 }
116 return 1;
117
118 err:
119 if (dest->mont_ctx != NULL) {
120 BN_MONT_CTX_free(dest->mont_ctx);
121 dest->mont_ctx = NULL;
122 }
123 return 0;
124}
125
126static int
127ec_GFp_mont_group_set_curve(EC_GROUP *group, const BIGNUM *p, const BIGNUM *a,
128 const BIGNUM *b, BN_CTX *ctx)
129{
130 BN_MONT_CTX *mont = NULL;
131 BIGNUM *one = NULL;
132 int ret = 0;
133
134 ec_GFp_mont_group_clear(group);
135
136 mont = BN_MONT_CTX_new();
137 if (mont == NULL)
138 goto err;
139 if (!BN_MONT_CTX_set(mont, p, ctx)) {
140 ECerror(ERR_R_BN_LIB);
141 goto err;
142 }
143 one = BN_new();
144 if (one == NULL)
145 goto err;
146 if (!BN_to_montgomery(one, BN_value_one(), mont, ctx))
147 goto err;
148
149 group->mont_ctx = mont;
150 mont = NULL;
151 group->mont_one = one;
152 one = NULL;
153
154 ret = ec_GFp_simple_group_set_curve(group, p, a, b, ctx);
155 if (!ret)
156 ec_GFp_mont_group_clear(group);
157
158 err:
159 BN_MONT_CTX_free(mont);
160 BN_free(one);
161
162 return ret;
163}
164
165static int
166ec_GFp_mont_field_mul(const EC_GROUP *group, BIGNUM *r, const BIGNUM *a,
167 const BIGNUM *b, BN_CTX *ctx)
168{
169 if (group->mont_ctx == NULL) {
170 ECerror(EC_R_NOT_INITIALIZED);
171 return 0;
172 }
173 return BN_mod_mul_montgomery(r, a, b, group->mont_ctx, ctx);
174}
175
176static int
177ec_GFp_mont_field_sqr(const EC_GROUP *group, BIGNUM *r, const BIGNUM *a,
178 BN_CTX *ctx)
179{
180 if (group->mont_ctx == NULL) {
181 ECerror(EC_R_NOT_INITIALIZED);
182 return 0;
183 }
184 return BN_mod_mul_montgomery(r, a, a, group->mont_ctx, ctx);
185}
186
187static int
188ec_GFp_mont_field_encode(const EC_GROUP *group, BIGNUM *r, const BIGNUM *a,
189 BN_CTX *ctx)
190{
191 if (group->mont_ctx == NULL) {
192 ECerror(EC_R_NOT_INITIALIZED);
193 return 0;
194 }
195 return BN_to_montgomery(r, a, group->mont_ctx, ctx);
196}
197
198static int
199ec_GFp_mont_field_decode(const EC_GROUP *group, BIGNUM *r, const BIGNUM *a,
200 BN_CTX *ctx)
201{
202 if (group->mont_ctx == NULL) {
203 ECerror(EC_R_NOT_INITIALIZED);
204 return 0;
205 }
206 return BN_from_montgomery(r, a, group->mont_ctx, ctx);
207}
208
209static int
210ec_GFp_mont_field_set_to_one(const EC_GROUP *group, BIGNUM *r, BN_CTX *ctx)
211{
212 if (group->mont_one == NULL) {
213 ECerror(EC_R_NOT_INITIALIZED);
214 return 0;
215 }
216 if (!bn_copy(r, group->mont_one))
217 return 0;
218
219 return 1;
220}
221
222static const EC_METHOD ec_GFp_mont_method = {
223 .field_type = NID_X9_62_prime_field,
224 .group_init = ec_GFp_mont_group_init,
225 .group_finish = ec_GFp_mont_group_finish,
226 .group_copy = ec_GFp_mont_group_copy,
227 .group_set_curve = ec_GFp_mont_group_set_curve,
228 .group_get_curve = ec_GFp_simple_group_get_curve,
229 .group_get_degree = ec_GFp_simple_group_get_degree,
230 .group_order_bits = ec_group_simple_order_bits,
231 .group_check_discriminant = ec_GFp_simple_group_check_discriminant,
232 .point_init = ec_GFp_simple_point_init,
233 .point_finish = ec_GFp_simple_point_finish,
234 .point_copy = ec_GFp_simple_point_copy,
235 .point_set_to_infinity = ec_GFp_simple_point_set_to_infinity,
236 .point_set_Jprojective_coordinates =
237 ec_GFp_simple_set_Jprojective_coordinates,
238 .point_get_Jprojective_coordinates =
239 ec_GFp_simple_get_Jprojective_coordinates,
240 .point_set_affine_coordinates =
241 ec_GFp_simple_point_set_affine_coordinates,
242 .point_get_affine_coordinates =
243 ec_GFp_simple_point_get_affine_coordinates,
244 .point_set_compressed_coordinates =
245 ec_GFp_simple_set_compressed_coordinates,
246 .add = ec_GFp_simple_add,
247 .dbl = ec_GFp_simple_dbl,
248 .invert = ec_GFp_simple_invert,
249 .is_at_infinity = ec_GFp_simple_is_at_infinity,
250 .is_on_curve = ec_GFp_simple_is_on_curve,
251 .point_cmp = ec_GFp_simple_cmp,
252 .make_affine = ec_GFp_simple_make_affine,
253 .points_make_affine = ec_GFp_simple_points_make_affine,
254 .mul_generator_ct = ec_GFp_simple_mul_generator_ct,
255 .mul_single_ct = ec_GFp_simple_mul_single_ct,
256 .mul_double_nonct = ec_GFp_simple_mul_double_nonct,
257 .field_mul = ec_GFp_mont_field_mul,
258 .field_sqr = ec_GFp_mont_field_sqr,
259 .field_encode = ec_GFp_mont_field_encode,
260 .field_decode = ec_GFp_mont_field_decode,
261 .field_set_to_one = ec_GFp_mont_field_set_to_one,
262 .blind_coordinates = ec_GFp_simple_blind_coordinates,
263};
264
265const EC_METHOD *
266EC_GFp_mont_method(void)
267{
268 return &ec_GFp_mont_method;
269}
270LCRYPTO_ALIAS(EC_GFp_mont_method);