iPXE
dhe.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2022 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 
24 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
25 
26 /** @file
27  *
28  * Ephemeral Diffie-Hellman key exchange
29  *
30  */
31 
32 #include <stdint.h>
33 #include <stdlib.h>
34 #include <errno.h>
35 #include <ipxe/bigint.h>
36 #include <ipxe/dhe.h>
37 
38 /**
39  * Calculate Diffie-Hellman key
40  *
41  * @v modulus Prime modulus
42  * @v len Length of prime modulus
43  * @v generator Generator
44  * @v generator_len Length of generator
45  * @v partner Partner public key
46  * @v partner_len Length of partner public key
47  * @v private Private key
48  * @v private_len Length of private key
49  * @ret public Public key (length equal to prime modulus)
50  * @ret shared Shared secret (length equal to prime modulus)
51  * @ret rc Return status code
52  */
53 int dhe_key ( const void *modulus, size_t len, const void *generator,
54  size_t generator_len, const void *partner, size_t partner_len,
55  const void *private, size_t private_len, void *public,
56  void *shared ) {
57  unsigned int size = bigint_required_size ( len );
58  unsigned int private_size = bigint_required_size ( private_len );
59  bigint_t ( size ) *mod;
60  size_t tmp_len = bigint_mod_exp_tmp_len ( mod );
61  struct {
62  bigint_t ( size ) modulus;
63  bigint_t ( size ) generator;
64  bigint_t ( size ) partner;
65  bigint_t ( private_size ) private;
66  bigint_t ( size ) result;
67  uint8_t tmp[tmp_len];
68  } __attribute__ (( packed )) *ctx;
69  int rc;
70 
71  DBGC2 ( modulus, "DHE %p modulus:\n", modulus );
72  DBGC2_HDA ( modulus, 0, modulus, len );
73  DBGC2 ( modulus, "DHE %p generator:\n", modulus );
74  DBGC2_HDA ( modulus, 0, generator, generator_len );
75  DBGC2 ( modulus, "DHE %p partner public key:\n", modulus );
76  DBGC2_HDA ( modulus, 0, partner, partner_len );
77  DBGC2 ( modulus, "DHE %p private key:\n", modulus );
78  DBGC2_HDA ( modulus, 0, private, private_len );
79 
80  /* Sanity checks */
81  if ( generator_len > len ) {
82  DBGC ( modulus, "DHE %p overlength generator\n", modulus );
83  rc = -EINVAL;
84  goto err_sanity;
85  }
86  if ( partner_len > len ) {
87  DBGC ( modulus, "DHE %p overlength partner public key\n",
88  modulus );
89  rc = -EINVAL;
90  goto err_sanity;
91  }
92  if ( private_len > len ) {
93  DBGC ( modulus, "DHE %p overlength private key\n", modulus );
94  rc = -EINVAL;
95  goto err_sanity;
96  }
97 
98  /* Allocate context */
99  ctx = malloc ( sizeof ( *ctx ) );
100  if ( ! ctx ) {
101  rc = -ENOMEM;
102  goto err_alloc;
103  }
104 
105  /* Initialise context */
106  bigint_init ( &ctx->modulus, modulus, len );
107  bigint_init ( &ctx->generator, generator, generator_len );
108  bigint_init ( &ctx->partner, partner, partner_len );
109  bigint_init ( &ctx->private, private, private_len );
110 
111  /* Calculate public key */
112  bigint_mod_exp ( &ctx->generator, &ctx->modulus, &ctx->private,
113  &ctx->result, ctx->tmp );
114  bigint_done ( &ctx->result, public, len );
115  DBGC2 ( modulus, "DHE %p public key:\n", modulus );
116  DBGC2_HDA ( modulus, 0, public, len );
117 
118  /* Calculate shared secret */
119  bigint_mod_exp ( &ctx->partner, &ctx->modulus, &ctx->private,
120  &ctx->result, ctx->tmp );
121  bigint_done ( &ctx->result, shared, len );
122  DBGC2 ( modulus, "DHE %p shared secret:\n", modulus );
123  DBGC2_HDA ( modulus, 0, shared, len );
124 
125  /* Success */
126  rc = 0;
127 
128  free ( ctx );
129  err_alloc:
130  err_sanity:
131  return rc;
132 }
#define __attribute__(x)
Definition: compiler.h:10
#define EINVAL
Invalid argument.
Definition: errno.h:428
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
#define bigint_mod_exp(base, modulus, exponent, result, tmp)
Perform modular exponentiation of big integers.
Definition: bigint.h:346
int dhe_key(const void *modulus, size_t len, const void *generator, size_t generator_len, const void *partner, size_t partner_len, const void *private, size_t private_len, void *public, void *shared)
Calculate Diffie-Hellman key.
Definition: dhe.c:53
Error codes.
uint8_t size
Entry size (in 32-bit words)
Definition: ena.h:16
#define DBGC(...)
Definition: compiler.h:505
#define bigint_init(value, data, len)
Initialise big integer.
Definition: bigint.h:61
struct golan_eq_context ctx
Definition: CIB_PRM.h:28
Big integer support.
unsigned long tmp
Definition: linux_pci.h:63
#define ENOMEM
Not enough space.
Definition: errno.h:534
#define bigint_done(value, out, len)
Finalise big integer.
Definition: bigint.h:74
#define DBGC2_HDA(...)
Definition: compiler.h:523
static void(* free)(struct refcnt *refcnt))
Definition: refcnt.h:54
#define bigint_required_size(len)
Determine number of elements required for a big-integer type.
Definition: bigint.h:30
unsigned char uint8_t
Definition: stdint.h:10
void * malloc(size_t size)
Allocate memory.
Definition: malloc.c:599
uint16_t result
Definition: hyperv.h:33
#define DBGC2(...)
Definition: compiler.h:522
Ephemeral Diffie-Hellman key exchange.
#define bigint_mod_exp_tmp_len(modulus)
Calculate temporary working space required for moduluar exponentiation.
Definition: bigint.h:360
uint32_t len
Length.
Definition: ena.h:14
FILE_LICENCE(GPL2_OR_LATER_OR_UBDL)
struct eth_slow_lacp_entity_tlv partner
Partner information.
Definition: eth_slow.h:16
typedef bigint_t(X25519_SIZE) x25519_t
An X25519 unsigned big integer used in internal calculations.