[BACK]Return to rsa.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / ssh

Annotation of src/usr.bin/ssh/rsa.c, Revision 1.1

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"
        !            38: RCSID("$Id: rsa.c,v 1.2 1999/05/04 11:59:05 bg Exp $");
        !            39:
        !            40: #include "rsa.h"
        !            41: #include "ssh.h"
        !            42: #include "xmalloc.h"
        !            43:
        !            44: int rsa_verbose = 1;
        !            45:
        !            46: /* Generates RSA public and private keys.  This initializes the data
        !            47:    structures; they should be freed with rsa_clear_private_key and
        !            48:    rsa_clear_public_key. */
        !            49:
        !            50: void
        !            51: rsa_generate_key(RSA *prv, RSA *pub, unsigned int bits)
        !            52: {
        !            53:   RSA *key;
        !            54:
        !            55:   if (rsa_verbose) {
        !            56:     printf("Generating RSA keys:  ");
        !            57:     fflush(stdout);
        !            58:   }
        !            59:
        !            60:   key = RSA_generate_key(bits, 35, NULL, NULL);
        !            61:
        !            62:   assert(key != NULL);
        !            63:
        !            64:   /* Copy public key parameters */
        !            65:   pub->n = BN_new();
        !            66:   BN_copy(pub->n, key->n);
        !            67:   pub->e = BN_new();
        !            68:   BN_copy(pub->e, key->e);
        !            69:
        !            70:   /* Copy private key parameters */
        !            71:   prv->n = BN_new();
        !            72:   BN_copy(prv->n, key->n);
        !            73:   prv->e = BN_new();
        !            74:   BN_copy(prv->e, key->e);
        !            75:   prv->d = BN_new();
        !            76:   BN_copy(prv->d, key->d);
        !            77:   prv->p = BN_new();
        !            78:   BN_copy(prv->p, key->p);
        !            79:   prv->q = BN_new();
        !            80:   BN_copy(prv->q, key->q);
        !            81:
        !            82:   prv->dmp1 = BN_new();
        !            83:   BN_copy(prv->dmp1, key->dmp1);
        !            84:
        !            85:   prv->dmq1 = BN_new();
        !            86:   BN_copy(prv->dmq1, key->dmq1);
        !            87:
        !            88:   prv->iqmp = BN_new();
        !            89:   BN_copy(prv->iqmp, key->iqmp);
        !            90:
        !            91:   RSA_free(key);
        !            92:
        !            93:   if (rsa_verbose)
        !            94:     printf("Key generation complete.\n");
        !            95: }
        !            96:
        !            97: void
        !            98: rsa_public_encrypt(BIGNUM *out, BIGNUM *in, RSA* key)
        !            99: {
        !           100:   char *inbuf, *outbuf;
        !           101:   int len;
        !           102:
        !           103:   len = BN_num_bytes(key->n);
        !           104:   outbuf = xmalloc(len);
        !           105:
        !           106:   len = BN_num_bytes(in);
        !           107:   inbuf = xmalloc(len);
        !           108:   BN_bn2bin(in, inbuf);
        !           109:
        !           110:   if ((len = RSA_public_encrypt(len, inbuf, outbuf, key,
        !           111:                                RSA_PKCS1_PADDING)) <= 0)
        !           112:     fatal("rsa_public_encrypt() failed");
        !           113:
        !           114:   BN_bin2bn(outbuf, len, out);
        !           115:
        !           116:   xfree(outbuf);
        !           117:   xfree(inbuf);
        !           118: }
        !           119:
        !           120: void
        !           121: rsa_private_decrypt(BIGNUM *out, BIGNUM *in, RSA *key)
        !           122: {
        !           123:   char *inbuf, *outbuf;
        !           124:   int len;
        !           125:
        !           126:   len = BN_num_bytes(key->n);
        !           127:   outbuf = xmalloc(len);
        !           128:
        !           129:   len = BN_num_bytes(in);
        !           130:   inbuf = xmalloc(len);
        !           131:   BN_bn2bin(in, inbuf);
        !           132:
        !           133:   if ((len = RSA_private_decrypt(len, inbuf, outbuf, key,
        !           134:                                 RSA_SSLV23_PADDING)) <= 0)
        !           135:     fatal("rsa_private_decrypt() failed");
        !           136:
        !           137:   BN_bin2bn(outbuf, len, out);
        !           138:
        !           139:   xfree(outbuf);
        !           140:   xfree(inbuf);
        !           141: }
        !           142:
        !           143: /* Set whether to output verbose messages during key generation. */
        !           144:
        !           145: void
        !           146: rsa_set_verbose(int verbose)
        !           147: {
        !           148:   rsa_verbose = verbose;
        !           149: }