@@ -724,25 +724,142 @@ A simple example:
724
724
Signing/Verifying
725
725
-----------------
726
726
727
+ Signing and verification mechanisms require two components:
728
+
729
+ * the cipher; and
730
+ * the hashing function.
731
+
732
+ Raw versions for some mechanisms also exist. These require you to do your
733
+ own hashing outside of PKCS #11.
734
+
735
+ Signing functions typically work on a finite length of data, so the signing
736
+ of large amounts of data requires hashing with a secure one-way hash function.
737
+
727
738
AES
728
739
~~~
729
740
741
+ A `MAC ` is required for signing with AES. The default mechanism is
742
+ `SHA512_HMAC ` (aka HMAC-SHA512).
743
+
744
+ A number of other hashing functions and MACs are available depending on
745
+ your implementation.
746
+
747
+ ::
748
+
749
+ # Given a secret key, `key`
750
+ signature = key.sign(data)
751
+
752
+ assert key.verify(data, signature)
753
+
730
754
RSA
731
755
~~~
732
756
757
+ The default signing and verification mechanism for RSA is `RSA_SHA512_PKCS `.
758
+
759
+ Other mechanisms are available:
760
+
761
+ +-------------------+-------------------------------------------+
762
+ | Mechanism | Notes |
763
+ +===================+===========================================+
764
+ | RSA_PKCS | No hashing. Supply your own. |
765
+ +-------------------+-------------------------------------------+
766
+ | SHA*_RSA_PKCS | SHAx message digesting. |
767
+ +-------------------+-------------------------------------------+
768
+ | RSA_PKCS_PSS | Not currently implemented. |
769
+ +-------------------+ |
770
+ | SHA*_RSA_PKCS_PSS | |
771
+ +-------------------+-------------------------------------------+
772
+ | RSA_9796 | ISO/IES 9796 RSA signing. |
773
+ +-------------------+-------------------------------------------+
774
+ | RSA_X_509 | X.509 (raw) RSA signing. |
775
+ +-------------------+-------------------------------------------+
776
+ | RSA_X9_31 | X9.31 RSA signing. |
777
+ +-------------------+-------------------------------------------+
778
+
779
+ ::
780
+
781
+ # Given a private key `private`
782
+ signature = private.sign(data)
783
+
784
+ # Given a public key `public`
785
+ assert public.verify(data, signature)
786
+
733
787
ECDSA
734
788
~~~~~
735
789
790
+ The default signing and verification mechanism for ECDSA is `ECDSA_SHA512 `.
791
+
792
+ Other mechanisms are available:
793
+
794
+ +------------+-------------------------------------------+
795
+ | Mechanism | Notes |
796
+ +============+===========================================+
797
+ | ECDSA | No hashing. Input truncated to 1024 bits. |
798
+ +------------+-------------------------------------------+
799
+ | ECDSA_SHA* | ECDSA with SHAx message digesting. |
800
+ +------------+-------------------------------------------+
801
+
802
+ ::
803
+
804
+ # Given a private key `private`
805
+ signature = private.sign(data)
806
+
807
+ # Given a public key `public`
808
+ assert public.verify(data, signature)
736
809
737
810
Wrapping/Unwrapping
738
811
-------------------
739
812
813
+ The expectation when using HSMs is that secret and private keys never leave
814
+ the secure boundary of the HSM. However, there is a use case for transmitting
815
+ secret and private keys over insecure mediums. We can do this using key
816
+ wrapping.
817
+
818
+ Key wrapping is similar to encryption and decryption except instead of turning
819
+ plaintext into crypttext it turns key objects into crypttext and vice versa.
820
+
821
+ Keys must be marked as `EXTRACTABLE ` to remove them from the HSM, even wrapped.
822
+
740
823
AES
741
824
~~~
742
825
826
+ The key we're wrapping can be any sensitive key, either a secret key or
827
+ a private key. In this example we're extracting an AES secret key:
828
+
829
+ ::
830
+
831
+ # Given two secret keys, `key1` and `key2`, we can extract an encrypted
832
+ # version of `key2`
833
+ crypttext = key1.wrap_key(key2)
834
+
835
+ Wrapping doesn't store any parameters about the keys. We must supply those
836
+ to import the key.
837
+
838
+ ::
839
+
840
+ key = key1.unwrap_key(ObjectClass.SECRET_KEY, KeyType.AES, crypttext)
841
+
743
842
RSA
744
843
~~~
745
844
845
+ The key we're wrapping can be any sensitive key, either a secret key or
846
+ a private key. In this example we're extracting an AES secret key:
847
+
848
+ ::
849
+
850
+ # Given a public key, `public`, and a secret key `key`, we can extract an
851
+ encrypted version of `key`
852
+ crypttext = public.wrap_key(key)
853
+
854
+ Wrapping doesn't store any parameters about the keys. We must supply those
855
+ to import the key.
856
+
857
+ ::
858
+
859
+ # Given a private key, `private`, matching `public` above we can decrypt
860
+ # and import `key`.
861
+ key = private.unwrap_key(ObjectClass.SECRET_KEY, KeyType.AES, crypttext)
862
+
746
863
Deriving Shared Keys
747
864
--------------------
748
865
0 commit comments