?? fipstest.c
字號:
/* * PKCS #11 FIPS Power-Up Self Test. * * The contents of this file are subject to the Mozilla Public * License Version 1.1 (the "License"); you may not use this file * except in compliance with the License. You may obtain a copy of * the License at http://www.mozilla.org/MPL/ * * Software distributed under the License is distributed on an "AS * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or * implied. See the License for the specific language governing * rights and limitations under the License. * * The Original Code is the Netscape security libraries. * * The Initial Developer of the Original Code is Netscape * Communications Corporation. Portions created by Netscape are * Copyright (C) 1994-2000 Netscape Communications Corporation. All * Rights Reserved. * * Contributor(s): * * Alternatively, the contents of this file may be used under the * terms of the GNU General Public License Version 2 or later (the * "GPL"), in which case the provisions of the GPL are applicable * instead of those above. If you wish to allow use of your * version of this file only under the terms of the GPL and not to * allow others to use your version of this file under the MPL, * indicate your decision by deleting the provisions above and * replace them with the notice and other provisions required by * the GPL. If you do not delete the provisions above, a recipient * may use your version of this file under either the MPL or the * GPL. * * $Id: fipstest.c,v 1.1 2000/03/31 19:25:43 relyea%netscape.com Exp $ */#include "softoken.h" /* Required for RC2-ECB, RC2-CBC, RC4, DES-ECB, */ /* DES-CBC, DES3-ECB, DES3-CBC, RSA */ /* and DSA. */#include "seccomon.h" /* Required for RSA and DSA. */#include "keylow.h" /* Required for RSA and DSA. */#include "pkcs11.h" /* Required for PKCS #11. */#include "secerr.h"/* FIPS preprocessor directives for RC2-ECB and RC2-CBC. */#define FIPS_RC2_KEY_LENGTH 5 /* 40-bits */#define FIPS_RC2_ENCRYPT_LENGTH 8 /* 64-bits */#define FIPS_RC2_DECRYPT_LENGTH 8 /* 64-bits *//* FIPS preprocessor directives for RC4. */#define FIPS_RC4_KEY_LENGTH 5 /* 40-bits */#define FIPS_RC4_ENCRYPT_LENGTH 8 /* 64-bits */#define FIPS_RC4_DECRYPT_LENGTH 8 /* 64-bits *//* FIPS preprocessor directives for DES-ECB and DES-CBC. */#define FIPS_DES_ENCRYPT_LENGTH 8 /* 64-bits */#define FIPS_DES_DECRYPT_LENGTH 8 /* 64-bits *//* FIPS preprocessor directives for DES3-CBC and DES3-ECB. */#define FIPS_DES3_ENCRYPT_LENGTH 8 /* 64-bits */#define FIPS_DES3_DECRYPT_LENGTH 8 /* 64-bits *//* FIPS preprocessor directives for MD2. */#define FIPS_MD2_HASH_MESSAGE_LENGTH 64 /* 512-bits *//* FIPS preprocessor directives for MD5. */#define FIPS_MD5_HASH_MESSAGE_LENGTH 64 /* 512-bits *//* FIPS preprocessor directives for SHA-1. */#define FIPS_SHA1_HASH_MESSAGE_LENGTH 64 /* 512-bits *//* FIPS preprocessor directives for RSA. */#define FIPS_RSA_TYPE siBuffer#define FIPS_RSA_PUBLIC_EXPONENT_LENGTH 1 /* 8-bits */#define FIPS_RSA_PRIVATE_VERSION_LENGTH 1 /* 8-bits */#define FIPS_RSA_MESSAGE_LENGTH 16 /* 128-bits */#define FIPS_RSA_COEFFICIENT_LENGTH 32 /* 256-bits */#define FIPS_RSA_PRIME0_LENGTH 33 /* 264-bits */#define FIPS_RSA_PRIME1_LENGTH 33 /* 264-bits */#define FIPS_RSA_EXPONENT0_LENGTH 33 /* 264-bits */#define FIPS_RSA_EXPONENT1_LENGTH 33 /* 264-bits */#define FIPS_RSA_PRIVATE_EXPONENT_LENGTH 64 /* 512-bits */#define FIPS_RSA_ENCRYPT_LENGTH 64 /* 512-bits */#define FIPS_RSA_DECRYPT_LENGTH 64 /* 512-bits */#define FIPS_RSA_CRYPTO_LENGTH 64 /* 512-bits */#define FIPS_RSA_SIGNATURE_LENGTH 64 /* 512-bits */#define FIPS_RSA_MODULUS_LENGTH 65 /* 520-bits *//* FIPS preprocessor directives for DSA. */#define FIPS_DSA_TYPE siBuffer#define FIPS_DSA_DIGEST_LENGTH 20 /* 160-bits */#define FIPS_DSA_SUBPRIME_LENGTH 20 /* 160-bits */#define FIPS_DSA_SIGNATURE_LENGTH 40 /* 320-bits */#define FIPS_DSA_PRIME_LENGTH 64 /* 512-bits */#define FIPS_DSA_BASE_LENGTH 64 /* 512-bits */static CK_RVpk11_fips_RC2_PowerUpSelfTest( void ){ /* RC2 Known Key (40-bits). */ static PRUint8 rc2_known_key[] = { "RSARC" }; /* RC2-CBC Known Initialization Vector (64-bits). */ static PRUint8 rc2_cbc_known_initialization_vector[] = {"Security"}; /* RC2 Known Plaintext (64-bits). */ static PRUint8 rc2_ecb_known_plaintext[] = {"Netscape"}; static PRUint8 rc2_cbc_known_plaintext[] = {"Netscape"}; /* RC2 Known Ciphertext (64-bits). */ static PRUint8 rc2_ecb_known_ciphertext[] = { 0x1a,0x71,0x33,0x54,0x8d,0x5c,0xd2,0x30}; static PRUint8 rc2_cbc_known_ciphertext[] = { 0xff,0x41,0xdb,0x94,0x8a,0x4c,0x33,0xb3}; /* RC2 variables. */ PRUint8 rc2_computed_ciphertext[FIPS_RC2_ENCRYPT_LENGTH]; PRUint8 rc2_computed_plaintext[FIPS_RC2_DECRYPT_LENGTH]; RC2Context * rc2_context; unsigned int rc2_bytes_encrypted; unsigned int rc2_bytes_decrypted; SECStatus rc2_status; /******************************************************/ /* RC2-ECB Single-Round Known Answer Encryption Test: */ /******************************************************/ rc2_context = RC2_CreateContext( rc2_known_key, FIPS_RC2_KEY_LENGTH, NULL, NSS_RC2, FIPS_RC2_KEY_LENGTH ); if( rc2_context == NULL ) return( CKR_HOST_MEMORY ); rc2_status = RC2_Encrypt( rc2_context, rc2_computed_ciphertext, &rc2_bytes_encrypted, FIPS_RC2_ENCRYPT_LENGTH, rc2_ecb_known_plaintext, FIPS_RC2_DECRYPT_LENGTH ); RC2_DestroyContext( rc2_context, PR_TRUE ); if( ( rc2_status != SECSuccess ) || ( rc2_bytes_encrypted != FIPS_RC2_ENCRYPT_LENGTH ) || ( PORT_Memcmp( rc2_computed_ciphertext, rc2_ecb_known_ciphertext, FIPS_RC2_ENCRYPT_LENGTH ) != 0 ) ) return( CKR_DEVICE_ERROR ); /******************************************************/ /* RC2-ECB Single-Round Known Answer Decryption Test: */ /******************************************************/ rc2_context = RC2_CreateContext( rc2_known_key, FIPS_RC2_KEY_LENGTH, NULL, NSS_RC2, FIPS_RC2_KEY_LENGTH ); if( rc2_context == NULL ) return( CKR_HOST_MEMORY ); rc2_status = RC2_Decrypt( rc2_context, rc2_computed_plaintext, &rc2_bytes_decrypted, FIPS_RC2_DECRYPT_LENGTH, rc2_ecb_known_ciphertext, FIPS_RC2_ENCRYPT_LENGTH ); RC2_DestroyContext( rc2_context, PR_TRUE ); if( ( rc2_status != SECSuccess ) || ( rc2_bytes_decrypted != FIPS_RC2_DECRYPT_LENGTH ) || ( PORT_Memcmp( rc2_computed_plaintext, rc2_ecb_known_plaintext, FIPS_RC2_DECRYPT_LENGTH ) != 0 ) ) return( CKR_DEVICE_ERROR ); /******************************************************/ /* RC2-CBC Single-Round Known Answer Encryption Test: */ /******************************************************/ rc2_context = RC2_CreateContext( rc2_known_key, FIPS_RC2_KEY_LENGTH, rc2_cbc_known_initialization_vector, NSS_RC2_CBC, FIPS_RC2_KEY_LENGTH ); if( rc2_context == NULL ) return( CKR_HOST_MEMORY ); rc2_status = RC2_Encrypt( rc2_context, rc2_computed_ciphertext, &rc2_bytes_encrypted, FIPS_RC2_ENCRYPT_LENGTH, rc2_cbc_known_plaintext, FIPS_RC2_DECRYPT_LENGTH ); RC2_DestroyContext( rc2_context, PR_TRUE ); if( ( rc2_status != SECSuccess ) || ( rc2_bytes_encrypted != FIPS_RC2_ENCRYPT_LENGTH ) || ( PORT_Memcmp( rc2_computed_ciphertext, rc2_cbc_known_ciphertext, FIPS_RC2_ENCRYPT_LENGTH ) != 0 ) ) return( CKR_DEVICE_ERROR ); /******************************************************/ /* RC2-CBC Single-Round Known Answer Decryption Test: */ /******************************************************/ rc2_context = RC2_CreateContext( rc2_known_key, FIPS_RC2_KEY_LENGTH, rc2_cbc_known_initialization_vector, NSS_RC2_CBC, FIPS_RC2_KEY_LENGTH ); if( rc2_context == NULL ) return( CKR_HOST_MEMORY ); rc2_status = RC2_Decrypt( rc2_context, rc2_computed_plaintext, &rc2_bytes_decrypted, FIPS_RC2_DECRYPT_LENGTH, rc2_cbc_known_ciphertext, FIPS_RC2_ENCRYPT_LENGTH ); RC2_DestroyContext( rc2_context, PR_TRUE ); if( ( rc2_status != SECSuccess ) || ( rc2_bytes_decrypted != FIPS_RC2_DECRYPT_LENGTH ) || ( PORT_Memcmp( rc2_computed_plaintext, rc2_ecb_known_plaintext, FIPS_RC2_DECRYPT_LENGTH ) != 0 ) ) return( CKR_DEVICE_ERROR ); return( CKR_OK );}static CK_RVpk11_fips_RC4_PowerUpSelfTest( void ){ /* RC4 Known Key (40-bits). */ static PRUint8 rc4_known_key[] = { "RSARC" }; /* RC4 Known Plaintext (64-bits). */ static PRUint8 rc4_known_plaintext[] = { "Netscape" }; /* RC4 Known Ciphertext (64-bits). */ static PRUint8 rc4_known_ciphertext[] = { 0x29,0x33,0xc7,0x9a,0x9d,0x6c,0x09,0xdd}; /* RC4 variables. */ PRUint8 rc4_computed_ciphertext[FIPS_RC4_ENCRYPT_LENGTH]; PRUint8 rc4_computed_plaintext[FIPS_RC4_DECRYPT_LENGTH]; RC4Context * rc4_context; unsigned int rc4_bytes_encrypted; unsigned int rc4_bytes_decrypted; SECStatus rc4_status; /**************************************************/ /* RC4 Single-Round Known Answer Encryption Test: */ /**************************************************/ rc4_context = RC4_CreateContext( rc4_known_key, FIPS_RC4_KEY_LENGTH ); if( rc4_context == NULL ) return( CKR_HOST_MEMORY ); rc4_status = RC4_Encrypt( rc4_context, rc4_computed_ciphertext, &rc4_bytes_encrypted, FIPS_RC4_ENCRYPT_LENGTH, rc4_known_plaintext, FIPS_RC4_DECRYPT_LENGTH ); RC4_DestroyContext( rc4_context, PR_TRUE ); if( ( rc4_status != SECSuccess ) || ( rc4_bytes_encrypted != FIPS_RC4_ENCRYPT_LENGTH ) || ( PORT_Memcmp( rc4_computed_ciphertext, rc4_known_ciphertext, FIPS_RC4_ENCRYPT_LENGTH ) != 0 ) ) return( CKR_DEVICE_ERROR ); /**************************************************/ /* RC4 Single-Round Known Answer Decryption Test: */ /**************************************************/ rc4_context = RC4_CreateContext( rc4_known_key, FIPS_RC4_KEY_LENGTH ); if( rc4_context == NULL ) return( CKR_HOST_MEMORY ); rc4_status = RC4_Decrypt( rc4_context, rc4_computed_plaintext, &rc4_bytes_decrypted, FIPS_RC4_DECRYPT_LENGTH, rc4_known_ciphertext, FIPS_RC4_ENCRYPT_LENGTH ); RC4_DestroyContext( rc4_context, PR_TRUE ); if( ( rc4_status != SECSuccess ) || ( rc4_bytes_decrypted != FIPS_RC4_DECRYPT_LENGTH ) || ( PORT_Memcmp( rc4_computed_plaintext, rc4_known_plaintext, FIPS_RC4_DECRYPT_LENGTH ) != 0 ) ) return( CKR_DEVICE_ERROR ); return( CKR_OK );}static CK_RVpk11_fips_DES_PowerUpSelfTest( void ){ /* DES Known Key (56-bits). */ static PRUint8 des_known_key[] = { "ANSI DES" }; /* DES-CBC Known Initialization Vector (64-bits). */ static PRUint8 des_cbc_known_initialization_vector[] = { "Security" }; /* DES Known Plaintext (64-bits). */ static PRUint8 des_ecb_known_plaintext[] = { "Netscape" }; static PRUint8 des_cbc_known_plaintext[] = { "Netscape" }; /* DES Known Ciphertext (64-bits). */ static PRUint8 des_ecb_known_ciphertext[] = { 0x26,0x14,0xe9,0xc3,0x28,0x80,0x50,0xb0}; static PRUint8 des_cbc_known_ciphertext[] = { 0x5e,0x95,0x94,0x5d,0x76,0xa2,0xd3,0x7d}; /* DES variables. */ PRUint8 des_computed_ciphertext[FIPS_DES_ENCRYPT_LENGTH]; PRUint8 des_computed_plaintext[FIPS_DES_DECRYPT_LENGTH]; DESContext * des_context; unsigned int des_bytes_encrypted; unsigned int des_bytes_decrypted; SECStatus des_status; /******************************************************/ /* DES-ECB Single-Round Known Answer Encryption Test: */ /******************************************************/ des_context = DES_CreateContext( des_known_key, NULL, NSS_DES, PR_TRUE ); if( des_context == NULL ) return( CKR_HOST_MEMORY ); des_status = DES_Encrypt( des_context, des_computed_ciphertext, &des_bytes_encrypted, FIPS_DES_ENCRYPT_LENGTH, des_ecb_known_plaintext, FIPS_DES_DECRYPT_LENGTH ); DES_DestroyContext( des_context, PR_TRUE ); if( ( des_status != SECSuccess ) || ( des_bytes_encrypted != FIPS_DES_ENCRYPT_LENGTH ) || ( PORT_Memcmp( des_computed_ciphertext, des_ecb_known_ciphertext, FIPS_DES_ENCRYPT_LENGTH ) != 0 ) ) return( CKR_DEVICE_ERROR ); /******************************************************/ /* DES-ECB Single-Round Known Answer Decryption Test: */ /******************************************************/ des_context = DES_CreateContext( des_known_key, NULL, NSS_DES, PR_FALSE ); if( des_context == NULL ) return( CKR_HOST_MEMORY );
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -