|
|
1.1.1.2 ! root 1: /* C include file for MPI library I/O routines ! 2: ! 3: (c) Copyright 1986 by Philip Zimmermann. All rights reserved. ! 4: The author assumes no liability for damages resulting from the use ! 5: of this software, even if the damage results from defects in this ! 6: software. No warranty is expressed or implied. ! 7: ! 8: These routines are for multiprecision arithmetic I/O functions for ! 9: number-theoretic cryptographic algorithms such as ElGamal, ! 10: Diffie-Hellman, Rabin, or factoring studies for large composite ! 11: numbers, as well as Rivest-Shamir-Adleman (RSA) public key ! 12: cryptography. ! 13: ! 14: The external data representation for RSA messages and keys that ! 15: some of these library routines assume is outlined in a paper by ! 16: Philip Zimmermann, "A Proposed Standard Format for RSA Cryptosystems", ! 17: IEEE Computer, September 1986, Vol. 19 No. 9, pages 21-34. ! 18: Some revisions to this data format have occurred since the paper ! 19: was published. ! 20: ! 21: NOTE: This assumes previous inclusion of "mpilib.h" ! 22: */ ! 23: ! 24: /*--------------------- Byte ordering stuff -------------------*/ ! 25: ! 26: /* XLOWFIRST is defined iff external file format is LSB-first byteorder */ ! 27: /* #define XLOWFIRST */ /* defined if external byteorder is LSB-first */ ! 28: ! 29: #ifdef NEEDSWAP ! 30: #undef NEEDSWAP /* make sure NEEDSWAP is initially undefined */ ! 31: #endif ! 32: ! 33: #ifdef XLOWFIRST ! 34: #ifdef HIGHFIRST ! 35: #define NEEDSWAP /* External/internal byteorder differs, need byte swap */ ! 36: #endif ! 37: #endif ! 38: ! 39: #ifndef XLOWFIRST ! 40: #ifndef HIGHFIRST ! 41: #define NEEDSWAP /* External/internal byteorder differs, need byte swap */ ! 42: #endif ! 43: #endif ! 44: ! 45: ! 46: word16 fetch_word16(byte *buf); ! 47: /* Fetches a 16-bit word from where byte pointer is pointing. ! 48: buf points to external-format byteorder array. */ ! 49: ! 50: byte *put_word16(word16 w, byte *buf); ! 51: /* Puts a 16-bit word to where byte pointer is pointing, and ! 52: returns updated byte pointer. ! 53: buf points to external-format byteorder array. */ ! 54: ! 55: word32 fetch_word32(byte *buf); ! 56: /* Fetches a 32-bit word from where byte pointer is pointing. ! 57: buf points to external-format byteorder array. */ ! 58: ! 59: byte *put_word32(word32 w, byte *buf); ! 60: /* Puts a 32-bit word to where byte pointer is pointing, and ! 61: returns updated byte pointer. ! 62: buf points to external-format byteorder array. */ ! 63: ! 64: /* Note that convert_byteorder does nothing if internal native ! 65: byteorder is already the same as external byteorder. */ ! 66: ! 67: #ifdef NEEDSWAP /* External/internal byteorder differs, need byte swap */ ! 68: #define convert_byteorder(buf,bytecount) hiloswap(buf,bytecount) ! 69: #define mp_convert_order(r) hiloswap(r,units2bytes(global_precision)) ! 70: #else ! 71: #define convert_byteorder(buf,bytecount) /* nil statement */ ! 72: #define mp_convert_order(r) /* nil statement */ ! 73: #endif /* not NEEDSWAP */ ! 74: ! 75: /*------------------ End byte ordering stuff -------------------*/ ! 76: ! 77: #include <string.h> ! 78: ! 79: #define fill0(buffer,count) memset( buffer, 0, count ) ! 80: /* Zero-fill the byte buffer. */ ! 81: ! 82: #ifdef EMBEDDED ! 83: int putchar(int c); /* standard C library function from <stdio.h> */ ! 84: #endif /* EMBEDDED */ ! 85: ! 86: int string_length(char *s); ! 87: /* Returns string length */ ! 88: ! 89: int str2reg(unitptr reg,string digitstr); ! 90: /* Converts a possibly-signed digit string into a large binary number. ! 91: Returns assumed radix, derived from suffix 'h','o',b','.' */ ! 92: ! 93: void putstr(string s); /* Put out null-terminated ASCII string via putchar. */ ! 94: void puthexbyte(byte b); /* Put out byte in ASCII hex via putchar. */ ! 95: void puthexw16(word16 w); /* Put out 16-bit word in hex, high byte first. */ ! 96: ! 97: int display_in_base(string s,unitptr n,short radix); ! 98: /* Display n in any base, such as base 10. Returns number of digits. */ ! 99: ! 100: void mp_display(string s,unitptr r); ! 101: /* Display register r in hex, with prefix string s. */ ! 102: ! 103: word16 checksum(register byteptr buf, register word16 count); ! 104: /* Returns checksum of buffer. */ ! 105: ! 106: void cbc_xor(register unitptr dst, register unitptr src, word16 bytecount); ! 107: /* Performs the XOR necessary for RSA Cipher Block Chaining. */ ! 108: ! 109: void hiloswap(byteptr r1,short numbytes); ! 110: /* Reverses the order of bytes in an array of bytes. */ ! 111: ! 112: short mpi2reg(register unitptr r, register byteptr buf); ! 113: /* Converts to unit array from byte array with bit length prefix word. */ ! 114: ! 115: short reg2mpi(register byteptr buf, register unitptr r); ! 116: /* Converts from unit array to byte array with bit length prefix word. */ ! 117: ! 118: short preblock(unitptr outreg, byteptr inbuf, short bytecount, ! 119: unitptr modulus, byteptr randompad); ! 120: /* Converts plaintext block into form suitable for RSA encryption. */ ! 121: ! 122: short postunblock(byteptr outbuf, unitptr inreg, unitptr modulus); ! 123: /* Converts a just-decrypted RSA block back ! 124: into unblocked plaintext form. */ ! 125: ! 126: ! 127: /****************** end of MPI I/O library ************************/ ! 128:
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.