@@ -166,7 +166,9 @@ public static byte[] bytes_to_X25519PublicKey(byte[] publicKeyBytes) {
166
166
167
167
public static byte [] P_256PublicKey_to_bytes (ECPublicKey publicKey ) {
168
168
169
- return EC5Util .convertPoint (publicKey .getParams (), publicKey .getW ()).getEncoded (true );
169
+ byte [] point = EC5Util .convertPoint (publicKey .getParams (), publicKey .getW ()).getEncoded (true );
170
+ if (point .length != 33 ) throw new IllegalArgumentException ("Invalid key size (not 33 bytes): " + Hex .encodeHexString (point ) + ", length=" + point .length );
171
+ return point ;
170
172
}
171
173
172
174
public static ECPublicKey bytes_to_P_256PublicKey (byte [] publicKeyBytes ) {
@@ -198,7 +200,9 @@ public static ECPublicKey bytes_to_P_256PublicKey(byte[] publicKeyBytes) {
198
200
199
201
public static byte [] P_384PublicKey_to_bytes (ECPublicKey publicKey ) {
200
202
201
- return EC5Util .convertPoint (publicKey .getParams (), publicKey .getW ()).getEncoded (true );
203
+ byte [] point = EC5Util .convertPoint (publicKey .getParams (), publicKey .getW ()).getEncoded (true );
204
+ if (point .length != 49 ) throw new IllegalArgumentException ("Invalid key size (not 49 bytes): " + Hex .encodeHexString (point ) + ", length=" + point .length );
205
+ return point ;
202
206
}
203
207
204
208
public static ECPublicKey bytes_to_P_384PublicKey (byte [] publicKeyBytes ) {
@@ -230,12 +234,14 @@ public static ECPublicKey bytes_to_P_384PublicKey(byte[] publicKeyBytes) {
230
234
231
235
public static byte [] P_521PublicKey_to_bytes (ECPublicKey publicKey ) {
232
236
233
- return EC5Util .convertPoint (publicKey .getParams (), publicKey .getW ()).getEncoded (true );
237
+ byte [] point = EC5Util .convertPoint (publicKey .getParams (), publicKey .getW ()).getEncoded (true );
238
+ if (point .length != 65 && point .length != 66 && point .length != 67 ) throw new IllegalArgumentException ("Invalid key size (not 65 or 66 or 67 bytes): " + Hex .encodeHexString (point ) + ", length=" + point .length );
239
+ return point ;
234
240
}
235
241
236
242
public static ECPublicKey bytes_to_P_521PublicKey (byte [] publicKeyBytes ) {
237
243
238
- if (publicKeyBytes .length != 65 && publicKeyBytes .length != 66 && publicKeyBytes .length != 67 ) throw new IllegalArgumentException ("Expected 64 or 65 or 66 bytes instead of " + publicKeyBytes .length );
244
+ if (publicKeyBytes .length != 65 && publicKeyBytes .length != 66 && publicKeyBytes .length != 67 ) throw new IllegalArgumentException ("Expected 65 or 66 or 67 bytes instead of " + publicKeyBytes .length );
239
245
240
246
ECNamedCurveParameterSpec ecNamedCurveParameterSpec = ECNamedCurveTable .getParameterSpec ("secp521r1" );
241
247
org .bouncycastle .math .ec .ECPoint bcEcPoint = ecNamedCurveParameterSpec .getCurve ().decodePoint (publicKeyBytes );
0 commit comments