aboutsummaryrefslogtreecommitdiff
path: root/util/ec_key_util.cc
blob: 7147448916893a6bf478e9b4201a62c9a3f3627e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
/*
 * Copyright 2019 Google Inc.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "util/ec_key_util.h"

#include "absl/strings/str_cat.h"
#include "crypto/ec_group.h"
#include "util/proto_util.h"
#include "util/recordio.h"

namespace private_join_and_compute::ec_key_util {

Status GenerateEcKey(int curve_id, absl::string_view ec_key_filename) {
  Context context;
  ASSIGN_OR_RETURN(ECGroup ec_group, ECGroup::Create(curve_id, &context));
  BigNum key = ec_group.GeneratePrivateKey();
  EcKeyProto key_proto;
  key_proto.set_curve_id(curve_id);
  key_proto.set_key(key.ToBytes());
  return ProtoUtils::WriteProtoToFile(key_proto, ec_key_filename);
}

StatusOr<BigNum> DeserializeEcKey(Context* context, int curve_id,
                                  EcKeyProto ec_key_proto) {
  if (curve_id != ec_key_proto.curve_id()) {
    return InvalidArgumentError(absl::StrCat(
        "EC key conversion failed, the given curve_id ", curve_id,
        " doesn't match the proto curve id ", ec_key_proto.curve_id()));
  }
  return context->CreateBigNum(ec_key_proto.key());
}

}  // namespace private_join_and_compute::ec_key_util