⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 rsa.c

📁 linux下从网卡远程启动
💻 C
📖 第 1 页 / 共 3 页
字号:
#ifdef	SAFEBOOT/*	RSA.C - RSA routines for RSAEURO    Copyright (c) J.S.A.Kapp 1994 - 1996.	RSAEURO - RSA Library compatible with RSAREF(tm) 2.0.	All functions prototypes are the Same as for RSAREF(tm).	To aid compatiblity the source and the files follow the	same naming comventions that RSAREF(tm) uses.  This should aid	direct importing to your applications.	This library is legal everywhere outside the US.  And should	NOT be imported to the US and used there.		Note: This seems to be outdated in 2003	All Trademarks Acknowledged.	RSA encryption performed as defined in the PKCS (#1) by RSADSI.		Here: No encryption, decryption only	Revision history		0.90 First revision, code produced very similar to that		of RSAREF(tm), still it worked fine.        0.91 Second revision, code altered to aid speeding up.		Used pointer accesses to arrays to speed up some parts,		mainly during the loops.        1.03 Third revision, Random Structure initialization        double check, RSAPublicEncrypt can now return RE_NEED_RANDOM.	This file has been modified to be used with Etherboot by	Anselm Martin Hoffmeister (anselm at hoffmeister-online dot de)	This happened after permission by RSAEuro.	Changed: Moved everything needed for RSA-decrypt into a single file	and removed lots of code that's not needed for etherboot	Changes (C) April, May 2003 AMH*///#include <string.h>/***************************************************************************** ****************************************************** #include "global.h" */#ifndef PROTOTYPES#define PROTOTYPES 1#endiftypedef unsigned char *POINTER; /* POINTER defines a generic pointer type */typedef unsigned short int UINT2;/* UINT2 defines a two byte word */typedef unsigned long int UINT4;/* UINT4 defines a four byte word */typedef unsigned char BYTE;/* BYTE defines a unsigned character */typedef signed long int signeddigit;/* internal signed value */#ifndef NULL_PTR#define NULL_PTR ((POINTER)0)#endif#ifndef UNUSED_ARG#define UNUSED_ARG(x) x = *(&x);#endif/* PROTO_LIST is defined depending on how PROTOTYPES is defined above.	 If using PROTOTYPES, then PROTO_LIST returns the list, otherwise it	 returns an empty list. */#if PROTOTYPES#define PROTO_LIST(list) list#else#define PROTO_LIST(list) ()#endif/***************************************************************************** ********************************************************** #include "nn.h" *//* Type definitions. */typedef UINT4 NN_DIGIT;typedef UINT2 NN_HALF_DIGIT;/* Constants.	 Note: MAX_NN_DIGITS is long enough to hold any RSA modulus, plus   one more digit as required by R_GeneratePEMKeys (for n and phiN,   whose lengths must be even). All natural numbers have at most   MAX_NN_DIGITS digits, except for double-length intermediate values	 in NN_Mult (t), NN_ModMult (t), NN_ModInv (w), and NN_Div (c).*/#define NN_DIGIT_BITS 32 /* Length of digit in bits */#define NN_HALF_DIGIT_BITS 16 /* Length of digit in bytes */#define NN_DIGIT_LEN (NN_DIGIT_BITS / 8) /* Maximum length in digits */#define MAX_NN_DIGITS \  ((MAX_RSA_MODULUS_LEN + NN_DIGIT_LEN - 1) / NN_DIGIT_LEN + 1)#define MAX_NN_DIGIT 0xffffffff /* Maximum digits */#define MAX_NN_HALF_DIGIT 0xffff#define NN_LT   -1#define NN_EQ   0#define NN_GT 1#define LOW_HALF(x) ((x) & MAX_NN_HALF_DIGIT)#define HIGH_HALF(x) (((x) >> NN_HALF_DIGIT_BITS) & MAX_NN_HALF_DIGIT)#define TO_HIGH_HALF(x) (((NN_DIGIT)(x)) << NN_HALF_DIGIT_BITS)#define DIGIT_MSB(x) (unsigned int)(((x) >> (NN_DIGIT_BITS - 1)) & 1)#define DIGIT_2MSB(x) (unsigned int)(((x) >> (NN_DIGIT_BITS - 2)) & 3)/* CONVERSIONS   NN_Decode (a, digits, b, len)   Decodes character string b into a.   NN_Encode (a, len, b, digits)   Encodes a into character string b.   ASSIGNMENTS   NN_Assign (a, b, digits)        Assigns a = b.   NN_ASSIGN_DIGIT (a, b, digits)  Assigns a = b, where b is a digit.	 NN_AssignZero (a, b, digits)    Assigns a = 0.   NN_Assign2Exp (a, b, digits)    Assigns a = 2^b.        ARITHMETIC OPERATIONS	 NN_Add (a, b, c, digits)        Computes a = b + c.   NN_Sub (a, b, c, digits)        Computes a = b - c.   NN_Mult (a, b, c, digits)       Computes a = b * c.   NN_LShift (a, b, c, digits)     Computes a = b * 2^c.   NN_RShift (a, b, c, digits)     Computes a = b / 2^c.   NN_Div (a, b, c, cDigits, d, dDigits)  Computes a = c div d and b = c mod d.   NUMBER THEORY   NN_Mod (a, b, bDigits, c, cDigits)  Computes a = b mod c.   NN_ModMult (a, b, c, d, digits) Computes a = b * c mod d.   NN_ModExp (a, b, c, cDigits, d, dDigits)  Computes a = b^c mod d.   NN_ModInv (a, b, c, digits)     Computes a = 1/b mod c.   NN_Gcd (a, b, c, digits)        Computes a = gcd (b, c).   OTHER OPERATIONS   NN_EVEN (a, digits)             Returns 1 iff a is even.   NN_Cmp (a, b, digits)           Returns sign of a - b.   NN_EQUAL (a, digits)            Returns 1 iff a = b.   NN_Zero (a, digits)             Returns 1 iff a = 0.	 NN_Digits (a, digits)           Returns significant length of a in digits.   NN_Bits (a, digits)             Returns significant length of a in bits. */void NN_Decode PROTO_LIST  ((NN_DIGIT *, unsigned int, unsigned char *, unsigned int));void NN_Encode PROTO_LIST  ((unsigned char *, unsigned int, NN_DIGIT *, unsigned int));void NN_Assign PROTO_LIST ((NN_DIGIT *, NN_DIGIT *, unsigned int));void NN_AssignZero PROTO_LIST ((NN_DIGIT *, unsigned int));void NN_Assign2Exp PROTO_LIST ((NN_DIGIT *, unsigned int, unsigned int));NN_DIGIT NN_Add PROTO_LIST	((NN_DIGIT *, NN_DIGIT *, NN_DIGIT *, unsigned int));NN_DIGIT NN_Sub PROTO_LIST	((NN_DIGIT *, NN_DIGIT *, NN_DIGIT *, unsigned int));void NN_Mult PROTO_LIST ((NN_DIGIT *, NN_DIGIT *, NN_DIGIT *, unsigned int));void NN_Div PROTO_LIST	((NN_DIGIT *, NN_DIGIT *, NN_DIGIT *, unsigned int, NN_DIGIT *,		unsigned int));NN_DIGIT NN_LShift PROTO_LIST	((NN_DIGIT *, NN_DIGIT *, unsigned int, unsigned int));NN_DIGIT NN_RShift PROTO_LIST	((NN_DIGIT *, NN_DIGIT *, unsigned int, unsigned int));NN_DIGIT NN_LRotate PROTO_LIST 	((NN_DIGIT *, NN_DIGIT *, unsigned int, unsigned int));void NN_Mod PROTO_LIST	((NN_DIGIT *, NN_DIGIT *, unsigned int, NN_DIGIT *, unsigned int));void NN_ModMult PROTO_LIST	((NN_DIGIT *, NN_DIGIT *, NN_DIGIT *, NN_DIGIT *, unsigned int));void NN_ModExp PROTO_LIST	((NN_DIGIT *, NN_DIGIT *, NN_DIGIT *, unsigned int, NN_DIGIT *,		unsigned int));void NN_ModInv PROTO_LIST	((NN_DIGIT *, NN_DIGIT *, NN_DIGIT *, unsigned int));void NN_Gcd PROTO_LIST ((NN_DIGIT *, NN_DIGIT *, NN_DIGIT *, unsigned int));int NN_Cmp PROTO_LIST ((NN_DIGIT *, NN_DIGIT *, unsigned int));int NN_Zero PROTO_LIST ((NN_DIGIT *, unsigned int));unsigned int NN_Bits PROTO_LIST ((NN_DIGIT *, unsigned int));unsigned int NN_Digits PROTO_LIST ((NN_DIGIT *, unsigned int));#define NN_ASSIGN_DIGIT(a, b, digits) {NN_AssignZero (a, digits); a[0] = b;}#define NN_EQUAL(a, b, digits) (! NN_Cmp (a, b, digits))#define NN_EVEN(a, digits) (((digits) == 0) || ! (a[0] & 1))/**************************************************************************** ********************************************************* #include "md2.h" */typedef struct {  unsigned char state[16];                                 /* state */  unsigned char checksum[16];                           /* checksum */  unsigned int count;                 /* number of bytes, modulo 16 */  unsigned char buffer[16];                         /* input buffer */} MD2_CTX;void MD2Init PROTO_LIST ((MD2_CTX *));void MD2Update PROTO_LIST  ((MD2_CTX *, unsigned char *, unsigned int));void MD2Final PROTO_LIST ((unsigned char [16], MD2_CTX *));/**************************************************************************** ********************************************************* #include "md4.h" */typedef struct {	UINT4 state[4];                                   /* state (ABCD) */	UINT4 count[2];        /* number of bits, modulo 2^64 (lsb first) */	unsigned char buffer[64];                         /* input buffer */} MD4_CTX;void MD4Init PROTO_LIST ((MD4_CTX *));void MD4Update PROTO_LIST ((MD4_CTX *, unsigned char *, unsigned int));void MD4Final PROTO_LIST ((unsigned char [16], MD4_CTX *));/**************************************************************************** ********************************************************* #include "md5.h" */typedef struct {  UINT4 state[4];                                   /* state (ABCD) */  UINT4 count[2];        /* number of bits, modulo 2^64 (lsb first) */  unsigned char buffer[64];                         /* input buffer */} MD5_CTX;void MD5Init PROTO_LIST ((MD5_CTX *));void MD5Update PROTO_LIST  ((MD5_CTX *, unsigned char *, unsigned int));void MD5Final PROTO_LIST ((unsigned char [16], MD5_CTX *));/**************************************************************************** ********************************************************* #include "shs.h" *//* The SHS block size and message digest sizes, in bytes */#define SHS_BLOCKSIZE   64#define SHS_DIGESTSIZE  20/* The structure for storing SHS info */typedef struct {	UINT4 digest [5];             /* Message digest */	UINT4 countLo, countHi;       /* 64-bit bit count */	UINT4 data [16];              /* SHS data buffer */} SHS_CTX;void SHSInit PROTO_LIST ((SHS_CTX *));void SHSUpdate PROTO_LIST ((SHS_CTX *, unsigned char *, int ));void SHSFinal PROTO_LIST ((unsigned char *, SHS_CTX *));/**************************************************************************** ********************************************************* #include "des.h" */typedef struct {  UINT4 subkeys[32];                                             /* subkeys */  UINT4 iv[2];                                       /* initializing vector */  UINT4 originalIV[2];                        /* for restarting the context */  int encrypt;                                               /* encrypt flag */} DES_CBC_CTX;typedef struct {  UINT4 subkeys[32];                                             /* subkeys */  UINT4 iv[2];                                       /* initializing vector */  UINT4 inputWhitener[2];                                 /* input whitener */  UINT4 outputWhitener[2];                               /* output whitener */  UINT4 originalIV[2];                        /* for restarting the context */  int encrypt;                                              /* encrypt flag */} DESX_CBC_CTX;typedef struct {  UINT4 subkeys[3][32];                     /* subkeys for three operations */  UINT4 iv[2];                                       /* initializing vector */  UINT4 originalIV[2];                        /* for restarting the context */  int encrypt;                                              /* encrypt flag */} DES3_CBC_CTX;void DES_CBCInit PROTO_LIST   ((DES_CBC_CTX *, unsigned char *, unsigned char *, int));int DES_CBCUpdate PROTO_LIST  ((DES_CBC_CTX *, unsigned char *, unsigned char *, unsigned int));void DES_CBCRestart PROTO_LIST ((DES_CBC_CTX *));void DESX_CBCInit PROTO_LIST  ((DESX_CBC_CTX *, unsigned char *, unsigned char *, int));int DESX_CBCUpdate PROTO_LIST  ((DESX_CBC_CTX *, unsigned char *, unsigned char *, unsigned int));void DESX_CBCRestart PROTO_LIST ((DESX_CBC_CTX *));void DES3_CBCInit PROTO_LIST   ((DES3_CBC_CTX *, unsigned char *, unsigned char *, int));int DES3_CBCUpdate PROTO_LIST  ((DES3_CBC_CTX *, unsigned char *, unsigned char *, unsigned int));void DES3_CBCRestart PROTO_LIST ((DES3_CBC_CTX *));/***************************************************************************** ***************************************************** #include "rsaeuro.h" *//* Message-digest algorithms. */#define DA_MD2 2#define DA_MD4 4#define DA_MD5 5#define DA_SHS 3/* Encryption algorithms to be ored with digest algorithm in Seal and Open. */#define EA_DES_CBC 1#define EA_DES_EDE2_CBC 2#define EA_DES_EDE3_CBC 3#define EA_DESX_CBC 4/* RSA key lengths. */#define MIN_RSA_MODULUS_BITS 508#define MAX_RSA_MODULUS_BITS 1024#define MAX_RSA_MODULUS_LEN ((MAX_RSA_MODULUS_BITS + 7) / 8)#define MAX_RSA_PRIME_BITS ((MAX_RSA_MODULUS_BITS + 1) / 2)#define MAX_RSA_PRIME_LEN ((MAX_RSA_PRIME_BITS + 7) / 8)/* Maximum lengths of encoded and encrypted content, as a function of	 content length len. Also, inverse functions. */#define ENCODED_CONTENT_LEN(len) (4*(len)/3 + 3)#define ENCRYPTED_CONTENT_LEN(len) ENCODED_CONTENT_LEN ((len)+8)#define DECODED_CONTENT_LEN(len) (3*(len)/4 + 1)#define DECRYPTED_CONTENT_LEN(len) (DECODED_CONTENT_LEN (len) - 1)/* Maximum lengths of signatures, encrypted keys, encrypted	 signatures, and message digests. */#define MAX_SIGNATURE_LEN MAX_RSA_MODULUS_LEN#define MAX_PEM_SIGNATURE_LEN ENCODED_CONTENT_LEN(MAX_SIGNATURE_LEN)#define MAX_ENCRYPTED_KEY_LEN MAX_RSA_MODULUS_LEN#define MAX_PEM_ENCRYPTED_KEY_LEN ENCODED_CONTENT_LEN(MAX_ENCRYPTED_KEY_LEN)#define MAX_PEM_ENCRYPTED_SIGNATURE_LEN ENCRYPTED_CONTENT_LEN(MAX_SIGNATURE_LEN)#define MAX_DIGEST_LEN 20/* Maximum length of Diffie-Hellman parameters. */#define DH_PRIME_LEN(bits) (((bits) + 7) / 8)/* Error codes. */#define RE_CONTENT_ENCODING 0x0400#define RE_DATA 0x0401#define RE_DIGEST_ALGORITHM 0x0402#define RE_ENCODING 0x0403#define RE_KEY 0x0404#define RE_KEY_ENCODING 0x0405#define RE_LEN 0x0406#define RE_MODULUS_LEN 0x0407#define RE_NEED_RANDOM 0x0408#define RE_PRIVATE_KEY 0x0409#define RE_PUBLIC_KEY 0x040a#define RE_SIGNATURE 0x040b#define RE_SIGNATURE_ENCODING 0x040c#define RE_ENCRYPTION_ALGORITHM 0x040d#define RE_FILE 0x040e/* Library details. */#define RSAEURO_VER_MAJ 1#define RSAEURO_VER_MIN 04#define RSAEURO_IDENT "RSAEURO Toolkit"#define RSAEURO_DATE "21/08/94"/* Internal Error Codes *//* IDOK and IDERROR changed to ID_OK and ID_ERROR */#define ID_OK    0#define ID_ERROR 1/* Internal defs. */#define TRUE    1#define FALSE   0/* Algorithm IDs */#define IA_MD2 0x00000001#define IA_MD4 0x00000002#define IA_MD5 0x00000004#define IA_SHS 0x00000008#define IA_DES_CBC 0x00000010#define IA_DES_EDE2_CBC 0x00000020#define IA_DES_EDE3_CBC 0x00000040#define IA_DESX_CBC 0x00000080#define IA_RSA 0x00010000#define IA_DH  0x00020000#define IA_FLAGS (IA_MD2|IA_MD4|IA_MD5|IA_SHS|IA_DES_CBC|IA_DES_EDE2_CBC|IA_DES_EDE3_CBC|IA_DESX_CBC|IA_RSA|IA_DH)/* RSAEuro Info Structure */typedef struct {    unsigned short int Version;                 /* RSAEuro Version */    unsigned int flags;                         /* Version Flags */    unsigned char ManufacturerID[32];           /* Toolkit ID */    unsigned int Algorithms;                    /* Algorithms Supported */} RSAEUROINFO;/* Random structure. */

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -