Coverage Report

Created: 2025-07-23 06:59

/src/wolfssl-normal-math/src/keys.c
Line
Count
Source (jump to first uncovered line)
1
/* keys.c
2
 *
3
 * Copyright (C) 2006-2025 wolfSSL Inc.
4
 *
5
 * This file is part of wolfSSL.
6
 *
7
 * wolfSSL is free software; you can redistribute it and/or modify
8
 * it under the terms of the GNU General Public License as published by
9
 * the Free Software Foundation; either version 3 of the License, or
10
 * (at your option) any later version.
11
 *
12
 * wolfSSL is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 * GNU General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU General Public License
18
 * along with this program; if not, write to the Free Software
19
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
20
 */
21
22
23
/* Name change compatibility layer no longer needs to be included here */
24
25
#include <wolfssl/wolfcrypt/libwolfssl_sources.h>
26
27
#if !defined(WOLFCRYPT_ONLY) && !defined(NO_TLS)
28
29
#include <wolfssl/internal.h>
30
#include <wolfssl/error-ssl.h>
31
#if defined(SHOW_SECRETS) || defined(CHACHA_AEAD_TEST)
32
    #ifndef NO_STDIO_FILESYSTEM
33
        #include <stdio.h>
34
    #endif
35
#endif
36
37
#if defined(WOLFSSL_RENESAS_FSPSM_TLS) || defined(WOLFSSL_RENESAS_TSIP_TLS)
38
#include <wolfssl/wolfcrypt/port/Renesas/renesas_cmn.h>
39
#endif
40
41
int SetCipherSpecs(WOLFSSL* ssl)
42
0
{
43
0
    int ret = GetCipherSpec(ssl->options.side, ssl->options.cipherSuite0,
44
0
                                ssl->options.cipherSuite, &ssl->specs,
45
0
                                &ssl->options);
46
0
    if (ret == 0) {
47
        /* set TLS if it hasn't been turned off */
48
0
        if (ssl->version.major == SSLv3_MAJOR &&
49
0
                ssl->version.minor >= TLSv1_MINOR) {
50
0
    #ifndef NO_TLS
51
0
            ssl->options.tls = 1;
52
0
        #if !defined(WOLFSSL_NO_TLS12) && !defined(WOLFSSL_AEAD_ONLY)
53
0
            #if !defined(WOLFSSL_RENESAS_FSPSM_TLS) && \
54
0
                !defined(WOLFSSL_RENESAS_TSIP_TLS)
55
0
            ssl->hmac = TLS_hmac;
56
            #else
57
            ssl->hmac = Renesas_cmn_TLS_hmac;
58
            #endif
59
0
        #endif
60
0
            if (ssl->version.minor >= TLSv1_1_MINOR) {
61
0
                ssl->options.tls1_1 = 1;
62
0
                if (ssl->version.minor >= TLSv1_3_MINOR)
63
0
                    ssl->options.tls1_3 = 1;
64
0
            }
65
0
    #endif
66
0
        }
67
68
0
    #if defined(HAVE_ENCRYPT_THEN_MAC) && !defined(WOLFSSL_AEAD_ONLY)
69
0
        if (IsAtLeastTLSv1_3(ssl->version) || ssl->specs.cipher_type != block)
70
0
           ssl->options.encThenMac = 0;
71
0
    #endif
72
73
    #if defined(WOLFSSL_DTLS)
74
        if (ssl->options.dtls && ssl->version.major == DTLS_MAJOR) {
75
        #ifndef WOLFSSL_AEAD_ONLY
76
            #if !defined(WOLFSSL_RENESAS_FSPSM_TLS) && \
77
                !defined(WOLFSSL_RENESAS_TSIP_TLS)
78
            ssl->hmac = TLS_hmac;
79
            #else
80
            ssl->hmac = Renesas_cmn_TLS_hmac;
81
            #endif
82
        #endif
83
            ssl->options.tls = 1;
84
            ssl->options.tls1_1 = 1; /* DTLS 1.0 == TLS 1.1 */
85
        #ifdef WOLFSSL_DTLS13
86
            if (ssl->version.minor <= DTLSv1_3_MINOR)
87
                ssl->options.tls1_3 = 1;
88
        #endif
89
        }
90
    #endif
91
0
    }
92
0
    return ret;
93
0
}
94
95
/**
96
 * Populate specs with the specification of the chosen ciphersuite. If opts is
97
 * not NULL then the appropriate options will also be set.
98
 *
99
 * @param side         [in] WOLFSSL_SERVER_END or WOLFSSL_CLIENT_END
100
 * @param cipherSuite0 [in]
101
 * @param cipherSuite  [in]
102
 * @param specs        [out] CipherSpecs
103
 * @param opts         [in/out] Options can be NULL
104
 * @return int (less than 0 on fail, 0 on success)
105
 */
106
int GetCipherSpec(word16 side, byte cipherSuite0, byte cipherSuite,
107
                      CipherSpecs* specs, Options* opts)
