Debian Codegraph @

debian/rust-vendor/aws-lc-sys/aws-lc/crypto/fipsmodule/ecdh/ecdh.c

raw
1 // Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
2 //
3 // The Elliptic Curve Public-Key Crypto Library (ECC Code) included
4 // herein is developed by SUN MICROSYSTEMS, INC., and is contributed
5 // to the OpenSSL project.
6 //
7 // The ECDH software is originally written by Douglas Stebila of
8 // Sun Microsystems Laboratories.
9 //
10 // Copyright (c) 2000-2002 The OpenSSL Project. All rights reserved.
11 // SPDX-License-Identifier: Apache-2.0
12
13 #include <openssl/ecdh.h>
14
15 #include <string.h>
16
17 #include <openssl/ec.h>
18 #include <openssl/ec_key.h>
19 #include <openssl/err.h>
20 #include <openssl/mem.h>
21 #include <openssl/sha.h>
22
23 #include "../../internal.h"
24 #include "../ec/internal.h"
25 #include "../service_indicator/internal.h"
26
27
28 int ECDH_compute_shared_secret(uint8_t *buf, size_t *buflen, const EC_POINT *pub_key,
29 const EC_KEY *priv_key) {
30 boringssl_ensure_ecc_self_test();
31 if (priv_key->priv_key == NULL) {
32 OPENSSL_PUT_ERROR(ECDH, ECDH_R_NO_PRIVATE_VALUE);
33 return 0;
34 }
35 const EC_SCALAR *const priv = &priv_key->priv_key->scalar;
36 const EC_GROUP *const group = EC_KEY_get0_group(priv_key);
37 if (EC_GROUP_cmp(group, pub_key->group, NULL) != 0) {
38 OPENSSL_PUT_ERROR(EC, EC_R_INCOMPATIBLE_OBJECTS);
39 return 0;
40 }
41
42 // Lock state here to avoid the underlying |EC_KEY_check_fips| function
43 // updating the service indicator state unintentionally.
44 FIPS_service_indicator_lock_state();
45 int ret = 0;
46
47 // |EC_KEY_check_fips| is not an expensive operation on an external
48 // public key so we always perform it, even in the non-FIPS build.
49 EC_KEY *key_pub_key = NULL;
50 key_pub_key = EC_KEY_new();
51 if (key_pub_key == NULL) {
52 goto end;
53 }
54
55 if (!EC_KEY_set_group(key_pub_key, group) ||
56 // Creates a copy of pub_key within key_pub_key
57 !EC_KEY_set_public_key(key_pub_key, pub_key) ||
58 !EC_KEY_check_fips(key_pub_key)) {
59 OPENSSL_PUT_ERROR(EC, EC_R_PUBLIC_KEY_VALIDATION_FAILED);
60 goto end;
61 }
62
63 EC_JACOBIAN shared_point;
64 if (!ec_point_mul_scalar(group, &shared_point, &pub_key->raw, priv) ||
65 !ec_get_x_coordinate_as_bytes(group, buf, buflen, *buflen,
66 &shared_point)) {
67 OPENSSL_PUT_ERROR(ECDH, ECDH_R_POINT_ARITHMETIC_FAILURE);
68 goto end;
69 }
70
71 ret = 1;
72 end:
73 OPENSSL_cleanse(&shared_point, sizeof(EC_JACOBIAN));
74 FIPS_service_indicator_unlock_state();
75 if (key_pub_key != NULL) {
76 EC_KEY_free(key_pub_key);
77 }
78 return ret;
79 }
80
81 int ECDH_compute_key_fips(uint8_t *out, size_t out_len, const EC_POINT *pub_key,
82 const EC_KEY *priv_key) {
83 // Lock state here to avoid underlying |SHA*| functions updating the service
84 // indicator state unintentionally.
85 FIPS_service_indicator_lock_state();
86
87 uint8_t buf[EC_MAX_BYTES];
88 size_t buflen = sizeof(buf);
89 int ret = 0;
90
91 if (!ECDH_compute_shared_secret(buf, &buflen, pub_key, priv_key)) {
92 goto end;
93 }
94
95 switch (out_len) {
96 case SHA224_DIGEST_LENGTH:
97 SHA224(buf, buflen, out);
98 break;
99 case SHA256_DIGEST_LENGTH:
100 SHA256(buf, buflen, out);
101 break;
102 case SHA384_DIGEST_LENGTH:
103 SHA384(buf, buflen, out);
104 break;
105 case SHA512_DIGEST_LENGTH:
106 SHA512(buf, buflen, out);
107 break;
108 default:
109 OPENSSL_PUT_ERROR(ECDH, ECDH_R_UNKNOWN_DIGEST_LENGTH);
110 goto end;
111 }
112 ret = 1;
113
114 end:
115 OPENSSL_cleanse(buf, sizeof(buf));
116 FIPS_service_indicator_unlock_state();
117 if(ret) {
118 ECDH_verify_service_indicator(priv_key);
119 }
120 return ret;
121 }