themis 0.14.0

High-level cryptographic services for storage and messaging
Documentation
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
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
// Copyright 2018 (c) rust-themis developers
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//! Cryptographic keys.
//!
//! This module contains data structures for keys supported by Themis: RSA and ECDSA key pairs.
//!
//!   - [`EcdsaKeyPair`] consists of [`EcdsaPublicKey`] and [`EcdsaPrivateKey`]
//!   - [`RsaKeyPair`] consists of [`RsaPublicKey`] and [`RsaPrivateKey`]
//!
//! There are also generic data types which can hold keys of either kind:
//!
//!   - [`KeyPair`] consists of [`PublicKey`] and [`PrivateKey`]
//!
//! `KeyPair` may hold either an `EcdsaKeyPair` or an `RsaKeyPair`. It is guaranteed to contain
//! keys of matching kind, just as individual keys are guaranteed to be of the specified kind.
//!
//! [`EcdsaKeyPair`]: struct.EcdsaKeyPair.html
//! [`EcdsaPublicKey`]: struct.EcdsaPublicKey.html
//! [`EcdsaPrivateKey`]: struct.EcdsaPrivateKey.html
//! [`RsaKeyPair`]: struct.RsaKeyPair.html
//! [`RsaPublicKey`]: struct.RsaPublicKey.html
//! [`RsaPrivateKey`]: struct.RsaPrivateKey.html
//! [`KeyPair`]: struct.KeyPair.html
//! [`PublicKey`]: struct.PublicKey.html
//! [`PrivateKey`]: struct.PrivateKey.html
//!
//! # Examples
//!
//! ## Splitting and joining
//!
//! [Key generation functions][keygen] return matching key pairs. Some APIs (like Secure Message
//! in encryption mode) require you to pass key pairs so you are ready to go. Sometimes you may
//! need the keys separately, in which case they can be easily split into public and private half:
//!
//! [keygen]: ../keygen/index.html
//!
//! ```
//! use themis::keygen::gen_ec_key_pair;
//!
//! let key_pair = gen_ec_key_pair();
//!
//! let (private, public) = key_pair.split();
//! ```
//!
//! You may join them back into a pair if you wish:
//!
//! ```
//! # use themis::keygen::gen_ec_key_pair;
//! use themis::keys::EcdsaKeyPair;
//!
//! # let key_pair = gen_ec_key_pair();
//! # let (private, public) = key_pair.split();
//! let key_pair = EcdsaKeyPair::join(private, public);
//! ```
//!
//! Joining is a zero-cost and error-free operation for concrete key kinds (RSA or ECDSA).
//! However, when joining generic keys one must explicitly check for kind mismatch:
//!
//! ```
//! # fn check() -> Result<(), themis::Error> {
//! use themis::keygen::{gen_ec_key_pair, gen_rsa_key_pair};
//! use themis::keys::KeyPair;
//!
//! let (private_ec, _) = gen_ec_key_pair().split();
//! let (_, public_rsa) = gen_rsa_key_pair().split();
//!
//! // This will return an Err because ECDSA private key does not match RSA public key:
//! let key_pair = KeyPair::try_join(private_ec, public_rsa)?;
//! # Ok(())
//! # }
//! #
//! # assert!(check().is_err());
//! ```
//!
//! Note that all individual keys as well as key pairs are automatically convertible into generic
//! types via the standard `From`-`Into` traits.
//!
//! ## Serializing and deserializing
//!
//! All keys can be converted into bytes slices via the standard `AsRef` trait so that you can
//! easily write them into files, send via network, pass to other Themis functions, and so on:
//!
//! ```no_run
//! # fn main() -> Result<(), std::io::Error> {
//! use std::fs::File;
//! use std::io::Write;
//!
//! use themis::keygen::gen_rsa_key_pair;
//!
//! let (private, public) = gen_rsa_key_pair().split();
//!
//! let mut file = File::create("private.key")?;
//! file.write_all(private.as_ref())?;
//! # Ok(())
//! # }
//! ```
//!
//! You can also restore the keys from raw bytes using `try_from_slice` methods. They check that
//! the byte slice indeed contains a valid Themis key of the specified kind:
//!
//! ```
//! # fn main() -> Result<(), themis::Error> {
//! use themis::keys::EcdsaPublicKey;
//!
//! # const ECDSA_PUBLIC: &[u8] = b"\x55\x45\x43\x32\x00\x00\x00\x2d\x13\x8b\xdf\x0c\x02\x1f\x09\x88\x39\xd9\x73\x3a\x84\x8f\xa8\x50\xd9\x2b\xed\x3d\x38\xcf\x1d\xd0\xce\xf4\xae\xdb\xcf\xaf\xcb\x6b\xa5\x4a\x08\x11\x21";
//! #
//! # fn receive() -> Vec<u8> {
//! #     ECDSA_PUBLIC.to_vec()
//! # }
//! #
//! // Obtain the key bytes somehow (e.g., read from file).
//! let bytes: Vec<u8> = receive();
//!
//! let public = EcdsaPublicKey::try_from_slice(&bytes)?;
//! # Ok(())
//! # }
//! ```