108
0
{
109
0
    word16 havePSK = 0;
110
0
    (void)havePSK;
111
0
    (void)side;
112
#if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK)
113
    if (opts != NULL)
114
        havePSK = opts->havePSK;
115
#endif
116
0
#ifndef NO_WOLFSSL_CLIENT
117
0
    if (side == WOLFSSL_CLIENT_END) {
118
        /* server side verified before SetCipherSpecs call */
119
0
        if (VerifyClientSuite(havePSK, cipherSuite0, cipherSuite) != 1) {
120
0
            WOLFSSL_MSG("SetCipherSpecs() client has an unusable suite");
121
0
            WOLFSSL_ERROR_VERBOSE(UNSUPPORTED_SUITE);
122
0
            return UNSUPPORTED_SUITE;
123
0
        }
124
0
    }
125
0
#endif /* NO_WOLFSSL_CLIENT */
126
127
    /* Initialize specs */
128
0
    XMEMSET(specs, 0, sizeof(CipherSpecs));
129
130
    /* Chacha extensions, 0xcc */
131
0
    if (cipherSuite0 == CHACHA_BYTE) {
132
133
0
    switch (cipherSuite) {
134
0
#ifdef BUILD_TLS_ECDHE_RSA_WITH_CHACHA20_OLD_POLY1305_SHA256
135
0
    case TLS_ECDHE_RSA_WITH_CHACHA20_OLD_POLY1305_SHA256:
136
0
        specs->bulk_cipher_algorithm = wolfssl_chacha;
137
0
        specs->cipher_type           = aead;
138
0
        specs->mac_algorithm         = sha256_mac;
139
0
        specs->kea                   = ecc_diffie_hellman_kea;
140
0
        specs->sig_algo              = rsa_sa_algo;
141
0
        specs->hash_size             = WC_SHA256_DIGEST_SIZE;
142
0
        specs->pad_size              = PAD_SHA;
143
0
        specs->static_ecdh           = 0;
144
0
        specs->key_size              = CHACHA20_256_KEY_SIZE;
145
0
        specs->block_size            = CHACHA20_BLOCK_SIZE;
146
0
        specs->iv_size               = CHACHA20_IV_SIZE;
147
0
        specs->aead_mac_size         = POLY1305_AUTH_SZ;
148
0
        if (opts != NULL)
149
0
            opts->oldPoly            = 1; /* use old poly1305 padding */
150
151
0
        break;
152
0
#endif
153
154
0
#ifdef BUILD_TLS_ECDHE_ECDSA_WITH_CHACHA20_OLD_POLY1305_SHA256
155
0
    case TLS_ECDHE_ECDSA_WITH_CHACHA20_OLD_POLY1305_SHA256:
156
0
        specs->bulk_cipher_algorithm = wolfssl_chacha;
157
0
        specs->cipher_type           = aead;
158
0
        specs->mac_algorithm         = sha256_mac;
159
0
        specs->kea                   = ecc_diffie_hellman_kea;
160
0
        specs->sig_algo              = ecc_dsa_sa_algo;
161
0
        specs->hash_size             = WC_SHA256_DIGEST_SIZE;
162
0
        specs->pad_size              = PAD_SHA;
163
0
        specs->static_ecdh           = 0;
164
0
        specs->key_size              = CHACHA20_256_KEY_SIZE;
165
0
        specs->block_size            = CHACHA20_BLOCK_SIZE;
166
0
        specs->iv_size               = CHACHA20_IV_SIZE;
167
0
        specs->aead_mac_size         = POLY1305_AUTH_SZ;
168
0
        if (opts != NULL)
169
0
            opts->oldPoly            = 1; /* use old poly1305 padding */
170
171
0
        break;
172
0
#endif
173
174
0
#ifdef BUILD_TLS_DHE_RSA_WITH_CHACHA20_OLD_POLY1305_SHA256
175
0
    case TLS_DHE_RSA_WITH_CHACHA20_OLD_POLY1305_SHA256:
176
0
        specs->bulk_cipher_algorithm = wolfssl_chacha;
177
0
        specs->cipher_type           = aead;
178
0
        specs->mac_algorithm         = sha256_mac;
179
0
        specs->kea                   = diffie_hellman_kea;
180
0
        specs->sig_algo              = rsa_sa_algo;
181
0
        specs->hash_size             = WC_SHA256_DIGEST_SIZE;
182
0
        specs->pad_size              = PAD_SHA;
183
0
        specs->static_ecdh           = 0;
184
0
        specs->key_size              = CHACHA20_256_KEY_SIZE;
185
0
        specs->block_size            = CHACHA20_BLOCK_SIZE;
186
0
        specs->iv_size               = CHACHA20_IV_SIZE;
187
0
        specs->aead_mac_size         = POLY1305_AUTH_SZ;
188
0
        if (opts != NULL)
189
0
            opts->oldPoly            = 1; /* use old poly1305 padding */
190
191
0
        break;
192
0
#endif
193
0
#ifdef BUILD_TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256
194
0
    case TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256:
195
0
        specs->bulk_cipher_algorithm = wolfssl_chacha;
196
0
        specs->cipher_type           = aead;
197
0
        specs->mac_algorithm         = sha256_mac;
198
0
        specs->kea                   = ecc_diffie_hellman_kea;
199
0
        specs->sig_algo              = rsa_sa_algo;
200
0
        specs->hash_size             = WC_SHA256_DIGEST_SIZE;
201
0
        specs->pad_size              = PAD_SHA;
202
0
        specs->static_ecdh           = 0;
203
0
        specs->key_size              = CHACHA20_256_KEY_SIZE;
204
0
        specs->block_size            = CHACHA20_BLOCK_SIZE;
205
0
        specs->iv_size               = CHACHA20_IV_SIZE;
206
0
        specs->aead_mac_size         = POLY1305_AUTH_SZ;
207
0
        if (opts != NULL)
208
0
            opts->oldPoly            = 0; /* use recent padding RFC */
209
210
0
        break;
211
0
#endif
212
213
0
#ifdef BUILD_TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256
214
0
    case TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256:
215
0
        specs->bulk_cipher_algorithm = wolfssl_chacha;
216
0
        specs->cipher_type           = aead;
217
0
        specs->mac_algorithm         = sha256_mac;
218
0
        specs->kea                   = ecc_diffie_hellman_kea;
219
0
        specs->sig_algo              = ecc_dsa_sa_algo;
220
0
        specs->hash_size             = WC_SHA256_DIGEST_SIZE;
221
0
        specs->pad_size              = PAD_SHA;
222
0
        specs->static_ecdh           = 0;
223
0
        specs->key_size              = CHACHA20_256_KEY_SIZE;
224
0
        specs->block_size            = CHACHA20_BLOCK_SIZE;
225
0
        specs->iv_size               = CHACHA20_IV_SIZE;
226
0
        specs->aead_mac_size         = POLY1305_AUTH_SZ;
227
0
        if (opts != NULL)
228
0
            opts->oldPoly            = 0; /* use recent padding RFC */
229
230
0
        break;
231
0
#endif
232
233
0
#ifdef BUILD_TLS_DHE_RSA_WITH_CHACHA20_POLY1305_SHA256
234
0
    case TLS_DHE_RSA_WITH_CHACHA20_POLY1305_SHA256:
235
0
        specs->bulk_cipher_algorithm = wolfssl_chacha;
236
0
        specs->cipher_type           = aead;
237
0
        specs->mac_algorithm         = sha256_mac;
238
0
        specs->kea                   = diffie_hellman_kea;
239
0
        specs->sig_algo              = rsa_sa_algo;
240
0
        specs->hash_size             = WC_SHA256_DIGEST_SIZE;
241
0
        specs->pad_size              = PAD_SHA;
242
0
        specs->static_ecdh           = 0;
243
0
        specs->key_size              = CHACHA20_256_KEY_SIZE;
244
0
        specs->block_size            = CHACHA20_BLOCK_SIZE;
245
0
        specs->iv_size               = CHACHA20_IV_SIZE;
246
0
        specs->aead_mac_size         = POLY1305_AUTH_SZ;
247
0
        if (opts != NULL)
248
0
            opts->oldPoly            = 0; /* use recent padding RFC */
249
250
0
        break;
251
0
#endif
252
253
#ifdef BUILD_TLS_PSK_WITH_CHACHA20_POLY1305_SHA256
254
    case TLS_PSK_WITH_CHACHA20_POLY1305_SHA256:
255
        specs->bulk_cipher_algorithm = wolfssl_chacha;
256
        specs->cipher_type           = aead;
257
        specs->mac_algorithm         = sha256_mac;
258
        specs->kea                   = psk_kea;
259
        specs->sig_algo              = anonymous_sa_algo;
260
        specs->hash_size             = WC_SHA256_DIGEST_SIZE;
261
        specs->pad_size              = PAD_SHA;
262
        specs->static_ecdh           = 0;
263
        specs->key_size              = CHACHA20_256_KEY_SIZE;
264
        specs->block_size            = CHACHA20_BLOCK_SIZE;
265
        specs->iv_size               = CHACHA20_IV_SIZE;
266
        specs->aead_mac_size         = POLY1305_AUTH_SZ;
267
268
        if (opts != NULL) {
269
            opts->oldPoly            = 0; /* use recent padding RFC */
270
            opts->usingPSK_cipher    = 1;
271
        }
272
        break;
273
#endif
274
275
#ifdef BUILD_TLS_ECDHE_PSK_WITH_CHACHA20_POLY1305_SHA256
276
    case TLS_ECDHE_PSK_WITH_CHACHA20_POLY1305_SHA256:
277
        specs->bulk_cipher_algorithm = wolfssl_chacha;
278
        specs->cipher_type           = aead;
279
        specs->mac_algorithm         = sha256_mac;
280
        specs->kea                   = ecdhe_psk_kea;
281
        specs->sig_algo              = anonymous_sa_algo;
282
        specs->hash_size             = WC_SHA256_DIGEST_SIZE;
283
        specs->pad_size              = PAD_SHA;
284
        specs->static_ecdh           = 0;
285
        specs->key_size              = CHACHA20_256_KEY_SIZE;
286
        specs->block_size            = CHACHA20_BLOCK_SIZE;
287
        specs->iv_size               = CHACHA20_IV_SIZE;
288
        specs->aead_mac_size         = POLY1305_AUTH_SZ;
289
290
        if (opts != NULL) {
291
            opts->oldPoly            = 0; /* use recent padding RFC */
292
            opts->usingPSK_cipher    = 1;
293
        }
294
        break;
295
#endif
296
297
#ifdef BUILD_TLS_DHE_PSK_WITH_CHACHA20_POLY1305_SHA256
298
    case TLS_DHE_PSK_WITH_CHACHA20_POLY1305_SHA256:
299
        specs->bulk_cipher_algorithm = wolfssl_chacha;
300
        specs->cipher_type           = aead;
301
        specs->mac_algorithm         = sha256_mac;
302
        specs->kea                   = dhe_psk_kea;
303
        specs->sig_algo              = anonymous_sa_algo;
304
        specs->hash_size             = WC_SHA256_DIGEST_SIZE;
305
        specs->pad_size              = PAD_SHA;
306
        specs->static_ecdh           = 0;
307
        specs->key_size              = CHACHA20_256_KEY_SIZE;
308
        specs->block_size            = CHACHA20_BLOCK_SIZE;
309
        specs->iv_size               = CHACHA20_IV_SIZE;
310
        specs->aead_mac_size         = POLY1305_AUTH_SZ;
311
312
        if (opts != NULL) {
313
            opts->oldPoly            = 0; /* use recent padding RFC */
314
            opts->usingPSK_cipher    = 1;
315
        }
316
        break;
317
#endif
318
0
    default:
319
0
        WOLFSSL_MSG("Unsupported cipher suite, SetCipherSpecs ChaCha");
320
0
        return UNSUPPORTED_SUITE;
321
0
    }
322
0
    }
323
324
    /* ECC extensions, AES-CCM or TLS 1.3 Integrity-only */
325
0
    if (cipherSuite0 == ECC_BYTE) {
326
327
0
    switch (cipherSuite) {
328
329
0
#if defined(HAVE_ECC) || defined(HAVE_CURVE25519) || defined(HAVE_CURVE448)
330
331
0
#ifdef BUILD_TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256
332
0
    case TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256 :
333
0
        specs->bulk_cipher_algorithm = wolfssl_aes;
334
0
        specs->cipher_type           = block;
335
0
        specs->mac_algorithm         = sha256_mac;
336
0
        specs->kea                   = ecc_diffie_hellman_kea;
337
0
        specs->sig_algo              = rsa_sa_algo;
338
0
        specs->hash_size             = WC_SHA256_DIGEST_SIZE;
339
0
        specs->pad_size              = PAD_SHA;
340
0
        specs->static_ecdh           = 0;
341
0
        specs->key_size              = AES_128_KEY_SIZE;
342
0
        specs->iv_size               = AES_IV_SIZE;
343
0
        specs->block_size            = WC_AES_BLOCK_SIZE;
344
345
0
        break;
346
0
#endif
347
348
0
#ifdef BUILD_TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384
349
0
    case TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384 :
350
0
        specs->bulk_cipher_algorithm = wolfssl_aes;
351
0
        specs->cipher_type           = block;
352
0
        specs->mac_algorithm         = sha384_mac;
353
0
        specs->kea                   = ecc_diffie_hellman_kea;
354
0
        specs->sig_algo              = rsa_sa_algo;
355
0
        specs->hash_size             = WC_SHA384_DIGEST_SIZE;
356
0
        specs->pad_size              = PAD_SHA;
357
0
        specs->static_ecdh           = 0;
358
0
        specs->key_size              = AES_256_KEY_SIZE;
359
0
        specs->iv_size               = AES_IV_SIZE;
360
0
        specs->block_size            = WC_AES_BLOCK_SIZE;
361
362
0
        break;
363
0
#endif
364
365
0
#ifdef BUILD_TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA
366
0
    case TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA :
367
0
        specs->bulk_cipher_algorithm = wolfssl_aes;
368
0
        specs->cipher_type           = block;
369
0
        specs->mac_algorithm         = sha_mac;
370
0
        specs->kea                   = ecc_diffie_hellman_kea;
371
0
        specs->sig_algo              = rsa_sa_algo;
372
0
        specs->hash_size             = WC_SHA_DIGEST_SIZE;
373
0
        specs->pad_size              = PAD_SHA;
374
0
        specs->static_ecdh           = 0;
375
0
        specs->key_size              = AES_128_KEY_SIZE;
376
0
        specs->block_size            = WC_AES_BLOCK_SIZE;
377
0
        specs->iv_size               = AES_IV_SIZE;
378
379
0
        break;
380
0
#endif
381
382
#ifdef BUILD_TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA
383
    case TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA :
384
        specs->bulk_cipher_algorithm = wolfssl_triple_des;
385
        specs->cipher_type           = block;
386
        specs->mac_algorithm         = sha_mac;
387
        specs->kea                   = ecc_diffie_hellman_kea;
388
        specs->sig_algo              = rsa_sa_algo;
389
        specs->hash_size             = WC_SHA_DIGEST_SIZE;
390
        specs->pad_size              = PAD_SHA;
391
        specs->static_ecdh           = 0;
392
        specs->key_size              = DES3_KEY_SIZE;
393
        specs->block_size            = DES_BLOCK_SIZE;
394
/* DES_IV_SIZE is incorrectly 16 in FIPS v2. It should be 8, same as the
395
 * block size. */
396
#if defined(HAVE_FIPS) && defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION == 2)
397
        specs->iv_size               = DES_BLOCK_SIZE;
398
#else
399
        specs->iv_size               = DES_IV_SIZE;
400
#endif
401
402
        break;
403
#endif
404
405
0
#ifdef BUILD_TLS_ECDHE_RSA_WITH_RC4_128_SHA
406
0
    case TLS_ECDHE_RSA_WITH_RC4_128_SHA :
407
0
        specs->bulk_cipher_algorithm = wolfssl_rc4;
408
0
        specs->cipher_type           = stream;
409
0
        specs->mac_algorithm         = sha_mac;
410
0
        specs->kea                   = ecc_diffie_hellman_kea;
411
0
        specs->sig_algo              = rsa_sa_algo;
412
0
        specs->hash_size             = WC_SHA_DIGEST_SIZE;
413
0
        specs->pad_size              = PAD_SHA;
414
0
        specs->static_ecdh           = 0;
415
0
        specs->key_size              = RC4_KEY_SIZE;
416
0
        specs->iv_size               = 0;
417
0
        specs->block_size            = 0;
418
419
0
        break;
420
0
#endif
421
422
0
#ifdef BUILD_TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA
423
0
    case TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA :
424
0
        specs->bulk_cipher_algorithm = wolfssl_aes;
425
0
        specs->cipher_type           = block;
426
0
        specs->mac_algorithm         = sha_mac;
427
0
        specs->kea                   = ecc_diffie_hellman_kea;
428
0
        specs->sig_algo              = rsa_sa_algo;
429
0
        specs->hash_size             = WC_SHA_DIGEST_SIZE;
430
0
        specs->pad_size              = PAD_SHA;
431
0
        specs->static_ecdh           = 0;
432
0
        specs->key_size              = AES_256_KEY_SIZE;
433
0
        specs->block_size            = WC_AES_BLOCK_SIZE;
434
0
        specs->iv_size               = AES_IV_SIZE;
435
436
0
        break;
437
0
#endif
438
439
0
#ifdef BUILD_TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256
440
0
    case TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 :
441
0
        specs->bulk_cipher_algorithm = wolfssl_aes_gcm;
442
0
        specs->cipher_type           = aead;
443
0
        specs->mac_algorithm         = sha256_mac;
444
0
        specs->kea                   = ecc_diffie_hellman_kea;
445
0
        specs->sig_algo              = rsa_sa_algo;
446
0
        specs->hash_size             = WC_SHA256_DIGEST_SIZE;
447
0
        specs->pad_size              = PAD_SHA;
448
0
        specs->static_ecdh           = 0;
449
0
        specs->key_size              = AES_128_KEY_SIZE;
450
0
        specs->block_size            = WC_AES_BLOCK_SIZE;
451
0
        specs->iv_size               = AESGCM_IMP_IV_SZ;
452
0
        specs->aead_mac_size         = AES_GCM_AUTH_SZ;
453
454
0
        break;
455
0
#endif
456
457
0
#ifdef BUILD_TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384
458
0
    case TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 :
459
0
        specs->bulk_cipher_algorithm = wolfssl_aes_gcm;
460
0
        specs->cipher_type           = aead;
461
0
        specs->mac_algorithm         = sha384_mac;
462
0
        specs->kea                   = ecc_diffie_hellman_kea;
463
0
        specs->sig_algo              = rsa_sa_algo;
464
0
        specs->hash_size             = WC_SHA384_DIGEST_SIZE;
465
0
        specs->pad_size              = PAD_SHA;
466
0
        specs->static_ecdh           = 0;
467
0
        specs->key_size              = AES_256_KEY_SIZE;
468
0
        specs->block_size            = WC_AES_BLOCK_SIZE;
469
0
        specs->iv_size               = AESGCM_IMP_IV_SZ;
470
0
        specs->aead_mac_size         = AES_GCM_AUTH_SZ;
471
472
0
        break;
473
0
#endif
474
475
#ifdef BUILD_TLS_ECDHE_PSK_WITH_NULL_SHA256
476
    case TLS_ECDHE_PSK_WITH_NULL_SHA256 :
477
        specs->bulk_cipher_algorithm = wolfssl_cipher_null;
478
        specs->cipher_type           = stream;
479
        specs->mac_algorithm         = sha256_mac;
480
        specs->kea                   = ecdhe_psk_kea;
481
        specs->sig_algo              = anonymous_sa_algo;
482
        specs->hash_size             = WC_SHA256_DIGEST_SIZE;
483
        specs->pad_size              = PAD_SHA;
484
        specs->static_ecdh           = 0;
485
        specs->key_size              = 0;
486
        specs->block_size            = 0;
487
        specs->iv_size               = 0;
488
489
        if (opts != NULL)
490
            opts->usingPSK_cipher    = 1;
491
        break;
492
#endif
493
494
#ifdef BUILD_TLS_ECDHE_PSK_WITH_AES_128_CBC_SHA256
495
    case TLS_ECDHE_PSK_WITH_AES_128_CBC_SHA256 :
496
        specs->bulk_cipher_algorithm = wolfssl_aes;
497
        specs->cipher_type           = block;
498
        specs->mac_algorithm         = sha256_mac;
499
        specs->kea                   = ecdhe_psk_kea;
500
        specs->sig_algo              = anonymous_sa_algo;
501
        specs->hash_size             = WC_SHA256_DIGEST_SIZE;
502
        specs->pad_size              = PAD_SHA;
503
        specs->static_ecdh           = 0;
504
        specs->key_size              = AES_128_KEY_SIZE;
505
        specs->block_size            = WC_AES_BLOCK_SIZE;
506
        specs->iv_size               = AES_IV_SIZE;
507
508
        if (opts != NULL)
509
            opts->usingPSK_cipher    = 1;
510
        if (opts != NULL)
511
            opts->usingPSK_cipher    = 1;
512
        break;
513
#endif
514
515
0
#endif /* HAVE_ECC || HAVE_CURVE25519 || HAVE_CURVE448 */
516
517
0
#if defined(HAVE_ECC) || (defined(HAVE_CURVE25519) && defined(HAVE_ED25519)) \
518
0
                      || (defined(HAVE_CURVE448) && defined(HAVE_ED448))
519
520
0
#ifdef BUILD_TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256
521
0
    case TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256 :
522
0
        specs->bulk_cipher_algorithm = wolfssl_aes;
523
0
        specs->cipher_type           = block;
524
0
        specs->mac_algorithm         = sha256_mac;
525
0
        specs->kea                   = ecc_diffie_hellman_kea;
526
0
        specs->sig_algo              = ecc_dsa_sa_algo;
527
0
        specs->hash_size             = WC_SHA256_DIGEST_SIZE;
528
0
        specs->pad_size              = PAD_SHA;
529
0
        specs->static_ecdh           = 0;
530
0
        specs->key_size              = AES_128_KEY_SIZE;
531
0
        specs->iv_size               = AES_IV_SIZE;
532
0
        specs->block_size            = WC_AES_BLOCK_SIZE;
533
534
0
        break;
535
0
#endif
536
537
0
#ifdef BUILD_TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384
538
0
    case TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384 :
539
0
        specs->bulk_cipher_algorithm = wolfssl_aes;
540
0
        specs->cipher_type           = block;
541
0
        specs->mac_algorithm         = sha384_mac;
542
0
        specs->kea                   = ecc_diffie_hellman_kea;
543
0
        specs->sig_algo              = ecc_dsa_sa_algo;
544
0
        specs->hash_size             = WC_SHA384_DIGEST_SIZE;
545
0
        specs->pad_size              = PAD_SHA;
546
0
        specs->static_ecdh           = 0;
547
0
        specs->key_size              = AES_256_KEY_SIZE;
548
0
        specs->iv_size               = AES_IV_SIZE;
549
0
        specs->block_size            = WC_AES_BLOCK_SIZE;
550
551
0
        break;
552
0
#endif
553
554
#ifdef BUILD_TLS_ECDHE_ECDSA_WITH_3DES_EDE_CBC_SHA
555
    case TLS_ECDHE_ECDSA_WITH_3DES_EDE_CBC_SHA :
556
        specs->bulk_cipher_algorithm = wolfssl_triple_des;
557
        specs->cipher_type           = block;
558
        specs->mac_algorithm         = sha_mac;
559
        specs->kea                   = ecc_diffie_hellman_kea;
560
        specs->sig_algo              = ecc_dsa_sa_algo;
561
        specs->hash_size             = WC_SHA_DIGEST_SIZE;
562
        specs->pad_size              = PAD_SHA;
563
        specs->static_ecdh           = 0;
564
        specs->key_size              = DES3_KEY_SIZE;
565
        specs->block_size            = DES_BLOCK_SIZE;
566
#if defined(HAVE_FIPS) && defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION == 2)
567
        specs->iv_size               = DES_BLOCK_SIZE;
568
#else
569
        specs->iv_size               = DES_IV_SIZE;
570
#endif
571
572
        break;
573
#endif
574
575
0
#ifdef BUILD_TLS_ECDHE_ECDSA_WITH_RC4_128_SHA
576
0
    case TLS_ECDHE_ECDSA_WITH_RC4_128_SHA :
577
0
        specs->bulk_cipher_algorithm = wolfssl_rc4;
578
0
        specs->cipher_type           = stream;
579
0
        specs->mac_algorithm         = sha_mac;
580
0
        specs->kea                   = ecc_diffie_hellman_kea;
581
0
        specs->sig_algo              = ecc_dsa_sa_algo;
582
0
        specs->hash_size             = WC_SHA_DIGEST_SIZE;
583
0
        specs->pad_size              = PAD_SHA;
584
0
        specs->static_ecdh           = 0;
585
0
        specs->key_size              = RC4_KEY_SIZE;
586
0
        specs->iv_size               = 0;
587
0
        specs->block_size            = 0;
588
589
0
        break;
590
0
#endif
591
592
0
#ifdef BUILD_TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA
593
0
    case TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA :
594
0
        specs->bulk_cipher_algorithm = wolfssl_aes;
595
0
        specs->cipher_type           = block;
596
0
        specs->mac_algorithm         = sha_mac;
597
0
        specs->kea                   = ecc_diffie_hellman_kea;
598
0
        specs->sig_algo              = ecc_dsa_sa_algo;
599
0
        specs->hash_size             = WC_SHA_DIGEST_SIZE;
600
0
        specs->pad_size              = PAD_SHA;
601
0
        specs->static_ecdh           = 0;
602
0
        specs->key_size              = AES_128_KEY_SIZE;
603
0
        specs->block_size            = WC_AES_BLOCK_SIZE;
604
0
        specs->iv_size               = AES_IV_SIZE;
605
606
0
        break;
607
0
#endif
608
609
0
#ifdef BUILD_TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA
610
0
    case TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA :
611
0
        specs->bulk_cipher_algorithm = wolfssl_aes;
612
0
        specs->cipher_type           = block;
613
0
        specs->mac_algorithm         = sha_mac;
614
0
        specs->kea                   = ecc_diffie_hellman_kea;
615
0
        specs->sig_algo              = ecc_dsa_sa_algo;
616
0
        specs->hash_size             = WC_SHA_DIGEST_SIZE;
617
0
        specs->pad_size              = PAD_SHA;
618
0
        specs->static_ecdh           = 0;
619
0
        specs->key_size              = AES_256_KEY_SIZE;
620
0
        specs->block_size            = WC_AES_BLOCK_SIZE;
621
0
        specs->iv_size               = AES_IV_SIZE;
622
623
0
        break;
624
0
#endif
625
626
0
#ifdef BUILD_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256
627
0
    case TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256 :
628
0
        specs->bulk_cipher_algorithm = wolfssl_aes_gcm;
629
0
        specs->cipher_type           = aead;
630
0
        specs->mac_algorithm         = sha256_mac;
631
0
        specs->kea                   = ecc_diffie_hellman_kea;
632
0
        specs->sig_algo              = ecc_dsa_sa_algo;
633
0
        specs->hash_size             = WC_SHA256_DIGEST_SIZE;
634
0
        specs->pad_size              = PAD_SHA;
635
0
        specs->static_ecdh           = 0;
636
0
        specs->key_size              = AES_128_KEY_SIZE;
637
0
        specs->block_size            = WC_AES_BLOCK_SIZE;
638
0
        specs->iv_size               = AESGCM_IMP_IV_SZ;
639
0
        specs->aead_mac_size         = AES_GCM_AUTH_SZ;
640
641
0
        break;
642
0
#endif
643
644
0
#ifdef BUILD_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384
645
0
    case TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384 :
646
0
        specs->bulk_cipher_algorithm = wolfssl_aes_gcm;
647
0
        specs->cipher_type           = aead;
648
0
        specs->mac_algorithm         = sha384_mac;
649
0
        specs->kea                   = ecc_diffie_hellman_kea;
650
0
        specs->sig_algo              = ecc_dsa_sa_algo;
651
0
        specs->hash_size             = WC_SHA384_DIGEST_SIZE;
652
0
        specs->pad_size              = PAD_SHA;
653
0
        specs->static_ecdh           = 0;
654
0
        specs->key_size              = AES_256_KEY_SIZE;
655
0
        specs->block_size            = WC_AES_BLOCK_SIZE;
656
0
        specs->iv_size               = AESGCM_IMP_IV_SZ;
657
0
        specs->aead_mac_size         = AES_GCM_AUTH_SZ;
658
659
0
        break;
660
0
#endif
661
662
0
#ifdef BUILD_TLS_ECDHE_ECDSA_WITH_AES_128_CCM
663
0
    case TLS_ECDHE_ECDSA_WITH_AES_128_CCM :
664
0
        specs->bulk_cipher_algorithm = wolfssl_aes_ccm;
665
0
        specs->cipher_type           = aead;
666
0
        specs->mac_algorithm         = sha256_mac;
667
0
        specs->kea                   = ecc_diffie_hellman_kea;
668
0
        specs->sig_algo              = ecc_dsa_sa_algo;
669
0
        specs->hash_size             = WC_SHA256_DIGEST_SIZE;
670
0
        specs->pad_size              = PAD_SHA;
671
0
        specs->static_ecdh           = 0;
672
0
        specs->key_size              = AES_128_KEY_SIZE;
673
0
        specs->block_size            = WC_AES_BLOCK_SIZE;
674
0
        specs->iv_size               = AESCCM_IMP_IV_SZ;
675
0
        specs->aead_mac_size         = AES_CCM_16_AUTH_SZ;
676
677
0
        break;
678
0
#endif
679
680
0
#ifdef BUILD_TLS_ECDHE_ECDSA_WITH_AES_128_CCM_8
681
0
    case TLS_ECDHE_ECDSA_WITH_AES_128_CCM_8 :
682
0
        specs->bulk_cipher_algorithm = wolfssl_aes_ccm;
683
0
        specs->cipher_type           = aead;
684
0
        specs->mac_algorithm         = sha256_mac;
685
0
        specs->kea                   = ecc_diffie_hellman_kea;
686
0
        specs->sig_algo              = ecc_dsa_sa_algo;
687
0
        specs->hash_size             = WC_SHA256_DIGEST_SIZE;
688
0
        specs->pad_size              = PAD_SHA;
689
0
        specs->static_ecdh           = 0;
690
0
        specs->key_size              = AES_128_KEY_SIZE;
691
0
        specs->block_size            = WC_AES_BLOCK_SIZE;
692
0
        specs->iv_size               = AESCCM_IMP_IV_SZ;
693
0
        specs->aead_mac_size         = AES_CCM_8_AUTH_SZ;
694
695
0
        break;
696
0
#endif
697
698
0
#ifdef BUILD_TLS_ECDHE_ECDSA_WITH_AES_256_CCM_8
699
0
    case TLS_ECDHE_ECDSA_WITH_AES_256_CCM_8 :
700
0
        specs->bulk_cipher_algorithm = wolfssl_aes_ccm;
701
0
        specs->cipher_type           = aead;
702
0
        specs->mac_algorithm         = sha256_mac;
703
0
        specs->kea                   = ecc_diffie_hellman_kea;
704
0
        specs->sig_algo              = ecc_dsa_sa_algo;
705
0
        specs->hash_size             = WC_SHA256_DIGEST_SIZE;
706
0
        specs->pad_size              = PAD_SHA;
707
0
        specs->static_ecdh           = 0;
708
0
        specs->key_size              = AES_256_KEY_SIZE;
709
0
        specs->block_size            = WC_AES_BLOCK_SIZE;
710
0
        specs->iv_size               = AESCCM_IMP_IV_SZ;
711
0
        specs->aead_mac_size         = AES_CCM_8_AUTH_SZ;
712
713
0
        break;
714
0
#endif
715
716
#ifdef BUILD_TLS_ECDHE_ECDSA_WITH_NULL_SHA
717
    case TLS_ECDHE_ECDSA_WITH_NULL_SHA :
718
        specs->bulk_cipher_algorithm = wolfssl_cipher_null;
719
        specs->cipher_type           = stream;
720
        specs->mac_algorithm         = sha_mac;
721
        specs->kea                   = ecc_diffie_hellman_kea;
722
        specs->sig_algo              = ecc_dsa_sa_algo;
723
        specs->hash_size             = WC_SHA_DIGEST_SIZE;
724
        specs->pad_size              = PAD_SHA;
725
        specs->static_ecdh           = 0;
726
        specs->key_size              = 0;
727
        specs->block_size            = 0;
728
        specs->iv_size               = 0;
729
730
    break;
731
#endif
732
733
0
#endif /* HAVE_ECC || (CURVE25519 && ED25519) || (CURVE448 && ED448) */
734
735
0
#if defined(HAVE_ECC)
736
737
#ifdef BUILD_TLS_ECDH_RSA_WITH_AES_128_CBC_SHA256
738
    case TLS_ECDH_RSA_WITH_AES_128_CBC_SHA256 :
739
        specs->bulk_cipher_algorithm = wolfssl_aes;
740
        specs->cipher_type           = block;
741
        specs->mac_algorithm         = sha256_mac;
742
        specs->kea                   = ecc_diffie_hellman_kea;
743
        specs->sig_algo              = rsa_sa_algo;
744
        specs->hash_size             = WC_SHA256_DIGEST_SIZE;
745
        specs->pad_size              = PAD_SHA;
746
        specs->static_ecdh           = 1;
747
        specs->key_size              = AES_128_KEY_SIZE;
748
        specs->iv_size               = AES_IV_SIZE;
749
        specs->block_size            = WC_AES_BLOCK_SIZE;
750
751
        break;
752
#endif
753
754
#ifdef BUILD_TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA256
755
    case TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA256 :
756
        specs->bulk_cipher_algorithm = wolfssl_aes;
757
        specs->cipher_type           = block;
758
        specs->mac_algorithm         = sha256_mac;
759
        specs->kea                   = ecc_diffie_hellman_kea;
760
        specs->sig_algo              = ecc_dsa_sa_algo;
761
        specs->hash_size             = WC_SHA256_DIGEST_SIZE;
762
        specs->pad_size              = PAD_SHA;
763
        specs->static_ecdh           = 1;
764
        specs->key_size              = AES_128_KEY_SIZE;
765
        specs->iv_size               = AES_IV_SIZE;
766
        specs->block_size            = WC_AES_BLOCK_SIZE;
767
768
        break;
769
#endif
770
771
#ifdef BUILD_TLS_ECDH_RSA_WITH_AES_256_CBC_SHA384
772
    case TLS_ECDH_RSA_WITH_AES_256_CBC_SHA384 :
773
        specs->bulk_cipher_algorithm = wolfssl_aes;
774
        specs->cipher_type           = block;
775
        specs->mac_algorithm         = sha384_mac;
776
        specs->kea                   = ecc_diffie_hellman_kea;
777
        specs->sig_algo              = rsa_sa_algo;
778
        specs->hash_size             = WC_SHA384_DIGEST_SIZE;
779
        specs->pad_size              = PAD_SHA;
780
        specs->static_ecdh           = 1;
781
        specs->key_size              = AES_256_KEY_SIZE;
782
        specs->iv_size               = AES_IV_SIZE;
783
        specs->block_size            = WC_AES_BLOCK_SIZE;
784
785
        break;
786
#endif
787
788
#ifdef BUILD_TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA384
789
    case TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA384 :
790
        specs->bulk_cipher_algorithm = wolfssl_aes;
791
        specs->cipher_type           = block;
792
        specs->mac_algorithm         = sha384_mac;
793
        specs->kea                   = ecc_diffie_hellman_kea;
794
        specs->sig_algo              = ecc_dsa_sa_algo;
795
        specs->hash_size             = WC_SHA384_DIGEST_SIZE;
796
        specs->pad_size              = PAD_SHA;
797
        specs->static_ecdh           = 1;
798
        specs->key_size              = AES_256_KEY_SIZE;
799
        specs->iv_size               = AES_IV_SIZE;
800
        specs->block_size            = WC_AES_BLOCK_SIZE;
801
802
        break;
803
#endif
804
805
#ifdef BUILD_TLS_ECDH_RSA_WITH_AES_128_CBC_SHA
806
    case TLS_ECDH_RSA_WITH_AES_128_CBC_SHA :
807
        specs->bulk_cipher_algorithm = wolfssl_aes;
808
        specs->cipher_type           = block;
809
        specs->mac_algorithm         = sha_mac;
810
        specs->kea                   = ecc_diffie_hellman_kea;
811
        specs->sig_algo              = rsa_sa_algo;
812
        specs->hash_size             = WC_SHA_DIGEST_SIZE;
813
        specs->pad_size              = PAD_SHA;
814
        specs->static_ecdh           = 1;
815
        specs->key_size              = AES_128_KEY_SIZE;
816
        specs->block_size            = WC_AES_BLOCK_SIZE;
817
        specs->iv_size               = AES_IV_SIZE;
818
819
        break;
820
#endif
821
822
#ifdef BUILD_TLS_ECDH_RSA_WITH_3DES_EDE_CBC_SHA
823
    case TLS_ECDH_RSA_WITH_3DES_EDE_CBC_SHA :
824
        specs->bulk_cipher_algorithm = wolfssl_triple_des;
825
        specs->cipher_type           = block;
826
        specs->mac_algorithm         = sha_mac;
827
        specs->kea                   = ecc_diffie_hellman_kea;
828
        specs->sig_algo              = rsa_sa_algo;
829
        specs->hash_size             = WC_SHA_DIGEST_SIZE;
830
        specs->pad_size              = PAD_SHA;
831
        specs->static_ecdh           = 1;
832
        specs->key_size              = DES3_KEY_SIZE;
833
        specs->block_size            = DES_BLOCK_SIZE;
834
#if defined(HAVE_FIPS) && defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION == 2)
835
        specs->iv_size               = DES_BLOCK_SIZE;
836
#else
837
        specs->iv_size               = DES_IV_SIZE;
838
#endif
839
840
        break;
841
#endif
842
843
#ifdef BUILD_TLS_ECDH_RSA_WITH_RC4_128_SHA
844
    case TLS_ECDH_RSA_WITH_RC4_128_SHA :
845
        specs->bulk_cipher_algorithm = wolfssl_rc4;
846
        specs->cipher_type           = stream;
847
        specs->mac_algorithm         = sha_mac;
848
        specs->kea                   = ecc_diffie_hellman_kea;
849
        specs->sig_algo              = rsa_sa_algo;
850
        specs->hash_size             = WC_SHA_DIGEST_SIZE;
851
        specs->pad_size              = PAD_SHA;
852
        specs->static_ecdh           = 1;
853
        specs->key_size              = RC4_KEY_SIZE;
854
        specs->iv_size               = 0;
855
        specs->block_size            = 0;
856
857
        break;
858
#endif
859
860
#ifdef BUILD_TLS_ECDH_ECDSA_WITH_3DES_EDE_CBC_SHA
861
    case TLS_ECDH_ECDSA_WITH_3DES_EDE_CBC_SHA :
862
        specs->bulk_cipher_algorithm = wolfssl_triple_des;
863
        specs->cipher_type           = block;
864
        specs->mac_algorithm         = sha_mac;
865
        specs->kea                   = ecc_diffie_hellman_kea;
866
        specs->sig_algo              = ecc_dsa_sa_algo;
867
        specs->hash_size             = WC_SHA_DIGEST_SIZE;
868
        specs->pad_size              = PAD_SHA;
869
        specs->static_ecdh           = 1;
870
        specs->key_size              = DES3_KEY_SIZE;
871
        specs->block_size            = DES_BLOCK_SIZE;
872
#if defined(HAVE_FIPS) && defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION == 2)
873
        specs->iv_size               = DES_BLOCK_SIZE;
874
#else
875
        specs->iv_size               = DES_IV_SIZE;
876
#endif
877
878
        break;
879
#endif
880
881
#ifdef BUILD_TLS_ECDH_ECDSA_WITH_RC4_128_SHA
882
    case TLS_ECDH_ECDSA_WITH_RC4_128_SHA :
883
        specs->bulk_cipher_algorithm = wolfssl_rc4;
884
        specs->cipher_type           = stream;
885
        specs->mac_algorithm         = sha_mac;
886
        specs->kea                   = ecc_diffie_hellman_kea;
887
        specs->sig_algo              = ecc_dsa_sa_algo;
888
        specs->hash_size             = WC_SHA_DIGEST_SIZE;
889
        specs->pad_size              = PAD_SHA;
890
        specs->static_ecdh           = 1;
891
        specs->key_size              = RC4_KEY_SIZE;
892
        specs->iv_size               = 0;
893
        specs->block_size            = 0;
894
895
        break;
896
#endif
897
898
#ifdef BUILD_TLS_ECDH_RSA_WITH_AES_256_CBC_SHA
899
    case TLS_ECDH_RSA_WITH_AES_256_CBC_SHA :
900
        specs->bulk_cipher_algorithm = wolfssl_aes;
901
        specs->cipher_type           = block;
902
        specs->mac_algorithm         = sha_mac;
903
        specs->kea                   = ecc_diffie_hellman_kea;
904
        specs->sig_algo              = rsa_sa_algo;
905
        specs->hash_size             = WC_SHA_DIGEST_SIZE;
906
        specs->pad_size              = PAD_SHA;
907
        specs->static_ecdh           = 1;
908
        specs->key_size              = AES_256_KEY_SIZE;
909
        specs->block_size            = WC_AES_BLOCK_SIZE;
910
        specs->iv_size               = AES_IV_SIZE;
911
912
        break;
913
#endif
914
915
#ifdef BUILD_TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA
916
    case TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA :
917
        specs->bulk_cipher_algorithm = wolfssl_aes;
918
        specs->cipher_type           = block;
919
        specs->mac_algorithm         = sha_mac;
920
        specs->kea                   = ecc_diffie_hellman_kea;
921
        specs->sig_algo              = ecc_dsa_sa_algo;
922
        specs->hash_size             = WC_SHA_DIGEST_SIZE;
923
        specs->pad_size              = PAD_SHA;
924
        specs->static_ecdh           = 1;
925
        specs->key_size              = AES_128_KEY_SIZE;
926
        specs->block_size            = WC_AES_BLOCK_SIZE;
927
        specs->iv_size               = AES_IV_SIZE;
928
929
        break;
930
#endif
931
932
#ifdef BUILD_TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA
933
    case TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA :
934
        specs->bulk_cipher_algorithm = wolfssl_aes;
935
        specs->cipher_type           = block;
936
        specs->mac_algorithm         = sha_mac;
937
        specs->kea                   = ecc_diffie_hellman_kea;
938
        specs->sig_algo              = ecc_dsa_sa_algo;
939
        specs->hash_size             = WC_SHA_DIGEST_SIZE;
940
        specs->pad_size              = PAD_SHA;
941
        specs->static_ecdh           = 1;
942
        specs->key_size              = AES_256_KEY_SIZE;
943
        specs->block_size            = WC_AES_BLOCK_SIZE;
944
        specs->iv_size               = AES_IV_SIZE;
945
946
        break;
947
#endif
948
949
#ifdef BUILD_TLS_ECDH_RSA_WITH_AES_128_GCM_SHA256
950
    case TLS_ECDH_RSA_WITH_AES_128_GCM_SHA256 :
951
        specs->bulk_cipher_algorithm = wolfssl_aes_gcm;
952
        specs->cipher_type           = aead;
953
        specs->mac_algorithm         = sha256_mac;
954
        specs->kea                   = ecc_diffie_hellman_kea;
955
        specs->sig_algo              = rsa_sa_algo;
956
        specs->hash_size             = WC_SHA256_DIGEST_SIZE;
957
        specs->pad_size              = PAD_SHA;
958
        specs->static_ecdh           = 1;
959
        specs->key_size              = AES_128_KEY_SIZE;
960
        specs->block_size            = WC_AES_BLOCK_SIZE;
961
        specs->iv_size               = AESGCM_IMP_IV_SZ;
962
        specs->aead_mac_size         = AES_GCM_AUTH_SZ;
963
964
        break;
965
#endif
966
967
#ifdef BUILD_TLS_ECDH_RSA_WITH_AES_256_GCM_SHA384
968
    case TLS_ECDH_RSA_WITH_AES_256_GCM_SHA384 :
969
        specs->bulk_cipher_algorithm = wolfssl_aes_gcm;
970
        specs->cipher_type           = aead;
971
        specs->mac_algorithm         = sha384_mac;
972
        specs->kea                   = ecc_diffie_hellman_kea;
973
        specs->sig_algo              = rsa_sa_algo;
974
        specs->hash_size             = WC_SHA384_DIGEST_SIZE;
975
        specs->pad_size              = PAD_SHA;
976
        specs->static_ecdh           = 1;
977
        specs->key_size              = AES_256_KEY_SIZE;
978
        specs->block_size            = WC_AES_BLOCK_SIZE;
979
        specs->iv_size               = AESGCM_IMP_IV_SZ;
980
        specs->aead_mac_size         = AES_GCM_AUTH_SZ;
981
982
        break;
983
#endif
984
985
#ifdef BUILD_TLS_ECDH_ECDSA_WITH_AES_128_GCM_SHA256
986
    case TLS_ECDH_ECDSA_WITH_AES_128_GCM_SHA256 :
987
        specs->bulk_cipher_algorithm = wolfssl_aes_gcm;
988
        specs->cipher_type           = aead;
989
        specs->mac_algorithm         = sha256_mac;
990
        specs->kea                   = ecc_diffie_hellman_kea;
991
        specs->sig_algo              = ecc_dsa_sa_algo;
992
        specs->hash_size             = WC_SHA256_DIGEST_SIZE;
993
        specs->pad_size              = PAD_SHA;
994
        specs->static_ecdh           = 1;
995
        specs->key_size              = AES_128_KEY_SIZE;
996
        specs->block_size            = WC_AES_BLOCK_SIZE;
997
        specs->iv_size               = AESGCM_IMP_IV_SZ;
998
        specs->aead_mac_size         = AES_GCM_AUTH_SZ;
999
1000
        break;
1001
#endif
1002
1003
#ifdef BUILD_TLS_ECDH_ECDSA_WITH_AES_256_GCM_SHA384
1004
    case TLS_ECDH_ECDSA_WITH_AES_256_GCM_SHA384 :
1005
        specs->bulk_cipher_algorithm = wolfssl_aes_gcm;
1006
        specs->cipher_type           = aead;
1007
        specs->mac_algorithm         = sha384_mac;
1008
        specs->kea                   = ecc_diffie_hellman_kea;
1009
        specs->sig_algo              = ecc_dsa_sa_algo;
1010
        specs->hash_size             = WC_SHA384_DIGEST_SIZE;
1011
        specs->pad_size              = PAD_SHA;
1012
        specs->static_ecdh           = 1;
1013
        specs->key_size              = AES_256_KEY_SIZE;
1014
        specs->block_size            = WC_AES_BLOCK_SIZE;
1015
        specs->iv_size               = AESGCM_IMP_IV_SZ;
1016
        specs->aead_mac_size         = AES_GCM_AUTH_SZ;
1017
1018
        break;
1019
#endif
1020
1021
#ifdef BUILD_TLS_ECDHE_ECDSA_WITH_ARIA_128_GCM_SHA256
1022
    case TLS_ECDHE_ECDSA_WITH_ARIA_128_GCM_SHA256 :
1023
        specs->bulk_cipher_algorithm = wolfssl_aria_gcm;
1024
        specs->cipher_type           = aead;
1025
        specs->mac_algorithm         = sha256_mac;
1026
        specs->kea                   = ecc_diffie_hellman_kea;
1027
        specs->sig_algo              = ecc_dsa_sa_algo;
1028
        specs->hash_size             = WC_SHA256_DIGEST_SIZE;
1029
        specs->pad_size              = PAD_SHA;
1030
        specs->static_ecdh           = 0;
1031
        specs->key_size              = ARIA_128_KEY_SIZE;
1032
        specs->block_size            = ARIA_BLOCK_SIZE;
1033
        specs->iv_size               = AESGCM_IMP_IV_SZ;
1034
        specs->aead_mac_size         = ARIA_GCM_AUTH_SZ;
1035
1036
        break;
1037
#endif
1038
1039
#ifdef BUILD_TLS_ECDHE_ECDSA_WITH_ARIA_256_GCM_SHA384
1040
    case TLS_ECDHE_ECDSA_WITH_ARIA_256_GCM_SHA384 :
1041
        specs->bulk_cipher_algorithm = wolfssl_aria_gcm;
1042
        specs->cipher_type           = aead;
1043
        specs->mac_algorithm         = sha384_mac;
1044
        specs->kea                   = ecc_diffie_hellman_kea;
1045
        specs->sig_algo              = ecc_dsa_sa_algo;
1046
        specs->hash_size             = WC_SHA384_DIGEST_SIZE;
1047
        specs->pad_size              = PAD_SHA;
1048
        specs->static_ecdh           = 0;
1049
        specs->key_size              = ARIA_256_KEY_SIZE;
1050
        specs->block_size            = ARIA_BLOCK_SIZE;
1051
        specs->iv_size               = AESGCM_IMP_IV_SZ;
1052
        specs->aead_mac_size         = ARIA_GCM_AUTH_SZ;
1053
1054
        break;
1055
#endif
1056
1057
0
#endif /* HAVE_ECC */
1058
1059
#ifdef BUILD_TLS_RSA_WITH_AES_128_CCM_8
1060
    case TLS_RSA_WITH_AES_128_CCM_8 :
1061
        specs->bulk_cipher_algorithm = wolfssl_aes_ccm;
1062
        specs->cipher_type           = aead;
1063
        specs->mac_algorithm         = sha256_mac;
1064
        specs->kea                   = rsa_kea;
1065
        specs->sig_algo              = rsa_sa_algo;
1066
        specs->hash_size             = WC_SHA256_DIGEST_SIZE;
1067
        specs->pad_size              = PAD_SHA;
1068
        specs->static_ecdh           = 0;
1069
        specs->key_size              = AES_128_KEY_SIZE;
1070
        specs->block_size            = WC_AES_BLOCK_SIZE;
1071
        specs->iv_size               = AESCCM_IMP_IV_SZ;
1072
        specs->aead_mac_size         = AES_CCM_8_AUTH_SZ;
1073
1074
        break;
1075
#endif
1076
1077
#ifdef BUILD_TLS_RSA_WITH_AES_256_CCM_8
1078
    case TLS_RSA_WITH_AES_256_CCM_8 :
1079
        specs->bulk_cipher_algorithm = wolfssl_aes_ccm;
1080
        specs->cipher_type           = aead;
1081
        specs->mac_algorithm         = sha256_mac;
1082
        specs->kea                   = rsa_kea;
1083
        specs->sig_algo              = rsa_sa_algo;
1084
        specs->hash_size             = WC_SHA256_DIGEST_SIZE;
1085
        specs->pad_size              = PAD_SHA;
1086
        specs->static_ecdh           = 0;
1087
        specs->key_size              = AES_256_KEY_SIZE;
1088
        specs->block_size            = WC_AES_BLOCK_SIZE;
1089
        specs->iv_size               = AESCCM_IMP_IV_SZ;
1090
        specs->aead_mac_size         = AES_CCM_8_AUTH_SZ;
1091
1092
        break;
1093
#endif
1094
1095
#ifdef BUILD_TLS_PSK_WITH_AES_128_CCM_8
1096
    case TLS_PSK_WITH_AES_128_CCM_8 :
1097
        specs->bulk_cipher_algorithm = wolfssl_aes_ccm;
1098
        specs->cipher_type           = aead;
1099
        specs->mac_algorithm         = sha256_mac;
1100
        specs->kea                   = psk_kea;
1101
        specs->sig_algo              = anonymous_sa_algo;
1102
        specs->hash_size             = WC_SHA256_DIGEST_SIZE;
1103
        specs->pad_size              = PAD_SHA;
1104
        specs->static_ecdh           = 0;
1105
        specs->key_size              = AES_128_KEY_SIZE;
1106
        specs->block_size            = WC_AES_BLOCK_SIZE;
1107
        specs->iv_size               = AESCCM_IMP_IV_SZ;
1108
        specs->aead_mac_size         = AES_CCM_8_AUTH_SZ;
1109
1110
        if (opts != NULL)
1111
            opts->usingPSK_cipher    = 1;
1112
        break;
1113
#endif
1114
1115
#ifdef BUILD_TLS_PSK_WITH_AES_256_CCM_8
1116
    case TLS_PSK_WITH_AES_256_CCM_8 :
1117
        specs->bulk_cipher_algorithm = wolfssl_aes_ccm;
1118
        specs->cipher_type           = aead;
1119
        specs->mac_algorithm         = sha256_mac;
1120
        specs->kea                   = psk_kea;
1121
        specs->sig_algo              = anonymous_sa_algo;
1122
        specs->hash_size             = WC_SHA256_DIGEST_SIZE;
1123
        specs->pad_size              = PAD_SHA;
1124
        specs->static_ecdh           = 0;
1125
        specs->key_size              = AES_256_KEY_SIZE;
1126
        specs->block_size            = WC_AES_BLOCK_SIZE;
1127
        specs->iv_size               = AESCCM_IMP_IV_SZ;
1128
        specs->aead_mac_size         = AES_CCM_8_AUTH_SZ;
1129
1130
        if (opts != NULL)
1131
            opts->usingPSK_cipher    = 1;
1132
        break;
1133
#endif
1134
1135
#ifdef BUILD_TLS_PSK_WITH_AES_128_CCM
1136
    case TLS_PSK_WITH_AES_128_CCM :
1137
        specs->bulk_cipher_algorithm = wolfssl_aes_ccm;
1138
        specs->cipher_type           = aead;
1139
        specs->mac_algorithm         = sha256_mac;
1140
        specs->kea                   = psk_kea;
1141
        specs->sig_algo              = anonymous_sa_algo;
1142
        specs->hash_size             = WC_SHA256_DIGEST_SIZE;
1143
        specs->pad_size              = PAD_SHA;
1144
        specs->static_ecdh           = 0;
1145
        specs->key_size              = AES_128_KEY_SIZE;
1146
        specs->block_size            = WC_AES_BLOCK_SIZE;
1147
        specs->iv_size               = AESCCM_IMP_IV_SZ;
1148
        specs->aead_mac_size         = AES_CCM_16_AUTH_SZ;
1149
1150
        if (opts != NULL)
1151
            opts->usingPSK_cipher    = 1;
1152
        break;
1153
#endif
1154
1155
#ifdef BUILD_TLS_PSK_WITH_AES_256_CCM
1156
    case TLS_PSK_WITH_AES_256_CCM :
1157
        specs->bulk_cipher_algorithm = wolfssl_aes_ccm;
1158
        specs->cipher_type           = aead;
1159
        specs->mac_algorithm         = sha256_mac;
1160
        specs->kea                   = psk_kea;
1161
        specs->sig_algo              = anonymous_sa_algo;
1162
        specs->hash_size             = WC_SHA256_DIGEST_SIZE;
1163
        specs->pad_size              = PAD_SHA;
1164
        specs->static_ecdh           = 0;
1165
        specs->key_size              = AES_256_KEY_SIZE;
1166
        specs->block_size            = WC_AES_BLOCK_SIZE;
1167
        specs->iv_size               = AESCCM_IMP_IV_SZ;
1168
        specs->aead_mac_size         = AES_CCM_16_AUTH_SZ;
1169
1170
        if (opts != NULL)
1171
            opts->usingPSK_cipher    = 1;
1172
        break;
1173
#endif
1174
1175
#ifdef BUILD_TLS_DHE_PSK_WITH_AES_128_CCM
1176
    case TLS_DHE_PSK_WITH_AES_128_CCM :
1177
        specs->bulk_cipher_algorithm = wolfssl_aes_ccm;
1178
        specs->cipher_type           = aead;
1179
        specs->mac_algorithm         = sha256_mac;
1180
        specs->kea                   = dhe_psk_kea;
1181
        specs->sig_algo              = anonymous_sa_algo;
1182
        specs->hash_size             = WC_SHA256_DIGEST_SIZE;
1183
        specs->pad_size              = PAD_SHA;
1184
        specs->static_ecdh           = 0;
1185
        specs->key_size              = AES_128_KEY_SIZE;
1186
        specs->block_size            = WC_AES_BLOCK_SIZE;
1187
        specs->iv_size               = AESCCM_IMP_IV_SZ;
1188
        specs->aead_mac_size         = AES_CCM_16_AUTH_SZ;
1189
1190
        if (opts != NULL)
1191
            opts->usingPSK_cipher    = 1;
1192
        break;
1193
#endif
1194
1195
#ifdef BUILD_TLS_DHE_PSK_WITH_AES_256_CCM
1196
    case TLS_DHE_PSK_WITH_AES_256_CCM :
1197
        specs->bulk_cipher_algorithm = wolfssl_aes_ccm;
1198
        specs->cipher_type           = aead;
1199
        specs->mac_algorithm         = sha256_mac;
1200
        specs->kea                   = dhe_psk_kea;
1201
        specs->sig_algo              = anonymous_sa_algo;
1202
        specs->hash_size             = WC_SHA256_DIGEST_SIZE;
1203
        specs->pad_size              = PAD_SHA;
1204
        specs->static_ecdh           = 0;
1205
        specs->key_size              = AES_256_KEY_SIZE;
1206
        specs->block_size            = WC_AES_BLOCK_SIZE;
1207
        specs->iv_size               = AESCCM_IMP_IV_SZ;
1208
        specs->aead_mac_size         = AES_CCM_16_AUTH_SZ;
1209
1210
        if (opts != NULL)
1211
            opts->usingPSK_cipher    = 1;
1212
        break;
1213
#endif
1214
1215
#if defined(WOLFSSL_TLS13) && defined(HAVE_NULL_CIPHER)
1216
    #ifdef BUILD_TLS_SHA256_SHA256
1217
    case TLS_SHA256_SHA256 :
1218
        specs->bulk_cipher_algorithm = wolfssl_cipher_null;
1219
        specs->cipher_type           = aead;
1220
        specs->mac_algorithm         = sha256_mac;
1221
        specs->kea                   = 0;
1222
        specs->sig_algo              = 0;
1223
        specs->hash_size             = WC_SHA256_DIGEST_SIZE;
1224
        specs->pad_size              = PAD_SHA;
1225
        specs->static_ecdh           = 0;
1226
        specs->key_size              = WC_SHA256_DIGEST_SIZE;
1227
        specs->block_size            = 0;
1228
        specs->iv_size               = HMAC_NONCE_SZ;
1229
        specs->aead_mac_size         = WC_SHA256_DIGEST_SIZE;
1230
1231
        break;
1232
    #endif
1233
1234
    #ifdef BUILD_TLS_SHA384_SHA384
1235
    case TLS_SHA384_SHA384 :
1236
        specs->bulk_cipher_algorithm = wolfssl_cipher_null;
1237
        specs->cipher_type           = aead;
1238
        specs->mac_algorithm         = sha384_mac;
1239
        specs->kea                   = 0;
1240
        specs->sig_algo              = 0;
1241
        specs->hash_size             = WC_SHA384_DIGEST_SIZE;
1242
        specs->pad_size              = PAD_SHA;
1243
        specs->static_ecdh           = 0;
1244
        specs->key_size              = WC_SHA384_DIGEST_SIZE;
1245
        specs->block_size            = 0;
1246
        specs->iv_size               = HMAC_NONCE_SZ;
1247
        specs->aead_mac_size         = WC_SHA384_DIGEST_SIZE;
1248
1249
        break;
1250
    #endif
1251
#endif
1252
1253
0
    default:
1254
0
        WOLFSSL_MSG("Unsupported cipher suite, SetCipherSpecs ECC");
1255
0
        return UNSUPPORTED_SUITE;
1256
0
    }   /* switch */
1257
0
    }   /* if     */
1258
1259
    /* TLSi v1.3 cipher suites, 0x13 */
1260
0
    if (cipherSuite0 == TLS13_BYTE) {
1261
0
        switch (cipherSuite) {
1262
1263
0
#ifdef WOLFSSL_TLS13
1264
0
    #ifdef BUILD_TLS_AES_128_GCM_SHA256
1265
0
        case TLS_AES_128_GCM_SHA256 :
1266
0
            specs->bulk_cipher_algorithm = wolfssl_aes_gcm;
1267
0
            specs->cipher_type           = aead;
1268
0
            specs->mac_algorithm         = sha256_mac;
1269
0
            specs->kea                   = 0;
1270
0
            specs->sig_algo              = 0;
1271
0
            specs->hash_size             = WC_SHA256_DIGEST_SIZE;
1272
0
            specs->pad_size              = PAD_SHA;
1273
0
            specs->static_ecdh           = 0;
1274
0
            specs->key_size              = AES_128_KEY_SIZE;
1275
0
            specs->block_size            = WC_AES_BLOCK_SIZE;
1276
0
            specs->iv_size               = AESGCM_NONCE_SZ;
1277
0
            specs->aead_mac_size         = AES_GCM_AUTH_SZ;
1278
1279
0
            break;
1280
0
    #endif
1281
1282
0
    #ifdef BUILD_TLS_AES_256_GCM_SHA384
1283
0
        case TLS_AES_256_GCM_SHA384 :
1284
0
            specs->bulk_cipher_algorithm = wolfssl_aes_gcm;
1285
0
            specs->cipher_type           = aead;
1286
0
            specs->mac_algorithm         = sha384_mac;
1287
0
            specs->kea                   = 0;
1288
0
            specs->sig_algo              = 0;
1289
0
            specs->hash_size             = WC_SHA384_DIGEST_SIZE;
1290
0
            specs->pad_size              = PAD_SHA;
1291
0
            specs->static_ecdh           = 0;
1292
0
            specs->key_size              = AES_256_KEY_SIZE;
1293
0
            specs->block_size            = WC_AES_BLOCK_SIZE;
1294
0
            specs->iv_size               = AESGCM_NONCE_SZ;
1295
0
            specs->aead_mac_size         = AES_GCM_AUTH_SZ;
1296
1297
0
            break;
1298
0
    #endif
1299
1300
0
    #ifdef BUILD_TLS_CHACHA20_POLY1305_SHA256
1301
0
        case TLS_CHACHA20_POLY1305_SHA256 :
1302
0
            specs->bulk_cipher_algorithm = wolfssl_chacha;
1303
0
            specs->cipher_type           = aead;
1304
0
            specs->mac_algorithm         = sha256_mac;
1305
0
            specs->kea                   = 0;
1306
0
            specs->sig_algo              = 0;
1307
0
            specs->hash_size             = WC_SHA256_DIGEST_SIZE;
1308
0
            specs->pad_size              = PAD_SHA;
1309
0
            specs->static_ecdh           = 0;
1310
0
            specs->key_size              = CHACHA20_256_KEY_SIZE;
1311
0
            specs->block_size            = CHACHA20_BLOCK_SIZE;
1312
0
            specs->iv_size               = CHACHA20_IV_SIZE;
1313
0
            specs->aead_mac_size         = POLY1305_AUTH_SZ;
1314
0
            if (opts != NULL)
1315
0
                opts->oldPoly            = 0; /* use recent padding RFC */
1316
1317
0
            break;
1318
0
    #endif
1319
1320
0
    #ifdef BUILD_TLS_AES_128_CCM_SHA256
1321
0
        case TLS_AES_128_CCM_SHA256 :
1322
0
            specs->bulk_cipher_algorithm = wolfssl_aes_ccm;
1323
0
            specs->cipher_type           = aead;
1324
0
            specs->mac_algorithm         = sha256_mac;
1325
0
            specs->kea                   = 0;
1326
0
            specs->sig_algo              = 0;
1327
0
            specs->hash_size             = WC_SHA256_DIGEST_SIZE;
1328
0
            specs->pad_size              = PAD_SHA;
1329
0
            specs->static_ecdh           = 0;
1330
0
            specs->key_size              = AES_128_KEY_SIZE;
1331
0
            specs->block_size            = WC_AES_BLOCK_SIZE;
1332
0
            specs->iv_size               = AESCCM_NONCE_SZ;
1333
0
            specs->aead_mac_size         = AES_CCM_16_AUTH_SZ;
1334
1335
0
            break;
1336
0
    #endif
1337
1338
0
    #ifdef BUILD_TLS_AES_128_CCM_8_SHA256
1339
0
        case TLS_AES_128_CCM_8_SHA256 :
1340
0
            specs->bulk_cipher_algorithm = wolfssl_aes_ccm;
1341
0
            specs->cipher_type           = aead;
1342
0
            specs->mac_algorithm         = sha256_mac;
1343
0
            specs->kea                   = 0;
1344
0
            specs->sig_algo              = 0;
1345
0
            specs->hash_size             = WC_SHA256_DIGEST_SIZE;
1346
0
            specs->pad_size              = PAD_SHA;
1347
0
            specs->static_ecdh           = 0;
1348
0
            specs->key_size              = AES_128_KEY_SIZE;
1349
0
            specs->block_size            = WC_AES_BLOCK_SIZE;
1350
0
            specs->iv_size               = AESCCM_NONCE_SZ;
1351
0
            specs->aead_mac_size         = AES_CCM_8_AUTH_SZ;
1352
1353
0
            break;
1354
0
    #endif
1355
0
#endif /* WOLFSSL_TLS13 */
1356
0
        default:
1357
0
            break;
1358
0
        }
1359
0
    }
1360
1361
0
    if (cipherSuite0 == ECDHE_PSK_BYTE) {
1362
1363
0
    switch (cipherSuite) {
1364
1365
0
#if defined(HAVE_ECC) || defined(HAVE_CURVE25519) || defined(HAVE_CURVE448)
1366
#ifdef BUILD_TLS_ECDHE_PSK_WITH_AES_128_GCM_SHA256
1367
    case TLS_ECDHE_PSK_WITH_AES_128_GCM_SHA256 :
1368
        specs->bulk_cipher_algorithm = wolfssl_aes_gcm;
1369
        specs->cipher_type           = aead;
1370
        specs->mac_algorithm         = sha256_mac;
1371
        specs->kea                   = ecdhe_psk_kea;
1372
        specs->sig_algo              = anonymous_sa_algo;
1373
        specs->hash_size             = WC_SHA256_DIGEST_SIZE;
1374
        specs->pad_size              = PAD_SHA;
1375
        specs->static_ecdh           = 0;
1376
        specs->key_size              = AES_128_KEY_SIZE;
1377
        specs->block_size            = WC_AES_BLOCK_SIZE;
1378
        specs->iv_size               = AESGCM_IMP_IV_SZ;
1379
        specs->aead_mac_size         = AES_GCM_AUTH_SZ;
1380
1381
        if (opts != NULL)
1382
            opts->usingPSK_cipher    = 1;
1383
        break;
1384
#endif
1385
0
#endif
1386
1387
0
    default:
1388
0
        break;
1389
0
    }
1390
0
    }
1391
1392
0
    if (cipherSuite0 == SM_BYTE) {
1393
1394
0
    switch (cipherSuite) {
1395
0
#ifdef BUILD_TLS_ECDHE_ECDSA_WITH_SM4_CBC_SM3
1396
0
    case TLS_ECDHE_ECDSA_WITH_SM4_CBC_SM3 :
1397
0
        specs->bulk_cipher_algorithm = wolfssl_sm4_cbc;
1398
0
        specs->cipher_type           = block;
1399
0
        specs->mac_algorithm         = sm3_mac;
1400
0
        specs->kea                   = ecc_diffie_hellman_kea;
1401
0
        specs->sig_algo              = sm2_sa_algo;
1402
0
        specs->hash_size             = WC_SM3_DIGEST_SIZE;
1403
0
        specs->pad_size              = PAD_SHA;
1404
0
        specs->static_ecdh           = 0;
1405
0
        specs->key_size              = SM4_KEY_SIZE;
1406
0
        specs->iv_size               = SM4_IV_SIZE;
1407
0
        specs->block_size            = SM4_BLOCK_SIZE;
1408
1409
0
        break;
1410
0
#endif
1411
1412
0
#ifdef BUILD_TLS_ECDHE_ECDSA_WITH_SM4_GCM_SM3
1413
0
    case TLS_ECDHE_ECDSA_WITH_SM4_GCM_SM3 :
1414
0
        specs->bulk_cipher_algorithm = wolfssl_sm4_gcm;
1415
0
        specs->cipher_type           = aead;
1416
0
        specs->mac_algorithm         = sm3_mac;
1417
0
        specs->kea                   = ecc_diffie_hellman_kea;
1418
0
        specs->sig_algo              = sm2_sa_algo;
1419
0
        specs->hash_size             = WC_SM3_DIGEST_SIZE;
1420
0
        specs->pad_size              = PAD_SHA;
1421
0
        specs->static_ecdh           = 0;
1422
0
        specs->key_size              = SM4_KEY_SIZE;
1423
0
        specs->block_size            = SM4_BLOCK_SIZE;
1424
0
        specs->iv_size               = GCM_IMP_IV_SZ;
1425
0
        specs->aead_mac_size         = SM4_GCM_AUTH_SZ;
1426
1427
0
        break;
1428
0
#endif
1429
1430
0
#ifdef BUILD_TLS_ECDHE_ECDSA_WITH_SM4_CCM_SM3
1431
0
    case TLS_ECDHE_ECDSA_WITH_SM4_CCM_SM3 :
1432
0
        specs->bulk_cipher_algorithm = wolfssl_sm4_ccm;
1433
0
        specs->cipher_type           = aead;
1434
0
        specs->mac_algorithm         = sm3_mac;
1435
0
        specs->kea                   = ecc_diffie_hellman_kea;
1436
0
        specs->sig_algo              = sm2_sa_algo;
1437
0
        specs->hash_size             = WC_SM3_DIGEST_SIZE;
1438
0
        specs->pad_size              = PAD_SHA;
1439
0
        specs->static_ecdh           = 0;
1440
0
        specs->key_size              = SM4_KEY_SIZE;
1441
0
        specs->block_size            = SM4_BLOCK_SIZE;
1442
0
        specs->iv_size               = CCM_IMP_IV_SZ;
1443
0
        specs->aead_mac_size         = SM4_CCM_AUTH_SZ;
1444
1445
0
        break;
1446
0
#endif
1447
1448
0
    default:
1449
0
        break;
1450
0
    }
1451
0
    }
1452
1453
0
    if (cipherSuite0 != ECC_BYTE &&
1454
0
        cipherSuite0 != ECDHE_PSK_BYTE &&
1455
0
        cipherSuite0 != CHACHA_BYTE &&
1456
0
#if defined(WOLFSSL_SM2) && defined(WOLFSSL_SM3) && \
1457
0
    (defined(WOLFSSL_SM4_CBC) || defined(WOLFSSL_SM4_GCM) || \
1458
0
     defined(WOLFSSL_SM4_CCM))
1459
0
        cipherSuite0 != SM_BYTE &&
1460
0
#endif
1461
0
        cipherSuite0 != TLS13_BYTE) {   /* normal suites */
1462
0
    switch (cipherSuite) {
1463
1464
0
#ifdef BUILD_TLS_SM4_GCM_SM3
1465
0
    case TLS_SM4_GCM_SM3 :
1466
0
        specs->bulk_cipher_algorithm = wolfssl_sm4_gcm;
1467
0
        specs->cipher_type           = aead;
1468
0
        specs->mac_algorithm         = sm3_mac;
1469
0
        specs->kea                   = 0;
1470
0
        specs->sig_algo              = 0;
1471
0
        specs->hash_size             = WC_SM3_DIGEST_SIZE;
1472
0
        specs->pad_size              = PAD_SHA;
1473
0
        specs->static_ecdh           = 0;
1474
0
        specs->key_size              = SM4_KEY_SIZE;
1475
0
        specs->block_size            = SM4_BLOCK_SIZE;
1476
0
        specs->iv_size               = SM4_GCM_NONCE_SZ;
1477
0
        specs->aead_mac_size         = SM4_GCM_AUTH_SZ;
1478
1479
0
        break;
1480
0
#endif
1481
1482
0
#ifdef BUILD_TLS_SM4_CCM_SM3
1483
0
    case TLS_SM4_CCM_SM3 :
1484
0
        specs->bulk_cipher_algorithm = wolfssl_sm4_ccm;
1485
0
        specs->cipher_type           = aead;
1486
0
        specs->mac_algorithm         = sm3_mac;
1487
0
        specs->kea                   = 0;
1488
0
        specs->sig_algo              = 0;
1489
0
        specs->hash_size             = WC_SM3_DIGEST_SIZE;
1490
0
        specs->pad_size              = PAD_SHA;
1491
0
        specs->static_ecdh           = 0;
1492
0
        specs->key_size              = SM4_KEY_SIZE;
1493
0
        specs->block_size            = SM4_BLOCK_SIZE;
1494
0
        specs->iv_size               = SM4_CCM_NONCE_SZ;
1495
0
        specs->aead_mac_size         = SM4_CCM_AUTH_SZ;
1496
1497
0
        break;
1498
0
#endif
1499
1500
#ifdef BUILD_SSL_RSA_WITH_RC4_128_SHA
1501
    case SSL_RSA_WITH_RC4_128_SHA :
1502
        specs->bulk_cipher_algorithm = wolfssl_rc4;
1503
        specs->cipher_type           = stream;
1504
        specs->mac_algorithm         = sha_mac;
1505
        specs->kea                   = rsa_kea;
1506
        specs->sig_algo              = rsa_sa_algo;
1507
        specs->hash_size             = WC_SHA_DIGEST_SIZE;
1508
        specs->pad_size              = PAD_SHA;
1509
        specs->static_ecdh           = 0;
1510
        specs->key_size              = RC4_KEY_SIZE;
1511
        specs->iv_size               = 0;
1512
        specs->block_size            = 0;
1513
1514
        break;
1515
#endif
1516
1517
#ifdef BUILD_SSL_RSA_WITH_RC4_128_MD5
1518
    case SSL_RSA_WITH_RC4_128_MD5 :
1519
        specs->bulk_cipher_algorithm = wolfssl_rc4;
1520
        specs->cipher_type           = stream;
1521
        specs->mac_algorithm         = md5_mac;
1522
        specs->kea                   = rsa_kea;
1523
        specs->sig_algo              = rsa_sa_algo;
1524
        specs->hash_size             = WC_MD5_DIGEST_SIZE;
1525
        specs->pad_size              = PAD_MD5;
1526
        specs->static_ecdh           = 0;
1527
        specs->key_size              = RC4_KEY_SIZE;
1528
        specs->iv_size               = 0;
1529
        specs->block_size            = 0;
1530
1531
        break;
1532
#endif
1533
1534
#ifdef BUILD_SSL_RSA_WITH_3DES_EDE_CBC_SHA
1535
    case SSL_RSA_WITH_3DES_EDE_CBC_SHA :
1536
        specs->bulk_cipher_algorithm = wolfssl_triple_des;
1537
        specs->cipher_type           = block;
1538
        specs->mac_algorithm         = sha_mac;
1539
        specs->kea                   = rsa_kea;
1540
        specs->sig_algo              = rsa_sa_algo;
1541
        specs->hash_size             = WC_SHA_DIGEST_SIZE;
1542
        specs->pad_size              = PAD_SHA;
1543
        specs->static_ecdh           = 0;
1544
        specs->key_size              = DES3_KEY_SIZE;
1545
        specs->block_size            = DES_BLOCK_SIZE;
1546
#if defined(HAVE_FIPS) && defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION == 2)
1547
        specs->iv_size               = DES_BLOCK_SIZE;
1548
#else
1549
        specs->iv_size               = DES_IV_SIZE;
1550
#endif
1551
1552
        break;
1553
#endif
1554
1555
#ifdef BUILD_TLS_RSA_WITH_AES_128_CBC_SHA
1556
    case TLS_RSA_WITH_AES_128_CBC_SHA :
1557
        specs->bulk_cipher_algorithm = wolfssl_aes;
1558
        specs->cipher_type           = block;
1559
        specs->mac_algorithm         = sha_mac;
1560
        specs->kea                   = rsa_kea;
1561
        specs->sig_algo              = rsa_sa_algo;
1562
        specs->hash_size             = WC_SHA_DIGEST_SIZE;
1563
        specs->pad_size              = PAD_SHA;
1564
        specs->static_ecdh           = 0;
1565
        specs->key_size              = AES_128_KEY_SIZE;
1566
        specs->block_size            = WC_AES_BLOCK_SIZE;
1567
        specs->iv_size               = AES_IV_SIZE;
1568
1569
        break;
1570
#endif
1571
1572
#ifdef BUILD_TLS_RSA_WITH_AES_128_CBC_SHA256
1573
    case TLS_RSA_WITH_AES_128_CBC_SHA256 :
1574
        specs->bulk_cipher_algorithm = wolfssl_aes;
1575
        specs->cipher_type           = block;
1576
        specs->mac_algorithm         = sha256_mac;
1577
        specs->kea                   = rsa_kea;
1578
        specs->sig_algo              = rsa_sa_algo;
1579
        specs->hash_size             = WC_SHA256_DIGEST_SIZE;
1580
        specs->pad_size              = PAD_SHA;
1581
        specs->static_ecdh           = 0;
1582
        specs->key_size              = AES_128_KEY_SIZE;
1583
        specs->block_size            = WC_AES_BLOCK_SIZE;
1584
        specs->iv_size               = AES_IV_SIZE;
1585
1586
        break;
1587
#endif
1588
1589
#ifdef BUILD_TLS_RSA_WITH_NULL_MD5
1590
    case TLS_RSA_WITH_NULL_MD5 :
1591
        specs->bulk_cipher_algorithm = wolfssl_cipher_null;
1592
        specs->cipher_type           = stream;
1593
        specs->mac_algorithm         = md5_mac;
1594
        specs->kea                   = rsa_kea;
1595
        specs->sig_algo              = rsa_sa_algo;
1596
        specs->hash_size             = WC_MD5_DIGEST_SIZE;
1597
        specs->pad_size              = PAD_MD5;
1598
        specs->static_ecdh           = 0;
1599
        specs->key_size              = 0;
1600
        specs->block_size            = 0;
1601
        specs->iv_size               = 0;
1602
1603
        break;
1604
#endif
1605
1606
#ifdef BUILD_TLS_RSA_WITH_NULL_SHA
1607
    case TLS_RSA_WITH_NULL_SHA :
1608
        specs->bulk_cipher_algorithm = wolfssl_cipher_null;
1609
        specs->cipher_type           = stream;
1610
        specs->mac_algorithm         = sha_mac;
1611
        specs->kea                   = rsa_kea;
1612
        specs->sig_algo              = rsa_sa_algo;
1613
        specs->hash_size             = WC_SHA_DIGEST_SIZE;
1614
        specs->pad_size              = PAD_SHA;
1615
        specs->static_ecdh           = 0;
1616
        specs->key_size              = 0;
1617
        specs->block_size            = 0;
1618
        specs->iv_size               = 0;
1619
1620
        break;
1621
#endif
1622
1623
#ifdef BUILD_TLS_RSA_WITH_NULL_SHA256
1624
    case TLS_RSA_WITH_NULL_SHA256 :
1625
        specs->bulk_cipher_algorithm = wolfssl_cipher_null;
1626
        specs->cipher_type           = stream;
1627
        specs->mac_algorithm         = sha256_mac;
1628
        specs->kea                   = rsa_kea;
1629
        specs->sig_algo              = rsa_sa_algo;
1630
        specs->hash_size             = WC_SHA256_DIGEST_SIZE;
1631
        specs->pad_size              = PAD_SHA;
1632
        specs->static_ecdh           = 0;
1633
        specs->key_size              = 0;
1634
        specs->block_size            = 0;
1635
        specs->iv_size               = 0;
1636
1637
        break;
1638
#endif
1639
1640
#ifdef BUILD_TLS_RSA_WITH_AES_256_CBC_SHA
1641
    case TLS_RSA_WITH_AES_256_CBC_SHA :
1642
        specs->bulk_cipher_algorithm = wolfssl_aes;
1643
        specs->cipher_type           = block;
1644
        specs->mac_algorithm         = sha_mac;
1645
        specs->kea                   = rsa_kea;
1646
        specs->sig_algo              = rsa_sa_algo;
1647
        specs->hash_size             = WC_SHA_DIGEST_SIZE;
1648
        specs->pad_size              = PAD_SHA;
1649
        specs->static_ecdh           = 0;
1650
        specs->key_size              = AES_256_KEY_SIZE;
1651
        specs->block_size            = WC_AES_BLOCK_SIZE;
1652
        specs->iv_size               = AES_IV_SIZE;
1653
1654
        break;
1655
#endif
1656
1657
#ifdef BUILD_TLS_RSA_WITH_AES_256_CBC_SHA256
1658
    case TLS_RSA_WITH_AES_256_CBC_SHA256 :
1659
        specs->bulk_cipher_algorithm = wolfssl_aes;
1660
        specs->cipher_type           = block;
1661
        specs->mac_algorithm         = sha256_mac;
1662
        specs->kea                   = rsa_kea;
1663
        specs->sig_algo              = rsa_sa_algo;
1664
        specs->hash_size             = WC_SHA256_DIGEST_SIZE;
1665
        specs->pad_size              = PAD_SHA;
1666
        specs->static_ecdh           = 0;
1667
        specs->key_size              = AES_256_KEY_SIZE;
1668
        specs->block_size            = WC_AES_BLOCK_SIZE;
1669
        specs->iv_size               = AES_IV_SIZE;
1670
1671
        break;
1672
#endif
1673
1674
#ifdef BUILD_TLS_PSK_WITH_AES_128_GCM_SHA256
1675
    case TLS_PSK_WITH_AES_128_GCM_SHA256 :
1676
        specs->bulk_cipher_algorithm = wolfssl_aes_gcm;
1677
        specs->cipher_type           = aead;
1678
        specs->mac_algorithm         = sha256_mac;
1679
        specs->kea                   = psk_kea;
1680
        specs->sig_algo              = anonymous_sa_algo;
1681
        specs->hash_size             = WC_SHA256_DIGEST_SIZE;
1682
        specs->pad_size              = PAD_SHA;
1683
        specs->static_ecdh           = 0;
1684
        specs->key_size              = AES_128_KEY_SIZE;
1685
        specs->block_size            = WC_AES_BLOCK_SIZE;
1686
        specs->iv_size               = AESGCM_IMP_IV_SZ;
1687
        specs->aead_mac_size         = AES_GCM_AUTH_SZ;
1688
1689
        if (opts != NULL)
1690
            opts->usingPSK_cipher    = 1;
1691
        break;
1692
#endif
1693
1694
#ifdef BUILD_TLS_PSK_WITH_AES_256_GCM_SHA384
1695
    case TLS_PSK_WITH_AES_256_GCM_SHA384 :
1696
        specs->bulk_cipher_algorithm = wolfssl_aes_gcm;
1697
        specs->cipher_type           = aead;
1698
        specs->mac_algorithm         = sha384_mac;
1699
        specs->kea                   = psk_kea;
1700
        specs->sig_algo              = anonymous_sa_algo;
1701
        specs->hash_size             = WC_SHA384_DIGEST_SIZE;
1702
        specs->pad_size              = PAD_SHA;
1703
        specs->static_ecdh           = 0;
1704
        specs->key_size              = AES_256_KEY_SIZE;
1705
        specs->block_size            = WC_AES_BLOCK_SIZE;
1706
        specs->iv_size               = AESGCM_IMP_IV_SZ;
1707
        specs->aead_mac_size         = AES_GCM_AUTH_SZ;
1708
1709
        if (opts != NULL)
1710
            opts->usingPSK_cipher    = 1;
1711
        break;
1712
#endif
1713
1714
#ifdef BUILD_TLS_DH_anon_WITH_AES_256_GCM_SHA384
1715
    case TLS_DH_anon_WITH_AES_256_GCM_SHA384:
1716
        specs->bulk_cipher_algorithm = wolfssl_aes_gcm;
1717
        specs->cipher_type           = aead;
1718
        specs->mac_algorithm         = sha384_mac;
1719
        specs->kea                   = diffie_hellman_kea;
1720
        specs->sig_algo              = anonymous_sa_algo;
1721
        specs->hash_size             = WC_SHA384_DIGEST_SIZE;
1722
        specs->pad_size              = PAD_SHA;
1723
        specs->static_ecdh           = 0;
1724
        specs->key_size              = AES_256_KEY_SIZE;
1725
        specs->block_size            = WC_AES_BLOCK_SIZE;
1726
        specs->iv_size               = AESGCM_IMP_IV_SZ;
1727
        specs->aead_mac_size         = AES_GCM_AUTH_SZ;
1728
1729
        if (opts != NULL)
1730
            opts->usingAnon_cipher   = 1;
1731
        break;
1732
#endif
1733
1734
#ifdef BUILD_TLS_DHE_PSK_WITH_AES_128_GCM_SHA256
1735
    case TLS_DHE_PSK_WITH_AES_128_GCM_SHA256 :
1736
        specs->bulk_cipher_algorithm = wolfssl_aes_gcm;
1737
        specs->cipher_type           = aead;
1738
        specs->mac_algorithm         = sha256_mac;
1739
        specs->kea                   = dhe_psk_kea;
1740
        specs->sig_algo              = anonymous_sa_algo;
1741
        specs->hash_size             = WC_SHA256_DIGEST_SIZE;
1742
        specs->pad_size              = PAD_SHA;
1743
        specs->static_ecdh           = 0;
1744
        specs->key_size              = AES_128_KEY_SIZE;
1745
        specs->block_size            = WC_AES_BLOCK_SIZE;
1746
        specs->iv_size               = AESGCM_IMP_IV_SZ;
1747
        specs->aead_mac_size         = AES_GCM_AUTH_SZ;
1748
1749
        if (opts != NULL)
1750
            opts->usingPSK_cipher    = 1;
1751
        break;
1752
#endif
1753
1754
#ifdef BUILD_TLS_DHE_PSK_WITH_AES_256_GCM_SHA384
1755
    case TLS_DHE_PSK_WITH_AES_256_GCM_SHA384 :
1756
        specs->bulk_cipher_algorithm = wolfssl_aes_gcm;
1757
        specs->cipher_type           = aead;
1758
        specs->mac_algorithm         = sha384_mac;
1759
        specs->kea                   = dhe_psk_kea;
1760
        specs->sig_algo              = anonymous_sa_algo;
1761
        specs->hash_size             = WC_SHA384_DIGEST_SIZE;
1762
        specs->pad_size              = PAD_SHA;
1763
        specs->static_ecdh           = 0;
1764
        specs->key_size              = AES_256_KEY_SIZE;
1765
        specs->block_size            = WC_AES_BLOCK_SIZE;
1766
        specs->iv_size               = AESGCM_IMP_IV_SZ;
1767
        specs->aead_mac_size         = AES_GCM_AUTH_SZ;
1768
1769
        if (opts != NULL)
1770
            opts->usingPSK_cipher    = 1;
1771
        break;
1772
#endif
1773
1774
#ifdef BUILD_TLS_PSK_WITH_AES_128_CBC_SHA256
1775
    case TLS_PSK_WITH_AES_128_CBC_SHA256 :
1776
        specs->bulk_cipher_algorithm = wolfssl_aes;
1777
        specs->cipher_type           = block;
1778
        specs->mac_algorithm         = sha256_mac;
1779
        specs->kea                   = psk_kea;
1780
        specs->sig_algo              = anonymous_sa_algo;
1781
        specs->hash_size             = WC_SHA256_DIGEST_SIZE;
1782
        specs->pad_size              = PAD_SHA;
1783
        specs->static_ecdh           = 0;
1784
        specs->key_size              = AES_128_KEY_SIZE;
1785
        specs->block_size            = WC_AES_BLOCK_SIZE;
1786
        specs->iv_size               = AES_IV_SIZE;
1787
1788
        if (opts != NULL)
1789
            opts->usingPSK_cipher    = 1;
1790
        break;
1791
#endif
1792
1793
#ifdef BUILD_TLS_PSK_WITH_AES_256_CBC_SHA384
1794
    case TLS_PSK_WITH_AES_256_CBC_SHA384 :
1795
        specs->bulk_cipher_algorithm = wolfssl_aes;
1796
        specs->cipher_type           = block;
1797
        specs->mac_algorithm         = sha384_mac;
1798
        specs->kea                   = psk_kea;
1799
        specs->sig_algo              = anonymous_sa_algo;
1800
        specs->hash_size             = WC_SHA384_DIGEST_SIZE;
1801
        specs->pad_size              = PAD_SHA;
1802
        specs->static_ecdh           = 0;
1803
        specs->key_size              = AES_256_KEY_SIZE;
1804
        specs->block_size            = WC_AES_BLOCK_SIZE;
1805
        specs->iv_size               = AES_IV_SIZE;
1806
1807
        if (opts != NULL)
1808
            opts->usingPSK_cipher    = 1;
1809
        break;
1810
#endif
1811
1812
#ifdef BUILD_TLS_DHE_PSK_WITH_AES_128_CBC_SHA256
1813
    case TLS_DHE_PSK_WITH_AES_128_CBC_SHA256 :
1814
        specs->bulk_cipher_algorithm = wolfssl_aes;
1815
        specs->cipher_type           = block;
1816
        specs->mac_algorithm         = sha256_mac;
1817
        specs->kea                   = dhe_psk_kea;
1818
        specs->sig_algo              = anonymous_sa_algo;
1819
        specs->hash_size             = WC_SHA256_DIGEST_SIZE;
1820
        specs->pad_size              = PAD_SHA;
1821
        specs->static_ecdh           = 0;
1822
        specs->key_size              = AES_128_KEY_SIZE;
1823
        specs->block_size            = WC_AES_BLOCK_SIZE;
1824
        specs->iv_size               = AES_IV_SIZE;
1825
1826
        if (opts != NULL)
1827
            opts->usingPSK_cipher    = 1;
1828
        break;
1829
#endif
1830
1831
#ifdef BUILD_TLS_DHE_PSK_WITH_AES_256_CBC_SHA384
1832
    case TLS_DHE_PSK_WITH_AES_256_CBC_SHA384 :
1833
        specs->bulk_cipher_algorithm = wolfssl_aes;
1834
        specs->cipher_type           = block;
1835
        specs->mac_algorithm         = sha384_mac;
1836
        specs->kea                   = dhe_psk_kea;
1837
        specs->sig_algo              = anonymous_sa_algo;
1838
        specs->hash_size             = WC_SHA384_DIGEST_SIZE;
1839
        specs->pad_size              = PAD_SHA;
1840
        specs->static_ecdh           = 0;
1841
        specs->key_size              = AES_256_KEY_SIZE;
1842
        specs->block_size            = WC_AES_BLOCK_SIZE;
1843
        specs->iv_size               = AES_IV_SIZE;
1844
1845
        if (opts != NULL)
1846
            opts->usingPSK_cipher    = 1;
1847
        break;
1848
#endif
1849
1850
#ifdef BUILD_TLS_PSK_WITH_AES_128_CBC_SHA
1851
    case TLS_PSK_WITH_AES_128_CBC_SHA :
1852
        specs->bulk_cipher_algorithm = wolfssl_aes;
1853
        specs->cipher_type           = block;
1854
        specs->mac_algorithm         = sha_mac;
1855
        specs->kea                   = psk_kea;
1856
        specs->sig_algo              = anonymous_sa_algo;
1857
        specs->hash_size             = WC_SHA_DIGEST_SIZE;
1858
        specs->pad_size              = PAD_SHA;
1859
        specs->static_ecdh           = 0;
1860
        specs->key_size              = AES_128_KEY_SIZE;
1861
        specs->block_size            = WC_AES_BLOCK_SIZE;
1862
        specs->iv_size               = AES_IV_SIZE;
1863
1864
        if (opts != NULL)
1865
            opts->usingPSK_cipher    = 1;
1866
        break;
1867
#endif
1868
1869
#ifdef BUILD_TLS_PSK_WITH_AES_256_CBC_SHA
1870
    case TLS_PSK_WITH_AES_256_CBC_SHA :
1871
        specs->bulk_cipher_algorithm = wolfssl_aes;
1872
        specs->cipher_type           = block;
1873
        specs->mac_algorithm         = sha_mac;
1874
        specs->kea                   = psk_kea;
1875
        specs->sig_algo              = anonymous_sa_algo;
1876
        specs->hash_size             = WC_SHA_DIGEST_SIZE;
1877
        specs->pad_size              = PAD_SHA;
1878
        specs->static_ecdh           = 0;
1879
        specs->key_size              = AES_256_KEY_SIZE;
1880
        specs->block_size            = WC_AES_BLOCK_SIZE;
1881
        specs->iv_size               = AES_IV_SIZE;
1882
1883
        if (opts != NULL)
1884
            opts->usingPSK_cipher    = 1;
1885
        break;
1886
#endif
1887
1888
#ifdef BUILD_TLS_PSK_WITH_NULL_SHA256
1889
    case TLS_PSK_WITH_NULL_SHA256 :
1890
        specs->bulk_cipher_algorithm = wolfssl_cipher_null;
1891
        specs->cipher_type           = stream;
1892
        specs->mac_algorithm         = sha256_mac;
1893
        specs->kea                   = psk_kea;
1894
        specs->sig_algo              = anonymous_sa_algo;
1895
        specs->hash_size             = WC_SHA256_DIGEST_SIZE;
1896
        specs->pad_size              = PAD_SHA;
1897
        specs->static_ecdh           = 0;
1898
        specs->key_size              = 0;
1899
        specs->block_size            = 0;
1900
        specs->iv_size               = 0;
1901
1902
        if (opts != NULL)
1903
            opts->usingPSK_cipher    = 1;
1904
        break;
1905
#endif
1906
1907
#ifdef BUILD_TLS_PSK_WITH_NULL_SHA384
1908
    case TLS_PSK_WITH_NULL_SHA384 :
1909
        specs->bulk_cipher_algorithm = wolfssl_cipher_null;
1910
        specs->cipher_type           = stream;
1911
        specs->mac_algorithm         = sha384_mac;
1912
        specs->kea                   = psk_kea;
1913
        specs->sig_algo              = anonymous_sa_algo;
1914
        specs->hash_size             = WC_SHA384_DIGEST_SIZE;
1915
        specs->pad_size              = PAD_SHA;
1916
        specs->static_ecdh           = 0;
1917
        specs->key_size              = 0;
1918
        specs->block_size            = 0;
1919
        specs->iv_size               = 0;
1920
1921
        if (opts != NULL)
1922
            opts->usingPSK_cipher    = 1;
1923
        break;
1924
#endif
1925
1926
#ifdef BUILD_TLS_PSK_WITH_NULL_SHA
1927
    case TLS_PSK_WITH_NULL_SHA :
1928
        specs->bulk_cipher_algorithm = wolfssl_cipher_null;
1929
        specs->cipher_type           = stream;
1930
        specs->mac_algorithm         = sha_mac;
1931
        specs->kea                   = psk_kea;
1932
        specs->sig_algo              = anonymous_sa_algo;
1933
        specs->hash_size             = WC_SHA_DIGEST_SIZE;
1934
        specs->pad_size              = PAD_SHA;
1935
        specs->static_ecdh           = 0;
1936
        specs->key_size              = 0;
1937
        specs->block_size            = 0;
1938
        specs->iv_size               = 0;
1939
1940
        if (opts != NULL)
1941
            opts->usingPSK_cipher    = 1;
1942
        break;
1943
#endif
1944
1945
#ifdef BUILD_TLS_DHE_PSK_WITH_NULL_SHA256
1946
    case TLS_DHE_PSK_WITH_NULL_SHA256 :
1947
        specs->bulk_cipher_algorithm = wolfssl_cipher_null;
1948
        specs->cipher_type           = stream;
1949
        specs->mac_algorithm         = sha256_mac;
1950
        specs->kea                   = dhe_psk_kea;
1951
        specs->sig_algo              = anonymous_sa_algo;
1952
        specs->hash_size             = WC_SHA256_DIGEST_SIZE;
1953
        specs->pad_size              = PAD_SHA;
1954
        specs->static_ecdh           = 0;
1955
        specs->key_size              = 0;
1956
        specs->block_size            = 0;
1957
        specs->iv_size               = 0;
1958
1959
        if (opts != NULL)
1960
            opts->usingPSK_cipher    = 1;
1961
        break;
1962
#endif
1963
1964
#ifdef BUILD_TLS_DHE_PSK_WITH_NULL_SHA384
1965
    case TLS_DHE_PSK_WITH_NULL_SHA384 :
1966
        specs->bulk_cipher_algorithm = wolfssl_cipher_null;
1967
        specs->cipher_type           = stream;
1968
        specs->mac_algorithm         = sha384_mac;
1969
        specs->kea                   = dhe_psk_kea;
1970
        specs->sig_algo              = anonymous_sa_algo;
1971
        specs->hash_size             = WC_SHA384_DIGEST_SIZE;
1972
        specs->pad_size              = PAD_SHA;
1973
        specs->static_ecdh           = 0;
1974
        specs->key_size              = 0;
1975
        specs->block_size            = 0;
1976
        specs->iv_size               = 0;
1977
1978
        if (opts != NULL)
1979
            opts->usingPSK_cipher    = 1;
1980
        break;
1981
#endif
1982
1983
0
#ifdef BUILD_TLS_DHE_RSA_WITH_AES_128_CBC_SHA256
1984
0
    case TLS_DHE_RSA_WITH_AES_128_CBC_SHA256 :
1985
0
        specs->bulk_cipher_algorithm = wolfssl_aes;
1986
0
        specs->cipher_type           = block;
1987
0
        specs->mac_algorithm         = sha256_mac;
1988
0
        specs->kea                   = diffie_hellman_kea;
1989
0
        specs->sig_algo              = rsa_sa_algo;
1990
0
        specs->hash_size             = WC_SHA256_DIGEST_SIZE;
1991
0
        specs->pad_size              = PAD_SHA;
1992
0
        specs->static_ecdh           = 0;
1993
0
        specs->key_size              = AES_128_KEY_SIZE;
1994
0
        specs->block_size            = WC_AES_BLOCK_SIZE;
1995
0
        specs->iv_size               = AES_IV_SIZE;
1996
1997
0
        break;
1998
0
#endif
1999
2000
#ifdef BUILD_TLS_DHE_RSA_WITH_3DES_EDE_CBC_SHA
2001
    case TLS_DHE_RSA_WITH_3DES_EDE_CBC_SHA :
2002
        specs->bulk_cipher_algorithm = wolfssl_triple_des;
2003
        specs->cipher_type           = block;
2004
        specs->mac_algorithm         = sha_mac;
2005
        specs->kea                   = diffie_hellman_kea;
2006
        specs->sig_algo              = rsa_sa_algo;
2007
        specs->hash_size             = WC_SHA_DIGEST_SIZE;
2008
        specs->pad_size              = PAD_SHA;
2009
        specs->static_ecdh           = 0;
2010
        specs->key_size              = DES3_KEY_SIZE;
2011
        specs->block_size            = DES_BLOCK_SIZE;
2012
        specs->iv_size               = DES_IV_SIZE;
2013
2014
        break;
2015
#endif
2016
2017
0
#ifdef BUILD_TLS_DHE_RSA_WITH_AES_256_CBC_SHA256
2018
0
    case TLS_DHE_RSA_WITH_AES_256_CBC_SHA256 :
2019
0
        specs->bulk_cipher_algorithm = wolfssl_aes;
2020
0
        specs->cipher_type           = block;
2021
0
        specs->mac_algorithm         = sha256_mac;
2022
0
        specs->kea                   = diffie_hellman_kea;
2023
0
        specs->sig_algo              = rsa_sa_algo;
2024
0
        specs->hash_size             = WC_SHA256_DIGEST_SIZE;
2025
0
        specs->pad_size              = PAD_SHA;
2026
0
        specs->static_ecdh           = 0;
2027
0
        specs->key_size              = AES_256_KEY_SIZE;
2028
0
        specs->block_size            = WC_AES_BLOCK_SIZE;
2029
0
        specs->iv_size               = AES_IV_SIZE;
2030
2031
0
        break;
2032
0
#endif
2033
2034
0
#ifdef BUILD_TLS_DHE_RSA_WITH_AES_128_CBC_SHA
2035
0
    case TLS_DHE_RSA_WITH_AES_128_CBC_SHA :
2036
0
        specs->bulk_cipher_algorithm = wolfssl_aes;
2037
0
        specs->cipher_type           = block;
2038
0
        specs->mac_algorithm         = sha_mac;
2039
0
        specs->kea                   = diffie_hellman_kea;
2040
0
        specs->sig_algo              = rsa_sa_algo;
2041
0
        specs->hash_size             = WC_SHA_DIGEST_SIZE;
2042
0
        specs->pad_size              = PAD_SHA;
2043
0
        specs->static_ecdh           = 0;
2044
0
        specs->key_size              = AES_128_KEY_SIZE;
2045
0
        specs->block_size            = WC_AES_BLOCK_SIZE;
2046
0
        specs->iv_size               = AES_IV_SIZE;
2047
2048
0
        break;
2049
0
#endif
2050
2051
0
#ifdef BUILD_TLS_DHE_RSA_WITH_AES_256_CBC_SHA
2052
0
    case TLS_DHE_RSA_WITH_AES_256_CBC_SHA :
2053
0
        specs->bulk_cipher_algorithm = wolfssl_aes;
2054
0
        specs->cipher_type           = block;
2055
0
        specs->mac_algorithm         = sha_mac;
2056
0
        specs->kea                   = diffie_hellman_kea;
2057
0
        specs->sig_algo              = rsa_sa_algo;
2058
0
        specs->hash_size             = WC_SHA_DIGEST_SIZE;
2059
0
        specs->pad_size              = PAD_SHA;
2060
0
        specs->static_ecdh           = 0;
2061
0
        specs->key_size              = AES_256_KEY_SIZE;
2062
0
        specs->block_size            = WC_AES_BLOCK_SIZE;
2063
0
        specs->iv_size               = AES_IV_SIZE;
2064
2065
0
        break;
2066
0
#endif
2067
2068
#ifdef BUILD_TLS_RSA_WITH_AES_128_GCM_SHA256
2069
    case TLS_RSA_WITH_AES_128_GCM_SHA256 :
2070
        specs->bulk_cipher_algorithm = wolfssl_aes_gcm;
2071
        specs->cipher_type           = aead;
2072
        specs->mac_algorithm         = sha256_mac;
2073
        specs->kea                   = rsa_kea;
2074
        specs->sig_algo              = rsa_sa_algo;
2075
        specs->hash_size             = WC_SHA256_DIGEST_SIZE;
2076
        specs->pad_size              = PAD_SHA;
2077
        specs->static_ecdh           = 0;
2078
        specs->key_size              = AES_128_KEY_SIZE;
2079
        specs->block_size            = WC_AES_BLOCK_SIZE;
2080
        specs->iv_size               = AESGCM_IMP_IV_SZ;
2081
        specs->aead_mac_size         = AES_GCM_AUTH_SZ;
2082
2083
        break;
2084
#endif
2085
2086
#ifdef BUILD_TLS_RSA_WITH_AES_256_GCM_SHA384
2087
    case TLS_RSA_WITH_AES_256_GCM_SHA384 :
2088
        specs->bulk_cipher_algorithm = wolfssl_aes_gcm;
2089
        specs->cipher_type           = aead;
2090
        specs->mac_algorithm         = sha384_mac;
2091
        specs->kea                   = rsa_kea;
2092
        specs->sig_algo              = rsa_sa_algo;
2093
        specs->hash_size             = WC_SHA384_DIGEST_SIZE;
2094
        specs->pad_size              = PAD_SHA;
2095
        specs->static_ecdh           = 0;
2096
        specs->key_size              = AES_256_KEY_SIZE;
2097
        specs->block_size            = WC_AES_BLOCK_SIZE;
2098
        specs->iv_size               = AESGCM_IMP_IV_SZ;
2099
        specs->aead_mac_size         = AES_GCM_AUTH_SZ;
2100
2101
        break;
2102
#endif
2103
2104
0
#ifdef BUILD_TLS_DHE_RSA_WITH_AES_128_GCM_SHA256
2105
0
    case TLS_DHE_RSA_WITH_AES_128_GCM_SHA256 :
2106
0
        specs->bulk_cipher_algorithm = wolfssl_aes_gcm;
2107
0
        specs->cipher_type           = aead;
2108
0
        specs->mac_algorithm         = sha256_mac;
2109
0
        specs->kea                   = diffie_hellman_kea;
2110
0
        specs->sig_algo              = rsa_sa_algo;
2111
0
        specs->hash_size             = WC_SHA256_DIGEST_SIZE;
2112
0
        specs->pad_size              = PAD_SHA;
2113
0
        specs->static_ecdh           = 0;
2114
0
        specs->key_size              = AES_128_KEY_SIZE;
2115
0
        specs->block_size            = WC_AES_BLOCK_SIZE;
2116
0
        specs->iv_size               = AESGCM_IMP_IV_SZ;
2117
0
        specs->aead_mac_size         = AES_GCM_AUTH_SZ;
2118
2119
0
        break;
2120
0
#endif
2121
2122
0
#ifdef BUILD_TLS_DHE_RSA_WITH_AES_256_GCM_SHA384
2123
0
    case TLS_DHE_RSA_WITH_AES_256_GCM_SHA384 :
2124
0
        specs->bulk_cipher_algorithm = wolfssl_aes_gcm;
2125
0
        specs->cipher_type           = aead;
2126
0
        specs->mac_algorithm         = sha384_mac;
2127
0
        specs->kea                   = diffie_hellman_kea;
2128
0
        specs->sig_algo              = rsa_sa_algo;
2129
0
        specs->hash_size             = WC_SHA384_DIGEST_SIZE;
2130
0
        specs->pad_size              = PAD_SHA;
2131
0
        specs->static_ecdh           = 0;
2132
0
        specs->key_size              = AES_256_KEY_SIZE;
2133
0
        specs->block_size            = WC_AES_BLOCK_SIZE;
2134
0
        specs->iv_size               = AESGCM_IMP_IV_SZ;
2135
0
        specs->aead_mac_size         = AES_GCM_AUTH_SZ;
2136
2137
0
        break;
2138
0
#endif
2139
2140
#ifdef BUILD_TLS_RSA_WITH_CAMELLIA_128_CBC_SHA
2141
    case TLS_RSA_WITH_CAMELLIA_128_CBC_SHA :
2142
        specs->bulk_cipher_algorithm = wolfssl_camellia;
2143
        specs->cipher_type           = block;
2144
        specs->mac_algorithm         = sha_mac;
2145
        specs->kea                   = rsa_kea;
2146
        specs->sig_algo              = rsa_sa_algo;
2147
        specs->hash_size             = WC_SHA_DIGEST_SIZE;
2148
        specs->pad_size              = PAD_SHA;
2149
        specs->static_ecdh           = 0;
2150
        specs->key_size              = CAMELLIA_128_KEY_SIZE;
2151
        specs->block_size            = WC_CAMELLIA_BLOCK_SIZE;
2152
        specs->iv_size               = CAMELLIA_IV_SIZE;
2153
2154
        break;
2155
#endif
2156
2157
#ifdef BUILD_TLS_RSA_WITH_CAMELLIA_256_CBC_SHA
2158
    case TLS_RSA_WITH_CAMELLIA_256_CBC_SHA :
2159
        specs->bulk_cipher_algorithm = wolfssl_camellia;
2160
        specs->cipher_type           = block;
2161
        specs->mac_algorithm         = sha_mac;
2162
        specs->kea                   = rsa_kea;
2163
        specs->sig_algo              = rsa_sa_algo;
2164
        specs->hash_size             = WC_SHA_DIGEST_SIZE;
2165
        specs->pad_size              = PAD_SHA;
2166
        specs->static_ecdh           = 0;
2167
        specs->key_size              = CAMELLIA_256_KEY_SIZE;
2168
        specs->block_size            = WC_CAMELLIA_BLOCK_SIZE;
2169
        specs->iv_size               = CAMELLIA_IV_SIZE;
2170
2171
        break;
2172
#endif
2173
2174
#ifdef BUILD_TLS_RSA_WITH_CAMELLIA_128_CBC_SHA256
2175
    case TLS_RSA_WITH_CAMELLIA_128_CBC_SHA256 :
2176
        specs->bulk_cipher_algorithm = wolfssl_camellia;
2177
        specs->cipher_type           = block;
2178
        specs->mac_algorithm         = sha256_mac;
2179
        specs->kea                   = rsa_kea;
2180
        specs->sig_algo              = rsa_sa_algo;
2181
        specs->hash_size             = WC_SHA256_DIGEST_SIZE;
2182
        specs->pad_size              = PAD_SHA;
2183
        specs->static_ecdh           = 0;
2184
        specs->key_size              = CAMELLIA_128_KEY_SIZE;
2185
        specs->block_size            = WC_CAMELLIA_BLOCK_SIZE;
2186
        specs->iv_size               = CAMELLIA_IV_SIZE;
2187
2188
        break;
2189
#endif
2190
2191
#ifdef BUILD_TLS_RSA_WITH_CAMELLIA_256_CBC_SHA256
2192
    case TLS_RSA_WITH_CAMELLIA_256_CBC_SHA256 :
2193
        specs->bulk_cipher_algorithm = wolfssl_camellia;
2194
        specs->cipher_type           = block;
2195
        specs->mac_algorithm         = sha256_mac;
2196
        specs->kea                   = rsa_kea;
2197
        specs->sig_algo              = rsa_sa_algo;
2198
        specs->hash_size             = WC_SHA256_DIGEST_SIZE;
2199
        specs->pad_size              = PAD_SHA;
2200
        specs->static_ecdh           = 0;
2201
        specs->key_size              = CAMELLIA_256_KEY_SIZE;
2202
        specs->block_size            = WC_CAMELLIA_BLOCK_SIZE;
2203
        specs->iv_size               = CAMELLIA_IV_SIZE;
2204
2205
        break;
2206
#endif
2207
2208
0
#ifdef BUILD_TLS_DHE_RSA_WITH_CAMELLIA_128_CBC_SHA
2209
0
    case TLS_DHE_RSA_WITH_CAMELLIA_128_CBC_SHA :
2210
0
        specs->bulk_cipher_algorithm = wolfssl_camellia;
2211
0
        specs->cipher_type           = block;
2212
0
        specs->mac_algorithm         = sha_mac;
2213
0
        specs->kea                   = diffie_hellman_kea;
2214
0
        specs->sig_algo              = rsa_sa_algo;
2215
0
        specs->hash_size             = WC_SHA_DIGEST_SIZE;
2216
0
        specs->pad_size              = PAD_SHA;
2217
0
        specs->static_ecdh           = 0;
2218
0
        specs->key_size              = CAMELLIA_128_KEY_SIZE;
2219
0
        specs->block_size            = WC_CAMELLIA_BLOCK_SIZE;
2220
0
        specs->iv_size               = CAMELLIA_IV_SIZE;
2221
2222
0
        break;
2223
0
#endif
2224
2225
0
#ifdef BUILD_TLS_DHE_RSA_WITH_CAMELLIA_256_CBC_SHA
2226
0
    case TLS_DHE_RSA_WITH_CAMELLIA_256_CBC_SHA :
2227
0
        specs->bulk_cipher_algorithm = wolfssl_camellia;
2228
0
        specs->cipher_type           = block;
2229
0
        specs->mac_algorithm         = sha_mac;
2230
0
        specs->kea                   = diffie_hellman_kea;
2231
0
        specs->sig_algo              = rsa_sa_algo;
2232
0
        specs->hash_size             = WC_SHA_DIGEST_SIZE;
2233
0
        specs->pad_size              = PAD_SHA;
2234
0
        specs->static_ecdh           = 0;
2235
0
        specs->key_size              = CAMELLIA_256_KEY_SIZE;
2236
0
        specs->block_size            = WC_CAMELLIA_BLOCK_SIZE;
2237
0
        specs->iv_size               = CAMELLIA_IV_SIZE;
2238
2239
0
        break;
2240
0
#endif
2241
2242
0
#ifdef BUILD_TLS_DHE_RSA_WITH_CAMELLIA_128_CBC_SHA256
2243
0
    case TLS_DHE_RSA_WITH_CAMELLIA_128_CBC_SHA256 :
2244
0
        specs->bulk_cipher_algorithm = wolfssl_camellia;
2245
0
        specs->cipher_type           = block;
2246
0
        specs->mac_algorithm         = sha256_mac;
2247
0
        specs->kea                   = diffie_hellman_kea;
2248
0
        specs->sig_algo              = rsa_sa_algo;
2249
0
        specs->hash_size             = WC_SHA256_DIGEST_SIZE;
2250
0
        specs->pad_size              = PAD_SHA;
2251
0
        specs->static_ecdh           = 0;
2252
0
        specs->key_size              = CAMELLIA_128_KEY_SIZE;
2253
0
        specs->block_size            = WC_CAMELLIA_BLOCK_SIZE;
2254
0
        specs->iv_size               = CAMELLIA_IV_SIZE;
2255
2256
0
        break;
2257
0
#endif
2258
2259
0
#ifdef BUILD_TLS_DHE_RSA_WITH_CAMELLIA_256_CBC_SHA256
2260
0
    case TLS_DHE_RSA_WITH_CAMELLIA_256_CBC_SHA256 :
2261
0
        specs->bulk_cipher_algorithm = wolfssl_camellia;
2262
0
        specs->cipher_type           = block;
2263
0
        specs->mac_algorithm         = sha256_mac;
2264
0
        specs->kea                   = diffie_hellman_kea;
2265
0
        specs->sig_algo              = rsa_sa_algo;
2266
0
        specs->hash_size             = WC_SHA256_DIGEST_SIZE;
2267
0
        specs->pad_size              = PAD_SHA;
2268
0
        specs->static_ecdh           = 0;
2269
0
        specs->key_size              = CAMELLIA_256_KEY_SIZE;
2270
0
        specs->block_size            = WC_CAMELLIA_BLOCK_SIZE;
2271
0
        specs->iv_size               = CAMELLIA_IV_SIZE;
2272
2273
0
        break;
2274
0
#endif
2275
2276
#ifdef BUILD_TLS_DH_anon_WITH_AES_128_CBC_SHA
2277
    case TLS_DH_anon_WITH_AES_128_CBC_SHA :
2278
        specs->bulk_cipher_algorithm = wolfssl_aes;
2279
        specs->cipher_type           = block;
2280
        specs->mac_algorithm         = sha_mac;
2281
        specs->kea                   = diffie_hellman_kea;
2282
        specs->sig_algo              = anonymous_sa_algo;
2283
        specs->hash_size             = WC_SHA_DIGEST_SIZE;
2284
        specs->pad_size              = PAD_SHA;
2285
        specs->static_ecdh           = 0;
2286
        specs->key_size              = AES_128_KEY_SIZE;
2287
        specs->block_size            = WC_AES_BLOCK_SIZE;
2288
        specs->iv_size               = AES_IV_SIZE;
2289
2290
        if (opts != NULL)
2291
            opts->usingAnon_cipher   = 1;
2292
        break;
2293
#endif
2294
2295
#ifdef BUILD_WDM_WITH_NULL_SHA256
2296
        case WDM_WITH_NULL_SHA256 :
2297
            specs->bulk_cipher_algorithm = wolfssl_cipher_null;
2298
            specs->cipher_type           = stream;
2299
            specs->mac_algorithm         = sha256_mac;
2300
            specs->kea                   = no_kea;
2301
            specs->sig_algo              = anonymous_sa_algo;
2302
            specs->hash_size             = WC_SHA256_DIGEST_SIZE;
2303
            specs->pad_size              = PAD_SHA;
2304
2305
            break;
2306
#endif
2307
2308
0
    default:
2309
0
        WOLFSSL_MSG("Unsupported cipher suite, SetCipherSpecs");
2310
0
        WOLFSSL_ERROR_VERBOSE(UNSUPPORTED_SUITE);
2311
0
        return UNSUPPORTED_SUITE;
2312
0
    }  /* switch */
2313
0
    }  /* if ECC / Normal suites else */
2314
2315
0
    if (specs->sig_algo == anonymous_sa_algo && opts != NULL) {
2316
        /* CLIENT/SERVER: No peer authentication to be performed. */
2317
0
        opts->peerAuthGood = 1;
2318
0
    }
2319
2320
0
    return 0;
2321
0
}
2322
2323
2324
enum KeyStuff {
2325
    MASTER_ROUNDS = 3,
2326
    PREFIX        = 3,     /* up to three letters for master prefix */
2327
    KEY_PREFIX    = 9      /* up to 9 prefix letters for key rounds */
2328
2329
2330
};
2331
2332
#ifndef NO_OLD_TLS
2333
/* true or false, zero for error */
2334
static int SetPrefix(byte* sha_input, int idx)
2335
{
2336
    switch (idx) {
2337
    case 0:
2338
        XMEMCPY(sha_input, "A", 1);
2339
        break;
2340
    case 1:
2341
        XMEMCPY(sha_input, "BB", 2);
2342
        break;
2343
    case 2:
2344
        XMEMCPY(sha_input, "CCC", 3);
2345
        break;
2346
    case 3:
2347
        XMEMCPY(sha_input, "DDDD", 4);
2348
        break;
2349
    case 4:
2350
        XMEMCPY(sha_input, "EEEEE", 5);
2351
        break;
2352
    case 5:
2353
        XMEMCPY(sha_input, "FFFFFF", 6);
2354
        break;
2355
    case 6:
2356
        XMEMCPY(sha_input, "GGGGGGG", 7);
2357
        break;
2358
    case 7:
2359
        XMEMCPY(sha_input, "HHHHHHHH", 8);
2360
        break;
2361
    case 8:
2362
        XMEMCPY(sha_input, "IIIIIIIII", 9);
2363
        break;
2364
    default:
2365
        WOLFSSL_MSG("Set Prefix error, bad input");
2366
        return 0;
2367
    }
2368
    return 1;
2369
}
2370
#endif
2371
2372
2373
int SetKeys(Ciphers* enc, Ciphers* dec, Keys* keys, CipherSpecs* specs,
2374
                   int side, void* heap, int devId, WC_RNG* rng, int tls13)
