Debian Codegraph @

debian/rust-vendor/openssl-src/openssl/crypto/lms/lms_verify.c

raw
1 /*
2 * Copyright 2025 The OpenSSL Project Authors. All Rights Reserved.
3 *
4 * Licensed under the Apache License 2.0 (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
8 */
9
10 #include <openssl/byteorder.h>
11 #include "crypto/lms_sig.h"
12 #include "crypto/lms_util.h"
13 #include "internal/common.h"
14
15 /*
16 * Constants used for obtaining unique inputs for different hashing operations
17 * e.g H(I || q || OSSL_LMS_D_LEAF || ... )
18 */
19 const uint16_t OSSL_LMS_D_PBLC = 0x8080;
20 const uint16_t OSSL_LMS_D_MESG = 0x8181;
21 const uint16_t OSSL_LMS_D_LEAF = 0x8282;
22 const uint16_t OSSL_LMS_D_INTR = 0x8383;
23
24 /*
25 * @brief Compute the candidate LMS root value Tc
26 *
27 * @param paths An array of bytes representing the hash values associated with
28 * the path through the tree from the leaf associated with the
29 * LM-OTS signature to the root public key node.
30 * @param n The hash output size (The size of each path in |paths|)
31 * @param nodenum The leaf index node number. The root node had a value of 1
32 * Each subsequent level has nodes in the range 2^h...2^(h+1)-1
33 * @param ctx A EVP_MD_CTX object used for calculations
34 * @param ctxI A EVP_MD_CTX object containing an unfinalised H(I)
35 * @param Tc Contains H(I || u32str(node_num) || u16str(D_LEAF) || Kc) on input,
36 * and on output returns the calculated candidate public key.
37 * @returns 1 on success, or 0 otherwise.
38 */
39 static int lms_sig_compute_tc_from_path(const unsigned char *paths, uint32_t n,
40 uint32_t node_num,
41 EVP_MD_CTX *ctx, EVP_MD_CTX *ctxI,
42 unsigned char *Tc)
43 {
44 int ret = 0;
45 unsigned char qbuf[4];
46 unsigned char d_intr[sizeof(uint16_t)];
47 const unsigned char *path = paths;
48
49 OPENSSL_store_u16_be(d_intr, OSSL_LMS_D_INTR);
50
51 /*
52 * Calculate the public key Tc using the path
53 * The root hash is the hash of its 2 childrens' Hash values.
54 * A child hash for each level is passed in by paths, and we have
55 * a leaf value that can be used with the path to calculate the parent
56 * hash.
57 */
58 while (node_num > 1) {
59 /* At each level the path contains either the left or right child */
60 int odd = node_num & 1;
61
62 node_num = node_num >> 1; /* get the parent node_num */
63 OPENSSL_store_u32_be(qbuf, node_num);
64
65 /*
66 * Calculate Tc as either
67 * Tc(parent) = H(I || node_q || 0x8383 || paths[i][n] || Tc(right) OR
68 * Tc(parent) = H(I || node_q || 0x8383 || Tc(left) || paths[i][n])
69 */
70 if (!EVP_MD_CTX_copy_ex(ctx, ctxI)
71 || !EVP_DigestUpdate(ctx, qbuf, sizeof(qbuf))
72 || !EVP_DigestUpdate(ctx, d_intr, sizeof(d_intr)))
73 goto err;
74
75 if (odd) {
76 if (!EVP_DigestUpdate(ctx, path, n)
77 || !EVP_DigestUpdate(ctx, Tc, n))
78 goto err;
79 } else {
80 if (!EVP_DigestUpdate(ctx, Tc, n)
81 || !EVP_DigestUpdate(ctx, path, n))
82 goto err;
83 }
84 /*
85 * Tc = parent Hash, which is either the left or right child for the next
86 * level up (node_num determines if it is left or right).
87 */
88 if (!EVP_DigestFinal_ex(ctx, Tc, NULL))
89 goto err;
90 path += n;
91 }
92 ret = 1;
93 err:
94 return ret;
95 }
96
97 /*
98 * @brief LMS signature verification.
99 * See RFC 8554 Section 5.4.2. Algorithm 6: Steps 3 & 4
100 *
101 * @param lms_sig Is a valid decoded LMS_SIG signature object.
102 * @param pub Is a valid LMS public key object.
103 * @param md Contains the fetched digest to be used for Hash operations
104 * @param msg A message to verify
105 * @param msglen The size of |msg|
106 * @returns 1 if the verification succeeded, or 0 otherwise.
107 */
108 int ossl_lms_sig_verify(const LMS_SIG *lms_sig, const LMS_KEY *pub,
109 const EVP_MD *md,
110 const unsigned char *msg, size_t msglen)
111 {
112 int ret = 0;
113 EVP_MD_CTX *ctx = NULL, *ctxIq = NULL;
114 EVP_MD_CTX *ctxI;
115 unsigned char Kc[LMS_MAX_DIGEST_SIZE];
116 unsigned char Tc[LMS_MAX_DIGEST_SIZE];
117 unsigned char qbuf[4];
118 unsigned char d_leaf[sizeof(uint16_t)];
119 const LMS_PARAMS *lms_params = pub->lms_params;
120 uint32_t n = lms_params->n;
121 uint32_t node_num;
122
123 ctx = EVP_MD_CTX_create();
124 ctxIq = EVP_MD_CTX_create();
125 if (ctx == NULL || ctxIq == NULL)
126 goto err;
127
128 if (!lms_evp_md_ctx_init(ctxIq, md, lms_sig->params))
129 goto err;
130 /*
131 * Algorithm 6a: Step 3.
132 * Calculate a candidate public key |Kc| using the lmots_signature, message,
133 * and the identifiers I, q
134 */
135 if (!ossl_lm_ots_compute_pubkey(ctx, ctxIq, &lms_sig->sig,
136 pub->ots_params, pub->Id,
137 lms_sig->q, msg, msglen, Kc))
138 goto err;
139
140 /*
141 * Algorithm 6a: Step 4
142 * Compute the candidate LMS root value Tc
143 */
144 if (!ossl_assert(lms_sig->q < (uint32_t)(1 << lms_params->h)))
145 return 0;
146 node_num = (1 << lms_params->h) + lms_sig->q;
147
148 OPENSSL_store_u32_be(qbuf, node_num);
149 OPENSSL_store_u16_be(d_leaf, OSSL_LMS_D_LEAF);
150 ctxI = ctxIq;
151 /*
152 * Tc = H(I || u32str(node_num) || u16str(D_LEAF) || Kc)
153 *
154 * ctx is left initialised with the md from ossl_lm_ots_compute_pubkey,
155 * so there is no need to reinitialise it here.
156 */
157 if (!EVP_DigestInit_ex2(ctx, NULL, NULL)
158 || !EVP_DigestUpdate(ctx, pub->Id, LMS_SIZE_I)
159 || !EVP_MD_CTX_copy_ex(ctxI, ctx)
160 || !EVP_DigestUpdate(ctx, qbuf, sizeof(qbuf))
161 || !EVP_DigestUpdate(ctx, d_leaf, sizeof(d_leaf))
162 || !EVP_DigestUpdate(ctx, Kc, n)
163 || !EVP_DigestFinal_ex(ctx, Tc, NULL)
164 || !lms_sig_compute_tc_from_path(lms_sig->paths, n, node_num,
165 ctx, ctxI, Tc))
166 goto err;
167 /* Algorithm 6: Step 4 */
168 ret = (memcmp(pub->pub.K, Tc, n) == 0);
169 err:
170 EVP_MD_CTX_free(ctxIq);
171 EVP_MD_CTX_free(ctx);
172 return ret;
173 }