1 /*===========================================================================
2  *
3  *                            PUBLIC DOMAIN NOTICE
4  *               National Center for Biotechnology Information
5  *
6  *  This software/database is a "United States Government Work" under the
7  *  terms of the United States Copyright Act.  It was written as part of
8  *  the author's official duties as a United States Government employee and
9  *  thus cannot be copyrighted.  This software/database is freely available
10  *  to the public for use. The National Library of Medicine and the U.S.
11  *  Government have not placed any restriction on its use or reproduction.
12  *
13  *  Although all reasonable efforts have been taken to ensure the accuracy
14  *  and reliability of the software and data, the NLM and the U.S.
15  *  Government do not and cannot warrant the performance or results that
16  *  may be obtained by using this software or data. The NLM and the U.S.
17  *  Government disclaim all warranties, express or implied, including
18  *  warranties of performance, merchantability or fitness for any particular
19  *  purpose.
20  *
21  *  Please cite the author in any work or product based on this material.
22  *
23  * ===========================================================================
24  */
25 #ifndef _h_krypto_encfile_priv_libs_
26 #define _h_krypto_encfile_priv_libs_
27 
28 #ifdef  __cplusplus
29 extern "C" {
30 #endif
31 
32 /* ----------------------------------------------------------------------
33  * Offset conversions between encrypted file offset, decrypted file offset
34  * and block ID and block offset
35  */
36 
37 /* BlockId_to_CiphertextOffset
38  *  converts zero-based block id to byte offset into ciphertext
39  */
40 static __inline__
BlockId_to_CiphertextOffset(uint64_t block_id)41 uint64_t BlockId_to_CiphertextOffset ( uint64_t block_id )
42 {
43     return ( sizeof ( KEncFileHeader ) + block_id * sizeof ( KEncFileBlock ) );
44 }
45 
46 /* BlockId_to_DecryptedPos
47  *  converts zero-based block id to byte offset into plaintext
48  */
49 static __inline__
BlockId_to_PlaintextOffset(uint64_t block_id)50 uint64_t BlockId_to_PlaintextOffset ( uint64_t block_id )
51 {
52     return block_id * sizeof ( KEncFileData );
53 }
54 
55 /* PlaintextOffset_to_BlockId
56  *  converts from byte offset into plaintext to a zero-based block id
57  *  NB - will FAIL ( horribly ) if used with a plaintext SIZE
58  */
59 static __inline__
PlaintextOffset_to_BlockId(uint64_t pt_offset,uint32_t * poffset)60 uint64_t PlaintextOffset_to_BlockId ( uint64_t pt_offset, uint32_t * poffset )
61 {
62     uint64_t block_id = pt_offset / sizeof ( KEncFileData );
63 
64     if ( poffset != NULL )
65         * poffset = ( uint32_t ) ( pt_offset - BlockId_to_PlaintextOffset ( block_id ) );
66 
67     return block_id;
68 }
69 
70 static __inline__
PlaintextSize_to_BlockCount(uint64_t pt_size,uint32_t * padding)71 uint64_t PlaintextSize_to_BlockCount ( uint64_t pt_size, uint32_t * padding )
72 {
73     uint64_t block_count = ( pt_size + sizeof ( KEncFileData ) - 1 ) / sizeof ( KEncFileData );
74 
75     if ( padding != NULL )
76         * padding = ( uint32_t ) ( BlockId_to_PlaintextOffset ( block_count ) - pt_size );
77 
78     return block_count;
79 }
80 
81 static __inline__
EncryptedPos_to_BlockId(uint64_t enc_offset,uint32_t * poffset,bool * in_block)82 uint64_t EncryptedPos_to_BlockId (uint64_t enc_offset, uint32_t * poffset,
83                                      bool * in_block)
84 {
85     uint64_t block_id;
86 
87     if (enc_offset < sizeof (KEncFileHeader))
88     {
89         if (poffset)
90             *poffset = 0;
91         if (in_block)
92             *in_block = false;
93         block_id = 0;
94     }
95     else
96     {
97         uint64_t offset;
98 
99         enc_offset -= sizeof (KEncFileHeader);
100         block_id = enc_offset / sizeof (KEncFileBlock);
101         offset = enc_offset % sizeof (KEncFileBlock);
102 
103         if (offset <= sizeof(KEncFileKey))
104         {
105             if (poffset)
106                 *poffset = 0;
107             if (in_block)
108                 *in_block = false;
109         }
110         else
111         {
112             offset -= sizeof(KEncFileKey);
113 
114             if (offset >= sizeof(KEncFileData))
115             {
116                 if (poffset)
117                     *poffset = 0;
118                 if (in_block)
119                     *in_block = false;
120             }
121             else
122             {
123                 if (poffset)
124                     *poffset = (uint32_t)offset;
125                 if (in_block)
126                     *in_block = true;
127             }
128         }
129     }
130     return block_id;
131 }
132 
133 struct KFile;
134 struct KKey;
135 
136 KRYPTO_EXTERN rc_t CC KEncFileWriteHeader (struct KFile * self);
137 KRYPTO_EXTERN rc_t CC KEncFileWriteHeader_v1 (struct KFile * self);
138 KRYPTO_EXTERN rc_t CC KEncFileWriteHeader_v2 (struct KFile * self);
139 KRYPTO_EXTERN rc_t CC KEncFileMakeWriteBlock (struct KFile ** pself,
140                                               struct KFile * encrypted,
141                                               const struct KKey * key);
142 
143 KRYPTO_EXTERN rc_t CC KEncFileMakeBlock_v2 (struct KFile ** pself,
144                                          struct KFile * encrypted,
145                                          const struct KKey * key);
146 
147 
148 #if 0
149 #if USE_UPDATE_V1
150 #define KEncFileWriteHeader KEncFileWriteHeader_v1
151 #else
152 #define KEncFileWriteHeader KEncFileWriteHeader_v2
153 #endif
154 #endif
155 
156 #ifdef  __cplusplus
157 }
158 #endif
159 
160 #endif /* #ifndef _h_krypto_encfile_priv_libs_ */
161 
162 
163