2375
{
2376
    (void)rng;
2377
    (void)tls13;
2378
2379
#ifdef BUILD_ARC4
2380
    if (specs->bulk_cipher_algorithm == wolfssl_rc4) {
2381
        word32 sz = specs->key_size;
2382
        if (enc && enc->arc4 == NULL) {
2383
            enc->arc4 = (Arc4*)XMALLOC(sizeof(Arc4), heap, DYNAMIC_TYPE_CIPHER);
2384
            if (enc->arc4 == NULL)
2385
                 return MEMORY_E;
2386
        }
2387
        if (dec && dec->arc4 == NULL) {
2388
            dec->arc4 = (Arc4*)XMALLOC(sizeof(Arc4), heap, DYNAMIC_TYPE_CIPHER);
2389
            if (dec->arc4 == NULL)
2390
                return MEMORY_E;
2391
        }
2392
2393
        if (enc) {
2394
            if (wc_Arc4Init(enc->arc4, heap, devId) != 0) {
2395
                WOLFSSL_MSG("Arc4Init failed in SetKeys");
2396
                return ASYNC_INIT_E;
2397
            }
2398
        }
2399
        if (dec) {
2400
            if (wc_Arc4Init(dec->arc4, heap, devId) != 0) {
2401
                WOLFSSL_MSG("Arc4Init failed in SetKeys");
2402
                return ASYNC_INIT_E;
2403
            }
2404
        }
2405
2406
        if (side == WOLFSSL_CLIENT_END) {
2407
            if (enc)
2408
                wc_Arc4SetKey(enc->arc4, keys->client_write_key, sz);
2409
            if (dec)
2410
                wc_Arc4SetKey(dec->arc4, keys->server_write_key, sz);
2411
        }
2412
        else {
2413
            if (enc)
2414
                wc_Arc4SetKey(enc->arc4, keys->server_write_key, sz);
2415
            if (dec)
2416
                wc_Arc4SetKey(dec->arc4, keys->client_write_key, sz);
2417
        }
2418
        if (enc)
2419
            enc->setup = 1;
2420
        if (dec)
2421
            dec->setup = 1;
2422
    }
2423
#endif /* BUILD_ARC4 */
2424
2425
2426
#if defined(HAVE_CHACHA) && defined(HAVE_POLY1305) && !defined(NO_CHAPOL_AEAD)
2427
    /* Check that the max implicit iv size is sufficient */
2428
    #if (AEAD_MAX_IMP_SZ < 12) /* CHACHA20_IMP_IV_SZ */
2429
        #error AEAD_MAX_IMP_SZ is too small for ChaCha20
2430
    #endif
2431
    #if (MAX_WRITE_IV_SZ < 12) /* CHACHA20_IMP_IV_SZ */
2432
        #error MAX_WRITE_IV_SZ is too small for ChaCha20
2433
    #endif
2434
2435
    if (specs->bulk_cipher_algorithm == wolfssl_chacha) {
2436
        int chachaRet;
2437
        if (enc && enc->chacha == NULL)
2438
            enc->chacha =
2439
                    (ChaCha*)XMALLOC(sizeof(ChaCha), heap, DYNAMIC_TYPE_CIPHER);
2440
        if (enc && enc->chacha == NULL)
2441
            return MEMORY_E;
2442
    #ifdef WOLFSSL_CHECK_MEM_ZERO
2443
        if (enc) {
2444
            wc_MemZero_Add("SSL keys enc chacha", enc->chacha, sizeof(ChaCha));
2445
        }
2446
    #endif
2447
        if (dec && dec->chacha == NULL)
2448
            dec->chacha =
2449
                    (ChaCha*)XMALLOC(sizeof(ChaCha), heap, DYNAMIC_TYPE_CIPHER);
2450
        if (dec && dec->chacha == NULL)
2451
            return MEMORY_E;
2452
    #ifdef WOLFSSL_CHECK_MEM_ZERO
2453
        if (dec) {
2454
            wc_MemZero_Add("SSL keys dec chacha", dec->chacha, sizeof(ChaCha));
2455
        }
2456
    #endif
2457
        if (side == WOLFSSL_CLIENT_END) {
2458
            if (enc) {
2459
                chachaRet = wc_Chacha_SetKey(enc->chacha, keys->client_write_key,
2460
                                          specs->key_size);
2461
                XMEMCPY(keys->aead_enc_imp_IV, keys->client_write_IV,
2462
                        CHACHA20_IMP_IV_SZ);
2463
                if (chachaRet != 0) return chachaRet;
2464
            }
2465
            if (dec) {
2466
                chachaRet = wc_Chacha_SetKey(dec->chacha, keys->server_write_key,
2467
                                          specs->key_size);
2468
                XMEMCPY(keys->aead_dec_imp_IV, keys->server_write_IV,
2469
                        CHACHA20_IMP_IV_SZ);
2470
                if (chachaRet != 0) return chachaRet;
2471
            }
2472
        }
2473
        else {
2474
            if (enc) {
2475
                chachaRet = wc_Chacha_SetKey(enc->chacha, keys->server_write_key,
2476
                                          specs->key_size);
2477
                XMEMCPY(keys->aead_enc_imp_IV, keys->server_write_IV,
2478
                        CHACHA20_IMP_IV_SZ);
2479
                if (chachaRet != 0) return chachaRet;
2480
            }
2481
            if (dec) {
2482
                chachaRet = wc_Chacha_SetKey(dec->chacha, keys->client_write_key,
2483
                                          specs->key_size);
2484
                XMEMCPY(keys->aead_dec_imp_IV, keys->client_write_IV,
2485
                        CHACHA20_IMP_IV_SZ);
2486
                if (chachaRet != 0) return chachaRet;
2487
            }
2488
        }
2489
2490
        if (enc)
2491
            enc->setup = 1;
2492
        if (dec)
2493
            dec->setup = 1;
2494
    }
2495
#endif /* HAVE_CHACHA && HAVE_POLY1305 */
2496
2497
#ifdef BUILD_DES3
2498
    /* check that buffer sizes are sufficient */
2499
    #if (MAX_WRITE_IV_SZ < 8) /* DES_IV_SIZE */
2500
        #error MAX_WRITE_IV_SZ too small for 3DES
2501
    #endif
2502
2503
    if (specs->bulk_cipher_algorithm == wolfssl_triple_des) {
2504
        int desRet = 0;
2505
2506
        if (enc) {
2507
            if (enc->des3 == NULL)
2508
                enc->des3 = (Des3*)XMALLOC(sizeof(Des3), heap, DYNAMIC_TYPE_CIPHER);
2509
            if (enc->des3 == NULL)
2510
                return MEMORY_E;
2511
            XMEMSET(enc->des3, 0, sizeof(Des3));
2512
        }
2513
        if (dec) {
2514
            if (dec->des3 == NULL)
2515
                dec->des3 = (Des3*)XMALLOC(sizeof(Des3), heap, DYNAMIC_TYPE_CIPHER);
2516
            if (dec->des3 == NULL)
2517
                return MEMORY_E;
2518
            XMEMSET(dec->des3, 0, sizeof(Des3));
2519
        }
2520
2521
        if (enc) {
2522
            if (wc_Des3Init(enc->des3, heap, devId) != 0) {
2523
                WOLFSSL_MSG("Des3Init failed in SetKeys");
2524
                return ASYNC_INIT_E;
2525
            }
2526
        }
2527
        if (dec) {
2528
            if (wc_Des3Init(dec->des3, heap, devId) != 0) {
2529
                WOLFSSL_MSG("Des3Init failed in SetKeys");
2530
                return ASYNC_INIT_E;
2531
            }
2532
        }
2533
2534
        if (side == WOLFSSL_CLIENT_END) {
2535
            if (enc) {
2536
                desRet = wc_Des3_SetKey(enc->des3, keys->client_write_key,
2537
                                     keys->client_write_IV, DES_ENCRYPTION);
2538
                if (desRet != 0) return desRet;
2539
            }
2540
            if (dec) {
2541
                desRet = wc_Des3_SetKey(dec->des3, keys->server_write_key,
2542
                                     keys->server_write_IV, DES_DECRYPTION);
2543
                if (desRet != 0) return desRet;
2544
            }
2545
        }
2546
        else {
2547
            if (enc) {
2548
                desRet = wc_Des3_SetKey(enc->des3, keys->server_write_key,
2549
                                     keys->server_write_IV, DES_ENCRYPTION);
2550
                if (desRet != 0) return desRet;
2551
            }
2552
            if (dec) {
2553
                desRet = wc_Des3_SetKey(dec->des3, keys->client_write_key,
2554
                                     keys->client_write_IV, DES_DECRYPTION);
2555
                if (desRet != 0) return desRet;
2556
            }
2557
        }
2558
        if (enc)
2559
            enc->setup = 1;
2560
        if (dec)
2561
            dec->setup = 1;
2562
    }
2563
#endif /* BUILD_DES3 */
2564
2565
#ifdef BUILD_AES
2566
    /* check that buffer sizes are sufficient */
2567
    #if (MAX_WRITE_IV_SZ < 16) /* AES_IV_SIZE */
2568
        #error MAX_WRITE_IV_SZ too small for AES
2569
    #endif
2570
2571
    if (specs->bulk_cipher_algorithm == wolfssl_aes) {
2572
        int aesRet = 0;
2573
2574
        if (enc) {
2575
            if (enc->aes == NULL) {
2576
                enc->aes = (Aes*)XMALLOC(sizeof(Aes), heap, DYNAMIC_TYPE_CIPHER);
2577
                if (enc->aes == NULL)
2578
                    return MEMORY_E;
2579
            } else {
2580
                wc_AesFree(enc->aes);
2581
            }
2582
2583
            XMEMSET(enc->aes, 0, sizeof(Aes));
2584
        }
2585
        if (dec) {
2586
            if (dec->aes == NULL) {
2587
                dec->aes = (Aes*)XMALLOC(sizeof(Aes), heap, DYNAMIC_TYPE_CIPHER);
2588
                if (dec->aes == NULL)
2589
                    return MEMORY_E;
2590
            } else {
2591
                wc_AesFree(dec->aes);
2592
            }
2593
2594
            XMEMSET(dec->aes, 0, sizeof(Aes));
2595
        }
2596
        if (enc) {
2597
            if (wc_AesInit(enc->aes, heap, devId) != 0) {
2598
                WOLFSSL_MSG("AesInit failed in SetKeys");
2599
                return ASYNC_INIT_E;
2600
            }
2601
        }
2602
        if (dec) {
2603
            if (wc_AesInit(dec->aes, heap, devId) != 0) {
2604
                WOLFSSL_MSG("AesInit failed in SetKeys");
2605
                return ASYNC_INIT_E;
2606
            }
2607
        }
2608
2609
        if (side == WOLFSSL_CLIENT_END) {
2610
            if (enc) {
2611
                aesRet = wc_AesSetKey(enc->aes, keys->client_write_key,
2612
                                   specs->key_size, keys->client_write_IV,
2613
                                   AES_ENCRYPTION);
2614
                if (aesRet != 0) return aesRet;
2615
            }
2616
            if (dec) {
2617
                aesRet = wc_AesSetKey(dec->aes, keys->server_write_key,
2618
                                   specs->key_size, keys->server_write_IV,
2619
                                   AES_DECRYPTION);
2620
                if (aesRet != 0) return aesRet;
2621
            }
2622
        }
2623
        else {
2624
            if (enc) {
2625
                aesRet = wc_AesSetKey(enc->aes, keys->server_write_key,
2626
                                   specs->key_size, keys->server_write_IV,
2627
                                   AES_ENCRYPTION);
2628
                if (aesRet != 0) return aesRet;
2629
            }
2630
            if (dec) {
2631
                aesRet = wc_AesSetKey(dec->aes, keys->client_write_key,
2632
                                   specs->key_size, keys->client_write_IV,
2633
                                   AES_DECRYPTION);
2634
                if (aesRet != 0) return aesRet;
2635
            }
2636
        }
2637
        if (enc)
2638
            enc->setup = 1;
2639
        if (dec)
2640
            dec->setup = 1;
2641
    }
2642
#endif /* BUILD_AES */
2643
2644
#ifdef BUILD_AESGCM
2645
    /* check that buffer sizes are sufficient */
2646
    #if (AEAD_MAX_IMP_SZ < 4) /* AESGCM_IMP_IV_SZ */
2647
        #error AEAD_MAX_IMP_SZ too small for AESGCM
2648
    #endif
2649
    #if (AEAD_MAX_EXP_SZ < 8) /* AESGCM_EXP_IV_SZ */
2650
        #error AEAD_MAX_EXP_SZ too small for AESGCM
2651
    #endif
2652
    #if (MAX_WRITE_IV_SZ < 4) /* AESGCM_IMP_IV_SZ */
2653
        #error MAX_WRITE_IV_SZ too small for AESGCM
2654
    #endif
2655
2656
    if (specs->bulk_cipher_algorithm == wolfssl_aes_gcm) {
2657
        int gcmRet;
2658
2659
        if (enc) {
2660
            if (enc->aes == NULL) {
2661
                enc->aes = (Aes*)XMALLOC(sizeof(Aes), heap, DYNAMIC_TYPE_CIPHER);
2662
                if (enc->aes == NULL)
2663
                    return MEMORY_E;
2664
            } else {
2665
                wc_AesFree(enc->aes);
2666
            }
2667
2668
            XMEMSET(enc->aes, 0, sizeof(Aes));
2669
        }
2670
        if (dec) {
2671
            if (dec->aes == NULL) {
2672
                dec->aes = (Aes*)XMALLOC(sizeof(Aes), heap, DYNAMIC_TYPE_CIPHER);
2673
                if (dec->aes == NULL)
2674
                    return MEMORY_E;
2675
            } else {
2676
                wc_AesFree(dec->aes);
2677
            }
2678
2679
            XMEMSET(dec->aes, 0, sizeof(Aes));
2680
        }
2681
2682
        if (enc) {
2683
            if (wc_AesInit(enc->aes, heap, devId) != 0) {
2684
                WOLFSSL_MSG("AesInit failed in SetKeys");
2685
                return ASYNC_INIT_E;
2686
            }
2687
        }
2688
        if (dec) {
2689
            if (wc_AesInit(dec->aes, heap, devId) != 0) {
2690
                WOLFSSL_MSG("AesInit failed in SetKeys");
2691
                return ASYNC_INIT_E;
2692
            }
2693
        }
2694
2695
        if (side == WOLFSSL_CLIENT_END) {
2696
            if (enc) {
2697
                gcmRet = wc_AesGcmSetKey(enc->aes, keys->client_write_key,
2698
                                      specs->key_size);
2699
                if (gcmRet != 0) return gcmRet;
2700
                XMEMCPY(keys->aead_enc_imp_IV, keys->client_write_IV,
2701
                        AEAD_MAX_IMP_SZ);
2702
#if !defined(NO_PUBLIC_GCM_SET_IV) && \
2703
    ((!defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)) || \
2704
    (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2)))
2705
                if (!tls13) {
2706
                    gcmRet = wc_AesGcmSetIV(enc->aes, AESGCM_NONCE_SZ,
2707
                            keys->client_write_IV, AESGCM_IMP_IV_SZ, rng);
2708
                    if (gcmRet != 0) return gcmRet;
2709
                }
2710
#endif
2711
            }
2712
            if (dec) {
2713
                gcmRet = wc_AesGcmSetKey(dec->aes, keys->server_write_key,
2714
                                      specs->key_size);
2715
                if (gcmRet != 0) return gcmRet;
2716
                XMEMCPY(keys->aead_dec_imp_IV, keys->server_write_IV,
2717
                        AEAD_MAX_IMP_SZ);
2718
            }
2719
        }