use std::fmt;
use std::ptr;

use bindings::{themis_gen_sym_key, themis_get_asym_key_kind, themis_is_valid_asym_key};
use zeroize::Zeroize;

use crate::error::{Error, ErrorKind, Result};
use crate::utils::into_raw_parts;

/// Key material.
#[derive(Clone, Eq, PartialEq, Hash)]
pub(crate) struct KeyBytes(Vec<u8>);

impl KeyBytes {
    /// Makes a key from an owned byte vector.
    pub fn from_vec(bytes: Vec<u8>) -> Result<KeyBytes> {
        if bytes.is_empty() {
            Err(Error::with_kind(ErrorKind::InvalidParameter))
        } else {
            Ok(KeyBytes(bytes))
        }
    }

    /// Makes a key from a copy of a byte slice.
    pub fn copy_slice(bytes: &[u8]) -> Result<KeyBytes> {
        KeyBytes::from_vec(bytes.to_vec())
    }

    /// Returns key bytes.
    pub fn as_bytes(&self) -> &[u8] {
        &self.0
    }
}

impl fmt::Debug for KeyBytes {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "KeyBytes({} bytes)", self.0.len())
    }
}

// Make sure that sensitive key material is removed from memory as soon as it is no longer needed.
impl Drop for KeyBytes {
    fn drop(&mut self) {
        self.0.zeroize();
    }
}

//
// Key type definitions
//

/// RSA private key.
#[derive(Clone, Debug, Eq, PartialEq, Hash)]
pub struct RsaPrivateKey {
    inner: KeyBytes,
}

/// RSA public key.
#[derive(Clone, Debug, Eq, PartialEq, Hash)]
pub struct RsaPublicKey {
    inner: KeyBytes,
}

/// RSA key pair.
#[derive(Clone, Debug, Eq, PartialEq, Hash)]
pub struct RsaKeyPair {
    private_key: KeyBytes,
    public_key: KeyBytes,
}

/// ECDSA private key.
#[derive(Clone, Debug, Eq, PartialEq, Hash)]
pub struct EcdsaPrivateKey {
    inner: KeyBytes,
}

/// ECDSA public key.
#[derive(Clone, Debug, Eq, PartialEq, Hash)]
pub struct EcdsaPublicKey {
    inner: KeyBytes,
}

/// ECDSA key pair.
#[derive(Clone, Debug, Eq, PartialEq, Hash)]
pub struct EcdsaKeyPair {
    private_key: KeyBytes,
    public_key: KeyBytes,
}

/// A private key.
///
/// This structure is used by cryptographic services which can support any kind of key.
/// [`RsaPrivateKey`] or [`EcdsaPrivateKey`] can be turned into a `PrivateKey` at no cost.
///
/// [`RsaPrivateKey`]: struct.RsaPrivateKey.html
/// [`EcdsaPrivateKey`]: struct.EcdsaPrivateKey.html
#[derive(Clone, Debug, Eq, PartialEq, Hash)]
pub struct PrivateKey {
    inner: KeyBytes,
}

/// A public key.
///
/// This structure is used by cryptographic services which can support any kind of key.
/// [`RsaPublicKey`] or [`EcdsaPublicKey`] can be turned into a `PublicKey` at no cost.
///
/// [`RsaPublicKey`]: struct.RsaPublicKey.html
/// [`EcdsaPublicKey`]: struct.EcdsaPublicKey.html
#[derive(Clone, Debug, Eq, PartialEq, Hash)]
pub struct PublicKey {
    inner: KeyBytes,
}

