@@ -6,7 +6,7 @@ use pyo3::types::{PyAnyMethods, PyListMethods};
6
6
7
7
use crate :: buf:: CffiBuf ;
8
8
use crate :: error:: { CryptographyError , CryptographyResult } ;
9
- use crate :: { exceptions, types } ;
9
+ use crate :: exceptions;
10
10
11
11
fn check_length ( data : & [ u8 ] ) -> CryptographyResult < ( ) > {
12
12
if data. len ( ) > ( i32:: MAX as usize ) {
@@ -525,8 +525,10 @@ impl ChaCha20Poly1305 {
525
525
}
526
526
527
527
#[ staticmethod]
528
- fn generate_key ( py : pyo3:: Python < ' _ > ) -> CryptographyResult < pyo3:: Bound < ' _ , pyo3:: PyAny > > {
529
- Ok ( types:: OS_URANDOM . get ( py) ?. call1 ( ( 32 , ) ) ?)
528
+ fn generate_key (
529
+ py : pyo3:: Python < ' _ > ,
530
+ ) -> CryptographyResult < pyo3:: Bound < ' _ , pyo3:: types:: PyBytes > > {
531
+ crate :: backend:: rand:: get_rand_bytes ( py, 32 )
530
532
}
531
533
532
534
#[ pyo3( signature = ( nonce, data, associated_data) ) ]
@@ -639,14 +641,14 @@ impl AesGcm {
639
641
fn generate_key (
640
642
py : pyo3:: Python < ' _ > ,
641
643
bit_length : usize ,
642
- ) -> CryptographyResult < pyo3:: Bound < ' _ , pyo3:: PyAny > > {
644
+ ) -> CryptographyResult < pyo3:: Bound < ' _ , pyo3:: types :: PyBytes > > {
643
645
if bit_length != 128 && bit_length != 192 && bit_length != 256 {
644
646
return Err ( CryptographyError :: from (
645
647
pyo3:: exceptions:: PyValueError :: new_err ( "bit_length must be 128, 192, or 256" ) ,
646
648
) ) ;
647
649
}
648
650
649
- Ok ( types :: OS_URANDOM . get ( py) ? . call1 ( ( bit_length / 8 , ) ) ? )
651
+ crate :: backend :: rand :: get_rand_bytes ( py, bit_length / 8 )
650
652
}
651
653
652
654
#[ pyo3( signature = ( nonce, data, associated_data) ) ]
@@ -755,14 +757,13 @@ impl AesCcm {
755
757
fn generate_key (
756
758
py : pyo3:: Python < ' _ > ,
757
759
bit_length : usize ,
758
- ) -> CryptographyResult < pyo3:: Bound < ' _ , pyo3:: PyAny > > {
760
+ ) -> CryptographyResult < pyo3:: Bound < ' _ , pyo3:: types :: PyBytes > > {
759
761
if bit_length != 128 && bit_length != 192 && bit_length != 256 {
760
762
return Err ( CryptographyError :: from (
761
763
pyo3:: exceptions:: PyValueError :: new_err ( "bit_length must be 128, 192, or 256" ) ,
762
764
) ) ;
763
765
}
764
-
765
- Ok ( types:: OS_URANDOM . get ( py) ?. call1 ( ( bit_length / 8 , ) ) ?)
766
+ crate :: backend:: rand:: get_rand_bytes ( py, bit_length / 8 )
766
767
}
767
768
768
769
#[ pyo3( signature = ( nonce, data, associated_data) ) ]
@@ -891,14 +892,14 @@ impl AesSiv {
891
892
fn generate_key (
892
893
py : pyo3:: Python < ' _ > ,
893
894
bit_length : usize ,
894
- ) -> CryptographyResult < pyo3:: Bound < ' _ , pyo3:: PyAny > > {
895
+ ) -> CryptographyResult < pyo3:: Bound < ' _ , pyo3:: types :: PyBytes > > {
895
896
if bit_length != 256 && bit_length != 384 && bit_length != 512 {
896
897
return Err ( CryptographyError :: from (
897
898
pyo3:: exceptions:: PyValueError :: new_err ( "bit_length must be 256, 384, or 512" ) ,
898
899
) ) ;
899
900
}
900
901
901
- Ok ( types :: OS_URANDOM . get ( py) ? . call1 ( ( bit_length / 8 , ) ) ? )
902
+ crate :: backend :: rand :: get_rand_bytes ( py, bit_length / 8 )
902
903
}
903
904
904
905
#[ pyo3( signature = ( data, associated_data) ) ]
@@ -989,14 +990,14 @@ impl AesOcb3 {
989
990
fn generate_key (
990
991
py : pyo3:: Python < ' _ > ,
991
992
bit_length : usize ,
992
- ) -> CryptographyResult < pyo3:: Bound < ' _ , pyo3:: PyAny > > {
993
+ ) -> CryptographyResult < pyo3:: Bound < ' _ , pyo3:: types :: PyBytes > > {
993
994
if bit_length != 128 && bit_length != 192 && bit_length != 256 {
994
995
return Err ( CryptographyError :: from (
995
996
pyo3:: exceptions:: PyValueError :: new_err ( "bit_length must be 128, 192, or 256" ) ,
996
997
) ) ;
997
998
}
998
999
999
- Ok ( types :: OS_URANDOM . get ( py) ? . call1 ( ( bit_length / 8 , ) ) ? )
1000
+ crate :: backend :: rand :: get_rand_bytes ( py, bit_length / 8 )
1000
1001
}
1001
1002
1002
1003
#[ pyo3( signature = ( nonce, data, associated_data) ) ]
@@ -1116,14 +1117,14 @@ impl AesGcmSiv {
1116
1117
fn generate_key (
1117
1118
py : pyo3:: Python < ' _ > ,
1118
1119
bit_length : usize ,
1119
- ) -> CryptographyResult < pyo3:: Bound < ' _ , pyo3:: PyAny > > {
1120
+ ) -> CryptographyResult < pyo3:: Bound < ' _ , pyo3:: types :: PyBytes > > {
1120
1121
if bit_length != 128 && bit_length != 192 && bit_length != 256 {
1121
1122
return Err ( CryptographyError :: from (
1122
1123
pyo3:: exceptions:: PyValueError :: new_err ( "bit_length must be 128, 192, or 256" ) ,
1123
1124
) ) ;
1124
1125
}
1125
1126
1126
- Ok ( types :: OS_URANDOM . get ( py) ? . call1 ( ( bit_length / 8 , ) ) ? )
1127
+ crate :: backend :: rand :: get_rand_bytes ( py, bit_length / 8 )
1127
1128
}
1128
1129
1129
1130
#[ pyo3( signature = ( nonce, data, associated_data) ) ]
0 commit comments