2720
        else {
2721
            if (enc) {
2722
                gcmRet = wc_AesGcmSetKey(enc->aes, keys->server_write_key,
2723
                                      specs->key_size);
2724
                if (gcmRet != 0) return gcmRet;
2725
                XMEMCPY(keys->aead_enc_imp_IV, keys->server_write_IV,
2726
                        AEAD_MAX_IMP_SZ);
2727
#if !defined(NO_PUBLIC_GCM_SET_IV) && \
2728
    ((!defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)) || \
2729
    (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2)))
2730
                if (!tls13) {
2731
                    gcmRet = wc_AesGcmSetIV(enc->aes, AESGCM_NONCE_SZ,
2732
                            keys->server_write_IV, AESGCM_IMP_IV_SZ, rng);
2733
                    if (gcmRet != 0) return gcmRet;
2734
                }
2735
#endif
2736
            }
2737
            if (dec) {
2738
                gcmRet = wc_AesGcmSetKey(dec->aes, keys->client_write_key,
2739
                                      specs->key_size);
2740
                if (gcmRet != 0) return gcmRet;
2741
                XMEMCPY(keys->aead_dec_imp_IV, keys->client_write_IV,
2742
                        AEAD_MAX_IMP_SZ);
2743
            }
2744
        }
2745
        if (enc)
