|
| 1 | +/** |
| 2 | + * Copyright (C) 2013 Open Whisper Systems |
| 3 | + * |
| 4 | + * This program is free software: you can redistribute it and/or modify |
| 5 | + * it under the terms of the GNU General Public License as published by |
| 6 | + * the Free Software Foundation, either version 3 of the License, or |
| 7 | + * (at your option) any later version. |
| 8 | + * |
| 9 | + * This program is distributed in the hope that it will be useful, |
| 10 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | + * GNU General Public License for more details. |
| 13 | + * |
| 14 | + * You should have received a copy of the GNU General Public License |
| 15 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 16 | + */ |
| 17 | + |
| 18 | +#include <string.h> |
| 19 | +#include <stdint.h> |
| 20 | + |
| 21 | +#include <jni.h> |
| 22 | +#include "curve25519-donna.h" |
| 23 | + |
| 24 | +JNIEXPORT jbyteArray JNICALL Java_org_whispersystems_textsecure_crypto_ecc_Curve25519_generatePrivateKey |
| 25 | + (JNIEnv *env, jclass clazz, jbyteArray random) |
| 26 | +{ |
| 27 | + uint8_t* privateKey = (uint8_t*)(*env)->GetByteArrayElements(env, random, 0); |
| 28 | + |
| 29 | + privateKey[0] &= 248; |
| 30 | + privateKey[31] &= 127; |
| 31 | + privateKey[31] |= 64; |
| 32 | + |
| 33 | + (*env)->ReleaseByteArrayElements(env, random, privateKey, 0); |
| 34 | + |
| 35 | + return random; |
| 36 | +} |
| 37 | + |
| 38 | +JNIEXPORT jbyteArray JNICALL Java_org_whispersystems_textsecure_crypto_ecc_Curve25519_generatePublicKey |
| 39 | + (JNIEnv *env, jclass clazz, jbyteArray privateKey) |
| 40 | +{ |
| 41 | + static const uint8_t basepoint[32] = {9}; |
| 42 | + |
| 43 | + jbyteArray publicKey = (*env)->NewByteArray(env, 32); |
| 44 | + uint8_t* publicKeyBytes = (uint8_t*)(*env)->GetByteArrayElements(env, publicKey, 0); |
| 45 | + uint8_t* privateKeyBytes = (uint8_t*)(*env)->GetByteArrayElements(env, privateKey, 0); |
| 46 | + |
| 47 | + curve25519_donna(publicKeyBytes, privateKeyBytes, basepoint); |
| 48 | + |
| 49 | + (*env)->ReleaseByteArrayElements(env, publicKey, publicKeyBytes, 0); |
| 50 | + (*env)->ReleaseByteArrayElements(env, privateKey, privateKeyBytes, 0); |
| 51 | + |
| 52 | + return publicKey; |
| 53 | +} |
| 54 | + |
| 55 | +JNIEXPORT jbyteArray JNICALL Java_org_whispersystems_textsecure_crypto_ecc_Curve25519_calculateAgreement |
| 56 | + (JNIEnv *env, jclass clazz, jbyteArray privateKey, jbyteArray publicKey) |
| 57 | +{ |
| 58 | + jbyteArray sharedKey = (*env)->NewByteArray(env, 32); |
| 59 | + uint8_t* sharedKeyBytes = (uint8_t*)(*env)->GetByteArrayElements(env, sharedKey, 0); |
| 60 | + uint8_t* privateKeyBytes = (uint8_t*)(*env)->GetByteArrayElements(env, privateKey, 0); |
| 61 | + uint8_t* publicKeyBytes = (uint8_t*)(*env)->GetByteArrayElements(env, publicKey, 0); |
| 62 | + |
| 63 | + curve25519_donna(sharedKeyBytes, privateKeyBytes, publicKeyBytes); |
| 64 | + |
| 65 | + (*env)->ReleaseByteArrayElements(env, sharedKey, sharedKeyBytes, 0); |
| 66 | + (*env)->ReleaseByteArrayElements(env, publicKey, publicKeyBytes, 0); |
| 67 | + (*env)->ReleaseByteArrayElements(env, privateKey, privateKeyBytes, 0); |
| 68 | + |
| 69 | + return sharedKey; |
| 70 | +} |
0 commit comments