-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrsa_encode_gmp.c
48 lines (37 loc) · 1.06 KB
/
rsa_encode_gmp.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
/*
* $Id: rsa_encode_gmp.c,v 1.1 2000/03/14 00:28:47 ahn Exp $
*
* This is the server-side RSA support code that uses the GNU MP
* library. Berkeley MP is no longer supported.
*
* Originally written by Ray Jones. Updated by Dave Ahn.
* */
#include <gmp.h>
#include "rsa.h"
#include "config.h"
#include "rsa_gmp.h"
/* RSA calculation using the message, key, global N modulus */
void rsa_encode(unsigned char *out, unsigned char *message,
unsigned char *key, unsigned char *global, const int digits) {
mpz_t result;
mpz_t n_message;
mpz_t n_key;
mpz_t n_global;
mpz_init(result);
mpz_init(n_message);
mpz_init(n_key);
mpz_init(n_global);
mpz_set_ui(result, 0);
mpz_set_ui(n_message, 0);
mpz_set_ui(n_key, 0);
mpz_set_ui(n_global, 0);
raw_to_num(n_global, global, digits);
raw_to_num(n_message, message, digits);
raw_to_num(n_key, key, digits);
mpz_powm(result, n_message, n_key, n_global);
num_to_raw(out, result, digits);
mpz_clear(result);
mpz_clear(n_message);
mpz_clear(n_key);
mpz_clear(n_global);
}