1/*
2 * Copyright (C) 2012 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package org.conscrypt;
18
19import java.math.BigInteger;
20import java.security.spec.ECPoint;
21
22final class OpenSSLECPointContext {
23    private final OpenSSLECGroupContext group;
24    private final NativeRef.EC_POINT pointCtx;
25
26    OpenSSLECPointContext(OpenSSLECGroupContext group, NativeRef.EC_POINT pointCtx) {
27        this.group = group;
28        this.pointCtx = pointCtx;
29    }
30
31    @Override
32    public boolean equals(Object o) {
33        throw new IllegalArgumentException("OpenSSLECPointContext.equals is not defined.");
34    }
35
36    public ECPoint getECPoint() {
37        final byte[][] generatorCoords = NativeCrypto.EC_POINT_get_affine_coordinates(
38                group.getNativeRef(), pointCtx);
39        final BigInteger x = new BigInteger(generatorCoords[0]);
40        final BigInteger y = new BigInteger(generatorCoords[1]);
41        return new ECPoint(x, y);
42    }
43
44    @Override
45    public int hashCode() {
46        // TODO Auto-generated method stub
47        return super.hashCode();
48    }
49
50    public NativeRef.EC_POINT getNativeRef() {
51        return pointCtx;
52    }
53
54    public static OpenSSLECPointContext getInstance(OpenSSLECGroupContext group,
55            ECPoint javaPoint) {
56        OpenSSLECPointContext point = new OpenSSLECPointContext(group, new NativeRef.EC_POINT(
57                NativeCrypto.EC_POINT_new(group.getNativeRef())));
58        NativeCrypto.EC_POINT_set_affine_coordinates(group.getNativeRef(),
59                point.getNativeRef(), javaPoint.getAffineX().toByteArray(),
60                javaPoint.getAffineY().toByteArray());
61        return point;
62    }
63}
64