cipher: stop building the hash and RSA halves nothing calls - #220
Conversation
open_cipher.ko shipped at 172,328 bytes installed, and about half of that was two subsystems with no caller. A kernel module gets no dead-code elimination -- ld -r takes every object the Kbuild names -- so the vendor SDK's full crypto stack shipped whole: 39 KB of RSA, being the IFEP hardware driver plus a software mbedTLS bignum/RSA/ASN.1 fallback compiled into the kernel, and 14 KB of hash, SHA-1 through SHA-512 with HMAC. majestic is the only thing that opens /dev/cipher on these boards, and its include/majestic/hisi/cipher_abi.h is explicit about using neither: the engine's HMAC-SHA1 reprograms its key per packet and measured 192.4 us against mbedTLS's 37.4, and its RSA runs once per clip where userspace mbedTLS already does the job. `nm` agrees -- the two subsystems are reached through exactly twelve symbols, all of them from kapi_dispatch.c, and nothing in symc, klad, the TRNG or the OTP path touches them. So kapi_hash_rsa_absent.c supplies those twelve and the objects come off the list. The dispatch table is deliberately left whole: crypto_ioctl() indexes dispatch_func[] by the command's nr, and majestic derives its command numbers from the same struct sizes the driver does, so a removed row would land every later command on the wrong handler. The rows and their handlers stay; HashStart and RsaEncrypt now return HI_ERR_CIPHER_UNSUPPORTED instead of running code no camera reaches. Vendor sources are untouched and still in the tree. Restoring either subsystem is putting its objects back and dropping this file, not a merge. Measured on gk7205v200, same toolchain and kernel: 172,328 -> 96,188 bytes installed, -44.2%, and 51,844 -> 28,760 compressed, which is about 23 KB off an xz squashfs. gk7205v300_lite failed CI today four kilobytes over its 5120 KB partition. hi3516cv500 builds this from its own kbuild and keeps the full stack for now: its tree is the later SDK vintage, the kapi signatures differ, and the same trim there wants its own file and its own check.
The previous commit did the hi3516ev200 tree, which covers ev200/ev300 and the Goke parts. hi3516cv500 builds the same 36-object vendor list from its own kbuild and carried the same weight -- 171,380 bytes installed, with the same mbedTLS bignum/RSA/ASN.1 implementation compiled into the kernel and the same SHA-1-through-SHA-512 hash engine that nothing opens. The boundary was re-checked against this tree's own objects rather than carried over. It is the same twelve symbols, all from kapi_dispatch.c, and nothing in symc, klad, the TRNG or the OTP path reaches them -- which is worth having verified, because these two trees are different SDK vintages and not interchangeable: cv500's kapi_rsa_* take a cryp_rsa_crypt_data where ev200's take six arguments, its kapi_hash_finish takes the output buffer length, and its log macros are lower case. Hence a second copy of kapi_hash_rsa_absent.c rather than a shared one. Measured on hi3516cv500, same toolchain and kernel: 171,380 -> 97,208 bytes installed, -43.3%, and 52,468 -> 29,476 compressed. All 17 dispatch rows intact, no mbedTLS symbols left, every remaining undefined symbol an ordinary kernel or osal one. That is every board that builds this driver. The other chips naming cipher objects either build no cipher module at all -- hi3516av100, hi3516cv200 and hi3516cv100 have the objects defined with obj-m commented out -- or build a different and much smaller one: hi3520dv200's V2-era hi_cipher is three files and hi3519dv500's V5 security_subsys is two, neither with an RSA bignum or a software crypto fallback in it.
PR Summary by QodoTrim unused hash and RSA subsystems from cipher modules
AI Description
Diagram
High-Level Assessment
Files changed (4)
|
Code Review by Qodo🐞 Bugs (0) 📘 Rule violations (0) 📎 Requirement gaps (0)
Great, no issues found!Qodo reviewed your code and found no material issues that require reviewTip of the day💡 Did you know, you can show, collapse, or hide each part of a finding: code, evidence, and all |
Validated on hardware, one camera per trimmed tree
|
open_cipher.koships at 172,328 bytes installed, and about half of it is two subsystems with no caller. Ongk7202v300-litethat module is the whole+171.1 KBthe firmware-explorer trend attributes to hisilicon-opensdk — byte for byte, nothing else moved. Today it stopped being cosmetic:gk7205v300_litefailed CI four kilobytes over its 5120 KB partition.Why a driver for a few algorithms is 172 KB
Three reasons, and none of them is the AES:
bignum.c(2670 lines),rsa.c,rsa_internal.c,asn1parse.c,oid.c— a userspace big-number library compiled into a kernel module as the fallback for the hardware block..kogets no dead-code elimination. Modules are linked withld -r; there is no--gc-sections. Every object the Kbuild names ships whether or not an ioctl reaches it.INSTALL_MOD_STRIP=1is only--strip-debugand recovers 1.2 KB, because there was no debug info to begin with.Where the 110,772 bytes of code and data went:
The remaining 62 KB of the file is ELF metadata the module loader consumes — 32 KB of relocations, 30 KB of symtab/strtab. Not strippable, but it scales down with the code.
Nothing calls hash or RSA
majestic is the only consumer of
/dev/cipheron these boards — it goes straight to the ioctls rather than throughlibhi_cipher.so— and itsinclude/majestic/hisi/cipher_abi.his explicit about using neither:The engine's HMAC-SHA1 closes its handle on finish and so reprograms the key for every packet — 192.4 µs against mbedTLS's 37.4. The recorder's public-key mode is RSAES-OAEP once per clip, in userspace.
Verified by symbols rather than by reading: the two subsystems are reached through exactly twelve entry points, all of them from
kapi_dispatch.c. Nothing in symc, klad, the TRNG or the OTP path touches them. Re-checked independently against the cv500 tree, which is a different SDK vintage — same answer, same twelve.What changes
kapi_hash_rsa_absent.csupplies those twelve; the objects come off the Kbuild lists. Also gone are the_v100cores and theext_*extensions, which compile to empty objects under these parts'CHIP_TYPE.The dispatch table is deliberately left whole.
crypto_ioctl()indexesdispatch_func[]by the command'snrand then checks the row'scmdmatches, and majestic derives its command numbers from the same struct sizes the driver does — a removed row lands every later command on the wrong handler, and the only thing that catches it reports "copy data from user failed", which is a lie about a table that is simply off by one. All 17 rows and their handlers stay;HashStartandRsaEncryptnow returnHI_ERR_CIPHER_UNSUPPORTEDinstead of running code no camera reaches.Vendor sources are untouched and still in the tree. Restoring either subsystem is putting its objects back on the list and dropping the stub file — not a merge.
Two copies of the stub rather than one shared file, because the trees are not interchangeable: cv500's
kapi_rsa_*take acryp_rsa_crypt_datawhere ev200's take six arguments, itskapi_hash_finishtakes the output buffer length, and its log macros are lower case.Measured
Both built against their own kernel and toolchain, baseline and trimmed in the same setup:
Text drops 90,992 → 44,644 on the ev200 build. All 74 mbedTLS symbols gone, all 17 dispatch rows present, every remaining undefined symbol an ordinary kernel or osal one.
Against a 4 KB overflow on
gk7205v300_lite, that is roughly 6× the margin needed, and it lands onhi3518ev300_litetoo — the board thehisilicon-opensdk.mkcomments describe as sitting at 5120/5120 KB with sensors already trimmed to make room.Coverage
This is every board that builds this driver. The other chips naming cipher objects either build no cipher module at all — hi3516av100, hi3516cv200 and hi3516cv100 have the objects defined with
obj-mcommented out — or build a different and much smaller one: hi3520dv200's V2-erahi_cipheris three files and hi3519dv500's V5security_subsysis two, neither with an RSA bignum or a software crypto fallback in it.Testing
Built clean for
CHIPARCH=gk7205v200(kernel 4.9.37) andCHIPARCH=hi3516cv500. Not yet run on hardware — the behaviour worth checking on a camera is that SRTP still takes the engine, i.e./proc/interruptsshows cipher completions tracking packet rate under a WebRTC session, and thatmajestic --otp-statusstill passes its known-answer tests for the ladder and the TRNG.