Annotation of src/usr.bin/ssh/rsa.c, Revision 1.15
1.1 provos 1: /*
1.14 markus 2: *
1.10 deraadt 3: * rsa.c
1.14 markus 4: *
1.10 deraadt 5: * Author: Tatu Ylonen <ylo@cs.hut.fi>
1.14 markus 6: *
1.10 deraadt 7: * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
8: * All rights reserved
1.14 markus 9: *
1.10 deraadt 10: * Created: Fri Mar 3 22:07:06 1995 ylo
1.14 markus 11: *
1.10 deraadt 12: * Description of the RSA algorithm can be found e.g. from the following sources:
1.14 markus 13: *
1.10 deraadt 14: * Bruce Schneier: Applied Cryptography. John Wiley & Sons, 1994.
1.14 markus 15: *
1.10 deraadt 16: * Jennifer Seberry and Josed Pieprzyk: Cryptography: An Introduction to
17: * Computer Security. Prentice-Hall, 1989.
1.14 markus 18: *
1.10 deraadt 19: * Man Young Rhee: Cryptography and Secure Data Communications. McGraw-Hill,
20: * 1994.
1.14 markus 21: *
1.10 deraadt 22: * R. Rivest, A. Shamir, and L. M. Adleman: Cryptographic Communications
23: * System and Method. US Patent 4,405,829, 1983.
1.14 markus 24: *
1.10 deraadt 25: * Hans Riesel: Prime Numbers and Computer Methods for Factorization.
26: * Birkhauser, 1994.
1.14 markus 27: *
1.10 deraadt 28: * The RSA Frequently Asked Questions document by RSA Data Security, Inc., 1995.
1.14 markus 29: *
1.10 deraadt 30: * RSA in 3 lines of perl by Adam Back <aba@atlax.ex.ac.uk>, 1995, as included
31: * below:
1.14 markus 32: *
1.10 deraadt 33: * [gone - had to be deleted - what a pity]
1.14 markus 34: *
1.1 provos 35: */
36:
37: #include "includes.h"
1.15 ! markus 38: RCSID("$OpenBSD: rsa.c,v 1.14 2000/04/14 10:30:32 markus 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: {
1.9 markus 49: RSA *key;
1.2 deraadt 50:
1.9 markus 51: key = RSA_generate_key(32, 3, NULL, NULL);
52: if (key == NULL)
53: return (0);
54: RSA_free(key);
55: return (1);
1.2 deraadt 56: }
1.1 provos 57:
1.11 markus 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: */
1.1 provos 63:
64: void
65: rsa_generate_key(RSA *prv, RSA *pub, unsigned int bits)
66: {
1.9 markus 67: RSA *key;
1.1 provos 68:
1.9 markus 69: if (rsa_verbose) {
70: printf("Generating RSA keys: ");
71: fflush(stdout);
72: }
73: key = RSA_generate_key(bits, 35, NULL, NULL);
74: if (key == NULL)
75: fatal("rsa_generate_key: key generation failed.");
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");
1.1 provos 108: }
109:
110: void
1.9 markus 111: rsa_public_encrypt(BIGNUM *out, BIGNUM *in, RSA *key)
1.1 provos 112: {
1.13 markus 113: unsigned char *inbuf, *outbuf;
1.9 markus 114: int len, ilen, olen;
1.4 provos 115:
1.9 markus 116: if (BN_num_bits(key->e) < 2 || !BN_is_odd(key->e))
117: fatal("rsa_public_encrypt() exponent too small or not odd");
1.1 provos 118:
1.9 markus 119: olen = BN_num_bytes(key->n);
120: outbuf = xmalloc(olen);
1.1 provos 121:
1.9 markus 122: ilen = BN_num_bytes(in);
123: inbuf = xmalloc(ilen);
124: BN_bn2bin(in, inbuf);
1.1 provos 125:
1.9 markus 126: if ((len = RSA_public_encrypt(ilen, inbuf, outbuf, key,
1.12 markus 127: RSA_PKCS1_PADDING)) <= 0)
1.9 markus 128: fatal("rsa_public_encrypt() failed");
1.1 provos 129:
1.9 markus 130: BN_bin2bn(outbuf, len, out);
1.1 provos 131:
1.9 markus 132: memset(outbuf, 0, olen);
133: memset(inbuf, 0, ilen);
134: xfree(outbuf);
135: xfree(inbuf);
1.1 provos 136: }
137:
138: void
139: rsa_private_decrypt(BIGNUM *out, BIGNUM *in, RSA *key)
140: {
1.13 markus 141: unsigned char *inbuf, *outbuf;
1.9 markus 142: int len, ilen, olen;
1.1 provos 143:
1.9 markus 144: olen = BN_num_bytes(key->n);
145: outbuf = xmalloc(olen);
1.1 provos 146:
1.9 markus 147: ilen = BN_num_bytes(in);
148: inbuf = xmalloc(ilen);
149: BN_bn2bin(in, inbuf);
150:
151: if ((len = RSA_private_decrypt(ilen, inbuf, outbuf, key,
1.12 markus 152: RSA_PKCS1_PADDING)) <= 0)
1.9 markus 153: fatal("rsa_private_decrypt() failed");
154:
155: BN_bin2bn(outbuf, len, out);
156:
157: memset(outbuf, 0, olen);
158: memset(inbuf, 0, ilen);
159: xfree(outbuf);
160: xfree(inbuf);
1.1 provos 161: }
162:
163: /* Set whether to output verbose messages during key generation. */
164:
165: void
166: rsa_set_verbose(int verbose)
167: {
1.9 markus 168: rsa_verbose = verbose;
1.1 provos 169: }