3e494bf0921cf1bfacd06d0317409d2faa9e7b12
[openssl.git] / include / crypto / ecx.h
1 /*
2  * Copyright 2020 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the Apache License 2.0 (the "License").  You may not use
5  * this file except in compliance with the License.  You can obtain a copy
6  * in the file LICENSE in the source distribution or at
7  * https://www.openssl.org/source/license.html
8  */
9
10 /* Internal EC functions for other submodules: not for application use */
11
12 #ifndef OSSL_CRYPTO_ECX_H
13 # define OSSL_CRYPTO_ECX_H
14 # include <openssl/opensslconf.h>
15
16 # ifndef OPENSSL_NO_EC
17
18 #  include <openssl/e_os2.h>
19 #  include <openssl/crypto.h>
20 #  include "internal/refcount.h"
21
22 #  define X25519_KEYLEN         32
23 #  define X448_KEYLEN           56
24 #  define ED25519_KEYLEN        32
25 #  define ED448_KEYLEN          57
26
27 #  define MAX_KEYLEN  ED448_KEYLEN
28
29 #  define X25519_BITS           253
30 #  define X25519_SECURITY_BITS  128
31
32 #  define X448_BITS             448
33 #  define X448_SECURITY_BITS    224
34
35 #  define ED25519_BITS          256
36 /* RFC8032 Section 8.5 */
37 #  define ED25519_SECURITY_BITS 128
38 #  define ED25519_SIGSIZE       64
39
40 #  define ED448_BITS            456
41 /* RFC8032 Section 8.5 */
42 #  define ED448_SECURITY_BITS   224
43 #  define ED448_SIGSIZE         114
44
45 struct ecx_key_st {
46     unsigned int haspubkey:1;
47     unsigned char pubkey[MAX_KEYLEN];
48     unsigned char *privkey;
49     size_t keylen;
50     CRYPTO_REF_COUNT references;
51     CRYPTO_RWLOCK *lock;
52 };
53
54 typedef struct ecx_key_st ECX_KEY;
55
56 ECX_KEY *ecx_key_new(size_t keylen, int haspubkey);
57 unsigned char *ecx_key_allocate_privkey(ECX_KEY *key);
58 void ecx_key_free(ECX_KEY *key);
59 int ecx_key_up_ref(ECX_KEY *key);
60
61 int X25519(uint8_t out_shared_key[32], const uint8_t private_key[32],
62            const uint8_t peer_public_value[32]);
63 void X25519_public_from_private(uint8_t out_public_value[32],
64                                 const uint8_t private_key[32]);
65
66 int ED25519_sign(uint8_t *out_sig, const uint8_t *message, size_t message_len,
67                  const uint8_t public_key[32], const uint8_t private_key[32]);
68 int ED25519_verify(const uint8_t *message, size_t message_len,
69                    const uint8_t signature[64], const uint8_t public_key[32]);
70
71 int ED448_sign(OPENSSL_CTX *ctx, uint8_t *out_sig, const uint8_t *message,
72                size_t message_len, const uint8_t public_key[57],
73                const uint8_t private_key[57], const uint8_t *context,
74                size_t context_len);
75
76 int ED448_verify(OPENSSL_CTX *ctx, const uint8_t *message, size_t message_len,
77                  const uint8_t signature[114], const uint8_t public_key[57],
78                  const uint8_t *context, size_t context_len);
79
80 int X448(uint8_t out_shared_key[56], const uint8_t private_key[56],
81          const uint8_t peer_public_value[56]);
82 void X448_public_from_private(uint8_t out_public_value[56],
83                               const uint8_t private_key[56]);
84
85 int s390x_x25519_mul(unsigned char u_dst[32],
86                      const unsigned char u_src[32],
87                      const unsigned char d_src[32]);
88 int s390x_x448_mul(unsigned char u_dst[56],
89                    const unsigned char u_src[56],
90                    const unsigned char d_src[56]);
91
92 # endif /* OPENSSL_NO_EC */
93 #endif