Crypto-C
Cryptographic Components for C
Distribution
Trademarks
License agreement
Limit distribution of this document to trusted personnel
Contents
Quick Start
Cryptography
N t e n t s
Using Crypto-C 101
Algorithms in Crypto-C
Non-Cryptographic Operations 151
Saved State
Symmetric-Key Operations 177
Public-Key Operations 213
Secret Sharing Operations 305
Putting It All Together An X9.31 Example
Appendix a Command-Line Demos 327
Glossary 339 Index 349
List of Figures
A B S a F E C r y p t o C D e v e l o p e r ’s G u i d e
List of Tables
A B S a F E C r y p t o C D e v e l o p e r ’s G u i d e
Preface
Hardware support
What’s New in Version 5.2.2?
Improved performance
MultiPrime RSA
Organization of This Manual
Organization of This Manual
Advanced Encryption Standard AES
E f a c e
Crypto-C procedures to use with algorithm object
Conventions Used in This Manual
Conventions Used in This Manual
Terms and Abbreviations
Terms and Abbreviations
Related Documents
Related Documents
A B S a F E C r y p t o C D e v e l o p e r ’s G u i d e
Ieee Standard Specifications for Public-Key Cryptography on
Http//stdsbbs.ieee.org/groups/1363/index.html
How to Contact RSA Security
How to Contact RSA Security
RSA Security Web Site Getting Support and Service
You can get technical support as follows
Introduction
Crypto-C Toolkit
Algorithms
Elliptic Curve Public-Key Algorithms
Public-Key Algorithms
Digital Signatures
Secret Sharing
Nist Standards and Crypto-C
Cryptographic Standards and Crypto-C
Pkcs Standards and Crypto-C
Nist Approval and Windows 32-bit Platforms
Pkcs Compared with Nist
Nist Approval and Windows NT Platforms
Ansi X9 Standards and Crypto-C
Quick Start
Six-Step Sequence
Six-Step Sequence
Creating an Algorithm Object
Introductory Example
Include Files
Introductory Example
Typedef unsigned char *POINTER
Where Pointer is defined in aglobal.h
Nullptr is also defined in aglobal.h
#define Nullptr POINTER0
Setting the Algorithm Object
Balgorithmobj algorithmObject Algorithm object
Algorithm information
Init
Break
Setting a Key Object
Creating a Key Object
For our example, we use
Int BSetKeyInfo
Key information
Selecting an Algorithm Chooser
Now we can complete the call to BSetKeyInfo
Surrender Context
Update
Saving the Object State optional
We can now complete our call to BEncryptInit
Length of output data
Output data buffer
Unsigned int
Input data
Unsigned int outputLenUpdate
For now, we declare
Unsigned char *encryptedData = Nullptr
If status = BEncryptUpdate
Final
Destroy
Asurrenderctx *surrenderContext Surrender context
Balgorithmobj * algorithmObject
For our example, we use the following
Pointer to key object
Pointer block
Tfree encryptedData
Putting It All Together
For this example, call Tfree as follows
If rc4KeyItem.data != Nullptr
Init If status = BEncryptInit
Introductory Example
If encryptedData != Nullptr
EncryptedData = Nullptr End main
Decrypting the Introductory Example
Decrypting the Introductory Example
Creating the Key Object
First create the algorithm object
Setting the Key Object
Always destroy objects when you no longer need them
DecryptedData = Nullptr
Multiple Updates
Multiple Updates
Updatesize
Break End while
Multiple Updates
Create
Summary of the Six Steps
Include
Set
Final
Page
Cryptography
Ciphers
Cryptography Overview
Symmetric-Key Cryptography
Cryptography Overview
Ciphers in Crypto-C
Block Ciphers
Padding
Crypto-C implements the following block ciphers
Triple DES
2Triple DES Encryption as Implemented in Crypto-C
RC5
RC6
Modes of Operation
Four Modes
Electronic Codebook ECB Mode
3Electronic Codebook ECB Mode
Cipher Block Chaining CBC Mode
Cipher Feedback CFB Mode
5Cipher Feedback CFB Mode
Output Feedback OFB Mode
6Output Feedback Mode OFB
Stream Ciphers
Message Digests
RC4 algorithm with MAC
Message Digests and Pseudo-Random Numbers
Password-Based Encryption
Hash-Based Message Authentication Codes Hmac
8DES Key and IV Generation for Password Based Encryption
Public-Key Cryptography
RSA Algorithm
9Public-Key Cryptography
MultiPrime Numbers
Modular Math
Prime Numbers
RSA Algorithm
Cryptography Overview
Calculation is shown in Table
Summary
Security
1Calculation of 827 mod
Digital Envelopes
Optimal Asymmetric Encryption Padding Oaep
10Digital Envelope
Authentication and Digital Signatures
Cryptography Overview
11RSA Digital Signature
Digital Signature Algorithm DSA
Digital Certificates
Math
Diffie-Hellman Public Key Agreement
Algorithm
Phase
Phase
Parameter Generation
Math
Elliptic Curve Cryptography
Multiple-Party Key Agreement
Elliptic Curve Parameters
Finite Field
Fields of Even Characteristic
Odd Prime Fields
Coefficients Over an Odd Prime Field
= 0·I ≡ 2·2m-1·Imod2m
Points of an Elliptic Curve
Coefficients Over a Field of Even Characteristic
Point P and its Order
Elliptic curve parameters
Point of Prime Order
Order of an Elliptic Curve
Order of a Point
Is written EFq
Order n of P Is sometimes called the base point
Summary of Elliptic Curve Terminology
Cofactor
2Elliptic Curve Parameters
Representing Fields of Even Characteristic
Elliptic Curve Key Pair Generation
Ecdsa Signature Scheme
Creating the Key Pair
Signing a Message
Verifying a Signature
Math
Elliptic Curve Authenticated Encryption Scheme Ecaes
Recall that Q = dP, so
Now recall that s = k-1e+dr mod n, so
Encrypting a Message Using the Public Key
Decrypting a Message Using the Private Key
Elliptic Curve Diffie-Hellman Key Agreement
Phase
13Elliptic Curve Diffie-Hellman Key Agreement
Secret Sharing
First coordinate of S, xS, is their agreed-upon secret key
Working with Keys
Key Generation
Key Management
Key Escrow
Local Applications
Applications of Cryptography
Ascii Encoding and Decoding
Applications of Cryptography
Point-to-Point Applications
Client/Server Applications
Peer-to-Peer Applications
Stream vs. Block Symmetric-Key Algorithms
Choosing Algorithms
Public-Key vs. Symmetric-Key Cryptography
Choosing Algorithms
Block Symmetric-Key Algorithms
Key Agreement vs. Digital Envelopes
Secret Sharing and Key Escrow
Elliptic Curve Algorithms
Interoperability
Elliptic Curve Standards
Security Considerations
Handling Private Keys
Security Considerations
Temporary Buffers
Pseudo-Random Numbers and Seed Generation
Choosing Passwords
Initialization Vectors and Salts
DES Weak Keys
3DES Weak and Semi-Weak Keys
Stream Ciphers
Timing Attacks and Blinding
Pick a random value r and compute
= mre mod n
Choosing Key Sizes
MD5p padP MD5q padQ m
RSA Keys
4Summary of Recommended Key Sizes
RC4 Key Bits
Diffie-Hellman Parameters and DSA Keys
RC2 Effective Key Bits
RC5 Key Bits and Rounds
Elliptic Curve Keys
Using Crypto-C
Algorithms in Crypto-C
BER-Based Algorithm Info Types
Information Formats Provided by Crypto-C
Basic Algorithm Info Types
Algorithms in Crypto-C
BSAFE1 Algorithm Info Types
Summary of AIs
PEM-Based Algorithm Info Types
1Message Digests
4Pseudo-Random Number Generation
Algorithms in Crypto-C 2Message Authentication
3ASCII Encoding
5Symmetric Stream Ciphers
Algorithms in Crypto-C 5Symmetric Stream Ciphers
6Symmetric Block Ciphers
Algorithms in Crypto-C 6Symmetric Block Ciphers
RC2
7RSA Public-Key Cryptography
Key Generation
Algorithms in Crypto-C 7RSA Public-Key Cryptography
Oaep
8DSA Public-Key Cryptography
Digital Signatures
Algorithms in Crypto-C 9Diffie-Hellman Key Agreement
10Elliptic Curve Public-Key Cryptography
10 Elliptic Curve Public-Key Cryptography
11Bloom-Shamir Secret Sharing
12Hardware Interface
Algorithms in Crypto-C 13Advanced Encryption Standard AES
Algorithm Info Type
Keys In Crypto-C
Keys In Crypto-C
Summary of KIs
15Block Cipher Keys
Keys In Crypto-C 15Block Cipher Keys
16RSA Public and Private Keys
17DSA Public and Private Keys
Keys In Crypto-C 18Elliptic Curve Keys
An Encryption Algorithm Chooser
System Considerations In Crypto-C
Algorithm Choosers
System Considerations In Crypto-C
An RSA Algorithm Chooser
Surrender Context
Description of AIX962RandomV0 instead of AISHA1Random
Reserved for future use
Sample Surrender Function
Also gives the form that a surrender function must have
Int *Surrender Pointer handle
Saving State
When to Allocate Memory
Crypto-C uses the following memory-management routines
Memory-Management Routines
Memory-Management Routines and Standard C Libraries
Tmalloc Trealloc Tfree Tmemset Tmemcpy Tmemmove Tmemcmp
BER/DER Encoding
Memory Allocation
Binary Data
Typedef struct unsigned char *data unsigned int len
System Considerations In Crypto-C
Input constraints
Input and Output
Symmetric Block Algorithms
Output considerations
20Input Limits for RSA Pkcs Encryption
General Considerations
Item pubKeyAccelTableItem unsigned int maxTableLen
Item pubKeyAccelTableItem
Performing EC Diffie-Hellman Key Agreement
Build the public-key acceleration table
GeneralFlag = If status = BBuildTableFinal
Create
Optional Set Acceleration Table Info
If status = alicePublicValue == Nullptr != 0 break
Performing Ecdsa in Compliance with Ansi
If status = aliceSecretValue == Nullptr != 0 break
Generating EC Parameters
Generating an EC Key Pair
Computing a Digital Signature Create
Build an algorithm chooser with the appropriate AMs
Item aTableItem
Now, using BSignUpdate, pass in the data to be signed
Aecparams *ecParamInfo
If status = signature == Nullptr != 0 break
Use the same AI and digestInfo as you did for signing
Initialized random algorithm in BSignFinal
Destroy all objects that are no longer needed
Unsigned int signatureLen
Optional Set Public Key Acceleration Table Info
Performing Ecdsa with X9.62-Compliant BER
0x52 0xd1
Item stockECParamsBER
ECParamsBER154 = 0x30 0x81
0x7a
StockECParamsBER.data = ECParamsBER stockECParamsBER.len =
0x73
DataToSign = 0x53
0x68
0x79
Aecparams *ecParamsInfo
Use the same AI as you did for signing
Using Ecaes
Tfreesignature BDestoryAlgorithmObject &ecDSAVerify
Using Elliptic Curve Parameters
Using an EC Key Pair
Ecaes Public-Key Encryption
Optional Acceleration Table
Now, pass this information into your algorithm object
Item accelerationTableItem
Break FieldElementLen = ecParamInfo-fieldElementBits + 7
Final
Ecaes Private-Key Decryption
Steps for decryption are similar to those for encryption
Create an algorithm object
MaxDecryptedDataLen = outputLenTotal
BDecryptUpdate
OutputLenUpdate
= TmallocmaxDecryptedDataLen
Page
Secret Sharing
Generating Shares
Secret Sharing
Example in this section corresponds to the file scrtshar.c
#define Secretsize 16 #define Totalshares
Unsigned int outputLenFinal If status = BEncryptFinal
If status = secretSharecount == Nullptr != 0 break
Break If status != 0 break
Tfree secretSharecount
Reconstructing the Secret
Use the same AI, AIBSSecretSharing
Asurrenderctx *NULLPTR != 0 break If status != 0 break
BDestroyAlgorithmObject &secretReconstructer
Page
Putting It All Together An X9.31 Example
X9.31 Sample Program
X9.31 Sample Program
Bkeyobj privateKey = Bkeyobjnullptr Item randomSeed
Unsigned int status
Generating Random Bytes
Static unsigned char f4Data = 0x01, 0x00
Printf ================================================\n
To create a random algorithm object and set the parameters
Providing the Seed
Generating a Key Pair
Break
Computing a Digital Signature
Printf ============================= \n
RsaVerifyX931. For signing, use rsaSignX931
AX931PARAMS
Break Init
Verifying the Signature
If status !=
Surrendering Control
Return End GeneralSurrenderFunction
Printing the Buffer Contents
Overview of the Demos
Appendix a
Input Redirection mode
Command-Line Demo User’s Guide
Command Line mode
Command-Line Demo User’s Guide
Using Bdemo
Specifying User Keys
Sign a File
Create a File Envelope
Verify a Signed File
Open a File Envelope
Generate a Key Pair
Using Bdemodsa
Running Bdemodsa
Enter any arbitrary string of printable characters
Sign a File
Using Bdemoec
Running Bdemoec
File Reference
File Reference
Table A-1Demo Program Source Files
BSLite
BSLite Table A-1Demo Program Source Files
BSLite
Page
Glossary
American National Standards Institute
Advanced Encryption Standard
Series of steps used to complete a task
Application Programming Interface
O s s a r y
See ECC
Generic security service application program interface
Electronic business Data Interchange
See XOR
Act of creating a key
Process of having a third party hold onto encryption keys
Process that creates a larger key from the original key
An algorithm that generates the subkeys in a block cipher
Extra bits concatenated with a key, password, or plaintext
Data to be encrypted
Private key in the RSA public-key cryptosystem
Number extracted from a pseudo- random sequence
Secure Multipurpose Internet Mail Extensions
See secret key
Simple Mail Transfer Protocol
Secure Wide Area Network
Trusted Computer System Evaluation Criteria
XOR
Page
Index
Ecdsa
D e
A B S a F E C r y p t o C D e v e l o p e r ’s G u i d e
D e
See also RC4 subprime