2746
            enc->setup = 1;
2747
        if (dec)
2748
            dec->setup = 1;
2749
    }
2750
#endif /* BUILD_AESGCM */
2751
2752
#ifdef HAVE_AESCCM
2753
    /* check that buffer sizes are sufficient (CCM is same size as GCM) */
2754
    #if (AEAD_MAX_IMP_SZ < 4) /* AESGCM_IMP_IV_SZ */
2755
        #error AEAD_MAX_IMP_SZ too small for AESCCM
2756
    #endif
2757
    #if (AEAD_MAX_EXP_SZ < 8) /* AESGCM_EXP_IV_SZ */
2758
        #error AEAD_MAX_EXP_SZ too small for AESCCM
2759
    #endif
2760
    #if (MAX_WRITE_IV_SZ < 4) /* AESGCM_IMP_IV_SZ */
2761
        #error MAX_WRITE_IV_SZ too small for AESCCM
2762
    #endif
2763
2764
    if (specs->bulk_cipher_algorithm == wolfssl_aes_ccm) {
2765
        int CcmRet;
2766
2767
        if (enc) {
2768
            if (enc->aes == NULL) {
2769
                enc->aes = (Aes*)XMALLOC(sizeof(Aes), heap, DYNAMIC_TYPE_CIPHER);
2770
                if (enc->aes == NULL)
2771
                    return MEMORY_E;
2772
            } else {
2773
                wc_AesFree(enc->aes);
2774
            }
2775
2776
            XMEMSET(enc->aes, 0, sizeof(Aes));
2777
        }
2778
        if (dec) {
2779
            if (dec->aes == NULL) {
2780
                dec->aes = (Aes*)XMALLOC(sizeof(Aes), heap, DYNAMIC_TYPE_CIPHER);
2781
                if (dec->aes == NULL)
2782
                return MEMORY_E;
2783
            } else {
2784
                wc_AesFree(dec->aes);
2785
            }
2786
            XMEMSET(dec->aes, 0, sizeof(Aes));
2787
        }
2788
2789
        if (enc) {
2790
            if (wc_AesInit(enc->aes, heap, devId) != 0) {
2791
                WOLFSSL_MSG("AesInit failed in SetKeys");
2792
                return ASYNC_INIT_E;
2793
            }
2794
        }
2795
        if (dec) {
2796
            if (wc_AesInit(dec->aes, heap, devId) != 0) {
2797
                WOLFSSL_MSG("AesInit failed in SetKeys");
2798
                return ASYNC_INIT_E;
2799
            }
2800
        }
2801
2802
        if (side == WOLFSSL_CLIENT_END) {
2803
            if (enc) {
2804
                CcmRet = wc_AesCcmSetKey(enc->aes, keys->client_write_key,
2805
                                         specs->key_size);
2806
                if (CcmRet != 0) {
2807
                    return CcmRet;
2808
                }
2809
                XMEMCPY(keys->aead_enc_imp_IV, keys->client_write_IV,
2810
                        AEAD_MAX_IMP_SZ);
2811
#if !defined(NO_PUBLIC_CCM_SET_NONCE) && \
2812
    ((!defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)) || \
2813
    (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2)))
2814
                if (!tls13) {
2815
                    CcmRet = wc_AesCcmSetNonce(enc->aes, keys->client_write_IV,
2816
                            AEAD_MAX_IMP_SZ);
2817
                    if (CcmRet != 0) return CcmRet;
2818
                }
2819
#endif
2820
            }
