forked from rebolsource/r3
-
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
FIX: Diffie-Hellman algorithm (missing files)
- Loading branch information
Showing
7 changed files
with
359 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
/* | ||
Simple implementation of Diffie-Hellman algorithm (c) 2013 Richard Smolak | ||
The code uses Bigint implementation Copyright (c) 2007, Cameron Rich | ||
*/ | ||
|
||
#include "sys-dh.h" | ||
|
||
void DH_generate_key(DH_CTX *dh_ctx) | ||
{ | ||
BI_CTX *bi_ctx = bi_initialize(); | ||
int len = dh_ctx->len; | ||
bigint *p = bi_import(bi_ctx, dh_ctx->p, len); //p modulus | ||
bigint *g = bi_import(bi_ctx, dh_ctx->g, dh_ctx->len_g); //generator | ||
bigint *x, *gx; | ||
|
||
bi_permanent(g); | ||
|
||
//generate private key X | ||
get_random_NZ(len, dh_ctx->x); | ||
x = bi_import(bi_ctx, dh_ctx->x, len); | ||
bi_permanent(x); | ||
|
||
//calculate public key gx = g^x mod p | ||
bi_set_mod(bi_ctx, p, BIGINT_M_OFFSET); | ||
bi_ctx->mod_offset = BIGINT_M_OFFSET; | ||
gx = bi_mod_power(bi_ctx, g, x); | ||
bi_permanent(gx); | ||
|
||
bi_export(bi_ctx, x, dh_ctx->x, len); | ||
bi_export(bi_ctx, gx, dh_ctx->gx, len); | ||
|
||
bi_depermanent(g); | ||
bi_depermanent(x); | ||
bi_depermanent(gx); | ||
bi_free(bi_ctx, g); | ||
bi_free(bi_ctx, x); | ||
bi_free(bi_ctx, gx); | ||
|
||
bi_free_mod(bi_ctx, BIGINT_M_OFFSET); | ||
bi_terminate(bi_ctx); | ||
} | ||
|
||
void DH_compute_key(DH_CTX *dh_ctx) | ||
{ | ||
BI_CTX *bi_ctx = bi_initialize(); | ||
int len = dh_ctx->len; | ||
bigint *p = bi_import(bi_ctx, dh_ctx->p, len); //p modulus | ||
bigint *x = bi_import(bi_ctx, dh_ctx->x, len); //private key | ||
bigint *gy = bi_import(bi_ctx, dh_ctx->gy, len); //public key(peer) | ||
bigint *k; //negotiated(session) key | ||
|
||
bi_permanent(x); | ||
bi_permanent(gy); | ||
|
||
//calculate session key k = gy^x mod p | ||
bi_set_mod(bi_ctx, p, BIGINT_M_OFFSET); | ||
bi_ctx->mod_offset = BIGINT_M_OFFSET; | ||
k = bi_mod_power(bi_ctx, gy, x); | ||
bi_permanent(k); | ||
|
||
bi_export(bi_ctx, k, dh_ctx->k, len); | ||
|
||
bi_depermanent(x); | ||
bi_depermanent(gy); | ||
bi_depermanent(k); | ||
bi_free(bi_ctx, x); | ||
bi_free(bi_ctx, gy); | ||
bi_free(bi_ctx, k); | ||
|
||
bi_free_mod(bi_ctx, BIGINT_M_OFFSET); | ||
bi_terminate(bi_ctx); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/* | ||
Simple implementation of Diffie-Hellman algorithm (c) 2013 Richard Smolak | ||
The code uses Bigint implementation Copyright (c) 2007, Cameron Rich | ||
*/ | ||
|
||
#include <stdint.h> | ||
#include "bigint.h" | ||
|
||
typedef struct | ||
{ | ||
int len; //length of modulus and keys in bytes | ||
int len_g; //length of generator in bytes | ||
int len_data; //length of allocated following data blob | ||
union { | ||
uint8_t *data; // head of following keys | ||
uint8_t *g; // generator | ||
}; | ||
uint8_t *p; // prime modulus | ||
uint8_t *x; // private key | ||
uint8_t *gx; // public key(self) | ||
uint8_t *gy; // public key(peer) | ||
uint8_t *k; // negotiated key | ||
} | ||
DH_CTX; | ||
|
||
|
||
void DH_generate_key( | ||
DH_CTX *dh_ctx | ||
); | ||
|
||
void DH_compute_key( | ||
DH_CTX *dh_ctx | ||
); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -53,5 +53,5 @@ REBOL [ | |
;-- protocols: | ||
[ | ||
%prot-http.r | ||
%prot-tls.r | ||
;%prot-tls.r | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.