1 /*
2  *
3  *  Copyright (C) 1998-2019, OFFIS e.V.
4  *  All rights reserved.  See COPYRIGHT file for details.
5  *
6  *  This software and supporting documentation were developed by
7  *
8  *    OFFIS e.V.
9  *    R&D Division Health
10  *    Escherweg 2
11  *    D-26121 Oldenburg, Germany
12  *
13  *
14  *  Module: dcmsign
15  *
16  *  Author: Norbert Loxen, Marco Eichelberg
17  *
18  *  Purpose:
19  *    classes: SiRSA
20  *
21  */
22 
23 #ifndef SIRSA_H
24 #define SIRSA_H
25 
26 #include "dcmtk/config/osconfig.h"
27 
28 #ifdef WITH_OPENSSL
29 
30 #include "dcmtk/dcmsign/sialgo.h"
31 #include "dcmtk/ofstd/oftypes.h"
32 
33 class SiPrivateKey;
34 struct rsa_st;
35 typedef struct rsa_st RSA;
36 
37 /**
38  *  This class implements the RSA public key crypto algorithms.
39  *  @remark this class is only available if DCMTK is compiled with
40  *  OpenSSL support enabled.
41  */
42 
43 class DCMTK_DCMSIGN_EXPORT SiRSA : public SiAlgorithm
44 {
45 public:
46 
47   /** constructor
48    *  @param pointer to public RSA key
49    */
50   SiRSA(RSA *key);
51 
52   /// destructor
53   virtual ~SiRSA();
54 
55   /** creates a signature.
56    *  @param inputHash array of hash key bytes that are to be signed
57    *  @param inputHashSize length of hash key array in bytes
58    *  @param inputHashAlgorithm MAC algorithm used for creation of hash key.
59    *    Required for creation of PKCS#1 RSA signature padding.
60    *  @param outputSignature pointer to array of at least getSize() which must be allocated by caller.
61    *  @param outputSignatureSize returns the number of bytes written to outputSignature.
62    *  @return SI_EC_Normal if successful, errorcode otherwise.
63    */
64   virtual OFCondition sign(
65     const unsigned char *inputHash,
66     unsigned long inputHashSize,
67     E_MACType inputHashAlgorithm,
68     unsigned char *outputSignature,
69     unsigned long &outputSignatureSize);
70 
71   /** verifies a signature.
72    *  @param inputHash array of bytes containing hash key to be verified against signature
73    *  @param inputHashSize length of hash key array in bytes
74    *  @param inputHashAlgorithm MAC algorithm used for creation of hash key.
75    *    Required for creation of PKCS#1 RSA signature padding.
76    *  @param inputSignature array of bytes containing signature to be verified
77    *  @param inputSignatureSize length of signature array in bytes
78    *  @param verified returns whether the signature was successfully verified
79    *  @return SI_EC_Normal if successful, errorcode otherwise.
80    */
81   virtual OFCondition verify(
82     const unsigned char *inputHash,
83     unsigned long inputHashSize,
84     E_MACType inputHashAlgorithm,
85     const unsigned char *inputSignature,
86     unsigned long inputSignatureSize,
87     OFBool &verified);
88 
89   /** returns the size of a block of encrypted/decrypted ciphertext in bytes.
90    *  The result depends on the public key algorithm, key size and padding scheme.
91    *  In general the input to decrypt() or encrypt() must be less than or equal
92    *  to this block size.  The output of decrypt() or encrypt() is always equal
93    *  to this block size.
94    *  @return block size for this public key cryptosystem and key
95    */
96   virtual unsigned long getSize() const;
97 
98   /** returns the type of public key algorithm computed by this object
99    *  @return type of public key algorithm
100    */
101   virtual E_KeyType keyType() const;
102 
103 private:
104 
105   /// private undefined copy constructor
106   SiRSA(SiRSA& arg);
107 
108   /// private undefined copy assignment operator
109   SiRSA& operator=(SiRSA& arg);
110 
111   /// RSA key used for signature/verification
112   RSA *rsa;
113 
114 };
115 
116 #endif
117 #endif
118