/// A pair of asymmetric keys.
///
/// This structure is used by cryptographic services which can support any kind of key pair.
/// [`RsaKeyPair`] or [`EcdsaKeyPair`] can be turned into a `KeyPair` at no cost. A pair of
/// [`PrivateKey`] and [`PublicKey`] can be joined into a `KeyPair` after a quick type check
/// if their kinds match (either RSA or ECDSA).
///
/// [`RsaKeyPair`]: struct.RsaKeyPair.html
/// [`EcdsaKeyPair`]: struct.EcdsaKeyPair.html
/// [`PrivateKey`]: struct.PrivateKey.html
/// [`PublicKey`]: struct.PublicKey.html
#[derive(Clone, Debug, Eq, PartialEq, Hash)]
pub struct KeyPair {
    private_key: KeyBytes,
    public_key: KeyBytes,
}

/// Kind of an asymmetric key.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum KeyKind {
    /// RSA private key.
    RsaPrivate,
    /// RSA public key.
    RsaPublic,
    /// ECDSA private key.
    EcdsaPrivate,
    /// ECDSA public key.
    EcdsaPublic,
}

//
// Key pairs
//

impl RsaKeyPair {
    /// Splits this key pair into private and public keys.
    pub fn split(self) -> (RsaPrivateKey, RsaPublicKey) {
        (
            RsaPrivateKey {
                inner: self.private_key,
            },
            RsaPublicKey {
                inner: self.public_key,
            },
        )
    }

    /// Joins a pair of private and public keys.
    ///
    /// Note that this method _does not_ verify that the keys match: i.e., that it is possible
    /// to use the private key to decrypt data encrypted with the public key.
    pub fn join(private_key: RsaPrivateKey, public_key: RsaPublicKey) -> RsaKeyPair {
        RsaKeyPair {
            private_key: private_key.inner,
            public_key: public_key.inner,
        }
    }
}

impl EcdsaKeyPair {
    /// Splits this key pair into private and public keys.
    pub fn split(self) -> (EcdsaPrivateKey, EcdsaPublicKey) {
        (
            EcdsaPrivateKey {
                inner: self.private_key,
            },
            EcdsaPublicKey {
                inner: self.public_key,
            },
        )
    }

    /// Joins a pair of private and public keys.
    ///
    /// Note that this method _does not_ verify that the keys match: i.e., that it is possible
    /// to use the private key to decrypt data encrypted with the public key.
    pub fn join(private_key: EcdsaPrivateKey, public_key: EcdsaPublicKey) -> EcdsaKeyPair {
        EcdsaKeyPair {
            private_key: private_key.inner,
            public_key: public_key.inner,
        }
    }
}

impl KeyPair {
    /// Access bytes of the private key.
    pub(crate) fn private_key_bytes(&self) -> &[u8] {
        self.private_key.as_bytes()
    }

    /// Access bytes of the public key.
    pub(crate) fn public_key_bytes(&self) -> &[u8] {
        self.public_key.as_bytes()
    }

    /// Splits this key pair into private and public keys.
    pub fn split(self) -> (PrivateKey, PublicKey) {
        (
            PrivateKey {
                inner: self.private_key,
            },
            PublicKey {
                inner: self.public_key,
            },
        )
    }

    /// Joins a pair of private and public keys.
    ///
    /// Note that this method _does not_ verify that the keys match: i.e., that it is possible
    /// to use the private key to decrypt data encrypted with the public key.
    ///
    /// However, it does verify that _the kinds_ of the keys match: i.e., that they are both
    /// either RSA or ECDSA keys. An error is returned if that’s not the case. You can check
    /// the kind of the key beforehand via its `kind()` method.
    pub fn try_join(
        private_key: impl Into<PrivateKey>,
        public_key: impl Into<PublicKey>,
    ) -> Result<KeyPair> {
        let (private_key, public_key) = (private_key.into(), public_key.into());
        match (private_key.kind(), public_key.kind()) {
            (KeyKind::RsaPrivate, KeyKind::RsaPublic) => {}
            (KeyKind::EcdsaPrivate, KeyKind::EcdsaPublic) => {}
            _ => {
                return Err(Error::with_kind(ErrorKind::InvalidParameter));
            }
        }
        Ok(KeyPair {
            private_key: private_key.inner,
            public_key: public_key.inner,
        })
    }
}

//
// Individual keys
//

