|
26 | 26 | #include <wolfssl/options.h> |
27 | 27 | #endif |
28 | 28 | #include <wolfssl/wolfcrypt/rsa.h> |
| 29 | +#include <wolfssl/wolfcrypt/hash.h> |
| 30 | +#include <wolfssl/wolfcrypt/error-crypt.h> |
29 | 31 |
|
30 | 32 | #include "com_wolfssl_WolfCryptRSA.h" |
31 | 33 |
|
@@ -214,6 +216,189 @@ JNIEXPORT jint JNICALL Java_com_wolfssl_WolfCryptRSA_doEnc |
214 | 216 | return ret; |
215 | 217 | } |
216 | 218 |
|
| 219 | +JNIEXPORT jint JNICALL Java_com_wolfssl_WolfCryptRSA_doPssSign |
| 220 | + (JNIEnv* jenv, jobject jcl, jobject in, jlong inSz, jobject out, jintArray outSz, jint hash, jint mgf, jobject keyDer, jlong keySz) |
| 221 | +{ |
| 222 | +#ifdef WC_RSA_PSS |
| 223 | + int ret; |
| 224 | + WC_RNG rng; |
| 225 | + RsaKey myKey; |
| 226 | + int rngInit = 0; |
| 227 | + int keyInit = 0; |
| 228 | + unsigned int idx = 0; |
| 229 | + unsigned int tmpOut; |
| 230 | + unsigned char* inBuf = NULL; |
| 231 | + unsigned char* outBuf = NULL; |
| 232 | + unsigned char* keyBuf = NULL; |
| 233 | + enum wc_HashType hashType; |
| 234 | + (void)jcl; |
| 235 | + |
| 236 | + if ((inSz < 0) || (keySz < 0)) { |
| 237 | + return -1; |
| 238 | + } |
| 239 | + |
| 240 | + inBuf = (*jenv)->GetDirectBufferAddress(jenv, in); |
| 241 | + if (inBuf == NULL) { |
| 242 | + printf("problem getting in buffer address\n"); |
| 243 | + return -1; |
| 244 | + } |
| 245 | + |
| 246 | + outBuf = (*jenv)->GetDirectBufferAddress(jenv, out); |
| 247 | + if (outBuf == NULL) { |
| 248 | + printf("problem getting out buffer address\n"); |
| 249 | + return -1; |
| 250 | + } |
| 251 | + |
| 252 | + keyBuf = (*jenv)->GetDirectBufferAddress(jenv, keyDer); |
| 253 | + if (keyBuf == NULL) { |
| 254 | + printf("problem getting key buffer address\n"); |
| 255 | + return -1; |
| 256 | + } |
| 257 | + |
| 258 | + hashType = wc_OidGetHash(hash); |
| 259 | + if (hashType == WC_HASH_TYPE_NONE) { |
| 260 | + printf("doPssSign: unsupported hash OID %d\n", hash); |
| 261 | + return -1; |
| 262 | + } |
| 263 | + |
| 264 | + /* get output buffer size */ |
| 265 | + (*jenv)->GetIntArrayRegion(jenv, outSz, 0, 1, (jint*)&tmpOut); |
| 266 | + |
| 267 | + ret = wc_InitRng(&rng); |
| 268 | + if (ret != 0) { |
| 269 | + printf("wc_InitRng failed, ret = %d\n", ret); |
| 270 | + return ret; |
| 271 | + } |
| 272 | + rngInit = 1; |
| 273 | + |
| 274 | + ret = wc_InitRsaKey(&myKey, NULL); |
| 275 | + if (ret != 0) { |
| 276 | + printf("wc_InitRsaKey failed, ret = %d\n", ret); |
| 277 | + wc_FreeRng(&rng); |
| 278 | + return ret; |
| 279 | + } |
| 280 | + keyInit = 1; |
| 281 | + |
| 282 | + ret = wc_RsaPrivateKeyDecode(keyBuf, &idx, &myKey, (unsigned int)keySz); |
| 283 | + if (ret == 0) { |
| 284 | + |
| 285 | + ret = wc_RsaPSS_Sign(inBuf, (unsigned int)inSz, outBuf, tmpOut, |
| 286 | + hashType, mgf, &myKey, &rng); |
| 287 | + if (ret > 0) { |
| 288 | + tmpOut = ret; |
| 289 | + (*jenv)->SetIntArrayRegion(jenv, outSz, 0, 1, (jint*)&tmpOut); |
| 290 | + ret = 0; |
| 291 | + } |
| 292 | + } else { |
| 293 | + printf("wc_RsaPrivateKeyDecode failed, ret = %d\n", ret); |
| 294 | + } |
| 295 | + |
| 296 | + if (keyInit) { |
| 297 | + wc_FreeRsaKey(&myKey); |
| 298 | + } |
| 299 | + if (rngInit) { |
| 300 | + wc_FreeRng(&rng); |
| 301 | + } |
| 302 | + |
| 303 | + return ret; |
| 304 | +#else |
| 305 | + (void)jenv; |
| 306 | + (void)jcl; |
| 307 | + (void)in; |
| 308 | + (void)inSz; |
| 309 | + (void)out; |
| 310 | + (void)outSz; |
| 311 | + (void)hash; |
| 312 | + (void)mgf; |
| 313 | + (void)keyDer; |
| 314 | + (void)keySz; |
| 315 | + return (jint)NOT_COMPILED_IN; |
| 316 | +#endif /* WC_RSA_PSS */ |
| 317 | +} |
| 318 | + |
| 319 | +JNIEXPORT jint JNICALL Java_com_wolfssl_WolfCryptRSA_doPssVerify |
| 320 | + (JNIEnv* jenv, jobject jcl, jobject sig, jlong sigSz, jobject out, jlong outSz, jint hash, jint mgf, jobject keyDer, jlong keySz) |
| 321 | +{ |
| 322 | +#ifdef WC_RSA_PSS |
| 323 | + int ret; |
| 324 | + RsaKey myKey; |
| 325 | + unsigned int idx = 0; |
| 326 | + unsigned char* sigBuf = NULL; |
| 327 | + unsigned char* outBuf = NULL; |
| 328 | + unsigned char* keyBuf = NULL; |
| 329 | + enum wc_HashType hashType; |
| 330 | + (void)jcl; |
| 331 | + |
| 332 | + if ((sigSz < 0) || (keySz < 0) || (outSz < 0)) { |
| 333 | + return -1; |
| 334 | + } |
| 335 | + |
| 336 | + sigBuf = (*jenv)->GetDirectBufferAddress(jenv, sig); |
| 337 | + if (sigBuf == NULL) { |
| 338 | + printf("problem getting sig buffer address\n"); |
| 339 | + return -1; |
| 340 | + } |
| 341 | + |
| 342 | + outBuf = (*jenv)->GetDirectBufferAddress(jenv, out); |
| 343 | + if (outBuf == NULL) { |
| 344 | + printf("problem getting out buffer address\n"); |
| 345 | + return -1; |
| 346 | + } |
| 347 | + |
| 348 | + keyBuf = (*jenv)->GetDirectBufferAddress(jenv, keyDer); |
| 349 | + if (keyBuf == NULL) { |
| 350 | + printf("problem getting key buffer address\n"); |
| 351 | + return -1; |
| 352 | + } |
| 353 | + |
| 354 | + hashType = wc_OidGetHash(hash); |
| 355 | + if (hashType == WC_HASH_TYPE_NONE) { |
| 356 | + printf("doPssVerify: unsupported hash OID %d\n", hash); |
| 357 | + return -1; |
| 358 | + } |
| 359 | + |
| 360 | + ret = wc_InitRsaKey(&myKey, NULL); |
| 361 | + if (ret != 0) { |
| 362 | + printf("wc_InitRsaKey failed, ret = %d\n", ret); |
| 363 | + return ret; |
| 364 | + } |
| 365 | + |
| 366 | + /* Try private key decode first (sign check receives the server private), |
| 367 | + * fall back to public key decode (verify receives the peer public) */ |
| 368 | + ret = wc_RsaPrivateKeyDecode(keyBuf, &idx, &myKey, (unsigned int)keySz); |
| 369 | + if (ret != 0) { |
| 370 | + idx = 0; |
| 371 | + ret = wc_RsaPublicKeyDecode(keyBuf, &idx, &myKey, (unsigned int)keySz); |
| 372 | + } |
| 373 | + |
| 374 | + if (ret == 0) { |
| 375 | + ret = wc_RsaPSS_Verify(sigBuf, (unsigned int)sigSz, outBuf, |
| 376 | + (unsigned int)outSz, hashType, mgf, &myKey); |
| 377 | + if (ret < 0) { |
| 378 | + printf("wc_RsaPSS_Verify failed, ret = %d\n", ret); |
| 379 | + } |
| 380 | + } else { |
| 381 | + printf("RSA key decode failed, ret = %d\n", ret); |
| 382 | + } |
| 383 | + |
| 384 | + wc_FreeRsaKey(&myKey); |
| 385 | + |
| 386 | + return ret; |
| 387 | +#else |
| 388 | + (void)jenv; |
| 389 | + (void)jcl; |
| 390 | + (void)sig; |
| 391 | + (void)sigSz; |
| 392 | + (void)out; |
| 393 | + (void)outSz; |
| 394 | + (void)hash; |
| 395 | + (void)mgf; |
| 396 | + (void)keyDer; |
| 397 | + (void)keySz; |
| 398 | + return (jint)NOT_COMPILED_IN; |
| 399 | +#endif /* WC_RSA_PSS */ |
| 400 | +} |
| 401 | + |
217 | 402 | JNIEXPORT jint JNICALL Java_com_wolfssl_WolfCryptRSA_doDec |
218 | 403 | (JNIEnv* jenv, jobject jcl, jobject in, jlong inSz, jobject out, |
219 | 404 | jlong outSz, jobject keyDer, jlong keySz) |
|
0 commit comments