1*e7b1675dSTing-Kang Chang // Copyright 2022 Google LLC 2*e7b1675dSTing-Kang Chang // 3*e7b1675dSTing-Kang Chang // Licensed under the Apache License, Version 2.0 (the "License"); 4*e7b1675dSTing-Kang Chang // you may not use this file except in compliance with the License. 5*e7b1675dSTing-Kang Chang // You may obtain a copy of the License at 6*e7b1675dSTing-Kang Chang // 7*e7b1675dSTing-Kang Chang // http://www.apache.org/licenses/LICENSE-2.0 8*e7b1675dSTing-Kang Chang // 9*e7b1675dSTing-Kang Chang // Unless required by applicable law or agreed to in writing, software 10*e7b1675dSTing-Kang Chang // distributed under the License is distributed on an "AS IS" BASIS, 11*e7b1675dSTing-Kang Chang // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12*e7b1675dSTing-Kang Chang // See the License for the specific language governing permissions and 13*e7b1675dSTing-Kang Chang // limitations under the License. 14*e7b1675dSTing-Kang Chang // 15*e7b1675dSTing-Kang Chang //////////////////////////////////////////////////////////////////////////////// 16*e7b1675dSTing-Kang Chang 17*e7b1675dSTing-Kang Chang #ifndef TINK_KEY_H_ 18*e7b1675dSTing-Kang Chang #define TINK_KEY_H_ 19*e7b1675dSTing-Kang Chang 20*e7b1675dSTing-Kang Chang #include "absl/types/optional.h" 21*e7b1675dSTing-Kang Chang #include "tink/parameters.h" 22*e7b1675dSTing-Kang Chang 23*e7b1675dSTing-Kang Chang namespace crypto { 24*e7b1675dSTing-Kang Chang namespace tink { 25*e7b1675dSTing-Kang Chang 26*e7b1675dSTing-Kang Chang // Represents a cryptographic function. 27*e7b1675dSTing-Kang Chang // 28*e7b1675dSTing-Kang Chang // In Tink, `Key` objects represent cryptographic functions. For example, a 29*e7b1675dSTing-Kang Chang // `MacKey` represents the two functions: `computeMac()` and `verifyMac()`. 30*e7b1675dSTing-Kang Chang // The function `computeMac()` maps a byte sequence (possibly with additional 31*e7b1675dSTing-Kang Chang // randomness) to another byte sequence, called the tag. The function 32*e7b1675dSTing-Kang Chang // `verifyMac()` verifies the tag. A subclass `HmacKey` would contain all the 33*e7b1675dSTing-Kang Chang // information needed to properly compute an HMAC (e.g., including the hash 34*e7b1675dSTing-Kang Chang // function and tag length used). 35*e7b1675dSTing-Kang Chang // 36*e7b1675dSTing-Kang Chang // `Key` objects are lightweight, meaning they should have almost no 37*e7b1675dSTing-Kang Chang // dependencies except what is needed to represent the function. This allows 38*e7b1675dSTing-Kang Chang // `Key` objects to be used in contexts where dependencies need to be kept to a 39*e7b1675dSTing-Kang Chang // minimum. 40*e7b1675dSTing-Kang Chang class Key { 41*e7b1675dSTing-Kang Chang public: 42*e7b1675dSTing-Kang Chang // Returns a `Parameters` object containing all the information about the key 43*e7b1675dSTing-Kang Chang // that is not randomly chosen. 44*e7b1675dSTing-Kang Chang // 45*e7b1675dSTing-Kang Chang // Implementations should ensure that 'GetParameters().HasIdRequirement()` 46*e7b1675dSTing-Kang Chang // returns true if and only if `GetIdRequirement()` has a non-empty value. 47*e7b1675dSTing-Kang Chang virtual const Parameters& GetParameters() const = 0; 48*e7b1675dSTing-Kang Chang 49*e7b1675dSTing-Kang Chang // Returns the required id if this key has an id requirement. Otherwise, 50*e7b1675dSTing-Kang Chang // returns an empty value if the key can have an arbitrary id. 51*e7b1675dSTing-Kang Chang // 52*e7b1675dSTing-Kang Chang // Some keys within a keyset are required to have a specific id to work 53*e7b1675dSTing-Kang Chang // properly. This comes from the fact that Tink in some cases prefixes 54*e7b1675dSTing-Kang Chang // ciphertexts or signatures with the string '0x01<id>', where the key id is 55*e7b1675dSTing-Kang Chang // encoded in big endian format (see the documentation of the key type for 56*e7b1675dSTing-Kang Chang // details). The key id provides a hint for which specific key was used to 57*e7b1675dSTing-Kang Chang // generate the ciphertext or signature. 58*e7b1675dSTing-Kang Chang virtual absl::optional<int> GetIdRequirement() const = 0; 59*e7b1675dSTing-Kang Chang 60*e7b1675dSTing-Kang Chang // Returns true if all `Key` object fields have identical values, including 61*e7b1675dSTing-Kang Chang // the bytes for the raw key material. Otherwise, returns false. 62*e7b1675dSTing-Kang Chang // 63*e7b1675dSTing-Kang Chang // NOTE: Implementations must perform equality checks in constant time. 64*e7b1675dSTing-Kang Chang virtual bool operator==(const Key& other) const = 0; 65*e7b1675dSTing-Kang Chang bool operator!=(const Key& other) const { return !(*this == other); } 66*e7b1675dSTing-Kang Chang 67*e7b1675dSTing-Kang Chang virtual ~Key() = default; 68*e7b1675dSTing-Kang Chang }; 69*e7b1675dSTing-Kang Chang 70*e7b1675dSTing-Kang Chang } // namespace tink 71*e7b1675dSTing-Kang Chang } // namespace crypto 72*e7b1675dSTing-Kang Chang 73*e7b1675dSTing-Kang Chang #endif // TINK_KEY_H_ 74