impl RsaPrivateKey {
    /// Parses a key from a byte slice.
    ///
    /// Returns an error if the slice does not contain a valid RSA private key.
    pub fn try_from_slice(bytes: impl AsRef<[u8]>) -> Result<Self> {
        let key = KeyBytes::copy_slice(bytes.as_ref())?;
        match get_key_kind(&key)? {
            KeyKind::RsaPrivate => Ok(Self { inner: key }),
            _ => Err(Error::with_kind(ErrorKind::InvalidParameter)),
        }
    }

    /// Wraps an existing trusted byte vector into a key.
    pub(crate) fn from_vec(bytes: Vec<u8>) -> Self {
        let key = KeyBytes::from_vec(bytes).expect("invalid empty key");
        debug_assert_eq!(get_key_kind(&key), Ok(KeyKind::RsaPrivate));
        Self { inner: key }
    }
}

impl RsaPublicKey {
    /// Parses a key from a byte slice.
    ///
    /// Returns an error if the slice does not contain a valid RSA public key.
    pub fn try_from_slice(bytes: impl AsRef<[u8]>) -> Result<Self> {
        let key = KeyBytes::copy_slice(bytes.as_ref())?;
        match get_key_kind(&key)? {
            KeyKind::RsaPublic => Ok(Self { inner: key }),
            _ => Err(Error::with_kind(ErrorKind::InvalidParameter)),
        }
    }

    /// Wraps an existing trusted byte vector into a key.
    pub(crate) fn from_vec(bytes: Vec<u8>) -> Self {
        let key = KeyBytes::from_vec(bytes).expect("invalid empty key");
        debug_assert_eq!(get_key_kind(&key), Ok(KeyKind::RsaPublic));
        Self { inner: key }
    }
}

impl EcdsaPrivateKey {
    /// Parses a key from a byte slice.
    ///
    /// Returns an error if the slice does not contain a valid ECDSA private key.
    pub fn try_from_slice(bytes: impl AsRef<[u8]>) -> Result<Self> {
        let key = KeyBytes::copy_slice(bytes.as_ref())?;
        match get_key_kind(&key)? {
            KeyKind::EcdsaPrivate => Ok(Self { inner: key }),
            _ => Err(Error::with_kind(ErrorKind::InvalidParameter)),
        }
    }

    /// Wraps an existing trusted byte vector into a key.
    pub(crate) fn from_vec(bytes: Vec<u8>) -> Self {
        let key = KeyBytes::from_vec(bytes).expect("invalid empty key");
        debug_assert_eq!(get_key_kind(&key), Ok(KeyKind::EcdsaPrivate));
        Self { inner: key }
    }
}

impl EcdsaPublicKey {
    /// Parses a key from a byte slice.
    ///
    /// Returns an error if the slice does not contain a valid ECDSA public key.
    pub fn try_from_slice(bytes: impl AsRef<[u8]>) -> Result<Self> {
        let key = KeyBytes::copy_slice(bytes.as_ref())?;
        match get_key_kind(&key)? {
            KeyKind::EcdsaPublic => Ok(Self { inner: key }),
            _ => Err(Error::with_kind(ErrorKind::InvalidParameter)),
        }
    }

    /// Wraps an existing trusted byte vector into a key.
    pub(crate) fn from_vec(bytes: Vec<u8>) -> Self {
        let key = KeyBytes::from_vec(bytes).expect("invalid empty key");
        debug_assert_eq!(get_key_kind(&key), Ok(KeyKind::EcdsaPublic));
        Self { inner: key }
    }
}

impl PrivateKey {
    /// Retrieves actual kind of the stored key.
    pub fn kind(&self) -> KeyKind {
        get_key_kind_trusted(&self.inner)
    }

    /// Parses a key from a byte slice.
    ///
    /// Returns an error if the slice does not contain a valid RSA or ECDSA private key.
    pub fn try_from_slice(bytes: impl AsRef<[u8]>) -> Result<Self> {
        let key = KeyBytes::copy_slice(bytes.as_ref())?;
        match get_key_kind(&key)? {
            KeyKind::RsaPrivate => Ok(Self { inner: key }),
            KeyKind::EcdsaPrivate => Ok(Self { inner: key }),
            _ => Err(Error::with_kind(ErrorKind::InvalidParameter)),
        }
    }
}

impl PublicKey {
    /// Retrieves actual kind of the stored key.
    pub fn kind(&self) -> KeyKind {
        get_key_kind_trusted(&self.inner)
    }