2821
            if (dec) {
2822
                CcmRet = wc_AesCcmSetKey(dec->aes, keys->server_write_key,
2823
                                         specs->key_size);
2824
                if (CcmRet != 0) {
2825
                    return CcmRet;
2826
                }
2827
                XMEMCPY(keys->aead_dec_imp_IV, keys->server_write_IV,
2828
                        AEAD_MAX_IMP_SZ);
2829
            }
2830
        }
2831
        else {
2832
            if (enc) {
2833
                CcmRet = wc_AesCcmSetKey(enc->aes, keys->server_write_key,
2834
                                         specs->key_size);
2835
                if (CcmRet != 0) {
2836
                    return CcmRet;
2837
                }
2838
                XMEMCPY(keys->aead_enc_imp_IV, keys->server_write_IV,
2839
                        AEAD_MAX_IMP_SZ);
2840
#if !defined(NO_PUBLIC_CCM_SET_NONCE) && \
2841
    ((!defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)) || \
2842
    (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2)))
2843
                if (!tls13) {
2844
                    CcmRet = wc_AesCcmSetNonce(enc->aes, keys->server_write_IV,
2845
                            AEAD_MAX_IMP_SZ);
2846
                    if (CcmRet != 0) return CcmRet;
2847
                }
2848
#endif
2849
            }
2850
            if (dec) {
2851
                CcmRet = wc_AesCcmSetKey(dec->aes, keys->client_write_key,
2852
                                         specs->key_size);
2853
                if (CcmRet != 0) {
2854
                    return CcmRet;
2855
                }
2856
                XMEMCPY(keys->aead_dec_imp_IV, keys->client_write_IV,
2857
                        AEAD_MAX_IMP_SZ);
2858
            }
2859
        }
2860
        if (enc)
2861
            enc->setup = 1;
2862
        if (dec)
2863
            dec->setup = 1;
2864
    }
2865
#endif /* HAVE_AESCCM */
2866
2867
#ifdef HAVE_ARIA
2868
    /* check that buffer sizes are sufficient */
2869
    #if (MAX_WRITE_IV_SZ < 16) /* AES_IV_SIZE */
2870
        #error MAX_WRITE_IV_SZ too small for AES
2871
    #endif
2872
2873
    if (specs->bulk_cipher_algorithm == wolfssl_aria_gcm) {
2874
        int ret = 0;
2875
        MC_ALGID algo;
2876
2877
        switch(specs->key_size) {
2878
            case ARIA_128_KEY_SIZE:
2879
                algo = MC_ALGID_ARIA_128BITKEY;
2880
                break;
2881
            case ARIA_192_KEY_SIZE:
2882
                algo = MC_ALGID_ARIA_192BITKEY;
2883
                break;
2884
            case ARIA_256_KEY_SIZE:
2885
                algo = MC_ALGID_ARIA_256BITKEY;
2886
                break;
2887
            default:
2888
                return WOLFSSL_NOT_IMPLEMENTED; /* This should never happen */
2889
        }
2890
2891
        if (enc) {
2892
            if (enc->aria == NULL) {
2893
                enc->aria = (wc_Aria*)XMALLOC(sizeof(wc_Aria), heap, DYNAMIC_TYPE_CIPHER);
2894
                if (enc->aria == NULL)
2895
                    return MEMORY_E;
2896
            } else {
2897
                wc_AriaFreeCrypt(enc->aria);
2898
            }
2899
2900
            XMEMSET(enc->aria, 0, sizeof(wc_Aria));
2901
            if (wc_AriaInitCrypt(enc->aria, algo) != 0) {
2902
                WOLFSSL_MSG("AriaInit failed in SetKeys");
2903
                return ASYNC_INIT_E;
2904
            }
2905
        }
2906
        if (dec) {
2907
            if (dec->aria == NULL) {
2908
                dec->aria = (wc_Aria*)XMALLOC(sizeof(wc_Aria), heap, DYNAMIC_TYPE_CIPHER);
2909
                if (dec->aria == NULL)
2910
                    return MEMORY_E;
2911
            } else {
2912
                wc_AriaFreeCrypt(dec->aria);
2913
            }
2914
2915
            XMEMSET(dec->aria, 0, sizeof(wc_Aria));
2916
            if (wc_AriaInitCrypt(dec->aria, algo) != 0) {
2917
                WOLFSSL_MSG("AriaInit failed in SetKeys");
2918
                return ASYNC_INIT_E;
2919
            }
2920
        }
2921
2922
        if (side == WOLFSSL_CLIENT_END) {
2923
            if (enc) {
2924
                ret = wc_AriaSetKey(enc->aria, keys->client_write_key);
2925
                if (ret != 0) return ret;
2926
                XMEMCPY(keys->aead_enc_imp_IV, keys->client_write_IV,
2927
                        AEAD_MAX_IMP_SZ);
2928
                if (!tls13) {
2929
                    ret = wc_AriaGcmSetIV(enc->aria, AESGCM_NONCE_SZ,
2930
                            keys->client_write_IV, AESGCM_IMP_IV_SZ, rng);
2931
                    if (ret != 0) return ret;
2932
                }
2933
            }
2934
            if (dec) {
2935
                ret = wc_AriaSetKey(dec->aria, keys->server_write_key);
2936
                if (ret != 0) return ret;
2937
                XMEMCPY(keys->aead_dec_imp_IV, keys->server_write_IV,
2938
                        AEAD_MAX_IMP_SZ);
2939
            }
2940
        }
2941
        else {
2942
            if (enc) {
2943
                ret = wc_AriaSetKey(enc->aria, keys->server_write_key);
2944
                if (ret != 0) return ret;
2945
                XMEMCPY(keys->aead_enc_imp_IV, keys->server_write_IV,
2946
                        AEAD_MAX_IMP_SZ);
2947
                if (!tls13) {
2948
                    ret = wc_AriaGcmSetIV(enc->aria, AESGCM_NONCE_SZ,
2949
                            keys->server_write_IV, AESGCM_IMP_IV_SZ, rng);
2950
                    if (ret != 0) return ret;
2951
                }
2952
            }
2953
            if (dec) {
2954
                ret = wc_AriaSetKey(dec->aria, keys->client_write_key);
2955
                if (ret != 0) return ret;
2956
                XMEMCPY(keys->aead_dec_imp_IV, keys->client_write_IV,
2957
                        AEAD_MAX_IMP_SZ);
2958
            }
2959
        }
2960
        if (enc)
2961
            enc->setup = 1;
2962
        if (dec)
2963
            dec->setup = 1;
2964
    }
2965
#endif /* HAVE_ARIA */
2966
2967
#ifdef HAVE_CAMELLIA
2968
    /* check that buffer sizes are sufficient */
2969
    #if (MAX_WRITE_IV_SZ < 16) /* CAMELLIA_IV_SIZE */
2970
        #error MAX_WRITE_IV_SZ too small for CAMELLIA
2971
    #endif
2972
2973
    if (specs->bulk_cipher_algorithm == wolfssl_camellia) {
2974
        int camRet;
2975
2976
        if (enc && enc->cam == NULL)
2977
            enc->cam =
2978
                (wc_Camellia*)XMALLOC(sizeof(wc_Camellia), heap, DYNAMIC_TYPE_CIPHER);
2979
        if (enc && enc->cam == NULL)
2980
            return MEMORY_E;
2981
2982
        if (dec && dec->cam == NULL)
2983
            dec->cam =
2984
                (wc_Camellia*)XMALLOC(sizeof(wc_Camellia), heap, DYNAMIC_TYPE_CIPHER);
2985
        if (dec && dec->cam == NULL)
2986
            return MEMORY_E;
2987
2988
        if (side == WOLFSSL_CLIENT_END) {
2989
            if (enc) {
2990
                camRet = wc_CamelliaSetKey(enc->cam, keys->client_write_key,
2991
                                        specs->key_size, keys->client_write_IV);
2992
                if (camRet != 0) return camRet;
2993
            }
2994
            if (dec) {
2995
                camRet = wc_CamelliaSetKey(dec->cam, keys->server_write_key,
2996
                                        specs->key_size, keys->server_write_IV);
2997
                if (camRet != 0) return camRet;
2998
            }
2999
        }
3000
        else {
3001
            if (enc) {
3002
                camRet = wc_CamelliaSetKey(enc->cam, keys->server_write_key,
3003
                                        specs->key_size, keys->server_write_IV);
3004
                if (camRet != 0) return camRet;
3005
            }
3006
            if (dec) {
3007
                camRet = wc_CamelliaSetKey(dec->cam, keys->client_write_key,
3008
                                        specs->key_size, keys->client_write_IV);
3009
                if (camRet != 0) return camRet;
3010
            }
3011
        }
3012
        if (enc)
3013
            enc->setup = 1;
3014
        if (dec)
3015
            dec->setup = 1;
3016
    }
3017
#endif /* HAVE_CAMELLIA */
3018
3019
#ifdef WOLFSSL_SM4_CBC
3020
    /* check that buffer sizes are sufficient */
3021
    #if (MAX_WRITE_IV_SZ < 16) /* AES_IV_SIZE */
3022
        #error MAX_WRITE_IV_SZ too small for SM4_CBC
3023
    #endif
