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

1.1       djm         1: /* $OpenBSD */
                      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;
                     47:        const EVP_MD *evp_md = EVP_sha256();
                     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:        }
                     58:        EVP_DigestInit(&md, evp_md);
                     59:        EVP_DigestUpdate(&md, data, datalen);
                     60:        EVP_DigestFinal(&md, digest, &dlen);
                     61:
                     62:        sig = ECDSA_do_sign(digest, dlen, key->ecdsa);
                     63:        memset(digest, 'd', sizeof(digest));
                     64:
                     65:        if (sig == NULL) {
                     66:                error("%s: sign failed", __func__);
                     67:                return -1;
                     68:        }
                     69:
                     70:        buffer_init(&bb);
                     71:        buffer_put_bignum2(&bb, sig->r);
                     72:        buffer_put_bignum2(&bb, sig->s);
                     73:        ECDSA_SIG_free(sig);
                     74:
                     75:        buffer_init(&b);
                     76:        buffer_put_cstring(&b, key_ssh_name_plain(key));
                     77:        buffer_put_string(&b, buffer_ptr(&bb), buffer_len(&bb));
                     78:        buffer_free(&bb);
                     79:        len = buffer_len(&b);
                     80:        if (lenp != NULL)
                     81:                *lenp = len;
                     82:        if (sigp != NULL) {
                     83:                *sigp = xmalloc(len);
                     84:                memcpy(*sigp, buffer_ptr(&b), len);
                     85:        }
                     86:        buffer_free(&b);
                     87:
                     88:        return 0;
                     89: }
                     90: int
                     91: ssh_ecdsa_verify(const Key *key, const u_char *signature, u_int signaturelen,
                     92:     const u_char *data, u_int datalen)
                     93: {
                     94:        ECDSA_SIG *sig;
                     95:        const EVP_MD *evp_md = EVP_sha256();
                     96:        EVP_MD_CTX md;
                     97:        u_char digest[EVP_MAX_MD_SIZE], *sigblob;
                     98:        u_int len, dlen;
                     99:        int rlen, ret;
                    100:        Buffer b, bb;
1.2     ! deraadt   101:        char *ktype;
1.1       djm       102:
                    103:        if (key == NULL || key->ecdsa == NULL ||
                    104:            (key->type != KEY_ECDSA && key->type != KEY_ECDSA_CERT)) {
                    105:                error("%s: no ECDSA key", __func__);
                    106:                return -1;
                    107:        }
                    108:
                    109:        /* fetch signature */
                    110:        buffer_init(&b);
                    111:        buffer_append(&b, signature, signaturelen);
                    112:        ktype = buffer_get_string(&b, NULL);
                    113:        if (strcmp(key_ssh_name_plain(key), ktype) != 0) {
                    114:                error("%s: cannot handle type %s", __func__, ktype);
                    115:                buffer_free(&b);
                    116:                xfree(ktype);
                    117:                return -1;
                    118:        }
                    119:        xfree(ktype);
                    120:        sigblob = buffer_get_string(&b, &len);
                    121:        rlen = buffer_len(&b);
                    122:        buffer_free(&b);
                    123:        if (rlen != 0) {
                    124:                error("%s: remaining bytes in signature %d", __func__, rlen);
                    125:                xfree(sigblob);
                    126:                return -1;
                    127:        }
                    128:
                    129:        /* parse signature */
                    130:        if ((sig = ECDSA_SIG_new()) == NULL)
                    131:                fatal("%s: ECDSA_SIG_new failed", __func__);
                    132:        if ((sig->r = BN_new()) == NULL ||
                    133:            (sig->s = BN_new()) == NULL)
                    134:                fatal("%s: BN_new failed", __func__);
                    135:
                    136:        buffer_init(&bb);
                    137:        buffer_append(&bb, sigblob, len);
                    138:        buffer_get_bignum2(&bb, sig->r);
                    139:        buffer_get_bignum2(&bb, sig->s);
                    140:        if (buffer_len(&bb) != 0)
                    141:                fatal("%s: remaining bytes in inner sigblob", __func__);
                    142:
                    143:        /* clean up */
                    144:        memset(sigblob, 0, len);
                    145:        xfree(sigblob);
                    146:
                    147:        /* hash the data */
                    148:        EVP_DigestInit(&md, evp_md);
                    149:        EVP_DigestUpdate(&md, data, datalen);
                    150:        EVP_DigestFinal(&md, digest, &dlen);
                    151:
                    152:        ret = ECDSA_do_verify(digest, dlen, sig, key->ecdsa);
                    153:        memset(digest, 'd', sizeof(digest));
                    154:
                    155:        ECDSA_SIG_free(sig);
                    156:
                    157:        debug("%s: signature %s", __func__,
                    158:            ret == 1 ? "correct" : ret == 0 ? "incorrect" : "error");
                    159:        return ret;
                    160: }