|
|
1.1 ! 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: #ifdef NEEDSWAP ! 27: #undef NEEDSWAP /* make sure NEEDSWAP is initially undefined */ ! 28: #endif ! 29: ! 30: #ifdef XLOWFIRST ! 31: #ifdef HIGHFIRST ! 32: #define NEEDSWAP /* External/internal byteorder differs, need byte swap */ ! 33: #endif ! 34: #endif ! 35: ! 36: #ifndef XLOWFIRST ! 37: #ifndef HIGHFIRST ! 38: #define NEEDSWAP /* External/internal byteorder differs, need byte swap */ ! 39: #endif ! 40: #endif ! 41: ! 42: ! 43: word16 fetch_word16(byte *buf); ! 44: /* Fetches a 16-bit word from where byte pointer is pointing. ! 45: buf points to external-format byteorder array. */ ! 46: ! 47: byte *put_word16(word16 w, byte *buf); ! 48: /* Puts a 16-bit word to where byte pointer is pointing, and ! 49: returns updated byte pointer. ! 50: buf points to external-format byteorder array. */ ! 51: ! 52: word32 fetch_word32(byte *buf); ! 53: /* Fetches a 32-bit word from where byte pointer is pointing. ! 54: buf points to external-format byteorder array. */ ! 55: ! 56: byte *put_word32(word32 w, byte *buf); ! 57: /* Puts a 32-bit word to where byte pointer is pointing, and ! 58: returns updated byte pointer. ! 59: buf points to external-format byteorder array. */ ! 60: ! 61: /* Note that convert_byteorder does nothing if internal native ! 62: byteorder is already the same as external byteorder. */ ! 63: ! 64: #ifdef NEEDSWAP /* External/internal byteorder differs, need byte swap */ ! 65: #define convert_byteorder(buf,bytecount) hiloswap(buf,bytecount) ! 66: #define mp_convert_order(r) hiloswap(r,units2bytes(global_precision)) ! 67: #else ! 68: #define convert_byteorder(buf,bytecount) /* nil statement */ ! 69: #define mp_convert_order(r) /* nil statement */ ! 70: #endif /* not NEEDSWAP */ ! 71: ! 72: /*------------------ End byte ordering stuff -------------------*/ ! 73: ! 74: #include <string.h> ! 75: ! 76: #define fill0(buffer,count) memset( buffer, 0, count ) ! 77: /* Zero-fill the byte buffer. */ ! 78: ! 79: #ifdef EMBEDDED ! 80: int putchar(int c); /* standard C library function from <stdio.h> */ ! 81: #endif /* EMBEDDED */ ! 82: ! 83: int string_length(char *s); ! 84: /* Returns string length */ ! 85: ! 86: int str2reg(unitptr reg,string digitstr); ! 87: /* Converts a possibly-signed digit string into a large binary number. ! 88: Returns assumed radix, derived from suffix 'h','o',b','.' */ ! 89: ! 90: void putstr(string s); /* Put out null-terminated ASCII string via putchar. */ ! 91: void puthexbyte(byte b); /* Put out byte in ASCII hex via putchar. */ ! 92: void puthexw16(word16 w); /* Put out 16-bit word in hex, high byte first. */ ! 93: ! 94: int display_in_base(string s,unitptr n,short radix); ! 95: /* Display n in any base, such as base 10. Returns number of digits. */ ! 96: ! 97: void mp_display(string s,unitptr r); ! 98: /* Display register r in hex, with prefix string s. */ ! 99: ! 100: word16 checksum(register byteptr buf, register word16 count); ! 101: /* Returns checksum of buffer. */ ! 102: ! 103: void cbc_xor(register unitptr dst, register unitptr src, word16 bytecount); ! 104: /* Performs the XOR necessary for RSA Cipher Block Chaining. */ ! 105: ! 106: void hiloswap(byteptr r1,short numbytes); ! 107: /* Reverses the order of bytes in an array of bytes. */ ! 108: ! 109: short mpi2reg(register unitptr r, register byteptr buf); ! 110: /* Converts to unit array from byte array with bit length prefix word. */ ! 111: ! 112: short reg2mpi(register byteptr buf, register unitptr r); ! 113: /* Converts from unit array to byte array with bit length prefix word. */ ! 114: ! 115: short preblock(unitptr outreg, byteptr inbuf, short bytecount, ! 116: unitptr modulus, byteptr randompad); ! 117: /* Converts plaintext block into form suitable for RSA encryption. */ ! 118: ! 119: short postunblock(byteptr outbuf, unitptr inreg, unitptr modulus); ! 120: /* Converts a just-decrypted RSA block back ! 121: into unblocked plaintext form. */ ! 122: ! 123: ! 124: /****************** end of MPI I/O library ************************/ ! 125:
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.