|
| 1 | +/* |
| 2 | + * Copyright (c) The mldsa-native project authors |
| 3 | + * SPDX-License-Identifier: Apache-2.0 OR ISC OR MIT |
| 4 | + */ |
| 5 | + |
| 6 | +#include "prehash.h" |
| 7 | +#include "symmetric.h" |
| 8 | + |
| 9 | +#define MLD_PRE_HASH_OID_LEN 11 |
| 10 | + |
| 11 | +/************************************************* |
| 12 | + * Name: mld_get_hash_oid |
| 13 | + * |
| 14 | + * Description: Returns the OID of a given SHA-2/SHA-3 hash function. |
| 15 | + * |
| 16 | + * Arguments: - uint8_t oid[11]: pointer to output oid |
| 17 | + * - mld_hash_alg_t hashAlg: hash algorithm enumeration |
| 18 | + * |
| 19 | + **************************************************/ |
| 20 | +static void mld_get_hash_oid(uint8_t oid[MLD_PRE_HASH_OID_LEN], |
| 21 | + mld_hash_alg_t hashAlg) |
| 22 | +{ |
| 23 | + unsigned int i; |
| 24 | + static const struct |
| 25 | + { |
| 26 | + mld_hash_alg_t alg; |
| 27 | + uint8_t oid[MLD_PRE_HASH_OID_LEN]; |
| 28 | + } oid_map[] = { |
| 29 | + {MLD_SHA2_224, |
| 30 | + {0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x04}}, |
| 31 | + {MLD_SHA2_256, |
| 32 | + {0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01}}, |
| 33 | + {MLD_SHA2_384, |
| 34 | + {0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x02}}, |
| 35 | + {MLD_SHA2_512, |
| 36 | + {0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x03}}, |
| 37 | + {MLD_SHA2_512_224, |
| 38 | + {0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x05}}, |
| 39 | + {MLD_SHA2_512_256, |
| 40 | + {0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x06}}, |
| 41 | + {MLD_SHA3_224, |
| 42 | + {0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x07}}, |
| 43 | + {MLD_SHA3_256, |
| 44 | + {0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x08}}, |
| 45 | + {MLD_SHA3_384, |
| 46 | + {0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x09}}, |
| 47 | + {MLD_SHA3_512, |
| 48 | + {0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x0A}}, |
| 49 | + {MLD_SHAKE_128, |
| 50 | + {0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x0B}}, |
| 51 | + {MLD_SHAKE_256, |
| 52 | + {0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x0C}}}; |
| 53 | + |
| 54 | + for (i = 0; i < sizeof(oid_map) / sizeof(oid_map[0]); i++) |
| 55 | + __loop__( |
| 56 | + invariant(i <= sizeof(oid_map) / sizeof(oid_map[0])) |
| 57 | + ) |
| 58 | + { |
| 59 | + if (oid_map[i].alg == hashAlg) |
| 60 | + { |
| 61 | + mld_memcpy(oid, oid_map[i].oid, MLD_PRE_HASH_OID_LEN); |
| 62 | + return; |
| 63 | + } |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +int mld_validate_hash_length(mld_hash_alg_t hashAlg, size_t len) |
| 68 | +{ |
| 69 | + switch (hashAlg) |
| 70 | + { |
| 71 | + case MLD_SHA2_224: |
| 72 | + return (len == 224 / 8) ? 0 : -1; |
| 73 | + case MLD_SHA2_256: |
| 74 | + return (len == 256 / 8) ? 0 : -1; |
| 75 | + case MLD_SHA2_384: |
| 76 | + return (len == 384 / 8) ? 0 : -1; |
| 77 | + case MLD_SHA2_512: |
| 78 | + return (len == 512 / 8) ? 0 : -1; |
| 79 | + case MLD_SHA2_512_224: |
| 80 | + return (len == 224 / 8) ? 0 : -1; |
| 81 | + case MLD_SHA2_512_256: |
| 82 | + return (len == 256 / 8) ? 0 : -1; |
| 83 | + case MLD_SHA3_224: |
| 84 | + return (len == 224 / 8) ? 0 : -1; |
| 85 | + case MLD_SHA3_256: |
| 86 | + return (len == 256 / 8) ? 0 : -1; |
| 87 | + case MLD_SHA3_384: |
| 88 | + return (len == 384 / 8) ? 0 : -1; |
| 89 | + case MLD_SHA3_512: |
| 90 | + return (len == 512 / 8) ? 0 : -1; |
| 91 | + case MLD_SHAKE_128: |
| 92 | + return (len == 256 / 8) ? 0 : -1; |
| 93 | + case MLD_SHAKE_256: |
| 94 | + return (len == 512 / 8) ? 0 : -1; |
| 95 | + } |
| 96 | + return -1; |
| 97 | +} |
| 98 | + |
| 99 | +size_t mld_format_pre_hash_message( |
| 100 | + uint8_t fmsg[MLD_PRE_HASH_MAX_FORMATTED_MESSAGE_BYTES], const uint8_t *ph, |
| 101 | + size_t phlen, const uint8_t *ctx, size_t ctxlen, mld_hash_alg_t hashAlg) |
| 102 | +{ |
| 103 | + /* Format: 0x01 || ctxlen (1 byte) || ctx || oid (11 bytes) || ph */ |
| 104 | + fmsg[0] = 1; |
| 105 | + fmsg[1] = (uint8_t)ctxlen; |
| 106 | + |
| 107 | + /* Copy context if present */ |
| 108 | + if (ctxlen > 0) |
| 109 | + { |
| 110 | + mld_memcpy(fmsg + 2, ctx, ctxlen); |
| 111 | + } |
| 112 | + |
| 113 | + /* Write OID */ |
| 114 | + mld_get_hash_oid(fmsg + 2 + ctxlen, hashAlg); |
| 115 | + |
| 116 | + /* Copy pre-hash */ |
| 117 | + mld_memcpy(fmsg + 2 + ctxlen + MLD_PRE_HASH_OID_LEN, ph, phlen); |
| 118 | + |
| 119 | + /* Return total formatted message length */ |
| 120 | + return 2 + ctxlen + MLD_PRE_HASH_OID_LEN + phlen; |
| 121 | +} |
0 commit comments