1 /*
2 * Copyright 2022 Google LLC
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include <stdio.h>
18 #include <stdlib.h>
19 #include <string.h>
20
21 #include "np_ldt.h"
22
23 static const uint8_t KEY_SEED_BYTES[] = {204, 219, 36, 137, 233, 252, 172, 66, 179, 147, 72, 184, 148, 30, 209, 154, 29, 54, 14, 117, 224, 152, 200, 193, 94, 107, 28, 194, 182, 32, 205, 57};
24 static const uint8_t KNOWN_HMAC_BYTES[] = {223, 185, 10, 31, 155, 31, 226, 141, 24, 187, 204, 165, 34, 64, 181, 204, 44, 203, 95, 141, 82, 137, 163, 203, 100, 235, 53, 65, 202, 97, 75, 180};
25 static const uint8_t TEST_DATA_BYTES[] = {205, 104, 63, 225, 161, 209, 248, 70, 84, 61, 10, 19, 212, 174, 164, 0, 64, 200, 214, 123};
26
27
main()28 int main()
29 {
30 // Create test data
31 NpLdtKeySeed* key_seed = malloc(sizeof(NpLdtKeySeed));
32 memcpy(key_seed->bytes, KEY_SEED_BYTES, 32);
33
34 NpMetadataKeyHmac* known_hmac = malloc(sizeof(NpMetadataKeyHmac));
35 memcpy(known_hmac->bytes, KNOWN_HMAC_BYTES, 32);
36
37 uint8_t* plaintext = malloc(20 * sizeof(uint8_t));
38 memcpy(plaintext, TEST_DATA_BYTES, 20);
39
40 NpLdtSalt salt = {
41 {12, 15}
42 };
43
44 // Create handle for encryption
45 NpLdtEncryptHandle enc_handle = NpLdtEncryptCreate(*key_seed);
46 if (enc_handle.handle == 0)
47 {
48 return -1;
49 }
50
51 // Print original plaintext data bytes
52 printf("\n Plaintext data: ");
53 int i;
54 for (i = 0; *(plaintext + i) != 0x00; i++)
55 printf("%X ", *(plaintext + i));
56 printf("\n");
57
58 // Encrypt the data and print it
59 NP_LDT_RESULT result = NpLdtEncrypt(enc_handle, plaintext, 24, salt);
60 if (result)
61 {
62 printf("Error in NpLdtEncrypt: %d\n", result);
63 return result;
64 }
65
66 printf("\n Encrypted data: ");
67 for (i = 0; *(plaintext + i) != 0x00; i++)
68 printf("%X ", *(plaintext + i));
69 printf("\n");
70
71 // Create handle for encryption
72 NpLdtDecryptHandle dec_handle = NpLdtDecryptCreate(*key_seed, *known_hmac);
73 if (enc_handle.handle == 0)
74 {
75 return -1;
76 }
77
78 // Decrypt the data and print its bytes
79 result = NpLdtDecryptAndVerify(dec_handle, plaintext, 24, salt);
80 if (result)
81 {
82 printf("Error in NpDecryptAndVerify: %d\n", result);
83 return result;
84 }
85
86 printf("\n Decrypted data: ");
87 for (i = 0; *(plaintext + i) != 0x00; i++)
88 printf("%X ", *(plaintext + i));
89 printf("\n");
90
91 // Call NpLdtClose to free resources
92 result = NpLdtEncryptClose(enc_handle);
93 if (result)
94 {
95 printf("Error in NpLdtClose: %d\n", result);
96 return result;
97 }
98
99 // Call NpLdtClose to free resources
100 result = NpLdtDecryptClose(dec_handle);
101 if (result)
102 {
103 printf("Error in NpLdtClose: %d\n", result);
104 return result;
105 }
106
107 return 0;
108 }