diff options
| author | Jakob Unterwurzacher | 2021-09-03 17:11:57 +0200 | 
|---|---|---|
| committer | Jakob Unterwurzacher | 2021-09-07 18:14:05 +0200 | 
| commit | 69d626b26f5a3f55c752b59af44710b992e2ab76 (patch) | |
| tree | 030d8b65aedb4a6d8e40c3f9c3552775a38d0669 /internal/stupidgcm | |
| parent | a3f5a8492a8bc62d8e2a639bd449b425efa66ce2 (diff) | |
stupidgcm: replace chacha20poly1305_seal with generic aead_seal
Diffstat (limited to 'internal/stupidgcm')
| -rw-r--r-- | internal/stupidgcm/Makefile | 2 | ||||
| -rw-r--r-- | internal/stupidgcm/chacha.c | 20 | ||||
| -rw-r--r-- | internal/stupidgcm/chacha.h | 17 | ||||
| -rw-r--r-- | internal/stupidgcm/stupidchacha.go | 9 | 
4 files changed, 37 insertions, 11 deletions
| diff --git a/internal/stupidgcm/Makefile b/internal/stupidgcm/Makefile index 19f9914..22e3b2e 100644 --- a/internal/stupidgcm/Makefile +++ b/internal/stupidgcm/Makefile @@ -4,4 +4,4 @@ gcc:  .PHONY: format  format: -	clang-format --style=WebKit -i *.c +	clang-format --style=WebKit -i *.c *.h diff --git a/internal/stupidgcm/chacha.c b/internal/stupidgcm/chacha.c index c85cf78..e188bfc 100644 --- a/internal/stupidgcm/chacha.c +++ b/internal/stupidgcm/chacha.c @@ -1,9 +1,8 @@ +#include "chacha.h"  #include <openssl/evp.h>  #include <stdio.h>  //#cgo pkg-config: libcrypto -extern void panic1(void); -  static void panic(const char* const 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 -int chacha20poly1305_seal( +int aead_seal( +    const enum aeadType cipherId,      const unsigned char* const plaintext,      const int plaintextLen,      const unsigned char* const authData, @@ -23,6 +23,18 @@ int chacha20poly1305_seal(      unsigned char* const ciphertext,      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"      EVP_CIPHER_CTX* ctx = EVP_CIPHER_CTX_new();      if (!ctx) { @@ -30,7 +42,7 @@ int chacha20poly1305_seal(      }      // 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");      } diff --git a/internal/stupidgcm/chacha.h b/internal/stupidgcm/chacha.h new file mode 100644 index 0000000..780350a --- /dev/null +++ b/internal/stupidgcm/chacha.h @@ -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); diff --git a/internal/stupidgcm/stupidchacha.go b/internal/stupidgcm/stupidchacha.go index 1f16a5e..18037c6 100644 --- a/internal/stupidgcm/stupidchacha.go +++ b/internal/stupidgcm/stupidchacha.go @@ -13,12 +13,8 @@ import (  /*  #include <openssl/evp.h> +#include "chacha.h"  #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" @@ -75,7 +71,8 @@ func (g *stupidChacha20poly1305) Seal(dst, iv, in, authData []byte) []byte {  		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.uchar)(&authData[0]),  		C.int(len(authData)), | 
