|
6 | 6 |
|
7 | 7 | #include <string.h> |
8 | 8 |
|
| 9 | +static jobject ImportSubjectPublicKeyInfo(JNIEnv* env, const uint8_t* buffer, int32_t bufferLength); |
9 | 10 | static int32_t ExportEncodedKey(jobject key, uint8_t* buffer, int32_t bufferLength, int32_t* bytesWritten); |
10 | 11 |
|
11 | 12 | int32_t AndroidCryptoNative_X25519IsSupported(void) |
@@ -129,34 +130,9 @@ jobject AndroidCryptoNative_X25519ImportSubjectPublicKeyInfo(const uint8_t* buff |
129 | 130 | abort_if_negative_integer_argument(bufferLength); |
130 | 131 |
|
131 | 132 | JNIEnv* env = GetJNIEnv(); |
132 | | - jobject ret = NULL; |
133 | | - |
134 | | - INIT_LOCALS(loc, algorithmName, keyFactory, spkiBytes, keySpec, publicKey); |
135 | | - |
136 | | - loc[algorithmName] = make_java_string(env, "XDH"); |
137 | | - loc[keyFactory] = (*env)->CallStaticObjectMethod(env, g_KeyFactoryClass, g_KeyFactoryGetInstanceMethod, loc[algorithmName]); |
138 | | - ON_EXCEPTION_PRINT_AND_GOTO(cleanup); |
139 | | - |
140 | | - loc[spkiBytes] = make_java_byte_array(env, bufferLength); |
141 | | - (*env)->SetByteArrayRegion(env, loc[spkiBytes], 0, bufferLength, (const jbyte*)buffer); |
142 | | - ON_EXCEPTION_PRINT_AND_GOTO(cleanup); |
143 | | - |
144 | | - loc[keySpec] = (*env)->NewObject(env, g_X509EncodedKeySpecClass, g_X509EncodedKeySpecCtor, loc[spkiBytes]); |
145 | | - ON_EXCEPTION_PRINT_AND_GOTO(cleanup); |
146 | | - |
147 | | - loc[publicKey] = (*env)->CallObjectMethod(env, loc[keyFactory], g_KeyFactoryGenPublicMethod, loc[keySpec]); |
148 | | - ON_EXCEPTION_PRINT_AND_GOTO(cleanup); |
149 | | - |
150 | | - if (loc[publicKey] == NULL) |
151 | | - { |
152 | | - goto cleanup; |
153 | | - } |
154 | | - |
155 | | - ret = ToGRef(env, loc[publicKey]); |
156 | | - loc[publicKey] = NULL; |
| 133 | + jobject publicKey = ImportSubjectPublicKeyInfo(env, buffer, bufferLength); |
| 134 | + jobject ret = ToGRef(env, publicKey); |
157 | 135 |
|
158 | | -cleanup: |
159 | | - RELEASE_LOCALS(loc, env); |
160 | 136 | return ret; |
161 | 137 | } |
162 | 138 |
|
@@ -259,6 +235,38 @@ int32_t AndroidCryptoNative_X25519DeriveSecret( |
259 | 235 | return ret; |
260 | 236 | } |
261 | 237 |
|
| 238 | +int32_t AndroidCryptoNative_X25519DeriveSecretWithSubjectPublicKeyInfo( |
| 239 | + jobject privateKey, |
| 240 | + const uint8_t* buffer, |
| 241 | + int32_t bufferLength, |
| 242 | + uint8_t* destination, |
| 243 | + int32_t destinationLength) |
| 244 | +{ |
| 245 | + abort_if_invalid_pointer_argument(privateKey); |
| 246 | + abort_if_invalid_pointer_argument(buffer); |
| 247 | + abort_if_invalid_pointer_argument(destination); |
| 248 | + abort_if_negative_integer_argument(bufferLength); |
| 249 | + abort_if_negative_integer_argument(destinationLength); |
| 250 | + |
| 251 | + JNIEnv* env = GetJNIEnv(); |
| 252 | + int32_t ret = FAIL; |
| 253 | + |
| 254 | + INIT_LOCALS(loc, publicKey); |
| 255 | + |
| 256 | + loc[publicKey] = ImportSubjectPublicKeyInfo(env, buffer, bufferLength); |
| 257 | + |
| 258 | + if (loc[publicKey] == NULL) |
| 259 | + { |
| 260 | + goto cleanup; |
| 261 | + } |
| 262 | + |
| 263 | + ret = AndroidCryptoNative_X25519DeriveSecret(privateKey, loc[publicKey], destination, destinationLength); |
| 264 | + |
| 265 | +cleanup: |
| 266 | + RELEASE_LOCALS(loc, env); |
| 267 | + return ret; |
| 268 | +} |
| 269 | + |
262 | 270 | static int32_t ExportEncodedKey(jobject key, uint8_t* buffer, int32_t bufferLength, int32_t* bytesWritten) |
263 | 271 | { |
264 | 272 | abort_if_invalid_pointer_argument(key); |
@@ -301,3 +309,36 @@ static int32_t ExportEncodedKey(jobject key, uint8_t* buffer, int32_t bufferLeng |
301 | 309 | RELEASE_LOCALS(loc, env); |
302 | 310 | return ret; |
303 | 311 | } |
| 312 | + |
| 313 | +static jobject ImportSubjectPublicKeyInfo(JNIEnv* env, const uint8_t* buffer, int32_t bufferLength) |
| 314 | +{ |
| 315 | + jobject ret = NULL; |
| 316 | + |
| 317 | + INIT_LOCALS(loc, algorithmName, keyFactory, spkiBytes, keySpec, publicKey); |
| 318 | + |
| 319 | + loc[algorithmName] = make_java_string(env, "XDH"); |
| 320 | + loc[keyFactory] = (*env)->CallStaticObjectMethod(env, g_KeyFactoryClass, g_KeyFactoryGetInstanceMethod, loc[algorithmName]); |
| 321 | + ON_EXCEPTION_PRINT_AND_GOTO(cleanup); |
| 322 | + |
| 323 | + loc[spkiBytes] = make_java_byte_array(env, bufferLength); |
| 324 | + (*env)->SetByteArrayRegion(env, loc[spkiBytes], 0, bufferLength, (const jbyte*)buffer); |
| 325 | + ON_EXCEPTION_PRINT_AND_GOTO(cleanup); |
| 326 | + |
| 327 | + loc[keySpec] = (*env)->NewObject(env, g_X509EncodedKeySpecClass, g_X509EncodedKeySpecCtor, loc[spkiBytes]); |
| 328 | + ON_EXCEPTION_PRINT_AND_GOTO(cleanup); |
| 329 | + |
| 330 | + loc[publicKey] = (*env)->CallObjectMethod(env, loc[keyFactory], g_KeyFactoryGenPublicMethod, loc[keySpec]); |
| 331 | + ON_EXCEPTION_PRINT_AND_GOTO(cleanup); |
| 332 | + |
| 333 | + if (loc[publicKey] == NULL) |
| 334 | + { |
| 335 | + goto cleanup; |
| 336 | + } |
| 337 | + |
| 338 | + ret = loc[publicKey]; |
| 339 | + loc[publicKey] = NULL; |
| 340 | + |
| 341 | +cleanup: |
| 342 | + RELEASE_LOCALS(loc, env); |
| 343 | + return ret; |
| 344 | +} |
0 commit comments