Team-Fly Cryptography in C and C++ ISBN:189311595x by Michael Welschenbach Apress ?2001 (432 pages) A behind-the-scenes examination of binary cryptography, from the simple "modulo" and shift operations to complex algorithms like RSA Table of Contents Cryptography in C and C++ Preface to - the Translation of the Second Edition Preface to - the First Edition Part I - Arithmetic and Number Theory in C Chapter 1 Chapter 2 Chapter 3 Chapter 4 Chapter 5 Chapter 6 - Introduction Number Formats: The Representation of Large Numbers in C - Interface Semantics - The Fundamental Operations Modular Arithmetic: Calculating with Residue Classes Where All Roads Meet: Modular Exponentiation Chapter 7 Chapter 8 Chapter 9 Chapter 10 Chapter 11 Chapter 12 - Bitwise and Logical Functions - Input, Output, Assignment, Conversion - Dynamic Registers - Basic Number-Theoretic Functions - Large Random Numbers - Strategies for Testing LINT Part II - Arithmetic in C++ with the Class LINT Chapter 13 - Let C++ Simplify Your Life The LINT Public Interface: Members and Chapter 14 Friends Chapter 15 - Error Handling An Application Example: The RSA Chapter 16 Cryptosystem Chapter 17 - Do It Yourself: Test LINT Chapter 18 - Approaches for Further Extensions Rijndael: A Successor to the Data Chapter 19 Encryption Standard Part III - Appendices Appendix A - Directory of C Functions Appendix B - Directory of C++ Functions Appendix C - Macros Appendix D - Calculation Times Appendix E - Notation Appendix F - Arithmetic and Number-Theoretic Packages References Index List of Figures List of Tables List of Listings Team-Fly Team-Fly Back Cover Detailed treatment of public key cryptography with detailed coverage of the RSA algorithm that is now in the public domain Explains and includes an implementation of Rijndael, the new American Encryption Standard in both C and C++ Not toy code(tm), but a practical treatment of modern cryptographic methods Cryptography in C and C++ mainly focuses on the practical aspects involved in implementing public key cryptography methods, such as the RSA algorithm that was recently released from patent protection It also gives both a technical overview and an implementation of the Rijndael algorithm that was selected as the Advanced Encryption Standard by the U.S government Welschenbach avoids complexities by explaining cryptography and its mathematical basis in terms a programmer can easily understand This book offers a comprehensive, yet relentlessly practical overview of the fundamentals of modern cryptography It includes a wide-ranging library of code in C and C++, including the RSA algorithm, completed by an extensive Test Suite that proves that the code works correctly Readers will learn, step by step, how to implement a platform independent library for the all-important multi-precision arithmetic used in modern cryptography This is followed by an implementation of the cryptographic algorithms themselves About the Author Michael Welschenbach currently works for SRC Security Research & Consulting GmbH in Bonn, Germany He graduated with a Master's Degree in Mathematics from the University of Cologne and has gained extensive experience in cryptological research over the years Currently, his favorite programming languages are C and C++ When not working, he enjoys spending time with his wife and two sons, programming, reading and music Team-Fly Team-Fly Cryptography in C and C++ MICHAEL WELSCHENBACH Translated by DAVID KRAMER Copyright © 2001 by Michael Welschenbach 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 ISBN (pbk): 1-893115-95-X 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 Editorial Directors: Dan Appleman, Gary Cornell, Karen Watterson Translator and Compositor: David Kramer Proofreader: Anne Friedman TEX Support: Fred Bartlett and Arthur Ogawa Cover Designer: Karl Miyajima Distributed to the book trade in the United States by Springer-Verlag New York, Inc.,175 Fifth Avenue, New York, NY, 10010 and outside the United States by Springer-Verlag GmbH & Co KG, Tiergartenstr 17, 69112 Heidelberg, Germany In the United States, phone 1-800-SPRINGER; ; http://www.springer-ny.com Outside the United States, contact ; http://www.springer.de; fax +49 6221 345229 For information on translations, please contact Apress directly at 901 Grayson Street, Suite 204, Berkeley, CA, 94710 Phone: 510-549-5937; Fax: 510-549-5939; ; 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 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 For Helga, Daniel, and Lukas, from whom I borrowed the time for writing this book Team-Fly Team-Fly Preface to the Translation of the Second Edition When I have to wrestle with figures, I feel I'd like to stuff myself into a hole in the ground, so I can't see anything If I raise my eyes and see the sea, or a tree, or a woman—even if she's an old 'un—damme if all the sums and figures don't go to blazes They grow wings and I have to chase 'em —Nikos Kazanzakis, Zorba the Greek THE SECOND EDITION has been revised and expanded in a number of ways Additional examples of cryptographic algorithms have been added, such as the procedures of Rabin and El Gamal, and in the realization of the RSA procedure the hash function RIPEMD-160 and formatting according to PKCS #1 have been adopted There is also a discussion of possible sources of error that could lead to a weakening of the procedure The text has been expanded or clarified at a number of points, and errors have been corrected Additionally, certain didactic strategies have been strengthened, with the result that some of the programs on the CD-ROM differ in certain details from those presented in the book Not all technical details are of equal importance, and the desire for fast and efficient code is not always compatible with attractive and easy-to-read programs And speaking of efficiency, in Appendix D running times are compared to those for certain functions in the GNU Multiprecision Library In this comparison the FLINT/C exponentiation routine did not do at all badly As a further extension, Appendix F provides references to some arithmetic and number-theoretic packages The software has been 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 security-critical variables in the individual functions are deleted by being overwritten All C and C++ functions are now clearly cited and annotated in the appendices Chapter 5: Modular Arithmetic: Calculating with Residue Classes Table 5.1: Composition table for addition modulo 5 Table 5.2: Composition table for multiplication modulo 5 Chapter 6: Where All Roads Meet: Modular Exponentiation Table 6.1: Requirements for exponentiation Table 6.2: Numbers of multiplications for typical sizes of exponents and various bases 2k Table 6.3: Values for the factorization of the exponent digits into products of a power of 2 and an odd factor Table 6.4: Numbers of multiplications for typical sizes of exponents and various bases 2k Table 6.5: Exponentiation functions in FLINT/C Chapter 7: Bitwise and Logical Functions Table 7.1: Values of a Boolean function Table 7.2: Values of the CLINT function and_l() Table 7.3: Values of the CLINT function or_l() Table 7.4: Values of the CLINT function xor_l() Chapter 8: Input, Output, Assignment, Conversion Table 8.1: Diagnostic values of the function vcheck_l() Chapter 10: Basic Number-Theoretic Functions Table 10.1: The largest known primes (as of August 2000) Table 10.2: The number of primes up to various limits x Chapter 12: Strategies for Testing LINT Table 12.1: Group law for the integers to help in testing Table 12.2: FLINT/C test functions Chapter 13: Let C++ Simplify Your Life Table 13.1: LINT constructors Table 13.2: LINT arithmetic operators Table 13.3: LINT bitwise operators Table 13.4: LINT logical operators Table 13.5: LINT assignment operators Chapter 14: The LINT Public Interface: Members and Friends Table 14.1: LINT status functions and their effects Table 14.2: LINT manipulators and their effects Table 14.3: LINT flags for output formatting and their effects Chapter 15: Error Handling Table 15.1: LINT function error codes Chapter 16: An Application Example: The RSA Cryptosystem Table 16.1: Recommended key lengths according to Lenstra and Verheul Chapter 19: Rijndael: A Successor to the Data Encryption Standard Table 19.1: Elements of Table 19.2: Powers of g(x) = x + 1 Table 19.3: Logarithms to base g(x) = x + 1 (e.g., logg(x) 3 = 25, logg(x) 255 = 7) Table 19.4: Number of Rijndael rounds as a function of block and key length Table 19.5: Representation of message blocks Table 19.6: rc(j) constants (hexadecimal) Table 19.7: rc(j) constants (binary) Table 19.8: Representation of the round keys Table 19.9: The values of the S-box Table 19.10: The values of the inverted S-box Table 19.11: ShiftRow for blocks of length 128 bits (Lb = 4) Table 19.12: ShiftRow for blocks of length 192 bits (Lb = 6) Table 19.13: ShiftRow for blocks of length 256 bits (Lb = 8) Table 19.14: Distances of line rotations in ShiftRow Table 19.15: Interpretation of variables Table 19.16: Interpretation of fields Table 19.17: Interpretation of functions Appendix D: Calculation Times Table D.1: Calculation times for several C functions (without assembler support) Table D.2: Calculation times for several C functions (with 80×86 assembler support) Table D.3: Calculation times for several GMP functions (with 80×86 assembler support) Team-Fly Team-Fly List of Listings Chapter 15: Error Handling Example 1: Use of an uninitialized LINT object as argument Example 2: Division by a LINT object with the value 0 Team-Fly ... modern cryptographic methods Cryptography in C and C+ + mainly focuses on the practical aspects involved in implementing public key cryptography methods, such as the RSA algorithm that was recently released from patent protection... problems as calculating the Legendre and Jacobi symbols, and inverses and square roots in finite rings, and we shall also become familiar with the Chinese remainder theorem and its applications In connection with this we shall go into some detail about the principles of... detailed coverage of the RSA algorithm that is now in the public domain Explains and includes an implementation of Rijndael, the new American Encryption Standard in both C and C+ + Not toy code(tm), but a practical treatment of modern cryptographic methods