* @v shared Shared result to fill in
* @ret rc Return status code
*/
-int ffdhe ( struct ffdhe_group *group, const void *public, const void *private,
- void *shared ) {
+static int ffdhe ( struct ffdhe_group *group, const void *public,
+ const void *private, void *shared ) {
unsigned int expsize = group->expsize;
unsigned int size = group->size;
size_t explen = group->explen;
return 0;
}
+/**
+ * Calculate public key
+ *
+ * @v exchange Key exchange algorithm
+ * @v private Private key
+ * @v public Public key to fill in
+ */
+void ffdhe_public ( struct exchange_algorithm *exchange, const void *private,
+ void *public ) {
+ struct ffdhe_group *group = exchange->priv;
+
+ ffdhe ( group, NULL, private, public );
+}
+
+/**
+ * Calculate shared secret
+ *
+ * @v exchange Key exchange algorithm
+ * @v private Private key
+ * @v partner Partner public key
+ * @v shared Shared secret to fill in
+ * @ret rc Return status code
+ */
+int ffdhe_shared ( struct exchange_algorithm *exchange, const void *private,
+ const void *partner, void *shared ) {
+ struct ffdhe_group *group = exchange->priv;
+
+ return ffdhe ( group, partner, private, shared );
+}
+
/* Supported groups */
FFDHE_GROUP ( ffdhe2048, ffdhe2048_algorithm, euler, 2048, 225, 0x61285c97 );
FFDHE_GROUP ( ffdhe3072, ffdhe3072_algorithm, euler, 3072, 275, 0x66c62e37 );
/**
* Calculate public key
*
- * @v curve Weierstrass curve
+ * @v exchange Key exchange algorithm
* @v private Private key
* @v public Public key to fill in
*/
-void weierstrass_public ( struct weierstrass_curve *curve, const void *private,
- void *public ) {
+void weierstrass_public ( struct exchange_algorithm *exchange,
+ const void *private, void *public ) {
+ struct weierstrass_curve *curve = exchange->priv;
size_t len = curve->len;
weierstrass_uncompressed_t ( len ) *uncompressed = public;
int rc;
/**
* Calculate shared secret
*
- * @v curve Weierstrass curve
+ * @v exchange Key exchange algorithm
* @v private Private key
* @v partner Partner public key
* @v shared Shared secret to fill in
* @ret rc Return status code
*/
-int weierstrass_shared ( struct weierstrass_curve *curve, const void *private,
- const void *partner, void *shared ) {
+int weierstrass_shared ( struct exchange_algorithm *exchange,
+ const void *private, const void *partner,
+ void *shared ) {
+ struct weierstrass_curve *curve = exchange->priv;
size_t len = curve->len;
const weierstrass_uncompressed_t ( len ) *uncompressed = partner;
weierstrass_raw_t ( len ) point;
/**
* Calculate public key
*
+ * @v exchange Key exchange algorithm
* @v private Private key
* @v public Public key to fill in
*/
-static void x25519_public ( const void *private, void *public ) {
+static void x25519_public ( struct exchange_algorithm *exchange __unused,
+ const void *private, void *public ) {
/* Calculate public key */
x25519_key ( &x25519_generator, private, public );
/**
* Calculate shared secret
*
+ * @v exchange Key exchange algorithm
* @v private Private key
* @v partner Partner public key
* @v shared Shared secret to fill in
* @ret rc Return status code
*/
-static int x25519_shared ( const void *private, const void *partner,
+static int x25519_shared ( struct exchange_algorithm *exchange __unused,
+ const void *private, const void *partner,
void *shared ) {
/* Calculate shared secret */
/**
* Calculate public key
*
+ * @v exchange Key exchange algorithm
* @v private Private key
* @v public Public key to fill in
*/
- void ( * public ) ( const void *private, void *public );
+ void ( * public ) ( struct exchange_algorithm *exchange,
+ const void *private, void *public );
/**
* Calculate shared secret
*
+ * @v exchange Key exchange algorithm
* @v private Private key
* @v partner Partner public key
* @v shared Shared secret to fill in
* @ret rc Return status code
*/
- int ( * shared ) ( const void *private, const void *partner,
+ int ( * shared ) ( struct exchange_algorithm *exchange,
+ const void *private, const void *partner,
void *shared );
+ /** Algorithm private data */
+ void *priv;
};
/** An elliptic curve */
static inline __attribute__ (( always_inline )) void
exchange_public ( struct exchange_algorithm *exchange, const void *private,
void *public ) {
- exchange->public ( private, public );
+ exchange->public ( exchange, private, public );
}
static inline __attribute__ (( always_inline )) int
exchange_shared ( struct exchange_algorithm *exchange, const void *private,
const void *partner, void *shared ) {
- return exchange->shared ( private, partner, shared );
+ return exchange->shared ( exchange, private, partner, shared );
}
static inline __attribute__ (( always_inline )) int
uint32_t lsb32;
};
-extern int ffdhe ( struct ffdhe_group *group, const void *public,
- const void *private, void *shared );
+extern void ffdhe_public ( struct exchange_algorithm *exchange,
+ const void *private, void *public );
+extern int ffdhe_shared ( struct exchange_algorithm *exchange,
+ const void *private, const void *partner,
+ void *shared );
/** Define a finite field DHE group */
#define FFDHE_GROUP( _name, _exchange, _constant, _bits, _expbits, _lsb ) \
.expsize = bigint_required_size ( ( _expbits + 7 ) / 8 ), \
.lsb32 = cpu_to_be32 ( _lsb ), \
}; \
- static void _name ## _public ( const void *private, \
- void *public ) { \
- ffdhe ( &_name ## _group, NULL, private, public ); \
- } \
- static int _name ## _shared ( const void *private, \
- const void *partner, \
- void *shared ) { \
- return ffdhe ( &_name ## _group, partner, private, \
- shared ); \
- } \
struct exchange_algorithm _exchange = { \
.name = #_name, \
.privsize = ( ( _expbits + 7 ) / 8 ), \
.pubsize = ( _bits / 8 ), \
.sharedsize = ( _bits / 8 ), \
- .public = _name ## _public, \
- .shared = _name ## _shared, \
+ .public = ffdhe_public, \
+ .shared = ffdhe_shared, \
+ .priv = &_name ## _group, \
}
extern struct exchange_algorithm ffdhe2048_algorithm;
extern int weierstrass_add_once ( struct weierstrass_curve *curve,
const void *addend, const void *augend,
void *result );
-extern void weierstrass_public ( struct weierstrass_curve *curve,
+extern void weierstrass_public ( struct exchange_algorithm *exchange,
const void *private, void *public );
-extern int weierstrass_shared ( struct weierstrass_curve *curve,
+extern int weierstrass_shared ( struct exchange_algorithm *exchange,
const void *private, const void *partner,
void *shared );
return weierstrass_add_once ( &_name ## _weierstrass, \
addend, augend, result ); \
} \
- static void _name ## _public ( const void *private, \
- void *public ) { \
- weierstrass_public ( &_name ## _weierstrass, \
- private, public ); \
- } \
- static int _name ## _shared ( const void *private, \
- const void *partner, \
- void *shared ) { \
- return weierstrass_shared ( &_name ## _weierstrass, \
- private, partner, shared ); \
- } \
struct elliptic_curve _curve = { \
.name = #_name, \
.pointsize = sizeof ( weierstrass_raw_t(_len) ), \
.privsize = (_len), \
.pubsize = sizeof ( weierstrass_uncompressed_t(_len) ), \
.sharedsize = (_len), \
- .public = _name ## _public, \
- .shared = _name ## _shared, \
+ .public = weierstrass_public, \
+ .shared = weierstrass_shared, \
+ .priv = &_name ## _weierstrass, \
}
#endif /* _IPXE_WEIERSTRASS_H */