brintos

brintos / llvm-project-archived public Read only

0
0
Text · 3.6 KiB · 4daeb69 Raw
95 lines · c
1/*2  Name:     rsamath.h3  Purpose:  Implements part of PKCS#1, v. 2.1, June 14, 2002 (RSA Labs)4  Author:   M. J. Fromberger5 6  Copyright (C) 2002-2008 Michael J. Fromberger, All Rights Reserved.7 8  Permission is hereby granted, free of charge, to any person obtaining a copy9  of this software and associated documentation files (the "Software"), to deal10  in the Software without restriction, including without limitation the rights11  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell12  copies of the Software, and to permit persons to whom the Software is13  furnished to do so, subject to the following conditions:14 15  The above copyright notice and this permission notice shall be included in16  all copies or substantial portions of the Software.17 18  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR19  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,20  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE21  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER22  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,23  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE24  SOFTWARE.25 */26 27#ifndef RSAMATH_H_28#define RSAMATH_H_29 30#include "imath.h"31 32#ifdef __cplusplus33extern "C" {34#endif35 36/* Function to fill a buffer with nonzero random bytes */37typedef void (*random_f)(unsigned char *, int);38 39/* Convert integer to octet string, per PKCS#1 v.2.1 */40mp_result rsa_i2osp(mp_int z, unsigned char *out, int len);41 42/* Convert octet string to integer, per PKCS#1 v.2.1 */43mp_result rsa_os2ip(mp_int z, unsigned char *in, int len);44 45/* The following operations assume that you have converted your keys46   and message data into mp_int values somehow.                      */47 48/* Primitive RSA encryption operation */49mp_result rsa_rsaep(mp_int msg, mp_int exp, mp_int mod, mp_int cipher);50 51/* Primitive RSA decryption operation */52mp_result rsa_rsadp(mp_int cipher, mp_int exp, mp_int mod, mp_int msg);53 54/* Primitive RSA signing operation */55mp_result rsa_rsasp(mp_int msg, mp_int exp, mp_int mod, mp_int signature);56 57/* Primitive RSA verification operation */58mp_result rsa_rsavp(mp_int signature, mp_int exp, mp_int mod, mp_int msg);59 60/* Compute the maximum length in bytes a message can have using PKCS#161   v.1.5 encoding with the given modulus */62int       rsa_max_message_len(mp_int mod);63 64/* Encode a raw message per PKCS#1 v.1.565   buf      - the buffer containing the message66   msg_len  - the length in bytes of the message67   buf_len  - the size in bytes of the buffer68   tag      - the message tag (nonzero byte)69   filler   - function to generate pseudorandom nonzero padding70 71   On input, the message is in the first msg_len bytes of the buffer;72   on output, the contents of the buffer are replaced by the padded73   message.  If there is not enough room, MP_RANGE is returned.74 */75mp_result rsa_pkcs1v15_encode(unsigned char *buf, int msg_len, 76			      int buf_len, int tag, random_f filler);77 78/* Decode a PKCS#1 v.1.5 message back to its raw form 79   buf      - the buffer containing the encoded message80   buf_len  - the length in bytes of the buffer81   tag      - the expected message tag (nonzero byte)82   msg_len  - on output, receives the length of the message content83   84   On output, the message is packed into the first msg_len bytes of85   the buffer, and the rest of the buffer is zeroed.  If the buffer is86   not of the correct form, MP_UNDEF is returned and msg_len is undefined.87 */88mp_result rsa_pkcs1v15_decode(unsigned char *buf, int buf_len, 89			      int tag, int *msg_len);90 91#ifdef __cplusplus92}93#endif94#endif /* end RSAMATH_H_ */95