stupidgcm: replace chacha20poly1305_seal with generic aead_seal
This commit is contained in:
parent
a3f5a8492a
commit
69d626b26f
@ -4,4 +4,4 @@ gcc:
|
|||||||
|
|
||||||
.PHONY: format
|
.PHONY: format
|
||||||
format:
|
format:
|
||||||
clang-format --style=WebKit -i *.c
|
clang-format --style=WebKit -i *.c *.h
|
||||||
|
@ -1,9 +1,8 @@
|
|||||||
|
#include "chacha.h"
|
||||||
#include <openssl/evp.h>
|
#include <openssl/evp.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
//#cgo pkg-config: libcrypto
|
//#cgo pkg-config: libcrypto
|
||||||
|
|
||||||
extern void panic1(void);
|
|
||||||
|
|
||||||
static void panic(const char* const msg)
|
static void panic(const char* const msg)
|
||||||
{
|
{
|
||||||
fprintf(stderr, "panic in C code: %s\n", msg);
|
fprintf(stderr, "panic in C code: %s\n", msg);
|
||||||
@ -11,7 +10,8 @@ static void panic(const char* const msg)
|
|||||||
}
|
}
|
||||||
|
|
||||||
// https://wiki.openssl.org/index.php/EVP_Authenticated_Encryption_and_Decryption#Authenticated_Encryption_using_GCM_mode
|
// https://wiki.openssl.org/index.php/EVP_Authenticated_Encryption_and_Decryption#Authenticated_Encryption_using_GCM_mode
|
||||||
int chacha20poly1305_seal(
|
int aead_seal(
|
||||||
|
const enum aeadType cipherId,
|
||||||
const unsigned char* const plaintext,
|
const unsigned char* const plaintext,
|
||||||
const int plaintextLen,
|
const int plaintextLen,
|
||||||
const unsigned char* const authData,
|
const unsigned char* const authData,
|
||||||
@ -23,6 +23,18 @@ int chacha20poly1305_seal(
|
|||||||
unsigned char* const ciphertext,
|
unsigned char* const ciphertext,
|
||||||
const int ciphertextBufLen)
|
const int ciphertextBufLen)
|
||||||
{
|
{
|
||||||
|
const EVP_CIPHER* evpCipher = NULL;
|
||||||
|
switch (cipherId) {
|
||||||
|
case aeadTypeChacha:
|
||||||
|
evpCipher = EVP_chacha20_poly1305();
|
||||||
|
break;
|
||||||
|
case aeadTypeGcm:
|
||||||
|
evpCipher = EVP_aes_256_gcm();
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
panic("unknown cipherId");
|
||||||
|
}
|
||||||
|
|
||||||
// Create scratch space "context"
|
// Create scratch space "context"
|
||||||
EVP_CIPHER_CTX* ctx = EVP_CIPHER_CTX_new();
|
EVP_CIPHER_CTX* ctx = EVP_CIPHER_CTX_new();
|
||||||
if (!ctx) {
|
if (!ctx) {
|
||||||
@ -30,7 +42,7 @@ int chacha20poly1305_seal(
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Set cipher
|
// Set cipher
|
||||||
if (EVP_EncryptInit_ex(ctx, EVP_chacha20_poly1305(), NULL, NULL, NULL) != 1) {
|
if (EVP_EncryptInit_ex(ctx, evpCipher, NULL, NULL, NULL) != 1) {
|
||||||
panic("EVP_EncryptInit_ex set cipher failed");
|
panic("EVP_EncryptInit_ex set cipher failed");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
17
internal/stupidgcm/chacha.h
Normal file
17
internal/stupidgcm/chacha.h
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
enum aeadType {
|
||||||
|
aeadTypeChacha = 1,
|
||||||
|
aeadTypeGcm = 2,
|
||||||
|
};
|
||||||
|
|
||||||
|
int aead_seal(
|
||||||
|
const enum aeadType cipherId,
|
||||||
|
const unsigned char* const plaintext,
|
||||||
|
const int plaintextLen,
|
||||||
|
const unsigned char* const authData,
|
||||||
|
const int authDataLen,
|
||||||
|
const unsigned char* const key,
|
||||||
|
const int keyLen,
|
||||||
|
const unsigned char* const iv,
|
||||||
|
const int ivLen,
|
||||||
|
unsigned char* const ciphertext,
|
||||||
|
const int ciphertextBufLen);
|
@ -13,12 +13,8 @@ import (
|
|||||||
|
|
||||||
/*
|
/*
|
||||||
#include <openssl/evp.h>
|
#include <openssl/evp.h>
|
||||||
|
#include "chacha.h"
|
||||||
#cgo pkg-config: libcrypto
|
#cgo pkg-config: libcrypto
|
||||||
int chacha20poly1305_seal(const unsigned char * const plaintext, const int plaintextLen,
|
|
||||||
const unsigned char * const authData, const int authDataLen,
|
|
||||||
const unsigned char * const key, const int keyLen,
|
|
||||||
const unsigned char * const iv, const int ivLen,
|
|
||||||
unsigned char * const ciphertext, const int ciphertextBufLen);
|
|
||||||
*/
|
*/
|
||||||
import "C"
|
import "C"
|
||||||
|
|
||||||
@ -75,7 +71,8 @@ func (g *stupidChacha20poly1305) Seal(dst, iv, in, authData []byte) []byte {
|
|||||||
buf = make([]byte, outLen)
|
buf = make([]byte, outLen)
|
||||||
}
|
}
|
||||||
|
|
||||||
C.chacha20poly1305_seal((*C.uchar)(&in[0]),
|
C.aead_seal(C.aeadTypeChacha,
|
||||||
|
(*C.uchar)(&in[0]),
|
||||||
C.int(len(in)),
|
C.int(len(in)),
|
||||||
(*C.uchar)(&authData[0]),
|
(*C.uchar)(&authData[0]),
|
||||||
C.int(len(authData)),
|
C.int(len(authData)),
|
||||||
|
Loading…
Reference in New Issue
Block a user