chovy-3012 commented on issue #13377:
URL: https://github.com/apache/seatunnel/issues/13377#issuecomment-7137102157

   Thanks @DanielLeens — decisions for both gates below. The pure-Java 
implementation is dropped; no implementation PR until this is agreed.
   
   > "First provide a provider and packaging decision that states the supported 
JDK/runtime availability, the exact dependency or distribution path, shading 
and license impact, and how the provider is initialized and exercised in the 
packaged runtime."
   
   ## Provider / packaging: BouncyCastle, lightweight API, shaded + relocated
   
   - **Dependency:** `org.bouncycastle:bcprov-jdk18on` — one self-contained 
jar, zero transitive Maven dependencies, Java 8+ (repo baseline is Java 11). I 
validated the SM4 route locally against 1.80, 1.85, and 1.87 (current release): 
identical outputs on all probe vectors.
   - **API route:** BC's lightweight API directly (`SM4Engine` + 
`CBCBlockCipher` + `PKCS7Padding`) — no `Security.addProvider()`, no provider 
registry, no reflective lookups, so relocation is safe.
   - **Distribution:** compile dependency of `seatunnel-transforms-v2`, 
relocated into the transform jar by the module's existing `maven-shade-plugin` 
build (the same BC relocation pattern already used by the kubernetes resource 
manager and connector-file-smb); the dist already ships that jar in `lib/`. The 
existing BC use in unrelated artifacts is not relied on — BC becomes part of 
the transform's own jar.
   - **License / size:** pom-declared "Bouncy Castle Licence" (distribution: 
repo). ~7.3 MB added to the shaded jar at 1.87, with no other dependency-graph 
impact.
   - **Initialization / exercise:** cipher objects are plain per-use 
constructions — no global state. Exercised by known-answer tests on the 
published vectors, module unit tests (beside the existing `CryptoFunctionTest` 
/ `DesEncryptDecryptFunctionTest`), and job-level ITs in the existing 
`TestSQLIT` / `TestCalciteIT` suites. I already reproduced the SM4 standard's 
published known-answer vector locally on all three versions (key/plaintext 
`0123456789ABCDEFFEDCBA9876543210` → ciphertext 
`681EDF34D206965E86B3E94F536E4246`), plus round trips and tamper detection.
   
   > "Then settle one wire contract before coding: either a documented Base64 
`STRING` envelope compatible with the existing AES-function style, or a true 
`BINARY` result with the same observable type and byte layout in every 
supported SQL engine. For the latter, include the Zeta dispatcher/type path and 
Calcite's `CalciteUdf` SPI/`byte[]` bridge in the feasibility proof; do not 
claim engine parity from the Zeta implementation alone."
   
   ## Wire contract: BINARY, with the cross-engine proof
   
   Option (b). One `Sm4Util` (BC lightweight calls, key/IV validation, the two 
IV layouts) registered on both engines, mirroring the existing `DES_ENCRYPT` 
dual registration:
   
   - **Zeta:** a `ZetaUDF`. `ZetaSQLType` honors `udf.resultType(argsType)`, so 
the result is typed `PrimitiveByteArrayType.INSTANCE` (`byte[]` / `BYTES`), not 
STRING like the AES table. `TO_BASE64(byte[])` encodes it byte-exactly for 
display.
   - **Calcite:** a `CalciteUdf` with `public static byte[] eval(...)`. 
`BuiltinFunctions` auto-selects `BinaryAwareScalarFunction` for `byte[]` 
signatures, bridging `byte[]` ↔ `ByteString`; `CalciteTypeConverter` maps 
`BYTES` ↔ `VARBINARY`. This bridge already runs in production for the eight 
vector UDFs.
   - **Parity, precisely:** same key/IV/plaintext → either engine outputs a 
field typed `BYTES` holding the same `byte[]` (3-arg = bare ciphertext; 2-arg = 
`IV ‖ ciphertext`). The tests assert type and bytes on both engines, not Zeta 
alone.
   
   Two corrections to my previous proposal, found while verifying against 
current source:
   
   1. The decrypt examples wrapped in `FROM_BASE64()` were wrong — Zeta's 
`FROM_BASE64` returns a charset-decoded STRING and cannot losslessly restore 
cipher bytes. Corrected boundary: `SM4_DECRYPT` accepts BINARY only (a Base64 
STRING is an error); `TO_BASE64()` stays the display/export direction; 
decrypting Base64 VARCHAR storage is out of v1 scope.
   2. "No per-engine UDF registrations" is superseded by the dual registration 
above.
   
   > "The test plan should use published vectors and engine-level binary-type 
assertions in addition to the key, IV, malformed-input, and documentation cases 
already listed."
   
   ## Test plan
   
   Published vectors plus fixed key/IV triples asserted on raw bytes; the same 
SQL executed through both engines, asserting the result type is `BYTES` and the 
raw bytes are identical; all previously listed cases retained (key forms, IV 
handling, NULL, malformed/tampered input, wrong key, EN/ZH docs).
   
   > "Keep secrets and derived key material out of logs, exceptions, and 
persisted diagnostics."
   
   ## Secrets
   
   Key, derived key material, plaintext, ciphertext, and IV never appear in 
logs, exception messages, or persisted diagnostics — errors carry metadata only.
   
   If this provider decision and the BINARY contract look agreed, I'll open the 
focused implementation proposal against them.
   


-- 
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]

Reply via email to