aboutsummaryrefslogtreecommitdiff
path: root/nearby/connections/ukey2/ukey2_jni/java/src/main/java/com/google/security/cryptauth/lib/securegcm/ukey2/D2DConnectionContextV1.java
blob: 9ce25176e51e53b4a6c1b09cf3bcb94e91f23984 (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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/*
 * Copyright 2023 Google LLC
 *
 * 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
 *
 *     http://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.
 */

package com.google.security.cryptauth.lib.securegcm.ukey2;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;

public class D2DConnectionContextV1 {

  static {
    var path = System.getProperty("java.library.path");

    if (path == null) {
      throw new RuntimeException("Path isn't set.");
    }

    var paths = java.util.List.of(path.split(";"));
    paths.forEach(System.out::println);

    System.loadLibrary("ukey2_jni");
  }

  private static native byte[] encode_message_to_peer(
      long contextPtr, byte[] payload, byte[] associatedData) throws BadHandleException;

  private static native byte[] decode_message_from_peer(
      long contextPtr, byte[] message, byte[] associatedData) throws CryptoException;

  private static native byte[] get_session_unique(long contextPtr) throws BadHandleException;

  private static native int get_sequence_number_for_encoding(long contextPtr)
      throws BadHandleException;

  private static native int get_sequence_number_for_decoding(long contextPtr)
      throws BadHandleException;

  private static native byte[] save_session(long contextPtr) throws BadHandleException;

  private static native long from_saved_session(byte[] savedSessionInfo);

  private final long contextPtr;

  /**
   * Java wrapper for D2DConnectionContextV1 to interact with the underlying Rust implementation
   *
   * @param contextPtr the handle to the Rust implementation.
   */
  D2DConnectionContextV1(@Nonnull long contextPtr) {
    this.contextPtr = contextPtr;
  }

  /**
   * Encode a message to the connection peer using session keys derived from the handshake.
   *
   * @param payload The message to be encrypted.
   * @return The encrypted/encoded message.
   */
  @Nonnull
  public byte[] encodeMessageToPeer(@Nonnull byte[] payload, @Nullable byte[] associatedData)
      throws BadHandleException {
    return encode_message_to_peer(contextPtr, payload, associatedData);
  }

  /**
   * Decodes/decrypts a message from the connection peer.
   *
   * @param message The message received over the connection.
   * @return The decoded message from the connection peer.
   */
  @Nonnull
  public byte[] decodeMessageFromPeer(@Nonnull byte[] message, @Nullable byte[] associatedData)
      throws CryptoException {
    return decode_message_from_peer(contextPtr, message, associatedData);
  }

  /**
   * A unique session identifier derived from session-specific information
   *
   * @return The session unique identifier
   */
  @Nonnull
  public byte[] getSessionUnique() throws BadHandleException {
    return get_session_unique(contextPtr);
  }

  /**
   * Returns the encoding sequence number.
   *
   * @return the encoding sequence number.
   */
  public int getSequenceNumberForEncoding() throws BadHandleException {
    return get_sequence_number_for_encoding(contextPtr);
  }

  /**
   * Returns the decoding sequence number.
   *
   * @return the decoding sequence number.
   */
  public int getSequenceNumberForDecoding() throws BadHandleException {
    return get_sequence_number_for_decoding(contextPtr);
  }

  /**
   * Serializes the current session in a form usable by {@link
   * D2DConnectionContextV1#fromSavedSession}
   *
   * @return a byte array representing the current session.
   */
  @Nonnull
  public byte[] saveSession() throws BadHandleException {
    return save_session(contextPtr);
  }

  /**
   * Reconstructs and returns the session originally serialized by {@link
   * D2DConnectionContextV1#saveSession}
   *
   * @param savedSessionInfo the byte array from saveSession()
   * @return a D2DConnectionContextV1 session with the same properties as the context saved.
   */
  public static D2DConnectionContextV1 fromSavedSession(@Nonnull byte[] savedSessionInfo) {
    return new D2DConnectionContextV1(from_saved_session(savedSessionInfo));
  }
}