密码库LibTomCrypt学习记录——(2.26)分组密码算法的工作模式——EAX代码示例

192 篇文章 58 订阅
40 篇文章 14 订阅

#include "tomcrypt.h"


typedef struct NistEAXTestVector_st{
    char *    name;//test vetor name
    int keylen;
    int noncelen; 
    int headerlen; 
    int msglen;
    
    unsigned char key[MAXBLOCKSIZE]; 
    unsigned char nonce[MAXBLOCKSIZE]; 
    unsigned char header[MAXBLOCKSIZE]; 
    unsigned char plaintext[MAXBLOCKSIZE];
    unsigned char ciphertext[MAXBLOCKSIZE]; 
    unsigned char tag[MAXBLOCKSIZE];
} NistEAXTestVector; 

static const NistEAXTestVector tests_eax[] = { 
    {
        "NULL message",
            16, 0, 0, 0,
            /* key */
        { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
        0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f },
        /* nonce */
        { 0 },
        /* header */
        { 0 },
        /* plaintext */
        { 0 },
        /* ciphertext */
        { 0 },
        /* tag */
        { 0x9a, 0xd0, 0x7e, 0x7d, 0xbf, 0xf3, 0x01, 0xf5,
        0x05, 0xde, 0x59, 0x6b, 0x96, 0x15, 0xdf, 0xff }
    },
        
    {
        "test with nonce",
            16, 16, 0, 0,
            /* key */
        { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
        0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f },
        /* nonce */
        { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
        0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f },
        /* header */
        { 0 },
        /* plaintext */
        { 0 },
        /* ciphertext */
        { 0 },
        /* tag */
        { 0x1c, 0xe1, 0x0d, 0x3e, 0xff, 0xd4, 0xca, 0xdb,
        0xe2, 0xe4, 0x4b, 0x58, 0xd6, 0x0a, 0xb9, 0xec }
    },
        
        /*   */
    {
        "test with header [no nonce]",
            16, 0, 16, 0,
            /* key */
        { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
        0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f },
        /* nonce */
        { 0 },
        /* header */
        { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
        0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f },
        /* plaintext */
        { 0 },
        /* ciphertext */
        { 0 },
        /* tag */
        { 0x3a, 0x69, 0x8f, 0x7a, 0x27, 0x0e, 0x51, 0xb0,
        0xf6, 0x5b, 0x3d, 0x3e, 0x47, 0x19, 0x3c, 0xff }
    },
        
        /*  */
    {
        "test with header + nonce + plaintext",
            16, 16, 16, 32,
            /* key */
        { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
        0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f },
        /* nonce */
        { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
        0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f },  
        /* header */
        { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
        0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f },
        /* plaintext */
        { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
        0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
        0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
        0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f },
        /* ciphertext */
        { 0x29, 0xd8, 0x78, 0xd1, 0xa3, 0xbe, 0x85, 0x7b,
        0x6f, 0xb8, 0xc8, 0xea, 0x59, 0x50, 0xa7, 0x78,
        0x33, 0x1f, 0xbf, 0x2c, 0xcf, 0x33, 0x98, 0x6f,
        0x35, 0xe8, 0xcf, 0x12, 0x1d, 0xcb, 0x30, 0xbc },
        /* tag */
        { 0x4f, 0xbe, 0x03, 0x38, 0xbe, 0x1c, 0x8c, 0x7e,
        0x1d, 0x7a, 0xe7, 0xe4, 0x5b, 0x92, 0xc5, 0x87 }
    },
        
        /*  */
    {
        "test with header + nonce + plaintext [not even sizes!]",
            16, 15, 14, 29,
            /* key */
        { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
        0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f },
        /* nonce */
        { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
        0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e },  
        /* header */
        { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
        0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d },
        /* plaintext */
        { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
        0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
        0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
        0x18, 0x19, 0x1a, 0x1b, 0x1c },
        /* ciphertext */
        { 0xdd, 0x25, 0xc7, 0x54, 0xc5, 0xb1, 0x7c, 0x59,
        0x28, 0xb6, 0x9b, 0x73, 0x15, 0x5f, 0x7b, 0xb8,
        0x88, 0x8f, 0xaf, 0x37, 0x09, 0x1a, 0xd9, 0x2c,
        0x8a, 0x24, 0xdb, 0x86, 0x8b },
        /* tag */
        { 0x0d, 0x1a, 0x14, 0xe5, 0x22, 0x24, 0xff, 0xd2,
        0x3a, 0x05, 0xfa, 0x02, 0xcd, 0xef, 0x52, 0xda }
    },
        
        /* Vectors from Brian Gladman */
        
    {
        "Vectors from Brian Gladman",
            16, 16, 8, 0,
            /* key */
        { 0x23, 0x39, 0x52, 0xde, 0xe4, 0xd5, 0xed, 0x5f,
        0x9b, 0x9c, 0x6d, 0x6f, 0xf8, 0x0f, 0xf4, 0x78 },
        /* nonce */
        { 0x62, 0xec, 0x67, 0xf9, 0xc3, 0xa4, 0xa4, 0x07,
        0xfc, 0xb2, 0xa8, 0xc4, 0x90, 0x31, 0xa8, 0xb3 },
        /* header */
        { 0x6b, 0xfb, 0x91, 0x4f, 0xd0, 0x7e, 0xae, 0x6b },
        /* PT */
        { 0x00 },
        /* CT */
        { 0x00 },
        /* tag */
        { 0xe0, 0x37, 0x83, 0x0e, 0x83, 0x89, 0xf2, 0x7b,
        0x02, 0x5a, 0x2d, 0x65, 0x27, 0xe7, 0x9d, 0x01 }
    },
        
    {
        "",
            16, 16, 8, 2,
            /* key */ 
        { 0x91, 0x94, 0x5d, 0x3f, 0x4d, 0xcb, 0xee, 0x0b,
        0xf4, 0x5e, 0xf5, 0x22, 0x55, 0xf0, 0x95, 0xa4 },
        /* nonce */
        { 0xbe, 0xca, 0xf0, 0x43, 0xb0, 0xa2, 0x3d, 0x84,
        0x31, 0x94, 0xba, 0x97, 0x2c, 0x66, 0xde, 0xbd },
        /* header */
        { 0xfa, 0x3b, 0xfd, 0x48, 0x06, 0xeb, 0x53, 0xfa },
        /* PT */
        { 0xf7, 0xfb },
        /* CT */
        { 0x19, 0xdd },
        /* tag */
        { 0x5c, 0x4c, 0x93, 0x31, 0x04, 0x9d, 0x0b, 0xda,
        0xb0, 0x27, 0x74, 0x08, 0xf6, 0x79, 0x67, 0xe5 }
    },
        
    {
        "",
            16, 16, 8, 5,
            /* key */
        { 0x01, 0xf7, 0x4a, 0xd6, 0x40, 0x77, 0xf2, 0xe7,
        0x04, 0xc0, 0xf6, 0x0a, 0xda, 0x3d, 0xd5, 0x23 },
        /* nonce */
        { 0x70, 0xc3, 0xdb, 0x4f, 0x0d, 0x26, 0x36, 0x84,
        0x00, 0xa1, 0x0e, 0xd0, 0x5d, 0x2b, 0xff, 0x5e },
        /* header */
        { 0x23, 0x4a, 0x34, 0x63, 0xc1, 0x26, 0x4a, 0xc6 },
        /* PT */
        { 0x1a, 0x47, 0xcb, 0x49, 0x33 },
        /* CT */
        { 0xd8, 0x51, 0xd5, 0xba, 0xe0 },
        /* Tag */
        { 0x3a, 0x59, 0xf2, 0x38, 0xa2, 0x3e, 0x39, 0x19,
        0x9d, 0xc9, 0x26, 0x66, 0x26, 0xc4, 0x0f, 0x80 }
    }   
    
};

