| 1 | #ifndef AWS_CAL_ED25519_H |
| 2 | #define AWS_CAL_ED25519_H |
| 3 | /** |
| 4 | * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 5 | * SPDX-License-Identifier: Apache-2.0. |
| 6 | */ |
| 7 | #include <aws/cal/cal.h> |
| 8 | #include <aws/common/byte_buf.h> |
| 9 | |
| 10 | AWS_PUSH_SANE_WARNING_LEVEL |
| 11 | |
| 12 | struct aws_ed25519_key_pair; |
| 13 | |
| 14 | AWS_EXTERN_C_BEGIN |
| 15 | |
| 16 | /** |
| 17 | * Generate new Ed25519 key. |
| 18 | * Returns a new instance of aws_ed25519_key_pair if the key was successfully generated. |
| 19 | * Otherwise returns NULL. |
| 20 | * Note: keygen is not supported on all platforms and will return NULL for the key |
| 21 | * and raise AWS_ERROR_CAL_UNSUPPORTED_ALGORITHM. |
| 22 | * Examples of unsupported cases: |
| 23 | * - openssl pre 1.1.1 (Note: aws-lc and boringssl both expose the needed functions) |
| 24 | * - win/mac builds without special flag that forces linking to libcrypto to support this |
| 25 | */ |
| 26 | AWS_CAL_API struct aws_ed25519_key_pair *aws_ed25519_key_pair_new_generate(struct aws_allocator *allocator); |
| 27 | |
| 28 | /** |
| 29 | * Adds one to an Ed25519 key pair's ref count. |
| 30 | * Returns key_pair pointer. |
| 31 | */ |
| 32 | AWS_CAL_API struct aws_ed25519_key_pair *aws_ed25519_key_pair_acquire(struct aws_ed25519_key_pair *key_pair); |
| 33 | |
| 34 | /** |
| 35 | * Subtracts one from an Ed25519 key pair's ref count. If ref count reaches zero, the key pair is destroyed. |
| 36 | * Always returns NULL. |
| 37 | */ |
| 38 | AWS_CAL_API struct aws_ed25519_key_pair *aws_ed25519_key_pair_release(struct aws_ed25519_key_pair *key_pair); |
| 39 | |
| 40 | enum aws_ed25519_key_export_format { |
| 41 | /* Export the key as raw bytes */ |
| 42 | AWS_CAL_ED25519_KEY_EXPORT_RAW, |
| 43 | |
| 44 | /** |
| 45 | * Export the key to openssh format. |
| 46 | * This will only export the key block, framing (i.e. pem) is left as exercise for the caller. |
| 47 | * b64 encoding is done as convenience since common framing formats require it. |
| 48 | */ |
| 49 | AWS_CAL_ED25519_KEY_EXPORT_OPENSSH_B64, |
| 50 | }; |
| 51 | |
| 52 | /* |
| 53 | * Get public key for the key pair. |
| 54 | * Key in specified format is appended to the buffer. |
| 55 | * The buffer must be initialized before this call, with sufficient capacity to hold the result. |
| 56 | * Use aws_ed25519_key_pair_get_public_key_size to figure out how much capacity buffer needs for a given format. |
| 57 | */ |
| 58 | AWS_CAL_API int aws_ed25519_key_pair_get_public_key( |
| 59 | const struct aws_ed25519_key_pair *key_pair, |
| 60 | enum aws_ed25519_key_export_format format, |
| 61 | struct aws_byte_buf *out); |
| 62 | |
| 63 | /** |
| 64 | * Gets the size of the exported public key. |
| 65 | */ |
| 66 | AWS_CAL_API size_t aws_ed25519_key_pair_get_public_key_size(enum aws_ed25519_key_export_format format); |
| 67 | |
| 68 | /* |
| 69 | * Get private key for the key pair. |
| 70 | * Key in specified format is appended to the buffer. |
| 71 | * The buffer must be initialized before this call, with sufficient capacity to hold the result. |
| 72 | * Use aws_ed25519_key_pair_get_private_key_size to figure out how much capacity buffer needs for a given format. |
| 73 | */ |
| 74 | AWS_CAL_API int aws_ed25519_key_pair_get_private_key( |
| 75 | const struct aws_ed25519_key_pair *key_pair, |
| 76 | enum aws_ed25519_key_export_format format, |
| 77 | struct aws_byte_buf *out); |
| 78 | |
| 79 | /** |
| 80 | * Gets the size of the exported private key. |
| 81 | */ |
| 82 | AWS_CAL_API size_t aws_ed25519_key_pair_get_private_key_size(enum aws_ed25519_key_export_format format); |
| 83 | |
| 84 | AWS_EXTERN_C_END |
| 85 | |
| 86 | AWS_POP_SANE_WARNING_LEVEL |
| 87 | |
| 88 | #endif /* AWS_CAL_ED25519_H */ |