kryoptic-lib 1.5.2

A PKCS #11 software token written in Rust
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
// Copyright 2023-2026 Simo Sorce
// See LICENSE.txt file for terms

use std::fmt::Debug;
use std::sync::LazyLock;

use crate::attribute::{Attribute, CkAttrs};
use crate::error::Result;
use crate::hash::{HASH_LEN_SHA1, HASH_LEN_SHA256, HASH_LEN_SHA512};
use crate::mechanism::{
    Mac, MechOperation, Mechanism, Mechanisms, Sign, Verify,
};
use crate::native::hmac::HMACOperation;
use crate::object::factory::{
    attr_element, OAFlags, ObjectFactories, ObjectFactory, ObjectFactoryData,
};
use crate::object::key::{
    default_key_attributes, default_secret_key_generate, KeyFactory,
    SecretKeyFactory,
};
use crate::object::otp::OTPKeyFactory;
use crate::object::{Object, ObjectType};
use crate::pkcs11::*;

/// Object that holds HOTP Mechanisms
pub(crate) static HOTP_MECHS: LazyLock<[Box<dyn Mechanism>; 2]> =
    LazyLock::new(|| {
        [
            Box::new(HOTPKeyMechanism::new()),
            Box::new(HOTPMechanism::new()),
        ]
    });

/// The HOTP Key Factory facility.
static HOTP_KEY_FACTORY: LazyLock<Box<dyn ObjectFactory>> =
    LazyLock::new(|| Box::new(HOTPKeyFactory::new()));

/// Registers all implemented HOTP Mechanisms and Factories
pub fn register(mechs: &mut Mechanisms, ot: &mut ObjectFactories) {
    mechs.add_mechanism(CKM_HOTP_KEY_GEN, &(*HOTP_MECHS)[0]);
    mechs.add_mechanism(CKM_HOTP, &(*HOTP_MECHS)[1]);

    ot.add_factory(
        ObjectType::new(CKO_OTP_KEY, CKK_HOTP),
        &(*HOTP_KEY_FACTORY),
    );
}

/// This is a specialized factory for objects of class CKO_OTP_KEY
/// and CKA_KEY_TYPE of value CKK_HOTP
///
/// [HOTP secret key objects](https://docs.oasis-open.org/pkcs11/pkcs11-spec/v3.2/pkcs11-spec-v3.2.html#_Toc195693654)
#[derive(Debug)]
pub struct HOTPKeyFactory {
    data: ObjectFactoryData,
}

