@@ -6,7 +6,6 @@ use anyhow::{Context, Result};
6
6
use log:: { debug, error, info} ;
7
7
use pem_rfc7468:: LineEnding ;
8
8
use rand_core:: { impls, CryptoRng , Error as RngError , RngCore } ;
9
- use std:: collections:: HashSet ;
10
9
use std:: {
11
10
fs,
12
11
io:: { self , Write } ,
@@ -64,81 +63,11 @@ pub enum HsmError {
64
63
NotEnoughShares ,
65
64
}
66
65
67
- pub struct Alphabet {
68
- chars : Vec < char > ,
69
- }
70
-
71
- impl Default for Alphabet {
72
- fn default ( ) -> Self {
73
- Self :: new ( )
74
- }
75
- }
76
-
77
- impl Alphabet {
78
- pub fn new ( ) -> Self {
79
- let mut chars: HashSet < char > = HashSet :: new ( ) ;
80
- chars. extend ( 'a' ..='z' ) ;
81
- chars. extend ( 'A' ..='Z' ) ;
82
- chars. extend ( '0' ..='9' ) ;
83
-
84
- // Remove visually similar characters
85
- chars = & chars - & HashSet :: from ( [ 'l' , 'I' , '1' ] ) ;
86
- chars = & chars - & HashSet :: from ( [ 'B' , '8' ] ) ;
87
- chars = & chars - & HashSet :: from ( [ 'O' , '0' ] ) ;
88
-
89
- // We generate random passwords from this alphabet by getting a byte
90
- // of random data from the HSM and using this value to pick
91
- // characters from the alphabet. Our alphabet cannot be larger than
92
- // the u8::MAX or it will ignore characters after the u8::MAXth.
93
- assert ! ( usize :: from( u8 :: MAX ) > chars. len( ) ) ;
94
-
95
- Alphabet {
96
- chars : chars. into_iter ( ) . collect ( ) ,
97
- }
98
- }
99
-
100
- pub fn get_char ( & self , val : u8 ) -> Option < char > {
101
- let len = self . chars . len ( ) as u8 ;
102
- // let rand = ;
103
- // Avoid biasing results by ensuring the random values we use
104
- // are a multiple of the length of the alphabet. If they aren't
105
- // we just get another.
106
- if val < u8:: MAX - u8:: MAX % len {
107
- Some ( self . chars [ ( val % len) as usize ] )
108
- } else {
109
- None
110
- }
111
- }
112
-
113
- pub fn get_random_string (
114
- & self ,
115
- get_rand_u8 : impl Fn ( ) -> Result < u8 > ,
116
- length : usize ,
117
- ) -> Result < String > {
118
- let mut passwd = String :: with_capacity ( length + 1 ) ;
119
-
120
- for _ in 0 ..length {
121
- let char = loop {
122
- let rand = get_rand_u8 ( ) ?;
123
-
124
- if let Some ( char) = self . get_char ( rand) {
125
- break char;
126
- }
127
- } ;
128
-
129
- passwd. push ( char) ;
130
- }
131
-
132
- Ok ( passwd)
133
- }
134
- }
135
-
136
66
/// Structure holding common data used by OKS when interacting with the HSM.
137
67
pub struct Hsm {
138
68
pub client : Client ,
139
69
pub out_dir : PathBuf ,
140
70
pub state_dir : PathBuf ,
141
- pub alphabet : Alphabet ,
142
71
pub backup : bool ,
143
72
}
144
73
@@ -179,18 +108,10 @@ impl Hsm {
179
108
client,
180
109
out_dir : out_dir. to_path_buf ( ) ,
181
110
state_dir : state_dir. to_path_buf ( ) ,
182
- alphabet : Alphabet :: new ( ) ,
183
111
backup,
184
112
} )
185
113
}
186
114
187
- pub fn rand_string ( & self , length : usize ) -> Result < String > {
188
- self . alphabet . get_random_string (
189
- || Ok ( self . client . get_pseudo_random ( 1 ) ?[ 0 ] ) ,
190
- length,
191
- )
192
- }
193
-
194
115
/// Create a new wrap key, cut it up into shares, & a Feldman verifier,
195
116
/// then put the key into the YubiHSM. The shares and the verifier are then
196
117
/// returned to the caller. Generally they will then be distributed
0 commit comments