cryptography in c and c 2nd edition

504 595 0
cryptography in c and c 2nd edition

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

www.it-ebooks.info Cryptography in C and C++ MICHAEL WELSCHENBACH Translated by DAVID KRAMER Apress TM www.it-ebooks.info Cryptography in C and C++ Copyright c 2005 by Michael Welschenbach Translator and Compositor: David Kramer Editorial Board: Steve Anglin, Dan Appleman, Ewan Buckingham, Gary Cornell, Tony Davis, Jason Gilmore, Jonathan Hassell, Chris Mills, Dominic Shakeshaft, Jim Sumser Assistant Publisher: Grace Wong Copy Manager: Nicole LeClerc Production Manager: Kari Brooks-Copony Proofreader: Anne Friedman T E X Support: Fred Bartlett and Arthur Ogawa Manufacturing Manager: Tom Debolski Cover Designer: Kurt Krames Library of Congress Cataloging-in-Publication Data Welschenbach, Michael. [Kryptographie in C und C++. English] Cryptography in C and C++ / Michael Welschenbach ; translated by David Kramer.– 2nd American ed., rev. and enl. p. cm. The first American edition is a translation of the second German edition, which has been revised and expanded from the first German edition. Includes bibliographical references and index. ISBN 1-59059-502-5 1. Computer security. 2. Cryptography. 3. C (Computer program language) 4. C++ (Computer program language) I. Title. QA76.9.A25W4313 2005 005.8–dc22 2005002553 All rights reserved. No part of this work may be reproduced or transmitted in any form or by any means, electronic or mechanical, including photocopying, recording, or by any information storage or retrieval system, without the prior written permission of the copyright owner and the publisher. Printed and bound in the United States of America 9 87654321 Trademarked names may appear in this book. Rather than use a trademark symbol with every occurrence of a trademarked name, we use the names only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark. Distributed to the book trade in the United States by Springer-Verlag New York, Inc., 233 Spring Street, 6th Floor, New York, NY 10013, and outside the United States by Springer-Verlag GmbH & Co. KG, Tiergartenstr. 17, 69112 Heidelberg, Germany. In the United States: phone 1-800-SPRINGER, fax 201-348-4505, e-mail orders@springer-ny.com, or visit http://www.springer-ny.com. Outside the United States: fax +49 6221 345229, e-mail orders@springer.de, or visit http://www.springer.de. For information on translations, please contact Apress directly at 2560 Ninth Street, Suite 219, Berkeley, CA 94710. Phone 510-549-5930, fax 510-549-5939, e-mail info@apress.com, or visit http://www.apress.com. The information in this book is distributed on an “as is” basis, without warranty. Although every precaution has been taken in the preparation of this work, neither the author(s) nor Apress shall have any liability to any person or entity with respect to any loss or damage caused or alleged to be caused directly or indirectly by the information contained in this work. The source code for this book is available to readers at http://www.apress.com in the Downloads section. You will need to answer questions pertaining to this book in order to successfully download the code. www.it-ebooks.info To my family, as always www.it-ebooks.info www.it-ebooks.info Contents Foreword xiii About the Author xv About the Translator xvi Preface to the Second American Edition xvii Preface to the First American Edition xix Preface to the First German Edition xxiii I Arithmetic and Number Theory in C 1 1 Introduction 3 2 Number Formats: The Representation of Large Numbers in C 13 3 Interface Semantics 19 4 The Fundamental Operations 23 4.1 Addition and Subtraction 24 4.2 Multiplication 33 4.2.1 The Grade School Method 34 4.2.2 Squaring Is Faster 40 4.2.3 Do Things Go Better with Karatsuba? 45 4.3 Division with Remainder 50 5 Modular Arithmetic: Calculating with Residue Classes 67 6 Where All Roads Meet: Modular Exponentiation 81 6.1 First Approaches 81 6.2 M-ary Exponentiation 86 6.3 Addition Chains and Windows 101 6.4 Montgomery Reduction and Exponentiation 106 6.5 Cryptographic Application of Exponentiation 118 v www.it-ebooks.info Contents 7 Bitwise and Logical Functions 125 7.1 Shift Operations 125 7.2 All or Nothing: Bitwise Relations 131 7.3 Direct Access to Individual Binary Digits 137 7.4 Comparison Operators 140 8 Input, Output, Assignment, Conversion 145 9 Dynamic Registers 157 10 Basic Number-Theoretic Functions 167 10.1 Greatest Common Divisor 168 10.2 Multiplicative Inverse in Residue Class Rings 175 10.3 Roots and Logarithms 183 10.4 Square Roots in Residue Class Rings 191 10.4.1 The Jacobi Symbol 192 10.4.2 Square Roots Modulo p k 198 10.4.3 Square Roots Modulo n 203 10.4.4 Cryptography with Quadratic Residues 211 10.5 A Primality Test 214 11 Rijndael: A Successor to the Data Encryption Standard 237 11.1 Arithmetic with Polynomials 239 11.2 The Rijndael Algorithm 244 11.3 Calculating the Round Key 247 11.4 The S-Box 248 11.5 The ShiftRowsTransformation 249 11.6 The MixColumnsTransformation 250 11.7 The AddRoundKeyStep 252 11.8 Encryption as a Complete Process 253 11.9 Decryption 256 11.10 Performance 259 11.11 Modes of Operation 260 12 Large Random Numbers 261 12.1 A Simple Random Number Generator 265 12.2 Cryptographic Random Number Generators 268 12.2.1 The Generation of Start Values 269 12.2.2 The BBS Random Number Generator 273 12.2.3 The AES Generator 279 12.2.4 The RMDSHA-1 Generator 283 vi www.it-ebooks.info Contents 12.3 Quality Testing 286 12.3.1 Chi-Squared Test 287 12.3.2 Monobit Test 289 12.3.3 Poker Test 289 12.3.4 Runs Test 289 12.3.5 Longruns Test 289 12.3.6 Autocorrelation Test 290 12.3.7 Quality of the FLINT/C Random Number Generators . . . 290 12.4 More Complex Functions 291 13 Strategies for Testing LINT 305 13.1 Static Analysis 307 13.2 Run-Time Tests 309 II Arithmetic in C++ with the Class LINT 317 14 Let C++ Simplify Your Life 319 14.1 Not a Public Affair: The Representation of Numbers in LINT 324 14.2 Constructors 325 14.3 Overloaded Operators 329 15 The LINTPublic Interface: Members and Friends 337 15.1 Arithmetic 337 15.2 Number Theory 347 15.3 Stream I/O of LINTObjects 352 15.3.1 Formatted Output of LINTObjects 353 15.3.2 Manipulators 360 15.3.3 File I/O for LINTObjects 362 16 Error Handling 367 16.1 (Don’t) Panic 367 16.2 User-Defined Error Handling 369 16.3 LINTExceptions 370 17 An Application Example: The RSA Cryptosystem 377 17.1 Asymmetric Cryptosystems 378 17.2 The RSA Algorithm 380 17.3 Digital RSA Signatures 395 17.4 RSA Classes in C++ 403 18 Do It Yourself: Test LINT 413 vii www.it-ebooks.info Contents 19 Approaches for Further Extensions 417 III Appendices 419 A Directory of C Functions 421 A.1 Input/Output, Assignment, Conversions, Comparisons 421 A.2 Basic Calculations 422 A.3 Modular Arithmetic 423 A.4 Bitwise Operations 425 A.5 Number-Theoretic Functions 426 A.6 Generation of Pseudorandom Numbers 427 A.7 Register Management 431 B Directory of C++ Functions 433 B.1 Input/Output, Conversion, Comparison: Member Functions . . . 433 B.2 Input/Output, Conversion, Comparison: Friend Functions 436 B.3 Basic Operations: Member Functions 438 B.4 Basic Operations: Friend Functions 439 B.5 Modular Arithmetic: Member Functions 440 B.6 Modular Arithmetic: Friend Functions 442 B.7 Bitwise Operations: Member Functions 443 B.8 Bitwise Operations: Friend Functions 444 B.9 Number-Theoretic Member Functions 445 B.10 Number-Theoretic Friend Functions 446 B.11 Generation of Pseudorandom Numbers 450 B.12 Miscellaneous Functions 450 C Macros 451 C.1 Error Codes and Status Values 451 C.2 Additional Constants 451 C.3 Macros with Parameters 453 D Calculation Times 459 E Notation 461 F Arithmetic and Number-Theoretic Packages 463 References 465 Index 473 viii www.it-ebooks.info List of Figures 4-1 Calculations for multiplication 35 4-2 Calculations for squaring 41 4-3 CPU time for Karatsuba multiplication 49 4-4 Calculational schema for division 51 11-1 Layering of transformations in the Rijndael rounds 246 11-2 Diagram for round keys for L k =4 248 12-1 Periodic behavior of a pseudorandom sequence 263 17-1 Example of the construction of a certificate 401 17-2 Certification of a digital signature 402 ix www.it-ebooks.info [...]... respectively archives, are linked A typical compiler call, here for the GNU compiler gcc, looks something like the following (with the paths to the source directories suppressed): gcc -O2 -o rsademo rsademo.cpp rsakey.cpp flintpp.cpp randompp.cpp flint .c aes .c ripemd .c sha256 .c entropy .c random .c -lstdc++ The C+ + header files following the ANSI standard are used when in compilation the macro FLINTPP_ANSI... for constants as in extern int FLINT_API add_l(CLINT, CLINT, CLINT); extern USHORT FLINT_API_DATA smallprimes[]; or, respectively, in the use of the assembler functions extern int FLINT_API_A div_l (CLINT, CLINT, CLINT, CLINT); These macros are generally defined as empty comments /**/ With their aid, using the appropriate definitions, compiler- and linker-speci c instructions to functions and data can... library contains, among other items, the modules shown in Tables 1-1 through 1-5, which can be found as source code at www.apress.com 6 www.it-ebooks.info Introduction Table 1-1 Arithmetic and number theory in C in directory flint/src flint.h header file for using functions from flint .c flint .c arithmetic and number-theoretic functions in C kmul.{h ,c} functions for Karatsuba multiplication and squaring ripemd.{h ,c} ... multiplication, replaces the C function mult() in flint .c umul.{s,asm} multiplication, replaces the C function umul() sqr.{s,asm} squaring, replaces the C function sqr() div.{s,asm} division, replaces the C function div_l() Table 1-3 Tests (see Section 13.2 and Chapter 18) in directories flint/test and flint/test/testvals testxxx .c[ pp] test programs in C and C+ + xxx.txt test vectors for AES 7 www.it-ebooks.info... successor of every element that it contains From this postulated existence of (at least) one so-called successor set, which, beginning with 0, contains all successors, set theory derives the existence of a minimal successor set N, which is itself a subset of every successor set This minimal and thus uniquely determined successor set N is called the set of natural numbers, in which we expressly include... downloadable source code in translated form as libraries in OMF (object module 2 Call : ml /Cx /c /Gd filename 8 www.it-ebooks.info Introduction format) and COFF (common object file format), respectively, as well as in the form of a LINUX archive, and are used instead of the corresponding C functions when in translating C programs the macro FLINT_ASM is defined and the assembler object modules from the... calling conventions are to be called For modules that import FLINT /C functions and constants from a dynamic link library (DLL) under Microsoft Visual C/ C++, in translation the macros -D FLINT_API= cdecl and -D FLINT_API_DATA= declspec (dllimport) must be defined This has already been taken into account in flint.h, and it suffices in this case to define the macro FLINT_USEDLL for compilation For other development... expanded by several functions and in places has been significantly overhauled, and in the process a number of errors and points of imprecision were removed Additional test functions were developed and existing test functions expanded A security mode was implemented, whereby securitycritical variables in the individual functions are deleted by being overwritten All C and C+ + functions are now clearly cited... used and not 3 With modern computers with virtual memory, except in the case of DOS, one usually does not have to worry about this point, in particular with Unix or Linux systems 9 www.it-ebooks.info Chapter 1 the GNU compiler gcc, the macro FLINT_API_A is defined by cdecl, and some compilers understand this as an instruction that the assembler functions corresponding to the C name and calling conventions...www.it-ebooks.info List of Tables 1-1 1-2 1-5 Arithmetic and number theory in C in directory flint/src Arithmetic modules in 80x86 assembler (see Chapter 19) in directory flint/src/asm Tests (see Section 13.2 and Chapter 18) in directories flint/test and flint/test/testvals Libraries in 80x86 assembler (see Chapter 19) in directories flint/lib and flint/lib/dll . www.it-ebooks.info Cryptography in C and C+ + MICHAEL WELSCHENBACH Translated by DAVID KRAMER Apress TM www.it-ebooks.info Cryptography in C and C+ + Copyright c 2005 by Michael Welschenbach Translator and. Congress Cataloging -in- Publication Data Welschenbach, Michael. [Kryptographie in C und C+ +. English] Cryptography in C and C+ + / Michael Welschenbach ; translated by David Kramer.– 2nd American. Maryland, and his M.A. in music at Smith College. For many years he worked in higher education, first as a professor of mathematics and computer science, and later as a director of academic computing.

Ngày đăng: 24/04/2014, 15:02

Từ khóa liên quan

Mục lục

  • Cryptography in C and C++: See what it takes to build an industrial-strength crypto API, Second Edition

  • Contents

  • List of Tables

  • Foreword

  • About the Author

  • Preface to the Second American Edition

  • Preface to the First American Edition

  • Preface to the First German Edition

  • Part I - Arithmetic and Number Theory in C

    • 1. Introduction

    • 2. Number Formats: Representation of Large Numbers in C

    • 3. Interface Semantics

    • 4. The Fundamental Operations

    • 5. Modular Arithmetic: Calculating with Residue Classes

    • 6. Where All Roads Meet: Modular Exponentiation

    • 7. Bitwise and Logical Functions

    • 8. Input, Output, Assignment, Conversion

    • 9. Dynamic Registers

    • 10. Basic Number-Theoretic Functions

    • 11. Rijndael: A Successor to the Data Encryption Standard

    • 12. Large Random Numbers

Tài liệu cùng người dùng

Tài liệu liên quan