• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Copyright (c) 2017, Google Inc.
2  *
3  * Permission to use, copy, modify, and/or distribute this software for any
4  * purpose with or without fee is hereby granted, provided that the above
5  * copyright notice and this permission notice appear in all copies.
6  *
7  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
8  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
9  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
10  * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
11  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
12  * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
13  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
14 
15 #include <openssl/evp.h>
16 
17 #include <openssl/curve25519.h>
18 #include <openssl/err.h>
19 
20 #include "internal.h"
21 
22 
23 /* Ed25519 has no parameters to copy. */
pkey_ed25519_copy(EVP_PKEY_CTX * dst,EVP_PKEY_CTX * src)24 static int pkey_ed25519_copy(EVP_PKEY_CTX *dst, EVP_PKEY_CTX *src) { return 1; }
25 
pkey_ed25519_sign_message(EVP_PKEY_CTX * ctx,uint8_t * sig,size_t * siglen,const uint8_t * tbs,size_t tbslen)26 static int pkey_ed25519_sign_message(EVP_PKEY_CTX *ctx, uint8_t *sig,
27                                      size_t *siglen, const uint8_t *tbs,
28                                      size_t tbslen) {
29   ED25519_KEY *key = ctx->pkey->pkey.ptr;
30   if (!key->has_private) {
31     OPENSSL_PUT_ERROR(EVP, EVP_R_NOT_A_PRIVATE_KEY);
32     return 0;
33   }
34 
35   *siglen = 64;
36   if (sig == NULL) {
37     return 1;
38   }
39 
40   return ED25519_sign(sig, tbs, tbslen, key->key.priv);
41 }
42 
pkey_ed25519_verify_message(EVP_PKEY_CTX * ctx,const uint8_t * sig,size_t siglen,const uint8_t * tbs,size_t tbslen)43 static int pkey_ed25519_verify_message(EVP_PKEY_CTX *ctx, const uint8_t *sig,
44                                        size_t siglen, const uint8_t *tbs,
45                                        size_t tbslen) {
46   ED25519_KEY *key = ctx->pkey->pkey.ptr;
47   if (siglen != 64 ||
48       !ED25519_verify(tbs, tbslen, sig, key->key.pub.value)) {
49     OPENSSL_PUT_ERROR(EVP, EVP_R_INVALID_SIGNATURE);
50     return 0;
51   }
52 
53   return 1;
54 }
55 
56 const EVP_PKEY_METHOD ed25519_pkey_meth = {
57     EVP_PKEY_ED25519,
58     NULL /* init */,
59     pkey_ed25519_copy,
60     NULL /* cleanup */,
61     NULL /* keygen */,
62     NULL /* sign */,
63     pkey_ed25519_sign_message,
64     NULL /* verify */,
65     pkey_ed25519_verify_message,
66     NULL /* verify_recover */,
67     NULL /* encrypt */,
68     NULL /* decrypt */,
69     NULL /* derive */,
70     NULL /* ctrl */,
71 };
72