Coverage Report

Created: 2026-04-10 07:03

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/rnp/src/lib/crypto/kmac.cpp
Line
Count
Source
1
/*
2
 * Copyright (c) 2023, [MTG AG](https://www.mtg.de).
3
 * All rights reserved.
4
 *
5
 * Redistribution and use in source and binary forms, with or without modification,
6
 * are permitted provided that the following conditions are met:
7
 *
8
 * 1.  Redistributions of source code must retain the above copyright notice,
9
 *     this list of conditions and the following disclaimer.
10
 *
11
 * 2.  Redistributions in binary form must reproduce the above copyright notice,
12
 *     this list of conditions and the following disclaimer in the documentation
13
 *     and/or other materials provided with the distribution.
14
 *
15
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
16
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
19
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
22
 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
23
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25
 */
26
27
#include "config.h"
28
#include "kmac.hpp"
29
30
#if defined(ENABLE_PQC_DBG_LOG)
31
#include "crypto/mem.h"
32
#endif
33
34
#if defined(CRYPTO_BACKEND_BOTAN)
35
#include "kmac_botan.hpp"
36
#endif
37
#if defined(CRYPTO_BACKEND_OPENSSL)
38
#error KMAC256 not implemented for OpenSSL Backend
39
#endif
40
41
namespace rnp {
42
std::unique_ptr<KMAC256>
43
KMAC256::create()
44
0
{
45
#if defined(CRYPTO_BACKEND_OPENSSL)
46
#error KMAC256 not implemented for OpenSSL
47
    // return Hash_OpenSSL::create();
48
#elif defined(CRYPTO_BACKEND_BOTAN)
49
    return KMAC256_Botan::create();
50
#else
51
#error "Crypto backend not specified"
52
#endif
53
0
}
54
55
std::vector<uint8_t>
56
KMAC256::domSeparation() const
57
0
{
58
0
    return domSeparation_;
59
0
}
60
61
std::vector<uint8_t>
62
KMAC256::customizationString() const
63
0
{
64
0
    return customizationString_;
65
0
}
66
67
std::vector<uint8_t>
68
KMAC256::counter() const
69
0
{
70
0
    return counter_;
71
0
}
72
73
/*
74
    //   Input:
75
    //   algID     - the algorithm ID encoded as octet
76
77
    fixedInfo = algID
78
*/
79
std::vector<uint8_t>
80
KMAC256::fixedInfo(pgp_pubkey_alg_t alg_id)
81
0
{
82
0
    std::vector<uint8_t> result;
83
0
    result.push_back(static_cast<uint8_t>(alg_id));
84
0
    return result;
85
0
}
86
87
std::vector<uint8_t>
88
KMAC256::encData(const std::vector<uint8_t> &ecc_key_share,
89
                 const std::vector<uint8_t> &ecc_ciphertext,
90
                 const std::vector<uint8_t> &kyber_key_share,
91
                 const std::vector<uint8_t> &kyber_ciphertext,
92
                 pgp_pubkey_alg_t            alg_id)
93
0
{
94
0
    std::vector<uint8_t> enc_data;
95
0
    std::vector<uint8_t> counter_vec = counter();
96
0
    std::vector<uint8_t> fixedInfo_vec = fixedInfo(alg_id);
97
98
    /* draft-wussler-openpgp-pqc-02:
99
100
        eccKemData = eccKeyShare || eccCipherText
101
        kyberKemData = kyberKeyShare || kyberCipherText
102
        encData = counter || eccKemData || kyberKemData || fixedInfo
103
    */
104
#if defined(ENABLE_PQC_DBG_LOG)
105
    RNP_LOG_NO_POS_INFO("KMAC256 encData: ");
106
    RNP_LOG_U8VEC(" - counter: %s", counter_vec);
107
    RNP_LOG_U8VEC(" - eccKeyShare: %s", ecc_key_share);
108
    RNP_LOG_U8VEC(" - eccCipherText: %s", ecc_ciphertext);
109
    RNP_LOG_U8VEC(" - kyberKeyShare: %s", kyber_key_share);
110
    RNP_LOG_U8VEC(" - kyberCipherText: %s", kyber_ciphertext);
111
    RNP_LOG_U8VEC(" - fixedInfo: %s", fixedInfo_vec);
112
#endif
113
114
0
    enc_data.insert(enc_data.end(), counter_vec.begin(), counter_vec.end());
115
0
    enc_data.insert(enc_data.end(), ecc_key_share.begin(), ecc_key_share.end());
116
0
    enc_data.insert(enc_data.end(), ecc_ciphertext.begin(), ecc_ciphertext.end());
117
0
    enc_data.insert(enc_data.end(), kyber_key_share.begin(), kyber_key_share.end());
118
0
    enc_data.insert(enc_data.end(), kyber_ciphertext.begin(), kyber_ciphertext.end());
119
0
    enc_data.insert(enc_data.end(), fixedInfo_vec.begin(), fixedInfo_vec.end());
120
121
0
    return enc_data;
122
0
}
123
124
KMAC256::~KMAC256()
125
0
{
126
0
}
127
128
} // namespace rnp