aboutsummaryrefslogtreecommitdiff
path: root/src/kdf.c
blob: 0964473984b2bd05d0da25ffddf4d2da75e2294b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/*
 * This file is part of the SSH Library
 *
 * Copyright (c) 2009 by Aris Adamantiadis
 * Copyrihgt (c) 2018 Red Hat, Inc.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

#include "config.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>

#include "libssh/priv.h"
#include "libssh/crypto.h"
#include "libssh/buffer.h"
#include "libssh/session.h"
#include "libssh/misc.h"
#include "libssh/dh.h"
#include "libssh/ssh2.h"
#include "libssh/pki.h"
#include "libssh/bignum.h"

#include "libssh/string.h"


/* The following implements the SSHKDF for crypto backend that
 * do not have a native implementations */
struct ssh_mac_ctx_struct {
    enum ssh_kdf_digest digest_type;
    union {
        SHACTX sha1_ctx;
        SHA256CTX sha256_ctx;
        SHA384CTX sha384_ctx;
        SHA512CTX sha512_ctx;
    } ctx;
};

static ssh_mac_ctx ssh_mac_ctx_init(enum ssh_kdf_digest type)
{
    ssh_mac_ctx ctx = malloc(sizeof(struct ssh_mac_ctx_struct));
    if (ctx == NULL) {
        return NULL;
    }

    ctx->digest_type = type;
    switch(type){
    case SSH_KDF_SHA1:
        ctx->ctx.sha1_ctx = sha1_init();
        return ctx;
    case SSH_KDF_SHA256:
        ctx->ctx.sha256_ctx = sha256_init();
        return ctx;
    case SSH_KDF_SHA384:
        ctx->ctx.sha384_ctx = sha384_init();
        return ctx;
    case SSH_KDF_SHA512:
        ctx->ctx.sha512_ctx = sha512_init();
        return ctx;
    default:
        SAFE_FREE(ctx);
        return NULL;
    }
}

static void ssh_mac_update(ssh_mac_ctx ctx, const void *data, size_t len)
{
    switch(ctx->digest_type){
    case SSH_KDF_SHA1:
        sha1_update(ctx->ctx.sha1_ctx, data, len);
        break;
    case SSH_KDF_SHA256:
        sha256_update(ctx->ctx.sha256_ctx, data, len);
        break;
    case SSH_KDF_SHA384:
        sha384_update(ctx->ctx.sha384_ctx, data, len);
        break;
    case SSH_KDF_SHA512:
        sha512_update(ctx->ctx.sha512_ctx, data, len);
        break;
    }
}

static void ssh_mac_final(unsigned char *md, ssh_mac_ctx ctx)
{
    switch(ctx->digest_type){
    case SSH_KDF_SHA1:
        sha1_final(md,ctx->ctx.sha1_ctx);
        break;
    case SSH_KDF_SHA256:
        sha256_final(md,ctx->ctx.sha256_ctx);
        break;
    case SSH_KDF_SHA384:
        sha384_final(md,ctx->ctx.sha384_ctx);
        break;
    case SSH_KDF_SHA512:
        sha512_final(md,ctx->ctx.sha512_ctx);
        break;
    }
    SAFE_FREE(ctx);
}

int sshkdf_derive_key(struct ssh_crypto_struct *crypto,
                      unsigned char *key, size_t key_len,
                      int key_type, unsigned char *output,
                      size_t requested_len)
{
    /* Can't use VLAs with Visual Studio, so allocate the biggest
     * digest buffer we can possibly need */
    unsigned char digest[DIGEST_MAX_LEN];
    size_t output_len = crypto->digest_len;
    char letter = key_type;
    ssh_mac_ctx ctx;

    if (DIGEST_MAX_LEN < crypto->digest_len) {
        return -1;
    }

    ctx = ssh_mac_ctx_init(crypto->digest_type);
    if (ctx == NULL) {
        return -1;
    }

    ssh_mac_update(ctx, key, key_len);
    ssh_mac_update(ctx, crypto->secret_hash, crypto->digest_len);
    ssh_mac_update(ctx, &letter, 1);
    ssh_mac_update(ctx, crypto->session_id, crypto->session_id_len);
    ssh_mac_final(digest, ctx);

    if (requested_len < output_len) {
        output_len = requested_len;
    }
    memcpy(output, digest, output_len);

    while (requested_len > output_len) {
        ctx = ssh_mac_ctx_init(crypto->digest_type);
        if (ctx == NULL) {
            return -1;
        }
        ssh_mac_update(ctx, key, key_len);
        ssh_mac_update(ctx, crypto->secret_hash, crypto->digest_len);
        ssh_mac_update(ctx, output, output_len);
        ssh_mac_final(digest, ctx);
        if (requested_len < output_len + crypto->digest_len) {
            memcpy(output + output_len, digest, requested_len - output_len);
        } else {
            memcpy(output + output_len, digest, crypto->digest_len);
        }
        output_len += crypto->digest_len;
    }

    return 0;
}