3024
3025
    if (specs->bulk_cipher_algorithm == wolfssl_sm4_cbc) {
3026
        int sm4Ret = 0;
3027
3028
        if (enc) {
3029
            if (enc->sm4 == NULL) {
3030
                enc->sm4 = (wc_Sm4*)XMALLOC(sizeof(wc_Sm4), heap,
3031
                    DYNAMIC_TYPE_CIPHER);
3032
                if (enc->sm4 == NULL)
3033
                    return MEMORY_E;
3034
            }
3035
            else {
3036
                wc_Sm4Free(enc->sm4);
3037
            }
3038
3039
            XMEMSET(enc->sm4, 0, sizeof(wc_Sm4));
3040
        }
3041
        if (dec) {
3042
            if (dec->sm4 == NULL) {
3043
                dec->sm4 = (wc_Sm4*)XMALLOC(sizeof(wc_Sm4), heap,
3044
                    DYNAMIC_TYPE_CIPHER);
3045
                if (dec->sm4 == NULL)
3046
                    return MEMORY_E;
3047
            }
3048
            else {
3049
                wc_Sm4Free(dec->sm4);
3050
            }
3051
3052
            XMEMSET(dec->sm4, 0, sizeof(wc_Sm4));
3053
        }
3054
        if (enc) {
3055
            if (wc_Sm4Init(enc->sm4, heap, devId) != 0) {
3056
                WOLFSSL_MSG("Sm4Init failed in SetKeys");
3057
                return ASYNC_INIT_E;
3058
            }
3059
        }
3060
        if (dec) {
3061
            if (wc_Sm4Init(dec->sm4, heap, devId) != 0) {
3062
                WOLFSSL_MSG("Sm4Init failed in SetKeys");
3063
                return ASYNC_INIT_E;
3064
            }
3065
        }
3066
3067
        if (side == WOLFSSL_CLIENT_END) {
3068
            if (enc) {
3069
                sm4Ret = wc_Sm4SetKey(enc->sm4, keys->client_write_key,
3070
                    specs->key_size);
3071
                if (sm4Ret != 0) return sm4Ret;
3072
                sm4Ret = wc_Sm4SetIV(enc->sm4, keys->client_write_IV);
3073
                if (sm4Ret != 0) return sm4Ret;
3074
            }
3075
            if (dec) {
3076
                sm4Ret = wc_Sm4SetKey(dec->sm4, keys->server_write_key,
3077
                    specs->key_size);
3078
                if (sm4Ret != 0) return sm4Ret;
3079
                sm4Ret = wc_Sm4SetIV(dec->sm4, keys->server_write_IV);
3080
                if (sm4Ret != 0) return sm4Ret;
3081
            }
3082
        }
3083
        else {
3084
            if (enc) {
3085
                sm4Ret = wc_Sm4SetKey(enc->sm4, keys->server_write_key,
3086
                    specs->key_size);
3087
                if (sm4Ret != 0) return sm4Ret;
3088
                sm4Ret = wc_Sm4SetIV(enc->sm4, keys->server_write_IV);
3089
                if (sm4Ret != 0) return sm4Ret;
3090
            }
3091
            if (dec) {
3092
                sm4Ret = wc_Sm4SetKey(dec->sm4, keys->client_write_key,
3093
                    specs->key_size);
3094
                if (sm4Ret != 0) return sm4Ret;
3095
                sm4Ret = wc_Sm4SetIV(dec->sm4, keys->client_write_IV);
3096
                if (sm4Ret != 0) return sm4Ret;
3097
            }
3098
        }
3099
        if (enc)
3100
            enc->setup = 1;
3101
        if (dec)
3102
            dec->setup = 1;
3103
    }
3104
#endif /* WOLFSSL_SM4_CBC */
3105
3106
#ifdef WOLFSSL_SM4_GCM
3107
    /* check that buffer sizes are sufficient */
3108
    #if (AEAD_MAX_IMP_SZ < 4) /* SM4-GCM_IMP_IV_SZ */
3109
        #error AEAD_MAX_IMP_SZ too small for SM4-GCM
3110
    #endif
3111
    #if (AEAD_MAX_EXP_SZ < 8) /* SM4-GCM_EXP_IV_SZ */
3112
        #error AEAD_MAX_EXP_SZ too small for SM4-GCM
3113
    #endif
3114
    #if (MAX_WRITE_IV_SZ < 4) /* SM4-GCM_IMP_IV_SZ */
3115
        #error MAX_WRITE_IV_SZ too small for SM4-GCM
3116
    #endif
3117
3118
    if (specs->bulk_cipher_algorithm == wolfssl_sm4_gcm) {
3119
        int gcmRet;
3120
3121
        if (enc) {
3122
            if (enc->sm4 == NULL) {
3123
                enc->sm4 = (wc_Sm4*)XMALLOC(sizeof(wc_Sm4), heap,
3124
                                            DYNAMIC_TYPE_CIPHER);
3125
                if (enc->sm4 == NULL)
3126
                    return MEMORY_E;
3127
            } else {
3128
                wc_Sm4Free(enc->sm4);
3129
            }
3130
3131
            XMEMSET(enc->sm4, 0, sizeof(wc_Sm4));
3132
        }
3133
        if (dec) {
3134
            if (dec->sm4 == NULL) {
3135
                dec->sm4 = (wc_Sm4*)XMALLOC(sizeof(wc_Sm4), heap,
3136
                                            DYNAMIC_TYPE_CIPHER);
3137
                if (dec->sm4 == NULL)
3138
                    return MEMORY_E;
3139
            } else {
3140
                wc_Sm4Free(dec->sm4);
3141
            }
3142
3143
            XMEMSET(dec->sm4, 0, sizeof(wc_Sm4));
3144
        }
3145
3146
        if (enc) {
3147
            if (wc_Sm4Init(enc->sm4, heap, devId) != 0) {
3148
                WOLFSSL_MSG("Sm4Init failed in SetKeys");
3149
                return ASYNC_INIT_E;
3150
            }
3151
        }
3152
        if (dec) {
3153
            if (wc_Sm4Init(dec->sm4, heap, devId) != 0) {
3154
                WOLFSSL_MSG("Sm4Init failed in SetKeys");
3155
                return ASYNC_INIT_E;
3156
            }
3157
        }
3158
3159
        if (side == WOLFSSL_CLIENT_END) {
3160
            if (enc) {
3161
                gcmRet = wc_Sm4GcmSetKey(enc->sm4, keys->client_write_key,
3162
                                      specs->key_size);
3163
                if (gcmRet != 0) return gcmRet;
3164
                XMEMCPY(keys->aead_enc_imp_IV, keys->client_write_IV,
3165
                        AEAD_MAX_IMP_SZ);
3166
            }
3167
            if (dec) {
3168
                gcmRet = wc_Sm4GcmSetKey(dec->sm4, keys->server_write_key,
3169
                                      specs->key_size);
3170
                if (gcmRet != 0) return gcmRet;
3171
                XMEMCPY(keys->aead_dec_imp_IV, keys->server_write_IV,
3172
                        AEAD_MAX_IMP_SZ);
3173
            }
3174
        }
3175
        else {
3176
            if (enc) {
3177
                gcmRet = wc_Sm4GcmSetKey(enc->sm4, keys->server_write_key,
3178
                                      specs->key_size);
3179
                if (gcmRet != 0) return gcmRet;
3180
                XMEMCPY(keys->aead_enc_imp_IV, keys->server_write_IV,
3181
                        AEAD_MAX_IMP_SZ);
3182
            }
3183
            if (dec) {
3184
                gcmRet = wc_Sm4GcmSetKey(dec->sm4, keys->client_write_key,
3185
                                      specs->key_size);
3186
                if (gcmRet != 0) return gcmRet;
3187
                XMEMCPY(keys->aead_dec_imp_IV, keys->client_write_IV,
3188
                        AEAD_MAX_IMP_SZ);
3189
            }
3190
        }
3191
        if (enc)
3192
            enc->setup = 1;
3193
        if (dec)
3194
            dec->setup = 1;
3195
    }
3196
#endif /* WOLFSSL_SM4_GCM */
3197
3198
#ifdef WOLFSSL_SM4_CCM
3199
    /* check that buffer sizes are sufficient (CCM is same size as GCM) */
3200
    #if (AEAD_MAX_IMP_SZ < 4) /* SM4-CCM_IMP_IV_SZ */
3201
        #error AEAD_MAX_IMP_SZ too small for SM4-CCM
3202
    #endif
3203
    #if (AEAD_MAX_EXP_SZ < 8) /* SM4-CCM_EXP_IV_SZ */
3204
        #error AEAD_MAX_EXP_SZ too small for SM4-CCM
3205
    #endif
3206
    #if (MAX_WRITE_IV_SZ < 4) /* SM4-CCM_IMP_IV_SZ */
3207
        #error MAX_WRITE_IV_SZ too small for SM4-CCM
3208
    #endif
3209
3210
    if (specs->bulk_cipher_algorithm == wolfssl_sm4_ccm) {
3211
        int CcmRet;
3212
3213
        if (enc) {
3214
            if (enc->sm4 == NULL) {
3215
                enc->sm4 = (wc_Sm4*)XMALLOC(sizeof(wc_Sm4), heap,
3216
                                            DYNAMIC_TYPE_CIPHER);
3217
                if (enc->sm4 == NULL)
3218
                    return MEMORY_E;
3219
            } else {
3220
                wc_Sm4Free(enc->sm4);
3221
            }
3222
3223
            XMEMSET(enc->sm4, 0, sizeof(wc_Sm4));
3224
        }
3225
        if (dec) {
3226
            if (dec->sm4 == NULL) {
3227
                dec->sm4 = (wc_Sm4*)XMALLOC(sizeof(wc_Sm4), heap,
3228
                                            DYNAMIC_TYPE_CIPHER);
3229
                if (dec->sm4 == NULL)
3230
                return MEMORY_E;
3231
            } else {
3232
                wc_Sm4Free(dec->sm4);
3233
            }
3234
            XMEMSET(dec->sm4, 0, sizeof(wc_Sm4));
3235
        }
3236
3237
        if (enc) {
3238
            if (wc_Sm4Init(enc->sm4, heap, devId) != 0) {
3239
                WOLFSSL_MSG("Sm4Init failed in SetKeys");
3240
                return ASYNC_INIT_E;
3241
            }
3242
        }
3243
        if (dec) {
3244
            if (wc_Sm4Init(dec->sm4, heap, devId) != 0) {
3245
                WOLFSSL_MSG("Sm4Init failed in SetKeys");
3246
                return ASYNC_INIT_E;
3247
            }
3248
        }
3249
3250
        if (side == WOLFSSL_CLIENT_END) {
3251
            if (enc) {
3252
                CcmRet = wc_Sm4SetKey(enc->sm4, keys->client_write_key,
3253
                                      specs->key_size);
3254
                if (CcmRet != 0) {
3255
                    return CcmRet;
3256
                }
3257
                XMEMCPY(keys->aead_enc_imp_IV, keys->client_write_IV,
3258
                        AEAD_MAX_IMP_SZ);
3259
            }
3260
            if (dec) {
3261
                CcmRet = wc_Sm4SetKey(dec->sm4, keys->server_write_key,
3262
                                      specs->key_size);
3263
                if (CcmRet != 0) {
3264
                    return CcmRet;
3265
                }
3266
                XMEMCPY(keys->aead_dec_imp_IV, keys->server_write_IV,
3267
                        AEAD_MAX_IMP_SZ);
3268
            }
3269
        }
3270
        else {
3271
            if (enc) {
3272
                CcmRet = wc_Sm4SetKey(enc->sm4, keys->server_write_key,
3273
                                      specs->key_size);
3274
                if (CcmRet != 0) {
3275
                    return CcmRet;
3276
                }
3277
                XMEMCPY(keys->aead_enc_imp_IV, keys->server_write_IV,
3278
                        AEAD_MAX_IMP_SZ);
3279
            }
3280
            if (dec) {
3281
                CcmRet = wc_Sm4SetKey(dec->sm4, keys->client_write_key,
3282
                                      specs->key_size);
3283
                if (CcmRet != 0) {
3284
                    return CcmRet;
3285
                }
3286
                XMEMCPY(keys->aead_dec_imp_IV, keys->client_write_IV,
3287
                        AEAD_MAX_IMP_SZ);
3288
            }
3289
        }
3290
        if (enc)
3291
            enc->setup = 1;
3292
        if (dec)
3293
            dec->setup = 1;
3294
    }
3295
#endif /* WOLFSSL_SM4_CCM */
3296
3297
#ifdef HAVE_NULL_CIPHER
3298
    if (specs->bulk_cipher_algorithm == wolfssl_cipher_null) {
3299
    #ifdef WOLFSSL_TLS13
3300
        if (tls13) {
3301
            int hmacRet;
3302
            int hashType = WC_HASH_TYPE_NONE;
3303
3304
            switch (specs->mac_algorithm) {
3305
                case sha256_mac:
3306
                    hashType = WC_SHA256;
3307
                    break;
3308
                case sha384_mac:
3309
                    hashType = WC_SHA384;
3310
                    break;
3311
                default:
3312
                    break;
3313
            }
3314
3315
            if (enc && enc->hmac == NULL) {
3316
                enc->hmac = (Hmac*)XMALLOC(sizeof(Hmac), heap,
3317
                                                           DYNAMIC_TYPE_CIPHER);
3318
                if (enc->hmac == NULL)
3319
                    return MEMORY_E;
3320
3321
                if (wc_HmacInit(enc->hmac, heap, devId) != 0) {
3322
                    WOLFSSL_MSG("HmacInit failed in SetKeys");
3323
                    XFREE(enc->hmac, heap, DYNAMIC_TYPE_CIPHER);
3324
                    enc->hmac = NULL;
3325
                    return ASYNC_INIT_E;
3326
                }
3327
            }
3328
3329
            if (dec && dec->hmac == NULL) {
3330
                dec->hmac = (Hmac*)XMALLOC(sizeof(Hmac), heap,
3331
                                                           DYNAMIC_TYPE_CIPHER);
3332
                if (dec->hmac == NULL)
3333
                    return MEMORY_E;
3334
3335
                if (wc_HmacInit(dec->hmac, heap, devId) != 0) {
3336
                    WOLFSSL_MSG("HmacInit failed in SetKeys");
3337
                    XFREE(dec->hmac, heap, DYNAMIC_TYPE_CIPHER);
3338
                    dec->hmac = NULL;
3339
                    return ASYNC_INIT_E;
3340
                }
3341
            }
3342
3343
            if (side == WOLFSSL_CLIENT_END) {
3344
                if (enc) {
3345
                    XMEMCPY(keys->aead_enc_imp_IV, keys->client_write_IV,
3346
                            HMAC_NONCE_SZ);
3347
                    hmacRet = wc_HmacSetKey(enc->hmac, hashType,
3348
                                       keys->client_write_key, specs->key_size);
3349
                    if (hmacRet != 0) return hmacRet;
3350
                }
3351
                if (dec) {
3352
                    XMEMCPY(keys->aead_dec_imp_IV, keys->server_write_IV,
3353
                            HMAC_NONCE_SZ);
3354
                    hmacRet = wc_HmacSetKey(dec->hmac, hashType,
3355
                                       keys->server_write_key, specs->key_size);
3356
                    if (hmacRet != 0) return hmacRet;
3357
                }
3358
            }
3359
            else {
3360
                if (enc) {
3361
                    XMEMCPY(keys->aead_enc_imp_IV, keys->server_write_IV,
3362
                            HMAC_NONCE_SZ);
3363
                    hmacRet = wc_HmacSetKey(enc->hmac, hashType,
3364
                                       keys->server_write_key, specs->key_size);
3365
                    if (hmacRet != 0) return hmacRet;
3366
                }
3367
                if (dec) {
3368
                    XMEMCPY(keys->aead_dec_imp_IV, keys->client_write_IV,
3369
                            HMAC_NONCE_SZ);
3370
                    hmacRet = wc_HmacSetKey(dec->hmac, hashType,
3371
                                       keys->client_write_key, specs->key_size);
3372
                    if (hmacRet != 0) return hmacRet;
3373
                }
3374
            }
3375
        }
3376
    #endif
3377
        if (enc)
3378
            enc->setup = 1;
3379
        if (dec)
3380
            dec->setup = 1;
3381
    }
3382
#endif
3383
3384
    if (enc) {
3385
        keys->sequence_number_hi      = 0;
3386
        keys->sequence_number_lo      = 0;
3387
    }
3388
    if (dec) {
3389
        keys->peer_sequence_number_hi = 0;
3390
        keys->peer_sequence_number_lo = 0;
3391
    }
3392
    (void)side;
3393
    (void)heap;
3394
    (void)enc;
3395
    (void)dec;
3396
    (void)specs;
3397
    (void)devId;
3398
3399
    return 0;
3400
}
3401
3402
3403
#ifdef HAVE_ONE_TIME_AUTH
3404
/* set one time authentication keys */
3405
static int SetAuthKeys(OneTimeAuth* authentication, Keys* keys,
3406
                       CipherSpecs* specs, void* heap, int devId)
3407
390
{
3408
3409
390
#ifdef HAVE_POLY1305
3410
        /* set up memory space for poly1305 */
3411
390
        if (authentication && authentication->poly1305 == NULL)
3412
390
            authentication->poly1305 =
3413
390
                (Poly1305*)XMALLOC(sizeof(Poly1305), heap, DYNAMIC_TYPE_CIPHER);
3414
390
        if (authentication && authentication->poly1305 == NULL)
3415
2
            return MEMORY_E;
3416
    #ifdef WOLFSSL_CHECK_MEM_ZERO
3417
        wc_MemZero_Add("SSL auth keys poly1305", authentication->poly1305,
3418
            sizeof(Poly1305));
3419
    #endif
3420
388
        if (authentication)
3421
388
            authentication->setup = 1;
3422
388
#endif
3423
388
        (void)authentication;
3424
388
        (void)heap;
3425
388
        (void)keys;
3426
388
        (void)specs;
3427
388
        (void)devId;
3428
3429
388
        return 0;
3430
390
}
3431
#endif /* HAVE_ONE_TIME_AUTH */
3432
3433
#ifdef HAVE_SECURE_RENEGOTIATION
3434
/* function name is for cache_status++
3435
 * This function was added because of error incrementing enum type when
3436
 * compiling with a C++ compiler.
3437
 */
3438
static void CacheStatusPP(SecureRenegotiation* cache)
3439
{
3440
    switch (cache->cache_status) {
3441
        case SCR_CACHE_NULL:
3442
            cache->cache_status = SCR_CACHE_NEEDED;
3443
            break;
3444
3445
        case SCR_CACHE_NEEDED:
3446
            cache->cache_status = SCR_CACHE_COPY;
3447
            break;
3448
3449
        case SCR_CACHE_COPY:
3450
            cache->cache_status = SCR_CACHE_PARTIAL;
3451
            break;
3452
3453
        case SCR_CACHE_PARTIAL:
3454
            cache->cache_status = SCR_CACHE_COMPLETE;
3455
            break;
3456
3457
        case SCR_CACHE_COMPLETE:
3458
            WOLFSSL_MSG("SCR Cache state Complete");
3459
            break;
3460
3461
        default:
3462
            WOLFSSL_MSG("Unknown cache state!!");
3463
    }
3464
}
3465
#endif /* HAVE_SECURE_RENEGOTIATION */
3466
3467
3468
/* Set wc_encrypt/wc_decrypt or both sides of key setup
3469
 * note: use wc_encrypt to avoid shadowing global encrypt
3470
 * declared in unistd.h
3471
 */
