1 /*
2  * Copyright (C) 2015-2016 The Android Open Source Project
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 #pragma once
18 
19 #include <stdint.h>
20 
21 #define DEBUG_MAC_VALUES 0
22 
23 struct key {
24     uint8_t byte[32];
25 };
26 
27 struct mac {
28     uint8_t byte[16];
29 };
30 
31 struct iv {
32     uint8_t byte[16];
33 };
34 #define IV_INITIAL_ZERO_VALUE(iv) \
35     {                             \
36         { 0 }                     \
37     }
38 
39 #if DEBUG_MAC_VALUES
40 #define UINT8_16_PRINTF_STR \
41     "%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x"
42 #define UINT8_16_PRINTF_ARGS(var)                                           \
43     var[0], var[1], var[2], var[3], var[4], var[5], var[6], var[7], var[8], \
44             var[9], var[10], var[11], var[12], var[13], var[14], var[15]
45 #else
46 #define UINT8_16_PRINTF_STR "%c"
47 #define UINT8_16_PRINTF_ARGS(var) '*'
48 #endif
49 
50 #define MAC_PRINTF_STR UINT8_16_PRINTF_STR
51 #define MAC_PRINTF_ARGS(var) UINT8_16_PRINTF_ARGS((var)->byte)
52 #define IV_PRINTF_STR UINT8_16_PRINTF_STR
53 #define IV_PRINTF_ARGS(var) UINT8_16_PRINTF_ARGS((var)->byte)
54 
55 uint64_t str_hash(const char* str);
56 
57 int calculate_mac(const struct key* key,
58                   struct mac* mac,
59                   const void* data,
60                   size_t data_size);
61 
62 int generate_iv(struct iv* iv_out);
63 
64 int storage_encrypt(const struct key* key,
65                     void* data_in_out,
66                     size_t data_size,
67                     const struct iv* iv_in);
68 
69 int storage_decrypt(const struct key* key,
70                     void* data_in_out,
71                     size_t data_size,
72                     const struct iv* iv_in);
73 
74 void crypt_init(void);
75 
76 void crypt_shutdown(void);
77