gheb / shamir-secret-sharing-scheme
用于Shamir秘密共享方案的PHP库
Requires
- php: ^7.4|^8.0
- ext-bcmath: *
- ext-gmp: *
- ext-sodium: *
- krowinski/bcmath-extended: ^6.0
Requires (Dev)
- phpstan/phpstan: ^0.12.70
This package is auto-updated.
Last update: 2024-08-29 05:43:45 UTC
README
请勿在生产环境中使用,此版本处于原型阶段
基本示例可在examples目录中找到。
<?php declare(strict_types=1); require '../vendor/autoload.php'; use Gheb\ShamirSecretSharingScheme\SodiumHelper; use Gheb\ShamirSecretSharingScheme\ShamirSecretSharingHelper; $secretKey = SodiumHelper::generateKey(); // this will be stored in shares. $nonce = SodiumHelper::generateNonce(); // this could be part of your app configuration $secret = 'Sensitive information'; var_dump("secret : $secret"); // symmetric key encryption with padded message to hide it's length. This does not matter, it's for show ! $encryptedMessage = SodiumHelper::encrypt($secret, $secretKey, $nonce); // This is the best part ! // It splits the secret key into 3 shares. (but it could be more) // initialisation of modulo value, addressing insecure integer arithmetic. // this would be part of your app configuration or stored elsewhere. $m = "997"; // chose any prime number (here around 1000) $points = ShamirSecretSharingHelper::getShareablePoints($secretKey, $m, 3); var_dump($points); // there you can store your points at different locations. // and later get them back to get your secret back // reconstructing and decrypting // to reconstruct the secretKey the 3 points are needed along the $decryptedSecretKey = ShamirSecretSharingHelper::reconstructSecret($points, $m); $decryptedSecret = SodiumHelper::decrypt($encryptedMessage, $nonce, $decryptedSecretKey); var_dump("decrypted secret : $decryptedSecret");
资源
Shamir秘密共享方案阅读材料
https://en.wikipedia.org/wiki/Homomorphic_secret_sharing
https://en.wikipedia.org/wiki/Shamir%27s_Secret_Sharing
https://wiki.owasp.org/index.php/Security_by_Design_Principles
https://kariera.future-processing.pl/blog/splitting-your-secrets-with-shamirs-secret-sharing-scheme/
https://www.geeksforgeeks.org/shamirs-secret-sharing-algorithm-cryptography/
https://ericrafaloff.com/shamirs-secret-sharing-scheme/
安全考虑
Sharmir的秘密共享方案提供了信息论安全性,这意味着我们探索的数学已经被证明是不可破解的,即使是对拥有无限计算能力的攻击者也是如此。然而,该方案仍然存在一些已知问题。
例如,Sharmir的方案不会产生可验证的份额,这意味着个人可以自由提交伪造的份额,从而阻止正确秘密的重建。拥有足够信息的对抗性份额持有者甚至可以产生一个不同的份额,使得SS重建为他们的选择值。这个问题可以通过如Feldman方案这样的可验证秘密共享方案来解决。
另一个问题是,由于任何给定份额的长度等于相关秘密的长度,秘密的长度很容易泄露。这个问题可以通过简单地将秘密填充到固定长度来解决,这在示例中已用sodium演示。
最后,需要注意的是,我们对安全的担忧可能不仅限于方案本身。对于现实世界的密码学应用,通常存在侧信道攻击的威胁,其中攻击者试图从应用程序的计时、缓存、故障等中提取有用信息。如果这是一个问题,应在开发过程中仔细考虑,例如使用恒定时间函数和查找,防止内存分页到磁盘,以及其他一些超出此库范围的事情。