//

int Test_EAX_AES(void)

    int err, x, idx, res, flag;
    unsigned long len;
    unsigned char outct[MAXBLOCKSIZE], outtag[MAXBLOCKSIZE];
    
    if ( register_cipher (&aes_desc) != CRYPT_OK ) { return CRYPT_INVALID_CIPHER; }
    if ((idx = find_cipher("aes")) == -1) { return CRYPT_NOP; }
    
    printf("Test EAX Begin!\n");    
    for (x = 0; x < (int)(sizeof(tests_eax)/sizeof(tests_eax[0])); x++)
    {
        len = sizeof(outtag);
        if ((err = eax_encrypt_authenticate_memory(idx, tests_eax[x].key, tests_eax[x].keylen,
            tests_eax[x].nonce, tests_eax[x].noncelen, tests_eax[x].header, tests_eax[x].headerlen,
            tests_eax[x].plaintext, tests_eax[x].msglen, outct, outtag, &len)) != CRYPT_OK) 
        {
//            return err;
        }
        
        flag = XMEMCMP(outct, tests_eax[x].ciphertext, tests_eax[x].msglen) || XMEMCMP(outtag, tests_eax[x].tag, len);
        if ( flag ) 
        {
//            return CRYPT_FAIL_TESTVECTOR;
        }
        
        /* test decrypt */
        if ((err = eax_decrypt_verify_memory(idx, tests_eax[x].key, tests_eax[x].keylen,
            tests_eax[x].nonce, tests_eax[x].noncelen, tests_eax[x].header, tests_eax[x].headerlen,
            outct, tests_eax[x].msglen, outct, outtag, len, &res)) != CRYPT_OK) 
        {
//            return err;
        }
        
        flag = (res != 1) || XMEMCMP(outct, tests_eax[x].plaintext, tests_eax[x].msglen);
        if (flag) 
        {
//            return CRYPT_FAIL_TESTVECTOR;
        }

        printf("Test Vector %d: %s\n", x, flag ? "fail!" : "pass!" );
            
    }
    printf("Test EAX End!\n\n");
    return CRYPT_OK;
}
 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
