andre@0: /*
andre@0:  * PKCS #11 FIPS Power-Up Self Test.
andre@0:  *
andre@0:  * This Source Code Form is subject to the terms of the Mozilla Public
andre@0:  * License, v. 2.0. If a copy of the MPL was not distributed with this
andre@0:  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
andre@0: 
andre@0: #include "softoken.h"   /* Required for RC2-ECB, RC2-CBC, RC4, DES-ECB,  */
andre@0:                         /*              DES-CBC, DES3-ECB, DES3-CBC, RSA */
andre@0:                         /*              and DSA.                         */
andre@0: #include "seccomon.h"   /* Required for RSA and DSA. */
andre@0: #include "lowkeyi.h"    /* Required for RSA and DSA. */
andre@0: #include "pkcs11.h"     /* Required for PKCS #11. */
andre@0: #include "secerr.h"
andre@0: 
andre@0: #ifndef NSS_DISABLE_ECC
andre@0: #include "ec.h"         /* Required for ECDSA */
andre@0: #endif
andre@0: 
andre@0: 
andre@0: /* FIPS preprocessor directives for RC2-ECB and RC2-CBC.        */
andre@0: #define FIPS_RC2_KEY_LENGTH                      5  /*  40-bits */
andre@0: #define FIPS_RC2_ENCRYPT_LENGTH                  8  /*  64-bits */
andre@0: #define FIPS_RC2_DECRYPT_LENGTH                  8  /*  64-bits */
andre@0: 
andre@0: 
andre@0: /* FIPS preprocessor directives for RC4.                        */
andre@0: #define FIPS_RC4_KEY_LENGTH                      5  /*  40-bits */
andre@0: #define FIPS_RC4_ENCRYPT_LENGTH                  8  /*  64-bits */
andre@0: #define FIPS_RC4_DECRYPT_LENGTH                  8  /*  64-bits */
andre@0: 
andre@0: 
andre@0: /* FIPS preprocessor directives for DES-ECB and DES-CBC.        */
andre@0: #define FIPS_DES_ENCRYPT_LENGTH                  8  /*  64-bits */
andre@0: #define FIPS_DES_DECRYPT_LENGTH                  8  /*  64-bits */
andre@0: 
andre@0: 
andre@0: /* FIPS preprocessor directives for DES3-CBC and DES3-ECB.      */
andre@0: #define FIPS_DES3_ENCRYPT_LENGTH                 8  /*  64-bits */
andre@0: #define FIPS_DES3_DECRYPT_LENGTH                 8  /*  64-bits */
andre@0: 
andre@0: 
andre@0: /* FIPS preprocessor directives for AES-ECB and AES-CBC.        */
andre@0: #define FIPS_AES_BLOCK_SIZE                     16  /* 128-bits */
andre@0: #define FIPS_AES_ENCRYPT_LENGTH                 16  /* 128-bits */
andre@0: #define FIPS_AES_DECRYPT_LENGTH                 16  /* 128-bits */
andre@0: #define FIPS_AES_128_KEY_SIZE                   16  /* 128-bits */
andre@0: #define FIPS_AES_192_KEY_SIZE                   24  /* 192-bits */
andre@0: #define FIPS_AES_256_KEY_SIZE                   32  /* 256-bits */
andre@0: 
andre@0: 
andre@0: /* FIPS preprocessor directives for message digests             */
andre@0: #define FIPS_KNOWN_HASH_MESSAGE_LENGTH          64  /* 512-bits */
andre@0: 
andre@0: 
andre@0: /* FIPS preprocessor directives for RSA.                         */
andre@0: #define FIPS_RSA_TYPE                           siBuffer
andre@0: #define FIPS_RSA_PUBLIC_EXPONENT_LENGTH           3 /*   24-bits */
andre@0: #define FIPS_RSA_PRIVATE_VERSION_LENGTH           1 /*    8-bits */
andre@0: #define FIPS_RSA_MESSAGE_LENGTH                 256 /* 2048-bits */
andre@0: #define FIPS_RSA_COEFFICIENT_LENGTH             128 /* 1024-bits */
andre@0: #define FIPS_RSA_PRIME0_LENGTH                  128 /* 1024-bits */
andre@0: #define FIPS_RSA_PRIME1_LENGTH                  128 /* 1024-bits */
andre@0: #define FIPS_RSA_EXPONENT0_LENGTH               128 /* 1024-bits */
andre@0: #define FIPS_RSA_EXPONENT1_LENGTH               128 /* 1024-bits */
andre@0: #define FIPS_RSA_PRIVATE_EXPONENT_LENGTH        256 /* 2048-bits */
andre@0: #define FIPS_RSA_ENCRYPT_LENGTH                 256 /* 2048-bits */
andre@0: #define FIPS_RSA_DECRYPT_LENGTH                 256 /* 2048-bits */
andre@0: #define FIPS_RSA_SIGNATURE_LENGTH               256 /* 2048-bits */
andre@0: #define FIPS_RSA_MODULUS_LENGTH                 256 /* 2048-bits */
andre@0: 
andre@0: 
andre@0: /* FIPS preprocessor directives for DSA.                        */
andre@0: #define FIPS_DSA_TYPE                           siBuffer
andre@0: #define FIPS_DSA_DIGEST_LENGTH                  20 /*  160-bits */
andre@0: #define FIPS_DSA_SUBPRIME_LENGTH                20 /*  160-bits */
andre@0: #define FIPS_DSA_SIGNATURE_LENGTH               40 /*  320-bits */
andre@0: #define FIPS_DSA_PRIME_LENGTH                  128 /* 1024-bits */
andre@0: #define FIPS_DSA_BASE_LENGTH                   128 /* 1024-bits */
andre@0: 
andre@0: /* FIPS preprocessor directives for RNG.                        */
andre@0: #define FIPS_RNG_XKEY_LENGTH                    32  /* 256-bits */
andre@0: 
andre@0: static CK_RV
andre@0: sftk_fips_RC2_PowerUpSelfTest( void )
andre@0: {
andre@0:     /* RC2 Known Key (40-bits). */
andre@0:     static const PRUint8 rc2_known_key[] = { "RSARC" };
andre@0: 
andre@0:     /* RC2-CBC Known Initialization Vector (64-bits). */
andre@0:     static const PRUint8 rc2_cbc_known_initialization_vector[] = {"Security"};
andre@0: 
andre@0:     /* RC2 Known Plaintext (64-bits). */
andre@0:     static const PRUint8 rc2_ecb_known_plaintext[] = {"Netscape"};
andre@0:     static const PRUint8 rc2_cbc_known_plaintext[] = {"Netscape"};
andre@0: 
andre@0:     /* RC2 Known Ciphertext (64-bits). */
andre@0:     static const PRUint8 rc2_ecb_known_ciphertext[] = {
andre@0: 				  0x1a,0x71,0x33,0x54,0x8d,0x5c,0xd2,0x30};
andre@0:     static const PRUint8 rc2_cbc_known_ciphertext[] = {
andre@0: 				  0xff,0x41,0xdb,0x94,0x8a,0x4c,0x33,0xb3};
andre@0: 
andre@0:     /* RC2 variables. */
andre@0:     PRUint8        rc2_computed_ciphertext[FIPS_RC2_ENCRYPT_LENGTH];
andre@0:     PRUint8        rc2_computed_plaintext[FIPS_RC2_DECRYPT_LENGTH];
andre@0:     RC2Context *   rc2_context;
andre@0:     unsigned int   rc2_bytes_encrypted;
andre@0:     unsigned int   rc2_bytes_decrypted;
andre@0:     SECStatus      rc2_status;
andre@0: 
andre@0: 
andre@0:     /******************************************************/
andre@0:     /* RC2-ECB Single-Round Known Answer Encryption Test: */
andre@0:     /******************************************************/
andre@0: 
andre@0:     rc2_context = RC2_CreateContext( rc2_known_key, FIPS_RC2_KEY_LENGTH,
andre@0:                                      NULL, NSS_RC2,
andre@0:                                      FIPS_RC2_KEY_LENGTH );
andre@0: 
andre@0:     if( rc2_context == NULL )
andre@0:         return( CKR_HOST_MEMORY );
andre@0: 
andre@0:     rc2_status = RC2_Encrypt( rc2_context, rc2_computed_ciphertext,
andre@0:                               &rc2_bytes_encrypted, FIPS_RC2_ENCRYPT_LENGTH,
andre@0:                               rc2_ecb_known_plaintext,
andre@0:                               FIPS_RC2_DECRYPT_LENGTH );
andre@0: 
andre@0:     RC2_DestroyContext( rc2_context, PR_TRUE );
andre@0: 
andre@0:     if( ( rc2_status != SECSuccess ) ||
andre@0:         ( rc2_bytes_encrypted != FIPS_RC2_ENCRYPT_LENGTH ) ||
andre@0:         ( PORT_Memcmp( rc2_computed_ciphertext, rc2_ecb_known_ciphertext,
andre@0:                        FIPS_RC2_ENCRYPT_LENGTH ) != 0 ) )
andre@0:         return( CKR_DEVICE_ERROR );
andre@0: 
andre@0: 
andre@0:     /******************************************************/
andre@0:     /* RC2-ECB Single-Round Known Answer Decryption Test: */
andre@0:     /******************************************************/
andre@0: 
andre@0:     rc2_context = RC2_CreateContext( rc2_known_key, FIPS_RC2_KEY_LENGTH,
andre@0:                                      NULL, NSS_RC2,
andre@0:                                      FIPS_RC2_KEY_LENGTH );
andre@0: 
andre@0:     if( rc2_context == NULL )
andre@0:         return( CKR_HOST_MEMORY );
andre@0: 
andre@0:     rc2_status = RC2_Decrypt( rc2_context, rc2_computed_plaintext,
andre@0:                               &rc2_bytes_decrypted, FIPS_RC2_DECRYPT_LENGTH,
andre@0:                               rc2_ecb_known_ciphertext,
andre@0:                               FIPS_RC2_ENCRYPT_LENGTH );
andre@0: 
andre@0:     RC2_DestroyContext( rc2_context, PR_TRUE );
andre@0: 
andre@0:     if( ( rc2_status != SECSuccess ) ||
andre@0:         ( rc2_bytes_decrypted != FIPS_RC2_DECRYPT_LENGTH ) ||
andre@0:         ( PORT_Memcmp( rc2_computed_plaintext, rc2_ecb_known_plaintext,
andre@0:                        FIPS_RC2_DECRYPT_LENGTH ) != 0 ) )
andre@0:         return( CKR_DEVICE_ERROR );
andre@0: 
andre@0: 
andre@0:     /******************************************************/
andre@0:     /* RC2-CBC Single-Round Known Answer Encryption Test: */
andre@0:     /******************************************************/
andre@0: 
andre@0:     rc2_context = RC2_CreateContext( rc2_known_key, FIPS_RC2_KEY_LENGTH,
andre@0:                                      rc2_cbc_known_initialization_vector,
andre@0:                                      NSS_RC2_CBC, FIPS_RC2_KEY_LENGTH );
andre@0: 
andre@0:     if( rc2_context == NULL )
andre@0:         return( CKR_HOST_MEMORY );
andre@0: 
andre@0:     rc2_status = RC2_Encrypt( rc2_context, rc2_computed_ciphertext,
andre@0:                               &rc2_bytes_encrypted, FIPS_RC2_ENCRYPT_LENGTH,
andre@0:                               rc2_cbc_known_plaintext,
andre@0:                               FIPS_RC2_DECRYPT_LENGTH );
andre@0: 
andre@0:     RC2_DestroyContext( rc2_context, PR_TRUE );
andre@0: 
andre@0:     if( ( rc2_status != SECSuccess ) ||
andre@0:         ( rc2_bytes_encrypted != FIPS_RC2_ENCRYPT_LENGTH ) ||
andre@0:         ( PORT_Memcmp( rc2_computed_ciphertext, rc2_cbc_known_ciphertext,
andre@0:                        FIPS_RC2_ENCRYPT_LENGTH ) != 0 ) )
andre@0:         return( CKR_DEVICE_ERROR );
andre@0: 
andre@0: 
andre@0:     /******************************************************/
andre@0:     /* RC2-CBC Single-Round Known Answer Decryption Test: */
andre@0:     /******************************************************/
andre@0: 
andre@0:     rc2_context = RC2_CreateContext( rc2_known_key, FIPS_RC2_KEY_LENGTH,
andre@0:                                      rc2_cbc_known_initialization_vector,
andre@0:                                      NSS_RC2_CBC, FIPS_RC2_KEY_LENGTH );
andre@0: 
andre@0:     if( rc2_context == NULL )
andre@0:         return( CKR_HOST_MEMORY );
andre@0: 
andre@0:     rc2_status = RC2_Decrypt( rc2_context, rc2_computed_plaintext,
andre@0:                               &rc2_bytes_decrypted, FIPS_RC2_DECRYPT_LENGTH,
andre@0:                               rc2_cbc_known_ciphertext,
andre@0:                               FIPS_RC2_ENCRYPT_LENGTH );
andre@0: 
andre@0:     RC2_DestroyContext( rc2_context, PR_TRUE );
andre@0: 
andre@0:     if( ( rc2_status != SECSuccess ) ||
andre@0:         ( rc2_bytes_decrypted != FIPS_RC2_DECRYPT_LENGTH ) ||
andre@0:         ( PORT_Memcmp( rc2_computed_plaintext, rc2_ecb_known_plaintext,
andre@0:                        FIPS_RC2_DECRYPT_LENGTH ) != 0 ) )
andre@0:         return( CKR_DEVICE_ERROR );
andre@0: 
andre@0:     return( CKR_OK );
andre@0: }
andre@0: 
andre@0: 
andre@0: static CK_RV
andre@0: sftk_fips_RC4_PowerUpSelfTest( void )
andre@0: {
andre@0:     /* RC4 Known Key (40-bits). */
andre@0:     static const PRUint8 rc4_known_key[] = { "RSARC" };
andre@0: 
andre@0:     /* RC4 Known Plaintext (64-bits). */
andre@0:     static const PRUint8 rc4_known_plaintext[] = { "Netscape" };
andre@0: 
andre@0:     /* RC4 Known Ciphertext (64-bits). */
andre@0:     static const PRUint8 rc4_known_ciphertext[] = {
andre@0: 				0x29,0x33,0xc7,0x9a,0x9d,0x6c,0x09,0xdd};
andre@0: 
andre@0:     /* RC4 variables. */
andre@0:     PRUint8        rc4_computed_ciphertext[FIPS_RC4_ENCRYPT_LENGTH];
andre@0:     PRUint8        rc4_computed_plaintext[FIPS_RC4_DECRYPT_LENGTH];
andre@0:     RC4Context *   rc4_context;
andre@0:     unsigned int   rc4_bytes_encrypted;
andre@0:     unsigned int   rc4_bytes_decrypted;
andre@0:     SECStatus      rc4_status;
andre@0: 
andre@0: 
andre@0:     /**************************************************/
andre@0:     /* RC4 Single-Round Known Answer Encryption Test: */
andre@0:     /**************************************************/
andre@0: 
andre@0:     rc4_context = RC4_CreateContext( rc4_known_key, FIPS_RC4_KEY_LENGTH );
andre@0: 
andre@0:     if( rc4_context == NULL )
andre@0:         return( CKR_HOST_MEMORY );
andre@0: 
andre@0:     rc4_status = RC4_Encrypt( rc4_context, rc4_computed_ciphertext,
andre@0:                               &rc4_bytes_encrypted, FIPS_RC4_ENCRYPT_LENGTH,
andre@0:                               rc4_known_plaintext, FIPS_RC4_DECRYPT_LENGTH );
andre@0: 
andre@0:     RC4_DestroyContext( rc4_context, PR_TRUE );
andre@0: 
andre@0:     if( ( rc4_status != SECSuccess ) ||
andre@0:         ( rc4_bytes_encrypted != FIPS_RC4_ENCRYPT_LENGTH ) ||
andre@0:         ( PORT_Memcmp( rc4_computed_ciphertext, rc4_known_ciphertext,
andre@0:                        FIPS_RC4_ENCRYPT_LENGTH ) != 0 ) )
andre@0:         return( CKR_DEVICE_ERROR );
andre@0: 
andre@0: 
andre@0:     /**************************************************/
andre@0:     /* RC4 Single-Round Known Answer Decryption Test: */
andre@0:     /**************************************************/
andre@0: 
andre@0:     rc4_context = RC4_CreateContext( rc4_known_key, FIPS_RC4_KEY_LENGTH );
andre@0: 
andre@0:     if( rc4_context == NULL )
andre@0:         return( CKR_HOST_MEMORY );
andre@0: 
andre@0:     rc4_status = RC4_Decrypt( rc4_context, rc4_computed_plaintext,
andre@0:                               &rc4_bytes_decrypted, FIPS_RC4_DECRYPT_LENGTH,
andre@0:                               rc4_known_ciphertext, FIPS_RC4_ENCRYPT_LENGTH );
andre@0: 
andre@0:     RC4_DestroyContext( rc4_context, PR_TRUE );
andre@0: 
andre@0:     if( ( rc4_status != SECSuccess ) ||
andre@0:         ( rc4_bytes_decrypted != FIPS_RC4_DECRYPT_LENGTH ) ||
andre@0:         ( PORT_Memcmp( rc4_computed_plaintext, rc4_known_plaintext,
andre@0:                        FIPS_RC4_DECRYPT_LENGTH ) != 0 ) )
andre@0:         return( CKR_DEVICE_ERROR );
andre@0: 
andre@0:     return( CKR_OK );
andre@0: }
andre@0: 
andre@0: 
andre@0: static CK_RV
andre@0: sftk_fips_DES_PowerUpSelfTest( void )
andre@0: {
andre@0:     /* DES Known Key (56-bits). */
andre@0:     static const PRUint8 des_known_key[] = { "ANSI DES" };
andre@0: 
andre@0:     /* DES-CBC Known Initialization Vector (64-bits). */
andre@0:     static const PRUint8 des_cbc_known_initialization_vector[] = { "Security" };
andre@0: 
andre@0:     /* DES Known Plaintext (64-bits). */
andre@0:     static const PRUint8 des_ecb_known_plaintext[] = { "Netscape" };
andre@0:     static const PRUint8 des_cbc_known_plaintext[] = { "Netscape" };
andre@0: 
andre@0:     /* DES Known Ciphertext (64-bits). */
andre@0:     static const PRUint8 des_ecb_known_ciphertext[] = {
andre@0: 			       0x26,0x14,0xe9,0xc3,0x28,0x80,0x50,0xb0};
andre@0:     static const PRUint8 des_cbc_known_ciphertext[]  = {
andre@0: 			       0x5e,0x95,0x94,0x5d,0x76,0xa2,0xd3,0x7d};
andre@0: 
andre@0:     /* DES variables. */
andre@0:     PRUint8        des_computed_ciphertext[FIPS_DES_ENCRYPT_LENGTH];
andre@0:     PRUint8        des_computed_plaintext[FIPS_DES_DECRYPT_LENGTH];
andre@0:     DESContext *   des_context;
andre@0:     unsigned int   des_bytes_encrypted;
andre@0:     unsigned int   des_bytes_decrypted;
andre@0:     SECStatus      des_status;
andre@0: 
andre@0: 
andre@0:     /******************************************************/
andre@0:     /* DES-ECB Single-Round Known Answer Encryption Test: */
andre@0:     /******************************************************/
andre@0: 
andre@0:     des_context = DES_CreateContext( des_known_key, NULL, NSS_DES, PR_TRUE );
andre@0: 
andre@0:     if( des_context == NULL )
andre@0:         return( CKR_HOST_MEMORY );
andre@0: 
andre@0:     des_status = DES_Encrypt( des_context, des_computed_ciphertext,
andre@0:                               &des_bytes_encrypted, FIPS_DES_ENCRYPT_LENGTH,
andre@0:                               des_ecb_known_plaintext,
andre@0:                               FIPS_DES_DECRYPT_LENGTH );
andre@0: 
andre@0:     DES_DestroyContext( des_context, PR_TRUE );
andre@0: 
andre@0:     if( ( des_status != SECSuccess ) ||
andre@0:         ( des_bytes_encrypted != FIPS_DES_ENCRYPT_LENGTH ) ||
andre@0:         ( PORT_Memcmp( des_computed_ciphertext, des_ecb_known_ciphertext,
andre@0:                        FIPS_DES_ENCRYPT_LENGTH ) != 0 ) )
andre@0:         return( CKR_DEVICE_ERROR );
andre@0: 
andre@0: 
andre@0:     /******************************************************/
andre@0:     /* DES-ECB Single-Round Known Answer Decryption Test: */
andre@0:     /******************************************************/
andre@0: 
andre@0:     des_context = DES_CreateContext( des_known_key, NULL, NSS_DES, PR_FALSE );
andre@0: 
andre@0:     if( des_context == NULL )
andre@0:         return( CKR_HOST_MEMORY );
andre@0: 
andre@0:     des_status = DES_Decrypt( des_context, des_computed_plaintext,
andre@0:                               &des_bytes_decrypted, FIPS_DES_DECRYPT_LENGTH,
andre@0:                               des_ecb_known_ciphertext,
andre@0:                               FIPS_DES_ENCRYPT_LENGTH );
andre@0: 
andre@0:     DES_DestroyContext( des_context, PR_TRUE );
andre@0: 
andre@0:     if( ( des_status != SECSuccess ) ||
andre@0:         ( des_bytes_decrypted != FIPS_DES_DECRYPT_LENGTH ) ||
andre@0:         ( PORT_Memcmp( des_computed_plaintext, des_ecb_known_plaintext,
andre@0:                        FIPS_DES_DECRYPT_LENGTH ) != 0 ) )
andre@0:         return( CKR_DEVICE_ERROR );
andre@0: 
andre@0: 
andre@0:     /******************************************************/
andre@0:     /* DES-CBC Single-Round Known Answer Encryption Test. */
andre@0:     /******************************************************/
andre@0: 
andre@0:     des_context = DES_CreateContext( des_known_key,
andre@0:                                      des_cbc_known_initialization_vector,
andre@0:                                      NSS_DES_CBC, PR_TRUE );
andre@0: 
andre@0:     if( des_context == NULL )
andre@0:         return( CKR_HOST_MEMORY );
andre@0: 
andre@0:     des_status = DES_Encrypt( des_context, des_computed_ciphertext,
andre@0:                               &des_bytes_encrypted, FIPS_DES_ENCRYPT_LENGTH,
andre@0:                               des_cbc_known_plaintext,
andre@0:                               FIPS_DES_DECRYPT_LENGTH );
andre@0: 
andre@0:     DES_DestroyContext( des_context, PR_TRUE );
andre@0: 
andre@0:     if( ( des_status != SECSuccess ) ||
andre@0:         ( des_bytes_encrypted != FIPS_DES_ENCRYPT_LENGTH ) ||
andre@0:         ( PORT_Memcmp( des_computed_ciphertext, des_cbc_known_ciphertext,
andre@0:                        FIPS_DES_ENCRYPT_LENGTH ) != 0 ) )
andre@0:         return( CKR_DEVICE_ERROR );
andre@0: 
andre@0: 
andre@0:     /******************************************************/
andre@0:     /* DES-CBC Single-Round Known Answer Decryption Test. */
andre@0:     /******************************************************/
andre@0: 
andre@0:     des_context = DES_CreateContext( des_known_key,
andre@0:                                      des_cbc_known_initialization_vector,
andre@0:                                      NSS_DES_CBC, PR_FALSE );
andre@0: 
andre@0:     if( des_context == NULL )
andre@0:         return( CKR_HOST_MEMORY );
andre@0: 
andre@0:     des_status = DES_Decrypt( des_context, des_computed_plaintext,
andre@0:                               &des_bytes_decrypted, FIPS_DES_DECRYPT_LENGTH,
andre@0:                               des_cbc_known_ciphertext,
andre@0:                               FIPS_DES_ENCRYPT_LENGTH );
andre@0: 
andre@0:     DES_DestroyContext( des_context, PR_TRUE );
andre@0: 
andre@0:     if( ( des_status != SECSuccess ) ||
andre@0:         ( des_bytes_decrypted != FIPS_DES_DECRYPT_LENGTH ) ||
andre@0:         ( PORT_Memcmp( des_computed_plaintext, des_cbc_known_plaintext,
andre@0:                        FIPS_DES_DECRYPT_LENGTH ) != 0 ) )
andre@0:         return( CKR_DEVICE_ERROR );
andre@0: 
andre@0:     return( CKR_OK );
andre@0: }
andre@0: 
andre@0: 
andre@0: static CK_RV
andre@0: sftk_fips_DES3_PowerUpSelfTest( void )
andre@0: {
andre@0:     /* DES3 Known Key (56-bits). */
andre@0:     static const PRUint8 des3_known_key[] = { "ANSI Triple-DES Key Data" };
andre@0: 
andre@0:     /* DES3-CBC Known Initialization Vector (64-bits). */
andre@0:     static const PRUint8 des3_cbc_known_initialization_vector[] = { "Security" };
andre@0: 
andre@0:     /* DES3 Known Plaintext (64-bits). */
andre@0:     static const PRUint8 des3_ecb_known_plaintext[] = { "Netscape" };
andre@0:     static const PRUint8 des3_cbc_known_plaintext[] = { "Netscape" };
andre@0: 
andre@0:     /* DES3 Known Ciphertext (64-bits). */
andre@0:     static const PRUint8 des3_ecb_known_ciphertext[] = {
andre@0: 			   0x55,0x8e,0xad,0x3c,0xee,0x49,0x69,0xbe};
andre@0:     static const PRUint8 des3_cbc_known_ciphertext[] = {
andre@0: 			   0x43,0xdc,0x6a,0xc1,0xaf,0xa6,0x32,0xf5};
andre@0: 
andre@0:     /* DES3 variables. */
andre@0:     PRUint8        des3_computed_ciphertext[FIPS_DES3_ENCRYPT_LENGTH];
andre@0:     PRUint8        des3_computed_plaintext[FIPS_DES3_DECRYPT_LENGTH];
andre@0:     DESContext *   des3_context;
andre@0:     unsigned int   des3_bytes_encrypted;
andre@0:     unsigned int   des3_bytes_decrypted;
andre@0:     SECStatus      des3_status;
andre@0: 
andre@0: 
andre@0:     /*******************************************************/
andre@0:     /* DES3-ECB Single-Round Known Answer Encryption Test. */
andre@0:     /*******************************************************/
andre@0: 
andre@0:     des3_context = DES_CreateContext( des3_known_key, NULL,
andre@0:                                      NSS_DES_EDE3, PR_TRUE );
andre@0: 
andre@0:     if( des3_context == NULL )
andre@0:         return( CKR_HOST_MEMORY );
andre@0: 
andre@0:     des3_status = DES_Encrypt( des3_context, des3_computed_ciphertext,
andre@0:                                &des3_bytes_encrypted, FIPS_DES3_ENCRYPT_LENGTH,
andre@0:                                des3_ecb_known_plaintext,
andre@0:                                FIPS_DES3_DECRYPT_LENGTH );
andre@0: 
andre@0:     DES_DestroyContext( des3_context, PR_TRUE );
andre@0: 
andre@0:     if( ( des3_status != SECSuccess ) ||
andre@0:         ( des3_bytes_encrypted != FIPS_DES3_ENCRYPT_LENGTH ) ||
andre@0:         ( PORT_Memcmp( des3_computed_ciphertext, des3_ecb_known_ciphertext,
andre@0:                        FIPS_DES3_ENCRYPT_LENGTH ) != 0 ) )
andre@0:         return( CKR_DEVICE_ERROR );
andre@0: 
andre@0: 
andre@0:     /*******************************************************/
andre@0:     /* DES3-ECB Single-Round Known Answer Decryption Test. */
andre@0:     /*******************************************************/
andre@0: 
andre@0:     des3_context = DES_CreateContext( des3_known_key, NULL,
andre@0:                                      NSS_DES_EDE3, PR_FALSE );
andre@0: 
andre@0:     if( des3_context == NULL )
andre@0:         return( CKR_HOST_MEMORY );
andre@0: 
andre@0:     des3_status = DES_Decrypt( des3_context, des3_computed_plaintext,
andre@0:                                &des3_bytes_decrypted, FIPS_DES3_DECRYPT_LENGTH,
andre@0:                                des3_ecb_known_ciphertext,
andre@0:                                FIPS_DES3_ENCRYPT_LENGTH );
andre@0: 
andre@0:     DES_DestroyContext( des3_context, PR_TRUE );
andre@0: 
andre@0:     if( ( des3_status != SECSuccess ) ||
andre@0:         ( des3_bytes_decrypted != FIPS_DES3_DECRYPT_LENGTH ) ||
andre@0:         ( PORT_Memcmp( des3_computed_plaintext, des3_ecb_known_plaintext,
andre@0:                        FIPS_DES3_DECRYPT_LENGTH ) != 0 ) )
andre@0:         return( CKR_DEVICE_ERROR );
andre@0: 
andre@0: 
andre@0:     /*******************************************************/
andre@0:     /* DES3-CBC Single-Round Known Answer Encryption Test. */
andre@0:     /*******************************************************/
andre@0: 
andre@0:     des3_context = DES_CreateContext( des3_known_key,
andre@0:                                       des3_cbc_known_initialization_vector,
andre@0:                                       NSS_DES_EDE3_CBC, PR_TRUE );
andre@0: 
andre@0:     if( des3_context == NULL )
andre@0:         return( CKR_HOST_MEMORY );
andre@0: 
andre@0:     des3_status = DES_Encrypt( des3_context, des3_computed_ciphertext,
andre@0:                                &des3_bytes_encrypted, FIPS_DES3_ENCRYPT_LENGTH,
andre@0:                                des3_cbc_known_plaintext,
andre@0:                                FIPS_DES3_DECRYPT_LENGTH );
andre@0: 
andre@0:     DES_DestroyContext( des3_context, PR_TRUE );
andre@0: 
andre@0:     if( ( des3_status != SECSuccess ) ||
andre@0:         ( des3_bytes_encrypted != FIPS_DES3_ENCRYPT_LENGTH ) ||
andre@0:         ( PORT_Memcmp( des3_computed_ciphertext, des3_cbc_known_ciphertext,
andre@0:                        FIPS_DES3_ENCRYPT_LENGTH ) != 0 ) )
andre@0:         return( CKR_DEVICE_ERROR );
andre@0: 
andre@0: 
andre@0:     /*******************************************************/
andre@0:     /* DES3-CBC Single-Round Known Answer Decryption Test. */
andre@0:     /*******************************************************/
andre@0: 
andre@0:     des3_context = DES_CreateContext( des3_known_key,
andre@0:                                       des3_cbc_known_initialization_vector,
andre@0:                                       NSS_DES_EDE3_CBC, PR_FALSE );
andre@0: 
andre@0:     if( des3_context == NULL )
andre@0:         return( CKR_HOST_MEMORY );
andre@0: 
andre@0:     des3_status = DES_Decrypt( des3_context, des3_computed_plaintext,
andre@0:                                &des3_bytes_decrypted, FIPS_DES3_DECRYPT_LENGTH,
andre@0:                                des3_cbc_known_ciphertext,
andre@0:                                FIPS_DES3_ENCRYPT_LENGTH );
andre@0: 
andre@0:     DES_DestroyContext( des3_context, PR_TRUE );
andre@0: 
andre@0:     if( ( des3_status != SECSuccess ) ||
andre@0:         ( des3_bytes_decrypted != FIPS_DES3_DECRYPT_LENGTH ) ||
andre@0:         ( PORT_Memcmp( des3_computed_plaintext, des3_cbc_known_plaintext,
andre@0:                        FIPS_DES3_DECRYPT_LENGTH ) != 0 ) )
andre@0:         return( CKR_DEVICE_ERROR );
andre@0: 
andre@0:     return( CKR_OK );
andre@0: }
andre@0: 
andre@0: 
andre@0: /* AES self-test for 128-bit, 192-bit, or 256-bit key sizes*/
andre@0: static CK_RV
andre@0: sftk_fips_AES_PowerUpSelfTest( int aes_key_size )
andre@0: {
andre@0:     /* AES Known Key (up to 256-bits). */
andre@0:     static const PRUint8 aes_known_key[] = 
andre@0:         { "AES-128 RIJNDAELLEADNJIR 821-SEA" };
andre@0: 
andre@0:     /* AES-CBC Known Initialization Vector (128-bits). */
andre@0:     static const PRUint8 aes_cbc_known_initialization_vector[] = 
andre@0:         { "SecurityytiruceS" };
andre@0: 
andre@0:     /* AES Known Plaintext (128-bits). (blocksize is 128-bits) */
andre@0:     static const PRUint8 aes_known_plaintext[] = { "NetscapeepacsteN" };
andre@0: 
andre@0:     /* AES Known Ciphertext (128-bit key). */
andre@0:     static const PRUint8 aes_ecb128_known_ciphertext[] = {
andre@0:         0x3c,0xa5,0x96,0xf3,0x34,0x6a,0x96,0xc1,
andre@0:         0x03,0x88,0x16,0x7b,0x20,0xbf,0x35,0x47 };
andre@0: 
andre@0:     static const PRUint8 aes_cbc128_known_ciphertext[]  = {
andre@0:         0xcf,0x15,0x1d,0x4f,0x96,0xe4,0x4f,0x63,
andre@0:         0x15,0x54,0x14,0x1d,0x4e,0xd8,0xd5,0xea };
andre@0: 
andre@0:     /* AES Known Ciphertext (192-bit key). */
andre@0:     static const PRUint8 aes_ecb192_known_ciphertext[] = { 
andre@0:         0xa0,0x18,0x62,0xed,0x88,0x19,0xcb,0x62,
andre@0:         0x88,0x1d,0x4d,0xfe,0x84,0x02,0x89,0x0e };
andre@0: 
andre@0:     static const PRUint8 aes_cbc192_known_ciphertext[]  = { 
andre@0:         0x83,0xf7,0xa4,0x76,0xd1,0x6f,0x07,0xbe,
andre@0:         0x07,0xbc,0x43,0x2f,0x6d,0xad,0x29,0xe1 };
andre@0: 
andre@0:     /* AES Known Ciphertext (256-bit key). */
andre@0:     static const PRUint8 aes_ecb256_known_ciphertext[] = { 
andre@0:         0xdb,0xa6,0x52,0x01,0x8a,0x70,0xae,0x66,
andre@0:         0x3a,0x99,0xd8,0x95,0x7f,0xfb,0x01,0x67 };
andre@0:     
andre@0:     static const PRUint8 aes_cbc256_known_ciphertext[]  = { 
andre@0:         0x37,0xea,0x07,0x06,0x31,0x1c,0x59,0x27,
andre@0:         0xc5,0xc5,0x68,0x71,0x6e,0x34,0x40,0x16 };
andre@0: 
andre@0:     const PRUint8 *aes_ecb_known_ciphertext =
andre@0:         ( aes_key_size == FIPS_AES_128_KEY_SIZE) ? aes_ecb128_known_ciphertext :
andre@0:         ( aes_key_size == FIPS_AES_192_KEY_SIZE) ? aes_ecb192_known_ciphertext :
andre@0:                                 aes_ecb256_known_ciphertext;
andre@0: 
andre@0:     const PRUint8 *aes_cbc_known_ciphertext =
andre@0:         ( aes_key_size == FIPS_AES_128_KEY_SIZE) ? aes_cbc128_known_ciphertext :
andre@0:         ( aes_key_size == FIPS_AES_192_KEY_SIZE) ? aes_cbc192_known_ciphertext :
andre@0:                                 aes_cbc256_known_ciphertext;
andre@0: 
andre@0:     /* AES variables. */
andre@0:     PRUint8        aes_computed_ciphertext[FIPS_AES_ENCRYPT_LENGTH];
andre@0:     PRUint8        aes_computed_plaintext[FIPS_AES_DECRYPT_LENGTH];
andre@0:     AESContext *   aes_context;
andre@0:     unsigned int   aes_bytes_encrypted;
andre@0:     unsigned int   aes_bytes_decrypted;
andre@0:     SECStatus      aes_status;
andre@0: 
andre@0:     /*check if aes_key_size is 128, 192, or 256 bits */
andre@0:     if ((aes_key_size != FIPS_AES_128_KEY_SIZE) && 
andre@0:         (aes_key_size != FIPS_AES_192_KEY_SIZE) && 
andre@0:         (aes_key_size != FIPS_AES_256_KEY_SIZE)) 
andre@0:             return( CKR_DEVICE_ERROR );
andre@0: 
andre@0:     /******************************************************/
andre@0:     /* AES-ECB Single-Round Known Answer Encryption Test: */
andre@0:     /******************************************************/
andre@0: 
andre@0:     aes_context = AES_CreateContext( aes_known_key, NULL, NSS_AES, PR_TRUE,
andre@0:                                      aes_key_size, FIPS_AES_BLOCK_SIZE );
andre@0: 
andre@0:     if( aes_context == NULL )
andre@0:         return( CKR_HOST_MEMORY );
andre@0: 
andre@0:     aes_status = AES_Encrypt( aes_context, aes_computed_ciphertext,
andre@0:                               &aes_bytes_encrypted, FIPS_AES_ENCRYPT_LENGTH,
andre@0:                               aes_known_plaintext,
andre@0:                               FIPS_AES_DECRYPT_LENGTH );
andre@0:     
andre@0:     AES_DestroyContext( aes_context, PR_TRUE );
andre@0: 
andre@0:     if( ( aes_status != SECSuccess ) ||
andre@0:         ( aes_bytes_encrypted != FIPS_AES_ENCRYPT_LENGTH ) ||
andre@0:         ( PORT_Memcmp( aes_computed_ciphertext, aes_ecb_known_ciphertext,
andre@0:                        FIPS_AES_ENCRYPT_LENGTH ) != 0 ) )
andre@0:         return( CKR_DEVICE_ERROR );
andre@0: 
andre@0: 
andre@0:     /******************************************************/
andre@0:     /* AES-ECB Single-Round Known Answer Decryption Test: */
andre@0:     /******************************************************/
andre@0: 
andre@0:     aes_context = AES_CreateContext( aes_known_key, NULL, NSS_AES, PR_FALSE,
andre@0:                                      aes_key_size, FIPS_AES_BLOCK_SIZE );
andre@0: 
andre@0:     if( aes_context == NULL )
andre@0:         return( CKR_HOST_MEMORY );
andre@0: 
andre@0:     aes_status = AES_Decrypt( aes_context, aes_computed_plaintext,
andre@0:                               &aes_bytes_decrypted, FIPS_AES_DECRYPT_LENGTH,
andre@0:                               aes_ecb_known_ciphertext,
andre@0:                               FIPS_AES_ENCRYPT_LENGTH );
andre@0: 
andre@0:     AES_DestroyContext( aes_context, PR_TRUE );
andre@0: 
andre@0:     if( ( aes_status != SECSuccess ) ||         
andre@0:         ( aes_bytes_decrypted != FIPS_AES_DECRYPT_LENGTH ) ||
andre@0:         ( PORT_Memcmp( aes_computed_plaintext, aes_known_plaintext,
andre@0:                        FIPS_AES_DECRYPT_LENGTH ) != 0 ) )
andre@0:         return( CKR_DEVICE_ERROR );
andre@0: 
andre@0: 
andre@0:     /******************************************************/
andre@0:     /* AES-CBC Single-Round Known Answer Encryption Test. */
andre@0:     /******************************************************/
andre@0: 
andre@0:     aes_context = AES_CreateContext( aes_known_key,
andre@0:                                      aes_cbc_known_initialization_vector,
andre@0:                                      NSS_AES_CBC, PR_TRUE, aes_key_size, 
andre@0:                                      FIPS_AES_BLOCK_SIZE );
andre@0: 
andre@0:     if( aes_context == NULL )
andre@0:         return( CKR_HOST_MEMORY );
andre@0: 
andre@0:     aes_status = AES_Encrypt( aes_context, aes_computed_ciphertext,
andre@0:                               &aes_bytes_encrypted, FIPS_AES_ENCRYPT_LENGTH,
andre@0:                               aes_known_plaintext,
andre@0:                               FIPS_AES_DECRYPT_LENGTH );
andre@0: 
andre@0:     AES_DestroyContext( aes_context, PR_TRUE );
andre@0: 
andre@0:     if( ( aes_status != SECSuccess ) ||
andre@0:         ( aes_bytes_encrypted != FIPS_AES_ENCRYPT_LENGTH ) ||
andre@0:         ( PORT_Memcmp( aes_computed_ciphertext, aes_cbc_known_ciphertext,
andre@0:                        FIPS_AES_ENCRYPT_LENGTH ) != 0 ) )
andre@0:         return( CKR_DEVICE_ERROR );
andre@0: 
andre@0: 
andre@0:     /******************************************************/
andre@0:     /* AES-CBC Single-Round Known Answer Decryption Test. */
andre@0:     /******************************************************/
andre@0: 
andre@0:     aes_context = AES_CreateContext( aes_known_key,
andre@0:                                      aes_cbc_known_initialization_vector,
andre@0:                                      NSS_AES_CBC, PR_FALSE, aes_key_size, 
andre@0:                                      FIPS_AES_BLOCK_SIZE );
andre@0: 
andre@0:     if( aes_context == NULL )
andre@0:         return( CKR_HOST_MEMORY );
andre@0: 
andre@0:     aes_status = AES_Decrypt( aes_context, aes_computed_plaintext,
andre@0:                               &aes_bytes_decrypted, FIPS_AES_DECRYPT_LENGTH,
andre@0:                               aes_cbc_known_ciphertext,
andre@0:                               FIPS_AES_ENCRYPT_LENGTH );
andre@0: 
andre@0:     AES_DestroyContext( aes_context, PR_TRUE );
andre@0: 
andre@0:     if( ( aes_status != SECSuccess ) ||
andre@0:         ( aes_bytes_decrypted != FIPS_AES_DECRYPT_LENGTH ) ||
andre@0:         ( PORT_Memcmp( aes_computed_plaintext, aes_known_plaintext,
andre@0:                        FIPS_AES_DECRYPT_LENGTH ) != 0 ) )
andre@0:         return( CKR_DEVICE_ERROR );
andre@0: 
andre@0:     return( CKR_OK );
andre@0: }
andre@0: 
andre@0: /* Known Hash Message (512-bits).  Used for all hashes (incl. SHA-N [N>1]). */
andre@0: static const PRUint8 known_hash_message[] = {
andre@0:   "The test message for the MD2, MD5, and SHA-1 hashing algorithms." };
andre@0: 
andre@0: 
andre@0: static CK_RV
andre@0: sftk_fips_MD2_PowerUpSelfTest( void )
andre@0: {
andre@0:     /* MD2 Known Digest Message (128-bits). */
andre@0:     static const PRUint8 md2_known_digest[]  = {
andre@0:                                    0x41,0x5a,0x12,0xb2,0x3f,0x28,0x97,0x17,
andre@0:                                    0x0c,0x71,0x4e,0xcc,0x40,0xc8,0x1d,0x1b};
andre@0: 
andre@0:     /* MD2 variables. */
andre@0:     MD2Context * md2_context;
andre@0:     unsigned int md2_bytes_hashed;
andre@0:     PRUint8      md2_computed_digest[MD2_LENGTH];
andre@0: 
andre@0: 
andre@0:     /***********************************************/
andre@0:     /* MD2 Single-Round Known Answer Hashing Test. */
andre@0:     /***********************************************/
andre@0: 
andre@0:     md2_context = MD2_NewContext();
andre@0: 
andre@0:     if( md2_context == NULL )
andre@0:         return( CKR_HOST_MEMORY );
andre@0: 
andre@0:     MD2_Begin( md2_context );
andre@0: 
andre@0:     MD2_Update( md2_context, known_hash_message,
andre@0:                 FIPS_KNOWN_HASH_MESSAGE_LENGTH );
andre@0: 
andre@0:     MD2_End( md2_context, md2_computed_digest, &md2_bytes_hashed, MD2_LENGTH );
andre@0: 
andre@0:     MD2_DestroyContext( md2_context , PR_TRUE );
andre@0:     
andre@0:     if( ( md2_bytes_hashed != MD2_LENGTH ) ||
andre@0:         ( PORT_Memcmp( md2_computed_digest, md2_known_digest,
andre@0:                        MD2_LENGTH ) != 0 ) )
andre@0:         return( CKR_DEVICE_ERROR );
andre@0: 
andre@0:     return( CKR_OK );
andre@0: }
andre@0: 
andre@0: 
andre@0: static CK_RV
andre@0: sftk_fips_MD5_PowerUpSelfTest( void )
andre@0: {
andre@0:     /* MD5 Known Digest Message (128-bits). */
andre@0:     static const PRUint8 md5_known_digest[]  = {
andre@0: 				   0x25,0xc8,0xc0,0x10,0xc5,0x6e,0x68,0x28,
andre@0: 				   0x28,0xa4,0xa5,0xd2,0x98,0x9a,0xea,0x2d};
andre@0: 
andre@0:     /* MD5 variables. */
andre@0:     PRUint8        md5_computed_digest[MD5_LENGTH];
andre@0:     SECStatus      md5_status;
andre@0: 
andre@0: 
andre@0:     /***********************************************/
andre@0:     /* MD5 Single-Round Known Answer Hashing Test. */
andre@0:     /***********************************************/
andre@0: 
andre@0:     md5_status = MD5_HashBuf( md5_computed_digest, known_hash_message,
andre@0:                               FIPS_KNOWN_HASH_MESSAGE_LENGTH );
andre@0: 
andre@0:     if( ( md5_status != SECSuccess ) ||
andre@0:         ( PORT_Memcmp( md5_computed_digest, md5_known_digest,
andre@0:                        MD5_LENGTH ) != 0 ) )
andre@0:         return( CKR_DEVICE_ERROR );
andre@0: 
andre@0:     return( CKR_OK );
andre@0: }
andre@0: 
andre@0: /****************************************************/
andre@0: /* Single Round HMAC SHA-X test                     */
andre@0: /****************************************************/
andre@0: static SECStatus
andre@0: sftk_fips_HMAC(unsigned char *hmac_computed,
andre@0:                const PRUint8 *secret_key,
andre@0:                unsigned int secret_key_length,
andre@0:                const PRUint8 *message,
andre@0:                unsigned int message_length,
andre@0:                HASH_HashType hashAlg )
andre@0: {
andre@0:     SECStatus hmac_status = SECFailure;
andre@0:     HMACContext *cx = NULL;
andre@0:     SECHashObject *hashObj = NULL;
andre@0:     unsigned int bytes_hashed = 0;
andre@0: 
andre@0:     hashObj = (SECHashObject *) HASH_GetRawHashObject(hashAlg);
andre@0:  
andre@0:     if (!hashObj) 
andre@0:         return( SECFailure );
andre@0: 
andre@0:     cx = HMAC_Create(hashObj, secret_key, 
andre@0:                      secret_key_length, 
andre@0:                      PR_TRUE);  /* PR_TRUE for in FIPS mode */
andre@0: 
andre@0:     if (cx == NULL) 
andre@0:         return( SECFailure );
andre@0: 
andre@0:     HMAC_Begin(cx);
andre@0:     HMAC_Update(cx, message, message_length);
andre@0:     hmac_status = HMAC_Finish(cx, hmac_computed, &bytes_hashed, 
andre@0:                               hashObj->length);
andre@0: 
andre@0:     HMAC_Destroy(cx, PR_TRUE);
andre@0: 
andre@0:     return( hmac_status );
andre@0: }
andre@0: 
andre@0: static CK_RV
andre@0: sftk_fips_HMAC_PowerUpSelfTest( void )
andre@0: {
andre@0:     static const PRUint8 HMAC_known_secret_key[] = {
andre@0:                          "Firefox and ThunderBird are awesome!"};
andre@0: 
andre@0:     static const PRUint8 HMAC_known_secret_key_length 
andre@0:                          = sizeof HMAC_known_secret_key;
andre@0: 
andre@0:     /* known SHA1 hmac (20 bytes) */
andre@0:     static const PRUint8 known_SHA1_hmac[] = {
andre@0:         0xd5, 0x85, 0xf6, 0x5b, 0x39, 0xfa, 0xb9, 0x05, 
andre@0:         0x3b, 0x57, 0x1d, 0x61, 0xe7, 0xb8, 0x84, 0x1e, 
andre@0:         0x5d, 0x0e, 0x1e, 0x11};
andre@0: 
andre@0:     /* known SHA224 hmac (28 bytes) */
andre@0:     static const PRUint8 known_SHA224_hmac[] = {
andre@0:         0x1c, 0xc3, 0x06, 0x8e, 0xce, 0x37, 0x68, 0xfb, 
andre@0:         0x1a, 0x82, 0x4a, 0xbe, 0x2b, 0x00, 0x51, 0xf8,
andre@0:         0x9d, 0xb6, 0xe0, 0x90, 0x0d, 0x00, 0xc9, 0x64,
andre@0:         0x9a, 0xb8, 0x98, 0x4e};
andre@0: 
andre@0:     /* known SHA256 hmac (32 bytes) */
andre@0:     static const PRUint8 known_SHA256_hmac[] = {
andre@0:         0x05, 0x75, 0x9a, 0x9e, 0x70, 0x5e, 0xe7, 0x44, 
andre@0:         0xe2, 0x46, 0x4b, 0x92, 0x22, 0x14, 0x22, 0xe0, 
andre@0:         0x1b, 0x92, 0x8a, 0x0c, 0xfe, 0xf5, 0x49, 0xe9, 
andre@0:         0xa7, 0x1b, 0x56, 0x7d, 0x1d, 0x29, 0x40, 0x48};        
andre@0: 
andre@0:     /* known SHA384 hmac (48 bytes) */
andre@0:     static const PRUint8 known_SHA384_hmac[] = {
andre@0:         0xcd, 0x56, 0x14, 0xec, 0x05, 0x53, 0x06, 0x2b,
andre@0:         0x7e, 0x9c, 0x8a, 0x18, 0x5e, 0xea, 0xf3, 0x91,
andre@0:         0x33, 0xfb, 0x64, 0xf6, 0xe3, 0x9f, 0x89, 0x0b,
andre@0:         0xaf, 0xbe, 0x83, 0x4d, 0x3f, 0x3c, 0x43, 0x4d,
andre@0:         0x4a, 0x0c, 0x56, 0x98, 0xf8, 0xca, 0xb4, 0xaa,
andre@0:         0x9a, 0xf4, 0x0a, 0xaf, 0x4f, 0x69, 0xca, 0x87};
andre@0: 
andre@0:     /* known SHA512 hmac (64 bytes) */
andre@0:     static const PRUint8 known_SHA512_hmac[] = {
andre@0:         0xf6, 0x0e, 0x97, 0x12, 0x00, 0x67, 0x6e, 0xb9,
andre@0:         0x0c, 0xb2, 0x63, 0xf0, 0x60, 0xac, 0x75, 0x62,
andre@0:         0x70, 0x95, 0x2a, 0x52, 0x22, 0xee, 0xdd, 0xd2,
andre@0:         0x71, 0xb1, 0xe8, 0x26, 0x33, 0xd3, 0x13, 0x27,
andre@0:         0xcb, 0xff, 0x44, 0xef, 0x87, 0x97, 0x16, 0xfb,
andre@0:         0xd3, 0x0b, 0x48, 0xbe, 0x12, 0x4e, 0xda, 0xb1,
andre@0:         0x89, 0x90, 0xfb, 0x06, 0x0c, 0xbe, 0xe5, 0xc4,
andre@0:         0xff, 0x24, 0x37, 0x3d, 0xc7, 0xe4, 0xe4, 0x37};
andre@0: 
andre@0:     SECStatus    hmac_status;
andre@0:     PRUint8      hmac_computed[HASH_LENGTH_MAX]; 
andre@0: 
andre@0:     /***************************************************/
andre@0:     /* HMAC SHA-1 Single-Round Known Answer HMAC Test. */
andre@0:     /***************************************************/
andre@0: 
andre@0:     hmac_status = sftk_fips_HMAC(hmac_computed, 
andre@0:                                  HMAC_known_secret_key,
andre@0:                                  HMAC_known_secret_key_length,
andre@0:                                  known_hash_message,
andre@0:                                  FIPS_KNOWN_HASH_MESSAGE_LENGTH,
andre@0:                                  HASH_AlgSHA1); 
andre@0: 
andre@0:     if( ( hmac_status != SECSuccess ) || 
andre@0:         ( PORT_Memcmp( hmac_computed, known_SHA1_hmac,
andre@0:                        SHA1_LENGTH ) != 0 ) )
andre@0:         return( CKR_DEVICE_ERROR );
andre@0: 
andre@0:     /***************************************************/
andre@0:     /* HMAC SHA-224 Single-Round Known Answer Test.    */
andre@0:     /***************************************************/
andre@0: 
andre@0:     hmac_status = sftk_fips_HMAC(hmac_computed, 
andre@0:                                  HMAC_known_secret_key,
andre@0:                                  HMAC_known_secret_key_length,
andre@0:                                  known_hash_message,
andre@0:                                  FIPS_KNOWN_HASH_MESSAGE_LENGTH,
andre@0:                                  HASH_AlgSHA224);
andre@0: 
andre@0:     if( ( hmac_status != SECSuccess ) || 
andre@0:         ( PORT_Memcmp( hmac_computed, known_SHA224_hmac,
andre@0:                        SHA224_LENGTH ) != 0 ) )
andre@0:         return( CKR_DEVICE_ERROR );
andre@0: 
andre@0:     /***************************************************/
andre@0:     /* HMAC SHA-256 Single-Round Known Answer Test.    */
andre@0:     /***************************************************/
andre@0: 
andre@0:     hmac_status = sftk_fips_HMAC(hmac_computed, 
andre@0:                                  HMAC_known_secret_key,
andre@0:                                  HMAC_known_secret_key_length,
andre@0:                                  known_hash_message,
andre@0:                                  FIPS_KNOWN_HASH_MESSAGE_LENGTH,
andre@0:                                  HASH_AlgSHA256); 
andre@0: 
andre@0:     if( ( hmac_status != SECSuccess ) || 
andre@0:         ( PORT_Memcmp( hmac_computed, known_SHA256_hmac,
andre@0:                        SHA256_LENGTH ) != 0 ) )
andre@0:         return( CKR_DEVICE_ERROR );
andre@0: 
andre@0:     /***************************************************/
andre@0:     /* HMAC SHA-384 Single-Round Known Answer Test.    */
andre@0:     /***************************************************/
andre@0: 
andre@0:     hmac_status = sftk_fips_HMAC(hmac_computed,
andre@0:                                  HMAC_known_secret_key,
andre@0:                                  HMAC_known_secret_key_length,
andre@0:                                  known_hash_message,
andre@0:                                  FIPS_KNOWN_HASH_MESSAGE_LENGTH,
andre@0:                                  HASH_AlgSHA384);
andre@0: 
andre@0:     if( ( hmac_status != SECSuccess ) ||
andre@0:         ( PORT_Memcmp( hmac_computed, known_SHA384_hmac,
andre@0:                        SHA384_LENGTH ) != 0 ) )
andre@0:         return( CKR_DEVICE_ERROR );
andre@0: 
andre@0:     /***************************************************/
andre@0:     /* HMAC SHA-512 Single-Round Known Answer Test.    */
andre@0:     /***************************************************/
andre@0: 
andre@0:     hmac_status = sftk_fips_HMAC(hmac_computed,
andre@0:                                  HMAC_known_secret_key,
andre@0:                                  HMAC_known_secret_key_length,
andre@0:                                  known_hash_message,
andre@0:                                  FIPS_KNOWN_HASH_MESSAGE_LENGTH,
andre@0:                                  HASH_AlgSHA512);
andre@0: 
andre@0:     if( ( hmac_status != SECSuccess ) ||
andre@0:         ( PORT_Memcmp( hmac_computed, known_SHA512_hmac,
andre@0:                        SHA512_LENGTH ) != 0 ) )
andre@0:         return( CKR_DEVICE_ERROR );
andre@0: 
andre@0:     return( CKR_OK );
andre@0: }
andre@0: 
andre@0: static CK_RV
andre@0: sftk_fips_SHA_PowerUpSelfTest( void )
andre@0: {
andre@0:     /* SHA-1 Known Digest Message (160-bits). */
andre@0:     static const PRUint8 sha1_known_digest[] = {
andre@0: 			       0x0a,0x6d,0x07,0xba,0x1e,0xbd,0x8a,0x1b,
andre@0: 			       0x72,0xf6,0xc7,0x22,0xf1,0x27,0x9f,0xf0,
andre@0: 			       0xe0,0x68,0x47,0x7a};
andre@0: 
andre@0:     /* SHA-224 Known Digest Message (224-bits). */
andre@0:     static const PRUint8 sha224_known_digest[] = {
andre@0:         0x89,0x5e,0x7f,0xfd,0x0e,0xd8,0x35,0x6f,
andre@0:         0x64,0x6d,0xf2,0xde,0x5e,0xed,0xa6,0x7f, 
andre@0:         0x29,0xd1,0x12,0x73,0x42,0x84,0x95,0x4f, 
andre@0:         0x8e,0x08,0xe5,0xcb};
andre@0: 
andre@0:     /* SHA-256 Known Digest Message (256-bits). */
andre@0:     static const PRUint8 sha256_known_digest[] = {
andre@0:         0x38,0xa9,0xc1,0xf0,0x35,0xf6,0x5d,0x61,
andre@0:         0x11,0xd4,0x0b,0xdc,0xce,0x35,0x14,0x8d,
andre@0:         0xf2,0xdd,0xaf,0xaf,0xcf,0xb7,0x87,0xe9,
andre@0:         0x96,0xa5,0xd2,0x83,0x62,0x46,0x56,0x79};
andre@0:  
andre@0:     /* SHA-384 Known Digest Message (384-bits). */
andre@0:     static const PRUint8 sha384_known_digest[] = {
andre@0:         0x11,0xfe,0x1c,0x00,0x89,0x48,0xde,0xb3,
andre@0:         0x99,0xee,0x1c,0x18,0xb4,0x10,0xfb,0xfe,
andre@0:         0xe3,0xa8,0x2c,0xf3,0x04,0xb0,0x2f,0xc8,
andre@0:         0xa3,0xc4,0x5e,0xea,0x7e,0x60,0x48,0x7b,
andre@0:         0xce,0x2c,0x62,0xf7,0xbc,0xa7,0xe8,0xa3,
andre@0:         0xcf,0x24,0xce,0x9c,0xe2,0x8b,0x09,0x72};
andre@0: 
andre@0:     /* SHA-512 Known Digest Message (512-bits). */
andre@0:     static const PRUint8 sha512_known_digest[] = {
andre@0:         0xc8,0xb3,0x27,0xf9,0x0b,0x24,0xc8,0xbf,
andre@0:         0x4c,0xba,0x33,0x54,0xf2,0x31,0xbf,0xdb,
andre@0:         0xab,0xfd,0xb3,0x15,0xd7,0xfa,0x48,0x99,
andre@0:         0x07,0x60,0x0f,0x57,0x41,0x1a,0xdd,0x28,
andre@0:         0x12,0x55,0x25,0xac,0xba,0x3a,0x99,0x12,
andre@0:         0x2c,0x7a,0x8f,0x75,0x3a,0xe1,0x06,0x6f,
andre@0:         0x30,0x31,0xc9,0x33,0xc6,0x1b,0x90,0x1a,
andre@0:         0x6c,0x98,0x9a,0x87,0xd0,0xb2,0xf8,0x07};
andre@0: 
andre@0:     /* SHA-X variables. */
andre@0:     PRUint8        sha_computed_digest[HASH_LENGTH_MAX];
andre@0:     SECStatus      sha_status;
andre@0: 
andre@0:     /*************************************************/
andre@0:     /* SHA-1 Single-Round Known Answer Hashing Test. */
andre@0:     /*************************************************/
andre@0: 
andre@0:     sha_status = SHA1_HashBuf( sha_computed_digest, known_hash_message,
andre@0:                                 FIPS_KNOWN_HASH_MESSAGE_LENGTH );
andre@0:  
andre@0:     if( ( sha_status != SECSuccess ) ||
andre@0:         ( PORT_Memcmp( sha_computed_digest, sha1_known_digest,
andre@0:                        SHA1_LENGTH ) != 0 ) )
andre@0:         return( CKR_DEVICE_ERROR );
andre@0: 
andre@0:     /***************************************************/
andre@0:     /* SHA-224 Single-Round Known Answer Hashing Test. */
andre@0:     /***************************************************/
andre@0: 
andre@0:     sha_status = SHA224_HashBuf( sha_computed_digest, known_hash_message,
andre@0:                                 FIPS_KNOWN_HASH_MESSAGE_LENGTH );
andre@0: 
andre@0:     if( ( sha_status != SECSuccess ) ||
andre@0:         ( PORT_Memcmp( sha_computed_digest, sha224_known_digest,
andre@0:                        SHA224_LENGTH ) != 0 ) )
andre@0:         return( CKR_DEVICE_ERROR );
andre@0: 
andre@0:     /***************************************************/
andre@0:     /* SHA-256 Single-Round Known Answer Hashing Test. */
andre@0:     /***************************************************/
andre@0: 
andre@0:     sha_status = SHA256_HashBuf( sha_computed_digest, known_hash_message,
andre@0:                                 FIPS_KNOWN_HASH_MESSAGE_LENGTH );
andre@0: 
andre@0:     if( ( sha_status != SECSuccess ) ||
andre@0:         ( PORT_Memcmp( sha_computed_digest, sha256_known_digest,
andre@0:                        SHA256_LENGTH ) != 0 ) )
andre@0:         return( CKR_DEVICE_ERROR );
andre@0: 
andre@0:     /***************************************************/
andre@0:     /* SHA-384 Single-Round Known Answer Hashing Test. */
andre@0:     /***************************************************/
andre@0: 
andre@0:     sha_status = SHA384_HashBuf( sha_computed_digest, known_hash_message,
andre@0:                                 FIPS_KNOWN_HASH_MESSAGE_LENGTH );
andre@0: 
andre@0:     if( ( sha_status != SECSuccess ) ||
andre@0:         ( PORT_Memcmp( sha_computed_digest, sha384_known_digest,
andre@0:                        SHA384_LENGTH ) != 0 ) )
andre@0:         return( CKR_DEVICE_ERROR );
andre@0: 
andre@0:     /***************************************************/
andre@0:     /* SHA-512 Single-Round Known Answer Hashing Test. */
andre@0:     /***************************************************/
andre@0: 
andre@0:     sha_status = SHA512_HashBuf( sha_computed_digest, known_hash_message,
andre@0:                                 FIPS_KNOWN_HASH_MESSAGE_LENGTH );
andre@0: 
andre@0:     if( ( sha_status != SECSuccess ) ||
andre@0:         ( PORT_Memcmp( sha_computed_digest, sha512_known_digest,
andre@0:                        SHA512_LENGTH ) != 0 ) )
andre@0:         return( CKR_DEVICE_ERROR );
andre@0: 
andre@0:     return( CKR_OK );
andre@0: }
andre@0: 
andre@0: /*
andre@0: * Single round RSA Signature Known Answer Test
andre@0: */
andre@0: static SECStatus
andre@0: sftk_fips_RSA_PowerUpSigSelfTest (HASH_HashType  shaAlg,
andre@0:                                   NSSLOWKEYPublicKey *rsa_public_key,
andre@0:                                   NSSLOWKEYPrivateKey *rsa_private_key,
andre@0:                                   const unsigned char *rsa_known_msg,
andre@0:                                   const unsigned int rsa_kmsg_length,
andre@0:                                   const unsigned char *rsa_known_signature)
andre@0: {
andre@0:     SECOidTag      shaOid;   /* SHA OID */
andre@0:     unsigned char  sha[HASH_LENGTH_MAX];    /* SHA digest */
andre@0:     unsigned int   shaLength = 0;           /* length of SHA */
andre@0:     unsigned int   rsa_bytes_signed;
andre@0:     unsigned char  rsa_computed_signature[FIPS_RSA_SIGNATURE_LENGTH];
andre@0:     SECStatus      rv;
andre@0: 
andre@0:     if (shaAlg == HASH_AlgSHA1) {
andre@0:         if (SHA1_HashBuf(sha, rsa_known_msg, rsa_kmsg_length)
andre@0:                             != SECSuccess) {
andre@0:              goto loser;
andre@0:         }
andre@0:         shaLength = SHA1_LENGTH;
andre@0:         shaOid = SEC_OID_SHA1;
andre@0:     } else if (shaAlg == HASH_AlgSHA256) {
andre@0:         if (SHA256_HashBuf(sha, rsa_known_msg, rsa_kmsg_length)
andre@0:                             != SECSuccess) {
andre@0:              goto loser;
andre@0:         }
andre@0:         shaLength = SHA256_LENGTH;
andre@0:         shaOid = SEC_OID_SHA256;
andre@0:     } else if (shaAlg == HASH_AlgSHA384) {
andre@0:         if (SHA384_HashBuf(sha, rsa_known_msg, rsa_kmsg_length)
andre@0:                             != SECSuccess) {
andre@0:              goto loser;
andre@0:         }
andre@0:         shaLength = SHA384_LENGTH;
andre@0:         shaOid = SEC_OID_SHA384;
andre@0:     } else if (shaAlg == HASH_AlgSHA512) {
andre@0:         if (SHA512_HashBuf(sha, rsa_known_msg, rsa_kmsg_length)
andre@0:                             != SECSuccess) {
andre@0:              goto loser;
andre@0:         }
andre@0:         shaLength = SHA512_LENGTH;
andre@0:         shaOid = SEC_OID_SHA512;
andre@0:     } else {
andre@0:         goto loser;
andre@0:     }
andre@0: 
andre@0:     /*************************************************/
andre@0:     /* RSA Single-Round Known Answer Signature Test. */
andre@0:     /*************************************************/
andre@0: 
andre@0:     /* Perform RSA signature with the RSA private key. */
andre@0:     rv = RSA_HashSign( shaOid,
andre@0:                        rsa_private_key,
andre@0:                        rsa_computed_signature,
andre@0:                        &rsa_bytes_signed,
andre@0:                        FIPS_RSA_SIGNATURE_LENGTH,
andre@0:                        sha,
andre@0:                        shaLength);
andre@0: 
andre@0:     if( ( rv != SECSuccess ) ||
andre@0:         ( rsa_bytes_signed != FIPS_RSA_SIGNATURE_LENGTH ) ||
andre@0:         ( PORT_Memcmp( rsa_computed_signature, rsa_known_signature,
andre@0:                        FIPS_RSA_SIGNATURE_LENGTH ) != 0 ) ) {
andre@0:         goto loser;
andre@0:     }
andre@0: 
andre@0:     /****************************************************/
andre@0:     /* RSA Single-Round Known Answer Verification Test. */
andre@0:     /****************************************************/
andre@0: 
andre@0:     /* Perform RSA verification with the RSA public key. */
andre@0:     rv = RSA_HashCheckSign( shaOid,
andre@0:                             rsa_public_key,
andre@0:                             rsa_computed_signature,
andre@0:                             rsa_bytes_signed,
andre@0:                             sha,
andre@0:                             shaLength);
andre@0: 
andre@0:     if( rv != SECSuccess ) {
andre@0:          goto loser;
andre@0:     }
andre@0:     return( SECSuccess );
andre@0: 
andre@0: loser:
andre@0: 
andre@0:     return( SECFailure );
andre@0: 
andre@0: }
andre@0: 
andre@0: static CK_RV
andre@0: sftk_fips_RSA_PowerUpSelfTest( void )
andre@0: {
andre@0:     /* RSA Known Modulus used in both Public/Private Key Values (2048-bits). */
andre@0:     static const PRUint8 rsa_modulus[FIPS_RSA_MODULUS_LENGTH] = {
andre@0:                             0xb8, 0x15, 0x00, 0x33, 0xda, 0x0c, 0x9d, 0xa5,
andre@0:                             0x14, 0x8c, 0xde, 0x1f, 0x23, 0x07, 0x54, 0xe2,
andre@0:                             0xc6, 0xb9, 0x51, 0x04, 0xc9, 0x65, 0x24, 0x6e,
andre@0:                             0x0a, 0x46, 0x34, 0x5c, 0x37, 0x86, 0x6b, 0x88,
andre@0:                             0x24, 0x27, 0xac, 0xa5, 0x02, 0x79, 0xfb, 0xed,
andre@0:                             0x75, 0xc5, 0x3f, 0x6e, 0xdf, 0x05, 0x5f, 0x0f,
andre@0:                             0x20, 0x70, 0xa0, 0x5b, 0x85, 0xdb, 0xac, 0xb9,
andre@0:                             0x5f, 0x02, 0xc2, 0x64, 0x1e, 0x84, 0x5b, 0x3e,
andre@0:                             0xad, 0xbf, 0xf6, 0x2e, 0x51, 0xd6, 0xad, 0xf7,
andre@0:                             0xa7, 0x86, 0x75, 0x86, 0xec, 0xa7, 0xe1, 0xf7,
andre@0:                             0x08, 0xbf, 0xdc, 0x56, 0xb1, 0x3b, 0xca, 0xd8,
andre@0:                             0xfc, 0x51, 0xdf, 0x9a, 0x2a, 0x37, 0x06, 0xf2,
andre@0:                             0xd1, 0x6b, 0x9a, 0x5e, 0x2a, 0xe5, 0x20, 0x57,
andre@0:                             0x35, 0x9f, 0x1f, 0x98, 0xcf, 0x40, 0xc7, 0xd6,
andre@0:                             0x98, 0xdb, 0xde, 0xf5, 0x64, 0x53, 0xf7, 0x9d,
andre@0:                             0x45, 0xf3, 0xd6, 0x78, 0xb9, 0xe3, 0xa3, 0x20,
andre@0:                             0xcd, 0x79, 0x43, 0x35, 0xef, 0xd7, 0xfb, 0xb9,
andre@0:                             0x80, 0x88, 0x27, 0x2f, 0x63, 0xa8, 0x67, 0x3d,
andre@0:                             0x4a, 0xfa, 0x06, 0xc6, 0xd2, 0x86, 0x0b, 0xa7,
andre@0:                             0x28, 0xfd, 0xe0, 0x1e, 0x93, 0x4b, 0x17, 0x2e,
andre@0:                             0xb0, 0x11, 0x6f, 0xc6, 0x2b, 0x98, 0x0f, 0x15,
andre@0:                             0xe3, 0x87, 0x16, 0x7a, 0x7c, 0x67, 0x3e, 0x12,
andre@0:                             0x2b, 0xf8, 0xbe, 0x48, 0xc1, 0x97, 0x47, 0xf4,
andre@0:                             0x1f, 0x81, 0x80, 0x12, 0x28, 0xe4, 0x7b, 0x1e,
andre@0:                             0xb7, 0x00, 0xa4, 0xde, 0xaa, 0xfb, 0x0f, 0x77,
andre@0:                             0x84, 0xa3, 0xd6, 0xb2, 0x03, 0x48, 0xdd, 0x53,
andre@0:                             0x8b, 0x46, 0x41, 0x28, 0x52, 0xc4, 0x53, 0xf0,
andre@0:                             0x1c, 0x95, 0xd9, 0x36, 0xe0, 0x0f, 0x26, 0x46,
andre@0:                             0x9c, 0x61, 0x0e, 0x80, 0xca, 0x86, 0xaf, 0x39,
andre@0:                             0x95, 0xe5, 0x60, 0x43, 0x61, 0x3e, 0x2b, 0xb4,
andre@0:                             0xe8, 0xbd, 0x8d, 0x77, 0x62, 0xf5, 0x32, 0x43,
andre@0:                             0x2f, 0x4b, 0x65, 0x82, 0x14, 0xdd, 0x29, 0x5b};
andre@0: 
andre@0:     /* RSA Known Public Key Values (24-bits). */
andre@0:     static const PRUint8 rsa_public_exponent[FIPS_RSA_PUBLIC_EXPONENT_LENGTH] 
andre@0:                                                        = { 0x01, 0x00, 0x01 };
andre@0:     /* RSA Known Private Key Values (version                 is    8-bits), */
andre@0:     /*                              (private exponent        is 2048-bits), */
andre@0:     /*                              (private prime0          is 1024-bits), */
andre@0:     /*                              (private prime1          is 1024-bits), */
andre@0:     /*                              (private prime exponent0 is 1024-bits), */
andre@0:     /*                              (private prime exponent1 is 1024-bits), */
andre@0:     /*                          and (private coefficient     is 1024-bits). */
andre@0:     static const PRUint8 rsa_version[] = { 0x00 };
andre@0: 
andre@0:     static const PRUint8 rsa_private_exponent[FIPS_RSA_PRIVATE_EXPONENT_LENGTH]
andre@0:                          = {0x29, 0x08, 0x05, 0x53, 0x89, 0x76, 0xe6, 0x6c,
andre@0:                             0xb5, 0x77, 0xf0, 0xca, 0xdf, 0xf3, 0xf2, 0x67,
andre@0:                             0xda, 0x03, 0xd4, 0x9b, 0x4c, 0x88, 0xce, 0xe5,
andre@0:                             0xf8, 0x44, 0x4d, 0xc7, 0x80, 0x58, 0xe5, 0xff,
andre@0:                             0x22, 0x8f, 0xf5, 0x5b, 0x92, 0x81, 0xbe, 0x35,
andre@0:                             0xdf, 0xda, 0x67, 0x99, 0x3e, 0xfc, 0xe3, 0x83,
andre@0:                             0x6b, 0xa7, 0xaf, 0x16, 0xb7, 0x6f, 0x8f, 0xc0,
andre@0:                             0x81, 0xfd, 0x0b, 0x77, 0x65, 0x95, 0xfb, 0x00,
andre@0:                             0xad, 0x99, 0xec, 0x35, 0xc6, 0xe8, 0x23, 0x3e,
andre@0:                             0xe0, 0x88, 0x88, 0x09, 0xdb, 0x16, 0x50, 0xb7,
andre@0:                             0xcf, 0xab, 0x74, 0x61, 0x9e, 0x7f, 0xc5, 0x67,
andre@0:                             0x38, 0x56, 0xc7, 0x90, 0x85, 0x78, 0x5e, 0x84,
andre@0:                             0x21, 0x49, 0xea, 0xce, 0xb2, 0xa0, 0xff, 0xe4,
andre@0:                             0x70, 0x7f, 0x57, 0x7b, 0xa8, 0x36, 0xb8, 0x54,
andre@0:                             0x8d, 0x1d, 0xf5, 0x44, 0x9d, 0x68, 0x59, 0xf9,
andre@0:                             0x24, 0x6e, 0x85, 0x8f, 0xc3, 0x5f, 0x8a, 0x2c,
andre@0:                             0x94, 0xb7, 0xbc, 0x0e, 0xa5, 0xef, 0x93, 0x06,
andre@0:                             0x38, 0xcd, 0x07, 0x0c, 0xae, 0xb8, 0x44, 0x1a,
andre@0:                             0xd8, 0xe7, 0xf5, 0x9a, 0x1e, 0x9c, 0x18, 0xc7,
andre@0:                             0x6a, 0xc2, 0x7f, 0x28, 0x01, 0x4f, 0xb4, 0xb8,
andre@0:                             0x90, 0x97, 0x5a, 0x43, 0x38, 0xad, 0xe8, 0x95,
andre@0:                             0x68, 0x83, 0x1a, 0x1b, 0x10, 0x07, 0xe6, 0x02,
andre@0:                             0x52, 0x1f, 0xbf, 0x76, 0x6b, 0x46, 0xd6, 0xfb,
andre@0:                             0xc3, 0xbe, 0xb5, 0xac, 0x52, 0x53, 0x01, 0x1c,
andre@0:                             0xf3, 0xc5, 0xeb, 0x64, 0xf2, 0x1e, 0xc4, 0x38,
andre@0:                             0xe9, 0xaa, 0xd9, 0xc3, 0x72, 0x51, 0xa5, 0x44,
andre@0:                             0x58, 0x69, 0x0b, 0x1b, 0x98, 0x7f, 0xf2, 0x23,
andre@0:                             0xff, 0xeb, 0xf0, 0x75, 0x24, 0xcf, 0xc5, 0x1e,
andre@0:                             0xb8, 0x6a, 0xc5, 0x2f, 0x4f, 0x23, 0x50, 0x7d,
andre@0:                             0x15, 0x9d, 0x19, 0x7a, 0x0b, 0x82, 0xe0, 0x21,
andre@0:                             0x5b, 0x5f, 0x9d, 0x50, 0x2b, 0x83, 0xe4, 0x48,
andre@0:                             0xcc, 0x39, 0xe5, 0xfb, 0x13, 0x7b, 0x6f, 0x81 };
andre@0: 
andre@0:     static const PRUint8 rsa_prime0[FIPS_RSA_PRIME0_LENGTH]   = {
andre@0:                             0xe4, 0xbf, 0x21, 0x62, 0x9b, 0xa9, 0x77, 0x40,
andre@0:                             0x8d, 0x2a, 0xce, 0xa1, 0x67, 0x5a, 0x4c, 0x96,
andre@0:                             0x45, 0x98, 0x67, 0xbd, 0x75, 0x22, 0x33, 0x6f,
andre@0:                             0xe6, 0xcb, 0x77, 0xde, 0x9e, 0x97, 0x7d, 0x96,
andre@0:                             0x8c, 0x5e, 0x5d, 0x34, 0xfb, 0x27, 0xfc, 0x6d,
andre@0:                             0x74, 0xdb, 0x9d, 0x2e, 0x6d, 0xf6, 0xea, 0xfc,
andre@0:                             0xce, 0x9e, 0xda, 0xa7, 0x25, 0xa2, 0xf4, 0x58,
andre@0:                             0x6d, 0x0a, 0x3f, 0x01, 0xc2, 0xb4, 0xab, 0x38,
andre@0:                             0xc1, 0x14, 0x85, 0xb6, 0xfa, 0x94, 0xc3, 0x85,
andre@0:                             0xf9, 0x3c, 0x2e, 0x96, 0x56, 0x01, 0xe7, 0xd6,
andre@0:                             0x14, 0x71, 0x4f, 0xfb, 0x4c, 0x85, 0x52, 0xc4,
andre@0:                             0x61, 0x1e, 0xa5, 0x1e, 0x96, 0x13, 0x0d, 0x8f,
andre@0:                             0x66, 0xae, 0xa0, 0xcd, 0x7d, 0x25, 0x66, 0x19,
andre@0:                             0x15, 0xc2, 0xcf, 0xc3, 0x12, 0x3c, 0xe8, 0xa4,
andre@0:                             0x52, 0x4c, 0xcb, 0x28, 0x3c, 0xc4, 0xbf, 0x95,
andre@0:                             0x33, 0xe3, 0x81, 0xea, 0x0c, 0x6c, 0xa2, 0x05};
andre@0:     static const PRUint8 rsa_prime1[FIPS_RSA_PRIME1_LENGTH]   = {
andre@0:                             0xce, 0x03, 0x94, 0xf4, 0xa9, 0x2c, 0x1e, 0x06,
andre@0:                             0xe7, 0x40, 0x30, 0x01, 0xf7, 0xbb, 0x68, 0x8c,
andre@0:                             0x27, 0xd2, 0x15, 0xe3, 0x28, 0x49, 0x5b, 0xa8,
andre@0:                             0xc1, 0x9a, 0x42, 0x7e, 0x31, 0xf9, 0x08, 0x34,
andre@0:                             0x81, 0xa2, 0x0f, 0x04, 0x61, 0x34, 0xe3, 0x36,
andre@0:                             0x92, 0xb1, 0x09, 0x2b, 0xe9, 0xef, 0x84, 0x88,
andre@0:                             0xbe, 0x9c, 0x98, 0x60, 0xa6, 0x60, 0x84, 0xe9,
andre@0:                             0x75, 0x6f, 0xcc, 0x81, 0xd1, 0x96, 0xef, 0xdd,
andre@0:                             0x2e, 0xca, 0xc4, 0xf5, 0x42, 0xfb, 0x13, 0x2b,
andre@0:                             0x57, 0xbf, 0x14, 0x5e, 0xc2, 0x7f, 0x77, 0x35,
andre@0:                             0x29, 0xc4, 0xe5, 0xe0, 0xf9, 0x6d, 0x15, 0x4a,
andre@0:                             0x42, 0x56, 0x1c, 0x3e, 0x0c, 0xc5, 0xce, 0x70,
andre@0:                             0x08, 0x63, 0x1e, 0x73, 0xdb, 0x7e, 0x74, 0x05,
andre@0:                             0x32, 0x01, 0xc6, 0x36, 0x32, 0x75, 0x6b, 0xed,
andre@0:                             0x9d, 0xfe, 0x7c, 0x7e, 0xa9, 0x57, 0xb4, 0xe9,
andre@0:                             0x22, 0xe4, 0xe7, 0xfe, 0x36, 0x07, 0x9b, 0xdf};
andre@0:     static const PRUint8 rsa_exponent0[FIPS_RSA_EXPONENT0_LENGTH] = {
andre@0:                             0x04, 0x5a, 0x3a, 0xa9, 0x64, 0xaa, 0xd9, 0xd1,
andre@0:                             0x09, 0x9e, 0x99, 0xe5, 0xea, 0x50, 0x86, 0x8a,
andre@0:                             0x89, 0x72, 0x77, 0xee, 0xdb, 0xee, 0xb5, 0xa9,
andre@0:                             0xd8, 0x6b, 0x60, 0xb1, 0x84, 0xb4, 0xff, 0x37,
andre@0:                             0xc1, 0x1d, 0xfe, 0x8a, 0x06, 0x89, 0x61, 0x3d,
andre@0:                             0x37, 0xef, 0x01, 0xd3, 0xa3, 0x56, 0x02, 0x6c,
andre@0:                             0xa3, 0x05, 0xd4, 0xc5, 0x3f, 0x6b, 0x15, 0x59,
andre@0:                             0x25, 0x61, 0xff, 0x86, 0xea, 0x0c, 0x84, 0x01,
andre@0:                             0x85, 0x72, 0xfd, 0x84, 0x58, 0xca, 0x41, 0xda,
andre@0:                             0x27, 0xbe, 0xe4, 0x68, 0x09, 0xe4, 0xe9, 0x63,
andre@0:                             0x62, 0x6a, 0x31, 0x8a, 0x67, 0x8f, 0x55, 0xde,
andre@0:                             0xd4, 0xb6, 0x3f, 0x90, 0x10, 0x6c, 0xf6, 0x62,
andre@0:                             0x17, 0x23, 0x15, 0x7e, 0x33, 0x76, 0x65, 0xb5,
andre@0:                             0xee, 0x7b, 0x11, 0x76, 0xf5, 0xbe, 0xe0, 0xf2,
andre@0:                             0x57, 0x7a, 0x8c, 0x97, 0x0c, 0x68, 0xf5, 0xf8,
andre@0:                             0x41, 0xcf, 0x7f, 0x66, 0x53, 0xac, 0x31, 0x7d};
andre@0:     static const PRUint8 rsa_exponent1[FIPS_RSA_EXPONENT1_LENGTH] = {
andre@0:                             0x93, 0x54, 0x14, 0x6e, 0x73, 0x9d, 0x4d, 0x4b,
andre@0:                             0xfa, 0x8c, 0xf8, 0xc8, 0x2f, 0x76, 0x22, 0xea,
andre@0:                             0x38, 0x80, 0x11, 0x8f, 0x05, 0xfc, 0x90, 0x44,
andre@0:                             0x3b, 0x50, 0x2a, 0x45, 0x3d, 0x4f, 0xaf, 0x02,
andre@0:                             0x7d, 0xc2, 0x7b, 0xa2, 0xd2, 0x31, 0x94, 0x5c,
andre@0:                             0x2e, 0xc3, 0xd4, 0x9f, 0x47, 0x09, 0x37, 0x6a,
andre@0:                             0xe3, 0x85, 0xf1, 0xa3, 0x0c, 0xd8, 0xf1, 0xb4,
andre@0:                             0x53, 0x7b, 0xc4, 0x71, 0x02, 0x86, 0x42, 0xbb,
andre@0:                             0x96, 0xff, 0x03, 0xa3, 0xb2, 0x67, 0x03, 0xea,
andre@0:                             0x77, 0x31, 0xfb, 0x4b, 0x59, 0x24, 0xf7, 0x07,
andre@0:                             0x59, 0xfb, 0xa9, 0xba, 0x1e, 0x26, 0x58, 0x97,
andre@0:                             0x66, 0xa1, 0x56, 0x49, 0x39, 0xb1, 0x2c, 0x55,
andre@0:                             0x0a, 0x6a, 0x78, 0x18, 0xba, 0xdb, 0xcf, 0xf4,
andre@0:                             0xf7, 0x32, 0x35, 0xa2, 0x04, 0xab, 0xdc, 0xa7,
andre@0:                             0x6d, 0xd9, 0xd5, 0x06, 0x6f, 0xec, 0x7d, 0x40,
andre@0:                             0x4c, 0xe8, 0x0e, 0xd0, 0xc9, 0xaa, 0xdf, 0x59};
andre@0:     static const PRUint8 rsa_coefficient[FIPS_RSA_COEFFICIENT_LENGTH] = {
andre@0:                             0x17, 0xd7, 0xf5, 0x0a, 0xf0, 0x68, 0x97, 0x96,
andre@0:                             0xc4, 0x29, 0x18, 0x77, 0x9a, 0x1f, 0xe3, 0xf3,
andre@0:                             0x12, 0x13, 0x0f, 0x7e, 0x7b, 0xb9, 0xc1, 0x91,
andre@0:                             0xf9, 0xc7, 0x08, 0x56, 0x5c, 0xa4, 0xbc, 0x83,
andre@0:                             0x71, 0xf9, 0x78, 0xd9, 0x2b, 0xec, 0xfe, 0x6b,
andre@0:                             0xdc, 0x2f, 0x63, 0xc9, 0xcd, 0x50, 0x14, 0x5b,
andre@0:                             0xd3, 0x6e, 0x85, 0x4d, 0x0c, 0xa2, 0x0b, 0xa0,
andre@0:                             0x09, 0xb6, 0xca, 0x34, 0x9c, 0xc2, 0xc1, 0x4a,
andre@0:                             0xb0, 0xbc, 0x45, 0x93, 0xa5, 0x7e, 0x99, 0xb5,
andre@0:                             0xbd, 0xe4, 0x69, 0x29, 0x08, 0x28, 0xd2, 0xcd,
andre@0:                             0xab, 0x24, 0x78, 0x48, 0x41, 0x26, 0x0b, 0x37,
andre@0:                             0xa3, 0x43, 0xd1, 0x95, 0x1a, 0xd6, 0xee, 0x22,
andre@0:                             0x1c, 0x00, 0x0b, 0xc2, 0xb7, 0xa4, 0xa3, 0x21,
andre@0:                             0xa9, 0xcd, 0xe4, 0x69, 0xd3, 0x45, 0x02, 0xb1,
andre@0:                             0xb7, 0x3a, 0xbf, 0x51, 0x35, 0x1b, 0x78, 0xc2,
andre@0:                             0xcf, 0x0c, 0x0d, 0x60, 0x09, 0xa9, 0x44, 0x02};
andre@0: 
andre@0:     /* RSA Known Plaintext Message (1024-bits). */
andre@0:     static const PRUint8 rsa_known_plaintext_msg[FIPS_RSA_MESSAGE_LENGTH] = {
andre@0:                                          "Known plaintext message utilized"
andre@0:                                          "for RSA Encryption &  Decryption"
andre@0:                                          "blocks SHA256, SHA384  and      "
andre@0:                                          "SHA512 RSA Signature KAT tests. "
andre@0:                                          "Known plaintext message utilized"
andre@0:                                          "for RSA Encryption &  Decryption"
andre@0:                                          "blocks SHA256, SHA384  and      "
andre@0:                                          "SHA512 RSA Signature KAT  tests."};
andre@0: 
andre@0:     /* RSA Known Ciphertext (2048-bits). */
andre@0:     static const PRUint8 rsa_known_ciphertext[] = {
andre@0:                             0x04, 0x12, 0x46, 0xe3, 0x6a, 0xee, 0xde, 0xdd,
andre@0:                             0x49, 0xa1, 0xd9, 0x83, 0xf7, 0x35, 0xf9, 0x70,
andre@0:                             0x88, 0x03, 0x2d, 0x01, 0x8b, 0xd1, 0xbf, 0xdb,
andre@0:                             0xe5, 0x1c, 0x85, 0xbe, 0xb5, 0x0b, 0x48, 0x45,
andre@0:                             0x7a, 0xf0, 0xa0, 0xe3, 0xa2, 0xbb, 0x4b, 0xf6,
andre@0:                             0x27, 0xd0, 0x1b, 0x12, 0xe3, 0x77, 0x52, 0x34,
andre@0:                             0x9e, 0x8e, 0x03, 0xd2, 0xf8, 0x79, 0x6e, 0x39,
andre@0:                             0x79, 0x53, 0x3c, 0x44, 0x14, 0x94, 0xbb, 0x8d,
andre@0:                             0xaa, 0x14, 0x44, 0xa0, 0x7b, 0xa5, 0x8c, 0x93,
andre@0:                             0x5f, 0x99, 0xa4, 0xa3, 0x6e, 0x7a, 0x38, 0x40,
andre@0:                             0x78, 0xfa, 0x36, 0x91, 0x5e, 0x9a, 0x9c, 0xba,
andre@0:                             0x1e, 0xd4, 0xf9, 0xda, 0x4b, 0x0f, 0xa8, 0xa3,
andre@0:                             0x1c, 0xf3, 0x3a, 0xd1, 0xa5, 0xb4, 0x51, 0x16,
andre@0:                             0xed, 0x4b, 0xcf, 0xec, 0x93, 0x7b, 0x90, 0x21,
andre@0:                             0xbc, 0x3a, 0xf4, 0x0b, 0xd1, 0x3a, 0x2b, 0xba,
andre@0:                             0xa6, 0x7d, 0x5b, 0x53, 0xd8, 0x64, 0xf9, 0x29,
andre@0:                             0x7b, 0x7f, 0x77, 0x3e, 0x51, 0x4c, 0x9a, 0x94,
andre@0:                             0xd2, 0x4b, 0x4a, 0x8d, 0x61, 0x74, 0x97, 0xae,
andre@0:                             0x53, 0x6a, 0xf4, 0x90, 0xc2, 0x2c, 0x49, 0xe2,
andre@0:                             0xfa, 0xeb, 0x91, 0xc5, 0xe5, 0x83, 0x13, 0xc9,
andre@0:                             0x44, 0x4b, 0x95, 0x2c, 0x57, 0x70, 0x15, 0x5c,
andre@0:                             0x64, 0x8d, 0x1a, 0xfd, 0x2a, 0xc7, 0xb2, 0x9c,
andre@0:                             0x5c, 0x99, 0xd3, 0x4a, 0xfd, 0xdd, 0xf6, 0x82,
andre@0:                             0x87, 0x8c, 0x5a, 0xc4, 0xa8, 0x0d, 0x2a, 0xef,
andre@0:                             0xc3, 0xa2, 0x7e, 0x8e, 0x67, 0x9f, 0x6f, 0x63,
andre@0:                             0xdb, 0xbb, 0x1d, 0x31, 0xc4, 0xbb, 0xbc, 0x13,
andre@0:                             0x3f, 0x54, 0xc6, 0xf6, 0xc5, 0x28, 0x32, 0xab,
andre@0:                             0x96, 0x42, 0x10, 0x36, 0x40, 0x92, 0xbb, 0x57,
andre@0:                             0x55, 0x38, 0xf5, 0x43, 0x7e, 0x43, 0xc4, 0x65,
andre@0:                             0x47, 0x64, 0xaa, 0x0f, 0x4c, 0xe9, 0x49, 0x16,
andre@0:                             0xec, 0x6a, 0x50, 0xfd, 0x14, 0x49, 0xca, 0xdb,
andre@0:                             0x44, 0x54, 0xca, 0xbe, 0xa3, 0x0e, 0x5f, 0xef};
andre@0: 
andre@0:     /* RSA Known Signed Hash (2048-bits). */
andre@0:     static const PRUint8 rsa_known_sha256_signature[] = {
andre@0:                             0x8c, 0x2d, 0x2e, 0xfb, 0x37, 0xb5, 0x6f, 0x38,
andre@0:                             0x9f, 0x06, 0x5a, 0xf3, 0x8c, 0xa0, 0xd0, 0x7a,
andre@0:                             0xde, 0xcf, 0xf9, 0x14, 0x95, 0x59, 0xd3, 0x5f,
andre@0:                             0x51, 0x5d, 0x5d, 0xad, 0xd8, 0x71, 0x33, 0x50,
andre@0:                             0x1d, 0x03, 0x3b, 0x3a, 0x32, 0x00, 0xb4, 0xde,
andre@0:                             0x7f, 0xe4, 0xb1, 0xe5, 0x6b, 0x83, 0xf4, 0x80,
andre@0:                             0x10, 0x3b, 0xb8, 0x8a, 0xdb, 0xe8, 0x0a, 0x42,
andre@0:                             0x9e, 0x8d, 0xd7, 0xbe, 0xed, 0xde, 0x5a, 0x3d,
andre@0:                             0xc6, 0xdb, 0xfe, 0x49, 0x6a, 0xe9, 0x1e, 0x75,
andre@0:                             0x66, 0xf1, 0x3f, 0x9e, 0x3f, 0xff, 0x05, 0x65,
andre@0:                             0xde, 0xca, 0x62, 0x62, 0xf3, 0xec, 0x53, 0x09,
andre@0:                             0xa0, 0x37, 0xd5, 0x66, 0x62, 0x72, 0x14, 0xb6,
andre@0:                             0x51, 0x32, 0x67, 0x50, 0xc1, 0xe1, 0x2f, 0x9e,
andre@0:                             0x98, 0x4e, 0x53, 0x96, 0x55, 0x4b, 0xc4, 0x92,
andre@0:                             0xc3, 0xb4, 0x80, 0xf0, 0x35, 0xc9, 0x00, 0x4b,
andre@0:                             0x5c, 0x85, 0x92, 0xb1, 0xe8, 0x6e, 0xa5, 0x51,
andre@0:                             0x38, 0x9f, 0xc9, 0x11, 0xb6, 0x14, 0xdf, 0x34,
andre@0:                             0x64, 0x40, 0x82, 0x82, 0xde, 0x16, 0x69, 0x93,
andre@0:                             0x89, 0x4e, 0x5c, 0x32, 0xf2, 0x0a, 0x4e, 0x9e,
andre@0:                             0xbd, 0x63, 0x99, 0x4f, 0xf3, 0x15, 0x90, 0xc2,
andre@0:                             0xfe, 0x6f, 0xb7, 0xf4, 0xad, 0xd4, 0x8e, 0x0b,
andre@0:                             0xd2, 0xf5, 0x22, 0xd2, 0x71, 0x65, 0x13, 0xf7,
andre@0:                             0x82, 0x7b, 0x75, 0xb6, 0xc1, 0xb4, 0x45, 0xbd,
andre@0:                             0x8f, 0x95, 0xcf, 0x5b, 0x95, 0x32, 0xef, 0x18,
andre@0:                             0x5f, 0xd3, 0xdf, 0x7e, 0x22, 0xdd, 0x25, 0xeb,
andre@0:                             0xe1, 0xbf, 0x3b, 0x9a, 0x55, 0x75, 0x4f, 0x3c,
andre@0:                             0x38, 0x67, 0x57, 0x04, 0x04, 0x57, 0x27, 0xf6,
andre@0:                             0x34, 0x0e, 0x57, 0x8a, 0x7c, 0xff, 0x7d, 0xca,
andre@0:                             0x8c, 0x06, 0xf8, 0x9d, 0xdb, 0xe4, 0xd8, 0x19,
andre@0:                             0xdd, 0x4d, 0xfd, 0x8f, 0xa0, 0x06, 0x53, 0xe8,
andre@0:                             0x33, 0x00, 0x70, 0x3f, 0x6b, 0xc3, 0xbd, 0x9a,
andre@0:                             0x78, 0xb5, 0xa9, 0xef, 0x6d, 0xda, 0x67, 0x92};
andre@0: 
andre@0:    /* RSA Known Signed Hash (2048-bits). */
andre@0:    static const PRUint8 rsa_known_sha384_signature[] = {
andre@0:                             0x20, 0x2d, 0x21, 0x3a, 0xaa, 0x1e, 0x05, 0x15,
andre@0:                             0x5c, 0xca, 0x84, 0x86, 0xc0, 0x15, 0x81, 0xdf,
andre@0:                             0xd4, 0x06, 0x9f, 0xe0, 0xc1, 0xed, 0xef, 0x0f,
andre@0:                             0xfe, 0xb3, 0xc3, 0xbb, 0x28, 0xa5, 0x56, 0xbf,
andre@0:                             0xe3, 0x11, 0x5c, 0xc2, 0xc0, 0x0b, 0xfa, 0xfa,
andre@0:                             0x3d, 0xd3, 0x06, 0x20, 0xe2, 0xc9, 0xe4, 0x66,
andre@0:                             0x28, 0xb7, 0xc0, 0x3b, 0x3c, 0x96, 0xc6, 0x49,
andre@0:                             0x3b, 0xcf, 0x86, 0x49, 0x31, 0xaf, 0x5b, 0xa3,
andre@0:                             0xec, 0x63, 0x10, 0xdf, 0xda, 0x2f, 0x68, 0xac,
andre@0:                             0x7b, 0x3a, 0x49, 0xfa, 0xe6, 0x0d, 0xfe, 0x37,
andre@0:                             0x17, 0x56, 0x8e, 0x5c, 0x48, 0x97, 0x43, 0xf7,
andre@0:                             0xa0, 0xbc, 0xe3, 0x4b, 0x42, 0xde, 0x58, 0x1d,
andre@0:                             0xd9, 0x5d, 0xb3, 0x08, 0x35, 0xbd, 0xa4, 0xe1,
andre@0:                             0x80, 0xc3, 0x64, 0xab, 0x21, 0x97, 0xad, 0xfb,
andre@0:                             0x71, 0xee, 0xa3, 0x3d, 0x9c, 0xaa, 0xfa, 0x16,
andre@0:                             0x60, 0x46, 0x32, 0xda, 0x44, 0x2e, 0x10, 0x92,
andre@0:                             0x20, 0xd8, 0x98, 0x80, 0x84, 0x75, 0x5b, 0x70,
andre@0:                             0x91, 0x00, 0x33, 0x19, 0x69, 0xc9, 0x2a, 0xec,
andre@0:                             0x3d, 0xe5, 0x5f, 0x0f, 0x9a, 0xa7, 0x97, 0x1f,
andre@0:                             0x79, 0xc3, 0x1d, 0x65, 0x74, 0x62, 0xc5, 0xa1,
andre@0:                             0x23, 0x65, 0x4b, 0x84, 0xa1, 0x03, 0x98, 0xf3,
andre@0:                             0xf1, 0x02, 0x24, 0xca, 0xe5, 0xd4, 0xc8, 0xa2,
andre@0:                             0x30, 0xad, 0x72, 0x7d, 0x29, 0x60, 0x1a, 0x8e,
andre@0:                             0x6f, 0x23, 0xa4, 0xda, 0x68, 0xa4, 0x45, 0x9c,
andre@0:                             0x39, 0x70, 0x44, 0x18, 0x4b, 0x73, 0xfe, 0xf8,
andre@0:                             0x33, 0x53, 0x1d, 0x7e, 0x93, 0x93, 0xac, 0xc7,
andre@0:                             0x1e, 0x6e, 0x6b, 0xfd, 0x9e, 0xba, 0xa6, 0x71,
andre@0:                             0x70, 0x47, 0x6a, 0xd6, 0x82, 0x32, 0xa2, 0x6e,
andre@0:                             0x20, 0x72, 0xb0, 0xba, 0xec, 0x91, 0xbb, 0x6b,
andre@0:                             0xcc, 0x84, 0x0a, 0x33, 0x2b, 0x8a, 0x8d, 0xeb,
andre@0:                             0x71, 0xcd, 0xca, 0x67, 0x1b, 0xad, 0x10, 0xd4,
andre@0:                             0xce, 0x4f, 0xc0, 0x29, 0xec, 0xfa, 0xed, 0xfa};
andre@0: 
andre@0:    /* RSA Known Signed Hash (2048-bits). */
andre@0:    static const PRUint8 rsa_known_sha512_signature[] = {
andre@0:                             0x35, 0x0e, 0x74, 0x9d, 0xeb, 0xc7, 0x67, 0x31,
andre@0:                             0x9f, 0xff, 0x0b, 0xbb, 0x5e, 0x66, 0xb4, 0x2f,
andre@0:                             0xbf, 0x72, 0x60, 0x4f, 0xe9, 0xbd, 0xec, 0xc8,
andre@0:                             0x17, 0x79, 0x5f, 0x39, 0x83, 0xb4, 0x54, 0x2e,
andre@0:                             0x01, 0xb9, 0xd3, 0x20, 0x47, 0xcb, 0xd4, 0x42,
andre@0:                             0xf2, 0x6e, 0x36, 0xc1, 0x97, 0xad, 0xef, 0x8e,
andre@0:                             0xe6, 0x51, 0xee, 0x5e, 0x9e, 0x88, 0xb4, 0x9d,
andre@0:                             0xda, 0x3e, 0x77, 0x4b, 0xe8, 0xae, 0x48, 0x53,
andre@0:                             0x2c, 0xc4, 0xd3, 0x25, 0x6b, 0x23, 0xb7, 0x54,
andre@0:                             0x3c, 0x95, 0x8f, 0xfb, 0x6f, 0x6d, 0xc5, 0x56,
andre@0:                             0x39, 0x69, 0x28, 0x0e, 0x74, 0x9b, 0x31, 0xe8,
andre@0:                             0x76, 0x77, 0x2b, 0xc1, 0x44, 0x89, 0x81, 0x93,
andre@0:                             0xfc, 0xf6, 0xec, 0x5f, 0x8f, 0x89, 0xfc, 0x1d,
andre@0:                             0xa4, 0x53, 0x58, 0x8c, 0xe9, 0xc0, 0xc0, 0x26,
andre@0:                             0xe6, 0xdf, 0x6d, 0x27, 0xb1, 0x8e, 0x3e, 0xb6,
andre@0:                             0x47, 0xe1, 0x02, 0x96, 0xc2, 0x5f, 0x7f, 0x3d,
andre@0:                             0xc5, 0x6c, 0x2f, 0xea, 0xaa, 0x5e, 0x39, 0xfc,
andre@0:                             0x77, 0xca, 0x00, 0x02, 0x5c, 0x64, 0x7c, 0xce,
andre@0:                             0x7d, 0x63, 0x82, 0x05, 0xed, 0xf7, 0x5b, 0x55,
andre@0:                             0x58, 0xc0, 0xeb, 0x76, 0xd7, 0x95, 0x55, 0x37,
andre@0:                             0x85, 0x7d, 0x17, 0xad, 0xd2, 0x11, 0xfd, 0x97,
andre@0:                             0x48, 0xb5, 0xc2, 0x5e, 0xc7, 0x62, 0xc0, 0xe0,
andre@0:                             0x68, 0xa8, 0x61, 0x14, 0x41, 0xca, 0x25, 0x3a,
andre@0:                             0xec, 0x48, 0x54, 0x22, 0x83, 0x2b, 0x69, 0x54,
andre@0:                             0xfd, 0xc8, 0x99, 0x9a, 0xee, 0x37, 0x03, 0xa3,
andre@0:                             0x8f, 0x0f, 0x32, 0xb0, 0xaa, 0x74, 0x39, 0x04,
andre@0:                             0x7c, 0xd9, 0xc2, 0x8f, 0xbe, 0xf2, 0xc4, 0xbe,
andre@0:                             0xdd, 0x7a, 0x7a, 0x7f, 0x72, 0xd3, 0x80, 0x59,
andre@0:                             0x18, 0xa0, 0xa1, 0x2d, 0x6f, 0xa3, 0xa9, 0x48,
andre@0:                             0xed, 0x20, 0xa6, 0xea, 0xaa, 0x10, 0x83, 0x98,
andre@0:                             0x0c, 0x13, 0x69, 0x6e, 0xcd, 0x31, 0x6b, 0xd0,
andre@0:                             0x66, 0xa6, 0x5e, 0x30, 0x0c, 0x82, 0xd5, 0x81};
andre@0: 
andre@0:     static const RSAPublicKey    bl_public_key = { NULL,
andre@0:       { FIPS_RSA_TYPE, (unsigned char *)rsa_modulus,         
andre@0:                                         FIPS_RSA_MODULUS_LENGTH },
andre@0:       { FIPS_RSA_TYPE, (unsigned char *)rsa_public_exponent, 
andre@0:                                         FIPS_RSA_PUBLIC_EXPONENT_LENGTH }
andre@0:     };
andre@0:     static const RSAPrivateKey   bl_private_key = { NULL,
andre@0:       { FIPS_RSA_TYPE, (unsigned char *)rsa_version,          
andre@0:                                         FIPS_RSA_PRIVATE_VERSION_LENGTH },
andre@0:       { FIPS_RSA_TYPE, (unsigned char *)rsa_modulus,          
andre@0:                                         FIPS_RSA_MODULUS_LENGTH },
andre@0:       { FIPS_RSA_TYPE, (unsigned char *)rsa_public_exponent,  
andre@0:                                         FIPS_RSA_PUBLIC_EXPONENT_LENGTH },
andre@0:       { FIPS_RSA_TYPE, (unsigned char *)rsa_private_exponent, 
andre@0:                                         FIPS_RSA_PRIVATE_EXPONENT_LENGTH },
andre@0:       { FIPS_RSA_TYPE, (unsigned char *)rsa_prime0,           
andre@0:                                         FIPS_RSA_PRIME0_LENGTH },
andre@0:       { FIPS_RSA_TYPE, (unsigned char *)rsa_prime1,           
andre@0:                                         FIPS_RSA_PRIME1_LENGTH },
andre@0:       { FIPS_RSA_TYPE, (unsigned char *)rsa_exponent0,        
andre@0:                                         FIPS_RSA_EXPONENT0_LENGTH },
andre@0:       { FIPS_RSA_TYPE, (unsigned char *)rsa_exponent1,        
andre@0:                                         FIPS_RSA_EXPONENT1_LENGTH },
andre@0:       { FIPS_RSA_TYPE, (unsigned char *)rsa_coefficient,      
andre@0:                                         FIPS_RSA_COEFFICIENT_LENGTH }
andre@0:     };
andre@0: 
andre@0:     /* RSA variables. */
andre@0: #ifdef CREATE_TEMP_ARENAS
andre@0:     PLArenaPool *         rsa_public_arena;
andre@0:     PLArenaPool *         rsa_private_arena;
andre@0: #endif
andre@0:     NSSLOWKEYPublicKey *  rsa_public_key;
andre@0:     NSSLOWKEYPrivateKey * rsa_private_key;
andre@0:     SECStatus             rsa_status;
andre@0: 
andre@0:     NSSLOWKEYPublicKey    low_public_key   = { NULL, NSSLOWKEYRSAKey, };
andre@0:     NSSLOWKEYPrivateKey   low_private_key  = { NULL, NSSLOWKEYRSAKey, };
andre@0:     PRUint8               rsa_computed_ciphertext[FIPS_RSA_ENCRYPT_LENGTH];
andre@0:     PRUint8               rsa_computed_plaintext[FIPS_RSA_DECRYPT_LENGTH];
andre@0: 
andre@0:     /****************************************/
andre@0:     /* Compose RSA Public/Private Key Pair. */
andre@0:     /****************************************/
andre@0: 
andre@0:     low_public_key.u.rsa  = bl_public_key;
andre@0:     low_private_key.u.rsa = bl_private_key;
andre@0: 
andre@0:     rsa_public_key  = &low_public_key;
andre@0:     rsa_private_key = &low_private_key;
andre@0: 
andre@0: #ifdef CREATE_TEMP_ARENAS
andre@0:     /* Create some space for the RSA public key. */
andre@0:     rsa_public_arena = PORT_NewArena( NSS_SOFTOKEN_DEFAULT_CHUNKSIZE );
andre@0: 
andre@0:     if( rsa_public_arena == NULL ) {
andre@0:         PORT_SetError( SEC_ERROR_NO_MEMORY );
andre@0:         return( CKR_HOST_MEMORY );
andre@0:     }
andre@0: 
andre@0:     /* Create some space for the RSA private key. */
andre@0:     rsa_private_arena = PORT_NewArena( NSS_SOFTOKEN_DEFAULT_CHUNKSIZE );
andre@0: 
andre@0:     if( rsa_private_arena == NULL ) {
andre@0:         PORT_FreeArena( rsa_public_arena, PR_TRUE );
andre@0:         PORT_SetError( SEC_ERROR_NO_MEMORY );
andre@0:         return( CKR_HOST_MEMORY );
andre@0:     }
andre@0: 
andre@0:     rsa_public_key->arena = rsa_public_arena;
andre@0:     rsa_private_key->arena = rsa_private_arena;
andre@0: #endif
andre@0: 
andre@0:     /**************************************************/
andre@0:     /* RSA Single-Round Known Answer Encryption Test. */
andre@0:     /**************************************************/
andre@0: 
andre@0:     /* Perform RSA Public Key Encryption. */
andre@0:     rsa_status = RSA_PublicKeyOp(&rsa_public_key->u.rsa,
andre@0:                                  rsa_computed_ciphertext,
andre@0:                                  rsa_known_plaintext_msg);
andre@0: 
andre@0:     if( ( rsa_status != SECSuccess ) ||
andre@0:         ( PORT_Memcmp( rsa_computed_ciphertext, rsa_known_ciphertext,
andre@0:                        FIPS_RSA_ENCRYPT_LENGTH ) != 0 ) )
andre@0:         goto rsa_loser;
andre@0: 
andre@0:     /**************************************************/
andre@0:     /* RSA Single-Round Known Answer Decryption Test. */
andre@0:     /**************************************************/
andre@0: 
andre@0:     /* Perform RSA Private Key Decryption. */
andre@0:     rsa_status = RSA_PrivateKeyOp(&rsa_private_key->u.rsa,
andre@0:                                   rsa_computed_plaintext,
andre@0:                                   rsa_known_ciphertext);
andre@0: 
andre@0:     if( ( rsa_status != SECSuccess ) ||
andre@0:         ( PORT_Memcmp( rsa_computed_plaintext, rsa_known_plaintext_msg,
andre@0:                        FIPS_RSA_DECRYPT_LENGTH ) != 0 ) )
andre@0:         goto rsa_loser;
andre@0: 
andre@0:     rsa_status = sftk_fips_RSA_PowerUpSigSelfTest (HASH_AlgSHA256,
andre@0:                            rsa_public_key, rsa_private_key,
andre@0:                            rsa_known_plaintext_msg, FIPS_RSA_MESSAGE_LENGTH,
andre@0:                            rsa_known_sha256_signature);
andre@0:     if( rsa_status != SECSuccess )
andre@0:         goto rsa_loser;
andre@0: 
andre@0:     rsa_status = sftk_fips_RSA_PowerUpSigSelfTest (HASH_AlgSHA384,
andre@0:                            rsa_public_key, rsa_private_key,
andre@0:                            rsa_known_plaintext_msg, FIPS_RSA_MESSAGE_LENGTH,
andre@0:                            rsa_known_sha384_signature);
andre@0:     if( rsa_status != SECSuccess )
andre@0:         goto rsa_loser;
andre@0: 
andre@0:     rsa_status = sftk_fips_RSA_PowerUpSigSelfTest (HASH_AlgSHA512,
andre@0:                            rsa_public_key, rsa_private_key,
andre@0:                            rsa_known_plaintext_msg, FIPS_RSA_MESSAGE_LENGTH,
andre@0:                            rsa_known_sha512_signature);
andre@0:     if( rsa_status != SECSuccess )
andre@0:         goto rsa_loser;
andre@0: 
andre@0:     /* Dispose of all RSA key material. */
andre@0:     nsslowkey_DestroyPublicKey( rsa_public_key );
andre@0:     nsslowkey_DestroyPrivateKey( rsa_private_key );
andre@0: 
andre@0:     return( CKR_OK );
andre@0: 
andre@0: rsa_loser:
andre@0: 
andre@0:     nsslowkey_DestroyPublicKey( rsa_public_key );
andre@0:     nsslowkey_DestroyPrivateKey( rsa_private_key );
andre@0: 
andre@0:     return( CKR_DEVICE_ERROR );
andre@0: }
andre@0: 
andre@0: #ifndef NSS_DISABLE_ECC
andre@0: 
andre@0: static CK_RV
andre@0: sftk_fips_ECDSA_Test(const PRUint8 *encodedParams, 
andre@0:                      unsigned int encodedParamsLen,
andre@0:                      const PRUint8 *knownSignature, 
andre@0:                      unsigned int knownSignatureLen) {
andre@0: 
andre@0:     /* ECDSA Known Seed info for curves nistp256 and nistk283  */
andre@0:     static const PRUint8 ecdsa_Known_Seed[] = {
andre@0:                             0x6a, 0x9b, 0xf6, 0xf7, 0xce, 0xed, 0x79, 0x11,
andre@0:                             0xf0, 0xc7, 0xc8, 0x9a, 0xa5, 0xd1, 0x57, 0xb1,
andre@0:                             0x7b, 0x5a, 0x3b, 0x76, 0x4e, 0x7b, 0x7c, 0xbc,
andre@0:                             0xf2, 0x76, 0x1c, 0x1c, 0x7f, 0xc5, 0x53, 0x2f};
andre@0: 
andre@0:     static const PRUint8 msg[] = {
andre@0:                             "Firefox and ThunderBird are awesome!"};
andre@0: 
andre@0:     unsigned char sha1[SHA1_LENGTH];  /* SHA-1 hash (160 bits) */
andre@0:     unsigned char sig[2*MAX_ECKEY_LEN];
andre@0:     SECItem signature, digest;
andre@0:     SECItem encodedparams;
andre@0:     ECParams *ecparams = NULL;
andre@0:     ECPrivateKey *ecdsa_private_key = NULL;
andre@0:     ECPublicKey ecdsa_public_key;
andre@0:     SECStatus ecdsaStatus = SECSuccess;
andre@0: 
andre@0:     /* construct the ECDSA private/public key pair */
andre@0:     encodedparams.type = siBuffer;
andre@0:     encodedparams.data = (unsigned char *) encodedParams;
andre@0:     encodedparams.len = encodedParamsLen;
andre@0:     
andre@0:     if (EC_DecodeParams(&encodedparams, &ecparams) != SECSuccess) {
andre@0:         return( CKR_DEVICE_ERROR );
andre@0:     }
andre@0: 
andre@0:     /* Generates a new EC key pair. The private key is a supplied
andre@0:      * random value (in seed) and the public key is the result of 
andre@0:      * performing a scalar point multiplication of that value with 
andre@0:      * the curve's base point.
andre@0:      */
andre@0:     ecdsaStatus = EC_NewKeyFromSeed(ecparams, &ecdsa_private_key, 
andre@0:                                    ecdsa_Known_Seed, 
andre@0:                                    sizeof(ecdsa_Known_Seed));
andre@0:     /* free the ecparams they are no longer needed */
andre@0:     PORT_FreeArena(ecparams->arena, PR_FALSE);
andre@0:     ecparams = NULL;
andre@0:     if (ecdsaStatus != SECSuccess) {
andre@0:         return ( CKR_DEVICE_ERROR );    
andre@0:     }
andre@0: 
andre@0:     /* construct public key from private key. */
andre@0:     ecdsaStatus = EC_CopyParams(ecdsa_private_key->ecParams.arena, 
andre@0:                                 &ecdsa_public_key.ecParams,
andre@0:                                 &ecdsa_private_key->ecParams);
andre@0:     if (ecdsaStatus != SECSuccess) {
andre@0:         goto loser;
andre@0:     }
andre@0:     ecdsa_public_key.publicValue = ecdsa_private_key->publicValue;
andre@0: 
andre@0:     /* validate public key value */
andre@0:     ecdsaStatus = EC_ValidatePublicKey(&ecdsa_public_key.ecParams, 
andre@0:                                        &ecdsa_public_key.publicValue);
andre@0:     if (ecdsaStatus != SECSuccess) {
andre@0:         goto loser;
andre@0:     }
andre@0: 
andre@0:     /* validate public key value */
andre@0:     ecdsaStatus = EC_ValidatePublicKey(&ecdsa_private_key->ecParams, 
andre@0:                                        &ecdsa_private_key->publicValue);
andre@0:     if (ecdsaStatus != SECSuccess) {
andre@0:         goto loser;
andre@0:     }
andre@0: 
andre@0:     /***************************************************/
andre@0:     /* ECDSA Single-Round Known Answer Signature Test. */
andre@0:     /***************************************************/
andre@0:     
andre@0:     ecdsaStatus = SHA1_HashBuf(sha1, msg, sizeof msg);
andre@0:     if (ecdsaStatus != SECSuccess) {
andre@0:         goto loser;
andre@0:     }
andre@0:     digest.type = siBuffer;
andre@0:     digest.data = sha1;
andre@0:     digest.len = SHA1_LENGTH;
andre@0:     
andre@0:     memset(sig, 0, sizeof sig);
andre@0:     signature.type = siBuffer;
andre@0:     signature.data = sig;
andre@0:     signature.len = sizeof sig;
andre@0:     
andre@0:     ecdsaStatus = ECDSA_SignDigestWithSeed(ecdsa_private_key, &signature, 
andre@0:                          &digest, ecdsa_Known_Seed, sizeof ecdsa_Known_Seed);
andre@0:     if (ecdsaStatus != SECSuccess) {
andre@0:         goto loser;
andre@0:     }
andre@0: 
andre@0:     if( ( signature.len != knownSignatureLen ) ||
andre@0:         ( PORT_Memcmp( signature.data, knownSignature,
andre@0:                     knownSignatureLen ) != 0 ) ) {
andre@0:         ecdsaStatus = SECFailure;
andre@0:         goto loser;
andre@0:     }
andre@0:     
andre@0:     /******************************************************/
andre@0:     /* ECDSA Single-Round Known Answer Verification Test. */
andre@0:     /******************************************************/
andre@0: 
andre@0:     /* Perform ECDSA verification process. */
andre@0:     ecdsaStatus = ECDSA_VerifyDigest(&ecdsa_public_key, &signature, &digest);
andre@0: 
andre@0: loser:
andre@0:     /* free the memory for the private key arena*/
andre@0:     if (ecdsa_private_key != NULL) {
andre@0:         PORT_FreeArena(ecdsa_private_key->ecParams.arena, PR_FALSE);
andre@0:     }
andre@0: 
andre@0:     if (ecdsaStatus != SECSuccess) {
andre@0:         return CKR_DEVICE_ERROR ;
andre@0:     }
andre@0:     return( CKR_OK );
andre@0: }
andre@0: 
andre@0: static CK_RV
andre@0: sftk_fips_ECDSA_PowerUpSelfTest() {
andre@0: 
andre@0:    /* ECDSA Known curve nistp256 == SEC_OID_SECG_EC_SECP256R1 params */
andre@0:     static const PRUint8 ecdsa_known_P256_EncodedParams[] = {
andre@0:                             0x06,0x08,0x2a,0x86,0x48,0xce,0x3d,0x03,
andre@0:                             0x01,0x07};
andre@0: 
andre@0:     static const PRUint8 ecdsa_known_P256_signature[] = {
andre@0:                             0x07,0xb1,0xcb,0x57,0x20,0xa7,0x10,0xd6, 
andre@0:                             0x9d,0x37,0x4b,0x1c,0xdc,0x35,0x90,0xff, 
andre@0:                             0x1a,0x2d,0x98,0x95,0x1b,0x2f,0xeb,0x7f, 
andre@0:                             0xbb,0x81,0xca,0xc0,0x69,0x75,0xea,0xc5,
andre@0:                             0x59,0x6a,0x62,0x49,0x3d,0x50,0xc9,0xe1, 
andre@0:                             0x27,0x3b,0xff,0x9b,0x13,0x66,0x67,0xdd, 
andre@0:                             0x7d,0xd1,0x0d,0x2d,0x7c,0x44,0x04,0x1b, 
andre@0:                             0x16,0x21,0x12,0xc5,0xcb,0xbd,0x9e,0x75};
andre@0: 
andre@0: #ifdef NSS_ECC_MORE_THAN_SUITE_B
andre@0:     /* ECDSA Known curve nistk283 == SEC_OID_SECG_EC_SECT283K1 params */
andre@0:     static const PRUint8 ecdsa_known_K283_EncodedParams[] = {
andre@0:                             0x06,0x05,0x2b,0x81,0x04,0x00,0x10};
andre@0:                          
andre@0:     static const PRUint8 ecdsa_known_K283_signature[] = {
andre@0:                             0x00,0x45,0x88,0xc0,0x79,0x09,0x07,0xd1, 
andre@0:                             0x4e,0x88,0xe6,0xd5,0x2f,0x22,0x04,0x74, 
andre@0:                             0x35,0x24,0x65,0xe8,0x15,0xde,0x90,0x66, 
andre@0:                             0x94,0x70,0xdd,0x3a,0x14,0x70,0x02,0xd1,
andre@0:                             0xef,0x86,0xbd,0x15,0x00,0xd9,0xdc,0xfc, 
andre@0:                             0x87,0x2e,0x7c,0x99,0xe2,0xe3,0x79,0xb8, 
andre@0:                             0xd9,0x10,0x49,0x78,0x4b,0x59,0x8b,0x05, 
andre@0:                             0x77,0xec,0x6c,0xe8,0x35,0xe6,0x2e,0xa9,
andre@0:                             0xf9,0x77,0x1f,0x71,0x86,0xa5,0x4a,0xd0};
andre@0: #endif
andre@0: 
andre@0:     CK_RV crv;
andre@0: 
andre@0:     /* ECDSA GF(p) prime field curve test */
andre@0:     crv = sftk_fips_ECDSA_Test(ecdsa_known_P256_EncodedParams,
andre@0:                                sizeof ecdsa_known_P256_EncodedParams,
andre@0:                                ecdsa_known_P256_signature,
andre@0:                                sizeof ecdsa_known_P256_signature );
andre@0:     if (crv != CKR_OK) {
andre@0:         return( CKR_DEVICE_ERROR );
andre@0:     }
andre@0: 
andre@0: #ifdef NSS_ECC_MORE_THAN_SUITE_B
andre@0:     /* ECDSA GF(2m) binary field curve test */
andre@0:     crv = sftk_fips_ECDSA_Test(ecdsa_known_K283_EncodedParams,
andre@0:                                sizeof ecdsa_known_K283_EncodedParams,
andre@0:                                ecdsa_known_K283_signature,
andre@0:                                sizeof ecdsa_known_K283_signature );
andre@0:     if (crv != CKR_OK) {
andre@0:         return( CKR_DEVICE_ERROR );
andre@0:     }
andre@0: #endif
andre@0: 
andre@0:     return( CKR_OK );
andre@0: }
andre@0: 
andre@0: #endif /* NSS_DISABLE_ECC */
andre@0: 
andre@0: static CK_RV
andre@0: sftk_fips_DSA_PowerUpSelfTest( void )
andre@0: {
andre@0:     /* DSA Known P (1024-bits), Q (160-bits), and G (1024-bits) Values. */
andre@0:     static const PRUint8 dsa_P[] = {
andre@0:          0x80,0xb0,0xd1,0x9d,0x6e,0xa4,0xf3,0x28, 
andre@0:          0x9f,0x24,0xa9,0x8a,0x49,0xd0,0x0c,0x63, 
andre@0:          0xe8,0x59,0x04,0xf9,0x89,0x4a,0x5e,0xc0, 
andre@0:          0x6d,0xd2,0x67,0x6b,0x37,0x81,0x83,0x0c,
andre@0:          0xfe,0x3a,0x8a,0xfd,0xa0,0x3b,0x08,0x91, 
andre@0:          0x1c,0xcb,0xb5,0x63,0xb0,0x1c,0x70,0xd0, 
andre@0:          0xae,0xe1,0x60,0x2e,0x12,0xeb,0x54,0xc7, 
andre@0:          0xcf,0xc6,0xcc,0xae,0x97,0x52,0x32,0x63,
andre@0:          0xd3,0xeb,0x55,0xea,0x2f,0x4c,0xd5,0xd7, 
andre@0:          0x3f,0xda,0xec,0x49,0x27,0x0b,0x14,0x56, 
andre@0:          0xc5,0x09,0xbe,0x4d,0x09,0x15,0x75,0x2b, 
andre@0:          0xa3,0x42,0x0d,0x03,0x71,0xdf,0x0f,0xf4,
andre@0:          0x0e,0xe9,0x0c,0x46,0x93,0x3d,0x3f,0xa6, 
andre@0:          0x6c,0xdb,0xca,0xe5,0xac,0x96,0xc8,0x64, 
andre@0:          0x5c,0xec,0x4b,0x35,0x65,0xfc,0xfb,0x5a, 
andre@0:          0x1b,0x04,0x1b,0xa1,0x0e,0xfd,0x88,0x15};
andre@0:         
andre@0:     static const PRUint8 dsa_Q[] = {
andre@0:          0xad,0x22,0x59,0xdf,0xe5,0xec,0x4c,0x6e, 
andre@0:          0xf9,0x43,0xf0,0x4b,0x2d,0x50,0x51,0xc6, 
andre@0:          0x91,0x99,0x8b,0xcf};
andre@0:         
andre@0:     static const PRUint8 dsa_G[] = {
andre@0:          0x78,0x6e,0xa9,0xd8,0xcd,0x4a,0x85,0xa4, 
andre@0:          0x45,0xb6,0x6e,0x5d,0x21,0x50,0x61,0xf6, 
andre@0:          0x5f,0xdf,0x5c,0x7a,0xde,0x0d,0x19,0xd3, 
andre@0:          0xc1,0x3b,0x14,0xcc,0x8e,0xed,0xdb,0x17,
andre@0:          0xb6,0xca,0xba,0x86,0xa9,0xea,0x51,0x2d, 
andre@0:          0xc1,0xa9,0x16,0xda,0xf8,0x7b,0x59,0x8a, 
andre@0:          0xdf,0xcb,0xa4,0x67,0x00,0x44,0xea,0x24, 
andre@0:          0x73,0xe5,0xcb,0x4b,0xaf,0x2a,0x31,0x25,
andre@0:          0x22,0x28,0x3f,0x16,0x10,0x82,0xf7,0xeb, 
andre@0:          0x94,0x0d,0xdd,0x09,0x22,0x14,0x08,0x79, 
andre@0:          0xba,0x11,0x0b,0xf1,0xff,0x2d,0x67,0xac, 
andre@0:          0xeb,0xb6,0x55,0x51,0x69,0x97,0xa7,0x25,
andre@0:          0x6b,0x9c,0xa0,0x9b,0xd5,0x08,0x9b,0x27, 
andre@0:          0x42,0x1c,0x7a,0x69,0x57,0xe6,0x2e,0xed, 
andre@0:          0xa9,0x5b,0x25,0xe8,0x1f,0xd2,0xed,0x1f, 
andre@0:          0xdf,0xe7,0x80,0x17,0xba,0x0d,0x4d,0x38};
andre@0: 
andre@0:     /* DSA Known Random Values (known random key block       is 160-bits)  */
andre@0:     /*                     and (known random signature block is 160-bits). */
andre@0:     static const PRUint8 dsa_known_random_key_block[] = {
andre@0:                                                       "Mozilla Rules World!"};
andre@0:     static const PRUint8 dsa_known_random_signature_block[] = {
andre@0:                                                       "Random DSA Signature"};
andre@0: 
andre@0:     /* DSA Known Digest (160-bits) */
andre@0:     static const PRUint8 dsa_known_digest[] = { "DSA Signature Digest" };
andre@0: 
andre@0:     /* DSA Known Signature (320-bits). */
andre@0:     static const PRUint8 dsa_known_signature[] = {
andre@0:         0x25,0x7c,0x3a,0x79,0x32,0x45,0xb7,0x32, 
andre@0:         0x70,0xca,0x62,0x63,0x2b,0xf6,0x29,0x2c, 
andre@0:         0x22,0x2a,0x03,0xce,0x48,0x15,0x11,0x72, 
andre@0:         0x7b,0x7e,0xf5,0x7a,0xf3,0x10,0x3b,0xde,
andre@0:         0x34,0xc1,0x9e,0xd7,0x27,0x9e,0x77,0x38};
andre@0: 
andre@0:     /* DSA variables. */
andre@0:     DSAPrivateKey *        dsa_private_key;
andre@0:     SECStatus              dsa_status;
andre@0:     SECItem                dsa_signature_item;
andre@0:     SECItem                dsa_digest_item;
andre@0:     DSAPublicKey           dsa_public_key;
andre@0:     PRUint8                dsa_computed_signature[FIPS_DSA_SIGNATURE_LENGTH];
andre@0:     static const PQGParams dsa_pqg = { NULL,
andre@0: 			    { FIPS_DSA_TYPE, (unsigned char *)dsa_P, FIPS_DSA_PRIME_LENGTH },
andre@0: 			    { FIPS_DSA_TYPE, (unsigned char *)dsa_Q, FIPS_DSA_SUBPRIME_LENGTH },
andre@0: 			    { FIPS_DSA_TYPE, (unsigned char *)dsa_G, FIPS_DSA_BASE_LENGTH }};
andre@0: 
andre@0:     /*******************************************/
andre@0:     /* Generate a DSA public/private key pair. */
andre@0:     /*******************************************/
andre@0: 
andre@0:     /* Generate a DSA public/private key pair. */
andre@0:     dsa_status = DSA_NewKeyFromSeed(&dsa_pqg, dsa_known_random_key_block,
andre@0:                                     &dsa_private_key);
andre@0: 
andre@0:     if( dsa_status != SECSuccess )
andre@0:         return( CKR_HOST_MEMORY );
andre@0: 
andre@0:     /* construct public key from private key. */
andre@0:     dsa_public_key.params      = dsa_private_key->params;
andre@0:     dsa_public_key.publicValue = dsa_private_key->publicValue;
andre@0: 
andre@0:     /*************************************************/
andre@0:     /* DSA Single-Round Known Answer Signature Test. */
andre@0:     /*************************************************/
andre@0: 
andre@0:     dsa_signature_item.data = dsa_computed_signature;
andre@0:     dsa_signature_item.len  = sizeof dsa_computed_signature;
andre@0: 
andre@0:     dsa_digest_item.data    = (unsigned char *)dsa_known_digest;
andre@0:     dsa_digest_item.len     = SHA1_LENGTH;
andre@0: 
andre@0:     /* Perform DSA signature process. */
andre@0:     dsa_status = DSA_SignDigestWithSeed( dsa_private_key, 
andre@0:                                          &dsa_signature_item,
andre@0:                                          &dsa_digest_item,
andre@0:                                          dsa_known_random_signature_block );
andre@0: 
andre@0:     if( ( dsa_status != SECSuccess ) ||
andre@0:         ( dsa_signature_item.len != FIPS_DSA_SIGNATURE_LENGTH ) ||
andre@0:         ( PORT_Memcmp( dsa_computed_signature, dsa_known_signature,
andre@0:                        FIPS_DSA_SIGNATURE_LENGTH ) != 0 ) ) {
andre@0:         dsa_status = SECFailure;
andre@0:     } else {
andre@0: 
andre@0:     /****************************************************/
andre@0:     /* DSA Single-Round Known Answer Verification Test. */
andre@0:     /****************************************************/
andre@0: 
andre@0:     /* Perform DSA verification process. */
andre@0:     dsa_status = DSA_VerifyDigest( &dsa_public_key, 
andre@0:                                    &dsa_signature_item,
andre@0:                                    &dsa_digest_item);
andre@0:     }
andre@0: 
andre@0:     PORT_FreeArena(dsa_private_key->params.arena, PR_TRUE);
andre@0:     /* Don't free public key, it uses same arena as private key */
andre@0: 
andre@0:     /* Verify DSA signature. */
andre@0:     if( dsa_status != SECSuccess )
andre@0:         return( CKR_DEVICE_ERROR );
andre@0: 
andre@0:     return( CKR_OK );
andre@0: 
andre@0: 
andre@0: }
andre@0: 
andre@0: static CK_RV
andre@0: sftk_fips_RNG_PowerUpSelfTest( void )
andre@0: {
andre@0:    static const PRUint8 Q[] = {
andre@0: 			0x85,0x89,0x9c,0x77,0xa3,0x79,0xff,0x1a,
andre@0: 			0x86,0x6f,0x2f,0x3e,0x2e,0xf9,0x8c,0x9c,
andre@0: 			0x9d,0xef,0xeb,0xed};
andre@0:    static const PRUint8 GENX[] = {
andre@0: 			0x65,0x48,0xe3,0xca,0xac,0x64,0x2d,0xf7,
andre@0: 			0x7b,0xd3,0x4e,0x79,0xc9,0x7d,0xa6,0xa8,
andre@0: 			0xa2,0xc2,0x1f,0x8f,0xe9,0xb9,0xd3,0xa1,
andre@0: 			0x3f,0xf7,0x0c,0xcd,0xa6,0xca,0xbf,0xce,
andre@0: 			0x84,0x0e,0xb6,0xf1,0x0d,0xbe,0xa9,0xa3};
andre@0:    static const PRUint8 rng_known_DSAX[] = {
andre@0: 			0x7a,0x86,0xf1,0x7f,0xbd,0x4e,0x6e,0xd9,
andre@0: 			0x0a,0x26,0x21,0xd0,0x19,0xcb,0x86,0x73,
andre@0: 			0x10,0x1f,0x60,0xd7};
andre@0: 
andre@0: 
andre@0: 
andre@0:    SECStatus rng_status = SECSuccess;
andre@0:    PRUint8 DSAX[FIPS_DSA_SUBPRIME_LENGTH];
andre@0: 
andre@0:    /*******************************************/
andre@0:    /*   Run the SP 800-90 Health tests        */
andre@0:    /*******************************************/
andre@0:    rng_status = PRNGTEST_RunHealthTests();
andre@0:    if (rng_status != SECSuccess) {
andre@0: 	return (CKR_DEVICE_ERROR);
andre@0:    }
andre@0:   
andre@0:    /*******************************************/
andre@0:    /* Generate DSAX fow given Q.              */
andre@0:    /*******************************************/
andre@0: 
andre@0:    rng_status = FIPS186Change_ReduceModQForDSA(GENX, Q, DSAX);
andre@0: 
andre@0:    /* Verify DSAX to perform the RNG integrity check */
andre@0:    if( ( rng_status != SECSuccess ) ||
andre@0:        ( PORT_Memcmp( DSAX, rng_known_DSAX,
andre@0:                       (FIPS_DSA_SUBPRIME_LENGTH) ) != 0 ) )
andre@0:        return( CKR_DEVICE_ERROR );
andre@0:        
andre@0:    return( CKR_OK ); 
andre@0: }
andre@0: 
andre@0: static CK_RV
andre@0: sftk_fipsSoftwareIntegrityTest(void)
andre@0: {
andre@0:     CK_RV crv = CKR_OK;
andre@0: 
andre@0:     /* make sure that our check file signatures are OK */
andre@0:     if( !BLAPI_VerifySelf( NULL ) || 
andre@0: 	!BLAPI_SHVerify( SOFTOKEN_LIB_NAME, (PRFuncPtr) sftk_fips_HMAC ) ) {
andre@0: 	crv = CKR_DEVICE_ERROR; /* better error code? checksum error? */
andre@0:     }
andre@0:     return crv;
andre@0: }
andre@0: 
andre@0: CK_RV
andre@0: sftk_fipsPowerUpSelfTest( void )
andre@0: {
andre@0:     CK_RV rv;
andre@0: 
andre@0:     /* RC2 Power-Up SelfTest(s). */
andre@0:     rv = sftk_fips_RC2_PowerUpSelfTest();
andre@0: 
andre@0:     if( rv != CKR_OK )
andre@0:         return rv;
andre@0: 
andre@0:     /* RC4 Power-Up SelfTest(s). */
andre@0:     rv = sftk_fips_RC4_PowerUpSelfTest();
andre@0: 
andre@0:     if( rv != CKR_OK )
andre@0:         return rv;
andre@0: 
andre@0:     /* DES Power-Up SelfTest(s). */
andre@0:     rv = sftk_fips_DES_PowerUpSelfTest();
andre@0: 
andre@0:     if( rv != CKR_OK )
andre@0:         return rv;
andre@0: 
andre@0:     /* DES3 Power-Up SelfTest(s). */
andre@0:     rv = sftk_fips_DES3_PowerUpSelfTest();
andre@0: 
andre@0:     if( rv != CKR_OK )
andre@0:         return rv;
andre@0:  
andre@0:     /* AES Power-Up SelfTest(s) for 128-bit key. */
andre@0:     rv = sftk_fips_AES_PowerUpSelfTest(FIPS_AES_128_KEY_SIZE);
andre@0: 
andre@0:     if( rv != CKR_OK )
andre@0:         return rv;
andre@0: 
andre@0:     /* AES Power-Up SelfTest(s) for 192-bit key. */
andre@0:     rv = sftk_fips_AES_PowerUpSelfTest(FIPS_AES_192_KEY_SIZE);
andre@0: 
andre@0:     if( rv != CKR_OK )
andre@0:         return rv;
andre@0: 
andre@0:     /* AES Power-Up SelfTest(s) for 256-bit key. */
andre@0:     rv = sftk_fips_AES_PowerUpSelfTest(FIPS_AES_256_KEY_SIZE);
andre@0: 
andre@0:     if( rv != CKR_OK )
andre@0:         return rv;
andre@0: 
andre@0:     /* MD2 Power-Up SelfTest(s). */
andre@0:     rv = sftk_fips_MD2_PowerUpSelfTest();
andre@0: 
andre@0:     if( rv != CKR_OK )
andre@0:         return rv;
andre@0: 
andre@0:     /* MD5 Power-Up SelfTest(s). */
andre@0:     rv = sftk_fips_MD5_PowerUpSelfTest();
andre@0: 
andre@0:     if( rv != CKR_OK )
andre@0:         return rv;
andre@0: 
andre@0:     /* SHA-X Power-Up SelfTest(s). */
andre@0:     rv = sftk_fips_SHA_PowerUpSelfTest();
andre@0: 
andre@0:     if( rv != CKR_OK )
andre@0:         return rv;
andre@0: 
andre@0:     /* HMAC SHA-X Power-Up SelfTest(s). */
andre@0:     rv = sftk_fips_HMAC_PowerUpSelfTest();
andre@0:  
andre@0:     if( rv != CKR_OK )
andre@0:         return rv;
andre@0: 
andre@0:     /* RSA Power-Up SelfTest(s). */
andre@0:     rv = sftk_fips_RSA_PowerUpSelfTest();
andre@0: 
andre@0:     if( rv != CKR_OK )
andre@0:         return rv;
andre@0: 
andre@0:     /* DSA Power-Up SelfTest(s). */
andre@0:     rv = sftk_fips_DSA_PowerUpSelfTest();
andre@0: 
andre@0:     if( rv != CKR_OK )
andre@0:         return rv;
andre@0: 
andre@0:     /* RNG Power-Up SelfTest(s). */
andre@0:     rv = sftk_fips_RNG_PowerUpSelfTest();
andre@0: 
andre@0:     if( rv != CKR_OK )
andre@0:         return rv;
andre@0:     
andre@0: #ifndef NSS_DISABLE_ECC
andre@0:     /* ECDSA Power-Up SelfTest(s). */
andre@0:     rv = sftk_fips_ECDSA_PowerUpSelfTest();
andre@0: 
andre@0:     if( rv != CKR_OK )
andre@0:         return rv;
andre@0: #endif
andre@0: 
andre@0:     /* Software/Firmware Integrity Test. */
andre@0:     rv = sftk_fipsSoftwareIntegrityTest();
andre@0: 
andre@0:     if( rv != CKR_OK )
andre@0:         return rv;
andre@0: 
andre@0:     /* Passed Power-Up SelfTest(s). */
andre@0:     return( CKR_OK );
andre@0: }
andre@0: