Debian Codegraph @

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

raw
1 // Copyright (c) 2018, Google Inc.
2 // SPDX-License-Identifier: ISC
3
4 #include <openssl/ec.h>
5 #include <openssl/err.h>
6 #include <openssl/mem.h>
7
8 #include <assert.h>
9
10 #include "internal.h"
11 #include "../bn/internal.h"
12 #include "../../internal.h"
13
14
15 const EC_FELEM *ec_felem_one(const EC_GROUP *group) {
16 // We reuse generator.Z as a cache for 1 in the field.
17 return &group->generator.raw.Z;
18 }
19
20 int ec_bignum_to_felem(const EC_GROUP *group, EC_FELEM *out, const BIGNUM *in) {
21 uint8_t bytes[EC_MAX_BYTES];
22 size_t len = BN_num_bytes(&group->field.N);
23 assert(sizeof(bytes) >= len);
24 if (BN_is_negative(in) || BN_cmp(in, &group->field.N) >= 0 ||
25 !BN_bn2bin_padded(bytes, len, in)) {
26 OPENSSL_PUT_ERROR(EC, EC_R_COORDINATES_OUT_OF_RANGE);
27 return 0;
28 }
29
30 return ec_felem_from_bytes(group, out, bytes, len);
31 }
32
33 int ec_felem_to_bignum(const EC_GROUP *group, BIGNUM *out, const EC_FELEM *in) {
34 uint8_t bytes[EC_MAX_BYTES];
35 size_t len;
36 ec_felem_to_bytes(group, bytes, &len, in);
37 return BN_bin2bn(bytes, len, out) != NULL;
38 }
39
40 void ec_felem_to_bytes(const EC_GROUP *group, uint8_t *out, size_t *out_len,
41 const EC_FELEM *in) {
42 group->meth->felem_to_bytes(group, out, out_len, in);
43 }
44
45 int ec_felem_from_bytes(const EC_GROUP *group, EC_FELEM *out, const uint8_t *in,
46 size_t len) {
47 return group->meth->felem_from_bytes(group, out, in, len);
48 }
49
50 void ec_felem_neg(const EC_GROUP *group, EC_FELEM *out, const EC_FELEM *a) {
51 // -a is zero if a is zero and p-a otherwise.
52 BN_ULONG mask = ec_felem_non_zero_mask(group, a);
53 BN_ULONG borrow = bn_sub_words(out->words, group->field.N.d, a->words,
54 group->field.N.width);
55 assert(borrow == 0);
56 (void)borrow;
57 for (int i = 0; i < group->field.N.width; i++) {
58 out->words[i] &= mask;
59 }
60 }
61
62 void ec_felem_add(const EC_GROUP *group, EC_FELEM *out, const EC_FELEM *a,
63 const EC_FELEM *b) {
64 EC_FELEM tmp;
65 bn_mod_add_words(out->words, a->words, b->words, group->field.N.d, tmp.words,
66 group->field.N.width);
67 }
68
69 void ec_felem_sub(const EC_GROUP *group, EC_FELEM *out, const EC_FELEM *a,
70 const EC_FELEM *b) {
71 EC_FELEM tmp;
72 bn_mod_sub_words(out->words, a->words, b->words, group->field.N.d, tmp.words,
73 group->field.N.width);
74 }
75
76 BN_ULONG ec_felem_non_zero_mask(const EC_GROUP *group, const EC_FELEM *a) {
77 BN_ULONG mask = 0;
78 for (int i = 0; i < group->field.N.width; i++) {
79 mask |= a->words[i];
80 }
81 return ~constant_time_is_zero_w(mask);
82 }
83
84 void ec_felem_select(const EC_GROUP *group, EC_FELEM *out, BN_ULONG mask,
85 const EC_FELEM *a, const EC_FELEM *b) {
86 bn_select_words(out->words, mask, a->words, b->words, group->field.N.width);
87 }
88
89 int ec_felem_equal(const EC_GROUP *group, const EC_FELEM *a,
90 const EC_FELEM *b) {
91 return CRYPTO_memcmp(a->words, b->words,
92 group->field.N.width * sizeof(BN_ULONG)) == 0;
93 }