impl HOTPKeyFactory {
    /// Initializes a new HOTPKeyFactory object
    pub fn new() -> HOTPKeyFactory {
        let mut factory = HOTPKeyFactory {
            data: ObjectFactoryData::new(CKO_OTP_KEY),
        };

        factory.add_common_otp_key_attrs();

        let attributes = factory.data.get_attributes_mut();

        attributes.push(attr_element!(
            CKA_VALUE; OAFlags::Sensitive | OAFlags::RequiredOnCreate
            | OAFlags::SettableOnlyOnCreate; Attribute::from_bytes;
            val Vec::new()));
        attributes.push(attr_element!(
            CKA_VALUE_LEN; OAFlags::Defval; Attribute::from_ulong;
            val 20));

        /* default to true CKA_PRIVATE, CKA_SIGN, CKA_VERIFY */
        let private = attr_element!(
            CKA_PRIVATE; OAFlags::Defval | OAFlags::ChangeOnCopy; Attribute::from_bool; val true);
        match attributes.iter().position(|x| x.get_type() == CKA_PRIVATE) {
            Some(idx) => attributes[idx] = private,
            None => attributes.push(private),
        }
        let sign = attr_element!(
            CKA_SIGN; OAFlags::Defval; Attribute::from_bool; val true);
        match attributes.iter().position(|x| x.get_type() == CKA_SIGN) {
            Some(idx) => attributes[idx] = sign,
            None => attributes.push(sign),
        }
        let verify = attr_element!(
            CKA_VERIFY; OAFlags::Defval; Attribute::from_bool; val true);
        match attributes.iter().position(|x| x.get_type() == CKA_VERIFY) {
            Some(idx) => attributes[idx] = verify,
            None => attributes.push(verify),
        }

        /* override CKA_OTP_COUNTER to have 8 bytes of 0s as defval */
        let counter = attr_element!(
            CKA_OTP_COUNTER; OAFlags::Defval | OAFlags::SettableOnlyOnCreate; Attribute::from_bytes;
            val vec![0; 8]);
        match attributes
            .iter()
            .position(|x| x.get_type() == CKA_OTP_COUNTER)
        {
            Some(idx) => attributes[idx] = counter,
            None => attributes.push(counter),
        }

        /* override CKA_OTP_FORMAT to be settable only on creation */
        let format = attr_element!(
            CKA_OTP_FORMAT; OAFlags::Defval | OAFlags::SettableOnlyOnCreate; Attribute::from_ulong;
            val CK_OTP_FORMAT_DECIMAL);
        match attributes
            .iter()
            .position(|x| x.get_type() == CKA_OTP_FORMAT)
        {
            Some(idx) => attributes[idx] = format,
            None => attributes.push(format),
        }

        /* override CKA_OTP_USER_FRIENDLY_MODE to be true by default */
        let user_friendly = attr_element!(
            CKA_OTP_USER_FRIENDLY_MODE; OAFlags::Defval; Attribute::from_bool;
            val true);
        match attributes
            .iter()
            .position(|x| x.get_type() == CKA_OTP_USER_FRIENDLY_MODE)
        {
            Some(idx) => attributes[idx] = user_friendly,
            None => attributes.push(user_friendly),
        }

        factory.data.finalize();

        factory
    }

    pub fn validate_object(&self, obj: &mut Object) -> Result<()> {
        obj.ensure_ulong(CKA_CLASS, CKO_OTP_KEY)
            .map_err(|_| CKR_TEMPLATE_INCONSISTENT)?;
        obj.ensure_ulong(CKA_KEY_TYPE, CKK_HOTP)
            .map_err(|_| CKR_TEMPLATE_INCONSISTENT)?;

        for attr in [CKA_ENCRYPT, CKA_DECRYPT, CKA_WRAP, CKA_UNWRAP] {
            if let Ok(true) = obj.get_attr_as_bool(attr) {
                return Err(CKR_ATTRIBUTE_VALUE_INVALID)?;
            }
        }

        let set_mech = match obj.get_attr_as_bytes(CKA_ALLOWED_MECHANISMS) {
            Ok(mechs) => {
                if mechs.is_empty() {
                    true
                } else if mechs != CKM_HOTP.to_ne_bytes().as_slice() {
                    return Err(CKR_ATTRIBUTE_VALUE_INVALID)?;
                } else {
                    false
                }
            }
            Err(_) => true,
        };
        if set_mech {
            obj.set_attr(Attribute::from_bytes(
                CKA_ALLOWED_MECHANISMS,
                CKM_HOTP.to_ne_bytes().to_vec(),
            ))?;
        }

        if let Ok(c) = obj.get_attr_as_bytes(CKA_OTP_COUNTER) {
            if c.len() != 8 {
                return Err(CKR_ATTRIBUTE_VALUE_INVALID)?;
            }
        }

        if let Ok(format) = obj.get_attr_as_ulong(CKA_OTP_FORMAT) {
            if format != CK_OTP_FORMAT_DECIMAL {
                return Err(CKR_ATTRIBUTE_VALUE_INVALID)?;
            }
        }

        if let Ok(len) = obj.get_attr_as_ulong(CKA_VALUE_LEN) {
            if len != 20 && len != 32 && len != 64 {
                return Err(CKR_ATTRIBUTE_VALUE_INVALID)?;
            }
        }

        if let Ok(otp_len) = obj.get_attr_as_ulong(CKA_OTP_LENGTH) {
            if otp_len < 6 || otp_len > 8 {
                return Err(CKR_ATTRIBUTE_VALUE_INVALID)?;
            }
        }

        if let Ok(time) = obj.get_attr_as_string(CKA_OTP_TIME) {
            if !time.is_empty() {
                return Err(CKR_ATTRIBUTE_VALUE_INVALID)?;
            }
        }

        if let Ok(req) = obj.get_attr_as_ulong(CKA_OTP_TIME_REQUIREMENT) {
            if req != CK_OTP_PARAM_IGNORED {
                return Err(CKR_ATTRIBUTE_VALUE_INVALID)?;
            }
        }

        if let Ok(req) = obj.get_attr_as_ulong(CKA_OTP_CHALLENGE_REQUIREMENT) {
            if req != CK_OTP_PARAM_IGNORED {
                return Err(CKR_ATTRIBUTE_VALUE_INVALID)?;
            }
        }

        if let Ok(req) = obj.get_attr_as_ulong(CKA_OTP_PIN_REQUIREMENT) {
            if req != CK_OTP_PARAM_IGNORED {
                return Err(CKR_ATTRIBUTE_VALUE_INVALID)?;
            }
        }

        Ok(())
    }
}

