1 // Copyright 2023 Google LLC 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 // 15 //////////////////////////////////////////////////////////////////////////////// 16 17 package com.google.tinkuser; 18 19 import com.google.crypto.tink.Aead; 20 import com.google.crypto.tink.BinaryKeysetReader; 21 import com.google.crypto.tink.KeysetHandle; 22 import com.google.crypto.tink.KeysetReader; 23 import com.google.crypto.tink.aead.AeadKeyTemplates; 24 import com.google.crypto.tink.daead.DeterministicAeadKeyTemplates; 25 import com.google.crypto.tink.mac.MacKeyTemplates; 26 import com.google.crypto.tink.streamingaead.StreamingAeadKeyTemplates; 27 import java.io.IOException; 28 import java.security.GeneralSecurityException; 29 30 /** Example user code */ 31 public final class TinkUser { useReadNoSecret(byte[] b)32 public Aead useReadNoSecret(byte[] b) throws GeneralSecurityException { 33 return KeysetHandle.readNoSecret(b).getPrimitive(Aead.class); 34 } useBinaryReader(byte[] b)35 public Aead useBinaryReader(byte[] b) throws GeneralSecurityException, IOException { 36 return KeysetHandle.readNoSecret(BinaryKeysetReader.withBytes(b)).getPrimitive(Aead.class); 37 } useAnyReader(KeysetReader r)38 public Aead useAnyReader(KeysetReader r) throws GeneralSecurityException, IOException { 39 return KeysetHandle.readNoSecret(r).getPrimitive(Aead.class); 40 } 41 macKeyTemplateUser()42 public void macKeyTemplateUser() throws Exception { 43 Object a = KeysetHandle.generateNew(MacKeyTemplates.HMAC_SHA256_128BITTAG); 44 Object b = KeysetHandle.generateNew(MacKeyTemplates.HMAC_SHA256_256BITTAG); 45 Object c = KeysetHandle.generateNew(MacKeyTemplates.HMAC_SHA512_256BITTAG); 46 Object d = KeysetHandle.generateNew(MacKeyTemplates.HMAC_SHA512_512BITTAG); 47 Object e = KeysetHandle.generateNew(MacKeyTemplates.AES_CMAC); 48 } 49 aeadKeyTemplateUser()50 public void aeadKeyTemplateUser() throws Exception { 51 Object a = KeysetHandle.generateNew(AeadKeyTemplates.AES128_GCM); 52 Object b = KeysetHandle.generateNew(AeadKeyTemplates.AES256_GCM); 53 Object c = KeysetHandle.generateNew(AeadKeyTemplates.AES128_EAX); 54 Object d = KeysetHandle.generateNew(AeadKeyTemplates.AES256_EAX); 55 Object e = KeysetHandle.generateNew(AeadKeyTemplates.AES128_CTR_HMAC_SHA256); 56 Object f = KeysetHandle.generateNew(AeadKeyTemplates.AES256_CTR_HMAC_SHA256); 57 Object g = KeysetHandle.generateNew(AeadKeyTemplates.CHACHA20_POLY1305); 58 Object h = KeysetHandle.generateNew(AeadKeyTemplates.XCHACHA20_POLY1305); 59 } 60 deterministicAeadKeyTemplateUser()61 public void deterministicAeadKeyTemplateUser() throws Exception { 62 Object a = KeysetHandle.generateNew(DeterministicAeadKeyTemplates.AES256_SIV); 63 } 64 streamingAeadKeyTemplateUser()65 public void streamingAeadKeyTemplateUser() throws Exception { 66 Object a = KeysetHandle.generateNew(StreamingAeadKeyTemplates.AES128_CTR_HMAC_SHA256_4KB); 67 Object b = KeysetHandle.generateNew(StreamingAeadKeyTemplates.AES128_CTR_HMAC_SHA256_1MB); 68 Object c = KeysetHandle.generateNew(StreamingAeadKeyTemplates.AES256_CTR_HMAC_SHA256_4KB); 69 Object d = KeysetHandle.generateNew(StreamingAeadKeyTemplates.AES256_CTR_HMAC_SHA256_1MB); 70 Object e = KeysetHandle.generateNew(StreamingAeadKeyTemplates.AES128_GCM_HKDF_4KB); 71 Object f = KeysetHandle.generateNew(StreamingAeadKeyTemplates.AES128_GCM_HKDF_1MB); 72 Object g = KeysetHandle.generateNew(StreamingAeadKeyTemplates.AES256_GCM_HKDF_4KB); 73 Object h = KeysetHandle.generateNew(StreamingAeadKeyTemplates.AES256_GCM_HKDF_1MB); 74 } 75 } 76