Coverage Report

Created: 2026-04-05 07:22

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