impl ObjectFactory for HOTPKeyFactory {
    fn create(&self, template: &[CK_ATTRIBUTE]) -> Result<Object> {
        let mut obj = self.key_create(template)?;
        let len = self.get_key_buffer_len(&obj)?;
        if len == 0 {
            return Err(CKR_ATTRIBUTE_VALUE_INVALID)?;
        }
        /* By default CKA_VALUE_LEN is set to 20 by the object's factory,
         * ensure we set it to the actual buffer value passed in via
         * template instead */
        obj.set_attr(Attribute::from_ulong(CKA_VALUE_LEN, len as CK_ULONG))?;

        self.validate_object(&mut obj)?;

        Ok(obj)
    }

    fn get_data(&self) -> &ObjectFactoryData {
        &self.data
    }
    fn get_data_mut(&mut self) -> &mut ObjectFactoryData {
        &mut self.data
    }

    fn as_key_factory(&self) -> Result<&dyn KeyFactory> {
        Ok(self)
    }
    fn as_secret_key_factory(&self) -> Result<&dyn SecretKeyFactory> {
        Ok(self)
    }
}

impl KeyFactory for HOTPKeyFactory {
    fn export_for_wrapping(&self, key: &Object) -> Result<Vec<u8>> {
        SecretKeyFactory::default_export_for_wrapping(self, key)
    }

    fn import_from_wrapped(
        &self,
        data: Vec<u8>,
        template: &[CK_ATTRIBUTE],
    ) -> Result<Object> {
        SecretKeyFactory::default_import_from_wrapped(self, data, template)
    }
}

impl SecretKeyFactory for HOTPKeyFactory {
    fn recommend_key_size(&self, _default: usize) -> Result<usize> {
        Ok(HASH_LEN_SHA256)
    }
}

impl OTPKeyFactory for HOTPKeyFactory {}

const HOTP_MIN_KEY_SIZE: CK_ULONG = HASH_LEN_SHA1 as CK_ULONG;
const HOTP_MAX_KEY_SIZE: CK_ULONG = HASH_LEN_SHA512 as CK_ULONG;

/// Generic reusable object to represent mechanisms associated
/// with HOTP key objects
#[derive(Debug)]
pub struct HOTPKeyMechanism {
    info: CK_MECHANISM_INFO,
}

impl HOTPKeyMechanism {
    /// Instantiates a mechanism info for HOTP key type
    pub fn new() -> HOTPKeyMechanism {
        HOTPKeyMechanism {
            info: CK_MECHANISM_INFO {
                ulMinKeySize: HOTP_MIN_KEY_SIZE,
                ulMaxKeySize: HOTP_MAX_KEY_SIZE,
                flags: CKF_GENERATE,
            },
        }
    }
}

impl Mechanism for HOTPKeyMechanism {
    fn info(&self) -> &CK_MECHANISM_INFO {
        &self.info
    }

