summaryrefslogtreecommitdiff
path: root/lib/accelerated/intel/padlock.c
blob: 403a9d818fabd39cfcb5ae11632635c13b09997b (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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
/*
 * Copyright (C) 2011 Free Software Foundation, Inc.
 *
 * Author: Nikos Mavrogiannopoulos
 *
 * This file is part of GnuTLS.
 *
 * The GnuTLS 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 3 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 program.  If not, see <http://www.gnu.org/licenses/>
 *
 */

/*
 * The following code is an implementation of the AES-128-CBC cipher
 * using VIA Padlock instruction set. 
 */

#include <gnutls_errors.h>
#include <gnutls_int.h>
#include <gnutls/crypto.h>
#include <gnutls_errors.h>
#include <aes-x86.h>
#include <x86.h>
#ifdef HAVE_LIBNETTLE
# include <nettle/aes.h>         /* for key generation in 192 and 256 bits */
#endif

struct padlock_cipher_data {
    unsigned char iv[16];       /* Initialization vector */
    union {
        unsigned int pad[4];
        struct {
            int rounds:4;
            int dgst:1;         /* n/a in C3 */
            int align:1;        /* n/a in C3 */
            int ciphr:1;        /* n/a in C3 */
            unsigned int keygen:1;
            int interm:1;
            unsigned int encdec:1;
            int ksize:2;
        } b;
    } cword;                    /* Control word */
    AES_KEY ks;                 /* Encryption key */
};

struct padlock_ctx {
    struct padlock_cipher_data expanded_key;
    int enc;
};

unsigned int padlock_capability();
void padlock_key_bswap(AES_KEY * key);
void padlock_verify_context(struct padlock_cipher_data *ctx);
void padlock_reload_key();
void padlock_aes_block(void *out, const void *inp,
                       struct padlock_cipher_data *ctx);
int padlock_ecb_encrypt(void *out, const void *inp,
                        struct padlock_cipher_data *ctx, size_t len);
int padlock_cbc_encrypt(void *out, const void *inp,
                        struct padlock_cipher_data *ctx, size_t len);
int padlock_ctr32_encrypt(void *out, const void *inp,
                          struct padlock_cipher_data *ctx, size_t len);
void padlock_sha1_oneshot(void *ctx, const void *inp, size_t len);
void padlock_sha1(void *ctx, const void *inp, size_t len);
void padlock_sha256_oneshot(void *ctx, const void *inp, size_t len);
void padlock_sha256(void *ctx, const void *inp, size_t len);


static int
aes_cipher_init(gnutls_cipher_algorithm_t algorithm, void **_ctx, int enc)
{
    /* we use key size to distinguish */
    if (algorithm != GNUTLS_CIPHER_AES_128_CBC
        && algorithm != GNUTLS_CIPHER_AES_192_CBC
        && algorithm != GNUTLS_CIPHER_AES_256_CBC)
        return GNUTLS_E_INVALID_REQUEST;

    *_ctx = gnutls_calloc(1, sizeof(struct padlock_ctx));
    if (*_ctx == NULL) {
        gnutls_assert();
        return GNUTLS_E_MEMORY_ERROR;
    }

    ((struct padlock_ctx*)(*_ctx))->enc = enc;
    return 0;
}

static int
aes_cipher_setkey(void *_ctx, const void *userkey, size_t keysize)
{
    struct padlock_ctx *ctx = _ctx;
    struct padlock_cipher_data *pce;
#ifdef HAVE_LIBNETTLE
    struct aes_ctx nc;
#endif
    int ret;

    memset(_ctx, 0, sizeof(struct padlock_cipher_data));

    pce = ALIGN16(&ctx->expanded_key);

    pce->cword.b.encdec = (ctx->enc == 0);

    switch (keysize) {
    case 16:
        pce->cword.b.ksize = 0;
        pce->cword.b.rounds = 10;
        memcpy(pce->ks.rd_key, userkey, 16);
        pce->cword.b.keygen = 0;
        break;
#ifdef HAVE_LIBNETTLE
    case 24:
        pce->cword.b.ksize = 1;
        pce->cword.b.rounds = 12;
        goto common_24_32;
    case 32:
        pce->cword.b.ksize = 2;
        pce->cword.b.rounds = 14;
common_24_32:
        /* expand key using nettle */
        if (ctx->enc)
          aes_set_encrypt_key(&nc, keysize, userkey);
        else
          aes_set_decrypt_key(&nc, keysize, userkey);

        memcpy(pce->ks.rd_key, nc.keys, sizeof(nc.keys));
        pce->ks.rounds = nc.nrounds;

        pce->cword.b.keygen = 1;
        break;
#endif
    default:
        return gnutls_assert_val(GNUTLS_E_ENCRYPTION_FAILED);
    }
    
    padlock_reload_key ();

    return 0;
}

static int aes_setiv(void *_ctx, const void *iv, size_t iv_size)
{
    struct padlock_ctx *ctx = _ctx;
    struct padlock_cipher_data *pce;

    pce = ALIGN16(&ctx->expanded_key);

    memcpy(pce->iv, iv, 16);
    return 0;
}

static int
padlock_aes_encrypt(void *_ctx, const void *src, size_t src_size,
            void *dst, size_t dst_size)
{
    struct padlock_ctx *ctx = _ctx;
    struct padlock_cipher_data *pce;

    pce = ALIGN16(&ctx->expanded_key);

    padlock_cbc_encrypt(dst, src, pce, src_size);

    return 0;
}

static int
padlock_aes_decrypt(void *_ctx, const void *src, size_t src_size,
            void *dst, size_t dst_size)
{
    struct padlock_ctx *ctx = _ctx;
    struct padlock_cipher_data *pcd;

    pcd = ALIGN16(&ctx->expanded_key);

    padlock_cbc_encrypt(dst, src, pcd, src_size);

    return 0;
}

static void aes_deinit(void *_ctx)
{
    gnutls_free(_ctx);
}

static const gnutls_crypto_cipher_st cipher_struct = {
    .init = aes_cipher_init,
    .setkey = aes_cipher_setkey,
    .setiv = aes_setiv,
    .encrypt = padlock_aes_encrypt,
    .decrypt = padlock_aes_decrypt,
    .deinit = aes_deinit,
};

static int check_padlock(void)
{
    unsigned int edx = padlock_capability();

    return ((edx & (0x3 << 6)) == (0x3 << 6));
}

static unsigned check_via(void)
{
    unsigned int a, b, c, d;
    cpuid(0, a, b, c, d);

    if ((memcmp(&b, "VIA ", 4) == 0 &&
         memcmp(&d, "VIA ", 4) == 0 && memcmp(&c, "VIA ", 4) == 0)) {
        return 1;
    }

    return 0;
}

void register_padlock_crypto(void)
{
    int ret;

    if (check_via() == 0)
        return;

    if (check_padlock()) {
        _gnutls_debug_log("Padlock AES accelerator was detected\n");
        ret =
            gnutls_crypto_single_cipher_register(GNUTLS_CIPHER_AES_128_CBC,
                                                 80, &cipher_struct);
        if (ret < 0) {
            gnutls_assert();
        }

#ifdef HAVE_LIBNETTLE
        ret =
            gnutls_crypto_single_cipher_register(GNUTLS_CIPHER_AES_192_CBC,
                                                 80, &cipher_struct);
        if (ret < 0) {
            gnutls_assert();
        }

        ret =
            gnutls_crypto_single_cipher_register(GNUTLS_CIPHER_AES_256_CBC,
                                                 80, &cipher_struct);
        if (ret < 0) {
            gnutls_assert();
        }
#endif
    }

    return;
}