1 | /* |
---|
2 | |
---|
3 | rsa.h |
---|
4 | |
---|
5 | Author: Tatu Ylonen <ylo@cs.hut.fi> |
---|
6 | |
---|
7 | Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland |
---|
8 | All rights reserved |
---|
9 | |
---|
10 | Created: Fri Mar 3 22:01:06 1995 ylo |
---|
11 | |
---|
12 | RSA key generation, encryption and decryption. |
---|
13 | |
---|
14 | */ |
---|
15 | |
---|
16 | /* |
---|
17 | * $Id: rsa.h,v 1.1.1.2 1999-03-08 17:43:40 danw Exp $ |
---|
18 | * $Log: not supported by cvs2svn $ |
---|
19 | * Revision 1.3 1997/03/26 07:11:51 kivinen |
---|
20 | * Fixed prototypes. |
---|
21 | * |
---|
22 | * Revision 1.2 1996/02/19 16:09:38 huima |
---|
23 | * Comments fixed. |
---|
24 | * |
---|
25 | * Revision 1.1.1.1 1996/02/18 21:38:10 ylo |
---|
26 | * Imported ssh-1.2.13. |
---|
27 | * |
---|
28 | * Revision 1.3 1995/07/13 01:33:11 ylo |
---|
29 | * Fixed comments and label used to protect again multiple inclusion. |
---|
30 | * |
---|
31 | * Revision 1.2 1995/07/13 01:31:43 ylo |
---|
32 | * Removed "Last modified" header. |
---|
33 | * Added cvs log. |
---|
34 | * |
---|
35 | * $Endlog$ |
---|
36 | */ |
---|
37 | |
---|
38 | #ifndef RSA_H |
---|
39 | #define RSA_H |
---|
40 | |
---|
41 | #include "gmp.h" |
---|
42 | #include "randoms.h" |
---|
43 | |
---|
44 | typedef struct |
---|
45 | { |
---|
46 | unsigned int bits; /* Modulus size in bits. */ |
---|
47 | MP_INT e; /* Public exponent. */ |
---|
48 | MP_INT n; /* Modulus. */ |
---|
49 | } RSAPublicKey; |
---|
50 | |
---|
51 | typedef struct |
---|
52 | { |
---|
53 | unsigned int bits; /* Modulus size in bits. */ |
---|
54 | MP_INT n; /* Modulus. */ |
---|
55 | MP_INT e; /* Public exponent. */ |
---|
56 | MP_INT d; /* Private exponent. */ |
---|
57 | MP_INT u; /* Multiplicative inverse of p mod q. */ |
---|
58 | MP_INT p; /* Prime number p. */ |
---|
59 | MP_INT q; /* Prime number q. */ |
---|
60 | } RSAPrivateKey; |
---|
61 | |
---|
62 | /* Generates a random integer of the desired number of bits. */ |
---|
63 | void rsa_random_integer(MP_INT *ret, RandomState *state, unsigned int bits); |
---|
64 | |
---|
65 | /* Makes and returns a random prime of the desired number of bits. |
---|
66 | Note that the random number generator must be initialized properly |
---|
67 | before using this. |
---|
68 | |
---|
69 | The generated prime will have the highest bit set, and will have |
---|
70 | the two lowest bits set. */ |
---|
71 | void rsa_random_prime(MP_INT *ret, RandomState *state, unsigned int bits); |
---|
72 | |
---|
73 | /* Generates RSA public and private keys. This initializes the data |
---|
74 | structures; they should be freed with rsa_clear_private_key and |
---|
75 | rsa_clear_public_key. */ |
---|
76 | void rsa_generate_key(RSAPrivateKey *prv, RSAPublicKey *pub, |
---|
77 | RandomState *state, unsigned int bits); |
---|
78 | |
---|
79 | /* Frees any memory associated with the private key. */ |
---|
80 | void rsa_clear_private_key(RSAPrivateKey *prv); |
---|
81 | |
---|
82 | /* Frees any memory associated with the public key. */ |
---|
83 | void rsa_clear_public_key(RSAPublicKey *pub); |
---|
84 | |
---|
85 | /* Performs a private-key RSA operation (encrypt/decrypt). */ |
---|
86 | void rsa_private(MP_INT *output, MP_INT *input, RSAPrivateKey *prv); |
---|
87 | |
---|
88 | /* Performs a public-key RSA operation (encrypt/decrypt). */ |
---|
89 | void rsa_public(MP_INT *output, MP_INT *input, RSAPublicKey *pub); |
---|
90 | |
---|
91 | /* Sets MP_INT memory allocation routines to ones that clear any memory |
---|
92 | when freed. */ |
---|
93 | void rsa_set_mp_memory_allocation(void); |
---|
94 | |
---|
95 | /* Indicates whether the rsa module is permitted to show messages on |
---|
96 | the terminal. */ |
---|
97 | void rsa_set_verbose(int verbose); |
---|
98 | |
---|
99 | /************* Kludge functions for RSAREF compatibility *******************/ |
---|
100 | |
---|
101 | /* These functions are a kludge but can be implemented using rsaref. */ |
---|
102 | |
---|
103 | /* It is not assumed that output != input. */ |
---|
104 | |
---|
105 | /* Encrypt input using the public key. Input should be a 256 bit value. */ |
---|
106 | void rsa_public_encrypt(MP_INT *output, MP_INT *input, RSAPublicKey *key, |
---|
107 | RandomState *state); |
---|
108 | |
---|
109 | /* Performs a private key decrypt operation. */ |
---|
110 | void rsa_private_decrypt(MP_INT *output, MP_INT *input, RSAPrivateKey *key); |
---|
111 | |
---|
112 | #endif /* RSA_H */ |
---|