    fn generate_key(
        &self,
        mech: &CK_MECHANISM,
        template: &[CK_ATTRIBUTE],
        _: &Mechanisms,
        _: &ObjectFactories,
    ) -> Result<Object> {
        let factory = HOTPKeyFactory::new();
        let mut key = factory.key_generate(template)?;

        factory.validate_object(&mut key)?;

        default_secret_key_generate(&mut key)?;
        default_key_attributes(&mut key, mech.mechanism)?;

        if key.get_attr_as_bytes(CKA_OTP_COUNTER).is_err() {
            key.set_attr(Attribute::from_bytes(CKA_OTP_COUNTER, vec![0; 8]))?;
        }

        Ok(key)
    }
}

/// Generic reusable object to represent HOTP signature and verification mechanism
#[derive(Debug)]
pub struct HOTPMechanism {
    info: CK_MECHANISM_INFO,
}

impl HOTPMechanism {
    /// Instantiates a mechanism info for HOTP mechanism
    pub fn new() -> HOTPMechanism {
        HOTPMechanism {
            info: CK_MECHANISM_INFO {
                ulMinKeySize: HOTP_MIN_KEY_SIZE,
                ulMaxKeySize: HOTP_MAX_KEY_SIZE,
                flags: CKF_SIGN | CKF_VERIFY,
            },
        }
    }
}

impl Mechanism for HOTPMechanism {
    fn info(&self) -> &CK_MECHANISM_INFO {
        &self.info
    }

    fn sign_new(
        &self,
        mech: &CK_MECHANISM,
        key: &Object,
    ) -> Result<Box<dyn Sign>> {
        if key.get_attr_as_ulong(CKA_CLASS)? != CKO_OTP_KEY
            || key.get_attr_as_ulong(CKA_KEY_TYPE)? != CKK_HOTP
        {
            return Err(CKR_KEY_TYPE_INCONSISTENT)?;
        }
        let can_sign = key
            .get_attr_as_bool(CKA_SIGN)
            .map_err(|_| CKR_GENERAL_ERROR)?;
        if !can_sign {
            return Err(CKR_KEY_FUNCTION_NOT_PERMITTED)?;
        }
        Ok(Box::new(HOTPOperation::new(mech, key, true)?))
    }

    fn verify_new(
        &self,
        mech: &CK_MECHANISM,
        key: &Object,
    ) -> Result<Box<dyn Verify>> {
        if key.get_attr_as_ulong(CKA_CLASS)? != CKO_OTP_KEY
            || key.get_attr_as_ulong(CKA_KEY_TYPE)? != CKK_HOTP
        {
            return Err(CKR_KEY_TYPE_INCONSISTENT)?;
        }
        let can_verify = key
            .get_attr_as_bool(CKA_VERIFY)
            .map_err(|_| CKR_GENERAL_ERROR)?;
        if !can_verify {
            return Err(CKR_KEY_FUNCTION_NOT_PERMITTED)?;
        }
        Ok(Box::new(HOTPOperation::new(mech, key, false)?))
    }
}

#[derive(Debug)]
pub struct HOTPOperation {
    mech_type: CK_MECHANISM_TYPE,
    hmac: HMACOperation,
    counter: [u8; 8],
    key_handle: CK_OBJECT_HANDLE,
    length: usize,
    mac_len: usize,
    finalized: bool,
    update: Option<bool>,
}

