yandrey321 commented on code in PR #9344:
URL: https://github.com/apache/ozone/pull/9344#discussion_r2561310359


##########
hadoop-ozone/ozone-manager/src/main/java/org/apache/hadoop/ozone/security/STSTokenEncryption.java:
##########
@@ -0,0 +1,190 @@
+/*
+ * 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.
+ */
+
+package org.apache.hadoop.ozone.security;
+
+import com.google.common.base.Preconditions;
+import java.nio.charset.StandardCharsets;
+import java.security.SecureRandom;
+import java.util.Base64;
+import javax.crypto.Cipher;
+import javax.crypto.spec.GCMParameterSpec;
+import javax.crypto.spec.SecretKeySpec;
+import org.apache.hadoop.hdds.annotation.InterfaceAudience;
+import org.apache.hadoop.hdds.annotation.InterfaceStability;
+import org.bouncycastle.crypto.digests.SHA256Digest;
+import org.bouncycastle.crypto.generators.HKDFBytesGenerator;
+import org.bouncycastle.crypto.params.HKDFParameters;
+import org.bouncycastle.jce.provider.BouncyCastleProvider;
+
+/**
+ * Utility class for encrypting and decrypting sensitive data in STS tokens.
+ * Uses HKDF to derive an AES encryption key from the SCM ManagedSecretKey,
+ * then uses AES-GCM for authenticated encryption.
+ */
[email protected]
[email protected]
+public final class STSTokenEncryption {
+  
+  // HKDF parameters
+  private static final byte[] HKDF_INFO = 
"STS-TOKEN-ENCRYPTION".getBytes(StandardCharsets.UTF_8);
+  private static final int HKDF_SALT_LENGTH = 16; // 128 bits
+  private static final int AES_KEY_LENGTH = 32; // 256 bits
+  
+  // AES-GCM parameters
+  private static final int GCM_IV_LENGTH = 12; // 96 bits
+  
+  private static final SecureRandom SECURE_RANDOM = new SecureRandom();
+  private static final BouncyCastleProvider BC_PROVIDER = new 
BouncyCastleProvider();
+  
+  private STSTokenEncryption() {
+  }
+
+  /**
+   * Encrypt sensitive data using AES-GCM with a key derived from the secret 
key via HKDF,
+   * binding the provided AAD to the authentication tag.
+   *
+   * @param plaintext         the sensitive data to encrypt
+   * @param secretKeyBytes    the secret key bytes from ManagedSecretKey
+   * @param aad               additional authenticated data to bind
+   * @return base64-encoded encrypted data with Salt and IV prepended
+   * @throws STSTokenEncryptionException if encryption fails
+   */
+  public static String encrypt(String plaintext, byte[] secretKeyBytes, byte[] 
aad) throws STSTokenEncryptionException {
+    Preconditions.checkArgument(
+        secretKeyBytes != null && secretKeyBytes.length > 0, "The 
secretKeyBytes must not be null nor empty");
+    Preconditions.checkArgument(aad != null && aad.length > 0, "The aad must 
not be null nor empty");
+    // Don't encrypt null/empty strings
+    if (plaintext == null || plaintext.isEmpty()) {
+      return plaintext;
+    }
+    
+    byte[] aesKey;
+    byte[] iv;
+    byte[] salt;
+    try {
+      // Generate random salt
+      salt = new byte[HKDF_SALT_LENGTH];
+      SECURE_RANDOM.nextBytes(salt);
+
+      // Derive AES key using HKDF with random salt
+      aesKey = deriveKey(secretKeyBytes, salt);
+      
+      // Generate random IV
+      iv = new byte[GCM_IV_LENGTH];
+      SECURE_RANDOM.nextBytes(iv);
+
+      // Initialize AES-GCM cipher
+      final Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding", 
BC_PROVIDER);
+      final GCMParameterSpec spec = new GCMParameterSpec(128, iv);

Review Comment:
   128 should be a named param



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to