@@ -809,8 +809,173 @@ mod test {
809
809
aes_128_cbc ( cipher) ;
810
810
}
811
811
812
+ #[ cfg( not( boringssl) ) ]
813
+ #[ test]
814
+ fn default_aes_128_ccm ( ) {
815
+ // from https://csrc.nist.gov/CSRC/media/Projects/Cryptographic-Algorithm-Validation-Program/documents/mac/ccmtestvectors.zip
816
+ let cipher = Cipher :: aes_128_ccm ( ) ;
817
+ aes_ccm (
818
+ cipher,
819
+ "26511fb51fcfa75cb4b44da75a6e5a0e" ,
820
+ "ea98ec44f5a86715014783172e" ,
821
+ "4da40b80579c1d9a5309f7efecb7c059a2f914511ca5fc10" ,
822
+ "e4692b9f06b666c7451b146c8aeb07a6e30c629d28065c3dde5940325b14b810" ,
823
+ "1bf0ba0ebb20d8edba59f29a9371750c9c714078f73c335d" ,
824
+ "2f1322ac69b848b001476323aed84c47" ,
825
+ ) ;
826
+ }
827
+
828
+ #[ cfg( not( boringssl) ) ]
829
+ #[ test]
830
+ fn default_aes_192_ccm ( ) {
831
+ // from https://csrc.nist.gov/CSRC/media/Projects/Cryptographic-Algorithm-Validation-Program/documents/mac/ccmtestvectors.zip
832
+ let cipher = Cipher :: aes_192_ccm ( ) ;
833
+ aes_ccm (
834
+ cipher,
835
+ "26511fb51fcfa75cb4b44da75a6e5a0eb8d9c8f3b906f886" ,
836
+ "ea98ec44f5a86715014783172e" ,
837
+ "4da40b80579c1d9a5309f7efecb7c059a2f914511ca5fc10" ,
838
+ "e4692b9f06b666c7451b146c8aeb07a6e30c629d28065c3dde5940325b14b810" ,
839
+ "30c154c616946eccc2e241d336ad33720953e449a0e6b0f0" ,
840
+ "dbf8e9464909bdf337e48093c082a10b" ,
841
+ ) ;
842
+ }
843
+
844
+ #[ cfg( not( boringssl) ) ]
845
+ #[ test]
846
+ fn default_aes_256_ccm ( ) {
847
+ // from https://csrc.nist.gov/CSRC/media/Projects/Cryptographic-Algorithm-Validation-Program/documents/mac/ccmtestvectors.zip
848
+ let cipher = Cipher :: aes_256_ccm ( ) ;
849
+ aes_ccm (
850
+ cipher,
851
+ "314a202f836f9f257e22d8c11757832ae5131d357a72df88f3eff0ffcee0da4e" ,
852
+ "3542fbe0f59a6d5f3abf619b7d" ,
853
+ "c5b3d71312ea14f2f8fae5bd1a453192b6604a45db75c5ed" ,
854
+ "dd4531f158a2fa3bc8a339f770595048f4a42bc1b03f2e824efc6ba4985119d8" ,
855
+ "39c2e8f6edfe663b90963b98eb79e2d4f7f28a5053ae8881" ,
856
+ "567a6b4426f1667136bed4a5e32a2bc1" ,
857
+ ) ;
858
+ }
859
+
860
+ #[ cfg( not( boringssl) ) ]
861
+ fn aes_ccm (
862
+ cipher : & CipherRef ,
863
+ key : & ' static str ,
864
+ iv : & ' static str ,
865
+ pt : & ' static str ,
866
+ aad : & ' static str ,
867
+ ct : & ' static str ,
868
+ tag : & ' static str ,
869
+ ) {
870
+ let key = hex:: decode ( key) . unwrap ( ) ;
871
+ let iv = hex:: decode ( iv) . unwrap ( ) ;
872
+ let pt = hex:: decode ( pt) . unwrap ( ) ;
873
+ let ct = hex:: decode ( ct) . unwrap ( ) ;
874
+ let aad = hex:: decode ( aad) . unwrap ( ) ;
875
+ let tag = hex:: decode ( tag) . unwrap ( ) ;
876
+
877
+ let mut ctx = CipherCtx :: new ( ) . unwrap ( ) ;
878
+
879
+ ctx. encrypt_init ( Some ( cipher) , None , None ) . unwrap ( ) ;
880
+ ctx. set_iv_length ( iv. len ( ) ) . unwrap ( ) ;
881
+ ctx. set_tag_length ( tag. len ( ) ) . unwrap ( ) ;
882
+ ctx. encrypt_init ( None , Some ( & key) , Some ( & iv) ) . unwrap ( ) ;
883
+ ctx. set_data_len ( pt. len ( ) ) . unwrap ( ) ;
884
+
885
+ let mut buf = vec ! [ ] ;
886
+ ctx. cipher_update ( & aad, None ) . unwrap ( ) ;
887
+ ctx. cipher_update_vec ( & pt, & mut buf) . unwrap ( ) ;
888
+ ctx. cipher_final_vec ( & mut buf) . unwrap ( ) ;
889
+ assert_eq ! ( buf, ct) ;
890
+
891
+ let mut out_tag = vec ! [ 0u8 ; tag. len( ) ] ;
892
+ ctx. tag ( & mut out_tag) . unwrap ( ) ;
893
+ assert_eq ! ( tag, out_tag) ;
894
+
895
+ ctx. decrypt_init ( Some ( cipher) , None , None ) . unwrap ( ) ;
896
+ ctx. set_iv_length ( iv. len ( ) ) . unwrap ( ) ;
897
+ ctx. set_tag ( & tag) . unwrap ( ) ;
898
+ ctx. decrypt_init ( None , Some ( & key) , Some ( & iv) ) . unwrap ( ) ;
899
+ ctx. set_data_len ( pt. len ( ) ) . unwrap ( ) ;
900
+
901
+ let mut buf = vec ! [ ] ;
902
+ ctx. cipher_update ( & aad, None ) . unwrap ( ) ;
903
+ ctx. cipher_update_vec ( & ct, & mut buf) . unwrap ( ) ;
904
+ // Some older libraries don't support calling EVP_CipherFinal/EVP_DecryptFinal for CCM
905
+ // https://wiki.openssl.org/index.php/EVP_Authenticated_Encryption_and_Decryption#Authenticated_Decryption_using_CCM_mode
906
+ #[ cfg( any( ossl111, awslc, boringssl) ) ]
907
+ ctx. cipher_final_vec ( & mut buf) . unwrap ( ) ;
908
+
909
+ assert_eq ! ( buf, pt) ;
910
+ }
911
+
912
+ #[ cfg( not( any( boringssl, awslc) ) ) ]
913
+ #[ test]
914
+ fn default_aes_128_xts ( ) {
915
+ // https://csrc.nist.gov/CSRC/media/Projects/Cryptographic-Algorithm-Validation-Program/documents/aes/XTSTestVectors.zip
916
+ let cipher = Cipher :: aes_128_xts ( ) ;
917
+ aes_xts (
918
+ cipher,
919
+ "a1b90cba3f06ac353b2c343876081762090923026e91771815f29dab01932f2f" ,
920
+ "4faef7117cda59c66e4b92013e768ad5" ,
921
+ "ebabce95b14d3c8d6fb350390790311c" ,
922
+ "778ae8b43cb98d5a825081d5be471c63" ,
923
+ ) ;
924
+ }
925
+
926
+ #[ cfg( not( boringssl) ) ]
927
+ #[ test]
928
+ fn default_aes_256_xts ( ) {
929
+ // https://csrc.nist.gov/CSRC/media/Projects/Cryptographic-Algorithm-Validation-Program/documents/aes/XTSTestVectors.zip
930
+ let cipher = Cipher :: aes_256_xts ( ) ;
931
+ aes_xts ( cipher, "1ea661c58d943a0e4801e42f4b0947149e7f9f8e3e68d0c7505210bd311a0e7cd6e13ffdf2418d8d1911c004cda58da3d619b7e2b9141e58318eea392cf41b08" , "adf8d92627464ad2f0428e84a9f87564" , "2eedea52cd8215e1acc647e810bbc3642e87287f8d2e57e36c0a24fbc12a202e" , "cbaad0e2f6cea3f50b37f934d46a9b130b9d54f07e34f36af793e86f73c6d7db" ) ;
932
+ }
933
+
934
+ #[ cfg( not( boringssl) ) ]
935
+ fn aes_xts (
936
+ cipher : & CipherRef ,
937
+ key : & ' static str ,
938
+ i : & ' static str ,
939
+ pt : & ' static str ,
940
+ ct : & ' static str ,
941
+ ) {
942
+ let key = hex:: decode ( key) . unwrap ( ) ;
943
+ let i = hex:: decode ( i) . unwrap ( ) ;
944
+ let pt = hex:: decode ( pt) . unwrap ( ) ;
945
+ let ct = hex:: decode ( ct) . unwrap ( ) ;
946
+
947
+ let mut ctx = CipherCtx :: new ( ) . unwrap ( ) ;
948
+ ctx. encrypt_init ( Some ( cipher) , Some ( & key) , Some ( & i) )
949
+ . unwrap ( ) ;
950
+ let mut buf = vec ! [ ] ;
951
+ ctx. cipher_update_vec ( & pt, & mut buf) . unwrap ( ) ;
952
+ ctx. cipher_final_vec ( & mut buf) . unwrap ( ) ;
953
+
954
+ assert_eq ! ( ct, buf) ;
955
+
956
+ ctx. decrypt_init ( Some ( cipher) , Some ( & key) , Some ( & i) )
957
+ . unwrap ( ) ;
958
+ let mut buf = vec ! [ ] ;
959
+ ctx. cipher_update_vec ( & ct, & mut buf) . unwrap ( ) ;
960
+ ctx. cipher_final_vec ( & mut buf) . unwrap ( ) ;
961
+
962
+ assert_eq ! ( pt, buf) ;
963
+ }
964
+
812
965
#[ test]
813
966
fn test_stream_ciphers ( ) {
967
+ #[ cfg( not( boringssl) ) ]
968
+ {
969
+ test_stream_cipher ( Cipher :: aes_128_cfb1 ( ) ) ;
970
+ test_stream_cipher ( Cipher :: aes_128_cfb8 ( ) ) ;
971
+ test_stream_cipher ( Cipher :: aes_128_cfb128 ( ) ) ;
972
+ test_stream_cipher ( Cipher :: aes_192_cfb1 ( ) ) ;
973
+ test_stream_cipher ( Cipher :: aes_192_cfb8 ( ) ) ;
974
+ test_stream_cipher ( Cipher :: aes_192_cfb128 ( ) ) ;
975
+ test_stream_cipher ( Cipher :: aes_256_cfb1 ( ) ) ;
976
+ test_stream_cipher ( Cipher :: aes_256_cfb8 ( ) ) ;
977
+ test_stream_cipher ( Cipher :: aes_256_cfb128 ( ) ) ;
978
+ }
814
979
test_stream_cipher ( Cipher :: aes_192_ctr ( ) ) ;
815
980
test_stream_cipher ( Cipher :: aes_256_ctr ( ) ) ;
816
981
}
0 commit comments