impl HOTPOperation {
    pub fn new(
        mech: &CK_MECHANISM,
        key: &Object,
        _is_sign: bool,
    ) -> Result<Self> {
        let mut override_counter = None;
        let update: Option<bool>;

        if mech.ulParameterLen != 0 && !mech.pParameter.is_null() {
            if mech.ulParameterLen as usize
                != std::mem::size_of::<CK_OTP_PARAMS>()
            {
                return Err(CKR_MECHANISM_PARAM_INVALID)?;
            }
            let params = mech.get_parameters::<CK_OTP_PARAMS>()?;
            if params.ulCount > 0 && params.pParams.is_null() {
                return Err(CKR_MECHANISM_PARAM_INVALID)?;
            }
            for p in params.to_slice()? {
                match p.type_ {
                    CK_OTP_FLAGS => {
                        let flags = p.to_ulong()?;
                        if (flags & !CKF_USER_FRIENDLY_OTP) != 0 {
                            return Err(CKR_MECHANISM_PARAM_INVALID)?;
                        }
                    }
                    CK_OTP_COUNTER => override_counter = Some(p.to_buf()?),
                    _ => return Err(CKR_MECHANISM_PARAM_INVALID)?,
                }
            }
        }

        let req = key
            .get_attr_as_ulong(CKA_OTP_COUNTER_REQUIREMENT)
            .map_err(|_| CKR_GENERAL_ERROR)?;

        let c_vec = if let Some(oc) = override_counter {
            if req == CK_OTP_PARAM_OPTIONAL || req == CK_OTP_PARAM_MANDATORY {
                update = None;
                oc
            } else {
                update = Some(false);
                key.get_attr_as_bytes(CKA_OTP_COUNTER)
                    .map_err(|_| CKR_GENERAL_ERROR)?
                    .to_vec()
            }
        } else {
            if req == CK_OTP_PARAM_MANDATORY {
                return Err(CKR_MECHANISM_PARAM_INVALID)?;
            }
            update = Some(false);
            key.get_attr_as_bytes(CKA_OTP_COUNTER)
                .map_err(|_| CKR_GENERAL_ERROR)?
                .to_vec()
        };

        if c_vec.len() != 8 {
            return Err(CKR_GENERAL_ERROR)?;
        }
        let mut counter = [0u8; 8];
        counter.copy_from_slice(&c_vec);

        let length = key
            .get_attr_as_ulong(CKA_OTP_LENGTH)
            .map_err(|_| CKR_GENERAL_ERROR)? as usize;

        let key_val = key.get_attr_as_bytes(CKA_VALUE)?.clone();
        let (hmac_mech, mac_len) = match key_val.len() {
            HASH_LEN_SHA1 => (CKM_SHA_1_HMAC, HASH_LEN_SHA1),
            HASH_LEN_SHA256 => (CKM_SHA256_HMAC, HASH_LEN_SHA256),
            HASH_LEN_SHA512 => (CKM_SHA512_HMAC, HASH_LEN_SHA512),
            _ => return Err(CKR_GENERAL_ERROR)?,
        };
        let hmac = HMACOperation::internal(hmac_mech, key_val, mac_len)?;

        Ok(HOTPOperation {
            mech_type: mech.mechanism,
            hmac,
            counter,
            key_handle: key.get_handle(),
            length,
            mac_len,
            finalized: false,
            update: update,
        })
    }

    fn generate_otp(
        &mut self,
        counter: &[u8; 8],
        output: &mut [u8],
    ) -> Result<()> {
        if output.len() != self.length {
            return Err(CKR_GENERAL_ERROR)?;
        }

        let mut mac = [0u8; HOTP_MAX_KEY_SIZE as usize];
        self.hmac.mac(counter, &mut mac[0..self.mac_len])?;

        let offset = (mac[self.mac_len - 1] & 0x0f) as usize;
        mac[offset] &= 0x7f;
        let value = u32::from_be_bytes(mac[offset..(offset + 4)].try_into()?);
        let otp = value
            % match self.length {
                6 => 1000000,
                7 => 10000000,
                8 => 100000000,
                _ => return Err(CKR_GENERAL_ERROR)?,
            };
        let otp_str = format!("{:0width$}", otp, width = self.length);
        output.copy_from_slice(otp_str.as_bytes());
        Ok(())
    }
}

impl MechOperation for HOTPOperation {
    fn mechanism(&self) -> Result<CK_MECHANISM_TYPE> {
        Ok(self.mech_type)
    }

    fn finalized(&self) -> bool {
        self.finalized
    }
}

impl Sign for HOTPOperation {
    fn sign(&mut self, data: &[u8], signature: &mut [u8]) -> Result<()> {
        if data.len() != 0 {
            return Err(CKR_DATA_INVALID)?;
        }
        self.sign_final(signature)
    }

    fn sign_update(&mut self, _data: &[u8]) -> Result<()> {
        Err(CKR_DATA_INVALID)?
    }

