File: token_encryptor.h

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (90 lines) | stat: -rw-r--r-- 3,422 bytes parent folder | download | duplicates (3)
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
// Copyright 2013 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CHROME_BROWSER_DEVICE_IDENTITY_CHROMEOS_TOKEN_ENCRYPTOR_H_
#define CHROME_BROWSER_DEVICE_IDENTITY_CHROMEOS_TOKEN_ENCRYPTOR_H_

#include <stdint.h>

#include <array>
#include <memory>
#include <string>
#include <string_view>

namespace ash {

// Interface class for classes that encrypt and decrypt tokens using the
// system salt.
//
// This class supports two methods of encryption: the old "weak" method and the
// new method. Unfortunately neither method provides actual confidentiality
// protection and both have completely equivalent strength. Both methods rely on
// the system salt, which is stored in the clear on disk. The old method is:
//
//   AES-256-CTR(key = salt, counter = salt[0 .. 15], token)
//
// and the new method is:
//
//   counter = random_bytes(16)
//   counter || AES-256-CTR(key = salt, counter = counter, token)
//
// so in both cases there are no actual secrets involved as either key or
// counter. The only practical distinction is that the new method generates
// larger encoded values and that two separate encryptions of the same token
// will yield different encoded values.
class TokenEncryptor {
 public:
  virtual ~TokenEncryptor() = default;

  // Encrypts |token| with the system salt key (stable for the lifetime
  // of the device).  Useful to avoid storing plain text in place like
  // Local State.
  virtual std::string EncryptWithSystemSalt(std::string_view token) = 0;

  // Decrypts |token| with the system salt key (stable for the lifetime
  // of the device).
  virtual std::string DecryptWithSystemSalt(
      std::string_view encrypted_token_hex) = 0;

  // Old deprecated versions of Encrypt and Decrypt. These functions are weak
  // because they do not use a proper counter with the encryptor.
  //
  // The WeakEncrypt function will CHECK-fail if called in non-test code. No new
  // code should ever use it, the function is only kept to enable testing of
  // WeakDecrypt. The WeakDecrypt is available to allow code to read old tokens.
  virtual std::string WeakEncryptWithSystemSalt(const std::string& token) = 0;
  virtual std::string WeakDecryptWithSystemSalt(
      const std::string& encrypted_token_hex) = 0;
};

// TokenEncryptor based on the system salt from cryptohome daemon. This
// implementation is used in production.
class CryptohomeTokenEncryptor : public TokenEncryptor {
 public:
  explicit CryptohomeTokenEncryptor(const std::string& system_salt);

  CryptohomeTokenEncryptor(const CryptohomeTokenEncryptor&) = delete;
  CryptohomeTokenEncryptor& operator=(const CryptohomeTokenEncryptor&) = delete;

  ~CryptohomeTokenEncryptor() override;

  // TokenEncryptor overrides:
  std::string EncryptWithSystemSalt(std::string_view token) override;
  std::string DecryptWithSystemSalt(
      std::string_view encrypted_token_hex) override;
  std::string WeakEncryptWithSystemSalt(const std::string& token) override;
  std::string WeakDecryptWithSystemSalt(
      const std::string& encrypted_token_hex) override;

 private:
  static constexpr size_t kDerivedKeySize = 32;
  static constexpr size_t kNonceSize = 16;

  std::array<uint8_t, kDerivedKeySize> key_;
  std::array<uint8_t, kNonceSize> nonce_;
};

}  // namespace ash

#endif  // CHROME_BROWSER_DEVICE_IDENTITY_CHROMEOS_TOKEN_ENCRYPTOR_H_