| 6 | // |
| 7 | // https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 |
| 15 | #include <openssl/mldsa.h> |
| 16 |
| 17 | #include "../fipsmodule/bcm_interface.h" |
| 18 |
| 19 | static_assert(sizeof(BCM_mldsa65_private_key) == sizeof(MLDSA65_private_key), |
| 20 | ""); |
| 21 | static_assert(alignof(BCM_mldsa65_private_key) == alignof(MLDSA65_private_key), |
| 22 | ""); |
| 23 | static_assert(sizeof(BCM_mldsa65_public_key) == sizeof(MLDSA65_public_key), ""); |
| 24 | static_assert(alignof(BCM_mldsa65_public_key) == alignof(MLDSA65_public_key), |
| 25 | ""); |
| 26 | static_assert(MLDSA_SEED_BYTES == BCM_MLDSA_SEED_BYTES, ""); |
| 27 | static_assert(MLDSA65_PRIVATE_KEY_BYTES == BCM_MLDSA65_PRIVATE_KEY_BYTES, ""); |
| 28 | static_assert(MLDSA65_PUBLIC_KEY_BYTES == BCM_MLDSA65_PUBLIC_KEY_BYTES, ""); |
| 29 | static_assert(MLDSA65_SIGNATURE_BYTES == BCM_MLDSA65_SIGNATURE_BYTES, ""); |
| 30 |
| 31 | int MLDSA65_generate_key( |
| 32 | uint8_t out_encoded_public_key[MLDSA65_PUBLIC_KEY_BYTES], |
| 33 | uint8_t out_seed[MLDSA_SEED_BYTES], |
| 34 | struct MLDSA65_private_key *out_private_key) { |
| 35 | return bcm_success(BCM_mldsa65_generate_key( |
| 36 | out_encoded_public_key, out_seed, |
| 37 | reinterpret_cast<BCM_mldsa65_private_key *>(out_private_key))); |
| 38 | } |
| 39 |
| 40 | int MLDSA65_private_key_from_seed(struct MLDSA65_private_key *out_private_key, |
| 41 | const uint8_t *seed, size_t seed_len) { |
| 42 | if (seed_len != BCM_MLDSA_SEED_BYTES) { |
| 43 | return 0; |
| 44 | } |
| 45 | return bcm_success(BCM_mldsa65_private_key_from_seed( |
| 46 | reinterpret_cast<BCM_mldsa65_private_key *>(out_private_key), seed)); |
| 47 | } |
| 48 |
| 49 | int MLDSA65_public_from_private(struct MLDSA65_public_key *out_public_key, |
| 50 | const struct MLDSA65_private_key *private_key) { |
| 51 | return bcm_success(BCM_mldsa65_public_from_private( |
| 52 | reinterpret_cast<BCM_mldsa65_public_key *>(out_public_key), |
| 53 | reinterpret_cast<const BCM_mldsa65_private_key *>(private_key))); |
| 54 | } |
| 55 |
| 56 | int MLDSA65_sign(uint8_t out_encoded_signature[MLDSA65_SIGNATURE_BYTES], |
| 57 | const struct MLDSA65_private_key *private_key, |
| 58 | const uint8_t *msg, size_t msg_len, const uint8_t *context, |
| 59 | size_t context_len) { |
| 60 | if (context_len > 255) { |
| 61 | return 0; |
| 62 | } |
| 63 | return bcm_success(BCM_mldsa65_sign( |
| 64 | out_encoded_signature, |
| 65 | reinterpret_cast<const BCM_mldsa65_private_key *>(private_key), msg, |
| 66 | msg_len, context, context_len)); |
| 67 | } |
| 68 |
| 69 | int MLDSA65_verify(const struct MLDSA65_public_key *public_key, |
| 70 | const uint8_t *signature, size_t signature_len, |
| 71 | const uint8_t *msg, size_t msg_len, const uint8_t *context, |
| 72 | size_t context_len) { |
| 73 | if (context_len > 255 || signature_len != BCM_MLDSA65_SIGNATURE_BYTES) { |
| 74 | return 0; |
| 75 | } |
| 76 | return bcm_success(BCM_mldsa65_verify( |
| 77 | reinterpret_cast<const BCM_mldsa65_public_key *>(public_key), signature, |
| 78 | msg, msg_len, context, context_len)); |
| 79 | } |
| 80 |
| 81 | int MLDSA65_marshal_public_key(CBB *out, |
| 82 | const struct MLDSA65_public_key *public_key) { |
| 83 | return bcm_success(BCM_mldsa65_marshal_public_key( |
| 84 | out, reinterpret_cast<const BCM_mldsa65_public_key *>(public_key))); |
| 85 | } |
| 86 |
| 87 | int MLDSA65_parse_public_key(struct MLDSA65_public_key *public_key, CBS *in) { |
| 88 | return bcm_success(BCM_mldsa65_parse_public_key( |
| 89 | reinterpret_cast<BCM_mldsa65_public_key *>(public_key), in)); |
| 90 | } |
| 91 |