    fn sign_final(&mut self, signature: &mut [u8]) -> Result<()> {
        if self.finalized {
            return Err(CKR_OPERATION_NOT_INITIALIZED)?;
        }

        let struct_size = std::mem::size_of::<CK_OTP_SIGNATURE_INFO>();
        let param_size = std::mem::size_of::<CK_OTP_PARAM>();
        let total_size = struct_size + param_size + self.length;

        if signature.len() < total_size {
            return Err(CKR_BUFFER_TOO_SMALL)?;
        }

        self.finalized = true;

        let mut otp = vec![0u8; self.length];
        let counter = self.counter;
        self.generate_otp(&counter, &mut otp)?;

        unsafe {
            let sig_info = signature.as_mut_ptr() as *mut CK_OTP_SIGNATURE_INFO;
            let p_params =
                signature.as_mut_ptr().add(struct_size) as *mut CK_OTP_PARAM;
            let p_value = signature.as_mut_ptr().add(struct_size + param_size);

            std::ptr::write_unaligned(&mut (*sig_info).pParams, p_params);
            std::ptr::write_unaligned(&mut (*sig_info).ulCount, 1);

            std::ptr::write_unaligned(&mut (*p_params).type_, CK_OTP_VALUE);
            std::ptr::write_unaligned(
                &mut (*p_params).pValue,
                p_value as *mut std::ffi::c_void,
            );
            std::ptr::write_unaligned(
                &mut (*p_params).ulValueLen,
                self.length as CK_ULONG,
            );

            std::ptr::copy_nonoverlapping(otp.as_ptr(), p_value, self.length);
        }

        if self.update.is_some() {
            let mut c_val = u64::from_be_bytes(self.counter);
            c_val += 1;
            self.counter = c_val.to_be_bytes();
            self.update = Some(true);
        }

        Ok(())
    }

    fn signature_len(&self) -> Result<usize> {
        let struct_size = std::mem::size_of::<CK_OTP_SIGNATURE_INFO>();
        let param_size = std::mem::size_of::<CK_OTP_PARAM>();
        Ok(struct_size + param_size + self.length)
    }

    fn updates_object(&self) -> Option<(CK_OBJECT_HANDLE, CkAttrs<'_>)> {
        if self.update == Some(true) {
            let mut attrs = CkAttrs::new();
            if attrs
                .add_owned_slice(CKA_OTP_COUNTER, &self.counter)
                .is_ok()
            {
                return Some((self.key_handle, attrs));
            }
        }
        None
    }
}

impl Verify for HOTPOperation {
    fn verify(&mut self, data: &[u8], signature: &[u8]) -> Result<()> {
        if data.len() != 0 {
            return Err(CKR_DATA_INVALID)?;
        }
        self.verify_final(signature)
    }

    fn verify_update(&mut self, _data: &[u8]) -> Result<()> {
        Err(CKR_DATA_INVALID)?
    }

    fn verify_final(&mut self, signature: &[u8]) -> Result<()> {
        if self.finalized {
            return Err(CKR_OPERATION_NOT_INITIALIZED)?;
        }
        self.finalized = true;

        if signature.len() != self.length {
            return Err(CKR_SIGNATURE_LEN_RANGE)?;
        }

        let mut c_val = u64::from_be_bytes(self.counter);
        for i in 0..5 {
            if i > 0 {
                self.hmac.reset()?;
            }
            let current_counter = c_val.to_be_bytes();
            let mut otp = vec![0u8; self.length];
            self.generate_otp(&current_counter, &mut otp)?;
            if signature == otp {
                if self.update.is_some() {
                    self.counter = (c_val + 1).to_be_bytes();
                    self.update = Some(true);
                }
                return Ok(());
            }
            c_val += 1;
        }
        Err(CKR_SIGNATURE_INVALID)?
    }

    fn signature_len(&self) -> Result<usize> {
        Ok(self.length)
    }

    fn updates_object(&self) -> Option<(CK_OBJECT_HANDLE, CkAttrs<'_>)> {
        if self.update == Some(true) {
            let mut attrs = CkAttrs::new();
            if attrs
                .add_owned_slice(CKA_OTP_COUNTER, &self.counter)
                .is_ok()
            {
                return Some((self.key_handle, attrs));
            }
        }
        None
    }
}