private static byte[] specToBytes(final GCMParameterSpec spec) { final byte[] nonce = spec.getIV(); final ByteArrayOutputStream baos = new ByteArrayOutputStream(); try (final DataOutputStream dos = new DataOutputStream(baos)) { dos.writeInt(spec.getTLen()); dos.writeInt(nonce.length); dos.write(nonce); dos.close(); baos.close(); } catch (final IOException ex) { throw new AssertionError("Impossible exception", ex); } return baos.toByteArray(); }
/** * Convert from platform's GCMParameterSpec to our internal version. */ @SuppressWarnings("unused") static GCMParameters fromGCMParameterSpec(AlgorithmParameterSpec params) { if (params instanceof GCMParameterSpec) { GCMParameterSpec gcmParams = (GCMParameterSpec) params; return new GCMParameters(gcmParams.getTLen(), gcmParams.getIV()); } return null; }
/** * Convert from platform's GCMParameterSpec to our internal version. */ @SuppressWarnings("unused") static GCMParameters fromGCMParameterSpec(AlgorithmParameterSpec params) { if (params instanceof GCMParameterSpec) { GCMParameterSpec gcmParams = (GCMParameterSpec) params; return new GCMParameters(gcmParams.getTLen(), gcmParams.getIV()); } return null; }
/** * Retrieves the actual algorithm parameters and validates them. * * @param cipher The cipher to interrogate for the parameters it * actually used. * * @return The IV used by the specified cipher. * * @throws JOSEException If retrieval of the algorithm parameters from * the cipher failed, or the parameters are * deemed unusable. * * @see {@link #actualParamsOf(Cipher)} * @see #validate(byte[], int) */ private static byte[] actualIVOf(final Cipher cipher) throws JOSEException { GCMParameterSpec actualParams = actualParamsOf(cipher); byte[] iv = actualParams.getIV(); int tLen = actualParams.getTLen(); validate(iv, tLen); return iv; }