[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.16

1.1       provos      1: /*
1.10      deraadt     2:  * Author: Tatu Ylonen <ylo@cs.hut.fi>
                      3:  * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
                      4:  *                    All rights reserved
1.14      markus      5:  *
1.16    ! deraadt     6:  * As far as I am concerned, the code I have written for this software
        !             7:  * can be used freely for any purpose.  Any derived versions of this
        !             8:  * software must be clearly marked as such, and if the derived work is
        !             9:  * incompatible with the protocol description in the RFC file, it must be
        !            10:  * called by a name other than "ssh" or "Secure Shell".
        !            11:  *
        !            12:  *
        !            13:  * Copyright (c) 1999 Niels Provos.  All rights reserved.
        !            14:  *
        !            15:  * Redistribution and use in source and binary forms, with or without
        !            16:  * modification, are permitted provided that the following conditions
        !            17:  * are met:
        !            18:  * 1. Redistributions of source code must retain the above copyright
        !            19:  *    notice, this list of conditions and the following disclaimer.
        !            20:  * 2. Redistributions in binary form must reproduce the above copyright
        !            21:  *    notice, this list of conditions and the following disclaimer in the
        !            22:  *    documentation and/or other materials provided with the distribution.
        !            23:  *
        !            24:  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
        !            25:  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
        !            26:  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
        !            27:  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
        !            28:  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
        !            29:  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
        !            30:  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
        !            31:  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
        !            32:  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
        !            33:  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
1.14      markus     34:  *
1.16    ! deraadt    35:  *
        !            36:  * Description of the RSA algorithm can be found e.g. from the following
        !            37:  * sources:
1.14      markus     38:  *
1.10      deraadt    39:  *   Bruce Schneier: Applied Cryptography.  John Wiley & Sons, 1994.
1.14      markus     40:  *
1.10      deraadt    41:  *   Jennifer Seberry and Josed Pieprzyk: Cryptography: An Introduction to
                     42:  *   Computer Security.  Prentice-Hall, 1989.
1.14      markus     43:  *
1.10      deraadt    44:  *   Man Young Rhee: Cryptography and Secure Data Communications.  McGraw-Hill,
                     45:  *   1994.
1.14      markus     46:  *
1.10      deraadt    47:  *   R. Rivest, A. Shamir, and L. M. Adleman: Cryptographic Communications
                     48:  *   System and Method.  US Patent 4,405,829, 1983.
1.14      markus     49:  *
1.10      deraadt    50:  *   Hans Riesel: Prime Numbers and Computer Methods for Factorization.
                     51:  *   Birkhauser, 1994.
1.14      markus     52:  *
1.16    ! deraadt    53:  *   The RSA Frequently Asked Questions document by RSA Data Security,
        !            54:  *   Inc., 1995.
1.14      markus     55:  *
1.16    ! deraadt    56:  *   RSA in 3 lines of perl by Adam Back <aba@atlax.ex.ac.uk>, 1995, as
        !            57:  * included below:
1.14      markus     58:  *
1.10      deraadt    59:  *     [gone - had to be deleted - what a pity]
1.16    ! deraadt    60:  */
1.1       provos     61:
                     62: #include "includes.h"
1.16    ! deraadt    63: RCSID("$OpenBSD: rsa.c,v 1.15 2000/06/20 01:39:44 markus Exp $");
1.1       provos     64:
                     65: #include "rsa.h"
                     66: #include "ssh.h"
                     67: #include "xmalloc.h"
                     68:
                     69: int rsa_verbose = 1;
1.2       deraadt    70:
                     71: int
                     72: rsa_alive()
                     73: {
1.9       markus     74:        RSA *key;
1.2       deraadt    75:
1.9       markus     76:        key = RSA_generate_key(32, 3, NULL, NULL);
                     77:        if (key == NULL)
                     78:                return (0);
                     79:        RSA_free(key);
                     80:        return (1);
1.2       deraadt    81: }
1.1       provos     82:
1.11      markus     83: /*
                     84:  * Generates RSA public and private keys.  This initializes the data
                     85:  * structures; they should be freed with rsa_clear_private_key and
                     86:  * rsa_clear_public_key.
                     87:  */
1.1       provos     88:
                     89: void
                     90: rsa_generate_key(RSA *prv, RSA *pub, unsigned int bits)
                     91: {
1.9       markus     92:        RSA *key;
1.1       provos     93:
1.9       markus     94:        if (rsa_verbose) {
                     95:                printf("Generating RSA keys:  ");
                     96:                fflush(stdout);
                     97:        }
                     98:        key = RSA_generate_key(bits, 35, NULL, NULL);
                     99:        if (key == NULL)
                    100:                fatal("rsa_generate_key: key generation failed.");
                    101:
                    102:        /* Copy public key parameters */
                    103:        pub->n = BN_new();
                    104:        BN_copy(pub->n, key->n);
                    105:        pub->e = BN_new();
                    106:        BN_copy(pub->e, key->e);
                    107:
                    108:        /* Copy private key parameters */
                    109:        prv->n = BN_new();
                    110:        BN_copy(prv->n, key->n);
                    111:        prv->e = BN_new();
                    112:        BN_copy(prv->e, key->e);
                    113:        prv->d = BN_new();
                    114:        BN_copy(prv->d, key->d);
                    115:        prv->p = BN_new();
                    116:        BN_copy(prv->p, key->p);
                    117:        prv->q = BN_new();
                    118:        BN_copy(prv->q, key->q);
                    119:
                    120:        prv->dmp1 = BN_new();
                    121:        BN_copy(prv->dmp1, key->dmp1);
                    122:
                    123:        prv->dmq1 = BN_new();
                    124:        BN_copy(prv->dmq1, key->dmq1);
                    125:
                    126:        prv->iqmp = BN_new();
                    127:        BN_copy(prv->iqmp, key->iqmp);
                    128:
                    129:        RSA_free(key);
                    130:
                    131:        if (rsa_verbose)
                    132:                printf("Key generation complete.\n");
1.1       provos    133: }
                    134:
                    135: void
1.9       markus    136: rsa_public_encrypt(BIGNUM *out, BIGNUM *in, RSA *key)
1.1       provos    137: {
1.13      markus    138:        unsigned char *inbuf, *outbuf;
1.9       markus    139:        int len, ilen, olen;
1.4       provos    140:
1.9       markus    141:        if (BN_num_bits(key->e) < 2 || !BN_is_odd(key->e))
                    142:                fatal("rsa_public_encrypt() exponent too small or not odd");
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);
1.1       provos    150:
1.9       markus    151:        if ((len = RSA_public_encrypt(ilen, inbuf, outbuf, key,
1.12      markus    152:            RSA_PKCS1_PADDING)) <= 0)
1.9       markus    153:                fatal("rsa_public_encrypt() failed");
1.1       provos    154:
1.9       markus    155:        BN_bin2bn(outbuf, len, out);
1.1       provos    156:
1.9       markus    157:        memset(outbuf, 0, olen);
                    158:        memset(inbuf, 0, ilen);
                    159:        xfree(outbuf);
                    160:        xfree(inbuf);
1.1       provos    161: }
                    162:
                    163: void
                    164: rsa_private_decrypt(BIGNUM *out, BIGNUM *in, RSA *key)
                    165: {
1.13      markus    166:        unsigned char *inbuf, *outbuf;
1.9       markus    167:        int len, ilen, olen;
1.1       provos    168:
1.9       markus    169:        olen = BN_num_bytes(key->n);
                    170:        outbuf = xmalloc(olen);
1.1       provos    171:
1.9       markus    172:        ilen = BN_num_bytes(in);
                    173:        inbuf = xmalloc(ilen);
                    174:        BN_bn2bin(in, inbuf);
                    175:
                    176:        if ((len = RSA_private_decrypt(ilen, inbuf, outbuf, key,
1.12      markus    177:            RSA_PKCS1_PADDING)) <= 0)
1.9       markus    178:                fatal("rsa_private_decrypt() failed");
                    179:
                    180:        BN_bin2bn(outbuf, len, out);
                    181:
                    182:        memset(outbuf, 0, olen);
                    183:        memset(inbuf, 0, ilen);
                    184:        xfree(outbuf);
                    185:        xfree(inbuf);
1.1       provos    186: }
                    187:
                    188: /* Set whether to output verbose messages during key generation. */
                    189:
                    190: void
                    191: rsa_set_verbose(int verbose)
                    192: {
1.9       markus    193:        rsa_verbose = verbose;
1.1       provos    194: }