Annotation of src/usr.bin/ssh/rsa.c, Revision 1.2
1.1 provos 1: /*
2:
3: rsa.c
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:07:06 1995 ylo
11:
12: Description of the RSA algorithm can be found e.g. from the following sources:
13:
14: Bruce Schneier: Applied Cryptography. John Wiley & Sons, 1994.
15:
16: Jennifer Seberry and Josed Pieprzyk: Cryptography: An Introduction to
17: Computer Security. Prentice-Hall, 1989.
18:
19: Man Young Rhee: Cryptography and Secure Data Communications. McGraw-Hill,
20: 1994.
21:
22: R. Rivest, A. Shamir, and L. M. Adleman: Cryptographic Communications
23: System and Method. US Patent 4,405,829, 1983.
24:
25: Hans Riesel: Prime Numbers and Computer Methods for Factorization.
26: Birkhauser, 1994.
27:
28: The RSA Frequently Asked Questions document by RSA Data Security, Inc., 1995.
29:
30: RSA in 3 lines of perl by Adam Back <aba@atlax.ex.ac.uk>, 1995, as included
31: below:
32:
33: gone - had to be deleted - what a pity
34:
35: */
36:
37: #include "includes.h"
1.2 ! deraadt 38: RCSID("$Id: rsa.c,v 1.1 1999/09/28 04:45:37 provos Exp $");
1.1 provos 39:
40: #include "rsa.h"
41: #include "ssh.h"
42: #include "xmalloc.h"
43:
44: int rsa_verbose = 1;
1.2 ! deraadt 45:
! 46: int
! 47: rsa_alive()
! 48: {
! 49: RSA *key;
! 50: extern char *__progname;
! 51:
! 52: key = RSA_generate_key(32, 3, NULL, NULL);
! 53: if (key == NULL)
! 54: return (0);
! 55: RSA_free(key);
! 56: return (1);
! 57: }
1.1 provos 58:
59: /* Generates RSA public and private keys. This initializes the data
60: structures; they should be freed with rsa_clear_private_key and
61: rsa_clear_public_key. */
62:
63: void
64: rsa_generate_key(RSA *prv, RSA *pub, unsigned int bits)
65: {
66: RSA *key;
67:
68: if (rsa_verbose) {
69: printf("Generating RSA keys: ");
70: fflush(stdout);
71: }
72:
73: key = RSA_generate_key(bits, 35, NULL, NULL);
74:
75: assert(key != NULL);
76:
77: /* Copy public key parameters */
78: pub->n = BN_new();
79: BN_copy(pub->n, key->n);
80: pub->e = BN_new();
81: BN_copy(pub->e, key->e);
82:
83: /* Copy private key parameters */
84: prv->n = BN_new();
85: BN_copy(prv->n, key->n);
86: prv->e = BN_new();
87: BN_copy(prv->e, key->e);
88: prv->d = BN_new();
89: BN_copy(prv->d, key->d);
90: prv->p = BN_new();
91: BN_copy(prv->p, key->p);
92: prv->q = BN_new();
93: BN_copy(prv->q, key->q);
94:
95: prv->dmp1 = BN_new();
96: BN_copy(prv->dmp1, key->dmp1);
97:
98: prv->dmq1 = BN_new();
99: BN_copy(prv->dmq1, key->dmq1);
100:
101: prv->iqmp = BN_new();
102: BN_copy(prv->iqmp, key->iqmp);
103:
104: RSA_free(key);
105:
106: if (rsa_verbose)
107: printf("Key generation complete.\n");
108: }
109:
110: void
111: rsa_public_encrypt(BIGNUM *out, BIGNUM *in, RSA* key)
112: {
113: char *inbuf, *outbuf;
114: int len;
115:
116: len = BN_num_bytes(key->n);
117: outbuf = xmalloc(len);
118:
119: len = BN_num_bytes(in);
120: inbuf = xmalloc(len);
121: BN_bn2bin(in, inbuf);
122:
123: if ((len = RSA_public_encrypt(len, inbuf, outbuf, key,
124: RSA_PKCS1_PADDING)) <= 0)
125: fatal("rsa_public_encrypt() failed");
126:
127: BN_bin2bn(outbuf, len, out);
128:
129: xfree(outbuf);
130: xfree(inbuf);
131: }
132:
133: void
134: rsa_private_decrypt(BIGNUM *out, BIGNUM *in, RSA *key)
135: {
136: char *inbuf, *outbuf;
137: int len;
138:
139: len = BN_num_bytes(key->n);
140: outbuf = xmalloc(len);
141:
142: len = BN_num_bytes(in);
143: inbuf = xmalloc(len);
144: BN_bn2bin(in, inbuf);
145:
146: if ((len = RSA_private_decrypt(len, inbuf, outbuf, key,
147: RSA_SSLV23_PADDING)) <= 0)
148: fatal("rsa_private_decrypt() failed");
149:
150: BN_bin2bn(outbuf, len, out);
151:
152: xfree(outbuf);
153: xfree(inbuf);
154: }
155:
156: /* Set whether to output verbose messages during key generation. */
157:
158: void
159: rsa_set_verbose(int verbose)
160: {
161: rsa_verbose = verbose;
162: }