    /// Parses a key from a byte slice.
    ///
    /// Returns an error if the slice does not contain a valid RSA or ECDSA public key.
    pub fn try_from_slice(bytes: impl AsRef<[u8]>) -> Result<Self> {
        let key = KeyBytes::copy_slice(bytes.as_ref())?;
        match get_key_kind(&key)? {
            KeyKind::RsaPublic => Ok(Self { inner: key }),
            KeyKind::EcdsaPublic => Ok(Self { inner: key }),
            _ => Err(Error::with_kind(ErrorKind::InvalidParameter)),
        }
    }
}

// The following functions have to be called in a particular sequence in order to be safe to use.
// That's why they are free functions, not methods of KeyBytes.
//
// You can call get_key_kind() on any byte slice. If you get an Ok result back then you can call
// get_key_kind_trusted() again on the very same byte slice to get the result faster.
//
// There's also a reason why they receive &KeyBytes, not just &[u8]. This is to maintain correct
// pointer alignment.

fn get_key_kind(key: &KeyBytes) -> Result<KeyKind> {
    is_valid_themis_key(key)?;
    try_get_key_kind(key)
}

fn get_key_kind_trusted(key: &KeyBytes) -> KeyKind {
    debug_assert!(is_valid_themis_key(key).is_ok());
    try_get_key_kind(key).expect("get_key_kind_trusted() called for invalid key")
}

fn is_valid_themis_key(key: &KeyBytes) -> Result<()> {
    let (ptr, len) = into_raw_parts(key.as_bytes());
    let status = unsafe { themis_is_valid_asym_key(ptr, len) };
    let error = Error::from_themis_status(status);
    if error.kind() != ErrorKind::Success {
        return Err(error);
    }
    Ok(())
}

fn try_get_key_kind(key: &KeyBytes) -> Result<KeyKind> {
    use bindings::themis_key_kind::*;
    let (ptr, len) = into_raw_parts(key.as_bytes());
    let kind = unsafe { themis_get_asym_key_kind(ptr, len) };
    match kind {
        THEMIS_KEY_RSA_PRIVATE => Ok(KeyKind::RsaPrivate),
        THEMIS_KEY_RSA_PUBLIC => Ok(KeyKind::RsaPublic),
        THEMIS_KEY_EC_PRIVATE => Ok(KeyKind::EcdsaPrivate),
        THEMIS_KEY_EC_PUBLIC => Ok(KeyKind::EcdsaPublic),
        THEMIS_KEY_INVALID => Err(Error::with_kind(ErrorKind::InvalidParameter)),
    }
}

//
// Symmetric keys
//

/// Symmetric encryption key.
///
/// These keys are used by [`SecureCell`] objects.
///
/// Note that managing keys is _your_ responsibility. You have to make sure that keys
/// are stored safely and are never disclosed to untrusted parties. You can consult
/// [our guidelines][key-management] for some advice on key management.
///
/// [`SecureCell`]: ../secure_cell/index.html
/// [key-management]: https://docs.cossacklabs.com/themis/crypto-theory/key-management/
///
/// # Examples
///
/// Generating a new symmetric key is trivial:
///
/// ```
/// # fn main() -> Result<(), themis::Error> {
/// use themis::keys::SymmetricKey;
/// use themis::secure_cell::SecureCell;
///
/// let key = SymmetricKey::new();
///
/// let cell = SecureCell::with_key(&key)?.seal();
///
/// let encrypted = cell.encrypt(b"message")?;
/// let decrypted = cell.decrypt(&encrypted)?;
/// assert_eq!(decrypted, b"message");
/// # Ok(())
/// # }
/// ```
///
/// Keys can be converted into a byte slice via the standard `AsRef` trait so that you can
/// easily write them into files, send via network, pass to other Themis functions, and so on:
///
/// ```no_run
/// # fn main() -> Result<(), std::io::Error> {
/// # use themis::keys::SymmetricKey;
/// # let key = SymmetricKey::new();
/// use std::fs::File;
/// use std::io::Write;
///
/// let mut file = File::create("master.key")?;
/// file.write_all(key.as_ref())?;
/// # Ok(())
/// # }
/// ```
///
/// You can also restore the keys from raw bytes using `try_from_slice` method. It checks that
/// the byte slice indeed contains a valid Themis key:
///
/// ```no_run
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
/// # use themis::keys::SymmetricKey;
/// use std::fs::File;
/// use std::io::Read;
///
/// let mut file = File::open("master.key")?;
///
/// let mut buffer = Vec::new();
/// file.read_to_end(&mut buffer)?;
///
/// let key = SymmetricKey::try_from_slice(&buffer)?;
/// # Ok(())
/// # }
/// ```
#[derive(Clone, Debug, Eq, PartialEq, Hash)]
pub struct SymmetricKey {
    inner: KeyBytes,
}

