summaryrefslogtreecommitdiff
path: root/FreeRTOS-Plus/Demo/corePKCS11_Windows_Simulator/examples/mechanisms_and_digests.c
blob: c5472c4f348dd37bdffb1ac767f2b50a31eb90cd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
/*
 * FreeRTOS V202112.00
 * Copyright (C) 2020 Amazon.com, Inc. or its affiliates.  All Rights Reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy of
 * this software and associated documentation files (the "Software"), to deal in
 * the Software without restriction, including without limitation the rights to
 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
 * the Software, and to permit persons to whom the Software is furnished to do so,
 * subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 *
 * https://www.FreeRTOS.org
 * https://github.com/FreeRTOS
 *
 */

/* FreeRTOS includes. */
#include "FreeRTOS.h"
#include "task.h"

/* Standard includes. */
#include "stdio.h"

/* PKCS #11 includes. */
#include "core_pkcs11_config.h"
#include "core_pkcs11.h"
#include "pkcs11.h"

/* Demo includes. */
#include "demo_helpers.h"
#include "pkcs11_demos.h"

/**
 * This function details what Cryptoki mechanisms are, how to query a slot's
 * support for them, and how to use those mechanisms to generate a hash of a buffer.
 * This can then be used as a message digest.
 *
 * http://docs.oasis-open.org/pkcs11/pkcs11-base/v2.40/os/pkcs11-base-v2.40-os.html
 * please consult the standard for more information.
 *
 * The standard has grouped the functions presented in this demo as:
 * Slot and token management functions.
 * Message digesting functions.
 *
 */
