
org.apache.milagro.amcl.RSA3072.RSA Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of milagro-crypto-java Show documentation
Show all versions of milagro-crypto-java Show documentation
MCJL - Milagro Crypto Java Library
The newest version!
/*
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you 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.
*/
/* RSA API high-level functions */
package org.apache.milagro.amcl.RSA3072;
import org.apache.milagro.amcl.RAND;
import org.apache.milagro.amcl.HASH256;
import org.apache.milagro.amcl.HASH384;
import org.apache.milagro.amcl.HASH512;
public final class RSA {
public static final int RFS=BIG.MODBYTES*FF.FFLEN;
public static final int SHA256=32;
public static final int SHA384=48;
public static final int SHA512=64;
public static final int HASH_TYPE=SHA256;
/* Hash number (optional) and string to array size of Bigs */
public static byte[] hashit(int sha,byte[] A,int n)
{
byte[] R=null;
if (sha==SHA256)
{
HASH256 H=new HASH256();
if (A!=null) H.process_array(A);
if (n>=0) H.process_num(n);
R=H.hash();
}
if (sha==SHA384)
{
HASH384 H=new HASH384();
if (A!=null) H.process_array(A);
if (n>=0) H.process_num(n);
R=H.hash();
}
if (sha==SHA512)
{
HASH512 H=new HASH512();
if (A!=null) H.process_array(A);
if (n>=0) H.process_num(n);
R=H.hash();
}
return R;
}
/* generate an RSA key pair */
public static void KEY_PAIR(RAND rng,int e,private_key PRIV,public_key PUB)
{ /* IEEE1363 A16.11/A16.12 more or less */
int n=PUB.n.getlen()/2;
FF t = new FF(n);
FF p1=new FF(n);
FF q1=new FF(n);
for (;;)
{
PRIV.p.random(rng);
while (PRIV.p.lastbits(2)!=3) PRIV.p.inc(1);
while (!FF.prime(PRIV.p,rng)) PRIV.p.inc(4);
p1.copy(PRIV.p);
p1.dec(1);
if (p1.cfactor(e)) continue;
break;
}
for (;;)
{
PRIV.q.random(rng);
while (PRIV.q.lastbits(2)!=3) PRIV.q.inc(1);
while (!FF.prime(PRIV.q,rng)) PRIV.q.inc(4);
q1.copy(PRIV.q);
q1.dec(1);
if (q1.cfactor(e)) continue;
break;
}
PUB.n=FF.mul(PRIV.p,PRIV.q);
PUB.e=e;
t.copy(p1);
t.shr();
PRIV.dp.set(e);
PRIV.dp.invmodp(t);
if (PRIV.dp.parity()==0) PRIV.dp.add(t);
PRIV.dp.norm();
t.copy(q1);
t.shr();
PRIV.dq.set(e);
PRIV.dq.invmodp(t);
if (PRIV.dq.parity()==0) PRIV.dq.add(t);
PRIV.dq.norm();
PRIV.c.copy(PRIV.p);
PRIV.c.invmodp(PRIV.q);
return;
}
/* Mask Generation Function */
public static void MGF1(int sha,byte[] Z,int olen,byte[] K)
{
int hlen=sha;
byte[] B;
int counter,cthreshold,k=0;
for (int i=0;iolen) for (int i=0;iolen-hlen-seedlen-1) return new byte[0];
byte[] DBMASK=new byte[olen-seedlen];
byte[] h=hashit(sha,p,-1);
for (i=0;i=d;i--)
f[i]=f[i-d];
for (i=d-1;i>=0;i--)
f[i]=0;
return f;
}
/* OAEP Message Decoding for Decryption */
public static byte[] OAEP_DECODE(int sha,byte[] p,byte[] f)
{
int x,t;
boolean comp;
int i,k,olen=RFS-1;
int hlen,seedlen;
hlen=sha;
byte[] SEED=new byte[hlen];
seedlen=hlen;
byte[] CHASH=new byte[hlen];
if (olen=d;i--)
f[i]=f[i-d];
for (i=d-1;i>=0;i--)
f[i]=0;
}
byte[] h=hashit(sha,p,-1);
for (i=0;i=olen-seedlen-hlen) return new byte[0];
if (DBMASK[k]!=0) break;
}
t=DBMASK[k];
if (!comp || x!=0 || t!=0x01)
{
for (i=0;i0) jq.add(PRIV.q);
jq.sub(jp);
jq.norm();
FF t=FF.mul(PRIV.c,jq);
jq=t.dmod(PRIV.q);
t=FF.mul(jq,PRIV.p);
g.add(t);
g.norm();
g.toBytes(F);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy