-
Notifications
You must be signed in to change notification settings - Fork 569
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
PQC: Classic McEliece #3883
base: master
Are you sure you want to change the base?
PQC: Classic McEliece #3883
Conversation
f73829f
to
e505390
Compare
04b9314
to
190261d
Compare
This comment was marked as resolved.
This comment was marked as resolved.
7662592
to
2a3e60f
Compare
This comment was marked as resolved.
This comment was marked as resolved.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This mostly looks at cmce_decaps.cpp
, cmce_encaps.cpp
and cmce.cpp
, noting many minor code style things and a few suggestions for alternative code structuring. Not looking into the Classic McEliece specifics at all here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
First pass on cmce_field_orderings.cpp
. I'm somewhat concerned that this isn't very efficient. But it may well be "good enough". Should we profile that?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
More comments. I didn't look at cmce_parameter_set.*
, cmce_parameters_*
and cmce_poly.*
, yet.
// TODO: Only for test instances. Remove on final PR | ||
size_t m = Classic_McEliece_GF::log_q_from_mod(mod); | ||
|
||
for(int i = static_cast<int>(m) - 2; i >= 0; --i) { | ||
x ^= CT::Mask<uint32_t>::expand((uint32_t(1) << (i + m)) & x) | ||
.if_set_return(static_cast<uint32_t>(mod.get()) << i); | ||
} | ||
|
||
return GF_Elem(static_cast<uint16_t>(x)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't forget to remove (and probably replace by some exception?)
Code_Word Classic_McEliece_Matrix::mul(const Classic_McEliece_Parameters& params, const Error_Vector& e) const { | ||
auto s = e.subvector(0, params.pk_no_rows()); | ||
auto e_T = e.subvector(params.pk_no_rows()); | ||
auto pk_slicer = BufferSlicer(m_mat_bytes); | ||
|
||
for(size_t i = 0; i < params.pk_no_rows(); ++i) { | ||
auto pk_current_bytes = pk_slicer.take(params.pk_row_size_bytes()); | ||
auto row = secure_bitvector(pk_current_bytes, params.n() - params.pk_no_rows()); | ||
row &= e_T; | ||
s.at(i) = s.at(i) ^ row.has_odd_hamming_weight(); | ||
} | ||
|
||
BOTAN_ASSERT_NOMSG(pk_slicer.empty()); | ||
return s.as<Code_Word>(); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This produces quite a few copies and allocations. If that's in the implementation's hot path, we should probably want to have another look. Here's which (I believe cause allocations/copies):
.subvector()
always creates a newbitvector
and copies the content into a newly allocated buffer,- the c'tor of
bitvector
copies the passed-in buffer (particularlypk_current_bytes
.as<>
produces a copy of thebitvector
with a new type
For (3): This could already be the right type using e.subvector<Code_Word>()
, no?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Some context: This function is called once per encapsulation. Also, e
and s
are pretty small, while m_mat_bytes
is gigantic. Therefore, 1) and 3) are not critical; I'll apply your suggestion for 3) anyway. I don't know how we can prevent 2) without having something like a bitvector view or dropping the bitvector altogether.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think I'll have a timeboxed look into a bitvector that doesn't own its underlying storage. Perhaps that isn't too hard to achieve, especially when we can limit it to byte-aligned subvectors.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done with a first pass on the implementation. Don't get put off by the number of comments. Most are just C++ style nits and smaller programming suggestions.
That's really good work! 😃
/// Reduced instances for side channel analysis (Self-created test instance with | ||
/// m=8, n=128, t=8, f(z)=z^8+z^7+z^2+z+1, F(y)=y^8+y^4+y^3+y^2+1) | ||
/// Minimal instance without semi-systematic matrix creation and no plaintext confirmation | ||
test, | ||
/// Minimal instance with semi-systematic matrix creation and no plaintext confirmation | ||
testf, | ||
/// Minimal instance without semi-systematic matrix creation and with plaintext confirmation | ||
testpc, | ||
/// Minimal instance with semi-systematic matrix creation and with plaintext confirmation | ||
testpcf |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
TODO: remove before merging
/** | ||
* @returns ceil(n/d) | ||
* TODO: Remove once LMS is merged | ||
*/ | ||
constexpr size_t ceil_div(size_t n, size_t d) { | ||
return (n + d - 1) / d; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Re-evaluate before merging this.
Thanks a lot for your extensive review, @reneme! I addressed your suggestions and am optimistic that this PR is ready to drop its Draft status 🎉. Note that some suggestions that depend on other PRs are still open, which are not critical, though. Also, note that an extensive side-channel analysis is still in progress. |
src/lib/utils/bitvector.h
Outdated
constexpr bitref& operator^=(bool other) noexcept { return assign(this->is_set() ^ other); } | ||
|
||
private: | ||
constexpr bitref& assign(bool bit) noexcept { return (bit) ? set() : unset(); } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As discussed, DATA identified this line as a leakage during our SCA review.
Due to the ?
operator, a control-flow difference is observed based on the boolean input bit
variable.
The assign()
routine is used within the push_back()
routine, which in turn is used within the decode()
routine. This may allow an adversary to observe the error vector e
and, hence recover the shared secret.
We suggest to perform both the set()
and unset()
functions with the input as a mask, for example:
private:
constexpr bitref& assign(bool bit) noexcept {
const block_type assign_mask = 0 - static_cast<block_type>(bit);
this->m_block |= (this->m_mask & assign_mask);
this->m_block &= ~(this->m_mask & ~assign_mask);
return *this;
}
In our case, this results in the following instructions - without any conditional branch based on the input:
[ ... ]
const block_type assign_mask = 0 - static_cast<block_type>(bit);
41d397: 41 f7 dc neg %r12d
[ ... ]
this->m_block |= (this->m_mask & assign_mask);
41d3ab: 44 89 e1 mov %r12d,%ecx
41d3ae: 21 c1 and %eax,%ecx
this->m_block &= ~(this->m_mask & ~assign_mask);
41d3b0: f7 d0 not %eax
this->m_block |= (this->m_mask & assign_mask);
41d3b2: 0a 0a or (%rdx),%cl
this->m_block &= ~(this->m_mask & ~assign_mask);
41d3b4: 44 09 e0 or %r12d,%eax
41d3b7: 21 c8 and %ecx,%eax
[ ... ]
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for your analysis and your report! I applied your fix using Botan's constant-time helper class (7a0fe59)
@@ -1,6 +1,8 @@ | |||
/* | |||
* An abstraction for an arbitrarily large bitvector that can |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@reneme the code still contains some artefacts for varying the underlying data type that we'd ideally want to get rid of.
5f0efcc
to
54395c2
Compare
e07e615
to
b679113
Compare
f88fe6b
to
5012217
Compare
- constant time conditional swap with mask - floor_log2 Co-Authored-By: Amos Treiber <[email protected]>
This is an implementation of the Classic McEliece KEM according to the NIST Round 4 submission and the ISO draft 20230419. Co-Authored-By: Amos Treiber <[email protected]>
Co-Authored-By: Jack Lloyd <[email protected]>
Co-Authored-By: Fabian Albert <[email protected]>
This allows extracting subvectors of the bitvector as unsigned integral bit masks.
... introduced in randombit#4170
Use CT::poison/unpoison for CMCE. Simultaneously, fixes some constant-time issues in the CMCE implementation that may leak some information about the pivots (for the semi-systematic matrix form). Co-Authored-By: René Meusel <[email protected]>
5012217
to
083e153
Compare
Rebased and resolved several conflicts after all other PQC algorithms got merged. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Some review comments. Also I left some comments in July that never got responded too
@@ -419,6 +429,12 @@ std::unique_ptr<Private_Key> load_private_key(const AlgorithmIdentifier& alg_id, | |||
} | |||
#endif | |||
|
|||
#if defined(BOTAN_HAS_CLASSICMCELIECE) || defined(BOTAN_HAS_CLASSICMCELIECE) | |||
if(alg_name.starts_with("mceliece")) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Uses ClassicMcEliece elsewhere we should be consistent
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We named the algorithm identifiers as defined in the specifications:
The OIDs were taken from the IETF Hackathon GitHub. IMO, we should stay with the same naming. But I agree that it is annoying that these are not named classic...
@@ -266,6 +270,12 @@ std::unique_ptr<Public_Key> load_public_key(const AlgorithmIdentifier& alg_id, | |||
} | |||
#endif | |||
|
|||
#if defined(BOTAN_HAS_CLASSICMCELIECE) || defined(BOTAN_HAS_CLASSICMCELIECE) | |||
if(alg_name.starts_with("mceliece")) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ClassicMcEliece?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
see above
1.3.6.1.4.1.22554.5.1.7 = mceliece6960119 | ||
1.3.6.1.4.1.22554.5.1.8 = mceliece6960119f | ||
1.3.6.1.4.1.22554.5.1.9 = mceliece8192128 | ||
1.3.6.1.4.1.22554.5.1.10 = mceliece8192128f |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Too avoid confusion these should maybe start with ClassicMcEliece-
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
see above
/** | ||
* @brief XOR assign a GF_Elem to the element of this. Constant time. | ||
*/ | ||
Classic_McEliece_GF& operator^=(CmceGfElem other) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't this be += keeping with the additive notation in GF(2)?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note that CmceGfElem
is a strong type for uint16. If I remember correctly, we wanted to distinguish the operation with Classic_McEliece_GF
objects from the operation with a CmceGfElem
. However, while looking for its usage, I found that this operator is not used at all. Oops... I'll remove it ;)
auto g_alpha = goppa_poly(alphas[i]); | ||
auto r = (g_alpha * g_alpha).inv(); | ||
|
||
auto c_mask = GF_Mask(CT::Mask<uint16_t>::expand(code_word.at(i))); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should GF_Mask
have an overload of expand
here?
|
||
} // anonymous namespace | ||
|
||
std::optional<Classic_McEliece_Field_Ordering> Classic_McEliece_Field_Ordering::create_field_ordering( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does the non-constant time nature of std::optional
bring up issues here? If so we should consider using CT::Option
, if not a comment that it's fine would be helpful.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A std::optional
should suffice. The field ordering creation is performed in a rejection sampling loop. I.e., if the result is a nullopt, all secrets are dismissed anyway and a new rejection sampling loop starts from the beginning.
/** | ||
* Renders this bitvector into a sequence of "0"s and "1"s. | ||
*/ | ||
std::string to_string() const { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fine to leave it here
97cc674
to
969d584
Compare
Pull Request Dependencies
Description
This PR relates to the Classic McEliece KEM as specified in this ISO draft. It also contains the instances defined in the NIST Round 4 submission. The test cases were generated using the NIST submissions reference implementation. Note that Classic McEliece (module
cmce
) is not the original McEliece Algorithm that is implemented in Botan'smce
module. See the Classic McEliece homepage, for a brief comparison.TODO Tracker
Use uint8_t as bitvector return type(see: PQC: Classic McEliece #3883 (comment))