Skip to content

Commit bacec5a

Browse files
committed
progress at applying #to all crates
1 parent 1dc415b commit bacec5a

9 files changed

Lines changed: 200 additions & 37 deletions

File tree

CONTRIBUTING.md

Lines changed: 66 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,76 +2,117 @@
22

33
Thank you for contributing to Bouncy Castle!
44

5-
In this guide, you get an overview of the contribution workflow from starting a discussion or opening an issue, to creating, reviewing, and merging a pull request.
5+
In this guide, you get an overview of the contribution workflow from starting a discussion or opening an issue, to
6+
creating, reviewing, and merging a pull request.
67

7-
For an overview of the project, see [README](README.md).
8+
For an overview of the project, see [README](README.md).
89

910
## Start a discussion
10-
If you have a question or problem, you can [search in discussions](https://github.com/bcgit/bc-rust/discussions), if someone has already found a solution to your problem.
1111

12-
Or you can [start a new discussion](https://github.com/bcgit/bc-rust/discussions/new/choose) and ask your question.
12+
If you have a question or problem, you can [search in discussions](https://github.com/bcgit/bc-rust/discussions), if
13+
someone has already found a solution to your problem.
14+
15+
Or you can [start a new discussion](https://github.com/bcgit/bc-rust/discussions/new/choose) and ask your question.
1316

1417
## Create an issue
1518

1619
If you find a problem with Bouncy Castle, [search if an issue already exists](https://github.com/bcgit/bc-rust/issues).
1720

1821
> **_NOTE:_** If the issue is a __potential security problem__, please contact us
19-
before posting anything public. See [Security Policy](SECURITY.md).
22+
> before posting anything public. See [Security Policy](SECURITY.md).
2023
21-
If a related discussion or issue doesn't exist, and the issue is not security related, you can [open a new issue](https://github.com/bcgit/bc-java/issues/new). An issue can be converted into a discussion if regarded as one.
24+
If a related discussion or issue doesn't exist, and the issue is not security related, you
25+
can [open a new issue](https://github.com/bcgit/bc-java/issues/new). An issue can be converted into a discussion if
26+
regarded as one.
2227

2328
## Coding philosophy
2429

2530
> Slow is smooth, smooth is fast.
2631
27-
There is a time and a place for "Move fast and break things", but the source code of a crypto library is not one of them.
32+
There is a time and a place for "Move fast and break things", but the source code of a crypto library is not one of
33+
them.
2834

29-
This project takes the philosophy that taking the time to do things right pays off in the long run, both in terms of
30-
the runtime and memory footprint of the code, and it terms of the time required for a future maintainer to get up to speed with the code
35+
This project takes the philosophy that taking the time to do things right pays off in the long run, both in terms of
36+
the runtime and memory footprint of the code, and it terms of the time required for a future maintainer to get up to
37+
speed with the code
3138
and avoid introducing bugs due to the code being hard to understand.
3239

3340
Some specifics:
3441

35-
* Respect that the innovative process sometimes requires exploring several dead-ends before you find the most elegant solution.
36-
* Public APIs of a library should be both ergonomic and expressive. When defining a new trait or public function, ask yourself whether a programmer who is new to cryptography is likely to use this in a way that will get them into trouble.
37-
* Variables should be well-named, well-structured, and well-commented (a comment-to-code ration of 1:1 is a goal to be strived for!). Think about memory footprint and, where possible, use unnamed scopes to allow the compiler to pop intermediate value variables off the stack as soon as they are no longer needed.
38-
* Always run your code through `cargo mutants` and get the issue count as low as your can. As a first pass, this forces you to write thorough unit tests. As a second pass, this draws your attention to bits of your code that cannot be tested from the outside. Often this means that the code can be simplified without affecting functionality (as defined by your set of unit tests) -- "simpler code" usually means faster runtime and easier future maintenance.
42+
* Respect that the innovative process sometimes requires exploring several dead-ends before you find the most elegant
43+
solution.
44+
* Public APIs of a library should be both ergonomic and expressive. When defining a new trait or public function, ask
45+
yourself whether a programmer who is new to cryptography is likely to use this in a way that will get them into
46+
trouble.
47+
* Variables should be well-named, well-structured, and well-commented (a comment-to-code ration of 1:1 is a goal to be
48+
strived for!). Think about memory footprint and, where possible, use unnamed scopes to allow the compiler to pop
49+
intermediate value variables off the stack as soon as they are no longer needed.
50+
* Always run your code through `cargo mutants` and get the issue count as low as your can. As a first pass, this forces
51+
you to write thorough unit tests. As a second pass, this draws your attention to bits of your code that cannot be
52+
tested from the outside. Often this means that the code can be simplified without affecting functionality (as defined
53+
by your set of unit tests) -- "simpler code" usually means faster runtime and easier future maintenance.
3954

4055
## Contribute to the code
4156

42-
For substantial, non-trivial contributions, you may be asked to sign a contributor assignment agreement. Optionally, you can also have your name and contact information listed in [Contributors](https://www.bouncycastle.org/contributors.html).
57+
For substantial, non-trivial contributions, you may be asked to sign a contributor assignment agreement. Optionally, you
58+
can also have your name and contact information listed
59+
in [Contributors](https://www.bouncycastle.org/contributors.html).
4360

44-
Please note we are unable to accept contributions which cannot be released under the [Bouncy Castle License](https://www.bouncycastle.org/licence.html). Issuing a pull request on our public github mirror is taken as agreement to issuing under the Bouncy Castle License.
61+
Please note we are unable to accept contributions which cannot be released under
62+
the [Bouncy Castle License](https://www.bouncycastle.org/licence.html). Issuing a pull request on our public github
63+
mirror is taken as agreement to issuing under the Bouncy Castle License.
4564

4665
### Create a pull request
4766

4867
> **_NOTE:_** If the issue is a __potential security problem__, please contact us. See [Security Policy](SECURITY.md).
4968
50-
You are welcome to send patches, under the Bouncy Castle License, as pull requests. For more information, see [Creating a pull request](https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/proposing-changes-to-your-work-with-pull-requests/creating-a-pull-request). For minor updates, you can instead choose to create an issue with short snippets of code. See above.
69+
You are welcome to send patches, under the Bouncy Castle License, as pull requests. For more information,
70+
see [Creating a pull request](https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/proposing-changes-to-your-work-with-pull-requests/creating-a-pull-request).
71+
For minor updates, you can instead choose to create an issue with short snippets of code. See above.
5172

52-
* For contributions touching multiple files try and split up the pull request, smaller changes are easier to review and test, as well as being less likely to run into merge issues.
53-
* Create a test cases for your change, it may be a simple addition to an existing test. If you do not know how to do this, ask us and we will help you.
54-
* If you run into any merge issues, check out this [git tutorial](https://github.com/skills/resolve-merge-conflicts) to help you resolve merge conflicts and other issues.
73+
* For contributions touching multiple files try and split up the pull request, smaller changes are easier to review and
74+
test, as well as being less likely to run into merge issues.
75+
* Create a test cases for your change, it may be a simple addition to an existing test. If you do not know how to do
76+
this, ask us and we will help you.
77+
* If you run into any merge issues, check out this [git tutorial](https://github.com/skills/resolve-merge-conflicts) to
78+
help you resolve merge conflicts and other issues.
5579

56-
For more information, refer to the Bouncy Castle documentation on [Getting Started with Bouncy Castle](https://doc.primekey.com/bouncycastle/introduction#Introduction-GettingStartedwithBouncyCastle).
80+
For more information, refer to the Bouncy Castle documentation
81+
on [Getting Started with Bouncy Castle](https://doc.primekey.com/bouncycastle/introduction#Introduction-GettingStartedwithBouncyCastle).
5782

5883
### Quality Standards
84+
5985
Except where otherwise noted, all crates must have:
6086

6187
* benchmarks
6288
* unit tests that (mostly) satisfy cargo mutants
6389
* lib.rs needs to compile with: #![forbid(missing_docs)], #![no_std]
64-
* Fallibility: as much as humanly possible, Result and unwrap() should be used for "Bad input data" type things and not "Programmer didn't read the docs" type things. Things like \[u8]'s of the wrong length, or trying to call an algorithm with a key of the wrong parameter set should be detected at compile time via the typing system and should not require a Result / unwrap() mechanism. Please run `./dev_scripts/quality_stats.sh` before and after your change to see if you have increased the fallibility of the code you changed.
90+
* Fallibility: as much as humanly possible, Result and unwrap() should be used for "Bad input data" type things and
91+
not "Programmer didn't read the docs" type things. Things like \[u8]'s of the wrong length, or trying to call an
92+
algorithm with a key of the wrong parameter set should be detected at compile time via the typing system and should
93+
not require a Result / unwrap() mechanism. Please run `./dev_scripts/quality_stats.sh` before and after your change to
94+
see if you have increased the fallibility of the code you changed.
6595

66-
Code submissions that do not meet these standards, or that require significant effort from the maintainers in order to meet these standards, will not be accepted.
96+
Code submissions that do not meet these standards, or that require significant effort from the maintainers in order to
97+
meet these standards, will not be accepted.
6798

6899
### Self-review
69100

70101
Don't forget to self-review. Please follow these simple guidelines:
71-
* Keep the patch limited, only change the parts related to your patch.
72-
* Do not change other lines, such as whitespace, adding line breaks to Java doc, etc. It will make it very hard for us to review the patch.
102+
103+
* Keep the patch limited, only change the parts related to your patch.
104+
* Do not change other lines, such as whitespace, adding line breaks to Java doc, etc. It will make it very hard for us
105+
to review the patch.
73106

74107
#### Your pull request is merged
75108

76-
Someone on the Bouncy Castle core team will review the pull request when there is time, and let you know if something is missing or suggest improvements. If it is a useful and generic feature it will be integrated in Bouncy Castle to be available in a later release.
109+
Someone on the Bouncy Castle core team will review the pull request when there is time, and let you know if something is
110+
missing or suggest improvements. If it is a useful and generic feature it will be integrated in Bouncy Castle to be
111+
available in a later release.
112+
113+
## AI Policy
77114

115+
LLM-based coding assistants are a great tool, but, especially for a cryptography library, they must be used under the
116+
strict supervision of a human who takes responsibility for the submitted code, code review process, and for asserting
117+
that the submission does not include copyrighted material and is assignable to the Legion of the Bouncy Castle.
118+
Contributions not clearly from a human will be rejected.

alpha_0.1.2_release_notes.md

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,14 @@
2222
appropriate.
2323
* Probably it makes sense to leave Hex and Base64 as requiring std; ... or maybe add a no_std version that uses
2424
fixed-sized blocks?
25-
* Make this build on the stable compiler. IE Remove the rust-toolchain.toml file that builds with nightly. Will require
26-
some refactoring.
2725
* Create a cargo feature #[cfg(feature='rng')] and put it around things like keygen that takes an rng so that the build
2826
dependency on bouncycastle_rng is optional.
2927
* Factories ... Are they worth it? Michael Richardson says Very Yes. If we are keeping them, then we need a serious
3028
re-engineering of them because I really dislike that currently they make it hard for the underlying primitive to have
3129
static one-shot APIs.
3230
* Deal with as many of the inline TODOs as possible
3331
* Close all open github issues and document them in this file.
34-
* After everything is merged, circle back to crucible, and make sure that the harness still works (and maybe remove the
35-
nightly build toolchain)
32+
* After everything is merged, circle back to crucible, and make sure that the harness still works
3633
* Search for all the uses of .unwrap() in non-test code and replace each with either a comment or an expect with a
3734
meaningful error string.
3835

@@ -59,6 +56,7 @@
5956
.drop_hazardous_operations(), it now uses a closure-based do_hazardous_operations(). Github issue #39.
6057
* Renamed KeyMaterial::KeyType's and deleted KeyMaterial::concatenate in order to give a better intuition and
6158
FIPS-alignment.
59+
* Removed the dependence on nightly / experimental compiler features; the library now buildds on stable.
6260
* Github issues resolved:
6361
* #6: https://github.com/bcgit/bc-rust/issues/6, thanks to Q. T. Felix (github: @Quant-TheodoreFelix)
6462
* #10: https://github.com/bcgit/bc-rust/issues/10, thanks to Nicola Tuveri (github: @romen)

crypto/base64/src/lib.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,9 @@
8181
// /// "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_="
8282
// URLSafe,
8383

84+
#![forbid(unsafe_code)]
85+
#![forbid(missing_docs)]
86+
8487
use bouncycastle_utils::ct::Condition;
8588

8689
/// One-shot encode from bytes to a base64-encoded string using a constant-time implementation.
@@ -93,6 +96,7 @@ pub fn decode<T: AsRef<[u8]>>(input: T) -> Result<Vec<u8>, Base64Error> {
9396
Base64Decoder::new(true).do_final(input)
9497
}
9598

99+
/// Return type for errors relating to Base64 encoding and decoding.
96100
#[derive(Debug)]
97101
pub enum Base64Error {
98102
/// the do_update() method must not be called on a block that contains padding.
@@ -141,6 +145,8 @@ impl Base64Encoder {
141145
ret as u8
142146
}
143147

148+
/// Streaming API that performs Base64 encoding of the provided input, but does not apply
149+
/// the final padding and will hold an incomplete block while waiting for more input.
144150
pub fn do_update<T: AsRef<[u8]>>(&mut self, input: T) -> String {
145151
let inref = input.as_ref();
146152
let mut out: Vec<u8> = Vec::with_capacity(inref.len() * 4 / 3 + 4);
@@ -245,6 +251,8 @@ impl Base64Decoder {
245251
ret as u8
246252
}
247253

254+
/// Streaming API that performs Base64 encoding of the provided input, but does not apply
255+
/// the final padding and will hold an incomplete block while waiting for more input.
248256
pub fn do_update<T: AsRef<[u8]>>(&mut self, input: T) -> Result<Vec<u8>, Base64Error> {
249257
self.decode_internal(input, true)
250258
}

crypto/core/src/errors.rs

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,99 @@
1+
//! Core error types to be used in Result return types.
2+
//! Errors defined in this module are typically one-to-one with traits defined in [crate::traits].
3+
//!
4+
//! Most errors are self-explanatory, but additional description is available on some.
5+
6+
///
17
#[derive(Debug)]
28
pub enum HashError {
9+
///
310
GenericError(&'static str),
11+
///
412
InvalidLength(&'static str),
13+
///
514
InvalidState(&'static str),
15+
///
616
InvalidInput(&'static str),
17+
///
718
KeyMaterialError(KeyMaterialError),
819
}
920

21+
///
1022
#[derive(Debug)]
1123
pub enum KeyMaterialError {
24+
///
1225
ActingOnZeroizedKey,
26+
///
1327
GenericError(&'static str),
28+
///
1429
HazardousOperationNotPermitted,
30+
///
1531
InputDataLongerThanKeyCapacity,
32+
///
1633
InvalidKeyType(&'static str),
34+
///
1735
InvalidLength,
36+
///
1837
SecurityStrength(&'static str),
1938
}
2039

40+
///
2141
#[derive(Debug)]
2242
pub enum KDFError {
43+
///
2344
GenericError(&'static str),
45+
///
2446
HashError(HashError),
47+
///
2548
InvalidLength(&'static str),
49+
///
2650
KeyMaterialError(KeyMaterialError),
51+
///
2752
MACError(MACError),
2853
}
2954

55+
///
3056
#[derive(Debug)]
3157
pub enum KEMError {
58+
///
3259
GenericError(&'static str),
60+
///
3361
ConsistencyCheckFailed(&'static str),
62+
///
3463
EncodingError(&'static str),
64+
///
3565
DecapsulationFailed,
66+
///
3667
DecodingError(&'static str),
68+
///
3769
KeyGenError(&'static str),
70+
///
3871
KeyMaterialError(KeyMaterialError),
72+
///
3973
LengthError(&'static str),
74+
///
4075
RNGError(RNGError),
4176
}
4277

78+
///
4379
#[derive(Debug)]
4480
pub enum MACError {
81+
///
4582
GenericError(&'static str),
83+
///
4684
HashError(HashError),
85+
///
4786
InvalidLength(&'static str),
87+
///
4888
InvalidState(&'static str),
89+
///
4990
KeyMaterialError(KeyMaterialError),
5091
}
5192

93+
///
5294
#[derive(Debug)]
5395
pub enum RNGError {
96+
///
5497
GenericError(&'static str),
5598

5699
/// Attempting to extract output before the RNG has been seeded.
@@ -69,9 +112,11 @@ pub enum RNGError {
69112
/// than the algorithm requires.
70113
SecurityStrengthInsufficientForAlgorithm,
71114

115+
///
72116
KeyMaterialError(KeyMaterialError),
73117
}
74118

119+
///
75120
#[derive(Debug)]
76121
pub enum SuspendableError {
77122
/// The serialized state was produced by a library version incompatible with this one.
@@ -80,16 +125,26 @@ pub enum SuspendableError {
80125
InvalidData,
81126
}
82127

128+
///
83129
#[derive(Debug)]
84130
pub enum SignatureError {
131+
///
85132
GenericError(&'static str),
133+
///
86134
ConsistencyCheckFailed(),
135+
///
87136
EncodingError(&'static str),
137+
///
88138
DecodingError(&'static str),
139+
///
89140
KeyGenError(&'static str),
141+
///
90142
KeyMaterialError(KeyMaterialError),
143+
///
91144
LengthError(&'static str),
145+
///
92146
SignatureVerificationFailed,
147+
///
93148
RNGError(RNGError),
94149
}
95150

crypto/core/src/key_material.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,11 @@ use core::fmt;
5959

6060
/// Sometimes you just need a zero-length dummy key.
6161
pub type KeyMaterial0 = KeyMaterial<0>;
62-
62+
/// Named type for a 128-bit (16-byte) key, for convenience.
6363
pub type KeyMaterial128 = KeyMaterial<16>;
64+
/// Named type for a 256-bit (32-byte) key, for convenience.
6465
pub type KeyMaterial256 = KeyMaterial<32>;
66+
/// Named type for a 512-bit (64-byte) key, for convenience.
6567
pub type KeyMaterial512 = KeyMaterial<64>;
6668

6769
/// A helper class used across the bc-rust.test library to hold bytes-like key material.
@@ -153,6 +155,7 @@ pub trait KeyMaterialTrait: KeyMaterialInternalTrait {
153155
/// [do_hazardous_operations] closure.
154156
fn set_key_len(&mut self, key_len: usize) -> Result<(), KeyMaterialError>;
155157

158+
/// Returns the [KeyType] of this KeyMaterial object.
156159
fn key_type(&self) -> KeyType;
157160

158161
/// Sets (or safely converts) the [KeyType] of this KeyMaterial object.
@@ -225,6 +228,7 @@ impl<const KEY_LEN: usize> Secret for KeyMaterial<KEY_LEN> {}
225228
// `SerializableState` implementations (see the `TryFrom<u8>` impl below). Pin each value to its
226229
// variant name: reordering variants is fine, but never reuse or renumber an existing discriminant,
227230
// or previously-serialized states will be misread.
231+
///
228232
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
229233
#[repr(u8)]
230234
pub enum KeyType {
@@ -279,6 +283,8 @@ impl<const KEY_LEN: usize> Default for KeyMaterial<KEY_LEN> {
279283
}
280284

281285
impl<const KEY_LEN: usize> KeyMaterial<KEY_LEN> {
286+
/// Creates a new empty instance (key_len = 0, key_type = Zeroized).
287+
/// If you want a properly populated instance, use [KeyMaterial::from_rng].
282288
pub fn new() -> Self {
283289
Self {
284290
buf: [0u8; KEY_LEN],
@@ -289,7 +295,7 @@ impl<const KEY_LEN: usize> KeyMaterial<KEY_LEN> {
289295
}
290296
}
291297

292-
/// Create a new instance of KeyMaterial containing random bytes from the provided random number generator.
298+
/// Creates a new instance of KeyMaterial containing random bytes from the provided random number generator.
293299
pub fn from_rng(rng: &mut impl RNG) -> Result<Self, KeyMaterialError> {
294300
let mut key = Self::new();
295301

0 commit comments

Comments
 (0)