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

Annotation of src/usr.bin/ssh/ssh-ecdsa.c, Revision 1.5

1.5     ! miod        1: /* $OpenBSD: ssh-ecdsa.c,v 1.4 2010/09/10 01:04:10 djm Exp $ */
1.1       djm         2: /*
                      3:  * Copyright (c) 2000 Markus Friedl.  All rights reserved.
                      4:  * Copyright (c) 2010 Damien Miller.  All rights reserved.
                      5:  *
                      6:  * Redistribution and use in source and binary forms, with or without
                      7:  * modification, are permitted provided that the following conditions
                      8:  * are met:
                      9:  * 1. Redistributions of source code must retain the above copyright
                     10:  *    notice, this list of conditions and the following disclaimer.
                     11:  * 2. Redistributions in binary form must reproduce the above copyright
                     12:  *    notice, this list of conditions and the following disclaimer in the
                     13:  *    documentation and/or other materials provided with the distribution.
                     14:  *
                     15:  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
                     16:  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
                     17:  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
                     18:  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
                     19:  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
                     20:  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
                     21:  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
                     22:  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
                     23:  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
                     24:  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
                     25:  */
                     26:
                     27: #include <sys/types.h>
                     28:
                     29: #include <openssl/bn.h>
                     30: #include <openssl/ec.h>
                     31: #include <openssl/ecdsa.h>
                     32: #include <openssl/evp.h>
                     33:
                     34: #include <string.h>
                     35:
                     36: #include "xmalloc.h"
                     37: #include "buffer.h"
                     38: #include "compat.h"
                     39: #include "log.h"
                     40: #include "key.h"
                     41:
                     42: int
                     43: ssh_ecdsa_sign(const Key *key, u_char **sigp, u_int *lenp,
                     44:     const u_char *data, u_int datalen)
                     45: {
                     46:        ECDSA_SIG *sig;
1.3       djm        47:        const EVP_MD *evp_md;
1.1       djm        48:        EVP_MD_CTX md;
                     49:        u_char digest[EVP_MAX_MD_SIZE];
                     50:        u_int len, dlen;
                     51:        Buffer b, bb;
                     52:
                     53:        if (key == NULL || key->ecdsa == NULL ||
                     54:            (key->type != KEY_ECDSA && key->type != KEY_ECDSA_CERT)) {
                     55:                error("%s: no ECDSA key", __func__);
                     56:                return -1;
                     57:        }
1.3       djm        58:        evp_md = key_ec_nid_to_evpmd(key->ecdsa_nid);
1.1       djm        59:        EVP_DigestInit(&md, evp_md);
                     60:        EVP_DigestUpdate(&md, data, datalen);
                     61:        EVP_DigestFinal(&md, digest, &dlen);
                     62:
                     63:        sig = ECDSA_do_sign(digest, dlen, key->ecdsa);
                     64:        memset(digest, 'd', sizeof(digest));
                     65:
                     66:        if (sig == NULL) {
                     67:                error("%s: sign failed", __func__);
                     68:                return -1;
                     69:        }
                     70:
                     71:        buffer_init(&bb);
                     72:        buffer_put_bignum2(&bb, sig->r);
                     73:        buffer_put_bignum2(&bb, sig->s);
                     74:        ECDSA_SIG_free(sig);
                     75:
                     76:        buffer_init(&b);
                     77:        buffer_put_cstring(&b, key_ssh_name_plain(key));
                     78:        buffer_put_string(&b, buffer_ptr(&bb), buffer_len(&bb));
                     79:        buffer_free(&bb);
                     80:        len = buffer_len(&b);
                     81:        if (lenp != NULL)
                     82:                *lenp = len;
                     83:        if (sigp != NULL) {
                     84:                *sigp = xmalloc(len);
                     85:                memcpy(*sigp, buffer_ptr(&b), len);
                     86:        }
                     87:        buffer_free(&b);
                     88:
                     89:        return 0;
                     90: }
                     91: int
                     92: ssh_ecdsa_verify(const Key *key, const u_char *signature, u_int signaturelen,
                     93:     const u_char *data, u_int datalen)
                     94: {
                     95:        ECDSA_SIG *sig;
1.3       djm        96:        const EVP_MD *evp_md;
1.1       djm        97:        EVP_MD_CTX md;
                     98:        u_char digest[EVP_MAX_MD_SIZE], *sigblob;
                     99:        u_int len, dlen;
                    100:        int rlen, ret;
                    101:        Buffer b, bb;
1.2       deraadt   102:        char *ktype;
1.1       djm       103:
                    104:        if (key == NULL || key->ecdsa == NULL ||
                    105:            (key->type != KEY_ECDSA && key->type != KEY_ECDSA_CERT)) {
                    106:                error("%s: no ECDSA key", __func__);
                    107:                return -1;
                    108:        }
1.3       djm       109:        evp_md = key_ec_nid_to_evpmd(key->ecdsa_nid);
1.1       djm       110:
                    111:        /* fetch signature */
                    112:        buffer_init(&b);
                    113:        buffer_append(&b, signature, signaturelen);
                    114:        ktype = buffer_get_string(&b, NULL);
                    115:        if (strcmp(key_ssh_name_plain(key), ktype) != 0) {
                    116:                error("%s: cannot handle type %s", __func__, ktype);
                    117:                buffer_free(&b);
                    118:                xfree(ktype);
                    119:                return -1;
                    120:        }
                    121:        xfree(ktype);
                    122:        sigblob = buffer_get_string(&b, &len);
                    123:        rlen = buffer_len(&b);
                    124:        buffer_free(&b);
                    125:        if (rlen != 0) {
                    126:                error("%s: remaining bytes in signature %d", __func__, rlen);
                    127:                xfree(sigblob);
                    128:                return -1;
                    129:        }
                    130:
                    131:        /* parse signature */
                    132:        if ((sig = ECDSA_SIG_new()) == NULL)
                    133:                fatal("%s: ECDSA_SIG_new failed", __func__);
                    134:        if ((sig->r = BN_new()) == NULL ||
                    135:            (sig->s = BN_new()) == NULL)
                    136:                fatal("%s: BN_new failed", __func__);
                    137:
                    138:        buffer_init(&bb);
                    139:        buffer_append(&bb, sigblob, len);
                    140:        buffer_get_bignum2(&bb, sig->r);
                    141:        buffer_get_bignum2(&bb, sig->s);
                    142:        if (buffer_len(&bb) != 0)
                    143:                fatal("%s: remaining bytes in inner sigblob", __func__);
1.5     ! miod      144:        buffer_free(&bb);
1.1       djm       145:
                    146:        /* clean up */
                    147:        memset(sigblob, 0, len);
                    148:        xfree(sigblob);
                    149:
                    150:        /* hash the data */
                    151:        EVP_DigestInit(&md, evp_md);
                    152:        EVP_DigestUpdate(&md, data, datalen);
                    153:        EVP_DigestFinal(&md, digest, &dlen);
                    154:
                    155:        ret = ECDSA_do_verify(digest, dlen, sig, key->ecdsa);
                    156:        memset(digest, 'd', sizeof(digest));
                    157:
                    158:        ECDSA_SIG_free(sig);
                    159:
                    160:        debug("%s: signature %s", __func__,
                    161:            ret == 1 ? "correct" : ret == 0 ? "incorrect" : "error");
                    162:        return ret;
                    163: }