forked from hardcoresushi/DroidFS
Inital commit
This commit is contained in:
commit
bc00740f5b
18
.gitignore
vendored
Normal file
18
.gitignore
vendored
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
.DS_Store
|
||||||
|
.Trashes
|
||||||
|
ehthumbs.db
|
||||||
|
Thumbs.db
|
||||||
|
build
|
||||||
|
.cxx
|
||||||
|
/app/release
|
||||||
|
.gradle
|
||||||
|
local.properties
|
||||||
|
.idea/
|
||||||
|
*.apk
|
||||||
|
*.ap_
|
||||||
|
*.dex
|
||||||
|
*.class
|
||||||
|
# Compiled Object files, Static and Dynamic libs (Shared Objects)
|
||||||
|
*.o
|
||||||
|
*.a
|
||||||
|
*.so
|
661
LICENSE.txt
Normal file
661
LICENSE.txt
Normal file
@ -0,0 +1,661 @@
|
|||||||
|
GNU AFFERO GENERAL PUBLIC LICENSE
|
||||||
|
Version 3, 19 November 2007
|
||||||
|
|
||||||
|
Copyright (C) 2007 Free Software Foundation, Inc. <https://fsf.org/>
|
||||||
|
Everyone is permitted to copy and distribute verbatim copies
|
||||||
|
of this license document, but changing it is not allowed.
|
||||||
|
|
||||||
|
Preamble
|
||||||
|
|
||||||
|
The GNU Affero General Public License is a free, copyleft license for
|
||||||
|
software and other kinds of works, specifically designed to ensure
|
||||||
|
cooperation with the community in the case of network server software.
|
||||||
|
|
||||||
|
The licenses for most software and other practical works are designed
|
||||||
|
to take away your freedom to share and change the works. By contrast,
|
||||||
|
our General Public Licenses are intended to guarantee your freedom to
|
||||||
|
share and change all versions of a program--to make sure it remains free
|
||||||
|
software for all its users.
|
||||||
|
|
||||||
|
When we speak of free software, we are referring to freedom, not
|
||||||
|
price. Our General Public Licenses are designed to make sure that you
|
||||||
|
have the freedom to distribute copies of free software (and charge for
|
||||||
|
them if you wish), that you receive source code or can get it if you
|
||||||
|
want it, that you can change the software or use pieces of it in new
|
||||||
|
free programs, and that you know you can do these things.
|
||||||
|
|
||||||
|
Developers that use our General Public Licenses protect your rights
|
||||||
|
with two steps: (1) assert copyright on the software, and (2) offer
|
||||||
|
you this License which gives you legal permission to copy, distribute
|
||||||
|
and/or modify the software.
|
||||||
|
|
||||||
|
A secondary benefit of defending all users' freedom is that
|
||||||
|
improvements made in alternate versions of the program, if they
|
||||||
|
receive widespread use, become available for other developers to
|
||||||
|
incorporate. Many developers of free software are heartened and
|
||||||
|
encouraged by the resulting cooperation. However, in the case of
|
||||||
|
software used on network servers, this result may fail to come about.
|
||||||
|
The GNU General Public License permits making a modified version and
|
||||||
|
letting the public access it on a server without ever releasing its
|
||||||
|
source code to the public.
|
||||||
|
|
||||||
|
The GNU Affero General Public License is designed specifically to
|
||||||
|
ensure that, in such cases, the modified source code becomes available
|
||||||
|
to the community. It requires the operator of a network server to
|
||||||
|
provide the source code of the modified version running there to the
|
||||||
|
users of that server. Therefore, public use of a modified version, on
|
||||||
|
a publicly accessible server, gives the public access to the source
|
||||||
|
code of the modified version.
|
||||||
|
|
||||||
|
An older license, called the Affero General Public License and
|
||||||
|
published by Affero, was designed to accomplish similar goals. This is
|
||||||
|
a different license, not a version of the Affero GPL, but Affero has
|
||||||
|
released a new version of the Affero GPL which permits relicensing under
|
||||||
|
this license.
|
||||||
|
|
||||||
|
The precise terms and conditions for copying, distribution and
|
||||||
|
modification follow.
|
||||||
|
|
||||||
|
TERMS AND CONDITIONS
|
||||||
|
|
||||||
|
0. Definitions.
|
||||||
|
|
||||||
|
"This License" refers to version 3 of the GNU Affero General Public License.
|
||||||
|
|
||||||
|
"Copyright" also means copyright-like laws that apply to other kinds of
|
||||||
|
works, such as semiconductor masks.
|
||||||
|
|
||||||
|
"The Program" refers to any copyrightable work licensed under this
|
||||||
|
License. Each licensee is addressed as "you". "Licensees" and
|
||||||
|
"recipients" may be individuals or organizations.
|
||||||
|
|
||||||
|
To "modify" a work means to copy from or adapt all or part of the work
|
||||||
|
in a fashion requiring copyright permission, other than the making of an
|
||||||
|
exact copy. The resulting work is called a "modified version" of the
|
||||||
|
earlier work or a work "based on" the earlier work.
|
||||||
|
|
||||||
|
A "covered work" means either the unmodified Program or a work based
|
||||||
|
on the Program.
|
||||||
|
|
||||||
|
To "propagate" a work means to do anything with it that, without
|
||||||
|
permission, would make you directly or secondarily liable for
|
||||||
|
infringement under applicable copyright law, except executing it on a
|
||||||
|
computer or modifying a private copy. Propagation includes copying,
|
||||||
|
distribution (with or without modification), making available to the
|
||||||
|
public, and in some countries other activities as well.
|
||||||
|
|
||||||
|
To "convey" a work means any kind of propagation that enables other
|
||||||
|
parties to make or receive copies. Mere interaction with a user through
|
||||||
|
a computer network, with no transfer of a copy, is not conveying.
|
||||||
|
|
||||||
|
An interactive user interface displays "Appropriate Legal Notices"
|
||||||
|
to the extent that it includes a convenient and prominently visible
|
||||||
|
feature that (1) displays an appropriate copyright notice, and (2)
|
||||||
|
tells the user that there is no warranty for the work (except to the
|
||||||
|
extent that warranties are provided), that licensees may convey the
|
||||||
|
work under this License, and how to view a copy of this License. If
|
||||||
|
the interface presents a list of user commands or options, such as a
|
||||||
|
menu, a prominent item in the list meets this criterion.
|
||||||
|
|
||||||
|
1. Source Code.
|
||||||
|
|
||||||
|
The "source code" for a work means the preferred form of the work
|
||||||
|
for making modifications to it. "Object code" means any non-source
|
||||||
|
form of a work.
|
||||||
|
|
||||||
|
A "Standard Interface" means an interface that either is an official
|
||||||
|
standard defined by a recognized standards body, or, in the case of
|
||||||
|
interfaces specified for a particular programming language, one that
|
||||||
|
is widely used among developers working in that language.
|
||||||
|
|
||||||
|
The "System Libraries" of an executable work include anything, other
|
||||||
|
than the work as a whole, that (a) is included in the normal form of
|
||||||
|
packaging a Major Component, but which is not part of that Major
|
||||||
|
Component, and (b) serves only to enable use of the work with that
|
||||||
|
Major Component, or to implement a Standard Interface for which an
|
||||||
|
implementation is available to the public in source code form. A
|
||||||
|
"Major Component", in this context, means a major essential component
|
||||||
|
(kernel, window system, and so on) of the specific operating system
|
||||||
|
(if any) on which the executable work runs, or a compiler used to
|
||||||
|
produce the work, or an object code interpreter used to run it.
|
||||||
|
|
||||||
|
The "Corresponding Source" for a work in object code form means all
|
||||||
|
the source code needed to generate, install, and (for an executable
|
||||||
|
work) run the object code and to modify the work, including scripts to
|
||||||
|
control those activities. However, it does not include the work's
|
||||||
|
System Libraries, or general-purpose tools or generally available free
|
||||||
|
programs which are used unmodified in performing those activities but
|
||||||
|
which are not part of the work. For example, Corresponding Source
|
||||||
|
includes interface definition files associated with source files for
|
||||||
|
the work, and the source code for shared libraries and dynamically
|
||||||
|
linked subprograms that the work is specifically designed to require,
|
||||||
|
such as by intimate data communication or control flow between those
|
||||||
|
subprograms and other parts of the work.
|
||||||
|
|
||||||
|
The Corresponding Source need not include anything that users
|
||||||
|
can regenerate automatically from other parts of the Corresponding
|
||||||
|
Source.
|
||||||
|
|
||||||
|
The Corresponding Source for a work in source code form is that
|
||||||
|
same work.
|
||||||
|
|
||||||
|
2. Basic Permissions.
|
||||||
|
|
||||||
|
All rights granted under this License are granted for the term of
|
||||||
|
copyright on the Program, and are irrevocable provided the stated
|
||||||
|
conditions are met. This License explicitly affirms your unlimited
|
||||||
|
permission to run the unmodified Program. The output from running a
|
||||||
|
covered work is covered by this License only if the output, given its
|
||||||
|
content, constitutes a covered work. This License acknowledges your
|
||||||
|
rights of fair use or other equivalent, as provided by copyright law.
|
||||||
|
|
||||||
|
You may make, run and propagate covered works that you do not
|
||||||
|
convey, without conditions so long as your license otherwise remains
|
||||||
|
in force. You may convey covered works to others for the sole purpose
|
||||||
|
of having them make modifications exclusively for you, or provide you
|
||||||
|
with facilities for running those works, provided that you comply with
|
||||||
|
the terms of this License in conveying all material for which you do
|
||||||
|
not control copyright. Those thus making or running the covered works
|
||||||
|
for you must do so exclusively on your behalf, under your direction
|
||||||
|
and control, on terms that prohibit them from making any copies of
|
||||||
|
your copyrighted material outside their relationship with you.
|
||||||
|
|
||||||
|
Conveying under any other circumstances is permitted solely under
|
||||||
|
the conditions stated below. Sublicensing is not allowed; section 10
|
||||||
|
makes it unnecessary.
|
||||||
|
|
||||||
|
3. Protecting Users' Legal Rights From Anti-Circumvention Law.
|
||||||
|
|
||||||
|
No covered work shall be deemed part of an effective technological
|
||||||
|
measure under any applicable law fulfilling obligations under article
|
||||||
|
11 of the WIPO copyright treaty adopted on 20 December 1996, or
|
||||||
|
similar laws prohibiting or restricting circumvention of such
|
||||||
|
measures.
|
||||||
|
|
||||||
|
When you convey a covered work, you waive any legal power to forbid
|
||||||
|
circumvention of technological measures to the extent such circumvention
|
||||||
|
is effected by exercising rights under this License with respect to
|
||||||
|
the covered work, and you disclaim any intention to limit operation or
|
||||||
|
modification of the work as a means of enforcing, against the work's
|
||||||
|
users, your or third parties' legal rights to forbid circumvention of
|
||||||
|
technological measures.
|
||||||
|
|
||||||
|
4. Conveying Verbatim Copies.
|
||||||
|
|
||||||
|
You may convey verbatim copies of the Program's source code as you
|
||||||
|
receive it, in any medium, provided that you conspicuously and
|
||||||
|
appropriately publish on each copy an appropriate copyright notice;
|
||||||
|
keep intact all notices stating that this License and any
|
||||||
|
non-permissive terms added in accord with section 7 apply to the code;
|
||||||
|
keep intact all notices of the absence of any warranty; and give all
|
||||||
|
recipients a copy of this License along with the Program.
|
||||||
|
|
||||||
|
You may charge any price or no price for each copy that you convey,
|
||||||
|
and you may offer support or warranty protection for a fee.
|
||||||
|
|
||||||
|
5. Conveying Modified Source Versions.
|
||||||
|
|
||||||
|
You may convey a work based on the Program, or the modifications to
|
||||||
|
produce it from the Program, in the form of source code under the
|
||||||
|
terms of section 4, provided that you also meet all of these conditions:
|
||||||
|
|
||||||
|
a) The work must carry prominent notices stating that you modified
|
||||||
|
it, and giving a relevant date.
|
||||||
|
|
||||||
|
b) The work must carry prominent notices stating that it is
|
||||||
|
released under this License and any conditions added under section
|
||||||
|
7. This requirement modifies the requirement in section 4 to
|
||||||
|
"keep intact all notices".
|
||||||
|
|
||||||
|
c) You must license the entire work, as a whole, under this
|
||||||
|
License to anyone who comes into possession of a copy. This
|
||||||
|
License will therefore apply, along with any applicable section 7
|
||||||
|
additional terms, to the whole of the work, and all its parts,
|
||||||
|
regardless of how they are packaged. This License gives no
|
||||||
|
permission to license the work in any other way, but it does not
|
||||||
|
invalidate such permission if you have separately received it.
|
||||||
|
|
||||||
|
d) If the work has interactive user interfaces, each must display
|
||||||
|
Appropriate Legal Notices; however, if the Program has interactive
|
||||||
|
interfaces that do not display Appropriate Legal Notices, your
|
||||||
|
work need not make them do so.
|
||||||
|
|
||||||
|
A compilation of a covered work with other separate and independent
|
||||||
|
works, which are not by their nature extensions of the covered work,
|
||||||
|
and which are not combined with it such as to form a larger program,
|
||||||
|
in or on a volume of a storage or distribution medium, is called an
|
||||||
|
"aggregate" if the compilation and its resulting copyright are not
|
||||||
|
used to limit the access or legal rights of the compilation's users
|
||||||
|
beyond what the individual works permit. Inclusion of a covered work
|
||||||
|
in an aggregate does not cause this License to apply to the other
|
||||||
|
parts of the aggregate.
|
||||||
|
|
||||||
|
6. Conveying Non-Source Forms.
|
||||||
|
|
||||||
|
You may convey a covered work in object code form under the terms
|
||||||
|
of sections 4 and 5, provided that you also convey the
|
||||||
|
machine-readable Corresponding Source under the terms of this License,
|
||||||
|
in one of these ways:
|
||||||
|
|
||||||
|
a) Convey the object code in, or embodied in, a physical product
|
||||||
|
(including a physical distribution medium), accompanied by the
|
||||||
|
Corresponding Source fixed on a durable physical medium
|
||||||
|
customarily used for software interchange.
|
||||||
|
|
||||||
|
b) Convey the object code in, or embodied in, a physical product
|
||||||
|
(including a physical distribution medium), accompanied by a
|
||||||
|
written offer, valid for at least three years and valid for as
|
||||||
|
long as you offer spare parts or customer support for that product
|
||||||
|
model, to give anyone who possesses the object code either (1) a
|
||||||
|
copy of the Corresponding Source for all the software in the
|
||||||
|
product that is covered by this License, on a durable physical
|
||||||
|
medium customarily used for software interchange, for a price no
|
||||||
|
more than your reasonable cost of physically performing this
|
||||||
|
conveying of source, or (2) access to copy the
|
||||||
|
Corresponding Source from a network server at no charge.
|
||||||
|
|
||||||
|
c) Convey individual copies of the object code with a copy of the
|
||||||
|
written offer to provide the Corresponding Source. This
|
||||||
|
alternative is allowed only occasionally and noncommercially, and
|
||||||
|
only if you received the object code with such an offer, in accord
|
||||||
|
with subsection 6b.
|
||||||
|
|
||||||
|
d) Convey the object code by offering access from a designated
|
||||||
|
place (gratis or for a charge), and offer equivalent access to the
|
||||||
|
Corresponding Source in the same way through the same place at no
|
||||||
|
further charge. You need not require recipients to copy the
|
||||||
|
Corresponding Source along with the object code. If the place to
|
||||||
|
copy the object code is a network server, the Corresponding Source
|
||||||
|
may be on a different server (operated by you or a third party)
|
||||||
|
that supports equivalent copying facilities, provided you maintain
|
||||||
|
clear directions next to the object code saying where to find the
|
||||||
|
Corresponding Source. Regardless of what server hosts the
|
||||||
|
Corresponding Source, you remain obligated to ensure that it is
|
||||||
|
available for as long as needed to satisfy these requirements.
|
||||||
|
|
||||||
|
e) Convey the object code using peer-to-peer transmission, provided
|
||||||
|
you inform other peers where the object code and Corresponding
|
||||||
|
Source of the work are being offered to the general public at no
|
||||||
|
charge under subsection 6d.
|
||||||
|
|
||||||
|
A separable portion of the object code, whose source code is excluded
|
||||||
|
from the Corresponding Source as a System Library, need not be
|
||||||
|
included in conveying the object code work.
|
||||||
|
|
||||||
|
A "User Product" is either (1) a "consumer product", which means any
|
||||||
|
tangible personal property which is normally used for personal, family,
|
||||||
|
or household purposes, or (2) anything designed or sold for incorporation
|
||||||
|
into a dwelling. In determining whether a product is a consumer product,
|
||||||
|
doubtful cases shall be resolved in favor of coverage. For a particular
|
||||||
|
product received by a particular user, "normally used" refers to a
|
||||||
|
typical or common use of that class of product, regardless of the status
|
||||||
|
of the particular user or of the way in which the particular user
|
||||||
|
actually uses, or expects or is expected to use, the product. A product
|
||||||
|
is a consumer product regardless of whether the product has substantial
|
||||||
|
commercial, industrial or non-consumer uses, unless such uses represent
|
||||||
|
the only significant mode of use of the product.
|
||||||
|
|
||||||
|
"Installation Information" for a User Product means any methods,
|
||||||
|
procedures, authorization keys, or other information required to install
|
||||||
|
and execute modified versions of a covered work in that User Product from
|
||||||
|
a modified version of its Corresponding Source. The information must
|
||||||
|
suffice to ensure that the continued functioning of the modified object
|
||||||
|
code is in no case prevented or interfered with solely because
|
||||||
|
modification has been made.
|
||||||
|
|
||||||
|
If you convey an object code work under this section in, or with, or
|
||||||
|
specifically for use in, a User Product, and the conveying occurs as
|
||||||
|
part of a transaction in which the right of possession and use of the
|
||||||
|
User Product is transferred to the recipient in perpetuity or for a
|
||||||
|
fixed term (regardless of how the transaction is characterized), the
|
||||||
|
Corresponding Source conveyed under this section must be accompanied
|
||||||
|
by the Installation Information. But this requirement does not apply
|
||||||
|
if neither you nor any third party retains the ability to install
|
||||||
|
modified object code on the User Product (for example, the work has
|
||||||
|
been installed in ROM).
|
||||||
|
|
||||||
|
The requirement to provide Installation Information does not include a
|
||||||
|
requirement to continue to provide support service, warranty, or updates
|
||||||
|
for a work that has been modified or installed by the recipient, or for
|
||||||
|
the User Product in which it has been modified or installed. Access to a
|
||||||
|
network may be denied when the modification itself materially and
|
||||||
|
adversely affects the operation of the network or violates the rules and
|
||||||
|
protocols for communication across the network.
|
||||||
|
|
||||||
|
Corresponding Source conveyed, and Installation Information provided,
|
||||||
|
in accord with this section must be in a format that is publicly
|
||||||
|
documented (and with an implementation available to the public in
|
||||||
|
source code form), and must require no special password or key for
|
||||||
|
unpacking, reading or copying.
|
||||||
|
|
||||||
|
7. Additional Terms.
|
||||||
|
|
||||||
|
"Additional permissions" are terms that supplement the terms of this
|
||||||
|
License by making exceptions from one or more of its conditions.
|
||||||
|
Additional permissions that are applicable to the entire Program shall
|
||||||
|
be treated as though they were included in this License, to the extent
|
||||||
|
that they are valid under applicable law. If additional permissions
|
||||||
|
apply only to part of the Program, that part may be used separately
|
||||||
|
under those permissions, but the entire Program remains governed by
|
||||||
|
this License without regard to the additional permissions.
|
||||||
|
|
||||||
|
When you convey a copy of a covered work, you may at your option
|
||||||
|
remove any additional permissions from that copy, or from any part of
|
||||||
|
it. (Additional permissions may be written to require their own
|
||||||
|
removal in certain cases when you modify the work.) You may place
|
||||||
|
additional permissions on material, added by you to a covered work,
|
||||||
|
for which you have or can give appropriate copyright permission.
|
||||||
|
|
||||||
|
Notwithstanding any other provision of this License, for material you
|
||||||
|
add to a covered work, you may (if authorized by the copyright holders of
|
||||||
|
that material) supplement the terms of this License with terms:
|
||||||
|
|
||||||
|
a) Disclaiming warranty or limiting liability differently from the
|
||||||
|
terms of sections 15 and 16 of this License; or
|
||||||
|
|
||||||
|
b) Requiring preservation of specified reasonable legal notices or
|
||||||
|
author attributions in that material or in the Appropriate Legal
|
||||||
|
Notices displayed by works containing it; or
|
||||||
|
|
||||||
|
c) Prohibiting misrepresentation of the origin of that material, or
|
||||||
|
requiring that modified versions of such material be marked in
|
||||||
|
reasonable ways as different from the original version; or
|
||||||
|
|
||||||
|
d) Limiting the use for publicity purposes of names of licensors or
|
||||||
|
authors of the material; or
|
||||||
|
|
||||||
|
e) Declining to grant rights under trademark law for use of some
|
||||||
|
trade names, trademarks, or service marks; or
|
||||||
|
|
||||||
|
f) Requiring indemnification of licensors and authors of that
|
||||||
|
material by anyone who conveys the material (or modified versions of
|
||||||
|
it) with contractual assumptions of liability to the recipient, for
|
||||||
|
any liability that these contractual assumptions directly impose on
|
||||||
|
those licensors and authors.
|
||||||
|
|
||||||
|
All other non-permissive additional terms are considered "further
|
||||||
|
restrictions" within the meaning of section 10. If the Program as you
|
||||||
|
received it, or any part of it, contains a notice stating that it is
|
||||||
|
governed by this License along with a term that is a further
|
||||||
|
restriction, you may remove that term. If a license document contains
|
||||||
|
a further restriction but permits relicensing or conveying under this
|
||||||
|
License, you may add to a covered work material governed by the terms
|
||||||
|
of that license document, provided that the further restriction does
|
||||||
|
not survive such relicensing or conveying.
|
||||||
|
|
||||||
|
If you add terms to a covered work in accord with this section, you
|
||||||
|
must place, in the relevant source files, a statement of the
|
||||||
|
additional terms that apply to those files, or a notice indicating
|
||||||
|
where to find the applicable terms.
|
||||||
|
|
||||||
|
Additional terms, permissive or non-permissive, may be stated in the
|
||||||
|
form of a separately written license, or stated as exceptions;
|
||||||
|
the above requirements apply either way.
|
||||||
|
|
||||||
|
8. Termination.
|
||||||
|
|
||||||
|
You may not propagate or modify a covered work except as expressly
|
||||||
|
provided under this License. Any attempt otherwise to propagate or
|
||||||
|
modify it is void, and will automatically terminate your rights under
|
||||||
|
this License (including any patent licenses granted under the third
|
||||||
|
paragraph of section 11).
|
||||||
|
|
||||||
|
However, if you cease all violation of this License, then your
|
||||||
|
license from a particular copyright holder is reinstated (a)
|
||||||
|
provisionally, unless and until the copyright holder explicitly and
|
||||||
|
finally terminates your license, and (b) permanently, if the copyright
|
||||||
|
holder fails to notify you of the violation by some reasonable means
|
||||||
|
prior to 60 days after the cessation.
|
||||||
|
|
||||||
|
Moreover, your license from a particular copyright holder is
|
||||||
|
reinstated permanently if the copyright holder notifies you of the
|
||||||
|
violation by some reasonable means, this is the first time you have
|
||||||
|
received notice of violation of this License (for any work) from that
|
||||||
|
copyright holder, and you cure the violation prior to 30 days after
|
||||||
|
your receipt of the notice.
|
||||||
|
|
||||||
|
Termination of your rights under this section does not terminate the
|
||||||
|
licenses of parties who have received copies or rights from you under
|
||||||
|
this License. If your rights have been terminated and not permanently
|
||||||
|
reinstated, you do not qualify to receive new licenses for the same
|
||||||
|
material under section 10.
|
||||||
|
|
||||||
|
9. Acceptance Not Required for Having Copies.
|
||||||
|
|
||||||
|
You are not required to accept this License in order to receive or
|
||||||
|
run a copy of the Program. Ancillary propagation of a covered work
|
||||||
|
occurring solely as a consequence of using peer-to-peer transmission
|
||||||
|
to receive a copy likewise does not require acceptance. However,
|
||||||
|
nothing other than this License grants you permission to propagate or
|
||||||
|
modify any covered work. These actions infringe copyright if you do
|
||||||
|
not accept this License. Therefore, by modifying or propagating a
|
||||||
|
covered work, you indicate your acceptance of this License to do so.
|
||||||
|
|
||||||
|
10. Automatic Licensing of Downstream Recipients.
|
||||||
|
|
||||||
|
Each time you convey a covered work, the recipient automatically
|
||||||
|
receives a license from the original licensors, to run, modify and
|
||||||
|
propagate that work, subject to this License. You are not responsible
|
||||||
|
for enforcing compliance by third parties with this License.
|
||||||
|
|
||||||
|
An "entity transaction" is a transaction transferring control of an
|
||||||
|
organization, or substantially all assets of one, or subdividing an
|
||||||
|
organization, or merging organizations. If propagation of a covered
|
||||||
|
work results from an entity transaction, each party to that
|
||||||
|
transaction who receives a copy of the work also receives whatever
|
||||||
|
licenses to the work the party's predecessor in interest had or could
|
||||||
|
give under the previous paragraph, plus a right to possession of the
|
||||||
|
Corresponding Source of the work from the predecessor in interest, if
|
||||||
|
the predecessor has it or can get it with reasonable efforts.
|
||||||
|
|
||||||
|
You may not impose any further restrictions on the exercise of the
|
||||||
|
rights granted or affirmed under this License. For example, you may
|
||||||
|
not impose a license fee, royalty, or other charge for exercise of
|
||||||
|
rights granted under this License, and you may not initiate litigation
|
||||||
|
(including a cross-claim or counterclaim in a lawsuit) alleging that
|
||||||
|
any patent claim is infringed by making, using, selling, offering for
|
||||||
|
sale, or importing the Program or any portion of it.
|
||||||
|
|
||||||
|
11. Patents.
|
||||||
|
|
||||||
|
A "contributor" is a copyright holder who authorizes use under this
|
||||||
|
License of the Program or a work on which the Program is based. The
|
||||||
|
work thus licensed is called the contributor's "contributor version".
|
||||||
|
|
||||||
|
A contributor's "essential patent claims" are all patent claims
|
||||||
|
owned or controlled by the contributor, whether already acquired or
|
||||||
|
hereafter acquired, that would be infringed by some manner, permitted
|
||||||
|
by this License, of making, using, or selling its contributor version,
|
||||||
|
but do not include claims that would be infringed only as a
|
||||||
|
consequence of further modification of the contributor version. For
|
||||||
|
purposes of this definition, "control" includes the right to grant
|
||||||
|
patent sublicenses in a manner consistent with the requirements of
|
||||||
|
this License.
|
||||||
|
|
||||||
|
Each contributor grants you a non-exclusive, worldwide, royalty-free
|
||||||
|
patent license under the contributor's essential patent claims, to
|
||||||
|
make, use, sell, offer for sale, import and otherwise run, modify and
|
||||||
|
propagate the contents of its contributor version.
|
||||||
|
|
||||||
|
In the following three paragraphs, a "patent license" is any express
|
||||||
|
agreement or commitment, however denominated, not to enforce a patent
|
||||||
|
(such as an express permission to practice a patent or covenant not to
|
||||||
|
sue for patent infringement). To "grant" such a patent license to a
|
||||||
|
party means to make such an agreement or commitment not to enforce a
|
||||||
|
patent against the party.
|
||||||
|
|
||||||
|
If you convey a covered work, knowingly relying on a patent license,
|
||||||
|
and the Corresponding Source of the work is not available for anyone
|
||||||
|
to copy, free of charge and under the terms of this License, through a
|
||||||
|
publicly available network server or other readily accessible means,
|
||||||
|
then you must either (1) cause the Corresponding Source to be so
|
||||||
|
available, or (2) arrange to deprive yourself of the benefit of the
|
||||||
|
patent license for this particular work, or (3) arrange, in a manner
|
||||||
|
consistent with the requirements of this License, to extend the patent
|
||||||
|
license to downstream recipients. "Knowingly relying" means you have
|
||||||
|
actual knowledge that, but for the patent license, your conveying the
|
||||||
|
covered work in a country, or your recipient's use of the covered work
|
||||||
|
in a country, would infringe one or more identifiable patents in that
|
||||||
|
country that you have reason to believe are valid.
|
||||||
|
|
||||||
|
If, pursuant to or in connection with a single transaction or
|
||||||
|
arrangement, you convey, or propagate by procuring conveyance of, a
|
||||||
|
covered work, and grant a patent license to some of the parties
|
||||||
|
receiving the covered work authorizing them to use, propagate, modify
|
||||||
|
or convey a specific copy of the covered work, then the patent license
|
||||||
|
you grant is automatically extended to all recipients of the covered
|
||||||
|
work and works based on it.
|
||||||
|
|
||||||
|
A patent license is "discriminatory" if it does not include within
|
||||||
|
the scope of its coverage, prohibits the exercise of, or is
|
||||||
|
conditioned on the non-exercise of one or more of the rights that are
|
||||||
|
specifically granted under this License. You may not convey a covered
|
||||||
|
work if you are a party to an arrangement with a third party that is
|
||||||
|
in the business of distributing software, under which you make payment
|
||||||
|
to the third party based on the extent of your activity of conveying
|
||||||
|
the work, and under which the third party grants, to any of the
|
||||||
|
parties who would receive the covered work from you, a discriminatory
|
||||||
|
patent license (a) in connection with copies of the covered work
|
||||||
|
conveyed by you (or copies made from those copies), or (b) primarily
|
||||||
|
for and in connection with specific products or compilations that
|
||||||
|
contain the covered work, unless you entered into that arrangement,
|
||||||
|
or that patent license was granted, prior to 28 March 2007.
|
||||||
|
|
||||||
|
Nothing in this License shall be construed as excluding or limiting
|
||||||
|
any implied license or other defenses to infringement that may
|
||||||
|
otherwise be available to you under applicable patent law.
|
||||||
|
|
||||||
|
12. No Surrender of Others' Freedom.
|
||||||
|
|
||||||
|
If conditions are imposed on you (whether by court order, agreement or
|
||||||
|
otherwise) that contradict the conditions of this License, they do not
|
||||||
|
excuse you from the conditions of this License. If you cannot convey a
|
||||||
|
covered work so as to satisfy simultaneously your obligations under this
|
||||||
|
License and any other pertinent obligations, then as a consequence you may
|
||||||
|
not convey it at all. For example, if you agree to terms that obligate you
|
||||||
|
to collect a royalty for further conveying from those to whom you convey
|
||||||
|
the Program, the only way you could satisfy both those terms and this
|
||||||
|
License would be to refrain entirely from conveying the Program.
|
||||||
|
|
||||||
|
13. Remote Network Interaction; Use with the GNU General Public License.
|
||||||
|
|
||||||
|
Notwithstanding any other provision of this License, if you modify the
|
||||||
|
Program, your modified version must prominently offer all users
|
||||||
|
interacting with it remotely through a computer network (if your version
|
||||||
|
supports such interaction) an opportunity to receive the Corresponding
|
||||||
|
Source of your version by providing access to the Corresponding Source
|
||||||
|
from a network server at no charge, through some standard or customary
|
||||||
|
means of facilitating copying of software. This Corresponding Source
|
||||||
|
shall include the Corresponding Source for any work covered by version 3
|
||||||
|
of the GNU General Public License that is incorporated pursuant to the
|
||||||
|
following paragraph.
|
||||||
|
|
||||||
|
Notwithstanding any other provision of this License, you have
|
||||||
|
permission to link or combine any covered work with a work licensed
|
||||||
|
under version 3 of the GNU General Public License into a single
|
||||||
|
combined work, and to convey the resulting work. The terms of this
|
||||||
|
License will continue to apply to the part which is the covered work,
|
||||||
|
but the work with which it is combined will remain governed by version
|
||||||
|
3 of the GNU General Public License.
|
||||||
|
|
||||||
|
14. Revised Versions of this License.
|
||||||
|
|
||||||
|
The Free Software Foundation may publish revised and/or new versions of
|
||||||
|
the GNU Affero General Public License from time to time. Such new versions
|
||||||
|
will be similar in spirit to the present version, but may differ in detail to
|
||||||
|
address new problems or concerns.
|
||||||
|
|
||||||
|
Each version is given a distinguishing version number. If the
|
||||||
|
Program specifies that a certain numbered version of the GNU Affero General
|
||||||
|
Public License "or any later version" applies to it, you have the
|
||||||
|
option of following the terms and conditions either of that numbered
|
||||||
|
version or of any later version published by the Free Software
|
||||||
|
Foundation. If the Program does not specify a version number of the
|
||||||
|
GNU Affero General Public License, you may choose any version ever published
|
||||||
|
by the Free Software Foundation.
|
||||||
|
|
||||||
|
If the Program specifies that a proxy can decide which future
|
||||||
|
versions of the GNU Affero General Public License can be used, that proxy's
|
||||||
|
public statement of acceptance of a version permanently authorizes you
|
||||||
|
to choose that version for the Program.
|
||||||
|
|
||||||
|
Later license versions may give you additional or different
|
||||||
|
permissions. However, no additional obligations are imposed on any
|
||||||
|
author or copyright holder as a result of your choosing to follow a
|
||||||
|
later version.
|
||||||
|
|
||||||
|
15. Disclaimer of Warranty.
|
||||||
|
|
||||||
|
THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY
|
||||||
|
APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT
|
||||||
|
HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY
|
||||||
|
OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO,
|
||||||
|
THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||||
|
PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM
|
||||||
|
IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF
|
||||||
|
ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
|
||||||
|
|
||||||
|
16. Limitation of Liability.
|
||||||
|
|
||||||
|
IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
|
||||||
|
WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS
|
||||||
|
THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY
|
||||||
|
GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE
|
||||||
|
USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF
|
||||||
|
DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD
|
||||||
|
PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS),
|
||||||
|
EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
|
||||||
|
SUCH DAMAGES.
|
||||||
|
|
||||||
|
17. Interpretation of Sections 15 and 16.
|
||||||
|
|
||||||
|
If the disclaimer of warranty and limitation of liability provided
|
||||||
|
above cannot be given local legal effect according to their terms,
|
||||||
|
reviewing courts shall apply local law that most closely approximates
|
||||||
|
an absolute waiver of all civil liability in connection with the
|
||||||
|
Program, unless a warranty or assumption of liability accompanies a
|
||||||
|
copy of the Program in return for a fee.
|
||||||
|
|
||||||
|
END OF TERMS AND CONDITIONS
|
||||||
|
|
||||||
|
How to Apply These Terms to Your New Programs
|
||||||
|
|
||||||
|
If you develop a new program, and you want it to be of the greatest
|
||||||
|
possible use to the public, the best way to achieve this is to make it
|
||||||
|
free software which everyone can redistribute and change under these terms.
|
||||||
|
|
||||||
|
To do so, attach the following notices to the program. It is safest
|
||||||
|
to attach them to the start of each source file to most effectively
|
||||||
|
state the exclusion of warranty; and each file should have at least
|
||||||
|
the "copyright" line and a pointer to where the full notice is found.
|
||||||
|
|
||||||
|
<one line to give the program's name and a brief idea of what it does.>
|
||||||
|
Copyright (C) <year> <name of author>
|
||||||
|
|
||||||
|
This program is free software: you can redistribute it and/or modify
|
||||||
|
it under the terms of the GNU Affero General Public License as published
|
||||||
|
by the Free Software Foundation, either version 3 of the License, or
|
||||||
|
(at your option) any later version.
|
||||||
|
|
||||||
|
This program is distributed in the hope that it will be useful,
|
||||||
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
GNU Affero General Public License for more details.
|
||||||
|
|
||||||
|
You should have received a copy of the GNU Affero General Public License
|
||||||
|
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
Also add information on how to contact you by electronic and paper mail.
|
||||||
|
|
||||||
|
If your software can interact with users remotely through a computer
|
||||||
|
network, you should also make sure that it provides a way for users to
|
||||||
|
get its source. For example, if your program is a web application, its
|
||||||
|
interface could display a "Source" link that leads users to an archive
|
||||||
|
of the code. There are many ways you could offer source, and different
|
||||||
|
solutions will be better for different programs; see section 13 for the
|
||||||
|
specific requirements.
|
||||||
|
|
||||||
|
You should also get your employer (if you work as a programmer) or school,
|
||||||
|
if any, to sign a "copyright disclaimer" for the program, if necessary.
|
||||||
|
For more information on this, and how to apply and follow the GNU AGPL, see
|
||||||
|
<https://www.gnu.org/licenses/>.
|
64
README.md
Normal file
64
README.md
Normal file
@ -0,0 +1,64 @@
|
|||||||
|
# DroidFS
|
||||||
|
DroidFS is an implementation of encrypted overlay filesystems for Android.
|
||||||
|
It allows you to store files and folder in encrypted virtual volumes so that you can share them, store them in the cloud or simply access them more securely.
|
||||||
|
It currently only supports [gocryptfs](https://github.com/rfjakob/gocryptfs) but support for [CryFS](https://github.com/cryfs/cryfs) is expected to be added soon.<br>
|
||||||
|
DroidFS require Android API level 21+ (Android Lollipop).
|
||||||
|
|
||||||
|
# Disclamer
|
||||||
|
DroidFS is provided "as is", without any warranty of any kind.
|
||||||
|
It shouldn't be considered an absolute safe way to store files.
|
||||||
|
DroidFS cannot protect you from screen recording apps, keyloggers, apk backdooring, compromised root accesses, memory dumps etc.
|
||||||
|
Do not use this app with volumes containing sensitive data unless you know exactly what you are doing.
|
||||||
|
|
||||||
|
# Unsafe features
|
||||||
|
|
||||||
|
DroidFS allows you to enable/disable unsafe features to fit your needs between security and comfort.
|
||||||
|
It is strongly recommended to read the documentation of a feature before enabling it.
|
||||||
|
|
||||||
|
#### Allow screenshots:
|
||||||
|
Disable the secure flag of DroidFS activities. This will allow you to take screenshots from the app, but will also allow other apps to record the screen while using DroidFS.
|
||||||
|
Note: apps with root access don't care about this flag: they can take screenshots or record the screen of any app without any permissions.
|
||||||
|
|
||||||
|
#### Allow opening files with other applications:
|
||||||
|
Decrypt and open file using external apps. This require writing the plain file to disk (DroidFS internal storage).
|
||||||
|
|
||||||
|
#### Allow exporting files:
|
||||||
|
Decrypt and write file to disk (external storage). Any app with storage permissions can access exported files.
|
||||||
|
|
||||||
|
#### Allow sharing files via the android share menu:
|
||||||
|
Decrypt and share file with other apps. This require writing the plain file to disk (DroidFS internal storage).
|
||||||
|
|
||||||
|
#### Allow saving password hash using fingerprint:
|
||||||
|
Generate an AES-256 GCM key in the Android Keystore (protected by fingerprint authentication), then use it to encrypt the volume password hash and store it to the DroidFS internal storage. This require Android v6.0+
|
||||||
|
|
||||||
|
# Download
|
||||||
|
You can download the latest version [here](https://github.com/hardcore-sushi/DroidFS/releases).
|
||||||
|
|
||||||
|
# Build
|
||||||
|
Most of the original gocryptfs code was used as is (written in Go) and compiled to native code. That's why you need [Go](https://golang.org) and the [Android Native Development Kit (NDK)](https://developer.android.com/ndk/) to build DroidFS from source.
|
||||||
|
|
||||||
|
#### Install Requirements
|
||||||
|
- [Android Studio](https://developer.android.com/studio/)
|
||||||
|
- [Android NDK and CMake](https://developer.android.com/studio/projects/install-ndk)
|
||||||
|
- [Go](https://golang.org/doc/install)
|
||||||
|
|
||||||
|
#### Download Sources
|
||||||
|
```
|
||||||
|
$ git clone https://github.com/hardcore-sushi/DroidFS.git
|
||||||
|
```
|
||||||
|
Gocryptfs need openssl to work:
|
||||||
|
```
|
||||||
|
$ cd DroidFS/app/libgocryptfs
|
||||||
|
$ wget -qO - https://www.openssl.org/source/openssl-1.1.1g.tar.gz | tar -xvzf -
|
||||||
|
```
|
||||||
|
|
||||||
|
#### Build
|
||||||
|
First, we need to build libgocryptfs.<br>
|
||||||
|
Retrieve your Android NDK installation path, usually someting like "\<Android SDK path\>/ndk/\<NDK version\>".
|
||||||
|
```
|
||||||
|
$ cd DroidFS/app/libgocryptfs
|
||||||
|
$ env ANDROID_NDK_HOME="<your ndk path>" OPENSSL_PATH="./openssl-1.1.1g" ./build.sh
|
||||||
|
```
|
||||||
|
Then, open the DroidFS project with Android Studio.<br>
|
||||||
|
If a device (virtual or physical) is connected, just click on "Run".<br>
|
||||||
|
If you want to generate a signed APK, you can follow this [post](https://stackoverflow.com/a/28938286).
|
28
app/CMakeLists.txt
Normal file
28
app/CMakeLists.txt
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
cmake_minimum_required(VERSION 3.4.1)
|
||||||
|
|
||||||
|
add_library(
|
||||||
|
gocryptfs
|
||||||
|
SHARED
|
||||||
|
IMPORTED
|
||||||
|
)
|
||||||
|
|
||||||
|
set_target_properties(
|
||||||
|
gocryptfs
|
||||||
|
PROPERTIES IMPORTED_LOCATION
|
||||||
|
${PROJECT_SOURCE_DIR}/libgocryptfs/build/${ANDROID_ABI}/libgocryptfs.so
|
||||||
|
)
|
||||||
|
|
||||||
|
add_library(
|
||||||
|
gocryptfs_jni
|
||||||
|
SHARED
|
||||||
|
src/main/native/gocryptfs_jni.c
|
||||||
|
)
|
||||||
|
|
||||||
|
target_link_libraries(
|
||||||
|
gocryptfs_jni
|
||||||
|
gocryptfs
|
||||||
|
)
|
||||||
|
|
||||||
|
include_directories(
|
||||||
|
${PROJECT_SOURCE_DIR}/libgocryptfs/build/${ANDROID_ABI}/
|
||||||
|
)
|
46
app/build.gradle
Normal file
46
app/build.gradle
Normal file
@ -0,0 +1,46 @@
|
|||||||
|
apply plugin: 'com.android.application'
|
||||||
|
apply plugin: 'kotlin-android'
|
||||||
|
apply plugin: 'kotlin-android-extensions'
|
||||||
|
|
||||||
|
android {
|
||||||
|
compileSdkVersion 29
|
||||||
|
buildToolsVersion "30.0.0"
|
||||||
|
|
||||||
|
defaultConfig {
|
||||||
|
applicationId "sushi.hardcore.droidfs"
|
||||||
|
minSdkVersion 21
|
||||||
|
targetSdkVersion 28
|
||||||
|
versionCode 1
|
||||||
|
versionName "1.0"
|
||||||
|
|
||||||
|
ndk {
|
||||||
|
abiFilters 'x86_64', 'armeabi-v7a', 'arm64-v8a'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
buildTypes {
|
||||||
|
release {
|
||||||
|
minifyEnabled false
|
||||||
|
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
externalNativeBuild {
|
||||||
|
cmake {
|
||||||
|
path file('CMakeLists.txt')
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
dependencies {
|
||||||
|
implementation fileTree(dir: "libs", include: ["*.jar"])
|
||||||
|
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
|
||||||
|
implementation 'androidx.core:core-ktx:1.3.0'
|
||||||
|
implementation 'androidx.appcompat:appcompat:1.1.0'
|
||||||
|
testImplementation 'junit:junit:4.12'
|
||||||
|
|
||||||
|
implementation 'androidx.sqlite:sqlite:2.1.0'
|
||||||
|
implementation 'androidx.preference:preference:1.1.1'
|
||||||
|
implementation 'com.github.clans:fab:1.6.4'
|
||||||
|
implementation 'com.jaredrummler:cyanea:1.0.2'
|
||||||
|
}
|
4
app/libgocryptfs/.gitignore
vendored
Normal file
4
app/libgocryptfs/.gitignore
vendored
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
openssl*
|
||||||
|
lib
|
||||||
|
include
|
||||||
|
build
|
75
app/libgocryptfs/build.sh
Executable file
75
app/libgocryptfs/build.sh
Executable file
@ -0,0 +1,75 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
|
||||||
|
if [ -z ${ANDROID_NDK_HOME+x} ]; then
|
||||||
|
echo "Error: \$ANDROID_NDK_HOME is not defined."
|
||||||
|
elif [ -z ${OPENSSL_PATH+x} ]; then
|
||||||
|
echo "Error: \$OPENSSL_PATH is not defined."
|
||||||
|
else
|
||||||
|
NDK_BIN_PATH="$ANDROID_NDK_HOME/toolchains/llvm/prebuilt/linux-x86_64/bin"
|
||||||
|
declare -a ABIs=("x86_64" "arm64-v8a" "armeabi-v7a")
|
||||||
|
|
||||||
|
compile_openssl(){
|
||||||
|
if [ ! -d "./lib/$1" ]; then
|
||||||
|
if [ "$1" = "x86_64" ]; then
|
||||||
|
OPENSSL_ARCH="android-x86_64"
|
||||||
|
elif [ "$1" = "arm64-v8a" ]; then
|
||||||
|
OPENSSL_ARCH="android-arm64"
|
||||||
|
elif [ "$1" = "armeabi-v7a" ]; then
|
||||||
|
OPENSSL_ARCH="android-arm"
|
||||||
|
else
|
||||||
|
echo "Invalid ABI: $1"
|
||||||
|
exit
|
||||||
|
fi
|
||||||
|
|
||||||
|
export CFLAGS=-D__ANDROID_API__=21
|
||||||
|
export PATH=$ANDROID_NDK_HOME/toolchains/llvm/prebuilt/linux-x86_64/bin:$ANDROID_NDK_HOME/toolchains/arm-linux-androideabi-4.9/prebuilt/linux-x86_64/bin:$PATH
|
||||||
|
(cd "$OPENSSL_PATH" && if [ -f "Makefile" ]; then make clean; fi && ./Configure $OPENSSL_ARCH -D__ANDROID_API__=21 no-stdio && make build_libs)
|
||||||
|
mkdir -p "./lib/$1" && cp "$OPENSSL_PATH/libcrypto.a" "$OPENSSL_PATH/libssl.a" "./lib/$1"
|
||||||
|
mkdir -p "./include/$1" && cp -r "$OPENSSL_PATH"/include/* "./include/$1/"
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
compile_for_arch(){
|
||||||
|
compile_openssl $1
|
||||||
|
MAIN_PACKAGE="main.go"
|
||||||
|
if [ "$1" = "x86_64" ]; then
|
||||||
|
CFN="x86_64-linux-android21-clang"
|
||||||
|
elif [ "$1" = "arm64-v8a" ]; then
|
||||||
|
CFN="aarch64-linux-android21-clang"
|
||||||
|
export GOARCH=arm64
|
||||||
|
export GOARM=7
|
||||||
|
elif [ "$1" = "armeabi-v7a" ]; then
|
||||||
|
CFN="armv7a-linux-androideabi21-clang"
|
||||||
|
export GOARCH=arm
|
||||||
|
export GOARM=7
|
||||||
|
MAIN_PACKAGE="main32.go"
|
||||||
|
#patch arch specific code
|
||||||
|
sed "s/C.malloc(C.ulong/C.malloc(C.uint/g" main.go > $MAIN_PACKAGE
|
||||||
|
sed -i "s/st.Mtim.Sec/int64(st.Mtim.Sec)/g" $MAIN_PACKAGE
|
||||||
|
else
|
||||||
|
echo "Invalid ABI: $1"
|
||||||
|
exit
|
||||||
|
fi
|
||||||
|
|
||||||
|
export CC="$NDK_BIN_PATH/$CFN"
|
||||||
|
export CXX="$NDK_BIN_PATH/$CFN++"
|
||||||
|
export CGO_ENABLED=1
|
||||||
|
export GOOS=android
|
||||||
|
export CGO_CFLAGS="-I ${PWD}/include/$1"
|
||||||
|
export CGO_LDFLAGS="-Wl,-soname=libgocryptfs.so -L${PWD}/lib/$1"
|
||||||
|
go build -o build/$1/libgocryptfs.so -buildmode=c-shared $MAIN_PACKAGE
|
||||||
|
if [ $MAIN_PACKAGE = "main32.go" ]; then
|
||||||
|
rm $MAIN_PACKAGE
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
if [ "$#" -eq 1 ]; then
|
||||||
|
compile_for_arch $1
|
||||||
|
else
|
||||||
|
for abi in ${ABIs[@]}; do
|
||||||
|
echo "Compiling for $abi..."
|
||||||
|
compile_for_arch $abi
|
||||||
|
done
|
||||||
|
fi
|
||||||
|
echo "Done."
|
||||||
|
fi
|
168
app/libgocryptfs/gocryptfs_internal/cryptocore/cryptocore.go
Normal file
168
app/libgocryptfs/gocryptfs_internal/cryptocore/cryptocore.go
Normal file
@ -0,0 +1,168 @@
|
|||||||
|
// Package cryptocore wraps OpenSSL and Go GCM crypto and provides
|
||||||
|
// a nonce generator.
|
||||||
|
package cryptocore
|
||||||
|
|
||||||
|
import (
|
||||||
|
"crypto/aes"
|
||||||
|
"crypto/cipher"
|
||||||
|
"crypto/sha512"
|
||||||
|
"fmt"
|
||||||
|
"log"
|
||||||
|
"runtime"
|
||||||
|
|
||||||
|
"../eme"
|
||||||
|
|
||||||
|
"../siv_aead"
|
||||||
|
"../stupidgcm"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
// KeyLen is the cipher key length in bytes. 32 for AES-256.
|
||||||
|
KeyLen = 32
|
||||||
|
// AuthTagLen is the length of a GCM auth tag in bytes.
|
||||||
|
AuthTagLen = 16
|
||||||
|
)
|
||||||
|
|
||||||
|
// AEADTypeEnum indicates the type of AEAD backend in use.
|
||||||
|
type AEADTypeEnum int
|
||||||
|
|
||||||
|
const (
|
||||||
|
// BackendOpenSSL specifies the OpenSSL backend.
|
||||||
|
BackendOpenSSL AEADTypeEnum = 3
|
||||||
|
// BackendGoGCM specifies the Go based GCM backend.
|
||||||
|
BackendGoGCM AEADTypeEnum = 4
|
||||||
|
// BackendAESSIV specifies an AESSIV backend.
|
||||||
|
BackendAESSIV AEADTypeEnum = 5
|
||||||
|
)
|
||||||
|
|
||||||
|
// CryptoCore is the low level crypto implementation.
|
||||||
|
type CryptoCore struct {
|
||||||
|
// EME is used for filename encryption.
|
||||||
|
EMECipher *eme.EMECipher
|
||||||
|
// GCM or AES-SIV. This is used for content encryption.
|
||||||
|
AEADCipher cipher.AEAD
|
||||||
|
// Which backend is behind AEADCipher?
|
||||||
|
AEADBackend AEADTypeEnum
|
||||||
|
// GCM needs unique IVs (nonces)
|
||||||
|
IVGenerator *nonceGenerator
|
||||||
|
IVLen int
|
||||||
|
}
|
||||||
|
|
||||||
|
// New returns a new CryptoCore object or panics.
|
||||||
|
//
|
||||||
|
// Even though the "GCMIV128" feature flag is now mandatory, we must still
|
||||||
|
// support 96-bit IVs here because they were used for encrypting the master
|
||||||
|
// key in gocryptfs.conf up to gocryptfs v1.2. v1.3 switched to 128 bits.
|
||||||
|
//
|
||||||
|
// Note: "key" is either the scrypt hash of the password (when decrypting
|
||||||
|
// a config file) or the masterkey (when finally mounting the filesystem).
|
||||||
|
func New(key []byte, aeadType AEADTypeEnum, IVBitLen int, useHKDF bool, forceDecode bool) *CryptoCore {
|
||||||
|
if len(key) != KeyLen {
|
||||||
|
log.Panic(fmt.Sprintf("Unsupported key length %d", len(key)))
|
||||||
|
}
|
||||||
|
// We want the IV size in bytes
|
||||||
|
IVLen := IVBitLen / 8
|
||||||
|
|
||||||
|
// Initialize EME for filename encryption.
|
||||||
|
var emeCipher *eme.EMECipher
|
||||||
|
var err error
|
||||||
|
{
|
||||||
|
var emeBlockCipher cipher.Block
|
||||||
|
if useHKDF {
|
||||||
|
emeKey := HkdfDerive(key, HkdfInfoEMENames, KeyLen)
|
||||||
|
emeBlockCipher, err = aes.NewCipher(emeKey)
|
||||||
|
for i := range emeKey {
|
||||||
|
emeKey[i] = 0
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
emeBlockCipher, err = aes.NewCipher(key)
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
log.Panic(err)
|
||||||
|
}
|
||||||
|
emeCipher = eme.New(emeBlockCipher)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Initialize an AEAD cipher for file content encryption.
|
||||||
|
var aeadCipher cipher.AEAD
|
||||||
|
if aeadType == BackendOpenSSL || aeadType == BackendGoGCM {
|
||||||
|
var gcmKey []byte
|
||||||
|
if useHKDF {
|
||||||
|
gcmKey = HkdfDerive(key, hkdfInfoGCMContent, KeyLen)
|
||||||
|
} else {
|
||||||
|
gcmKey = append([]byte{}, key...)
|
||||||
|
}
|
||||||
|
switch aeadType {
|
||||||
|
case BackendOpenSSL:
|
||||||
|
if IVLen != 16 {
|
||||||
|
log.Panic("stupidgcm only supports 128-bit IVs")
|
||||||
|
}
|
||||||
|
aeadCipher = stupidgcm.New(gcmKey, forceDecode)
|
||||||
|
case BackendGoGCM:
|
||||||
|
goGcmBlockCipher, err := aes.NewCipher(gcmKey)
|
||||||
|
if err != nil {
|
||||||
|
log.Panic(err)
|
||||||
|
}
|
||||||
|
aeadCipher, err = cipher.NewGCMWithNonceSize(goGcmBlockCipher, IVLen)
|
||||||
|
if err != nil {
|
||||||
|
log.Panic(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for i := range gcmKey {
|
||||||
|
gcmKey[i] = 0
|
||||||
|
}
|
||||||
|
} else if aeadType == BackendAESSIV {
|
||||||
|
if IVLen != 16 {
|
||||||
|
// SIV supports any nonce size, but we only use 16.
|
||||||
|
log.Panic("AES-SIV must use 16-byte nonces")
|
||||||
|
}
|
||||||
|
// AES-SIV uses 1/2 of the key for authentication, 1/2 for
|
||||||
|
// encryption, so we need a 64-bytes key for AES-256. Derive it from
|
||||||
|
// the 32-byte master key using HKDF, or, for older filesystems, with
|
||||||
|
// SHA256.
|
||||||
|
var key64 []byte
|
||||||
|
if useHKDF {
|
||||||
|
key64 = HkdfDerive(key, hkdfInfoSIVContent, siv_aead.KeyLen)
|
||||||
|
} else {
|
||||||
|
s := sha512.Sum512(key)
|
||||||
|
key64 = s[:]
|
||||||
|
}
|
||||||
|
aeadCipher = siv_aead.New(key64)
|
||||||
|
for i := range key64 {
|
||||||
|
key64[i] = 0
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
log.Panic("unknown backend cipher")
|
||||||
|
}
|
||||||
|
return &CryptoCore{
|
||||||
|
EMECipher: emeCipher,
|
||||||
|
AEADCipher: aeadCipher,
|
||||||
|
AEADBackend: aeadType,
|
||||||
|
IVGenerator: &nonceGenerator{nonceLen: IVLen},
|
||||||
|
IVLen: IVLen,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
type wiper interface {
|
||||||
|
Wipe()
|
||||||
|
}
|
||||||
|
|
||||||
|
// Wipe tries to wipe secret keys from memory by overwriting them with zeros
|
||||||
|
// and/or setting references to nil.
|
||||||
|
//
|
||||||
|
// This is not bulletproof due to possible GC copies, but
|
||||||
|
// still raises to bar for extracting the key.
|
||||||
|
func (c *CryptoCore) Wipe() {
|
||||||
|
be := c.AEADBackend
|
||||||
|
if be == BackendOpenSSL || be == BackendAESSIV {
|
||||||
|
// We don't use "x, ok :=" because we *want* to crash loudly if the
|
||||||
|
// type assertion fails.
|
||||||
|
w := c.AEADCipher.(wiper)
|
||||||
|
w.Wipe()
|
||||||
|
}
|
||||||
|
// We have no access to the keys (or key-equivalents) stored inside the
|
||||||
|
// Go stdlib. Best we can is to nil the references and force a GC.
|
||||||
|
c.AEADCipher = nil
|
||||||
|
c.EMECipher = nil
|
||||||
|
runtime.GC()
|
||||||
|
}
|
29
app/libgocryptfs/gocryptfs_internal/cryptocore/hkdf.go
Normal file
29
app/libgocryptfs/gocryptfs_internal/cryptocore/hkdf.go
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
package cryptocore
|
||||||
|
|
||||||
|
import (
|
||||||
|
"crypto/sha256"
|
||||||
|
"log"
|
||||||
|
|
||||||
|
"golang.org/x/crypto/hkdf"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
// "info" data that HKDF mixes into the generated key to make it unique.
|
||||||
|
// For convenience, we use a readable string.
|
||||||
|
HkdfInfoEMENames = "EME filename encryption"
|
||||||
|
hkdfInfoGCMContent = "AES-GCM file content encryption"
|
||||||
|
hkdfInfoSIVContent = "AES-SIV file content encryption"
|
||||||
|
)
|
||||||
|
|
||||||
|
// hkdfDerive derives "outLen" bytes from "masterkey" and "info" using
|
||||||
|
// HKDF-SHA256 (RFC 5869).
|
||||||
|
// It returns the derived bytes or panics.
|
||||||
|
func HkdfDerive(masterkey []byte, info string, outLen int) (out []byte) {
|
||||||
|
h := hkdf.New(sha256.New, masterkey, nil, []byte(info))
|
||||||
|
out = make([]byte, outLen)
|
||||||
|
n, err := h.Read(out)
|
||||||
|
if n != outLen || err != nil {
|
||||||
|
log.Panicf("hkdfDerive: hkdf read failed, got %d bytes, error: %v", n, err)
|
||||||
|
}
|
||||||
|
return out
|
||||||
|
}
|
32
app/libgocryptfs/gocryptfs_internal/cryptocore/nonce.go
Normal file
32
app/libgocryptfs/gocryptfs_internal/cryptocore/nonce.go
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
package cryptocore
|
||||||
|
|
||||||
|
import (
|
||||||
|
"crypto/rand"
|
||||||
|
"encoding/binary"
|
||||||
|
"log"
|
||||||
|
)
|
||||||
|
|
||||||
|
// RandBytes gets "n" random bytes from /dev/urandom or panics
|
||||||
|
func RandBytes(n int) []byte {
|
||||||
|
b := make([]byte, n)
|
||||||
|
_, err := rand.Read(b)
|
||||||
|
if err != nil {
|
||||||
|
log.Panic("Failed to read random bytes: " + err.Error())
|
||||||
|
}
|
||||||
|
return b
|
||||||
|
}
|
||||||
|
|
||||||
|
// RandUint64 returns a secure random uint64
|
||||||
|
func RandUint64() uint64 {
|
||||||
|
b := RandBytes(8)
|
||||||
|
return binary.BigEndian.Uint64(b)
|
||||||
|
}
|
||||||
|
|
||||||
|
type nonceGenerator struct {
|
||||||
|
nonceLen int // bytes
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get a random "nonceLen"-byte nonce
|
||||||
|
func (n *nonceGenerator) Get() []byte {
|
||||||
|
return randPrefetcher.read(n.nonceLen)
|
||||||
|
}
|
@ -0,0 +1,55 @@
|
|||||||
|
package cryptocore
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"log"
|
||||||
|
"sync"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Number of bytes to prefetch.
|
||||||
|
// 512 looks like a good compromise between throughput and latency - see
|
||||||
|
// randsize_test.go for numbers.
|
||||||
|
const prefetchN = 512
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
randPrefetcher.refill = make(chan []byte)
|
||||||
|
go randPrefetcher.refillWorker()
|
||||||
|
}
|
||||||
|
|
||||||
|
type randPrefetcherT struct {
|
||||||
|
sync.Mutex
|
||||||
|
buf bytes.Buffer
|
||||||
|
refill chan []byte
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *randPrefetcherT) read(want int) (out []byte) {
|
||||||
|
out = make([]byte, want)
|
||||||
|
r.Lock()
|
||||||
|
// Note: don't use defer, it slows us down!
|
||||||
|
have, err := r.buf.Read(out)
|
||||||
|
if have == want && err == nil {
|
||||||
|
r.Unlock()
|
||||||
|
return out
|
||||||
|
}
|
||||||
|
// Buffer was empty -> re-fill
|
||||||
|
fresh := <-r.refill
|
||||||
|
if len(fresh) != prefetchN {
|
||||||
|
log.Panicf("randPrefetcher: refill: got %d bytes instead of %d", len(fresh), prefetchN)
|
||||||
|
}
|
||||||
|
r.buf.Reset()
|
||||||
|
r.buf.Write(fresh)
|
||||||
|
have, err = r.buf.Read(out)
|
||||||
|
if have != want || err != nil {
|
||||||
|
log.Panicf("randPrefetcher could not satisfy read: have=%d want=%d err=%v", have, want, err)
|
||||||
|
}
|
||||||
|
r.Unlock()
|
||||||
|
return out
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *randPrefetcherT) refillWorker() {
|
||||||
|
for {
|
||||||
|
r.refill <- RandBytes(prefetchN)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var randPrefetcher randPrefetcherT
|
11
app/libgocryptfs/gocryptfs_internal/eme/.travis.yml
Normal file
11
app/libgocryptfs/gocryptfs_internal/eme/.travis.yml
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
language: go
|
||||||
|
|
||||||
|
go:
|
||||||
|
- 1.11.x # Debian 10 "Buster"
|
||||||
|
- 1.12.x # Ubuntu 19.10
|
||||||
|
- 1.13.x # Debian 11 "Bullseye"
|
||||||
|
- stable
|
||||||
|
|
||||||
|
script:
|
||||||
|
- go build
|
||||||
|
- ./test.bash
|
21
app/libgocryptfs/gocryptfs_internal/eme/LICENSE
Normal file
21
app/libgocryptfs/gocryptfs_internal/eme/LICENSE
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
The MIT License (MIT)
|
||||||
|
|
||||||
|
Copyright (c) 2015 Jakob Unterwurzacher
|
||||||
|
|
||||||
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
of this software and associated documentation files (the "Software"), to deal
|
||||||
|
in the Software without restriction, including without limitation the rights
|
||||||
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
copies of the Software, and to permit persons to whom the Software is
|
||||||
|
furnished to do so, subject to the following conditions:
|
||||||
|
|
||||||
|
The above copyright notice and this permission notice shall be included in all
|
||||||
|
copies or substantial portions of the Software.
|
||||||
|
|
||||||
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
SOFTWARE.
|
111
app/libgocryptfs/gocryptfs_internal/eme/README.md
Normal file
111
app/libgocryptfs/gocryptfs_internal/eme/README.md
Normal file
@ -0,0 +1,111 @@
|
|||||||
|
EME for Go [![Build Status](https://travis-ci.org/rfjakob/eme.svg?branch=master)](https://travis-ci.org/rfjakob/eme) [![GoDoc](https://godoc.org/github.com/rfjakob/eme?status.svg)](https://godoc.org/github.com/rfjakob/eme) ![MIT License](https://img.shields.io/badge/license-MIT-blue.svg)
|
||||||
|
==========
|
||||||
|
|
||||||
|
**EME** (ECB-Mix-ECB or, clearer, **Encrypt-Mix-Encrypt**) is a wide-block
|
||||||
|
encryption mode developed by Halevi
|
||||||
|
and Rogaway in 2003 [[eme]](#eme).
|
||||||
|
|
||||||
|
EME uses multiple invocations of a block cipher to construct a new
|
||||||
|
cipher of bigger block size (in multiples of 16 bytes, up to 2048 bytes).
|
||||||
|
|
||||||
|
Quoting from the original [[eme]](#eme) paper:
|
||||||
|
|
||||||
|
> We describe a block-cipher mode of operation, EME, that turns an n-bit block cipher into
|
||||||
|
> a tweakable enciphering scheme that acts on strings of mn bits, where m ∈ [1..n]. The mode is
|
||||||
|
> parallelizable, but as serial-efficient as the non-parallelizable mode CMC [6]. EME can be used
|
||||||
|
> to solve the disk-sector encryption problem. The algorithm entails two layers of ECB encryption
|
||||||
|
> and a “lightweight mixing” in between. We prove EME secure, in the reduction-based sense of
|
||||||
|
> modern cryptography.
|
||||||
|
|
||||||
|
Figure 2 from the [[eme]](#eme) paper shows an overview of the transformation:
|
||||||
|
|
||||||
|
[![Figure 2 from [eme]](paper-eme-fig2.png)](#)
|
||||||
|
|
||||||
|
This is an implementation of EME in Go, complete with test vectors from IEEE [[p1619-2]](#p1619-2)
|
||||||
|
and Halevi [[eme-32-testvec]](#eme-32-testvec).
|
||||||
|
|
||||||
|
It has no dependencies outside the standard library.
|
||||||
|
|
||||||
|
Is it patentend?
|
||||||
|
----------------
|
||||||
|
|
||||||
|
In 2007, the UC Davis has decided to abandon [[patabandon]](#patabandon)
|
||||||
|
the patent application [[patappl]](#patappl) for EME.
|
||||||
|
|
||||||
|
Related algorithms
|
||||||
|
------------------
|
||||||
|
|
||||||
|
**EME-32** is EME with the cipher set to AES and the length set to 512.
|
||||||
|
That is, EME-32 [[eme-32-pdf]](#eme-32-pdf) is a subset of EME.
|
||||||
|
|
||||||
|
**EME2**, also known as EME\* [[emestar]](#emestar), is an extended version of EME
|
||||||
|
that has built-in handling for data that is not a multiple of 16 bytes
|
||||||
|
long.
|
||||||
|
EME2 has been selected for standardization in IEEE P1619.2 [[p1619.2]](#p1619.2).
|
||||||
|
|
||||||
|
References
|
||||||
|
----------
|
||||||
|
|
||||||
|
#### [eme]
|
||||||
|
*A Parallelizable Enciphering Mode*
|
||||||
|
Shai Halevi, Phillip Rogaway, 28 Jul 2003
|
||||||
|
https://eprint.iacr.org/2003/147.pdf
|
||||||
|
|
||||||
|
Note: This is the original EME paper. EME is specified for an arbitrary
|
||||||
|
number of block-cipher blocks. EME-32 is a concrete implementation of
|
||||||
|
EME with a fixed length of 32 AES blocks.
|
||||||
|
|
||||||
|
#### [eme-32-email]
|
||||||
|
*Re: EME-32-AES with editorial comments*
|
||||||
|
Shai Halevi, 07 Jun 2005
|
||||||
|
http://grouper.ieee.org/groups/1619/email/msg00310.html
|
||||||
|
|
||||||
|
#### [eme-32-pdf]
|
||||||
|
*Draft Standard for Tweakable Wide-block Encryption*
|
||||||
|
Shai Halevi, 02 June 2005
|
||||||
|
http://grouper.ieee.org/groups/1619/email/pdf00020.pdf
|
||||||
|
|
||||||
|
Note: This is the latest version of the EME-32 draft that I could find. It
|
||||||
|
includes test vectors and C source code.
|
||||||
|
|
||||||
|
#### [eme-32-testvec]
|
||||||
|
*Re: Test vectors for LRW and EME*
|
||||||
|
Shai Halevi, 16 Nov 2004
|
||||||
|
http://grouper.ieee.org/groups/1619/email/msg00218.html
|
||||||
|
|
||||||
|
#### [emestar]
|
||||||
|
*EME\*: extending EME to handle arbitrary-length messages with associated data*
|
||||||
|
Shai Halevi, 27 May 2004
|
||||||
|
https://eprint.iacr.org/2004/125.pdf
|
||||||
|
|
||||||
|
#### [patabandon]
|
||||||
|
*Re: [P1619-2] Non-awareness patent statement made by UC Davis*
|
||||||
|
Mat Ball, 26 Nov 2007
|
||||||
|
http://grouper.ieee.org/groups/1619/email-2/msg00005.html
|
||||||
|
|
||||||
|
#### [patappl]
|
||||||
|
*Block cipher mode of operation for constructing a wide-blocksize block cipher from a conventional block cipher*
|
||||||
|
US patent application US20040131182
|
||||||
|
http://www.google.com/patents/US20040131182
|
||||||
|
|
||||||
|
#### [p1619-2]
|
||||||
|
*IEEE P1619.2™/D9 Draft Standard for Wide-Block Encryption for Shared Storage Media*
|
||||||
|
IEEE, Dec 2008
|
||||||
|
http://siswg.net/index2.php?option=com_docman&task=doc_view&gid=156&Itemid=41
|
||||||
|
|
||||||
|
Note: This is a draft version. The final version is not freely available
|
||||||
|
and must be bought from IEEE.
|
||||||
|
|
||||||
|
Package Changelog
|
||||||
|
-----------------
|
||||||
|
|
||||||
|
v1.1.1, 2020-04-13
|
||||||
|
* Update `go vet` call in `test.bash` to work on recent Go versions
|
||||||
|
* No code changes
|
||||||
|
|
||||||
|
v1.1, 2017-03-05
|
||||||
|
* Add eme.New() / \*EMECipher convenience wrapper
|
||||||
|
* Improve panic message and parameter wording
|
||||||
|
|
||||||
|
v1.0, 2015-12-08
|
||||||
|
* Stable release
|
3
app/libgocryptfs/gocryptfs_internal/eme/benchmark.bash
Executable file
3
app/libgocryptfs/gocryptfs_internal/eme/benchmark.bash
Executable file
@ -0,0 +1,3 @@
|
|||||||
|
#!/bin/bash -eu
|
||||||
|
|
||||||
|
go test -bench=.
|
206
app/libgocryptfs/gocryptfs_internal/eme/eme.go
Normal file
206
app/libgocryptfs/gocryptfs_internal/eme/eme.go
Normal file
@ -0,0 +1,206 @@
|
|||||||
|
// EME (ECB-Mix-ECB or, clearer, Encrypt-Mix-Encrypt) is a wide-block
|
||||||
|
// encryption mode developed by Halevi and Rogaway.
|
||||||
|
//
|
||||||
|
// It was presented in the 2003 paper "A Parallelizable Enciphering Mode" by
|
||||||
|
// Halevi and Rogaway.
|
||||||
|
//
|
||||||
|
// EME uses multiple invocations of a block cipher to construct a new cipher
|
||||||
|
// of bigger block size (in multiples of 16 bytes, up to 2048 bytes).
|
||||||
|
package eme
|
||||||
|
|
||||||
|
import (
|
||||||
|
"crypto/cipher"
|
||||||
|
"log"
|
||||||
|
)
|
||||||
|
|
||||||
|
type directionConst bool
|
||||||
|
|
||||||
|
const (
|
||||||
|
// Encrypt "inputData"
|
||||||
|
DirectionEncrypt = directionConst(true)
|
||||||
|
// Decrypt "inputData"
|
||||||
|
DirectionDecrypt = directionConst(false)
|
||||||
|
)
|
||||||
|
|
||||||
|
// multByTwo - GF multiplication as specified in the EME-32 draft
|
||||||
|
func multByTwo(out []byte, in []byte) {
|
||||||
|
if len(in) != 16 {
|
||||||
|
panic("len must be 16")
|
||||||
|
}
|
||||||
|
tmp := make([]byte, 16)
|
||||||
|
|
||||||
|
tmp[0] = 2 * in[0]
|
||||||
|
if in[15] >= 128 {
|
||||||
|
tmp[0] = tmp[0] ^ 135
|
||||||
|
}
|
||||||
|
for j := 1; j < 16; j++ {
|
||||||
|
tmp[j] = 2 * in[j]
|
||||||
|
if in[j-1] >= 128 {
|
||||||
|
tmp[j] += 1
|
||||||
|
}
|
||||||
|
}
|
||||||
|
copy(out, tmp)
|
||||||
|
}
|
||||||
|
|
||||||
|
func xorBlocks(out []byte, in1 []byte, in2 []byte) {
|
||||||
|
if len(in1) != len(in2) {
|
||||||
|
log.Panicf("len(in1)=%d is not equal to len(in2)=%d", len(in1), len(in2))
|
||||||
|
}
|
||||||
|
|
||||||
|
for i := range in1 {
|
||||||
|
out[i] = in1[i] ^ in2[i]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// aesTransform - encrypt or decrypt (according to "direction") using block
|
||||||
|
// cipher "bc" (typically AES)
|
||||||
|
func aesTransform(dst []byte, src []byte, direction directionConst, bc cipher.Block) {
|
||||||
|
if direction == DirectionEncrypt {
|
||||||
|
bc.Encrypt(dst, src)
|
||||||
|
return
|
||||||
|
} else if direction == DirectionDecrypt {
|
||||||
|
bc.Decrypt(dst, src)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// tabulateL - calculate L_i for messages up to a length of m cipher blocks
|
||||||
|
func tabulateL(bc cipher.Block, m int) [][]byte {
|
||||||
|
/* set L0 = 2*AESenc(K; 0) */
|
||||||
|
eZero := make([]byte, 16)
|
||||||
|
Li := make([]byte, 16)
|
||||||
|
bc.Encrypt(Li, eZero)
|
||||||
|
|
||||||
|
LTable := make([][]byte, m)
|
||||||
|
// Allocate pool once and slice into m pieces in the loop
|
||||||
|
pool := make([]byte, m*16)
|
||||||
|
for i := 0; i < m; i++ {
|
||||||
|
multByTwo(Li, Li)
|
||||||
|
LTable[i] = pool[i*16 : (i+1)*16]
|
||||||
|
copy(LTable[i], Li)
|
||||||
|
}
|
||||||
|
return LTable
|
||||||
|
}
|
||||||
|
|
||||||
|
// Transform - EME-encrypt or EME-decrypt, according to "direction"
|
||||||
|
// (defined in the constants DirectionEncrypt and DirectionDecrypt).
|
||||||
|
// The data in "inputData" is en- or decrypted with the block ciper "bc" under
|
||||||
|
// "tweak" (also known as IV).
|
||||||
|
//
|
||||||
|
// The tweak is used to randomize the encryption in the same way as an
|
||||||
|
// IV. A use of this encryption mode envisioned by the authors of the
|
||||||
|
// algorithm was to encrypt each sector of a disk, with the tweak
|
||||||
|
// being the sector number. If you encipher the same data with the
|
||||||
|
// same tweak you will get the same ciphertext.
|
||||||
|
//
|
||||||
|
// The result is returned in a freshly allocated slice of the same
|
||||||
|
// size as inputData.
|
||||||
|
//
|
||||||
|
// Limitations:
|
||||||
|
// * The block cipher must have block size 16 (usually AES).
|
||||||
|
// * The size of "tweak" must be 16
|
||||||
|
// * "inputData" must be a multiple of 16 bytes long
|
||||||
|
// If any of these pre-conditions are not met, the function will panic.
|
||||||
|
//
|
||||||
|
// Note that you probably don't want to call this function directly and instead
|
||||||
|
// use eme.New(), which provides conventient wrappers.
|
||||||
|
func Transform(bc cipher.Block, tweak []byte, inputData []byte, direction directionConst) []byte {
|
||||||
|
// In the paper, the tweak is just called "T". Call it the same here to
|
||||||
|
// make following the paper easy.
|
||||||
|
T := tweak
|
||||||
|
// In the paper, the plaintext data is called "P" and the ciphertext is
|
||||||
|
// called "C". Because encryption and decryption are virtually identical,
|
||||||
|
// we share the code and always call the input data "P" and the output data
|
||||||
|
// "C", regardless of the direction.
|
||||||
|
P := inputData
|
||||||
|
|
||||||
|
if bc.BlockSize() != 16 {
|
||||||
|
log.Panicf("Using a block size other than 16 is not implemented")
|
||||||
|
}
|
||||||
|
if len(T) != 16 {
|
||||||
|
log.Panicf("Tweak must be 16 bytes long, is %d", len(T))
|
||||||
|
}
|
||||||
|
if len(P)%16 != 0 {
|
||||||
|
log.Panicf("Data P must be a multiple of 16 long, is %d", len(P))
|
||||||
|
}
|
||||||
|
m := len(P) / 16
|
||||||
|
if m == 0 || m > 16*8 {
|
||||||
|
log.Panicf("EME operates on 1 to %d block-cipher blocks, you passed %d", 16*8, m)
|
||||||
|
}
|
||||||
|
|
||||||
|
C := make([]byte, len(P))
|
||||||
|
|
||||||
|
LTable := tabulateL(bc, m)
|
||||||
|
|
||||||
|
PPj := make([]byte, 16)
|
||||||
|
for j := 0; j < m; j++ {
|
||||||
|
Pj := P[j*16 : (j+1)*16]
|
||||||
|
/* PPj = 2**(j-1)*L xor Pj */
|
||||||
|
xorBlocks(PPj, Pj, LTable[j])
|
||||||
|
/* PPPj = AESenc(K; PPj) */
|
||||||
|
aesTransform(C[j*16:(j+1)*16], PPj, direction, bc)
|
||||||
|
}
|
||||||
|
|
||||||
|
/* MP =(xorSum PPPj) xor T */
|
||||||
|
MP := make([]byte, 16)
|
||||||
|
xorBlocks(MP, C[0:16], T)
|
||||||
|
for j := 1; j < m; j++ {
|
||||||
|
xorBlocks(MP, MP, C[j*16:(j+1)*16])
|
||||||
|
}
|
||||||
|
|
||||||
|
/* MC = AESenc(K; MP) */
|
||||||
|
MC := make([]byte, 16)
|
||||||
|
aesTransform(MC, MP, direction, bc)
|
||||||
|
|
||||||
|
/* M = MP xor MC */
|
||||||
|
M := make([]byte, 16)
|
||||||
|
xorBlocks(M, MP, MC)
|
||||||
|
CCCj := make([]byte, 16)
|
||||||
|
for j := 1; j < m; j++ {
|
||||||
|
multByTwo(M, M)
|
||||||
|
/* CCCj = 2**(j-1)*M xor PPPj */
|
||||||
|
xorBlocks(CCCj, C[j*16:(j+1)*16], M)
|
||||||
|
copy(C[j*16:(j+1)*16], CCCj)
|
||||||
|
}
|
||||||
|
|
||||||
|
/* CCC1 = (xorSum CCCj) xor T xor MC */
|
||||||
|
CCC1 := make([]byte, 16)
|
||||||
|
xorBlocks(CCC1, MC, T)
|
||||||
|
for j := 1; j < m; j++ {
|
||||||
|
xorBlocks(CCC1, CCC1, C[j*16:(j+1)*16])
|
||||||
|
}
|
||||||
|
copy(C[0:16], CCC1)
|
||||||
|
|
||||||
|
for j := 0; j < m; j++ {
|
||||||
|
/* CCj = AES-enc(K; CCCj) */
|
||||||
|
aesTransform(C[j*16:(j+1)*16], C[j*16:(j+1)*16], direction, bc)
|
||||||
|
/* Cj = 2**(j-1)*L xor CCj */
|
||||||
|
xorBlocks(C[j*16:(j+1)*16], C[j*16:(j+1)*16], LTable[j])
|
||||||
|
}
|
||||||
|
|
||||||
|
return C
|
||||||
|
}
|
||||||
|
|
||||||
|
// EMECipher provides EME-Encryption and -Decryption functions that are more
|
||||||
|
// convenient than calling Transform directly.
|
||||||
|
type EMECipher struct {
|
||||||
|
bc cipher.Block
|
||||||
|
}
|
||||||
|
|
||||||
|
// New returns a new EMECipher object. "bc" must have a block size of 16,
|
||||||
|
// or subsequent calls to Encrypt and Decrypt will panic.
|
||||||
|
func New(bc cipher.Block) *EMECipher {
|
||||||
|
return &EMECipher{
|
||||||
|
bc: bc,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Encrypt is equivalent to calling Transform with direction=DirectionEncrypt.
|
||||||
|
func (e *EMECipher) Encrypt(tweak []byte, inputData []byte) []byte {
|
||||||
|
return Transform(e.bc, tweak, inputData, DirectionEncrypt)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Decrypt is equivalent to calling Transform with direction=DirectionDecrypt.
|
||||||
|
func (e *EMECipher) Decrypt(tweak []byte, inputData []byte) []byte {
|
||||||
|
return Transform(e.bc, tweak, inputData, DirectionDecrypt)
|
||||||
|
}
|
BIN
app/libgocryptfs/gocryptfs_internal/eme/paper-eme-fig2.png
Normal file
BIN
app/libgocryptfs/gocryptfs_internal/eme/paper-eme-fig2.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 9.4 KiB |
97
app/libgocryptfs/gocryptfs_internal/exitcodes/exitcodes.go
Normal file
97
app/libgocryptfs/gocryptfs_internal/exitcodes/exitcodes.go
Normal file
@ -0,0 +1,97 @@
|
|||||||
|
// Package exitcodes contains all well-defined exit codes that gocryptfs
|
||||||
|
// can return.
|
||||||
|
package exitcodes
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
// Usage - usage error like wrong cli syntax, wrong number of parameters.
|
||||||
|
Usage = 1
|
||||||
|
// 2 is reserved because it is used by Go panic
|
||||||
|
// 3 is reserved because it was used by earlier gocryptfs version as a generic
|
||||||
|
// "mount" error.
|
||||||
|
|
||||||
|
// CipherDir means that the CIPHERDIR does not exist, is not empty, or is not
|
||||||
|
// a directory.
|
||||||
|
CipherDir = 6
|
||||||
|
// Init is an error on filesystem init
|
||||||
|
Init = 7
|
||||||
|
// LoadConf is an error while loading gocryptfs.conf
|
||||||
|
LoadConf = 8
|
||||||
|
// ReadPassword means something went wrong reading the password
|
||||||
|
ReadPassword = 9
|
||||||
|
// MountPoint error means that the mountpoint is invalid (not empty etc).
|
||||||
|
MountPoint = 10
|
||||||
|
// Other error - please inspect the message
|
||||||
|
Other = 11
|
||||||
|
// PasswordIncorrect - the password was incorrect when mounting or when
|
||||||
|
// changing the password.
|
||||||
|
PasswordIncorrect = 12
|
||||||
|
// ScryptParams means that scrypt was called with invalid parameters
|
||||||
|
ScryptParams = 13
|
||||||
|
// MasterKey means that something went wrong when parsing the "-masterkey"
|
||||||
|
// command line option
|
||||||
|
MasterKey = 14
|
||||||
|
// SigInt means we got SIGINT
|
||||||
|
SigInt = 15
|
||||||
|
// PanicLogNotEmpty means the panic log was not empty when we were unmounted
|
||||||
|
PanicLogNotEmpty = 16
|
||||||
|
// ForkChild means forking the worker child failed
|
||||||
|
ForkChild = 17
|
||||||
|
// OpenSSL means you tried to enable OpenSSL, but we were compiled without it.
|
||||||
|
OpenSSL = 18
|
||||||
|
// FuseNewServer - this exit code means that the call to fuse.NewServer failed.
|
||||||
|
// This usually means that there was a problem executing fusermount, or
|
||||||
|
// fusermount could not attach the mountpoint to the kernel.
|
||||||
|
FuseNewServer = 19
|
||||||
|
// CtlSock - the control socket file could not be created.
|
||||||
|
CtlSock = 20
|
||||||
|
// Downgraded to a warning in gocryptfs v1.4
|
||||||
|
//PanicLogCreate = 21
|
||||||
|
|
||||||
|
// PasswordEmpty - we received an empty password
|
||||||
|
PasswordEmpty = 22
|
||||||
|
// OpenConf - the was an error opening the gocryptfs.conf file for reading
|
||||||
|
OpenConf = 23
|
||||||
|
// WriteConf - could not write the gocryptfs.conf
|
||||||
|
WriteConf = 24
|
||||||
|
// Profiler - error occurred when trying to write cpu or memory profile or
|
||||||
|
// execution trace
|
||||||
|
Profiler = 25
|
||||||
|
// FsckErrors - the filesystem check found errors
|
||||||
|
FsckErrors = 26
|
||||||
|
// DeprecatedFS - this filesystem is deprecated
|
||||||
|
DeprecatedFS = 27
|
||||||
|
// skip 28
|
||||||
|
// ExcludeError - an error occurred while processing "-exclude"
|
||||||
|
ExcludeError = 29
|
||||||
|
// DevNull means that /dev/null could not be opened
|
||||||
|
DevNull = 30
|
||||||
|
)
|
||||||
|
|
||||||
|
// Err wraps an error with an associated numeric exit code
|
||||||
|
type Err struct {
|
||||||
|
error
|
||||||
|
code int
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewErr returns an error containing "msg" and the exit code "code".
|
||||||
|
func NewErr(msg string, code int) Err {
|
||||||
|
return Err{
|
||||||
|
error: fmt.Errorf(msg),
|
||||||
|
code: code,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Exit extracts the numeric exit code from "err" (if available) and exits the
|
||||||
|
// application.
|
||||||
|
func Exit(err error) {
|
||||||
|
err2, ok := err.(Err)
|
||||||
|
if !ok {
|
||||||
|
os.Exit(Other)
|
||||||
|
}
|
||||||
|
os.Exit(err2.code)
|
||||||
|
}
|
22
app/libgocryptfs/gocryptfs_internal/jacobsa_crypto/.gitignore
vendored
Normal file
22
app/libgocryptfs/gocryptfs_internal/jacobsa_crypto/.gitignore
vendored
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
# Compiled Object files, Static and Dynamic libs (Shared Objects)
|
||||||
|
*.o
|
||||||
|
*.a
|
||||||
|
*.so
|
||||||
|
|
||||||
|
# Folders
|
||||||
|
_obj
|
||||||
|
_test
|
||||||
|
|
||||||
|
# Architecture specific extensions/prefixes
|
||||||
|
*.[568vq]
|
||||||
|
[568vq].out
|
||||||
|
|
||||||
|
*.cgo1.go
|
||||||
|
*.cgo2.c
|
||||||
|
_cgo_defun.c
|
||||||
|
_cgo_gotypes.go
|
||||||
|
_cgo_export.*
|
||||||
|
|
||||||
|
_testmain.go
|
||||||
|
|
||||||
|
*.exe
|
@ -0,0 +1,4 @@
|
|||||||
|
# Cf. http://docs.travis-ci.com/user/getting-started/
|
||||||
|
# Cf. http://docs.travis-ci.com/user/languages/go/
|
||||||
|
|
||||||
|
language: go
|
202
app/libgocryptfs/gocryptfs_internal/jacobsa_crypto/LICENSE
Normal file
202
app/libgocryptfs/gocryptfs_internal/jacobsa_crypto/LICENSE
Normal file
@ -0,0 +1,202 @@
|
|||||||
|
|
||||||
|
Apache License
|
||||||
|
Version 2.0, January 2004
|
||||||
|
http://www.apache.org/licenses/
|
||||||
|
|
||||||
|
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
||||||
|
|
||||||
|
1. Definitions.
|
||||||
|
|
||||||
|
"License" shall mean the terms and conditions for use, reproduction,
|
||||||
|
and distribution as defined by Sections 1 through 9 of this document.
|
||||||
|
|
||||||
|
"Licensor" shall mean the copyright owner or entity authorized by
|
||||||
|
the copyright owner that is granting the License.
|
||||||
|
|
||||||
|
"Legal Entity" shall mean the union of the acting entity and all
|
||||||
|
other entities that control, are controlled by, or are under common
|
||||||
|
control with that entity. For the purposes of this definition,
|
||||||
|
"control" means (i) the power, direct or indirect, to cause the
|
||||||
|
direction or management of such entity, whether by contract or
|
||||||
|
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
||||||
|
outstanding shares, or (iii) beneficial ownership of such entity.
|
||||||
|
|
||||||
|
"You" (or "Your") shall mean an individual or Legal Entity
|
||||||
|
exercising permissions granted by this License.
|
||||||
|
|
||||||
|
"Source" form shall mean the preferred form for making modifications,
|
||||||
|
including but not limited to software source code, documentation
|
||||||
|
source, and configuration files.
|
||||||
|
|
||||||
|
"Object" form shall mean any form resulting from mechanical
|
||||||
|
transformation or translation of a Source form, including but
|
||||||
|
not limited to compiled object code, generated documentation,
|
||||||
|
and conversions to other media types.
|
||||||
|
|
||||||
|
"Work" shall mean the work of authorship, whether in Source or
|
||||||
|
Object form, made available under the License, as indicated by a
|
||||||
|
copyright notice that is included in or attached to the work
|
||||||
|
(an example is provided in the Appendix below).
|
||||||
|
|
||||||
|
"Derivative Works" shall mean any work, whether in Source or Object
|
||||||
|
form, that is based on (or derived from) the Work and for which the
|
||||||
|
editorial revisions, annotations, elaborations, or other modifications
|
||||||
|
represent, as a whole, an original work of authorship. For the purposes
|
||||||
|
of this License, Derivative Works shall not include works that remain
|
||||||
|
separable from, or merely link (or bind by name) to the interfaces of,
|
||||||
|
the Work and Derivative Works thereof.
|
||||||
|
|
||||||
|
"Contribution" shall mean any work of authorship, including
|
||||||
|
the original version of the Work and any modifications or additions
|
||||||
|
to that Work or Derivative Works thereof, that is intentionally
|
||||||
|
submitted to Licensor for inclusion in the Work by the copyright owner
|
||||||
|
or by an individual or Legal Entity authorized to submit on behalf of
|
||||||
|
the copyright owner. For the purposes of this definition, "submitted"
|
||||||
|
means any form of electronic, verbal, or written communication sent
|
||||||
|
to the Licensor or its representatives, including but not limited to
|
||||||
|
communication on electronic mailing lists, source code control systems,
|
||||||
|
and issue tracking systems that are managed by, or on behalf of, the
|
||||||
|
Licensor for the purpose of discussing and improving the Work, but
|
||||||
|
excluding communication that is conspicuously marked or otherwise
|
||||||
|
designated in writing by the copyright owner as "Not a Contribution."
|
||||||
|
|
||||||
|
"Contributor" shall mean Licensor and any individual or Legal Entity
|
||||||
|
on behalf of whom a Contribution has been received by Licensor and
|
||||||
|
subsequently incorporated within the Work.
|
||||||
|
|
||||||
|
2. Grant of Copyright License. Subject to the terms and conditions of
|
||||||
|
this License, each Contributor hereby grants to You a perpetual,
|
||||||
|
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||||
|
copyright license to reproduce, prepare Derivative Works of,
|
||||||
|
publicly display, publicly perform, sublicense, and distribute the
|
||||||
|
Work and such Derivative Works in Source or Object form.
|
||||||
|
|
||||||
|
3. Grant of Patent License. Subject to the terms and conditions of
|
||||||
|
this License, each Contributor hereby grants to You a perpetual,
|
||||||
|
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||||
|
(except as stated in this section) patent license to make, have made,
|
||||||
|
use, offer to sell, sell, import, and otherwise transfer the Work,
|
||||||
|
where such license applies only to those patent claims licensable
|
||||||
|
by such Contributor that are necessarily infringed by their
|
||||||
|
Contribution(s) alone or by combination of their Contribution(s)
|
||||||
|
with the Work to which such Contribution(s) was submitted. If You
|
||||||
|
institute patent litigation against any entity (including a
|
||||||
|
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
||||||
|
or a Contribution incorporated within the Work constitutes direct
|
||||||
|
or contributory patent infringement, then any patent licenses
|
||||||
|
granted to You under this License for that Work shall terminate
|
||||||
|
as of the date such litigation is filed.
|
||||||
|
|
||||||
|
4. Redistribution. You may reproduce and distribute copies of the
|
||||||
|
Work or Derivative Works thereof in any medium, with or without
|
||||||
|
modifications, and in Source or Object form, provided that You
|
||||||
|
meet the following conditions:
|
||||||
|
|
||||||
|
(a) You must give any other recipients of the Work or
|
||||||
|
Derivative Works a copy of this License; and
|
||||||
|
|
||||||
|
(b) You must cause any modified files to carry prominent notices
|
||||||
|
stating that You changed the files; and
|
||||||
|
|
||||||
|
(c) You must retain, in the Source form of any Derivative Works
|
||||||
|
that You distribute, all copyright, patent, trademark, and
|
||||||
|
attribution notices from the Source form of the Work,
|
||||||
|
excluding those notices that do not pertain to any part of
|
||||||
|
the Derivative Works; and
|
||||||
|
|
||||||
|
(d) If the Work includes a "NOTICE" text file as part of its
|
||||||
|
distribution, then any Derivative Works that You distribute must
|
||||||
|
include a readable copy of the attribution notices contained
|
||||||
|
within such NOTICE file, excluding those notices that do not
|
||||||
|
pertain to any part of the Derivative Works, in at least one
|
||||||
|
of the following places: within a NOTICE text file distributed
|
||||||
|
as part of the Derivative Works; within the Source form or
|
||||||
|
documentation, if provided along with the Derivative Works; or,
|
||||||
|
within a display generated by the Derivative Works, if and
|
||||||
|
wherever such third-party notices normally appear. The contents
|
||||||
|
of the NOTICE file are for informational purposes only and
|
||||||
|
do not modify the License. You may add Your own attribution
|
||||||
|
notices within Derivative Works that You distribute, alongside
|
||||||
|
or as an addendum to the NOTICE text from the Work, provided
|
||||||
|
that such additional attribution notices cannot be construed
|
||||||
|
as modifying the License.
|
||||||
|
|
||||||
|
You may add Your own copyright statement to Your modifications and
|
||||||
|
may provide additional or different license terms and conditions
|
||||||
|
for use, reproduction, or distribution of Your modifications, or
|
||||||
|
for any such Derivative Works as a whole, provided Your use,
|
||||||
|
reproduction, and distribution of the Work otherwise complies with
|
||||||
|
the conditions stated in this License.
|
||||||
|
|
||||||
|
5. Submission of Contributions. Unless You explicitly state otherwise,
|
||||||
|
any Contribution intentionally submitted for inclusion in the Work
|
||||||
|
by You to the Licensor shall be under the terms and conditions of
|
||||||
|
this License, without any additional terms or conditions.
|
||||||
|
Notwithstanding the above, nothing herein shall supersede or modify
|
||||||
|
the terms of any separate license agreement you may have executed
|
||||||
|
with Licensor regarding such Contributions.
|
||||||
|
|
||||||
|
6. Trademarks. This License does not grant permission to use the trade
|
||||||
|
names, trademarks, service marks, or product names of the Licensor,
|
||||||
|
except as required for reasonable and customary use in describing the
|
||||||
|
origin of the Work and reproducing the content of the NOTICE file.
|
||||||
|
|
||||||
|
7. Disclaimer of Warranty. Unless required by applicable law or
|
||||||
|
agreed to in writing, Licensor provides the Work (and each
|
||||||
|
Contributor provides its Contributions) on an "AS IS" BASIS,
|
||||||
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
||||||
|
implied, including, without limitation, any warranties or conditions
|
||||||
|
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
||||||
|
PARTICULAR PURPOSE. You are solely responsible for determining the
|
||||||
|
appropriateness of using or redistributing the Work and assume any
|
||||||
|
risks associated with Your exercise of permissions under this License.
|
||||||
|
|
||||||
|
8. Limitation of Liability. In no event and under no legal theory,
|
||||||
|
whether in tort (including negligence), contract, or otherwise,
|
||||||
|
unless required by applicable law (such as deliberate and grossly
|
||||||
|
negligent acts) or agreed to in writing, shall any Contributor be
|
||||||
|
liable to You for damages, including any direct, indirect, special,
|
||||||
|
incidental, or consequential damages of any character arising as a
|
||||||
|
result of this License or out of the use or inability to use the
|
||||||
|
Work (including but not limited to damages for loss of goodwill,
|
||||||
|
work stoppage, computer failure or malfunction, or any and all
|
||||||
|
other commercial damages or losses), even if such Contributor
|
||||||
|
has been advised of the possibility of such damages.
|
||||||
|
|
||||||
|
9. Accepting Warranty or Additional Liability. While redistributing
|
||||||
|
the Work or Derivative Works thereof, You may choose to offer,
|
||||||
|
and charge a fee for, acceptance of support, warranty, indemnity,
|
||||||
|
or other liability obligations and/or rights consistent with this
|
||||||
|
License. However, in accepting such obligations, You may act only
|
||||||
|
on Your own behalf and on Your sole responsibility, not on behalf
|
||||||
|
of any other Contributor, and only if You agree to indemnify,
|
||||||
|
defend, and hold each Contributor harmless for any liability
|
||||||
|
incurred by, or claims asserted against, such Contributor by reason
|
||||||
|
of your accepting any such warranty or additional liability.
|
||||||
|
|
||||||
|
END OF TERMS AND CONDITIONS
|
||||||
|
|
||||||
|
APPENDIX: How to apply the Apache License to your work.
|
||||||
|
|
||||||
|
To apply the Apache License to your work, attach the following
|
||||||
|
boilerplate notice, with the fields enclosed by brackets "[]"
|
||||||
|
replaced with your own identifying information. (Don't include
|
||||||
|
the brackets!) The text should be enclosed in the appropriate
|
||||||
|
comment syntax for the file format. We also recommend that a
|
||||||
|
file or class name and description of purpose be included on the
|
||||||
|
same "printed page" as the copyright notice for easier
|
||||||
|
identification within third-party archives.
|
||||||
|
|
||||||
|
Copyright [yyyy] [name of copyright owner]
|
||||||
|
|
||||||
|
Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
you may not use this file except in compliance with the License.
|
||||||
|
You may obtain a copy of the License at
|
||||||
|
|
||||||
|
http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
|
||||||
|
Unless required by applicable law or agreed to in writing, software
|
||||||
|
distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
See the License for the specific language governing permissions and
|
||||||
|
limitations under the License.
|
10
app/libgocryptfs/gocryptfs_internal/jacobsa_crypto/README.md
Normal file
10
app/libgocryptfs/gocryptfs_internal/jacobsa_crypto/README.md
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
This repository contains Go packages related to cryptographic standards that are
|
||||||
|
not included in the Go standard library. These include:
|
||||||
|
|
||||||
|
* [SIV mode][siv], which provides deterministic encryption with
|
||||||
|
authentication.
|
||||||
|
|
||||||
|
* [CMAC][cmac], a message authentication system used by SIV mode.
|
||||||
|
|
||||||
|
[siv]: https://godoc.org/github.com/jacobsa/crypto/siv
|
||||||
|
[cmac]: https://godoc.org/github.com/jacobsa/crypto/cmac
|
@ -0,0 +1,23 @@
|
|||||||
|
// Copyright 2012 Aaron Jacobs. All Rights Reserved.
|
||||||
|
// Author: aaronjjacobs@gmail.com (Aaron Jacobs)
|
||||||
|
//
|
||||||
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
// you may not use this file except in compliance with the License.
|
||||||
|
// You may obtain a copy of the License at
|
||||||
|
//
|
||||||
|
// http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
//
|
||||||
|
// Unless required by applicable law or agreed to in writing, software
|
||||||
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
// See the License for the specific language governing permissions and
|
||||||
|
// limitations under the License.
|
||||||
|
|
||||||
|
package cmac
|
||||||
|
|
||||||
|
import "crypto/aes"
|
||||||
|
|
||||||
|
// The size of an AES-CMAC checksum, in bytes.
|
||||||
|
const Size = aes.BlockSize
|
||||||
|
|
||||||
|
const blockSize = Size
|
@ -0,0 +1,19 @@
|
|||||||
|
// Copyright 2012 Aaron Jacobs. All Rights Reserved.
|
||||||
|
// Author: aaronjjacobs@gmail.com (Aaron Jacobs)
|
||||||
|
//
|
||||||
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
// you may not use this file except in compliance with the License.
|
||||||
|
// You may obtain a copy of the License at
|
||||||
|
//
|
||||||
|
// http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
//
|
||||||
|
// Unless required by applicable law or agreed to in writing, software
|
||||||
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
// See the License for the specific language governing permissions and
|
||||||
|
// limitations under the License.
|
||||||
|
|
||||||
|
// Package cmac implements the CMAC mode for message authentication, as defined
|
||||||
|
// by NIST Special Publication 800-38B. When a 16-byte key is used, this
|
||||||
|
// matches the AES-CMAC algorithm defined by RFC 4493.
|
||||||
|
package cmac
|
170
app/libgocryptfs/gocryptfs_internal/jacobsa_crypto/cmac/hash.go
Normal file
170
app/libgocryptfs/gocryptfs_internal/jacobsa_crypto/cmac/hash.go
Normal file
@ -0,0 +1,170 @@
|
|||||||
|
// Copyright 2012 Aaron Jacobs. All Rights Reserved.
|
||||||
|
// Author: aaronjjacobs@gmail.com (Aaron Jacobs)
|
||||||
|
//
|
||||||
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
// you may not use this file except in compliance with the License.
|
||||||
|
// You may obtain a copy of the License at
|
||||||
|
//
|
||||||
|
// http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
//
|
||||||
|
// Unless required by applicable law or agreed to in writing, software
|
||||||
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
// See the License for the specific language governing permissions and
|
||||||
|
// limitations under the License.
|
||||||
|
|
||||||
|
package cmac
|
||||||
|
|
||||||
|
import (
|
||||||
|
"crypto/aes"
|
||||||
|
"crypto/cipher"
|
||||||
|
"fmt"
|
||||||
|
"hash"
|
||||||
|
"unsafe"
|
||||||
|
|
||||||
|
"../common"
|
||||||
|
)
|
||||||
|
|
||||||
|
type cmacHash struct {
|
||||||
|
// An AES cipher configured with the original key.
|
||||||
|
ciph cipher.Block
|
||||||
|
|
||||||
|
// Generated sub-keys.
|
||||||
|
k1 []byte
|
||||||
|
k2 []byte
|
||||||
|
|
||||||
|
// Data that has been seen by Write but not yet incorporated into x, due to
|
||||||
|
// us not being sure if it is the final block or not.
|
||||||
|
//
|
||||||
|
// INVARIANT: len(data) <= blockSize
|
||||||
|
data []byte
|
||||||
|
|
||||||
|
// The current value of X, as defined in the AES-CMAC algorithm in RFC 4493.
|
||||||
|
// Initially this is a 128-bit zero, and it is updated with the current block
|
||||||
|
// when we're sure it's not the last one.
|
||||||
|
x []byte
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *cmacHash) Write(p []byte) (n int, err error) {
|
||||||
|
n = len(p)
|
||||||
|
|
||||||
|
// First step: consume enough data to expand h.data to a full block, if
|
||||||
|
// possible.
|
||||||
|
{
|
||||||
|
toConsume := blockSize - len(h.data)
|
||||||
|
if toConsume > len(p) {
|
||||||
|
toConsume = len(p)
|
||||||
|
}
|
||||||
|
|
||||||
|
h.data = append(h.data, p[:toConsume]...)
|
||||||
|
p = p[toConsume:]
|
||||||
|
}
|
||||||
|
|
||||||
|
// If there's no data left in p, it means h.data might not be a full block.
|
||||||
|
// Even if it is, we're not sure it's the final block, which we must treat
|
||||||
|
// specially. So we must stop here.
|
||||||
|
if len(p) == 0 {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// h.data is a full block and is not the last; process it.
|
||||||
|
h.writeBlocks(h.data)
|
||||||
|
h.data = h.data[:0]
|
||||||
|
|
||||||
|
// Consume any further full blocks in p that we're sure aren't the last. Note
|
||||||
|
// that we're sure that len(p) is greater than zero here.
|
||||||
|
blocksToProcess := (len(p) - 1) / blockSize
|
||||||
|
bytesToProcess := blocksToProcess * blockSize
|
||||||
|
|
||||||
|
h.writeBlocks(p[:bytesToProcess])
|
||||||
|
p = p[bytesToProcess:]
|
||||||
|
|
||||||
|
// Store the rest for later.
|
||||||
|
h.data = append(h.data, p...)
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Process block-aligned data that we're sure does not contain the final block.
|
||||||
|
//
|
||||||
|
// REQUIRES: len(p) % blockSize == 0
|
||||||
|
func (h *cmacHash) writeBlocks(p []byte) {
|
||||||
|
y := make([]byte, blockSize)
|
||||||
|
|
||||||
|
for off := 0; off < len(p); off += blockSize {
|
||||||
|
block := p[off : off+blockSize]
|
||||||
|
|
||||||
|
xorBlock(
|
||||||
|
unsafe.Pointer(&y[0]),
|
||||||
|
unsafe.Pointer(&h.x[0]),
|
||||||
|
unsafe.Pointer(&block[0]))
|
||||||
|
|
||||||
|
h.ciph.Encrypt(h.x, y)
|
||||||
|
}
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *cmacHash) Sum(b []byte) []byte {
|
||||||
|
dataLen := len(h.data)
|
||||||
|
|
||||||
|
// We should have at most one block left.
|
||||||
|
if dataLen > blockSize {
|
||||||
|
panic(fmt.Sprintf("Unexpected data: %x", h.data))
|
||||||
|
}
|
||||||
|
|
||||||
|
// Calculate M_last.
|
||||||
|
mLast := make([]byte, blockSize)
|
||||||
|
if dataLen == blockSize {
|
||||||
|
common.Xor(mLast, h.data, h.k1)
|
||||||
|
} else {
|
||||||
|
// TODO(jacobsa): Accept a destination buffer in common.PadBlock and
|
||||||
|
// simplify this code.
|
||||||
|
common.Xor(mLast, common.PadBlock(h.data), h.k2)
|
||||||
|
}
|
||||||
|
|
||||||
|
y := make([]byte, blockSize)
|
||||||
|
common.Xor(y, mLast, h.x)
|
||||||
|
|
||||||
|
result := make([]byte, blockSize)
|
||||||
|
h.ciph.Encrypt(result, y)
|
||||||
|
|
||||||
|
b = append(b, result...)
|
||||||
|
return b
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *cmacHash) Reset() {
|
||||||
|
h.data = h.data[:0]
|
||||||
|
h.x = make([]byte, blockSize)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *cmacHash) Size() int {
|
||||||
|
return h.ciph.BlockSize()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *cmacHash) BlockSize() int {
|
||||||
|
return h.ciph.BlockSize()
|
||||||
|
}
|
||||||
|
|
||||||
|
// New returns an AES-CMAC hash using the supplied key. The key must be 16, 24,
|
||||||
|
// or 32 bytes long.
|
||||||
|
func New(key []byte) (hash.Hash, error) {
|
||||||
|
switch len(key) {
|
||||||
|
case 16, 24, 32:
|
||||||
|
default:
|
||||||
|
return nil, fmt.Errorf("AES-CMAC requires a 16-, 24-, or 32-byte key.")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create a cipher.
|
||||||
|
ciph, err := aes.NewCipher(key)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("aes.NewCipher: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Set up the hash object.
|
||||||
|
h := &cmacHash{ciph: ciph}
|
||||||
|
h.k1, h.k2 = generateSubkeys(ciph)
|
||||||
|
h.Reset()
|
||||||
|
|
||||||
|
return h, nil
|
||||||
|
}
|
@ -0,0 +1,47 @@
|
|||||||
|
// Copyright 2012 Aaron Jacobs. All Rights Reserved.
|
||||||
|
// Author: aaronjjacobs@gmail.com (Aaron Jacobs)
|
||||||
|
//
|
||||||
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
// you may not use this file except in compliance with the License.
|
||||||
|
// You may obtain a copy of the License at
|
||||||
|
//
|
||||||
|
// http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
//
|
||||||
|
// Unless required by applicable law or agreed to in writing, software
|
||||||
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
// See the License for the specific language governing permissions and
|
||||||
|
// limitations under the License.
|
||||||
|
|
||||||
|
// +build 386 arm,!arm64 mips mipsle
|
||||||
|
|
||||||
|
package cmac
|
||||||
|
|
||||||
|
import (
|
||||||
|
"log"
|
||||||
|
"unsafe"
|
||||||
|
)
|
||||||
|
|
||||||
|
// XOR the blockSize bytes starting at a and b, writing the result over dst.
|
||||||
|
func xorBlock(
|
||||||
|
dstPtr unsafe.Pointer,
|
||||||
|
aPtr unsafe.Pointer,
|
||||||
|
bPtr unsafe.Pointer) {
|
||||||
|
// Check assumptions. (These are compile-time constants, so this should
|
||||||
|
// compile out.)
|
||||||
|
const wordSize = unsafe.Sizeof(uintptr(0))
|
||||||
|
if blockSize != 4*wordSize {
|
||||||
|
log.Panicf("%d %d", blockSize, wordSize)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Convert.
|
||||||
|
a := (*[4]uintptr)(aPtr)
|
||||||
|
b := (*[4]uintptr)(bPtr)
|
||||||
|
dst := (*[4]uintptr)(dstPtr)
|
||||||
|
|
||||||
|
// Compute.
|
||||||
|
dst[0] = a[0] ^ b[0]
|
||||||
|
dst[1] = a[1] ^ b[1]
|
||||||
|
dst[2] = a[2] ^ b[2]
|
||||||
|
dst[3] = a[3] ^ b[3]
|
||||||
|
}
|
@ -0,0 +1,55 @@
|
|||||||
|
// Copyright 2012 Aaron Jacobs. All Rights Reserved.
|
||||||
|
// Author: aaronjjacobs@gmail.com (Aaron Jacobs)
|
||||||
|
//
|
||||||
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
// you may not use this file except in compliance with the License.
|
||||||
|
// You may obtain a copy of the License at
|
||||||
|
//
|
||||||
|
// http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
//
|
||||||
|
// Unless required by applicable law or agreed to in writing, software
|
||||||
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
// See the License for the specific language governing permissions and
|
||||||
|
// limitations under the License.
|
||||||
|
|
||||||
|
// +build amd64 arm64 ppc64 ppc64le s390x mips64 mips64le
|
||||||
|
|
||||||
|
// This code assumes that it's safe to perform unaligned word-sized loads. This is safe on:
|
||||||
|
// - arm64 per http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.den0024a/ch05s01s02.html
|
||||||
|
// - Section "5.5.8 Alignment Interrupt" of PowerPC Operating Environment Architecture Book III Version 2.02
|
||||||
|
// (the first PowerPC ISA version to include 64-bit), available from
|
||||||
|
// http://www.ibm.com/developerworks/systems/library/es-archguide-v2.html does not permit fixed-point loads
|
||||||
|
// or stores to generate exceptions on unaligned access
|
||||||
|
// - IBM mainframe's have allowed unaligned accesses since the System/370 arrived in 1970
|
||||||
|
// - On mips unaligned accesses are fixed up by the kernel per https://www.linux-mips.org/wiki/Alignment
|
||||||
|
// so performance might be quite bad but it will work.
|
||||||
|
|
||||||
|
package cmac
|
||||||
|
|
||||||
|
import (
|
||||||
|
"log"
|
||||||
|
"unsafe"
|
||||||
|
)
|
||||||
|
|
||||||
|
// XOR the blockSize bytes starting at a and b, writing the result over dst.
|
||||||
|
func xorBlock(
|
||||||
|
dstPtr unsafe.Pointer,
|
||||||
|
aPtr unsafe.Pointer,
|
||||||
|
bPtr unsafe.Pointer) {
|
||||||
|
// Check assumptions. (These are compile-time constants, so this should
|
||||||
|
// compile out.)
|
||||||
|
const wordSize = unsafe.Sizeof(uintptr(0))
|
||||||
|
if blockSize != 2*wordSize {
|
||||||
|
log.Panicf("%d %d", blockSize, wordSize)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Convert.
|
||||||
|
a := (*[2]uintptr)(aPtr)
|
||||||
|
b := (*[2]uintptr)(bPtr)
|
||||||
|
dst := (*[2]uintptr)(dstPtr)
|
||||||
|
|
||||||
|
// Compute.
|
||||||
|
dst[0] = a[0] ^ b[0]
|
||||||
|
dst[1] = a[1] ^ b[1]
|
||||||
|
}
|
@ -0,0 +1,65 @@
|
|||||||
|
// Copyright 2012 Aaron Jacobs. All Rights Reserved.
|
||||||
|
// Author: aaronjjacobs@gmail.com (Aaron Jacobs)
|
||||||
|
//
|
||||||
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
// you may not use this file except in compliance with the License.
|
||||||
|
// You may obtain a copy of the License at
|
||||||
|
//
|
||||||
|
// http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
//
|
||||||
|
// Unless required by applicable law or agreed to in writing, software
|
||||||
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
// See the License for the specific language governing permissions and
|
||||||
|
// limitations under the License.
|
||||||
|
|
||||||
|
package cmac
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"crypto/cipher"
|
||||||
|
|
||||||
|
"../common"
|
||||||
|
)
|
||||||
|
|
||||||
|
var subkeyZero []byte
|
||||||
|
var subkeyRb []byte
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
subkeyZero = bytes.Repeat([]byte{0x00}, blockSize)
|
||||||
|
subkeyRb = append(bytes.Repeat([]byte{0x00}, blockSize-1), 0x87)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Given the supplied cipher, whose block size must be 16 bytes, return two
|
||||||
|
// subkeys that can be used in MAC generation. See section 5.3 of NIST SP
|
||||||
|
// 800-38B. Note that the other NIST-approved block size of 8 bytes is not
|
||||||
|
// supported by this function.
|
||||||
|
func generateSubkeys(ciph cipher.Block) (k1 []byte, k2 []byte) {
|
||||||
|
if ciph.BlockSize() != blockSize {
|
||||||
|
panic("generateSubkeys requires a cipher with a block size of 16 bytes.")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Step 1
|
||||||
|
l := make([]byte, blockSize)
|
||||||
|
ciph.Encrypt(l, subkeyZero)
|
||||||
|
|
||||||
|
// Step 2: Derive the first subkey.
|
||||||
|
if common.Msb(l) == 0 {
|
||||||
|
// TODO(jacobsa): Accept a destination buffer in ShiftLeft and then hoist
|
||||||
|
// the allocation in the else branch below.
|
||||||
|
k1 = common.ShiftLeft(l)
|
||||||
|
} else {
|
||||||
|
k1 = make([]byte, blockSize)
|
||||||
|
common.Xor(k1, common.ShiftLeft(l), subkeyRb)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Step 3: Derive the second subkey.
|
||||||
|
if common.Msb(k1) == 0 {
|
||||||
|
k2 = common.ShiftLeft(k1)
|
||||||
|
} else {
|
||||||
|
k2 = make([]byte, blockSize)
|
||||||
|
common.Xor(k2, common.ShiftLeft(k1), subkeyRb)
|
||||||
|
}
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
@ -0,0 +1,18 @@
|
|||||||
|
// Copyright 2012 Aaron Jacobs. All Rights Reserved.
|
||||||
|
// Author: aaronjjacobs@gmail.com (Aaron Jacobs)
|
||||||
|
//
|
||||||
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
// you may not use this file except in compliance with the License.
|
||||||
|
// You may obtain a copy of the License at
|
||||||
|
//
|
||||||
|
// http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
//
|
||||||
|
// Unless required by applicable law or agreed to in writing, software
|
||||||
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
// See the License for the specific language governing permissions and
|
||||||
|
// limitations under the License.
|
||||||
|
|
||||||
|
// Package common contains common implementation details of other packages, and
|
||||||
|
// should not be used directly.
|
||||||
|
package common
|
@ -0,0 +1,26 @@
|
|||||||
|
// Copyright 2012 Aaron Jacobs. All Rights Reserved.
|
||||||
|
// Author: aaronjjacobs@gmail.com (Aaron Jacobs)
|
||||||
|
//
|
||||||
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
// you may not use this file except in compliance with the License.
|
||||||
|
// You may obtain a copy of the License at
|
||||||
|
//
|
||||||
|
// http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
//
|
||||||
|
// Unless required by applicable law or agreed to in writing, software
|
||||||
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
// See the License for the specific language governing permissions and
|
||||||
|
// limitations under the License.
|
||||||
|
|
||||||
|
package common
|
||||||
|
|
||||||
|
// Msb returns the most significant bit of the supplied data (which must be
|
||||||
|
// non-empty). This is the MSB(L) function of RFC 4493.
|
||||||
|
func Msb(buf []byte) uint8 {
|
||||||
|
if len(buf) == 0 {
|
||||||
|
panic("msb requires non-empty buffer.")
|
||||||
|
}
|
||||||
|
|
||||||
|
return buf[0] >> 7
|
||||||
|
}
|
@ -0,0 +1,36 @@
|
|||||||
|
// Copyright 2012 Aaron Jacobs. All Rights Reserved.
|
||||||
|
// Author: aaronjjacobs@gmail.com (Aaron Jacobs)
|
||||||
|
//
|
||||||
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
// you may not use this file except in compliance with the License.
|
||||||
|
// You may obtain a copy of the License at
|
||||||
|
//
|
||||||
|
// http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
//
|
||||||
|
// Unless required by applicable law or agreed to in writing, software
|
||||||
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
// See the License for the specific language governing permissions and
|
||||||
|
// limitations under the License.
|
||||||
|
|
||||||
|
package common
|
||||||
|
|
||||||
|
import (
|
||||||
|
"crypto/aes"
|
||||||
|
)
|
||||||
|
|
||||||
|
// PadBlock pads a string of bytes less than 16 bytes long to a full block size
|
||||||
|
// by appending a one bit followed by zero bits. This is the padding function
|
||||||
|
// used in RFCs 4493 and 5297.
|
||||||
|
func PadBlock(block []byte) []byte {
|
||||||
|
blockLen := len(block)
|
||||||
|
if blockLen >= aes.BlockSize {
|
||||||
|
panic("PadBlock input must be less than 16 bytes.")
|
||||||
|
}
|
||||||
|
|
||||||
|
result := make([]byte, aes.BlockSize)
|
||||||
|
copy(result, block)
|
||||||
|
result[blockLen] = 0x80
|
||||||
|
|
||||||
|
return result
|
||||||
|
}
|
@ -0,0 +1,37 @@
|
|||||||
|
// Copyright 2012 Aaron Jacobs. All Rights Reserved.
|
||||||
|
// Author: aaronjjacobs@gmail.com (Aaron Jacobs)
|
||||||
|
//
|
||||||
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
// you may not use this file except in compliance with the License.
|
||||||
|
// You may obtain a copy of the License at
|
||||||
|
//
|
||||||
|
// http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
//
|
||||||
|
// Unless required by applicable law or agreed to in writing, software
|
||||||
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
// See the License for the specific language governing permissions and
|
||||||
|
// limitations under the License.
|
||||||
|
|
||||||
|
package common
|
||||||
|
|
||||||
|
// ShiftLeft shifts the binary string left by one bit, causing the
|
||||||
|
// most-signficant bit to disappear and a zero to be introduced at the right.
|
||||||
|
// This corresponds to the `x << 1` notation of RFC 4493.
|
||||||
|
func ShiftLeft(b []byte) []byte {
|
||||||
|
l := len(b)
|
||||||
|
if l == 0 {
|
||||||
|
panic("shiftLeft requires a non-empty buffer.")
|
||||||
|
}
|
||||||
|
|
||||||
|
output := make([]byte, l)
|
||||||
|
|
||||||
|
overflow := byte(0)
|
||||||
|
for i := int(l - 1); i >= 0; i-- {
|
||||||
|
output[i] = b[i] << 1
|
||||||
|
output[i] |= overflow
|
||||||
|
overflow = (b[i] & 0x80) >> 7
|
||||||
|
}
|
||||||
|
|
||||||
|
return output
|
||||||
|
}
|
@ -0,0 +1,33 @@
|
|||||||
|
// Copyright 2012 Aaron Jacobs. All Rights Reserved.
|
||||||
|
// Author: aaronjjacobs@gmail.com (Aaron Jacobs)
|
||||||
|
//
|
||||||
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
// you may not use this file except in compliance with the License.
|
||||||
|
// You may obtain a copy of the License at
|
||||||
|
//
|
||||||
|
// http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
//
|
||||||
|
// Unless required by applicable law or agreed to in writing, software
|
||||||
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
// See the License for the specific language governing permissions and
|
||||||
|
// limitations under the License.
|
||||||
|
|
||||||
|
package common
|
||||||
|
|
||||||
|
import "log"
|
||||||
|
|
||||||
|
// Xor computes `a XOR b`, as defined by RFC 4493. dst, a, and b must all have
|
||||||
|
// the same length.
|
||||||
|
func Xor(dst []byte, a []byte, b []byte) {
|
||||||
|
// TODO(jacobsa): Consider making this a helper function with known sizes
|
||||||
|
// where it is most hot, then even trying to inline it entirely.
|
||||||
|
|
||||||
|
if len(dst) != len(a) || len(a) != len(b) {
|
||||||
|
log.Panicf("Bad buffer lengths: %d, %d, %d", len(dst), len(a), len(b))
|
||||||
|
}
|
||||||
|
|
||||||
|
for i, _ := range a {
|
||||||
|
dst[i] = a[i] ^ b[i]
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,48 @@
|
|||||||
|
// Copyright 2012 Aaron Jacobs. All Rights Reserved.
|
||||||
|
// Author: aaronjjacobs@gmail.com (Aaron Jacobs)
|
||||||
|
//
|
||||||
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
// you may not use this file except in compliance with the License.
|
||||||
|
// You may obtain a copy of the License at
|
||||||
|
//
|
||||||
|
// http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
//
|
||||||
|
// Unless required by applicable law or agreed to in writing, software
|
||||||
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
// See the License for the specific language governing permissions and
|
||||||
|
// limitations under the License.
|
||||||
|
|
||||||
|
package siv
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"crypto/aes"
|
||||||
|
|
||||||
|
"../common"
|
||||||
|
)
|
||||||
|
|
||||||
|
var dblRb []byte
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
dblRb = append(bytes.Repeat([]byte{0x00}, 15), 0x87)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Given a 128-bit binary string, shift the string left by one bit and XOR the
|
||||||
|
// result with 0x00...87 if the bit shifted off was one. This is the dbl
|
||||||
|
// function of RFC 5297.
|
||||||
|
func dbl(b []byte) []byte {
|
||||||
|
if len(b) != aes.BlockSize {
|
||||||
|
panic("dbl requires a 16-byte buffer.")
|
||||||
|
}
|
||||||
|
|
||||||
|
shiftedOne := common.Msb(b) == 1
|
||||||
|
b = common.ShiftLeft(b)
|
||||||
|
if shiftedOne {
|
||||||
|
tmp := make([]byte, aes.BlockSize)
|
||||||
|
common.Xor(tmp, b, dblRb)
|
||||||
|
b = tmp
|
||||||
|
}
|
||||||
|
|
||||||
|
return b
|
||||||
|
}
|
@ -0,0 +1,103 @@
|
|||||||
|
// Copyright 2012 Aaron Jacobs. All Rights Reserved.
|
||||||
|
// Author: aaronjjacobs@gmail.com (Aaron Jacobs)
|
||||||
|
//
|
||||||
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
// you may not use this file except in compliance with the License.
|
||||||
|
// You may obtain a copy of the License at
|
||||||
|
//
|
||||||
|
// http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
//
|
||||||
|
// Unless required by applicable law or agreed to in writing, software
|
||||||
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
// See the License for the specific language governing permissions and
|
||||||
|
// limitations under the License.
|
||||||
|
|
||||||
|
package siv
|
||||||
|
|
||||||
|
import (
|
||||||
|
"crypto/aes"
|
||||||
|
"crypto/cipher"
|
||||||
|
"crypto/subtle"
|
||||||
|
"fmt"
|
||||||
|
)
|
||||||
|
|
||||||
|
// *NotAuthenticError is returned by Decrypt if the input is otherwise
|
||||||
|
// well-formed but the ciphertext doesn't check out as authentic. This could be
|
||||||
|
// due to an incorrect key, corrupted ciphertext, or incorrect/corrupted
|
||||||
|
// associated data.
|
||||||
|
type NotAuthenticError struct {
|
||||||
|
s string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (e *NotAuthenticError) Error() string {
|
||||||
|
return e.s
|
||||||
|
}
|
||||||
|
|
||||||
|
// Given ciphertext previously generated by Encrypt and the key and associated
|
||||||
|
// data that were used when generating the ciphertext, return the original
|
||||||
|
// plaintext given to Encrypt. If the input is well-formed but the key is
|
||||||
|
// incorrect, return an instance of WrongKeyError.
|
||||||
|
func Decrypt(key, ciphertext []byte, associated [][]byte) ([]byte, error) {
|
||||||
|
keyLen := len(key)
|
||||||
|
associatedLen := len(associated)
|
||||||
|
|
||||||
|
// The first 16 bytes of the ciphertext are the SIV.
|
||||||
|
if len(ciphertext) < aes.BlockSize {
|
||||||
|
return nil, fmt.Errorf("Invalid ciphertext; length must be at least 16.")
|
||||||
|
}
|
||||||
|
|
||||||
|
v := ciphertext[0:aes.BlockSize]
|
||||||
|
c := ciphertext[aes.BlockSize:]
|
||||||
|
|
||||||
|
// Make sure the key length is legal.
|
||||||
|
switch keyLen {
|
||||||
|
case 32, 48, 64:
|
||||||
|
default:
|
||||||
|
return nil, fmt.Errorf("SIV requires a 32-, 48-, or 64-byte key.")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Derive subkeys.
|
||||||
|
k1 := key[:keyLen/2]
|
||||||
|
k2 := key[keyLen/2:]
|
||||||
|
|
||||||
|
// Make sure the number of associated data is legal, per RFC 5297 section 7.
|
||||||
|
if associatedLen > 126 {
|
||||||
|
return nil, fmt.Errorf("len(associated) may be no more than 126.")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create a CTR cipher using a version of v with the 31st and 63rd bits
|
||||||
|
// zeroed out.
|
||||||
|
q := dup(v)
|
||||||
|
q[aes.BlockSize-4] &= 0x7f
|
||||||
|
q[aes.BlockSize-8] &= 0x7f
|
||||||
|
|
||||||
|
ciph, err := aes.NewCipher(k2)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("aes.NewCipher: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
ctrCiph := cipher.NewCTR(ciph, q)
|
||||||
|
|
||||||
|
// Decrypt the ciphertext.
|
||||||
|
plaintext := make([]byte, len(c))
|
||||||
|
ctrCiph.XORKeyStream(plaintext, c)
|
||||||
|
|
||||||
|
// Verify the SIV.
|
||||||
|
s2vStrings := make([][]byte, associatedLen+1)
|
||||||
|
copy(s2vStrings, associated)
|
||||||
|
s2vStrings[associatedLen] = plaintext
|
||||||
|
|
||||||
|
t := s2v(k1, s2vStrings, nil)
|
||||||
|
if len(t) != aes.BlockSize {
|
||||||
|
panic(fmt.Sprintf("Unexpected output of S2V: %v", t))
|
||||||
|
}
|
||||||
|
|
||||||
|
if subtle.ConstantTimeCompare(t, v) != 1 {
|
||||||
|
return nil, &NotAuthenticError{
|
||||||
|
"Couldn't validate the authenticity of the ciphertext and " +
|
||||||
|
"associated data."}
|
||||||
|
}
|
||||||
|
|
||||||
|
return plaintext, nil
|
||||||
|
}
|
@ -0,0 +1,21 @@
|
|||||||
|
// Copyright 2012 Aaron Jacobs. All Rights Reserved.
|
||||||
|
// Author: aaronjjacobs@gmail.com (Aaron Jacobs)
|
||||||
|
//
|
||||||
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
// you may not use this file except in compliance with the License.
|
||||||
|
// You may obtain a copy of the License at
|
||||||
|
//
|
||||||
|
// http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
//
|
||||||
|
// Unless required by applicable law or agreed to in writing, software
|
||||||
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
// See the License for the specific language governing permissions and
|
||||||
|
// limitations under the License.
|
||||||
|
|
||||||
|
// Package siv implements the SIV (Synthetic Initialization Vector) mode of
|
||||||
|
// AES, as defined by RFC 5297.
|
||||||
|
//
|
||||||
|
// This mode offers the choice of deterministic authenticated encryption or
|
||||||
|
// nonce-based, misuse-resistant authenticated encryption.
|
||||||
|
package siv
|
@ -0,0 +1,124 @@
|
|||||||
|
// Copyright 2012 Aaron Jacobs. All Rights Reserved.
|
||||||
|
// Author: aaronjjacobs@gmail.com (Aaron Jacobs)
|
||||||
|
//
|
||||||
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
// you may not use this file except in compliance with the License.
|
||||||
|
// You may obtain a copy of the License at
|
||||||
|
//
|
||||||
|
// http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
//
|
||||||
|
// Unless required by applicable law or agreed to in writing, software
|
||||||
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
// See the License for the specific language governing permissions and
|
||||||
|
// limitations under the License.
|
||||||
|
|
||||||
|
package siv
|
||||||
|
|
||||||
|
import (
|
||||||
|
"crypto/aes"
|
||||||
|
"crypto/cipher"
|
||||||
|
"fmt"
|
||||||
|
)
|
||||||
|
|
||||||
|
func dup(d []byte) []byte {
|
||||||
|
result := make([]byte, len(d))
|
||||||
|
copy(result, d)
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
// Given a key and plaintext, encrypt the plaintext using the SIV mode of AES,
|
||||||
|
// as defined by RFC 5297, append the result (including both the synthetic
|
||||||
|
// initialization vector and the ciphertext) to dst, and return the updated
|
||||||
|
// slice. The output can later be fed to Decrypt to recover the plaintext.
|
||||||
|
//
|
||||||
|
// In addition to confidentiality, this function also offers authenticity. That
|
||||||
|
// is, without the secret key an attacker is unable to construct a byte string
|
||||||
|
// that Decrypt will accept.
|
||||||
|
//
|
||||||
|
// The supplied key must be 32, 48, or 64 bytes long.
|
||||||
|
//
|
||||||
|
// The supplied associated data, up to 126 strings, is also authenticated,
|
||||||
|
// though it is not included in the ciphertext. The user must supply the same
|
||||||
|
// associated data to Decrypt in order for the Decrypt call to succeed. If no
|
||||||
|
// associated data is desired, pass an empty slice.
|
||||||
|
//
|
||||||
|
// If the same key, plaintext, and associated data are supplied to this
|
||||||
|
// function multiple times, the output is guaranteed to be identical. As per
|
||||||
|
// RFC 5297 section 3, you may use this function for nonce-based authenticated
|
||||||
|
// encryption by passing a nonce as the last associated data element.
|
||||||
|
func Encrypt(dst, key, plaintext []byte, associated [][]byte) ([]byte, error) {
|
||||||
|
keyLen := len(key)
|
||||||
|
associatedLen := len(associated)
|
||||||
|
|
||||||
|
// The output will consist of the current contents of dst, followed by the IV
|
||||||
|
// generated by s2v, followed by the ciphertext (which is the same size as
|
||||||
|
// the plaintext).
|
||||||
|
//
|
||||||
|
// Make sure dst is long enough, then carve it up.
|
||||||
|
var iv []byte
|
||||||
|
var ciphertext []byte
|
||||||
|
{
|
||||||
|
dstSize := len(dst)
|
||||||
|
dstAndIVSize := dstSize + s2vSize
|
||||||
|
outputSize := dstAndIVSize + len(plaintext)
|
||||||
|
|
||||||
|
if cap(dst) < outputSize {
|
||||||
|
tmp := make([]byte, dstSize, outputSize+outputSize/4)
|
||||||
|
copy(tmp, dst)
|
||||||
|
dst = tmp
|
||||||
|
}
|
||||||
|
|
||||||
|
dst = dst[:outputSize]
|
||||||
|
iv = dst[dstSize:dstAndIVSize]
|
||||||
|
ciphertext = dst[dstAndIVSize:outputSize]
|
||||||
|
}
|
||||||
|
|
||||||
|
// Make sure the key length is legal.
|
||||||
|
switch keyLen {
|
||||||
|
case 32, 48, 64:
|
||||||
|
default:
|
||||||
|
return nil, fmt.Errorf("SIV requires a 32-, 48-, or 64-byte key.")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Make sure the number of associated data is legal, per RFC 5297 section 7.
|
||||||
|
if associatedLen > 126 {
|
||||||
|
return nil, fmt.Errorf("len(associated) may be no more than 126.")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Derive subkeys.
|
||||||
|
k1 := key[:keyLen/2]
|
||||||
|
k2 := key[keyLen/2:]
|
||||||
|
|
||||||
|
// Call S2V to derive the synthetic initialization vector. Use the ciphertext
|
||||||
|
// output buffer as scratch space, since it's the same length as the final
|
||||||
|
// string.
|
||||||
|
s2vStrings := make([][]byte, associatedLen+1)
|
||||||
|
copy(s2vStrings, associated)
|
||||||
|
s2vStrings[associatedLen] = plaintext
|
||||||
|
|
||||||
|
v := s2v(k1, s2vStrings, ciphertext)
|
||||||
|
if len(v) != len(iv) {
|
||||||
|
panic(fmt.Sprintf("Unexpected vector: %v", v))
|
||||||
|
}
|
||||||
|
|
||||||
|
copy(iv, v)
|
||||||
|
|
||||||
|
// Create a CTR cipher using a version of v with the 31st and 63rd bits
|
||||||
|
// zeroed out.
|
||||||
|
q := dup(v)
|
||||||
|
q[aes.BlockSize-4] &= 0x7f
|
||||||
|
q[aes.BlockSize-8] &= 0x7f
|
||||||
|
|
||||||
|
ciph, err := aes.NewCipher(k2)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("aes.NewCipher: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
ctrCiph := cipher.NewCTR(ciph, q)
|
||||||
|
|
||||||
|
// Fill in the ciphertext.
|
||||||
|
ctrCiph.XORKeyStream(ciphertext, plaintext)
|
||||||
|
|
||||||
|
return dst, nil
|
||||||
|
}
|
@ -0,0 +1,98 @@
|
|||||||
|
// Copyright 2012 Aaron Jacobs. All Rights Reserved.
|
||||||
|
// Author: aaronjjacobs@gmail.com (Aaron Jacobs)
|
||||||
|
//
|
||||||
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
// you may not use this file except in compliance with the License.
|
||||||
|
// You may obtain a copy of the License at
|
||||||
|
//
|
||||||
|
// http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
//
|
||||||
|
// Unless required by applicable law or agreed to in writing, software
|
||||||
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
// See the License for the specific language governing permissions and
|
||||||
|
// limitations under the License.
|
||||||
|
|
||||||
|
package siv
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"crypto/aes"
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"../cmac"
|
||||||
|
"../common"
|
||||||
|
)
|
||||||
|
|
||||||
|
var s2vZero []byte
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
s2vZero = bytes.Repeat([]byte{0x00}, aes.BlockSize)
|
||||||
|
}
|
||||||
|
|
||||||
|
// The output size of the s2v function.
|
||||||
|
const s2vSize = cmac.Size
|
||||||
|
|
||||||
|
// Run the S2V "string to vector" function of RFC 5297 using the input key and
|
||||||
|
// string vector, which must be non-empty. (RFC 5297 defines S2V to handle the
|
||||||
|
// empty vector case, but it is never used that way by higher-level functions.)
|
||||||
|
//
|
||||||
|
// If provided, the supplied scatch space will be used to avoid an allocation.
|
||||||
|
// It should be (but is not required to be) as large as the last element of
|
||||||
|
// strings.
|
||||||
|
//
|
||||||
|
// The result is guaranteed to be of length s2vSize.
|
||||||
|
func s2v(key []byte, strings [][]byte, scratch []byte) []byte {
|
||||||
|
numStrings := len(strings)
|
||||||
|
if numStrings == 0 {
|
||||||
|
panic("strings vector must be non-empty.")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create a CMAC hash.
|
||||||
|
h, err := cmac.New(key)
|
||||||
|
if err != nil {
|
||||||
|
panic(fmt.Sprintf("cmac.New: %v", err))
|
||||||
|
}
|
||||||
|
|
||||||
|
// Initialize.
|
||||||
|
if _, err := h.Write(s2vZero); err != nil {
|
||||||
|
panic(fmt.Sprintf("h.Write: %v", err))
|
||||||
|
}
|
||||||
|
|
||||||
|
d := h.Sum([]byte{})
|
||||||
|
h.Reset()
|
||||||
|
|
||||||
|
// Handle all strings but the last.
|
||||||
|
for i := 0; i < numStrings-1; i++ {
|
||||||
|
if _, err := h.Write(strings[i]); err != nil {
|
||||||
|
panic(fmt.Sprintf("h.Write: %v", err))
|
||||||
|
}
|
||||||
|
|
||||||
|
common.Xor(d, dbl(d), h.Sum([]byte{}))
|
||||||
|
h.Reset()
|
||||||
|
}
|
||||||
|
|
||||||
|
// Handle the last string.
|
||||||
|
lastString := strings[numStrings-1]
|
||||||
|
var t []byte
|
||||||
|
if len(lastString) >= aes.BlockSize {
|
||||||
|
// Make an output buffer the length of lastString.
|
||||||
|
if cap(scratch) >= len(lastString) {
|
||||||
|
t = scratch[:len(lastString)]
|
||||||
|
} else {
|
||||||
|
t = make([]byte, len(lastString))
|
||||||
|
}
|
||||||
|
|
||||||
|
// XOR d on the end of lastString.
|
||||||
|
xorend(t, lastString, d)
|
||||||
|
} else {
|
||||||
|
t = make([]byte, aes.BlockSize)
|
||||||
|
common.Xor(t, dbl(d), common.PadBlock(lastString))
|
||||||
|
}
|
||||||
|
|
||||||
|
if _, err := h.Write(t); err != nil {
|
||||||
|
panic(fmt.Sprintf("h.Write: %v", err))
|
||||||
|
}
|
||||||
|
|
||||||
|
return h.Sum([]byte{})
|
||||||
|
}
|
@ -0,0 +1,44 @@
|
|||||||
|
// Copyright 2012 Aaron Jacobs. All Rights Reserved.
|
||||||
|
// Author: aaronjjacobs@gmail.com (Aaron Jacobs)
|
||||||
|
//
|
||||||
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
// you may not use this file except in compliance with the License.
|
||||||
|
// You may obtain a copy of the License at
|
||||||
|
//
|
||||||
|
// http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
//
|
||||||
|
// Unless required by applicable law or agreed to in writing, software
|
||||||
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
// See the License for the specific language governing permissions and
|
||||||
|
// limitations under the License.
|
||||||
|
|
||||||
|
package siv
|
||||||
|
|
||||||
|
import (
|
||||||
|
"log"
|
||||||
|
|
||||||
|
"../common"
|
||||||
|
)
|
||||||
|
|
||||||
|
// The xorend operator of RFC 5297.
|
||||||
|
//
|
||||||
|
// Given strings A and B with len(A) >= len(B), let D be len(A) - len(B). Write
|
||||||
|
// A[:D] followed by xor(A[D:], B) into dst. In other words, xor B over the
|
||||||
|
// rightmost end of A and write the result into dst.
|
||||||
|
func xorend(dst, a, b []byte) {
|
||||||
|
aLen := len(a)
|
||||||
|
bLen := len(b)
|
||||||
|
dstLen := len(dst)
|
||||||
|
|
||||||
|
if dstLen < aLen || aLen < bLen {
|
||||||
|
log.Panicf("Bad buffer lengths: %d, %d, %d", dstLen, aLen, bLen)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Copy the left part.
|
||||||
|
difference := aLen - bLen
|
||||||
|
copy(dst, a[:difference])
|
||||||
|
|
||||||
|
// XOR in the right part.
|
||||||
|
common.Xor(dst[difference:difference+bLen], a[difference:], b)
|
||||||
|
}
|
119
app/libgocryptfs/gocryptfs_internal/nametransform/diriv.go
Normal file
119
app/libgocryptfs/gocryptfs_internal/nametransform/diriv.go
Normal file
@ -0,0 +1,119 @@
|
|||||||
|
package nametransform
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
"syscall"
|
||||||
|
|
||||||
|
"../cryptocore"
|
||||||
|
"../../rewrites/syscallcompat"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
// DirIVLen is identical to AES block size
|
||||||
|
DirIVLen = 16
|
||||||
|
// DirIVFilename is the filename used to store directory IV.
|
||||||
|
// Exported because we have to ignore this name in directory listing.
|
||||||
|
DirIVFilename = "gocryptfs.diriv"
|
||||||
|
)
|
||||||
|
|
||||||
|
// ReadDirIVAt reads "gocryptfs.diriv" from the directory that is opened as "dirfd".
|
||||||
|
// Using the dirfd makes it immune to concurrent renames of the directory.
|
||||||
|
func ReadDirIVAt(dirfd int) (iv []byte, err error) {
|
||||||
|
fdRaw, err := syscallcompat.Openat(dirfd, DirIVFilename,
|
||||||
|
syscall.O_RDONLY|syscall.O_NOFOLLOW, 0)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
fd := os.NewFile(uintptr(fdRaw), DirIVFilename)
|
||||||
|
defer fd.Close()
|
||||||
|
return fdReadDirIV(fd)
|
||||||
|
}
|
||||||
|
|
||||||
|
// allZeroDirIV is preallocated to quickly check if the data read from disk is all zero
|
||||||
|
var allZeroDirIV = make([]byte, DirIVLen)
|
||||||
|
|
||||||
|
// fdReadDirIV reads and verifies the DirIV from an opened gocryptfs.diriv file.
|
||||||
|
func fdReadDirIV(fd *os.File) (iv []byte, err error) {
|
||||||
|
// We want to detect if the file is bigger than DirIVLen, so
|
||||||
|
// make the buffer 1 byte bigger than necessary.
|
||||||
|
iv = make([]byte, DirIVLen+1)
|
||||||
|
n, err := fd.Read(iv)
|
||||||
|
if err != nil && err != io.EOF {
|
||||||
|
return nil, fmt.Errorf("read failed: %v", err)
|
||||||
|
}
|
||||||
|
iv = iv[0:n]
|
||||||
|
if len(iv) != DirIVLen {
|
||||||
|
return nil, fmt.Errorf("wanted %d bytes, got %d", DirIVLen, len(iv))
|
||||||
|
}
|
||||||
|
if bytes.Equal(iv, allZeroDirIV) {
|
||||||
|
return nil, fmt.Errorf("diriv is all-zero")
|
||||||
|
}
|
||||||
|
return iv, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// WriteDirIVAt - create a new gocryptfs.diriv file in the directory opened at
|
||||||
|
// "dirfd". On error we try to delete the incomplete file.
|
||||||
|
// This function is exported because it is used from fusefrontend, main,
|
||||||
|
// and also the automated tests.
|
||||||
|
func WriteDirIVAt(dirfd int) error {
|
||||||
|
// It makes sense to have the diriv files group-readable so the FS can
|
||||||
|
// be mounted from several users from a network drive (see
|
||||||
|
// https://github.com/rfjakob/gocryptfs/issues/387 ).
|
||||||
|
//
|
||||||
|
// Note that gocryptfs.conf is still created with 0400 permissions so the
|
||||||
|
// owner must explicitly chmod it to permit access.
|
||||||
|
const dirivPerms = 0440
|
||||||
|
|
||||||
|
iv := cryptocore.RandBytes(DirIVLen)
|
||||||
|
// 0400 permissions: gocryptfs.diriv should never be modified after creation.
|
||||||
|
// Don't use "ioutil.WriteFile", it causes trouble on NFS:
|
||||||
|
// https://github.com/rfjakob/gocryptfs/commit/7d38f80a78644c8ec4900cc990bfb894387112ed
|
||||||
|
fd, err := syscallcompat.Openat(dirfd, DirIVFilename, os.O_WRONLY|os.O_CREATE|os.O_EXCL, dirivPerms)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
// Wrap the fd in an os.File - we need the write retry logic.
|
||||||
|
f := os.NewFile(uintptr(fd), DirIVFilename)
|
||||||
|
_, err = f.Write(iv)
|
||||||
|
if err != nil {
|
||||||
|
f.Close()
|
||||||
|
// Delete incomplete gocryptfs.diriv file
|
||||||
|
syscallcompat.Unlinkat(dirfd, DirIVFilename, 0)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
err = f.Close()
|
||||||
|
if err != nil {
|
||||||
|
// Delete incomplete gocryptfs.diriv file
|
||||||
|
syscallcompat.Unlinkat(dirfd, DirIVFilename, 0)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// encryptAndHashName encrypts "name" and hashes it to a longname if it is
|
||||||
|
// too long.
|
||||||
|
// Returns ENAMETOOLONG if "name" is longer than 255 bytes.
|
||||||
|
func (be *NameTransform) EncryptAndHashName(name string, iv []byte) (string, error) {
|
||||||
|
// Prevent the user from creating files longer than 255 chars.
|
||||||
|
if len(name) > NameMax {
|
||||||
|
return "", syscall.ENAMETOOLONG
|
||||||
|
}
|
||||||
|
cName := be.EncryptName(name, iv)
|
||||||
|
if be.longNames && len(cName) > NameMax {
|
||||||
|
return be.HashLongName(cName), nil
|
||||||
|
}
|
||||||
|
return cName, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Dir is like filepath.Dir but returns "" instead of ".".
|
||||||
|
func Dir(path string) string {
|
||||||
|
d := filepath.Dir(path)
|
||||||
|
if d == "." {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
return d
|
||||||
|
}
|
153
app/libgocryptfs/gocryptfs_internal/nametransform/longnames.go
Normal file
153
app/libgocryptfs/gocryptfs_internal/nametransform/longnames.go
Normal file
@ -0,0 +1,153 @@
|
|||||||
|
package nametransform
|
||||||
|
|
||||||
|
import (
|
||||||
|
"crypto/sha256"
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
"strings"
|
||||||
|
"syscall"
|
||||||
|
|
||||||
|
"../../rewrites/syscallcompat"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
// LongNameSuffix is the suffix used for files with long names.
|
||||||
|
// Files with long names are stored in two files:
|
||||||
|
// gocryptfs.longname.[sha256] <--- File content, prefix = gocryptfs.longname.
|
||||||
|
// gocryptfs.longname.[sha256].name <--- File name, suffix = .name
|
||||||
|
LongNameSuffix = ".name"
|
||||||
|
longNamePrefix = "gocryptfs.longname."
|
||||||
|
)
|
||||||
|
|
||||||
|
// HashLongName - take the hash of a long string "name" and return
|
||||||
|
// "gocryptfs.longname.[sha256]"
|
||||||
|
//
|
||||||
|
// This function does not do any I/O.
|
||||||
|
func (n *NameTransform) HashLongName(name string) string {
|
||||||
|
hashBin := sha256.Sum256([]byte(name))
|
||||||
|
hashBase64 := n.B64.EncodeToString(hashBin[:])
|
||||||
|
return longNamePrefix + hashBase64
|
||||||
|
}
|
||||||
|
|
||||||
|
// Values returned by IsLongName
|
||||||
|
const (
|
||||||
|
// LongNameContent is the file that stores the file content.
|
||||||
|
// Example: gocryptfs.longname.URrM8kgxTKYMgCk4hKk7RO9Lcfr30XQof4L_5bD9Iro=
|
||||||
|
LongNameContent = iota
|
||||||
|
// LongNameFilename is the file that stores the full encrypted filename.
|
||||||
|
// Example: gocryptfs.longname.URrM8kgxTKYMgCk4hKk7RO9Lcfr30XQof4L_5bD9Iro=.name
|
||||||
|
LongNameFilename = iota
|
||||||
|
// LongNameNone is used when the file does not have a long name.
|
||||||
|
// Example: i1bpTaVLZq7sRNA9mL_2Ig==
|
||||||
|
LongNameNone = iota
|
||||||
|
)
|
||||||
|
|
||||||
|
// NameType - detect if cName is
|
||||||
|
// gocryptfs.longname.[sha256] ........ LongNameContent (content of a long name file)
|
||||||
|
// gocryptfs.longname.[sha256].name .... LongNameFilename (full file name of a long name file)
|
||||||
|
// else ................................ LongNameNone (normal file)
|
||||||
|
//
|
||||||
|
// This function does not do any I/O.
|
||||||
|
func NameType(cName string) int {
|
||||||
|
if !strings.HasPrefix(cName, longNamePrefix) {
|
||||||
|
return LongNameNone
|
||||||
|
}
|
||||||
|
if strings.HasSuffix(cName, LongNameSuffix) {
|
||||||
|
return LongNameFilename
|
||||||
|
}
|
||||||
|
return LongNameContent
|
||||||
|
}
|
||||||
|
|
||||||
|
// IsLongContent returns true if "cName" is the content store of a long name
|
||||||
|
// file (looks like "gocryptfs.longname.[sha256]").
|
||||||
|
//
|
||||||
|
// This function does not do any I/O.
|
||||||
|
func IsLongContent(cName string) bool {
|
||||||
|
return NameType(cName) == LongNameContent
|
||||||
|
}
|
||||||
|
|
||||||
|
// RemoveLongNameSuffix removes the ".name" suffix from cName, returning the corresponding
|
||||||
|
// content file name.
|
||||||
|
// No check is made if cName actually is a LongNameFilename.
|
||||||
|
func RemoveLongNameSuffix(cName string) string {
|
||||||
|
return cName[:len(cName)-len(LongNameSuffix)]
|
||||||
|
}
|
||||||
|
|
||||||
|
// ReadLongName - read cName + ".name" from the directory opened as dirfd.
|
||||||
|
//
|
||||||
|
// Symlink-safe through Openat().
|
||||||
|
func ReadLongNameAt(dirfd int, cName string) (string, error) {
|
||||||
|
cName += LongNameSuffix
|
||||||
|
var f *os.File
|
||||||
|
{
|
||||||
|
fd, err := syscallcompat.Openat(dirfd, cName, syscall.O_RDONLY|syscall.O_NOFOLLOW, 0)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
f = os.NewFile(uintptr(fd), "")
|
||||||
|
// fd runs out of scope here
|
||||||
|
}
|
||||||
|
defer f.Close()
|
||||||
|
// 256 (=255 padded to 16) bytes base64-encoded take 344 bytes: "AAAAAAA...AAA=="
|
||||||
|
lim := 344
|
||||||
|
// Allocate a bigger buffer so we see whether the file is too big
|
||||||
|
buf := make([]byte, lim+1)
|
||||||
|
n, err := f.ReadAt(buf, 0)
|
||||||
|
if err != nil && err != io.EOF {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
if n == 0 {
|
||||||
|
return "", fmt.Errorf("ReadLongName: empty file")
|
||||||
|
}
|
||||||
|
if n > lim {
|
||||||
|
return "", fmt.Errorf("ReadLongName: size=%d > limit=%d", n, lim)
|
||||||
|
}
|
||||||
|
return string(buf[0:n]), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// DeleteLongName deletes "hashName.name" in the directory opened at "dirfd".
|
||||||
|
//
|
||||||
|
// This function is symlink-safe through the use of Unlinkat().
|
||||||
|
func DeleteLongNameAt(dirfd int, hashName string) error {
|
||||||
|
return syscallcompat.Unlinkat(dirfd, hashName+LongNameSuffix, 0)
|
||||||
|
}
|
||||||
|
|
||||||
|
// WriteLongName encrypts plainName and writes it into "hashName.name".
|
||||||
|
// For the convenience of the caller, plainName may also be a path and will be
|
||||||
|
// Base()named internally.
|
||||||
|
//
|
||||||
|
// This function is symlink-safe through the use of Openat().
|
||||||
|
func (n *NameTransform) WriteLongNameAt(dirfd int, hashName string, plainName string) (err error) {
|
||||||
|
plainName = filepath.Base(plainName)
|
||||||
|
|
||||||
|
// Encrypt the basename
|
||||||
|
dirIV, err := ReadDirIVAt(dirfd)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
cName := n.EncryptName(plainName, dirIV)
|
||||||
|
|
||||||
|
// Write the encrypted name into hashName.name
|
||||||
|
fdRaw, err := syscallcompat.Openat(dirfd, hashName+LongNameSuffix,
|
||||||
|
syscall.O_WRONLY|syscall.O_CREAT|syscall.O_EXCL, 0400)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
fd := os.NewFile(uintptr(fdRaw), hashName+LongNameSuffix)
|
||||||
|
_, err = fd.Write([]byte(cName))
|
||||||
|
if err != nil {
|
||||||
|
fd.Close()
|
||||||
|
// Delete incomplete longname file
|
||||||
|
syscallcompat.Unlinkat(dirfd, hashName+LongNameSuffix, 0)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
err = fd.Close()
|
||||||
|
if err != nil {
|
||||||
|
// Delete incomplete longname file
|
||||||
|
syscallcompat.Unlinkat(dirfd, hashName+LongNameSuffix, 0)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
133
app/libgocryptfs/gocryptfs_internal/nametransform/names.go
Normal file
133
app/libgocryptfs/gocryptfs_internal/nametransform/names.go
Normal file
@ -0,0 +1,133 @@
|
|||||||
|
// Package nametransform encrypts and decrypts filenames.
|
||||||
|
package nametransform
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"crypto/aes"
|
||||||
|
"encoding/base64"
|
||||||
|
"path/filepath"
|
||||||
|
"syscall"
|
||||||
|
|
||||||
|
"../eme"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
// Like ext4, we allow at most 255 bytes for a file name.
|
||||||
|
NameMax = 255
|
||||||
|
)
|
||||||
|
|
||||||
|
// NameTransformer is an interface used to transform filenames.
|
||||||
|
type NameTransformer interface {
|
||||||
|
DecryptName(cipherName string, iv []byte) (string, error)
|
||||||
|
EncryptName(plainName string, iv []byte) string
|
||||||
|
EncryptAndHashName(name string, iv []byte) (string, error)
|
||||||
|
HashLongName(name string) string
|
||||||
|
WriteLongNameAt(dirfd int, hashName string, plainName string) error
|
||||||
|
B64EncodeToString(src []byte) string
|
||||||
|
B64DecodeString(s string) ([]byte, error)
|
||||||
|
}
|
||||||
|
|
||||||
|
// NameTransform is used to transform filenames.
|
||||||
|
type NameTransform struct {
|
||||||
|
emeCipher *eme.EMECipher
|
||||||
|
longNames bool
|
||||||
|
// B64 = either base64.URLEncoding or base64.RawURLEncoding, depending
|
||||||
|
// on the Raw64 feature flag
|
||||||
|
B64 *base64.Encoding
|
||||||
|
// Patterns to bypass decryption
|
||||||
|
BadnamePatterns []string
|
||||||
|
}
|
||||||
|
|
||||||
|
// New returns a new NameTransform instance.
|
||||||
|
func New(e *eme.EMECipher, longNames bool, raw64 bool) *NameTransform {
|
||||||
|
b64 := base64.URLEncoding
|
||||||
|
if raw64 {
|
||||||
|
b64 = base64.RawURLEncoding
|
||||||
|
}
|
||||||
|
return &NameTransform{
|
||||||
|
emeCipher: e,
|
||||||
|
longNames: longNames,
|
||||||
|
B64: b64,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// DecryptName calls decryptName to try and decrypt a base64-encoded encrypted
|
||||||
|
// filename "cipherName", and failing that checks if it can be bypassed
|
||||||
|
func (n *NameTransform) DecryptName(cipherName string, iv []byte) (string, error) {
|
||||||
|
res, err := n.decryptName(cipherName, iv)
|
||||||
|
if err != nil {
|
||||||
|
for _, pattern := range n.BadnamePatterns {
|
||||||
|
match, err := filepath.Match(pattern, cipherName)
|
||||||
|
if err == nil && match { // Pattern should have been validated already
|
||||||
|
// Find longest decryptable substring
|
||||||
|
// At least 16 bytes due to AES --> at least 22 characters in base64
|
||||||
|
nameMin := n.B64.EncodedLen(aes.BlockSize)
|
||||||
|
for charpos := len(cipherName) - 1; charpos >= nameMin; charpos-- {
|
||||||
|
res, err = n.decryptName(cipherName[:charpos], iv)
|
||||||
|
if err == nil {
|
||||||
|
return res + cipherName[charpos:] + " GOCRYPTFS_BAD_NAME", nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return cipherName + " GOCRYPTFS_BAD_NAME", nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return res, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// decryptName decrypts a base64-encoded encrypted filename "cipherName" using the
|
||||||
|
// initialization vector "iv".
|
||||||
|
func (n *NameTransform) decryptName(cipherName string, iv []byte) (string, error) {
|
||||||
|
bin, err := n.B64.DecodeString(cipherName)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
if len(bin) == 0 {
|
||||||
|
return "", syscall.EBADMSG
|
||||||
|
}
|
||||||
|
if len(bin)%aes.BlockSize != 0 {
|
||||||
|
return "", syscall.EBADMSG
|
||||||
|
}
|
||||||
|
bin = n.emeCipher.Decrypt(iv, bin)
|
||||||
|
bin, err = unPad16(bin)
|
||||||
|
if err != nil {
|
||||||
|
// unPad16 returns detailed errors including the position of the
|
||||||
|
// incorrect bytes. Kill the padding oracle by lumping everything into
|
||||||
|
// a generic error.
|
||||||
|
return "", syscall.EBADMSG
|
||||||
|
}
|
||||||
|
// A name can never contain a null byte or "/". Make sure we never return those
|
||||||
|
// to the kernel, even when we read a corrupted (or fuzzed) filesystem.
|
||||||
|
if bytes.Contains(bin, []byte{0}) || bytes.Contains(bin, []byte("/")) {
|
||||||
|
return "", syscall.EBADMSG
|
||||||
|
}
|
||||||
|
// The name should never be "." or "..".
|
||||||
|
if bytes.Equal(bin, []byte(".")) || bytes.Equal(bin, []byte("..")) {
|
||||||
|
return "", syscall.EBADMSG
|
||||||
|
}
|
||||||
|
plain := string(bin)
|
||||||
|
return plain, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// EncryptName encrypts "plainName", returns a base64-encoded "cipherName64",
|
||||||
|
// encrypted using EME (https://github.com/rfjakob/eme).
|
||||||
|
//
|
||||||
|
// This function is exported because in some cases, fusefrontend needs access
|
||||||
|
// to the full (not hashed) name if longname is used.
|
||||||
|
func (n *NameTransform) EncryptName(plainName string, iv []byte) (cipherName64 string) {
|
||||||
|
bin := []byte(plainName)
|
||||||
|
bin = pad16(bin)
|
||||||
|
bin = n.emeCipher.Encrypt(iv, bin)
|
||||||
|
cipherName64 = n.B64.EncodeToString(bin)
|
||||||
|
return cipherName64
|
||||||
|
}
|
||||||
|
|
||||||
|
// B64EncodeToString returns a Base64-encoded string
|
||||||
|
func (n *NameTransform) B64EncodeToString(src []byte) string {
|
||||||
|
return n.B64.EncodeToString(src)
|
||||||
|
}
|
||||||
|
|
||||||
|
// B64DecodeString decodes a Base64-encoded string
|
||||||
|
func (n *NameTransform) B64DecodeString(s string) ([]byte, error) {
|
||||||
|
return n.B64.DecodeString(s)
|
||||||
|
}
|
64
app/libgocryptfs/gocryptfs_internal/nametransform/pad16.go
Normal file
64
app/libgocryptfs/gocryptfs_internal/nametransform/pad16.go
Normal file
@ -0,0 +1,64 @@
|
|||||||
|
package nametransform
|
||||||
|
|
||||||
|
import (
|
||||||
|
"crypto/aes"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"log"
|
||||||
|
)
|
||||||
|
|
||||||
|
// pad16 - pad data to AES block size (=16 byte) using standard PKCS#7 padding
|
||||||
|
// https://tools.ietf.org/html/rfc5652#section-6.3
|
||||||
|
func pad16(orig []byte) (padded []byte) {
|
||||||
|
oldLen := len(orig)
|
||||||
|
if oldLen == 0 {
|
||||||
|
log.Panic("Padding zero-length string makes no sense")
|
||||||
|
}
|
||||||
|
padLen := aes.BlockSize - oldLen%aes.BlockSize
|
||||||
|
if padLen == 0 {
|
||||||
|
padLen = aes.BlockSize
|
||||||
|
}
|
||||||
|
newLen := oldLen + padLen
|
||||||
|
padded = make([]byte, newLen)
|
||||||
|
copy(padded, orig)
|
||||||
|
padByte := byte(padLen)
|
||||||
|
for i := oldLen; i < newLen; i++ {
|
||||||
|
padded[i] = padByte
|
||||||
|
}
|
||||||
|
return padded
|
||||||
|
}
|
||||||
|
|
||||||
|
// unPad16 - remove padding
|
||||||
|
func unPad16(padded []byte) ([]byte, error) {
|
||||||
|
oldLen := len(padded)
|
||||||
|
if oldLen == 0 {
|
||||||
|
return nil, errors.New("Empty input")
|
||||||
|
}
|
||||||
|
if oldLen%aes.BlockSize != 0 {
|
||||||
|
return nil, errors.New("Unaligned size")
|
||||||
|
}
|
||||||
|
// The last byte is always a padding byte
|
||||||
|
padByte := padded[oldLen-1]
|
||||||
|
// The padding byte's value is the padding length
|
||||||
|
padLen := int(padByte)
|
||||||
|
// Padding must be at least 1 byte
|
||||||
|
if padLen == 0 {
|
||||||
|
return nil, errors.New("Padding cannot be zero-length")
|
||||||
|
}
|
||||||
|
// Padding more than 16 bytes make no sense
|
||||||
|
if padLen > aes.BlockSize {
|
||||||
|
return nil, fmt.Errorf("Padding too long, padLen=%d > 16", padLen)
|
||||||
|
}
|
||||||
|
// Padding cannot be as long as (or longer than) the whole string,
|
||||||
|
if padLen >= oldLen {
|
||||||
|
return nil, fmt.Errorf("Padding too long, oldLen=%d >= padLen=%d", oldLen, padLen)
|
||||||
|
}
|
||||||
|
// All padding bytes must be identical
|
||||||
|
for i := oldLen - padLen; i < oldLen; i++ {
|
||||||
|
if padded[i] != padByte {
|
||||||
|
return nil, fmt.Errorf("Padding byte at i=%d is invalid", i)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
newLen := oldLen - padLen
|
||||||
|
return padded[0:newLen], nil
|
||||||
|
}
|
7
app/libgocryptfs/gocryptfs_internal/siv_aead/benchmark.bash
Executable file
7
app/libgocryptfs/gocryptfs_internal/siv_aead/benchmark.bash
Executable file
@ -0,0 +1,7 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
|
||||||
|
set -eu
|
||||||
|
|
||||||
|
cd "$(dirname "$0")"
|
||||||
|
|
||||||
|
../stupidgcm/benchmark.bash
|
97
app/libgocryptfs/gocryptfs_internal/siv_aead/siv_aead.go
Normal file
97
app/libgocryptfs/gocryptfs_internal/siv_aead/siv_aead.go
Normal file
@ -0,0 +1,97 @@
|
|||||||
|
// Package siv_aead wraps the functions provided by siv
|
||||||
|
// in a crypto.AEAD interface.
|
||||||
|
package siv_aead
|
||||||
|
|
||||||
|
import (
|
||||||
|
"crypto/cipher"
|
||||||
|
"log"
|
||||||
|
|
||||||
|
"../jacobsa_crypto/siv"
|
||||||
|
)
|
||||||
|
|
||||||
|
type sivAead struct {
|
||||||
|
key []byte
|
||||||
|
}
|
||||||
|
|
||||||
|
var _ cipher.AEAD = &sivAead{}
|
||||||
|
|
||||||
|
const (
|
||||||
|
// KeyLen is the required key length. The SIV algorithm supports other lengths,
|
||||||
|
// but we only support 64.
|
||||||
|
KeyLen = 64
|
||||||
|
)
|
||||||
|
|
||||||
|
// New returns a new cipher.AEAD implementation.
|
||||||
|
func New(key []byte) cipher.AEAD {
|
||||||
|
if len(key) != KeyLen {
|
||||||
|
// SIV supports 32, 48 or 64-byte keys, but in gocryptfs we
|
||||||
|
// exclusively use 64.
|
||||||
|
log.Panicf("Key must be %d byte long (you passed %d)", KeyLen, len(key))
|
||||||
|
}
|
||||||
|
return new2(key)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Same as "New" without the 64-byte restriction.
|
||||||
|
func new2(keyIn []byte) cipher.AEAD {
|
||||||
|
// Create a private copy so the caller can zero the one he owns
|
||||||
|
key := append([]byte{}, keyIn...)
|
||||||
|
return &sivAead{
|
||||||
|
key: key,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *sivAead) NonceSize() int {
|
||||||
|
// SIV supports any nonce size, but in gocryptfs we exclusively use 16.
|
||||||
|
return 16
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *sivAead) Overhead() int {
|
||||||
|
return 16
|
||||||
|
}
|
||||||
|
|
||||||
|
// Seal encrypts "in" using "nonce" and "authData" and appends the result to "dst"
|
||||||
|
func (s *sivAead) Seal(dst, nonce, plaintext, authData []byte) []byte {
|
||||||
|
if len(nonce) != 16 {
|
||||||
|
// SIV supports any nonce size, but in gocryptfs we exclusively use 16.
|
||||||
|
log.Panic("nonce must be 16 bytes long")
|
||||||
|
}
|
||||||
|
if len(s.key) == 0 {
|
||||||
|
log.Panic("Key has been wiped?")
|
||||||
|
}
|
||||||
|
// https://github.com/jacobsa/crypto/blob/master/siv/encrypt.go#L48:
|
||||||
|
// As per RFC 5297 section 3, you may use this function for nonce-based
|
||||||
|
// authenticated encryption by passing a nonce as the last associated
|
||||||
|
// data element.
|
||||||
|
associated := [][]byte{authData, nonce}
|
||||||
|
out, err := siv.Encrypt(dst, s.key, plaintext, associated)
|
||||||
|
if err != nil {
|
||||||
|
log.Panic(err)
|
||||||
|
}
|
||||||
|
return out
|
||||||
|
}
|
||||||
|
|
||||||
|
// Open decrypts "in" using "nonce" and "authData" and appends the result to "dst"
|
||||||
|
func (s *sivAead) Open(dst, nonce, ciphertext, authData []byte) ([]byte, error) {
|
||||||
|
if len(nonce) != 16 {
|
||||||
|
// SIV supports any nonce size, but in gocryptfs we exclusively use 16.
|
||||||
|
log.Panic("nonce must be 16 bytes long")
|
||||||
|
}
|
||||||
|
if len(s.key) == 0 {
|
||||||
|
log.Panic("Key has been wiped?")
|
||||||
|
}
|
||||||
|
associated := [][]byte{authData, nonce}
|
||||||
|
dec, err := siv.Decrypt(s.key, ciphertext, associated)
|
||||||
|
return append(dst, dec...), err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Wipe tries to wipe the AES key from memory by overwriting it with zeros
|
||||||
|
// and setting the reference to nil.
|
||||||
|
//
|
||||||
|
// This is not bulletproof due to possible GC copies, but
|
||||||
|
// still raises to bar for extracting the key.
|
||||||
|
func (s *sivAead) Wipe() {
|
||||||
|
for i := range s.key {
|
||||||
|
s.key[i] = 0
|
||||||
|
}
|
||||||
|
s.key = nil
|
||||||
|
}
|
8
app/libgocryptfs/gocryptfs_internal/stupidgcm/autherr.go
Normal file
8
app/libgocryptfs/gocryptfs_internal/stupidgcm/autherr.go
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
package stupidgcm
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
)
|
||||||
|
|
||||||
|
// ErrAuth is returned when the message authentication fails
|
||||||
|
var ErrAuth = fmt.Errorf("stupidgcm: message authentication failed")
|
3
app/libgocryptfs/gocryptfs_internal/stupidgcm/benchmark.bash
Executable file
3
app/libgocryptfs/gocryptfs_internal/stupidgcm/benchmark.bash
Executable file
@ -0,0 +1,3 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
|
||||||
|
exec ../speed/benchmark.bash
|
28
app/libgocryptfs/gocryptfs_internal/stupidgcm/locking.go
Normal file
28
app/libgocryptfs/gocryptfs_internal/stupidgcm/locking.go
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
// +build !without_openssl
|
||||||
|
|
||||||
|
package stupidgcm
|
||||||
|
|
||||||
|
// In general, OpenSSL is only threadsafe if you provide a locking function
|
||||||
|
// through CRYPTO_set_locking_callback. However, the GCM operations that
|
||||||
|
// stupidgcm uses never call that function. Additionally, the manual locking
|
||||||
|
// has been removed completely in openssl 1.1.0.
|
||||||
|
|
||||||
|
/*
|
||||||
|
#include <openssl/crypto.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
static void dummy_callback(int mode, int n, const char *file, int line) {
|
||||||
|
printf("stupidgcm: thread locking is not implemented and should not be "
|
||||||
|
"needed. Please upgrade openssl.\n");
|
||||||
|
// panic
|
||||||
|
__builtin_trap();
|
||||||
|
}
|
||||||
|
static void set_dummy_callback() {
|
||||||
|
CRYPTO_set_locking_callback(dummy_callback);
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
import "C"
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
C.set_dummy_callback()
|
||||||
|
}
|
28
app/libgocryptfs/gocryptfs_internal/stupidgcm/prefer.go
Normal file
28
app/libgocryptfs/gocryptfs_internal/stupidgcm/prefer.go
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
package stupidgcm
|
||||||
|
|
||||||
|
import (
|
||||||
|
"golang.org/x/sys/cpu"
|
||||||
|
)
|
||||||
|
|
||||||
|
// PreferOpenSSL tells us if OpenSSL is faster than Go GCM on this machine.
|
||||||
|
//
|
||||||
|
// Go GCM is only faster if the CPU either:
|
||||||
|
//
|
||||||
|
// 1) Is X86_64 && has AES instructions && Go is v1.6 or higher
|
||||||
|
// 2) Is ARM64 && has AES instructions && Go is v1.11 or higher
|
||||||
|
// (commit https://github.com/golang/go/commit/4f1f503373cda7160392be94e3849b0c9b9ebbda)
|
||||||
|
//
|
||||||
|
// See https://github.com/rfjakob/gocryptfs/wiki/CPU-Benchmarks
|
||||||
|
// for benchmarks.
|
||||||
|
func PreferOpenSSL() bool {
|
||||||
|
if BuiltWithoutOpenssl {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
// Safe to call on other architectures - will just read false.
|
||||||
|
if cpu.X86.HasAES || cpu.ARM64.HasAES {
|
||||||
|
// Go stdlib is probably faster
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
// Openssl is probably faster
|
||||||
|
return true
|
||||||
|
}
|
250
app/libgocryptfs/gocryptfs_internal/stupidgcm/stupidgcm.go
Normal file
250
app/libgocryptfs/gocryptfs_internal/stupidgcm/stupidgcm.go
Normal file
@ -0,0 +1,250 @@
|
|||||||
|
// +build !without_openssl
|
||||||
|
|
||||||
|
// Package stupidgcm is a thin wrapper for OpenSSL's GCM encryption and
|
||||||
|
// decryption functions. It only support 32-byte keys and 16-bit IVs.
|
||||||
|
package stupidgcm
|
||||||
|
|
||||||
|
//#include <openssl/err.h>
|
||||||
|
// #include <openssl/evp.h>
|
||||||
|
// #cgo pkg-config: libcrypto
|
||||||
|
import "C"
|
||||||
|
|
||||||
|
import (
|
||||||
|
"crypto/cipher"
|
||||||
|
"fmt"
|
||||||
|
"log"
|
||||||
|
"unsafe"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
// BuiltWithoutOpenssl indicates if openssl been disabled at compile-time
|
||||||
|
BuiltWithoutOpenssl = false
|
||||||
|
|
||||||
|
keyLen = 32
|
||||||
|
ivLen = 16
|
||||||
|
tagLen = 16
|
||||||
|
)
|
||||||
|
|
||||||
|
// StupidGCM implements the cipher.AEAD interface
|
||||||
|
type StupidGCM struct {
|
||||||
|
key []byte
|
||||||
|
forceDecode bool
|
||||||
|
}
|
||||||
|
|
||||||
|
// Verify that we satisfy the cipher.AEAD interface
|
||||||
|
var _ cipher.AEAD = &StupidGCM{}
|
||||||
|
|
||||||
|
// New returns a new cipher.AEAD implementation..
|
||||||
|
func New(keyIn []byte, forceDecode bool) cipher.AEAD {
|
||||||
|
if len(keyIn) != keyLen {
|
||||||
|
log.Panicf("Only %d-byte keys are supported", keyLen)
|
||||||
|
}
|
||||||
|
// Create a private copy of the key
|
||||||
|
key := append([]byte{}, keyIn...)
|
||||||
|
return &StupidGCM{key: key, forceDecode: forceDecode}
|
||||||
|
}
|
||||||
|
|
||||||
|
// NonceSize returns the required size of the nonce / IV.
|
||||||
|
func (g *StupidGCM) NonceSize() int {
|
||||||
|
return ivLen
|
||||||
|
}
|
||||||
|
|
||||||
|
// Overhead returns the number of bytes that are added for authentication.
|
||||||
|
func (g *StupidGCM) Overhead() int {
|
||||||
|
return tagLen
|
||||||
|
}
|
||||||
|
|
||||||
|
// Seal encrypts "in" using "iv" and "authData" and append the result to "dst"
|
||||||
|
func (g *StupidGCM) Seal(dst, iv, in, authData []byte) []byte {
|
||||||
|
if len(iv) != ivLen {
|
||||||
|
log.Panicf("Only %d-byte IVs are supported", ivLen)
|
||||||
|
}
|
||||||
|
if len(in) == 0 {
|
||||||
|
log.Panic("Zero-length input data is not supported")
|
||||||
|
}
|
||||||
|
if len(g.key) != keyLen {
|
||||||
|
log.Panicf("Wrong key length: %d. Key has been wiped?", len(g.key))
|
||||||
|
}
|
||||||
|
|
||||||
|
// If the "dst" slice is large enough we can use it as our output buffer
|
||||||
|
outLen := len(in) + tagLen
|
||||||
|
var buf []byte
|
||||||
|
inplace := false
|
||||||
|
if cap(dst)-len(dst) >= outLen {
|
||||||
|
inplace = true
|
||||||
|
buf = dst[len(dst) : len(dst)+outLen]
|
||||||
|
} else {
|
||||||
|
buf = make([]byte, outLen)
|
||||||
|
}
|
||||||
|
|
||||||
|
// https://wiki.openssl.org/index.php/EVP_Authenticated_Encryption_and_Decryption#Authenticated_Encryption_using_GCM_mode
|
||||||
|
|
||||||
|
// Create scratch space "context"
|
||||||
|
ctx := C.EVP_CIPHER_CTX_new()
|
||||||
|
if ctx == nil {
|
||||||
|
log.Panic("EVP_CIPHER_CTX_new failed")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Set cipher to AES-256
|
||||||
|
if C.EVP_EncryptInit_ex(ctx, C.EVP_aes_256_gcm(), nil, nil, nil) != 1 {
|
||||||
|
log.Panic("EVP_EncryptInit_ex I failed")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Use 16-byte IV
|
||||||
|
if C.EVP_CIPHER_CTX_ctrl(ctx, C.EVP_CTRL_GCM_SET_IVLEN, ivLen, nil) != 1 {
|
||||||
|
log.Panic("EVP_CIPHER_CTX_ctrl EVP_CTRL_GCM_SET_IVLEN failed")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Set key and IV
|
||||||
|
if C.EVP_EncryptInit_ex(ctx, nil, nil, (*C.uchar)(&g.key[0]), (*C.uchar)(&iv[0])) != 1 {
|
||||||
|
log.Panic("EVP_EncryptInit_ex II failed")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Provide authentication data
|
||||||
|
var resultLen C.int
|
||||||
|
if C.EVP_EncryptUpdate(ctx, nil, &resultLen, (*C.uchar)(&authData[0]), C.int(len(authData))) != 1 {
|
||||||
|
log.Panic("EVP_EncryptUpdate authData failed")
|
||||||
|
}
|
||||||
|
if int(resultLen) != len(authData) {
|
||||||
|
log.Panicf("Unexpected length %d", resultLen)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Encrypt "in" into "buf"
|
||||||
|
if C.EVP_EncryptUpdate(ctx, (*C.uchar)(&buf[0]), &resultLen, (*C.uchar)(&in[0]), C.int(len(in))) != 1 {
|
||||||
|
log.Panic("EVP_EncryptUpdate failed")
|
||||||
|
}
|
||||||
|
if int(resultLen) != len(in) {
|
||||||
|
log.Panicf("Unexpected length %d", resultLen)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Finalise encryption
|
||||||
|
// Because GCM is a stream encryption, this will not write out any data.
|
||||||
|
dummy := make([]byte, 16)
|
||||||
|
if C.EVP_EncryptFinal_ex(ctx, (*C.uchar)(&dummy[0]), &resultLen) != 1 {
|
||||||
|
log.Panic("EVP_EncryptFinal_ex failed")
|
||||||
|
}
|
||||||
|
if resultLen != 0 {
|
||||||
|
log.Panicf("Unexpected length %d", resultLen)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get GMAC tag and append it to the ciphertext in "buf"
|
||||||
|
if C.EVP_CIPHER_CTX_ctrl(ctx, C.EVP_CTRL_GCM_GET_TAG, tagLen, (unsafe.Pointer)(&buf[len(in)])) != 1 {
|
||||||
|
log.Panic("EVP_CIPHER_CTX_ctrl EVP_CTRL_GCM_GET_TAG failed")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Free scratch space
|
||||||
|
C.EVP_CIPHER_CTX_free(ctx)
|
||||||
|
|
||||||
|
if inplace {
|
||||||
|
return dst[:len(dst)+outLen]
|
||||||
|
}
|
||||||
|
return append(dst, buf...)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Open decrypts "in" using "iv" and "authData" and append the result to "dst"
|
||||||
|
func (g *StupidGCM) Open(dst, iv, in, authData []byte) ([]byte, error) {
|
||||||
|
if len(iv) != ivLen {
|
||||||
|
log.Panicf("Only %d-byte IVs are supported", ivLen)
|
||||||
|
}
|
||||||
|
if len(g.key) != keyLen {
|
||||||
|
log.Panicf("Wrong key length: %d. Key has been wiped?", len(g.key))
|
||||||
|
}
|
||||||
|
if len(in) <= tagLen {
|
||||||
|
return nil, fmt.Errorf("stupidgcm: input data too short (%d bytes)", len(in))
|
||||||
|
}
|
||||||
|
|
||||||
|
// If the "dst" slice is large enough we can use it as our output buffer
|
||||||
|
outLen := len(in) - tagLen
|
||||||
|
var buf []byte
|
||||||
|
inplace := false
|
||||||
|
if cap(dst)-len(dst) >= outLen {
|
||||||
|
inplace = true
|
||||||
|
buf = dst[len(dst) : len(dst)+outLen]
|
||||||
|
} else {
|
||||||
|
buf = make([]byte, len(in)-tagLen)
|
||||||
|
}
|
||||||
|
|
||||||
|
ciphertext := in[:len(in)-tagLen]
|
||||||
|
tag := in[len(in)-tagLen:]
|
||||||
|
|
||||||
|
// https://wiki.openssl.org/index.php/EVP_Authenticated_Encryption_and_Decryption#Authenticated_Encryption_using_GCM_mode
|
||||||
|
|
||||||
|
// Create scratch space "context"
|
||||||
|
ctx := C.EVP_CIPHER_CTX_new()
|
||||||
|
if ctx == nil {
|
||||||
|
log.Panic("EVP_CIPHER_CTX_new failed")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Set cipher to AES-256
|
||||||
|
if C.EVP_DecryptInit_ex(ctx, C.EVP_aes_256_gcm(), nil, nil, nil) != 1 {
|
||||||
|
log.Panic("EVP_DecryptInit_ex I failed")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Use 16-byte IV
|
||||||
|
if C.EVP_CIPHER_CTX_ctrl(ctx, C.EVP_CTRL_GCM_SET_IVLEN, ivLen, nil) != 1 {
|
||||||
|
log.Panic("EVP_CIPHER_CTX_ctrl EVP_CTRL_GCM_SET_IVLEN failed")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Set key and IV
|
||||||
|
if C.EVP_DecryptInit_ex(ctx, nil, nil, (*C.uchar)(&g.key[0]), (*C.uchar)(&iv[0])) != 1 {
|
||||||
|
log.Panic("EVP_DecryptInit_ex II failed")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Set expected GMAC tag
|
||||||
|
if C.EVP_CIPHER_CTX_ctrl(ctx, C.EVP_CTRL_GCM_SET_TAG, tagLen, (unsafe.Pointer)(&tag[0])) != 1 {
|
||||||
|
log.Panic("EVP_CIPHER_CTX_ctrl failed")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Provide authentication data
|
||||||
|
var resultLen C.int
|
||||||
|
if C.EVP_DecryptUpdate(ctx, nil, &resultLen, (*C.uchar)(&authData[0]), C.int(len(authData))) != 1 {
|
||||||
|
log.Panic("EVP_DecryptUpdate authData failed")
|
||||||
|
}
|
||||||
|
if int(resultLen) != len(authData) {
|
||||||
|
log.Panicf("Unexpected length %d", resultLen)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Decrypt "ciphertext" into "buf"
|
||||||
|
if C.EVP_DecryptUpdate(ctx, (*C.uchar)(&buf[0]), &resultLen, (*C.uchar)(&ciphertext[0]), C.int(len(ciphertext))) != 1 {
|
||||||
|
log.Panic("EVP_DecryptUpdate failed")
|
||||||
|
}
|
||||||
|
if int(resultLen) != len(ciphertext) {
|
||||||
|
log.Panicf("Unexpected length %d", resultLen)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check GMAC
|
||||||
|
dummy := make([]byte, 16)
|
||||||
|
res := C.EVP_DecryptFinal_ex(ctx, (*C.uchar)(&dummy[0]), &resultLen)
|
||||||
|
if resultLen != 0 {
|
||||||
|
log.Panicf("Unexpected length %d", resultLen)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Free scratch space
|
||||||
|
C.EVP_CIPHER_CTX_free(ctx)
|
||||||
|
|
||||||
|
if res != 1 {
|
||||||
|
// The error code must always be checked by the calling function, because the decrypted buffer
|
||||||
|
// may contain corrupted data that we are returning in case the user forced reads
|
||||||
|
if g.forceDecode == true {
|
||||||
|
return append(dst, buf...), ErrAuth
|
||||||
|
}
|
||||||
|
return nil, ErrAuth
|
||||||
|
}
|
||||||
|
|
||||||
|
if inplace {
|
||||||
|
return dst[:len(dst)+outLen], nil
|
||||||
|
}
|
||||||
|
return append(dst, buf...), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Wipe tries to wipe the AES key from memory by overwriting it with zeros
|
||||||
|
// and setting the reference to nil.
|
||||||
|
//
|
||||||
|
// This is not bulletproof due to possible GC copies, but
|
||||||
|
// still raises to bar for extracting the key.
|
||||||
|
func (g *StupidGCM) Wipe() {
|
||||||
|
for i := range g.key {
|
||||||
|
g.key[i] = 0
|
||||||
|
}
|
||||||
|
g.key = nil
|
||||||
|
}
|
@ -0,0 +1,52 @@
|
|||||||
|
// +build without_openssl
|
||||||
|
|
||||||
|
package stupidgcm
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
|
||||||
|
"github.com/rfjakob/gocryptfs/internal/exitcodes"
|
||||||
|
)
|
||||||
|
|
||||||
|
type StupidGCM struct{}
|
||||||
|
|
||||||
|
const (
|
||||||
|
// BuiltWithoutOpenssl indicates if openssl been disabled at compile-time
|
||||||
|
BuiltWithoutOpenssl = true
|
||||||
|
)
|
||||||
|
|
||||||
|
func errExit() {
|
||||||
|
fmt.Fprintln(os.Stderr, "gocryptfs has been compiled without openssl support but you are still trying to use openssl")
|
||||||
|
os.Exit(exitcodes.OpenSSL)
|
||||||
|
}
|
||||||
|
|
||||||
|
func New(_ []byte, _ bool) *StupidGCM {
|
||||||
|
errExit()
|
||||||
|
// Never reached
|
||||||
|
return &StupidGCM{}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (g *StupidGCM) NonceSize() int {
|
||||||
|
errExit()
|
||||||
|
return -1
|
||||||
|
}
|
||||||
|
|
||||||
|
func (g *StupidGCM) Overhead() int {
|
||||||
|
errExit()
|
||||||
|
return -1
|
||||||
|
}
|
||||||
|
|
||||||
|
func (g *StupidGCM) Seal(_, _, _, _ []byte) []byte {
|
||||||
|
errExit()
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (g *StupidGCM) Open(_, _, _, _ []byte) ([]byte, error) {
|
||||||
|
errExit()
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (g *StupidGCM) Wipe() {
|
||||||
|
errExit()
|
||||||
|
}
|
952
app/libgocryptfs/main.go
Normal file
952
app/libgocryptfs/main.go
Normal file
@ -0,0 +1,952 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"C"
|
||||||
|
"crypto/cipher"
|
||||||
|
"crypto/aes"
|
||||||
|
"syscall"
|
||||||
|
"strings"
|
||||||
|
"bytes"
|
||||||
|
"unsafe"
|
||||||
|
"os"
|
||||||
|
"io"
|
||||||
|
"fmt"
|
||||||
|
"path/filepath"
|
||||||
|
"golang.org/x/sys/unix"
|
||||||
|
|
||||||
|
"./gocryptfs_internal/cryptocore"
|
||||||
|
"./gocryptfs_internal/stupidgcm"
|
||||||
|
"./gocryptfs_internal/eme"
|
||||||
|
"./gocryptfs_internal/nametransform"
|
||||||
|
"./rewrites/syscallcompat"
|
||||||
|
"./rewrites/configfile"
|
||||||
|
"./rewrites/contentenc"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
file_mode = uint32(0660)
|
||||||
|
folder_mode = uint32(0770)
|
||||||
|
)
|
||||||
|
|
||||||
|
type Directory struct {
|
||||||
|
fd int
|
||||||
|
iv []byte
|
||||||
|
}
|
||||||
|
|
||||||
|
type File struct {
|
||||||
|
fd *os.File
|
||||||
|
path string
|
||||||
|
}
|
||||||
|
|
||||||
|
type SessionVars struct {
|
||||||
|
root_cipher_dir string
|
||||||
|
nameTransform *nametransform.NameTransform
|
||||||
|
cryptoCore *cryptocore.CryptoCore
|
||||||
|
contentEnc *contentenc.ContentEnc
|
||||||
|
dirCache map[string]Directory
|
||||||
|
file_handles map[int]File
|
||||||
|
fileIDs map[int][]byte
|
||||||
|
}
|
||||||
|
|
||||||
|
//var fdebug *os.File
|
||||||
|
var sessions map[int]SessionVars
|
||||||
|
|
||||||
|
func err_to_bool(e error) bool {
|
||||||
|
if e == nil {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
func wipe(d []byte){
|
||||||
|
for i := range d {
|
||||||
|
d[i] = 0
|
||||||
|
}
|
||||||
|
d = nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func clear_dirCache(sessionID int) {
|
||||||
|
for k, _ := range sessions[sessionID].dirCache {
|
||||||
|
delete(sessions[sessionID].dirCache, k)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func openBackingDir(sessionID int, relPath string) (dirfd int, cName string, err error) {
|
||||||
|
dirRelPath := nametransform.Dir(relPath)
|
||||||
|
dir, ok := sessions[sessionID].dirCache[dirRelPath]
|
||||||
|
if ok {
|
||||||
|
// If relPath is empty, cName is ".".
|
||||||
|
if relPath == "" {
|
||||||
|
cache_dirfd, err := syscall.Dup(dir.fd)
|
||||||
|
if err != nil {
|
||||||
|
return -1, "", err
|
||||||
|
}
|
||||||
|
return cache_dirfd, ".", nil
|
||||||
|
}
|
||||||
|
name := filepath.Base(relPath)
|
||||||
|
cName, err = sessions[sessionID].nameTransform.EncryptAndHashName(name, dir.iv)
|
||||||
|
if err != nil {
|
||||||
|
syscall.Close(dir.fd)
|
||||||
|
return -1, "", err
|
||||||
|
}
|
||||||
|
cache_dirfd, err := syscall.Dup(dir.fd)
|
||||||
|
if err != nil {
|
||||||
|
return -1, "", err
|
||||||
|
}
|
||||||
|
return cache_dirfd, cName, nil
|
||||||
|
}
|
||||||
|
// Open cipherdir (following symlinks)
|
||||||
|
dirfd, err = syscall.Open(sessions[sessionID].root_cipher_dir, syscall.O_DIRECTORY|syscallcompat.O_PATH, 0)
|
||||||
|
if err != nil {
|
||||||
|
return -1, "", err
|
||||||
|
}
|
||||||
|
// If relPath is empty, cName is ".".
|
||||||
|
if relPath == "" {
|
||||||
|
return dirfd, ".", nil
|
||||||
|
}
|
||||||
|
// Walk the directory tree
|
||||||
|
parts := strings.Split(relPath, "/")
|
||||||
|
for i, name := range parts {
|
||||||
|
iv, err := nametransform.ReadDirIVAt(dirfd)
|
||||||
|
if err != nil {
|
||||||
|
syscall.Close(dirfd)
|
||||||
|
return -1, "", err
|
||||||
|
}
|
||||||
|
cName, err = sessions[sessionID].nameTransform.EncryptAndHashName(name, iv)
|
||||||
|
if err != nil {
|
||||||
|
syscall.Close(dirfd)
|
||||||
|
return -1, "", err
|
||||||
|
}
|
||||||
|
// Last part? We are done.
|
||||||
|
if i == len(parts)-1 {
|
||||||
|
cache_dirfd, err := syscall.Dup(dirfd)
|
||||||
|
if err == nil {
|
||||||
|
sessions[sessionID].dirCache[dirRelPath] = Directory{cache_dirfd, iv}
|
||||||
|
}
|
||||||
|
break
|
||||||
|
}
|
||||||
|
// Not the last part? Descend into next directory.
|
||||||
|
dirfd2, err := syscallcompat.Openat(dirfd, cName, syscall.O_NOFOLLOW|syscall.O_DIRECTORY|syscallcompat.O_PATH, 0)
|
||||||
|
syscall.Close(dirfd)
|
||||||
|
if err != nil {
|
||||||
|
return -1, "", err
|
||||||
|
}
|
||||||
|
dirfd = dirfd2
|
||||||
|
}
|
||||||
|
return dirfd, cName, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func mkdirWithIv(dirfd int, cName string, mode uint32) error {
|
||||||
|
err := syscallcompat.Mkdirat(dirfd, cName, mode)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
dirfd2, err := syscallcompat.Openat(dirfd, cName, syscall.O_DIRECTORY|syscall.O_NOFOLLOW|syscallcompat.O_PATH, 0)
|
||||||
|
if err == nil {
|
||||||
|
// Create gocryptfs.diriv
|
||||||
|
err = nametransform.WriteDirIVAt(dirfd2)
|
||||||
|
syscall.Close(dirfd2)
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
// Delete inconsistent directory (missing gocryptfs.diriv!)
|
||||||
|
err2 := syscallcompat.Unlinkat(dirfd, cName, unix.AT_REMOVEDIR)
|
||||||
|
if err2 != nil {
|
||||||
|
return err2
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
func mangleOpenFlags(flags uint32) (newFlags int) {
|
||||||
|
newFlags = int(flags)
|
||||||
|
// Convert WRONLY to RDWR. We always need read access to do read-modify-write cycles.
|
||||||
|
if (newFlags & syscall.O_ACCMODE) == syscall.O_WRONLY {
|
||||||
|
newFlags = newFlags ^ os.O_WRONLY | os.O_RDWR
|
||||||
|
}
|
||||||
|
// We also cannot open the file in append mode, we need to seek back for RMW
|
||||||
|
newFlags = newFlags &^ os.O_APPEND
|
||||||
|
// O_DIRECT accesses must be aligned in both offset and length. Due to our
|
||||||
|
// crypto header, alignment will be off, even if userspace makes aligned
|
||||||
|
// accesses. Running xfstests generic/013 on ext4 used to trigger lots of
|
||||||
|
// EINVAL errors due to missing alignment. Just fall back to buffered IO.
|
||||||
|
newFlags = newFlags &^ syscallcompat.O_DIRECT
|
||||||
|
// Create and Open are two separate FUSE operations, so O_CREAT should not
|
||||||
|
// be part of the open flags.
|
||||||
|
newFlags = newFlags &^ syscall.O_CREAT
|
||||||
|
// We always want O_NOFOLLOW to be safe against symlink races
|
||||||
|
newFlags |= syscall.O_NOFOLLOW
|
||||||
|
return newFlags
|
||||||
|
}
|
||||||
|
|
||||||
|
func register_file_handle(sessionID int, file File) int {
|
||||||
|
handleID := -1
|
||||||
|
c := 0
|
||||||
|
for handleID == -1 {
|
||||||
|
_, ok := sessions[sessionID].file_handles[c]
|
||||||
|
if !ok {
|
||||||
|
handleID = c
|
||||||
|
}
|
||||||
|
c++
|
||||||
|
}
|
||||||
|
sessions[sessionID].file_handles[handleID] = file
|
||||||
|
return handleID
|
||||||
|
}
|
||||||
|
|
||||||
|
func readFileID(fd *os.File) ([]byte, error) {
|
||||||
|
// We read +1 byte to determine if the file has actual content
|
||||||
|
// and not only the header. A header-only file will be considered empty.
|
||||||
|
// This makes File ID poisoning more difficult.
|
||||||
|
readLen := contentenc.HeaderLen + 1
|
||||||
|
buf := make([]byte, readLen)
|
||||||
|
_, err := fd.ReadAt(buf, 0)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
buf = buf[:contentenc.HeaderLen]
|
||||||
|
h, err := contentenc.ParseHeader(buf)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return h.ID, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func createHeader(fd *os.File) (fileID []byte, err error) {
|
||||||
|
h := contentenc.RandomHeader()
|
||||||
|
buf := h.Pack()
|
||||||
|
// Prevent partially written (=corrupt) header by preallocating the space beforehand
|
||||||
|
//NoPrealloc
|
||||||
|
err = syscallcompat.EnospcPrealloc(int(fd.Fd()), 0, contentenc.HeaderLen)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
// Actually write header
|
||||||
|
_, err = fd.WriteAt(buf, 0)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return h.ID, err
|
||||||
|
}
|
||||||
|
|
||||||
|
func doRead(sessionID, handleID int, dst_buff []byte, offset uint64, length uint64) ([]byte, bool) {
|
||||||
|
f, ok := sessions[sessionID].file_handles[handleID]
|
||||||
|
if !ok {
|
||||||
|
return nil, false
|
||||||
|
}
|
||||||
|
fd := f.fd
|
||||||
|
var fileID []byte
|
||||||
|
test_fileID, ok := sessions[sessionID].fileIDs[handleID]
|
||||||
|
if ok {
|
||||||
|
fileID = test_fileID
|
||||||
|
} else {
|
||||||
|
var err error
|
||||||
|
fileID, err = readFileID(fd)
|
||||||
|
if err != nil || fileID == nil {
|
||||||
|
return nil, false
|
||||||
|
}
|
||||||
|
sessions[sessionID].fileIDs[handleID] = fileID
|
||||||
|
}
|
||||||
|
|
||||||
|
// Read the backing ciphertext in one go
|
||||||
|
blocks := sessions[sessionID].contentEnc.ExplodePlainRange(offset, length)
|
||||||
|
alignedOffset, alignedLength := blocks[0].JointCiphertextRange(blocks)
|
||||||
|
skip := blocks[0].Skip
|
||||||
|
|
||||||
|
ciphertext := sessions[sessionID].contentEnc.CReqPool.Get()
|
||||||
|
ciphertext = ciphertext[:int(alignedLength)]
|
||||||
|
n, err := fd.ReadAt(ciphertext, int64(alignedOffset))
|
||||||
|
if err != nil && err != io.EOF {
|
||||||
|
return nil, false
|
||||||
|
}
|
||||||
|
// The ReadAt came back empty. We can skip all the decryption and return early.
|
||||||
|
if n == 0 {
|
||||||
|
sessions[sessionID].contentEnc.CReqPool.Put(ciphertext)
|
||||||
|
return dst_buff, true
|
||||||
|
}
|
||||||
|
// Truncate ciphertext buffer down to actually read bytes
|
||||||
|
ciphertext = ciphertext[0:n]
|
||||||
|
|
||||||
|
firstBlockNo := blocks[0].BlockNo
|
||||||
|
|
||||||
|
// Decrypt it
|
||||||
|
plaintext, err := sessions[sessionID].contentEnc.DecryptBlocks(ciphertext, firstBlockNo, fileID)
|
||||||
|
sessions[sessionID].contentEnc.CReqPool.Put(ciphertext)
|
||||||
|
if err != nil {
|
||||||
|
return nil, false
|
||||||
|
}
|
||||||
|
|
||||||
|
// Crop down to the relevant part
|
||||||
|
var out []byte
|
||||||
|
lenHave := len(plaintext)
|
||||||
|
lenWant := int(skip + length)
|
||||||
|
if lenHave > lenWant {
|
||||||
|
out = plaintext[skip:lenWant]
|
||||||
|
} else if lenHave > int(skip) {
|
||||||
|
out = plaintext[skip:lenHave]
|
||||||
|
}
|
||||||
|
// else: out stays empty, file was smaller than the requested offset
|
||||||
|
|
||||||
|
out = append(dst_buff, out...)
|
||||||
|
sessions[sessionID].contentEnc.PReqPool.Put(plaintext)
|
||||||
|
return out, true
|
||||||
|
}
|
||||||
|
|
||||||
|
func doWrite(sessionID, handleID int, data []byte, offset uint64) (uint32, bool){
|
||||||
|
fileWasEmpty := false
|
||||||
|
f, ok := sessions[sessionID].file_handles[handleID]
|
||||||
|
if !ok {
|
||||||
|
return 0, false
|
||||||
|
}
|
||||||
|
fd := f.fd
|
||||||
|
var err error
|
||||||
|
var fileID []byte
|
||||||
|
test_fileID, ok := sessions[sessionID].fileIDs[handleID]
|
||||||
|
if ok {
|
||||||
|
fileID = test_fileID
|
||||||
|
} else {
|
||||||
|
fileID, err = readFileID(fd)
|
||||||
|
// Write a new file header if the file is empty
|
||||||
|
if err == io.EOF {
|
||||||
|
fileID, err = createHeader(fd)
|
||||||
|
fileWasEmpty = true
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
return 0, false
|
||||||
|
}
|
||||||
|
sessions[sessionID].fileIDs[handleID] = fileID
|
||||||
|
}
|
||||||
|
// Handle payload data
|
||||||
|
dataBuf := bytes.NewBuffer(data)
|
||||||
|
blocks := sessions[sessionID].contentEnc.ExplodePlainRange(offset, uint64(len(data)))
|
||||||
|
toEncrypt := make([][]byte, len(blocks))
|
||||||
|
for i, b := range blocks {
|
||||||
|
blockData := dataBuf.Next(int(b.Length))
|
||||||
|
// Incomplete block -> Read-Modify-Write
|
||||||
|
if b.IsPartial() {
|
||||||
|
// Read
|
||||||
|
oldData, success := doRead(sessionID, handleID, nil, b.BlockPlainOff(), sessions[sessionID].contentEnc.PlainBS())
|
||||||
|
if !success {
|
||||||
|
return 0, false
|
||||||
|
}
|
||||||
|
// Modify
|
||||||
|
blockData = sessions[sessionID].contentEnc.MergeBlocks(oldData, blockData, int(b.Skip))
|
||||||
|
}
|
||||||
|
// Write into the to-encrypt list
|
||||||
|
toEncrypt[i] = blockData
|
||||||
|
}
|
||||||
|
// Encrypt all blocks
|
||||||
|
ciphertext := sessions[sessionID].contentEnc.EncryptBlocks(toEncrypt, blocks[0].BlockNo, fileID)
|
||||||
|
// Preallocate so we cannot run out of space in the middle of the write.
|
||||||
|
// This prevents partially written (=corrupt) blocks.
|
||||||
|
cOff := int64(blocks[0].BlockCipherOff())
|
||||||
|
|
||||||
|
//NoPrealloc
|
||||||
|
err = syscallcompat.EnospcPrealloc(int(fd.Fd()), cOff, int64(len(ciphertext)))
|
||||||
|
if err != nil {
|
||||||
|
if fileWasEmpty {
|
||||||
|
syscall.Ftruncate(int(fd.Fd()), 0)
|
||||||
|
// Kill the file header again
|
||||||
|
gcf_close_file(sessionID, handleID) //f.fileTableEntry.ID = nil
|
||||||
|
}
|
||||||
|
return 0, false
|
||||||
|
}
|
||||||
|
// Write
|
||||||
|
_, err = fd.WriteAt(ciphertext, cOff)
|
||||||
|
// Return memory to CReqPool
|
||||||
|
sessions[sessionID].contentEnc.CReqPool.Put(ciphertext)
|
||||||
|
if err != nil {
|
||||||
|
return 0, false
|
||||||
|
}
|
||||||
|
return uint32(len(data)), true
|
||||||
|
}
|
||||||
|
|
||||||
|
// Zero-pad the file of size plainSize to the next block boundary. This is a no-op
|
||||||
|
// if the file is already block-aligned.
|
||||||
|
func zeroPad(sessionID, handleID int, plainSize uint64) bool {
|
||||||
|
lastBlockLen := plainSize % sessions[sessionID].contentEnc.PlainBS()
|
||||||
|
if lastBlockLen == 0 {
|
||||||
|
// Already block-aligned
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
missing := sessions[sessionID].contentEnc.PlainBS() - lastBlockLen
|
||||||
|
pad := make([]byte, missing)
|
||||||
|
_, success := doWrite(sessionID, handleID, pad, plainSize)
|
||||||
|
return success
|
||||||
|
}
|
||||||
|
|
||||||
|
// truncateGrowFile extends a file using seeking or ftruncate performing RMW on
|
||||||
|
// the first and last block as necessary. New blocks in the middle become
|
||||||
|
// file holes unless they have been fallocate()'d beforehand.
|
||||||
|
func truncateGrowFile(sessionID, handleID int, oldPlainSz uint64, newPlainSz uint64) bool {
|
||||||
|
if newPlainSz <= oldPlainSz {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
newEOFOffset := newPlainSz - 1
|
||||||
|
if oldPlainSz > 0 {
|
||||||
|
n1 := sessions[sessionID].contentEnc.PlainOffToBlockNo(oldPlainSz - 1)
|
||||||
|
n2 := sessions[sessionID].contentEnc.PlainOffToBlockNo(newEOFOffset)
|
||||||
|
// The file is grown within one block, no need to pad anything.
|
||||||
|
// Write a single zero to the last byte and let doWrite figure out the RMW.
|
||||||
|
if n1 == n2 {
|
||||||
|
buf := make([]byte, 1)
|
||||||
|
_, success := doWrite(sessionID, handleID, buf, newEOFOffset)
|
||||||
|
return success
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// The truncate creates at least one new block.
|
||||||
|
//
|
||||||
|
// Make sure the old last block is padded to the block boundary. This call
|
||||||
|
// is a no-op if it is already block-aligned.
|
||||||
|
success := zeroPad(sessionID, handleID, oldPlainSz)
|
||||||
|
if !success {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
// The new size is block-aligned. In this case we can do everything ourselves
|
||||||
|
// and avoid the call to doWrite.
|
||||||
|
if newPlainSz%sessions[sessionID].contentEnc.PlainBS() == 0 {
|
||||||
|
// The file was empty, so it did not have a header. Create one.
|
||||||
|
if oldPlainSz == 0 {
|
||||||
|
id, err := createHeader(sessions[sessionID].file_handles[handleID].fd)
|
||||||
|
if err != nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
sessions[sessionID].fileIDs[handleID] = id
|
||||||
|
}
|
||||||
|
cSz := int64(sessions[sessionID].contentEnc.PlainSizeToCipherSize(newPlainSz))
|
||||||
|
return err_to_bool(syscall.Ftruncate(int(sessions[sessionID].file_handles[handleID].fd.Fd()), cSz))
|
||||||
|
}
|
||||||
|
// The new size is NOT aligned, so we need to write a partial block.
|
||||||
|
// Write a single zero to the last byte and let doWrite figure it out.
|
||||||
|
buf := make([]byte, 1)
|
||||||
|
_, success = doWrite(sessionID, handleID, buf, newEOFOffset)
|
||||||
|
return success
|
||||||
|
}
|
||||||
|
|
||||||
|
func truncate(sessionID, handleID int, newSize uint64) bool {
|
||||||
|
fileFD := int(sessions[sessionID].file_handles[handleID].fd.Fd())
|
||||||
|
/*// Common case first: Truncate to zero
|
||||||
|
if newSize == 0 {
|
||||||
|
err = syscall.Ftruncate(fileFD, 0)
|
||||||
|
if err != nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
// Truncate to zero kills the file header
|
||||||
|
f.fileTableEntry.ID = nil
|
||||||
|
return true
|
||||||
|
}*/
|
||||||
|
// We need the old file size to determine if we are growing or shrinking
|
||||||
|
// the file
|
||||||
|
oldSize, _, success := gcf_get_attrs(sessionID, sessions[sessionID].file_handles[handleID].path)
|
||||||
|
if !success {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
// File size stays the same - nothing to do
|
||||||
|
if newSize == oldSize {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
// File grows
|
||||||
|
if newSize > oldSize {
|
||||||
|
return truncateGrowFile(sessionID, handleID, oldSize, newSize)
|
||||||
|
}
|
||||||
|
|
||||||
|
// File shrinks
|
||||||
|
blockNo := sessions[sessionID].contentEnc.PlainOffToBlockNo(newSize)
|
||||||
|
cipherOff := sessions[sessionID].contentEnc.BlockNoToCipherOff(blockNo)
|
||||||
|
plainOff := sessions[sessionID].contentEnc.BlockNoToPlainOff(blockNo)
|
||||||
|
lastBlockLen := newSize - plainOff
|
||||||
|
var data []byte
|
||||||
|
if lastBlockLen > 0 {
|
||||||
|
data, success = doRead(sessionID, handleID, nil, plainOff, lastBlockLen)
|
||||||
|
if !success {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Truncate down to the last complete block
|
||||||
|
err := syscall.Ftruncate(fileFD, int64(cipherOff))
|
||||||
|
if err != nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
// Append partial block
|
||||||
|
if lastBlockLen > 0 {
|
||||||
|
_, success := doWrite(sessionID, handleID, data, plainOff)
|
||||||
|
return success
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
func init_new_session(root_cipher_dir string, masterkey []byte) int {
|
||||||
|
// Initialize EME for filename encryption.
|
||||||
|
var emeCipher *eme.EMECipher
|
||||||
|
var err error
|
||||||
|
var emeBlockCipher cipher.Block
|
||||||
|
emeKey := cryptocore.HkdfDerive(masterkey, cryptocore.HkdfInfoEMENames, cryptocore.KeyLen)
|
||||||
|
emeBlockCipher, err = aes.NewCipher(emeKey)
|
||||||
|
for i := range emeKey {
|
||||||
|
emeKey[i] = 0
|
||||||
|
}
|
||||||
|
if err == nil {
|
||||||
|
var new_session SessionVars
|
||||||
|
emeCipher = eme.New(emeBlockCipher)
|
||||||
|
new_session.nameTransform = nametransform.New(emeCipher, true, true)
|
||||||
|
|
||||||
|
// Initialize contentEnc
|
||||||
|
cryptoBackend := cryptocore.BackendGoGCM
|
||||||
|
if stupidgcm.PreferOpenSSL() {
|
||||||
|
cryptoBackend = cryptocore.BackendOpenSSL
|
||||||
|
}
|
||||||
|
forcedecode := false
|
||||||
|
new_session.cryptoCore = cryptocore.New(masterkey, cryptoBackend, contentenc.DefaultIVBits, true, forcedecode)
|
||||||
|
new_session.contentEnc = contentenc.New(new_session.cryptoCore, contentenc.DefaultBS, forcedecode)
|
||||||
|
|
||||||
|
//copying root_cipher_dir
|
||||||
|
var grcd strings.Builder
|
||||||
|
grcd.WriteString(root_cipher_dir)
|
||||||
|
new_session.root_cipher_dir = grcd.String()
|
||||||
|
|
||||||
|
// New empty caches
|
||||||
|
new_session.dirCache = make(map[string]Directory)
|
||||||
|
new_session.file_handles = make(map[int]File)
|
||||||
|
new_session.fileIDs = make(map[int][]byte)
|
||||||
|
|
||||||
|
//find unused sessionID
|
||||||
|
sessionID := -1
|
||||||
|
c := 0
|
||||||
|
for sessionID == -1 {
|
||||||
|
_, ok := sessions[c]
|
||||||
|
if !ok {
|
||||||
|
sessionID = c
|
||||||
|
}
|
||||||
|
c++
|
||||||
|
}
|
||||||
|
if sessions == nil {
|
||||||
|
//fdebug, _ = os.OpenFile("/storage/emulated/0/gologs.txt", os.O_WRONLY | os.O_TRUNC | os.O_CREATE, os.FileMode(file_mode))
|
||||||
|
sessions = make(map[int]SessionVars)
|
||||||
|
}
|
||||||
|
sessions[sessionID] = new_session;
|
||||||
|
return sessionID
|
||||||
|
}
|
||||||
|
return -1
|
||||||
|
}
|
||||||
|
|
||||||
|
//export gcf_init
|
||||||
|
func gcf_init(root_cipher_dir string, password, givenScryptHash, returnedScryptHashBuff []byte) int {
|
||||||
|
sessionID := -1
|
||||||
|
cf, err := configfile.Load(filepath.Join(root_cipher_dir, configfile.ConfDefaultName))
|
||||||
|
if err == nil {
|
||||||
|
masterkey := cf.GetMasterkey(password, givenScryptHash, returnedScryptHashBuff)
|
||||||
|
if masterkey != nil {
|
||||||
|
sessionID = init_new_session(root_cipher_dir, masterkey)
|
||||||
|
wipe(masterkey)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return sessionID
|
||||||
|
}
|
||||||
|
|
||||||
|
//export gcf_close
|
||||||
|
func gcf_close(sessionID int){
|
||||||
|
sessions[sessionID].cryptoCore.Wipe()
|
||||||
|
for handleID, _ := range sessions[sessionID].file_handles {
|
||||||
|
gcf_close_file(sessionID, handleID)
|
||||||
|
}
|
||||||
|
clear_dirCache(sessionID)
|
||||||
|
delete(sessions, sessionID)
|
||||||
|
}
|
||||||
|
|
||||||
|
//export gcf_create_volume
|
||||||
|
func gcf_create_volume(root_cipher_dir string, password []byte, logN int, creator string) bool {
|
||||||
|
err := configfile.Create(filepath.Join(root_cipher_dir, configfile.ConfDefaultName), password, false, logN, creator, false, false)
|
||||||
|
if err == nil {
|
||||||
|
dirfd, err := syscall.Open(root_cipher_dir, syscall.O_DIRECTORY|syscallcompat.O_PATH, 0)
|
||||||
|
if err == nil {
|
||||||
|
err = nametransform.WriteDirIVAt(dirfd)
|
||||||
|
syscall.Close(dirfd)
|
||||||
|
return err_to_bool(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
//export gcf_change_password
|
||||||
|
func gcf_change_password(root_cipher_dir string, old_password, givenScryptHash, new_password, returnedScryptHashBuff []byte) bool {
|
||||||
|
success := false
|
||||||
|
cf, err := configfile.Load(filepath.Join(root_cipher_dir, configfile.ConfDefaultName))
|
||||||
|
if err == nil {
|
||||||
|
masterkey := cf.GetMasterkey(old_password, givenScryptHash, nil)
|
||||||
|
if masterkey != nil {
|
||||||
|
logN := cf.ScryptObject.LogN()
|
||||||
|
scryptHash := cf.EncryptKey(masterkey, new_password, logN, len(returnedScryptHashBuff)>0)
|
||||||
|
wipe(masterkey)
|
||||||
|
for i := range scryptHash {
|
||||||
|
returnedScryptHashBuff[i] = scryptHash[i]
|
||||||
|
scryptHash[i] = 0
|
||||||
|
}
|
||||||
|
success = err_to_bool(cf.WriteFile())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return success
|
||||||
|
}
|
||||||
|
|
||||||
|
//export gcf_list_dir
|
||||||
|
func gcf_list_dir(sessionID int, dirName string) (*C.char, *C.int, C.int) {
|
||||||
|
parentDirFd, cDirName, err := openBackingDir(sessionID, dirName)
|
||||||
|
if err != nil {
|
||||||
|
return nil, nil, 0
|
||||||
|
}
|
||||||
|
defer syscall.Close(parentDirFd)
|
||||||
|
// Read ciphertext directory
|
||||||
|
var cipherEntries []syscallcompat.DirEntry
|
||||||
|
fd, err := syscallcompat.Openat(parentDirFd, cDirName, syscall.O_RDONLY|syscall.O_DIRECTORY|syscall.O_NOFOLLOW, 0)
|
||||||
|
if err != nil {
|
||||||
|
return nil, nil, 0
|
||||||
|
}
|
||||||
|
defer syscall.Close(fd)
|
||||||
|
cipherEntries, err = syscallcompat.Getdents(fd)
|
||||||
|
if err != nil {
|
||||||
|
return nil, nil, 0
|
||||||
|
}
|
||||||
|
// Get DirIV (stays nil if PlaintextNames is used)
|
||||||
|
var cachedIV []byte
|
||||||
|
// Read the DirIV from disk
|
||||||
|
cachedIV, err = nametransform.ReadDirIVAt(fd)
|
||||||
|
if err != nil {
|
||||||
|
return nil, nil, 0
|
||||||
|
}
|
||||||
|
// Decrypted directory entries
|
||||||
|
var plain strings.Builder
|
||||||
|
var modes []uint32
|
||||||
|
// Filter and decrypt filenames
|
||||||
|
for i := range cipherEntries {
|
||||||
|
cName := cipherEntries[i].Name
|
||||||
|
if dirName == "" && cName == configfile.ConfDefaultName {
|
||||||
|
// silently ignore "gocryptfs.conf" in the top level dir
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if cName == nametransform.DirIVFilename {
|
||||||
|
// silently ignore "gocryptfs.diriv" everywhere if dirIV is enabled
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
// Handle long file name
|
||||||
|
isLong := nametransform.NameType(cName)
|
||||||
|
if isLong == nametransform.LongNameContent {
|
||||||
|
cNameLong, err := nametransform.ReadLongNameAt(fd, cName)
|
||||||
|
if err != nil {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
cName = cNameLong
|
||||||
|
} else if isLong == nametransform.LongNameFilename {
|
||||||
|
// ignore "gocryptfs.longname.*.name"
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
name, err := sessions[sessionID].nameTransform.DecryptName(cName, cachedIV)
|
||||||
|
if err != nil {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
// Override the ciphertext name with the plaintext name but reuse the rest
|
||||||
|
// of the structure
|
||||||
|
cipherEntries[i].Name = name
|
||||||
|
plain.WriteString(cipherEntries[i].Name+"\x00")
|
||||||
|
modes = append(modes, cipherEntries[i].Mode)
|
||||||
|
}
|
||||||
|
p := C.malloc(C.ulong(C.sizeof_int*len(modes)))
|
||||||
|
for i := 0; i < len(modes); i++ {
|
||||||
|
offset := C.sizeof_int*uintptr(i)
|
||||||
|
*(*C.int)(unsafe.Pointer(uintptr(p)+offset)) = (C.int)(modes[i])
|
||||||
|
}
|
||||||
|
return C.CString(plain.String()), (*C.int)(p), (C.int)(len(modes))
|
||||||
|
}
|
||||||
|
|
||||||
|
//export gcf_mkdir
|
||||||
|
func gcf_mkdir(sessionID int, newPath string) bool {
|
||||||
|
dirfd, cName, err := openBackingDir(sessionID, newPath)
|
||||||
|
if err != nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
defer syscall.Close(dirfd)
|
||||||
|
// We need write and execute permissions to create gocryptfs.diriv.
|
||||||
|
// Also, we need read permissions to open the directory (to avoid
|
||||||
|
// race-conditions between getting and setting the mode).
|
||||||
|
origMode := folder_mode
|
||||||
|
mode := folder_mode | 0700
|
||||||
|
|
||||||
|
// Handle long file name
|
||||||
|
if nametransform.IsLongContent(cName) {
|
||||||
|
// Create ".name"
|
||||||
|
err = sessions[sessionID].nameTransform.WriteLongNameAt(dirfd, cName, newPath)
|
||||||
|
if err != nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create directory
|
||||||
|
err = mkdirWithIv(dirfd, cName, mode)
|
||||||
|
if err != nil {
|
||||||
|
nametransform.DeleteLongNameAt(dirfd, cName)
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
err = mkdirWithIv(dirfd, cName, mode)
|
||||||
|
if err != nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Set mode
|
||||||
|
if origMode != mode {
|
||||||
|
dirfd2, err := syscallcompat.Openat(dirfd, cName,
|
||||||
|
syscall.O_RDONLY|syscall.O_DIRECTORY|syscall.O_NOFOLLOW, 0)
|
||||||
|
if err != nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
defer syscall.Close(dirfd2)
|
||||||
|
|
||||||
|
var st syscall.Stat_t
|
||||||
|
err = syscall.Fstat(dirfd2, &st)
|
||||||
|
if err != nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
// Preserve SGID bit if it was set due to inheritance.
|
||||||
|
origMode = uint32(st.Mode&^0777) | origMode
|
||||||
|
err = syscall.Fchmod(dirfd2, origMode)
|
||||||
|
if err != nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
//export gcf_rmdir
|
||||||
|
func gcf_rmdir(sessionID int, relPath string) bool {
|
||||||
|
defer clear_dirCache(sessionID)
|
||||||
|
parentDirFd, cName, err := openBackingDir(sessionID, relPath)
|
||||||
|
if err != nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
defer syscall.Close(parentDirFd)
|
||||||
|
dirfd, err := syscallcompat.Openat(parentDirFd, cName, syscall.O_RDONLY|syscall.O_DIRECTORY|syscall.O_NOFOLLOW, 0)
|
||||||
|
if err != nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
defer syscall.Close(dirfd)
|
||||||
|
// Check directory contents
|
||||||
|
children, err := syscallcompat.Getdents(dirfd)
|
||||||
|
if err == io.EOF {
|
||||||
|
// The directory is empty
|
||||||
|
err = unix.Unlinkat(parentDirFd, cName, unix.AT_REMOVEDIR)
|
||||||
|
return err_to_bool(err)
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
// If the directory is not empty besides gocryptfs.diriv, do not even
|
||||||
|
// attempt the dance around gocryptfs.diriv.
|
||||||
|
if len(children) > 1 {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
// Move "gocryptfs.diriv" to the parent dir as "gocryptfs.diriv.rmdir.XYZ"
|
||||||
|
tmpName := fmt.Sprintf("%s.rmdir.%d", nametransform.DirIVFilename, cryptocore.RandUint64())
|
||||||
|
err = syscallcompat.Renameat(dirfd, nametransform.DirIVFilename, parentDirFd, tmpName)
|
||||||
|
if err != nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
// Actual Rmdir
|
||||||
|
err = syscallcompat.Unlinkat(parentDirFd, cName, unix.AT_REMOVEDIR)
|
||||||
|
if err != nil {
|
||||||
|
// This can happen if another file in the directory was created in the
|
||||||
|
// meantime, undo the rename
|
||||||
|
err2 := syscallcompat.Renameat(parentDirFd, tmpName, dirfd, nametransform.DirIVFilename)
|
||||||
|
return err_to_bool(err2)
|
||||||
|
}
|
||||||
|
// Delete "gocryptfs.diriv.rmdir.XYZ"
|
||||||
|
err = syscallcompat.Unlinkat(parentDirFd, tmpName, 0)
|
||||||
|
// Delete .name file
|
||||||
|
if nametransform.IsLongContent(cName) {
|
||||||
|
nametransform.DeleteLongNameAt(parentDirFd, cName)
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
//export gcf_open_read_mode
|
||||||
|
func gcf_open_read_mode(sessionID int, path string) int {
|
||||||
|
newFlags := mangleOpenFlags(0)
|
||||||
|
dirfd, cName, err := openBackingDir(sessionID, path)
|
||||||
|
if err != nil {
|
||||||
|
return -1
|
||||||
|
}
|
||||||
|
defer syscall.Close(dirfd)
|
||||||
|
fd, err := syscallcompat.Openat(dirfd, cName, newFlags, 0)
|
||||||
|
// Handle a few specific errors
|
||||||
|
if err != nil {
|
||||||
|
return -1
|
||||||
|
}
|
||||||
|
return register_file_handle(sessionID, File{os.NewFile(uintptr(fd), cName), path})
|
||||||
|
}
|
||||||
|
|
||||||
|
//export gcf_open_write_mode
|
||||||
|
func gcf_open_write_mode(sessionID int, path string) int {
|
||||||
|
newFlags := mangleOpenFlags(syscall.O_RDWR)
|
||||||
|
dirfd, cName, err := openBackingDir(sessionID, path)
|
||||||
|
if err != nil {
|
||||||
|
return -1
|
||||||
|
}
|
||||||
|
defer syscall.Close(dirfd)
|
||||||
|
fd := -1
|
||||||
|
// Handle long file name
|
||||||
|
if nametransform.IsLongContent(cName) {
|
||||||
|
// Create ".name"
|
||||||
|
err = sessions[sessionID].nameTransform.WriteLongNameAt(dirfd, cName, path)
|
||||||
|
if err != nil {
|
||||||
|
return -1
|
||||||
|
}
|
||||||
|
// Create content
|
||||||
|
fd, err = syscallcompat.Openat(dirfd, cName, newFlags|syscall.O_CREAT, file_mode)
|
||||||
|
if err != nil {
|
||||||
|
nametransform.DeleteLongNameAt(dirfd, cName)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// Create content, normal (short) file name
|
||||||
|
fd, err = syscallcompat.Openat(dirfd, cName, newFlags|syscall.O_CREAT, file_mode)
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
// xfstests generic/488 triggers this
|
||||||
|
if err == syscall.EMFILE {
|
||||||
|
var lim syscall.Rlimit
|
||||||
|
syscall.Getrlimit(syscall.RLIMIT_NOFILE, &lim)
|
||||||
|
}
|
||||||
|
return -1
|
||||||
|
}
|
||||||
|
return register_file_handle(sessionID, File{os.NewFile(uintptr(fd), cName), path})
|
||||||
|
}
|
||||||
|
|
||||||
|
//export gcf_truncate
|
||||||
|
func gcf_truncate(sessionID int, path string, offset uint64) bool {
|
||||||
|
handleID := gcf_open_write_mode(sessionID, path)
|
||||||
|
if handleID != -1 {
|
||||||
|
success := truncate(sessionID, handleID, offset)
|
||||||
|
gcf_close_file(sessionID, handleID)
|
||||||
|
return success
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
//export gcf_close_file
|
||||||
|
func gcf_close_file(sessionID, handleID int){
|
||||||
|
f, ok := sessions[sessionID].file_handles[handleID]
|
||||||
|
if ok {
|
||||||
|
f.fd.Close()
|
||||||
|
delete(sessions[sessionID].file_handles, handleID)
|
||||||
|
_, ok := sessions[sessionID].fileIDs[handleID]
|
||||||
|
if ok {
|
||||||
|
delete(sessions[sessionID].fileIDs, handleID)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//export gcf_read_file
|
||||||
|
func gcf_read_file(sessionID, handleID int, offset uint64, dst_buff []byte) uint32 {
|
||||||
|
length := uint64(len(dst_buff))
|
||||||
|
if length > contentenc.MAX_KERNEL_WRITE {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
out, _ := doRead(sessionID, handleID, dst_buff[:0], offset, length)
|
||||||
|
|
||||||
|
return uint32(len(out))
|
||||||
|
}
|
||||||
|
|
||||||
|
//export gcf_write_file
|
||||||
|
func gcf_write_file(sessionID, handleID int, offset uint64, data []byte) uint32 {
|
||||||
|
length := uint64(len(data))
|
||||||
|
if length > contentenc.MAX_KERNEL_WRITE {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
written, _ := doWrite(sessionID, handleID, data, offset)
|
||||||
|
|
||||||
|
return written
|
||||||
|
}
|
||||||
|
|
||||||
|
//export gcf_get_attrs
|
||||||
|
func gcf_get_attrs(sessionID int, relPath string) (uint64, int64, bool) {
|
||||||
|
dirfd, cName, err := openBackingDir(sessionID, relPath)
|
||||||
|
if err != nil {
|
||||||
|
return 0, 0, false
|
||||||
|
}
|
||||||
|
var st unix.Stat_t
|
||||||
|
err = syscallcompat.Fstatat(dirfd, cName, &st, unix.AT_SYMLINK_NOFOLLOW)
|
||||||
|
syscall.Close(dirfd)
|
||||||
|
if err != nil {
|
||||||
|
return 0, 0, false
|
||||||
|
}
|
||||||
|
return sessions[sessionID].contentEnc.CipherSizeToPlainSize(uint64(st.Size)), st.Mtim.Sec, true
|
||||||
|
}
|
||||||
|
|
||||||
|
//export gcf_rename
|
||||||
|
func gcf_rename(sessionID int, oldPath string, newPath string) bool {
|
||||||
|
defer clear_dirCache(sessionID)
|
||||||
|
oldDirfd, oldCName, err := openBackingDir(sessionID, oldPath)
|
||||||
|
if err != nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
defer syscall.Close(oldDirfd)
|
||||||
|
newDirfd, newCName, err := openBackingDir(sessionID, newPath)
|
||||||
|
if err != nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
defer syscall.Close(newDirfd)
|
||||||
|
// Long destination file name: create .name file
|
||||||
|
nameFileAlreadyThere := false
|
||||||
|
if nametransform.IsLongContent(newCName) {
|
||||||
|
err = sessions[sessionID].nameTransform.WriteLongNameAt(newDirfd, newCName, newPath)
|
||||||
|
// Failure to write the .name file is expected when the target path already
|
||||||
|
// exists. Since hashes are pretty unique, there is no need to modify the
|
||||||
|
// .name file in this case, and we ignore the error.
|
||||||
|
if err == syscall.EEXIST {
|
||||||
|
nameFileAlreadyThere = true
|
||||||
|
} else if err != nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Actual rename
|
||||||
|
err = syscallcompat.Renameat(oldDirfd, oldCName, newDirfd, newCName)
|
||||||
|
if err == syscall.ENOTEMPTY || err == syscall.EEXIST {
|
||||||
|
// If an empty directory is overwritten we will always get an error as
|
||||||
|
// the "empty" directory will still contain gocryptfs.diriv.
|
||||||
|
// Interestingly, ext4 returns ENOTEMPTY while xfs returns EEXIST.
|
||||||
|
// We handle that by trying to fs.Rmdir() the target directory and trying
|
||||||
|
// again.
|
||||||
|
if gcf_rmdir(sessionID, newPath) {
|
||||||
|
err = syscallcompat.Renameat(oldDirfd, oldCName, newDirfd, newCName)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
if nametransform.IsLongContent(newCName) && nameFileAlreadyThere == false {
|
||||||
|
// Roll back .name creation unless the .name file was already there
|
||||||
|
nametransform.DeleteLongNameAt(newDirfd, newCName)
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
if nametransform.IsLongContent(oldCName) {
|
||||||
|
nametransform.DeleteLongNameAt(oldDirfd, oldCName)
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
//export gcf_remove_file
|
||||||
|
func gcf_remove_file(sessionID int, path string) bool {
|
||||||
|
dirfd, cName, err := openBackingDir(sessionID, path)
|
||||||
|
if err != nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
defer syscall.Close(dirfd)
|
||||||
|
// Delete content
|
||||||
|
err = syscallcompat.Unlinkat(dirfd, cName, 0)
|
||||||
|
if err != nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
// Delete ".name" file
|
||||||
|
if nametransform.IsLongContent(cName) {
|
||||||
|
err = nametransform.DeleteLongNameAt(dirfd, cName)
|
||||||
|
}
|
||||||
|
return err_to_bool(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
func main(){}
|
325
app/libgocryptfs/rewrites/configfile/config_file.go
Normal file
325
app/libgocryptfs/rewrites/configfile/config_file.go
Normal file
@ -0,0 +1,325 @@
|
|||||||
|
// Package configfile reads and writes gocryptfs.conf does the key
|
||||||
|
// wrapping.
|
||||||
|
package configfile
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"io/ioutil"
|
||||||
|
"log"
|
||||||
|
"syscall"
|
||||||
|
|
||||||
|
"../contentenc"
|
||||||
|
"../../gocryptfs_internal/cryptocore"
|
||||||
|
"../../gocryptfs_internal/exitcodes"
|
||||||
|
)
|
||||||
|
import "os"
|
||||||
|
|
||||||
|
const (
|
||||||
|
// ConfDefaultName is the default configuration file name.
|
||||||
|
// The dot "." is not used in base64url (RFC4648), hence
|
||||||
|
// we can never clash with an encrypted file.
|
||||||
|
ConfDefaultName = "gocryptfs.conf"
|
||||||
|
// ConfReverseName is the default configuration file name in reverse mode,
|
||||||
|
// the config file gets stored next to the plain-text files. Make it hidden
|
||||||
|
// (start with dot) to not annoy the user.
|
||||||
|
ConfReverseName = ".gocryptfs.reverse.conf"
|
||||||
|
)
|
||||||
|
|
||||||
|
// ConfFile is the content of a config file.
|
||||||
|
type ConfFile struct {
|
||||||
|
// Creator is the gocryptfs version string.
|
||||||
|
// This only documents the config file for humans who look at it. The actual
|
||||||
|
// technical info is contained in FeatureFlags.
|
||||||
|
Creator string
|
||||||
|
// EncryptedKey holds an encrypted AES key, unlocked using a password
|
||||||
|
// hashed with scrypt
|
||||||
|
EncryptedKey []byte
|
||||||
|
// ScryptObject stores parameters for scrypt hashing (key derivation)
|
||||||
|
ScryptObject ScryptKDF
|
||||||
|
// Version is the On-Disk-Format version this filesystem uses
|
||||||
|
Version uint16
|
||||||
|
// FeatureFlags is a list of feature flags this filesystem has enabled.
|
||||||
|
// If gocryptfs encounters a feature flag it does not support, it will refuse
|
||||||
|
// mounting. This mechanism is analogous to the ext4 feature flags that are
|
||||||
|
// stored in the superblock.
|
||||||
|
FeatureFlags []string
|
||||||
|
// Filename is the name of the config file. Not exported to JSON.
|
||||||
|
filename string
|
||||||
|
}
|
||||||
|
|
||||||
|
// randBytesDevRandom gets "n" random bytes from /dev/random or panics
|
||||||
|
func randBytesDevRandom(n int) []byte {
|
||||||
|
f, err := os.Open("/dev/random")
|
||||||
|
if err != nil {
|
||||||
|
log.Panic("Failed to open /dev/random: " + err.Error())
|
||||||
|
}
|
||||||
|
defer f.Close()
|
||||||
|
b := make([]byte, n)
|
||||||
|
_, err = io.ReadFull(f, b)
|
||||||
|
if err != nil {
|
||||||
|
log.Panic("Failed to read random bytes: " + err.Error())
|
||||||
|
}
|
||||||
|
return b
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create - create a new config with a random key encrypted with
|
||||||
|
// "password" and write it to "filename".
|
||||||
|
// Uses scrypt with cost parameter logN.
|
||||||
|
func Create(filename string, password []byte, plaintextNames bool,
|
||||||
|
logN int, creator string, aessiv bool, devrandom bool) error {
|
||||||
|
var cf ConfFile
|
||||||
|
cf.filename = filename
|
||||||
|
cf.Creator = creator
|
||||||
|
cf.Version = contentenc.CurrentVersion
|
||||||
|
|
||||||
|
// Set feature flags
|
||||||
|
cf.FeatureFlags = append(cf.FeatureFlags, knownFlags[FlagGCMIV128])
|
||||||
|
cf.FeatureFlags = append(cf.FeatureFlags, knownFlags[FlagHKDF])
|
||||||
|
if plaintextNames {
|
||||||
|
cf.FeatureFlags = append(cf.FeatureFlags, knownFlags[FlagPlaintextNames])
|
||||||
|
} else {
|
||||||
|
cf.FeatureFlags = append(cf.FeatureFlags, knownFlags[FlagDirIV])
|
||||||
|
cf.FeatureFlags = append(cf.FeatureFlags, knownFlags[FlagEMENames])
|
||||||
|
cf.FeatureFlags = append(cf.FeatureFlags, knownFlags[FlagLongNames])
|
||||||
|
cf.FeatureFlags = append(cf.FeatureFlags, knownFlags[FlagRaw64])
|
||||||
|
}
|
||||||
|
if aessiv {
|
||||||
|
cf.FeatureFlags = append(cf.FeatureFlags, knownFlags[FlagAESSIV])
|
||||||
|
}
|
||||||
|
{
|
||||||
|
// Generate new random master key
|
||||||
|
var key []byte
|
||||||
|
if devrandom {
|
||||||
|
key = randBytesDevRandom(cryptocore.KeyLen)
|
||||||
|
} else {
|
||||||
|
key = cryptocore.RandBytes(cryptocore.KeyLen)
|
||||||
|
}
|
||||||
|
// Encrypt it using the password
|
||||||
|
// This sets ScryptObject and EncryptedKey
|
||||||
|
// Note: this looks at the FeatureFlags, so call it AFTER setting them.
|
||||||
|
cf.EncryptKey(key, password, logN, false)
|
||||||
|
for i := range key {
|
||||||
|
key[i] = 0
|
||||||
|
}
|
||||||
|
// key runs out of scope here
|
||||||
|
}
|
||||||
|
// Write file to disk
|
||||||
|
return cf.WriteFile()
|
||||||
|
}
|
||||||
|
|
||||||
|
// LoadAndDecrypt - read config file from disk and decrypt the
|
||||||
|
// contained key using "password".
|
||||||
|
// Returns the decrypted key and the ConfFile object
|
||||||
|
//
|
||||||
|
// If "password" is empty, the config file is read
|
||||||
|
// but the key is not decrypted (returns nil in its place).
|
||||||
|
func LoadAndDecrypt(filename string, password []byte) ([]byte, *ConfFile, error) {
|
||||||
|
cf, err := Load(filename)
|
||||||
|
if err != nil {
|
||||||
|
return nil, nil, err
|
||||||
|
}
|
||||||
|
if len(password) == 0 {
|
||||||
|
// We have validated the config file, but without a password we cannot
|
||||||
|
// decrypt the master key. Return only the parsed config.
|
||||||
|
return nil, cf, nil
|
||||||
|
// TODO: Make this an error in gocryptfs v1.7. All code should now call
|
||||||
|
// Load() instead of calling LoadAndDecrypt() with an empty password.
|
||||||
|
}
|
||||||
|
|
||||||
|
// Decrypt the masterkey using the password
|
||||||
|
key, _, err := cf.DecryptMasterKey(password, false)
|
||||||
|
if err != nil {
|
||||||
|
return nil, nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return key, cf, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Load loads and parses the config file at "filename".
|
||||||
|
func Load(filename string) (*ConfFile, error) {
|
||||||
|
var cf ConfFile
|
||||||
|
cf.filename = filename
|
||||||
|
|
||||||
|
// Read from disk
|
||||||
|
js, err := ioutil.ReadFile(filename)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if len(js) == 0 {
|
||||||
|
return nil, fmt.Errorf("Config file is empty")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Unmarshal
|
||||||
|
err = json.Unmarshal(js, &cf)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
if cf.Version != contentenc.CurrentVersion {
|
||||||
|
return nil, fmt.Errorf("Unsupported on-disk format %d", cf.Version)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check that all set feature flags are known
|
||||||
|
for _, flag := range cf.FeatureFlags {
|
||||||
|
if !cf.isFeatureFlagKnown(flag) {
|
||||||
|
return nil, fmt.Errorf("Unsupported feature flag %q", flag)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check that all required feature flags are set
|
||||||
|
var requiredFlags []flagIota
|
||||||
|
if cf.IsFeatureFlagSet(FlagPlaintextNames) {
|
||||||
|
requiredFlags = requiredFlagsPlaintextNames
|
||||||
|
} else {
|
||||||
|
requiredFlags = requiredFlagsNormal
|
||||||
|
}
|
||||||
|
deprecatedFs := false
|
||||||
|
for _, i := range requiredFlags {
|
||||||
|
if !cf.IsFeatureFlagSet(i) {
|
||||||
|
fmt.Fprintf(os.Stderr, "Required feature flag %q is missing\n", knownFlags[i])
|
||||||
|
deprecatedFs = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if deprecatedFs {
|
||||||
|
return nil, exitcodes.NewErr("Deprecated filesystem", exitcodes.DeprecatedFS)
|
||||||
|
}
|
||||||
|
|
||||||
|
// All good
|
||||||
|
return &cf, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// DecryptMasterKey decrypts the masterkey stored in cf.EncryptedKey using
|
||||||
|
// password.
|
||||||
|
func (cf *ConfFile) DecryptMasterKey(password []byte, giveHash bool) (masterkey, scryptHash []byte, err error) {
|
||||||
|
// Generate derived key from password
|
||||||
|
scryptHash = cf.ScryptObject.DeriveKey(password)
|
||||||
|
|
||||||
|
// Unlock master key using password-based key
|
||||||
|
useHKDF := cf.IsFeatureFlagSet(FlagHKDF)
|
||||||
|
ce := GetKeyEncrypter(scryptHash, useHKDF)
|
||||||
|
|
||||||
|
masterkey, err = ce.DecryptBlock(cf.EncryptedKey, 0, nil)
|
||||||
|
|
||||||
|
ce.Wipe()
|
||||||
|
ce = nil
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return nil, nil, exitcodes.NewErr("Password incorrect.", exitcodes.PasswordIncorrect)
|
||||||
|
}
|
||||||
|
|
||||||
|
if !giveHash {
|
||||||
|
// Purge scrypt-derived key
|
||||||
|
for i := range scryptHash {
|
||||||
|
scryptHash[i] = 0
|
||||||
|
}
|
||||||
|
scryptHash = nil
|
||||||
|
}
|
||||||
|
|
||||||
|
return masterkey, scryptHash, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// EncryptKey - encrypt "key" using an scrypt hash generated from "password"
|
||||||
|
// and store it in cf.EncryptedKey.
|
||||||
|
// Uses scrypt with cost parameter logN and stores the scrypt parameters in
|
||||||
|
// cf.ScryptObject.
|
||||||
|
func (cf *ConfFile) EncryptKey(key []byte, password []byte, logN int, giveHash bool) []byte {
|
||||||
|
// Generate scrypt-derived key from password
|
||||||
|
cf.ScryptObject = NewScryptKDF(logN)
|
||||||
|
scryptHash := cf.ScryptObject.DeriveKey(password)
|
||||||
|
|
||||||
|
// Lock master key using password-based key
|
||||||
|
useHKDF := cf.IsFeatureFlagSet(FlagHKDF)
|
||||||
|
ce := GetKeyEncrypter(scryptHash, useHKDF)
|
||||||
|
cf.EncryptedKey = ce.EncryptBlock(key, 0, nil)
|
||||||
|
|
||||||
|
if !giveHash {
|
||||||
|
// Purge scrypt-derived key
|
||||||
|
for i := range scryptHash {
|
||||||
|
scryptHash[i] = 0
|
||||||
|
}
|
||||||
|
scryptHash = nil
|
||||||
|
}
|
||||||
|
ce.Wipe()
|
||||||
|
ce = nil
|
||||||
|
|
||||||
|
return scryptHash
|
||||||
|
}
|
||||||
|
|
||||||
|
// DroidFS function to allow masterkey to be decrypted directely using the scrypt hash and return it if requested
|
||||||
|
func (cf *ConfFile) GetMasterkey(password, givenScryptHash, returnedScryptHashBuff []byte) []byte {
|
||||||
|
var masterkey []byte
|
||||||
|
var err error
|
||||||
|
var scryptHash []byte
|
||||||
|
if len(givenScryptHash) > 0 { //decrypt with hash
|
||||||
|
useHKDF := cf.IsFeatureFlagSet(FlagHKDF)
|
||||||
|
ce := GetKeyEncrypter(givenScryptHash, useHKDF)
|
||||||
|
masterkey, err = ce.DecryptBlock(cf.EncryptedKey, 0, nil)
|
||||||
|
ce.Wipe()
|
||||||
|
ce = nil
|
||||||
|
if err == nil {
|
||||||
|
return masterkey
|
||||||
|
}
|
||||||
|
} else { //decrypt with password
|
||||||
|
masterkey, scryptHash, err = cf.DecryptMasterKey(password, len(returnedScryptHashBuff)>0)
|
||||||
|
//copy and wipe scryptHash
|
||||||
|
for i := range scryptHash {
|
||||||
|
returnedScryptHashBuff[i] = scryptHash[i]
|
||||||
|
scryptHash[i] = 0
|
||||||
|
}
|
||||||
|
if err == nil {
|
||||||
|
return masterkey
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// WriteFile - write out config in JSON format to file "filename.tmp"
|
||||||
|
// then rename over "filename".
|
||||||
|
// This way a password change atomically replaces the file.
|
||||||
|
func (cf *ConfFile) WriteFile() error {
|
||||||
|
tmp := cf.filename + ".tmp"
|
||||||
|
// 0400 permissions: gocryptfs.conf should be kept secret and never be written to.
|
||||||
|
fd, err := os.OpenFile(tmp, os.O_WRONLY|os.O_CREATE|os.O_EXCL, 0400)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
js, err := json.MarshalIndent(cf, "", "\t")
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
// For convenience for the user, add a newline at the end.
|
||||||
|
js = append(js, '\n')
|
||||||
|
_, err = fd.Write(js)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
err = fd.Sync()
|
||||||
|
if err != nil {
|
||||||
|
// This can happen on network drives: FRITZ.NAS mounted on MacOS returns
|
||||||
|
// "operation not supported": https://github.com/rfjakob/gocryptfs/issues/390
|
||||||
|
// Try sync instead
|
||||||
|
syscall.Sync()
|
||||||
|
}
|
||||||
|
err = fd.Close()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
err = os.Rename(tmp, cf.filename)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// getKeyEncrypter is a helper function that returns the right ContentEnc
|
||||||
|
// instance for the "useHKDF" setting.
|
||||||
|
func GetKeyEncrypter(scryptHash []byte, useHKDF bool) *contentenc.ContentEnc {
|
||||||
|
IVLen := 96
|
||||||
|
// gocryptfs v1.2 and older used 96-bit IVs for master key encryption.
|
||||||
|
// v1.3 adds the "HKDF" feature flag, which also enables 128-bit nonces.
|
||||||
|
if useHKDF {
|
||||||
|
IVLen = contentenc.DefaultIVBits
|
||||||
|
}
|
||||||
|
cc := cryptocore.New(scryptHash, cryptocore.BackendGoGCM, IVLen, useHKDF, false)
|
||||||
|
ce := contentenc.New(cc, 4096, false)
|
||||||
|
return ce
|
||||||
|
}
|
74
app/libgocryptfs/rewrites/configfile/feature_flags.go
Normal file
74
app/libgocryptfs/rewrites/configfile/feature_flags.go
Normal file
@ -0,0 +1,74 @@
|
|||||||
|
package configfile
|
||||||
|
|
||||||
|
type flagIota int
|
||||||
|
|
||||||
|
const (
|
||||||
|
// FlagPlaintextNames indicates that filenames are unencrypted.
|
||||||
|
FlagPlaintextNames flagIota = iota
|
||||||
|
// FlagDirIV indicates that a per-directory IV file is used.
|
||||||
|
FlagDirIV
|
||||||
|
// FlagEMENames indicates EME (ECB-Mix-ECB) filename encryption.
|
||||||
|
// This flag is mandatory since gocryptfs v1.0.
|
||||||
|
FlagEMENames
|
||||||
|
// FlagGCMIV128 indicates 128-bit GCM IVs.
|
||||||
|
// This flag is mandatory since gocryptfs v1.0.
|
||||||
|
FlagGCMIV128
|
||||||
|
// FlagLongNames allows file names longer than 176 bytes.
|
||||||
|
FlagLongNames
|
||||||
|
// FlagAESSIV selects an AES-SIV based crypto backend.
|
||||||
|
FlagAESSIV
|
||||||
|
// FlagRaw64 enables raw (unpadded) base64 encoding for file names
|
||||||
|
FlagRaw64
|
||||||
|
// FlagHKDF enables HKDF-derived keys for use with GCM, EME and SIV
|
||||||
|
// instead of directly using the master key (GCM and EME) or the SHA-512
|
||||||
|
// hashed master key (SIV).
|
||||||
|
// Note that this flag does not change the password hashing algorithm
|
||||||
|
// which always is scrypt.
|
||||||
|
FlagHKDF
|
||||||
|
)
|
||||||
|
|
||||||
|
// knownFlags stores the known feature flags and their string representation
|
||||||
|
var knownFlags = map[flagIota]string{
|
||||||
|
FlagPlaintextNames: "PlaintextNames",
|
||||||
|
FlagDirIV: "DirIV",
|
||||||
|
FlagEMENames: "EMENames",
|
||||||
|
FlagGCMIV128: "GCMIV128",
|
||||||
|
FlagLongNames: "LongNames",
|
||||||
|
FlagAESSIV: "AESSIV",
|
||||||
|
FlagRaw64: "Raw64",
|
||||||
|
FlagHKDF: "HKDF",
|
||||||
|
}
|
||||||
|
|
||||||
|
// Filesystems that do not have these feature flags set are deprecated.
|
||||||
|
var requiredFlagsNormal = []flagIota{
|
||||||
|
FlagDirIV,
|
||||||
|
FlagEMENames,
|
||||||
|
FlagGCMIV128,
|
||||||
|
}
|
||||||
|
|
||||||
|
// Filesystems without filename encryption obviously don't have or need the
|
||||||
|
// filename related feature flags.
|
||||||
|
var requiredFlagsPlaintextNames = []flagIota{
|
||||||
|
FlagGCMIV128,
|
||||||
|
}
|
||||||
|
|
||||||
|
// isFeatureFlagKnown verifies that we understand a feature flag.
|
||||||
|
func (cf *ConfFile) isFeatureFlagKnown(flag string) bool {
|
||||||
|
for _, knownFlag := range knownFlags {
|
||||||
|
if knownFlag == flag {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
// IsFeatureFlagSet returns true if the feature flag "flagWant" is enabled.
|
||||||
|
func (cf *ConfFile) IsFeatureFlagSet(flagWant flagIota) bool {
|
||||||
|
flagString := knownFlags[flagWant]
|
||||||
|
for _, flag := range cf.FeatureFlags {
|
||||||
|
if flag == flagString {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
101
app/libgocryptfs/rewrites/configfile/scrypt.go
Normal file
101
app/libgocryptfs/rewrites/configfile/scrypt.go
Normal file
@ -0,0 +1,101 @@
|
|||||||
|
package configfile
|
||||||
|
|
||||||
|
import (
|
||||||
|
"log"
|
||||||
|
"math"
|
||||||
|
|
||||||
|
"golang.org/x/crypto/scrypt"
|
||||||
|
|
||||||
|
"../../gocryptfs_internal/cryptocore"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
// ScryptDefaultLogN is the default scrypt logN configuration parameter.
|
||||||
|
// logN=16 (N=2^16) uses 64MB of memory and takes 4 seconds on my Atom Z3735F
|
||||||
|
// netbook.
|
||||||
|
ScryptDefaultLogN = 16
|
||||||
|
// From RFC7914, section 2:
|
||||||
|
// At the current time, r=8 and p=1 appears to yield good
|
||||||
|
// results, but as memory latency and CPU parallelism increase, it is
|
||||||
|
// likely that the optimum values for both r and p will increase.
|
||||||
|
// We reject all lower values that we might get through modified config files.
|
||||||
|
scryptMinR = 8
|
||||||
|
scryptMinP = 1
|
||||||
|
// logN=10 takes 6ms on a Pentium G630. This should be fast enough for all
|
||||||
|
// purposes. We reject lower values.
|
||||||
|
scryptMinLogN = 10
|
||||||
|
// We always generate 32-byte salts. Anything smaller than that is rejected.
|
||||||
|
scryptMinSaltLen = 32
|
||||||
|
)
|
||||||
|
|
||||||
|
// ScryptKDF is an instance of the scrypt key deriviation function.
|
||||||
|
type ScryptKDF struct {
|
||||||
|
// Salt is the random salt that is passed to scrypt
|
||||||
|
Salt []byte
|
||||||
|
// N: scrypt CPU/Memory cost parameter
|
||||||
|
N int
|
||||||
|
// R: scrypt block size parameter
|
||||||
|
R int
|
||||||
|
// P: scrypt parallelization parameter
|
||||||
|
P int
|
||||||
|
// KeyLen is the output data length
|
||||||
|
KeyLen int
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewScryptKDF returns a new instance of ScryptKDF.
|
||||||
|
func NewScryptKDF(logN int) ScryptKDF {
|
||||||
|
var s ScryptKDF
|
||||||
|
s.Salt = cryptocore.RandBytes(cryptocore.KeyLen)
|
||||||
|
if logN <= 0 {
|
||||||
|
s.N = 1 << ScryptDefaultLogN
|
||||||
|
} else {
|
||||||
|
s.N = 1 << uint32(logN)
|
||||||
|
}
|
||||||
|
s.R = 8 // Always 8
|
||||||
|
s.P = 1 // Always 1
|
||||||
|
s.KeyLen = cryptocore.KeyLen
|
||||||
|
return s
|
||||||
|
}
|
||||||
|
|
||||||
|
// DeriveKey returns a new key from a supplied password.
|
||||||
|
func (s *ScryptKDF) DeriveKey(pw []byte) []byte {
|
||||||
|
if s.validateParams() {
|
||||||
|
k, err := scrypt.Key(pw, s.Salt, s.N, s.R, s.P, s.KeyLen)
|
||||||
|
if err != nil {
|
||||||
|
log.Panicf("DeriveKey failed: %v", err)
|
||||||
|
}
|
||||||
|
return k
|
||||||
|
} else {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// LogN - N is saved as 2^LogN, but LogN is much easier to work with.
|
||||||
|
// This function gives you LogN = Log2(N).
|
||||||
|
func (s *ScryptKDF) LogN() int {
|
||||||
|
return int(math.Log2(float64(s.N)) + 0.5)
|
||||||
|
}
|
||||||
|
|
||||||
|
// validateParams checks that all parameters are at or above hardcoded limits.
|
||||||
|
// If not, it exists with an error message.
|
||||||
|
// This makes sure we do not get weak parameters passed through a
|
||||||
|
// rougue gocryptfs.conf.
|
||||||
|
func (s *ScryptKDF) validateParams() bool {
|
||||||
|
minN := 1 << scryptMinLogN
|
||||||
|
if s.N < minN {
|
||||||
|
return false//os.Exit(exitcodes.ScryptParams)
|
||||||
|
}
|
||||||
|
if s.R < scryptMinR {
|
||||||
|
return false//os.Exit(exitcodes.ScryptParams)
|
||||||
|
}
|
||||||
|
if s.P < scryptMinP {
|
||||||
|
return false//os.Exit(exitcodes.ScryptParams)
|
||||||
|
}
|
||||||
|
if len(s.Salt) < scryptMinSaltLen {
|
||||||
|
return false//os.Exit(exitcodes.ScryptParams)
|
||||||
|
}
|
||||||
|
if s.KeyLen < cryptocore.KeyLen {
|
||||||
|
return false//os.Exit(exitcodes.ScryptParams)
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
39
app/libgocryptfs/rewrites/contentenc/bpool.go
Normal file
39
app/libgocryptfs/rewrites/contentenc/bpool.go
Normal file
@ -0,0 +1,39 @@
|
|||||||
|
package contentenc
|
||||||
|
|
||||||
|
import (
|
||||||
|
"log"
|
||||||
|
"sync"
|
||||||
|
)
|
||||||
|
|
||||||
|
// bPool is a byte slice pool
|
||||||
|
type bPool struct {
|
||||||
|
sync.Pool
|
||||||
|
sliceLen int
|
||||||
|
}
|
||||||
|
|
||||||
|
func newBPool(sliceLen int) bPool {
|
||||||
|
return bPool{
|
||||||
|
Pool: sync.Pool{
|
||||||
|
New: func() interface{} { return make([]byte, sliceLen) },
|
||||||
|
},
|
||||||
|
sliceLen: sliceLen,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Put grows the slice "s" to its maximum capacity and puts it into the pool.
|
||||||
|
func (b *bPool) Put(s []byte) {
|
||||||
|
s = s[:cap(s)]
|
||||||
|
if len(s) != b.sliceLen {
|
||||||
|
log.Panicf("wrong len=%d, want=%d", len(s), b.sliceLen)
|
||||||
|
}
|
||||||
|
b.Pool.Put(s)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get returns a byte slice from the pool.
|
||||||
|
func (b *bPool) Get() (s []byte) {
|
||||||
|
s = b.Pool.Get().([]byte)
|
||||||
|
if len(s) != b.sliceLen {
|
||||||
|
log.Panicf("wrong len=%d, want=%d", len(s), b.sliceLen)
|
||||||
|
}
|
||||||
|
return s
|
||||||
|
}
|
335
app/libgocryptfs/rewrites/contentenc/content.go
Normal file
335
app/libgocryptfs/rewrites/contentenc/content.go
Normal file
@ -0,0 +1,335 @@
|
|||||||
|
// Package contentenc encrypts and decrypts file blocks.
|
||||||
|
package contentenc
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"encoding/binary"
|
||||||
|
"errors"
|
||||||
|
"log"
|
||||||
|
"runtime"
|
||||||
|
"sync"
|
||||||
|
|
||||||
|
"../../gocryptfs_internal/cryptocore"
|
||||||
|
"../../gocryptfs_internal/stupidgcm"
|
||||||
|
)
|
||||||
|
|
||||||
|
// NonceMode determines how nonces are created.
|
||||||
|
type NonceMode int
|
||||||
|
|
||||||
|
const (
|
||||||
|
//value from FUSE doc
|
||||||
|
MAX_KERNEL_WRITE = 128 * 1024
|
||||||
|
|
||||||
|
|
||||||
|
// DefaultBS is the default plaintext block size
|
||||||
|
DefaultBS = 4096
|
||||||
|
// DefaultIVBits is the default length of IV, in bits.
|
||||||
|
// We always use 128-bit IVs for file content, but the
|
||||||
|
// master key in the config file is encrypted with a 96-bit IV for
|
||||||
|
// gocryptfs v1.2 and earlier. v1.3 switched to 128 bit.
|
||||||
|
DefaultIVBits = 128
|
||||||
|
|
||||||
|
_ = iota // skip zero
|
||||||
|
// RandomNonce chooses a random nonce.
|
||||||
|
RandomNonce NonceMode = iota
|
||||||
|
// ReverseDeterministicNonce chooses a deterministic nonce, suitable for
|
||||||
|
// use in reverse mode.
|
||||||
|
ReverseDeterministicNonce NonceMode = iota
|
||||||
|
// ExternalNonce derives a nonce from external sources.
|
||||||
|
ExternalNonce NonceMode = iota
|
||||||
|
)
|
||||||
|
|
||||||
|
// ContentEnc is used to encipher and decipher file content.
|
||||||
|
type ContentEnc struct {
|
||||||
|
// Cryptographic primitives
|
||||||
|
cryptoCore *cryptocore.CryptoCore
|
||||||
|
// Plaintext block size
|
||||||
|
plainBS uint64
|
||||||
|
// Ciphertext block size
|
||||||
|
cipherBS uint64
|
||||||
|
// All-zero block of size cipherBS, for fast compares
|
||||||
|
allZeroBlock []byte
|
||||||
|
// All-zero block of size IVBitLen/8, for fast compares
|
||||||
|
allZeroNonce []byte
|
||||||
|
// Force decode even if integrity check fails (openSSL only)
|
||||||
|
forceDecode bool
|
||||||
|
|
||||||
|
// Ciphertext block "sync.Pool" pool. Always returns cipherBS-sized byte
|
||||||
|
// slices (usually 4128 bytes).
|
||||||
|
cBlockPool bPool
|
||||||
|
// Plaintext block pool. Always returns plainBS-sized byte slices
|
||||||
|
// (usually 4096 bytes).
|
||||||
|
pBlockPool bPool
|
||||||
|
// Ciphertext request data pool. Always returns byte slices of size
|
||||||
|
// fuse.MAX_KERNEL_WRITE + encryption overhead.
|
||||||
|
// Used by Read() to temporarily store the ciphertext as it is read from
|
||||||
|
// disk.
|
||||||
|
CReqPool bPool
|
||||||
|
// Plaintext request data pool. Slice have size fuse.MAX_KERNEL_WRITE.
|
||||||
|
PReqPool bPool
|
||||||
|
}
|
||||||
|
|
||||||
|
// New returns an initialized ContentEnc instance.
|
||||||
|
func New(cc *cryptocore.CryptoCore, plainBS uint64, forceDecode bool) *ContentEnc {
|
||||||
|
if MAX_KERNEL_WRITE%plainBS == 0 {
|
||||||
|
cipherBS := plainBS + uint64(cc.IVLen) + cryptocore.AuthTagLen
|
||||||
|
// Take IV and GHASH overhead into account.
|
||||||
|
cReqSize := int(MAX_KERNEL_WRITE / plainBS * cipherBS)
|
||||||
|
// Unaligned reads (happens during fsck, could also happen with O_DIRECT?)
|
||||||
|
// touch one additional ciphertext and plaintext block. Reserve space for the
|
||||||
|
// extra block.
|
||||||
|
cReqSize += int(cipherBS)
|
||||||
|
pReqSize := MAX_KERNEL_WRITE + int(plainBS)
|
||||||
|
c := &ContentEnc{
|
||||||
|
cryptoCore: cc,
|
||||||
|
plainBS: plainBS,
|
||||||
|
cipherBS: cipherBS,
|
||||||
|
allZeroBlock: make([]byte, cipherBS),
|
||||||
|
allZeroNonce: make([]byte, cc.IVLen),
|
||||||
|
forceDecode: forceDecode,
|
||||||
|
cBlockPool: newBPool(int(cipherBS)),
|
||||||
|
CReqPool: newBPool(cReqSize),
|
||||||
|
pBlockPool: newBPool(int(plainBS)),
|
||||||
|
PReqPool: newBPool(pReqSize),
|
||||||
|
}
|
||||||
|
return c
|
||||||
|
} else {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// PlainBS returns the plaintext block size
|
||||||
|
func (be *ContentEnc) PlainBS() uint64 {
|
||||||
|
return be.plainBS
|
||||||
|
}
|
||||||
|
|
||||||
|
// CipherBS returns the ciphertext block size
|
||||||
|
func (be *ContentEnc) CipherBS() uint64 {
|
||||||
|
return be.cipherBS
|
||||||
|
}
|
||||||
|
|
||||||
|
// DecryptBlocks decrypts a number of blocks
|
||||||
|
func (be *ContentEnc) DecryptBlocks(ciphertext []byte, firstBlockNo uint64, fileID []byte) ([]byte, error) {
|
||||||
|
cBuf := bytes.NewBuffer(ciphertext)
|
||||||
|
var err error
|
||||||
|
pBuf := bytes.NewBuffer(be.PReqPool.Get()[:0])
|
||||||
|
blockNo := firstBlockNo
|
||||||
|
for cBuf.Len() > 0 {
|
||||||
|
cBlock := cBuf.Next(int(be.cipherBS))
|
||||||
|
var pBlock []byte
|
||||||
|
pBlock, err = be.DecryptBlock(cBlock, blockNo, fileID)
|
||||||
|
if err != nil {
|
||||||
|
if !be.forceDecode || err != stupidgcm.ErrAuth {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
pBuf.Write(pBlock)
|
||||||
|
be.pBlockPool.Put(pBlock)
|
||||||
|
blockNo++
|
||||||
|
}
|
||||||
|
return pBuf.Bytes(), err
|
||||||
|
}
|
||||||
|
|
||||||
|
// concatAD concatenates the block number and the file ID to a byte blob
|
||||||
|
// that can be passed to AES-GCM as associated data (AD).
|
||||||
|
// Result is: aData = [blockNo.bigEndian fileID].
|
||||||
|
func concatAD(blockNo uint64, fileID []byte) (aData []byte) {
|
||||||
|
if fileID != nil && len(fileID) != headerIDLen {
|
||||||
|
// fileID is nil when decrypting the master key from the config file,
|
||||||
|
// and for symlinks and xattrs.
|
||||||
|
log.Panicf("wrong fileID length: %d", len(fileID))
|
||||||
|
}
|
||||||
|
const lenUint64 = 8
|
||||||
|
// Preallocate space to save an allocation in append()
|
||||||
|
aData = make([]byte, lenUint64, lenUint64+headerIDLen)
|
||||||
|
binary.BigEndian.PutUint64(aData, blockNo)
|
||||||
|
aData = append(aData, fileID...)
|
||||||
|
return aData
|
||||||
|
}
|
||||||
|
|
||||||
|
// DecryptBlock - Verify and decrypt GCM block
|
||||||
|
//
|
||||||
|
// Corner case: A full-sized block of all-zero ciphertext bytes is translated
|
||||||
|
// to an all-zero plaintext block, i.e. file hole passthrough.
|
||||||
|
func (be *ContentEnc) DecryptBlock(ciphertext []byte, blockNo uint64, fileID []byte) ([]byte, error) {
|
||||||
|
// Empty block?
|
||||||
|
if len(ciphertext) == 0 {
|
||||||
|
return ciphertext, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// All-zero block?
|
||||||
|
if bytes.Equal(ciphertext, be.allZeroBlock) {
|
||||||
|
return make([]byte, be.plainBS), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(ciphertext) < be.cryptoCore.IVLen {
|
||||||
|
return nil, errors.New("Block is too short")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Extract nonce
|
||||||
|
nonce := ciphertext[:be.cryptoCore.IVLen]
|
||||||
|
if bytes.Equal(nonce, be.allZeroNonce) {
|
||||||
|
// Bug in tmpfs?
|
||||||
|
// https://github.com/rfjakob/gocryptfs/issues/56
|
||||||
|
// http://www.spinics.net/lists/kernel/msg2370127.html
|
||||||
|
return nil, errors.New("all-zero nonce")
|
||||||
|
}
|
||||||
|
ciphertext = ciphertext[be.cryptoCore.IVLen:]
|
||||||
|
|
||||||
|
// Decrypt
|
||||||
|
plaintext := be.pBlockPool.Get()
|
||||||
|
plaintext = plaintext[:0]
|
||||||
|
aData := concatAD(blockNo, fileID)
|
||||||
|
plaintext, err := be.cryptoCore.AEADCipher.Open(plaintext, nonce, ciphertext, aData)
|
||||||
|
if err != nil {
|
||||||
|
if be.forceDecode && err == stupidgcm.ErrAuth {
|
||||||
|
return plaintext, err
|
||||||
|
}
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return plaintext, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// At some point, splitting the ciphertext into more groups will not improve
|
||||||
|
// performance, as spawning goroutines comes at a cost.
|
||||||
|
// 2 seems to work ok for now.
|
||||||
|
const encryptMaxSplit = 2
|
||||||
|
|
||||||
|
// encryptBlocksParallel splits the plaintext into parts and encrypts them
|
||||||
|
// in parallel.
|
||||||
|
func (be *ContentEnc) encryptBlocksParallel(plaintextBlocks [][]byte, ciphertextBlocks [][]byte, firstBlockNo uint64, fileID []byte) {
|
||||||
|
ncpu := runtime.NumCPU()
|
||||||
|
if ncpu > encryptMaxSplit {
|
||||||
|
ncpu = encryptMaxSplit
|
||||||
|
}
|
||||||
|
groupSize := len(plaintextBlocks) / ncpu
|
||||||
|
var wg sync.WaitGroup
|
||||||
|
for i := 0; i < ncpu; i++ {
|
||||||
|
wg.Add(1)
|
||||||
|
go func(i int) {
|
||||||
|
low := i * groupSize
|
||||||
|
high := (i + 1) * groupSize
|
||||||
|
if i == ncpu-1 {
|
||||||
|
// Last part picks up any left-over blocks
|
||||||
|
//
|
||||||
|
// The last part could run in the original goroutine, but
|
||||||
|
// doing that complicates the code, and, surprisingly,
|
||||||
|
// incurs a 1 % performance penalty.
|
||||||
|
high = len(plaintextBlocks)
|
||||||
|
}
|
||||||
|
be.doEncryptBlocks(plaintextBlocks[low:high], ciphertextBlocks[low:high], firstBlockNo+uint64(low), fileID)
|
||||||
|
wg.Done()
|
||||||
|
}(i)
|
||||||
|
}
|
||||||
|
wg.Wait()
|
||||||
|
}
|
||||||
|
|
||||||
|
// EncryptBlocks is like EncryptBlock but takes multiple plaintext blocks.
|
||||||
|
// Returns a byte slice from CReqPool - so don't forget to return it
|
||||||
|
// to the pool.
|
||||||
|
func (be *ContentEnc) EncryptBlocks(plaintextBlocks [][]byte, firstBlockNo uint64, fileID []byte) []byte {
|
||||||
|
ciphertextBlocks := make([][]byte, len(plaintextBlocks))
|
||||||
|
// For large writes, we parallelize encryption.
|
||||||
|
if len(plaintextBlocks) >= 32 && runtime.NumCPU() >= 2 {
|
||||||
|
be.encryptBlocksParallel(plaintextBlocks, ciphertextBlocks, firstBlockNo, fileID)
|
||||||
|
} else {
|
||||||
|
be.doEncryptBlocks(plaintextBlocks, ciphertextBlocks, firstBlockNo, fileID)
|
||||||
|
}
|
||||||
|
// Concatenate ciphertext into a single byte array.
|
||||||
|
tmp := be.CReqPool.Get()
|
||||||
|
out := bytes.NewBuffer(tmp[:0])
|
||||||
|
for _, v := range ciphertextBlocks {
|
||||||
|
out.Write(v)
|
||||||
|
// Return the memory to cBlockPool
|
||||||
|
be.cBlockPool.Put(v)
|
||||||
|
}
|
||||||
|
return out.Bytes()
|
||||||
|
}
|
||||||
|
|
||||||
|
// doEncryptBlocks is called by EncryptBlocks to do the actual encryption work
|
||||||
|
func (be *ContentEnc) doEncryptBlocks(in [][]byte, out [][]byte, firstBlockNo uint64, fileID []byte) {
|
||||||
|
for i, v := range in {
|
||||||
|
out[i] = be.EncryptBlock(v, firstBlockNo+uint64(i), fileID)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// EncryptBlock - Encrypt plaintext using a random nonce.
|
||||||
|
// blockNo and fileID are used as associated data.
|
||||||
|
// The output is nonce + ciphertext + tag.
|
||||||
|
func (be *ContentEnc) EncryptBlock(plaintext []byte, blockNo uint64, fileID []byte) []byte {
|
||||||
|
// Get a fresh random nonce
|
||||||
|
nonce := be.cryptoCore.IVGenerator.Get()
|
||||||
|
return be.doEncryptBlock(plaintext, blockNo, fileID, nonce)
|
||||||
|
}
|
||||||
|
|
||||||
|
// EncryptBlockNonce - Encrypt plaintext using a nonce chosen by the caller.
|
||||||
|
// blockNo and fileID are used as associated data.
|
||||||
|
// The output is nonce + ciphertext + tag.
|
||||||
|
// This function can only be used in SIV mode.
|
||||||
|
func (be *ContentEnc) EncryptBlockNonce(plaintext []byte, blockNo uint64, fileID []byte, nonce []byte) []byte {
|
||||||
|
if be.cryptoCore.AEADBackend != cryptocore.BackendAESSIV {
|
||||||
|
log.Panic("deterministic nonces are only secure in SIV mode")
|
||||||
|
}
|
||||||
|
return be.doEncryptBlock(plaintext, blockNo, fileID, nonce)
|
||||||
|
}
|
||||||
|
|
||||||
|
// doEncryptBlock is the backend for EncryptBlock and EncryptBlockNonce.
|
||||||
|
// blockNo and fileID are used as associated data.
|
||||||
|
// The output is nonce + ciphertext + tag.
|
||||||
|
func (be *ContentEnc) doEncryptBlock(plaintext []byte, blockNo uint64, fileID []byte, nonce []byte) []byte {
|
||||||
|
// Empty block?
|
||||||
|
if len(plaintext) == 0 {
|
||||||
|
return plaintext
|
||||||
|
}
|
||||||
|
if len(nonce) != be.cryptoCore.IVLen {
|
||||||
|
log.Panic("wrong nonce length")
|
||||||
|
}
|
||||||
|
// Block is authenticated with block number and file ID
|
||||||
|
aData := concatAD(blockNo, fileID)
|
||||||
|
// Get a cipherBS-sized block of memory, copy the nonce into it and truncate to
|
||||||
|
// nonce length
|
||||||
|
cBlock := be.cBlockPool.Get()
|
||||||
|
copy(cBlock, nonce)
|
||||||
|
cBlock = cBlock[0:len(nonce)]
|
||||||
|
// Encrypt plaintext and append to nonce
|
||||||
|
ciphertext := be.cryptoCore.AEADCipher.Seal(cBlock, nonce, plaintext, aData)
|
||||||
|
overhead := int(be.cipherBS - be.plainBS)
|
||||||
|
if len(plaintext)+overhead != len(ciphertext) {
|
||||||
|
log.Panicf("unexpected ciphertext length: plaintext=%d, overhead=%d, ciphertext=%d",
|
||||||
|
len(plaintext), overhead, len(ciphertext))
|
||||||
|
}
|
||||||
|
return ciphertext
|
||||||
|
}
|
||||||
|
|
||||||
|
// MergeBlocks - Merge newData into oldData at offset
|
||||||
|
// New block may be bigger than both newData and oldData
|
||||||
|
func (be *ContentEnc) MergeBlocks(oldData []byte, newData []byte, offset int) []byte {
|
||||||
|
// Fastpath for small-file creation
|
||||||
|
if len(oldData) == 0 && offset == 0 {
|
||||||
|
return newData
|
||||||
|
}
|
||||||
|
|
||||||
|
// Make block of maximum size
|
||||||
|
out := make([]byte, be.plainBS)
|
||||||
|
|
||||||
|
// Copy old and new data into it
|
||||||
|
copy(out, oldData)
|
||||||
|
l := len(newData)
|
||||||
|
copy(out[offset:offset+l], newData)
|
||||||
|
|
||||||
|
// Crop to length
|
||||||
|
outLen := len(oldData)
|
||||||
|
newLen := offset + len(newData)
|
||||||
|
if outLen < newLen {
|
||||||
|
outLen = newLen
|
||||||
|
}
|
||||||
|
return out[0:outLen]
|
||||||
|
}
|
||||||
|
|
||||||
|
// Wipe tries to wipe secret keys from memory by overwriting them with zeros
|
||||||
|
// and/or setting references to nil.
|
||||||
|
func (be *ContentEnc) Wipe() {
|
||||||
|
be.cryptoCore.Wipe()
|
||||||
|
be.cryptoCore = nil
|
||||||
|
}
|
77
app/libgocryptfs/rewrites/contentenc/file_header.go
Normal file
77
app/libgocryptfs/rewrites/contentenc/file_header.go
Normal file
@ -0,0 +1,77 @@
|
|||||||
|
package contentenc
|
||||||
|
|
||||||
|
// Per-file header
|
||||||
|
//
|
||||||
|
// Format: [ "Version" uint16 big endian ] [ "Id" 16 random bytes ]
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"encoding/binary"
|
||||||
|
"encoding/hex"
|
||||||
|
"fmt"
|
||||||
|
"log"
|
||||||
|
|
||||||
|
"../../gocryptfs_internal/cryptocore"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
// CurrentVersion is the current On-Disk-Format version
|
||||||
|
CurrentVersion = 2
|
||||||
|
|
||||||
|
headerVersionLen = 2 // uint16
|
||||||
|
headerIDLen = 16 // 128 bit random file id
|
||||||
|
// HeaderLen is the total header length
|
||||||
|
HeaderLen = headerVersionLen + headerIDLen
|
||||||
|
)
|
||||||
|
|
||||||
|
// FileHeader represents the header stored on each non-empty file.
|
||||||
|
type FileHeader struct {
|
||||||
|
Version uint16
|
||||||
|
ID []byte
|
||||||
|
}
|
||||||
|
|
||||||
|
// Pack - serialize fileHeader object
|
||||||
|
func (h *FileHeader) Pack() []byte {
|
||||||
|
if len(h.ID) != headerIDLen || h.Version != CurrentVersion {
|
||||||
|
log.Panic("FileHeader object not properly initialized")
|
||||||
|
}
|
||||||
|
buf := make([]byte, HeaderLen)
|
||||||
|
binary.BigEndian.PutUint16(buf[0:headerVersionLen], h.Version)
|
||||||
|
copy(buf[headerVersionLen:], h.ID)
|
||||||
|
return buf
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
// allZeroFileID is preallocated to quickly check if the data read from disk is all zero
|
||||||
|
var allZeroFileID = make([]byte, headerIDLen)
|
||||||
|
var allZeroHeader = make([]byte, HeaderLen)
|
||||||
|
|
||||||
|
// ParseHeader - parse "buf" into fileHeader object
|
||||||
|
func ParseHeader(buf []byte) (*FileHeader, error) {
|
||||||
|
if len(buf) != HeaderLen {
|
||||||
|
return nil, fmt.Errorf("ParseHeader: invalid length, want=%d have=%d", HeaderLen, len(buf))
|
||||||
|
}
|
||||||
|
if bytes.Equal(buf, allZeroHeader) {
|
||||||
|
return nil, fmt.Errorf("ParseHeader: header is all-zero. Header hexdump: %s", hex.EncodeToString(buf))
|
||||||
|
}
|
||||||
|
var h FileHeader
|
||||||
|
h.Version = binary.BigEndian.Uint16(buf[0:headerVersionLen])
|
||||||
|
if h.Version != CurrentVersion {
|
||||||
|
return nil, fmt.Errorf("ParseHeader: invalid version, want=%d have=%d. Header hexdump: %s",
|
||||||
|
CurrentVersion, h.Version, hex.EncodeToString(buf))
|
||||||
|
}
|
||||||
|
h.ID = buf[headerVersionLen:]
|
||||||
|
if bytes.Equal(h.ID, allZeroFileID) {
|
||||||
|
return nil, fmt.Errorf("ParseHeader: file id is all-zero. Header hexdump: %s",
|
||||||
|
hex.EncodeToString(buf))
|
||||||
|
}
|
||||||
|
return &h, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// RandomHeader - create new fileHeader object with random Id
|
||||||
|
func RandomHeader() *FileHeader {
|
||||||
|
var h FileHeader
|
||||||
|
h.Version = CurrentVersion
|
||||||
|
h.ID = cryptocore.RandBytes(headerIDLen)
|
||||||
|
return &h
|
||||||
|
}
|
71
app/libgocryptfs/rewrites/contentenc/intrablock.go
Normal file
71
app/libgocryptfs/rewrites/contentenc/intrablock.go
Normal file
@ -0,0 +1,71 @@
|
|||||||
|
package contentenc
|
||||||
|
|
||||||
|
// IntraBlock identifies a part of a file block
|
||||||
|
type IntraBlock struct {
|
||||||
|
// BlockNo is the block number in the file
|
||||||
|
BlockNo uint64
|
||||||
|
// Skip is an offset into the block payload
|
||||||
|
// In forward mode: block plaintext
|
||||||
|
// In reverse mode: offset into block ciphertext. Takes the header into
|
||||||
|
// account.
|
||||||
|
Skip uint64
|
||||||
|
// Length of payload data in this block
|
||||||
|
// In forward mode: length of the plaintext
|
||||||
|
// In reverse mode: length of the ciphertext. Takes header and trailer into
|
||||||
|
// account.
|
||||||
|
Length uint64
|
||||||
|
fs *ContentEnc
|
||||||
|
}
|
||||||
|
|
||||||
|
// IsPartial - is the block partial? This means we have to do read-modify-write.
|
||||||
|
func (ib *IntraBlock) IsPartial() bool {
|
||||||
|
if ib.Skip > 0 || ib.Length < ib.fs.plainBS {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
// BlockCipherOff returns the ciphertext offset corresponding to BlockNo
|
||||||
|
func (ib *IntraBlock) BlockCipherOff() (offset uint64) {
|
||||||
|
return ib.fs.BlockNoToCipherOff(ib.BlockNo)
|
||||||
|
}
|
||||||
|
|
||||||
|
// BlockPlainOff returns the plaintext offset corresponding to BlockNo
|
||||||
|
func (ib *IntraBlock) BlockPlainOff() (offset uint64) {
|
||||||
|
return ib.fs.BlockNoToPlainOff(ib.BlockNo)
|
||||||
|
}
|
||||||
|
|
||||||
|
// CropBlock - crop a potentially larger plaintext block down to the relevant part
|
||||||
|
func (ib *IntraBlock) CropBlock(d []byte) []byte {
|
||||||
|
lenHave := len(d)
|
||||||
|
lenWant := int(ib.Skip + ib.Length)
|
||||||
|
if lenHave < lenWant {
|
||||||
|
return d[ib.Skip:lenHave]
|
||||||
|
}
|
||||||
|
return d[ib.Skip:lenWant]
|
||||||
|
}
|
||||||
|
|
||||||
|
// JointCiphertextRange is the ciphertext range corresponding to the sum of all
|
||||||
|
// "blocks" (complete blocks)
|
||||||
|
func (ib *IntraBlock) JointCiphertextRange(blocks []IntraBlock) (offset uint64, length uint64) {
|
||||||
|
firstBlock := blocks[0]
|
||||||
|
lastBlock := blocks[len(blocks)-1]
|
||||||
|
|
||||||
|
offset = ib.fs.BlockNoToCipherOff(firstBlock.BlockNo)
|
||||||
|
offsetLast := ib.fs.BlockNoToCipherOff(lastBlock.BlockNo)
|
||||||
|
length = offsetLast + ib.fs.cipherBS - offset
|
||||||
|
|
||||||
|
return offset, length
|
||||||
|
}
|
||||||
|
|
||||||
|
// JointPlaintextRange is the plaintext range corresponding to the sum of all
|
||||||
|
// "blocks" (complete blocks)
|
||||||
|
func JointPlaintextRange(blocks []IntraBlock) (offset uint64, length uint64) {
|
||||||
|
firstBlock := blocks[0]
|
||||||
|
lastBlock := blocks[len(blocks)-1]
|
||||||
|
|
||||||
|
offset = firstBlock.BlockPlainOff()
|
||||||
|
length = lastBlock.BlockPlainOff() + lastBlock.fs.PlainBS() - offset
|
||||||
|
|
||||||
|
return offset, length
|
||||||
|
}
|
135
app/libgocryptfs/rewrites/contentenc/offsets.go
Normal file
135
app/libgocryptfs/rewrites/contentenc/offsets.go
Normal file
@ -0,0 +1,135 @@
|
|||||||
|
package contentenc
|
||||||
|
|
||||||
|
import (
|
||||||
|
"log"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Contentenc methods that translate offsets between ciphertext and plaintext
|
||||||
|
|
||||||
|
// PlainOffToBlockNo converts a plaintext offset to the ciphertext block number.
|
||||||
|
func (be *ContentEnc) PlainOffToBlockNo(plainOffset uint64) uint64 {
|
||||||
|
return plainOffset / be.plainBS
|
||||||
|
}
|
||||||
|
|
||||||
|
// CipherOffToBlockNo converts the ciphertext offset to the plaintext block number.
|
||||||
|
func (be *ContentEnc) CipherOffToBlockNo(cipherOffset uint64) uint64 {
|
||||||
|
if cipherOffset < HeaderLen {
|
||||||
|
log.Panicf("BUG: offset %d is inside the file header", cipherOffset)
|
||||||
|
}
|
||||||
|
return (cipherOffset - HeaderLen) / be.cipherBS
|
||||||
|
}
|
||||||
|
|
||||||
|
// BlockNoToCipherOff gets the ciphertext offset of block "blockNo"
|
||||||
|
func (be *ContentEnc) BlockNoToCipherOff(blockNo uint64) uint64 {
|
||||||
|
return HeaderLen + blockNo*be.cipherBS
|
||||||
|
}
|
||||||
|
|
||||||
|
// BlockNoToPlainOff gets the plaintext offset of block "blockNo"
|
||||||
|
func (be *ContentEnc) BlockNoToPlainOff(blockNo uint64) uint64 {
|
||||||
|
return blockNo * be.plainBS
|
||||||
|
}
|
||||||
|
|
||||||
|
// CipherSizeToPlainSize calculates the plaintext size from a ciphertext size
|
||||||
|
func (be *ContentEnc) CipherSizeToPlainSize(cipherSize uint64) uint64 {
|
||||||
|
// Zero-sized files stay zero-sized
|
||||||
|
if cipherSize == 0 {
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
|
||||||
|
if cipherSize == HeaderLen {
|
||||||
|
// This can happen between createHeader() and Write() and is harmless.
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
|
||||||
|
if cipherSize < HeaderLen {
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
|
||||||
|
// Block number at last byte
|
||||||
|
blockNo := be.CipherOffToBlockNo(cipherSize - 1)
|
||||||
|
blockCount := blockNo + 1
|
||||||
|
|
||||||
|
overhead := be.BlockOverhead()*blockCount + HeaderLen
|
||||||
|
|
||||||
|
if overhead > cipherSize {
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
|
||||||
|
return cipherSize - overhead
|
||||||
|
}
|
||||||
|
|
||||||
|
// PlainSizeToCipherSize calculates the ciphertext size from a plaintext size
|
||||||
|
func (be *ContentEnc) PlainSizeToCipherSize(plainSize uint64) uint64 {
|
||||||
|
// Zero-sized files stay zero-sized
|
||||||
|
if plainSize == 0 {
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
|
||||||
|
// Block number at last byte
|
||||||
|
blockNo := be.PlainOffToBlockNo(plainSize - 1)
|
||||||
|
blockCount := blockNo + 1
|
||||||
|
|
||||||
|
overhead := be.BlockOverhead()*blockCount + HeaderLen
|
||||||
|
|
||||||
|
return plainSize + overhead
|
||||||
|
}
|
||||||
|
|
||||||
|
// ExplodePlainRange splits a plaintext byte range into (possibly partial) blocks
|
||||||
|
// Returns an empty slice if length == 0.
|
||||||
|
func (be *ContentEnc) ExplodePlainRange(offset uint64, length uint64) []IntraBlock {
|
||||||
|
var blocks []IntraBlock
|
||||||
|
var nextBlock IntraBlock
|
||||||
|
nextBlock.fs = be
|
||||||
|
|
||||||
|
for length > 0 {
|
||||||
|
nextBlock.BlockNo = be.PlainOffToBlockNo(offset)
|
||||||
|
nextBlock.Skip = offset - be.BlockNoToPlainOff(nextBlock.BlockNo)
|
||||||
|
|
||||||
|
// Minimum of remaining plaintext data and remaining space in the block
|
||||||
|
nextBlock.Length = MinUint64(length, be.plainBS-nextBlock.Skip)
|
||||||
|
|
||||||
|
blocks = append(blocks, nextBlock)
|
||||||
|
offset += nextBlock.Length
|
||||||
|
length -= nextBlock.Length
|
||||||
|
}
|
||||||
|
return blocks
|
||||||
|
}
|
||||||
|
|
||||||
|
// ExplodeCipherRange splits a ciphertext byte range into (possibly partial)
|
||||||
|
// blocks This is used in reverse mode when reading files
|
||||||
|
func (be *ContentEnc) ExplodeCipherRange(offset uint64, length uint64) []IntraBlock {
|
||||||
|
var blocks []IntraBlock
|
||||||
|
var nextBlock IntraBlock
|
||||||
|
nextBlock.fs = be
|
||||||
|
|
||||||
|
for length > 0 {
|
||||||
|
nextBlock.BlockNo = be.CipherOffToBlockNo(offset)
|
||||||
|
nextBlock.Skip = offset - be.BlockNoToCipherOff(nextBlock.BlockNo)
|
||||||
|
|
||||||
|
// This block can carry up to "maxLen" payload bytes
|
||||||
|
maxLen := be.cipherBS - nextBlock.Skip
|
||||||
|
nextBlock.Length = maxLen
|
||||||
|
// But if the user requested less, we truncate the block to "length".
|
||||||
|
if length < maxLen {
|
||||||
|
nextBlock.Length = length
|
||||||
|
}
|
||||||
|
|
||||||
|
blocks = append(blocks, nextBlock)
|
||||||
|
offset += nextBlock.Length
|
||||||
|
length -= nextBlock.Length
|
||||||
|
}
|
||||||
|
return blocks
|
||||||
|
}
|
||||||
|
|
||||||
|
// BlockOverhead returns the per-block overhead.
|
||||||
|
func (be *ContentEnc) BlockOverhead() uint64 {
|
||||||
|
return be.cipherBS - be.plainBS
|
||||||
|
}
|
||||||
|
|
||||||
|
// MinUint64 returns the minimum of two uint64 values.
|
||||||
|
func MinUint64(x uint64, y uint64) uint64 {
|
||||||
|
if x < y {
|
||||||
|
return x
|
||||||
|
}
|
||||||
|
return y
|
||||||
|
}
|
29
app/libgocryptfs/rewrites/syscallcompat/emulate.go
Normal file
29
app/libgocryptfs/rewrites/syscallcompat/emulate.go
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
package syscallcompat
|
||||||
|
|
||||||
|
import (
|
||||||
|
"path/filepath"
|
||||||
|
"sync"
|
||||||
|
"syscall"
|
||||||
|
)
|
||||||
|
|
||||||
|
var chdirMutex sync.Mutex
|
||||||
|
|
||||||
|
// emulateMknodat emulates the syscall for platforms that don't have it
|
||||||
|
// in the kernel (darwin).
|
||||||
|
func emulateMknodat(dirfd int, path string, mode uint32, dev int) error {
|
||||||
|
if !filepath.IsAbs(path) {
|
||||||
|
chdirMutex.Lock()
|
||||||
|
defer chdirMutex.Unlock()
|
||||||
|
cwd, err := syscall.Open(".", syscall.O_RDONLY, 0)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer syscall.Close(cwd)
|
||||||
|
err = syscall.Fchdir(dirfd)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer syscall.Fchdir(cwd)
|
||||||
|
}
|
||||||
|
return syscall.Mknod(path, mode, dev)
|
||||||
|
}
|
151
app/libgocryptfs/rewrites/syscallcompat/getdents_linux.go
Normal file
151
app/libgocryptfs/rewrites/syscallcompat/getdents_linux.go
Normal file
@ -0,0 +1,151 @@
|
|||||||
|
// +build linux
|
||||||
|
|
||||||
|
package syscallcompat
|
||||||
|
|
||||||
|
// Other implementations of getdents in Go:
|
||||||
|
// https://github.com/ericlagergren/go-gnulib/blob/cb7a6e136427e242099b2c29d661016c19458801/dirent/getdents_unix.go
|
||||||
|
// https://github.com/golang/tools/blob/5831d16d18029819d39f99bdc2060b8eff410b6b/imports/fastwalk_unix.go
|
||||||
|
|
||||||
|
import (
|
||||||
|
"sync"
|
||||||
|
"bytes"
|
||||||
|
"syscall"
|
||||||
|
"unsafe"
|
||||||
|
|
||||||
|
"golang.org/x/sys/unix"
|
||||||
|
)
|
||||||
|
|
||||||
|
const sizeofDirent = int(unsafe.Sizeof(unix.Dirent{}))
|
||||||
|
|
||||||
|
// maxReclen sanity check: Reclen should never be larger than this.
|
||||||
|
// Due to padding between entries, it is 280 even on 32-bit architectures.
|
||||||
|
// See https://github.com/rfjakob/gocryptfs/issues/197 for details.
|
||||||
|
const maxReclen = 280
|
||||||
|
|
||||||
|
type DirEntry struct {
|
||||||
|
Name string
|
||||||
|
Mode uint32
|
||||||
|
}
|
||||||
|
|
||||||
|
// getdents wraps unix.Getdents and converts the result to []fuse.DirEntry.
|
||||||
|
func getdents(fd int) ([]DirEntry, error) {
|
||||||
|
// Collect syscall result in smartBuf.
|
||||||
|
// "bytes.Buffer" is smart about expanding the capacity and avoids the
|
||||||
|
// exponential runtime of simple append().
|
||||||
|
var smartBuf bytes.Buffer
|
||||||
|
tmp := make([]byte, 10000)
|
||||||
|
for {
|
||||||
|
n, err := unix.Getdents(fd, tmp)
|
||||||
|
// unix.Getdents has been observed to return EINTR on cifs mounts
|
||||||
|
if err == unix.EINTR {
|
||||||
|
if n > 0 {
|
||||||
|
smartBuf.Write(tmp[:n])
|
||||||
|
}
|
||||||
|
continue
|
||||||
|
} else if err != nil {
|
||||||
|
if smartBuf.Len() > 0 {
|
||||||
|
return nil, syscall.EIO
|
||||||
|
}
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if n == 0 {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
smartBuf.Write(tmp[:n])
|
||||||
|
}
|
||||||
|
// Make sure we have at least Sizeof(Dirent) of zeros after the last
|
||||||
|
// entry. This prevents a cast to Dirent from reading past the buffer.
|
||||||
|
smartBuf.Grow(sizeofDirent)
|
||||||
|
buf := smartBuf.Bytes()
|
||||||
|
// Count the number of directory entries in the buffer so we can allocate
|
||||||
|
// a fuse.DirEntry slice of the correct size at once.
|
||||||
|
var numEntries, offset int
|
||||||
|
for offset < len(buf) {
|
||||||
|
s := *(*unix.Dirent)(unsafe.Pointer(&buf[offset]))
|
||||||
|
if s.Reclen == 0 {
|
||||||
|
// EBADR = Invalid request descriptor
|
||||||
|
return nil, syscall.EBADR
|
||||||
|
}
|
||||||
|
if int(s.Reclen) > maxReclen {
|
||||||
|
return nil, syscall.EBADR
|
||||||
|
}
|
||||||
|
offset += int(s.Reclen)
|
||||||
|
numEntries++
|
||||||
|
}
|
||||||
|
// Parse the buffer into entries.
|
||||||
|
// Note: syscall.ParseDirent() only returns the names,
|
||||||
|
// we want all the data, so we have to implement
|
||||||
|
// it on our own.
|
||||||
|
entries := make([]DirEntry, 0, numEntries)
|
||||||
|
offset = 0
|
||||||
|
for offset < len(buf) {
|
||||||
|
s := *(*unix.Dirent)(unsafe.Pointer(&buf[offset]))
|
||||||
|
name, err := getdentsName(s)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
offset += int(s.Reclen)
|
||||||
|
if name == "." || name == ".." {
|
||||||
|
// os.File.Readdir() drops "." and "..". Let's be compatible.
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
mode, err := convertDType(fd, name, s.Type)
|
||||||
|
if err != nil {
|
||||||
|
// The uint32file may have been deleted in the meantime. Just skip it
|
||||||
|
// and go on.
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
entries = append(entries, DirEntry{
|
||||||
|
Name: name,
|
||||||
|
Mode: mode,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
return entries, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// getdentsName extracts the filename from a Dirent struct and returns it as
|
||||||
|
// a Go string.
|
||||||
|
func getdentsName(s unix.Dirent) (string, error) {
|
||||||
|
// After the loop, l contains the index of the first '\0'.
|
||||||
|
l := 0
|
||||||
|
for l = range s.Name {
|
||||||
|
if s.Name[l] == 0 {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if l < 1 {
|
||||||
|
// EBADR = Invalid request descriptor
|
||||||
|
return "", syscall.EBADR
|
||||||
|
}
|
||||||
|
// Copy to byte slice.
|
||||||
|
name := make([]byte, l)
|
||||||
|
for i := range name {
|
||||||
|
name[i] = byte(s.Name[i])
|
||||||
|
}
|
||||||
|
return string(name), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
var dtUnknownWarnOnce sync.Once
|
||||||
|
|
||||||
|
func dtUnknownWarn(dirfd int) {
|
||||||
|
const XFS_SUPER_MAGIC = 0x58465342 // From man 2 statfs
|
||||||
|
var buf syscall.Statfs_t
|
||||||
|
syscall.Fstatfs(dirfd, &buf)
|
||||||
|
}
|
||||||
|
|
||||||
|
// convertDType converts a Dirent.Type to at Stat_t.Mode value.
|
||||||
|
func convertDType(dirfd int, name string, dtype uint8) (uint32, error) {
|
||||||
|
if dtype != syscall.DT_UNKNOWN {
|
||||||
|
// Shift up by four octal digits = 12 bits
|
||||||
|
return uint32(dtype) << 12, nil
|
||||||
|
}
|
||||||
|
// DT_UNKNOWN: we have to call stat()
|
||||||
|
dtUnknownWarnOnce.Do(func() { dtUnknownWarn(dirfd) })
|
||||||
|
var st unix.Stat_t
|
||||||
|
err := Fstatat(dirfd, name, &st, unix.AT_SYMLINK_NOFOLLOW)
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
// The S_IFMT bit mask extracts the file type from the mode.
|
||||||
|
return st.Mode & syscall.S_IFMT, nil
|
||||||
|
}
|
@ -0,0 +1 @@
|
|||||||
|
package syscallcompat
|
21
app/libgocryptfs/rewrites/syscallcompat/helpers.go
Normal file
21
app/libgocryptfs/rewrites/syscallcompat/helpers.go
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
package syscallcompat
|
||||||
|
|
||||||
|
import (
|
||||||
|
"os"
|
||||||
|
"syscall"
|
||||||
|
)
|
||||||
|
|
||||||
|
// IsENOSPC tries to find out if "err" is a (potentially wrapped) ENOSPC error.
|
||||||
|
func IsENOSPC(err error) bool {
|
||||||
|
// syscallcompat.EnospcPrealloc returns the naked syscall error
|
||||||
|
if err == syscall.ENOSPC {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
// os.File.WriteAt returns &PathError
|
||||||
|
if err2, ok := err.(*os.PathError); ok {
|
||||||
|
if err2.Err == syscall.ENOSPC {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
44
app/libgocryptfs/rewrites/syscallcompat/open_nofollow.go
Normal file
44
app/libgocryptfs/rewrites/syscallcompat/open_nofollow.go
Normal file
@ -0,0 +1,44 @@
|
|||||||
|
package syscallcompat
|
||||||
|
|
||||||
|
import (
|
||||||
|
"path/filepath"
|
||||||
|
"strings"
|
||||||
|
"syscall"
|
||||||
|
)
|
||||||
|
|
||||||
|
// OpenDirNofollow opens the dir at "relPath" in a way that is secure against
|
||||||
|
// symlink attacks. Symlinks that are part of "relPath" are never followed.
|
||||||
|
// This function is implemented by walking the directory tree, starting at
|
||||||
|
// "baseDir", using the Openat syscall with the O_NOFOLLOW flag.
|
||||||
|
// Symlinks that are part of the "baseDir" path are followed.
|
||||||
|
func OpenDirNofollow(baseDir string, relPath string) (fd int, err error) {
|
||||||
|
if !filepath.IsAbs(baseDir) {
|
||||||
|
return -1, syscall.EINVAL
|
||||||
|
}
|
||||||
|
if filepath.IsAbs(relPath) {
|
||||||
|
return -1, syscall.EINVAL
|
||||||
|
}
|
||||||
|
// Open the base dir (following symlinks)
|
||||||
|
dirfd, err := syscall.Open(baseDir, syscall.O_DIRECTORY|O_PATH, 0)
|
||||||
|
if err != nil {
|
||||||
|
return -1, err
|
||||||
|
}
|
||||||
|
// Caller wanted to open baseDir itself?
|
||||||
|
if relPath == "" {
|
||||||
|
return dirfd, nil
|
||||||
|
}
|
||||||
|
// Split the path into components
|
||||||
|
parts := strings.Split(relPath, "/")
|
||||||
|
// Walk the directory tree
|
||||||
|
var dirfd2 int
|
||||||
|
for _, name := range parts {
|
||||||
|
dirfd2, err = Openat(dirfd, name, syscall.O_NOFOLLOW|syscall.O_DIRECTORY|O_PATH, 0)
|
||||||
|
syscall.Close(dirfd)
|
||||||
|
if err != nil {
|
||||||
|
return -1, err
|
||||||
|
}
|
||||||
|
dirfd = dirfd2
|
||||||
|
}
|
||||||
|
// Return fd to final directory
|
||||||
|
return dirfd, nil
|
||||||
|
}
|
215
app/libgocryptfs/rewrites/syscallcompat/sys_common.go
Normal file
215
app/libgocryptfs/rewrites/syscallcompat/sys_common.go
Normal file
@ -0,0 +1,215 @@
|
|||||||
|
package syscallcompat
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"syscall"
|
||||||
|
|
||||||
|
"golang.org/x/sys/unix"
|
||||||
|
)
|
||||||
|
|
||||||
|
// PATH_MAX is the maximum allowed path length on Linux.
|
||||||
|
// It is not defined on Darwin, so we use the Linux value.
|
||||||
|
const PATH_MAX = 4096
|
||||||
|
|
||||||
|
// Readlinkat is a convenience wrapper around unix.Readlinkat() that takes
|
||||||
|
// care of buffer sizing. Implemented like os.Readlink().
|
||||||
|
func Readlinkat(dirfd int, path string) (string, error) {
|
||||||
|
// Allocate the buffer exponentially like os.Readlink does.
|
||||||
|
for bufsz := 128; ; bufsz *= 2 {
|
||||||
|
buf := make([]byte, bufsz)
|
||||||
|
n, err := unix.Readlinkat(dirfd, path, buf)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
if n < bufsz {
|
||||||
|
return string(buf[0:n]), nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Faccessat exists both in Linux and in MacOS 10.10+, but the Linux version
|
||||||
|
// DOES NOT support any flags. Emulate AT_SYMLINK_NOFOLLOW like glibc does.
|
||||||
|
func Faccessat(dirfd int, path string, mode uint32) error {
|
||||||
|
var st unix.Stat_t
|
||||||
|
err := Fstatat(dirfd, path, &st, unix.AT_SYMLINK_NOFOLLOW)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if st.Mode&syscall.S_IFMT == syscall.S_IFLNK {
|
||||||
|
// Pretend that a symlink is always accessible
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
return unix.Faccessat(dirfd, path, mode, 0)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Openat wraps the Openat syscall.
|
||||||
|
func Openat(dirfd int, path string, flags int, mode uint32) (fd int, err error) {
|
||||||
|
if flags&syscall.O_CREAT != 0 {
|
||||||
|
// O_CREAT should be used with O_EXCL. O_NOFOLLOW has no effect with O_EXCL.
|
||||||
|
if flags&syscall.O_EXCL == 0 {
|
||||||
|
flags |= syscall.O_EXCL
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// If O_CREAT is not used, we should use O_NOFOLLOW
|
||||||
|
if flags&syscall.O_NOFOLLOW == 0 {
|
||||||
|
flags |= syscall.O_NOFOLLOW
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return unix.Openat(dirfd, path, flags, mode)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Renameat wraps the Renameat syscall.
|
||||||
|
func Renameat(olddirfd int, oldpath string, newdirfd int, newpath string) (err error) {
|
||||||
|
return unix.Renameat(olddirfd, oldpath, newdirfd, newpath)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Unlinkat syscall.
|
||||||
|
func Unlinkat(dirfd int, path string, flags int) (err error) {
|
||||||
|
return unix.Unlinkat(dirfd, path, flags)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Fchownat syscall.
|
||||||
|
func Fchownat(dirfd int, path string, uid int, gid int, flags int) (err error) {
|
||||||
|
// Why would we ever want to call this without AT_SYMLINK_NOFOLLOW?
|
||||||
|
if flags&unix.AT_SYMLINK_NOFOLLOW == 0 {
|
||||||
|
flags |= unix.AT_SYMLINK_NOFOLLOW
|
||||||
|
}
|
||||||
|
return unix.Fchownat(dirfd, path, uid, gid, flags)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Linkat exists both in Linux and in MacOS 10.10+.
|
||||||
|
func Linkat(olddirfd int, oldpath string, newdirfd int, newpath string, flags int) (err error) {
|
||||||
|
return unix.Linkat(olddirfd, oldpath, newdirfd, newpath, flags)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Symlinkat syscall.
|
||||||
|
func Symlinkat(oldpath string, newdirfd int, newpath string) (err error) {
|
||||||
|
return unix.Symlinkat(oldpath, newdirfd, newpath)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Mkdirat syscall.
|
||||||
|
func Mkdirat(dirfd int, path string, mode uint32) (err error) {
|
||||||
|
return unix.Mkdirat(dirfd, path, mode)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Fstatat syscall.
|
||||||
|
func Fstatat(dirfd int, path string, stat *unix.Stat_t, flags int) (err error) {
|
||||||
|
// Why would we ever want to call this without AT_SYMLINK_NOFOLLOW?
|
||||||
|
if flags&unix.AT_SYMLINK_NOFOLLOW == 0 {
|
||||||
|
flags |= unix.AT_SYMLINK_NOFOLLOW
|
||||||
|
}
|
||||||
|
return unix.Fstatat(dirfd, path, stat, flags)
|
||||||
|
}
|
||||||
|
|
||||||
|
const XATTR_SIZE_MAX = 65536
|
||||||
|
|
||||||
|
// Make the buffer 1kB bigger so we can detect overflows
|
||||||
|
const XATTR_BUFSZ = XATTR_SIZE_MAX + 1024
|
||||||
|
|
||||||
|
// Fgetxattr is a wrapper around unix.Fgetxattr that handles the buffer sizing.
|
||||||
|
func Fgetxattr(fd int, attr string) (val []byte, err error) {
|
||||||
|
// If the buffer is too small to fit the value, Linux and MacOS react
|
||||||
|
// differently:
|
||||||
|
// Linux: returns an ERANGE error and "-1" bytes.
|
||||||
|
// MacOS: truncates the value and returns "size" bytes.
|
||||||
|
//
|
||||||
|
// We choose the simple approach of buffer that is bigger than the limit on
|
||||||
|
// Linux, and return an error for everything that is bigger (which can
|
||||||
|
// only happen on MacOS).
|
||||||
|
//
|
||||||
|
// See https://github.com/pkg/xattr for a smarter solution.
|
||||||
|
// TODO: smarter buffer sizing?
|
||||||
|
buf := make([]byte, XATTR_BUFSZ)
|
||||||
|
sz, err := unix.Fgetxattr(fd, attr, buf)
|
||||||
|
if err == syscall.ERANGE {
|
||||||
|
// Do NOT return ERANGE - the user might retry ad inifinitum!
|
||||||
|
return nil, syscall.EOVERFLOW
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if sz >= XATTR_SIZE_MAX {
|
||||||
|
return nil, syscall.EOVERFLOW
|
||||||
|
}
|
||||||
|
// Copy only the actually used bytes to a new (smaller) buffer
|
||||||
|
// so "buf" never leaves the function and can be allocated on the stack.
|
||||||
|
val = make([]byte, sz)
|
||||||
|
copy(val, buf)
|
||||||
|
return val, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Lgetxattr is a wrapper around unix.Lgetxattr that handles the buffer sizing.
|
||||||
|
func Lgetxattr(path string, attr string) (val []byte, err error) {
|
||||||
|
// See the buffer sizing comments in Fgetxattr.
|
||||||
|
// TODO: smarter buffer sizing?
|
||||||
|
buf := make([]byte, XATTR_BUFSZ)
|
||||||
|
sz, err := unix.Lgetxattr(path, attr, buf)
|
||||||
|
if err == syscall.ERANGE {
|
||||||
|
// Do NOT return ERANGE - the user might retry ad inifinitum!
|
||||||
|
return nil, syscall.EOVERFLOW
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if sz >= XATTR_SIZE_MAX {
|
||||||
|
return nil, syscall.EOVERFLOW
|
||||||
|
}
|
||||||
|
// Copy only the actually used bytes to a new (smaller) buffer
|
||||||
|
// so "buf" never leaves the function and can be allocated on the stack.
|
||||||
|
val = make([]byte, sz)
|
||||||
|
copy(val, buf)
|
||||||
|
return val, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Flistxattr is a wrapper for unix.Flistxattr that handles buffer sizing and
|
||||||
|
// parsing the returned blob to a string slice.
|
||||||
|
func Flistxattr(fd int) (attrs []string, err error) {
|
||||||
|
// See the buffer sizing comments in Fgetxattr.
|
||||||
|
// TODO: smarter buffer sizing?
|
||||||
|
buf := make([]byte, XATTR_BUFSZ)
|
||||||
|
sz, err := unix.Flistxattr(fd, buf)
|
||||||
|
if err == syscall.ERANGE {
|
||||||
|
// Do NOT return ERANGE - the user might retry ad inifinitum!
|
||||||
|
return nil, syscall.EOVERFLOW
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if sz >= XATTR_SIZE_MAX {
|
||||||
|
return nil, syscall.EOVERFLOW
|
||||||
|
}
|
||||||
|
attrs = parseListxattrBlob(buf[:sz])
|
||||||
|
return attrs, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Llistxattr is a wrapper for unix.Llistxattr that handles buffer sizing and
|
||||||
|
// parsing the returned blob to a string slice.
|
||||||
|
func Llistxattr(path string) (attrs []string, err error) {
|
||||||
|
// TODO: smarter buffer sizing?
|
||||||
|
buf := make([]byte, XATTR_BUFSZ)
|
||||||
|
sz, err := unix.Llistxattr(path, buf)
|
||||||
|
if err == syscall.ERANGE {
|
||||||
|
// Do NOT return ERANGE - the user might retry ad inifinitum!
|
||||||
|
return nil, syscall.EOVERFLOW
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if sz >= XATTR_SIZE_MAX {
|
||||||
|
return nil, syscall.EOVERFLOW
|
||||||
|
}
|
||||||
|
attrs = parseListxattrBlob(buf[:sz])
|
||||||
|
return attrs, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func parseListxattrBlob(buf []byte) (attrs []string) {
|
||||||
|
parts := bytes.Split(buf, []byte{0})
|
||||||
|
for _, part := range parts {
|
||||||
|
if len(part) == 0 {
|
||||||
|
// Last part is empty, ignore
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
attrs = append(attrs, string(part))
|
||||||
|
}
|
||||||
|
return attrs
|
||||||
|
}
|
215
app/libgocryptfs/rewrites/syscallcompat/sys_darwin.go
Normal file
215
app/libgocryptfs/rewrites/syscallcompat/sys_darwin.go
Normal file
@ -0,0 +1,215 @@
|
|||||||
|
package syscallcompat
|
||||||
|
|
||||||
|
import (
|
||||||
|
"log"
|
||||||
|
"path/filepath"
|
||||||
|
"runtime"
|
||||||
|
"syscall"
|
||||||
|
"time"
|
||||||
|
"unsafe"
|
||||||
|
|
||||||
|
"golang.org/x/sys/unix"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
// O_DIRECT means oncached I/O on Linux. No direct equivalent on MacOS and defined
|
||||||
|
// to zero there.
|
||||||
|
O_DIRECT = 0
|
||||||
|
|
||||||
|
// O_PATH is only defined on Linux
|
||||||
|
O_PATH = 0
|
||||||
|
|
||||||
|
// KAUTH_UID_NONE and KAUTH_GID_NONE are special values to
|
||||||
|
// revert permissions to the process credentials.
|
||||||
|
KAUTH_UID_NONE = ^uint32(0) - 100
|
||||||
|
KAUTH_GID_NONE = ^uint32(0) - 100
|
||||||
|
)
|
||||||
|
|
||||||
|
// Unfortunately pthread_setugid_np does not have a syscall wrapper yet.
|
||||||
|
func pthread_setugid_np(uid uint32, gid uint32) (err error) {
|
||||||
|
_, _, e1 := syscall.RawSyscall(syscall.SYS_SETTID, uintptr(uid), uintptr(gid), 0)
|
||||||
|
if e1 != 0 {
|
||||||
|
err = e1
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Unfortunately fsetattrlist does not have a syscall wrapper yet.
|
||||||
|
func fsetattrlist(fd int, list unsafe.Pointer, buf unsafe.Pointer, size uintptr, options int) (err error) {
|
||||||
|
_, _, e1 := syscall.Syscall6(syscall.SYS_FSETATTRLIST, uintptr(fd), uintptr(list), uintptr(buf), uintptr(size), uintptr(options), 0)
|
||||||
|
if e1 != 0 {
|
||||||
|
err = e1
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Setattrlist already has a syscall wrapper, but it is not exported.
|
||||||
|
func setattrlist(path *byte, list unsafe.Pointer, buf unsafe.Pointer, size uintptr, options int) (err error) {
|
||||||
|
_, _, e1 := syscall.Syscall6(syscall.SYS_SETATTRLIST, uintptr(unsafe.Pointer(path)), uintptr(list), uintptr(buf), uintptr(size), uintptr(options), 0)
|
||||||
|
if e1 != 0 {
|
||||||
|
err = e1
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Sorry, fallocate is not available on OSX at all and
|
||||||
|
// fcntl F_PREALLOCATE is not accessible from Go.
|
||||||
|
// See https://github.com/rfjakob/gocryptfs/issues/18 if you want to help.
|
||||||
|
func EnospcPrealloc(fd int, off int64, len int64) error {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// See above.
|
||||||
|
func Fallocate(fd int, mode uint32, off int64, len int64) error {
|
||||||
|
return syscall.EOPNOTSUPP
|
||||||
|
}
|
||||||
|
|
||||||
|
// Dup3 is not available on Darwin, so we use Dup2 instead.
|
||||||
|
func Dup3(oldfd int, newfd int, flags int) (err error) {
|
||||||
|
if flags != 0 {
|
||||||
|
log.Panic("darwin does not support dup3 flags")
|
||||||
|
}
|
||||||
|
return syscall.Dup2(oldfd, newfd)
|
||||||
|
}
|
||||||
|
|
||||||
|
////////////////////////////////////////////////////////
|
||||||
|
//// Emulated Syscalls (see emulate.go) ////////////////
|
||||||
|
////////////////////////////////////////////////////////
|
||||||
|
|
||||||
|
func OpenatUser(dirfd int, path string, flags int, mode uint32, context *fuse.Context) (fd int, err error) {
|
||||||
|
if context != nil {
|
||||||
|
runtime.LockOSThread()
|
||||||
|
defer runtime.UnlockOSThread()
|
||||||
|
|
||||||
|
err = pthread_setugid_np(context.Owner.Uid, context.Owner.Gid)
|
||||||
|
if err != nil {
|
||||||
|
return -1, err
|
||||||
|
}
|
||||||
|
defer pthread_setugid_np(KAUTH_UID_NONE, KAUTH_GID_NONE)
|
||||||
|
}
|
||||||
|
|
||||||
|
return Openat(dirfd, path, flags, mode)
|
||||||
|
}
|
||||||
|
|
||||||
|
func Mknodat(dirfd int, path string, mode uint32, dev int) (err error) {
|
||||||
|
return emulateMknodat(dirfd, path, mode, dev)
|
||||||
|
}
|
||||||
|
|
||||||
|
func MknodatUser(dirfd int, path string, mode uint32, dev int, context *fuse.Context) (err error) {
|
||||||
|
if context != nil {
|
||||||
|
runtime.LockOSThread()
|
||||||
|
defer runtime.UnlockOSThread()
|
||||||
|
|
||||||
|
err = pthread_setugid_np(context.Owner.Uid, context.Owner.Gid)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer pthread_setugid_np(KAUTH_UID_NONE, KAUTH_GID_NONE)
|
||||||
|
}
|
||||||
|
|
||||||
|
return Mknodat(dirfd, path, mode, dev)
|
||||||
|
}
|
||||||
|
|
||||||
|
func FchmodatNofollow(dirfd int, path string, mode uint32) (err error) {
|
||||||
|
return unix.Fchmodat(dirfd, path, mode, unix.AT_SYMLINK_NOFOLLOW)
|
||||||
|
}
|
||||||
|
|
||||||
|
func SymlinkatUser(oldpath string, newdirfd int, newpath string, context *fuse.Context) (err error) {
|
||||||
|
if context != nil {
|
||||||
|
runtime.LockOSThread()
|
||||||
|
defer runtime.UnlockOSThread()
|
||||||
|
|
||||||
|
err = pthread_setugid_np(context.Owner.Uid, context.Owner.Gid)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer pthread_setugid_np(KAUTH_UID_NONE, KAUTH_GID_NONE)
|
||||||
|
}
|
||||||
|
|
||||||
|
return Symlinkat(oldpath, newdirfd, newpath)
|
||||||
|
}
|
||||||
|
|
||||||
|
func MkdiratUser(dirfd int, path string, mode uint32, context *fuse.Context) (err error) {
|
||||||
|
if context != nil {
|
||||||
|
runtime.LockOSThread()
|
||||||
|
defer runtime.UnlockOSThread()
|
||||||
|
|
||||||
|
err = pthread_setugid_np(context.Owner.Uid, context.Owner.Gid)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer pthread_setugid_np(KAUTH_UID_NONE, KAUTH_GID_NONE)
|
||||||
|
}
|
||||||
|
|
||||||
|
return Mkdirat(dirfd, path, mode)
|
||||||
|
}
|
||||||
|
|
||||||
|
type attrList struct {
|
||||||
|
bitmapCount uint16
|
||||||
|
_ uint16
|
||||||
|
CommonAttr uint32
|
||||||
|
VolAttr uint32
|
||||||
|
DirAttr uint32
|
||||||
|
FileAttr uint32
|
||||||
|
Forkattr uint32
|
||||||
|
}
|
||||||
|
|
||||||
|
func timesToAttrList(a *time.Time, m *time.Time) (attrList attrList, attributes [2]unix.Timespec) {
|
||||||
|
attrList.bitmapCount = unix.ATTR_BIT_MAP_COUNT
|
||||||
|
attrList.CommonAttr = 0
|
||||||
|
i := 0
|
||||||
|
if m != nil {
|
||||||
|
attributes[i] = unix.Timespec(fuse.UtimeToTimespec(m))
|
||||||
|
attrList.CommonAttr |= unix.ATTR_CMN_MODTIME
|
||||||
|
i += 1
|
||||||
|
}
|
||||||
|
if a != nil {
|
||||||
|
attributes[i] = unix.Timespec(fuse.UtimeToTimespec(a))
|
||||||
|
attrList.CommonAttr |= unix.ATTR_CMN_ACCTIME
|
||||||
|
i += 1
|
||||||
|
}
|
||||||
|
return attrList, attributes
|
||||||
|
}
|
||||||
|
|
||||||
|
// FutimesNano syscall.
|
||||||
|
func FutimesNano(fd int, a *time.Time, m *time.Time) (err error) {
|
||||||
|
attrList, attributes := timesToAttrList(a, m)
|
||||||
|
return fsetattrlist(fd, unsafe.Pointer(&attrList), unsafe.Pointer(&attributes),
|
||||||
|
unsafe.Sizeof(attributes), 0)
|
||||||
|
}
|
||||||
|
|
||||||
|
// UtimesNanoAtNofollow is like UtimesNanoAt but never follows symlinks.
|
||||||
|
//
|
||||||
|
// Unfortunately we cannot use unix.UtimesNanoAt since it is broken and just
|
||||||
|
// ignores the provided 'dirfd'. In addition, it also lacks handling of 'nil'
|
||||||
|
// pointers (used to preserve one of both timestamps).
|
||||||
|
func UtimesNanoAtNofollow(dirfd int, path string, a *time.Time, m *time.Time) (err error) {
|
||||||
|
if !filepath.IsAbs(path) {
|
||||||
|
chdirMutex.Lock()
|
||||||
|
defer chdirMutex.Unlock()
|
||||||
|
var cwd int
|
||||||
|
cwd, err = syscall.Open(".", syscall.O_RDONLY, 0)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer syscall.Close(cwd)
|
||||||
|
err = syscall.Fchdir(dirfd)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer syscall.Fchdir(cwd)
|
||||||
|
}
|
||||||
|
|
||||||
|
_p0, err := syscall.BytePtrFromString(path)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
attrList, attributes := timesToAttrList(a, m)
|
||||||
|
return setattrlist(_p0, unsafe.Pointer(&attrList), unsafe.Pointer(&attributes),
|
||||||
|
unsafe.Sizeof(attributes), unix.FSOPT_NOFOLLOW)
|
||||||
|
}
|
||||||
|
|
||||||
|
func Getdents(fd int) ([]fuse.DirEntry, error) {
|
||||||
|
return emulateGetdents(fd)
|
||||||
|
}
|
153
app/libgocryptfs/rewrites/syscallcompat/sys_linux.go
Normal file
153
app/libgocryptfs/rewrites/syscallcompat/sys_linux.go
Normal file
@ -0,0 +1,153 @@
|
|||||||
|
// Package syscallcompat wraps Linux-specific syscalls.
|
||||||
|
package syscallcompat
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"io/ioutil"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
"sync"
|
||||||
|
"syscall"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"golang.org/x/sys/unix"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
_FALLOC_FL_KEEP_SIZE = 0x01
|
||||||
|
|
||||||
|
// O_DIRECT means oncached I/O on Linux. No direct equivalent on MacOS and defined
|
||||||
|
// to zero there.
|
||||||
|
O_DIRECT = syscall.O_DIRECT
|
||||||
|
|
||||||
|
// O_PATH is only defined on Linux
|
||||||
|
O_PATH = unix.O_PATH
|
||||||
|
)
|
||||||
|
|
||||||
|
var preallocWarn sync.Once
|
||||||
|
|
||||||
|
// EnospcPrealloc preallocates ciphertext space without changing the file
|
||||||
|
// size. This guarantees that we don't run out of space while writing a
|
||||||
|
// ciphertext block (that would corrupt the block).
|
||||||
|
func EnospcPrealloc(fd int, off int64, len int64) (err error) {
|
||||||
|
for {
|
||||||
|
err = syscall.Fallocate(fd, _FALLOC_FL_KEEP_SIZE, off, len)
|
||||||
|
if err == syscall.EINTR {
|
||||||
|
// fallocate, like many syscalls, can return EINTR. This is not an
|
||||||
|
// error and just signifies that the operation was interrupted by a
|
||||||
|
// signal and we should try again.
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if err == syscall.EOPNOTSUPP {
|
||||||
|
// ZFS and ext3 do not support fallocate. Warn but continue anyway.
|
||||||
|
// https://github.com/rfjakob/gocryptfs/issues/22
|
||||||
|
preallocWarn.Do(func() {})
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Fallocate wraps the Fallocate syscall.
|
||||||
|
func Fallocate(fd int, mode uint32, off int64, len int64) (err error) {
|
||||||
|
return syscall.Fallocate(fd, mode, off, len)
|
||||||
|
}
|
||||||
|
|
||||||
|
func getSupplementaryGroups(pid uint32) (gids []int) {
|
||||||
|
procPath := fmt.Sprintf("/proc/%d/task/%d/status", pid, pid)
|
||||||
|
blob, err := ioutil.ReadFile(procPath)
|
||||||
|
if err != nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
lines := strings.Split(string(blob), "\n")
|
||||||
|
for _, line := range lines {
|
||||||
|
if strings.HasPrefix(line, "Groups:") {
|
||||||
|
f := strings.Fields(line[7:])
|
||||||
|
gids = make([]int, len(f))
|
||||||
|
for i := range gids {
|
||||||
|
val, err := strconv.ParseInt(f[i], 10, 32)
|
||||||
|
if err != nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
gids[i] = int(val)
|
||||||
|
}
|
||||||
|
return gids
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Mknodat wraps the Mknodat syscall.
|
||||||
|
func Mknodat(dirfd int, path string, mode uint32, dev int) (err error) {
|
||||||
|
return syscall.Mknodat(dirfd, path, mode, dev)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Dup3 wraps the Dup3 syscall. We want to use Dup3 rather than Dup2 because Dup2
|
||||||
|
// is not implemented on arm64.
|
||||||
|
func Dup3(oldfd int, newfd int, flags int) (err error) {
|
||||||
|
return syscall.Dup3(oldfd, newfd, flags)
|
||||||
|
}
|
||||||
|
|
||||||
|
// FchmodatNofollow is like Fchmodat but never follows symlinks.
|
||||||
|
//
|
||||||
|
// This should be handled by the AT_SYMLINK_NOFOLLOW flag, but Linux
|
||||||
|
// does not implement it, so we have to perform an elaborate dance
|
||||||
|
// with O_PATH and /proc/self/fd.
|
||||||
|
//
|
||||||
|
// See also: Qemu implemented the same logic as fchmodat_nofollow():
|
||||||
|
// https://git.qemu.org/?p=qemu.git;a=blob;f=hw/9pfs/9p-local.c#l335
|
||||||
|
func FchmodatNofollow(dirfd int, path string, mode uint32) (err error) {
|
||||||
|
// Open handle to the filename (but without opening the actual file).
|
||||||
|
// This succeeds even when we don't have read permissions to the file.
|
||||||
|
fd, err := syscall.Openat(dirfd, path, syscall.O_NOFOLLOW|O_PATH, 0)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer syscall.Close(fd)
|
||||||
|
|
||||||
|
// Now we can check the type without the risk of race-conditions.
|
||||||
|
// Return syscall.ELOOP if it is a symlink.
|
||||||
|
var st syscall.Stat_t
|
||||||
|
err = syscall.Fstat(fd, &st)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if st.Mode&syscall.S_IFMT == syscall.S_IFLNK {
|
||||||
|
return syscall.ELOOP
|
||||||
|
}
|
||||||
|
|
||||||
|
// Change mode of the actual file. Fchmod does not work with O_PATH,
|
||||||
|
// but Chmod via /proc/self/fd works.
|
||||||
|
procPath := fmt.Sprintf("/proc/self/fd/%d", fd)
|
||||||
|
return syscall.Chmod(procPath, mode)
|
||||||
|
}
|
||||||
|
|
||||||
|
func timesToTimespec(a *time.Time, m *time.Time) []unix.Timespec {
|
||||||
|
ts := make([]unix.Timespec, 2)
|
||||||
|
ta, _ := unix.TimeToTimespec(*a)
|
||||||
|
ts[0] = unix.Timespec(ta)
|
||||||
|
tm, _ := unix.TimeToTimespec(*m)
|
||||||
|
ts[1] = unix.Timespec(tm)
|
||||||
|
return ts
|
||||||
|
}
|
||||||
|
|
||||||
|
// FutimesNano syscall.
|
||||||
|
func FutimesNano(fd int, a *time.Time, m *time.Time) (err error) {
|
||||||
|
ts := timesToTimespec(a, m)
|
||||||
|
// To avoid introducing a separate syscall wrapper for futimens()
|
||||||
|
// (as done in go-fuse, for example), we instead use the /proc/self/fd trick.
|
||||||
|
procPath := fmt.Sprintf("/proc/self/fd/%d", fd)
|
||||||
|
return unix.UtimesNanoAt(unix.AT_FDCWD, procPath, ts, 0)
|
||||||
|
}
|
||||||
|
|
||||||
|
// UtimesNanoAtNofollow is like UtimesNanoAt but never follows symlinks.
|
||||||
|
func UtimesNanoAtNofollow(dirfd int, path string, a *time.Time, m *time.Time) (err error) {
|
||||||
|
ts := timesToTimespec(a, m)
|
||||||
|
return unix.UtimesNanoAt(dirfd, path, ts, unix.AT_SYMLINK_NOFOLLOW)
|
||||||
|
}
|
||||||
|
|
||||||
|
func Getdents(fd int) ([]DirEntry, error) {
|
||||||
|
return getdents(fd)
|
||||||
|
}
|
@ -0,0 +1,26 @@
|
|||||||
|
package syscallcompat
|
||||||
|
|
||||||
|
import (
|
||||||
|
"syscall"
|
||||||
|
|
||||||
|
"golang.org/x/sys/unix"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Unix2syscall converts a unix.Stat_t struct to a syscall.Stat_t struct.
|
||||||
|
func Unix2syscall(u unix.Stat_t) syscall.Stat_t {
|
||||||
|
return syscall.Stat_t{
|
||||||
|
Dev: u.Dev,
|
||||||
|
Ino: u.Ino,
|
||||||
|
Nlink: u.Nlink,
|
||||||
|
Mode: u.Mode,
|
||||||
|
Uid: u.Uid,
|
||||||
|
Gid: u.Gid,
|
||||||
|
Rdev: u.Rdev,
|
||||||
|
Size: u.Size,
|
||||||
|
Blksize: u.Blksize,
|
||||||
|
Blocks: u.Blocks,
|
||||||
|
Atimespec: syscall.Timespec(u.Atim),
|
||||||
|
Mtimespec: syscall.Timespec(u.Mtim),
|
||||||
|
Ctimespec: syscall.Timespec(u.Ctim),
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,28 @@
|
|||||||
|
package syscallcompat
|
||||||
|
|
||||||
|
import (
|
||||||
|
"syscall"
|
||||||
|
|
||||||
|
"golang.org/x/sys/unix"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Unix2syscall converts a unix.Stat_t struct to a syscall.Stat_t struct.
|
||||||
|
// A direct cast does not work because the padding is named differently in
|
||||||
|
// unix.Stat_t for some reason ("X__unused" in syscall, "_" in unix).
|
||||||
|
func Unix2syscall(u unix.Stat_t) syscall.Stat_t {
|
||||||
|
return syscall.Stat_t{
|
||||||
|
Dev: u.Dev,
|
||||||
|
Ino: u.Ino,
|
||||||
|
Nlink: u.Nlink,
|
||||||
|
Mode: u.Mode,
|
||||||
|
Uid: u.Uid,
|
||||||
|
Gid: u.Gid,
|
||||||
|
Rdev: u.Rdev,
|
||||||
|
Size: u.Size,
|
||||||
|
Blksize: u.Blksize,
|
||||||
|
Blocks: u.Blocks,
|
||||||
|
Atim: syscall.NsecToTimespec(unix.TimespecToNsec(u.Atim)),
|
||||||
|
Mtim: syscall.NsecToTimespec(unix.TimespecToNsec(u.Mtim)),
|
||||||
|
Ctim: syscall.NsecToTimespec(unix.TimespecToNsec(u.Ctim)),
|
||||||
|
}
|
||||||
|
}
|
21
app/proguard-rules.pro
vendored
Normal file
21
app/proguard-rules.pro
vendored
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
# Add project specific ProGuard rules here.
|
||||||
|
# You can control the set of applied configuration files using the
|
||||||
|
# proguardFiles setting in build.gradle.
|
||||||
|
#
|
||||||
|
# For more details, see
|
||||||
|
# http://developer.android.com/guide/developing/tools/proguard.html
|
||||||
|
|
||||||
|
# If your project uses WebView with JS, uncomment the following
|
||||||
|
# and specify the fully qualified class name to the JavaScript interface
|
||||||
|
# class:
|
||||||
|
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
|
||||||
|
# public *;
|
||||||
|
#}
|
||||||
|
|
||||||
|
# Uncomment this to preserve the line number information for
|
||||||
|
# debugging stack traces.
|
||||||
|
#-keepattributes SourceFile,LineNumberTable
|
||||||
|
|
||||||
|
# If you keep the line number information, uncomment this to
|
||||||
|
# hide the original source file name.
|
||||||
|
#-renamesourcefileattribute SourceFile
|
66
app/src/main/AndroidManifest.xml
Normal file
66
app/src/main/AndroidManifest.xml
Normal file
@ -0,0 +1,66 @@
|
|||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
|
package="sushi.hardcore.droidfs">
|
||||||
|
|
||||||
|
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
|
||||||
|
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
|
||||||
|
<uses-permission android:name="android.permission.USE_FINGERPRINT" />
|
||||||
|
<uses-permission android:name="android.permission.USE_BIOMETRIC" />
|
||||||
|
|
||||||
|
<application
|
||||||
|
android:name=".ColoredApplication"
|
||||||
|
android:allowBackup="true"
|
||||||
|
android:icon="@drawable/logo"
|
||||||
|
android:label="@string/app_name"
|
||||||
|
android:supportsRtl="true"
|
||||||
|
android:theme="@style/AppTheme">
|
||||||
|
<activity
|
||||||
|
android:name=".SettingsActivity"
|
||||||
|
android:theme="@style/Theme.Cyanea.Dark.NoActionBar"
|
||||||
|
android:label="@string/title_activity_settings"
|
||||||
|
android:parentActivityName=".MainActivity"/>
|
||||||
|
<activity android:name=".explorers.ExplorerActivity" />
|
||||||
|
<activity android:name=".explorers.ExplorerActivityPick" />
|
||||||
|
<activity android:name=".explorers.ExplorerActivityDrop" />
|
||||||
|
<activity
|
||||||
|
android:name=".OpenActivity"
|
||||||
|
android:theme="@style/Theme.Cyanea.Dark.NoActionBar"
|
||||||
|
android:screenOrientation="nosensor"
|
||||||
|
android:windowSoftInputMode="adjustPan">
|
||||||
|
<intent-filter android:label="@string/share_menu_label">
|
||||||
|
<action android:name="android.intent.action.SEND" />
|
||||||
|
<action android:name="android.intent.action.SEND_MULTIPLE" />
|
||||||
|
<category android:name="android.intent.category.DEFAULT" />
|
||||||
|
<data android:mimeType="*/*" />
|
||||||
|
</intent-filter>
|
||||||
|
</activity>
|
||||||
|
<activity
|
||||||
|
android:name=".CreateActivity"
|
||||||
|
android:theme="@style/Theme.Cyanea.Dark.NoActionBar"
|
||||||
|
android:screenOrientation="nosensor" />
|
||||||
|
<activity
|
||||||
|
android:name=".ChangePasswordActivity"
|
||||||
|
android:theme="@style/Theme.Cyanea.Dark.NoActionBar"
|
||||||
|
android:screenOrientation="nosensor"
|
||||||
|
android:windowSoftInputMode="adjustPan" />
|
||||||
|
<activity
|
||||||
|
android:name=".MainActivity"
|
||||||
|
android:screenOrientation="nosensor">
|
||||||
|
<intent-filter>
|
||||||
|
<action android:name="android.intent.action.MAIN" />
|
||||||
|
<category android:name="android.intent.category.LAUNCHER" />
|
||||||
|
</intent-filter>
|
||||||
|
</activity>
|
||||||
|
<activity android:name=".file_viewers.ImageViewer" android:configChanges="screenSize|orientation" /> <!-- don't reload content on configuration change -->
|
||||||
|
<activity android:name=".file_viewers.VideoPlayer" android:configChanges="screenSize|orientation" />
|
||||||
|
<activity android:name=".file_viewers.AudioPlayer" android:configChanges="screenSize|orientation" />
|
||||||
|
<activity android:name=".file_viewers.TextEditor" android:configChanges="screenSize|orientation" />
|
||||||
|
|
||||||
|
<provider
|
||||||
|
android:name=".provider.TemporaryFileProvider"
|
||||||
|
android:authorities="${applicationId}.tempstorage"
|
||||||
|
android:exported="true"
|
||||||
|
android:writePermission="${applicationId}.READ_TEMPORARY_STORAGE" />
|
||||||
|
</application>
|
||||||
|
|
||||||
|
</manifest>
|
@ -0,0 +1,202 @@
|
|||||||
|
package sushi.hardcore.droidfs
|
||||||
|
|
||||||
|
import android.app.Activity
|
||||||
|
import android.content.Intent
|
||||||
|
import android.os.Build
|
||||||
|
import android.os.Bundle
|
||||||
|
import android.text.Editable
|
||||||
|
import android.text.TextWatcher
|
||||||
|
import android.view.View
|
||||||
|
import android.view.WindowManager
|
||||||
|
import android.widget.AdapterView.OnItemClickListener
|
||||||
|
import android.widget.Toast
|
||||||
|
import kotlinx.android.synthetic.main.activity_change_password.*
|
||||||
|
import kotlinx.android.synthetic.main.activity_change_password.checkbox_remember_path
|
||||||
|
import kotlinx.android.synthetic.main.activity_change_password.checkbox_save_password
|
||||||
|
import kotlinx.android.synthetic.main.activity_change_password.edit_volume_path
|
||||||
|
import kotlinx.android.synthetic.main.activity_change_password.saved_path_listview
|
||||||
|
import kotlinx.android.synthetic.main.toolbar.*
|
||||||
|
import sushi.hardcore.droidfs.adapters.SavedVolumesAdapter
|
||||||
|
import sushi.hardcore.droidfs.fingerprint_stuff.FingerprintPasswordHashSaver
|
||||||
|
import sushi.hardcore.droidfs.util.FilesUtils
|
||||||
|
import sushi.hardcore.droidfs.util.GocryptfsVolume
|
||||||
|
import sushi.hardcore.droidfs.util.WidgetUtil
|
||||||
|
import sushi.hardcore.droidfs.util.Wiper
|
||||||
|
import sushi.hardcore.droidfs.widgets.ColoredAlertDialog
|
||||||
|
import java.util.*
|
||||||
|
|
||||||
|
class ChangePasswordActivity : ColoredActivity() {
|
||||||
|
companion object {
|
||||||
|
private const val PICK_DIRECTORY_REQUEST_CODE = 1
|
||||||
|
}
|
||||||
|
private lateinit var fingerprintPasswordHashSaver: FingerprintPasswordHashSaver
|
||||||
|
private lateinit var root_cipher_dir: String
|
||||||
|
private var usf_fingerprint = false
|
||||||
|
override fun onCreate(savedInstanceState: Bundle?) {
|
||||||
|
super.onCreate(savedInstanceState)
|
||||||
|
if (!sharedPrefs.getBoolean("usf_screenshot", false)){
|
||||||
|
window.setFlags(WindowManager.LayoutParams.FLAG_SECURE, WindowManager.LayoutParams.FLAG_SECURE)
|
||||||
|
}
|
||||||
|
setContentView(R.layout.activity_change_password)
|
||||||
|
setSupportActionBar(toolbar)
|
||||||
|
usf_fingerprint = sharedPrefs.getBoolean("usf_fingerprint", false)
|
||||||
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && usf_fingerprint) {
|
||||||
|
fingerprintPasswordHashSaver = FingerprintPasswordHashSaver(this, sharedPrefs)
|
||||||
|
} else {
|
||||||
|
WidgetUtil.hide(checkbox_save_password)
|
||||||
|
}
|
||||||
|
val savedVolumesAdapter = SavedVolumesAdapter(this, sharedPrefs)
|
||||||
|
if (savedVolumesAdapter.count > 0){
|
||||||
|
saved_path_listview.adapter = savedVolumesAdapter
|
||||||
|
saved_path_listview.onItemClickListener = OnItemClickListener { _, _, position, _ ->
|
||||||
|
edit_volume_path.setText(savedVolumesAdapter.getItem(position))
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
WidgetUtil.hide(saved_path_listview)
|
||||||
|
}
|
||||||
|
edit_volume_path.addTextChangedListener(object: TextWatcher{
|
||||||
|
override fun afterTextChanged(s: Editable?) {
|
||||||
|
}
|
||||||
|
override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
|
||||||
|
}
|
||||||
|
override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
|
||||||
|
if (sharedPrefs.getString(s.toString(), null) == null) {
|
||||||
|
edit_old_password.hint = null
|
||||||
|
} else {
|
||||||
|
edit_old_password.hint = getString(R.string.hash_saved_hint)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
edit_new_password_confirm.setOnEditorActionListener { v, _, _ ->
|
||||||
|
onClickChangePassword(v)
|
||||||
|
true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun pick_directory(view: View?) {
|
||||||
|
val i = Intent(Intent.ACTION_OPEN_DOCUMENT_TREE)
|
||||||
|
startActivityForResult(i, PICK_DIRECTORY_REQUEST_CODE)
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
|
||||||
|
super.onActivityResult(requestCode, resultCode, data)
|
||||||
|
if (resultCode == Activity.RESULT_OK) {
|
||||||
|
if (requestCode == PICK_DIRECTORY_REQUEST_CODE) {
|
||||||
|
if (data != null) {
|
||||||
|
val path = FilesUtils.getFullPathFromTreeUri(data.data, this)
|
||||||
|
edit_volume_path.setText(path)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun onClickChangePassword(view: View?) {
|
||||||
|
root_cipher_dir = edit_volume_path.text.toString()
|
||||||
|
if (root_cipher_dir.isEmpty()) {
|
||||||
|
Toast.makeText(this, R.string.enter_volume_path, Toast.LENGTH_SHORT).show()
|
||||||
|
} else {
|
||||||
|
changePassword(null)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun changePassword(givenHash: ByteArray?){
|
||||||
|
val new_password = edit_new_password.text.toString().toCharArray()
|
||||||
|
val new_password_confirm = edit_new_password_confirm.text.toString().toCharArray()
|
||||||
|
if (!new_password.contentEquals(new_password_confirm)) {
|
||||||
|
Toast.makeText(applicationContext, R.string.passwords_mismatch, Toast.LENGTH_SHORT).show()
|
||||||
|
} else {
|
||||||
|
val old_password = edit_old_password.text.toString().toCharArray()
|
||||||
|
var returnedHash: ByteArray? = null
|
||||||
|
if (usf_fingerprint && checkbox_save_password.isChecked){
|
||||||
|
returnedHash = ByteArray(GocryptfsVolume.KeyLen)
|
||||||
|
}
|
||||||
|
var changePasswordImmediately = true
|
||||||
|
if (givenHash == null){
|
||||||
|
val cipherText = sharedPrefs.getString(root_cipher_dir, null)
|
||||||
|
if (cipherText != null){ //password hash saved
|
||||||
|
fingerprintPasswordHashSaver.decrypt(cipherText, root_cipher_dir, ::changePassword)
|
||||||
|
changePasswordImmediately = false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (changePasswordImmediately){
|
||||||
|
if (GocryptfsVolume.change_password(root_cipher_dir, old_password, givenHash, new_password, returnedHash)) {
|
||||||
|
val editor = sharedPrefs.edit()
|
||||||
|
if (sharedPrefs.getString(root_cipher_dir, null) != null){
|
||||||
|
editor.remove(root_cipher_dir)
|
||||||
|
editor.apply()
|
||||||
|
}
|
||||||
|
var continueImmediately = true
|
||||||
|
if (checkbox_remember_path.isChecked) {
|
||||||
|
val old_saved_volumes_paths = sharedPrefs.getStringSet(ConstValues.saved_volumes_key, HashSet()) as Set<String>
|
||||||
|
val new_saved_volumes_paths = old_saved_volumes_paths.toMutableList()
|
||||||
|
if (!old_saved_volumes_paths.contains(root_cipher_dir)) {
|
||||||
|
new_saved_volumes_paths.add(root_cipher_dir)
|
||||||
|
editor.putStringSet(ConstValues.saved_volumes_key, new_saved_volumes_paths.toSet())
|
||||||
|
editor.apply()
|
||||||
|
}
|
||||||
|
if (checkbox_save_password.isChecked && returnedHash != null){
|
||||||
|
fingerprintPasswordHashSaver.encryptAndSave(returnedHash, root_cipher_dir){ _ ->
|
||||||
|
onPasswordChanged()
|
||||||
|
}
|
||||||
|
continueImmediately = false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (continueImmediately){
|
||||||
|
onPasswordChanged()
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
ColoredAlertDialog(this)
|
||||||
|
.setTitle(R.string.error)
|
||||||
|
.setMessage(R.string.change_password_failed)
|
||||||
|
.setPositiveButton(R.string.ok, null)
|
||||||
|
.show()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Arrays.fill(old_password, 0.toChar())
|
||||||
|
}
|
||||||
|
Arrays.fill(new_password, 0.toChar())
|
||||||
|
Arrays.fill(new_password_confirm, 0.toChar())
|
||||||
|
}
|
||||||
|
|
||||||
|
fun onPasswordChanged(){
|
||||||
|
ColoredAlertDialog(this)
|
||||||
|
.setTitle(R.string.success_change_password)
|
||||||
|
.setMessage(R.string.success_change_password_msg)
|
||||||
|
.setCancelable(false)
|
||||||
|
.setPositiveButton(R.string.ok) { _, _ -> finish() }
|
||||||
|
.show()
|
||||||
|
}
|
||||||
|
|
||||||
|
fun onClickSavePasswordHash(view: View) {
|
||||||
|
if (checkbox_save_password.isChecked){
|
||||||
|
if (!fingerprintPasswordHashSaver.canAuthenticate()){
|
||||||
|
checkbox_save_password.isChecked = false
|
||||||
|
} else {
|
||||||
|
checkbox_remember_path.isChecked = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun onClickRememberPath(view: View) {
|
||||||
|
if (!checkbox_remember_path.isChecked){
|
||||||
|
checkbox_save_password.isChecked = false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onPause() {
|
||||||
|
super.onPause()
|
||||||
|
if (::fingerprintPasswordHashSaver.isInitialized && fingerprintPasswordHashSaver.isListening){
|
||||||
|
fingerprintPasswordHashSaver.stopListening()
|
||||||
|
if (fingerprintPasswordHashSaver.fingerprintFragment.isAdded){
|
||||||
|
fingerprintPasswordHashSaver.fingerprintFragment.dismiss()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onDestroy() {
|
||||||
|
super.onDestroy()
|
||||||
|
Wiper.wipeEditText(edit_old_password)
|
||||||
|
Wiper.wipeEditText(edit_new_password)
|
||||||
|
Wiper.wipeEditText(edit_new_password_confirm)
|
||||||
|
}
|
||||||
|
}
|
9
app/src/main/java/sushi/hardcore/droidfs/ColorEngine.kt
Normal file
9
app/src/main/java/sushi/hardcore/droidfs/ColorEngine.kt
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
package sushi.hardcore.droidfs
|
||||||
|
|
||||||
|
import android.widget.ImageView
|
||||||
|
|
||||||
|
class ColorEngine(val themeColor: Int) {
|
||||||
|
fun applyTo(imageView: ImageView){
|
||||||
|
imageView.setColorFilter(themeColor)
|
||||||
|
}
|
||||||
|
}
|
32
app/src/main/java/sushi/hardcore/droidfs/ColoredActivity.kt
Normal file
32
app/src/main/java/sushi/hardcore/droidfs/ColoredActivity.kt
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
package sushi.hardcore.droidfs
|
||||||
|
|
||||||
|
import android.content.SharedPreferences
|
||||||
|
import android.os.Bundle
|
||||||
|
import android.view.WindowManager
|
||||||
|
import androidx.core.content.ContextCompat
|
||||||
|
import androidx.preference.PreferenceManager
|
||||||
|
import com.jaredrummler.cyanea.app.CyaneaAppCompatActivity
|
||||||
|
import sushi.hardcore.droidfs.widgets.ThemeColor
|
||||||
|
|
||||||
|
open class ColoredActivity: CyaneaAppCompatActivity() {
|
||||||
|
protected lateinit var sharedPrefs: SharedPreferences
|
||||||
|
override fun onCreate(savedInstanceState: Bundle?) {
|
||||||
|
super.onCreate(savedInstanceState)
|
||||||
|
sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this)
|
||||||
|
if (!sharedPrefs.getBoolean("usf_screenshot", false)){
|
||||||
|
window.setFlags(WindowManager.LayoutParams.FLAG_SECURE, WindowManager.LayoutParams.FLAG_SECURE)
|
||||||
|
}
|
||||||
|
val themeColor = ThemeColor.getThemeColor(this)
|
||||||
|
val backgroundColor = ContextCompat.getColor(this, R.color.backgroundColor)
|
||||||
|
if (cyanea.accent != themeColor){
|
||||||
|
cyanea.edit{
|
||||||
|
accent(themeColor)
|
||||||
|
//accentDark(themeColor)
|
||||||
|
//accentLight(themeColor)
|
||||||
|
background(backgroundColor)
|
||||||
|
//backgroundDark(backgroundColor)
|
||||||
|
//backgroundLight(backgroundColor)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,11 @@
|
|||||||
|
package sushi.hardcore.droidfs
|
||||||
|
|
||||||
|
import android.app.Application
|
||||||
|
import com.jaredrummler.cyanea.Cyanea
|
||||||
|
|
||||||
|
class ColoredApplication: Application() {
|
||||||
|
override fun onCreate() {
|
||||||
|
super.onCreate()
|
||||||
|
Cyanea.init(this, resources)
|
||||||
|
}
|
||||||
|
}
|
41
app/src/main/java/sushi/hardcore/droidfs/ConstValues.kt
Normal file
41
app/src/main/java/sushi/hardcore/droidfs/ConstValues.kt
Normal file
@ -0,0 +1,41 @@
|
|||||||
|
package sushi.hardcore.droidfs
|
||||||
|
|
||||||
|
import java.io.File
|
||||||
|
|
||||||
|
class ConstValues {
|
||||||
|
companion object {
|
||||||
|
const val creator = "DroidFS"
|
||||||
|
const val saved_volumes_key = "saved_volumes"
|
||||||
|
const val sort_order_key = "sort_order"
|
||||||
|
const val wipe_passes = 2
|
||||||
|
const val seek_bar_inc = 200
|
||||||
|
private val fileExtensions = mapOf(
|
||||||
|
Pair("image", listOf("png", "jpg", "jpeg")),
|
||||||
|
Pair("video", listOf("mp4", "webm")),
|
||||||
|
Pair("audio", listOf("mp3", "ogg")),
|
||||||
|
Pair("text", listOf("txt", "json", "conf", "xml", "java", "kt", "py", "go", "c", "h", "cpp", "hpp", "sh", "js", "html", "css", "php"))
|
||||||
|
)
|
||||||
|
|
||||||
|
fun isImage(path: String): Boolean {
|
||||||
|
return fileExtensions["image"]?.contains(File(path).extension) ?: false
|
||||||
|
}
|
||||||
|
fun isVideo(path: String): Boolean {
|
||||||
|
return fileExtensions["video"]?.contains(File(path).extension) ?: false
|
||||||
|
}
|
||||||
|
fun isAudio(path: String): Boolean {
|
||||||
|
return fileExtensions["audio"]?.contains(File(path).extension) ?: false
|
||||||
|
}
|
||||||
|
fun isText(path: String): Boolean {
|
||||||
|
return fileExtensions["text"]?.contains(File(path).extension) ?: false
|
||||||
|
}
|
||||||
|
fun getAssociatedDrawable(path: String): Int {
|
||||||
|
return when {
|
||||||
|
isAudio(path) -> R.drawable.icon_file_audio
|
||||||
|
isImage(path) -> R.drawable.icon_file_image
|
||||||
|
isVideo(path) -> R.drawable.icon_file_video
|
||||||
|
isText(path) -> R.drawable.icon_file_text
|
||||||
|
else -> R.drawable.icon_file_unknown
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
191
app/src/main/java/sushi/hardcore/droidfs/CreateActivity.kt
Normal file
191
app/src/main/java/sushi/hardcore/droidfs/CreateActivity.kt
Normal file
@ -0,0 +1,191 @@
|
|||||||
|
package sushi.hardcore.droidfs
|
||||||
|
|
||||||
|
import android.app.Activity
|
||||||
|
import android.content.Intent
|
||||||
|
import android.os.Build
|
||||||
|
import android.os.Bundle
|
||||||
|
import android.view.View
|
||||||
|
import android.view.WindowManager
|
||||||
|
import android.widget.Toast
|
||||||
|
import kotlinx.android.synthetic.main.activity_create.*
|
||||||
|
import kotlinx.android.synthetic.main.activity_create.checkbox_remember_path
|
||||||
|
import kotlinx.android.synthetic.main.activity_create.checkbox_save_password
|
||||||
|
import kotlinx.android.synthetic.main.activity_create.edit_password
|
||||||
|
import kotlinx.android.synthetic.main.activity_create.edit_volume_path
|
||||||
|
import kotlinx.android.synthetic.main.toolbar.*
|
||||||
|
import sushi.hardcore.droidfs.explorers.ExplorerActivity
|
||||||
|
import sushi.hardcore.droidfs.fingerprint_stuff.FingerprintPasswordHashSaver
|
||||||
|
import sushi.hardcore.droidfs.util.FilesUtils
|
||||||
|
import sushi.hardcore.droidfs.util.GocryptfsVolume
|
||||||
|
import sushi.hardcore.droidfs.util.WidgetUtil
|
||||||
|
import sushi.hardcore.droidfs.util.Wiper
|
||||||
|
import sushi.hardcore.droidfs.widgets.ColoredAlertDialog
|
||||||
|
import java.io.File
|
||||||
|
import java.util.*
|
||||||
|
|
||||||
|
class CreateActivity : ColoredActivity() {
|
||||||
|
companion object {
|
||||||
|
private const val PICK_DIRECTORY_REQUEST_CODE = 1
|
||||||
|
}
|
||||||
|
private lateinit var fingerprintPasswordHashSaver: FingerprintPasswordHashSaver
|
||||||
|
private lateinit var root_cipher_dir: String
|
||||||
|
private var sessionID = -1
|
||||||
|
private var usf_fingerprint = false
|
||||||
|
override fun onCreate(savedInstanceState: Bundle?) {
|
||||||
|
super.onCreate(savedInstanceState)
|
||||||
|
if (!sharedPrefs.getBoolean("usf_screenshot", false)){
|
||||||
|
window.setFlags(WindowManager.LayoutParams.FLAG_SECURE, WindowManager.LayoutParams.FLAG_SECURE)
|
||||||
|
}
|
||||||
|
setContentView(R.layout.activity_create)
|
||||||
|
setSupportActionBar(toolbar)
|
||||||
|
usf_fingerprint = sharedPrefs.getBoolean("usf_fingerprint", false)
|
||||||
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && usf_fingerprint) {
|
||||||
|
fingerprintPasswordHashSaver = FingerprintPasswordHashSaver(this, sharedPrefs)
|
||||||
|
} else {
|
||||||
|
WidgetUtil.hide(checkbox_save_password)
|
||||||
|
}
|
||||||
|
edit_password_confirm.setOnEditorActionListener { v, _, _ ->
|
||||||
|
onClickCreate(v)
|
||||||
|
true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun pick_directory(view: View?) {
|
||||||
|
val i = Intent(Intent.ACTION_OPEN_DOCUMENT_TREE)
|
||||||
|
startActivityForResult(i, PICK_DIRECTORY_REQUEST_CODE)
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
|
||||||
|
super.onActivityResult(requestCode, resultCode, data)
|
||||||
|
if (resultCode == Activity.RESULT_OK) {
|
||||||
|
if (requestCode == PICK_DIRECTORY_REQUEST_CODE) {
|
||||||
|
if (data != null) {
|
||||||
|
val path = FilesUtils.getFullPathFromTreeUri(data.data, this)
|
||||||
|
edit_volume_path.setText(path)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun onClickCreate(view: View?) {
|
||||||
|
val password = edit_password.text.toString().toCharArray()
|
||||||
|
val password_confirm = edit_password_confirm.text.toString().toCharArray()
|
||||||
|
if (!password.contentEquals(password_confirm)) {
|
||||||
|
Toast.makeText(applicationContext, R.string.passwords_mismatch, Toast.LENGTH_SHORT).show()
|
||||||
|
} else {
|
||||||
|
root_cipher_dir = edit_volume_path.text.toString()
|
||||||
|
val volume_path_file = File(root_cipher_dir)
|
||||||
|
var good_directory = false
|
||||||
|
if (!volume_path_file.isDirectory) {
|
||||||
|
if (volume_path_file.mkdirs()) {
|
||||||
|
good_directory = true
|
||||||
|
} else {
|
||||||
|
Toast.makeText(applicationContext, R.string.error_mkdir, Toast.LENGTH_SHORT).show()
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
val dir_content = volume_path_file.list()
|
||||||
|
if (dir_content != null){
|
||||||
|
if (dir_content.isEmpty()) {
|
||||||
|
good_directory = true
|
||||||
|
} else {
|
||||||
|
Toast.makeText(applicationContext, R.string.dir_not_empty, Toast.LENGTH_SHORT).show()
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
Toast.makeText(applicationContext, getString(R.string.listdir_null_error_msg), Toast.LENGTH_SHORT).show()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (good_directory) {
|
||||||
|
if (GocryptfsVolume.create_volume(root_cipher_dir, password, GocryptfsVolume.ScryptDefaultLogN, ConstValues.creator)) {
|
||||||
|
var returnedHash: ByteArray? = null
|
||||||
|
if (usf_fingerprint && checkbox_save_password.isChecked){
|
||||||
|
returnedHash = ByteArray(GocryptfsVolume.KeyLen)
|
||||||
|
}
|
||||||
|
sessionID = GocryptfsVolume.init(root_cipher_dir, password, null, returnedHash)
|
||||||
|
if (sessionID != -1) {
|
||||||
|
var startExplorerImmediately = true
|
||||||
|
if (checkbox_remember_path.isChecked) {
|
||||||
|
val old_saved_volumes_paths = sharedPrefs.getStringSet(ConstValues.saved_volumes_key, HashSet()) as Set<String>
|
||||||
|
val editor = sharedPrefs.edit()
|
||||||
|
val new_saved_volumes_paths = old_saved_volumes_paths.toMutableList()
|
||||||
|
if (old_saved_volumes_paths.contains(root_cipher_dir)) {
|
||||||
|
if (sharedPrefs.getString(root_cipher_dir, null) != null){
|
||||||
|
editor.remove(root_cipher_dir)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
new_saved_volumes_paths.add(root_cipher_dir)
|
||||||
|
editor.putStringSet(ConstValues.saved_volumes_key, new_saved_volumes_paths.toSet())
|
||||||
|
}
|
||||||
|
editor.apply()
|
||||||
|
if (checkbox_save_password.isChecked && returnedHash != null){
|
||||||
|
fingerprintPasswordHashSaver.encryptAndSave(returnedHash, root_cipher_dir){ _ ->
|
||||||
|
startExplorer()
|
||||||
|
}
|
||||||
|
startExplorerImmediately = false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (startExplorerImmediately){
|
||||||
|
startExplorer()
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
Toast.makeText(this, R.string.open_volume_failed, Toast.LENGTH_SHORT).show()
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
ColoredAlertDialog(this)
|
||||||
|
.setTitle(R.string.error)
|
||||||
|
.setMessage(R.string.create_volume_failed)
|
||||||
|
.setPositiveButton(R.string.ok, null)
|
||||||
|
.show()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Arrays.fill(password, 0.toChar())
|
||||||
|
Arrays.fill(password_confirm, 0.toChar())
|
||||||
|
}
|
||||||
|
|
||||||
|
fun startExplorer(){
|
||||||
|
ColoredAlertDialog(this)
|
||||||
|
.setTitle(R.string.success_volume_create)
|
||||||
|
.setMessage(R.string.success_volume_create_msg)
|
||||||
|
.setCancelable(false)
|
||||||
|
.setPositiveButton(R.string.ok) { _, _ ->
|
||||||
|
val intent = Intent(applicationContext, ExplorerActivity::class.java)
|
||||||
|
intent.putExtra("sessionID", sessionID)
|
||||||
|
intent.putExtra("volume_name", File(root_cipher_dir).name)
|
||||||
|
startActivity(intent)
|
||||||
|
finish()
|
||||||
|
}
|
||||||
|
.show()
|
||||||
|
}
|
||||||
|
|
||||||
|
fun onClickSavePasswordHash(view: View) {
|
||||||
|
if (checkbox_save_password.isChecked){
|
||||||
|
if (!fingerprintPasswordHashSaver.canAuthenticate()){
|
||||||
|
checkbox_save_password.isChecked = false
|
||||||
|
} else {
|
||||||
|
checkbox_remember_path.isChecked = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun onClickRememberPath(view: View) {
|
||||||
|
if (!checkbox_remember_path.isChecked){
|
||||||
|
checkbox_save_password.isChecked = false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onPause() {
|
||||||
|
super.onPause()
|
||||||
|
if (::fingerprintPasswordHashSaver.isInitialized && fingerprintPasswordHashSaver.isListening){
|
||||||
|
fingerprintPasswordHashSaver.stopListening()
|
||||||
|
if (fingerprintPasswordHashSaver.fingerprintFragment.isAdded){
|
||||||
|
fingerprintPasswordHashSaver.fingerprintFragment.dismiss()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onDestroy() {
|
||||||
|
super.onDestroy()
|
||||||
|
Wiper.wipeEditText(edit_password)
|
||||||
|
Wiper.wipeEditText(edit_password_confirm)
|
||||||
|
}
|
||||||
|
}
|
110
app/src/main/java/sushi/hardcore/droidfs/MainActivity.kt
Normal file
110
app/src/main/java/sushi/hardcore/droidfs/MainActivity.kt
Normal file
@ -0,0 +1,110 @@
|
|||||||
|
package sushi.hardcore.droidfs
|
||||||
|
|
||||||
|
import android.Manifest
|
||||||
|
import android.content.Intent
|
||||||
|
import android.content.pm.PackageManager
|
||||||
|
import android.os.Build
|
||||||
|
import android.os.Bundle
|
||||||
|
import android.os.Environment
|
||||||
|
import android.util.DisplayMetrics
|
||||||
|
import android.util.Log
|
||||||
|
import android.view.*
|
||||||
|
import androidx.appcompat.app.AppCompatActivity
|
||||||
|
import androidx.core.content.ContextCompat
|
||||||
|
import androidx.preference.PreferenceManager
|
||||||
|
import kotlinx.android.synthetic.main.activity_main.*
|
||||||
|
import kotlinx.android.synthetic.main.toolbar.*
|
||||||
|
import sushi.hardcore.droidfs.widgets.ColoredAlertDialog
|
||||||
|
|
||||||
|
class MainActivity : AppCompatActivity() {
|
||||||
|
companion object {
|
||||||
|
private const val STORAGE_PERMISSIONS_REQUEST = 1
|
||||||
|
}
|
||||||
|
override fun onCreate(savedInstanceState: Bundle?) {
|
||||||
|
super.onCreate(savedInstanceState)
|
||||||
|
val sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this)
|
||||||
|
if (!sharedPrefs.getBoolean("usf_screenshot", false)){
|
||||||
|
window.setFlags(WindowManager.LayoutParams.FLAG_SECURE, WindowManager.LayoutParams.FLAG_SECURE)
|
||||||
|
}
|
||||||
|
setContentView(R.layout.activity_main)
|
||||||
|
setSupportActionBar(toolbar)
|
||||||
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
|
||||||
|
if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) +
|
||||||
|
ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)
|
||||||
|
!= PackageManager.PERMISSION_GRANTED) {
|
||||||
|
requestPermissions(arrayOf(Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE), STORAGE_PERMISSIONS_REQUEST)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
val state = Environment.getExternalStorageState()
|
||||||
|
val storageAvailable = Environment.MEDIA_MOUNTED == state || Environment.MEDIA_MOUNTED_READ_ONLY == state
|
||||||
|
if (!storageAvailable) {
|
||||||
|
ColoredAlertDialog(this)
|
||||||
|
.setTitle(R.string.storage_unavailable)
|
||||||
|
.setMessage(getString(R.string.storage_unavailable_msg))
|
||||||
|
.setPositiveButton(R.string.ok
|
||||||
|
) { _, _ -> finish() }.show()
|
||||||
|
}
|
||||||
|
if (!sharedPrefs.getBoolean("alreadyLaunched", false)){
|
||||||
|
ColoredAlertDialog(this)
|
||||||
|
.setTitle(R.string.warning)
|
||||||
|
.setMessage(getString(R.string.usf_home_warning_msg))
|
||||||
|
.setCancelable(false)
|
||||||
|
.setPositiveButton(getString(R.string.see_unsafe_features)){ _, _ ->
|
||||||
|
val intent = Intent(this, SettingsActivity::class.java)
|
||||||
|
intent.putExtra("screen", "UnsafeFeaturesSettingsFragment")
|
||||||
|
startActivity(intent)
|
||||||
|
}
|
||||||
|
.setNegativeButton(R.string.ok, null)
|
||||||
|
.setOnDismissListener { sharedPrefs.edit().putBoolean("alreadyLaunched", true).apply() }
|
||||||
|
.show()
|
||||||
|
}
|
||||||
|
val metrics = DisplayMetrics()
|
||||||
|
windowManager.defaultDisplay.getMetrics(metrics)
|
||||||
|
image_logo.layoutParams.height = (metrics.heightPixels/2.2).toInt()
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<String>, grantResults: IntArray) {
|
||||||
|
when (requestCode) {
|
||||||
|
STORAGE_PERMISSIONS_REQUEST -> if (grantResults.size == 2) {
|
||||||
|
if (grantResults[0] != PackageManager.PERMISSION_GRANTED || grantResults[1] != PackageManager.PERMISSION_GRANTED) {
|
||||||
|
ColoredAlertDialog(this)
|
||||||
|
.setTitle(R.string.storage_perm_denied)
|
||||||
|
.setMessage(getString(R.string.storage_perm_denied_msg))
|
||||||
|
.setPositiveButton(R.string.ok
|
||||||
|
) { _, _ -> finish() }.show()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onOptionsItemSelected(item: MenuItem): Boolean {
|
||||||
|
return when (item.itemId) {
|
||||||
|
R.id.menu_settings -> {
|
||||||
|
val intent = Intent(this, SettingsActivity::class.java)
|
||||||
|
startActivity(intent)
|
||||||
|
true
|
||||||
|
}
|
||||||
|
else -> super.onOptionsItemSelected(item)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onCreateOptionsMenu(menu: Menu): Boolean {
|
||||||
|
menuInflater.inflate(R.menu.main_activity, menu)
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
fun onClickCreate(v: View?) {
|
||||||
|
val intent = Intent(this, CreateActivity::class.java)
|
||||||
|
startActivity(intent)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun onClickOpen(v: View?) {
|
||||||
|
val intent = Intent(this, OpenActivity::class.java)
|
||||||
|
startActivity(intent)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun onClickChangePassword(v: View?) {
|
||||||
|
val intent = Intent(this, ChangePasswordActivity::class.java)
|
||||||
|
startActivity(intent)
|
||||||
|
}
|
||||||
|
}
|
192
app/src/main/java/sushi/hardcore/droidfs/OpenActivity.kt
Normal file
192
app/src/main/java/sushi/hardcore/droidfs/OpenActivity.kt
Normal file
@ -0,0 +1,192 @@
|
|||||||
|
package sushi.hardcore.droidfs
|
||||||
|
|
||||||
|
import android.app.Activity
|
||||||
|
import android.content.Intent
|
||||||
|
import android.os.Build
|
||||||
|
import android.os.Bundle
|
||||||
|
import android.view.View
|
||||||
|
import android.widget.AdapterView.OnItemClickListener
|
||||||
|
import android.widget.Toast
|
||||||
|
import kotlinx.android.synthetic.main.activity_change_password.*
|
||||||
|
import kotlinx.android.synthetic.main.activity_open.*
|
||||||
|
import kotlinx.android.synthetic.main.activity_open.checkbox_remember_path
|
||||||
|
import kotlinx.android.synthetic.main.activity_open.checkbox_save_password
|
||||||
|
import kotlinx.android.synthetic.main.activity_open.edit_password
|
||||||
|
import kotlinx.android.synthetic.main.activity_open.edit_volume_path
|
||||||
|
import kotlinx.android.synthetic.main.activity_open.saved_path_listview
|
||||||
|
import kotlinx.android.synthetic.main.toolbar.*
|
||||||
|
import sushi.hardcore.droidfs.adapters.SavedVolumesAdapter
|
||||||
|
import sushi.hardcore.droidfs.explorers.ExplorerActivity
|
||||||
|
import sushi.hardcore.droidfs.explorers.ExplorerActivityDrop
|
||||||
|
import sushi.hardcore.droidfs.explorers.ExplorerActivityPick
|
||||||
|
import sushi.hardcore.droidfs.fingerprint_stuff.FingerprintPasswordHashSaver
|
||||||
|
import sushi.hardcore.droidfs.util.FilesUtils
|
||||||
|
import sushi.hardcore.droidfs.util.GocryptfsVolume
|
||||||
|
import sushi.hardcore.droidfs.util.WidgetUtil
|
||||||
|
import sushi.hardcore.droidfs.util.Wiper
|
||||||
|
import sushi.hardcore.droidfs.widgets.ColoredAlertDialog
|
||||||
|
import java.io.File
|
||||||
|
import java.util.*
|
||||||
|
|
||||||
|
class OpenActivity : ColoredActivity() {
|
||||||
|
companion object {
|
||||||
|
private const val PICK_DIRECTORY_REQUEST_CODE = 1
|
||||||
|
}
|
||||||
|
private lateinit var savedVolumesAdapter: SavedVolumesAdapter
|
||||||
|
private lateinit var fingerprintPasswordHashSaver: FingerprintPasswordHashSaver
|
||||||
|
private lateinit var root_cipher_dir: String
|
||||||
|
private var sessionID = -1
|
||||||
|
private var usf_fingerprint = false
|
||||||
|
override fun onCreate(savedInstanceState: Bundle?) {
|
||||||
|
super.onCreate(savedInstanceState)
|
||||||
|
setContentView(R.layout.activity_open)
|
||||||
|
setSupportActionBar(toolbar)
|
||||||
|
//val sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this)
|
||||||
|
usf_fingerprint = sharedPrefs.getBoolean("usf_fingerprint", false)
|
||||||
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && usf_fingerprint) {
|
||||||
|
fingerprintPasswordHashSaver = FingerprintPasswordHashSaver(this, sharedPrefs)
|
||||||
|
} else {
|
||||||
|
WidgetUtil.hide(checkbox_save_password)
|
||||||
|
}
|
||||||
|
savedVolumesAdapter = SavedVolumesAdapter(this, sharedPrefs)
|
||||||
|
if (savedVolumesAdapter.count > 0){
|
||||||
|
saved_path_listview.adapter = savedVolumesAdapter
|
||||||
|
saved_path_listview.onItemClickListener = OnItemClickListener { _, _, position, _ ->
|
||||||
|
root_cipher_dir = savedVolumesAdapter.getItem(position)
|
||||||
|
edit_volume_path.setText(root_cipher_dir)
|
||||||
|
val cipherText = sharedPrefs.getString(root_cipher_dir, null)
|
||||||
|
if (cipherText != null){ //password hash saved
|
||||||
|
fingerprintPasswordHashSaver.decrypt(cipherText, root_cipher_dir, ::openUsingPasswordHash)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
WidgetUtil.hide(saved_path_listview)
|
||||||
|
}
|
||||||
|
edit_password.setOnEditorActionListener { v, _, _ ->
|
||||||
|
onClickOpen(v)
|
||||||
|
true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun pick_directory(view: View?) {
|
||||||
|
val i = Intent(Intent.ACTION_OPEN_DOCUMENT_TREE)
|
||||||
|
startActivityForResult(i, PICK_DIRECTORY_REQUEST_CODE)
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
|
||||||
|
super.onActivityResult(requestCode, resultCode, data)
|
||||||
|
if (resultCode == Activity.RESULT_OK) {
|
||||||
|
if (requestCode == PICK_DIRECTORY_REQUEST_CODE) {
|
||||||
|
if (data != null) {
|
||||||
|
val path = FilesUtils.getFullPathFromTreeUri(data.data, this)
|
||||||
|
edit_volume_path.setText(path)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun onClickOpen(view: View?) {
|
||||||
|
root_cipher_dir = edit_volume_path.text.toString() //fresh get in case of manual rewrite
|
||||||
|
if (root_cipher_dir.isEmpty()) {
|
||||||
|
Toast.makeText(this, R.string.enter_volume_path, Toast.LENGTH_SHORT).show()
|
||||||
|
} else {
|
||||||
|
val password = edit_password.text.toString().toCharArray()
|
||||||
|
var returnedHash: ByteArray? = null
|
||||||
|
if (usf_fingerprint && checkbox_save_password.isChecked){
|
||||||
|
returnedHash = ByteArray(GocryptfsVolume.KeyLen)
|
||||||
|
}
|
||||||
|
sessionID = GocryptfsVolume.init(root_cipher_dir, password, null, returnedHash)
|
||||||
|
if (sessionID != -1) {
|
||||||
|
var startExplorerImmediately = true
|
||||||
|
if (checkbox_remember_path.isChecked) {
|
||||||
|
savedVolumesAdapter.addVolumePath(root_cipher_dir)
|
||||||
|
if (checkbox_save_password.isChecked && returnedHash != null){
|
||||||
|
fingerprintPasswordHashSaver.encryptAndSave(returnedHash, root_cipher_dir) {success ->
|
||||||
|
if (success){
|
||||||
|
startExplorer()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
startExplorerImmediately = false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (startExplorerImmediately){
|
||||||
|
startExplorer()
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
ColoredAlertDialog(this)
|
||||||
|
.setTitle(R.string.open_volume_failed)
|
||||||
|
.setMessage(R.string.open_volume_failed_msg)
|
||||||
|
.setPositiveButton(R.string.ok, null)
|
||||||
|
.show()
|
||||||
|
}
|
||||||
|
Arrays.fill(password, 0.toChar())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun openUsingPasswordHash(passwordHash: ByteArray){
|
||||||
|
sessionID = GocryptfsVolume.init(root_cipher_dir, null, passwordHash, null)
|
||||||
|
if (sessionID != -1){
|
||||||
|
startExplorer()
|
||||||
|
} else {
|
||||||
|
ColoredAlertDialog(this)
|
||||||
|
.setTitle(R.string.open_volume_failed)
|
||||||
|
.setMessage(getString(R.string.open_failed_hash_msg))
|
||||||
|
.setPositiveButton(R.string.ok, null)
|
||||||
|
.show()
|
||||||
|
}
|
||||||
|
Arrays.fill(passwordHash, 0)
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun startExplorer() {
|
||||||
|
var explorer_intent: Intent? = null
|
||||||
|
val current_intent_action = intent.action
|
||||||
|
if (current_intent_action != null) {
|
||||||
|
if ((current_intent_action == Intent.ACTION_SEND || current_intent_action == Intent.ACTION_SEND_MULTIPLE) && intent.extras != null) { //import via android share menu
|
||||||
|
explorer_intent = Intent(this, ExplorerActivityDrop::class.java)
|
||||||
|
explorer_intent.action = current_intent_action //forward action
|
||||||
|
explorer_intent.putExtras(intent.extras!!) //forward extras
|
||||||
|
} else if (current_intent_action == "pick") { //pick items to import
|
||||||
|
explorer_intent = Intent(this, ExplorerActivityPick::class.java)
|
||||||
|
explorer_intent.flags = Intent.FLAG_ACTIVITY_FORWARD_RESULT
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (explorer_intent == null) {
|
||||||
|
explorer_intent = Intent(this, ExplorerActivity::class.java) //default opening
|
||||||
|
}
|
||||||
|
explorer_intent.putExtra("sessionID", sessionID)
|
||||||
|
explorer_intent.putExtra("volume_name", File(root_cipher_dir).name)
|
||||||
|
startActivity(explorer_intent)
|
||||||
|
finish()
|
||||||
|
}
|
||||||
|
|
||||||
|
fun onClickSavePasswordHash(view: View) {
|
||||||
|
if (checkbox_save_password.isChecked){
|
||||||
|
if (!fingerprintPasswordHashSaver.canAuthenticate()){
|
||||||
|
checkbox_save_password.isChecked = false
|
||||||
|
} else {
|
||||||
|
checkbox_remember_path.isChecked = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun onClickRememberPath(view: View) {
|
||||||
|
if (!checkbox_remember_path.isChecked){
|
||||||
|
checkbox_save_password.isChecked = false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onPause() {
|
||||||
|
super.onPause()
|
||||||
|
if (::fingerprintPasswordHashSaver.isInitialized && fingerprintPasswordHashSaver.isListening){
|
||||||
|
fingerprintPasswordHashSaver.stopListening()
|
||||||
|
if (fingerprintPasswordHashSaver.fingerprintFragment.isAdded){
|
||||||
|
fingerprintPasswordHashSaver.fingerprintFragment.dismiss()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onDestroy() {
|
||||||
|
super.onDestroy()
|
||||||
|
Wiper.wipeEditText(edit_password)
|
||||||
|
}
|
||||||
|
}
|
52
app/src/main/java/sushi/hardcore/droidfs/SettingsActivity.kt
Normal file
52
app/src/main/java/sushi/hardcore/droidfs/SettingsActivity.kt
Normal file
@ -0,0 +1,52 @@
|
|||||||
|
package sushi.hardcore.droidfs
|
||||||
|
|
||||||
|
import android.os.Bundle
|
||||||
|
import android.view.WindowManager
|
||||||
|
import androidx.fragment.app.Fragment
|
||||||
|
import androidx.preference.Preference
|
||||||
|
import androidx.preference.PreferenceFragmentCompat
|
||||||
|
import kotlinx.android.synthetic.main.toolbar.*
|
||||||
|
|
||||||
|
class SettingsActivity : ColoredActivity(), PreferenceFragmentCompat.OnPreferenceStartFragmentCallback {
|
||||||
|
|
||||||
|
override fun onCreate(savedInstanceState: Bundle?) {
|
||||||
|
super.onCreate(savedInstanceState)
|
||||||
|
if (!sharedPrefs.getBoolean("usf_screenshot", false)){
|
||||||
|
window.setFlags(WindowManager.LayoutParams.FLAG_SECURE, WindowManager.LayoutParams.FLAG_SECURE)
|
||||||
|
}
|
||||||
|
setContentView(R.layout.activity_settings)
|
||||||
|
setSupportActionBar(toolbar)
|
||||||
|
supportActionBar?.setDisplayHomeAsUpEnabled(true)
|
||||||
|
val screen = intent.extras?.getString("screen") ?: "main"
|
||||||
|
val fragment: Fragment
|
||||||
|
fragment = if (screen == "UnsafeFeaturesSettingsFragment") {
|
||||||
|
UnsafeFeaturesSettingsFragment()
|
||||||
|
} else {
|
||||||
|
SettingsFragment()
|
||||||
|
}
|
||||||
|
supportFragmentManager
|
||||||
|
.beginTransaction()
|
||||||
|
.replace(R.id.settings, fragment)
|
||||||
|
.commit()
|
||||||
|
}
|
||||||
|
|
||||||
|
class SettingsFragment : PreferenceFragmentCompat() {
|
||||||
|
override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
|
||||||
|
setPreferencesFromResource(R.xml.root_preferences, rootKey)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class UnsafeFeaturesSettingsFragment : PreferenceFragmentCompat() {
|
||||||
|
override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
|
||||||
|
setPreferencesFromResource(R.xml.unsafe_features_preferences, rootKey)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onPreferenceStartFragment(caller: PreferenceFragmentCompat, pref: Preference): Boolean {
|
||||||
|
val fragment = supportFragmentManager.fragmentFactory.instantiate(classLoader, pref.fragment)
|
||||||
|
fragment.arguments = pref.extras
|
||||||
|
fragment.setTargetFragment(caller, 0)
|
||||||
|
supportFragmentManager.beginTransaction().replace(R.id.settings, fragment).addToBackStack(null).commit()
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,128 @@
|
|||||||
|
package sushi.hardcore.droidfs.adapters
|
||||||
|
|
||||||
|
import android.content.Context
|
||||||
|
import android.graphics.Color
|
||||||
|
import android.graphics.PorterDuff
|
||||||
|
import android.graphics.PorterDuffColorFilter
|
||||||
|
import android.view.LayoutInflater
|
||||||
|
import android.view.View
|
||||||
|
import android.view.ViewGroup
|
||||||
|
import android.widget.BaseAdapter
|
||||||
|
import android.widget.ImageView
|
||||||
|
import android.widget.TextView
|
||||||
|
import androidx.core.content.ContextCompat
|
||||||
|
import sushi.hardcore.droidfs.ConstValues.Companion.getAssociatedDrawable
|
||||||
|
import sushi.hardcore.droidfs.explorers.ExplorerElement
|
||||||
|
import sushi.hardcore.droidfs.R
|
||||||
|
import sushi.hardcore.droidfs.util.FilesUtils
|
||||||
|
import sushi.hardcore.droidfs.widgets.ThemeColor
|
||||||
|
import java.text.DateFormat
|
||||||
|
import java.util.*
|
||||||
|
|
||||||
|
class ExplorerElementAdapter(private val context: Context) : BaseAdapter() {
|
||||||
|
private val dateFormat: DateFormat = DateFormat.getDateTimeInstance(DateFormat.DEFAULT, DateFormat.DEFAULT, context.resources.configuration.locale)
|
||||||
|
private lateinit var explorer_elements: List<ExplorerElement>
|
||||||
|
private val inflater: LayoutInflater = LayoutInflater.from(context)
|
||||||
|
val selectedItems: MutableList<Int> = ArrayList()
|
||||||
|
private val themeColor = ThemeColor.getThemeColor(context)
|
||||||
|
override fun getCount(): Int {
|
||||||
|
return explorer_elements.size
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun getItem(position: Int): ExplorerElement {
|
||||||
|
return explorer_elements[position]
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun getItemId(position: Int): Long {
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
|
||||||
|
val view: View = convertView ?: inflater.inflate(R.layout.adapter_explorer_element, parent, false)
|
||||||
|
val currentElement = getItem(position)
|
||||||
|
val textElementName = view.findViewById<TextView>(R.id.text_element_name)
|
||||||
|
textElementName.text = currentElement.name
|
||||||
|
val textElementMtime = view.findViewById<TextView>(R.id.text_element_mtime)
|
||||||
|
val textElementSize = view.findViewById<TextView>(R.id.text_element_size)
|
||||||
|
textElementSize.text = ""
|
||||||
|
var drawableId = R.drawable.icon_folder
|
||||||
|
when {
|
||||||
|
currentElement.isDirectory -> {
|
||||||
|
textElementMtime.text = dateFormat.format(currentElement.mTime)
|
||||||
|
}
|
||||||
|
currentElement.isParentFolder -> {
|
||||||
|
textElementMtime.setText(R.string.parent_folder)
|
||||||
|
}
|
||||||
|
else -> {
|
||||||
|
textElementMtime.text = dateFormat.format(currentElement.mTime)
|
||||||
|
textElementSize.text = FilesUtils.formatSize(currentElement.size)
|
||||||
|
drawableId = getAssociatedDrawable(currentElement.name)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
val elementIcon = view.findViewById<ImageView>(R.id.icon_element)
|
||||||
|
val icon = context.getDrawable(drawableId)
|
||||||
|
icon?.colorFilter = PorterDuffColorFilter(themeColor, PorterDuff.Mode.SRC_IN)
|
||||||
|
elementIcon.setImageDrawable(icon)
|
||||||
|
if (selectedItems.contains(position)) {
|
||||||
|
view.setBackgroundColor(ContextCompat.getColor(context, R.color.item_selected))
|
||||||
|
} else {
|
||||||
|
view.setBackgroundColor(Color.alpha(0))
|
||||||
|
}
|
||||||
|
return view
|
||||||
|
}
|
||||||
|
|
||||||
|
fun onItemClick(position: Int) {
|
||||||
|
if (selectedItems.isNotEmpty()) {
|
||||||
|
if (!explorer_elements[position].isParentFolder) {
|
||||||
|
if (selectedItems.contains(position)) {
|
||||||
|
selectedItems.remove(position)
|
||||||
|
} else {
|
||||||
|
selectedItems.add(position)
|
||||||
|
}
|
||||||
|
notifyDataSetInvalidated()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun onItemLongClick(position: Int) {
|
||||||
|
if (!explorer_elements[position].isParentFolder) {
|
||||||
|
if (!selectedItems.contains(position)) {
|
||||||
|
selectedItems.add(position)
|
||||||
|
} else {
|
||||||
|
selectedItems.remove(position)
|
||||||
|
}
|
||||||
|
notifyDataSetInvalidated()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun selectAll() {
|
||||||
|
for (i in explorer_elements.indices) {
|
||||||
|
if (!selectedItems.contains(i) && !explorer_elements[i].isParentFolder) {
|
||||||
|
selectedItems.add(i)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
notifyDataSetInvalidated()
|
||||||
|
}
|
||||||
|
|
||||||
|
fun unSelectAll() {
|
||||||
|
selectedItems.clear()
|
||||||
|
notifyDataSetInvalidated()
|
||||||
|
}
|
||||||
|
|
||||||
|
fun setExplorerElements(explorer_elements: List<ExplorerElement>) {
|
||||||
|
unSelectAll()
|
||||||
|
this.explorer_elements = explorer_elements
|
||||||
|
}
|
||||||
|
|
||||||
|
val currentDirectoryTotalSize: Long
|
||||||
|
get() {
|
||||||
|
var total_size: Long = 0
|
||||||
|
for (e in explorer_elements) {
|
||||||
|
if (e.isRegularFile) {
|
||||||
|
total_size += e.size
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return total_size
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,36 @@
|
|||||||
|
package sushi.hardcore.droidfs.adapters
|
||||||
|
|
||||||
|
import android.content.Context
|
||||||
|
import android.view.LayoutInflater
|
||||||
|
import android.view.View
|
||||||
|
import android.view.ViewGroup
|
||||||
|
import android.widget.BaseAdapter
|
||||||
|
import android.widget.TextView
|
||||||
|
import sushi.hardcore.droidfs.R
|
||||||
|
import sushi.hardcore.droidfs.widgets.ColoredImageView
|
||||||
|
|
||||||
|
class OpenAsDialogAdapter(private val context: Context): BaseAdapter() {
|
||||||
|
private val inflater: LayoutInflater = LayoutInflater.from(context)
|
||||||
|
private val items = listOf(
|
||||||
|
listOf("image", context.getString(R.string.image), R.drawable.icon_file_image),
|
||||||
|
listOf("video", context.getString(R.string.video), R.drawable.icon_file_video),
|
||||||
|
listOf("audio", context.getString(R.string.audio), R.drawable.icon_file_audio),
|
||||||
|
listOf("text", context.getString(R.string.text), R.drawable.icon_file_text)
|
||||||
|
)
|
||||||
|
override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View {
|
||||||
|
val view: View = convertView ?: inflater.inflate(R.layout.adapter_dialog_listview, parent, false)
|
||||||
|
val text = view.findViewById<TextView>(R.id.text)
|
||||||
|
text.text = items[position][1] as String
|
||||||
|
val icon = view.findViewById<ColoredImageView>(R.id.icon)
|
||||||
|
icon.setImageDrawable(context.getDrawable(items[position][2] as Int))
|
||||||
|
return view
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun getItem(position: Int): String {
|
||||||
|
return items[position][0] as String
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun getItemId(position: Int): Long { return 0 }
|
||||||
|
|
||||||
|
override fun getCount(): Int { return items.size }
|
||||||
|
}
|
@ -0,0 +1,100 @@
|
|||||||
|
package sushi.hardcore.droidfs.adapters
|
||||||
|
|
||||||
|
import androidx.appcompat.app.AlertDialog
|
||||||
|
import android.content.Context
|
||||||
|
import android.content.SharedPreferences
|
||||||
|
import android.content.SharedPreferences.Editor
|
||||||
|
import android.util.Log
|
||||||
|
import android.view.LayoutInflater
|
||||||
|
import android.view.View
|
||||||
|
import android.view.ViewGroup
|
||||||
|
import android.widget.BaseAdapter
|
||||||
|
import android.widget.ImageView
|
||||||
|
import android.widget.TextView
|
||||||
|
import sushi.hardcore.droidfs.ConstValues
|
||||||
|
import sushi.hardcore.droidfs.R
|
||||||
|
import sushi.hardcore.droidfs.util.WidgetUtil
|
||||||
|
import sushi.hardcore.droidfs.widgets.ColoredAlertDialog
|
||||||
|
import java.util.*
|
||||||
|
|
||||||
|
class SavedVolumesAdapter(val context: Context, val shared_prefs: SharedPreferences) : BaseAdapter() {
|
||||||
|
private val inflater: LayoutInflater = LayoutInflater.from(context)
|
||||||
|
private val saved_volumes_paths: MutableList<String> = ArrayList()
|
||||||
|
private val shared_prefs_editor: Editor = shared_prefs.edit()
|
||||||
|
|
||||||
|
init {
|
||||||
|
val saved_volumes_paths_set = shared_prefs.getStringSet(ConstValues.saved_volumes_key, HashSet()) as Set<String>
|
||||||
|
for (volume_path in saved_volumes_paths_set) {
|
||||||
|
saved_volumes_paths.add(volume_path)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun update_shared_prefs() {
|
||||||
|
val saved_volumes_paths_set = saved_volumes_paths.toSet()
|
||||||
|
shared_prefs_editor.remove(ConstValues.saved_volumes_key)
|
||||||
|
shared_prefs_editor.putStringSet(ConstValues.saved_volumes_key, saved_volumes_paths_set)
|
||||||
|
shared_prefs_editor.apply()
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun getCount(): Int {
|
||||||
|
return saved_volumes_paths.size
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun getItem(position: Int): String {
|
||||||
|
return saved_volumes_paths[position]
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun getItemId(position: Int): Long {
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
|
||||||
|
val view: View = convertView ?: inflater.inflate(R.layout.adapter_saved_volume, parent, false)
|
||||||
|
val volume_name_textview = view.findViewById<TextView>(R.id.volume_name_textview)
|
||||||
|
val currentVolume = getItem(position)
|
||||||
|
volume_name_textview.text = currentVolume
|
||||||
|
val delete_imageview = view.findViewById<ImageView>(R.id.delete_imageview)
|
||||||
|
delete_imageview.setOnClickListener {
|
||||||
|
val volume_path = saved_volumes_paths[position]
|
||||||
|
val dialog = ColoredAlertDialog(context)
|
||||||
|
dialog.setTitle(R.string.warning)
|
||||||
|
if (shared_prefs.getString(volume_path, null) != null){
|
||||||
|
dialog.setMessage(context.getString(R.string.delete_hash_or_all))
|
||||||
|
dialog.setPositiveButton(context.getString(R.string.delete_all)) { _, _ ->
|
||||||
|
saved_volumes_paths.removeAt(position)
|
||||||
|
shared_prefs_editor.remove(volume_path)
|
||||||
|
update_shared_prefs()
|
||||||
|
refresh(parent)
|
||||||
|
}
|
||||||
|
dialog.setNegativeButton(context.getString(R.string.delete_hash)) { _, _ ->
|
||||||
|
shared_prefs_editor.remove(volume_path)
|
||||||
|
shared_prefs_editor.apply()
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
dialog.setMessage(context.getString(R.string.ask_delete_volume_path))
|
||||||
|
dialog.setPositiveButton(R.string.ok) {_, _ ->
|
||||||
|
saved_volumes_paths.removeAt(position)
|
||||||
|
update_shared_prefs()
|
||||||
|
refresh(parent)
|
||||||
|
}
|
||||||
|
dialog.setNegativeButton(R.string.cancel, null)
|
||||||
|
}
|
||||||
|
dialog.show()
|
||||||
|
}
|
||||||
|
return view
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun refresh(parent: ViewGroup) {
|
||||||
|
notifyDataSetChanged()
|
||||||
|
if (count == 0){
|
||||||
|
WidgetUtil.hide(parent)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun addVolumePath(volume_path: String) {
|
||||||
|
if (!saved_volumes_paths.contains(volume_path)) {
|
||||||
|
saved_volumes_paths.add(volume_path)
|
||||||
|
update_shared_prefs()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,369 @@
|
|||||||
|
package sushi.hardcore.droidfs.explorers
|
||||||
|
|
||||||
|
import android.app.Activity
|
||||||
|
import android.content.Intent
|
||||||
|
import android.net.Uri
|
||||||
|
import android.view.Menu
|
||||||
|
import android.view.MenuItem
|
||||||
|
import android.view.View
|
||||||
|
import kotlinx.android.synthetic.main.activity_explorer.*
|
||||||
|
import sushi.hardcore.droidfs.OpenActivity
|
||||||
|
import sushi.hardcore.droidfs.R
|
||||||
|
import sushi.hardcore.droidfs.util.ExternalProvider
|
||||||
|
import sushi.hardcore.droidfs.util.FilesUtils
|
||||||
|
import sushi.hardcore.droidfs.util.GocryptfsVolume
|
||||||
|
import sushi.hardcore.droidfs.util.Wiper
|
||||||
|
import sushi.hardcore.droidfs.widgets.ColoredAlertDialog
|
||||||
|
import java.io.File
|
||||||
|
import java.util.*
|
||||||
|
|
||||||
|
class ExplorerActivity : ExplorerActivityRO() {
|
||||||
|
private val PICK_DIRECTORY_REQUEST_CODE = 1
|
||||||
|
private val PICK_FILES_REQUEST_CODE = 2
|
||||||
|
private val PICK_OTHER_VOLUME_ITEMS_REQUEST_CODE = 3
|
||||||
|
private var usf_decrypt = false
|
||||||
|
private var usf_share = false
|
||||||
|
override fun init() {
|
||||||
|
setContentView(R.layout.activity_explorer)
|
||||||
|
usf_decrypt = sharedPrefs.getBoolean("usf_decrypt", false)
|
||||||
|
usf_share = sharedPrefs.getBoolean("usf_share", false)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun onClickAddFile(view: View?) {
|
||||||
|
fam_explorer.close(true)
|
||||||
|
val i = Intent(Intent.ACTION_OPEN_DOCUMENT)
|
||||||
|
i.type = "*/*"
|
||||||
|
i.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true)
|
||||||
|
i.addCategory(Intent.CATEGORY_OPENABLE)
|
||||||
|
startActivityForResult(i, PICK_FILES_REQUEST_CODE)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun onClickAddFileFromOtherVolume(view: View?) {
|
||||||
|
fam_explorer.close(true)
|
||||||
|
val intent = Intent(this, OpenActivity::class.java)
|
||||||
|
intent.action = "pick"
|
||||||
|
startActivityForResult(intent, PICK_OTHER_VOLUME_ITEMS_REQUEST_CODE)
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
|
||||||
|
super.onActivityResult(requestCode, resultCode, data)
|
||||||
|
if (requestCode == PICK_FILES_REQUEST_CODE) {
|
||||||
|
if (resultCode == Activity.RESULT_OK && data != null) {
|
||||||
|
val uris: MutableList<Uri> = ArrayList()
|
||||||
|
val single_uri = data.data
|
||||||
|
if (single_uri == null) { //multiples choices
|
||||||
|
val clipdata = data.clipData
|
||||||
|
if (clipdata != null){
|
||||||
|
for (i in 0 until clipdata.itemCount) {
|
||||||
|
uris.add(clipdata.getItemAt(i).uri)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
uris.add(single_uri)
|
||||||
|
}
|
||||||
|
if (uris.isNotEmpty()){
|
||||||
|
var success = true
|
||||||
|
for (uri in uris) {
|
||||||
|
val dst_path = FilesUtils.path_join(current_path, FilesUtils.getFilenameFromURI(this, uri))
|
||||||
|
var `is` = contentResolver.openInputStream(uri)
|
||||||
|
if (`is` != null) {
|
||||||
|
success = gocryptfsVolume.import_file(`is`, dst_path)
|
||||||
|
}
|
||||||
|
if (!success) {
|
||||||
|
ColoredAlertDialog(this)
|
||||||
|
.setTitle(R.string.error)
|
||||||
|
.setMessage(getString(R.string.import_failed, uri))
|
||||||
|
.setPositiveButton(R.string.ok, null)
|
||||||
|
.show()
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (success) {
|
||||||
|
ColoredAlertDialog(this)
|
||||||
|
.setTitle(R.string.success_import)
|
||||||
|
.setMessage("""
|
||||||
|
${getString(R.string.success_import_msg)}
|
||||||
|
${getString(R.string.ask_for_wipe)}
|
||||||
|
""".trimIndent())
|
||||||
|
.setPositiveButton(R.string.yes) { _, _ ->
|
||||||
|
success = true
|
||||||
|
for (uri in uris) {
|
||||||
|
if (!Wiper.wipe(this, uri)) {
|
||||||
|
ColoredAlertDialog(this)
|
||||||
|
.setTitle(R.string.error)
|
||||||
|
.setMessage(getString(R.string.wipe_failed, uri))
|
||||||
|
.setPositiveButton(R.string.ok, null)
|
||||||
|
.show()
|
||||||
|
success = false
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (success) {
|
||||||
|
ColoredAlertDialog(this)
|
||||||
|
.setTitle(R.string.wipe_successful)
|
||||||
|
.setMessage(R.string.wipe_success_msg)
|
||||||
|
.setPositiveButton(R.string.ok, null)
|
||||||
|
.show()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
.setNegativeButton(getString(R.string.no), null)
|
||||||
|
.show()
|
||||||
|
}
|
||||||
|
setCurrentPath(current_path)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else if (requestCode == PICK_DIRECTORY_REQUEST_CODE) {
|
||||||
|
if (resultCode == Activity.RESULT_OK && data != null) {
|
||||||
|
val uri = data.data
|
||||||
|
val output_dir = FilesUtils.getFullPathFromTreeUri(uri, this)
|
||||||
|
var failed_item: String? = null
|
||||||
|
for (i in explorer_adapter.selectedItems) {
|
||||||
|
val element = explorer_adapter.getItem(i)
|
||||||
|
val full_path = FilesUtils.path_join(current_path, element.name)
|
||||||
|
failed_item = if (element.isDirectory) {
|
||||||
|
recursive_export_directory(full_path, output_dir)
|
||||||
|
} else {
|
||||||
|
if (gocryptfsVolume.export_file(full_path, FilesUtils.path_join(output_dir, element.name))) null else full_path
|
||||||
|
}
|
||||||
|
if (failed_item != null) {
|
||||||
|
ColoredAlertDialog(this)
|
||||||
|
.setTitle(R.string.error)
|
||||||
|
.setMessage(getString(R.string.export_failed, failed_item))
|
||||||
|
.setPositiveButton(R.string.ok, null)
|
||||||
|
.show()
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (failed_item == null) {
|
||||||
|
ColoredAlertDialog(this)
|
||||||
|
.setTitle(R.string.success_export)
|
||||||
|
.setMessage(R.string.success_export_msg)
|
||||||
|
.setPositiveButton(R.string.ok, null)
|
||||||
|
.show()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
explorer_adapter.unSelectAll()
|
||||||
|
invalidateOptionsMenu()
|
||||||
|
} else if (requestCode == PICK_OTHER_VOLUME_ITEMS_REQUEST_CODE) {
|
||||||
|
if (resultCode == Activity.RESULT_OK && data != null) {
|
||||||
|
val remote_sessionID = data.getIntExtra("sessionID", -1)
|
||||||
|
val remote_gocryptfsVolume = GocryptfsVolume(remote_sessionID)
|
||||||
|
val path = data.getStringExtra("path")
|
||||||
|
var failed_item: String? = null
|
||||||
|
if (path == null) {
|
||||||
|
val paths = data.getStringArrayListExtra("paths")
|
||||||
|
val types = data.getIntegerArrayListExtra("types")
|
||||||
|
if (types != null && paths != null){
|
||||||
|
for (i in paths.indices) {
|
||||||
|
failed_item = if (types[i] == 0) { //directory
|
||||||
|
recursive_import_directory_from_other_volume(remote_gocryptfsVolume, paths[i], current_path)
|
||||||
|
} else {
|
||||||
|
if (import_file_from_other_volume(remote_gocryptfsVolume, paths[i], current_path)) null else paths[i]
|
||||||
|
}
|
||||||
|
if (failed_item != null) {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
failed_item = if (import_file_from_other_volume(remote_gocryptfsVolume, path, current_path)) null else path
|
||||||
|
}
|
||||||
|
if (failed_item == null) {
|
||||||
|
ColoredAlertDialog(this)
|
||||||
|
.setTitle(R.string.success_import)
|
||||||
|
.setMessage(R.string.success_import_msg)
|
||||||
|
.setPositiveButton(R.string.ok, null)
|
||||||
|
.show()
|
||||||
|
} else {
|
||||||
|
ColoredAlertDialog(this)
|
||||||
|
.setTitle(R.string.error)
|
||||||
|
.setMessage(getString(R.string.import_failed, failed_item))
|
||||||
|
.setPositiveButton(R.string.ok, null)
|
||||||
|
.show()
|
||||||
|
}
|
||||||
|
remote_gocryptfsVolume.close()
|
||||||
|
setCurrentPath(current_path)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onCreateOptionsMenu(menu: Menu): Boolean {
|
||||||
|
menuInflater.inflate(R.menu.explorer, menu)
|
||||||
|
handle_menu_items(menu)
|
||||||
|
if (usf_share){
|
||||||
|
menu.findItem(R.id.explorer_menu_share).isVisible = false
|
||||||
|
}
|
||||||
|
val any_item_selected = explorer_adapter.selectedItems.isNotEmpty()
|
||||||
|
menu.findItem(R.id.explorer_menu_select_all).isVisible = any_item_selected
|
||||||
|
menu.findItem(R.id.explorer_menu_delete).isVisible = any_item_selected
|
||||||
|
menu.findItem(R.id.explorer_menu_decrypt).isVisible = any_item_selected && usf_decrypt
|
||||||
|
if (any_item_selected && usf_share){
|
||||||
|
var containsDir = false
|
||||||
|
for (i in explorer_adapter.selectedItems) {
|
||||||
|
if (explorer_elements[i].isDirectory) {
|
||||||
|
containsDir = true
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (!containsDir) {
|
||||||
|
menu.findItem(R.id.explorer_menu_share).isVisible = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onOptionsItemSelected(item: MenuItem): Boolean {
|
||||||
|
return when (item.itemId) {
|
||||||
|
R.id.explorer_menu_select_all -> {
|
||||||
|
explorer_adapter.selectAll()
|
||||||
|
invalidateOptionsMenu()
|
||||||
|
true
|
||||||
|
}
|
||||||
|
R.id.explorer_menu_delete -> {
|
||||||
|
val size = explorer_adapter.selectedItems.size
|
||||||
|
val dialog = ColoredAlertDialog(this)
|
||||||
|
dialog.setTitle(R.string.warning)
|
||||||
|
dialog.setPositiveButton(R.string.ok) { _, _ -> remove_selected_items() }
|
||||||
|
dialog.setNegativeButton(R.string.cancel, null)
|
||||||
|
if (size > 1) {
|
||||||
|
dialog.setMessage(getString(R.string.multiple_delete_confirm, explorer_adapter.selectedItems.size.toString()))
|
||||||
|
} else {
|
||||||
|
dialog.setMessage(getString(R.string.single_delete_confirm, explorer_adapter.getItem(explorer_adapter.selectedItems[0]).name))
|
||||||
|
}
|
||||||
|
dialog.show()
|
||||||
|
true
|
||||||
|
}
|
||||||
|
R.id.explorer_menu_share -> {
|
||||||
|
val paths: MutableList<String> = ArrayList()
|
||||||
|
for (i in explorer_adapter.selectedItems) {
|
||||||
|
val e = explorer_elements[i]
|
||||||
|
paths.add(FilesUtils.path_join(current_path, e.name))
|
||||||
|
}
|
||||||
|
ExternalProvider.share(this, gocryptfsVolume, paths)
|
||||||
|
explorer_adapter.unSelectAll()
|
||||||
|
invalidateOptionsMenu()
|
||||||
|
true
|
||||||
|
}
|
||||||
|
R.id.explorer_menu_decrypt -> {
|
||||||
|
val i = Intent(Intent.ACTION_OPEN_DOCUMENT_TREE)
|
||||||
|
startActivityForResult(i, PICK_DIRECTORY_REQUEST_CODE)
|
||||||
|
true
|
||||||
|
}
|
||||||
|
else -> super.onOptionsItemSelected(item)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun import_file_from_other_volume(remote_gocryptfsVolume: GocryptfsVolume, full_path: String, output_dir: String): Boolean {
|
||||||
|
val output_path = FilesUtils.path_join(output_dir, File(full_path).name)
|
||||||
|
var success = true
|
||||||
|
val src_handleID = remote_gocryptfsVolume.open_read_mode(full_path)
|
||||||
|
if (src_handleID != -1) {
|
||||||
|
val dst_handleID = gocryptfsVolume.open_write_mode(output_path)
|
||||||
|
if (dst_handleID != -1) {
|
||||||
|
var length: Int
|
||||||
|
val io_buffer = ByteArray(GocryptfsVolume.DefaultBS)
|
||||||
|
var offset: Long = 0
|
||||||
|
while (remote_gocryptfsVolume.read_file(src_handleID, offset, io_buffer).also { length = it } > 0){
|
||||||
|
val written = gocryptfsVolume.write_file(dst_handleID, offset, io_buffer, length).toLong()
|
||||||
|
if (written == length.toLong()) {
|
||||||
|
offset += length.toLong()
|
||||||
|
} else {
|
||||||
|
success = false
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
gocryptfsVolume.close_file(dst_handleID)
|
||||||
|
}
|
||||||
|
remote_gocryptfsVolume.close_file(src_handleID)
|
||||||
|
}
|
||||||
|
return success
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun recursive_import_directory_from_other_volume(remote_gocryptfsVolume: GocryptfsVolume, remote_directory_path: String, output_dir: String): String? {
|
||||||
|
val directory_path = FilesUtils.path_join(output_dir, File(remote_directory_path).name)
|
||||||
|
if (!gocryptfsVolume.path_exists(directory_path)) {
|
||||||
|
if (!gocryptfsVolume.mkdir(directory_path)) {
|
||||||
|
return directory_path
|
||||||
|
}
|
||||||
|
}
|
||||||
|
val explorer_elements = remote_gocryptfsVolume.list_dir(remote_directory_path)
|
||||||
|
for (e in explorer_elements) {
|
||||||
|
val full_path = FilesUtils.path_join(remote_directory_path, e.name)
|
||||||
|
if (e.isDirectory) {
|
||||||
|
val failed_item = recursive_import_directory_from_other_volume(remote_gocryptfsVolume, full_path, directory_path)
|
||||||
|
failed_item?.let { return it }
|
||||||
|
} else {
|
||||||
|
if (!import_file_from_other_volume(remote_gocryptfsVolume, full_path, directory_path)) {
|
||||||
|
return full_path
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun recursive_export_directory(plain_directory_path: String, output_dir: String?): String? {
|
||||||
|
if (File(FilesUtils.path_join(output_dir, plain_directory_path)).mkdir()) {
|
||||||
|
val explorer_elements = gocryptfsVolume.list_dir(plain_directory_path)
|
||||||
|
for (e in explorer_elements) {
|
||||||
|
val full_path = FilesUtils.path_join(plain_directory_path, e.name)
|
||||||
|
if (e.isDirectory) {
|
||||||
|
val failed_item = recursive_export_directory(full_path, output_dir)
|
||||||
|
failed_item?.let { return it }
|
||||||
|
} else {
|
||||||
|
if (!gocryptfsVolume.export_file(full_path, FilesUtils.path_join(output_dir, full_path))) {
|
||||||
|
return full_path
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
return output_dir
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun recursive_remove_directory(plain_directory_path: String): String? {
|
||||||
|
val explorer_elements = gocryptfsVolume.list_dir(plain_directory_path)
|
||||||
|
for (e in explorer_elements) {
|
||||||
|
val full_path = FilesUtils.path_join(plain_directory_path, e.name)
|
||||||
|
if (e.isDirectory) {
|
||||||
|
val result = recursive_remove_directory(full_path)
|
||||||
|
result?.let { return it }
|
||||||
|
} else {
|
||||||
|
if (!gocryptfsVolume.remove_file(full_path)) {
|
||||||
|
return full_path
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return if (!gocryptfsVolume.rmdir(plain_directory_path)) {
|
||||||
|
plain_directory_path
|
||||||
|
} else {
|
||||||
|
null
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun remove_selected_items() {
|
||||||
|
var failed_item: String? = null
|
||||||
|
for (i in explorer_adapter.selectedItems) {
|
||||||
|
val element = explorer_adapter.getItem(i)
|
||||||
|
val full_path = FilesUtils.path_join(current_path, element.name)
|
||||||
|
if (element.isDirectory) {
|
||||||
|
val result = recursive_remove_directory(full_path)
|
||||||
|
result?.let{ failed_item = it }
|
||||||
|
} else {
|
||||||
|
if (!gocryptfsVolume.remove_file(full_path)) {
|
||||||
|
failed_item = full_path
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (failed_item != null) {
|
||||||
|
ColoredAlertDialog(this)
|
||||||
|
.setTitle(R.string.error)
|
||||||
|
.setMessage(getString(R.string.remove_failed, failed_item))
|
||||||
|
.setPositiveButton(R.string.ok, null)
|
||||||
|
.show()
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
explorer_adapter.unSelectAll()
|
||||||
|
invalidateOptionsMenu()
|
||||||
|
setCurrentPath(current_path) //refresh
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,68 @@
|
|||||||
|
package sushi.hardcore.droidfs.explorers
|
||||||
|
|
||||||
|
import android.content.Intent
|
||||||
|
import android.net.Uri
|
||||||
|
import android.view.Menu
|
||||||
|
import android.view.MenuItem
|
||||||
|
import sushi.hardcore.droidfs.R
|
||||||
|
import sushi.hardcore.droidfs.util.FilesUtils
|
||||||
|
import sushi.hardcore.droidfs.widgets.ColoredAlertDialog
|
||||||
|
|
||||||
|
class ExplorerActivityDrop : ExplorerActivityRO() {
|
||||||
|
override fun init() {
|
||||||
|
setContentView(R.layout.activity_explorer_drop)
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onCreateOptionsMenu(menu: Menu): Boolean {
|
||||||
|
menuInflater.inflate(R.menu.explorer_drop, menu)
|
||||||
|
handle_menu_items(menu)
|
||||||
|
menu.findItem(R.id.explorer_menu_validate).isVisible = explorer_adapter.selectedItems.isEmpty()
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onOptionsItemSelected(item: MenuItem): Boolean {
|
||||||
|
return when (item.itemId) {
|
||||||
|
R.id.explorer_menu_validate -> {
|
||||||
|
val alertDialog = ColoredAlertDialog(this)
|
||||||
|
alertDialog.setCancelable(false)
|
||||||
|
alertDialog.setPositiveButton(R.string.ok) { _, _ -> finish() }
|
||||||
|
var error_msg: String? = null
|
||||||
|
val extras = intent.extras
|
||||||
|
if (extras != null && extras.containsKey(Intent.EXTRA_STREAM)){
|
||||||
|
if (intent.action == Intent.ACTION_SEND) {
|
||||||
|
val uri = intent.getParcelableExtra<Uri>(Intent.EXTRA_STREAM)
|
||||||
|
val output_path = FilesUtils.path_join(current_path, FilesUtils.getFilenameFromURI(this, uri))
|
||||||
|
error_msg = if (gocryptfsVolume.import_file(this, uri, output_path)) null else getString(R.string.import_failed, output_path)
|
||||||
|
} else if (intent.action == Intent.ACTION_SEND_MULTIPLE) {
|
||||||
|
val uris = intent.getParcelableArrayListExtra<Uri>(Intent.EXTRA_STREAM)
|
||||||
|
if (uris != null){
|
||||||
|
for (uri in uris) {
|
||||||
|
val output_path = FilesUtils.path_join(current_path, FilesUtils.getFilenameFromURI(this, uri))
|
||||||
|
if (!gocryptfsVolume.import_file(this, uri, output_path)) {
|
||||||
|
error_msg = getString(R.string.import_failed, output_path)
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
error_msg = getString(R.string.share_intent_parsing_failed)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
error_msg = getString(R.string.share_intent_parsing_failed)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
error_msg = getString(R.string.share_intent_parsing_failed)
|
||||||
|
}
|
||||||
|
if (error_msg == null) {
|
||||||
|
alertDialog.setTitle(R.string.success_import)
|
||||||
|
alertDialog.setMessage(R.string.success_import_msg)
|
||||||
|
} else {
|
||||||
|
alertDialog.setTitle(R.string.error)
|
||||||
|
alertDialog.setMessage(error_msg)
|
||||||
|
}
|
||||||
|
alertDialog.show()
|
||||||
|
true
|
||||||
|
}
|
||||||
|
else -> super.onOptionsItemSelected(item)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,89 @@
|
|||||||
|
package sushi.hardcore.droidfs.explorers
|
||||||
|
|
||||||
|
import android.app.Activity
|
||||||
|
import android.content.Intent
|
||||||
|
import android.view.Menu
|
||||||
|
import android.view.MenuItem
|
||||||
|
import sushi.hardcore.droidfs.R
|
||||||
|
import sushi.hardcore.droidfs.provider.TemporaryFileProvider
|
||||||
|
import sushi.hardcore.droidfs.util.FilesUtils
|
||||||
|
import java.util.*
|
||||||
|
|
||||||
|
class ExplorerActivityPick : ExplorerActivityRO() {
|
||||||
|
private var result_intent = Intent()
|
||||||
|
override fun init() {
|
||||||
|
super.init()
|
||||||
|
result_intent.putExtra("sessionID", gocryptfsVolume.sessionID)
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onExplorerItemClick(position: Int) {
|
||||||
|
val wasSelecting = explorer_adapter.selectedItems.isNotEmpty()
|
||||||
|
explorer_adapter.onItemClick(position)
|
||||||
|
if (explorer_adapter.selectedItems.isEmpty()) {
|
||||||
|
if (!wasSelecting) {
|
||||||
|
val full_path = FilesUtils.path_join(current_path, explorer_elements[position].name)
|
||||||
|
when {
|
||||||
|
explorer_elements[position].isDirectory -> {
|
||||||
|
setCurrentPath(full_path)
|
||||||
|
}
|
||||||
|
explorer_elements[position].isParentFolder -> {
|
||||||
|
setCurrentPath(FilesUtils.get_parent_path(current_path))
|
||||||
|
}
|
||||||
|
else -> {
|
||||||
|
result_intent.putExtra("path", full_path)
|
||||||
|
return_activity_result()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
invalidateOptionsMenu()
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onCreateOptionsMenu(menu: Menu): Boolean {
|
||||||
|
menuInflater.inflate(R.menu.explorer_pick, menu)
|
||||||
|
handle_menu_items(menu)
|
||||||
|
val any_item_selected = explorer_adapter.selectedItems.isNotEmpty()
|
||||||
|
menu.findItem(R.id.explorer_menu_select_all).isVisible = any_item_selected
|
||||||
|
menu.findItem(R.id.explorer_menu_validate).isVisible = any_item_selected
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onOptionsItemSelected(item: MenuItem): Boolean {
|
||||||
|
return when (item.itemId) {
|
||||||
|
R.id.explorer_menu_select_all -> {
|
||||||
|
explorer_adapter.selectAll()
|
||||||
|
invalidateOptionsMenu()
|
||||||
|
true
|
||||||
|
}
|
||||||
|
R.id.explorer_menu_validate -> {
|
||||||
|
val paths = ArrayList<String>()
|
||||||
|
val types = ArrayList<Int>()
|
||||||
|
for (i in explorer_adapter.selectedItems) {
|
||||||
|
val e = explorer_elements[i]
|
||||||
|
paths.add(FilesUtils.path_join(current_path, e.name))
|
||||||
|
types.add(e.elementType.toInt())
|
||||||
|
}
|
||||||
|
result_intent.putStringArrayListExtra("paths", paths)
|
||||||
|
result_intent.putIntegerArrayListExtra("types", types)
|
||||||
|
return_activity_result()
|
||||||
|
true
|
||||||
|
}
|
||||||
|
else -> super.onOptionsItemSelected(item)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun return_activity_result() {
|
||||||
|
setResult(Activity.RESULT_OK, result_intent)
|
||||||
|
finish()
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun closeVolumeOnDestroy() {
|
||||||
|
//don't close volume
|
||||||
|
TemporaryFileProvider.wipeAll()
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun closeVolumeOnUserExit() {
|
||||||
|
super.closeVolumeOnUserExit()
|
||||||
|
super.closeVolumeOnDestroy()
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,365 @@
|
|||||||
|
package sushi.hardcore.droidfs.explorers
|
||||||
|
|
||||||
|
import android.content.Intent
|
||||||
|
import android.content.SharedPreferences
|
||||||
|
import android.os.Bundle
|
||||||
|
import android.view.Menu
|
||||||
|
import android.view.MenuItem
|
||||||
|
import android.view.View
|
||||||
|
import android.view.WindowManager
|
||||||
|
import android.widget.AdapterView.OnItemClickListener
|
||||||
|
import android.widget.AdapterView.OnItemLongClickListener
|
||||||
|
import android.widget.EditText
|
||||||
|
import android.widget.ListView
|
||||||
|
import android.widget.Toast
|
||||||
|
import com.github.clans.fab.FloatingActionMenu
|
||||||
|
import kotlinx.android.synthetic.main.activity_explorer_ro.*
|
||||||
|
import kotlinx.android.synthetic.main.explorer_info_bar.*
|
||||||
|
import kotlinx.android.synthetic.main.toolbar.*
|
||||||
|
import sushi.hardcore.droidfs.ColoredActivity
|
||||||
|
import sushi.hardcore.droidfs.ConstValues
|
||||||
|
import sushi.hardcore.droidfs.ConstValues.Companion.isAudio
|
||||||
|
import sushi.hardcore.droidfs.ConstValues.Companion.isImage
|
||||||
|
import sushi.hardcore.droidfs.ConstValues.Companion.isText
|
||||||
|
import sushi.hardcore.droidfs.ConstValues.Companion.isVideo
|
||||||
|
import sushi.hardcore.droidfs.R
|
||||||
|
import sushi.hardcore.droidfs.adapters.OpenAsDialogAdapter
|
||||||
|
import sushi.hardcore.droidfs.adapters.ExplorerElementAdapter
|
||||||
|
import sushi.hardcore.droidfs.file_viewers.AudioPlayer
|
||||||
|
import sushi.hardcore.droidfs.file_viewers.ImageViewer
|
||||||
|
import sushi.hardcore.droidfs.file_viewers.TextEditor
|
||||||
|
import sushi.hardcore.droidfs.file_viewers.VideoPlayer
|
||||||
|
import sushi.hardcore.droidfs.provider.TemporaryFileProvider
|
||||||
|
import sushi.hardcore.droidfs.util.ExternalProvider
|
||||||
|
import sushi.hardcore.droidfs.util.FilesUtils
|
||||||
|
import sushi.hardcore.droidfs.util.GocryptfsVolume
|
||||||
|
import sushi.hardcore.droidfs.widgets.ColoredAlertDialog
|
||||||
|
import java.util.*
|
||||||
|
|
||||||
|
open class ExplorerActivityRO : ColoredActivity() {
|
||||||
|
private lateinit var shared_prefs_editor: SharedPreferences.Editor
|
||||||
|
private lateinit var sort_modes_entries: Array<String>
|
||||||
|
private lateinit var sort_modes_values: Array<String>
|
||||||
|
private var current_sort_mode_index = 0
|
||||||
|
protected lateinit var gocryptfsVolume: GocryptfsVolume
|
||||||
|
private lateinit var volume_name: String
|
||||||
|
protected var current_path = ""
|
||||||
|
protected lateinit var explorer_elements: MutableList<ExplorerElement>
|
||||||
|
protected lateinit var explorer_adapter: ExplorerElementAdapter
|
||||||
|
private var usf_open = false
|
||||||
|
override fun onCreate(savedInstanceState: Bundle?) {
|
||||||
|
super.onCreate(savedInstanceState)
|
||||||
|
if (!sharedPrefs.getBoolean("usf_screenshot", false)){
|
||||||
|
window.setFlags(WindowManager.LayoutParams.FLAG_SECURE, WindowManager.LayoutParams.FLAG_SECURE)
|
||||||
|
}
|
||||||
|
usf_open = sharedPrefs.getBoolean("usf_open", false)
|
||||||
|
val intent = intent
|
||||||
|
volume_name = intent.getStringExtra("volume_name")
|
||||||
|
val sessionID = intent.getIntExtra("sessionID", -1)
|
||||||
|
gocryptfsVolume = GocryptfsVolume(sessionID)
|
||||||
|
sort_modes_entries = resources.getStringArray(R.array.sort_orders_entries)
|
||||||
|
sort_modes_values = resources.getStringArray(R.array.sort_orders_values)
|
||||||
|
current_sort_mode_index = resources.getStringArray(R.array.sort_orders_values).indexOf(sharedPrefs.getString(ConstValues.sort_order_key, "name"))
|
||||||
|
shared_prefs_editor = sharedPrefs.edit()
|
||||||
|
init()
|
||||||
|
setSupportActionBar(toolbar)
|
||||||
|
title = ""
|
||||||
|
title_text.text = getString(R.string.volume, volume_name)
|
||||||
|
explorer_adapter = ExplorerElementAdapter(this)
|
||||||
|
setCurrentPath(current_path)
|
||||||
|
list_explorer.adapter = explorer_adapter
|
||||||
|
list_explorer.onItemClickListener = OnItemClickListener { _, _, position, _ -> onExplorerItemClick(position) }
|
||||||
|
list_explorer.onItemLongClickListener = OnItemLongClickListener { _, _, position, _ ->
|
||||||
|
explorer_adapter.onItemLongClick(position)
|
||||||
|
invalidateOptionsMenu()
|
||||||
|
true
|
||||||
|
}
|
||||||
|
refresher.setOnRefreshListener {
|
||||||
|
setCurrentPath(current_path)
|
||||||
|
refresher.isRefreshing = false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
protected open fun init() {
|
||||||
|
setContentView(R.layout.activity_explorer_ro)
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun startFileViewer(cls: Class<*>, filePath: String){
|
||||||
|
val intent = Intent(this, cls)
|
||||||
|
intent.putExtra("path", filePath)
|
||||||
|
intent.putExtra("sessionID", gocryptfsVolume.sessionID)
|
||||||
|
startActivity(intent)
|
||||||
|
}
|
||||||
|
|
||||||
|
protected open fun onExplorerItemClick(position: Int) {
|
||||||
|
val wasSelecting = explorer_adapter.selectedItems.isNotEmpty()
|
||||||
|
explorer_adapter.onItemClick(position)
|
||||||
|
if (explorer_adapter.selectedItems.isEmpty()) {
|
||||||
|
if (!wasSelecting) {
|
||||||
|
val full_path = FilesUtils.path_join(current_path, explorer_elements[position].name)
|
||||||
|
when {
|
||||||
|
explorer_elements[position].isDirectory -> {
|
||||||
|
setCurrentPath(full_path)
|
||||||
|
}
|
||||||
|
explorer_elements[position].isParentFolder -> {
|
||||||
|
setCurrentPath(FilesUtils.get_parent_path(current_path))
|
||||||
|
}
|
||||||
|
isImage(full_path) -> {
|
||||||
|
startFileViewer(ImageViewer::class.java, full_path)
|
||||||
|
}
|
||||||
|
isVideo(full_path) -> {
|
||||||
|
startFileViewer(VideoPlayer::class.java, full_path)
|
||||||
|
}
|
||||||
|
isText(full_path) -> {
|
||||||
|
startFileViewer(TextEditor::class.java, full_path)
|
||||||
|
}
|
||||||
|
isAudio(full_path) -> {
|
||||||
|
startFileViewer(AudioPlayer::class.java, full_path)
|
||||||
|
}
|
||||||
|
else -> {
|
||||||
|
val dialogListView = layoutInflater.inflate(R.layout.dialog_listview, null)
|
||||||
|
val listView = dialogListView.findViewById<ListView>(R.id.listview)
|
||||||
|
val adapter = OpenAsDialogAdapter(this)
|
||||||
|
listView.adapter = adapter
|
||||||
|
val dialog = ColoredAlertDialog(this)
|
||||||
|
.setView(dialogListView)
|
||||||
|
.setTitle(getString(R.string.open_as))
|
||||||
|
.setNegativeButton(R.string.cancel, null)
|
||||||
|
.create()
|
||||||
|
listView.setOnItemClickListener{_, _, fileTypePosition, _ ->
|
||||||
|
when (adapter.getItem(fileTypePosition)){
|
||||||
|
"image" -> startFileViewer(ImageViewer::class.java, full_path)
|
||||||
|
"video" -> startFileViewer(VideoPlayer::class.java, full_path)
|
||||||
|
"audio" -> startFileViewer(AudioPlayer::class.java, full_path)
|
||||||
|
"text" -> startFileViewer(TextEditor::class.java, full_path)
|
||||||
|
}
|
||||||
|
dialog.dismiss()
|
||||||
|
}
|
||||||
|
dialog.show()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
invalidateOptionsMenu()
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun sort_explorer_elements() {
|
||||||
|
when (sort_modes_values[current_sort_mode_index]) {
|
||||||
|
"name" -> {
|
||||||
|
explorer_elements.sortWith(Comparator { o1, o2 -> o1.name.compareTo(o2.name) })
|
||||||
|
}
|
||||||
|
"size" -> {
|
||||||
|
explorer_elements.sortWith(Comparator { o1, o2 -> (o1.size - o2.size).toInt() })
|
||||||
|
}
|
||||||
|
"date" -> {
|
||||||
|
explorer_elements.sortWith(Comparator { o1, o2 -> o1.mTime.compareTo(o2.mTime) })
|
||||||
|
}
|
||||||
|
"name_desc" -> {
|
||||||
|
explorer_elements.sortWith(Comparator { o1, o2 -> o2.name.compareTo(o1.name) })
|
||||||
|
}
|
||||||
|
"size_desc" -> {
|
||||||
|
explorer_elements.sortWith(Comparator { o1, o2 -> (o2.size - o1.size).toInt() })
|
||||||
|
}
|
||||||
|
"date_desc" -> {
|
||||||
|
explorer_elements.sortWith(Comparator { o1, o2 -> o2.mTime.compareTo(o1.mTime) })
|
||||||
|
}
|
||||||
|
}
|
||||||
|
shared_prefs_editor.putString(ConstValues.sort_order_key, sort_modes_values[current_sort_mode_index])
|
||||||
|
shared_prefs_editor.apply()
|
||||||
|
}
|
||||||
|
|
||||||
|
protected fun setCurrentPath(path: String) {
|
||||||
|
explorer_elements = gocryptfsVolume.list_dir(path)
|
||||||
|
text_dir_empty.visibility = if (explorer_elements.size == 0) View.VISIBLE else View.INVISIBLE
|
||||||
|
sort_explorer_elements()
|
||||||
|
if (path.isNotEmpty()) { //not root
|
||||||
|
explorer_elements.add(0, ExplorerElement("..", (-1).toShort(), -1, -1))
|
||||||
|
}
|
||||||
|
explorer_adapter.setExplorerElements(explorer_elements)
|
||||||
|
current_path = path
|
||||||
|
current_path_text.text = getString(R.string.location, current_path)
|
||||||
|
total_size_text.text = getString(R.string.total_size, FilesUtils.formatSize(explorer_adapter.currentDirectoryTotalSize))
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun askCloseVolume() {
|
||||||
|
ColoredAlertDialog(this)
|
||||||
|
.setTitle(R.string.warning)
|
||||||
|
.setMessage(R.string.ask_close_volume)
|
||||||
|
.setPositiveButton(R.string.ok) { _, _ -> closeVolumeOnUserExit() }
|
||||||
|
.setNegativeButton(R.string.cancel, null)
|
||||||
|
.show()
|
||||||
|
}
|
||||||
|
|
||||||
|
protected open fun closeVolumeOnUserExit() {
|
||||||
|
finish()
|
||||||
|
}
|
||||||
|
|
||||||
|
protected open fun closeVolumeOnDestroy() {
|
||||||
|
gocryptfsVolume.close()
|
||||||
|
TemporaryFileProvider.wipeAll() //additional security
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onBackPressed() {
|
||||||
|
if (explorer_adapter.selectedItems.isEmpty()) {
|
||||||
|
val parent_path = FilesUtils.get_parent_path(current_path)
|
||||||
|
if (parent_path == current_path) {
|
||||||
|
askCloseVolume()
|
||||||
|
} else {
|
||||||
|
setCurrentPath(FilesUtils.get_parent_path(current_path))
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
explorer_adapter.unSelectAll()
|
||||||
|
invalidateOptionsMenu()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun createFolder(folder_name: String){
|
||||||
|
if (folder_name.isEmpty()) {
|
||||||
|
Toast.makeText(this, R.string.error_filename_empty, Toast.LENGTH_SHORT).show()
|
||||||
|
} else {
|
||||||
|
if (!gocryptfsVolume.mkdir(FilesUtils.path_join(current_path, folder_name))) {
|
||||||
|
ColoredAlertDialog(this)
|
||||||
|
.setTitle(R.string.error)
|
||||||
|
.setMessage(R.string.error_mkdir)
|
||||||
|
.setPositiveButton(R.string.ok, null)
|
||||||
|
.show()
|
||||||
|
} else {
|
||||||
|
setCurrentPath(current_path)
|
||||||
|
invalidateOptionsMenu()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun onClickAddFolder(view: View?) {
|
||||||
|
findViewById<FloatingActionMenu>(R.id.fam_explorer).close(true)
|
||||||
|
val dialog_edit_text_view = layoutInflater.inflate(R.layout.dialog_edit_text, null)
|
||||||
|
val dialog_edit_text = dialog_edit_text_view.findViewById<EditText>(R.id.dialog_edit_text)
|
||||||
|
val dialog = ColoredAlertDialog(this)
|
||||||
|
.setView(dialog_edit_text_view)
|
||||||
|
.setTitle(R.string.enter_folder_name)
|
||||||
|
.setPositiveButton(R.string.ok) { _, _ ->
|
||||||
|
val folder_name = dialog_edit_text.text.toString()
|
||||||
|
createFolder(folder_name)
|
||||||
|
}
|
||||||
|
.setNegativeButton(R.string.cancel, null)
|
||||||
|
.create()
|
||||||
|
dialog_edit_text.setOnEditorActionListener { _, _, _ ->
|
||||||
|
val folder_name = dialog_edit_text.text.toString()
|
||||||
|
dialog.dismiss()
|
||||||
|
createFolder(folder_name)
|
||||||
|
true
|
||||||
|
}
|
||||||
|
dialog.window?.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE)
|
||||||
|
dialog.show()
|
||||||
|
}
|
||||||
|
|
||||||
|
fun rename(old_name: String, new_name: String){
|
||||||
|
if (new_name.isEmpty()) {
|
||||||
|
Toast.makeText(this, R.string.error_filename_empty, Toast.LENGTH_SHORT).show()
|
||||||
|
} else {
|
||||||
|
if (!gocryptfsVolume.rename(FilesUtils.path_join(current_path, old_name), FilesUtils.path_join(current_path, new_name))) {
|
||||||
|
ColoredAlertDialog(this)
|
||||||
|
.setTitle(R.string.error)
|
||||||
|
.setMessage(getString(R.string.rename_failed, old_name))
|
||||||
|
.setPositiveButton(R.string.ok, null)
|
||||||
|
.show()
|
||||||
|
} else {
|
||||||
|
setCurrentPath(current_path)
|
||||||
|
invalidateOptionsMenu()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun handle_menu_items(menu: Menu){
|
||||||
|
menu.findItem(R.id.explorer_menu_rename).isVisible = false
|
||||||
|
if (usf_open){
|
||||||
|
menu.findItem(R.id.explorer_menu_external_open)?.isVisible = false
|
||||||
|
}
|
||||||
|
val selectedItems = explorer_adapter.selectedItems
|
||||||
|
if (selectedItems.isEmpty()){
|
||||||
|
toolbar.navigationIcon = null
|
||||||
|
menu.findItem(R.id.explorer_menu_close).isVisible = true
|
||||||
|
menu.findItem(R.id.explorer_menu_sort).isVisible = true
|
||||||
|
} else {
|
||||||
|
toolbar.setNavigationIcon(R.drawable.icon_arrow_back)
|
||||||
|
menu.findItem(R.id.explorer_menu_close).isVisible = false
|
||||||
|
menu.findItem(R.id.explorer_menu_sort).isVisible = false
|
||||||
|
if (selectedItems.size == 1) {
|
||||||
|
menu.findItem(R.id.explorer_menu_rename).isVisible = true
|
||||||
|
if (usf_open && explorer_elements[selectedItems[0]].isRegularFile) {
|
||||||
|
menu.findItem(R.id.explorer_menu_external_open)?.isVisible = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onOptionsItemSelected(item: MenuItem): Boolean {
|
||||||
|
return when (item.itemId) {
|
||||||
|
android.R.id.home -> {
|
||||||
|
explorer_adapter.unSelectAll()
|
||||||
|
invalidateOptionsMenu()
|
||||||
|
true
|
||||||
|
}
|
||||||
|
R.id.explorer_menu_sort -> {
|
||||||
|
ColoredAlertDialog(this)
|
||||||
|
.setTitle(R.string.sort_order)
|
||||||
|
.setSingleChoiceItems(sort_modes_entries, current_sort_mode_index) { dialog, which ->
|
||||||
|
current_sort_mode_index = which
|
||||||
|
setCurrentPath(current_path)
|
||||||
|
dialog.dismiss()
|
||||||
|
}.show()
|
||||||
|
true
|
||||||
|
}
|
||||||
|
R.id.explorer_menu_rename -> {
|
||||||
|
val dialog_edit_text_view = layoutInflater.inflate(R.layout.dialog_edit_text, null)
|
||||||
|
val old_name = explorer_elements[explorer_adapter.selectedItems[0]].name
|
||||||
|
val dialog_edit_text = dialog_edit_text_view.findViewById<EditText>(R.id.dialog_edit_text)
|
||||||
|
dialog_edit_text.setText(old_name)
|
||||||
|
dialog_edit_text.selectAll()
|
||||||
|
val dialog = ColoredAlertDialog(this)
|
||||||
|
.setView(dialog_edit_text_view)
|
||||||
|
.setTitle(R.string.rename_title)
|
||||||
|
.setPositiveButton(R.string.ok) { _, _ ->
|
||||||
|
val new_name = dialog_edit_text.text.toString()
|
||||||
|
rename(old_name, new_name)
|
||||||
|
}
|
||||||
|
.setNegativeButton(R.string.cancel, null)
|
||||||
|
.create()
|
||||||
|
dialog_edit_text.setOnEditorActionListener { _, _, _ ->
|
||||||
|
val new_name = dialog_edit_text.text.toString()
|
||||||
|
dialog.dismiss()
|
||||||
|
rename(old_name, new_name)
|
||||||
|
true
|
||||||
|
}
|
||||||
|
dialog.window?.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE)
|
||||||
|
dialog.show()
|
||||||
|
true
|
||||||
|
}
|
||||||
|
R.id.explorer_menu_external_open -> {
|
||||||
|
if (usf_open){
|
||||||
|
ExternalProvider.open(this, gocryptfsVolume, FilesUtils.path_join(current_path, explorer_elements[explorer_adapter.selectedItems[0]].name))
|
||||||
|
explorer_adapter.unSelectAll()
|
||||||
|
invalidateOptionsMenu()
|
||||||
|
}
|
||||||
|
true
|
||||||
|
}
|
||||||
|
R.id.explorer_menu_close -> {
|
||||||
|
askCloseVolume()
|
||||||
|
true
|
||||||
|
}
|
||||||
|
else -> super.onOptionsItemSelected(item)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onDestroy() {
|
||||||
|
super.onDestroy()
|
||||||
|
if (!isChangingConfigurations) { //activity won't be recreated
|
||||||
|
closeVolumeOnDestroy()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onResume() {
|
||||||
|
super.onResume()
|
||||||
|
ExternalProvider.clear_cache(this)
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,17 @@
|
|||||||
|
package sushi.hardcore.droidfs.explorers
|
||||||
|
|
||||||
|
import java.util.*
|
||||||
|
|
||||||
|
class ExplorerElement(val name: String, val elementType: Short, val size: Long, mtime: Long) {
|
||||||
|
val mTime = Date((mtime * 1000).toString().toLong())
|
||||||
|
|
||||||
|
val isDirectory: Boolean
|
||||||
|
get() = elementType.toInt() == 0
|
||||||
|
|
||||||
|
val isParentFolder: Boolean
|
||||||
|
get() = elementType.toInt() == -1
|
||||||
|
|
||||||
|
val isRegularFile: Boolean
|
||||||
|
get() = elementType.toInt() == 1
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,87 @@
|
|||||||
|
package sushi.hardcore.droidfs.file_viewers
|
||||||
|
|
||||||
|
import android.media.MediaPlayer
|
||||||
|
import android.os.Handler
|
||||||
|
import android.widget.SeekBar
|
||||||
|
import androidx.core.content.ContextCompat
|
||||||
|
import kotlinx.android.synthetic.main.activity_audio_player.*
|
||||||
|
import sushi.hardcore.droidfs.ConstValues
|
||||||
|
import sushi.hardcore.droidfs.R
|
||||||
|
import sushi.hardcore.droidfs.widgets.ColoredAlertDialog
|
||||||
|
import java.io.File
|
||||||
|
import java.io.IOException
|
||||||
|
|
||||||
|
class AudioPlayer: FileViewerActivity(){
|
||||||
|
private lateinit var player: MediaPlayer
|
||||||
|
private var isPrepared = false
|
||||||
|
override fun viewFile() {
|
||||||
|
setContentView(R.layout.activity_audio_player)
|
||||||
|
val filename = File(filePath).name
|
||||||
|
val pos = filename.lastIndexOf('.')
|
||||||
|
music_title.text = if (pos != -1){
|
||||||
|
filename.substring(0,pos)
|
||||||
|
} else {
|
||||||
|
filename
|
||||||
|
}
|
||||||
|
val tmpFileUri = exportFile(filePath)
|
||||||
|
tmpFileUri?.let {
|
||||||
|
player = MediaPlayer()
|
||||||
|
player.setDataSource(this, tmpFileUri)
|
||||||
|
try {
|
||||||
|
player.prepare()
|
||||||
|
isPrepared = true
|
||||||
|
} catch (e: IOException){
|
||||||
|
ColoredAlertDialog(this)
|
||||||
|
.setTitle(R.string.error)
|
||||||
|
.setMessage(getString(R.string.media_player_prepare_failed))
|
||||||
|
.setCancelable(false)
|
||||||
|
.setPositiveButton(R.string.ok) { _, _ -> finish() }
|
||||||
|
.show()
|
||||||
|
}
|
||||||
|
if (isPrepared){
|
||||||
|
player.isLooping = true
|
||||||
|
button_pause.setOnClickListener {
|
||||||
|
if (player.isPlaying) {
|
||||||
|
player.pause()
|
||||||
|
button_pause.setImageDrawable(ContextCompat.getDrawable(this, R.drawable.icon_play))
|
||||||
|
} else {
|
||||||
|
player.start()
|
||||||
|
button_pause.setImageDrawable(ContextCompat.getDrawable(this, R.drawable.icon_pause))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
button_stop.setOnClickListener { finish() }
|
||||||
|
seekbar.max = player.duration / ConstValues.seek_bar_inc
|
||||||
|
val handler = Handler()
|
||||||
|
runOnUiThread(object : Runnable {
|
||||||
|
override fun run() {
|
||||||
|
if (isPrepared) {
|
||||||
|
seekbar.progress = player.currentPosition / ConstValues.seek_bar_inc
|
||||||
|
}
|
||||||
|
handler.postDelayed(this, ConstValues.seek_bar_inc.toLong())
|
||||||
|
}
|
||||||
|
})
|
||||||
|
seekbar.setOnSeekBarChangeListener(object : SeekBar.OnSeekBarChangeListener {
|
||||||
|
override fun onProgressChanged(seekBar: SeekBar, progress: Int, fromUser: Boolean) {
|
||||||
|
if (::player.isInitialized && fromUser) {
|
||||||
|
player.seekTo(progress * ConstValues.seek_bar_inc)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
override fun onStartTrackingTouch(seekBar: SeekBar) {}
|
||||||
|
override fun onStopTrackingTouch(seekBar: SeekBar) {}
|
||||||
|
})
|
||||||
|
player.start()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onDestroy() {
|
||||||
|
super.onDestroy()
|
||||||
|
if (::player.isInitialized) {
|
||||||
|
if (player.isPlaying) {
|
||||||
|
player.stop()
|
||||||
|
}
|
||||||
|
isPrepared = false
|
||||||
|
player.release()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,114 @@
|
|||||||
|
package sushi.hardcore.droidfs.file_viewers
|
||||||
|
|
||||||
|
import android.net.Uri
|
||||||
|
import android.os.Bundle
|
||||||
|
import android.view.View
|
||||||
|
import android.view.WindowManager
|
||||||
|
import androidx.preference.PreferenceManager
|
||||||
|
import sushi.hardcore.droidfs.ColoredActivity
|
||||||
|
import sushi.hardcore.droidfs.R
|
||||||
|
import sushi.hardcore.droidfs.provider.TemporaryFileProvider
|
||||||
|
import sushi.hardcore.droidfs.util.GocryptfsVolume
|
||||||
|
import sushi.hardcore.droidfs.util.Wiper
|
||||||
|
import sushi.hardcore.droidfs.widgets.ColoredAlertDialog
|
||||||
|
import java.io.File
|
||||||
|
import java.util.ArrayList
|
||||||
|
|
||||||
|
abstract class FileViewerActivity: ColoredActivity() {
|
||||||
|
var cachedFiles: MutableList<Uri> = ArrayList()
|
||||||
|
lateinit var gocryptfsVolume: GocryptfsVolume
|
||||||
|
lateinit var filePath: String
|
||||||
|
override fun onCreate(savedInstanceState: Bundle?) {
|
||||||
|
super.onCreate(savedInstanceState)
|
||||||
|
val sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this)
|
||||||
|
if (!sharedPrefs.getBoolean("usf_screenshot", false)){
|
||||||
|
window.setFlags(WindowManager.LayoutParams.FLAG_SECURE, WindowManager.LayoutParams.FLAG_SECURE)
|
||||||
|
}
|
||||||
|
filePath = intent.getStringExtra("path")!!
|
||||||
|
val sessionID = intent.getIntExtra("sessionID", -1)
|
||||||
|
gocryptfsVolume = GocryptfsVolume(sessionID)
|
||||||
|
toggleFullscreen()
|
||||||
|
viewFile()
|
||||||
|
}
|
||||||
|
open fun toggleFullscreen(){
|
||||||
|
var uiOptions = window.decorView.systemUiVisibility
|
||||||
|
//uiOptions ^= View.SYSTEM_UI_FLAG_HIDE_NAVIGATION;
|
||||||
|
uiOptions = uiOptions xor View.SYSTEM_UI_FLAG_FULLSCREEN
|
||||||
|
uiOptions = uiOptions xor View.SYSTEM_UI_FLAG_IMMERSIVE
|
||||||
|
window.decorView.systemUiVisibility = uiOptions
|
||||||
|
}
|
||||||
|
abstract fun viewFile()
|
||||||
|
fun loadWholeFile(path: String): ByteArray? {
|
||||||
|
val fileSize = gocryptfsVolume.get_size(path)
|
||||||
|
if (fileSize >= 0){
|
||||||
|
try {
|
||||||
|
val fileBuff = ByteArray(fileSize.toInt())
|
||||||
|
var success = false
|
||||||
|
val handleID = gocryptfsVolume.open_read_mode(path)
|
||||||
|
if (handleID != -1) {
|
||||||
|
var offset: Long = 0
|
||||||
|
val io_buffer = ByteArray(GocryptfsVolume.DefaultBS)
|
||||||
|
var length: Int
|
||||||
|
while (gocryptfsVolume.read_file(handleID, offset, io_buffer).also { length = it } > 0){
|
||||||
|
System.arraycopy(io_buffer, 0, fileBuff, offset.toInt(), length)
|
||||||
|
offset += length.toLong()
|
||||||
|
}
|
||||||
|
gocryptfsVolume.close_file(handleID)
|
||||||
|
success = offset == fileBuff.size.toLong()
|
||||||
|
}
|
||||||
|
if (success){
|
||||||
|
return fileBuff
|
||||||
|
} else {
|
||||||
|
ColoredAlertDialog(this)
|
||||||
|
.setTitle(R.string.error)
|
||||||
|
.setMessage(R.string.read_file_failed)
|
||||||
|
.setCancelable(false)
|
||||||
|
.setPositiveButton(R.string.ok) { _, _ -> finish() }
|
||||||
|
.show()
|
||||||
|
}
|
||||||
|
} catch (e: OutOfMemoryError){
|
||||||
|
ColoredAlertDialog(this)
|
||||||
|
.setTitle(R.string.error)
|
||||||
|
.setMessage(getString(R.string.outofmemoryerror_msg))
|
||||||
|
.setCancelable(false)
|
||||||
|
.setPositiveButton(getString(R.string.ok)) { _, _ -> finish() }
|
||||||
|
.show()
|
||||||
|
}
|
||||||
|
|
||||||
|
} else {
|
||||||
|
ColoredAlertDialog(this)
|
||||||
|
.setTitle(R.string.error)
|
||||||
|
.setMessage(R.string.get_size_failed)
|
||||||
|
.setCancelable(false)
|
||||||
|
.setPositiveButton(R.string.ok) { _, _ -> finish() }
|
||||||
|
.show()
|
||||||
|
}
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
fun exportFile(path: String): Uri? {
|
||||||
|
val tmpFileUri = TemporaryFileProvider.createFile(this, File(path).name)
|
||||||
|
cachedFiles.add(tmpFileUri)
|
||||||
|
return if (gocryptfsVolume.export_file(this, path, tmpFileUri)) {
|
||||||
|
tmpFileUri
|
||||||
|
} else {
|
||||||
|
ColoredAlertDialog(this)
|
||||||
|
.setTitle(R.string.error)
|
||||||
|
.setMessage(getString(R.string.export_failed, path))
|
||||||
|
.setCancelable(false)
|
||||||
|
.setPositiveButton(R.string.ok) { _, _ -> finish() }
|
||||||
|
.show()
|
||||||
|
null
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onDestroy() {
|
||||||
|
super.onDestroy()
|
||||||
|
Thread{
|
||||||
|
for (uri in cachedFiles) {
|
||||||
|
if (Wiper.wipe(this, uri)){
|
||||||
|
cachedFiles.remove(uri)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}.start()
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,72 @@
|
|||||||
|
package sushi.hardcore.droidfs.file_viewers
|
||||||
|
|
||||||
|
import android.graphics.Bitmap
|
||||||
|
import android.graphics.BitmapFactory
|
||||||
|
import android.graphics.Matrix
|
||||||
|
import android.os.Handler
|
||||||
|
import android.util.DisplayMetrics
|
||||||
|
import android.view.View
|
||||||
|
import kotlinx.android.synthetic.main.activity_image_viewer.*
|
||||||
|
import sushi.hardcore.droidfs.R
|
||||||
|
|
||||||
|
class ImageViewer: FileViewerActivity() {
|
||||||
|
companion object {
|
||||||
|
private const val hideDelay: Long = 3000
|
||||||
|
}
|
||||||
|
private lateinit var bmpImage: Bitmap
|
||||||
|
private val handler = Handler()
|
||||||
|
private val hideActionButtons = Runnable { action_buttons.visibility = View.GONE }
|
||||||
|
override fun viewFile() {
|
||||||
|
loadWholeFile(filePath)?.let {
|
||||||
|
val metrics = DisplayMetrics()
|
||||||
|
windowManager.defaultDisplay.getRealMetrics(metrics)
|
||||||
|
bmpImage = decodeSampledBitmapFromBuffer(it, metrics.widthPixels, metrics.heightPixels)
|
||||||
|
setContentView(R.layout.activity_image_viewer)
|
||||||
|
image_viewer.setImageBitmap(bmpImage)
|
||||||
|
handler.postDelayed(hideActionButtons, hideDelay)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun calculateInSampleSize(options: BitmapFactory.Options, reqWidth: Int, reqHeight: Int): Int {
|
||||||
|
var inSampleSize = 1
|
||||||
|
if (options.outHeight > reqHeight || options.outWidth > reqWidth){
|
||||||
|
val halfHeight = options.outHeight/2
|
||||||
|
val halfWidth = options.outWidth/2
|
||||||
|
while (halfHeight/inSampleSize >= reqHeight && halfWidth/inSampleSize >= reqWidth){
|
||||||
|
inSampleSize *= 2
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return inSampleSize
|
||||||
|
}
|
||||||
|
private fun decodeSampledBitmapFromBuffer(buff: ByteArray, reqWidth: Int, reqHeight: Int): Bitmap {
|
||||||
|
return BitmapFactory.Options().run {
|
||||||
|
inJustDecodeBounds = true
|
||||||
|
BitmapFactory.decodeByteArray(buff, 0, buff.size, this)
|
||||||
|
inSampleSize = calculateInSampleSize(this, reqWidth, reqHeight)
|
||||||
|
inJustDecodeBounds = false
|
||||||
|
BitmapFactory.decodeByteArray(buff, 0, buff.size, this)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun rotateImage(degrees: Float){
|
||||||
|
val matrix = Matrix()
|
||||||
|
matrix.postRotate(degrees)
|
||||||
|
bmpImage = Bitmap.createBitmap(bmpImage, 0, 0, bmpImage.width, bmpImage.height, matrix, true)
|
||||||
|
image_viewer.setImageBitmap(bmpImage)
|
||||||
|
}
|
||||||
|
fun onCLickRotateRight(view: View){
|
||||||
|
rotateImage(90F)
|
||||||
|
}
|
||||||
|
fun onClickRotateLeft(view: View){
|
||||||
|
rotateImage(-90F)
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onUserInteraction() {
|
||||||
|
super.onUserInteraction()
|
||||||
|
if (action_buttons.visibility == View.GONE){
|
||||||
|
action_buttons.visibility = View.VISIBLE
|
||||||
|
handler.removeCallbacks(hideActionButtons)
|
||||||
|
handler.postDelayed(hideActionButtons, hideDelay)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,147 @@
|
|||||||
|
package sushi.hardcore.droidfs.file_viewers
|
||||||
|
|
||||||
|
import android.text.Editable
|
||||||
|
import android.text.TextWatcher
|
||||||
|
import android.view.Menu
|
||||||
|
import android.view.MenuItem
|
||||||
|
import android.widget.EditText
|
||||||
|
import android.widget.TextView
|
||||||
|
import android.widget.Toast
|
||||||
|
import androidx.appcompat.widget.Toolbar
|
||||||
|
import sushi.hardcore.droidfs.R
|
||||||
|
import sushi.hardcore.droidfs.util.GocryptfsVolume
|
||||||
|
import sushi.hardcore.droidfs.widgets.ColoredAlertDialog
|
||||||
|
import java.io.ByteArrayInputStream
|
||||||
|
import java.io.File
|
||||||
|
|
||||||
|
class TextEditor: FileViewerActivity() {
|
||||||
|
private lateinit var fileName: String
|
||||||
|
private lateinit var editor: EditText
|
||||||
|
private lateinit var toolbar: Toolbar
|
||||||
|
private lateinit var titleText: TextView
|
||||||
|
private var changedSinceLastSave = false
|
||||||
|
private var wordWrap = true
|
||||||
|
override fun toggleFullscreen() {
|
||||||
|
//don't toggle fullscreen
|
||||||
|
}
|
||||||
|
override fun viewFile() {
|
||||||
|
loadWholeFile(filePath)?.let {
|
||||||
|
fileName = File(filePath).name
|
||||||
|
try {
|
||||||
|
loadLayout(String(it))
|
||||||
|
} catch (e: OutOfMemoryError){
|
||||||
|
ColoredAlertDialog(this)
|
||||||
|
.setTitle(R.string.error)
|
||||||
|
.setMessage(getString(R.string.outofmemoryerror_msg))
|
||||||
|
.setCancelable(false)
|
||||||
|
.setPositiveButton(getString(R.string.ok)) { _, _ -> finish() }
|
||||||
|
.show()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
private fun loadLayout(fileContent: String){
|
||||||
|
if (wordWrap){
|
||||||
|
setContentView(R.layout.activity_text_editor_wrap)
|
||||||
|
} else {
|
||||||
|
setContentView(R.layout.activity_text_editor)
|
||||||
|
}
|
||||||
|
toolbar = findViewById(R.id.toolbar)
|
||||||
|
setSupportActionBar(toolbar)
|
||||||
|
title = ""
|
||||||
|
titleText = findViewById(R.id.title_text)
|
||||||
|
titleText.text = fileName
|
||||||
|
editor = findViewById(R.id.text_editor)
|
||||||
|
editor.setText(fileContent)
|
||||||
|
editor.addTextChangedListener(object: TextWatcher {
|
||||||
|
override fun afterTextChanged(s: Editable?) {
|
||||||
|
}
|
||||||
|
override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
|
||||||
|
}
|
||||||
|
override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
|
||||||
|
if (!changedSinceLastSave){
|
||||||
|
changedSinceLastSave = true
|
||||||
|
titleText.text = "*$fileName"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
private fun save(): Boolean{
|
||||||
|
var success = false
|
||||||
|
val content = editor.text.toString().toByteArray()
|
||||||
|
val handleID = gocryptfsVolume.open_write_mode(filePath)
|
||||||
|
if (handleID != -1){
|
||||||
|
val buff = ByteArrayInputStream(content)
|
||||||
|
var offset: Long = 0
|
||||||
|
val io_buffer = ByteArray(GocryptfsVolume.DefaultBS)
|
||||||
|
var length: Int
|
||||||
|
while (buff.read(io_buffer).also { length = it } > 0) {
|
||||||
|
val written = gocryptfsVolume.write_file(handleID, offset, io_buffer, length).toLong()
|
||||||
|
if (written == length.toLong()) {
|
||||||
|
offset += written
|
||||||
|
} else {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (offset == content.size.toLong()){
|
||||||
|
success = gocryptfsVolume.truncate(filePath, offset)
|
||||||
|
}
|
||||||
|
gocryptfsVolume.close_file(handleID)
|
||||||
|
buff.close()
|
||||||
|
}
|
||||||
|
if (success){
|
||||||
|
Toast.makeText(this, getString(R.string.file_saved), Toast.LENGTH_SHORT).show()
|
||||||
|
} else {
|
||||||
|
Toast.makeText(this, getString(R.string.save_failed), Toast.LENGTH_SHORT).show()
|
||||||
|
}
|
||||||
|
return success
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun checkSaveAndExit(){
|
||||||
|
if (changedSinceLastSave){
|
||||||
|
ColoredAlertDialog(this)
|
||||||
|
.setTitle(R.string.warning)
|
||||||
|
.setMessage(getString(R.string.ask_save))
|
||||||
|
.setPositiveButton(getString(R.string.save)) { _, _ ->
|
||||||
|
if (save()){
|
||||||
|
finish()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
.setNegativeButton(getString(R.string.discard)){ _, _ -> finish() }
|
||||||
|
.show()
|
||||||
|
} else {
|
||||||
|
finish()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onCreateOptionsMenu(menu: Menu): Boolean {
|
||||||
|
menuInflater.inflate(R.menu.text_editor, menu)
|
||||||
|
toolbar.setNavigationIcon(R.drawable.icon_arrow_back)
|
||||||
|
menu.findItem(R.id.word_wrap).isChecked = wordWrap
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onOptionsItemSelected(item: MenuItem): Boolean {
|
||||||
|
when (item.itemId){
|
||||||
|
android.R.id.home -> {
|
||||||
|
checkSaveAndExit()
|
||||||
|
}
|
||||||
|
R.id.menu_save -> {
|
||||||
|
if (save()){
|
||||||
|
changedSinceLastSave = false
|
||||||
|
titleText.text = fileName
|
||||||
|
}
|
||||||
|
}
|
||||||
|
R.id.word_wrap -> {
|
||||||
|
wordWrap = !item.isChecked
|
||||||
|
loadLayout(editor.text.toString())
|
||||||
|
invalidateOptionsMenu()
|
||||||
|
}
|
||||||
|
else -> super.onOptionsItemSelected(item)
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onBackPressed() {
|
||||||
|
checkSaveAndExit()
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,27 @@
|
|||||||
|
package sushi.hardcore.droidfs.file_viewers
|
||||||
|
|
||||||
|
import android.widget.MediaController
|
||||||
|
import kotlinx.android.synthetic.main.activity_video_player.*
|
||||||
|
import sushi.hardcore.droidfs.R
|
||||||
|
import sushi.hardcore.droidfs.widgets.ColoredAlertDialog
|
||||||
|
|
||||||
|
class VideoPlayer: FileViewerActivity() {
|
||||||
|
override fun viewFile() {
|
||||||
|
val mc = MediaController(this)
|
||||||
|
setContentView(R.layout.activity_video_player)
|
||||||
|
mc.setAnchorView(video_player)
|
||||||
|
video_player.setOnErrorListener { _, _, _ ->
|
||||||
|
ColoredAlertDialog(this)
|
||||||
|
.setTitle(R.string.error)
|
||||||
|
.setMessage(getString(R.string.video_play_failed))
|
||||||
|
.setCancelable(false)
|
||||||
|
.setPositiveButton(R.string.ok) { _, _ -> finish() }
|
||||||
|
.show()
|
||||||
|
true
|
||||||
|
}
|
||||||
|
val tmpFileUri = exportFile(filePath)
|
||||||
|
video_player.setVideoURI(tmpFileUri)
|
||||||
|
video_player.setMediaController(mc)
|
||||||
|
video_player.start()
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,31 @@
|
|||||||
|
package sushi.hardcore.droidfs.fingerprint_stuff
|
||||||
|
|
||||||
|
import android.content.DialogInterface
|
||||||
|
import android.os.Bundle
|
||||||
|
import android.view.LayoutInflater
|
||||||
|
import android.view.View
|
||||||
|
import android.view.ViewGroup
|
||||||
|
import android.widget.ImageView
|
||||||
|
import android.widget.TextView
|
||||||
|
import androidx.fragment.app.DialogFragment
|
||||||
|
import sushi.hardcore.droidfs.R
|
||||||
|
|
||||||
|
class FingerprintFragment(val volume_path: String, val action_description: String, val callbackOnDismiss: () -> Unit) : DialogFragment() {
|
||||||
|
lateinit var image_fingerprint: ImageView
|
||||||
|
lateinit var text_instruction: TextView
|
||||||
|
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
|
||||||
|
val view = inflater.inflate(R.layout.fragment_fingerprint, container, false)
|
||||||
|
val text_volume = view.findViewById<TextView>(R.id.text_volume)
|
||||||
|
text_volume.text = volume_path
|
||||||
|
image_fingerprint = view.findViewById(R.id.image_fingerprint)
|
||||||
|
val text_action_description = view.findViewById<TextView>(R.id.text_action_description)
|
||||||
|
text_action_description.text = action_description
|
||||||
|
text_instruction = view.findViewById(R.id.text_instruction)
|
||||||
|
return view
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onDismiss(dialog: DialogInterface) {
|
||||||
|
super.onDismiss(dialog)
|
||||||
|
callbackOnDismiss()
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,33 @@
|
|||||||
|
package sushi.hardcore.droidfs.fingerprint_stuff
|
||||||
|
|
||||||
|
import android.content.Context
|
||||||
|
import android.hardware.fingerprint.FingerprintManager
|
||||||
|
import android.os.Build
|
||||||
|
import android.os.CancellationSignal
|
||||||
|
import android.util.Log
|
||||||
|
import android.widget.Toast
|
||||||
|
import androidx.annotation.RequiresApi
|
||||||
|
|
||||||
|
@RequiresApi(Build.VERSION_CODES.M)
|
||||||
|
class FingerprintHandler(private val context: Context) : FingerprintManager.AuthenticationCallback(){
|
||||||
|
private lateinit var cancellationSignal: CancellationSignal
|
||||||
|
private lateinit var onTouched: (resultCode: onTouchedResultCodes) -> Unit
|
||||||
|
|
||||||
|
fun startAuth(fingerprintManager: FingerprintManager, cryptoObject: FingerprintManager.CryptoObject, onTouched: (resultCode: onTouchedResultCodes) -> Unit){
|
||||||
|
cancellationSignal = CancellationSignal()
|
||||||
|
this.onTouched = onTouched
|
||||||
|
fingerprintManager.authenticate(cryptoObject, cancellationSignal, 0, this, null)
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onAuthenticationSucceeded(result: FingerprintManager.AuthenticationResult?) {
|
||||||
|
onTouched(onTouchedResultCodes.SUCCEED)
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onAuthenticationError(errorCode: Int, errString: CharSequence?) {
|
||||||
|
onTouched(onTouchedResultCodes.ERROR)
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onAuthenticationFailed() {
|
||||||
|
onTouched(onTouchedResultCodes.FAILED)
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,261 @@
|
|||||||
|
package sushi.hardcore.droidfs.fingerprint_stuff
|
||||||
|
|
||||||
|
import android.Manifest
|
||||||
|
import android.app.KeyguardManager
|
||||||
|
import android.content.Context
|
||||||
|
import android.content.DialogInterface
|
||||||
|
import android.content.SharedPreferences
|
||||||
|
import android.content.pm.PackageManager
|
||||||
|
import android.hardware.biometrics.BiometricPrompt
|
||||||
|
import android.hardware.fingerprint.FingerprintManager
|
||||||
|
import android.os.Build
|
||||||
|
import android.os.CancellationSignal
|
||||||
|
import android.os.Handler
|
||||||
|
import android.security.keystore.KeyGenParameterSpec
|
||||||
|
import android.security.keystore.KeyProperties
|
||||||
|
import android.util.Base64
|
||||||
|
import android.util.Log
|
||||||
|
import android.widget.Toast
|
||||||
|
import androidx.annotation.RequiresApi
|
||||||
|
import androidx.appcompat.app.AlertDialog
|
||||||
|
import androidx.appcompat.app.AppCompatActivity
|
||||||
|
import androidx.core.content.ContextCompat
|
||||||
|
import sushi.hardcore.droidfs.ConstValues
|
||||||
|
import sushi.hardcore.droidfs.R
|
||||||
|
import sushi.hardcore.droidfs.widgets.ColoredAlertDialog
|
||||||
|
import java.security.KeyStore
|
||||||
|
import javax.crypto.*
|
||||||
|
import javax.crypto.spec.GCMParameterSpec
|
||||||
|
|
||||||
|
@RequiresApi(Build.VERSION_CODES.M)
|
||||||
|
class FingerprintPasswordHashSaver(private val activityContext: AppCompatActivity, private val shared_prefs: SharedPreferences) {
|
||||||
|
private var isPrepared = false
|
||||||
|
var isListening = false
|
||||||
|
var authenticationFailed = false
|
||||||
|
private val shared_prefs_editor: SharedPreferences.Editor = shared_prefs.edit()
|
||||||
|
private val fingerprintManager = activityContext.getSystemService(Context.FINGERPRINT_SERVICE) as FingerprintManager
|
||||||
|
private lateinit var root_cipher_dir: String
|
||||||
|
private lateinit var action_description: String
|
||||||
|
private lateinit var onAuthenticationResult: (success: Boolean) -> Unit
|
||||||
|
private lateinit var onPasswordDecrypted: (password: ByteArray) -> Unit
|
||||||
|
private lateinit var keyStore: KeyStore
|
||||||
|
private lateinit var key: SecretKey
|
||||||
|
lateinit var fingerprintFragment: FingerprintFragment
|
||||||
|
private val handler = Handler()
|
||||||
|
private lateinit var cancellationSignal: CancellationSignal
|
||||||
|
private var actionMode: Int? = null
|
||||||
|
private lateinit var dataToProcess: ByteArray
|
||||||
|
private lateinit var cipher: Cipher
|
||||||
|
companion object {
|
||||||
|
private const val ANDROID_KEY_STORE = "AndroidKeyStore"
|
||||||
|
private const val KEY_ALIAS = "Hash Key"
|
||||||
|
private const val KEY_SIZE = 256
|
||||||
|
private const val GCM_TAG_LEN = 128
|
||||||
|
private const val CIPHER_TYPE = "AES/GCM/NoPadding"
|
||||||
|
private const val SUCCESS_DISMISS_DIALOG_DELAY: Long = 400
|
||||||
|
private const val FAILED_DISMISS_DIALOG_DELAY: Long = 800
|
||||||
|
}
|
||||||
|
private fun reset_hash_storage() {
|
||||||
|
keyStore.deleteEntry(KEY_ALIAS)
|
||||||
|
val saved_volume_paths = shared_prefs.getStringSet(ConstValues.saved_volumes_key, HashSet<String>()) as Set<String>
|
||||||
|
for (path in saved_volume_paths){
|
||||||
|
val saved_hash = shared_prefs.getString(path, null)
|
||||||
|
if (saved_hash != null){
|
||||||
|
shared_prefs_editor.remove(path)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
shared_prefs_editor.apply()
|
||||||
|
Toast.makeText(activityContext, activityContext.getString(R.string.hash_storage_reset), Toast.LENGTH_SHORT).show()
|
||||||
|
}
|
||||||
|
|
||||||
|
fun canAuthenticate(): Boolean{
|
||||||
|
if (ContextCompat.checkSelfPermission(activityContext, Manifest.permission.USE_FINGERPRINT) != PackageManager.PERMISSION_GRANTED){
|
||||||
|
Toast.makeText(activityContext, activityContext.getString(R.string.fingerprint_perm_denied), Toast.LENGTH_SHORT).show()
|
||||||
|
} else if (!fingerprintManager.isHardwareDetected){
|
||||||
|
Toast.makeText(activityContext, activityContext.getString(R.string.no_fingerprint_sensor), Toast.LENGTH_SHORT).show()
|
||||||
|
} else {
|
||||||
|
val keyguardManager = activityContext.getSystemService(Context.KEYGUARD_SERVICE) as KeyguardManager
|
||||||
|
if (!keyguardManager.isKeyguardSecure || !fingerprintManager.hasEnrolledFingerprints()) {
|
||||||
|
Toast.makeText(activityContext, activityContext.getString(R.string.no_fingerprint_configured), Toast.LENGTH_SHORT).show()
|
||||||
|
} else {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
private fun prepare() {
|
||||||
|
keyStore = KeyStore.getInstance(ANDROID_KEY_STORE)
|
||||||
|
keyStore.load(null)
|
||||||
|
key = if (keyStore.containsAlias(KEY_ALIAS)){
|
||||||
|
keyStore.getKey(KEY_ALIAS, null) as SecretKey
|
||||||
|
} else {
|
||||||
|
val builder = KeyGenParameterSpec.Builder(KEY_ALIAS, KeyProperties.PURPOSE_ENCRYPT or KeyProperties.PURPOSE_DECRYPT)
|
||||||
|
builder.setBlockModes(KeyProperties.BLOCK_MODE_GCM)
|
||||||
|
builder.setKeySize(KEY_SIZE)
|
||||||
|
builder.setEncryptionPaddings(KeyProperties.ENCRYPTION_PADDING_NONE)
|
||||||
|
builder.setUserAuthenticationRequired(true)
|
||||||
|
val keyGenerator = KeyGenerator.getInstance(KeyProperties.KEY_ALGORITHM_AES, ANDROID_KEY_STORE)
|
||||||
|
keyGenerator.init(builder.build())
|
||||||
|
keyGenerator.generateKey()
|
||||||
|
}
|
||||||
|
cipher = Cipher.getInstance(CIPHER_TYPE)
|
||||||
|
fingerprintFragment = FingerprintFragment(root_cipher_dir, action_description, ::stopListening)
|
||||||
|
isPrepared = true
|
||||||
|
}
|
||||||
|
fun encryptAndSave(plainText: ByteArray, root_cipher_dir: String, onAuthenticationResult: (success: Boolean) -> Unit){
|
||||||
|
if (shared_prefs.getString(root_cipher_dir, null) == null){
|
||||||
|
this.root_cipher_dir = root_cipher_dir
|
||||||
|
this.action_description = activityContext.getString(R.string.encrypt_action_description)
|
||||||
|
this.onAuthenticationResult = onAuthenticationResult
|
||||||
|
if (!isPrepared){
|
||||||
|
prepare()
|
||||||
|
}
|
||||||
|
dataToProcess = plainText
|
||||||
|
actionMode = Cipher.ENCRYPT_MODE
|
||||||
|
cipher.init(Cipher.ENCRYPT_MODE, key)
|
||||||
|
startListening()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
fun decrypt(cipherText: String, root_cipher_dir: String, onPasswordDecrypted: (password: ByteArray) -> Unit){
|
||||||
|
this.root_cipher_dir = root_cipher_dir
|
||||||
|
this.action_description = activityContext.getString(R.string.decrypt_action_description)
|
||||||
|
this.onPasswordDecrypted = onPasswordDecrypted
|
||||||
|
if (!isPrepared){
|
||||||
|
prepare()
|
||||||
|
}
|
||||||
|
actionMode = Cipher.DECRYPT_MODE
|
||||||
|
val encodedElements = cipherText.split(":")
|
||||||
|
dataToProcess = Base64.decode(encodedElements[1], 0)
|
||||||
|
val iv = Base64.decode(encodedElements[0], 0)
|
||||||
|
val gcmSpec = GCMParameterSpec(GCM_TAG_LEN, iv)
|
||||||
|
cipher.init(Cipher.DECRYPT_MODE, key, gcmSpec)
|
||||||
|
startListening()
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun startListening(){
|
||||||
|
cancellationSignal = CancellationSignal()
|
||||||
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
|
||||||
|
val biometricPrompt = BiometricPrompt.Builder(activityContext)
|
||||||
|
.setTitle(root_cipher_dir)
|
||||||
|
.setSubtitle(action_description)
|
||||||
|
.setDescription(activityContext.getString(R.string.fingerprint_instruction))
|
||||||
|
.setNegativeButton(activityContext.getString(R.string.cancel), activityContext.mainExecutor, DialogInterface.OnClickListener{_, _ ->
|
||||||
|
cancellationSignal.cancel()
|
||||||
|
callbackOnAuthenticationFailed() //toggle on onAuthenticationResult
|
||||||
|
}).build()
|
||||||
|
biometricPrompt.authenticate(BiometricPrompt.CryptoObject(cipher), cancellationSignal, activityContext.mainExecutor, object: BiometricPrompt.AuthenticationCallback(){
|
||||||
|
override fun onAuthenticationError(errorCode: Int, errString: CharSequence?) {
|
||||||
|
callbackOnAuthenticationError()
|
||||||
|
}
|
||||||
|
override fun onAuthenticationFailed() {
|
||||||
|
callbackOnAuthenticationFailed()
|
||||||
|
}
|
||||||
|
override fun onAuthenticationSucceeded(result: BiometricPrompt.AuthenticationResult?) {
|
||||||
|
callbackOnAuthenticationSucceeded()
|
||||||
|
}
|
||||||
|
})
|
||||||
|
} else {
|
||||||
|
fingerprintFragment.show(activityContext.supportFragmentManager, null)
|
||||||
|
fingerprintManager.authenticate(FingerprintManager.CryptoObject(cipher), cancellationSignal, 0, object: FingerprintManager.AuthenticationCallback(){
|
||||||
|
override fun onAuthenticationError(errorCode: Int, errString: CharSequence?) {
|
||||||
|
callbackOnAuthenticationError()
|
||||||
|
}
|
||||||
|
override fun onAuthenticationFailed() {
|
||||||
|
callbackOnAuthenticationFailed()
|
||||||
|
}
|
||||||
|
override fun onAuthenticationSucceeded(result: FingerprintManager.AuthenticationResult?) {
|
||||||
|
callbackOnAuthenticationSucceeded()
|
||||||
|
}
|
||||||
|
}, null)
|
||||||
|
}
|
||||||
|
isListening = true
|
||||||
|
}
|
||||||
|
|
||||||
|
fun stopListening(){
|
||||||
|
cancellationSignal.cancel()
|
||||||
|
isListening = false
|
||||||
|
}
|
||||||
|
|
||||||
|
fun callbackOnAuthenticationError() {
|
||||||
|
if (!authenticationFailed){
|
||||||
|
if (fingerprintFragment.isAdded){
|
||||||
|
fingerprintFragment.image_fingerprint.setColorFilter(ContextCompat.getColor(activityContext, R.color.fingerprint_failed))
|
||||||
|
fingerprintFragment.text_instruction.setText(activityContext.getString(R.string.authentication_error))
|
||||||
|
handler.postDelayed({ fingerprintFragment.dismiss() }, 1000)
|
||||||
|
}
|
||||||
|
if (actionMode == Cipher.ENCRYPT_MODE){
|
||||||
|
handler.postDelayed({ onAuthenticationResult(false) }, FAILED_DISMISS_DIALOG_DELAY)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun callbackOnAuthenticationFailed() {
|
||||||
|
authenticationFailed = true
|
||||||
|
if (fingerprintFragment.isAdded){
|
||||||
|
fingerprintFragment.image_fingerprint.setColorFilter(ContextCompat.getColor(activityContext, R.color.fingerprint_failed))
|
||||||
|
fingerprintFragment.text_instruction.text = activityContext.getString(R.string.authentication_failed)
|
||||||
|
handler.postDelayed({ fingerprintFragment.dismiss() }, FAILED_DISMISS_DIALOG_DELAY)
|
||||||
|
stopListening()
|
||||||
|
} else {
|
||||||
|
handler.postDelayed({ stopListening() }, FAILED_DISMISS_DIALOG_DELAY)
|
||||||
|
}
|
||||||
|
if (actionMode == Cipher.ENCRYPT_MODE){
|
||||||
|
handler.postDelayed({ onAuthenticationResult(false) }, FAILED_DISMISS_DIALOG_DELAY)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun callbackOnAuthenticationSucceeded() {
|
||||||
|
if (fingerprintFragment.isAdded){
|
||||||
|
fingerprintFragment.image_fingerprint.setColorFilter(ContextCompat.getColor(activityContext, R.color.fingerprint_success))
|
||||||
|
fingerprintFragment.text_instruction.text = activityContext.getString(R.string.authenticated)
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
when (actionMode) {
|
||||||
|
Cipher.ENCRYPT_MODE -> {
|
||||||
|
val cipherText = cipher.doFinal(dataToProcess)
|
||||||
|
val encodedCipherText = Base64.encodeToString(cipherText, 0)
|
||||||
|
val encodedIv = Base64.encodeToString(cipher.iv, 0)
|
||||||
|
shared_prefs_editor.putString(root_cipher_dir, "$encodedIv:$encodedCipherText")
|
||||||
|
shared_prefs_editor.apply()
|
||||||
|
handler.postDelayed({
|
||||||
|
if (fingerprintFragment.isAdded){
|
||||||
|
fingerprintFragment.dismiss()
|
||||||
|
}
|
||||||
|
onAuthenticationResult(true)
|
||||||
|
}, SUCCESS_DISMISS_DIALOG_DELAY)
|
||||||
|
}
|
||||||
|
Cipher.DECRYPT_MODE -> {
|
||||||
|
try {
|
||||||
|
val plainText = cipher.doFinal(dataToProcess)
|
||||||
|
handler.postDelayed({
|
||||||
|
if (fingerprintFragment.isAdded){
|
||||||
|
fingerprintFragment.dismiss()
|
||||||
|
}
|
||||||
|
onPasswordDecrypted(plainText)
|
||||||
|
}, SUCCESS_DISMISS_DIALOG_DELAY)
|
||||||
|
} catch (e: AEADBadTagException){
|
||||||
|
ColoredAlertDialog(activityContext)
|
||||||
|
.setTitle(R.string.error)
|
||||||
|
.setMessage(activityContext.getString(R.string.MAC_verification_failed))
|
||||||
|
.setPositiveButton(activityContext.getString(R.string.reset_hash_storage)) { _, _ ->
|
||||||
|
reset_hash_storage()
|
||||||
|
}
|
||||||
|
.setNegativeButton(R.string.cancel, null)
|
||||||
|
.show()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (e: IllegalBlockSizeException){
|
||||||
|
stopListening()
|
||||||
|
ColoredAlertDialog(activityContext)
|
||||||
|
.setTitle(R.string.authentication_error)
|
||||||
|
.setMessage(activityContext.getString(R.string.authentication_error_msg))
|
||||||
|
.setPositiveButton(activityContext.getString(R.string.reset_hash_storage)) { _, _ ->
|
||||||
|
reset_hash_storage()
|
||||||
|
}
|
||||||
|
.setNegativeButton(R.string.cancel, null)
|
||||||
|
.show()
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,7 @@
|
|||||||
|
package sushi.hardcore.droidfs.fingerprint_stuff
|
||||||
|
|
||||||
|
enum class onTouchedResultCodes {
|
||||||
|
SUCCEED,
|
||||||
|
FAILED,
|
||||||
|
ERROR
|
||||||
|
}
|
@ -0,0 +1,326 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) 2017 Schürmann & Breitmoser GbR
|
||||||
|
*
|
||||||
|
* This program is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU General Public License as published by
|
||||||
|
* the Free Software Foundation, either version 3 of the License, or
|
||||||
|
* (at your option) any later version.
|
||||||
|
*
|
||||||
|
* This program is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License
|
||||||
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package sushi.hardcore.droidfs.provider;
|
||||||
|
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.FileNotFoundException;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.lang.reflect.InvocationTargetException;
|
||||||
|
import java.lang.reflect.Method;
|
||||||
|
import java.util.UUID;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
import java.util.regex.Matcher;
|
||||||
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
|
import android.annotation.SuppressLint;
|
||||||
|
import android.app.Application;
|
||||||
|
import android.content.ClipDescription;
|
||||||
|
import android.content.ContentProvider;
|
||||||
|
import android.content.ContentResolver;
|
||||||
|
import android.content.ContentValues;
|
||||||
|
import android.content.Context;
|
||||||
|
import android.database.Cursor;
|
||||||
|
import android.database.MatrixCursor;
|
||||||
|
import android.database.sqlite.SQLiteDatabase;
|
||||||
|
import android.database.sqlite.SQLiteOpenHelper;
|
||||||
|
import android.net.Uri;
|
||||||
|
import android.os.Build;
|
||||||
|
import android.os.ParcelFileDescriptor;
|
||||||
|
import android.provider.MediaStore;
|
||||||
|
import android.util.Log;
|
||||||
|
|
||||||
|
import androidx.annotation.NonNull;
|
||||||
|
|
||||||
|
import sushi.hardcore.droidfs.BuildConfig;
|
||||||
|
|
||||||
|
import sushi.hardcore.droidfs.util.DatabaseUtil;
|
||||||
|
import sushi.hardcore.droidfs.util.Wiper;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Borrowed from OpenKeyChain
|
||||||
|
* I removed the scheduled cleanup because it requires unwanted permissions and doesn't work very well.
|
||||||
|
* But don't panic ! The "clear_cache" function from ExternalProvider do the same job when needed.
|
||||||
|
**/
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* TemporaryStorageProvider stores decrypted files inside the app's cache directory previously to
|
||||||
|
* sharing them with other applications.
|
||||||
|
* <p/>
|
||||||
|
* Security:
|
||||||
|
* - It is writable by OpenKeychain only (see Manifest), but exported for reading files
|
||||||
|
* - It uses UUIDs as identifiers which makes predicting files from outside impossible
|
||||||
|
* - Querying a number of files is not allowed, only querying single files
|
||||||
|
* -> You can only open a file if you know the Uri containing the precise UUID, this Uri is only
|
||||||
|
* revealed when the user shares a decrypted file with another app.
|
||||||
|
* <p/>
|
||||||
|
* Why is support lib's FileProvider not used?
|
||||||
|
* Because granting Uri permissions temporarily does not work correctly. See
|
||||||
|
* - https://code.google.com/p/android/issues/detail?id=76683
|
||||||
|
* - https://github.com/nmr8acme/FileProvider-permission-bug
|
||||||
|
* - http://stackoverflow.com/q/24467696
|
||||||
|
* - http://stackoverflow.com/q/18249007
|
||||||
|
* - Comments at http://www.blogc.at/2014/03/23/share-private-files-with-other-apps-fileprovider/
|
||||||
|
*/
|
||||||
|
public class TemporaryFileProvider extends ContentProvider {
|
||||||
|
|
||||||
|
private static final int TEMPFILE_TTL = 10 * 60 * 1000; // 10 minutes
|
||||||
|
|
||||||
|
private static final String DB_NAME = "tempstorage.db";
|
||||||
|
private static final String TABLE_FILES = "files";
|
||||||
|
public static final String AUTHORITY = BuildConfig.APPLICATION_ID + ".tempstorage";
|
||||||
|
public static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY);
|
||||||
|
private static final int DB_VERSION = 3;
|
||||||
|
|
||||||
|
interface TemporaryFileColumns {
|
||||||
|
String COLUMN_UUID = "id";
|
||||||
|
String COLUMN_NAME = "name";
|
||||||
|
String COLUMN_TIME = "time";
|
||||||
|
String COLUMN_TYPE = "mimetype";
|
||||||
|
}
|
||||||
|
|
||||||
|
private static final String TEMP_FILES_DIR = "temp";
|
||||||
|
private static File tempFilesDir;
|
||||||
|
|
||||||
|
private static Pattern UUID_PATTERN = Pattern.compile("[a-fA-F0-9-]+");
|
||||||
|
|
||||||
|
public static void wipeAll(){
|
||||||
|
for (File f: tempFilesDir.listFiles()){
|
||||||
|
Wiper.wipe(f);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Uri createFile(Context context, String targetName, String mimeType) {
|
||||||
|
ContentResolver contentResolver = context.getContentResolver();
|
||||||
|
|
||||||
|
ContentValues contentValues = new ContentValues();
|
||||||
|
contentValues.put(TemporaryFileColumns.COLUMN_NAME, targetName);
|
||||||
|
contentValues.put(TemporaryFileColumns.COLUMN_TYPE, mimeType);
|
||||||
|
contentValues.put(TemporaryFileColumns.COLUMN_TIME, System.currentTimeMillis());
|
||||||
|
Uri resultUri = contentResolver.insert(CONTENT_URI, contentValues);
|
||||||
|
|
||||||
|
//scheduleCleanupAfterTtl(context);
|
||||||
|
return resultUri;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Uri createFile(Context context, String targetName) {
|
||||||
|
return createFile(context, targetName, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Uri createFile(Context context) {
|
||||||
|
ContentValues contentValues = new ContentValues();
|
||||||
|
return context.getContentResolver().insert(CONTENT_URI, contentValues);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static int setName(Context context, Uri uri, String name) {
|
||||||
|
ContentValues values = new ContentValues();
|
||||||
|
values.put(TemporaryFileColumns.COLUMN_NAME, name);
|
||||||
|
return context.getContentResolver().update(uri, values, null, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static int setMimeType(Context context, Uri uri, String mimetype) {
|
||||||
|
ContentValues values = new ContentValues();
|
||||||
|
values.put(TemporaryFileColumns.COLUMN_TYPE, mimetype);
|
||||||
|
return context.getContentResolver().update(uri, values, null, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static class TemporaryStorageDatabase extends SQLiteOpenHelper {
|
||||||
|
|
||||||
|
public TemporaryStorageDatabase(Context context) {
|
||||||
|
super(context, DB_NAME, null, DB_VERSION);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onCreate(SQLiteDatabase db) {
|
||||||
|
db.execSQL("CREATE TABLE IF NOT EXISTS " + TABLE_FILES + " (" +
|
||||||
|
TemporaryFileColumns.COLUMN_UUID + " TEXT PRIMARY KEY, " +
|
||||||
|
TemporaryFileColumns.COLUMN_NAME + " TEXT, " +
|
||||||
|
TemporaryFileColumns.COLUMN_TYPE + " TEXT, " +
|
||||||
|
TemporaryFileColumns.COLUMN_TIME + " INTEGER" +
|
||||||
|
");");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
|
||||||
|
|
||||||
|
switch (oldVersion) {
|
||||||
|
case 1:
|
||||||
|
db.execSQL("DROP TABLE IF EXISTS files");
|
||||||
|
db.execSQL("CREATE TABLE IF NOT EXISTS " + TABLE_FILES + " (" +
|
||||||
|
TemporaryFileColumns.COLUMN_UUID + " TEXT PRIMARY KEY, " +
|
||||||
|
TemporaryFileColumns.COLUMN_NAME + " TEXT, " +
|
||||||
|
TemporaryFileColumns.COLUMN_TIME + " INTEGER" +
|
||||||
|
");");
|
||||||
|
case 2:
|
||||||
|
db.execSQL("ALTER TABLE files ADD COLUMN " + TemporaryFileColumns.COLUMN_TYPE + " TEXT");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static TemporaryStorageDatabase db;
|
||||||
|
|
||||||
|
private File getFile(Uri uri) throws FileNotFoundException {
|
||||||
|
try {
|
||||||
|
return getFile(uri.getLastPathSegment());
|
||||||
|
} catch (NumberFormatException e) {
|
||||||
|
throw new FileNotFoundException();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private File getFile(String id) {
|
||||||
|
Matcher m = UUID_PATTERN.matcher(id);
|
||||||
|
if (!m.matches()) {
|
||||||
|
throw new SecurityException("Can only open temporary files with UUIDs!");
|
||||||
|
}
|
||||||
|
|
||||||
|
return new File(tempFilesDir, id);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean onCreate() {
|
||||||
|
db = new TemporaryStorageDatabase(getContext());
|
||||||
|
tempFilesDir = new File(getContext().getCacheDir(), TEMP_FILES_DIR);
|
||||||
|
return tempFilesDir.mkdirs();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
|
||||||
|
if (uri.getLastPathSegment() == null) {
|
||||||
|
throw new SecurityException("Listing temporary files is not allowed, only querying single files.");
|
||||||
|
}
|
||||||
|
|
||||||
|
File file;
|
||||||
|
try {
|
||||||
|
file = getFile(uri);
|
||||||
|
} catch (FileNotFoundException e) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
Cursor fileName = db.getReadableDatabase().query(TABLE_FILES,
|
||||||
|
new String[]{TemporaryFileColumns.COLUMN_NAME},
|
||||||
|
TemporaryFileColumns.COLUMN_UUID + "=?",
|
||||||
|
new String[]{uri.getLastPathSegment()}, null, null, null);
|
||||||
|
if (fileName != null) {
|
||||||
|
if (fileName.moveToNext()) {
|
||||||
|
MatrixCursor cursor = new MatrixCursor(new String[]{
|
||||||
|
MediaStore.MediaColumns.DISPLAY_NAME,
|
||||||
|
MediaStore.MediaColumns.SIZE,
|
||||||
|
MediaStore.MediaColumns.DATA,
|
||||||
|
});
|
||||||
|
cursor.newRow()
|
||||||
|
.add(fileName.getString(0))
|
||||||
|
.add(file.length())
|
||||||
|
.add(file.getAbsolutePath());
|
||||||
|
fileName.close();
|
||||||
|
return cursor;
|
||||||
|
}
|
||||||
|
fileName.close();
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getType(Uri uri) {
|
||||||
|
Cursor cursor = db.getReadableDatabase().query(TABLE_FILES,
|
||||||
|
new String[]{TemporaryFileColumns.COLUMN_TYPE},
|
||||||
|
TemporaryFileColumns.COLUMN_UUID + "=?",
|
||||||
|
new String[]{uri.getLastPathSegment()}, null, null, null);
|
||||||
|
if (cursor != null) {
|
||||||
|
try {
|
||||||
|
if (cursor.moveToNext()) {
|
||||||
|
if (!cursor.isNull(0)) {
|
||||||
|
return cursor.getString(0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} finally {
|
||||||
|
cursor.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return "application/octet-stream";
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String[] getStreamTypes(Uri uri, String mimeTypeFilter) {
|
||||||
|
String type = getType(uri);
|
||||||
|
if (ClipDescription.compareMimeTypes(type, mimeTypeFilter)) {
|
||||||
|
return new String[]{type};
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Uri insert(Uri uri, ContentValues values) {
|
||||||
|
String uuid = UUID.randomUUID().toString();
|
||||||
|
values.put(TemporaryFileColumns.COLUMN_UUID, uuid);
|
||||||
|
int insert = (int) db.getWritableDatabase().insert(TABLE_FILES, null, values);
|
||||||
|
if (insert == -1) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
getFile(uuid).createNewFile();
|
||||||
|
} catch (IOException e) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
return Uri.withAppendedPath(CONTENT_URI, uuid);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int delete(Uri uri, String selection, String[] selectionArgs) {
|
||||||
|
if (uri == null) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
String fileUuidFromUri = uri.getLastPathSegment();
|
||||||
|
if (fileUuidFromUri != null) {
|
||||||
|
selection = DatabaseUtil.concatenateWhere(selection, TemporaryFileColumns.COLUMN_UUID + "=?");
|
||||||
|
selectionArgs = DatabaseUtil.appendSelectionArgs(selectionArgs, new String[]{ fileUuidFromUri });
|
||||||
|
}
|
||||||
|
|
||||||
|
Cursor files = db.getReadableDatabase().query(TABLE_FILES, new String[]{TemporaryFileColumns.COLUMN_UUID}, selection,
|
||||||
|
selectionArgs, null, null, null);
|
||||||
|
if (files != null) {
|
||||||
|
while (files.moveToNext()) {
|
||||||
|
getFile(files.getString(0)).delete();
|
||||||
|
}
|
||||||
|
files.close();
|
||||||
|
return db.getWritableDatabase().delete(TABLE_FILES, selection, selectionArgs);
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
|
||||||
|
if (values.size() != 1) {
|
||||||
|
throw new UnsupportedOperationException("Update supported only for one field at a time!");
|
||||||
|
}
|
||||||
|
if (!values.containsKey(TemporaryFileColumns.COLUMN_NAME) && !values.containsKey(TemporaryFileColumns.COLUMN_TYPE)) {
|
||||||
|
throw new UnsupportedOperationException("Update supported only for name and type field!");
|
||||||
|
}
|
||||||
|
if (selection != null || selectionArgs != null) {
|
||||||
|
throw new UnsupportedOperationException("Update supported only for plain uri!");
|
||||||
|
}
|
||||||
|
return db.getWritableDatabase().update(TABLE_FILES, values,
|
||||||
|
TemporaryFileColumns.COLUMN_UUID + " = ?", new String[]{uri.getLastPathSegment()});
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public ParcelFileDescriptor openFile(Uri uri, String mode) throws FileNotFoundException {
|
||||||
|
return openFileHelper(uri, mode);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,84 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) 2017 Schürmann & Breitmoser GbR
|
||||||
|
*
|
||||||
|
* This program is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU General Public License as published by
|
||||||
|
* the Free Software Foundation, either version 3 of the License, or
|
||||||
|
* (at your option) any later version.
|
||||||
|
*
|
||||||
|
* This program is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License
|
||||||
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package sushi.hardcore.droidfs.util;
|
||||||
|
|
||||||
|
|
||||||
|
import androidx.sqlite.db.SupportSQLiteDatabase;
|
||||||
|
import android.database.Cursor;
|
||||||
|
import android.text.TextUtils;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Borrowed from OpenKeyChain
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Shamelessly copied from android.database.DatabaseUtils
|
||||||
|
*/
|
||||||
|
public class DatabaseUtil {
|
||||||
|
/**
|
||||||
|
* Concatenates two SQL WHERE clauses, handling empty or null values.
|
||||||
|
*/
|
||||||
|
public static String concatenateWhere(String a, String b) {
|
||||||
|
if (TextUtils.isEmpty(a)) {
|
||||||
|
return b;
|
||||||
|
}
|
||||||
|
if (TextUtils.isEmpty(b)) {
|
||||||
|
return a;
|
||||||
|
}
|
||||||
|
|
||||||
|
return "(" + a + ") AND (" + b + ")";
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Appends one set of selection args to another. This is useful when adding a selection
|
||||||
|
* argument to a user provided set.
|
||||||
|
*/
|
||||||
|
public static String[] appendSelectionArgs(String[] originalValues, String[] newValues) {
|
||||||
|
if (originalValues == null || originalValues.length == 0) {
|
||||||
|
return newValues;
|
||||||
|
}
|
||||||
|
String[] result = new String[originalValues.length + newValues.length ];
|
||||||
|
System.arraycopy(originalValues, 0, result, 0, originalValues.length);
|
||||||
|
System.arraycopy(newValues, 0, result, originalValues.length, newValues.length);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void explainQuery(SupportSQLiteDatabase db, String sql) {
|
||||||
|
Cursor explainCursor = db.query("EXPLAIN QUERY PLAN " + sql, new String[0]);
|
||||||
|
|
||||||
|
// this is a debugging feature, we can be a little careless
|
||||||
|
explainCursor.moveToFirst();
|
||||||
|
|
||||||
|
StringBuilder line = new StringBuilder();
|
||||||
|
for (int i = 0; i < explainCursor.getColumnCount(); i++) {
|
||||||
|
line.append(explainCursor.getColumnName(i)).append(", ");
|
||||||
|
}
|
||||||
|
|
||||||
|
while (!explainCursor.isAfterLast()) {
|
||||||
|
line = new StringBuilder();
|
||||||
|
for (int i = 0; i < explainCursor.getColumnCount(); i++) {
|
||||||
|
line.append(explainCursor.getString(i)).append(", ");
|
||||||
|
}
|
||||||
|
explainCursor.moveToNext();
|
||||||
|
}
|
||||||
|
|
||||||
|
explainCursor.close();
|
||||||
|
}
|
||||||
|
}
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in New Issue
Block a user