| 1 | /* |
| 2 | * Copyright 2017-2018 The OpenSSL Project Authors. All Rights Reserved. |
| 3 | * Copyright 2015-2016 Cryptography Research, Inc. |
| 4 | * |
| 5 | * Licensed under the Apache License 2.0 (the "License"). You may not use |
| 6 | * this file except in compliance with the License. You can obtain a copy |
| 7 | * in the file LICENSE in the source distribution or at |
| 8 | * https://www.openssl.org/source/license.html |
| 9 | * |
| 10 | * Originally written by Mike Hamburg |
| 11 | */ |
| 12 | |
| 13 | #ifndef OSSL_CRYPTO_EC_CURVE448_ED448_H |
| 14 | # define OSSL_CRYPTO_EC_CURVE448_ED448_H |
| 15 | |
| 16 | # include "point_448.h" |
| 17 | |
| 18 | /* Number of bytes in an EdDSA public key. */ |
| 19 | # define EDDSA_448_PUBLIC_BYTES 57 |
| 20 | |
| 21 | /* Number of bytes in an EdDSA private key. */ |
| 22 | # define EDDSA_448_PRIVATE_BYTES EDDSA_448_PUBLIC_BYTES |
| 23 | |
| 24 | /* Number of bytes in an EdDSA private key. */ |
| 25 | # define EDDSA_448_SIGNATURE_BYTES (EDDSA_448_PUBLIC_BYTES + \ |
| 26 | EDDSA_448_PRIVATE_BYTES) |
| 27 | |
| 28 | /* EdDSA encoding ratio. */ |
| 29 | # define C448_EDDSA_ENCODE_RATIO 4 |
| 30 | |
| 31 | /* EdDSA decoding ratio. */ |
| 32 | # define C448_EDDSA_DECODE_RATIO (4 / 4) |
| 33 | |
| 34 | /* |
| 35 | * EdDSA key generation. This function uses a different (non-Decaf) encoding. |
| 36 | * |
| 37 | * pubkey (out): The public key. |
| 38 | * privkey (in): The private key. |
| 39 | */ |
| 40 | c448_error_t c448_ed448_derive_public_key( |
| 41 | OPENSSL_CTX *ctx, |
| 42 | uint8_t pubkey [EDDSA_448_PUBLIC_BYTES], |
| 43 | const uint8_t privkey [EDDSA_448_PRIVATE_BYTES]); |
| 44 | |
| 45 | /* |
| 46 | * EdDSA signing. |
| 47 | * |
| 48 | * signature (out): The signature. |
| 49 | * privkey (in): The private key. |
| 50 | * pubkey (in): The public key. |
| 51 | * message (in): The message to sign. |
| 52 | * message_len (in): The length of the message. |
| 53 | * prehashed (in): Nonzero if the message is actually the hash of something |
| 54 | * you want to sign. |
| 55 | * context (in): A "context" for this signature of up to 255 bytes. |
| 56 | * context_len (in): Length of the context. |
| 57 | * |
| 58 | * For Ed25519, it is unsafe to use the same key for both prehashed and |
| 59 | * non-prehashed messages, at least without some very careful protocol-level |
| 60 | * disambiguation. For Ed448 it is safe. |
| 61 | */ |
| 62 | c448_error_t c448_ed448_sign( |
| 63 | OPENSSL_CTX *ctx, |
| 64 | uint8_t signature[EDDSA_448_SIGNATURE_BYTES], |
| 65 | const uint8_t privkey[EDDSA_448_PRIVATE_BYTES], |
| 66 | const uint8_t pubkey[EDDSA_448_PUBLIC_BYTES], |
| 67 | const uint8_t *message, size_t message_len, |
| 68 | uint8_t prehashed, const uint8_t *context, |
| 69 | size_t context_len); |
| 70 | |
| 71 | /* |
| 72 | * EdDSA signing with prehash. |
| 73 | * |
| 74 | * signature (out): The signature. |
| 75 | * privkey (in): The private key. |
| 76 | * pubkey (in): The public key. |
| 77 | * hash (in): The hash of the message. This object will not be modified by the |
| 78 | * call. |
| 79 | * context (in): A "context" for this signature of up to 255 bytes. Must be the |
| 80 | * same as what was used for the prehash. |
| 81 | * context_len (in): Length of the context. |
| 82 | * |
| 83 | * For Ed25519, it is unsafe to use the same key for both prehashed and |
| 84 | * non-prehashed messages, at least without some very careful protocol-level |
| 85 | * disambiguation. For Ed448 it is safe. |
| 86 | */ |
| 87 | c448_error_t c448_ed448_sign_prehash( |
| 88 | OPENSSL_CTX *ctx, |
| 89 | uint8_t signature[EDDSA_448_SIGNATURE_BYTES], |
| 90 | const uint8_t privkey[EDDSA_448_PRIVATE_BYTES], |
| 91 | const uint8_t pubkey[EDDSA_448_PUBLIC_BYTES], |
| 92 | const uint8_t hash[64], |
| 93 | const uint8_t *context, |
| 94 | size_t context_len); |
| 95 | |
| 96 | /* |
| 97 | * EdDSA signature verification. |
| 98 | * |
| 99 | * Uses the standard (i.e. less-strict) verification formula. |
| 100 | * |
| 101 | * signature (in): The signature. |
| 102 | * pubkey (in): The public key. |
| 103 | * message (in): The message to verify. |
| 104 | * message_len (in): The length of the message. |
| 105 | * prehashed (in): Nonzero if the message is actually the hash of something you |
| 106 | * want to verify. |
| 107 | * context (in): A "context" for this signature of up to 255 bytes. |
| 108 | * context_len (in): Length of the context. |
| 109 | * |
| 110 | * For Ed25519, it is unsafe to use the same key for both prehashed and |
| 111 | * non-prehashed messages, at least without some very careful protocol-level |
| 112 | * disambiguation. For Ed448 it is safe. |
| 113 | */ |
| 114 | c448_error_t c448_ed448_verify(OPENSSL_CTX *ctx, |
| 115 | const uint8_t |
| 116 | signature[EDDSA_448_SIGNATURE_BYTES], |
| 117 | const uint8_t |
| 118 | pubkey[EDDSA_448_PUBLIC_BYTES], |
| 119 | const uint8_t *message, size_t message_len, |
| 120 | uint8_t prehashed, const uint8_t *context, |
| 121 | uint8_t context_len); |
| 122 | |
| 123 | /* |
| 124 | * EdDSA signature verification. |
| 125 | * |
| 126 | * Uses the standard (i.e. less-strict) verification formula. |
| 127 | * |
| 128 | * signature (in): The signature. |
| 129 | * pubkey (in): The public key. |
| 130 | * hash (in): The hash of the message. This object will not be modified by the |
| 131 | * call. |
| 132 | * context (in): A "context" for this signature of up to 255 bytes. Must be the |
| 133 | * same as what was used for the prehash. |
| 134 | * context_len (in): Length of the context. |
| 135 | * |
| 136 | * For Ed25519, it is unsafe to use the same key for both prehashed and |
| 137 | * non-prehashed messages, at least without some very careful protocol-level |
| 138 | * disambiguation. For Ed448 it is safe. |
| 139 | */ |
| 140 | c448_error_t c448_ed448_verify_prehash( |
| 141 | OPENSSL_CTX *ctx, |
| 142 | const uint8_t signature[EDDSA_448_SIGNATURE_BYTES], |
| 143 | const uint8_t pubkey[EDDSA_448_PUBLIC_BYTES], |
| 144 | const uint8_t hash[64], |
| 145 | const uint8_t *context, |
| 146 | uint8_t context_len); |
| 147 | |
| 148 | /* |
| 149 | * EdDSA point encoding. Used internally, exposed externally. |
| 150 | * Multiplies by C448_EDDSA_ENCODE_RATIO first. |
| 151 | * |
| 152 | * The multiplication is required because the EdDSA encoding represents |
| 153 | * the cofactor information, but the Decaf encoding ignores it (which |
| 154 | * is the whole point). So if you decode from EdDSA and re-encode to |
| 155 | * EdDSA, the cofactor info must get cleared, because the intermediate |
| 156 | * representation doesn't track it. |
| 157 | * |
| 158 | * The way we handle this is to multiply by C448_EDDSA_DECODE_RATIO when |
| 159 | * decoding, and by C448_EDDSA_ENCODE_RATIO when encoding. The product of |
| 160 | * these ratios is always exactly the cofactor 4, so the cofactor ends up |
| 161 | * cleared one way or another. But exactly how that shakes out depends on the |
| 162 | * base points specified in RFC 8032. |
| 163 | * |
| 164 | * The upshot is that if you pass the Decaf/Ristretto base point to |
| 165 | * this function, you will get C448_EDDSA_ENCODE_RATIO times the |
| 166 | * EdDSA base point. |
| 167 | * |
| 168 | * enc (out): The encoded point. |
| 169 | * p (in): The point. |
| 170 | */ |
| 171 | void curve448_point_mul_by_ratio_and_encode_like_eddsa( |
| 172 | uint8_t enc [EDDSA_448_PUBLIC_BYTES], |
| 173 | const curve448_point_t p); |
| 174 | |
| 175 | /* |
| 176 | * EdDSA point decoding. Multiplies by C448_EDDSA_DECODE_RATIO, and |
| 177 | * ignores cofactor information. |
| 178 | * |
| 179 | * See notes on curve448_point_mul_by_ratio_and_encode_like_eddsa |
| 180 | * |
| 181 | * enc (out): The encoded point. |
| 182 | * p (in): The point. |
| 183 | */ |
| 184 | c448_error_t curve448_point_decode_like_eddsa_and_mul_by_ratio( |
| 185 | curve448_point_t p, |
| 186 | const uint8_t enc[EDDSA_448_PUBLIC_BYTES]); |
| 187 | |
| 188 | /* |
| 189 | * EdDSA to ECDH private key conversion |
| 190 | * Using the appropriate hash function, hash the EdDSA private key |
| 191 | * and keep only the lower bytes to get the ECDH private key |
| 192 | * |
| 193 | * x (out): The ECDH private key as in RFC7748 |
| 194 | * ed (in): The EdDSA private key |
| 195 | */ |
| 196 | c448_error_t c448_ed448_convert_private_key_to_x448( |
| 197 | OPENSSL_CTX *ctx, |
| 198 | uint8_t x[X448_PRIVATE_BYTES], |
| 199 | const uint8_t ed[EDDSA_448_PRIVATE_BYTES]); |
| 200 | |
| 201 | #endif /* OSSL_CRYPTO_EC_CURVE448_ED448_H */ |
| 202 | |