void vPKCS11MechanismsAndDigestDemo( void )
{
    /*
     * This demo builds upon the demo found in "management_and_rng.c". It borrows
     * code and terminology defined and explained, and it is recommended to complete
     * the "management and rng" demo before this one.
     */
    configPRINTF( ( "\r\nStarting PKCS #11 Mechanisms and Digest Demo.\r\n" ) );

    CK_SESSION_HANDLE hSession = CK_INVALID_HANDLE;
    CK_SLOT_ID * pxSlotId = 0;
    CK_FUNCTION_LIST_PTR pxFunctionList = NULL;
    CK_ULONG ulIndex = 0;
    CK_RV xResult = CKR_OK;

    /* The PKCS #11 standard defines a mechanism to be a "A process for
     * implementing a cryptographic operation." For example the SHA-256 algorithm
     * will be the mechanism used in this demo to perform a digest (hash operation).
     *
     * The mechanism types are defined in "pkcs11t.h", and are prefixed CKM_, to
     * provide a portable way to identify mechanisms.
     */
    CK_MECHANISM_TYPE xMechanismType = 0;

    /* This variable is not directly used, but is instantiated for demonstration
     * purposes. 
     */
    ( void ) xMechanismType;

    /* The CK_MECHANISM_INFO allows the application to retrieve the minimum and
     * maximum key sizes supported by the mechanism (could be in bits or bytes).
     * The structure also has a flags field, that is populated with bit flags
     * for what features the mechanism supports.
     */
    CK_MECHANISM_INFO MechanismInfo = { 0 };

    /* The CK_MECHANISM type contains the mechanism type, as well as a pointer
     * for mechanism parameters and a CK_ULONG indicating the length of the
     * parameters.
     */
    CK_MECHANISM xDigestMechanism = { 0 };

    /* The digest will return a hash of the known SHA-256 hash size, 32 bytes.
     * Please see this page for further explanation of the SHA-256 hash.
     * https://en.wikipedia.org/wiki/SHA-2
     */
    CK_BYTE xDigestResult[ pkcs11SHA256_DIGEST_LENGTH ] = { 0 };
    CK_ULONG ulDigestLength = pkcs11SHA256_DIGEST_LENGTH;

    CK_BYTE pxKnownMessage[] = "Hello world!";

    vStart( &hSession, &pxSlotId );
    xResult = C_GetFunctionList( &pxFunctionList );
    configASSERT( CKR_OK == xResult );
    configASSERT( pxFunctionList->C_GetMechanismInfo != NULL );
    configASSERT( pxFunctionList->C_DigestInit != NULL );
    configASSERT( pxFunctionList->C_DigestUpdate != NULL );
    configASSERT( pxFunctionList->C_DigestFinal != NULL );

    /*************************** RSA Capabilities ***************************/
    xResult = pxFunctionList->C_GetMechanismInfo( pxSlotId[ 0 ],
                                                  CKM_RSA_PKCS,
                                                  &MechanismInfo );
    configASSERT( CKR_OK == xResult );

    /* Check to see if the slot supports signing. This capability is important
     * because we want to use the Cryptoki API to sign messages, without directly
     * accessing the private key. This concept will be explained further in the
     * "sign_verify.c" demo, but for now we will just check that the slot has the
     * capabilities we need. See https://en.wikipedia.org/wiki/Public-key_cryptography
     * for more information regarding private keys and public keys.
     */
    if( 0 != ( CKF_SIGN & MechanismInfo.flags ) )
    {
        configPRINTF( ( "This Cryptoki library supports signing messages with RSA" \
                        " private keys.\r\n" ) );
    }
    else
    {
        configPRINTF( ( "This Cryptoki library does not support signing messages" \
                        " with RSA private keys.\r\n" ) );
    }

    /* This Cryptoki library assumes that RSA private keys are 2048 bit . */
    configASSERT( MechanismInfo.ulMaxKeySize >= pkcs11RSA_2048_MODULUS_BITS );
    configASSERT( MechanismInfo.ulMinKeySize <= pkcs11RSA_2048_MODULUS_BITS );

    /* Check for pre-padded signature verification support, this feature will
     * be used in the "sign_verify.c" demo.
     */
    xResult = pxFunctionList->C_GetMechanismInfo( pxSlotId[ 0 ],
                                                  CKM_RSA_X_509,
                                                  &MechanismInfo );

    /* If this fails, the slot is not able to verify the signature using
     * a RSA public key. Please see https://en.wikipedia.org/wiki/Public_key_infrastructure
     * for more information regarding PKI (Public Key Infrastructure).
     */
    if( 0 != ( CKF_VERIFY & MechanismInfo.flags ) )
    {
        configPRINTF( ( "This Cryptoki library supports verifying messages with RSA" \
                        " public keys.\r\n" ) );
    }
    else
    {
        configPRINTF( ( "This Cryptoki library does not support verifying messages" \
                        " with RSA public keys.\r\n" ) );
    }

    /* This Cryptoki library assumes that RSA public keys are 2048 bit . */
    configASSERT( MechanismInfo.ulMaxKeySize >= pkcs11RSA_2048_MODULUS_BITS );
    configASSERT( MechanismInfo.ulMinKeySize <= pkcs11RSA_2048_MODULUS_BITS );

    /*************************** ECDSA Capabilities ***************************/
    xResult = pxFunctionList->C_GetMechanismInfo( pxSlotId[ 0 ],
                                                  CKM_ECDSA,
                                                  &MechanismInfo );
    configASSERT( CKR_OK == xResult );

    if( 0 != ( CKF_SIGN & MechanismInfo.flags ) )
    {
        configPRINTF( ( "This Cryptoki library supports signing messages with" \
                        " ECDSA private keys.\r\n" ) );
    }
    else
    {
        configPRINTF( ( "This Cryptoki library does not support signing messages" \
                        " with ECDSA private keys.\r\n" ) );
    }

    if( 0 != ( CKF_VERIFY & MechanismInfo.flags ) )
    {
        configPRINTF( ( "This Cryptoki library supports verifying messages with" \
                        " ECDSA public keys.\r\n" ) );
    }
    else
    {
        configPRINTF( ( "This Cryptoki library does not support verifying" \
                        " messages with ECDSA public keys.\r\n" ) );
    }

    configASSERT( MechanismInfo.ulMaxKeySize >= pkcs11ECDSA_P256_KEY_BITS );
    configASSERT( MechanismInfo.ulMinKeySize <= pkcs11ECDSA_P256_KEY_BITS );

    /************************** Digest Capabilities **************************/
    xResult = pxFunctionList->C_GetMechanismInfo( pxSlotId[ 0 ],
                                                  CKM_SHA256,
                                                  &MechanismInfo );
    configASSERT( CKR_OK == xResult );

    if( 0 != ( CKF_DIGEST & MechanismInfo.flags ) )
    {
        configPRINTF( ( "The Cryptoki library supports the " \
                        "SHA-256 algorithm.\r\n" ) );
    }
    else
    {
        configPRINTF( ( "The Cryptoki library doesn't support the " \
                        "SHA-256 algorithm.\r\n" ) );
    }

    /***************************** Buffer Digest *****************************/
    /* Hash with SHA256 mechanism. */
    xDigestMechanism.mechanism = CKM_SHA256;

    /* Initializes the digest operation and sets what mechanism will be used
     * for the digest. */
    xResult = pxFunctionList->C_DigestInit( hSession,
                                            &xDigestMechanism );
    configASSERT( CKR_OK == xResult );


    /* Pass a pointer to the buffer of bytes to be hashed, and it's size. */
    xResult = pxFunctionList->C_DigestUpdate( hSession,
                                              pxKnownMessage,
                                              /* Strip NULL Terminator. */
                                              sizeof( pxKnownMessage ) - 1 );
    configASSERT( CKR_OK == xResult );

    /* Retrieve the digest buffer. Since the mechanism is a SHA-256 algorithm,
     * the size will always be 32 bytes. If the size cannot be known ahead of time,
     * a NULL value to the second parameter pDigest, will set the third parameter,
     * pulDigestLen to the number of required bytes. */
    xResult = pxFunctionList->C_DigestFinal( hSession,
                                             xDigestResult,
                                             &ulDigestLength );
    configASSERT( CKR_OK == xResult );

    /* This will now print out the digest of the known message. You can compare
     * the hash generated by the Cryptoki library in a UNIX shell by using the
     * command '$ echo -n "{pxKnownMessage}" | shasum -a 256'
     * this command should generate the same hash. */
    configPRINTF( ( "Known message: %s\r\n", ( char * ) pxKnownMessage ) );
    configPRINTF( ( "Hash of known message using SHA256:\r\n" ) );

    for( ulIndex = 0; ulIndex < ulDigestLength; ulIndex++ )
    {
        configPRINTF( ( "%x", xDigestResult[ ulIndex ] ) );
    }
    configPRINTF( ( "\r\n" ) );

    configPRINTF( ( "Finished PKCS #11 Mechanisms and Digest Demo.\r\n" ) );
    vEnd( hSession, pxSlotId );
}