AesGcmOutputStream.java org::apache::iceberg::encryption::AesGcmOutputStream org::apache::iceberg::encryption /* *LicensedtotheApacheSoftwareFoundation(ASF)underone *ormorecontributorlicenseagreements.SeetheNOTICEfile *distributedwiththisworkforadditionalinformation *regardingcopyrightownership.TheASFlicensesthisfile *toyouundertheApacheLicense,Version2.0(the *"License");youmaynotusethisfileexceptincompliance *withtheLicense.YoumayobtainacopyoftheLicenseat * *http://www.apache.org/licenses/LICENSE-2.0 * *Unlessrequiredbyapplicablelaworagreedtoinwriting, *softwaredistributedundertheLicenseisdistributedonan *"ASIS"BASIS,WITHOUTWARRANTIESORCONDITIONSOFANY *KIND,eitherexpressorimplied.SeetheLicenseforthe *specificlanguagegoverningpermissionsandlimitations *undertheLicense. */ packageorg.apache.iceberg.encryption; importjava.io.IOException; importjava.nio.ByteBuffer; importjava.nio.ByteOrder; importorg.apache.iceberg.io.PositionOutputStream; importorg.apache.iceberg.relocated.com.google.common.base.Preconditions; publicclassAesGcmOutputStreamextendsPositionOutputStream{ privatestaticfinalbyte[]HEADER_BYTES= ByteBuffer.allocate(Ciphers.GCM_STREAM_HEADER_LENGTH) .order(ByteOrder.LITTLE_ENDIAN) .put(Ciphers.GCM_STREAM_MAGIC_ARRAY) .putInt(Ciphers.PLAIN_BLOCK_SIZE) .array(); privatefinalCiphers.AesGcmEncryptorgcmEncryptor; privatefinalPositionOutputStreamtargetStream; privatefinalbyte[]fileAadPrefix; privatefinalbyte[]singleByte; privatefinalbyte[]plainBlock; privatefinalbyte[]cipherBlock; privateintpositionInPlainBlock; privateintcurrentBlockIndex; privatebooleanisHeaderWritten; privatebooleanlastBlockWritten; privatebooleanisClosed; privatelongfinalPosition; AesGcmOutputStream(PositionOutputStreamtargetStream,byte[]aesKey,byte[]fileAadPrefix){ this.targetStream=targetStream; this.gcmEncryptor=newCiphers.AesGcmEncryptor(aesKey); this.fileAadPrefix=fileAadPrefix; this.singleByte=newbyte[1]; this.plainBlock=newbyte[Ciphers.PLAIN_BLOCK_SIZE]; this.cipherBlock=newbyte[Ciphers.CIPHER_BLOCK_SIZE]; this.positionInPlainBlock=0; this.currentBlockIndex=0; this.isHeaderWritten=false; this.lastBlockWritten=false; this.isClosed=false; this.finalPosition=0; } @Override publicvoidwrite(intb)throwsIOException{ singleByte[0]=(byte)(b&0x000000FF); write(singleByte); } @Override publicvoidwrite(byte[]b,intoff,intlen)throwsIOException{ if(isClosed){ thrownewIOException("Writingtoclosedstream"); } if(!isHeaderWritten){ writeHeader(); } if(b.length-off<len){ thrownewIOException( "Insufficientbytesinbuffer:"+b.length+"-"+off+"<"+len); } intremaining=len; intoffset=off; while(remaining>0){ intfreeBlockBytes=plainBlock.length-positionInPlainBlock; inttoWrite=Math.min(freeBlockBytes,remaining); System.arraycopy(b,offset,plainBlock,positionInPlainBlock,toWrite); positionInPlainBlock+=toWrite; offset+=toWrite; remaining-=toWrite; if(positionInPlainBlock==plainBlock.length){ encryptAndWriteBlock(); } } } @Override publiclonggetPos()throwsIOException{ if(isClosed){ returnfinalPosition; } return(long)currentBlockIndex*Ciphers.PLAIN_BLOCK_SIZE+positionInPlainBlock; } @Override publicvoidflush()throwsIOException{ targetStream.flush(); } @Override publicvoidclose()throwsIOException{ if(!isHeaderWritten){ writeHeader(); } finalPosition=getPos(); isClosed=true; encryptAndWriteBlock(); targetStream.close(); } @Override publiclongstoredLength()throwsIOException{ returntargetStream.storedLength(); } privatevoidwriteHeader()throwsIOException{ targetStream.write(HEADER_BYTES); isHeaderWritten=true; } privatevoidencryptAndWriteBlock()throwsIOException{ Preconditions.checkState( !lastBlockWritten,"Cannotencryptblock:apartialblockhasalreadybeenwritten"); if(currentBlockIndex==Integer.MAX_VALUE){ thrownewIOException("Cannotwriteblock:exceededInteger.MAX_VALUEblocks"); } if(positionInPlainBlock==0&&currentBlockIndex!=0){ return; } if(positionInPlainBlock!=plainBlock.length){ //signalthatapartialblockhasbeenwrittenandmustbethelast this.lastBlockWritten=true; } byte[]aad=Ciphers.streamBlockAAD(fileAadPrefix,currentBlockIndex); intciphertextLength= gcmEncryptor.encrypt(plainBlock,0,positionInPlainBlock,cipherBlock,0,aad); targetStream.write(cipherBlock,0,ciphertextLength); positionInPlainBlock=0; currentBlockIndex++; } }