Coverage Report

Created: 2025-12-31 07:08

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