iPXE
elliptic.c
Go to the documentation of this file.
1/*
2 * Copyright (C) 2026 Michael Brown <mbrown@fensystems.co.uk>.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2 of the
7 * License, or any later version.
8 *
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17 * 02110-1301, USA.
18 *
19 * You can also choose to distribute this program under the terms of
20 * the Unmodified Binary Distribution Licence (as given in the file
21 * COPYING.UBDL), provided that you have satisfied its requirements.
22 */
23
24FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
25FILE_SECBOOT ( PERMITTED );
26
27/** @file
28 *
29 * Elliptic curves
30 *
31 */
32
33#include <assert.h>
34#include <errno.h>
35#include <string.h>
36#include <ipxe/crypto.h>
37#include <ipxe/elliptic.h>
38
39/**
40 * Share public key
41 *
42 * @v exchange Key exchange algorithm
43 * @v private Private key
44 * @v public Public key to fill in
45 * @ret rc Return status code
46 */
47int elliptic_share ( struct exchange_algorithm *exchange, const void *private,
48 void *public ) {
49 struct elliptic_curve *curve = exchange->priv;
50 size_t len = exchange->sharedsize;
52 int rc;
53
54 /* Sanity checks */
55 assert ( curve->keysize == exchange->privsize );
56 assert ( curve->pointsize == sizeof ( result->xy ) );
57 assert ( sizeof ( *result ) == exchange->pubsize );
58
59 /* Calculate public key */
61 if ( ( rc = elliptic_multiply ( curve, curve->base, private,
62 &result->xy ) ) != 0 ) {
63 /* Can never fail when using the curve's own base point */
64 assert ( 0 );
65 return rc;
66 }
67
68 return rc;
69}
70
71/**
72 * Agree shared secret
73 *
74 * @v exchange Key exchange algorithm
75 * @v private Private key
76 * @v partner Partner public key
77 * @v shared Shared secret to fill in
78 * @ret rc Return status code
79 */
80int elliptic_agree ( struct exchange_algorithm *exchange, const void *private,
81 const void *partner, void *shared ) {
82 struct elliptic_curve *curve = exchange->priv;
83 size_t len = exchange->sharedsize;
86 int rc;
87
88 /* Sanity checks */
89 assert ( curve->keysize == exchange->privsize );
90 assert ( curve->pointsize == sizeof ( base->xy ) );
91 assert ( curve->pointsize == sizeof ( result.xy ) );
92 assert ( sizeof ( *base ) == exchange->pubsize );
93 assert ( sizeof ( result.x ) == exchange->sharedsize );
94
95 /* Calculate shared secret */
96 if ( ( rc = elliptic_multiply ( curve, &base->xy, private,
97 result.xy ) ) != 0 )
98 return rc;
99 memcpy ( shared, &result.x, sizeof ( result.x ) );
100
101 /* Check for point at infinity */
102 if ( elliptic_is_infinity ( curve, &result.xy ) )
103 return -EPERM;
104
105 /* Check format byte */
106 if ( base->format != ELLIPTIC_FORMAT_UNCOMPRESSED )
107 return -EPERM;
108
109 return 0;
110}
struct arbelprm_rc_send_wqe rc
Definition arbel.h:3
uint16_t result
Definition hyperv.h:33
Assertions.
#define assert(condition)
Assert a condition at run-time.
Definition assert.h:50
ring len
Length.
Definition dwmac.h:226
int elliptic_agree(struct exchange_algorithm *exchange, const void *private, const void *partner, void *shared)
Agree shared secret.
Definition elliptic.c:80
int elliptic_share(struct exchange_algorithm *exchange, const void *private, void *public)
Share public key.
Definition elliptic.c:47
Elliptic curves.
#define elliptic_uncompressed_t(len)
An uncompressed elliptic curve point.
Definition elliptic.h:17
#define ELLIPTIC_FORMAT_UNCOMPRESSED
Format byte for uncompressed curve point representation.
Definition elliptic.h:30
Error codes.
struct eth_slow_lacp_entity_tlv partner
Partner information.
Definition eth_slow.h:5
#define FILE_LICENCE(_licence)
Declare a particular licence as applying to a file.
Definition compiler.h:921
#define EPERM
Operation not permitted.
Definition errno.h:615
#define FILE_SECBOOT(_status)
Declare a file's UEFI Secure Boot permission status.
Definition compiler.h:951
Cryptographic API.
static int elliptic_multiply(struct elliptic_curve *curve, const void *base, const void *scalar, void *result)
Definition crypto.h:414
static int elliptic_is_infinity(struct elliptic_curve *curve, const void *point)
Definition crypto.h:409
String functions.
void * memcpy(void *dest, const void *src, size_t len) __nonnull
uint32_t base
Base.
Definition librm.h:3
An elliptic curve.
Definition crypto.h:246
size_t keysize
Scalar (and private key) size.
Definition crypto.h:252
size_t pointsize
Point (and public key) size.
Definition crypto.h:250
const void * base
Generator base point.
Definition crypto.h:254
A key exchange algorithm.
Definition crypto.h:210
size_t sharedsize
Shared secret size.
Definition crypto.h:218
size_t privsize
Private key size.
Definition crypto.h:214
size_t pubsize
Public key size.
Definition crypto.h:216
void * priv
Algorithm private data.
Definition crypto.h:242