3472
int SetKeysSide(WOLFSSL* ssl, enum encrypt_side side)
3473
{
3474
    int ret, copy = 0;
3475
    Ciphers* wc_encrypt = NULL;
3476
    Ciphers* wc_decrypt = NULL;
3477
    Keys*    keys    = &ssl->keys;
3478
3479
    (void)copy;
3480
3481
#ifdef HAVE_SECURE_RENEGOTIATION
3482
    if (ssl->secure_renegotiation &&
3483
            ssl->secure_renegotiation->cache_status != SCR_CACHE_NULL) {
3484
        keys = &ssl->secure_renegotiation->tmp_keys;
3485
#ifdef WOLFSSL_DTLS
3486
        /* For DTLS, copy is done in StoreKeys */
3487
        if (!ssl->options.dtls)
3488
#endif
3489
            copy = 1;
3490
    }
3491
#endif /* HAVE_SECURE_RENEGOTIATION */
3492
3493
    switch (side) {
3494
        case ENCRYPT_SIDE_ONLY:
3495
#ifdef WOLFSSL_DEBUG_TLS
3496
            WOLFSSL_MSG("Provisioning ENCRYPT key");
3497
            if (ssl->options.side == WOLFSSL_CLIENT_END) {
3498
                WOLFSSL_BUFFER(keys->client_write_key, ssl->specs.key_size);
3499
            }
3500
            else {
3501
                WOLFSSL_BUFFER(keys->server_write_key, ssl->specs.key_size);
3502
            }
3503
#endif
3504
            wc_encrypt = &ssl->encrypt;
3505
            break;
3506
3507
        case DECRYPT_SIDE_ONLY:
3508
#ifdef WOLFSSL_DEBUG_TLS
3509
            WOLFSSL_MSG("Provisioning DECRYPT key");
3510
            if (ssl->options.side == WOLFSSL_CLIENT_END) {
3511
                WOLFSSL_BUFFER(keys->server_write_key, ssl->specs.key_size);
3512
            }
3513
            else {
3514
                WOLFSSL_BUFFER(keys->client_write_key, ssl->specs.key_size);
3515
            }
3516
#endif
3517
            wc_decrypt = &ssl->decrypt;
3518
            break;
3519
3520
        case ENCRYPT_AND_DECRYPT_SIDE:
3521
#ifdef WOLFSSL_DEBUG_TLS
3522
            WOLFSSL_MSG("Provisioning ENCRYPT key");
3523
            if (ssl->options.side == WOLFSSL_CLIENT_END) {
3524
                WOLFSSL_BUFFER(keys->client_write_key, ssl->specs.key_size);
3525
            }
3526
            else {
3527
                WOLFSSL_BUFFER(keys->server_write_key, ssl->specs.key_size);
3528
            }
3529
            WOLFSSL_MSG("Provisioning DECRYPT key");
3530
            if (ssl->options.side == WOLFSSL_CLIENT_END) {
3531
                WOLFSSL_BUFFER(keys->server_write_key, ssl->specs.key_size);
3532
            }
3533
            else {
3534
                WOLFSSL_BUFFER(keys->client_write_key, ssl->specs.key_size);
3535
            }
3536
#endif
3537
            wc_encrypt = &ssl->encrypt;
3538
            wc_decrypt = &ssl->decrypt;
3539
            break;
3540
3541
        default:
3542
            return BAD_FUNC_ARG;
3543
    }
3544
3545
#ifdef HAVE_ONE_TIME_AUTH
3546
    if (!ssl->auth.setup && ssl->specs.bulk_cipher_algorithm == wolfssl_chacha){
3547
        ret = SetAuthKeys(&ssl->auth, keys, &ssl->specs, ssl->heap, ssl->devId);
3548
        if (ret != 0)
3549
           return ret;
3550
    }
3551
#endif
3552
3553
#if !defined(NO_CERTS) && defined(HAVE_PK_CALLBACKS)
3554
    ret = PROTOCOLCB_UNAVAILABLE;
3555
    if (ssl->ctx->EncryptKeysCb) {
3556
        void* ctx = wolfSSL_GetEncryptKeysCtx(ssl);
3557
        #if defined(WOLFSSL_RENESAS_FSPSM_TLS)
3558
            FSPSM_ST* cbInfo = (FSPSM_ST*)ctx;
3559
            cbInfo->side = side;
3560
        #elif defined(WOLFSSL_RENESAS_TSIP_TLS)
3561
            TsipUserCtx* cbInfo = (TsipUserCtx*)ctx;
3562
            cbInfo->internal->key_side = side;
3563
        #endif
3564
        ret = ssl->ctx->EncryptKeysCb(ssl, ctx);
3565
    }
3566
    if (!ssl->ctx->EncryptKeysCb ||
3567
        ret == WC_NO_ERR_TRACE(PROTOCOLCB_UNAVAILABLE))
3568
#endif
3569
    {
3570
        ret = SetKeys(wc_encrypt, wc_decrypt, keys, &ssl->specs, ssl->options.side,
3571
                      ssl->heap, ssl->devId, ssl->rng, ssl->options.tls1_3);
3572
    }
3573
3574
#ifdef WOLFSSL_DTLS13
3575
    if (ret == 0 && ssl->options.dtls && IsAtLeastTLSv1_3(ssl->version))
3576
        ret = Dtls13SetRecordNumberKeys(ssl, side);
3577
#endif /* WOLFSSL_DTLS13 */
3578
#ifdef WOLFSSL_QUIC
3579
    if (ret == 0 && WOLFSSL_IS_QUIC(ssl)) {
3580
        ret = wolfSSL_quic_keys_active(ssl, side);
3581
    }
3582
#endif /* WOLFSSL_QUIC */
3583
3584
#ifdef HAVE_SECURE_RENEGOTIATION
3585
#ifdef WOLFSSL_DTLS
3586
    if (ret == 0 && ssl->options.dtls && !ssl->options.tls1_3) {
3587
        if (wc_encrypt)
3588
            wc_encrypt->src = keys == &ssl->keys ? KEYS : SCR;
3589
        if (wc_decrypt)
3590
            wc_decrypt->src = keys == &ssl->keys ? KEYS : SCR;
3591
    }
3592
#endif
3593
3594
    if (copy) {
3595
        int clientCopy = 0;
3596
3597
        /* Sanity check that keys == ssl->secure_renegotiation->tmp_keys.
3598
         * Otherwise the memcpy calls would copy overlapping memory
3599
         * and cause UB. Fail early. */
3600
        if (keys == &ssl->keys)
3601
            return BAD_FUNC_ARG;
3602
3603
        if (ssl->options.side == WOLFSSL_CLIENT_END && wc_encrypt)
3604
            clientCopy = 1;
3605
        else if (ssl->options.side == WOLFSSL_SERVER_END && wc_decrypt)
3606
            clientCopy = 1;
3607
3608
        if (clientCopy) {
3609
    #ifndef WOLFSSL_AEAD_ONLY
3610
            XMEMCPY(ssl->keys.client_write_MAC_secret,
3611
                    keys->client_write_MAC_secret, WC_MAX_DIGEST_SIZE);
3612
    #endif
3613
            XMEMCPY(ssl->keys.client_write_key,
3614
                    keys->client_write_key, AES_256_KEY_SIZE);
3615
            XMEMCPY(ssl->keys.client_write_IV,
3616
                    keys->client_write_IV, MAX_WRITE_IV_SZ);
3617
        } else {
3618
    #ifndef WOLFSSL_AEAD_ONLY
3619
            XMEMCPY(ssl->keys.server_write_MAC_secret,
3620
                    keys->server_write_MAC_secret, WC_MAX_DIGEST_SIZE);
3621
    #endif
3622
            XMEMCPY(ssl->keys.server_write_key,
3623
                    keys->server_write_key, AES_256_KEY_SIZE);
3624
            XMEMCPY(ssl->keys.server_write_IV,
3625
                    keys->server_write_IV, MAX_WRITE_IV_SZ);
3626
        }
3627
        if (wc_encrypt) {
3628
            ssl->keys.sequence_number_hi = keys->sequence_number_hi;
3629
            ssl->keys.sequence_number_lo = keys->sequence_number_lo;
3630
            #ifdef HAVE_AEAD
3631
                if (ssl->specs.cipher_type == aead) {
3632
                    /* Initialize the AES-GCM/CCM explicit IV to a zero. */
3633
                    XMEMCPY(ssl->keys.aead_exp_IV, keys->aead_exp_IV,
3634
                            AEAD_MAX_EXP_SZ);
3635
3636
                    /* Initialize encrypt implicit IV by encrypt side */
3637
                    if (ssl->options.side == WOLFSSL_CLIENT_END) {
3638
                        XMEMCPY(ssl->keys.aead_enc_imp_IV,
3639
                                keys->client_write_IV, AEAD_MAX_IMP_SZ);
3640
                    } else {
3641
                        XMEMCPY(ssl->keys.aead_enc_imp_IV,
3642
                                keys->server_write_IV, AEAD_MAX_IMP_SZ);
3643
                    }
3644
                }
3645
            #endif
3646
        }
3647
        if (wc_decrypt) {
3648
            ssl->keys.peer_sequence_number_hi = keys->peer_sequence_number_hi;
3649
            ssl->keys.peer_sequence_number_lo = keys->peer_sequence_number_lo;
3650
            #ifdef HAVE_AEAD
3651
                if (ssl->specs.cipher_type == aead) {
3652
                    /* Initialize decrypt implicit IV by decrypt side */
3653
                    if (ssl->options.side == WOLFSSL_SERVER_END) {
3654
                        XMEMCPY(ssl->keys.aead_dec_imp_IV,
3655
                                keys->client_write_IV, AEAD_MAX_IMP_SZ);
3656
                    } else {
3657
                        XMEMCPY(ssl->keys.aead_dec_imp_IV,
3658
                                keys->server_write_IV, AEAD_MAX_IMP_SZ);
3659
                    }
3660
                }
3661
            #endif
3662
        }
3663
        CacheStatusPP(ssl->secure_renegotiation);
3664
    }
3665
#endif /* HAVE_SECURE_RENEGOTIATION */
3666
3667
    return ret;
3668
}
3669
3670
3671
/* TLS can call too */
3672
int StoreKeys(WOLFSSL* ssl, const byte* keyData, int side)
3673
0
{
3674
0
    size_t sz;
3675
0
    int i = 0;
3676
0
    Keys* keys = &ssl->keys;
3677
#ifdef WOLFSSL_DTLS
3678
    /* In case of DTLS, ssl->keys is updated here */
3679
    int scr_copy = 0;
3680
#endif
3681
3682
#ifdef HAVE_SECURE_RENEGOTIATION
3683
    if (ssl->secure_renegotiation &&
3684
            ssl->secure_renegotiation->cache_status == SCR_CACHE_NEEDED) {
3685
        keys = &ssl->secure_renegotiation->tmp_keys;
3686
#ifdef WOLFSSL_DTLS
3687
        if (ssl->options.dtls) {
3688
            /* epoch is incremented after StoreKeys is called */
3689
            ssl->secure_renegotiation->tmp_keys.dtls_epoch = ssl->keys.dtls_epoch + 1;
3690
            /* we only need to copy keys on second and future renegotiations */
3691
            if (ssl->keys.dtls_epoch > 1)
3692
                scr_copy = 1;
3693
            ssl->encrypt.src = KEYS_NOT_SET;
3694
            ssl->decrypt.src = KEYS_NOT_SET;
3695
        }
3696
#endif
3697
        CacheStatusPP(ssl->secure_renegotiation);
3698
    }
3699
#endif /* HAVE_SECURE_RENEGOTIATION */
3700
3701
#ifdef WOLFSSL_MULTICAST
3702
    if (ssl->options.haveMcast) {
3703
        /* Use the same keys for encrypt and decrypt. */
3704
        if (ssl->specs.cipher_type != aead) {
3705
            sz = ssl->specs.hash_size;
3706
    #ifndef WOLFSSL_AEAD_ONLY
3707
3708
    #ifdef WOLFSSL_DTLS
3709
            if (scr_copy) {
3710
                XMEMCPY(ssl->keys.client_write_MAC_secret,
3711
                        keys->client_write_MAC_secret, sz);
3712
                XMEMCPY(ssl->keys.server_write_MAC_secret,
3713
                        keys->server_write_MAC_secret, sz);
3714
            }
3715
    #endif
3716
            XMEMCPY(keys->client_write_MAC_secret,&keyData[i], sz);
3717
            XMEMCPY(keys->server_write_MAC_secret,&keyData[i], sz);
3718
    #endif
3719
            i += (int)sz;
3720
        }
3721
        sz = ssl->specs.key_size;
3722
    #ifdef WOLFSSL_DTLS
3723
        if (scr_copy) {
3724
            XMEMCPY(ssl->keys.client_write_key,
3725
                    keys->client_write_key, sz);
3726
            XMEMCPY(ssl->keys.server_write_key,
3727
                    keys->server_write_key, sz);
3728
        }
3729
    #endif
3730
        XMEMCPY(keys->client_write_key, &keyData[i], sz);
3731
        XMEMCPY(keys->server_write_key, &keyData[i], sz);
3732
        i += (int)sz;
3733
3734
        sz = ssl->specs.iv_size;
3735
    #ifdef WOLFSSL_DTLS
3736
        if (scr_copy) {
3737
            XMEMCPY(ssl->keys.client_write_IV,
3738
                    keys->client_write_IV, sz);
3739
            XMEMCPY(ssl->keys.server_write_IV,
3740
                    keys->server_write_IV, sz);
3741
        }
3742
    #endif
3743
        XMEMCPY(keys->client_write_IV, &keyData[i], sz);
3744
        XMEMCPY(keys->server_write_IV, &keyData[i], sz);
3745
3746
#ifdef HAVE_AEAD
3747
        if (ssl->specs.cipher_type == aead) {
3748
            /* Initialize the AES-GCM/CCM explicit IV to a zero. */
3749
        #ifdef WOLFSSL_DTLS
3750
            if (scr_copy) {
3751
                XMEMCPY(ssl->keys.aead_exp_IV,
3752
                        keys->aead_exp_IV, AEAD_MAX_EXP_SZ);
3753
            }
3754
        #endif
3755
            XMEMSET(keys->aead_exp_IV, 0, AEAD_MAX_EXP_SZ);
3756
        }
3757
#endif /* HAVE_AEAD */
3758
3759
        return 0;
3760
    }
3761
#endif /* WOLFSSL_MULTICAST */
3762
3763
0
    if (ssl->specs.cipher_type != aead) {
3764
0
        sz = ssl->specs.hash_size;
3765
0
        if (side & PROVISION_CLIENT) {
3766
0
    #ifndef WOLFSSL_AEAD_ONLY
3767
        #ifdef WOLFSSL_DTLS
3768
            if (scr_copy)
3769
                XMEMCPY(ssl->keys.client_write_MAC_secret,
3770
                        keys->client_write_MAC_secret, sz);
3771
        #endif
3772
0
            XMEMCPY(keys->client_write_MAC_secret,&keyData[i], sz);
3773
0
    #endif
3774
0
            i += (int)sz;
3775
0
        }
3776
0
        if (side & PROVISION_SERVER) {
3777
0
    #ifndef WOLFSSL_AEAD_ONLY
3778
        #ifdef WOLFSSL_DTLS
3779
            if (scr_copy)
3780
                XMEMCPY(ssl->keys.server_write_MAC_secret,
3781
                        keys->server_write_MAC_secret, sz);
3782
        #endif
3783
0
            XMEMCPY(keys->server_write_MAC_secret,&keyData[i], sz);
3784
0
    #endif
3785
0
            i += (int)sz;
3786
0
        }
3787
0
    }
3788
0
    sz = ssl->specs.key_size;
3789
0
    if (side & PROVISION_CLIENT) {
3790
    #ifdef WOLFSSL_DTLS
3791
        if (scr_copy)
3792
            XMEMCPY(ssl->keys.client_write_key,
3793
                    keys->client_write_key, sz);
3794
    #endif
3795
0
        XMEMCPY(keys->client_write_key, &keyData[i], sz);
3796
0
        i += (int)sz;
3797
0
    }
3798
0
    if (side & PROVISION_SERVER) {
3799
    #ifdef WOLFSSL_DTLS
3800
        if (scr_copy)
3801
            XMEMCPY(ssl->keys.server_write_key,
3802
                    keys->server_write_key, sz);
3803
    #endif
3804
0
        XMEMCPY(keys->server_write_key, &keyData[i], sz);
3805
0
        i += (int)sz;
3806
0
    }
3807
3808
0
    sz = ssl->specs.iv_size;
3809
0
    if (side & PROVISION_CLIENT) {
3810
    #ifdef WOLFSSL_DTLS
3811
        if (scr_copy)
3812
            XMEMCPY(ssl->keys.client_write_IV,
3813
                    keys->client_write_IV, sz);
3814
    #endif
3815
0
        XMEMCPY(keys->client_write_IV, &keyData[i], sz);
3816
0
        i += (int)sz;
3817
0
    }
3818
0
    if (side & PROVISION_SERVER) {
3819
    #ifdef WOLFSSL_DTLS
3820
        if (scr_copy)
3821
            XMEMCPY(ssl->keys.server_write_IV,
3822
                    keys->server_write_IV, sz);
3823
    #endif
3824
0
        XMEMCPY(keys->server_write_IV, &keyData[i], sz);
3825
0
    }
3826
3827
0
#ifdef HAVE_AEAD
3828
0
    if (ssl->specs.cipher_type == aead) {
3829
        /* Initialize the AES-GCM/CCM explicit IV to a zero. */
3830
    #ifdef WOLFSSL_DTLS
3831
        if (scr_copy)
3832
            XMEMMOVE(ssl->keys.aead_exp_IV,
3833
                    keys->aead_exp_IV, AEAD_MAX_EXP_SZ);
3834
    #endif
3835
0
        XMEMSET(keys->aead_exp_IV, 0, AEAD_MAX_EXP_SZ);
3836
0
    }
3837
0
#endif
3838
3839
0
    return 0;
3840
0
}
3841
3842
#ifndef NO_OLD_TLS
3843
int DeriveKeys(WOLFSSL* ssl)
3844
{
3845
    int    length = 2 * ssl->specs.hash_size +
3846
                    2 * ssl->specs.key_size  +
3847
                    2 * ssl->specs.iv_size;
3848
    int    rounds = (length + WC_MD5_DIGEST_SIZE - 1 ) / WC_MD5_DIGEST_SIZE;
3849
    int    ret = 0;
3850
3851
#ifdef WOLFSSL_SMALL_STACK
3852
    byte*  shaOutput;
3853
    byte*  md5Input;
3854
    byte*  shaInput;
3855
    byte*  keyData;
3856
    wc_Md5* md5;
3857
    wc_Sha* sha;
3858
#else
3859
    byte   shaOutput[WC_SHA_DIGEST_SIZE];
3860
    byte   md5Input[SECRET_LEN + WC_SHA_DIGEST_SIZE];
3861
    byte   shaInput[KEY_PREFIX + SECRET_LEN + 2 * RAN_LEN];
3862
    byte   keyData[KEY_PREFIX * WC_MD5_DIGEST_SIZE];
3863
    wc_Md5 md5[1];
3864
    wc_Sha sha[1];
3865
#endif
3866
3867
#ifdef WOLFSSL_SMALL_STACK
3868
    shaOutput = (byte*)XMALLOC(WC_SHA_DIGEST_SIZE,
3869
                                            NULL, DYNAMIC_TYPE_TMP_BUFFER);
3870
    md5Input  = (byte*)XMALLOC(SECRET_LEN + WC_SHA_DIGEST_SIZE,
3871
                                            NULL, DYNAMIC_TYPE_TMP_BUFFER);
3872
    shaInput  = (byte*)XMALLOC(KEY_PREFIX + SECRET_LEN + 2 * RAN_LEN,
3873
                                            NULL, DYNAMIC_TYPE_TMP_BUFFER);
3874
    keyData   = (byte*)XMALLOC(KEY_PREFIX * WC_MD5_DIGEST_SIZE,
3875
                                            NULL, DYNAMIC_TYPE_TMP_BUFFER);
3876
    md5       =  (wc_Md5*)XMALLOC(sizeof(wc_Md5), NULL, DYNAMIC_TYPE_TMP_BUFFER);
3877
    sha       =  (wc_Sha*)XMALLOC(sizeof(wc_Sha), NULL, DYNAMIC_TYPE_TMP_BUFFER);
3878
3879
    if (shaOutput == NULL || md5Input == NULL || shaInput == NULL ||
3880
        keyData   == NULL || md5      == NULL || sha      == NULL) {
3881
        XFREE(shaOutput, NULL, DYNAMIC_TYPE_TMP_BUFFER);
3882
        XFREE(md5Input, NULL, DYNAMIC_TYPE_TMP_BUFFER);
3883
        XFREE(shaInput, NULL, DYNAMIC_TYPE_TMP_BUFFER);
3884
        XFREE(keyData, NULL, DYNAMIC_TYPE_TMP_BUFFER);
3885
        XFREE(md5, NULL, DYNAMIC_TYPE_TMP_BUFFER);
3886
        XFREE(sha, NULL, DYNAMIC_TYPE_TMP_BUFFER);
3887
3888
        return MEMORY_E;
3889
    }
3890
#endif
3891
    XMEMSET(shaOutput, 0, WC_SHA_DIGEST_SIZE);
3892
    ret = wc_InitMd5(md5);
3893
    if (ret == 0) {
3894
        ret = wc_InitSha(sha);
3895
    }
3896
    if (ret == 0) {
3897
        int i;
3898
3899
        XMEMCPY(md5Input, ssl->arrays->masterSecret, SECRET_LEN);
3900
3901
        for (i = 0; i < rounds; ++i) {
3902
            int j   = i + 1;
3903
            int idx = j;
3904
3905
            if (!SetPrefix(shaInput, i)) {
3906
                ret = PREFIX_ERROR;
3907
                break;
3908
            }
3909
3910
            XMEMCPY(shaInput + idx, ssl->arrays->masterSecret, SECRET_LEN);
3911
            idx += SECRET_LEN;
3912
            XMEMCPY(shaInput + idx, ssl->arrays->serverRandom, RAN_LEN);
3913
            idx += RAN_LEN;
3914
            XMEMCPY(shaInput + idx, ssl->arrays->clientRandom, RAN_LEN);
3915
            if (ret == 0) {
3916
                ret = wc_ShaUpdate(sha, shaInput,
3917
                    (KEY_PREFIX + SECRET_LEN + 2 * RAN_LEN) - KEY_PREFIX +
3918
                        (word32)(j));
3919
            }
3920
            if (ret == 0) {
3921
                ret = wc_ShaFinal(sha, shaOutput);
3922
            }
3923
3924
            XMEMCPY(md5Input + SECRET_LEN, shaOutput, WC_SHA_DIGEST_SIZE);
3925
            if (ret == 0) {
3926
                ret = wc_Md5Update(md5, md5Input, SECRET_LEN + WC_SHA_DIGEST_SIZE);
3927
            }
3928
            if (ret == 0) {
3929
                ret = wc_Md5Final(md5, keyData + i * WC_MD5_DIGEST_SIZE);
3930
            }
3931
        }
3932
3933
        if (ret == 0)
3934
            ret = StoreKeys(ssl, keyData, PROVISION_CLIENT_SERVER);
3935
    }
3936
3937
#ifdef WOLFSSL_SMALL_STACK
3938
    XFREE(shaOutput, NULL, DYNAMIC_TYPE_TMP_BUFFER);
3939
    XFREE(md5Input,  NULL, DYNAMIC_TYPE_TMP_BUFFER);
3940
    XFREE(shaInput,  NULL, DYNAMIC_TYPE_TMP_BUFFER);
3941
    XFREE(keyData,   NULL, DYNAMIC_TYPE_TMP_BUFFER);
3942
    XFREE(md5,       NULL, DYNAMIC_TYPE_TMP_BUFFER);
3943
    XFREE(sha,       NULL, DYNAMIC_TYPE_TMP_BUFFER);
3944
#endif
3945
3946
    return ret;
3947
}
3948
3949
3950
static int CleanPreMaster(WOLFSSL* ssl)
3951
{
3952
    int i, ret, sz = (int)(ssl->arrays->preMasterSz);
3953
3954
    for (i = 0; i < sz; i++)
3955
        ssl->arrays->preMasterSecret[i] = 0;
3956
3957
    ret = wc_RNG_GenerateBlock(ssl->rng, ssl->arrays->preMasterSecret,
3958
                                                            (word32)(sz));
3959
    if (ret != 0)
3960
        return ret;
3961
3962
    for (i = 0; i < sz; i++)
3963
        ssl->arrays->preMasterSecret[i] = 0;
3964
3965
    XFREE(ssl->arrays->preMasterSecret, ssl->heap, DYNAMIC_TYPE_SECRET);
3966
    ssl->arrays->preMasterSecret = NULL;
3967
    ssl->arrays->preMasterSz = 0;
3968
3969
    return 0;
3970
}
3971
3972
3973
/* Create and store the master secret see page 32, 6.1 */
3974
static int MakeSslMasterSecret(WOLFSSL* ssl)
3975
{
3976
    int    i, ret;
3977
    word32 idx;
3978
    word32 pmsSz = ssl->arrays->preMasterSz;
3979
3980
#ifdef WOLFSSL_SMALL_STACK
3981
    byte*  shaOutput;
3982
    byte*  md5Input;
3983
    byte*  shaInput;
3984
    wc_Md5* md5;
3985
    wc_Sha* sha;
3986
#else
3987
    byte   shaOutput[WC_SHA_DIGEST_SIZE];
3988
    byte   md5Input[ENCRYPT_LEN + WC_SHA_DIGEST_SIZE];
3989
    byte   shaInput[PREFIX + ENCRYPT_LEN + 2 * RAN_LEN];
3990
    wc_Md5 md5[1];
3991
    wc_Sha sha[1];
3992
#endif
3993
3994
    if (ssl->arrays->preMasterSecret == NULL) {
3995
        return BAD_FUNC_ARG;
3996
    }
3997
3998
#ifdef SHOW_SECRETS
3999
    {
4000
        word32 j;
4001
        printf("pre master secret: ");
4002
        for (j = 0; j < pmsSz; j++)
4003
            printf("%02x", ssl->arrays->preMasterSecret[j]);
4004
        printf("\n");
4005
    }
4006
#endif
4007
4008
#ifdef WOLFSSL_SMALL_STACK
4009
    shaOutput = (byte*)XMALLOC(WC_SHA_DIGEST_SIZE,
4010
                                            NULL, DYNAMIC_TYPE_TMP_BUFFER);
4011
    md5Input  = (byte*)XMALLOC(ENCRYPT_LEN + WC_SHA_DIGEST_SIZE,
4012
                                            NULL, DYNAMIC_TYPE_TMP_BUFFER);
4013
    shaInput  = (byte*)XMALLOC(PREFIX + ENCRYPT_LEN + 2 * RAN_LEN,
4014
                                            NULL, DYNAMIC_TYPE_TMP_BUFFER);
4015
    md5       =  (wc_Md5*)XMALLOC(sizeof(wc_Md5), NULL, DYNAMIC_TYPE_TMP_BUFFER);
4016
    sha       =  (wc_Sha*)XMALLOC(sizeof(wc_Sha), NULL, DYNAMIC_TYPE_TMP_BUFFER);
4017
4018
    if (shaOutput == NULL || md5Input == NULL || shaInput == NULL ||
4019
                             md5      == NULL || sha      == NULL) {
4020
        XFREE(shaOutput, NULL, DYNAMIC_TYPE_TMP_BUFFER);
4021
        XFREE(md5Input, NULL, DYNAMIC_TYPE_TMP_BUFFER);
4022
        XFREE(shaInput, NULL, DYNAMIC_TYPE_TMP_BUFFER);
4023
        XFREE(md5, NULL, DYNAMIC_TYPE_TMP_BUFFER);
4024
        XFREE(sha, NULL, DYNAMIC_TYPE_TMP_BUFFER);
4025
4026
        return MEMORY_E;
4027
    }
4028
#endif
4029
    XMEMSET(shaOutput, 0, WC_SHA_DIGEST_SIZE);
4030
4031
    ret = wc_InitMd5(md5);
4032
    if (ret == 0) {
4033
        ret = wc_InitSha(sha);
4034
    }
4035
    if (ret == 0) {
4036
        XMEMCPY(md5Input, ssl->arrays->preMasterSecret, pmsSz);
4037
4038
        for (i = 0; i < MASTER_ROUNDS; ++i) {
4039
            byte prefix[KEY_PREFIX];      /* only need PREFIX bytes but static */
4040
            if (!SetPrefix(prefix, i)) {  /* analysis thinks will overrun      */
4041
                ret = PREFIX_ERROR;
4042
                break;
4043
            }
4044
4045
            idx = 0;
4046
            XMEMCPY(shaInput, prefix, (size_t)(i + 1));
4047
            idx += (word32)(i + 1);
4048
4049
            XMEMCPY(shaInput + idx, ssl->arrays->preMasterSecret, pmsSz);
4050
            idx += pmsSz;
4051
            XMEMCPY(shaInput + idx, ssl->arrays->clientRandom, RAN_LEN);
4052
            idx += RAN_LEN;
4053
            XMEMCPY(shaInput + idx, ssl->arrays->serverRandom, RAN_LEN);
4054
            idx += RAN_LEN;
4055
            if (ret == 0) {
4056
                ret = wc_ShaUpdate(sha, shaInput, idx);
4057
            }
4058
            if (ret == 0) {
4059
                ret = wc_ShaFinal(sha, shaOutput);
4060
            }
4061
            idx = pmsSz;  /* preSz */
4062
            XMEMCPY(md5Input + idx, shaOutput, WC_SHA_DIGEST_SIZE);
4063
            idx += WC_SHA_DIGEST_SIZE;
4064
            if (ret == 0) {
4065
                ret = wc_Md5Update(md5, md5Input, idx);
4066
            }
4067
            if (ret == 0) {
4068
                ret = wc_Md5Final(md5,
4069
                            &ssl->arrays->masterSecret[i * WC_MD5_DIGEST_SIZE]);
4070
            }
4071
        }
4072
4073
#ifdef SHOW_SECRETS
4074
        {
4075
            word32 j;
4076
            printf("master secret: ");
4077
            for (j = 0; j < SECRET_LEN; j++)
4078
                printf("%02x", ssl->arrays->masterSecret[j]);
4079
            printf("\n");
4080
        }
4081
#endif
4082
4083
        if (ret == 0)
4084
            ret = DeriveKeys(ssl);
4085
    }
4086
4087
#ifdef WOLFSSL_SMALL_STACK
4088
    XFREE(shaOutput, NULL, DYNAMIC_TYPE_TMP_BUFFER);
4089
    XFREE(md5Input,  NULL, DYNAMIC_TYPE_TMP_BUFFER);
4090
    XFREE(shaInput,  NULL, DYNAMIC_TYPE_TMP_BUFFER);
4091
    XFREE(md5,       NULL, DYNAMIC_TYPE_TMP_BUFFER);
4092
    XFREE(sha,       NULL, DYNAMIC_TYPE_TMP_BUFFER);
4093
#endif
4094
4095
    if (ret == 0)
4096
        ret = CleanPreMaster(ssl);
4097
    else
4098
        CleanPreMaster(ssl);
4099
4100
    return ret;
4101
}
4102
#endif
4103
4104
4105
/* Master wrapper, doesn't use SSL stack space in TLS mode */
4106
int MakeMasterSecret(WOLFSSL* ssl)
4107
{
4108
    /* append secret to premaster : premaster | SerSi | CliSi */
4109
#ifndef NO_OLD_TLS
4110
    if (ssl->options.tls) return MakeTlsMasterSecret(ssl);
4111
    return MakeSslMasterSecret(ssl);
4112
#elif !defined(WOLFSSL_NO_TLS12) && !defined(NO_TLS)
4113
    return MakeTlsMasterSecret(ssl);
4114
#else
4115
    (void)ssl;
4116
    return 0;
4117
#endif
4118
}
4119
4120
#endif /* !WOLFCRYPT_ONLY && !NO_TLS */