Full Code of YuZhang/cryptography for AI

master 6423e8c6ff14 cached
176 files
737.1 KB
324.2k tokens
1 requests
Download .txt
Showing preview only (890K chars total). Download the full file or copy to clipboard to get everything.
Repository: YuZhang/cryptography
Branch: master
Commit: 6423e8c6ff14
Files: 176
Total size: 737.1 KB

Directory structure:
gitextract_ii66oz9u/

├── .gitignore
├── LICENSE.txt
├── Makefile
├── README.md
├── homework/
│   ├── amspset.cls
│   ├── hw1.tex
│   ├── hw2.tex
│   ├── hw3.tex
│   ├── hw4.tex
│   ├── hw5.tex
│   └── hw6.tex
├── lab/
│   ├── lab1.docx
│   ├── lab2.docx
│   ├── lab3.docx
│   └── lab4.docx
├── misc/
│   ├── cryptographers/
│   │   └── cryptographers.key
│   ├── gallery/
│   │   ├── frame.tex
│   │   ├── gallery.tex
│   │   └── gen-gallery.sh
│   └── tikztest/
│       └── tikz-test.tex
├── notes-Chinese/
│   ├── 0 课程介绍.md
│   ├── 1 导论.md
│   ├── 10 密码学协议动物园.md
│   ├── 2 完美保密.md
│   ├── 3.1 私钥加密与伪随机性-第一部分.md
│   ├── 3.2 私钥加密与伪随机性 第二部分.md
│   ├── 4 伪随机排列的实践构造-块密码.md
│   ├── 5 伪随机对象的理论构造.md
│   ├── 6 消息认证码与抗碰撞哈希函数.md
│   ├── 7 CCA安全与认证加密.md
│   ├── 8.1 公钥加密理论.md
│   ├── 8.2 RSA问题与加密.md
│   ├── 8.3 DH问题与加密.md
│   ├── 9 数字签名.md
│   └── README.md
├── source/
│   ├── 0intro.tex
│   ├── 10protocols.tex
│   ├── 11summary.tex
│   ├── 12backup.tex
│   ├── 1introduction.tex
│   ├── 2perfectlysecret.tex
│   ├── 3.1privatekey.tex
│   ├── 3.2privatekey.tex
│   ├── 4blockcipher.tex
│   ├── 5owf.tex
│   ├── 6mac-crhf.tex
│   ├── 7cca-ae.tex
│   ├── 8.1pubkey.tex
│   ├── 8.2RSA.tex
│   ├── 8.3DH.tex
│   ├── 9digital-sig.tex
│   ├── crush-course.tex
│   └── header/
│       ├── frame.tex
│       ├── handout.tex
│       └── main.tex
└── tikz/
    ├── 1outof2.tex
    ├── 3ballot.tex
    ├── 3parties-DHKE.tex
    ├── 3parties-JOUX.tex
    ├── CBC-MAC.tex
    ├── CBC-small.tex
    ├── CBC.tex
    ├── CCA-PKCS.tex
    ├── CCA.tex
    ├── CMAC.tex
    ├── CTR.tex
    ├── DESkey.tex
    ├── DHkey.tex
    ├── Davies-Meyer.tex
    ├── ECB.tex
    ├── ElGamal.tex
    ├── HMAC.tex
    ├── IBE.tex
    ├── KDC.tex
    ├── MDtransform.tex
    ├── MS-PPTP.tex
    ├── Miyaguchi-Preneel.tex
    ├── NMAC.tex
    ├── OAEP-plus.tex
    ├── OAEP.tex
    ├── OFB.tex
    ├── OWF.tex
    ├── SAEP-plus.tex
    ├── SIV-CTR.tex
    ├── TDES.tex
    ├── VCBC-MAC.tex
    ├── alice.tex
    ├── attack-spn.tex
    ├── authentication.tex
    ├── baby-giant.tex
    ├── bilinear-map.tex
    ├── birthdayattack.tex
    ├── blindsignature.tex
    ├── cPRF.tex
    ├── certificates.tex
    ├── chain-sig.tex
    ├── cipher-stealing.tex
    ├── coinflipping.tex
    ├── collision.tex
    ├── combination.tex
    ├── compute-sec.tex
    ├── constructD.tex
    ├── des.tex
    ├── desx.tex
    ├── differential.tex
    ├── digitalsignature.tex
    ├── dining.tex
    ├── doubleE.tex
    ├── ePRG.tex
    ├── ecdhke.tex
    ├── elgamal-con.tex
    ├── ellipticcurve.tex
    ├── encryptionwithpf.tex
    ├── encryptionwithpg.tex
    ├── feistel-prp.tex
    ├── feistel.tex
    ├── hash.tex
    ├── hcp.tex
    ├── hs-reduce-1.tex
    ├── hs-reduce-2.tex
    ├── hybrid-enc-proof.tex
    ├── hybrid-encrypt.tex
    ├── hybrideg.tex
    ├── hybridproof.tex
    ├── identification-schnorr.tex
    ├── identification.tex
    ├── integrity.tex
    ├── interlock.tex
    ├── keyed-func.tex
    ├── linear.tex
    ├── mac.tex
    ├── macforge-exp.tex
    ├── macwithprf.tex
    ├── man-in-middle.tex
    ├── meet-in-middle.tex
    ├── multiple-enc-exp.tex
    ├── outfile
    ├── owff.tex
    ├── owfover.tex
    ├── padding-oracle-lastbyte.tex
    ├── padding-oracle-null.tex
    ├── padding-oracle.tex
    ├── pgfD.tex
    ├── pgfMAC.tex
    ├── pnp.tex
    ├── prg-distinguisher.tex
    ├── prg-sparse.tex
    ├── pri-cpa-exp.tex
    ├── pri-eav-exp.tex
    ├── private-key.tex
    ├── public-key.tex
    ├── qr-qnr.tex
    ├── rabinOT.tex
    ├── reduction-prg.tex
    ├── reduction.tex
    ├── schnorr-signature.tex
    ├── schnorr.tex
    ├── spn.tex
    ├── ssl.tex
    ├── suf-mac.tex
    ├── synchronizedmode.tex
    ├── tdp-cca1.tex
    ├── tdp-cca2.tex
    ├── tdp-cpa.tex
    ├── tdp-pk.tex
    ├── threepass.tex
    ├── tls13hs.tex
    ├── toy-OT.tex
    ├── transmission.tex
    ├── trapdoor.tex
    ├── tree-sig.tex
    ├── worldofpk.tex
    ├── zkp-commitment.tex
    ├── zkp-hanmilton.tex
    ├── zkp-rsa.tex
    └── zkp.tex

================================================
FILE CONTENTS
================================================

================================================
FILE: .gitignore
================================================
*.nav
*.out
*.snm
*.toc
*.log
*.aux
*.swp
*.vrb
*.gz
*.DS_Store
.vscode/*
*.fls
*.fdb_latexmk
*tikz-test.pdf

================================================
FILE: LICENSE.txt
================================================
Attribution-ShareAlike 4.0 International

=======================================================================

Creative Commons Corporation ("Creative Commons") is not a law firm and
does not provide legal services or legal advice. Distribution of
Creative Commons public licenses does not create a lawyer-client or
other relationship. Creative Commons makes its licenses and related
information available on an "as-is" basis. Creative Commons gives no
warranties regarding its licenses, any material licensed under their
terms and conditions, or any related information. Creative Commons
disclaims all liability for damages resulting from their use to the
fullest extent possible.

Using Creative Commons Public Licenses

Creative Commons public licenses provide a standard set of terms and
conditions that creators and other rights holders may use to share
original works of authorship and other material subject to copyright
and certain other rights specified in the public license below. The
following considerations are for informational purposes only, are not
exhaustive, and do not form part of our licenses.

     Considerations for licensors: Our public licenses are
     intended for use by those authorized to give the public
     permission to use material in ways otherwise restricted by
     copyright and certain other rights. Our licenses are
     irrevocable. Licensors should read and understand the terms
     and conditions of the license they choose before applying it.
     Licensors should also secure all rights necessary before
     applying our licenses so that the public can reuse the
     material as expected. Licensors should clearly mark any
     material not subject to the license. This includes other CC-
     licensed material, or material used under an exception or
     limitation to copyright. More considerations for licensors:
	wiki.creativecommons.org/Considerations_for_licensors

     Considerations for the public: By using one of our public
     licenses, a licensor grants the public permission to use the
     licensed material under specified terms and conditions. If
     the licensor's permission is not necessary for any reason--for
     example, because of any applicable exception or limitation to
     copyright--then that use is not regulated by the license. Our
     licenses grant only permissions under copyright and certain
     other rights that a licensor has authority to grant. Use of
     the licensed material may still be restricted for other
     reasons, including because others have copyright or other
     rights in the material. A licensor may make special requests,
     such as asking that all changes be marked or described.
     Although not required by our licenses, you are encouraged to
     respect those requests where reasonable. More_considerations
     for the public:
	wiki.creativecommons.org/Considerations_for_licensees

=======================================================================

Creative Commons Attribution-ShareAlike 4.0 International Public
License

By exercising the Licensed Rights (defined below), You accept and agree
to be bound by the terms and conditions of this Creative Commons
Attribution-ShareAlike 4.0 International Public License ("Public
License"). To the extent this Public License may be interpreted as a
contract, You are granted the Licensed Rights in consideration of Your
acceptance of these terms and conditions, and the Licensor grants You
such rights in consideration of benefits the Licensor receives from
making the Licensed Material available under these terms and
conditions.


Section 1 -- Definitions.

  a. Adapted Material means material subject to Copyright and Similar
     Rights that is derived from or based upon the Licensed Material
     and in which the Licensed Material is translated, altered,
     arranged, transformed, or otherwise modified in a manner requiring
     permission under the Copyright and Similar Rights held by the
     Licensor. For purposes of this Public License, where the Licensed
     Material is a musical work, performance, or sound recording,
     Adapted Material is always produced where the Licensed Material is
     synched in timed relation with a moving image.

  b. Adapter's License means the license You apply to Your Copyright
     and Similar Rights in Your contributions to Adapted Material in
     accordance with the terms and conditions of this Public License.

  c. BY-SA Compatible License means a license listed at
     creativecommons.org/compatiblelicenses, approved by Creative
     Commons as essentially the equivalent of this Public License.

  d. Copyright and Similar Rights means copyright and/or similar rights
     closely related to copyright including, without limitation,
     performance, broadcast, sound recording, and Sui Generis Database
     Rights, without regard to how the rights are labeled or
     categorized. For purposes of this Public License, the rights
     specified in Section 2(b)(1)-(2) are not Copyright and Similar
     Rights.

  e. Effective Technological Measures means those measures that, in the
     absence of proper authority, may not be circumvented under laws
     fulfilling obligations under Article 11 of the WIPO Copyright
     Treaty adopted on December 20, 1996, and/or similar international
     agreements.

  f. Exceptions and Limitations means fair use, fair dealing, and/or
     any other exception or limitation to Copyright and Similar Rights
     that applies to Your use of the Licensed Material.

  g. License Elements means the license attributes listed in the name
     of a Creative Commons Public License. The License Elements of this
     Public License are Attribution and ShareAlike.

  h. Licensed Material means the artistic or literary work, database,
     or other material to which the Licensor applied this Public
     License.

  i. Licensed Rights means the rights granted to You subject to the
     terms and conditions of this Public License, which are limited to
     all Copyright and Similar Rights that apply to Your use of the
     Licensed Material and that the Licensor has authority to license.

  j. Licensor means the individual(s) or entity(ies) granting rights
     under this Public License.

  k. Share means to provide material to the public by any means or
     process that requires permission under the Licensed Rights, such
     as reproduction, public display, public performance, distribution,
     dissemination, communication, or importation, and to make material
     available to the public including in ways that members of the
     public may access the material from a place and at a time
     individually chosen by them.

  l. Sui Generis Database Rights means rights other than copyright
     resulting from Directive 96/9/EC of the European Parliament and of
     the Council of 11 March 1996 on the legal protection of databases,
     as amended and/or succeeded, as well as other essentially
     equivalent rights anywhere in the world.

  m. You means the individual or entity exercising the Licensed Rights
     under this Public License. Your has a corresponding meaning.


Section 2 -- Scope.

  a. License grant.

       1. Subject to the terms and conditions of this Public License,
          the Licensor hereby grants You a worldwide, royalty-free,
          non-sublicensable, non-exclusive, irrevocable license to
          exercise the Licensed Rights in the Licensed Material to:

            a. reproduce and Share the Licensed Material, in whole or
               in part; and

            b. produce, reproduce, and Share Adapted Material.

       2. Exceptions and Limitations. For the avoidance of doubt, where
          Exceptions and Limitations apply to Your use, this Public
          License does not apply, and You do not need to comply with
          its terms and conditions.

       3. Term. The term of this Public License is specified in Section
          6(a).

       4. Media and formats; technical modifications allowed. The
          Licensor authorizes You to exercise the Licensed Rights in
          all media and formats whether now known or hereafter created,
          and to make technical modifications necessary to do so. The
          Licensor waives and/or agrees not to assert any right or
          authority to forbid You from making technical modifications
          necessary to exercise the Licensed Rights, including
          technical modifications necessary to circumvent Effective
          Technological Measures. For purposes of this Public License,
          simply making modifications authorized by this Section 2(a)
          (4) never produces Adapted Material.

       5. Downstream recipients.

            a. Offer from the Licensor -- Licensed Material. Every
               recipient of the Licensed Material automatically
               receives an offer from the Licensor to exercise the
               Licensed Rights under the terms and conditions of this
               Public License.

            b. Additional offer from the Licensor -- Adapted Material.
               Every recipient of Adapted Material from You
               automatically receives an offer from the Licensor to
               exercise the Licensed Rights in the Adapted Material
               under the conditions of the Adapter's License You apply.

            c. No downstream restrictions. You may not offer or impose
               any additional or different terms or conditions on, or
               apply any Effective Technological Measures to, the
               Licensed Material if doing so restricts exercise of the
               Licensed Rights by any recipient of the Licensed
               Material.

       6. No endorsement. Nothing in this Public License constitutes or
          may be construed as permission to assert or imply that You
          are, or that Your use of the Licensed Material is, connected
          with, or sponsored, endorsed, or granted official status by,
          the Licensor or others designated to receive attribution as
          provided in Section 3(a)(1)(A)(i).

  b. Other rights.

       1. Moral rights, such as the right of integrity, are not
          licensed under this Public License, nor are publicity,
          privacy, and/or other similar personality rights; however, to
          the extent possible, the Licensor waives and/or agrees not to
          assert any such rights held by the Licensor to the limited
          extent necessary to allow You to exercise the Licensed
          Rights, but not otherwise.

       2. Patent and trademark rights are not licensed under this
          Public License.

       3. To the extent possible, the Licensor waives any right to
          collect royalties from You for the exercise of the Licensed
          Rights, whether directly or through a collecting society
          under any voluntary or waivable statutory or compulsory
          licensing scheme. In all other cases the Licensor expressly
          reserves any right to collect such royalties.


Section 3 -- License Conditions.

Your exercise of the Licensed Rights is expressly made subject to the
following conditions.

  a. Attribution.

       1. If You Share the Licensed Material (including in modified
          form), You must:

            a. retain the following if it is supplied by the Licensor
               with the Licensed Material:

                 i. identification of the creator(s) of the Licensed
                    Material and any others designated to receive
                    attribution, in any reasonable manner requested by
                    the Licensor (including by pseudonym if
                    designated);

                ii. a copyright notice;

               iii. a notice that refers to this Public License;

                iv. a notice that refers to the disclaimer of
                    warranties;

                 v. a URI or hyperlink to the Licensed Material to the
                    extent reasonably practicable;

            b. indicate if You modified the Licensed Material and
               retain an indication of any previous modifications; and

            c. indicate the Licensed Material is licensed under this
               Public License, and include the text of, or the URI or
               hyperlink to, this Public License.

       2. You may satisfy the conditions in Section 3(a)(1) in any
          reasonable manner based on the medium, means, and context in
          which You Share the Licensed Material. For example, it may be
          reasonable to satisfy the conditions by providing a URI or
          hyperlink to a resource that includes the required
          information.

       3. If requested by the Licensor, You must remove any of the
          information required by Section 3(a)(1)(A) to the extent
          reasonably practicable.

  b. ShareAlike.

     In addition to the conditions in Section 3(a), if You Share
     Adapted Material You produce, the following conditions also apply.

       1. The Adapter's License You apply must be a Creative Commons
          license with the same License Elements, this version or
          later, or a BY-SA Compatible License.

       2. You must include the text of, or the URI or hyperlink to, the
          Adapter's License You apply. You may satisfy this condition
          in any reasonable manner based on the medium, means, and
          context in which You Share Adapted Material.

       3. You may not offer or impose any additional or different terms
          or conditions on, or apply any Effective Technological
          Measures to, Adapted Material that restrict exercise of the
          rights granted under the Adapter's License You apply.


Section 4 -- Sui Generis Database Rights.

Where the Licensed Rights include Sui Generis Database Rights that
apply to Your use of the Licensed Material:

  a. for the avoidance of doubt, Section 2(a)(1) grants You the right
     to extract, reuse, reproduce, and Share all or a substantial
     portion of the contents of the database;

  b. if You include all or a substantial portion of the database
     contents in a database in which You have Sui Generis Database
     Rights, then the database in which You have Sui Generis Database
     Rights (but not its individual contents) is Adapted Material,

     including for purposes of Section 3(b); and
  c. You must comply with the conditions in Section 3(a) if You Share
     all or a substantial portion of the contents of the database.

For the avoidance of doubt, this Section 4 supplements and does not
replace Your obligations under this Public License where the Licensed
Rights include other Copyright and Similar Rights.


Section 5 -- Disclaimer of Warranties and Limitation of Liability.

  a. UNLESS OTHERWISE SEPARATELY UNDERTAKEN BY THE LICENSOR, TO THE
     EXTENT POSSIBLE, THE LICENSOR OFFERS THE LICENSED MATERIAL AS-IS
     AND AS-AVAILABLE, AND MAKES NO REPRESENTATIONS OR WARRANTIES OF
     ANY KIND CONCERNING THE LICENSED MATERIAL, WHETHER EXPRESS,
     IMPLIED, STATUTORY, OR OTHER. THIS INCLUDES, WITHOUT LIMITATION,
     WARRANTIES OF TITLE, MERCHANTABILITY, FITNESS FOR A PARTICULAR
     PURPOSE, NON-INFRINGEMENT, ABSENCE OF LATENT OR OTHER DEFECTS,
     ACCURACY, OR THE PRESENCE OR ABSENCE OF ERRORS, WHETHER OR NOT
     KNOWN OR DISCOVERABLE. WHERE DISCLAIMERS OF WARRANTIES ARE NOT
     ALLOWED IN FULL OR IN PART, THIS DISCLAIMER MAY NOT APPLY TO YOU.

  b. TO THE EXTENT POSSIBLE, IN NO EVENT WILL THE LICENSOR BE LIABLE
     TO YOU ON ANY LEGAL THEORY (INCLUDING, WITHOUT LIMITATION,
     NEGLIGENCE) OR OTHERWISE FOR ANY DIRECT, SPECIAL, INDIRECT,
     INCIDENTAL, CONSEQUENTIAL, PUNITIVE, EXEMPLARY, OR OTHER LOSSES,
     COSTS, EXPENSES, OR DAMAGES ARISING OUT OF THIS PUBLIC LICENSE OR
     USE OF THE LICENSED MATERIAL, EVEN IF THE LICENSOR HAS BEEN
     ADVISED OF THE POSSIBILITY OF SUCH LOSSES, COSTS, EXPENSES, OR
     DAMAGES. WHERE A LIMITATION OF LIABILITY IS NOT ALLOWED IN FULL OR
     IN PART, THIS LIMITATION MAY NOT APPLY TO YOU.

  c. The disclaimer of warranties and limitation of liability provided
     above shall be interpreted in a manner that, to the extent
     possible, most closely approximates an absolute disclaimer and
     waiver of all liability.


Section 6 -- Term and Termination.

  a. This Public License applies for the term of the Copyright and
     Similar Rights licensed here. However, if You fail to comply with
     this Public License, then Your rights under this Public License
     terminate automatically.

  b. Where Your right to use the Licensed Material has terminated under
     Section 6(a), it reinstates:

       1. automatically as of the date the violation is cured, provided
          it is cured within 30 days of Your discovery of the
          violation; or

       2. upon express reinstatement by the Licensor.

     For the avoidance of doubt, this Section 6(b) does not affect any
     right the Licensor may have to seek remedies for Your violations
     of this Public License.

  c. For the avoidance of doubt, the Licensor may also offer the
     Licensed Material under separate terms or conditions or stop
     distributing the Licensed Material at any time; however, doing so
     will not terminate this Public License.

  d. Sections 1, 5, 6, 7, and 8 survive termination of this Public
     License.


Section 7 -- Other Terms and Conditions.

  a. The Licensor shall not be bound by any additional or different
     terms or conditions communicated by You unless expressly agreed.

  b. Any arrangements, understandings, or agreements regarding the
     Licensed Material not stated herein are separate from and
     independent of the terms and conditions of this Public License.


Section 8 -- Interpretation.

  a. For the avoidance of doubt, this Public License does not, and
     shall not be interpreted to, reduce, limit, restrict, or impose
     conditions on any use of the Licensed Material that could lawfully
     be made without permission under this Public License.

  b. To the extent possible, if any provision of this Public License is
     deemed unenforceable, it shall be automatically reformed to the
     minimum extent necessary to make it enforceable. If the provision
     cannot be reformed, it shall be severed from this Public License
     without affecting the enforceability of the remaining terms and
     conditions.

  c. No term or condition of this Public License will be waived and no
     failure to comply consented to unless expressly agreed to by the
     Licensor.

  d. Nothing in this Public License constitutes or may be interpreted
     as a limitation upon, or waiver of, any privileges and immunities
     that apply to the Licensor or You, including from the legal
     processes of any jurisdiction or authority.


=======================================================================

Creative Commons is not a party to its public
licenses. Notwithstanding, Creative Commons may elect to apply one of
its public licenses to material it publishes and in those instances
will be considered the “Licensor.” The text of the Creative Commons
public licenses is dedicated to the public domain under the CC0 Public
Domain Dedication. Except for the limited purpose of indicating that
material is shared under a Creative Commons public license or as
otherwise permitted by the Creative Commons policies published at
creativecommons.org/policies, Creative Commons does not authorize the
use of the trademark "Creative Commons" or any other trademark or logo
of Creative Commons without its prior written consent including,
without limitation, in connection with any unauthorized modifications
to any of its public licenses or any other arrangements,
understandings, or agreements concerning use of licensed material. For
the avoidance of doubt, this paragraph does not form part of the
public licenses.

Creative Commons may be contacted at creativecommons.org.


================================================
FILE: Makefile
================================================

TEX = pdflatex

OPT= -output-directory pdf

BUILD = ${TEX} ${OPT} $< ; ${TEX} ${OPT} $< ;

SRCS := $(wildcard source/*.tex)
PDFS := $(SRCS:source/%.tex=pdf/%.pdf)

all: ${PDFS}

pdf/%.pdf : source/%.tex
	${BUILD}

.PHONY: all clean 

gallery.pdf: #misc/gallery/gallery.tex
	sh misc/gallery/gen-gallery.sh > misc/gallery/gallery.tex;
	${TEX} -output-directory misc/gallery misc/gallery/gallery.tex

clean:
	/bin/rm -rf *.log *.nav *.out *.snm *.synctex.gz *.toc *.aux tikz/*.log *.vrb
	cd source && /bin/rm -rf *.log *.nav *.out *.snm *.synctex.gz *.toc *.aux *.vrb
	cd pdf && /bin/rm -rf *.log *.nav *.out *.snm *.synctex.gz *.toc *.aux *.vrb
	cd homework && /bin/rm -rf *.log *.nav *.out *.snm *.synctex.gz *.toc *.aux *.vrb


================================================
FILE: README.md
================================================
## Cryptography Course Slides

[![made-with-latex](https://img.shields.io/badge/Made%20with-LaTeX-1f425f.svg)](https://www.latex-project.org/)  [![CC BY-SA 4.0][cc-by-sa-shield]][cc-by-sa]

Yu Zhang, Harbin Insitute of Technology, 2011, 2012, 2014, 2015, 2016, 2017, 2018, 2019, 2020, 2021, 2022, 2023, 2024

- New! 2024.04 增加了实验指导书,在[lab](lab/)目录下
- New! 2024.04 增加了中文PPT,在[slides-Chinese](slides-Chinese/)目录下
- New! 2020.12 Alice, Bob, Evil and Charlie have new avatars
- New! 2020.09 增加了中文课程讲义,在[notes-Chinese](notes-Chinese/)目录下

## Requirement for compiling slides

* Developed with [MacTeX](https://www.tug.org/mactex/)
* run `make` to compile slides, which will be in [/pdf](/pdf)
* run `make gallery.pdf` to generate [the gallery of tikz diagrams](/misc/gallery/gallery.pdf)

## References
* Textbook: [Introduction to Modern Cryptography](http://www.cs.umd.edu/~jkatz/imc.html) by Jonathan Katz and Yehuda Lindell
* MOOC: [Cryptography @Coursera (Stanford)](https://crypto.stanford.edu/~dabo/courses/OnlineCrypto/) by Prof. Dan Boneh

## License

This work is licensed under a [Creative Commons Attribution-ShareAlike 4.0 International License][cc-by-sa].

[![CC BY-SA 4.0][cc-by-sa-image]][cc-by-sa]

[cc-by-sa]: http://creativecommons.org/licenses/by-sa/4.0/
[cc-by-sa-image]: https://licensebuttons.net/l/by-sa/4.0/88x31.png
[cc-by-sa-shield]: https://img.shields.io/badge/License-CC%20BY--SA%204.0-lightgrey.svg



================================================
FILE: homework/amspset.cls
================================================
% AMS Dept HW class file
% v0.05 by Eric Harley, 07 Sept 2005
% forked from HMC Math dept HW class file v0.04, 07 Sept 2005 developed by Eric J. Malm.
%%% IDENTIFICATION --------------------------------------------------------
\NeedsTeXFormat{LaTeX2e}[1995/01/01]
\ProvidesClass{amspset}
    [2005/09/07 v0.05 AMS Dept problem set class]

%%% INITIAL CODE ----------------------------------------------------------
%% Flag for compilation to PDF
\newif\ifpdfh
\ifx\pdfoutput\undefined
    \pdffalse
\else
    \pdfoutput = 1
    \pdftrue
\fi

%%% DECLARATION OF OPTIONS ------------------------------------------------
%% Header Options: header*, no header
\newif\ifamspset@header

% no header block in upper right hand corner
\DeclareOption{noheader}{%
    \amspset@headerfalse%
}

% do print header block
\DeclareOption{header}{%
    \amspset@headertrue%
}

%% Font Options: palatino*, cm
\newif\ifamspset@palatino

% use palatino fonts
\DeclareOption{palatino}{%
    \amspset@palatinotrue%
}

% use compuer modern fonts
\DeclareOption{cm}{%
    \amspset@palatinofalse%
}

%% Problem Boxing: boxed*, unboxed
\newif\ifamspset@boxed

% box problem statements
\DeclareOption{boxed}{%
    \amspset@boxedtrue%
}
   
% don't box problem statements
\DeclareOption{unboxed}{%
    \amspset@boxedfalse%
}
   
% pass remaining options to article class
\DeclareOption*{\PassOptionsToClass{\CurrentOption}{article}}

%%% EXECUTION OF OPTIONS --------------------------------------------------
%% default to:
% including header, 
% loading mathpazo package for palatino fonts,
% boxing problem statements
\ExecuteOptions{header,palatino,boxed}

\ProcessOptions

%%% PACKAGE LOADING -------------------------------------------------------
%% based on std article class
\LoadClass{article}

%% Font loading: Palatino text/math fonts
\ifamspset@palatino
    \RequirePackage{mathpazo}
\fi

%% AMSLaTeX math environments and symbols
\RequirePackage{amsmath}
\RequirePackage{amssymb}

%% boxed minipage for boxed problem environment
\RequirePackage{boxedminipage}

%%% MAIN CODE -------------------------------------------------------------
%% Tell dvips/pdflatex correct page size
\ifpdfh
  \AtBeginDocument{%
    \setlength{\pdfpageheight}{\paperheight}%
    \setlength{\pdfpagewidth}{\paperwidth}%
  }
\else
  \AtBeginDvi{\special{papersize=\the\paperwidth,\the\paperheight}}%
\fi


%% Problem set environments
% boxed problem environment
\newenvironment{problem}[1][]{%
  \ifamspset@boxed\def\amspset@probenv{boxed}\else\def\amspset@probenv{}\fi%
  \bigskip% put space before problem statement box %
  \noindent\begin{\amspset@probenv minipage}{\columnwidth}%
  \def\@tempa{#1}%
  \ifx\@tempa\empty\else%
    \amspset@probformat{#1}\hspace{0.5em}%
  \fi%
}{%
  \end{\amspset@probenv minipage}%
}
% display optional argument to problem in bold
\let\amspset@probformat\textbf

% solution environment with endmark and optional argument
\newenvironment{solution}[1][]{%
  \begin{trivlist}%
  \def\@tempa{#1}%
  \ifx\@tempa\empty%
    \item[]%
  \else%
    \item[\hskip\labelsep\relax #1]%
  \fi%
}{%
  \mbox{}\penalty10000\hfill\amspset@endmark%
  \end{trivlist}%
}

% default endmark is small black square
\def\amspset@endmark{\ensuremath{\scriptscriptstyle\blacksquare}}

%% Problem set list, for top of document
\newcommand{\problemlist}[1]{\begin{center}\large\sffamily{#1}\end{center}}

%% commands for upper-right id header block
\newcommand{\headerblock}{%
\begin{flushright}
\mbox{\amspset@name}\protect\\
\mbox{\amspset@class}\protect\\
\mbox{\amspset@assignment}\protect\\
\amspset@duedate%
\ifx\amspset@extraline\empty\else\protect\\\amspset@extraline\fi%
\end{flushright}%
}

% put id header block at start of document
\ifamspset@header\AtBeginDocument{\headerblock}\fi

% internal state for headerblock
\def\amspset@name{}
\def\amspset@class{}
\def\amspset@assignment{}
\def\amspset@duedate{}
\def\amspset@extraline{}

% commands to set header block info
\newcommand{\name}[1]{\def\amspset@name{#1}}
\newcommand{\class}[1]{\def\amspset@class{#1}}
\newcommand{\assignment}[1]{\def\amspset@assignment{#1}}
\newcommand{\duedate}[1]{\def\amspset@duedate{#1}}
\newcommand{\extraline}[1]{\def\amspset@extraline{#1}}

%Yu's custermization

\providecommand{\abs}[1]{\lvert#1\rvert}


================================================
FILE: homework/hw1.tex
================================================
\documentclass[11pt]{article}

% set 1-inch margins in the document
\usepackage[margin=1in]{geometry}
\usepackage{amsthm}
\theoremstyle{definition}

% include this if you want to import graphics files with /includegraphics
\usepackage{graphicx}

% info for header block in upper right hand corner

\newtheorem{problem}{Problem}

\title{HIT --- Cryptography --- Homework 1}

\begin{document}

\maketitle

\begin{problem}
Show that the shift, Mono-Alphabetic sub., and Vigen\`{e}re ciphers are all trivial to break using a known-plaintext attack. How much known plaintext (how many characters) is needed to completely recover the key for each of the ciphers?  (show how to break the cipher)
\end{problem}

\begin{problem}
Show that the shift, Mono-Alphabetic sub., and Vigen\`{e}re ciphers are all trivial to break using a chosen-plaintext attack. How much plaintext (how many characters) must be encrypted to completely recover the key? (show your chosen plaintext) 
\end{problem}

\begin{problem}
Prove or refute: For every encryption scheme that is perfectly secret it holds that for every distribution over the message space $\mathcal{M}$, every $m, m' \in \mathcal{M}$, and every $c \in \mathcal{C}$:
\[ \Pr[M=m | C=c] = \Pr[M=m'|C=c].
\]
\end{problem}

\begin{problem}
Study conditions under which the shift, mono-alphabetic sub., and Vigen\`{e}re cipher ciphers are perfectly secret:
\begin{itemize}
\item (a) Prove that if only a single character is encrypted, then the shift cipher is perfectly secret.
\item (b) What is the largest plaintext space $M$ you can find for which the mono-alphabetic sub. cipher provides perfect secrecy?
\item (c) Show how to use the Vigen\`{e}re cipher to encrypt any word of length $t$ so that perfect secrecy is obtained (note: you can choose the length of the key). Prove your answer.
\end{itemize}
\end{problem}

\begin{problem}
In the one-time pad encryption scheme, it can sometimes happen that the key is the all-zero string. In this case, the encryption of a message $m$ is given by $m \oplus 0^{l} = m$ and therefore the ciphertext is identical to the message!
\begin{itemize}
\item (a) Do you think the one-time pad scheme should be modified so that the all-zero key is not used? Explain.
\item (b) Explain how it is possible that the one-time pad is perfectly secure even though the above situation can occur with non-zero probability.
\end{itemize}
\end{problem}

\end{document}


================================================
FILE: homework/hw2.tex
================================================
\documentclass[11pt]{article}

% set 1-inch margins in the document
\usepackage[margin=1in]{geometry}
\usepackage{amsthm}
\theoremstyle{definition}

% include this if you want to import graphics files with /includegraphics
\usepackage{graphicx}

% info for header block in upper right hand corner

\newtheorem{problem}{Problem}

\title{HIT --- Cryptography --- Homework 2}

\begin{document}

\maketitle

\begin{problem}
Assuming the existence of a variable output-length pseudorandom generator, present a construction of variable-length encryption scheme, and prove that your construction has indistinguishable encryptions in the presence of an eavesdropper. {\small Hint: the construction of secure fixed-length encryption scheme also holds here.}
\end{problem}

\begin{problem}
Assume $f(s)$ and $f'(s)$ are PRGs. Is $g(s)=f(s) \oplus f'(s)$ also necessarily a PRG? How about $g(s)=f(s) \oplus s$?
\end{problem}

\begin{problem}
Assuming the existence of a pseudorandom function, prove that there exists an encryption scheme that has indistinguishable multiple encryptions in the presence of an eavesdropper, but is not CPA-secure.
{\small Hint: You will need to use the fact that in a CPA the adversary can choose its queries to the encryption oracle adaptively (i.e., new query may be constructed from previous queries).}
\end{problem}

%\begin{problem}
%Present formulas for decryption of all the different modes of operation for encryption: ECB, CBC, OFB, CTR. For which modes can decryption be parallelized?
%\end{problem}

\begin{problem}
Present a construction of a variable output-length pseudorandom generator from any pseudorandom function. Prove that your construction satisfies Definition: `a variable output-length pseudorandom generator'.
\end{problem}

\begin{problem}
Show that the CBC mode do not yield CPA-secure encryption in the case that the $IV$ is predicable. {\small Hint: The messages presented by the adversary could be constructed from the predicable $IV$ and previous queries.}
\end{problem}

\begin{problem}
Show that the CBC, OFB and CTR modes do not yield CCA-secure encryption schemes (regardless of F). {\small Hint: If one bit of Ciphertext is flipped, so does one bit of Plaintext.}
\end{problem}

\begin{problem}
Show how to learn whether the length of message is 1 bytes with the padding-oracle attack. 
\end{problem}

\end{document}


================================================
FILE: homework/hw3.tex
================================================
\documentclass[11pt]{article}

% set 1-inch margins in the document
\usepackage[margin=1in]{geometry}
\usepackage{amsthm}
\usepackage{amsmath}
\usepackage{amssymb,amsfonts}
\theoremstyle{definition}

% include this if you want to import graphics files with /includegraphics
\usepackage{graphicx}
\providecommand{\abs}[1]{\lvert#1\rvert}

% info for header block in upper right hand corner

\newtheorem{problem}{Problem}

\title{HIT --- Cryptography --- Homework 3}

\begin{document}

\maketitle

\begin{problem}
In our attack on a 1-round substitution-permutation network, we considered a block length of 64 bits and a network with 16 $S$-boxes that each take a 4-bit input. 
\begin{enumerate}
\item Repeat the analysis for the case of 8 $S$-boxes, each taking an 8-bit input. What is the complexity of the attack now?
\item Repeat the analysis again with a 128-bit block length and 16 $S$-boxes that each take an 8-bit input.
\item Does the $S$-boxes length make any difference? Does the block length make any difference?
\end{enumerate}
\end{problem}

\begin{problem}
Show that DES has the property that $DES_k(x) = \overline{DES_{\overline{k}}(\overline{x})}$ for every key $k$ and input $x$ (where $\overline{z}$ denotes the bitwise complement of $z$). This is called the complementarity property of $DES$.
\end{problem}

\begin{problem}
Is the addition function $f(x, y) = x + y$ (where $|x| = |y|$ and $x$ and $y$ are interpreted as natural numbers) a one-way function?
\end{problem}

\begin{problem}
Let $f_{1}(x)$ and $f_{2}(x)$ be one-way functions. Is $f(x) = (f_{1}(x), f_{2}(x))$ necessarily a one-way function? Prove your answers.
\end{problem}

\begin{problem}
Let $f$ be a one-way function. Is $g(x) = f(f(x))$ necessarily a one-way function? What about $g(x) = (f(x),f(f(x)))$? Prove your answers.
\end{problem}

\end{document}

================================================
FILE: homework/hw4.tex
================================================
\documentclass[11pt]{article}

% set 1-inch margins in the document
\usepackage[margin=1in]{geometry}
\usepackage{amsthm}
\usepackage{amsmath}
\usepackage{amssymb,amsfonts}
\theoremstyle{definition}

% include this if you want to import graphics files with /includegraphics
\usepackage{graphicx}
\providecommand{\abs}[1]{\lvert#1\rvert}

% info for header block in upper right hand corner

\newtheorem{problem}{Problem}

\title{HIT --- Cryptography --- Homework 4}

\begin{document}

\maketitle

\begin{problem}
Let $F$ be a pseudorandom function. Show that the following MAC for messages of length $2n$ is insecure: The shared key is a random $k\in \{0,1\}^n$. To authenticate a message $m_1\| m_2$ with $\abs{m_1} =\abs{m_2} = n$, compute the tag $\langle F_k(m_1), F_k(F_k(m_2))\rangle$. 
\end{problem}

\begin{problem}
Let $(\mathsf{Gen},H)$ be a collision-resistant hash function. Is $(\mathsf{Gen},\hat{H})$ defined by $(\hat{H}^s(x) \overset{\text{def}}{=} H^s(H^s(x))$ necessarily collision resistant? Prove your answer. 
\end{problem}

\begin{problem}
For each of following modifications to the Merkle-Damg\r{a}rd transform, determine whether the result is collision resistant or not. If yes, provide a proof; if not, demonstrate an attack. {\small Hint: you may use two facts on hash function: (1) $h(x) = x$ is collision resistant. Although $x$ is leaked, there is no collision. (2) A crhf $h$ can be constructed from another crhf $g$ by letting $h(x) = x\|0$ for $x = 0$ and letting $h(x) = g(x)\|1$ for $x \ne 0$. }
\begin{enumerate}
\item Modify the construction so that the input length is not included at all (i.e, output $z_B$ and not $z_{B+1} = h^s(z_B\| L)$).
\item Modify the construction so that instead of outputting $z = h^s(z_B\| L)$, the algorithm outputs $z_B\|L$
\item Instead of using an $IV$, just start the computation from $x_1$. That is, define $z_1 := x_1$ and then compute $z_i := h^s(z_{i-1}\|x_i)$ for $i=2,\dotsc,B+1$ and output $z_{B+1}$ as before.
\item Instead of using a fixed $IV$, set $z_0 := L$ and then compute $z_i := h^s(z_{i-1}\|x_i)$ for $i=1,\dotsc,B$ and output $z_B$.
\end{enumerate}
\end{problem}
\begin{problem}
We have learned that CCA-secure encryption schemes can be constructed by Enc-then-MAC in the class. Is there any other way to achieve CCA-secure scheme but without MAC? For example, (1) do you think the following scheme is CCA-secure? And why?
\begin{itemize}
\item message $m \in \{0,1 \}^{n/2}$ and key $k \in \{0,1 \}^{n}$. In encryption, choose a random string $r \gets \{0,1 \}^{n/2}$ and ciphertext $c := F_{k}(r\| m)$, where $F$ is a strong PRP.
\end{itemize}
Furthermore, no matter what is your answer to the above question, (2) do you think CCA-security implies secure Authenticated Encryption (A.E.)? And why?
\end{problem}
\begin{problem}
Show a message transmission scheme that achieves authentication communication (with integrity and authenticity) but is not a secure A.E (without confidentiality).
\end{problem}
\end{document}

================================================
FILE: homework/hw5.tex
================================================
\documentclass[11pt]{article}

% set 1-inch margins in the document
\usepackage[margin=1in]{geometry}
\usepackage{amsthm}
\usepackage{amsmath}
\usepackage{amssymb,amsfonts}
\theoremstyle{definition}

% include this if you want to import graphics files with /includegraphics
\usepackage{graphicx}
\providecommand{\abs}[1]{\lvert#1\rvert}

% info for header block in upper right hand corner

\newtheorem{problem}{Problem}

\title{HIT --- Cryptography --- Homework 5}

\begin{document}

\maketitle

%\begin{problem}
%This question concerns the Euler phi function.
%\begin{enumerate}
%\item Let  $p$ be a prime and $e \ge 1$ an integer. Show that $\phi(p^e) = p^{e-1}(p-1)$.
%\item Let $p,q$ be relatively prime. Show that $\phi(pq) = \phi(p)\cdot \phi(q)$. (You may use the Chinese remainder theorem.)
%\item Prove Theorem: $N = \prod_ip_i^{e_i}$, $\{p_i\}$ are distinct primes, $\phi(N) = \prod_ip_i^{e_i-1}(p_i-1)$.
%\end{enumerate}
%\end{problem}

\begin{problem}
Compute $[101^{4,800,000,023} \bmod 35]$ (by hand).
\end{problem}

\begin{problem}
Let $N=pq$ be a product of two distinct primes. Show that if $\phi(N)$ and $N$ are known, then it is possible to compute $p$ and $q$ in polynomial time.
\end{problem}

\begin{problem}
For an RSA public key  $\langle N, e \rangle$, we have an algorithm $\mathcal{A}$ that always correctly computes $LSB(x)$ given $[ x^e \mod N]$. Design an algorithm that computes $x$ from $[ x^e \mod N]$.
\end{problem}

\begin{problem}
Consider the following key-exchange protocol:
\begin{enumerate}
\item Alice chooses $k,r \gets \{0,1\}^n$ at random, and sends $s:=k\oplus r$ to Bob.
\item Bob chooses $t \gets \{0,1\}^n$ at random and sends $u := s\oplus t$ to Alice.
\item Alice computes $w := u\oplus r$ and sends $w$ to Bob.
\item Alice outputs $k$ and Bob computes $w \oplus t$.
\end{enumerate}
Show that Alice and Bob output the same key. Analyze the security of the scheme (i.e. either prove its security or show a concrete attack by an eavesdropper).
\end{problem}

\begin{problem}
Consider the following public-key encryption scheme. The public key is $(\mathbb{G},q,g,h)$ and the private key is $x$, generated exactly as in the El Gamal encryption scheme. In order to encrypt a bit $b$, the sender does the following:
\begin{itemize}
\item If $b=0$ then choose a random $y \gets \mathbb{Z}_q$ and compute $c_1= g^y$ and $c_2=h^y$. The ciphertext is $\langle c_1,c_2\rangle$.
\item If $b=1$ then choose independent random $y,z \gets \mathbb{Z}_q$ and compute $c_1= g^y$ and $c_2=g^z$, and set the ciphertext is $\langle c_1,c_2\rangle$.
\end{itemize}
(a) Show that it is possible to decrypt efficiently given knowledge of $x$. (b) Prove that this encryption scheme is CPA-secure if the decisional Diffie-Hellman problem is hard relative to $\mathcal{G}$
\end{problem}

\begin{problem}
The natural way of applying hybrid encryption to the El Gamal encryption scheme is as follows. The public key is $pk = \langle \mathbb{G},q,g,h\rangle $ as in the El Gamal scheme, and to encrypt a message $m$ the sender chooses random $k \gets \{0,1\}^n$ and sends
\[ \langle g^r, h^r\cdot k, \mathsf{Enc}_k(m)\rangle, \]
where $r\gets \mathbb{Z}_q$ is chosen at random and $\mathsf{Enc}$ represents a private-key encryption scheme. Suggest an improvement that results in a shorter ciphertext containing only a \emph{single} group element followed by a private-key encryption of $m$.
\end{problem}

\begin{problem}
For each of the following variants of the definition of security for signatures, state whether textbook RSA is secure and prove your answer: 
\begin{itemize}
\item (a) In this first variant, the experiment is as follows: the adversary is given the public key $pk$ and a random message $m$. The adversary is then allowed to query the signing oracle once on a single message that does not equal $m$. Following this, the adversary outputs a signature $\sigma$ and succeeds if $\mathsf{Vrfy}_{pk}(m,\sigma)=1$. As usual, security is said to hold if the adversary can succeed in this experiment with at most negligible probability.	
\item (b) The second variant is as above, except that the adversary is not allowed to query the signing oracle at all.
\end{itemize}
\end{problem}

\begin{problem}
Consider the Lamport one-time signature scheme. Describe an adversary who obtains signatures on two messages of its choice and can then forge signatures on any message it likes.
\end{problem}

\end{document}

================================================
FILE: homework/hw6.tex
================================================
\documentclass[11pt]{article}

% set 1-inch margins in the document
\usepackage[margin=1in]{geometry}
\usepackage{amsthm}
\usepackage{amsmath}
\usepackage{amssymb,amsfonts}
\theoremstyle{definition}

% include this if you want to import graphics files with /includegraphics
\usepackage{graphicx}
\providecommand{\abs}[1]{\lvert#1\rvert}

% info for header block in upper right hand corner

\newtheorem{problem}{Problem}

\title{HIT --- Cryptography --- Homework 6}

\begin{document}

\maketitle

\begin{problem}
We have almost finished the course. Let's put things together to design a secure communication system for  
a multi-player on-line game platform, such as the DOTA or the SanGuoSha. Communications are among players with or without a centric server.
In your report, please describe the threats your system may face, the cryptographic requirements and the corresponding constructions to satisfy the requirements. The description of threat may look like this:
\begin{itemize}
\item In on-line gaming, one player may eavesdrop the messages between another player and the centric server, which can happen when two players are within the same LAN. Then the eavesdropping player may learn something which should be confidential, such as the current position of the other on the map in  the DOTA, or cards in the other's hand in the SanGuoSha.
\end{itemize}
Please present at least two other threats besides the above example. If the threat you proposed is novel and different with others, and you give a reasonable design to avoid or detect the threat, you will be awarded 5 extra points.\\

Now let's make on-line gaming fair-play!
\end{problem}
\end{document}

================================================
FILE: misc/gallery/frame.tex
================================================
% presentation

%\documentclass{beamer}
%\usetheme[height=7mm]{Rochester}
%\usecolortheme{rose}

% handout

\documentclass[handout]{beamer}
\usepackage{pgfpages} \pgfpagesuselayout{16 on 1}[a4paper,landscape]

%\documentclass[mathserif]{article}
%\usepackage{beamerarticle}

\usepackage{amsmath}
\usepackage{amssymb,amsfonts}
\usepackage[T1]{fontenc}
\usepackage{lmodern}
\usepackage{tikz}
\usepackage{comment}
\usepackage{simpsons}
\usepackage{marvosym}
\usepackage{color}
\usepackage{multirow}
\usepackage{pgffor}
\usepackage{pgfplots}
\usepackage[slide,algoruled,titlenumbered,vlined,noend,linesnumbered,]{algorithm2e}

% handout
%\usefonttheme{structurebold}

\setbeamertemplate{footline}[frame number]
\setbeamertemplate{navigation symbols}{}
\setbeamerfont{smallverb}{size*={73}}
\usefonttheme[onlymath]{serif}
\setbeamertemplate{theorems}[numbered]
\newtheorem{construction}[theorem]{Construction}
\newtheorem{proposition}[theorem]{Proposition}

%\AtBeginSection[] { 
%  \begin{frame} 
%    \frametitle{Content} 
%    \tableofcontents[currentsection]
%  \end{frame} 
%  \addtocounter{framenumber}{-1} 
%}

\usetikzlibrary[shapes.arrows]
\usetikzlibrary{shapes.geometric}
\usetikzlibrary{backgrounds}
\usetikzlibrary{positioning}
\usetikzlibrary{calc}
\usetikzlibrary{intersections}
\usetikzlibrary{fadings}
\usetikzlibrary{decorations.footprints}
\usetikzlibrary{patterns}
\usetikzlibrary{shapes.callouts}
\usetikzlibrary{fit}
%handout

\providecommand{\abs}[1]{\lvert#1\rvert}

%\tikzset{every picture/.style={line width=1pt,show background rectangle},background rectangle/.style={fill=blue!10,rounded corners=2ex}}

\newcommand{\Bob}[3]{ \begin{scope}[shift={(#1,#2)},scale=#3]
    \draw (0,0) circle (0.95 and 1);
    \fill (-0.3,-0.1) circle (0.1);
    \fill (+0.3,-0.1) circle (0.1);
    \draw (0.35,-0.5) arc (-70:-110: 1 and 0.4);
    \draw (-0.3,0.5) arc (-10:-80: 0.8 and 0.8);
    \draw (-0.5,0.8) arc (190:255: 2 and 1);
    \draw (-0.7,0.9) -- +(0.2,-0.09) -- +(0.25,0.2);
    \end{scope} }
  
\newcommand{\Alice}[3]{ \begin{scope}[shift={(#1,#2)},scale=#3]
  \draw (0,0) circle (0.95 and 1);
  \fill (-0.3,-0.1) circle (0.1);
  \fill (+0.3,-0.1) circle (0.1);
  \draw (0.35,-0.5) arc (-70:-110: 1 and 0.4);
  \draw (0.3,1.3) arc (20:-100: 1.4 and 1);
  \draw (0.5,1.3) arc (150:260: 1 and 1);
  \draw (0.41,1.3) circle (0.35);
  \end{scope} }

  \newcommand{\Evil}[3]{ \begin{scope}[shift={(#1,#2)},scale=#3]
    \draw (0,0) circle (0.95 and 1);
    \fill (-0.1,-0.1) -- +(-0.2,-0.1) -- +(-0.4,0.2); %eye
    \fill (0.1,-0.1) -- +(0.2,-0.1) -- +(0.4,0.2);
    \draw (0.35,-0.5) arc (-70:-110: 1 and 0.4);
    %\fill (0.3,-0.5) -- +(-0.1,-0.2) -- +(-0.2,-0.02);
    %\fill (-0.3,-0.5) -- +(0.1,-0.2) -- +(0.2,-0.02);
    \fill (0.3,0.7) -- +(0.5,0.4) -- +(0.4,-0.2); % horn
    \fill (-0.3,0.7) -- +(-0.5,0.4) -- +(-0.4,-0.2);
    %\draw (0.3,1.3) arc (20:-100: 1.4 and 1);
    %\draw (0.5,1.3) arc (150:260: 1 and 1);
    %\draw (0.41,1.3) circle (0.35);
    \end{scope} }

\newcommand{\Charlie}[3]{ \begin{scope}[shift={(#1,#2)},scale=#3]
    \draw (0,0) circle (0.95 and 1);
    \filldraw[fill=black!20] (-0.35,-0.1) circle (0.25);
    \filldraw[fill=black!20] (+0.35,-0.1) circle (0.25);
    %\draw (0.9,0.2) to [bend left] (-0.9,0.2);
    \draw (0.2,0) to [bend left] (-0.2,0);


    %\draw (0.3,0.7) to [bend right] (-0.3,0.7);
    %\draw (0.4,0.5) to [bend right] (-0.4,0.5);
    %\draw (0.35,-0.5) arc (-70:-110: 1 and 0.4);
    \draw (-0.7,-0.6) to [bend right] (0,-0.6) to [bend right] (0.7,-0.6) to [bend right]  (0,-0.5)  to [bend right]  cycle ;
    %\draw (0.3,1.3) arc (20:-100: 1.4 and 1);
    %\draw (0.5,1.3) arc (150:260: 1 and 1);
    %\draw (0.41,1.3) circle (0.35);
    \end{scope} }

\author{Yu Zhang}
\institute{HIT/CST/NIS}
\date[Crypt'12S]{Cryptography, Spring, 2012}

%\input{1introduction.tex}
%\input{2perfectlysecret.tex}
%\input{3privatekey.tex}


================================================
FILE: misc/gallery/gallery.tex
================================================
\input{misc/gallery/frame.tex}
\begin{document}
\begin{frame} \frametitle{1outof2}
\begin{figure}
\begin{center}
\input{tikz/1outof2.tex}
\end{center}
\end{figure}
\end{frame}
\begin{frame} \frametitle{3ballot}
\begin{figure}
\begin{center}
\input{tikz/3ballot.tex}
\end{center}
\end{figure}
\end{frame}
\begin{frame} \frametitle{3parties-DHKE}
\begin{figure}
\begin{center}
\input{tikz/3parties-DHKE.tex}
\end{center}
\end{figure}
\end{frame}
\begin{frame} \frametitle{3parties-JOUX}
\begin{figure}
\begin{center}
\input{tikz/3parties-JOUX.tex}
\end{center}
\end{figure}
\end{frame}
\begin{frame} \frametitle{CBC-MAC}
\begin{figure}
\begin{center}
\input{tikz/CBC-MAC.tex}
\end{center}
\end{figure}
\end{frame}
\begin{frame} \frametitle{CBC-small}
\begin{figure}
\begin{center}
\input{tikz/CBC-small.tex}
\end{center}
\end{figure}
\end{frame}
\begin{frame} \frametitle{CBC}
\begin{figure}
\begin{center}
\input{tikz/CBC.tex}
\end{center}
\end{figure}
\end{frame}
\begin{frame} \frametitle{CCA-PKCS}
\begin{figure}
\begin{center}
\input{tikz/CCA-PKCS.tex}
\end{center}
\end{figure}
\end{frame}
\begin{frame} \frametitle{CCA}
\begin{figure}
\begin{center}
\input{tikz/CCA.tex}
\end{center}
\end{figure}
\end{frame}
\begin{frame} \frametitle{CMAC}
\begin{figure}
\begin{center}
\input{tikz/CMAC.tex}
\end{center}
\end{figure}
\end{frame}
\begin{frame} \frametitle{CTR}
\begin{figure}
\begin{center}
\input{tikz/CTR.tex}
\end{center}
\end{figure}
\end{frame}
\begin{frame} \frametitle{DESkey}
\begin{figure}
\begin{center}
\input{tikz/DESkey.tex}
\end{center}
\end{figure}
\end{frame}
\begin{frame} \frametitle{DHkey}
\begin{figure}
\begin{center}
\input{tikz/DHkey.tex}
\end{center}
\end{figure}
\end{frame}
\begin{frame} \frametitle{Davies-Meyer}
\begin{figure}
\begin{center}
\input{tikz/Davies-Meyer.tex}
\end{center}
\end{figure}
\end{frame}
\begin{frame} \frametitle{ECB}
\begin{figure}
\begin{center}
\input{tikz/ECB.tex}
\end{center}
\end{figure}
\end{frame}
\begin{frame} \frametitle{ElGamal}
\begin{figure}
\begin{center}
\input{tikz/ElGamal.tex}
\end{center}
\end{figure}
\end{frame}
\begin{frame} \frametitle{HMAC}
\begin{figure}
\begin{center}
\input{tikz/HMAC.tex}
\end{center}
\end{figure}
\end{frame}
\begin{frame} \frametitle{IBE}
\begin{figure}
\begin{center}
\input{tikz/IBE.tex}
\end{center}
\end{figure}
\end{frame}
\begin{frame} \frametitle{KDC}
\begin{figure}
\begin{center}
\input{tikz/KDC.tex}
\end{center}
\end{figure}
\end{frame}
\begin{frame} \frametitle{MDtransform}
\begin{figure}
\begin{center}
\input{tikz/MDtransform.tex}
\end{center}
\end{figure}
\end{frame}
\begin{frame} \frametitle{MS-PPTP}
\begin{figure}
\begin{center}
\input{tikz/MS-PPTP.tex}
\end{center}
\end{figure}
\end{frame}
\begin{frame} \frametitle{Miyaguchi-Preneel}
\begin{figure}
\begin{center}
\input{tikz/Miyaguchi-Preneel.tex}
\end{center}
\end{figure}
\end{frame}
\begin{frame} \frametitle{NMAC}
\begin{figure}
\begin{center}
\input{tikz/NMAC.tex}
\end{center}
\end{figure}
\end{frame}
\begin{frame} \frametitle{OAEP-plus}
\begin{figure}
\begin{center}
\input{tikz/OAEP-plus.tex}
\end{center}
\end{figure}
\end{frame}
\begin{frame} \frametitle{OAEP}
\begin{figure}
\begin{center}
\input{tikz/OAEP.tex}
\end{center}
\end{figure}
\end{frame}
\begin{frame} \frametitle{OFB}
\begin{figure}
\begin{center}
\input{tikz/OFB.tex}
\end{center}
\end{figure}
\end{frame}
\begin{frame} \frametitle{OWF}
\begin{figure}
\begin{center}
\input{tikz/OWF.tex}
\end{center}
\end{figure}
\end{frame}
\begin{frame} \frametitle{SAEP-plus}
\begin{figure}
\begin{center}
\input{tikz/SAEP-plus.tex}
\end{center}
\end{figure}
\end{frame}
\begin{frame} \frametitle{SIV-CTR}
\begin{figure}
\begin{center}
\input{tikz/SIV-CTR.tex}
\end{center}
\end{figure}
\end{frame}
\begin{frame} \frametitle{TDES}
\begin{figure}
\begin{center}
\input{tikz/TDES.tex}
\end{center}
\end{figure}
\end{frame}
\begin{frame} \frametitle{VCBC-MAC}
\begin{figure}
\begin{center}
\input{tikz/VCBC-MAC.tex}
\end{center}
\end{figure}
\end{frame}
\begin{frame} \frametitle{alice}
\begin{figure}
\begin{center}
\input{tikz/alice.tex}
\end{center}
\end{figure}
\end{frame}
\begin{frame} \frametitle{attack-spn}
\begin{figure}
\begin{center}
\input{tikz/attack-spn.tex}
\end{center}
\end{figure}
\end{frame}
\begin{frame} \frametitle{authentication}
\begin{figure}
\begin{center}
\input{tikz/authentication.tex}
\end{center}
\end{figure}
\end{frame}
\begin{frame} \frametitle{baby-giant}
\begin{figure}
\begin{center}
\input{tikz/baby-giant.tex}
\end{center}
\end{figure}
\end{frame}
\begin{frame} \frametitle{bilinear-map}
\begin{figure}
\begin{center}
\input{tikz/bilinear-map.tex}
\end{center}
\end{figure}
\end{frame}
\begin{frame} \frametitle{birthdayattack}
\begin{figure}
\begin{center}
\input{tikz/birthdayattack.tex}
\end{center}
\end{figure}
\end{frame}
\begin{frame} \frametitle{blindsignature}
\begin{figure}
\begin{center}
\input{tikz/blindsignature.tex}
\end{center}
\end{figure}
\end{frame}
\begin{frame} \frametitle{cPRF}
\begin{figure}
\begin{center}
\input{tikz/cPRF.tex}
\end{center}
\end{figure}
\end{frame}
\begin{frame} \frametitle{certificates}
\begin{figure}
\begin{center}
\input{tikz/certificates.tex}
\end{center}
\end{figure}
\end{frame}
\begin{frame} \frametitle{chain-sig}
\begin{figure}
\begin{center}
\input{tikz/chain-sig.tex}
\end{center}
\end{figure}
\end{frame}
\begin{frame} \frametitle{cipher-stealing}
\begin{figure}
\begin{center}
\input{tikz/cipher-stealing.tex}
\end{center}
\end{figure}
\end{frame}
\begin{frame} \frametitle{coinflipping}
\begin{figure}
\begin{center}
\input{tikz/coinflipping.tex}
\end{center}
\end{figure}
\end{frame}
\begin{frame} \frametitle{collision}
\begin{figure}
\begin{center}
\input{tikz/collision.tex}
\end{center}
\end{figure}
\end{frame}
\begin{frame} \frametitle{combination}
\begin{figure}
\begin{center}
\input{tikz/combination.tex}
\end{center}
\end{figure}
\end{frame}
\begin{frame} \frametitle{compute-sec}
\begin{figure}
\begin{center}
\input{tikz/compute-sec.tex}
\end{center}
\end{figure}
\end{frame}
\begin{frame} \frametitle{constructD}
\begin{figure}
\begin{center}
\input{tikz/constructD.tex}
\end{center}
\end{figure}
\end{frame}
\begin{frame} \frametitle{des}
\begin{figure}
\begin{center}
\input{tikz/des.tex}
\end{center}
\end{figure}
\end{frame}
\begin{frame} \frametitle{desx}
\begin{figure}
\begin{center}
\input{tikz/desx.tex}
\end{center}
\end{figure}
\end{frame}
\begin{frame} \frametitle{differential}
\begin{figure}
\begin{center}
\input{tikz/differential.tex}
\end{center}
\end{figure}
\end{frame}
\begin{frame} \frametitle{digitalsignature}
\begin{figure}
\begin{center}
\input{tikz/digitalsignature.tex}
\end{center}
\end{figure}
\end{frame}
\begin{frame} \frametitle{dining}
\begin{figure}
\begin{center}
\input{tikz/dining.tex}
\end{center}
\end{figure}
\end{frame}
\begin{frame} \frametitle{doubleE}
\begin{figure}
\begin{center}
\input{tikz/doubleE.tex}
\end{center}
\end{figure}
\end{frame}
\begin{frame} \frametitle{ePRG}
\begin{figure}
\begin{center}
\input{tikz/ePRG.tex}
\end{center}
\end{figure}
\end{frame}
\begin{frame} \frametitle{ecdhke}
\begin{figure}
\begin{center}
\input{tikz/ecdhke.tex}
\end{center}
\end{figure}
\end{frame}
\begin{frame} \frametitle{elgamal-con}
\begin{figure}
\begin{center}
\input{tikz/elgamal-con.tex}
\end{center}
\end{figure}
\end{frame}
\begin{frame} \frametitle{ellipticcurve}
\begin{figure}
\begin{center}
\input{tikz/ellipticcurve.tex}
\end{center}
\end{figure}
\end{frame}
\begin{frame} \frametitle{encryptionwithpf}
\begin{figure}
\begin{center}
\input{tikz/encryptionwithpf.tex}
\end{center}
\end{figure}
\end{frame}
\begin{frame} \frametitle{encryptionwithpg}
\begin{figure}
\begin{center}
\input{tikz/encryptionwithpg.tex}
\end{center}
\end{figure}
\end{frame}
\begin{frame} \frametitle{feistel-prp}
\begin{figure}
\begin{center}
\input{tikz/feistel-prp.tex}
\end{center}
\end{figure}
\end{frame}
\begin{frame} \frametitle{feistel}
\begin{figure}
\begin{center}
\input{tikz/feistel.tex}
\end{center}
\end{figure}
\end{frame}
\begin{frame} \frametitle{hash}
\begin{figure}
\begin{center}
\input{tikz/hash.tex}
\end{center}
\end{figure}
\end{frame}
\begin{frame} \frametitle{hcp}
\begin{figure}
\begin{center}
\input{tikz/hcp.tex}
\end{center}
\end{figure}
\end{frame}
\begin{frame} \frametitle{hs-reduce-1}
\begin{figure}
\begin{center}
\input{tikz/hs-reduce-1.tex}
\end{center}
\end{figure}
\end{frame}
\begin{frame} \frametitle{hs-reduce-2}
\begin{figure}
\begin{center}
\input{tikz/hs-reduce-2.tex}
\end{center}
\end{figure}
\end{frame}
\begin{frame} \frametitle{hybrid-enc-proof}
\begin{figure}
\begin{center}
\input{tikz/hybrid-enc-proof.tex}
\end{center}
\end{figure}
\end{frame}
\begin{frame} \frametitle{hybrid-encrypt}
\begin{figure}
\begin{center}
\input{tikz/hybrid-encrypt.tex}
\end{center}
\end{figure}
\end{frame}
\begin{frame} \frametitle{hybrideg}
\begin{figure}
\begin{center}
\input{tikz/hybrideg.tex}
\end{center}
\end{figure}
\end{frame}
\begin{frame} \frametitle{hybridproof}
\begin{figure}
\begin{center}
\input{tikz/hybridproof.tex}
\end{center}
\end{figure}
\end{frame}
\begin{frame} \frametitle{identification-schnorr}
\begin{figure}
\begin{center}
\input{tikz/identification-schnorr.tex}
\end{center}
\end{figure}
\end{frame}
\begin{frame} \frametitle{identification}
\begin{figure}
\begin{center}
\input{tikz/identification.tex}
\end{center}
\end{figure}
\end{frame}
\begin{frame} \frametitle{integrity}
\begin{figure}
\begin{center}
\input{tikz/integrity.tex}
\end{center}
\end{figure}
\end{frame}
\begin{frame} \frametitle{interlock}
\begin{figure}
\begin{center}
\input{tikz/interlock.tex}
\end{center}
\end{figure}
\end{frame}
\begin{frame} \frametitle{keyed-func}
\begin{figure}
\begin{center}
\input{tikz/keyed-func.tex}
\end{center}
\end{figure}
\end{frame}
\begin{frame} \frametitle{linear}
\begin{figure}
\begin{center}
\input{tikz/linear.tex}
\end{center}
\end{figure}
\end{frame}
\begin{frame} \frametitle{mac}
\begin{figure}
\begin{center}
\input{tikz/mac.tex}
\end{center}
\end{figure}
\end{frame}
\begin{frame} \frametitle{macforge-exp}
\begin{figure}
\begin{center}
\input{tikz/macforge-exp.tex}
\end{center}
\end{figure}
\end{frame}
\begin{frame} \frametitle{macwithprf}
\begin{figure}
\begin{center}
\input{tikz/macwithprf.tex}
\end{center}
\end{figure}
\end{frame}
\begin{frame} \frametitle{man-in-middle}
\begin{figure}
\begin{center}
\input{tikz/man-in-middle.tex}
\end{center}
\end{figure}
\end{frame}
\begin{frame} \frametitle{meet-in-middle}
\begin{figure}
\begin{center}
\input{tikz/meet-in-middle.tex}
\end{center}
\end{figure}
\end{frame}
\begin{frame} \frametitle{multiple-enc-exp}
\begin{figure}
\begin{center}
\input{tikz/multiple-enc-exp.tex}
\end{center}
\end{figure}
\end{frame}
\begin{frame} \frametitle{owff}
\begin{figure}
\begin{center}
\input{tikz/owff.tex}
\end{center}
\end{figure}
\end{frame}
\begin{frame} \frametitle{owfover}
\begin{figure}
\begin{center}
\input{tikz/owfover.tex}
\end{center}
\end{figure}
\end{frame}
\begin{frame} \frametitle{padding-oracle-lastbyte}
\begin{figure}
\begin{center}
\input{tikz/padding-oracle-lastbyte.tex}
\end{center}
\end{figure}
\end{frame}
\begin{frame} \frametitle{padding-oracle-null}
\begin{figure}
\begin{center}
\input{tikz/padding-oracle-null.tex}
\end{center}
\end{figure}
\end{frame}
\begin{frame} \frametitle{padding-oracle}
\begin{figure}
\begin{center}
\input{tikz/padding-oracle.tex}
\end{center}
\end{figure}
\end{frame}
\begin{frame} \frametitle{pgfD}
\begin{figure}
\begin{center}
\input{tikz/pgfD.tex}
\end{center}
\end{figure}
\end{frame}
\begin{frame} \frametitle{pgfMAC}
\begin{figure}
\begin{center}
\input{tikz/pgfMAC.tex}
\end{center}
\end{figure}
\end{frame}
\begin{frame} \frametitle{pnp}
\begin{figure}
\begin{center}
\input{tikz/pnp.tex}
\end{center}
\end{figure}
\end{frame}
\begin{frame} \frametitle{prg-distinguisher}
\begin{figure}
\begin{center}
\input{tikz/prg-distinguisher.tex}
\end{center}
\end{figure}
\end{frame}
\begin{frame} \frametitle{prg-sparse}
\begin{figure}
\begin{center}
\input{tikz/prg-sparse.tex}
\end{center}
\end{figure}
\end{frame}
\begin{frame} \frametitle{pri-cpa-exp}
\begin{figure}
\begin{center}
\input{tikz/pri-cpa-exp.tex}
\end{center}
\end{figure}
\end{frame}
\begin{frame} \frametitle{pri-eav-exp}
\begin{figure}
\begin{center}
\input{tikz/pri-eav-exp.tex}
\end{center}
\end{figure}
\end{frame}
\begin{frame} \frametitle{private-key}
\begin{figure}
\begin{center}
\input{tikz/private-key.tex}
\end{center}
\end{figure}
\end{frame}
\begin{frame} \frametitle{public-key}
\begin{figure}
\begin{center}
\input{tikz/public-key.tex}
\end{center}
\end{figure}
\end{frame}
\begin{frame} \frametitle{qr-qnr}
\begin{figure}
\begin{center}
\input{tikz/qr-qnr.tex}
\end{center}
\end{figure}
\end{frame}
\begin{frame} \frametitle{rabinOT}
\begin{figure}
\begin{center}
\input{tikz/rabinOT.tex}
\end{center}
\end{figure}
\end{frame}
\begin{frame} \frametitle{reduction-prg}
\begin{figure}
\begin{center}
\input{tikz/reduction-prg.tex}
\end{center}
\end{figure}
\end{frame}
\begin{frame} \frametitle{reduction}
\begin{figure}
\begin{center}
\input{tikz/reduction.tex}
\end{center}
\end{figure}
\end{frame}
\begin{frame} \frametitle{schnorr-signature}
\begin{figure}
\begin{center}
\input{tikz/schnorr-signature.tex}
\end{center}
\end{figure}
\end{frame}
\begin{frame} \frametitle{schnorr}
\begin{figure}
\begin{center}
\input{tikz/schnorr.tex}
\end{center}
\end{figure}
\end{frame}
\begin{frame} \frametitle{spn}
\begin{figure}
\begin{center}
\input{tikz/spn.tex}
\end{center}
\end{figure}
\end{frame}
\begin{frame} \frametitle{ssl}
\begin{figure}
\begin{center}
\input{tikz/ssl.tex}
\end{center}
\end{figure}
\end{frame}
\begin{frame} \frametitle{suf-mac}
\begin{figure}
\begin{center}
\input{tikz/suf-mac.tex}
\end{center}
\end{figure}
\end{frame}
\begin{frame} \frametitle{synchronizedmode}
\begin{figure}
\begin{center}
\input{tikz/synchronizedmode.tex}
\end{center}
\end{figure}
\end{frame}
\begin{frame} \frametitle{tdp-cca1}
\begin{figure}
\begin{center}
\input{tikz/tdp-cca1.tex}
\end{center}
\end{figure}
\end{frame}
\begin{frame} \frametitle{tdp-cca2}
\begin{figure}
\begin{center}
\input{tikz/tdp-cca2.tex}
\end{center}
\end{figure}
\end{frame}
\begin{frame} \frametitle{tdp-cpa}
\begin{figure}
\begin{center}
\input{tikz/tdp-cpa.tex}
\end{center}
\end{figure}
\end{frame}
\begin{frame} \frametitle{tdp-pk}
\begin{figure}
\begin{center}
\input{tikz/tdp-pk.tex}
\end{center}
\end{figure}
\end{frame}
\begin{frame} \frametitle{threepass}
\begin{figure}
\begin{center}
\input{tikz/threepass.tex}
\end{center}
\end{figure}
\end{frame}
\begin{frame} \frametitle{tls13hs}
\begin{figure}
\begin{center}
\input{tikz/tls13hs.tex}
\end{center}
\end{figure}
\end{frame}
\begin{frame} \frametitle{toy-OT}
\begin{figure}
\begin{center}
\input{tikz/toy-OT.tex}
\end{center}
\end{figure}
\end{frame}
\begin{frame} \frametitle{transmission}
\begin{figure}
\begin{center}
\input{tikz/transmission.tex}
\end{center}
\end{figure}
\end{frame}
\begin{frame} \frametitle{trapdoor}
\begin{figure}
\begin{center}
\input{tikz/trapdoor.tex}
\end{center}
\end{figure}
\end{frame}
\begin{frame} \frametitle{tree-sig}
\begin{figure}
\begin{center}
\input{tikz/tree-sig.tex}
\end{center}
\end{figure}
\end{frame}
\begin{frame} \frametitle{worldofpk}
\begin{figure}
\begin{center}
\input{tikz/worldofpk.tex}
\end{center}
\end{figure}
\end{frame}
\begin{frame} \frametitle{zkp-commitment}
\begin{figure}
\begin{center}
\input{tikz/zkp-commitment.tex}
\end{center}
\end{figure}
\end{frame}
\begin{frame} \frametitle{zkp-hanmilton}
\begin{figure}
\begin{center}
\input{tikz/zkp-hanmilton.tex}
\end{center}
\end{figure}
\end{frame}
\begin{frame} \frametitle{zkp-rsa}
\begin{figure}
\begin{center}
\input{tikz/zkp-rsa.tex}
\end{center}
\end{figure}
\end{frame}
\begin{frame} \frametitle{zkp}
\begin{figure}
\begin{center}
\input{tikz/zkp.tex}
\end{center}
\end{figure}
\end{frame}
\end{document}


================================================
FILE: misc/gallery/gen-gallery.sh
================================================
echo '\input{misc/gallery/frame.tex}'
echo '\\begin{document}'
for a in `ls -1 tikz/*.tex`
do a=`echo $a | sed 's/^.*\/\(.*\).tex/\1/'`
echo '\\begin{frame} \\frametitle{'${a}'}'
echo '\\begin{figure}'
echo '\\begin{center}'
echo '\input{tikz/'${a}'.tex}'
echo '\end{center}'
echo '\end{figure}'
echo '\end{frame}'
done
echo '\end{document}'


================================================
FILE: misc/tikztest/tikz-test.tex
================================================
\input{../../source/header/main.tex}
%\input{header/main.tex}
\begin{document}
\begin{frame}\frametitle{Tikz Test}
\begin{figure}
\begin{center}
\input{../../tikz/zkp-hanmilton}
\end{center}
\end{figure}
\end{frame}
\end{document}


================================================
FILE: notes-Chinese/0 课程介绍.md
================================================
# 课程介绍

本课程讲义需结合幻灯片和参考教材学习。中文讲义大体可以看作是英文幻灯片的翻译,其区别主要在于:

- 中文讲义不包含英文幻灯片中图片
- 中文讲义对英文幻灯片中一小部分内容只有简单的占位性的文字
- 中文讲义对英文幻灯片的部分内容有解释性补充

信息化重塑了人类社会,形成了信息社会,而密码学是保护信息社会中人类信息活动的理论基础。

1. 在本课程中,我们学习密码学原理,内容包含实现信息安全所需要的应用数学知识,但不包含如何实现相关方案。
2. 密码学是一个庞大的工具,作为许多安全机制的基础,用于保护通信和计算安全。

   - 其应用包括,保护Web流量的HTTPS,保护Wifi的WPA2/3,数字内容保护,比特币等。
   - 然而,密码学无法解决所有安全问题,并且只有在其被正确地实现和使用时才是可靠的。
   - 值得强调的是,我们不应该自己发明密码学,而是尽量用现成的。在后面的课程中会学习为何如此。
3. 本课程目的是学习什么是严格的信息安全,如何严格地保护信息,以及数学与工程是如何互动的。
4. 密码学成果中很多都归功于图灵奖得主们。我们将学习这些研究成果。
   - (思考)我们以这些科学家为榜样!同时,意识到我们在信息安全基础研究方面的差距。
   - (思考)姚期智老师回国任教,培养未来的科学家。
5. 本课程大纲包括古典密码学与完美保密,私钥(对称)加密,消息认证码(MAC),分组(块)密码,单向函数(OWF),数论,整数分解,离散对数,密钥管理,公钥(非对称)密码学,数字签名,陷门函数(TPD),随机预言机(ROM),(神奇的)密码学协议等。
6. 本课程大纲组织结构(中文)如图所示,可分为四大部分:安全思想,基础理论/假设,安全定义,以及如何根据基础理论/假设来实现安全定义的密码学体制。
7. 教材、幻灯片课件与交流方式
   - 参考教材为Jonathan Katz和Yehuda Lindell合著的IMC(Introduction to Mordern Cryptography)的第3版。
   - 课件和作业在GitHub上,随着课程开展会不定期更新。https://github.com/YuZhang/cryptography
   - 在Coursera上有斯坦福大学Dan Boneh教授的英文慕课,建议大家学习。
   - 请大家加入本课程的QQ群进行学习交流。
   - 为什么用英文教材和课件?因为未来我们将与英文的密码学资料打交道。密码学知识成果最初是英文书写的,最好的密码学学习资料也是英文的,而且密码学工具也是英文。为了更易于理解和交流,我们上课用汉语。为此,配套了中文讲义(对,正是您在阅读的这个)。
8. 本课程考核包括两部分:作业占4%x5=20%,期末考试占80%。

   - 根据期末成绩的最高分,优秀作业有可能(只是可能)被奖励额外5%。

   - 建议认真研读参考教材IMC,自己做作业(否则很难理解密码学核心思想),当然更不要抄袭作业。

================================================
FILE: notes-Chinese/1 导论.md
================================================
# 导论

1. 在本节课程中,我们学习密码学导论,包括对称加密基本概念、古典密码学和现代密码学基本原则。我们通过学习这些可在直觉上被理解的,古典的加密方案和破解方法,建立对加密和安全的直观概念,从而理解现代密码学基本原则为什么是合理的和必要的。
2. 目录
3. 密码学概念

   - 密码学的英文单词来自两个希腊单词:kryptos,意为“隐藏,保密”,和graphin,意为“书写”,即秘写。
   - 密码学在简明牛津字典中定义为书写或破解代码(code)的艺术。其中,Code(代码)是一个预先编排好的符号的系统,特别用于确保消息传输中的秘密。另外中文中“密码”相关的英文单词还包括password(口令)、cipher(加密方案)、key(密钥)。这些术语要注意区分。
   - 现在密码学的开端在1980年代。那时在美国以DES、公钥密码学等为代表的成果相继出现,并且在美国密码学也从军用转变为民用。整个信息技术也是起步于上世纪八零年代的美国,包括个人计算机以及互联网。密码学的发展得益于个人计算机和互联网的发展。在互联网这个开放环境下安全通信的需求催生了密码学研究和大规模应用。
   - 参考教材IMC中对现代密码学定义:用于保护数字信息,系统和分布式计算免于敌对攻击的数学技术的科学研究。本课程主要是学习如何保护信息。
4. 什么是密码学?(思考)

   - 漫画:在美国,密码学曾经被作为武器而被禁止出口
   - 目前各国对密码学产品进出口都有严格限制
   - 《中华人民共和国密码法》
     - 第二条:本法所称密码,是指采用特定变换的方法对信息等进行加密保护、安全认证的技术、产品和服务。
     - 第七条:核心密码、普通密码用于保护国家秘密信息,核心密码保护信息的最高密级为绝密级,普通密码保护信息的最高密级为机密级。核心密码、普通密码属于国家秘密。
5. 私钥加密(private key encryption)设定

   - 目标是构造一个加密方案,用于在预先共享了私钥(对称密钥)的双方之间进行保密通信。
   - 隐含一个假设:存在某种方法,以保密的方式来分享一个密钥。
   - 磁盘加密相当于同一个人在不同的时刻间通信。
6. Alice和Bob

   - Alice和Bob是密码学领域最出名的人,他们首次出现在1978年的著名的RSA论文,《A Method for Obtaining Digital Signatures and Public-key Cryptosystems》(一种产生数字签名和公开密钥系统的方法)。有关Alice和Bob的[参考资料](https://en.wikipedia.org/wiki/Alice_and_Bob)。
7. 加密的词法

   - 什么是加密方案?下面是一个形式化的描述。其中的符号与表达比较符合直觉。

   - 作为发送者的Alice,向作为接收者的Bob,发送一个消息。该消息使用一个对称密钥加密。敌手(Adversary)窃听密文(窃听者通常命名为Eve),尝试获得明文。

   - **key** 密钥 $k \in \mathcal{K}$, **plaintext (or message)** 明文或消息 $m \in \mathcal{M}$, **ciphertext** 密文 $c \in \mathcal{C}$
     - 用小写字母表示一个具体信息,用花体大写字母表示一个集合
   - **Key-generation Algorithm** 密钥生成算法 $k \gets \mathsf{Gen}$
     - 用左箭头"$\leftarrow$"表示随机生成结果的赋值
   - **Encryption Algorithm** 加密算法 $c:= \mathsf{Enc}_k(m)$
     - 用冒号加等号“$:=$”表示“deterministic assignment”(确定性赋值),类似程序设计中的等号
   - **Decryption Algorithm** 解密算法 $m:= \mathsf{Dec}_k(c)$
   - **Encryption scheme** 加密方案: $\Pi = (\mathsf{Gen}, \mathsf{Enc}, \mathsf{Dec})$
     - 密钥生成算法也是加密方案的一部分!
   - **Basic correctness requirement** 基本正确性要求 : $\mathsf{Dec}_k(\mathsf{Enc}_k(m)) = m$
     - 对一个明文使用一个密钥加密后,再用同一个密钥解密,应该得到相同的明文。
     - 等号“=”表示“相等”,“是”
8. 保护密钥还是隐瞒算法?

   - 加密方案的“秘密”包括两部分:加密/解密算法和密钥,那么我们应该保密什么?
   - 更容易维护一个短密钥的秘密
   - 在密钥暴露的情况下,对于诚实方,更换密钥更容易
   - 在许多人彼此通信的情况下,更容易采用相同算法,不同密钥
   - **Kerchhoffs原则** (**柯克霍夫原则**):加密方法一定不必是秘密,即便落入敌手也必无不妥
   - **香农箴言**:敌人了解系统
9. 为什要“开放密码学设计”?

   - 发表的设计经过公开检验会更强健(类似在提倡开源软件时,所给出的一个优点)
   - 相对于被攻击者发现,由有道德的黑客来发现安全缺陷会更好
   - 即便不公开,代码逆向工程(或被工业间谍泄漏)也构成了严重的安全威胁
   - 使标准的建立成为可能
   - 即使成为标准也不意味着安全:Dual EC是一个标准化的后门
     - Dual EC曾经与其他算法一起被NIST, ANSI和ISO标准化来产生随机数
     - 斯诺登披露,以及在关于Bullrun项目和SIGINT使能项目的报告中已经表明,Dual EC是NSA颠覆标准的系统化工作的一部分。
     - 路透社报道,NSA在一笔交易中向RSA公司支付了1千万美元,用于将Dual EC设置为BSafe软件中优先或者缺省的数字生成方法。	
10. 攻击场景

       - 除了窃听密文(称为COA),敌手还有其它手段(敌手能力)

       - **Ciphertext-only (COA)** 唯密文: 敌手只观察密文
    
       - **Known-plaintext (KPA)** 已知明文: 敌手获知同一密钥下的若干明文/密文对
    
       - **Chosen-plaintext (CPA)** 选择明文: 敌手有获得所选择明文加密(获得该明文的密文)的能力
    
       - **Chosen-ciphertext (CCA)** 选择密文: 敌手有获得所选择的**其它密文**解密(获得该密文的明文)的能力
    
       - 被动攻击: COA KPA,由于不是所有密文都是机密的
    
       - 主动攻击: CPA CCA,当敌手能够加密/解密任何其所希望的信息

11. 历史上的加密方案及其密码分析(Cryptanalysis)

    - 下面学习古典密码,目的是了解加密并没有想象的复杂,但设计安全的加密是很困难的。同时,理解一些密码学设计的基本原则,并思考一个问题:如何确定一个加密方案是安全的?

    - 凯撒加密方案(Caesar's Cipher):凯撒将机密消息加密书写,这是将字母表中字母顺序改变使得没有一个单词可以被理解。若有人要解密,则他必须将字母表中第四个字母,即D,替换成A,并且对其它字母也这么做。
    - $\mathsf{Enc}(m)=m+3\mod 26$
    - 例子:明文`begintheattacknow`,采用凯撒加密的密文是什么?
    - 其弱点是什么?怎么改进?

12. 移位加密(Shift Cipher)

    - $\mathsf{Enc}_k(m)=m+k\mod 26$
    - $\mathsf{Dec}_k(c)=c-k\mod 26$
    - 例子:解密`EHJLQWKHDWWDFNQRZ`
    - 弱点: 是什么? 怎么改进?
    - **充足密钥空间原则**:任何安全加密方案必须具有一个经受住穷举搜索的密钥空间
    - 问题:如何在穷举过程中自动化地确定密钥?

13. 重合指数(Index of Coincidence)方法(寻找密钥 $k$)

    - 如何自动确定解密出来的是英文原文?

    - 重合指数$I$是两个随机挑选(挑选后放回)字母相同的概率

    - 令 $p_i$表示英文文本中第$i$个字母的概率

      $$ I \overset{\text{def}}{=}\sum_{i=0}^{25} p_i^2 $$

    - 例子:计算`apple`的重合指数?

      - 先计算字符在字符串中出现的比例,a、l和e都是1/5,p是2/5

      - 重合指数$=3\times \left( \frac{1}{5}\right)^2 + \left( \frac{2}{5}\right)^2 = \frac{7}{25}$

    - 根据统计,对于足够长的英文文本,其重合指数为0.065。

      - 当每个字母以相同频率出现时,重合指数为$26 \times \frac{1}{26}^2 = \frac{1}{26} = 0.038$

      - 这里可以看出英文中字母频率分布是不均匀的

    - 对于 $j = 0, 1, \dotsc , 25$, 设$q_j$为密文中第 $j$ 个字母的概率,定义一个带参数$s$的重合指数

      $$ I_s \overset{\text{def}}{=}\sum_{i=0}^{25} p_i \cdot q_{i+s} $$

      - $q_{i+s}$ 就是明文第 $i$ 个字母被移位 $s$ 个后所得到字母在密文中的概率

    - 问题:当得到$I_s = 0.065$时就找到了密钥$k$?
      - 当$s=k$为密钥时,重合指数最大,因为平方和大于乘积的和,$a^2 + b^2 > 2ab$
      - 例如,将凯撒密码当成$k=3$的移位密码,a被替换成D,D在密文中的概率与a在明文中的概率是一样的。当$k=3$时,$p_0$就是明文中a的概率,$q_{0+3}$就是密文中D的概率,此时,$p_0 = q_3$。

    - **这不正是大数据(足够的英文)+人工智能(是否是英文)吗!?**

14. 单表替换加密(Mono-Alphabetic Substitution)

    - 思想:将每个字母以任意方式映射到一个不同字母
    - 优点:密钥空间足够大 $\approx 2^{88}$。如何计算的?
    - 缺点:是什么?怎么改进?
    - 明文`abcdefghijklmnopqrstuvwxyz`
    - 密文`XEUADNBKVMROCQFSYHWGLZIJPT`
    - 明文`tellhimaboutme` ,密文是什么?

15. 利用统计模式来攻击

    1. 将密文中字母的频率制表,得到每个密文字母的频率
    2. 与英文文本中字符频率比较(英文文本中字母频率)
    3. 猜测频率最高的字母对应e,如此猜测其他字母
    4. 挑选“合理的”明文,但并不简单

16. 一个密文例子,人眼很难看出个所以然来

17. 一个频率分析的例子

    - 计数,猜,试错

18. 一个频率分析出的明文,《金甲虫》小说片段

19. 维吉尼亚(多表移位)加密  (Vigenere (poly-alphabetic shift) Cipher)

    - 思想:通过将明文中相同字母的不同出现映射到密文中不同字母,以此抹平密文中统计分布。
    - 加密:$c_i=m_i+k_{[i\bmod t]}$, $t$ 是 $k$ 的长度(周期)
    - 密码分析:
      - 需要发现 $t$;这曾经很难。
      - 若 $t$ 已知,则需要知道解密是否“合理”,但当$t > 15$时,蛮力破解 ($26^t$) 不可行;因为密钥中每一个字母都是移位加密的密钥,可以用重合指数来一个一个字母猜密钥。

20. Kasiski的方法(寻找周期 $t$)

    - 多表移位加密在2百多年内未被有效破解,直到...
    - 识别出长度2或3的重复模式,猜想这些重复应该是由于相同的明文片段被相同密钥片段加密的结果;
    - 那么,假设密钥中没有重复模式,则这些重复出现之间的距离应该是密钥长度 $t$ 的倍数;
    - 那么,假设明文中重复模式是随机的,则密钥长度 $t$ 是所有重复出现间距离的最大公约数;
    - 但重复出现也可能是巧合,有没有更有效的方法?

21. 重合指数法(寻找周期 $t$)

    - 对于 $\tau = 1, 2, \dotsc$ 作为猜测的周期,$c_1, c_{1+\tau}, c_{1+2\tau}, \dotsc$,是密文中以$\tau$为固定间隔的字符集合;

    - $q_i$ 是该字符集合中第 $i$ 个字母出现的概率,计算该以$\tau$为固定间隔字符集合的重合指数等于

      $$ I_\tau \overset{\text{def}}{=}\sum_{i=0}^{25} q_i^2 $$

    - 若 $\tau = t$, 那么 $I_\tau \approx ?$ 

      - 通过遍历 $\tau$ 来寻找 $t$, 若 $\tau = t$, 则使用了同一个密钥的移位加密所得到的密文 $c_1, c_{1+t}, c_{1+2t}, \dotsc$,其中相同的明文字母被映射为相同的密文字母,因此,重合指数$I_\tau$与明文的相同。

    - 否则, 认为固定间隔字符集中字符的概率都是相同的,$q_i \approx \frac{1}{26}$ 并且

      $$ I_\tau \approx \sum_{i=0}^{25} \left(\frac{1}{26}\right)^2 \approx 0.038 $$

      - 此时,假设所挑选出的密文是由明文字母被某个密钥中随机的字母映射所得到的,还假设密钥足够长且其中字母足够多样,则密文中每个字母出现是充分随机的,即出现概率为1/26,(在完美保密部分会进一步学习)。

    - 确定周期后,再次使用重合指数法寻找密钥中每个位置 $i$ 的字符 $k_i$.

    - 古典密码学最终都被破解说明一个道理:

      - 任意敌手原则 (Arbitrary Adversary Principle):对于一类具有指定能力的敌手们,对于其中任意一个敌手,安全必须被确保。换句话说,安全与否只考虑敌手能力,不受敌手具体策略左右。

22. 密码分析(作业)

    - 在COA下,对密文的需求与密钥空间规模有关。多表移位 > 单表替换 > 移位
    - 在KPA下,很容易破解。
    - 通过古典密码学到的教训:
      - 充分密钥空间原则
      - 设计加密方案是一项艰巨的任务
      - 复杂性不意味着安全
      - 任意敌手原则
    - 从上述古典密码学历史中可以认识到一个道理:从提出一个加密方案,到被该方案被破解,再到针对破解方法提出一个新加密方案,这种“打补丁式”的路线难以保证安全。那么,我们该如何研究密码学呢?
      - 凯撒无密钥,增加密钥得到移位密码
      - 移位密码密钥空间太小,增加替换变化程度得到单表替换
      - 单表替换中字母频率不变,增加位置因素得到多表替换
      - 多表替换中周期间隔的字母频率不变,怎么改造?

23. 现代密码学原则:定义,假设,证明

    1. 安全和威胁模型的严格定义的形式化
    2. 当一个加密方案的安全依赖于无法证明的假设时,这个假设必须被精确地描述并且尽可能地小
    3. 加密方案应该带有一个基于以上定义和假设对安全性的严格证明

24. 原则1,对精确定义的形式化

    - 如何形式化私钥加密的安全?
    - 已知密文,没有敌手能够找到密钥, $\mathsf{Enc}_k(m)=m$
    - 没有敌手能够找到与密文所对应的明文,$\mathsf{Enc}_k(m)=m_{0}\| \mathsf{AES}_k(m)$,其中$m_0$表示第一个比特
    - 没有敌手能够确定与密文所对应的明文中任意字符,$m=1000$, 但有人能知道 $ 800 < m < 1200$
    - 没有敌手能够从密文中获得关于明文的任何有意义的信息,但如何定义“有意义”?

25. 原则1,如何定义

    - 根据图灵对计算的定义,需要直觉,证明定义等价,用该定义来解决例子。
    - 后面将学习很多密码学相关定义,大家体会。

26. 原则2,依赖于精确的假设

    - 大多数密码学构造不能够被无条件的证明安全。
    - 换句话说,当假设为真,则构造安全。
    - 假设需要验证,方案需要比较,证明需要假设。
    - 简单,低级的假设容易被研究,拒绝和修正。

27. 原则3,安全性的严格证明

    - 安全性需要否定式证明,如何证明没有人能破解?如何证明“地球上没有龙”?
    - 规约法(Reduction):给定假设X很难是真,则根据定义构造Y是安全的。
    - 证明:将难题X的问题规约到破解Y的问题。
    - 这里X中每个问题都是难题,假设其无法解决。
    - 规约的意思是,可以将每个X中的问题转换为一个破解Y的问题,并且若一个破解Y的问题有答案,则可以由此答案构造一个对X中该问题的解。这里试图证明对Y的破解问题比X中问题要难。假设X中问题无法解决,而破解Y比X中问题还难,则破解Y也不可能。

28. 总结

    - 密码学保护信息、事务和计算安全
    - Kerckhoffs原则,开放密码学设计
    - 凯撒、移位、单表替换、多表替换
    - 蛮力,字母频率,Kasiski方法,重合指数(IC)
    - 充分密钥空间原则
    - 任意敌手原则
    - 严格证明安全

    

    

================================================
FILE: notes-Chinese/10 密码学协议动物园.md
================================================
# 10 密码学协议动物园

1. 本节学习密码学协议。这些协议以之前学习的密码学知识为基础实现了一些“奇妙”的事情!
2. 动物园地图
   - 密码学协议种类繁多,应用广泛。
3. 目录:略
4. 协议:
   - **通信协议**是为了一个特定目的的数字消息格式与交换规则的形式化描述
     - 协议之于通信,如算法之于计算
     - 每个人必须知道并同意服从协议
   - 无歧义:每个步骤必须被明确定义且无误解的可能
   - 完备性:对每个可能的情况都必须有一个明确的行为
   - 密码学协议:除了上述属性,还应该不可能比协议中说明的做的更多或者知道的更多
5. 协议类型
   - 仲裁协议:一个仲裁者是一个公正的可信第三方,帮助完成协议
   - 审判协议:一个法官是也是一个公正的可信第三方。与仲裁者不同,其不直接参与协议,而是来审判协议是否正确执行
   - 自强制协议:最佳的协议类型。协议本身保证公平性。
   - 例子:两人平分蛋糕协议。先分蛋糕的人后选。
6. 对协议的攻击
   - 被动攻击:攻击者不影响协议,例如窃听
   - 主动攻击:攻击者更改协议以获得优势
   - 作弊者:攻击者是协议中的一方
     - 被动作弊者:按照协议执行,但试图获得比协议所设定的更多的信息
     - 主动作弊者:在协议进程中干扰协议来作弊
7. 三次传递协议
   - 目的:两方之间无共享密钥下的保密通信
   - 类比:两人同一个箱子来传递一个秘密,该箱子可以上锁
     - 第一步,Alice将秘密放进箱子里,并上一把只有Alice自己有钥匙的锁,发送给Bob
     - 第二步,Bob收到箱子后,也无法打开箱子;在箱子上又上一把只有Bob自己能打开的锁,并发送给Alice
     - 第三步,Alice收到箱子后,打开自己之前上的锁,并发送给Bob;Bob收到箱子后打开自己上的锁,得到秘密
   - 这个方案需要加密方案具有一个性质:$\mathsf{Dec}_{k_1}(\mathsf{Enc}_{k_2}(\mathsf{Enc}_{k_1}(m))) = \mathsf{Enc}_{k_2}(m)$;用两个密钥分别加密两次后,用任意一个密钥解密,得到另一个密钥加密的密文
   - Shamir协议:$p$ 是一个素数,生成 $e,d$ 满足$\gcd(e,p-1)=1$ 并且 $ed \equiv 1 \pmod{p-1}$;加密$c = m^e$,满足上面的性质。
   - 弱点:中间人攻击,也叫水桶小队攻击。
     - Alice并不能确定和其通信的真的是Bob本人,Bob也不能确定对方是Alice。中间人攻击可以伪装成双方,与双方分别进行三次传递协议,与双方分别传递一个秘密,而双方并不知情。中间人可以获得Alice发给Bob的秘密,也可以伪造一个秘密发给Bob。
8. 中间人攻击
   - 在一个新场景,通信双方交换彼此公钥并传递密文,也存在一种中间人攻击。攻击者与受害双方分别独立建立连接,并且在双方之间中继消息,使得双方以为在和彼此通信。
   - 敌手在中间用自己的密钥来执行协议,交换后的明文没有变化,而Alice和Bob都无法发现他们实际上是在于攻击者通信。
9. 互锁协议
   - 介绍一种抵御上面的中间人攻击的方法,并不需要对双方身份进行鉴别。
   - 这是由Ron Rivest和Adi Shamir提出的,思路是将两个要交换的密文分成两部分,分别先交换密文的一半,然后再交换另一半。
   - 敌手收到一半密文后,因为没有得到整个密文,无法用自己的密钥解密的原本的明文,无法传递密文的一半。如果敌手自己产生一个明文,加密并发送,那么就无法最终令诚实方收到原本的消息。与其他身份鉴别方案结合,可以发现攻击者。
10. 双线性映射
    - 三个循环群之间存在一个关系,称为双线性映射(也叫双线性配对):两个群中两个元素的对可以被有效映射到第三个群中的元素。前两个群是同一个群,其中的两个元素$aP$和$bP$可以被有效映射到第三个群中的元素$P^{ab}$。由此,得到$G_1$中一对元素$aP$和$bP$与另一对元素$P$和$abP$将映射到$G_2$中的同一个元素。
    - 定理:若映射是有效的,则在$G_1$中的判断式DH问题,即给定$aP$和$bP$,判定一个元素是否是$abP$,就是一个容易的问题,因为可以判断一对元素$(P,cP)$是否映射到$e(P,P)^{ab}$。
    - Weil和Tate配对是两个常用的双线性映射,其中$G_1$是椭圆曲线群,$G_2$是有限域。
11. Jounx密钥交换协议
    - Jounx的一轮、三方密钥协商协议,其中Alice计算密钥$e(bP, cP)^a = e(P, P)^{abc}$;
    - 双线性DH(BDH)假设:给定$\left<P, aP, bP, cP \right>$,计算$e(P, P)^{abc}$是难题;
    - 定理:给定BDH假设,Jounx协议是安全的。
12. 基于身份的加密
    - IBE:不使用数字证书来实现公钥分发,直接用接收方的ID作为公钥,例如,直接用接收方的email地址作为其公钥。需要一个可信第三方来协助,即密钥生成中心KGC;
    - 接收方从KGC获得自己私钥;发送方需要预先获得KGC的公钥,但不再需要接收方的数字证书。
    - 优点:TTP在生成用户的私钥后可以被去掉,不需要PKI来分发密钥
    - 弱点:单点失效,隐式的密钥托管
13. Boneh-Franklin的IBE方案
    - 由Boneh和Franklin两人在2001年提出的IBE方案。
    - KGC负责产生一个全局公钥$sP$和私钥$s$,并为用户$A$生成一个私钥$d_{A} = sH_1(A)$
      - 用户私钥由KGC私钥和其身份得到
    - 加密:$\mathsf{Enc}(sP, A, m) = \left< rP, m\oplus H_2(e(H_1(A), sP)^r)\right>$,$c = (u, v)$
      - 用户公钥由KGC公钥和其身份得到
    - 解密:接收方从KGC获得其私钥$d_{A} = sH_1(A)$,并解密$\mathsf{Dec}(d_{A}, u, v) = v \oplus H_2(e(d_A, u)).$ 
      - 根据私钥无法获知$s$,因为这是一个离散对数问题
    - 正确性:$e(d_A, u) = e(sH_1(A), rP) = e(H_1(A), P)^{sr} = e(H_1(A), sP)^r$
14. 盲签名
    - 签名者在看不见消息的情况下对消息签名;
    - 类比隔着一个信封对一个文件盖一个钢印,然后打开信封,文件上有钢印;
    - Chaum的盲签名方案:
      - Alice将消息加密后发给签名者,类比于将文件装进了信封;
      - 签名者对密文签名,类比于隔着信封盖钢印;
      - Alice将加密消息的签名处理后得到原消息的签名,发送给Bob,类比于打开信封,发送带钢印的文件。
15. 群签名
    - 群签名是一种签名方案,一组人中某一个人的签名可以被公开验证是这组人中的某一个人签的,但不能确认到底是谁签的。这组人中有一个组长,他可以确认并证明是谁签的。
      - 有效性:群成员的签名可被验证有效;
      - 不可伪造:只有群成员可以进行有效签名;
      - 匿名性:不知道具体是谁签的;
      - 可追踪:群主可以知道是谁签的;
      - 不可关联:不能判断两个签名是否是同一个人签的;
      - 可开脱:不能伪造其他成员的签名,可以证明不是自己签的;
    - 一个简单的群签名方案:组长负责生成所有公私钥对,并发给每个组员一对。使用一个未被使用过的私钥来签名,并用所有公钥来验证。这个方案满足以上性质。
16. 环签名
    - 环签名是一种特殊的群签名,但其中不需要组长,并且无法确认到底是谁签的。
    - 基于双线性映射可以实现环签名。签名者用其他人的公钥和自己的私钥来签名,验证时需要使用所有人的公钥。
17. 秘密分享
    - 一个秘密在一组人中共享,每个人持有秘密的一部分,但当手里的秘密的份数没有达到某个阈值的时候,没有人能还原秘密;而当秘密的份数达到了某个阈值时,可以还原出秘密。
    - 例子,一个三维空间中的一个点,可以被分解为三个面;
    - 例子,中国剩余定理中将秘密分解为各个素数的群中元素;
18. Shamir的秘密分享
    - 利用一个线性多项式来分享秘密。秘密是该多项式中常数项,每一份秘密是该多项式的一个点。一个$t$次多项式,至少需要$t+1$个点来确定常数项,即秘密。
    - 例子,将一个2次多项式中的秘密分成6份,至少用3份可以恢复秘密。敌手如果有两个小秘密(两个点),但至少有3个未知数,包括$a_1$、$a_2$和$m_1 - m_2$,因此无法求解出任何未知数。
    - 优点:实现了信息论的安全
    - 缺点:每份秘密的正确性无法验证
19. 门限密码学
    - 在一个(t, n)门限加密方案中,需要至少n个参与方中的t个小秘密才可以解密密文或签名消息。
    - 利用Elgamal加密方案可以实现一个门限加密方案。采用Shamir的秘密分享方案令私钥 $s = \Sigma_i s_i\cdot \lambda_i$ ,其中$s_i = f(i)$是小秘密;$\lambda_i$ 是拉格朗日系数,是公开信息;并且公开发布 $h_i = g^{s_i}$ 。
      - 其中,公开$h_i$是用于后面验证秘密持有者持有小秘密$s_i$,即一个离散对数问题的解,并且用这个小秘密来对密文进行解密;
    - 加密:按照普通的Elgamal加密方案加密$(c_1, c_2) = (g^y, h^y \cdot m)$,$y$为新的随机数;
    - 解密:秘密持有者 $i$用自己的小秘密来解密,输出 $d_i = c_1^{s_i}$ 和一个关于 $\log_gh_i = \log_{c_1} d_i$ 的零知识证明(后面会介绍);
      - 这里秘密持有者利用自己的小秘密来实现解密,但不泄漏小秘密;同时,证明自己是秘密持有者;
    - 解密出消息 $ m = c_2/\Pi_i d_i^{\lambda_i} $,因为$c_2/\Pi_i d_i^{\lambda_i} = c_2/\Pi_i c_1^{s_i\cdot \lambda_i} = c_2/c_1^{\Sigma_i s_i\cdot \lambda_i} = c_2/c_1^s=m$。
20. 承诺方案
    - 互联网上掷硬币:利用哈希函数实现对承诺的绑定(binding),即信息和承诺一一对应,承诺后不能改变信息;和隐藏(hiding),即承诺本身不泄漏信息;
    - 掷硬币并对结果做出承诺:随机选择一个比特$b$为掷硬币结果,将$h = \mathsf{Hash}(b\|r)$作为承诺发送给对方;其中,$r$为随机串;这个承诺具有绑定和隐藏的功能;
    - 收到承诺的一方给出自己猜测的结果;此时,仍不知道实际结果,但条件是哈希函数需要隐藏信息;
    - 掷硬币一方揭示结果,由于抗碰撞性质,只能揭示$b\| r$,否则会被对方利用收到的承诺来识破;
21. 零知识证明
    - 一种交互式证明,其中证明方成功说服验证方:证明方知道某事,但同时除了该陈述外,不泄漏任何其他信息
    - 完备性:如果陈述是真的,那么诚实的验证方可以被诚实的证明方说服
    - 有效性:如果陈述是假的,那么没有作弊的证明方可以说服诚实的验证方
    - 存在性:如果单项函数存在,则存在对任意NP问题的零知识证明
    - 西格玛协议:分三轮:声明(承诺),挑战,响应
22. 一个玩具例子
    - 有一个环形山洞,山洞有一个入口,从A和B两条路可以进入洞内;在内部有一个魔法门,魔法门可以用一个咒语开启
    - Alice知道开启魔法门的咒语,并想向Bob证明,步骤分三步:
    - 声明(承诺):Alice随机选择一条路进入山洞内部并藏起来,Bob不知道Alice的选择;Bob站在洞口
    - 挑战:Bob向洞内大喊:“请从A/B方向出来”
    - 响应:根据Bob的挑战和Alice当时进入山洞的选择,Alice可以利用咒语开启魔法门,或者直接从洞中出来
    - 以上游戏重复多次,如果Alice的次次都通过挑战,那么Bob可以相信Alice知道咒语
    - 为什么这是零知识证明?
      - 表面的原因:这个游戏中对Bob有意义的唯一知识——魔法门咒语,Bob始终不知道
      - 更本质的原因:知道魔法门咒语和读心术(Alice预知验证者Bob给出挑战)之间不可区分
23. 汉弥尔顿环路的零知识证明
    - 汉弥尔顿环路是一个NPC问题:给定一个图,给出一个经过所有节点一次的环路。证明者知道一个图的汉弥尔顿环路;
    - 声明(承诺):首先,证明者将图重新做标记:将节点重新编号,并构造邻接矩阵(行列表示节点,两点之间有连接时置1,否则置0);将原节点编号和新编号对应关系(N个箱子,N为图中节点数量)以及新邻接矩阵(N*(N-1)/2个箱子)加密,全部发送给验证者
    - 挑战:验证者从两个挑战问题中随机选择一个,一是打开被加密消息中所有箱子,以揭示其与原图是同一个图;二是打开被加密的邻接矩阵中一个汉弥尔顿环路,但不打开原节点编号和新编号对应关系的箱子
      - 第一个挑战可以令验证者确认证明者的确对图做重新标记
      - 第二个挑战可以令验证者确认证明者的确知道一个环路,但泄漏给验证者答案
    - 响应:证明者根据挑战,或者揭示所有箱子,或者揭示一个汉弥尔顿环路
    - 零知识:知道汉弥尔顿环路和预知挑战问题之间不可区分
24. 零知识证明和承诺
    - 模拟范式:当一件事Y本来就可以从X得到,那么通过Y并不会从X额外获得什么;这个范式用于保证验证者不会通过证明过程额外知道其他知识;
    - 在关于是否知道RSA私钥的零知识证明中,验证者给一个密文C后,让证明者给出对应明文M,来验证证明者知道私钥
      - 当没有承诺协议时,验证者可能在不知道明文M时直接给出一个密文C,而证明者返回的消息M令验证者额外知道了M;
      - 当加入承诺后时,证明者在给出M之前,先给出对M的承诺,即不泄漏M,又对后面给出的M作出承诺;在验证者提供M后,证明者知道验证者已经知道M了,根据上面的模拟范式可知,之后验证者获得的M对于验证者也不是新信息。
25. Schnorr协议
    - 之前在数字签名中学习过的Schnorr身份认证协议就是一个对离散对数问题的零知识证明
    - 知道离散对数的解和预知挑战之间不可区分
26. 破解RSA能力的零知识证明
    - 知道RSA问题的解和操纵挑战c之间不可区分
27. 健忘传输
    - 健忘传输:发送者不知道信息是否被传递
    - 社会学家百万富翁问题:判断两个数(各自的工资)是否相同,但不暴露工资(如果两人相同,则知道对方工资)
    - **1**  Bob准备4个有锁的建议箱(上面有投递口)并标记上工资数额
    - **2**  Bob销毁所有建议箱的钥匙,除了那个标记为他自己工资的箱子的钥匙
    - **3**  Alice将“YES”的纸条放进标记为她自己工资的箱子中,将“NO”的纸条放进其他箱子中
    - **4**  Bob打开标记他自己工资的箱子,可以(也可以不)将纸条与Alice分享
    - 健忘传输:Alice一共发送了4个消息,但不知道Bob得到其中哪一个消息
28. Rabin的健忘传输协议
    - Alice向Bob发送消息,但不知道Bob是否可以解密并获得该消息
    - 1 Alice将消息$m$用RSA加密后发送给Bob,内容为$N, e, m^e \mod N$
    - 2 Bob将一个随机的二次剩余发送给Alice,内容为$x^2 \mod N$
    - 3 Alice求$x^2$的平方根$y$,这是本身是一个难题;由于Alice可以分解$N$,所以可计算出来,并将$y$发送给Bob
    - 4 如果$y \neq ± x$,Bob可以计算出$m$,即得到消息
    - 这个方案的原理在于,当$y \neq ± x$时,$y^2 - x^2 = 0$且$(y-x)$或$(y+x)$中有$N$的因子,Bob可以利用$\gcd(y-x,N)$将$N$分解,并计算出解密密钥$d$;由于每个二次剩余有四个根,因此,Bob有1/2的机会分解$N$并得到消息;但是,Alice并不知道$y \neq ± x$是否成立,也就不清楚Bob是否能得到消息
29. 二选一健忘传输
    - 二选一健忘传输:发送者发送了两个消息,知道接收者收到了两个消息中的一个,但不知道具体是哪一个;接收者只能随机地收到两个消息中的一个
    - 1 Alice将公钥和两个随机数发送给Bob,内容为$N, e, x_0, x_1$
    - 2 Bob随机生成一个比特$b$和一个随机数$k$,计算$v = x_b + k^e$并发送给Alice
    - 3 Alice尝试计算$k$,$k_0 = (v - x_0)^d$,$k_1 = (v-x_1)^d$,其中有一个是$k$,但Alice不知道是哪个;接着,将消息$m_0$和$m_1$分别用$k_0$和$k_1$加密得到$k_0 + m_0$和$k_1 + m_1$,发送给Bob
    - 4 Bob用$k$从中解密出一个消息$m_b$
30. 安全多方计算
    - 一群人用大家的输入共同计算一个函数,但保留各自输入的隐私
    - 密码学家午餐问题:一群密码学家在饭后判断是否有人买单,但不知道每个人是否买单;至多有一个人买单,如果买单,则输入为1,否则为0,这是一个布尔或的多方安全计算问题
    - 密码学家围坐在一个圆桌,相邻的人共同协商一个比特的秘密
    - 每个密码学家喊出一个消息:三个比特的异或值,包括自己是否买单的一个比特,和相邻的人协商的两个比特
      - 这一步使得每个密码学家是否买单是保密的,相邻的人也无法推测出来
    - 最后,每个人将所有密码学家喊出的消息异或,就得到了一个布尔或;如果结果为1,则说明有人买单;否则,还没有人买单;
      - 这是因为相邻的人协商的秘密都出现了2次,异或为0;每个人自己是否买单的比特都出了1次,当至多有一个1时,其“异或”结果与“或”的结果相同
31. 同态加密
    - 两个密文操作后,得到新密文;新密文解密后得到对应两个明文操作后的结果,即$\mathsf{Dec}_{sk}(c_1\circ c_2)=m_1\circ m_2$.
    - Elgamal加密方案是乘法的同态加密方案
    - Pailier加密方案是加法的同态加密方案
    - 应用:投票,计票,但不暴露投票内容
    - 第一个支持加法和乘法的完全同台加密方案在2009年由Craig Gentry提出
32. 端到端投票系统
    - 端到端投票系统
      - 投票:投票到投票机
      - 张贴:将票公开到公告烂
      - 计票:根据公告栏由选举官计票

    - 安全目标
      - 端到端可验证性:任何投票者确信按意愿投票,按投票来张贴,按张贴来计票
      - 隐私:没人知道投了什么票,甚至投票者也无法说服其他人她投了什么票;隐私意味着抗强迫!
33. 三票投票法
    - 原理:按行选,按列投
    - 每个投票者投三张票,每行是一个候选人,每列是一张票。每行做1或2个标记,选谁就做2个标记,不选谁就做1个标记。不能不做,也不能做3个标记。
    - 每张票有唯一的ID。所有票公布在PBB上。
    - 投票者将任意一张票的拷贝作为收据带回家。收据用对照PBB做完整性检查。
    - 是否是安全的端到端投票?
34. 量子密码学
    - 利用量子物理学规律的密码学
    - 量子物理学规律:超态,相干,纠缠,不可测
35. 量子密码学现状
    - 并未证明量子计算机比经典图灵机强
    - 有多项式时间的算法用量子计算机解决难问题:整数分解,离散对数
    - 一些问题还没有多项式时间量子算法,例如NPC问题,一些单项函数求逆,对称加密和MAC
36. 量子密钥分发
    - BB84 QKD,由**Bennett和Brassard (1984)**发明,利用光子偏振状态来在公开信道上传递消息,并可以发现窃听者
    - 利用光子偏振的方向来表达信息,分为+和x两种基(Basis),其中
      - +: 竖线表示0,横线表示1
      - x:撇表示0,捺表示1
      - 用与制备基相同的测量基来测量,则得到原始光子偏振方向;否则,得到随机的方向
    - 首先,Alice产生随机比特串,并用随机生成的一组制备基来产生相应的带偏振的光子,发送给Bob
    - 然后,Bob产生随机测量基来测量光子偏振,得到一个比特串
    - 最后,Alice和Bob公开自己的制备基和测量基,将使用了相同基处理和得到的部分比特串作为密钥的一部分;为了检查是否有人窃听,也就是在传递信道中对光子偏振测量;Alice和Bob分配公开一段相同基下得到的比特串,如果相同说明中间没有人窃听;如果敌手窃听,则会影响量子传输过程,光子被其他的基测量后会改变偏振方向,从而被监测发现
37. 总结:克拉克三定律之一:任何足够先进的技术和魔法是不可区分的。





================================================
FILE: notes-Chinese/2 完美保密.md
================================================
# 完美保密加密

1. 在本节课程中,我们学习信息论意义上的安全——完美保密。完美保密的安全在信息论上是无需前提假设的,但其存在实践上的局限性,是完美中的不完美。本节将学习若干“等价”的完美保密定义,从中体会看似不同的定义却存在相同的本质,并且体会理解对同一个概念,从不同的角度去定义,对理解和应用这个概念是至关重要的。

2. 目录

3. 回顾加密词法

   - 以小写字母表示一个具体值,用花体字母表示一个集合,用大写字母表示随机变量

   - 密钥,明文,密文分别为$k \in \mathcal{K}, m \in \mathcal{M}, c \in \mathcal{C}$.
   - 密钥生成,加密算法,解密算法分别为$k \gets \mathsf{Gen}, c:= \mathsf{Enc}_k(m), m:= \mathsf{Dec}_k(c)$.
   - 加密方案: $\Pi = (\mathsf{Gen}, \mathsf{Enc}, \mathsf{Dec})$.
   - 随机变量: $K, M, C$ 对应密钥,明文,密文.
   - 概率: $\Pr[K=k], \Pr[M=m], \Pr[C=c]$,随机变量为某一个具体值的概率

4. 完美保密(**Perfect Secrecy**)定义

   - 直觉:**一个加密方案是安全的,那么敌手在获得密文后,密文应该对敌手猜测明文没有任何帮助。**敌手是知道明文本来的概率分布,例如,敌手知道明文是一个真或假问题的答案:真、假,并且知道两种答案的概率。敌手也知道加密方案。敌手要根据密文确定明文中的答案。如果加密方案是安全的,则密文应该对敌手猜测答案没有任何效果。

   - 换句话说,根据密文来猜测答案和不知道密文猜测答案对敌手来说是一样的。从概率的角度看,在获得密文后的某个明文后验似然(posteriori likehood)应该与该明文被发送的先验概率(priori probability)没有差别。

   - 定义:在$\mathcal{M}$上$\Pi$是完美保密的,若对于$\mathcal{M}$上的任意概率分布, $\forall m \in \mathcal{M}$ 与 $\forall c \in \mathcal{C}$ , 且 $\Pr[C = c] > 0$:

     $$ \Pr[M=m | C=c] = \Pr[M=m]. $$

   - 上面的公式表示,给定密文的条件下,明文的概率分布与预先知道的相同,即知道密文对猜测明文没有帮助。

   - 下面看一个例子,这个方案是完美保密的吗?For $\mathcal{M}=\mathcal{K} = \{ 0,1 \} , \mathsf{Enc}_k(m)= m \oplus k$. 这里的$\oplus$是异或。

   - 尽管这个方案看起来很简单(可能是最简单的),但答案是肯定的,是完美保密。下面我们来证明。

5. 一比特上的完美保密

   - 这里假设$\mathcal{M}$上的概率分布是$\Pr[M=1] = p$和$\Pr[M=0]= 1-p$,计算$ \Pr[M=1 | C=0] $。
     - 这里的例子个与课件不同的,课件上是计算$ \Pr[M=1 | C=1] $
     - 注意:加密事件逻辑是从明文和密钥得到密文,而不是相反的。
   
   - 根据贝叶斯定理:
   
   $$ \Pr[M=1 | C=0] = \Pr[C=0 | M=1] \cdot \Pr[M=1] / \Pr[C=0]$$  
   
   $$ = \Pr[M \oplus K =0 | M=1] \cdot p / (\Pr[C=0 | M=1] \cdot \Pr[M=1]+\Pr[C=0 | M=0] \cdot \Pr[M=0]) $$
   
   $$ = \Pr[1 \oplus K = 0] \cdot p / (\Pr[1 \oplus K = 0] \cdot p +\Pr[0 \oplus K = 0] \cdot (1-p)) $$
   
   $$ = \frac{1}{2} p / (\frac{1}{2}p + \frac{1}{2}(1-p)) = p = \Pr[M=1]$$
   
   - 注意:$\Pr[1 \oplus K = 0] = \frac{1}{2} \neq \Pr[M=1, C=0] = p \cdot \frac{1}{2}$
   
   - 这里需要理解到,只要密钥是均匀随机的,密文的概率分布不受明文的概率分布的影响(注意密文不独立于明文,而是由明文和密钥一起决定的),密文不会携带明文的统计模式,从而安全。
   
6. 完美保密定义的等价公式

   - 在完美保密加密方案中,密文的先验概率等于其后验概率,即$ \Pr[C=c | M=m] = \Pr[C=c]$。
   - 这个从之前例子中可以体会到,无论是什么明文被加密,密文出现的概率不变。
   - 从右到左证明:
     - 两边同时乘以$\Pr[M=m]/\Pr[C=c]$,得到
     - $ \Pr[C=c | M=m] \cdot \Pr[M=m] / \Pr[C=c] = \Pr[M=m]$
     - 应用贝叶斯定理,左边等于 $ \Pr[M=m | C=c] \cdot \Pr[C=c] / \Pr[C=c] = \Pr[M=m | C=c]$
     - 得到完美保密定义:$\Pr[M=m \| C=c] = \Pr[M=m]$
   - 从左到右证明略。
   - 从另一个方向思考,在完美保密中,密文出现概率根据明文概率分布和密钥概率分布以及加密算法可以预先计算。给定任意明文,对加密结果的预期与预先计算结果是一样。

7. 完美不可区分性(**Perfect Indistinguishability**)

   - $\Pr[C=c | M=m_0] = \Pr[C=c | M=m_1]$
   - 在完美保密加密方案中,任意两个明文加密后为相同密文的概率是相同的。换句话说,无论用什么明文,加密后得到相同密文的概率是相同的。证明见幻灯片。
   - 不可区分是什么意思?任意明文加密成某个密文的概率都相同,攻击者无法区分出密文是由哪个明文加密得到的,具体见后面的窃听者不可区分实验。

8. 一次一密(**One-Time Pad (Vernam’s Cipher)**)

   - 将明文以比特串的形式与相同长度的密钥按位异或得到密文。解密时将密文与密钥按比特异或得到明文。这种加密方案称为“一次一密”。
   - 是一种完美保密的加密方案,道理和前面给出的一个比特异或加密的例子一样。证明见幻灯片。
   - 问题:除了一次一密,还有没有其它实现完美保密的方案?

9. 完美保密的局限性

   - 很容易观察到一次一密加密方案中密钥需要和明文一样长,难以存储和分享。把比特串长度换一个表达方式,换成比特串数量——比特串所构成空间的规模,意味着密钥数量需要和明文数量一样多。
   - 那如果密钥长度比明文短,或者说密钥数量比明文少,能否实现完美保密?答案是否定的。要实现完美保密一定需要密钥空间大于等于明文空间,即$|\mathcal{K}| \ge |\mathcal{M}|$。
     - 采用反证法证明,假设密钥数量比明文数量少$|\mathcal{K}| < |\mathcal{M}|$,则不可能实现完美保密。
     - 将从一个密文$c$解密得到的所有明文集合,表示为$\mathcal{M}(c) \overset{\text{def}}{=} \{ \hat{m} | \hat{m} = \mathsf{Dec}_k(c)\ \text{for some}\ \hat{k} \in \mathcal{K} \}$。
     - 对于一个密钥$k$,最多有个一个明文$m$使得$m = \mathsf{Dec}_k(c)$。这是因为如果有多个明文的话,就根本不是一个加密方案。
     - 因此,从一个密文解密出来的明文数量不会超过密钥数量,也就不超过明文总数: $|\mathcal{M}(c)|\le |\mathcal{K}| < |\mathcal{M}|$.
     - 那么,一定存在一个明文$m'$是无法由$c$解密出来的,即 $\Pr[M=m'|C=c] = 0 \neq \Pr[M = m']$。因此,不是完美保密。
   - 尽管有这个局限性,但一次一密也可以用在实践中,例如美国和苏联之间的“Red line”。

10. 二次加密:真实世界案例

    - $ c\oplus c'=(m\oplus k)\oplus (m'\oplus k)=m\oplus m'. $ 
    - 如果一个密钥用了两次,那么敌手会得到两次明文的异或值,这显然不是完美保密了,而且根据异或值结合之前学习的自然语言统计模式分析可以破解出明文。
    - 一个例子真实世界例子是,在MS-PPTP协议中,通信双方采用同一个密钥来加密双向相互发送的两个消息。
    - 改进方法是双方各两个方向(分别作为源和目的)的通信使用不同的密钥。

11. 香农定理(**Shannon’s Theorem**)

    - 前面的完美保密相关定义的可操作性不高,原因是不容易直接获得明文概率分布。香农定理使得完美保密的可操作性得到了很大提高。
    - 当明文空间、密钥空间和密文空间规模相同时,加密方案是完美保密的,当且仅当满足两个条件:
      - (1)每个密钥是从密钥空间中均匀随机生成的;
      - (2)对于任意明文和密文对,存在唯一的密钥使得该明文加密成该密文。
      - 证明见幻灯片。

12. 香农定理的例题

    - 请根据香农定理来分析。这里从更通用的形式来了解一次一密。
    - **讲义中通常不给出例题答案**

13. 窃听不可区分实验(**Eavesdropping Indistinguishability Experiment**)

    这里引入**密码学中最重要的思想实验**:存在一个挑战者,挑战敌手不能破解加密方案,并配合敌手做一个实验。

    1. 敌手根据自己的策略选择两个不同的长度相同的明文,并发送给挑战者;

    2. 挑战者随机挑选其中一个明文,并新生成一个密钥,用加密方案来加密选中的明文,得到密文(称为一个挑战),并将密文发送给敌手;
    3. 敌手根据收到的密文,猜测哪一个明文被加密了。如果猜对了,则敌手在这次实验中成功。

    实验中的一个重点在于实验可重复足够多次。每次实验中挑战者都是生成新的密钥。

14. 敌手不可区分(**Adversarial Indistinguishability**)

    - 下面给出一个新的完美保密的定义:对于完美保密的加密方案,在窃听不可区分实验中,任意敌手成功的概率等于1/2。
    - 1/2是敌手采用瞎猜策略时成功的概率。因此,完美保密也意味着任意敌手在实验中不会获得比瞎猜更好的结果,或者说敌手获得密文后也不会比瞎猜策略获得更大的优势。后面还会反复学习这一概念。
    - 直觉理解为什么是敌手不可区分是一个完美保密的等价定义:
      - 无论明文如何分布,也无论敌手如何挑选两个明文,在实验中由挑战者随机二选一,明文空间缩减为二,每个明文被选中的概率为1/2。
      - 如果加密方案是完美保密的,则敌手获得密文后猜测明文的后验似然也是1/2。
      - 如果加密方案不是完美保密的,则意味着敌手可以利用某个明文和密文获得比瞎猜更大的优势,则敌手成功的概率不等于1/2。
    - 例题:其中$\|$表示比特串连接,LSB表示最低有效位(the least significant bit)。如果你觉得是完美保密,请指出其和一次一密的关系;否则,请说明在不可区分实验中敌手如何成功。

15. 总结

    - 完美保密 = 完美不可区分 = 敌手不可区分
      - 知道密文对猜测明文没有帮助
      - 给定明文对推测密文没有帮助
      - 任意明文加密成某个密文的概率是相同的
    - 完美保密是可获得的。一次一密。
    - 香农定理(可操作的完美保密)

================================================
FILE: notes-Chinese/3.1 私钥加密与伪随机性-第一部分.md
================================================
# 私钥加密与伪随机性 第一部分

1. 在本节课程中,我们学习计算安全下的私钥加密和伪随机性的第一部分。我们会学习一个完整的现代密码学研究过程,从定义到假设,再到一个密码学方案,最后使用规约法来证明其安全性。

2. 目录:密码学的计算方法论,计算安全加密的定义,伪随机性,规约法,构造安全的加密方案

3. 计算安全思想

   - 完美保密局限性在于密钥需要很长,而且如果密钥不够长,则不能达到完美保密。Kerchhoffs提出另一个原则:**一个加密方案如果不是数学上,那必须是实践上不可破解的。** 不同于在完美保密部的信息论上的安全,计算安全放松了安全条件来追求实践中的安全,使得密钥相对于明文可以很短。
   - 计算安全:
     - 敌手在**可行的时间**内运行,破解密码的时间是有限的
     - 敌手以**非常小的概率**成功,能成功但可能性很小

4. 放松条件的必要性

   为什么相对于完美保密,要放松对安全的需求。考虑之前的不可区分实验,

   - 为了对抗蛮力攻击,需要限定敌手的能力;因为只要给了充足的时间来遍历$|\mathcal{K}|$,蛮力攻击一定会成功;
   - 为了对抗随机猜测,需要允许小到可忽略的**(negligible)**成功概率;因为瞎猜也有$1/|\mathcal{K}|$概率成功;

5. 具体法与渐进法

   - 具体法:限定时间和成功的概率为具体值;一个加密方案是$(t,\varepsilon)$-安全的,如果对任意敌手以时间 $t$ 运行,成功破解方案的概率最多是 $\varepsilon$。
   - 具体法的问题是缺乏规律性,无法描述密钥长度、时间和成功概率之间的关系。
   - 渐进法:计算复杂性理论使用是与输入规模$n$有关的函数来表示时间或空间复杂性。例如,快速排序算法的时间复杂性是$O(n\cdot \log n)$,其中$n$是问题的规模,这里是排序元素的个数。
   - 具体法和渐进法的区别之一是,一个是点,一个是线。

6. P=NP?

   - 如何定义“可行的时间”和“非常小的概率”?答案来自计算复杂性理论,理论上认为一个搜索问题(例如,获得密钥)是相对简单的,如果解决该问题算法的时间复杂性为问题规模参数$n$的多项式;而需要非多项式(包括指数)时间复杂性来解决的问题是难以被实际解决的。
   - 在计算复杂性理论中,问题可分为两类:
     - 一类可解的问题,称为P(polynomial time)问题,是指能够在问题规模的多项式时间内由确定性图灵机解决的问题;
     - 另一类包含P问题的更大范围的NP(nondeterministic polynomial time)问题,不能确定是否在多项式时间内可以解决,但能够在多项式时间内验证一个答案是否正确的问题;尽管理论上用非确定性图灵机可在多项式时间解决,但非确定性图灵机还无法实现;
     - 在NP问题中,包含一类相似的难题,尚未找到多项式时间算法,但这些问题中的一个若被解决了,则其它也能被解决,称为NP完全问题(NP-Complete);与NP完全问题一样难或更难的问题,称为NP难问题(NP-Hard);
     - 科学家们相信NP问题集合不同于P问题集合,在NP问题中有一些难题无法在多项式时间内解决,即P$\neq$NP;
       - 在一部穿越电视剧《天才基本法》中,一个情节是:P=NP被证明真成立。
     - 加密与计算复杂性:1955年,约翰·纳什在其给NSA的信中说,他猜测破解一个复杂的代码需要密钥长度指数的时间。如果如此,则意味着P$\neq$NP,因为解决问题所需时间不是多项式的,而验证答案是多项式的。
     - 因此,将多项式时间认为是“可行的时间”,而非多项式的指数时间被认为是“不可行的”;
     - 非常小的概率定义为,比任何多项式分之一都小。

7. 有效的计算

   - 一个算法是多项式时间的(polynomial time),如果存在一个多项式使得对于任意输入,算法都在该多项式步骤内结束。
   - 一个算法可以在多项式时间内以任何多项式时间算法作为子例程来运行;
   - 概率(probabilistic)算法有“掷硬币”的能力。其中,随机数生成器应该是为密码学用途来设计的,而不是C语言里的`random()`。相反地,没有随机性的算法就是确定性的;
   - 开放问题:概率性的敌手比确定性的敌手更强大吗?$\mathcal{P} = \mathcal{BPP}$ (限定错误的概率多项式)?

8. 可忽略的成功概率

   - 一个函数$f$是可忽略的,若对于任意多项式$p(\cdot)$,存在一个$N$使得对于所有整数$n>N$,$f(n) < \frac{1}{p(n)}$。

9. 渐进方法(Asymptotic)

   - 根据上面的基础,采用渐进方法来定义安全,所谓“渐进”是指不研究一个参数固定的问题的复杂性,而是研究时间复杂性随着问题参数$n$的变化而变化的规律;
   - 问题X(破解加密方案)是难的,若X不能由任何多项式时间算法以时间$t$解决,除非以可忽略的概率$\varepsilon$;
   - $t$和$\varepsilon$都描述为安全参数$n$(通常是密钥长度)的函数;
   - 注意:安全是对足够大的$n$值来说的;
   - 例如,例子中随着$n$的增加,破解的复杂性随密钥空间指数增加,加密方案更难破解。

10. 定义私钥加密方案

    - 回顾私钥加密相关定义

11. 窃听不可区分实验

    - 在窃听不可区分实验中,敌手和挑战者之间进行一个思维实验。敌手根据安全参数产生两个相同长度的不同消息,并发送给挑战者;挑战者根据安全参数生成密钥,并对随机选择的一个消息进行加密,将挑战密文发送给敌手。敌手输出一个比特,来表示对被加密消息的猜测,若猜对,则实验成功。
    - 一个敌手$\mathcal{A}$与一个挑战者$\mathcal{C}$进行3轮交互:
      1. $\mathcal{A}$选择两个长度相同、内容不同明文$m_0, m_1$,并发送给$\mathcal{C}$;
      2. $\mathcal{C}$根据密钥生成算法生成一个新密钥$k$,随机生成一个比特$b$并挑选一个明文$m_b$,加密$\mathsf{Enc}_k(m_b)$后得到挑战密文$c$,并发送给$\mathcal{A}$;
      3. $\mathcal{A}$输出对所加密明文的猜测$b'$,若$b=b'$,则$\mathcal{A}$成功;否则,失败;
    - 这与之前在完美保密中的不可区分实验类似的,区别在于本实验不是无条件的,而是输入“安全参数”,该参数将作用于安全定义。窃听不可区分实验既用在了信息论安全定义,也用在了计算安全定义,这就在两者之间建立了联系。
12. 私钥加密安全定义

    - 一个加密方案在出现窃听者时是不可区分加密,若对于任意概率多项式时间的敌手,存在一个可忽略函数,使得不可区分实验成功概率与1/2相比(两者间的差异)是可忽略的。
    - 其中,多项式时间和可忽略都是对于“安全参数”的函数。
13. 理解不可区分性的定义

    - 一次一密方案在出现窃听者时是否是不可区分的?
    - 若一个敌手一直在实验中失败,该方案是安全的吗?
    - 在两个连续窃听不可区分实验中,使用同一个密钥的概率有多大?
    - 若从密文中猜测到消息中最低比特的概率是3/4,该方案是安全的吗?
    - 若从密文中猜测到消息中最低3个比特的概率是3/8,该方案是安全的吗?
- 相关性:$X$和$Z$的分布不可区分,$Y$和$Z$的分布不可区分,那么$X$和$Y$的分布是不可区分的吗?
14. 语义安全(semantic security)

    - 之前在导论部分有一个问题:如何定义不泄漏“meaningful”的信息。下面引入语义安全的概念来解决这个问题。
    - 直觉:没有关于明文的任何有意义的信息泄漏
    - 关于明文的信息用明文的函数来表示,$h(m)$表示敌手预先了解的关于明文的外部信息,$f(m)$表示敌手希望获取的关于明文的有意义的信息
    - 定义:加密方案是窃听者出现时**语义安全的**,如果对于任意敌手,任意明文分布,任意函数$f$和$h$,一个敌手根据密文和$h(m)$获得$f(m)$,另一个敌手只根据$h(m)$获得$f(m)$,这两个敌手成功的概率之间的差异是可以忽略的
    - 定理:一个私钥加密方案是窃听者不可区分的,当且仅当该方案是语义安全的。
    - 证明略。直觉上,从右到左:若敌手能够在不可区分实验中成功(不是不可区分的),则意味着根据密文获得了关于区分明文的某些信息(不是语义安全);反之,若敌手能够获得关于明文的某些信息(不是语义安全),那么可以利用这些信息来区分明文(不是不可区分的)。
15. 伪随机性概念(Pseudorandomness)

    - 回顾之前完美保密的局限性,密钥长度需要和明文一样长才安全;计算安全中放松了安全的定义,那密钥能不能短一些,或者说能不能放松对随机性的要求,产生足够长但不完全随机的密钥?下面我们来学习伪随机性概念。
    - 真随机性不能由一个可描述的机制产生。这里的“可描述的机制”显然是不包括“掷骰子”,而是指确定性的机制;
    - 伪随机对于不知道其机制的观察者来说看起来是真的随机;
    - 一个固定的字符串谈不上是否随机/伪随机,随机/伪随机指的是产生字符串的**过程**;
    - 问题:能否绝对地证明随机性?不能,因为我们可能是不知道其机制的观察者。
16. 区分器(Distinguisher):统计测试

    - 一类判断是否随机的务实的方法是,从一个随机生成器中得到多个随机序列并进行一套统计测试。
    - 例如,序列中0和1的数量之差不应该太大,最大连续0的长度不应该太长等等。
    - 伪随机性意味着**下一比特不可预测**(next-bit unpredictable),通过所有下一比特测试等且仅当通过所有统计测试。(这是姚期智的贡献)
    - 问题是难以确定多少测试才足够?
17. 定义伪随机性的直觉

    - 直觉:从一个短的真随机种子生成一个长的随机串,这个伪随机串与真随机串是不可区分的。
    - 这是不是和图灵测试类似?
    - 区分器输入一个比特串,输出1位比特。*注意:该比特不一定表示输入的串是否是随机的。*
18. 伪随机生成器**(Pseudorandom Generator)**定义

    - 一个确定性的多项式时间算法$G : \{0,1\}^n \to \{0,1\}^{\ell(n)}$是一个伪随机生成器(PRG),如果:
      - 延展:$\forall n, \ell(n) > n$。只有生成更长的串才有意义,否则可以直接从种子中复制一段输出;
      - 伪随机:对于任意PPT区分器$D$,$\left|\Pr[D(r)=1] - \Pr[D(G(s))=1]\right| \le \mathsf{negl}(n)$。其中,$r$是随机的,种子$s$随机的,$\ell(\cdot)$是延展因子。这里的意思是输出不同结果的概率差可以忽略,如果有一个区分器始终输出1,则两个概率都是1,差为0;另外,输出1并不需要表示特定含义,改成输出0也可以。
    - 存在性:若单向函数存在或$\mathcal{P} \ne \mathcal{NP}$,则PRG存在。后面我们会进一步学习。
19. 真实案例

    - C语言的`random()`
    - Netscape早期版本的漏洞https://people.eecs.berkeley.edu/~daw/papers/ddj-netscape.html
- 从这两个例子可以看出来,输出都是可预测的。
20. 关于PRG的一些问题

    - 利用下一比特不可预测,还有PRG的不可区分实验定义可以解决这些问题。
21. 充分种子空间

    - 稀疏输出:当扩展因子为$2n$时,在长度为$2n$的串中只会产生$2^{-n}$。
    - 蛮力攻击:给定无穷的时间,通过枚举所有种子来产生所有串,能以较高的概率区分出伪随机串。
    - 充分种子空间:种子必须长来抵抗蛮力攻击。
22. 不充分的随机性

    - 2008年,为了避免一个编译警告,Debian的一个发布版本中误删了一行代码,引起OpenSSL中关于随机生成器的漏洞。
23. 规约法(**Reduction**)

    - 规约法是将一个问题A变换为另一个问题B。变换的意思可以理解为,A可以通过解决B来解决。
    - 规约$A \le_m B$:$A$可规约为B,如果B的解存在并且给定该解时A可解,其中$m$表示映射规约;这里可以将规约理解为A对B的子函数调用,除了子函数B是一个黑盒,解决A的步骤都应该是明确的。
    - 解决A不能比解决B更难,因为A可以通过解决B来得到解决。
    - 例题,测量矩形面积可规约到测量矩形边长;计算一个数的平方可规约到两个数乘积,相反可以规约吗?
24. 规约证明

    - 我们现在站在敌手的角色来思考,希望解决“破解”加密方案这个问题,并且在此之前我们已经知道有个一“假设”问题是不可解决的;
    - 为了证明一个加密方案$\Pi$在假设$X$下是安全的,就是证明“破解”问题不可解。
    - 将解决“假设”$X$问题的算法$\mathcal{A}'$规约到“破解”$\Pi$的算法$\mathcal{A}$。如果加密方案可以被破解,则假设问题也可以解决。然而,由于假设问题是难以解决的,这导致矛盾,说明加密方案不可以被破解。
    - 先令一个概率多项式时间的算法$\mathcal{A}$能够以概率$\varepsilon(n)$破解$\Pi$ ;
    - 假设:一个问题$X$是难以解决的,即不存在多项式时间算法来解决$X$;$\mathcal{A}'$是一个解决$X$的概率算法;
    - 规约:解决假设问题$X$可以通过破解加密方案$\Pi$,即将$\mathcal{A}'$规约到$\mathcal{A}$,$\mathcal{A}'$通过以$\mathcal{A}$作为子函数可以以概率$1/p(n)$有效地解决问题$X$;
    - 矛盾:若加密方案可以被有效破解,即$\varepsilon(n)$是不可忽略的,则$\mathcal{A}'$可以以不可忽略的概率$\varepsilon(n)/p(n)$解决问题$X$,这与假设矛盾,因而$\varepsilon(n)$一定是可忽略的。
25. 一个规约法证明PRG的例子

    - 假设$F$是PRG,证明$G$也是PRG。
    - 问题A:如何区分$F$;问题B:如何区分$G$;
    - 从A规约到B:区分$F$的算法输入按位取反后作为区分$G$的算法输入,区分$G$的算法输出作为区分$F$的算法输出。
26. 一个规约法证明PRG的例子(续)

    - 由此,建立了不可区分定义中概率的联系。
27. 一个安全的定长加密方案

    -  $|G(k)| = \ell(|k|)$, $m \in \{0,1\}^{\ell(n)}$, 一个PRG以长度为$n$的密钥作为种子,输出与明文相同长度的pad;
    - $\mathsf{Gen}$: $k \in \{0,1\}^n$,密钥作为种子,长度小于明文长度;
    - $\mathsf{Enc}$: $c := G(k)\oplus m$,加密方法和一次一密一样;
    - $\mathsf{Dec}$: $m := G(k)\oplus c$,解密也是;
    - 定理:该定长加密方案是窃听下不可区分的。
    - 直觉上,这个方案和一次一密是类似的,除了密钥更短并且用伪随机生成器生成的比特串来与明文异或。因为伪随机对于任何敌手都可以认为是真随机,所以对于敌手而言,该方案与一次一密是一样的。由此,我们得到了一个安全的加密方案,同时避免了一次一密的最大局限性——密钥过长。
28. 证明不可区分加密方案

    - 思路:区分伪随机性为难题假设,破解加密方案为规约的子函数。针对伪随机生成器$G$的区分器$D$以$\mathcal{A}$为子函数,使得当$\mathcal{A}$破解了$\Pi$则$D$可以区分出$G$,与$G$的伪随机性矛盾。注意这里我们用了符号$\tilde{\Pi}$来表示$\Pi$的一个变体,来刻画加密方案中可能使用了真随机串来加密;
    - 回顾针对伪随机生成器的区分器$D$的问题是,输入一个串$w$,输出一个比特;这里关键问题是输出的比特从何而来?
    - 将$D$规约到$\mathcal{A}$。回顾窃听者不可区分实验中,$\mathcal{A}$与一个挑战者进行3轮交互:
      1. $\mathcal{A}$选择两个不同明文$m_0, m_1$,并发送给挑战者;
      2. 挑战者生成密钥,并随机挑选一个明文$m_b$加密后得到挑战密文$c$,并发送给$\mathcal{A}$;
      3. $\mathcal{A}$输出对所加密明文的猜测$b'$,若$b=b'$,则$\mathcal{A}$成功;否则,失败;
    - 区分器$D$成为窃听不可区分实验中的挑战者,特别之处在于:在第2步,不需要生成密钥,而是直接以输入串$w$作为pad来加密,$c := w \oplus m_b$;根据$w$的两种可能,分两种情况:
      - 当$w$是由$G$生成的,即伪随机串,则$c$就是加密方案$\Pi$中密文,$\mathcal{A}$面对的就是$\Pi$;
      - 当$w$是真随机串,则$c$不同于加密方案$\Pi$中密文,而与一次一密中一样,$\mathcal{A}$面对的就是$\tilde{\Pi}$一次一密;
    - 回答前面关于$D$输出什么的问题:破解加密方案的$\mathcal{A}$成功时,$D$输出1;否则,$D$输出0。
29. 证明不可区分加密方案(续)

    - 规约完毕,证明$\mathcal{A}$在实验中成功的概率是可忽略的
      - 当$w$为真随机串$r$,就是一次一密,$\Pr[D(r)=1] = \Pr[\mathsf{PrivK}^{\mathsf{eav}}_{\mathcal{A},\tilde{\Pi}}(n)=1]=\frac{1}{2}$;
      - 当$w$为伪随机串$G(k)$,$\Pr[D(G(k))=1] = \Pr[\mathsf{PrivK}^{\mathsf{eav}}_{\mathcal{A},\Pi}(n)=1] = \frac{1}{2} + \varepsilon(n)$;
      - 根据伪随机生成器定义,上下两个公式相减,$\left|\Pr[D(r)=1] - \Pr[D(G(k))=1]\right| = \varepsilon(n) \le \mathsf{negl}(n)$;
      - 所以$\varepsilon(n)$是可忽略的,即$\Pi$是窃听者不可区分的。
    - 小结:通过规约将$\mathcal{A}$的不可区分实验成功的概率与$D$的区分器实验输出1的概率建立等式;分析输入真随机串时$D$输出1的概率(即不可区分实验成功概率)是1/2;根据PRG的定义,输入伪随机串时$D$输出1的概率(1/2+$\varepsilon(n)$)与输入真随机串时$D$输出1的概率(1/2)的差异时可忽略的。
30. 处理变长消息

    - 对于一个变长输出的伪随机生成器,前面的加密方案和安全性都成立;这是作业,其中一个关键是条件2,短串是长串的前缀。
31. 计算安全与信息安全

    - 敌手:PPT窃听者,无限算力窃听者;
    - 定义:不可区分性$\frac{1}{2} + \mathsf{negl}$,不可区分性 $\frac{1}{2}$;
    - 假设:伪随机,随机;
    - 密钥:短随机串,长随机串;
    - 构造:异或pad,异或pad;
    - 证明:规约法,概率论;



================================================
FILE: notes-Chinese/3.2 私钥加密与伪随机性 第二部分.md
================================================
# 私钥加密与伪随机性 第二部分

1. 本节课学习另外两种私钥加密安全理论:选择明文攻击(CPA)下不可区分性、选择密文攻击(CCA)下不可区分性以及相关的密码学原语、假设、构造和证明。这些攻击更好的刻画了现实世界中敌手的能力,相应的密码学方案也是目前真正在实际使用的。

2. 目录:流加密与CPA,CPA安全加密方案,操作模式,CCA安全加密方案

3. 流加密方案(Stream Cipher)

   - 首先介绍当有多个消息需要被传递时,如何利用之前学习的基于PRG的加密方案来保护消息。
   - 思路:受一次一密方案的启发,通过将变长消息与密钥的异或来加密
   - 流加密方案:通过将多个消息“拼成”一个消息,与伪随机的比特流(密钥流)异或来加密
   - 密钥流:由一个变长的伪随机生成器产生
   - 优点:逻辑简单,比分组密码更快
   - 缺点:难以做到安全
   
4. 采用流加密方案的安全多重加密

   - 同步模式:用一个流中不同部分分别加密各个消息;
   - 异步模式:以密钥和初始向量一起作为输入来产生流,每个明文的加密采用相同的密钥和不同的初始向量
     - **初始向量(Initial Vector)**,$IV$是随机选取的并且是公开的;其生成是随机的并不受控制,但生成后并不保密;密钥的生成是随机的并不受控制,但生成后也要保密。
   - 两种模式差异:
     - 同步模式适合持续通信场景,例如语音;异步模式适合间断通信场景,例如即时消息。

5. 流密码的安全性

   1. 现状:没有标准化和流行的方案,安全性仍有疑问,例如在802.11中WEP协议的RC4,线性反馈移位寄存器(Linear Feedback Shift Registers);
   2. 警告:不要使用任何流加密方案,如果一定需要的话,采用由分组加密方案构造的。
   3. eStream项目致力于设计安全的流密码

6. 相关密钥:真实世界例子

   - 用于多重加密的密钥(初始向量和密钥对)必须是独立的。否则,前面的攻击就会生效;
   - 对于802.11b WEP的若干攻击:
     - WEP为异步模式,$\mathsf{Enc}(m_i) := \left< IV_i, G(IV_i\|k) \oplus m_i\right>$
     - $IV$长度为24比特,在$2^{24} \approx$ 16M 帧后$IV$会产生重复;
     - 在一些WiFi网卡上,在电源重启后$IV$重置为0;
     - $IV_i = IV_{i-1} + 1$. 对于RC4,在40,000帧后可以恢复 $k$ ;

7. 多重加密(Multiple Encryptions)

   - 在一次一密中,一个密钥不可以用于对多个消息的加密;否则,就是不安全的。如果敌手能够获得用同一个密钥加密后的多个密文,则之前的方案都是不安全的;为此,我们需要新的加密方案来防御这样的攻击。
   - 多个明文的加密实验$\mathsf{PrivK}^{\mathsf{mult}}_{\mathcal{A},\Pi}(n)$,当一次加密多个明文时,窃听者敌手能够区分出两组明文吗?
   - 一个敌手$\mathcal{A}$与一个挑战者$\mathcal{C}$进行3轮交互:
     1. $\mathcal{A}$选择两个长度相同、内容不同明文向量$\vec{M}_0=(m_0^1,\dots,m_0^t)$, $\vec{M}_1=(m_1^1,\dots,m_1^t)$,其中两个向量中同一位置的明文长度相同$\forall i, |m_0^i| = |m_1^i|$,发送给$\mathcal{C}$;
     2. $\mathcal{C}$根据密钥生成算法生成一个新密钥$k \gets \mathsf{Gen}(1^n)$,一个随机比特$b \gets \{0,1\}$。对向量$\vec{M}_b$中每个明文加密 $c^i \gets \mathsf{Enc}_k(m_b^i)$ 得到一个密文向量 $\vec{C}=(c^1,\dots,c^t)$ ,并发送给$\mathcal{A}$;
     3. $\mathcal{A}$输出对所加密明文向量的猜测$b'$,若$b=b'$,则$\mathcal{A}$成功;否则,失败;
   - 这与之前的单个消息不可区分实验类似的,区别在于*用同一个密钥加密的多个消息*。敌手可以获得多个明文的密文,比单个明文不可区分实验中的敌手有更强的能力。

8. 多重加密安全定义

   - $\Pi$ 是**窃听者出现时不可区分的多重加密方案**,如果任意PPT的敌手$\mathcal{A}$,  存在可忽略的函数$\mathsf{negl}$ 使得

     $ \Pr\left[\mathsf{PrivK}^{\mathsf{mult}}_{\mathcal{A},\Pi}(n)=1\right] \le \frac{1}{2} + \mathsf{negl}(n).$

   - 根据这个定义,来分析迄今学习的密码学方案是否是多重加密不可区分的?

9. 攻击确定性加密方案

   - 问题:如果一个加密方案中加密算法是确定性的,即同一个明文会被同一个密钥加密成同一个密文,那么该加密方案是多重加密安全的吗?
   - 攻击:对于确定性加密方案,敌手可以构造$m_0^1 = m_0^2$ 并且 $m_1^1 \neq m_1^2$,然后当$c^1 = c^2$,输出 $b'=0$,否则 $b'=1$。
   - 因此,确定性加密方案不是多重加密安全的,我们需要新的密码学原语来防御多重加密攻击。接下来,我们介绍一种更强的攻击,其涵盖了多重加密攻击。只要防御了这个新定义的攻击,也就同时防御了多重加密攻击。

10. 选择明文攻击(**Chosen-Plaintext Attacks (CPA)**)(思考)

       - 敌手具有获得其所选择明文对应的密文的能力。

       - 第二次世界大战中的例子:美国海军密码分析学家相信密文“AF”表示日语中的“中途岛”;但美国将军不认为中途岛会遭到攻击;美国海军密码分析学家发送了一个明文,中途岛淡水供给不足;日本军队截获的明文,并发送了一段密文,“AF”淡水不足;美国军队派出三艘航空母舰并且取胜。

       - 这里例子里,美国海军密码分析学家选择了明文并得到了密文。


11. CPA安全实验

    - CPA不可区分实验 $\mathsf{PrivK}^{\mathsf{cpa}}_{\mathcal{A},\Pi}(n)$:
      1. 挑战者生成密钥 $k \gets \mathsf{Gen}(1^n)$;(这里与窃听者不可区分实验相比,密钥的生成提前了,这是为了下一步提供加密预言机)
      2. $\mathcal{A}$ 被给予输入 $1^n$ 和对加密函数 $\mathsf{Enc}_k(\cdot)$的**预言机访问(oracle access)** $\mathcal{A}^{\mathsf{Enc}_k(\cdot)}$ ,输出相同长度 $m_0, m_1$ ;
      3. 挑战者生成随机比特 $b \gets \{0,1\}$,将挑战密文 $c \gets \mathsf{Enc}_k(m_b)$ 发送给 $\mathcal{A}$;
      4. $\mathcal{A}$ 继续对 $\mathsf{Enc}_k(\cdot)$的预言机的访问,输出$b'$;如果$b' = b$,则$\mathcal{A}$成功$\mathsf{PrivK}^{\mathsf{cpa}}_{\mathcal{A},\Pi}=1$,否则 0。
    - 敌手对加密函数预言机访问是指,敌手以任意明文作为输入,可以从预言机得到对应密文。此处,密钥是已经提前生成的,因此才能通过加密函数预研机得到密文,但仍对敌手保密。预言机是一个形象的比喻,它是一个黑盒,只接收输入并返回输出;访问者不需要了解其内部构造。
    - 该实验与窃听者不可区分实验的区别在于,敌手可访问加密预言机,在实验过程中始终可以,包括在产生两个明文阶段,以及在收到挑战密文后猜测被加密明文阶段,获得任意明文被同一密钥加密的密文;而且密文是逐个获得,可以根据之前的明文和密文对来“适应性地”构造新的查询。
    - CPA敌手比多重加密的敌手更“强大”,因为多重加密敌手是可以一次性地获得一组密文,而CPA敌手可以根据已经获得的明文和密文“多次适应性地”再次获得密文。

12. CPA安全

    - $\Pi$ 是CPA不可区分加密方案 (CPA安全的),如果任意概率多项式时间算法$\mathcal{A}$,存在可忽略的函数$\mathsf{negl}$使得,

      $ \Pr\left[\mathsf{PrivK}^{\mathsf{cpa}}_{\mathcal{A},\Pi}(n)=1\right] \le \frac{1}{2} + \mathsf{negl}(n)$

    - 定理:CPA安全也是多重加密安全的。证明略。直觉上,CPA敌手比多重加密敌手更强大。

    - 之前的方案也难以实现CPA安全;

    - 多重加密安全意味着CPA安全?(作业)显然是否定的。那么,思考两种安全定义的区别成为解题的关键。

13. 伪随机函数(**Pseudorandom Function**)概念

    - 为了实现CPA安全,之前的PRG提供的随机性不够用了,需要新的数学工具为加密提供额外的随机性。为此引入伪随机函数(PRF),是对伪PRG的泛化:PRG从一个种子生成一个随机串,PRF从一个key生成一个函数;
    - 带密钥的函数**Keyed function** $F : \{0,1\}^* \times \{0,1\}^* \to \{0,1\}^*$ 
      - $F_k : \{0,1\}^* \to \{0,1\}^*$, $F_k(x) \overset{\text{def}}{=} F(k,x)$
      - 两个输入到一个输出,看上去像,但不是加密函数;输入key,得到一个一输入到一输出的函数;
    - 查表**Look-up table** $f$: $\{0,1\}^n \to \{0,1\}^n$ 需要多少比特信息存储? 
      - 查表是一个直接描述输入与输出间映射的表格,一个条目对应一个输入与一个输出;当该映射是随机产生的,是一个真随机函数;
    - 函数族**Function family** $\mathsf{Func}_n$: 包含所有函数 $\{0,1\}^n \to \{0,1\}^n$. $|\mathsf{Func}_n| = 2^{n\cdot2^n}$
      - 一个PRF是函数族中一个子集,key确定下的PRF是函数族中一个元素,一个查表是函数族中一个元素;
    - 长度保留**Length Preserving**: $\ell_{key}(n) = \ell_{in}(n) = \ell_{out}(n) = n$;密钥长度与函数输入、输出长度相同为$n$;没有特殊说明时,只讨论长度保留的函数;

14. 伪随机函数定义

    - 直觉上,一个PRF生成的带密钥的函数与从函数族中随机选择的真随机函数(查表)之间是不可区分的;然而,一个真随机函数具有指数长度,无法“预先生成”,只能“on-the-fly”(边运行、边生成)的使用,引入一个对函数$\mathcal{O}$的确定性的预言机访问(oracle access)$D^\mathcal{O}$。
    - 这里的预言机是一个抽象的函数。访问预言机,就是给出任意输入,得到该函数的输出。访问预言机的能力不包括了解正在访问的预言机具体内部构造。
    - 一个带密钥的函数是一个伪随机函数(PRF),对任意PPT区分器$D$,$\left|\Pr[D^{F_k(\cdot)}(1^n)=1] - \Pr[D^{f(\cdot)}(1^n)=1]\right| \le \mathsf{negl}(n)$,其中$f$是$\mathsf{Func}_n$中随机函数。
      - 这里区分器$D$是一个算法,可以访问预言机,但并不知道预言机背后是什么。
      - 这里不可区分性关键是,对真随机查表和伪随机函数,区分器输出相同结果概率的差异。区分器输出1或0本身没有,也无需,有特定语义。
    - PRF和PRG的关系在后面会学习,可以由PRG来构造PRF。

15. PRF例题

    - 问题一个固定长度的一次一密方案是一个PRF吗?
    - 对于一个PRF,在密钥保密和没有预言机访问时,给指定输入,能以不可忽略的概率猜测输出相关信息吗?
    - 如果是PRF,则给出该函数与查表的相似性;否则,给出一个区分器可以区分出该函数不是随机的。

16. 以PRF实现CPA安全

    - 新随机串 $r$,每次新生成一个随机串;
    - $F_k(r)$: $|k| = |m| = |r| = n$. 长度保留;
    - $\mathsf{Gen}$: $k \in \{0,1\}^n$.
    - $\mathsf{Enc}$: $s := F_k(r)\oplus m$, $c := \left<r, s\right>$. 密文包括两部分新随机串,以及异或输出;
    - $\mathsf{Dec}$: $m := F_k(r)\oplus s$. 
    - 定理:上述方案是CPA安全的。

17. 从PRF到CPA安全的证明

    - 思路:从PRF的区分器算法$\mathcal{D}$规约到加密方案敌手算法$\mathcal{A}$,区分器$\mathcal{D}$作为敌手$\mathcal{A}$的挑战者,敌手$\mathcal{A}$实验成功时区分器$\mathcal{D}$输出1。分两种情况,当输入真随机函数$f$时,相当于一次一密;当输入伪随机函数$F_k$时,为加密方案。
    - 规约:$\mathcal{D}$输入预言机,输出一个比特;$\mathcal{A}$的加密预言机访问通过$\mathcal{D}$的预言机$\mathcal{O}$来提供,$c := \left<r, \mathcal{O}(r) \oplus m \right>$;$\mathcal{D}$输出1,当$\mathcal{A}$在实验中成功。
      - 这里有两个预言机:$\mathcal{D}$访问的预言机$\mathcal{O}$,$\mathcal{A}$访问的加密预言机$\mathsf{Enc}_k$,后者不能直接访问前者的预言机。

18. 从PRF到CPA安全的证明(续)

    - 考虑真随机函数$f$的情况,分析不可区分实验成功概率$\Pr[\mathsf{PrivK}_{\mathcal{A},\tilde{\Pi}}^{\mathsf{cpa}}(n) = 1] = \Pr[\mathsf{Break}]$。敌手$\mathcal{A}$访问加密预言机可以获得多项式$q(n)$个明文与密文对的查询结果并得到随机串和pad$\{ \left< r_i, f(r_i) \right> \}$;当收到挑战密文$c=\left<r_c, s:=f(r_c)\oplus m_b\right>$时,根据之前查询结果中随机串是否与挑战密文中随机串相同,分为两种情况:
      - 当有相同随机串时,根据$r$可以得到$f(r_c)$,$m_b=f(r_c)\oplus s$,但这种情况发生的概率$q(n)/2^n$是可忽略的;
      - 当没有相同随机串时,输出是随机串,相当于一次一密,成功概率=1/2;

    - $ \Pr[D^{F_k(\cdot)}(1^n)=1] = \Pr[\mathsf{PrivK}_{\mathcal{A},\Pi}^{\mathsf{cpa}}(n) = 1] = \frac{1}{2} + \varepsilon(n). $
    - $ \Pr[D^{f(\cdot)}(1^n)=1] = \Pr[\mathsf{PrivK}_{\mathcal{A},\tilde{\Pi}}^{\mathsf{cpa}}(n) = 1] = \Pr[\mathsf{Break}] \le \frac{1}{2} + \frac{q(n)}{2^n}. $
    - $\Pr[D^{F_k(\cdot)}(1^n)=1] - \Pr[D^{f(\cdot)}(1^n)=1] \ge \varepsilon(n) - \frac{q(n)}{2^n}.$ 根据伪随机函数定义,$\varepsilon(n)$ 是可忽略的.
    - 小结:通过规约将$\mathcal{A}$的不可区分实验成功的概率与$D$的区分器实验输出1的概率建立等式;分析输入真随机函数预言机时$D$输出1的概率(即不可区分实验成功概率)是1/2+一个可忽略函数;根据PRF的定义,输入伪随机函数预言机时$D$输出1的概率(1/2+$\varepsilon(n)$)与输入真随机函数预言机时$D$输出1的概率(1/2)的差异时可忽略的。

19. CPA安全例题
    - $\mathsf{Enc}_k(m) = PRG(k\|r) \oplus m $, $r$ 是新的随机串。这是CPA安全的吗?
    - 从PRF到CPA安全:变长消息
    - 对于任意长度消息 $m = m_1, \dots , m_{\ell}$,$ c := \left< r_1, F_k(r_1) \oplus m_1, r_2, F_k(r_2) \oplus m_2, \dots, r_\ell, F_k(r_\ell) \oplus m_\ell\right> $
    - 推论:如果$F$是一个 PRF,那么 $\Pi$ 对任意长度消息是 CPA 安全的。
    - 问题:这个方案有什么缺点?
    - 有效性: $|c| = 2|m|$.  密文长度是明文长度的二倍,并且需要大量的真随机串。
20. 伪随机排列(**Pseudorandom Permutations**)

    - 为了提高对任意长度消息加密的效率,以及更高级的加密基础工具,学习伪随机排列PRP的概念;

    - 双射 **Bijection**: $F$ 是一到一的(一个输入对应一个唯一输出)且满射(覆盖输出集中每个元素);

    - 排列 **Permutation**: 一个从一个集合到自身的双射函数;

    - 带密钥的排列 **Keyed permutation**: $\forall k, F_k(\cdot)$是排列;类似带密钥的函数;

    - $F$ 是一个双射 $\iff F^{-1}$ 是一个双射;函数和逆函数都是双射;

    - 定义:一个有效的带密钥的排列 $F$ 是PRP,如果对于任意PPT的区分器$D$,

      $ \left|\Pr[D^{F_k(\cdot),F_k^{-1}(\cdot)}(1^n)=1] - \Pr[D^{f(\cdot),f^{-1}(\cdot)}(1^n)=1]\right| \le \mathsf{negl}(n) $

    - 问题:一个PRP也是一个PRF吗?

21. PRP例题

    - 对1比特的PRP、PRF的分析;
    - 交换引理:如果 $F$ 是一个 PRP 并且 $\ell_{in} (n) \ge n$,那么 $F$ 也是一个 PRF。
      - 一个随机排列和一个查表是不可取分的,PRP和随机排列不可取分,因此,PRP和查表是不可取分的。

22. 操作模式概念(**Modes of Operation**)

    - 操作模式是使用PRP或PRF来加密任意长度消息的方法;
    - 操作模式是从PRP或PRF来构造一个PRG的方法;
    - 将一个消息分成若干等长的块(分组,block),每个块以相似方式处理;

23. **Electronic Code Book (ECB)** 模式

    - 在窃听者出现时,是否是不可区分的?
    - $F$ 可以是任意PRF吗?

24. 对ECB的攻击

    - 为什么仍然可以识别企鹅?

25. **Cipher Block Chaining (CBC)**模式

    - $IV$初始向量,一个新的随机串;
    - 是CPA的吗?可并行化吗?F可以是任意PRF吗?

26. **Output Feedback (OFB) Mode**模式

    - 是CPA安全吗?可并行化吗?F可以是任意PRF吗?

27. **Counter (CTR) Mode**模式

    - $ctr$是一个初始向量,并且逐一增加;
    - 是CPA安全吗?可并行化吗?F可以是任意PRF吗?

28. CTR模式是CPA安全

    - 定理:如果$F$是一个PRF,那么随机CTR模式是CPA安全的。
    - 证明:其安全性与之前基于PRF的CPA安全证明类似,从PRF的伪随机假设规约到CPA安全加密方案。其中,对$ctr$的安全性直觉在于,$ctr$也是在加密前不可预测的,且每个块所用$ctr$都是不同的;
    - 当加密预言机是由真随机查表构成时,敌手多次访问加密预言机得到的$ctr$序列与挑战密文的$ctr$序列之间有重叠的概率$\frac{2q(n)^2}{2^n}$是可以忽略的;若没有重叠,则相当于一次一密;

29. CTR模式是CPA安全(续)

    - 规约与之前证明基于PRF的CPA安全加密方案一样,证明过程也类似。

30. 初始向量不应该可预测

    - 如果$IV$是可预测的,那么CBC/OFB/CTR模式不是CPA安全的。
    - 为什么?(作业)
    - 在SSL/TLS 1.0中的漏洞:记录$\#i$的$IV$是上一个记录$\#(i-1)$的密文块。
    - OpenSSL中API:需要用户输入$IV$,但$IV$应在函数内实现。当$IV$不充分随机时不安全。

31. 非确定性加密

    - 有三种通用的实现CPA安全的非确定性加密方法:
    - 随机化的:$r$随机生成,如构造5;需要更多熵,长密文
    - 有状态的:$r$为计数器,如CTR模式;需要通信双方同步计数器
    - 基于Nonce的:$r$只用一次;需要保证只用一次,长密文

32. 选择密文攻击 **Chosen-Ciphertext Attacks (CCA)**

    - CCA不可区分实验 $\mathsf{PrivK}^{\mathsf{cca}}_{\mathcal{A},\Pi}(n)$:

      1. 挑战者生成密钥 $k \gets \mathsf{Gen}(1^n)$;(为了下一步的预言机)
      2. $\mathcal{A}$ 被给予输入 $1^n$ 和对加密函数 $\mathsf{Enc}_k(\cdot)$和解密函数$\mathsf{Dec}_k(\cdot)$的**预言机访问(oracle access)** $\mathcal{A}^{\mathsf{Enc}_k(\cdot)}$ 和 $\mathcal{A}^{\mathsf{Dec}_k(\cdot)}$,输出相同长度 $m_0, m_1$ ;
      3. 挑战者生成随机比特 $b \gets \{0,1\}$,将挑战密文 $c \gets \mathsf{Enc}_k(m_b)$ 发送给 $\mathcal{A}$;
      4. $\mathcal{A}$ 继续对除了挑战密文$c$之外的预言机的访问,输出$b'$;如果$b' = b$,则$\mathcal{A}$成功$\mathsf{PrivK}^{\mathsf{cca}}_{\mathcal{A},\Pi}=1$,否则 0。

      定义:一个加密方案是CCA安全的,如果实验成功的概率与1/2的差异是可忽略的。

33. 理解CCA安全

    - 在现实世界中,敌手可以通过影响被解密的内容来实施CCA。如果通信没有认证,那么敌手可以以通信参与方的身份来发送特定密文。下一页有具体真实案例。

    - CCA安全性意味着“non-malleability”(不可锻造性,即改变但不毁坏),不能修改密文来获得新的有效密文。

    - 之前的方案中没有CCA安全,因为都不是不可锻造。

    - 对基于PRF的CPA安全加密方案的CCA攻击:

      - $\mathcal{A}$ 获得挑战密文 $c = \left<r, F_k(r)\oplus m_{b}\right>$,并且查询与$c$只相差了一个翻转的比特的密文$c'$,那么

        $m' = c' \oplus F_k(r)$ 应该与 $m_{b}$ 除了什么之外都相同?

    - 问题:上述操作模式也不是CCA安全的(作业)

    - 由此,可以总结出CCA下敌手的常用策略:

      - 修改挑战密文$c$为$c'$,并查询解密预言机得到$m'$
      - 根据关系,由$m'$来猜测被加密明文$m_b$

34. Padding-Oracle(填充预言机)攻击真实案例

    - CAPTCHA服务商为Web网站提供验证用户是否为人类的服务。为此,一个CAPTCHA服务器与Web服务器间事先共享一个密钥$k$,服务工作原理如下:
      1. 当Web服务器验证用户是否为人类时,生成一个消息$w$并以$k$加密,向用户发送一个密文$Enc_k(w)$;
      2. 用户将密文$Enc_k(w)$转发给CAPTCHA服务器;(可实施填充预言机攻击)
      3. CAPTCHA服务器用密钥$k$将密文解密,根据解密结果返回给用户信息:一个由$w$生成的图像,或者坏填充错误;
      4. 用户根据图像获得 $w$ 并将 $w$ 发送给Web服务器。
    - 在第2步,当恶意用户可以利用CAPTCHA服务器会返回给用户坏填充错误这一漏洞,来实施填充错误攻击。

35. Padding-Oracle(填充预言机)攻击

    - 在PKCS #5 padding(填充)标准中,为了将一个消息的长度“填充”到块长度的整数倍,在最后一个块中填充$b$个字节的$b$;必要时,添加一个哑块(dummy block,不包含消息的一个填充块)。存在一种攻击手段:当填充错误时,解密服务器返回一个“坏填充错误”,这相当于提供了一个解密预言机,最终可以获得整个明文;
    - 具体攻击原理:
      - 更改密文(包含$IV$部分)并发送给解密服务器;
      - 一旦触发了“坏填充错误”,则说明对密文的更改导致了填充部分内容的更改;否则,对密文的更改导致了原明文部分的更改;
      - 通过仔细修改密文来控制填充部分,从而获得消息长度和内容。

36. 填充预言机攻击:获得消息长度

    - 攻击的第一步判断消息是否为空:在单个块的CBC中,通过更改$IV$的首个字节,攻击者能够获知是否$m$是否为空。因为如果$m$是空的话,更改$IV$首个字节将更改解密出的填充内容,解密服务器就会返回坏填充错误(1比特信息),具体分析如下:
      - 如果$m$是空的,那么明文会添加一个哑块$\{b\}^b$;
      - PRP的输入为$IV\oplus \{b\}^b$;设$IV$的首个字节为$x$,则PRP的输入为$(x \oplus b) \| (\{\cdot\}^{b-1} \oplus \{b\}^{b-1})$;
      - 将$IV$的首个字节从$x$改成$y$变为 $y \| (\{\cdot\}^{b-1})$,不改变$c_1$解密得到的PRP的输入不会变,而解密出的明文会改变为 $(x \oplus y \oplus b) \| \{b\}^{b-1}$;
      - 上述明文首个字节一定不是$b$,这是填充格式错误,会触发服务器返回错误;
      - 如果上面的尝试没有触发错误,那么说明消息非空;下一步,发现消息长度是否为1字节,方法与上一步一样,区别在于只改变$IV$的第2个字节;如此继续,获得消息的长度;(作业)

37. 填充预言机攻击:获得消息内容

    - 一旦获得消息的长度,也就知道了填充的长度$b$,采用下面的方法来获得消息的最后一个字节内容,进而获得整个消息;
    - 更改密文中倒数第二块,来获得消息的最后一个字节$s$;
    - 明文的最后一个块 $m_{last} = \cdots s \| \{b\}^{b}$,密文的倒数第二个块  $c_{last-1} = \cdots t \| \{\cdot \}^{b} $;
    - 最后一块的PRP输入为$c_{last-1} \oplus m_{last} = \cdots (s \oplus t) \| (\{b\}^b \oplus \{\cdot \}^{b}) $;
    - 敌手更改 $c_{last-1}$ 为 $c_{last-1}' = \cdots u \| (\{\cdot \}^{b} \oplus \{b\}^{b} \oplus \{b+1\}^{b}) $;其中,$u$是敌手猜测的某个字节;
    - 解密获得最后一块明文$m'_{last} = c_{last-1} \oplus m_{last} \oplus c_{last-1}' = \cdots (s \oplus t \oplus u)\| \{ b+1 \}^b$; 
    - 如果没有返回坏填充错误,那么意味着填充了$b+1$个字节的$b+1$,所以 $s \oplus t \oplus u = (b+1)$ ,而 $s = t \oplus u \oplus (b+1) $ 。

38. 总结

    - 略

    



================================================
FILE: notes-Chinese/4 伪随机排列的实践构造-块密码.md
================================================
# 4 伪随机排列实践构造(块密码/分组密码)

1. 本节学习如何设计一个PRP(伪随机排列)。通过一个经典的加密方案DES来学习PRP的构造和相关安全分析。此外,简要介绍当前广泛使用的AES。

2. 目录:替换-置换网络、Feistel网络、DES、增加密钥长度、AES、差分分析与线性分析

3. 块(分组)密码(**Block Ciphers**)
   - 块密码 $F : \{0,1\}^n \times \{0,1\}^\ell \to \{0,1\}^\ell$. 是一个带密钥的函数。
   
     $F_k : \{0,1\}^\ell \to \{0,1\}^\ell$, $F_k(x) \overset{\text{def}}{=} F(k,x)$. 
   
     $n$ 是密钥长度, $\ell$ 是块长度.
   
   - 构造是启发式的,而非被证明了的;
   
   - 注意:虽然有“密码”二字,但在实践中,块密码被当作是一个PRP,而非加密方案;在之前AES的提案召集中要求,算法输出的范围应该与输入块的随机排列是不可区分的;
   
   - 方案被认为是“优秀的”,如果已知的最佳攻击具有的时间复杂性与蛮力搜索密钥大致相当
   
     - 一个$n=112$的加密方案,可以在$2^{56}$时间内被破解是不安全的;
     - 在渐进设定中,尽管$2^{\frac{n}{2}}$是指数,但与上面的例子一样,实际可能不安全;
   
4. 漫画

   - 略

5. 混淆-扩散范式(**The Confusion-Diffusion Paradigm**)

   - 目标:构造“简洁”的看上去随机的排列;强调“简洁”的原因在于直接存储随机排列所需的空间太大,需要 $2^{n\cdot 2^n}$ 比特;
   - 为此,香农提出了一种实现伪随机排列的方法——混淆-扩散范式;
   - 混淆:令密钥和密文的关系尽可能地复杂和难懂;一个大的随机排列$F_k(x)$可以由若干小随机排列$f_i(x_i)$来构造,将一个大输入分为若干小输入,$F_k(x) = f_1(x_1)f_2(x_2) \cdots f_{i}(x_{i})$;
   - 扩散:在明文统计特征中的冗余性在密文统计特征被消去;其中,统计“冗余性”是指明文中一部分信息可以从密文中获得;

6. 替换-置换网络(**Substitution-Permutation Network**)

   - 一种混淆-扩散范式的设计是替换-置换网络(SPN),如图所示:
     1. 明文首先与密钥混合(例如,异或);进行混淆
     2. 经过S盒的替换操作;进行混淆
     3. 经过P盒的置换操作;进行扩散
     4. 进入下一轮
   - 一个关键点是在结尾需要有一次密钥混淆操作,否则最后一轮替换和置换操作对于加密都是无效的;

7. 设计原则1——S盒的可逆性

   - S盒必须是可逆的,否则块密码不是排列;这可以从SPN构造中观察到,其中密钥混合(异或)和P盒(置换)都是排列操作,为了令SPN是排列,那剩下的S盒必须是可逆的;
   - 定理:令$F$为一个带密钥的函数,该函数由一个SPN定义,其中的S盒是1对1的且满射。无论密钥生成方案和轮次,$F_k$是对于任意$k$都是一个排列;

8. 设计原则2——雪崩效应(**Avalanche Effect**)

   - 雪崩效应:改变输入的一个比特影响输出的每个比特;
   - 严格雪崩条件:一个输入比特取补,每个输出比特都有50%的概率改变;
   - 比特独立条件:对于任意$i, j, k$,当改变一个输入比特$i$时,输出比特 $j$ 和 $k$ 应该独立改变;
   - S盒:改变1比特输入会改变至少2比特输出;
   - P盒:每个S盒的输出都被扩散到下一轮的不同S盒;
   - 问题:对于4比特的S盒,改变输入的1个比特,在经过$R$轮的SPN后,影响输出的$2^R$个比特;

9. 一个针对块密码的KPA框架

   - KPA:知道同一密钥下的若干明文/密文对;
   - 分析步骤:
     1. 观察明文、密文、密钥之间的关系;
     2. 设计基于上面关系的$t$比特的测试;
     3. 搜索$k$比特空间;一个猜测的密钥通过测试的概率为$2^{-t}$;
        1. 这里具有一般性。
     4. 用$p$对明文/密文对来确定密钥的期望是$2^{k-pt}$;期望足够小就可以,例如$2^{-10}$;
   - 分析16比特密钥的一轮SPN下的KPA:
     - 尽管这个例子“简单”得缺乏意义,但可以展示上述框架如何应用;
     - 之前提到的一个关键点是在结尾需要有一次密钥混淆操作,否则最后一轮替换和置换操作都是无效的。**因此,这个结构并不是一个完整的一轮SPN。在教材的第二版在这方面存在一个修正。**这里我们沿用第一版的内容,因为更容易理解,并且不影响对缩减了轮次的SPN存在弱点的分析。
     - 关系:PT $\oplus$ Key $\oplus$ Input-of-$S$-boxes $=$ 0;其中,根据SPN的可逆性,S盒输入可以通过密文得到;
     - 测试:$t=16$ 比特,因为Input-of-$S$-boxes $=$ PT $\oplus$ Key;
     - 搜索:密钥空间$k=16$比特;通过测试的概率是$2^{-16}$;
     - 确定:使用$p=1$对明文/密文对来确定密钥,期望为$1$;

10. 攻击轮次减少的SPN(作业)

       - 攻击一个1轮SPN:64比特块,128比特密钥(2个64比特子密钥),16x4比特的S盒,以及用异或来实现密钥混合;

       - 根据图中关系可以观察到,根据明文和密文知道的20个比特,密钥中未知的20个比特,以及4个比特来比较;

       - 猜测20比特密钥:第一个子密钥的16比特,第二个子密钥的4个比特;

       - 通过4比特测试的概率$2^{-4}$;

       - 需要8对明文/密文对来确定密钥使得期望小于1,期望$2^{20-4\times 8}$;

       - 破解的复杂性为$8\cdot 2^{20} \cdot 16= 2^{27} \ll 2^{128}$ ,其中,8是明文/密文对数,16是S盒数量(因为每次猜测只覆盖1个S盒对应的第2个子密钥中4个比特);


11. Feistel网络

    - 为了构造排列,要求SPN网络中S盒是可逆的,这对S盒的设计提出了要求;那么,能不能放松对S盒设计需求,同时保留排列的?
    - Feistel网络可以满足上面的需求:从若干非可逆组件构造一个可逆函数;
    -  $L_i := R_{i-1}$ 并且 $R_i := L_{i-1} \oplus f_i(R_{i-1})$;
    - 求逆: $L_{i-1} :=\ ?$
    - Luby-Rackoff定理:无论mangler函数$f_i{}$和轮次,对于任意$k$,$F_k$是一个排列;

12. Feistel网络的例题

    - 问题:当输入($L_0, R_0$)是下面两个情况中之一时,一个$r$轮Feistel网络的输出是什么?
      - 每个轮函数输出都是$0$,无论输入是什么;
      - 每个轮函数输出都是一个恒等函数,即输入和输出相同;

13. DES设计

    - 16轮的Feistel网络;64位块;56位密钥,48位子密钥 (64位密钥带有8个校验位)
    - 密钥编排: 56 bits $\xrightarrow[\text{left rotation, PC}]{\text{divided into two halves}}$ 48 bits.
    - 以初始排列开始 ($IP$) 以 $IP^{-1}$ 结束;
    - 轮函数 $f$ 是一个 32位 I/O 的不可逆函数;
    -  $f_i$ 由mangler函数 $\hat{f}_i$ 和子密钥 $k_i$ 来确定;
    - $S$盒是 4 到1 函数,将6位映射为4位;

14. DES算法概览

    - 略

15. DES的Mangler函数

    - 略

16. DES中S盒例子

    - 略

17. 密钥编排

    - 略

18. 弱密钥

    - 弱密钥:DES的密钥编排会产生相同的子密钥
    - 半弱密钥:产生两个不同的子密钥
    - 这些情况发生时,应该更换密钥

19. DES编年史

    - DES经历了一个从成为加密标准到安全性不足、到安全性增强、到被彻底破解的历程;

    - [1973] NBS (NIST) 发布标准召集公告;
    - [1974] DES 在联邦政府公告发布; 
    - [1977] DES 被发布为 FIPS PUB 46;
    - [1990]  $2^{47}$ 个明文的CPA下差分分析; 
    - [1997] DESCHALL 项目公开破解DES;
    - [1998] EFF(电子前沿基金会)的Deep Crack在56小时内花费\$250,000破解DES;
    - [1999] 三重 DES
    - [2001] AES 在 FIPS PUB 197 发布;
    - [2004] DES标准 FIPS PUB 46-3 被撤销;
    - [2006] COPACOBANA 在9天内花费1万美元破解DES;
    - [2008] RIVYERA 在1天内破解 DES;
    - [2016] Hashcat用8块GTX 1080Ti在2天内破解DES;
    - [2017] 利用CPA攻击,针对一个特定明文在25秒内获得密钥;

20. 双重加密

    - 为了弥补DES密钥长度不足的缺点,增强加密安全性有两种思路:从内部修改 vs. 黑盒构造;
    - 从内部修改不可行,因为即使以最小的方式修改DES,也将失去我们已经从DES获得的信心;
    - 双重加密:$y = F'_{k_1,k_2}(x) \overset{\text{def}}{=} F_{k_2}(F_{k_1}(x))$;
    - 密钥长度乘2会更安全吗?

21. 中间相遇攻击(**The Meet-In-the-Middle Attack**)

    - 双重加密在**中间相遇攻击(MITM)**下并不比原本的DES更安全;

    - 在已知明文攻击(KPA)下,从输入方向输入一个明文,通过一次DES加密,猜测不同密钥来得到一组中间值,保存这些密钥和中间值对;从输出方向反向输入一个密文,通过一个DES解密,猜测不同密钥来得到另一组中间值,也保存这些密钥和中间值对;这两组中间值中相同的为$z_0$,相应的两个密钥$k_1$和$k_2$就可能是实际密钥。
    - $z_0 = F_{k_1}(x) = F^{-1}_{k_2}(y) \iff y = F'_{k_1,k_2}(x)$.
    - 密钥对 $(k_1,k_2)$ 满足上面等式的概率为 $2^{-n}$;因为中间值有$2^n$中可能;
    - 这样的密钥对数量为 $2^{2n}/2^n = 2^n$;这是密钥对数量乘以满足等式的概率;
    - 用另两个明文/密文对,密钥对的期望数量为 $2^{n}/2^{2n}=2^{-n}$. 足够小,因此剩下的就是密钥!
    -  $\mathcal{O}(2^n)$ 时间复杂性并且 $\mathcal{O}(2^n)$ 空间复杂性,这是一种典型的空间换时间的设计;
    - 可见,双重DES在时间复杂性上与DES没有区别。

22. DESX(XEX模式)

    - 为了增强DES并对抗中间相遇攻击,DESX通过**密钥白化**来增加有效密钥长度;
    - **白化(whitening)**:一个**xor-enc-xor(XEX)**模式,用部分密钥来与输入和输出进行异或;
    - DESX:$k = (k_1,k_2,k_3), |k_1|=|k_3|=64, |k_2|=56$;
    - 加密 $y = k_3\oplus F_{k_2}(x \oplus k_1)$;
    - 解密 $x = k_1\oplus F^{-1}_{k_2}(y \oplus k_3)$;
    - 安全性:$|k|=184$,在MITM攻击下,破解所需时间 $2^{64+56}$;原因在于,为了获得中间值需要从输入或输出中一个方向猜测2个密钥;

23. 三重加密(**Triple Encryption**)

    - 三重DES **Triple-DES (3DES)**:以连续的加密,解密,加密三个DES来加密明文,根据密钥选择分三种情况:
      -  $k_1 = k_2 = k_3$: 相当于一次DES,向后兼容,即采用该方案的通信方可以与采用普通DES的通信方互相通信;
      -  $k_1 \neq k_2 \neq k_3$: 在MITM攻击下,破解时间为 $2^{2n}$ ;这与DESX类似,某个方向需要猜测两个密钥;
      -  $k_1 = k_3 \neq k_2$: 只有两个不同密钥,用一个I/O对的破解时间为 $2^{2n}$ ; 用$2^{n}$个I/O对的破解时间为 $2^n$;
    -   安全性更强,但块长度仍然不变并且更慢;

24. 高级加密标准 AES(**The Advanced Encryption Standard**)

    - 1997年,NIST召集高级加密标准 AES提案;
    - 2001年,J. Daemen 和 V. Rijmen设计的Rijndael成为AES;
    - AES是第一个用于绝密信息的公开可用密码;
    - 设计目标不仅包括安全,还包括有效性和灵活性等;
    - 128位块长度,128,192,或256位密钥;
    - 并非一个Feistel结构,而是一个SPN;
    - 对于减少轮次的变体只有非简单的攻击:
      - 对于 6/10轮的128位密钥,$2^{27}$ 时间;
      - 对于 8/12轮的192位密钥,$2^{188}$ 时间;
      - 对于 8/14轮的 256位密钥,$2^{204}$ 时间;

25. AES概览

    - 动画

26. SM4(思考)

    - 我国商用密码标准SM4是分组密码的国家标准,用于无线局域网和TLS。
    - SM4由吕述望老师主要开发,2006年解密,2012年由国家密码局发布。
    - SM1以芯片实现,和SM7用于轻量级场景,也都是对称加密方案,都保密,未公开。
    - 问题:为什么这些国密标准不公开,或者很晚才公开?

27. 线性密码分析(**Linear Cryptanalysis**)

    - 下面内容来自于“[A Tutorial on Linear and Differential Cryptanalysis](https://www.engr.mun.ca/~howard/PAPERS/ldc_tutorial.pdf)”

    - 针对DES的密码学分析的重点是分析S盒,因为S盒是DES中唯一的非线形部分,输入和输出之间关系被有意地设计成难以简单描述;线性分析就是要通过分析来寻找输入和输出之间的线性关系,从而破解加密方案;

    - 在输入和输出之间的线性分析:对于随机选择的输入$x$和密钥$k$,有 $y=F_k(x)$,在比特位置 $i_1, ... ,i_\ell$ 与 $i_1', ... , i_\ell'$ 之间存在**偏差(bias)**  $p$ , 之所以称为“偏差”,是与“正常”概率$\frac{1}{2}$相比而言;

      $ \Pr [x_{i_1} \oplus \cdots \oplus x_{i_\ell} \oplus y_{i_1'} \oplus \cdots \oplus y_{i_\ell'} = 0] = p+\frac{1}{2}. $

      线性关系就是指这些比特的异或值的统计结果与随机值之间存在偏差,**无论异或结果为0还是为1**,重点在于这些位置比特之间存在线形关系。

    - 当偏差较大时,如极端情况$p=\frac{1}{2}$,可以认为输入中若干位置异或值等于输出中若干位置异或值;

    - 采用KPA(无需CPA)进行线性分析攻击的步骤:

      1. 构造S盒的线性近似表(linear approximation table),从而穿透S盒;
      2. 构造带有较大偏差的$r$轮SPN的前$r-1$轮的线性近似关系,从而建立了明文和最后一轮输入的线性关系;
      3. 根据已知的最后一轮输入和输出提取最后一轮的子密钥中若干比特,这部分密钥满足上一步建立的线性近似关系;

28. 一个对S盒进行线性分析的例子

    - 以一个4位到4位的S盒为例,图中表格按列分为三个部分:$X$各比特值,$Y$各比特值,线性关系($X$中若干比特异或值,$Y$中若干比特异或值,本例子中列出了3组);按行共16行,每行对应一个$X$值,内容包括(由S盒决定的)相应的$Y$值和线性关系;
    - 以第一行为例,$X=0000$,根据S盒构造可知$Y = 1110$;第一组线性关系,$X_2 \oplus X_3 = 0 \oplus 0 = 0$,$Y_1 \oplus Y_3 \oplus Y_4 = 1 \oplus 1 \oplus 0 = 0$;
    - 统计$X_2 \oplus X_3 $等于$Y_1 \oplus Y_3 \oplus Y_4 $的情况,一共12个;偏差为$12 - 16/2 = 4$;

29. 一个线性分布表的例子

    - 将$X$/$Y$中选择的比特位置表示为一个16进制整数来作为行号/列号,其中选中的位置为1,未选中的为0;例如,$X_2 \oplus X_3 $中选择了第2、3比特,表示为0110 = 6;$Y_1 \oplus Y_3 \oplus Y_4 $中选择了第1、3、4位,表示为1011 = B;
    - 根据此前对S盒的线性分析结果在表格中填入偏差;例如,$(6, B)=4$;
    - 至此,我们可以认为S盒被“穿透了”:找到了S盒上输入与输出的线性关系;

30. 一个线性密码分析的例子

    1. 从上向下,第一轮S盒线性分析结果为$S_{1,2}$: $x_1 \oplus x_3 \oplus x_4 = y_2$;其中,S盒输入$x_1, x_3, x_4$为明文和第一轮子密钥中$p_5, p_7, p_8$和$k_{1,5}, k_{1,7}, k_{1,8}$的异或值;

    2. 第2轮S盒线性分析结果为$S_{2,2}$: $x_2 = y_2 \oplus y_4$,输出的2个比特影响最后一轮的2个S盒输入$u_{3,6},  u_{3,14}$。

    3. 将输入、密钥和最后一轮S盒输入间关系表达出来:$ p_5 \oplus p_7 \oplus p_8 \oplus k_{1,5} \oplus k_{1,7} \oplus k_{1,8} \oplus k_{2,6} \oplus k_{3,6} \oplus k_{3,14} = u_{3,6} \oplus  u_{3,14} $,

       其中,密钥比特异或部分$\Sigma{k} = k_{1,5} \oplus k_{1,7} \oplus k_{1,8} \oplus k_{2,6} \oplus k_{3,6} \oplus k_{3,14}$ 是由密钥决定的一个固定的值。根据前面线性关系的含义,无论$\Sigma{k}$是0还是1,都有一个线性关系$ p_5 \oplus p_7 \oplus p_8 = u_{3,6} \oplus u_{3,14} $;

    4. 从SPN的输出反向分析,$u_{3,6} \oplus u_{3,14} $ 由密文和第4个子密钥的所有偶数位异或值决定;

    5. 猜测第4个子密钥的所有偶数位,满足上面线性关系的就可能是真的密钥;需要$2^8$时间;

    6. 重复上面的过程,逐渐获得所有密钥;

31. 差分密码分析(**Differential Cryptanalysis**)

    - 与线性分析类似,但分析的是特定输入差异 $\Delta_X$ 产生特定输出差异 $\Delta_Y$ 的概率 $p \gg 2^{-n}$,

       $x_1\oplus x_2=\Delta_X$, $F_k(x_1) \oplus F_k(x_2)=\Delta_Y$ 的概率 $p$.

    - 当$p$远大于随机概率$2^{-n}$时,可以认为输入差异与输出差异间存在关系;

    - 这个攻击需要通过CPA进行,因为需要构造特定输入差异,攻击步骤:
      - 构造S盒的差分分布表(difference distribution table)穿透S盒;
      - 构造带有较大偏差的$r$轮SPN的前$r-1$轮的差分特征,从而建立了明文和最后一轮输入的差分特征关系;
      - 根据已知的最后一轮输入和输出提取最后一轮的子密钥中若干比特,这部分密钥满足上一步建立的差分特征关系;

32. 一个对S盒进行差分分析的例子

    - 以一个4位到4位的S盒为例,图中表格按列分为三个部分:$X$各比特值,$Y$各比特值,差分结果($\Delta_X$下的$\Delta_Y$,本例子中列出了3组);按行共16行,每行对应一个$X$值,内容包括(由S盒决定的)相应的$Y$值和$\Delta_Y$值;
    - 以第一行第一列为例,$X=0000$,根据S盒构造可知$Y = 1110$;根据$\Delta_X = 1011$,可知 $X' = \Delta_X \oplus X = 1011$,进而得到 $Y' = 1100$ ,有 $\Delta_Y = Y \oplus Y' = 0010$;
    - 统计不同$\Delta_X$时$\Delta_Y$各个值的频率,找出频率较高的情况,例如0010出现了8次;

33. 一个差分分布表的例子

    - 将$\Delta_X$和$\Delta_Y$用16进制整数来表示,作为行号/列号,其中填入出现频率;例如,$\Delta X = 1011 = B, \Delta Y = 0010 = 2$,出现了8次,于是 $(B, 2) = 8$;
    - 至此,我们可以认为S盒被“穿透了”:找到了S盒上差分特征;

34. 一个差分密码分析的例子

    - 与线性分析时类似,可以得到$\Delta_P$与$\Delta_U$间关系,而不需要关心中间子密钥;
    - 同样从SPN的输出反向分析,猜测第4个子密钥的所有偶数位,满足上面差分特征的就可能是真的密钥;需要$2^8$时间;
    - 重复上面的过程,逐渐获得所有密钥;

35. 块密码补充

    - 块长度应该足够大;
    - 块密码不能抵御消息篡改;
    - Padding:填充n个内容为n的字节,或哑块;
    - 流密码 与 分组密码:流密码更快但安全性更低,应该采用块密码的“流密码模式”;

36. 总结

    略;



================================================
FILE: notes-Chinese/5 伪随机对象的理论构造.md
================================================
# 5 伪随机对象的理论构造

1. 本节学习如何设计基于单向函数存在的假设从理论上构造PRG、PRF、PRP这三个伪随机对象。

2. 目录:单向函数(One-Way Function),从OWF到PRP

3. 概览
   - 现代密码学的贡献之一是,单向函数的存在等价于所有(有意义的)私钥密码学的存在;
   - 我们学习一系列密码学对象的构造过程:从OWF构造核心断言(HCP),构造RPG,构造PRF,构造PRP,构造安全私钥加密方案,而安全私钥加密方案就是一个OWF,从而形成一个闭环;
   
4. 单向函数(**One-Way Functions (OWF)**)

  - 单向函数是一个正向易于计算(多项式时间),而逆向难以计算(无多项式时间);
  - 下面的单向函数定义是由姚期智提出的;
  - 求逆实验 $\mathsf{Invert}_{\mathcal{A},f}(n)$:
    1. 随机产生输入 $x \gets \{0,1\}^n$. 计算 $y := f(x)$. <u>*注:挑战$y$是由随机产生的$x$得到的,而不是直接随机挑选一个$y$;*</u>
    2.  $\mathcal{A}$ 以 $1^n$ 和 $y$ (挑战)作为输入,并输出 $x'$.
    3. 实验成功 $\mathsf{Invert}_{\mathcal{A},f}(n) = 1$ ,如果 $f(x')=y$, 否则 0. <u>*注:这里不需要$x'= x$;*</u>

5. OWF/OWP的定义 [Yao]

  - 定义:多项式时间算法 $M_f$ 和 $\mathcal{A}$.

    一个函数 $f\;:\; \{0,1\}^* \to \{0,1\}^*$ 是单向函数,如果满足:

    易于计算: $\exists$ $M_f$: $\forall x, M_f(x) = f(x)$. *注:这里说明计算不需要用原本的函数,只要结果相同就可以*

    难以求逆: $\forall$ $\mathcal{A}$, $\exists\;\mathsf{negl}$ 使得,$\Pr[\mathsf{Invert}_{\mathcal{A},f}(n)=1] \le \mathsf{negl}(n)$ 或者 $\Pr_{\substack{x \gets \{0,1\}^n}}[\mathcal{A}(f(x)) \in f^{-1}(f(x))] \le \mathsf{negl}(n). $ 

  - *注:后半部分是难以求逆的另一种表达*

6. 若干候选的单向函数

  - 乘法与分解(**Multiplication and factoring**):$f_{\mathsf{mult}}(x,y)=(xy,\|x\|,\|y\|)$, $x$ 和 $y$ 是相同长度的质数;*注:后面会学习RSA问题*
  - 模平方和平方根(**Modular squaring and square roots**):$f_{\mathsf{square}}(x)=x^2\bmod N$;*注:也被应用于公钥密码学*
  - 离散指数与对数(**Discrete exponential and logarithm**):$f_{g,p}(x)=g^x\bmod p$;*注:后面将学习DH密钥交换协议*
  - 子集和问题(**Subset sum problem**):$f(x_1,\dotsc,x_n,J)=(x_1,\dotsc,x_n,\sum_{j \in J} x_j)$;*注:子集和问题判定是否存在一个子集中元素之和为给定的值*
  - 密码学安全哈希函数(**Cryptographically secure hash functions**):稍后会学习;

7. 单向函数例题

  - 单向函数的理由在于,如果$f'$不是单向的,那么$f$也不是;
  - 不是单向函数的理由在于,$f'$可以容易求逆;
  - 另外,要注意求逆实验是从随机挑选的$x$得到$y$,并不能直接指定$y$;

8. 核心断言 **Hard-Core Predicates (HCP)**

  - 一个函数 $\mathsf{hc}\; : \; \{0,1\}^* \to \{0,1\}$ 是一个函数$f$的核心断言( **hard-core predicate**),如果
    -  (1) $\mathsf{hc}$ 可在多项式时间计算;
    -  (2) $\forall$  概率多项式时间 $\mathcal{A}$, $\exists\; \mathsf{negl}$ 使得 $ \Pr_{\substack{x \gets \{0,1\}^n}}[\mathcal{A}(f(x)) = \mathsf{hc}(x)] \le \frac{1}{2} + \mathsf{negl}(n). $
  - *注:核心断言可以理解为根据函数的输出最难推断的关于输入的一个比特信息,任意敌手算法与随机猜测相比几乎没有差异。*

9. 对于任意OWF的HCP [Goldreich and Levin]

  - 定理:$f$是一个OWF。那么,存在一个OWF $g$ 并与 $g$ 伴随着一个HCP $gl$。
  - 问题: $\mathsf{gl}(x) = \bigoplus^{n}_{i=1} x_i$ 是任意OWF的HCP吗? 答案是否定的,例如一个单向函数输出的最后一个比特就是输入按位异或的结果;
  - 证明:$g(x,r) \overset{\text{def}}{=} (f(x), r)$, for $|x| = |r|$, 并定义 $ \mathsf{gl}(x,r) \overset{\text{def}}{=} \bigoplus^{n}_{i=1} x_i \cdot r_i $。 其中,$r$ 是一个随机串。
  - *说明:$\mathsf{gl}$就是从$x$中随机选择若干比特异或结果作为核心断言。即便敌手根据输出推断出$x$中若干比特的信息,但仍不能推断出(由$r$来)随机挑选的任意若干比特信息(核心断言),否则意味着敌手可以求出整个$x$。*

10. 从OWP到PRG:Blum-Micali Generator

  - 定理:$f$ 是一个OWP并且 $\mathsf{hc}$ 是一个 $f$ 的 HCP 。那么,$G(s) \overset{\text{def}}{=} (f(s), \mathsf{hc}(s))$ 构造了一个 PRG 带有扩展因子 $\ell(n) = n+1$,并且 $\forall$ 多项式 $p(n) > n$, $\exists$ 一个 PRG 带有扩展因子 $\ell(n) = p(n)$。
  - 定理成立的理由有两点:
    - 因为$f$为排列(这很重要,不能是非排列的函数),那么当$s$随机生成时,$f(s)$也是均匀随机的,$G(s)$的头部也就是随机的;
    - 根据$f(s)$难以推断核心断言$\mathsf{hc}(s)$,这正是伪随机生成器的伪随机性的判断依据:下一比特不可预测性。

11. 从PRG到PRF [Goldreich, Goldwasser, Micali]

   - 定理:如果存在一个PRG带有扩展因子$\ell(n) = 2n$,那么存在一个PRF。
   - $F_k(x_1x_2\cdots x_n) = G_{x_n}(\cdots(G_{x_2}(G_{x_1}(k)))\cdots), G(s)=(G_0(s),G_1(s)).$
   - 以密钥$k$为PRG的种子生成随机串,并将该随机串对半分为两个子串$G_0(s),G_1(s)$;再以每个子串作为种子分别生成两个新的子串;由此,构造一个以密钥(种子)为根的二叉树,每个叶子节点对应伪随机函数的一个输出,从输入到输出的映射就是从根到叶子的一条分支,根据输入每个比特值来选择分叉:0为左,1为右;
   - 例如,$F_k(011) = G_1(G_1(G_0(k)))$;以$k$为根,根据第一个比特选择左分支,接着选择右分支,右分支。
   - PRF随机性来自于PRG的随机性。

12. 从PRF到PRP [Lucy, Rackoff]

   - Feistel网络可以将一个$n$比特的PRF转变为一个$2n$比特的PRP,有以下定理
   - 定理:一个3轮的Feistel网络可将一个PRF转变为一个PRP。
   - 定理:一个4轮的Feistel网络可将一个PRP转变为一个strong PRP。
   - 说明:
     - 首先,Feistel网络本身特性是排列,因此证明上述定理成立的关键在于,证明伪随机性;伪随机性来自与每轮的mangler函数是PRF,其输出是一个独立的随机值。
     - 对于为什么至少需要3轮?首先可以观察到如果只有1轮,则不是伪随机的,因为$R_0$被直接输出为$L_1$;如果只有2轮,也不是随机的,因为只改变$L_0$来翻转1个比特,那么$R_2$也只翻转1个比特。当3轮时,上述两个情况不会发生,并且输出结果$L_3, R_3$都是经过了PRF结果得到的。

13. 必要的假设

   - 前面的理论说明OWP的存在是安全加密方案的充分条件,同时我们还可以证明OWP的存在也是安全加密方案的必要条件。
   - 定理:假设存在OWP,那么存在PRG,PRF,PRP和CCA安全私钥加密方案。
        - 如何构造CCA安全的加密方案将在后面学习。
   - 命题:如果存在窃听者不可区分私钥加密方案,那么存在一个OWF。
   - 证明:$f(k,m,r) \overset{\text{def}}{=} (\mathsf{Enc}_k(m,r),m)$,其中 $|k|=n, |m|=2n, |r|=\ell(n)$。
        - 从破解加密方案问题$\mathcal{A}'$规约到单向函数求逆问题$\mathcal{A}$。规约的关键之一在于将挑战密文和一个明文 $c\|m_0$ 作为$\mathcal{A}$求逆的输入。当求拟成功时,$\mathcal{A}'$输出0;否则,输出1。当$m_0$被加密,则破解加密方案意味着可求逆;当$m_1$被加密,则破解加密方案意味着没有成功求逆,概率为$1-1/2^n$。

14. 总结

   - OWF意味着安全私钥加密方案,安全私钥加密方案意味着OWF。



================================================
FILE: notes-Chinese/6 消息认证码与抗碰撞哈希函数.md
================================================
# 6 消息认证码与抗碰撞哈希函数

1. 本节学习用于保护信息的完整性和真实性的消息认证码(MAC)和抗碰撞的哈希函数(CRHF)。

2. 目录:MAC、构建安全MAC、CBC-MAC、CRHF、HMAC、信息论上MAC。

3. 完整性与真实性(**Integrity and Authentication**)
   - 敌手篡改传输中的密文(或明文)是对完整性的攻击;敌手伪装成Alice发送密文(或明文)是对真实性(认证)的攻击;
   - 上述两种攻击可以归结为对真实性(认证)的攻击,消息是由敌手构造并发出的;
   - 注意,这里的真实性是指消息的来源是来自接受者所预期的发送者,不是指内容的真假!
   
4. MAC的词法(**Message Authentication Code**)
   - 密钥 $k$, 标签(tag) $t$, 一个比特 $b$ 为有效的 (valid}) ,如果 $b=1$; 或 无效的 ​(invalid}​) ,如果 $b=0$.
   - 密钥生成 **Key-generation** 算法 $k \gets \mathsf{Gen}(1^n), |k| \ge n$.
   - 标签生成 **Tag-generation** 算法 $t \gets \mathsf{Mac}_k(m)$.
   -  验证 **Verification** 算法 $b:= \mathsf{Vrfy}_k(m,t)$.
   -  消息认证码  **Message authentication code**: $\Pi = (\mathsf{Gen}, \mathsf{Mac}, \mathsf{Vrfy})$.
   -  基本正确性需求 **Basic correctness requirement **: $\mathsf{Vrfy}_k(m,\mathsf{Mac}_k(m)) = 1$.
   - 注:不同于加密方案,MAC并不需要从标签得到密文。
   
5. MAC安全
   - 直觉上,没有敌手能够伪造一个**从未被发送过的新消息**的有效标签。这里“新消息”是为了排除“重放攻击”。
   - 重放攻击(Replay attack):敌手记录并发送之前的消息和标签,从而发送了一个伪造的消息并带有有效的标签;为了避免重放攻击,可以通过两种非密码学的方法。
     - 序列号:接收方需要记录之前的序列号,从而发现序列号较小(或曾经接收过的)的旧消息;
     - 时间戳:双方维护时钟同步,从而发现晚与当前时钟的旧消息;
     - 这两种方法都不依赖于密码学,因此,防御重放攻击不需要在密码学的范畴内考虑。
   - 存在性不可伪造(**Existential unforgeability**):不能伪造任何消息的标签,一个都不能伪造。
     - 存在性伪造 **Existential forgery**: 至少伪造一个消息的标签。
     - 选择性伪造 **Selective forgery**: 实施攻击前选择一个消息,并伪造该消息的标签。
     - 全域性伪造 **Universal forgery**: 伪造任意给定的消息的标签。
     - 最强的安全目标是阻止最弱的敌手造成的后果。
   - 适应性选择消息攻击(**Adaptive chosen-message attack (CMA)**):敌手在攻击过程中始终具有获得任意消息的有效标签的能力,即访问标签生成预言机;
   
6. 定义MAC安全
   - 消息认证实验 $\mathsf{Macforge}_{\mathcal{A},\Pi }(n)$ 在挑战者和敌手之间:
     1. 挑战者生成密钥 $k \gets \mathsf{Gen}(1^n)$.
     2. 敌手 $\mathcal{A}$ 具有访问标签生成算法$\mathsf{Mac}_k(\cdot)$的预言机的能力,并输出 $(m,t)$. 对预言机查询的消息集合为 $\mathcal{Q}$ 。
     3. $\mathsf{Macforge}_{\mathcal{A},\Pi }(n)=1 \iff$ $\mathsf{Vrfy}_k(m,t)=1$ $\land$ $m \notin \mathcal{Q}$.  敌手成功,如果输出的消息和标签通过了验证,并且输出的消息是从未向预言机查询过的新消息。
   - 定义:一个 MAC $\Pi$ 是在适应性CMA下的存在性不可伪造 (**existentially unforgeable under an adaptive CMA**),如果 $\forall$ PPT $\mathcal{A}$, $\exists$ $\mathsf{negl}$ 使得: $ \Pr [\mathsf{Macforge}_{\mathcal{A},\Pi }(n)=1] \le \mathsf{negl}(n). $
   
7. 真实例子

   - WEP 802.11 MAC中的漏洞有两点
   - 一是存在不同消息的CRC32可能是一样的情况,而且这种情况很容易给出。那么,敌手可以查询一个消息$m$并得到对应的标签$t$;然后,输出另一个与所查询消息$m$具有相同CRC32值的新消息$m'$,以及查到的标签$t$。
   - 二是敌手可以查询一个消息$m'$并获得标签$(r, t')$,由此计算得到$F(k,r) = t'\oplus \mathsf{CRC32}(m')$;输出一个新消息$m$以及标签$t = (r, F(k,r)\oplus\mathsf{CRC32}(m))$。
   - 上述漏洞展现了攻击MAC的两种常用手段:一是找到两个消息得到相同的中间结果,从而以一个消息的标签作为另一个新消息的标签;二是利用对一个/多个消息的标签来获得构造标签所需的信息,从而构造一个新消息的标签。
   
8. 例题

   - 如果认为是安全的,则要用反证法证明,若新方案不安全,则原方案也不安全;
   - 如果认为是不安全的,则给出一个新消息和对应的标签;

9. MAC应用

   - Web cookie:Web服务器在发给浏览器的cookie中包含自己生成的MAC标签,来阻止攻击者伪造其他用户的cookie;
   - TCP SYN cookie:在TCP三次握手中,服务器在其发给客户端的初始序列号中包含一个服务器生成的MAC标签,来避免保留握手状态,从而防御SYN Flooding DDoS攻击;
   - 临时一次口令:用户发送给服务器的临时登录口令为一个MAC标签$p=\mathsf{Mac}_k(T)$,其中$k$为原始口令,$T$为当前时间(按半分钟取整);敌手窃听了之前的临时口令也无法伪造未来的临时口令;

10. 构造安全MAC

	- 基于PRF构造安全MAC
		-  $F$ 是 PRF. $|m| = n$.
    	- $\mathsf{Gen}(1^n)$: $k \gets \{0,1\}^n$ .
    	- $\mathsf{Mac}_k(m)$: $t := F_k(m)$.
    	- $\mathsf{Vrfy}_k(m,t)$: $1 \iff t \overset{?}{=} F_k(m)$.
    - 定理:如果 $F$ 是一个PRF,那么上述构造是安全的固定长度 MAC。
    - 引理:如果 $F$ 是一个 PRF,那么 $F^t_k(m) = F_k(m)[1,\dots,t]$ 也是一个PRF。
      - 注:这个引理说明部分输出仍保留伪随机性。引理成立的原因在于,如果根据更短的输出可以区分出伪随机函数,那么根据原长度输出也可以区分出伪随机函数了。
11. 证明基于PRF的安全MAC

    - 证明思路是从PRF的区分器算法$D$规约到伪造标签的敌手算法$\mathcal{A}$。$D$作为$\mathcal{A}$的挑战者,用$D$要区分的预言机作为$\mathcal{A}$的标签生成预言机;当$\mathcal{A}$伪造标签成功时,$D$输出1。

12. 证明基于PRF的安全MAC(续)

    - 如果是真随机 $f$ 被使用 $t=f(m)$ 是均匀随机的.

      $ \Pr[D^{f(\cdot)}(1^n)=1] = \Pr[\mathsf{Macforge}_{\mathcal{A},\tilde{\Pi}}(n) = 1] \le 2^{-n}.$

    - 如果 $F_k$ 被使用,那么就是在执行实验 $\mathsf{Macforge}_{\mathcal{A},\Pi}(n)$. 

      $ \Pr[D^{F_k(\cdot)}(1^n)=1] = \Pr[\mathsf{Macforge}_{\mathcal{A},\Pi}(n) = 1] = \varepsilon(n).$

    - 根据PRF的定义有,$ \left| \Pr[D^{F_k(\cdot)}(1^n)=1] - \Pr[D^{f(\cdot)}(1^n)=1] \right| \ge \varepsilon(n) - 2^{-n}. $

13. 扩展到变长消息

    - 对于变长消息,下面的建议是安全的吗?
    - 建议1:将所有块异或后,对结果进行认证:$t := \mathsf{Mac}_k'(\oplus_i m_i)$;
    - 建议2:对每个块分别认证,$t_i := \mathsf{Mac}_k'(m_i)$;
    - 建议3:对每个块连带一个序列号一起认证, $t_i := \mathsf{Mac}_k'(i\| m_i)$.

14. 构造固定长度的CBC-MAC

    - 为了构造用于变长消息的MAC,先学习固定长度的CBC-MAC,其与CBC结构类似,做了两处改变:
    - 改动1:将初始向量IV改为0;如果不这样改动,则敌手查询 $m_1$ 并获得 $(IV, t_1)$;然后,输出 $m_1' = IV' \oplus IV \oplus m_{1}$ 并且 $t' = (IV',t_1)$,一个有效的标签。
    - 改动2:标签只包括最后一个块的输出;如果不这样改动,则敌手查询 $m_i$ 并得到 $t_i$;然后,输出 $m_i' = t_{i-1}' \oplus t_{i-1} \oplus m_{i}$ 以及 $t_{i}' = t_i$,一个有效的标签。

15. 构造固定长度的CBC-MAC(续)

    - 定理:如果$F$是一个PRF,那么上面的构造就是一个安全的固定长度MAC。
    - 这个构造不能用于变长消息,因为对于一个块的消息$m$和标签$t$,敌手可以在其后添加一个块$m\oplus t$并且输出标签$t$。

16. 安全变长MAC

    - 有三种方法可以将CBC-MAC改造为用于变长消息的MAC,都可以防御上面在结尾添加新块的攻击。
    - 输入长度密钥分离:$k_{\ell} := F_k(\ell)$, 用 $k_{\ell}$ 作为 CBC-MAC 的密钥。不同长度下采用不同密钥,追加新块后长度变化,之前的标签无法利用。
    - 在开头添加长度:在CBC-MAC的明文$m$前添加一个长度块$|m|$。不同长度下消息有不同的初始块,追加新块后长度变化,之前的标签无法利用。
    - 加密末块输出(ECBC-MAC):采用两个密钥$k_1, k_2$。用$k_1$和CBC-MAC计算出 $t$,然后输出 $\hat{t} := F_{k_2}(t)$。输出结果被加密,之前的标签无法利用。

17. MAC填充(**Padding**)

    - 与加密类似,为了将消息长度与块长度对齐,MAC中也需要在消息中填充。为了安全性,需要保证填充是可逆的,即不同的消息在填充后也应该不同!
    - $m_0\neq m_1 \Rightarrow \mathsf{pad}(m_0) \neq \mathsf{pad}(m_1).$
    - ISO的填充标准:用“100...00”填充,并按需填充哑块。
    - 如果不填充哑块,则会导致什么?
    - CMAC(Cipher-based MAC):不填充哑块,不加密最后一块的输出,密钥包括三个 $k, k_1, k_2$
      - $k$用于CBC-MAC;
      - $k_1$ 和 $k_2$ 与最后一块消息异或来阻止利用最后一块输出;
      - 用$k_1$ 和 $k_2$ 来区分是否添加了哑块。

18. 定义哈希函数(**Hash Function**)

    - 一个哈希函数 (压缩函数) 是一对PPT算法 $(\mathsf{Gen}, H)$ 满足以下条件:
      -  一个密钥 $s \gets \mathsf{Gen}(1^n)$, $s$ 不保密.
      -  $H^s(x) \in \{0,1\}^{\ell(n)}$, 其中 $x \in \{0,1\}^*$ 且 $\ell$ 为多项式。
    - 若 $H^s$ 只在 $x \in \{0,1\}^{\ell'(n)}$ 上定义并且 $\ell'(n) > \ell(n)$,那么 $(\mathsf{Gen}, H)$ 是固定长度的哈希函数。
    - 上面的定义说明,哈希函数将长消息转变为短消息。

19. 定义抗碰撞(**Collision Resistance**)

    - 碰撞(**Collision**):$x \neq x'$ 并且 $H(x) = H(x')$。

    - 抗碰撞(**Collision Resistance**):对于任意PPT算法,找到碰撞是不可能的。

    - 碰撞发现实验$\mathsf{Hashcoll}_{\mathcal{A},\Pi}(n)$:

      -  $s \gets \mathsf{Gen}(1^n)$.

        - 敌手 $\mathcal{A}$ 输入 $s$ ,输出 $x, x'$. *注:敌手有$s$,意味着可以访问哈希函数*

      -  $\mathsf{Hashcoll}_{\mathcal{A},\Pi}(n) =1 \iff x\ne x' \land H^s(x) = H^s(x')$.

      - 哈希函数 $\Pi$ ($\mathsf{Gen}$, $H^s$) 是抗碰撞的,如果$\forall$  ppt $\mathcal{A}$, $\exists\;\mathsf{negl}$ 使得

        $ \Pr[\mathsf{Hashcoll}_{\mathcal{A},\Pi}(n)=1] \le \mathsf{negl}(n).$

20. 哈希函数安全的更弱的概念

    - 抗碰撞(Collision resistance): 难以找到 $(x, x'), x' \ne x$ 使得 $H(x) = H(x')$.
    - 抗二次原像 (Second pre-image resistance): 给定 $s$ 和 $x$, 难以发现 $x' \ne x$ 使得 $H^s(x') = H^s(x)$.
    - 抗原像 (Pre-image resistance): 给定 $s$ 和 $y = H^s(x)$, 难以发现 $x'$ 使得 $H^s(x')=y$.
    - 攻击越难,反过来可以防范这种攻击的安全性就越弱。

21. 关于CRHF的问题

    - 如果认为不是,那么请给出一个碰撞;
    - 如果认为是,则用反证法证明找到了$H'$的碰撞意味着$H$的碰撞。

22. 哈希函数的应用

    - 文件指纹和去重(Fingerprinting 和 Deduplication):识别一个文件,用于病毒指纹识别,去重复,P2P文件共享;
    - 默克尔树 (Merkle Tree):构造多个文件或一个文件多个部分的指纹,从而定位有问题的文件或者文件中的部分;
    - 口令哈希(Password Hashing):$(salt, H(salt, pw))$,缓解明文口令泄漏风险;
    - 密钥派生(Key Derivation):从一个高熵(但不必均匀随机)的共享秘密中派生一个密钥;
    - 承诺方案(Commitment Scheme):将一个承诺与一份信息绑定,隐藏承诺的信息;例如,互联网上掷硬币。

23. 生日问题

    - 生日问题:“如果一群人中有两个人的生日是同一天的概率有1/2,这群人数有多少?”。答案是23。这与我们平时的认知差异,也被称作“生日悖论”。具体计算见教材附件。
    - 这个问题意味着哈希函数的输出需要足够长,否则敌手可能通过蛮力枚举来发现碰撞。
    - 在现实攻击中,找到有意义的消息的碰撞对于攻击者来说更有价值。这对攻击者来说并不是难题,可以很容易的构造足够数量的、有意义的消息来实施攻击。对消息中一个单词的替换,所构造明文的数量翻番。

24. MD变换(**Merkle-Damgård Transform**)

    - 从定长哈希函数$(\mathsf{Gen}, h)$ ($2\ell$ bits $\to \ell$ bits, $\ell = \ell(n)$)构造变长哈希函数 CRHF $(\mathsf{Gen}, H)$  :
      -  $\mathsf{Gen}$: 不变
      -  $H$: 密钥 $s$ 与串 $x \in \{0,1\}^*$, $L=|x|< 2^{\ell}$:
        -  $B := \lceil \frac{L}{\ell} \rceil$ (块数)。 用0填充。 $\ell$-位的块 $x_1,\dotsc,x_B$。最后一块是长度 $x_{B+1} := L$, $L$ 以 $\ell$ 位编码,这是必要的,因为只用0填充会导致不同消息的输入是一样的。
        -  $z_0 := IV = 0^\ell$。 对于 $i=1,\dotsc,B+1$, 计算 $z_i := h^s(z_{i-1}\| x_i)$。

25. MD变换的安全性

    - 定理:如果$h$是定长CRHF,那么$H$也是CRHF。
    - 证明:思路是$H$上的碰撞意味着$h$上的碰撞,而$h$是不会被找到碰撞的。两个消息 $x \ne x'$ ,长度分别为 $L$ 和 $L'$ ,块数分别为$B$ 和 $B'$,使得 $H^s(x) = H^s(x')$。 有两种情况:
      - $L \ne L'$: $z_B\| L \ne z_{B'}\| L'$;长度不同,意味着最后一个哈希函数$h$的输入不同,但输出相同,发现碰撞。
      - $L = L'$: $z_{i^*-1}\| x_{i^*} \ne z_{i^*-1}'\| x_{i^*}'$;长度相同,意味着中间某一块的输入不同,但输出相同,发现碰撞。
      - 因此,必定有 $x \neq x'$ 使得 $h^s(x) = h^s(x')$。
    - 作业中有关于MD变换的变体的安全性分析问题。

26. 从分组密码构造CRHF

    - 可以从块密码来构造CRHF,例如Davies-Meyer方法 (SHA-1/2, MD5)  $h_{i} = F_{m_{i}}(h_{i-1}) \oplus h_{i-1}$,或者 Miyaguchi-Preneel 方法 (Whirlpool) $h_{i} = F_{h_{i-1}}(m_{i}) \oplus h_{i-1} \oplus m$。
    - 定理:如果$F$是一个理想的加密方案,那么Davies-Meyer构造得到一个CRHF。*注:理想的加密方案参考后面要学习的随机预言机模型。目前,没有找到$F$是强伪随机排列下该方法是CRHF的证明。*
    - 对于这个定理不做严格证明,而是回答两个问题:
      - 如果 $h_{i} = F_{m_{i}}(h_{i-1})$ ,不与 $h_{i-1}$ 异或,会如何?敌手尝试以相同的$h_i$和不同的$m_i$对$F$求逆。
      - 如果 $F$ 不是理想的,而是 $\exists x, F_k(x)=x$,会如何?敌手输入不同$m_i$,但都得到0;

27. SHA-1和MD5

    - 曾将广泛采用的哈希函数SHA1和MD5都已经被破解。对于128位的MD5,找到碰撞需要$2^{20.96}$;对于160位的SHA1,找到碰撞需要$2^{51}$。

28. Hash-and-MAC

    - 有了CRHF,一个自然的想法是:先将任意长度消息哈希,然后通过PRF对哈希值做MAC,实现任意长度消息MAC。$F_k(H(m))$
    - 这个方案的安全性分两种情况分析:当不同消息得到相同哈希值时,这意味着碰撞发生;否则,意味着MAC标签被伪造。

29. NMAC

    - 使用CRHF(MD变换)来构造MAC,而不需要用PRF
    - 之所以需要开头的密钥,是为了在哈希函数为弱抗碰撞性时也保障安全;如果哈希函数是CRHF,则不需要开头的密钥
    - 缺点:需要修改哈希函数(MD变换中初始向量)

30. HMAC(基于哈希的MAC)

    - 以MD变换为基础构造一个安全的MAC。在开头和结尾以两个不同密钥作为哈希函数输入。
    - 不需要修改哈希函数。
    - $\mathsf{Gen}(1^n)$: 输出 $(s, k)$. $s \gets \widetilde{\mathsf{Gen}}, k \gets \{0,1\}^n$ u.a.r;
    - $\mathsf{Mac}_{s,k}(m)$: $t := H_{IV}^s\Big((k \oplus \mathsf{opad}) \| H_{IV}^s\big((k \oplus \mathsf{ipad}) \| m\big)\Big)$
    - $\mathsf{Vrfy}_{s,k}(m,t)$: $1 \iff t \overset{?}{=} \mathsf{Mac}_{s,k}(m)$

31. HMAC安全性

    - 定理:$G(k) \overset{\text{def}}{=} h^s(IV\| (k\oplus \mathsf{opad})) \| h^s(IV\| (k\oplus \mathsf{ipad})) = k_1\| k_2$ 。其中,$h$是CRHF。如果$G$是PRG,那么HMAC是安全的。
    - 在HMAC之前,其他不安全的方案包括:
    - $H^s(k\| x)$ 存在长度扩展攻击弱点。 在获得$H^s(k\| x)$和消息长度后,敌手能够获得新消息 $x \| x'$ 的有效标签 $H^s(k\| x \| x')$ 。因为$H^s(k\| x)$的输出标签$t$和$x'$作为哈希函数的输入直接得到输出。
    - $H^s(x\| k)$: 在一个弱哈希函数上的碰撞会导致MAC上碰撞。回顾NMAC中需要开头的密钥来支持弱抗碰撞的情况。
    - $H^s(k\| x\| k)$: 也存在一些已知的弱点,即使使用两个不同的密钥。
    - $H^s(k \| H^s(k \| x))$:这是NMAC和HMAC的情况

32. HMAC结语

    - HMAC是基于NMAC的改进,是工业标准(RFC2104),HMAC比CBC-MAC更快;

    - 验证计时攻击:

      - Keyczar密码学库(Python):

      - `def Verify(key, msg, sig_bytes): `

        $\qquad$ `return HMAC(key, msg) == sig_bytes`

      - 存在问题是上述比较是按字节匹配,通过观察函数返回时间可以判断相同字节的数量,从而按字节猜测标签内容。

      - 在Xbox 360中,相邻字节上被验证拒绝的时间差有2.2毫秒.

    - 不要自己实现密码学!

33. 信息论上MAC安全定义

    - 不可能达到“完美的、不可伪造的”MAC,因为算力无限制的敌手可以至少以$1/2^{|t|}$ 的概率输出一个有效的标签。为此,对敌手查询MAC预言机的次数需要加以限制,下面分析只允许敌手查询一次MAC预言机的情况。
    - 一次消息认证实验 $\mathsf{Macforge}^{\mathsf{1-time}}_{\mathcal{A},\Pi }$: 敌手查询一次MAC预言机后输出消息和标签,
      - $k \gets \mathsf{Gen}$.
      - $\mathcal{A}$ 输出一个消息 $m'$并且获得一个标签 $t' \gets \mathsf{Mac}_k(m')$, 然后输出 $(m,t)$.
      - $\mathsf{Macforge}^{\mathsf{1-time}}_{\mathcal{A},\Pi }=1 \iff$ $\mathsf{Vrfy}_k(m,t)=1$ $\land$ $m \neq m'$. 
    - 定义:一个MAC $\Pi$ 是一次$\varepsilon$-安全的(one-time $\varepsilon$-secure),如果  $\forall$ ppt $\mathcal{A}$: $\Pr [\mathsf{Macforge}^{\mathsf{1-time}}_{\mathcal{A},\Pi}=1] \le \varepsilon.$
    - 这里$\varepsilon$应该为$1/2^{|t|}$,才能达到之前的信息论安全。信息论安全的MAC在允许敌手查询MAC预言机若干次之后,成功伪造MAC的概率应该不大于$1/2^{|t|}$。

34. 理解信息论MAC安全

    - 假设敌手算法是确定性的,其最合理的步骤如下:
      - (1)选择的$m'$是固定的,查询得到$t'$;
      - (2)根据$m'$和$t'$确定$k$的所有可能集合$\mathcal{K}(t')$,从中选择一个$k^*$;
      - (3)选择输出$m$是固定的,根据$k^*$计算$t$并输出。
    - 问题:$\mathcal{K}(t')$太大或太小会如何?
    - 设想如果根据第一次消息和标签能够唯一确定密钥$k$,那么敌手一定可以成功伪造;反之,如果不能唯一确定密钥,并且密钥可能的范围$\mathcal{K}(t')$充分大,那么敌手就难以成功伪造。从另一个角度,需要第一次查询获得的一个对消息和标签与敌手伪造另一个新消息的标签这两个事件之间是充分独立的。密钥空间太大也不安全,因为令$(m, t)$是有效标签密钥集合也更大,其概率也增大。

35. 信息论上MAC的构造

    - 一个函数 $h$: $\mathcal{K} \times \mathcal{M} \to \mathcal{T}$ 是一个强全域函数(**Strongly Universal Function (SUF)**),如果对于所有不同的 $m, m' \in \mathcal{M}$ 以及所有 $t, t' \in \mathcal{T}$, 以下成立: $ \Pr [h_k(m) = t \land h_k(m') = t'] = 1 / |\mathcal{T}|^2 $,其中概率来自均匀选择的 $k \in \mathcal{K}$.
    - 为了实现一次$1/2^{|t|}$-安全的MAC,需要一个新的数学对象,不同输入会独立产生不同的输出,输入间任何差异都会导致输出之间是完全独立的。将函数的这种性质称为:“成对独立的,**pairwise-independent**” 或者 “成对不可预测,**pairwise-unpredictable**”。
    - SUF是具有上面性质的函数,下一页证明
    - 信息论安全MAC构造:
      - 令 $h$: $\mathcal{K} \times \mathcal{M} \to \mathcal{T}$ 为一个SUF.
      - $\mathsf{Gen}$: $k \gets \{0,1\}^n$ u.a.r.
      - $\mathsf{Mac}_k(m)$: $t := h_k(m)$.
      - $\mathsf{Vrfy}_k(m,t)$: $1 \iff t \overset{?}{=} h_k(m)$. (如果 $m \notin \mathcal{M}$,那么输出 0.)

36. 构造一个SUF

    - 定理:对于任意质数 $P$,函数 $h$ 是一个SUF: $ h_{a,b}(m) \overset{\mathsf{def}}{=} [ a \cdot m + b \mod p] $
    - 证明:$h_{a,b}(m) = t$ 且 $h_{a,b}(m') = t'$,只有当 $a \cdot m + b = t \mod p$ 且 $a \cdot m' + b = t' \mod p$. 我们有 $a = [(t-t') \cdot (m - m')^{-1} \mod p]$ 且  $b = [t - a \cdot m \mod p]$,这意味着存在一个唯一的密钥 $(a, b)$。由于存在 $|\mathcal{T}|^2$ 个密钥,$ \Pr [h_k(m) = t \land h_k(m') = t'] = \frac{1}{|\mathcal{T}|^2}$。

37. 来自SUF的MAC的安全性

    - 定理:如果 $h$ 是一个 SUF,构造是一个 $1/|\mathcal{T}|-$安全MAC.
    - 证明:假设敌手算法是确定性的,不失一般性可以固定$m'$并遍历所有可能的$t'$,敌手以$(m', t')$作为输入并输出$(m, t)$。根据SUF的定义,可以得到敌手成功的概率为$1/|\mathcal{T}|$。

38. 信息论MAC的局限性

    - 任意 $\ell$次 $2^{-n}$-安全 MAC 需要密钥长度至少为 $(\ell +1) \cdot n$. 
    - 定理:令 $\Pi$ 为一次 $2^{-n}$-安全 MAC,其中所有密钥长度相同。那么,密钥必须具有$2n$长度。
    - 证明:直觉上,每对消息和标签成立需要$2^n$个密钥,才能保证 $2^{-n}$-安全。一共2对,需要$2^{2n}$。
    - 令 $\mathcal{K}(t') \overset{\mathsf{def}}{=} \{ k | \mathsf{Vrfy}_k(m', t') = 1\}$,即所有由所查询消息得到标签的密钥集合。对于任意 $t'$, $|\mathcal{K}(t')| \leq 2^{-n} \cdot |\mathcal{K}|$。 否则,敌手$\mathcal{A}$从全体密钥集合中随机挑选一个密钥得到 $(m, t)$ 是一个有效标签的概率至少为 $|\mathcal{K}(t')|/|\mathcal{K}|> 2^{-n}$,这与安全要求矛盾。 $\mathcal{A}$有无限算力可以根据从第一次查询中得到对应的密钥集合$\mathcal{K}(t')$,从中选择一个密钥$k^*$,并输出一个新消息$m$的有效标签的概率是至少 $\frac{1}{|\mathcal{K}(t')|}$。固定$m'$遍历所有标签$t'$计算敌手成功概率为: $\sum_{t'} \Pr [\mathsf{Mac}_k(m') = t'] \cdot \frac{1}{|\mathcal{K}(t')|} \geq \sum_{t'} \Pr [\mathsf{Mac}_k(m') = t'] \cdot \frac{2^n}{|\mathcal{K}|} = \frac{2^n}{|\mathcal{K}|} $ 。由于概率至多 $2^{-n}$, $|\mathcal{K}| \geq 2^{2n}$。由于所有密钥具有相同长度,每个密钥的长度至少是 $2n$。

39. 总结

    - 认证意味着存在不可伪造
    - 用PRF来实现安全MAC
    - 用带密钥的CRHF来实现安全MAC
    - 信息论MAC安全需要非常、非常长的密钥





================================================
FILE: notes-Chinese/7 CCA安全与认证加密.md
================================================
# 7 CCA安全与认证加密

1. 本节学习用于抵抗CCA攻击的加密方案以及同时保证通信机密性和真实性的认证加密方案。

2. 目录:CCA安全加密,认证加密,确定性加密,密钥派生函数。

3. 回顾CCA不可区分实验

   - CCA不可区分实验 $\mathsf{PrivK}^{\mathsf{cca}}_{\mathcal{A},\Pi}(n)$:
   	1. 挑战者生成密钥 $k \gets \mathsf{Gen}(1^n)$;(为了下一步的预言机)
   	2. $\mathcal{A}$ 被给予输入 $1^n$ 和对加密函数 $\mathsf{Enc}_k(\cdot)$和解密函数$\mathsf{Dec}_k(\cdot)$的**预言机访问(oracle access)** $\mathcal{A}^{\mathsf{Enc}_k(\cdot)}$ 和 $\mathcal{A}^{\mathsf{Dec}_k(\cdot)}$,输出相同长度 $m_0, m_1$ ;
   	3. 挑战者生成随机比特 $b \gets \{0,1\}$,将挑战密文 $c \gets \mathsf{Enc}_k(m_b)$ 发送给 $\mathcal{A}$;
   	4. $\mathcal{A}$ 继续对除了挑战密文$c$之外的预言机的访问,输出$b'$;如果$b' = b$,则$\mathcal{A}$成功$\mathsf{PrivK}^{\mathsf{cca}}_{\mathcal{A},\Pi}=1$,否则 0。
   - 定义:一个加密方案是CCA安全的,如果实验成功的概率与1/2之间的差异是可忽略的。

4. 消息传递方案
   - 我们先不直接处理CCA安全,而是研究一个比CCA更安全的通信场景,其中引入了之前学习的真实性要求;
   - CCA安全与消息的真实性有关,下面学习同时保护消息机密性和真实性的消息传递方案。
   - 密钥生成(**Key-generation**) 算法输出 $k \gets \mathsf{Gen'}(1^n)$. $k = (k_1,k_2)$. $k_1 \gets \mathsf{Gen}_E(1^n)$, $k_2 \gets \mathsf{Gen}_M(1^n)$.
   - 消息传递(**Message transmission** )算法由 $\mathsf{Enc}_{k_1}(\cdot)$ 和 $\mathsf{Mac}_{k_2}(\cdot)$ 生成,输出 $c \gets \mathsf{EncMac'}_{k_1,k_2}(m)$.
   - 解密(**Decryption**)算法由 $\mathsf{Dec}_{k_1}(\cdot)$ 和 $\mathsf{Vrfy}_{k_2}(\cdot)$ 生成,输出 $m \gets \mathsf{Dec}'_{k_1,k_2}(c)$ 或 $\bot$.
   - 正确性需求: $\mathsf{Dec}'_{k_1,k_2}(\mathsf{EncMac}'_{k_1,k_2}(m)) = m$.
   - 注:在消息传递方案中,消息被加密并且被MAC。在解密算法中,当密文没有通过真实性验证时,输出空(可以理解为“报错”);这意味着未认证的密文无法解密。

5. 定义安全消息传递
   - 先定义保护真实性的认证通信,然后定义同时保护机密性和真实性的认证加密。
   - 安全消息传递实验(**secure message transmission**) $\mathsf{Auth}_{\mathcal{A},\Pi'}(n)$:
     - $k = (k_1,k_2) \gets \mathsf{Gen}'(1^n)$.
     - $\mathcal{A}$ 输入 $1^n$ 和对 $\mathsf{EncMac'}_k$的预言机访问,并输出 $c \gets \mathsf{EncMac'}_{k}(m)$.
     - $m := \mathsf{Dec}'_k(c)$. $\mathsf{Auth}_{\mathcal{A},\Pi'}(n) = 1 \iff m \ne \bot \land\; m \notin \mathcal{Q}$.
   - 定义:$\Pi'$ 实现认证通信( **authenticated communication**),如果 $\forall$ ppt $\mathcal{A}$, $\exists\; \mathsf{negl}$ 使得,$ \Pr[\mathsf{Auth}_{\mathcal{A},\Pi'}(n) = 1] \le \mathsf{negl}(n). $
   - 定义:$\Pi'$ 是安全的认证加密(**secure Authenticated Encryption (A.E.)**), 如果其既是CCA安全的也是实现了认证通信。
   - 问题:CCA安全意味着A.E.吗?(作业)

6. 关于认证加密的例题
   - 如果认为是安全的,那么利用反证法证明;
   - 如果认为是不安全的,那么或者可以伪造消息,或者破解明文;

7. 加密和认证组合
   - 加密和认证如何组合来同时保护机密性和真实性?
   - 加密并认证(**Encrypt-and-authenticate**) (例如, SSH):$ c \gets \mathsf{Enc}_{k_1}(m),\; t \gets \mathsf{Mac}_{k_2}(m).$
   - 先认证后加密(**Authenticate-then-encrypt**) (例如, SSL):$ t \gets \mathsf{Mac}_{k_2}(m),\; c \gets \mathsf{Enc}_{k_1}(m\| t).$
   - 先加密后认证(**Encrypt-then-authenticate**) (例如, IPsec):$ c \gets \mathsf{Enc}_{k_1}(m),\; t \gets \mathsf{Mac}_{k_2}(c). $

8. 分析组合的安全性
   - 采用全或无(All-or-nothing)分析,即一种组合方案要么在全部情况下都是安全的,要么只要存在一个不安全的反例就被认为是不安全的;
   - 加密并认证: $\mathsf{Mac}'_k(m) = (m, \mathsf{Mac}_k(m))$. 
     - 这表明,认证可能泄漏消息。
   - 先认证后加密: 
     - 一个例子:
       - $\mathsf{Trans}: 0 \to 00; 1 \to 10/01$; 
       - $\mathsf{Enc}'$ 采用CTR模式; $c = \mathsf{Enc}'(\mathsf{Trans}(m\| \mathsf{Mac}(m)))$.
       - 将 $c$ 的前两个比特翻转并且验证密文是否有效。$10/01 \to 01/10 \to 1$, $00 \to 11 \to \bot$.
         - 明文为1时,不改变明文;明文为0时,解密无效
       - 如果可以有效解密,则意味着消息的第一比特是1,否则是0; 
       - 对于任何MAC,这都不是CCA安全的;
     - 这个例子表明,缺乏完整性保护时,敌手可解密,而密文是否有效也价值1个比特的信息。
   - 先加密后认证: 解密: 如果 $\mathsf{Vrfy}(\cdot) = 1$, 那么 $\mathsf{Dec}(\cdot)$; 否则,输出 $\bot$。下面来证明。

9. 构造AE/CCA安全的加密方案

   - 思想:令解密预言机没用。AE/CCA =CPA-then-MAC。
   - $\Pi_E = (\mathsf{Gen}_E, \mathsf{Enc}, \mathsf{Dec})$, $\Pi_M = (\mathsf{Gen}_M, \mathsf{Mac}, \mathsf{Vrfy})$. $\Pi'$:
     - $\mathsf{Gen}'(1^n)$: $k_1 \gets \mathsf{Gen}_E(1^n)$ and $k_2 \gets \mathsf{Gen}_M(1^n)$
     - $\mathsf{Enc}'_{k_1,k_2}(m)$: $c \gets \mathsf{Enc}_{k_1}(m)$, $t \gets \mathsf{Mac}_{k_2}(c)$ and output $\left< c,t \right>$
     - $\mathsf{Dec}'_{k_1,k_2}(\left< c,t \right>) = \mathsf{Dec}_{k_1}(c)\ \text{if}\ \mathsf{Vrfy}_{k_2}(c,t) \overset{?}{=} 1;\ \text{otherwise}\ \bot$
   - 加密时,先加密后对密文做认证;解密时,先验证,若未通过验证,则输出空,否则解密。

10. AE/CCA安全加密方案证明

    - 定理:如果 $\Pi_E$ 是CPA安全的私钥加密方案并且 $\Pi_M$ 是一个安全的MAC,那么构造 $\Pi'$ 是CCA安全的。

    - 证明:$\mathsf{VQ}$ (有效查询): $\mathcal{A}$ 向预言机$\mathsf{Dec}'$提交一个新查询并且 $\mathsf{Vrfy}=1$。*注:VQ表示敌手向预言机查询可经过验证并解密。*

    - $ \Pr[\mathsf{PrivK}^{\mathsf{cca}}_{\mathcal{A},\Pi'}(n)=1] \le \Pr[\mathsf{VQ}] + \Pr[\mathsf{PrivK}^{\mathsf{cca}}_{\mathcal{A},\Pi'}(n)=1 \land \overline{\mathsf{VQ}}] $

    - 我们需要证明以下:

      - $\Pr[\mathsf{VQ}]$ 是可忽略的;敌手无法利用解密预言机获得有效查询;

      - $\Pr[\mathsf{PrivK}^{\mathsf{cca}}_{\mathcal{A},\Pi'}(n)=1 \land \overline{\mathsf{VQ}}] \le \frac{1}{2} + \mathsf{negl}(n)$;在无法利用解密预言机时难以破解加密方案。

11. 证明敌手无法利用解密预言机获得有效查询

    - 思路:将 $\mathcal{A}_M$ (有预言机 $\mathsf{Mac}_{k_2}(\cdot)$攻击 $\Pi_M$ ) 规约到 $\mathcal{A}$。
    -  $\mathcal{A}_M$以 $\mathcal{A}$ 为子函数来运行。$\mathcal{A}$ 将产生$q(n)$个解密预言机查询,$\mathcal{A}_M$ 预先从中均匀随机选择一个编号 $i \gets \{1,\dotsc,q(n)\}$,并将该查询作为输出的伪造;
    - 当$\mathcal{A}$以$m$查询加密预言机时, $\mathcal{A}_M$ 产生加密密钥并以加密预言机的角色先计算密文$c$,然后用密文查询MAC预言机并将$\left<c, t\right>$返回给$\mathcal{A}$;
    - 当$\mathcal{A}$以$\left<c, t\right>$查询解密预言机时,如果这是第 $i$ 个查询,那么$\mathcal{A}_M$ 输出$\left<c, t\right>$并停止;否则,如果这是曾经在加密预言机查询过的,$\mathcal{A}_M$ 返回明文,否则,返回$\bot$(因为只要$\mathsf{VQ}$未发生,就应该返回$\bot$);
    - $\mathsf{Macforge}_{\mathcal{A}_M,\Pi_M }(n)=1$ 的条件是,只有当 $\mathsf{VQ}$ 发生并且 $\mathcal{A}_M$ 正确地猜测了 $i$ (概率为 $1/q(n)$)。
    - $ \Pr [\mathsf{Macforge}_{\mathcal{A}_M,\Pi_M }(n)=1] \ge \Pr[\mathsf{VQ}]/q(n).$

12. 证明在无法利用解密预言机时难以破解加密方案

    - 思路:将 $\mathcal{A}_E$ (以 $\mathsf{Enc}_{k_1}(\cdot)$ 预言机来攻击 $\Pi_E$ ) 规约到 $\mathcal{A}$。

    -  $\mathcal{A}_E$ 以 $\mathcal{A}$ 为子函数来运行。 $\mathcal{A}_E$ 扮演 $\mathcal{A}$ 的加密预言机和解密预言机方法与 $\mathcal{A}_M$ 的类似;

    - 实验 $\mathsf{PrivK}^{\mathsf{cpa}}_{\mathcal{A}_E,\Pi_E}$ 与实验 $\mathsf{PrivK}^{\mathsf{cca}}_{\mathcal{A},\Pi'}$ 的运行一样, $\mathcal{A}_E$ 输出与 $\mathcal{A}$ 一样的 $b'$ ;

    - $\Pr[\mathsf{PrivK}^{\mathsf{cpa}}_{\mathcal{A}_E,\Pi_E}(n)=1 \land \overline{\mathsf{VQ}}] = \Pr[\mathsf{PrivK}^{\mathsf{cca}}_{\mathcal{A},\Pi'}(n)=1 \land \overline{\mathsf{VQ}}]$;

       $ \Pr [\mathsf{PrivK}^{\mathsf{cpa}}_{\mathcal{A}_E,\Pi_E }(n)=1] \ge \Pr[\mathsf{PrivK}^{\mathsf{cca}}_{\mathcal{A},\Pi'}(n)=1 \land \overline{\mathsf{VQ}}] $。

13. 认证加密理论与实践

    - 定理:$\Pi_E$ 是CPA安全的并且 $\Pi_M$ 是一个带有唯一标签的安全MAC(强安全MAC),那么由先加密后认证得到的 $\Pi'$ 是安全的。*注:强安全MAC是指一个消息只有一个有效标签*
    - GCM (Galois/Counter Mode): 先CTR加密,然后做 Galois MAC. (RFC4106/4543/5647/5288 on IPsec/SSH/TLS)
    - EAX: 先CTR 加密,然后 CMAC(Cipher-based MAC)。
    - 定理:先认证后加密方法是安全的,如果 $\Pi_E$ 是CTR模式或者CBC模式。
    - CCM (Counter with CBC-MAC): 先 CBC-MAC 后 CTR 加密。 (802.11i, RFC3610)
    - OCB (Offset Codebook Mode): 将MAC整合到加密中。 (是CCM, EAX的2倍快)
    - 上述方案都支持 AEAD (A.E. with associated data): 部分是明文并且整个消息被认证。这在实践中是很常用的,例如一个IP报文需要加密,但IP头部需要以明文方式传输。

14. 安全消息传递补充

    - 认证可能泄漏消息;*注:完整性不同于机密性*
    - 安全消息传递意味着CCA安全性,但反之未必;
    - 不同安全目标应该采用不同的密钥;否则,可能泄漏消息,例如 $\mathsf{Mac}_k(c)=\mathsf{Dec}_k(c)$。
    - 实现可能摧毁理论上的安全性:
       - Padding Oracle 攻击(TLS 1.0): 解密返回两种类型错误: padding error,MAC error;敌手通过猜测来获得最后一字节,如果没有padding错误;参考之前在CCA部分学习的Padding Oracle攻击;
       - 攻击非原子解密(SSH Binary Packet Protocol):解密时,分三步 (1)解密消息长度; (2)读取长度所表明的包数; (3) 检查MAC;敌手针对这种非原子解密过程,实施攻击分三步 (1)发送密文 $c$;(2)发送 $l$ 个包直到“MAC error”发生;(3)获得密文对应的明文 $l = \mathsf{Dec}(c)$。

15. 确定性CPA安全(**Deterministic CPA Security**)

    - 应用:在加密数据库索引后,检索时需要加密明文来检索密文;在磁盘加密中,密文大小需要与明文一样大。但之前学习的CPA安全加密都是非确定性的,而且密文比明文长。
    - 确定性加密(Deterministic encryption):相同的消息在相同密钥下被加密为相同的密文。
      - 问题:这样能实现CPA安全吗?答案是不可能,因为CPA安全意味着非确定性加密,密文长于明文。于是,我们需要新的安全定义。
    - 确定性CPA安全(Deterministic CPA Security): 如果从来不用相同的密钥加密同一个消息两次,实现CPA安全,即密钥和消息对$\left<k,m\right>$ 是唯一的。
      - 这里引入新的条件:消息是可重复的,密钥也可重复,但同一密钥不能重复加密同一消息。这是为了实现CPA而做出的必要改变。相当于获得确定性下CPA安全的同时,丧失同一个消息被同一个密文加密多次的能力。
    - 一个PRP就是固定长度的确定性CPA安全加密方案。
    - 确定性认证加密(Deterministic Authenticated Encryption,DAE):与上面的确定性CPA安全概念类似。

16. 在变长加密中的一个常见错误

    - 常见错误:在 CBC/CTR 模式中采用固定的$IV$。这虽然是确定性的,但是不安全。
    - 敌手能够查询 $(m_{q1}, m_{q2})$ 并且得到 $(c_{q1}, c_{q2})$;然后输出明文:$ IV\oplus c_{q1} \oplus m_{q2}$ 并且期待密文: $c_{q2}$。注:第一个PRF的输入就是$ IV\oplus IV\oplus c_{q1} \oplus m_{q2} = c_{q1} \oplus m_{q2}$ 
    - 下面介绍三种变长明文的CPA安全的确定性加密方案。

17. 合成初始向量法(**Synthetic** IV **(SIV)**)

    - 思路:保持初始向量对敌手仍是不可预测的,但是由明文和密钥确定的。
    - 合成初始向量 SIV :对同一对$\left<k,m\right>$使用一个固定的 $IV$ ,用明文通过PRF生成SIV,再用另一个密钥加密;
    - 一个PRF $F$,和一个 CPA安全 $\Pi:(\mathsf{Enc}_k(r,m), \mathsf{Dec}_k(r,s))$;
    - 生成两个密钥 $(k_1,k_2) \gets \mathsf{Gen}$; 得到合成初始向量 $SIV \gets F_{k_1}(m)$;以SIV做为IV来加密 $c = \left<SIV,\mathsf{Enc}_{k_2}(SIV,m) \right>$;
    - 采用SIV-CTR可以实现 DAE:MAC标签 $t := SIV$ ,然后应用 $CTR_{k_2}$。

18. 宽块PRP(**Wide Block PRP**)

    - 思路:因为一个PRP本身是确定性CPA安全,因此,构造一个大的PRP来加密。
    - 宽块PRP就是PRP,从较短的PRP(例如AES)构造一个更长的块大小,和消息一样大(例如磁盘上一个扇区)。
    - PRP-based DAE: $\mathsf{Enc}_k(m\| 0^{\ell})$。在解密中$\mathsf{Dec}$,如果后半部分明文 $\neq 0^{\ell}$,输出 $\perp$。
    - 窄块(Narrow block)可能泄漏信息,由于有一些块相同时,可能泄漏信息。
    - 标准: IEEE P1619.2 中 CBC-mask-CBC (CMC) 和 ECB-mask-ECB (EME)  。
    - 代价:由于两轮加密比SIV方法慢两倍。

19. 可调加密(**Tweakable Encryption**)

    - 思路:从密钥生成不同的密钥,一次一密
    - 无扩展加密(Encryption without expansion): 明文空间与密文空间相同 $\mathcal{M} = \mathcal{C}$ 意味着没有完整性保护的确定性加密,例如磁盘加密。
    - Tweak是一个类似初始向量的值,在同一密钥下,不同的tweak构造不同的PRP。每一个块采用不同的tweak。
    - 可调块密码(Tweakable block ciphers):用一个密钥生成许多PRP $\mathcal{K} \times \mathcal{T} \times \mathcal{X} \to \mathcal{X}$, $\mathcal{T}$ 是tweak集合。
    - 一种简单的解决方法:以一个Tweak $t$来生成密钥 $k_t = F_k(t), t=1,\dots,\ell$,但要加密两次效率不高,需要更有效的方法。

20. XTS

    - XTS:XEX(Xor-Encrypt-Xor)-based tweaked-codebook mode with ciphertext stealing。 (XTS-AES, NIST SP 800-38E)
    - XEX: $c = F_k(m\oplus x)\oplus x$,其中在 Galois 域上 $x=F_k(I)\otimes 2^j$ ,在扇区 $I$中块 $j$ 对应的tweak是 $(I,j)$ 。
    - Ciphertext stealing (CTS):无需填充(padding),没有扩展。

21. 密钥派生函数(**Key Derivation Function (KDF)**)

    - 密钥派生函数(Key Derivation Function,KDF):从一个秘密的原密钥 $sk$ 产生许多密钥;
    - 对于均匀随机的 $sk$:$F$ 是 PRF, $ctx$ 是标识应用的唯一串,$\mathsf{KDF}(sk,ctx,l) = \left<F_{sk}(ctx\|0),F_{sk}(ctx\|1)\cdots,F_{sk}(ctx\|l)\right>.$
    - 对于非均匀随机的 $sk$:提取并扩展范式 
       - 提取(extract): HKDF $k \gets \mathsf{HMAC}(salt,sk)$, $salt$(盐)是一个随机数。用盐来向密钥添加熵。
       - 扩展(expand):与上面均匀随机情况一样。

22. 基于口令的KDF(**Password-Based KDF, PBKDF**)

    - 密钥延展(Key stretching)增加测试密钥的时间 (使用较慢的哈希函数)。
    - 密钥加强(Key strengthening)增加密钥的长度和随机性 (使用盐)。
    - PKCS\#5 (PBKDF1):$H^{(c)}(pwd\|salt)$, 哈希函数迭代 $c$ 次。
    - 敌手攻击,或者尝试被加强的密钥 (更大的密钥空间),或者尝试初始密钥 (每个密钥花费更长时间)。

23. IV,Nonce,Counter,Tweak和Salt

    - IV:密码学原语的输入,提供随机性。
    - nonce:用来标记一次通信的只使用一次的一个数。
    - counter:一个连续的数,用作nonce或IV。
    - tweak:在一个密码中对每个块只用一次的输入。
    - salt:随机比特,用于创建一个函数的输入。

24. 总结

    - 略



================================================
FILE: notes-Chinese/8.1 公钥加密理论.md
================================================
# 8.1 公钥加密理论

1. 本节学习用于保护信息的完整性和真实性的消息认证码(MAC)和抗碰撞的哈希函数(CRHF)。

2. 目录:公钥加密的定义和安全,陷门排列,选择密文攻击安全,在随机预言机模型中从陷门排列到公钥加密。

3. 私钥密码学局限性

   - 密钥分发需要通信各方在物理上会面;
   - $U$个用户的密钥的数量 $\Theta(U^2)$;
   - 开放系统的安全通信:基于私钥密码学的解决方案无法充分处理开放系统中的安全通信问题,在开放系统中通信各方不能物理上会面,或只能暂时交互;
   - 注:私钥密码学中的一个核心问题就是密钥分发与管理问题。

4. **Needham-Schroeder 协议**

   - *Needham–Schroeder Symmetric Key Protocol*:在开放网络中双方通过一个可信的第三方建立一个会话密钥(session key);
   - 密钥分发中心(Key Distribution Center,KDC)作为可信的第三方(Trusted Third Party,TTP),与通信双方Alice和Bob在事前分别建立了对称密钥;
   - KDC根据Alice的请求,生成一个新的 $k$ 会话密钥(session key),分别用与Alice和Bob分别共享的密钥来加密并发送给Alice;$E_{Bob}(k)$ 作为一个来访问Bob所需的凭证(ticket);
   - 用于MIT's Kerberos 协议 (in Windows);
   - 优点:每一方只需要存储一个密钥;不需要更新通信双方密钥(因为采用新的会话密钥);
   - 弱点:单点失效,一旦KDC被破坏,则整个系统都不安全。

5. Merkle难题(无需可信第三方的密钥交换)

   - Alice准备 $2^{32}$ 个难题 $\mathsf{Puzzle}_i$,并且发送给Bob;难题如下:

     $\mathsf{Puzzle}_i \gets \mathsf{Enc}_{(0^{96}\|p_i)}(\text{``Puzzle \#''} x_i \| k_i),$,其中 $\mathsf{Enc}$ 是 128位加密,$p_i \gets \{0,1\}^{32}$ 并且 $x_i,k_i \gets \{0,1\}^{128}$。

     注:每个难题中明文包括一个随机数和一个密钥,用一个密钥加密;

   - Bob随机选择一个难题 $\mathsf{Puzzle}_j$,并且在 $2^{32}$ 时间内猜测  $p_j$ ,获得 $x_j,k_j$ 并将 $x_j$ 发送给 Alice。

   - Alice 按照$x_j$查询谜题,并且使用 $k_j$ 作为密钥。

   - 敌手需要 $2^{32+32}$ 时间,是诚实方所需时间复杂性的二次方。

   - 在诚实方和敌手之间存在更好的差距吗?如果将加密方法看作是一个黑盒预言机,那么二次差距是最好的。

   - Merkle难题的缺点是谜题数量太大,获得密钥的代价太大;

   - 注:Merkle当时是UC的一名本科生,这是他的一门课程设计申请。

6. 公钥革命

   - 在1976年,Whitfield Diffie 和 Martin Hellman 发表了 “New Directions in Cryptography” (密码学的新方向)。在这篇论文中,提出公钥加密方案、陷门(Trap door)和数字签名等概念。[论文原文链接](https://ee.stanford.edu/%7Ehellman/publications/24.pdf)
   - 非对称(Asymmetric)或公钥(public-key)加密方案:
     - 公钥(Public key)作为加密密钥;(注:接收方产生,发送方持有)
     - 私钥(Private key)作为解密密钥; (注:接收方产生,接收方持有)
   - 公钥原语(Public-key primitives):
     - 公钥加密(Public-key encryption)
     - 数字签名(Digital signatures) (不可抵赖性,non-repudiation)
     - 交互式密钥交换(Interactive key exchange)
   - 优点:
     - 在公开信道上密钥分发
     - 减少保存大量密钥的需求
     - 使得在开放系统的安全成为可能
   - 缺点:慢两到三个数量级,针对公钥分发的主动攻击
     - 注:如何保证Alice得到的公钥真的是Bob的公钥?

7. 公钥加密定义

   - 密钥生成(Key-generation)算法: $(pk,sk) \gets \mathsf{Gen}$, 密钥长度 $\ge n$;
   - 明文空间: $\mathcal{M}$ 与 $pk$ 相关;(注:公钥加密方案通常以数学难题为基础,明文与公钥之间并不完全独立)
   - 加密(Encryption)算法: $c \gets \mathsf{Enc}_{pk}(m)$.
   - 解密(Decryption)算法:$m:= \mathsf{Dec}_{sk}(c)$, 或者输出 $\perp$.
   - 需求:$\Pr[\mathsf{Dec}_{sk}(\mathsf{Enc}_{pk}(m)) = m] \ge 1 - \mathsf{negl}(n)$. (注:公钥加密方案通常以数学难题为基础,存在解密不成功的可能。)

8. 对窃听者的安全 = CPA

   - 由于公钥是公开的,敌手不仅能窃听,而且能够加密任意明文。
   - 在敌手和挑战者间窃听不可区分实验 $\mathsf{PubK}^{\mathsf{eav}}_{\mathcal{A},\Pi}(n)$:
     - 挑战者生成密钥 $(pk,sk) \gets \mathsf{Gen}(1^n)$。
     -  敌手 $\mathcal{A}$ 被给予 $\mathbf{pk}$ 以及 $\mathbf{\mathsf{Enc}_{pk}(\cdot)}$ 预言机的访问,输出相同长度的 $m_0, m_1$ 。 
     -  挑战者随机生成 $b \gets \{0,1\}$。将挑战密文 $c \gets \mathsf{Enc}_{pk}(m_b)$ 发送给敌手 $\mathcal{A}$。
     -  $\mathcal{A}$ 继续访问预言机 $\mathbf{\mathsf{Enc}_{pk}(\cdot)}$ 并且输出 $b'$。
     -  如果 $b' = b$,$\mathcal{A}$ 成功 $\mathsf{PubK}^{\mathsf{eav}}_{\mathcal{A},\Pi}=1$,否则 0。
   - 定义:$\Pi$ 是 CPA-secure, 如果 $\forall$ ppt $\mathcal{A}$, $\exists$ $\mathsf{negl}$ 使得 $\Pr\left[\mathsf{PubK}^{\mathsf{cpa}}_{\mathcal{A},\Pi}(n)=1\right] \le \frac{1}{2} + \mathsf{negl}(n)$。

9. 公钥加密的安全属性

   - 对称加密可以加密32比特消息,产生32比特密文,例如,使用一次一密。在公钥系统中能够做到同样的吗?
   - 一个确定性的公钥加密方案在窃听者出现时是安全的?
   - 如果 $\Pi$ 在窃听者出现时是安全的,那么 $\Pi$ 也是CPA安全的? 是否是多重加密安全的?
   - 完美保密的公钥加密是可能的吗?(注:不可能)

10. 密钥长度比较

    NIST(美国国家标准技术研究所)推荐可比较的密钥长度 (按比特) 。NIST 认为一个112比特的有效密钥长度直到2030年是可接受的,但是推荐 128 比特或更长的密钥。

    | 对称密钥(AES) | RSA/DH | ECC  |
    | --------------- | ------ | ---- |
    | 56              | 512    | 112  |
    | 80              | 1024   | 160  |
    | 112             | 2048   | 224  |
    | 128             | 3072   | 256  |
    | 192             | 7680   | 384  |
    | 256             | 15360  | 512  |

11. 混合加密(**Hybrid Encryption**)构造

    - 为了加速加密,采用私钥加密方案 $\Pi'$ (数据封装机制,data-encapsulation mechanism, DEM) 与公钥加密方案 $\Pi$ (密钥封装机制, key-encapsulation mechanism, KEM) 一起。
    - $\Pi^{\mathsf{hy}} = (\mathsf{Gen}^{\mathsf{hy}}, \mathsf{Enc}^{\mathsf{hy}}, \mathsf{Dec}^{\mathsf{hy}})$:
    -  $\mathsf{Gen}^{\mathsf{hy}}$: $(pk,sk) \gets \mathsf{Gen}(1^n)$. *注:只需提前生成公钥加密方案所需密钥*
    -  $\mathsf{Enc}^{\mathsf{hy}}$: $pk$ and $m$. 
      - $k \gets \{0,1\}^n$. *注:生成私钥加密密钥*
      - $c_1 \gets \mathsf{Enc}_{pk}(k)$, $c_2 \gets \mathsf{Enc}'_{k}(m)$.  *注:用公钥加密的公钥加密私钥加密密钥,用私钥加密密钥加密消息。*
    - $\mathsf{Dec}^{\mathsf{hy}}$: $sk$ and $\langle c_1,c_2\rangle$.
      - $k := \mathsf{Dec}_{sk}(c_1)$. *注:用公钥加密中私钥解密获得私钥加密密钥*
      - $m := \mathsf{Dec}'_k(c_2)$. *注:用私钥加密密钥获得明文*
    - 问题:混合加密方案是公钥加密还是私钥加密?

12. 混合加密安全

    - 定理:如果 $\Pi$ 是一个CPA安全的公钥加密方案,并且 $\Pi'$ 是窃听者不可区分的私钥加密方案,那么 $\Pi^{\mathsf{hy}}$ 是CPA安全的公钥加密方案。
    - 这里对于私钥加密方案的安全性要求只是窃听者不可区分的,不要求是CPA安全的,因为*私钥加密密钥是每次加密时随机产生的新密钥*,私钥加密的加密预言机提供的结果无法被利用。
    - 整个方案安全证明的思路是利用各方案之间不可区分性,以及不可区分性所具有的传递性(transitiviy)。
    - 目标是证明 (1)$\langle pk,\mathsf{Enc}_{pk}(k),\mathsf{Enc}_{k}'(m_0)\rangle$ 与(2) $\langle pk,\mathsf{Enc}_{pk}(k),\mathsf{Enc}_{k}'(m_1)\rangle$ 之间对于不同明文的不可区分性。为此,先观察(1) $\langle pk,\mathsf{Enc}_{pk}(k),\mathsf{Enc}_{k}'(m_0)\rangle$ 与(3) $\langle pk,\mathsf{Enc}_{pk}(0^n),\mathsf{Enc}_{k}'(m_0)\rangle$ 之间对于不同公钥加密明文(私钥加密密钥)之间由于公钥加密方案不可区分性也是不可区分的;同理,(2)$\langle pk,\mathsf{Enc}_{pk}(k),\mathsf{Enc}_{k}'(m_1)\rangle$ 与(4) $\langle pk,\mathsf{Enc}_{pk}(0^n),\mathsf{Enc}_{k}'(m_1)\rangle$ 之间也是不可区分的。(3)$\langle pk,\mathsf{Enc}_{pk}(0^n),\mathsf{Enc}_{k}'(m_0)\rangle$ 与(4) $\langle pk,\mathsf{Enc}_{pk}(0^n),\mathsf{Enc}_{k}'(m_1)\rangle$ 之间由于私钥加密方案不可区分性也是不可区分的。最后,根据不可区分性所具有的传递性,证明混合加密方案的不可区分性。

13. 混合加密范式应用

    - 共享文件访问,Alice用自己的对称密钥加密文件,Bob的公钥加密对称密钥
    - 密钥托管,Alice用托管服务器的公钥加密对称密钥,领导从托管服务器获得私钥来解锁

14. 陷门函数(**Trapdoor Function**)

    - 陷门函数(Trapdoor function): 易于计算,在缺乏特定信息(陷门)时难以求逆,即带有陷门的单向函数。
    - 1982年,姚期智在论文《Theory and Applications of Trapdoor Functions》中提出,从任意陷门函数中可构造一个公钥加密方案。

15. 函数族(**Families of Functions**)

    - $\Pi = (\mathsf{Gen}, \mathsf{Samp}, f)$ 是一个函数组,如果:
      - 参数生成(Parameter-generation)算法: $I \gets \mathsf{Gen}(1^n)$。参数$I$定义了定义域(domain)$\mathcal{D}_I$和值域(range)$\mathcal{D}_R$。注:这里产生了一个具体的函数参数。
      - 采样(sampling)算法: $x \gets \mathsf{Samp}(I)$,均匀随机地产生一个$x$。
      - 确定性赋值(evaluation)算法: $y := f_I(x)$。
    - 这里强调采样算法是因为后面要学习的数论难题的输入是要满足某些条件的。

16. 陷门排列族

    - 一组多项式时间算法 $\Pi = (\mathsf{Gen}, \mathsf{Samp}, f, \mathsf{Inv})$ 是一个陷门排列族(family of trapdoor permutations,TDP),如果:
      - 参数生成(parameter generation)算法 $\mathsf{Gen}$, 输入 $1^n$,输出 $(I,\mathsf{td})$ 有 $|I| \ge n$。其中, $(I, \mathsf{td})$ 定义了集合 $\mathcal{D}_I = \mathcal{D}_{\mathsf{td}}$。注:陷门排列族是一个函数集合,参数生成算法产生一个具体陷门排列所需的参数。
      - $\mathsf{Gen}_I$ 只输出 $I$。$(\mathsf{Gen}_I, \mathsf{Samp}, f)$ 是 OWP。其中的$\mathsf{Samp}$是采样函数,用于获得函数的输入$x \gets \mathcal{D}_I$。
      - 一个确定性求逆算法 $\mathsf{Inv}$,对于 $\forall (I,\mathsf{td})$ 并且 $\forall x \in \mathcal{D}_{I}$, $ \mathsf{Inv}_{\mathsf{td}}(f_I(x))=x$。*注:可求逆*
    - 核心断言:确定性多项式时间算法 $\mathsf{hc}$ 是$\Pi$ 的一个核心断言(hard-core predicate),如果 $\forall$  ppt $\mathcal{A}$,$\exists$ $\mathsf{negl}$ 使得 $ \Pr[\mathcal{A}(I,f_I(x)) = \mathsf{hc}_I(x)] \le \frac{1}{2} +\mathsf{negl}(n)$。
    - 定理:给定一个陷门排列族$\Pi = (\mathsf{Gen}, \mathsf{Samp}, f, \mathsf{Inv})$,则存在一个带有核心断言的陷门排列族$\widehat{\Pi} = (\widehat{\mathsf{Gen}}, \mathsf{Samp}, f, \mathsf{Inv})$。注:证明与单向函数部分关于核心断言的定理类似。

17. TDP例题

    - 如果答案是肯定的,则需要反证法证明,$f'$若不是TDP,那么$f$也不是;
    - 如果答案是否定的,则需要给出一个有效的求逆方法。

18. 从TDP到公钥加密方案

    - 从一个带有核心断言$\mathsf{hc}$的陷门排列族$\widehat{\Pi} = (\widehat{\mathsf{Gen}}, \mathsf{Samp}, f, \mathsf{Inv})$来构造一个公钥加密方案:
      - $\mathsf{Gen}$: $(I, \mathsf{td}) \gets \widehat{Gen}$ 输出公钥 $I$ 和私钥 $\mathsf{td}$。
      - $\mathsf{Enc}$: 输入 $I$ 和 $m \in \{0,1\}$,选择一个 $x\gets \mathcal{D}_I$ 并且输出 $\langle f_I(x), \mathsf{hc}_I(x)\oplus m \rangle$。
      - $\mathsf{Dec}$: 输入 $\mathsf{td}$ 和 $\langle y, m'\rangle$,计算 $x:= f^{-1}_I(y)$ 并且输出 $\mathsf{hc}_I(x)\oplus m'$。
    - 定理:如果 $\widehat{\Pi}=(\widehat{Gen},f)$ 是 TDP,并且 $\mathsf{hc}$ 是$\widehat{\Pi}$的 HCP  ,那么构造 $\Pi$ 是 CPA安全的。
    - 问题:这个方案是安全的吗?$\mathsf{Enc}_{I}(m) = f_I(m)$, $\mathsf{Dec}_{\mathsf{td}}(c) = f^{-1}_I(c)$。

19. 证明

    - $\mathsf{hc}_I(x)$ 是伪随机的。将 $\mathcal{A}_{\mathsf{hc}}$ for $\mathsf{hc}$ 规约到 $\mathcal{A}$ for $\Pi$。
    - $\Pr[\mathcal{A}_{\mathsf{hc}}(I,f_I(x))=\mathsf{hc}_I(x)] = $ $\frac{1}{2}\cdot (\Pr[b'=b|z=\mathsf{hc}_I(x)]+\Pr[b'\neq b|z\neq \mathsf{hc}_I(x)]).$
    - 上面的公式的含义是 $\mathcal{A}_{\mathsf{hc}}$成功得到核心断言包含两种情况:
      - 当$z$是核心断言,则$\mathcal{A}$面对的是方案$\Pi$,$\mathcal{A}$成功($b'=b$),输出的$z$就是核心断言;
      - 当$z$不是核心断言,则$\mathcal{A}$面对的挑战密文与$\Pi$中是相反的,$\mathcal{A}$失败($b'\neq b$),输出的$\overline{z}$就是核心断言;

20. 证明(续)

    - $\Pr[b'=b|z=\mathsf{hc}_I(x)] = \Pr[\mathsf{PubK}^{\mathsf{eav}}_{\mathcal{A},\Pi}(n)=1]=\varepsilon(n)$。 *注:$\mathcal{A}$实验成功。*
    - 如果 $z \neq \mathsf{hc}_I(x)$, $m' = m_b\oplus \overline{\mathsf{hc}}_I(x) = m_{\overline{b}}\oplus \mathsf{hc}_I(x)$,这意味着 $m_{\overline{b}}$ 被加密了。
    - $\Pr[b'=b|z\neq \mathsf{hc}_I(x)] = \Pr[\mathsf{PubK}^{\mathsf{eav}}_{\mathcal{A},\Pi}(n)=0]=1-\varepsilon(n)$。 *注:$\mathcal{A}$实验失败了。*
    - $\Pr[b'\neq b|z\neq \mathsf{hc}_I(x)] =\varepsilon(n)$。
    - $\Pr[\mathcal{A}_{\mathsf{hc}}(I,f_I(x))=\mathsf{hc}_I(x)] = \frac{1}{2}\cdot (\varepsilon(n)+\varepsilon(n)) = \varepsilon(n)$。 *注:根据上一页的公式。*
    - 至此,我们学习了基于陷门排列的公钥加密方案,但只能加密一个比特,如何加密一个更长的明文?后面学习随机预言机模型设定下的公钥加密方案。

21. 在公钥设定中CCA情景

    - CCA
      - 敌手 $\mathcal{A}$ 观察由 $\mathcal{S}$ 发送给 $\mathcal{R}$ 的密文 $c$ 。
      - $\mathcal{A}$ 以$\mathcal{S}$ 或自己的名义发送 $c'$ 给 $\mathcal{R}$ 。
      - $\mathcal{A}$ 根据从 $c'$ 中解密出的 $m'$ 来推断 $m$。
    - 情景
      - 用口令来登陆在线银行:试错,从银行反馈中获得信息。
      - 邮件回复中包含解密出的文本的引用。
      - 密文的可锻造性,例如,在拍卖中将其他人的出价翻倍。

22. 对CCA/CCA2的安全定义

    - CCA/CCA2 不可区分实验 $\mathsf{PubK}^{\mathsf{cca}}_{\mathcal{A},\Pi}(n)$:
      - $(pk,sk) \gets \mathsf{Gen}(1^n)$.
      - $\mathcal{A}$ 给定输入 $pk$ 和预言机访问 $\mathsf{Dec}_{sk}(\cdot)$,输出相同长度的 $m_0, m_1$ 。
      - $b \gets \{0,1\}$。挑战密文 $c \gets \mathsf{Enc}_{pk}(m_b)$ 给 $\mathcal{A}$。
      - 在CCA2中,$\mathcal{A}$ 除了 $c$ 之外还可以访问 $\mathsf{Dec}_{sk}(\cdot)$,并输出$b'$ 。注:CCA 也被称作午餐攻击。CCA2 也被称为适应性的 CCA。
      - 如果 $b' = b$,那么 $\mathcal{A}$ 成功, $\mathsf{PrivK}^{\mathsf{cca}}_{\mathcal{A},\Pi}=1$,否则 0。
    - $\Pi$ 是 CCA/CCA2安全的,如果 $\forall$ ppt $\mathcal{A}$, $\exists$ $\mathsf{negl}$ 使得 $ \Pr\left[\mathsf{PubK}^{\mathsf{cca}}_{\mathcal{A},\Pi}(n)=1\right] \le \frac{1}{2} + \mathsf{negl}(n)$.

23. 例题

    - 略

24. CCA2安全加密技术进展

    - 零知识证明(Zero-Knowledge Proof):复杂并不可实践。(例如,Dolev-Dwork-Naor)
    - 随机预言机模型(Random Oracle model):有效,但并不踏实 (将 CRHF 当作 RO)。 (例如,RSA-OAEP 和 Fujisaki-Okamoto)
    - DDH(决策性Diffie-Hellman)假设和UOWHF(全域单向哈希函数):大小扩展2倍,但可以在没有RO和ZKP场景下证明安全  (例如,Cramer-Shoup system)。
    - CCA2安全意味着明文感知(Plaintext-aware):敌手在不知道明文的情况下,不能产生有效的密文。
    - 开放问题:如何构造一个与“书本上RSA”一样有效的,基于RSA问题的CCA2安全的方案。

25. 随机预言机模型(**Random Oracle Model,ROM**)

    - 为了在实践中实现CPA安全和CCA安全的公钥加密方案,引入了一个更强大的随机对象,称为随机预言机(Random Oracle Model)。
    - 随机预言机(RO):一个真随机函数($H$)对每个可能的查询回答一个随机应答。
      - 一致性:如果$H$曾经在运行中为一个输入 $x$ 输出 $y$,那么它一直对相同的输入输出相同的答案。
      - 无人“知道”整个函数 $H$。
    - 随机预言机模型(ROM):存在一个公开的RO。与此相对的,不存在RO的情况,称作标准模型。
    - 方法论:在ROM中构造可证明的安全。
      1. 在ROM中,一个方案被设计并被证明是安全的。
      2. 将 $H$ 用一个哈希函数 $\hat{H}$,例如 SHA256。
    - 无人严格地声明随机预言机存在。
    - 存在某些方案,在ROM中被证明是安全的,但无论如何将随机预言机实例化都不是安全的。
    - 使用ROM,很容易实现可证明安全,同时通过正确的实例化来保持高效。

26. ROM的简单例子

    - 由于RO “强大的随机性”,其可以充当或构造之前学习过得密码学原语,包括为单向函数、抗碰撞哈希函数、伪随机函数等。

    - 一个 RO 将 $n_1$ 比特输入映射为 $n_2$ 比特输出。

    - RO 作为 OWF,进行如下实验:

      1. 选择一个RO $H$ ;

      2. 选择一个随机的 $x \in \{0,1\}^{n_1}$ ,并且赋值  $y := H(x)$ ;

      3. 敌手 $\mathcal{A}$ 被给予 $y$,如果输出 $x'$: $H(x')=y$,则成功;

         解释:如果敌手成功求逆,则意味着敌手“事先”询问过RO;

    -  RO 作为 CRHF,进行如下实验:

      1.  选择一个RO $H$ ;

      2.  敌手 $\mathcal{A}$ 成功,如果其输出 $x, x'$ 满足 $H(x)=H(x')$ ,但是 $x\neq x'$;

         解释:如果敌手找到碰撞,则意味着$H$不是随机的,因为两个随机输出不可能相同。

    - 从一个RO构造PRF : $n_1=2n$, $n_2=n$.

      - $ F_k(x) \overset{\text{def}}{=} H(k\| x),$ $|k|=|x|=n.$

        解释:如果$F$不是伪随机的,则$H$也可以与真随机相区分。

27. CPA安全

    - 思路:PubK CPA = PrivK + (Secret Key = TDP + RO) 
    - 实现CPA安全的公钥加密方案,可以基于一个安全的私钥加密方案,其中私钥加密的密钥由RO得到,通过TDP传递生成密钥所用的随机量;
    - 构造:
      -  $\mathsf{Gen}$: $pk = I$, $sk = \mathsf{td}$.
      -  $\mathsf{Enc}$: $r \gets \{0,1\}^*$, 输出 $\langle c_1= f_I(r), c_2 = \mathsf{H}(r)\oplus m\rangle$.
      -  $\mathsf{Dec}$: $r := f^{-1}_{\mathsf{td}}(c_1)$, 输出 $\mathsf{H}(r)\oplus c_2$.
    - 定理:如果 $f$ 是 TDP, 并且 $H$ 是 RO,则构造是 CPA 安全的。
    - 解释:私钥加密方案只需要是窃听下安全,因为每次加密都是概率性的,每次私钥加密密钥都是重新生成的。该方案不是CCA安全的,因为篡改密文可以直接影响明文。
    - 用RO的必要性:由于$r$的部分信息可能通过TPD泄漏,如果以一个PRG来替换掉RO,则由于种子的部分信息已知,PRG的输出也不在是伪随机的,加密方案也不再安全。
    
28. 基于私钥加密的CCA安全

    - 思路:PubK CCA = PrivK CCA + (Secret Key = TPD + RO)
    - 实现CCA安全的公钥加密方案,可以基于一个CCA安全的私钥加密方案,其中私钥加密密钥由RO得到,通过TDP传递生成密钥所用的随机量;
    - 构造:
      -  $\Pi'$ 是一个安全私钥加密方案。
      - $\mathsf{Gen}$: $pk = I$, $sk = \mathsf{td}$.
      - $\mathsf{Enc}$: $k := H(r), r \gets D_I$, 输出 $\langle c_1= f_I(r), c_2 = \mathsf{Enc}'_k(m)\rangle$.
      - $\mathsf{Dec}$: $r := f^{-1}_{\mathsf{td}}(c_1)$, $k:=H(r)$, 输出 $\mathsf{Dec}'_k(c_2)$.
    - 定理:如果 $f$ 是 TDP,$\Pi'$ 是 CCA 安全的,并且 $H$ 是 RO,那么构造是 CCA 安全的。
    - 解释:公钥加密方案的CCA安全性来自私钥加密方案的CCA安全性。

29. 在ROM中基于TPD的CCA安全

    - 思路:PubK CCA = TDP + 2 RO (一个用于加密,一个用于MAC)
    - 实现CCA安全的公钥加密方案,可以通过RO来构造一个CPA安全的公钥加密方案,以明文和密文一起作为输入来生成MAC标签。
    - 构造:
      -  $\mathsf{Gen}$: $pk = I$, $sk = \mathsf{td}$
      -  $\mathsf{Enc}$: $r \gets D_I$,输出 $\langle c_1=f_I(r), c_2 = H(r)\oplus m, c_3=G(c_2\|m)\rangle$
      -  $\mathsf{Dec}$: $r := f^{-1}_{\mathsf{td}}(c_1)$, $m := H(r)\oplus c_2$。如果 $G(c_2\|m) = c_3$ 输出 $m$,否则 $\perp$。
    - 定理:如果 $f$ 是 TDP,$G,H$ 是 RO,那么构造是 CCA 安全的。
    - 解释:其CCA安全性在于对密文的任何篡改,都无法通过MAC验证。

30. 私钥加密 vs. 公钥加密

    |             | 私钥加密 | 公钥加密 |
    | ----------- | :------: | :------: |
    | 密钥        |   双方   |  接收者  |
    | 最弱攻击    |  窃听者  |   CPA    |
    | 概率性      | CPA/CCA  |   一直   |
    | 对CPA的假设 |   OWF    |   TDP    |
    | 对CCA的假设 |   OWF    |  TDP+RO  |
    | 效率        |    快    |    慢    |

    





================================================
FILE: notes-Chinese/8.2 RSA问题与加密.md
================================================
# 8.2 RSA问题与加密

1. 本节学习第一个也是目前应用最广泛的公钥加密方案RSA。

2. 目录:RSA问题,针对“书本上RSA”加密的攻击,实践中的RSA加密。

3. RSA概览
   
   - RSA: Ron Rivest, Adi Shamir and Leonard Adleman, 三位作者于1977年发表RSA加密方案。
   - RSA问题: 给定 $N = pq$  (两个不同的大质数的乘积) 并且 $y \in \mathbb{Z}^*_N$,计算 $y^{-e}$,即$y$模$N$下的$e$次方根。
   - 开放问题:RSA问题比分解 $N$ 更容易吗?
   - RSA相关标准: PKCS\#1 (RFC3447/8017), ANSI X9.31, IEEE 1363
   - 密钥长度:1,024 到 4,096 比特
   - 已知最强的公开密码学分析:768比特密钥已经被破解 
   - RSA挑战赛:破解 RSA-2048 来赢得 \$200,000 USD
   - 密钥长度比较 :3072比特RSA密钥安全强度相当于128比特对称密钥
   
4. 书本上的RSA

   - 构造:
     - $\mathsf{Gen}$: 输入 $1^n$ 运行 $\mathsf{GenRSA}(1^n)$ 产生 $N,e,d$。 $pk = \langle N,e \rangle$ 和 $sk = \langle N,d \rangle$。
     - $\mathsf{Enc}$: 输入 $pk$ 和 $m \in \mathbb{Z}^*_N$,获得密文  $c:= [m^e \bmod N]$.
     - $\mathsf{Dec}$: 输入 $sk$ 和 $m \in \mathbb{Z}^*_N$,获得明文 $m:= [c^d \bmod N]$.
   - 不安全性:由于“书本上的RSA”是确定性的,在我们已经提出的任何安全定义下都是不安全的。
   - 下面学习问题:如何产生 $N,e,d$? 什么是 $\mathbb{Z}^*_N$? 如何计算 $m^e \bmod N$? 这个难题是TDP? 为什么很难?
   - 参考教材:《A Computational Introduction to Number Theory and Algebra》(Version 2)  Victor Shoup。

5. 质数与模算术

   - 整数集合 $\mathbb{Z}$, $a,b,c \in \mathbb{Z}$。
   - $a$ 整除 $b$: $a \mid b$ 如果 $\exists c, ac=b$ (否则 $a \nmid b$). $b$ 是 $a$ 的倍数。如果 $a \notin \{1,b\}$,那么 $a$ 是 $b$ 的因子。 
   -  $p > 1$ 是质数(素数),如果其没有因子;否则,是合数。
   - $\forall a,b$, $\exists$ 商 $q$, 余数 $r$: $a=qb+r$, 且 $0\le r < b$。
   - 最大公因子 $\gcd(a,b)$ 是最大的整数 $c$ 使得 $c\mid a$ 且 $c\mid b$。 $\gcd(0,b)=b$, $\gcd(0,0)$未定义。
   - $a$ 和 $b$ 是互质,如果 $\gcd(a,b)=1$。
   - 余数 $r= [a\bmod N] = a - b\lfloor a/b\rfloor $ 并且 $r<N$. $N$ 称为模。
   - $\mathbb{Z}_N = \{0,1,\dots,N-1\} = \{a \bmod N | a \in \mathbb{Z}\}$.
   - $a$ 是模 $N$ 下可逆的$\iff \gcd(a,N) = 1$。如果 $ab \equiv 1 \pmod N$,那么 $b=a^{-1}$是模 $N$ 下 $a$ 的乘法逆。  

6. 模算术例子

   - 欧几里德算法(辗转相除法): $\gcd(a,b) = \gcd(b, [a \bmod b]).$
     -  $\gcd(12, 27)$ 
     
   - 扩展欧几里德算法:给定 $a,N$,寻找 $X,Y$ 使得 $Xa+YN = \gcd(a,N)$ (贝祖定理)
     - 例子,求11 (mod 17)下的逆元,$a = 11$,$N = 17$,$Xa + YN = r$
     
       ```
                      r     X    Y  m
                      17    0    1
                      11    1    0  1
                       6   -1    1  1
                       5    2   -1  1
                       1   -3    2   
       ```
     
   - 求余然后相加/乘
   
     - 计算 $193028 \cdot 190301 \bmod 100$
   
   - 消去律:如果 $\gcd(a,N)=1$ 且 $ab \equiv ac \pmod N$,那么 $b \equiv c \pmod N$.
     - $a=3, c=10, b=2, N=24$
   
7. $\mathbb{Z}_N^*$ 群

   - $ \mathbb{Z}_N^* \overset{\text{def}}{=} \{a \in \{1,\dotsc,N-1 \} | \gcd(a,N) = 1\} $
   - 群是一个集合 $\mathbb{G}$ 带有一个二元操作 $\circ$:
     - 闭包: $\forall g,h \in \mathbb{G}$, $g \circ h \in \mathbb{G}$.
     - 单位元: $\exists$ 单位元 $e\in \mathbb{G}$ 使得 $\forall g\in \mathbb{G}, e \circ g = g = g \circ e$.
     - 逆元: $\forall g \in G$, $\exists\; h \in \mathbb{G}$ 使得 $g \circ h =e = h \circ g$. $h$ 是 $g$ 的逆元.
     - 结合律:$\forall g_1,g_2,g_3 \in \mathbb{G}$, $(g_1\circ g_2)\circ g_3 = g_1 \circ (g_2 \circ g_3)$.
   - $\mathbb{G}$ with $\circ$ 是阿贝尔群,如果有交换律:$\forall g,h \in \mathbb{G}, g\circ h = h\circ g$.
   - 逆元的存在意味着消去律
   - 当 $\mathbb{G}$ 是有限群,$| \mathbb{G}|$ 是群的阶。
   - 问题: $\mathbb{Z}_N^*$ 是乘法下的群吗? $\mathbb{Z}_N$ 在乘法下呢? $\mathbb{Z}_{15}^* = ?$   $\mathbb{Z}_{13}^* = ?$

8. 群指数

   - $ g^m \overset{\text{def}}{=} \underbrace{g\circ g\circ \cdots \circ g}_{m\; \text{times}}. $
   - 欧拉定理:$\mathbb{G}$ 是有限群。那么, $\forall g \in \mathbb{G}, g^{|\mathbb{G}|}=1$. 
   - 注:课上证明,将群中每个元素与 $g$ 相乘后连乘等于群中元素连乘。
   - 例子:计算 $3 \in \mathbb{Z}_{7}^*$ 的所有幂。
   - 费马小定理:$\forall g \in \mathbb{G}$ and $i$, $g^i \equiv g^{[i \bmod {|\mathbb{G}|}]}$.
   - 注:这是欧拉定理的推论。
   - 例子:计算 $3^{78} \in \mathbb{Z}_{7}^*$

9. 算术算法

   - 加/减:线性时间 $O(n)$.
   - 乘:最初 $O(n^2)$。
     - Karatsuba (1960,当时23岁): $O(n^{\log_2 3})$ $(2^bx_1+x_0) \times (2^by_1+ y_0)$ 使用3个乘法。
     - 注:因为 $x_1 \cdot y_0 + x_0 \cdot y_1 = (x_1 + x_0) \cdot (y_1 + y_0) - x_1 \cdot y_1 - x_0 \cdot y_0$。
     - 最佳渐进算法: $O(n\log n)$。
   - 除/求余:$O(n^2)$。
   - 指数:$O(n^3)$,平方指数法,例如计算8次幂并不需要乘8次,而是计算4次幂的平方,而4次幂来自2次幂平方。
     - 输入 $g \in G$; 指数 $x=[x_nx_{n-1}\dots x_2x_1x_0]_2$
     - 输出:$g^x$
     - $y \gets g; z \gets 1$
     - For $i = 0$ to $n$  
       - If ($x_i == 1$){$z \gets z \times y$} 
       - $y \gets y^2$
     - Return $z$
     - 这里举个例子,例如算$g^9$

10. 欧拉的Phi函数

    - 欧拉phi函数:$\phi(N) \overset{\text{def}}{=} |\mathbb{Z}_N^*|$. *注:整数乘法群的阶*
    - 算法基本定理:$N = \prod_ip_i^{e_i}$ , $\{p_i\}$ 是不同的质数, $\phi(N) = \prod_ip_i^{e_i-1}(p_i-1)$。
    - 例题:$N=pq$ 其中 $p,q$ 是不同质数。$\phi(N)=?$ $\phi(12)=?$ $\phi(30)=?$
    - 欧拉定理与费马小定理:$a \in \mathbb{Z}_N^*$. $a^{\phi (N)} \equiv 1 \pmod N$. *注:前面证明过*
    - 如果 $p$ 是质数并且 $a \in \{1,\dotsc,p-1\}$,那么 $a^{p-1} \equiv 1 \pmod p$. *注:因为质数$p$乘法群的阶为$p-1$*
    - 例题:$3^{43} \bmod 49 = ?$

11. 基于群指数函数的排列

    - 指数函数 $f_e\;:$ $\mathbb{Z}^*_N \to \mathbb{Z}^*_N$ by $f_e(x) =[x^e \bmod N]$.
    - 对指数函数求逆:$y$ 的 $e$ 次方根: $x^e \equiv y$, $x \equiv y^{1/e}$. 
    - 推论:如果 $\gcd(e,\phi(N))=1$,那么 $f_e$ 是排列。
    - 证明:令 $d = [e^{-1} \bmod \phi(N)]$,那么 $f_d$ 是 $f_e$ 的逆函数。$y \equiv x^{e};\quad f_{d}(y) \equiv y^d \equiv x^{ed} \equiv x$. 
    - 例题:在 $\mathbb{Z}^*_{10}$ 中, $e = 3,\ d = ?,\ f_{e}(3) = ?,\ f_{d}(f_{e}(3)) = ?,\ 9^{\frac{1}{3}} = ?$
    - 问题:如果对于某些特别的$N$无法计算 $\phi(N)$ ,那么会如何?如果不能分解 $N$ 呢?

12. 整数分解是难的

    - 分解 $N=pq$. $p,q$ 长度相同为 $n$.
    - 尝试分解: $\mathcal{O}(\sqrt{N}\cdot \mathsf{polylog}(N))$.
    - Pollard's $p-1$ 方法: 当 $p-1$ 具有小质数因子时有效。
    - Pollard's rho 方法: $\mathcal{O}(N^{1/4}\cdot \mathsf{polylog}(N))$.
    - 二次筛法 [Carl Pomerance]: 亚指数时间 $\mathcal{O}(\exp(\sqrt{n\cdot \log n}))$.
    - 已知最优算法为通用数域筛法 [Pollard]:$\mathcal{O}(\exp(n^{1/3}\cdot(\log n)^{2/3}))$.

13. RSA问题是难的

    - 思路:分解难 $\implies$ 对于 $N=pq$, 找到 $p,q$ 难 $\implies$ 计算 $\phi(N)=(p-1)(q-1)$ 难 

      $\implies$ 无法模 $\phi(N)$ 计算

      $\implies$ 计算 $e^{-1} \bmod \phi(N)$ 难

      **这里存在一段空白**

      $\implies$ RSA 问题难:给定 $y \in \mathbb{Z}^*_N$, 计算 $y^{-e}$ modulo $N$.

    - 开放问题:RSA 比分解容易?

14. 产生随机质数

    - 为了构造RSA问题,首先需要一个产生随机质数的方法:随机选择的一个数,测试其是否为质数。
    - 该方法的有效性需要回答两个问题:(1) 随机选择的数是质数的概率多大?(2) 是否能够有效地测试其是否为质数?
    - 对于问题1,$\exists$ 常数 $c$ 使得, $\forall n>1$, 一个随机选择的 $n$ 比特数为质数的概率至少 $c/n$。
    - 对于问题2,如果 $N$ 是质数,那么Miller-Rabin质性测试始终输出质数。如果 $N$ 是合数,那么算法输出质数的概率至多 $2^{-t}$。

15. 产生RSA问题

    - 令 $\mathsf{GenModulus}(1^n)$ 为一个概率多项式时间算法,输入 $1^n$, 输出 $(N,p,q)$ ,其中 $N=pq$, 并且 $p,q$ 是 $n$ 比特质数,除了有可忽略的概率失败。
    - 产生RSA问题算法简述:
      1. 由$\mathsf{GenModulus}(1^n)$ 产生 $(N,p,q)$ ;
      2. 计算$\phi(N) := (p-1)(q-1)$;
      3. 寻找一个$e$,使得$\gcd(e,\phi(N))=1$;
      4. 计算$d := [e^{-1} \bmod \phi(N)]$;
      5. 返回 $N,e,d$

16. RSA假设

    - RSA实验 $\mathsf{RSAinv}_{\mathcal{A},\mathsf{GenRSA}}(n)$:
      1. 运行 $\mathsf{GenRSA}(1^n)$ 来产生 $(N,e,d)$。
      2. 选择 $y \gets \mathbb{Z}^*_N$。
      3. 敌手 $\mathcal{A}$ 给定 $N,e,y$, 并输出 $x \in \mathbb{Z}^*_N$.
      4. $\mathsf{RSAinv}_{\mathcal{A},\mathsf{GenRSA}}(n)=1$ ,实验成功,如果 $x^e \equiv y \pmod N$,否则实验失败 0 。
    - 定义:RSA问题相对于$\mathsf{GenRSA}$是难的,如果 $\forall$ PPT算法 $\mathcal{A}$, $\exists$ $\mathsf{negl}$ 使得,$ \Pr[\mathsf{RSAinv}_{\mathcal{A},\mathsf{GenRSA}}(n) = 1] \le \mathsf{negl}(n). $

17. 构造陷门排列

    - 用 $\mathsf{GenRSA}$ 来定义一个排列族:
      - $\mathsf{Gen}$: 输入 $1^n$, 运行 $\mathsf{GenRSA}(1^n)$ 来产生 $(N,e,d)$ 并且 $I=\langle N,e \rangle, \mathsf{td}=d$, 令 $\mathcal{D}_I = \mathcal{D}_{\mathsf{td}} = \mathbb{Z}^*_N$.
      - $\mathsf{Samp}$: 输入 $I$, 挑选一个随机元素 $x$ of $\mathbb{Z}^*_N$.
      - $f_{I}(x) = [ x^e \bmod N]$.
      - 确定性求逆算法 $\mathsf{Inv}_{\mathsf{td}}(y) = [ y^d \bmod N]$.
    - 将RSA问题规约到陷门排列求逆问题。

18. 回顾“书本上的RSA”

    - 略

19. 攻击带有小$e$的“书本上的RSA”

    - 小 $e$ 和 小 $m$ 令模算术失去作用,不再是难题。
      - 如果 $e=3$ 并且 $m < N^{1/3}$,那么 $c = m^3$ 并且 $m=$ ?
      - 在混合加密中,1024比特 RSA 与 128比特 AES。
    - 当小$e$ 被使用时通用攻击:
      -  $e=3$, 同一个消息 $m$ 被发送给 3 个不同的接收者。
      -  $c_1= [ m^3 \bmod N_1]$, $c_2= [ m^3 \bmod N_2]$, $c_3= [ m^3 \bmod N_3]$.
      -  $N_1,N_2,N_3$ 互质, 并且 $N^*=N_1N_2N_3$,使用中国剩余定理可知,$\exists$ 唯一的 $\hat{c} < N^*$:
      - $\hat{c} \equiv c_1 \pmod{N_1}$, $\hat{c} \equiv c_2 \pmod{N_2}$, $\hat{c} \equiv c_3 \pmod{N_3}$.
      - $\hat{c} \equiv m^3 \pmod{N^*}$. 由于 $m^3 < N^*$, $m = \hat{c}^{1/3}$.

20. 对恢复明文的二次改进

    - 如果 $1 \le m < \mathcal{L} = 2^{\ell}$, 存在一个算法可以在 $\sqrt{\mathcal{L}}$ 时间恢复 $m$ 。
    - 思路:$ c \equiv m^e = (r\cdot s)^e = r^e\cdot s^e \pmod N $
    - 算法:
      - 输入:公钥 $\langle N,e \rangle$; 密文 $c$; 参数 $\ell$
      - 输出:$m < 2^{\ell}$ 使得 $m^e \equiv c \pmod N$
      -  $T := 2^{\alpha \ell}$     //$\frac{1}{2} < \text{constant}\; \alpha <1$
      - For{$r=1$ to $T$} {$x_r := [c/r^e \bmod N]$}
      - sort pairs $\{ (r,x_r)\}^T_{r=1}$ by $x_r$ 
      - For $s=1$ to $T$
        - If $[s^e \bmod N] \overset{?}{=} x_r$ for some $r$
          - Return $[r\cdot s \bmod N]$
      - Return fail

21. 共模攻击

    - 共模攻击使用相同的模数 $N$.
    - 情况1:多个用户带有自己的密钥。每个用户可以以自己的 $e,d$ 计算 $\phi(N)$ ,然后找到其他人的  $d$.
    - 情况2:用两个公钥为同一个消息加密。
      - 假设 $\gcd(e_1,e_2)=1$, $c_1 \equiv m^{e_1}$ and $c_2 \equiv m^{e_2} \pmod N$. $\exists X,Y$ 使得 $Xe_1 + Ye_2 = 1$ (贝祖定理).
      - $ c_1^X\cdot c_2^Y \equiv m^{Xe_1}m^{Ye_2} \equiv m^1 \pmod N. $
      - $N = 15, e_{1} = 3, e_{2} = 5, c_{1} = 8, c_{2} = 2, m = ?$  

22. 对“书本上RSA”的CCA

    - 使用CCA恢复消息:敌手 $\mathcal{A}$ 选择一个随机数 $r \gets \mathbb{Z}^*_N$ 并计算 $c' = [r^e\cdot c \bmod N]$,使用CCA获得 $m'$ 。那么,$m= ?$
    - 在拍卖中讲价格翻倍:$c = [m^e \bmod N]$. $c'= [2^ec \bmod N]$.

23. RSA实现问题

    - 将二进制串编码为 $\mathbb{Z}^*_N$ 中元素: $\ell = \|N\|$。任意长度为$\ell - 1$ 的二进制串 $m$ 可以被看作是 $Z_N$ 中元素。尽管 $m$ 不在 $Z_N^*$ 中,RSA 仍工作。
    - $e$ 的选择:$e=3$ 或小 $d$ 都是坏选择。 推荐 $e=65537=2^{16}+1$
    - 使用中国剩余定理来加速解密:$ [c^d \bmod N] \leftrightarrow ([c^d \bmod p],[c^d \bmod q]). $
    - 假设一个 $n$ 比特整数指数预算需要 $n^3$ 操作。RSA 解密花费 $(2n)^3=8n^3$,其中使用中国剩余定理需要 $2n^3$。

24. Padded RSA

    - 思路:添加随机性来改进安全
    - 构造:
      - 令 $\ell$ 为一个函数,对所有 $n$, $\ell(n) \le 2n-2$,为被加密的消息长度。
      - $\mathsf{Gen}$: 输入 $1^n$, 运行 $\mathsf{GenRSA}(1^n)$ 来产生 $(N,e,d)$. 输出 $pk = \langle N,e \rangle$ 和 $sk = \langle N,d \rangle$。
      - $\mathsf{Enc}$: 输入 $m \in \{0,1\}^{\ell(n)}$, 选择随机串 $r \gets \{0,1\}^{\|N\| - \ell(n)-1}$. 输出 $c:=[(r\|m)^e \bmod N]$。*注:填充随机串后加密*
      - $\mathsf{Dec}$: 计算 $\hat{m} := [c^d \bmod N]$, 并输出  $\hat{m}$  中的低$\ell(n)$个比特。*注:这部分为明文*
    - $\ell$ 不应该太大 (理论上的 $r$ 太小) 也不应该太小 (实践中的 $m$ 太小)。
    - 定理:如果RSA问题相对于$\mathsf{GenRSA}$ 是难的,那么基于 $\ell(n)=\mathcal{O}(\log n)$ 的构造是CPA安全的。
    - 证明:与对称加密中CPA安全方案类似。

25. 对RSA的实现攻击

    - 对HTTPS中PKCS1 v1.5的简化的CCA攻击 [Bleichenbacher]
    - 服务器对给定的密文来应答明文的最高有效位是否等于1 (版本号) 。攻击者发送 $c' = (2^{r})^{e}\cdot c$。如果收到 $Yes$,那么明文中第$(r+1)$最高有效位= ?
    - 防御:处理格式不正确的消息和格式正确的消息的方式应该是不可区分的。[RFC 5246]

26. PKCS #1 v2.1 (RSAES-OAEP)

    - 最优非对称加密填充(Optimal Asymmetric Encryption Padding,OAEP): 将长度 $n/2$ 的 $m$ 编码为长度 $2n$ 的消息 $\hat{m}$ 。 $G, H$ 是随机预言机。
    - RSA-OAEP在ROM下是CCA安全的。(当RO实例化后可能不安全)
    - CPA攻击下,敌手不知道$r$,则$m$被完美保护;若要知道 $r$,则必须知道$s$,这不可能。
    - CCA攻击下,无法有效进行解密查询,因为在应答前会检查明文中“00...0”。
    - 局限性:这个方案对RSA是安全的,但对其他TDP可能不是。

27. OAEP改进

    - OAEP+对所有TDP都是CCA安全的
    - SAEP+更简单填充,同样安全

28. 对RSA的实现攻击(续)

    - 计时攻击:[Kocher et al. 1997]  计算 $c^d$ 所消耗的时间可能泄漏 $d$。 (需要高解析时钟)
    - 能耗攻击:[Kocher et al. 1999] 为计算$c^d$ 智能卡消耗的能量可能泄漏$d$。
    - 防御:将密文和随机数 $r$ 绑定,解密 $r^{e}\cdot c$。
    - 密钥生成问题:(在 OpenSSL RSA 密钥生成过程中):
    - 相同的 $p$ 由多个设备产生 (源自启动时的低熵),但是不同的 $q$ (源自额外的随机性).
      - 问题: 不同设备的 $N_1,N_2$ , $\gcd(N_1,N_2) = ?$
      - 实验结果: 可分解 0.4% 的公开的HTTPS密钥。

29. 对RSA的故障攻击

    - 故障攻击:在解密过程中 $c^d\bmod N$ 发生的计算机故障可能泄漏 $d$ 。

    - 之前提到过使用中国剩余定理来加速解密:

      $ [c^d \bmod N] \leftrightarrow ([m_p \equiv c^d \pmod p],[m_q \equiv c^d \pmod q])$

    - 假设在计算 $m_q$ 时发生错误,但在计算 $m_p$ 时没有错误。
    - $m' \equiv c^d \pmod p$, $m' \not \equiv c^d \pmod q$。
    - $(m')^e \equiv c \pmod p$, $(m')^e \not \equiv c \pmod q$
    - $\gcd((m')^e-c, N)=\ ? $
    - 防御:检查输出 (但减慢 10% )。

30. 总结

    - RSA问题是TPD,但书本上RSA加密不安全,RSA-OAEP在ROM下是CCA安全的。	







================================================
FILE: notes-Chinese/8.3 DH问题与加密.md
================================================
# 8.3 DH问题与加密

1. 本节学习基于循环群上离散对数问题的DH问题及Elgamal加密方案。

2. 目录:循环群与离散对数,DH假设和应用,Elgamal加密方案。

3. 循环群(Cyclic Groups)与生成元(Generators)

   - $\mathbb{G}$ 是一个群并且一个元素 $g \in \mathbb{G}$通过运算生成一个子群 $ \langle g \rangle \overset{\text{def}}{=} \{ g^0,g^1,\dotsc,\} = \{ g^0,g^1,\dotsc, g^{i-1}\}$。
   - $g$ 的阶是最小的正整数 $i$ 令 $g^i=1$。
   - $\mathbb{G}$ 是一个循环群(cyclic group)如果 $\exists\;g$ 有阶 $m = |\mathbb{G}|$. $\langle g \rangle = \mathbb{G}$, $g$ 是 $\mathbb{G}$ 的生成元。注:循环群中存在一个元素通过指数运算可生成整个群中每个元素。
   - 例题: 乘法下的$\mathbb{Z}_6^*$, $\mathbb{Z}_7^*$,或 $\mathbb{Z}_8^*$ 是循环群吗? 找到生成元。

4. 离散对数

   - 如果 $\mathbb{G}$ 是阶为 $q$ 的循环群,那么 $\exists$ 生成元 $g \in \mathbb{G}$ 使得 $\{ g^0,g^1,\dotsc,g^{q-1}\} = \mathbb{G}$。
   -  $\forall h \in \mathbb{G}$, $\exists$ 唯一的 $x \in \mathbb{Z}_q$ 使得 $g^x = h$。
   - $x= \log_gh$ 是以$g$为底$h$的离散对数(discrete logarithm)。
   - 如果 $g^{x'}=h$, 那么 $\log_gh = [x' \bmod q]$。
   - $\log_g1=0$ 并且 $\log_g(h_1\cdot h_2) = [(\log_gh_1+\log_gh_2) \bmod q]$。

5. 离散对数算法概览

   - 给定一个生成元 $g \in \mathbb{G}$ 并且 $y \in \langle g \rangle$,求 $x$ 使得 $g^x=y$.
   - 蛮力: $\mathcal{O}(q)$, $q = \mathsf{ord}(g)$ 是 $\langle g\rangle$ 的阶。
   - Baby-step/giant-step [Shanks]: $\mathcal{O}(\sqrt{q}\cdot \mathsf{polylog}(q))$.
   - Pohlig-Hellman算法:当 $q$ 有较小因子。
   - Index calculus 法: $\mathcal{O}(\exp{(\sqrt{n\cdot \log n})})$.
   - 已知最好的算法是通用数域筛法:$\mathcal{O}(\exp(n^{1/3}\cdot(\log n)^{2/3}))$.
   - 椭圆曲线群 vs. $\mathbb{Z}_p^*$: 在保证安全性相同的同时,更高效。(1024-bit $\mathbb{Z}_p^*$ 和 132-bit 椭圆曲线都需要 $2^{66}$ 步来破解。)

6. 使用质数阶群

   - 定理:如果 $\mathbb{G}$ 是质数阶,那么 $\mathbb{G}$ 是循环群。除单位元外,所有 $g \in \mathbb{G}$  是生成元。
   - 根据拉格朗日定理,任意元素的阶都等于群的阶。
   - **拉格朗日定理**:子群阶可以整除群阶。$\langle g \rangle$ 是 $\mathbb{G}$ 子群,并且 $|\langle g \rangle| \mid |\mathbb{G}|$。
     - 思路:由一个子群可以派生覆盖了整个群的若干子集,这些子集的阶与子群相同,并且这些子集彼此不相交。
     - 设群$\mathbb{G}$的子群$H$,陪集(coset)$gH$($g$和$H$中每个元素$h$运算构成的集合)和子群$H$的阶相同。
     - 子群的任意两个陪集$g_1H$和$g_2H$。
       - 或者相同,如果$g_1^{-1}g_2 \in H$。$g_1(g_1^{-1}g_2 )h \in g_1H$,$g_2h \in g_1H$。
       - 或者没有交集,如果$g_1^{-1}g_2 \notin H$。采用反证法,如果有交集,则$g_1h_1 = g_2h_2$,$g_1^{-1}g_2 = h_1h_2^{-1} \in H$,矛盾。
     - 因此,群可以划分为任意子群的若干不相交陪集,每个陪集阶相同,群的阶就是子群的整数倍。
     - 推荐参考:https://brilliant.org/wiki/lagranges-theorem/
   - 离散对数问题在质数阶群上是最难的。 
   - 在质数阶群上找一个生成元很简单。
   - 任何非零指数在以质数阶为模下都可逆。
   - DDH问题是难题的必要条件是 $\mathsf{DH}_g(h_1,h_2)$ 与群中随机元素之间是不可区分的。在质数阶群上这基本成立。

7. 产生质数阶(子)群

   - 如果 $p$ 是质数,那么 $\mathbb{Z}^*_p$ 是乘法群。
   - $y \in \mathbb{Z}^*_p$ 是模$p$下的二次剩余(quadratic residue modulo),如果 $\exists x \in \mathbb{Z}^*_p$ 使得 $x^2 \equiv y \pmod p$
   - 例题:$\mathbb{Z}_{7}^{*}$ 下的二次剩余?
   - QR集合是一个子群(满足群条件),阶为 $(p-1)/2$,因为 $x^2 \equiv (p-x)^2 \pmod p$。
   - $p$ 是一个强质数(strong prime),如果 $p=2q+1$ 且 $q$ 是质数。
   - 强质数下的二次剩余子群是一个循环群,因为群的阶是质数。
   - 循环群生成算法:产生一个强质数$p$,阶为$q=(p-1)/2$,随机选择一个$x \in \mathbb{Z}^*_p$,得到生成元$g=x^2$,输出$p, q, g$。

8. 离散对数假设

   - 离散对数(discrete logarithm)实验 $\mathsf{DLog}_{\mathcal{A},\mathcal{G}}(n)$:
     - 运行一个群生成算法 $\mathcal{G}(1^n)$ 来产生 $(\mathbb{G},q,g)$,其中 $\mathbb{G}$ 是阶为 $q$ ( $\|q\|=n$) 的循环群,并且 $g$ 是 $\mathbb{G}$ 的生成元。
     - 挑选一个 $h \gets \mathbb{G}$. ($x' \gets \mathbb{Z}_q$ and $h := g^{x'}$)
     - 敌手 $\mathcal{A}$ 给定 $\mathbb{G}, q, g, h$,并且输出 $x \in \mathbb{Z}_q$.
     - 实验成功 $\mathsf{DLog}_{\mathcal{A},\mathcal{G}}(n) = 1$,如果 $g^x = h$, 否则 0 。
   - 定义:离散对数问题相对于群$\mathcal{G}$是难的,如果 $\forall$ ppt 算法 $\mathcal{A}$, $\exists$ $\mathsf{negl}$ 使得 $ \Pr[\mathsf{DLog}_{\mathcal{A},\mathcal{G}}(n)=1] \le \mathsf{negl}(n).$

9. DH假设

   - 计算性DH(Computational Diffie-Hellman, CDH)问题:$ \mathsf{DH}_g(h_1,h_2) \overset{\text{def}}{=} g^{\log_gh_1\cdot \log_gh_2} $
   - 判断性DH(Decisional Diffie-Hellman, DDH))问题:区分 $\mathsf{DH}_g(h_1,h_2)$ 与一个随机的群元素 $h'$.
   - 定义:DDH问题与$\mathcal{G}$相关的是难的,如果 $\forall$ ppt $\mathcal{A}$, $\exists$ $\mathsf{negl}$ 使得  $ |\Pr[\mathcal{A}(\mathbb{G},q,g,g^x,g^y,g^z)=1] - \Pr[\mathcal{A}(\mathbb{G},q,g,g^x,g^y,g^{xy})=1]|\le \mathsf{negl}(n). $
   - DL, CDH 和 DDH 的难解性:DDH 比 CDH 和 DL 容易。

10. 安全密钥交换实验

    - 密钥交换实验(key-exchange experiment) $\mathsf{KE}^{\mathsf{eav}}_{\mathcal{A},\Pi}(n)$:

      1. 双方持有安全参数 $1^n$ 执行协议 $\Pi$。 $\Pi$ 执行的结果为对话记录 (transcript) $\mathsf{trans}$ 包含双方发送的所有消息,以及各方都输出的密钥 $k$ 。
      2. 选择一个随机比特 $b \gets \{0,1\}$ 。 如果 $b=0$ 那么选择 $\hat{k} \gets \{0,1\}^n$ u.a.r;如果 $b=1$ 那么令 $\hat{k} :=k$。
      3. 敌手 $\mathcal{A}$ 给定 $\mathsf{trans}$ 和 $\hat{k}$, 并且输出一个比特 $b'$。
      4. $\mathsf{KE}^{\mathsf{eav}}_{\mathcal{A},\Pi}(n)=1$ 如果 $b'=b$, 否则 0 。

    - 定义:一个密钥交换协议 $\Pi$ 在出现窃听者攻击下是安全的,如果 $\forall$ ppt $\mathcal{A}$, $\exists$ $\mathsf{negl}$ 使得

      $ \Pr[\mathsf{KE}^{\mathsf{eav}}_{\mathcal{A},\Pi}(n) = 1] < \frac{1}{2} + \mathsf{negl}(n). $

11. DH密钥交换协议

    - $\widehat{\mathsf{KE}}^{\mathsf{eav}}_{\mathcal{A},\Pi}$ 表示一个实验,其中如果 $b=0$ ,敌手被给予 $\hat{k} \gets \mathbb{G}$。
    - 定理:如果DDH问题与$\mathcal{G}$相关是难的,那么DH 密钥交换协议 $\Pi$ 在出现窃听者时是安全的 (对应改动的实验 $\widehat{\mathsf{KE}}^{\mathsf{eav}}_{\mathcal{A},\Pi}$)。 
    - 不安全性:在主动的敌手,中间人,攻击下是不安全的 (Man-In-The-Middle)。敌手在中间与双方分别通信,通信双方无法发现在与敌手通信,敌手可以与双方分别协商出密钥。

12. 证明

    - $\Pr \left[ \widehat{\mathsf{KE}}^{\mathsf{eav}}_{\mathcal{A},\Pi} =1\right] $ $= \frac{1}{2}\cdot \Pr\left[ \widehat{\mathsf{KE}}^{\mathsf{eav}}_{\mathcal{A},\Pi} =1 | b=1\right] + \frac{1}{2}\cdot \Pr\left[ \widehat{\mathsf{KE}}^{\mathsf{eav}}_{\mathcal{A},\Pi} =1 | b=0\right] $
    - 如果 $b=1$, 那么给真密钥;否则给随机的  $g^z$.
    - $= \frac{1}{2}\cdot \Pr\left[ \mathcal{A}(g^x,g^y,g^{xy})=1 \right] + \frac{1}{2}\cdot \Pr\left[ \mathcal{A}(g^x,g^y,g^z)=0 \right] $
    - $= \frac{1}{2}\cdot \Pr\left[ \mathcal{A}(g^x,g^y,g^{xy})=1 \right] + \frac{1}{2}\cdot (1-\Pr\left[ \mathcal{A}(g^x,g^y,g^z)=1 \right]) $
    - $= \frac{1}{2} + \frac{1}{2}\cdot \left( \Pr\left[ \mathcal{A}(g^x,g^y,g^{xy})=1 \right] - \Pr\left[ \mathcal{A}(g^x,g^y,g^z)=1 \right] \right) $
    - $ \le \frac{1}{2} + \frac{1}{2}\cdot \mathsf{negl}(n) %\left| \Pr\left[ \mathcal{A}(g^x,g^y,g^{xy})=1 \right] - \Pr\left[ \mathcal{A}(g^x,g^y,g^z)=1 \right] \right| $

13. DHKE例子

    - $\mathbb{G} = \mathbb{Z}^*_{11}$ ;二次剩余循环群的阶 $q = ?$
    - 二次剩余子群 ?
    -  $g = 3$ 是生成元吗?
    - 如果 $x = 3$ 且 $y = 4$,Bob发给Alice消息是?
    - Alice 如何计算密钥?
    - Bob 如何计算密钥? 

14. 三方密钥交换

    - DH基于的KE在2轮实现三方密钥交换,Key$=g^{abc}$。
    - Joux's KE 在 1 轮实现三方密钥交换。在 bilinear map中,Key$=e(P,P)^{abc}$ 。
    - 开放问题:如何在一轮中在4方之间交换密钥?

15. 完美保密私钥加密引理

    - 引理:$\mathbb{G}$ 是有限群并且 $m\in \mathbb{G}$ 是任意元素。那么选择随机 $k \gets \mathbb{G}$ 并令 $c := m\cdot k$ ,将得到与随机选择的 $c \gets \mathbb{G}$ 相同的分布,即 $\forall g \in \mathbb{G}$: $ \Pr[m\cdot k = g] = 1/|\mathbb{G}| $。
    - 证明:$g \in \mathbb{G}$ 是任意的,那么 $\Pr[m\cdot k = g] = \Pr[k = m^{-1}\cdot g] $。由于 $k$ 均匀随机选择,选择 $k$ 的概率与一个固定元素 $m^{-1}\cdot g$ 相同,都是 $1/|\mathbb{G}$|。
    - 注:这是一种完美保密的私钥加密方案,将一个元素(明文)与另一个元素(密钥)的运算得到第三个元素(密文),与之前一个字母的移位密码是完美保密是类似的。

16. Elgamal加密方案

    - 一个算法 $\mathcal{G}$, 输入 $1^n$, 输出一个循环群 $\mathbb{G}$, 其阶为 $q$ ($\|q\| = n$), 并且生成元为 $g$。
    - 构造:
      - $\mathsf{Gen}$: 运行 $\mathcal{G}(1^n)$ 来产生 $(\mathbb{G},q,g)$。一个随机的 $x \gets \mathbb{Z}_q$ 和 $h := g^x$。 $pk = \langle \mathbb{G},q,g,h \rangle$ 并且 $sk = \langle \mathbb{G},q,g,x \rangle$。
      - $\mathsf{Enc}$: 一个随机 $y \gets \mathbb{Z}_q$ 并且输入 $\langle c_1, c_2 \rangle = \langle g^y, h^y\cdot m\rangle$。
      - $\mathsf{Dec}$: $m:=c_2/c_1^x$。
    - 定理:如果DDH问题与$\mathcal{G}$相关是难的,那么Elgamal加密方案是CPA安全。

17. Elgamal加密例子

    - 加密前首先对明文进行二进制串编码:
      - 在模一个强质数 $p = (2q+1)$ 的二次剩余子群中,
      - 一个串 $\hat{m} \in \{0,1\}^{n-1}$, $n = \|q\|$。
      - 将 $\hat{m}$ 映射到被加密的明文 $m = [(\hat{m}+1)^2 \bmod p]$。
        - 这里的加1是为了保证在消息为“0”时,明文也在这个乘法群里
      - 映射是一对一且可逆的。
    - 例子,略。

18. 证明

    - 思路:通过将DDH问题的算法$D$规约到窃听者算法$\mathcal{A}$来证明 $\Pi$ 在窃听者出现时是安全的。
    - 将 $\Pi$ 改造为 $\tilde{\Pi}$: 加密是通过随机选择的 $y \gets \mathbb{Z}_q$ 和 $z \gets \mathbb{Z}_q$ 然后输出密文:$ \langle g^y, g^z\cdot m\rangle$。
    - $\tilde{\Pi}$ 不是一个加密方案.
    - $g^y$ 独立于 $m$。
    - $g^z\cdot m$ 是独立于 $m$ 的随机元素 (之前的私钥加密引理)。
    - 实验成功概率与完美保密加密方案中是相同的。 $\Pr\left[\mathsf{PubK}^{\mathsf{eav}}_{\mathcal{A},\tilde{\Pi}}(n)=1\right] = \frac{1}{2}.$

19. 证明(续一)

    - 将DDH问题的算法$D$规约到窃听者算法$\mathcal{A}$。

20. 证明(续二)

    - 情况1: $g_3 = g^z$, 密文是 $\langle g^y, g^z\cdot m_b\rangle$.

      $ \Pr[D(g^x,g^y,g^z)=1] = \Pr\left[\mathsf{PubK}^{\mathsf{eav}}_{\mathcal{A},\tilde{\Pi}}(n)=1\right] = \frac{1}{2}. $

    - 情况2: $g_3 = g^{xy}$, 密文是 $\langle g^y, g^{xy}\cdot m_b\rangle$.

      $ \Pr[D(g^x,g^y,g^{xy})=1] = \Pr\left[\mathsf{PubK}^{\mathsf{eav}}_{\mathcal{A},\Pi}(n)=1\right] = \varepsilon(n). $

    - 由于DDH问题是难的,

      $ \mathsf{negl}(n) \ge |\Pr[D(g^x,g^y,g^z)=1] - \Pr[D(g^x,g^y,g^{xy})=1]|  =|\frac{1}{2}-\varepsilon(n)|. $

21. Elgamal加密中CCA

    - Elgamal不是CCA安全的。
    - 例题:构造明文 $m\cdot m'$ 的密文。
      - 给定 $pk=\langle g, h\rangle$, $c = \langle c_1, c_2\rangle$, $c_1=g^y$, $c_2=h^y\cdot m$,
      - 方法1:计算 $c_2' := c_2\cdot m'$, 和 $c' = \langle c_1, c_2'\rangle$. $ \frac{c_2'}{c_1^x} = ? $  
      - 方法2:计算 $c_1'' := c_1\cdot g^{y''}$, 和 $c_2'' := c_2\cdot h^{y''}\cdot m'$。
        -  $ c_1''=g^y\cdot g^{y''} = g^{y+y''}\;\text{and}\; c_2''= ? $ 
        - 所以 $c''=\langle c_1'',c_2''\rangle$ 是 $m\cdot m'$ 的密文。

22. Elgamal实现问题

    - 共享公开参数:$\mathcal{G}$ 产生参数 $\mathbb{G},q,g$。
      - 这些参数可以只产生一次并且为所有人所使用(''once-and-for-all'')。
      - 可以被多个接收者使用。
      - 每个接收者必须选择各自的保密数值 $x$ 并且发布他们自己的公钥包含 $h=g^x$。
    - 参数共享:在 Elgamal 的情况下,公开参数可以被共享。在 RSA 情况下,参数可以被共享吗?

23. 椭圆曲线密码学

    - 在椭圆曲线群上构造的离散对数问题
    - 其他密码学上的应用在1985年被提出
    - 类比离散对数,DH密钥交换,ElGamal加密和DSA,在椭圆曲线上有,ECDL,ECDHKE,ElGamal ECC,ECDSA
    - 比自然数域上更有效,密钥长度是所需蛮力搜索指数长度的二倍。
      - 二倍的原因是,离散对数问题的蛮力搜索所需指数长度是群阶指数长度的一半

24. 椭圆曲线群

    - 椭圆曲线群是在一个有限域中的一个平面代数曲线上的点之间“加法”操作
    - 在有限域中取模是关键,单位元是无穷远点

25. 在椭圆曲线点上做加法构成循环群

    - 每条直线和曲线有三个交点
      - 一条直线与曲线的切点算2次
      - 垂直线上,无穷远点计做一个点
    - 点上的加法
      - 三点成一线,三点之和为无穷远点
    - 密钥生成
      - 私钥是$d$,公钥为$dP$

26. ECDHKE的一个例子

    - 计算ECDHKE的密钥,这里枚举了生成元为(3,4)的所有指数结果
    - Alice的密钥为$a = 4$,收到(2,7)
    - Alice密钥计算是从(2,7)开始,向后数3个点(乘4=加3次)
    - Bob密钥计算是从(4,10)开始(因为$a=4$),向后数2个点(因为$b=3$)

27. 实践中的椭圆曲线密码系统

    - P256有一定风险,被揭露有NSA的后门;
    - Curve25519更安全高效,其中常数选择有充分的解释;

28. 总结

    - DHKE,ElGamal加密来自于CDH,DDH问题,后者来自于在指数阶群上的离散对数问题
    - 椭圆曲线密码学更有效并且被广泛使用





================================================
FILE: notes-Chinese/9 数字签名.md
================================================
# 9 数字签名

1. 本节学习公钥密码学中用于保护信息完整性和真实性的数字签名。

2. 目录:数字签名定义、RSA签名、来自离散对数问题的数字签名、一次签名方案、证书与公钥基础设施。

3. 数字签名概览
   
   - 数字签名(Digital signature)是一个数学方案用来证明一个数字消息的真实性/完整性。
   - 数字签名允许一个签名者(Signer)$S$ 用其自己的私钥来“签名”(sign)一个消息,并且任何知道 $S$ 的公钥的人可以验证(verify)其真实性/完整性。
   - 与MAC相比,数字签名是:
     - 公开可验证的(publicily verifiable);
     - 可转移的(transferable);
     - 不可抵赖(non-repudiation);
     - 但速度慢。
   - 问题:数字签名和手写签名的区别是什么?
   - 数字签名**不是**公钥加密的逆。
   
4. 数字签名方案词法

   - 签名 $\sigma$, 比特 $b$ 表示有效( $\mathsf{valid}$)如果 $b=1$; 无效($\mathsf{invalid}$)如果 $b=0$。
   - 密钥生成算法(Key-generation):$(pk,sk) \gets \mathsf{Gen}(1^n), |pk|,|sk| \ge n$。
   - 签名(Signing)算法:$\sigma \gets \mathsf{Sign}_{sk}(m)$。
   - 验证(Verification)算法:$b:= \mathsf{Vrfy}_{pk}(m,\sigma)$。
   - 基本正确性要求: $\mathsf{Vrfy}_{pk}(m,\mathsf{Sign}_{sk}(m)) = 1$。

5. 定义签名安全

   - 安全数字签名定义与安全MAC类似,敌手难以伪造一个“新消息”的签名。
   - 签名实验 $\mathsf{Sigforge}_{\mathcal{A},\Pi }(n)$:
     1. 挑战者生成密钥对 $(pk,sk) \gets \mathsf{Gen}(1^n)$。
     2. 敌手 $\mathcal{A}$ 给予输入 $1^n$ 以及对签名预言机的访问 $\mathsf{Sign}_{sk}(\cdot)$,然后输出 $(m,\sigma)$。 $\mathcal{Q}$ 是对预言机的查询的集合。
     3. 实验成功 $\mathsf{Sigforge}_{\mathcal{A},\Pi }(n)=1 \iff$ $\mathsf{Vrfy}_{pk}(m,\sigma)=1$ $\land$ $m \notin \mathcal{Q}$. 
   - 一个签名方案 $\Pi$ 是在适应性选择消息攻击下的存在性不可伪造(existentially unforgeable under an adaptive CMA),如果 $\forall$ PPT $\mathcal{A}$, $\exists$ $\mathsf{negl}$ 使得:  $ \Pr [\mathsf{Sigforge}_{\mathcal{A},\Pi }(n)=1] \le \mathsf{negl}(n).$
   - 问题:在MAC和数字签名中敌手能力的差别是什么?如果敌手不限制算力为PPT会如何?

6. “书本上RSA”的不安全性

   - 构造:
     - $\mathsf{Gen}$: on input $1^n$ run $\mathsf{GenRSA}(1^n)$ to obtain $N,e,d$. $pk = \langle N,e \rangle$ and $sk = \langle N,d \rangle$.
     -  $\mathsf{Sign}$: on input $sk$ and $m \in \mathbb{Z}^*_N$, $\sigma:= [m^d \bmod N]$.
     -  $\mathsf{Vrfy}$: on input $pk$ and $m \in \mathbb{Z}^*_N$, $m \overset{?}{=} [\sigma^e \bmod N]$.
   - 无消息攻击(no-message attack):
     - 选择一个任意 $\sigma \in \mathbb{Z}^*_N$ 并且计算 $m := [\sigma^e \bmod N]$。输出伪造签名 $(m,\sigma)$。
     - 例子:$pk = \left<15, 3\right>,\ \sigma = 2,\ m = ?\ m^{d} = ?$
   - 任意消息攻击(Forging a signature on an arbitrary message):为了伪造 $m$ 的签名,选择一个随机的 $m_1$,令 $m_2 := [m/m_1 \bmod N]$,查询预言机获得消息  $m_1, m_2$ 的签名 $\sigma_1, \sigma_2$ 。
     - 问题:$\sigma := [\underline{\qquad} \bmod N]$ 是 $m$ 的一个有效签名。

7. 哈希(Hashed)RSA签名

   - 思路:用哈希函数来打破消息和签名之间的的强代数关系
   - RSA-FDH 签名方案:随机预言机作为一个全域哈希(Full Domain Hash,FDH)),其定义域大小为 RSA 的模数 $N-1$。(PKCS \#1 v2.1)
   - 目前实际使用哈希RSA数字签名方案:
     - $\mathsf{Gen}$: 一个哈希函数 $H : \{0,1\}^* \to \mathbb{Z}_N^*$ 作为公钥的一部分。
     - $\mathsf{Sign}$: $\sigma := [H(m)^d \bmod N]$.
     - $\mathsf{Vrfy}$: $\sigma^e \overset{?}{=} H(m) \bmod N$.
   - 如果 $H$ 无法有效求逆,那么无消息攻击和伪造任意消息的签名都是难的。
   - 无消息攻击:敌手无法求逆
   - 任意消息攻击:$\sigma_2$ 与$\sigma$没有关系
   - 不安全性:没有已知函数 $H$ 使得哈希RSA签名是安全的。


     <div>
         <!--“哈希签名”范式
     - 将消息哈希后再签名可以实现安全的数字签名。
     - $\Pi = (\mathsf{Gen}_S, \mathsf{Sign}, \mathsf{Vrfy})$, $\Pi_H = (\mathsf{Gen}_H, H)$. 一个签名方案 $\Pi'$:
       - $\mathsf{Gen}'$: 输入 $1^n$ 运行 $\mathsf{Gen}_S(1^n)$ 来得到 $(pk,sk)$, 并且运行 $\mathsf{Gen}_H(1^n)$ 来得到 $s$。 公钥是 $pk'=\langle pk,s\rangle$ 并且私钥是 $sk' = \langle sk,s\rangle$。
       - $\mathsf{Sign}'$: 输入 $sk'$ 并且 $m \in \{0,1\}^*$, $\sigma \gets \mathsf{Sign}_{sk}(H^s(m))$。
       - $\mathsf{Vrfy}'$: 输入 $pk'$, $m \in \{0,1\}^*$ 并且 $\sigma$, 输出 1 $\iff$ $\mathsf{Vrfy}_{pk}(H^s(m),\sigma)=1$。
     - 定理:如果 $\Pi$ 是在适应性CMA下的存在性不可伪造,并且 $\Pi_H$ 是抗碰撞,那么构造是适应性CMA下的存在性不可伪造。
     - 证明:敌手无法实施之前的“无消息攻击”和“伪造任意消息签名攻击”。敌手的成功需要发现哈希碰撞,或者针对$\Pi$伪造签名。-->
     </div>

8. Schnorr签名概览

   - Schnorr签名展现了签名,身份识别和零知识证明之间的联系
   - 该方案是Schnorr身份识别协议的非交互版本,而后者是一个对离散对数问题的解的交互式零知识证明
   - 安全:在ROM下和离散对数难题假设下,将Fiat-Shamir变换应用于Schnorr身份识别协议
   - 应用于多重签名,门限签名和盲签名,这些技术被广泛应用于密码学货币

     <div>
         <!--身份认证(Identification)方案
     - 下面学习**Schnorr**身份认证方案,该方案可以用于构造基于离散对数问题的**Schnorr**数字签名方案。
     - 身份认证(identification)方案 $\Pi = (\mathsf{Gen}, \mathcal{P}_1, \mathcal{P}_2, \mathcal{V})$ 是一个在证明者(prover)和验证者(verifier)之间的三轮协议。其中,证明者运行$\mathcal{P}_1, \mathcal{P}_2$两个算法,验证者运行$\mathcal{V}$算法。
     - 证明者说服验证者其是一个公钥所对应的私钥的持有者,通过“知道什么”来证明自己的身份。
     - 敌手能够窃听并且可以通过作为一个验证者来访问一个预言机 $\mathsf{Trans}_{sk}$ 来获得信息 $(I, r, s)$ ,即真的证明者与敌手(作为验证者)间执行身份认证协议。
     - 公开可验证:除了证明者和验证者之外,其他人也可以根据传递的消息来验证证明者的身份!
     - 身份认证协议:
       1. 证明者生成$(I, \mathsf{st})\gets \mathcal{P}_1(sk)$,并将$I$发送给验证者。*注:这里不能泄漏关于私钥的信息,并且这个$I$可用于保护私钥。*
       2. 验证者生成 $r \gets \Omega_{pk}$,并将$r$发送给证明者。*注:挑战信息不能被证明者预知,否则证明者有可能在不知道私钥的情况下伪装自己*
       3. 证明者生成 $s := \mathcal{P}_2(sk, \mathsf{st}, r)$,并将$s$发送给证明者。*注:证明者对挑战作出响应,必然要用到自己的私钥,但不能让验证者推断出私钥*
       4. 验证者验证$\mathcal{V}(pk, r, s) \overset{?}{=} I$。*注:确定只有私钥的持有者能通过验证*
     - 其中,$\mathsf{st}$ 表示证明者维护的状态信息。
     身份认证方案安全定义
     - 思路:敌手能够作为验证者进行实验,但仍不能自己伪装成证明者。
     - 身份认证实验 $\mathsf{Ident}_{\mathcal{A},\Pi }(n)$:
       1.  证明者生成密钥对,$(pk,sk) \gets \mathsf{Gen}(1^n)$.
       2.  敌手 $\mathcal{A}$ 给予输入 $1^n$ 和对 $\mathsf{Trans}_{sk}(\cdot)$ 的预言机访问,输出一个消息 $I$。
       3.  挑战者挑选一个均匀的挑战 $r$ 并将其发送给 $\mathcal{A}$,然后敌手 $\mathcal{A}$ 输出 $s$。 ($\mathcal{A}$ 可以继续查询预言机。)
       4.  实验成功如果验证成功,$\mathsf{Ident}_{\mathcal{A},\Pi }(n) = 1 \iff \mathcal{V}(pk, r, s) \overset{?}{=} I$。 
     - 定义:一个身份认证方案 $\Pi = (\mathsf{Gen}, \mathcal{P}_1, \mathcal{P}_2, \mathcal{V})$ 是安全的,如果 $\forall$ PPT $\mathcal{A}$, $\exists$ $\mathsf{negl}$ 使得:
       $ \Pr [\mathsf{Ident}_{\mathcal{A},\Pi }(n) = 1] \le \mathsf{negl}(n). $-->
     </div>

9. Schnorr身份认证方案

   - 证明者公开地证明其知道一个离散对数问题的解,通过一个三轮的西格玛协议
     1. 证明者持有一个离散对数问题$g^x = y$的私钥部分$x$,生成$k \gets \mathbb{Z}_q$; $I := g^k$,并将$I$发送给验证者。
        - *注:这个$k$就是 $\mathsf{st}$, 后面用来隐藏私钥$x$;*
     2. 验证者生成 $r \gets \mathbb{Z}_q$,并将$r$发送给证明者。
        - *注:随机的$r$不能被预测,并且在$I$之后产生;原因见下面* 
     3. 证明者生成 $s := [rx + k \mod q]$,并将$s$发送给证明者。
        - *注:用$x$生成应答,并用$k$隐藏$x$;*
     4. 验证者验证$\mathcal{V}(pk, r, s) = g^s \cdot y^{-r} \overset{?}{=} I$。*注:$g^s \cdot y^{-r} = g^{rx+k} g^{-rx} = g^k$*
   - 这里$r$不能被预测,且在收到$I$之后被生成。否则,不知道$x$的敌手可以假装知道$x$。方法是随便产生一个$s$,在第一步将$I = g^s \cdot y^{-r}$发送给验证者,并且用$s$做第3步响应。
   - 问题:为什么一个更简单的协议不安全?
     - 验证者生成一个随机数$r$,并计算$g^r$将其发送给证明者;证明者计算$g^{rx}$并公开结果;验证者通过比较$y^r \overset{?}{=} g^{rx}$来判断证明者是否知道$x$。这种身份认证方案安全吗?
     - 答案是不安全。这个协议可以用DH密钥交换来理解。Alice向Bob证明她知道$x$的过程,就是在密钥交换完成后,Alice公开密钥$g^{xr}$。

10. Schnorr身份认证方案证明

    - 定理:如果离散对数是难的,那么Schnorr身份认证方案是安全的。
    - 思路:如果攻击身份认证方案的敌手可以成功使得 $g^s \cdot y^{-r} = I$,那么离散对数问题可以被解决。敌手将实验过程“倒带”,在两次实验中使用相同的$I$。
    - 证明:将求$y$的逆的算法 $\mathcal{A}'$ 规约到攻击Schnorr方案的$\mathcal{A}$:
      -  $\mathcal{A}'$ 作为验证者并运行 $\mathcal{A}$ 作为证明者,$\mathcal{A}'$回答$\mathcal{A}$的查询。
      - 当 $\mathcal{A}$ 输出 $I$, $\mathcal{A}'$ 选择 $r_1 \in \mathbb{Z}_q$ 并且发送给 $\mathcal{A}$,后者以 $s_1$ 应答。
      - 再一次运行 $\mathcal{A}$ ,将 $r_2 \in \mathbb{Z}_q$ 发送给 $\mathcal{A}$ ,后者其应答 $s_2$。
      - 如果 $g^{s_1} \cdot h^{-r_1} = I$ 并且 $g^{s_2} \cdot h^{-r_2} = I$ 并且 $r_1 \neq r_2$ 那么输出 $x = [ (s_1 - s_2)\cdot (r_1 - r_2)^{-1} \mod q]$,否则输出空。

    <div>
        <!--Fiat-Shamir变换
    - Fiat-Shamir变换通过签名者自己运行身份认证协议来构造一个(非交互)的签名方案。如果签名可以被伪造,那么意味着身份可以伪造。
    - 令 $\Pi = (\mathsf{Gen}_{\mathsf{id}}, \mathcal{P}_1, \mathcal{P}_2, \mathcal{V})$ 为一个身份识别方案,签名方案构造如下:
      - $\mathsf{Gen}$: $(pk, sk) \gets \mathsf{Gen}_{\mathsf{id}}$。一个函数 $H : \{0,1\}^* \to \Omega_{pk}$ (挑战集).
      - $\mathsf{Sign}$: 输入 $sk$ 并且 $m \in \{0,1\}^*$;
        - 计算 $(I, \mathsf{st}) \gets \mathcal{P}_1(sk)$
        - 计算 $r := H(I, m)$    *注:此处引入随机预言机来得到消息的指纹*
        - 计算 $s := \mathcal{P}_2(sk, \mathsf{st}, r)$ *注:消息的指纹和私钥一起生成签名*
        - 输出签名 $r, s$。*注:签名是概率性的*
      - $\mathsf{Vrfy}$: 计算 $I := \mathcal{V}(pk, r, s)$,并且输出 $1 \iff H(I, m) \overset{?}{=} r$。*注:*
    - 定理:如果 $\Pi$ 是一个安全身份认证方案,并且 $H$ 是随机预言机,那么 Fiat-Shamir 变换会得到一个安全签名方案。
    - 证明:大致思路是,$r$与消息通过$H$绑定,改变消息会得到一个新的随机的$r$。将身份认证方案的敌手算法规约到签名方案的敌手算法。如果签名可以被伪造,那么意味着身份可以伪造。-->
    </div>

11. Schnorr签名方案

    - 根据Fiat-shamir变换,可以用Schnorr身份认证方案来构造数字签名方案,签名者自己运行身份识别协议
      - $\mathsf{Gen}$: $(\mathcal{G}, q, g) \gets \mathcal{G}(1^n)$。选择 $x \in \mathbb{Z}_q$ 并且令 $y := g^x$。私钥为 $x$ 而公钥为 $(\mathcal{G}, q, g, y)$。一个函数 $H : \{0,1\}^* \to \mathbb{Z}_q$。
      - $\mathsf{Sign}$: 输入 $x$ 和 $m \in \{0,1\}^*$,执行以下操作
        - 计算 $I := g^k$, 其中一个均匀的 $k \in \mathbb{Z}_q$;
        - 计算 $r := H(I, m)$;
        - 计算 $s := [ rx + k \mod q]$;
        - 输出签名 $(r, s)$。
      - $\mathsf{Vrfy}$: 计算 $I := g^s \cdot y^{-r}$ 并且输出 $1 \iff H(I, m) \overset{?}{=} r$。

12. DSS/DSA(数字签名标准/算法)

    - NIST从1994年到2013年颁布的数字签名标准(Digital Signature Standard,DSS) 使用数字签名算法(Digital Signature Algorithm,DSA),该算法是一个ElGamal签名方案的变体。DSS中还包括椭圆曲线数字签名算法(Elliptic Curve Digital Signature Algorithm,ECDSA)和 RSA签名算法。
    - 这两种算法基于相同的算法抽象:基于身份认证方案的签名方案。
    - 构造:
      -  $\mathsf{Gen}$: $(\mathbb{G},q,g) \gets \mathcal{G}$. 两个哈希函数 $H, F : \{0,1\}^* \to \mathbb{Z}_q$. 
        - $x \gets \mathbb{Z}_q$ 和 $y:= g^x $.
        - $pk = \langle \mathbb{G},q,g,y,H,F\rangle$. $sk=\langle \mathbb{G},q,g,x,H,F\rangle$.
      - $\mathsf{Sign}$: $k\gets \mathbb{Z}^*_q$ 并且 $r:= F(g^k) $, $s:= (H(m)+xr)\cdot k^{-1}$. 输出 $(r,s)$.
      - $\mathsf{Vrfy}$: 输出 $1 \iff r \overset{?}{=} F(g^{H(m)\cdot s^{-1}}y^{r\cdot s^{-1}}).$
    - DSA中验证的正确性?

13. DSS/DSA安全性

    - 不安全性:DSS的安全性依赖于离散对数问题的难解性,尚未有基于离散对数假设的DSS安全性证明。
    - $k$ 的熵、保密和唯一性是安全性的关键。
    - 情况1:如果$k$是可预测的,那么$x$将泄漏,因为$s:= (H(m)+xr)\cdot k^{-1}$中只有$x$是未知的。
    - 情况2:如果同一个$k$被用于同一私钥下的两个不同签名,那么$k$和$x$都将泄漏。问题:如何做?
      - 该攻击曾在2010年用于对Sony PlayStation (PS3) 提取私钥。

14. 一次签名(OTS)

    - 下面学习不基于数论假设,而是基于哈希函数来构造安全的数字签名方案。

    - 一次签名(One-Time Signature,OTS): 在一种较弱的攻击场景下,一个秘密只用于一个消息签名。

    - 模拟一次签名场景,敌手最多只允许查询一次签名预言机,之后需要给出新消息和签名。

    - OTS实验 $\mathsf{Sigforge}_{\mathcal{A},\Pi }^{\text{1-time}}(n)$:

      -  $(pk,sk) \gets \mathsf{Gen}(1^n)$.
      -  $\mathcal{A}$ 输入 $1^n$ 和对 $\mathsf{Sign}_{sk}(\cdot)$的一次查询 $m'$ ,并且输出 $(m,\sigma)$, $m \neq m'$。
      - $\mathsf{Sigforge}_{\mathcal{A},\Pi }^{\text{1-time}}(n)=1 \iff \mathsf{Vrfy}_{pk}(m,\sigma)=1$. 

    - 一个签名方案 $\Pi$ 在单个消息攻击下是存在性不可伪造的,如果 $\forall$ PPT $\mathcal{A}$,$\exists$ $\mathsf{negl}$ 使得:

      $ \Pr [\mathsf{Sigforge}_{\mathcal{A},\Pi }^{\text{1-time}}(n)=1] \le \mathsf{negl}(n).$

15. Lamport的OTS (1979)

    - 思路:从单向函数构造OTS;每个比特为一个映射 。
    - 构造:
      - $f$ 是一个OWF。抗碰撞哈希函数也是属于OWF。
      - $\mathsf{Gen}$: 输入 $1^n$,对于 $i \in \{1,\dotsc, \ell\}$:
        - 选择随机的两个函数输入 $x_{i,0}, x_{i,1} \gets \{0,1\}^n$;
        - 计算 $y_{i,0} := f(x_{i,0})$ 和 $y_{i,1} := f(x_{i,1})$。
        - 构成2个2xn的矩阵,$x_{i,j}$构成的矩阵是私钥,$y_{i,j}$构成的矩阵是公钥。
      - $\mathsf{Sign}$: $m = m_1\cdots m_{\ell}$, 输出 $\sigma = (x_{1,m_1},\dotsc,x_{\ell,m_{\ell}})$。根据消息中每个比特的值(0或1)来选择$x_{i,j}$,得到的一个向量为签名。
      - $\mathsf{Vrfy}$: $\sigma = (x_1,\dotsc,x_{\ell})$,输出 $1 \iff$  对于所有 $i$,$f(x_i) = y_{i,m_i}$。对消息的每个比特
    - 定理:如果 $f$ 是 OWF,$\Pi$ 是长度为 $\ell$ 的消息的OTS。

16. Lamport的OTS例子

    - 略

17. Lamport的OTS安全性证明

    - 思路:如果 $m \neq m'$,那么 $\exists i^*, m_{i*} = b^* \neq m'_{i*}$。因此,为了伪造一个消息至少要对一个$y_{i^*,b^*}$求逆。 
    - 证明:将对$y$求逆的 $\mathcal{I}$ 算法规约到攻击 $\Pi$的 $\mathcal{A}$ 算法:
      - $\mathcal{I}$算法构造 $pk$:选择 $i^* \gets \{1,\dotsc,\ell\}$ 并且 $b^* \gets \{0,1\}$,令 $y_{i^*,b^*} := y$。对于 $i \neq i^*$ $y_{i,b} := f(x_{i,b})$;
        - 在公钥中随机选择一个位置$(i^*,b^*)$,将待求逆的$y$放在该位置;对于其它位置,正常构造公私钥对。
      - $\mathcal{A}$算法查询 $m'$:如果 $m_{i_*}' = b^*$,则停止。否则,返回 $\sigma = (x_{1,m'_1},\dots,x_{\ell,m'_{\ell}})$;
        - 如果$\mathcal{A}$ 的查询正好落在位置$(i^*,b^*)$,而该位置的$x_{i^*,b^*}$本应该是$y$对应的$x$,是未知的,终止实验。否则,正常返回签名。
      - 当 $\mathcal{A}$ 输出 $(m,\sigma)$,$\sigma=(x_1,\dotsc,x_{\ell})$,如果 $\mathcal{A}$ 在 $(i^*,b^*)$输出了一个伪造的值,并且有 $\mathsf{Vrfy}_{pk}(m,\sigma)=1$ 且 $m_{i^*} =b^* \neq m'_{i^*}$,那么输出 $x_{i^*,b^*}$;
        - 通过验证并且在$y$对应位置上输出签名,说明 $\mathcal{A}$ 输出的签名满足 $f(x_{i,m_i}) = y_{i,m_i}$。
      - $ \Pr[\mathcal{I}\;\; \text{succeeds} ] \ge \frac{1}{2\ell}\Pr[\mathcal{A}\;\; \text{succeeds}] $ 
        - 这是因为位置正好在特定位置满足条件的概率是$\frac{1}{2\ell}$.

18. 有状态签名方案

    - 思路:为了对任意数量的消息签名,可以从旧状态中获得新的密钥并以此来实现和OTS一样的效果
    - 定义:有状态签名方案(Stateful signature scheme)
      - 密钥生成算法:$(pk,sk,s_0) \gets \mathsf{Gen}(1^n)$。 $s_0$ 是初始状态。
      - 签名算法:$(\sigma,s_i) \gets \mathsf{Sign}_{sk,s_{i-1}}(m)$。
      - 验证算法:$b:= \mathsf{Vrfy}_{pk}(m,\sigma)$.
    - 一个简单的有状态OTS签名方案:独立产生 $(pk_i,sk_i)$,令 $pk := (pk_1,\dotsc,pk_{\ell})$ 并且 $sk := (sk_1,\dotsc,sk_{\ell})$。 从状态 $1$ 开始,用 $sk_s$ 签第 $s$ 个消息,用 $pk_s$ 来验证,并且更新状态到 $s+1$。
      - 安全性:每个密钥只签了一个消息。
      - 弱点:消息数量上届 $\ell$ 必须事先确定。

19. 链式签名

    - 思路:按需随时产生密钥并且对密钥链签名,解决消息数量有上限的问题。
    - 最初使用一个公钥 $pk_1$,用 $sk_i$ 来对每个当前消息 $m_i$ 和下一个公钥 $pk_{i+1}$ 签名 : $\sigma_i \gets \mathsf{Sign}_{sk_i}(m_i\| pk_{i+1}),$ 输出 $\langle pk_{i+1},\sigma_i \rangle$, 并且用 $pk_i$ 验证 $\sigma_i$,签名 $(pk_{i+1},\sigma_i,\{m_j,pk_{j+1},\sigma_j\}^{i-1}_{j=1})$。
    - 弱点:仍然有状态,包括之前签过的所有消息,效率低,需要揭示之前所有消息才能验证当前消息。

20. 树式签名

    - 思路:减少所需维护状态,构造一个密钥树,树上的一个分支是为每个消息生成一个密钥链并且对这个链签名。
    - 根据按消息中的比特构造一个二叉树,根为 $\varepsilon$ (空串),叶子为消息 $m$,并且内部节点为密钥对 $(pk_w,sk_w)$,其中 $w$ 是 $m$ 的前缀。
    - 每个节点 $pk_w$ 负责对其子节点公钥 $pk_{w0}\| pk_{w1}$ 或消息 $w$ 来签名。

21. 无状态签名

    - 思路:用确定的随机性来模拟树的状态。
    - 使用PRF $F$ 和两个密钥 $k,k'$ 来产生 $pk_w,sk_w$:
      1. 计算 $r_w := F_k(w)$。
      2. 计算 $(pk_w,sk_w) := \mathsf{Gen}(1^n;r_w)$, 用 $r_w$ 作为随机硬币。
      3. $k'$ 用于产生 $r_w'$ ,后者在产生签名 $\sigma_w$ 时使用。
    - 如果 OWF 存在,那么 $\exists$ OTS (对于任意长度消息)。
    - 定理:如果 OWF 存在,那么 $\exists$ (无状态) 安全签名方案。

22. 证书

    - 从之前的方案中可以观察到,一种安全地分发公钥的方法是对该公钥做一个数字签名。
    - 对一个公钥的数字签名被称为,数字证书(Certificate);专门签发数字证书的机构被称为,证书权威机构(Certificate Authority,CA),CA是一个可信的第三方,其公钥($pk_C$)被所有相信CA的主体所持有。
    - 一个数字证书 $ \mathsf{cert}_{C\to B} \overset{\text{def}}{=} \mathsf{Sign}_{sk_C}(\text{`Bob's key is } pk_B\text{'})$,表示 CA 用其私钥($sk_C$)给一个主体 Bob 签发的数字证书,其中消息内容包括:主体的身份(Bob)和该主体所持的公钥($pk_B$)。其本质是**绑定一个身份和一个公钥。**
    - Bob 将自己的公钥提交给 CA,然后收到证书,最后将自己的公钥和证书一起发送给通信的另一个方。
    - 一个问题是 CA 的公钥是如何分发的?通常随应用程序一起分发,例如浏览器中内置了全世界约170个左右的CA的公钥。在DNSSEC中,递归服务器软件中内置了DNS根的公钥。
    - 另一个问题:CA如何知道收到的公钥是否是Bob的?需要采用其它渠道,例如一个CA “Let's Encrypt” 通过证明域名的所有权来识别证书申请者的身份。

23. 公钥基础设施(PKI)

    - 单一 CA: 被所有人信任。
      - 
Download .txt
gitextract_ii66oz9u/

├── .gitignore
├── LICENSE.txt
├── Makefile
├── README.md
├── homework/
│   ├── amspset.cls
│   ├── hw1.tex
│   ├── hw2.tex
│   ├── hw3.tex
│   ├── hw4.tex
│   ├── hw5.tex
│   └── hw6.tex
├── lab/
│   ├── lab1.docx
│   ├── lab2.docx
│   ├── lab3.docx
│   └── lab4.docx
├── misc/
│   ├── cryptographers/
│   │   └── cryptographers.key
│   ├── gallery/
│   │   ├── frame.tex
│   │   ├── gallery.tex
│   │   └── gen-gallery.sh
│   └── tikztest/
│       └── tikz-test.tex
├── notes-Chinese/
│   ├── 0 课程介绍.md
│   ├── 1 导论.md
│   ├── 10 密码学协议动物园.md
│   ├── 2 完美保密.md
│   ├── 3.1 私钥加密与伪随机性-第一部分.md
│   ├── 3.2 私钥加密与伪随机性 第二部分.md
│   ├── 4 伪随机排列的实践构造-块密码.md
│   ├── 5 伪随机对象的理论构造.md
│   ├── 6 消息认证码与抗碰撞哈希函数.md
│   ├── 7 CCA安全与认证加密.md
│   ├── 8.1 公钥加密理论.md
│   ├── 8.2 RSA问题与加密.md
│   ├── 8.3 DH问题与加密.md
│   ├── 9 数字签名.md
│   └── README.md
├── source/
│   ├── 0intro.tex
│   ├── 10protocols.tex
│   ├── 11summary.tex
│   ├── 12backup.tex
│   ├── 1introduction.tex
│   ├── 2perfectlysecret.tex
│   ├── 3.1privatekey.tex
│   ├── 3.2privatekey.tex
│   ├── 4blockcipher.tex
│   ├── 5owf.tex
│   ├── 6mac-crhf.tex
│   ├── 7cca-ae.tex
│   ├── 8.1pubkey.tex
│   ├── 8.2RSA.tex
│   ├── 8.3DH.tex
│   ├── 9digital-sig.tex
│   ├── crush-course.tex
│   └── header/
│       ├── frame.tex
│       ├── handout.tex
│       └── main.tex
└── tikz/
    ├── 1outof2.tex
    ├── 3ballot.tex
    ├── 3parties-DHKE.tex
    ├── 3parties-JOUX.tex
    ├── CBC-MAC.tex
    ├── CBC-small.tex
    ├── CBC.tex
    ├── CCA-PKCS.tex
    ├── CCA.tex
    ├── CMAC.tex
    ├── CTR.tex
    ├── DESkey.tex
    ├── DHkey.tex
    ├── Davies-Meyer.tex
    ├── ECB.tex
    ├── ElGamal.tex
    ├── HMAC.tex
    ├── IBE.tex
    ├── KDC.tex
    ├── MDtransform.tex
    ├── MS-PPTP.tex
    ├── Miyaguchi-Preneel.tex
    ├── NMAC.tex
    ├── OAEP-plus.tex
    ├── OAEP.tex
    ├── OFB.tex
    ├── OWF.tex
    ├── SAEP-plus.tex
    ├── SIV-CTR.tex
    ├── TDES.tex
    ├── VCBC-MAC.tex
    ├── alice.tex
    ├── attack-spn.tex
    ├── authentication.tex
    ├── baby-giant.tex
    ├── bilinear-map.tex
    ├── birthdayattack.tex
    ├── blindsignature.tex
    ├── cPRF.tex
    ├── certificates.tex
    ├── chain-sig.tex
    ├── cipher-stealing.tex
    ├── coinflipping.tex
    ├── collision.tex
    ├── combination.tex
    ├── compute-sec.tex
    ├── constructD.tex
    ├── des.tex
    ├── desx.tex
    ├── differential.tex
    ├── digitalsignature.tex
    ├── dining.tex
    ├── doubleE.tex
    ├── ePRG.tex
    ├── ecdhke.tex
    ├── elgamal-con.tex
    ├── ellipticcurve.tex
    ├── encryptionwithpf.tex
    ├── encryptionwithpg.tex
    ├── feistel-prp.tex
    ├── feistel.tex
    ├── hash.tex
    ├── hcp.tex
    ├── hs-reduce-1.tex
    ├── hs-reduce-2.tex
    ├── hybrid-enc-proof.tex
    ├── hybrid-encrypt.tex
    ├── hybrideg.tex
    ├── hybridproof.tex
    ├── identification-schnorr.tex
    ├── identification.tex
    ├── integrity.tex
    ├── interlock.tex
    ├── keyed-func.tex
    ├── linear.tex
    ├── mac.tex
    ├── macforge-exp.tex
    ├── macwithprf.tex
    ├── man-in-middle.tex
    ├── meet-in-middle.tex
    ├── multiple-enc-exp.tex
    ├── outfile
    ├── owff.tex
    ├── owfover.tex
    ├── padding-oracle-lastbyte.tex
    ├── padding-oracle-null.tex
    ├── padding-oracle.tex
    ├── pgfD.tex
    ├── pgfMAC.tex
    ├── pnp.tex
    ├── prg-distinguisher.tex
    ├── prg-sparse.tex
    ├── pri-cpa-exp.tex
    ├── pri-eav-exp.tex
    ├── private-key.tex
    ├── public-key.tex
    ├── qr-qnr.tex
    ├── rabinOT.tex
    ├── reduction-prg.tex
    ├── reduction.tex
    ├── schnorr-signature.tex
    ├── schnorr.tex
    ├── spn.tex
    ├── ssl.tex
    ├── suf-mac.tex
    ├── synchronizedmode.tex
    ├── tdp-cca1.tex
    ├── tdp-cca2.tex
    ├── tdp-cpa.tex
    ├── tdp-pk.tex
    ├── threepass.tex
    ├── tls13hs.tex
    ├── toy-OT.tex
    ├── transmission.tex
    ├── trapdoor.tex
    ├── tree-sig.tex
    ├── worldofpk.tex
    ├── zkp-commitment.tex
    ├── zkp-hanmilton.tex
    ├── zkp-rsa.tex
    └── zkp.tex
Condensed preview — 176 files, each showing path, character count, and a content snippet. Download the .json file or copy for the full structured content (915K chars).
[
  {
    "path": ".gitignore",
    "chars": 108,
    "preview": "*.nav\n*.out\n*.snm\n*.toc\n*.log\n*.aux\n*.swp\n*.vrb\n*.gz\n*.DS_Store\n.vscode/*\n*.fls\n*.fdb_latexmk\n*tikz-test.pdf"
  },
  {
    "path": "LICENSE.txt",
    "chars": 20127,
    "preview": "Attribution-ShareAlike 4.0 International\n\n=======================================================================\n\nCreat"
  },
  {
    "path": "Makefile",
    "chars": 727,
    "preview": "\nTEX = pdflatex\n\nOPT= -output-directory pdf\n\nBUILD = ${TEX} ${OPT} $< ; ${TEX} ${OPT} $< ;\n\nSRCS := $(wildcard source/*."
  },
  {
    "path": "README.md",
    "chars": 1426,
    "preview": "## Cryptography Course Slides\n\n[![made-with-latex](https://img.shields.io/badge/Made%20with-LaTeX-1f425f.svg)](https://w"
  },
  {
    "path": "homework/amspset.cls",
    "chars": 4268,
    "preview": "% AMS Dept HW class file\n% v0.05 by Eric Harley, 07 Sept 2005\n% forked from HMC Math dept HW class file v0.04, 07 Sept 2"
  },
  {
    "path": "homework/hw1.tex",
    "chars": 2430,
    "preview": "\\documentclass[11pt]{article}\n\n% set 1-inch margins in the document\n\\usepackage[margin=1in]{geometry}\n\\usepackage{amsthm"
  },
  {
    "path": "homework/hw2.tex",
    "chars": 2373,
    "preview": "\\documentclass[11pt]{article}\n\n% set 1-inch margins in the document\n\\usepackage[margin=1in]{geometry}\n\\usepackage{amsthm"
  },
  {
    "path": "homework/hw3.tex",
    "chars": 1843,
    "preview": "\\documentclass[11pt]{article}\n\n% set 1-inch margins in the document\n\\usepackage[margin=1in]{geometry}\n\\usepackage{amsthm"
  },
  {
    "path": "homework/hw4.tex",
    "chars": 3007,
    "preview": "\\documentclass[11pt]{article}\n\n% set 1-inch margins in the document\n\\usepackage[margin=1in]{geometry}\n\\usepackage{amsthm"
  },
  {
    "path": "homework/hw5.tex",
    "chars": 4441,
    "preview": "\\documentclass[11pt]{article}\n\n% set 1-inch margins in the document\n\\usepackage[margin=1in]{geometry}\n\\usepackage{amsthm"
  },
  {
    "path": "homework/hw6.tex",
    "chars": 1665,
    "preview": "\\documentclass[11pt]{article}\n\n% set 1-inch margins in the document\n\\usepackage[margin=1in]{geometry}\n\\usepackage{amsthm"
  },
  {
    "path": "misc/gallery/frame.tex",
    "chars": 3896,
    "preview": "% presentation\n\n%\\documentclass{beamer}\n%\\usetheme[height=7mm]{Rochester}\n%\\usecolortheme{rose}\n\n% handout\n\n\\documentcla"
  },
  {
    "path": "misc/gallery/gallery.tex",
    "chars": 15921,
    "preview": "\\input{misc/gallery/frame.tex}\n\\begin{document}\n\\begin{frame} \\frametitle{1outof2}\n\\begin{figure}\n\\begin{center}\n\\input{"
  },
  {
    "path": "misc/gallery/gen-gallery.sh",
    "chars": 342,
    "preview": "echo '\\input{misc/gallery/frame.tex}'\necho '\\\\begin{document}'\nfor a in `ls -1 tikz/*.tex`\ndo a=`echo $a | sed 's/^.*\\/\\"
  },
  {
    "path": "misc/tikztest/tikz-test.tex",
    "chars": 231,
    "preview": "\\input{../../source/header/main.tex}\n%\\input{header/main.tex}\n\\begin{document}\n\\begin{frame}\\frametitle{Tikz Test}\n\\begi"
  },
  {
    "path": "notes-Chinese/0 课程介绍.md",
    "chars": 1275,
    "preview": "# 课程介绍\n\n本课程讲义需结合幻灯片和参考教材学习。中文讲义大体可以看作是英文幻灯片的翻译,其区别主要在于:\n\n- 中文讲义不包含英文幻灯片中图片\n- 中文讲义对英文幻灯片中一小部分内容只有简单的占位性的文字\n- 中文讲义对英文幻灯片的部"
  },
  {
    "path": "notes-Chinese/1 导论.md",
    "chars": 7863,
    "preview": "# 导论\n\n1. 在本节课程中,我们学习密码学导论,包括对称加密基本概念、古典密码学和现代密码学基本原则。我们通过学习这些可在直觉上被理解的,古典的加密方案和破解方法,建立对加密和安全的直观概念,从而理解现代密码学基本原则为什么是合理的和必"
  },
  {
    "path": "notes-Chinese/10 密码学协议动物园.md",
    "chars": 9280,
    "preview": "# 10 密码学协议动物园\n\n1. 本节学习密码学协议。这些协议以之前学习的密码学知识为基础实现了一些“奇妙”的事情!\n2. 动物园地图\n   - 密码学协议种类繁多,应用广泛。\n3. 目录:略\n4. 协议:\n   - **通信协议**是为"
  },
  {
    "path": "notes-Chinese/2 完美保密.md",
    "chars": 5231,
    "preview": "# 完美保密加密\n\n1. 在本节课程中,我们学习信息论意义上的安全——完美保密。完美保密的安全在信息论上是无需前提假设的,但其存在实践上的局限性,是完美中的不完美。本节将学习若干“等价”的完美保密定义,从中体会看似不同的定义却存在相同的本质"
  },
  {
    "path": "notes-Chinese/3.1 私钥加密与伪随机性-第一部分.md",
    "chars": 8612,
    "preview": "# 私钥加密与伪随机性 第一部分\n\n1. 在本节课程中,我们学习计算安全下的私钥加密和伪随机性的第一部分。我们会学习一个完整的现代密码学研究过程,从定义到假设,再到一个密码学方案,最后使用规约法来证明其安全性。\n\n2. 目录:密码学的计算方"
  },
  {
    "path": "notes-Chinese/3.2 私钥加密与伪随机性 第二部分.md",
    "chars": 12834,
    "preview": "# 私钥加密与伪随机性 第二部分\n\n1. 本节课学习另外两种私钥加密安全理论:选择明文攻击(CPA)下不可区分性、选择密文攻击(CCA)下不可区分性以及相关的密码学原语、假设、构造和证明。这些攻击更好的刻画了现实世界中敌手的能力,相应的密码"
  },
  {
    "path": "notes-Chinese/4 伪随机排列的实践构造-块密码.md",
    "chars": 10076,
    "preview": "# 4 伪随机排列实践构造(块密码/分组密码)\n\n1. 本节学习如何设计一个PRP(伪随机排列)。通过一个经典的加密方案DES来学习PRP的构造和相关安全分析。此外,简要介绍当前广泛使用的AES。\n\n2. 目录:替换-置换网络、Feiste"
  },
  {
    "path": "notes-Chinese/5 伪随机对象的理论构造.md",
    "chars": 4463,
    "preview": "# 5 伪随机对象的理论构造\n\n1. 本节学习如何设计基于单向函数存在的假设从理论上构造PRG、PRF、PRP这三个伪随机对象。\n\n2. 目录:单向函数(One-Way Function),从OWF到PRP\n\n3. 概览\n   - 现代密码"
  },
  {
    "path": "notes-Chinese/6 消息认证码与抗碰撞哈希函数.md",
    "chars": 13898,
    "preview": "# 6 消息认证码与抗碰撞哈希函数\n\n1. 本节学习用于保护信息的完整性和真实性的消息认证码(MAC)和抗碰撞的哈希函数(CRHF)。\n\n2. 目录:MAC、构建安全MAC、CBC-MAC、CRHF、HMAC、信息论上MAC。\n\n3. 完整"
  },
  {
    "path": "notes-Chinese/7 CCA安全与认证加密.md",
    "chars": 10441,
    "preview": "# 7 CCA安全与认证加密\n\n1. 本节学习用于抵抗CCA攻击的加密方案以及同时保证通信机密性和真实性的认证加密方案。\n\n2. 目录:CCA安全加密,认证加密,确定性加密,密钥派生函数。\n\n3. 回顾CCA不可区分实验\n\n   - CCA"
  },
  {
    "path": "notes-Chinese/8.1 公钥加密理论.md",
    "chars": 13868,
    "preview": "# 8.1 公钥加密理论\n\n1. 本节学习用于保护信息的完整性和真实性的消息认证码(MAC)和抗碰撞的哈希函数(CRHF)。\n\n2. 目录:公钥加密的定义和安全,陷门排列,选择密文攻击安全,在随机预言机模型中从陷门排列到公钥加密。\n\n3. "
  },
  {
    "path": "notes-Chinese/8.2 RSA问题与加密.md",
    "chars": 11719,
    "preview": "# 8.2 RSA问题与加密\n\n1. 本节学习第一个也是目前应用最广泛的公钥加密方案RSA。\n\n2. 目录:RSA问题,针对“书本上RSA”加密的攻击,实践中的RSA加密。\n\n3. RSA概览\n   \n   - RSA: Ron Rives"
  },
  {
    "path": "notes-Chinese/8.3 DH问题与加密.md",
    "chars": 10065,
    "preview": "# 8.3 DH问题与加密\n\n1. 本节学习基于循环群上离散对数问题的DH问题及Elgamal加密方案。\n\n2. 目录:循环群与离散对数,DH假设和应用,Elgamal加密方案。\n\n3. 循环群(Cyclic Groups)与生成元(Gen"
  },
  {
    "path": "notes-Chinese/9 数字签名.md",
    "chars": 16497,
    "preview": "# 9 数字签名\n\n1. 本节学习公钥密码学中用于保护信息完整性和真实性的数字签名。\n\n2. 目录:数字签名定义、RSA签名、来自离散对数问题的数字签名、一次签名方案、证书与公钥基础设施。\n\n3. 数字签名概览\n   \n   - 数字签名("
  },
  {
    "path": "notes-Chinese/README.md",
    "chars": 343,
    "preview": "# 密码学原理课程讲义\n\n这个讲义是辅助材料,不能替代英文教材!当讲义、幻灯片与教材不一致时,以教材为准。\n\n这是与幻灯片搭配的中文讲义,每个讲义中的数字编号与幻灯片中页码是相同的。\n\n为了正确显示讲义中的公式,请下载Markdown文件到"
  },
  {
    "path": "source/0intro.tex",
    "chars": 2837,
    "preview": "\\input{source/header/main.tex}\n\n\\title{Cryptography Principles}\n\n\\begin{document}\n\\maketitle\n\\begin{frame}\\frametitle{Wh"
  },
  {
    "path": "source/10protocols.tex",
    "chars": 22576,
    "preview": "\\input{source/header/main.tex}\n\n\\title{A Quick Tour of \\\\ Cryptographic Protocols Zoo}\n\n\\begin{document} \n\\maketitle\n\\be"
  },
  {
    "path": "source/11summary.tex",
    "chars": 9853,
    "preview": "\\input{source/header/main.tex}\n\n\\title{Closing Remarks}\n\\begin{document}\n\t\\maketitle\t\n\\begin{comment}\n\\begin{frame}\\fram"
  },
  {
    "path": "source/12backup.tex",
    "chars": 22225,
    "preview": "\\input{source/header/main.tex}\n\n\\title{More on Public-Key Cryptography}\n\n\\begin{document}\n\\maketitle\n\\begin{frame}\n\\fram"
  },
  {
    "path": "source/1introduction.tex",
    "chars": 17045,
    "preview": "\\input{source/header/main.tex}\n\n\\title{Introduction}\n\n\\begin{document}\n\\maketitle\n\\begin{frame}\n\\frametitle{Outline}\n\\ta"
  },
  {
    "path": "source/2perfectlysecret.tex",
    "chars": 9318,
    "preview": "\\input{source/header/main.tex}\n\n\\title{Perfectly Secret Encryption}\n\n\\begin{document}\n\\maketitle\n\\begin{frame}\\frametitl"
  },
  {
    "path": "source/3.1privatekey.tex",
    "chars": 20331,
    "preview": "\\input{source/header/main.tex}\n\n\\title{Private-Key Encryption and Pseudorandomness (Part I)}\n\n\\begin{document}\n\\maketitl"
  },
  {
    "path": "source/3.2privatekey.tex",
    "chars": 23346,
    "preview": "\\input{source/header/main.tex}\n\n\\title{Private-Key Encryption and Pseudorandomness (Part II)}\n\n\\begin{document}\n\\maketit"
  },
  {
    "path": "source/4blockcipher.tex",
    "chars": 20891,
    "preview": "\\input{source/header/main.tex}\n\n\\title{Practical Constructions of Pseudorandom Permutations (Block Ciphers)}\n\n\\begin{doc"
  },
  {
    "path": "source/5owf.tex",
    "chars": 8638,
    "preview": "\\input{source/header/main.tex}\n\n\\title{Theoretical Constructions of Pseudorandom Objects}\n\n\\begin{document}\n\\maketitle\n\\"
  },
  {
    "path": "source/6mac-crhf.tex",
    "chars": 34921,
    "preview": "\\input{source/header/main.tex}\n\n\\title{Message Authentication Codes and Collision-Resistant Hash Functions}\n\n\\begin{docu"
  },
  {
    "path": "source/7cca-ae.tex",
    "chars": 16285,
    "preview": "\\input{source/header/main.tex}\n\\title{CCA-Secure and Authentication Encryption}\n\\begin{document}\n\\maketitle\n\\begin{frame"
  },
  {
    "path": "source/8.1pubkey.tex",
    "chars": 29499,
    "preview": "\\input{source/header/main.tex}\n\n\\title{Public-Key Encryption Theory}\n\n\\begin{document}\n\\maketitle\n\\begin{frame}\n\\frameti"
  },
  {
    "path": "source/8.2RSA.tex",
    "chars": 29542,
    "preview": "\\input{source/header/main.tex}\n\n\\title{RSA Problem and Encryption}\n\n\\begin{document}\n\\maketitle\n\\begin{frame}\n\\frametitl"
  },
  {
    "path": "source/8.3DH.tex",
    "chars": 26054,
    "preview": "\\input{source/header/main.tex}\n\n\\title{Diffie-Hellman Problem and Cryptography}\n\n\\begin{document}\n\\maketitle\n\\begin{fram"
  },
  {
    "path": "source/9digital-sig.tex",
    "chars": 28694,
    "preview": "\\input{source/header/main.tex}\n\n\\title{Digital Signature}\n\n\\begin{document}\n\\maketitle\n\\begin{frame}\n\\frametitle{Outline"
  },
  {
    "path": "source/crush-course.tex",
    "chars": 54133,
    "preview": "\\input{source/header/main.tex}\n\n\\title{A Crush Course On Cryptography}\n\\begin{document}\n\t\\maketitle\t\n\\begin{frame}\\frame"
  },
  {
    "path": "source/header/frame.tex",
    "chars": 1795,
    "preview": "% presentation\n\n%\\documentclass{beamer}\n%\\usetheme[height=7mm]{Rochester}\n%\\usecolortheme{rose}\n\n% handout\n\n\\documentcla"
  },
  {
    "path": "source/header/handout.tex",
    "chars": 1804,
    "preview": "% presentation\n\n%\\documentclass{beamer}\n%\\usetheme[height=7mm]{Rochester}\n%\\usecolortheme{rose}\n\n% handout\n\n\\documentcla"
  },
  {
    "path": "source/header/main.tex",
    "chars": 3862,
    "preview": "% presentation\n\\documentclass{beamer}\n\\usetheme[height=7mm]{Rochester}\n\\usecolortheme{rose}\n\n% handout\n\n%\\documentclass["
  },
  {
    "path": "tikz/1outof2.tex",
    "chars": 1423,
    "preview": "\\begin{tikzpicture}[font=\\footnotesize]\n\\node (A) at (0,0) [minimum size=1cm] {}; \\Alice{0}{0}{0.4};\n\\node (B) [right of"
  },
  {
    "path": "tikz/3ballot.tex",
    "chars": 929,
    "preview": "\\begin{tikzpicture}[scale=0.7, every node/.style={scale=0.7}]\n\\foreach \\n in {0, 1} {\n\\foreach \\x in {1, 2, 3} {\n\\node ("
  },
  {
    "path": "tikz/3parties-DHKE.tex",
    "chars": 873,
    "preview": "\\begin{tikzpicture}[scale=0.7, every node/.style={scale=0.7}]\n\\node (A) at (0,0) [minimum size=1.4cm] {}; \\Alice{0}{0}{0"
  },
  {
    "path": "tikz/3parties-JOUX.tex",
    "chars": 834,
    "preview": "\\begin{tikzpicture}[scale=0.7, every node/.style={scale=0.7}]\n\\node (A) at (0,0) [minimum size=1.4cm] {}; \\Alice{0}{0}{0"
  },
  {
    "path": "tikz/CBC-MAC.tex",
    "chars": 727,
    "preview": "\\begin{tikzpicture}[scale=0.7, every node/.style={scale=0.7}]\n\\foreach \\x in {1, 2, 3} {\n\\node (f\\x) at ($\\x*(2.5cm,0)$)"
  },
  {
    "path": "tikz/CBC-small.tex",
    "chars": 826,
    "preview": "\\begin{tikzpicture}[scale=0.7, every node/.style={scale=0.7}]\n\\foreach \\x in {1, 2, 3} {\n\\node (f\\x) at ($\\x*(2.5cm,0)$)"
  },
  {
    "path": "tikz/CBC.tex",
    "chars": 784,
    "preview": "\\begin{tikzpicture}\n\\foreach \\x in {1, 2, 3} {\n\\node (f\\x) at ($\\x*(2.5cm,0)$) [minimum size=1.25cm,rounded corners=1ex,"
  },
  {
    "path": "tikz/CCA-PKCS.tex",
    "chars": 529,
    "preview": "\\begin{tikzpicture}\n\\node (sender) [minimum size=1cm,label=below:Client, label=above:$pk$] {}; \\Alice{0}{0}{0.4};\n\\node "
  },
  {
    "path": "tikz/CCA.tex",
    "chars": 1390,
    "preview": "\\begin{tikzpicture}\n\\node (sender) [minimum size=1cm] {}; \\Alice{0}{0}{0.4};\n\\node (bart) [below of = sender, node dista"
  },
  {
    "path": "tikz/CMAC.tex",
    "chars": 817,
    "preview": "\\begin{tikzpicture}[scale=0.7, every node/.style={scale=0.7}]\n\\foreach \\x in {1, 2, 3} {\n\\node (f\\x) at ($\\x*(2.5cm,0)$)"
  },
  {
    "path": "tikz/CTR.tex",
    "chars": 763,
    "preview": "\\begin{tikzpicture}\n\\foreach \\x in {1, 2, 3} {\n\\node (f\\x) at ($\\x*(2.5cm,0)$) [minimum size=1.25cm,rounded corners=1ex,"
  },
  {
    "path": "tikz/DESkey.tex",
    "chars": 1335,
    "preview": "\\begin{tikzpicture}[rc/.style={rounded corners=1ex}]\n\\node (k1) [minimum width=4cm, draw] {56-bit key};\n\\node (lh) at ($"
  },
  {
    "path": "tikz/DHkey.tex",
    "chars": 929,
    "preview": "\\begin{tikzpicture}[font=\\footnotesize]\n%\\node (A) at (0,0) {\\Lisa};\n%\\node (B) [right of = A, node distance = 3cm] {\\Le"
  },
  {
    "path": "tikz/Davies-Meyer.tex",
    "chars": 641,
    "preview": "\\begin{tikzpicture}\n%[scale=0.7, every node/.style={scale=0.7}]\n\n\\node (f) at (0,0) [minimum size=1cm,rounded corners=1e"
  },
  {
    "path": "tikz/ECB.tex",
    "chars": 334,
    "preview": "\\begin{tikzpicture}\n\\foreach \\x in {1, 2, 3} {\n\\node (f\\x) at ($\\x*(2.5cm,0)$) [minimum size=1.25cm,rounded corners=1ex,"
  },
  {
    "path": "tikz/ElGamal.tex",
    "chars": 917,
    "preview": "\\begin{tikzpicture}\n\\draw (0,0) rectangle (5,4);\n\\draw (4.25,0.2) rectangle (4.75,3);\n\\draw[-latex] (-2.5,3.5) -- (0,3.5"
  },
  {
    "path": "tikz/HMAC.tex",
    "chars": 1778,
    "preview": "\\begin{tikzpicture}[cf/.style={trapezium left angle=65, trapezium right angle=90, minimum height=1cm,minimum width=1cm,t"
  },
  {
    "path": "tikz/IBE.tex",
    "chars": 985,
    "preview": "\\begin{tikzpicture}[font=\\footnotesize,scale=0.7, every node/.style={scale=0.7}]\n%\\node (A) at (0,0) [label=below:Alice]"
  },
  {
    "path": "tikz/KDC.tex",
    "chars": 753,
    "preview": "\\begin{tikzpicture}[font=\\footnotesize,scale=0.7, every node/.style={scale=0.7}]\n%\\node (A) at (0,0) [label=below:Alice]"
  },
  {
    "path": "tikz/MDtransform.tex",
    "chars": 1235,
    "preview": "\\begin{tikzpicture}[cf/.style={trapezium left angle=65, trapezium right angle=90, minimum height=1cm,minimum width=1cm,t"
  },
  {
    "path": "tikz/MS-PPTP.tex",
    "chars": 560,
    "preview": "\\begin{tikzpicture}\n\\node (sender) [minimum size=1cm,label=below:Client, label=above:$k$] {}; \\Alice{0}{0}{0.4};\n\\node ("
  },
  {
    "path": "tikz/Miyaguchi-Preneel.tex",
    "chars": 676,
    "preview": "\\begin{tikzpicture}\n%[scale=0.7, every node/.style={scale=0.7}]\n\n\\node (f) at (0,0) [minimum size=1cm,rounded corners=1e"
  },
  {
    "path": "tikz/NMAC.tex",
    "chars": 1195,
    "preview": "\\begin{tikzpicture}[cf/.style={trapezium left angle=65, trapezium right angle=90, minimum height=1cm,minimum width=1cm,t"
  },
  {
    "path": "tikz/OAEP-plus.tex",
    "chars": 1082,
    "preview": "\\begin{tikzpicture}[scale=0.8, every node/.style={scale=0.8}]\n\\node (m) [draw,minimum width=1.5cm, minimum height=0.5cm]"
  },
  {
    "path": "tikz/OAEP.tex",
    "chars": 1072,
    "preview": "\\begin{tikzpicture}[scale=0.8, every node/.style={scale=0.8}]\n\\node (m) [draw,minimum width=1.5cm, minimum height=0.5cm]"
  },
  {
    "path": "tikz/OFB.tex",
    "chars": 912,
    "preview": "\\begin{tikzpicture}\n\\foreach \\x in {1, 2, 3} {\n\\node (f\\x) at ($\\x*(2.5cm,0)$) [minimum size=1.25cm,rounded corners=1ex,"
  },
  {
    "path": "tikz/OWF.tex",
    "chars": 321,
    "preview": "\\begin{tikzpicture}\n\\node (x) [circle, minimum size=1.5cm, draw] {$x$};\n\\node (fx) [circle, draw, right of=x, minimum si"
  },
  {
    "path": "tikz/SAEP-plus.tex",
    "chars": 1085,
    "preview": "\\begin{tikzpicture}[scale=0.8, every node/.style={scale=0.8}]\n\\node (m) [draw,minimum width=1.5cm, minimum height=0.5cm]"
  },
  {
    "path": "tikz/SIV-CTR.tex",
    "chars": 1016,
    "preview": "\\begin{tikzpicture}[scale=0.7, every node/.style={scale=0.7}]\n\\foreach \\x in {1, 2, 3} {\n\\node (f\\x) at ($\\x*(2.5cm,0)$)"
  },
  {
    "path": "tikz/TDES.tex",
    "chars": 722,
    "preview": "\\begin{tikzpicture}\n\\node (x)  {$x$};\n\\node (f1) [right of=x, rounded corners=1ex, minimum width=1cm, draw, node distanc"
  },
  {
    "path": "tikz/VCBC-MAC.tex",
    "chars": 820,
    "preview": "\\begin{tikzpicture}[scale=0.7, every node/.style={scale=0.7}]\n\\foreach \\x in {1, 2, 3, 4} {\n\\node (f\\x) at ($\\x*(2.5cm,0"
  },
  {
    "path": "tikz/alice.tex",
    "chars": 174,
    "preview": "\\begin{tikzpicture}\n    %\\draw[help lines] (-1,-3) grid (10,3);\n\n    \\Alice{0}{0}{1};\n\n    \\Bob{2.5}{0}{1};\n\n    \\Evil{5"
  },
  {
    "path": "tikz/attack-spn.tex",
    "chars": 3777,
    "preview": "\\begin{tikzpicture}[font=\\tiny,thin,\n\tfw/.style={inner sep=0pt, black,fill=white},\n\trv/.style={blue, very thick},\n\tuk/.s"
  },
  {
    "path": "tikz/authentication.tex",
    "chars": 1237,
    "preview": "\\begin{tikzpicture}\n\\node (sender) [minimum size=1cm] {}; \\Alice{0}{0}{0.4};\n\\node (bart) [below of = sender, node dista"
  },
  {
    "path": "tikz/baby-giant.tex",
    "chars": 1054,
    "preview": "\\begin{tikzpicture}[thin,giant/.style={decorate,fill=blue!60,decoration={footprints,foot length=20pt, stride length=5cm,"
  },
  {
    "path": "tikz/bilinear-map.tex",
    "chars": 1819,
    "preview": "\\begin{tikzpicture}\n\\node (g1) at (-0.5,0.17) [ellipse,minimum width=4cm,minimum height=1.5cm,draw] {};\n\\node (g1n) [lef"
  },
  {
    "path": "tikz/birthdayattack.tex",
    "chars": 422,
    "preview": "\\begin{tikzpicture}\n\\draw[-] (0,0) -- (9.5,0);\n\\foreach \\x in {0,1.5,...,7.6}{\n\\fill[red!60] (\\x,-3pt) rectangle +(1.43,"
  },
  {
    "path": "tikz/blindsignature.tex",
    "chars": 904,
    "preview": "\\begin{tikzpicture}[font=\\footnotesize]\n\\node (A) at (0,0) [minimum size=1.4cm] {}; \\Alice{0}{0}{0.4};\n\\node (B) [right "
  },
  {
    "path": "tikz/cPRF.tex",
    "chars": 987,
    "preview": "\\begin{tikzpicture}[nn/.style={draw,minimum width=1cm, minimum height=0.5cm, rounded corners=1ex},level distance=1cm,\nle"
  },
  {
    "path": "tikz/certificates.tex",
    "chars": 984,
    "preview": "\\begin{tikzpicture}[font=\\footnotesize]\n\\node (A) at (0,0) [minimum size=1.4cm] {}; \\Alice{0}{0}{0.4}; \\node at (0,-0.7)"
  },
  {
    "path": "tikz/chain-sig.tex",
    "chars": 1396,
    "preview": "\\begin{tikzpicture}[ps/.style={fill=blue!30},ss/.style={fill=red!30},ms/.style={fill=green!30},gs/.style={fill=yellow!30"
  },
  {
    "path": "tikz/cipher-stealing.tex",
    "chars": 1332,
    "preview": "\\begin{tikzpicture}[scale=0.7, every node/.style={scale=0.7,minimum size=0.5cm}]\n\\node (m2) at (0,0) [minimum width=1.5c"
  },
  {
    "path": "tikz/coinflipping.tex",
    "chars": 1143,
    "preview": "\\begin{tikzpicture}[font=\\footnotesize]\n\\node (A) at (0,0) [minimum size=1cm] {}; \\Alice{0}{0}{0.4};\n\\node (B) [right of"
  },
  {
    "path": "tikz/collision.tex",
    "chars": 1500,
    "preview": "\\begin{tikzpicture}[uk/.style={inner sep=1pt, minimum width=10pt, circle, fill=red!50},kk/.style={inner sep=1pt, minimum"
  },
  {
    "path": "tikz/combination.tex",
    "chars": 884,
    "preview": "\\begin{tikzpicture}\n\\node (enc1) [minimum width=1.5cm, minimum height=1.5cm, draw] {$\\mathsf{Enc}$};\n\\node (mac1) [right"
  },
  {
    "path": "tikz/compute-sec.tex",
    "chars": 469,
    "preview": "\\begin{tikzpicture}[scale=1.2]\n\\draw[->] (0,0) -- (4,0) node [midway, below] {Time: $t$};\n\\draw[->] (0,0) -- (0,4) node "
  },
  {
    "path": "tikz/constructD.tex",
    "chars": 732,
    "preview": "\\begin{tikzpicture}\n\\draw (0,0) rectangle (5,4);\n\\draw (4.25,0.2) rectangle (4.75,3);\n\\draw[-latex] (-2.5,3) -- (0,3) no"
  },
  {
    "path": "tikz/des.tex",
    "chars": 1065,
    "preview": "\\begin{tikzpicture}\n\\node (xor) [circle, draw] {};\n\\draw[-] (xor.north) -- (xor.south);\n\\draw[-] (xor.east) -- (xor.west"
  },
  {
    "path": "tikz/desx.tex",
    "chars": 694,
    "preview": "\\begin{tikzpicture}\n\\node (x)  {$x$};\n\\node (xor) [right of=x, circle, draw] {};\n\\draw[-] (xor.north) -- (xor.south);\n\\d"
  },
  {
    "path": "tikz/differential.tex",
    "chars": 2586,
    "preview": "\\begin{tikzpicture}[font=\\tiny,thin,\n\tfw/.style={inner sep=0pt, black,fill=white},\n\trv/.style={red, very thick}]\n\\foreac"
  },
  {
    "path": "tikz/digitalsignature.tex",
    "chars": 1864,
    "preview": "\\begin{tikzpicture}[rc/.style={rounded corners=1ex, minimum width=1cm, minimum height=0.7cm}]\n\\node (sender) [minimum si"
  },
  {
    "path": "tikz/dining.tex",
    "chars": 734,
    "preview": "\\begin{tikzpicture}[out=45,in=135]\n\\foreach \\x / \\y / \\z [evaluate=\\x as \\dg using \\x*60,evaluate=\\x as \\dgg using \\x*60"
  },
  {
    "path": "tikz/doubleE.tex",
    "chars": 415,
    "preview": "\\begin{tikzpicture}\n\\node (x)  {$x$};\n\\node (f1) [right of=x, rounded corners=1ex, draw] {$F$};\n\\node (f2) [right of=f1,"
  },
  {
    "path": "tikz/ePRG.tex",
    "chars": 534,
    "preview": "\\begin{tikzpicture}\n\\foreach \\x / \\z [evaluate=\\x as \\shade using \\x*15] in {0/-1,1/0,2/1,3/2} {\n\\node (f\\x) at ($(0,-1."
  },
  {
    "path": "tikz/ecdhke.tex",
    "chars": 1883,
    "preview": "\\begin{tikzpicture}[scale=0.5,\nevery node/.style={scale=0.6}, \nevery label/.style={blue},\ndot/.style={inner sep=1pt, min"
  },
  {
    "path": "tikz/elgamal-con.tex",
    "chars": 684,
    "preview": "\\begin{tikzpicture}\n\\node (f1) [rounded corners=1ex,minimum size=0.7cm, draw] {$g^y$};\n\\node (h1) [right of=f1, node dis"
  },
  {
    "path": "tikz/ellipticcurve.tex",
    "chars": 2923,
    "preview": "\\begin{tikzpicture}[scale=0.7]\n\n\\newcommand*{\\ShowIntersection}{\n\\fill \n    [name intersections={of=ec1 and l1, name=i, "
  },
  {
    "path": "tikz/encryptionwithpf.tex",
    "chars": 748,
    "preview": "\\begin{tikzpicture}\n\\node (r) {$r$};\n\\node (pg) [draw, below of = r, rounded corners=1ex,node distance = 1.5cm] {$F$};\n\\"
  },
  {
    "path": "tikz/encryptionwithpg.tex",
    "chars": 639,
    "preview": "\\begin{tikzpicture}\n\\node (k) {$k$};\n\\node (pg) [draw, below of = k, rounded corners=1ex,node distance = 1.5cm] {$G$};\n\\"
  },
  {
    "path": "tikz/feistel-prp.tex",
    "chars": 1643,
    "preview": "\\begin{tikzpicture}[rr/.style={blue, very thick}, ll/.style={red, very thick}]\n\\foreach \\z in {1, 2,...,4} {\n\\node (f\\z)"
  },
  {
    "path": "tikz/feistel.tex",
    "chars": 1397,
    "preview": "\\begin{tikzpicture}[scale=0.8, every node/.style={scale=0.8}]\n\\foreach \\z in {1, 2,...,4} {\n\\node (f\\z) at ($\\z*(0,-1.5c"
  },
  {
    "path": "tikz/hash.tex",
    "chars": 655,
    "preview": "\\begin{tikzpicture}[cf/.style={trapezium left angle=65, trapezium right angle=90, minimum height=1cm,minimum width=1cm,t"
  },
  {
    "path": "tikz/hcp.tex",
    "chars": 479,
    "preview": "\\begin{tikzpicture}\n\\node (x) [circle, minimum size=1.5cm, draw] {$x$};\n\\node (fx) [circle, draw, right of=x, minimum si"
  },
  {
    "path": "tikz/hs-reduce-1.tex",
    "chars": 956,
    "preview": "\\begin{tikzpicture}\n\\draw (0,0) rectangle (5,4);\n\\draw (4.25,0.2) rectangle (4.75,3);\n\\draw[-latex] (-2.5,3.5) -- (0,3.5"
  },
  {
    "path": "tikz/hs-reduce-2.tex",
    "chars": 856,
    "preview": "\\begin{tikzpicture}\n\\draw (0,0) rectangle (5,4);\n\\draw (4.25,0.2) rectangle (4.75,3);\n\\draw[-latex] (-2.5,3.5) -- (0,3.5"
  },
  {
    "path": "tikz/hybrid-enc-proof.tex",
    "chars": 780,
    "preview": "\\begin{tikzpicture}[font=\\small]\n\\node (pk0) at (0,0) {$\\langle pk,\\mathsf{Enc}_{pk}(k),\\mathsf{Enc}_{k}'(m_0)\\rangle$};"
  },
  {
    "path": "tikz/hybrid-encrypt.tex",
    "chars": 521,
    "preview": "\\begin{tikzpicture}\n\\node (f1) [minimum size=1.25cm,rounded corners=1ex,draw] {$\\mathsf{Enc}_{pk}$};\n\\node (f2) at (2.5c"
  },
  {
    "path": "tikz/hybrideg.tex",
    "chars": 762,
    "preview": "\\begin{tikzpicture}\n\\draw (0,0) rectangle (5,4);\n\\draw (4.25,0.2) rectangle (4.75,3);\n\\draw[-latex] (-2.5,3.5) -- (0,3.5"
  },
  {
    "path": "tikz/hybridproof.tex",
    "chars": 801,
    "preview": "\\begin{tikzpicture}\n\\draw (0,0) rectangle (5,4);\n\\draw (4.25,0.2) rectangle (4.75,3);\n\\draw[-latex] (-2.5,3.5) -- (0,3.5"
  },
  {
    "path": "tikz/identification-schnorr.tex",
    "chars": 2000,
    "preview": "\\begin{tikzpicture}[font=\\footnotesize]\n\\node (A) at (0,0) [minimum size=1cm] {}; \\Alice{0}{0}{0.4};\n\\node (B) [right of"
  },
  {
    "path": "tikz/identification.tex",
    "chars": 880,
    "preview": "\\begin{tikzpicture}[font=\\footnotesize]\n\\node (A) at (0,0) [minimum size=1cm] {}; \\Alice{0}{0}{0.4};\n\\node (B) [right of"
  },
  {
    "path": "tikz/integrity.tex",
    "chars": 1273,
    "preview": "\\begin{tikzpicture}\n\\node (sender) [minimum size=1cm] {}; \\Alice{0}{0}{0.4};\n\\node (bart) [below of = sender, node dista"
  },
  {
    "path": "tikz/interlock.tex",
    "chars": 1780,
    "preview": "\\begin{tikzpicture}[font=\\footnotesize]\n\\node (A) at (0,0) [minimum size=1cm] {}; \\Alice{0}{0}{0.4};\n\\node (B) [right of"
  },
  {
    "path": "tikz/keyed-func.tex",
    "chars": 2108,
    "preview": "\\begin{tikzpicture}[uk/.style={inner sep=1pt, minimum width=10pt, circle, fill=red!50},kk/.style={inner sep=1pt, minimum"
  },
  {
    "path": "tikz/linear.tex",
    "chars": 2882,
    "preview": "\\begin{tikzpicture}[font=\\tiny,thin,\n\tfw/.style={inner sep=0pt, black,fill=white},\n\trv/.style={red, very thick}]\n\\foreac"
  },
  {
    "path": "tikz/mac.tex",
    "chars": 1856,
    "preview": "\\begin{tikzpicture}[rc/.style={rounded corners=1ex, minimum width=1cm, minimum height=0.7cm}]\n\\node (sender) [minimum si"
  },
  {
    "path": "tikz/macforge-exp.tex",
    "chars": 1515,
    "preview": "\\begin{tikzpicture}\n%\\node (A) at (0,0) {\\Homer};\n%\\node (B) [right of = A, node distance = 4cm] {\\Left\\Burns};\n\\node (A"
  },
  {
    "path": "tikz/macwithprf.tex",
    "chars": 328,
    "preview": "\\begin{tikzpicture}\n\\node (r) {$m$};\n\\node (pg) [draw, below of = r, rounded corners=1ex,node distance = 1.5cm] {$F$};\n\\"
  },
  {
    "path": "tikz/man-in-middle.tex",
    "chars": 1884,
    "preview": "\\begin{tikzpicture}[font=\\footnotesize]\n\\node (A) at (0,0) [minimum size=1cm] {}; \\Alice{0}{0}{0.4};\n\\node (B) [right of"
  },
  {
    "path": "tikz/meet-in-middle.tex",
    "chars": 844,
    "preview": "\\begin{tikzpicture}\n\\node (x)  {$x$};\n\\node (f1) [right of=x, rounded corners=1ex, minimum width=1cm, draw,node distance"
  },
  {
    "path": "tikz/multiple-enc-exp.tex",
    "chars": 1232,
    "preview": "\\begin{tikzpicture}\n%\\node (A) at (0,0) {\\Homer};\n%\\node (B) [right of = A, node distance = 4cm] {\\Left\\Burns};\n\\node (A"
  },
  {
    "path": "tikz/outfile",
    "chars": 35310,
    "preview": "\n#Surface 0 of 1 surfaces\n\n# Contour 0, label:        0\n 1.52332  1.73574  0 \n 1.52561  1.73974  0 \n 1.52791  1.74374  0"
  },
  {
    "path": "tikz/owff.tex",
    "chars": 1571,
    "preview": "\\begin{tikzpicture}[uk/.style={inner sep=1pt, minimum width=15pt, circle},kk/.style={inner sep=1pt, minimum width=15pt, "
  },
  {
    "path": "tikz/owfover.tex",
    "chars": 438,
    "preview": "\\begin{tikzpicture}[out=45,in=135]\n\\foreach \\x / \\y [evaluate=\\x as \\dg using \\x*60] in {0/OWF,1/HCP,2/PRG,3/PRF,4/PRP,5"
  },
  {
    "path": "tikz/padding-oracle-lastbyte.tex",
    "chars": 962,
    "preview": "\\begin{tikzpicture}[scale=0.7, every node/.style={scale=0.7}]\n\\foreach \\x in {1, 2, 3} {\n\\node (f\\x) at ($\\x*(2.5cm,0)$)"
  },
  {
    "path": "tikz/padding-oracle-null.tex",
    "chars": 875,
    "preview": "\\begin{tikzpicture}[scale=0.7, every node/.style={scale=0.7}]\n\\foreach \\x in {1} {\n\\node (f\\x) at ($\\x*(2.5cm,0)$) [mini"
  },
  {
    "path": "tikz/padding-oracle.tex",
    "chars": 1092,
    "preview": "\\begin{tikzpicture}[font=\\footnotesize]\n%\\node (A) at (0,0) [label=below:User] {\\Lisa};\n%\\node (B) [right of = A, node d"
  },
  {
    "path": "tikz/pgfD.tex",
    "chars": 794,
    "preview": "\\begin{tikzpicture}\n\\draw (0,0) rectangle (5,4);\n\\draw (4.25,0.2) rectangle (4.75,3);\n\\draw[-latex] (-2.5,3) -- (0,3) no"
  },
  {
    "path": "tikz/pgfMAC.tex",
    "chars": 848,
    "preview": "\\begin{tikzpicture}\n\\draw (0,0) rectangle (5,4);\n\\draw (4.25,0.2) rectangle (4.75,3);\n\\draw[-latex] (-2.5,3) -- (0,3) no"
  },
  {
    "path": "tikz/pnp.tex",
    "chars": 1101,
    "preview": "\\begin{tikzpicture}\n\\clip (-2.5,-4.5) rectangle (7.5,1);\n\\draw[dashed] (2.5,-5) -- (2.5,1);\n\\node (nph) {$\\mathcal{NP}\\t"
  },
  {
    "path": "tikz/prg-distinguisher.tex",
    "chars": 1283,
    "preview": "\\begin{tikzpicture}\n%\\node (A) at (0,0) {\\Homer};\n%\\node (B) [right of = A, node distance = 4cm] {\\Left\\Burns};\n\\node (A"
  },
  {
    "path": "tikz/prg-sparse.tex",
    "chars": 499,
    "preview": "\\pgfmathsetseed{2}\n\\begin{tikzpicture}\n\\draw (-1.1,-1.1) rectangle (1.1,1.1);\n\\node (seed) at (-2,0) [draw=red!70,fill=r"
  },
  {
    "path": "tikz/pri-cpa-exp.tex",
    "chars": 1463,
    "preview": "\\begin{tikzpicture}\n%\\node (A) at (0,0) {\\Homer};\n%\\node (B) [right of = A, node distance = 4cm] {\\Left\\Burns};\n\\node (A"
  },
  {
    "path": "tikz/pri-eav-exp.tex",
    "chars": 1198,
    "preview": "\\begin{tikzpicture}\n%\\node (A) at (0,0) {\\Homer};\n%\\node (B) [right of = A, node distance = 4cm] {\\Left\\Burns};\n\\node (A"
  },
  {
    "path": "tikz/private-key.tex",
    "chars": 1224,
    "preview": "\\begin{tikzpicture}\n\\node (sender) [minimum size=1cm] {}; \\Alice{0}{0}{0.4};\n\\node (bart) [below of = sender, node dista"
  },
  {
    "path": "tikz/public-key.tex",
    "chars": 1263,
    "preview": "\\begin{tikzpicture}\n\\node (sender) [minimum size=1cm] {}; \\Alice{0}{0}{0.4};\n\\node (bart) [below of = sender, node dista"
  },
  {
    "path": "tikz/qr-qnr.tex",
    "chars": 1002,
    "preview": "\\begin{tikzpicture}[scale=0.7, every node/.style={scale=0.7}]\n\\node (zp) [draw, minimum size=3cm, circle, label=above:$\\"
  },
  {
    "path": "tikz/rabinOT.tex",
    "chars": 1008,
    "preview": "\\begin{tikzpicture}[font=\\footnotesize]\n\\node (A) at (0,0) [minimum size=1cm] {}; \\Alice{0}{0}{0.4};\n\\node (B) [right of"
  },
  {
    "path": "tikz/reduction-prg.tex",
    "chars": 552,
    "preview": "\\begin{tikzpicture} %[scale=0.7, every node/.style={scale=0.7}]\n\\draw (0,0) rectangle (5,4);\n\\draw (3,0.5) rectangle (4."
  },
  {
    "path": "tikz/reduction.tex",
    "chars": 593,
    "preview": "\\begin{tikzpicture}\n\\draw (0,0) rectangle (5,4);\n\\draw (3,0.5) rectangle (4.5,3);\n\\draw[-latex] (-2.5,3) -- (0,3) node ["
  },
  {
    "path": "tikz/schnorr-signature.tex",
    "chars": 2337,
    "preview": "\\begin{tikzpicture}\n    \\node (x1) [rounded corners=1ex,minimum size=0.7cm, draw] {$g^x$};\n    \\node (k1) [right of=x1, "
  },
  {
    "path": "tikz/schnorr.tex",
    "chars": 944,
    "preview": "\\begin{tikzpicture}[font=\\footnotesize]\n\\node (A) at (0,0) [minimum size=1cm] {}; \\Alice{0}{0}{0.4};\n\\node (B) [right of"
  },
  {
    "path": "tikz/spn.tex",
    "chars": 1151,
    "preview": "\\begin{tikzpicture}\n\\foreach \\z in {1, 2,...,3} {\n\\node (km\\z) at ($\\z*(0,-2.2cm)$) [minimum width=6cm,rounded corners=1"
  },
  {
    "path": "tikz/ssl.tex",
    "chars": 1579,
    "preview": "\\begin{tikzpicture}[font=\\footnotesize]\n%\\node (A) at (0,0) {\\Lisa(Client)};\n%\\node (B) [right of = A, node distance = 4"
  },
  {
    "path": "tikz/suf-mac.tex",
    "chars": 2718,
    "preview": "\\begin{tikzpicture}[uk/.style={inner sep=1pt, minimum width=18pt, circle, fill=red!40},kk/.style={inner sep=1pt, minimum"
  },
  {
    "path": "tikz/synchronizedmode.tex",
    "chars": 1894,
    "preview": "\\begin{tikzpicture}\n\\node (sm) {};\n\\foreach \\x in {1, 2, 3} {\n\\node (g\\x) at ($(sm) + \\x*(2.8cm,0) - (4cm,0)$)  [] {};\n\\"
  },
  {
    "path": "tikz/tdp-cca1.tex",
    "chars": 776,
    "preview": "\\begin{tikzpicture}\n\\node (f1) [rounded corners=1ex,minimum size=0.7cm, draw] {$f_{I}$};\n\\node (h1) [right of=f1, node d"
  },
  {
    "path": "tikz/tdp-cca2.tex",
    "chars": 884,
    "preview": "\\begin{tikzpicture}\n\\node (f1) [rounded corners=1ex,minimum size=0.7cm, draw] {$f_{I}$};\n\\node (h1) [right of=f1, node d"
  },
  {
    "path": "tikz/tdp-cpa.tex",
    "chars": 674,
    "preview": "\\begin{tikzpicture}\n\\node (f1) [rounded corners=1ex,minimum size=0.7cm, draw] {$f_{I}$};\n\\node (h1) [right of=f1, node d"
  },
  {
    "path": "tikz/tdp-pk.tex",
    "chars": 1013,
    "preview": "\\begin{tikzpicture}\n\\draw (0,0) rectangle (5,4);\n\\draw (4.25,0.2) rectangle (4.75,3);\n\\draw[-latex] (-2.5,3.5) -- (0,3.5"
  },
  {
    "path": "tikz/threepass.tex",
    "chars": 1029,
    "preview": "\\begin{tikzpicture}[font=\\footnotesize]\n\\node (A) at (0,0) [minimum size=1cm] {}; \\Alice{0}{0}{0.4};\n\\node (B) [right of"
  },
  {
    "path": "tikz/tls13hs.tex",
    "chars": 2551,
    "preview": "\\begin{tikzpicture}[font=\\footnotesize, scale=0.8, every node/.style={scale=0.8},\n    ln/.style={text width = 3.5cm, ali"
  },
  {
    "path": "tikz/toy-OT.tex",
    "chars": 1525,
    "preview": "\\begin{tikzpicture}[scale=0.8, every node/.style={scale=0.8}]\n\\foreach \\y in {1, 2, 3, 4} {\n\\node (r\\y) at ($ (0.8cm,0) "
  },
  {
    "path": "tikz/transmission.tex",
    "chars": 1365,
    "preview": "\\begin{tikzpicture}\n\\node (sender) [minimum size=1cm] {}; \\Alice{0}{0}{0.4};\n\\node (bart) [below of = sender, node dista"
  },
  {
    "path": "tikz/trapdoor.tex",
    "chars": 702,
    "preview": "\\begin{tikzpicture}\n\\node (x) [circle, minimum size=1.5cm, draw] {$x$};\n\\node (fx) [circle, draw, right of=x, minimum si"
  },
  {
    "path": "tikz/tree-sig.tex",
    "chars": 1368,
    "preview": "\\begin{tikzpicture}[nn/.style={draw,minimum width=1cm, minimum height=0.5cm, rounded corners=1ex},nl/.style={draw,minimu"
  },
  {
    "path": "tikz/worldofpk.tex",
    "chars": 824,
    "preview": "\\begin{tikzpicture}[block/.style={rectangle, draw,align=center, rounded corners,\nminimum height=2em, minimum width=11em}"
  },
  {
    "path": "tikz/zkp-commitment.tex",
    "chars": 1445,
    "preview": "\\begin{tikzpicture}[font=\\footnotesize]\n\\node (A) at (0,0) [minimum size=1cm] {}; \\Alice{0}{0}{0.4};\n\\node (B) [right of"
  },
  {
    "path": "tikz/zkp-hanmilton.tex",
    "chars": 1867,
    "preview": "\\begin{tikzpicture}[scale=0.7, every node/.style={scale=0.7}]\n\\node (1) at (1,1) [draw,circle] {1};\n\\node (2) at (1,-0.5"
  },
  {
    "path": "tikz/zkp-rsa.tex",
    "chars": 1133,
    "preview": "\\begin{tikzpicture}[font=\\footnotesize]\n\\node (A) at (0,0) [minimum size=1cm] {}; \\Alice{0}{0}{0.4};\n\\node (B) [right of"
  },
  {
    "path": "tikz/zkp.tex",
    "chars": 1752,
    "preview": "\\begin{tikzpicture}[pattern color=white,fill=blue!50]\n%\\draw [help lines] (-2,-2) grid [step=0.1] (10,2);\n%\\draw [help l"
  }
]

// ... and 5 more files (download for full content)

About this extraction

This page contains the full source code of the YuZhang/cryptography GitHub repository, extracted and formatted as plain text for AI agents and large language models (LLMs). The extraction includes 176 files (737.1 KB), approximately 324.2k tokens. Use this with OpenClaw, Claude, ChatGPT, Cursor, Windsurf, or any other AI tool that accepts text input. You can copy the full output to your clipboard or download it as a .txt file.

Extracted by GitExtract — free GitHub repo to text converter for AI. Built by Nikandr Surkov.

Copied to clipboard!