|
21 | 21 | #include "unit.h" |
22 | 22 | #include <openssl/core_names.h> |
23 | 23 | #include <openssl/decoder.h> |
| 24 | +#include <wolfprovider/internal.h> |
24 | 25 |
|
25 | 26 | #ifdef WP_HAVE_DH |
26 | 27 |
|
@@ -1546,4 +1547,98 @@ int test_dh_import_group_no_nul(void *data) |
1546 | 1547 | return err; |
1547 | 1548 | } |
1548 | 1549 |
|
| 1550 | +/* Enforce the minimum prime size: a below-minimum request must not produce |
| 1551 | + * parameters, while a request at the minimum must still succeed. Sizes come |
| 1552 | + * from the provider's own WP_DH_MIN_BITS so the two cannot diverge. */ |
| 1553 | +#define TEST_DH_MIN_BITS WP_DH_MIN_BITS |
| 1554 | +#define TEST_DH_WEAK_BITS (WP_DH_MIN_BITS / 2) |
| 1555 | +int test_dh_pgen_min_bits(void *data) |
| 1556 | +{ |
| 1557 | + int err = 0; |
| 1558 | + int rc; |
| 1559 | + EVP_PKEY_CTX *ctx = NULL; |
| 1560 | + EVP_PKEY *keyParams = NULL; |
| 1561 | + EVP_PKEY *weakParams = NULL; |
| 1562 | + BIGNUM *prime = NULL; |
| 1563 | + |
| 1564 | + (void)data; |
| 1565 | + |
| 1566 | + PRINT_MSG("Testing DH parameter generation minimum prime size enforcement"); |
| 1567 | + |
| 1568 | + /* Below-minimum size must be rejected at set-params time. */ |
| 1569 | + ctx = EVP_PKEY_CTX_new_from_name(wpLibCtx, "DH", NULL); |
| 1570 | + if (ctx == NULL) { |
| 1571 | + err = 1; |
| 1572 | + } |
| 1573 | + if (err == 0) { |
| 1574 | + err = EVP_PKEY_paramgen_init(ctx) != 1; |
| 1575 | + } |
| 1576 | + if (err == 0) { |
| 1577 | + rc = EVP_PKEY_CTX_set_dh_paramgen_prime_len(ctx, TEST_DH_WEAK_BITS); |
| 1578 | + if (rc == 1) { |
| 1579 | + PRINT_MSG("set_dh_paramgen_prime_len accepted a below-minimum size"); |
| 1580 | + err = 1; |
| 1581 | + } |
| 1582 | + } |
| 1583 | + EVP_PKEY_CTX_free(ctx); |
| 1584 | + ctx = NULL; |
| 1585 | + |
| 1586 | + /* A caller that ignores the set-params failure must never end up with |
| 1587 | + * below-minimum parameters: generation either fails, or the prime it |
| 1588 | + * produced is at least the minimum size. */ |
| 1589 | + if (err == 0) { |
| 1590 | + ctx = EVP_PKEY_CTX_new_from_name(wpLibCtx, "DH", NULL); |
| 1591 | + err = ctx == NULL; |
| 1592 | + } |
| 1593 | + if (err == 0) { |
| 1594 | + err = EVP_PKEY_paramgen_init(ctx) != 1; |
| 1595 | + } |
| 1596 | + if (err == 0) { |
| 1597 | + (void)EVP_PKEY_CTX_set_dh_paramgen_prime_len(ctx, TEST_DH_WEAK_BITS); |
| 1598 | + if (EVP_PKEY_paramgen(ctx, &weakParams) == 1) { |
| 1599 | + err = EVP_PKEY_get_bn_param(weakParams, OSSL_PKEY_PARAM_FFC_P, |
| 1600 | + &prime) != 1; |
| 1601 | + if ((err == 0) && (BN_num_bits(prime) < TEST_DH_MIN_BITS)) { |
| 1602 | + PRINT_MSG("paramgen produced a prime below the minimum size"); |
| 1603 | + err = 1; |
| 1604 | + } |
| 1605 | + } |
| 1606 | + } |
| 1607 | + EVP_PKEY_CTX_free(ctx); |
| 1608 | + ctx = NULL; |
| 1609 | + |
| 1610 | + /* Size at the minimum must still succeed. */ |
| 1611 | + if (err == 0) { |
| 1612 | + ctx = EVP_PKEY_CTX_new_from_name(wpLibCtx, "DH", NULL); |
| 1613 | + err = ctx == NULL; |
| 1614 | + } |
| 1615 | + if (err == 0) { |
| 1616 | + err = EVP_PKEY_paramgen_init(ctx) != 1; |
| 1617 | + } |
| 1618 | + if (err == 0) { |
| 1619 | + err = EVP_PKEY_CTX_set_dh_paramgen_prime_len(ctx, TEST_DH_MIN_BITS) |
| 1620 | + != 1; |
| 1621 | + } |
| 1622 | + if (err == 0) { |
| 1623 | + err = EVP_PKEY_paramgen(ctx, &keyParams) != 1; |
| 1624 | + if (err != 0) { |
| 1625 | + PRINT_MSG("DH paramgen failed at the minimum prime size"); |
| 1626 | + } |
| 1627 | + } |
| 1628 | + if (err == 0) { |
| 1629 | + err = EVP_PKEY_get_bn_param(keyParams, OSSL_PKEY_PARAM_FFC_P, |
| 1630 | + &prime) != 1; |
| 1631 | + if ((err == 0) && (BN_num_bits(prime) < TEST_DH_MIN_BITS)) { |
| 1632 | + PRINT_MSG("paramgen produced a prime below the minimum size"); |
| 1633 | + err = 1; |
| 1634 | + } |
| 1635 | + } |
| 1636 | + |
| 1637 | + BN_free(prime); |
| 1638 | + EVP_PKEY_free(weakParams); |
| 1639 | + EVP_PKEY_free(keyParams); |
| 1640 | + EVP_PKEY_CTX_free(ctx); |
| 1641 | + return err; |
| 1642 | +} |
| 1643 | + |
1549 | 1644 | #endif /* WP_HAVE_DH */ |
0 commit comments