#ifndef TOMCRYPT_H_ #define TOMCRYPT_H_ #include <assert.h> #include <stdio.h> #include <string.h> #include <stdlib.h> #include <time.h> #include <ctype.h> #include <limits.h> /* use configuration data */ #include <tomcrypt_custom.h> #ifdef __cplusplus extern "C" { #endif /* version */ #define CRYPT 0x0116 #define SCRYPT "1.16" /* max size of either a cipher/hash block or symmetric key [largest of the two] */ #define MAXBLOCKSIZE 128 /* descriptor table size */ /* Dropbear change - this should be smaller, saves some size */ #define TAB_SIZE 4 /* error codes [will be expanded in future releases] */ enum { CRYPT_OK=0, /* Result OK */ CRYPT_ERROR, /* Generic Error */ CRYPT_NOP, /* Not a failure but no operation was performed */ CRYPT_INVALID_KEYSIZE, /* Invalid key size given */ CRYPT_INVALID_ROUNDS, /* Invalid number of rounds */ CRYPT_FAIL_TESTVECTOR, /* Algorithm failed test vectors */ CRYPT_BUFFER_OVERFLOW, /* Not enough space for output */ CRYPT_INVALID_PACKET, /* Invalid input packet given */ CRYPT_INVALID_PRNGSIZE, /* Invalid number of bits for a PRNG */ CRYPT_ERROR_READPRNG, /* Could not read enough from PRNG */ CRYPT_INVALID_CIPHER, /* Invalid cipher specified */ CRYPT_INVALID_HASH, /* Invalid hash specified */ CRYPT_INVALID_PRNG, /* Invalid PRNG specified */ CRYPT_MEM, /* Out of memory */ CRYPT_PK_TYPE_MISMATCH, /* Not equivalent types of PK keys */ CRYPT_PK_NOT_PRIVATE, /* Requires a private PK key */ CRYPT_INVALID_ARG, /* Generic invalid argument */ CRYPT_FILE_NOTFOUND, /* File Not Found */ CRYPT_PK_INVALID_TYPE, /* Invalid type of PK key */ CRYPT_PK_INVALID_SYSTEM,/* Invalid PK system specified */ CRYPT_PK_DUP, /* Duplicate key already in key ring */ CRYPT_PK_NOT_FOUND, /* Key not found in keyring */ CRYPT_PK_INVALID_SIZE, /* Invalid size input for PK parameters */ CRYPT_INVALID_PRIME_SIZE,/* Invalid size of prime requested */ CRYPT_PK_INVALID_PADDING /* Invalid padding on input */ }; #include <tomcrypt_cfg.h> #include <tomcrypt_macros.h> #include <tomcrypt_cipher.h> #include <tomcrypt_hash.h> #include <tomcrypt_mac.h> #include <tomcrypt_prng.h> #include <tomcrypt_pk.h> #include <tomcrypt_math.h> #include <tomcrypt_misc.h> #include <tomcrypt_argchk.h> #include <tomcrypt_pkcs.h> #ifdef __cplusplus } #endif #endif /* TOMCRYPT_H_ */ /* $Source: /cvs/libtom/libtomcrypt/src/headers/tomcrypt.h,v $ */ /* $Revision: 1.20 $ */ /* $Date: 2006/11/26 01:45:14 $ */
密码学的高级应用包括数字签名、加密通信、安全多方计算等。以下是相关代码示例: 1. 数字签名(使用RSA算法): ```python from Crypto.Signature import pkcs1_15 from Crypto.Hash import SHA256 from Crypto.PublicKey import RSA def sign_message(private_key, message): key = RSA.import_key(private_key) h = SHA256.new(message.encode('utf-8')) signer = pkcs1_15.new(key) signature = signer.sign(h) return signature def verify_signature(public_key, message, signature): key = RSA.import_key(public_key) h = SHA256.new(message.encode('utf-8')) verifier = pkcs1_15.new(key) try: verifier.verify(h, signature) return True except (ValueError, TypeError): return False # 使用示例 private_key = """-----BEGIN RSA PRIVATE KEY----- ... -----END RSA PRIVATE KEY-----""" public_key = """-----BEGIN RSA PUBLIC KEY----- ... -----END RSA PUBLIC KEY-----""" message = "Hello World!" signature = sign_message(private_key, message) print("Signature:", signature) valid = verify_signature(public_key, message, signature) print("Valid Signature:", valid) ``` 2. 加密通信(使用AES算法): ```python from Crypto.Cipher import AES from Crypto.Random import get_random_bytes def encrypt_message(key, message): cipher = AES.new(key, AES.MODE_EAX) nonce = cipher.nonce ciphertext, tag = cipher.encrypt_and_digest(message.encode('utf-8')) return nonce + ciphertext + tag def decrypt_message(key, encrypted_message): nonce = encrypted_message[:16] ciphertext = encrypted_message[16:-16] tag = encrypted_message[-16:] cipher = AES.new(key, AES.MODE_EAX, nonce) try: plaintext = cipher.decrypt_and_verify(ciphertext, tag) return plaintext.decode('utf-8') except ValueError: return None # 使用示例 key = get_random_bytes(16) message = "Hello World!" encrypted_message = encrypt_message(key, message) print("Encrypted Message:", encrypted_message) decrypted_message = decrypt_message(key, encrypted_message) print("Decrypted Message:", decrypted_message) ``` 3. 安全多方计算(使用秘密分享协议): ```python from secretsharing import PlaintextToHexSecretSharer def share_secret(secret, num_participants): shares = PlaintextToHexSecretSharer.split_secret(secret, num_participants) return shares def reconstruct_secret(shares): secret = PlaintextToHexSecretSharer.recover_secret(shares) return secret # 使用示例 secret = "Hello World!" num_participants = 5 shares = share_secret(secret, num_participants) print("Shares:", shares) reconstructed_secret = reconstruct_secret(shares) print("Reconstructed Secret:", reconstructed_secret) ``` 请注意,以上代码只是示例,并不包含完整的错误处理和安全性措施。在实际应用中,需要更加谨慎地处理密钥和数据,并采取适当的保护措施。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值