fipstest.c
上传用户:lyxiangda
上传日期:2007-01-12
资源大小:3042k
文件大小:41k
源码类别:

CA认证

开发平台:

WINDOWS

  1. /*
  2.  * PKCS #11 FIPS Power-Up Self Test.
  3.  *
  4.  * The contents of this file are subject to the Mozilla Public
  5.  * License Version 1.1 (the "License"); you may not use this file
  6.  * except in compliance with the License. You may obtain a copy of
  7.  * the License at http://www.mozilla.org/MPL/
  8.  * 
  9.  * Software distributed under the License is distributed on an "AS
  10.  * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
  11.  * implied. See the License for the specific language governing
  12.  * rights and limitations under the License.
  13.  * 
  14.  * The Original Code is the Netscape security libraries.
  15.  * 
  16.  * The Initial Developer of the Original Code is Netscape
  17.  * Communications Corporation.  Portions created by Netscape are 
  18.  * Copyright (C) 1994-2000 Netscape Communications Corporation.  All
  19.  * Rights Reserved.
  20.  * 
  21.  * Contributor(s):
  22.  * 
  23.  * Alternatively, the contents of this file may be used under the
  24.  * terms of the GNU General Public License Version 2 or later (the
  25.  * "GPL"), in which case the provisions of the GPL are applicable 
  26.  * instead of those above.  If you wish to allow use of your 
  27.  * version of this file only under the terms of the GPL and not to
  28.  * allow others to use your version of this file under the MPL,
  29.  * indicate your decision by deleting the provisions above and
  30.  * replace them with the notice and other provisions required by
  31.  * the GPL.  If you do not delete the provisions above, a recipient
  32.  * may use your version of this file under either the MPL or the
  33.  * GPL.
  34.  *
  35.  * $Id: fipstest.c,v 1.1 2000/03/31 19:25:43 relyea%netscape.com Exp $
  36.  */
  37. #include "softoken.h"   /* Required for RC2-ECB, RC2-CBC, RC4, DES-ECB,  */
  38.                         /*              DES-CBC, DES3-ECB, DES3-CBC, RSA */
  39.                         /*              and DSA.                         */
  40. #include "seccomon.h"   /* Required for RSA and DSA. */
  41. #include "keylow.h"     /* Required for RSA and DSA. */
  42. #include "pkcs11.h"     /* Required for PKCS #11. */
  43. #include "secerr.h"
  44. /* FIPS preprocessor directives for RC2-ECB and RC2-CBC.        */
  45. #define FIPS_RC2_KEY_LENGTH                      5  /*  40-bits */
  46. #define FIPS_RC2_ENCRYPT_LENGTH                  8  /*  64-bits */
  47. #define FIPS_RC2_DECRYPT_LENGTH                  8  /*  64-bits */
  48. /* FIPS preprocessor directives for RC4.                        */
  49. #define FIPS_RC4_KEY_LENGTH                      5  /*  40-bits */
  50. #define FIPS_RC4_ENCRYPT_LENGTH                  8  /*  64-bits */
  51. #define FIPS_RC4_DECRYPT_LENGTH                  8  /*  64-bits */
  52. /* FIPS preprocessor directives for DES-ECB and DES-CBC.        */
  53. #define FIPS_DES_ENCRYPT_LENGTH                  8  /*  64-bits */
  54. #define FIPS_DES_DECRYPT_LENGTH                  8  /*  64-bits */
  55. /* FIPS preprocessor directives for DES3-CBC and DES3-ECB.      */
  56. #define FIPS_DES3_ENCRYPT_LENGTH                 8  /*  64-bits */
  57. #define FIPS_DES3_DECRYPT_LENGTH                 8  /*  64-bits */
  58. /* FIPS preprocessor directives for MD2.                        */
  59. #define FIPS_MD2_HASH_MESSAGE_LENGTH            64  /* 512-bits */
  60. /* FIPS preprocessor directives for MD5.                        */
  61. #define FIPS_MD5_HASH_MESSAGE_LENGTH            64  /* 512-bits */
  62. /* FIPS preprocessor directives for SHA-1.                      */
  63. #define FIPS_SHA1_HASH_MESSAGE_LENGTH           64  /* 512-bits */
  64. /* FIPS preprocessor directives for RSA.                        */
  65. #define FIPS_RSA_TYPE                           siBuffer
  66. #define FIPS_RSA_PUBLIC_EXPONENT_LENGTH          1  /*   8-bits */
  67. #define FIPS_RSA_PRIVATE_VERSION_LENGTH          1  /*   8-bits */
  68. #define FIPS_RSA_MESSAGE_LENGTH                 16  /* 128-bits */
  69. #define FIPS_RSA_COEFFICIENT_LENGTH             32  /* 256-bits */
  70. #define FIPS_RSA_PRIME0_LENGTH                  33  /* 264-bits */
  71. #define FIPS_RSA_PRIME1_LENGTH                  33  /* 264-bits */
  72. #define FIPS_RSA_EXPONENT0_LENGTH               33  /* 264-bits */
  73. #define FIPS_RSA_EXPONENT1_LENGTH               33  /* 264-bits */
  74. #define FIPS_RSA_PRIVATE_EXPONENT_LENGTH        64  /* 512-bits */
  75. #define FIPS_RSA_ENCRYPT_LENGTH                 64  /* 512-bits */
  76. #define FIPS_RSA_DECRYPT_LENGTH                 64  /* 512-bits */
  77. #define FIPS_RSA_CRYPTO_LENGTH                  64  /* 512-bits */
  78. #define FIPS_RSA_SIGNATURE_LENGTH               64  /* 512-bits */
  79. #define FIPS_RSA_MODULUS_LENGTH                 65  /* 520-bits */
  80. /* FIPS preprocessor directives for DSA.                        */
  81. #define FIPS_DSA_TYPE                           siBuffer
  82. #define FIPS_DSA_DIGEST_LENGTH                  20  /* 160-bits */
  83. #define FIPS_DSA_SUBPRIME_LENGTH                20  /* 160-bits */
  84. #define FIPS_DSA_SIGNATURE_LENGTH               40  /* 320-bits */
  85. #define FIPS_DSA_PRIME_LENGTH                   64  /* 512-bits */
  86. #define FIPS_DSA_BASE_LENGTH                    64  /* 512-bits */
  87. static CK_RV
  88. pk11_fips_RC2_PowerUpSelfTest( void )
  89. {
  90.     /* RC2 Known Key (40-bits). */
  91.     static PRUint8 rc2_known_key[] = { "RSARC" };
  92.     /* RC2-CBC Known Initialization Vector (64-bits). */
  93.     static PRUint8 rc2_cbc_known_initialization_vector[] = {"Security"};
  94.     /* RC2 Known Plaintext (64-bits). */
  95.     static PRUint8 rc2_ecb_known_plaintext[] = {"Netscape"};
  96.     static PRUint8 rc2_cbc_known_plaintext[] = {"Netscape"};
  97.     /* RC2 Known Ciphertext (64-bits). */
  98.     static PRUint8 rc2_ecb_known_ciphertext[] = {
  99.   0x1a,0x71,0x33,0x54,0x8d,0x5c,0xd2,0x30};
  100.     static PRUint8 rc2_cbc_known_ciphertext[] = {
  101.   0xff,0x41,0xdb,0x94,0x8a,0x4c,0x33,0xb3};
  102.     /* RC2 variables. */
  103.     PRUint8        rc2_computed_ciphertext[FIPS_RC2_ENCRYPT_LENGTH];
  104.     PRUint8        rc2_computed_plaintext[FIPS_RC2_DECRYPT_LENGTH];
  105.     RC2Context *   rc2_context;
  106.     unsigned int   rc2_bytes_encrypted;
  107.     unsigned int   rc2_bytes_decrypted;
  108.     SECStatus      rc2_status;
  109.     /******************************************************/
  110.     /* RC2-ECB Single-Round Known Answer Encryption Test: */
  111.     /******************************************************/
  112.     rc2_context = RC2_CreateContext( rc2_known_key, FIPS_RC2_KEY_LENGTH,
  113.                                      NULL, NSS_RC2,
  114.                                      FIPS_RC2_KEY_LENGTH );
  115.     if( rc2_context == NULL )
  116.         return( CKR_HOST_MEMORY );
  117.     rc2_status = RC2_Encrypt( rc2_context, rc2_computed_ciphertext,
  118.                               &rc2_bytes_encrypted, FIPS_RC2_ENCRYPT_LENGTH,
  119.                               rc2_ecb_known_plaintext,
  120.                               FIPS_RC2_DECRYPT_LENGTH );
  121.     RC2_DestroyContext( rc2_context, PR_TRUE );
  122.     if( ( rc2_status != SECSuccess ) ||
  123.         ( rc2_bytes_encrypted != FIPS_RC2_ENCRYPT_LENGTH ) ||
  124.         ( PORT_Memcmp( rc2_computed_ciphertext, rc2_ecb_known_ciphertext,
  125.                        FIPS_RC2_ENCRYPT_LENGTH ) != 0 ) )
  126.         return( CKR_DEVICE_ERROR );
  127.     /******************************************************/
  128.     /* RC2-ECB Single-Round Known Answer Decryption Test: */
  129.     /******************************************************/
  130.     rc2_context = RC2_CreateContext( rc2_known_key, FIPS_RC2_KEY_LENGTH,
  131.                                      NULL, NSS_RC2,
  132.                                      FIPS_RC2_KEY_LENGTH );
  133.     if( rc2_context == NULL )
  134.         return( CKR_HOST_MEMORY );
  135.     rc2_status = RC2_Decrypt( rc2_context, rc2_computed_plaintext,
  136.                               &rc2_bytes_decrypted, FIPS_RC2_DECRYPT_LENGTH,
  137.                               rc2_ecb_known_ciphertext,
  138.                               FIPS_RC2_ENCRYPT_LENGTH );
  139.     RC2_DestroyContext( rc2_context, PR_TRUE );
  140.     if( ( rc2_status != SECSuccess ) ||
  141.         ( rc2_bytes_decrypted != FIPS_RC2_DECRYPT_LENGTH ) ||
  142.         ( PORT_Memcmp( rc2_computed_plaintext, rc2_ecb_known_plaintext,
  143.                        FIPS_RC2_DECRYPT_LENGTH ) != 0 ) )
  144.         return( CKR_DEVICE_ERROR );
  145.     /******************************************************/
  146.     /* RC2-CBC Single-Round Known Answer Encryption Test: */
  147.     /******************************************************/
  148.     rc2_context = RC2_CreateContext( rc2_known_key, FIPS_RC2_KEY_LENGTH,
  149.                                      rc2_cbc_known_initialization_vector,
  150.                                      NSS_RC2_CBC, FIPS_RC2_KEY_LENGTH );
  151.     if( rc2_context == NULL )
  152.         return( CKR_HOST_MEMORY );
  153.     rc2_status = RC2_Encrypt( rc2_context, rc2_computed_ciphertext,
  154.                               &rc2_bytes_encrypted, FIPS_RC2_ENCRYPT_LENGTH,
  155.                               rc2_cbc_known_plaintext,
  156.                               FIPS_RC2_DECRYPT_LENGTH );
  157.     RC2_DestroyContext( rc2_context, PR_TRUE );
  158.     if( ( rc2_status != SECSuccess ) ||
  159.         ( rc2_bytes_encrypted != FIPS_RC2_ENCRYPT_LENGTH ) ||
  160.         ( PORT_Memcmp( rc2_computed_ciphertext, rc2_cbc_known_ciphertext,
  161.                        FIPS_RC2_ENCRYPT_LENGTH ) != 0 ) )
  162.         return( CKR_DEVICE_ERROR );
  163.     /******************************************************/
  164.     /* RC2-CBC Single-Round Known Answer Decryption Test: */
  165.     /******************************************************/
  166.     rc2_context = RC2_CreateContext( rc2_known_key, FIPS_RC2_KEY_LENGTH,
  167.                                      rc2_cbc_known_initialization_vector,
  168.                                      NSS_RC2_CBC, FIPS_RC2_KEY_LENGTH );
  169.     if( rc2_context == NULL )
  170.         return( CKR_HOST_MEMORY );
  171.     rc2_status = RC2_Decrypt( rc2_context, rc2_computed_plaintext,
  172.                               &rc2_bytes_decrypted, FIPS_RC2_DECRYPT_LENGTH,
  173.                               rc2_cbc_known_ciphertext,
  174.                               FIPS_RC2_ENCRYPT_LENGTH );
  175.     RC2_DestroyContext( rc2_context, PR_TRUE );
  176.     if( ( rc2_status != SECSuccess ) ||
  177.         ( rc2_bytes_decrypted != FIPS_RC2_DECRYPT_LENGTH ) ||
  178.         ( PORT_Memcmp( rc2_computed_plaintext, rc2_ecb_known_plaintext,
  179.                        FIPS_RC2_DECRYPT_LENGTH ) != 0 ) )
  180.         return( CKR_DEVICE_ERROR );
  181.     return( CKR_OK );
  182. }
  183. static CK_RV
  184. pk11_fips_RC4_PowerUpSelfTest( void )
  185. {
  186.     /* RC4 Known Key (40-bits). */
  187.     static PRUint8 rc4_known_key[] = { "RSARC" };
  188.     /* RC4 Known Plaintext (64-bits). */
  189.     static PRUint8 rc4_known_plaintext[] = { "Netscape" };
  190.     /* RC4 Known Ciphertext (64-bits). */
  191.     static PRUint8 rc4_known_ciphertext[] = {
  192. 0x29,0x33,0xc7,0x9a,0x9d,0x6c,0x09,0xdd};
  193.     /* RC4 variables. */
  194.     PRUint8        rc4_computed_ciphertext[FIPS_RC4_ENCRYPT_LENGTH];
  195.     PRUint8        rc4_computed_plaintext[FIPS_RC4_DECRYPT_LENGTH];
  196.     RC4Context *   rc4_context;
  197.     unsigned int   rc4_bytes_encrypted;
  198.     unsigned int   rc4_bytes_decrypted;
  199.     SECStatus      rc4_status;
  200.     /**************************************************/
  201.     /* RC4 Single-Round Known Answer Encryption Test: */
  202.     /**************************************************/
  203.     rc4_context = RC4_CreateContext( rc4_known_key, FIPS_RC4_KEY_LENGTH );
  204.     if( rc4_context == NULL )
  205.         return( CKR_HOST_MEMORY );
  206.     rc4_status = RC4_Encrypt( rc4_context, rc4_computed_ciphertext,
  207.                               &rc4_bytes_encrypted, FIPS_RC4_ENCRYPT_LENGTH,
  208.                               rc4_known_plaintext, FIPS_RC4_DECRYPT_LENGTH );
  209.     RC4_DestroyContext( rc4_context, PR_TRUE );
  210.     if( ( rc4_status != SECSuccess ) ||
  211.         ( rc4_bytes_encrypted != FIPS_RC4_ENCRYPT_LENGTH ) ||
  212.         ( PORT_Memcmp( rc4_computed_ciphertext, rc4_known_ciphertext,
  213.                        FIPS_RC4_ENCRYPT_LENGTH ) != 0 ) )
  214.         return( CKR_DEVICE_ERROR );
  215.     /**************************************************/
  216.     /* RC4 Single-Round Known Answer Decryption Test: */
  217.     /**************************************************/
  218.     rc4_context = RC4_CreateContext( rc4_known_key, FIPS_RC4_KEY_LENGTH );
  219.     if( rc4_context == NULL )
  220.         return( CKR_HOST_MEMORY );
  221.     rc4_status = RC4_Decrypt( rc4_context, rc4_computed_plaintext,
  222.                               &rc4_bytes_decrypted, FIPS_RC4_DECRYPT_LENGTH,
  223.                               rc4_known_ciphertext, FIPS_RC4_ENCRYPT_LENGTH );
  224.     RC4_DestroyContext( rc4_context, PR_TRUE );
  225.     if( ( rc4_status != SECSuccess ) ||
  226.         ( rc4_bytes_decrypted != FIPS_RC4_DECRYPT_LENGTH ) ||
  227.         ( PORT_Memcmp( rc4_computed_plaintext, rc4_known_plaintext,
  228.                        FIPS_RC4_DECRYPT_LENGTH ) != 0 ) )
  229.         return( CKR_DEVICE_ERROR );
  230.     return( CKR_OK );
  231. }
  232. static CK_RV
  233. pk11_fips_DES_PowerUpSelfTest( void )
  234. {
  235.     /* DES Known Key (56-bits). */
  236.     static PRUint8 des_known_key[] = { "ANSI DES" };
  237.     /* DES-CBC Known Initialization Vector (64-bits). */
  238.     static PRUint8 des_cbc_known_initialization_vector[] = { "Security" };
  239.     /* DES Known Plaintext (64-bits). */
  240.     static PRUint8 des_ecb_known_plaintext[] = { "Netscape" };
  241.     static PRUint8 des_cbc_known_plaintext[] = { "Netscape" };
  242.     /* DES Known Ciphertext (64-bits). */
  243.     static PRUint8 des_ecb_known_ciphertext[] = {
  244.        0x26,0x14,0xe9,0xc3,0x28,0x80,0x50,0xb0};
  245.     static PRUint8 des_cbc_known_ciphertext[]  = {
  246.        0x5e,0x95,0x94,0x5d,0x76,0xa2,0xd3,0x7d};
  247.     /* DES variables. */
  248.     PRUint8        des_computed_ciphertext[FIPS_DES_ENCRYPT_LENGTH];
  249.     PRUint8        des_computed_plaintext[FIPS_DES_DECRYPT_LENGTH];
  250.     DESContext *   des_context;
  251.     unsigned int   des_bytes_encrypted;
  252.     unsigned int   des_bytes_decrypted;
  253.     SECStatus      des_status;
  254.     /******************************************************/
  255.     /* DES-ECB Single-Round Known Answer Encryption Test: */
  256.     /******************************************************/
  257.     des_context = DES_CreateContext( des_known_key, NULL, NSS_DES, PR_TRUE );
  258.     if( des_context == NULL )
  259.         return( CKR_HOST_MEMORY );
  260.     des_status = DES_Encrypt( des_context, des_computed_ciphertext,
  261.                               &des_bytes_encrypted, FIPS_DES_ENCRYPT_LENGTH,
  262.                               des_ecb_known_plaintext,
  263.                               FIPS_DES_DECRYPT_LENGTH );
  264.     DES_DestroyContext( des_context, PR_TRUE );
  265.     if( ( des_status != SECSuccess ) ||
  266.         ( des_bytes_encrypted != FIPS_DES_ENCRYPT_LENGTH ) ||
  267.         ( PORT_Memcmp( des_computed_ciphertext, des_ecb_known_ciphertext,
  268.                        FIPS_DES_ENCRYPT_LENGTH ) != 0 ) )
  269.         return( CKR_DEVICE_ERROR );
  270.     /******************************************************/
  271.     /* DES-ECB Single-Round Known Answer Decryption Test: */
  272.     /******************************************************/
  273.     des_context = DES_CreateContext( des_known_key, NULL, NSS_DES, PR_FALSE );
  274.     if( des_context == NULL )
  275.         return( CKR_HOST_MEMORY );
  276.     des_status = DES_Decrypt( des_context, des_computed_plaintext,
  277.                               &des_bytes_decrypted, FIPS_DES_DECRYPT_LENGTH,
  278.                               des_ecb_known_ciphertext,
  279.                               FIPS_DES_ENCRYPT_LENGTH );
  280.     DES_DestroyContext( des_context, PR_TRUE );
  281.     if( ( des_status != SECSuccess ) ||
  282.         ( des_bytes_decrypted != FIPS_DES_DECRYPT_LENGTH ) ||
  283.         ( PORT_Memcmp( des_computed_plaintext, des_ecb_known_plaintext,
  284.                        FIPS_DES_DECRYPT_LENGTH ) != 0 ) )
  285.         return( CKR_DEVICE_ERROR );
  286.     /******************************************************/
  287.     /* DES-CBC Single-Round Known Answer Encryption Test. */
  288.     /******************************************************/
  289.     des_context = DES_CreateContext( des_known_key,
  290.                                      des_cbc_known_initialization_vector,
  291.                                      NSS_DES_CBC, PR_TRUE );
  292.     if( des_context == NULL )
  293.         return( CKR_HOST_MEMORY );
  294.     des_status = DES_Encrypt( des_context, des_computed_ciphertext,
  295.                               &des_bytes_encrypted, FIPS_DES_ENCRYPT_LENGTH,
  296.                               des_cbc_known_plaintext,
  297.                               FIPS_DES_DECRYPT_LENGTH );
  298.     DES_DestroyContext( des_context, PR_TRUE );
  299.     if( ( des_status != SECSuccess ) ||
  300.         ( des_bytes_encrypted != FIPS_DES_ENCRYPT_LENGTH ) ||
  301.         ( PORT_Memcmp( des_computed_ciphertext, des_cbc_known_ciphertext,
  302.                        FIPS_DES_ENCRYPT_LENGTH ) != 0 ) )
  303.         return( CKR_DEVICE_ERROR );
  304.     /******************************************************/
  305.     /* DES-CBC Single-Round Known Answer Decryption Test. */
  306.     /******************************************************/
  307.     des_context = DES_CreateContext( des_known_key,
  308.                                      des_cbc_known_initialization_vector,
  309.                                      NSS_DES_CBC, PR_FALSE );
  310.     if( des_context == NULL )
  311.         return( CKR_HOST_MEMORY );
  312.     des_status = DES_Decrypt( des_context, des_computed_plaintext,
  313.                               &des_bytes_decrypted, FIPS_DES_DECRYPT_LENGTH,
  314.                               des_cbc_known_ciphertext,
  315.                               FIPS_DES_ENCRYPT_LENGTH );
  316.     DES_DestroyContext( des_context, PR_TRUE );
  317.     if( ( des_status != SECSuccess ) ||
  318.         ( des_bytes_decrypted != FIPS_DES_DECRYPT_LENGTH ) ||
  319.         ( PORT_Memcmp( des_computed_plaintext, des_cbc_known_plaintext,
  320.                        FIPS_DES_DECRYPT_LENGTH ) != 0 ) )
  321.         return( CKR_DEVICE_ERROR );
  322.     return( CKR_OK );
  323. }
  324. static CK_RV
  325. pk11_fips_DES3_PowerUpSelfTest( void )
  326. {
  327.     /* DES3 Known Key (56-bits). */
  328.     static PRUint8 des3_known_key[] = { "ANSI Triple-DES Key Data" };
  329.     /* DES3-CBC Known Initialization Vector (64-bits). */
  330.     static PRUint8 des3_cbc_known_initialization_vector[] = { "Security" };
  331.     /* DES3 Known Plaintext (64-bits). */
  332.     static PRUint8 des3_ecb_known_plaintext[] = { "Netscape" };
  333.     static PRUint8 des3_cbc_known_plaintext[] = { "Netscape" };
  334.     /* DES3 Known Ciphertext (64-bits). */
  335.     static PRUint8 des3_ecb_known_ciphertext[] = {
  336.    0x55,0x8e,0xad,0x3c,0xee,0x49,0x69,0xbe};
  337.     static PRUint8 des3_cbc_known_ciphertext[] = {
  338.    0x43,0xdc,0x6a,0xc1,0xaf,0xa6,0x32,0xf5};
  339.     /* DES3 variables. */
  340.     PRUint8        des3_computed_ciphertext[FIPS_DES3_ENCRYPT_LENGTH];
  341.     PRUint8        des3_computed_plaintext[FIPS_DES3_DECRYPT_LENGTH];
  342.     DESContext *   des3_context;
  343.     unsigned int   des3_bytes_encrypted;
  344.     unsigned int   des3_bytes_decrypted;
  345.     SECStatus      des3_status;
  346.     /*******************************************************/
  347.     /* DES3-ECB Single-Round Known Answer Encryption Test. */
  348.     /*******************************************************/
  349.     des3_context = DES_CreateContext( des3_known_key, NULL,
  350.                                      NSS_DES_EDE3, PR_TRUE );
  351.     if( des3_context == NULL )
  352.         return( CKR_HOST_MEMORY );
  353.     des3_status = DES_Encrypt( des3_context, des3_computed_ciphertext,
  354.                                &des3_bytes_encrypted, FIPS_DES3_ENCRYPT_LENGTH,
  355.                                des3_ecb_known_plaintext,
  356.                                FIPS_DES3_DECRYPT_LENGTH );
  357.     DES_DestroyContext( des3_context, PR_TRUE );
  358.     if( ( des3_status != SECSuccess ) ||
  359.         ( des3_bytes_encrypted != FIPS_DES3_ENCRYPT_LENGTH ) ||
  360.         ( PORT_Memcmp( des3_computed_ciphertext, des3_ecb_known_ciphertext,
  361.                        FIPS_DES3_ENCRYPT_LENGTH ) != 0 ) )
  362.         return( CKR_DEVICE_ERROR );
  363.     /*******************************************************/
  364.     /* DES3-ECB Single-Round Known Answer Decryption Test. */
  365.     /*******************************************************/
  366.     des3_context = DES_CreateContext( des3_known_key, NULL,
  367.                                      NSS_DES_EDE3, PR_FALSE );
  368.     if( des3_context == NULL )
  369.         return( CKR_HOST_MEMORY );
  370.     des3_status = DES_Decrypt( des3_context, des3_computed_plaintext,
  371.                                &des3_bytes_decrypted, FIPS_DES3_DECRYPT_LENGTH,
  372.                                des3_ecb_known_ciphertext,
  373.                                FIPS_DES3_ENCRYPT_LENGTH );
  374.     DES_DestroyContext( des3_context, PR_TRUE );
  375.     if( ( des3_status != SECSuccess ) ||
  376.         ( des3_bytes_decrypted != FIPS_DES3_DECRYPT_LENGTH ) ||
  377.         ( PORT_Memcmp( des3_computed_plaintext, des3_ecb_known_plaintext,
  378.                        FIPS_DES3_DECRYPT_LENGTH ) != 0 ) )
  379.         return( CKR_DEVICE_ERROR );
  380.     /*******************************************************/
  381.     /* DES3-CBC Single-Round Known Answer Encryption Test. */
  382.     /*******************************************************/
  383.     des3_context = DES_CreateContext( des3_known_key,
  384.                                       des3_cbc_known_initialization_vector,
  385.                                       NSS_DES_EDE3_CBC, PR_TRUE );
  386.     if( des3_context == NULL )
  387.         return( CKR_HOST_MEMORY );
  388.     des3_status = DES_Encrypt( des3_context, des3_computed_ciphertext,
  389.                                &des3_bytes_encrypted, FIPS_DES3_ENCRYPT_LENGTH,
  390.                                des3_cbc_known_plaintext,
  391.                                FIPS_DES3_DECRYPT_LENGTH );
  392.     DES_DestroyContext( des3_context, PR_TRUE );
  393.     if( ( des3_status != SECSuccess ) ||
  394.         ( des3_bytes_encrypted != FIPS_DES3_ENCRYPT_LENGTH ) ||
  395.         ( PORT_Memcmp( des3_computed_ciphertext, des3_cbc_known_ciphertext,
  396.                        FIPS_DES3_ENCRYPT_LENGTH ) != 0 ) )
  397.         return( CKR_DEVICE_ERROR );
  398.     /*******************************************************/
  399.     /* DES3-CBC Single-Round Known Answer Decryption Test. */
  400.     /*******************************************************/
  401.     des3_context = DES_CreateContext( des3_known_key,
  402.                                       des3_cbc_known_initialization_vector,
  403.                                       NSS_DES_EDE3_CBC, PR_FALSE );
  404.     if( des3_context == NULL )
  405.         return( CKR_HOST_MEMORY );
  406.     des3_status = DES_Decrypt( des3_context, des3_computed_plaintext,
  407.                                &des3_bytes_decrypted, FIPS_DES3_DECRYPT_LENGTH,
  408.                                des3_cbc_known_ciphertext,
  409.                                FIPS_DES3_ENCRYPT_LENGTH );
  410.     DES_DestroyContext( des3_context, PR_TRUE );
  411.     if( ( des3_status != SECSuccess ) ||
  412.         ( des3_bytes_decrypted != FIPS_DES3_DECRYPT_LENGTH ) ||
  413.         ( PORT_Memcmp( des3_computed_plaintext, des3_cbc_known_plaintext,
  414.                        FIPS_DES3_DECRYPT_LENGTH ) != 0 ) )
  415.         return( CKR_DEVICE_ERROR );
  416.     return( CKR_OK );
  417. }
  418. static CK_RV
  419. pk11_fips_MD2_PowerUpSelfTest( void )
  420. {
  421.     /* MD2 Known Hash Message (512-bits). */
  422.     static PRUint8 md2_known_hash_message[] = {
  423. "The test message for the MD2, MD5, and SHA-1 hashing algorithms." };
  424.     /* MD2 Known Digest Message (128-bits). */
  425.     static PRUint8 md2_known_digest[]  = {
  426.    0x41,0x5a,0x12,0xb2,0x3f,0x28,0x97,0x17,
  427.    0x0c,0x71,0x4e,0xcc,0x40,0xc8,0x1d,0x1b};
  428.     /* MD2 variables. */
  429.     MD2Context * md2_context;
  430.     unsigned int md2_bytes_hashed;
  431.     PRUint8      md2_computed_digest[MD2_LENGTH];
  432.     /***********************************************/
  433.     /* MD2 Single-Round Known Answer Hashing Test. */
  434.     /***********************************************/
  435.     md2_context = MD2_NewContext();
  436.     if( md2_context == NULL )
  437.         return( CKR_HOST_MEMORY );
  438.     MD2_Begin( md2_context );
  439.     MD2_Update( md2_context, md2_known_hash_message,
  440.                 FIPS_MD2_HASH_MESSAGE_LENGTH );
  441.     MD2_End( md2_context, md2_computed_digest, &md2_bytes_hashed, MD2_LENGTH );
  442.     MD2_DestroyContext( md2_context , PR_TRUE );
  443.     if( ( md2_bytes_hashed != MD2_LENGTH ) ||
  444.         ( PORT_Memcmp( md2_computed_digest, md2_known_digest,
  445.                        MD2_LENGTH ) != 0 ) )
  446.         return( CKR_DEVICE_ERROR );
  447.     return( CKR_OK );
  448. }
  449. static CK_RV
  450. pk11_fips_MD5_PowerUpSelfTest( void )
  451. {
  452.     /* MD5 Known Hash Message (512-bits). */
  453.     static PRUint8 md5_known_hash_message[] = {
  454. "The test message for the MD2, MD5, and SHA-1 hashing algorithms." };
  455.     /* MD5 Known Digest Message (128-bits). */
  456.     static PRUint8 md5_known_digest[]  = {
  457.    0x25,0xc8,0xc0,0x10,0xc5,0x6e,0x68,0x28,
  458.    0x28,0xa4,0xa5,0xd2,0x98,0x9a,0xea,0x2d};
  459.     /* MD5 variables. */
  460.     PRUint8        md5_computed_digest[MD5_LENGTH];
  461.     SECStatus      md5_status;
  462.     /***********************************************/
  463.     /* MD5 Single-Round Known Answer Hashing Test. */
  464.     /***********************************************/
  465.     md5_status = MD5_HashBuf( md5_computed_digest, md5_known_hash_message,
  466.                               FIPS_MD5_HASH_MESSAGE_LENGTH );
  467.     if( ( md5_status != SECSuccess ) ||
  468.         ( PORT_Memcmp( md5_computed_digest, md5_known_digest,
  469.                        MD5_LENGTH ) != 0 ) )
  470.         return( CKR_DEVICE_ERROR );
  471.     return( CKR_OK );
  472. }
  473. static CK_RV
  474. pk11_fips_SHA1_PowerUpSelfTest( void )
  475. {
  476.     /* SHA-1 Known Hash Message (512-bits). */
  477.     static PRUint8 sha1_known_hash_message[] = {
  478.  "The test message for the MD2, MD5, and SHA-1 hashing algorithms." };
  479.     /* SHA-1 Known Digest Message (160-bits). */
  480.     static PRUint8 sha1_known_digest[] = {
  481.        0x0a,0x6d,0x07,0xba,0x1e,0xbd,0x8a,0x1b,
  482.        0x72,0xf6,0xc7,0x22,0xf1,0x27,0x9f,0xf0,
  483.        0xe0,0x68,0x47,0x7a};
  484.     /* SHA-1 variables. */
  485.     PRUint8        sha1_computed_digest[SHA1_LENGTH];
  486.     SECStatus      sha1_status;
  487.     /*************************************************/
  488.     /* SHA-1 Single-Round Known Answer Hashing Test. */
  489.     /*************************************************/
  490.     sha1_status = SHA1_HashBuf( sha1_computed_digest, sha1_known_hash_message,
  491.                                 FIPS_SHA1_HASH_MESSAGE_LENGTH );
  492.     if( ( sha1_status != SECSuccess ) ||
  493.         ( PORT_Memcmp( sha1_computed_digest, sha1_known_digest,
  494.                        SHA1_LENGTH ) != 0 ) )
  495.         return( CKR_DEVICE_ERROR );
  496.     return( CKR_OK );
  497. }
  498. static CK_RV
  499. pk11_fips_RSA_PowerUpSelfTest( void )
  500. {
  501.     /* RSA Known Modulus used in both Public/Private Key Values (520-bits). */
  502.     static PRUint8 rsa_modulus[FIPS_RSA_MODULUS_LENGTH] = {
  503.                                  0x00,0xa1,0xe9,0x5e,0x66,0x88,0xe2,0xf2,
  504.                                  0x2b,0xe7,0x70,0x36,0x33,0xbc,0xeb,0x55,
  505.                                  0x55,0xf1,0x60,0x18,0x3c,0xfb,0xd2,0x79,
  506.                                  0xf6,0xc4,0xb8,0x09,0xe3,0x12,0xf6,0x63,
  507.                                  0x6d,0xc7,0x8e,0x19,0xc0,0x0e,0x10,0x78,
  508.                                  0xc1,0xfe,0x2a,0x41,0x74,0x2d,0xf7,0xc4,
  509.                                  0x69,0xa7,0x3c,0xbc,0x8a,0xc8,0x31,0x2b,
  510.                                  0x4f,0x60,0xf0,0xf1,0xec,0x5a,0x29,0xec,
  511.                                  0x6b};
  512.     /* RSA Known Public Key Values (8-bits). */
  513.     static PRUint8 rsa_public_exponent[] = { 0x03 };
  514.     /* RSA Known Private Key Values (version                 is   8-bits), */
  515.     /*                              (private exponent        is 512-bits), */
  516.     /*                              (private prime0          is 264-bits), */
  517.     /*                              (private prime1          is 264-bits), */
  518.     /*                              (private prime exponent0 is 264-bits), */
  519.     /*                              (private prime exponent1 is 264-bits), */
  520.     /*                          and (private coefficient     is 256-bits). */
  521.     static PRUint8 rsa_version[] = { 0x00 };
  522.     static PRUint8 rsa_private_exponent[FIPS_RSA_PRIVATE_EXPONENT_LENGTH] = {
  523.   0x6b,0xf0,0xe9,0x99,0xb0,0x97,0x4c,0x1d,
  524.   0x44,0xf5,0x79,0x77,0xd3,0x47,0x8e,0x39,
  525.   0x4b,0x95,0x65,0x7d,0xfd,0x36,0xfb,0xf9,
  526.   0xd8,0x7a,0xb1,0x42,0x0c,0xa4,0x42,0x48,
  527.   0x20,0x1c,0x6b,0x7d,0x5d,0xa3,0x58,0xd6,
  528.   0x95,0xd6,0x41,0xe3,0xd6,0x73,0xad,0xdb,
  529.   0x3b,0x89,0x00,0x8a,0xcd,0x1d,0xb9,0x06,
  530.   0xac,0xac,0x0e,0x02,0x72,0x1c,0xf8,0xab };
  531.     static PRUint8 rsa_prime0[FIPS_RSA_PRIME0_LENGTH]   = {
  532.       0x00,0xd2,0x2c,0x9d,0xef,0x7c,0x8f,0x58,
  533.       0x93,0x19,0xa1,0x77,0x0e,0x38,0x3e,0x85,
  534.       0xb4,0xaf,0xcc,0x99,0xa5,0x43,0xbf,0x97,
  535.       0xdc,0x46,0xb8,0x3f,0x6e,0x85,0x18,0x00,
  536.       0x81};
  537.     static PRUint8 rsa_prime1[FIPS_RSA_PRIME1_LENGTH]   = {
  538.       0x00,0xc5,0x36,0xda,0x94,0x85,0x0c,0x1a,
  539.       0xed,0x03,0xc7,0x67,0x90,0x34,0x0b,0xb9,
  540.       0xec,0x1e,0x22,0xa2,0x15,0x50,0xc4,0xfd,
  541.       0xe9,0x17,0x36,0x9d,0x7a,0x29,0xe6,0x76,
  542.       0xeb};
  543.     static PRUint8 rsa_exponent0[FIPS_RSA_EXPONENT0_LENGTH] = {
  544.  0x00,0x8c,0x1d,0xbe,0x9f,0xa8,
  545.  0x5f,0x90,0x62,0x11,0x16,0x4f,
  546.  0x5e,0xd0,0x29,0xae,0x78,0x75,
  547.  0x33,0x11,0x18,0xd7,0xd5,0x0f,
  548.  0xe8,0x2f,0x25,0x7f,0x9f,0x03,
  549.  0x65,0x55,0xab};
  550.     static PRUint8 rsa_exponent1[FIPS_RSA_EXPONENT1_LENGTH] = {
  551.  0x00,0x83,0x79,0xe7,0x0d,0xae,
  552.  0x08,0x11,0xf3,0x57,0xda,0x45,
  553.  0x0a,0xcd,0x5d,0x26,0x9d,0x69,
  554.  0x6c,0x6c,0x0e,0x35,0xd8,0xa9,
  555.  0x46,0x0f,0x79,0xbe,0x51,0x71,
  556.  0x44,0x4f,0x47};
  557.     static PRUint8 rsa_coefficient[FIPS_RSA_COEFFICIENT_LENGTH] = {
  558.  0x54,0x8d,0xb8,0xdc,0x8b,0xde,0xbb,
  559.  0x08,0xc9,0x67,0xb7,0xa9,0x5f,0xa5,
  560.  0xc4,0x5e,0x67,0xaa,0xfe,0x1a,0x08,
  561.  0xeb,0x48,0x43,0xcb,0xb0,0xb9,0x38,
  562.  0x3a,0x31,0x39,0xde};
  563.     /* RSA Known Plaintext (512-bits). */
  564.     static PRUint8 rsa_known_plaintext[] = {
  565.                                          "Known plaintext utilized for RSA"
  566.                                          " Encryption and Decryption test." };
  567.     /* RSA Known Ciphertext (512-bits). */
  568.     static PRUint8 rsa_known_ciphertext[] = {
  569.       0x12,0x80,0x3a,0x53,0xee,0x93,0x81,0xa5,
  570.       0xf7,0x40,0xc5,0xb1,0xef,0xd9,0x27,0xaf,
  571.       0xef,0x4b,0x87,0x44,0x00,0xd0,0xda,0xcf,
  572.       0x10,0x57,0x4c,0xd5,0xc3,0xed,0x84,0xdc,
  573.       0x74,0x03,0x19,0x69,0x2c,0xd6,0x54,0x3e,
  574.       0xd2,0xe3,0x90,0xb6,0x67,0x91,0x2f,0x1f,
  575.       0x54,0x13,0x99,0x00,0x0b,0xfd,0x52,0x7f,
  576.       0xd8,0xc6,0xdb,0x8a,0xfe,0x06,0xf3,0xb1};
  577.     /* RSA Known Message (128-bits). */
  578.     static PRUint8 rsa_known_message[]  = { "Netscape Forever" };
  579.     /* RSA Known Signed Hash (512-bits). */
  580.     static PRUint8 rsa_known_signature[] = {
  581.      0x27,0x23,0xa6,0x71,0x57,0xc8,0x70,0x5f,
  582.      0x70,0x0e,0x06,0x7b,0x96,0x6a,0xaa,0x41,
  583.      0x6e,0xab,0x67,0x4b,0x5f,0x76,0xc4,0x53,
  584.      0x23,0xd7,0x57,0x7a,0x3a,0xbc,0x4c,0x27,
  585.      0x65,0xca,0xde,0x9f,0xd3,0x1d,0xa4,0x5a,
  586.      0xf9,0x8f,0xb2,0x05,0xa3,0x86,0xf9,0x66,
  587.      0x55,0x4c,0x68,0x50,0x66,0xa4,0xe9,0x17,
  588.      0x45,0x11,0xb8,0x1a,0xfc,0xbc,0x79,0x3b};
  589.     static RSAPublicKey    bl_public_key = { NULL, 
  590.       { FIPS_RSA_TYPE, rsa_modulus,         FIPS_RSA_MODULUS_LENGTH },
  591.       { FIPS_RSA_TYPE, rsa_public_exponent, FIPS_RSA_PUBLIC_EXPONENT_LENGTH }
  592.     };
  593.     static RSAPrivateKey   bl_private_key = { NULL,
  594.       { FIPS_RSA_TYPE, rsa_version,          FIPS_RSA_PRIVATE_VERSION_LENGTH },
  595.       { FIPS_RSA_TYPE, rsa_modulus,          FIPS_RSA_MODULUS_LENGTH },
  596.       { FIPS_RSA_TYPE, rsa_public_exponent,  FIPS_RSA_PUBLIC_EXPONENT_LENGTH },
  597.       { FIPS_RSA_TYPE, rsa_private_exponent, FIPS_RSA_PRIVATE_EXPONENT_LENGTH },
  598.       { FIPS_RSA_TYPE, rsa_prime0,           FIPS_RSA_PRIME0_LENGTH },
  599.       { FIPS_RSA_TYPE, rsa_prime1,           FIPS_RSA_PRIME1_LENGTH },
  600.       { FIPS_RSA_TYPE, rsa_exponent0,        FIPS_RSA_EXPONENT0_LENGTH },
  601.       { FIPS_RSA_TYPE, rsa_exponent1,        FIPS_RSA_EXPONENT1_LENGTH },
  602.       { FIPS_RSA_TYPE, rsa_coefficient,      FIPS_RSA_COEFFICIENT_LENGTH }
  603.     };
  604.     /* RSA variables. */
  605. #ifdef CREATE_TEMP_ARENAS
  606.     PLArenaPool *         rsa_public_arena;
  607.     PLArenaPool *         rsa_private_arena;
  608. #endif
  609.     SECKEYLowPublicKey *  rsa_public_key;
  610.     SECKEYLowPrivateKey * rsa_private_key;
  611.     unsigned int          rsa_bytes_signed;
  612.     SECStatus             rsa_status;
  613.     SECKEYLowPublicKey    low_public_key   = { NULL, rsaKey, };
  614.     SECKEYLowPrivateKey   low_private_key  = { NULL, rsaKey, };
  615.     PRUint8               rsa_computed_ciphertext[FIPS_RSA_ENCRYPT_LENGTH];
  616.     PRUint8               rsa_computed_plaintext[FIPS_RSA_DECRYPT_LENGTH];
  617.     PRUint8               rsa_computed_signature[FIPS_RSA_SIGNATURE_LENGTH];
  618.     /****************************************/
  619.     /* Compose RSA Public/Private Key Pair. */
  620.     /****************************************/
  621.     low_public_key.u.rsa  = bl_public_key;
  622.     low_private_key.u.rsa = bl_private_key;
  623.     rsa_public_key  = &low_public_key;
  624.     rsa_private_key = &low_private_key;
  625. #ifdef CREATE_TEMP_ARENAS
  626.     /* Create some space for the RSA public key. */
  627.     rsa_public_arena = PORT_NewArena( NSS_SOFTOKEN_DEFAULT_CHUNKSIZE );
  628.     if( rsa_public_arena == NULL ) {
  629.         PORT_SetError( SEC_ERROR_NO_MEMORY );
  630.         return( CKR_HOST_MEMORY );
  631.     }
  632.     /* Create some space for the RSA private key. */
  633.     rsa_private_arena = PORT_NewArena( NSS_SOFTOKEN_DEFAULT_CHUNKSIZE );
  634.     if( rsa_private_arena == NULL ) {
  635.         PORT_FreeArena( rsa_public_arena, PR_TRUE );
  636.         PORT_SetError( SEC_ERROR_NO_MEMORY );
  637.         return( CKR_HOST_MEMORY );
  638.     }
  639.     rsa_public_key->arena = rsa_public_arena;
  640.     rsa_private_key->arena = rsa_private_arena;
  641. #endif
  642.     /**************************************************/
  643.     /* RSA Single-Round Known Answer Encryption Test. */
  644.     /**************************************************/
  645.     /* Perform RSA Public Key Encryption. */
  646.     rsa_status = RSA_PublicKeyOp(&rsa_public_key->u.rsa, 
  647.                                  rsa_computed_ciphertext, rsa_known_plaintext);
  648.     if( ( rsa_status != SECSuccess ) ||
  649.         ( PORT_Memcmp( rsa_computed_ciphertext, rsa_known_ciphertext,
  650.                        FIPS_RSA_ENCRYPT_LENGTH ) != 0 ) )
  651.         goto rsa_loser;
  652.     /**************************************************/
  653.     /* RSA Single-Round Known Answer Decryption Test. */
  654.     /**************************************************/
  655.     /* Perform RSA Private Key Decryption. */
  656.     rsa_status = RSA_PrivateKeyOp(&rsa_private_key->u.rsa, 
  657.                                   rsa_computed_plaintext, rsa_known_ciphertext);
  658.     if( ( rsa_status != SECSuccess ) ||
  659.         ( PORT_Memcmp( rsa_computed_plaintext, rsa_known_plaintext,
  660.                        FIPS_RSA_DECRYPT_LENGTH ) != 0 ) )
  661.         goto rsa_loser;
  662.     /*************************************************/
  663.     /* RSA Single-Round Known Answer Signature Test. */
  664.     /*************************************************/
  665.     /* Perform RSA signature with the RSA private key. */
  666.     rsa_status = RSA_Sign( rsa_private_key, rsa_computed_signature,
  667.                            &rsa_bytes_signed,
  668.                            FIPS_RSA_SIGNATURE_LENGTH, rsa_known_message,
  669.                            FIPS_RSA_MESSAGE_LENGTH );
  670.     if( ( rsa_status != SECSuccess ) ||
  671.         ( rsa_bytes_signed != FIPS_RSA_SIGNATURE_LENGTH ) ||
  672.         ( PORT_Memcmp( rsa_computed_signature, rsa_known_signature,
  673.                        FIPS_RSA_SIGNATURE_LENGTH ) != 0 ) )
  674.         goto rsa_loser;
  675.     /****************************************************/
  676.     /* RSA Single-Round Known Answer Verification Test. */
  677.     /****************************************************/
  678.     /* Perform RSA verification with the RSA public key. */
  679.     rsa_status = RSA_CheckSign( rsa_public_key,
  680.                                 rsa_computed_signature,
  681.                                 FIPS_RSA_SIGNATURE_LENGTH,
  682.                                 rsa_known_message,
  683.                                 FIPS_RSA_MESSAGE_LENGTH );
  684.     if( rsa_status != SECSuccess )
  685.         goto rsa_loser;
  686.     /* Dispose of all RSA key material. */
  687.     SECKEY_LowDestroyPublicKey( rsa_public_key );
  688.     SECKEY_LowDestroyPrivateKey( rsa_private_key );
  689.     return( CKR_OK );
  690. rsa_loser:
  691.     SECKEY_LowDestroyPublicKey( rsa_public_key );
  692.     SECKEY_LowDestroyPrivateKey( rsa_private_key );
  693.     return( CKR_DEVICE_ERROR );
  694. }
  695. static CK_RV
  696. pk11_fips_DSA_PowerUpSelfTest( void )
  697. {
  698.     /* DSA Known P (512-bits), Q (160-bits), and G (512-bits) Values. */
  699.     static PRUint8 dsa_P[] = {
  700.                            0x8d,0xf2,0xa4,0x94,0x49,0x22,0x76,0xaa,
  701.                            0x3d,0x25,0x75,0x9b,0xb0,0x68,0x69,0xcb,
  702.                            0xea,0xc0,0xd8,0x3a,0xfb,0x8d,0x0c,0xf7,
  703.                            0xcb,0xb8,0x32,0x4f,0x0d,0x78,0x82,0xe5,
  704.                            0xd0,0x76,0x2f,0xc5,0xb7,0x21,0x0e,0xaf,
  705.                            0xc2,0xe9,0xad,0xac,0x32,0xab,0x7a,0xac,
  706.                            0x49,0x69,0x3d,0xfb,0xf8,0x37,0x24,0xc2,
  707.                            0xec,0x07,0x36,0xee,0x31,0xc8,0x02,0x91};
  708.     static PRUint8 dsa_Q[] = {
  709.                            0xc7,0x73,0x21,0x8c,0x73,0x7e,0xc8,0xee,
  710.                            0x99,0x3b,0x4f,0x2d,0xed,0x30,0xf4,0x8e,
  711.                            0xda,0xce,0x91,0x5f};
  712.     static PRUint8 dsa_G[] = {
  713.                            0x62,0x6d,0x02,0x78,0x39,0xea,0x0a,0x13,
  714.                            0x41,0x31,0x63,0xa5,0x5b,0x4c,0xb5,0x00,
  715.                            0x29,0x9d,0x55,0x22,0x95,0x6c,0xef,0xcb,
  716.                            0x3b,0xff,0x10,0xf3,0x99,0xce,0x2c,0x2e,
  717.                            0x71,0xcb,0x9d,0xe5,0xfa,0x24,0xba,0xbf,
  718.                            0x58,0xe5,0xb7,0x95,0x21,0x92,0x5c,0x9c,
  719.                            0xc4,0x2e,0x9f,0x6f,0x46,0x4b,0x08,0x8c,
  720.                            0xc5,0x72,0xaf,0x53,0xe6,0xd7,0x88,0x02};
  721.     /* DSA Known Random Values (known random key block       is 160-bits)  */
  722.     /*                     and (known random signature block is 160-bits). */
  723.     static PRUint8 dsa_known_random_key_block[] = {
  724.                                                       "Mozilla Rules World!"};
  725.     static PRUint8 dsa_known_random_signature_block[] = {
  726.                                                       "Random DSA Signature"};
  727.     /* DSA Known Digest (160-bits) */
  728.     static PRUint8 dsa_known_digest[] = { "DSA Signature Digest" };
  729.     /* DSA Known Signature (320-bits). */
  730.     static PRUint8 dsa_known_signature[] = {
  731.      0x39,0x0d,0x84,0xb1,0xf7,0x52,0x89,0xba,
  732.      0xec,0x1e,0xa8,0xe2,0x00,0x8e,0x37,0x8f,
  733.      0xc2,0xf5,0xf8,0x70,0x11,0xa8,0xc7,0x02,
  734.      0x0e,0x75,0xcf,0x6b,0x54,0x4a,0x52,0xe8,
  735.      0xd8,0x6d,0x4a,0xe8,0xee,0x56,0x8e,0x59};
  736.     /* DSA variables. */
  737.     DSAPrivateKey *        dsa_private_key;
  738.     SECStatus              dsa_status;
  739.     SECItem                dsa_signature_item;
  740.     SECItem                dsa_digest_item;
  741.     DSAPublicKey           dsa_public_key;
  742.     PRUint8                dsa_computed_signature[FIPS_DSA_SIGNATURE_LENGTH];
  743.     static PQGParams       dsa_pqg = { NULL,
  744.     { FIPS_DSA_TYPE, dsa_P, FIPS_DSA_PRIME_LENGTH },
  745.     { FIPS_DSA_TYPE, dsa_Q, FIPS_DSA_SUBPRIME_LENGTH },
  746.     { FIPS_DSA_TYPE, dsa_G, FIPS_DSA_BASE_LENGTH }};
  747.     /*******************************************/
  748.     /* Generate a DSA public/private key pair. */
  749.     /*******************************************/
  750.     /* Generate a DSA public/private key pair. */
  751.     dsa_status = DSA_NewKeyFromSeed(&dsa_pqg, dsa_known_random_key_block,
  752.                                     &dsa_private_key);
  753.     if( dsa_status != SECSuccess )
  754. return( CKR_HOST_MEMORY );
  755.     /* construct public key from private key. */
  756.     dsa_public_key.params      = dsa_private_key->params;
  757.     dsa_public_key.publicValue = dsa_private_key->publicValue;
  758.     /*************************************************/
  759.     /* DSA Single-Round Known Answer Signature Test. */
  760.     /*************************************************/
  761.     dsa_signature_item.data = dsa_computed_signature;
  762.     dsa_signature_item.len  = sizeof dsa_computed_signature;
  763.     dsa_digest_item.data    = dsa_known_digest;
  764.     dsa_digest_item.len     = SHA1_LENGTH;
  765.     /* Perform DSA signature process. */
  766.     dsa_status = DSA_SignDigestWithSeed( dsa_private_key, 
  767.                                          &dsa_signature_item,
  768.  &dsa_digest_item,
  769.  dsa_known_random_signature_block );
  770.     if( ( dsa_status != SECSuccess ) ||
  771.         ( dsa_signature_item.len != FIPS_DSA_SIGNATURE_LENGTH ) ||
  772.         ( PORT_Memcmp( dsa_computed_signature, dsa_known_signature,
  773.                        FIPS_DSA_SIGNATURE_LENGTH ) != 0 ) ) {
  774.         dsa_status = SECFailure;
  775.     } else {
  776.     /****************************************************/
  777.     /* DSA Single-Round Known Answer Verification Test. */
  778.     /****************************************************/
  779.     /* Perform DSA verification process. */
  780.     dsa_status = DSA_VerifyDigest( &dsa_public_key, 
  781.                                    &dsa_signature_item,
  782.    &dsa_digest_item);
  783.     }
  784.     PORT_FreeArena(dsa_private_key->params.arena, PR_TRUE);
  785.     /* Don't free public key, it uses same arena as private key */
  786.     /* Verify DSA signature. */
  787.     if( dsa_status != SECSuccess )
  788.         return( CKR_DEVICE_ERROR );
  789.     return( CKR_OK );
  790. }
  791. CK_RV
  792. pk11_fipsPowerUpSelfTest( void )
  793. {
  794.     CK_RV rv;
  795.     /* RC2 Power-Up SelfTest(s). */
  796.     rv = pk11_fips_RC2_PowerUpSelfTest();
  797.     if( rv != CKR_OK )
  798.         return rv;
  799.     /* RC4 Power-Up SelfTest(s). */
  800.     rv = pk11_fips_RC4_PowerUpSelfTest();
  801.     if( rv != CKR_OK )
  802.         return rv;
  803.     /* DES Power-Up SelfTest(s). */
  804.     rv = pk11_fips_DES_PowerUpSelfTest();
  805.     if( rv != CKR_OK )
  806.         return rv;
  807.     /* DES3 Power-Up SelfTest(s). */
  808.     rv = pk11_fips_DES3_PowerUpSelfTest();
  809.     if( rv != CKR_OK )
  810.         return rv;
  811.     /* MD2 Power-Up SelfTest(s). */
  812.     rv = pk11_fips_MD2_PowerUpSelfTest();
  813.     if( rv != CKR_OK )
  814.         return rv;
  815.     /* MD5 Power-Up SelfTest(s). */
  816.     rv = pk11_fips_MD5_PowerUpSelfTest();
  817.     if( rv != CKR_OK )
  818.         return rv;
  819.     /* SHA-1 Power-Up SelfTest(s). */
  820.     rv = pk11_fips_SHA1_PowerUpSelfTest();
  821.     if( rv != CKR_OK )
  822.         return rv;
  823.     /* RSA Power-Up SelfTest(s). */
  824.     rv = pk11_fips_RSA_PowerUpSelfTest();
  825.     if( rv != CKR_OK )
  826.         return rv;
  827.     /* DSA Power-Up SelfTest(s). */
  828.     rv = pk11_fips_DSA_PowerUpSelfTest();
  829.     if( rv != CKR_OK )
  830.         return rv;
  831.     /* Passed Power-Up SelfTest(s). */
  832.     return( CKR_OK );
  833. }