impl SymmetricKey {
    /// Generates a new symmetric key.
    ///
    /// # Panics
    ///
    /// This function may panic in case of unrecoverable errors inside the library
    /// (e.g., out-of-memory or assertion violations).
    pub fn new() -> Self {
        match Self::try_gen_sym_key() {
            Ok(key) => key,
            Err(e) => panic!("themis_gen_sym_key() failed: {}", e),
        }
    }

    /// Generates a new symmetric key.
    fn try_gen_sym_key() -> Result<Self> {
        let mut key = Vec::new();
        let mut key_len = 0;

        unsafe {
            let status = themis_gen_sym_key(ptr::null_mut(), &mut key_len);
            let error = Error::from_themis_status(status);
            if error.kind() != ErrorKind::BufferTooSmall {
                return Err(error);
            }
        }

        key.reserve(key_len);

        unsafe {
            let status = themis_gen_sym_key(key.as_mut_ptr(), &mut key_len);
            let error = Error::from_themis_status(status);
            if error.kind() != ErrorKind::Success {
                return Err(error);
            }
            debug_assert!(key_len <= key.capacity());
            key.set_len(key_len as usize);
        }

        Ok(Self {
            inner: KeyBytes::from_vec(key).expect("invalid empty key"),
        })
    }

    /// Parses a key from a byte slice.
    ///
    /// Returns an error if the slice does not contain a valid symmetric key.
    pub fn try_from_slice(bytes: impl AsRef<[u8]>) -> Result<Self> {
        Ok(Self {
            inner: KeyBytes::copy_slice(bytes.as_ref())?,
        })
    }
}

impl Default for SymmetricKey {
    fn default() -> Self {
        SymmetricKey::new()
    }
}

//
// AsRef<[u8]> casts
//

impl AsRef<[u8]> for RsaPrivateKey {
    fn as_ref(&self) -> &[u8] {
        self.inner.as_bytes()
    }
}

impl AsRef<[u8]> for RsaPublicKey {
    fn as_ref(&self) -> &[u8] {
        self.inner.as_bytes()
    }
}

impl AsRef<[u8]> for EcdsaPrivateKey {
    fn as_ref(&self) -> &[u8] {
        self.inner.as_bytes()
    }
}

impl AsRef<[u8]> for EcdsaPublicKey {
    fn as_ref(&self) -> &[u8] {
        self.inner.as_bytes()
    }
}

impl AsRef<[u8]> for PrivateKey {
    fn as_ref(&self) -> &[u8] {
        self.inner.as_bytes()
    }
}

impl AsRef<[u8]> for PublicKey {
    fn as_ref(&self) -> &[u8] {
        self.inner.as_bytes()
    }
}

impl AsRef<[u8]> for SymmetricKey {
    fn as_ref(&self) -> &[u8] {
        self.inner.as_bytes()
    }
}

//
// From/Into conversions
//

impl From<RsaPrivateKey> for PrivateKey {
    fn from(private_key: RsaPrivateKey) -> PrivateKey {
        PrivateKey {
            inner: private_key.inner,
        }
    }
}

impl From<RsaPublicKey> for PublicKey {
    fn from(public_key: RsaPublicKey) -> PublicKey {
        PublicKey {
            inner: public_key.inner,
        }
    }
}

impl From<EcdsaPrivateKey> for PrivateKey {
    fn from(private_key: EcdsaPrivateKey) -> PrivateKey {
        PrivateKey {
            inner: private_key.inner,
        }
    }
}

impl From<EcdsaPublicKey> for PublicKey {
    fn from(public_key: EcdsaPublicKey) -> PublicKey {
        PublicKey {
            inner: public_key.inner,
        }
    }
}

impl From<RsaKeyPair> for KeyPair {
    fn from(key_pair: RsaKeyPair) -> KeyPair {
        KeyPair {
            private_key: key_pair.private_key,
            public_key: key_pair.public_key,
        }
    }
}

impl From<EcdsaKeyPair> for KeyPair {
    fn from(key_pair: EcdsaKeyPair) -> KeyPair {
        KeyPair {
            private_key: key_pair.private_key,
            public_key: key_pair.public_key,
        }
    }
}