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        if (!(o instanceof OpenSSLECPointContext)) {
34            return false;
35        }
36
37        final OpenSSLECPointContext other = (OpenSSLECPointContext) o;
38        if (!NativeCrypto.EC_GROUP_cmp(group.getNativeRef(), other.group.getNativeRef())) {
39            return false;
40        }
41
42        return NativeCrypto.EC_POINT_cmp(group.getNativeRef(), pointCtx, other.pointCtx);
43    }
44
45    public ECPoint getECPoint() {
46        final byte[][] generatorCoords = NativeCrypto.EC_POINT_get_affine_coordinates(
47                group.getNativeRef(), pointCtx);
48        final BigInteger x = new BigInteger(generatorCoords[0]);
49        final BigInteger y = new BigInteger(generatorCoords[1]);
50        return new ECPoint(x, y);
51    }
52
53    @Override
54    public int hashCode() {
55        // TODO Auto-generated method stub
56        return super.hashCode();
57    }
58
59    public NativeRef.EC_POINT getNativeRef() {
60        return pointCtx;
61    }
62
63    public static OpenSSLECPointContext getInstance(int curveType, OpenSSLECGroupContext group,
64            ECPoint javaPoint) {
65        OpenSSLECPointContext point = new OpenSSLECPointContext(group, new NativeRef.EC_POINT(
66                NativeCrypto.EC_POINT_new(group.getNativeRef())));
67        NativeCrypto.EC_POINT_set_affine_coordinates(group.getNativeRef(),
68                point.getNativeRef(), javaPoint.getAffineX().toByteArray(),
69                javaPoint.getAffineY().toByteArray());
70        return point;
71    }
72}
73