/external/bouncycastle/bcprov/src/main/java/org/bouncycastle/jcajce/provider/asymmetric/ec/SignatureSpi.java

https://gitlab.com/brian0218/rk3066_r-box_android4.2.2_sdk · Java · 352 lines · 199 code · 40 blank · 113 comment · 8 complexity · b678a4e4e2e10ffe24ede3e58f234aa7 MD5 · raw file

  1. package org.bouncycastle.jcajce.provider.asymmetric.ec;
  2. import java.io.IOException;
  3. import java.math.BigInteger;
  4. import java.security.InvalidKeyException;
  5. import java.security.PrivateKey;
  6. import java.security.PublicKey;
  7. import java.security.interfaces.ECPublicKey;
  8. import org.bouncycastle.asn1.ASN1EncodableVector;
  9. import org.bouncycastle.asn1.ASN1Encoding;
  10. import org.bouncycastle.asn1.ASN1Primitive;
  11. import org.bouncycastle.asn1.ASN1Sequence;
  12. import org.bouncycastle.asn1.DERInteger;
  13. import org.bouncycastle.asn1.DERSequence;
  14. import org.bouncycastle.asn1.x509.SubjectPublicKeyInfo;
  15. import org.bouncycastle.crypto.CipherParameters;
  16. import org.bouncycastle.crypto.DSA;
  17. import org.bouncycastle.crypto.Digest;
  18. import org.bouncycastle.crypto.digests.NullDigest;
  19. // BEGIN android-added
  20. import org.bouncycastle.crypto.digests.AndroidDigestFactory;
  21. // END android-added
  22. // BEGIN android-removed
  23. // import org.bouncycastle.crypto.digests.RIPEMD160Digest;
  24. // import org.bouncycastle.crypto.digests.SHA1Digest;
  25. // import org.bouncycastle.crypto.digests.SHA224Digest;
  26. // import org.bouncycastle.crypto.digests.SHA256Digest;
  27. // import org.bouncycastle.crypto.digests.SHA384Digest;
  28. // import org.bouncycastle.crypto.digests.SHA512Digest;
  29. // END android-removed
  30. import org.bouncycastle.crypto.params.ParametersWithRandom;
  31. import org.bouncycastle.crypto.signers.ECDSASigner;
  32. // BEGIN android-removed
  33. // import org.bouncycastle.crypto.signers.ECNRSigner;
  34. // END android-removed
  35. import org.bouncycastle.jcajce.provider.asymmetric.util.DSABase;
  36. import org.bouncycastle.jcajce.provider.asymmetric.util.DSAEncoder;
  37. import org.bouncycastle.jce.interfaces.ECKey;
  38. import org.bouncycastle.jce.provider.BouncyCastleProvider;
  39. public class SignatureSpi
  40. extends DSABase
  41. {
  42. SignatureSpi(Digest digest, DSA signer, DSAEncoder encoder)
  43. {
  44. super(digest, signer, encoder);
  45. }
  46. protected void engineInitVerify(PublicKey publicKey)
  47. throws InvalidKeyException
  48. {
  49. CipherParameters param;
  50. if (publicKey instanceof ECPublicKey)
  51. {
  52. param = ECUtil.generatePublicKeyParameter(publicKey);
  53. }
  54. else
  55. {
  56. try
  57. {
  58. byte[] bytes = publicKey.getEncoded();
  59. publicKey = BouncyCastleProvider.getPublicKey(SubjectPublicKeyInfo.getInstance(bytes));
  60. if (publicKey instanceof ECPublicKey)
  61. {
  62. param = ECUtil.generatePublicKeyParameter(publicKey);
  63. }
  64. else
  65. {
  66. throw new InvalidKeyException("can't recognise key type in ECDSA based signer");
  67. }
  68. }
  69. catch (Exception e)
  70. {
  71. throw new InvalidKeyException("can't recognise key type in ECDSA based signer");
  72. }
  73. }
  74. digest.reset();
  75. signer.init(false, param);
  76. }
  77. protected void engineInitSign(
  78. PrivateKey privateKey)
  79. throws InvalidKeyException
  80. {
  81. CipherParameters param;
  82. if (privateKey instanceof ECKey)
  83. {
  84. param = ECUtil.generatePrivateKeyParameter(privateKey);
  85. }
  86. else
  87. {
  88. throw new InvalidKeyException("can't recognise key type in ECDSA based signer");
  89. }
  90. digest.reset();
  91. if (appRandom != null)
  92. {
  93. signer.init(true, new ParametersWithRandom(param, appRandom));
  94. }
  95. else
  96. {
  97. signer.init(true, param);
  98. }
  99. }
  100. static public class ecDSA
  101. extends SignatureSpi
  102. {
  103. public ecDSA()
  104. {
  105. // BEGIN android-changed
  106. super(AndroidDigestFactory.getSHA1(), new ECDSASigner(), new StdDSAEncoder());
  107. // END android-changed
  108. }
  109. }
  110. static public class ecDSAnone
  111. extends SignatureSpi
  112. {
  113. public ecDSAnone()
  114. {
  115. super(new NullDigest(), new ECDSASigner(), new StdDSAEncoder());
  116. }
  117. }
  118. // BEGIN android-removed
  119. // static public class ecDSA224
  120. // extends SignatureSpi
  121. // {
  122. // public ecDSA224()
  123. // {
  124. // super(new SHA224Digest(), new ECDSASigner(), new StdDSAEncoder());
  125. // }
  126. // }
  127. // END android-removed
  128. static public class ecDSA256
  129. extends SignatureSpi
  130. {
  131. public ecDSA256()
  132. {
  133. // BEGIN android-changed
  134. super(AndroidDigestFactory.getSHA256(), new ECDSASigner(), new StdDSAEncoder());
  135. // END android-changed
  136. }
  137. }
  138. static public class ecDSA384
  139. extends SignatureSpi
  140. {
  141. public ecDSA384()
  142. {
  143. // BEGIN android-changed
  144. super(AndroidDigestFactory.getSHA384(), new ECDSASigner(), new StdDSAEncoder());
  145. // END android-changed
  146. }
  147. }
  148. static public class ecDSA512
  149. extends SignatureSpi
  150. {
  151. public ecDSA512()
  152. {
  153. // BEGIN android-changed
  154. super(AndroidDigestFactory.getSHA512(), new ECDSASigner(), new StdDSAEncoder());
  155. // END android-changed
  156. }
  157. }
  158. // BEGIN android-removed
  159. // static public class ecDSARipeMD160
  160. // extends SignatureSpi
  161. // {
  162. // public ecDSARipeMD160()
  163. // {
  164. // super(new RIPEMD160Digest(), new ECDSASigner(), new StdDSAEncoder());
  165. // }
  166. // }
  167. //
  168. // static public class ecNR
  169. // extends SignatureSpi
  170. // {
  171. // public ecNR()
  172. // {
  173. // super(new SHA1Digest(), new ECNRSigner(), new StdDSAEncoder());
  174. // }
  175. // }
  176. //
  177. // static public class ecNR224
  178. // extends SignatureSpi
  179. // {
  180. // public ecNR224()
  181. // {
  182. // super(new SHA224Digest(), new ECNRSigner(), new StdDSAEncoder());
  183. // }
  184. // }
  185. //
  186. // static public class ecNR256
  187. // extends SignatureSpi
  188. // {
  189. // public ecNR256()
  190. // {
  191. // super(new SHA256Digest(), new ECNRSigner(), new StdDSAEncoder());
  192. // }
  193. // }
  194. //
  195. // static public class ecNR384
  196. // extends SignatureSpi
  197. // {
  198. // public ecNR384()
  199. // {
  200. // super(new SHA384Digest(), new ECNRSigner(), new StdDSAEncoder());
  201. // }
  202. // }
  203. //
  204. // static public class ecNR512
  205. // extends SignatureSpi
  206. // {
  207. // public ecNR512()
  208. // {
  209. // super(new SHA512Digest(), new ECNRSigner(), new StdDSAEncoder());
  210. // }
  211. // }
  212. //
  213. // static public class ecCVCDSA
  214. // extends SignatureSpi
  215. // {
  216. // public ecCVCDSA()
  217. // {
  218. // super(new SHA1Digest(), new ECDSASigner(), new CVCDSAEncoder());
  219. // }
  220. // }
  221. //
  222. // static public class ecCVCDSA224
  223. // extends SignatureSpi
  224. // {
  225. // public ecCVCDSA224()
  226. // {
  227. // super(new SHA224Digest(), new ECDSASigner(), new CVCDSAEncoder());
  228. // }
  229. // }
  230. //
  231. // static public class ecCVCDSA256
  232. // extends SignatureSpi
  233. // {
  234. // public ecCVCDSA256()
  235. // {
  236. // super(new SHA256Digest(), new ECDSASigner(), new CVCDSAEncoder());
  237. // }
  238. // }
  239. // END android-removed
  240. private static class StdDSAEncoder
  241. implements DSAEncoder
  242. {
  243. public byte[] encode(
  244. BigInteger r,
  245. BigInteger s)
  246. throws IOException
  247. {
  248. ASN1EncodableVector v = new ASN1EncodableVector();
  249. v.add(new DERInteger(r));
  250. v.add(new DERInteger(s));
  251. return new DERSequence(v).getEncoded(ASN1Encoding.DER);
  252. }
  253. public BigInteger[] decode(
  254. byte[] encoding)
  255. throws IOException
  256. {
  257. ASN1Sequence s = (ASN1Sequence)ASN1Primitive.fromByteArray(encoding);
  258. BigInteger[] sig = new BigInteger[2];
  259. sig[0] = ((DERInteger)s.getObjectAt(0)).getValue();
  260. sig[1] = ((DERInteger)s.getObjectAt(1)).getValue();
  261. return sig;
  262. }
  263. }
  264. private static class CVCDSAEncoder
  265. implements DSAEncoder
  266. {
  267. public byte[] encode(
  268. BigInteger r,
  269. BigInteger s)
  270. throws IOException
  271. {
  272. byte[] first = makeUnsigned(r);
  273. byte[] second = makeUnsigned(s);
  274. byte[] res;
  275. if (first.length > second.length)
  276. {
  277. res = new byte[first.length * 2];
  278. }
  279. else
  280. {
  281. res = new byte[second.length * 2];
  282. }
  283. System.arraycopy(first, 0, res, res.length / 2 - first.length, first.length);
  284. System.arraycopy(second, 0, res, res.length - second.length, second.length);
  285. return res;
  286. }
  287. private byte[] makeUnsigned(BigInteger val)
  288. {
  289. byte[] res = val.toByteArray();
  290. if (res[0] == 0)
  291. {
  292. byte[] tmp = new byte[res.length - 1];
  293. System.arraycopy(res, 1, tmp, 0, tmp.length);
  294. return tmp;
  295. }
  296. return res;
  297. }
  298. public BigInteger[] decode(
  299. byte[] encoding)
  300. throws IOException
  301. {
  302. BigInteger[] sig = new BigInteger[2];
  303. byte[] first = new byte[encoding.length / 2];
  304. byte[] second = new byte[encoding.length / 2];
  305. System.arraycopy(encoding, 0, first, 0, first.length);
  306. System.arraycopy(encoding, first.length, second, 0, second.length);
  307. sig[0] = new BigInteger(1, first);
  308. sig[1] = new BigInteger(1, second);
  309. return sig;
  310. }
  311. }
  312. }