diff --git a/MTProtoKit/MTEncryption.m b/MTProtoKit/MTEncryption.m index eeafaed55b..f63e611dd1 100644 --- a/MTProtoKit/MTEncryption.m +++ b/MTProtoKit/MTEncryption.m @@ -20,6 +20,11 @@ #import "MTAes.h" #import "MTRsa.h" +#if TARGET_OS_IOS +#else +# include +#endif + NSData *MTSha1(NSData *data) { uint8_t digest[20]; @@ -236,12 +241,39 @@ NSData *MTAesDecrypt(NSData *data, NSData *key, NSData *iv) NSData *MTRsaEncrypt(NSString *publicKey, NSData *data) { +#if TARGET_OS_IOS NSMutableData *updatedData = [[NSMutableData alloc] initWithData:data]; while (updatedData.length < 256) { uint8_t zero = 0; [updatedData replaceBytesInRange:NSMakeRange(0, 0) withBytes:&zero length:1]; } return [MTRsa encryptData:updatedData publicKey:publicKey]; +#else + BIO *keyBio = BIO_new(BIO_s_mem()); + const char *keyData = [publicKey UTF8String]; + BIO_write(keyBio, keyData, (int)publicKey.length); + RSA *rsaKey = PEM_read_bio_RSAPublicKey(keyBio, NULL, NULL, NULL); + BIO_free(keyBio); + + BN_CTX *ctx = BN_CTX_new(); + BIGNUM *a = BN_bin2bn(data.bytes, (int)data.length, NULL); + BIGNUM *r = BN_new(); + + BN_mod_exp(r, a, rsaKey->e, rsaKey->n, ctx); + + unsigned char *res = malloc((size_t)BN_num_bytes(r)); + int resLen = BN_bn2bin(r, res); + + BN_CTX_free(ctx); + BN_free(a); + BN_free(r); + + RSA_free(rsaKey); + + NSData *result = [[NSData alloc] initWithBytesNoCopy:res length:(NSUInteger)resLen freeWhenDone:true]; + + return result; +#endif } NSData *MTExp(NSData *base, NSData *exp, NSData *modulus)